Commit | Line | Data |
---|---|---|
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> | |
3a84331d | 20 | |
06458e27 JC |
21 | #include <linux/iio/iio.h> |
22 | #include <linux/iio/sysfs.h> | |
23 | #include <linux/iio/events.h> | |
24 | #include <linux/iio/buffer.h> | |
e6477000 | 25 | #include "iio_simple_dummy.h" |
3a84331d JC |
26 | |
27 | /* | |
28 | * A few elements needed to fake a bus for this driver | |
25c38aa3 | 29 | * Note instances parameter controls how many of these |
3a84331d JC |
30 | * dummy devices are registered. |
31 | */ | |
32 | static unsigned instances = 1; | |
33 | module_param(instances, int, 0); | |
34 | ||
35 | /* Pointer array used to fake bus elements */ | |
36 | static struct iio_dev **iio_dummy_devs; | |
37 | ||
38 | /* Fake a name for the part number, usually obtained from the id table */ | |
39 | static 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 | */ | |
47 | struct iio_dummy_accel_calibscale { | |
48 | int val; | |
49 | int val2; | |
50 | int regval; /* what would be written to hardware */ | |
51 | }; | |
52 | ||
53 | static const struct iio_dummy_accel_calibscale dummy_scales[] = { | |
54 | { 0, 100, 0x8 }, /* 0.000100 */ | |
55 | { 0, 133, 0x7 }, /* 0.000133 */ | |
569c4ac2 | 56 | { 733, 13, 0x9 }, /* 733.000013 */ |
3a84331d JC |
57 | }; |
58 | ||
bda624b0 LPC |
59 | #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS |
60 | ||
61 | /* | |
62 | * simple event - triggered when value rises above | |
63 | * a threshold | |
64 | */ | |
65 | static 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 | ||
3e34e650 DB |
71 | /* |
72 | * simple step detect event - triggered when a step is detected | |
73 | */ | |
74 | static const struct iio_event_spec step_detect_event = { | |
17a2cbc2 | 75 | .type = IIO_EV_TYPE_CHANGE, |
3e34e650 DB |
76 | .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 | */ | |
84 | static 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 | */ | |
94 | static 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 | }; | |
bda624b0 LPC |
99 | #endif |
100 | ||
3a84331d JC |
101 | /* |
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 | */ | |
f4e4b955 | 107 | static const struct iio_chan_spec iio_dummy_channels[] = { |
3a84331d JC |
108 | /* 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? */ | |
fa357a6a | 115 | .info_mask_separate = |
41fd935b LPC |
116 | /* |
117 | * in_voltage0_raw | |
118 | * Raw (unscaled no bias removal etc) measurement | |
119 | * from the device. | |
120 | */ | |
fa357a6a | 121 | BIT(IIO_CHAN_INFO_RAW) | |
3a84331d JC |
122 | /* |
123 | * in_voltage0_offset | |
124 | * Offset for userspace to apply prior to scale | |
125 | * when converting to standard units (microvolts) | |
126 | */ | |
fa357a6a | 127 | BIT(IIO_CHAN_INFO_OFFSET) | |
3a84331d JC |
128 | /* |
129 | * in_voltage0_scale | |
130 | * Multipler for userspace to apply post offset | |
131 | * when converting to standard units (microvolts) | |
132 | */ | |
fa357a6a | 133 | BIT(IIO_CHAN_INFO_SCALE), |
6a63aa0a JC |
134 | /* |
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), | |
9ad2e2e1 JC |
139 | /* The ordering of elements in the buffer via an enum */ |
140 | .scan_index = voltage0, | |
141 | .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 | }, | |
e6477000 | 147 | #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS |
bda624b0 LPC |
148 | .event_spec = &iio_dummy_event, |
149 | .num_event_specs = 1, | |
e6477000 | 150 | #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */ |
3a84331d JC |
151 | }, |
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, | |
41fd935b LPC |
163 | /* |
164 | * in_voltage1-voltage2_raw | |
165 | * Raw (unscaled no bias removal etc) measurement | |
166 | * from the device. | |
167 | */ | |
fa357a6a | 168 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
3a84331d JC |
169 | /* |
170 | * in_voltage-voltage_scale | |
171 | * Shared version of scale - shared by differential | |
172 | * input channels of type IIO_VOLTAGE. | |
173 | */ | |
fa357a6a | 174 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), |
6a63aa0a JC |
175 | /* |
176 | * sampling_frequency | |
177 | * The frequency in Hz at which the channels are sampled | |
178 | */ | |
9ad2e2e1 JC |
179 | .scan_index = diffvoltage1m2, |
180 | .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 | }, | |
3a84331d JC |
186 | }, |
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, | |
fa357a6a JC |
194 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
195 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), | |
6a63aa0a | 196 | .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ), |
9ad2e2e1 JC |
197 | .scan_index = diffvoltage3m4, |
198 | .scan_type = { | |
199 | .sign = 's', | |
200 | .realbits = 11, | |
201 | .storagebits = 16, | |
202 | .shift = 0, | |
203 | }, | |
3a84331d JC |
204 | }, |
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, | |
fa357a6a | 214 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
3a84331d | 215 | /* |
ff3fc8eb | 216 | * Internal bias and gain correction values. Applied |
3a84331d JC |
217 | * by the hardware or driver prior to userspace |
218 | * seeing the readings. Typically part of hardware | |
219 | * calibration. | |
220 | */ | |
ff3fc8eb | 221 | BIT(IIO_CHAN_INFO_CALIBSCALE) | |
fa357a6a | 222 | BIT(IIO_CHAN_INFO_CALIBBIAS), |
6a63aa0a | 223 | .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ), |
9ad2e2e1 JC |
224 | .scan_index = accelx, |
225 | .scan_type = { /* Description of storage in buffer */ | |
226 | .sign = 's', /* signed */ | |
25c38aa3 | 227 | .realbits = 16, /* 16 bits */ |
9ad2e2e1 JC |
228 | .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, | |
fa357a6a | 240 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
4ae03019 | 241 | .scan_index = -1, /* No buffer support */ |
9ad2e2e1 JC |
242 | .output = 1, |
243 | .indexed = 1, | |
244 | .channel = 0, | |
3a84331d | 245 | }, |
3e34e650 DB |
246 | { |
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), | |
4ae03019 | 251 | .scan_index = -1, /* No buffer support */ |
3e34e650 DB |
252 | #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), | |
4ae03019 | 262 | .scan_index = -1, /* No buffer support */ |
3e34e650 DB |
263 | #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), | |
4ae03019 | 273 | .scan_index = -1, /* No buffer support */ |
3e34e650 DB |
274 | #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 | }, | |
3a84331d JC |
279 | }; |
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) | |
fa357a6a JC |
287 | * @mask: what we actually want to read as per the info_mask_* |
288 | * in iio_chan_spec. | |
3a84331d JC |
289 | */ |
290 | static 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) { | |
41fd935b | 301 | case IIO_CHAN_INFO_RAW: /* magic value - channel value read */ |
3a84331d JC |
302 | 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; | |
3e34e650 DB |
327 | 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; | |
c8a9f805 | 351 | case IIO_CHAN_INFO_OFFSET: |
3a84331d JC |
352 | /* only single ended adc -> 7 */ |
353 | *val = 7; | |
354 | ret = IIO_VAL_INT; | |
355 | break; | |
c8a9f805 | 356 | case IIO_CHAN_INFO_SCALE: |
3e34e650 DB |
357 | 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: | |
367 | /* all differential adc channels -> | |
368 | * 0.000001344 */ | |
369 | *val = 0; | |
370 | *val2 = 1344; | |
371 | ret = IIO_VAL_INT_PLUS_NANO; | |
372 | } | |
373 | break; | |
374 | default: | |
c8a9f805 | 375 | break; |
c8a9f805 | 376 | } |
3a84331d | 377 | break; |
c8a9f805 | 378 | case IIO_CHAN_INFO_CALIBBIAS: |
3a84331d JC |
379 | /* only the acceleration axis - read from cache */ |
380 | *val = st->accel_calibbias; | |
381 | ret = IIO_VAL_INT; | |
382 | break; | |
c8a9f805 | 383 | case IIO_CHAN_INFO_CALIBSCALE: |
3a84331d JC |
384 | *val = st->accel_calibscale->val; |
385 | *val2 = st->accel_calibscale->val2; | |
386 | ret = IIO_VAL_INT_PLUS_MICRO; | |
387 | break; | |
6a63aa0a JC |
388 | case IIO_CHAN_INFO_SAMP_FREQ: |
389 | *val = 3; | |
390 | *val2 = 33; | |
391 | ret = IIO_VAL_INT_PLUS_NANO; | |
392 | break; | |
3e34e650 DB |
393 | case IIO_CHAN_INFO_ENABLE: |
394 | switch (chan->type) { | |
395 | case IIO_STEPS: | |
396 | *val = st->steps_enabled; | |
397 | ret = IIO_VAL_INT; | |
398 | break; | |
399 | default: | |
400 | break; | |
401 | } | |
402 | break; | |
403 | case IIO_CHAN_INFO_CALIBHEIGHT: | |
404 | switch (chan->type) { | |
405 | case IIO_STEPS: | |
406 | *val = st->height; | |
407 | ret = IIO_VAL_INT; | |
408 | break; | |
409 | default: | |
410 | break; | |
411 | } | |
412 | break; | |
413 | ||
3a84331d JC |
414 | default: |
415 | break; | |
416 | } | |
417 | mutex_unlock(&st->lock); | |
418 | return ret; | |
419 | } | |
420 | ||
421 | /** | |
422 | * iio_dummy_write_raw() - data write function. | |
423 | * @indio_dev: the struct iio_dev associated with this device instance | |
569c4ac2 | 424 | * @chan: the channel whose data is to be written |
25c38aa3 PM |
425 | * @val: first element of value to set (typically INT) |
426 | * @val2: second element of value to set (typically MICRO) | |
fa357a6a JC |
427 | * @mask: what we actually want to write as per the info_mask_* |
428 | * in iio_chan_spec. | |
3a84331d JC |
429 | * |
430 | * Note that all raw writes are assumed IIO_VAL_INT and info mask elements | |
431 | * are assumed to be IIO_INT_PLUS_MICRO unless the callback write_raw_get_fmt | |
432 | * in struct iio_info is provided by the driver. | |
433 | */ | |
434 | static int iio_dummy_write_raw(struct iio_dev *indio_dev, | |
435 | struct iio_chan_spec const *chan, | |
436 | int val, | |
437 | int val2, | |
438 | long mask) | |
439 | { | |
440 | int i; | |
441 | int ret = 0; | |
442 | struct iio_dummy_state *st = iio_priv(indio_dev); | |
443 | ||
444 | switch (mask) { | |
41fd935b | 445 | case IIO_CHAN_INFO_RAW: |
3e34e650 DB |
446 | switch (chan->type) { |
447 | case IIO_VOLTAGE: | |
448 | if (chan->output == 0) | |
449 | return -EINVAL; | |
3a84331d | 450 | |
3e34e650 DB |
451 | /* Locking not required as writing single value */ |
452 | mutex_lock(&st->lock); | |
453 | st->dac_val = val; | |
454 | mutex_unlock(&st->lock); | |
455 | return 0; | |
456 | default: | |
457 | return -EINVAL; | |
458 | } | |
459 | case IIO_CHAN_INFO_PROCESSED: | |
460 | switch (chan->type) { | |
461 | case IIO_STEPS: | |
462 | mutex_lock(&st->lock); | |
463 | st->steps = val; | |
464 | mutex_unlock(&st->lock); | |
465 | return 0; | |
466 | case IIO_ACTIVITY: | |
467 | if (val < 0) | |
468 | val = 0; | |
469 | if (val > 100) | |
470 | val = 100; | |
471 | switch (chan->channel2) { | |
472 | case IIO_MOD_RUNNING: | |
473 | st->activity_running = val; | |
474 | return 0; | |
475 | case IIO_MOD_WALKING: | |
476 | st->activity_walking = val; | |
477 | return 0; | |
478 | default: | |
479 | return -EINVAL; | |
480 | } | |
481 | break; | |
482 | default: | |
483 | return -EINVAL; | |
484 | } | |
ff3fc8eb | 485 | case IIO_CHAN_INFO_CALIBSCALE: |
3a84331d JC |
486 | mutex_lock(&st->lock); |
487 | /* Compare against table - hard matching here */ | |
488 | for (i = 0; i < ARRAY_SIZE(dummy_scales); i++) | |
489 | if (val == dummy_scales[i].val && | |
490 | val2 == dummy_scales[i].val2) | |
491 | break; | |
492 | if (i == ARRAY_SIZE(dummy_scales)) | |
493 | ret = -EINVAL; | |
494 | else | |
495 | st->accel_calibscale = &dummy_scales[i]; | |
496 | mutex_unlock(&st->lock); | |
497 | return ret; | |
ff3fc8eb JF |
498 | case IIO_CHAN_INFO_CALIBBIAS: |
499 | mutex_lock(&st->lock); | |
500 | st->accel_calibbias = val; | |
501 | mutex_unlock(&st->lock); | |
502 | return 0; | |
3e34e650 DB |
503 | case IIO_CHAN_INFO_ENABLE: |
504 | switch (chan->type) { | |
505 | case IIO_STEPS: | |
506 | mutex_lock(&st->lock); | |
507 | st->steps_enabled = val; | |
508 | mutex_unlock(&st->lock); | |
509 | return 0; | |
510 | default: | |
511 | return -EINVAL; | |
512 | } | |
513 | case IIO_CHAN_INFO_CALIBHEIGHT: | |
514 | switch (chan->type) { | |
515 | case IIO_STEPS: | |
516 | st->height = val; | |
517 | return 0; | |
518 | default: | |
519 | return -EINVAL; | |
520 | } | |
ff3fc8eb | 521 | |
3a84331d JC |
522 | default: |
523 | return -EINVAL; | |
524 | } | |
525 | } | |
526 | ||
527 | /* | |
528 | * Device type specific information. | |
529 | */ | |
530 | static const struct iio_info iio_dummy_info = { | |
531 | .driver_module = THIS_MODULE, | |
532 | .read_raw = &iio_dummy_read_raw, | |
533 | .write_raw = &iio_dummy_write_raw, | |
e6477000 | 534 | #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS |
cb955852 LPC |
535 | .read_event_config = &iio_simple_dummy_read_event_config, |
536 | .write_event_config = &iio_simple_dummy_write_event_config, | |
537 | .read_event_value = &iio_simple_dummy_read_event_value, | |
538 | .write_event_value = &iio_simple_dummy_write_event_value, | |
e6477000 | 539 | #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */ |
3a84331d JC |
540 | }; |
541 | ||
542 | /** | |
543 | * iio_dummy_init_device() - device instance specific init | |
544 | * @indio_dev: the iio device structure | |
545 | * | |
546 | * Most drivers have one of these to set up default values, | |
547 | * reset the device to known state etc. | |
548 | */ | |
549 | static int iio_dummy_init_device(struct iio_dev *indio_dev) | |
550 | { | |
551 | struct iio_dummy_state *st = iio_priv(indio_dev); | |
552 | ||
553 | st->dac_val = 0; | |
554 | st->single_ended_adc_val = 73; | |
555 | st->differential_adc_val[0] = 33; | |
556 | st->differential_adc_val[1] = -34; | |
557 | st->accel_val = 34; | |
558 | st->accel_calibbias = -7; | |
559 | st->accel_calibscale = &dummy_scales[0]; | |
3e34e650 DB |
560 | st->steps = 47; |
561 | st->activity_running = 98; | |
562 | st->activity_walking = 4; | |
3a84331d JC |
563 | |
564 | return 0; | |
565 | } | |
566 | ||
567 | /** | |
568 | * iio_dummy_probe() - device instance probe | |
569 | * @index: an id number for this instance. | |
570 | * | |
571 | * Arguments are bus type specific. | |
572 | * I2C: iio_dummy_probe(struct i2c_client *client, | |
573 | * const struct i2c_device_id *id) | |
574 | * SPI: iio_dummy_probe(struct spi_device *spi) | |
575 | */ | |
4ae1c61f | 576 | static int iio_dummy_probe(int index) |
3a84331d JC |
577 | { |
578 | int ret; | |
579 | struct iio_dev *indio_dev; | |
580 | struct iio_dummy_state *st; | |
581 | ||
582 | /* | |
583 | * Allocate an IIO device. | |
584 | * | |
585 | * This structure contains all generic state | |
586 | * information about the device instance. | |
587 | * It also has a region (accessed by iio_priv() | |
588 | * for chip specific state information. | |
589 | */ | |
7cbb7537 | 590 | indio_dev = iio_device_alloc(sizeof(*st)); |
8f94c31f | 591 | if (!indio_dev) { |
3a84331d JC |
592 | ret = -ENOMEM; |
593 | goto error_ret; | |
594 | } | |
595 | ||
596 | st = iio_priv(indio_dev); | |
597 | mutex_init(&st->lock); | |
598 | ||
599 | iio_dummy_init_device(indio_dev); | |
600 | /* | |
601 | * With hardware: Set the parent device. | |
602 | * indio_dev->dev.parent = &spi->dev; | |
603 | * indio_dev->dev.parent = &client->dev; | |
604 | */ | |
605 | ||
606 | /* | |
607 | * Make the iio_dev struct available to remove function. | |
608 | * Bus equivalents | |
609 | * i2c_set_clientdata(client, indio_dev); | |
610 | * spi_set_drvdata(spi, indio_dev); | |
611 | */ | |
612 | iio_dummy_devs[index] = indio_dev; | |
613 | ||
614 | ||
615 | /* | |
616 | * Set the device name. | |
617 | * | |
618 | * This is typically a part number and obtained from the module | |
619 | * id table. | |
620 | * e.g. for i2c and spi: | |
621 | * indio_dev->name = id->name; | |
622 | * indio_dev->name = spi_get_device_id(spi)->name; | |
623 | */ | |
624 | indio_dev->name = iio_dummy_part_number; | |
625 | ||
626 | /* Provide description of available channels */ | |
627 | indio_dev->channels = iio_dummy_channels; | |
628 | indio_dev->num_channels = ARRAY_SIZE(iio_dummy_channels); | |
629 | ||
630 | /* | |
631 | * Provide device type specific interface functions and | |
632 | * constant data. | |
633 | */ | |
634 | indio_dev->info = &iio_dummy_info; | |
635 | ||
636 | /* Specify that device provides sysfs type interfaces */ | |
637 | indio_dev->modes = INDIO_DIRECT_MODE; | |
638 | ||
e6477000 | 639 | ret = iio_simple_dummy_events_register(indio_dev); |
3a84331d JC |
640 | if (ret < 0) |
641 | goto error_free_device; | |
9ad2e2e1 | 642 | |
4ae03019 | 643 | ret = iio_simple_dummy_configure_buffer(indio_dev); |
9ad2e2e1 | 644 | if (ret < 0) |
3fff2274 | 645 | goto error_unregister_events; |
9ad2e2e1 JC |
646 | |
647 | ret = iio_device_register(indio_dev); | |
648 | if (ret < 0) | |
3fff2274 | 649 | goto error_unconfigure_buffer; |
9ad2e2e1 | 650 | |
3a84331d | 651 | return 0; |
9ad2e2e1 JC |
652 | error_unconfigure_buffer: |
653 | iio_simple_dummy_unconfigure_buffer(indio_dev); | |
e6477000 JC |
654 | error_unregister_events: |
655 | iio_simple_dummy_events_unregister(indio_dev); | |
3a84331d | 656 | error_free_device: |
7cbb7537 | 657 | iio_device_free(indio_dev); |
3a84331d JC |
658 | error_ret: |
659 | return ret; | |
660 | } | |
661 | ||
662 | /** | |
663 | * iio_dummy_remove() - device instance removal function | |
664 | * @index: device index. | |
665 | * | |
666 | * Parameters follow those of iio_dummy_probe for buses. | |
667 | */ | |
668 | static int iio_dummy_remove(int index) | |
669 | { | |
e6477000 | 670 | int ret; |
3a84331d JC |
671 | /* |
672 | * Get a pointer to the device instance iio_dev structure | |
673 | * from the bus subsystem. E.g. | |
674 | * struct iio_dev *indio_dev = i2c_get_clientdata(client); | |
675 | * struct iio_dev *indio_dev = spi_get_drvdata(spi); | |
676 | */ | |
677 | struct iio_dev *indio_dev = iio_dummy_devs[index]; | |
678 | ||
e6477000 | 679 | |
3a84331d JC |
680 | /* Unregister the device */ |
681 | iio_device_unregister(indio_dev); | |
682 | ||
683 | /* Device specific code to power down etc */ | |
684 | ||
9ad2e2e1 | 685 | /* Buffered capture related cleanup */ |
9ad2e2e1 | 686 | iio_simple_dummy_unconfigure_buffer(indio_dev); |
e6477000 JC |
687 | |
688 | ret = iio_simple_dummy_events_unregister(indio_dev); | |
689 | if (ret) | |
690 | goto error_ret; | |
691 | ||
3a84331d | 692 | /* Free all structures */ |
7cbb7537 | 693 | iio_device_free(indio_dev); |
3a84331d | 694 | |
e6477000 JC |
695 | error_ret: |
696 | return ret; | |
3a84331d JC |
697 | } |
698 | ||
699 | /** | |
700 | * iio_dummy_init() - device driver registration | |
701 | * | |
702 | * Varies depending on bus type of the device. As there is no device | |
703 | * here, call probe directly. For information on device registration | |
704 | * i2c: | |
705 | * Documentation/i2c/writing-clients | |
706 | * spi: | |
707 | * Documentation/spi/spi-summary | |
708 | */ | |
709 | static __init int iio_dummy_init(void) | |
710 | { | |
711 | int i, ret; | |
aff89455 | 712 | |
3a84331d JC |
713 | if (instances > 10) { |
714 | instances = 1; | |
715 | return -EINVAL; | |
716 | } | |
3fff2274 | 717 | |
3a84331d | 718 | /* Fake a bus */ |
d83fb184 TM |
719 | iio_dummy_devs = kcalloc(instances, sizeof(*iio_dummy_devs), |
720 | GFP_KERNEL); | |
3a84331d JC |
721 | /* Here we have no actual device so call probe */ |
722 | for (i = 0; i < instances; i++) { | |
723 | ret = iio_dummy_probe(i); | |
724 | if (ret < 0) | |
725 | return ret; | |
726 | } | |
727 | return 0; | |
728 | } | |
729 | module_init(iio_dummy_init); | |
730 | ||
731 | /** | |
732 | * iio_dummy_exit() - device driver removal | |
733 | * | |
734 | * Varies depending on bus type of the device. | |
735 | * As there is no device here, call remove directly. | |
736 | */ | |
737 | static __exit void iio_dummy_exit(void) | |
738 | { | |
739 | int i; | |
aff89455 | 740 | |
3a84331d JC |
741 | for (i = 0; i < instances; i++) |
742 | iio_dummy_remove(i); | |
743 | kfree(iio_dummy_devs); | |
744 | } | |
745 | module_exit(iio_dummy_exit); | |
746 | ||
0f8c9620 | 747 | MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); |
3a84331d JC |
748 | MODULE_DESCRIPTION("IIO dummy driver"); |
749 | MODULE_LICENSE("GPL v2"); |