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 */
60 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
63 * simple event - triggered when value rises above
66 static const struct iio_event_spec iio_dummy_event = {
67 .type = IIO_EV_TYPE_THRESH,
68 .dir = IIO_EV_DIR_RISING,
69 .mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE),
75 * iio_dummy_channels - Description of available channels
77 * This array of structures tells the IIO core about what the device
78 * actually provides for a given channel.
80 static const struct iio_chan_spec iio_dummy_channels[] = {
81 /* indexed ADC channel in_voltage0_raw etc */
84 /* Channel has a numeric index of 0 */
87 /* What other information is available? */
91 * Raw (unscaled no bias removal etc) measurement
94 BIT(IIO_CHAN_INFO_RAW) |
97 * Offset for userspace to apply prior to scale
98 * when converting to standard units (microvolts)
100 BIT(IIO_CHAN_INFO_OFFSET) |
103 * Multipler for userspace to apply post offset
104 * when converting to standard units (microvolts)
106 BIT(IIO_CHAN_INFO_SCALE),
109 * The frequency in Hz at which the channels are sampled
111 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
112 /* The ordering of elements in the buffer via an enum */
113 .scan_index = voltage0,
114 .scan_type = { /* Description of storage in buffer */
115 .sign = 'u', /* unsigned */
116 .realbits = 13, /* 13 bits */
117 .storagebits = 16, /* 16 bits used for storage */
118 .shift = 0, /* zero shift */
120 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
121 .event_spec = &iio_dummy_event,
122 .num_event_specs = 1,
123 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
125 /* Differential ADC channel in_voltage1-voltage2_raw etc*/
130 * Indexing for differential channels uses channel
131 * for the positive part, channel2 for the negative.
137 * in_voltage1-voltage2_raw
138 * Raw (unscaled no bias removal etc) measurement
141 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
143 * in_voltage-voltage_scale
144 * Shared version of scale - shared by differential
145 * input channels of type IIO_VOLTAGE.
147 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
150 * The frequency in Hz at which the channels are sampled
152 .scan_index = diffvoltage1m2,
153 .scan_type = { /* Description of storage in buffer */
154 .sign = 's', /* signed */
155 .realbits = 12, /* 12 bits */
156 .storagebits = 16, /* 16 bits used for storage */
157 .shift = 0, /* zero shift */
160 /* Differential ADC channel in_voltage3-voltage4_raw etc*/
167 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
168 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
169 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
170 .scan_index = diffvoltage3m4,
179 * 'modified' (i.e. axis specified) acceleration channel
185 /* Channel 2 is use for modifiers */
186 .channel2 = IIO_MOD_X,
187 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
189 * Internal bias and gain correction values. Applied
190 * by the hardware or driver prior to userspace
191 * seeing the readings. Typically part of hardware
194 BIT(IIO_CHAN_INFO_CALIBSCALE) |
195 BIT(IIO_CHAN_INFO_CALIBBIAS),
196 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
197 .scan_index = accelx,
198 .scan_type = { /* Description of storage in buffer */
199 .sign = 's', /* signed */
200 .realbits = 16, /* 16 bits */
201 .storagebits = 16, /* 16 bits used for storage */
202 .shift = 0, /* zero shift */
206 * Convenience macro for timestamps. 4 is the index in
209 IIO_CHAN_SOFT_TIMESTAMP(4),
210 /* DAC channel out_voltage0_raw */
213 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
221 * iio_dummy_read_raw() - data read function.
222 * @indio_dev: the struct iio_dev associated with this device instance
223 * @chan: the channel whose data is to be read
224 * @val: first element of returned value (typically INT)
225 * @val2: second element of returned value (typically MICRO)
226 * @mask: what we actually want to read as per the info_mask_*
229 static int iio_dummy_read_raw(struct iio_dev *indio_dev,
230 struct iio_chan_spec const *chan,
235 struct iio_dummy_state *st = iio_priv(indio_dev);
238 mutex_lock(&st->lock);
240 case IIO_CHAN_INFO_RAW: /* magic value - channel value read */
241 switch (chan->type) {
244 /* Set integer part to cached value */
247 } else if (chan->differential) {
248 if (chan->channel == 1)
249 *val = st->differential_adc_val[0];
251 *val = st->differential_adc_val[1];
254 *val = st->single_ended_adc_val;
259 *val = st->accel_val;
266 case IIO_CHAN_INFO_OFFSET:
267 /* only single ended adc -> 7 */
271 case IIO_CHAN_INFO_SCALE:
272 switch (chan->differential) {
274 /* only single ended adc -> 0.001333 */
277 ret = IIO_VAL_INT_PLUS_MICRO;
280 /* all differential adc channels -> 0.000001344 */
283 ret = IIO_VAL_INT_PLUS_NANO;
286 case IIO_CHAN_INFO_CALIBBIAS:
287 /* only the acceleration axis - read from cache */
288 *val = st->accel_calibbias;
291 case IIO_CHAN_INFO_CALIBSCALE:
292 *val = st->accel_calibscale->val;
293 *val2 = st->accel_calibscale->val2;
294 ret = IIO_VAL_INT_PLUS_MICRO;
296 case IIO_CHAN_INFO_SAMP_FREQ:
299 ret = IIO_VAL_INT_PLUS_NANO;
304 mutex_unlock(&st->lock);
309 * iio_dummy_write_raw() - data write function.
310 * @indio_dev: the struct iio_dev associated with this device instance
311 * @chan: the channel whose data is to be written
312 * @val: first element of value to set (typically INT)
313 * @val2: second element of value to set (typically MICRO)
314 * @mask: what we actually want to write as per the info_mask_*
317 * Note that all raw writes are assumed IIO_VAL_INT and info mask elements
318 * are assumed to be IIO_INT_PLUS_MICRO unless the callback write_raw_get_fmt
319 * in struct iio_info is provided by the driver.
321 static int iio_dummy_write_raw(struct iio_dev *indio_dev,
322 struct iio_chan_spec const *chan,
329 struct iio_dummy_state *st = iio_priv(indio_dev);
332 case IIO_CHAN_INFO_RAW:
333 if (chan->output == 0)
336 /* Locking not required as writing single value */
337 mutex_lock(&st->lock);
339 mutex_unlock(&st->lock);
341 case IIO_CHAN_INFO_CALIBSCALE:
342 mutex_lock(&st->lock);
343 /* Compare against table - hard matching here */
344 for (i = 0; i < ARRAY_SIZE(dummy_scales); i++)
345 if (val == dummy_scales[i].val &&
346 val2 == dummy_scales[i].val2)
348 if (i == ARRAY_SIZE(dummy_scales))
351 st->accel_calibscale = &dummy_scales[i];
352 mutex_unlock(&st->lock);
354 case IIO_CHAN_INFO_CALIBBIAS:
355 mutex_lock(&st->lock);
356 st->accel_calibbias = val;
357 mutex_unlock(&st->lock);
366 * Device type specific information.
368 static const struct iio_info iio_dummy_info = {
369 .driver_module = THIS_MODULE,
370 .read_raw = &iio_dummy_read_raw,
371 .write_raw = &iio_dummy_write_raw,
372 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
373 .read_event_config = &iio_simple_dummy_read_event_config,
374 .write_event_config = &iio_simple_dummy_write_event_config,
375 .read_event_value = &iio_simple_dummy_read_event_value,
376 .write_event_value = &iio_simple_dummy_write_event_value,
377 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
381 * iio_dummy_init_device() - device instance specific init
382 * @indio_dev: the iio device structure
384 * Most drivers have one of these to set up default values,
385 * reset the device to known state etc.
387 static int iio_dummy_init_device(struct iio_dev *indio_dev)
389 struct iio_dummy_state *st = iio_priv(indio_dev);
392 st->single_ended_adc_val = 73;
393 st->differential_adc_val[0] = 33;
394 st->differential_adc_val[1] = -34;
396 st->accel_calibbias = -7;
397 st->accel_calibscale = &dummy_scales[0];
403 * iio_dummy_probe() - device instance probe
404 * @index: an id number for this instance.
406 * Arguments are bus type specific.
407 * I2C: iio_dummy_probe(struct i2c_client *client,
408 * const struct i2c_device_id *id)
409 * SPI: iio_dummy_probe(struct spi_device *spi)
411 static int iio_dummy_probe(int index)
414 struct iio_dev *indio_dev;
415 struct iio_dummy_state *st;
418 * Allocate an IIO device.
420 * This structure contains all generic state
421 * information about the device instance.
422 * It also has a region (accessed by iio_priv()
423 * for chip specific state information.
425 indio_dev = iio_device_alloc(sizeof(*st));
426 if (indio_dev == NULL) {
431 st = iio_priv(indio_dev);
432 mutex_init(&st->lock);
434 iio_dummy_init_device(indio_dev);
436 * With hardware: Set the parent device.
437 * indio_dev->dev.parent = &spi->dev;
438 * indio_dev->dev.parent = &client->dev;
442 * Make the iio_dev struct available to remove function.
444 * i2c_set_clientdata(client, indio_dev);
445 * spi_set_drvdata(spi, indio_dev);
447 iio_dummy_devs[index] = indio_dev;
451 * Set the device name.
453 * This is typically a part number and obtained from the module
455 * e.g. for i2c and spi:
456 * indio_dev->name = id->name;
457 * indio_dev->name = spi_get_device_id(spi)->name;
459 indio_dev->name = iio_dummy_part_number;
461 /* Provide description of available channels */
462 indio_dev->channels = iio_dummy_channels;
463 indio_dev->num_channels = ARRAY_SIZE(iio_dummy_channels);
466 * Provide device type specific interface functions and
469 indio_dev->info = &iio_dummy_info;
471 /* Specify that device provides sysfs type interfaces */
472 indio_dev->modes = INDIO_DIRECT_MODE;
474 ret = iio_simple_dummy_events_register(indio_dev);
476 goto error_free_device;
479 * Configure buffered capture support and register the channels with the
480 * buffer, but avoid the output channel being registered by reducing the
481 * number of channels by 1.
483 ret = iio_simple_dummy_configure_buffer(indio_dev,
484 iio_dummy_channels, 5);
486 goto error_unregister_events;
488 ret = iio_device_register(indio_dev);
490 goto error_unconfigure_buffer;
493 error_unconfigure_buffer:
494 iio_simple_dummy_unconfigure_buffer(indio_dev);
495 error_unregister_events:
496 iio_simple_dummy_events_unregister(indio_dev);
498 iio_device_free(indio_dev);
504 * iio_dummy_remove() - device instance removal function
505 * @index: device index.
507 * Parameters follow those of iio_dummy_probe for buses.
509 static int iio_dummy_remove(int index)
513 * Get a pointer to the device instance iio_dev structure
514 * from the bus subsystem. E.g.
515 * struct iio_dev *indio_dev = i2c_get_clientdata(client);
516 * struct iio_dev *indio_dev = spi_get_drvdata(spi);
518 struct iio_dev *indio_dev = iio_dummy_devs[index];
521 /* Unregister the device */
522 iio_device_unregister(indio_dev);
524 /* Device specific code to power down etc */
526 /* Buffered capture related cleanup */
527 iio_simple_dummy_unconfigure_buffer(indio_dev);
529 ret = iio_simple_dummy_events_unregister(indio_dev);
533 /* Free all structures */
534 iio_device_free(indio_dev);
541 * iio_dummy_init() - device driver registration
543 * Varies depending on bus type of the device. As there is no device
544 * here, call probe directly. For information on device registration
546 * Documentation/i2c/writing-clients
548 * Documentation/spi/spi-summary
550 static __init int iio_dummy_init(void)
553 if (instances > 10) {
559 iio_dummy_devs = kcalloc(instances, sizeof(*iio_dummy_devs),
561 /* Here we have no actual device so call probe */
562 for (i = 0; i < instances; i++) {
563 ret = iio_dummy_probe(i);
569 module_init(iio_dummy_init);
572 * iio_dummy_exit() - device driver removal
574 * Varies depending on bus type of the device.
575 * As there is no device here, call remove directly.
577 static __exit void iio_dummy_exit(void)
580 for (i = 0; i < instances; i++)
582 kfree(iio_dummy_devs);
584 module_exit(iio_dummy_exit);
586 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
587 MODULE_DESCRIPTION("IIO dummy driver");
588 MODULE_LICENSE("GPL v2");