blob: ad3410e528b68fbc2cc281983da4a19921fa401b [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>
Daniel Baluta3d85fb6f2016-04-25 16:15:52 +030020#include <linux/string.h>
Jonathan Cameron3a843312011-10-14 16:34:13 +010021
Jonathan Cameron06458e22012-04-25 15:54:58 +010022#include <linux/iio/iio.h>
23#include <linux/iio/sysfs.h>
24#include <linux/iio/events.h>
25#include <linux/iio/buffer.h>
Daniel Baluta3d85fb6f2016-04-25 16:15:52 +030026#include <linux/iio/sw_device.h>
Jonathan Camerone6477002011-10-14 16:34:14 +010027#include "iio_simple_dummy.h"
Jonathan Cameron3a843312011-10-14 16:34:13 +010028
Daniel Baluta3d85fb6f2016-04-25 16:15:52 +030029static struct config_item_type iio_dummy_type = {
30 .ct_owner = THIS_MODULE,
31};
Jonathan Cameron3a843312011-10-14 16:34:13 +010032
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.
38 */
39struct iio_dummy_accel_calibscale {
40 int val;
41 int val2;
42 int regval; /* what would be written to hardware */
43};
44
45static const struct iio_dummy_accel_calibscale dummy_scales[] = {
46 { 0, 100, 0x8 }, /* 0.000100 */
47 { 0, 133, 0x7 }, /* 0.000133 */
Alexander Stein569c4ac2012-12-19 17:56:00 +000048 { 733, 13, 0x9 }, /* 733.000013 */
Jonathan Cameron3a843312011-10-14 16:34:13 +010049};
50
Lars-Peter Clausenbda624b2013-10-07 15:11:00 +010051#ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
52
53/*
54 * simple event - triggered when value rises above
55 * a threshold
56 */
57static 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),
61};
62
Daniel Baluta3e34e652014-11-10 14:45:34 +020063/*
64 * simple step detect event - triggered when a step is detected
65 */
66static const struct iio_event_spec step_detect_event = {
Irina Tirdea17a2cbc2015-01-11 21:10:12 +020067 .type = IIO_EV_TYPE_CHANGE,
Daniel Baluta3e34e652014-11-10 14:45:34 +020068 .dir = IIO_EV_DIR_NONE,
69 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
70};
71
72/*
73 * simple transition event - triggered when the reported running confidence
74 * value rises above a threshold value
75 */
76static 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),
80};
81
82/*
83 * simple transition event - triggered when the reported walking confidence
84 * value falls under a threshold value
85 */
86static 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),
90};
Lars-Peter Clausenbda624b2013-10-07 15:11:00 +010091#endif
92
Jonathan Cameron3a843312011-10-14 16:34:13 +010093/*
94 * iio_dummy_channels - Description of available channels
95 *
96 * This array of structures tells the IIO core about what the device
97 * actually provides for a given channel.
98 */
Lars-Peter Clausenf4e4b952012-08-09 08:51:00 +010099static const struct iio_chan_spec iio_dummy_channels[] = {
Jonathan Cameron3a843312011-10-14 16:34:13 +0100100 /* indexed ADC channel in_voltage0_raw etc */
101 {
102 .type = IIO_VOLTAGE,
103 /* Channel has a numeric index of 0 */
104 .indexed = 1,
105 .channel = 0,
106 /* What other information is available? */
Jonathan Cameronfa357a62013-02-19 21:12:21 +0000107 .info_mask_separate =
Jonathan Cameron3a843312011-10-14 16:34:13 +0100108 /*
Lars-Peter Clausen41fd9352012-04-15 17:41:27 +0100109 * in_voltage0_raw
110 * Raw (unscaled no bias removal etc) measurement
111 * from the device.
112 */
Jonathan Cameronfa357a62013-02-19 21:12:21 +0000113 BIT(IIO_CHAN_INFO_RAW) |
Lars-Peter Clausen41fd9352012-04-15 17:41:27 +0100114 /*
Jonathan Cameron3a843312011-10-14 16:34:13 +0100115 * in_voltage0_offset
116 * Offset for userspace to apply prior to scale
117 * when converting to standard units (microvolts)
118 */
Jonathan Cameronfa357a62013-02-19 21:12:21 +0000119 BIT(IIO_CHAN_INFO_OFFSET) |
Jonathan Cameron3a843312011-10-14 16:34:13 +0100120 /*
121 * in_voltage0_scale
122 * Multipler for userspace to apply post offset
123 * when converting to standard units (microvolts)
124 */
Jonathan Cameronfa357a62013-02-19 21:12:21 +0000125 BIT(IIO_CHAN_INFO_SCALE),
Jonathan Cameron6a63aa02013-09-08 14:57:00 +0100126 /*
127 * sampling_frequency
128 * The frequency in Hz at which the channels are sampled
129 */
130 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
Jonathan Cameron9ad2e2e2011-10-14 16:34:15 +0100131 /* The ordering of elements in the buffer via an enum */
Alison Schofieldf8087ab2015-10-26 13:48:23 -0700132 .scan_index = DUMMY_INDEX_VOLTAGE_0,
Jonathan Cameron9ad2e2e2011-10-14 16:34:15 +0100133 .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 */
138 },
Jonathan Camerone6477002011-10-14 16:34:14 +0100139#ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
Lars-Peter Clausenbda624b2013-10-07 15:11:00 +0100140 .event_spec = &iio_dummy_event,
141 .num_event_specs = 1,
Jonathan Camerone6477002011-10-14 16:34:14 +0100142#endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
Jonathan Cameron3a843312011-10-14 16:34:13 +0100143 },
144 /* Differential ADC channel in_voltage1-voltage2_raw etc*/
145 {
146 .type = IIO_VOLTAGE,
147 .differential = 1,
148 /*
149 * Indexing for differential channels uses channel
150 * for the positive part, channel2 for the negative.
151 */
152 .indexed = 1,
153 .channel = 1,
154 .channel2 = 2,
Jonathan Cameron3a843312011-10-14 16:34:13 +0100155 /*
Lars-Peter Clausen41fd9352012-04-15 17:41:27 +0100156 * in_voltage1-voltage2_raw
157 * Raw (unscaled no bias removal etc) measurement
158 * from the device.
159 */
Jonathan Cameronfa357a62013-02-19 21:12:21 +0000160 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
Lars-Peter Clausen41fd9352012-04-15 17:41:27 +0100161 /*
Jonathan Cameron3a843312011-10-14 16:34:13 +0100162 * in_voltage-voltage_scale
163 * Shared version of scale - shared by differential
164 * input channels of type IIO_VOLTAGE.
165 */
Jonathan Cameronfa357a62013-02-19 21:12:21 +0000166 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
Jonathan Cameron6a63aa02013-09-08 14:57:00 +0100167 /*
168 * sampling_frequency
169 * The frequency in Hz at which the channels are sampled
170 */
Alison Schofieldf8087ab2015-10-26 13:48:23 -0700171 .scan_index = DUMMY_INDEX_DIFFVOLTAGE_1M2,
Jonathan Cameron9ad2e2e2011-10-14 16:34:15 +0100172 .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 */
177 },
Jonathan Cameron3a843312011-10-14 16:34:13 +0100178 },
179 /* Differential ADC channel in_voltage3-voltage4_raw etc*/
180 {
181 .type = IIO_VOLTAGE,
182 .differential = 1,
183 .indexed = 1,
184 .channel = 3,
185 .channel2 = 4,
Jonathan Cameronfa357a62013-02-19 21:12:21 +0000186 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
187 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
Jonathan Cameron6a63aa02013-09-08 14:57:00 +0100188 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
Alison Schofieldf8087ab2015-10-26 13:48:23 -0700189 .scan_index = DUMMY_INDEX_DIFFVOLTAGE_3M4,
Jonathan Cameron9ad2e2e2011-10-14 16:34:15 +0100190 .scan_type = {
191 .sign = 's',
192 .realbits = 11,
193 .storagebits = 16,
194 .shift = 0,
195 },
Jonathan Cameron3a843312011-10-14 16:34:13 +0100196 },
197 /*
198 * 'modified' (i.e. axis specified) acceleration channel
199 * in_accel_z_raw
200 */
201 {
202 .type = IIO_ACCEL,
203 .modified = 1,
204 /* Channel 2 is use for modifiers */
205 .channel2 = IIO_MOD_X,
Jonathan Cameronfa357a62013-02-19 21:12:21 +0000206 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
Jonathan Cameron3a843312011-10-14 16:34:13 +0100207 /*
Jin Fengff3fc8e2013-04-05 05:51:00 +0100208 * Internal bias and gain correction values. Applied
Jonathan Cameron3a843312011-10-14 16:34:13 +0100209 * by the hardware or driver prior to userspace
210 * seeing the readings. Typically part of hardware
211 * calibration.
212 */
Jin Fengff3fc8e2013-04-05 05:51:00 +0100213 BIT(IIO_CHAN_INFO_CALIBSCALE) |
Jonathan Cameronfa357a62013-02-19 21:12:21 +0000214 BIT(IIO_CHAN_INFO_CALIBBIAS),
Jonathan Cameron6a63aa02013-09-08 14:57:00 +0100215 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
Alison Schofieldf8087ab2015-10-26 13:48:23 -0700216 .scan_index = DUMMY_INDEX_ACCELX,
Jonathan Cameron9ad2e2e2011-10-14 16:34:15 +0100217 .scan_type = { /* Description of storage in buffer */
218 .sign = 's', /* signed */
Peter Meerwald25c38aa2012-06-15 19:27:10 +0200219 .realbits = 16, /* 16 bits */
Jonathan Cameron9ad2e2e2011-10-14 16:34:15 +0100220 .storagebits = 16, /* 16 bits used for storage */
221 .shift = 0, /* zero shift */
222 },
223 },
224 /*
225 * Convenience macro for timestamps. 4 is the index in
226 * the buffer.
227 */
228 IIO_CHAN_SOFT_TIMESTAMP(4),
229 /* DAC channel out_voltage0_raw */
230 {
231 .type = IIO_VOLTAGE,
Jonathan Cameronfa357a62013-02-19 21:12:21 +0000232 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
Lars-Peter Clausen4ae03012014-11-26 18:55:11 +0100233 .scan_index = -1, /* No buffer support */
Jonathan Cameron9ad2e2e2011-10-14 16:34:15 +0100234 .output = 1,
235 .indexed = 1,
236 .channel = 0,
Jonathan Cameron3a843312011-10-14 16:34:13 +0100237 },
Daniel Baluta3e34e652014-11-10 14:45:34 +0200238 {
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),
Lars-Peter Clausen4ae03012014-11-26 18:55:11 +0100243 .scan_index = -1, /* No buffer support */
Daniel Baluta3e34e652014-11-10 14:45:34 +0200244#ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
245 .event_spec = &step_detect_event,
246 .num_event_specs = 1,
247#endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
248 },
249 {
250 .type = IIO_ACTIVITY,
251 .modified = 1,
252 .channel2 = IIO_MOD_RUNNING,
253 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
Lars-Peter Clausen4ae03012014-11-26 18:55:11 +0100254 .scan_index = -1, /* No buffer support */
Daniel Baluta3e34e652014-11-10 14:45:34 +0200255#ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
256 .event_spec = &iio_running_event,
257 .num_event_specs = 1,
258#endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
259 },
260 {
261 .type = IIO_ACTIVITY,
262 .modified = 1,
263 .channel2 = IIO_MOD_WALKING,
264 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
Lars-Peter Clausen4ae03012014-11-26 18:55:11 +0100265 .scan_index = -1, /* No buffer support */
Daniel Baluta3e34e652014-11-10 14:45:34 +0200266#ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
267 .event_spec = &iio_walking_event,
268 .num_event_specs = 1,
269#endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
270 },
Jonathan Cameron3a843312011-10-14 16:34:13 +0100271};
272
273/**
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)
Jonathan Cameronfa357a62013-02-19 21:12:21 +0000279 * @mask: what we actually want to read as per the info_mask_*
280 * in iio_chan_spec.
Jonathan Cameron3a843312011-10-14 16:34:13 +0100281 */
282static 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)
287{
288 struct iio_dummy_state *st = iio_priv(indio_dev);
289 int ret = -EINVAL;
290
291 mutex_lock(&st->lock);
292 switch (mask) {
Lars-Peter Clausen41fd9352012-04-15 17:41:27 +0100293 case IIO_CHAN_INFO_RAW: /* magic value - channel value read */
Jonathan Cameron3a843312011-10-14 16:34:13 +0100294 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;
309 }
310 break;
311 case IIO_ACCEL:
312 *val = st->accel_val;
313 ret = IIO_VAL_INT;
314 break;
315 default:
316 break;
317 }
318 break;
Daniel Baluta3e34e652014-11-10 14:45:34 +0200319 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;
337 }
338 break;
339 default:
340 break;
341 }
342 break;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100343 case IIO_CHAN_INFO_OFFSET:
Jonathan Cameron3a843312011-10-14 16:34:13 +0100344 /* only single ended adc -> 7 */
345 *val = 7;
346 ret = IIO_VAL_INT;
347 break;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100348 case IIO_CHAN_INFO_SCALE:
Daniel Baluta3e34e652014-11-10 14:45:34 +0200349 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:
Alison Schofield4b60c882015-10-26 13:50:29 -0700359 /* all differential adc -> 0.000001344 */
Daniel Baluta3e34e652014-11-10 14:45:34 +0200360 *val = 0;
361 *val2 = 1344;
362 ret = IIO_VAL_INT_PLUS_NANO;
363 }
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100364 break;
Daniel Baluta3e34e652014-11-10 14:45:34 +0200365 default:
366 break;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100367 }
Jonathan Cameron3a843312011-10-14 16:34:13 +0100368 break;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100369 case IIO_CHAN_INFO_CALIBBIAS:
Jonathan Cameron3a843312011-10-14 16:34:13 +0100370 /* only the acceleration axis - read from cache */
371 *val = st->accel_calibbias;
372 ret = IIO_VAL_INT;
373 break;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100374 case IIO_CHAN_INFO_CALIBSCALE:
Jonathan Cameron3a843312011-10-14 16:34:13 +0100375 *val = st->accel_calibscale->val;
376 *val2 = st->accel_calibscale->val2;
377 ret = IIO_VAL_INT_PLUS_MICRO;
378 break;
Jonathan Cameron6a63aa02013-09-08 14:57:00 +0100379 case IIO_CHAN_INFO_SAMP_FREQ:
380 *val = 3;
381 *val2 = 33;
382 ret = IIO_VAL_INT_PLUS_NANO;
383 break;
Daniel Baluta3e34e652014-11-10 14:45:34 +0200384 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;
392 }
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;
402 }
403 break;
404
Jonathan Cameron3a843312011-10-14 16:34:13 +0100405 default:
406 break;
407 }
408 mutex_unlock(&st->lock);
409 return ret;
410}
411
412/**
413 * iio_dummy_write_raw() - data write function.
414 * @indio_dev: the struct iio_dev associated with this device instance
Alexander Stein569c4ac2012-12-19 17:56:00 +0000415 * @chan: the channel whose data is to be written
Peter Meerwald25c38aa2012-06-15 19:27:10 +0200416 * @val: first element of value to set (typically INT)
417 * @val2: second element of value to set (typically MICRO)
Jonathan Cameronfa357a62013-02-19 21:12:21 +0000418 * @mask: what we actually want to write as per the info_mask_*
419 * in iio_chan_spec.
Jonathan Cameron3a843312011-10-14 16:34:13 +0100420 *
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.
424 */
425static 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)
430{
431 int i;
432 int ret = 0;
433 struct iio_dummy_state *st = iio_priv(indio_dev);
434
435 switch (mask) {
Lars-Peter Clausen41fd9352012-04-15 17:41:27 +0100436 case IIO_CHAN_INFO_RAW:
Daniel Baluta3e34e652014-11-10 14:45:34 +0200437 switch (chan->type) {
438 case IIO_VOLTAGE:
439 if (chan->output == 0)
440 return -EINVAL;
Jonathan Cameron3a843312011-10-14 16:34:13 +0100441
Daniel Baluta3e34e652014-11-10 14:45:34 +0200442 /* 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;
449 }
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;
471 }
472 break;
473 default:
474 return -EINVAL;
475 }
Jin Fengff3fc8e2013-04-05 05:51:00 +0100476 case IIO_CHAN_INFO_CALIBSCALE:
Jonathan Cameron3a843312011-10-14 16:34:13 +0100477 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;
Jin Fengff3fc8e2013-04-05 05:51:00 +0100489 case IIO_CHAN_INFO_CALIBBIAS:
490 mutex_lock(&st->lock);
491 st->accel_calibbias = val;
492 mutex_unlock(&st->lock);
493 return 0;
Daniel Baluta3e34e652014-11-10 14:45:34 +0200494 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;
503 }
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;
511 }
Jin Fengff3fc8e2013-04-05 05:51:00 +0100512
Jonathan Cameron3a843312011-10-14 16:34:13 +0100513 default:
514 return -EINVAL;
515 }
516}
517
518/*
519 * Device type specific information.
520 */
521static const struct iio_info iio_dummy_info = {
522 .driver_module = THIS_MODULE,
523 .read_raw = &iio_dummy_read_raw,
524 .write_raw = &iio_dummy_write_raw,
Jonathan Camerone6477002011-10-14 16:34:14 +0100525#ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
Lars-Peter Clausencb955852013-12-07 10:45:00 +0000526 .read_event_config = &iio_simple_dummy_read_event_config,
527 .write_event_config = &iio_simple_dummy_write_event_config,
528 .read_event_value = &iio_simple_dummy_read_event_value,
529 .write_event_value = &iio_simple_dummy_write_event_value,
Jonathan Camerone6477002011-10-14 16:34:14 +0100530#endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
Jonathan Cameron3a843312011-10-14 16:34:13 +0100531};
532
533/**
534 * iio_dummy_init_device() - device instance specific init
535 * @indio_dev: the iio device structure
536 *
537 * Most drivers have one of these to set up default values,
538 * reset the device to known state etc.
539 */
540static int iio_dummy_init_device(struct iio_dev *indio_dev)
541{
542 struct iio_dummy_state *st = iio_priv(indio_dev);
543
544 st->dac_val = 0;
545 st->single_ended_adc_val = 73;
546 st->differential_adc_val[0] = 33;
547 st->differential_adc_val[1] = -34;
548 st->accel_val = 34;
549 st->accel_calibbias = -7;
550 st->accel_calibscale = &dummy_scales[0];
Daniel Baluta3e34e652014-11-10 14:45:34 +0200551 st->steps = 47;
552 st->activity_running = 98;
553 st->activity_walking = 4;
Jonathan Cameron3a843312011-10-14 16:34:13 +0100554
555 return 0;
556}
557
558/**
559 * iio_dummy_probe() - device instance probe
560 * @index: an id number for this instance.
561 *
562 * Arguments are bus type specific.
563 * I2C: iio_dummy_probe(struct i2c_client *client,
564 * const struct i2c_device_id *id)
565 * SPI: iio_dummy_probe(struct spi_device *spi)
566 */
Daniel Baluta3d85fb6f2016-04-25 16:15:52 +0300567static struct iio_sw_device *iio_dummy_probe(const char *name)
Jonathan Cameron3a843312011-10-14 16:34:13 +0100568{
569 int ret;
570 struct iio_dev *indio_dev;
571 struct iio_dummy_state *st;
Daniel Baluta3d85fb6f2016-04-25 16:15:52 +0300572 struct iio_sw_device *swd;
Jonathan Cameron3a843312011-10-14 16:34:13 +0100573
Daniel Baluta3d85fb6f2016-04-25 16:15:52 +0300574 swd = kzalloc(sizeof(*swd), GFP_KERNEL);
575 if (!swd) {
576 ret = -ENOMEM;
577 goto error_kzalloc;
578 }
Jonathan Cameron3a843312011-10-14 16:34:13 +0100579 /*
580 * Allocate an IIO device.
581 *
582 * This structure contains all generic state
583 * information about the device instance.
584 * It also has a region (accessed by iio_priv()
585 * for chip specific state information.
586 */
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200587 indio_dev = iio_device_alloc(sizeof(*st));
Cristina Opriceana8f94c312015-03-31 12:51:38 +0300588 if (!indio_dev) {
Jonathan Cameron3a843312011-10-14 16:34:13 +0100589 ret = -ENOMEM;
590 goto error_ret;
591 }
592
593 st = iio_priv(indio_dev);
594 mutex_init(&st->lock);
595
596 iio_dummy_init_device(indio_dev);
597 /*
598 * With hardware: Set the parent device.
599 * indio_dev->dev.parent = &spi->dev;
600 * indio_dev->dev.parent = &client->dev;
601 */
602
603 /*
604 * Make the iio_dev struct available to remove function.
605 * Bus equivalents
606 * i2c_set_clientdata(client, indio_dev);
607 * spi_set_drvdata(spi, indio_dev);
608 */
Daniel Baluta3d85fb6f2016-04-25 16:15:52 +0300609 swd->device = indio_dev;
Jonathan Cameron3a843312011-10-14 16:34:13 +0100610
Jonathan Cameron3a843312011-10-14 16:34:13 +0100611 /*
612 * Set the device name.
613 *
614 * This is typically a part number and obtained from the module
615 * id table.
616 * e.g. for i2c and spi:
617 * indio_dev->name = id->name;
618 * indio_dev->name = spi_get_device_id(spi)->name;
619 */
Daniel Baluta3d85fb6f2016-04-25 16:15:52 +0300620 indio_dev->name = kstrdup(name, GFP_KERNEL);
Jonathan Cameron3a843312011-10-14 16:34:13 +0100621
622 /* Provide description of available channels */
623 indio_dev->channels = iio_dummy_channels;
624 indio_dev->num_channels = ARRAY_SIZE(iio_dummy_channels);
625
626 /*
627 * Provide device type specific interface functions and
628 * constant data.
629 */
630 indio_dev->info = &iio_dummy_info;
631
632 /* Specify that device provides sysfs type interfaces */
633 indio_dev->modes = INDIO_DIRECT_MODE;
634
Jonathan Camerone6477002011-10-14 16:34:14 +0100635 ret = iio_simple_dummy_events_register(indio_dev);
Jonathan Cameron3a843312011-10-14 16:34:13 +0100636 if (ret < 0)
637 goto error_free_device;
Jonathan Cameron9ad2e2e2011-10-14 16:34:15 +0100638
Lars-Peter Clausen4ae03012014-11-26 18:55:11 +0100639 ret = iio_simple_dummy_configure_buffer(indio_dev);
Jonathan Camerone6477002011-10-14 16:34:14 +0100640 if (ret < 0)
641 goto error_unregister_events;
Jonathan Cameron3a843312011-10-14 16:34:13 +0100642
Lars-Peter Clausen3fff2272012-09-12 12:06:00 +0100643 ret = iio_device_register(indio_dev);
Jonathan Cameron9ad2e2e2011-10-14 16:34:15 +0100644 if (ret < 0)
645 goto error_unconfigure_buffer;
646
Daniel Baluta3d85fb6f2016-04-25 16:15:52 +0300647 iio_swd_group_init_type_name(swd, name, &iio_dummy_type);
648
649 return swd;
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:
Daniel Baluta3d85fb6f2016-04-25 16:15:52 +0300657 kfree(swd);
658error_kzalloc:
659 return ERR_PTR(ret);
Jonathan Cameron3a843312011-10-14 16:34:13 +0100660}
661
662/**
663 * iio_dummy_remove() - device instance removal function
Daniel Baluta3d85fb6f2016-04-25 16:15:52 +0300664 * @swd: pointer to software IIO device abstraction
Jonathan Cameron3a843312011-10-14 16:34:13 +0100665 *
666 * Parameters follow those of iio_dummy_probe for buses.
667 */
Daniel Baluta3d85fb6f2016-04-25 16:15:52 +0300668static int iio_dummy_remove(struct iio_sw_device *swd)
Jonathan Cameron3a843312011-10-14 16:34:13 +0100669{
670 /*
671 * Get a pointer to the device instance iio_dev structure
672 * from the bus subsystem. E.g.
673 * struct iio_dev *indio_dev = i2c_get_clientdata(client);
674 * struct iio_dev *indio_dev = spi_get_drvdata(spi);
675 */
Daniel Baluta3d85fb6f2016-04-25 16:15:52 +0300676 struct iio_dev *indio_dev = swd->device;
Jonathan Cameron3a843312011-10-14 16:34:13 +0100677
678 /* Unregister the device */
679 iio_device_unregister(indio_dev);
680
681 /* Device specific code to power down etc */
682
Jonathan Cameron9ad2e2e2011-10-14 16:34:15 +0100683 /* Buffered capture related cleanup */
Jonathan Cameron9ad2e2e2011-10-14 16:34:15 +0100684 iio_simple_dummy_unconfigure_buffer(indio_dev);
Jonathan Camerone6477002011-10-14 16:34:14 +0100685
Vladimirs Ambrosovs62a90da2015-05-30 11:20:16 +0300686 iio_simple_dummy_events_unregister(indio_dev);
Jonathan Camerone6477002011-10-14 16:34:14 +0100687
Jonathan Cameron3a843312011-10-14 16:34:13 +0100688 /* Free all structures */
Daniel Baluta3d85fb6f2016-04-25 16:15:52 +0300689 kfree(indio_dev->name);
Lars-Peter Clausen7cbb7532012-04-26 13:35:01 +0200690 iio_device_free(indio_dev);
Jonathan Cameron3a843312011-10-14 16:34:13 +0100691
Daniel Baluta3d85fb6f2016-04-25 16:15:52 +0300692 return 0;
693}
Jonathan Cameron3a843312011-10-14 16:34:13 +0100694/**
Daniel Baluta3d85fb6f2016-04-25 16:15:52 +0300695 * module_iio_sw_device_driver() - device driver registration
Jonathan Cameron3a843312011-10-14 16:34:13 +0100696 *
697 * Varies depending on bus type of the device. As there is no device
698 * here, call probe directly. For information on device registration
699 * i2c:
700 * Documentation/i2c/writing-clients
701 * spi:
702 * Documentation/spi/spi-summary
703 */
Daniel Baluta3d85fb6f2016-04-25 16:15:52 +0300704static const struct iio_sw_device_ops iio_dummy_device_ops = {
705 .probe = iio_dummy_probe,
706 .remove = iio_dummy_remove,
707};
Jimmy Picardaff89452014-06-13 06:56:00 +0100708
Daniel Baluta3d85fb6f2016-04-25 16:15:52 +0300709static struct iio_sw_device_type iio_dummy_device = {
710 .name = "dummy",
711 .owner = THIS_MODULE,
712 .ops = &iio_dummy_device_ops,
713};
Lars-Peter Clausen3fff2272012-09-12 12:06:00 +0100714
Daniel Baluta3d85fb6f2016-04-25 16:15:52 +0300715module_iio_sw_device_driver(iio_dummy_device);
Jonathan Cameron3a843312011-10-14 16:34:13 +0100716
Jonathan Cameron0f8c9622012-09-02 21:34:59 +0100717MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
Jonathan Cameron3a843312011-10-14 16:34:13 +0100718MODULE_DESCRIPTION("IIO dummy driver");
719MODULE_LICENSE("GPL v2");