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 371 /* Enable attached power regulator if any. */ 372 static int ak8975_power_on(const struct ak8975_data *data) 373 { 374 int ret; 375 376 ret = regulator_enable(data->vdd); 377 if (ret) { 378 dev_warn(&data->client->dev, 379 "Failed to enable specified Vdd supply\n"); 380 return ret; 381 } 382 ret = regulator_enable(data->vid); 383 if (ret) { 384 dev_warn(&data->client->dev, 385 "Failed to enable specified Vid supply\n"); 386 return ret; 387 } 388 389 gpiod_set_value_cansleep(data->reset_gpiod, 0); 390 391 /* 392 * According to the datasheet the power supply rise time is 200us 393 * and the minimum wait time before mode setting is 100us, in 394 * total 300us. Add some margin and say minimum 500us here. 395 */ 396 usleep_range(500, 1000); 397 return 0; 398 } 399 400 /* Disable attached power regulator if any. */ 401 static void ak8975_power_off(const struct ak8975_data *data) 402 { 403 gpiod_set_value_cansleep(data->reset_gpiod, 1); 404 405 regulator_disable(data->vid); 406 regulator_disable(data->vdd); 407 } 408 409 /* 410 * Return 0 if the i2c device is the one we expect. 411 * return a negative error number otherwise 412 */ 413 static int ak8975_who_i_am(struct i2c_client *client, 414 enum asahi_compass_chipset type) 415 { 416 u8 wia_val[2]; 417 int ret; 418 419 /* 420 * Signature for each device: 421 * Device | WIA1 | WIA2 422 * AK09912 | DEVICE_ID | AK09912_DEVICE_ID 423 * AK09911 | DEVICE_ID | AK09911_DEVICE_ID 424 * AK8975 | DEVICE_ID | NA 425 * AK8963 | DEVICE_ID | NA 426 */ 427 ret = i2c_smbus_read_i2c_block_data_or_emulated( 428 client, AK09912_REG_WIA1, 2, wia_val); 429 if (ret < 0) { 430 dev_err(&client->dev, "Error reading WIA\n"); 431 return ret; 432 } 433 434 if (wia_val[0] != AK8975_DEVICE_ID) 435 return -ENODEV; 436 437 switch (type) { 438 case AK8975: 439 case AK8963: 440 return 0; 441 case AK09911: 442 if (wia_val[1] == AK09911_DEVICE_ID) 443 return 0; 444 break; 445 case AK09912: 446 if (wia_val[1] == AK09912_DEVICE_ID) 447 return 0; 448 break; 449 default: 450 dev_err(&client->dev, "Type %d unknown\n", type); 451 } 452 return -ENODEV; 453 } 454 455 /* 456 * Helper function to write to CNTL register. 457 */ 458 static int ak8975_set_mode(struct ak8975_data *data, enum ak_ctrl_mode mode) 459 { 460 u8 regval; 461 int ret; 462 463 regval = (data->cntl_cache & ~data->def->ctrl_masks[CNTL_MODE]) | 464 data->def->ctrl_modes[mode]; 465 ret = i2c_smbus_write_byte_data(data->client, 466 data->def->ctrl_regs[CNTL], regval); 467 if (ret < 0) { 468 return ret; 469 } 470 data->cntl_cache = regval; 471 /* After mode change wait atleast 100us */ 472 usleep_range(100, 500); 473 474 return 0; 475 } 476 477 /* 478 * Handle data ready irq 479 */ 480 static irqreturn_t ak8975_irq_handler(int irq, void *data) 481 { 482 struct ak8975_data *ak8975 = data; 483 484 set_bit(0, &ak8975->flags); 485 wake_up(&ak8975->data_ready_queue); 486 487 return IRQ_HANDLED; 488 } 489 490 /* 491 * Install data ready interrupt handler 492 */ 493 static int ak8975_setup_irq(struct ak8975_data *data) 494 { 495 struct i2c_client *client = data->client; 496 int rc; 497 int irq; 498 499 init_waitqueue_head(&data->data_ready_queue); 500 clear_bit(0, &data->flags); 501 if (client->irq) 502 irq = client->irq; 503 else 504 irq = gpiod_to_irq(data->eoc_gpiod); 505 506 rc = devm_request_irq(&client->dev, irq, ak8975_irq_handler, 507 IRQF_TRIGGER_RISING | IRQF_ONESHOT, 508 dev_name(&client->dev), data); 509 if (rc < 0) { 510 dev_err(&client->dev, "irq %d request failed: %d\n", irq, rc); 511 return rc; 512 } 513 514 data->eoc_irq = irq; 515 516 return rc; 517 } 518 519 520 /* 521 * Perform some start-of-day setup, including reading the asa calibration 522 * values and caching them. 523 */ 524 static int ak8975_setup(struct i2c_client *client) 525 { 526 struct iio_dev *indio_dev = i2c_get_clientdata(client); 527 struct ak8975_data *data = iio_priv(indio_dev); 528 int ret; 529 530 /* Write the fused rom access mode. */ 531 ret = ak8975_set_mode(data, FUSE_ROM); 532 if (ret < 0) { 533 dev_err(&client->dev, "Error in setting fuse access mode\n"); 534 return ret; 535 } 536 537 /* Get asa data and store in the device data. */ 538 ret = i2c_smbus_read_i2c_block_data_or_emulated( 539 client, data->def->ctrl_regs[ASA_BASE], 540 3, data->asa); 541 if (ret < 0) { 542 dev_err(&client->dev, "Not able to read asa data\n"); 543 return ret; 544 } 545 546 /* After reading fuse ROM data set power-down mode */ 547 ret = ak8975_set_mode(data, POWER_DOWN); 548 if (ret < 0) { 549 dev_err(&client->dev, "Error in setting power-down mode\n"); 550 return ret; 551 } 552 553 if (data->eoc_gpiod || client->irq > 0) { 554 ret = ak8975_setup_irq(data); 555 if (ret < 0) { 556 dev_err(&client->dev, 557 "Error setting data ready interrupt\n"); 558 return ret; 559 } 560 } 561 562 data->raw_to_gauss[0] = data->def->raw_to_gauss(data->asa[0]); 563 data->raw_to_gauss[1] = data->def->raw_to_gauss(data->asa[1]); 564 data->raw_to_gauss[2] = data->def->raw_to_gauss(data->asa[2]); 565 566 return 0; 567 } 568 569 static int wait_conversion_complete_gpio(struct ak8975_data *data) 570 { 571 struct i2c_client *client = data->client; 572 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT; 573 int ret; 574 575 /* Wait for the conversion to complete. */ 576 while (timeout_ms) { 577 msleep(AK8975_CONVERSION_DONE_POLL_TIME); 578 if (gpiod_get_value(data->eoc_gpiod)) 579 break; 580 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME; 581 } 582 if (!timeout_ms) { 583 dev_err(&client->dev, "Conversion timeout happened\n"); 584 return -EINVAL; 585 } 586 587 ret = i2c_smbus_read_byte_data(client, data->def->ctrl_regs[ST1]); 588 if (ret < 0) 589 dev_err(&client->dev, "Error in reading ST1\n"); 590 591 return ret; 592 } 593 594 static int wait_conversion_complete_polled(struct ak8975_data *data) 595 { 596 struct i2c_client *client = data->client; 597 u8 read_status; 598 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT; 599 int ret; 600 601 /* Wait for the conversion to complete. */ 602 while (timeout_ms) { 603 msleep(AK8975_CONVERSION_DONE_POLL_TIME); 604 ret = i2c_smbus_read_byte_data(client, 605 data->def->ctrl_regs[ST1]); 606 if (ret < 0) { 607 dev_err(&client->dev, "Error in reading ST1\n"); 608 return ret; 609 } 610 read_status = ret; 611 if (read_status) 612 break; 613 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME; 614 } 615 if (!timeout_ms) { 616 dev_err(&client->dev, "Conversion timeout happened\n"); 617 return -EINVAL; 618 } 619 620 return read_status; 621 } 622 623 /* Returns 0 if the end of conversion interrupt occured or -ETIME otherwise */ 624 static int wait_conversion_complete_interrupt(struct ak8975_data *data) 625 { 626 int ret; 627 628 ret = wait_event_timeout(data->data_ready_queue, 629 test_bit(0, &data->flags), 630 AK8975_DATA_READY_TIMEOUT); 631 clear_bit(0, &data->flags); 632 633 return ret > 0 ? 0 : -ETIME; 634 } 635 636 static int ak8975_start_read_axis(struct ak8975_data *data, 637 const struct i2c_client *client) 638 { 639 /* Set up the device for taking a sample. */ 640 int ret = ak8975_set_mode(data, MODE_ONCE); 641 642 if (ret < 0) { 643 dev_err(&client->dev, "Error in setting operating mode\n"); 644 return ret; 645 } 646 647 /* Wait for the conversion to complete. */ 648 if (data->eoc_irq) 649 ret = wait_conversion_complete_interrupt(data); 650 else if (data->eoc_gpiod) 651 ret = wait_conversion_complete_gpio(data); 652 else 653 ret = wait_conversion_complete_polled(data); 654 if (ret < 0) 655 return ret; 656 657 /* This will be executed only for non-interrupt based waiting case */ 658 if (ret & data->def->ctrl_masks[ST1_DRDY]) { 659 ret = i2c_smbus_read_byte_data(client, 660 data->def->ctrl_regs[ST2]); 661 if (ret < 0) { 662 dev_err(&client->dev, "Error in reading ST2\n"); 663 return ret; 664 } 665 if (ret & (data->def->ctrl_masks[ST2_DERR] | 666 data->def->ctrl_masks[ST2_HOFL])) { 667 dev_err(&client->dev, "ST2 status error 0x%x\n", ret); 668 return -EINVAL; 669 } 670 } 671 672 return 0; 673 } 674 675 /* Retrieve raw flux value for one of the x, y, or z axis. */ 676 static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val) 677 { 678 struct ak8975_data *data = iio_priv(indio_dev); 679 const struct i2c_client *client = data->client; 680 const struct ak_def *def = data->def; 681 __le16 rval; 682 u16 buff; 683 int ret; 684 685 pm_runtime_get_sync(&data->client->dev); 686 687 mutex_lock(&data->lock); 688 689 ret = ak8975_start_read_axis(data, client); 690 if (ret) 691 goto exit; 692 693 ret = i2c_smbus_read_i2c_block_data_or_emulated( 694 client, def->data_regs[index], 695 sizeof(rval), (u8*)&rval); 696 if (ret < 0) 697 goto exit; 698 699 mutex_unlock(&data->lock); 700 701 pm_runtime_mark_last_busy(&data->client->dev); 702 pm_runtime_put_autosuspend(&data->client->dev); 703 704 /* Swap bytes and convert to valid range. */ 705 buff = le16_to_cpu(rval); 706 *val = clamp_t(s16, buff, -def->range, def->range); 707 return IIO_VAL_INT; 708 709 exit: 710 mutex_unlock(&data->lock); 711 dev_err(&client->dev, "Error in reading axis\n"); 712 return ret; 713 } 714 715 static int ak8975_read_raw(struct iio_dev *indio_dev, 716 struct iio_chan_spec const *chan, 717 int *val, int *val2, 718 long mask) 719 { 720 struct ak8975_data *data = iio_priv(indio_dev); 721 722 switch (mask) { 723 case IIO_CHAN_INFO_RAW: 724 return ak8975_read_axis(indio_dev, chan->address, val); 725 case IIO_CHAN_INFO_SCALE: 726 *val = 0; 727 *val2 = data->raw_to_gauss[chan->address]; 728 return IIO_VAL_INT_PLUS_MICRO; 729 } 730 return -EINVAL; 731 } 732 733 static const struct iio_mount_matrix * 734 ak8975_get_mount_matrix(const struct iio_dev *indio_dev, 735 const struct iio_chan_spec *chan) 736 { 737 struct ak8975_data *data = iio_priv(indio_dev); 738 739 return &data->orientation; 740 } 741 742 static const struct iio_chan_spec_ext_info ak8975_ext_info[] = { 743 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, ak8975_get_mount_matrix), 744 { } 745 }; 746 747 #define AK8975_CHANNEL(axis, index) \ 748 { \ 749 .type = IIO_MAGN, \ 750 .modified = 1, \ 751 .channel2 = IIO_MOD_##axis, \ 752 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 753 BIT(IIO_CHAN_INFO_SCALE), \ 754 .address = index, \ 755 .scan_index = index, \ 756 .scan_type = { \ 757 .sign = 's', \ 758 .realbits = 16, \ 759 .storagebits = 16, \ 760 .endianness = IIO_CPU \ 761 }, \ 762 .ext_info = ak8975_ext_info, \ 763 } 764 765 static const struct iio_chan_spec ak8975_channels[] = { 766 AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2), 767 IIO_CHAN_SOFT_TIMESTAMP(3), 768 }; 769 770 static const unsigned long ak8975_scan_masks[] = { 0x7, 0 }; 771 772 static const struct iio_info ak8975_info = { 773 .read_raw = &ak8975_read_raw, 774 }; 775 776 #ifdef CONFIG_ACPI 777 static const struct acpi_device_id ak_acpi_match[] = { 778 {"AK8975", AK8975}, 779 {"AK8963", AK8963}, 780 {"INVN6500", AK8963}, 781 {"AK009911", AK09911}, 782 {"AK09911", AK09911}, 783 {"AKM9911", AK09911}, 784 {"AK09912", AK09912}, 785 { } 786 }; 787 MODULE_DEVICE_TABLE(acpi, ak_acpi_match); 788 #endif 789 790 static void ak8975_fill_buffer(struct iio_dev *indio_dev) 791 { 792 struct ak8975_data *data = iio_priv(indio_dev); 793 const struct i2c_client *client = data->client; 794 const struct ak_def *def = data->def; 795 int ret; 796 s16 buff[8]; /* 3 x 16 bits axis values + 1 aligned 64 bits timestamp */ 797 __le16 fval[3]; 798 799 mutex_lock(&data->lock); 800 801 ret = ak8975_start_read_axis(data, client); 802 if (ret) 803 goto unlock; 804 805 /* 806 * For each axis, read the flux value from the appropriate register 807 * (the register is specified in the iio device attributes). 808 */ 809 ret = i2c_smbus_read_i2c_block_data_or_emulated(client, 810 def->data_regs[0], 811 3 * sizeof(fval[0]), 812 (u8 *)fval); 813 if (ret < 0) 814 goto unlock; 815 816 mutex_unlock(&data->lock); 817 818 /* Clamp to valid range. */ 819 buff[0] = clamp_t(s16, le16_to_cpu(fval[0]), -def->range, def->range); 820 buff[1] = clamp_t(s16, le16_to_cpu(fval[1]), -def->range, def->range); 821 buff[2] = clamp_t(s16, le16_to_cpu(fval[2]), -def->range, def->range); 822 823 iio_push_to_buffers_with_timestamp(indio_dev, buff, 824 iio_get_time_ns(indio_dev)); 825 return; 826 827 unlock: 828 mutex_unlock(&data->lock); 829 dev_err(&client->dev, "Error in reading axes block\n"); 830 } 831 832 static irqreturn_t ak8975_handle_trigger(int irq, void *p) 833 { 834 const struct iio_poll_func *pf = p; 835 struct iio_dev *indio_dev = pf->indio_dev; 836 837 ak8975_fill_buffer(indio_dev); 838 iio_trigger_notify_done(indio_dev->trig); 839 return IRQ_HANDLED; 840 } 841 842 static int ak8975_probe(struct i2c_client *client, 843 const struct i2c_device_id *id) 844 { 845 struct ak8975_data *data; 846 struct iio_dev *indio_dev; 847 struct gpio_desc *eoc_gpiod; 848 struct gpio_desc *reset_gpiod; 849 const void *match; 850 unsigned int i; 851 int err; 852 enum asahi_compass_chipset chipset; 853 const char *name = NULL; 854 855 /* 856 * Grab and set up the supplied GPIO. 857 * We may not have a GPIO based IRQ to scan, that is fine, we will 858 * poll if so. 859 */ 860 eoc_gpiod = devm_gpiod_get_optional(&client->dev, NULL, GPIOD_IN); 861 if (IS_ERR(eoc_gpiod)) 862 return PTR_ERR(eoc_gpiod); 863 if (eoc_gpiod) 864 gpiod_set_consumer_name(eoc_gpiod, "ak_8975"); 865 866 /* 867 * According to AK09911 datasheet, if reset GPIO is provided then 868 * deassert reset on ak8975_power_on() and assert reset on 869 * ak8975_power_off(). 870 */ 871 reset_gpiod = devm_gpiod_get_optional(&client->dev, 872 "reset", GPIOD_OUT_HIGH); 873 if (IS_ERR(reset_gpiod)) 874 return PTR_ERR(reset_gpiod); 875 876 /* Register with IIO */ 877 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 878 if (indio_dev == NULL) 879 return -ENOMEM; 880 881 data = iio_priv(indio_dev); 882 i2c_set_clientdata(client, indio_dev); 883 884 data->client = client; 885 data->eoc_gpiod = eoc_gpiod; 886 data->reset_gpiod = reset_gpiod; 887 data->eoc_irq = 0; 888 889 err = iio_read_mount_matrix(&client->dev, "mount-matrix", &data->orientation); 890 if (err) 891 return err; 892 893 /* id will be NULL when enumerated via ACPI */ 894 match = device_get_match_data(&client->dev); 895 if (match) { 896 chipset = (enum asahi_compass_chipset)(match); 897 name = dev_name(&client->dev); 898 } else if (id) { 899 chipset = (enum asahi_compass_chipset)(id->driver_data); 900 name = id->name; 901 } else 902 return -ENOSYS; 903 904 for (i = 0; i < ARRAY_SIZE(ak_def_array); i++) 905 if (ak_def_array[i].type == chipset) 906 break; 907 908 if (i == ARRAY_SIZE(ak_def_array)) { 909 dev_err(&client->dev, "AKM device type unsupported: %d\n", 910 chipset); 911 return -ENODEV; 912 } 913 914 data->def = &ak_def_array[i]; 915 916 /* Fetch the regulators */ 917 data->vdd = devm_regulator_get(&client->dev, "vdd"); 918 if (IS_ERR(data->vdd)) 919 return PTR_ERR(data->vdd); 920 data->vid = devm_regulator_get(&client->dev, "vid"); 921 if (IS_ERR(data->vid)) 922 return PTR_ERR(data->vid); 923 924 err = ak8975_power_on(data); 925 if (err) 926 return err; 927 928 err = ak8975_who_i_am(client, data->def->type); 929 if (err < 0) { 930 dev_err(&client->dev, "Unexpected device\n"); 931 goto power_off; 932 } 933 dev_dbg(&client->dev, "Asahi compass chip %s\n", name); 934 935 /* Perform some basic start-of-day setup of the device. */ 936 err = ak8975_setup(client); 937 if (err < 0) { 938 dev_err(&client->dev, "%s initialization fails\n", name); 939 goto power_off; 940 } 941 942 mutex_init(&data->lock); 943 indio_dev->channels = ak8975_channels; 944 indio_dev->num_channels = ARRAY_SIZE(ak8975_channels); 945 indio_dev->info = &ak8975_info; 946 indio_dev->available_scan_masks = ak8975_scan_masks; 947 indio_dev->modes = INDIO_DIRECT_MODE; 948 indio_dev->name = name; 949 950 err = iio_triggered_buffer_setup(indio_dev, NULL, ak8975_handle_trigger, 951 NULL); 952 if (err) { 953 dev_err(&client->dev, "triggered buffer setup failed\n"); 954 goto power_off; 955 } 956 957 err = iio_device_register(indio_dev); 958 if (err) { 959 dev_err(&client->dev, "device register failed\n"); 960 goto cleanup_buffer; 961 } 962 963 /* Enable runtime PM */ 964 pm_runtime_get_noresume(&client->dev); 965 pm_runtime_set_active(&client->dev); 966 pm_runtime_enable(&client->dev); 967 /* 968 * The device comes online in 500us, so add two orders of magnitude 969 * of delay before autosuspending: 50 ms. 970 */ 971 pm_runtime_set_autosuspend_delay(&client->dev, 50); 972 pm_runtime_use_autosuspend(&client->dev); 973 pm_runtime_put(&client->dev); 974 975 return 0; 976 977 cleanup_buffer: 978 iio_triggered_buffer_cleanup(indio_dev); 979 power_off: 980 ak8975_power_off(data); 981 return err; 982 } 983 984 static int ak8975_remove(struct i2c_client *client) 985 { 986 struct iio_dev *indio_dev = i2c_get_clientdata(client); 987 struct ak8975_data *data = iio_priv(indio_dev); 988 989 pm_runtime_get_sync(&client->dev); 990 pm_runtime_put_noidle(&client->dev); 991 pm_runtime_disable(&client->dev); 992 iio_device_unregister(indio_dev); 993 iio_triggered_buffer_cleanup(indio_dev); 994 ak8975_set_mode(data, POWER_DOWN); 995 ak8975_power_off(data); 996 997 return 0; 998 } 999 1000 #ifdef CONFIG_PM 1001 static int ak8975_runtime_suspend(struct device *dev) 1002 { 1003 struct i2c_client *client = to_i2c_client(dev); 1004 struct iio_dev *indio_dev = i2c_get_clientdata(client); 1005 struct ak8975_data *data = iio_priv(indio_dev); 1006 int ret; 1007 1008 /* Set the device in power down if it wasn't already */ 1009 ret = ak8975_set_mode(data, POWER_DOWN); 1010 if (ret < 0) { 1011 dev_err(&client->dev, "Error in setting power-down mode\n"); 1012 return ret; 1013 } 1014 /* Next cut the regulators */ 1015 ak8975_power_off(data); 1016 1017 return 0; 1018 } 1019 1020 static int ak8975_runtime_resume(struct device *dev) 1021 { 1022 struct i2c_client *client = to_i2c_client(dev); 1023 struct iio_dev *indio_dev = i2c_get_clientdata(client); 1024 struct ak8975_data *data = iio_priv(indio_dev); 1025 int ret; 1026 1027 /* Take up the regulators */ 1028 ak8975_power_on(data); 1029 /* 1030 * We come up in powered down mode, the reading routines will 1031 * put us in the mode to read values later. 1032 */ 1033 ret = ak8975_set_mode(data, POWER_DOWN); 1034 if (ret < 0) { 1035 dev_err(&client->dev, "Error in setting power-down mode\n"); 1036 return ret; 1037 } 1038 1039 return 0; 1040 } 1041 #endif /* CONFIG_PM */ 1042 1043 static const struct dev_pm_ops ak8975_dev_pm_ops = { 1044 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, 1045 pm_runtime_force_resume) 1046 SET_RUNTIME_PM_OPS(ak8975_runtime_suspend, 1047 ak8975_runtime_resume, NULL) 1048 }; 1049 1050 static const struct i2c_device_id ak8975_id[] = { 1051 {"ak8975", AK8975}, 1052 {"ak8963", AK8963}, 1053 {"AK8963", AK8963}, 1054 {"ak09911", AK09911}, 1055 {"ak09912", AK09912}, 1056 {} 1057 }; 1058 1059 MODULE_DEVICE_TABLE(i2c, ak8975_id); 1060 1061 static const struct of_device_id ak8975_of_match[] = { 1062 { .compatible = "asahi-kasei,ak8975", }, 1063 { .compatible = "ak8975", }, 1064 { .compatible = "asahi-kasei,ak8963", }, 1065 { .compatible = "ak8963", }, 1066 { .compatible = "asahi-kasei,ak09911", }, 1067 { .compatible = "ak09911", }, 1068 { .compatible = "asahi-kasei,ak09912", }, 1069 { .compatible = "ak09912", }, 1070 {} 1071 }; 1072 MODULE_DEVICE_TABLE(of, ak8975_of_match); 1073 1074 static struct i2c_driver ak8975_driver = { 1075 .driver = { 1076 .name = "ak8975", 1077 .pm = &ak8975_dev_pm_ops, 1078 .of_match_table = of_match_ptr(ak8975_of_match), 1079 .acpi_match_table = ACPI_PTR(ak_acpi_match), 1080 }, 1081 .probe = ak8975_probe, 1082 .remove = ak8975_remove, 1083 .id_table = ak8975_id, 1084 }; 1085 module_i2c_driver(ak8975_driver); 1086 1087 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>"); 1088 MODULE_DESCRIPTION("AK8975 magnetometer driver"); 1089 MODULE_LICENSE("GPL"); 1090