2 * Freescale MMA9551L Intelligent Motion-Sensing Platform driver
3 * Copyright (c) 2014, Intel Corporation.
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.
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
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"
28 #define MMA9551_DRV_NAME "mma9551"
29 #define MMA9551_IRQ_NAME "mma9551_event"
30 #define MMA9551_GPIO_COUNT 4
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)
44 #define MMA9551_DEFAULT_SAMPLE_RATE 122 /* Hz */
46 /* Tilt events are mapped to the first three GPIO pins. */
47 enum mma9551_tilt_axis {
54 struct i2c_client *client;
57 int irqs[MMA9551_GPIO_COUNT];
60 static int mma9551_read_incli_chan(struct i2c_client *client,
61 const struct iio_chan_spec *chan,
64 u8 quad_shift, angle, quadrant;
68 switch (chan->channel2) {
70 reg_addr = MMA9551_TILT_YZ_ANG_REG;
71 quad_shift = MMA9551_TILT_YZ_QUAD_SHIFT;
74 reg_addr = MMA9551_TILT_XZ_ANG_REG;
75 quad_shift = MMA9551_TILT_XZ_QUAD_SHIFT;
78 reg_addr = MMA9551_TILT_XY_ANG_REG;
79 quad_shift = MMA9551_TILT_XY_QUAD_SHIFT;
85 ret = mma9551_set_power_state(client, true);
89 ret = mma9551_read_status_byte(client, MMA9551_APPID_TILT,
94 ret = mma9551_read_status_byte(client, MMA9551_APPID_TILT,
95 MMA9551_TILT_QUAD_REG, &quadrant);
99 angle &= ~MMA9551_TILT_ANGFLG;
100 quadrant = (quadrant >> quad_shift) & 0x03;
102 if (quadrant == 1 || quadrant == 3)
103 *val = 90 * (quadrant + 1) - angle;
105 *val = angle + 90 * quadrant;
110 mma9551_set_power_state(client, false);
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)
118 struct mma9551_data *data = iio_priv(indio_dev);
122 case IIO_CHAN_INFO_PROCESSED:
123 switch (chan->type) {
125 mutex_lock(&data->mutex);
126 ret = mma9551_read_incli_chan(data->client, chan, val);
127 mutex_unlock(&data->mutex);
132 case IIO_CHAN_INFO_RAW:
133 switch (chan->type) {
135 mutex_lock(&data->mutex);
136 ret = mma9551_read_accel_chan(data->client,
138 mutex_unlock(&data->mutex);
143 case IIO_CHAN_INFO_SCALE:
144 switch (chan->type) {
146 return mma9551_read_accel_scale(val, val2);
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)
160 struct mma9551_data *data = iio_priv(indio_dev);
162 switch (chan->type) {
164 /* IIO counts axes from 1, because IIO_NO_MOD is 0. */
165 return data->event_enabled[chan->channel2 - 1];
171 static int mma9551_config_incli_event(struct iio_dev *indio_dev,
172 enum iio_modifier axis,
175 struct mma9551_data *data = iio_priv(indio_dev);
176 enum mma9551_tilt_axis mma_axis;
179 /* IIO counts axes from 1, because IIO_NO_MOD is 0. */
182 if (data->event_enabled[mma_axis] == state)
186 ret = mma9551_gpio_config(data->client,
187 (enum mma9551_gpio_pin)mma_axis,
188 MMA9551_APPID_NONE, 0, 0);
192 ret = mma9551_set_power_state(data->client, false);
198 /* Bit 7 of each angle register holds the angle flag. */
201 bitnum = 7 + 8 * MMA9551_TILT_YZ_ANG_REG;
204 bitnum = 7 + 8 * MMA9551_TILT_XZ_ANG_REG;
207 bitnum = 7 + 8 * MMA9551_TILT_XY_ANG_REG;
214 ret = mma9551_set_power_state(data->client, true);
218 ret = mma9551_gpio_config(data->client,
219 (enum mma9551_gpio_pin)mma_axis,
220 MMA9551_APPID_TILT, bitnum, 0);
222 mma9551_set_power_state(data->client, false);
227 data->event_enabled[mma_axis] = state;
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,
238 struct mma9551_data *data = iio_priv(indio_dev);
241 switch (chan->type) {
243 mutex_lock(&data->mutex);
244 ret = mma9551_config_incli_event(indio_dev,
245 chan->channel2, state);
246 mutex_unlock(&data->mutex);
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,
260 struct mma9551_data *data = iio_priv(indio_dev);
263 switch (chan->type) {
265 if (val2 != 0 || val < 1 || val > 10)
267 mutex_lock(&data->mutex);
268 ret = mma9551_update_config_bits(data->client,
270 MMA9551_TILT_CFG_REG,
271 MMA9551_TILT_ANG_THRESH_MASK,
273 mutex_unlock(&data->mutex);
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,
287 struct mma9551_data *data = iio_priv(indio_dev);
291 switch (chan->type) {
293 mutex_lock(&data->mutex);
294 ret = mma9551_read_config_byte(data->client,
296 MMA9551_TILT_CFG_REG, &tmp);
297 mutex_unlock(&data->mutex);
300 *val = tmp & MMA9551_TILT_ANG_THRESH_MASK;
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),
315 #define MMA9551_INCLI_CHANNEL(axis) { \
319 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
320 .event_spec = &mma9551_incli_event, \
321 .num_event_specs = 1, \
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),
329 MMA9551_INCLI_CHANNEL(IIO_MOD_X),
330 MMA9551_INCLI_CHANNEL(IIO_MOD_Y),
331 MMA9551_INCLI_CHANNEL(IIO_MOD_Z),
334 static const struct iio_info mma9551_info = {
335 .driver_module = THIS_MODULE,
336 .read_raw = mma9551_read_raw,
337 .read_event_config = mma9551_read_event_config,
338 .write_event_config = mma9551_write_event_config,
339 .read_event_value = mma9551_read_event_value,
340 .write_event_value = mma9551_write_event_value,
343 static irqreturn_t mma9551_event_handler(int irq, void *private)
345 struct iio_dev *indio_dev = private;
346 struct mma9551_data *data = iio_priv(indio_dev);
347 int i, ret, mma_axis = -1;
351 mutex_lock(&data->mutex);
353 for (i = 0; i < 3; i++)
354 if (irq == data->irqs[i]) {
359 if (mma_axis == -1) {
360 /* IRQ was triggered on 4th line, which we don't use. */
361 dev_warn(&data->client->dev,
362 "irq triggered on unused line %d\n", data->irqs[3]);
368 reg = MMA9551_TILT_YZ_ANG_REG;
371 reg = MMA9551_TILT_XZ_ANG_REG;
374 reg = MMA9551_TILT_XY_ANG_REG;
379 * Read the angle even though we don't use it, otherwise we
380 * won't get any further interrupts.
382 ret = mma9551_read_status_byte(data->client, MMA9551_APPID_TILT,
385 dev_err(&data->client->dev,
386 "error %d reading tilt register in IRQ\n", ret);
390 iio_push_event(indio_dev,
391 IIO_MOD_EVENT_CODE(IIO_INCLI, 0, (mma_axis + 1),
392 IIO_EV_TYPE_ROC, IIO_EV_DIR_RISING),
393 iio_get_time_ns(indio_dev));
396 mutex_unlock(&data->mutex);
401 static int mma9551_init(struct mma9551_data *data)
405 ret = mma9551_read_version(data->client);
409 return mma9551_set_device_state(data->client, true);
412 static int mma9551_gpio_probe(struct iio_dev *indio_dev)
414 struct gpio_desc *gpio;
416 struct mma9551_data *data = iio_priv(indio_dev);
417 struct device *dev = &data->client->dev;
419 for (i = 0; i < MMA9551_GPIO_COUNT; i++) {
420 gpio = devm_gpiod_get_index(dev, NULL, i, GPIOD_IN);
422 dev_err(dev, "acpi gpio get index failed\n");
423 return PTR_ERR(gpio);
426 ret = gpiod_to_irq(gpio);
431 ret = devm_request_threaded_irq(dev, data->irqs[i],
432 NULL, mma9551_event_handler,
433 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
434 MMA9551_IRQ_NAME, indio_dev);
436 dev_err(dev, "request irq %d failed\n", data->irqs[i]);
440 dev_dbg(dev, "gpio resource, no:%d irq:%d\n",
441 desc_to_gpio(gpio), data->irqs[i]);
447 static const char *mma9551_match_acpi_device(struct device *dev)
449 const struct acpi_device_id *id;
451 id = acpi_match_device(dev->driver->acpi_match_table, dev);
455 return dev_name(dev);
458 static int mma9551_probe(struct i2c_client *client,
459 const struct i2c_device_id *id)
461 struct mma9551_data *data;
462 struct iio_dev *indio_dev;
463 const char *name = NULL;
466 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
470 data = iio_priv(indio_dev);
471 i2c_set_clientdata(client, indio_dev);
472 data->client = client;
476 else if (ACPI_HANDLE(&client->dev))
477 name = mma9551_match_acpi_device(&client->dev);
479 ret = mma9551_init(data);
483 mutex_init(&data->mutex);
485 indio_dev->dev.parent = &client->dev;
486 indio_dev->channels = mma9551_channels;
487 indio_dev->num_channels = ARRAY_SIZE(mma9551_channels);
488 indio_dev->name = name;
489 indio_dev->modes = INDIO_DIRECT_MODE;
490 indio_dev->info = &mma9551_info;
492 ret = mma9551_gpio_probe(indio_dev);
496 ret = pm_runtime_set_active(&client->dev);
500 pm_runtime_enable(&client->dev);
501 pm_runtime_set_autosuspend_delay(&client->dev,
502 MMA9551_AUTO_SUSPEND_DELAY_MS);
503 pm_runtime_use_autosuspend(&client->dev);
505 ret = iio_device_register(indio_dev);
507 dev_err(&client->dev, "unable to register iio device\n");
514 mma9551_set_device_state(client, false);
519 static int mma9551_remove(struct i2c_client *client)
521 struct iio_dev *indio_dev = i2c_get_clientdata(client);
522 struct mma9551_data *data = iio_priv(indio_dev);
524 iio_device_unregister(indio_dev);
526 pm_runtime_disable(&client->dev);
527 pm_runtime_set_suspended(&client->dev);
528 pm_runtime_put_noidle(&client->dev);
530 mutex_lock(&data->mutex);
531 mma9551_set_device_state(data->client, false);
532 mutex_unlock(&data->mutex);
538 static int mma9551_runtime_suspend(struct device *dev)
540 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
541 struct mma9551_data *data = iio_priv(indio_dev);
544 mutex_lock(&data->mutex);
545 ret = mma9551_set_device_state(data->client, false);
546 mutex_unlock(&data->mutex);
548 dev_err(&data->client->dev, "powering off device failed\n");
555 static int mma9551_runtime_resume(struct device *dev)
557 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
558 struct mma9551_data *data = iio_priv(indio_dev);
561 ret = mma9551_set_device_state(data->client, true);
565 mma9551_sleep(MMA9551_DEFAULT_SAMPLE_RATE);
571 #ifdef CONFIG_PM_SLEEP
572 static int mma9551_suspend(struct device *dev)
574 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
575 struct mma9551_data *data = iio_priv(indio_dev);
578 mutex_lock(&data->mutex);
579 ret = mma9551_set_device_state(data->client, false);
580 mutex_unlock(&data->mutex);
585 static int mma9551_resume(struct device *dev)
587 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
588 struct mma9551_data *data = iio_priv(indio_dev);
591 mutex_lock(&data->mutex);
592 ret = mma9551_set_device_state(data->client, true);
593 mutex_unlock(&data->mutex);
599 static const struct dev_pm_ops mma9551_pm_ops = {
600 SET_SYSTEM_SLEEP_PM_OPS(mma9551_suspend, mma9551_resume)
601 SET_RUNTIME_PM_OPS(mma9551_runtime_suspend,
602 mma9551_runtime_resume, NULL)
605 static const struct acpi_device_id mma9551_acpi_match[] = {
610 MODULE_DEVICE_TABLE(acpi, mma9551_acpi_match);
612 static const struct i2c_device_id mma9551_id[] = {
617 MODULE_DEVICE_TABLE(i2c, mma9551_id);
619 static struct i2c_driver mma9551_driver = {
621 .name = MMA9551_DRV_NAME,
622 .acpi_match_table = ACPI_PTR(mma9551_acpi_match),
623 .pm = &mma9551_pm_ops,
625 .probe = mma9551_probe,
626 .remove = mma9551_remove,
627 .id_table = mma9551_id,
630 module_i2c_driver(mma9551_driver);
632 MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>");
633 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
634 MODULE_LICENSE("GPL v2");
635 MODULE_DESCRIPTION("MMA9551L motion-sensing platform driver");