Skip to content

Commit 3e34e65

Browse files
Daniel Balutajic23
authored andcommitted
iio: dummy: Demonstrate the usage of new channel types
Adds support for the new channel types in the dummy driver: * a new channel IIO_ACTIVITY * two state transition events (running and walking) * a new channel IIO_STEPS and support for reading and writing pedometer step counter * step detect event * a new IIO_CHAN_INFO_CALIBHEIGHT mask bit for reading and writing user's height. Signed-off-by: Irina Tirdea <[email protected]> Signed-off-by: Daniel Baluta <[email protected]> Signed-off-by: Jonathan Cameron <[email protected]>
1 parent bcdf28f commit 3e34e65

File tree

3 files changed

+229
-18
lines changed

3 files changed

+229
-18
lines changed

drivers/staging/iio/iio_simple_dummy.c

Lines changed: 181 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,34 @@ static const struct iio_event_spec iio_dummy_event = {
6969
.mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE),
7070
};
7171

72+
/*
73+
* simple step detect event - triggered when a step is detected
74+
*/
75+
static const struct iio_event_spec step_detect_event = {
76+
.type = IIO_EV_TYPE_INSTANCE,
77+
.dir = IIO_EV_DIR_NONE,
78+
.mask_separate = BIT(IIO_EV_INFO_ENABLE),
79+
};
80+
81+
/*
82+
* simple transition event - triggered when the reported running confidence
83+
* value rises above a threshold value
84+
*/
85+
static const struct iio_event_spec iio_running_event = {
86+
.type = IIO_EV_TYPE_THRESH,
87+
.dir = IIO_EV_DIR_RISING,
88+
.mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE),
89+
};
90+
91+
/*
92+
* simple transition event - triggered when the reported walking confidence
93+
* value falls under a threshold value
94+
*/
95+
static const struct iio_event_spec iio_walking_event = {
96+
.type = IIO_EV_TYPE_THRESH,
97+
.dir = IIO_EV_DIR_FALLING,
98+
.mask_separate = BIT(IIO_EV_INFO_VALUE) | BIT(IIO_EV_INFO_ENABLE),
99+
};
72100
#endif
73101

74102
/*
@@ -215,6 +243,37 @@ static const struct iio_chan_spec iio_dummy_channels[] = {
215243
.indexed = 1,
216244
.channel = 0,
217245
},
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),
251+
.scan_index = -1,
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),
262+
#ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
263+
.event_spec = &iio_running_event,
264+
.num_event_specs = 1,
265+
#endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
266+
},
267+
{
268+
.type = IIO_ACTIVITY,
269+
.modified = 1,
270+
.channel2 = IIO_MOD_WALKING,
271+
.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
272+
#ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
273+
.event_spec = &iio_walking_event,
274+
.num_event_specs = 1,
275+
#endif /* CONFIG_IIO_SIMPLE_DUMMY_EVENTS */
276+
},
218277
};
219278

