MIPS: BCM63XX: Introduce bcm_readq & bcm_writeq.
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / staging / iio / iio_simple_dummy.c
1 /**
2  * Copyright (c) 2011 Jonathan Cameron
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 as published by
6  * the Free Software Foundation.
7  *
8  * A reference industrial I/O driver to illustrate the functionality available.
9  *
10  * There are numerous real drivers to illustrate the finer points.
11  * The purpose of this driver is to provide a driver with far more comments
12  * and explanatory notes than any 'real' driver would have.
13  * Anyone starting out writing an IIO driver should first make sure they
14  * understand all of this driver except those bits specifically marked
15  * as being present to allow us to 'fake' the presence of hardware.
16  */
17 #include <linux/kernel.h>
18 #include <linux/slab.h>
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21
22 #include "iio.h"
23 #include "sysfs.h"
24 #include "buffer_generic.h"
25 #include "iio_simple_dummy.h"
26
27 /*
28  * A few elements needed to fake a bus for this driver
29  * Note instances parmeter controls how many of these
30  * dummy devices are registered.
31  */
32 static unsigned instances = 1;
33 module_param(instances, int, 0);
34
35 /* Pointer array used to fake bus elements */
36 static struct iio_dev **iio_dummy_devs;
37
38 /* Fake a name for the part number, usually obtained from the id table */
39 static const char *iio_dummy_part_number = "iio_dummy_part_no";
40
41 /**
42  * struct iio_dummy_accel_calibscale - realworld to register mapping
43  * @val: first value in read_raw - here integer part.
44  * @val2: second value in read_raw etc - here micro part.
45  * @regval: register value - magic device specific numbers.
46  */
47 struct iio_dummy_accel_calibscale {
48         int val;
49         int val2;
50         int regval; /* what would be written to hardware */
51 };
52
53 static const struct iio_dummy_accel_calibscale dummy_scales[] = {
54         { 0, 100, 0x8 }, /* 0.000100 */
55         { 0, 133, 0x7 }, /* 0.000133 */
56         { 733, 13, 0x9 }, /* 733.00013 */
57 };
58
59 /*
60  * iio_dummy_channels - Description of available channels
61  *
62  * This array of structures tells the IIO core about what the device
63  * actually provides for a given channel.
64  */
65 static struct iio_chan_spec iio_dummy_channels[] = {
66         /* indexed ADC channel in_voltage0_raw etc */
67         {
68                 .type = IIO_VOLTAGE,
69                 /* Channel has a numeric index of 0 */
70                 .indexed = 1,
71                 .channel = 0,
72                 /* What other information is available? */
73                 .info_mask =
74                 /*
75                  * in_voltage0_offset
76                  * Offset for userspace to apply prior to scale
77                  * when converting to standard units (microvolts)
78                  */
79                 (1 << IIO_CHAN_INFO_OFFSET_SEPARATE) |
80                 /*
81                  * in_voltage0_scale
82                  * Multipler for userspace to apply post offset
83                  * when converting to standard units (microvolts)
84                  */
85                 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
86                 /* The ordering of elements in the buffer via an enum */
87                 .scan_index = voltage0,
88                 .scan_type = { /* Description of storage in buffer */
89                         .sign = 'u', /* unsigned */
90                         .realbits = 13, /* 13 bits */
91                         .storagebits = 16, /* 16 bits used for storage */
92                         .shift = 0, /* zero shift */
93                 },
94 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
95                 /*
96                  * simple event - triggered when value rises above
97                  * a threshold
98                  */
99                 .event_mask = IIO_EV_BIT(IIO_EV_TYPE_THRESH,
100                                          IIO_EV_DIR_RISING),
101 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
102         },
103         /* Differential ADC channel in_voltage1-voltage2_raw etc*/
104         {
105                 .type = IIO_VOLTAGE,
106                 .differential = 1,
107                 /*
108                  * Indexing for differential channels uses channel
109                  * for the positive part, channel2 for the negative.
110                  */
111                 .indexed = 1,
112                 .channel = 1,
113                 .channel2 = 2,
114                 .info_mask =
115                 /*
116                  * in_voltage-voltage_scale
117                  * Shared version of scale - shared by differential
118                  * input channels of type IIO_VOLTAGE.
119                  */
120                 (1 << IIO_CHAN_INFO_SCALE_SHARED),
121                 .scan_index = diffvoltage1m2,
122                 .scan_type = { /* Description of storage in buffer */
123                         .sign = 's', /* signed */
124                         .realbits = 12, /* 12 bits */
125                         .storagebits = 16, /* 16 bits used for storage */
126                         .shift = 0, /* zero shift */
127                 },
128         },
129         /* Differential ADC channel in_voltage3-voltage4_raw etc*/
130         {
131                 .type = IIO_VOLTAGE,
132                 .differential = 1,
133                 .indexed = 1,
134                 .channel = 3,
135                 .channel2 = 4,
136                 .info_mask =
137                 (1 << IIO_CHAN_INFO_SCALE_SHARED),
138                 .scan_index = diffvoltage3m4,
139                 .scan_type = {
140                         .sign = 's',
141                         .realbits = 11,
142                         .storagebits = 16,
143                         .shift = 0,
144                 },
145         },
146         /*
147          * 'modified' (i.e. axis specified) acceleration channel
148          * in_accel_z_raw
149          */
150         {
151                 .type = IIO_ACCEL,
152                 .modified = 1,
153                 /* Channel 2 is use for modifiers */
154                 .channel2 = IIO_MOD_X,
155                 .info_mask =
156                 /*
157                  * Internal bias correction value. Applied
158                  * by the hardware or driver prior to userspace
159                  * seeing the readings. Typically part of hardware
160                  * calibration.
161                  */
162                 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE),
163                 .scan_index = accelx,
164                 .scan_type = { /* Description of storage in buffer */
165                         .sign = 's', /* signed */
166                         .realbits = 16, /* 12 bits */
167                         .storagebits = 16, /* 16 bits used for storage */
168                         .shift = 0, /* zero shift */
169                 },
170         },
171         /*
172          * Convenience macro for timestamps. 4 is the index in
173          * the buffer.
174          */
175         IIO_CHAN_SOFT_TIMESTAMP(4),
176         /* DAC channel out_voltage0_raw */
177         {
178                 .type = IIO_VOLTAGE,
179                 .output = 1,
180                 .indexed = 1,
181                 .channel = 0,
182         },
183 };
184
185 /**
186  * iio_dummy_read_raw() - data read function.
187  * @indio_dev:  the struct iio_dev associated with this device instance
188  * @chan:       the channel whose data is to be read
189  * @val:        first element of returned value (typically INT)
190  * @val2:       second element of returned value (typically MICRO)
191  * @mask:       what we actually want to read. 0 is the channel, everything else
192  *              is as per the info_mask in iio_chan_spec.
193  */
194 static int iio_dummy_read_raw(struct iio_dev *indio_dev,
195                               struct iio_chan_spec const *chan,
196                               int *val,
197                               int *val2,
198                               long mask)
199 {
200         struct iio_dummy_state *st = iio_priv(indio_dev);
201         int ret = -EINVAL;
202
203         mutex_lock(&st->lock);
204         switch (mask) {
205         case 0: /* magic value - channel value read */
206                 switch (chan->type) {
207                 case IIO_VOLTAGE:
208                         if (chan->output) {
209                                 /* Set integer part to cached value */
210                                 *val = st->dac_val;
211                                 ret = IIO_VAL_INT;
212                         } else if (chan->differential) {
213                                 if (chan->channel == 1)
214                                         *val = st->differential_adc_val[0];
215                                 else
216                                         *val = st->differential_adc_val[1];
217                                 ret = IIO_VAL_INT;
218                         } else {
219                                 *val = st->single_ended_adc_val;
220                                 ret = IIO_VAL_INT;
221                         }
222                         break;
223                 case IIO_ACCEL:
224                         *val = st->accel_val;
225                         ret = IIO_VAL_INT;
226                         break;
227                 default:
228                         break;
229                 }
230                 break;
231         case (1 << IIO_CHAN_INFO_OFFSET_SEPARATE):
232                 /* only single ended adc -> 7 */
233                 *val = 7;
234                 ret = IIO_VAL_INT;
235                 break;
236         case (1 << IIO_CHAN_INFO_SCALE_SEPARATE):
237                 /* only single ended adc -> 0.001333 */
238                 *val = 0;
239                 *val2 = 1333;
240                 ret = IIO_VAL_INT_PLUS_MICRO;
241                 break;
242         case (1 << IIO_CHAN_INFO_SCALE_SHARED):
243                 /* all differential adc channels -> 0.000001344 */
244                 *val = 0;
245                 *val2 = 1344;
246                 ret = IIO_VAL_INT_PLUS_NANO;
247                 break;
248         case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
249                 /* only the acceleration axis - read from cache */
250                 *val = st->accel_calibbias;
251                 ret = IIO_VAL_INT;
252                 break;
253         case (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE):
254                 *val = st->accel_calibscale->val;
255                 *val2 = st->accel_calibscale->val2;
256                 ret = IIO_VAL_INT_PLUS_MICRO;
257                 break;
258         default:
259                 break;
260         }
261         mutex_unlock(&st->lock);
262         return ret;
263 }
264
265 /**
266  * iio_dummy_write_raw() - data write function.
267  * @indio_dev:  the struct iio_dev associated with this device instance
268  * @chan:       the channel whose data is to be read
269  * @val:        first element of returned value (typically INT)
270  * @val2:       second element of returned value (typically MICRO)
271  * @mask:       what we actually want to read. 0 is the channel, everything else
272  *              is as per the info_mask in iio_chan_spec.
273  *
274  * Note that all raw writes are assumed IIO_VAL_INT and info mask elements
275  * are assumed to be IIO_INT_PLUS_MICRO unless the callback write_raw_get_fmt
276  * in struct iio_info is provided by the driver.
277  */
278 static int iio_dummy_write_raw(struct iio_dev *indio_dev,
279                                struct iio_chan_spec const *chan,
280                                int val,
281                                int val2,
282                                long mask)
283 {
284         int i;
285         int ret = 0;
286         struct iio_dummy_state *st = iio_priv(indio_dev);
287
288         switch (mask) {
289         case 0:
290                 if (chan->output == 0)
291                         return -EINVAL;
292
293                 /* Locking not required as writing single value */
294                 mutex_lock(&st->lock);
295                 st->dac_val = val;
296                 mutex_unlock(&st->lock);
297                 return 0;
298         case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
299                 mutex_lock(&st->lock);
300                 /* Compare against table - hard matching here */
301                 for (i = 0; i < ARRAY_SIZE(dummy_scales); i++)
302                         if (val == dummy_scales[i].val &&
303                             val2 == dummy_scales[i].val2)
304                                 break;
305                 if (i == ARRAY_SIZE(dummy_scales))
306                         ret = -EINVAL;
307                 else
308                         st->accel_calibscale = &dummy_scales[i];
309                 mutex_unlock(&st->lock);
310                 return ret;
311         default:
312                 return -EINVAL;
313         }
314 }
315
316 /*
317  * Device type specific information.
318  */
319 static const struct iio_info iio_dummy_info = {
320         .driver_module = THIS_MODULE,
321         .read_raw = &iio_dummy_read_raw,
322         .write_raw = &iio_dummy_write_raw,
323 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
324         .read_event_config = &iio_simple_dummy_read_event_config,
325         .write_event_config = &iio_simple_dummy_write_event_config,
326         .read_event_value = &iio_simple_dummy_read_event_value,
327         .write_event_value = &iio_simple_dummy_write_event_value,
328 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
329 };
330
331 /**
332  * iio_dummy_init_device() - device instance specific init
333  * @indio_dev: the iio device structure
334  *
335  * Most drivers have one of these to set up default values,
336  * reset the device to known state etc.
337  */
338 static int iio_dummy_init_device(struct iio_dev *indio_dev)
339 {
340         struct iio_dummy_state *st = iio_priv(indio_dev);
341
342         st->dac_val = 0;
343         st->single_ended_adc_val = 73;
344         st->differential_adc_val[0] = 33;
345         st->differential_adc_val[1] = -34;
346         st->accel_val = 34;
347         st->accel_calibbias = -7;
348         st->accel_calibscale = &dummy_scales[0];
349
350         return 0;
351 }
352
353 /**
354  * iio_dummy_probe() - device instance probe
355  * @index: an id number for this instance.
356  *
357  * Arguments are bus type specific.
358  * I2C: iio_dummy_probe(struct i2c_client *client,
359  *                      const struct i2c_device_id *id)
360  * SPI: iio_dummy_probe(struct spi_device *spi)
361  */
362 static int __devinit iio_dummy_probe(int index)
363 {
364         int ret;
365         struct iio_dev *indio_dev;
366         struct iio_dummy_state *st;
367
368         /*
369          * Allocate an IIO device.
370          *
371          * This structure contains all generic state
372          * information about the device instance.
373          * It also has a region (accessed by iio_priv()
374          * for chip specific state information.
375          */
376         indio_dev = iio_allocate_device(sizeof(*st));
377         if (indio_dev == NULL) {
378                 ret = -ENOMEM;
379                 goto error_ret;
380         }
381
382         st = iio_priv(indio_dev);
383         mutex_init(&st->lock);
384
385         iio_dummy_init_device(indio_dev);
386         /*
387          * With hardware: Set the parent device.
388          * indio_dev->dev.parent = &spi->dev;
389          * indio_dev->dev.parent = &client->dev;
390          */
391
392          /*
393          * Make the iio_dev struct available to remove function.
394          * Bus equivalents
395          * i2c_set_clientdata(client, indio_dev);
396          * spi_set_drvdata(spi, indio_dev);
397          */
398         iio_dummy_devs[index] = indio_dev;
399
400
401         /*
402          * Set the device name.
403          *
404          * This is typically a part number and obtained from the module
405          * id table.
406          * e.g. for i2c and spi:
407          *    indio_dev->name = id->name;
408          *    indio_dev->name = spi_get_device_id(spi)->name;
409          */
410         indio_dev->name = iio_dummy_part_number;
411
412         /* Provide description of available channels */
413         indio_dev->channels = iio_dummy_channels;
414         indio_dev->num_channels = ARRAY_SIZE(iio_dummy_channels);
415
416         /*
417          * Provide device type specific interface functions and
418          * constant data.
419          */
420         indio_dev->info = &iio_dummy_info;
421
422         /* Specify that device provides sysfs type interfaces */
423         indio_dev->modes = INDIO_DIRECT_MODE;
424
425         ret = iio_simple_dummy_events_register(indio_dev);
426         if (ret < 0)
427                 goto error_free_device;
428
429         /* Configure buffered capture support. */
430         ret = iio_simple_dummy_configure_buffer(indio_dev);
431         if (ret < 0)
432                 goto error_unregister_events;
433
434         /*
435          * Register the channels with the buffer, but avoid the output
436          * channel being registered by reducing the number of channels by 1.
437          */
438         ret = iio_buffer_register(indio_dev, iio_dummy_channels, 5);
439         if (ret < 0)
440                 goto error_unconfigure_buffer;
441
442         ret = iio_device_register(indio_dev);
443         if (ret < 0)
444                 goto error_unregister_buffer;
445
446         return 0;
447 error_unregister_buffer:
448         iio_buffer_unregister(indio_dev);
449 error_unconfigure_buffer:
450         iio_simple_dummy_unconfigure_buffer(indio_dev);
451 error_unregister_events:
452         iio_simple_dummy_events_unregister(indio_dev);
453 error_free_device:
454         /* Note free device should only be called, before registration
455          * has succeeded. */
456         iio_free_device(indio_dev);
457 error_ret:
458         return ret;
459 }
460
461 /**
462  * iio_dummy_remove() - device instance removal function
463  * @index: device index.
464  *
465  * Parameters follow those of iio_dummy_probe for buses.
466  */
467 static int iio_dummy_remove(int index)
468 {
469         int ret;
470         /*
471          * Get a pointer to the device instance iio_dev structure
472          * from the bus subsystem. E.g.
473          * struct iio_dev *indio_dev = i2c_get_clientdata(client);
474          * struct iio_dev *indio_dev = spi_get_drvdata(spi);
475          */
476         struct iio_dev *indio_dev = iio_dummy_devs[index];
477
478
479         /* Unregister the device */
480         iio_device_unregister(indio_dev);
481
482         /* Device specific code to power down etc */
483
484         /* Buffered capture related cleanup */
485         iio_buffer_unregister(indio_dev);
486         iio_simple_dummy_unconfigure_buffer(indio_dev);
487
488         ret = iio_simple_dummy_events_unregister(indio_dev);
489         if (ret)
490                 goto error_ret;
491
492         /* Free all structures */
493         iio_free_device(indio_dev);
494
495 error_ret:
496         return ret;
497 }
498
499 /**
500  * iio_dummy_init() -  device driver registration
501  *
502  * Varies depending on bus type of the device. As there is no device
503  * here, call probe directly. For information on device registration
504  * i2c:
505  * Documentation/i2c/writing-clients
506  * spi:
507  * Documentation/spi/spi-summary
508  */
509 static __init int iio_dummy_init(void)
510 {
511         int i, ret;
512         if (instances > 10) {
513                 instances = 1;
514                 return -EINVAL;
515         }
516         /* Fake a bus */
517         iio_dummy_devs = kzalloc(sizeof(*iio_dummy_devs)*instances, GFP_KERNEL);
518         /* Here we have no actual device so call probe */
519         for (i = 0; i < instances; i++) {
520                 ret = iio_dummy_probe(i);
521                 if (ret < 0)
522                         return ret;
523         }
524         return 0;
525 }
526 module_init(iio_dummy_init);
527
528 /**
529  * iio_dummy_exit() - device driver removal
530  *
531  * Varies depending on bus type of the device.
532  * As there is no device here, call remove directly.
533  */
534 static __exit void iio_dummy_exit(void)
535 {
536         int i;
537         for (i = 0; i < instances; i++)
538                 iio_dummy_remove(i);
539         kfree(iio_dummy_devs);
540 }
541 module_exit(iio_dummy_exit);
542
543 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
544 MODULE_DESCRIPTION("IIO dummy driver");
545 MODULE_LICENSE("GPL v2");