Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1 | /* |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 2 | * 3-axis accelerometer driver supporting following Bosch-Sensortec chips: |
| 3 | * - BMC150 |
| 4 | * - BMI055 |
| 5 | * - BMA255 |
| 6 | * - BMA250E |
| 7 | * - BMA222E |
| 8 | * - BMA280 |
| 9 | * |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 10 | * Copyright (c) 2014, Intel Corporation. |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify it |
| 13 | * under the terms and conditions of the GNU General Public License, |
| 14 | * version 2, as published by the Free Software Foundation. |
| 15 | * |
| 16 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 17 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 19 | * more details. |
| 20 | */ |
| 21 | |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/i2c.h> |
| 24 | #include <linux/interrupt.h> |
| 25 | #include <linux/delay.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/acpi.h> |
| 28 | #include <linux/gpio/consumer.h> |
| 29 | #include <linux/pm.h> |
| 30 | #include <linux/pm_runtime.h> |
| 31 | #include <linux/iio/iio.h> |
| 32 | #include <linux/iio/sysfs.h> |
| 33 | #include <linux/iio/buffer.h> |
| 34 | #include <linux/iio/events.h> |
| 35 | #include <linux/iio/trigger.h> |
| 36 | #include <linux/iio/trigger_consumer.h> |
| 37 | #include <linux/iio/triggered_buffer.h> |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 38 | #include <linux/regmap.h> |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 39 | |
| 40 | #define BMC150_ACCEL_DRV_NAME "bmc150_accel" |
| 41 | #define BMC150_ACCEL_IRQ_NAME "bmc150_accel_event" |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 42 | |
| 43 | #define BMC150_ACCEL_REG_CHIP_ID 0x00 |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 44 | |
| 45 | #define BMC150_ACCEL_REG_INT_STATUS_2 0x0B |
| 46 | #define BMC150_ACCEL_ANY_MOTION_MASK 0x07 |
Srinivas Pandruvada | 8d5a978 | 2014-10-10 20:35:32 -0700 | [diff] [blame] | 47 | #define BMC150_ACCEL_ANY_MOTION_BIT_X BIT(0) |
| 48 | #define BMC150_ACCEL_ANY_MOTION_BIT_Y BIT(1) |
| 49 | #define BMC150_ACCEL_ANY_MOTION_BIT_Z BIT(2) |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 50 | #define BMC150_ACCEL_ANY_MOTION_BIT_SIGN BIT(3) |
| 51 | |
| 52 | #define BMC150_ACCEL_REG_PMU_LPW 0x11 |
| 53 | #define BMC150_ACCEL_PMU_MODE_MASK 0xE0 |
| 54 | #define BMC150_ACCEL_PMU_MODE_SHIFT 5 |
| 55 | #define BMC150_ACCEL_PMU_BIT_SLEEP_DUR_MASK 0x17 |
| 56 | #define BMC150_ACCEL_PMU_BIT_SLEEP_DUR_SHIFT 1 |
| 57 | |
| 58 | #define BMC150_ACCEL_REG_PMU_RANGE 0x0F |
| 59 | |
| 60 | #define BMC150_ACCEL_DEF_RANGE_2G 0x03 |
| 61 | #define BMC150_ACCEL_DEF_RANGE_4G 0x05 |
| 62 | #define BMC150_ACCEL_DEF_RANGE_8G 0x08 |
| 63 | #define BMC150_ACCEL_DEF_RANGE_16G 0x0C |
| 64 | |
| 65 | /* Default BW: 125Hz */ |
| 66 | #define BMC150_ACCEL_REG_PMU_BW 0x10 |
| 67 | #define BMC150_ACCEL_DEF_BW 125 |
| 68 | |
| 69 | #define BMC150_ACCEL_REG_INT_MAP_0 0x19 |
| 70 | #define BMC150_ACCEL_INT_MAP_0_BIT_SLOPE BIT(2) |
| 71 | |
| 72 | #define BMC150_ACCEL_REG_INT_MAP_1 0x1A |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 73 | #define BMC150_ACCEL_INT_MAP_1_BIT_DATA BIT(0) |
| 74 | #define BMC150_ACCEL_INT_MAP_1_BIT_FWM BIT(1) |
| 75 | #define BMC150_ACCEL_INT_MAP_1_BIT_FFULL BIT(2) |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 76 | |
| 77 | #define BMC150_ACCEL_REG_INT_RST_LATCH 0x21 |
| 78 | #define BMC150_ACCEL_INT_MODE_LATCH_RESET 0x80 |
| 79 | #define BMC150_ACCEL_INT_MODE_LATCH_INT 0x0F |
| 80 | #define BMC150_ACCEL_INT_MODE_NON_LATCH_INT 0x00 |
| 81 | |
| 82 | #define BMC150_ACCEL_REG_INT_EN_0 0x16 |
| 83 | #define BMC150_ACCEL_INT_EN_BIT_SLP_X BIT(0) |
| 84 | #define BMC150_ACCEL_INT_EN_BIT_SLP_Y BIT(1) |
| 85 | #define BMC150_ACCEL_INT_EN_BIT_SLP_Z BIT(2) |
| 86 | |
| 87 | #define BMC150_ACCEL_REG_INT_EN_1 0x17 |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 88 | #define BMC150_ACCEL_INT_EN_BIT_DATA_EN BIT(4) |
| 89 | #define BMC150_ACCEL_INT_EN_BIT_FFULL_EN BIT(5) |
| 90 | #define BMC150_ACCEL_INT_EN_BIT_FWM_EN BIT(6) |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 91 | |
| 92 | #define BMC150_ACCEL_REG_INT_OUT_CTRL 0x20 |
| 93 | #define BMC150_ACCEL_INT_OUT_CTRL_INT1_LVL BIT(0) |
| 94 | |
| 95 | #define BMC150_ACCEL_REG_INT_5 0x27 |
| 96 | #define BMC150_ACCEL_SLOPE_DUR_MASK 0x03 |
| 97 | |
| 98 | #define BMC150_ACCEL_REG_INT_6 0x28 |
| 99 | #define BMC150_ACCEL_SLOPE_THRES_MASK 0xFF |
| 100 | |
| 101 | /* Slope duration in terms of number of samples */ |
Srinivas Pandruvada | 9e8e228 | 2014-10-10 20:35:33 -0700 | [diff] [blame] | 102 | #define BMC150_ACCEL_DEF_SLOPE_DURATION 1 |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 103 | /* in terms of multiples of g's/LSB, based on range */ |
Srinivas Pandruvada | 9e8e228 | 2014-10-10 20:35:33 -0700 | [diff] [blame] | 104 | #define BMC150_ACCEL_DEF_SLOPE_THRESHOLD 1 |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 105 | |
| 106 | #define BMC150_ACCEL_REG_XOUT_L 0x02 |
| 107 | |
| 108 | #define BMC150_ACCEL_MAX_STARTUP_TIME_MS 100 |
| 109 | |
| 110 | /* Sleep Duration values */ |
| 111 | #define BMC150_ACCEL_SLEEP_500_MICRO 0x05 |
| 112 | #define BMC150_ACCEL_SLEEP_1_MS 0x06 |
| 113 | #define BMC150_ACCEL_SLEEP_2_MS 0x07 |
| 114 | #define BMC150_ACCEL_SLEEP_4_MS 0x08 |
| 115 | #define BMC150_ACCEL_SLEEP_6_MS 0x09 |
| 116 | #define BMC150_ACCEL_SLEEP_10_MS 0x0A |
| 117 | #define BMC150_ACCEL_SLEEP_25_MS 0x0B |
| 118 | #define BMC150_ACCEL_SLEEP_50_MS 0x0C |
| 119 | #define BMC150_ACCEL_SLEEP_100_MS 0x0D |
| 120 | #define BMC150_ACCEL_SLEEP_500_MS 0x0E |
| 121 | #define BMC150_ACCEL_SLEEP_1_SEC 0x0F |
| 122 | |
| 123 | #define BMC150_ACCEL_REG_TEMP 0x08 |
| 124 | #define BMC150_ACCEL_TEMP_CENTER_VAL 24 |
| 125 | |
| 126 | #define BMC150_ACCEL_AXIS_TO_REG(axis) (BMC150_ACCEL_REG_XOUT_L + (axis * 2)) |
| 127 | #define BMC150_AUTO_SUSPEND_DELAY_MS 2000 |
| 128 | |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 129 | #define BMC150_ACCEL_REG_FIFO_STATUS 0x0E |
| 130 | #define BMC150_ACCEL_REG_FIFO_CONFIG0 0x30 |
| 131 | #define BMC150_ACCEL_REG_FIFO_CONFIG1 0x3E |
| 132 | #define BMC150_ACCEL_REG_FIFO_DATA 0x3F |
| 133 | #define BMC150_ACCEL_FIFO_LENGTH 32 |
| 134 | |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 135 | enum bmc150_accel_axis { |
| 136 | AXIS_X, |
| 137 | AXIS_Y, |
| 138 | AXIS_Z, |
| 139 | }; |
| 140 | |
| 141 | enum bmc150_power_modes { |
| 142 | BMC150_ACCEL_SLEEP_MODE_NORMAL, |
| 143 | BMC150_ACCEL_SLEEP_MODE_DEEP_SUSPEND, |
| 144 | BMC150_ACCEL_SLEEP_MODE_LPM, |
| 145 | BMC150_ACCEL_SLEEP_MODE_SUSPEND = 0x04, |
| 146 | }; |
| 147 | |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 148 | struct bmc150_scale_info { |
| 149 | int scale; |
| 150 | u8 reg_range; |
| 151 | }; |
| 152 | |
| 153 | struct bmc150_accel_chip_info { |
Bastien Nocera | 0ad4bf3 | 2015-07-23 17:21:07 +0200 | [diff] [blame] | 154 | const char *name; |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 155 | u8 chip_id; |
| 156 | const struct iio_chan_spec *channels; |
| 157 | int num_channels; |
| 158 | const struct bmc150_scale_info scale_table[4]; |
| 159 | }; |
| 160 | |
Octavian Purdila | 3e825ec | 2015-03-03 18:17:57 +0200 | [diff] [blame] | 161 | struct bmc150_accel_interrupt { |
| 162 | const struct bmc150_accel_interrupt_info *info; |
| 163 | atomic_t users; |
| 164 | }; |
| 165 | |
Octavian Purdila | 7d96321 | 2015-03-03 18:17:58 +0200 | [diff] [blame] | 166 | struct bmc150_accel_trigger { |
| 167 | struct bmc150_accel_data *data; |
| 168 | struct iio_trigger *indio_trig; |
| 169 | int (*setup)(struct bmc150_accel_trigger *t, bool state); |
| 170 | int intr; |
| 171 | bool enabled; |
| 172 | }; |
| 173 | |
Octavian Purdila | 3e825ec | 2015-03-03 18:17:57 +0200 | [diff] [blame] | 174 | enum bmc150_accel_interrupt_id { |
| 175 | BMC150_ACCEL_INT_DATA_READY, |
| 176 | BMC150_ACCEL_INT_ANY_MOTION, |
| 177 | BMC150_ACCEL_INT_WATERMARK, |
| 178 | BMC150_ACCEL_INTERRUPTS, |
| 179 | }; |
| 180 | |
Octavian Purdila | 7d96321 | 2015-03-03 18:17:58 +0200 | [diff] [blame] | 181 | enum bmc150_accel_trigger_id { |
| 182 | BMC150_ACCEL_TRIGGER_DATA_READY, |
| 183 | BMC150_ACCEL_TRIGGER_ANY_MOTION, |
| 184 | BMC150_ACCEL_TRIGGERS, |
| 185 | }; |
| 186 | |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 187 | struct bmc150_accel_data { |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 188 | struct regmap *regmap; |
| 189 | struct device *dev; |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 190 | int irq; |
Octavian Purdila | 3e825ec | 2015-03-03 18:17:57 +0200 | [diff] [blame] | 191 | struct bmc150_accel_interrupt interrupts[BMC150_ACCEL_INTERRUPTS]; |
Octavian Purdila | 3e825ec | 2015-03-03 18:17:57 +0200 | [diff] [blame] | 192 | atomic_t active_intr; |
Octavian Purdila | 7d96321 | 2015-03-03 18:17:58 +0200 | [diff] [blame] | 193 | struct bmc150_accel_trigger triggers[BMC150_ACCEL_TRIGGERS]; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 194 | struct mutex mutex; |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 195 | u8 fifo_mode, watermark; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 196 | s16 buffer[8]; |
| 197 | u8 bw_bits; |
| 198 | u32 slope_dur; |
| 199 | u32 slope_thres; |
| 200 | u32 range; |
| 201 | int ev_enable_state; |
Vlad Dogaru | c16bff4 | 2015-05-12 17:03:24 +0300 | [diff] [blame] | 202 | int64_t timestamp, old_timestamp; /* Only used in hw fifo mode. */ |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 203 | const struct bmc150_accel_chip_info *chip_info; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 204 | }; |
| 205 | |
| 206 | static const struct { |
| 207 | int val; |
| 208 | int val2; |
| 209 | u8 bw_bits; |
Sathyanarayanan Kuppuswamy | 0ba8da9 | 2015-03-03 18:17:56 +0200 | [diff] [blame] | 210 | } bmc150_accel_samp_freq_table[] = { {15, 620000, 0x08}, |
| 211 | {31, 260000, 0x09}, |
| 212 | {62, 500000, 0x0A}, |
| 213 | {125, 0, 0x0B}, |
| 214 | {250, 0, 0x0C}, |
| 215 | {500, 0, 0x0D}, |
| 216 | {1000, 0, 0x0E}, |
| 217 | {2000, 0, 0x0F} }; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 218 | |
| 219 | static const struct { |
| 220 | int bw_bits; |
| 221 | int msec; |
| 222 | } bmc150_accel_sample_upd_time[] = { {0x08, 64}, |
| 223 | {0x09, 32}, |
| 224 | {0x0A, 16}, |
| 225 | {0x0B, 8}, |
| 226 | {0x0C, 4}, |
| 227 | {0x0D, 2}, |
| 228 | {0x0E, 1}, |
| 229 | {0x0F, 1} }; |
| 230 | |
| 231 | static const struct { |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 232 | int sleep_dur; |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 233 | u8 reg_value; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 234 | } bmc150_accel_sleep_value_table[] = { {0, 0}, |
| 235 | {500, BMC150_ACCEL_SLEEP_500_MICRO}, |
| 236 | {1000, BMC150_ACCEL_SLEEP_1_MS}, |
| 237 | {2000, BMC150_ACCEL_SLEEP_2_MS}, |
| 238 | {4000, BMC150_ACCEL_SLEEP_4_MS}, |
| 239 | {6000, BMC150_ACCEL_SLEEP_6_MS}, |
| 240 | {10000, BMC150_ACCEL_SLEEP_10_MS}, |
| 241 | {25000, BMC150_ACCEL_SLEEP_25_MS}, |
| 242 | {50000, BMC150_ACCEL_SLEEP_50_MS}, |
| 243 | {100000, BMC150_ACCEL_SLEEP_100_MS}, |
| 244 | {500000, BMC150_ACCEL_SLEEP_500_MS}, |
| 245 | {1000000, BMC150_ACCEL_SLEEP_1_SEC} }; |
| 246 | |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 247 | static const struct regmap_config bmc150_i2c_regmap_conf = { |
| 248 | .reg_bits = 8, |
| 249 | .val_bits = 8, |
| 250 | .max_register = 0x3f, |
| 251 | }; |
| 252 | |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 253 | static int bmc150_accel_set_mode(struct bmc150_accel_data *data, |
| 254 | enum bmc150_power_modes mode, |
| 255 | int dur_us) |
| 256 | { |
| 257 | int i; |
| 258 | int ret; |
| 259 | u8 lpw_bits; |
| 260 | int dur_val = -1; |
| 261 | |
| 262 | if (dur_us > 0) { |
| 263 | for (i = 0; i < ARRAY_SIZE(bmc150_accel_sleep_value_table); |
| 264 | ++i) { |
| 265 | if (bmc150_accel_sleep_value_table[i].sleep_dur == |
| 266 | dur_us) |
| 267 | dur_val = |
| 268 | bmc150_accel_sleep_value_table[i].reg_value; |
| 269 | } |
Hartmut Knaack | e20008e | 2015-06-15 23:48:26 +0200 | [diff] [blame] | 270 | } else { |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 271 | dur_val = 0; |
Hartmut Knaack | e20008e | 2015-06-15 23:48:26 +0200 | [diff] [blame] | 272 | } |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 273 | |
| 274 | if (dur_val < 0) |
| 275 | return -EINVAL; |
| 276 | |
| 277 | lpw_bits = mode << BMC150_ACCEL_PMU_MODE_SHIFT; |
| 278 | lpw_bits |= (dur_val << BMC150_ACCEL_PMU_BIT_SLEEP_DUR_SHIFT); |
| 279 | |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 280 | dev_dbg(data->dev, "Set Mode bits %x\n", lpw_bits); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 281 | |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 282 | ret = regmap_write(data->regmap, BMC150_ACCEL_REG_PMU_LPW, lpw_bits); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 283 | if (ret < 0) { |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 284 | dev_err(data->dev, "Error writing reg_pmu_lpw\n"); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 285 | return ret; |
| 286 | } |
| 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | static int bmc150_accel_set_bw(struct bmc150_accel_data *data, int val, |
| 292 | int val2) |
| 293 | { |
| 294 | int i; |
| 295 | int ret; |
| 296 | |
| 297 | for (i = 0; i < ARRAY_SIZE(bmc150_accel_samp_freq_table); ++i) { |
| 298 | if (bmc150_accel_samp_freq_table[i].val == val && |
Hartmut Knaack | e20008e | 2015-06-15 23:48:26 +0200 | [diff] [blame] | 299 | bmc150_accel_samp_freq_table[i].val2 == val2) { |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 300 | ret = regmap_write(data->regmap, |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 301 | BMC150_ACCEL_REG_PMU_BW, |
| 302 | bmc150_accel_samp_freq_table[i].bw_bits); |
| 303 | if (ret < 0) |
| 304 | return ret; |
| 305 | |
| 306 | data->bw_bits = |
| 307 | bmc150_accel_samp_freq_table[i].bw_bits; |
| 308 | return 0; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | return -EINVAL; |
| 313 | } |
| 314 | |
Octavian Purdila | 802a3ae | 2015-01-31 02:00:03 +0200 | [diff] [blame] | 315 | static int bmc150_accel_update_slope(struct bmc150_accel_data *data) |
| 316 | { |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 317 | int ret; |
Octavian Purdila | 802a3ae | 2015-01-31 02:00:03 +0200 | [diff] [blame] | 318 | |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 319 | ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_6, |
Octavian Purdila | 802a3ae | 2015-01-31 02:00:03 +0200 | [diff] [blame] | 320 | data->slope_thres); |
| 321 | if (ret < 0) { |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 322 | dev_err(data->dev, "Error writing reg_int_6\n"); |
Octavian Purdila | 802a3ae | 2015-01-31 02:00:03 +0200 | [diff] [blame] | 323 | return ret; |
| 324 | } |
| 325 | |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 326 | ret = regmap_update_bits(data->regmap, BMC150_ACCEL_REG_INT_5, |
| 327 | BMC150_ACCEL_SLOPE_DUR_MASK, data->slope_dur); |
Octavian Purdila | 802a3ae | 2015-01-31 02:00:03 +0200 | [diff] [blame] | 328 | if (ret < 0) { |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 329 | dev_err(data->dev, "Error updating reg_int_5\n"); |
Octavian Purdila | 802a3ae | 2015-01-31 02:00:03 +0200 | [diff] [blame] | 330 | return ret; |
| 331 | } |
| 332 | |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 333 | dev_dbg(data->dev, "%s: %x %x\n", __func__, data->slope_thres, |
Octavian Purdila | 802a3ae | 2015-01-31 02:00:03 +0200 | [diff] [blame] | 334 | data->slope_dur); |
| 335 | |
| 336 | return ret; |
| 337 | } |
| 338 | |
Octavian Purdila | 7d96321 | 2015-03-03 18:17:58 +0200 | [diff] [blame] | 339 | static int bmc150_accel_any_motion_setup(struct bmc150_accel_trigger *t, |
| 340 | bool state) |
| 341 | { |
| 342 | if (state) |
| 343 | return bmc150_accel_update_slope(t->data); |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 348 | static int bmc150_accel_get_bw(struct bmc150_accel_data *data, int *val, |
| 349 | int *val2) |
| 350 | { |
| 351 | int i; |
| 352 | |
| 353 | for (i = 0; i < ARRAY_SIZE(bmc150_accel_samp_freq_table); ++i) { |
| 354 | if (bmc150_accel_samp_freq_table[i].bw_bits == data->bw_bits) { |
| 355 | *val = bmc150_accel_samp_freq_table[i].val; |
| 356 | *val2 = bmc150_accel_samp_freq_table[i].val2; |
| 357 | return IIO_VAL_INT_PLUS_MICRO; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | return -EINVAL; |
| 362 | } |
| 363 | |
Rafael J. Wysocki | 6f0a13f | 2014-12-04 01:08:13 +0100 | [diff] [blame] | 364 | #ifdef CONFIG_PM |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 365 | static int bmc150_accel_get_startup_times(struct bmc150_accel_data *data) |
| 366 | { |
| 367 | int i; |
| 368 | |
| 369 | for (i = 0; i < ARRAY_SIZE(bmc150_accel_sample_upd_time); ++i) { |
| 370 | if (bmc150_accel_sample_upd_time[i].bw_bits == data->bw_bits) |
| 371 | return bmc150_accel_sample_upd_time[i].msec; |
| 372 | } |
| 373 | |
| 374 | return BMC150_ACCEL_MAX_STARTUP_TIME_MS; |
| 375 | } |
| 376 | |
| 377 | static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on) |
| 378 | { |
| 379 | int ret; |
| 380 | |
Hartmut Knaack | e20008e | 2015-06-15 23:48:26 +0200 | [diff] [blame] | 381 | if (on) { |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 382 | ret = pm_runtime_get_sync(data->dev); |
Hartmut Knaack | e20008e | 2015-06-15 23:48:26 +0200 | [diff] [blame] | 383 | } else { |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 384 | pm_runtime_mark_last_busy(data->dev); |
| 385 | ret = pm_runtime_put_autosuspend(data->dev); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 386 | } |
Hartmut Knaack | e20008e | 2015-06-15 23:48:26 +0200 | [diff] [blame] | 387 | |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 388 | if (ret < 0) { |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 389 | dev_err(data->dev, |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 390 | "Failed: bmc150_accel_set_power_state for %d\n", on); |
Srinivas Pandruvada | aaeecd8 | 2014-10-10 20:35:31 -0700 | [diff] [blame] | 391 | if (on) |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 392 | pm_runtime_put_noidle(data->dev); |
Srinivas Pandruvada | aaeecd8 | 2014-10-10 20:35:31 -0700 | [diff] [blame] | 393 | |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 394 | return ret; |
| 395 | } |
| 396 | |
| 397 | return 0; |
| 398 | } |
Laurentiu Palcu | b31b05c | 2014-08-29 09:38:00 +0100 | [diff] [blame] | 399 | #else |
| 400 | static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on) |
| 401 | { |
| 402 | return 0; |
| 403 | } |
| 404 | #endif |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 405 | |
Octavian Purdila | 8e22f47 | 2015-01-31 02:00:04 +0200 | [diff] [blame] | 406 | static const struct bmc150_accel_interrupt_info { |
| 407 | u8 map_reg; |
| 408 | u8 map_bitmask; |
| 409 | u8 en_reg; |
| 410 | u8 en_bitmask; |
Octavian Purdila | 3e825ec | 2015-03-03 18:17:57 +0200 | [diff] [blame] | 411 | } bmc150_accel_interrupts[BMC150_ACCEL_INTERRUPTS] = { |
Octavian Purdila | 8e22f47 | 2015-01-31 02:00:04 +0200 | [diff] [blame] | 412 | { /* data ready interrupt */ |
| 413 | .map_reg = BMC150_ACCEL_REG_INT_MAP_1, |
| 414 | .map_bitmask = BMC150_ACCEL_INT_MAP_1_BIT_DATA, |
| 415 | .en_reg = BMC150_ACCEL_REG_INT_EN_1, |
| 416 | .en_bitmask = BMC150_ACCEL_INT_EN_BIT_DATA_EN, |
| 417 | }, |
| 418 | { /* motion interrupt */ |
| 419 | .map_reg = BMC150_ACCEL_REG_INT_MAP_0, |
| 420 | .map_bitmask = BMC150_ACCEL_INT_MAP_0_BIT_SLOPE, |
| 421 | .en_reg = BMC150_ACCEL_REG_INT_EN_0, |
| 422 | .en_bitmask = BMC150_ACCEL_INT_EN_BIT_SLP_X | |
| 423 | BMC150_ACCEL_INT_EN_BIT_SLP_Y | |
| 424 | BMC150_ACCEL_INT_EN_BIT_SLP_Z |
| 425 | }, |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 426 | { /* fifo watermark interrupt */ |
| 427 | .map_reg = BMC150_ACCEL_REG_INT_MAP_1, |
| 428 | .map_bitmask = BMC150_ACCEL_INT_MAP_1_BIT_FWM, |
| 429 | .en_reg = BMC150_ACCEL_REG_INT_EN_1, |
| 430 | .en_bitmask = BMC150_ACCEL_INT_EN_BIT_FWM_EN, |
| 431 | }, |
Octavian Purdila | 8e22f47 | 2015-01-31 02:00:04 +0200 | [diff] [blame] | 432 | }; |
| 433 | |
Octavian Purdila | 3e825ec | 2015-03-03 18:17:57 +0200 | [diff] [blame] | 434 | static void bmc150_accel_interrupts_setup(struct iio_dev *indio_dev, |
| 435 | struct bmc150_accel_data *data) |
| 436 | { |
| 437 | int i; |
| 438 | |
| 439 | for (i = 0; i < BMC150_ACCEL_INTERRUPTS; i++) |
| 440 | data->interrupts[i].info = &bmc150_accel_interrupts[i]; |
| 441 | } |
| 442 | |
| 443 | static int bmc150_accel_set_interrupt(struct bmc150_accel_data *data, int i, |
Octavian Purdila | 8e22f47 | 2015-01-31 02:00:04 +0200 | [diff] [blame] | 444 | bool state) |
| 445 | { |
Octavian Purdila | 3e825ec | 2015-03-03 18:17:57 +0200 | [diff] [blame] | 446 | struct bmc150_accel_interrupt *intr = &data->interrupts[i]; |
| 447 | const struct bmc150_accel_interrupt_info *info = intr->info; |
Octavian Purdila | 8e22f47 | 2015-01-31 02:00:04 +0200 | [diff] [blame] | 448 | int ret; |
| 449 | |
Octavian Purdila | 3e825ec | 2015-03-03 18:17:57 +0200 | [diff] [blame] | 450 | if (state) { |
| 451 | if (atomic_inc_return(&intr->users) > 1) |
| 452 | return 0; |
| 453 | } else { |
| 454 | if (atomic_dec_return(&intr->users) > 0) |
| 455 | return 0; |
| 456 | } |
| 457 | |
Octavian Purdila | 8e22f47 | 2015-01-31 02:00:04 +0200 | [diff] [blame] | 458 | /* |
Hartmut Knaack | e20008e | 2015-06-15 23:48:26 +0200 | [diff] [blame] | 459 | * We will expect the enable and disable to do operation in reverse |
| 460 | * order. This will happen here anyway, as our resume operation uses |
| 461 | * sync mode runtime pm calls. The suspend operation will be delayed |
| 462 | * by autosuspend delay. |
| 463 | * So the disable operation will still happen in reverse order of |
| 464 | * enable operation. When runtime pm is disabled the mode is always on, |
| 465 | * so sequence doesn't matter. |
Octavian Purdila | 8e22f47 | 2015-01-31 02:00:04 +0200 | [diff] [blame] | 466 | */ |
| 467 | ret = bmc150_accel_set_power_state(data, state); |
| 468 | if (ret < 0) |
| 469 | return ret; |
| 470 | |
| 471 | /* map the interrupt to the appropriate pins */ |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 472 | ret = regmap_update_bits(data->regmap, info->map_reg, info->map_bitmask, |
| 473 | (state ? info->map_bitmask : 0)); |
Octavian Purdila | 8e22f47 | 2015-01-31 02:00:04 +0200 | [diff] [blame] | 474 | if (ret < 0) { |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 475 | dev_err(data->dev, "Error updating reg_int_map\n"); |
Octavian Purdila | 8e22f47 | 2015-01-31 02:00:04 +0200 | [diff] [blame] | 476 | goto out_fix_power_state; |
| 477 | } |
| 478 | |
| 479 | /* enable/disable the interrupt */ |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 480 | ret = regmap_update_bits(data->regmap, info->en_reg, info->en_bitmask, |
| 481 | (state ? info->en_bitmask : 0)); |
Octavian Purdila | 8e22f47 | 2015-01-31 02:00:04 +0200 | [diff] [blame] | 482 | if (ret < 0) { |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 483 | dev_err(data->dev, "Error updating reg_int_en\n"); |
Octavian Purdila | 8e22f47 | 2015-01-31 02:00:04 +0200 | [diff] [blame] | 484 | goto out_fix_power_state; |
| 485 | } |
| 486 | |
Octavian Purdila | 3e825ec | 2015-03-03 18:17:57 +0200 | [diff] [blame] | 487 | if (state) |
| 488 | atomic_inc(&data->active_intr); |
| 489 | else |
| 490 | atomic_dec(&data->active_intr); |
| 491 | |
Octavian Purdila | 8e22f47 | 2015-01-31 02:00:04 +0200 | [diff] [blame] | 492 | return 0; |
| 493 | |
| 494 | out_fix_power_state: |
| 495 | bmc150_accel_set_power_state(data, false); |
| 496 | return ret; |
| 497 | } |
| 498 | |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 499 | static int bmc150_accel_set_scale(struct bmc150_accel_data *data, int val) |
| 500 | { |
| 501 | int ret, i; |
| 502 | |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 503 | for (i = 0; i < ARRAY_SIZE(data->chip_info->scale_table); ++i) { |
| 504 | if (data->chip_info->scale_table[i].scale == val) { |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 505 | ret = regmap_write(data->regmap, |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 506 | BMC150_ACCEL_REG_PMU_RANGE, |
| 507 | data->chip_info->scale_table[i].reg_range); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 508 | if (ret < 0) { |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 509 | dev_err(data->dev, |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 510 | "Error writing pmu_range\n"); |
| 511 | return ret; |
| 512 | } |
| 513 | |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 514 | data->range = data->chip_info->scale_table[i].reg_range; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 515 | return 0; |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | return -EINVAL; |
| 520 | } |
| 521 | |
| 522 | static int bmc150_accel_get_temp(struct bmc150_accel_data *data, int *val) |
| 523 | { |
| 524 | int ret; |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 525 | unsigned int value; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 526 | |
| 527 | mutex_lock(&data->mutex); |
| 528 | |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 529 | ret = regmap_read(data->regmap, BMC150_ACCEL_REG_TEMP, &value); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 530 | if (ret < 0) { |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 531 | dev_err(data->dev, "Error reading reg_temp\n"); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 532 | mutex_unlock(&data->mutex); |
| 533 | return ret; |
| 534 | } |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 535 | *val = sign_extend32(value, 7); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 536 | |
| 537 | mutex_unlock(&data->mutex); |
| 538 | |
| 539 | return IIO_VAL_INT; |
| 540 | } |
| 541 | |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 542 | static int bmc150_accel_get_axis(struct bmc150_accel_data *data, |
| 543 | struct iio_chan_spec const *chan, |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 544 | int *val) |
| 545 | { |
| 546 | int ret; |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 547 | int axis = chan->scan_index; |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 548 | unsigned int raw_val; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 549 | |
| 550 | mutex_lock(&data->mutex); |
| 551 | ret = bmc150_accel_set_power_state(data, true); |
| 552 | if (ret < 0) { |
| 553 | mutex_unlock(&data->mutex); |
| 554 | return ret; |
| 555 | } |
| 556 | |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 557 | ret = regmap_bulk_read(data->regmap, BMC150_ACCEL_AXIS_TO_REG(axis), |
| 558 | &raw_val, 2); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 559 | if (ret < 0) { |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 560 | dev_err(data->dev, "Error reading axis %d\n", axis); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 561 | bmc150_accel_set_power_state(data, false); |
| 562 | mutex_unlock(&data->mutex); |
| 563 | return ret; |
| 564 | } |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 565 | *val = sign_extend32(raw_val >> chan->scan_type.shift, |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 566 | chan->scan_type.realbits - 1); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 567 | ret = bmc150_accel_set_power_state(data, false); |
| 568 | mutex_unlock(&data->mutex); |
| 569 | if (ret < 0) |
| 570 | return ret; |
| 571 | |
| 572 | return IIO_VAL_INT; |
| 573 | } |
| 574 | |
| 575 | static int bmc150_accel_read_raw(struct iio_dev *indio_dev, |
| 576 | struct iio_chan_spec const *chan, |
| 577 | int *val, int *val2, long mask) |
| 578 | { |
| 579 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 580 | int ret; |
| 581 | |
| 582 | switch (mask) { |
| 583 | case IIO_CHAN_INFO_RAW: |
| 584 | switch (chan->type) { |
| 585 | case IIO_TEMP: |
| 586 | return bmc150_accel_get_temp(data, val); |
| 587 | case IIO_ACCEL: |
| 588 | if (iio_buffer_enabled(indio_dev)) |
| 589 | return -EBUSY; |
| 590 | else |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 591 | return bmc150_accel_get_axis(data, chan, val); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 592 | default: |
| 593 | return -EINVAL; |
| 594 | } |
| 595 | case IIO_CHAN_INFO_OFFSET: |
| 596 | if (chan->type == IIO_TEMP) { |
| 597 | *val = BMC150_ACCEL_TEMP_CENTER_VAL; |
| 598 | return IIO_VAL_INT; |
Hartmut Knaack | e20008e | 2015-06-15 23:48:26 +0200 | [diff] [blame] | 599 | } else { |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 600 | return -EINVAL; |
Hartmut Knaack | e20008e | 2015-06-15 23:48:26 +0200 | [diff] [blame] | 601 | } |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 602 | case IIO_CHAN_INFO_SCALE: |
| 603 | *val = 0; |
| 604 | switch (chan->type) { |
| 605 | case IIO_TEMP: |
| 606 | *val2 = 500000; |
| 607 | return IIO_VAL_INT_PLUS_MICRO; |
| 608 | case IIO_ACCEL: |
| 609 | { |
| 610 | int i; |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 611 | const struct bmc150_scale_info *si; |
| 612 | int st_size = ARRAY_SIZE(data->chip_info->scale_table); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 613 | |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 614 | for (i = 0; i < st_size; ++i) { |
| 615 | si = &data->chip_info->scale_table[i]; |
| 616 | if (si->reg_range == data->range) { |
| 617 | *val2 = si->scale; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 618 | return IIO_VAL_INT_PLUS_MICRO; |
| 619 | } |
| 620 | } |
| 621 | return -EINVAL; |
| 622 | } |
| 623 | default: |
| 624 | return -EINVAL; |
| 625 | } |
| 626 | case IIO_CHAN_INFO_SAMP_FREQ: |
| 627 | mutex_lock(&data->mutex); |
| 628 | ret = bmc150_accel_get_bw(data, val, val2); |
| 629 | mutex_unlock(&data->mutex); |
| 630 | return ret; |
| 631 | default: |
| 632 | return -EINVAL; |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | static int bmc150_accel_write_raw(struct iio_dev *indio_dev, |
| 637 | struct iio_chan_spec const *chan, |
| 638 | int val, int val2, long mask) |
| 639 | { |
| 640 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 641 | int ret; |
| 642 | |
| 643 | switch (mask) { |
| 644 | case IIO_CHAN_INFO_SAMP_FREQ: |
| 645 | mutex_lock(&data->mutex); |
| 646 | ret = bmc150_accel_set_bw(data, val, val2); |
| 647 | mutex_unlock(&data->mutex); |
| 648 | break; |
| 649 | case IIO_CHAN_INFO_SCALE: |
| 650 | if (val) |
| 651 | return -EINVAL; |
| 652 | |
| 653 | mutex_lock(&data->mutex); |
| 654 | ret = bmc150_accel_set_scale(data, val2); |
| 655 | mutex_unlock(&data->mutex); |
| 656 | return ret; |
| 657 | default: |
| 658 | ret = -EINVAL; |
| 659 | } |
| 660 | |
| 661 | return ret; |
| 662 | } |
| 663 | |
| 664 | static int bmc150_accel_read_event(struct iio_dev *indio_dev, |
| 665 | const struct iio_chan_spec *chan, |
| 666 | enum iio_event_type type, |
| 667 | enum iio_event_direction dir, |
| 668 | enum iio_event_info info, |
| 669 | int *val, int *val2) |
| 670 | { |
| 671 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 672 | |
| 673 | *val2 = 0; |
| 674 | switch (info) { |
| 675 | case IIO_EV_INFO_VALUE: |
| 676 | *val = data->slope_thres; |
| 677 | break; |
| 678 | case IIO_EV_INFO_PERIOD: |
Octavian Purdila | 802a3ae | 2015-01-31 02:00:03 +0200 | [diff] [blame] | 679 | *val = data->slope_dur; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 680 | break; |
| 681 | default: |
| 682 | return -EINVAL; |
| 683 | } |
| 684 | |
| 685 | return IIO_VAL_INT; |
| 686 | } |
| 687 | |
| 688 | static int bmc150_accel_write_event(struct iio_dev *indio_dev, |
| 689 | const struct iio_chan_spec *chan, |
| 690 | enum iio_event_type type, |
| 691 | enum iio_event_direction dir, |
| 692 | enum iio_event_info info, |
| 693 | int val, int val2) |
| 694 | { |
| 695 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 696 | |
| 697 | if (data->ev_enable_state) |
| 698 | return -EBUSY; |
| 699 | |
| 700 | switch (info) { |
| 701 | case IIO_EV_INFO_VALUE: |
Hartmut Knaack | fdd15f6 | 2015-06-15 23:48:25 +0200 | [diff] [blame] | 702 | data->slope_thres = val & BMC150_ACCEL_SLOPE_THRES_MASK; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 703 | break; |
| 704 | case IIO_EV_INFO_PERIOD: |
Octavian Purdila | 802a3ae | 2015-01-31 02:00:03 +0200 | [diff] [blame] | 705 | data->slope_dur = val & BMC150_ACCEL_SLOPE_DUR_MASK; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 706 | break; |
| 707 | default: |
| 708 | return -EINVAL; |
| 709 | } |
| 710 | |
| 711 | return 0; |
| 712 | } |
| 713 | |
| 714 | static int bmc150_accel_read_event_config(struct iio_dev *indio_dev, |
| 715 | const struct iio_chan_spec *chan, |
| 716 | enum iio_event_type type, |
| 717 | enum iio_event_direction dir) |
| 718 | { |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 719 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 720 | |
| 721 | return data->ev_enable_state; |
| 722 | } |
| 723 | |
| 724 | static int bmc150_accel_write_event_config(struct iio_dev *indio_dev, |
| 725 | const struct iio_chan_spec *chan, |
| 726 | enum iio_event_type type, |
| 727 | enum iio_event_direction dir, |
| 728 | int state) |
| 729 | { |
| 730 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 731 | int ret; |
| 732 | |
Octavian Purdila | 14ee64f | 2015-01-31 02:00:05 +0200 | [diff] [blame] | 733 | if (state == data->ev_enable_state) |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 734 | return 0; |
| 735 | |
| 736 | mutex_lock(&data->mutex); |
| 737 | |
Octavian Purdila | 3e825ec | 2015-03-03 18:17:57 +0200 | [diff] [blame] | 738 | ret = bmc150_accel_set_interrupt(data, BMC150_ACCEL_INT_ANY_MOTION, |
| 739 | state); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 740 | if (ret < 0) { |
| 741 | mutex_unlock(&data->mutex); |
| 742 | return ret; |
| 743 | } |
| 744 | |
| 745 | data->ev_enable_state = state; |
| 746 | mutex_unlock(&data->mutex); |
| 747 | |
| 748 | return 0; |
| 749 | } |
| 750 | |
| 751 | static int bmc150_accel_validate_trigger(struct iio_dev *indio_dev, |
Hartmut Knaack | e20008e | 2015-06-15 23:48:26 +0200 | [diff] [blame] | 752 | struct iio_trigger *trig) |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 753 | { |
| 754 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
Octavian Purdila | 7d96321 | 2015-03-03 18:17:58 +0200 | [diff] [blame] | 755 | int i; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 756 | |
Octavian Purdila | 7d96321 | 2015-03-03 18:17:58 +0200 | [diff] [blame] | 757 | for (i = 0; i < BMC150_ACCEL_TRIGGERS; i++) { |
| 758 | if (data->triggers[i].indio_trig == trig) |
| 759 | return 0; |
| 760 | } |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 761 | |
Octavian Purdila | 7d96321 | 2015-03-03 18:17:58 +0200 | [diff] [blame] | 762 | return -EINVAL; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 763 | } |
| 764 | |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 765 | static ssize_t bmc150_accel_get_fifo_watermark(struct device *dev, |
| 766 | struct device_attribute *attr, |
| 767 | char *buf) |
| 768 | { |
| 769 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
| 770 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 771 | int wm; |
| 772 | |
| 773 | mutex_lock(&data->mutex); |
| 774 | wm = data->watermark; |
| 775 | mutex_unlock(&data->mutex); |
| 776 | |
| 777 | return sprintf(buf, "%d\n", wm); |
| 778 | } |
| 779 | |
| 780 | static ssize_t bmc150_accel_get_fifo_state(struct device *dev, |
| 781 | struct device_attribute *attr, |
| 782 | char *buf) |
| 783 | { |
| 784 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
| 785 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 786 | bool state; |
| 787 | |
| 788 | mutex_lock(&data->mutex); |
| 789 | state = data->fifo_mode; |
| 790 | mutex_unlock(&data->mutex); |
| 791 | |
| 792 | return sprintf(buf, "%d\n", state); |
| 793 | } |
| 794 | |
| 795 | static IIO_CONST_ATTR(hwfifo_watermark_min, "1"); |
| 796 | static IIO_CONST_ATTR(hwfifo_watermark_max, |
| 797 | __stringify(BMC150_ACCEL_FIFO_LENGTH)); |
| 798 | static IIO_DEVICE_ATTR(hwfifo_enabled, S_IRUGO, |
| 799 | bmc150_accel_get_fifo_state, NULL, 0); |
| 800 | static IIO_DEVICE_ATTR(hwfifo_watermark, S_IRUGO, |
| 801 | bmc150_accel_get_fifo_watermark, NULL, 0); |
| 802 | |
| 803 | static const struct attribute *bmc150_accel_fifo_attributes[] = { |
| 804 | &iio_const_attr_hwfifo_watermark_min.dev_attr.attr, |
| 805 | &iio_const_attr_hwfifo_watermark_max.dev_attr.attr, |
| 806 | &iio_dev_attr_hwfifo_watermark.dev_attr.attr, |
| 807 | &iio_dev_attr_hwfifo_enabled.dev_attr.attr, |
| 808 | NULL, |
| 809 | }; |
| 810 | |
| 811 | static int bmc150_accel_set_watermark(struct iio_dev *indio_dev, unsigned val) |
| 812 | { |
| 813 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 814 | |
| 815 | if (val > BMC150_ACCEL_FIFO_LENGTH) |
| 816 | val = BMC150_ACCEL_FIFO_LENGTH; |
| 817 | |
| 818 | mutex_lock(&data->mutex); |
| 819 | data->watermark = val; |
| 820 | mutex_unlock(&data->mutex); |
| 821 | |
| 822 | return 0; |
| 823 | } |
| 824 | |
| 825 | /* |
| 826 | * We must read at least one full frame in one burst, otherwise the rest of the |
| 827 | * frame data is discarded. |
| 828 | */ |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 829 | static int bmc150_accel_fifo_transfer(struct bmc150_accel_data *data, |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 830 | char *buffer, int samples) |
| 831 | { |
| 832 | int sample_length = 3 * 2; |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 833 | int ret; |
| 834 | int total_length = samples * sample_length; |
| 835 | int i; |
| 836 | size_t step = regmap_get_raw_read_max(data->regmap); |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 837 | |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 838 | if (!step || step > total_length) |
| 839 | step = total_length; |
| 840 | else if (step < total_length) |
| 841 | step = sample_length; |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 842 | |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 843 | /* |
| 844 | * Seems we have a bus with size limitation so we have to execute |
| 845 | * multiple reads |
| 846 | */ |
| 847 | for (i = 0; i < total_length; i += step) { |
| 848 | ret = regmap_raw_read(data->regmap, BMC150_ACCEL_REG_FIFO_DATA, |
| 849 | &buffer[i], step); |
| 850 | if (ret) |
| 851 | break; |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | if (ret) |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 855 | dev_err(data->dev, "Error transferring data from fifo in single steps of %zu\n", |
| 856 | step); |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 857 | |
| 858 | return ret; |
| 859 | } |
| 860 | |
| 861 | static int __bmc150_accel_fifo_flush(struct iio_dev *indio_dev, |
| 862 | unsigned samples, bool irq) |
| 863 | { |
| 864 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 865 | int ret, i; |
| 866 | u8 count; |
| 867 | u16 buffer[BMC150_ACCEL_FIFO_LENGTH * 3]; |
| 868 | int64_t tstamp; |
| 869 | uint64_t sample_period; |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 870 | unsigned int val; |
Hartmut Knaack | e20008e | 2015-06-15 23:48:26 +0200 | [diff] [blame] | 871 | |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 872 | ret = regmap_read(data->regmap, BMC150_ACCEL_REG_FIFO_STATUS, &val); |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 873 | if (ret < 0) { |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 874 | dev_err(data->dev, "Error reading reg_fifo_status\n"); |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 875 | return ret; |
| 876 | } |
| 877 | |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 878 | count = val & 0x7F; |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 879 | |
| 880 | if (!count) |
| 881 | return 0; |
| 882 | |
| 883 | /* |
| 884 | * If we getting called from IRQ handler we know the stored timestamp is |
| 885 | * fairly accurate for the last stored sample. Otherwise, if we are |
| 886 | * called as a result of a read operation from userspace and hence |
| 887 | * before the watermark interrupt was triggered, take a timestamp |
| 888 | * now. We can fall anywhere in between two samples so the error in this |
| 889 | * case is at most one sample period. |
| 890 | */ |
| 891 | if (!irq) { |
| 892 | data->old_timestamp = data->timestamp; |
| 893 | data->timestamp = iio_get_time_ns(); |
| 894 | } |
| 895 | |
| 896 | /* |
| 897 | * Approximate timestamps for each of the sample based on the sampling |
| 898 | * frequency, timestamp for last sample and number of samples. |
| 899 | * |
| 900 | * Note that we can't use the current bandwidth settings to compute the |
| 901 | * sample period because the sample rate varies with the device |
| 902 | * (e.g. between 31.70ms to 32.20ms for a bandwidth of 15.63HZ). That |
| 903 | * small variation adds when we store a large number of samples and |
| 904 | * creates significant jitter between the last and first samples in |
| 905 | * different batches (e.g. 32ms vs 21ms). |
| 906 | * |
| 907 | * To avoid this issue we compute the actual sample period ourselves |
| 908 | * based on the timestamp delta between the last two flush operations. |
| 909 | */ |
| 910 | sample_period = (data->timestamp - data->old_timestamp); |
| 911 | do_div(sample_period, count); |
| 912 | tstamp = data->timestamp - (count - 1) * sample_period; |
| 913 | |
| 914 | if (samples && count > samples) |
| 915 | count = samples; |
| 916 | |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 917 | ret = bmc150_accel_fifo_transfer(data, (u8 *)buffer, count); |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 918 | if (ret) |
| 919 | return ret; |
| 920 | |
| 921 | /* |
| 922 | * Ideally we want the IIO core to handle the demux when running in fifo |
| 923 | * mode but not when running in triggered buffer mode. Unfortunately |
| 924 | * this does not seem to be possible, so stick with driver demux for |
| 925 | * now. |
| 926 | */ |
| 927 | for (i = 0; i < count; i++) { |
| 928 | u16 sample[8]; |
| 929 | int j, bit; |
| 930 | |
| 931 | j = 0; |
| 932 | for_each_set_bit(bit, indio_dev->active_scan_mask, |
| 933 | indio_dev->masklength) |
| 934 | memcpy(&sample[j++], &buffer[i * 3 + bit], 2); |
| 935 | |
| 936 | iio_push_to_buffers_with_timestamp(indio_dev, sample, tstamp); |
| 937 | |
| 938 | tstamp += sample_period; |
| 939 | } |
| 940 | |
| 941 | return count; |
| 942 | } |
| 943 | |
| 944 | static int bmc150_accel_fifo_flush(struct iio_dev *indio_dev, unsigned samples) |
| 945 | { |
| 946 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 947 | int ret; |
| 948 | |
| 949 | mutex_lock(&data->mutex); |
| 950 | ret = __bmc150_accel_fifo_flush(indio_dev, samples, false); |
| 951 | mutex_unlock(&data->mutex); |
| 952 | |
| 953 | return ret; |
| 954 | } |
| 955 | |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 956 | static IIO_CONST_ATTR_SAMP_FREQ_AVAIL( |
Sathyanarayanan Kuppuswamy | 0ba8da9 | 2015-03-03 18:17:56 +0200 | [diff] [blame] | 957 | "15.620000 31.260000 62.50000 125 250 500 1000 2000"); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 958 | |
| 959 | static struct attribute *bmc150_accel_attributes[] = { |
| 960 | &iio_const_attr_sampling_frequency_available.dev_attr.attr, |
| 961 | NULL, |
| 962 | }; |
| 963 | |
| 964 | static const struct attribute_group bmc150_accel_attrs_group = { |
| 965 | .attrs = bmc150_accel_attributes, |
| 966 | }; |
| 967 | |
| 968 | static const struct iio_event_spec bmc150_accel_event = { |
| 969 | .type = IIO_EV_TYPE_ROC, |
Srinivas Pandruvada | 1174124 | 2014-10-10 20:35:34 -0700 | [diff] [blame] | 970 | .dir = IIO_EV_DIR_EITHER, |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 971 | .mask_separate = BIT(IIO_EV_INFO_VALUE) | |
| 972 | BIT(IIO_EV_INFO_ENABLE) | |
| 973 | BIT(IIO_EV_INFO_PERIOD) |
| 974 | }; |
| 975 | |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 976 | #define BMC150_ACCEL_CHANNEL(_axis, bits) { \ |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 977 | .type = IIO_ACCEL, \ |
| 978 | .modified = 1, \ |
| 979 | .channel2 = IIO_MOD_##_axis, \ |
| 980 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 981 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ |
| 982 | BIT(IIO_CHAN_INFO_SAMP_FREQ), \ |
| 983 | .scan_index = AXIS_##_axis, \ |
| 984 | .scan_type = { \ |
| 985 | .sign = 's', \ |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 986 | .realbits = (bits), \ |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 987 | .storagebits = 16, \ |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 988 | .shift = 16 - (bits), \ |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 989 | }, \ |
| 990 | .event_spec = &bmc150_accel_event, \ |
| 991 | .num_event_specs = 1 \ |
| 992 | } |
| 993 | |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 994 | #define BMC150_ACCEL_CHANNELS(bits) { \ |
| 995 | { \ |
| 996 | .type = IIO_TEMP, \ |
| 997 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
| 998 | BIT(IIO_CHAN_INFO_SCALE) | \ |
| 999 | BIT(IIO_CHAN_INFO_OFFSET), \ |
| 1000 | .scan_index = -1, \ |
| 1001 | }, \ |
| 1002 | BMC150_ACCEL_CHANNEL(X, bits), \ |
| 1003 | BMC150_ACCEL_CHANNEL(Y, bits), \ |
| 1004 | BMC150_ACCEL_CHANNEL(Z, bits), \ |
| 1005 | IIO_CHAN_SOFT_TIMESTAMP(3), \ |
| 1006 | } |
| 1007 | |
| 1008 | static const struct iio_chan_spec bma222e_accel_channels[] = |
| 1009 | BMC150_ACCEL_CHANNELS(8); |
| 1010 | static const struct iio_chan_spec bma250e_accel_channels[] = |
| 1011 | BMC150_ACCEL_CHANNELS(10); |
| 1012 | static const struct iio_chan_spec bmc150_accel_channels[] = |
| 1013 | BMC150_ACCEL_CHANNELS(12); |
| 1014 | static const struct iio_chan_spec bma280_accel_channels[] = |
| 1015 | BMC150_ACCEL_CHANNELS(14); |
| 1016 | |
| 1017 | enum { |
| 1018 | bmc150, |
| 1019 | bmi055, |
| 1020 | bma255, |
| 1021 | bma250e, |
| 1022 | bma222e, |
| 1023 | bma280, |
| 1024 | }; |
| 1025 | |
| 1026 | static const struct bmc150_accel_chip_info bmc150_accel_chip_info_tbl[] = { |
| 1027 | [bmc150] = { |
Bastien Nocera | 0ad4bf3 | 2015-07-23 17:21:07 +0200 | [diff] [blame] | 1028 | .name = "BMC150A", |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 1029 | .chip_id = 0xFA, |
| 1030 | .channels = bmc150_accel_channels, |
| 1031 | .num_channels = ARRAY_SIZE(bmc150_accel_channels), |
| 1032 | .scale_table = { {9610, BMC150_ACCEL_DEF_RANGE_2G}, |
| 1033 | {19122, BMC150_ACCEL_DEF_RANGE_4G}, |
| 1034 | {38344, BMC150_ACCEL_DEF_RANGE_8G}, |
| 1035 | {76590, BMC150_ACCEL_DEF_RANGE_16G} }, |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1036 | }, |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 1037 | [bmi055] = { |
Bastien Nocera | 0ad4bf3 | 2015-07-23 17:21:07 +0200 | [diff] [blame] | 1038 | .name = "BMI055A", |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 1039 | .chip_id = 0xFA, |
| 1040 | .channels = bmc150_accel_channels, |
| 1041 | .num_channels = ARRAY_SIZE(bmc150_accel_channels), |
| 1042 | .scale_table = { {9610, BMC150_ACCEL_DEF_RANGE_2G}, |
| 1043 | {19122, BMC150_ACCEL_DEF_RANGE_4G}, |
| 1044 | {38344, BMC150_ACCEL_DEF_RANGE_8G}, |
| 1045 | {76590, BMC150_ACCEL_DEF_RANGE_16G} }, |
| 1046 | }, |
| 1047 | [bma255] = { |
Bastien Nocera | 0ad4bf3 | 2015-07-23 17:21:07 +0200 | [diff] [blame] | 1048 | .name = "BMA0255", |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 1049 | .chip_id = 0xFA, |
| 1050 | .channels = bmc150_accel_channels, |
| 1051 | .num_channels = ARRAY_SIZE(bmc150_accel_channels), |
| 1052 | .scale_table = { {9610, BMC150_ACCEL_DEF_RANGE_2G}, |
| 1053 | {19122, BMC150_ACCEL_DEF_RANGE_4G}, |
| 1054 | {38344, BMC150_ACCEL_DEF_RANGE_8G}, |
| 1055 | {76590, BMC150_ACCEL_DEF_RANGE_16G} }, |
| 1056 | }, |
| 1057 | [bma250e] = { |
Bastien Nocera | 0ad4bf3 | 2015-07-23 17:21:07 +0200 | [diff] [blame] | 1058 | .name = "BMA250E", |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 1059 | .chip_id = 0xF9, |
| 1060 | .channels = bma250e_accel_channels, |
| 1061 | .num_channels = ARRAY_SIZE(bma250e_accel_channels), |
| 1062 | .scale_table = { {38344, BMC150_ACCEL_DEF_RANGE_2G}, |
| 1063 | {76590, BMC150_ACCEL_DEF_RANGE_4G}, |
| 1064 | {153277, BMC150_ACCEL_DEF_RANGE_8G}, |
| 1065 | {306457, BMC150_ACCEL_DEF_RANGE_16G} }, |
| 1066 | }, |
| 1067 | [bma222e] = { |
Bastien Nocera | 0ad4bf3 | 2015-07-23 17:21:07 +0200 | [diff] [blame] | 1068 | .name = "BMA222E", |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 1069 | .chip_id = 0xF8, |
| 1070 | .channels = bma222e_accel_channels, |
| 1071 | .num_channels = ARRAY_SIZE(bma222e_accel_channels), |
| 1072 | .scale_table = { {153277, BMC150_ACCEL_DEF_RANGE_2G}, |
| 1073 | {306457, BMC150_ACCEL_DEF_RANGE_4G}, |
| 1074 | {612915, BMC150_ACCEL_DEF_RANGE_8G}, |
| 1075 | {1225831, BMC150_ACCEL_DEF_RANGE_16G} }, |
| 1076 | }, |
| 1077 | [bma280] = { |
Bastien Nocera | 0ad4bf3 | 2015-07-23 17:21:07 +0200 | [diff] [blame] | 1078 | .name = "BMA0280", |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 1079 | .chip_id = 0xFB, |
| 1080 | .channels = bma280_accel_channels, |
| 1081 | .num_channels = ARRAY_SIZE(bma280_accel_channels), |
| 1082 | .scale_table = { {2392, BMC150_ACCEL_DEF_RANGE_2G}, |
| 1083 | {4785, BMC150_ACCEL_DEF_RANGE_4G}, |
| 1084 | {9581, BMC150_ACCEL_DEF_RANGE_8G}, |
| 1085 | {19152, BMC150_ACCEL_DEF_RANGE_16G} }, |
| 1086 | }, |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1087 | }; |
| 1088 | |
| 1089 | static const struct iio_info bmc150_accel_info = { |
| 1090 | .attrs = &bmc150_accel_attrs_group, |
| 1091 | .read_raw = bmc150_accel_read_raw, |
| 1092 | .write_raw = bmc150_accel_write_raw, |
| 1093 | .read_event_value = bmc150_accel_read_event, |
| 1094 | .write_event_value = bmc150_accel_write_event, |
| 1095 | .write_event_config = bmc150_accel_write_event_config, |
| 1096 | .read_event_config = bmc150_accel_read_event_config, |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1097 | .driver_module = THIS_MODULE, |
| 1098 | }; |
| 1099 | |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 1100 | static const struct iio_info bmc150_accel_info_fifo = { |
| 1101 | .attrs = &bmc150_accel_attrs_group, |
| 1102 | .read_raw = bmc150_accel_read_raw, |
| 1103 | .write_raw = bmc150_accel_write_raw, |
| 1104 | .read_event_value = bmc150_accel_read_event, |
| 1105 | .write_event_value = bmc150_accel_write_event, |
| 1106 | .write_event_config = bmc150_accel_write_event_config, |
| 1107 | .read_event_config = bmc150_accel_read_event_config, |
| 1108 | .validate_trigger = bmc150_accel_validate_trigger, |
| 1109 | .hwfifo_set_watermark = bmc150_accel_set_watermark, |
| 1110 | .hwfifo_flush_to_buffer = bmc150_accel_fifo_flush, |
| 1111 | .driver_module = THIS_MODULE, |
| 1112 | }; |
| 1113 | |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1114 | static irqreturn_t bmc150_accel_trigger_handler(int irq, void *p) |
| 1115 | { |
| 1116 | struct iio_poll_func *pf = p; |
| 1117 | struct iio_dev *indio_dev = pf->indio_dev; |
| 1118 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 1119 | int bit, ret, i = 0; |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 1120 | unsigned int raw_val; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1121 | |
| 1122 | mutex_lock(&data->mutex); |
Octavian Purdila | 70dddee | 2015-03-02 21:03:05 +0200 | [diff] [blame] | 1123 | for_each_set_bit(bit, indio_dev->active_scan_mask, |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1124 | indio_dev->masklength) { |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 1125 | ret = regmap_bulk_read(data->regmap, |
| 1126 | BMC150_ACCEL_AXIS_TO_REG(bit), &raw_val, |
| 1127 | 2); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1128 | if (ret < 0) { |
| 1129 | mutex_unlock(&data->mutex); |
| 1130 | goto err_read; |
| 1131 | } |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 1132 | data->buffer[i++] = raw_val; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1133 | } |
| 1134 | mutex_unlock(&data->mutex); |
| 1135 | |
| 1136 | iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, |
Vlad Dogaru | c16bff4 | 2015-05-12 17:03:24 +0300 | [diff] [blame] | 1137 | pf->timestamp); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1138 | err_read: |
| 1139 | iio_trigger_notify_done(indio_dev->trig); |
| 1140 | |
| 1141 | return IRQ_HANDLED; |
| 1142 | } |
| 1143 | |
| 1144 | static int bmc150_accel_trig_try_reen(struct iio_trigger *trig) |
| 1145 | { |
Octavian Purdila | 7d96321 | 2015-03-03 18:17:58 +0200 | [diff] [blame] | 1146 | struct bmc150_accel_trigger *t = iio_trigger_get_drvdata(trig); |
| 1147 | struct bmc150_accel_data *data = t->data; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1148 | int ret; |
| 1149 | |
| 1150 | /* new data interrupts don't need ack */ |
Octavian Purdila | 7d96321 | 2015-03-03 18:17:58 +0200 | [diff] [blame] | 1151 | if (t == &t->data->triggers[BMC150_ACCEL_TRIGGER_DATA_READY]) |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1152 | return 0; |
| 1153 | |
| 1154 | mutex_lock(&data->mutex); |
| 1155 | /* clear any latched interrupt */ |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 1156 | ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH, |
| 1157 | BMC150_ACCEL_INT_MODE_LATCH_INT | |
| 1158 | BMC150_ACCEL_INT_MODE_LATCH_RESET); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1159 | mutex_unlock(&data->mutex); |
| 1160 | if (ret < 0) { |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 1161 | dev_err(data->dev, |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1162 | "Error writing reg_int_rst_latch\n"); |
| 1163 | return ret; |
| 1164 | } |
| 1165 | |
| 1166 | return 0; |
| 1167 | } |
| 1168 | |
Octavian Purdila | 7d96321 | 2015-03-03 18:17:58 +0200 | [diff] [blame] | 1169 | static int bmc150_accel_trigger_set_state(struct iio_trigger *trig, |
Hartmut Knaack | e20008e | 2015-06-15 23:48:26 +0200 | [diff] [blame] | 1170 | bool state) |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1171 | { |
Octavian Purdila | 7d96321 | 2015-03-03 18:17:58 +0200 | [diff] [blame] | 1172 | struct bmc150_accel_trigger *t = iio_trigger_get_drvdata(trig); |
| 1173 | struct bmc150_accel_data *data = t->data; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1174 | int ret; |
| 1175 | |
| 1176 | mutex_lock(&data->mutex); |
| 1177 | |
Octavian Purdila | 7d96321 | 2015-03-03 18:17:58 +0200 | [diff] [blame] | 1178 | if (t->enabled == state) { |
| 1179 | mutex_unlock(&data->mutex); |
| 1180 | return 0; |
| 1181 | } |
| 1182 | |
| 1183 | if (t->setup) { |
| 1184 | ret = t->setup(t, state); |
| 1185 | if (ret < 0) { |
Octavian Purdila | 14ee64f | 2015-01-31 02:00:05 +0200 | [diff] [blame] | 1186 | mutex_unlock(&data->mutex); |
Octavian Purdila | 7d96321 | 2015-03-03 18:17:58 +0200 | [diff] [blame] | 1187 | return ret; |
Octavian Purdila | 14ee64f | 2015-01-31 02:00:05 +0200 | [diff] [blame] | 1188 | } |
| 1189 | } |
| 1190 | |
Octavian Purdila | 7d96321 | 2015-03-03 18:17:58 +0200 | [diff] [blame] | 1191 | ret = bmc150_accel_set_interrupt(data, t->intr, state); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1192 | if (ret < 0) { |
| 1193 | mutex_unlock(&data->mutex); |
| 1194 | return ret; |
| 1195 | } |
Octavian Purdila | 7d96321 | 2015-03-03 18:17:58 +0200 | [diff] [blame] | 1196 | |
| 1197 | t->enabled = state; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1198 | |
| 1199 | mutex_unlock(&data->mutex); |
| 1200 | |
| 1201 | return ret; |
| 1202 | } |
| 1203 | |
| 1204 | static const struct iio_trigger_ops bmc150_accel_trigger_ops = { |
Octavian Purdila | 7d96321 | 2015-03-03 18:17:58 +0200 | [diff] [blame] | 1205 | .set_trigger_state = bmc150_accel_trigger_set_state, |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1206 | .try_reenable = bmc150_accel_trig_try_reen, |
| 1207 | .owner = THIS_MODULE, |
| 1208 | }; |
| 1209 | |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 1210 | static int bmc150_accel_handle_roc_event(struct iio_dev *indio_dev) |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1211 | { |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1212 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1213 | int dir; |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 1214 | int ret; |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 1215 | unsigned int val; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1216 | |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 1217 | ret = regmap_read(data->regmap, BMC150_ACCEL_REG_INT_STATUS_2, &val); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1218 | if (ret < 0) { |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 1219 | dev_err(data->dev, "Error reading reg_int_status_2\n"); |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 1220 | return ret; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1221 | } |
| 1222 | |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 1223 | if (val & BMC150_ACCEL_ANY_MOTION_BIT_SIGN) |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1224 | dir = IIO_EV_DIR_FALLING; |
| 1225 | else |
| 1226 | dir = IIO_EV_DIR_RISING; |
| 1227 | |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 1228 | if (val & BMC150_ACCEL_ANY_MOTION_BIT_X) |
Hartmut Knaack | e20008e | 2015-06-15 23:48:26 +0200 | [diff] [blame] | 1229 | iio_push_event(indio_dev, |
| 1230 | IIO_MOD_EVENT_CODE(IIO_ACCEL, |
| 1231 | 0, |
| 1232 | IIO_MOD_X, |
| 1233 | IIO_EV_TYPE_ROC, |
| 1234 | dir), |
| 1235 | data->timestamp); |
| 1236 | |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 1237 | if (val & BMC150_ACCEL_ANY_MOTION_BIT_Y) |
Hartmut Knaack | e20008e | 2015-06-15 23:48:26 +0200 | [diff] [blame] | 1238 | iio_push_event(indio_dev, |
| 1239 | IIO_MOD_EVENT_CODE(IIO_ACCEL, |
| 1240 | 0, |
| 1241 | IIO_MOD_Y, |
| 1242 | IIO_EV_TYPE_ROC, |
| 1243 | dir), |
| 1244 | data->timestamp); |
| 1245 | |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 1246 | if (val & BMC150_ACCEL_ANY_MOTION_BIT_Z) |
Hartmut Knaack | e20008e | 2015-06-15 23:48:26 +0200 | [diff] [blame] | 1247 | iio_push_event(indio_dev, |
| 1248 | IIO_MOD_EVENT_CODE(IIO_ACCEL, |
| 1249 | 0, |
| 1250 | IIO_MOD_Z, |
| 1251 | IIO_EV_TYPE_ROC, |
| 1252 | dir), |
| 1253 | data->timestamp); |
| 1254 | |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 1255 | return ret; |
| 1256 | } |
| 1257 | |
| 1258 | static irqreturn_t bmc150_accel_irq_thread_handler(int irq, void *private) |
| 1259 | { |
| 1260 | struct iio_dev *indio_dev = private; |
| 1261 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 1262 | bool ack = false; |
| 1263 | int ret; |
| 1264 | |
| 1265 | mutex_lock(&data->mutex); |
| 1266 | |
| 1267 | if (data->fifo_mode) { |
| 1268 | ret = __bmc150_accel_fifo_flush(indio_dev, |
| 1269 | BMC150_ACCEL_FIFO_LENGTH, true); |
| 1270 | if (ret > 0) |
| 1271 | ack = true; |
| 1272 | } |
| 1273 | |
| 1274 | if (data->ev_enable_state) { |
| 1275 | ret = bmc150_accel_handle_roc_event(indio_dev); |
| 1276 | if (ret > 0) |
| 1277 | ack = true; |
| 1278 | } |
| 1279 | |
| 1280 | if (ack) { |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 1281 | ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH, |
| 1282 | BMC150_ACCEL_INT_MODE_LATCH_INT | |
| 1283 | BMC150_ACCEL_INT_MODE_LATCH_RESET); |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 1284 | if (ret) |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 1285 | dev_err(data->dev, "Error writing reg_int_rst_latch\n"); |
Hartmut Knaack | e20008e | 2015-06-15 23:48:26 +0200 | [diff] [blame] | 1286 | |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 1287 | ret = IRQ_HANDLED; |
| 1288 | } else { |
| 1289 | ret = IRQ_NONE; |
| 1290 | } |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1291 | |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 1292 | mutex_unlock(&data->mutex); |
| 1293 | |
| 1294 | return ret; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1295 | } |
| 1296 | |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 1297 | static irqreturn_t bmc150_accel_irq_handler(int irq, void *private) |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1298 | { |
| 1299 | struct iio_dev *indio_dev = private; |
| 1300 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 1301 | bool ack = false; |
Octavian Purdila | 7d96321 | 2015-03-03 18:17:58 +0200 | [diff] [blame] | 1302 | int i; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1303 | |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 1304 | data->old_timestamp = data->timestamp; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1305 | data->timestamp = iio_get_time_ns(); |
| 1306 | |
Octavian Purdila | 7d96321 | 2015-03-03 18:17:58 +0200 | [diff] [blame] | 1307 | for (i = 0; i < BMC150_ACCEL_TRIGGERS; i++) { |
| 1308 | if (data->triggers[i].enabled) { |
| 1309 | iio_trigger_poll(data->triggers[i].indio_trig); |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 1310 | ack = true; |
Octavian Purdila | 7d96321 | 2015-03-03 18:17:58 +0200 | [diff] [blame] | 1311 | break; |
| 1312 | } |
| 1313 | } |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1314 | |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 1315 | if (data->ev_enable_state || data->fifo_mode) |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1316 | return IRQ_WAKE_THREAD; |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 1317 | |
| 1318 | if (ack) |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1319 | return IRQ_HANDLED; |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 1320 | |
| 1321 | return IRQ_NONE; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1322 | } |
| 1323 | |
Octavian Purdila | 7d96321 | 2015-03-03 18:17:58 +0200 | [diff] [blame] | 1324 | static const struct { |
| 1325 | int intr; |
| 1326 | const char *name; |
| 1327 | int (*setup)(struct bmc150_accel_trigger *t, bool state); |
| 1328 | } bmc150_accel_triggers[BMC150_ACCEL_TRIGGERS] = { |
| 1329 | { |
| 1330 | .intr = 0, |
| 1331 | .name = "%s-dev%d", |
| 1332 | }, |
| 1333 | { |
| 1334 | .intr = 1, |
| 1335 | .name = "%s-any-motion-dev%d", |
| 1336 | .setup = bmc150_accel_any_motion_setup, |
| 1337 | }, |
| 1338 | }; |
| 1339 | |
| 1340 | static void bmc150_accel_unregister_triggers(struct bmc150_accel_data *data, |
| 1341 | int from) |
| 1342 | { |
| 1343 | int i; |
| 1344 | |
Hartmut Knaack | 7a1d0d9 | 2015-06-15 23:48:24 +0200 | [diff] [blame] | 1345 | for (i = from; i >= 0; i--) { |
Octavian Purdila | 7d96321 | 2015-03-03 18:17:58 +0200 | [diff] [blame] | 1346 | if (data->triggers[i].indio_trig) { |
| 1347 | iio_trigger_unregister(data->triggers[i].indio_trig); |
| 1348 | data->triggers[i].indio_trig = NULL; |
| 1349 | } |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | static int bmc150_accel_triggers_setup(struct iio_dev *indio_dev, |
| 1354 | struct bmc150_accel_data *data) |
| 1355 | { |
| 1356 | int i, ret; |
| 1357 | |
| 1358 | for (i = 0; i < BMC150_ACCEL_TRIGGERS; i++) { |
| 1359 | struct bmc150_accel_trigger *t = &data->triggers[i]; |
| 1360 | |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 1361 | t->indio_trig = devm_iio_trigger_alloc(data->dev, |
Octavian Purdila | 7d96321 | 2015-03-03 18:17:58 +0200 | [diff] [blame] | 1362 | bmc150_accel_triggers[i].name, |
| 1363 | indio_dev->name, |
| 1364 | indio_dev->id); |
| 1365 | if (!t->indio_trig) { |
| 1366 | ret = -ENOMEM; |
| 1367 | break; |
| 1368 | } |
| 1369 | |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 1370 | t->indio_trig->dev.parent = data->dev; |
Octavian Purdila | 7d96321 | 2015-03-03 18:17:58 +0200 | [diff] [blame] | 1371 | t->indio_trig->ops = &bmc150_accel_trigger_ops; |
| 1372 | t->intr = bmc150_accel_triggers[i].intr; |
| 1373 | t->data = data; |
| 1374 | t->setup = bmc150_accel_triggers[i].setup; |
| 1375 | iio_trigger_set_drvdata(t->indio_trig, t); |
| 1376 | |
| 1377 | ret = iio_trigger_register(t->indio_trig); |
| 1378 | if (ret) |
| 1379 | break; |
| 1380 | } |
| 1381 | |
| 1382 | if (ret) |
| 1383 | bmc150_accel_unregister_triggers(data, i - 1); |
| 1384 | |
| 1385 | return ret; |
| 1386 | } |
| 1387 | |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 1388 | #define BMC150_ACCEL_FIFO_MODE_STREAM 0x80 |
| 1389 | #define BMC150_ACCEL_FIFO_MODE_FIFO 0x40 |
| 1390 | #define BMC150_ACCEL_FIFO_MODE_BYPASS 0x00 |
| 1391 | |
| 1392 | static int bmc150_accel_fifo_set_mode(struct bmc150_accel_data *data) |
| 1393 | { |
| 1394 | u8 reg = BMC150_ACCEL_REG_FIFO_CONFIG1; |
| 1395 | int ret; |
| 1396 | |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 1397 | ret = regmap_write(data->regmap, reg, data->fifo_mode); |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 1398 | if (ret < 0) { |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 1399 | dev_err(data->dev, "Error writing reg_fifo_config1\n"); |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 1400 | return ret; |
| 1401 | } |
| 1402 | |
| 1403 | if (!data->fifo_mode) |
| 1404 | return 0; |
| 1405 | |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 1406 | ret = regmap_write(data->regmap, BMC150_ACCEL_REG_FIFO_CONFIG0, |
| 1407 | data->watermark); |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 1408 | if (ret < 0) |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 1409 | dev_err(data->dev, "Error writing reg_fifo_config0\n"); |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 1410 | |
| 1411 | return ret; |
| 1412 | } |
| 1413 | |
Vlad Dogaru | c16bff4 | 2015-05-12 17:03:24 +0300 | [diff] [blame] | 1414 | static int bmc150_accel_buffer_preenable(struct iio_dev *indio_dev) |
| 1415 | { |
| 1416 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 1417 | |
| 1418 | return bmc150_accel_set_power_state(data, true); |
| 1419 | } |
| 1420 | |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 1421 | static int bmc150_accel_buffer_postenable(struct iio_dev *indio_dev) |
| 1422 | { |
| 1423 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 1424 | int ret = 0; |
| 1425 | |
| 1426 | if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) |
| 1427 | return iio_triggered_buffer_postenable(indio_dev); |
| 1428 | |
| 1429 | mutex_lock(&data->mutex); |
| 1430 | |
| 1431 | if (!data->watermark) |
| 1432 | goto out; |
| 1433 | |
| 1434 | ret = bmc150_accel_set_interrupt(data, BMC150_ACCEL_INT_WATERMARK, |
| 1435 | true); |
| 1436 | if (ret) |
| 1437 | goto out; |
| 1438 | |
| 1439 | data->fifo_mode = BMC150_ACCEL_FIFO_MODE_FIFO; |
| 1440 | |
| 1441 | ret = bmc150_accel_fifo_set_mode(data); |
| 1442 | if (ret) { |
| 1443 | data->fifo_mode = 0; |
| 1444 | bmc150_accel_set_interrupt(data, BMC150_ACCEL_INT_WATERMARK, |
| 1445 | false); |
| 1446 | } |
| 1447 | |
| 1448 | out: |
| 1449 | mutex_unlock(&data->mutex); |
| 1450 | |
| 1451 | return ret; |
| 1452 | } |
| 1453 | |
| 1454 | static int bmc150_accel_buffer_predisable(struct iio_dev *indio_dev) |
| 1455 | { |
| 1456 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 1457 | |
| 1458 | if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) |
| 1459 | return iio_triggered_buffer_predisable(indio_dev); |
| 1460 | |
| 1461 | mutex_lock(&data->mutex); |
| 1462 | |
| 1463 | if (!data->fifo_mode) |
| 1464 | goto out; |
| 1465 | |
| 1466 | bmc150_accel_set_interrupt(data, BMC150_ACCEL_INT_WATERMARK, false); |
| 1467 | __bmc150_accel_fifo_flush(indio_dev, BMC150_ACCEL_FIFO_LENGTH, false); |
| 1468 | data->fifo_mode = 0; |
| 1469 | bmc150_accel_fifo_set_mode(data); |
| 1470 | |
| 1471 | out: |
| 1472 | mutex_unlock(&data->mutex); |
| 1473 | |
| 1474 | return 0; |
| 1475 | } |
| 1476 | |
Vlad Dogaru | c16bff4 | 2015-05-12 17:03:24 +0300 | [diff] [blame] | 1477 | static int bmc150_accel_buffer_postdisable(struct iio_dev *indio_dev) |
| 1478 | { |
| 1479 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 1480 | |
| 1481 | return bmc150_accel_set_power_state(data, false); |
| 1482 | } |
| 1483 | |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 1484 | static const struct iio_buffer_setup_ops bmc150_accel_buffer_ops = { |
Vlad Dogaru | c16bff4 | 2015-05-12 17:03:24 +0300 | [diff] [blame] | 1485 | .preenable = bmc150_accel_buffer_preenable, |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 1486 | .postenable = bmc150_accel_buffer_postenable, |
| 1487 | .predisable = bmc150_accel_buffer_predisable, |
Vlad Dogaru | c16bff4 | 2015-05-12 17:03:24 +0300 | [diff] [blame] | 1488 | .postdisable = bmc150_accel_buffer_postdisable, |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 1489 | }; |
| 1490 | |
Bastien Nocera | c4eaab7 | 2015-07-23 17:20:59 +0200 | [diff] [blame] | 1491 | static int bmc150_accel_chip_init(struct bmc150_accel_data *data) |
| 1492 | { |
Bastien Nocera | 0ad4bf3 | 2015-07-23 17:21:07 +0200 | [diff] [blame] | 1493 | int ret, i; |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 1494 | unsigned int val; |
Bastien Nocera | c4eaab7 | 2015-07-23 17:20:59 +0200 | [diff] [blame] | 1495 | |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 1496 | ret = regmap_read(data->regmap, BMC150_ACCEL_REG_CHIP_ID, &val); |
Bastien Nocera | c4eaab7 | 2015-07-23 17:20:59 +0200 | [diff] [blame] | 1497 | if (ret < 0) { |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 1498 | dev_err(data->dev, |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 1499 | "Error: Reading chip id\n"); |
Bastien Nocera | c4eaab7 | 2015-07-23 17:20:59 +0200 | [diff] [blame] | 1500 | return ret; |
| 1501 | } |
| 1502 | |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 1503 | dev_dbg(data->dev, "Chip Id %x\n", val); |
Bastien Nocera | 0ad4bf3 | 2015-07-23 17:21:07 +0200 | [diff] [blame] | 1504 | for (i = 0; i < ARRAY_SIZE(bmc150_accel_chip_info_tbl); i++) { |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 1505 | if (bmc150_accel_chip_info_tbl[i].chip_id == val) { |
Bastien Nocera | 0ad4bf3 | 2015-07-23 17:21:07 +0200 | [diff] [blame] | 1506 | data->chip_info = &bmc150_accel_chip_info_tbl[i]; |
| 1507 | break; |
| 1508 | } |
| 1509 | } |
| 1510 | |
| 1511 | if (!data->chip_info) { |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 1512 | dev_err(data->dev, "Invalid chip %x\n", val); |
Bastien Nocera | c4eaab7 | 2015-07-23 17:20:59 +0200 | [diff] [blame] | 1513 | return -ENODEV; |
| 1514 | } |
| 1515 | |
| 1516 | ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0); |
| 1517 | if (ret < 0) |
| 1518 | return ret; |
| 1519 | |
| 1520 | /* Set Bandwidth */ |
| 1521 | ret = bmc150_accel_set_bw(data, BMC150_ACCEL_DEF_BW, 0); |
| 1522 | if (ret < 0) |
| 1523 | return ret; |
| 1524 | |
| 1525 | /* Set Default Range */ |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 1526 | ret = regmap_write(data->regmap, BMC150_ACCEL_REG_PMU_RANGE, |
| 1527 | BMC150_ACCEL_DEF_RANGE_4G); |
Bastien Nocera | c4eaab7 | 2015-07-23 17:20:59 +0200 | [diff] [blame] | 1528 | if (ret < 0) { |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 1529 | dev_err(data->dev, |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 1530 | "Error writing reg_pmu_range\n"); |
Bastien Nocera | c4eaab7 | 2015-07-23 17:20:59 +0200 | [diff] [blame] | 1531 | return ret; |
| 1532 | } |
| 1533 | |
| 1534 | data->range = BMC150_ACCEL_DEF_RANGE_4G; |
| 1535 | |
| 1536 | /* Set default slope duration and thresholds */ |
| 1537 | data->slope_thres = BMC150_ACCEL_DEF_SLOPE_THRESHOLD; |
| 1538 | data->slope_dur = BMC150_ACCEL_DEF_SLOPE_DURATION; |
| 1539 | ret = bmc150_accel_update_slope(data); |
| 1540 | if (ret < 0) |
| 1541 | return ret; |
| 1542 | |
| 1543 | /* Set default as latched interrupts */ |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 1544 | ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH, |
| 1545 | BMC150_ACCEL_INT_MODE_LATCH_INT | |
| 1546 | BMC150_ACCEL_INT_MODE_LATCH_RESET); |
Bastien Nocera | c4eaab7 | 2015-07-23 17:20:59 +0200 | [diff] [blame] | 1547 | if (ret < 0) { |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 1548 | dev_err(data->dev, |
Bastien Nocera | c4eaab7 | 2015-07-23 17:20:59 +0200 | [diff] [blame] | 1549 | "Error writing reg_int_rst_latch\n"); |
| 1550 | return ret; |
| 1551 | } |
| 1552 | |
| 1553 | return 0; |
| 1554 | } |
| 1555 | |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1556 | static int bmc150_accel_probe(struct i2c_client *client, |
| 1557 | const struct i2c_device_id *id) |
| 1558 | { |
| 1559 | struct bmc150_accel_data *data; |
| 1560 | struct iio_dev *indio_dev; |
| 1561 | int ret; |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 1562 | const char *name = NULL; |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 1563 | struct device *dev; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1564 | |
| 1565 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 1566 | if (!indio_dev) |
| 1567 | return -ENOMEM; |
| 1568 | |
| 1569 | data = iio_priv(indio_dev); |
| 1570 | i2c_set_clientdata(client, indio_dev); |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 1571 | data->dev = &client->dev; |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 1572 | dev = &client->dev; |
| 1573 | data->irq = client->irq; |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 1574 | |
| 1575 | data->regmap = devm_regmap_init_i2c(client, &bmc150_i2c_regmap_conf); |
| 1576 | if (IS_ERR(data->regmap)) { |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 1577 | dev_err(dev, "Failed to initialize i2c regmap\n"); |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 1578 | return PTR_ERR(data->regmap); |
| 1579 | } |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1580 | |
Bastien Nocera | 0ad4bf3 | 2015-07-23 17:21:07 +0200 | [diff] [blame] | 1581 | if (id) |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 1582 | name = id->name; |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 1583 | |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1584 | ret = bmc150_accel_chip_init(data); |
| 1585 | if (ret < 0) |
| 1586 | return ret; |
| 1587 | |
| 1588 | mutex_init(&data->mutex); |
| 1589 | |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 1590 | indio_dev->dev.parent = dev; |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 1591 | indio_dev->channels = data->chip_info->channels; |
| 1592 | indio_dev->num_channels = data->chip_info->num_channels; |
Bastien Nocera | 0ad4bf3 | 2015-07-23 17:21:07 +0200 | [diff] [blame] | 1593 | indio_dev->name = name ? name : data->chip_info->name; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1594 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 1595 | indio_dev->info = &bmc150_accel_info; |
| 1596 | |
Vlad Dogaru | c16bff4 | 2015-05-12 17:03:24 +0300 | [diff] [blame] | 1597 | ret = iio_triggered_buffer_setup(indio_dev, |
| 1598 | &iio_pollfunc_store_time, |
| 1599 | bmc150_accel_trigger_handler, |
| 1600 | &bmc150_accel_buffer_ops); |
| 1601 | if (ret < 0) { |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 1602 | dev_err(data->dev, "Failed: iio triggered buffer setup\n"); |
Vlad Dogaru | c16bff4 | 2015-05-12 17:03:24 +0300 | [diff] [blame] | 1603 | return ret; |
| 1604 | } |
| 1605 | |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 1606 | if (data->irq > 0) { |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1607 | ret = devm_request_threaded_irq( |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 1608 | data->dev, data->irq, |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 1609 | bmc150_accel_irq_handler, |
| 1610 | bmc150_accel_irq_thread_handler, |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1611 | IRQF_TRIGGER_RISING, |
| 1612 | BMC150_ACCEL_IRQ_NAME, |
| 1613 | indio_dev); |
| 1614 | if (ret) |
Vlad Dogaru | c16bff4 | 2015-05-12 17:03:24 +0300 | [diff] [blame] | 1615 | goto err_buffer_cleanup; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1616 | |
Octavian Purdila | 8e22f47 | 2015-01-31 02:00:04 +0200 | [diff] [blame] | 1617 | /* |
| 1618 | * Set latched mode interrupt. While certain interrupts are |
| 1619 | * non-latched regardless of this settings (e.g. new data) we |
| 1620 | * want to use latch mode when we can to prevent interrupt |
| 1621 | * flooding. |
| 1622 | */ |
Markus Pargmann | 4011eda | 2015-09-21 12:55:13 +0200 | [diff] [blame] | 1623 | ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH, |
| 1624 | BMC150_ACCEL_INT_MODE_LATCH_RESET); |
Octavian Purdila | 8e22f47 | 2015-01-31 02:00:04 +0200 | [diff] [blame] | 1625 | if (ret < 0) { |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 1626 | dev_err(data->dev, "Error writing reg_int_rst_latch\n"); |
Vlad Dogaru | c16bff4 | 2015-05-12 17:03:24 +0300 | [diff] [blame] | 1627 | goto err_buffer_cleanup; |
Octavian Purdila | 8e22f47 | 2015-01-31 02:00:04 +0200 | [diff] [blame] | 1628 | } |
| 1629 | |
Octavian Purdila | 3e825ec | 2015-03-03 18:17:57 +0200 | [diff] [blame] | 1630 | bmc150_accel_interrupts_setup(indio_dev, data); |
| 1631 | |
Octavian Purdila | 7d96321 | 2015-03-03 18:17:58 +0200 | [diff] [blame] | 1632 | ret = bmc150_accel_triggers_setup(indio_dev, data); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1633 | if (ret) |
Vlad Dogaru | c16bff4 | 2015-05-12 17:03:24 +0300 | [diff] [blame] | 1634 | goto err_buffer_cleanup; |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 1635 | |
| 1636 | if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) || |
| 1637 | i2c_check_functionality(client->adapter, |
| 1638 | I2C_FUNC_SMBUS_READ_I2C_BLOCK)) { |
| 1639 | indio_dev->modes |= INDIO_BUFFER_SOFTWARE; |
| 1640 | indio_dev->info = &bmc150_accel_info_fifo; |
| 1641 | indio_dev->buffer->attrs = bmc150_accel_fifo_attributes; |
| 1642 | } |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | ret = iio_device_register(indio_dev); |
| 1646 | if (ret < 0) { |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 1647 | dev_err(data->dev, "Unable to register iio device\n"); |
Vlad Dogaru | c16bff4 | 2015-05-12 17:03:24 +0300 | [diff] [blame] | 1648 | goto err_trigger_unregister; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1649 | } |
| 1650 | |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 1651 | ret = pm_runtime_set_active(dev); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1652 | if (ret) |
| 1653 | goto err_iio_unregister; |
| 1654 | |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 1655 | pm_runtime_enable(dev); |
| 1656 | pm_runtime_set_autosuspend_delay(dev, BMC150_AUTO_SUSPEND_DELAY_MS); |
| 1657 | pm_runtime_use_autosuspend(dev); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1658 | |
| 1659 | return 0; |
| 1660 | |
| 1661 | err_iio_unregister: |
| 1662 | iio_device_unregister(indio_dev); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1663 | err_trigger_unregister: |
Octavian Purdila | 7d96321 | 2015-03-03 18:17:58 +0200 | [diff] [blame] | 1664 | bmc150_accel_unregister_triggers(data, BMC150_ACCEL_TRIGGERS - 1); |
Vlad Dogaru | c16bff4 | 2015-05-12 17:03:24 +0300 | [diff] [blame] | 1665 | err_buffer_cleanup: |
| 1666 | iio_triggered_buffer_cleanup(indio_dev); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1667 | |
| 1668 | return ret; |
| 1669 | } |
| 1670 | |
| 1671 | static int bmc150_accel_remove(struct i2c_client *client) |
| 1672 | { |
| 1673 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 1674 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 1675 | |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 1676 | pm_runtime_disable(data->dev); |
| 1677 | pm_runtime_set_suspended(data->dev); |
| 1678 | pm_runtime_put_noidle(data->dev); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1679 | |
| 1680 | iio_device_unregister(indio_dev); |
| 1681 | |
Octavian Purdila | 7d96321 | 2015-03-03 18:17:58 +0200 | [diff] [blame] | 1682 | bmc150_accel_unregister_triggers(data, BMC150_ACCEL_TRIGGERS - 1); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1683 | |
Vlad Dogaru | c16bff4 | 2015-05-12 17:03:24 +0300 | [diff] [blame] | 1684 | iio_triggered_buffer_cleanup(indio_dev); |
| 1685 | |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1686 | mutex_lock(&data->mutex); |
| 1687 | bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_DEEP_SUSPEND, 0); |
| 1688 | mutex_unlock(&data->mutex); |
| 1689 | |
| 1690 | return 0; |
| 1691 | } |
| 1692 | |
| 1693 | #ifdef CONFIG_PM_SLEEP |
| 1694 | static int bmc150_accel_suspend(struct device *dev) |
| 1695 | { |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 1696 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1697 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 1698 | |
| 1699 | mutex_lock(&data->mutex); |
| 1700 | bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_SUSPEND, 0); |
| 1701 | mutex_unlock(&data->mutex); |
| 1702 | |
| 1703 | return 0; |
| 1704 | } |
| 1705 | |
| 1706 | static int bmc150_accel_resume(struct device *dev) |
| 1707 | { |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 1708 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1709 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 1710 | |
| 1711 | mutex_lock(&data->mutex); |
Octavian Purdila | 3e825ec | 2015-03-03 18:17:57 +0200 | [diff] [blame] | 1712 | if (atomic_read(&data->active_intr)) |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1713 | bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0); |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 1714 | bmc150_accel_fifo_set_mode(data); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1715 | mutex_unlock(&data->mutex); |
| 1716 | |
| 1717 | return 0; |
| 1718 | } |
| 1719 | #endif |
| 1720 | |
Rafael J. Wysocki | 6f0a13f | 2014-12-04 01:08:13 +0100 | [diff] [blame] | 1721 | #ifdef CONFIG_PM |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1722 | static int bmc150_accel_runtime_suspend(struct device *dev) |
| 1723 | { |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 1724 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1725 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
Srinivas Pandruvada | aaeecd8 | 2014-10-10 20:35:31 -0700 | [diff] [blame] | 1726 | int ret; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1727 | |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 1728 | dev_dbg(data->dev, __func__); |
Srinivas Pandruvada | aaeecd8 | 2014-10-10 20:35:31 -0700 | [diff] [blame] | 1729 | ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_SUSPEND, 0); |
| 1730 | if (ret < 0) |
| 1731 | return -EAGAIN; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1732 | |
Srinivas Pandruvada | aaeecd8 | 2014-10-10 20:35:31 -0700 | [diff] [blame] | 1733 | return 0; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | static int bmc150_accel_runtime_resume(struct device *dev) |
| 1737 | { |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 1738 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1739 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 1740 | int ret; |
| 1741 | int sleep_val; |
| 1742 | |
Markus Pargmann | 19c95d6 | 2015-09-21 12:55:14 +0200 | [diff] [blame^] | 1743 | dev_dbg(data->dev, __func__); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1744 | |
| 1745 | ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0); |
| 1746 | if (ret < 0) |
| 1747 | return ret; |
Octavian Purdila | 3bbec97 | 2015-03-22 20:33:40 +0200 | [diff] [blame] | 1748 | ret = bmc150_accel_fifo_set_mode(data); |
| 1749 | if (ret < 0) |
| 1750 | return ret; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1751 | |
| 1752 | sleep_val = bmc150_accel_get_startup_times(data); |
| 1753 | if (sleep_val < 20) |
| 1754 | usleep_range(sleep_val * 1000, 20000); |
| 1755 | else |
| 1756 | msleep_interruptible(sleep_val); |
| 1757 | |
| 1758 | return 0; |
| 1759 | } |
| 1760 | #endif |
| 1761 | |
| 1762 | static const struct dev_pm_ops bmc150_accel_pm_ops = { |
| 1763 | SET_SYSTEM_SLEEP_PM_OPS(bmc150_accel_suspend, bmc150_accel_resume) |
| 1764 | SET_RUNTIME_PM_OPS(bmc150_accel_runtime_suspend, |
| 1765 | bmc150_accel_runtime_resume, NULL) |
| 1766 | }; |
| 1767 | |
| 1768 | static const struct acpi_device_id bmc150_accel_acpi_match[] = { |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 1769 | {"BSBA0150", bmc150}, |
| 1770 | {"BMC150A", bmc150}, |
| 1771 | {"BMI055A", bmi055}, |
| 1772 | {"BMA0255", bma255}, |
| 1773 | {"BMA250E", bma250e}, |
| 1774 | {"BMA222E", bma222e}, |
| 1775 | {"BMA0280", bma280}, |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1776 | { }, |
| 1777 | }; |
| 1778 | MODULE_DEVICE_TABLE(acpi, bmc150_accel_acpi_match); |
| 1779 | |
| 1780 | static const struct i2c_device_id bmc150_accel_id[] = { |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 1781 | {"bmc150_accel", bmc150}, |
| 1782 | {"bmi055_accel", bmi055}, |
| 1783 | {"bma255", bma255}, |
| 1784 | {"bma250e", bma250e}, |
| 1785 | {"bma222e", bma222e}, |
| 1786 | {"bma280", bma280}, |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1787 | {} |
| 1788 | }; |
| 1789 | |
| 1790 | MODULE_DEVICE_TABLE(i2c, bmc150_accel_id); |
| 1791 | |
| 1792 | static struct i2c_driver bmc150_accel_driver = { |
| 1793 | .driver = { |
| 1794 | .name = BMC150_ACCEL_DRV_NAME, |
| 1795 | .acpi_match_table = ACPI_PTR(bmc150_accel_acpi_match), |
| 1796 | .pm = &bmc150_accel_pm_ops, |
| 1797 | }, |
| 1798 | .probe = bmc150_accel_probe, |
| 1799 | .remove = bmc150_accel_remove, |
| 1800 | .id_table = bmc150_accel_id, |
| 1801 | }; |
| 1802 | module_i2c_driver(bmc150_accel_driver); |
| 1803 | |
| 1804 | MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>"); |
| 1805 | MODULE_LICENSE("GPL v2"); |
| 1806 | MODULE_DESCRIPTION("BMC150 accelerometer driver"); |