summaryrefslogtreecommitdiff
path: root/cleopatre/linux-2.6.25.10-spc300/drivers/afe/ad986x.c
blob: 76279ceb8570a6dac217ba36864842ad69d44e32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
 * Driver for Analog Front End AD986x devices family
 *
 * (C) Copyright 2009 SPiDCOM Technologies
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <linux/types.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/proc_fs.h>
#include <linux/spi/spi.h>

#include <linux/afe.h>

static int ad986x_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);

/** Misc device structures */
static const struct file_operations ad986x_fops = {
	.owner = THIS_MODULE,
	.ioctl = ad986x_ioctl,
};
static struct miscdevice ad986x_miscdev = {
	.minor = AFE_MINOR,
	.name  = "ad986x",
	.fops  = &ad986x_fops,
};

/** spi_device to communicate with the AD986X device. */
static struct spi_device *spi_dev;

/**
 * Read a register of the AD986X device.
 *
 * \param  reg  the register.
 * \param[out]  val  the value of the register.
 * \return  0, on success.
 *          -EFAULT, on error.
 */
static int ad986x_read_reg(uint8_t reg, uint8_t *val)
{
    uint8_t command;

    //Create SPI tx command : 1b(r/w) + 2b(nbbytes) + 5b(addr)
    command = (uint8_t)((1 << 7) | (1 << 5) | (reg & 0x1F));

    //Send it and wait the response
    return spi_write_then_read(spi_dev, &command, 1, val, 1);
}

/**
 * Write to a register of the AD986X device.
 *
 * \param  reg  the register.
 * \param  val  the value to write.
 * \return  0, on success.
 *          -EFAULT, on error.
 */
static int ad986x_write_reg(uint8_t reg, uint8_t val)
{
    uint8_t command[2];
    uint8_t header;

    //Create SPI tx transfer : 1b(r/w) + 2b(nbbytes) + 5b(addr) + 8b(data)
    header = (uint8_t)((0 << 7) | (2 << 5) | (reg & 0x1F));

    if(spi_dev->mode & SPI_LSB_FIRST)
    {
        command[0] = val;
        command[1] = header;
    }
    else //MSB first need to swap
    {
        command[0] = header;
        command[1] = val;
    }

    //Send it
    return spi_write(spi_dev, &command[0], 2) ? -EFAULT : 0;
}

/**
 * Read a register of the AFE.
 *
 * \param  reg  the register.
 * \param[out]  val  the value of the register.
 * \return  0, on success.
 *          -EFAULT, on error.
 */
int afe_read_reg(uint8_t reg, uint8_t *val)
{
    return ad986x_read_reg(reg, val);
}
EXPORT_SYMBOL(afe_read_reg);

/**
 * Write to a register of the AFE.
 *
 * \param  reg  the register.
 * \param  val  the value to write.
 * \return  0, on success.
 *          -EFAULT, on error.
 */
int afe_write_reg(uint8_t reg, uint8_t val)
{
    return ad986x_write_reg(reg, val);
}
EXPORT_SYMBOL(afe_write_reg);

/**
 * IOCTL commands for AD986X device.
 *
 * \param  inode  inode structure.
 * \param  file  user exchange structure.
 * \param  cmd  command to execute.
 * \param  arg  arguments.
 * \return  error code.
 */
static int ad986x_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
    void __user *argp = (void __user *)arg;
    struct afe_info info;

    switch(cmd)
    {
    case AFEIOC_GETREG:
        //Restore register number given by user
        if(copy_from_user(&info, argp, sizeof(struct afe_info)))
            return -EFAULT;

        if (ad986x_read_reg(info.reg, &info.val))
            return -EFAULT;

        //Give the response to user
        return copy_to_user(argp, &info, sizeof(struct afe_info)) ? -EFAULT : 0;


    case AFEIOC_SETREG:
        //Restore register number and it value given by user
        if(copy_from_user(&info, argp, sizeof(struct afe_info)))
            return -EFAULT;

        return ad986x_write_reg(info.reg, info.val);

    default:
        return -EFAULT;
    }
}

static int ad986x_read_proc_helper(char **p, uint8_t reg)
{
    uint8_t val;

    int ret = ad986x_read_reg(reg, &val);

    if (ret)
        return ret;

    *p += sprintf(*p, "@0x%02x = 0x%02x\n", reg, val);

    return 0;
}

/**
 * Read the AFE configuration by /proc.
 *
 * \param  file  file structure.
 * \param  buffer  string pointer given by user.
 * \param  start  string pointer begin.
 * \param  offset  offset value.
 * \param  count  count parameter.
 * \param  eof  end of file.
 * \param  data  network device structure.
 * \return  new pointer position.
 */
