Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Bosch BMC150 three-axis magnetic field sensor driver |
| 3 | * |
| 4 | * Copyright (c) 2015, Intel Corporation. |
| 5 | * |
| 6 | * This code is based on bmm050_api.c authored by contact@bosch.sensortec.com: |
| 7 | * |
| 8 | * (C) Copyright 2011~2014 Bosch Sensortec GmbH All Rights Reserved |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify it |
| 11 | * under the terms and conditions of the GNU General Public License, |
| 12 | * version 2, as published by the Free Software Foundation. |
| 13 | * |
| 14 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 15 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 17 | * more details. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/i2c.h> |
| 22 | #include <linux/interrupt.h> |
| 23 | #include <linux/delay.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/acpi.h> |
| 26 | #include <linux/gpio/consumer.h> |
| 27 | #include <linux/pm.h> |
| 28 | #include <linux/pm_runtime.h> |
| 29 | #include <linux/iio/iio.h> |
| 30 | #include <linux/iio/sysfs.h> |
| 31 | #include <linux/iio/buffer.h> |
| 32 | #include <linux/iio/events.h> |
| 33 | #include <linux/iio/trigger.h> |
| 34 | #include <linux/iio/trigger_consumer.h> |
| 35 | #include <linux/iio/triggered_buffer.h> |
| 36 | #include <linux/regmap.h> |
| 37 | |
| 38 | #define BMC150_MAGN_DRV_NAME "bmc150_magn" |
| 39 | #define BMC150_MAGN_IRQ_NAME "bmc150_magn_event" |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 40 | |
| 41 | #define BMC150_MAGN_REG_CHIP_ID 0x40 |
| 42 | #define BMC150_MAGN_CHIP_ID_VAL 0x32 |
| 43 | |
| 44 | #define BMC150_MAGN_REG_X_L 0x42 |
| 45 | #define BMC150_MAGN_REG_X_M 0x43 |
| 46 | #define BMC150_MAGN_REG_Y_L 0x44 |
| 47 | #define BMC150_MAGN_REG_Y_M 0x45 |
| 48 | #define BMC150_MAGN_SHIFT_XY_L 3 |
| 49 | #define BMC150_MAGN_REG_Z_L 0x46 |
| 50 | #define BMC150_MAGN_REG_Z_M 0x47 |
| 51 | #define BMC150_MAGN_SHIFT_Z_L 1 |
| 52 | #define BMC150_MAGN_REG_RHALL_L 0x48 |
| 53 | #define BMC150_MAGN_REG_RHALL_M 0x49 |
| 54 | #define BMC150_MAGN_SHIFT_RHALL_L 2 |
| 55 | |
| 56 | #define BMC150_MAGN_REG_INT_STATUS 0x4A |
| 57 | |
| 58 | #define BMC150_MAGN_REG_POWER 0x4B |
| 59 | #define BMC150_MAGN_MASK_POWER_CTL BIT(0) |
| 60 | |
| 61 | #define BMC150_MAGN_REG_OPMODE_ODR 0x4C |
| 62 | #define BMC150_MAGN_MASK_OPMODE GENMASK(2, 1) |
| 63 | #define BMC150_MAGN_SHIFT_OPMODE 1 |
| 64 | #define BMC150_MAGN_MODE_NORMAL 0x00 |
| 65 | #define BMC150_MAGN_MODE_FORCED 0x01 |
| 66 | #define BMC150_MAGN_MODE_SLEEP 0x03 |
| 67 | #define BMC150_MAGN_MASK_ODR GENMASK(5, 3) |
| 68 | #define BMC150_MAGN_SHIFT_ODR 3 |
| 69 | |
| 70 | #define BMC150_MAGN_REG_INT 0x4D |
| 71 | |
| 72 | #define BMC150_MAGN_REG_INT_DRDY 0x4E |
| 73 | #define BMC150_MAGN_MASK_DRDY_EN BIT(7) |
| 74 | #define BMC150_MAGN_SHIFT_DRDY_EN 7 |
| 75 | #define BMC150_MAGN_MASK_DRDY_INT3 BIT(6) |
| 76 | #define BMC150_MAGN_MASK_DRDY_Z_EN BIT(5) |
| 77 | #define BMC150_MAGN_MASK_DRDY_Y_EN BIT(4) |
| 78 | #define BMC150_MAGN_MASK_DRDY_X_EN BIT(3) |
| 79 | #define BMC150_MAGN_MASK_DRDY_DR_POLARITY BIT(2) |
| 80 | #define BMC150_MAGN_MASK_DRDY_LATCHING BIT(1) |
| 81 | #define BMC150_MAGN_MASK_DRDY_INT3_POLARITY BIT(0) |
| 82 | |
| 83 | #define BMC150_MAGN_REG_LOW_THRESH 0x4F |
| 84 | #define BMC150_MAGN_REG_HIGH_THRESH 0x50 |
| 85 | #define BMC150_MAGN_REG_REP_XY 0x51 |
| 86 | #define BMC150_MAGN_REG_REP_Z 0x52 |
Hartmut Knaack | 1506f3c | 2015-07-17 10:52:31 +0200 | [diff] [blame] | 87 | #define BMC150_MAGN_REG_REP_DATAMASK GENMASK(7, 0) |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 88 | |
| 89 | #define BMC150_MAGN_REG_TRIM_START 0x5D |
| 90 | #define BMC150_MAGN_REG_TRIM_END 0x71 |
| 91 | |
| 92 | #define BMC150_MAGN_XY_OVERFLOW_VAL -4096 |
| 93 | #define BMC150_MAGN_Z_OVERFLOW_VAL -16384 |
| 94 | |
| 95 | /* Time from SUSPEND to SLEEP */ |
| 96 | #define BMC150_MAGN_START_UP_TIME_MS 3 |
| 97 | |
| 98 | #define BMC150_MAGN_AUTO_SUSPEND_DELAY_MS 2000 |
| 99 | |
| 100 | #define BMC150_MAGN_REGVAL_TO_REPXY(regval) (((regval) * 2) + 1) |
| 101 | #define BMC150_MAGN_REGVAL_TO_REPZ(regval) ((regval) + 1) |
| 102 | #define BMC150_MAGN_REPXY_TO_REGVAL(rep) (((rep) - 1) / 2) |
| 103 | #define BMC150_MAGN_REPZ_TO_REGVAL(rep) ((rep) - 1) |
| 104 | |
| 105 | enum bmc150_magn_axis { |
| 106 | AXIS_X, |
| 107 | AXIS_Y, |
| 108 | AXIS_Z, |
| 109 | RHALL, |
| 110 | AXIS_XYZ_MAX = RHALL, |
| 111 | AXIS_XYZR_MAX, |
| 112 | }; |
| 113 | |
| 114 | enum bmc150_magn_power_modes { |
| 115 | BMC150_MAGN_POWER_MODE_SUSPEND, |
| 116 | BMC150_MAGN_POWER_MODE_SLEEP, |
| 117 | BMC150_MAGN_POWER_MODE_NORMAL, |
| 118 | }; |
| 119 | |
| 120 | struct bmc150_magn_trim_regs { |
| 121 | s8 x1; |
| 122 | s8 y1; |
| 123 | __le16 reserved1; |
| 124 | u8 reserved2; |
| 125 | __le16 z4; |
| 126 | s8 x2; |
| 127 | s8 y2; |
| 128 | __le16 reserved3; |
| 129 | __le16 z2; |
| 130 | __le16 z1; |
| 131 | __le16 xyz1; |
| 132 | __le16 z3; |
| 133 | s8 xy2; |
| 134 | u8 xy1; |
| 135 | } __packed; |
| 136 | |
| 137 | struct bmc150_magn_data { |
| 138 | struct i2c_client *client; |
| 139 | /* |
| 140 | * 1. Protect this structure. |
| 141 | * 2. Serialize sequences that power on/off the device and access HW. |
| 142 | */ |
| 143 | struct mutex mutex; |
| 144 | struct regmap *regmap; |
| 145 | /* 4 x 32 bits for x, y z, 4 bytes align, 64 bits timestamp */ |
| 146 | s32 buffer[6]; |
| 147 | struct iio_trigger *dready_trig; |
| 148 | bool dready_trigger_on; |
Irina Tirdea | 5990dc9 | 2015-04-29 21:16:40 +0300 | [diff] [blame] | 149 | int max_odr; |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | static const struct { |
| 153 | int freq; |
| 154 | u8 reg_val; |
| 155 | } bmc150_magn_samp_freq_table[] = { {2, 0x01}, |
| 156 | {6, 0x02}, |
| 157 | {8, 0x03}, |
| 158 | {10, 0x00}, |
| 159 | {15, 0x04}, |
| 160 | {20, 0x05}, |
| 161 | {25, 0x06}, |
| 162 | {30, 0x07} }; |
| 163 | |
| 164 | enum bmc150_magn_presets { |
| 165 | LOW_POWER_PRESET, |
| 166 | REGULAR_PRESET, |
| 167 | ENHANCED_REGULAR_PRESET, |
| 168 | HIGH_ACCURACY_PRESET |
| 169 | }; |
| 170 | |
| 171 | static const struct bmc150_magn_preset { |
| 172 | u8 rep_xy; |
| 173 | u8 rep_z; |
| 174 | u8 odr; |
| 175 | } bmc150_magn_presets_table[] = { |
| 176 | [LOW_POWER_PRESET] = {3, 3, 10}, |
| 177 | [REGULAR_PRESET] = {9, 15, 10}, |
| 178 | [ENHANCED_REGULAR_PRESET] = {15, 27, 10}, |
| 179 | [HIGH_ACCURACY_PRESET] = {47, 83, 20}, |
| 180 | }; |
| 181 | |
| 182 | #define BMC150_MAGN_DEFAULT_PRESET REGULAR_PRESET |
| 183 | |
| 184 | static bool bmc150_magn_is_writeable_reg(struct device *dev, unsigned int reg) |
| 185 | { |
| 186 | switch (reg) { |
| 187 | case BMC150_MAGN_REG_POWER: |
| 188 | case BMC150_MAGN_REG_OPMODE_ODR: |
| 189 | case BMC150_MAGN_REG_INT: |
| 190 | case BMC150_MAGN_REG_INT_DRDY: |
| 191 | case BMC150_MAGN_REG_LOW_THRESH: |
| 192 | case BMC150_MAGN_REG_HIGH_THRESH: |
| 193 | case BMC150_MAGN_REG_REP_XY: |
| 194 | case BMC150_MAGN_REG_REP_Z: |
| 195 | return true; |
| 196 | default: |
| 197 | return false; |
| 198 | }; |
| 199 | } |
| 200 | |
| 201 | static bool bmc150_magn_is_volatile_reg(struct device *dev, unsigned int reg) |
| 202 | { |
| 203 | switch (reg) { |
| 204 | case BMC150_MAGN_REG_X_L: |
| 205 | case BMC150_MAGN_REG_X_M: |
| 206 | case BMC150_MAGN_REG_Y_L: |
| 207 | case BMC150_MAGN_REG_Y_M: |
| 208 | case BMC150_MAGN_REG_Z_L: |
| 209 | case BMC150_MAGN_REG_Z_M: |
| 210 | case BMC150_MAGN_REG_RHALL_L: |
| 211 | case BMC150_MAGN_REG_RHALL_M: |
| 212 | case BMC150_MAGN_REG_INT_STATUS: |
| 213 | return true; |
| 214 | default: |
| 215 | return false; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | static const struct regmap_config bmc150_magn_regmap_config = { |
| 220 | .reg_bits = 8, |
| 221 | .val_bits = 8, |
| 222 | |
| 223 | .max_register = BMC150_MAGN_REG_TRIM_END, |
| 224 | .cache_type = REGCACHE_RBTREE, |
| 225 | |
| 226 | .writeable_reg = bmc150_magn_is_writeable_reg, |
| 227 | .volatile_reg = bmc150_magn_is_volatile_reg, |
| 228 | }; |
| 229 | |
| 230 | static int bmc150_magn_set_power_mode(struct bmc150_magn_data *data, |
| 231 | enum bmc150_magn_power_modes mode, |
| 232 | bool state) |
| 233 | { |
| 234 | int ret; |
| 235 | |
| 236 | switch (mode) { |
| 237 | case BMC150_MAGN_POWER_MODE_SUSPEND: |
| 238 | ret = regmap_update_bits(data->regmap, BMC150_MAGN_REG_POWER, |
| 239 | BMC150_MAGN_MASK_POWER_CTL, !state); |
| 240 | if (ret < 0) |
| 241 | return ret; |
| 242 | usleep_range(BMC150_MAGN_START_UP_TIME_MS * 1000, 20000); |
| 243 | return 0; |
| 244 | case BMC150_MAGN_POWER_MODE_SLEEP: |
| 245 | return regmap_update_bits(data->regmap, |
| 246 | BMC150_MAGN_REG_OPMODE_ODR, |
| 247 | BMC150_MAGN_MASK_OPMODE, |
| 248 | BMC150_MAGN_MODE_SLEEP << |
| 249 | BMC150_MAGN_SHIFT_OPMODE); |
| 250 | case BMC150_MAGN_POWER_MODE_NORMAL: |
| 251 | return regmap_update_bits(data->regmap, |
| 252 | BMC150_MAGN_REG_OPMODE_ODR, |
| 253 | BMC150_MAGN_MASK_OPMODE, |
| 254 | BMC150_MAGN_MODE_NORMAL << |
| 255 | BMC150_MAGN_SHIFT_OPMODE); |
| 256 | } |
| 257 | |
| 258 | return -EINVAL; |
| 259 | } |
| 260 | |
| 261 | static int bmc150_magn_set_power_state(struct bmc150_magn_data *data, bool on) |
| 262 | { |
| 263 | #ifdef CONFIG_PM |
| 264 | int ret; |
| 265 | |
| 266 | if (on) { |
| 267 | ret = pm_runtime_get_sync(&data->client->dev); |
| 268 | } else { |
| 269 | pm_runtime_mark_last_busy(&data->client->dev); |
| 270 | ret = pm_runtime_put_autosuspend(&data->client->dev); |
| 271 | } |
| 272 | |
| 273 | if (ret < 0) { |
| 274 | dev_err(&data->client->dev, |
| 275 | "failed to change power state to %d\n", on); |
| 276 | if (on) |
| 277 | pm_runtime_put_noidle(&data->client->dev); |
| 278 | |
| 279 | return ret; |
| 280 | } |
| 281 | #endif |
| 282 | |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | static int bmc150_magn_get_odr(struct bmc150_magn_data *data, int *val) |
| 287 | { |
| 288 | int ret, reg_val; |
| 289 | u8 i, odr_val; |
| 290 | |
| 291 | ret = regmap_read(data->regmap, BMC150_MAGN_REG_OPMODE_ODR, ®_val); |
| 292 | if (ret < 0) |
| 293 | return ret; |
| 294 | odr_val = (reg_val & BMC150_MAGN_MASK_ODR) >> BMC150_MAGN_SHIFT_ODR; |
| 295 | |
| 296 | for (i = 0; i < ARRAY_SIZE(bmc150_magn_samp_freq_table); i++) |
| 297 | if (bmc150_magn_samp_freq_table[i].reg_val == odr_val) { |
| 298 | *val = bmc150_magn_samp_freq_table[i].freq; |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | return -EINVAL; |
| 303 | } |
| 304 | |
| 305 | static int bmc150_magn_set_odr(struct bmc150_magn_data *data, int val) |
| 306 | { |
| 307 | int ret; |
| 308 | u8 i; |
| 309 | |
| 310 | for (i = 0; i < ARRAY_SIZE(bmc150_magn_samp_freq_table); i++) { |
| 311 | if (bmc150_magn_samp_freq_table[i].freq == val) { |
| 312 | ret = regmap_update_bits(data->regmap, |
| 313 | BMC150_MAGN_REG_OPMODE_ODR, |
| 314 | BMC150_MAGN_MASK_ODR, |
| 315 | bmc150_magn_samp_freq_table[i]. |
| 316 | reg_val << |
| 317 | BMC150_MAGN_SHIFT_ODR); |
| 318 | if (ret < 0) |
| 319 | return ret; |
| 320 | return 0; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | return -EINVAL; |
| 325 | } |
| 326 | |
Irina Tirdea | 5990dc9 | 2015-04-29 21:16:40 +0300 | [diff] [blame] | 327 | static int bmc150_magn_set_max_odr(struct bmc150_magn_data *data, int rep_xy, |
| 328 | int rep_z, int odr) |
| 329 | { |
| 330 | int ret, reg_val, max_odr; |
| 331 | |
| 332 | if (rep_xy <= 0) { |
| 333 | ret = regmap_read(data->regmap, BMC150_MAGN_REG_REP_XY, |
| 334 | ®_val); |
| 335 | if (ret < 0) |
| 336 | return ret; |
| 337 | rep_xy = BMC150_MAGN_REGVAL_TO_REPXY(reg_val); |
| 338 | } |
| 339 | if (rep_z <= 0) { |
| 340 | ret = regmap_read(data->regmap, BMC150_MAGN_REG_REP_Z, |
| 341 | ®_val); |
| 342 | if (ret < 0) |
| 343 | return ret; |
| 344 | rep_z = BMC150_MAGN_REGVAL_TO_REPZ(reg_val); |
| 345 | } |
| 346 | if (odr <= 0) { |
| 347 | ret = bmc150_magn_get_odr(data, &odr); |
| 348 | if (ret < 0) |
| 349 | return ret; |
| 350 | } |
| 351 | /* the maximum selectable read-out frequency from datasheet */ |
| 352 | max_odr = 1000000 / (145 * rep_xy + 500 * rep_z + 980); |
| 353 | if (odr > max_odr) { |
| 354 | dev_err(&data->client->dev, |
| 355 | "Can't set oversampling with sampling freq %d\n", |
| 356 | odr); |
| 357 | return -EINVAL; |
| 358 | } |
| 359 | data->max_odr = max_odr; |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 364 | static s32 bmc150_magn_compensate_x(struct bmc150_magn_trim_regs *tregs, s16 x, |
| 365 | u16 rhall) |
| 366 | { |
| 367 | s16 val; |
| 368 | u16 xyz1 = le16_to_cpu(tregs->xyz1); |
| 369 | |
| 370 | if (x == BMC150_MAGN_XY_OVERFLOW_VAL) |
| 371 | return S32_MIN; |
| 372 | |
| 373 | if (!rhall) |
| 374 | rhall = xyz1; |
| 375 | |
| 376 | val = ((s16)(((u16)((((s32)xyz1) << 14) / rhall)) - ((u16)0x4000))); |
| 377 | val = ((s16)((((s32)x) * ((((((((s32)tregs->xy2) * ((((s32)val) * |
| 378 | ((s32)val)) >> 7)) + (((s32)val) * |
| 379 | ((s32)(((s16)tregs->xy1) << 7)))) >> 9) + ((s32)0x100000)) * |
| 380 | ((s32)(((s16)tregs->x2) + ((s16)0xA0)))) >> 12)) >> 13)) + |
| 381 | (((s16)tregs->x1) << 3); |
| 382 | |
| 383 | return (s32)val; |
| 384 | } |
| 385 | |
| 386 | static s32 bmc150_magn_compensate_y(struct bmc150_magn_trim_regs *tregs, s16 y, |
| 387 | u16 rhall) |
| 388 | { |
| 389 | s16 val; |
| 390 | u16 xyz1 = le16_to_cpu(tregs->xyz1); |
| 391 | |
| 392 | if (y == BMC150_MAGN_XY_OVERFLOW_VAL) |
| 393 | return S32_MIN; |
| 394 | |
| 395 | if (!rhall) |
| 396 | rhall = xyz1; |
| 397 | |
| 398 | val = ((s16)(((u16)((((s32)xyz1) << 14) / rhall)) - ((u16)0x4000))); |
| 399 | val = ((s16)((((s32)y) * ((((((((s32)tregs->xy2) * ((((s32)val) * |
| 400 | ((s32)val)) >> 7)) + (((s32)val) * |
| 401 | ((s32)(((s16)tregs->xy1) << 7)))) >> 9) + ((s32)0x100000)) * |
| 402 | ((s32)(((s16)tregs->y2) + ((s16)0xA0)))) >> 12)) >> 13)) + |
| 403 | (((s16)tregs->y1) << 3); |
| 404 | |
| 405 | return (s32)val; |
| 406 | } |
| 407 | |
| 408 | static s32 bmc150_magn_compensate_z(struct bmc150_magn_trim_regs *tregs, s16 z, |
| 409 | u16 rhall) |
| 410 | { |
| 411 | s32 val; |
| 412 | u16 xyz1 = le16_to_cpu(tregs->xyz1); |
| 413 | u16 z1 = le16_to_cpu(tregs->z1); |
| 414 | s16 z2 = le16_to_cpu(tregs->z2); |
| 415 | s16 z3 = le16_to_cpu(tregs->z3); |
| 416 | s16 z4 = le16_to_cpu(tregs->z4); |
| 417 | |
| 418 | if (z == BMC150_MAGN_Z_OVERFLOW_VAL) |
| 419 | return S32_MIN; |
| 420 | |
| 421 | val = (((((s32)(z - z4)) << 15) - ((((s32)z3) * ((s32)(((s16)rhall) - |
| 422 | ((s16)xyz1)))) >> 2)) / (z2 + ((s16)(((((s32)z1) * |
| 423 | ((((s16)rhall) << 1))) + (1 << 15)) >> 16)))); |
| 424 | |
| 425 | return val; |
| 426 | } |
| 427 | |
| 428 | static int bmc150_magn_read_xyz(struct bmc150_magn_data *data, s32 *buffer) |
| 429 | { |
| 430 | int ret; |
| 431 | __le16 values[AXIS_XYZR_MAX]; |
| 432 | s16 raw_x, raw_y, raw_z; |
| 433 | u16 rhall; |
| 434 | struct bmc150_magn_trim_regs tregs; |
| 435 | |
| 436 | ret = regmap_bulk_read(data->regmap, BMC150_MAGN_REG_X_L, |
| 437 | values, sizeof(values)); |
| 438 | if (ret < 0) |
| 439 | return ret; |
| 440 | |
| 441 | raw_x = (s16)le16_to_cpu(values[AXIS_X]) >> BMC150_MAGN_SHIFT_XY_L; |
| 442 | raw_y = (s16)le16_to_cpu(values[AXIS_Y]) >> BMC150_MAGN_SHIFT_XY_L; |
| 443 | raw_z = (s16)le16_to_cpu(values[AXIS_Z]) >> BMC150_MAGN_SHIFT_Z_L; |
| 444 | rhall = le16_to_cpu(values[RHALL]) >> BMC150_MAGN_SHIFT_RHALL_L; |
| 445 | |
| 446 | ret = regmap_bulk_read(data->regmap, BMC150_MAGN_REG_TRIM_START, |
| 447 | &tregs, sizeof(tregs)); |
| 448 | if (ret < 0) |
| 449 | return ret; |
| 450 | |
| 451 | buffer[AXIS_X] = bmc150_magn_compensate_x(&tregs, raw_x, rhall); |
| 452 | buffer[AXIS_Y] = bmc150_magn_compensate_y(&tregs, raw_y, rhall); |
| 453 | buffer[AXIS_Z] = bmc150_magn_compensate_z(&tregs, raw_z, rhall); |
| 454 | |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | static int bmc150_magn_read_raw(struct iio_dev *indio_dev, |
| 459 | struct iio_chan_spec const *chan, |
| 460 | int *val, int *val2, long mask) |
| 461 | { |
| 462 | struct bmc150_magn_data *data = iio_priv(indio_dev); |
Irina Tirdea | 5990dc9 | 2015-04-29 21:16:40 +0300 | [diff] [blame] | 463 | int ret, tmp; |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 464 | s32 values[AXIS_XYZ_MAX]; |
| 465 | |
| 466 | switch (mask) { |
| 467 | case IIO_CHAN_INFO_RAW: |
| 468 | if (iio_buffer_enabled(indio_dev)) |
| 469 | return -EBUSY; |
| 470 | mutex_lock(&data->mutex); |
| 471 | |
| 472 | ret = bmc150_magn_set_power_state(data, true); |
| 473 | if (ret < 0) { |
| 474 | mutex_unlock(&data->mutex); |
| 475 | return ret; |
| 476 | } |
| 477 | |
| 478 | ret = bmc150_magn_read_xyz(data, values); |
| 479 | if (ret < 0) { |
| 480 | bmc150_magn_set_power_state(data, false); |
| 481 | mutex_unlock(&data->mutex); |
| 482 | return ret; |
| 483 | } |
| 484 | *val = values[chan->scan_index]; |
| 485 | |
| 486 | ret = bmc150_magn_set_power_state(data, false); |
| 487 | if (ret < 0) { |
| 488 | mutex_unlock(&data->mutex); |
| 489 | return ret; |
| 490 | } |
| 491 | |
| 492 | mutex_unlock(&data->mutex); |
| 493 | return IIO_VAL_INT; |
| 494 | case IIO_CHAN_INFO_SCALE: |
| 495 | /* |
| 496 | * The API/driver performs an off-chip temperature |
| 497 | * compensation and outputs x/y/z magnetic field data in |
| 498 | * 16 LSB/uT to the upper application layer. |
| 499 | */ |
| 500 | *val = 0; |
| 501 | *val2 = 625; |
| 502 | return IIO_VAL_INT_PLUS_MICRO; |
| 503 | case IIO_CHAN_INFO_SAMP_FREQ: |
| 504 | ret = bmc150_magn_get_odr(data, val); |
| 505 | if (ret < 0) |
| 506 | return ret; |
| 507 | return IIO_VAL_INT; |
Irina Tirdea | 5990dc9 | 2015-04-29 21:16:40 +0300 | [diff] [blame] | 508 | case IIO_CHAN_INFO_OVERSAMPLING_RATIO: |
| 509 | switch (chan->channel2) { |
| 510 | case IIO_MOD_X: |
| 511 | case IIO_MOD_Y: |
| 512 | ret = regmap_read(data->regmap, BMC150_MAGN_REG_REP_XY, |
| 513 | &tmp); |
| 514 | if (ret < 0) |
| 515 | return ret; |
| 516 | *val = BMC150_MAGN_REGVAL_TO_REPXY(tmp); |
| 517 | return IIO_VAL_INT; |
| 518 | case IIO_MOD_Z: |
| 519 | ret = regmap_read(data->regmap, BMC150_MAGN_REG_REP_Z, |
| 520 | &tmp); |
| 521 | if (ret < 0) |
| 522 | return ret; |
| 523 | *val = BMC150_MAGN_REGVAL_TO_REPZ(tmp); |
| 524 | return IIO_VAL_INT; |
| 525 | default: |
| 526 | return -EINVAL; |
| 527 | } |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 528 | default: |
| 529 | return -EINVAL; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | static int bmc150_magn_write_raw(struct iio_dev *indio_dev, |
| 534 | struct iio_chan_spec const *chan, |
| 535 | int val, int val2, long mask) |
| 536 | { |
| 537 | struct bmc150_magn_data *data = iio_priv(indio_dev); |
| 538 | int ret; |
| 539 | |
| 540 | switch (mask) { |
| 541 | case IIO_CHAN_INFO_SAMP_FREQ: |
Irina Tirdea | 5990dc9 | 2015-04-29 21:16:40 +0300 | [diff] [blame] | 542 | if (val > data->max_odr) |
| 543 | return -EINVAL; |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 544 | mutex_lock(&data->mutex); |
| 545 | ret = bmc150_magn_set_odr(data, val); |
| 546 | mutex_unlock(&data->mutex); |
| 547 | return ret; |
Irina Tirdea | 5990dc9 | 2015-04-29 21:16:40 +0300 | [diff] [blame] | 548 | case IIO_CHAN_INFO_OVERSAMPLING_RATIO: |
| 549 | switch (chan->channel2) { |
| 550 | case IIO_MOD_X: |
| 551 | case IIO_MOD_Y: |
| 552 | if (val < 1 || val > 511) |
| 553 | return -EINVAL; |
| 554 | mutex_lock(&data->mutex); |
| 555 | ret = bmc150_magn_set_max_odr(data, val, 0, 0); |
| 556 | if (ret < 0) { |
| 557 | mutex_unlock(&data->mutex); |
| 558 | return ret; |
| 559 | } |
| 560 | ret = regmap_update_bits(data->regmap, |
| 561 | BMC150_MAGN_REG_REP_XY, |
Hartmut Knaack | 1506f3c | 2015-07-17 10:52:31 +0200 | [diff] [blame] | 562 | BMC150_MAGN_REG_REP_DATAMASK, |
Irina Tirdea | 5990dc9 | 2015-04-29 21:16:40 +0300 | [diff] [blame] | 563 | BMC150_MAGN_REPXY_TO_REGVAL |
| 564 | (val)); |
| 565 | mutex_unlock(&data->mutex); |
| 566 | return ret; |
| 567 | case IIO_MOD_Z: |
| 568 | if (val < 1 || val > 256) |
| 569 | return -EINVAL; |
| 570 | mutex_lock(&data->mutex); |
| 571 | ret = bmc150_magn_set_max_odr(data, 0, val, 0); |
| 572 | if (ret < 0) { |
| 573 | mutex_unlock(&data->mutex); |
| 574 | return ret; |
| 575 | } |
| 576 | ret = regmap_update_bits(data->regmap, |
| 577 | BMC150_MAGN_REG_REP_Z, |
Hartmut Knaack | 1506f3c | 2015-07-17 10:52:31 +0200 | [diff] [blame] | 578 | BMC150_MAGN_REG_REP_DATAMASK, |
Irina Tirdea | 5990dc9 | 2015-04-29 21:16:40 +0300 | [diff] [blame] | 579 | BMC150_MAGN_REPZ_TO_REGVAL |
| 580 | (val)); |
| 581 | mutex_unlock(&data->mutex); |
| 582 | return ret; |
| 583 | default: |
| 584 | return -EINVAL; |
| 585 | } |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 586 | default: |
| 587 | return -EINVAL; |
| 588 | } |
| 589 | } |
| 590 | |
Irina Tirdea | 5990dc9 | 2015-04-29 21:16:40 +0300 | [diff] [blame] | 591 | static ssize_t bmc150_magn_show_samp_freq_avail(struct device *dev, |
| 592 | struct device_attribute *attr, |
| 593 | char *buf) |
| 594 | { |
| 595 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
| 596 | struct bmc150_magn_data *data = iio_priv(indio_dev); |
| 597 | size_t len = 0; |
| 598 | u8 i; |
| 599 | |
| 600 | for (i = 0; i < ARRAY_SIZE(bmc150_magn_samp_freq_table); i++) { |
| 601 | if (bmc150_magn_samp_freq_table[i].freq > data->max_odr) |
| 602 | break; |
| 603 | len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", |
| 604 | bmc150_magn_samp_freq_table[i].freq); |
| 605 | } |
| 606 | /* replace last space with a newline */ |
| 607 | buf[len - 1] = '\n'; |
| 608 | |
| 609 | return len; |
| 610 | } |
| 611 | |
| 612 | static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(bmc150_magn_show_samp_freq_avail); |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 613 | |
| 614 | static struct attribute *bmc150_magn_attributes[] = { |
Irina Tirdea | 5990dc9 | 2015-04-29 21:16:40 +0300 | [diff] [blame] | 615 | &iio_dev_attr_sampling_frequency_available.dev_attr.attr, |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 616 | NULL, |
| 617 | }; |
| 618 | |
| 619 | static const struct attribute_group bmc150_magn_attrs_group = { |
| 620 | .attrs = bmc150_magn_attributes, |
| 621 | }; |
| 622 | |
| 623 | #define BMC150_MAGN_CHANNEL(_axis) { \ |
| 624 | .type = IIO_MAGN, \ |
| 625 | .modified = 1, \ |
| 626 | .channel2 = IIO_MOD_##_axis, \ |
Irina Tirdea | 5990dc9 | 2015-04-29 21:16:40 +0300 | [diff] [blame] | 627 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
| 628 | BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \ |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 629 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \ |
| 630 | BIT(IIO_CHAN_INFO_SCALE), \ |
| 631 | .scan_index = AXIS_##_axis, \ |
| 632 | .scan_type = { \ |
| 633 | .sign = 's', \ |
| 634 | .realbits = 32, \ |
| 635 | .storagebits = 32, \ |
| 636 | .endianness = IIO_LE \ |
| 637 | }, \ |
| 638 | } |
| 639 | |
| 640 | static const struct iio_chan_spec bmc150_magn_channels[] = { |
| 641 | BMC150_MAGN_CHANNEL(X), |
| 642 | BMC150_MAGN_CHANNEL(Y), |
| 643 | BMC150_MAGN_CHANNEL(Z), |
| 644 | IIO_CHAN_SOFT_TIMESTAMP(3), |
| 645 | }; |
| 646 | |
| 647 | static const struct iio_info bmc150_magn_info = { |
| 648 | .attrs = &bmc150_magn_attrs_group, |
| 649 | .read_raw = bmc150_magn_read_raw, |
| 650 | .write_raw = bmc150_magn_write_raw, |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 651 | .driver_module = THIS_MODULE, |
| 652 | }; |
| 653 | |
Hartmut Knaack | 47764c7 | 2015-07-17 10:52:27 +0200 | [diff] [blame] | 654 | static const unsigned long bmc150_magn_scan_masks[] = { |
| 655 | BIT(AXIS_X) | BIT(AXIS_Y) | BIT(AXIS_Z), |
| 656 | 0}; |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 657 | |
| 658 | static irqreturn_t bmc150_magn_trigger_handler(int irq, void *p) |
| 659 | { |
| 660 | struct iio_poll_func *pf = p; |
| 661 | struct iio_dev *indio_dev = pf->indio_dev; |
| 662 | struct bmc150_magn_data *data = iio_priv(indio_dev); |
| 663 | int ret; |
| 664 | |
| 665 | mutex_lock(&data->mutex); |
| 666 | ret = bmc150_magn_read_xyz(data, data->buffer); |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 667 | if (ret < 0) |
| 668 | goto err; |
| 669 | |
| 670 | iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, |
| 671 | pf->timestamp); |
| 672 | |
| 673 | err: |
Hartmut Knaack | 3021678 | 2015-07-17 10:52:30 +0200 | [diff] [blame] | 674 | mutex_unlock(&data->mutex); |
Vlad Dogaru | 9d174b4 | 2015-06-12 11:31:38 +0300 | [diff] [blame] | 675 | iio_trigger_notify_done(indio_dev->trig); |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 676 | |
| 677 | return IRQ_HANDLED; |
| 678 | } |
| 679 | |
| 680 | static int bmc150_magn_init(struct bmc150_magn_data *data) |
| 681 | { |
| 682 | int ret, chip_id; |
| 683 | struct bmc150_magn_preset preset; |
| 684 | |
| 685 | ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, |
| 686 | false); |
| 687 | if (ret < 0) { |
| 688 | dev_err(&data->client->dev, |
| 689 | "Failed to bring up device from suspend mode\n"); |
| 690 | return ret; |
| 691 | } |
| 692 | |
| 693 | ret = regmap_read(data->regmap, BMC150_MAGN_REG_CHIP_ID, &chip_id); |
| 694 | if (ret < 0) { |
| 695 | dev_err(&data->client->dev, "Failed reading chip id\n"); |
| 696 | goto err_poweroff; |
| 697 | } |
| 698 | if (chip_id != BMC150_MAGN_CHIP_ID_VAL) { |
Hartmut Knaack | 6a14925 | 2015-07-17 10:52:26 +0200 | [diff] [blame] | 699 | dev_err(&data->client->dev, "Invalid chip id 0x%x\n", chip_id); |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 700 | ret = -ENODEV; |
| 701 | goto err_poweroff; |
| 702 | } |
Hartmut Knaack | 6a14925 | 2015-07-17 10:52:26 +0200 | [diff] [blame] | 703 | dev_dbg(&data->client->dev, "Chip id %x\n", chip_id); |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 704 | |
| 705 | preset = bmc150_magn_presets_table[BMC150_MAGN_DEFAULT_PRESET]; |
| 706 | ret = bmc150_magn_set_odr(data, preset.odr); |
| 707 | if (ret < 0) { |
| 708 | dev_err(&data->client->dev, "Failed to set ODR to %d\n", |
| 709 | preset.odr); |
| 710 | goto err_poweroff; |
| 711 | } |
| 712 | |
| 713 | ret = regmap_write(data->regmap, BMC150_MAGN_REG_REP_XY, |
| 714 | BMC150_MAGN_REPXY_TO_REGVAL(preset.rep_xy)); |
| 715 | if (ret < 0) { |
| 716 | dev_err(&data->client->dev, "Failed to set REP XY to %d\n", |
| 717 | preset.rep_xy); |
| 718 | goto err_poweroff; |
| 719 | } |
| 720 | |
| 721 | ret = regmap_write(data->regmap, BMC150_MAGN_REG_REP_Z, |
| 722 | BMC150_MAGN_REPZ_TO_REGVAL(preset.rep_z)); |
| 723 | if (ret < 0) { |
| 724 | dev_err(&data->client->dev, "Failed to set REP Z to %d\n", |
| 725 | preset.rep_z); |
| 726 | goto err_poweroff; |
| 727 | } |
| 728 | |
Irina Tirdea | 5990dc9 | 2015-04-29 21:16:40 +0300 | [diff] [blame] | 729 | ret = bmc150_magn_set_max_odr(data, preset.rep_xy, preset.rep_z, |
| 730 | preset.odr); |
| 731 | if (ret < 0) |
| 732 | goto err_poweroff; |
| 733 | |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 734 | ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_NORMAL, |
| 735 | true); |
| 736 | if (ret < 0) { |
| 737 | dev_err(&data->client->dev, "Failed to power on device\n"); |
| 738 | goto err_poweroff; |
| 739 | } |
| 740 | |
| 741 | return 0; |
| 742 | |
| 743 | err_poweroff: |
| 744 | bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, true); |
| 745 | return ret; |
| 746 | } |
| 747 | |
| 748 | static int bmc150_magn_reset_intr(struct bmc150_magn_data *data) |
| 749 | { |
| 750 | int tmp; |
| 751 | |
| 752 | /* |
| 753 | * Data Ready (DRDY) is always cleared after |
| 754 | * readout of data registers ends. |
| 755 | */ |
| 756 | return regmap_read(data->regmap, BMC150_MAGN_REG_X_L, &tmp); |
| 757 | } |
| 758 | |
| 759 | static int bmc150_magn_trig_try_reen(struct iio_trigger *trig) |
| 760 | { |
| 761 | struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); |
| 762 | struct bmc150_magn_data *data = iio_priv(indio_dev); |
| 763 | int ret; |
| 764 | |
| 765 | if (!data->dready_trigger_on) |
| 766 | return 0; |
| 767 | |
| 768 | mutex_lock(&data->mutex); |
| 769 | ret = bmc150_magn_reset_intr(data); |
| 770 | mutex_unlock(&data->mutex); |
| 771 | |
| 772 | return ret; |
| 773 | } |
| 774 | |
| 775 | static int bmc150_magn_data_rdy_trigger_set_state(struct iio_trigger *trig, |
| 776 | bool state) |
| 777 | { |
| 778 | struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); |
| 779 | struct bmc150_magn_data *data = iio_priv(indio_dev); |
| 780 | int ret = 0; |
| 781 | |
| 782 | mutex_lock(&data->mutex); |
| 783 | if (state == data->dready_trigger_on) |
| 784 | goto err_unlock; |
| 785 | |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 786 | ret = regmap_update_bits(data->regmap, BMC150_MAGN_REG_INT_DRDY, |
| 787 | BMC150_MAGN_MASK_DRDY_EN, |
| 788 | state << BMC150_MAGN_SHIFT_DRDY_EN); |
| 789 | if (ret < 0) |
Irina Tirdea | 5ab744d | 2015-07-23 20:15:02 +0300 | [diff] [blame] | 790 | goto err_unlock; |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 791 | |
| 792 | data->dready_trigger_on = state; |
| 793 | |
| 794 | if (state) { |
| 795 | ret = bmc150_magn_reset_intr(data); |
| 796 | if (ret < 0) |
Irina Tirdea | 5ab744d | 2015-07-23 20:15:02 +0300 | [diff] [blame] | 797 | goto err_unlock; |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 798 | } |
| 799 | mutex_unlock(&data->mutex); |
| 800 | |
| 801 | return 0; |
| 802 | |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 803 | err_unlock: |
| 804 | mutex_unlock(&data->mutex); |
| 805 | return ret; |
| 806 | } |
| 807 | |
| 808 | static const struct iio_trigger_ops bmc150_magn_trigger_ops = { |
| 809 | .set_trigger_state = bmc150_magn_data_rdy_trigger_set_state, |
| 810 | .try_reenable = bmc150_magn_trig_try_reen, |
| 811 | .owner = THIS_MODULE, |
| 812 | }; |
| 813 | |
Vlad Dogaru | 9d174b4 | 2015-06-12 11:31:38 +0300 | [diff] [blame] | 814 | static int bmc150_magn_buffer_preenable(struct iio_dev *indio_dev) |
| 815 | { |
| 816 | struct bmc150_magn_data *data = iio_priv(indio_dev); |
| 817 | |
| 818 | return bmc150_magn_set_power_state(data, true); |
| 819 | } |
| 820 | |
| 821 | static int bmc150_magn_buffer_postdisable(struct iio_dev *indio_dev) |
| 822 | { |
| 823 | struct bmc150_magn_data *data = iio_priv(indio_dev); |
| 824 | |
| 825 | return bmc150_magn_set_power_state(data, false); |
| 826 | } |
| 827 | |
| 828 | static const struct iio_buffer_setup_ops bmc150_magn_buffer_setup_ops = { |
| 829 | .preenable = bmc150_magn_buffer_preenable, |
| 830 | .postenable = iio_triggered_buffer_postenable, |
| 831 | .predisable = iio_triggered_buffer_predisable, |
| 832 | .postdisable = bmc150_magn_buffer_postdisable, |
| 833 | }; |
| 834 | |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 835 | static const char *bmc150_magn_match_acpi_device(struct device *dev) |
| 836 | { |
| 837 | const struct acpi_device_id *id; |
| 838 | |
| 839 | id = acpi_match_device(dev->driver->acpi_match_table, dev); |
| 840 | if (!id) |
| 841 | return NULL; |
| 842 | |
| 843 | return dev_name(dev); |
| 844 | } |
| 845 | |
| 846 | static int bmc150_magn_probe(struct i2c_client *client, |
| 847 | const struct i2c_device_id *id) |
| 848 | { |
| 849 | struct bmc150_magn_data *data; |
| 850 | struct iio_dev *indio_dev; |
| 851 | const char *name = NULL; |
| 852 | int ret; |
| 853 | |
| 854 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 855 | if (!indio_dev) |
| 856 | return -ENOMEM; |
| 857 | |
| 858 | data = iio_priv(indio_dev); |
| 859 | i2c_set_clientdata(client, indio_dev); |
| 860 | data->client = client; |
| 861 | |
| 862 | if (id) |
| 863 | name = id->name; |
| 864 | else if (ACPI_HANDLE(&client->dev)) |
| 865 | name = bmc150_magn_match_acpi_device(&client->dev); |
| 866 | else |
| 867 | return -ENOSYS; |
| 868 | |
| 869 | mutex_init(&data->mutex); |
| 870 | data->regmap = devm_regmap_init_i2c(client, &bmc150_magn_regmap_config); |
| 871 | if (IS_ERR(data->regmap)) { |
| 872 | dev_err(&client->dev, "Failed to allocate register map\n"); |
| 873 | return PTR_ERR(data->regmap); |
| 874 | } |
| 875 | |
| 876 | ret = bmc150_magn_init(data); |
| 877 | if (ret < 0) |
| 878 | return ret; |
| 879 | |
| 880 | indio_dev->dev.parent = &client->dev; |
| 881 | indio_dev->channels = bmc150_magn_channels; |
| 882 | indio_dev->num_channels = ARRAY_SIZE(bmc150_magn_channels); |
| 883 | indio_dev->available_scan_masks = bmc150_magn_scan_masks; |
| 884 | indio_dev->name = name; |
| 885 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 886 | indio_dev->info = &bmc150_magn_info; |
| 887 | |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 888 | if (client->irq > 0) { |
| 889 | data->dready_trig = devm_iio_trigger_alloc(&client->dev, |
| 890 | "%s-dev%d", |
| 891 | indio_dev->name, |
| 892 | indio_dev->id); |
| 893 | if (!data->dready_trig) { |
| 894 | ret = -ENOMEM; |
| 895 | dev_err(&client->dev, "iio trigger alloc failed\n"); |
| 896 | goto err_poweroff; |
| 897 | } |
| 898 | |
| 899 | data->dready_trig->dev.parent = &client->dev; |
| 900 | data->dready_trig->ops = &bmc150_magn_trigger_ops; |
| 901 | iio_trigger_set_drvdata(data->dready_trig, indio_dev); |
| 902 | ret = iio_trigger_register(data->dready_trig); |
| 903 | if (ret) { |
| 904 | dev_err(&client->dev, "iio trigger register failed\n"); |
| 905 | goto err_poweroff; |
| 906 | } |
| 907 | |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 908 | ret = request_threaded_irq(client->irq, |
| 909 | iio_trigger_generic_data_rdy_poll, |
| 910 | NULL, |
| 911 | IRQF_TRIGGER_RISING | IRQF_ONESHOT, |
| 912 | BMC150_MAGN_IRQ_NAME, |
| 913 | data->dready_trig); |
| 914 | if (ret < 0) { |
| 915 | dev_err(&client->dev, "request irq %d failed\n", |
| 916 | client->irq); |
Vlad Dogaru | 9d174b4 | 2015-06-12 11:31:38 +0300 | [diff] [blame] | 917 | goto err_trigger_unregister; |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 918 | } |
| 919 | } |
| 920 | |
Vlad Dogaru | 9d174b4 | 2015-06-12 11:31:38 +0300 | [diff] [blame] | 921 | ret = iio_triggered_buffer_setup(indio_dev, |
| 922 | iio_pollfunc_store_time, |
| 923 | bmc150_magn_trigger_handler, |
| 924 | &bmc150_magn_buffer_setup_ops); |
| 925 | if (ret < 0) { |
| 926 | dev_err(&client->dev, |
| 927 | "iio triggered buffer setup failed\n"); |
| 928 | goto err_free_irq; |
| 929 | } |
| 930 | |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 931 | ret = pm_runtime_set_active(&client->dev); |
| 932 | if (ret) |
Adriana Reus | 7d0ead5 | 2015-11-05 16:25:29 +0200 | [diff] [blame] | 933 | goto err_buffer_cleanup; |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 934 | |
| 935 | pm_runtime_enable(&client->dev); |
| 936 | pm_runtime_set_autosuspend_delay(&client->dev, |
| 937 | BMC150_MAGN_AUTO_SUSPEND_DELAY_MS); |
| 938 | pm_runtime_use_autosuspend(&client->dev); |
| 939 | |
Adriana Reus | 7d0ead5 | 2015-11-05 16:25:29 +0200 | [diff] [blame] | 940 | ret = iio_device_register(indio_dev); |
| 941 | if (ret < 0) { |
| 942 | dev_err(&client->dev, "unable to register iio device\n"); |
| 943 | goto err_buffer_cleanup; |
| 944 | } |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 945 | |
Adriana Reus | 7d0ead5 | 2015-11-05 16:25:29 +0200 | [diff] [blame] | 946 | dev_dbg(&indio_dev->dev, "Registered device %s\n", name); |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 947 | return 0; |
| 948 | |
Vlad Dogaru | 9d174b4 | 2015-06-12 11:31:38 +0300 | [diff] [blame] | 949 | err_buffer_cleanup: |
| 950 | iio_triggered_buffer_cleanup(indio_dev); |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 951 | err_free_irq: |
| 952 | if (client->irq > 0) |
| 953 | free_irq(client->irq, data->dready_trig); |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 954 | err_trigger_unregister: |
| 955 | if (data->dready_trig) |
| 956 | iio_trigger_unregister(data->dready_trig); |
| 957 | err_poweroff: |
| 958 | bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, true); |
| 959 | return ret; |
| 960 | } |
| 961 | |
| 962 | static int bmc150_magn_remove(struct i2c_client *client) |
| 963 | { |
| 964 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 965 | struct bmc150_magn_data *data = iio_priv(indio_dev); |
| 966 | |
Adriana Reus | 7d0ead5 | 2015-11-05 16:25:29 +0200 | [diff] [blame] | 967 | iio_device_unregister(indio_dev); |
| 968 | |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 969 | pm_runtime_disable(&client->dev); |
| 970 | pm_runtime_set_suspended(&client->dev); |
| 971 | pm_runtime_put_noidle(&client->dev); |
| 972 | |
Vlad Dogaru | 9d174b4 | 2015-06-12 11:31:38 +0300 | [diff] [blame] | 973 | iio_triggered_buffer_cleanup(indio_dev); |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 974 | |
| 975 | if (client->irq > 0) |
| 976 | free_irq(data->client->irq, data->dready_trig); |
| 977 | |
Vlad Dogaru | 9d174b4 | 2015-06-12 11:31:38 +0300 | [diff] [blame] | 978 | if (data->dready_trig) |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 979 | iio_trigger_unregister(data->dready_trig); |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 980 | |
| 981 | mutex_lock(&data->mutex); |
| 982 | bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, true); |
| 983 | mutex_unlock(&data->mutex); |
| 984 | |
| 985 | return 0; |
| 986 | } |
| 987 | |
| 988 | #ifdef CONFIG_PM |
| 989 | static int bmc150_magn_runtime_suspend(struct device *dev) |
| 990 | { |
| 991 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 992 | struct bmc150_magn_data *data = iio_priv(indio_dev); |
| 993 | int ret; |
| 994 | |
| 995 | mutex_lock(&data->mutex); |
| 996 | ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SLEEP, |
| 997 | true); |
| 998 | mutex_unlock(&data->mutex); |
| 999 | if (ret < 0) { |
| 1000 | dev_err(&data->client->dev, "powering off device failed\n"); |
| 1001 | return ret; |
| 1002 | } |
| 1003 | return 0; |
| 1004 | } |
| 1005 | |
Irina Tirdea | 019cc46 | 2015-07-23 20:15:01 +0300 | [diff] [blame] | 1006 | /* |
| 1007 | * Should be called with data->mutex held. |
| 1008 | */ |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 1009 | static int bmc150_magn_runtime_resume(struct device *dev) |
| 1010 | { |
| 1011 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 1012 | struct bmc150_magn_data *data = iio_priv(indio_dev); |
| 1013 | |
| 1014 | return bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_NORMAL, |
| 1015 | true); |
| 1016 | } |
| 1017 | #endif |
| 1018 | |
| 1019 | #ifdef CONFIG_PM_SLEEP |
| 1020 | static int bmc150_magn_suspend(struct device *dev) |
| 1021 | { |
| 1022 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 1023 | struct bmc150_magn_data *data = iio_priv(indio_dev); |
| 1024 | int ret; |
| 1025 | |
| 1026 | mutex_lock(&data->mutex); |
| 1027 | ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SLEEP, |
| 1028 | true); |
| 1029 | mutex_unlock(&data->mutex); |
| 1030 | |
| 1031 | return ret; |
| 1032 | } |
| 1033 | |
| 1034 | static int bmc150_magn_resume(struct device *dev) |
| 1035 | { |
| 1036 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 1037 | struct bmc150_magn_data *data = iio_priv(indio_dev); |
| 1038 | int ret; |
| 1039 | |
| 1040 | mutex_lock(&data->mutex); |
| 1041 | ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_NORMAL, |
| 1042 | true); |
| 1043 | mutex_unlock(&data->mutex); |
| 1044 | |
| 1045 | return ret; |
| 1046 | } |
| 1047 | #endif |
| 1048 | |
| 1049 | static const struct dev_pm_ops bmc150_magn_pm_ops = { |
| 1050 | SET_SYSTEM_SLEEP_PM_OPS(bmc150_magn_suspend, bmc150_magn_resume) |
| 1051 | SET_RUNTIME_PM_OPS(bmc150_magn_runtime_suspend, |
| 1052 | bmc150_magn_runtime_resume, NULL) |
| 1053 | }; |
| 1054 | |
| 1055 | static const struct acpi_device_id bmc150_magn_acpi_match[] = { |
| 1056 | {"BMC150B", 0}, |
Vlad Dogaru | da8ef4e | 2015-06-12 11:31:39 +0300 | [diff] [blame] | 1057 | {"BMC156B", 0}, |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 1058 | {}, |
| 1059 | }; |
| 1060 | MODULE_DEVICE_TABLE(acpi, bmc150_magn_acpi_match); |
| 1061 | |
| 1062 | static const struct i2c_device_id bmc150_magn_id[] = { |
| 1063 | {"bmc150_magn", 0}, |
Vlad Dogaru | da8ef4e | 2015-06-12 11:31:39 +0300 | [diff] [blame] | 1064 | {"bmc156_magn", 0}, |
Irina Tirdea | c91746a | 2015-04-29 21:16:37 +0300 | [diff] [blame] | 1065 | {}, |
| 1066 | }; |
| 1067 | MODULE_DEVICE_TABLE(i2c, bmc150_magn_id); |
| 1068 | |
| 1069 | static struct i2c_driver bmc150_magn_driver = { |
| 1070 | .driver = { |
| 1071 | .name = BMC150_MAGN_DRV_NAME, |
| 1072 | .acpi_match_table = ACPI_PTR(bmc150_magn_acpi_match), |
| 1073 | .pm = &bmc150_magn_pm_ops, |
| 1074 | }, |
| 1075 | .probe = bmc150_magn_probe, |
| 1076 | .remove = bmc150_magn_remove, |
| 1077 | .id_table = bmc150_magn_id, |
| 1078 | }; |
| 1079 | module_i2c_driver(bmc150_magn_driver); |
| 1080 | |
| 1081 | MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>"); |
| 1082 | MODULE_LICENSE("GPL v2"); |
| 1083 | MODULE_DESCRIPTION("BMC150 magnetometer driver"); |