1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2011 Jonathan Cameron
5 * A reference industrial I/O driver to illustrate the functionality available.
7 * There are numerous real drivers to illustrate the finer points.
8 * The purpose of this driver is to provide a driver with far more comments
9 * and explanatory notes than any 'real' driver would have.
10 * Anyone starting out writing an IIO driver should first make sure they
11 * understand all of this driver except those bits specifically marked
12 * as being present to allow us to 'fake' the presence of hardware.
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include <linux/string.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21 #include <linux/iio/events.h>
22 #include <linux/iio/buffer.h>
23 #include <linux/iio/sw_device.h>
24 #include "iio_simple_dummy.h"
26 static const struct config_item_type iio_dummy_type
= {
27 .ct_owner
= THIS_MODULE
,
31 * struct iio_dummy_accel_calibscale - realworld to register mapping
32 * @val: first value in read_raw - here integer part.
33 * @val2: second value in read_raw etc - here micro part.
34 * @regval: register value - magic device specific numbers.
36 struct iio_dummy_accel_calibscale
{
39 int regval
; /* what would be written to hardware */
42 static const struct iio_dummy_accel_calibscale dummy_scales
[] = {
43 { 0, 100, 0x8 }, /* 0.000100 */
44 { 0, 133, 0x7 }, /* 0.000133 */
45 { 733, 13, 0x9 }, /* 733.000013 */
48 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
51 * simple event - triggered when value rises above
54 static const struct iio_event_spec iio_dummy_event
= {
55 .type
= IIO_EV_TYPE_THRESH
,
56 .dir
= IIO_EV_DIR_RISING
,
57 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) | BIT(IIO_EV_INFO_ENABLE
),
61 * simple step detect event - triggered when a step is detected
63 static const struct iio_event_spec step_detect_event
= {
64 .type
= IIO_EV_TYPE_CHANGE
,
65 .dir
= IIO_EV_DIR_NONE
,
66 .mask_separate
= BIT(IIO_EV_INFO_ENABLE
),
70 * simple transition event - triggered when the reported running confidence
71 * value rises above a threshold value
73 static const struct iio_event_spec iio_running_event
= {
74 .type
= IIO_EV_TYPE_THRESH
,
75 .dir
= IIO_EV_DIR_RISING
,
76 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) | BIT(IIO_EV_INFO_ENABLE
),
80 * simple transition event - triggered when the reported walking confidence
81 * value falls under a threshold value
83 static const struct iio_event_spec iio_walking_event
= {
84 .type
= IIO_EV_TYPE_THRESH
,
85 .dir
= IIO_EV_DIR_FALLING
,
86 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) | BIT(IIO_EV_INFO_ENABLE
),
91 * iio_dummy_channels - Description of available channels
93 * This array of structures tells the IIO core about what the device
94 * actually provides for a given channel.
96 static const struct iio_chan_spec iio_dummy_channels
[] = {
97 /* indexed ADC channel in_voltage0_raw etc */
100 /* Channel has a numeric index of 0 */
103 /* What other information is available? */
104 .info_mask_separate
=
107 * Raw (unscaled no bias removal etc) measurement
110 BIT(IIO_CHAN_INFO_RAW
) |
113 * Offset for userspace to apply prior to scale
114 * when converting to standard units (microvolts)
116 BIT(IIO_CHAN_INFO_OFFSET
) |
119 * Multipler for userspace to apply post offset
120 * when converting to standard units (microvolts)
122 BIT(IIO_CHAN_INFO_SCALE
),
125 * The frequency in Hz at which the channels are sampled
127 .info_mask_shared_by_dir
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
128 /* The ordering of elements in the buffer via an enum */
129 .scan_index
= DUMMY_INDEX_VOLTAGE_0
,
130 .scan_type
= { /* Description of storage in buffer */
131 .sign
= 'u', /* unsigned */
132 .realbits
= 13, /* 13 bits */
133 .storagebits
= 16, /* 16 bits used for storage */
134 .shift
= 0, /* zero shift */
136 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
137 .event_spec
= &iio_dummy_event
,
138 .num_event_specs
= 1,
139 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
141 /* Differential ADC channel in_voltage1-voltage2_raw etc*/
146 * Indexing for differential channels uses channel
147 * for the positive part, channel2 for the negative.
153 * in_voltage1-voltage2_raw
154 * Raw (unscaled no bias removal etc) measurement
157 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
159 * in_voltage-voltage_scale
160 * Shared version of scale - shared by differential
161 * input channels of type IIO_VOLTAGE.
163 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
),
166 * The frequency in Hz at which the channels are sampled
168 .scan_index
= DUMMY_INDEX_DIFFVOLTAGE_1M2
,
169 .scan_type
= { /* Description of storage in buffer */
170 .sign
= 's', /* signed */
171 .realbits
= 12, /* 12 bits */
172 .storagebits
= 16, /* 16 bits used for storage */
173 .shift
= 0, /* zero shift */
176 /* Differential ADC channel in_voltage3-voltage4_raw etc*/
183 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
184 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
),
185 .info_mask_shared_by_dir
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
186 .scan_index
= DUMMY_INDEX_DIFFVOLTAGE_3M4
,
195 * 'modified' (i.e. axis specified) acceleration channel
201 /* Channel 2 is use for modifiers */
202 .channel2
= IIO_MOD_X
,
203 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
205 * Internal bias and gain correction values. Applied
206 * by the hardware or driver prior to userspace
207 * seeing the readings. Typically part of hardware
210 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
211 BIT(IIO_CHAN_INFO_CALIBBIAS
),
212 .info_mask_shared_by_dir
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
213 .scan_index
= DUMMY_INDEX_ACCELX
,
214 .scan_type
= { /* Description of storage in buffer */
215 .sign
= 's', /* signed */
216 .realbits
= 16, /* 16 bits */
217 .storagebits
= 16, /* 16 bits used for storage */
218 .shift
= 0, /* zero shift */
222 * Convenience macro for timestamps. 4 is the index in
225 IIO_CHAN_SOFT_TIMESTAMP(4),
226 /* DAC channel out_voltage0_raw */
229 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
230 .scan_index
= -1, /* No buffer support */
237 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_ENABLE
) |
238 BIT(IIO_CHAN_INFO_CALIBHEIGHT
),
239 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
240 .scan_index
= -1, /* No buffer support */
241 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
242 .event_spec
= &step_detect_event
,
243 .num_event_specs
= 1,
244 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
247 .type
= IIO_ACTIVITY
,
249 .channel2
= IIO_MOD_RUNNING
,
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
= &iio_running_event
,
254 .num_event_specs
= 1,
255 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
258 .type
= IIO_ACTIVITY
,
260 .channel2
= IIO_MOD_WALKING
,
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_walking_event
,
265 .num_event_specs
= 1,
266 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
271 * iio_dummy_read_raw() - data read function.
272 * @indio_dev: the struct iio_dev associated with this device instance
273 * @chan: the channel whose data is to be read
274 * @val: first element of returned value (typically INT)
275 * @val2: second element of returned value (typically MICRO)
276 * @mask: what we actually want to read as per the info_mask_*
279 static int iio_dummy_read_raw(struct iio_dev
*indio_dev
,
280 struct iio_chan_spec
const *chan
,
285 struct iio_dummy_state
*st
= iio_priv(indio_dev
);
288 mutex_lock(&st
->lock
);
290 case IIO_CHAN_INFO_RAW
: /* magic value - channel value read */
291 switch (chan
->type
) {
294 /* Set integer part to cached value */
297 } else if (chan
->differential
) {
298 if (chan
->channel
== 1)
299 *val
= st
->differential_adc_val
[0];
301 *val
= st
->differential_adc_val
[1];
304 *val
= st
->single_ended_adc_val
;
309 *val
= st
->accel_val
;
316 case IIO_CHAN_INFO_PROCESSED
:
317 switch (chan
->type
) {
323 switch (chan
->channel2
) {
324 case IIO_MOD_RUNNING
:
325 *val
= st
->activity_running
;
328 case IIO_MOD_WALKING
:
329 *val
= st
->activity_walking
;
340 case IIO_CHAN_INFO_OFFSET
:
341 /* only single ended adc -> 7 */
345 case IIO_CHAN_INFO_SCALE
:
346 switch (chan
->type
) {
348 switch (chan
->differential
) {
350 /* only single ended adc -> 0.001333 */
353 ret
= IIO_VAL_INT_PLUS_MICRO
;
356 /* all differential adc -> 0.000001344 */
359 ret
= IIO_VAL_INT_PLUS_NANO
;
366 case IIO_CHAN_INFO_CALIBBIAS
:
367 /* only the acceleration axis - read from cache */
368 *val
= st
->accel_calibbias
;
371 case IIO_CHAN_INFO_CALIBSCALE
:
372 *val
= st
->accel_calibscale
->val
;
373 *val2
= st
->accel_calibscale
->val2
;
374 ret
= IIO_VAL_INT_PLUS_MICRO
;
376 case IIO_CHAN_INFO_SAMP_FREQ
:
379 ret
= IIO_VAL_INT_PLUS_NANO
;
381 case IIO_CHAN_INFO_ENABLE
:
382 switch (chan
->type
) {
384 *val
= st
->steps_enabled
;
391 case IIO_CHAN_INFO_CALIBHEIGHT
:
392 switch (chan
->type
) {
405 mutex_unlock(&st
->lock
);
410 * iio_dummy_write_raw() - data write function.
411 * @indio_dev: the struct iio_dev associated with this device instance
412 * @chan: the channel whose data is to be written
413 * @val: first element of value to set (typically INT)
414 * @val2: second element of value to set (typically MICRO)
415 * @mask: what we actually want to write as per the info_mask_*
418 * Note that all raw writes are assumed IIO_VAL_INT and info mask elements
419 * are assumed to be IIO_INT_PLUS_MICRO unless the callback write_raw_get_fmt
420 * in struct iio_info is provided by the driver.
422 static int iio_dummy_write_raw(struct iio_dev
*indio_dev
,
423 struct iio_chan_spec
const *chan
,
430 struct iio_dummy_state
*st
= iio_priv(indio_dev
);
433 case IIO_CHAN_INFO_RAW
:
434 switch (chan
->type
) {
436 if (chan
->output
== 0)
439 /* Locking not required as writing single value */
440 mutex_lock(&st
->lock
);
442 mutex_unlock(&st
->lock
);
447 case IIO_CHAN_INFO_PROCESSED
:
448 switch (chan
->type
) {
450 mutex_lock(&st
->lock
);
452 mutex_unlock(&st
->lock
);
459 switch (chan
->channel2
) {
460 case IIO_MOD_RUNNING
:
461 st
->activity_running
= val
;
463 case IIO_MOD_WALKING
:
464 st
->activity_walking
= val
;
473 case IIO_CHAN_INFO_CALIBSCALE
:
474 mutex_lock(&st
->lock
);
475 /* Compare against table - hard matching here */
476 for (i
= 0; i
< ARRAY_SIZE(dummy_scales
); i
++)
477 if (val
== dummy_scales
[i
].val
&&
478 val2
== dummy_scales
[i
].val2
)
480 if (i
== ARRAY_SIZE(dummy_scales
))
483 st
->accel_calibscale
= &dummy_scales
[i
];
484 mutex_unlock(&st
->lock
);
486 case IIO_CHAN_INFO_CALIBBIAS
:
487 mutex_lock(&st
->lock
);
488 st
->accel_calibbias
= val
;
489 mutex_unlock(&st
->lock
);
491 case IIO_CHAN_INFO_ENABLE
:
492 switch (chan
->type
) {
494 mutex_lock(&st
->lock
);
495 st
->steps_enabled
= val
;
496 mutex_unlock(&st
->lock
);
501 case IIO_CHAN_INFO_CALIBHEIGHT
:
502 switch (chan
->type
) {
516 * Device type specific information.
518 static const struct iio_info iio_dummy_info
= {
519 .read_raw
= &iio_dummy_read_raw
,
520 .write_raw
= &iio_dummy_write_raw
,
521 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
522 .read_event_config
= &iio_simple_dummy_read_event_config
,
523 .write_event_config
= &iio_simple_dummy_write_event_config
,
524 .read_event_value
= &iio_simple_dummy_read_event_value
,
525 .write_event_value
= &iio_simple_dummy_write_event_value
,
526 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
530 * iio_dummy_init_device() - device instance specific init
531 * @indio_dev: the iio device structure
533 * Most drivers have one of these to set up default values,
534 * reset the device to known state etc.
536 static int iio_dummy_init_device(struct iio_dev
*indio_dev
)
538 struct iio_dummy_state
*st
= iio_priv(indio_dev
);
541 st
->single_ended_adc_val
= 73;
542 st
->differential_adc_val
[0] = 33;
543 st
->differential_adc_val
[1] = -34;
545 st
->accel_calibbias
= -7;
546 st
->accel_calibscale
= &dummy_scales
[0];
548 st
->activity_running
= 98;
549 st
->activity_walking
= 4;
555 * iio_dummy_probe() - device instance probe
556 * @name: name of this instance.
558 * Arguments are bus type specific.
559 * I2C: iio_dummy_probe(struct i2c_client *client,
560 * const struct i2c_device_id *id)
561 * SPI: iio_dummy_probe(struct spi_device *spi)
563 static struct iio_sw_device
*iio_dummy_probe(const char *name
)
566 struct iio_dev
*indio_dev
;
567 struct iio_dummy_state
*st
;
568 struct iio_sw_device
*swd
;
569 struct device
*parent
= NULL
;
572 * With hardware: Set the parent device.
573 * parent = &spi->dev;
574 * parent = &client->dev;
577 swd
= kzalloc(sizeof(*swd
), GFP_KERNEL
);
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(parent
, sizeof(*st
));
596 st
= iio_priv(indio_dev
);
597 mutex_init(&st
->lock
);
599 iio_dummy_init_device(indio_dev
);
602 * Make the iio_dev struct available to remove function.
604 * i2c_set_clientdata(client, indio_dev);
605 * spi_set_drvdata(spi, indio_dev);
607 swd
->device
= indio_dev
;
610 * Set the device name.
612 * This is typically a part number and obtained from the module
614 * e.g. for i2c and spi:
615 * indio_dev->name = id->name;
616 * indio_dev->name = spi_get_device_id(spi)->name;
618 indio_dev
->name
= kstrdup(name
, GFP_KERNEL
);
620 /* Provide description of available channels */
621 indio_dev
->channels
= iio_dummy_channels
;
622 indio_dev
->num_channels
= ARRAY_SIZE(iio_dummy_channels
);
625 * Provide device type specific interface functions and
628 indio_dev
->info
= &iio_dummy_info
;
630 /* Specify that device provides sysfs type interfaces */
631 indio_dev
->modes
= INDIO_DIRECT_MODE
;
633 ret
= iio_simple_dummy_events_register(indio_dev
);
635 goto error_free_device
;
637 ret
= iio_simple_dummy_configure_buffer(indio_dev
);
639 goto error_unregister_events
;
641 ret
= iio_device_register(indio_dev
);
643 goto error_unconfigure_buffer
;
645 iio_swd_group_init_type_name(swd
, name
, &iio_dummy_type
);
648 error_unconfigure_buffer
:
649 iio_simple_dummy_unconfigure_buffer(indio_dev
);
650 error_unregister_events
:
651 iio_simple_dummy_events_unregister(indio_dev
);
653 iio_device_free(indio_dev
);
661 * iio_dummy_remove() - device instance removal function
662 * @swd: pointer to software IIO device abstraction
664 * Parameters follow those of iio_dummy_probe for buses.
666 static int iio_dummy_remove(struct iio_sw_device
*swd
)
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
= swd
->device
;
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 kfree(indio_dev
->name
);
688 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.rst
701 * Documentation/spi/spi-summary.rst
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");