static int ad986x_read_proc(char *buf, char **start, off_t offset, int count, int *eof, void *data)
{
    char *p;
    p = buf;

    p += sprintf(p, "\n%s:\n", "SPI port configuration");

    if (ad986x_read_proc_helper(&p, 0x00))
        goto error;

    p += sprintf(p, "\n%s:\n", "Power control registers");

    if (ad986x_read_proc_helper(&p, 0x01)
        || ad986x_read_proc_helper(&p, 0x02))
        goto error;

    p += sprintf(p, "\n%s:\n", "Half-Duplex power control");

    if (ad986x_read_proc_helper(&p, 0x03))
        goto error;

    p += sprintf(p, "\n%s:\n", "PLL clock multiplier/synthesizer control");

    if (ad986x_read_proc_helper(&p, 0x04)
        || ad986x_read_proc_helper(&p, 0x05)
        || ad986x_read_proc_helper(&p, 0x06))
        goto error;

    p += sprintf(p, "\n%s:\n", "RX path control");


    if (ad986x_read_proc_helper(&p, 0x07)
        || ad986x_read_proc_helper(&p, 0x08))
        goto error;

    p += sprintf(p, "\n%s:\n", "TX/RX path gain control");

    if (ad986x_read_proc_helper(&p, 0x09)
        || ad986x_read_proc_helper(&p, 0x0A))
        goto error;

    p += sprintf(p, "\n%s:\n", "TX and RX PGA control");

    if (ad986x_read_proc_helper(&p, 0x0B))
        goto error;

    p += sprintf(p, "\n%s:\n", "TX digital filter and interface");

    if (ad986x_read_proc_helper(&p, 0x0C))
        goto error;

    p += sprintf(p, "\n%s:\n", "RX interface and analog/digital loopback");

    if (ad986x_read_proc_helper(&p, 0x0D))
        goto error;

    p += sprintf(p, "\n%s:\n", "Digital output drive strength, TxDAC output and rev id");

    if (ad986x_read_proc_helper(&p, 0x0E)
        || ad986x_read_proc_helper(&p, 0x0F))
        goto error;

    p += sprintf(p, "\n%s:\n", "TX IAMP gain and BIAS control");

    if (ad986x_read_proc_helper(&p, 0x10)
        || ad986x_read_proc_helper(&p, 0x11)
        || ad986x_read_proc_helper(&p, 0x12)
        || ad986x_read_proc_helper(&p, 0x13))
        goto error;

    p += sprintf(p, "\n");

    *eof = 1;

    return p-buf+1;

error:
    return -1;
}

/**
 * Change the AFE configuration by /proc.
 *
 * \param  file  file structure.
 * \param  buffer  string pointer given by user.
 * \param  count  count parameter.
 * \param  data  network device structure.
 * \return  counter value.
 */
static int ad986x_write_proc(struct file *file, const char *buffer, unsigned long count, void *data)
{
    uint32_t reg;
    uint32_t value;

    if(sscanf(buffer, "0x%02x=0x%02x", &reg, &value) == 2)
    {
        if(reg <= 0x13)
        {
            ad986x_write_reg((uint8_t)reg, (uint8_t)value);
        }
    }
    else
    {
        printk("Syntax: reg=value example: 0x12=0xa3\n");
    }

    return count;
}

/**
 * Create the entry in /proc.
 *
 * \return  0, on success.
 *          -EFAULT, on error.
 */
static inline int ad986x_create_proc_entry(void)
{
    struct proc_dir_entry *entry;

   //Create proc entry for AFE configuration
    entry = create_proc_entry("driver/afe", 0, NULL);
    if (entry == NULL)
    {
        /* dev_err( , "error while registering proc_entry\n"); */
        return -EFAULT;
    }

    entry->read_proc  = ad986x_read_proc;
    entry->write_proc = ad986x_write_proc;

    return 0;
}

/**
 * Initialise AFE driver.
 * \param  spi  spi device structure.
 * \return  error code.
 */
static int __devinit ad986x_probe(struct spi_device *spi)
{
    int res;

    //Check arguments
    if(!spi)
        return -EFAULT;

    spi_dev = spi;

    //Set bits_per_word for this device
    spi_dev->bits_per_word = 8;

    //Check if someone has already register it
    if(ad986x_miscdev.parent)
        return -EBUSY;
    ad986x_miscdev.parent = &spi_dev->dev;

    //Register a Misc device for ioctl management
    if((res = misc_register(&ad986x_miscdev)) != 0)
        return res;

    if (ad986x_create_proc_entry())
    {
        dev_err(&spi_dev->dev, "failed to create proc entry\n");

        res = misc_deregister(&ad986x_miscdev);
        if (res)
        {
            dev_err(&spi_dev->dev, "failed to unregister misc driver %d\n", res);
        }
        return -EFAULT;
    }

    dev_info(&spi_dev->dev, "AFE Driver enabled\n");

    return 0;
}

/**
 * Uninitialise AFE driver.
 * \param  spi  spi device structure.
 * \return  error code.
 */
static int __devexit ad986x_remove(struct spi_device *spi)
{
    int res;

    remove_proc_entry("afe", proc_root_driver);

    //Unregister Misc device
    res = misc_deregister(&ad986x_miscdev);
    if(!res)
        ad986x_miscdev.parent = NULL;

    return res;
}

/** Module structure */
static struct spi_driver ad986x_driver = {
    .driver = {
        .name   = "ad986x",
        .bus    = &spi_bus_type,
        .owner  = THIS_MODULE,
    },
    .probe      = ad986x_probe,
    .suspend    = NULL,
    .resume     = NULL,
    .remove     = __devexit_p(ad986x_remove),
};

/**
 * Module initialization.
 * \return  error code.
 */
static int ad986x_init(void)
{
    //AD986x is driven by SPI
    return spi_register_driver(&ad986x_driver);
}

/**
 * Module uninitialization.
 * \return  error code.
 */
static void ad986x_exit(void)
{
    spi_unregister_driver(&ad986x_driver);
}

module_init(ad986x_init);
module_exit(ad986x_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("SPiDCOM Technologies");
MODULE_DESCRIPTION("SPI driver for AD986X chips family");
MODULE_ALIAS_MISCDEV(AFE_MINOR);