1 | // SPDX-License-Identifier: GPL-2.0-only |
2 | /* |
3 | * Copyright (c) 2011 Jonathan Cameron |
4 | * |
5 | * A reference industrial I/O driver to illustrate the functionality available. |
6 | * |
7 | * There are numerous real drivers to illustrate the finer points. |
8 | * The purpose of this driver is to provide a driver with far more comments |
9 | * and explanatory notes than any 'real' driver would have. |
10 | * Anyone starting out writing an IIO driver should first make sure they |
11 | * understand all of this driver except those bits specifically marked |
12 | * as being present to allow us to 'fake' the presence of hardware. |
13 | */ |
14 | #include <linux/kernel.h> |
15 | #include <linux/slab.h> |
16 | #include <linux/module.h> |
17 | #include <linux/string.h> |
18 | |
19 | #include <linux/iio/iio.h> |
20 | #include <linux/iio/sysfs.h> |
21 | #include <linux/iio/events.h> |
22 | #include <linux/iio/buffer.h> |
23 | #include <linux/iio/sw_device.h> |
24 | #include "iio_simple_dummy.h" |
25 | |
26 | static const struct config_item_type iio_dummy_type = { |
27 | .ct_owner = THIS_MODULE, |
28 | }; |
29 | |
30 | /** |
31 | * struct iio_dummy_accel_calibscale - realworld to register mapping |
32 | * @val: first value in read_raw - here integer part. |
33 | * @val2: second value in read_raw etc - here micro part. |
34 | * @regval: register value - magic device specific numbers. |
35 | */ |
36 | struct iio_dummy_accel_calibscale { |
37 | int val; |
38 | int val2; |
39 | int regval; /* what would be written to hardware */ |
40 | }; |
41 | |
42 | static const struct iio_dummy_accel_calibscale dummy_scales[] = { |
43 | { 0, 100, 0x8 }, /* 0.000100 */ |
44 | { 0, 133, 0x7 }, /* 0.000133 */ |
45 | { 733, 13, 0x9 }, /* 733.000013 */ |
46 | }; |
47 | |
48 | #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS |
49 | |
50 | /* |
51 | * simple event - triggered when value rises above |
52 | * a threshold |
53 | */ |
54 | static const struct iio_event_spec iio_dummy_event = { |
55 | .type = IIO_EV_TYPE_THRESH, |
56 | .dir = IIO_EV_DIR_RISING, |
57 | .mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE), |
58 | }; |
59 | |
60 | /* |
61 | * simple step detect event - triggered when a step is detected |
62 | */ |
63 | static const struct iio_event_spec step_detect_event = { |
64 | .type = IIO_EV_TYPE_CHANGE, |
65 | .dir = IIO_EV_DIR_NONE, |
66 | .mask_separate = BIT(IIO_EV_INFO_ENABLE), |
67 | }; |
68 | |
69 | /* |
70 | * simple transition event - triggered when the reported running confidence |
71 | * value rises above a threshold value |
72 | */ |
73 | static const struct iio_event_spec iio_running_event = { |
74 | .type = IIO_EV_TYPE_THRESH, |
75 | .dir = IIO_EV_DIR_RISING, |
76 | .mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE), |
77 | }; |
78 | |
79 | /* |
80 | * simple transition event - triggered when the reported walking confidence |
81 | * value falls under a threshold value |
82 | */ |
83 | static const struct iio_event_spec iio_walking_event = { |
84 | .type = IIO_EV_TYPE_THRESH, |
85 | .dir = IIO_EV_DIR_FALLING, |
86 | .mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE), |
87 | }; |
88 | #endif |
89 | |
90 | /* |
91 | * iio_dummy_channels - Description of available channels |
92 | * |
93 | * This array of structures tells the IIO core about what the device |
94 | * actually provides for a given channel. |
95 | */ |
96 | static const struct iio_chan_spec iio_dummy_channels[] = { |
97 | /* indexed ADC channel in_voltage0_raw etc */ |
98 | { |
99 | .type = IIO_VOLTAGE, |
100 | /* Channel has a numeric index of 0 */ |
101 | .indexed = 1, |
102 | .channel = 0, |
103 | /* What other information is available? */ |
104 | .info_mask_separate = |
105 | /* |
106 | * in_voltage0_raw |
107 | * Raw (unscaled no bias removal etc) measurement |
108 | * from the device. |
109 | */ |
110 | BIT(IIO_CHAN_INFO_RAW) | |
111 | /* |
112 | * in_voltage0_offset |
113 | * Offset for userspace to apply prior to scale |
114 | * when converting to standard units (microvolts) |
115 | */ |
116 | BIT(IIO_CHAN_INFO_OFFSET) | |
117 | /* |
118 | * in_voltage0_scale |
119 | * Multipler for userspace to apply post offset |
120 | * when converting to standard units (microvolts) |
121 | */ |
122 | BIT(IIO_CHAN_INFO_SCALE), |
123 | /* |
124 | * sampling_frequency |
125 | * The frequency in Hz at which the channels are sampled |
126 | */ |
127 | .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ), |
128 | /* The ordering of elements in the buffer via an enum */ |
129 | .scan_index = DUMMY_INDEX_VOLTAGE_0, |
130 | .scan_type = { /* Description of storage in buffer */ |
131 | .sign = 'u', /* unsigned */ |
132 | .realbits = 13, /* 13 bits */ |
133 | .storagebits = 16, /* 16 bits used for storage */ |
134 | .shift = 0, /* zero shift */ |
135 | }, |
136 | #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS |
137 | .event_spec = &iio_dummy_event, |
138 | .num_event_specs = 1, |
139 | #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */ |
140 | }, |
141 | /* Differential ADC channel in_voltage1-voltage2_raw etc*/ |
142 | { |
143 | .type = IIO_VOLTAGE, |
144 | .differential = 1, |
145 | /* |
146 | * Indexing for differential channels uses channel |
147 | * for the positive part, channel2 for the negative. |
148 | */ |
149 | .indexed = 1, |
150 | .channel = 1, |
151 | .channel2 = 2, |
152 | /* |
153 | * in_voltage1-voltage2_raw |
154 | * Raw (unscaled no bias removal etc) measurement |
155 | * from the device. |
156 | */ |
157 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
158 | /* |
159 | * in_voltage-voltage_scale |
160 | * Shared version of scale - shared by differential |
161 | * input channels of type IIO_VOLTAGE. |
162 | */ |
163 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), |
164 | /* |
165 | * sampling_frequency |
166 | * The frequency in Hz at which the channels are sampled |
167 | */ |
168 | .scan_index = DUMMY_INDEX_DIFFVOLTAGE_1M2, |
169 | .scan_type = { /* Description of storage in buffer */ |
170 | .sign = 's', /* signed */ |
171 | .realbits = 12, /* 12 bits */ |
172 | .storagebits = 16, /* 16 bits used for storage */ |
173 | .shift = 0, /* zero shift */ |
174 | }, |
175 | }, |
176 | /* Differential ADC channel in_voltage3-voltage4_raw etc*/ |
177 | { |
178 | .type = IIO_VOLTAGE, |
179 | .differential = 1, |
180 | .indexed = 1, |
181 | .channel = 3, |
182 | .channel2 = 4, |
183 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
184 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), |
185 | .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ), |
186 | .scan_index = DUMMY_INDEX_DIFFVOLTAGE_3M4, |
187 | .scan_type = { |
188 | .sign = 's', |
189 | .realbits = 11, |
190 | .storagebits = 16, |
191 | .shift = 0, |
192 | }, |
193 | }, |
194 | /* |
195 | * 'modified' (i.e. axis specified) acceleration channel |
196 | * in_accel_z_raw |
197 | */ |
198 | { |
199 | .type = IIO_ACCEL, |
200 | .modified = 1, |
201 | /* Channel 2 is use for modifiers */ |
202 | .channel2 = IIO_MOD_X, |
203 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
204 | /* |
205 | * Internal bias and gain correction values. Applied |
206 | * by the hardware or driver prior to userspace |
207 | * seeing the readings. Typically part of hardware |
208 | * calibration. |
209 | */ |
210 | BIT(IIO_CHAN_INFO_CALIBSCALE) | |
211 | BIT(IIO_CHAN_INFO_CALIBBIAS), |
212 | .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ), |
213 | .scan_index = DUMMY_INDEX_ACCELX, |
214 | .scan_type = { /* Description of storage in buffer */ |
215 | .sign = 's', /* signed */ |
216 | .realbits = 16, /* 16 bits */ |
217 | .storagebits = 16, /* 16 bits used for storage */ |
218 | .shift = 0, /* zero shift */ |
219 | }, |
220 | }, |
221 | /* |
222 | * Convenience macro for timestamps. 4 is the index in |
223 | * the buffer. |
224 | */ |
225 | IIO_CHAN_SOFT_TIMESTAMP(4), |
226 | /* DAC channel out_voltage0_raw */ |
227 | { |
228 | .type = IIO_VOLTAGE, |
229 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
230 | .scan_index = -1, /* No buffer support */ |
231 | .output = 1, |
232 | .indexed = 1, |
233 | .channel = 0, |
234 | }, |
235 | { |
236 | .type = IIO_STEPS, |
237 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_ENABLE) | |
238 | BIT(IIO_CHAN_INFO_CALIBHEIGHT), |
239 | .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), |
240 | .scan_index = -1, /* No buffer support */ |
241 | #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS |
242 | .event_spec = &step_detect_event, |
243 | .num_event_specs = 1, |
244 | #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */ |
245 | }, |
246 | { |
247 | .type = IIO_ACTIVITY, |
248 | .modified = 1, |
249 | .channel2 = IIO_MOD_RUNNING, |
250 | .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), |
251 | .scan_index = -1, /* No buffer support */ |
252 | #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS |
253 | .event_spec = &iio_running_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_WALKING, |
261 | .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), |
262 | .scan_index = -1, /* No buffer support */ |
263 | #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS |
264 | .event_spec = &iio_walking_event, |
265 | .num_event_specs = 1, |
266 | #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */ |
267 | }, |
268 | }; |
269 | |
270 | /** |
271 | * iio_dummy_read_raw() - data read function. |
272 | * @indio_dev: the struct iio_dev associated with this device instance |
273 | * @chan: the channel whose data is to be read |
274 | * @val: first element of returned value (typically INT) |
275 | * @val2: second element of returned value (typically MICRO) |
276 | * @mask: what we actually want to read as per the info_mask_* |
277 | * in iio_chan_spec. |
278 | */ |
279 | static int iio_dummy_read_raw(struct iio_dev *indio_dev, |
280 | struct iio_chan_spec const *chan, |
281 | int *val, |
282 | int *val2, |
283 | long mask) |
284 | { |
285 | struct iio_dummy_state *st = iio_priv(indio_dev); |
286 | |
287 | switch (mask) { |
288 | case IIO_CHAN_INFO_RAW: /* magic value - channel value read */ |
289 | iio_device_claim_direct_scoped(return -EBUSY, indio_dev) { |
290 | guard(mutex)(T: &st->lock); |
291 | switch (chan->type) { |
292 | case IIO_VOLTAGE: |
293 | if (chan->output) { |
294 | /* Set integer part to cached value */ |
295 | *val = st->dac_val; |
296 | return IIO_VAL_INT; |
297 | } else if (chan->differential) { |
298 | if (chan->channel == 1) |
299 | *val = st->differential_adc_val[0]; |
300 | else |
301 | *val = st->differential_adc_val[1]; |
302 | return IIO_VAL_INT; |
303 | } else { |
304 | *val = st->single_ended_adc_val; |
305 | return IIO_VAL_INT; |
306 | } |
307 | |
308 | case IIO_ACCEL: |
309 | *val = st->accel_val; |
310 | return IIO_VAL_INT; |
311 | default: |
312 | return -EINVAL; |
313 | } |
314 | } |
315 | unreachable(); |
316 | case IIO_CHAN_INFO_PROCESSED: |
317 | iio_device_claim_direct_scoped(return -EBUSY, indio_dev) { |
318 | guard(mutex)(T: &st->lock); |
319 | switch (chan->type) { |
320 | case IIO_STEPS: |
321 | *val = st->steps; |
322 | return IIO_VAL_INT; |
323 | case IIO_ACTIVITY: |
324 | switch (chan->channel2) { |
325 | case IIO_MOD_RUNNING: |
326 | *val = st->activity_running; |
327 | return IIO_VAL_INT; |
328 | case IIO_MOD_WALKING: |
329 | *val = st->activity_walking; |
330 | return IIO_VAL_INT; |
331 | default: |
332 | return -EINVAL; |
333 | } |
334 | default: |
335 | return -EINVAL; |
336 | } |
337 | } |
338 | unreachable(); |
339 | case IIO_CHAN_INFO_OFFSET: |
340 | /* only single ended adc -> 7 */ |
341 | *val = 7; |
342 | return IIO_VAL_INT; |
343 | case IIO_CHAN_INFO_SCALE: |
344 | switch (chan->type) { |
345 | case IIO_VOLTAGE: |
346 | switch (chan->differential) { |
347 | case 0: |
348 | /* only single ended adc -> 0.001333 */ |
349 | *val = 0; |
350 | *val2 = 1333; |
351 | return IIO_VAL_INT_PLUS_MICRO; |
352 | case 1: |
353 | /* all differential adc -> 0.000001344 */ |
354 | *val = 0; |
355 | *val2 = 1344; |
356 | return IIO_VAL_INT_PLUS_NANO; |
357 | default: |
358 | return -EINVAL; |
359 | } |
360 | default: |
361 | return -EINVAL; |
362 | } |
363 | case IIO_CHAN_INFO_CALIBBIAS: { |
364 | guard(mutex)(T: &st->lock); |
365 | /* only the acceleration axis - read from cache */ |
366 | *val = st->accel_calibbias; |
367 | return IIO_VAL_INT; |
368 | } |
369 | case IIO_CHAN_INFO_CALIBSCALE: { |
370 | guard(mutex)(T: &st->lock); |
371 | *val = st->accel_calibscale->val; |
372 | *val2 = st->accel_calibscale->val2; |
373 | return IIO_VAL_INT_PLUS_MICRO; |
374 | } |
375 | case IIO_CHAN_INFO_SAMP_FREQ: |
376 | *val = 3; |
377 | *val2 = 33; |
378 | return IIO_VAL_INT_PLUS_NANO; |
379 | case IIO_CHAN_INFO_ENABLE: { |
380 | guard(mutex)(T: &st->lock); |
381 | switch (chan->type) { |
382 | case IIO_STEPS: |
383 | *val = st->steps_enabled; |
384 | return IIO_VAL_INT; |
385 | default: |
386 | return -EINVAL; |
387 | } |
388 | } |
389 | case IIO_CHAN_INFO_CALIBHEIGHT: { |
390 | guard(mutex)(T: &st->lock); |
391 | switch (chan->type) { |
392 | case IIO_STEPS: |
393 | *val = st->height; |
394 | return IIO_VAL_INT; |
395 | default: |
396 | return -EINVAL; |
397 | } |
398 | } |
399 | default: |
400 | return -EINVAL; |
401 | } |
402 | } |
403 | |
404 | /** |
405 | * iio_dummy_write_raw() - data write function. |
406 | * @indio_dev: the struct iio_dev associated with this device instance |
407 | * @chan: the channel whose data is to be written |
408 | * @val: first element of value to set (typically INT) |
409 | * @val2: second element of value to set (typically MICRO) |
410 | * @mask: what we actually want to write as per the info_mask_* |
411 | * in iio_chan_spec. |
412 | * |
413 | * Note that all raw writes are assumed IIO_VAL_INT and info mask elements |
414 | * are assumed to be IIO_INT_PLUS_MICRO unless the callback write_raw_get_fmt |
415 | * in struct iio_info is provided by the driver. |
416 | */ |
417 | static int iio_dummy_write_raw(struct iio_dev *indio_dev, |
418 | struct iio_chan_spec const *chan, |
419 | int val, |
420 | int val2, |
421 | long mask) |
422 | { |
423 | int i; |
424 | struct iio_dummy_state *st = iio_priv(indio_dev); |
425 | |
426 | switch (mask) { |
427 | case IIO_CHAN_INFO_RAW: |
428 | switch (chan->type) { |
429 | case IIO_VOLTAGE: |
430 | if (chan->output == 0) |
431 | return -EINVAL; |
432 | |
433 | scoped_guard(mutex, &st->lock) { |
434 | /* Locking not required as writing single value */ |
435 | st->dac_val = val; |
436 | } |
437 | return 0; |
438 | default: |
439 | return -EINVAL; |
440 | } |
441 | case IIO_CHAN_INFO_PROCESSED: |
442 | switch (chan->type) { |
443 | case IIO_STEPS: |
444 | scoped_guard(mutex, &st->lock) { |
445 | st->steps = val; |
446 | } |
447 | return 0; |
448 | case IIO_ACTIVITY: |
449 | if (val < 0) |
450 | val = 0; |
451 | if (val > 100) |
452 | val = 100; |
453 | switch (chan->channel2) { |
454 | case IIO_MOD_RUNNING: |
455 | st->activity_running = val; |
456 | return 0; |
457 | case IIO_MOD_WALKING: |
458 | st->activity_walking = val; |
459 | return 0; |
460 | default: |
461 | return -EINVAL; |
462 | } |
463 | break; |
464 | default: |
465 | return -EINVAL; |
466 | } |
467 | case IIO_CHAN_INFO_CALIBSCALE: { |
468 | guard(mutex)(T: &st->lock); |
469 | /* Compare against table - hard matching here */ |
470 | for (i = 0; i < ARRAY_SIZE(dummy_scales); i++) |
471 | if (val == dummy_scales[i].val && |
472 | val2 == dummy_scales[i].val2) |
473 | break; |
474 | if (i == ARRAY_SIZE(dummy_scales)) |
475 | return -EINVAL; |
476 | st->accel_calibscale = &dummy_scales[i]; |
477 | return 0; |
478 | } |
479 | case IIO_CHAN_INFO_CALIBBIAS: |
480 | scoped_guard(mutex, &st->lock) { |
481 | st->accel_calibbias = val; |
482 | } |
483 | return 0; |
484 | case IIO_CHAN_INFO_ENABLE: |
485 | switch (chan->type) { |
486 | case IIO_STEPS: |
487 | scoped_guard(mutex, &st->lock) { |
488 | st->steps_enabled = val; |
489 | } |
490 | return 0; |
491 | default: |
492 | return -EINVAL; |
493 | } |
494 | case IIO_CHAN_INFO_CALIBHEIGHT: |
495 | switch (chan->type) { |
496 | case IIO_STEPS: |
497 | st->height = val; |
498 | return 0; |
499 | default: |
500 | return -EINVAL; |
501 | } |
502 | |
503 | default: |
504 | return -EINVAL; |
505 | } |
506 | } |
507 | |
508 | /* |
509 | * Device type specific information. |
510 | */ |
511 | static const struct iio_info iio_dummy_info = { |
512 | .read_raw = &iio_dummy_read_raw, |
513 | .write_raw = &iio_dummy_write_raw, |
514 | #ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS |
515 | .read_event_config = &iio_simple_dummy_read_event_config, |
516 | .write_event_config = &iio_simple_dummy_write_event_config, |
517 | .read_event_value = &iio_simple_dummy_read_event_value, |
518 | .write_event_value = &iio_simple_dummy_write_event_value, |
519 | #endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */ |
520 | }; |
521 | |
522 | /** |
523 | * iio_dummy_init_device() - device instance specific init |
524 | * @indio_dev: the iio device structure |
525 | * |
526 | * Most drivers have one of these to set up default values, |
527 | * reset the device to known state etc. |
528 | */ |
529 | static int iio_dummy_init_device(struct iio_dev *indio_dev) |
530 | { |
531 | struct iio_dummy_state *st = iio_priv(indio_dev); |
532 | |
533 | st->dac_val = 0; |
534 | st->single_ended_adc_val = 73; |
535 | st->differential_adc_val[0] = 33; |
536 | st->differential_adc_val[1] = -34; |
537 | st->accel_val = 34; |
538 | st->accel_calibbias = -7; |
539 | st->accel_calibscale = &dummy_scales[0]; |
540 | st->steps = 47; |
541 | st->activity_running = 98; |
542 | st->activity_walking = 4; |
543 | |
544 | return 0; |
545 | } |
546 | |
547 | /** |
548 | * iio_dummy_probe() - device instance probe |
549 | * @name: name of this instance. |
550 | * |
551 | * Arguments are bus type specific. |
552 | * I2C: iio_dummy_probe(struct i2c_client *client, |
553 | * const struct i2c_device_id *id) |
554 | * SPI: iio_dummy_probe(struct spi_device *spi) |
555 | */ |
556 | static struct iio_sw_device *iio_dummy_probe(const char *name) |
557 | { |
558 | int ret; |
559 | struct iio_dev *indio_dev; |
560 | struct iio_dummy_state *st; |
561 | struct iio_sw_device *swd; |
562 | struct device *parent = NULL; |
563 | |
564 | /* |
565 | * With hardware: Set the parent device. |
566 | * parent = &spi->dev; |
567 | * parent = &client->dev; |
568 | */ |
569 | |
570 | swd = kzalloc(size: sizeof(*swd), GFP_KERNEL); |
571 | if (!swd) |
572 | return ERR_PTR(error: -ENOMEM); |
573 | |
574 | /* |
575 | * Allocate an IIO device. |
576 | * |
577 | * This structure contains all generic state |
578 | * information about the device instance. |
579 | * It also has a region (accessed by iio_priv() |
580 | * for chip specific state information. |
581 | */ |
582 | indio_dev = iio_device_alloc(parent, sizeof_priv: sizeof(*st)); |
583 | if (!indio_dev) { |
584 | ret = -ENOMEM; |
585 | goto error_free_swd; |
586 | } |
587 | |
588 | st = iio_priv(indio_dev); |
589 | mutex_init(&st->lock); |
590 | |
591 | iio_dummy_init_device(indio_dev); |
592 | |
593 | /* |
594 | * Make the iio_dev struct available to remove function. |
595 | * Bus equivalents |
596 | * i2c_set_clientdata(client, indio_dev); |
597 | * spi_set_drvdata(spi, indio_dev); |
598 | */ |
599 | swd->device = indio_dev; |
600 | |
601 | /* |
602 | * Set the device name. |
603 | * |
604 | * This is typically a part number and obtained from the module |
605 | * id table. |
606 | * e.g. for i2c and spi: |
607 | * indio_dev->name = id->name; |
608 | * indio_dev->name = spi_get_device_id(spi)->name; |
609 | */ |
610 | indio_dev->name = kstrdup(s: name, GFP_KERNEL); |
611 | if (!indio_dev->name) { |
612 | ret = -ENOMEM; |
613 | goto error_free_device; |
614 | } |
615 | |
616 | /* Provide description of available channels */ |
617 | indio_dev->channels = iio_dummy_channels; |
618 | indio_dev->num_channels = ARRAY_SIZE(iio_dummy_channels); |
619 | |
620 | /* |
621 | * Provide device type specific interface functions and |
622 | * constant data. |
623 | */ |
624 | indio_dev->info = &iio_dummy_info; |
625 | |
626 | /* Specify that device provides sysfs type interfaces */ |
627 | indio_dev->modes = INDIO_DIRECT_MODE; |
628 | |
629 | ret = iio_simple_dummy_events_register(indio_dev); |
630 | if (ret < 0) |
631 | goto error_free_name; |
632 | |
633 | ret = iio_simple_dummy_configure_buffer(indio_dev); |
634 | if (ret < 0) |
635 | goto error_unregister_events; |
636 | |
637 | ret = iio_device_register(indio_dev); |
638 | if (ret < 0) |
639 | goto error_unconfigure_buffer; |
640 | |
641 | iio_swd_group_init_type_name(d: swd, name, type: &iio_dummy_type); |
642 | |
643 | return swd; |
644 | error_unconfigure_buffer: |
645 | iio_simple_dummy_unconfigure_buffer(indio_dev); |
646 | error_unregister_events: |
647 | iio_simple_dummy_events_unregister(indio_dev); |
648 | error_free_name: |
649 | kfree(objp: indio_dev->name); |
650 | error_free_device: |
651 | iio_device_free(indio_dev); |
652 | error_free_swd: |
653 | kfree(objp: swd); |
654 | return ERR_PTR(error: ret); |
655 | } |
656 | |
657 | /** |
658 | * iio_dummy_remove() - device instance removal function |
659 | * @swd: pointer to software IIO device abstraction |
660 | * |
661 | * Parameters follow those of iio_dummy_probe for buses. |
662 | */ |
663 | static int iio_dummy_remove(struct iio_sw_device *swd) |
664 | { |
665 | /* |
666 | * Get a pointer to the device instance iio_dev structure |
667 | * from the bus subsystem. E.g. |
668 | * struct iio_dev *indio_dev = i2c_get_clientdata(client); |
669 | * struct iio_dev *indio_dev = spi_get_drvdata(spi); |
670 | */ |
671 | struct iio_dev *indio_dev = swd->device; |
672 | |
673 | /* Unregister the device */ |
674 | iio_device_unregister(indio_dev); |
675 | |
676 | /* Device specific code to power down etc */ |
677 | |
678 | /* Buffered capture related cleanup */ |
679 | iio_simple_dummy_unconfigure_buffer(indio_dev); |
680 | |
681 | iio_simple_dummy_events_unregister(indio_dev); |
682 | |
683 | /* Free all structures */ |
684 | kfree(objp: indio_dev->name); |
685 | iio_device_free(indio_dev); |
686 | |
687 | return 0; |
688 | } |
689 | |
690 | /* |
691 | * module_iio_sw_device_driver() - 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.rst |
697 | * spi: |
698 | * Documentation/spi/spi-summary.rst |
699 | */ |
700 | static const struct iio_sw_device_ops iio_dummy_device_ops = { |
701 | .probe = iio_dummy_probe, |
702 | .remove = iio_dummy_remove, |
703 | }; |
704 | |
705 | static struct iio_sw_device_type iio_dummy_device = { |
706 | .name = "dummy" , |
707 | .owner = THIS_MODULE, |
708 | .ops = &iio_dummy_device_ops, |
709 | }; |
710 | |
711 | module_iio_sw_device_driver(iio_dummy_device); |
712 | |
713 | MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>" ); |
714 | MODULE_DESCRIPTION("IIO dummy driver" ); |
715 | MODULE_LICENSE("GPL v2" ); |
716 | |