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_NAME "mma9551_int"
31 #define MMA9551_GPIO_COUNT 4
33 /* Tilt application (inclination in IIO terms). */
34 #define MMA9551_TILT_XZ_ANG_REG 0x00
35 #define MMA9551_TILT_YZ_ANG_REG 0x01
36 #define MMA9551_TILT_XY_ANG_REG 0x02
37 #define MMA9551_TILT_ANGFLG BIT(7)
38 #define MMA9551_TILT_QUAD_REG 0x03
39 #define MMA9551_TILT_XY_QUAD_SHIFT 0
40 #define MMA9551_TILT_YZ_QUAD_SHIFT 2
41 #define MMA9551_TILT_XZ_QUAD_SHIFT 4
42 #define MMA9551_TILT_CFG_REG 0x01
43 #define MMA9551_TILT_ANG_THRESH_MASK GENMASK(3, 0)
45 #define MMA9551_DEFAULT_SAMPLE_RATE 122 /* Hz */
47 /* Tilt events are mapped to the first three GPIO pins. */
48 enum mma9551_tilt_axis
{
55 struct i2c_client
*client
;
58 int irqs
[MMA9551_GPIO_COUNT
];
61 static int mma9551_read_incli_chan(struct i2c_client
*client
,
62 const struct iio_chan_spec
*chan
,
65 u8 quad_shift
, angle
, quadrant
;
69 switch (chan
->channel2
) {
71 reg_addr
= MMA9551_TILT_YZ_ANG_REG
;
72 quad_shift
= MMA9551_TILT_YZ_QUAD_SHIFT
;
75 reg_addr
= MMA9551_TILT_XZ_ANG_REG
;
76 quad_shift
= MMA9551_TILT_XZ_QUAD_SHIFT
;
79 reg_addr
= MMA9551_TILT_XY_ANG_REG
;
80 quad_shift
= MMA9551_TILT_XY_QUAD_SHIFT
;
86 ret
= mma9551_set_power_state(client
, true);
90 ret
= mma9551_read_status_byte(client
, MMA9551_APPID_TILT
,
95 ret
= mma9551_read_status_byte(client
, MMA9551_APPID_TILT
,
96 MMA9551_TILT_QUAD_REG
, &quadrant
);
100 angle
&= ~MMA9551_TILT_ANGFLG
;
101 quadrant
= (quadrant
>> quad_shift
) & 0x03;
103 if (quadrant
== 1 || quadrant
== 3)
104 *val
= 90 * (quadrant
+ 1) - angle
;
106 *val
= angle
+ 90 * quadrant
;
111 mma9551_set_power_state(client
, false);
115 static int mma9551_read_raw(struct iio_dev
*indio_dev
,
116 struct iio_chan_spec
const *chan
,
117 int *val
, int *val2
, long mask
)
119 struct mma9551_data
*data
= iio_priv(indio_dev
);
123 case IIO_CHAN_INFO_PROCESSED
:
124 switch (chan
->type
) {
126 mutex_lock(&data
->mutex
);
127 ret
= mma9551_read_incli_chan(data
->client
, chan
, val
);
128 mutex_unlock(&data
->mutex
);
133 case IIO_CHAN_INFO_RAW
:
134 switch (chan
->type
) {
136 mutex_lock(&data
->mutex
);
137 ret
= mma9551_read_accel_chan(data
->client
,
139 mutex_unlock(&data
->mutex
);
144 case IIO_CHAN_INFO_SCALE
:
145 switch (chan
->type
) {
147 return mma9551_read_accel_scale(val
, val2
);
156 static int mma9551_read_event_config(struct iio_dev
*indio_dev
,
157 const struct iio_chan_spec
*chan
,
158 enum iio_event_type type
,
159 enum iio_event_direction dir
)
161 struct mma9551_data
*data
= iio_priv(indio_dev
);
163 switch (chan
->type
) {
165 /* IIO counts axes from 1, because IIO_NO_MOD is 0. */
166 return data
->event_enabled
[chan
->channel2
- 1];
172 static int mma9551_config_incli_event(struct iio_dev
*indio_dev
,
173 enum iio_modifier axis
,
176 struct mma9551_data
*data
= iio_priv(indio_dev
);
177 enum mma9551_tilt_axis mma_axis
;
180 /* IIO counts axes from 1, because IIO_NO_MOD is 0. */
183 if (data
->event_enabled
[mma_axis
] == state
)
187 ret
= mma9551_gpio_config(data
->client
,
188 (enum mma9551_gpio_pin
)mma_axis
,
189 MMA9551_APPID_NONE
, 0, 0);
193 ret
= mma9551_set_power_state(data
->client
, false);
199 /* Bit 7 of each angle register holds the angle flag. */
202 bitnum
= 7 + 8 * MMA9551_TILT_YZ_ANG_REG
;
205 bitnum
= 7 + 8 * MMA9551_TILT_XZ_ANG_REG
;
208 bitnum
= 7 + 8 * MMA9551_TILT_XY_ANG_REG
;
215 ret
= mma9551_set_power_state(data
->client
, true);
219 ret
= mma9551_gpio_config(data
->client
,
220 (enum mma9551_gpio_pin
)mma_axis
,
221 MMA9551_APPID_TILT
, bitnum
, 0);
223 mma9551_set_power_state(data
->client
, false);
228 data
->event_enabled
[mma_axis
] = state
;
233 static int mma9551_write_event_config(struct iio_dev
*indio_dev
,
234 const struct iio_chan_spec
*chan
,
235 enum iio_event_type type
,
236 enum iio_event_direction dir
,
239 struct mma9551_data
*data
= iio_priv(indio_dev
);
242 switch (chan
->type
) {
244 mutex_lock(&data
->mutex
);
245 ret
= mma9551_config_incli_event(indio_dev
,
246 chan
->channel2
, state
);
247 mutex_unlock(&data
->mutex
);
254 static int mma9551_write_event_value(struct iio_dev
*indio_dev
,
255 const struct iio_chan_spec
*chan
,
256 enum iio_event_type type
,
257 enum iio_event_direction dir
,
258 enum iio_event_info info
,
261 struct mma9551_data
*data
= iio_priv(indio_dev
);
264 switch (chan
->type
) {
266 if (val2
!= 0 || val
< 1 || val
> 10)
268 mutex_lock(&data
->mutex
);
269 ret
= mma9551_update_config_bits(data
->client
,
271 MMA9551_TILT_CFG_REG
,
272 MMA9551_TILT_ANG_THRESH_MASK
,
274 mutex_unlock(&data
->mutex
);
281 static int mma9551_read_event_value(struct iio_dev
*indio_dev
,
282 const struct iio_chan_spec
*chan
,
283 enum iio_event_type type
,
284 enum iio_event_direction dir
,
285 enum iio_event_info info
,
288 struct mma9551_data
*data
= iio_priv(indio_dev
);
292 switch (chan
->type
) {
294 mutex_lock(&data
->mutex
);
295 ret
= mma9551_read_config_byte(data
->client
,
297 MMA9551_TILT_CFG_REG
, &tmp
);
298 mutex_unlock(&data
->mutex
);
301 *val
= tmp
& MMA9551_TILT_ANG_THRESH_MASK
;
309 static const struct iio_event_spec mma9551_incli_event
= {
310 .type
= IIO_EV_TYPE_ROC
,
311 .dir
= IIO_EV_DIR_RISING
,
312 .mask_separate
= BIT(IIO_EV_INFO_ENABLE
),
313 .mask_shared_by_type
= BIT(IIO_EV_INFO_VALUE
),
316 #define MMA9551_INCLI_CHANNEL(axis) { \
320 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
321 .event_spec = &mma9551_incli_event, \
322 .num_event_specs = 1, \
325 static const struct iio_chan_spec mma9551_channels
[] = {
326 MMA9551_ACCEL_CHANNEL(IIO_MOD_X
),
327 MMA9551_ACCEL_CHANNEL(IIO_MOD_Y
),
328 MMA9551_ACCEL_CHANNEL(IIO_MOD_Z
),
330 MMA9551_INCLI_CHANNEL(IIO_MOD_X
),
331 MMA9551_INCLI_CHANNEL(IIO_MOD_Y
),
332 MMA9551_INCLI_CHANNEL(IIO_MOD_Z
),
335 static const struct iio_info mma9551_info
= {
336 .driver_module
= THIS_MODULE
,
337 .read_raw
= mma9551_read_raw
,
338 .read_event_config
= mma9551_read_event_config
,
339 .write_event_config
= mma9551_write_event_config
,
340 .read_event_value
= mma9551_read_event_value
,
341 .write_event_value
= mma9551_write_event_value
,
344 static irqreturn_t
mma9551_event_handler(int irq
, void *private)
346 struct iio_dev
*indio_dev
= private;
347 struct mma9551_data
*data
= iio_priv(indio_dev
);
348 int i
, ret
, mma_axis
= -1;
352 mutex_lock(&data
->mutex
);
354 for (i
= 0; i
< 3; i
++)
355 if (irq
== data
->irqs
[i
]) {
360 if (mma_axis
== -1) {
361 /* IRQ was triggered on 4th line, which we don't use. */
362 dev_warn(&data
->client
->dev
,
363 "irq triggered on unused line %d\n", data
->irqs
[3]);
369 reg
= MMA9551_TILT_YZ_ANG_REG
;
372 reg
= MMA9551_TILT_XZ_ANG_REG
;
375 reg
= MMA9551_TILT_XY_ANG_REG
;
380 * Read the angle even though we don't use it, otherwise we
381 * won't get any further interrupts.
383 ret
= mma9551_read_status_byte(data
->client
, MMA9551_APPID_TILT
,
386 dev_err(&data
->client
->dev
,
387 "error %d reading tilt register in IRQ\n", ret
);
391 iio_push_event(indio_dev
,
392 IIO_MOD_EVENT_CODE(IIO_INCLI
, 0, (mma_axis
+ 1),
393 IIO_EV_TYPE_ROC
, IIO_EV_DIR_RISING
),
394 iio_get_time_ns(indio_dev
));
397 mutex_unlock(&data
->mutex
);
402 static int mma9551_init(struct mma9551_data
*data
)
406 ret
= mma9551_read_version(data
->client
);
410 return mma9551_set_device_state(data
->client
, true);
413 static int mma9551_gpio_probe(struct iio_dev
*indio_dev
)
415 struct gpio_desc
*gpio
;
417 struct mma9551_data
*data
= iio_priv(indio_dev
);
418 struct device
*dev
= &data
->client
->dev
;
420 for (i
= 0; i
< MMA9551_GPIO_COUNT
; i
++) {
421 gpio
= devm_gpiod_get_index(dev
, MMA9551_GPIO_NAME
, i
,
424 dev_err(dev
, "acpi gpio get index failed\n");
425 return PTR_ERR(gpio
);
428 ret
= gpiod_to_irq(gpio
);
433 ret
= devm_request_threaded_irq(dev
, data
->irqs
[i
],
434 NULL
, mma9551_event_handler
,
435 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
436 MMA9551_IRQ_NAME
, indio_dev
);
438 dev_err(dev
, "request irq %d failed\n", data
->irqs
[i
]);
442 dev_dbg(dev
, "gpio resource, no:%d irq:%d\n",
443 desc_to_gpio(gpio
), data
->irqs
[i
]);
449 static const char *mma9551_match_acpi_device(struct device
*dev
)
451 const struct acpi_device_id
*id
;
453 id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
457 return dev_name(dev
);
460 static int mma9551_probe(struct i2c_client
*client
,
461 const struct i2c_device_id
*id
)
463 struct mma9551_data
*data
;
464 struct iio_dev
*indio_dev
;
465 const char *name
= NULL
;
468 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
472 data
= iio_priv(indio_dev
);
473 i2c_set_clientdata(client
, indio_dev
);
474 data
->client
= client
;
478 else if (ACPI_HANDLE(&client
->dev
))
479 name
= mma9551_match_acpi_device(&client
->dev
);
481 ret
= mma9551_init(data
);
485 mutex_init(&data
->mutex
);
487 indio_dev
->dev
.parent
= &client
->dev
;
488 indio_dev
->channels
= mma9551_channels
;
489 indio_dev
->num_channels
= ARRAY_SIZE(mma9551_channels
);
490 indio_dev
->name
= name
;
491 indio_dev
->modes
= INDIO_DIRECT_MODE
;
492 indio_dev
->info
= &mma9551_info
;
494 ret
= mma9551_gpio_probe(indio_dev
);
498 ret
= pm_runtime_set_active(&client
->dev
);
502 pm_runtime_enable(&client
->dev
);
503 pm_runtime_set_autosuspend_delay(&client
->dev
,
504 MMA9551_AUTO_SUSPEND_DELAY_MS
);
505 pm_runtime_use_autosuspend(&client
->dev
);
507 ret
= iio_device_register(indio_dev
);
509 dev_err(&client
->dev
, "unable to register iio device\n");
516 mma9551_set_device_state(client
, false);
521 static int mma9551_remove(struct i2c_client
*client
)
523 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
524 struct mma9551_data
*data
= iio_priv(indio_dev
);
526 iio_device_unregister(indio_dev
);
528 pm_runtime_disable(&client
->dev
);
529 pm_runtime_set_suspended(&client
->dev
);
530 pm_runtime_put_noidle(&client
->dev
);
532 mutex_lock(&data
->mutex
);
533 mma9551_set_device_state(data
->client
, false);
534 mutex_unlock(&data
->mutex
);
540 static int mma9551_runtime_suspend(struct device
*dev
)
542 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
543 struct mma9551_data
*data
= iio_priv(indio_dev
);
546 mutex_lock(&data
->mutex
);
547 ret
= mma9551_set_device_state(data
->client
, false);
548 mutex_unlock(&data
->mutex
);
550 dev_err(&data
->client
->dev
, "powering off device failed\n");
557 static int mma9551_runtime_resume(struct device
*dev
)
559 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
560 struct mma9551_data
*data
= iio_priv(indio_dev
);
563 ret
= mma9551_set_device_state(data
->client
, true);
567 mma9551_sleep(MMA9551_DEFAULT_SAMPLE_RATE
);
573 #ifdef CONFIG_PM_SLEEP
574 static int mma9551_suspend(struct device
*dev
)
576 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
577 struct mma9551_data
*data
= iio_priv(indio_dev
);
580 mutex_lock(&data
->mutex
);
581 ret
= mma9551_set_device_state(data
->client
, false);
582 mutex_unlock(&data
->mutex
);
587 static int mma9551_resume(struct device
*dev
)
589 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
590 struct mma9551_data
*data
= iio_priv(indio_dev
);
593 mutex_lock(&data
->mutex
);
594 ret
= mma9551_set_device_state(data
->client
, true);
595 mutex_unlock(&data
->mutex
);
601 static const struct dev_pm_ops mma9551_pm_ops
= {
602 SET_SYSTEM_SLEEP_PM_OPS(mma9551_suspend
, mma9551_resume
)
603 SET_RUNTIME_PM_OPS(mma9551_runtime_suspend
,
604 mma9551_runtime_resume
, NULL
)
607 static const struct acpi_device_id mma9551_acpi_match
[] = {
612 MODULE_DEVICE_TABLE(acpi
, mma9551_acpi_match
);
614 static const struct i2c_device_id mma9551_id
[] = {
619 MODULE_DEVICE_TABLE(i2c
, mma9551_id
);
621 static struct i2c_driver mma9551_driver
= {
623 .name
= MMA9551_DRV_NAME
,
624 .acpi_match_table
= ACPI_PTR(mma9551_acpi_match
),
625 .pm
= &mma9551_pm_ops
,
627 .probe
= mma9551_probe
,
628 .remove
= mma9551_remove
,
629 .id_table
= mma9551_id
,
632 module_i2c_driver(mma9551_driver
);
634 MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>");
635 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
636 MODULE_LICENSE("GPL v2");
637 MODULE_DESCRIPTION("MMA9551L motion-sensing platform driver");