2 * Freescale MMA9553L Intelligent Pedometer driver
3 * Copyright (c) 2014, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 #include <linux/module.h>
16 #include <linux/i2c.h>
17 #include <linux/interrupt.h>
18 #include <linux/slab.h>
19 #include <linux/acpi.h>
20 #include <linux/gpio/consumer.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/events.h>
24 #include <linux/pm_runtime.h>
25 #include "mma9551_core.h"
27 #define MMA9553_DRV_NAME "mma9553"
28 #define MMA9553_IRQ_NAME "mma9553_event"
29 #define MMA9553_GPIO_NAME "mma9553_int"
31 /* Pedometer configuration registers (R/W) */
32 #define MMA9553_REG_CONF_SLEEPMIN 0x00
33 #define MMA9553_REG_CONF_SLEEPMAX 0x02
34 #define MMA9553_REG_CONF_SLEEPTHD 0x04
35 #define MMA9553_MASK_CONF_WORD GENMASK(15, 0)
37 #define MMA9553_REG_CONF_CONF_STEPLEN 0x06
38 #define MMA9553_MASK_CONF_CONFIG BIT(15)
39 #define MMA9553_MASK_CONF_ACT_DBCNTM BIT(14)
40 #define MMA9553_MASK_CONF_SLP_DBCNTM BIT(13)
41 #define MMA9553_MASK_CONF_STEPLEN GENMASK(7, 0)
43 #define MMA9553_REG_CONF_HEIGHT_WEIGHT 0x08
44 #define MMA9553_MASK_CONF_HEIGHT GENMASK(15, 8)
45 #define MMA9553_MASK_CONF_WEIGHT GENMASK(7, 0)
47 #define MMA9553_REG_CONF_FILTER 0x0A
48 #define MMA9553_MASK_CONF_FILTSTEP GENMASK(15, 8)
49 #define MMA9553_MASK_CONF_MALE BIT(7)
50 #define MMA9553_MASK_CONF_FILTTIME GENMASK(6, 0)
52 #define MMA9553_REG_CONF_SPEED_STEP 0x0C
53 #define MMA9553_MASK_CONF_SPDPRD GENMASK(15, 8)
54 #define MMA9553_MASK_CONF_STEPCOALESCE GENMASK(7, 0)
56 #define MMA9553_REG_CONF_ACTTHD 0x0E
57 #define MMA9553_MAX_ACTTHD GENMASK(15, 0)
59 /* Pedometer status registers (R-only) */
60 #define MMA9553_REG_STATUS 0x00
61 #define MMA9553_MASK_STATUS_MRGFL BIT(15)
62 #define MMA9553_MASK_STATUS_SUSPCHG BIT(14)
63 #define MMA9553_MASK_STATUS_STEPCHG BIT(13)
64 #define MMA9553_MASK_STATUS_ACTCHG BIT(12)
65 #define MMA9553_MASK_STATUS_SUSP BIT(11)
66 #define MMA9553_MASK_STATUS_ACTIVITY (BIT(10) | BIT(9) | BIT(8))
67 #define MMA9553_MASK_STATUS_VERSION 0x00FF
69 #define MMA9553_REG_STEPCNT 0x02
70 #define MMA9553_REG_DISTANCE 0x04
71 #define MMA9553_REG_SPEED 0x06
72 #define MMA9553_REG_CALORIES 0x08
73 #define MMA9553_REG_SLEEPCNT 0x0A
75 /* Pedometer events are always mapped to this pin. */
76 #define MMA9553_DEFAULT_GPIO_PIN mma9551_gpio6
77 #define MMA9553_DEFAULT_GPIO_POLARITY 0
79 /* Bitnum used for gpio configuration = bit number in high status byte */
80 #define STATUS_TO_BITNUM(bit) (ffs(bit) - 9)
82 #define MMA9553_DEFAULT_SAMPLE_RATE 30 /* Hz */
85 * The internal activity level must be stable for ACTTHD samples before
86 * ACTIVITY is updated.The ACTIVITY variable contains the current activity
87 * level and is updated every time a step is detected or once a second
88 * if there are no steps.
90 #define MMA9553_ACTIVITY_THD_TO_SEC(thd) ((thd) / MMA9553_DEFAULT_SAMPLE_RATE)
91 #define MMA9553_ACTIVITY_SEC_TO_THD(sec) ((sec) * MMA9553_DEFAULT_SAMPLE_RATE)
94 * Autonomously suspend pedometer if acceleration vector magnitude
95 * is near 1g (4096 at 0.244 mg/LSB resolution) for 30 seconds.
97 #define MMA9553_DEFAULT_SLEEPMIN 3688 /* 0,9 g */
98 #define MMA9553_DEFAULT_SLEEPMAX 4508 /* 1,1 g */
99 #define MMA9553_DEFAULT_SLEEPTHD (MMA9553_DEFAULT_SAMPLE_RATE * 30)
101 #define MMA9553_CONFIG_RETRIES 2
103 /* Status register - activity field */
104 enum activity_level {
112 static struct mma9553_event_info {
113 enum iio_chan_type type;
114 enum iio_modifier mod;
115 enum iio_event_direction dir;
116 } mma9553_events_info[] = {
120 .dir = IIO_EV_DIR_NONE,
123 .type = IIO_ACTIVITY,
124 .mod = IIO_MOD_STILL,
125 .dir = IIO_EV_DIR_RISING,
128 .type = IIO_ACTIVITY,
129 .mod = IIO_MOD_STILL,
130 .dir = IIO_EV_DIR_FALLING,
133 .type = IIO_ACTIVITY,
134 .mod = IIO_MOD_WALKING,
135 .dir = IIO_EV_DIR_RISING,
138 .type = IIO_ACTIVITY,
139 .mod = IIO_MOD_WALKING,
140 .dir = IIO_EV_DIR_FALLING,
143 .type = IIO_ACTIVITY,
144 .mod = IIO_MOD_JOGGING,
145 .dir = IIO_EV_DIR_RISING,
148 .type = IIO_ACTIVITY,
149 .mod = IIO_MOD_JOGGING,
150 .dir = IIO_EV_DIR_FALLING,
153 .type = IIO_ACTIVITY,
154 .mod = IIO_MOD_RUNNING,
155 .dir = IIO_EV_DIR_RISING,
158 .type = IIO_ACTIVITY,
159 .mod = IIO_MOD_RUNNING,
160 .dir = IIO_EV_DIR_FALLING,
164 #define MMA9553_EVENTS_INFO_SIZE ARRAY_SIZE(mma9553_events_info)
166 struct mma9553_event {
167 struct mma9553_event_info *info;
171 struct mma9553_conf_regs {
182 struct mma9553_data {
183 struct i2c_client *client;
185 struct mma9553_conf_regs conf;
186 struct mma9553_event events[MMA9553_EVENTS_INFO_SIZE];
190 * This is used for all features that depend on step count:
191 * step count, distance, speed, calories.
193 bool stepcnt_enabled;
199 static u8 mma9553_get_bits(u16 val, u16 mask)
201 return (val & mask) >> (ffs(mask) - 1);
204 static u16 mma9553_set_bits(u16 current_val, u16 val, u16 mask)
206 return (current_val & ~mask) | (val << (ffs(mask) - 1));
209 static enum iio_modifier mma9553_activity_to_mod(enum activity_level activity)
212 case ACTIVITY_RUNNING:
213 return IIO_MOD_RUNNING;
214 case ACTIVITY_JOGGING:
215 return IIO_MOD_JOGGING;
216 case ACTIVITY_WALKING:
217 return IIO_MOD_WALKING;
219 return IIO_MOD_STILL;
220 case ACTIVITY_UNKNOWN:
226 static void mma9553_init_events(struct mma9553_data *data)
230 data->num_events = MMA9553_EVENTS_INFO_SIZE;
231 for (i = 0; i < data->num_events; i++) {
232 data->events[i].info = &mma9553_events_info[i];
233 data->events[i].enabled = false;
237 static struct mma9553_event *mma9553_get_event(struct mma9553_data *data,
238 enum iio_chan_type type,
239 enum iio_modifier mod,
240 enum iio_event_direction dir)
244 for (i = 0; i < data->num_events; i++)
245 if (data->events[i].info->type == type &&
246 data->events[i].info->mod == mod &&
247 data->events[i].info->dir == dir)
248 return &data->events[i];
253 static bool mma9553_is_any_event_enabled(struct mma9553_data *data,
255 enum iio_chan_type type)
259 for (i = 0; i < data->num_events; i++)
260 if ((check_type && data->events[i].info->type == type &&
261 data->events[i].enabled) ||
262 (!check_type && data->events[i].enabled))
268 static int mma9553_set_config(struct mma9553_data *data, u16 reg,
269 u16 *p_reg_val, u16 val, u16 mask)
274 reg_val = *p_reg_val;
275 if (val == mma9553_get_bits(reg_val, mask))
278 reg_val = mma9553_set_bits(reg_val, val, mask);
279 ret = mma9551_write_config_word(data->client, MMA9551_APPID_PEDOMETER,
282 dev_err(&data->client->dev,
283 "error writing config register 0x%x\n", reg);
287 *p_reg_val = reg_val;
289 /* Reinitializes the pedometer with current configuration values */
290 config = mma9553_set_bits(data->conf.config, 1,
291 MMA9553_MASK_CONF_CONFIG);
293 ret = mma9551_write_config_word(data->client, MMA9551_APPID_PEDOMETER,
294 MMA9553_REG_CONF_CONF_STEPLEN, config);
296 dev_err(&data->client->dev,
297 "error writing config register 0x%x\n",
298 MMA9553_REG_CONF_CONF_STEPLEN);
302 retries = MMA9553_CONFIG_RETRIES;
304 mma9551_sleep(MMA9553_DEFAULT_SAMPLE_RATE);
305 ret = mma9551_read_config_word(data->client,
306 MMA9551_APPID_PEDOMETER,
307 MMA9553_REG_CONF_CONF_STEPLEN,
311 } while (mma9553_get_bits(config, MMA9553_MASK_CONF_CONFIG) &&
317 static int mma9553_read_activity_stepcnt(struct mma9553_data *data,
318 u8 *activity, u16 *stepcnt)
323 ret = mma9551_read_status_words(data->client, MMA9551_APPID_PEDOMETER,
324 MMA9553_REG_STATUS, sizeof(u32), buf);
326 dev_err(&data->client->dev,
327 "error reading status and stepcnt\n");
331 *activity = mma9553_get_bits(buf[0], MMA9553_MASK_STATUS_ACTIVITY);
337 static int mma9553_conf_gpio(struct mma9553_data *data)
339 u8 bitnum = 0, appid = MMA9551_APPID_PEDOMETER;
341 struct mma9553_event *ev_step_detect;
342 bool activity_enabled;
345 mma9553_is_any_event_enabled(data, true, IIO_ACTIVITY);
347 mma9553_get_event(data, IIO_STEPS, IIO_NO_MOD, IIO_EV_DIR_NONE);
350 * If both step detector and activity are enabled, use the MRGFL bit.
351 * This bit is the logical OR of the SUSPCHG, STEPCHG, and ACTCHG flags.
353 if (activity_enabled && ev_step_detect->enabled)
354 bitnum = STATUS_TO_BITNUM(MMA9553_MASK_STATUS_MRGFL);
355 else if (ev_step_detect->enabled)
356 bitnum = STATUS_TO_BITNUM(MMA9553_MASK_STATUS_STEPCHG);
357 else if (activity_enabled)
358 bitnum = STATUS_TO_BITNUM(MMA9553_MASK_STATUS_ACTCHG);
360 appid = MMA9551_APPID_NONE;
362 if (data->gpio_bitnum == bitnum)
365 /* Save initial values for activity and stepcnt */
366 if (activity_enabled || ev_step_detect->enabled)
367 mma9553_read_activity_stepcnt(data, &data->activity,
370 ret = mma9551_gpio_config(data->client,
371 MMA9553_DEFAULT_GPIO_PIN,
372 appid, bitnum, MMA9553_DEFAULT_GPIO_POLARITY);
375 data->gpio_bitnum = bitnum;
380 static int mma9553_init(struct mma9553_data *data)
384 ret = mma9551_read_version(data->client);
389 * Read all the pedometer configuration registers. This is used as
390 * a device identification command to differentiate the MMA9553L
394 mma9551_read_config_words(data->client, MMA9551_APPID_PEDOMETER,
395 MMA9553_REG_CONF_SLEEPMIN,
396 sizeof(data->conf), (u16 *) &data->conf);
398 dev_err(&data->client->dev,
399 "device is not MMA9553L: failed to read cfg regs\n");
405 data->gpio_bitnum = -1;
406 ret = mma9553_conf_gpio(data);
410 ret = mma9551_app_reset(data->client, MMA9551_RSC_PED);
414 /* Init config registers */
415 data->conf.sleepmin = MMA9553_DEFAULT_SLEEPMIN;
416 data->conf.sleepmax = MMA9553_DEFAULT_SLEEPMAX;
417 data->conf.sleepthd = MMA9553_DEFAULT_SLEEPTHD;
419 mma9553_set_bits(data->conf.config, 1, MMA9553_MASK_CONF_CONFIG);
421 * Clear the activity debounce counter when the activity level changes,
422 * so that the confidence level applies for any activity level.
424 data->conf.config = mma9553_set_bits(data->conf.config, 1,
425 MMA9553_MASK_CONF_ACT_DBCNTM);
427 mma9551_write_config_words(data->client, MMA9551_APPID_PEDOMETER,
428 MMA9553_REG_CONF_SLEEPMIN,
429 sizeof(data->conf), (u16 *) &data->conf);
431 dev_err(&data->client->dev,
432 "failed to write configuration registers\n");
436 return mma9551_set_device_state(data->client, true);
439 static int mma9553_read_raw(struct iio_dev *indio_dev,
440 struct iio_chan_spec const *chan,
441 int *val, int *val2, long mask)
443 struct mma9553_data *data = iio_priv(indio_dev);
450 case IIO_CHAN_INFO_PROCESSED:
451 switch (chan->type) {
454 * The HW only counts steps and other dependent
455 * parameters (speed, distance, calories, activity)
456 * if power is on (from enabling an event or the
459 mma9553_is_any_event_enabled(data, false, 0) ||
460 data->stepcnt_enabled;
462 dev_err(&data->client->dev,
463 "No channels enabled\n");
466 mutex_lock(&data->mutex);
467 ret = mma9551_read_status_word(data->client,
468 MMA9551_APPID_PEDOMETER,
471 mutex_unlock(&data->mutex);
478 mma9553_is_any_event_enabled(data, false, 0) ||
479 data->stepcnt_enabled;
481 dev_err(&data->client->dev,
482 "No channels enabled\n");
485 mutex_lock(&data->mutex);
486 ret = mma9551_read_status_word(data->client,
487 MMA9551_APPID_PEDOMETER,
488 MMA9553_REG_DISTANCE,
490 mutex_unlock(&data->mutex);
497 mma9553_is_any_event_enabled(data, false, 0) ||
498 data->stepcnt_enabled;
500 dev_err(&data->client->dev,
501 "No channels enabled\n");
504 mutex_lock(&data->mutex);
505 ret = mma9551_read_status_word(data->client,
506 MMA9551_APPID_PEDOMETER,
509 mutex_unlock(&data->mutex);
514 mma9553_get_bits(tmp, MMA9553_MASK_STATUS_ACTIVITY);
517 * The device does not support confidence value levels,
518 * so we will always have 100% for current activity and
521 if (chan->channel2 == mma9553_activity_to_mod(activity))
529 case IIO_CHAN_INFO_RAW:
530 switch (chan->type) {
531 case IIO_VELOCITY: /* m/h */
532 if (chan->channel2 != IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z)
535 mma9553_is_any_event_enabled(data, false, 0) ||
536 data->stepcnt_enabled;
538 dev_err(&data->client->dev,
539 "No channels enabled\n");
542 mutex_lock(&data->mutex);
543 ret = mma9551_read_status_word(data->client,
544 MMA9551_APPID_PEDOMETER,
545 MMA9553_REG_SPEED, &tmp);
546 mutex_unlock(&data->mutex);
551 case IIO_ENERGY: /* Cal or kcal */
553 mma9553_is_any_event_enabled(data, false, 0) ||
554 data->stepcnt_enabled;
556 dev_err(&data->client->dev,
557 "No channels enabled\n");
560 mutex_lock(&data->mutex);
561 ret = mma9551_read_status_word(data->client,
562 MMA9551_APPID_PEDOMETER,
563 MMA9553_REG_CALORIES,
565 mutex_unlock(&data->mutex);
571 mutex_lock(&data->mutex);
572 ret = mma9551_read_accel_chan(data->client,
574 mutex_unlock(&data->mutex);
579 case IIO_CHAN_INFO_SCALE:
580 switch (chan->type) {
581 case IIO_VELOCITY: /* m/h to m/s */
582 if (chan->channel2 != IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z)
585 *val2 = 277; /* 0.000277 */
586 return IIO_VAL_INT_PLUS_MICRO;
587 case IIO_ENERGY: /* Cal or kcal to J */
591 return mma9551_read_accel_scale(val, val2);
595 case IIO_CHAN_INFO_ENABLE:
596 *val = data->stepcnt_enabled;
598 case IIO_CHAN_INFO_CALIBHEIGHT:
599 tmp = mma9553_get_bits(data->conf.height_weight,
600 MMA9553_MASK_CONF_HEIGHT);
601 *val = tmp / 100; /* cm to m */
602 *val2 = (tmp % 100) * 10000;
603 return IIO_VAL_INT_PLUS_MICRO;
604 case IIO_CHAN_INFO_CALIBWEIGHT:
605 *val = mma9553_get_bits(data->conf.height_weight,
606 MMA9553_MASK_CONF_WEIGHT);
608 case IIO_CHAN_INFO_DEBOUNCE_COUNT:
609 switch (chan->type) {
611 *val = mma9553_get_bits(data->conf.filter,
612 MMA9553_MASK_CONF_FILTSTEP);
617 case IIO_CHAN_INFO_DEBOUNCE_TIME:
618 switch (chan->type) {
620 *val = mma9553_get_bits(data->conf.filter,
621 MMA9553_MASK_CONF_FILTTIME);
626 case IIO_CHAN_INFO_INT_TIME:
627 switch (chan->type) {
629 if (chan->channel2 != IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z)
631 *val = mma9553_get_bits(data->conf.speed_step,
632 MMA9553_MASK_CONF_SPDPRD);
642 static int mma9553_write_raw(struct iio_dev *indio_dev,
643 struct iio_chan_spec const *chan,
644 int val, int val2, long mask)
646 struct mma9553_data *data = iio_priv(indio_dev);
650 case IIO_CHAN_INFO_ENABLE:
651 if (data->stepcnt_enabled == !!val)
653 mutex_lock(&data->mutex);
654 ret = mma9551_set_power_state(data->client, val);
656 mutex_unlock(&data->mutex);
659 data->stepcnt_enabled = val;
660 mutex_unlock(&data->mutex);
662 case IIO_CHAN_INFO_CALIBHEIGHT:
664 tmp = val * 100 + val2 / 10000;
665 if (tmp < 0 || tmp > 255)
667 mutex_lock(&data->mutex);
668 ret = mma9553_set_config(data,
669 MMA9553_REG_CONF_HEIGHT_WEIGHT,
670 &data->conf.height_weight,
671 tmp, MMA9553_MASK_CONF_HEIGHT);
672 mutex_unlock(&data->mutex);
674 case IIO_CHAN_INFO_CALIBWEIGHT:
675 if (val < 0 || val > 255)
677 mutex_lock(&data->mutex);
678 ret = mma9553_set_config(data,
679 MMA9553_REG_CONF_HEIGHT_WEIGHT,
680 &data->conf.height_weight,
681 val, MMA9553_MASK_CONF_WEIGHT);
682 mutex_unlock(&data->mutex);
684 case IIO_CHAN_INFO_DEBOUNCE_COUNT:
685 switch (chan->type) {
688 * Set to 0 to disable step filtering. If the value
689 * specified is greater than 6, then 6 will be used.
695 mutex_lock(&data->mutex);
696 ret = mma9553_set_config(data, MMA9553_REG_CONF_FILTER,
697 &data->conf.filter, val,
698 MMA9553_MASK_CONF_FILTSTEP);
699 mutex_unlock(&data->mutex);
704 case IIO_CHAN_INFO_DEBOUNCE_TIME:
705 switch (chan->type) {
707 if (val < 0 || val > 127)
709 mutex_lock(&data->mutex);
710 ret = mma9553_set_config(data, MMA9553_REG_CONF_FILTER,
711 &data->conf.filter, val,
712 MMA9553_MASK_CONF_FILTTIME);
713 mutex_unlock(&data->mutex);
718 case IIO_CHAN_INFO_INT_TIME:
719 switch (chan->type) {
721 if (chan->channel2 != IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z)
724 * If set to a value greater than 5, then 5 will be
725 * used. Warning: Do not set SPDPRD to 0 or 1 as
726 * this may cause undesirable behavior.
732 mutex_lock(&data->mutex);
733 ret = mma9553_set_config(data,
734 MMA9553_REG_CONF_SPEED_STEP,
735 &data->conf.speed_step, val,
736 MMA9553_MASK_CONF_SPDPRD);
737 mutex_unlock(&data->mutex);
747 static int mma9553_read_event_config(struct iio_dev *indio_dev,
748 const struct iio_chan_spec *chan,
749 enum iio_event_type type,
750 enum iio_event_direction dir)
753 struct mma9553_data *data = iio_priv(indio_dev);
754 struct mma9553_event *event;
756 event = mma9553_get_event(data, chan->type, chan->channel2, dir);
760 return event->enabled;
763 static int mma9553_write_event_config(struct iio_dev *indio_dev,
764 const struct iio_chan_spec *chan,
765 enum iio_event_type type,
766 enum iio_event_direction dir, int state)
768 struct mma9553_data *data = iio_priv(indio_dev);
769 struct mma9553_event *event;
772 event = mma9553_get_event(data, chan->type, chan->channel2, dir);
776 if (event->enabled == state)
779 mutex_lock(&data->mutex);
781 ret = mma9551_set_power_state(data->client, state);
784 event->enabled = state;
786 ret = mma9553_conf_gpio(data);
790 mutex_unlock(&data->mutex);
796 event->enabled = false;
797 mma9551_set_power_state(data->client, false);
800 mutex_unlock(&data->mutex);
804 static int mma9553_read_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,
811 struct mma9553_data *data = iio_priv(indio_dev);
815 case IIO_EV_INFO_VALUE:
816 switch (chan->type) {
818 *val = mma9553_get_bits(data->conf.speed_step,
819 MMA9553_MASK_CONF_STEPCOALESCE);
823 * The device does not support confidence value levels.
824 * We set an average of 50%.
831 case IIO_EV_INFO_PERIOD:
832 switch (chan->type) {
834 *val = MMA9553_ACTIVITY_THD_TO_SEC(data->conf.actthd);
844 static int mma9553_write_event_value(struct iio_dev *indio_dev,
845 const struct iio_chan_spec *chan,
846 enum iio_event_type type,
847 enum iio_event_direction dir,
848 enum iio_event_info info,
851 struct mma9553_data *data = iio_priv(indio_dev);
855 case IIO_EV_INFO_VALUE:
856 switch (chan->type) {
858 if (val < 0 || val > 255)
860 mutex_lock(&data->mutex);
861 ret = mma9553_set_config(data,
862 MMA9553_REG_CONF_SPEED_STEP,
863 &data->conf.speed_step, val,
864 MMA9553_MASK_CONF_STEPCOALESCE);
865 mutex_unlock(&data->mutex);
870 case IIO_EV_INFO_PERIOD:
871 switch (chan->type) {
873 if (val < 0 || val > MMA9553_ACTIVITY_THD_TO_SEC(
876 mutex_lock(&data->mutex);
877 ret = mma9553_set_config(data, MMA9553_REG_CONF_ACTTHD,
879 MMA9553_ACTIVITY_SEC_TO_THD
880 (val), MMA9553_MASK_CONF_WORD);
881 mutex_unlock(&data->mutex);
891 static int mma9553_get_calibgender_mode(struct iio_dev *indio_dev,
892 const struct iio_chan_spec *chan)
894 struct mma9553_data *data = iio_priv(indio_dev);
897 gender = mma9553_get_bits(data->conf.filter, MMA9553_MASK_CONF_MALE);
899 * HW expects 0 for female and 1 for male,
900 * while iio index is 0 for male and 1 for female
905 static int mma9553_set_calibgender_mode(struct iio_dev *indio_dev,
906 const struct iio_chan_spec *chan,
909 struct mma9553_data *data = iio_priv(indio_dev);
913 if ((mode != 0) && (mode != 1))
915 mutex_lock(&data->mutex);
916 ret = mma9553_set_config(data, MMA9553_REG_CONF_FILTER,
917 &data->conf.filter, gender,
918 MMA9553_MASK_CONF_MALE);
919 mutex_unlock(&data->mutex);
924 static const struct iio_event_spec mma9553_step_event = {
925 .type = IIO_EV_TYPE_CHANGE,
926 .dir = IIO_EV_DIR_NONE,
927 .mask_separate = BIT(IIO_EV_INFO_ENABLE) | BIT(IIO_EV_INFO_VALUE),
930 static const struct iio_event_spec mma9553_activity_events[] = {
932 .type = IIO_EV_TYPE_THRESH,
933 .dir = IIO_EV_DIR_RISING,
934 .mask_separate = BIT(IIO_EV_INFO_ENABLE) |
935 BIT(IIO_EV_INFO_VALUE) |
936 BIT(IIO_EV_INFO_PERIOD),
939 .type = IIO_EV_TYPE_THRESH,
940 .dir = IIO_EV_DIR_FALLING,
941 .mask_separate = BIT(IIO_EV_INFO_ENABLE) |
942 BIT(IIO_EV_INFO_VALUE) |
943 BIT(IIO_EV_INFO_PERIOD),
947 static const char * const calibgender_modes[] = { "male", "female" };
949 static const struct iio_enum mma9553_calibgender_enum = {
950 .items = calibgender_modes,
951 .num_items = ARRAY_SIZE(calibgender_modes),
952 .get = mma9553_get_calibgender_mode,
953 .set = mma9553_set_calibgender_mode,
956 static const struct iio_chan_spec_ext_info mma9553_ext_info[] = {
957 IIO_ENUM("calibgender", IIO_SHARED_BY_TYPE, &mma9553_calibgender_enum),
958 IIO_ENUM_AVAILABLE("calibgender", &mma9553_calibgender_enum),
962 #define MMA9553_PEDOMETER_CHANNEL(_type, _mask) { \
964 .info_mask_separate = BIT(IIO_CHAN_INFO_ENABLE) | \
965 BIT(IIO_CHAN_INFO_CALIBHEIGHT) | \
967 .ext_info = mma9553_ext_info, \
970 #define MMA9553_ACTIVITY_CHANNEL(_chan2) { \
971 .type = IIO_ACTIVITY, \
973 .channel2 = _chan2, \
974 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
975 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_CALIBHEIGHT) | \
976 BIT(IIO_CHAN_INFO_ENABLE), \
977 .event_spec = mma9553_activity_events, \
978 .num_event_specs = ARRAY_SIZE(mma9553_activity_events), \
979 .ext_info = mma9553_ext_info, \
982 static const struct iio_chan_spec mma9553_channels[] = {
983 MMA9551_ACCEL_CHANNEL(IIO_MOD_X),
984 MMA9551_ACCEL_CHANNEL(IIO_MOD_Y),
985 MMA9551_ACCEL_CHANNEL(IIO_MOD_Z),
989 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
990 BIT(IIO_CHAN_INFO_ENABLE) |
991 BIT(IIO_CHAN_INFO_DEBOUNCE_COUNT) |
992 BIT(IIO_CHAN_INFO_DEBOUNCE_TIME),
993 .event_spec = &mma9553_step_event,
994 .num_event_specs = 1,
997 MMA9553_PEDOMETER_CHANNEL(IIO_DISTANCE, BIT(IIO_CHAN_INFO_PROCESSED)),
999 .type = IIO_VELOCITY,
1001 .channel2 = IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z,
1002 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1003 BIT(IIO_CHAN_INFO_SCALE) |
1004 BIT(IIO_CHAN_INFO_INT_TIME) |
1005 BIT(IIO_CHAN_INFO_ENABLE),
1006 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_CALIBHEIGHT),
1007 .ext_info = mma9553_ext_info,
1009 MMA9553_PEDOMETER_CHANNEL(IIO_ENERGY, BIT(IIO_CHAN_INFO_RAW) |
1010 BIT(IIO_CHAN_INFO_SCALE) |
1011 BIT(IIO_CHAN_INFO_CALIBWEIGHT)),
1013 MMA9553_ACTIVITY_CHANNEL(IIO_MOD_RUNNING),
1014 MMA9553_ACTIVITY_CHANNEL(IIO_MOD_JOGGING),
1015 MMA9553_ACTIVITY_CHANNEL(IIO_MOD_WALKING),
1016 MMA9553_ACTIVITY_CHANNEL(IIO_MOD_STILL),
1019 static const struct iio_info mma9553_info = {
1020 .driver_module = THIS_MODULE,
1021 .read_raw = mma9553_read_raw,
1022 .write_raw = mma9553_write_raw,
1023 .read_event_config = mma9553_read_event_config,
1024 .write_event_config = mma9553_write_event_config,
1025 .read_event_value = mma9553_read_event_value,
1026 .write_event_value = mma9553_write_event_value,
1029 static irqreturn_t mma9553_irq_handler(int irq, void *private)
1031 struct iio_dev *indio_dev = private;
1032 struct mma9553_data *data = iio_priv(indio_dev);
1034 data->timestamp = iio_get_time_ns();
1036 * Since we only configure the interrupt pin when an
1037 * event is enabled, we are sure we have at least
1038 * one event enabled at this point.
1040 return IRQ_WAKE_THREAD;
1043 static irqreturn_t mma9553_event_handler(int irq, void *private)
1045 struct iio_dev *indio_dev = private;
1046 struct mma9553_data *data = iio_priv(indio_dev);
1049 struct mma9553_event *ev_activity, *ev_prev_activity, *ev_step_detect;
1052 mutex_lock(&data->mutex);
1053 ret = mma9553_read_activity_stepcnt(data, &activity, &stepcnt);
1055 mutex_unlock(&data->mutex);
1060 mma9553_get_event(data, IIO_ACTIVITY,
1061 mma9553_activity_to_mod(data->activity),
1062 IIO_EV_DIR_FALLING);
1064 mma9553_get_event(data, IIO_ACTIVITY,
1065 mma9553_activity_to_mod(activity),
1068 mma9553_get_event(data, IIO_STEPS, IIO_NO_MOD, IIO_EV_DIR_NONE);
1070 if (ev_step_detect->enabled && (stepcnt != data->stepcnt)) {
1071 data->stepcnt = stepcnt;
1072 iio_push_event(indio_dev,
1073 IIO_EVENT_CODE(IIO_STEPS, 0, IIO_NO_MOD,
1074 IIO_EV_DIR_NONE, IIO_EV_TYPE_CHANGE, 0, 0, 0),
1078 if (activity != data->activity) {
1079 data->activity = activity;
1080 /* ev_activity can be NULL if activity == ACTIVITY_UNKNOWN */
1081 if (ev_prev_activity && ev_prev_activity->enabled)
1082 iio_push_event(indio_dev,
1083 IIO_EVENT_CODE(IIO_ACTIVITY, 0,
1084 ev_prev_activity->info->mod,
1086 IIO_EV_TYPE_THRESH, 0, 0, 0),
1089 if (ev_activity && ev_activity->enabled)
1090 iio_push_event(indio_dev,
1091 IIO_EVENT_CODE(IIO_ACTIVITY, 0,
1092 ev_activity->info->mod,
1094 IIO_EV_TYPE_THRESH, 0, 0, 0),
1097 mutex_unlock(&data->mutex);
1102 static int mma9553_gpio_probe(struct i2c_client *client)
1105 struct gpio_desc *gpio;
1113 /* data ready gpio interrupt pin */
1114 gpio = devm_gpiod_get_index(dev, MMA9553_GPIO_NAME, 0, GPIOD_IN);
1116 dev_err(dev, "acpi gpio get index failed\n");
1117 return PTR_ERR(gpio);
1120 ret = gpiod_to_irq(gpio);
1122 dev_dbg(dev, "gpio resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret);
1127 static const char *mma9553_match_acpi_device(struct device *dev)
1129 const struct acpi_device_id *id;
1131 id = acpi_match_device(dev->driver->acpi_match_table, dev);
1135 return dev_name(dev);
1138 static int mma9553_probe(struct i2c_client *client,
1139 const struct i2c_device_id *id)
1141 struct mma9553_data *data;
1142 struct iio_dev *indio_dev;
1143 const char *name = NULL;
1146 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
1150 data = iio_priv(indio_dev);
1151 i2c_set_clientdata(client, indio_dev);
1152 data->client = client;
1156 else if (ACPI_HANDLE(&client->dev))
1157 name = mma9553_match_acpi_device(&client->dev);
1161 mutex_init(&data->mutex);
1162 mma9553_init_events(data);
1164 ret = mma9553_init(data);
1168 indio_dev->dev.parent = &client->dev;
1169 indio_dev->channels = mma9553_channels;
1170 indio_dev->num_channels = ARRAY_SIZE(mma9553_channels);
1171 indio_dev->name = name;
1172 indio_dev->modes = INDIO_DIRECT_MODE;
1173 indio_dev->info = &mma9553_info;
1175 if (client->irq < 0)
1176 client->irq = mma9553_gpio_probe(client);
1178 if (client->irq >= 0) {
1179 ret = devm_request_threaded_irq(&client->dev, client->irq,
1180 mma9553_irq_handler,
1181 mma9553_event_handler,
1182 IRQF_TRIGGER_RISING,
1183 MMA9553_IRQ_NAME, indio_dev);
1185 dev_err(&client->dev, "request irq %d failed\n",
1192 ret = iio_device_register(indio_dev);
1194 dev_err(&client->dev, "unable to register iio device\n");
1198 ret = pm_runtime_set_active(&client->dev);
1200 goto out_iio_unregister;
1202 pm_runtime_enable(&client->dev);
1203 pm_runtime_set_autosuspend_delay(&client->dev,
1204 MMA9551_AUTO_SUSPEND_DELAY_MS);
1205 pm_runtime_use_autosuspend(&client->dev);
1207 dev_dbg(&indio_dev->dev, "Registered device %s\n", name);
1212 iio_device_unregister(indio_dev);
1214 mma9551_set_device_state(client, false);
1218 static int mma9553_remove(struct i2c_client *client)
1220 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1221 struct mma9553_data *data = iio_priv(indio_dev);
1223 pm_runtime_disable(&client->dev);
1224 pm_runtime_set_suspended(&client->dev);
1225 pm_runtime_put_noidle(&client->dev);
1227 iio_device_unregister(indio_dev);
1228 mutex_lock(&data->mutex);
1229 mma9551_set_device_state(data->client, false);
1230 mutex_unlock(&data->mutex);
1236 static int mma9553_runtime_suspend(struct device *dev)
1238 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1239 struct mma9553_data *data = iio_priv(indio_dev);
1242 mutex_lock(&data->mutex);
1243 ret = mma9551_set_device_state(data->client, false);
1244 mutex_unlock(&data->mutex);
1246 dev_err(&data->client->dev, "powering off device failed\n");
1253 static int mma9553_runtime_resume(struct device *dev)
1255 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1256 struct mma9553_data *data = iio_priv(indio_dev);
1259 ret = mma9551_set_device_state(data->client, true);
1263 mma9551_sleep(MMA9553_DEFAULT_SAMPLE_RATE);
1269 #ifdef CONFIG_PM_SLEEP
1270 static int mma9553_suspend(struct device *dev)
1272 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1273 struct mma9553_data *data = iio_priv(indio_dev);
1276 mutex_lock(&data->mutex);
1277 ret = mma9551_set_device_state(data->client, false);
1278 mutex_unlock(&data->mutex);
1283 static int mma9553_resume(struct device *dev)
1285 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1286 struct mma9553_data *data = iio_priv(indio_dev);
1289 mutex_lock(&data->mutex);
1290 ret = mma9551_set_device_state(data->client, true);
1291 mutex_unlock(&data->mutex);
1297 static const struct dev_pm_ops mma9553_pm_ops = {
1298 SET_SYSTEM_SLEEP_PM_OPS(mma9553_suspend, mma9553_resume)
1299 SET_RUNTIME_PM_OPS(mma9553_runtime_suspend,
1300 mma9553_runtime_resume, NULL)
1303 static const struct acpi_device_id mma9553_acpi_match[] = {
1308 MODULE_DEVICE_TABLE(acpi, mma9553_acpi_match);
1310 static const struct i2c_device_id mma9553_id[] = {
1315 MODULE_DEVICE_TABLE(i2c, mma9553_id);
1317 static struct i2c_driver mma9553_driver = {
1319 .name = MMA9553_DRV_NAME,
1320 .acpi_match_table = ACPI_PTR(mma9553_acpi_match),
1321 .pm = &mma9553_pm_ops,
1323 .probe = mma9553_probe,
1324 .remove = mma9553_remove,
1325 .id_table = mma9553_id,
1328 module_i2c_driver(mma9553_driver);
1330 MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>");
1331 MODULE_LICENSE("GPL v2");
1332 MODULE_DESCRIPTION("MMA9553L pedometer platform driver");