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
, uint
, 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
= DUMMY_INDEX_VOLTAGE_0
,
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
= DUMMY_INDEX_DIFFVOLTAGE_1M2
,
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
= DUMMY_INDEX_DIFFVOLTAGE_3M4
,
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
= DUMMY_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 -> 0.000001344 */
370 ret
= IIO_VAL_INT_PLUS_NANO
;
377 case IIO_CHAN_INFO_CALIBBIAS
:
378 /* only the acceleration axis - read from cache */
379 *val
= st
->accel_calibbias
;
382 case IIO_CHAN_INFO_CALIBSCALE
:
383 *val
= st
->accel_calibscale
->val
;
384 *val2
= st
->accel_calibscale
->val2
;
385 ret
= IIO_VAL_INT_PLUS_MICRO
;
387 case IIO_CHAN_INFO_SAMP_FREQ
:
390 ret
= IIO_VAL_INT_PLUS_NANO
;
392 case IIO_CHAN_INFO_ENABLE
:
393 switch (chan
->type
) {
395 *val
= st
->steps_enabled
;
402 case IIO_CHAN_INFO_CALIBHEIGHT
:
403 switch (chan
->type
) {
416 mutex_unlock(&st
->lock
);
421 * iio_dummy_write_raw() - data write function.
422 * @indio_dev: the struct iio_dev associated with this device instance
423 * @chan: the channel whose data is to be written
424 * @val: first element of value to set (typically INT)
425 * @val2: second element of value to set (typically MICRO)
426 * @mask: what we actually want to write as per the info_mask_*
429 * Note that all raw writes are assumed IIO_VAL_INT and info mask elements
430 * are assumed to be IIO_INT_PLUS_MICRO unless the callback write_raw_get_fmt
431 * in struct iio_info is provided by the driver.
433 static int iio_dummy_write_raw(struct iio_dev
*indio_dev
,
434 struct iio_chan_spec
const *chan
,
441 struct iio_dummy_state
*st
= iio_priv(indio_dev
);
444 case IIO_CHAN_INFO_RAW
:
445 switch (chan
->type
) {
447 if (chan
->output
== 0)
450 /* Locking not required as writing single value */
451 mutex_lock(&st
->lock
);
453 mutex_unlock(&st
->lock
);
458 case IIO_CHAN_INFO_PROCESSED
:
459 switch (chan
->type
) {
461 mutex_lock(&st
->lock
);
463 mutex_unlock(&st
->lock
);
470 switch (chan
->channel2
) {
471 case IIO_MOD_RUNNING
:
472 st
->activity_running
= val
;
474 case IIO_MOD_WALKING
:
475 st
->activity_walking
= val
;
484 case IIO_CHAN_INFO_CALIBSCALE
:
485 mutex_lock(&st
->lock
);
486 /* Compare against table - hard matching here */
487 for (i
= 0; i
< ARRAY_SIZE(dummy_scales
); i
++)
488 if (val
== dummy_scales
[i
].val
&&
489 val2
== dummy_scales
[i
].val2
)
491 if (i
== ARRAY_SIZE(dummy_scales
))
494 st
->accel_calibscale
= &dummy_scales
[i
];
495 mutex_unlock(&st
->lock
);
497 case IIO_CHAN_INFO_CALIBBIAS
:
498 mutex_lock(&st
->lock
);
499 st
->accel_calibbias
= val
;
500 mutex_unlock(&st
->lock
);
502 case IIO_CHAN_INFO_ENABLE
:
503 switch (chan
->type
) {
505 mutex_lock(&st
->lock
);
506 st
->steps_enabled
= val
;
507 mutex_unlock(&st
->lock
);
512 case IIO_CHAN_INFO_CALIBHEIGHT
:
513 switch (chan
->type
) {
527 * Device type specific information.
529 static const struct iio_info iio_dummy_info
= {
530 .driver_module
= THIS_MODULE
,
531 .read_raw
= &iio_dummy_read_raw
,
532 .write_raw
= &iio_dummy_write_raw
,
533 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
534 .read_event_config
= &iio_simple_dummy_read_event_config
,
535 .write_event_config
= &iio_simple_dummy_write_event_config
,
536 .read_event_value
= &iio_simple_dummy_read_event_value
,
537 .write_event_value
= &iio_simple_dummy_write_event_value
,
538 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
542 * iio_dummy_init_device() - device instance specific init
543 * @indio_dev: the iio device structure
545 * Most drivers have one of these to set up default values,
546 * reset the device to known state etc.
548 static int iio_dummy_init_device(struct iio_dev
*indio_dev
)
550 struct iio_dummy_state
*st
= iio_priv(indio_dev
);
553 st
->single_ended_adc_val
= 73;
554 st
->differential_adc_val
[0] = 33;
555 st
->differential_adc_val
[1] = -34;
557 st
->accel_calibbias
= -7;
558 st
->accel_calibscale
= &dummy_scales
[0];
560 st
->activity_running
= 98;
561 st
->activity_walking
= 4;
567 * iio_dummy_probe() - device instance probe
568 * @index: an id number for this instance.
570 * Arguments are bus type specific.
571 * I2C: iio_dummy_probe(struct i2c_client *client,
572 * const struct i2c_device_id *id)
573 * SPI: iio_dummy_probe(struct spi_device *spi)
575 static int iio_dummy_probe(int index
)
578 struct iio_dev
*indio_dev
;
579 struct iio_dummy_state
*st
;
582 * Allocate an IIO device.
584 * This structure contains all generic state
585 * information about the device instance.
586 * It also has a region (accessed by iio_priv()
587 * for chip specific state information.
589 indio_dev
= iio_device_alloc(sizeof(*st
));
595 st
= iio_priv(indio_dev
);
596 mutex_init(&st
->lock
);
598 iio_dummy_init_device(indio_dev
);
600 * With hardware: Set the parent device.
601 * indio_dev->dev.parent = &spi->dev;
602 * indio_dev->dev.parent = &client->dev;
606 * Make the iio_dev struct available to remove function.
608 * i2c_set_clientdata(client, indio_dev);
609 * spi_set_drvdata(spi, indio_dev);
611 iio_dummy_devs
[index
] = indio_dev
;
614 * Set the device name.
616 * This is typically a part number and obtained from the module
618 * e.g. for i2c and spi:
619 * indio_dev->name = id->name;
620 * indio_dev->name = spi_get_device_id(spi)->name;
622 indio_dev
->name
= iio_dummy_part_number
;
624 /* Provide description of available channels */
625 indio_dev
->channels
= iio_dummy_channels
;
626 indio_dev
->num_channels
= ARRAY_SIZE(iio_dummy_channels
);
629 * Provide device type specific interface functions and
632 indio_dev
->info
= &iio_dummy_info
;
634 /* Specify that device provides sysfs type interfaces */
635 indio_dev
->modes
= INDIO_DIRECT_MODE
;
637 ret
= iio_simple_dummy_events_register(indio_dev
);
639 goto error_free_device
;
641 ret
= iio_simple_dummy_configure_buffer(indio_dev
);
643 goto error_unregister_events
;
645 ret
= iio_device_register(indio_dev
);
647 goto error_unconfigure_buffer
;
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
);
661 * iio_dummy_remove() - device instance removal function
662 * @index: device index.
664 * Parameters follow those of iio_dummy_probe for buses.
666 static void iio_dummy_remove(int index
)
669 * Get a pointer to the device instance iio_dev structure
670 * from the bus subsystem. E.g.
671 * struct iio_dev *indio_dev = i2c_get_clientdata(client);
672 * struct iio_dev *indio_dev = spi_get_drvdata(spi);
674 struct iio_dev
*indio_dev
= iio_dummy_devs
[index
];
676 /* Unregister the device */
677 iio_device_unregister(indio_dev
);
679 /* Device specific code to power down etc */
681 /* Buffered capture related cleanup */
682 iio_simple_dummy_unconfigure_buffer(indio_dev
);
684 iio_simple_dummy_events_unregister(indio_dev
);
686 /* Free all structures */
687 iio_device_free(indio_dev
);
691 * iio_dummy_init() - device driver registration
693 * Varies depending on bus type of the device. As there is no device
694 * here, call probe directly. For information on device registration
696 * Documentation/i2c/writing-clients
698 * Documentation/spi/spi-summary
700 static __init
int iio_dummy_init(void)
704 if (instances
> 10) {
710 iio_dummy_devs
= kcalloc(instances
, sizeof(*iio_dummy_devs
),
712 /* Here we have no actual device so call probe */
713 for (i
= 0; i
< instances
; i
++) {
714 ret
= iio_dummy_probe(i
);
716 goto error_remove_devs
;
724 kfree(iio_dummy_devs
);
727 module_init(iio_dummy_init
);
730 * iio_dummy_exit() - device driver removal
732 * Varies depending on bus type of the device.
733 * As there is no device here, call remove directly.
735 static __exit
void iio_dummy_exit(void)
739 for (i
= 0; i
< instances
; i
++)
741 kfree(iio_dummy_devs
);
743 module_exit(iio_dummy_exit
);
745 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
746 MODULE_DESCRIPTION("IIO dummy driver");
747 MODULE_LICENSE("GPL v2");