Martin Kepplinger | 80e3f01 | 2017-11-18 10:10:11 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 2 | /* |
Martin Kepplinger | f26ab1a | 2016-06-03 14:51:52 +0200 | [diff] [blame] | 3 | * mma8452.c - Support for following Freescale / NXP 3-axis accelerometers: |
Martin Kepplinger | c5ea1b58 | 2015-09-01 13:45:09 +0200 | [diff] [blame] | 4 | * |
Martin Kepplinger | 16df666 | 2016-06-03 14:51:51 +0200 | [diff] [blame] | 5 | * device name digital output 7-bit I2C slave address (pin selectable) |
| 6 | * --------------------------------------------------------------------- |
| 7 | * MMA8451Q 14 bit 0x1c / 0x1d |
| 8 | * MMA8452Q 12 bit 0x1c / 0x1d |
| 9 | * MMA8453Q 10 bit 0x1c / 0x1d |
| 10 | * MMA8652FC 12 bit 0x1d |
| 11 | * MMA8653FC 10 bit 0x1d |
| 12 | * FXLS8471Q 14 bit 0x1e / 0x1d / 0x1c / 0x1f |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 13 | * |
Martin Kepplinger | 40836bc | 2016-06-03 14:51:50 +0200 | [diff] [blame] | 14 | * Copyright 2015 Martin Kepplinger <martink@posteo.de> |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 15 | * Copyright 2014 Peter Meerwald <pmeerw@pmeerw.net> |
| 16 | * |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 17 | * |
Martin Kepplinger | bce59b6 | 2016-03-14 12:26:29 +0100 | [diff] [blame] | 18 | * TODO: orientation events |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/i2c.h> |
| 23 | #include <linux/iio/iio.h> |
| 24 | #include <linux/iio/sysfs.h> |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 25 | #include <linux/iio/buffer.h> |
Martin Fuzzey | ae6d9ce | 2015-06-01 15:39:58 +0200 | [diff] [blame] | 26 | #include <linux/iio/trigger.h> |
| 27 | #include <linux/iio/trigger_consumer.h> |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 28 | #include <linux/iio/triggered_buffer.h> |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 29 | #include <linux/iio/events.h> |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 30 | #include <linux/delay.h> |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 31 | #include <linux/of_device.h> |
Martin Kepplinger | d2a3e09 | 2015-10-15 15:10:32 +0200 | [diff] [blame] | 32 | #include <linux/of_irq.h> |
Martin Kepplinger | 96c0cb2 | 2016-03-03 09:24:03 +0100 | [diff] [blame] | 33 | #include <linux/pm_runtime.h> |
Anson Huang | f6ff49b | 2019-01-08 09:14:06 +0000 | [diff] [blame] | 34 | #include <linux/regulator/consumer.h> |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 35 | |
Hartmut Knaack | 69abff8 | 2015-08-02 22:43:50 +0200 | [diff] [blame] | 36 | #define MMA8452_STATUS 0x00 |
| 37 | #define MMA8452_STATUS_DRDY (BIT(2) | BIT(1) | BIT(0)) |
Martin Kepplinger | c5ea1b58 | 2015-09-01 13:45:09 +0200 | [diff] [blame] | 38 | #define MMA8452_OUT_X 0x01 /* MSB first */ |
Hartmut Knaack | 69abff8 | 2015-08-02 22:43:50 +0200 | [diff] [blame] | 39 | #define MMA8452_OUT_Y 0x03 |
| 40 | #define MMA8452_OUT_Z 0x05 |
| 41 | #define MMA8452_INT_SRC 0x0c |
| 42 | #define MMA8452_WHO_AM_I 0x0d |
| 43 | #define MMA8452_DATA_CFG 0x0e |
| 44 | #define MMA8452_DATA_CFG_FS_MASK GENMASK(1, 0) |
| 45 | #define MMA8452_DATA_CFG_FS_2G 0 |
| 46 | #define MMA8452_DATA_CFG_FS_4G 1 |
| 47 | #define MMA8452_DATA_CFG_FS_8G 2 |
| 48 | #define MMA8452_DATA_CFG_HPF_MASK BIT(4) |
| 49 | #define MMA8452_HP_FILTER_CUTOFF 0x0f |
| 50 | #define MMA8452_HP_FILTER_CUTOFF_SEL_MASK GENMASK(1, 0) |
Martin Kepplinger | 60f562e | 2015-09-01 13:45:10 +0200 | [diff] [blame] | 51 | #define MMA8452_FF_MT_CFG 0x15 |
| 52 | #define MMA8452_FF_MT_CFG_OAE BIT(6) |
| 53 | #define MMA8452_FF_MT_CFG_ELE BIT(7) |
| 54 | #define MMA8452_FF_MT_SRC 0x16 |
| 55 | #define MMA8452_FF_MT_SRC_XHE BIT(1) |
| 56 | #define MMA8452_FF_MT_SRC_YHE BIT(3) |
| 57 | #define MMA8452_FF_MT_SRC_ZHE BIT(5) |
| 58 | #define MMA8452_FF_MT_THS 0x17 |
| 59 | #define MMA8452_FF_MT_THS_MASK 0x7f |
| 60 | #define MMA8452_FF_MT_COUNT 0x18 |
Sean Nyekjaer | 9013b1d | 2021-03-01 09:00:28 +0100 | [diff] [blame] | 61 | #define MMA8452_FF_MT_CHAN_SHIFT 3 |
Hartmut Knaack | 69abff8 | 2015-08-02 22:43:50 +0200 | [diff] [blame] | 62 | #define MMA8452_TRANSIENT_CFG 0x1d |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 63 | #define MMA8452_TRANSIENT_CFG_CHAN(chan) BIT(chan + 1) |
Hartmut Knaack | 69abff8 | 2015-08-02 22:43:50 +0200 | [diff] [blame] | 64 | #define MMA8452_TRANSIENT_CFG_HPF_BYP BIT(0) |
Hartmut Knaack | 69abff8 | 2015-08-02 22:43:50 +0200 | [diff] [blame] | 65 | #define MMA8452_TRANSIENT_CFG_ELE BIT(4) |
| 66 | #define MMA8452_TRANSIENT_SRC 0x1e |
| 67 | #define MMA8452_TRANSIENT_SRC_XTRANSE BIT(1) |
| 68 | #define MMA8452_TRANSIENT_SRC_YTRANSE BIT(3) |
| 69 | #define MMA8452_TRANSIENT_SRC_ZTRANSE BIT(5) |
| 70 | #define MMA8452_TRANSIENT_THS 0x1f |
| 71 | #define MMA8452_TRANSIENT_THS_MASK GENMASK(6, 0) |
| 72 | #define MMA8452_TRANSIENT_COUNT 0x20 |
Sean Nyekjaer | 9013b1d | 2021-03-01 09:00:28 +0100 | [diff] [blame] | 73 | #define MMA8452_TRANSIENT_CHAN_SHIFT 1 |
Hartmut Knaack | 69abff8 | 2015-08-02 22:43:50 +0200 | [diff] [blame] | 74 | #define MMA8452_CTRL_REG1 0x2a |
| 75 | #define MMA8452_CTRL_ACTIVE BIT(0) |
| 76 | #define MMA8452_CTRL_DR_MASK GENMASK(5, 3) |
| 77 | #define MMA8452_CTRL_DR_SHIFT 3 |
| 78 | #define MMA8452_CTRL_DR_DEFAULT 0x4 /* 50 Hz sample frequency */ |
| 79 | #define MMA8452_CTRL_REG2 0x2b |
| 80 | #define MMA8452_CTRL_REG2_RST BIT(6) |
Martin Kepplinger | ed859fc | 2016-04-25 14:08:25 +0200 | [diff] [blame] | 81 | #define MMA8452_CTRL_REG2_MODS_SHIFT 3 |
| 82 | #define MMA8452_CTRL_REG2_MODS_MASK 0x1b |
Hartmut Knaack | 69abff8 | 2015-08-02 22:43:50 +0200 | [diff] [blame] | 83 | #define MMA8452_CTRL_REG4 0x2d |
| 84 | #define MMA8452_CTRL_REG5 0x2e |
| 85 | #define MMA8452_OFF_X 0x2f |
| 86 | #define MMA8452_OFF_Y 0x30 |
| 87 | #define MMA8452_OFF_Z 0x31 |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 88 | |
Hartmut Knaack | 69abff8 | 2015-08-02 22:43:50 +0200 | [diff] [blame] | 89 | #define MMA8452_MAX_REG 0x31 |
Martin Fuzzey | 2a17698c | 2015-05-13 12:26:40 +0200 | [diff] [blame] | 90 | |
Hartmut Knaack | 69abff8 | 2015-08-02 22:43:50 +0200 | [diff] [blame] | 91 | #define MMA8452_INT_DRDY BIT(0) |
Martin Kepplinger | 60f562e | 2015-09-01 13:45:10 +0200 | [diff] [blame] | 92 | #define MMA8452_INT_FF_MT BIT(2) |
Hartmut Knaack | 69abff8 | 2015-08-02 22:43:50 +0200 | [diff] [blame] | 93 | #define MMA8452_INT_TRANS BIT(5) |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 94 | |
Martin Kepplinger | 244a93f | 2016-01-16 15:35:22 +0100 | [diff] [blame] | 95 | #define MMA8451_DEVICE_ID 0x1a |
Martin Kepplinger | 36775d5 | 2016-01-16 15:35:21 +0100 | [diff] [blame] | 96 | #define MMA8452_DEVICE_ID 0x2a |
| 97 | #define MMA8453_DEVICE_ID 0x3a |
Martin Kepplinger | 417e008 | 2015-09-01 13:45:11 +0200 | [diff] [blame] | 98 | #define MMA8652_DEVICE_ID 0x4a |
| 99 | #define MMA8653_DEVICE_ID 0x5a |
Martin Kepplinger | e873118 | 2016-03-09 12:01:29 +0100 | [diff] [blame] | 100 | #define FXLS8471_DEVICE_ID 0x6a |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 101 | |
Martin Kepplinger | 96c0cb2 | 2016-03-03 09:24:03 +0100 | [diff] [blame] | 102 | #define MMA8452_AUTO_SUSPEND_DELAY_MS 2000 |
| 103 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 104 | struct mma8452_data { |
| 105 | struct i2c_client *client; |
| 106 | struct mutex lock; |
| 107 | u8 ctrl_reg1; |
| 108 | u8 data_cfg; |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 109 | const struct mma_chip_info *chip_info; |
Richard Tresidder | a45d123 | 2018-05-11 16:54:59 +0800 | [diff] [blame] | 110 | int sleep_val; |
Anson Huang | f6ff49b | 2019-01-08 09:14:06 +0000 | [diff] [blame] | 111 | struct regulator *vdd_reg; |
| 112 | struct regulator *vddio_reg; |
Jonathan Cameron | 89226a2 | 2020-07-22 16:50:38 +0100 | [diff] [blame] | 113 | |
| 114 | /* Ensure correct alignment of time stamp when present */ |
| 115 | struct { |
| 116 | __be16 channels[3]; |
| 117 | s64 ts __aligned(8); |
| 118 | } buffer; |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 119 | }; |
| 120 | |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 121 | /** |
| 122 | * struct mma8452_event_regs - chip specific data related to events |
| 123 | * @ev_cfg: event config register address |
| 124 | * @ev_cfg_ele: latch bit in event config register |
| 125 | * @ev_cfg_chan_shift: number of the bit to enable events in X |
| 126 | * direction; in event config register |
| 127 | * @ev_src: event source register address |
| 128 | * @ev_ths: event threshold register address |
| 129 | * @ev_ths_mask: mask for the threshold value |
| 130 | * @ev_count: event count (period) register address |
| 131 | * |
| 132 | * Since not all chips supported by the driver support comparing high pass |
| 133 | * filtered data for events (interrupts), different interrupt sources are |
| 134 | * used for different chips and the relevant registers are included here. |
| 135 | */ |
| 136 | struct mma8452_event_regs { |
Sean Nyekjaer | 9013b1d | 2021-03-01 09:00:28 +0100 | [diff] [blame] | 137 | u8 ev_cfg; |
| 138 | u8 ev_cfg_ele; |
| 139 | u8 ev_cfg_chan_shift; |
| 140 | u8 ev_src; |
| 141 | u8 ev_ths; |
| 142 | u8 ev_ths_mask; |
| 143 | u8 ev_count; |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 144 | }; |
| 145 | |
Harinath Nampally | a654c06 | 2017-11-05 13:00:03 -0500 | [diff] [blame] | 146 | static const struct mma8452_event_regs ff_mt_ev_regs = { |
Sean Nyekjaer | 9013b1d | 2021-03-01 09:00:28 +0100 | [diff] [blame] | 147 | .ev_cfg = MMA8452_FF_MT_CFG, |
| 148 | .ev_cfg_ele = MMA8452_FF_MT_CFG_ELE, |
| 149 | .ev_cfg_chan_shift = MMA8452_FF_MT_CHAN_SHIFT, |
| 150 | .ev_src = MMA8452_FF_MT_SRC, |
| 151 | .ev_ths = MMA8452_FF_MT_THS, |
| 152 | .ev_ths_mask = MMA8452_FF_MT_THS_MASK, |
| 153 | .ev_count = MMA8452_FF_MT_COUNT |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 154 | }; |
| 155 | |
Harinath Nampally | a654c06 | 2017-11-05 13:00:03 -0500 | [diff] [blame] | 156 | static const struct mma8452_event_regs trans_ev_regs = { |
Sean Nyekjaer | 9013b1d | 2021-03-01 09:00:28 +0100 | [diff] [blame] | 157 | .ev_cfg = MMA8452_TRANSIENT_CFG, |
| 158 | .ev_cfg_ele = MMA8452_TRANSIENT_CFG_ELE, |
| 159 | .ev_cfg_chan_shift = MMA8452_TRANSIENT_CHAN_SHIFT, |
| 160 | .ev_src = MMA8452_TRANSIENT_SRC, |
| 161 | .ev_ths = MMA8452_TRANSIENT_THS, |
| 162 | .ev_ths_mask = MMA8452_TRANSIENT_THS_MASK, |
| 163 | .ev_count = MMA8452_TRANSIENT_COUNT, |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 164 | }; |
| 165 | |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 166 | /** |
Martin Kepplinger | f26ab1a | 2016-06-03 14:51:52 +0200 | [diff] [blame] | 167 | * struct mma_chip_info - chip specific data |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 168 | * @chip_id: WHO_AM_I register's value |
| 169 | * @channels: struct iio_chan_spec matching the device's |
| 170 | * capabilities |
| 171 | * @num_channels: number of channels |
| 172 | * @mma_scales: scale factors for converting register values |
| 173 | * to m/s^2; 3 modes: 2g, 4g, 8g; 2 integers |
| 174 | * per mode: m/s^2 and micro m/s^2 |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 175 | * @all_events: all events supported by this chip |
| 176 | * @enabled_events: event flags enabled and handled by this driver |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 177 | */ |
| 178 | struct mma_chip_info { |
| 179 | u8 chip_id; |
| 180 | const struct iio_chan_spec *channels; |
| 181 | int num_channels; |
| 182 | const int mma_scales[3][2]; |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 183 | int all_events; |
| 184 | int enabled_events; |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 185 | }; |
| 186 | |
Martin Kepplinger | e60378c | 2015-12-15 17:45:00 +0100 | [diff] [blame] | 187 | enum { |
| 188 | idx_x, |
| 189 | idx_y, |
| 190 | idx_z, |
| 191 | idx_ts, |
| 192 | }; |
| 193 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 194 | static int mma8452_drdy(struct mma8452_data *data) |
| 195 | { |
| 196 | int tries = 150; |
| 197 | |
| 198 | while (tries-- > 0) { |
| 199 | int ret = i2c_smbus_read_byte_data(data->client, |
| 200 | MMA8452_STATUS); |
| 201 | if (ret < 0) |
| 202 | return ret; |
| 203 | if ((ret & MMA8452_STATUS_DRDY) == MMA8452_STATUS_DRDY) |
| 204 | return 0; |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 205 | |
Richard Tresidder | a45d123 | 2018-05-11 16:54:59 +0800 | [diff] [blame] | 206 | if (data->sleep_val <= 20) |
| 207 | usleep_range(data->sleep_val * 250, |
| 208 | data->sleep_val * 500); |
| 209 | else |
| 210 | msleep(20); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | dev_err(&data->client->dev, "data not ready\n"); |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 214 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 215 | return -EIO; |
| 216 | } |
| 217 | |
Martin Kepplinger | 96c0cb2 | 2016-03-03 09:24:03 +0100 | [diff] [blame] | 218 | static int mma8452_set_runtime_pm_state(struct i2c_client *client, bool on) |
| 219 | { |
| 220 | #ifdef CONFIG_PM |
| 221 | int ret; |
| 222 | |
| 223 | if (on) { |
| 224 | ret = pm_runtime_get_sync(&client->dev); |
| 225 | } else { |
| 226 | pm_runtime_mark_last_busy(&client->dev); |
| 227 | ret = pm_runtime_put_autosuspend(&client->dev); |
| 228 | } |
| 229 | |
| 230 | if (ret < 0) { |
| 231 | dev_err(&client->dev, |
| 232 | "failed to change power state to %d\n", on); |
| 233 | if (on) |
| 234 | pm_runtime_put_noidle(&client->dev); |
| 235 | |
| 236 | return ret; |
| 237 | } |
| 238 | #endif |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 243 | static int mma8452_read(struct mma8452_data *data, __be16 buf[3]) |
| 244 | { |
| 245 | int ret = mma8452_drdy(data); |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 246 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 247 | if (ret < 0) |
| 248 | return ret; |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 249 | |
Martin Kepplinger | 96c0cb2 | 2016-03-03 09:24:03 +0100 | [diff] [blame] | 250 | ret = mma8452_set_runtime_pm_state(data->client, true); |
| 251 | if (ret) |
| 252 | return ret; |
| 253 | |
| 254 | ret = i2c_smbus_read_i2c_block_data(data->client, MMA8452_OUT_X, |
| 255 | 3 * sizeof(__be16), (u8 *)buf); |
| 256 | |
| 257 | ret = mma8452_set_runtime_pm_state(data->client, false); |
| 258 | |
| 259 | return ret; |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 262 | static ssize_t mma8452_show_int_plus_micros(char *buf, const int (*vals)[2], |
| 263 | int n) |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 264 | { |
| 265 | size_t len = 0; |
| 266 | |
| 267 | while (n-- > 0) |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 268 | len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06d ", |
| 269 | vals[n][0], vals[n][1]); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 270 | |
| 271 | /* replace trailing space by newline */ |
| 272 | buf[len - 1] = '\n'; |
| 273 | |
| 274 | return len; |
| 275 | } |
| 276 | |
| 277 | static int mma8452_get_int_plus_micros_index(const int (*vals)[2], int n, |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 278 | int val, int val2) |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 279 | { |
| 280 | while (n-- > 0) |
| 281 | if (val == vals[n][0] && val2 == vals[n][1]) |
| 282 | return n; |
| 283 | |
| 284 | return -EINVAL; |
| 285 | } |
| 286 | |
Martin Kepplinger | 32b2807 | 2016-11-21 20:53:54 +0100 | [diff] [blame] | 287 | static unsigned int mma8452_get_odr_index(struct mma8452_data *data) |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 288 | { |
| 289 | return (data->ctrl_reg1 & MMA8452_CTRL_DR_MASK) >> |
| 290 | MMA8452_CTRL_DR_SHIFT; |
| 291 | } |
| 292 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 293 | static const int mma8452_samp_freq[8][2] = { |
| 294 | {800, 0}, {400, 0}, {200, 0}, {100, 0}, {50, 0}, {12, 500000}, |
| 295 | {6, 250000}, {1, 560000} |
| 296 | }; |
| 297 | |
Martin Kepplinger | ed859fc | 2016-04-25 14:08:25 +0200 | [diff] [blame] | 298 | /* Datasheet table: step time "Relationship with the ODR" (sample frequency) */ |
Harinath Nampally | cc54a66 | 2017-11-05 13:00:02 -0500 | [diff] [blame] | 299 | static const unsigned int mma8452_time_step_us[4][8] = { |
Martin Kepplinger | ed859fc | 2016-04-25 14:08:25 +0200 | [diff] [blame] | 300 | { 1250, 2500, 5000, 10000, 20000, 20000, 20000, 20000 }, /* normal */ |
| 301 | { 1250, 2500, 5000, 10000, 20000, 80000, 80000, 80000 }, /* l p l n */ |
| 302 | { 1250, 2500, 2500, 2500, 2500, 2500, 2500, 2500 }, /* high res*/ |
| 303 | { 1250, 2500, 5000, 10000, 20000, 80000, 160000, 160000 } /* l p */ |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 304 | }; |
| 305 | |
Martin Kepplinger | ed859fc | 2016-04-25 14:08:25 +0200 | [diff] [blame] | 306 | /* Datasheet table "High-Pass Filter Cutoff Options" */ |
| 307 | static const int mma8452_hp_filter_cutoff[4][8][4][2] = { |
| 308 | { /* normal */ |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 309 | { {16, 0}, {8, 0}, {4, 0}, {2, 0} }, /* 800 Hz sample */ |
| 310 | { {16, 0}, {8, 0}, {4, 0}, {2, 0} }, /* 400 Hz sample */ |
| 311 | { {8, 0}, {4, 0}, {2, 0}, {1, 0} }, /* 200 Hz sample */ |
| 312 | { {4, 0}, {2, 0}, {1, 0}, {0, 500000} }, /* 100 Hz sample */ |
| 313 | { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} }, /* 50 Hz sample */ |
| 314 | { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} }, /* 12.5 Hz sample */ |
| 315 | { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} }, /* 6.25 Hz sample */ |
| 316 | { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} } /* 1.56 Hz sample */ |
Martin Kepplinger | ed859fc | 2016-04-25 14:08:25 +0200 | [diff] [blame] | 317 | }, |
| 318 | { /* low noise low power */ |
| 319 | { {16, 0}, {8, 0}, {4, 0}, {2, 0} }, |
| 320 | { {16, 0}, {8, 0}, {4, 0}, {2, 0} }, |
| 321 | { {8, 0}, {4, 0}, {2, 0}, {1, 0} }, |
| 322 | { {4, 0}, {2, 0}, {1, 0}, {0, 500000} }, |
| 323 | { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} }, |
| 324 | { {0, 500000}, {0, 250000}, {0, 125000}, {0, 063000} }, |
| 325 | { {0, 500000}, {0, 250000}, {0, 125000}, {0, 063000} }, |
| 326 | { {0, 500000}, {0, 250000}, {0, 125000}, {0, 063000} } |
| 327 | }, |
| 328 | { /* high resolution */ |
| 329 | { {16, 0}, {8, 0}, {4, 0}, {2, 0} }, |
| 330 | { {16, 0}, {8, 0}, {4, 0}, {2, 0} }, |
| 331 | { {16, 0}, {8, 0}, {4, 0}, {2, 0} }, |
| 332 | { {16, 0}, {8, 0}, {4, 0}, {2, 0} }, |
| 333 | { {16, 0}, {8, 0}, {4, 0}, {2, 0} }, |
| 334 | { {16, 0}, {8, 0}, {4, 0}, {2, 0} }, |
| 335 | { {16, 0}, {8, 0}, {4, 0}, {2, 0} }, |
| 336 | { {16, 0}, {8, 0}, {4, 0}, {2, 0} } |
| 337 | }, |
| 338 | { /* low power */ |
| 339 | { {16, 0}, {8, 0}, {4, 0}, {2, 0} }, |
| 340 | { {8, 0}, {4, 0}, {2, 0}, {1, 0} }, |
| 341 | { {4, 0}, {2, 0}, {1, 0}, {0, 500000} }, |
| 342 | { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} }, |
| 343 | { {1, 0}, {0, 500000}, {0, 250000}, {0, 125000} }, |
| 344 | { {0, 250000}, {0, 125000}, {0, 063000}, {0, 031000} }, |
| 345 | { {0, 250000}, {0, 125000}, {0, 063000}, {0, 031000} }, |
| 346 | { {0, 250000}, {0, 125000}, {0, 063000}, {0, 031000} } |
| 347 | } |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 348 | }; |
| 349 | |
Martin Kepplinger | ed859fc | 2016-04-25 14:08:25 +0200 | [diff] [blame] | 350 | /* Datasheet table "MODS Oversampling modes averaging values at each ODR" */ |
| 351 | static const u16 mma8452_os_ratio[4][8] = { |
| 352 | /* 800 Hz, 400 Hz, ... , 1.56 Hz */ |
| 353 | { 2, 4, 4, 4, 4, 16, 32, 128 }, /* normal */ |
| 354 | { 2, 4, 4, 4, 4, 4, 8, 32 }, /* low power low noise */ |
| 355 | { 2, 4, 8, 16, 32, 128, 256, 1024 }, /* high resolution */ |
| 356 | { 2, 2, 2, 2, 2, 2, 4, 16 } /* low power */ |
| 357 | }; |
| 358 | |
| 359 | static int mma8452_get_power_mode(struct mma8452_data *data) |
| 360 | { |
| 361 | int reg; |
| 362 | |
| 363 | reg = i2c_smbus_read_byte_data(data->client, |
| 364 | MMA8452_CTRL_REG2); |
| 365 | if (reg < 0) |
| 366 | return reg; |
| 367 | |
| 368 | return ((reg & MMA8452_CTRL_REG2_MODS_MASK) >> |
| 369 | MMA8452_CTRL_REG2_MODS_SHIFT); |
| 370 | } |
| 371 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 372 | static ssize_t mma8452_show_samp_freq_avail(struct device *dev, |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 373 | struct device_attribute *attr, |
| 374 | char *buf) |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 375 | { |
| 376 | return mma8452_show_int_plus_micros(buf, mma8452_samp_freq, |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 377 | ARRAY_SIZE(mma8452_samp_freq)); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | static ssize_t mma8452_show_scale_avail(struct device *dev, |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 381 | struct device_attribute *attr, |
| 382 | char *buf) |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 383 | { |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 384 | struct mma8452_data *data = iio_priv(i2c_get_clientdata( |
| 385 | to_i2c_client(dev))); |
| 386 | |
| 387 | return mma8452_show_int_plus_micros(buf, data->chip_info->mma_scales, |
| 388 | ARRAY_SIZE(data->chip_info->mma_scales)); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 391 | static ssize_t mma8452_show_hp_cutoff_avail(struct device *dev, |
| 392 | struct device_attribute *attr, |
| 393 | char *buf) |
| 394 | { |
| 395 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
| 396 | struct mma8452_data *data = iio_priv(indio_dev); |
Martin Kepplinger | ed859fc | 2016-04-25 14:08:25 +0200 | [diff] [blame] | 397 | int i, j; |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 398 | |
Martin Kepplinger | ed859fc | 2016-04-25 14:08:25 +0200 | [diff] [blame] | 399 | i = mma8452_get_odr_index(data); |
| 400 | j = mma8452_get_power_mode(data); |
| 401 | if (j < 0) |
| 402 | return j; |
| 403 | |
| 404 | return mma8452_show_int_plus_micros(buf, mma8452_hp_filter_cutoff[j][i], |
| 405 | ARRAY_SIZE(mma8452_hp_filter_cutoff[0][0])); |
| 406 | } |
| 407 | |
| 408 | static ssize_t mma8452_show_os_ratio_avail(struct device *dev, |
| 409 | struct device_attribute *attr, |
| 410 | char *buf) |
| 411 | { |
| 412 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
| 413 | struct mma8452_data *data = iio_priv(indio_dev); |
| 414 | int i = mma8452_get_odr_index(data); |
| 415 | int j; |
| 416 | u16 val = 0; |
| 417 | size_t len = 0; |
| 418 | |
| 419 | for (j = 0; j < ARRAY_SIZE(mma8452_os_ratio); j++) { |
| 420 | if (val == mma8452_os_ratio[j][i]) |
| 421 | continue; |
| 422 | |
| 423 | val = mma8452_os_ratio[j][i]; |
| 424 | |
| 425 | len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", val); |
| 426 | } |
| 427 | buf[len - 1] = '\n'; |
| 428 | |
| 429 | return len; |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 430 | } |
| 431 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 432 | static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(mma8452_show_samp_freq_avail); |
Harinath Nampally | cd327b0 | 2017-09-23 16:56:29 -0400 | [diff] [blame] | 433 | static IIO_DEVICE_ATTR(in_accel_scale_available, 0444, |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 434 | mma8452_show_scale_avail, NULL, 0); |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 435 | static IIO_DEVICE_ATTR(in_accel_filter_high_pass_3db_frequency_available, |
Harinath Nampally | cd327b0 | 2017-09-23 16:56:29 -0400 | [diff] [blame] | 436 | 0444, mma8452_show_hp_cutoff_avail, NULL, 0); |
| 437 | static IIO_DEVICE_ATTR(in_accel_oversampling_ratio_available, 0444, |
Martin Kepplinger | ed859fc | 2016-04-25 14:08:25 +0200 | [diff] [blame] | 438 | mma8452_show_os_ratio_avail, NULL, 0); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 439 | |
| 440 | static int mma8452_get_samp_freq_index(struct mma8452_data *data, |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 441 | int val, int val2) |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 442 | { |
| 443 | return mma8452_get_int_plus_micros_index(mma8452_samp_freq, |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 444 | ARRAY_SIZE(mma8452_samp_freq), |
| 445 | val, val2); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 448 | static int mma8452_get_scale_index(struct mma8452_data *data, int val, int val2) |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 449 | { |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 450 | return mma8452_get_int_plus_micros_index(data->chip_info->mma_scales, |
| 451 | ARRAY_SIZE(data->chip_info->mma_scales), val, val2); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 454 | static int mma8452_get_hp_filter_index(struct mma8452_data *data, |
| 455 | int val, int val2) |
| 456 | { |
Martin Kepplinger | ed859fc | 2016-04-25 14:08:25 +0200 | [diff] [blame] | 457 | int i, j; |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 458 | |
Martin Kepplinger | ed859fc | 2016-04-25 14:08:25 +0200 | [diff] [blame] | 459 | i = mma8452_get_odr_index(data); |
| 460 | j = mma8452_get_power_mode(data); |
| 461 | if (j < 0) |
| 462 | return j; |
| 463 | |
| 464 | return mma8452_get_int_plus_micros_index(mma8452_hp_filter_cutoff[j][i], |
| 465 | ARRAY_SIZE(mma8452_hp_filter_cutoff[0][0]), val, val2); |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | static int mma8452_read_hp_filter(struct mma8452_data *data, int *hz, int *uHz) |
| 469 | { |
Martin Kepplinger | ed859fc | 2016-04-25 14:08:25 +0200 | [diff] [blame] | 470 | int j, i, ret; |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 471 | |
| 472 | ret = i2c_smbus_read_byte_data(data->client, MMA8452_HP_FILTER_CUTOFF); |
| 473 | if (ret < 0) |
| 474 | return ret; |
| 475 | |
| 476 | i = mma8452_get_odr_index(data); |
Martin Kepplinger | ed859fc | 2016-04-25 14:08:25 +0200 | [diff] [blame] | 477 | j = mma8452_get_power_mode(data); |
| 478 | if (j < 0) |
| 479 | return j; |
| 480 | |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 481 | ret &= MMA8452_HP_FILTER_CUTOFF_SEL_MASK; |
Martin Kepplinger | ed859fc | 2016-04-25 14:08:25 +0200 | [diff] [blame] | 482 | *hz = mma8452_hp_filter_cutoff[j][i][ret][0]; |
| 483 | *uHz = mma8452_hp_filter_cutoff[j][i][ret][1]; |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 484 | |
| 485 | return 0; |
| 486 | } |
| 487 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 488 | static int mma8452_read_raw(struct iio_dev *indio_dev, |
| 489 | struct iio_chan_spec const *chan, |
| 490 | int *val, int *val2, long mask) |
| 491 | { |
| 492 | struct mma8452_data *data = iio_priv(indio_dev); |
| 493 | __be16 buffer[3]; |
| 494 | int i, ret; |
| 495 | |
| 496 | switch (mask) { |
| 497 | case IIO_CHAN_INFO_RAW: |
Alison Schofield | 4d9b041 | 2016-10-11 12:31:36 -0700 | [diff] [blame] | 498 | ret = iio_device_claim_direct_mode(indio_dev); |
| 499 | if (ret) |
| 500 | return ret; |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 501 | |
| 502 | mutex_lock(&data->lock); |
| 503 | ret = mma8452_read(data, buffer); |
| 504 | mutex_unlock(&data->lock); |
Alison Schofield | 4d9b041 | 2016-10-11 12:31:36 -0700 | [diff] [blame] | 505 | iio_device_release_direct_mode(indio_dev); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 506 | if (ret < 0) |
| 507 | return ret; |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 508 | |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 509 | *val = sign_extend32(be16_to_cpu( |
| 510 | buffer[chan->scan_index]) >> chan->scan_type.shift, |
| 511 | chan->scan_type.realbits - 1); |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 512 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 513 | return IIO_VAL_INT; |
| 514 | case IIO_CHAN_INFO_SCALE: |
| 515 | i = data->data_cfg & MMA8452_DATA_CFG_FS_MASK; |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 516 | *val = data->chip_info->mma_scales[i][0]; |
| 517 | *val2 = data->chip_info->mma_scales[i][1]; |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 518 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 519 | return IIO_VAL_INT_PLUS_MICRO; |
| 520 | case IIO_CHAN_INFO_SAMP_FREQ: |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 521 | i = mma8452_get_odr_index(data); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 522 | *val = mma8452_samp_freq[i][0]; |
| 523 | *val2 = mma8452_samp_freq[i][1]; |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 524 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 525 | return IIO_VAL_INT_PLUS_MICRO; |
| 526 | case IIO_CHAN_INFO_CALIBBIAS: |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 527 | ret = i2c_smbus_read_byte_data(data->client, |
Martin Kepplinger | 8b8ff3a | 2016-03-03 09:24:01 +0100 | [diff] [blame] | 528 | MMA8452_OFF_X + |
| 529 | chan->scan_index); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 530 | if (ret < 0) |
| 531 | return ret; |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 532 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 533 | *val = sign_extend32(ret, 7); |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 534 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 535 | return IIO_VAL_INT; |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 536 | case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY: |
| 537 | if (data->data_cfg & MMA8452_DATA_CFG_HPF_MASK) { |
| 538 | ret = mma8452_read_hp_filter(data, val, val2); |
| 539 | if (ret < 0) |
| 540 | return ret; |
| 541 | } else { |
| 542 | *val = 0; |
| 543 | *val2 = 0; |
| 544 | } |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 545 | |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 546 | return IIO_VAL_INT_PLUS_MICRO; |
Martin Kepplinger | ed859fc | 2016-04-25 14:08:25 +0200 | [diff] [blame] | 547 | case IIO_CHAN_INFO_OVERSAMPLING_RATIO: |
| 548 | ret = mma8452_get_power_mode(data); |
| 549 | if (ret < 0) |
| 550 | return ret; |
| 551 | |
| 552 | i = mma8452_get_odr_index(data); |
| 553 | |
| 554 | *val = mma8452_os_ratio[ret][i]; |
| 555 | return IIO_VAL_INT; |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 556 | } |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 557 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 558 | return -EINVAL; |
| 559 | } |
| 560 | |
Richard Tresidder | a45d123 | 2018-05-11 16:54:59 +0800 | [diff] [blame] | 561 | static int mma8452_calculate_sleep(struct mma8452_data *data) |
| 562 | { |
| 563 | int ret, i = mma8452_get_odr_index(data); |
| 564 | |
| 565 | if (mma8452_samp_freq[i][0] > 0) |
| 566 | ret = 1000 / mma8452_samp_freq[i][0]; |
| 567 | else |
| 568 | ret = 1000; |
| 569 | |
| 570 | return ret == 0 ? 1 : ret; |
| 571 | } |
| 572 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 573 | static int mma8452_standby(struct mma8452_data *data) |
| 574 | { |
| 575 | return i2c_smbus_write_byte_data(data->client, MMA8452_CTRL_REG1, |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 576 | data->ctrl_reg1 & ~MMA8452_CTRL_ACTIVE); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | static int mma8452_active(struct mma8452_data *data) |
| 580 | { |
| 581 | return i2c_smbus_write_byte_data(data->client, MMA8452_CTRL_REG1, |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 582 | data->ctrl_reg1); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Martin Kepplinger | e866853 | 2016-03-03 09:24:02 +0100 | [diff] [blame] | 585 | /* returns >0 if active, 0 if in standby and <0 on error */ |
| 586 | static int mma8452_is_active(struct mma8452_data *data) |
| 587 | { |
| 588 | int reg; |
| 589 | |
| 590 | reg = i2c_smbus_read_byte_data(data->client, MMA8452_CTRL_REG1); |
| 591 | if (reg < 0) |
| 592 | return reg; |
| 593 | |
| 594 | return reg & MMA8452_CTRL_ACTIVE; |
| 595 | } |
| 596 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 597 | static int mma8452_change_config(struct mma8452_data *data, u8 reg, u8 val) |
| 598 | { |
| 599 | int ret; |
Martin Kepplinger | e866853 | 2016-03-03 09:24:02 +0100 | [diff] [blame] | 600 | int is_active; |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 601 | |
| 602 | mutex_lock(&data->lock); |
| 603 | |
Martin Kepplinger | e866853 | 2016-03-03 09:24:02 +0100 | [diff] [blame] | 604 | is_active = mma8452_is_active(data); |
| 605 | if (is_active < 0) { |
| 606 | ret = is_active; |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 607 | goto fail; |
Martin Kepplinger | e866853 | 2016-03-03 09:24:02 +0100 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | /* config can only be changed when in standby */ |
| 611 | if (is_active > 0) { |
| 612 | ret = mma8452_standby(data); |
| 613 | if (ret < 0) |
| 614 | goto fail; |
| 615 | } |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 616 | |
| 617 | ret = i2c_smbus_write_byte_data(data->client, reg, val); |
| 618 | if (ret < 0) |
| 619 | goto fail; |
| 620 | |
Martin Kepplinger | e866853 | 2016-03-03 09:24:02 +0100 | [diff] [blame] | 621 | if (is_active > 0) { |
| 622 | ret = mma8452_active(data); |
| 623 | if (ret < 0) |
| 624 | goto fail; |
| 625 | } |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 626 | |
| 627 | ret = 0; |
| 628 | fail: |
| 629 | mutex_unlock(&data->lock); |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 630 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 631 | return ret; |
| 632 | } |
| 633 | |
Martin Kepplinger | ed859fc | 2016-04-25 14:08:25 +0200 | [diff] [blame] | 634 | static int mma8452_set_power_mode(struct mma8452_data *data, u8 mode) |
| 635 | { |
| 636 | int reg; |
| 637 | |
| 638 | reg = i2c_smbus_read_byte_data(data->client, |
| 639 | MMA8452_CTRL_REG2); |
| 640 | if (reg < 0) |
| 641 | return reg; |
| 642 | |
| 643 | reg &= ~MMA8452_CTRL_REG2_MODS_MASK; |
| 644 | reg |= mode << MMA8452_CTRL_REG2_MODS_SHIFT; |
| 645 | |
| 646 | return mma8452_change_config(data, MMA8452_CTRL_REG2, reg); |
| 647 | } |
| 648 | |
Martin Kepplinger | 8b8ff3a | 2016-03-03 09:24:01 +0100 | [diff] [blame] | 649 | /* returns >0 if in freefall mode, 0 if not or <0 if an error occurred */ |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame] | 650 | static int mma8452_freefall_mode_enabled(struct mma8452_data *data) |
| 651 | { |
| 652 | int val; |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame] | 653 | |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 654 | val = i2c_smbus_read_byte_data(data->client, MMA8452_FF_MT_CFG); |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame] | 655 | if (val < 0) |
| 656 | return val; |
| 657 | |
| 658 | return !(val & MMA8452_FF_MT_CFG_OAE); |
| 659 | } |
| 660 | |
| 661 | static int mma8452_set_freefall_mode(struct mma8452_data *data, bool state) |
| 662 | { |
| 663 | int val; |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame] | 664 | |
| 665 | if ((state && mma8452_freefall_mode_enabled(data)) || |
| 666 | (!state && !(mma8452_freefall_mode_enabled(data)))) |
| 667 | return 0; |
| 668 | |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 669 | val = i2c_smbus_read_byte_data(data->client, MMA8452_FF_MT_CFG); |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame] | 670 | if (val < 0) |
| 671 | return val; |
| 672 | |
| 673 | if (state) { |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 674 | val |= BIT(idx_x + MMA8452_FF_MT_CHAN_SHIFT); |
| 675 | val |= BIT(idx_y + MMA8452_FF_MT_CHAN_SHIFT); |
| 676 | val |= BIT(idx_z + MMA8452_FF_MT_CHAN_SHIFT); |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame] | 677 | val &= ~MMA8452_FF_MT_CFG_OAE; |
| 678 | } else { |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 679 | val &= ~BIT(idx_x + MMA8452_FF_MT_CHAN_SHIFT); |
| 680 | val &= ~BIT(idx_y + MMA8452_FF_MT_CHAN_SHIFT); |
| 681 | val &= ~BIT(idx_z + MMA8452_FF_MT_CHAN_SHIFT); |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame] | 682 | val |= MMA8452_FF_MT_CFG_OAE; |
| 683 | } |
| 684 | |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 685 | return mma8452_change_config(data, MMA8452_FF_MT_CFG, val); |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame] | 686 | } |
| 687 | |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 688 | static int mma8452_set_hp_filter_frequency(struct mma8452_data *data, |
| 689 | int val, int val2) |
| 690 | { |
| 691 | int i, reg; |
| 692 | |
| 693 | i = mma8452_get_hp_filter_index(data, val, val2); |
| 694 | if (i < 0) |
Hartmut Knaack | b9fddcd | 2015-08-02 22:43:48 +0200 | [diff] [blame] | 695 | return i; |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 696 | |
| 697 | reg = i2c_smbus_read_byte_data(data->client, |
| 698 | MMA8452_HP_FILTER_CUTOFF); |
| 699 | if (reg < 0) |
| 700 | return reg; |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 701 | |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 702 | reg &= ~MMA8452_HP_FILTER_CUTOFF_SEL_MASK; |
| 703 | reg |= i; |
| 704 | |
| 705 | return mma8452_change_config(data, MMA8452_HP_FILTER_CUTOFF, reg); |
| 706 | } |
| 707 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 708 | static int mma8452_write_raw(struct iio_dev *indio_dev, |
| 709 | struct iio_chan_spec const *chan, |
| 710 | int val, int val2, long mask) |
| 711 | { |
| 712 | struct mma8452_data *data = iio_priv(indio_dev); |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 713 | int i, ret; |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 714 | |
Jonathan Cameron | 79de2ee | 2016-10-15 15:55:06 +0100 | [diff] [blame] | 715 | ret = iio_device_claim_direct_mode(indio_dev); |
| 716 | if (ret) |
| 717 | return ret; |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 718 | |
| 719 | switch (mask) { |
| 720 | case IIO_CHAN_INFO_SAMP_FREQ: |
| 721 | i = mma8452_get_samp_freq_index(data, val, val2); |
Jonathan Cameron | 79de2ee | 2016-10-15 15:55:06 +0100 | [diff] [blame] | 722 | if (i < 0) { |
| 723 | ret = i; |
| 724 | break; |
| 725 | } |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 726 | data->ctrl_reg1 &= ~MMA8452_CTRL_DR_MASK; |
| 727 | data->ctrl_reg1 |= i << MMA8452_CTRL_DR_SHIFT; |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 728 | |
Richard Tresidder | a45d123 | 2018-05-11 16:54:59 +0800 | [diff] [blame] | 729 | data->sleep_val = mma8452_calculate_sleep(data); |
| 730 | |
Jonathan Cameron | 79de2ee | 2016-10-15 15:55:06 +0100 | [diff] [blame] | 731 | ret = mma8452_change_config(data, MMA8452_CTRL_REG1, |
| 732 | data->ctrl_reg1); |
| 733 | break; |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 734 | case IIO_CHAN_INFO_SCALE: |
| 735 | i = mma8452_get_scale_index(data, val, val2); |
Jonathan Cameron | 79de2ee | 2016-10-15 15:55:06 +0100 | [diff] [blame] | 736 | if (i < 0) { |
| 737 | ret = i; |
| 738 | break; |
| 739 | } |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 740 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 741 | data->data_cfg &= ~MMA8452_DATA_CFG_FS_MASK; |
| 742 | data->data_cfg |= i; |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 743 | |
Jonathan Cameron | 79de2ee | 2016-10-15 15:55:06 +0100 | [diff] [blame] | 744 | ret = mma8452_change_config(data, MMA8452_DATA_CFG, |
| 745 | data->data_cfg); |
| 746 | break; |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 747 | case IIO_CHAN_INFO_CALIBBIAS: |
Jonathan Cameron | 79de2ee | 2016-10-15 15:55:06 +0100 | [diff] [blame] | 748 | if (val < -128 || val > 127) { |
| 749 | ret = -EINVAL; |
| 750 | break; |
| 751 | } |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 752 | |
Jonathan Cameron | 79de2ee | 2016-10-15 15:55:06 +0100 | [diff] [blame] | 753 | ret = mma8452_change_config(data, |
| 754 | MMA8452_OFF_X + chan->scan_index, |
| 755 | val); |
| 756 | break; |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 757 | |
| 758 | case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY: |
| 759 | if (val == 0 && val2 == 0) { |
| 760 | data->data_cfg &= ~MMA8452_DATA_CFG_HPF_MASK; |
| 761 | } else { |
| 762 | data->data_cfg |= MMA8452_DATA_CFG_HPF_MASK; |
| 763 | ret = mma8452_set_hp_filter_frequency(data, val, val2); |
| 764 | if (ret < 0) |
Jonathan Cameron | 79de2ee | 2016-10-15 15:55:06 +0100 | [diff] [blame] | 765 | break; |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 766 | } |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 767 | |
Jonathan Cameron | 79de2ee | 2016-10-15 15:55:06 +0100 | [diff] [blame] | 768 | ret = mma8452_change_config(data, MMA8452_DATA_CFG, |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 769 | data->data_cfg); |
Jonathan Cameron | 79de2ee | 2016-10-15 15:55:06 +0100 | [diff] [blame] | 770 | break; |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 771 | |
Martin Kepplinger | ed859fc | 2016-04-25 14:08:25 +0200 | [diff] [blame] | 772 | case IIO_CHAN_INFO_OVERSAMPLING_RATIO: |
| 773 | ret = mma8452_get_odr_index(data); |
| 774 | |
| 775 | for (i = 0; i < ARRAY_SIZE(mma8452_os_ratio); i++) { |
Jonathan Cameron | 79de2ee | 2016-10-15 15:55:06 +0100 | [diff] [blame] | 776 | if (mma8452_os_ratio[i][ret] == val) { |
| 777 | ret = mma8452_set_power_mode(data, i); |
| 778 | break; |
| 779 | } |
Martin Kepplinger | ed859fc | 2016-04-25 14:08:25 +0200 | [diff] [blame] | 780 | } |
Jonathan Cameron | 79de2ee | 2016-10-15 15:55:06 +0100 | [diff] [blame] | 781 | break; |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 782 | default: |
Jonathan Cameron | 79de2ee | 2016-10-15 15:55:06 +0100 | [diff] [blame] | 783 | ret = -EINVAL; |
| 784 | break; |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 785 | } |
Jonathan Cameron | 79de2ee | 2016-10-15 15:55:06 +0100 | [diff] [blame] | 786 | |
| 787 | iio_device_release_direct_mode(indio_dev); |
| 788 | return ret; |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 791 | static int mma8452_get_event_regs(struct mma8452_data *data, |
| 792 | const struct iio_chan_spec *chan, enum iio_event_direction dir, |
| 793 | const struct mma8452_event_regs **ev_reg) |
| 794 | { |
| 795 | if (!chan) |
| 796 | return -EINVAL; |
| 797 | |
| 798 | switch (chan->type) { |
| 799 | case IIO_ACCEL: |
| 800 | switch (dir) { |
| 801 | case IIO_EV_DIR_RISING: |
| 802 | if ((data->chip_info->all_events |
| 803 | & MMA8452_INT_TRANS) && |
| 804 | (data->chip_info->enabled_events |
| 805 | & MMA8452_INT_TRANS)) |
Harinath Nampally | a654c06 | 2017-11-05 13:00:03 -0500 | [diff] [blame] | 806 | *ev_reg = &trans_ev_regs; |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 807 | else |
Harinath Nampally | a654c06 | 2017-11-05 13:00:03 -0500 | [diff] [blame] | 808 | *ev_reg = &ff_mt_ev_regs; |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 809 | return 0; |
| 810 | case IIO_EV_DIR_FALLING: |
Harinath Nampally | a654c06 | 2017-11-05 13:00:03 -0500 | [diff] [blame] | 811 | *ev_reg = &ff_mt_ev_regs; |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 812 | return 0; |
| 813 | default: |
| 814 | return -EINVAL; |
| 815 | } |
| 816 | default: |
| 817 | return -EINVAL; |
| 818 | } |
| 819 | } |
| 820 | |
Harinath Nampally | 4febd9f | 2017-09-25 06:40:08 -0400 | [diff] [blame] | 821 | static int mma8452_read_event_value(struct iio_dev *indio_dev, |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 822 | const struct iio_chan_spec *chan, |
| 823 | enum iio_event_type type, |
| 824 | enum iio_event_direction dir, |
| 825 | enum iio_event_info info, |
| 826 | int *val, int *val2) |
| 827 | { |
| 828 | struct mma8452_data *data = iio_priv(indio_dev); |
Martin Kepplinger | ed859fc | 2016-04-25 14:08:25 +0200 | [diff] [blame] | 829 | int ret, us, power_mode; |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 830 | const struct mma8452_event_regs *ev_regs; |
| 831 | |
| 832 | ret = mma8452_get_event_regs(data, chan, dir, &ev_regs); |
| 833 | if (ret) |
| 834 | return ret; |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 835 | |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 836 | switch (info) { |
| 837 | case IIO_EV_INFO_VALUE: |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 838 | ret = i2c_smbus_read_byte_data(data->client, ev_regs->ev_ths); |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 839 | if (ret < 0) |
| 840 | return ret; |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 841 | |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 842 | *val = ret & ev_regs->ev_ths_mask; |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 843 | |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 844 | return IIO_VAL_INT; |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 845 | |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 846 | case IIO_EV_INFO_PERIOD: |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 847 | ret = i2c_smbus_read_byte_data(data->client, ev_regs->ev_count); |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 848 | if (ret < 0) |
| 849 | return ret; |
| 850 | |
Martin Kepplinger | ed859fc | 2016-04-25 14:08:25 +0200 | [diff] [blame] | 851 | power_mode = mma8452_get_power_mode(data); |
| 852 | if (power_mode < 0) |
| 853 | return power_mode; |
| 854 | |
Harinath Nampally | cc54a66 | 2017-11-05 13:00:02 -0500 | [diff] [blame] | 855 | us = ret * mma8452_time_step_us[power_mode][ |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 856 | mma8452_get_odr_index(data)]; |
| 857 | *val = us / USEC_PER_SEC; |
| 858 | *val2 = us % USEC_PER_SEC; |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 859 | |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 860 | return IIO_VAL_INT_PLUS_MICRO; |
| 861 | |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 862 | case IIO_EV_INFO_HIGH_PASS_FILTER_3DB: |
| 863 | ret = i2c_smbus_read_byte_data(data->client, |
| 864 | MMA8452_TRANSIENT_CFG); |
| 865 | if (ret < 0) |
| 866 | return ret; |
| 867 | |
| 868 | if (ret & MMA8452_TRANSIENT_CFG_HPF_BYP) { |
| 869 | *val = 0; |
| 870 | *val2 = 0; |
| 871 | } else { |
| 872 | ret = mma8452_read_hp_filter(data, val, val2); |
| 873 | if (ret < 0) |
| 874 | return ret; |
| 875 | } |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 876 | |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 877 | return IIO_VAL_INT_PLUS_MICRO; |
| 878 | |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 879 | default: |
| 880 | return -EINVAL; |
| 881 | } |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 882 | } |
| 883 | |
Harinath Nampally | 4febd9f | 2017-09-25 06:40:08 -0400 | [diff] [blame] | 884 | static int mma8452_write_event_value(struct iio_dev *indio_dev, |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 885 | const struct iio_chan_spec *chan, |
| 886 | enum iio_event_type type, |
| 887 | enum iio_event_direction dir, |
| 888 | enum iio_event_info info, |
| 889 | int val, int val2) |
| 890 | { |
| 891 | struct mma8452_data *data = iio_priv(indio_dev); |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 892 | int ret, reg, steps; |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 893 | const struct mma8452_event_regs *ev_regs; |
| 894 | |
| 895 | ret = mma8452_get_event_regs(data, chan, dir, &ev_regs); |
| 896 | if (ret) |
| 897 | return ret; |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 898 | |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 899 | switch (info) { |
| 900 | case IIO_EV_INFO_VALUE: |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 901 | if (val < 0 || val > ev_regs->ev_ths_mask) |
Hartmut Knaack | 1121822 | 2015-08-02 22:43:49 +0200 | [diff] [blame] | 902 | return -EINVAL; |
| 903 | |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 904 | return mma8452_change_config(data, ev_regs->ev_ths, val); |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 905 | |
| 906 | case IIO_EV_INFO_PERIOD: |
Martin Kepplinger | ed859fc | 2016-04-25 14:08:25 +0200 | [diff] [blame] | 907 | ret = mma8452_get_power_mode(data); |
| 908 | if (ret < 0) |
| 909 | return ret; |
| 910 | |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 911 | steps = (val * USEC_PER_SEC + val2) / |
Harinath Nampally | cc54a66 | 2017-11-05 13:00:02 -0500 | [diff] [blame] | 912 | mma8452_time_step_us[ret][ |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 913 | mma8452_get_odr_index(data)]; |
| 914 | |
Hartmut Knaack | 1121822 | 2015-08-02 22:43:49 +0200 | [diff] [blame] | 915 | if (steps < 0 || steps > 0xff) |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 916 | return -EINVAL; |
| 917 | |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 918 | return mma8452_change_config(data, ev_regs->ev_count, steps); |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 919 | |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 920 | case IIO_EV_INFO_HIGH_PASS_FILTER_3DB: |
| 921 | reg = i2c_smbus_read_byte_data(data->client, |
| 922 | MMA8452_TRANSIENT_CFG); |
| 923 | if (reg < 0) |
| 924 | return reg; |
| 925 | |
| 926 | if (val == 0 && val2 == 0) { |
| 927 | reg |= MMA8452_TRANSIENT_CFG_HPF_BYP; |
| 928 | } else { |
| 929 | reg &= ~MMA8452_TRANSIENT_CFG_HPF_BYP; |
| 930 | ret = mma8452_set_hp_filter_frequency(data, val, val2); |
| 931 | if (ret < 0) |
| 932 | return ret; |
| 933 | } |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 934 | |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 935 | return mma8452_change_config(data, MMA8452_TRANSIENT_CFG, reg); |
| 936 | |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 937 | default: |
| 938 | return -EINVAL; |
| 939 | } |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | static int mma8452_read_event_config(struct iio_dev *indio_dev, |
| 943 | const struct iio_chan_spec *chan, |
| 944 | enum iio_event_type type, |
| 945 | enum iio_event_direction dir) |
| 946 | { |
| 947 | struct mma8452_data *data = iio_priv(indio_dev); |
| 948 | int ret; |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 949 | const struct mma8452_event_regs *ev_regs; |
| 950 | |
| 951 | ret = mma8452_get_event_regs(data, chan, dir, &ev_regs); |
| 952 | if (ret) |
| 953 | return ret; |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 954 | |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame] | 955 | switch (dir) { |
| 956 | case IIO_EV_DIR_FALLING: |
| 957 | return mma8452_freefall_mode_enabled(data); |
| 958 | case IIO_EV_DIR_RISING: |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame] | 959 | ret = i2c_smbus_read_byte_data(data->client, |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 960 | ev_regs->ev_cfg); |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame] | 961 | if (ret < 0) |
| 962 | return ret; |
| 963 | |
Martin Kepplinger | 8b8ff3a | 2016-03-03 09:24:01 +0100 | [diff] [blame] | 964 | return !!(ret & BIT(chan->scan_index + |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 965 | ev_regs->ev_cfg_chan_shift)); |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame] | 966 | default: |
| 967 | return -EINVAL; |
| 968 | } |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | static int mma8452_write_event_config(struct iio_dev *indio_dev, |
| 972 | const struct iio_chan_spec *chan, |
| 973 | enum iio_event_type type, |
| 974 | enum iio_event_direction dir, |
| 975 | int state) |
| 976 | { |
| 977 | struct mma8452_data *data = iio_priv(indio_dev); |
Martin Kepplinger | 96c0cb2 | 2016-03-03 09:24:03 +0100 | [diff] [blame] | 978 | int val, ret; |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 979 | const struct mma8452_event_regs *ev_regs; |
| 980 | |
| 981 | ret = mma8452_get_event_regs(data, chan, dir, &ev_regs); |
| 982 | if (ret) |
| 983 | return ret; |
Martin Kepplinger | 96c0cb2 | 2016-03-03 09:24:03 +0100 | [diff] [blame] | 984 | |
| 985 | ret = mma8452_set_runtime_pm_state(data->client, state); |
| 986 | if (ret) |
| 987 | return ret; |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 988 | |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame] | 989 | switch (dir) { |
| 990 | case IIO_EV_DIR_FALLING: |
| 991 | return mma8452_set_freefall_mode(data, state); |
| 992 | case IIO_EV_DIR_RISING: |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 993 | val = i2c_smbus_read_byte_data(data->client, ev_regs->ev_cfg); |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame] | 994 | if (val < 0) |
| 995 | return val; |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 996 | |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame] | 997 | if (state) { |
| 998 | if (mma8452_freefall_mode_enabled(data)) { |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 999 | val &= ~BIT(idx_x + ev_regs->ev_cfg_chan_shift); |
| 1000 | val &= ~BIT(idx_y + ev_regs->ev_cfg_chan_shift); |
| 1001 | val &= ~BIT(idx_z + ev_regs->ev_cfg_chan_shift); |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame] | 1002 | val |= MMA8452_FF_MT_CFG_OAE; |
| 1003 | } |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 1004 | val |= BIT(chan->scan_index + |
| 1005 | ev_regs->ev_cfg_chan_shift); |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame] | 1006 | } else { |
| 1007 | if (mma8452_freefall_mode_enabled(data)) |
| 1008 | return 0; |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1009 | |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 1010 | val &= ~BIT(chan->scan_index + |
| 1011 | ev_regs->ev_cfg_chan_shift); |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame] | 1012 | } |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1013 | |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 1014 | val |= ev_regs->ev_cfg_ele; |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame] | 1015 | |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 1016 | return mma8452_change_config(data, ev_regs->ev_cfg, val); |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame] | 1017 | default: |
| 1018 | return -EINVAL; |
| 1019 | } |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | static void mma8452_transient_interrupt(struct iio_dev *indio_dev) |
| 1023 | { |
| 1024 | struct mma8452_data *data = iio_priv(indio_dev); |
Gregor Boirie | bc2b7da | 2016-03-09 19:05:49 +0100 | [diff] [blame] | 1025 | s64 ts = iio_get_time_ns(indio_dev); |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1026 | int src; |
| 1027 | |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 1028 | src = i2c_smbus_read_byte_data(data->client, MMA8452_TRANSIENT_SRC); |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1029 | if (src < 0) |
| 1030 | return; |
| 1031 | |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 1032 | if (src & MMA8452_TRANSIENT_SRC_XTRANSE) |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1033 | iio_push_event(indio_dev, |
| 1034 | IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X, |
Martin Kepplinger | c5d0db0 | 2015-07-05 19:50:18 +0200 | [diff] [blame] | 1035 | IIO_EV_TYPE_MAG, |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1036 | IIO_EV_DIR_RISING), |
| 1037 | ts); |
| 1038 | |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 1039 | if (src & MMA8452_TRANSIENT_SRC_YTRANSE) |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1040 | iio_push_event(indio_dev, |
| 1041 | IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_Y, |
Martin Kepplinger | c5d0db0 | 2015-07-05 19:50:18 +0200 | [diff] [blame] | 1042 | IIO_EV_TYPE_MAG, |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1043 | IIO_EV_DIR_RISING), |
| 1044 | ts); |
| 1045 | |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 1046 | if (src & MMA8452_TRANSIENT_SRC_ZTRANSE) |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1047 | iio_push_event(indio_dev, |
| 1048 | IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_Z, |
Martin Kepplinger | c5d0db0 | 2015-07-05 19:50:18 +0200 | [diff] [blame] | 1049 | IIO_EV_TYPE_MAG, |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1050 | IIO_EV_DIR_RISING), |
| 1051 | ts); |
| 1052 | } |
| 1053 | |
| 1054 | static irqreturn_t mma8452_interrupt(int irq, void *p) |
| 1055 | { |
| 1056 | struct iio_dev *indio_dev = p; |
| 1057 | struct mma8452_data *data = iio_priv(indio_dev); |
Martin Fuzzey | ae6d9ce | 2015-06-01 15:39:58 +0200 | [diff] [blame] | 1058 | int ret = IRQ_NONE; |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1059 | int src; |
| 1060 | |
| 1061 | src = i2c_smbus_read_byte_data(data->client, MMA8452_INT_SRC); |
| 1062 | if (src < 0) |
| 1063 | return IRQ_NONE; |
| 1064 | |
Leonard Crestez | b02ec67 | 2018-06-07 21:52:50 +0300 | [diff] [blame] | 1065 | if (!(src & (data->chip_info->enabled_events | MMA8452_INT_DRDY))) |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 1066 | return IRQ_NONE; |
| 1067 | |
Martin Fuzzey | ae6d9ce | 2015-06-01 15:39:58 +0200 | [diff] [blame] | 1068 | if (src & MMA8452_INT_DRDY) { |
| 1069 | iio_trigger_poll_chained(indio_dev->trig); |
| 1070 | ret = IRQ_HANDLED; |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1071 | } |
| 1072 | |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 1073 | if (src & MMA8452_INT_FF_MT) { |
| 1074 | if (mma8452_freefall_mode_enabled(data)) { |
| 1075 | s64 ts = iio_get_time_ns(indio_dev); |
| 1076 | |
| 1077 | iio_push_event(indio_dev, |
| 1078 | IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, |
| 1079 | IIO_MOD_X_AND_Y_AND_Z, |
| 1080 | IIO_EV_TYPE_MAG, |
| 1081 | IIO_EV_DIR_FALLING), |
| 1082 | ts); |
| 1083 | } |
| 1084 | ret = IRQ_HANDLED; |
| 1085 | } |
| 1086 | |
| 1087 | if (src & MMA8452_INT_TRANS) { |
Martin Fuzzey | ae6d9ce | 2015-06-01 15:39:58 +0200 | [diff] [blame] | 1088 | mma8452_transient_interrupt(indio_dev); |
| 1089 | ret = IRQ_HANDLED; |
| 1090 | } |
| 1091 | |
| 1092 | return ret; |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1093 | } |
| 1094 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1095 | static irqreturn_t mma8452_trigger_handler(int irq, void *p) |
| 1096 | { |
| 1097 | struct iio_poll_func *pf = p; |
| 1098 | struct iio_dev *indio_dev = pf->indio_dev; |
| 1099 | struct mma8452_data *data = iio_priv(indio_dev); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1100 | int ret; |
| 1101 | |
Jonathan Cameron | 89226a2 | 2020-07-22 16:50:38 +0100 | [diff] [blame] | 1102 | ret = mma8452_read(data, data->buffer.channels); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1103 | if (ret < 0) |
| 1104 | goto done; |
| 1105 | |
Jonathan Cameron | 89226a2 | 2020-07-22 16:50:38 +0100 | [diff] [blame] | 1106 | iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer, |
Gregor Boirie | bc2b7da | 2016-03-09 19:05:49 +0100 | [diff] [blame] | 1107 | iio_get_time_ns(indio_dev)); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1108 | |
| 1109 | done: |
| 1110 | iio_trigger_notify_done(indio_dev->trig); |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 1111 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1112 | return IRQ_HANDLED; |
| 1113 | } |
| 1114 | |
Martin Fuzzey | 2a17698c | 2015-05-13 12:26:40 +0200 | [diff] [blame] | 1115 | static int mma8452_reg_access_dbg(struct iio_dev *indio_dev, |
Harinath Nampally | f8b7b30 | 2017-09-23 16:56:30 -0400 | [diff] [blame] | 1116 | unsigned int reg, unsigned int writeval, |
| 1117 | unsigned int *readval) |
Martin Fuzzey | 2a17698c | 2015-05-13 12:26:40 +0200 | [diff] [blame] | 1118 | { |
| 1119 | int ret; |
| 1120 | struct mma8452_data *data = iio_priv(indio_dev); |
| 1121 | |
| 1122 | if (reg > MMA8452_MAX_REG) |
| 1123 | return -EINVAL; |
| 1124 | |
| 1125 | if (!readval) |
| 1126 | return mma8452_change_config(data, reg, writeval); |
| 1127 | |
| 1128 | ret = i2c_smbus_read_byte_data(data->client, reg); |
| 1129 | if (ret < 0) |
| 1130 | return ret; |
| 1131 | |
| 1132 | *readval = ret; |
| 1133 | |
| 1134 | return 0; |
| 1135 | } |
| 1136 | |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame] | 1137 | static const struct iio_event_spec mma8452_freefall_event[] = { |
| 1138 | { |
| 1139 | .type = IIO_EV_TYPE_MAG, |
| 1140 | .dir = IIO_EV_DIR_FALLING, |
| 1141 | .mask_separate = BIT(IIO_EV_INFO_ENABLE), |
| 1142 | .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) | |
| 1143 | BIT(IIO_EV_INFO_PERIOD) | |
| 1144 | BIT(IIO_EV_INFO_HIGH_PASS_FILTER_3DB) |
| 1145 | }, |
| 1146 | }; |
| 1147 | |
| 1148 | static const struct iio_event_spec mma8652_freefall_event[] = { |
| 1149 | { |
| 1150 | .type = IIO_EV_TYPE_MAG, |
| 1151 | .dir = IIO_EV_DIR_FALLING, |
| 1152 | .mask_separate = BIT(IIO_EV_INFO_ENABLE), |
| 1153 | .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) | |
| 1154 | BIT(IIO_EV_INFO_PERIOD) |
| 1155 | }, |
| 1156 | }; |
| 1157 | |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1158 | static const struct iio_event_spec mma8452_transient_event[] = { |
| 1159 | { |
Martin Kepplinger | c5d0db0 | 2015-07-05 19:50:18 +0200 | [diff] [blame] | 1160 | .type = IIO_EV_TYPE_MAG, |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1161 | .dir = IIO_EV_DIR_RISING, |
| 1162 | .mask_separate = BIT(IIO_EV_INFO_ENABLE), |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 1163 | .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) | |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 1164 | BIT(IIO_EV_INFO_PERIOD) | |
| 1165 | BIT(IIO_EV_INFO_HIGH_PASS_FILTER_3DB) |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1166 | }, |
| 1167 | }; |
| 1168 | |
Martin Kepplinger | 60f562e | 2015-09-01 13:45:10 +0200 | [diff] [blame] | 1169 | static const struct iio_event_spec mma8452_motion_event[] = { |
| 1170 | { |
| 1171 | .type = IIO_EV_TYPE_MAG, |
| 1172 | .dir = IIO_EV_DIR_RISING, |
| 1173 | .mask_separate = BIT(IIO_EV_INFO_ENABLE), |
| 1174 | .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) | |
| 1175 | BIT(IIO_EV_INFO_PERIOD) |
| 1176 | }, |
| 1177 | }; |
| 1178 | |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1179 | /* |
| 1180 | * Threshold is configured in fixed 8G/127 steps regardless of |
| 1181 | * currently selected scale for measurement. |
| 1182 | */ |
| 1183 | static IIO_CONST_ATTR_NAMED(accel_transient_scale, in_accel_scale, "0.617742"); |
| 1184 | |
| 1185 | static struct attribute *mma8452_event_attributes[] = { |
| 1186 | &iio_const_attr_accel_transient_scale.dev_attr.attr, |
| 1187 | NULL, |
| 1188 | }; |
| 1189 | |
Rikard Falkeborn | 681ab2c | 2020-10-01 01:29:39 +0200 | [diff] [blame] | 1190 | static const struct attribute_group mma8452_event_attribute_group = { |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1191 | .attrs = mma8452_event_attributes, |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1192 | }; |
| 1193 | |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame] | 1194 | #define MMA8452_FREEFALL_CHANNEL(modifier) { \ |
| 1195 | .type = IIO_ACCEL, \ |
| 1196 | .modified = 1, \ |
| 1197 | .channel2 = modifier, \ |
| 1198 | .scan_index = -1, \ |
| 1199 | .event_spec = mma8452_freefall_event, \ |
| 1200 | .num_event_specs = ARRAY_SIZE(mma8452_freefall_event), \ |
| 1201 | } |
| 1202 | |
| 1203 | #define MMA8652_FREEFALL_CHANNEL(modifier) { \ |
| 1204 | .type = IIO_ACCEL, \ |
| 1205 | .modified = 1, \ |
| 1206 | .channel2 = modifier, \ |
| 1207 | .scan_index = -1, \ |
| 1208 | .event_spec = mma8652_freefall_event, \ |
| 1209 | .num_event_specs = ARRAY_SIZE(mma8652_freefall_event), \ |
| 1210 | } |
| 1211 | |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 1212 | #define MMA8452_CHANNEL(axis, idx, bits) { \ |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1213 | .type = IIO_ACCEL, \ |
| 1214 | .modified = 1, \ |
| 1215 | .channel2 = IIO_MOD_##axis, \ |
| 1216 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 1217 | BIT(IIO_CHAN_INFO_CALIBBIAS), \ |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1218 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \ |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 1219 | BIT(IIO_CHAN_INFO_SCALE) | \ |
Martin Kepplinger | ed859fc | 2016-04-25 14:08:25 +0200 | [diff] [blame] | 1220 | BIT(IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY) | \ |
| 1221 | BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \ |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1222 | .scan_index = idx, \ |
| 1223 | .scan_type = { \ |
| 1224 | .sign = 's', \ |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 1225 | .realbits = (bits), \ |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1226 | .storagebits = 16, \ |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 1227 | .shift = 16 - (bits), \ |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1228 | .endianness = IIO_BE, \ |
| 1229 | }, \ |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1230 | .event_spec = mma8452_transient_event, \ |
| 1231 | .num_event_specs = ARRAY_SIZE(mma8452_transient_event), \ |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
Martin Kepplinger | 417e008 | 2015-09-01 13:45:11 +0200 | [diff] [blame] | 1234 | #define MMA8652_CHANNEL(axis, idx, bits) { \ |
| 1235 | .type = IIO_ACCEL, \ |
| 1236 | .modified = 1, \ |
| 1237 | .channel2 = IIO_MOD_##axis, \ |
| 1238 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
| 1239 | BIT(IIO_CHAN_INFO_CALIBBIAS), \ |
| 1240 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \ |
Martin Kepplinger | ed859fc | 2016-04-25 14:08:25 +0200 | [diff] [blame] | 1241 | BIT(IIO_CHAN_INFO_SCALE) | \ |
| 1242 | BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \ |
Martin Kepplinger | 417e008 | 2015-09-01 13:45:11 +0200 | [diff] [blame] | 1243 | .scan_index = idx, \ |
| 1244 | .scan_type = { \ |
| 1245 | .sign = 's', \ |
| 1246 | .realbits = (bits), \ |
| 1247 | .storagebits = 16, \ |
| 1248 | .shift = 16 - (bits), \ |
| 1249 | .endianness = IIO_BE, \ |
| 1250 | }, \ |
| 1251 | .event_spec = mma8452_motion_event, \ |
| 1252 | .num_event_specs = ARRAY_SIZE(mma8452_motion_event), \ |
| 1253 | } |
| 1254 | |
Martin Kepplinger | 244a93f | 2016-01-16 15:35:22 +0100 | [diff] [blame] | 1255 | static const struct iio_chan_spec mma8451_channels[] = { |
| 1256 | MMA8452_CHANNEL(X, idx_x, 14), |
| 1257 | MMA8452_CHANNEL(Y, idx_y, 14), |
| 1258 | MMA8452_CHANNEL(Z, idx_z, 14), |
| 1259 | IIO_CHAN_SOFT_TIMESTAMP(idx_ts), |
| 1260 | MMA8452_FREEFALL_CHANNEL(IIO_MOD_X_AND_Y_AND_Z), |
| 1261 | }; |
| 1262 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1263 | static const struct iio_chan_spec mma8452_channels[] = { |
Martin Kepplinger | e60378c | 2015-12-15 17:45:00 +0100 | [diff] [blame] | 1264 | MMA8452_CHANNEL(X, idx_x, 12), |
| 1265 | MMA8452_CHANNEL(Y, idx_y, 12), |
| 1266 | MMA8452_CHANNEL(Z, idx_z, 12), |
| 1267 | IIO_CHAN_SOFT_TIMESTAMP(idx_ts), |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame] | 1268 | MMA8452_FREEFALL_CHANNEL(IIO_MOD_X_AND_Y_AND_Z), |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1269 | }; |
| 1270 | |
Martin Kepplinger | c5ea1b58 | 2015-09-01 13:45:09 +0200 | [diff] [blame] | 1271 | static const struct iio_chan_spec mma8453_channels[] = { |
Martin Kepplinger | e60378c | 2015-12-15 17:45:00 +0100 | [diff] [blame] | 1272 | MMA8452_CHANNEL(X, idx_x, 10), |
| 1273 | MMA8452_CHANNEL(Y, idx_y, 10), |
| 1274 | MMA8452_CHANNEL(Z, idx_z, 10), |
| 1275 | IIO_CHAN_SOFT_TIMESTAMP(idx_ts), |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame] | 1276 | MMA8452_FREEFALL_CHANNEL(IIO_MOD_X_AND_Y_AND_Z), |
Martin Kepplinger | c5ea1b58 | 2015-09-01 13:45:09 +0200 | [diff] [blame] | 1277 | }; |
| 1278 | |
Martin Kepplinger | 417e008 | 2015-09-01 13:45:11 +0200 | [diff] [blame] | 1279 | static const struct iio_chan_spec mma8652_channels[] = { |
Martin Kepplinger | e60378c | 2015-12-15 17:45:00 +0100 | [diff] [blame] | 1280 | MMA8652_CHANNEL(X, idx_x, 12), |
| 1281 | MMA8652_CHANNEL(Y, idx_y, 12), |
| 1282 | MMA8652_CHANNEL(Z, idx_z, 12), |
| 1283 | IIO_CHAN_SOFT_TIMESTAMP(idx_ts), |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame] | 1284 | MMA8652_FREEFALL_CHANNEL(IIO_MOD_X_AND_Y_AND_Z), |
Martin Kepplinger | 417e008 | 2015-09-01 13:45:11 +0200 | [diff] [blame] | 1285 | }; |
| 1286 | |
| 1287 | static const struct iio_chan_spec mma8653_channels[] = { |
Martin Kepplinger | e60378c | 2015-12-15 17:45:00 +0100 | [diff] [blame] | 1288 | MMA8652_CHANNEL(X, idx_x, 10), |
| 1289 | MMA8652_CHANNEL(Y, idx_y, 10), |
| 1290 | MMA8652_CHANNEL(Z, idx_z, 10), |
| 1291 | IIO_CHAN_SOFT_TIMESTAMP(idx_ts), |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame] | 1292 | MMA8652_FREEFALL_CHANNEL(IIO_MOD_X_AND_Y_AND_Z), |
Martin Kepplinger | 417e008 | 2015-09-01 13:45:11 +0200 | [diff] [blame] | 1293 | }; |
| 1294 | |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 1295 | enum { |
Martin Kepplinger | 244a93f | 2016-01-16 15:35:22 +0100 | [diff] [blame] | 1296 | mma8451, |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 1297 | mma8452, |
Martin Kepplinger | c5ea1b58 | 2015-09-01 13:45:09 +0200 | [diff] [blame] | 1298 | mma8453, |
Martin Kepplinger | 417e008 | 2015-09-01 13:45:11 +0200 | [diff] [blame] | 1299 | mma8652, |
| 1300 | mma8653, |
Martin Kepplinger | e873118 | 2016-03-09 12:01:29 +0100 | [diff] [blame] | 1301 | fxls8471, |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 1302 | }; |
| 1303 | |
| 1304 | static const struct mma_chip_info mma_chip_info_table[] = { |
Martin Kepplinger | 244a93f | 2016-01-16 15:35:22 +0100 | [diff] [blame] | 1305 | [mma8451] = { |
| 1306 | .chip_id = MMA8451_DEVICE_ID, |
| 1307 | .channels = mma8451_channels, |
| 1308 | .num_channels = ARRAY_SIZE(mma8451_channels), |
| 1309 | /* |
| 1310 | * Hardware has fullscale of -2G, -4G, -8G corresponding to |
| 1311 | * raw value -8192 for 14 bit, -2048 for 12 bit or -512 for 10 |
| 1312 | * bit. |
| 1313 | * The userspace interface uses m/s^2 and we declare micro units |
| 1314 | * So scale factor for 12 bit here is given by: |
Martin Kepplinger | 8b8ff3a | 2016-03-03 09:24:01 +0100 | [diff] [blame] | 1315 | * g * N * 1000000 / 2048 for N = 2, 4, 8 and g=9.80665 |
Martin Kepplinger | 244a93f | 2016-01-16 15:35:22 +0100 | [diff] [blame] | 1316 | */ |
| 1317 | .mma_scales = { {0, 2394}, {0, 4788}, {0, 9577} }, |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 1318 | /* |
| 1319 | * Although we enable the interrupt sources once and for |
| 1320 | * all here the event detection itself is not enabled until |
| 1321 | * userspace asks for it by mma8452_write_event_config() |
| 1322 | */ |
| 1323 | .all_events = MMA8452_INT_DRDY | |
| 1324 | MMA8452_INT_TRANS | |
| 1325 | MMA8452_INT_FF_MT, |
| 1326 | .enabled_events = MMA8452_INT_TRANS | |
| 1327 | MMA8452_INT_FF_MT, |
Martin Kepplinger | 244a93f | 2016-01-16 15:35:22 +0100 | [diff] [blame] | 1328 | }, |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 1329 | [mma8452] = { |
| 1330 | .chip_id = MMA8452_DEVICE_ID, |
| 1331 | .channels = mma8452_channels, |
| 1332 | .num_channels = ARRAY_SIZE(mma8452_channels), |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 1333 | .mma_scales = { {0, 9577}, {0, 19154}, {0, 38307} }, |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 1334 | /* |
| 1335 | * Although we enable the interrupt sources once and for |
| 1336 | * all here the event detection itself is not enabled until |
| 1337 | * userspace asks for it by mma8452_write_event_config() |
| 1338 | */ |
| 1339 | .all_events = MMA8452_INT_DRDY | |
| 1340 | MMA8452_INT_TRANS | |
| 1341 | MMA8452_INT_FF_MT, |
| 1342 | .enabled_events = MMA8452_INT_TRANS | |
| 1343 | MMA8452_INT_FF_MT, |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 1344 | }, |
Martin Kepplinger | c5ea1b58 | 2015-09-01 13:45:09 +0200 | [diff] [blame] | 1345 | [mma8453] = { |
| 1346 | .chip_id = MMA8453_DEVICE_ID, |
| 1347 | .channels = mma8453_channels, |
| 1348 | .num_channels = ARRAY_SIZE(mma8453_channels), |
| 1349 | .mma_scales = { {0, 38307}, {0, 76614}, {0, 153228} }, |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 1350 | /* |
| 1351 | * Although we enable the interrupt sources once and for |
| 1352 | * all here the event detection itself is not enabled until |
| 1353 | * userspace asks for it by mma8452_write_event_config() |
| 1354 | */ |
| 1355 | .all_events = MMA8452_INT_DRDY | |
| 1356 | MMA8452_INT_TRANS | |
| 1357 | MMA8452_INT_FF_MT, |
| 1358 | .enabled_events = MMA8452_INT_TRANS | |
| 1359 | MMA8452_INT_FF_MT, |
Martin Kepplinger | c5ea1b58 | 2015-09-01 13:45:09 +0200 | [diff] [blame] | 1360 | }, |
Martin Kepplinger | 417e008 | 2015-09-01 13:45:11 +0200 | [diff] [blame] | 1361 | [mma8652] = { |
| 1362 | .chip_id = MMA8652_DEVICE_ID, |
| 1363 | .channels = mma8652_channels, |
| 1364 | .num_channels = ARRAY_SIZE(mma8652_channels), |
| 1365 | .mma_scales = { {0, 9577}, {0, 19154}, {0, 38307} }, |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 1366 | .all_events = MMA8452_INT_DRDY | |
| 1367 | MMA8452_INT_FF_MT, |
| 1368 | .enabled_events = MMA8452_INT_FF_MT, |
Martin Kepplinger | 417e008 | 2015-09-01 13:45:11 +0200 | [diff] [blame] | 1369 | }, |
| 1370 | [mma8653] = { |
| 1371 | .chip_id = MMA8653_DEVICE_ID, |
| 1372 | .channels = mma8653_channels, |
| 1373 | .num_channels = ARRAY_SIZE(mma8653_channels), |
| 1374 | .mma_scales = { {0, 38307}, {0, 76614}, {0, 153228} }, |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 1375 | /* |
| 1376 | * Although we enable the interrupt sources once and for |
| 1377 | * all here the event detection itself is not enabled until |
| 1378 | * userspace asks for it by mma8452_write_event_config() |
| 1379 | */ |
| 1380 | .all_events = MMA8452_INT_DRDY | |
| 1381 | MMA8452_INT_FF_MT, |
| 1382 | .enabled_events = MMA8452_INT_FF_MT, |
Martin Kepplinger | 417e008 | 2015-09-01 13:45:11 +0200 | [diff] [blame] | 1383 | }, |
Martin Kepplinger | e873118 | 2016-03-09 12:01:29 +0100 | [diff] [blame] | 1384 | [fxls8471] = { |
| 1385 | .chip_id = FXLS8471_DEVICE_ID, |
| 1386 | .channels = mma8451_channels, |
| 1387 | .num_channels = ARRAY_SIZE(mma8451_channels), |
| 1388 | .mma_scales = { {0, 2394}, {0, 4788}, {0, 9577} }, |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 1389 | /* |
| 1390 | * Although we enable the interrupt sources once and for |
| 1391 | * all here the event detection itself is not enabled until |
| 1392 | * userspace asks for it by mma8452_write_event_config() |
| 1393 | */ |
| 1394 | .all_events = MMA8452_INT_DRDY | |
| 1395 | MMA8452_INT_TRANS | |
| 1396 | MMA8452_INT_FF_MT, |
| 1397 | .enabled_events = MMA8452_INT_TRANS | |
| 1398 | MMA8452_INT_FF_MT, |
Martin Kepplinger | e873118 | 2016-03-09 12:01:29 +0100 | [diff] [blame] | 1399 | }, |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 1400 | }; |
| 1401 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1402 | static struct attribute *mma8452_attributes[] = { |
| 1403 | &iio_dev_attr_sampling_frequency_available.dev_attr.attr, |
| 1404 | &iio_dev_attr_in_accel_scale_available.dev_attr.attr, |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 1405 | &iio_dev_attr_in_accel_filter_high_pass_3db_frequency_available.dev_attr.attr, |
Martin Kepplinger | ed859fc | 2016-04-25 14:08:25 +0200 | [diff] [blame] | 1406 | &iio_dev_attr_in_accel_oversampling_ratio_available.dev_attr.attr, |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1407 | NULL |
| 1408 | }; |
| 1409 | |
| 1410 | static const struct attribute_group mma8452_group = { |
| 1411 | .attrs = mma8452_attributes, |
| 1412 | }; |
| 1413 | |
| 1414 | static const struct iio_info mma8452_info = { |
| 1415 | .attrs = &mma8452_group, |
| 1416 | .read_raw = &mma8452_read_raw, |
| 1417 | .write_raw = &mma8452_write_raw, |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1418 | .event_attrs = &mma8452_event_attribute_group, |
Harinath Nampally | 4febd9f | 2017-09-25 06:40:08 -0400 | [diff] [blame] | 1419 | .read_event_value = &mma8452_read_event_value, |
| 1420 | .write_event_value = &mma8452_write_event_value, |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1421 | .read_event_config = &mma8452_read_event_config, |
| 1422 | .write_event_config = &mma8452_write_event_config, |
Martin Fuzzey | 2a17698c | 2015-05-13 12:26:40 +0200 | [diff] [blame] | 1423 | .debugfs_reg_access = &mma8452_reg_access_dbg, |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1424 | }; |
| 1425 | |
| 1426 | static const unsigned long mma8452_scan_masks[] = {0x7, 0}; |
| 1427 | |
Martin Fuzzey | ae6d9ce | 2015-06-01 15:39:58 +0200 | [diff] [blame] | 1428 | static int mma8452_data_rdy_trigger_set_state(struct iio_trigger *trig, |
| 1429 | bool state) |
| 1430 | { |
| 1431 | struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); |
| 1432 | struct mma8452_data *data = iio_priv(indio_dev); |
Martin Kepplinger | 96c0cb2 | 2016-03-03 09:24:03 +0100 | [diff] [blame] | 1433 | int reg, ret; |
| 1434 | |
| 1435 | ret = mma8452_set_runtime_pm_state(data->client, state); |
| 1436 | if (ret) |
| 1437 | return ret; |
Martin Fuzzey | ae6d9ce | 2015-06-01 15:39:58 +0200 | [diff] [blame] | 1438 | |
| 1439 | reg = i2c_smbus_read_byte_data(data->client, MMA8452_CTRL_REG4); |
| 1440 | if (reg < 0) |
| 1441 | return reg; |
| 1442 | |
| 1443 | if (state) |
| 1444 | reg |= MMA8452_INT_DRDY; |
| 1445 | else |
| 1446 | reg &= ~MMA8452_INT_DRDY; |
| 1447 | |
| 1448 | return mma8452_change_config(data, MMA8452_CTRL_REG4, reg); |
| 1449 | } |
| 1450 | |
Martin Fuzzey | ae6d9ce | 2015-06-01 15:39:58 +0200 | [diff] [blame] | 1451 | static const struct iio_trigger_ops mma8452_trigger_ops = { |
| 1452 | .set_trigger_state = mma8452_data_rdy_trigger_set_state, |
Lars-Peter Clausen | 19808e0 | 2016-09-23 17:19:42 +0200 | [diff] [blame] | 1453 | .validate_device = iio_trigger_validate_own_device, |
Martin Fuzzey | ae6d9ce | 2015-06-01 15:39:58 +0200 | [diff] [blame] | 1454 | }; |
| 1455 | |
| 1456 | static int mma8452_trigger_setup(struct iio_dev *indio_dev) |
| 1457 | { |
| 1458 | struct mma8452_data *data = iio_priv(indio_dev); |
| 1459 | struct iio_trigger *trig; |
| 1460 | int ret; |
| 1461 | |
| 1462 | trig = devm_iio_trigger_alloc(&data->client->dev, "%s-dev%d", |
| 1463 | indio_dev->name, |
| 1464 | indio_dev->id); |
| 1465 | if (!trig) |
| 1466 | return -ENOMEM; |
| 1467 | |
Martin Fuzzey | ae6d9ce | 2015-06-01 15:39:58 +0200 | [diff] [blame] | 1468 | trig->ops = &mma8452_trigger_ops; |
| 1469 | iio_trigger_set_drvdata(trig, indio_dev); |
| 1470 | |
| 1471 | ret = iio_trigger_register(trig); |
| 1472 | if (ret) |
| 1473 | return ret; |
| 1474 | |
| 1475 | indio_dev->trig = trig; |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 1476 | |
Martin Fuzzey | ae6d9ce | 2015-06-01 15:39:58 +0200 | [diff] [blame] | 1477 | return 0; |
| 1478 | } |
| 1479 | |
| 1480 | static void mma8452_trigger_cleanup(struct iio_dev *indio_dev) |
| 1481 | { |
| 1482 | if (indio_dev->trig) |
| 1483 | iio_trigger_unregister(indio_dev->trig); |
| 1484 | } |
| 1485 | |
Martin Fuzzey | ecabae7 | 2015-05-13 12:26:38 +0200 | [diff] [blame] | 1486 | static int mma8452_reset(struct i2c_client *client) |
| 1487 | { |
| 1488 | int i; |
| 1489 | int ret; |
| 1490 | |
| 1491 | ret = i2c_smbus_write_byte_data(client, MMA8452_CTRL_REG2, |
| 1492 | MMA8452_CTRL_REG2_RST); |
| 1493 | if (ret < 0) |
| 1494 | return ret; |
| 1495 | |
| 1496 | for (i = 0; i < 10; i++) { |
| 1497 | usleep_range(100, 200); |
| 1498 | ret = i2c_smbus_read_byte_data(client, MMA8452_CTRL_REG2); |
| 1499 | if (ret == -EIO) |
| 1500 | continue; /* I2C comm reset */ |
| 1501 | if (ret < 0) |
| 1502 | return ret; |
| 1503 | if (!(ret & MMA8452_CTRL_REG2_RST)) |
| 1504 | return 0; |
| 1505 | } |
| 1506 | |
| 1507 | return -ETIMEDOUT; |
| 1508 | } |
| 1509 | |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 1510 | static const struct of_device_id mma8452_dt_ids[] = { |
Martin Kepplinger | 244a93f | 2016-01-16 15:35:22 +0100 | [diff] [blame] | 1511 | { .compatible = "fsl,mma8451", .data = &mma_chip_info_table[mma8451] }, |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 1512 | { .compatible = "fsl,mma8452", .data = &mma_chip_info_table[mma8452] }, |
Martin Kepplinger | c5ea1b58 | 2015-09-01 13:45:09 +0200 | [diff] [blame] | 1513 | { .compatible = "fsl,mma8453", .data = &mma_chip_info_table[mma8453] }, |
Martin Kepplinger | 417e008 | 2015-09-01 13:45:11 +0200 | [diff] [blame] | 1514 | { .compatible = "fsl,mma8652", .data = &mma_chip_info_table[mma8652] }, |
| 1515 | { .compatible = "fsl,mma8653", .data = &mma_chip_info_table[mma8653] }, |
Martin Kepplinger | e873118 | 2016-03-09 12:01:29 +0100 | [diff] [blame] | 1516 | { .compatible = "fsl,fxls8471", .data = &mma_chip_info_table[fxls8471] }, |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 1517 | { } |
| 1518 | }; |
| 1519 | MODULE_DEVICE_TABLE(of, mma8452_dt_ids); |
| 1520 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1521 | static int mma8452_probe(struct i2c_client *client, |
| 1522 | const struct i2c_device_id *id) |
| 1523 | { |
| 1524 | struct mma8452_data *data; |
| 1525 | struct iio_dev *indio_dev; |
| 1526 | int ret; |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 1527 | const struct of_device_id *match; |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1528 | |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 1529 | match = of_match_device(mma8452_dt_ids, &client->dev); |
| 1530 | if (!match) { |
| 1531 | dev_err(&client->dev, "unknown device model\n"); |
| 1532 | return -ENODEV; |
| 1533 | } |
| 1534 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1535 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 1536 | if (!indio_dev) |
| 1537 | return -ENOMEM; |
| 1538 | |
| 1539 | data = iio_priv(indio_dev); |
| 1540 | data->client = client; |
| 1541 | mutex_init(&data->lock); |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 1542 | data->chip_info = match->data; |
| 1543 | |
Anson Huang | f6ff49b | 2019-01-08 09:14:06 +0000 | [diff] [blame] | 1544 | data->vdd_reg = devm_regulator_get(&client->dev, "vdd"); |
Krzysztof Kozlowski | c8b9a02 | 2020-08-29 08:47:10 +0200 | [diff] [blame] | 1545 | if (IS_ERR(data->vdd_reg)) |
| 1546 | return dev_err_probe(&client->dev, PTR_ERR(data->vdd_reg), |
| 1547 | "failed to get VDD regulator!\n"); |
Anson Huang | f6ff49b | 2019-01-08 09:14:06 +0000 | [diff] [blame] | 1548 | |
| 1549 | data->vddio_reg = devm_regulator_get(&client->dev, "vddio"); |
Krzysztof Kozlowski | c8b9a02 | 2020-08-29 08:47:10 +0200 | [diff] [blame] | 1550 | if (IS_ERR(data->vddio_reg)) |
| 1551 | return dev_err_probe(&client->dev, PTR_ERR(data->vddio_reg), |
| 1552 | "failed to get VDDIO regulator!\n"); |
Anson Huang | f6ff49b | 2019-01-08 09:14:06 +0000 | [diff] [blame] | 1553 | |
| 1554 | ret = regulator_enable(data->vdd_reg); |
| 1555 | if (ret) { |
| 1556 | dev_err(&client->dev, "failed to enable VDD regulator!\n"); |
| 1557 | return ret; |
| 1558 | } |
| 1559 | |
| 1560 | ret = regulator_enable(data->vddio_reg); |
| 1561 | if (ret) { |
| 1562 | dev_err(&client->dev, "failed to enable VDDIO regulator!\n"); |
| 1563 | goto disable_regulator_vdd; |
| 1564 | } |
| 1565 | |
Martin Kepplinger | 417e008 | 2015-09-01 13:45:11 +0200 | [diff] [blame] | 1566 | ret = i2c_smbus_read_byte_data(client, MMA8452_WHO_AM_I); |
| 1567 | if (ret < 0) |
Anson Huang | f6ff49b | 2019-01-08 09:14:06 +0000 | [diff] [blame] | 1568 | goto disable_regulators; |
Martin Kepplinger | 417e008 | 2015-09-01 13:45:11 +0200 | [diff] [blame] | 1569 | |
| 1570 | switch (ret) { |
Martin Kepplinger | 244a93f | 2016-01-16 15:35:22 +0100 | [diff] [blame] | 1571 | case MMA8451_DEVICE_ID: |
Martin Kepplinger | 417e008 | 2015-09-01 13:45:11 +0200 | [diff] [blame] | 1572 | case MMA8452_DEVICE_ID: |
| 1573 | case MMA8453_DEVICE_ID: |
| 1574 | case MMA8652_DEVICE_ID: |
| 1575 | case MMA8653_DEVICE_ID: |
Martin Kepplinger | e873118 | 2016-03-09 12:01:29 +0100 | [diff] [blame] | 1576 | case FXLS8471_DEVICE_ID: |
Martin Kepplinger | 417e008 | 2015-09-01 13:45:11 +0200 | [diff] [blame] | 1577 | if (ret == data->chip_info->chip_id) |
| 1578 | break; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 1579 | fallthrough; |
Martin Kepplinger | 417e008 | 2015-09-01 13:45:11 +0200 | [diff] [blame] | 1580 | default: |
Anson Huang | f6ff49b | 2019-01-08 09:14:06 +0000 | [diff] [blame] | 1581 | ret = -ENODEV; |
| 1582 | goto disable_regulators; |
Martin Kepplinger | 417e008 | 2015-09-01 13:45:11 +0200 | [diff] [blame] | 1583 | } |
| 1584 | |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 1585 | dev_info(&client->dev, "registering %s accelerometer; ID 0x%x\n", |
| 1586 | match->compatible, data->chip_info->chip_id); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1587 | |
| 1588 | i2c_set_clientdata(client, indio_dev); |
| 1589 | indio_dev->info = &mma8452_info; |
| 1590 | indio_dev->name = id->name; |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1591 | indio_dev->modes = INDIO_DIRECT_MODE; |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 1592 | indio_dev->channels = data->chip_info->channels; |
| 1593 | indio_dev->num_channels = data->chip_info->num_channels; |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1594 | indio_dev->available_scan_masks = mma8452_scan_masks; |
| 1595 | |
Martin Fuzzey | ecabae7 | 2015-05-13 12:26:38 +0200 | [diff] [blame] | 1596 | ret = mma8452_reset(client); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1597 | if (ret < 0) |
Anson Huang | f6ff49b | 2019-01-08 09:14:06 +0000 | [diff] [blame] | 1598 | goto disable_regulators; |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1599 | |
| 1600 | data->data_cfg = MMA8452_DATA_CFG_FS_2G; |
| 1601 | ret = i2c_smbus_write_byte_data(client, MMA8452_DATA_CFG, |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 1602 | data->data_cfg); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1603 | if (ret < 0) |
Anson Huang | f6ff49b | 2019-01-08 09:14:06 +0000 | [diff] [blame] | 1604 | goto disable_regulators; |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1605 | |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1606 | /* |
| 1607 | * By default set transient threshold to max to avoid events if |
| 1608 | * enabling without configuring threshold. |
| 1609 | */ |
| 1610 | ret = i2c_smbus_write_byte_data(client, MMA8452_TRANSIENT_THS, |
| 1611 | MMA8452_TRANSIENT_THS_MASK); |
| 1612 | if (ret < 0) |
Anson Huang | f6ff49b | 2019-01-08 09:14:06 +0000 | [diff] [blame] | 1613 | goto disable_regulators; |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1614 | |
| 1615 | if (client->irq) { |
Martin Kepplinger | d2a3e09 | 2015-10-15 15:10:32 +0200 | [diff] [blame] | 1616 | int irq2; |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1617 | |
Martin Kepplinger | d2a3e09 | 2015-10-15 15:10:32 +0200 | [diff] [blame] | 1618 | irq2 = of_irq_get_byname(client->dev.of_node, "INT2"); |
| 1619 | |
| 1620 | if (irq2 == client->irq) { |
| 1621 | dev_dbg(&client->dev, "using interrupt line INT2\n"); |
| 1622 | } else { |
| 1623 | ret = i2c_smbus_write_byte_data(client, |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 1624 | MMA8452_CTRL_REG5, |
| 1625 | data->chip_info->all_events); |
Martin Kepplinger | d2a3e09 | 2015-10-15 15:10:32 +0200 | [diff] [blame] | 1626 | if (ret < 0) |
Anson Huang | f6ff49b | 2019-01-08 09:14:06 +0000 | [diff] [blame] | 1627 | goto disable_regulators; |
Martin Kepplinger | d2a3e09 | 2015-10-15 15:10:32 +0200 | [diff] [blame] | 1628 | |
| 1629 | dev_dbg(&client->dev, "using interrupt line INT1\n"); |
| 1630 | } |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1631 | |
| 1632 | ret = i2c_smbus_write_byte_data(client, |
Harinath Nampally | 605f72d | 2017-09-09 15:56:58 -0400 | [diff] [blame] | 1633 | MMA8452_CTRL_REG4, |
| 1634 | data->chip_info->enabled_events); |
Martin Fuzzey | ae6d9ce | 2015-06-01 15:39:58 +0200 | [diff] [blame] | 1635 | if (ret < 0) |
Anson Huang | f6ff49b | 2019-01-08 09:14:06 +0000 | [diff] [blame] | 1636 | goto disable_regulators; |
Martin Fuzzey | ae6d9ce | 2015-06-01 15:39:58 +0200 | [diff] [blame] | 1637 | |
| 1638 | ret = mma8452_trigger_setup(indio_dev); |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1639 | if (ret < 0) |
Anson Huang | f6ff49b | 2019-01-08 09:14:06 +0000 | [diff] [blame] | 1640 | goto disable_regulators; |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1641 | } |
| 1642 | |
Martin Fuzzey | ecabae7 | 2015-05-13 12:26:38 +0200 | [diff] [blame] | 1643 | data->ctrl_reg1 = MMA8452_CTRL_ACTIVE | |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 1644 | (MMA8452_CTRL_DR_DEFAULT << MMA8452_CTRL_DR_SHIFT); |
Richard Tresidder | a45d123 | 2018-05-11 16:54:59 +0800 | [diff] [blame] | 1645 | |
| 1646 | data->sleep_val = mma8452_calculate_sleep(data); |
| 1647 | |
Martin Fuzzey | ecabae7 | 2015-05-13 12:26:38 +0200 | [diff] [blame] | 1648 | ret = i2c_smbus_write_byte_data(client, MMA8452_CTRL_REG1, |
| 1649 | data->ctrl_reg1); |
| 1650 | if (ret < 0) |
Martin Fuzzey | ae6d9ce | 2015-06-01 15:39:58 +0200 | [diff] [blame] | 1651 | goto trigger_cleanup; |
Martin Fuzzey | ecabae7 | 2015-05-13 12:26:38 +0200 | [diff] [blame] | 1652 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1653 | ret = iio_triggered_buffer_setup(indio_dev, NULL, |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 1654 | mma8452_trigger_handler, NULL); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1655 | if (ret < 0) |
Martin Fuzzey | ae6d9ce | 2015-06-01 15:39:58 +0200 | [diff] [blame] | 1656 | goto trigger_cleanup; |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1657 | |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1658 | if (client->irq) { |
| 1659 | ret = devm_request_threaded_irq(&client->dev, |
| 1660 | client->irq, |
| 1661 | NULL, mma8452_interrupt, |
| 1662 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, |
| 1663 | client->name, indio_dev); |
| 1664 | if (ret) |
| 1665 | goto buffer_cleanup; |
| 1666 | } |
| 1667 | |
Martin Kepplinger | 96c0cb2 | 2016-03-03 09:24:03 +0100 | [diff] [blame] | 1668 | ret = pm_runtime_set_active(&client->dev); |
| 1669 | if (ret < 0) |
| 1670 | goto buffer_cleanup; |
| 1671 | |
| 1672 | pm_runtime_enable(&client->dev); |
| 1673 | pm_runtime_set_autosuspend_delay(&client->dev, |
| 1674 | MMA8452_AUTO_SUSPEND_DELAY_MS); |
| 1675 | pm_runtime_use_autosuspend(&client->dev); |
| 1676 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1677 | ret = iio_device_register(indio_dev); |
| 1678 | if (ret < 0) |
| 1679 | goto buffer_cleanup; |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1680 | |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame] | 1681 | ret = mma8452_set_freefall_mode(data, false); |
Bijosh Thykkoottathil | 1a965d4 | 2016-07-04 10:08:53 +0000 | [diff] [blame] | 1682 | if (ret < 0) |
Chuhong Yuan | d7369ae | 2020-05-28 14:41:21 +0800 | [diff] [blame] | 1683 | goto unregister_device; |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame] | 1684 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1685 | return 0; |
| 1686 | |
Chuhong Yuan | d7369ae | 2020-05-28 14:41:21 +0800 | [diff] [blame] | 1687 | unregister_device: |
| 1688 | iio_device_unregister(indio_dev); |
| 1689 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1690 | buffer_cleanup: |
| 1691 | iio_triggered_buffer_cleanup(indio_dev); |
Martin Fuzzey | ae6d9ce | 2015-06-01 15:39:58 +0200 | [diff] [blame] | 1692 | |
| 1693 | trigger_cleanup: |
| 1694 | mma8452_trigger_cleanup(indio_dev); |
| 1695 | |
Anson Huang | f6ff49b | 2019-01-08 09:14:06 +0000 | [diff] [blame] | 1696 | disable_regulators: |
| 1697 | regulator_disable(data->vddio_reg); |
| 1698 | |
| 1699 | disable_regulator_vdd: |
| 1700 | regulator_disable(data->vdd_reg); |
| 1701 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1702 | return ret; |
| 1703 | } |
| 1704 | |
| 1705 | static int mma8452_remove(struct i2c_client *client) |
| 1706 | { |
| 1707 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
Anson Huang | f6ff49b | 2019-01-08 09:14:06 +0000 | [diff] [blame] | 1708 | struct mma8452_data *data = iio_priv(indio_dev); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1709 | |
| 1710 | iio_device_unregister(indio_dev); |
Martin Kepplinger | 96c0cb2 | 2016-03-03 09:24:03 +0100 | [diff] [blame] | 1711 | |
| 1712 | pm_runtime_disable(&client->dev); |
| 1713 | pm_runtime_set_suspended(&client->dev); |
| 1714 | pm_runtime_put_noidle(&client->dev); |
| 1715 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1716 | iio_triggered_buffer_cleanup(indio_dev); |
Martin Fuzzey | ae6d9ce | 2015-06-01 15:39:58 +0200 | [diff] [blame] | 1717 | mma8452_trigger_cleanup(indio_dev); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1718 | mma8452_standby(iio_priv(indio_dev)); |
| 1719 | |
Anson Huang | f6ff49b | 2019-01-08 09:14:06 +0000 | [diff] [blame] | 1720 | regulator_disable(data->vddio_reg); |
| 1721 | regulator_disable(data->vdd_reg); |
| 1722 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1723 | return 0; |
| 1724 | } |
| 1725 | |
Martin Kepplinger | 96c0cb2 | 2016-03-03 09:24:03 +0100 | [diff] [blame] | 1726 | #ifdef CONFIG_PM |
| 1727 | static int mma8452_runtime_suspend(struct device *dev) |
| 1728 | { |
| 1729 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 1730 | struct mma8452_data *data = iio_priv(indio_dev); |
| 1731 | int ret; |
| 1732 | |
| 1733 | mutex_lock(&data->lock); |
| 1734 | ret = mma8452_standby(data); |
| 1735 | mutex_unlock(&data->lock); |
| 1736 | if (ret < 0) { |
| 1737 | dev_err(&data->client->dev, "powering off device failed\n"); |
| 1738 | return -EAGAIN; |
| 1739 | } |
| 1740 | |
Anson Huang | f6ff49b | 2019-01-08 09:14:06 +0000 | [diff] [blame] | 1741 | ret = regulator_disable(data->vddio_reg); |
| 1742 | if (ret) { |
| 1743 | dev_err(dev, "failed to disable VDDIO regulator\n"); |
| 1744 | return ret; |
| 1745 | } |
| 1746 | |
| 1747 | ret = regulator_disable(data->vdd_reg); |
| 1748 | if (ret) { |
| 1749 | dev_err(dev, "failed to disable VDD regulator\n"); |
| 1750 | return ret; |
| 1751 | } |
| 1752 | |
Martin Kepplinger | 96c0cb2 | 2016-03-03 09:24:03 +0100 | [diff] [blame] | 1753 | return 0; |
| 1754 | } |
| 1755 | |
| 1756 | static int mma8452_runtime_resume(struct device *dev) |
| 1757 | { |
| 1758 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 1759 | struct mma8452_data *data = iio_priv(indio_dev); |
| 1760 | int ret, sleep_val; |
| 1761 | |
Anson Huang | f6ff49b | 2019-01-08 09:14:06 +0000 | [diff] [blame] | 1762 | ret = regulator_enable(data->vdd_reg); |
| 1763 | if (ret) { |
| 1764 | dev_err(dev, "failed to enable VDD regulator\n"); |
| 1765 | return ret; |
| 1766 | } |
| 1767 | |
| 1768 | ret = regulator_enable(data->vddio_reg); |
| 1769 | if (ret) { |
| 1770 | dev_err(dev, "failed to enable VDDIO regulator\n"); |
| 1771 | regulator_disable(data->vdd_reg); |
| 1772 | return ret; |
| 1773 | } |
| 1774 | |
Martin Kepplinger | 96c0cb2 | 2016-03-03 09:24:03 +0100 | [diff] [blame] | 1775 | ret = mma8452_active(data); |
| 1776 | if (ret < 0) |
Anson Huang | f6ff49b | 2019-01-08 09:14:06 +0000 | [diff] [blame] | 1777 | goto runtime_resume_failed; |
Martin Kepplinger | 96c0cb2 | 2016-03-03 09:24:03 +0100 | [diff] [blame] | 1778 | |
| 1779 | ret = mma8452_get_odr_index(data); |
| 1780 | sleep_val = 1000 / mma8452_samp_freq[ret][0]; |
| 1781 | if (sleep_val < 20) |
| 1782 | usleep_range(sleep_val * 1000, 20000); |
| 1783 | else |
| 1784 | msleep_interruptible(sleep_val); |
| 1785 | |
| 1786 | return 0; |
Martin Kepplinger | 96c0cb2 | 2016-03-03 09:24:03 +0100 | [diff] [blame] | 1787 | |
Anson Huang | f6ff49b | 2019-01-08 09:14:06 +0000 | [diff] [blame] | 1788 | runtime_resume_failed: |
| 1789 | regulator_disable(data->vddio_reg); |
| 1790 | regulator_disable(data->vdd_reg); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1791 | |
Anson Huang | f6ff49b | 2019-01-08 09:14:06 +0000 | [diff] [blame] | 1792 | return ret; |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1793 | } |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1794 | #endif |
| 1795 | |
Martin Kepplinger | 96c0cb2 | 2016-03-03 09:24:03 +0100 | [diff] [blame] | 1796 | static const struct dev_pm_ops mma8452_pm_ops = { |
Anson Huang | f6ff49b | 2019-01-08 09:14:06 +0000 | [diff] [blame] | 1797 | SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume) |
Martin Kepplinger | 96c0cb2 | 2016-03-03 09:24:03 +0100 | [diff] [blame] | 1798 | SET_RUNTIME_PM_OPS(mma8452_runtime_suspend, |
| 1799 | mma8452_runtime_resume, NULL) |
| 1800 | }; |
| 1801 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1802 | static const struct i2c_device_id mma8452_id[] = { |
Martin Kepplinger | ddb851a | 2016-03-14 12:33:14 +0100 | [diff] [blame] | 1803 | { "mma8451", mma8451 }, |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 1804 | { "mma8452", mma8452 }, |
Martin Kepplinger | c5ea1b58 | 2015-09-01 13:45:09 +0200 | [diff] [blame] | 1805 | { "mma8453", mma8453 }, |
Martin Kepplinger | 417e008 | 2015-09-01 13:45:11 +0200 | [diff] [blame] | 1806 | { "mma8652", mma8652 }, |
| 1807 | { "mma8653", mma8653 }, |
Martin Kepplinger | e873118 | 2016-03-09 12:01:29 +0100 | [diff] [blame] | 1808 | { "fxls8471", fxls8471 }, |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1809 | { } |
| 1810 | }; |
| 1811 | MODULE_DEVICE_TABLE(i2c, mma8452_id); |
| 1812 | |
| 1813 | static struct i2c_driver mma8452_driver = { |
| 1814 | .driver = { |
| 1815 | .name = "mma8452", |
Martin Fuzzey | a3fb96a | 2014-11-07 14:06:00 +0000 | [diff] [blame] | 1816 | .of_match_table = of_match_ptr(mma8452_dt_ids), |
Martin Kepplinger | 96c0cb2 | 2016-03-03 09:24:03 +0100 | [diff] [blame] | 1817 | .pm = &mma8452_pm_ops, |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1818 | }, |
| 1819 | .probe = mma8452_probe, |
| 1820 | .remove = mma8452_remove, |
| 1821 | .id_table = mma8452_id, |
| 1822 | }; |
| 1823 | module_i2c_driver(mma8452_driver); |
| 1824 | |
| 1825 | MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>"); |
Martin Kepplinger | f26ab1a | 2016-06-03 14:51:52 +0200 | [diff] [blame] | 1826 | MODULE_DESCRIPTION("Freescale / NXP MMA8452 accelerometer driver"); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1827 | MODULE_LICENSE("GPL"); |