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 .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
,
342 static irqreturn_t
mma9551_event_handler(int irq
, void *private)
344 struct iio_dev
*indio_dev
= private;
345 struct mma9551_data
*data
= iio_priv(indio_dev
);
346 int i
, ret
, mma_axis
= -1;
350 mutex_lock(&data
->mutex
);
352 for (i
= 0; i
< 3; i
++)
353 if (irq
== data
->irqs
[i
]) {
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]);
367 reg
= MMA9551_TILT_YZ_ANG_REG
;
370 reg
= MMA9551_TILT_XZ_ANG_REG
;
373 reg
= MMA9551_TILT_XY_ANG_REG
;
378 * Read the angle even though we don't use it, otherwise we
379 * won't get any further interrupts.
381 ret
= mma9551_read_status_byte(data
->client
, MMA9551_APPID_TILT
,
384 dev_err(&data
->client
->dev
,
385 "error %d reading tilt register in IRQ\n", ret
);
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
));
395 mutex_unlock(&data
->mutex
);
400 static int mma9551_init(struct mma9551_data
*data
)
404 ret
= mma9551_read_version(data
->client
);
408 return mma9551_set_device_state(data
->client
, true);
411 static int mma9551_gpio_probe(struct iio_dev
*indio_dev
)
413 struct gpio_desc
*gpio
;
415 struct mma9551_data
*data
= iio_priv(indio_dev
);
416 struct device
*dev
= &data
->client
->dev
;
418 for (i
= 0; i
< MMA9551_GPIO_COUNT
; i
++) {
419 gpio
= devm_gpiod_get_index(dev
, NULL
, i
, GPIOD_IN
);
421 dev_err(dev
, "acpi gpio get index failed\n");
422 return PTR_ERR(gpio
);
425 ret
= gpiod_to_irq(gpio
);
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
);
435 dev_err(dev
, "request irq %d failed\n", data
->irqs
[i
]);
439 dev_dbg(dev
, "gpio resource, no:%d irq:%d\n",
440 desc_to_gpio(gpio
), data
->irqs
[i
]);
446 static const char *mma9551_match_acpi_device(struct device
*dev
)
448 const struct acpi_device_id
*id
;
450 id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
454 return dev_name(dev
);
457 static int mma9551_probe(struct i2c_client
*client
,
458 const struct i2c_device_id
*id
)
460 struct mma9551_data
*data
;
461 struct iio_dev
*indio_dev
;
462 const char *name
= NULL
;
465 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
469 data
= iio_priv(indio_dev
);
470 i2c_set_clientdata(client
, indio_dev
);
471 data
->client
= client
;
475 else if (ACPI_HANDLE(&client
->dev
))
476 name
= mma9551_match_acpi_device(&client
->dev
);
478 ret
= mma9551_init(data
);
482 mutex_init(&data
->mutex
);
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
;
491 ret
= mma9551_gpio_probe(indio_dev
);
495 ret
= pm_runtime_set_active(&client
->dev
);
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
);
504 ret
= iio_device_register(indio_dev
);
506 dev_err(&client
->dev
, "unable to register iio device\n");
513 mma9551_set_device_state(client
, false);
518 static int mma9551_remove(struct i2c_client
*client
)
520 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
521 struct mma9551_data
*data
= iio_priv(indio_dev
);
523 iio_device_unregister(indio_dev
);
525 pm_runtime_disable(&client
->dev
);
526 pm_runtime_set_suspended(&client
->dev
);
527 pm_runtime_put_noidle(&client
->dev
);
529 mutex_lock(&data
->mutex
);
530 mma9551_set_device_state(data
->client
, false);
531 mutex_unlock(&data
->mutex
);
537 static int mma9551_runtime_suspend(struct device
*dev
)
539 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
540 struct mma9551_data
*data
= iio_priv(indio_dev
);
543 mutex_lock(&data
->mutex
);
544 ret
= mma9551_set_device_state(data
->client
, false);
545 mutex_unlock(&data
->mutex
);
547 dev_err(&data
->client
->dev
, "powering off device failed\n");
554 static int mma9551_runtime_resume(struct device
*dev
)
556 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
557 struct mma9551_data
*data
= iio_priv(indio_dev
);
560 ret
= mma9551_set_device_state(data
->client
, true);
564 mma9551_sleep(MMA9551_DEFAULT_SAMPLE_RATE
);
570 #ifdef CONFIG_PM_SLEEP
571 static int mma9551_suspend(struct device
*dev
)
573 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
574 struct mma9551_data
*data
= iio_priv(indio_dev
);
577 mutex_lock(&data
->mutex
);
578 ret
= mma9551_set_device_state(data
->client
, false);
579 mutex_unlock(&data
->mutex
);
584 static int mma9551_resume(struct device
*dev
)
586 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
587 struct mma9551_data
*data
= iio_priv(indio_dev
);
590 mutex_lock(&data
->mutex
);
591 ret
= mma9551_set_device_state(data
->client
, true);
592 mutex_unlock(&data
->mutex
);
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
)
604 static const struct acpi_device_id mma9551_acpi_match
[] = {
609 MODULE_DEVICE_TABLE(acpi
, mma9551_acpi_match
);
611 static const struct i2c_device_id mma9551_id
[] = {
616 MODULE_DEVICE_TABLE(i2c
, mma9551_id
);
618 static struct i2c_driver mma9551_driver
= {
620 .name
= MMA9551_DRV_NAME
,
621 .acpi_match_table
= ACPI_PTR(mma9551_acpi_match
),
622 .pm
= &mma9551_pm_ops
,
624 .probe
= mma9551_probe
,
625 .remove
= mma9551_remove
,
626 .id_table
= mma9551_id
,
629 module_i2c_driver(mma9551_driver
);
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");