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>
20 #include <linux/moduleparam.h>
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"
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.
33 static unsigned instances = 1;
34 module_param(instances, int, 0);
36 /* Pointer array used to fake bus elements */
37 static struct iio_dev **iio_dummy_devs;
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";
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.
48 struct iio_dummy_accel_calibscale {
51 int regval; /* what would be written to hardware */
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 */
60 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
63 * simple event - triggered when value rises above
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),
73 * simple step detect event - triggered when a step is detected
75 static const struct iio_event_spec step_detect_event = {
76 .type = IIO_EV_TYPE_CHANGE,
77 .dir = IIO_EV_DIR_NONE,
78 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
82 * simple transition event - triggered when the reported running confidence
83 * value rises above a threshold value
85 static const struct iio_event_spec iio_running_event = {
86 .type = IIO_EV_TYPE_THRESH,
87 .dir = IIO_EV_DIR_RISING,
88 .mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE),
92 * simple transition event - triggered when the reported walking confidence
93 * value falls under a threshold value
95 static const struct iio_event_spec iio_walking_event = {
96 .type = IIO_EV_TYPE_THRESH,
97 .dir = IIO_EV_DIR_FALLING,
98 .mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE),
103 * iio_dummy_channels - Description of available channels
105 * This array of structures tells the IIO core about what the device
106 * actually provides for a given channel.
108 static const struct iio_chan_spec iio_dummy_channels[] = {
109 /* indexed ADC channel in_voltage0_raw etc */
112 /* Channel has a numeric index of 0 */
115 /* What other information is available? */
116 .info_mask_separate =
119 * Raw (unscaled no bias removal etc) measurement
122 BIT(IIO_CHAN_INFO_RAW) |
125 * Offset for userspace to apply prior to scale
126 * when converting to standard units (microvolts)
128 BIT(IIO_CHAN_INFO_OFFSET) |
131 * Multipler for userspace to apply post offset
132 * when converting to standard units (microvolts)
134 BIT(IIO_CHAN_INFO_SCALE),
137 * The frequency in Hz at which the channels are sampled
139 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
140 /* The ordering of elements in the buffer via an enum */
141 .scan_index = voltage0,
142 .scan_type = { /* Description of storage in buffer */
143 .sign = 'u', /* unsigned */
144 .realbits = 13, /* 13 bits */
145 .storagebits = 16, /* 16 bits used for storage */
146 .shift = 0, /* zero shift */
148 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
149 .event_spec = &iio_dummy_event,
150 .num_event_specs = 1,
151 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
153 /* Differential ADC channel in_voltage1-voltage2_raw etc*/
158 * Indexing for differential channels uses channel
159 * for the positive part, channel2 for the negative.
165 * in_voltage1-voltage2_raw
166 * Raw (unscaled no bias removal etc) measurement
169 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
171 * in_voltage-voltage_scale
172 * Shared version of scale - shared by differential
173 * input channels of type IIO_VOLTAGE.
175 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
178 * The frequency in Hz at which the channels are sampled
180 .scan_index = diffvoltage1m2,
181 .scan_type = { /* Description of storage in buffer */
182 .sign = 's', /* signed */
183 .realbits = 12, /* 12 bits */
184 .storagebits = 16, /* 16 bits used for storage */
185 .shift = 0, /* zero shift */
188 /* Differential ADC channel in_voltage3-voltage4_raw etc*/
195 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
196 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
197 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
198 .scan_index = diffvoltage3m4,
207 * 'modified' (i.e. axis specified) acceleration channel
213 /* Channel 2 is use for modifiers */
214 .channel2 = IIO_MOD_X,
215 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
217 * Internal bias and gain correction values. Applied
218 * by the hardware or driver prior to userspace
219 * seeing the readings. Typically part of hardware
222 BIT(IIO_CHAN_INFO_CALIBSCALE) |
223 BIT(IIO_CHAN_INFO_CALIBBIAS),
224 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
225 .scan_index = accelx,
226 .scan_type = { /* Description of storage in buffer */
227 .sign = 's', /* signed */
228 .realbits = 16, /* 16 bits */
229 .storagebits = 16, /* 16 bits used for storage */
230 .shift = 0, /* zero shift */
234 * Convenience macro for timestamps. 4 is the index in
237 IIO_CHAN_SOFT_TIMESTAMP(4),
238 /* DAC channel out_voltage0_raw */
241 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
242 .scan_index = -1, /* No buffer support */
249 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_ENABLE) |
250 BIT(IIO_CHAN_INFO_CALIBHEIGHT),
251 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
252 .scan_index = -1, /* No buffer support */
253 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
254 .event_spec = &step_detect_event,
255 .num_event_specs = 1,
256 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
259 .type = IIO_ACTIVITY,
261 .channel2 = IIO_MOD_RUNNING,
262 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
263 .scan_index = -1, /* No buffer support */
264 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
265 .event_spec = &iio_running_event,
266 .num_event_specs = 1,
267 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
270 .type = IIO_ACTIVITY,
272 .channel2 = IIO_MOD_WALKING,
273 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
274 .scan_index = -1, /* No buffer support */
275 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
276 .event_spec = &iio_walking_event,
277 .num_event_specs = 1,
278 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
283 * iio_dummy_read_raw() - data read function.
284 * @indio_dev: the struct iio_dev associated with this device instance
285 * @chan: the channel whose data is to be read
286 * @val: first element of returned value (typically INT)
287 * @val2: second element of returned value (typically MICRO)
288 * @mask: what we actually want to read as per the info_mask_*
291 static int iio_dummy_read_raw(struct iio_dev *indio_dev,
292 struct iio_chan_spec const *chan,
297 struct iio_dummy_state *st = iio_priv(indio_dev);
300 mutex_lock(&st->lock);
302 case IIO_CHAN_INFO_RAW: /* magic value - channel value read */
303 switch (chan->type) {
306 /* Set integer part to cached value */
309 } else if (chan->differential) {
310 if (chan->channel == 1)
311 *val = st->differential_adc_val[0];
313 *val = st->differential_adc_val[1];
316 *val = st->single_ended_adc_val;
321 *val = st->accel_val;
328 case IIO_CHAN_INFO_PROCESSED:
329 switch (chan->type) {
335 switch (chan->channel2) {
336 case IIO_MOD_RUNNING:
337 *val = st->activity_running;
340 case IIO_MOD_WALKING:
341 *val = st->activity_walking;
352 case IIO_CHAN_INFO_OFFSET:
353 /* only single ended adc -> 7 */
357 case IIO_CHAN_INFO_SCALE:
358 switch (chan->type) {
360 switch (chan->differential) {
362 /* only single ended adc -> 0.001333 */
365 ret = IIO_VAL_INT_PLUS_MICRO;
368 /* all differential adc channels ->
372 ret = IIO_VAL_INT_PLUS_NANO;
379 case IIO_CHAN_INFO_CALIBBIAS:
380 /* only the acceleration axis - read from cache */
381 *val = st->accel_calibbias;
384 case IIO_CHAN_INFO_CALIBSCALE:
385 *val = st->accel_calibscale->val;
386 *val2 = st->accel_calibscale->val2;
387 ret = IIO_VAL_INT_PLUS_MICRO;
389 case IIO_CHAN_INFO_SAMP_FREQ:
392 ret = IIO_VAL_INT_PLUS_NANO;
394 case IIO_CHAN_INFO_ENABLE:
395 switch (chan->type) {
397 *val = st->steps_enabled;
404 case IIO_CHAN_INFO_CALIBHEIGHT:
405 switch (chan->type) {
418 mutex_unlock(&st->lock);
423 * iio_dummy_write_raw() - data write function.
424 * @indio_dev: the struct iio_dev associated with this device instance
425 * @chan: the channel whose data is to be written
426 * @val: first element of value to set (typically INT)
427 * @val2: second element of value to set (typically MICRO)
428 * @mask: what we actually want to write as per the info_mask_*
431 * Note that all raw writes are assumed IIO_VAL_INT and info mask elements
432 * are assumed to be IIO_INT_PLUS_MICRO unless the callback write_raw_get_fmt
433 * in struct iio_info is provided by the driver.
435 static int iio_dummy_write_raw(struct iio_dev *indio_dev,
436 struct iio_chan_spec const *chan,
443 struct iio_dummy_state *st = iio_priv(indio_dev);
446 case IIO_CHAN_INFO_RAW:
447 switch (chan->type) {
449 if (chan->output == 0)
452 /* Locking not required as writing single value */
453 mutex_lock(&st->lock);
455 mutex_unlock(&st->lock);
460 case IIO_CHAN_INFO_PROCESSED:
461 switch (chan->type) {
463 mutex_lock(&st->lock);
465 mutex_unlock(&st->lock);
472 switch (chan->channel2) {
473 case IIO_MOD_RUNNING:
474 st->activity_running = val;
476 case IIO_MOD_WALKING:
477 st->activity_walking = val;
486 case IIO_CHAN_INFO_CALIBSCALE:
487 mutex_lock(&st->lock);
488 /* Compare against table - hard matching here */
489 for (i = 0; i < ARRAY_SIZE(dummy_scales); i++)
490 if (val == dummy_scales[i].val &&
491 val2 == dummy_scales[i].val2)
493 if (i == ARRAY_SIZE(dummy_scales))
496 st->accel_calibscale = &dummy_scales[i];
497 mutex_unlock(&st->lock);
499 case IIO_CHAN_INFO_CALIBBIAS:
500 mutex_lock(&st->lock);
501 st->accel_calibbias = val;
502 mutex_unlock(&st->lock);
504 case IIO_CHAN_INFO_ENABLE:
505 switch (chan->type) {
507 mutex_lock(&st->lock);
508 st->steps_enabled = val;
509 mutex_unlock(&st->lock);
514 case IIO_CHAN_INFO_CALIBHEIGHT:
515 switch (chan->type) {
529 * Device type specific information.
531 static const struct iio_info iio_dummy_info = {
532 .driver_module = THIS_MODULE,
533 .read_raw = &iio_dummy_read_raw,
534 .write_raw = &iio_dummy_write_raw,
535 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
536 .read_event_config = &iio_simple_dummy_read_event_config,
537 .write_event_config = &iio_simple_dummy_write_event_config,
538 .read_event_value = &iio_simple_dummy_read_event_value,
539 .write_event_value = &iio_simple_dummy_write_event_value,
540 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
544 * iio_dummy_init_device() - device instance specific init
545 * @indio_dev: the iio device structure
547 * Most drivers have one of these to set up default values,
548 * reset the device to known state etc.
550 static int iio_dummy_init_device(struct iio_dev *indio_dev)
552 struct iio_dummy_state *st = iio_priv(indio_dev);
555 st->single_ended_adc_val = 73;
556 st->differential_adc_val[0] = 33;
557 st->differential_adc_val[1] = -34;
559 st->accel_calibbias = -7;
560 st->accel_calibscale = &dummy_scales[0];
562 st->activity_running = 98;
563 st->activity_walking = 4;
569 * iio_dummy_probe() - device instance probe
570 * @index: an id number for this instance.
572 * Arguments are bus type specific.
573 * I2C: iio_dummy_probe(struct i2c_client *client,
574 * const struct i2c_device_id *id)
575 * SPI: iio_dummy_probe(struct spi_device *spi)
577 static int iio_dummy_probe(int index)
580 struct iio_dev *indio_dev;
581 struct iio_dummy_state *st;
584 * Allocate an IIO device.
586 * This structure contains all generic state
587 * information about the device instance.
588 * It also has a region (accessed by iio_priv()
589 * for chip specific state information.
591 indio_dev = iio_device_alloc(sizeof(*st));
592 if (indio_dev == NULL) {
597 st = iio_priv(indio_dev);
598 mutex_init(&st->lock);
600 iio_dummy_init_device(indio_dev);
602 * With hardware: Set the parent device.
603 * indio_dev->dev.parent = &spi->dev;
604 * indio_dev->dev.parent = &client->dev;
608 * Make the iio_dev struct available to remove function.
610 * i2c_set_clientdata(client, indio_dev);
611 * spi_set_drvdata(spi, indio_dev);
613 iio_dummy_devs[index] = indio_dev;
617 * Set the device name.
619 * This is typically a part number and obtained from the module
621 * e.g. for i2c and spi:
622 * indio_dev->name = id->name;
623 * indio_dev->name = spi_get_device_id(spi)->name;
625 indio_dev->name = iio_dummy_part_number;
627 /* Provide description of available channels */
628 indio_dev->channels = iio_dummy_channels;
629 indio_dev->num_channels = ARRAY_SIZE(iio_dummy_channels);
632 * Provide device type specific interface functions and
635 indio_dev->info = &iio_dummy_info;
637 /* Specify that device provides sysfs type interfaces */
638 indio_dev->modes = INDIO_DIRECT_MODE;
640 ret = iio_simple_dummy_events_register(indio_dev);
642 goto error_free_device;
644 ret = iio_simple_dummy_configure_buffer(indio_dev);
646 goto error_unregister_events;
648 ret = iio_device_register(indio_dev);
650 goto error_unconfigure_buffer;
653 error_unconfigure_buffer:
654 iio_simple_dummy_unconfigure_buffer(indio_dev);
655 error_unregister_events:
656 iio_simple_dummy_events_unregister(indio_dev);
658 iio_device_free(indio_dev);
664 * iio_dummy_remove() - device instance removal function
665 * @index: device index.
667 * Parameters follow those of iio_dummy_probe for buses.
669 static int iio_dummy_remove(int index)
673 * Get a pointer to the device instance iio_dev structure
674 * from the bus subsystem. E.g.
675 * struct iio_dev *indio_dev = i2c_get_clientdata(client);
676 * struct iio_dev *indio_dev = spi_get_drvdata(spi);
678 struct iio_dev *indio_dev = iio_dummy_devs[index];
681 /* Unregister the device */
682 iio_device_unregister(indio_dev);
684 /* Device specific code to power down etc */
686 /* Buffered capture related cleanup */
687 iio_simple_dummy_unconfigure_buffer(indio_dev);
689 ret = iio_simple_dummy_events_unregister(indio_dev);
693 /* Free all structures */
694 iio_device_free(indio_dev);
701 * iio_dummy_init() - device driver registration
703 * Varies depending on bus type of the device. As there is no device
704 * here, call probe directly. For information on device registration
706 * Documentation/i2c/writing-clients
708 * Documentation/spi/spi-summary
710 static __init int iio_dummy_init(void)
714 if (instances > 10) {
720 iio_dummy_devs = kcalloc(instances, sizeof(*iio_dummy_devs),
722 /* Here we have no actual device so call probe */
723 for (i = 0; i < instances; i++) {
724 ret = iio_dummy_probe(i);
730 module_init(iio_dummy_init);
733 * iio_dummy_exit() - device driver removal
735 * Varies depending on bus type of the device.
736 * As there is no device here, call remove directly.
738 static __exit void iio_dummy_exit(void)
742 for (i = 0; i < instances; i++)
744 kfree(iio_dummy_devs);
746 module_exit(iio_dummy_exit);
748 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
749 MODULE_DESCRIPTION("IIO dummy driver");
750 MODULE_LICENSE("GPL v2");