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/string.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 <linux/iio/sw_device.h>
27 #include "iio_simple_dummy.h"
29 static struct config_item_type iio_dummy_type = {
30 .ct_owner = THIS_MODULE,
34 * struct iio_dummy_accel_calibscale - realworld to register mapping
35 * @val: first value in read_raw - here integer part.
36 * @val2: second value in read_raw etc - here micro part.
37 * @regval: register value - magic device specific numbers.
39 struct iio_dummy_accel_calibscale {
42 int regval; /* what would be written to hardware */
45 static const struct iio_dummy_accel_calibscale dummy_scales[] = {
46 { 0, 100, 0x8 }, /* 0.000100 */
47 { 0, 133, 0x7 }, /* 0.000133 */
48 { 733, 13, 0x9 }, /* 733.000013 */
51 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
54 * simple event - triggered when value rises above
57 static const struct iio_event_spec iio_dummy_event = {
58 .type = IIO_EV_TYPE_THRESH,
59 .dir = IIO_EV_DIR_RISING,
60 .mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE),
64 * simple step detect event - triggered when a step is detected
66 static const struct iio_event_spec step_detect_event = {
67 .type = IIO_EV_TYPE_CHANGE,
68 .dir = IIO_EV_DIR_NONE,
69 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
73 * simple transition event - triggered when the reported running confidence
74 * value rises above a threshold value
76 static const struct iio_event_spec iio_running_event = {
77 .type = IIO_EV_TYPE_THRESH,
78 .dir = IIO_EV_DIR_RISING,
79 .mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE),
83 * simple transition event - triggered when the reported walking confidence
84 * value falls under a threshold value
86 static const struct iio_event_spec iio_walking_event = {
87 .type = IIO_EV_TYPE_THRESH,
88 .dir = IIO_EV_DIR_FALLING,
89 .mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE),
94 * iio_dummy_channels - Description of available channels
96 * This array of structures tells the IIO core about what the device
97 * actually provides for a given channel.
99 static const struct iio_chan_spec iio_dummy_channels[] = {
100 /* indexed ADC channel in_voltage0_raw etc */
103 /* Channel has a numeric index of 0 */
106 /* What other information is available? */
107 .info_mask_separate =
110 * Raw (unscaled no bias removal etc) measurement
113 BIT(IIO_CHAN_INFO_RAW) |
116 * Offset for userspace to apply prior to scale
117 * when converting to standard units (microvolts)
119 BIT(IIO_CHAN_INFO_OFFSET) |
122 * Multipler for userspace to apply post offset
123 * when converting to standard units (microvolts)
125 BIT(IIO_CHAN_INFO_SCALE),
128 * The frequency in Hz at which the channels are sampled
130 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
131 /* The ordering of elements in the buffer via an enum */
132 .scan_index = DUMMY_INDEX_VOLTAGE_0,
133 .scan_type = { /* Description of storage in buffer */
134 .sign = 'u', /* unsigned */
135 .realbits = 13, /* 13 bits */
136 .storagebits = 16, /* 16 bits used for storage */
137 .shift = 0, /* zero shift */
139 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
140 .event_spec = &iio_dummy_event,
141 .num_event_specs = 1,
142 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
144 /* Differential ADC channel in_voltage1-voltage2_raw etc*/
149 * Indexing for differential channels uses channel
150 * for the positive part, channel2 for the negative.
156 * in_voltage1-voltage2_raw
157 * Raw (unscaled no bias removal etc) measurement
160 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
162 * in_voltage-voltage_scale
163 * Shared version of scale - shared by differential
164 * input channels of type IIO_VOLTAGE.
166 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
169 * The frequency in Hz at which the channels are sampled
171 .scan_index = DUMMY_INDEX_DIFFVOLTAGE_1M2,
172 .scan_type = { /* Description of storage in buffer */
173 .sign = 's', /* signed */
174 .realbits = 12, /* 12 bits */
175 .storagebits = 16, /* 16 bits used for storage */
176 .shift = 0, /* zero shift */
179 /* Differential ADC channel in_voltage3-voltage4_raw etc*/
186 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
187 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
188 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
189 .scan_index = DUMMY_INDEX_DIFFVOLTAGE_3M4,
198 * 'modified' (i.e. axis specified) acceleration channel
204 /* Channel 2 is use for modifiers */
205 .channel2 = IIO_MOD_X,
206 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
208 * Internal bias and gain correction values. Applied
209 * by the hardware or driver prior to userspace
210 * seeing the readings. Typically part of hardware
213 BIT(IIO_CHAN_INFO_CALIBSCALE) |
214 BIT(IIO_CHAN_INFO_CALIBBIAS),
215 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
216 .scan_index = DUMMY_INDEX_ACCELX,
217 .scan_type = { /* Description of storage in buffer */
218 .sign = 's', /* signed */
219 .realbits = 16, /* 16 bits */
220 .storagebits = 16, /* 16 bits used for storage */
221 .shift = 0, /* zero shift */
225 * Convenience macro for timestamps. 4 is the index in
228 IIO_CHAN_SOFT_TIMESTAMP(4),
229 /* DAC channel out_voltage0_raw */
232 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
233 .scan_index = -1, /* No buffer support */
240 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_ENABLE) |
241 BIT(IIO_CHAN_INFO_CALIBHEIGHT),
242 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
243 .scan_index = -1, /* No buffer support */
244 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
245 .event_spec = &step_detect_event,
246 .num_event_specs = 1,
247 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
250 .type = IIO_ACTIVITY,
252 .channel2 = IIO_MOD_RUNNING,
253 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
254 .scan_index = -1, /* No buffer support */
255 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
256 .event_spec = &iio_running_event,
257 .num_event_specs = 1,
258 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
261 .type = IIO_ACTIVITY,
263 .channel2 = IIO_MOD_WALKING,
264 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
265 .scan_index = -1, /* No buffer support */
266 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
267 .event_spec = &iio_walking_event,
268 .num_event_specs = 1,
269 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
274 * iio_dummy_read_raw() - data read function.
275 * @indio_dev: the struct iio_dev associated with this device instance
276 * @chan: the channel whose data is to be read
277 * @val: first element of returned value (typically INT)
278 * @val2: second element of returned value (typically MICRO)
279 * @mask: what we actually want to read as per the info_mask_*
282 static int iio_dummy_read_raw(struct iio_dev *indio_dev,
283 struct iio_chan_spec const *chan,
288 struct iio_dummy_state *st = iio_priv(indio_dev);
291 mutex_lock(&st->lock);
293 case IIO_CHAN_INFO_RAW: /* magic value - channel value read */
294 switch (chan->type) {
297 /* Set integer part to cached value */
300 } else if (chan->differential) {
301 if (chan->channel == 1)
302 *val = st->differential_adc_val[0];
304 *val = st->differential_adc_val[1];
307 *val = st->single_ended_adc_val;
312 *val = st->accel_val;
319 case IIO_CHAN_INFO_PROCESSED:
320 switch (chan->type) {
326 switch (chan->channel2) {
327 case IIO_MOD_RUNNING:
328 *val = st->activity_running;
331 case IIO_MOD_WALKING:
332 *val = st->activity_walking;
343 case IIO_CHAN_INFO_OFFSET:
344 /* only single ended adc -> 7 */
348 case IIO_CHAN_INFO_SCALE:
349 switch (chan->type) {
351 switch (chan->differential) {
353 /* only single ended adc -> 0.001333 */
356 ret = IIO_VAL_INT_PLUS_MICRO;
359 /* all differential adc -> 0.000001344 */
362 ret = IIO_VAL_INT_PLUS_NANO;
369 case IIO_CHAN_INFO_CALIBBIAS:
370 /* only the acceleration axis - read from cache */
371 *val = st->accel_calibbias;
374 case IIO_CHAN_INFO_CALIBSCALE:
375 *val = st->accel_calibscale->val;
376 *val2 = st->accel_calibscale->val2;
377 ret = IIO_VAL_INT_PLUS_MICRO;
379 case IIO_CHAN_INFO_SAMP_FREQ:
382 ret = IIO_VAL_INT_PLUS_NANO;
384 case IIO_CHAN_INFO_ENABLE:
385 switch (chan->type) {
387 *val = st->steps_enabled;
394 case IIO_CHAN_INFO_CALIBHEIGHT:
395 switch (chan->type) {
408 mutex_unlock(&st->lock);
413 * iio_dummy_write_raw() - data write function.
414 * @indio_dev: the struct iio_dev associated with this device instance
415 * @chan: the channel whose data is to be written
416 * @val: first element of value to set (typically INT)
417 * @val2: second element of value to set (typically MICRO)
418 * @mask: what we actually want to write as per the info_mask_*
421 * Note that all raw writes are assumed IIO_VAL_INT and info mask elements
422 * are assumed to be IIO_INT_PLUS_MICRO unless the callback write_raw_get_fmt
423 * in struct iio_info is provided by the driver.
425 static int iio_dummy_write_raw(struct iio_dev *indio_dev,
426 struct iio_chan_spec const *chan,
433 struct iio_dummy_state *st = iio_priv(indio_dev);
436 case IIO_CHAN_INFO_RAW:
437 switch (chan->type) {
439 if (chan->output == 0)
442 /* Locking not required as writing single value */
443 mutex_lock(&st->lock);
445 mutex_unlock(&st->lock);
450 case IIO_CHAN_INFO_PROCESSED:
451 switch (chan->type) {
453 mutex_lock(&st->lock);
455 mutex_unlock(&st->lock);
462 switch (chan->channel2) {
463 case IIO_MOD_RUNNING:
464 st->activity_running = val;
466 case IIO_MOD_WALKING:
467 st->activity_walking = val;
476 case IIO_CHAN_INFO_CALIBSCALE:
477 mutex_lock(&st->lock);
478 /* Compare against table - hard matching here */
479 for (i = 0; i < ARRAY_SIZE(dummy_scales); i++)
480 if (val == dummy_scales[i].val &&
481 val2 == dummy_scales[i].val2)
483 if (i == ARRAY_SIZE(dummy_scales))
486 st->accel_calibscale = &dummy_scales[i];
487 mutex_unlock(&st->lock);
489 case IIO_CHAN_INFO_CALIBBIAS:
490 mutex_lock(&st->lock);
491 st->accel_calibbias = val;
492 mutex_unlock(&st->lock);
494 case IIO_CHAN_INFO_ENABLE:
495 switch (chan->type) {
497 mutex_lock(&st->lock);
498 st->steps_enabled = val;
499 mutex_unlock(&st->lock);
504 case IIO_CHAN_INFO_CALIBHEIGHT:
505 switch (chan->type) {
519 * Device type specific information.
521 static const struct iio_info iio_dummy_info = {
522 .driver_module = THIS_MODULE,
523 .read_raw = &iio_dummy_read_raw,
524 .write_raw = &iio_dummy_write_raw,
525 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
526 .read_event_config = &iio_simple_dummy_read_event_config,
527 .write_event_config = &iio_simple_dummy_write_event_config,
528 .read_event_value = &iio_simple_dummy_read_event_value,
529 .write_event_value = &iio_simple_dummy_write_event_value,
530 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
534 * iio_dummy_init_device() - device instance specific init
535 * @indio_dev: the iio device structure
537 * Most drivers have one of these to set up default values,
538 * reset the device to known state etc.
540 static int iio_dummy_init_device(struct iio_dev *indio_dev)
542 struct iio_dummy_state *st = iio_priv(indio_dev);
545 st->single_ended_adc_val = 73;
546 st->differential_adc_val[0] = 33;
547 st->differential_adc_val[1] = -34;
549 st->accel_calibbias = -7;
550 st->accel_calibscale = &dummy_scales[0];
552 st->activity_running = 98;
553 st->activity_walking = 4;
559 * iio_dummy_probe() - device instance probe
560 * @index: an id number for this instance.
562 * Arguments are bus type specific.
563 * I2C: iio_dummy_probe(struct i2c_client *client,
564 * const struct i2c_device_id *id)
565 * SPI: iio_dummy_probe(struct spi_device *spi)
567 static struct iio_sw_device *iio_dummy_probe(const char *name)
570 struct iio_dev *indio_dev;
571 struct iio_dummy_state *st;
572 struct iio_sw_device *swd;
574 swd = kzalloc(sizeof(*swd), GFP_KERNEL);
580 * Allocate an IIO device.
582 * This structure contains all generic state
583 * information about the device instance.
584 * It also has a region (accessed by iio_priv()
585 * for chip specific state information.
587 indio_dev = iio_device_alloc(sizeof(*st));
593 st = iio_priv(indio_dev);
594 mutex_init(&st->lock);
596 iio_dummy_init_device(indio_dev);
598 * With hardware: Set the parent device.
599 * indio_dev->dev.parent = &spi->dev;
600 * indio_dev->dev.parent = &client->dev;
604 * Make the iio_dev struct available to remove function.
606 * i2c_set_clientdata(client, indio_dev);
607 * spi_set_drvdata(spi, indio_dev);
609 swd->device = indio_dev;
612 * Set the device name.
614 * This is typically a part number and obtained from the module
616 * e.g. for i2c and spi:
617 * indio_dev->name = id->name;
618 * indio_dev->name = spi_get_device_id(spi)->name;
620 indio_dev->name = kstrdup(name, GFP_KERNEL);
622 /* Provide description of available channels */
623 indio_dev->channels = iio_dummy_channels;
624 indio_dev->num_channels = ARRAY_SIZE(iio_dummy_channels);
627 * Provide device type specific interface functions and
630 indio_dev->info = &iio_dummy_info;
632 /* Specify that device provides sysfs type interfaces */
633 indio_dev->modes = INDIO_DIRECT_MODE;
635 ret = iio_simple_dummy_events_register(indio_dev);
637 goto error_free_device;
639 ret = iio_simple_dummy_configure_buffer(indio_dev);
641 goto error_unregister_events;
643 ret = iio_device_register(indio_dev);
645 goto error_unconfigure_buffer;
647 iio_swd_group_init_type_name(swd, name, &iio_dummy_type);
650 error_unconfigure_buffer:
651 iio_simple_dummy_unconfigure_buffer(indio_dev);
652 error_unregister_events:
653 iio_simple_dummy_events_unregister(indio_dev);
655 iio_device_free(indio_dev);
663 * iio_dummy_remove() - device instance removal function
664 * @swd: pointer to software IIO device abstraction
666 * Parameters follow those of iio_dummy_probe for buses.
668 static int iio_dummy_remove(struct iio_sw_device *swd)
671 * Get a pointer to the device instance iio_dev structure
672 * from the bus subsystem. E.g.
673 * struct iio_dev *indio_dev = i2c_get_clientdata(client);
674 * struct iio_dev *indio_dev = spi_get_drvdata(spi);
676 struct iio_dev *indio_dev = swd->device;
678 /* Unregister the device */
679 iio_device_unregister(indio_dev);
681 /* Device specific code to power down etc */
683 /* Buffered capture related cleanup */
684 iio_simple_dummy_unconfigure_buffer(indio_dev);
686 iio_simple_dummy_events_unregister(indio_dev);
688 /* Free all structures */
689 kfree(indio_dev->name);
690 iio_device_free(indio_dev);
695 * module_iio_sw_device_driver() - device driver registration
697 * Varies depending on bus type of the device. As there is no device
698 * here, call probe directly. For information on device registration
700 * Documentation/i2c/writing-clients
702 * Documentation/spi/spi-summary
704 static const struct iio_sw_device_ops iio_dummy_device_ops = {
705 .probe = iio_dummy_probe,
706 .remove = iio_dummy_remove,
709 static struct iio_sw_device_type iio_dummy_device = {
711 .owner = THIS_MODULE,
712 .ops = &iio_dummy_device_ops,
715 module_iio_sw_device_driver(iio_dummy_device);
717 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
718 MODULE_DESCRIPTION("IIO dummy driver");
719 MODULE_LICENSE("GPL v2");