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" 40 #define BMC150_MAGN_GPIO_INT "interrupt" 41 42 #define BMC150_MAGN_REG_CHIP_ID 0x40 43 #define BMC150_MAGN_CHIP_ID_VAL 0x32 44 45 #define BMC150_MAGN_REG_X_L 0x42 46 #define BMC150_MAGN_REG_X_M 0x43 47 #define BMC150_MAGN_REG_Y_L 0x44 48 #define BMC150_MAGN_REG_Y_M 0x45 49 #define BMC150_MAGN_SHIFT_XY_L 3 50 #define BMC150_MAGN_REG_Z_L 0x46 51 #define BMC150_MAGN_REG_Z_M 0x47 52 #define BMC150_MAGN_SHIFT_Z_L 1 53 #define BMC150_MAGN_REG_RHALL_L 0x48 54 #define BMC150_MAGN_REG_RHALL_M 0x49 55 #define BMC150_MAGN_SHIFT_RHALL_L 2 56 57 #define BMC150_MAGN_REG_INT_STATUS 0x4A 58 59 #define BMC150_MAGN_REG_POWER 0x4B 60 #define BMC150_MAGN_MASK_POWER_CTL BIT(0) 61 62 #define BMC150_MAGN_REG_OPMODE_ODR 0x4C 63 #define BMC150_MAGN_MASK_OPMODE GENMASK(2, 1) 64 #define BMC150_MAGN_SHIFT_OPMODE 1 65 #define BMC150_MAGN_MODE_NORMAL 0x00 66 #define BMC150_MAGN_MODE_FORCED 0x01 67 #define BMC150_MAGN_MODE_SLEEP 0x03 68 #define BMC150_MAGN_MASK_ODR GENMASK(5, 3) 69 #define BMC150_MAGN_SHIFT_ODR 3 70 71 #define BMC150_MAGN_REG_INT 0x4D 72 73 #define BMC150_MAGN_REG_INT_DRDY 0x4E 74 #define BMC150_MAGN_MASK_DRDY_EN BIT(7) 75 #define BMC150_MAGN_SHIFT_DRDY_EN 7 76 #define BMC150_MAGN_MASK_DRDY_INT3 BIT(6) 77 #define BMC150_MAGN_MASK_DRDY_Z_EN BIT(5) 78 #define BMC150_MAGN_MASK_DRDY_Y_EN BIT(4) 79 #define BMC150_MAGN_MASK_DRDY_X_EN BIT(3) 80 #define BMC150_MAGN_MASK_DRDY_DR_POLARITY BIT(2) 81 #define BMC150_MAGN_MASK_DRDY_LATCHING BIT(1) 82 #define BMC150_MAGN_MASK_DRDY_INT3_POLARITY BIT(0) 83 84 #define BMC150_MAGN_REG_LOW_THRESH 0x4F 85 #define BMC150_MAGN_REG_HIGH_THRESH 0x50 86 #define BMC150_MAGN_REG_REP_XY 0x51 87 #define BMC150_MAGN_REG_REP_Z 0x52 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; 149 int max_odr; 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 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 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); 463 int ret, tmp; 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; 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 } 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: 542 if (val > data->max_odr) 543 return -EINVAL; 544 mutex_lock(&data->mutex); 545 ret = bmc150_magn_set_odr(data, val); 546 mutex_unlock(&data->mutex); 547 return ret; 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, 562 0xFF, 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, 578 0xFF, 579 BMC150_MAGN_REPZ_TO_REGVAL 580 (val)); 581 mutex_unlock(&data->mutex); 582 return ret; 583 default: 584 return -EINVAL; 585 } 586 default: 587 return -EINVAL; 588 } 589 } 590 591 static int bmc150_magn_validate_trigger(struct iio_dev *indio_dev, 592 struct iio_trigger *trig) 593 { 594 struct bmc150_magn_data *data = iio_priv(indio_dev); 595 596 if (data->dready_trig != trig) 597 return -EINVAL; 598 599 return 0; 600 } 601 602 static ssize_t bmc150_magn_show_samp_freq_avail(struct device *dev, 603 struct device_attribute *attr, 604 char *buf) 605 { 606 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 607 struct bmc150_magn_data *data = iio_priv(indio_dev); 608 size_t len = 0; 609 u8 i; 610 611 for (i = 0; i < ARRAY_SIZE(bmc150_magn_samp_freq_table); i++) { 612 if (bmc150_magn_samp_freq_table[i].freq > data->max_odr) 613 break; 614 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", 615 bmc150_magn_samp_freq_table[i].freq); 616 } 617 /* replace last space with a newline */ 618 buf[len - 1] = '\n'; 619 620 return len; 621 } 622 623 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(bmc150_magn_show_samp_freq_avail); 624 625 static struct attribute *bmc150_magn_attributes[] = { 626 &iio_dev_attr_sampling_frequency_available.dev_attr.attr, 627 NULL, 628 }; 629 630 static const struct attribute_group bmc150_magn_attrs_group = { 631 .attrs = bmc150_magn_attributes, 632 }; 633 634 #define BMC150_MAGN_CHANNEL(_axis) { \ 635 .type = IIO_MAGN, \ 636 .modified = 1, \ 637 .channel2 = IIO_MOD_##_axis, \ 638 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 639 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \ 640 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \ 641 BIT(IIO_CHAN_INFO_SCALE), \ 642 .scan_index = AXIS_##_axis, \ 643 .scan_type = { \ 644 .sign = 's', \ 645 .realbits = 32, \ 646 .storagebits = 32, \ 647 .endianness = IIO_LE \ 648 }, \ 649 } 650 651 static const struct iio_chan_spec bmc150_magn_channels[] = { 652 BMC150_MAGN_CHANNEL(X), 653 BMC150_MAGN_CHANNEL(Y), 654 BMC150_MAGN_CHANNEL(Z), 655 IIO_CHAN_SOFT_TIMESTAMP(3), 656 }; 657 658 static const struct iio_info bmc150_magn_info = { 659 .attrs = &bmc150_magn_attrs_group, 660 .read_raw = bmc150_magn_read_raw, 661 .write_raw = bmc150_magn_write_raw, 662 .validate_trigger = bmc150_magn_validate_trigger, 663 .driver_module = THIS_MODULE, 664 }; 665 666 static const unsigned long bmc150_magn_scan_masks[] = {0x07, 0}; 667 668 static irqreturn_t bmc150_magn_trigger_handler(int irq, void *p) 669 { 670 struct iio_poll_func *pf = p; 671 struct iio_dev *indio_dev = pf->indio_dev; 672 struct bmc150_magn_data *data = iio_priv(indio_dev); 673 int ret; 674 675 mutex_lock(&data->mutex); 676 ret = bmc150_magn_read_xyz(data, data->buffer); 677 mutex_unlock(&data->mutex); 678 if (ret < 0) 679 goto err; 680 681 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, 682 pf->timestamp); 683 684 err: 685 iio_trigger_notify_done(data->dready_trig); 686 687 return IRQ_HANDLED; 688 } 689 690 static int bmc150_magn_init(struct bmc150_magn_data *data) 691 { 692 int ret, chip_id; 693 struct bmc150_magn_preset preset; 694 695 ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, 696 false); 697 if (ret < 0) { 698 dev_err(&data->client->dev, 699 "Failed to bring up device from suspend mode\n"); 700 return ret; 701 } 702 703 ret = regmap_read(data->regmap, BMC150_MAGN_REG_CHIP_ID, &chip_id); 704 if (ret < 0) { 705 dev_err(&data->client->dev, "Failed reading chip id\n"); 706 goto err_poweroff; 707 } 708 if (chip_id != BMC150_MAGN_CHIP_ID_VAL) { 709 dev_err(&data->client->dev, "Invalid chip id 0x%x\n", chip_id); 710 ret = -ENODEV; 711 goto err_poweroff; 712 } 713 dev_dbg(&data->client->dev, "Chip id %x\n", chip_id); 714 715 preset = bmc150_magn_presets_table[BMC150_MAGN_DEFAULT_PRESET]; 716 ret = bmc150_magn_set_odr(data, preset.odr); 717 if (ret < 0) { 718 dev_err(&data->client->dev, "Failed to set ODR to %d\n", 719 preset.odr); 720 goto err_poweroff; 721 } 722 723 ret = regmap_write(data->regmap, BMC150_MAGN_REG_REP_XY, 724 BMC150_MAGN_REPXY_TO_REGVAL(preset.rep_xy)); 725 if (ret < 0) { 726 dev_err(&data->client->dev, "Failed to set REP XY to %d\n", 727 preset.rep_xy); 728 goto err_poweroff; 729 } 730 731 ret = regmap_write(data->regmap, BMC150_MAGN_REG_REP_Z, 732 BMC150_MAGN_REPZ_TO_REGVAL(preset.rep_z)); 733 if (ret < 0) { 734 dev_err(&data->client->dev, "Failed to set REP Z to %d\n", 735 preset.rep_z); 736 goto err_poweroff; 737 } 738 739 ret = bmc150_magn_set_max_odr(data, preset.rep_xy, preset.rep_z, 740 preset.odr); 741 if (ret < 0) 742 goto err_poweroff; 743 744 ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_NORMAL, 745 true); 746 if (ret < 0) { 747 dev_err(&data->client->dev, "Failed to power on device\n"); 748 goto err_poweroff; 749 } 750 751 return 0; 752 753 err_poweroff: 754 bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, true); 755 return ret; 756 } 757 758 static int bmc150_magn_reset_intr(struct bmc150_magn_data *data) 759 { 760 int tmp; 761 762 /* 763 * Data Ready (DRDY) is always cleared after 764 * readout of data registers ends. 765 */ 766 return regmap_read(data->regmap, BMC150_MAGN_REG_X_L, &tmp); 767 } 768 769 static int bmc150_magn_trig_try_reen(struct iio_trigger *trig) 770 { 771 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 772 struct bmc150_magn_data *data = iio_priv(indio_dev); 773 int ret; 774 775 if (!data->dready_trigger_on) 776 return 0; 777 778 mutex_lock(&data->mutex); 779 ret = bmc150_magn_reset_intr(data); 780 mutex_unlock(&data->mutex); 781 782 return ret; 783 } 784 785 static int bmc150_magn_data_rdy_trigger_set_state(struct iio_trigger *trig, 786 bool state) 787 { 788 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 789 struct bmc150_magn_data *data = iio_priv(indio_dev); 790 int ret = 0; 791 792 mutex_lock(&data->mutex); 793 if (state == data->dready_trigger_on) 794 goto err_unlock; 795 796 ret = bmc150_magn_set_power_state(data, state); 797 if (ret < 0) 798 goto err_unlock; 799 800 ret = regmap_update_bits(data->regmap, BMC150_MAGN_REG_INT_DRDY, 801 BMC150_MAGN_MASK_DRDY_EN, 802 state << BMC150_MAGN_SHIFT_DRDY_EN); 803 if (ret < 0) 804 goto err_poweroff; 805 806 data->dready_trigger_on = state; 807 808 if (state) { 809 ret = bmc150_magn_reset_intr(data); 810 if (ret < 0) 811 goto err_poweroff; 812 } 813 mutex_unlock(&data->mutex); 814 815 return 0; 816 817 err_poweroff: 818 bmc150_magn_set_power_state(data, false); 819 err_unlock: 820 mutex_unlock(&data->mutex); 821 return ret; 822 } 823 824 static const struct iio_trigger_ops bmc150_magn_trigger_ops = { 825 .set_trigger_state = bmc150_magn_data_rdy_trigger_set_state, 826 .try_reenable = bmc150_magn_trig_try_reen, 827 .owner = THIS_MODULE, 828 }; 829 830 static int bmc150_magn_gpio_probe(struct i2c_client *client) 831 { 832 struct device *dev; 833 struct gpio_desc *gpio; 834 int ret; 835 836 if (!client) 837 return -EINVAL; 838 839 dev = &client->dev; 840 841 /* data ready GPIO interrupt pin */ 842 gpio = devm_gpiod_get_index(dev, BMC150_MAGN_GPIO_INT, 0); 843 if (IS_ERR(gpio)) { 844 dev_err(dev, "ACPI GPIO get index failed\n"); 845 return PTR_ERR(gpio); 846 } 847 848 ret = gpiod_direction_input(gpio); 849 if (ret) 850 return ret; 851 852 ret = gpiod_to_irq(gpio); 853 854 dev_dbg(dev, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret); 855 856 return ret; 857 } 858 859 static const char *bmc150_magn_match_acpi_device(struct device *dev) 860 { 861 const struct acpi_device_id *id; 862 863 id = acpi_match_device(dev->driver->acpi_match_table, dev); 864 if (!id) 865 return NULL; 866 867 return dev_name(dev); 868 } 869 870 static int bmc150_magn_probe(struct i2c_client *client, 871 const struct i2c_device_id *id) 872 { 873 struct bmc150_magn_data *data; 874 struct iio_dev *indio_dev; 875 const char *name = NULL; 876 int ret; 877 878 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 879 if (!indio_dev) 880 return -ENOMEM; 881 882 data = iio_priv(indio_dev); 883 i2c_set_clientdata(client, indio_dev); 884 data->client = client; 885 886 if (id) 887 name = id->name; 888 else if (ACPI_HANDLE(&client->dev)) 889 name = bmc150_magn_match_acpi_device(&client->dev); 890 else 891 return -ENOSYS; 892 893 mutex_init(&data->mutex); 894 data->regmap = devm_regmap_init_i2c(client, &bmc150_magn_regmap_config); 895 if (IS_ERR(data->regmap)) { 896 dev_err(&client->dev, "Failed to allocate register map\n"); 897 return PTR_ERR(data->regmap); 898 } 899 900 ret = bmc150_magn_init(data); 901 if (ret < 0) 902 return ret; 903 904 indio_dev->dev.parent = &client->dev; 905 indio_dev->channels = bmc150_magn_channels; 906 indio_dev->num_channels = ARRAY_SIZE(bmc150_magn_channels); 907 indio_dev->available_scan_masks = bmc150_magn_scan_masks; 908 indio_dev->name = name; 909 indio_dev->modes = INDIO_DIRECT_MODE; 910 indio_dev->info = &bmc150_magn_info; 911 912 if (client->irq <= 0) 913 client->irq = bmc150_magn_gpio_probe(client); 914 915 if (client->irq > 0) { 916 data->dready_trig = devm_iio_trigger_alloc(&client->dev, 917 "%s-dev%d", 918 indio_dev->name, 919 indio_dev->id); 920 if (!data->dready_trig) { 921 ret = -ENOMEM; 922 dev_err(&client->dev, "iio trigger alloc failed\n"); 923 goto err_poweroff; 924 } 925 926 data->dready_trig->dev.parent = &client->dev; 927 data->dready_trig->ops = &bmc150_magn_trigger_ops; 928 iio_trigger_set_drvdata(data->dready_trig, indio_dev); 929 ret = iio_trigger_register(data->dready_trig); 930 if (ret) { 931 dev_err(&client->dev, "iio trigger register failed\n"); 932 goto err_poweroff; 933 } 934 935 ret = iio_triggered_buffer_setup(indio_dev, 936 &iio_pollfunc_store_time, 937 bmc150_magn_trigger_handler, 938 NULL); 939 if (ret < 0) { 940 dev_err(&client->dev, 941 "iio triggered buffer setup failed\n"); 942 goto err_trigger_unregister; 943 } 944 945 ret = request_threaded_irq(client->irq, 946 iio_trigger_generic_data_rdy_poll, 947 NULL, 948 IRQF_TRIGGER_RISING | IRQF_ONESHOT, 949 BMC150_MAGN_IRQ_NAME, 950 data->dready_trig); 951 if (ret < 0) { 952 dev_err(&client->dev, "request irq %d failed\n", 953 client->irq); 954 goto err_buffer_cleanup; 955 } 956 } 957 958 ret = iio_device_register(indio_dev); 959 if (ret < 0) { 960 dev_err(&client->dev, "unable to register iio device\n"); 961 goto err_free_irq; 962 } 963 964 ret = pm_runtime_set_active(&client->dev); 965 if (ret) 966 goto err_iio_unregister; 967 968 pm_runtime_enable(&client->dev); 969 pm_runtime_set_autosuspend_delay(&client->dev, 970 BMC150_MAGN_AUTO_SUSPEND_DELAY_MS); 971 pm_runtime_use_autosuspend(&client->dev); 972 973 dev_dbg(&indio_dev->dev, "Registered device %s\n", name); 974 975 return 0; 976 977 err_iio_unregister: 978 iio_device_unregister(indio_dev); 979 err_free_irq: 980 if (client->irq > 0) 981 free_irq(client->irq, data->dready_trig); 982 err_buffer_cleanup: 983 if (data->dready_trig) 984 iio_triggered_buffer_cleanup(indio_dev); 985 err_trigger_unregister: 986 if (data->dready_trig) 987 iio_trigger_unregister(data->dready_trig); 988 err_poweroff: 989 bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, true); 990 return ret; 991 } 992 993 static int bmc150_magn_remove(struct i2c_client *client) 994 { 995 struct iio_dev *indio_dev = i2c_get_clientdata(client); 996 struct bmc150_magn_data *data = iio_priv(indio_dev); 997 998 pm_runtime_disable(&client->dev); 999 pm_runtime_set_suspended(&client->dev); 1000 pm_runtime_put_noidle(&client->dev); 1001 1002 iio_device_unregister(indio_dev); 1003 1004 if (client->irq > 0) 1005 free_irq(data->client->irq, data->dready_trig); 1006 1007 if (data->dready_trig) { 1008 iio_triggered_buffer_cleanup(indio_dev); 1009 iio_trigger_unregister(data->dready_trig); 1010 } 1011 1012 mutex_lock(&data->mutex); 1013 bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, true); 1014 mutex_unlock(&data->mutex); 1015 1016 return 0; 1017 } 1018 1019 #ifdef CONFIG_PM 1020 static int bmc150_magn_runtime_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 if (ret < 0) { 1031 dev_err(&data->client->dev, "powering off device failed\n"); 1032 return ret; 1033 } 1034 return 0; 1035 } 1036 1037 static int bmc150_magn_runtime_resume(struct device *dev) 1038 { 1039 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 1040 struct bmc150_magn_data *data = iio_priv(indio_dev); 1041 1042 return bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_NORMAL, 1043 true); 1044 } 1045 #endif 1046 1047 #ifdef CONFIG_PM_SLEEP 1048 static int bmc150_magn_suspend(struct device *dev) 1049 { 1050 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 1051 struct bmc150_magn_data *data = iio_priv(indio_dev); 1052 int ret; 1053 1054 mutex_lock(&data->mutex); 1055 ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SLEEP, 1056 true); 1057 mutex_unlock(&data->mutex); 1058 1059 return ret; 1060 } 1061 1062 static int bmc150_magn_resume(struct device *dev) 1063 { 1064 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 1065 struct bmc150_magn_data *data = iio_priv(indio_dev); 1066 int ret; 1067 1068 mutex_lock(&data->mutex); 1069 ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_NORMAL, 1070 true); 1071 mutex_unlock(&data->mutex); 1072 1073 return ret; 1074 } 1075 #endif 1076 1077 static const struct dev_pm_ops bmc150_magn_pm_ops = { 1078 SET_SYSTEM_SLEEP_PM_OPS(bmc150_magn_suspend, bmc150_magn_resume) 1079 SET_RUNTIME_PM_OPS(bmc150_magn_runtime_suspend, 1080 bmc150_magn_runtime_resume, NULL) 1081 }; 1082 1083 static const struct acpi_device_id bmc150_magn_acpi_match[] = { 1084 {"BMC150B", 0}, 1085 {}, 1086 }; 1087 MODULE_DEVICE_TABLE(acpi, bmc150_magn_acpi_match); 1088 1089 static const struct i2c_device_id bmc150_magn_id[] = { 1090 {"bmc150_magn", 0}, 1091 {}, 1092 }; 1093 MODULE_DEVICE_TABLE(i2c, bmc150_magn_id); 1094 1095 static struct i2c_driver bmc150_magn_driver = { 1096 .driver = { 1097 .name = BMC150_MAGN_DRV_NAME, 1098 .acpi_match_table = ACPI_PTR(bmc150_magn_acpi_match), 1099 .pm = &bmc150_magn_pm_ops, 1100 }, 1101 .probe = bmc150_magn_probe, 1102 .remove = bmc150_magn_remove, 1103 .id_table = bmc150_magn_id, 1104 }; 1105 module_i2c_driver(bmc150_magn_driver); 1106 1107 MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>"); 1108 MODULE_LICENSE("GPL v2"); 1109 MODULE_DESCRIPTION("BMC150 magnetometer driver"); 1110