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