1 // SPDX-License-Identifier: GPL-2.0-only
3 * Freescale MMA9553L Intelligent Pedometer driver
4 * Copyright (c) 2014, Intel Corporation.
7 #include <linux/module.h>
9 #include <linux/interrupt.h>
10 #include <linux/slab.h>
11 #include <linux/acpi.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"
18 #define MMA9553_DRV_NAME "mma9553"
19 #define MMA9553_IRQ_NAME "mma9553_event"
21 /* Pedometer configuration registers (R/W) */
22 #define MMA9553_REG_CONF_SLEEPMIN 0x00
23 #define MMA9553_REG_CONF_SLEEPMAX 0x02
24 #define MMA9553_REG_CONF_SLEEPTHD 0x04
25 #define MMA9553_MASK_CONF_WORD GENMASK(15, 0)
27 #define MMA9553_REG_CONF_CONF_STEPLEN 0x06
28 #define MMA9553_MASK_CONF_CONFIG BIT(15)
29 #define MMA9553_MASK_CONF_ACT_DBCNTM BIT(14)
30 #define MMA9553_MASK_CONF_SLP_DBCNTM BIT(13)
31 #define MMA9553_MASK_CONF_STEPLEN GENMASK(7, 0)
33 #define MMA9553_REG_CONF_HEIGHT_WEIGHT 0x08
34 #define MMA9553_MASK_CONF_HEIGHT GENMASK(15, 8)
35 #define MMA9553_MASK_CONF_WEIGHT GENMASK(7, 0)
37 #define MMA9553_REG_CONF_FILTER 0x0A
38 #define MMA9553_MASK_CONF_FILTSTEP GENMASK(15, 8)
39 #define MMA9553_MASK_CONF_MALE BIT(7)
40 #define MMA9553_MASK_CONF_FILTTIME GENMASK(6, 0)
42 #define MMA9553_REG_CONF_SPEED_STEP 0x0C
43 #define MMA9553_MASK_CONF_SPDPRD GENMASK(15, 8)
44 #define MMA9553_MASK_CONF_STEPCOALESCE GENMASK(7, 0)
46 #define MMA9553_REG_CONF_ACTTHD 0x0E
47 #define MMA9553_MAX_ACTTHD GENMASK(15, 0)
49 /* Pedometer status registers (R-only) */
50 #define MMA9553_REG_STATUS 0x00
51 #define MMA9553_MASK_STATUS_MRGFL BIT(15)
52 #define MMA9553_MASK_STATUS_SUSPCHG BIT(14)
53 #define MMA9553_MASK_STATUS_STEPCHG BIT(13)
54 #define MMA9553_MASK_STATUS_ACTCHG BIT(12)
55 #define MMA9553_MASK_STATUS_SUSP BIT(11)
56 #define MMA9553_MASK_STATUS_ACTIVITY GENMASK(10, 8)
57 #define MMA9553_MASK_STATUS_VERSION GENMASK(7, 0)
59 #define MMA9553_REG_STEPCNT 0x02
60 #define MMA9553_REG_DISTANCE 0x04
61 #define MMA9553_REG_SPEED 0x06
62 #define MMA9553_REG_CALORIES 0x08
63 #define MMA9553_REG_SLEEPCNT 0x0A
65 /* Pedometer events are always mapped to this pin. */
66 #define MMA9553_DEFAULT_GPIO_PIN mma9551_gpio6
67 #define MMA9553_DEFAULT_GPIO_POLARITY 0
69 /* Bitnum used for GPIO configuration = bit number in high status byte */
70 #define MMA9553_STATUS_TO_BITNUM(bit) (ffs(bit) - 9)
71 #define MMA9553_MAX_BITNUM MMA9553_STATUS_TO_BITNUM(BIT(16))
73 #define MMA9553_DEFAULT_SAMPLE_RATE 30 /* Hz */
76 * The internal activity level must be stable for ACTTHD samples before
77 * ACTIVITY is updated. The ACTIVITY variable contains the current activity
78 * level and is updated every time a step is detected or once a second
79 * if there are no steps.
81 #define MMA9553_ACTIVITY_THD_TO_SEC(thd) ((thd) / MMA9553_DEFAULT_SAMPLE_RATE)
82 #define MMA9553_ACTIVITY_SEC_TO_THD(sec) ((sec) * MMA9553_DEFAULT_SAMPLE_RATE)
85 * Autonomously suspend pedometer if acceleration vector magnitude
86 * is near 1g (4096 at 0.244 mg/LSB resolution) for 30 seconds.
88 #define MMA9553_DEFAULT_SLEEPMIN 3688 /* 0,9 g */
89 #define MMA9553_DEFAULT_SLEEPMAX 4508 /* 1,1 g */
90 #define MMA9553_DEFAULT_SLEEPTHD (MMA9553_DEFAULT_SAMPLE_RATE * 30)
92 #define MMA9553_CONFIG_RETRIES 2
94 /* Status register - activity field */
103 static struct mma9553_event_info
{
104 enum iio_chan_type type
;
105 enum iio_modifier mod
;
106 enum iio_event_direction dir
;
107 } mma9553_events_info
[] = {
111 .dir
= IIO_EV_DIR_NONE
,
114 .type
= IIO_ACTIVITY
,
115 .mod
= IIO_MOD_STILL
,
116 .dir
= IIO_EV_DIR_RISING
,
119 .type
= IIO_ACTIVITY
,
120 .mod
= IIO_MOD_STILL
,
121 .dir
= IIO_EV_DIR_FALLING
,
124 .type
= IIO_ACTIVITY
,
125 .mod
= IIO_MOD_WALKING
,
126 .dir
= IIO_EV_DIR_RISING
,
129 .type
= IIO_ACTIVITY
,
130 .mod
= IIO_MOD_WALKING
,
131 .dir
= IIO_EV_DIR_FALLING
,
134 .type
= IIO_ACTIVITY
,
135 .mod
= IIO_MOD_JOGGING
,
136 .dir
= IIO_EV_DIR_RISING
,
139 .type
= IIO_ACTIVITY
,
140 .mod
= IIO_MOD_JOGGING
,
141 .dir
= IIO_EV_DIR_FALLING
,
144 .type
= IIO_ACTIVITY
,
145 .mod
= IIO_MOD_RUNNING
,
146 .dir
= IIO_EV_DIR_RISING
,
149 .type
= IIO_ACTIVITY
,
150 .mod
= IIO_MOD_RUNNING
,
151 .dir
= IIO_EV_DIR_FALLING
,
155 #define MMA9553_EVENTS_INFO_SIZE ARRAY_SIZE(mma9553_events_info)
157 struct mma9553_event
{
158 struct mma9553_event_info
*info
;
162 struct mma9553_conf_regs
{
173 struct mma9553_data
{
174 struct i2c_client
*client
;
176 * 1. Serialize access to HW (requested by mma9551_core API).
177 * 2. Serialize sequences that power on/off the device and access HW.
180 struct mma9553_conf_regs conf
;
181 struct mma9553_event events
[MMA9553_EVENTS_INFO_SIZE
];
185 * This is used for all features that depend on step count:
186 * step count, distance, speed, calories.
188 bool stepcnt_enabled
;
194 static u8
mma9553_get_bits(u16 val
, u16 mask
)
196 return (val
& mask
) >> (ffs(mask
) - 1);
199 static u16
mma9553_set_bits(u16 current_val
, u16 val
, u16 mask
)
201 return (current_val
& ~mask
) | (val
<< (ffs(mask
) - 1));
204 static enum iio_modifier
mma9553_activity_to_mod(enum activity_level activity
)
207 case ACTIVITY_RUNNING
:
208 return IIO_MOD_RUNNING
;
209 case ACTIVITY_JOGGING
:
210 return IIO_MOD_JOGGING
;
211 case ACTIVITY_WALKING
:
212 return IIO_MOD_WALKING
;
214 return IIO_MOD_STILL
;
215 case ACTIVITY_UNKNOWN
:
221 static void mma9553_init_events(struct mma9553_data
*data
)
225 data
->num_events
= MMA9553_EVENTS_INFO_SIZE
;
226 for (i
= 0; i
< data
->num_events
; i
++) {
227 data
->events
[i
].info
= &mma9553_events_info
[i
];
228 data
->events
[i
].enabled
= false;
232 static struct mma9553_event
*mma9553_get_event(struct mma9553_data
*data
,
233 enum iio_chan_type type
,
234 enum iio_modifier mod
,
235 enum iio_event_direction dir
)
239 for (i
= 0; i
< data
->num_events
; i
++)
240 if (data
->events
[i
].info
->type
== type
&&
241 data
->events
[i
].info
->mod
== mod
&&
242 data
->events
[i
].info
->dir
== dir
)
243 return &data
->events
[i
];
248 static bool mma9553_is_any_event_enabled(struct mma9553_data
*data
,
250 enum iio_chan_type type
)
254 for (i
= 0; i
< data
->num_events
; i
++)
255 if ((check_type
&& data
->events
[i
].info
->type
== type
&&
256 data
->events
[i
].enabled
) ||
257 (!check_type
&& data
->events
[i
].enabled
))
263 static int mma9553_set_config(struct mma9553_data
*data
, u16 reg
,
264 u16
*p_reg_val
, u16 val
, u16 mask
)
269 reg_val
= *p_reg_val
;
270 if (val
== mma9553_get_bits(reg_val
, mask
))
273 reg_val
= mma9553_set_bits(reg_val
, val
, mask
);
274 ret
= mma9551_write_config_word(data
->client
, MMA9551_APPID_PEDOMETER
,
277 dev_err(&data
->client
->dev
,
278 "error writing config register 0x%x\n", reg
);
282 *p_reg_val
= reg_val
;
284 /* Reinitializes the pedometer with current configuration values */
285 config
= mma9553_set_bits(data
->conf
.config
, 1,
286 MMA9553_MASK_CONF_CONFIG
);
288 ret
= mma9551_write_config_word(data
->client
, MMA9551_APPID_PEDOMETER
,
289 MMA9553_REG_CONF_CONF_STEPLEN
, config
);
291 dev_err(&data
->client
->dev
,
292 "error writing config register 0x%x\n",
293 MMA9553_REG_CONF_CONF_STEPLEN
);
297 retries
= MMA9553_CONFIG_RETRIES
;
299 mma9551_sleep(MMA9553_DEFAULT_SAMPLE_RATE
);
300 ret
= mma9551_read_config_word(data
->client
,
301 MMA9551_APPID_PEDOMETER
,
302 MMA9553_REG_CONF_CONF_STEPLEN
,
306 } while (mma9553_get_bits(config
, MMA9553_MASK_CONF_CONFIG
) &&
312 static int mma9553_read_activity_stepcnt(struct mma9553_data
*data
,
313 u8
*activity
, u16
*stepcnt
)
318 ret
= mma9551_read_status_words(data
->client
, MMA9551_APPID_PEDOMETER
,
319 MMA9553_REG_STATUS
, ARRAY_SIZE(buf
),
322 dev_err(&data
->client
->dev
,
323 "error reading status and stepcnt\n");
327 *activity
= mma9553_get_bits(buf
[0], MMA9553_MASK_STATUS_ACTIVITY
);
333 static int mma9553_conf_gpio(struct mma9553_data
*data
)
335 u8 bitnum
= 0, appid
= MMA9551_APPID_PEDOMETER
;
337 struct mma9553_event
*ev_step_detect
;
338 bool activity_enabled
;
340 activity_enabled
= mma9553_is_any_event_enabled(data
, true,
342 ev_step_detect
= mma9553_get_event(data
, IIO_STEPS
, IIO_NO_MOD
,
346 * If both step detector and activity are enabled, use the MRGFL bit.
347 * This bit is the logical OR of the SUSPCHG, STEPCHG, and ACTCHG flags.
349 if (activity_enabled
&& ev_step_detect
->enabled
)
350 bitnum
= MMA9553_STATUS_TO_BITNUM(MMA9553_MASK_STATUS_MRGFL
);
351 else if (ev_step_detect
->enabled
)
352 bitnum
= MMA9553_STATUS_TO_BITNUM(MMA9553_MASK_STATUS_STEPCHG
);
353 else if (activity_enabled
)
354 bitnum
= MMA9553_STATUS_TO_BITNUM(MMA9553_MASK_STATUS_ACTCHG
);
356 appid
= MMA9551_APPID_NONE
;
358 if (data
->gpio_bitnum
== bitnum
)
361 /* Save initial values for activity and stepcnt */
362 if (activity_enabled
|| ev_step_detect
->enabled
) {
363 ret
= mma9553_read_activity_stepcnt(data
, &data
->activity
,
369 ret
= mma9551_gpio_config(data
->client
, MMA9553_DEFAULT_GPIO_PIN
, appid
,
370 bitnum
, MMA9553_DEFAULT_GPIO_POLARITY
);
373 data
->gpio_bitnum
= bitnum
;
378 static int mma9553_init(struct mma9553_data
*data
)
382 ret
= mma9551_read_version(data
->client
);
387 * Read all the pedometer configuration registers. This is used as
388 * a device identification command to differentiate the MMA9553L
391 ret
= mma9551_read_config_words(data
->client
, MMA9551_APPID_PEDOMETER
,
392 MMA9553_REG_CONF_SLEEPMIN
,
393 sizeof(data
->conf
) / sizeof(u16
),
396 dev_err(&data
->client
->dev
,
397 "failed to read configuration registers\n");
402 data
->gpio_bitnum
= MMA9553_MAX_BITNUM
;
403 ret
= mma9553_conf_gpio(data
);
407 ret
= mma9551_app_reset(data
->client
, MMA9551_RSC_PED
);
411 /* Init config registers */
412 data
->conf
.sleepmin
= MMA9553_DEFAULT_SLEEPMIN
;
413 data
->conf
.sleepmax
= MMA9553_DEFAULT_SLEEPMAX
;
414 data
->conf
.sleepthd
= MMA9553_DEFAULT_SLEEPTHD
;
415 data
->conf
.config
= mma9553_set_bits(data
->conf
.config
, 1,
416 MMA9553_MASK_CONF_CONFIG
);
418 * Clear the activity debounce counter when the activity level changes,
419 * so that the confidence level applies for any activity level.
421 data
->conf
.config
= mma9553_set_bits(data
->conf
.config
, 1,
422 MMA9553_MASK_CONF_ACT_DBCNTM
);
423 ret
= mma9551_write_config_words(data
->client
, MMA9551_APPID_PEDOMETER
,
424 MMA9553_REG_CONF_SLEEPMIN
,
425 sizeof(data
->conf
) / sizeof(u16
),
428 dev_err(&data
->client
->dev
,
429 "failed to write configuration registers\n");
433 return mma9551_set_device_state(data
->client
, true);
436 static int mma9553_read_status_word(struct mma9553_data
*data
, u16 reg
,
443 * The HW only counts steps and other dependent
444 * parameters (speed, distance, calories, activity)
445 * if power is on (from enabling an event or the
448 powered_on
= mma9553_is_any_event_enabled(data
, false, 0) ||
449 data
->stepcnt_enabled
;
451 dev_err(&data
->client
->dev
, "No channels enabled\n");
455 mutex_lock(&data
->mutex
);
456 ret
= mma9551_read_status_word(data
->client
, MMA9551_APPID_PEDOMETER
,
458 mutex_unlock(&data
->mutex
);
462 static int mma9553_read_raw(struct iio_dev
*indio_dev
,
463 struct iio_chan_spec
const *chan
,
464 int *val
, int *val2
, long mask
)
466 struct mma9553_data
*data
= iio_priv(indio_dev
);
472 case IIO_CHAN_INFO_PROCESSED
:
473 switch (chan
->type
) {
475 ret
= mma9553_read_status_word(data
,
483 ret
= mma9553_read_status_word(data
,
484 MMA9553_REG_DISTANCE
,
491 ret
= mma9553_read_status_word(data
,
498 mma9553_get_bits(tmp
, MMA9553_MASK_STATUS_ACTIVITY
);
501 * The device does not support confidence value levels,
502 * so we will always have 100% for current activity and
505 if (chan
->channel2
== mma9553_activity_to_mod(activity
))
513 case IIO_CHAN_INFO_RAW
:
514 switch (chan
->type
) {
515 case IIO_VELOCITY
: /* m/h */
516 if (chan
->channel2
!= IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z
)
518 ret
= mma9553_read_status_word(data
,
525 case IIO_ENERGY
: /* Cal or kcal */
526 ret
= mma9553_read_status_word(data
,
527 MMA9553_REG_CALORIES
,
534 mutex_lock(&data
->mutex
);
535 ret
= mma9551_read_accel_chan(data
->client
,
537 mutex_unlock(&data
->mutex
);
542 case IIO_CHAN_INFO_SCALE
:
543 switch (chan
->type
) {
544 case IIO_VELOCITY
: /* m/h to m/s */
545 if (chan
->channel2
!= IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z
)
548 *val2
= 277; /* 0.000277 */
549 return IIO_VAL_INT_PLUS_MICRO
;
550 case IIO_ENERGY
: /* Cal or kcal to J */
554 return mma9551_read_accel_scale(val
, val2
);
558 case IIO_CHAN_INFO_ENABLE
:
559 *val
= data
->stepcnt_enabled
;
561 case IIO_CHAN_INFO_CALIBHEIGHT
:
562 tmp
= mma9553_get_bits(data
->conf
.height_weight
,
563 MMA9553_MASK_CONF_HEIGHT
);
564 *val
= tmp
/ 100; /* cm to m */
565 *val2
= (tmp
% 100) * 10000;
566 return IIO_VAL_INT_PLUS_MICRO
;
567 case IIO_CHAN_INFO_CALIBWEIGHT
:
568 *val
= mma9553_get_bits(data
->conf
.height_weight
,
569 MMA9553_MASK_CONF_WEIGHT
);
571 case IIO_CHAN_INFO_DEBOUNCE_COUNT
:
572 switch (chan
->type
) {
574 *val
= mma9553_get_bits(data
->conf
.filter
,
575 MMA9553_MASK_CONF_FILTSTEP
);
580 case IIO_CHAN_INFO_DEBOUNCE_TIME
:
581 switch (chan
->type
) {
583 *val
= mma9553_get_bits(data
->conf
.filter
,
584 MMA9553_MASK_CONF_FILTTIME
);
589 case IIO_CHAN_INFO_INT_TIME
:
590 switch (chan
->type
) {
592 if (chan
->channel2
!= IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z
)
594 *val
= mma9553_get_bits(data
->conf
.speed_step
,
595 MMA9553_MASK_CONF_SPDPRD
);
605 static int mma9553_write_raw(struct iio_dev
*indio_dev
,
606 struct iio_chan_spec
const *chan
,
607 int val
, int val2
, long mask
)
609 struct mma9553_data
*data
= iio_priv(indio_dev
);
613 case IIO_CHAN_INFO_ENABLE
:
614 if (data
->stepcnt_enabled
== !!val
)
616 mutex_lock(&data
->mutex
);
617 ret
= mma9551_set_power_state(data
->client
, val
);
619 mutex_unlock(&data
->mutex
);
622 data
->stepcnt_enabled
= val
;
623 mutex_unlock(&data
->mutex
);
625 case IIO_CHAN_INFO_CALIBHEIGHT
:
627 tmp
= val
* 100 + val2
/ 10000;
628 if (tmp
< 0 || tmp
> 255)
630 mutex_lock(&data
->mutex
);
631 ret
= mma9553_set_config(data
,
632 MMA9553_REG_CONF_HEIGHT_WEIGHT
,
633 &data
->conf
.height_weight
,
634 tmp
, MMA9553_MASK_CONF_HEIGHT
);
635 mutex_unlock(&data
->mutex
);
637 case IIO_CHAN_INFO_CALIBWEIGHT
:
638 if (val
< 0 || val
> 255)
640 mutex_lock(&data
->mutex
);
641 ret
= mma9553_set_config(data
,
642 MMA9553_REG_CONF_HEIGHT_WEIGHT
,
643 &data
->conf
.height_weight
,
644 val
, MMA9553_MASK_CONF_WEIGHT
);
645 mutex_unlock(&data
->mutex
);
647 case IIO_CHAN_INFO_DEBOUNCE_COUNT
:
648 switch (chan
->type
) {
651 * Set to 0 to disable step filtering. If the value
652 * specified is greater than 6, then 6 will be used.
658 mutex_lock(&data
->mutex
);
659 ret
= mma9553_set_config(data
, MMA9553_REG_CONF_FILTER
,
660 &data
->conf
.filter
, val
,
661 MMA9553_MASK_CONF_FILTSTEP
);
662 mutex_unlock(&data
->mutex
);
667 case IIO_CHAN_INFO_DEBOUNCE_TIME
:
668 switch (chan
->type
) {
670 if (val
< 0 || val
> 127)
672 mutex_lock(&data
->mutex
);
673 ret
= mma9553_set_config(data
, MMA9553_REG_CONF_FILTER
,
674 &data
->conf
.filter
, val
,
675 MMA9553_MASK_CONF_FILTTIME
);
676 mutex_unlock(&data
->mutex
);
681 case IIO_CHAN_INFO_INT_TIME
:
682 switch (chan
->type
) {
684 if (chan
->channel2
!= IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z
)
687 * If set to a value greater than 5, then 5 will be
688 * used. Warning: Do not set SPDPRD to 0 or 1 as
689 * this may cause undesirable behavior.
695 mutex_lock(&data
->mutex
);
696 ret
= mma9553_set_config(data
,
697 MMA9553_REG_CONF_SPEED_STEP
,
698 &data
->conf
.speed_step
, val
,
699 MMA9553_MASK_CONF_SPDPRD
);
700 mutex_unlock(&data
->mutex
);
710 static int mma9553_read_event_config(struct iio_dev
*indio_dev
,
711 const struct iio_chan_spec
*chan
,
712 enum iio_event_type type
,
713 enum iio_event_direction dir
)
715 struct mma9553_data
*data
= iio_priv(indio_dev
);
716 struct mma9553_event
*event
;
718 event
= mma9553_get_event(data
, chan
->type
, chan
->channel2
, dir
);
722 return event
->enabled
;
725 static int mma9553_write_event_config(struct iio_dev
*indio_dev
,
726 const struct iio_chan_spec
*chan
,
727 enum iio_event_type type
,
728 enum iio_event_direction dir
, int state
)
730 struct mma9553_data
*data
= iio_priv(indio_dev
);
731 struct mma9553_event
*event
;
734 event
= mma9553_get_event(data
, chan
->type
, chan
->channel2
, dir
);
738 if (event
->enabled
== state
)
741 mutex_lock(&data
->mutex
);
743 ret
= mma9551_set_power_state(data
->client
, state
);
746 event
->enabled
= state
;
748 ret
= mma9553_conf_gpio(data
);
752 mutex_unlock(&data
->mutex
);
758 event
->enabled
= false;
759 mma9551_set_power_state(data
->client
, false);
762 mutex_unlock(&data
->mutex
);
766 static int mma9553_read_event_value(struct iio_dev
*indio_dev
,
767 const struct iio_chan_spec
*chan
,
768 enum iio_event_type type
,
769 enum iio_event_direction dir
,
770 enum iio_event_info info
,
773 struct mma9553_data
*data
= iio_priv(indio_dev
);
777 case IIO_EV_INFO_VALUE
:
778 switch (chan
->type
) {
780 *val
= mma9553_get_bits(data
->conf
.speed_step
,
781 MMA9553_MASK_CONF_STEPCOALESCE
);
785 * The device does not support confidence value levels.
786 * We set an average of 50%.
793 case IIO_EV_INFO_PERIOD
:
794 switch (chan
->type
) {
796 *val
= MMA9553_ACTIVITY_THD_TO_SEC(data
->conf
.actthd
);
806 static int mma9553_write_event_value(struct iio_dev
*indio_dev
,
807 const struct iio_chan_spec
*chan
,
808 enum iio_event_type type
,
809 enum iio_event_direction dir
,
810 enum iio_event_info info
,
813 struct mma9553_data
*data
= iio_priv(indio_dev
);
817 case IIO_EV_INFO_VALUE
:
818 switch (chan
->type
) {
820 if (val
< 0 || val
> 255)
822 mutex_lock(&data
->mutex
);
823 ret
= mma9553_set_config(data
,
824 MMA9553_REG_CONF_SPEED_STEP
,
825 &data
->conf
.speed_step
, val
,
826 MMA9553_MASK_CONF_STEPCOALESCE
);
827 mutex_unlock(&data
->mutex
);
832 case IIO_EV_INFO_PERIOD
:
833 switch (chan
->type
) {
835 if (val
< 0 || val
> MMA9553_ACTIVITY_THD_TO_SEC(
838 mutex_lock(&data
->mutex
);
839 ret
= mma9553_set_config(data
, MMA9553_REG_CONF_ACTTHD
,
841 MMA9553_ACTIVITY_SEC_TO_THD
842 (val
), MMA9553_MASK_CONF_WORD
);
843 mutex_unlock(&data
->mutex
);
853 static int mma9553_get_calibgender_mode(struct iio_dev
*indio_dev
,
854 const struct iio_chan_spec
*chan
)
856 struct mma9553_data
*data
= iio_priv(indio_dev
);
859 gender
= mma9553_get_bits(data
->conf
.filter
, MMA9553_MASK_CONF_MALE
);
861 * HW expects 0 for female and 1 for male,
862 * while iio index is 0 for male and 1 for female.
867 static int mma9553_set_calibgender_mode(struct iio_dev
*indio_dev
,
868 const struct iio_chan_spec
*chan
,
871 struct mma9553_data
*data
= iio_priv(indio_dev
);
875 if ((mode
!= 0) && (mode
!= 1))
877 mutex_lock(&data
->mutex
);
878 ret
= mma9553_set_config(data
, MMA9553_REG_CONF_FILTER
,
879 &data
->conf
.filter
, gender
,
880 MMA9553_MASK_CONF_MALE
);
881 mutex_unlock(&data
->mutex
);
886 static const struct iio_event_spec mma9553_step_event
= {
887 .type
= IIO_EV_TYPE_CHANGE
,
888 .dir
= IIO_EV_DIR_NONE
,
889 .mask_separate
= BIT(IIO_EV_INFO_ENABLE
) | BIT(IIO_EV_INFO_VALUE
),
892 static const struct iio_event_spec mma9553_activity_events
[] = {
894 .type
= IIO_EV_TYPE_THRESH
,
895 .dir
= IIO_EV_DIR_RISING
,
896 .mask_separate
= BIT(IIO_EV_INFO_ENABLE
) |
897 BIT(IIO_EV_INFO_VALUE
) |
898 BIT(IIO_EV_INFO_PERIOD
),
901 .type
= IIO_EV_TYPE_THRESH
,
902 .dir
= IIO_EV_DIR_FALLING
,
903 .mask_separate
= BIT(IIO_EV_INFO_ENABLE
) |
904 BIT(IIO_EV_INFO_VALUE
) |
905 BIT(IIO_EV_INFO_PERIOD
),
909 static const char * const mma9553_calibgender_modes
[] = { "male", "female" };
911 static const struct iio_enum mma9553_calibgender_enum
= {
912 .items
= mma9553_calibgender_modes
,
913 .num_items
= ARRAY_SIZE(mma9553_calibgender_modes
),
914 .get
= mma9553_get_calibgender_mode
,
915 .set
= mma9553_set_calibgender_mode
,
918 static const struct iio_chan_spec_ext_info mma9553_ext_info
[] = {
919 IIO_ENUM("calibgender", IIO_SHARED_BY_TYPE
, &mma9553_calibgender_enum
),
920 IIO_ENUM_AVAILABLE("calibgender", IIO_SHARED_BY_TYPE
, &mma9553_calibgender_enum
),
924 #define MMA9553_PEDOMETER_CHANNEL(_type, _mask) { \
926 .info_mask_separate = BIT(IIO_CHAN_INFO_ENABLE) | \
927 BIT(IIO_CHAN_INFO_CALIBHEIGHT) | \
929 .ext_info = mma9553_ext_info, \
932 #define MMA9553_ACTIVITY_CHANNEL(_chan2) { \
933 .type = IIO_ACTIVITY, \
935 .channel2 = _chan2, \
936 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
937 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_CALIBHEIGHT) | \
938 BIT(IIO_CHAN_INFO_ENABLE), \
939 .event_spec = mma9553_activity_events, \
940 .num_event_specs = ARRAY_SIZE(mma9553_activity_events), \
941 .ext_info = mma9553_ext_info, \
944 static const struct iio_chan_spec mma9553_channels
[] = {
945 MMA9551_ACCEL_CHANNEL(IIO_MOD_X
),
946 MMA9551_ACCEL_CHANNEL(IIO_MOD_Y
),
947 MMA9551_ACCEL_CHANNEL(IIO_MOD_Z
),
951 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
) |
952 BIT(IIO_CHAN_INFO_ENABLE
) |
953 BIT(IIO_CHAN_INFO_DEBOUNCE_COUNT
) |
954 BIT(IIO_CHAN_INFO_DEBOUNCE_TIME
),
955 .event_spec
= &mma9553_step_event
,
956 .num_event_specs
= 1,
959 MMA9553_PEDOMETER_CHANNEL(IIO_DISTANCE
, BIT(IIO_CHAN_INFO_PROCESSED
)),
961 .type
= IIO_VELOCITY
,
963 .channel2
= IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z
,
964 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
965 BIT(IIO_CHAN_INFO_SCALE
) |
966 BIT(IIO_CHAN_INFO_INT_TIME
) |
967 BIT(IIO_CHAN_INFO_ENABLE
),
968 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_CALIBHEIGHT
),
969 .ext_info
= mma9553_ext_info
,
971 MMA9553_PEDOMETER_CHANNEL(IIO_ENERGY
, BIT(IIO_CHAN_INFO_RAW
) |
972 BIT(IIO_CHAN_INFO_SCALE
) |
973 BIT(IIO_CHAN_INFO_CALIBWEIGHT
)),
975 MMA9553_ACTIVITY_CHANNEL(IIO_MOD_RUNNING
),
976 MMA9553_ACTIVITY_CHANNEL(IIO_MOD_JOGGING
),
977 MMA9553_ACTIVITY_CHANNEL(IIO_MOD_WALKING
),
978 MMA9553_ACTIVITY_CHANNEL(IIO_MOD_STILL
),
981 static const struct iio_info mma9553_info
= {
982 .read_raw
= mma9553_read_raw
,
983 .write_raw
= mma9553_write_raw
,
984 .read_event_config
= mma9553_read_event_config
,
985 .write_event_config
= mma9553_write_event_config
,
986 .read_event_value
= mma9553_read_event_value
,
987 .write_event_value
= mma9553_write_event_value
,
990 static irqreturn_t
mma9553_irq_handler(int irq
, void *private)
992 struct iio_dev
*indio_dev
= private;
993 struct mma9553_data
*data
= iio_priv(indio_dev
);
995 data
->timestamp
= iio_get_time_ns(indio_dev
);
997 * Since we only configure the interrupt pin when an
998 * event is enabled, we are sure we have at least
999 * one event enabled at this point.
1001 return IRQ_WAKE_THREAD
;
1004 static irqreturn_t
mma9553_event_handler(int irq
, void *private)
1006 struct iio_dev
*indio_dev
= private;
1007 struct mma9553_data
*data
= iio_priv(indio_dev
);
1010 struct mma9553_event
*ev_activity
, *ev_prev_activity
, *ev_step_detect
;
1013 mutex_lock(&data
->mutex
);
1014 ret
= mma9553_read_activity_stepcnt(data
, &activity
, &stepcnt
);
1016 mutex_unlock(&data
->mutex
);
1020 ev_prev_activity
= mma9553_get_event(data
, IIO_ACTIVITY
,
1021 mma9553_activity_to_mod(
1023 IIO_EV_DIR_FALLING
);
1024 ev_activity
= mma9553_get_event(data
, IIO_ACTIVITY
,
1025 mma9553_activity_to_mod(activity
),
1027 ev_step_detect
= mma9553_get_event(data
, IIO_STEPS
, IIO_NO_MOD
,
1030 if (ev_step_detect
->enabled
&& (stepcnt
!= data
->stepcnt
)) {
1031 data
->stepcnt
= stepcnt
;
1032 iio_push_event(indio_dev
,
1033 IIO_EVENT_CODE(IIO_STEPS
, 0, IIO_NO_MOD
,
1035 IIO_EV_TYPE_CHANGE
, 0, 0, 0),
1039 if (activity
!= data
->activity
) {
1040 data
->activity
= activity
;
1041 /* ev_activity can be NULL if activity == ACTIVITY_UNKNOWN */
1042 if (ev_prev_activity
&& ev_prev_activity
->enabled
)
1043 iio_push_event(indio_dev
,
1044 IIO_EVENT_CODE(IIO_ACTIVITY
, 0,
1045 ev_prev_activity
->info
->mod
,
1047 IIO_EV_TYPE_THRESH
, 0, 0,
1051 if (ev_activity
&& ev_activity
->enabled
)
1052 iio_push_event(indio_dev
,
1053 IIO_EVENT_CODE(IIO_ACTIVITY
, 0,
1054 ev_activity
->info
->mod
,
1056 IIO_EV_TYPE_THRESH
, 0, 0,
1060 mutex_unlock(&data
->mutex
);
1065 static const char *mma9553_match_acpi_device(struct device
*dev
)
1067 const struct acpi_device_id
*id
;
1069 id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
1073 return dev_name(dev
);
1076 static int mma9553_probe(struct i2c_client
*client
)
1078 const struct i2c_device_id
*id
= i2c_client_get_device_id(client
);
1079 struct mma9553_data
*data
;
1080 struct iio_dev
*indio_dev
;
1081 const char *name
= NULL
;
1084 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
1088 data
= iio_priv(indio_dev
);
1089 i2c_set_clientdata(client
, indio_dev
);
1090 data
->client
= client
;
1094 else if (ACPI_HANDLE(&client
->dev
))
1095 name
= mma9553_match_acpi_device(&client
->dev
);
1099 mutex_init(&data
->mutex
);
1100 mma9553_init_events(data
);
1102 ret
= mma9553_init(data
);
1106 indio_dev
->channels
= mma9553_channels
;
1107 indio_dev
->num_channels
= ARRAY_SIZE(mma9553_channels
);
1108 indio_dev
->name
= name
;
1109 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1110 indio_dev
->info
= &mma9553_info
;
1112 if (client
->irq
> 0) {
1113 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1114 mma9553_irq_handler
,
1115 mma9553_event_handler
,
1116 IRQF_TRIGGER_RISING
,
1117 MMA9553_IRQ_NAME
, indio_dev
);
1119 dev_err(&client
->dev
, "request irq %d failed\n",
1125 ret
= pm_runtime_set_active(&client
->dev
);
1129 pm_runtime_enable(&client
->dev
);
1130 pm_runtime_set_autosuspend_delay(&client
->dev
,
1131 MMA9551_AUTO_SUSPEND_DELAY_MS
);
1132 pm_runtime_use_autosuspend(&client
->dev
);
1134 ret
= iio_device_register(indio_dev
);
1136 dev_err(&client
->dev
, "unable to register iio device\n");
1137 goto err_pm_cleanup
;
1140 dev_dbg(&indio_dev
->dev
, "Registered device %s\n", name
);
1144 pm_runtime_dont_use_autosuspend(&client
->dev
);
1145 pm_runtime_disable(&client
->dev
);
1147 mma9551_set_device_state(client
, false);
1151 static void mma9553_remove(struct i2c_client
*client
)
1153 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
1154 struct mma9553_data
*data
= iio_priv(indio_dev
);
1156 iio_device_unregister(indio_dev
);
1158 pm_runtime_disable(&client
->dev
);
1159 pm_runtime_set_suspended(&client
->dev
);
1161 mutex_lock(&data
->mutex
);
1162 mma9551_set_device_state(data
->client
, false);
1163 mutex_unlock(&data
->mutex
);
1166 static int mma9553_runtime_suspend(struct device
*dev
)
1168 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1169 struct mma9553_data
*data
= iio_priv(indio_dev
);
1172 mutex_lock(&data
->mutex
);
1173 ret
= mma9551_set_device_state(data
->client
, false);
1174 mutex_unlock(&data
->mutex
);
1176 dev_err(&data
->client
->dev
, "powering off device failed\n");
1183 static int mma9553_runtime_resume(struct device
*dev
)
1185 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1186 struct mma9553_data
*data
= iio_priv(indio_dev
);
1189 ret
= mma9551_set_device_state(data
->client
, true);
1193 mma9551_sleep(MMA9553_DEFAULT_SAMPLE_RATE
);
1198 static int mma9553_suspend(struct device
*dev
)
1200 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1201 struct mma9553_data
*data
= iio_priv(indio_dev
);
1204 mutex_lock(&data
->mutex
);
1205 ret
= mma9551_set_device_state(data
->client
, false);
1206 mutex_unlock(&data
->mutex
);
1211 static int mma9553_resume(struct device
*dev
)
1213 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1214 struct mma9553_data
*data
= iio_priv(indio_dev
);
1217 mutex_lock(&data
->mutex
);
1218 ret
= mma9551_set_device_state(data
->client
, true);
1219 mutex_unlock(&data
->mutex
);
1224 static const struct dev_pm_ops mma9553_pm_ops
= {
1225 SYSTEM_SLEEP_PM_OPS(mma9553_suspend
, mma9553_resume
)
1226 RUNTIME_PM_OPS(mma9553_runtime_suspend
, mma9553_runtime_resume
, NULL
)
1229 static const struct acpi_device_id mma9553_acpi_match
[] = {
1234 MODULE_DEVICE_TABLE(acpi
, mma9553_acpi_match
);
1236 static const struct i2c_device_id mma9553_id
[] = {
1241 MODULE_DEVICE_TABLE(i2c
, mma9553_id
);
1243 static struct i2c_driver mma9553_driver
= {
1245 .name
= MMA9553_DRV_NAME
,
1246 .acpi_match_table
= mma9553_acpi_match
,
1247 .pm
= pm_ptr(&mma9553_pm_ops
),
1249 .probe
= mma9553_probe
,
1250 .remove
= mma9553_remove
,
1251 .id_table
= mma9553_id
,
1254 module_i2c_driver(mma9553_driver
);
1256 MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>");
1257 MODULE_LICENSE("GPL v2");
1258 MODULE_DESCRIPTION("MMA9553L pedometer platform driver");
1259 MODULE_IMPORT_NS(IIO_MMA9551
);