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> |
| 38 | |
| 39 | #define BMC150_ACCEL_DRV_NAME "bmc150_accel" |
| 40 | #define BMC150_ACCEL_IRQ_NAME "bmc150_accel_event" |
| 41 | #define BMC150_ACCEL_GPIO_NAME "bmc150_accel_int" |
| 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 |
| 73 | #define BMC150_ACCEL_INT_MAP_1_BIT_DATA BIT(0) |
| 74 | |
| 75 | #define BMC150_ACCEL_REG_INT_RST_LATCH 0x21 |
| 76 | #define BMC150_ACCEL_INT_MODE_LATCH_RESET 0x80 |
| 77 | #define BMC150_ACCEL_INT_MODE_LATCH_INT 0x0F |
| 78 | #define BMC150_ACCEL_INT_MODE_NON_LATCH_INT 0x00 |
| 79 | |
| 80 | #define BMC150_ACCEL_REG_INT_EN_0 0x16 |
| 81 | #define BMC150_ACCEL_INT_EN_BIT_SLP_X BIT(0) |
| 82 | #define BMC150_ACCEL_INT_EN_BIT_SLP_Y BIT(1) |
| 83 | #define BMC150_ACCEL_INT_EN_BIT_SLP_Z BIT(2) |
| 84 | |
| 85 | #define BMC150_ACCEL_REG_INT_EN_1 0x17 |
| 86 | #define BMC150_ACCEL_INT_EN_BIT_DATA_EN BIT(4) |
| 87 | |
| 88 | #define BMC150_ACCEL_REG_INT_OUT_CTRL 0x20 |
| 89 | #define BMC150_ACCEL_INT_OUT_CTRL_INT1_LVL BIT(0) |
| 90 | |
| 91 | #define BMC150_ACCEL_REG_INT_5 0x27 |
| 92 | #define BMC150_ACCEL_SLOPE_DUR_MASK 0x03 |
| 93 | |
| 94 | #define BMC150_ACCEL_REG_INT_6 0x28 |
| 95 | #define BMC150_ACCEL_SLOPE_THRES_MASK 0xFF |
| 96 | |
| 97 | /* Slope duration in terms of number of samples */ |
Srinivas Pandruvada | 9e8e228 | 2014-10-10 20:35:33 -0700 | [diff] [blame] | 98 | #define BMC150_ACCEL_DEF_SLOPE_DURATION 1 |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 99 | /* in terms of multiples of g's/LSB, based on range */ |
Srinivas Pandruvada | 9e8e228 | 2014-10-10 20:35:33 -0700 | [diff] [blame] | 100 | #define BMC150_ACCEL_DEF_SLOPE_THRESHOLD 1 |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 101 | |
| 102 | #define BMC150_ACCEL_REG_XOUT_L 0x02 |
| 103 | |
| 104 | #define BMC150_ACCEL_MAX_STARTUP_TIME_MS 100 |
| 105 | |
| 106 | /* Sleep Duration values */ |
| 107 | #define BMC150_ACCEL_SLEEP_500_MICRO 0x05 |
| 108 | #define BMC150_ACCEL_SLEEP_1_MS 0x06 |
| 109 | #define BMC150_ACCEL_SLEEP_2_MS 0x07 |
| 110 | #define BMC150_ACCEL_SLEEP_4_MS 0x08 |
| 111 | #define BMC150_ACCEL_SLEEP_6_MS 0x09 |
| 112 | #define BMC150_ACCEL_SLEEP_10_MS 0x0A |
| 113 | #define BMC150_ACCEL_SLEEP_25_MS 0x0B |
| 114 | #define BMC150_ACCEL_SLEEP_50_MS 0x0C |
| 115 | #define BMC150_ACCEL_SLEEP_100_MS 0x0D |
| 116 | #define BMC150_ACCEL_SLEEP_500_MS 0x0E |
| 117 | #define BMC150_ACCEL_SLEEP_1_SEC 0x0F |
| 118 | |
| 119 | #define BMC150_ACCEL_REG_TEMP 0x08 |
| 120 | #define BMC150_ACCEL_TEMP_CENTER_VAL 24 |
| 121 | |
| 122 | #define BMC150_ACCEL_AXIS_TO_REG(axis) (BMC150_ACCEL_REG_XOUT_L + (axis * 2)) |
| 123 | #define BMC150_AUTO_SUSPEND_DELAY_MS 2000 |
| 124 | |
| 125 | enum bmc150_accel_axis { |
| 126 | AXIS_X, |
| 127 | AXIS_Y, |
| 128 | AXIS_Z, |
| 129 | }; |
| 130 | |
| 131 | enum bmc150_power_modes { |
| 132 | BMC150_ACCEL_SLEEP_MODE_NORMAL, |
| 133 | BMC150_ACCEL_SLEEP_MODE_DEEP_SUSPEND, |
| 134 | BMC150_ACCEL_SLEEP_MODE_LPM, |
| 135 | BMC150_ACCEL_SLEEP_MODE_SUSPEND = 0x04, |
| 136 | }; |
| 137 | |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 138 | struct bmc150_scale_info { |
| 139 | int scale; |
| 140 | u8 reg_range; |
| 141 | }; |
| 142 | |
| 143 | struct bmc150_accel_chip_info { |
| 144 | u8 chip_id; |
| 145 | const struct iio_chan_spec *channels; |
| 146 | int num_channels; |
| 147 | const struct bmc150_scale_info scale_table[4]; |
| 148 | }; |
| 149 | |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 150 | struct bmc150_accel_data { |
| 151 | struct i2c_client *client; |
| 152 | struct iio_trigger *dready_trig; |
| 153 | struct iio_trigger *motion_trig; |
| 154 | struct mutex mutex; |
| 155 | s16 buffer[8]; |
| 156 | u8 bw_bits; |
| 157 | u32 slope_dur; |
| 158 | u32 slope_thres; |
| 159 | u32 range; |
| 160 | int ev_enable_state; |
| 161 | bool dready_trigger_on; |
| 162 | bool motion_trigger_on; |
| 163 | int64_t timestamp; |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 164 | const struct bmc150_accel_chip_info *chip_info; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | static const struct { |
| 168 | int val; |
| 169 | int val2; |
| 170 | u8 bw_bits; |
| 171 | } bmc150_accel_samp_freq_table[] = { {7, 810000, 0x08}, |
| 172 | {15, 630000, 0x09}, |
| 173 | {31, 250000, 0x0A}, |
| 174 | {62, 500000, 0x0B}, |
| 175 | {125, 0, 0x0C}, |
| 176 | {250, 0, 0x0D}, |
| 177 | {500, 0, 0x0E}, |
| 178 | {1000, 0, 0x0F} }; |
| 179 | |
| 180 | static const struct { |
| 181 | int bw_bits; |
| 182 | int msec; |
| 183 | } bmc150_accel_sample_upd_time[] = { {0x08, 64}, |
| 184 | {0x09, 32}, |
| 185 | {0x0A, 16}, |
| 186 | {0x0B, 8}, |
| 187 | {0x0C, 4}, |
| 188 | {0x0D, 2}, |
| 189 | {0x0E, 1}, |
| 190 | {0x0F, 1} }; |
| 191 | |
| 192 | static const struct { |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 193 | int sleep_dur; |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 194 | u8 reg_value; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 195 | } bmc150_accel_sleep_value_table[] = { {0, 0}, |
| 196 | {500, BMC150_ACCEL_SLEEP_500_MICRO}, |
| 197 | {1000, BMC150_ACCEL_SLEEP_1_MS}, |
| 198 | {2000, BMC150_ACCEL_SLEEP_2_MS}, |
| 199 | {4000, BMC150_ACCEL_SLEEP_4_MS}, |
| 200 | {6000, BMC150_ACCEL_SLEEP_6_MS}, |
| 201 | {10000, BMC150_ACCEL_SLEEP_10_MS}, |
| 202 | {25000, BMC150_ACCEL_SLEEP_25_MS}, |
| 203 | {50000, BMC150_ACCEL_SLEEP_50_MS}, |
| 204 | {100000, BMC150_ACCEL_SLEEP_100_MS}, |
| 205 | {500000, BMC150_ACCEL_SLEEP_500_MS}, |
| 206 | {1000000, BMC150_ACCEL_SLEEP_1_SEC} }; |
| 207 | |
| 208 | |
| 209 | static int bmc150_accel_set_mode(struct bmc150_accel_data *data, |
| 210 | enum bmc150_power_modes mode, |
| 211 | int dur_us) |
| 212 | { |
| 213 | int i; |
| 214 | int ret; |
| 215 | u8 lpw_bits; |
| 216 | int dur_val = -1; |
| 217 | |
| 218 | if (dur_us > 0) { |
| 219 | for (i = 0; i < ARRAY_SIZE(bmc150_accel_sleep_value_table); |
| 220 | ++i) { |
| 221 | if (bmc150_accel_sleep_value_table[i].sleep_dur == |
| 222 | dur_us) |
| 223 | dur_val = |
| 224 | bmc150_accel_sleep_value_table[i].reg_value; |
| 225 | } |
| 226 | } else |
| 227 | dur_val = 0; |
| 228 | |
| 229 | if (dur_val < 0) |
| 230 | return -EINVAL; |
| 231 | |
| 232 | lpw_bits = mode << BMC150_ACCEL_PMU_MODE_SHIFT; |
| 233 | lpw_bits |= (dur_val << BMC150_ACCEL_PMU_BIT_SLEEP_DUR_SHIFT); |
| 234 | |
| 235 | dev_dbg(&data->client->dev, "Set Mode bits %x\n", lpw_bits); |
| 236 | |
| 237 | ret = i2c_smbus_write_byte_data(data->client, |
| 238 | BMC150_ACCEL_REG_PMU_LPW, lpw_bits); |
| 239 | if (ret < 0) { |
| 240 | dev_err(&data->client->dev, "Error writing reg_pmu_lpw\n"); |
| 241 | return ret; |
| 242 | } |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | static int bmc150_accel_set_bw(struct bmc150_accel_data *data, int val, |
| 248 | int val2) |
| 249 | { |
| 250 | int i; |
| 251 | int ret; |
| 252 | |
| 253 | for (i = 0; i < ARRAY_SIZE(bmc150_accel_samp_freq_table); ++i) { |
| 254 | if (bmc150_accel_samp_freq_table[i].val == val && |
| 255 | bmc150_accel_samp_freq_table[i].val2 == val2) { |
| 256 | ret = i2c_smbus_write_byte_data( |
| 257 | data->client, |
| 258 | BMC150_ACCEL_REG_PMU_BW, |
| 259 | bmc150_accel_samp_freq_table[i].bw_bits); |
| 260 | if (ret < 0) |
| 261 | return ret; |
| 262 | |
| 263 | data->bw_bits = |
| 264 | bmc150_accel_samp_freq_table[i].bw_bits; |
| 265 | return 0; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | return -EINVAL; |
| 270 | } |
| 271 | |
Octavian Purdila | 802a3ae | 2015-01-31 02:00:03 +0200 | [diff] [blame^] | 272 | static int bmc150_accel_update_slope(struct bmc150_accel_data *data) |
| 273 | { |
| 274 | int ret, val; |
| 275 | |
| 276 | ret = i2c_smbus_write_byte_data(data->client, BMC150_ACCEL_REG_INT_6, |
| 277 | data->slope_thres); |
| 278 | if (ret < 0) { |
| 279 | dev_err(&data->client->dev, "Error writing reg_int_6\n"); |
| 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | ret = i2c_smbus_read_byte_data(data->client, BMC150_ACCEL_REG_INT_5); |
| 284 | if (ret < 0) { |
| 285 | dev_err(&data->client->dev, "Error reading reg_int_5\n"); |
| 286 | return ret; |
| 287 | } |
| 288 | |
| 289 | val = (ret & ~BMC150_ACCEL_SLOPE_DUR_MASK) | data->slope_dur; |
| 290 | ret = i2c_smbus_write_byte_data(data->client, BMC150_ACCEL_REG_INT_5, |
| 291 | val); |
| 292 | if (ret < 0) { |
| 293 | dev_err(&data->client->dev, "Error write reg_int_5\n"); |
| 294 | return ret; |
| 295 | } |
| 296 | |
| 297 | dev_dbg(&data->client->dev, "%s: %x %x\n", __func__, data->slope_thres, |
| 298 | data->slope_dur); |
| 299 | |
| 300 | return ret; |
| 301 | } |
| 302 | |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 303 | static int bmc150_accel_chip_init(struct bmc150_accel_data *data) |
| 304 | { |
| 305 | int ret; |
| 306 | |
| 307 | ret = i2c_smbus_read_byte_data(data->client, BMC150_ACCEL_REG_CHIP_ID); |
| 308 | if (ret < 0) { |
| 309 | dev_err(&data->client->dev, |
| 310 | "Error: Reading chip id\n"); |
| 311 | return ret; |
| 312 | } |
| 313 | |
| 314 | dev_dbg(&data->client->dev, "Chip Id %x\n", ret); |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 315 | if (ret != data->chip_info->chip_id) { |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 316 | dev_err(&data->client->dev, "Invalid chip %x\n", ret); |
| 317 | return -ENODEV; |
| 318 | } |
| 319 | |
| 320 | ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0); |
| 321 | if (ret < 0) |
| 322 | return ret; |
| 323 | |
| 324 | /* Set Bandwidth */ |
| 325 | ret = bmc150_accel_set_bw(data, BMC150_ACCEL_DEF_BW, 0); |
| 326 | if (ret < 0) |
| 327 | return ret; |
| 328 | |
| 329 | /* Set Default Range */ |
| 330 | ret = i2c_smbus_write_byte_data(data->client, |
| 331 | BMC150_ACCEL_REG_PMU_RANGE, |
| 332 | BMC150_ACCEL_DEF_RANGE_4G); |
| 333 | if (ret < 0) { |
| 334 | dev_err(&data->client->dev, |
| 335 | "Error writing reg_pmu_range\n"); |
| 336 | return ret; |
| 337 | } |
| 338 | |
| 339 | data->range = BMC150_ACCEL_DEF_RANGE_4G; |
| 340 | |
Octavian Purdila | 802a3ae | 2015-01-31 02:00:03 +0200 | [diff] [blame^] | 341 | /* Set default slope duration and thresholds */ |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 342 | data->slope_thres = BMC150_ACCEL_DEF_SLOPE_THRESHOLD; |
Octavian Purdila | 802a3ae | 2015-01-31 02:00:03 +0200 | [diff] [blame^] | 343 | data->slope_dur = BMC150_ACCEL_DEF_SLOPE_DURATION; |
| 344 | ret = bmc150_accel_update_slope(data); |
| 345 | if (ret < 0) |
| 346 | return ret; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 347 | |
| 348 | /* Set default as latched interrupts */ |
| 349 | ret = i2c_smbus_write_byte_data(data->client, |
| 350 | BMC150_ACCEL_REG_INT_RST_LATCH, |
| 351 | BMC150_ACCEL_INT_MODE_LATCH_INT | |
| 352 | BMC150_ACCEL_INT_MODE_LATCH_RESET); |
| 353 | if (ret < 0) { |
| 354 | dev_err(&data->client->dev, |
| 355 | "Error writing reg_int_rst_latch\n"); |
| 356 | return ret; |
| 357 | } |
| 358 | |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | static int bmc150_accel_setup_any_motion_interrupt( |
| 363 | struct bmc150_accel_data *data, |
| 364 | bool status) |
| 365 | { |
| 366 | int ret; |
| 367 | |
| 368 | /* Enable/Disable INT1 mapping */ |
| 369 | ret = i2c_smbus_read_byte_data(data->client, |
| 370 | BMC150_ACCEL_REG_INT_MAP_0); |
| 371 | if (ret < 0) { |
| 372 | dev_err(&data->client->dev, "Error reading reg_int_map_0\n"); |
| 373 | return ret; |
| 374 | } |
| 375 | if (status) |
| 376 | ret |= BMC150_ACCEL_INT_MAP_0_BIT_SLOPE; |
| 377 | else |
| 378 | ret &= ~BMC150_ACCEL_INT_MAP_0_BIT_SLOPE; |
| 379 | |
| 380 | ret = i2c_smbus_write_byte_data(data->client, |
| 381 | BMC150_ACCEL_REG_INT_MAP_0, |
| 382 | ret); |
| 383 | if (ret < 0) { |
| 384 | dev_err(&data->client->dev, "Error writing reg_int_map_0\n"); |
| 385 | return ret; |
| 386 | } |
| 387 | |
| 388 | if (status) { |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 389 | /* |
| 390 | * New data interrupt is always non-latched, |
| 391 | * which will have higher priority, so no need |
| 392 | * to set latched mode, we will be flooded anyway with INTR |
| 393 | */ |
| 394 | if (!data->dready_trigger_on) { |
| 395 | ret = i2c_smbus_write_byte_data(data->client, |
| 396 | BMC150_ACCEL_REG_INT_RST_LATCH, |
| 397 | BMC150_ACCEL_INT_MODE_LATCH_INT | |
| 398 | BMC150_ACCEL_INT_MODE_LATCH_RESET); |
| 399 | if (ret < 0) { |
| 400 | dev_err(&data->client->dev, |
| 401 | "Error writing reg_int_rst_latch\n"); |
| 402 | return ret; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | ret = i2c_smbus_write_byte_data(data->client, |
| 407 | BMC150_ACCEL_REG_INT_EN_0, |
| 408 | BMC150_ACCEL_INT_EN_BIT_SLP_X | |
| 409 | BMC150_ACCEL_INT_EN_BIT_SLP_Y | |
| 410 | BMC150_ACCEL_INT_EN_BIT_SLP_Z); |
| 411 | } else |
| 412 | ret = i2c_smbus_write_byte_data(data->client, |
| 413 | BMC150_ACCEL_REG_INT_EN_0, |
| 414 | 0); |
| 415 | |
| 416 | if (ret < 0) { |
| 417 | dev_err(&data->client->dev, "Error writing reg_int_en_0\n"); |
| 418 | return ret; |
| 419 | } |
| 420 | |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | static int bmc150_accel_setup_new_data_interrupt(struct bmc150_accel_data *data, |
| 425 | bool status) |
| 426 | { |
| 427 | int ret; |
| 428 | |
| 429 | /* Enable/Disable INT1 mapping */ |
| 430 | ret = i2c_smbus_read_byte_data(data->client, |
| 431 | BMC150_ACCEL_REG_INT_MAP_1); |
| 432 | if (ret < 0) { |
| 433 | dev_err(&data->client->dev, "Error reading reg_int_map_1\n"); |
| 434 | return ret; |
| 435 | } |
| 436 | if (status) |
| 437 | ret |= BMC150_ACCEL_INT_MAP_1_BIT_DATA; |
| 438 | else |
| 439 | ret &= ~BMC150_ACCEL_INT_MAP_1_BIT_DATA; |
| 440 | |
| 441 | ret = i2c_smbus_write_byte_data(data->client, |
| 442 | BMC150_ACCEL_REG_INT_MAP_1, |
| 443 | ret); |
| 444 | if (ret < 0) { |
| 445 | dev_err(&data->client->dev, "Error writing reg_int_map_1\n"); |
| 446 | return ret; |
| 447 | } |
| 448 | |
| 449 | if (status) { |
| 450 | /* |
| 451 | * Set non latched mode interrupt and clear any latched |
| 452 | * interrupt |
| 453 | */ |
| 454 | ret = i2c_smbus_write_byte_data(data->client, |
| 455 | BMC150_ACCEL_REG_INT_RST_LATCH, |
| 456 | BMC150_ACCEL_INT_MODE_NON_LATCH_INT | |
| 457 | BMC150_ACCEL_INT_MODE_LATCH_RESET); |
| 458 | if (ret < 0) { |
| 459 | dev_err(&data->client->dev, |
| 460 | "Error writing reg_int_rst_latch\n"); |
| 461 | return ret; |
| 462 | } |
| 463 | |
| 464 | ret = i2c_smbus_write_byte_data(data->client, |
| 465 | BMC150_ACCEL_REG_INT_EN_1, |
| 466 | BMC150_ACCEL_INT_EN_BIT_DATA_EN); |
| 467 | |
| 468 | } else { |
| 469 | /* Restore default interrupt mode */ |
| 470 | ret = i2c_smbus_write_byte_data(data->client, |
| 471 | BMC150_ACCEL_REG_INT_RST_LATCH, |
| 472 | BMC150_ACCEL_INT_MODE_LATCH_INT | |
| 473 | BMC150_ACCEL_INT_MODE_LATCH_RESET); |
| 474 | if (ret < 0) { |
| 475 | dev_err(&data->client->dev, |
| 476 | "Error writing reg_int_rst_latch\n"); |
| 477 | return ret; |
| 478 | } |
| 479 | |
| 480 | ret = i2c_smbus_write_byte_data(data->client, |
| 481 | BMC150_ACCEL_REG_INT_EN_1, |
| 482 | 0); |
| 483 | } |
| 484 | |
| 485 | if (ret < 0) { |
| 486 | dev_err(&data->client->dev, "Error writing reg_int_en_1\n"); |
| 487 | return ret; |
| 488 | } |
| 489 | |
| 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | static int bmc150_accel_get_bw(struct bmc150_accel_data *data, int *val, |
| 494 | int *val2) |
| 495 | { |
| 496 | int i; |
| 497 | |
| 498 | for (i = 0; i < ARRAY_SIZE(bmc150_accel_samp_freq_table); ++i) { |
| 499 | if (bmc150_accel_samp_freq_table[i].bw_bits == data->bw_bits) { |
| 500 | *val = bmc150_accel_samp_freq_table[i].val; |
| 501 | *val2 = bmc150_accel_samp_freq_table[i].val2; |
| 502 | return IIO_VAL_INT_PLUS_MICRO; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | return -EINVAL; |
| 507 | } |
| 508 | |
Rafael J. Wysocki | 6f0a13f | 2014-12-04 01:08:13 +0100 | [diff] [blame] | 509 | #ifdef CONFIG_PM |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 510 | static int bmc150_accel_get_startup_times(struct bmc150_accel_data *data) |
| 511 | { |
| 512 | int i; |
| 513 | |
| 514 | for (i = 0; i < ARRAY_SIZE(bmc150_accel_sample_upd_time); ++i) { |
| 515 | if (bmc150_accel_sample_upd_time[i].bw_bits == data->bw_bits) |
| 516 | return bmc150_accel_sample_upd_time[i].msec; |
| 517 | } |
| 518 | |
| 519 | return BMC150_ACCEL_MAX_STARTUP_TIME_MS; |
| 520 | } |
| 521 | |
| 522 | static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on) |
| 523 | { |
| 524 | int ret; |
| 525 | |
| 526 | if (on) |
| 527 | ret = pm_runtime_get_sync(&data->client->dev); |
| 528 | else { |
| 529 | pm_runtime_mark_last_busy(&data->client->dev); |
| 530 | ret = pm_runtime_put_autosuspend(&data->client->dev); |
| 531 | } |
| 532 | if (ret < 0) { |
| 533 | dev_err(&data->client->dev, |
| 534 | "Failed: bmc150_accel_set_power_state for %d\n", on); |
Srinivas Pandruvada | aaeecd8 | 2014-10-10 20:35:31 -0700 | [diff] [blame] | 535 | if (on) |
| 536 | pm_runtime_put_noidle(&data->client->dev); |
| 537 | |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 538 | return ret; |
| 539 | } |
| 540 | |
| 541 | return 0; |
| 542 | } |
Laurentiu Palcu | b31b05c | 2014-08-29 09:38:00 +0100 | [diff] [blame] | 543 | #else |
| 544 | static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on) |
| 545 | { |
| 546 | return 0; |
| 547 | } |
| 548 | #endif |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 549 | |
| 550 | static int bmc150_accel_set_scale(struct bmc150_accel_data *data, int val) |
| 551 | { |
| 552 | int ret, i; |
| 553 | |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 554 | for (i = 0; i < ARRAY_SIZE(data->chip_info->scale_table); ++i) { |
| 555 | if (data->chip_info->scale_table[i].scale == val) { |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 556 | ret = i2c_smbus_write_byte_data( |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 557 | data->client, |
| 558 | BMC150_ACCEL_REG_PMU_RANGE, |
| 559 | data->chip_info->scale_table[i].reg_range); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 560 | if (ret < 0) { |
| 561 | dev_err(&data->client->dev, |
| 562 | "Error writing pmu_range\n"); |
| 563 | return ret; |
| 564 | } |
| 565 | |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 566 | data->range = data->chip_info->scale_table[i].reg_range; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 567 | return 0; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | return -EINVAL; |
| 572 | } |
| 573 | |
| 574 | static int bmc150_accel_get_temp(struct bmc150_accel_data *data, int *val) |
| 575 | { |
| 576 | int ret; |
| 577 | |
| 578 | mutex_lock(&data->mutex); |
| 579 | |
| 580 | ret = i2c_smbus_read_byte_data(data->client, BMC150_ACCEL_REG_TEMP); |
| 581 | if (ret < 0) { |
| 582 | dev_err(&data->client->dev, "Error reading reg_temp\n"); |
| 583 | mutex_unlock(&data->mutex); |
| 584 | return ret; |
| 585 | } |
| 586 | *val = sign_extend32(ret, 7); |
| 587 | |
| 588 | mutex_unlock(&data->mutex); |
| 589 | |
| 590 | return IIO_VAL_INT; |
| 591 | } |
| 592 | |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 593 | static int bmc150_accel_get_axis(struct bmc150_accel_data *data, |
| 594 | struct iio_chan_spec const *chan, |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 595 | int *val) |
| 596 | { |
| 597 | int ret; |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 598 | int axis = chan->scan_index; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 599 | |
| 600 | mutex_lock(&data->mutex); |
| 601 | ret = bmc150_accel_set_power_state(data, true); |
| 602 | if (ret < 0) { |
| 603 | mutex_unlock(&data->mutex); |
| 604 | return ret; |
| 605 | } |
| 606 | |
| 607 | ret = i2c_smbus_read_word_data(data->client, |
| 608 | BMC150_ACCEL_AXIS_TO_REG(axis)); |
| 609 | if (ret < 0) { |
| 610 | dev_err(&data->client->dev, "Error reading axis %d\n", axis); |
| 611 | bmc150_accel_set_power_state(data, false); |
| 612 | mutex_unlock(&data->mutex); |
| 613 | return ret; |
| 614 | } |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 615 | *val = sign_extend32(ret >> chan->scan_type.shift, |
| 616 | chan->scan_type.realbits - 1); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 617 | ret = bmc150_accel_set_power_state(data, false); |
| 618 | mutex_unlock(&data->mutex); |
| 619 | if (ret < 0) |
| 620 | return ret; |
| 621 | |
| 622 | return IIO_VAL_INT; |
| 623 | } |
| 624 | |
| 625 | static int bmc150_accel_read_raw(struct iio_dev *indio_dev, |
| 626 | struct iio_chan_spec const *chan, |
| 627 | int *val, int *val2, long mask) |
| 628 | { |
| 629 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 630 | int ret; |
| 631 | |
| 632 | switch (mask) { |
| 633 | case IIO_CHAN_INFO_RAW: |
| 634 | switch (chan->type) { |
| 635 | case IIO_TEMP: |
| 636 | return bmc150_accel_get_temp(data, val); |
| 637 | case IIO_ACCEL: |
| 638 | if (iio_buffer_enabled(indio_dev)) |
| 639 | return -EBUSY; |
| 640 | else |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 641 | return bmc150_accel_get_axis(data, chan, val); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 642 | default: |
| 643 | return -EINVAL; |
| 644 | } |
| 645 | case IIO_CHAN_INFO_OFFSET: |
| 646 | if (chan->type == IIO_TEMP) { |
| 647 | *val = BMC150_ACCEL_TEMP_CENTER_VAL; |
| 648 | return IIO_VAL_INT; |
| 649 | } else |
| 650 | return -EINVAL; |
| 651 | case IIO_CHAN_INFO_SCALE: |
| 652 | *val = 0; |
| 653 | switch (chan->type) { |
| 654 | case IIO_TEMP: |
| 655 | *val2 = 500000; |
| 656 | return IIO_VAL_INT_PLUS_MICRO; |
| 657 | case IIO_ACCEL: |
| 658 | { |
| 659 | int i; |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 660 | const struct bmc150_scale_info *si; |
| 661 | int st_size = ARRAY_SIZE(data->chip_info->scale_table); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 662 | |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 663 | for (i = 0; i < st_size; ++i) { |
| 664 | si = &data->chip_info->scale_table[i]; |
| 665 | if (si->reg_range == data->range) { |
| 666 | *val2 = si->scale; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 667 | return IIO_VAL_INT_PLUS_MICRO; |
| 668 | } |
| 669 | } |
| 670 | return -EINVAL; |
| 671 | } |
| 672 | default: |
| 673 | return -EINVAL; |
| 674 | } |
| 675 | case IIO_CHAN_INFO_SAMP_FREQ: |
| 676 | mutex_lock(&data->mutex); |
| 677 | ret = bmc150_accel_get_bw(data, val, val2); |
| 678 | mutex_unlock(&data->mutex); |
| 679 | return ret; |
| 680 | default: |
| 681 | return -EINVAL; |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | static int bmc150_accel_write_raw(struct iio_dev *indio_dev, |
| 686 | struct iio_chan_spec const *chan, |
| 687 | int val, int val2, long mask) |
| 688 | { |
| 689 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 690 | int ret; |
| 691 | |
| 692 | switch (mask) { |
| 693 | case IIO_CHAN_INFO_SAMP_FREQ: |
| 694 | mutex_lock(&data->mutex); |
| 695 | ret = bmc150_accel_set_bw(data, val, val2); |
| 696 | mutex_unlock(&data->mutex); |
| 697 | break; |
| 698 | case IIO_CHAN_INFO_SCALE: |
| 699 | if (val) |
| 700 | return -EINVAL; |
| 701 | |
| 702 | mutex_lock(&data->mutex); |
| 703 | ret = bmc150_accel_set_scale(data, val2); |
| 704 | mutex_unlock(&data->mutex); |
| 705 | return ret; |
| 706 | default: |
| 707 | ret = -EINVAL; |
| 708 | } |
| 709 | |
| 710 | return ret; |
| 711 | } |
| 712 | |
| 713 | static int bmc150_accel_read_event(struct iio_dev *indio_dev, |
| 714 | const struct iio_chan_spec *chan, |
| 715 | enum iio_event_type type, |
| 716 | enum iio_event_direction dir, |
| 717 | enum iio_event_info info, |
| 718 | int *val, int *val2) |
| 719 | { |
| 720 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 721 | |
| 722 | *val2 = 0; |
| 723 | switch (info) { |
| 724 | case IIO_EV_INFO_VALUE: |
| 725 | *val = data->slope_thres; |
| 726 | break; |
| 727 | case IIO_EV_INFO_PERIOD: |
Octavian Purdila | 802a3ae | 2015-01-31 02:00:03 +0200 | [diff] [blame^] | 728 | *val = data->slope_dur; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 729 | break; |
| 730 | default: |
| 731 | return -EINVAL; |
| 732 | } |
| 733 | |
| 734 | return IIO_VAL_INT; |
| 735 | } |
| 736 | |
| 737 | static int bmc150_accel_write_event(struct iio_dev *indio_dev, |
| 738 | const struct iio_chan_spec *chan, |
| 739 | enum iio_event_type type, |
| 740 | enum iio_event_direction dir, |
| 741 | enum iio_event_info info, |
| 742 | int val, int val2) |
| 743 | { |
| 744 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 745 | |
| 746 | if (data->ev_enable_state) |
| 747 | return -EBUSY; |
| 748 | |
| 749 | switch (info) { |
| 750 | case IIO_EV_INFO_VALUE: |
Octavian Purdila | 802a3ae | 2015-01-31 02:00:03 +0200 | [diff] [blame^] | 751 | data->slope_thres = val & 0xFF; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 752 | break; |
| 753 | case IIO_EV_INFO_PERIOD: |
Octavian Purdila | 802a3ae | 2015-01-31 02:00:03 +0200 | [diff] [blame^] | 754 | data->slope_dur = val & BMC150_ACCEL_SLOPE_DUR_MASK; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 755 | break; |
| 756 | default: |
| 757 | return -EINVAL; |
| 758 | } |
| 759 | |
| 760 | return 0; |
| 761 | } |
| 762 | |
| 763 | static int bmc150_accel_read_event_config(struct iio_dev *indio_dev, |
| 764 | const struct iio_chan_spec *chan, |
| 765 | enum iio_event_type type, |
| 766 | enum iio_event_direction dir) |
| 767 | { |
| 768 | |
| 769 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 770 | |
| 771 | return data->ev_enable_state; |
| 772 | } |
| 773 | |
| 774 | static int bmc150_accel_write_event_config(struct iio_dev *indio_dev, |
| 775 | const struct iio_chan_spec *chan, |
| 776 | enum iio_event_type type, |
| 777 | enum iio_event_direction dir, |
| 778 | int state) |
| 779 | { |
| 780 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 781 | int ret; |
| 782 | |
| 783 | if (state && data->ev_enable_state) |
| 784 | return 0; |
| 785 | |
| 786 | mutex_lock(&data->mutex); |
| 787 | |
| 788 | if (!state && data->motion_trigger_on) { |
| 789 | data->ev_enable_state = 0; |
| 790 | mutex_unlock(&data->mutex); |
| 791 | return 0; |
| 792 | } |
| 793 | |
| 794 | /* |
| 795 | * We will expect the enable and disable to do operation in |
| 796 | * in reverse order. This will happen here anyway as our |
| 797 | * resume operation uses sync mode runtime pm calls, the |
| 798 | * suspend operation will be delayed by autosuspend delay |
| 799 | * So the disable operation will still happen in reverse of |
| 800 | * enable operation. When runtime pm is disabled the mode |
| 801 | * is always on so sequence doesn't matter |
| 802 | */ |
| 803 | |
| 804 | ret = bmc150_accel_set_power_state(data, state); |
| 805 | if (ret < 0) { |
| 806 | mutex_unlock(&data->mutex); |
| 807 | return ret; |
| 808 | } |
| 809 | |
| 810 | ret = bmc150_accel_setup_any_motion_interrupt(data, state); |
| 811 | if (ret < 0) { |
Srinivas Pandruvada | aaeecd8 | 2014-10-10 20:35:31 -0700 | [diff] [blame] | 812 | bmc150_accel_set_power_state(data, false); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 813 | mutex_unlock(&data->mutex); |
| 814 | return ret; |
| 815 | } |
| 816 | |
| 817 | data->ev_enable_state = state; |
| 818 | mutex_unlock(&data->mutex); |
| 819 | |
| 820 | return 0; |
| 821 | } |
| 822 | |
| 823 | static int bmc150_accel_validate_trigger(struct iio_dev *indio_dev, |
| 824 | struct iio_trigger *trig) |
| 825 | { |
| 826 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 827 | |
| 828 | if (data->dready_trig != trig && data->motion_trig != trig) |
| 829 | return -EINVAL; |
| 830 | |
| 831 | return 0; |
| 832 | } |
| 833 | |
| 834 | static IIO_CONST_ATTR_SAMP_FREQ_AVAIL( |
| 835 | "7.810000 15.630000 31.250000 62.500000 125 250 500 1000"); |
| 836 | |
| 837 | static struct attribute *bmc150_accel_attributes[] = { |
| 838 | &iio_const_attr_sampling_frequency_available.dev_attr.attr, |
| 839 | NULL, |
| 840 | }; |
| 841 | |
| 842 | static const struct attribute_group bmc150_accel_attrs_group = { |
| 843 | .attrs = bmc150_accel_attributes, |
| 844 | }; |
| 845 | |
| 846 | static const struct iio_event_spec bmc150_accel_event = { |
| 847 | .type = IIO_EV_TYPE_ROC, |
Srinivas Pandruvada | 1174124 | 2014-10-10 20:35:34 -0700 | [diff] [blame] | 848 | .dir = IIO_EV_DIR_EITHER, |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 849 | .mask_separate = BIT(IIO_EV_INFO_VALUE) | |
| 850 | BIT(IIO_EV_INFO_ENABLE) | |
| 851 | BIT(IIO_EV_INFO_PERIOD) |
| 852 | }; |
| 853 | |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 854 | #define BMC150_ACCEL_CHANNEL(_axis, bits) { \ |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 855 | .type = IIO_ACCEL, \ |
| 856 | .modified = 1, \ |
| 857 | .channel2 = IIO_MOD_##_axis, \ |
| 858 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 859 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ |
| 860 | BIT(IIO_CHAN_INFO_SAMP_FREQ), \ |
| 861 | .scan_index = AXIS_##_axis, \ |
| 862 | .scan_type = { \ |
| 863 | .sign = 's', \ |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 864 | .realbits = (bits), \ |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 865 | .storagebits = 16, \ |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 866 | .shift = 16 - (bits), \ |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 867 | }, \ |
| 868 | .event_spec = &bmc150_accel_event, \ |
| 869 | .num_event_specs = 1 \ |
| 870 | } |
| 871 | |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 872 | #define BMC150_ACCEL_CHANNELS(bits) { \ |
| 873 | { \ |
| 874 | .type = IIO_TEMP, \ |
| 875 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
| 876 | BIT(IIO_CHAN_INFO_SCALE) | \ |
| 877 | BIT(IIO_CHAN_INFO_OFFSET), \ |
| 878 | .scan_index = -1, \ |
| 879 | }, \ |
| 880 | BMC150_ACCEL_CHANNEL(X, bits), \ |
| 881 | BMC150_ACCEL_CHANNEL(Y, bits), \ |
| 882 | BMC150_ACCEL_CHANNEL(Z, bits), \ |
| 883 | IIO_CHAN_SOFT_TIMESTAMP(3), \ |
| 884 | } |
| 885 | |
| 886 | static const struct iio_chan_spec bma222e_accel_channels[] = |
| 887 | BMC150_ACCEL_CHANNELS(8); |
| 888 | static const struct iio_chan_spec bma250e_accel_channels[] = |
| 889 | BMC150_ACCEL_CHANNELS(10); |
| 890 | static const struct iio_chan_spec bmc150_accel_channels[] = |
| 891 | BMC150_ACCEL_CHANNELS(12); |
| 892 | static const struct iio_chan_spec bma280_accel_channels[] = |
| 893 | BMC150_ACCEL_CHANNELS(14); |
| 894 | |
| 895 | enum { |
| 896 | bmc150, |
| 897 | bmi055, |
| 898 | bma255, |
| 899 | bma250e, |
| 900 | bma222e, |
| 901 | bma280, |
| 902 | }; |
| 903 | |
| 904 | static const struct bmc150_accel_chip_info bmc150_accel_chip_info_tbl[] = { |
| 905 | [bmc150] = { |
| 906 | .chip_id = 0xFA, |
| 907 | .channels = bmc150_accel_channels, |
| 908 | .num_channels = ARRAY_SIZE(bmc150_accel_channels), |
| 909 | .scale_table = { {9610, BMC150_ACCEL_DEF_RANGE_2G}, |
| 910 | {19122, BMC150_ACCEL_DEF_RANGE_4G}, |
| 911 | {38344, BMC150_ACCEL_DEF_RANGE_8G}, |
| 912 | {76590, BMC150_ACCEL_DEF_RANGE_16G} }, |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 913 | }, |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 914 | [bmi055] = { |
| 915 | .chip_id = 0xFA, |
| 916 | .channels = bmc150_accel_channels, |
| 917 | .num_channels = ARRAY_SIZE(bmc150_accel_channels), |
| 918 | .scale_table = { {9610, BMC150_ACCEL_DEF_RANGE_2G}, |
| 919 | {19122, BMC150_ACCEL_DEF_RANGE_4G}, |
| 920 | {38344, BMC150_ACCEL_DEF_RANGE_8G}, |
| 921 | {76590, BMC150_ACCEL_DEF_RANGE_16G} }, |
| 922 | }, |
| 923 | [bma255] = { |
| 924 | .chip_id = 0xFA, |
| 925 | .channels = bmc150_accel_channels, |
| 926 | .num_channels = ARRAY_SIZE(bmc150_accel_channels), |
| 927 | .scale_table = { {9610, BMC150_ACCEL_DEF_RANGE_2G}, |
| 928 | {19122, BMC150_ACCEL_DEF_RANGE_4G}, |
| 929 | {38344, BMC150_ACCEL_DEF_RANGE_8G}, |
| 930 | {76590, BMC150_ACCEL_DEF_RANGE_16G} }, |
| 931 | }, |
| 932 | [bma250e] = { |
| 933 | .chip_id = 0xF9, |
| 934 | .channels = bma250e_accel_channels, |
| 935 | .num_channels = ARRAY_SIZE(bma250e_accel_channels), |
| 936 | .scale_table = { {38344, BMC150_ACCEL_DEF_RANGE_2G}, |
| 937 | {76590, BMC150_ACCEL_DEF_RANGE_4G}, |
| 938 | {153277, BMC150_ACCEL_DEF_RANGE_8G}, |
| 939 | {306457, BMC150_ACCEL_DEF_RANGE_16G} }, |
| 940 | }, |
| 941 | [bma222e] = { |
| 942 | .chip_id = 0xF8, |
| 943 | .channels = bma222e_accel_channels, |
| 944 | .num_channels = ARRAY_SIZE(bma222e_accel_channels), |
| 945 | .scale_table = { {153277, BMC150_ACCEL_DEF_RANGE_2G}, |
| 946 | {306457, BMC150_ACCEL_DEF_RANGE_4G}, |
| 947 | {612915, BMC150_ACCEL_DEF_RANGE_8G}, |
| 948 | {1225831, BMC150_ACCEL_DEF_RANGE_16G} }, |
| 949 | }, |
| 950 | [bma280] = { |
| 951 | .chip_id = 0xFB, |
| 952 | .channels = bma280_accel_channels, |
| 953 | .num_channels = ARRAY_SIZE(bma280_accel_channels), |
| 954 | .scale_table = { {2392, BMC150_ACCEL_DEF_RANGE_2G}, |
| 955 | {4785, BMC150_ACCEL_DEF_RANGE_4G}, |
| 956 | {9581, BMC150_ACCEL_DEF_RANGE_8G}, |
| 957 | {19152, BMC150_ACCEL_DEF_RANGE_16G} }, |
| 958 | }, |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 959 | }; |
| 960 | |
| 961 | static const struct iio_info bmc150_accel_info = { |
| 962 | .attrs = &bmc150_accel_attrs_group, |
| 963 | .read_raw = bmc150_accel_read_raw, |
| 964 | .write_raw = bmc150_accel_write_raw, |
| 965 | .read_event_value = bmc150_accel_read_event, |
| 966 | .write_event_value = bmc150_accel_write_event, |
| 967 | .write_event_config = bmc150_accel_write_event_config, |
| 968 | .read_event_config = bmc150_accel_read_event_config, |
| 969 | .validate_trigger = bmc150_accel_validate_trigger, |
| 970 | .driver_module = THIS_MODULE, |
| 971 | }; |
| 972 | |
| 973 | static irqreturn_t bmc150_accel_trigger_handler(int irq, void *p) |
| 974 | { |
| 975 | struct iio_poll_func *pf = p; |
| 976 | struct iio_dev *indio_dev = pf->indio_dev; |
| 977 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 978 | int bit, ret, i = 0; |
| 979 | |
| 980 | mutex_lock(&data->mutex); |
| 981 | for_each_set_bit(bit, indio_dev->buffer->scan_mask, |
| 982 | indio_dev->masklength) { |
| 983 | ret = i2c_smbus_read_word_data(data->client, |
| 984 | BMC150_ACCEL_AXIS_TO_REG(bit)); |
| 985 | if (ret < 0) { |
| 986 | mutex_unlock(&data->mutex); |
| 987 | goto err_read; |
| 988 | } |
| 989 | data->buffer[i++] = ret; |
| 990 | } |
| 991 | mutex_unlock(&data->mutex); |
| 992 | |
| 993 | iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, |
| 994 | data->timestamp); |
| 995 | err_read: |
| 996 | iio_trigger_notify_done(indio_dev->trig); |
| 997 | |
| 998 | return IRQ_HANDLED; |
| 999 | } |
| 1000 | |
| 1001 | static int bmc150_accel_trig_try_reen(struct iio_trigger *trig) |
| 1002 | { |
| 1003 | struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); |
| 1004 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 1005 | int ret; |
| 1006 | |
| 1007 | /* new data interrupts don't need ack */ |
| 1008 | if (data->dready_trigger_on) |
| 1009 | return 0; |
| 1010 | |
| 1011 | mutex_lock(&data->mutex); |
| 1012 | /* clear any latched interrupt */ |
| 1013 | ret = i2c_smbus_write_byte_data(data->client, |
| 1014 | BMC150_ACCEL_REG_INT_RST_LATCH, |
| 1015 | BMC150_ACCEL_INT_MODE_LATCH_INT | |
| 1016 | BMC150_ACCEL_INT_MODE_LATCH_RESET); |
| 1017 | mutex_unlock(&data->mutex); |
| 1018 | if (ret < 0) { |
| 1019 | dev_err(&data->client->dev, |
| 1020 | "Error writing reg_int_rst_latch\n"); |
| 1021 | return ret; |
| 1022 | } |
| 1023 | |
| 1024 | return 0; |
| 1025 | } |
| 1026 | |
| 1027 | static int bmc150_accel_data_rdy_trigger_set_state(struct iio_trigger *trig, |
| 1028 | bool state) |
| 1029 | { |
| 1030 | struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); |
| 1031 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 1032 | int ret; |
| 1033 | |
| 1034 | mutex_lock(&data->mutex); |
| 1035 | |
| 1036 | if (!state && data->ev_enable_state && data->motion_trigger_on) { |
| 1037 | data->motion_trigger_on = false; |
| 1038 | mutex_unlock(&data->mutex); |
| 1039 | return 0; |
| 1040 | } |
| 1041 | |
| 1042 | /* |
| 1043 | * Refer to comment in bmc150_accel_write_event_config for |
| 1044 | * enable/disable operation order |
| 1045 | */ |
| 1046 | ret = bmc150_accel_set_power_state(data, state); |
| 1047 | if (ret < 0) { |
| 1048 | mutex_unlock(&data->mutex); |
| 1049 | return ret; |
| 1050 | } |
Octavian Purdila | 802a3ae | 2015-01-31 02:00:03 +0200 | [diff] [blame^] | 1051 | |
| 1052 | if (data->motion_trig == trig) { |
| 1053 | ret = bmc150_accel_update_slope(data); |
| 1054 | if (!ret) |
| 1055 | ret = bmc150_accel_setup_any_motion_interrupt(data, |
| 1056 | state); |
| 1057 | } else { |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1058 | ret = bmc150_accel_setup_new_data_interrupt(data, state); |
Octavian Purdila | 802a3ae | 2015-01-31 02:00:03 +0200 | [diff] [blame^] | 1059 | } |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1060 | if (ret < 0) { |
Srinivas Pandruvada | aaeecd8 | 2014-10-10 20:35:31 -0700 | [diff] [blame] | 1061 | bmc150_accel_set_power_state(data, false); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1062 | mutex_unlock(&data->mutex); |
| 1063 | return ret; |
| 1064 | } |
| 1065 | if (data->motion_trig == trig) |
| 1066 | data->motion_trigger_on = state; |
| 1067 | else |
| 1068 | data->dready_trigger_on = state; |
| 1069 | |
| 1070 | mutex_unlock(&data->mutex); |
| 1071 | |
| 1072 | return ret; |
| 1073 | } |
| 1074 | |
| 1075 | static const struct iio_trigger_ops bmc150_accel_trigger_ops = { |
| 1076 | .set_trigger_state = bmc150_accel_data_rdy_trigger_set_state, |
| 1077 | .try_reenable = bmc150_accel_trig_try_reen, |
| 1078 | .owner = THIS_MODULE, |
| 1079 | }; |
| 1080 | |
| 1081 | static irqreturn_t bmc150_accel_event_handler(int irq, void *private) |
| 1082 | { |
| 1083 | struct iio_dev *indio_dev = private; |
| 1084 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 1085 | int ret; |
| 1086 | int dir; |
| 1087 | |
| 1088 | ret = i2c_smbus_read_byte_data(data->client, |
| 1089 | BMC150_ACCEL_REG_INT_STATUS_2); |
| 1090 | if (ret < 0) { |
| 1091 | dev_err(&data->client->dev, "Error reading reg_int_status_2\n"); |
| 1092 | goto ack_intr_status; |
| 1093 | } |
| 1094 | |
| 1095 | if (ret & BMC150_ACCEL_ANY_MOTION_BIT_SIGN) |
| 1096 | dir = IIO_EV_DIR_FALLING; |
| 1097 | else |
| 1098 | dir = IIO_EV_DIR_RISING; |
| 1099 | |
Srinivas Pandruvada | 8d5a978 | 2014-10-10 20:35:32 -0700 | [diff] [blame] | 1100 | if (ret & BMC150_ACCEL_ANY_MOTION_BIT_X) |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1101 | iio_push_event(indio_dev, IIO_MOD_EVENT_CODE(IIO_ACCEL, |
| 1102 | 0, |
Srinivas Pandruvada | 8d5a978 | 2014-10-10 20:35:32 -0700 | [diff] [blame] | 1103 | IIO_MOD_X, |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1104 | IIO_EV_TYPE_ROC, |
Srinivas Pandruvada | 8d5a978 | 2014-10-10 20:35:32 -0700 | [diff] [blame] | 1105 | dir), |
| 1106 | data->timestamp); |
| 1107 | if (ret & BMC150_ACCEL_ANY_MOTION_BIT_Y) |
| 1108 | iio_push_event(indio_dev, IIO_MOD_EVENT_CODE(IIO_ACCEL, |
| 1109 | 0, |
| 1110 | IIO_MOD_Y, |
| 1111 | IIO_EV_TYPE_ROC, |
| 1112 | dir), |
| 1113 | data->timestamp); |
| 1114 | if (ret & BMC150_ACCEL_ANY_MOTION_BIT_Z) |
| 1115 | iio_push_event(indio_dev, IIO_MOD_EVENT_CODE(IIO_ACCEL, |
| 1116 | 0, |
| 1117 | IIO_MOD_Z, |
| 1118 | IIO_EV_TYPE_ROC, |
| 1119 | dir), |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1120 | data->timestamp); |
| 1121 | ack_intr_status: |
| 1122 | if (!data->dready_trigger_on) |
| 1123 | ret = i2c_smbus_write_byte_data(data->client, |
| 1124 | BMC150_ACCEL_REG_INT_RST_LATCH, |
| 1125 | BMC150_ACCEL_INT_MODE_LATCH_INT | |
| 1126 | BMC150_ACCEL_INT_MODE_LATCH_RESET); |
| 1127 | |
| 1128 | return IRQ_HANDLED; |
| 1129 | } |
| 1130 | |
| 1131 | static irqreturn_t bmc150_accel_data_rdy_trig_poll(int irq, void *private) |
| 1132 | { |
| 1133 | struct iio_dev *indio_dev = private; |
| 1134 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 1135 | |
| 1136 | data->timestamp = iio_get_time_ns(); |
| 1137 | |
| 1138 | if (data->dready_trigger_on) |
| 1139 | iio_trigger_poll(data->dready_trig); |
| 1140 | else if (data->motion_trigger_on) |
| 1141 | iio_trigger_poll(data->motion_trig); |
| 1142 | |
| 1143 | if (data->ev_enable_state) |
| 1144 | return IRQ_WAKE_THREAD; |
| 1145 | else |
| 1146 | return IRQ_HANDLED; |
| 1147 | } |
| 1148 | |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 1149 | static const char *bmc150_accel_match_acpi_device(struct device *dev, int *data) |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1150 | { |
| 1151 | const struct acpi_device_id *id; |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 1152 | |
| 1153 | id = acpi_match_device(dev->driver->acpi_match_table, dev); |
| 1154 | |
| 1155 | if (!id) |
| 1156 | return NULL; |
| 1157 | |
| 1158 | *data = (int) id->driver_data; |
| 1159 | |
| 1160 | return dev_name(dev); |
| 1161 | } |
| 1162 | |
| 1163 | static int bmc150_accel_gpio_probe(struct i2c_client *client, |
| 1164 | struct bmc150_accel_data *data) |
| 1165 | { |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1166 | struct device *dev; |
| 1167 | struct gpio_desc *gpio; |
| 1168 | int ret; |
| 1169 | |
| 1170 | if (!client) |
| 1171 | return -EINVAL; |
| 1172 | |
| 1173 | dev = &client->dev; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1174 | |
| 1175 | /* data ready gpio interrupt pin */ |
| 1176 | gpio = devm_gpiod_get_index(dev, BMC150_ACCEL_GPIO_NAME, 0); |
| 1177 | if (IS_ERR(gpio)) { |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 1178 | dev_err(dev, "Failed: gpio get index\n"); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1179 | return PTR_ERR(gpio); |
| 1180 | } |
| 1181 | |
| 1182 | ret = gpiod_direction_input(gpio); |
| 1183 | if (ret) |
| 1184 | return ret; |
| 1185 | |
| 1186 | ret = gpiod_to_irq(gpio); |
| 1187 | |
| 1188 | dev_dbg(dev, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret); |
| 1189 | |
| 1190 | return ret; |
| 1191 | } |
| 1192 | |
| 1193 | static int bmc150_accel_probe(struct i2c_client *client, |
| 1194 | const struct i2c_device_id *id) |
| 1195 | { |
| 1196 | struct bmc150_accel_data *data; |
| 1197 | struct iio_dev *indio_dev; |
| 1198 | int ret; |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 1199 | const char *name = NULL; |
| 1200 | int chip_id = 0; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1201 | |
| 1202 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 1203 | if (!indio_dev) |
| 1204 | return -ENOMEM; |
| 1205 | |
| 1206 | data = iio_priv(indio_dev); |
| 1207 | i2c_set_clientdata(client, indio_dev); |
| 1208 | data->client = client; |
| 1209 | |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 1210 | if (id) { |
| 1211 | name = id->name; |
| 1212 | chip_id = id->driver_data; |
| 1213 | } |
| 1214 | |
| 1215 | if (ACPI_HANDLE(&client->dev)) |
| 1216 | name = bmc150_accel_match_acpi_device(&client->dev, &chip_id); |
| 1217 | |
| 1218 | data->chip_info = &bmc150_accel_chip_info_tbl[chip_id]; |
| 1219 | |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1220 | ret = bmc150_accel_chip_init(data); |
| 1221 | if (ret < 0) |
| 1222 | return ret; |
| 1223 | |
| 1224 | mutex_init(&data->mutex); |
| 1225 | |
| 1226 | indio_dev->dev.parent = &client->dev; |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 1227 | indio_dev->channels = data->chip_info->channels; |
| 1228 | indio_dev->num_channels = data->chip_info->num_channels; |
| 1229 | indio_dev->name = name; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1230 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 1231 | indio_dev->info = &bmc150_accel_info; |
| 1232 | |
| 1233 | if (client->irq < 0) |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 1234 | client->irq = bmc150_accel_gpio_probe(client, data); |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1235 | |
| 1236 | if (client->irq >= 0) { |
| 1237 | ret = devm_request_threaded_irq( |
| 1238 | &client->dev, client->irq, |
| 1239 | bmc150_accel_data_rdy_trig_poll, |
| 1240 | bmc150_accel_event_handler, |
| 1241 | IRQF_TRIGGER_RISING, |
| 1242 | BMC150_ACCEL_IRQ_NAME, |
| 1243 | indio_dev); |
| 1244 | if (ret) |
| 1245 | return ret; |
| 1246 | |
| 1247 | data->dready_trig = devm_iio_trigger_alloc(&client->dev, |
| 1248 | "%s-dev%d", |
| 1249 | indio_dev->name, |
| 1250 | indio_dev->id); |
| 1251 | if (!data->dready_trig) |
| 1252 | return -ENOMEM; |
| 1253 | |
| 1254 | data->motion_trig = devm_iio_trigger_alloc(&client->dev, |
| 1255 | "%s-any-motion-dev%d", |
| 1256 | indio_dev->name, |
| 1257 | indio_dev->id); |
| 1258 | if (!data->motion_trig) |
| 1259 | return -ENOMEM; |
| 1260 | |
| 1261 | data->dready_trig->dev.parent = &client->dev; |
| 1262 | data->dready_trig->ops = &bmc150_accel_trigger_ops; |
| 1263 | iio_trigger_set_drvdata(data->dready_trig, indio_dev); |
| 1264 | ret = iio_trigger_register(data->dready_trig); |
| 1265 | if (ret) |
| 1266 | return ret; |
| 1267 | |
| 1268 | data->motion_trig->dev.parent = &client->dev; |
| 1269 | data->motion_trig->ops = &bmc150_accel_trigger_ops; |
| 1270 | iio_trigger_set_drvdata(data->motion_trig, indio_dev); |
| 1271 | ret = iio_trigger_register(data->motion_trig); |
| 1272 | if (ret) { |
| 1273 | data->motion_trig = NULL; |
| 1274 | goto err_trigger_unregister; |
| 1275 | } |
| 1276 | |
| 1277 | ret = iio_triggered_buffer_setup(indio_dev, |
| 1278 | &iio_pollfunc_store_time, |
| 1279 | bmc150_accel_trigger_handler, |
| 1280 | NULL); |
| 1281 | if (ret < 0) { |
| 1282 | dev_err(&client->dev, |
| 1283 | "Failed: iio triggered buffer setup\n"); |
| 1284 | goto err_trigger_unregister; |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | ret = iio_device_register(indio_dev); |
| 1289 | if (ret < 0) { |
| 1290 | dev_err(&client->dev, "Unable to register iio device\n"); |
| 1291 | goto err_buffer_cleanup; |
| 1292 | } |
| 1293 | |
| 1294 | ret = pm_runtime_set_active(&client->dev); |
| 1295 | if (ret) |
| 1296 | goto err_iio_unregister; |
| 1297 | |
| 1298 | pm_runtime_enable(&client->dev); |
| 1299 | pm_runtime_set_autosuspend_delay(&client->dev, |
| 1300 | BMC150_AUTO_SUSPEND_DELAY_MS); |
| 1301 | pm_runtime_use_autosuspend(&client->dev); |
| 1302 | |
| 1303 | return 0; |
| 1304 | |
| 1305 | err_iio_unregister: |
| 1306 | iio_device_unregister(indio_dev); |
| 1307 | err_buffer_cleanup: |
| 1308 | if (data->dready_trig) |
| 1309 | iio_triggered_buffer_cleanup(indio_dev); |
| 1310 | err_trigger_unregister: |
| 1311 | if (data->dready_trig) |
| 1312 | iio_trigger_unregister(data->dready_trig); |
| 1313 | if (data->motion_trig) |
| 1314 | iio_trigger_unregister(data->motion_trig); |
| 1315 | |
| 1316 | return ret; |
| 1317 | } |
| 1318 | |
| 1319 | static int bmc150_accel_remove(struct i2c_client *client) |
| 1320 | { |
| 1321 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 1322 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 1323 | |
| 1324 | pm_runtime_disable(&client->dev); |
| 1325 | pm_runtime_set_suspended(&client->dev); |
| 1326 | pm_runtime_put_noidle(&client->dev); |
| 1327 | |
| 1328 | iio_device_unregister(indio_dev); |
| 1329 | |
| 1330 | if (data->dready_trig) { |
| 1331 | iio_triggered_buffer_cleanup(indio_dev); |
| 1332 | iio_trigger_unregister(data->dready_trig); |
| 1333 | iio_trigger_unregister(data->motion_trig); |
| 1334 | } |
| 1335 | |
| 1336 | mutex_lock(&data->mutex); |
| 1337 | bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_DEEP_SUSPEND, 0); |
| 1338 | mutex_unlock(&data->mutex); |
| 1339 | |
| 1340 | return 0; |
| 1341 | } |
| 1342 | |
| 1343 | #ifdef CONFIG_PM_SLEEP |
| 1344 | static int bmc150_accel_suspend(struct device *dev) |
| 1345 | { |
| 1346 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 1347 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 1348 | |
| 1349 | mutex_lock(&data->mutex); |
| 1350 | bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_SUSPEND, 0); |
| 1351 | mutex_unlock(&data->mutex); |
| 1352 | |
| 1353 | return 0; |
| 1354 | } |
| 1355 | |
| 1356 | static int bmc150_accel_resume(struct device *dev) |
| 1357 | { |
| 1358 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 1359 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 1360 | |
| 1361 | mutex_lock(&data->mutex); |
| 1362 | if (data->dready_trigger_on || data->motion_trigger_on || |
| 1363 | data->ev_enable_state) |
| 1364 | bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0); |
| 1365 | mutex_unlock(&data->mutex); |
| 1366 | |
| 1367 | return 0; |
| 1368 | } |
| 1369 | #endif |
| 1370 | |
Rafael J. Wysocki | 6f0a13f | 2014-12-04 01:08:13 +0100 | [diff] [blame] | 1371 | #ifdef CONFIG_PM |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1372 | static int bmc150_accel_runtime_suspend(struct device *dev) |
| 1373 | { |
| 1374 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 1375 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
Srinivas Pandruvada | aaeecd8 | 2014-10-10 20:35:31 -0700 | [diff] [blame] | 1376 | int ret; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1377 | |
| 1378 | dev_dbg(&data->client->dev, __func__); |
Srinivas Pandruvada | aaeecd8 | 2014-10-10 20:35:31 -0700 | [diff] [blame] | 1379 | ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_SUSPEND, 0); |
| 1380 | if (ret < 0) |
| 1381 | return -EAGAIN; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1382 | |
Srinivas Pandruvada | aaeecd8 | 2014-10-10 20:35:31 -0700 | [diff] [blame] | 1383 | return 0; |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | static int bmc150_accel_runtime_resume(struct device *dev) |
| 1387 | { |
| 1388 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 1389 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
| 1390 | int ret; |
| 1391 | int sleep_val; |
| 1392 | |
| 1393 | dev_dbg(&data->client->dev, __func__); |
| 1394 | |
| 1395 | ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0); |
| 1396 | if (ret < 0) |
| 1397 | return ret; |
| 1398 | |
| 1399 | sleep_val = bmc150_accel_get_startup_times(data); |
| 1400 | if (sleep_val < 20) |
| 1401 | usleep_range(sleep_val * 1000, 20000); |
| 1402 | else |
| 1403 | msleep_interruptible(sleep_val); |
| 1404 | |
| 1405 | return 0; |
| 1406 | } |
| 1407 | #endif |
| 1408 | |
| 1409 | static const struct dev_pm_ops bmc150_accel_pm_ops = { |
| 1410 | SET_SYSTEM_SLEEP_PM_OPS(bmc150_accel_suspend, bmc150_accel_resume) |
| 1411 | SET_RUNTIME_PM_OPS(bmc150_accel_runtime_suspend, |
| 1412 | bmc150_accel_runtime_resume, NULL) |
| 1413 | }; |
| 1414 | |
| 1415 | static const struct acpi_device_id bmc150_accel_acpi_match[] = { |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 1416 | {"BSBA0150", bmc150}, |
| 1417 | {"BMC150A", bmc150}, |
| 1418 | {"BMI055A", bmi055}, |
| 1419 | {"BMA0255", bma255}, |
| 1420 | {"BMA250E", bma250e}, |
| 1421 | {"BMA222E", bma222e}, |
| 1422 | {"BMA0280", bma280}, |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1423 | { }, |
| 1424 | }; |
| 1425 | MODULE_DEVICE_TABLE(acpi, bmc150_accel_acpi_match); |
| 1426 | |
| 1427 | static const struct i2c_device_id bmc150_accel_id[] = { |
Laurentiu Palcu | 8ecbb3c | 2014-02-09 10:30:00 +0000 | [diff] [blame] | 1428 | {"bmc150_accel", bmc150}, |
| 1429 | {"bmi055_accel", bmi055}, |
| 1430 | {"bma255", bma255}, |
| 1431 | {"bma250e", bma250e}, |
| 1432 | {"bma222e", bma222e}, |
| 1433 | {"bma280", bma280}, |
Srinivas Pandruvada | bd7fe5b | 2014-05-08 22:57:00 +0100 | [diff] [blame] | 1434 | {} |
| 1435 | }; |
| 1436 | |
| 1437 | MODULE_DEVICE_TABLE(i2c, bmc150_accel_id); |
| 1438 | |
| 1439 | static struct i2c_driver bmc150_accel_driver = { |
| 1440 | .driver = { |
| 1441 | .name = BMC150_ACCEL_DRV_NAME, |
| 1442 | .acpi_match_table = ACPI_PTR(bmc150_accel_acpi_match), |
| 1443 | .pm = &bmc150_accel_pm_ops, |
| 1444 | }, |
| 1445 | .probe = bmc150_accel_probe, |
| 1446 | .remove = bmc150_accel_remove, |
| 1447 | .id_table = bmc150_accel_id, |
| 1448 | }; |
| 1449 | module_i2c_driver(bmc150_accel_driver); |
| 1450 | |
| 1451 | MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>"); |
| 1452 | MODULE_LICENSE("GPL v2"); |
| 1453 | MODULE_DESCRIPTION("BMC150 accelerometer driver"); |