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