staging:iio:dummy Add buffered reading support
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / iio / iio_simple_dummy.c
blobaf0c99236d4f5ac76b754e63a7548498aae612de
1 /**
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 "iio.h"
23 #include "sysfs.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";
41 /**
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 {
48 int val;
49 int val2;
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 */
68 .type = IIO_VOLTAGE,
69 /* Channel has a numeric index of 0 */
70 .indexed = 1,
71 .channel = 0,
72 /* What other information is available? */
73 .info_mask =
75 * in_voltage0_offset
76 * Offset for userspace to apply prior to scale
77 * when converting to standard units (microvolts)
79 (1 << IIO_CHAN_INFO_OFFSET_SEPARATE) |
81 * in_voltage0_scale
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
97 * a threshold
99 .event_mask = IIO_EV_BIT(IIO_EV_TYPE_THRESH,
100 IIO_EV_DIR_RISING),
101 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
103 /* Differential ADC channel in_voltage1-voltage2_raw etc*/
105 .type = IIO_VOLTAGE,
106 .differential = 1,
108 * Indexing for differential channels uses channel
109 * for the positive part, channel2 for the negative.
111 .indexed = 1,
112 .channel = 1,
113 .channel2 = 2,
114 .info_mask =
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*/
131 .type = IIO_VOLTAGE,
132 .differential = 1,
133 .indexed = 1,
134 .channel = 3,
135 .channel2 = 4,
136 .info_mask =
137 (1 << IIO_CHAN_INFO_SCALE_SHARED),
138 .scan_index = diffvoltage3m4,
139 .scan_type = {
140 .sign = 's',
141 .realbits = 11,
142 .storagebits = 16,
143 .shift = 0,
147 * 'modified' (i.e. axis specified) acceleration channel
148 * in_accel_z_raw
151 .type = IIO_ACCEL,
152 .modified = 1,
153 /* Channel 2 is use for modifiers */
154 .channel2 = IIO_MOD_X,
155 .info_mask =
157 * Internal bias correction value. Applied
158 * by the hardware or driver prior to userspace
159 * seeing the readings. Typically part of hardware
160 * calibration.
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
173 * the buffer.
175 IIO_CHAN_SOFT_TIMESTAMP(4),
176 /* DAC channel out_voltage0_raw */
178 .type = IIO_VOLTAGE,
179 .output = 1,
180 .indexed = 1,
181 .channel = 0,
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,
196 int *val,
197 int *val2,
198 long mask)
200 struct iio_dummy_state *st = iio_priv(indio_dev);
201 int ret = -EINVAL;
203 mutex_lock(&st->lock);
204 switch (mask) {
205 case 0: /* magic value - channel value read */
206 switch (chan->type) {
207 case IIO_VOLTAGE:
208 if (chan->output) {
209 /* Set integer part to cached value */
210 *val = st->dac_val;
211 ret = IIO_VAL_INT;
212 } else if (chan->differential) {
213 if (chan->channel == 1)
214 *val = st->differential_adc_val[0];
215 else
216 *val = st->differential_adc_val[1];
217 ret = IIO_VAL_INT;
218 } else {
219 *val = st->single_ended_adc_val;
220 ret = IIO_VAL_INT;
222 break;
223 case IIO_ACCEL:
224 *val = st->accel_val;
225 ret = IIO_VAL_INT;
226 break;
227 default:
228 break;
230 break;
231 case (1 << IIO_CHAN_INFO_OFFSET_SEPARATE):
232 /* only single ended adc -> 7 */
233 *val = 7;
234 ret = IIO_VAL_INT;
235 break;
236 case (1 << IIO_CHAN_INFO_SCALE_SEPARATE):
237 /* only single ended adc -> 0.001333 */
238 *val = 0;
239 *val2 = 1333;
240 ret = IIO_VAL_INT_PLUS_MICRO;
241 break;
242 case (1 << IIO_CHAN_INFO_SCALE_SHARED):
243 /* all differential adc channels -> 0.000001344 */
244 *val = 0;
245 *val2 = 1344;
246 ret = IIO_VAL_INT_PLUS_NANO;
247 break;
248 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
249 /* only the acceleration axis - read from cache */
250 *val = st->accel_calibbias;
251 ret = IIO_VAL_INT;
252 break;
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;
257 break;
258 default:
259 break;
261 mutex_unlock(&st->lock);
262 return ret;
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,
280 int val,
281 int val2,
282 long mask)
284 int i;
285 int ret = 0;
286 struct iio_dummy_state *st = iio_priv(indio_dev);
288 switch (mask) {
289 case 0:
290 if (chan->output == 0)
291 return -EINVAL;
293 /* Locking not required as writing single value */
294 mutex_lock(&st->lock);
295 st->dac_val = val;
296 mutex_unlock(&st->lock);
297 return 0;
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)
304 break;
305 if (i == ARRAY_SIZE(dummy_scales))
306 ret = -EINVAL;
307 else
308 st->accel_calibscale = &dummy_scales[i];
309 mutex_unlock(&st->lock);
310 return ret;
311 default:
312 return -EINVAL;
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);
342 st->dac_val = 0;
343 st->single_ended_adc_val = 73;
344 st->differential_adc_val[0] = 33;
345 st->differential_adc_val[1] = -34;
346 st->accel_val = 34;
347 st->accel_calibbias = -7;
348 st->accel_calibscale = &dummy_scales[0];
350 return 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)
364 int ret;
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) {
378 ret = -ENOMEM;
379 goto error_ret;
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.
394 * Bus equivalents
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
405 * id table.
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
418 * constant data.
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);
426 if (ret < 0)
427 goto error_free_device;
429 /* Configure buffered capture support. */
430 ret = iio_simple_dummy_configure_buffer(indio_dev);
431 if (ret < 0)
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);
439 if (ret < 0)
440 goto error_unconfigure_buffer;
442 ret = iio_device_register(indio_dev);
443 if (ret < 0)
444 goto error_unregister_buffer;
446 return 0;
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);
453 error_free_device:
454 /* Note free device should only be called, before registration
455 * has succeeded. */
456 iio_free_device(indio_dev);
457 error_ret:
458 return ret;
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)
469 int ret;
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);
489 if (ret)
490 goto error_ret;
492 /* Free all structures */
493 iio_free_device(indio_dev);
495 error_ret:
496 return ret;
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
504 * i2c:
505 * Documentation/i2c/writing-clients
506 * spi:
507 * Documentation/spi/spi-summary
509 static __init int iio_dummy_init(void)
511 int i, ret;
512 if (instances > 10) {
513 instances = 1;
514 return -EINVAL;
516 /* Fake a bus */
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);
521 if (ret < 0)
522 return ret;
524 return 0;
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)
536 int i;
537 for (i = 0; i < instances; i++)
538 iio_dummy_remove(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");