Linux 4.16.11
[linux/fpc-iii.git] / drivers / iio / dummy / iio_simple_dummy.c
blob62052479c349d57f2f667cc70cba8f72e94c5817
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/string.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 <linux/iio/sw_device.h>
27 #include "iio_simple_dummy.h"
29 static const struct config_item_type iio_dummy_type = {
30 .ct_owner = THIS_MODULE,
33 /**
34 * struct iio_dummy_accel_calibscale - realworld to register mapping
35 * @val: first value in read_raw - here integer part.
36 * @val2: second value in read_raw etc - here micro part.
37 * @regval: register value - magic device specific numbers.
39 struct iio_dummy_accel_calibscale {
40 int val;
41 int val2;
42 int regval; /* what would be written to hardware */
45 static const struct iio_dummy_accel_calibscale dummy_scales[] = {
46 { 0, 100, 0x8 }, /* 0.000100 */
47 { 0, 133, 0x7 }, /* 0.000133 */
48 { 733, 13, 0x9 }, /* 733.000013 */
51 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
54 * simple event - triggered when value rises above
55 * a threshold
57 static const struct iio_event_spec iio_dummy_event = {
58 .type = IIO_EV_TYPE_THRESH,
59 .dir = IIO_EV_DIR_RISING,
60 .mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE),
64 * simple step detect event - triggered when a step is detected
66 static const struct iio_event_spec step_detect_event = {
67 .type = IIO_EV_TYPE_CHANGE,
68 .dir = IIO_EV_DIR_NONE,
69 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
73 * simple transition event - triggered when the reported running confidence
74 * value rises above a threshold value
76 static const struct iio_event_spec iio_running_event = {
77 .type = IIO_EV_TYPE_THRESH,
78 .dir = IIO_EV_DIR_RISING,
79 .mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE),
83 * simple transition event - triggered when the reported walking confidence
84 * value falls under a threshold value
86 static const struct iio_event_spec iio_walking_event = {
87 .type = IIO_EV_TYPE_THRESH,
88 .dir = IIO_EV_DIR_FALLING,
89 .mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE),
91 #endif
94 * iio_dummy_channels - Description of available channels
96 * This array of structures tells the IIO core about what the device
97 * actually provides for a given channel.
99 static const struct iio_chan_spec iio_dummy_channels[] = {
100 /* indexed ADC channel in_voltage0_raw etc */
102 .type = IIO_VOLTAGE,
103 /* Channel has a numeric index of 0 */
104 .indexed = 1,
105 .channel = 0,
106 /* What other information is available? */
107 .info_mask_separate =
109 * in_voltage0_raw
110 * Raw (unscaled no bias removal etc) measurement
111 * from the device.
113 BIT(IIO_CHAN_INFO_RAW) |
115 * in_voltage0_offset
116 * Offset for userspace to apply prior to scale
117 * when converting to standard units (microvolts)
119 BIT(IIO_CHAN_INFO_OFFSET) |
121 * in_voltage0_scale
122 * Multipler for userspace to apply post offset
123 * when converting to standard units (microvolts)
125 BIT(IIO_CHAN_INFO_SCALE),
127 * sampling_frequency
128 * The frequency in Hz at which the channels are sampled
130 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
131 /* The ordering of elements in the buffer via an enum */
132 .scan_index = DUMMY_INDEX_VOLTAGE_0,
133 .scan_type = { /* Description of storage in buffer */
134 .sign = 'u', /* unsigned */
135 .realbits = 13, /* 13 bits */
136 .storagebits = 16, /* 16 bits used for storage */
137 .shift = 0, /* zero shift */
139 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
140 .event_spec = &iio_dummy_event,
141 .num_event_specs = 1,
142 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
144 /* Differential ADC channel in_voltage1-voltage2_raw etc*/
146 .type = IIO_VOLTAGE,
147 .differential = 1,
149 * Indexing for differential channels uses channel
150 * for the positive part, channel2 for the negative.
152 .indexed = 1,
153 .channel = 1,
154 .channel2 = 2,
156 * in_voltage1-voltage2_raw
157 * Raw (unscaled no bias removal etc) measurement
158 * from the device.
160 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
162 * in_voltage-voltage_scale
163 * Shared version of scale - shared by differential
164 * input channels of type IIO_VOLTAGE.
166 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
168 * sampling_frequency
169 * The frequency in Hz at which the channels are sampled
171 .scan_index = DUMMY_INDEX_DIFFVOLTAGE_1M2,
172 .scan_type = { /* Description of storage in buffer */
173 .sign = 's', /* signed */
174 .realbits = 12, /* 12 bits */
175 .storagebits = 16, /* 16 bits used for storage */
176 .shift = 0, /* zero shift */
179 /* Differential ADC channel in_voltage3-voltage4_raw etc*/
181 .type = IIO_VOLTAGE,
182 .differential = 1,
183 .indexed = 1,
184 .channel = 3,
185 .channel2 = 4,
186 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
187 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
188 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
189 .scan_index = DUMMY_INDEX_DIFFVOLTAGE_3M4,
190 .scan_type = {
191 .sign = 's',
192 .realbits = 11,
193 .storagebits = 16,
194 .shift = 0,
198 * 'modified' (i.e. axis specified) acceleration channel
199 * in_accel_z_raw
202 .type = IIO_ACCEL,
203 .modified = 1,
204 /* Channel 2 is use for modifiers */
205 .channel2 = IIO_MOD_X,
206 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
208 * Internal bias and gain correction values. Applied
209 * by the hardware or driver prior to userspace
210 * seeing the readings. Typically part of hardware
211 * calibration.
213 BIT(IIO_CHAN_INFO_CALIBSCALE) |
214 BIT(IIO_CHAN_INFO_CALIBBIAS),
215 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
216 .scan_index = DUMMY_INDEX_ACCELX,
217 .scan_type = { /* Description of storage in buffer */
218 .sign = 's', /* signed */
219 .realbits = 16, /* 16 bits */
220 .storagebits = 16, /* 16 bits used for storage */
221 .shift = 0, /* zero shift */
225 * Convenience macro for timestamps. 4 is the index in
226 * the buffer.
228 IIO_CHAN_SOFT_TIMESTAMP(4),
229 /* DAC channel out_voltage0_raw */
231 .type = IIO_VOLTAGE,
232 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
233 .scan_index = -1, /* No buffer support */
234 .output = 1,
235 .indexed = 1,
236 .channel = 0,
239 .type = IIO_STEPS,
240 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_ENABLE) |
241 BIT(IIO_CHAN_INFO_CALIBHEIGHT),
242 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
243 .scan_index = -1, /* No buffer support */
244 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
245 .event_spec = &step_detect_event,
246 .num_event_specs = 1,
247 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
250 .type = IIO_ACTIVITY,
251 .modified = 1,
252 .channel2 = IIO_MOD_RUNNING,
253 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
254 .scan_index = -1, /* No buffer support */
255 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
256 .event_spec = &iio_running_event,
257 .num_event_specs = 1,
258 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
261 .type = IIO_ACTIVITY,
262 .modified = 1,
263 .channel2 = IIO_MOD_WALKING,
264 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
265 .scan_index = -1, /* No buffer support */
266 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
267 .event_spec = &iio_walking_event,
268 .num_event_specs = 1,
269 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
274 * iio_dummy_read_raw() - data read function.
275 * @indio_dev: the struct iio_dev associated with this device instance
276 * @chan: the channel whose data is to be read
277 * @val: first element of returned value (typically INT)
278 * @val2: second element of returned value (typically MICRO)
279 * @mask: what we actually want to read as per the info_mask_*
280 * in iio_chan_spec.
282 static int iio_dummy_read_raw(struct iio_dev *indio_dev,
283 struct iio_chan_spec const *chan,
284 int *val,
285 int *val2,
286 long mask)
288 struct iio_dummy_state *st = iio_priv(indio_dev);
289 int ret = -EINVAL;
291 mutex_lock(&st->lock);
292 switch (mask) {
293 case IIO_CHAN_INFO_RAW: /* magic value - channel value read */
294 switch (chan->type) {
295 case IIO_VOLTAGE:
296 if (chan->output) {
297 /* Set integer part to cached value */
298 *val = st->dac_val;
299 ret = IIO_VAL_INT;
300 } else if (chan->differential) {
301 if (chan->channel == 1)
302 *val = st->differential_adc_val[0];
303 else
304 *val = st->differential_adc_val[1];
305 ret = IIO_VAL_INT;
306 } else {
307 *val = st->single_ended_adc_val;
308 ret = IIO_VAL_INT;
310 break;
311 case IIO_ACCEL:
312 *val = st->accel_val;
313 ret = IIO_VAL_INT;
314 break;
315 default:
316 break;
318 break;
319 case IIO_CHAN_INFO_PROCESSED:
320 switch (chan->type) {
321 case IIO_STEPS:
322 *val = st->steps;
323 ret = IIO_VAL_INT;
324 break;
325 case IIO_ACTIVITY:
326 switch (chan->channel2) {
327 case IIO_MOD_RUNNING:
328 *val = st->activity_running;
329 ret = IIO_VAL_INT;
330 break;
331 case IIO_MOD_WALKING:
332 *val = st->activity_walking;
333 ret = IIO_VAL_INT;
334 break;
335 default:
336 break;
338 break;
339 default:
340 break;
342 break;
343 case IIO_CHAN_INFO_OFFSET:
344 /* only single ended adc -> 7 */
345 *val = 7;
346 ret = IIO_VAL_INT;
347 break;
348 case IIO_CHAN_INFO_SCALE:
349 switch (chan->type) {
350 case IIO_VOLTAGE:
351 switch (chan->differential) {
352 case 0:
353 /* only single ended adc -> 0.001333 */
354 *val = 0;
355 *val2 = 1333;
356 ret = IIO_VAL_INT_PLUS_MICRO;
357 break;
358 case 1:
359 /* all differential adc -> 0.000001344 */
360 *val = 0;
361 *val2 = 1344;
362 ret = IIO_VAL_INT_PLUS_NANO;
364 break;
365 default:
366 break;
368 break;
369 case IIO_CHAN_INFO_CALIBBIAS:
370 /* only the acceleration axis - read from cache */
371 *val = st->accel_calibbias;
372 ret = IIO_VAL_INT;
373 break;
374 case IIO_CHAN_INFO_CALIBSCALE:
375 *val = st->accel_calibscale->val;
376 *val2 = st->accel_calibscale->val2;
377 ret = IIO_VAL_INT_PLUS_MICRO;
378 break;
379 case IIO_CHAN_INFO_SAMP_FREQ:
380 *val = 3;
381 *val2 = 33;
382 ret = IIO_VAL_INT_PLUS_NANO;
383 break;
384 case IIO_CHAN_INFO_ENABLE:
385 switch (chan->type) {
386 case IIO_STEPS:
387 *val = st->steps_enabled;
388 ret = IIO_VAL_INT;
389 break;
390 default:
391 break;
393 break;
394 case IIO_CHAN_INFO_CALIBHEIGHT:
395 switch (chan->type) {
396 case IIO_STEPS:
397 *val = st->height;
398 ret = IIO_VAL_INT;
399 break;
400 default:
401 break;
403 break;
405 default:
406 break;
408 mutex_unlock(&st->lock);
409 return ret;
413 * iio_dummy_write_raw() - data write function.
414 * @indio_dev: the struct iio_dev associated with this device instance
415 * @chan: the channel whose data is to be written
416 * @val: first element of value to set (typically INT)
417 * @val2: second element of value to set (typically MICRO)
418 * @mask: what we actually want to write as per the info_mask_*
419 * in iio_chan_spec.
421 * Note that all raw writes are assumed IIO_VAL_INT and info mask elements
422 * are assumed to be IIO_INT_PLUS_MICRO unless the callback write_raw_get_fmt
423 * in struct iio_info is provided by the driver.
425 static int iio_dummy_write_raw(struct iio_dev *indio_dev,
426 struct iio_chan_spec const *chan,
427 int val,
428 int val2,
429 long mask)
431 int i;
432 int ret = 0;
433 struct iio_dummy_state *st = iio_priv(indio_dev);
435 switch (mask) {
436 case IIO_CHAN_INFO_RAW:
437 switch (chan->type) {
438 case IIO_VOLTAGE:
439 if (chan->output == 0)
440 return -EINVAL;
442 /* Locking not required as writing single value */
443 mutex_lock(&st->lock);
444 st->dac_val = val;
445 mutex_unlock(&st->lock);
446 return 0;
447 default:
448 return -EINVAL;
450 case IIO_CHAN_INFO_PROCESSED:
451 switch (chan->type) {
452 case IIO_STEPS:
453 mutex_lock(&st->lock);
454 st->steps = val;
455 mutex_unlock(&st->lock);
456 return 0;
457 case IIO_ACTIVITY:
458 if (val < 0)
459 val = 0;
460 if (val > 100)
461 val = 100;
462 switch (chan->channel2) {
463 case IIO_MOD_RUNNING:
464 st->activity_running = val;
465 return 0;
466 case IIO_MOD_WALKING:
467 st->activity_walking = val;
468 return 0;
469 default:
470 return -EINVAL;
472 break;
473 default:
474 return -EINVAL;
476 case IIO_CHAN_INFO_CALIBSCALE:
477 mutex_lock(&st->lock);
478 /* Compare against table - hard matching here */
479 for (i = 0; i < ARRAY_SIZE(dummy_scales); i++)
480 if (val == dummy_scales[i].val &&
481 val2 == dummy_scales[i].val2)
482 break;
483 if (i == ARRAY_SIZE(dummy_scales))
484 ret = -EINVAL;
485 else
486 st->accel_calibscale = &dummy_scales[i];
487 mutex_unlock(&st->lock);
488 return ret;
489 case IIO_CHAN_INFO_CALIBBIAS:
490 mutex_lock(&st->lock);
491 st->accel_calibbias = val;
492 mutex_unlock(&st->lock);
493 return 0;
494 case IIO_CHAN_INFO_ENABLE:
495 switch (chan->type) {
496 case IIO_STEPS:
497 mutex_lock(&st->lock);
498 st->steps_enabled = val;
499 mutex_unlock(&st->lock);
500 return 0;
501 default:
502 return -EINVAL;
504 case IIO_CHAN_INFO_CALIBHEIGHT:
505 switch (chan->type) {
506 case IIO_STEPS:
507 st->height = val;
508 return 0;
509 default:
510 return -EINVAL;
513 default:
514 return -EINVAL;
519 * Device type specific information.
521 static const struct iio_info iio_dummy_info = {
522 .read_raw = &iio_dummy_read_raw,
523 .write_raw = &iio_dummy_write_raw,
524 #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
525 .read_event_config = &iio_simple_dummy_read_event_config,
526 .write_event_config = &iio_simple_dummy_write_event_config,
527 .read_event_value = &iio_simple_dummy_read_event_value,
528 .write_event_value = &iio_simple_dummy_write_event_value,
529 #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
533 * iio_dummy_init_device() - device instance specific init
534 * @indio_dev: the iio device structure
536 * Most drivers have one of these to set up default values,
537 * reset the device to known state etc.
539 static int iio_dummy_init_device(struct iio_dev *indio_dev)
541 struct iio_dummy_state *st = iio_priv(indio_dev);
543 st->dac_val = 0;
544 st->single_ended_adc_val = 73;
545 st->differential_adc_val[0] = 33;
546 st->differential_adc_val[1] = -34;
547 st->accel_val = 34;
548 st->accel_calibbias = -7;
549 st->accel_calibscale = &dummy_scales[0];
550 st->steps = 47;
551 st->activity_running = 98;
552 st->activity_walking = 4;
554 return 0;
558 * iio_dummy_probe() - device instance probe
559 * @index: an id number for this instance.
561 * Arguments are bus type specific.
562 * I2C: iio_dummy_probe(struct i2c_client *client,
563 * const struct i2c_device_id *id)
564 * SPI: iio_dummy_probe(struct spi_device *spi)
566 static struct iio_sw_device *iio_dummy_probe(const char *name)
568 int ret;
569 struct iio_dev *indio_dev;
570 struct iio_dummy_state *st;
571 struct iio_sw_device *swd;
573 swd = kzalloc(sizeof(*swd), GFP_KERNEL);
574 if (!swd) {
575 ret = -ENOMEM;
576 goto error_kzalloc;
579 * Allocate an IIO device.
581 * This structure contains all generic state
582 * information about the device instance.
583 * It also has a region (accessed by iio_priv()
584 * for chip specific state information.
586 indio_dev = iio_device_alloc(sizeof(*st));
587 if (!indio_dev) {
588 ret = -ENOMEM;
589 goto error_ret;
592 st = iio_priv(indio_dev);
593 mutex_init(&st->lock);
595 iio_dummy_init_device(indio_dev);
597 * With hardware: Set the parent device.
598 * indio_dev->dev.parent = &spi->dev;
599 * indio_dev->dev.parent = &client->dev;
603 * Make the iio_dev struct available to remove function.
604 * Bus equivalents
605 * i2c_set_clientdata(client, indio_dev);
606 * spi_set_drvdata(spi, indio_dev);
608 swd->device = indio_dev;
611 * Set the device name.
613 * This is typically a part number and obtained from the module
614 * id table.
615 * e.g. for i2c and spi:
616 * indio_dev->name = id->name;
617 * indio_dev->name = spi_get_device_id(spi)->name;
619 indio_dev->name = kstrdup(name, GFP_KERNEL);
621 /* Provide description of available channels */
622 indio_dev->channels = iio_dummy_channels;
623 indio_dev->num_channels = ARRAY_SIZE(iio_dummy_channels);
626 * Provide device type specific interface functions and
627 * constant data.
629 indio_dev->info = &iio_dummy_info;
631 /* Specify that device provides sysfs type interfaces */
632 indio_dev->modes = INDIO_DIRECT_MODE;
634 ret = iio_simple_dummy_events_register(indio_dev);
635 if (ret < 0)
636 goto error_free_device;
638 ret = iio_simple_dummy_configure_buffer(indio_dev);
639 if (ret < 0)
640 goto error_unregister_events;
642 ret = iio_device_register(indio_dev);
643 if (ret < 0)
644 goto error_unconfigure_buffer;
646 iio_swd_group_init_type_name(swd, name, &iio_dummy_type);
648 return swd;
649 error_unconfigure_buffer:
650 iio_simple_dummy_unconfigure_buffer(indio_dev);
651 error_unregister_events:
652 iio_simple_dummy_events_unregister(indio_dev);
653 error_free_device:
654 iio_device_free(indio_dev);
655 error_ret:
656 kfree(swd);
657 error_kzalloc:
658 return ERR_PTR(ret);
662 * iio_dummy_remove() - device instance removal function
663 * @swd: pointer to software IIO device abstraction
665 * Parameters follow those of iio_dummy_probe for buses.
667 static int iio_dummy_remove(struct iio_sw_device *swd)
670 * Get a pointer to the device instance iio_dev structure
671 * from the bus subsystem. E.g.
672 * struct iio_dev *indio_dev = i2c_get_clientdata(client);
673 * struct iio_dev *indio_dev = spi_get_drvdata(spi);
675 struct iio_dev *indio_dev = swd->device;
677 /* Unregister the device */
678 iio_device_unregister(indio_dev);
680 /* Device specific code to power down etc */
682 /* Buffered capture related cleanup */
683 iio_simple_dummy_unconfigure_buffer(indio_dev);
685 iio_simple_dummy_events_unregister(indio_dev);
687 /* Free all structures */
688 kfree(indio_dev->name);
689 iio_device_free(indio_dev);
691 return 0;
694 * module_iio_sw_device_driver() - device driver registration
696 * Varies depending on bus type of the device. As there is no device
697 * here, call probe directly. For information on device registration
698 * i2c:
699 * Documentation/i2c/writing-clients
700 * spi:
701 * Documentation/spi/spi-summary
703 static const struct iio_sw_device_ops iio_dummy_device_ops = {
704 .probe = iio_dummy_probe,
705 .remove = iio_dummy_remove,
708 static struct iio_sw_device_type iio_dummy_device = {
709 .name = "dummy",
710 .owner = THIS_MODULE,
711 .ops = &iio_dummy_device_ops,
714 module_iio_sw_device_driver(iio_dummy_device);
716 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
717 MODULE_DESCRIPTION("IIO dummy driver");
718 MODULE_LICENSE("GPL v2");