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),
95 * The frequency in Hz at which the channels are sampled
97 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
98 /* The ordering of elements in the buffer via an enum */
99 .scan_index = voltage0,
100 .scan_type = { /* Description of storage in buffer */
101 .sign = 'u', /* unsigned */
102 .realbits = 13, /* 13 bits */
103 .storagebits = 16, /* 16 bits used for storage */
104 .shift = 0, /* zero shift */
106 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
108 * simple event - triggered when value rises above
111 .event_mask = IIO_EV_BIT(IIO_EV_TYPE_THRESH,
113 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
115 /* Differential ADC channel in_voltage1-voltage2_raw etc*/
120 * Indexing for differential channels uses channel
121 * for the positive part, channel2 for the negative.
127 * in_voltage1-voltage2_raw
128 * Raw (unscaled no bias removal etc) measurement
131 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
133 * in_voltage-voltage_scale
134 * Shared version of scale - shared by differential
135 * input channels of type IIO_VOLTAGE.
137 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
140 * The frequency in Hz at which the channels are sampled
142 .scan_index = diffvoltage1m2,
143 .scan_type = { /* Description of storage in buffer */
144 .sign = 's', /* signed */
145 .realbits = 12, /* 12 bits */
146 .storagebits = 16, /* 16 bits used for storage */
147 .shift = 0, /* zero shift */
150 /* Differential ADC channel in_voltage3-voltage4_raw etc*/
157 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
158 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
159 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
160 .scan_index = diffvoltage3m4,
169 * 'modified' (i.e. axis specified) acceleration channel
175 /* Channel 2 is use for modifiers */
176 .channel2 = IIO_MOD_X,
177 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
179 * Internal bias and gain correction values. Applied
180 * by the hardware or driver prior to userspace
181 * seeing the readings. Typically part of hardware
184 BIT(IIO_CHAN_INFO_CALIBSCALE) |
185 BIT(IIO_CHAN_INFO_CALIBBIAS),
186 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
187 .scan_index = accelx,
188 .scan_type = { /* Description of storage in buffer */
189 .sign = 's', /* signed */
190 .realbits = 16, /* 16 bits */
191 .storagebits = 16, /* 16 bits used for storage */
192 .shift = 0, /* zero shift */
196 * Convenience macro for timestamps. 4 is the index in
199 IIO_CHAN_SOFT_TIMESTAMP(4),
200 /* DAC channel out_voltage0_raw */
203 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
211 * iio_dummy_read_raw() - data read function.
212 * @indio_dev: the struct iio_dev associated with this device instance
213 * @chan: the channel whose data is to be read
214 * @val: first element of returned value (typically INT)
215 * @val2: second element of returned value (typically MICRO)
216 * @mask: what we actually want to read as per the info_mask_*
219 static int iio_dummy_read_raw(struct iio_dev *indio_dev,
220 struct iio_chan_spec const *chan,
225 struct iio_dummy_state *st = iio_priv(indio_dev);
228 mutex_lock(&st->lock);
230 case IIO_CHAN_INFO_RAW: /* magic value - channel value read */
231 switch (chan->type) {
234 /* Set integer part to cached value */
237 } else if (chan->differential) {
238 if (chan->channel == 1)
239 *val = st->differential_adc_val[0];
241 *val = st->differential_adc_val[1];
244 *val = st->single_ended_adc_val;
249 *val = st->accel_val;
256 case IIO_CHAN_INFO_OFFSET:
257 /* only single ended adc -> 7 */
261 case IIO_CHAN_INFO_SCALE:
262 switch (chan->differential) {
264 /* only single ended adc -> 0.001333 */
267 ret = IIO_VAL_INT_PLUS_MICRO;
270 /* all differential adc channels -> 0.000001344 */
273 ret = IIO_VAL_INT_PLUS_NANO;
276 case IIO_CHAN_INFO_CALIBBIAS:
277 /* only the acceleration axis - read from cache */
278 *val = st->accel_calibbias;
281 case IIO_CHAN_INFO_CALIBSCALE:
282 *val = st->accel_calibscale->val;
283 *val2 = st->accel_calibscale->val2;
284 ret = IIO_VAL_INT_PLUS_MICRO;
286 case IIO_CHAN_INFO_SAMP_FREQ:
289 ret = IIO_VAL_INT_PLUS_NANO;
294 mutex_unlock(&st->lock);
299 * iio_dummy_write_raw() - data write function.
300 * @indio_dev: the struct iio_dev associated with this device instance
301 * @chan: the channel whose data is to be written
302 * @val: first element of value to set (typically INT)
303 * @val2: second element of value to set (typically MICRO)
304 * @mask: what we actually want to write as per the info_mask_*
307 * Note that all raw writes are assumed IIO_VAL_INT and info mask elements
308 * are assumed to be IIO_INT_PLUS_MICRO unless the callback write_raw_get_fmt
309 * in struct iio_info is provided by the driver.
311 static int iio_dummy_write_raw(struct iio_dev *indio_dev,
312 struct iio_chan_spec const *chan,
319 struct iio_dummy_state *st = iio_priv(indio_dev);
322 case IIO_CHAN_INFO_RAW:
323 if (chan->output == 0)
326 /* Locking not required as writing single value */
327 mutex_lock(&st->lock);
329 mutex_unlock(&st->lock);
331 case IIO_CHAN_INFO_CALIBSCALE:
332 mutex_lock(&st->lock);
333 /* Compare against table - hard matching here */
334 for (i = 0; i < ARRAY_SIZE(dummy_scales); i++)
335 if (val == dummy_scales[i].val &&
336 val2 == dummy_scales[i].val2)
338 if (i == ARRAY_SIZE(dummy_scales))
341 st->accel_calibscale = &dummy_scales[i];
342 mutex_unlock(&st->lock);
344 case IIO_CHAN_INFO_CALIBBIAS:
345 mutex_lock(&st->lock);
346 st->accel_calibbias = val;
347 mutex_unlock(&st->lock);
356 * Device type specific information.
358 static const struct iio_info iio_dummy_info = {
359 .driver_module = THIS_MODULE,
360 .read_raw = &iio_dummy_read_raw,
361 .write_raw = &iio_dummy_write_raw,
362 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
363 .read_event_config = &iio_simple_dummy_read_event_config,
364 .write_event_config = &iio_simple_dummy_write_event_config,
365 .read_event_value = &iio_simple_dummy_read_event_value,
366 .write_event_value = &iio_simple_dummy_write_event_value,
367 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
371 * iio_dummy_init_device() - device instance specific init
372 * @indio_dev: the iio device structure
374 * Most drivers have one of these to set up default values,
375 * reset the device to known state etc.
377 static int iio_dummy_init_device(struct iio_dev *indio_dev)
379 struct iio_dummy_state *st = iio_priv(indio_dev);
382 st->single_ended_adc_val = 73;
383 st->differential_adc_val[0] = 33;
384 st->differential_adc_val[1] = -34;
386 st->accel_calibbias = -7;
387 st->accel_calibscale = &dummy_scales[0];
393 * iio_dummy_probe() - device instance probe
394 * @index: an id number for this instance.
396 * Arguments are bus type specific.
397 * I2C: iio_dummy_probe(struct i2c_client *client,
398 * const struct i2c_device_id *id)
399 * SPI: iio_dummy_probe(struct spi_device *spi)
401 static int iio_dummy_probe(int index)
404 struct iio_dev *indio_dev;
405 struct iio_dummy_state *st;
408 * Allocate an IIO device.
410 * This structure contains all generic state
411 * information about the device instance.
412 * It also has a region (accessed by iio_priv()
413 * for chip specific state information.
415 indio_dev = iio_device_alloc(sizeof(*st));
416 if (indio_dev == NULL) {
421 st = iio_priv(indio_dev);
422 mutex_init(&st->lock);
424 iio_dummy_init_device(indio_dev);
426 * With hardware: Set the parent device.
427 * indio_dev->dev.parent = &spi->dev;
428 * indio_dev->dev.parent = &client->dev;
432 * Make the iio_dev struct available to remove function.
434 * i2c_set_clientdata(client, indio_dev);
435 * spi_set_drvdata(spi, indio_dev);
437 iio_dummy_devs[index] = indio_dev;
441 * Set the device name.
443 * This is typically a part number and obtained from the module
445 * e.g. for i2c and spi:
446 * indio_dev->name = id->name;
447 * indio_dev->name = spi_get_device_id(spi)->name;
449 indio_dev->name = iio_dummy_part_number;
451 /* Provide description of available channels */
452 indio_dev->channels = iio_dummy_channels;
453 indio_dev->num_channels = ARRAY_SIZE(iio_dummy_channels);
456 * Provide device type specific interface functions and
459 indio_dev->info = &iio_dummy_info;
461 /* Specify that device provides sysfs type interfaces */
462 indio_dev->modes = INDIO_DIRECT_MODE;
464 ret = iio_simple_dummy_events_register(indio_dev);
466 goto error_free_device;
469 * Configure buffered capture support and register the channels with the
470 * buffer, but avoid the output channel being registered by reducing the
471 * number of channels by 1.
473 ret = iio_simple_dummy_configure_buffer(indio_dev, iio_dummy_channels, 5);
475 goto error_unregister_events;
477 ret = iio_device_register(indio_dev);
479 goto error_unconfigure_buffer;
482 error_unconfigure_buffer:
483 iio_simple_dummy_unconfigure_buffer(indio_dev);
484 error_unregister_events:
485 iio_simple_dummy_events_unregister(indio_dev);
487 iio_device_free(indio_dev);
493 * iio_dummy_remove() - device instance removal function
494 * @index: device index.
496 * Parameters follow those of iio_dummy_probe for buses.
498 static int iio_dummy_remove(int index)
502 * Get a pointer to the device instance iio_dev structure
503 * from the bus subsystem. E.g.
504 * struct iio_dev *indio_dev = i2c_get_clientdata(client);
505 * struct iio_dev *indio_dev = spi_get_drvdata(spi);
507 struct iio_dev *indio_dev = iio_dummy_devs[index];
510 /* Unregister the device */
511 iio_device_unregister(indio_dev);
513 /* Device specific code to power down etc */
515 /* Buffered capture related cleanup */
516 iio_simple_dummy_unconfigure_buffer(indio_dev);
518 ret = iio_simple_dummy_events_unregister(indio_dev);
522 /* Free all structures */
523 iio_device_free(indio_dev);
530 * iio_dummy_init() - device driver registration
532 * Varies depending on bus type of the device. As there is no device
533 * here, call probe directly. For information on device registration
535 * Documentation/i2c/writing-clients
537 * Documentation/spi/spi-summary
539 static __init int iio_dummy_init(void)
542 if (instances > 10) {
548 iio_dummy_devs = kcalloc(instances, sizeof(*iio_dummy_devs),
550 /* Here we have no actual device so call probe */
551 for (i = 0; i < instances; i++) {
552 ret = iio_dummy_probe(i);
558 module_init(iio_dummy_init);
561 * iio_dummy_exit() - device driver removal
563 * Varies depending on bus type of the device.
564 * As there is no device here, call remove directly.
566 static __exit void iio_dummy_exit(void)
569 for (i = 0; i < instances; i++)
571 kfree(iio_dummy_devs);
573 module_exit(iio_dummy_exit);
575 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
576 MODULE_DESCRIPTION("IIO dummy driver");
577 MODULE_LICENSE("GPL v2");