220279
/**
@@ -263,24 +322,55 @@ static int iio_dummy_read_raw(struct iio_dev *indio_dev,
263322
break;
264323
}
265324
break;
325+
case IIO_CHAN_INFO_PROCESSED:
326+
switch (chan->type) {
327+
case IIO_STEPS:
328+
*val = st->steps;
329+
ret = IIO_VAL_INT;
330+
break;
331+
case IIO_ACTIVITY:
332+
switch (chan->channel2) {
333+
case IIO_MOD_RUNNING:
334+
*val = st->activity_running;
335+
ret = IIO_VAL_INT;
336+
break;
337+
case IIO_MOD_WALKING:
338+
*val = st->activity_walking;
339+
ret = IIO_VAL_INT;
340+
break;
341+
default:
342+
break;
343+
}
344+
break;
345+
default:
346+
break;
347+
}
348+
break;
266349
case IIO_CHAN_INFO_OFFSET:
267350
/* only single ended adc -> 7 */
268351
*val = 7;
269352
ret = IIO_VAL_INT;
270353
break;
271354
case IIO_CHAN_INFO_SCALE:
272-
switch (chan->differential) {
273-
case 0:
274-
/* only single ended adc -> 0.001333 */
275-
*val = 0;
276-
*val2 = 1333;
277-
ret = IIO_VAL_INT_PLUS_MICRO;
355+
switch (chan->type) {
356+
case IIO_VOLTAGE:
357+
switch (chan->differential) {
358+
case 0:
359+
/* only single ended adc -> 0.001333 */
360+
*val = 0;
361+
*val2 = 1333;
362+
ret = IIO_VAL_INT_PLUS_MICRO;
363+
break;
364+
case 1:
365+
/* all differential adc channels ->
366+
* 0.000001344 */
367+
*val = 0;
368+
*val2 = 1344;
369+
ret = IIO_VAL_INT_PLUS_NANO;
370+
}
371+
break;
372+
default:
278373
break;
279-
case 1:
280-
/* all differential adc channels -> 0.000001344 */
281-
*val = 0;
282-
*val2 = 1344;
283-
ret = IIO_VAL_INT_PLUS_NANO;
284374
}
285375
break;
286376
case IIO_CHAN_INFO_CALIBBIAS:
@@ -298,6 +388,27 @@ static int iio_dummy_read_raw(struct iio_dev *indio_dev,
298388
*val2 = 33;
299389
ret = IIO_VAL_INT_PLUS_NANO;
300390
break;
391+
case IIO_CHAN_INFO_ENABLE:
392+
switch (chan->type) {
393+
case IIO_STEPS:
394+
*val = st->steps_enabled;
395+
ret = IIO_VAL_INT;
396+
break;
397+
default:
398+
break;
399+
}
400+
break;
401+
case IIO_CHAN_INFO_CALIBHEIGHT:
402+
switch (chan->type) {
403+
case IIO_STEPS:
404+
*val = st->height;
405+
ret = IIO_VAL_INT;
406+
break;
407+
default:
408+
break;
409+
}
410+
break;
411+
301412
default:
302413
break;
303414
}
@@ -330,14 +441,45 @@ static int iio_dummy_write_raw(struct iio_dev *indio_dev,
330441

331442
switch (mask) {
332443
case IIO_CHAN_INFO_RAW:
333-
if (chan->output == 0)
444+
switch (chan->type) {
445+
case IIO_VOLTAGE:
446+
if (chan->output == 0)
447+
return -EINVAL;
448+
449+
/* Locking not required as writing single value */
450+
mutex_lock(&st->lock);
451+
st->dac_val = val;
452+
mutex_unlock(&st->lock);
453+
return 0;
454+
default:
334455
return -EINVAL;
335-
336-
/* Locking not required as writing single value */
337-
mutex_lock(&st->lock);
338-
st->dac_val = val;
339-
mutex_unlock(&st->lock);
340-
return 0;
456+
}
457+
case IIO_CHAN_INFO_PROCESSED:
458+
switch (chan->type) {
459+
case IIO_STEPS:
460+
mutex_lock(&st->lock);
461+
st->steps = val;
462+
mutex_unlock(&st->lock);
463+
return 0;
464+
case IIO_ACTIVITY:
465+
if (val < 0)
466+
val = 0;
467+
if (val > 100)
468+
val = 100;
469+
switch (chan->channel2) {
470+
case IIO_MOD_RUNNING:
471+
st->activity_running = val;
472+
return 0;
473+
case IIO_MOD_WALKING:
474+
st->activity_walking = val;
475+
return 0;
476+
default:
477+
return -EINVAL;
478+
}
479+
break;
480+
default:
481+
return -EINVAL;
482+
}
341483
case IIO_CHAN_INFO_CALIBSCALE:
342484
mutex_lock(&st->lock);
343485
/* Compare against table - hard matching here */
@@ -356,6 +498,24 @@ static int iio_dummy_write_raw(struct iio_dev *indio_dev,
356498
st->accel_calibbias = val;
357499
mutex_unlock(&st->lock);
358500
return 0;
501+
case IIO_CHAN_INFO_ENABLE:
502+
switch (chan->type) {
503+
case IIO_STEPS:
504+
mutex_lock(&st->lock);
505+
st->steps_enabled = val;
506+
mutex_unlock(&st->lock);
507+
return 0;
508+
default:
509+
return -EINVAL;
510+
}
511+
case IIO_CHAN_INFO_CALIBHEIGHT:
512+
switch (chan->type) {
513+
case IIO_STEPS:
514+
st->height = val;
515+
return 0;
516+
default:
517+
return -EINVAL;
518+
}
359519

360520
default:
361521
return -EINVAL;
@@ -395,6 +555,9 @@ static int iio_dummy_init_device(struct iio_dev *indio_dev)
395555
st->accel_val = 34;
396556
st->accel_calibbias = -7;
397557
st->accel_calibscale = &dummy_scales[0];
558+
st->steps = 47;
559+
st->activity_running = 98;
560+
st->activity_walking = 4;
398561

399562
return 0;
400563
}

drivers/staging/iio/iio_simple_dummy.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,14 @@ struct iio_dummy_state {
3434
int differential_adc_val[2];
3535
int accel_val;
3636
int accel_calibbias;
37+
int activity_running;
38+
int activity_walking;
3739
const struct iio_dummy_accel_calibscale *accel_calibscale;
3840
struct mutex lock;
3941
struct iio_dummy_regs *regs;
42+
int steps_enabled;
43+
int steps;
44+
int height;
4045
#ifdef CONFIG_IIO_SIMPLE_DUMMY_EVENTS
4146
int event_irq;
4247
int event_val;

drivers/staging/iio/iio_simple_dummy_events.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@ int iio_simple_dummy_write_event_config(struct iio_dev *indio_dev,
7272
st->event_en = state;
7373
else
7474
return -EINVAL;
75+
default:
76+
return -EINVAL;
77+
}
78+
break;
79+
case IIO_ACTIVITY:
80+
switch (type) {
81+
case IIO_EV_TYPE_THRESH:
82+
st->event_en = state;
83+
break;
84+
default:
85+
return -EINVAL;
86+
}
87+
case IIO_STEPS:
88+
switch (type) {
89+
case IIO_EV_TYPE_INSTANCE:
90+
st->event_en = state;
7591
break;
7692
default:
7793
return -EINVAL;
@@ -161,6 +177,33 @@ static irqreturn_t iio_simple_dummy_event_handler(int irq, void *private)
161177
IIO_EV_TYPE_THRESH, 0, 0, 0),
162178
iio_get_time_ns());
163179
break;
180+
case 1:
181+
if (st->activity_running > st->event_val)
182+
iio_push_event(indio_dev,
183+
IIO_EVENT_CODE(IIO_ACTIVITY, 0,
184+
IIO_MOD_RUNNING,
185+
IIO_EV_DIR_RISING,
186+
IIO_EV_TYPE_THRESH,
187+
0, 0, 0),
188+
iio_get_time_ns());
189+
break;
190+
case 2:
191+
if (st->activity_walking < st->event_val)
192+
iio_push_event(indio_dev,
193+
IIO_EVENT_CODE(IIO_ACTIVITY, 0,
194+
IIO_MOD_WALKING,
195+
IIO_EV_DIR_FALLING,
196+
IIO_EV_TYPE_THRESH,
197+
0, 0, 0),
198+
iio_get_time_ns());
199+
break;
200+
case 3:
201+
iio_push_event(indio_dev,
202+
IIO_EVENT_CODE(IIO_STEPS, 0, IIO_NO_MOD,
203+
IIO_EV_DIR_NONE,
204+
IIO_EV_TYPE_INSTANCE, 0, 0, 0),
205+
iio_get_time_ns());
206+
break;
164207
default:
165208
break;
166209
}

0 commit comments

Comments
 (0)