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