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