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 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 static const struct acpi_device_id ak_acpi_match[] = { 783 {"AK8975", AK8975}, 784 {"AK8963", AK8963}, 785 {"INVN6500", AK8963}, 786 {"AK009911", AK09911}, 787 {"AK09911", AK09911}, 788 {"AKM9911", AK09911}, 789 {"AK09912", AK09912}, 790 { } 791 }; 792 MODULE_DEVICE_TABLE(acpi, ak_acpi_match); 793 794 static void ak8975_fill_buffer(struct iio_dev *indio_dev) 795 { 796 struct ak8975_data *data = iio_priv(indio_dev); 797 const struct i2c_client *client = data->client; 798 const struct ak_def *def = data->def; 799 int ret; 800 __le16 fval[3]; 801 802 mutex_lock(&data->lock); 803 804 ret = ak8975_start_read_axis(data, client); 805 if (ret) 806 goto unlock; 807 808 /* 809 * For each axis, read the flux value from the appropriate register 810 * (the register is specified in the iio device attributes). 811 */ 812 ret = i2c_smbus_read_i2c_block_data_or_emulated(client, 813 def->data_regs[0], 814 3 * sizeof(fval[0]), 815 (u8 *)fval); 816 if (ret < 0) 817 goto unlock; 818 819 mutex_unlock(&data->lock); 820 821 /* Clamp to valid range. */ 822 data->scan.channels[0] = clamp_t(s16, le16_to_cpu(fval[0]), -def->range, def->range); 823 data->scan.channels[1] = clamp_t(s16, le16_to_cpu(fval[1]), -def->range, def->range); 824 data->scan.channels[2] = clamp_t(s16, le16_to_cpu(fval[2]), -def->range, def->range); 825 826 iio_push_to_buffers_with_timestamp(indio_dev, &data->scan, 827 iio_get_time_ns(indio_dev)); 828 829 return; 830 831 unlock: 832 mutex_unlock(&data->lock); 833 dev_err(&client->dev, "Error in reading axes block\n"); 834 } 835 836 static irqreturn_t ak8975_handle_trigger(int irq, void *p) 837 { 838 const struct iio_poll_func *pf = p; 839 struct iio_dev *indio_dev = pf->indio_dev; 840 841 ak8975_fill_buffer(indio_dev); 842 iio_trigger_notify_done(indio_dev->trig); 843 return IRQ_HANDLED; 844 } 845 846 static int ak8975_probe(struct i2c_client *client, 847 const struct i2c_device_id *id) 848 { 849 struct ak8975_data *data; 850 struct iio_dev *indio_dev; 851 struct gpio_desc *eoc_gpiod; 852 struct gpio_desc *reset_gpiod; 853 const void *match; 854 unsigned int i; 855 int err; 856 enum asahi_compass_chipset chipset; 857 const char *name = NULL; 858 859 /* 860 * Grab and set up the supplied GPIO. 861 * We may not have a GPIO based IRQ to scan, that is fine, we will 862 * poll if so. 863 */ 864 eoc_gpiod = devm_gpiod_get_optional(&client->dev, NULL, GPIOD_IN); 865 if (IS_ERR(eoc_gpiod)) 866 return PTR_ERR(eoc_gpiod); 867 if (eoc_gpiod) 868 gpiod_set_consumer_name(eoc_gpiod, "ak_8975"); 869 870 /* 871 * According to AK09911 datasheet, if reset GPIO is provided then 872 * deassert reset on ak8975_power_on() and assert reset on 873 * ak8975_power_off(). 874 */ 875 reset_gpiod = devm_gpiod_get_optional(&client->dev, 876 "reset", GPIOD_OUT_HIGH); 877 if (IS_ERR(reset_gpiod)) 878 return PTR_ERR(reset_gpiod); 879 880 /* Register with IIO */ 881 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 882 if (indio_dev == NULL) 883 return -ENOMEM; 884 885 data = iio_priv(indio_dev); 886 i2c_set_clientdata(client, indio_dev); 887 888 data->client = client; 889 data->eoc_gpiod = eoc_gpiod; 890 data->reset_gpiod = reset_gpiod; 891 data->eoc_irq = 0; 892 893 err = iio_read_mount_matrix(&client->dev, "mount-matrix", &data->orientation); 894 if (err) 895 return err; 896 897 /* id will be NULL when enumerated via ACPI */ 898 match = device_get_match_data(&client->dev); 899 if (match) { 900 chipset = (enum asahi_compass_chipset)(match); 901 name = dev_name(&client->dev); 902 } else if (id) { 903 chipset = (enum asahi_compass_chipset)(id->driver_data); 904 name = id->name; 905 } else 906 return -ENOSYS; 907 908 for (i = 0; i < ARRAY_SIZE(ak_def_array); i++) 909 if (ak_def_array[i].type == chipset) 910 break; 911 912 if (i == ARRAY_SIZE(ak_def_array)) { 913 dev_err(&client->dev, "AKM device type unsupported: %d\n", 914 chipset); 915 return -ENODEV; 916 } 917 918 data->def = &ak_def_array[i]; 919 920 /* Fetch the regulators */ 921 data->vdd = devm_regulator_get(&client->dev, "vdd"); 922 if (IS_ERR(data->vdd)) 923 return PTR_ERR(data->vdd); 924 data->vid = devm_regulator_get(&client->dev, "vid"); 925 if (IS_ERR(data->vid)) 926 return PTR_ERR(data->vid); 927 928 err = ak8975_power_on(data); 929 if (err) 930 return err; 931 932 err = ak8975_who_i_am(client, data->def->type); 933 if (err < 0) { 934 dev_err(&client->dev, "Unexpected device\n"); 935 goto power_off; 936 } 937 dev_dbg(&client->dev, "Asahi compass chip %s\n", name); 938 939 /* Perform some basic start-of-day setup of the device. */ 940 err = ak8975_setup(client); 941 if (err < 0) { 942 dev_err(&client->dev, "%s initialization fails\n", name); 943 goto power_off; 944 } 945 946 mutex_init(&data->lock); 947 indio_dev->channels = ak8975_channels; 948 indio_dev->num_channels = ARRAY_SIZE(ak8975_channels); 949 indio_dev->info = &ak8975_info; 950 indio_dev->available_scan_masks = ak8975_scan_masks; 951 indio_dev->modes = INDIO_DIRECT_MODE; 952 indio_dev->name = name; 953 954 err = iio_triggered_buffer_setup(indio_dev, NULL, ak8975_handle_trigger, 955 NULL); 956 if (err) { 957 dev_err(&client->dev, "triggered buffer setup failed\n"); 958 goto power_off; 959 } 960 961 err = iio_device_register(indio_dev); 962 if (err) { 963 dev_err(&client->dev, "device register failed\n"); 964 goto cleanup_buffer; 965 } 966 967 /* Enable runtime PM */ 968 pm_runtime_get_noresume(&client->dev); 969 pm_runtime_set_active(&client->dev); 970 pm_runtime_enable(&client->dev); 971 /* 972 * The device comes online in 500us, so add two orders of magnitude 973 * of delay before autosuspending: 50 ms. 974 */ 975 pm_runtime_set_autosuspend_delay(&client->dev, 50); 976 pm_runtime_use_autosuspend(&client->dev); 977 pm_runtime_put(&client->dev); 978 979 return 0; 980 981 cleanup_buffer: 982 iio_triggered_buffer_cleanup(indio_dev); 983 power_off: 984 ak8975_power_off(data); 985 return err; 986 } 987 988 static int ak8975_remove(struct i2c_client *client) 989 { 990 struct iio_dev *indio_dev = i2c_get_clientdata(client); 991 struct ak8975_data *data = iio_priv(indio_dev); 992 993 pm_runtime_get_sync(&client->dev); 994 pm_runtime_put_noidle(&client->dev); 995 pm_runtime_disable(&client->dev); 996 iio_device_unregister(indio_dev); 997 iio_triggered_buffer_cleanup(indio_dev); 998 ak8975_set_mode(data, POWER_DOWN); 999 ak8975_power_off(data); 1000 1001 return 0; 1002 } 1003 1004 #ifdef CONFIG_PM 1005 static int ak8975_runtime_suspend(struct device *dev) 1006 { 1007 struct i2c_client *client = to_i2c_client(dev); 1008 struct iio_dev *indio_dev = i2c_get_clientdata(client); 1009 struct ak8975_data *data = iio_priv(indio_dev); 1010 int ret; 1011 1012 /* Set the device in power down if it wasn't already */ 1013 ret = ak8975_set_mode(data, POWER_DOWN); 1014 if (ret < 0) { 1015 dev_err(&client->dev, "Error in setting power-down mode\n"); 1016 return ret; 1017 } 1018 /* Next cut the regulators */ 1019 ak8975_power_off(data); 1020 1021 return 0; 1022 } 1023 1024 static int ak8975_runtime_resume(struct device *dev) 1025 { 1026 struct i2c_client *client = to_i2c_client(dev); 1027 struct iio_dev *indio_dev = i2c_get_clientdata(client); 1028 struct ak8975_data *data = iio_priv(indio_dev); 1029 int ret; 1030 1031 /* Take up the regulators */ 1032 ak8975_power_on(data); 1033 /* 1034 * We come up in powered down mode, the reading routines will 1035 * put us in the mode to read values later. 1036 */ 1037 ret = ak8975_set_mode(data, POWER_DOWN); 1038 if (ret < 0) { 1039 dev_err(&client->dev, "Error in setting power-down mode\n"); 1040 return ret; 1041 } 1042 1043 return 0; 1044 } 1045 #endif /* CONFIG_PM */ 1046 1047 static const struct dev_pm_ops ak8975_dev_pm_ops = { 1048 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, 1049 pm_runtime_force_resume) 1050 SET_RUNTIME_PM_OPS(ak8975_runtime_suspend, 1051 ak8975_runtime_resume, NULL) 1052 }; 1053 1054 static const struct i2c_device_id ak8975_id[] = { 1055 {"ak8975", AK8975}, 1056 {"ak8963", AK8963}, 1057 {"AK8963", AK8963}, 1058 {"ak09911", AK09911}, 1059 {"ak09912", AK09912}, 1060 {} 1061 }; 1062 1063 MODULE_DEVICE_TABLE(i2c, ak8975_id); 1064 1065 static const struct of_device_id ak8975_of_match[] = { 1066 { .compatible = "asahi-kasei,ak8975", }, 1067 { .compatible = "ak8975", }, 1068 { .compatible = "asahi-kasei,ak8963", }, 1069 { .compatible = "ak8963", }, 1070 { .compatible = "asahi-kasei,ak09911", }, 1071 { .compatible = "ak09911", }, 1072 { .compatible = "asahi-kasei,ak09912", }, 1073 { .compatible = "ak09912", }, 1074 {} 1075 }; 1076 MODULE_DEVICE_TABLE(of, ak8975_of_match); 1077 1078 static struct i2c_driver ak8975_driver = { 1079 .driver = { 1080 .name = "ak8975", 1081 .pm = &ak8975_dev_pm_ops, 1082 .of_match_table = ak8975_of_match, 1083 .acpi_match_table = ak_acpi_match, 1084 }, 1085 .probe = ak8975_probe, 1086 .remove = ak8975_remove, 1087 .id_table = ak8975_id, 1088 }; 1089 module_i2c_driver(ak8975_driver); 1090 1091 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>"); 1092 MODULE_DESCRIPTION("AK8975 magnetometer driver"); 1093 MODULE_LICENSE("GPL"); 1094