]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/staging/iio/iio_simple_dummy.c
Merge tag 'fbdev-omap-3.15' of git://git.kernel.org/pub/scm/linux/kernel/git/tomba...
[mirror_ubuntu-zesty-kernel.git] / drivers / staging / iio / iio_simple_dummy.c
CommitLineData
3a84331d
JC
1/**
2 * Copyright (c) 2011 Jonathan Cameron
3 *
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.
7 *
8 * A reference industrial I/O driver to illustrate the functionality available.
9 *
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.
16 */
17#include <linux/kernel.h>
18#include <linux/slab.h>
19#include <linux/module.h>
20#include <linux/moduleparam.h>
21
06458e27
JC
22#include <linux/iio/iio.h>
23#include <linux/iio/sysfs.h>
24#include <linux/iio/events.h>
25#include <linux/iio/buffer.h>
e6477000 26#include "iio_simple_dummy.h"
3a84331d
JC
27
28/*
29 * A few elements needed to fake a bus for this driver
25c38aa3 30 * Note instances parameter controls how many of these
3a84331d
JC
31 * dummy devices are registered.
32 */
33static unsigned instances = 1;
34module_param(instances, int, 0);
35
36/* Pointer array used to fake bus elements */
37static struct iio_dev **iio_dummy_devs;
38
39/* Fake a name for the part number, usually obtained from the id table */
40static const char *iio_dummy_part_number = "iio_dummy_part_no";
41
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.
47 */
48struct iio_dummy_accel_calibscale {
49 int val;
50 int val2;
51 int regval; /* what would be written to hardware */
52};
53
54static const struct iio_dummy_accel_calibscale dummy_scales[] = {
55 { 0, 100, 0x8 }, /* 0.000100 */
56 { 0, 133, 0x7 }, /* 0.000133 */
569c4ac2 57 { 733, 13, 0x9 }, /* 733.000013 */
3a84331d
JC
58};
59
bda624b0
LPC
60#ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
61
62/*
63 * simple event - triggered when value rises above
64 * a threshold
65 */
66static const struct iio_event_spec iio_dummy_event = {
67 .type = IIO_EV_TYPE_THRESH,
68 .dir = IIO_EV_DIR_RISING,
69 .mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE),
70};
71
72#endif
73
3a84331d
JC
74/*
75 * iio_dummy_channels - Description of available channels
76 *
77 * This array of structures tells the IIO core about what the device
78 * actually provides for a given channel.
79 */
f4e4b955 80static const struct iio_chan_spec iio_dummy_channels[] = {
3a84331d
JC
81 /* indexed ADC channel in_voltage0_raw etc */
82 {
83 .type = IIO_VOLTAGE,
84 /* Channel has a numeric index of 0 */
85 .indexed = 1,
86 .channel = 0,
87 /* What other information is available? */
fa357a6a 88 .info_mask_separate =
41fd935b
LPC
89 /*
90 * in_voltage0_raw
91 * Raw (unscaled no bias removal etc) measurement
92 * from the device.
93 */
fa357a6a 94 BIT(IIO_CHAN_INFO_RAW) |
3a84331d
JC
95 /*
96 * in_voltage0_offset
97 * Offset for userspace to apply prior to scale
98 * when converting to standard units (microvolts)
99 */
fa357a6a 100 BIT(IIO_CHAN_INFO_OFFSET) |
3a84331d
JC
101 /*
102 * in_voltage0_scale
103 * Multipler for userspace to apply post offset
104 * when converting to standard units (microvolts)
105 */
fa357a6a 106 BIT(IIO_CHAN_INFO_SCALE),
6a63aa0a
JC
107 /*
108 * sampling_frequency
109 * The frequency in Hz at which the channels are sampled
110 */
111 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
9ad2e2e1
JC
112 /* The ordering of elements in the buffer via an enum */
113 .scan_index = voltage0,
114 .scan_type = { /* Description of storage in buffer */
115 .sign = 'u', /* unsigned */
116 .realbits = 13, /* 13 bits */
117 .storagebits = 16, /* 16 bits used for storage */
118 .shift = 0, /* zero shift */
119 },
e6477000 120#ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
bda624b0
LPC
121 .event_spec = &iio_dummy_event,
122 .num_event_specs = 1,
e6477000 123#endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
3a84331d
JC
124 },
125 /* Differential ADC channel in_voltage1-voltage2_raw etc*/
126 {
127 .type = IIO_VOLTAGE,
128 .differential = 1,
129 /*
130 * Indexing for differential channels uses channel
131 * for the positive part, channel2 for the negative.
132 */
133 .indexed = 1,
134 .channel = 1,
135 .channel2 = 2,
41fd935b
LPC
136 /*
137 * in_voltage1-voltage2_raw
138 * Raw (unscaled no bias removal etc) measurement
139 * from the device.
140 */
fa357a6a 141 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
3a84331d
JC
142 /*
143 * in_voltage-voltage_scale
144 * Shared version of scale - shared by differential
145 * input channels of type IIO_VOLTAGE.
146 */
fa357a6a 147 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
6a63aa0a
JC
148 /*
149 * sampling_frequency
150 * The frequency in Hz at which the channels are sampled
151 */
9ad2e2e1
JC
152 .scan_index = diffvoltage1m2,
153 .scan_type = { /* Description of storage in buffer */
154 .sign = 's', /* signed */
155 .realbits = 12, /* 12 bits */
156 .storagebits = 16, /* 16 bits used for storage */
157 .shift = 0, /* zero shift */
158 },
3a84331d
JC
159 },
160 /* Differential ADC channel in_voltage3-voltage4_raw etc*/
161 {
162 .type = IIO_VOLTAGE,
163 .differential = 1,
164 .indexed = 1,
165 .channel = 3,
166 .channel2 = 4,
fa357a6a
JC
167 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
168 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
6a63aa0a 169 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
9ad2e2e1
JC
170 .scan_index = diffvoltage3m4,
171 .scan_type = {
172 .sign = 's',
173 .realbits = 11,
174 .storagebits = 16,
175 .shift = 0,
176 },
3a84331d
JC
177 },
178 /*
179 * 'modified' (i.e. axis specified) acceleration channel
180 * in_accel_z_raw
181 */
182 {
183 .type = IIO_ACCEL,
184 .modified = 1,
185 /* Channel 2 is use for modifiers */
186 .channel2 = IIO_MOD_X,
fa357a6a 187 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
3a84331d 188 /*
ff3fc8eb 189 * Internal bias and gain correction values. Applied
3a84331d
JC
190 * by the hardware or driver prior to userspace
191 * seeing the readings. Typically part of hardware
192 * calibration.
193 */
ff3fc8eb 194 BIT(IIO_CHAN_INFO_CALIBSCALE) |
fa357a6a 195 BIT(IIO_CHAN_INFO_CALIBBIAS),
6a63aa0a 196 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
9ad2e2e1
JC
197 .scan_index = accelx,
198 .scan_type = { /* Description of storage in buffer */
199 .sign = 's', /* signed */
25c38aa3 200 .realbits = 16, /* 16 bits */
9ad2e2e1
JC
201 .storagebits = 16, /* 16 bits used for storage */
202 .shift = 0, /* zero shift */
203 },
204 },
205 /*
206 * Convenience macro for timestamps. 4 is the index in
207 * the buffer.
208 */
209 IIO_CHAN_SOFT_TIMESTAMP(4),
210 /* DAC channel out_voltage0_raw */
211 {
212 .type = IIO_VOLTAGE,
fa357a6a 213 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
9ad2e2e1
JC
214 .output = 1,
215 .indexed = 1,
216 .channel = 0,
3a84331d
JC
217 },
218};
219
220/**
221 * iio_dummy_read_raw() - data read function.
222 * @indio_dev: the struct iio_dev associated with this device instance
223 * @chan: the channel whose data is to be read
224 * @val: first element of returned value (typically INT)
225 * @val2: second element of returned value (typically MICRO)
fa357a6a
JC
226 * @mask: what we actually want to read as per the info_mask_*
227 * in iio_chan_spec.
3a84331d
JC
228 */
229static int iio_dummy_read_raw(struct iio_dev *indio_dev,
230 struct iio_chan_spec const *chan,
231 int *val,
232 int *val2,
233 long mask)
234{
235 struct iio_dummy_state *st = iio_priv(indio_dev);
236 int ret = -EINVAL;
237
238 mutex_lock(&st->lock);
239 switch (mask) {
41fd935b 240 case IIO_CHAN_INFO_RAW: /* magic value - channel value read */
3a84331d
JC
241 switch (chan->type) {
242 case IIO_VOLTAGE:
243 if (chan->output) {
244 /* Set integer part to cached value */
245 *val = st->dac_val;
246 ret = IIO_VAL_INT;
247 } else if (chan->differential) {
248 if (chan->channel == 1)
249 *val = st->differential_adc_val[0];
250 else
251 *val = st->differential_adc_val[1];
252 ret = IIO_VAL_INT;
253 } else {
254 *val = st->single_ended_adc_val;
255 ret = IIO_VAL_INT;
256 }
257 break;
258 case IIO_ACCEL:
259 *val = st->accel_val;
260 ret = IIO_VAL_INT;
261 break;
262 default:
263 break;
264 }
265 break;
c8a9f805 266 case IIO_CHAN_INFO_OFFSET:
3a84331d
JC
267 /* only single ended adc -> 7 */
268 *val = 7;
269 ret = IIO_VAL_INT;
270 break;
c8a9f805
JC
271 case IIO_CHAN_INFO_SCALE:
272 switch (chan->differential) {
273 case 0:
274 /* only single ended adc -> 0.001333 */
275 *val = 0;
276 *val2 = 1333;
277 ret = IIO_VAL_INT_PLUS_MICRO;
278 break;
279 case 1:
280 /* all differential adc channels -> 0.000001344 */
281 *val = 0;
282 *val2 = 1344;
283 ret = IIO_VAL_INT_PLUS_NANO;
284 }
3a84331d 285 break;
c8a9f805 286 case IIO_CHAN_INFO_CALIBBIAS:
3a84331d
JC
287 /* only the acceleration axis - read from cache */
288 *val = st->accel_calibbias;
289 ret = IIO_VAL_INT;
290 break;
c8a9f805 291 case IIO_CHAN_INFO_CALIBSCALE:
3a84331d
JC
292 *val = st->accel_calibscale->val;
293 *val2 = st->accel_calibscale->val2;
294 ret = IIO_VAL_INT_PLUS_MICRO;
295 break;
6a63aa0a
JC
296 case IIO_CHAN_INFO_SAMP_FREQ:
297 *val = 3;
298 *val2 = 33;
299 ret = IIO_VAL_INT_PLUS_NANO;
300 break;
3a84331d
JC
301 default:
302 break;
303 }
304 mutex_unlock(&st->lock);
305 return ret;
306}
307
308/**
309 * iio_dummy_write_raw() - data write function.
310 * @indio_dev: the struct iio_dev associated with this device instance
569c4ac2 311 * @chan: the channel whose data is to be written
25c38aa3
PM
312 * @val: first element of value to set (typically INT)
313 * @val2: second element of value to set (typically MICRO)
fa357a6a
JC
314 * @mask: what we actually want to write as per the info_mask_*
315 * in iio_chan_spec.
3a84331d
JC
316 *
317 * Note that all raw writes are assumed IIO_VAL_INT and info mask elements
318 * are assumed to be IIO_INT_PLUS_MICRO unless the callback write_raw_get_fmt
319 * in struct iio_info is provided by the driver.
320 */
321static int iio_dummy_write_raw(struct iio_dev *indio_dev,
322 struct iio_chan_spec const *chan,
323 int val,
324 int val2,
325 long mask)
326{
327 int i;
328 int ret = 0;
329 struct iio_dummy_state *st = iio_priv(indio_dev);
330
331 switch (mask) {
41fd935b 332 case IIO_CHAN_INFO_RAW:
3a84331d
JC
333 if (chan->output == 0)
334 return -EINVAL;
335
336 /* Locking not required as writing single value */
337 mutex_lock(&st->lock);
338 st->dac_val = val;
339 mutex_unlock(&st->lock);
340 return 0;
ff3fc8eb 341 case IIO_CHAN_INFO_CALIBSCALE:
3a84331d
JC
342 mutex_lock(&st->lock);
343 /* Compare against table - hard matching here */
344 for (i = 0; i < ARRAY_SIZE(dummy_scales); i++)
345 if (val == dummy_scales[i].val &&
346 val2 == dummy_scales[i].val2)
347 break;
348 if (i == ARRAY_SIZE(dummy_scales))
349 ret = -EINVAL;
350 else
351 st->accel_calibscale = &dummy_scales[i];
352 mutex_unlock(&st->lock);
353 return ret;
ff3fc8eb
JF
354 case IIO_CHAN_INFO_CALIBBIAS:
355 mutex_lock(&st->lock);
356 st->accel_calibbias = val;
357 mutex_unlock(&st->lock);
358 return 0;
359
3a84331d
JC
360 default:
361 return -EINVAL;
362 }
363}
364
365/*
366 * Device type specific information.
367 */
368static const struct iio_info iio_dummy_info = {
369 .driver_module = THIS_MODULE,
370 .read_raw = &iio_dummy_read_raw,
371 .write_raw = &iio_dummy_write_raw,
e6477000 372#ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
cb955852
LPC
373 .read_event_config = &iio_simple_dummy_read_event_config,
374 .write_event_config = &iio_simple_dummy_write_event_config,
375 .read_event_value = &iio_simple_dummy_read_event_value,
376 .write_event_value = &iio_simple_dummy_write_event_value,
e6477000 377#endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
3a84331d
JC
378};
379
380/**
381 * iio_dummy_init_device() - device instance specific init
382 * @indio_dev: the iio device structure
383 *
384 * Most drivers have one of these to set up default values,
385 * reset the device to known state etc.
386 */
387static int iio_dummy_init_device(struct iio_dev *indio_dev)
388{
389 struct iio_dummy_state *st = iio_priv(indio_dev);
390
391 st->dac_val = 0;
392 st->single_ended_adc_val = 73;
393 st->differential_adc_val[0] = 33;
394 st->differential_adc_val[1] = -34;
395 st->accel_val = 34;
396 st->accel_calibbias = -7;
397 st->accel_calibscale = &dummy_scales[0];
398
399 return 0;
400}
401
402/**
403 * iio_dummy_probe() - device instance probe
404 * @index: an id number for this instance.
405 *
406 * Arguments are bus type specific.
407 * I2C: iio_dummy_probe(struct i2c_client *client,
408 * const struct i2c_device_id *id)
409 * SPI: iio_dummy_probe(struct spi_device *spi)
410 */
4ae1c61f 411static int iio_dummy_probe(int index)
3a84331d
JC
412{
413 int ret;
414 struct iio_dev *indio_dev;
415 struct iio_dummy_state *st;
416
417 /*
418 * Allocate an IIO device.
419 *
420 * This structure contains all generic state
421 * information about the device instance.
422 * It also has a region (accessed by iio_priv()
423 * for chip specific state information.
424 */
7cbb7537 425 indio_dev = iio_device_alloc(sizeof(*st));
3a84331d
JC
426 if (indio_dev == NULL) {
427 ret = -ENOMEM;
428 goto error_ret;
429 }
430
431 st = iio_priv(indio_dev);
432 mutex_init(&st->lock);
433
434 iio_dummy_init_device(indio_dev);
435 /*
436 * With hardware: Set the parent device.
437 * indio_dev->dev.parent = &spi->dev;
438 * indio_dev->dev.parent = &client->dev;
439 */
440
441 /*
442 * Make the iio_dev struct available to remove function.
443 * Bus equivalents
444 * i2c_set_clientdata(client, indio_dev);
445 * spi_set_drvdata(spi, indio_dev);
446 */
447 iio_dummy_devs[index] = indio_dev;
448
449
450 /*
451 * Set the device name.
452 *
453 * This is typically a part number and obtained from the module
454 * id table.
455 * e.g. for i2c and spi:
456 * indio_dev->name = id->name;
457 * indio_dev->name = spi_get_device_id(spi)->name;
458 */
459 indio_dev->name = iio_dummy_part_number;
460
461 /* Provide description of available channels */
462 indio_dev->channels = iio_dummy_channels;
463 indio_dev->num_channels = ARRAY_SIZE(iio_dummy_channels);
464
465 /*
466 * Provide device type specific interface functions and
467 * constant data.
468 */
469 indio_dev->info = &iio_dummy_info;
470
471 /* Specify that device provides sysfs type interfaces */
472 indio_dev->modes = INDIO_DIRECT_MODE;
473
e6477000 474 ret = iio_simple_dummy_events_register(indio_dev);
3a84331d
JC
475 if (ret < 0)
476 goto error_free_device;
9ad2e2e1 477
9ad2e2e1 478 /*
3fff2274
LPC
479 * Configure buffered capture support and register the channels with the
480 * buffer, but avoid the output channel being registered by reducing the
481 * number of channels by 1.
9ad2e2e1 482 */
d7c0886e
EA
483 ret = iio_simple_dummy_configure_buffer(indio_dev,
484 iio_dummy_channels, 5);
9ad2e2e1 485 if (ret < 0)
3fff2274 486 goto error_unregister_events;
9ad2e2e1
JC
487
488 ret = iio_device_register(indio_dev);
489 if (ret < 0)
3fff2274 490 goto error_unconfigure_buffer;
9ad2e2e1 491
3a84331d 492 return 0;
9ad2e2e1
JC
493error_unconfigure_buffer:
494 iio_simple_dummy_unconfigure_buffer(indio_dev);
e6477000
JC
495error_unregister_events:
496 iio_simple_dummy_events_unregister(indio_dev);
3a84331d 497error_free_device:
7cbb7537 498 iio_device_free(indio_dev);
3a84331d
JC
499error_ret:
500 return ret;
501}
502
503/**
504 * iio_dummy_remove() - device instance removal function
505 * @index: device index.
506 *
507 * Parameters follow those of iio_dummy_probe for buses.
508 */
509static int iio_dummy_remove(int index)
510{
e6477000 511 int ret;
3a84331d
JC
512 /*
513 * Get a pointer to the device instance iio_dev structure
514 * from the bus subsystem. E.g.
515 * struct iio_dev *indio_dev = i2c_get_clientdata(client);
516 * struct iio_dev *indio_dev = spi_get_drvdata(spi);
517 */
518 struct iio_dev *indio_dev = iio_dummy_devs[index];
519
e6477000 520
3a84331d
JC
521 /* Unregister the device */
522 iio_device_unregister(indio_dev);
523
524 /* Device specific code to power down etc */
525
9ad2e2e1 526 /* Buffered capture related cleanup */
9ad2e2e1 527 iio_simple_dummy_unconfigure_buffer(indio_dev);
e6477000
JC
528
529 ret = iio_simple_dummy_events_unregister(indio_dev);
530 if (ret)
531 goto error_ret;
532
3a84331d 533 /* Free all structures */
7cbb7537 534 iio_device_free(indio_dev);
3a84331d 535
e6477000
JC
536error_ret:
537 return ret;
3a84331d
JC
538}
539
540/**
541 * iio_dummy_init() - device driver registration
542 *
543 * Varies depending on bus type of the device. As there is no device
544 * here, call probe directly. For information on device registration
545 * i2c:
546 * Documentation/i2c/writing-clients
547 * spi:
548 * Documentation/spi/spi-summary
549 */
550static __init int iio_dummy_init(void)
551{
552 int i, ret;
553 if (instances > 10) {
554 instances = 1;
555 return -EINVAL;
556 }
3fff2274 557
3a84331d 558 /* Fake a bus */
d83fb184
TM
559 iio_dummy_devs = kcalloc(instances, sizeof(*iio_dummy_devs),
560 GFP_KERNEL);
3a84331d
JC
561 /* Here we have no actual device so call probe */
562 for (i = 0; i < instances; i++) {
563 ret = iio_dummy_probe(i);
564 if (ret < 0)
565 return ret;
566 }
567 return 0;
568}
569module_init(iio_dummy_init);
570
571/**
572 * iio_dummy_exit() - device driver removal
573 *
574 * Varies depending on bus type of the device.
575 * As there is no device here, call remove directly.
576 */
577static __exit void iio_dummy_exit(void)
578{
579 int i;
580 for (i = 0; i < instances; i++)
581 iio_dummy_remove(i);
582 kfree(iio_dummy_devs);
583}
584module_exit(iio_dummy_exit);
585
0f8c9620 586MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
3a84331d
JC
587MODULE_DESCRIPTION("IIO dummy driver");
588MODULE_LICENSE("GPL v2");