xref: /openbmc/linux/drivers/iio/accel/mma9551.c (revision 82e6fdd6)
1 /*
2  * Freescale MMA9551L Intelligent Motion-Sensing Platform 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/delay.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
24 #include <linux/iio/events.h>
25 #include <linux/pm_runtime.h>
26 #include "mma9551_core.h"
27 
28 #define MMA9551_DRV_NAME		"mma9551"
29 #define MMA9551_IRQ_NAME		"mma9551_event"
30 #define MMA9551_GPIO_COUNT		4
31 
32 /* Tilt application (inclination in IIO terms). */
33 #define MMA9551_TILT_XZ_ANG_REG		0x00
34 #define MMA9551_TILT_YZ_ANG_REG		0x01
35 #define MMA9551_TILT_XY_ANG_REG		0x02
36 #define MMA9551_TILT_ANGFLG		BIT(7)
37 #define MMA9551_TILT_QUAD_REG		0x03
38 #define MMA9551_TILT_XY_QUAD_SHIFT	0
39 #define MMA9551_TILT_YZ_QUAD_SHIFT	2
40 #define MMA9551_TILT_XZ_QUAD_SHIFT	4
41 #define MMA9551_TILT_CFG_REG		0x01
42 #define MMA9551_TILT_ANG_THRESH_MASK	GENMASK(3, 0)
43 
44 #define MMA9551_DEFAULT_SAMPLE_RATE	122	/* Hz */
45 
46 /* Tilt events are mapped to the first three GPIO pins. */
47 enum mma9551_tilt_axis {
48 	mma9551_x = 0,
49 	mma9551_y,
50 	mma9551_z,
51 };
52 
53 struct mma9551_data {
54 	struct i2c_client *client;
55 	struct mutex mutex;
56 	int event_enabled[3];
57 	int irqs[MMA9551_GPIO_COUNT];
58 };
59 
60 static int mma9551_read_incli_chan(struct i2c_client *client,
61 				   const struct iio_chan_spec *chan,
62 				   int *val)
63 {
64 	u8 quad_shift, angle, quadrant;
65 	u16 reg_addr;
66 	int ret;
67 
68 	switch (chan->channel2) {
69 	case IIO_MOD_X:
70 		reg_addr = MMA9551_TILT_YZ_ANG_REG;
71 		quad_shift = MMA9551_TILT_YZ_QUAD_SHIFT;
72 		break;
73 	case IIO_MOD_Y:
74 		reg_addr = MMA9551_TILT_XZ_ANG_REG;
75 		quad_shift = MMA9551_TILT_XZ_QUAD_SHIFT;
76 		break;
77 	case IIO_MOD_Z:
78 		reg_addr = MMA9551_TILT_XY_ANG_REG;
79 		quad_shift = MMA9551_TILT_XY_QUAD_SHIFT;
80 		break;
81 	default:
82 		return -EINVAL;
83 	}
84 
85 	ret = mma9551_set_power_state(client, true);
86 	if (ret < 0)
87 		return ret;
88 
89 	ret = mma9551_read_status_byte(client, MMA9551_APPID_TILT,
90 				       reg_addr, &angle);
91 	if (ret < 0)
92 		goto out_poweroff;
93 
94 	ret = mma9551_read_status_byte(client, MMA9551_APPID_TILT,
95 				       MMA9551_TILT_QUAD_REG, &quadrant);
96 	if (ret < 0)
97 		goto out_poweroff;
98 
99 	angle &= ~MMA9551_TILT_ANGFLG;
100 	quadrant = (quadrant >> quad_shift) & 0x03;
101 
102 	if (quadrant == 1 || quadrant == 3)
103 		*val = 90 * (quadrant + 1) - angle;
104 	else
105 		*val = angle + 90 * quadrant;
106 
107 	ret = IIO_VAL_INT;
108 
109 out_poweroff:
110 	mma9551_set_power_state(client, false);
111 	return ret;
112 }
113 
114 static int mma9551_read_raw(struct iio_dev *indio_dev,
115 			    struct iio_chan_spec const *chan,
116 			    int *val, int *val2, long mask)
117 {
118 	struct mma9551_data *data = iio_priv(indio_dev);
119 	int ret;
120 
121 	switch (mask) {
122 	case IIO_CHAN_INFO_PROCESSED:
123 		switch (chan->type) {
124 		case IIO_INCLI:
125 			mutex_lock(&data->mutex);
126 			ret = mma9551_read_incli_chan(data->client, chan, val);
127 			mutex_unlock(&data->mutex);
128 			return ret;
129 		default:
130 			return -EINVAL;
131 		}
132 	case IIO_CHAN_INFO_RAW:
133 		switch (chan->type) {
134 		case IIO_ACCEL:
135 			mutex_lock(&data->mutex);
136 			ret = mma9551_read_accel_chan(data->client,
137 						      chan, val, val2);
138 			mutex_unlock(&data->mutex);
139 			return ret;
140 		default:
141 			return -EINVAL;
142 		}
143 	case IIO_CHAN_INFO_SCALE:
144 		switch (chan->type) {
145 		case IIO_ACCEL:
146 			return mma9551_read_accel_scale(val, val2);
147 		default:
148 			return -EINVAL;
149 		}
150 	default:
151 		return -EINVAL;
152 	}
153 }
154 
155 static int mma9551_read_event_config(struct iio_dev *indio_dev,
156 				     const struct iio_chan_spec *chan,
157 				     enum iio_event_type type,
158 				     enum iio_event_direction dir)
159 {
160 	struct mma9551_data *data = iio_priv(indio_dev);
161 
162 	switch (chan->type) {
163 	case IIO_INCLI:
164 		/* IIO counts axes from 1, because IIO_NO_MOD is 0. */
165 		return data->event_enabled[chan->channel2 - 1];
166 	default:
167 		return -EINVAL;
168 	}
169 }
170 
171 static int mma9551_config_incli_event(struct iio_dev *indio_dev,
172 				      enum iio_modifier axis,
173 				      int state)
174 {
175 	struct mma9551_data *data = iio_priv(indio_dev);
176 	enum mma9551_tilt_axis mma_axis;
177 	int ret;
178 
179 	/* IIO counts axes from 1, because IIO_NO_MOD is 0. */
180 	mma_axis = axis - 1;
181 
182 	if (data->event_enabled[mma_axis] == state)
183 		return 0;
184 
185 	if (state == 0) {
186 		ret = mma9551_gpio_config(data->client,
187 					  (enum mma9551_gpio_pin)mma_axis,
188 					  MMA9551_APPID_NONE, 0, 0);
189 		if (ret < 0)
190 			return ret;
191 
192 		ret = mma9551_set_power_state(data->client, false);
193 		if (ret < 0)
194 			return ret;
195 	} else {
196 		int bitnum;
197 
198 		/* Bit 7 of each angle register holds the angle flag. */
199 		switch (axis) {
200 		case IIO_MOD_X:
201 			bitnum = 7 + 8 * MMA9551_TILT_YZ_ANG_REG;
202 			break;
203 		case IIO_MOD_Y:
204 			bitnum = 7 + 8 * MMA9551_TILT_XZ_ANG_REG;
205 			break;
206 		case IIO_MOD_Z:
207 			bitnum = 7 + 8 * MMA9551_TILT_XY_ANG_REG;
208 			break;
209 		default:
210 			return -EINVAL;
211 		}
212 
213 
214 		ret = mma9551_set_power_state(data->client, true);
215 		if (ret < 0)
216 			return ret;
217 
218 		ret = mma9551_gpio_config(data->client,
219 					  (enum mma9551_gpio_pin)mma_axis,
220 					  MMA9551_APPID_TILT, bitnum, 0);
221 		if (ret < 0) {
222 			mma9551_set_power_state(data->client, false);
223 			return ret;
224 		}
225 	}
226 
227 	data->event_enabled[mma_axis] = state;
228 
229 	return ret;
230 }
231 
232 static int mma9551_write_event_config(struct iio_dev *indio_dev,
233 				      const struct iio_chan_spec *chan,
234 				      enum iio_event_type type,
235 				      enum iio_event_direction dir,
236 				      int state)
237 {
238 	struct mma9551_data *data = iio_priv(indio_dev);
239 	int ret;
240 
241 	switch (chan->type) {
242 	case IIO_INCLI:
243 		mutex_lock(&data->mutex);
244 		ret = mma9551_config_incli_event(indio_dev,
245 						 chan->channel2, state);
246 		mutex_unlock(&data->mutex);
247 		return ret;
248 	default:
249 		return -EINVAL;
250 	}
251 }
252 
253 static int mma9551_write_event_value(struct iio_dev *indio_dev,
254 				     const struct iio_chan_spec *chan,
255 				     enum iio_event_type type,
256 				     enum iio_event_direction dir,
257 				     enum iio_event_info info,
258 				     int val, int val2)
259 {
260 	struct mma9551_data *data = iio_priv(indio_dev);
261 	int ret;
262 
263 	switch (chan->type) {
264 	case IIO_INCLI:
265 		if (val2 != 0 || val < 1 || val > 10)
266 			return -EINVAL;
267 		mutex_lock(&data->mutex);
268 		ret = mma9551_update_config_bits(data->client,
269 						 MMA9551_APPID_TILT,
270 						 MMA9551_TILT_CFG_REG,
271 						 MMA9551_TILT_ANG_THRESH_MASK,
272 						 val);
273 		mutex_unlock(&data->mutex);
274 		return ret;
275 	default:
276 		return -EINVAL;
277 	}
278 }
279 
280 static int mma9551_read_event_value(struct iio_dev *indio_dev,
281 				    const struct iio_chan_spec *chan,
282 				    enum iio_event_type type,
283 				    enum iio_event_direction dir,
284 				    enum iio_event_info info,
285 				    int *val, int *val2)
286 {
287 	struct mma9551_data *data = iio_priv(indio_dev);
288 	int ret;
289 	u8 tmp;
290 
291 	switch (chan->type) {
292 	case IIO_INCLI:
293 		mutex_lock(&data->mutex);
294 		ret = mma9551_read_config_byte(data->client,
295 					       MMA9551_APPID_TILT,
296 					       MMA9551_TILT_CFG_REG, &tmp);
297 		mutex_unlock(&data->mutex);
298 		if (ret < 0)
299 			return ret;
300 		*val = tmp & MMA9551_TILT_ANG_THRESH_MASK;
301 		*val2 = 0;
302 		return IIO_VAL_INT;
303 	default:
304 		return -EINVAL;
305 	}
306 }
307 
308 static const struct iio_event_spec mma9551_incli_event = {
309 	.type = IIO_EV_TYPE_ROC,
310 	.dir = IIO_EV_DIR_RISING,
311 	.mask_separate = BIT(IIO_EV_INFO_ENABLE),
312 	.mask_shared_by_type = BIT(IIO_EV_INFO_VALUE),
313 };
314 
315 #define MMA9551_INCLI_CHANNEL(axis) {				\
316 	.type = IIO_INCLI,					\
317 	.modified = 1,						\
318 	.channel2 = axis,					\
319 	.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),	\
320 	.event_spec = &mma9551_incli_event,			\
321 	.num_event_specs = 1,					\
322 }
323 
324 static const struct iio_chan_spec mma9551_channels[] = {
325 	MMA9551_ACCEL_CHANNEL(IIO_MOD_X),
326 	MMA9551_ACCEL_CHANNEL(IIO_MOD_Y),
327 	MMA9551_ACCEL_CHANNEL(IIO_MOD_Z),
328 
329 	MMA9551_INCLI_CHANNEL(IIO_MOD_X),
330 	MMA9551_INCLI_CHANNEL(IIO_MOD_Y),
331 	MMA9551_INCLI_CHANNEL(IIO_MOD_Z),
332 };
333 
334 static const struct iio_info mma9551_info = {
335 	.read_raw = mma9551_read_raw,
336 	.read_event_config = mma9551_read_event_config,
337 	.write_event_config = mma9551_write_event_config,
338 	.read_event_value = mma9551_read_event_value,
339 	.write_event_value = mma9551_write_event_value,
340 };
341 
342 static irqreturn_t mma9551_event_handler(int irq, void *private)
343 {
344 	struct iio_dev *indio_dev = private;
345 	struct mma9551_data *data = iio_priv(indio_dev);
346 	int i, ret, mma_axis = -1;
347 	u16 reg;
348 	u8 val;
349 
350 	mutex_lock(&data->mutex);
351 
352 	for (i = 0; i < 3; i++)
353 		if (irq == data->irqs[i]) {
354 			mma_axis = i;
355 			break;
356 		}
357 
358 	if (mma_axis == -1) {
359 		/* IRQ was triggered on 4th line, which we don't use. */
360 		dev_warn(&data->client->dev,
361 			 "irq triggered on unused line %d\n", data->irqs[3]);
362 		goto out;
363 	}
364 
365 	switch (mma_axis) {
366 	case mma9551_x:
367 		reg = MMA9551_TILT_YZ_ANG_REG;
368 		break;
369 	case mma9551_y:
370 		reg = MMA9551_TILT_XZ_ANG_REG;
371 		break;
372 	case mma9551_z:
373 		reg = MMA9551_TILT_XY_ANG_REG;
374 		break;
375 	}
376 
377 	/*
378 	 * Read the angle even though we don't use it, otherwise we
379 	 * won't get any further interrupts.
380 	 */
381 	ret = mma9551_read_status_byte(data->client, MMA9551_APPID_TILT,
382 				       reg, &val);
383 	if (ret < 0) {
384 		dev_err(&data->client->dev,
385 			"error %d reading tilt register in IRQ\n", ret);
386 		goto out;
387 	}
388 
389 	iio_push_event(indio_dev,
390 		       IIO_MOD_EVENT_CODE(IIO_INCLI, 0, (mma_axis + 1),
391 					  IIO_EV_TYPE_ROC, IIO_EV_DIR_RISING),
392 		       iio_get_time_ns(indio_dev));
393 
394 out:
395 	mutex_unlock(&data->mutex);
396 
397 	return IRQ_HANDLED;
398 }
399 
400 static int mma9551_init(struct mma9551_data *data)
401 {
402 	int ret;
403 
404 	ret = mma9551_read_version(data->client);
405 	if (ret)
406 		return ret;
407 
408 	return mma9551_set_device_state(data->client, true);
409 }
410 
411 static int mma9551_gpio_probe(struct iio_dev *indio_dev)
412 {
413 	struct gpio_desc *gpio;
414 	int i, ret;
415 	struct mma9551_data *data = iio_priv(indio_dev);
416 	struct device *dev = &data->client->dev;
417 
418 	for (i = 0; i < MMA9551_GPIO_COUNT; i++) {
419 		gpio = devm_gpiod_get_index(dev, NULL, i, GPIOD_IN);
420 		if (IS_ERR(gpio)) {
421 			dev_err(dev, "acpi gpio get index failed\n");
422 			return PTR_ERR(gpio);
423 		}
424 
425 		ret = gpiod_to_irq(gpio);
426 		if (ret < 0)
427 			return ret;
428 
429 		data->irqs[i] = ret;
430 		ret = devm_request_threaded_irq(dev, data->irqs[i],
431 				NULL, mma9551_event_handler,
432 				IRQF_TRIGGER_RISING | IRQF_ONESHOT,
433 				MMA9551_IRQ_NAME, indio_dev);
434 		if (ret < 0) {
435 			dev_err(dev, "request irq %d failed\n", data->irqs[i]);
436 			return ret;
437 		}
438 
439 		dev_dbg(dev, "gpio resource, no:%d irq:%d\n",
440 			desc_to_gpio(gpio), data->irqs[i]);
441 	}
442 
443 	return 0;
444 }
445 
446 static const char *mma9551_match_acpi_device(struct device *dev)
447 {
448 	const struct acpi_device_id *id;
449 
450 	id = acpi_match_device(dev->driver->acpi_match_table, dev);
451 	if (!id)
452 		return NULL;
453 
454 	return dev_name(dev);
455 }
456 
457 static int mma9551_probe(struct i2c_client *client,
458 			 const struct i2c_device_id *id)
459 {
460 	struct mma9551_data *data;
461 	struct iio_dev *indio_dev;
462 	const char *name = NULL;
463 	int ret;
464 
465 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
466 	if (!indio_dev)
467 		return -ENOMEM;
468 
469 	data = iio_priv(indio_dev);
470 	i2c_set_clientdata(client, indio_dev);
471 	data->client = client;
472 
473 	if (id)
474 		name = id->name;
475 	else if (ACPI_HANDLE(&client->dev))
476 		name = mma9551_match_acpi_device(&client->dev);
477 
478 	ret = mma9551_init(data);
479 	if (ret < 0)
480 		return ret;
481 
482 	mutex_init(&data->mutex);
483 
484 	indio_dev->dev.parent = &client->dev;
485 	indio_dev->channels = mma9551_channels;
486 	indio_dev->num_channels = ARRAY_SIZE(mma9551_channels);
487 	indio_dev->name = name;
488 	indio_dev->modes = INDIO_DIRECT_MODE;
489 	indio_dev->info = &mma9551_info;
490 
491 	ret = mma9551_gpio_probe(indio_dev);
492 	if (ret < 0)
493 		goto out_poweroff;
494 
495 	ret = pm_runtime_set_active(&client->dev);
496 	if (ret < 0)
497 		goto out_poweroff;
498 
499 	pm_runtime_enable(&client->dev);
500 	pm_runtime_set_autosuspend_delay(&client->dev,
501 					 MMA9551_AUTO_SUSPEND_DELAY_MS);
502 	pm_runtime_use_autosuspend(&client->dev);
503 
504 	ret = iio_device_register(indio_dev);
505 	if (ret < 0) {
506 		dev_err(&client->dev, "unable to register iio device\n");
507 		goto out_poweroff;
508 	}
509 
510 	return 0;
511 
512 out_poweroff:
513 	mma9551_set_device_state(client, false);
514 
515 	return ret;
516 }
517 
518 static int mma9551_remove(struct i2c_client *client)
519 {
520 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
521 	struct mma9551_data *data = iio_priv(indio_dev);
522 
523 	iio_device_unregister(indio_dev);
524 
525 	pm_runtime_disable(&client->dev);
526 	pm_runtime_set_suspended(&client->dev);
527 	pm_runtime_put_noidle(&client->dev);
528 
529 	mutex_lock(&data->mutex);
530 	mma9551_set_device_state(data->client, false);
531 	mutex_unlock(&data->mutex);
532 
533 	return 0;
534 }
535 
536 #ifdef CONFIG_PM
537 static int mma9551_runtime_suspend(struct device *dev)
538 {
539 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
540 	struct mma9551_data *data = iio_priv(indio_dev);
541 	int ret;
542 
543 	mutex_lock(&data->mutex);
544 	ret = mma9551_set_device_state(data->client, false);
545 	mutex_unlock(&data->mutex);
546 	if (ret < 0) {
547 		dev_err(&data->client->dev, "powering off device failed\n");
548 		return -EAGAIN;
549 	}
550 
551 	return 0;
552 }
553 
554 static int mma9551_runtime_resume(struct device *dev)
555 {
556 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
557 	struct mma9551_data *data = iio_priv(indio_dev);
558 	int ret;
559 
560 	ret = mma9551_set_device_state(data->client, true);
561 	if (ret < 0)
562 		return ret;
563 
564 	mma9551_sleep(MMA9551_DEFAULT_SAMPLE_RATE);
565 
566 	return 0;
567 }
568 #endif
569 
570 #ifdef CONFIG_PM_SLEEP
571 static int mma9551_suspend(struct device *dev)
572 {
573 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
574 	struct mma9551_data *data = iio_priv(indio_dev);
575 	int ret;
576 
577 	mutex_lock(&data->mutex);
578 	ret = mma9551_set_device_state(data->client, false);
579 	mutex_unlock(&data->mutex);
580 
581 	return ret;
582 }
583 
584 static int mma9551_resume(struct device *dev)
585 {
586 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
587 	struct mma9551_data *data = iio_priv(indio_dev);
588 	int ret;
589 
590 	mutex_lock(&data->mutex);
591 	ret = mma9551_set_device_state(data->client, true);
592 	mutex_unlock(&data->mutex);
593 
594 	return ret;
595 }
596 #endif
597 
598 static const struct dev_pm_ops mma9551_pm_ops = {
599 	SET_SYSTEM_SLEEP_PM_OPS(mma9551_suspend, mma9551_resume)
600 	SET_RUNTIME_PM_OPS(mma9551_runtime_suspend,
601 			   mma9551_runtime_resume, NULL)
602 };
603 
604 static const struct acpi_device_id mma9551_acpi_match[] = {
605 	{"MMA9551", 0},
606 	{},
607 };
608 
609 MODULE_DEVICE_TABLE(acpi, mma9551_acpi_match);
610 
611 static const struct i2c_device_id mma9551_id[] = {
612 	{"mma9551", 0},
613 	{}
614 };
615 
616 MODULE_DEVICE_TABLE(i2c, mma9551_id);
617 
618 static struct i2c_driver mma9551_driver = {
619 	.driver = {
620 		   .name = MMA9551_DRV_NAME,
621 		   .acpi_match_table = ACPI_PTR(mma9551_acpi_match),
622 		   .pm = &mma9551_pm_ops,
623 		   },
624 	.probe = mma9551_probe,
625 	.remove = mma9551_remove,
626 	.id_table = mma9551_id,
627 };
628 
629 module_i2c_driver(mma9551_driver);
630 
631 MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>");
632 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
633 MODULE_LICENSE("GPL v2");
634 MODULE_DESCRIPTION("MMA9551L motion-sensing platform driver");
635