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