1 // SPDX-License-Identifier: GPL-2.0-only
3 * Freescale MMA9551L Intelligent Motion-Sensing Platform driver
4 * Copyright (c) 2014, Intel Corporation.
7 #include <linux/module.h>
9 #include <linux/interrupt.h>
10 #include <linux/slab.h>
11 #include <linux/acpi.h>
12 #include <linux/delay.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/iio/iio.h>
15 #include <linux/iio/sysfs.h>
16 #include <linux/iio/events.h>
17 #include <linux/pm_runtime.h>
18 #include "mma9551_core.h"
20 #define MMA9551_DRV_NAME "mma9551"
21 #define MMA9551_IRQ_NAME "mma9551_event"
22 #define MMA9551_GPIO_COUNT 4
24 /* Tilt application (inclination in IIO terms). */
25 #define MMA9551_TILT_XZ_ANG_REG 0x00
26 #define MMA9551_TILT_YZ_ANG_REG 0x01
27 #define MMA9551_TILT_XY_ANG_REG 0x02
28 #define MMA9551_TILT_ANGFLG BIT(7)
29 #define MMA9551_TILT_QUAD_REG 0x03
30 #define MMA9551_TILT_XY_QUAD_SHIFT 0
31 #define MMA9551_TILT_YZ_QUAD_SHIFT 2
32 #define MMA9551_TILT_XZ_QUAD_SHIFT 4
33 #define MMA9551_TILT_CFG_REG 0x01
34 #define MMA9551_TILT_ANG_THRESH_MASK GENMASK(3, 0)
36 #define MMA9551_DEFAULT_SAMPLE_RATE 122 /* Hz */
38 /* Tilt events are mapped to the first three GPIO pins. */
39 enum mma9551_tilt_axis
{
46 struct i2c_client
*client
;
49 int irqs
[MMA9551_GPIO_COUNT
];
52 static int mma9551_read_incli_chan(struct i2c_client
*client
,
53 const struct iio_chan_spec
*chan
,
56 u8 quad_shift
, angle
, quadrant
;
60 switch (chan
->channel2
) {
62 reg_addr
= MMA9551_TILT_YZ_ANG_REG
;
63 quad_shift
= MMA9551_TILT_YZ_QUAD_SHIFT
;
66 reg_addr
= MMA9551_TILT_XZ_ANG_REG
;
67 quad_shift
= MMA9551_TILT_XZ_QUAD_SHIFT
;
70 reg_addr
= MMA9551_TILT_XY_ANG_REG
;
71 quad_shift
= MMA9551_TILT_XY_QUAD_SHIFT
;
77 ret
= mma9551_set_power_state(client
, true);
81 ret
= mma9551_read_status_byte(client
, MMA9551_APPID_TILT
,
86 ret
= mma9551_read_status_byte(client
, MMA9551_APPID_TILT
,
87 MMA9551_TILT_QUAD_REG
, &quadrant
);
91 angle
&= ~MMA9551_TILT_ANGFLG
;
92 quadrant
= (quadrant
>> quad_shift
) & 0x03;
94 if (quadrant
== 1 || quadrant
== 3)
95 *val
= 90 * (quadrant
+ 1) - angle
;
97 *val
= angle
+ 90 * quadrant
;
102 mma9551_set_power_state(client
, false);
106 static int mma9551_read_raw(struct iio_dev
*indio_dev
,
107 struct iio_chan_spec
const *chan
,
108 int *val
, int *val2
, long mask
)
110 struct mma9551_data
*data
= iio_priv(indio_dev
);
114 case IIO_CHAN_INFO_PROCESSED
:
115 switch (chan
->type
) {
117 mutex_lock(&data
->mutex
);
118 ret
= mma9551_read_incli_chan(data
->client
, chan
, val
);
119 mutex_unlock(&data
->mutex
);
124 case IIO_CHAN_INFO_RAW
:
125 switch (chan
->type
) {
127 mutex_lock(&data
->mutex
);
128 ret
= mma9551_read_accel_chan(data
->client
,
130 mutex_unlock(&data
->mutex
);
135 case IIO_CHAN_INFO_SCALE
:
136 switch (chan
->type
) {
138 return mma9551_read_accel_scale(val
, val2
);
147 static int mma9551_read_event_config(struct iio_dev
*indio_dev
,
148 const struct iio_chan_spec
*chan
,
149 enum iio_event_type type
,
150 enum iio_event_direction dir
)
152 struct mma9551_data
*data
= iio_priv(indio_dev
);
154 switch (chan
->type
) {
156 /* IIO counts axes from 1, because IIO_NO_MOD is 0. */
157 return data
->event_enabled
[chan
->channel2
- 1];
163 static int mma9551_config_incli_event(struct iio_dev
*indio_dev
,
164 enum iio_modifier axis
,
167 struct mma9551_data
*data
= iio_priv(indio_dev
);
168 enum mma9551_tilt_axis mma_axis
;
171 /* IIO counts axes from 1, because IIO_NO_MOD is 0. */
174 if (data
->event_enabled
[mma_axis
] == state
)
178 ret
= mma9551_gpio_config(data
->client
,
179 (enum mma9551_gpio_pin
)mma_axis
,
180 MMA9551_APPID_NONE
, 0, 0);
184 ret
= mma9551_set_power_state(data
->client
, false);
190 /* Bit 7 of each angle register holds the angle flag. */
193 bitnum
= 7 + 8 * MMA9551_TILT_YZ_ANG_REG
;
196 bitnum
= 7 + 8 * MMA9551_TILT_XZ_ANG_REG
;
199 bitnum
= 7 + 8 * MMA9551_TILT_XY_ANG_REG
;
206 ret
= mma9551_set_power_state(data
->client
, true);
210 ret
= mma9551_gpio_config(data
->client
,
211 (enum mma9551_gpio_pin
)mma_axis
,
212 MMA9551_APPID_TILT
, bitnum
, 0);
214 mma9551_set_power_state(data
->client
, false);
219 data
->event_enabled
[mma_axis
] = state
;
224 static int mma9551_write_event_config(struct iio_dev
*indio_dev
,
225 const struct iio_chan_spec
*chan
,
226 enum iio_event_type type
,
227 enum iio_event_direction dir
,
230 struct mma9551_data
*data
= iio_priv(indio_dev
);
233 switch (chan
->type
) {
235 mutex_lock(&data
->mutex
);
236 ret
= mma9551_config_incli_event(indio_dev
,
237 chan
->channel2
, state
);
238 mutex_unlock(&data
->mutex
);
245 static int mma9551_write_event_value(struct iio_dev
*indio_dev
,
246 const struct iio_chan_spec
*chan
,
247 enum iio_event_type type
,
248 enum iio_event_direction dir
,
249 enum iio_event_info info
,
252 struct mma9551_data
*data
= iio_priv(indio_dev
);
255 switch (chan
->type
) {
257 if (val2
!= 0 || val
< 1 || val
> 10)
259 mutex_lock(&data
->mutex
);
260 ret
= mma9551_update_config_bits(data
->client
,
262 MMA9551_TILT_CFG_REG
,
263 MMA9551_TILT_ANG_THRESH_MASK
,
265 mutex_unlock(&data
->mutex
);
272 static int mma9551_read_event_value(struct iio_dev
*indio_dev
,
273 const struct iio_chan_spec
*chan
,
274 enum iio_event_type type
,
275 enum iio_event_direction dir
,
276 enum iio_event_info info
,
279 struct mma9551_data
*data
= iio_priv(indio_dev
);
283 switch (chan
->type
) {
285 mutex_lock(&data
->mutex
);
286 ret
= mma9551_read_config_byte(data
->client
,
288 MMA9551_TILT_CFG_REG
, &tmp
);
289 mutex_unlock(&data
->mutex
);
292 *val
= tmp
& MMA9551_TILT_ANG_THRESH_MASK
;
300 static const struct iio_event_spec mma9551_incli_event
= {
301 .type
= IIO_EV_TYPE_ROC
,
302 .dir
= IIO_EV_DIR_RISING
,
303 .mask_separate
= BIT(IIO_EV_INFO_ENABLE
),
304 .mask_shared_by_type
= BIT(IIO_EV_INFO_VALUE
),
307 #define MMA9551_INCLI_CHANNEL(axis) { \
311 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
312 .event_spec = &mma9551_incli_event, \
313 .num_event_specs = 1, \
316 static const struct iio_chan_spec mma9551_channels
[] = {
317 MMA9551_ACCEL_CHANNEL(IIO_MOD_X
),
318 MMA9551_ACCEL_CHANNEL(IIO_MOD_Y
),
319 MMA9551_ACCEL_CHANNEL(IIO_MOD_Z
),
321 MMA9551_INCLI_CHANNEL(IIO_MOD_X
),
322 MMA9551_INCLI_CHANNEL(IIO_MOD_Y
),
323 MMA9551_INCLI_CHANNEL(IIO_MOD_Z
),
326 static const struct iio_info mma9551_info
= {
327 .read_raw
= mma9551_read_raw
,
328 .read_event_config
= mma9551_read_event_config
,
329 .write_event_config
= mma9551_write_event_config
,
330 .read_event_value
= mma9551_read_event_value
,
331 .write_event_value
= mma9551_write_event_value
,
334 static irqreturn_t
mma9551_event_handler(int irq
, void *private)
336 struct iio_dev
*indio_dev
= private;
337 struct mma9551_data
*data
= iio_priv(indio_dev
);
338 int i
, ret
, mma_axis
= -1;
342 mutex_lock(&data
->mutex
);
344 for (i
= 0; i
< 3; i
++)
345 if (irq
== data
->irqs
[i
]) {
350 if (mma_axis
== -1) {
351 /* IRQ was triggered on 4th line, which we don't use. */
352 dev_warn(&data
->client
->dev
,
353 "irq triggered on unused line %d\n", data
->irqs
[3]);
359 reg
= MMA9551_TILT_YZ_ANG_REG
;
362 reg
= MMA9551_TILT_XZ_ANG_REG
;
365 reg
= MMA9551_TILT_XY_ANG_REG
;
370 * Read the angle even though we don't use it, otherwise we
371 * won't get any further interrupts.
373 ret
= mma9551_read_status_byte(data
->client
, MMA9551_APPID_TILT
,
376 dev_err(&data
->client
->dev
,
377 "error %d reading tilt register in IRQ\n", ret
);
381 iio_push_event(indio_dev
,
382 IIO_MOD_EVENT_CODE(IIO_INCLI
, 0, (mma_axis
+ 1),
383 IIO_EV_TYPE_ROC
, IIO_EV_DIR_RISING
),
384 iio_get_time_ns(indio_dev
));
387 mutex_unlock(&data
->mutex
);
392 static int mma9551_init(struct mma9551_data
*data
)
396 ret
= mma9551_read_version(data
->client
);
400 return mma9551_set_device_state(data
->client
, true);
403 static int mma9551_gpio_probe(struct iio_dev
*indio_dev
)
405 struct gpio_desc
*gpio
;
407 struct mma9551_data
*data
= iio_priv(indio_dev
);
408 struct device
*dev
= &data
->client
->dev
;
410 for (i
= 0; i
< MMA9551_GPIO_COUNT
; i
++) {
411 gpio
= devm_gpiod_get_index(dev
, NULL
, i
, GPIOD_IN
);
413 dev_err(dev
, "acpi gpio get index failed\n");
414 return PTR_ERR(gpio
);
417 ret
= gpiod_to_irq(gpio
);
422 ret
= devm_request_threaded_irq(dev
, data
->irqs
[i
],
423 NULL
, mma9551_event_handler
,
424 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
425 MMA9551_IRQ_NAME
, indio_dev
);
427 dev_err(dev
, "request irq %d failed\n", data
->irqs
[i
]);
431 dev_dbg(dev
, "gpio resource, no:%d irq:%d\n",
432 desc_to_gpio(gpio
), data
->irqs
[i
]);
438 static const char *mma9551_match_acpi_device(struct device
*dev
)
440 const struct acpi_device_id
*id
;
442 id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
446 return dev_name(dev
);
449 static int mma9551_probe(struct i2c_client
*client
,
450 const struct i2c_device_id
*id
)
452 struct mma9551_data
*data
;
453 struct iio_dev
*indio_dev
;
454 const char *name
= NULL
;
457 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
461 data
= iio_priv(indio_dev
);
462 i2c_set_clientdata(client
, indio_dev
);
463 data
->client
= client
;
467 else if (ACPI_HANDLE(&client
->dev
))
468 name
= mma9551_match_acpi_device(&client
->dev
);
470 ret
= mma9551_init(data
);
474 mutex_init(&data
->mutex
);
476 indio_dev
->dev
.parent
= &client
->dev
;
477 indio_dev
->channels
= mma9551_channels
;
478 indio_dev
->num_channels
= ARRAY_SIZE(mma9551_channels
);
479 indio_dev
->name
= name
;
480 indio_dev
->modes
= INDIO_DIRECT_MODE
;
481 indio_dev
->info
= &mma9551_info
;
483 ret
= mma9551_gpio_probe(indio_dev
);
487 ret
= pm_runtime_set_active(&client
->dev
);
491 pm_runtime_enable(&client
->dev
);
492 pm_runtime_set_autosuspend_delay(&client
->dev
,
493 MMA9551_AUTO_SUSPEND_DELAY_MS
);
494 pm_runtime_use_autosuspend(&client
->dev
);
496 ret
= iio_device_register(indio_dev
);
498 dev_err(&client
->dev
, "unable to register iio device\n");
505 mma9551_set_device_state(client
, false);
510 static int mma9551_remove(struct i2c_client
*client
)
512 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
513 struct mma9551_data
*data
= iio_priv(indio_dev
);
515 iio_device_unregister(indio_dev
);
517 pm_runtime_disable(&client
->dev
);
518 pm_runtime_set_suspended(&client
->dev
);
519 pm_runtime_put_noidle(&client
->dev
);
521 mutex_lock(&data
->mutex
);
522 mma9551_set_device_state(data
->client
, false);
523 mutex_unlock(&data
->mutex
);
529 static int mma9551_runtime_suspend(struct device
*dev
)
531 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
532 struct mma9551_data
*data
= iio_priv(indio_dev
);
535 mutex_lock(&data
->mutex
);
536 ret
= mma9551_set_device_state(data
->client
, false);
537 mutex_unlock(&data
->mutex
);
539 dev_err(&data
->client
->dev
, "powering off device failed\n");
546 static int mma9551_runtime_resume(struct device
*dev
)
548 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
549 struct mma9551_data
*data
= iio_priv(indio_dev
);
552 ret
= mma9551_set_device_state(data
->client
, true);
556 mma9551_sleep(MMA9551_DEFAULT_SAMPLE_RATE
);
562 #ifdef CONFIG_PM_SLEEP
563 static int mma9551_suspend(struct device
*dev
)
565 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
566 struct mma9551_data
*data
= iio_priv(indio_dev
);
569 mutex_lock(&data
->mutex
);
570 ret
= mma9551_set_device_state(data
->client
, false);
571 mutex_unlock(&data
->mutex
);
576 static int mma9551_resume(struct device
*dev
)
578 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
579 struct mma9551_data
*data
= iio_priv(indio_dev
);
582 mutex_lock(&data
->mutex
);
583 ret
= mma9551_set_device_state(data
->client
, true);
584 mutex_unlock(&data
->mutex
);
590 static const struct dev_pm_ops mma9551_pm_ops
= {
591 SET_SYSTEM_SLEEP_PM_OPS(mma9551_suspend
, mma9551_resume
)
592 SET_RUNTIME_PM_OPS(mma9551_runtime_suspend
,
593 mma9551_runtime_resume
, NULL
)
596 static const struct acpi_device_id mma9551_acpi_match
[] = {
601 MODULE_DEVICE_TABLE(acpi
, mma9551_acpi_match
);
603 static const struct i2c_device_id mma9551_id
[] = {
608 MODULE_DEVICE_TABLE(i2c
, mma9551_id
);
610 static struct i2c_driver mma9551_driver
= {
612 .name
= MMA9551_DRV_NAME
,
613 .acpi_match_table
= ACPI_PTR(mma9551_acpi_match
),
614 .pm
= &mma9551_pm_ops
,
616 .probe
= mma9551_probe
,
617 .remove
= mma9551_remove
,
618 .id_table
= mma9551_id
,
621 module_i2c_driver(mma9551_driver
);
623 MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>");
624 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
625 MODULE_LICENSE("GPL v2");
626 MODULE_DESCRIPTION("MMA9551L motion-sensing platform driver");