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