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>
24 #include "buffer_generic.h"
25 #include "iio_simple_dummy.h"
28 * A few elements needed to fake a bus for this driver
29 * Note instances parmeter controls how many of these
30 * dummy devices are registered.
32 static unsigned instances
= 1;
33 module_param(instances
, int, 0);
35 /* Pointer array used to fake bus elements */
36 static struct iio_dev
**iio_dummy_devs
;
38 /* Fake a name for the part number, usually obtained from the id table */
39 static const char *iio_dummy_part_number
= "iio_dummy_part_no";
42 * struct iio_dummy_accel_calibscale - realworld to register mapping
43 * @val: first value in read_raw - here integer part.
44 * @val2: second value in read_raw etc - here micro part.
45 * @regval: register value - magic device specific numbers.
47 struct iio_dummy_accel_calibscale
{
50 int regval
; /* what would be written to hardware */
53 static const struct iio_dummy_accel_calibscale dummy_scales
[] = {
54 { 0, 100, 0x8 }, /* 0.000100 */
55 { 0, 133, 0x7 }, /* 0.000133 */
56 { 733, 13, 0x9 }, /* 733.00013 */
60 * iio_dummy_channels - Description of available channels
62 * This array of structures tells the IIO core about what the device
63 * actually provides for a given channel.
65 static struct iio_chan_spec iio_dummy_channels
[] = {
66 /* indexed ADC channel in_voltage0_raw etc */
69 /* Channel has a numeric index of 0 */
72 /* What other information is available? */
76 * Offset for userspace to apply prior to scale
77 * when converting to standard units (microvolts)
79 (1 << IIO_CHAN_INFO_OFFSET_SEPARATE
) |
82 * Multipler for userspace to apply post offset
83 * when converting to standard units (microvolts)
85 (1 << IIO_CHAN_INFO_SCALE_SEPARATE
),
86 /* The ordering of elements in the buffer via an enum */
87 .scan_index
= voltage0
,
88 .scan_type
= { /* Description of storage in buffer */
89 .sign
= 'u', /* unsigned */
90 .realbits
= 13, /* 13 bits */
91 .storagebits
= 16, /* 16 bits used for storage */
92 .shift
= 0, /* zero shift */
94 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
96 * simple event - triggered when value rises above
99 .event_mask
= IIO_EV_BIT(IIO_EV_TYPE_THRESH
,
101 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
103 /* Differential ADC channel in_voltage1-voltage2_raw etc*/
108 * Indexing for differential channels uses channel
109 * for the positive part, channel2 for the negative.
116 * in_voltage-voltage_scale
117 * Shared version of scale - shared by differential
118 * input channels of type IIO_VOLTAGE.
120 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
121 .scan_index
= diffvoltage1m2
,
122 .scan_type
= { /* Description of storage in buffer */
123 .sign
= 's', /* signed */
124 .realbits
= 12, /* 12 bits */
125 .storagebits
= 16, /* 16 bits used for storage */
126 .shift
= 0, /* zero shift */
129 /* Differential ADC channel in_voltage3-voltage4_raw etc*/
137 (1 << IIO_CHAN_INFO_SCALE_SHARED
),
138 .scan_index
= diffvoltage3m4
,
147 * 'modified' (i.e. axis specified) acceleration channel
153 /* Channel 2 is use for modifiers */
154 .channel2
= IIO_MOD_X
,
157 * Internal bias correction value. Applied
158 * by the hardware or driver prior to userspace
159 * seeing the readings. Typically part of hardware
162 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE
),
163 .scan_index
= accelx
,
164 .scan_type
= { /* Description of storage in buffer */
165 .sign
= 's', /* signed */
166 .realbits
= 16, /* 12 bits */
167 .storagebits
= 16, /* 16 bits used for storage */
168 .shift
= 0, /* zero shift */
172 * Convenience macro for timestamps. 4 is the index in
175 IIO_CHAN_SOFT_TIMESTAMP(4),
176 /* DAC channel out_voltage0_raw */
186 * iio_dummy_read_raw() - data read function.
187 * @indio_dev: the struct iio_dev associated with this device instance
188 * @chan: the channel whose data is to be read
189 * @val: first element of returned value (typically INT)
190 * @val2: second element of returned value (typically MICRO)
191 * @mask: what we actually want to read. 0 is the channel, everything else
192 * is as per the info_mask in iio_chan_spec.
194 static int iio_dummy_read_raw(struct iio_dev
*indio_dev
,
195 struct iio_chan_spec
const *chan
,
200 struct iio_dummy_state
*st
= iio_priv(indio_dev
);
203 mutex_lock(&st
->lock
);
205 case 0: /* magic value - channel value read */
206 switch (chan
->type
) {
209 /* Set integer part to cached value */
212 } else if (chan
->differential
) {
213 if (chan
->channel
== 1)
214 *val
= st
->differential_adc_val
[0];
216 *val
= st
->differential_adc_val
[1];
219 *val
= st
->single_ended_adc_val
;
224 *val
= st
->accel_val
;
231 case (1 << IIO_CHAN_INFO_OFFSET_SEPARATE
):
232 /* only single ended adc -> 7 */
236 case (1 << IIO_CHAN_INFO_SCALE_SEPARATE
):
237 /* only single ended adc -> 0.001333 */
240 ret
= IIO_VAL_INT_PLUS_MICRO
;
242 case (1 << IIO_CHAN_INFO_SCALE_SHARED
):
243 /* all differential adc channels -> 0.000001344 */
246 ret
= IIO_VAL_INT_PLUS_NANO
;
248 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE
):
249 /* only the acceleration axis - read from cache */
250 *val
= st
->accel_calibbias
;
253 case (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE
):
254 *val
= st
->accel_calibscale
->val
;
255 *val2
= st
->accel_calibscale
->val2
;
256 ret
= IIO_VAL_INT_PLUS_MICRO
;
261 mutex_unlock(&st
->lock
);
266 * iio_dummy_write_raw() - data write function.
267 * @indio_dev: the struct iio_dev associated with this device instance
268 * @chan: the channel whose data is to be read
269 * @val: first element of returned value (typically INT)
270 * @val2: second element of returned value (typically MICRO)
271 * @mask: what we actually want to read. 0 is the channel, everything else
272 * is as per the info_mask in iio_chan_spec.
274 * Note that all raw writes are assumed IIO_VAL_INT and info mask elements
275 * are assumed to be IIO_INT_PLUS_MICRO unless the callback write_raw_get_fmt
276 * in struct iio_info is provided by the driver.
278 static int iio_dummy_write_raw(struct iio_dev
*indio_dev
,
279 struct iio_chan_spec
const *chan
,
286 struct iio_dummy_state
*st
= iio_priv(indio_dev
);
290 if (chan
->output
== 0)
293 /* Locking not required as writing single value */
294 mutex_lock(&st
->lock
);
296 mutex_unlock(&st
->lock
);
298 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE
):
299 mutex_lock(&st
->lock
);
300 /* Compare against table - hard matching here */
301 for (i
= 0; i
< ARRAY_SIZE(dummy_scales
); i
++)
302 if (val
== dummy_scales
[i
].val
&&
303 val2
== dummy_scales
[i
].val2
)
305 if (i
== ARRAY_SIZE(dummy_scales
))
308 st
->accel_calibscale
= &dummy_scales
[i
];
309 mutex_unlock(&st
->lock
);
317 * Device type specific information.
319 static const struct iio_info iio_dummy_info
= {
320 .driver_module
= THIS_MODULE
,
321 .read_raw
= &iio_dummy_read_raw
,
322 .write_raw
= &iio_dummy_write_raw
,
323 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
324 .read_event_config
= &iio_simple_dummy_read_event_config
,
325 .write_event_config
= &iio_simple_dummy_write_event_config
,
326 .read_event_value
= &iio_simple_dummy_read_event_value
,
327 .write_event_value
= &iio_simple_dummy_write_event_value
,
328 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
332 * iio_dummy_init_device() - device instance specific init
333 * @indio_dev: the iio device structure
335 * Most drivers have one of these to set up default values,
336 * reset the device to known state etc.
338 static int iio_dummy_init_device(struct iio_dev
*indio_dev
)
340 struct iio_dummy_state
*st
= iio_priv(indio_dev
);
343 st
->single_ended_adc_val
= 73;
344 st
->differential_adc_val
[0] = 33;
345 st
->differential_adc_val
[1] = -34;
347 st
->accel_calibbias
= -7;
348 st
->accel_calibscale
= &dummy_scales
[0];
354 * iio_dummy_probe() - device instance probe
355 * @index: an id number for this instance.
357 * Arguments are bus type specific.
358 * I2C: iio_dummy_probe(struct i2c_client *client,
359 * const struct i2c_device_id *id)
360 * SPI: iio_dummy_probe(struct spi_device *spi)
362 static int __devinit
iio_dummy_probe(int index
)
365 struct iio_dev
*indio_dev
;
366 struct iio_dummy_state
*st
;
369 * Allocate an IIO device.
371 * This structure contains all generic state
372 * information about the device instance.
373 * It also has a region (accessed by iio_priv()
374 * for chip specific state information.
376 indio_dev
= iio_allocate_device(sizeof(*st
));
377 if (indio_dev
== NULL
) {
382 st
= iio_priv(indio_dev
);
383 mutex_init(&st
->lock
);
385 iio_dummy_init_device(indio_dev
);
387 * With hardware: Set the parent device.
388 * indio_dev->dev.parent = &spi->dev;
389 * indio_dev->dev.parent = &client->dev;
393 * Make the iio_dev struct available to remove function.
395 * i2c_set_clientdata(client, indio_dev);
396 * spi_set_drvdata(spi, indio_dev);
398 iio_dummy_devs
[index
] = indio_dev
;
402 * Set the device name.
404 * This is typically a part number and obtained from the module
406 * e.g. for i2c and spi:
407 * indio_dev->name = id->name;
408 * indio_dev->name = spi_get_device_id(spi)->name;
410 indio_dev
->name
= iio_dummy_part_number
;
412 /* Provide description of available channels */
413 indio_dev
->channels
= iio_dummy_channels
;
414 indio_dev
->num_channels
= ARRAY_SIZE(iio_dummy_channels
);
417 * Provide device type specific interface functions and
420 indio_dev
->info
= &iio_dummy_info
;
422 /* Specify that device provides sysfs type interfaces */
423 indio_dev
->modes
= INDIO_DIRECT_MODE
;
425 ret
= iio_simple_dummy_events_register(indio_dev
);
427 goto error_free_device
;
429 /* Configure buffered capture support. */
430 ret
= iio_simple_dummy_configure_buffer(indio_dev
);
432 goto error_unregister_events
;
435 * Register the channels with the buffer, but avoid the output
436 * channel being registered by reducing the number of channels by 1.
438 ret
= iio_buffer_register(indio_dev
, iio_dummy_channels
, 5);
440 goto error_unconfigure_buffer
;
442 ret
= iio_device_register(indio_dev
);
444 goto error_unregister_buffer
;
447 error_unregister_buffer
:
448 iio_buffer_unregister(indio_dev
);
449 error_unconfigure_buffer
:
450 iio_simple_dummy_unconfigure_buffer(indio_dev
);
451 error_unregister_events
:
452 iio_simple_dummy_events_unregister(indio_dev
);
454 /* Note free device should only be called, before registration
456 iio_free_device(indio_dev
);
462 * iio_dummy_remove() - device instance removal function
463 * @index: device index.
465 * Parameters follow those of iio_dummy_probe for buses.
467 static int iio_dummy_remove(int index
)
471 * Get a pointer to the device instance iio_dev structure
472 * from the bus subsystem. E.g.
473 * struct iio_dev *indio_dev = i2c_get_clientdata(client);
474 * struct iio_dev *indio_dev = spi_get_drvdata(spi);
476 struct iio_dev
*indio_dev
= iio_dummy_devs
[index
];
479 /* Unregister the device */
480 iio_device_unregister(indio_dev
);
482 /* Device specific code to power down etc */
484 /* Buffered capture related cleanup */
485 iio_buffer_unregister(indio_dev
);
486 iio_simple_dummy_unconfigure_buffer(indio_dev
);
488 ret
= iio_simple_dummy_events_unregister(indio_dev
);
492 /* Free all structures */
493 iio_free_device(indio_dev
);
500 * iio_dummy_init() - device driver registration
502 * Varies depending on bus type of the device. As there is no device
503 * here, call probe directly. For information on device registration
505 * Documentation/i2c/writing-clients
507 * Documentation/spi/spi-summary
509 static __init
int iio_dummy_init(void)
512 if (instances
> 10) {
517 iio_dummy_devs
= kzalloc(sizeof(*iio_dummy_devs
)*instances
, GFP_KERNEL
);
518 /* Here we have no actual device so call probe */
519 for (i
= 0; i
< instances
; i
++) {
520 ret
= iio_dummy_probe(i
);
526 module_init(iio_dummy_init
);
529 * iio_dummy_exit() - device driver removal
531 * Varies depending on bus type of the device.
532 * As there is no device here, call remove directly.
534 static __exit
void iio_dummy_exit(void)
537 for (i
= 0; i
< instances
; i
++)
539 kfree(iio_dummy_devs
);
541 module_exit(iio_dummy_exit
);
543 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
544 MODULE_DESCRIPTION("IIO dummy driver");
545 MODULE_LICENSE("GPL v2");