blob: 43fe4ba7d0dcdac2f5d30666139eb2270001080f [file] [log] [blame]
Jonathan Cameron3a843312011-10-14 16:34:13 +01001/**
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>
Jonathan Cameron3a843312011-10-14 16:34:13 +010020
Jonathan Cameron06458e22012-04-25 15:54:58 +010021#include <linux/iio/iio.h>
22#include <linux/iio/sysfs.h>
23#include <linux/iio/events.h>
24#include <linux/iio/buffer.h>
Jonathan Camerone6477002011-10-14 16:34:14 +010025#include "iio_simple_dummy.h"
Jonathan Cameron3a843312011-10-14 16:34:13 +010026
27/*
28 * A few elements needed to fake a bus for this driver
Peter Meerwald25c38aa2012-06-15 19:27:10 +020029 * Note instances parameter controls how many of these
Jonathan Cameron3a843312011-10-14 16:34:13 +010030 * dummy devices are registered.
31 */
32static unsigned instances = 1;
Vladimirs Ambrosovs4dcaa5f2015-05-30 11:20:17 +030033module_param(instances, uint, 0);
Jonathan Cameron3a843312011-10-14 16:34:13 +010034
35/* Pointer array used to fake bus elements */
36static struct iio_dev **iio_dummy_devs;
37
38/* Fake a name for the part number, usually obtained from the id table */
39static const char *iio_dummy_part_number = "iio_dummy_part_no";
40
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.
46 */
47struct iio_dummy_accel_calibscale {
48 int val;
49 int val2;
50 int regval; /* what would be written to hardware */
51};
52
53static const struct iio_dummy_accel_calibscale dummy_scales[] = {
54 { 0, 100, 0x8 }, /* 0.000100 */
55 { 0, 133, 0x7 }, /* 0.000133 */
Alexander Stein569c4ac2012-12-19 17:56:00 +000056 { 733, 13, 0x9 }, /* 733.000013 */
Jonathan Cameron3a843312011-10-14 16:34:13 +010057};
58
Lars-Peter Clausenbda624b2013-10-07 15:11:00 +010059#ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
60
61/*
62 * simple event - triggered when value rises above
63 * a threshold
64 */
65static const struct iio_event_spec iio_dummy_event = {
66 .type = IIO_EV_TYPE_THRESH,
67 .dir = IIO_EV_DIR_RISING,
68 .mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE),
69};
70
Daniel Baluta3e34e652014-11-10 14:45:34 +020071/*
72 * simple step detect event - triggered when a step is detected
73 */
74static const struct iio_event_spec step_detect_event = {
Irina Tirdea17a2cbc2015-01-11 21:10:12 +020075 .type = IIO_EV_TYPE_CHANGE,
Daniel Baluta3e34e652014-11-10 14:45:34 +020076 .dir = IIO_EV_DIR_NONE,
77 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
78};
79
80/*
81 * simple transition event - triggered when the reported running confidence
82 * value rises above a threshold value
83 */
84static const struct iio_event_spec iio_running_event = {
85 .type = IIO_EV_TYPE_THRESH,
86 .dir = IIO_EV_DIR_RISING,
87 .mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE),
88};
89
90/*
91 * simple transition event - triggered when the reported walking confidence
92 * value falls under a threshold value
93 */
94static const struct iio_event_spec iio_walking_event = {
95 .type = IIO_EV_TYPE_THRESH,
96 .dir = IIO_EV_DIR_FALLING,
97 .mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE),
98};
Lars-Peter Clausenbda624b2013-10-07 15:11:00 +010099#endif
100
Jonathan Cameron3a843312011-10-14 16:34:13 +0100101/*
102 * iio_dummy_channels - Description of available channels
103 *
104 * This array of structures tells the IIO core about what the device
105 * actually provides for a given channel.
106 */
Lars-Peter Clausenf4e4b952012-08-09 08:51:00 +0100107static const struct iio_chan_spec iio_dummy_channels[] = {
Jonathan Cameron3a843312011-10-14 16:34:13 +0100108 /* indexed ADC channel in_voltage0_raw etc */
109 {
110 .type = IIO_VOLTAGE,
111 /* Channel has a numeric index of 0 */
112 .indexed = 1,
113 .channel = 0,
114 /* What other information is available? */
Jonathan Cameronfa357a62013-02-19 21:12:21 +0000115 .info_mask_separate =
Jonathan Cameron3a843312011-10-14 16:34:13 +0100116 /*
Lars-Peter Clausen41fd9352012-04-15 17:41:27 +0100117 * in_voltage0_raw
118 * Raw (unscaled no bias removal etc) measurement
119 * from the device.
120 */
Jonathan Cameronfa357a62013-02-19 21:12:21 +0000121 BIT(IIO_CHAN_INFO_RAW) |
Lars-Peter Clausen41fd9352012-04-15 17:41:27 +0100122 /*
Jonathan Cameron3a843312011-10-14 16:34:13 +0100123 * in_voltage0_offset
124 * Offset for userspace to apply prior to scale
125 * when converting to standard units (microvolts)
126 */
Jonathan Cameronfa357a62013-02-19 21:12:21 +0000127 BIT(IIO_CHAN_INFO_OFFSET) |
Jonathan Cameron3a843312011-10-14 16:34:13 +0100128 /*
129 * in_voltage0_scale
130 * Multipler for userspace to apply post offset
131 * when converting to standard units (microvolts)
132 */
Jonathan Cameronfa357a62013-02-19 21:12:21 +0000133 BIT(IIO_CHAN_INFO_SCALE),
Jonathan Cameron6a63aa02013-09-08 14:57:00 +0100134 /*
135 * sampling_frequency
136 * The frequency in Hz at which the channels are sampled
137 */
138 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
Jonathan Cameron9ad2e2e2011-10-14 16:34:15 +0100139 /* The ordering of elements in the buffer via an enum */
Alison Schofieldf8087ab2015-10-26 13:48:23 -0700140 .scan_index = DUMMY_INDEX_VOLTAGE_0,
Jonathan Cameron9ad2e2e2011-10-14 16:34:15 +0100141 .scan_type = { /* Description of storage in buffer */
142 .sign = 'u', /* unsigned */
143 .realbits = 13, /* 13 bits */
144 .storagebits = 16, /* 16 bits used for storage */
145 .shift = 0, /* zero shift */
146 },
Jonathan Camerone6477002011-10-14 16:34:14 +0100147#ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
Lars-Peter Clausenbda624b2013-10-07 15:11:00 +0100148 .event_spec = &iio_dummy_event,
149 .num_event_specs = 1,
Jonathan Camerone6477002011-10-14 16:34:14 +0100150#endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
Jonathan Cameron3a843312011-10-14 16:34:13 +0100151 },
152 /* Differential ADC channel in_voltage1-voltage2_raw etc*/
153 {
154 .type = IIO_VOLTAGE,
155 .differential = 1,
156 /*
157 * Indexing for differential channels uses channel
158 * for the positive part, channel2 for the negative.
159 */
160 .indexed = 1,
161 .channel = 1,
162 .channel2 = 2,
Jonathan Cameron3a843312011-10-14 16:34:13 +0100163 /*
Lars-Peter Clausen41fd9352012-04-15 17:41:27 +0100164 * in_voltage1-voltage2_raw
165 * Raw (unscaled no bias removal etc) measurement
166 * from the device.
167 */
Jonathan Cameronfa357a62013-02-19 21:12:21 +0000168 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
Lars-Peter Clausen41fd9352012-04-15 17:41:27 +0100169 /*
Jonathan Cameron3a843312011-10-14 16:34:13 +0100170 * in_voltage-voltage_scale
171 * Shared version of scale - shared by differential
172 * input channels of type IIO_VOLTAGE.
173 */
Jonathan Cameronfa357a62013-02-19 21:12:21 +0000174 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
Jonathan Cameron6a63aa02013-09-08 14:57:00 +0100175 /*
176 * sampling_frequency
177 * The frequency in Hz at which the channels are sampled
178 */
Alison Schofieldf8087ab2015-10-26 13:48:23 -0700179 .scan_index = DUMMY_INDEX_DIFFVOLTAGE_1M2,
Jonathan Cameron9ad2e2e2011-10-14 16:34:15 +0100180 .scan_type = { /* Description of storage in buffer */
181 .sign = 's', /* signed */
182 .realbits = 12, /* 12 bits */
183 .storagebits = 16, /* 16 bits used for storage */
184 .shift = 0, /* zero shift */
185 },
Jonathan Cameron3a843312011-10-14 16:34:13 +0100186 },
187 /* Differential ADC channel in_voltage3-voltage4_raw etc*/
188 {
189 .type = IIO_VOLTAGE,
190 .differential = 1,
191 .indexed = 1,
192 .channel = 3,
193 .channel2 = 4,
Jonathan Cameronfa357a62013-02-19 21:12:21 +0000194 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
195 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
Jonathan Cameron6a63aa02013-09-08 14:57:00 +0100196 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
Alison Schofieldf8087ab2015-10-26 13:48:23 -0700197 .scan_index = DUMMY_INDEX_DIFFVOLTAGE_3M4,
Jonathan Cameron9ad2e2e2011-10-14 16:34:15 +0100198 .scan_type = {
199 .sign = 's',
200 .realbits = 11,
201 .storagebits = 16,
202 .shift = 0,
203 },
Jonathan Cameron3a843312011-10-14 16:34:13 +0100204 },
205 /*
206 * 'modified' (i.e. axis specified) acceleration channel
207 * in_accel_z_raw
208 */
209 {
210 .type = IIO_ACCEL,
211 .modified = 1,
212 /* Channel 2 is use for modifiers */
213 .channel2 = IIO_MOD_X,
Jonathan Cameronfa357a62013-02-19 21:12:21 +0000214 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
Jonathan Cameron3a843312011-10-14 16:34:13 +0100215 /*
Jin Fengff3fc8e2013-04-05 05:51:00 +0100216 * Internal bias and gain correction values. Applied
Jonathan Cameron3a843312011-10-14 16:34:13 +0100217 * by the hardware or driver prior to userspace
218 * seeing the readings. Typically part of hardware
219 * calibration.
220 */
Jin Fengff3fc8e2013-04-05 05:51:00 +0100221 BIT(IIO_CHAN_INFO_CALIBSCALE) |
Jonathan Cameronfa357a62013-02-19 21:12:21 +0000222 BIT(IIO_CHAN_INFO_CALIBBIAS),
Jonathan Cameron6a63aa02013-09-08 14:57:00 +0100223 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
Alison Schofieldf8087ab2015-10-26 13:48:23 -0700224 .scan_index = DUMMY_INDEX_ACCELX,
Jonathan Cameron9ad2e2e2011-10-14 16:34:15 +0100225 .scan_type = { /* Description of storage in buffer */
226 .sign = 's', /* signed */
Peter Meerwald25c38aa2012-06-15 19:27:10 +0200227 .realbits = 16, /* 16 bits */
Jonathan Cameron9ad2e2e2011-10-14 16:34:15 +0100228 .storagebits = 16, /* 16 bits used for storage */
229 .shift = 0, /* zero shift */
230 },
231 },
232 /*
233 * Convenience macro for timestamps. 4 is the index in
234 * the buffer.
235 */
236 IIO_CHAN_SOFT_TIMESTAMP(4),
237 /* DAC channel out_voltage0_raw */
238 {
239 .type = IIO_VOLTAGE,
Jonathan Cameronfa357a62013-02-19 21:12:21 +0000240 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
Lars-Peter Clausen4ae03012014-11-26 18:55:11 +0100241 .scan_index = -1, /* No buffer support */
Jonathan Cameron9ad2e2e2011-10-14 16:34:15 +0100242 .output = 1,
243 .indexed = 1,
244 .channel = 0,
Jonathan Cameron3a843312011-10-14 16:34:13 +0100245 },
Daniel Baluta3e34e652014-11-10 14:45:34 +0200246 {
247 .type = IIO_STEPS,
248 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_ENABLE) |
249 BIT(IIO_CHAN_INFO_CALIBHEIGHT),
250 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
Lars-Peter Clausen4ae03012014-11-26 18:55:11 +0100251 .scan_index = -1, /* No buffer support */
Daniel Baluta3e34e652014-11-10 14:45:34 +0200252#ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
253 .event_spec = &step_detect_event,
254 .num_event_specs = 1,
255#endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
256 },
257 {
258 .type = IIO_ACTIVITY,
259 .modified = 1,
260 .channel2 = IIO_MOD_RUNNING,
261 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
Lars-Peter Clausen4ae03012014-11-26 18:55:11 +0100262 .scan_index = -1, /* No buffer support */
Daniel Baluta3e34e652014-11-10 14:45:34 +0200263#ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
264 .event_spec = &iio_running_event,
265 .num_event_specs = 1,
266#endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
267 },
268 {
269 .type = IIO_ACTIVITY,
270 .modified = 1,
271 .channel2 = IIO_MOD_WALKING,
272 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
Lars-Peter Clausen4ae03012014-11-26 18:55:11 +0100273 .scan_index = -1, /* No buffer support */
Daniel Baluta3e34e652014-11-10 14:45:34 +0200274#ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
275 .event_spec = &iio_walking_event,
276 .num_event_specs = 1,
277#endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
278 },
Jonathan Cameron3a843312011-10-14 16:34:13 +0100279};
280
281/**
282 * iio_dummy_read_raw() - data read function.
283 * @indio_dev: the struct iio_dev associated with this device instance
284 * @chan: the channel whose data is to be read
285 * @val: first element of returned value (typically INT)
286 * @val2: second element of returned value (typically MICRO)
Jonathan Cameronfa357a62013-02-19 21:12:21 +0000287 * @mask: what we actually want to read as per the info_mask_*
288 * in iio_chan_spec.
Jonathan Cameron3a843312011-10-14 16:34:13 +0100289 */
290static int iio_dummy_read_raw(struct iio_dev *indio_dev,
291 struct iio_chan_spec const *chan,
292 int *val,
293 int *val2,
294 long mask)
295{
296 struct iio_dummy_state *st = iio_priv(indio_dev);
297 int ret = -EINVAL;
298
299 mutex_lock(&st->lock);
300 switch (mask) {
Lars-Peter Clausen41fd9352012-04-15 17:41:27 +0100301 case IIO_CHAN_INFO_RAW: /* magic value - channel value read */
Jonathan Cameron3a843312011-10-14 16:34:13 +0100302 switch (chan->type) {
303 case IIO_VOLTAGE:
304 if (chan->output) {
305 /* Set integer part to cached value */
306 *val = st->dac_val;
307 ret = IIO_VAL_INT;
308 } else if (chan->differential) {
309 if (chan->channel == 1)
310 *val = st->differential_adc_val[0];
311 else
312 *val = st->differential_adc_val[1];
313 ret = IIO_VAL_INT;
314 } else {
315 *val = st->single_ended_adc_val;
316 ret = IIO_VAL_INT;
317 }
318 break;
319 case IIO_ACCEL:
320 *val = st->accel_val;
321 ret = IIO_VAL_INT;
322 break;
323 default:
324 break;
325 }
326 break;
Daniel Baluta3e34e652014-11-10 14:45:34 +0200327 case IIO_CHAN_INFO_PROCESSED:
328 switch (chan->type) {
329 case IIO_STEPS:
330 *val = st->steps;
331 ret = IIO_VAL_INT;
332 break;
333 case IIO_ACTIVITY:
334 switch (chan->channel2) {
335 case IIO_MOD_RUNNING:
336 *val = st->activity_running;
337 ret = IIO_VAL_INT;
338 break;
339 case IIO_MOD_WALKING:
340 *val = st->activity_walking;
341 ret = IIO_VAL_INT;
342 break;
343 default:
344 break;
345 }
346 break;
347 default:
348 break;
349 }
350 break;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100351 case IIO_CHAN_INFO_OFFSET:
Jonathan Cameron3a843312011-10-14 16:34:13 +0100352 /* only single ended adc -> 7 */
353 *val = 7;
354 ret = IIO_VAL_INT;
355 break;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100356 case IIO_CHAN_INFO_SCALE:
Daniel Baluta3e34e652014-11-10 14:45:34 +0200357 switch (chan->type) {
358 case IIO_VOLTAGE:
359 switch (chan->differential) {
360 case 0:
361 /* only single ended adc -> 0.001333 */
362 *val = 0;
363 *val2 = 1333;
364 ret = IIO_VAL_INT_PLUS_MICRO;
365 break;
366 case 1:
Alison Schofield4b60c882015-10-26 13:50:29 -0700367 /* all differential adc -> 0.000001344 */
Daniel Baluta3e34e652014-11-10 14:45:34 +0200368 *val = 0;
369 *val2 = 1344;
370 ret = IIO_VAL_INT_PLUS_NANO;
371 }
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100372 break;
Daniel Baluta3e34e652014-11-10 14:45:34 +0200373 default:
374 break;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100375 }
Jonathan Cameron3a843312011-10-14 16:34:13 +0100376 break;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100377 case IIO_CHAN_INFO_CALIBBIAS:
Jonathan Cameron3a843312011-10-14 16:34:13 +0100378 /* only the acceleration axis - read from cache */
379 *val = st->accel_calibbias;
380 ret = IIO_VAL_INT;
381 break;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100382 case IIO_CHAN_INFO_CALIBSCALE:
Jonathan Cameron3a843312011-10-14 16:34:13 +0100383 *val = st->accel_calibscale->val;
384 *val2 = st->accel_calibscale->val2;
385 ret = IIO_VAL_INT_PLUS_MICRO;
386 break;
Jonathan Cameron6a63aa02013-09-08 14:57:00 +0100387 case IIO_CHAN_INFO_SAMP_FREQ:
388 *val = 3;
389 *val2 = 33;
390 ret = IIO_VAL_INT_PLUS_NANO;
391 break;
Daniel Baluta3e34e652014-11-10 14:45:34 +0200392 case IIO_CHAN_INFO_ENABLE:
393 switch (chan->type) {
394 case IIO_STEPS:
395 *val = st->steps_enabled;
396 ret = IIO_VAL_INT;
397 break;
398 default:
399 break;
400 }
401 break;
402 case IIO_CHAN_INFO_CALIBHEIGHT:
403 switch (chan->type) {
404 case IIO_STEPS:
405 *val = st->height;
406 ret = IIO_VAL_INT;
407 break;
408 default:
409 break;
410 }
411 break;
412
Jonathan Cameron3a843312011-10-14 16:34:13 +0100413 default:
414 break;
415 }
416 mutex_unlock(&st->lock);
417 return ret;
418}
419
420/**
421 * iio_dummy_write_raw() - data write function.
422 * @indio_dev: the struct iio_dev associated with this device instance
Alexander Stein569c4ac2012-12-19 17:56:00 +0000423 * @chan: the channel whose data is to be written
Peter Meerwald25c38aa2012-06-15 19:27:10 +0200424 * @val: first element of value to set (typically INT)
425 * @val2: second element of value to set (typically MICRO)
Jonathan Cameronfa357a62013-02-19 21:12:21 +0000426 * @mask: what we actually want to write as per the info_mask_*
427 * in iio_chan_spec.
Jonathan Cameron3a843312011-10-14 16:34:13 +0100428 *
429 * Note that all raw writes are assumed IIO_VAL_INT and info mask elements
430 * are assumed to be IIO_INT_PLUS_MICRO unless the callback write_raw_get_fmt
431 * in struct iio_info is provided by the driver.
432 */
433static int iio_dummy_write_raw(struct iio_dev *indio_dev,
434 struct iio_chan_spec const *chan,
435 int val,
436 int val2,
437 long mask)
438{
439 int i;
440 int ret = 0;
441 struct iio_dummy_state *st = iio_priv(indio_dev);
442
443 switch (mask) {
Lars-Peter Clausen41fd9352012-04-15 17:41:27 +0100444 case IIO_CHAN_INFO_RAW:
Daniel Baluta3e34e652014-11-10 14:45:34 +0200445 switch (chan->type) {
446 case IIO_VOLTAGE:
447 if (chan->output == 0)
448 return -EINVAL;
Jonathan Cameron3a843312011-10-14 16:34:13 +0100449
Daniel Baluta3e34e652014-11-10 14:45:34 +0200450 /* Locking not required as writing single value */
451 mutex_lock(&st->lock);
452 st->dac_val = val;
453 mutex_unlock(&st->lock);
454 return 0;
455 default:
456 return -EINVAL;
457 }
458 case IIO_CHAN_INFO_PROCESSED:
459 switch (chan->type) {
460 case IIO_STEPS:
461 mutex_lock(&st->lock);
462 st->steps = val;
463 mutex_unlock(&st->lock);
464 return 0;
465 case IIO_ACTIVITY:
466 if (val < 0)
467 val = 0;
468 if (val > 100)
469 val = 100;
470 switch (chan->channel2) {
471 case IIO_MOD_RUNNING:
472 st->activity_running = val;
473 return 0;
474 case IIO_MOD_WALKING:
475 st->activity_walking = val;
476 return 0;
477 default:
478 return -EINVAL;
479 }
480 break;
481 default:
482 return -EINVAL;
483 }
Jin Fengff3fc8e2013-04-05 05:51:00 +0100484 case IIO_CHAN_INFO_CALIBSCALE:
Jonathan Cameron3a843312011-10-14 16:34:13 +0100485 mutex_lock(&st->lock);
486 /* Compare against table - hard matching here */
487 for (i = 0; i < ARRAY_SIZE(dummy_scales); i++)
488 if (val == dummy_scales[i].val &&
489 val2 == dummy_scales[i].val2)
490 break;
491 if (i == ARRAY_SIZE(dummy_scales))
492 ret = -EINVAL;
493 else
494 st->accel_calibscale = &dummy_scales[i];
495 mutex_unlock(&st->lock);
496 return ret;
Jin Fengff3fc8e2013-04-05 05:51:00 +0100497 case IIO_CHAN_INFO_CALIBBIAS:
498 mutex_lock(&st->lock);
499 st->accel_calibbias = val;
500 mutex_unlock(&st->lock);
501 return 0;
Daniel Baluta3e34e652014-11-10 14:45:34 +0200502 case IIO_CHAN_INFO_ENABLE:
503 switch (chan->type) {
504 case IIO_STEPS:
505 mutex_lock(&st->lock);
506 st->steps_enabled = val;
507 mutex_unlock(&st->lock);
508 return 0;
509 default:
510 return -EINVAL;
511 }
512 case IIO_CHAN_INFO_CALIBHEIGHT:
513 switch (chan->type) {
514 case IIO_STEPS:
515 st->height = val;
516 return 0;
517 default:
518 return -EINVAL;
519 }
Jin Fengff3fc8e2013-04-05 05:51:00 +0100520
Jonathan Cameron3a843312011-10-14 16:34:13 +0100521 default:
522 return -EINVAL;
523 }
524}
525
526/*
527 * Device type specific information.
528 */
529static const struct iio_info iio_dummy_info = {
530 .driver_module = THIS_MODULE,
531 .read_raw = &iio_dummy_read_raw,
532 .write_raw = &iio_dummy_write_raw,
Jonathan Camerone6477002011-10-14 16:34:14 +0100533#ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
Lars-Peter Clausencb955852013-12-07 10:45:00 +0000534 .read_event_config = &iio_simple_dummy_read_event_config,
535 .write_event_config = &iio_simple_dummy_write_event_config,
536 .read_event_value = &iio_simple_dummy_read_event_value,
537 .write_event_value = &iio_simple_dummy_write_event_value,
Jonathan Camerone6477002011-10-14 16:34:14 +0100538#endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
Jonathan Cameron3a843312011-10-14 16:34:13 +0100539};
540
541/**
542 * iio_dummy_init_device() - device instance specific init
543 * @indio_dev: the iio device structure
544 *
545 * Most drivers have one of these to set up default values,
546 * reset the device to known state etc.
547 */
548static int iio_dummy_init_device(struct iio_dev *indio_dev)
549{
550 struct iio_dummy_state *st = iio_priv(indio_dev);
551
552 st->dac_val = 0;
553 st->single_ended_adc_val = 73;
554 st->differential_adc_val[0] = 33;
555 st->differential_adc_val[1] = -34;
556 st->accel_val = 34;
557 st->accel_calibbias = -7;
558 st->accel_calibscale = &dummy_scales[0];
Daniel Baluta3e34e652014-11-10 14:45:34 +0200559 st->steps = 47;
560 st->activity_running = 98;
561 st->activity_walking = 4;
Jonathan Cameron3a843312011-10-14 16:34:13 +0100562
563 return 0;
564}
565
566/**
567 * iio_dummy_probe() - device instance probe
568 * @index: an id number for this instance.
569 *
570 * Arguments are bus type specific.
571 * I2C: iio_dummy_probe(struct i2c_client *client,
572 * const struct i2c_device_id *id)
573 * SPI: iio_dummy_probe(struct spi_device *spi)
574 */
Bill Pemberton4ae1c612012-11-19 13:21:57 -0500575static int iio_dummy_probe(int index)
Jonathan Cameron3a843312011-10-14 16:34:13 +0100576{
577 int ret;
578 struct iio_dev *indio_dev;
579 struct iio_dummy_state *st;
580
581 /*
582 * Allocate an IIO device.
583 *
584 * This structure contains all generic state
585 * information about the device instance.
586 * It also has a region (accessed by iio_priv()
587 * for chip specific state information.
588 */
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200589 indio_dev = iio_device_alloc(sizeof(*st));
Cristina Opriceana8f94c312015-03-31 12:51:38 +0300590 if (!indio_dev) {
Jonathan Cameron3a843312011-10-14 16:34:13 +0100591 ret = -ENOMEM;
592 goto error_ret;
593 }
594
595 st = iio_priv(indio_dev);
596 mutex_init(&st->lock);
597
598 iio_dummy_init_device(indio_dev);
599 /*
600 * With hardware: Set the parent device.
601 * indio_dev->dev.parent = &spi->dev;
602 * indio_dev->dev.parent = &client->dev;
603 */
604
605 /*
606 * Make the iio_dev struct available to remove function.
607 * Bus equivalents
608 * i2c_set_clientdata(client, indio_dev);
609 * spi_set_drvdata(spi, indio_dev);
610 */
611 iio_dummy_devs[index] = indio_dev;
612
Jonathan Cameron3a843312011-10-14 16:34:13 +0100613 /*
614 * Set the device name.
615 *
616 * This is typically a part number and obtained from the module
617 * id table.
618 * e.g. for i2c and spi:
619 * indio_dev->name = id->name;
620 * indio_dev->name = spi_get_device_id(spi)->name;
621 */
622 indio_dev->name = iio_dummy_part_number;
623
624 /* Provide description of available channels */
625 indio_dev->channels = iio_dummy_channels;
626 indio_dev->num_channels = ARRAY_SIZE(iio_dummy_channels);
627
628 /*
629 * Provide device type specific interface functions and
630 * constant data.
631 */
632 indio_dev->info = &iio_dummy_info;
633
634 /* Specify that device provides sysfs type interfaces */
635 indio_dev->modes = INDIO_DIRECT_MODE;
636
Jonathan Camerone6477002011-10-14 16:34:14 +0100637 ret = iio_simple_dummy_events_register(indio_dev);
Jonathan Cameron3a843312011-10-14 16:34:13 +0100638 if (ret < 0)
639 goto error_free_device;
Jonathan Cameron9ad2e2e2011-10-14 16:34:15 +0100640
Lars-Peter Clausen4ae03012014-11-26 18:55:11 +0100641 ret = iio_simple_dummy_configure_buffer(indio_dev);
Jonathan Camerone6477002011-10-14 16:34:14 +0100642 if (ret < 0)
643 goto error_unregister_events;
Jonathan Cameron3a843312011-10-14 16:34:13 +0100644
Lars-Peter Clausen3fff2272012-09-12 12:06:00 +0100645 ret = iio_device_register(indio_dev);
Jonathan Cameron9ad2e2e2011-10-14 16:34:15 +0100646 if (ret < 0)
647 goto error_unconfigure_buffer;
648
Jonathan Cameron3a843312011-10-14 16:34:13 +0100649 return 0;
Jonathan Cameron9ad2e2e2011-10-14 16:34:15 +0100650error_unconfigure_buffer:
651 iio_simple_dummy_unconfigure_buffer(indio_dev);
Jonathan Camerone6477002011-10-14 16:34:14 +0100652error_unregister_events:
653 iio_simple_dummy_events_unregister(indio_dev);
Jonathan Cameron3a843312011-10-14 16:34:13 +0100654error_free_device:
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200655 iio_device_free(indio_dev);
Jonathan Cameron3a843312011-10-14 16:34:13 +0100656error_ret:
657 return ret;
658}
659
660/**
661 * iio_dummy_remove() - device instance removal function
662 * @index: device index.
663 *
664 * Parameters follow those of iio_dummy_probe for buses.
665 */
Vladimirs Ambrosovs62a90da2015-05-30 11:20:16 +0300666static void iio_dummy_remove(int index)
Jonathan Cameron3a843312011-10-14 16:34:13 +0100667{
668 /*
669 * Get a pointer to the device instance iio_dev structure
670 * from the bus subsystem. E.g.
671 * struct iio_dev *indio_dev = i2c_get_clientdata(client);
672 * struct iio_dev *indio_dev = spi_get_drvdata(spi);
673 */
674 struct iio_dev *indio_dev = iio_dummy_devs[index];
675
676 /* Unregister the device */
677 iio_device_unregister(indio_dev);
678
679 /* Device specific code to power down etc */
680
Jonathan Cameron9ad2e2e2011-10-14 16:34:15 +0100681 /* Buffered capture related cleanup */
Jonathan Cameron9ad2e2e2011-10-14 16:34:15 +0100682 iio_simple_dummy_unconfigure_buffer(indio_dev);
Jonathan Camerone6477002011-10-14 16:34:14 +0100683
Vladimirs Ambrosovs62a90da2015-05-30 11:20:16 +0300684 iio_simple_dummy_events_unregister(indio_dev);
Jonathan Camerone6477002011-10-14 16:34:14 +0100685
Jonathan Cameron3a843312011-10-14 16:34:13 +0100686 /* Free all structures */
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200687 iio_device_free(indio_dev);
Jonathan Cameron3a843312011-10-14 16:34:13 +0100688}
689
690/**
691 * iio_dummy_init() - device driver registration
692 *
693 * Varies depending on bus type of the device. As there is no device
694 * here, call probe directly. For information on device registration
695 * i2c:
696 * Documentation/i2c/writing-clients
697 * spi:
698 * Documentation/spi/spi-summary
699 */
700static __init int iio_dummy_init(void)
701{
702 int i, ret;
Jimmy Picardaff89452014-06-13 06:56:00 +0100703
Jonathan Cameron3a843312011-10-14 16:34:13 +0100704 if (instances > 10) {
705 instances = 1;
706 return -EINVAL;
707 }
Lars-Peter Clausen3fff2272012-09-12 12:06:00 +0100708
Jonathan Cameron3a843312011-10-14 16:34:13 +0100709 /* Fake a bus */
Thomas Meyerd83fb182011-11-29 22:08:00 +0100710 iio_dummy_devs = kcalloc(instances, sizeof(*iio_dummy_devs),
711 GFP_KERNEL);
Jonathan Cameron3a843312011-10-14 16:34:13 +0100712 /* Here we have no actual device so call probe */
713 for (i = 0; i < instances; i++) {
714 ret = iio_dummy_probe(i);
715 if (ret < 0)
Vladimirs Ambrosovsb3f6af32015-05-30 11:20:15 +0300716 goto error_remove_devs;
Jonathan Cameron3a843312011-10-14 16:34:13 +0100717 }
718 return 0;
Vladimirs Ambrosovsb3f6af32015-05-30 11:20:15 +0300719
720error_remove_devs:
721 while (i--)
722 iio_dummy_remove(i);
723
724 kfree(iio_dummy_devs);
725 return ret;
Jonathan Cameron3a843312011-10-14 16:34:13 +0100726}
727module_init(iio_dummy_init);
728
729/**
730 * iio_dummy_exit() - device driver removal
731 *
732 * Varies depending on bus type of the device.
733 * As there is no device here, call remove directly.
734 */
735static __exit void iio_dummy_exit(void)
736{
737 int i;
Jimmy Picardaff89452014-06-13 06:56:00 +0100738
Jonathan Cameron3a843312011-10-14 16:34:13 +0100739 for (i = 0; i < instances; i++)
740 iio_dummy_remove(i);
741 kfree(iio_dummy_devs);
742}
743module_exit(iio_dummy_exit);
744
Jonathan Cameron0f8c9622012-09-02 21:34:59 +0100745MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
Jonathan Cameron3a843312011-10-14 16:34:13 +0100746MODULE_DESCRIPTION("IIO dummy driver");
747MODULE_LICENSE("GPL v2");