1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * A sensor driver for the magnetometer AK8975. 4 * 5 * Magnetic compass sensor driver for monitoring magnetic flux information. 6 * 7 * Copyright (c) 2010, NVIDIA Corporation. 8 */ 9 10 #include <linux/module.h> 11 #include <linux/mod_devicetable.h> 12 #include <linux/kernel.h> 13 #include <linux/slab.h> 14 #include <linux/i2c.h> 15 #include <linux/interrupt.h> 16 #include <linux/err.h> 17 #include <linux/mutex.h> 18 #include <linux/delay.h> 19 #include <linux/bitops.h> 20 #include <linux/gpio/consumer.h> 21 #include <linux/regulator/consumer.h> 22 #include <linux/pm_runtime.h> 23 24 #include <linux/iio/iio.h> 25 #include <linux/iio/sysfs.h> 26 #include <linux/iio/buffer.h> 27 #include <linux/iio/trigger.h> 28 #include <linux/iio/trigger_consumer.h> 29 #include <linux/iio/triggered_buffer.h> 30 31 /* 32 * Register definitions, as well as various shifts and masks to get at the 33 * individual fields of the registers. 34 */ 35 #define AK8975_REG_WIA 0x00 36 #define AK8975_DEVICE_ID 0x48 37 38 #define AK8975_REG_INFO 0x01 39 40 #define AK8975_REG_ST1 0x02 41 #define AK8975_REG_ST1_DRDY_SHIFT 0 42 #define AK8975_REG_ST1_DRDY_MASK (1 << AK8975_REG_ST1_DRDY_SHIFT) 43 44 #define AK8975_REG_HXL 0x03 45 #define AK8975_REG_HXH 0x04 46 #define AK8975_REG_HYL 0x05 47 #define AK8975_REG_HYH 0x06 48 #define AK8975_REG_HZL 0x07 49 #define AK8975_REG_HZH 0x08 50 #define AK8975_REG_ST2 0x09 51 #define AK8975_REG_ST2_DERR_SHIFT 2 52 #define AK8975_REG_ST2_DERR_MASK (1 << AK8975_REG_ST2_DERR_SHIFT) 53 54 #define AK8975_REG_ST2_HOFL_SHIFT 3 55 #define AK8975_REG_ST2_HOFL_MASK (1 << AK8975_REG_ST2_HOFL_SHIFT) 56 57 #define AK8975_REG_CNTL 0x0A 58 #define AK8975_REG_CNTL_MODE_SHIFT 0 59 #define AK8975_REG_CNTL_MODE_MASK (0xF << AK8975_REG_CNTL_MODE_SHIFT) 60 #define AK8975_REG_CNTL_MODE_POWER_DOWN 0x00 61 #define AK8975_REG_CNTL_MODE_ONCE 0x01 62 #define AK8975_REG_CNTL_MODE_SELF_TEST 0x08 63 #define AK8975_REG_CNTL_MODE_FUSE_ROM 0x0F 64 65 #define AK8975_REG_RSVC 0x0B 66 #define AK8975_REG_ASTC 0x0C 67 #define AK8975_REG_TS1 0x0D 68 #define AK8975_REG_TS2 0x0E 69 #define AK8975_REG_I2CDIS 0x0F 70 #define AK8975_REG_ASAX 0x10 71 #define AK8975_REG_ASAY 0x11 72 #define AK8975_REG_ASAZ 0x12 73 74 #define AK8975_MAX_REGS AK8975_REG_ASAZ 75 76 /* 77 * AK09912 Register definitions 78 */ 79 #define AK09912_REG_WIA1 0x00 80 #define AK09912_REG_WIA2 0x01 81 #define AK09918_DEVICE_ID 0x0C 82 #define AK09916_DEVICE_ID 0x09 83 #define AK09912_DEVICE_ID 0x04 84 #define AK09911_DEVICE_ID 0x05 85 86 #define AK09911_REG_INFO1 0x02 87 #define AK09911_REG_INFO2 0x03 88 89 #define AK09912_REG_ST1 0x10 90 91 #define AK09912_REG_ST1_DRDY_SHIFT 0 92 #define AK09912_REG_ST1_DRDY_MASK (1 << AK09912_REG_ST1_DRDY_SHIFT) 93 94 #define AK09912_REG_HXL 0x11 95 #define AK09912_REG_HXH 0x12 96 #define AK09912_REG_HYL 0x13 97 #define AK09912_REG_HYH 0x14 98 #define AK09912_REG_HZL 0x15 99 #define AK09912_REG_HZH 0x16 100 #define AK09912_REG_TMPS 0x17 101 102 #define AK09912_REG_ST2 0x18 103 #define AK09912_REG_ST2_HOFL_SHIFT 3 104 #define AK09912_REG_ST2_HOFL_MASK (1 << AK09912_REG_ST2_HOFL_SHIFT) 105 106 #define AK09912_REG_CNTL1 0x30 107 108 #define AK09912_REG_CNTL2 0x31 109 #define AK09912_REG_CNTL_MODE_POWER_DOWN 0x00 110 #define AK09912_REG_CNTL_MODE_ONCE 0x01 111 #define AK09912_REG_CNTL_MODE_SELF_TEST 0x10 112 #define AK09912_REG_CNTL_MODE_FUSE_ROM 0x1F 113 #define AK09912_REG_CNTL2_MODE_SHIFT 0 114 #define AK09912_REG_CNTL2_MODE_MASK (0x1F << AK09912_REG_CNTL2_MODE_SHIFT) 115 116 #define AK09912_REG_CNTL3 0x32 117 118 #define AK09912_REG_TS1 0x33 119 #define AK09912_REG_TS2 0x34 120 #define AK09912_REG_TS3 0x35 121 #define AK09912_REG_I2CDIS 0x36 122 #define AK09912_REG_TS4 0x37 123 124 #define AK09912_REG_ASAX 0x60 125 #define AK09912_REG_ASAY 0x61 126 #define AK09912_REG_ASAZ 0x62 127 128 #define AK09912_MAX_REGS AK09912_REG_ASAZ 129 130 /* 131 * Miscellaneous values. 132 */ 133 #define AK8975_MAX_CONVERSION_TIMEOUT 500 134 #define AK8975_CONVERSION_DONE_POLL_TIME 10 135 #define AK8975_DATA_READY_TIMEOUT ((100*HZ)/1000) 136 137 /* 138 * Precalculate scale factor (in Gauss units) for each axis and 139 * store in the device data. 140 * 141 * This scale factor is axis-dependent, and is derived from 3 calibration 142 * factors ASA(x), ASA(y), and ASA(z). 143 * 144 * These ASA values are read from the sensor device at start of day, and 145 * cached in the device context struct. 146 * 147 * Adjusting the flux value with the sensitivity adjustment value should be 148 * done via the following formula: 149 * 150 * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 ) 151 * where H is the raw value, ASA is the sensitivity adjustment, and Hadj 152 * is the resultant adjusted value. 153 * 154 * We reduce the formula to: 155 * 156 * Hadj = H * (ASA + 128) / 256 157 * 158 * H is in the range of -4096 to 4095. The magnetometer has a range of 159 * +-1229uT. To go from the raw value to uT is: 160 * 161 * HuT = H * 1229/4096, or roughly, 3/10. 162 * 163 * Since 1uT = 0.01 gauss, our final scale factor becomes: 164 * 165 * Hadj = H * ((ASA + 128) / 256) * 3/10 * 1/100 166 * Hadj = H * ((ASA + 128) * 0.003) / 256 167 * 168 * Since ASA doesn't change, we cache the resultant scale factor into the 169 * device context in ak8975_setup(). 170 * 171 * Given we use IIO_VAL_INT_PLUS_MICRO bit when displaying the scale, we 172 * multiply the stored scale value by 1e6. 173 */ 174 static long ak8975_raw_to_gauss(u16 data) 175 { 176 return (((long)data + 128) * 3000) / 256; 177 } 178 179 /* 180 * For AK8963 and AK09911, same calculation, but the device is less sensitive: 181 * 182 * H is in the range of +-8190. The magnetometer has a range of 183 * +-4912uT. To go from the raw value to uT is: 184 * 185 * HuT = H * 4912/8190, or roughly, 6/10, instead of 3/10. 186 */ 187 188 static long ak8963_09911_raw_to_gauss(u16 data) 189 { 190 return (((long)data + 128) * 6000) / 256; 191 } 192 193 /* 194 * For AK09912, same calculation, except the device is more sensitive: 195 * 196 * H is in the range of -32752 to 32752. The magnetometer has a range of 197 * +-4912uT. To go from the raw value to uT is: 198 * 199 * HuT = H * 4912/32752, or roughly, 3/20, instead of 3/10. 200 */ 201 static long ak09912_raw_to_gauss(u16 data) 202 { 203 return (((long)data + 128) * 1500) / 256; 204 } 205 206 /* Compatible Asahi Kasei Compass parts */ 207 enum asahi_compass_chipset { 208 AK8975, 209 AK8963, 210 AK09911, 211 AK09912, 212 AK09916, 213 AK09918, 214 }; 215 216 enum ak_ctrl_reg_addr { 217 ST1, 218 ST2, 219 CNTL, 220 ASA_BASE, 221 MAX_REGS, 222 REGS_END, 223 }; 224 225 enum ak_ctrl_reg_mask { 226 ST1_DRDY, 227 ST2_HOFL, 228 ST2_DERR, 229 CNTL_MODE, 230 MASK_END, 231 }; 232 233 enum ak_ctrl_mode { 234 POWER_DOWN, 235 MODE_ONCE, 236 SELF_TEST, 237 FUSE_ROM, 238 MODE_END, 239 }; 240 241 struct ak_def { 242 enum asahi_compass_chipset type; 243 long (*raw_to_gauss)(u16 data); 244 u16 range; 245 u8 ctrl_regs[REGS_END]; 246 u8 ctrl_masks[MASK_END]; 247 u8 ctrl_modes[MODE_END]; 248 u8 data_regs[3]; 249 }; 250 251 static const struct ak_def ak_def_array[] = { 252 [AK8975] = { 253 .type = AK8975, 254 .raw_to_gauss = ak8975_raw_to_gauss, 255 .range = 4096, 256 .ctrl_regs = { 257 AK8975_REG_ST1, 258 AK8975_REG_ST2, 259 AK8975_REG_CNTL, 260 AK8975_REG_ASAX, 261 AK8975_MAX_REGS}, 262 .ctrl_masks = { 263 AK8975_REG_ST1_DRDY_MASK, 264 AK8975_REG_ST2_HOFL_MASK, 265 AK8975_REG_ST2_DERR_MASK, 266 AK8975_REG_CNTL_MODE_MASK}, 267 .ctrl_modes = { 268 AK8975_REG_CNTL_MODE_POWER_DOWN, 269 AK8975_REG_CNTL_MODE_ONCE, 270 AK8975_REG_CNTL_MODE_SELF_TEST, 271 AK8975_REG_CNTL_MODE_FUSE_ROM}, 272 .data_regs = { 273 AK8975_REG_HXL, 274 AK8975_REG_HYL, 275 AK8975_REG_HZL}, 276 }, 277 [AK8963] = { 278 .type = AK8963, 279 .raw_to_gauss = ak8963_09911_raw_to_gauss, 280 .range = 8190, 281 .ctrl_regs = { 282 AK8975_REG_ST1, 283 AK8975_REG_ST2, 284 AK8975_REG_CNTL, 285 AK8975_REG_ASAX, 286 AK8975_MAX_REGS}, 287 .ctrl_masks = { 288 AK8975_REG_ST1_DRDY_MASK, 289 AK8975_REG_ST2_HOFL_MASK, 290 0, 291 AK8975_REG_CNTL_MODE_MASK}, 292 .ctrl_modes = { 293 AK8975_REG_CNTL_MODE_POWER_DOWN, 294 AK8975_REG_CNTL_MODE_ONCE, 295 AK8975_REG_CNTL_MODE_SELF_TEST, 296 AK8975_REG_CNTL_MODE_FUSE_ROM}, 297 .data_regs = { 298 AK8975_REG_HXL, 299 AK8975_REG_HYL, 300 AK8975_REG_HZL}, 301 }, 302 [AK09911] = { 303 .type = AK09911, 304 .raw_to_gauss = ak8963_09911_raw_to_gauss, 305 .range = 8192, 306 .ctrl_regs = { 307 AK09912_REG_ST1, 308 AK09912_REG_ST2, 309 AK09912_REG_CNTL2, 310 AK09912_REG_ASAX, 311 AK09912_MAX_REGS}, 312 .ctrl_masks = { 313 AK09912_REG_ST1_DRDY_MASK, 314 AK09912_REG_ST2_HOFL_MASK, 315 0, 316 AK09912_REG_CNTL2_MODE_MASK}, 317 .ctrl_modes = { 318 AK09912_REG_CNTL_MODE_POWER_DOWN, 319 AK09912_REG_CNTL_MODE_ONCE, 320 AK09912_REG_CNTL_MODE_SELF_TEST, 321 AK09912_REG_CNTL_MODE_FUSE_ROM}, 322 .data_regs = { 323 AK09912_REG_HXL, 324 AK09912_REG_HYL, 325 AK09912_REG_HZL}, 326 }, 327 [AK09912] = { 328 .type = AK09912, 329 .raw_to_gauss = ak09912_raw_to_gauss, 330 .range = 32752, 331 .ctrl_regs = { 332 AK09912_REG_ST1, 333 AK09912_REG_ST2, 334 AK09912_REG_CNTL2, 335 AK09912_REG_ASAX, 336 AK09912_MAX_REGS}, 337 .ctrl_masks = { 338 AK09912_REG_ST1_DRDY_MASK, 339 AK09912_REG_ST2_HOFL_MASK, 340 0, 341 AK09912_REG_CNTL2_MODE_MASK}, 342 .ctrl_modes = { 343 AK09912_REG_CNTL_MODE_POWER_DOWN, 344 AK09912_REG_CNTL_MODE_ONCE, 345 AK09912_REG_CNTL_MODE_SELF_TEST, 346 AK09912_REG_CNTL_MODE_FUSE_ROM}, 347 .data_regs = { 348 AK09912_REG_HXL, 349 AK09912_REG_HYL, 350 AK09912_REG_HZL}, 351 }, 352 [AK09916] = { 353 .type = AK09916, 354 .raw_to_gauss = ak09912_raw_to_gauss, 355 .range = 32752, 356 .ctrl_regs = { 357 AK09912_REG_ST1, 358 AK09912_REG_ST2, 359 AK09912_REG_CNTL2, 360 AK09912_REG_ASAX, 361 AK09912_MAX_REGS}, 362 .ctrl_masks = { 363 AK09912_REG_ST1_DRDY_MASK, 364 AK09912_REG_ST2_HOFL_MASK, 365 0, 366 AK09912_REG_CNTL2_MODE_MASK}, 367 .ctrl_modes = { 368 AK09912_REG_CNTL_MODE_POWER_DOWN, 369 AK09912_REG_CNTL_MODE_ONCE, 370 AK09912_REG_CNTL_MODE_SELF_TEST, 371 AK09912_REG_CNTL_MODE_FUSE_ROM}, 372 .data_regs = { 373 AK09912_REG_HXL, 374 AK09912_REG_HYL, 375 AK09912_REG_HZL}, 376 }, 377 [AK09918] = { 378 /* ak09918 is register compatible with ak09912 this is for avoid 379 * unknown id messages. 380 */ 381 .type = AK09918, 382 .raw_to_gauss = ak09912_raw_to_gauss, 383 .range = 32752, 384 .ctrl_regs = { 385 AK09912_REG_ST1, 386 AK09912_REG_ST2, 387 AK09912_REG_CNTL2, 388 AK09912_REG_ASAX, 389 AK09912_MAX_REGS}, 390 .ctrl_masks = { 391 AK09912_REG_ST1_DRDY_MASK, 392 AK09912_REG_ST2_HOFL_MASK, 393 0, 394 AK09912_REG_CNTL2_MODE_MASK}, 395 .ctrl_modes = { 396 AK09912_REG_CNTL_MODE_POWER_DOWN, 397 AK09912_REG_CNTL_MODE_ONCE, 398 AK09912_REG_CNTL_MODE_SELF_TEST, 399 AK09912_REG_CNTL_MODE_FUSE_ROM}, 400 .data_regs = { 401 AK09912_REG_HXL, 402 AK09912_REG_HYL, 403 AK09912_REG_HZL}, 404 } 405 }; 406 407 /* 408 * Per-instance context data for the device. 409 */ 410 struct ak8975_data { 411 struct i2c_client *client; 412 const struct ak_def *def; 413 struct mutex lock; 414 u8 asa[3]; 415 long raw_to_gauss[3]; 416 struct gpio_desc *eoc_gpiod; 417 struct gpio_desc *reset_gpiod; 418 int eoc_irq; 419 wait_queue_head_t data_ready_queue; 420 unsigned long flags; 421 u8 cntl_cache; 422 struct iio_mount_matrix orientation; 423 struct regulator *vdd; 424 struct regulator *vid; 425 426 /* Ensure natural alignment of timestamp */ 427 struct { 428 s16 channels[3]; 429 s64 ts __aligned(8); 430 } scan; 431 }; 432 433 /* Enable attached power regulator if any. */ 434 static int ak8975_power_on(const struct ak8975_data *data) 435 { 436 int ret; 437 438 ret = regulator_enable(data->vdd); 439 if (ret) { 440 dev_warn(&data->client->dev, 441 "Failed to enable specified Vdd supply\n"); 442 return ret; 443 } 444 ret = regulator_enable(data->vid); 445 if (ret) { 446 dev_warn(&data->client->dev, 447 "Failed to enable specified Vid supply\n"); 448 regulator_disable(data->vdd); 449 return ret; 450 } 451 452 gpiod_set_value_cansleep(data->reset_gpiod, 0); 453 454 /* 455 * According to the datasheet the power supply rise time is 200us 456 * and the minimum wait time before mode setting is 100us, in 457 * total 300us. Add some margin and say minimum 500us here. 458 */ 459 usleep_range(500, 1000); 460 return 0; 461 } 462 463 /* Disable attached power regulator if any. */ 464 static void ak8975_power_off(const struct ak8975_data *data) 465 { 466 gpiod_set_value_cansleep(data->reset_gpiod, 1); 467 468 regulator_disable(data->vid); 469 regulator_disable(data->vdd); 470 } 471 472 /* 473 * Return 0 if the i2c device is the one we expect. 474 * return a negative error number otherwise 475 */ 476 static int ak8975_who_i_am(struct i2c_client *client, 477 enum asahi_compass_chipset type) 478 { 479 u8 wia_val[2]; 480 int ret; 481 482 /* 483 * Signature for each device: 484 * Device | WIA1 | WIA2 485 * AK09918 | DEVICE_ID_| AK09918_DEVICE_ID 486 * AK09916 | DEVICE_ID_| AK09916_DEVICE_ID 487 * AK09912 | DEVICE_ID | AK09912_DEVICE_ID 488 * AK09911 | DEVICE_ID | AK09911_DEVICE_ID 489 * AK8975 | DEVICE_ID | NA 490 * AK8963 | DEVICE_ID | NA 491 */ 492 ret = i2c_smbus_read_i2c_block_data_or_emulated( 493 client, AK09912_REG_WIA1, 2, wia_val); 494 if (ret < 0) { 495 dev_err(&client->dev, "Error reading WIA\n"); 496 return ret; 497 } 498 499 if (wia_val[0] != AK8975_DEVICE_ID) 500 return -ENODEV; 501 502 switch (type) { 503 case AK8975: 504 case AK8963: 505 return 0; 506 case AK09911: 507 if (wia_val[1] == AK09911_DEVICE_ID) 508 return 0; 509 break; 510 case AK09912: 511 if (wia_val[1] == AK09912_DEVICE_ID) 512 return 0; 513 break; 514 case AK09916: 515 if (wia_val[1] == AK09916_DEVICE_ID) 516 return 0; 517 break; 518 case AK09918: 519 if (wia_val[1] == AK09918_DEVICE_ID) 520 return 0; 521 break; 522 } 523 524 dev_info(&client->dev, "Device ID %x is unknown.\n", wia_val[1]); 525 /* 526 * Let driver to probe on unknown id for support more register 527 * compatible variants. 528 */ 529 return 0; 530 } 531 532 /* 533 * Helper function to write to CNTL register. 534 */ 535 static int ak8975_set_mode(struct ak8975_data *data, enum ak_ctrl_mode mode) 536 { 537 u8 regval; 538 int ret; 539 540 regval = (data->cntl_cache & ~data->def->ctrl_masks[CNTL_MODE]) | 541 data->def->ctrl_modes[mode]; 542 ret = i2c_smbus_write_byte_data(data->client, 543 data->def->ctrl_regs[CNTL], regval); 544 if (ret < 0) { 545 return ret; 546 } 547 data->cntl_cache = regval; 548 /* After mode change wait atleast 100us */ 549 usleep_range(100, 500); 550 551 return 0; 552 } 553 554 /* 555 * Handle data ready irq 556 */ 557 static irqreturn_t ak8975_irq_handler(int irq, void *data) 558 { 559 struct ak8975_data *ak8975 = data; 560 561 set_bit(0, &ak8975->flags); 562 wake_up(&ak8975->data_ready_queue); 563 564 return IRQ_HANDLED; 565 } 566 567 /* 568 * Install data ready interrupt handler 569 */ 570 static int ak8975_setup_irq(struct ak8975_data *data) 571 { 572 struct i2c_client *client = data->client; 573 int rc; 574 int irq; 575 576 init_waitqueue_head(&data->data_ready_queue); 577 clear_bit(0, &data->flags); 578 if (client->irq) 579 irq = client->irq; 580 else 581 irq = gpiod_to_irq(data->eoc_gpiod); 582 583 rc = devm_request_irq(&client->dev, irq, ak8975_irq_handler, 584 IRQF_TRIGGER_RISING | IRQF_ONESHOT, 585 dev_name(&client->dev), data); 586 if (rc < 0) { 587 dev_err(&client->dev, "irq %d request failed: %d\n", irq, rc); 588 return rc; 589 } 590 591 data->eoc_irq = irq; 592 593 return rc; 594 } 595 596 597 /* 598 * Perform some start-of-day setup, including reading the asa calibration 599 * values and caching them. 600 */ 601 static int ak8975_setup(struct i2c_client *client) 602 { 603 struct iio_dev *indio_dev = i2c_get_clientdata(client); 604 struct ak8975_data *data = iio_priv(indio_dev); 605 int ret; 606 607 /* Write the fused rom access mode. */ 608 ret = ak8975_set_mode(data, FUSE_ROM); 609 if (ret < 0) { 610 dev_err(&client->dev, "Error in setting fuse access mode\n"); 611 return ret; 612 } 613 614 /* Get asa data and store in the device data. */ 615 ret = i2c_smbus_read_i2c_block_data_or_emulated( 616 client, data->def->ctrl_regs[ASA_BASE], 617 3, data->asa); 618 if (ret < 0) { 619 dev_err(&client->dev, "Not able to read asa data\n"); 620 return ret; 621 } 622 623 /* After reading fuse ROM data set power-down mode */ 624 ret = ak8975_set_mode(data, POWER_DOWN); 625 if (ret < 0) { 626 dev_err(&client->dev, "Error in setting power-down mode\n"); 627 return ret; 628 } 629 630 if (data->eoc_gpiod || client->irq > 0) { 631 ret = ak8975_setup_irq(data); 632 if (ret < 0) { 633 dev_err(&client->dev, 634 "Error setting data ready interrupt\n"); 635 return ret; 636 } 637 } 638 639 data->raw_to_gauss[0] = data->def->raw_to_gauss(data->asa[0]); 640 data->raw_to_gauss[1] = data->def->raw_to_gauss(data->asa[1]); 641 data->raw_to_gauss[2] = data->def->raw_to_gauss(data->asa[2]); 642 643 return 0; 644 } 645 646 static int wait_conversion_complete_gpio(struct ak8975_data *data) 647 { 648 struct i2c_client *client = data->client; 649 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT; 650 int ret; 651 652 /* Wait for the conversion to complete. */ 653 while (timeout_ms) { 654 msleep(AK8975_CONVERSION_DONE_POLL_TIME); 655 if (gpiod_get_value(data->eoc_gpiod)) 656 break; 657 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME; 658 } 659 if (!timeout_ms) { 660 dev_err(&client->dev, "Conversion timeout happened\n"); 661 return -EINVAL; 662 } 663 664 ret = i2c_smbus_read_byte_data(client, data->def->ctrl_regs[ST1]); 665 if (ret < 0) 666 dev_err(&client->dev, "Error in reading ST1\n"); 667 668 return ret; 669 } 670 671 static int wait_conversion_complete_polled(struct ak8975_data *data) 672 { 673 struct i2c_client *client = data->client; 674 u8 read_status; 675 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT; 676 int ret; 677 678 /* Wait for the conversion to complete. */ 679 while (timeout_ms) { 680 msleep(AK8975_CONVERSION_DONE_POLL_TIME); 681 ret = i2c_smbus_read_byte_data(client, 682 data->def->ctrl_regs[ST1]); 683 if (ret < 0) { 684 dev_err(&client->dev, "Error in reading ST1\n"); 685 return ret; 686 } 687 read_status = ret; 688 if (read_status) 689 break; 690 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME; 691 } 692 if (!timeout_ms) { 693 dev_err(&client->dev, "Conversion timeout happened\n"); 694 return -EINVAL; 695 } 696 697 return read_status; 698 } 699 700 /* Returns 0 if the end of conversion interrupt occured or -ETIME otherwise */ 701 static int wait_conversion_complete_interrupt(struct ak8975_data *data) 702 { 703 int ret; 704 705 ret = wait_event_timeout(data->data_ready_queue, 706 test_bit(0, &data->flags), 707 AK8975_DATA_READY_TIMEOUT); 708 clear_bit(0, &data->flags); 709 710 return ret > 0 ? 0 : -ETIME; 711 } 712 713 static int ak8975_start_read_axis(struct ak8975_data *data, 714 const struct i2c_client *client) 715 { 716 /* Set up the device for taking a sample. */ 717 int ret = ak8975_set_mode(data, MODE_ONCE); 718 719 if (ret < 0) { 720 dev_err(&client->dev, "Error in setting operating mode\n"); 721 return ret; 722 } 723 724 /* Wait for the conversion to complete. */ 725 if (data->eoc_irq) 726 ret = wait_conversion_complete_interrupt(data); 727 else if (data->eoc_gpiod) 728 ret = wait_conversion_complete_gpio(data); 729 else 730 ret = wait_conversion_complete_polled(data); 731 if (ret < 0) 732 return ret; 733 734 /* Return with zero if the data is ready. */ 735 return !data->def->ctrl_regs[ST1_DRDY]; 736 } 737 738 /* Retrieve raw flux value for one of the x, y, or z axis. */ 739 static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val) 740 { 741 struct ak8975_data *data = iio_priv(indio_dev); 742 const struct i2c_client *client = data->client; 743 const struct ak_def *def = data->def; 744 __le16 rval; 745 u16 buff; 746 int ret; 747 748 pm_runtime_get_sync(&data->client->dev); 749 750 mutex_lock(&data->lock); 751 752 ret = ak8975_start_read_axis(data, client); 753 if (ret) 754 goto exit; 755 756 ret = i2c_smbus_read_i2c_block_data_or_emulated( 757 client, def->data_regs[index], 758 sizeof(rval), (u8*)&rval); 759 if (ret < 0) 760 goto exit; 761 762 /* Read out ST2 for release lock on measurment data. */ 763 ret = i2c_smbus_read_byte_data(client, data->def->ctrl_regs[ST2]); 764 if (ret < 0) { 765 dev_err(&client->dev, "Error in reading ST2\n"); 766 goto exit; 767 } 768 769 if (ret & (data->def->ctrl_masks[ST2_DERR] | 770 data->def->ctrl_masks[ST2_HOFL])) { 771 dev_err(&client->dev, "ST2 status error 0x%x\n", ret); 772 ret = -EINVAL; 773 goto exit; 774 } 775 776 mutex_unlock(&data->lock); 777 778 pm_runtime_mark_last_busy(&data->client->dev); 779 pm_runtime_put_autosuspend(&data->client->dev); 780 781 /* Swap bytes and convert to valid range. */ 782 buff = le16_to_cpu(rval); 783 *val = clamp_t(s16, buff, -def->range, def->range); 784 return IIO_VAL_INT; 785 786 exit: 787 mutex_unlock(&data->lock); 788 dev_err(&client->dev, "Error in reading axis\n"); 789 return ret; 790 } 791 792 static int ak8975_read_raw(struct iio_dev *indio_dev, 793 struct iio_chan_spec const *chan, 794 int *val, int *val2, 795 long mask) 796 { 797 struct ak8975_data *data = iio_priv(indio_dev); 798 799 switch (mask) { 800 case IIO_CHAN_INFO_RAW: 801 return ak8975_read_axis(indio_dev, chan->address, val); 802 case IIO_CHAN_INFO_SCALE: 803 *val = 0; 804 *val2 = data->raw_to_gauss[chan->address]; 805 return IIO_VAL_INT_PLUS_MICRO; 806 } 807 return -EINVAL; 808 } 809 810 static const struct iio_mount_matrix * 811 ak8975_get_mount_matrix(const struct iio_dev *indio_dev, 812 const struct iio_chan_spec *chan) 813 { 814 struct ak8975_data *data = iio_priv(indio_dev); 815 816 return &data->orientation; 817 } 818 819 static const struct iio_chan_spec_ext_info ak8975_ext_info[] = { 820 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, ak8975_get_mount_matrix), 821 { } 822 }; 823 824 #define AK8975_CHANNEL(axis, index) \ 825 { \ 826 .type = IIO_MAGN, \ 827 .modified = 1, \ 828 .channel2 = IIO_MOD_##axis, \ 829 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 830 BIT(IIO_CHAN_INFO_SCALE), \ 831 .address = index, \ 832 .scan_index = index, \ 833 .scan_type = { \ 834 .sign = 's', \ 835 .realbits = 16, \ 836 .storagebits = 16, \ 837 .endianness = IIO_CPU \ 838 }, \ 839 .ext_info = ak8975_ext_info, \ 840 } 841 842 static const struct iio_chan_spec ak8975_channels[] = { 843 AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2), 844 IIO_CHAN_SOFT_TIMESTAMP(3), 845 }; 846 847 static const unsigned long ak8975_scan_masks[] = { 0x7, 0 }; 848 849 static const struct iio_info ak8975_info = { 850 .read_raw = &ak8975_read_raw, 851 }; 852 853 static void ak8975_fill_buffer(struct iio_dev *indio_dev) 854 { 855 struct ak8975_data *data = iio_priv(indio_dev); 856 const struct i2c_client *client = data->client; 857 const struct ak_def *def = data->def; 858 int ret; 859 __le16 fval[3]; 860 861 mutex_lock(&data->lock); 862 863 ret = ak8975_start_read_axis(data, client); 864 if (ret) 865 goto unlock; 866 867 /* 868 * For each axis, read the flux value from the appropriate register 869 * (the register is specified in the iio device attributes). 870 */ 871 ret = i2c_smbus_read_i2c_block_data_or_emulated(client, 872 def->data_regs[0], 873 3 * sizeof(fval[0]), 874 (u8 *)fval); 875 if (ret < 0) 876 goto unlock; 877 878 mutex_unlock(&data->lock); 879 880 /* Clamp to valid range. */ 881 data->scan.channels[0] = clamp_t(s16, le16_to_cpu(fval[0]), -def->range, def->range); 882 data->scan.channels[1] = clamp_t(s16, le16_to_cpu(fval[1]), -def->range, def->range); 883 data->scan.channels[2] = clamp_t(s16, le16_to_cpu(fval[2]), -def->range, def->range); 884 885 iio_push_to_buffers_with_timestamp(indio_dev, &data->scan, 886 iio_get_time_ns(indio_dev)); 887 888 return; 889 890 unlock: 891 mutex_unlock(&data->lock); 892 dev_err(&client->dev, "Error in reading axes block\n"); 893 } 894 895 static irqreturn_t ak8975_handle_trigger(int irq, void *p) 896 { 897 const struct iio_poll_func *pf = p; 898 struct iio_dev *indio_dev = pf->indio_dev; 899 900 ak8975_fill_buffer(indio_dev); 901 iio_trigger_notify_done(indio_dev->trig); 902 return IRQ_HANDLED; 903 } 904 905 static int ak8975_probe(struct i2c_client *client) 906 { 907 const struct i2c_device_id *id = i2c_client_get_device_id(client); 908 struct ak8975_data *data; 909 struct iio_dev *indio_dev; 910 struct gpio_desc *eoc_gpiod; 911 struct gpio_desc *reset_gpiod; 912 int err; 913 const char *name = NULL; 914 915 /* 916 * Grab and set up the supplied GPIO. 917 * We may not have a GPIO based IRQ to scan, that is fine, we will 918 * poll if so. 919 */ 920 eoc_gpiod = devm_gpiod_get_optional(&client->dev, NULL, GPIOD_IN); 921 if (IS_ERR(eoc_gpiod)) 922 return PTR_ERR(eoc_gpiod); 923 if (eoc_gpiod) 924 gpiod_set_consumer_name(eoc_gpiod, "ak_8975"); 925 926 /* 927 * According to AK09911 datasheet, if reset GPIO is provided then 928 * deassert reset on ak8975_power_on() and assert reset on 929 * ak8975_power_off(). 930 */ 931 reset_gpiod = devm_gpiod_get_optional(&client->dev, 932 "reset", GPIOD_OUT_HIGH); 933 if (IS_ERR(reset_gpiod)) 934 return PTR_ERR(reset_gpiod); 935 936 /* Register with IIO */ 937 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 938 if (indio_dev == NULL) 939 return -ENOMEM; 940 941 data = iio_priv(indio_dev); 942 i2c_set_clientdata(client, indio_dev); 943 944 data->client = client; 945 data->eoc_gpiod = eoc_gpiod; 946 data->reset_gpiod = reset_gpiod; 947 data->eoc_irq = 0; 948 949 err = iio_read_mount_matrix(&client->dev, &data->orientation); 950 if (err) 951 return err; 952 953 /* id will be NULL when enumerated via ACPI */ 954 data->def = i2c_get_match_data(client); 955 if (!data->def) 956 return -ENODEV; 957 958 /* If enumerated via firmware node, fix the ABI */ 959 if (dev_fwnode(&client->dev)) 960 name = dev_name(&client->dev); 961 else 962 name = id->name; 963 964 /* Fetch the regulators */ 965 data->vdd = devm_regulator_get(&client->dev, "vdd"); 966 if (IS_ERR(data->vdd)) 967 return PTR_ERR(data->vdd); 968 data->vid = devm_regulator_get(&client->dev, "vid"); 969 if (IS_ERR(data->vid)) 970 return PTR_ERR(data->vid); 971 972 err = ak8975_power_on(data); 973 if (err) 974 return err; 975 976 err = ak8975_who_i_am(client, data->def->type); 977 if (err < 0) { 978 dev_err(&client->dev, "Unexpected device\n"); 979 goto power_off; 980 } 981 dev_dbg(&client->dev, "Asahi compass chip %s\n", name); 982 983 /* Perform some basic start-of-day setup of the device. */ 984 err = ak8975_setup(client); 985 if (err < 0) { 986 dev_err(&client->dev, "%s initialization fails\n", name); 987 goto power_off; 988 } 989 990 mutex_init(&data->lock); 991 indio_dev->channels = ak8975_channels; 992 indio_dev->num_channels = ARRAY_SIZE(ak8975_channels); 993 indio_dev->info = &ak8975_info; 994 indio_dev->available_scan_masks = ak8975_scan_masks; 995 indio_dev->modes = INDIO_DIRECT_MODE; 996 indio_dev->name = name; 997 998 err = iio_triggered_buffer_setup(indio_dev, NULL, ak8975_handle_trigger, 999 NULL); 1000 if (err) { 1001 dev_err(&client->dev, "triggered buffer setup failed\n"); 1002 goto power_off; 1003 } 1004 1005 err = iio_device_register(indio_dev); 1006 if (err) { 1007 dev_err(&client->dev, "device register failed\n"); 1008 goto cleanup_buffer; 1009 } 1010 1011 /* Enable runtime PM */ 1012 pm_runtime_get_noresume(&client->dev); 1013 pm_runtime_set_active(&client->dev); 1014 pm_runtime_enable(&client->dev); 1015 /* 1016 * The device comes online in 500us, so add two orders of magnitude 1017 * of delay before autosuspending: 50 ms. 1018 */ 1019 pm_runtime_set_autosuspend_delay(&client->dev, 50); 1020 pm_runtime_use_autosuspend(&client->dev); 1021 pm_runtime_put(&client->dev); 1022 1023 return 0; 1024 1025 cleanup_buffer: 1026 iio_triggered_buffer_cleanup(indio_dev); 1027 power_off: 1028 ak8975_power_off(data); 1029 return err; 1030 } 1031 1032 static void ak8975_remove(struct i2c_client *client) 1033 { 1034 struct iio_dev *indio_dev = i2c_get_clientdata(client); 1035 struct ak8975_data *data = iio_priv(indio_dev); 1036 1037 pm_runtime_get_sync(&client->dev); 1038 pm_runtime_put_noidle(&client->dev); 1039 pm_runtime_disable(&client->dev); 1040 iio_device_unregister(indio_dev); 1041 iio_triggered_buffer_cleanup(indio_dev); 1042 ak8975_set_mode(data, POWER_DOWN); 1043 ak8975_power_off(data); 1044 } 1045 1046 static int ak8975_runtime_suspend(struct device *dev) 1047 { 1048 struct i2c_client *client = to_i2c_client(dev); 1049 struct iio_dev *indio_dev = i2c_get_clientdata(client); 1050 struct ak8975_data *data = iio_priv(indio_dev); 1051 int ret; 1052 1053 /* Set the device in power down if it wasn't already */ 1054 ret = ak8975_set_mode(data, POWER_DOWN); 1055 if (ret < 0) { 1056 dev_err(&client->dev, "Error in setting power-down mode\n"); 1057 return ret; 1058 } 1059 /* Next cut the regulators */ 1060 ak8975_power_off(data); 1061 1062 return 0; 1063 } 1064 1065 static int ak8975_runtime_resume(struct device *dev) 1066 { 1067 struct i2c_client *client = to_i2c_client(dev); 1068 struct iio_dev *indio_dev = i2c_get_clientdata(client); 1069 struct ak8975_data *data = iio_priv(indio_dev); 1070 int ret; 1071 1072 /* Take up the regulators */ 1073 ak8975_power_on(data); 1074 /* 1075 * We come up in powered down mode, the reading routines will 1076 * put us in the mode to read values later. 1077 */ 1078 ret = ak8975_set_mode(data, POWER_DOWN); 1079 if (ret < 0) { 1080 dev_err(&client->dev, "Error in setting power-down mode\n"); 1081 return ret; 1082 } 1083 1084 return 0; 1085 } 1086 1087 static DEFINE_RUNTIME_DEV_PM_OPS(ak8975_dev_pm_ops, ak8975_runtime_suspend, 1088 ak8975_runtime_resume, NULL); 1089 1090 static const struct acpi_device_id ak_acpi_match[] = { 1091 {"AK8963", (kernel_ulong_t)&ak_def_array[AK8963] }, 1092 {"AK8975", (kernel_ulong_t)&ak_def_array[AK8975] }, 1093 {"AK009911", (kernel_ulong_t)&ak_def_array[AK09911] }, 1094 {"AK09911", (kernel_ulong_t)&ak_def_array[AK09911] }, 1095 {"AK09912", (kernel_ulong_t)&ak_def_array[AK09912] }, 1096 {"AKM9911", (kernel_ulong_t)&ak_def_array[AK09911] }, 1097 {"INVN6500", (kernel_ulong_t)&ak_def_array[AK8963] }, 1098 { } 1099 }; 1100 MODULE_DEVICE_TABLE(acpi, ak_acpi_match); 1101 1102 static const struct i2c_device_id ak8975_id[] = { 1103 {"AK8963", (kernel_ulong_t)&ak_def_array[AK8963] }, 1104 {"ak8963", (kernel_ulong_t)&ak_def_array[AK8963] }, 1105 {"ak8975", (kernel_ulong_t)&ak_def_array[AK8975] }, 1106 {"ak09911", (kernel_ulong_t)&ak_def_array[AK09911] }, 1107 {"ak09912", (kernel_ulong_t)&ak_def_array[AK09912] }, 1108 {"ak09916", (kernel_ulong_t)&ak_def_array[AK09916] }, 1109 {"ak09918", (kernel_ulong_t)&ak_def_array[AK09918] }, 1110 {} 1111 }; 1112 MODULE_DEVICE_TABLE(i2c, ak8975_id); 1113 1114 static const struct of_device_id ak8975_of_match[] = { 1115 { .compatible = "asahi-kasei,ak8975", .data = &ak_def_array[AK8975] }, 1116 { .compatible = "ak8975", .data = &ak_def_array[AK8975] }, 1117 { .compatible = "asahi-kasei,ak8963", .data = &ak_def_array[AK8963] }, 1118 { .compatible = "ak8963", .data = &ak_def_array[AK8963] }, 1119 { .compatible = "asahi-kasei,ak09911", .data = &ak_def_array[AK09911] }, 1120 { .compatible = "ak09911", .data = &ak_def_array[AK09911] }, 1121 { .compatible = "asahi-kasei,ak09912", .data = &ak_def_array[AK09912] }, 1122 { .compatible = "ak09912", .data = &ak_def_array[AK09912] }, 1123 { .compatible = "asahi-kasei,ak09916", .data = &ak_def_array[AK09916] }, 1124 { .compatible = "asahi-kasei,ak09918", .data = &ak_def_array[AK09918] }, 1125 {} 1126 }; 1127 MODULE_DEVICE_TABLE(of, ak8975_of_match); 1128 1129 static struct i2c_driver ak8975_driver = { 1130 .driver = { 1131 .name = "ak8975", 1132 .pm = pm_ptr(&ak8975_dev_pm_ops), 1133 .of_match_table = ak8975_of_match, 1134 .acpi_match_table = ak_acpi_match, 1135 }, 1136 .probe = ak8975_probe, 1137 .remove = ak8975_remove, 1138 .id_table = ak8975_id, 1139 }; 1140 module_i2c_driver(ak8975_driver); 1141 1142 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>"); 1143 MODULE_DESCRIPTION("AK8975 magnetometer driver"); 1144 MODULE_LICENSE("GPL"); 1145