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>
27 #define MMA9551_DRV_NAME "mma9551"
28 #define MMA9551_IRQ_NAME "mma9551_event"
29 #define MMA9551_GPIO_NAME "mma9551_int"
30 #define MMA9551_GPIO_COUNT 4
32 /* Applications IDs */
33 #define MMA9551_APPID_VERSION 0x00
34 #define MMA9551_APPID_GPIO 0x03
35 #define MMA9551_APPID_AFE 0x06
36 #define MMA9551_APPID_TILT 0x0B
37 #define MMA9551_APPID_SLEEP_WAKE 0x12
38 #define MMA9551_APPID_RESET 0x17
39 #define MMA9551_APPID_NONE 0xff
41 /* Command masks for mailbox write command */
42 #define MMA9551_CMD_READ_VERSION_INFO 0x00
43 #define MMA9551_CMD_READ_CONFIG 0x10
44 #define MMA9551_CMD_WRITE_CONFIG 0x20
45 #define MMA9551_CMD_READ_STATUS 0x30
47 enum mma9551_gpio_pin {
52 mma9551_gpio_max = mma9551_gpio9,
55 /* Mailbox read command */
56 #define MMA9551_RESPONSE_COCO BIT(7)
58 /* Error-Status codes returned in mailbox read command */
59 #define MMA9551_MCI_ERROR_NONE 0x00
60 #define MMA9551_MCI_ERROR_PARAM 0x04
61 #define MMA9551_MCI_INVALID_COUNT 0x19
62 #define MMA9551_MCI_ERROR_COMMAND 0x1C
63 #define MMA9551_MCI_ERROR_INVALID_LENGTH 0x21
64 #define MMA9551_MCI_ERROR_FIFO_BUSY 0x22
65 #define MMA9551_MCI_ERROR_FIFO_ALLOCATED 0x23
66 #define MMA9551_MCI_ERROR_FIFO_OVERSIZE 0x24
68 /* GPIO Application */
69 #define MMA9551_GPIO_POL_MSB 0x08
70 #define MMA9551_GPIO_POL_LSB 0x09
72 /* Sleep/Wake application */
73 #define MMA9551_SLEEP_CFG 0x06
74 #define MMA9551_SLEEP_CFG_SNCEN BIT(0)
75 #define MMA9551_SLEEP_CFG_FLEEN BIT(1)
76 #define MMA9551_SLEEP_CFG_SCHEN BIT(2)
79 #define MMA9551_AFE_X_ACCEL_REG 0x00
80 #define MMA9551_AFE_Y_ACCEL_REG 0x02
81 #define MMA9551_AFE_Z_ACCEL_REG 0x04
83 /* Tilt application (inclination in IIO terms). */
84 #define MMA9551_TILT_XZ_ANG_REG 0x00
85 #define MMA9551_TILT_YZ_ANG_REG 0x01
86 #define MMA9551_TILT_XY_ANG_REG 0x02
87 #define MMA9551_TILT_ANGFLG BIT(7)
88 #define MMA9551_TILT_QUAD_REG 0x03
89 #define MMA9551_TILT_XY_QUAD_SHIFT 0
90 #define MMA9551_TILT_YZ_QUAD_SHIFT 2
91 #define MMA9551_TILT_XZ_QUAD_SHIFT 4
92 #define MMA9551_TILT_CFG_REG 0x01
93 #define MMA9551_TILT_ANG_THRESH_MASK GENMASK(3, 0)
95 /* Tilt events are mapped to the first three GPIO pins. */
96 enum mma9551_tilt_axis {
103 * A response is composed of:
104 * - control registers: MB0-3
105 * - data registers: MB4-31
107 * A request is composed of:
108 * - mbox to write to (always 0)
109 * - control registers: MB1-4
110 * - data registers: MB5-31
112 #define MMA9551_MAILBOX_CTRL_REGS 4
113 #define MMA9551_MAX_MAILBOX_DATA_REGS 28
114 #define MMA9551_MAILBOX_REGS 32
116 #define MMA9551_I2C_READ_RETRIES 5
117 #define MMA9551_I2C_READ_DELAY 50 /* us */
119 #define MMA9551_DEFAULT_SAMPLE_RATE 122 /* Hz */
120 #define MMA9551_AUTO_SUSPEND_DELAY_MS 2000
122 struct mma9551_mbox_request {
123 u8 start_mbox; /* Always 0. */
126 * See Section 5.3.1 of the MMA955xL Software Reference Manual.
128 * Bit 7: reserved, always 0
130 * Bits 3-0: upper bits of register offset
135 u8 buf[MMA9551_MAX_MAILBOX_DATA_REGS - 1];
138 struct mma9551_mbox_response {
141 * See Section 5.3.3 of the MMA955xL Software Reference Manual.
144 * Bits 6-0: Error code.
149 u8 buf[MMA9551_MAX_MAILBOX_DATA_REGS];
152 struct mma9551_version_info {
160 struct mma9551_data {
161 struct i2c_client *client;
163 int event_enabled[3];
164 int irqs[MMA9551_GPIO_COUNT];
167 static int mma9551_transfer(struct i2c_client *client,
168 u8 app_id, u8 command, u16 offset,
169 u8 *inbytes, int num_inbytes,
170 u8 *outbytes, int num_outbytes)
172 struct mma9551_mbox_request req;
173 struct mma9551_mbox_response rsp;
174 struct i2c_msg in, out;
175 u8 req_len, err_code;
178 if (offset >= 1 << 12) {
179 dev_err(&client->dev, "register offset too large\n");
183 req_len = 1 + MMA9551_MAILBOX_CTRL_REGS + num_inbytes;
186 req.cmd_off = command | (offset >> 8);
187 req.lower_off = offset;
189 if (command == MMA9551_CMD_WRITE_CONFIG)
190 req.nbytes = num_inbytes;
192 req.nbytes = num_outbytes;
194 memcpy(req.buf, inbytes, num_inbytes);
196 out.addr = client->addr;
199 out.buf = (u8 *)&req;
201 ret = i2c_transfer(client->adapter, &out, 1);
203 dev_err(&client->dev, "i2c write failed\n");
207 retries = MMA9551_I2C_READ_RETRIES;
209 udelay(MMA9551_I2C_READ_DELAY);
211 in.addr = client->addr;
213 in.len = sizeof(rsp);
216 ret = i2c_transfer(client->adapter, &in, 1);
218 dev_err(&client->dev, "i2c read failed\n");
222 if (rsp.coco_err & MMA9551_RESPONSE_COCO)
224 } while (--retries > 0);
227 dev_err(&client->dev,
228 "timed out while waiting for command response\n");
232 if (rsp.app_id != app_id) {
233 dev_err(&client->dev,
234 "app_id mismatch in response got %02x expected %02x\n",
239 err_code = rsp.coco_err & ~MMA9551_RESPONSE_COCO;
240 if (err_code != MMA9551_MCI_ERROR_NONE) {
241 dev_err(&client->dev, "read returned error %x\n", err_code);
245 if (rsp.nbytes != rsp.req_bytes) {
246 dev_err(&client->dev,
247 "output length mismatch got %d expected %d\n",
248 rsp.nbytes, rsp.req_bytes);
253 memcpy(outbytes, rsp.buf, num_outbytes);
258 static int mma9551_read_config_byte(struct i2c_client *client, u8 app_id,
261 return mma9551_transfer(client, app_id, MMA9551_CMD_READ_CONFIG,
262 reg, NULL, 0, val, 1);
265 static int mma9551_write_config_byte(struct i2c_client *client, u8 app_id,
268 return mma9551_transfer(client, app_id, MMA9551_CMD_WRITE_CONFIG, reg,
272 static int mma9551_read_status_byte(struct i2c_client *client, u8 app_id,
275 return mma9551_transfer(client, app_id, MMA9551_CMD_READ_STATUS,
276 reg, NULL, 0, val, 1);
279 static int mma9551_read_status_word(struct i2c_client *client, u8 app_id,
285 ret = mma9551_transfer(client, app_id, MMA9551_CMD_READ_STATUS,
286 reg, NULL, 0, (u8 *)&v, 2);
287 *val = be16_to_cpu(v);
292 static int mma9551_update_config_bits(struct i2c_client *client, u8 app_id,
293 u16 reg, u8 mask, u8 val)
298 ret = mma9551_read_config_byte(client, app_id, reg, &orig);
308 return mma9551_write_config_byte(client, app_id, reg, tmp);
312 * The polarity parameter is described in section 6.2.2, page 66, of the
313 * Software Reference Manual. Basically, polarity=0 means the interrupt
314 * line has the same value as the selected bit, while polarity=1 means
315 * the line is inverted.
317 static int mma9551_gpio_config(struct i2c_client *client,
318 enum mma9551_gpio_pin pin,
319 u8 app_id, u8 bitnum, int polarity)
321 u8 reg, pol_mask, pol_val;
324 if (pin > mma9551_gpio_max) {
325 dev_err(&client->dev, "bad GPIO pin\n");
330 * Pin 6 is configured by regs 0x00 and 0x01, pin 7 by 0x02 and
335 ret = mma9551_write_config_byte(client, MMA9551_APPID_GPIO,
338 dev_err(&client->dev, "error setting GPIO app_id\n");
342 ret = mma9551_write_config_byte(client, MMA9551_APPID_GPIO,
345 dev_err(&client->dev, "error setting GPIO bit number\n");
351 reg = MMA9551_GPIO_POL_LSB;
355 reg = MMA9551_GPIO_POL_LSB;
359 reg = MMA9551_GPIO_POL_MSB;
363 reg = MMA9551_GPIO_POL_MSB;
367 pol_val = polarity ? pol_mask : 0;
369 ret = mma9551_update_config_bits(client, MMA9551_APPID_GPIO, reg,
372 dev_err(&client->dev, "error setting GPIO polarity\n");
377 static int mma9551_read_version(struct i2c_client *client)
379 struct mma9551_version_info info;
382 ret = mma9551_transfer(client, MMA9551_APPID_VERSION, 0x00, 0x00,
383 NULL, 0, (u8 *)&info, sizeof(info));
387 dev_info(&client->dev, "Device ID 0x%x, firmware version %02x.%02x\n",
388 be32_to_cpu(info.device_id), info.fw_version[0],
395 * Power on chip and enable doze mode.
396 * Use 'false' as the second parameter to cause the device to enter
399 static int mma9551_set_device_state(struct i2c_client *client, bool enable)
401 return mma9551_update_config_bits(client, MMA9551_APPID_SLEEP_WAKE,
403 MMA9551_SLEEP_CFG_SNCEN |
404 MMA9551_SLEEP_CFG_FLEEN |
405 MMA9551_SLEEP_CFG_SCHEN,
406 enable ? MMA9551_SLEEP_CFG_SCHEN |
407 MMA9551_SLEEP_CFG_FLEEN :
408 MMA9551_SLEEP_CFG_SNCEN);
411 static int mma9551_set_power_state(struct i2c_client *client, bool on)
417 ret = pm_runtime_get_sync(&client->dev);
419 pm_runtime_mark_last_busy(&client->dev);
420 ret = pm_runtime_put_autosuspend(&client->dev);
424 dev_err(&client->dev,
425 "failed to change power state to %d\n", on);
427 pm_runtime_put_noidle(&client->dev);
436 static void mma9551_sleep(int freq)
438 int sleep_val = 1000 / freq;
441 usleep_range(sleep_val * 1000, 20000);
443 msleep_interruptible(sleep_val);
446 static int mma9551_read_incli_chan(struct i2c_client *client,
447 const struct iio_chan_spec *chan,
450 u8 quad_shift, angle, quadrant;
454 switch (chan->channel2) {
456 reg_addr = MMA9551_TILT_YZ_ANG_REG;
457 quad_shift = MMA9551_TILT_YZ_QUAD_SHIFT;
460 reg_addr = MMA9551_TILT_XZ_ANG_REG;
461 quad_shift = MMA9551_TILT_XZ_QUAD_SHIFT;
464 reg_addr = MMA9551_TILT_XY_ANG_REG;
465 quad_shift = MMA9551_TILT_XY_QUAD_SHIFT;
471 ret = mma9551_set_power_state(client, true);
475 ret = mma9551_read_status_byte(client, MMA9551_APPID_TILT,
480 ret = mma9551_read_status_byte(client, MMA9551_APPID_TILT,
481 MMA9551_TILT_QUAD_REG, &quadrant);
485 angle &= ~MMA9551_TILT_ANGFLG;
486 quadrant = (quadrant >> quad_shift) & 0x03;
488 if (quadrant == 1 || quadrant == 3)
489 *val = 90 * (quadrant + 1) - angle;
491 *val = angle + 90 * quadrant;
496 mma9551_set_power_state(client, false);
500 static int mma9551_read_accel_chan(struct i2c_client *client,
501 const struct iio_chan_spec *chan,
508 switch (chan->channel2) {
510 reg_addr = MMA9551_AFE_X_ACCEL_REG;
513 reg_addr = MMA9551_AFE_Y_ACCEL_REG;
516 reg_addr = MMA9551_AFE_Z_ACCEL_REG;
522 ret = mma9551_set_power_state(client, true);
526 ret = mma9551_read_status_word(client, MMA9551_APPID_AFE,
527 reg_addr, &raw_accel);
536 mma9551_set_power_state(client, false);
540 static int mma9551_read_raw(struct iio_dev *indio_dev,
541 struct iio_chan_spec const *chan,
542 int *val, int *val2, long mask)
544 struct mma9551_data *data = iio_priv(indio_dev);
548 case IIO_CHAN_INFO_PROCESSED:
549 switch (chan->type) {
551 mutex_lock(&data->mutex);
552 ret = mma9551_read_incli_chan(data->client, chan, val);
553 mutex_unlock(&data->mutex);
558 case IIO_CHAN_INFO_RAW:
559 switch (chan->type) {
561 mutex_lock(&data->mutex);
562 ret = mma9551_read_accel_chan(data->client,
564 mutex_unlock(&data->mutex);
569 case IIO_CHAN_INFO_SCALE:
570 switch (chan->type) {
574 return IIO_VAL_INT_PLUS_MICRO;
583 static int mma9551_read_event_config(struct iio_dev *indio_dev,
584 const struct iio_chan_spec *chan,
585 enum iio_event_type type,
586 enum iio_event_direction dir)
588 struct mma9551_data *data = iio_priv(indio_dev);
590 switch (chan->type) {
592 /* IIO counts axes from 1, because IIO_NO_MOD is 0. */
593 return data->event_enabled[chan->channel2 - 1];
599 static int mma9551_config_incli_event(struct iio_dev *indio_dev,
600 enum iio_modifier axis,
603 struct mma9551_data *data = iio_priv(indio_dev);
604 enum mma9551_tilt_axis mma_axis;
607 /* IIO counts axes from 1, because IIO_NO_MOD is 0. */
610 if (data->event_enabled[mma_axis] == state)
614 ret = mma9551_gpio_config(data->client,
615 (enum mma9551_gpio_pin)mma_axis,
616 MMA9551_APPID_NONE, 0, 0);
620 ret = mma9551_set_power_state(data->client, false);
626 /* Bit 7 of each angle register holds the angle flag. */
629 bitnum = 7 + 8 * MMA9551_TILT_YZ_ANG_REG;
632 bitnum = 7 + 8 * MMA9551_TILT_XZ_ANG_REG;
635 bitnum = 7 + 8 * MMA9551_TILT_XY_ANG_REG;
642 ret = mma9551_set_power_state(data->client, true);
646 ret = mma9551_gpio_config(data->client,
647 (enum mma9551_gpio_pin)mma_axis,
648 MMA9551_APPID_TILT, bitnum, 0);
650 mma9551_set_power_state(data->client, false);
655 data->event_enabled[mma_axis] = state;
660 static int mma9551_write_event_config(struct iio_dev *indio_dev,
661 const struct iio_chan_spec *chan,
662 enum iio_event_type type,
663 enum iio_event_direction dir,
666 struct mma9551_data *data = iio_priv(indio_dev);
669 switch (chan->type) {
671 mutex_lock(&data->mutex);
672 ret = mma9551_config_incli_event(indio_dev,
673 chan->channel2, state);
674 mutex_unlock(&data->mutex);
681 static int mma9551_write_event_value(struct iio_dev *indio_dev,
682 const struct iio_chan_spec *chan,
683 enum iio_event_type type,
684 enum iio_event_direction dir,
685 enum iio_event_info info,
688 struct mma9551_data *data = iio_priv(indio_dev);
691 switch (chan->type) {
693 if (val2 != 0 || val < 1 || val > 10)
695 mutex_lock(&data->mutex);
696 ret = mma9551_update_config_bits(data->client,
698 MMA9551_TILT_CFG_REG,
699 MMA9551_TILT_ANG_THRESH_MASK,
701 mutex_unlock(&data->mutex);
708 static int mma9551_read_event_value(struct iio_dev *indio_dev,
709 const struct iio_chan_spec *chan,
710 enum iio_event_type type,
711 enum iio_event_direction dir,
712 enum iio_event_info info,
715 struct mma9551_data *data = iio_priv(indio_dev);
719 switch (chan->type) {
721 mutex_lock(&data->mutex);
722 ret = mma9551_read_config_byte(data->client,
724 MMA9551_TILT_CFG_REG, &tmp);
725 mutex_unlock(&data->mutex);
728 *val = tmp & MMA9551_TILT_ANG_THRESH_MASK;
736 static const struct iio_event_spec mma9551_incli_event = {
737 .type = IIO_EV_TYPE_ROC,
738 .dir = IIO_EV_DIR_RISING,
739 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
740 .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE),
743 #define MMA9551_ACCEL_CHANNEL(axis) { \
747 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
748 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
751 #define MMA9551_INCLI_CHANNEL(axis) { \
755 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
756 .event_spec = &mma9551_incli_event, \
757 .num_event_specs = 1, \
760 static const struct iio_chan_spec mma9551_channels[] = {
761 MMA9551_ACCEL_CHANNEL(IIO_MOD_X),
762 MMA9551_ACCEL_CHANNEL(IIO_MOD_Y),
763 MMA9551_ACCEL_CHANNEL(IIO_MOD_Z),
765 MMA9551_INCLI_CHANNEL(IIO_MOD_X),
766 MMA9551_INCLI_CHANNEL(IIO_MOD_Y),
767 MMA9551_INCLI_CHANNEL(IIO_MOD_Z),
770 static const struct iio_info mma9551_info = {
771 .driver_module = THIS_MODULE,
772 .read_raw = mma9551_read_raw,
773 .read_event_config = mma9551_read_event_config,
774 .write_event_config = mma9551_write_event_config,
775 .read_event_value = mma9551_read_event_value,
776 .write_event_value = mma9551_write_event_value,
779 static irqreturn_t mma9551_event_handler(int irq, void *private)
781 struct iio_dev *indio_dev = private;
782 struct mma9551_data *data = iio_priv(indio_dev);
783 int i, ret, mma_axis = -1;
787 mutex_lock(&data->mutex);
789 for (i = 0; i < 3; i++)
790 if (irq == data->irqs[i]) {
795 if (mma_axis == -1) {
796 /* IRQ was triggered on 4th line, which we don't use. */
797 dev_warn(&data->client->dev,
798 "irq triggered on unused line %d\n", data->irqs[3]);
804 reg = MMA9551_TILT_YZ_ANG_REG;
807 reg = MMA9551_TILT_XZ_ANG_REG;
810 reg = MMA9551_TILT_XY_ANG_REG;
815 * Read the angle even though we don't use it, otherwise we
816 * won't get any further interrupts.
818 ret = mma9551_read_status_byte(data->client, MMA9551_APPID_TILT,
821 dev_err(&data->client->dev,
822 "error %d reading tilt register in IRQ\n", ret);
826 iio_push_event(indio_dev,
827 IIO_MOD_EVENT_CODE(IIO_INCLI, 0, (mma_axis + 1),
828 IIO_EV_TYPE_ROC, IIO_EV_DIR_RISING),
832 mutex_unlock(&data->mutex);
837 static int mma9551_init(struct mma9551_data *data)
841 ret = mma9551_read_version(data->client);
845 return mma9551_set_device_state(data->client, true);
848 static int mma9551_gpio_probe(struct iio_dev *indio_dev)
850 struct gpio_desc *gpio;
852 struct mma9551_data *data = iio_priv(indio_dev);
853 struct device *dev = &data->client->dev;
855 for (i = 0; i < MMA9551_GPIO_COUNT; i++) {
856 gpio = devm_gpiod_get_index(dev, MMA9551_GPIO_NAME, i);
858 dev_err(dev, "acpi gpio get index failed\n");
859 return PTR_ERR(gpio);
862 ret = gpiod_direction_input(gpio);
866 data->irqs[i] = gpiod_to_irq(gpio);
867 ret = devm_request_threaded_irq(dev, data->irqs[i],
868 NULL, mma9551_event_handler,
869 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
870 MMA9551_IRQ_NAME, indio_dev);
872 dev_err(dev, "request irq %d failed\n", data->irqs[i]);
876 dev_dbg(dev, "gpio resource, no:%d irq:%d\n",
877 desc_to_gpio(gpio), data->irqs[i]);
883 static const char *mma9551_match_acpi_device(struct device *dev)
885 const struct acpi_device_id *id;
887 id = acpi_match_device(dev->driver->acpi_match_table, dev);
891 return dev_name(dev);
894 static int mma9551_probe(struct i2c_client *client,
895 const struct i2c_device_id *id)
897 struct mma9551_data *data;
898 struct iio_dev *indio_dev;
899 const char *name = NULL;
902 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
906 data = iio_priv(indio_dev);
907 i2c_set_clientdata(client, indio_dev);
908 data->client = client;
912 else if (ACPI_HANDLE(&client->dev))
913 name = mma9551_match_acpi_device(&client->dev);
915 ret = mma9551_init(data);
919 mutex_init(&data->mutex);
921 indio_dev->dev.parent = &client->dev;
922 indio_dev->channels = mma9551_channels;
923 indio_dev->num_channels = ARRAY_SIZE(mma9551_channels);
924 indio_dev->name = name;
925 indio_dev->modes = INDIO_DIRECT_MODE;
926 indio_dev->info = &mma9551_info;
928 ret = mma9551_gpio_probe(indio_dev);
932 ret = iio_device_register(indio_dev);
934 dev_err(&client->dev, "unable to register iio device\n");
938 ret = pm_runtime_set_active(&client->dev);
940 goto out_iio_unregister;
942 pm_runtime_enable(&client->dev);
943 pm_runtime_set_autosuspend_delay(&client->dev,
944 MMA9551_AUTO_SUSPEND_DELAY_MS);
945 pm_runtime_use_autosuspend(&client->dev);
950 iio_device_unregister(indio_dev);
952 mma9551_set_device_state(client, false);
957 static int mma9551_remove(struct i2c_client *client)
959 struct iio_dev *indio_dev = i2c_get_clientdata(client);
960 struct mma9551_data *data = iio_priv(indio_dev);
962 pm_runtime_disable(&client->dev);
963 pm_runtime_set_suspended(&client->dev);
964 pm_runtime_put_noidle(&client->dev);
966 iio_device_unregister(indio_dev);
967 mutex_lock(&data->mutex);
968 mma9551_set_device_state(data->client, false);
969 mutex_unlock(&data->mutex);
975 static int mma9551_runtime_suspend(struct device *dev)
977 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
978 struct mma9551_data *data = iio_priv(indio_dev);
981 mutex_lock(&data->mutex);
982 ret = mma9551_set_device_state(data->client, false);
983 mutex_unlock(&data->mutex);
985 dev_err(&data->client->dev, "powering off device failed\n");
992 static int mma9551_runtime_resume(struct device *dev)
994 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
995 struct mma9551_data *data = iio_priv(indio_dev);
998 ret = mma9551_set_device_state(data->client, true);
1002 mma9551_sleep(MMA9551_DEFAULT_SAMPLE_RATE);
1008 #ifdef CONFIG_PM_SLEEP
1009 static int mma9551_suspend(struct device *dev)
1011 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1012 struct mma9551_data *data = iio_priv(indio_dev);
1015 mutex_lock(&data->mutex);
1016 ret = mma9551_set_device_state(data->client, false);
1017 mutex_unlock(&data->mutex);
1022 static int mma9551_resume(struct device *dev)
1024 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1025 struct mma9551_data *data = iio_priv(indio_dev);
1028 mutex_lock(&data->mutex);
1029 ret = mma9551_set_device_state(data->client, true);
1030 mutex_unlock(&data->mutex);
1036 static const struct dev_pm_ops mma9551_pm_ops = {
1037 SET_SYSTEM_SLEEP_PM_OPS(mma9551_suspend, mma9551_resume)
1038 SET_RUNTIME_PM_OPS(mma9551_runtime_suspend,
1039 mma9551_runtime_resume, NULL)
1042 static const struct acpi_device_id mma9551_acpi_match[] = {
1047 MODULE_DEVICE_TABLE(acpi, mma9551_acpi_match);
1049 static const struct i2c_device_id mma9551_id[] = {
1054 MODULE_DEVICE_TABLE(i2c, mma9551_id);
1056 static struct i2c_driver mma9551_driver = {
1058 .name = MMA9551_DRV_NAME,
1059 .acpi_match_table = ACPI_PTR(mma9551_acpi_match),
1060 .pm = &mma9551_pm_ops,
1062 .probe = mma9551_probe,
1063 .remove = mma9551_remove,
1064 .id_table = mma9551_id,
1067 module_i2c_driver(mma9551_driver);
1069 MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>");
1070 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
1071 MODULE_LICENSE("GPL v2");
1072 MODULE_DESCRIPTION("MMA9551L motion-sensing platform driver");