Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next
[linux-2.6.git] / drivers / staging / iio / iio_simple_dummy.c
blob0e8e02a3cf5bb1b933106c7c8cff50467f923568
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 <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";
42 /**
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 {
49 int val;
50 int val2;
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 */
69 .type = IIO_VOLTAGE,
70 /* Channel has a numeric index of 0 */
71 .indexed = 1,
72 .channel = 0,
73 /* What other information is available? */
74 .info_mask_separate =
76 * in_voltage0_raw
77 * Raw (unscaled no bias removal etc) measurement
78 * from the device.
80 BIT(IIO_CHAN_INFO_RAW) |
82 * in_voltage0_offset
83 * Offset for userspace to apply prior to scale
84 * when converting to standard units (microvolts)
86 BIT(IIO_CHAN_INFO_OFFSET) |
88 * in_voltage0_scale
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
104 * a threshold
106 .event_mask = IIO_EV_BIT(IIO_EV_TYPE_THRESH,
107 IIO_EV_DIR_RISING),
108 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
110 /* Differential ADC channel in_voltage1-voltage2_raw etc*/
112 .type = IIO_VOLTAGE,
113 .differential = 1,
115 * Indexing for differential channels uses channel
116 * for the positive part, channel2 for the negative.
118 .indexed = 1,
119 .channel = 1,
120 .channel2 = 2,
122 * in_voltage1-voltage2_raw
123 * Raw (unscaled no bias removal etc) measurement
124 * from the device.
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*/
143 .type = IIO_VOLTAGE,
144 .differential = 1,
145 .indexed = 1,
146 .channel = 3,
147 .channel2 = 4,
148 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
149 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
150 .scan_index = diffvoltage3m4,
151 .scan_type = {
152 .sign = 's',
153 .realbits = 11,
154 .storagebits = 16,
155 .shift = 0,
159 * 'modified' (i.e. axis specified) acceleration channel
160 * in_accel_z_raw
163 .type = IIO_ACCEL,
164 .modified = 1,
165 /* Channel 2 is use for modifiers */
166 .channel2 = IIO_MOD_X,
167 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
169 * Internal bias and gain correction values. Applied
170 * by the hardware or driver prior to userspace
171 * seeing the readings. Typically part of hardware
172 * calibration.
174 BIT(IIO_CHAN_INFO_CALIBSCALE) |
175 BIT(IIO_CHAN_INFO_CALIBBIAS),
176 .scan_index = accelx,
177 .scan_type = { /* Description of storage in buffer */
178 .sign = 's', /* signed */
179 .realbits = 16, /* 16 bits */
180 .storagebits = 16, /* 16 bits used for storage */
181 .shift = 0, /* zero shift */
185 * Convenience macro for timestamps. 4 is the index in
186 * the buffer.
188 IIO_CHAN_SOFT_TIMESTAMP(4),
189 /* DAC channel out_voltage0_raw */
191 .type = IIO_VOLTAGE,
192 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
193 .output = 1,
194 .indexed = 1,
195 .channel = 0,
200 * iio_dummy_read_raw() - data read function.
201 * @indio_dev: the struct iio_dev associated with this device instance
202 * @chan: the channel whose data is to be read
203 * @val: first element of returned value (typically INT)
204 * @val2: second element of returned value (typically MICRO)
205 * @mask: what we actually want to read as per the info_mask_*
206 * in iio_chan_spec.
208 static int iio_dummy_read_raw(struct iio_dev *indio_dev,
209 struct iio_chan_spec const *chan,
210 int *val,
211 int *val2,
212 long mask)
214 struct iio_dummy_state *st = iio_priv(indio_dev);
215 int ret = -EINVAL;
217 mutex_lock(&st->lock);
218 switch (mask) {
219 case IIO_CHAN_INFO_RAW: /* magic value - channel value read */
220 switch (chan->type) {
221 case IIO_VOLTAGE:
222 if (chan->output) {
223 /* Set integer part to cached value */
224 *val = st->dac_val;
225 ret = IIO_VAL_INT;
226 } else if (chan->differential) {
227 if (chan->channel == 1)
228 *val = st->differential_adc_val[0];
229 else
230 *val = st->differential_adc_val[1];
231 ret = IIO_VAL_INT;
232 } else {
233 *val = st->single_ended_adc_val;
234 ret = IIO_VAL_INT;
236 break;
237 case IIO_ACCEL:
238 *val = st->accel_val;
239 ret = IIO_VAL_INT;
240 break;
241 default:
242 break;
244 break;
245 case IIO_CHAN_INFO_OFFSET:
246 /* only single ended adc -> 7 */
247 *val = 7;
248 ret = IIO_VAL_INT;
249 break;
250 case IIO_CHAN_INFO_SCALE:
251 switch (chan->differential) {
252 case 0:
253 /* only single ended adc -> 0.001333 */
254 *val = 0;
255 *val2 = 1333;
256 ret = IIO_VAL_INT_PLUS_MICRO;
257 break;
258 case 1:
259 /* all differential adc channels -> 0.000001344 */
260 *val = 0;
261 *val2 = 1344;
262 ret = IIO_VAL_INT_PLUS_NANO;
264 break;
265 case IIO_CHAN_INFO_CALIBBIAS:
266 /* only the acceleration axis - read from cache */
267 *val = st->accel_calibbias;
268 ret = IIO_VAL_INT;
269 break;
270 case IIO_CHAN_INFO_CALIBSCALE:
271 *val = st->accel_calibscale->val;
272 *val2 = st->accel_calibscale->val2;
273 ret = IIO_VAL_INT_PLUS_MICRO;
274 break;
275 default:
276 break;
278 mutex_unlock(&st->lock);
279 return ret;
283 * iio_dummy_write_raw() - data write function.
284 * @indio_dev: the struct iio_dev associated with this device instance
285 * @chan: the channel whose data is to be written
286 * @val: first element of value to set (typically INT)
287 * @val2: second element of value to set (typically MICRO)
288 * @mask: what we actually want to write as per the info_mask_*
289 * in iio_chan_spec.
291 * Note that all raw writes are assumed IIO_VAL_INT and info mask elements
292 * are assumed to be IIO_INT_PLUS_MICRO unless the callback write_raw_get_fmt
293 * in struct iio_info is provided by the driver.
295 static int iio_dummy_write_raw(struct iio_dev *indio_dev,
296 struct iio_chan_spec const *chan,
297 int val,
298 int val2,
299 long mask)
301 int i;
302 int ret = 0;
303 struct iio_dummy_state *st = iio_priv(indio_dev);
305 switch (mask) {
306 case IIO_CHAN_INFO_RAW:
307 if (chan->output == 0)
308 return -EINVAL;
310 /* Locking not required as writing single value */
311 mutex_lock(&st->lock);
312 st->dac_val = val;
313 mutex_unlock(&st->lock);
314 return 0;
315 case IIO_CHAN_INFO_CALIBSCALE:
316 mutex_lock(&st->lock);
317 /* Compare against table - hard matching here */
318 for (i = 0; i < ARRAY_SIZE(dummy_scales); i++)
319 if (val == dummy_scales[i].val &&
320 val2 == dummy_scales[i].val2)
321 break;
322 if (i == ARRAY_SIZE(dummy_scales))
323 ret = -EINVAL;
324 else
325 st->accel_calibscale = &dummy_scales[i];
326 mutex_unlock(&st->lock);
327 return ret;
328 case IIO_CHAN_INFO_CALIBBIAS:
329 mutex_lock(&st->lock);
330 st->accel_calibbias = val;
331 mutex_unlock(&st->lock);
332 return 0;
334 default:
335 return -EINVAL;
340 * Device type specific information.
342 static const struct iio_info iio_dummy_info = {
343 .driver_module = THIS_MODULE,
344 .read_raw = &iio_dummy_read_raw,
345 .write_raw = &iio_dummy_write_raw,
346 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
347 .read_event_config = &iio_simple_dummy_read_event_config,
348 .write_event_config = &iio_simple_dummy_write_event_config,
349 .read_event_value = &iio_simple_dummy_read_event_value,
350 .write_event_value = &iio_simple_dummy_write_event_value,
351 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
355 * iio_dummy_init_device() - device instance specific init
356 * @indio_dev: the iio device structure
358 * Most drivers have one of these to set up default values,
359 * reset the device to known state etc.
361 static int iio_dummy_init_device(struct iio_dev *indio_dev)
363 struct iio_dummy_state *st = iio_priv(indio_dev);
365 st->dac_val = 0;
366 st->single_ended_adc_val = 73;
367 st->differential_adc_val[0] = 33;
368 st->differential_adc_val[1] = -34;
369 st->accel_val = 34;
370 st->accel_calibbias = -7;
371 st->accel_calibscale = &dummy_scales[0];
373 return 0;
377 * iio_dummy_probe() - device instance probe
378 * @index: an id number for this instance.
380 * Arguments are bus type specific.
381 * I2C: iio_dummy_probe(struct i2c_client *client,
382 * const struct i2c_device_id *id)
383 * SPI: iio_dummy_probe(struct spi_device *spi)
385 static int iio_dummy_probe(int index)
387 int ret;
388 struct iio_dev *indio_dev;
389 struct iio_dummy_state *st;
392 * Allocate an IIO device.
394 * This structure contains all generic state
395 * information about the device instance.
396 * It also has a region (accessed by iio_priv()
397 * for chip specific state information.
399 indio_dev = iio_device_alloc(sizeof(*st));
400 if (indio_dev == NULL) {
401 ret = -ENOMEM;
402 goto error_ret;
405 st = iio_priv(indio_dev);
406 mutex_init(&st->lock);
408 iio_dummy_init_device(indio_dev);
410 * With hardware: Set the parent device.
411 * indio_dev->dev.parent = &spi->dev;
412 * indio_dev->dev.parent = &client->dev;
416 * Make the iio_dev struct available to remove function.
417 * Bus equivalents
418 * i2c_set_clientdata(client, indio_dev);
419 * spi_set_drvdata(spi, indio_dev);
421 iio_dummy_devs[index] = indio_dev;
425 * Set the device name.
427 * This is typically a part number and obtained from the module
428 * id table.
429 * e.g. for i2c and spi:
430 * indio_dev->name = id->name;
431 * indio_dev->name = spi_get_device_id(spi)->name;
433 indio_dev->name = iio_dummy_part_number;
435 /* Provide description of available channels */
436 indio_dev->channels = iio_dummy_channels;
437 indio_dev->num_channels = ARRAY_SIZE(iio_dummy_channels);
440 * Provide device type specific interface functions and
441 * constant data.
443 indio_dev->info = &iio_dummy_info;
445 /* Specify that device provides sysfs type interfaces */
446 indio_dev->modes = INDIO_DIRECT_MODE;
448 ret = iio_simple_dummy_events_register(indio_dev);
449 if (ret < 0)
450 goto error_free_device;
453 * Configure buffered capture support and register the channels with the
454 * buffer, but avoid the output channel being registered by reducing the
455 * number of channels by 1.
457 ret = iio_simple_dummy_configure_buffer(indio_dev, iio_dummy_channels, 5);
458 if (ret < 0)
459 goto error_unregister_events;
461 ret = iio_device_register(indio_dev);
462 if (ret < 0)
463 goto error_unconfigure_buffer;
465 return 0;
466 error_unconfigure_buffer:
467 iio_simple_dummy_unconfigure_buffer(indio_dev);
468 error_unregister_events:
469 iio_simple_dummy_events_unregister(indio_dev);
470 error_free_device:
471 iio_device_free(indio_dev);
472 error_ret:
473 return ret;
477 * iio_dummy_remove() - device instance removal function
478 * @index: device index.
480 * Parameters follow those of iio_dummy_probe for buses.
482 static int iio_dummy_remove(int index)
484 int ret;
486 * Get a pointer to the device instance iio_dev structure
487 * from the bus subsystem. E.g.
488 * struct iio_dev *indio_dev = i2c_get_clientdata(client);
489 * struct iio_dev *indio_dev = spi_get_drvdata(spi);
491 struct iio_dev *indio_dev = iio_dummy_devs[index];
494 /* Unregister the device */
495 iio_device_unregister(indio_dev);
497 /* Device specific code to power down etc */
499 /* Buffered capture related cleanup */
500 iio_simple_dummy_unconfigure_buffer(indio_dev);
502 ret = iio_simple_dummy_events_unregister(indio_dev);
503 if (ret)
504 goto error_ret;
506 /* Free all structures */
507 iio_device_free(indio_dev);
509 error_ret:
510 return ret;
514 * iio_dummy_init() - device driver registration
516 * Varies depending on bus type of the device. As there is no device
517 * here, call probe directly. For information on device registration
518 * i2c:
519 * Documentation/i2c/writing-clients
520 * spi:
521 * Documentation/spi/spi-summary
523 static __init int iio_dummy_init(void)
525 int i, ret;
526 if (instances > 10) {
527 instances = 1;
528 return -EINVAL;
531 /* Fake a bus */
532 iio_dummy_devs = kcalloc(instances, sizeof(*iio_dummy_devs),
533 GFP_KERNEL);
534 /* Here we have no actual device so call probe */
535 for (i = 0; i < instances; i++) {
536 ret = iio_dummy_probe(i);
537 if (ret < 0)
538 return ret;
540 return 0;
542 module_init(iio_dummy_init);
545 * iio_dummy_exit() - device driver removal
547 * Varies depending on bus type of the device.
548 * As there is no device here, call remove directly.
550 static __exit void iio_dummy_exit(void)
552 int i;
553 for (i = 0; i < instances; i++)
554 iio_dummy_remove(i);
555 kfree(iio_dummy_devs);
557 module_exit(iio_dummy_exit);
559 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
560 MODULE_DESCRIPTION("IIO dummy driver");
561 MODULE_LICENSE("GPL v2");