Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1 | /* |
Martin Kepplinger | c5ea1b58 | 2015-09-01 13:45:09 +0200 | [diff] [blame] | 2 | * mma8452.c - Support for following Freescale 3-axis accelerometers: |
| 3 | * |
| 4 | * MMA8452Q (12 bit) |
| 5 | * MMA8453Q (10 bit) |
Martin Kepplinger | 417e008 | 2015-09-01 13:45:11 +0200 | [diff] [blame] | 6 | * MMA8652FC (12 bit) |
| 7 | * MMA8653FC (10 bit) |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 8 | * |
Martin Kepplinger | d6223c3 | 2015-09-01 13:45:12 +0200 | [diff] [blame] | 9 | * Copyright 2015 Martin Kepplinger <martin.kepplinger@theobroma-systems.com> |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 10 | * Copyright 2014 Peter Meerwald <pmeerw@pmeerw.net> |
| 11 | * |
| 12 | * This file is subject to the terms and conditions of version 2 of |
| 13 | * the GNU General Public License. See the file COPYING in the main |
| 14 | * directory of this archive for more details. |
| 15 | * |
| 16 | * 7-bit I2C slave address 0x1c/0x1d (pin selectable) |
| 17 | * |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame^] | 18 | * TODO: orientation events, autosleep |
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> |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 33 | |
Hartmut Knaack | 69abff8 | 2015-08-02 22:43:50 +0200 | [diff] [blame] | 34 | #define MMA8452_STATUS 0x00 |
| 35 | #define MMA8452_STATUS_DRDY (BIT(2) | BIT(1) | BIT(0)) |
Martin Kepplinger | c5ea1b58 | 2015-09-01 13:45:09 +0200 | [diff] [blame] | 36 | #define MMA8452_OUT_X 0x01 /* MSB first */ |
Hartmut Knaack | 69abff8 | 2015-08-02 22:43:50 +0200 | [diff] [blame] | 37 | #define MMA8452_OUT_Y 0x03 |
| 38 | #define MMA8452_OUT_Z 0x05 |
| 39 | #define MMA8452_INT_SRC 0x0c |
| 40 | #define MMA8452_WHO_AM_I 0x0d |
| 41 | #define MMA8452_DATA_CFG 0x0e |
| 42 | #define MMA8452_DATA_CFG_FS_MASK GENMASK(1, 0) |
| 43 | #define MMA8452_DATA_CFG_FS_2G 0 |
| 44 | #define MMA8452_DATA_CFG_FS_4G 1 |
| 45 | #define MMA8452_DATA_CFG_FS_8G 2 |
| 46 | #define MMA8452_DATA_CFG_HPF_MASK BIT(4) |
| 47 | #define MMA8452_HP_FILTER_CUTOFF 0x0f |
| 48 | #define MMA8452_HP_FILTER_CUTOFF_SEL_MASK GENMASK(1, 0) |
Martin Kepplinger | 60f562e | 2015-09-01 13:45:10 +0200 | [diff] [blame] | 49 | #define MMA8452_FF_MT_CFG 0x15 |
| 50 | #define MMA8452_FF_MT_CFG_OAE BIT(6) |
| 51 | #define MMA8452_FF_MT_CFG_ELE BIT(7) |
| 52 | #define MMA8452_FF_MT_SRC 0x16 |
| 53 | #define MMA8452_FF_MT_SRC_XHE BIT(1) |
| 54 | #define MMA8452_FF_MT_SRC_YHE BIT(3) |
| 55 | #define MMA8452_FF_MT_SRC_ZHE BIT(5) |
| 56 | #define MMA8452_FF_MT_THS 0x17 |
| 57 | #define MMA8452_FF_MT_THS_MASK 0x7f |
| 58 | #define MMA8452_FF_MT_COUNT 0x18 |
Hartmut Knaack | 69abff8 | 2015-08-02 22:43:50 +0200 | [diff] [blame] | 59 | #define MMA8452_TRANSIENT_CFG 0x1d |
| 60 | #define MMA8452_TRANSIENT_CFG_HPF_BYP BIT(0) |
Hartmut Knaack | 69abff8 | 2015-08-02 22:43:50 +0200 | [diff] [blame] | 61 | #define MMA8452_TRANSIENT_CFG_ELE BIT(4) |
| 62 | #define MMA8452_TRANSIENT_SRC 0x1e |
| 63 | #define MMA8452_TRANSIENT_SRC_XTRANSE BIT(1) |
| 64 | #define MMA8452_TRANSIENT_SRC_YTRANSE BIT(3) |
| 65 | #define MMA8452_TRANSIENT_SRC_ZTRANSE BIT(5) |
| 66 | #define MMA8452_TRANSIENT_THS 0x1f |
| 67 | #define MMA8452_TRANSIENT_THS_MASK GENMASK(6, 0) |
| 68 | #define MMA8452_TRANSIENT_COUNT 0x20 |
| 69 | #define MMA8452_CTRL_REG1 0x2a |
| 70 | #define MMA8452_CTRL_ACTIVE BIT(0) |
| 71 | #define MMA8452_CTRL_DR_MASK GENMASK(5, 3) |
| 72 | #define MMA8452_CTRL_DR_SHIFT 3 |
| 73 | #define MMA8452_CTRL_DR_DEFAULT 0x4 /* 50 Hz sample frequency */ |
| 74 | #define MMA8452_CTRL_REG2 0x2b |
| 75 | #define MMA8452_CTRL_REG2_RST BIT(6) |
| 76 | #define MMA8452_CTRL_REG4 0x2d |
| 77 | #define MMA8452_CTRL_REG5 0x2e |
| 78 | #define MMA8452_OFF_X 0x2f |
| 79 | #define MMA8452_OFF_Y 0x30 |
| 80 | #define MMA8452_OFF_Z 0x31 |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 81 | |
Hartmut Knaack | 69abff8 | 2015-08-02 22:43:50 +0200 | [diff] [blame] | 82 | #define MMA8452_MAX_REG 0x31 |
Martin Fuzzey | 2a17698c | 2015-05-13 12:26:40 +0200 | [diff] [blame] | 83 | |
Hartmut Knaack | 69abff8 | 2015-08-02 22:43:50 +0200 | [diff] [blame] | 84 | #define MMA8452_INT_DRDY BIT(0) |
Martin Kepplinger | 60f562e | 2015-09-01 13:45:10 +0200 | [diff] [blame] | 85 | #define MMA8452_INT_FF_MT BIT(2) |
Hartmut Knaack | 69abff8 | 2015-08-02 22:43:50 +0200 | [diff] [blame] | 86 | #define MMA8452_INT_TRANS BIT(5) |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 87 | |
Hartmut Knaack | 69abff8 | 2015-08-02 22:43:50 +0200 | [diff] [blame] | 88 | #define MMA8452_DEVICE_ID 0x2a |
Martin Kepplinger | c5ea1b58 | 2015-09-01 13:45:09 +0200 | [diff] [blame] | 89 | #define MMA8453_DEVICE_ID 0x3a |
Martin Kepplinger | 417e008 | 2015-09-01 13:45:11 +0200 | [diff] [blame] | 90 | #define MMA8652_DEVICE_ID 0x4a |
| 91 | #define MMA8653_DEVICE_ID 0x5a |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 92 | |
| 93 | struct mma8452_data { |
| 94 | struct i2c_client *client; |
| 95 | struct mutex lock; |
| 96 | u8 ctrl_reg1; |
| 97 | u8 data_cfg; |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 98 | const struct mma_chip_info *chip_info; |
| 99 | }; |
| 100 | |
| 101 | /** |
| 102 | * struct mma_chip_info - chip specific data for Freescale's accelerometers |
| 103 | * @chip_id: WHO_AM_I register's value |
| 104 | * @channels: struct iio_chan_spec matching the device's |
| 105 | * capabilities |
| 106 | * @num_channels: number of channels |
| 107 | * @mma_scales: scale factors for converting register values |
| 108 | * to m/s^2; 3 modes: 2g, 4g, 8g; 2 integers |
| 109 | * per mode: m/s^2 and micro m/s^2 |
| 110 | * @ev_cfg: event config register address |
| 111 | * @ev_cfg_ele: latch bit in event config register |
| 112 | * @ev_cfg_chan_shift: number of the bit to enable events in X |
| 113 | * direction; in event config register |
| 114 | * @ev_src: event source register address |
| 115 | * @ev_src_xe: bit in event source register that indicates |
| 116 | * an event in X direction |
| 117 | * @ev_src_ye: bit in event source register that indicates |
| 118 | * an event in Y direction |
| 119 | * @ev_src_ze: bit in event source register that indicates |
| 120 | * an event in Z direction |
| 121 | * @ev_ths: event threshold register address |
| 122 | * @ev_ths_mask: mask for the threshold value |
| 123 | * @ev_count: event count (period) register address |
| 124 | * |
| 125 | * Since not all chips supported by the driver support comparing high pass |
| 126 | * filtered data for events (interrupts), different interrupt sources are |
| 127 | * used for different chips and the relevant registers are included here. |
| 128 | */ |
| 129 | struct mma_chip_info { |
| 130 | u8 chip_id; |
| 131 | const struct iio_chan_spec *channels; |
| 132 | int num_channels; |
| 133 | const int mma_scales[3][2]; |
| 134 | u8 ev_cfg; |
| 135 | u8 ev_cfg_ele; |
| 136 | u8 ev_cfg_chan_shift; |
| 137 | u8 ev_src; |
| 138 | u8 ev_src_xe; |
| 139 | u8 ev_src_ye; |
| 140 | u8 ev_src_ze; |
| 141 | u8 ev_ths; |
| 142 | u8 ev_ths_mask; |
| 143 | u8 ev_count; |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
Martin Kepplinger | e60378c | 2015-12-15 17:45:00 +0100 | [diff] [blame] | 146 | enum { |
| 147 | idx_x, |
| 148 | idx_y, |
| 149 | idx_z, |
| 150 | idx_ts, |
| 151 | }; |
| 152 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 153 | static int mma8452_drdy(struct mma8452_data *data) |
| 154 | { |
| 155 | int tries = 150; |
| 156 | |
| 157 | while (tries-- > 0) { |
| 158 | int ret = i2c_smbus_read_byte_data(data->client, |
| 159 | MMA8452_STATUS); |
| 160 | if (ret < 0) |
| 161 | return ret; |
| 162 | if ((ret & MMA8452_STATUS_DRDY) == MMA8452_STATUS_DRDY) |
| 163 | return 0; |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 164 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 165 | msleep(20); |
| 166 | } |
| 167 | |
| 168 | dev_err(&data->client->dev, "data not ready\n"); |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 169 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 170 | return -EIO; |
| 171 | } |
| 172 | |
| 173 | static int mma8452_read(struct mma8452_data *data, __be16 buf[3]) |
| 174 | { |
| 175 | int ret = mma8452_drdy(data); |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 176 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 177 | if (ret < 0) |
| 178 | return ret; |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 179 | |
| 180 | return i2c_smbus_read_i2c_block_data(data->client, MMA8452_OUT_X, |
| 181 | 3 * sizeof(__be16), (u8 *)buf); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 184 | static ssize_t mma8452_show_int_plus_micros(char *buf, const int (*vals)[2], |
| 185 | int n) |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 186 | { |
| 187 | size_t len = 0; |
| 188 | |
| 189 | while (n-- > 0) |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 190 | len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06d ", |
| 191 | vals[n][0], vals[n][1]); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 192 | |
| 193 | /* replace trailing space by newline */ |
| 194 | buf[len - 1] = '\n'; |
| 195 | |
| 196 | return len; |
| 197 | } |
| 198 | |
| 199 | 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] | 200 | int val, int val2) |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 201 | { |
| 202 | while (n-- > 0) |
| 203 | if (val == vals[n][0] && val2 == vals[n][1]) |
| 204 | return n; |
| 205 | |
| 206 | return -EINVAL; |
| 207 | } |
| 208 | |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 209 | static int mma8452_get_odr_index(struct mma8452_data *data) |
| 210 | { |
| 211 | return (data->ctrl_reg1 & MMA8452_CTRL_DR_MASK) >> |
| 212 | MMA8452_CTRL_DR_SHIFT; |
| 213 | } |
| 214 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 215 | static const int mma8452_samp_freq[8][2] = { |
| 216 | {800, 0}, {400, 0}, {200, 0}, {100, 0}, {50, 0}, {12, 500000}, |
| 217 | {6, 250000}, {1, 560000} |
| 218 | }; |
| 219 | |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 220 | /* Datasheet table 35 (step time vs sample frequency) */ |
| 221 | static const int mma8452_transient_time_step_us[8] = { |
| 222 | 1250, |
| 223 | 2500, |
| 224 | 5000, |
| 225 | 10000, |
| 226 | 20000, |
| 227 | 20000, |
| 228 | 20000, |
| 229 | 20000 |
| 230 | }; |
| 231 | |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 232 | /* Datasheet table 18 (normal mode) */ |
| 233 | static const int mma8452_hp_filter_cutoff[8][4][2] = { |
| 234 | { {16, 0}, {8, 0}, {4, 0}, {2, 0} }, /* 800 Hz sample */ |
| 235 | { {16, 0}, {8, 0}, {4, 0}, {2, 0} }, /* 400 Hz sample */ |
| 236 | { {8, 0}, {4, 0}, {2, 0}, {1, 0} }, /* 200 Hz sample */ |
| 237 | { {4, 0}, {2, 0}, {1, 0}, {0, 500000} }, /* 100 Hz sample */ |
| 238 | { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} }, /* 50 Hz sample */ |
| 239 | { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} }, /* 12.5 Hz sample */ |
| 240 | { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} }, /* 6.25 Hz sample */ |
| 241 | { {2, 0}, {1, 0}, {0, 500000}, {0, 250000} } /* 1.56 Hz sample */ |
| 242 | }; |
| 243 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 244 | static ssize_t mma8452_show_samp_freq_avail(struct device *dev, |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 245 | struct device_attribute *attr, |
| 246 | char *buf) |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 247 | { |
| 248 | return mma8452_show_int_plus_micros(buf, mma8452_samp_freq, |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 249 | ARRAY_SIZE(mma8452_samp_freq)); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | static ssize_t mma8452_show_scale_avail(struct device *dev, |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 253 | struct device_attribute *attr, |
| 254 | char *buf) |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 255 | { |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 256 | struct mma8452_data *data = iio_priv(i2c_get_clientdata( |
| 257 | to_i2c_client(dev))); |
| 258 | |
| 259 | return mma8452_show_int_plus_micros(buf, data->chip_info->mma_scales, |
| 260 | ARRAY_SIZE(data->chip_info->mma_scales)); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 263 | static ssize_t mma8452_show_hp_cutoff_avail(struct device *dev, |
| 264 | struct device_attribute *attr, |
| 265 | char *buf) |
| 266 | { |
| 267 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
| 268 | struct mma8452_data *data = iio_priv(indio_dev); |
| 269 | int i = mma8452_get_odr_index(data); |
| 270 | |
| 271 | return mma8452_show_int_plus_micros(buf, mma8452_hp_filter_cutoff[i], |
| 272 | ARRAY_SIZE(mma8452_hp_filter_cutoff[0])); |
| 273 | } |
| 274 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 275 | static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(mma8452_show_samp_freq_avail); |
| 276 | static IIO_DEVICE_ATTR(in_accel_scale_available, S_IRUGO, |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 277 | mma8452_show_scale_avail, NULL, 0); |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 278 | static IIO_DEVICE_ATTR(in_accel_filter_high_pass_3db_frequency_available, |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 279 | S_IRUGO, mma8452_show_hp_cutoff_avail, NULL, 0); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 280 | |
| 281 | static int mma8452_get_samp_freq_index(struct mma8452_data *data, |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 282 | int val, int val2) |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 283 | { |
| 284 | return mma8452_get_int_plus_micros_index(mma8452_samp_freq, |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 285 | ARRAY_SIZE(mma8452_samp_freq), |
| 286 | val, val2); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 289 | 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] | 290 | { |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 291 | return mma8452_get_int_plus_micros_index(data->chip_info->mma_scales, |
| 292 | ARRAY_SIZE(data->chip_info->mma_scales), val, val2); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 295 | static int mma8452_get_hp_filter_index(struct mma8452_data *data, |
| 296 | int val, int val2) |
| 297 | { |
| 298 | int i = mma8452_get_odr_index(data); |
| 299 | |
| 300 | return mma8452_get_int_plus_micros_index(mma8452_hp_filter_cutoff[i], |
Hartmut Knaack | 001fceb | 2015-08-02 22:43:46 +0200 | [diff] [blame] | 301 | ARRAY_SIZE(mma8452_hp_filter_cutoff[0]), val, val2); |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | static int mma8452_read_hp_filter(struct mma8452_data *data, int *hz, int *uHz) |
| 305 | { |
| 306 | int i, ret; |
| 307 | |
| 308 | ret = i2c_smbus_read_byte_data(data->client, MMA8452_HP_FILTER_CUTOFF); |
| 309 | if (ret < 0) |
| 310 | return ret; |
| 311 | |
| 312 | i = mma8452_get_odr_index(data); |
| 313 | ret &= MMA8452_HP_FILTER_CUTOFF_SEL_MASK; |
| 314 | *hz = mma8452_hp_filter_cutoff[i][ret][0]; |
| 315 | *uHz = mma8452_hp_filter_cutoff[i][ret][1]; |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 320 | static int mma8452_read_raw(struct iio_dev *indio_dev, |
| 321 | struct iio_chan_spec const *chan, |
| 322 | int *val, int *val2, long mask) |
| 323 | { |
| 324 | struct mma8452_data *data = iio_priv(indio_dev); |
| 325 | __be16 buffer[3]; |
| 326 | int i, ret; |
| 327 | |
| 328 | switch (mask) { |
| 329 | case IIO_CHAN_INFO_RAW: |
| 330 | if (iio_buffer_enabled(indio_dev)) |
| 331 | return -EBUSY; |
| 332 | |
| 333 | mutex_lock(&data->lock); |
| 334 | ret = mma8452_read(data, buffer); |
| 335 | mutex_unlock(&data->lock); |
| 336 | if (ret < 0) |
| 337 | return ret; |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 338 | |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 339 | *val = sign_extend32(be16_to_cpu( |
| 340 | buffer[chan->scan_index]) >> chan->scan_type.shift, |
| 341 | chan->scan_type.realbits - 1); |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 342 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 343 | return IIO_VAL_INT; |
| 344 | case IIO_CHAN_INFO_SCALE: |
| 345 | i = data->data_cfg & MMA8452_DATA_CFG_FS_MASK; |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 346 | *val = data->chip_info->mma_scales[i][0]; |
| 347 | *val2 = data->chip_info->mma_scales[i][1]; |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 348 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 349 | return IIO_VAL_INT_PLUS_MICRO; |
| 350 | case IIO_CHAN_INFO_SAMP_FREQ: |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 351 | i = mma8452_get_odr_index(data); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 352 | *val = mma8452_samp_freq[i][0]; |
| 353 | *val2 = mma8452_samp_freq[i][1]; |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 354 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 355 | return IIO_VAL_INT_PLUS_MICRO; |
| 356 | case IIO_CHAN_INFO_CALIBBIAS: |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 357 | ret = i2c_smbus_read_byte_data(data->client, |
| 358 | MMA8452_OFF_X + chan->scan_index); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 359 | if (ret < 0) |
| 360 | return ret; |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 361 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 362 | *val = sign_extend32(ret, 7); |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 363 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 364 | return IIO_VAL_INT; |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 365 | case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY: |
| 366 | if (data->data_cfg & MMA8452_DATA_CFG_HPF_MASK) { |
| 367 | ret = mma8452_read_hp_filter(data, val, val2); |
| 368 | if (ret < 0) |
| 369 | return ret; |
| 370 | } else { |
| 371 | *val = 0; |
| 372 | *val2 = 0; |
| 373 | } |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 374 | |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 375 | return IIO_VAL_INT_PLUS_MICRO; |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 376 | } |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 377 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 378 | return -EINVAL; |
| 379 | } |
| 380 | |
| 381 | static int mma8452_standby(struct mma8452_data *data) |
| 382 | { |
| 383 | return i2c_smbus_write_byte_data(data->client, MMA8452_CTRL_REG1, |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 384 | data->ctrl_reg1 & ~MMA8452_CTRL_ACTIVE); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | static int mma8452_active(struct mma8452_data *data) |
| 388 | { |
| 389 | return i2c_smbus_write_byte_data(data->client, MMA8452_CTRL_REG1, |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 390 | data->ctrl_reg1); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | static int mma8452_change_config(struct mma8452_data *data, u8 reg, u8 val) |
| 394 | { |
| 395 | int ret; |
| 396 | |
| 397 | mutex_lock(&data->lock); |
| 398 | |
| 399 | /* config can only be changed when in standby */ |
| 400 | ret = mma8452_standby(data); |
| 401 | if (ret < 0) |
| 402 | goto fail; |
| 403 | |
| 404 | ret = i2c_smbus_write_byte_data(data->client, reg, val); |
| 405 | if (ret < 0) |
| 406 | goto fail; |
| 407 | |
| 408 | ret = mma8452_active(data); |
| 409 | if (ret < 0) |
| 410 | goto fail; |
| 411 | |
| 412 | ret = 0; |
| 413 | fail: |
| 414 | mutex_unlock(&data->lock); |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 415 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 416 | return ret; |
| 417 | } |
| 418 | |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame^] | 419 | /* returns >0 if in freefall mode, 0 if not or <0 if an error occured */ |
| 420 | static int mma8452_freefall_mode_enabled(struct mma8452_data *data) |
| 421 | { |
| 422 | int val; |
| 423 | const struct mma_chip_info *chip = data->chip_info; |
| 424 | |
| 425 | val = i2c_smbus_read_byte_data(data->client, chip->ev_cfg); |
| 426 | if (val < 0) |
| 427 | return val; |
| 428 | |
| 429 | return !(val & MMA8452_FF_MT_CFG_OAE); |
| 430 | } |
| 431 | |
| 432 | static int mma8452_set_freefall_mode(struct mma8452_data *data, bool state) |
| 433 | { |
| 434 | int val; |
| 435 | const struct mma_chip_info *chip = data->chip_info; |
| 436 | |
| 437 | if ((state && mma8452_freefall_mode_enabled(data)) || |
| 438 | (!state && !(mma8452_freefall_mode_enabled(data)))) |
| 439 | return 0; |
| 440 | |
| 441 | val = i2c_smbus_read_byte_data(data->client, chip->ev_cfg); |
| 442 | if (val < 0) |
| 443 | return val; |
| 444 | |
| 445 | if (state) { |
| 446 | val |= BIT(idx_x + chip->ev_cfg_chan_shift); |
| 447 | val |= BIT(idx_y + chip->ev_cfg_chan_shift); |
| 448 | val |= BIT(idx_z + chip->ev_cfg_chan_shift); |
| 449 | val &= ~MMA8452_FF_MT_CFG_OAE; |
| 450 | } else { |
| 451 | val &= ~BIT(idx_x + chip->ev_cfg_chan_shift); |
| 452 | val &= ~BIT(idx_y + chip->ev_cfg_chan_shift); |
| 453 | val &= ~BIT(idx_z + chip->ev_cfg_chan_shift); |
| 454 | val |= MMA8452_FF_MT_CFG_OAE; |
| 455 | } |
| 456 | |
| 457 | val = mma8452_change_config(data, chip->ev_cfg, val); |
| 458 | if (val) |
| 459 | return val; |
| 460 | |
| 461 | return 0; |
| 462 | } |
| 463 | |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 464 | static int mma8452_set_hp_filter_frequency(struct mma8452_data *data, |
| 465 | int val, int val2) |
| 466 | { |
| 467 | int i, reg; |
| 468 | |
| 469 | i = mma8452_get_hp_filter_index(data, val, val2); |
| 470 | if (i < 0) |
Hartmut Knaack | b9fddcd | 2015-08-02 22:43:48 +0200 | [diff] [blame] | 471 | return i; |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 472 | |
| 473 | reg = i2c_smbus_read_byte_data(data->client, |
| 474 | MMA8452_HP_FILTER_CUTOFF); |
| 475 | if (reg < 0) |
| 476 | return reg; |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 477 | |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 478 | reg &= ~MMA8452_HP_FILTER_CUTOFF_SEL_MASK; |
| 479 | reg |= i; |
| 480 | |
| 481 | return mma8452_change_config(data, MMA8452_HP_FILTER_CUTOFF, reg); |
| 482 | } |
| 483 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 484 | static int mma8452_write_raw(struct iio_dev *indio_dev, |
| 485 | struct iio_chan_spec const *chan, |
| 486 | int val, int val2, long mask) |
| 487 | { |
| 488 | struct mma8452_data *data = iio_priv(indio_dev); |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 489 | int i, ret; |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 490 | |
| 491 | if (iio_buffer_enabled(indio_dev)) |
| 492 | return -EBUSY; |
| 493 | |
| 494 | switch (mask) { |
| 495 | case IIO_CHAN_INFO_SAMP_FREQ: |
| 496 | i = mma8452_get_samp_freq_index(data, val, val2); |
| 497 | if (i < 0) |
Hartmut Knaack | b9fddcd | 2015-08-02 22:43:48 +0200 | [diff] [blame] | 498 | return i; |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 499 | |
| 500 | data->ctrl_reg1 &= ~MMA8452_CTRL_DR_MASK; |
| 501 | data->ctrl_reg1 |= i << MMA8452_CTRL_DR_SHIFT; |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 502 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 503 | return mma8452_change_config(data, MMA8452_CTRL_REG1, |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 504 | data->ctrl_reg1); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 505 | case IIO_CHAN_INFO_SCALE: |
| 506 | i = mma8452_get_scale_index(data, val, val2); |
| 507 | if (i < 0) |
Hartmut Knaack | b9fddcd | 2015-08-02 22:43:48 +0200 | [diff] [blame] | 508 | return i; |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 509 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 510 | data->data_cfg &= ~MMA8452_DATA_CFG_FS_MASK; |
| 511 | data->data_cfg |= i; |
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 mma8452_change_config(data, MMA8452_DATA_CFG, |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 514 | data->data_cfg); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 515 | case IIO_CHAN_INFO_CALIBBIAS: |
| 516 | if (val < -128 || val > 127) |
| 517 | return -EINVAL; |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 518 | |
| 519 | return mma8452_change_config(data, |
| 520 | MMA8452_OFF_X + chan->scan_index, |
| 521 | val); |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 522 | |
| 523 | case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY: |
| 524 | if (val == 0 && val2 == 0) { |
| 525 | data->data_cfg &= ~MMA8452_DATA_CFG_HPF_MASK; |
| 526 | } else { |
| 527 | data->data_cfg |= MMA8452_DATA_CFG_HPF_MASK; |
| 528 | ret = mma8452_set_hp_filter_frequency(data, val, val2); |
| 529 | if (ret < 0) |
| 530 | return ret; |
| 531 | } |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 532 | |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 533 | return mma8452_change_config(data, MMA8452_DATA_CFG, |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 534 | data->data_cfg); |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 535 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 536 | default: |
| 537 | return -EINVAL; |
| 538 | } |
| 539 | } |
| 540 | |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 541 | static int mma8452_read_thresh(struct iio_dev *indio_dev, |
| 542 | const struct iio_chan_spec *chan, |
| 543 | enum iio_event_type type, |
| 544 | enum iio_event_direction dir, |
| 545 | enum iio_event_info info, |
| 546 | int *val, int *val2) |
| 547 | { |
| 548 | struct mma8452_data *data = iio_priv(indio_dev); |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 549 | int ret, us; |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 550 | |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 551 | switch (info) { |
| 552 | case IIO_EV_INFO_VALUE: |
| 553 | ret = i2c_smbus_read_byte_data(data->client, |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 554 | data->chip_info->ev_ths); |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 555 | if (ret < 0) |
| 556 | return ret; |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 557 | |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 558 | *val = ret & data->chip_info->ev_ths_mask; |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 559 | |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 560 | return IIO_VAL_INT; |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 561 | |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 562 | case IIO_EV_INFO_PERIOD: |
| 563 | ret = i2c_smbus_read_byte_data(data->client, |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 564 | data->chip_info->ev_count); |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 565 | if (ret < 0) |
| 566 | return ret; |
| 567 | |
| 568 | us = ret * mma8452_transient_time_step_us[ |
| 569 | mma8452_get_odr_index(data)]; |
| 570 | *val = us / USEC_PER_SEC; |
| 571 | *val2 = us % USEC_PER_SEC; |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 572 | |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 573 | return IIO_VAL_INT_PLUS_MICRO; |
| 574 | |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 575 | case IIO_EV_INFO_HIGH_PASS_FILTER_3DB: |
| 576 | ret = i2c_smbus_read_byte_data(data->client, |
| 577 | MMA8452_TRANSIENT_CFG); |
| 578 | if (ret < 0) |
| 579 | return ret; |
| 580 | |
| 581 | if (ret & MMA8452_TRANSIENT_CFG_HPF_BYP) { |
| 582 | *val = 0; |
| 583 | *val2 = 0; |
| 584 | } else { |
| 585 | ret = mma8452_read_hp_filter(data, val, val2); |
| 586 | if (ret < 0) |
| 587 | return ret; |
| 588 | } |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 589 | |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 590 | return IIO_VAL_INT_PLUS_MICRO; |
| 591 | |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 592 | default: |
| 593 | return -EINVAL; |
| 594 | } |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | static int mma8452_write_thresh(struct iio_dev *indio_dev, |
| 598 | const struct iio_chan_spec *chan, |
| 599 | enum iio_event_type type, |
| 600 | enum iio_event_direction dir, |
| 601 | enum iio_event_info info, |
| 602 | int val, int val2) |
| 603 | { |
| 604 | struct mma8452_data *data = iio_priv(indio_dev); |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 605 | int ret, reg, steps; |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 606 | |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 607 | switch (info) { |
| 608 | case IIO_EV_INFO_VALUE: |
Hartmut Knaack | 1121822 | 2015-08-02 22:43:49 +0200 | [diff] [blame] | 609 | if (val < 0 || val > MMA8452_TRANSIENT_THS_MASK) |
| 610 | return -EINVAL; |
| 611 | |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 612 | return mma8452_change_config(data, data->chip_info->ev_ths, |
| 613 | val); |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 614 | |
| 615 | case IIO_EV_INFO_PERIOD: |
| 616 | steps = (val * USEC_PER_SEC + val2) / |
| 617 | mma8452_transient_time_step_us[ |
| 618 | mma8452_get_odr_index(data)]; |
| 619 | |
Hartmut Knaack | 1121822 | 2015-08-02 22:43:49 +0200 | [diff] [blame] | 620 | if (steps < 0 || steps > 0xff) |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 621 | return -EINVAL; |
| 622 | |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 623 | return mma8452_change_config(data, data->chip_info->ev_count, |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 624 | steps); |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 625 | |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 626 | case IIO_EV_INFO_HIGH_PASS_FILTER_3DB: |
| 627 | reg = i2c_smbus_read_byte_data(data->client, |
| 628 | MMA8452_TRANSIENT_CFG); |
| 629 | if (reg < 0) |
| 630 | return reg; |
| 631 | |
| 632 | if (val == 0 && val2 == 0) { |
| 633 | reg |= MMA8452_TRANSIENT_CFG_HPF_BYP; |
| 634 | } else { |
| 635 | reg &= ~MMA8452_TRANSIENT_CFG_HPF_BYP; |
| 636 | ret = mma8452_set_hp_filter_frequency(data, val, val2); |
| 637 | if (ret < 0) |
| 638 | return ret; |
| 639 | } |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 640 | |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 641 | return mma8452_change_config(data, MMA8452_TRANSIENT_CFG, reg); |
| 642 | |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 643 | default: |
| 644 | return -EINVAL; |
| 645 | } |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | static int mma8452_read_event_config(struct iio_dev *indio_dev, |
| 649 | const struct iio_chan_spec *chan, |
| 650 | enum iio_event_type type, |
| 651 | enum iio_event_direction dir) |
| 652 | { |
| 653 | struct mma8452_data *data = iio_priv(indio_dev); |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 654 | const struct mma_chip_info *chip = data->chip_info; |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 655 | int ret; |
| 656 | |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame^] | 657 | switch (dir) { |
| 658 | case IIO_EV_DIR_FALLING: |
| 659 | return mma8452_freefall_mode_enabled(data); |
| 660 | case IIO_EV_DIR_RISING: |
| 661 | if (mma8452_freefall_mode_enabled(data)) |
| 662 | return 0; |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 663 | |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame^] | 664 | ret = i2c_smbus_read_byte_data(data->client, |
| 665 | data->chip_info->ev_cfg); |
| 666 | if (ret < 0) |
| 667 | return ret; |
| 668 | |
| 669 | return !!(ret & BIT(chan->scan_index + chip->ev_cfg_chan_shift)); |
| 670 | default: |
| 671 | return -EINVAL; |
| 672 | } |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | static int mma8452_write_event_config(struct iio_dev *indio_dev, |
| 676 | const struct iio_chan_spec *chan, |
| 677 | enum iio_event_type type, |
| 678 | enum iio_event_direction dir, |
| 679 | int state) |
| 680 | { |
| 681 | struct mma8452_data *data = iio_priv(indio_dev); |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 682 | const struct mma_chip_info *chip = data->chip_info; |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 683 | int val; |
| 684 | |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame^] | 685 | switch (dir) { |
| 686 | case IIO_EV_DIR_FALLING: |
| 687 | return mma8452_set_freefall_mode(data, state); |
| 688 | case IIO_EV_DIR_RISING: |
| 689 | val = i2c_smbus_read_byte_data(data->client, chip->ev_cfg); |
| 690 | if (val < 0) |
| 691 | return val; |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 692 | |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame^] | 693 | if (state) { |
| 694 | if (mma8452_freefall_mode_enabled(data)) { |
| 695 | val &= ~BIT(idx_x + chip->ev_cfg_chan_shift); |
| 696 | val &= ~BIT(idx_y + chip->ev_cfg_chan_shift); |
| 697 | val &= ~BIT(idx_z + chip->ev_cfg_chan_shift); |
| 698 | val |= MMA8452_FF_MT_CFG_OAE; |
| 699 | } |
| 700 | val |= BIT(chan->scan_index + chip->ev_cfg_chan_shift); |
| 701 | } else { |
| 702 | if (mma8452_freefall_mode_enabled(data)) |
| 703 | return 0; |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 704 | |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame^] | 705 | val &= ~BIT(chan->scan_index + chip->ev_cfg_chan_shift); |
| 706 | } |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 707 | |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame^] | 708 | val |= chip->ev_cfg_ele; |
| 709 | |
| 710 | return mma8452_change_config(data, chip->ev_cfg, val); |
| 711 | default: |
| 712 | return -EINVAL; |
| 713 | } |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | static void mma8452_transient_interrupt(struct iio_dev *indio_dev) |
| 717 | { |
| 718 | struct mma8452_data *data = iio_priv(indio_dev); |
| 719 | s64 ts = iio_get_time_ns(); |
| 720 | int src; |
| 721 | |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 722 | src = i2c_smbus_read_byte_data(data->client, data->chip_info->ev_src); |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 723 | if (src < 0) |
| 724 | return; |
| 725 | |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame^] | 726 | if (mma8452_freefall_mode_enabled(data)) { |
| 727 | iio_push_event(indio_dev, |
| 728 | IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, |
| 729 | IIO_MOD_X_AND_Y_AND_Z, |
| 730 | IIO_EV_TYPE_MAG, |
| 731 | IIO_EV_DIR_FALLING), |
| 732 | ts); |
| 733 | return; |
| 734 | } |
| 735 | |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 736 | if (src & data->chip_info->ev_src_xe) |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 737 | iio_push_event(indio_dev, |
| 738 | IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X, |
Martin Kepplinger | c5d0db0 | 2015-07-05 19:50:18 +0200 | [diff] [blame] | 739 | IIO_EV_TYPE_MAG, |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 740 | IIO_EV_DIR_RISING), |
| 741 | ts); |
| 742 | |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 743 | if (src & data->chip_info->ev_src_ye) |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 744 | iio_push_event(indio_dev, |
| 745 | IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_Y, |
Martin Kepplinger | c5d0db0 | 2015-07-05 19:50:18 +0200 | [diff] [blame] | 746 | IIO_EV_TYPE_MAG, |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 747 | IIO_EV_DIR_RISING), |
| 748 | ts); |
| 749 | |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 750 | if (src & data->chip_info->ev_src_ze) |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 751 | iio_push_event(indio_dev, |
| 752 | IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_Z, |
Martin Kepplinger | c5d0db0 | 2015-07-05 19:50:18 +0200 | [diff] [blame] | 753 | IIO_EV_TYPE_MAG, |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 754 | IIO_EV_DIR_RISING), |
| 755 | ts); |
| 756 | } |
| 757 | |
| 758 | static irqreturn_t mma8452_interrupt(int irq, void *p) |
| 759 | { |
| 760 | struct iio_dev *indio_dev = p; |
| 761 | struct mma8452_data *data = iio_priv(indio_dev); |
Martin Kepplinger | 60f562e | 2015-09-01 13:45:10 +0200 | [diff] [blame] | 762 | const struct mma_chip_info *chip = data->chip_info; |
Martin Fuzzey | ae6d9ce | 2015-06-01 15:39:58 +0200 | [diff] [blame] | 763 | int ret = IRQ_NONE; |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 764 | int src; |
| 765 | |
| 766 | src = i2c_smbus_read_byte_data(data->client, MMA8452_INT_SRC); |
| 767 | if (src < 0) |
| 768 | return IRQ_NONE; |
| 769 | |
Martin Fuzzey | ae6d9ce | 2015-06-01 15:39:58 +0200 | [diff] [blame] | 770 | if (src & MMA8452_INT_DRDY) { |
| 771 | iio_trigger_poll_chained(indio_dev->trig); |
| 772 | ret = IRQ_HANDLED; |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 773 | } |
| 774 | |
Martin Kepplinger | 60f562e | 2015-09-01 13:45:10 +0200 | [diff] [blame] | 775 | if ((src & MMA8452_INT_TRANS && |
| 776 | chip->ev_src == MMA8452_TRANSIENT_SRC) || |
| 777 | (src & MMA8452_INT_FF_MT && |
| 778 | chip->ev_src == MMA8452_FF_MT_SRC)) { |
Martin Fuzzey | ae6d9ce | 2015-06-01 15:39:58 +0200 | [diff] [blame] | 779 | mma8452_transient_interrupt(indio_dev); |
| 780 | ret = IRQ_HANDLED; |
| 781 | } |
| 782 | |
| 783 | return ret; |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 784 | } |
| 785 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 786 | static irqreturn_t mma8452_trigger_handler(int irq, void *p) |
| 787 | { |
| 788 | struct iio_poll_func *pf = p; |
| 789 | struct iio_dev *indio_dev = pf->indio_dev; |
| 790 | struct mma8452_data *data = iio_priv(indio_dev); |
| 791 | u8 buffer[16]; /* 3 16-bit channels + padding + ts */ |
| 792 | int ret; |
| 793 | |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 794 | ret = mma8452_read(data, (__be16 *)buffer); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 795 | if (ret < 0) |
| 796 | goto done; |
| 797 | |
| 798 | iio_push_to_buffers_with_timestamp(indio_dev, buffer, |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 799 | iio_get_time_ns()); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 800 | |
| 801 | done: |
| 802 | iio_trigger_notify_done(indio_dev->trig); |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 803 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 804 | return IRQ_HANDLED; |
| 805 | } |
| 806 | |
Martin Fuzzey | 2a17698c | 2015-05-13 12:26:40 +0200 | [diff] [blame] | 807 | static int mma8452_reg_access_dbg(struct iio_dev *indio_dev, |
| 808 | unsigned reg, unsigned writeval, |
| 809 | unsigned *readval) |
| 810 | { |
| 811 | int ret; |
| 812 | struct mma8452_data *data = iio_priv(indio_dev); |
| 813 | |
| 814 | if (reg > MMA8452_MAX_REG) |
| 815 | return -EINVAL; |
| 816 | |
| 817 | if (!readval) |
| 818 | return mma8452_change_config(data, reg, writeval); |
| 819 | |
| 820 | ret = i2c_smbus_read_byte_data(data->client, reg); |
| 821 | if (ret < 0) |
| 822 | return ret; |
| 823 | |
| 824 | *readval = ret; |
| 825 | |
| 826 | return 0; |
| 827 | } |
| 828 | |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame^] | 829 | static const struct iio_event_spec mma8452_freefall_event[] = { |
| 830 | { |
| 831 | .type = IIO_EV_TYPE_MAG, |
| 832 | .dir = IIO_EV_DIR_FALLING, |
| 833 | .mask_separate = BIT(IIO_EV_INFO_ENABLE), |
| 834 | .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) | |
| 835 | BIT(IIO_EV_INFO_PERIOD) | |
| 836 | BIT(IIO_EV_INFO_HIGH_PASS_FILTER_3DB) |
| 837 | }, |
| 838 | }; |
| 839 | |
| 840 | static const struct iio_event_spec mma8652_freefall_event[] = { |
| 841 | { |
| 842 | .type = IIO_EV_TYPE_MAG, |
| 843 | .dir = IIO_EV_DIR_FALLING, |
| 844 | .mask_separate = BIT(IIO_EV_INFO_ENABLE), |
| 845 | .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) | |
| 846 | BIT(IIO_EV_INFO_PERIOD) |
| 847 | }, |
| 848 | }; |
| 849 | |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 850 | static const struct iio_event_spec mma8452_transient_event[] = { |
| 851 | { |
Martin Kepplinger | c5d0db0 | 2015-07-05 19:50:18 +0200 | [diff] [blame] | 852 | .type = IIO_EV_TYPE_MAG, |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 853 | .dir = IIO_EV_DIR_RISING, |
| 854 | .mask_separate = BIT(IIO_EV_INFO_ENABLE), |
Martin Fuzzey | 5dbbd19 | 2015-06-01 15:39:54 +0200 | [diff] [blame] | 855 | .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) | |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 856 | BIT(IIO_EV_INFO_PERIOD) | |
| 857 | BIT(IIO_EV_INFO_HIGH_PASS_FILTER_3DB) |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 858 | }, |
| 859 | }; |
| 860 | |
Martin Kepplinger | 60f562e | 2015-09-01 13:45:10 +0200 | [diff] [blame] | 861 | static const struct iio_event_spec mma8452_motion_event[] = { |
| 862 | { |
| 863 | .type = IIO_EV_TYPE_MAG, |
| 864 | .dir = IIO_EV_DIR_RISING, |
| 865 | .mask_separate = BIT(IIO_EV_INFO_ENABLE), |
| 866 | .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) | |
| 867 | BIT(IIO_EV_INFO_PERIOD) |
| 868 | }, |
| 869 | }; |
| 870 | |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 871 | /* |
| 872 | * Threshold is configured in fixed 8G/127 steps regardless of |
| 873 | * currently selected scale for measurement. |
| 874 | */ |
| 875 | static IIO_CONST_ATTR_NAMED(accel_transient_scale, in_accel_scale, "0.617742"); |
| 876 | |
| 877 | static struct attribute *mma8452_event_attributes[] = { |
| 878 | &iio_const_attr_accel_transient_scale.dev_attr.attr, |
| 879 | NULL, |
| 880 | }; |
| 881 | |
| 882 | static struct attribute_group mma8452_event_attribute_group = { |
| 883 | .attrs = mma8452_event_attributes, |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 884 | }; |
| 885 | |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame^] | 886 | #define MMA8452_FREEFALL_CHANNEL(modifier) { \ |
| 887 | .type = IIO_ACCEL, \ |
| 888 | .modified = 1, \ |
| 889 | .channel2 = modifier, \ |
| 890 | .scan_index = -1, \ |
| 891 | .event_spec = mma8452_freefall_event, \ |
| 892 | .num_event_specs = ARRAY_SIZE(mma8452_freefall_event), \ |
| 893 | } |
| 894 | |
| 895 | #define MMA8652_FREEFALL_CHANNEL(modifier) { \ |
| 896 | .type = IIO_ACCEL, \ |
| 897 | .modified = 1, \ |
| 898 | .channel2 = modifier, \ |
| 899 | .scan_index = -1, \ |
| 900 | .event_spec = mma8652_freefall_event, \ |
| 901 | .num_event_specs = ARRAY_SIZE(mma8652_freefall_event), \ |
| 902 | } |
| 903 | |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 904 | #define MMA8452_CHANNEL(axis, idx, bits) { \ |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 905 | .type = IIO_ACCEL, \ |
| 906 | .modified = 1, \ |
| 907 | .channel2 = IIO_MOD_##axis, \ |
| 908 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 909 | BIT(IIO_CHAN_INFO_CALIBBIAS), \ |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 910 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \ |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 911 | BIT(IIO_CHAN_INFO_SCALE) | \ |
| 912 | BIT(IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY), \ |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 913 | .scan_index = idx, \ |
| 914 | .scan_type = { \ |
| 915 | .sign = 's', \ |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 916 | .realbits = (bits), \ |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 917 | .storagebits = 16, \ |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 918 | .shift = 16 - (bits), \ |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 919 | .endianness = IIO_BE, \ |
| 920 | }, \ |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 921 | .event_spec = mma8452_transient_event, \ |
| 922 | .num_event_specs = ARRAY_SIZE(mma8452_transient_event), \ |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 923 | } |
| 924 | |
Martin Kepplinger | 417e008 | 2015-09-01 13:45:11 +0200 | [diff] [blame] | 925 | #define MMA8652_CHANNEL(axis, idx, bits) { \ |
| 926 | .type = IIO_ACCEL, \ |
| 927 | .modified = 1, \ |
| 928 | .channel2 = IIO_MOD_##axis, \ |
| 929 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
| 930 | BIT(IIO_CHAN_INFO_CALIBBIAS), \ |
| 931 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \ |
| 932 | BIT(IIO_CHAN_INFO_SCALE), \ |
| 933 | .scan_index = idx, \ |
| 934 | .scan_type = { \ |
| 935 | .sign = 's', \ |
| 936 | .realbits = (bits), \ |
| 937 | .storagebits = 16, \ |
| 938 | .shift = 16 - (bits), \ |
| 939 | .endianness = IIO_BE, \ |
| 940 | }, \ |
| 941 | .event_spec = mma8452_motion_event, \ |
| 942 | .num_event_specs = ARRAY_SIZE(mma8452_motion_event), \ |
| 943 | } |
| 944 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 945 | static const struct iio_chan_spec mma8452_channels[] = { |
Martin Kepplinger | e60378c | 2015-12-15 17:45:00 +0100 | [diff] [blame] | 946 | MMA8452_CHANNEL(X, idx_x, 12), |
| 947 | MMA8452_CHANNEL(Y, idx_y, 12), |
| 948 | MMA8452_CHANNEL(Z, idx_z, 12), |
| 949 | IIO_CHAN_SOFT_TIMESTAMP(idx_ts), |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame^] | 950 | MMA8452_FREEFALL_CHANNEL(IIO_MOD_X_AND_Y_AND_Z), |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 951 | }; |
| 952 | |
Martin Kepplinger | c5ea1b58 | 2015-09-01 13:45:09 +0200 | [diff] [blame] | 953 | static const struct iio_chan_spec mma8453_channels[] = { |
Martin Kepplinger | e60378c | 2015-12-15 17:45:00 +0100 | [diff] [blame] | 954 | MMA8452_CHANNEL(X, idx_x, 10), |
| 955 | MMA8452_CHANNEL(Y, idx_y, 10), |
| 956 | MMA8452_CHANNEL(Z, idx_z, 10), |
| 957 | IIO_CHAN_SOFT_TIMESTAMP(idx_ts), |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame^] | 958 | MMA8452_FREEFALL_CHANNEL(IIO_MOD_X_AND_Y_AND_Z), |
Martin Kepplinger | c5ea1b58 | 2015-09-01 13:45:09 +0200 | [diff] [blame] | 959 | }; |
| 960 | |
Martin Kepplinger | 417e008 | 2015-09-01 13:45:11 +0200 | [diff] [blame] | 961 | static const struct iio_chan_spec mma8652_channels[] = { |
Martin Kepplinger | e60378c | 2015-12-15 17:45:00 +0100 | [diff] [blame] | 962 | MMA8652_CHANNEL(X, idx_x, 12), |
| 963 | MMA8652_CHANNEL(Y, idx_y, 12), |
| 964 | MMA8652_CHANNEL(Z, idx_z, 12), |
| 965 | IIO_CHAN_SOFT_TIMESTAMP(idx_ts), |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame^] | 966 | MMA8652_FREEFALL_CHANNEL(IIO_MOD_X_AND_Y_AND_Z), |
Martin Kepplinger | 417e008 | 2015-09-01 13:45:11 +0200 | [diff] [blame] | 967 | }; |
| 968 | |
| 969 | static const struct iio_chan_spec mma8653_channels[] = { |
Martin Kepplinger | e60378c | 2015-12-15 17:45:00 +0100 | [diff] [blame] | 970 | MMA8652_CHANNEL(X, idx_x, 10), |
| 971 | MMA8652_CHANNEL(Y, idx_y, 10), |
| 972 | MMA8652_CHANNEL(Z, idx_z, 10), |
| 973 | IIO_CHAN_SOFT_TIMESTAMP(idx_ts), |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame^] | 974 | MMA8652_FREEFALL_CHANNEL(IIO_MOD_X_AND_Y_AND_Z), |
Martin Kepplinger | 417e008 | 2015-09-01 13:45:11 +0200 | [diff] [blame] | 975 | }; |
| 976 | |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 977 | enum { |
| 978 | mma8452, |
Martin Kepplinger | c5ea1b58 | 2015-09-01 13:45:09 +0200 | [diff] [blame] | 979 | mma8453, |
Martin Kepplinger | 417e008 | 2015-09-01 13:45:11 +0200 | [diff] [blame] | 980 | mma8652, |
| 981 | mma8653, |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 982 | }; |
| 983 | |
| 984 | static const struct mma_chip_info mma_chip_info_table[] = { |
| 985 | [mma8452] = { |
| 986 | .chip_id = MMA8452_DEVICE_ID, |
| 987 | .channels = mma8452_channels, |
| 988 | .num_channels = ARRAY_SIZE(mma8452_channels), |
| 989 | /* |
| 990 | * Hardware has fullscale of -2G, -4G, -8G corresponding to |
| 991 | * raw value -2048 for 12 bit or -512 for 10 bit. |
| 992 | * The userspace interface uses m/s^2 and we declare micro units |
| 993 | * So scale factor for 12 bit here is given by: |
| 994 | * g * N * 1000000 / 2048 for N = 2, 4, 8 and g=9.80665 |
| 995 | */ |
| 996 | .mma_scales = { {0, 9577}, {0, 19154}, {0, 38307} }, |
| 997 | .ev_cfg = MMA8452_TRANSIENT_CFG, |
| 998 | .ev_cfg_ele = MMA8452_TRANSIENT_CFG_ELE, |
| 999 | .ev_cfg_chan_shift = 1, |
| 1000 | .ev_src = MMA8452_TRANSIENT_SRC, |
| 1001 | .ev_src_xe = MMA8452_TRANSIENT_SRC_XTRANSE, |
| 1002 | .ev_src_ye = MMA8452_TRANSIENT_SRC_YTRANSE, |
| 1003 | .ev_src_ze = MMA8452_TRANSIENT_SRC_ZTRANSE, |
| 1004 | .ev_ths = MMA8452_TRANSIENT_THS, |
| 1005 | .ev_ths_mask = MMA8452_TRANSIENT_THS_MASK, |
| 1006 | .ev_count = MMA8452_TRANSIENT_COUNT, |
| 1007 | }, |
Martin Kepplinger | c5ea1b58 | 2015-09-01 13:45:09 +0200 | [diff] [blame] | 1008 | [mma8453] = { |
| 1009 | .chip_id = MMA8453_DEVICE_ID, |
| 1010 | .channels = mma8453_channels, |
| 1011 | .num_channels = ARRAY_SIZE(mma8453_channels), |
| 1012 | .mma_scales = { {0, 38307}, {0, 76614}, {0, 153228} }, |
| 1013 | .ev_cfg = MMA8452_TRANSIENT_CFG, |
| 1014 | .ev_cfg_ele = MMA8452_TRANSIENT_CFG_ELE, |
| 1015 | .ev_cfg_chan_shift = 1, |
| 1016 | .ev_src = MMA8452_TRANSIENT_SRC, |
| 1017 | .ev_src_xe = MMA8452_TRANSIENT_SRC_XTRANSE, |
| 1018 | .ev_src_ye = MMA8452_TRANSIENT_SRC_YTRANSE, |
| 1019 | .ev_src_ze = MMA8452_TRANSIENT_SRC_ZTRANSE, |
| 1020 | .ev_ths = MMA8452_TRANSIENT_THS, |
| 1021 | .ev_ths_mask = MMA8452_TRANSIENT_THS_MASK, |
| 1022 | .ev_count = MMA8452_TRANSIENT_COUNT, |
| 1023 | }, |
Martin Kepplinger | 417e008 | 2015-09-01 13:45:11 +0200 | [diff] [blame] | 1024 | [mma8652] = { |
| 1025 | .chip_id = MMA8652_DEVICE_ID, |
| 1026 | .channels = mma8652_channels, |
| 1027 | .num_channels = ARRAY_SIZE(mma8652_channels), |
| 1028 | .mma_scales = { {0, 9577}, {0, 19154}, {0, 38307} }, |
| 1029 | .ev_cfg = MMA8452_FF_MT_CFG, |
| 1030 | .ev_cfg_ele = MMA8452_FF_MT_CFG_ELE, |
| 1031 | .ev_cfg_chan_shift = 3, |
| 1032 | .ev_src = MMA8452_FF_MT_SRC, |
| 1033 | .ev_src_xe = MMA8452_FF_MT_SRC_XHE, |
| 1034 | .ev_src_ye = MMA8452_FF_MT_SRC_YHE, |
| 1035 | .ev_src_ze = MMA8452_FF_MT_SRC_ZHE, |
| 1036 | .ev_ths = MMA8452_FF_MT_THS, |
| 1037 | .ev_ths_mask = MMA8452_FF_MT_THS_MASK, |
| 1038 | .ev_count = MMA8452_FF_MT_COUNT, |
| 1039 | }, |
| 1040 | [mma8653] = { |
| 1041 | .chip_id = MMA8653_DEVICE_ID, |
| 1042 | .channels = mma8653_channels, |
| 1043 | .num_channels = ARRAY_SIZE(mma8653_channels), |
| 1044 | .mma_scales = { {0, 38307}, {0, 76614}, {0, 153228} }, |
| 1045 | .ev_cfg = MMA8452_FF_MT_CFG, |
| 1046 | .ev_cfg_ele = MMA8452_FF_MT_CFG_ELE, |
| 1047 | .ev_cfg_chan_shift = 3, |
| 1048 | .ev_src = MMA8452_FF_MT_SRC, |
| 1049 | .ev_src_xe = MMA8452_FF_MT_SRC_XHE, |
| 1050 | .ev_src_ye = MMA8452_FF_MT_SRC_YHE, |
| 1051 | .ev_src_ze = MMA8452_FF_MT_SRC_ZHE, |
| 1052 | .ev_ths = MMA8452_FF_MT_THS, |
| 1053 | .ev_ths_mask = MMA8452_FF_MT_THS_MASK, |
| 1054 | .ev_count = MMA8452_FF_MT_COUNT, |
| 1055 | }, |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 1056 | }; |
| 1057 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1058 | static struct attribute *mma8452_attributes[] = { |
| 1059 | &iio_dev_attr_sampling_frequency_available.dev_attr.attr, |
| 1060 | &iio_dev_attr_in_accel_scale_available.dev_attr.attr, |
Martin Fuzzey | 1e79841 | 2015-06-01 15:39:56 +0200 | [diff] [blame] | 1061 | &iio_dev_attr_in_accel_filter_high_pass_3db_frequency_available.dev_attr.attr, |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1062 | NULL |
| 1063 | }; |
| 1064 | |
| 1065 | static const struct attribute_group mma8452_group = { |
| 1066 | .attrs = mma8452_attributes, |
| 1067 | }; |
| 1068 | |
| 1069 | static const struct iio_info mma8452_info = { |
| 1070 | .attrs = &mma8452_group, |
| 1071 | .read_raw = &mma8452_read_raw, |
| 1072 | .write_raw = &mma8452_write_raw, |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1073 | .event_attrs = &mma8452_event_attribute_group, |
| 1074 | .read_event_value = &mma8452_read_thresh, |
| 1075 | .write_event_value = &mma8452_write_thresh, |
| 1076 | .read_event_config = &mma8452_read_event_config, |
| 1077 | .write_event_config = &mma8452_write_event_config, |
Martin Fuzzey | 2a17698c | 2015-05-13 12:26:40 +0200 | [diff] [blame] | 1078 | .debugfs_reg_access = &mma8452_reg_access_dbg, |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1079 | .driver_module = THIS_MODULE, |
| 1080 | }; |
| 1081 | |
| 1082 | static const unsigned long mma8452_scan_masks[] = {0x7, 0}; |
| 1083 | |
Martin Fuzzey | ae6d9ce | 2015-06-01 15:39:58 +0200 | [diff] [blame] | 1084 | static int mma8452_data_rdy_trigger_set_state(struct iio_trigger *trig, |
| 1085 | bool state) |
| 1086 | { |
| 1087 | struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); |
| 1088 | struct mma8452_data *data = iio_priv(indio_dev); |
| 1089 | int reg; |
| 1090 | |
| 1091 | reg = i2c_smbus_read_byte_data(data->client, MMA8452_CTRL_REG4); |
| 1092 | if (reg < 0) |
| 1093 | return reg; |
| 1094 | |
| 1095 | if (state) |
| 1096 | reg |= MMA8452_INT_DRDY; |
| 1097 | else |
| 1098 | reg &= ~MMA8452_INT_DRDY; |
| 1099 | |
| 1100 | return mma8452_change_config(data, MMA8452_CTRL_REG4, reg); |
| 1101 | } |
| 1102 | |
| 1103 | static int mma8452_validate_device(struct iio_trigger *trig, |
| 1104 | struct iio_dev *indio_dev) |
| 1105 | { |
| 1106 | struct iio_dev *indio = iio_trigger_get_drvdata(trig); |
| 1107 | |
| 1108 | if (indio != indio_dev) |
| 1109 | return -EINVAL; |
| 1110 | |
| 1111 | return 0; |
| 1112 | } |
| 1113 | |
| 1114 | static const struct iio_trigger_ops mma8452_trigger_ops = { |
| 1115 | .set_trigger_state = mma8452_data_rdy_trigger_set_state, |
| 1116 | .validate_device = mma8452_validate_device, |
| 1117 | .owner = THIS_MODULE, |
| 1118 | }; |
| 1119 | |
| 1120 | static int mma8452_trigger_setup(struct iio_dev *indio_dev) |
| 1121 | { |
| 1122 | struct mma8452_data *data = iio_priv(indio_dev); |
| 1123 | struct iio_trigger *trig; |
| 1124 | int ret; |
| 1125 | |
| 1126 | trig = devm_iio_trigger_alloc(&data->client->dev, "%s-dev%d", |
| 1127 | indio_dev->name, |
| 1128 | indio_dev->id); |
| 1129 | if (!trig) |
| 1130 | return -ENOMEM; |
| 1131 | |
| 1132 | trig->dev.parent = &data->client->dev; |
| 1133 | trig->ops = &mma8452_trigger_ops; |
| 1134 | iio_trigger_set_drvdata(trig, indio_dev); |
| 1135 | |
| 1136 | ret = iio_trigger_register(trig); |
| 1137 | if (ret) |
| 1138 | return ret; |
| 1139 | |
| 1140 | indio_dev->trig = trig; |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 1141 | |
Martin Fuzzey | ae6d9ce | 2015-06-01 15:39:58 +0200 | [diff] [blame] | 1142 | return 0; |
| 1143 | } |
| 1144 | |
| 1145 | static void mma8452_trigger_cleanup(struct iio_dev *indio_dev) |
| 1146 | { |
| 1147 | if (indio_dev->trig) |
| 1148 | iio_trigger_unregister(indio_dev->trig); |
| 1149 | } |
| 1150 | |
Martin Fuzzey | ecabae7 | 2015-05-13 12:26:38 +0200 | [diff] [blame] | 1151 | static int mma8452_reset(struct i2c_client *client) |
| 1152 | { |
| 1153 | int i; |
| 1154 | int ret; |
| 1155 | |
| 1156 | ret = i2c_smbus_write_byte_data(client, MMA8452_CTRL_REG2, |
| 1157 | MMA8452_CTRL_REG2_RST); |
| 1158 | if (ret < 0) |
| 1159 | return ret; |
| 1160 | |
| 1161 | for (i = 0; i < 10; i++) { |
| 1162 | usleep_range(100, 200); |
| 1163 | ret = i2c_smbus_read_byte_data(client, MMA8452_CTRL_REG2); |
| 1164 | if (ret == -EIO) |
| 1165 | continue; /* I2C comm reset */ |
| 1166 | if (ret < 0) |
| 1167 | return ret; |
| 1168 | if (!(ret & MMA8452_CTRL_REG2_RST)) |
| 1169 | return 0; |
| 1170 | } |
| 1171 | |
| 1172 | return -ETIMEDOUT; |
| 1173 | } |
| 1174 | |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 1175 | static const struct of_device_id mma8452_dt_ids[] = { |
| 1176 | { .compatible = "fsl,mma8452", .data = &mma_chip_info_table[mma8452] }, |
Martin Kepplinger | c5ea1b58 | 2015-09-01 13:45:09 +0200 | [diff] [blame] | 1177 | { .compatible = "fsl,mma8453", .data = &mma_chip_info_table[mma8453] }, |
Martin Kepplinger | 417e008 | 2015-09-01 13:45:11 +0200 | [diff] [blame] | 1178 | { .compatible = "fsl,mma8652", .data = &mma_chip_info_table[mma8652] }, |
| 1179 | { .compatible = "fsl,mma8653", .data = &mma_chip_info_table[mma8653] }, |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 1180 | { } |
| 1181 | }; |
| 1182 | MODULE_DEVICE_TABLE(of, mma8452_dt_ids); |
| 1183 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1184 | static int mma8452_probe(struct i2c_client *client, |
| 1185 | const struct i2c_device_id *id) |
| 1186 | { |
| 1187 | struct mma8452_data *data; |
| 1188 | struct iio_dev *indio_dev; |
| 1189 | int ret; |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 1190 | const struct of_device_id *match; |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1191 | |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 1192 | match = of_match_device(mma8452_dt_ids, &client->dev); |
| 1193 | if (!match) { |
| 1194 | dev_err(&client->dev, "unknown device model\n"); |
| 1195 | return -ENODEV; |
| 1196 | } |
| 1197 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1198 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 1199 | if (!indio_dev) |
| 1200 | return -ENOMEM; |
| 1201 | |
| 1202 | data = iio_priv(indio_dev); |
| 1203 | data->client = client; |
| 1204 | mutex_init(&data->lock); |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 1205 | data->chip_info = match->data; |
| 1206 | |
Martin Kepplinger | 417e008 | 2015-09-01 13:45:11 +0200 | [diff] [blame] | 1207 | ret = i2c_smbus_read_byte_data(client, MMA8452_WHO_AM_I); |
| 1208 | if (ret < 0) |
| 1209 | return ret; |
| 1210 | |
| 1211 | switch (ret) { |
| 1212 | case MMA8452_DEVICE_ID: |
| 1213 | case MMA8453_DEVICE_ID: |
| 1214 | case MMA8652_DEVICE_ID: |
| 1215 | case MMA8653_DEVICE_ID: |
| 1216 | if (ret == data->chip_info->chip_id) |
| 1217 | break; |
| 1218 | default: |
| 1219 | return -ENODEV; |
| 1220 | } |
| 1221 | |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 1222 | dev_info(&client->dev, "registering %s accelerometer; ID 0x%x\n", |
| 1223 | match->compatible, data->chip_info->chip_id); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1224 | |
| 1225 | i2c_set_clientdata(client, indio_dev); |
| 1226 | indio_dev->info = &mma8452_info; |
| 1227 | indio_dev->name = id->name; |
| 1228 | indio_dev->dev.parent = &client->dev; |
| 1229 | indio_dev->modes = INDIO_DIRECT_MODE; |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 1230 | indio_dev->channels = data->chip_info->channels; |
| 1231 | indio_dev->num_channels = data->chip_info->num_channels; |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1232 | indio_dev->available_scan_masks = mma8452_scan_masks; |
| 1233 | |
Martin Fuzzey | ecabae7 | 2015-05-13 12:26:38 +0200 | [diff] [blame] | 1234 | ret = mma8452_reset(client); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1235 | if (ret < 0) |
| 1236 | return ret; |
| 1237 | |
| 1238 | data->data_cfg = MMA8452_DATA_CFG_FS_2G; |
| 1239 | ret = i2c_smbus_write_byte_data(client, MMA8452_DATA_CFG, |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 1240 | data->data_cfg); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1241 | if (ret < 0) |
| 1242 | return ret; |
| 1243 | |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1244 | /* |
| 1245 | * By default set transient threshold to max to avoid events if |
| 1246 | * enabling without configuring threshold. |
| 1247 | */ |
| 1248 | ret = i2c_smbus_write_byte_data(client, MMA8452_TRANSIENT_THS, |
| 1249 | MMA8452_TRANSIENT_THS_MASK); |
| 1250 | if (ret < 0) |
| 1251 | return ret; |
| 1252 | |
| 1253 | if (client->irq) { |
| 1254 | /* |
Martin Kepplinger | 60f562e | 2015-09-01 13:45:10 +0200 | [diff] [blame] | 1255 | * Although we enable the interrupt sources once and for |
| 1256 | * all here the event detection itself is not enabled until |
| 1257 | * userspace asks for it by mma8452_write_event_config() |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1258 | */ |
Martin Kepplinger | 60f562e | 2015-09-01 13:45:10 +0200 | [diff] [blame] | 1259 | int supported_interrupts = MMA8452_INT_DRDY | |
| 1260 | MMA8452_INT_TRANS | |
| 1261 | MMA8452_INT_FF_MT; |
| 1262 | int enabled_interrupts = MMA8452_INT_TRANS | |
| 1263 | MMA8452_INT_FF_MT; |
Martin Kepplinger | d2a3e09 | 2015-10-15 15:10:32 +0200 | [diff] [blame] | 1264 | int irq2; |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1265 | |
Martin Kepplinger | d2a3e09 | 2015-10-15 15:10:32 +0200 | [diff] [blame] | 1266 | irq2 = of_irq_get_byname(client->dev.of_node, "INT2"); |
| 1267 | |
| 1268 | if (irq2 == client->irq) { |
| 1269 | dev_dbg(&client->dev, "using interrupt line INT2\n"); |
| 1270 | } else { |
| 1271 | ret = i2c_smbus_write_byte_data(client, |
| 1272 | MMA8452_CTRL_REG5, |
| 1273 | supported_interrupts); |
| 1274 | if (ret < 0) |
| 1275 | return ret; |
| 1276 | |
| 1277 | dev_dbg(&client->dev, "using interrupt line INT1\n"); |
| 1278 | } |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1279 | |
| 1280 | ret = i2c_smbus_write_byte_data(client, |
| 1281 | MMA8452_CTRL_REG4, |
Martin Fuzzey | ae6d9ce | 2015-06-01 15:39:58 +0200 | [diff] [blame] | 1282 | enabled_interrupts); |
| 1283 | if (ret < 0) |
| 1284 | return ret; |
| 1285 | |
| 1286 | ret = mma8452_trigger_setup(indio_dev); |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1287 | if (ret < 0) |
| 1288 | return ret; |
| 1289 | } |
| 1290 | |
Martin Fuzzey | ecabae7 | 2015-05-13 12:26:38 +0200 | [diff] [blame] | 1291 | data->ctrl_reg1 = MMA8452_CTRL_ACTIVE | |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 1292 | (MMA8452_CTRL_DR_DEFAULT << MMA8452_CTRL_DR_SHIFT); |
Martin Fuzzey | ecabae7 | 2015-05-13 12:26:38 +0200 | [diff] [blame] | 1293 | ret = i2c_smbus_write_byte_data(client, MMA8452_CTRL_REG1, |
| 1294 | data->ctrl_reg1); |
| 1295 | if (ret < 0) |
Martin Fuzzey | ae6d9ce | 2015-06-01 15:39:58 +0200 | [diff] [blame] | 1296 | goto trigger_cleanup; |
Martin Fuzzey | ecabae7 | 2015-05-13 12:26:38 +0200 | [diff] [blame] | 1297 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1298 | ret = iio_triggered_buffer_setup(indio_dev, NULL, |
Hartmut Knaack | 686027f | 2015-08-02 22:43:51 +0200 | [diff] [blame] | 1299 | mma8452_trigger_handler, NULL); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1300 | if (ret < 0) |
Martin Fuzzey | ae6d9ce | 2015-06-01 15:39:58 +0200 | [diff] [blame] | 1301 | goto trigger_cleanup; |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1302 | |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1303 | if (client->irq) { |
| 1304 | ret = devm_request_threaded_irq(&client->dev, |
| 1305 | client->irq, |
| 1306 | NULL, mma8452_interrupt, |
| 1307 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, |
| 1308 | client->name, indio_dev); |
| 1309 | if (ret) |
| 1310 | goto buffer_cleanup; |
| 1311 | } |
| 1312 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1313 | ret = iio_device_register(indio_dev); |
| 1314 | if (ret < 0) |
| 1315 | goto buffer_cleanup; |
Martin Fuzzey | 28e3427 | 2015-06-01 15:39:52 +0200 | [diff] [blame] | 1316 | |
Martin Kepplinger | 4b04266 | 2016-01-16 15:35:20 +0100 | [diff] [blame^] | 1317 | ret = mma8452_set_freefall_mode(data, false); |
| 1318 | if (ret) |
| 1319 | return ret; |
| 1320 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1321 | return 0; |
| 1322 | |
| 1323 | buffer_cleanup: |
| 1324 | iio_triggered_buffer_cleanup(indio_dev); |
Martin Fuzzey | ae6d9ce | 2015-06-01 15:39:58 +0200 | [diff] [blame] | 1325 | |
| 1326 | trigger_cleanup: |
| 1327 | mma8452_trigger_cleanup(indio_dev); |
| 1328 | |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1329 | return ret; |
| 1330 | } |
| 1331 | |
| 1332 | static int mma8452_remove(struct i2c_client *client) |
| 1333 | { |
| 1334 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 1335 | |
| 1336 | iio_device_unregister(indio_dev); |
| 1337 | iio_triggered_buffer_cleanup(indio_dev); |
Martin Fuzzey | ae6d9ce | 2015-06-01 15:39:58 +0200 | [diff] [blame] | 1338 | mma8452_trigger_cleanup(indio_dev); |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1339 | mma8452_standby(iio_priv(indio_dev)); |
| 1340 | |
| 1341 | return 0; |
| 1342 | } |
| 1343 | |
| 1344 | #ifdef CONFIG_PM_SLEEP |
| 1345 | static int mma8452_suspend(struct device *dev) |
| 1346 | { |
| 1347 | return mma8452_standby(iio_priv(i2c_get_clientdata( |
| 1348 | to_i2c_client(dev)))); |
| 1349 | } |
| 1350 | |
| 1351 | static int mma8452_resume(struct device *dev) |
| 1352 | { |
| 1353 | return mma8452_active(iio_priv(i2c_get_clientdata( |
| 1354 | to_i2c_client(dev)))); |
| 1355 | } |
| 1356 | |
| 1357 | static SIMPLE_DEV_PM_OPS(mma8452_pm_ops, mma8452_suspend, mma8452_resume); |
| 1358 | #define MMA8452_PM_OPS (&mma8452_pm_ops) |
| 1359 | #else |
| 1360 | #define MMA8452_PM_OPS NULL |
| 1361 | #endif |
| 1362 | |
| 1363 | static const struct i2c_device_id mma8452_id[] = { |
Martin Kepplinger | c3cdd6e | 2015-09-01 13:45:08 +0200 | [diff] [blame] | 1364 | { "mma8452", mma8452 }, |
Martin Kepplinger | c5ea1b58 | 2015-09-01 13:45:09 +0200 | [diff] [blame] | 1365 | { "mma8453", mma8453 }, |
Martin Kepplinger | 417e008 | 2015-09-01 13:45:11 +0200 | [diff] [blame] | 1366 | { "mma8652", mma8652 }, |
| 1367 | { "mma8653", mma8653 }, |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1368 | { } |
| 1369 | }; |
| 1370 | MODULE_DEVICE_TABLE(i2c, mma8452_id); |
| 1371 | |
| 1372 | static struct i2c_driver mma8452_driver = { |
| 1373 | .driver = { |
| 1374 | .name = "mma8452", |
Martin Fuzzey | a3fb96a | 2014-11-07 14:06:00 +0000 | [diff] [blame] | 1375 | .of_match_table = of_match_ptr(mma8452_dt_ids), |
Peter Meerwald | c7eeea9 | 2014-02-05 09:51:00 +0000 | [diff] [blame] | 1376 | .pm = MMA8452_PM_OPS, |
| 1377 | }, |
| 1378 | .probe = mma8452_probe, |
| 1379 | .remove = mma8452_remove, |
| 1380 | .id_table = mma8452_id, |
| 1381 | }; |
| 1382 | module_i2c_driver(mma8452_driver); |
| 1383 | |
| 1384 | MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>"); |
| 1385 | MODULE_DESCRIPTION("Freescale MMA8452 accelerometer driver"); |
| 1386 | MODULE_LICENSE("GPL"); |