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