1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/kernel.h>
18#include <linux/slab.h>
19#include <linux/module.h>
20#include <linux/string.h>
21
22#include <linux/iio/iio.h>
23#include <linux/iio/sysfs.h>
24#include <linux/iio/events.h>
25#include <linux/iio/buffer.h>
26#include <linux/iio/sw_device.h>
27#include "iio_simple_dummy.h"
28
29static const struct config_item_type iio_dummy_type = {
30 .ct_owner = THIS_MODULE,
31};
32
33
34
35
36
37
38
39struct iio_dummy_accel_calibscale {
40 int val;
41 int val2;
42 int regval;
43};
44
45static const struct iio_dummy_accel_calibscale dummy_scales[] = {
46 { 0, 100, 0x8 },
47 { 0, 133, 0x7 },
48 { 733, 13, 0x9 },
49};
50
51#ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
52
53
54
55
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
63
64
65
66static const struct iio_event_spec step_detect_event = {
67 .type = IIO_EV_TYPE_CHANGE,
68 .dir = IIO_EV_DIR_NONE,
69 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
70};
71
72
73
74
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
84
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};
91#endif
92
93
94
95
96
97
98
99static const struct iio_chan_spec iio_dummy_channels[] = {
100
101 {
102 .type = IIO_VOLTAGE,
103
104 .indexed = 1,
105 .channel = 0,
106
107 .info_mask_separate =
108
109
110
111
112
113 BIT(IIO_CHAN_INFO_RAW) |
114
115
116
117
118
119 BIT(IIO_CHAN_INFO_OFFSET) |
120
121
122
123
124
125 BIT(IIO_CHAN_INFO_SCALE),
126
127
128
129
130 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
131
132 .scan_index = DUMMY_INDEX_VOLTAGE_0,
133 .scan_type = {
134 .sign = 'u',
135 .realbits = 13,
136 .storagebits = 16,
137 .shift = 0,
138 },
139#ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
140 .event_spec = &iio_dummy_event,
141 .num_event_specs = 1,
142#endif
143 },
144
145 {
146 .type = IIO_VOLTAGE,
147 .differential = 1,
148
149
150
151
152 .indexed = 1,
153 .channel = 1,
154 .channel2 = 2,
155
156
157
158
159
160 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
161
162
163
164
165
166 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
167
168
169
170
171 .scan_index = DUMMY_INDEX_DIFFVOLTAGE_1M2,
172 .scan_type = {
173 .sign = 's',
174 .realbits = 12,
175 .storagebits = 16,
176 .shift = 0,
177 },
178 },
179
180 {
181 .type = IIO_VOLTAGE,
182 .differential = 1,
183 .indexed = 1,
184 .channel = 3,
185 .channel2 = 4,
186 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
187 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
188 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
189 .scan_index = DUMMY_INDEX_DIFFVOLTAGE_3M4,
190 .scan_type = {
191 .sign = 's',
192 .realbits = 11,
193 .storagebits = 16,
194 .shift = 0,
195 },
196 },
197
198
199
200
201 {
202 .type = IIO_ACCEL,
203 .modified = 1,
204
205 .channel2 = IIO_MOD_X,
206 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
207
208
209
210
211
212
213 BIT(IIO_CHAN_INFO_CALIBSCALE) |
214 BIT(IIO_CHAN_INFO_CALIBBIAS),
215 .info_mask_shared_by_dir = BIT(IIO_CHAN_INFO_SAMP_FREQ),
216 .scan_index = DUMMY_INDEX_ACCELX,
217 .scan_type = {
218 .sign = 's',
219 .realbits = 16,
220 .storagebits = 16,
221 .shift = 0,
222 },
223 },
224
225
226
227
228 IIO_CHAN_SOFT_TIMESTAMP(4),
229
230 {
231 .type = IIO_VOLTAGE,
232 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
233 .scan_index = -1,
234 .output = 1,
235 .indexed = 1,
236 .channel = 0,
237 },
238 {
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),
243 .scan_index = -1,
244#ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
245 .event_spec = &step_detect_event,
246 .num_event_specs = 1,
247#endif
248 },
249 {
250 .type = IIO_ACTIVITY,
251 .modified = 1,
252 .channel2 = IIO_MOD_RUNNING,
253 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
254 .scan_index = -1,
255#ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
256 .event_spec = &iio_running_event,
257 .num_event_specs = 1,
258#endif
259 },
260 {
261 .type = IIO_ACTIVITY,
262 .modified = 1,
263 .channel2 = IIO_MOD_WALKING,
264 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
265 .scan_index = -1,
266#ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
267 .event_spec = &iio_walking_event,
268 .num_event_specs = 1,
269#endif
270 },
271};
272
273
274
275
276
277
278
279
280
281
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) {
293 case IIO_CHAN_INFO_RAW:
294 switch (chan->type) {
295 case IIO_VOLTAGE:
296 if (chan->output) {
297
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;
319 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;
343 case IIO_CHAN_INFO_OFFSET:
344
345 *val = 7;
346 ret = IIO_VAL_INT;
347 break;
348 case IIO_CHAN_INFO_SCALE:
349 switch (chan->type) {
350 case IIO_VOLTAGE:
351 switch (chan->differential) {
352 case 0:
353
354 *val = 0;
355 *val2 = 1333;
356 ret = IIO_VAL_INT_PLUS_MICRO;
357 break;
358 case 1:
359
360 *val = 0;
361 *val2 = 1344;
362 ret = IIO_VAL_INT_PLUS_NANO;
363 }
364 break;
365 default:
366 break;
367 }
368 break;
369 case IIO_CHAN_INFO_CALIBBIAS:
370
371 *val = st->accel_calibbias;
372 ret = IIO_VAL_INT;
373 break;
374 case IIO_CHAN_INFO_CALIBSCALE:
375 *val = st->accel_calibscale->val;
376 *val2 = st->accel_calibscale->val2;
377 ret = IIO_VAL_INT_PLUS_MICRO;
378 break;
379 case IIO_CHAN_INFO_SAMP_FREQ:
380 *val = 3;
381 *val2 = 33;
382 ret = IIO_VAL_INT_PLUS_NANO;
383 break;
384 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
405 default:
406 break;
407 }
408 mutex_unlock(&st->lock);
409 return ret;
410}
411
412
413
414
415
416
417
418
419
420
421
422
423
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) {
436 case IIO_CHAN_INFO_RAW:
437 switch (chan->type) {
438 case IIO_VOLTAGE:
439 if (chan->output == 0)
440 return -EINVAL;
441
442
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 }
476 case IIO_CHAN_INFO_CALIBSCALE:
477 mutex_lock(&st->lock);
478
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;
489 case IIO_CHAN_INFO_CALIBBIAS:
490 mutex_lock(&st->lock);
491 st->accel_calibbias = val;
492 mutex_unlock(&st->lock);
493 return 0;
494 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 }
512
513 default:
514 return -EINVAL;
515 }
516}
517
518
519
520
521static const struct iio_info iio_dummy_info = {
522 .read_raw = &iio_dummy_read_raw,
523 .write_raw = &iio_dummy_write_raw,
524#ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
525 .read_event_config = &iio_simple_dummy_read_event_config,
526 .write_event_config = &iio_simple_dummy_write_event_config,
527 .read_event_value = &iio_simple_dummy_read_event_value,
528 .write_event_value = &iio_simple_dummy_write_event_value,
529#endif
530};
531
532
533
534
535
536
537
538
539static int iio_dummy_init_device(struct iio_dev *indio_dev)
540{
541 struct iio_dummy_state *st = iio_priv(indio_dev);
542
543 st->dac_val = 0;
544 st->single_ended_adc_val = 73;
545 st->differential_adc_val[0] = 33;
546 st->differential_adc_val[1] = -34;
547 st->accel_val = 34;
548 st->accel_calibbias = -7;
549 st->accel_calibscale = &dummy_scales[0];
550 st->steps = 47;
551 st->activity_running = 98;
552 st->activity_walking = 4;
553
554 return 0;
555}
556
557
558
559
560
561
562
563
564
565
566static struct iio_sw_device *iio_dummy_probe(const char *name)
567{
568 int ret;
569 struct iio_dev *indio_dev;
570 struct iio_dummy_state *st;
571 struct iio_sw_device *swd;
572
573 swd = kzalloc(sizeof(*swd), GFP_KERNEL);
574 if (!swd) {
575 ret = -ENOMEM;
576 goto error_kzalloc;
577 }
578
579
580
581
582
583
584
585
586 indio_dev = iio_device_alloc(sizeof(*st));
587 if (!indio_dev) {
588 ret = -ENOMEM;
589 goto error_ret;
590 }
591
592 st = iio_priv(indio_dev);
593 mutex_init(&st->lock);
594
595 iio_dummy_init_device(indio_dev);
596
597
598
599
600
601
602
603
604
605
606
607
608 swd->device = indio_dev;
609
610
611
612
613
614
615
616
617
618
619 indio_dev->name = kstrdup(name, GFP_KERNEL);
620
621
622 indio_dev->channels = iio_dummy_channels;
623 indio_dev->num_channels = ARRAY_SIZE(iio_dummy_channels);
624
625
626
627
628
629 indio_dev->info = &iio_dummy_info;
630
631
632 indio_dev->modes = INDIO_DIRECT_MODE;
633
634 ret = iio_simple_dummy_events_register(indio_dev);
635 if (ret < 0)
636 goto error_free_device;
637
638 ret = iio_simple_dummy_configure_buffer(indio_dev);
639 if (ret < 0)
640 goto error_unregister_events;
641
642 ret = iio_device_register(indio_dev);
643 if (ret < 0)
644 goto error_unconfigure_buffer;
645
646 iio_swd_group_init_type_name(swd, name, &iio_dummy_type);
647
648 return swd;
649error_unconfigure_buffer:
650 iio_simple_dummy_unconfigure_buffer(indio_dev);
651error_unregister_events:
652 iio_simple_dummy_events_unregister(indio_dev);
653error_free_device:
654 iio_device_free(indio_dev);
655error_ret:
656 kfree(swd);
657error_kzalloc:
658 return ERR_PTR(ret);
659}
660
661
662
663
664
665
666
667static int iio_dummy_remove(struct iio_sw_device *swd)
668{
669
670
671
672
673
674
675 struct iio_dev *indio_dev = swd->device;
676
677
678 iio_device_unregister(indio_dev);
679
680
681
682
683 iio_simple_dummy_unconfigure_buffer(indio_dev);
684
685 iio_simple_dummy_events_unregister(indio_dev);
686
687
688 kfree(indio_dev->name);
689 iio_device_free(indio_dev);
690
691 return 0;
692}
693
694
695
696
697
698
699
700
701
702
703static const struct iio_sw_device_ops iio_dummy_device_ops = {
704 .probe = iio_dummy_probe,
705 .remove = iio_dummy_remove,
706};
707
708static struct iio_sw_device_type iio_dummy_device = {
709 .name = "dummy",
710 .owner = THIS_MODULE,
711 .ops = &iio_dummy_device_ops,
712};
713
714module_iio_sw_device_driver(iio_dummy_device);
715
716MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
717MODULE_DESCRIPTION("IIO dummy driver");
718MODULE_LICENSE("GPL v2");
719