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
58 /* Pedometer status registers (R-only) */
59 #define MMA9553_REG_STATUS 0x00
60 #define MMA9553_MASK_STATUS_MRGFL BIT(15)
61 #define MMA9553_MASK_STATUS_SUSPCHG BIT(14)
62 #define MMA9553_MASK_STATUS_STEPCHG BIT(13)
63 #define MMA9553_MASK_STATUS_ACTCHG BIT(12)
64 #define MMA9553_MASK_STATUS_SUSP BIT(11)
65 #define MMA9553_MASK_STATUS_ACTIVITY (BIT(10) | BIT(9) | BIT(8))
66 #define MMA9553_MASK_STATUS_VERSION 0x00FF
68 #define MMA9553_REG_STEPCNT 0x02
69 #define MMA9553_REG_DISTANCE 0x04
70 #define MMA9553_REG_SPEED 0x06
71 #define MMA9553_REG_CALORIES 0x08
72 #define MMA9553_REG_SLEEPCNT 0x0A
74 /* Pedometer events are always mapped to this pin. */
75 #define MMA9553_DEFAULT_GPIO_PIN mma9551_gpio6
76 #define MMA9553_DEFAULT_GPIO_POLARITY 0
78 /* Bitnum used for gpio configuration = bit number in high status byte */
79 #define STATUS_TO_BITNUM(bit) (ffs(bit) - 9)
81 #define MMA9553_DEFAULT_SAMPLE_RATE 30 /* Hz */
84 * The internal activity level must be stable for ACTTHD samples before
85 * ACTIVITY is updated.The ACTIVITY variable contains the current activity
86 * level and is updated every time a step is detected or once a second
87 * if there are no steps.
89 #define MMA9553_ACTIVITY_THD_TO_SEC(thd) ((thd) / MMA9553_DEFAULT_SAMPLE_RATE)
90 #define MMA9553_ACTIVITY_SEC_TO_THD(sec) ((sec) * MMA9553_DEFAULT_SAMPLE_RATE)
93 * Autonomously suspend pedometer if acceleration vector magnitude
94 * is near 1g (4096 at 0.244 mg/LSB resolution) for 30 seconds.
96 #define MMA9553_DEFAULT_SLEEPMIN 3688 /* 0,9 g */
97 #define MMA9553_DEFAULT_SLEEPMAX 4508 /* 1,1 g */
98 #define MMA9553_DEFAULT_SLEEPTHD (MMA9553_DEFAULT_SAMPLE_RATE * 30)
100 #define MMA9553_CONFIG_RETRIES 2
102 /* Status register - activity field */
103 enum activity_level
{
111 static struct mma9553_event_info
{
112 enum iio_chan_type type
;
113 enum iio_modifier mod
;
114 enum iio_event_direction dir
;
115 } mma9553_events_info
[] = {
119 .dir
= IIO_EV_DIR_NONE
,
122 .type
= IIO_ACTIVITY
,
123 .mod
= IIO_MOD_STILL
,
124 .dir
= IIO_EV_DIR_RISING
,
127 .type
= IIO_ACTIVITY
,
128 .mod
= IIO_MOD_STILL
,
129 .dir
= IIO_EV_DIR_FALLING
,
132 .type
= IIO_ACTIVITY
,
133 .mod
= IIO_MOD_WALKING
,
134 .dir
= IIO_EV_DIR_RISING
,
137 .type
= IIO_ACTIVITY
,
138 .mod
= IIO_MOD_WALKING
,
139 .dir
= IIO_EV_DIR_FALLING
,
142 .type
= IIO_ACTIVITY
,
143 .mod
= IIO_MOD_JOGGING
,
144 .dir
= IIO_EV_DIR_RISING
,
147 .type
= IIO_ACTIVITY
,
148 .mod
= IIO_MOD_JOGGING
,
149 .dir
= IIO_EV_DIR_FALLING
,
152 .type
= IIO_ACTIVITY
,
153 .mod
= IIO_MOD_RUNNING
,
154 .dir
= IIO_EV_DIR_RISING
,
157 .type
= IIO_ACTIVITY
,
158 .mod
= IIO_MOD_RUNNING
,
159 .dir
= IIO_EV_DIR_FALLING
,
163 #define MMA9553_EVENTS_INFO_SIZE ARRAY_SIZE(mma9553_events_info)
165 struct mma9553_event
{
166 struct mma9553_event_info
*info
;
170 struct mma9553_conf_regs
{
181 struct mma9553_data
{
182 struct i2c_client
*client
;
184 struct mma9553_conf_regs conf
;
185 struct mma9553_event events
[MMA9553_EVENTS_INFO_SIZE
];
189 * This is used for all features that depend on step count:
190 * step count, distance, speed, calories.
192 bool stepcnt_enabled
;
198 static u8
mma9553_get_bits(u16 val
, u16 mask
)
200 return (val
& mask
) >> (ffs(mask
) - 1);
203 static u16
mma9553_set_bits(u16 current_val
, u16 val
, u16 mask
)
205 return (current_val
& ~mask
) | (val
<< (ffs(mask
) - 1));
208 static enum iio_modifier
mma9553_activity_to_mod(enum activity_level activity
)
211 case ACTIVITY_RUNNING
:
212 return IIO_MOD_RUNNING
;
213 case ACTIVITY_JOGGING
:
214 return IIO_MOD_JOGGING
;
215 case ACTIVITY_WALKING
:
216 return IIO_MOD_WALKING
;
218 return IIO_MOD_STILL
;
219 case ACTIVITY_UNKNOWN
:
225 static void mma9553_init_events(struct mma9553_data
*data
)
229 data
->num_events
= MMA9553_EVENTS_INFO_SIZE
;
230 for (i
= 0; i
< data
->num_events
; i
++) {
231 data
->events
[i
].info
= &mma9553_events_info
[i
];
232 data
->events
[i
].enabled
= false;
236 static struct mma9553_event
*mma9553_get_event(struct mma9553_data
*data
,
237 enum iio_chan_type type
,
238 enum iio_modifier mod
,
239 enum iio_event_direction dir
)
243 for (i
= 0; i
< data
->num_events
; i
++)
244 if (data
->events
[i
].info
->type
== type
&&
245 data
->events
[i
].info
->mod
== mod
&&
246 data
->events
[i
].info
->dir
== dir
)
247 return &data
->events
[i
];
252 static bool mma9553_is_any_event_enabled(struct mma9553_data
*data
,
254 enum iio_chan_type type
)
258 for (i
= 0; i
< data
->num_events
; i
++)
259 if ((check_type
&& data
->events
[i
].info
->type
== type
&&
260 data
->events
[i
].enabled
) ||
261 (!check_type
&& data
->events
[i
].enabled
))
267 static int mma9553_set_config(struct mma9553_data
*data
, u16 reg
,
268 u16
*p_reg_val
, u16 val
, u16 mask
)
273 reg_val
= *p_reg_val
;
274 if (val
== mma9553_get_bits(reg_val
, mask
))
277 reg_val
= mma9553_set_bits(reg_val
, val
, mask
);
278 ret
= mma9551_write_config_word(data
->client
, MMA9551_APPID_PEDOMETER
,
281 dev_err(&data
->client
->dev
,
282 "error writing config register 0x%x\n", reg
);
286 *p_reg_val
= reg_val
;
288 /* Reinitializes the pedometer with current configuration values */
289 config
= mma9553_set_bits(data
->conf
.config
, 1,
290 MMA9553_MASK_CONF_CONFIG
);
292 ret
= mma9551_write_config_word(data
->client
, MMA9551_APPID_PEDOMETER
,
293 MMA9553_REG_CONF_CONF_STEPLEN
, config
);
295 dev_err(&data
->client
->dev
,
296 "error writing config register 0x%x\n",
297 MMA9553_REG_CONF_CONF_STEPLEN
);
301 retries
= MMA9553_CONFIG_RETRIES
;
303 mma9551_sleep(MMA9553_DEFAULT_SAMPLE_RATE
);
304 ret
= mma9551_read_config_word(data
->client
,
305 MMA9551_APPID_PEDOMETER
,
306 MMA9553_REG_CONF_CONF_STEPLEN
,
310 } while (mma9553_get_bits(config
, MMA9553_MASK_CONF_CONFIG
) &&
316 static int mma9553_read_activity_stepcnt(struct mma9553_data
*data
,
317 u8
*activity
, u16
*stepcnt
)
323 ret
= mma9551_read_status_words(data
->client
, MMA9551_APPID_PEDOMETER
,
324 MMA9553_REG_STATUS
, sizeof(u32
),
325 (u16
*) &status_stepcnt
);
327 dev_err(&data
->client
->dev
,
328 "error reading status and stepcnt\n");
332 status
= status_stepcnt
& MMA9553_MASK_CONF_WORD
;
333 *activity
= mma9553_get_bits(status
, MMA9553_MASK_STATUS_ACTIVITY
);
334 *stepcnt
= status_stepcnt
>> 16;
339 static int mma9553_conf_gpio(struct mma9553_data
*data
)
341 u8 bitnum
= 0, appid
= MMA9551_APPID_PEDOMETER
;
343 struct mma9553_event
*ev_step_detect
;
344 bool activity_enabled
;
347 mma9553_is_any_event_enabled(data
, true, IIO_ACTIVITY
);
349 mma9553_get_event(data
, IIO_STEPS
, IIO_NO_MOD
, IIO_EV_DIR_NONE
);
352 * If both step detector and activity are enabled, use the MRGFL bit.
353 * This bit is the logical OR of the SUSPCHG, STEPCHG, and ACTCHG flags.
355 if (activity_enabled
&& ev_step_detect
->enabled
)
356 bitnum
= STATUS_TO_BITNUM(MMA9553_MASK_STATUS_MRGFL
);
357 else if (ev_step_detect
->enabled
)
358 bitnum
= STATUS_TO_BITNUM(MMA9553_MASK_STATUS_STEPCHG
);
359 else if (activity_enabled
)
360 bitnum
= STATUS_TO_BITNUM(MMA9553_MASK_STATUS_ACTCHG
);
362 appid
= MMA9551_APPID_NONE
;
364 if (data
->gpio_bitnum
== bitnum
)
367 /* Save initial values for activity and stepcnt */
368 if (activity_enabled
|| ev_step_detect
->enabled
)
369 mma9553_read_activity_stepcnt(data
, &data
->activity
,
372 ret
= mma9551_gpio_config(data
->client
,
373 MMA9553_DEFAULT_GPIO_PIN
,
374 appid
, bitnum
, MMA9553_DEFAULT_GPIO_POLARITY
);
377 data
->gpio_bitnum
= bitnum
;
382 static int mma9553_init(struct mma9553_data
*data
)
386 ret
= mma9551_read_version(data
->client
);
391 * Read all the pedometer configuration registers. This is used as
392 * a device identification command to differentiate the MMA9553L
396 mma9551_read_config_words(data
->client
, MMA9551_APPID_PEDOMETER
,
397 MMA9553_REG_CONF_SLEEPMIN
,
398 sizeof(data
->conf
), (u16
*) &data
->conf
);
400 dev_err(&data
->client
->dev
,
401 "device is not MMA9553L: failed to read cfg regs\n");
407 data
->gpio_bitnum
= -1;
408 ret
= mma9553_conf_gpio(data
);
412 ret
= mma9551_app_reset(data
->client
, MMA9551_RSC_PED
);
416 /* Init config registers */
417 data
->conf
.sleepmin
= MMA9553_DEFAULT_SLEEPMIN
;
418 data
->conf
.sleepmax
= MMA9553_DEFAULT_SLEEPMAX
;
419 data
->conf
.sleepthd
= MMA9553_DEFAULT_SLEEPTHD
;
421 mma9553_set_bits(data
->conf
.config
, 1, MMA9553_MASK_CONF_CONFIG
);
423 * Clear the activity debounce counter when the activity level changes,
424 * so that the confidence level applies for any activity level.
426 data
->conf
.config
= mma9553_set_bits(data
->conf
.config
, 1,
427 MMA9553_MASK_CONF_ACT_DBCNTM
);
429 mma9551_write_config_words(data
->client
, MMA9551_APPID_PEDOMETER
,
430 MMA9553_REG_CONF_SLEEPMIN
,
431 sizeof(data
->conf
), (u16
*) &data
->conf
);
433 dev_err(&data
->client
->dev
,
434 "failed to write configuration registers\n");
438 return mma9551_set_device_state(data
->client
, true);
441 static int mma9553_read_raw(struct iio_dev
*indio_dev
,
442 struct iio_chan_spec
const *chan
,
443 int *val
, int *val2
, long mask
)
445 struct mma9553_data
*data
= iio_priv(indio_dev
);
452 case IIO_CHAN_INFO_PROCESSED
:
453 switch (chan
->type
) {
456 * The HW only counts steps and other dependent
457 * parameters (speed, distance, calories, activity)
458 * if power is on (from enabling an event or the
461 mma9553_is_any_event_enabled(data
, false, 0) ||
462 data
->stepcnt_enabled
;
464 dev_err(&data
->client
->dev
,
465 "No channels enabled\n");
468 mutex_lock(&data
->mutex
);
469 ret
= mma9551_read_status_word(data
->client
,
470 MMA9551_APPID_PEDOMETER
,
473 mutex_unlock(&data
->mutex
);
480 mma9553_is_any_event_enabled(data
, false, 0) ||
481 data
->stepcnt_enabled
;
483 dev_err(&data
->client
->dev
,
484 "No channels enabled\n");
487 mutex_lock(&data
->mutex
);
488 ret
= mma9551_read_status_word(data
->client
,
489 MMA9551_APPID_PEDOMETER
,
490 MMA9553_REG_DISTANCE
,
492 mutex_unlock(&data
->mutex
);
499 mma9553_is_any_event_enabled(data
, false, 0) ||
500 data
->stepcnt_enabled
;
502 dev_err(&data
->client
->dev
,
503 "No channels enabled\n");
506 mutex_lock(&data
->mutex
);
507 ret
= mma9551_read_status_word(data
->client
,
508 MMA9551_APPID_PEDOMETER
,
511 mutex_unlock(&data
->mutex
);
516 mma9553_get_bits(tmp
, MMA9553_MASK_STATUS_ACTIVITY
);
519 * The device does not support confidence value levels,
520 * so we will always have 100% for current activity and
523 if (chan
->channel2
== mma9553_activity_to_mod(activity
))
531 case IIO_CHAN_INFO_RAW
:
532 switch (chan
->type
) {
533 case IIO_VELOCITY
: /* m/h */
534 if (chan
->channel2
!= IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z
)
537 mma9553_is_any_event_enabled(data
, false, 0) ||
538 data
->stepcnt_enabled
;
540 dev_err(&data
->client
->dev
,
541 "No channels enabled\n");
544 mutex_lock(&data
->mutex
);
545 ret
= mma9551_read_status_word(data
->client
,
546 MMA9551_APPID_PEDOMETER
,
547 MMA9553_REG_SPEED
, &tmp
);
548 mutex_unlock(&data
->mutex
);
553 case IIO_ENERGY
: /* Cal or kcal */
555 mma9553_is_any_event_enabled(data
, false, 0) ||
556 data
->stepcnt_enabled
;
558 dev_err(&data
->client
->dev
,
559 "No channels enabled\n");
562 mutex_lock(&data
->mutex
);
563 ret
= mma9551_read_status_word(data
->client
,
564 MMA9551_APPID_PEDOMETER
,
565 MMA9553_REG_CALORIES
,
567 mutex_unlock(&data
->mutex
);
573 mutex_lock(&data
->mutex
);
574 ret
= mma9551_read_accel_chan(data
->client
,
576 mutex_unlock(&data
->mutex
);
581 case IIO_CHAN_INFO_SCALE
:
582 switch (chan
->type
) {
583 case IIO_VELOCITY
: /* m/h to m/s */
584 if (chan
->channel2
!= IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z
)
587 *val2
= 277; /* 0.000277 */
588 return IIO_VAL_INT_PLUS_MICRO
;
589 case IIO_ENERGY
: /* Cal or kcal to J */
593 return mma9551_read_accel_scale(val
, val2
);
597 case IIO_CHAN_INFO_ENABLE
:
598 *val
= data
->stepcnt_enabled
;
600 case IIO_CHAN_INFO_CALIBHEIGHT
:
601 tmp
= mma9553_get_bits(data
->conf
.height_weight
,
602 MMA9553_MASK_CONF_HEIGHT
);
603 *val
= tmp
/ 100; /* cm to m */
604 *val2
= (tmp
% 100) * 10000;
605 return IIO_VAL_INT_PLUS_MICRO
;
606 case IIO_CHAN_INFO_CALIBWEIGHT
:
607 *val
= mma9553_get_bits(data
->conf
.height_weight
,
608 MMA9553_MASK_CONF_WEIGHT
);
610 case IIO_CHAN_INFO_DEBOUNCE_COUNT
:
611 switch (chan
->type
) {
613 *val
= mma9553_get_bits(data
->conf
.filter
,
614 MMA9553_MASK_CONF_FILTSTEP
);
619 case IIO_CHAN_INFO_DEBOUNCE_TIME
:
620 switch (chan
->type
) {
622 *val
= mma9553_get_bits(data
->conf
.filter
,
623 MMA9553_MASK_CONF_FILTTIME
);
628 case IIO_CHAN_INFO_INT_TIME
:
629 switch (chan
->type
) {
631 if (chan
->channel2
!= IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z
)
633 *val
= mma9553_get_bits(data
->conf
.speed_step
,
634 MMA9553_MASK_CONF_SPDPRD
);
644 static int mma9553_write_raw(struct iio_dev
*indio_dev
,
645 struct iio_chan_spec
const *chan
,
646 int val
, int val2
, long mask
)
648 struct mma9553_data
*data
= iio_priv(indio_dev
);
652 case IIO_CHAN_INFO_ENABLE
:
653 if (data
->stepcnt_enabled
== !!val
)
655 mutex_lock(&data
->mutex
);
656 ret
= mma9551_set_power_state(data
->client
, val
);
658 mutex_unlock(&data
->mutex
);
661 data
->stepcnt_enabled
= val
;
662 mutex_unlock(&data
->mutex
);
664 case IIO_CHAN_INFO_CALIBHEIGHT
:
666 tmp
= val
* 100 + val2
/ 10000;
667 if (tmp
< 0 || tmp
> 255)
669 mutex_lock(&data
->mutex
);
670 ret
= mma9553_set_config(data
,
671 MMA9553_REG_CONF_HEIGHT_WEIGHT
,
672 &data
->conf
.height_weight
,
673 tmp
, MMA9553_MASK_CONF_HEIGHT
);
674 mutex_unlock(&data
->mutex
);
676 case IIO_CHAN_INFO_CALIBWEIGHT
:
677 if (val
< 0 || val
> 255)
679 mutex_lock(&data
->mutex
);
680 ret
= mma9553_set_config(data
,
681 MMA9553_REG_CONF_HEIGHT_WEIGHT
,
682 &data
->conf
.height_weight
,
683 val
, MMA9553_MASK_CONF_WEIGHT
);
684 mutex_unlock(&data
->mutex
);
686 case IIO_CHAN_INFO_DEBOUNCE_COUNT
:
687 switch (chan
->type
) {
690 * Set to 0 to disable step filtering. If the value
691 * specified is greater than 6, then 6 will be used.
697 mutex_lock(&data
->mutex
);
698 ret
= mma9553_set_config(data
, MMA9553_REG_CONF_FILTER
,
699 &data
->conf
.filter
, val
,
700 MMA9553_MASK_CONF_FILTSTEP
);
701 mutex_unlock(&data
->mutex
);
706 case IIO_CHAN_INFO_DEBOUNCE_TIME
:
707 switch (chan
->type
) {
709 if (val
< 0 || val
> 127)
711 mutex_lock(&data
->mutex
);
712 ret
= mma9553_set_config(data
, MMA9553_REG_CONF_FILTER
,
713 &data
->conf
.filter
, val
,
714 MMA9553_MASK_CONF_FILTTIME
);
715 mutex_unlock(&data
->mutex
);
720 case IIO_CHAN_INFO_INT_TIME
:
721 switch (chan
->type
) {
723 if (chan
->channel2
!= IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z
)
726 * If set to a value greater than 5, then 5 will be
727 * used. Warning: Do not set SPDPRD to 0 or 1 as
728 * this may cause undesirable behavior.
734 mutex_lock(&data
->mutex
);
735 ret
= mma9553_set_config(data
,
736 MMA9553_REG_CONF_SPEED_STEP
,
737 &data
->conf
.speed_step
, val
,
738 MMA9553_MASK_CONF_SPDPRD
);
739 mutex_unlock(&data
->mutex
);
749 static int mma9553_read_event_config(struct iio_dev
*indio_dev
,
750 const struct iio_chan_spec
*chan
,
751 enum iio_event_type type
,
752 enum iio_event_direction dir
)
755 struct mma9553_data
*data
= iio_priv(indio_dev
);
756 struct mma9553_event
*event
;
758 event
= mma9553_get_event(data
, chan
->type
, chan
->channel2
, dir
);
762 return event
->enabled
;
765 static int mma9553_write_event_config(struct iio_dev
*indio_dev
,
766 const struct iio_chan_spec
*chan
,
767 enum iio_event_type type
,
768 enum iio_event_direction dir
, int state
)
770 struct mma9553_data
*data
= iio_priv(indio_dev
);
771 struct mma9553_event
*event
;
774 event
= mma9553_get_event(data
, chan
->type
, chan
->channel2
, dir
);
778 if (event
->enabled
== state
)
781 mutex_lock(&data
->mutex
);
783 ret
= mma9551_set_power_state(data
->client
, state
);
786 event
->enabled
= state
;
788 ret
= mma9553_conf_gpio(data
);
792 mutex_unlock(&data
->mutex
);
798 event
->enabled
= false;
799 mma9551_set_power_state(data
->client
, false);
802 mutex_unlock(&data
->mutex
);
806 static int mma9553_read_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 *val
= mma9553_get_bits(data
->conf
.speed_step
,
821 MMA9553_MASK_CONF_STEPCOALESCE
);
825 * The device does not support confidence value levels.
826 * We set an average of 50%.
833 case IIO_EV_INFO_PERIOD
:
834 switch (chan
->type
) {
836 *val
= MMA9553_ACTIVITY_THD_TO_SEC(data
->conf
.actthd
);
846 static int mma9553_write_event_value(struct iio_dev
*indio_dev
,
847 const struct iio_chan_spec
*chan
,
848 enum iio_event_type type
,
849 enum iio_event_direction dir
,
850 enum iio_event_info info
,
853 struct mma9553_data
*data
= iio_priv(indio_dev
);
857 case IIO_EV_INFO_VALUE
:
858 switch (chan
->type
) {
860 if (val
< 0 || val
> 255)
862 mutex_lock(&data
->mutex
);
863 ret
= mma9553_set_config(data
,
864 MMA9553_REG_CONF_SPEED_STEP
,
865 &data
->conf
.speed_step
, val
,
866 MMA9553_MASK_CONF_STEPCOALESCE
);
867 mutex_unlock(&data
->mutex
);
872 case IIO_EV_INFO_PERIOD
:
873 switch (chan
->type
) {
875 mutex_lock(&data
->mutex
);
876 ret
= mma9553_set_config(data
, MMA9553_REG_CONF_ACTTHD
,
878 MMA9553_ACTIVITY_SEC_TO_THD
879 (val
), MMA9553_MASK_CONF_WORD
);
880 mutex_unlock(&data
->mutex
);
890 static int mma9553_get_calibgender_mode(struct iio_dev
*indio_dev
,
891 const struct iio_chan_spec
*chan
)
893 struct mma9553_data
*data
= iio_priv(indio_dev
);
896 gender
= mma9553_get_bits(data
->conf
.filter
, MMA9553_MASK_CONF_MALE
);
898 * HW expects 0 for female and 1 for male,
899 * while iio index is 0 for male and 1 for female
904 static int mma9553_set_calibgender_mode(struct iio_dev
*indio_dev
,
905 const struct iio_chan_spec
*chan
,
908 struct mma9553_data
*data
= iio_priv(indio_dev
);
912 if ((mode
!= 0) && (mode
!= 1))
914 mutex_lock(&data
->mutex
);
915 ret
= mma9553_set_config(data
, MMA9553_REG_CONF_FILTER
,
916 &data
->conf
.filter
, gender
,
917 MMA9553_MASK_CONF_MALE
);
918 mutex_unlock(&data
->mutex
);
923 static const struct iio_event_spec mma9553_step_event
= {
924 .type
= IIO_EV_TYPE_CHANGE
,
925 .dir
= IIO_EV_DIR_NONE
,
926 .mask_separate
= BIT(IIO_EV_INFO_ENABLE
) | BIT(IIO_EV_INFO_VALUE
),
929 static const struct iio_event_spec mma9553_activity_events
[] = {
931 .type
= IIO_EV_TYPE_THRESH
,
932 .dir
= IIO_EV_DIR_RISING
,
933 .mask_separate
= BIT(IIO_EV_INFO_ENABLE
) |
934 BIT(IIO_EV_INFO_VALUE
) |
935 BIT(IIO_EV_INFO_PERIOD
),
938 .type
= IIO_EV_TYPE_THRESH
,
939 .dir
= IIO_EV_DIR_FALLING
,
940 .mask_separate
= BIT(IIO_EV_INFO_ENABLE
) |
941 BIT(IIO_EV_INFO_VALUE
) |
942 BIT(IIO_EV_INFO_PERIOD
),
946 static const char * const calibgender_modes
[] = { "male", "female" };
948 static const struct iio_enum mma9553_calibgender_enum
= {
949 .items
= calibgender_modes
,
950 .num_items
= ARRAY_SIZE(calibgender_modes
),
951 .get
= mma9553_get_calibgender_mode
,
952 .set
= mma9553_set_calibgender_mode
,
955 static const struct iio_chan_spec_ext_info mma9553_ext_info
[] = {
956 IIO_ENUM("calibgender", IIO_SHARED_BY_TYPE
, &mma9553_calibgender_enum
),
957 IIO_ENUM_AVAILABLE("calibgender", &mma9553_calibgender_enum
),
961 #define MMA9553_PEDOMETER_CHANNEL(_type, _mask) { \
963 .info_mask_separate = BIT(IIO_CHAN_INFO_ENABLE) | \
964 BIT(IIO_CHAN_INFO_CALIBHEIGHT) | \
966 .ext_info = mma9553_ext_info, \
969 #define MMA9553_ACTIVITY_CHANNEL(_chan2) { \
970 .type = IIO_ACTIVITY, \
972 .channel2 = _chan2, \
973 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
974 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_CALIBHEIGHT), \
975 .event_spec = mma9553_activity_events, \
976 .num_event_specs = ARRAY_SIZE(mma9553_activity_events), \
977 .ext_info = mma9553_ext_info, \
980 static const struct iio_chan_spec mma9553_channels
[] = {
981 MMA9551_ACCEL_CHANNEL(IIO_MOD_X
),
982 MMA9551_ACCEL_CHANNEL(IIO_MOD_Y
),
983 MMA9551_ACCEL_CHANNEL(IIO_MOD_Z
),
987 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
) |
988 BIT(IIO_CHAN_INFO_ENABLE
) |
989 BIT(IIO_CHAN_INFO_DEBOUNCE_COUNT
) |
990 BIT(IIO_CHAN_INFO_DEBOUNCE_TIME
),
991 .event_spec
= &mma9553_step_event
,
992 .num_event_specs
= 1,
995 MMA9553_PEDOMETER_CHANNEL(IIO_DISTANCE
, BIT(IIO_CHAN_INFO_PROCESSED
)),
997 .type
= IIO_VELOCITY
,
999 .channel2
= IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z
,
1000 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1001 BIT(IIO_CHAN_INFO_SCALE
) |
1002 BIT(IIO_CHAN_INFO_INT_TIME
) |
1003 BIT(IIO_CHAN_INFO_ENABLE
),
1004 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_CALIBHEIGHT
),
1005 .ext_info
= mma9553_ext_info
,
1007 MMA9553_PEDOMETER_CHANNEL(IIO_ENERGY
, BIT(IIO_CHAN_INFO_RAW
) |
1008 BIT(IIO_CHAN_INFO_SCALE
) |
1009 BIT(IIO_CHAN_INFO_CALIBWEIGHT
)),
1011 MMA9553_ACTIVITY_CHANNEL(IIO_MOD_RUNNING
),
1012 MMA9553_ACTIVITY_CHANNEL(IIO_MOD_JOGGING
),
1013 MMA9553_ACTIVITY_CHANNEL(IIO_MOD_WALKING
),
1014 MMA9553_ACTIVITY_CHANNEL(IIO_MOD_STILL
),
1017 static const struct iio_info mma9553_info
= {
1018 .driver_module
= THIS_MODULE
,
1019 .read_raw
= mma9553_read_raw
,
1020 .write_raw
= mma9553_write_raw
,
1021 .read_event_config
= mma9553_read_event_config
,
1022 .write_event_config
= mma9553_write_event_config
,
1023 .read_event_value
= mma9553_read_event_value
,
1024 .write_event_value
= mma9553_write_event_value
,
1027 static irqreturn_t
mma9553_irq_handler(int irq
, void *private)
1029 struct iio_dev
*indio_dev
= private;
1030 struct mma9553_data
*data
= iio_priv(indio_dev
);
1032 data
->timestamp
= iio_get_time_ns();
1034 * Since we only configure the interrupt pin when an
1035 * event is enabled, we are sure we have at least
1036 * one event enabled at this point.
1038 return IRQ_WAKE_THREAD
;
1041 static irqreturn_t
mma9553_event_handler(int irq
, void *private)
1043 struct iio_dev
*indio_dev
= private;
1044 struct mma9553_data
*data
= iio_priv(indio_dev
);
1047 struct mma9553_event
*ev_activity
, *ev_prev_activity
, *ev_step_detect
;
1050 mutex_lock(&data
->mutex
);
1051 ret
= mma9553_read_activity_stepcnt(data
, &activity
, &stepcnt
);
1053 mutex_unlock(&data
->mutex
);
1058 mma9553_get_event(data
, IIO_ACTIVITY
,
1059 mma9553_activity_to_mod(data
->activity
),
1060 IIO_EV_DIR_FALLING
);
1062 mma9553_get_event(data
, IIO_ACTIVITY
,
1063 mma9553_activity_to_mod(activity
),
1066 mma9553_get_event(data
, IIO_STEPS
, IIO_NO_MOD
, IIO_EV_DIR_NONE
);
1068 if (ev_step_detect
->enabled
&& (stepcnt
!= data
->stepcnt
)) {
1069 data
->stepcnt
= stepcnt
;
1070 iio_push_event(indio_dev
,
1071 IIO_EVENT_CODE(IIO_STEPS
, 0, IIO_NO_MOD
,
1072 IIO_EV_DIR_NONE
, IIO_EV_TYPE_CHANGE
, 0, 0, 0),
1076 if (activity
!= data
->activity
) {
1077 data
->activity
= activity
;
1078 /* ev_activity can be NULL if activity == ACTIVITY_UNKNOWN */
1079 if (ev_prev_activity
&& ev_prev_activity
->enabled
)
1080 iio_push_event(indio_dev
,
1081 IIO_EVENT_CODE(IIO_ACTIVITY
, 0,
1082 ev_prev_activity
->info
->mod
,
1084 IIO_EV_TYPE_THRESH
, 0, 0, 0),
1087 if (ev_activity
&& ev_activity
->enabled
)
1088 iio_push_event(indio_dev
,
1089 IIO_EVENT_CODE(IIO_ACTIVITY
, 0,
1090 ev_activity
->info
->mod
,
1092 IIO_EV_TYPE_THRESH
, 0, 0, 0),
1095 mutex_unlock(&data
->mutex
);
1100 static int mma9553_gpio_probe(struct i2c_client
*client
)
1103 struct gpio_desc
*gpio
;
1111 /* data ready gpio interrupt pin */
1112 gpio
= devm_gpiod_get_index(dev
, MMA9553_GPIO_NAME
, 0);
1114 dev_err(dev
, "acpi gpio get index failed\n");
1115 return PTR_ERR(gpio
);
1118 ret
= gpiod_direction_input(gpio
);
1122 ret
= gpiod_to_irq(gpio
);
1124 dev_dbg(dev
, "gpio resource, no:%d irq:%d\n", desc_to_gpio(gpio
), ret
);
1129 static const char *mma9553_match_acpi_device(struct device
*dev
)
1131 const struct acpi_device_id
*id
;
1133 id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
1137 return dev_name(dev
);
1140 static int mma9553_probe(struct i2c_client
*client
,
1141 const struct i2c_device_id
*id
)
1143 struct mma9553_data
*data
;
1144 struct iio_dev
*indio_dev
;
1145 const char *name
= NULL
;
1148 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
1152 data
= iio_priv(indio_dev
);
1153 i2c_set_clientdata(client
, indio_dev
);
1154 data
->client
= client
;
1158 else if (ACPI_HANDLE(&client
->dev
))
1159 name
= mma9553_match_acpi_device(&client
->dev
);
1163 mutex_init(&data
->mutex
);
1164 mma9553_init_events(data
);
1166 ret
= mma9553_init(data
);
1170 indio_dev
->dev
.parent
= &client
->dev
;
1171 indio_dev
->channels
= mma9553_channels
;
1172 indio_dev
->num_channels
= ARRAY_SIZE(mma9553_channels
);
1173 indio_dev
->name
= name
;
1174 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1175 indio_dev
->info
= &mma9553_info
;
1177 if (client
->irq
< 0)
1178 client
->irq
= mma9553_gpio_probe(client
);
1180 if (client
->irq
>= 0) {
1181 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1182 mma9553_irq_handler
,
1183 mma9553_event_handler
,
1184 IRQF_TRIGGER_RISING
,
1185 MMA9553_IRQ_NAME
, indio_dev
);
1187 dev_err(&client
->dev
, "request irq %d failed\n",
1194 ret
= iio_device_register(indio_dev
);
1196 dev_err(&client
->dev
, "unable to register iio device\n");
1200 ret
= pm_runtime_set_active(&client
->dev
);
1202 goto out_iio_unregister
;
1204 pm_runtime_enable(&client
->dev
);
1205 pm_runtime_set_autosuspend_delay(&client
->dev
,
1206 MMA9551_AUTO_SUSPEND_DELAY_MS
);
1207 pm_runtime_use_autosuspend(&client
->dev
);
1209 dev_dbg(&indio_dev
->dev
, "Registered device %s\n", name
);
1214 iio_device_unregister(indio_dev
);
1216 mma9551_set_device_state(client
, false);
1220 static int mma9553_remove(struct i2c_client
*client
)
1222 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
1223 struct mma9553_data
*data
= iio_priv(indio_dev
);
1225 pm_runtime_disable(&client
->dev
);
1226 pm_runtime_set_suspended(&client
->dev
);
1227 pm_runtime_put_noidle(&client
->dev
);
1229 iio_device_unregister(indio_dev
);
1230 mutex_lock(&data
->mutex
);
1231 mma9551_set_device_state(data
->client
, false);
1232 mutex_unlock(&data
->mutex
);
1238 static int mma9553_runtime_suspend(struct device
*dev
)
1240 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1241 struct mma9553_data
*data
= iio_priv(indio_dev
);
1244 mutex_lock(&data
->mutex
);
1245 ret
= mma9551_set_device_state(data
->client
, false);
1246 mutex_unlock(&data
->mutex
);
1248 dev_err(&data
->client
->dev
, "powering off device failed\n");
1255 static int mma9553_runtime_resume(struct device
*dev
)
1257 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1258 struct mma9553_data
*data
= iio_priv(indio_dev
);
1261 ret
= mma9551_set_device_state(data
->client
, true);
1265 mma9551_sleep(MMA9553_DEFAULT_SAMPLE_RATE
);
1271 #ifdef CONFIG_PM_SLEEP
1272 static int mma9553_suspend(struct device
*dev
)
1274 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1275 struct mma9553_data
*data
= iio_priv(indio_dev
);
1278 mutex_lock(&data
->mutex
);
1279 ret
= mma9551_set_device_state(data
->client
, false);
1280 mutex_unlock(&data
->mutex
);
1285 static int mma9553_resume(struct device
*dev
)
1287 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1288 struct mma9553_data
*data
= iio_priv(indio_dev
);
1291 mutex_lock(&data
->mutex
);
1292 ret
= mma9551_set_device_state(data
->client
, true);
1293 mutex_unlock(&data
->mutex
);
1299 static const struct dev_pm_ops mma9553_pm_ops
= {
1300 SET_SYSTEM_SLEEP_PM_OPS(mma9553_suspend
, mma9553_resume
)
1301 SET_RUNTIME_PM_OPS(mma9553_runtime_suspend
,
1302 mma9553_runtime_resume
, NULL
)
1305 static const struct acpi_device_id mma9553_acpi_match
[] = {
1310 MODULE_DEVICE_TABLE(acpi
, mma9553_acpi_match
);
1312 static const struct i2c_device_id mma9553_id
[] = {
1317 MODULE_DEVICE_TABLE(i2c
, mma9553_id
);
1319 static struct i2c_driver mma9553_driver
= {
1321 .name
= MMA9553_DRV_NAME
,
1322 .acpi_match_table
= ACPI_PTR(mma9553_acpi_match
),
1323 .pm
= &mma9553_pm_ops
,
1325 .probe
= mma9553_probe
,
1326 .remove
= mma9553_remove
,
1327 .id_table
= mma9553_id
,
1330 module_i2c_driver(mma9553_driver
);
1332 MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>");
1333 MODULE_LICENSE("GPL v2");
1334 MODULE_DESCRIPTION("MMA9553L pedometer platform driver");