1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Driver for the TI TMAG5273 Low-Power Linear 3D Hall-Effect Sensor 4 * 5 * Copyright (C) 2022 WolfVision GmbH 6 * 7 * Author: Gerald Loacker <gerald.loacker@wolfvision.net> 8 */ 9 10 #include <linux/bitfield.h> 11 #include <linux/bits.h> 12 #include <linux/delay.h> 13 #include <linux/module.h> 14 #include <linux/i2c.h> 15 #include <linux/regmap.h> 16 #include <linux/pm_runtime.h> 17 18 #include <linux/iio/iio.h> 19 #include <linux/iio/sysfs.h> 20 21 #define TMAG5273_DEVICE_CONFIG_1 0x00 22 #define TMAG5273_DEVICE_CONFIG_2 0x01 23 #define TMAG5273_SENSOR_CONFIG_1 0x02 24 #define TMAG5273_SENSOR_CONFIG_2 0x03 25 #define TMAG5273_X_THR_CONFIG 0x04 26 #define TMAG5273_Y_THR_CONFIG 0x05 27 #define TMAG5273_Z_THR_CONFIG 0x06 28 #define TMAG5273_T_CONFIG 0x07 29 #define TMAG5273_INT_CONFIG_1 0x08 30 #define TMAG5273_MAG_GAIN_CONFIG 0x09 31 #define TMAG5273_MAG_OFFSET_CONFIG_1 0x0A 32 #define TMAG5273_MAG_OFFSET_CONFIG_2 0x0B 33 #define TMAG5273_I2C_ADDRESS 0x0C 34 #define TMAG5273_DEVICE_ID 0x0D 35 #define TMAG5273_MANUFACTURER_ID_LSB 0x0E 36 #define TMAG5273_MANUFACTURER_ID_MSB 0x0F 37 #define TMAG5273_T_MSB_RESULT 0x10 38 #define TMAG5273_T_LSB_RESULT 0x11 39 #define TMAG5273_X_MSB_RESULT 0x12 40 #define TMAG5273_X_LSB_RESULT 0x13 41 #define TMAG5273_Y_MSB_RESULT 0x14 42 #define TMAG5273_Y_LSB_RESULT 0x15 43 #define TMAG5273_Z_MSB_RESULT 0x16 44 #define TMAG5273_Z_LSB_RESULT 0x17 45 #define TMAG5273_CONV_STATUS 0x18 46 #define TMAG5273_ANGLE_RESULT_MSB 0x19 47 #define TMAG5273_ANGLE_RESULT_LSB 0x1A 48 #define TMAG5273_MAGNITUDE_RESULT 0x1B 49 #define TMAG5273_DEVICE_STATUS 0x1C 50 #define TMAG5273_MAX_REG TMAG5273_DEVICE_STATUS 51 52 #define TMAG5273_AUTOSLEEP_DELAY_MS 5000 53 #define TMAG5273_MAX_AVERAGE 32 54 55 /* 56 * bits in the TMAG5273_MANUFACTURER_ID_LSB / MSB register 57 * 16-bit unique manufacturer ID 0x49 / 0x54 = "TI" 58 */ 59 #define TMAG5273_MANUFACTURER_ID 0x5449 60 61 /* bits in the TMAG5273_DEVICE_CONFIG_1 register */ 62 #define TMAG5273_AVG_MODE_MASK GENMASK(4, 2) 63 #define TMAG5273_AVG_1_MODE FIELD_PREP(TMAG5273_AVG_MODE_MASK, 0) 64 #define TMAG5273_AVG_2_MODE FIELD_PREP(TMAG5273_AVG_MODE_MASK, 1) 65 #define TMAG5273_AVG_4_MODE FIELD_PREP(TMAG5273_AVG_MODE_MASK, 2) 66 #define TMAG5273_AVG_8_MODE FIELD_PREP(TMAG5273_AVG_MODE_MASK, 3) 67 #define TMAG5273_AVG_16_MODE FIELD_PREP(TMAG5273_AVG_MODE_MASK, 4) 68 #define TMAG5273_AVG_32_MODE FIELD_PREP(TMAG5273_AVG_MODE_MASK, 5) 69 70 /* bits in the TMAG5273_DEVICE_CONFIG_2 register */ 71 #define TMAG5273_OP_MODE_MASK GENMASK(1, 0) 72 #define TMAG5273_OP_MODE_STANDBY FIELD_PREP(TMAG5273_OP_MODE_MASK, 0) 73 #define TMAG5273_OP_MODE_SLEEP FIELD_PREP(TMAG5273_OP_MODE_MASK, 1) 74 #define TMAG5273_OP_MODE_CONT FIELD_PREP(TMAG5273_OP_MODE_MASK, 2) 75 #define TMAG5273_OP_MODE_WAKEUP FIELD_PREP(TMAG5273_OP_MODE_MASK, 3) 76 77 /* bits in the TMAG5273_SENSOR_CONFIG_1 register */ 78 #define TMAG5273_MAG_CH_EN_MASK GENMASK(7, 4) 79 #define TMAG5273_MAG_CH_EN_X_Y_Z 7 80 81 /* bits in the TMAG5273_SENSOR_CONFIG_2 register */ 82 #define TMAG5273_Z_RANGE_MASK BIT(0) 83 #define TMAG5273_X_Y_RANGE_MASK BIT(1) 84 #define TMAG5273_ANGLE_EN_MASK GENMASK(3, 2) 85 #define TMAG5273_ANGLE_EN_OFF 0 86 #define TMAG5273_ANGLE_EN_X_Y 1 87 #define TMAG5273_ANGLE_EN_Y_Z 2 88 #define TMAG5273_ANGLE_EN_X_Z 3 89 90 /* bits in the TMAG5273_T_CONFIG register */ 91 #define TMAG5273_T_CH_EN BIT(0) 92 93 /* bits in the TMAG5273_DEVICE_ID register */ 94 #define TMAG5273_VERSION_MASK GENMASK(1, 0) 95 96 /* bits in the TMAG5273_CONV_STATUS register */ 97 #define TMAG5273_CONV_STATUS_COMPLETE BIT(0) 98 99 enum tmag5273_channels { 100 TEMPERATURE = 0, 101 AXIS_X, 102 AXIS_Y, 103 AXIS_Z, 104 ANGLE, 105 MAGNITUDE, 106 }; 107 108 enum tmag5273_scale_index { 109 MAGN_RANGE_LOW = 0, 110 MAGN_RANGE_HIGH, 111 MAGN_RANGE_NUM 112 }; 113 114 /* state container for the TMAG5273 driver */ 115 struct tmag5273_data { 116 struct device *dev; 117 unsigned int devid; 118 unsigned int version; 119 char name[16]; 120 unsigned int conv_avg; 121 enum tmag5273_scale_index scale_index; 122 unsigned int angle_measurement; 123 struct regmap *map; 124 125 /* 126 * Locks the sensor for exclusive use during a measurement (which 127 * involves several register transactions so the regmap lock is not 128 * enough) so that measurements get serialized in a 129 * first-come-first-serve manner. 130 */ 131 struct mutex lock; 132 }; 133 134 static const char *const tmag5273_angle_names[] = { "off", "x-y", "y-z", "x-z" }; 135 136 /* 137 * Averaging enables additional sampling of the sensor data to reduce the noise 138 * effect, but also increases conversion time. 139 */ 140 static const unsigned int tmag5273_avg_table[] = { 141 1, 2, 4, 8, 16, 32, 142 }; 143 144 /* 145 * Magnetic resolution in Gauss for different TMAG5273 versions. 146 * Scale[Gauss] = Range[mT] * 1000 / 2^15 * 10, (1 mT = 10 Gauss) 147 * Only version 1 and 2 are valid, version 0 and 3 are reserved. 148 */ 149 static const struct iio_val_int_plus_micro tmag5273_scale[][MAGN_RANGE_NUM] = { 150 { { 0, 0 }, { 0, 0 } }, 151 { { 0, 12200 }, { 0, 24400 } }, 152 { { 0, 40600 }, { 0, 81200 } }, 153 { { 0, 0 }, { 0, 0 } }, 154 }; 155 156 static int tmag5273_get_measure(struct tmag5273_data *data, s16 *t, s16 *x, 157 s16 *y, s16 *z, u16 *angle, u16 *magnitude) 158 { 159 unsigned int status, val; 160 __be16 reg_data[4]; 161 int ret; 162 163 mutex_lock(&data->lock); 164 165 /* 166 * Max. conversion time is 2425 us in 32x averaging mode for all three 167 * channels. Since we are in continuous measurement mode, a measurement 168 * may already be there, so poll for completed measurement with 169 * timeout. 170 */ 171 ret = regmap_read_poll_timeout(data->map, TMAG5273_CONV_STATUS, status, 172 status & TMAG5273_CONV_STATUS_COMPLETE, 173 100, 10000); 174 if (ret) { 175 dev_err(data->dev, "timeout waiting for measurement\n"); 176 goto out_unlock; 177 } 178 179 ret = regmap_bulk_read(data->map, TMAG5273_T_MSB_RESULT, reg_data, 180 sizeof(reg_data)); 181 if (ret) 182 goto out_unlock; 183 *t = be16_to_cpu(reg_data[0]); 184 *x = be16_to_cpu(reg_data[1]); 185 *y = be16_to_cpu(reg_data[2]); 186 *z = be16_to_cpu(reg_data[3]); 187 188 ret = regmap_bulk_read(data->map, TMAG5273_ANGLE_RESULT_MSB, 189 ®_data[0], sizeof(reg_data[0])); 190 if (ret) 191 goto out_unlock; 192 /* 193 * angle has 9 bits integer value and 4 bits fractional part 194 * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 195 * 0 0 0 a a a a a a a a a f f f f 196 */ 197 *angle = be16_to_cpu(reg_data[0]); 198 199 ret = regmap_read(data->map, TMAG5273_MAGNITUDE_RESULT, &val); 200 if (ret < 0) 201 goto out_unlock; 202 *magnitude = val; 203 204 out_unlock: 205 mutex_unlock(&data->lock); 206 return ret; 207 } 208 209 static int tmag5273_write_osr(struct tmag5273_data *data, int val) 210 { 211 int i; 212 213 if (val == data->conv_avg) 214 return 0; 215 216 for (i = 0; i < ARRAY_SIZE(tmag5273_avg_table); i++) { 217 if (tmag5273_avg_table[i] == val) 218 break; 219 } 220 if (i == ARRAY_SIZE(tmag5273_avg_table)) 221 return -EINVAL; 222 data->conv_avg = val; 223 224 return regmap_update_bits(data->map, TMAG5273_DEVICE_CONFIG_1, 225 TMAG5273_AVG_MODE_MASK, 226 FIELD_PREP(TMAG5273_AVG_MODE_MASK, i)); 227 } 228 229 static int tmag5273_write_scale(struct tmag5273_data *data, int scale_micro) 230 { 231 u32 value; 232 int i; 233 234 for (i = 0; i < ARRAY_SIZE(tmag5273_scale[0]); i++) { 235 if (tmag5273_scale[data->version][i].micro == scale_micro) 236 break; 237 } 238 if (i == ARRAY_SIZE(tmag5273_scale[0])) 239 return -EINVAL; 240 data->scale_index = i; 241 242 if (data->scale_index == MAGN_RANGE_LOW) 243 value = 0; 244 else 245 value = TMAG5273_Z_RANGE_MASK | TMAG5273_X_Y_RANGE_MASK; 246 247 return regmap_update_bits(data->map, TMAG5273_SENSOR_CONFIG_2, 248 TMAG5273_Z_RANGE_MASK | TMAG5273_X_Y_RANGE_MASK, value); 249 } 250 251 static int tmag5273_read_avail(struct iio_dev *indio_dev, 252 struct iio_chan_spec const *chan, 253 const int **vals, int *type, int *length, 254 long mask) 255 { 256 struct tmag5273_data *data = iio_priv(indio_dev); 257 258 switch (mask) { 259 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 260 *vals = tmag5273_avg_table; 261 *type = IIO_VAL_INT; 262 *length = ARRAY_SIZE(tmag5273_avg_table); 263 return IIO_AVAIL_LIST; 264 case IIO_CHAN_INFO_SCALE: 265 switch (chan->type) { 266 case IIO_MAGN: 267 *type = IIO_VAL_INT_PLUS_MICRO; 268 *vals = (int *)tmag5273_scale[data->version]; 269 *length = ARRAY_SIZE(tmag5273_scale[data->version]) * 270 MAGN_RANGE_NUM; 271 return IIO_AVAIL_LIST; 272 default: 273 return -EINVAL; 274 } 275 default: 276 return -EINVAL; 277 } 278 } 279 280 static int tmag5273_read_raw(struct iio_dev *indio_dev, 281 const struct iio_chan_spec *chan, int *val, 282 int *val2, long mask) 283 { 284 struct tmag5273_data *data = iio_priv(indio_dev); 285 s16 t, x, y, z; 286 u16 angle, magnitude; 287 int ret; 288 289 switch (mask) { 290 case IIO_CHAN_INFO_PROCESSED: 291 case IIO_CHAN_INFO_RAW: 292 ret = pm_runtime_resume_and_get(data->dev); 293 if (ret < 0) 294 return ret; 295 296 ret = tmag5273_get_measure(data, &t, &x, &y, &z, &angle, &magnitude); 297 298 pm_runtime_mark_last_busy(data->dev); 299 pm_runtime_put_autosuspend(data->dev); 300 301 if (ret) 302 return ret; 303 304 switch (chan->address) { 305 case TEMPERATURE: 306 *val = t; 307 return IIO_VAL_INT; 308 case AXIS_X: 309 *val = x; 310 return IIO_VAL_INT; 311 case AXIS_Y: 312 *val = y; 313 return IIO_VAL_INT; 314 case AXIS_Z: 315 *val = z; 316 return IIO_VAL_INT; 317 case ANGLE: 318 *val = angle; 319 return IIO_VAL_INT; 320 case MAGNITUDE: 321 *val = magnitude; 322 return IIO_VAL_INT; 323 default: 324 return -EINVAL; 325 } 326 case IIO_CHAN_INFO_SCALE: 327 switch (chan->type) { 328 case IIO_TEMP: 329 /* 330 * Convert device specific value to millicelsius. 331 * Resolution from the sensor is 60.1 LSB/celsius and 332 * the reference value at 25 celsius is 17508 LSBs. 333 */ 334 *val = 10000; 335 *val2 = 601; 336 return IIO_VAL_FRACTIONAL; 337 case IIO_MAGN: 338 /* Magnetic resolution in uT */ 339 *val = 0; 340 *val2 = tmag5273_scale[data->version] 341 [data->scale_index].micro; 342 return IIO_VAL_INT_PLUS_MICRO; 343 case IIO_ANGL: 344 /* 345 * Angle is in degrees and has four fractional bits, 346 * therefore use 1/16 * pi/180 to convert to radians. 347 */ 348 *val = 1000; 349 *val2 = 916732; 350 return IIO_VAL_FRACTIONAL; 351 default: 352 return -EINVAL; 353 } 354 case IIO_CHAN_INFO_OFFSET: 355 switch (chan->type) { 356 case IIO_TEMP: 357 *val = -16005; 358 return IIO_VAL_INT; 359 default: 360 return -EINVAL; 361 } 362 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 363 *val = data->conv_avg; 364 return IIO_VAL_INT; 365 366 default: 367 return -EINVAL; 368 } 369 } 370 371 static int tmag5273_write_raw(struct iio_dev *indio_dev, 372 struct iio_chan_spec const *chan, int val, 373 int val2, long mask) 374 { 375 struct tmag5273_data *data = iio_priv(indio_dev); 376 377 switch (mask) { 378 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 379 return tmag5273_write_osr(data, val); 380 case IIO_CHAN_INFO_SCALE: 381 switch (chan->type) { 382 case IIO_MAGN: 383 if (val) 384 return -EINVAL; 385 return tmag5273_write_scale(data, val2); 386 default: 387 return -EINVAL; 388 } 389 default: 390 return -EINVAL; 391 } 392 } 393 394 #define TMAG5273_AXIS_CHANNEL(axis, index) \ 395 { \ 396 .type = IIO_MAGN, \ 397 .modified = 1, \ 398 .channel2 = IIO_MOD_##axis, \ 399 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 400 BIT(IIO_CHAN_INFO_SCALE), \ 401 .info_mask_shared_by_type_available = \ 402 BIT(IIO_CHAN_INFO_SCALE), \ 403 .info_mask_shared_by_all = \ 404 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \ 405 .info_mask_shared_by_all_available = \ 406 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \ 407 .address = index, \ 408 .scan_index = index, \ 409 .scan_type = { \ 410 .sign = 's', \ 411 .realbits = 16, \ 412 .storagebits = 16, \ 413 .endianness = IIO_CPU, \ 414 }, \ 415 } 416 417 static const struct iio_chan_spec tmag5273_channels[] = { 418 { 419 .type = IIO_TEMP, 420 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 421 BIT(IIO_CHAN_INFO_SCALE) | 422 BIT(IIO_CHAN_INFO_OFFSET), 423 .address = TEMPERATURE, 424 .scan_index = TEMPERATURE, 425 .scan_type = { 426 .sign = 'u', 427 .realbits = 16, 428 .storagebits = 16, 429 .endianness = IIO_CPU, 430 }, 431 }, 432 TMAG5273_AXIS_CHANNEL(X, AXIS_X), 433 TMAG5273_AXIS_CHANNEL(Y, AXIS_Y), 434 TMAG5273_AXIS_CHANNEL(Z, AXIS_Z), 435 { 436 .type = IIO_ANGL, 437 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 438 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), 439 .info_mask_shared_by_all = 440 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 441 .info_mask_shared_by_all_available = 442 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 443 .address = ANGLE, 444 .scan_index = ANGLE, 445 .scan_type = { 446 .sign = 'u', 447 .realbits = 16, 448 .storagebits = 16, 449 .endianness = IIO_CPU, 450 }, 451 }, 452 { 453 .type = IIO_DISTANCE, 454 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 455 .info_mask_shared_by_all = 456 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 457 .info_mask_shared_by_all_available = 458 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 459 .address = MAGNITUDE, 460 .scan_index = MAGNITUDE, 461 .scan_type = { 462 .sign = 'u', 463 .realbits = 16, 464 .storagebits = 16, 465 .endianness = IIO_CPU, 466 }, 467 }, 468 IIO_CHAN_SOFT_TIMESTAMP(6), 469 }; 470 471 static const struct iio_info tmag5273_info = { 472 .read_avail = tmag5273_read_avail, 473 .read_raw = tmag5273_read_raw, 474 .write_raw = tmag5273_write_raw, 475 }; 476 477 static bool tmag5273_volatile_reg(struct device *dev, unsigned int reg) 478 { 479 return reg >= TMAG5273_T_MSB_RESULT && reg <= TMAG5273_MAGNITUDE_RESULT; 480 } 481 482 static const struct regmap_config tmag5273_regmap_config = { 483 .reg_bits = 8, 484 .val_bits = 8, 485 .max_register = TMAG5273_MAX_REG, 486 .volatile_reg = tmag5273_volatile_reg, 487 }; 488 489 static int tmag5273_set_operating_mode(struct tmag5273_data *data, 490 unsigned int val) 491 { 492 return regmap_write(data->map, TMAG5273_DEVICE_CONFIG_2, val); 493 } 494 495 static void tmag5273_read_device_property(struct tmag5273_data *data) 496 { 497 struct device *dev = data->dev; 498 int ret; 499 500 data->angle_measurement = TMAG5273_ANGLE_EN_X_Y; 501 502 ret = device_property_match_property_string(dev, "ti,angle-measurement", 503 tmag5273_angle_names, 504 ARRAY_SIZE(tmag5273_angle_names)); 505 if (ret >= 0) 506 data->angle_measurement = ret; 507 } 508 509 static void tmag5273_wake_up(struct tmag5273_data *data) 510 { 511 int val; 512 513 /* Wake up the chip by sending a dummy I2C command */ 514 regmap_read(data->map, TMAG5273_DEVICE_ID, &val); 515 /* 516 * Time to go to stand-by mode from sleep mode is 50us 517 * typically, during this time no I2C access is possible. 518 */ 519 usleep_range(80, 200); 520 } 521 522 static int tmag5273_chip_init(struct tmag5273_data *data) 523 { 524 int ret; 525 526 ret = regmap_write(data->map, TMAG5273_DEVICE_CONFIG_1, 527 TMAG5273_AVG_32_MODE); 528 if (ret) 529 return ret; 530 data->conv_avg = 32; 531 532 ret = regmap_write(data->map, TMAG5273_DEVICE_CONFIG_2, 533 TMAG5273_OP_MODE_CONT); 534 if (ret) 535 return ret; 536 537 ret = regmap_write(data->map, TMAG5273_SENSOR_CONFIG_1, 538 FIELD_PREP(TMAG5273_MAG_CH_EN_MASK, 539 TMAG5273_MAG_CH_EN_X_Y_Z)); 540 if (ret) 541 return ret; 542 543 ret = regmap_write(data->map, TMAG5273_SENSOR_CONFIG_2, 544 FIELD_PREP(TMAG5273_ANGLE_EN_MASK, 545 data->angle_measurement)); 546 if (ret) 547 return ret; 548 data->scale_index = MAGN_RANGE_LOW; 549 550 return regmap_write(data->map, TMAG5273_T_CONFIG, TMAG5273_T_CH_EN); 551 } 552 553 static int tmag5273_check_device_id(struct tmag5273_data *data) 554 { 555 __le16 devid; 556 int val, ret; 557 558 ret = regmap_read(data->map, TMAG5273_DEVICE_ID, &val); 559 if (ret) 560 return dev_err_probe(data->dev, ret, "failed to power on device\n"); 561 data->version = FIELD_PREP(TMAG5273_VERSION_MASK, val); 562 563 ret = regmap_bulk_read(data->map, TMAG5273_MANUFACTURER_ID_LSB, &devid, 564 sizeof(devid)); 565 if (ret) 566 return dev_err_probe(data->dev, ret, "failed to read device ID\n"); 567 data->devid = le16_to_cpu(devid); 568 569 switch (data->devid) { 570 case TMAG5273_MANUFACTURER_ID: 571 /* 572 * The device name matches the orderable part number. 'x' stands 573 * for A, B, C or D devices, which have different I2C addresses. 574 * Versions 1 or 2 (0 and 3 is reserved) stands for different 575 * magnetic strengths. 576 */ 577 snprintf(data->name, sizeof(data->name), "tmag5273x%1u", data->version); 578 if (data->version < 1 || data->version > 2) 579 dev_warn(data->dev, "Unsupported device %s\n", data->name); 580 return 0; 581 default: 582 /* 583 * Only print warning in case of unknown device ID to allow 584 * fallback compatible in device tree. 585 */ 586 dev_warn(data->dev, "Unknown device ID 0x%x\n", data->devid); 587 return 0; 588 } 589 } 590 591 static void tmag5273_power_down(void *data) 592 { 593 tmag5273_set_operating_mode(data, TMAG5273_OP_MODE_SLEEP); 594 } 595 596 static int tmag5273_probe(struct i2c_client *i2c) 597 { 598 struct device *dev = &i2c->dev; 599 struct tmag5273_data *data; 600 struct iio_dev *indio_dev; 601 int ret; 602 603 indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); 604 if (!indio_dev) 605 return -ENOMEM; 606 607 data = iio_priv(indio_dev); 608 data->dev = dev; 609 i2c_set_clientdata(i2c, indio_dev); 610 611 data->map = devm_regmap_init_i2c(i2c, &tmag5273_regmap_config); 612 if (IS_ERR(data->map)) 613 return dev_err_probe(dev, PTR_ERR(data->map), 614 "failed to allocate register map\n"); 615 616 mutex_init(&data->lock); 617 618 ret = devm_regulator_get_enable(dev, "vcc"); 619 if (ret) 620 return dev_err_probe(dev, ret, "failed to enable regulator\n"); 621 622 tmag5273_wake_up(data); 623 624 ret = tmag5273_check_device_id(data); 625 if (ret) 626 return ret; 627 628 ret = tmag5273_set_operating_mode(data, TMAG5273_OP_MODE_CONT); 629 if (ret) 630 return dev_err_probe(dev, ret, "failed to power on device\n"); 631 632 /* 633 * Register powerdown deferred callback which suspends the chip 634 * after module unloaded. 635 * 636 * TMAG5273 should be in SUSPEND mode in the two cases: 637 * 1) When driver is loaded, but we do not have any data or 638 * configuration requests to it (we are solving it using 639 * autosuspend feature). 640 * 2) When driver is unloaded and device is not used (devm action is 641 * used in this case). 642 */ 643 ret = devm_add_action_or_reset(dev, tmag5273_power_down, data); 644 if (ret) 645 return dev_err_probe(dev, ret, "failed to add powerdown action\n"); 646 647 ret = pm_runtime_set_active(dev); 648 if (ret < 0) 649 return ret; 650 651 ret = devm_pm_runtime_enable(dev); 652 if (ret) 653 return ret; 654 655 pm_runtime_get_noresume(dev); 656 pm_runtime_set_autosuspend_delay(dev, TMAG5273_AUTOSLEEP_DELAY_MS); 657 pm_runtime_use_autosuspend(dev); 658 659 tmag5273_read_device_property(data); 660 661 ret = tmag5273_chip_init(data); 662 if (ret) 663 return dev_err_probe(dev, ret, "failed to init device\n"); 664 665 indio_dev->info = &tmag5273_info; 666 indio_dev->modes = INDIO_DIRECT_MODE; 667 indio_dev->name = data->name; 668 indio_dev->channels = tmag5273_channels; 669 indio_dev->num_channels = ARRAY_SIZE(tmag5273_channels); 670 671 pm_runtime_mark_last_busy(dev); 672 pm_runtime_put_autosuspend(dev); 673 674 ret = devm_iio_device_register(dev, indio_dev); 675 if (ret) 676 return dev_err_probe(dev, ret, "device register failed\n"); 677 678 return 0; 679 } 680 681 static int tmag5273_runtime_suspend(struct device *dev) 682 { 683 struct iio_dev *indio_dev = dev_get_drvdata(dev); 684 struct tmag5273_data *data = iio_priv(indio_dev); 685 int ret; 686 687 ret = tmag5273_set_operating_mode(data, TMAG5273_OP_MODE_SLEEP); 688 if (ret) 689 dev_err(dev, "failed to power off device (%pe)\n", ERR_PTR(ret)); 690 691 return ret; 692 } 693 694 static int tmag5273_runtime_resume(struct device *dev) 695 { 696 struct iio_dev *indio_dev = dev_get_drvdata(dev); 697 struct tmag5273_data *data = iio_priv(indio_dev); 698 int ret; 699 700 tmag5273_wake_up(data); 701 702 ret = tmag5273_set_operating_mode(data, TMAG5273_OP_MODE_CONT); 703 if (ret) 704 dev_err(dev, "failed to power on device (%pe)\n", ERR_PTR(ret)); 705 706 return ret; 707 } 708 709 static DEFINE_RUNTIME_DEV_PM_OPS(tmag5273_pm_ops, 710 tmag5273_runtime_suspend, tmag5273_runtime_resume, 711 NULL); 712 713 static const struct i2c_device_id tmag5273_id[] = { 714 { "tmag5273" }, 715 { /* sentinel */ } 716 }; 717 MODULE_DEVICE_TABLE(i2c, tmag5273_id); 718 719 static const struct of_device_id tmag5273_of_match[] = { 720 { .compatible = "ti,tmag5273" }, 721 { /* sentinel */ } 722 }; 723 MODULE_DEVICE_TABLE(of, tmag5273_of_match); 724 725 static struct i2c_driver tmag5273_driver = { 726 .driver = { 727 .name = "tmag5273", 728 .of_match_table = tmag5273_of_match, 729 .pm = pm_ptr(&tmag5273_pm_ops), 730 }, 731 .probe = tmag5273_probe, 732 .id_table = tmag5273_id, 733 }; 734 module_i2c_driver(tmag5273_driver); 735 736 MODULE_DESCRIPTION("TI TMAG5273 Low-Power Linear 3D Hall-Effect Sensor driver"); 737 MODULE_AUTHOR("Gerald Loacker <gerald.loacker@wolfvision.net>"); 738 MODULE_LICENSE("GPL"); 739