2 * Copyright (c) 2011 Jonathan Cameron
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.
8 * A reference industrial I/O driver to illustrate the functionality available.
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.
17 #include <linux/kernel.h>
18 #include <linux/slab.h>
19 #include <linux/module.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/events.h>
24 #include <linux/iio/buffer.h>
25 #include "iio_simple_dummy.h"
28 * A few elements needed to fake a bus for this driver
29 * Note instances parameter controls how many of these
30 * dummy devices are registered.
32 static unsigned instances = 1;
33 module_param(instances, int, 0);
35 /* Pointer array used to fake bus elements */
36 static struct iio_dev **iio_dummy_devs;
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";
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.
47 struct iio_dummy_accel_calibscale {
50 int regval; /* what would be written to hardware */
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.000013 */
59 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
62 * simple event - triggered when value rises above
65 static const struct iio_event_spec iio_dummy_event = {
66 .type = IIO_EV_TYPE_THRESH,
67 .dir = IIO_EV_DIR_RISING,
68 .mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE),
72 * simple step detect event - triggered when a step is detected
74 static const struct iio_event_spec step_detect_event = {
75 .type = IIO_EV_TYPE_CHANGE,
76 .dir = IIO_EV_DIR_NONE,
77 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
81 * simple transition event - triggered when the reported running confidence
82 * value rises above a threshold value
84 static const struct iio_event_spec iio_running_event = {
85 .type = IIO_EV_TYPE_THRESH,
86 .dir = IIO_EV_DIR_RISING,
87 .mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE),
91 * simple transition event - triggered when the reported walking confidence
92 * value falls under a threshold value
94 static const struct iio_event_spec iio_walking_event = {
95 .type = IIO_EV_TYPE_THRESH,
96 .dir = IIO_EV_DIR_FALLING,
97 .mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE),
102 * iio_dummy_channels - Description of available channels
104 * This array of structures tells the IIO core about what the device
105 * actually provides for a given channel.
107 static const struct iio_chan_spec iio_dummy_channels[] = {
108 /* indexed ADC channel in_voltage0_raw etc */
111 /* Channel has a numeric index of 0 */
114 /* What other information is available? */
115 .info_mask_separate =
118 * Raw (unscaled no bias removal etc) measurement
121 BIT(IIO_CHAN_INFO_RAW) |
124 * Offset for userspace to apply prior to scale
125 * when converting to standard units (microvolts)
127 BIT(IIO_CHAN_INFO_OFFSET) |
130 * Multipler for userspace to apply post offset
131 * when converting to standard units (microvolts)
133 BIT(IIO_CHAN_INFO_SCALE),
136 * The frequency in Hz at which the channels are sampled
138 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
139 /* The ordering of elements in the buffer via an enum */
140 .scan_index = voltage0,
141 .scan_type = { /* Description of storage in buffer */
142 .sign = 'u', /* unsigned */
143 .realbits = 13, /* 13 bits */
144 .storagebits = 16, /* 16 bits used for storage */
145 .shift = 0, /* zero shift */
147 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
148 .event_spec = &iio_dummy_event,
149 .num_event_specs = 1,
150 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
152 /* Differential ADC channel in_voltage1-voltage2_raw etc*/
157 * Indexing for differential channels uses channel
158 * for the positive part, channel2 for the negative.
164 * in_voltage1-voltage2_raw
165 * Raw (unscaled no bias removal etc) measurement
168 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
170 * in_voltage-voltage_scale
171 * Shared version of scale - shared by differential
172 * input channels of type IIO_VOLTAGE.
174 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
177 * The frequency in Hz at which the channels are sampled
179 .scan_index = diffvoltage1m2,
180 .scan_type = { /* Description of storage in buffer */
181 .sign = 's', /* signed */
182 .realbits = 12, /* 12 bits */
183 .storagebits = 16, /* 16 bits used for storage */
184 .shift = 0, /* zero shift */
187 /* Differential ADC channel in_voltage3-voltage4_raw etc*/
194 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
195 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
196 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
197 .scan_index = diffvoltage3m4,
206 * 'modified' (i.e. axis specified) acceleration channel
212 /* Channel 2 is use for modifiers */
213 .channel2 = IIO_MOD_X,
214 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
216 * Internal bias and gain correction values. Applied
217 * by the hardware or driver prior to userspace
218 * seeing the readings. Typically part of hardware
221 BIT(IIO_CHAN_INFO_CALIBSCALE) |
222 BIT(IIO_CHAN_INFO_CALIBBIAS),
223 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
224 .scan_index = accelx,
225 .scan_type = { /* Description of storage in buffer */
226 .sign = 's', /* signed */
227 .realbits = 16, /* 16 bits */
228 .storagebits = 16, /* 16 bits used for storage */
229 .shift = 0, /* zero shift */
233 * Convenience macro for timestamps. 4 is the index in
236 IIO_CHAN_SOFT_TIMESTAMP(4),
237 /* DAC channel out_voltage0_raw */
240 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
241 .scan_index = -1, /* No buffer support */
248 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_ENABLE) |
249 BIT(IIO_CHAN_INFO_CALIBHEIGHT),
250 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
251 .scan_index = -1, /* No buffer support */
252 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
253 .event_spec = &step_detect_event,
254 .num_event_specs = 1,
255 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
258 .type = IIO_ACTIVITY,
260 .channel2 = IIO_MOD_RUNNING,
261 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
262 .scan_index = -1, /* No buffer support */
263 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
264 .event_spec = &iio_running_event,
265 .num_event_specs = 1,
266 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
269 .type = IIO_ACTIVITY,
271 .channel2 = IIO_MOD_WALKING,
272 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
273 .scan_index = -1, /* No buffer support */
274 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
275 .event_spec = &iio_walking_event,
276 .num_event_specs = 1,
277 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
282 * iio_dummy_read_raw() - data read function.
283 * @indio_dev: the struct iio_dev associated with this device instance
284 * @chan: the channel whose data is to be read
285 * @val: first element of returned value (typically INT)
286 * @val2: second element of returned value (typically MICRO)
287 * @mask: what we actually want to read as per the info_mask_*
290 static int iio_dummy_read_raw(struct iio_dev *indio_dev,
291 struct iio_chan_spec const *chan,
296 struct iio_dummy_state *st = iio_priv(indio_dev);
299 mutex_lock(&st->lock);
301 case IIO_CHAN_INFO_RAW: /* magic value - channel value read */
302 switch (chan->type) {
305 /* Set integer part to cached value */
308 } else if (chan->differential) {
309 if (chan->channel == 1)
310 *val = st->differential_adc_val[0];
312 *val = st->differential_adc_val[1];
315 *val = st->single_ended_adc_val;
320 *val = st->accel_val;
327 case IIO_CHAN_INFO_PROCESSED:
328 switch (chan->type) {
334 switch (chan->channel2) {
335 case IIO_MOD_RUNNING:
336 *val = st->activity_running;
339 case IIO_MOD_WALKING:
340 *val = st->activity_walking;
351 case IIO_CHAN_INFO_OFFSET:
352 /* only single ended adc -> 7 */
356 case IIO_CHAN_INFO_SCALE:
357 switch (chan->type) {
359 switch (chan->differential) {
361 /* only single ended adc -> 0.001333 */
364 ret = IIO_VAL_INT_PLUS_MICRO;
367 /* all differential adc channels ->
371 ret = IIO_VAL_INT_PLUS_NANO;
378 case IIO_CHAN_INFO_CALIBBIAS:
379 /* only the acceleration axis - read from cache */
380 *val = st->accel_calibbias;
383 case IIO_CHAN_INFO_CALIBSCALE:
384 *val = st->accel_calibscale->val;
385 *val2 = st->accel_calibscale->val2;
386 ret = IIO_VAL_INT_PLUS_MICRO;
388 case IIO_CHAN_INFO_SAMP_FREQ:
391 ret = IIO_VAL_INT_PLUS_NANO;
393 case IIO_CHAN_INFO_ENABLE:
394 switch (chan->type) {
396 *val = st->steps_enabled;
403 case IIO_CHAN_INFO_CALIBHEIGHT:
404 switch (chan->type) {
417 mutex_unlock(&st->lock);
422 * iio_dummy_write_raw() - data write function.
423 * @indio_dev: the struct iio_dev associated with this device instance
424 * @chan: the channel whose data is to be written
425 * @val: first element of value to set (typically INT)
426 * @val2: second element of value to set (typically MICRO)
427 * @mask: what we actually want to write as per the info_mask_*
430 * Note that all raw writes are assumed IIO_VAL_INT and info mask elements
431 * are assumed to be IIO_INT_PLUS_MICRO unless the callback write_raw_get_fmt
432 * in struct iio_info is provided by the driver.
434 static int iio_dummy_write_raw(struct iio_dev *indio_dev,
435 struct iio_chan_spec const *chan,
442 struct iio_dummy_state *st = iio_priv(indio_dev);
445 case IIO_CHAN_INFO_RAW:
446 switch (chan->type) {
448 if (chan->output == 0)
451 /* Locking not required as writing single value */
452 mutex_lock(&st->lock);
454 mutex_unlock(&st->lock);
459 case IIO_CHAN_INFO_PROCESSED:
460 switch (chan->type) {
462 mutex_lock(&st->lock);
464 mutex_unlock(&st->lock);
471 switch (chan->channel2) {
472 case IIO_MOD_RUNNING:
473 st->activity_running = val;
475 case IIO_MOD_WALKING:
476 st->activity_walking = val;
485 case IIO_CHAN_INFO_CALIBSCALE:
486 mutex_lock(&st->lock);
487 /* Compare against table - hard matching here */
488 for (i = 0; i < ARRAY_SIZE(dummy_scales); i++)
489 if (val == dummy_scales[i].val &&
490 val2 == dummy_scales[i].val2)
492 if (i == ARRAY_SIZE(dummy_scales))
495 st->accel_calibscale = &dummy_scales[i];
496 mutex_unlock(&st->lock);
498 case IIO_CHAN_INFO_CALIBBIAS:
499 mutex_lock(&st->lock);
500 st->accel_calibbias = val;
501 mutex_unlock(&st->lock);
503 case IIO_CHAN_INFO_ENABLE:
504 switch (chan->type) {
506 mutex_lock(&st->lock);
507 st->steps_enabled = val;
508 mutex_unlock(&st->lock);
513 case IIO_CHAN_INFO_CALIBHEIGHT:
514 switch (chan->type) {
528 * Device type specific information.
530 static const struct iio_info iio_dummy_info = {
531 .driver_module = THIS_MODULE,
532 .read_raw = &iio_dummy_read_raw,
533 .write_raw = &iio_dummy_write_raw,
534 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
535 .read_event_config = &iio_simple_dummy_read_event_config,
536 .write_event_config = &iio_simple_dummy_write_event_config,
537 .read_event_value = &iio_simple_dummy_read_event_value,
538 .write_event_value = &iio_simple_dummy_write_event_value,
539 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
543 * iio_dummy_init_device() - device instance specific init
544 * @indio_dev: the iio device structure
546 * Most drivers have one of these to set up default values,
547 * reset the device to known state etc.
549 static int iio_dummy_init_device(struct iio_dev *indio_dev)
551 struct iio_dummy_state *st = iio_priv(indio_dev);
554 st->single_ended_adc_val = 73;
555 st->differential_adc_val[0] = 33;
556 st->differential_adc_val[1] = -34;
558 st->accel_calibbias = -7;
559 st->accel_calibscale = &dummy_scales[0];
561 st->activity_running = 98;
562 st->activity_walking = 4;
568 * iio_dummy_probe() - device instance probe
569 * @index: an id number for this instance.
571 * Arguments are bus type specific.
572 * I2C: iio_dummy_probe(struct i2c_client *client,
573 * const struct i2c_device_id *id)
574 * SPI: iio_dummy_probe(struct spi_device *spi)
576 static int iio_dummy_probe(int index)
579 struct iio_dev *indio_dev;
580 struct iio_dummy_state *st;
583 * Allocate an IIO device.
585 * This structure contains all generic state
586 * information about the device instance.
587 * It also has a region (accessed by iio_priv()
588 * for chip specific state information.
590 indio_dev = iio_device_alloc(sizeof(*st));
596 st = iio_priv(indio_dev);
597 mutex_init(&st->lock);
599 iio_dummy_init_device(indio_dev);
601 * With hardware: Set the parent device.
602 * indio_dev->dev.parent = &spi->dev;
603 * indio_dev->dev.parent = &client->dev;
607 * Make the iio_dev struct available to remove function.
609 * i2c_set_clientdata(client, indio_dev);
610 * spi_set_drvdata(spi, indio_dev);
612 iio_dummy_devs[index] = indio_dev;
616 * Set the device name.
618 * This is typically a part number and obtained from the module
620 * e.g. for i2c and spi:
621 * indio_dev->name = id->name;
622 * indio_dev->name = spi_get_device_id(spi)->name;
624 indio_dev->name = iio_dummy_part_number;
626 /* Provide description of available channels */
627 indio_dev->channels = iio_dummy_channels;
628 indio_dev->num_channels = ARRAY_SIZE(iio_dummy_channels);
631 * Provide device type specific interface functions and
634 indio_dev->info = &iio_dummy_info;
636 /* Specify that device provides sysfs type interfaces */
637 indio_dev->modes = INDIO_DIRECT_MODE;
639 ret = iio_simple_dummy_events_register(indio_dev);
641 goto error_free_device;
643 ret = iio_simple_dummy_configure_buffer(indio_dev);
645 goto error_unregister_events;
647 ret = iio_device_register(indio_dev);
649 goto error_unconfigure_buffer;
652 error_unconfigure_buffer:
653 iio_simple_dummy_unconfigure_buffer(indio_dev);
654 error_unregister_events:
655 iio_simple_dummy_events_unregister(indio_dev);
657 iio_device_free(indio_dev);
663 * iio_dummy_remove() - device instance removal function
664 * @index: device index.
666 * Parameters follow those of iio_dummy_probe for buses.
668 static int iio_dummy_remove(int index)
672 * Get a pointer to the device instance iio_dev structure
673 * from the bus subsystem. E.g.
674 * struct iio_dev *indio_dev = i2c_get_clientdata(client);
675 * struct iio_dev *indio_dev = spi_get_drvdata(spi);
677 struct iio_dev *indio_dev = iio_dummy_devs[index];
680 /* Unregister the device */
681 iio_device_unregister(indio_dev);
683 /* Device specific code to power down etc */
685 /* Buffered capture related cleanup */
686 iio_simple_dummy_unconfigure_buffer(indio_dev);
688 ret = iio_simple_dummy_events_unregister(indio_dev);
692 /* Free all structures */
693 iio_device_free(indio_dev);
700 * iio_dummy_init() - device driver registration
702 * Varies depending on bus type of the device. As there is no device
703 * here, call probe directly. For information on device registration
705 * Documentation/i2c/writing-clients
707 * Documentation/spi/spi-summary
709 static __init int iio_dummy_init(void)
713 if (instances > 10) {
719 iio_dummy_devs = kcalloc(instances, sizeof(*iio_dummy_devs),
721 /* Here we have no actual device so call probe */
722 for (i = 0; i < instances; i++) {
723 ret = iio_dummy_probe(i);
729 module_init(iio_dummy_init);
732 * iio_dummy_exit() - device driver removal
734 * Varies depending on bus type of the device.
735 * As there is no device here, call remove directly.
737 static __exit void iio_dummy_exit(void)
741 for (i = 0; i < instances; i++)
743 kfree(iio_dummy_devs);
745 module_exit(iio_dummy_exit);
747 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
748 MODULE_DESCRIPTION("IIO dummy driver");
749 MODULE_LICENSE("GPL v2");