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
)
451 const struct i2c_device_id
*id
= i2c_client_get_device_id(client
);
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
->channels
= mma9551_channels
;
477 indio_dev
->num_channels
= ARRAY_SIZE(mma9551_channels
);
478 indio_dev
->name
= name
;
479 indio_dev
->modes
= INDIO_DIRECT_MODE
;
480 indio_dev
->info
= &mma9551_info
;
482 ret
= mma9551_gpio_probe(indio_dev
);
486 ret
= pm_runtime_set_active(&client
->dev
);
490 pm_runtime_enable(&client
->dev
);
491 pm_runtime_set_autosuspend_delay(&client
->dev
,
492 MMA9551_AUTO_SUSPEND_DELAY_MS
);
493 pm_runtime_use_autosuspend(&client
->dev
);
495 ret
= iio_device_register(indio_dev
);
497 dev_err(&client
->dev
, "unable to register iio device\n");
504 pm_runtime_dont_use_autosuspend(&client
->dev
);
505 pm_runtime_disable(&client
->dev
);
507 mma9551_set_device_state(client
, false);
512 static void mma9551_remove(struct i2c_client
*client
)
514 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
515 struct mma9551_data
*data
= iio_priv(indio_dev
);
517 iio_device_unregister(indio_dev
);
519 pm_runtime_disable(&client
->dev
);
520 pm_runtime_set_suspended(&client
->dev
);
522 mutex_lock(&data
->mutex
);
523 mma9551_set_device_state(data
->client
, false);
524 mutex_unlock(&data
->mutex
);
527 static int mma9551_runtime_suspend(struct device
*dev
)
529 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
530 struct mma9551_data
*data
= iio_priv(indio_dev
);
533 mutex_lock(&data
->mutex
);
534 ret
= mma9551_set_device_state(data
->client
, false);
535 mutex_unlock(&data
->mutex
);
537 dev_err(&data
->client
->dev
, "powering off device failed\n");
544 static int mma9551_runtime_resume(struct device
*dev
)
546 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
547 struct mma9551_data
*data
= iio_priv(indio_dev
);
550 ret
= mma9551_set_device_state(data
->client
, true);
554 mma9551_sleep(MMA9551_DEFAULT_SAMPLE_RATE
);
559 static int mma9551_suspend(struct device
*dev
)
561 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
562 struct mma9551_data
*data
= iio_priv(indio_dev
);
565 mutex_lock(&data
->mutex
);
566 ret
= mma9551_set_device_state(data
->client
, false);
567 mutex_unlock(&data
->mutex
);
572 static int mma9551_resume(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
, true);
580 mutex_unlock(&data
->mutex
);
585 static const struct dev_pm_ops mma9551_pm_ops
= {
586 SYSTEM_SLEEP_PM_OPS(mma9551_suspend
, mma9551_resume
)
587 RUNTIME_PM_OPS(mma9551_runtime_suspend
, mma9551_runtime_resume
, NULL
)
590 static const struct acpi_device_id mma9551_acpi_match
[] = {
595 MODULE_DEVICE_TABLE(acpi
, mma9551_acpi_match
);
597 static const struct i2c_device_id mma9551_id
[] = {
602 MODULE_DEVICE_TABLE(i2c
, mma9551_id
);
604 static struct i2c_driver mma9551_driver
= {
606 .name
= MMA9551_DRV_NAME
,
607 .acpi_match_table
= mma9551_acpi_match
,
608 .pm
= pm_ptr(&mma9551_pm_ops
),
610 .probe
= mma9551_probe
,
611 .remove
= mma9551_remove
,
612 .id_table
= mma9551_id
,
615 module_i2c_driver(mma9551_driver
);
617 MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>");
618 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
619 MODULE_LICENSE("GPL v2");
620 MODULE_DESCRIPTION("MMA9551L motion-sensing platform driver");
621 MODULE_IMPORT_NS(IIO_MMA9551
);