1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Freescale MMA9553L Intelligent Pedometer driver
4 * Copyright (c) 2014, Intel Corporation.
5 */
6
7 #include <linux/i2c.h>
8 #include <linux/interrupt.h>
9 #include <linux/mod_devicetable.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/iio/iio.h>
13 #include <linux/iio/sysfs.h>
14 #include <linux/iio/events.h>
15 #include <linux/pm_runtime.h>
16 #include "mma9551_core.h"
17
18 /* Pedometer configuration registers (R/W) */
19 #define MMA9553_REG_CONF_SLEEPMIN 0x00
20 #define MMA9553_REG_CONF_SLEEPMAX 0x02
21 #define MMA9553_REG_CONF_SLEEPTHD 0x04
22 #define MMA9553_MASK_CONF_WORD GENMASK(15, 0)
23
24 #define MMA9553_REG_CONF_CONF_STEPLEN 0x06
25 #define MMA9553_MASK_CONF_CONFIG BIT(15)
26 #define MMA9553_MASK_CONF_ACT_DBCNTM BIT(14)
27 #define MMA9553_MASK_CONF_SLP_DBCNTM BIT(13)
28 #define MMA9553_MASK_CONF_STEPLEN GENMASK(7, 0)
29
30 #define MMA9553_REG_CONF_HEIGHT_WEIGHT 0x08
31 #define MMA9553_MASK_CONF_HEIGHT GENMASK(15, 8)
32 #define MMA9553_MASK_CONF_WEIGHT GENMASK(7, 0)
33
34 #define MMA9553_REG_CONF_FILTER 0x0A
35 #define MMA9553_MASK_CONF_FILTSTEP GENMASK(15, 8)
36 #define MMA9553_MASK_CONF_MALE BIT(7)
37 #define MMA9553_MASK_CONF_FILTTIME GENMASK(6, 0)
38
39 #define MMA9553_REG_CONF_SPEED_STEP 0x0C
40 #define MMA9553_MASK_CONF_SPDPRD GENMASK(15, 8)
41 #define MMA9553_MASK_CONF_STEPCOALESCE GENMASK(7, 0)
42
43 #define MMA9553_REG_CONF_ACTTHD 0x0E
44 #define MMA9553_MAX_ACTTHD GENMASK(15, 0)
45
46 /* Pedometer status registers (R-only) */
47 #define MMA9553_REG_STATUS 0x00
48 #define MMA9553_MASK_STATUS_MRGFL BIT(15)
49 #define MMA9553_MASK_STATUS_SUSPCHG BIT(14)
50 #define MMA9553_MASK_STATUS_STEPCHG BIT(13)
51 #define MMA9553_MASK_STATUS_ACTCHG BIT(12)
52 #define MMA9553_MASK_STATUS_SUSP BIT(11)
53 #define MMA9553_MASK_STATUS_ACTIVITY GENMASK(10, 8)
54 #define MMA9553_MASK_STATUS_VERSION GENMASK(7, 0)
55
56 #define MMA9553_REG_STEPCNT 0x02
57 #define MMA9553_REG_DISTANCE 0x04
58 #define MMA9553_REG_SPEED 0x06
59 #define MMA9553_REG_CALORIES 0x08
60 #define MMA9553_REG_SLEEPCNT 0x0A
61
62 /* Pedometer events are always mapped to this pin. */
63 #define MMA9553_DEFAULT_GPIO_PIN mma9551_gpio6
64 #define MMA9553_DEFAULT_GPIO_POLARITY 0
65
66 /* Bitnum used for GPIO configuration = bit number in high status byte */
67 #define MMA9553_STATUS_TO_BITNUM(bit) (ffs(bit) - 9)
68 #define MMA9553_MAX_BITNUM MMA9553_STATUS_TO_BITNUM(BIT(16))
69
70 #define MMA9553_DEFAULT_SAMPLE_RATE 30 /* Hz */
71
72 /*
73 * The internal activity level must be stable for ACTTHD samples before
74 * ACTIVITY is updated. The ACTIVITY variable contains the current activity
75 * level and is updated every time a step is detected or once a second
76 * if there are no steps.
77 */
78 #define MMA9553_ACTIVITY_THD_TO_SEC(thd) ((thd) / MMA9553_DEFAULT_SAMPLE_RATE)
79 #define MMA9553_ACTIVITY_SEC_TO_THD(sec) ((sec) * MMA9553_DEFAULT_SAMPLE_RATE)
80
81 /*
82 * Autonomously suspend pedometer if acceleration vector magnitude
83 * is near 1g (4096 at 0.244 mg/LSB resolution) for 30 seconds.
84 */
85 #define MMA9553_DEFAULT_SLEEPMIN 3688 /* 0,9 g */
86 #define MMA9553_DEFAULT_SLEEPMAX 4508 /* 1,1 g */
87 #define MMA9553_DEFAULT_SLEEPTHD (MMA9553_DEFAULT_SAMPLE_RATE * 30)
88
89 #define MMA9553_CONFIG_RETRIES 2
90
91 /* Status register - activity field */
92 enum activity_level {
93 ACTIVITY_UNKNOWN,
94 ACTIVITY_REST,
95 ACTIVITY_WALKING,
96 ACTIVITY_JOGGING,
97 ACTIVITY_RUNNING,
98 };
99
100 static const struct mma9553_event_info {
101 enum iio_chan_type type;
102 enum iio_modifier mod;
103 enum iio_event_direction dir;
104 } mma9553_events_info[] = {
105 {
106 .type = IIO_STEPS,
107 .mod = IIO_NO_MOD,
108 .dir = IIO_EV_DIR_NONE,
109 },
110 {
111 .type = IIO_ACTIVITY,
112 .mod = IIO_MOD_STILL,
113 .dir = IIO_EV_DIR_RISING,
114 },
115 {
116 .type = IIO_ACTIVITY,
117 .mod = IIO_MOD_STILL,
118 .dir = IIO_EV_DIR_FALLING,
119 },
120 {
121 .type = IIO_ACTIVITY,
122 .mod = IIO_MOD_WALKING,
123 .dir = IIO_EV_DIR_RISING,
124 },
125 {
126 .type = IIO_ACTIVITY,
127 .mod = IIO_MOD_WALKING,
128 .dir = IIO_EV_DIR_FALLING,
129 },
130 {
131 .type = IIO_ACTIVITY,
132 .mod = IIO_MOD_JOGGING,
133 .dir = IIO_EV_DIR_RISING,
134 },
135 {
136 .type = IIO_ACTIVITY,
137 .mod = IIO_MOD_JOGGING,
138 .dir = IIO_EV_DIR_FALLING,
139 },
140 {
141 .type = IIO_ACTIVITY,
142 .mod = IIO_MOD_RUNNING,
143 .dir = IIO_EV_DIR_RISING,
144 },
145 {
146 .type = IIO_ACTIVITY,
147 .mod = IIO_MOD_RUNNING,
148 .dir = IIO_EV_DIR_FALLING,
149 },
150 };
151
152 #define MMA9553_EVENTS_INFO_SIZE ARRAY_SIZE(mma9553_events_info)
153
154 struct mma9553_event {
155 const struct mma9553_event_info *info;
156 bool enabled;
157 };
158
159 struct mma9553_conf_regs {
160 u16 sleepmin;
161 u16 sleepmax;
162 u16 sleepthd;
163 u16 config;
164 u16 height_weight;
165 u16 filter;
166 u16 speed_step;
167 u16 actthd;
168 } __packed;
169
170 struct mma9553_data {
171 struct i2c_client *client;
172 /*
173 * 1. Serialize access to HW (requested by mma9551_core API).
174 * 2. Serialize sequences that power on/off the device and access HW.
175 */
176 struct mutex mutex;
177 struct mma9553_conf_regs conf;
178 struct mma9553_event events[MMA9553_EVENTS_INFO_SIZE];
179 int num_events;
180 u8 gpio_bitnum;
181 /*
182 * This is used for all features that depend on step count:
183 * step count, distance, speed, calories.
184 */
185 bool stepcnt_enabled;
186 u16 stepcnt;
187 u8 activity;
188 s64 timestamp;
189 };
190
mma9553_get_bits(u16 val,u16 mask)191 static u8 mma9553_get_bits(u16 val, u16 mask)
192 {
193 return (val & mask) >> (ffs(mask) - 1);
194 }
195
mma9553_set_bits(u16 current_val,u16 val,u16 mask)196 static u16 mma9553_set_bits(u16 current_val, u16 val, u16 mask)
197 {
198 return (current_val & ~mask) | (val << (ffs(mask) - 1));
199 }
200
mma9553_activity_to_mod(enum activity_level activity)201 static enum iio_modifier mma9553_activity_to_mod(enum activity_level activity)
202 {
203 switch (activity) {
204 case ACTIVITY_RUNNING:
205 return IIO_MOD_RUNNING;
206 case ACTIVITY_JOGGING:
207 return IIO_MOD_JOGGING;
208 case ACTIVITY_WALKING:
209 return IIO_MOD_WALKING;
210 case ACTIVITY_REST:
211 return IIO_MOD_STILL;
212 case ACTIVITY_UNKNOWN:
213 default:
214 return IIO_NO_MOD;
215 }
216 }
217
mma9553_init_events(struct mma9553_data * data)218 static void mma9553_init_events(struct mma9553_data *data)
219 {
220 int i;
221
222 data->num_events = MMA9553_EVENTS_INFO_SIZE;
223 for (i = 0; i < data->num_events; i++) {
224 data->events[i].info = &mma9553_events_info[i];
225 data->events[i].enabled = false;
226 }
227 }
228
mma9553_get_event(struct mma9553_data * data,enum iio_chan_type type,enum iio_modifier mod,enum iio_event_direction dir)229 static struct mma9553_event *mma9553_get_event(struct mma9553_data *data,
230 enum iio_chan_type type,
231 enum iio_modifier mod,
232 enum iio_event_direction dir)
233 {
234 int i;
235
236 for (i = 0; i < data->num_events; i++)
237 if (data->events[i].info->type == type &&
238 data->events[i].info->mod == mod &&
239 data->events[i].info->dir == dir)
240 return &data->events[i];
241
242 return NULL;
243 }
244
mma9553_is_any_event_enabled(struct mma9553_data * data,bool check_type,enum iio_chan_type type)245 static bool mma9553_is_any_event_enabled(struct mma9553_data *data,
246 bool check_type,
247 enum iio_chan_type type)
248 {
249 int i;
250
251 for (i = 0; i < data->num_events; i++)
252 if ((check_type && data->events[i].info->type == type &&
253 data->events[i].enabled) ||
254 (!check_type && data->events[i].enabled))
255 return true;
256
257 return false;
258 }
259
mma9553_set_config(struct mma9553_data * data,u16 reg,u16 * p_reg_val,u16 val,u16 mask)260 static int mma9553_set_config(struct mma9553_data *data, u16 reg,
261 u16 *p_reg_val, u16 val, u16 mask)
262 {
263 int ret, retries;
264 u16 reg_val, config;
265
266 reg_val = *p_reg_val;
267 if (val == mma9553_get_bits(reg_val, mask))
268 return 0;
269
270 reg_val = mma9553_set_bits(reg_val, val, mask);
271 ret = mma9551_write_config_word(data->client, MMA9551_APPID_PEDOMETER,
272 reg, reg_val);
273 if (ret < 0) {
274 dev_err(&data->client->dev,
275 "error writing config register 0x%x\n", reg);
276 return ret;
277 }
278
279 *p_reg_val = reg_val;
280
281 /* Reinitializes the pedometer with current configuration values */
282 config = mma9553_set_bits(data->conf.config, 1,
283 MMA9553_MASK_CONF_CONFIG);
284
285 ret = mma9551_write_config_word(data->client, MMA9551_APPID_PEDOMETER,
286 MMA9553_REG_CONF_CONF_STEPLEN, config);
287 if (ret < 0) {
288 dev_err(&data->client->dev,
289 "error writing config register 0x%x\n",
290 MMA9553_REG_CONF_CONF_STEPLEN);
291 return ret;
292 }
293
294 retries = MMA9553_CONFIG_RETRIES;
295 do {
296 mma9551_sleep(MMA9553_DEFAULT_SAMPLE_RATE);
297 ret = mma9551_read_config_word(data->client,
298 MMA9551_APPID_PEDOMETER,
299 MMA9553_REG_CONF_CONF_STEPLEN,
300 &config);
301 if (ret < 0)
302 return ret;
303 } while (mma9553_get_bits(config, MMA9553_MASK_CONF_CONFIG) &&
304 --retries > 0);
305
306 return 0;
307 }
308
mma9553_read_activity_stepcnt(struct mma9553_data * data,u8 * activity,u16 * stepcnt)309 static int mma9553_read_activity_stepcnt(struct mma9553_data *data,
310 u8 *activity, u16 *stepcnt)
311 {
312 u16 buf[2];
313 int ret;
314
315 ret = mma9551_read_status_words(data->client, MMA9551_APPID_PEDOMETER,
316 MMA9553_REG_STATUS, ARRAY_SIZE(buf),
317 buf);
318 if (ret < 0) {
319 dev_err(&data->client->dev,
320 "error reading status and stepcnt\n");
321 return ret;
322 }
323
324 *activity = mma9553_get_bits(buf[0], MMA9553_MASK_STATUS_ACTIVITY);
325 *stepcnt = buf[1];
326
327 return 0;
328 }
329
mma9553_conf_gpio(struct mma9553_data * data)330 static int mma9553_conf_gpio(struct mma9553_data *data)
331 {
332 u8 bitnum = 0, appid = MMA9551_APPID_PEDOMETER;
333 int ret;
334 struct mma9553_event *ev_step_detect;
335 bool activity_enabled;
336
337 activity_enabled = mma9553_is_any_event_enabled(data, true,
338 IIO_ACTIVITY);
339 ev_step_detect = mma9553_get_event(data, IIO_STEPS, IIO_NO_MOD,
340 IIO_EV_DIR_NONE);
341
342 /*
343 * If both step detector and activity are enabled, use the MRGFL bit.
344 * This bit is the logical OR of the SUSPCHG, STEPCHG, and ACTCHG flags.
345 */
346 if (activity_enabled && ev_step_detect->enabled)
347 bitnum = MMA9553_STATUS_TO_BITNUM(MMA9553_MASK_STATUS_MRGFL);
348 else if (ev_step_detect->enabled)
349 bitnum = MMA9553_STATUS_TO_BITNUM(MMA9553_MASK_STATUS_STEPCHG);
350 else if (activity_enabled)
351 bitnum = MMA9553_STATUS_TO_BITNUM(MMA9553_MASK_STATUS_ACTCHG);
352 else /* Reset */
353 appid = MMA9551_APPID_NONE;
354
355 if (data->gpio_bitnum == bitnum)
356 return 0;
357
358 /* Save initial values for activity and stepcnt */
359 if (activity_enabled || ev_step_detect->enabled) {
360 ret = mma9553_read_activity_stepcnt(data, &data->activity,
361 &data->stepcnt);
362 if (ret < 0)
363 return ret;
364 }
365
366 ret = mma9551_gpio_config(data->client, MMA9553_DEFAULT_GPIO_PIN, appid,
367 bitnum, MMA9553_DEFAULT_GPIO_POLARITY);
368 if (ret < 0)
369 return ret;
370 data->gpio_bitnum = bitnum;
371
372 return 0;
373 }
374
mma9553_init(struct mma9553_data * data)375 static int mma9553_init(struct mma9553_data *data)
376 {
377 int ret;
378
379 ret = mma9551_read_version(data->client);
380 if (ret)
381 return ret;
382
383 /*
384 * Read all the pedometer configuration registers. This is used as
385 * a device identification command to differentiate the MMA9553L
386 * from the MMA9550L.
387 */
388 ret = mma9551_read_config_words(data->client, MMA9551_APPID_PEDOMETER,
389 MMA9553_REG_CONF_SLEEPMIN,
390 sizeof(data->conf) / sizeof(u16),
391 (u16 *)&data->conf);
392 if (ret < 0) {
393 dev_err(&data->client->dev,
394 "failed to read configuration registers\n");
395 return ret;
396 }
397
398 /* Reset GPIO */
399 data->gpio_bitnum = MMA9553_MAX_BITNUM;
400 ret = mma9553_conf_gpio(data);
401 if (ret < 0)
402 return ret;
403
404 ret = mma9551_app_reset(data->client, MMA9551_RSC_PED);
405 if (ret < 0)
406 return ret;
407
408 /* Init config registers */
409 data->conf.sleepmin = MMA9553_DEFAULT_SLEEPMIN;
410 data->conf.sleepmax = MMA9553_DEFAULT_SLEEPMAX;
411 data->conf.sleepthd = MMA9553_DEFAULT_SLEEPTHD;
412 data->conf.config = mma9553_set_bits(data->conf.config, 1,
413 MMA9553_MASK_CONF_CONFIG);
414 /*
415 * Clear the activity debounce counter when the activity level changes,
416 * so that the confidence level applies for any activity level.
417 */
418 data->conf.config = mma9553_set_bits(data->conf.config, 1,
419 MMA9553_MASK_CONF_ACT_DBCNTM);
420 ret = mma9551_write_config_words(data->client, MMA9551_APPID_PEDOMETER,
421 MMA9553_REG_CONF_SLEEPMIN,
422 sizeof(data->conf) / sizeof(u16),
423 (u16 *)&data->conf);
424 if (ret < 0) {
425 dev_err(&data->client->dev,
426 "failed to write configuration registers\n");
427 return ret;
428 }
429
430 return mma9551_set_device_state(data->client, true);
431 }
432
mma9553_read_status_word(struct mma9553_data * data,u16 reg,u16 * tmp)433 static int mma9553_read_status_word(struct mma9553_data *data, u16 reg,
434 u16 *tmp)
435 {
436 bool powered_on;
437 int ret;
438
439 /*
440 * The HW only counts steps and other dependent
441 * parameters (speed, distance, calories, activity)
442 * if power is on (from enabling an event or the
443 * step counter).
444 */
445 powered_on = mma9553_is_any_event_enabled(data, false, 0) ||
446 data->stepcnt_enabled;
447 if (!powered_on) {
448 dev_err(&data->client->dev, "No channels enabled\n");
449 return -EINVAL;
450 }
451
452 mutex_lock(&data->mutex);
453 ret = mma9551_read_status_word(data->client, MMA9551_APPID_PEDOMETER,
454 reg, tmp);
455 mutex_unlock(&data->mutex);
456 return ret;
457 }
458
mma9553_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)459 static int mma9553_read_raw(struct iio_dev *indio_dev,
460 struct iio_chan_spec const *chan,
461 int *val, int *val2, long mask)
462 {
463 struct mma9553_data *data = iio_priv(indio_dev);
464 int ret;
465 u16 tmp;
466 u8 activity;
467
468 switch (mask) {
469 case IIO_CHAN_INFO_PROCESSED:
470 switch (chan->type) {
471 case IIO_STEPS:
472 ret = mma9553_read_status_word(data,
473 MMA9553_REG_STEPCNT,
474 &tmp);
475 if (ret < 0)
476 return ret;
477 *val = tmp;
478 return IIO_VAL_INT;
479 case IIO_DISTANCE:
480 ret = mma9553_read_status_word(data,
481 MMA9553_REG_DISTANCE,
482 &tmp);
483 if (ret < 0)
484 return ret;
485 *val = tmp;
486 return IIO_VAL_INT;
487 case IIO_ACTIVITY:
488 ret = mma9553_read_status_word(data,
489 MMA9553_REG_STATUS,
490 &tmp);
491 if (ret < 0)
492 return ret;
493
494 activity =
495 mma9553_get_bits(tmp, MMA9553_MASK_STATUS_ACTIVITY);
496
497 /*
498 * The device does not support confidence value levels,
499 * so we will always have 100% for current activity and
500 * 0% for the others.
501 */
502 if (chan->channel2 == mma9553_activity_to_mod(activity))
503 *val = 100;
504 else
505 *val = 0;
506 return IIO_VAL_INT;
507 default:
508 return -EINVAL;
509 }
510 case IIO_CHAN_INFO_RAW:
511 switch (chan->type) {
512 case IIO_VELOCITY: /* m/h */
513 if (chan->channel2 != IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z)
514 return -EINVAL;
515 ret = mma9553_read_status_word(data,
516 MMA9553_REG_SPEED,
517 &tmp);
518 if (ret < 0)
519 return ret;
520 *val = tmp;
521 return IIO_VAL_INT;
522 case IIO_ENERGY: /* Cal or kcal */
523 ret = mma9553_read_status_word(data,
524 MMA9553_REG_CALORIES,
525 &tmp);
526 if (ret < 0)
527 return ret;
528 *val = tmp;
529 return IIO_VAL_INT;
530 case IIO_ACCEL:
531 mutex_lock(&data->mutex);
532 ret = mma9551_read_accel_chan(data->client,
533 chan, val, val2);
534 mutex_unlock(&data->mutex);
535 return ret;
536 default:
537 return -EINVAL;
538 }
539 case IIO_CHAN_INFO_SCALE:
540 switch (chan->type) {
541 case IIO_VELOCITY: /* m/h to m/s */
542 if (chan->channel2 != IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z)
543 return -EINVAL;
544 *val = 0;
545 *val2 = 277; /* 0.000277 */
546 return IIO_VAL_INT_PLUS_MICRO;
547 case IIO_ENERGY: /* Cal or kcal to J */
548 *val = 4184;
549 return IIO_VAL_INT;
550 case IIO_ACCEL:
551 return mma9551_read_accel_scale(val, val2);
552 default:
553 return -EINVAL;
554 }
555 case IIO_CHAN_INFO_ENABLE:
556 *val = data->stepcnt_enabled;
557 return IIO_VAL_INT;
558 case IIO_CHAN_INFO_CALIBHEIGHT:
559 tmp = mma9553_get_bits(data->conf.height_weight,
560 MMA9553_MASK_CONF_HEIGHT);
561 *val = tmp / 100; /* cm to m */
562 *val2 = (tmp % 100) * 10000;
563 return IIO_VAL_INT_PLUS_MICRO;
564 case IIO_CHAN_INFO_CALIBWEIGHT:
565 *val = mma9553_get_bits(data->conf.height_weight,
566 MMA9553_MASK_CONF_WEIGHT);
567 return IIO_VAL_INT;
568 case IIO_CHAN_INFO_DEBOUNCE_COUNT:
569 switch (chan->type) {
570 case IIO_STEPS:
571 *val = mma9553_get_bits(data->conf.filter,
572 MMA9553_MASK_CONF_FILTSTEP);
573 return IIO_VAL_INT;
574 default:
575 return -EINVAL;
576 }
577 case IIO_CHAN_INFO_DEBOUNCE_TIME:
578 switch (chan->type) {
579 case IIO_STEPS:
580 *val = mma9553_get_bits(data->conf.filter,
581 MMA9553_MASK_CONF_FILTTIME);
582 return IIO_VAL_INT;
583 default:
584 return -EINVAL;
585 }
586 case IIO_CHAN_INFO_INT_TIME:
587 switch (chan->type) {
588 case IIO_VELOCITY:
589 if (chan->channel2 != IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z)
590 return -EINVAL;
591 *val = mma9553_get_bits(data->conf.speed_step,
592 MMA9553_MASK_CONF_SPDPRD);
593 return IIO_VAL_INT;
594 default:
595 return -EINVAL;
596 }
597 default:
598 return -EINVAL;
599 }
600 }
601
mma9553_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)602 static int mma9553_write_raw(struct iio_dev *indio_dev,
603 struct iio_chan_spec const *chan,
604 int val, int val2, long mask)
605 {
606 struct mma9553_data *data = iio_priv(indio_dev);
607 int ret, tmp;
608
609 switch (mask) {
610 case IIO_CHAN_INFO_ENABLE:
611 if (data->stepcnt_enabled == !!val)
612 return 0;
613 mutex_lock(&data->mutex);
614 ret = mma9551_set_power_state(data->client, val);
615 if (ret < 0) {
616 mutex_unlock(&data->mutex);
617 return ret;
618 }
619 data->stepcnt_enabled = val;
620 mutex_unlock(&data->mutex);
621 return 0;
622 case IIO_CHAN_INFO_CALIBHEIGHT:
623 /* m to cm */
624 tmp = val * 100 + val2 / 10000;
625 if (tmp < 0 || tmp > 255)
626 return -EINVAL;
627 mutex_lock(&data->mutex);
628 ret = mma9553_set_config(data,
629 MMA9553_REG_CONF_HEIGHT_WEIGHT,
630 &data->conf.height_weight,
631 tmp, MMA9553_MASK_CONF_HEIGHT);
632 mutex_unlock(&data->mutex);
633 return ret;
634 case IIO_CHAN_INFO_CALIBWEIGHT:
635 if (val < 0 || val > 255)
636 return -EINVAL;
637 mutex_lock(&data->mutex);
638 ret = mma9553_set_config(data,
639 MMA9553_REG_CONF_HEIGHT_WEIGHT,
640 &data->conf.height_weight,
641 val, MMA9553_MASK_CONF_WEIGHT);
642 mutex_unlock(&data->mutex);
643 return ret;
644 case IIO_CHAN_INFO_DEBOUNCE_COUNT:
645 switch (chan->type) {
646 case IIO_STEPS:
647 /*
648 * Set to 0 to disable step filtering. If the value
649 * specified is greater than 6, then 6 will be used.
650 */
651 if (val < 0)
652 return -EINVAL;
653 if (val > 6)
654 val = 6;
655 mutex_lock(&data->mutex);
656 ret = mma9553_set_config(data, MMA9553_REG_CONF_FILTER,
657 &data->conf.filter, val,
658 MMA9553_MASK_CONF_FILTSTEP);
659 mutex_unlock(&data->mutex);
660 return ret;
661 default:
662 return -EINVAL;
663 }
664 case IIO_CHAN_INFO_DEBOUNCE_TIME:
665 switch (chan->type) {
666 case IIO_STEPS:
667 if (val < 0 || val > 127)
668 return -EINVAL;
669 mutex_lock(&data->mutex);
670 ret = mma9553_set_config(data, MMA9553_REG_CONF_FILTER,
671 &data->conf.filter, val,
672 MMA9553_MASK_CONF_FILTTIME);
673 mutex_unlock(&data->mutex);
674 return ret;
675 default:
676 return -EINVAL;
677 }
678 case IIO_CHAN_INFO_INT_TIME:
679 switch (chan->type) {
680 case IIO_VELOCITY:
681 if (chan->channel2 != IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z)
682 return -EINVAL;
683 /*
684 * If set to a value greater than 5, then 5 will be
685 * used. Warning: Do not set SPDPRD to 0 or 1 as
686 * this may cause undesirable behavior.
687 */
688 if (val < 2)
689 return -EINVAL;
690 if (val > 5)
691 val = 5;
692 mutex_lock(&data->mutex);
693 ret = mma9553_set_config(data,
694 MMA9553_REG_CONF_SPEED_STEP,
695 &data->conf.speed_step, val,
696 MMA9553_MASK_CONF_SPDPRD);
697 mutex_unlock(&data->mutex);
698 return ret;
699 default:
700 return -EINVAL;
701 }
702 default:
703 return -EINVAL;
704 }
705 }
706
mma9553_read_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir)707 static int mma9553_read_event_config(struct iio_dev *indio_dev,
708 const struct iio_chan_spec *chan,
709 enum iio_event_type type,
710 enum iio_event_direction dir)
711 {
712 struct mma9553_data *data = iio_priv(indio_dev);
713 struct mma9553_event *event;
714
715 event = mma9553_get_event(data, chan->type, chan->channel2, dir);
716 if (!event)
717 return -EINVAL;
718
719 return event->enabled;
720 }
721
mma9553_write_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,bool state)722 static int mma9553_write_event_config(struct iio_dev *indio_dev,
723 const struct iio_chan_spec *chan,
724 enum iio_event_type type,
725 enum iio_event_direction dir,
726 bool state)
727 {
728 struct mma9553_data *data = iio_priv(indio_dev);
729 struct mma9553_event *event;
730 int ret;
731
732 event = mma9553_get_event(data, chan->type, chan->channel2, dir);
733 if (!event)
734 return -EINVAL;
735
736 if (event->enabled == state)
737 return 0;
738
739 mutex_lock(&data->mutex);
740
741 ret = mma9551_set_power_state(data->client, state);
742 if (ret < 0)
743 goto err_out;
744 event->enabled = state;
745
746 ret = mma9553_conf_gpio(data);
747 if (ret < 0)
748 goto err_conf_gpio;
749
750 mutex_unlock(&data->mutex);
751
752 return 0;
753
754 err_conf_gpio:
755 if (state) {
756 event->enabled = false;
757 mma9551_set_power_state(data->client, false);
758 }
759 err_out:
760 mutex_unlock(&data->mutex);
761 return ret;
762 }
763
mma9553_read_event_value(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int * val,int * val2)764 static int mma9553_read_event_value(struct iio_dev *indio_dev,
765 const struct iio_chan_spec *chan,
766 enum iio_event_type type,
767 enum iio_event_direction dir,
768 enum iio_event_info info,
769 int *val, int *val2)
770 {
771 struct mma9553_data *data = iio_priv(indio_dev);
772
773 *val2 = 0;
774 switch (info) {
775 case IIO_EV_INFO_VALUE:
776 switch (chan->type) {
777 case IIO_STEPS:
778 *val = mma9553_get_bits(data->conf.speed_step,
779 MMA9553_MASK_CONF_STEPCOALESCE);
780 return IIO_VAL_INT;
781 case IIO_ACTIVITY:
782 /*
783 * The device does not support confidence value levels.
784 * We set an average of 50%.
785 */
786 *val = 50;
787 return IIO_VAL_INT;
788 default:
789 return -EINVAL;
790 }
791 case IIO_EV_INFO_PERIOD:
792 switch (chan->type) {
793 case IIO_ACTIVITY:
794 *val = MMA9553_ACTIVITY_THD_TO_SEC(data->conf.actthd);
795 return IIO_VAL_INT;
796 default:
797 return -EINVAL;
798 }
799 default:
800 return -EINVAL;
801 }
802 }
803
mma9553_write_event_value(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int val,int val2)804 static int mma9553_write_event_value(struct iio_dev *indio_dev,
805 const struct iio_chan_spec *chan,
806 enum iio_event_type type,
807 enum iio_event_direction dir,
808 enum iio_event_info info,
809 int val, int val2)
810 {
811 struct mma9553_data *data = iio_priv(indio_dev);
812 int ret;
813
814 switch (info) {
815 case IIO_EV_INFO_VALUE:
816 switch (chan->type) {
817 case IIO_STEPS:
818 if (val < 0 || val > 255)
819 return -EINVAL;
820 mutex_lock(&data->mutex);
821 ret = mma9553_set_config(data,
822 MMA9553_REG_CONF_SPEED_STEP,
823 &data->conf.speed_step, val,
824 MMA9553_MASK_CONF_STEPCOALESCE);
825 mutex_unlock(&data->mutex);
826 return ret;
827 default:
828 return -EINVAL;
829 }
830 case IIO_EV_INFO_PERIOD:
831 switch (chan->type) {
832 case IIO_ACTIVITY:
833 if (val < 0 || val > MMA9553_ACTIVITY_THD_TO_SEC(
834 MMA9553_MAX_ACTTHD))
835 return -EINVAL;
836 mutex_lock(&data->mutex);
837 ret = mma9553_set_config(data, MMA9553_REG_CONF_ACTTHD,
838 &data->conf.actthd,
839 MMA9553_ACTIVITY_SEC_TO_THD
840 (val), MMA9553_MASK_CONF_WORD);
841 mutex_unlock(&data->mutex);
842 return ret;
843 default:
844 return -EINVAL;
845 }
846 default:
847 return -EINVAL;
848 }
849 }
850
mma9553_get_calibgender_mode(struct iio_dev * indio_dev,const struct iio_chan_spec * chan)851 static int mma9553_get_calibgender_mode(struct iio_dev *indio_dev,
852 const struct iio_chan_spec *chan)
853 {
854 struct mma9553_data *data = iio_priv(indio_dev);
855 u8 gender;
856
857 gender = mma9553_get_bits(data->conf.filter, MMA9553_MASK_CONF_MALE);
858 /*
859 * HW expects 0 for female and 1 for male,
860 * while iio index is 0 for male and 1 for female.
861 */
862 return !gender;
863 }
864
mma9553_set_calibgender_mode(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,unsigned int mode)865 static int mma9553_set_calibgender_mode(struct iio_dev *indio_dev,
866 const struct iio_chan_spec *chan,
867 unsigned int mode)
868 {
869 struct mma9553_data *data = iio_priv(indio_dev);
870 u8 gender = !mode;
871 int ret;
872
873 if ((mode != 0) && (mode != 1))
874 return -EINVAL;
875 mutex_lock(&data->mutex);
876 ret = mma9553_set_config(data, MMA9553_REG_CONF_FILTER,
877 &data->conf.filter, gender,
878 MMA9553_MASK_CONF_MALE);
879 mutex_unlock(&data->mutex);
880
881 return ret;
882 }
883
884 static const struct iio_event_spec mma9553_step_event = {
885 .type = IIO_EV_TYPE_CHANGE,
886 .dir = IIO_EV_DIR_NONE,
887 .mask_separate = BIT(IIO_EV_INFO_ENABLE) | BIT(IIO_EV_INFO_VALUE),
888 };
889
890 static const struct iio_event_spec mma9553_activity_events[] = {
891 {
892 .type = IIO_EV_TYPE_THRESH,
893 .dir = IIO_EV_DIR_RISING,
894 .mask_separate = BIT(IIO_EV_INFO_ENABLE) |
895 BIT(IIO_EV_INFO_VALUE) |
896 BIT(IIO_EV_INFO_PERIOD),
897 },
898 {
899 .type = IIO_EV_TYPE_THRESH,
900 .dir = IIO_EV_DIR_FALLING,
901 .mask_separate = BIT(IIO_EV_INFO_ENABLE) |
902 BIT(IIO_EV_INFO_VALUE) |
903 BIT(IIO_EV_INFO_PERIOD),
904 },
905 };
906
907 static const char * const mma9553_calibgender_modes[] = { "male", "female" };
908
909 static const struct iio_enum mma9553_calibgender_enum = {
910 .items = mma9553_calibgender_modes,
911 .num_items = ARRAY_SIZE(mma9553_calibgender_modes),
912 .get = mma9553_get_calibgender_mode,
913 .set = mma9553_set_calibgender_mode,
914 };
915
916 static const struct iio_chan_spec_ext_info mma9553_ext_info[] = {
917 IIO_ENUM("calibgender", IIO_SHARED_BY_TYPE, &mma9553_calibgender_enum),
918 IIO_ENUM_AVAILABLE("calibgender", IIO_SHARED_BY_TYPE, &mma9553_calibgender_enum),
919 { }
920 };
921
922 #define MMA9553_PEDOMETER_CHANNEL(_type, _mask) { \
923 .type = _type, \
924 .info_mask_separate = BIT(IIO_CHAN_INFO_ENABLE) | \
925 BIT(IIO_CHAN_INFO_CALIBHEIGHT) | \
926 _mask, \
927 .ext_info = mma9553_ext_info, \
928 }
929
930 #define MMA9553_ACTIVITY_CHANNEL(_chan2) { \
931 .type = IIO_ACTIVITY, \
932 .modified = 1, \
933 .channel2 = _chan2, \
934 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
935 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_CALIBHEIGHT) | \
936 BIT(IIO_CHAN_INFO_ENABLE), \
937 .event_spec = mma9553_activity_events, \
938 .num_event_specs = ARRAY_SIZE(mma9553_activity_events), \
939 .ext_info = mma9553_ext_info, \
940 }
941
942 static const struct iio_chan_spec mma9553_channels[] = {
943 MMA9551_ACCEL_CHANNEL(IIO_MOD_X),
944 MMA9551_ACCEL_CHANNEL(IIO_MOD_Y),
945 MMA9551_ACCEL_CHANNEL(IIO_MOD_Z),
946
947 {
948 .type = IIO_STEPS,
949 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
950 BIT(IIO_CHAN_INFO_ENABLE) |
951 BIT(IIO_CHAN_INFO_DEBOUNCE_COUNT) |
952 BIT(IIO_CHAN_INFO_DEBOUNCE_TIME),
953 .event_spec = &mma9553_step_event,
954 .num_event_specs = 1,
955 },
956
957 MMA9553_PEDOMETER_CHANNEL(IIO_DISTANCE, BIT(IIO_CHAN_INFO_PROCESSED)),
958 {
959 .type = IIO_VELOCITY,
960 .modified = 1,
961 .channel2 = IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z,
962 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
963 BIT(IIO_CHAN_INFO_SCALE) |
964 BIT(IIO_CHAN_INFO_INT_TIME) |
965 BIT(IIO_CHAN_INFO_ENABLE),
966 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_CALIBHEIGHT),
967 .ext_info = mma9553_ext_info,
968 },
969 MMA9553_PEDOMETER_CHANNEL(IIO_ENERGY, BIT(IIO_CHAN_INFO_RAW) |
970 BIT(IIO_CHAN_INFO_SCALE) |
971 BIT(IIO_CHAN_INFO_CALIBWEIGHT)),
972
973 MMA9553_ACTIVITY_CHANNEL(IIO_MOD_RUNNING),
974 MMA9553_ACTIVITY_CHANNEL(IIO_MOD_JOGGING),
975 MMA9553_ACTIVITY_CHANNEL(IIO_MOD_WALKING),
976 MMA9553_ACTIVITY_CHANNEL(IIO_MOD_STILL),
977 };
978
979 static const struct iio_info mma9553_info = {
980 .read_raw = mma9553_read_raw,
981 .write_raw = mma9553_write_raw,
982 .read_event_config = mma9553_read_event_config,
983 .write_event_config = mma9553_write_event_config,
984 .read_event_value = mma9553_read_event_value,
985 .write_event_value = mma9553_write_event_value,
986 };
987
mma9553_irq_handler(int irq,void * private)988 static irqreturn_t mma9553_irq_handler(int irq, void *private)
989 {
990 struct iio_dev *indio_dev = private;
991 struct mma9553_data *data = iio_priv(indio_dev);
992
993 data->timestamp = iio_get_time_ns(indio_dev);
994 /*
995 * Since we only configure the interrupt pin when an
996 * event is enabled, we are sure we have at least
997 * one event enabled at this point.
998 */
999 return IRQ_WAKE_THREAD;
1000 }
1001
mma9553_event_handler(int irq,void * private)1002 static irqreturn_t mma9553_event_handler(int irq, void *private)
1003 {
1004 struct iio_dev *indio_dev = private;
1005 struct mma9553_data *data = iio_priv(indio_dev);
1006 u16 stepcnt;
1007 u8 activity;
1008 struct mma9553_event *ev_activity, *ev_prev_activity, *ev_step_detect;
1009 int ret;
1010
1011 mutex_lock(&data->mutex);
1012 ret = mma9553_read_activity_stepcnt(data, &activity, &stepcnt);
1013 if (ret < 0) {
1014 mutex_unlock(&data->mutex);
1015 return IRQ_HANDLED;
1016 }
1017
1018 ev_prev_activity = mma9553_get_event(data, IIO_ACTIVITY,
1019 mma9553_activity_to_mod(
1020 data->activity),
1021 IIO_EV_DIR_FALLING);
1022 ev_activity = mma9553_get_event(data, IIO_ACTIVITY,
1023 mma9553_activity_to_mod(activity),
1024 IIO_EV_DIR_RISING);
1025 ev_step_detect = mma9553_get_event(data, IIO_STEPS, IIO_NO_MOD,
1026 IIO_EV_DIR_NONE);
1027
1028 if (ev_step_detect->enabled && (stepcnt != data->stepcnt)) {
1029 data->stepcnt = stepcnt;
1030 iio_push_event(indio_dev,
1031 IIO_UNMOD_EVENT_CODE(IIO_STEPS, 0,
1032 IIO_EV_TYPE_CHANGE,
1033 IIO_EV_DIR_NONE),
1034 data->timestamp);
1035 }
1036
1037 if (activity != data->activity) {
1038 data->activity = activity;
1039 /* ev_activity can be NULL if activity == ACTIVITY_UNKNOWN */
1040 if (ev_prev_activity && ev_prev_activity->enabled)
1041 iio_push_event(indio_dev,
1042 IIO_MOD_EVENT_CODE(IIO_ACTIVITY, 0,
1043 ev_prev_activity->info->mod,
1044 IIO_EV_TYPE_THRESH,
1045 IIO_EV_DIR_FALLING),
1046 data->timestamp);
1047
1048 if (ev_activity && ev_activity->enabled)
1049 iio_push_event(indio_dev,
1050 IIO_MOD_EVENT_CODE(IIO_ACTIVITY, 0,
1051 ev_activity->info->mod,
1052 IIO_EV_TYPE_THRESH,
1053 IIO_EV_DIR_RISING),
1054 data->timestamp);
1055 }
1056 mutex_unlock(&data->mutex);
1057
1058 return IRQ_HANDLED;
1059 }
1060
mma9553_probe(struct i2c_client * client)1061 static int mma9553_probe(struct i2c_client *client)
1062 {
1063 const struct i2c_device_id *id = i2c_client_get_device_id(client);
1064 struct mma9553_data *data;
1065 struct iio_dev *indio_dev;
1066 const char *name = NULL;
1067 int ret;
1068
1069 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
1070 if (!indio_dev)
1071 return -ENOMEM;
1072
1073 data = iio_priv(indio_dev);
1074 i2c_set_clientdata(client, indio_dev);
1075 data->client = client;
1076
1077 if (id)
1078 name = id->name;
1079 else
1080 name = iio_get_acpi_device_name(&client->dev);
1081 if (!name)
1082 return -ENOSYS;
1083
1084 mutex_init(&data->mutex);
1085 mma9553_init_events(data);
1086
1087 ret = mma9553_init(data);
1088 if (ret < 0)
1089 return ret;
1090
1091 indio_dev->channels = mma9553_channels;
1092 indio_dev->num_channels = ARRAY_SIZE(mma9553_channels);
1093 indio_dev->name = name;
1094 indio_dev->modes = INDIO_DIRECT_MODE;
1095 indio_dev->info = &mma9553_info;
1096
1097 if (client->irq > 0) {
1098 ret = devm_request_threaded_irq(&client->dev, client->irq,
1099 mma9553_irq_handler,
1100 mma9553_event_handler,
1101 IRQF_TRIGGER_RISING,
1102 "mma9553_event", indio_dev);
1103 if (ret < 0) {
1104 dev_err(&client->dev, "request irq %d failed\n",
1105 client->irq);
1106 goto out_poweroff;
1107 }
1108 }
1109
1110 ret = pm_runtime_set_active(&client->dev);
1111 if (ret < 0)
1112 goto out_poweroff;
1113
1114 pm_runtime_enable(&client->dev);
1115 pm_runtime_set_autosuspend_delay(&client->dev,
1116 MMA9551_AUTO_SUSPEND_DELAY_MS);
1117 pm_runtime_use_autosuspend(&client->dev);
1118
1119 ret = iio_device_register(indio_dev);
1120 if (ret < 0) {
1121 dev_err(&client->dev, "unable to register iio device\n");
1122 goto err_pm_cleanup;
1123 }
1124
1125 dev_dbg(&indio_dev->dev, "Registered device %s\n", name);
1126 return 0;
1127
1128 err_pm_cleanup:
1129 pm_runtime_dont_use_autosuspend(&client->dev);
1130 pm_runtime_disable(&client->dev);
1131 out_poweroff:
1132 mma9551_set_device_state(client, false);
1133 return ret;
1134 }
1135
mma9553_remove(struct i2c_client * client)1136 static void mma9553_remove(struct i2c_client *client)
1137 {
1138 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1139 struct mma9553_data *data = iio_priv(indio_dev);
1140
1141 iio_device_unregister(indio_dev);
1142
1143 pm_runtime_disable(&client->dev);
1144 pm_runtime_set_suspended(&client->dev);
1145
1146 mutex_lock(&data->mutex);
1147 mma9551_set_device_state(data->client, false);
1148 mutex_unlock(&data->mutex);
1149 }
1150
mma9553_runtime_suspend(struct device * dev)1151 static int mma9553_runtime_suspend(struct device *dev)
1152 {
1153 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1154 struct mma9553_data *data = iio_priv(indio_dev);
1155 int ret;
1156
1157 mutex_lock(&data->mutex);
1158 ret = mma9551_set_device_state(data->client, false);
1159 mutex_unlock(&data->mutex);
1160 if (ret < 0) {
1161 dev_err(&data->client->dev, "powering off device failed\n");
1162 return -EAGAIN;
1163 }
1164
1165 return 0;
1166 }
1167
mma9553_runtime_resume(struct device * dev)1168 static int mma9553_runtime_resume(struct device *dev)
1169 {
1170 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1171 struct mma9553_data *data = iio_priv(indio_dev);
1172 int ret;
1173
1174 ret = mma9551_set_device_state(data->client, true);
1175 if (ret < 0)
1176 return ret;
1177
1178 mma9551_sleep(MMA9553_DEFAULT_SAMPLE_RATE);
1179
1180 return 0;
1181 }
1182
mma9553_suspend(struct device * dev)1183 static int mma9553_suspend(struct device *dev)
1184 {
1185 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1186 struct mma9553_data *data = iio_priv(indio_dev);
1187 int ret;
1188
1189 mutex_lock(&data->mutex);
1190 ret = mma9551_set_device_state(data->client, false);
1191 mutex_unlock(&data->mutex);
1192
1193 return ret;
1194 }
1195
mma9553_resume(struct device * dev)1196 static int mma9553_resume(struct device *dev)
1197 {
1198 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1199 struct mma9553_data *data = iio_priv(indio_dev);
1200 int ret;
1201
1202 mutex_lock(&data->mutex);
1203 ret = mma9551_set_device_state(data->client, true);
1204 mutex_unlock(&data->mutex);
1205
1206 return ret;
1207 }
1208
1209 static const struct dev_pm_ops mma9553_pm_ops = {
1210 SYSTEM_SLEEP_PM_OPS(mma9553_suspend, mma9553_resume)
1211 RUNTIME_PM_OPS(mma9553_runtime_suspend, mma9553_runtime_resume, NULL)
1212 };
1213
1214 static const struct acpi_device_id mma9553_acpi_match[] = {
1215 {"MMA9553", 0},
1216 { }
1217 };
1218
1219 MODULE_DEVICE_TABLE(acpi, mma9553_acpi_match);
1220
1221 static const struct i2c_device_id mma9553_id[] = {
1222 { "mma9553" },
1223 { }
1224 };
1225
1226 MODULE_DEVICE_TABLE(i2c, mma9553_id);
1227
1228 static struct i2c_driver mma9553_driver = {
1229 .driver = {
1230 .name = "mma9553",
1231 .acpi_match_table = mma9553_acpi_match,
1232 .pm = pm_ptr(&mma9553_pm_ops),
1233 },
1234 .probe = mma9553_probe,
1235 .remove = mma9553_remove,
1236 .id_table = mma9553_id,
1237 };
1238
1239 module_i2c_driver(mma9553_driver);
1240
1241 MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>");
1242 MODULE_LICENSE("GPL v2");
1243 MODULE_DESCRIPTION("MMA9553L pedometer platform driver");
1244 MODULE_IMPORT_NS("IIO_MMA9551");
1245