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