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