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 const 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 .read_raw
= &iio_dummy_read_raw
,
523 .write_raw
= &iio_dummy_write_raw
,
524 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
525 .read_event_config
= &iio_simple_dummy_read_event_config
,
526 .write_event_config
= &iio_simple_dummy_write_event_config
,
527 .read_event_value
= &iio_simple_dummy_read_event_value
,
528 .write_event_value
= &iio_simple_dummy_write_event_value
,
529 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
533 * iio_dummy_init_device() - device instance specific init
534 * @indio_dev: the iio device structure
536 * Most drivers have one of these to set up default values,
537 * reset the device to known state etc.
539 static int iio_dummy_init_device(struct iio_dev
*indio_dev
)
541 struct iio_dummy_state
*st
= iio_priv(indio_dev
);
544 st
->single_ended_adc_val
= 73;
545 st
->differential_adc_val
[0] = 33;
546 st
->differential_adc_val
[1] = -34;
548 st
->accel_calibbias
= -7;
549 st
->accel_calibscale
= &dummy_scales
[0];
551 st
->activity_running
= 98;
552 st
->activity_walking
= 4;
558 * iio_dummy_probe() - device instance probe
559 * @index: an id number for this instance.
561 * Arguments are bus type specific.
562 * I2C: iio_dummy_probe(struct i2c_client *client,
563 * const struct i2c_device_id *id)
564 * SPI: iio_dummy_probe(struct spi_device *spi)
566 static struct iio_sw_device
*iio_dummy_probe(const char *name
)
569 struct iio_dev
*indio_dev
;
570 struct iio_dummy_state
*st
;
571 struct iio_sw_device
*swd
;
573 swd
= kzalloc(sizeof(*swd
), GFP_KERNEL
);
579 * Allocate an IIO device.
581 * This structure contains all generic state
582 * information about the device instance.
583 * It also has a region (accessed by iio_priv()
584 * for chip specific state information.
586 indio_dev
= iio_device_alloc(sizeof(*st
));
592 st
= iio_priv(indio_dev
);
593 mutex_init(&st
->lock
);
595 iio_dummy_init_device(indio_dev
);
597 * With hardware: Set the parent device.
598 * indio_dev->dev.parent = &spi->dev;
599 * indio_dev->dev.parent = &client->dev;
603 * Make the iio_dev struct available to remove function.
605 * i2c_set_clientdata(client, indio_dev);
606 * spi_set_drvdata(spi, indio_dev);
608 swd
->device
= indio_dev
;
611 * Set the device name.
613 * This is typically a part number and obtained from the module
615 * e.g. for i2c and spi:
616 * indio_dev->name = id->name;
617 * indio_dev->name = spi_get_device_id(spi)->name;
619 indio_dev
->name
= kstrdup(name
, GFP_KERNEL
);
621 /* Provide description of available channels */
622 indio_dev
->channels
= iio_dummy_channels
;
623 indio_dev
->num_channels
= ARRAY_SIZE(iio_dummy_channels
);
626 * Provide device type specific interface functions and
629 indio_dev
->info
= &iio_dummy_info
;
631 /* Specify that device provides sysfs type interfaces */
632 indio_dev
->modes
= INDIO_DIRECT_MODE
;
634 ret
= iio_simple_dummy_events_register(indio_dev
);
636 goto error_free_device
;
638 ret
= iio_simple_dummy_configure_buffer(indio_dev
);
640 goto error_unregister_events
;
642 ret
= iio_device_register(indio_dev
);
644 goto error_unconfigure_buffer
;
646 iio_swd_group_init_type_name(swd
, name
, &iio_dummy_type
);
649 error_unconfigure_buffer
:
650 iio_simple_dummy_unconfigure_buffer(indio_dev
);
651 error_unregister_events
:
652 iio_simple_dummy_events_unregister(indio_dev
);
654 iio_device_free(indio_dev
);
662 * iio_dummy_remove() - device instance removal function
663 * @swd: pointer to software IIO device abstraction
665 * Parameters follow those of iio_dummy_probe for buses.
667 static int iio_dummy_remove(struct iio_sw_device
*swd
)
670 * Get a pointer to the device instance iio_dev structure
671 * from the bus subsystem. E.g.
672 * struct iio_dev *indio_dev = i2c_get_clientdata(client);
673 * struct iio_dev *indio_dev = spi_get_drvdata(spi);
675 struct iio_dev
*indio_dev
= swd
->device
;
677 /* Unregister the device */
678 iio_device_unregister(indio_dev
);
680 /* Device specific code to power down etc */
682 /* Buffered capture related cleanup */
683 iio_simple_dummy_unconfigure_buffer(indio_dev
);
685 iio_simple_dummy_events_unregister(indio_dev
);
687 /* Free all structures */
688 kfree(indio_dev
->name
);
689 iio_device_free(indio_dev
);
694 * module_iio_sw_device_driver() - device driver registration
696 * Varies depending on bus type of the device. As there is no device
697 * here, call probe directly. For information on device registration
699 * Documentation/i2c/writing-clients
701 * Documentation/spi/spi-summary
703 static const struct iio_sw_device_ops iio_dummy_device_ops
= {
704 .probe
= iio_dummy_probe
,
705 .remove
= iio_dummy_remove
,
708 static struct iio_sw_device_type iio_dummy_device
= {
710 .owner
= THIS_MODULE
,
711 .ops
= &iio_dummy_device_ops
,
714 module_iio_sw_device_driver(iio_dummy_device
);
716 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
717 MODULE_DESCRIPTION("IIO dummy driver");
718 MODULE_LICENSE("GPL v2");