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 */
61 * iio_dummy_channels - Description of available channels
63 * This array of structures tells the IIO core about what the device
64 * actually provides for a given channel.
66 static const struct iio_chan_spec iio_dummy_channels
[] = {
67 /* indexed ADC channel in_voltage0_raw etc */
70 /* Channel has a numeric index of 0 */
73 /* What other information is available? */
77 * Raw (unscaled no bias removal etc) measurement
80 BIT(IIO_CHAN_INFO_RAW
) |
83 * Offset for userspace to apply prior to scale
84 * when converting to standard units (microvolts)
86 BIT(IIO_CHAN_INFO_OFFSET
) |
89 * Multipler for userspace to apply post offset
90 * when converting to standard units (microvolts)
92 BIT(IIO_CHAN_INFO_SCALE
),
93 /* The ordering of elements in the buffer via an enum */
94 .scan_index
= voltage0
,
95 .scan_type
= { /* Description of storage in buffer */
96 .sign
= 'u', /* unsigned */
97 .realbits
= 13, /* 13 bits */
98 .storagebits
= 16, /* 16 bits used for storage */
99 .shift
= 0, /* zero shift */
101 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
103 * simple event - triggered when value rises above
106 .event_mask
= IIO_EV_BIT(IIO_EV_TYPE_THRESH
,
108 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
110 /* Differential ADC channel in_voltage1-voltage2_raw etc*/
115 * Indexing for differential channels uses channel
116 * for the positive part, channel2 for the negative.
122 * in_voltage1-voltage2_raw
123 * Raw (unscaled no bias removal etc) measurement
126 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
128 * in_voltage-voltage_scale
129 * Shared version of scale - shared by differential
130 * input channels of type IIO_VOLTAGE.
132 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
),
133 .scan_index
= diffvoltage1m2
,
134 .scan_type
= { /* Description of storage in buffer */
135 .sign
= 's', /* signed */
136 .realbits
= 12, /* 12 bits */
137 .storagebits
= 16, /* 16 bits used for storage */
138 .shift
= 0, /* zero shift */
141 /* Differential ADC channel in_voltage3-voltage4_raw etc*/
148 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
149 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
),
150 .scan_index
= diffvoltage3m4
,
159 * 'modified' (i.e. axis specified) acceleration channel
165 /* Channel 2 is use for modifiers */
166 .channel2
= IIO_MOD_X
,
167 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
169 * Internal bias correction value. Applied
170 * by the hardware or driver prior to userspace
171 * seeing the readings. Typically part of hardware
174 BIT(IIO_CHAN_INFO_CALIBBIAS
),
175 .scan_index
= accelx
,
176 .scan_type
= { /* Description of storage in buffer */
177 .sign
= 's', /* signed */
178 .realbits
= 16, /* 16 bits */
179 .storagebits
= 16, /* 16 bits used for storage */
180 .shift
= 0, /* zero shift */
184 * Convenience macro for timestamps. 4 is the index in
187 IIO_CHAN_SOFT_TIMESTAMP(4),
188 /* DAC channel out_voltage0_raw */
191 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
199 * iio_dummy_read_raw() - data read function.
200 * @indio_dev: the struct iio_dev associated with this device instance
201 * @chan: the channel whose data is to be read
202 * @val: first element of returned value (typically INT)
203 * @val2: second element of returned value (typically MICRO)
204 * @mask: what we actually want to read as per the info_mask_*
207 static int iio_dummy_read_raw(struct iio_dev
*indio_dev
,
208 struct iio_chan_spec
const *chan
,
213 struct iio_dummy_state
*st
= iio_priv(indio_dev
);
216 mutex_lock(&st
->lock
);
218 case IIO_CHAN_INFO_RAW
: /* magic value - channel value read */
219 switch (chan
->type
) {
222 /* Set integer part to cached value */
225 } else if (chan
->differential
) {
226 if (chan
->channel
== 1)
227 *val
= st
->differential_adc_val
[0];
229 *val
= st
->differential_adc_val
[1];
232 *val
= st
->single_ended_adc_val
;
237 *val
= st
->accel_val
;
244 case IIO_CHAN_INFO_OFFSET
:
245 /* only single ended adc -> 7 */
249 case IIO_CHAN_INFO_SCALE
:
250 switch (chan
->differential
) {
252 /* only single ended adc -> 0.001333 */
255 ret
= IIO_VAL_INT_PLUS_MICRO
;
258 /* all differential adc channels -> 0.000001344 */
261 ret
= IIO_VAL_INT_PLUS_NANO
;
264 case IIO_CHAN_INFO_CALIBBIAS
:
265 /* only the acceleration axis - read from cache */
266 *val
= st
->accel_calibbias
;
269 case IIO_CHAN_INFO_CALIBSCALE
:
270 *val
= st
->accel_calibscale
->val
;
271 *val2
= st
->accel_calibscale
->val2
;
272 ret
= IIO_VAL_INT_PLUS_MICRO
;
277 mutex_unlock(&st
->lock
);
282 * iio_dummy_write_raw() - data write function.
283 * @indio_dev: the struct iio_dev associated with this device instance
284 * @chan: the channel whose data is to be written
285 * @val: first element of value to set (typically INT)
286 * @val2: second element of value to set (typically MICRO)
287 * @mask: what we actually want to write as per the info_mask_*
290 * Note that all raw writes are assumed IIO_VAL_INT and info mask elements
291 * are assumed to be IIO_INT_PLUS_MICRO unless the callback write_raw_get_fmt
292 * in struct iio_info is provided by the driver.
294 static int iio_dummy_write_raw(struct iio_dev
*indio_dev
,
295 struct iio_chan_spec
const *chan
,
302 struct iio_dummy_state
*st
= iio_priv(indio_dev
);
305 case IIO_CHAN_INFO_RAW
:
306 if (chan
->output
== 0)
309 /* Locking not required as writing single value */
310 mutex_lock(&st
->lock
);
312 mutex_unlock(&st
->lock
);
314 case IIO_CHAN_INFO_CALIBBIAS
:
315 mutex_lock(&st
->lock
);
316 /* Compare against table - hard matching here */
317 for (i
= 0; i
< ARRAY_SIZE(dummy_scales
); i
++)
318 if (val
== dummy_scales
[i
].val
&&
319 val2
== dummy_scales
[i
].val2
)
321 if (i
== ARRAY_SIZE(dummy_scales
))
324 st
->accel_calibscale
= &dummy_scales
[i
];
325 mutex_unlock(&st
->lock
);
333 * Device type specific information.
335 static const struct iio_info iio_dummy_info
= {
336 .driver_module
= THIS_MODULE
,
337 .read_raw
= &iio_dummy_read_raw
,
338 .write_raw
= &iio_dummy_write_raw
,
339 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
340 .read_event_config
= &iio_simple_dummy_read_event_config
,
341 .write_event_config
= &iio_simple_dummy_write_event_config
,
342 .read_event_value
= &iio_simple_dummy_read_event_value
,
343 .write_event_value
= &iio_simple_dummy_write_event_value
,
344 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
348 * iio_dummy_init_device() - device instance specific init
349 * @indio_dev: the iio device structure
351 * Most drivers have one of these to set up default values,
352 * reset the device to known state etc.
354 static int iio_dummy_init_device(struct iio_dev
*indio_dev
)
356 struct iio_dummy_state
*st
= iio_priv(indio_dev
);
359 st
->single_ended_adc_val
= 73;
360 st
->differential_adc_val
[0] = 33;
361 st
->differential_adc_val
[1] = -34;
363 st
->accel_calibbias
= -7;
364 st
->accel_calibscale
= &dummy_scales
[0];
370 * iio_dummy_probe() - device instance probe
371 * @index: an id number for this instance.
373 * Arguments are bus type specific.
374 * I2C: iio_dummy_probe(struct i2c_client *client,
375 * const struct i2c_device_id *id)
376 * SPI: iio_dummy_probe(struct spi_device *spi)
378 static int iio_dummy_probe(int index
)
381 struct iio_dev
*indio_dev
;
382 struct iio_dummy_state
*st
;
385 * Allocate an IIO device.
387 * This structure contains all generic state
388 * information about the device instance.
389 * It also has a region (accessed by iio_priv()
390 * for chip specific state information.
392 indio_dev
= iio_device_alloc(sizeof(*st
));
393 if (indio_dev
== NULL
) {
398 st
= iio_priv(indio_dev
);
399 mutex_init(&st
->lock
);
401 iio_dummy_init_device(indio_dev
);
403 * With hardware: Set the parent device.
404 * indio_dev->dev.parent = &spi->dev;
405 * indio_dev->dev.parent = &client->dev;
409 * Make the iio_dev struct available to remove function.
411 * i2c_set_clientdata(client, indio_dev);
412 * spi_set_drvdata(spi, indio_dev);
414 iio_dummy_devs
[index
] = indio_dev
;
418 * Set the device name.
420 * This is typically a part number and obtained from the module
422 * e.g. for i2c and spi:
423 * indio_dev->name = id->name;
424 * indio_dev->name = spi_get_device_id(spi)->name;
426 indio_dev
->name
= iio_dummy_part_number
;
428 /* Provide description of available channels */
429 indio_dev
->channels
= iio_dummy_channels
;
430 indio_dev
->num_channels
= ARRAY_SIZE(iio_dummy_channels
);
433 * Provide device type specific interface functions and
436 indio_dev
->info
= &iio_dummy_info
;
438 /* Specify that device provides sysfs type interfaces */
439 indio_dev
->modes
= INDIO_DIRECT_MODE
;
441 ret
= iio_simple_dummy_events_register(indio_dev
);
443 goto error_free_device
;
446 * Configure buffered capture support and register the channels with the
447 * buffer, but avoid the output channel being registered by reducing the
448 * number of channels by 1.
450 ret
= iio_simple_dummy_configure_buffer(indio_dev
, iio_dummy_channels
, 5);
452 goto error_unregister_events
;
454 ret
= iio_device_register(indio_dev
);
456 goto error_unconfigure_buffer
;
459 error_unconfigure_buffer
:
460 iio_simple_dummy_unconfigure_buffer(indio_dev
);
461 error_unregister_events
:
462 iio_simple_dummy_events_unregister(indio_dev
);
464 iio_device_free(indio_dev
);
470 * iio_dummy_remove() - device instance removal function
471 * @index: device index.
473 * Parameters follow those of iio_dummy_probe for buses.
475 static int iio_dummy_remove(int index
)
479 * Get a pointer to the device instance iio_dev structure
480 * from the bus subsystem. E.g.
481 * struct iio_dev *indio_dev = i2c_get_clientdata(client);
482 * struct iio_dev *indio_dev = spi_get_drvdata(spi);
484 struct iio_dev
*indio_dev
= iio_dummy_devs
[index
];
487 /* Unregister the device */
488 iio_device_unregister(indio_dev
);
490 /* Device specific code to power down etc */
492 /* Buffered capture related cleanup */
493 iio_simple_dummy_unconfigure_buffer(indio_dev
);
495 ret
= iio_simple_dummy_events_unregister(indio_dev
);
499 /* Free all structures */
500 iio_device_free(indio_dev
);
507 * iio_dummy_init() - device driver registration
509 * Varies depending on bus type of the device. As there is no device
510 * here, call probe directly. For information on device registration
512 * Documentation/i2c/writing-clients
514 * Documentation/spi/spi-summary
516 static __init
int iio_dummy_init(void)
519 if (instances
> 10) {
525 iio_dummy_devs
= kcalloc(instances
, sizeof(*iio_dummy_devs
),
527 /* Here we have no actual device so call probe */
528 for (i
= 0; i
< instances
; i
++) {
529 ret
= iio_dummy_probe(i
);
535 module_init(iio_dummy_init
);
538 * iio_dummy_exit() - device driver removal
540 * Varies depending on bus type of the device.
541 * As there is no device here, call remove directly.
543 static __exit
void iio_dummy_exit(void)
546 for (i
= 0; i
< instances
; i
++)
548 kfree(iio_dummy_devs
);
550 module_exit(iio_dummy_exit
);
552 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
553 MODULE_DESCRIPTION("IIO dummy driver");
554 MODULE_LICENSE("GPL v2");