rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Driver for the Asahi Kasei EMD Corporation AK8974 |
| 3 | * and Aichi Steel AMI305 magnetometer chips. |
| 4 | * Based on a patch from Samu Onkalo and the AK8975 IIO driver. |
| 5 | * |
| 6 | * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
| 7 | * Copyright (c) 2010 NVIDIA Corporation. |
| 8 | * Copyright (C) 2016 Linaro Ltd. |
| 9 | * |
| 10 | * Author: Samu Onkalo <samu.p.onkalo@nokia.com> |
| 11 | * Author: Linus Walleij <linus.walleij@linaro.org> |
| 12 | */ |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/i2c.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/irq.h> /* For irq_get_irq_data() */ |
| 18 | #include <linux/completion.h> |
| 19 | #include <linux/err.h> |
| 20 | #include <linux/mutex.h> |
| 21 | #include <linux/delay.h> |
| 22 | #include <linux/bitops.h> |
| 23 | #include <linux/random.h> |
| 24 | #include <linux/regmap.h> |
| 25 | #include <linux/regulator/consumer.h> |
| 26 | #include <linux/pm_runtime.h> |
| 27 | |
| 28 | #include <linux/iio/iio.h> |
| 29 | #include <linux/iio/sysfs.h> |
| 30 | #include <linux/iio/buffer.h> |
| 31 | #include <linux/iio/trigger.h> |
| 32 | #include <linux/iio/trigger_consumer.h> |
| 33 | #include <linux/iio/triggered_buffer.h> |
| 34 | |
| 35 | /* |
| 36 | * 16-bit registers are little-endian. LSB is at the address defined below |
| 37 | * and MSB is at the next higher address. |
| 38 | */ |
| 39 | |
| 40 | /* These registers are common for AK8974 and AMI30x */ |
| 41 | #define AK8974_SELFTEST 0x0C |
| 42 | #define AK8974_SELFTEST_IDLE 0x55 |
| 43 | #define AK8974_SELFTEST_OK 0xAA |
| 44 | |
| 45 | #define AK8974_INFO 0x0D |
| 46 | |
| 47 | #define AK8974_WHOAMI 0x0F |
| 48 | #define AK8974_WHOAMI_VALUE_AMI306 0x46 |
| 49 | #define AK8974_WHOAMI_VALUE_AMI305 0x47 |
| 50 | #define AK8974_WHOAMI_VALUE_AK8974 0x48 |
| 51 | |
| 52 | #define AK8974_DATA_X 0x10 |
| 53 | #define AK8974_DATA_Y 0x12 |
| 54 | #define AK8974_DATA_Z 0x14 |
| 55 | #define AK8974_INT_SRC 0x16 |
| 56 | #define AK8974_STATUS 0x18 |
| 57 | #define AK8974_INT_CLEAR 0x1A |
| 58 | #define AK8974_CTRL1 0x1B |
| 59 | #define AK8974_CTRL2 0x1C |
| 60 | #define AK8974_CTRL3 0x1D |
| 61 | #define AK8974_INT_CTRL 0x1E |
| 62 | #define AK8974_INT_THRES 0x26 /* Absolute any axis value threshold */ |
| 63 | #define AK8974_PRESET 0x30 |
| 64 | |
| 65 | /* AK8974-specific offsets */ |
| 66 | #define AK8974_OFFSET_X 0x20 |
| 67 | #define AK8974_OFFSET_Y 0x22 |
| 68 | #define AK8974_OFFSET_Z 0x24 |
| 69 | /* AMI305-specific offsets */ |
| 70 | #define AMI305_OFFSET_X 0x6C |
| 71 | #define AMI305_OFFSET_Y 0x72 |
| 72 | #define AMI305_OFFSET_Z 0x78 |
| 73 | |
| 74 | /* Different temperature registers */ |
| 75 | #define AK8974_TEMP 0x31 |
| 76 | #define AMI305_TEMP 0x60 |
| 77 | |
| 78 | /* AMI306-specific control register */ |
| 79 | #define AMI306_CTRL4 0x5C |
| 80 | |
| 81 | /* AMI306 factory calibration data */ |
| 82 | |
| 83 | /* fine axis sensitivity */ |
| 84 | #define AMI306_FINEOUTPUT_X 0x90 |
| 85 | #define AMI306_FINEOUTPUT_Y 0x92 |
| 86 | #define AMI306_FINEOUTPUT_Z 0x94 |
| 87 | |
| 88 | /* axis sensitivity */ |
| 89 | #define AMI306_SENS_X 0x96 |
| 90 | #define AMI306_SENS_Y 0x98 |
| 91 | #define AMI306_SENS_Z 0x9A |
| 92 | |
| 93 | /* axis cross-interference */ |
| 94 | #define AMI306_GAIN_PARA_XZ 0x9C |
| 95 | #define AMI306_GAIN_PARA_XY 0x9D |
| 96 | #define AMI306_GAIN_PARA_YZ 0x9E |
| 97 | #define AMI306_GAIN_PARA_YX 0x9F |
| 98 | #define AMI306_GAIN_PARA_ZY 0xA0 |
| 99 | #define AMI306_GAIN_PARA_ZX 0xA1 |
| 100 | |
| 101 | /* offset at ZERO magnetic field */ |
| 102 | #define AMI306_OFFZERO_X 0xF8 |
| 103 | #define AMI306_OFFZERO_Y 0xFA |
| 104 | #define AMI306_OFFZERO_Z 0xFC |
| 105 | |
| 106 | |
| 107 | #define AK8974_INT_X_HIGH BIT(7) /* Axis over +threshold */ |
| 108 | #define AK8974_INT_Y_HIGH BIT(6) |
| 109 | #define AK8974_INT_Z_HIGH BIT(5) |
| 110 | #define AK8974_INT_X_LOW BIT(4) /* Axis below -threshold */ |
| 111 | #define AK8974_INT_Y_LOW BIT(3) |
| 112 | #define AK8974_INT_Z_LOW BIT(2) |
| 113 | #define AK8974_INT_RANGE BIT(1) /* Range overflow (any axis) */ |
| 114 | |
| 115 | #define AK8974_STATUS_DRDY BIT(6) /* Data ready */ |
| 116 | #define AK8974_STATUS_OVERRUN BIT(5) /* Data overrun */ |
| 117 | #define AK8974_STATUS_INT BIT(4) /* Interrupt occurred */ |
| 118 | |
| 119 | #define AK8974_CTRL1_POWER BIT(7) /* 0 = standby; 1 = active */ |
| 120 | #define AK8974_CTRL1_RATE BIT(4) /* 0 = 10 Hz; 1 = 20 Hz */ |
| 121 | #define AK8974_CTRL1_FORCE_EN BIT(1) /* 0 = normal; 1 = force */ |
| 122 | #define AK8974_CTRL1_MODE2 BIT(0) /* 0 */ |
| 123 | |
| 124 | #define AK8974_CTRL2_INT_EN BIT(4) /* 1 = enable interrupts */ |
| 125 | #define AK8974_CTRL2_DRDY_EN BIT(3) /* 1 = enable data ready signal */ |
| 126 | #define AK8974_CTRL2_DRDY_POL BIT(2) /* 1 = data ready active high */ |
| 127 | #define AK8974_CTRL2_RESDEF (AK8974_CTRL2_DRDY_POL) |
| 128 | |
| 129 | #define AK8974_CTRL3_RESET BIT(7) /* Software reset */ |
| 130 | #define AK8974_CTRL3_FORCE BIT(6) /* Start forced measurement */ |
| 131 | #define AK8974_CTRL3_SELFTEST BIT(4) /* Set selftest register */ |
| 132 | #define AK8974_CTRL3_RESDEF 0x00 |
| 133 | |
| 134 | #define AK8974_INT_CTRL_XEN BIT(7) /* Enable interrupt for this axis */ |
| 135 | #define AK8974_INT_CTRL_YEN BIT(6) |
| 136 | #define AK8974_INT_CTRL_ZEN BIT(5) |
| 137 | #define AK8974_INT_CTRL_XYZEN (BIT(7)|BIT(6)|BIT(5)) |
| 138 | #define AK8974_INT_CTRL_POL BIT(3) /* 0 = active low; 1 = active high */ |
| 139 | #define AK8974_INT_CTRL_PULSE BIT(1) /* 0 = latched; 1 = pulse (50 usec) */ |
| 140 | #define AK8974_INT_CTRL_RESDEF (AK8974_INT_CTRL_XYZEN | AK8974_INT_CTRL_POL) |
| 141 | |
| 142 | /* The AMI305 has elaborate FW version and serial number registers */ |
| 143 | #define AMI305_VER 0xE8 |
| 144 | #define AMI305_SN 0xEA |
| 145 | |
| 146 | #define AK8974_MAX_RANGE 2048 |
| 147 | |
| 148 | #define AK8974_POWERON_DELAY 50 |
| 149 | #define AK8974_ACTIVATE_DELAY 1 |
| 150 | #define AK8974_SELFTEST_DELAY 1 |
| 151 | /* |
| 152 | * Set the autosuspend to two orders of magnitude larger than the poweron |
| 153 | * delay to make sane reasonable power tradeoff savings (5 seconds in |
| 154 | * this case). |
| 155 | */ |
| 156 | #define AK8974_AUTOSUSPEND_DELAY 5000 |
| 157 | |
| 158 | #define AK8974_MEASTIME 3 |
| 159 | |
| 160 | #define AK8974_PWR_ON 1 |
| 161 | #define AK8974_PWR_OFF 0 |
| 162 | |
| 163 | /** |
| 164 | * struct ak8974 - state container for the AK8974 driver |
| 165 | * @i2c: parent I2C client |
| 166 | * @orientation: mounting matrix, flipped axis etc |
| 167 | * @map: regmap to access the AK8974 registers over I2C |
| 168 | * @regs: the avdd and dvdd power regulators |
| 169 | * @name: the name of the part |
| 170 | * @variant: the whoami ID value (for selecting code paths) |
| 171 | * @lock: locks the magnetometer for exclusive use during a measurement |
| 172 | * @drdy_irq: uses the DRDY IRQ line |
| 173 | * @drdy_complete: completion for DRDY |
| 174 | * @drdy_active_low: the DRDY IRQ is active low |
| 175 | */ |
| 176 | struct ak8974 { |
| 177 | struct i2c_client *i2c; |
| 178 | struct iio_mount_matrix orientation; |
| 179 | struct regmap *map; |
| 180 | struct regulator_bulk_data regs[2]; |
| 181 | const char *name; |
| 182 | u8 variant; |
| 183 | struct mutex lock; |
| 184 | bool drdy_irq; |
| 185 | struct completion drdy_complete; |
| 186 | bool drdy_active_low; |
| 187 | /* Ensure timestamp is naturally aligned */ |
| 188 | struct { |
| 189 | __le16 channels[3]; |
| 190 | s64 ts __aligned(8); |
| 191 | } scan; |
| 192 | }; |
| 193 | |
| 194 | static const char ak8974_reg_avdd[] = "avdd"; |
| 195 | static const char ak8974_reg_dvdd[] = "dvdd"; |
| 196 | |
| 197 | static int ak8974_get_u16_val(struct ak8974 *ak8974, u8 reg, u16 *val) |
| 198 | { |
| 199 | int ret; |
| 200 | __le16 bulk; |
| 201 | |
| 202 | ret = regmap_bulk_read(ak8974->map, reg, &bulk, 2); |
| 203 | if (ret) |
| 204 | return ret; |
| 205 | *val = le16_to_cpu(bulk); |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | static int ak8974_set_u16_val(struct ak8974 *ak8974, u8 reg, u16 val) |
| 211 | { |
| 212 | __le16 bulk = cpu_to_le16(val); |
| 213 | |
| 214 | return regmap_bulk_write(ak8974->map, reg, &bulk, 2); |
| 215 | } |
| 216 | |
| 217 | static int ak8974_set_power(struct ak8974 *ak8974, bool mode) |
| 218 | { |
| 219 | int ret; |
| 220 | u8 val; |
| 221 | |
| 222 | val = mode ? AK8974_CTRL1_POWER : 0; |
| 223 | val |= AK8974_CTRL1_FORCE_EN; |
| 224 | ret = regmap_write(ak8974->map, AK8974_CTRL1, val); |
| 225 | if (ret < 0) |
| 226 | return ret; |
| 227 | |
| 228 | if (mode) |
| 229 | msleep(AK8974_ACTIVATE_DELAY); |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | static int ak8974_reset(struct ak8974 *ak8974) |
| 235 | { |
| 236 | int ret; |
| 237 | |
| 238 | /* Power on to get register access. Sets CTRL1 reg to reset state */ |
| 239 | ret = ak8974_set_power(ak8974, AK8974_PWR_ON); |
| 240 | if (ret) |
| 241 | return ret; |
| 242 | ret = regmap_write(ak8974->map, AK8974_CTRL2, AK8974_CTRL2_RESDEF); |
| 243 | if (ret) |
| 244 | return ret; |
| 245 | ret = regmap_write(ak8974->map, AK8974_CTRL3, AK8974_CTRL3_RESDEF); |
| 246 | if (ret) |
| 247 | return ret; |
| 248 | ret = regmap_write(ak8974->map, AK8974_INT_CTRL, |
| 249 | AK8974_INT_CTRL_RESDEF); |
| 250 | if (ret) |
| 251 | return ret; |
| 252 | |
| 253 | /* After reset, power off is default state */ |
| 254 | return ak8974_set_power(ak8974, AK8974_PWR_OFF); |
| 255 | } |
| 256 | |
| 257 | static int ak8974_configure(struct ak8974 *ak8974) |
| 258 | { |
| 259 | int ret; |
| 260 | |
| 261 | ret = regmap_write(ak8974->map, AK8974_CTRL2, AK8974_CTRL2_DRDY_EN | |
| 262 | AK8974_CTRL2_INT_EN); |
| 263 | if (ret) |
| 264 | return ret; |
| 265 | ret = regmap_write(ak8974->map, AK8974_CTRL3, 0); |
| 266 | if (ret) |
| 267 | return ret; |
| 268 | if (ak8974->variant == AK8974_WHOAMI_VALUE_AMI306) { |
| 269 | /* magic from datasheet: set high-speed measurement mode */ |
| 270 | ret = ak8974_set_u16_val(ak8974, AMI306_CTRL4, 0xA07E); |
| 271 | if (ret) |
| 272 | return ret; |
| 273 | } |
| 274 | ret = regmap_write(ak8974->map, AK8974_INT_CTRL, AK8974_INT_CTRL_POL); |
| 275 | if (ret) |
| 276 | return ret; |
| 277 | |
| 278 | return regmap_write(ak8974->map, AK8974_PRESET, 0); |
| 279 | } |
| 280 | |
| 281 | static int ak8974_trigmeas(struct ak8974 *ak8974) |
| 282 | { |
| 283 | unsigned int clear; |
| 284 | u8 mask; |
| 285 | u8 val; |
| 286 | int ret; |
| 287 | |
| 288 | /* Clear any previous measurement overflow status */ |
| 289 | ret = regmap_read(ak8974->map, AK8974_INT_CLEAR, &clear); |
| 290 | if (ret) |
| 291 | return ret; |
| 292 | |
| 293 | /* If we have a DRDY IRQ line, use it */ |
| 294 | if (ak8974->drdy_irq) { |
| 295 | mask = AK8974_CTRL2_INT_EN | |
| 296 | AK8974_CTRL2_DRDY_EN | |
| 297 | AK8974_CTRL2_DRDY_POL; |
| 298 | val = AK8974_CTRL2_DRDY_EN; |
| 299 | |
| 300 | if (!ak8974->drdy_active_low) |
| 301 | val |= AK8974_CTRL2_DRDY_POL; |
| 302 | |
| 303 | init_completion(&ak8974->drdy_complete); |
| 304 | ret = regmap_update_bits(ak8974->map, AK8974_CTRL2, |
| 305 | mask, val); |
| 306 | if (ret) |
| 307 | return ret; |
| 308 | } |
| 309 | |
| 310 | /* Force a measurement */ |
| 311 | return regmap_update_bits(ak8974->map, |
| 312 | AK8974_CTRL3, |
| 313 | AK8974_CTRL3_FORCE, |
| 314 | AK8974_CTRL3_FORCE); |
| 315 | } |
| 316 | |
| 317 | static int ak8974_await_drdy(struct ak8974 *ak8974) |
| 318 | { |
| 319 | int timeout = 2; |
| 320 | unsigned int val; |
| 321 | int ret; |
| 322 | |
| 323 | if (ak8974->drdy_irq) { |
| 324 | ret = wait_for_completion_timeout(&ak8974->drdy_complete, |
| 325 | 1 + msecs_to_jiffies(1000)); |
| 326 | if (!ret) { |
| 327 | dev_err(&ak8974->i2c->dev, |
| 328 | "timeout waiting for DRDY IRQ\n"); |
| 329 | return -ETIMEDOUT; |
| 330 | } |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | /* Default delay-based poll loop */ |
| 335 | do { |
| 336 | msleep(AK8974_MEASTIME); |
| 337 | ret = regmap_read(ak8974->map, AK8974_STATUS, &val); |
| 338 | if (ret < 0) |
| 339 | return ret; |
| 340 | if (val & AK8974_STATUS_DRDY) |
| 341 | return 0; |
| 342 | } while (--timeout); |
| 343 | |
| 344 | dev_err(&ak8974->i2c->dev, "timeout waiting for DRDY\n"); |
| 345 | return -ETIMEDOUT; |
| 346 | } |
| 347 | |
| 348 | static int ak8974_getresult(struct ak8974 *ak8974, __le16 *result) |
| 349 | { |
| 350 | unsigned int src; |
| 351 | int ret; |
| 352 | |
| 353 | ret = ak8974_await_drdy(ak8974); |
| 354 | if (ret) |
| 355 | return ret; |
| 356 | ret = regmap_read(ak8974->map, AK8974_INT_SRC, &src); |
| 357 | if (ret < 0) |
| 358 | return ret; |
| 359 | |
| 360 | /* Out of range overflow! Strong magnet close? */ |
| 361 | if (src & AK8974_INT_RANGE) { |
| 362 | dev_err(&ak8974->i2c->dev, |
| 363 | "range overflow in sensor\n"); |
| 364 | return -ERANGE; |
| 365 | } |
| 366 | |
| 367 | ret = regmap_bulk_read(ak8974->map, AK8974_DATA_X, result, 6); |
| 368 | if (ret) |
| 369 | return ret; |
| 370 | |
| 371 | return ret; |
| 372 | } |
| 373 | |
| 374 | static irqreturn_t ak8974_drdy_irq(int irq, void *d) |
| 375 | { |
| 376 | struct ak8974 *ak8974 = d; |
| 377 | |
| 378 | if (!ak8974->drdy_irq) |
| 379 | return IRQ_NONE; |
| 380 | |
| 381 | /* TODO: timestamp here to get good measurement stamps */ |
| 382 | return IRQ_WAKE_THREAD; |
| 383 | } |
| 384 | |
| 385 | static irqreturn_t ak8974_drdy_irq_thread(int irq, void *d) |
| 386 | { |
| 387 | struct ak8974 *ak8974 = d; |
| 388 | unsigned int val; |
| 389 | int ret; |
| 390 | |
| 391 | /* Check if this was a DRDY from us */ |
| 392 | ret = regmap_read(ak8974->map, AK8974_STATUS, &val); |
| 393 | if (ret < 0) { |
| 394 | dev_err(&ak8974->i2c->dev, "error reading DRDY status\n"); |
| 395 | return IRQ_HANDLED; |
| 396 | } |
| 397 | if (val & AK8974_STATUS_DRDY) { |
| 398 | /* Yes this was our IRQ */ |
| 399 | complete(&ak8974->drdy_complete); |
| 400 | return IRQ_HANDLED; |
| 401 | } |
| 402 | |
| 403 | /* We may be on a shared IRQ, let the next client check */ |
| 404 | return IRQ_NONE; |
| 405 | } |
| 406 | |
| 407 | static int ak8974_selftest(struct ak8974 *ak8974) |
| 408 | { |
| 409 | struct device *dev = &ak8974->i2c->dev; |
| 410 | unsigned int val; |
| 411 | int ret; |
| 412 | |
| 413 | ret = regmap_read(ak8974->map, AK8974_SELFTEST, &val); |
| 414 | if (ret) |
| 415 | return ret; |
| 416 | if (val != AK8974_SELFTEST_IDLE) { |
| 417 | dev_err(dev, "selftest not idle before test\n"); |
| 418 | return -EIO; |
| 419 | } |
| 420 | |
| 421 | /* Trigger self-test */ |
| 422 | ret = regmap_update_bits(ak8974->map, |
| 423 | AK8974_CTRL3, |
| 424 | AK8974_CTRL3_SELFTEST, |
| 425 | AK8974_CTRL3_SELFTEST); |
| 426 | if (ret) { |
| 427 | dev_err(dev, "could not write CTRL3\n"); |
| 428 | return ret; |
| 429 | } |
| 430 | |
| 431 | msleep(AK8974_SELFTEST_DELAY); |
| 432 | |
| 433 | ret = regmap_read(ak8974->map, AK8974_SELFTEST, &val); |
| 434 | if (ret) |
| 435 | return ret; |
| 436 | if (val != AK8974_SELFTEST_OK) { |
| 437 | dev_err(dev, "selftest result NOT OK (%02x)\n", val); |
| 438 | return -EIO; |
| 439 | } |
| 440 | |
| 441 | ret = regmap_read(ak8974->map, AK8974_SELFTEST, &val); |
| 442 | if (ret) |
| 443 | return ret; |
| 444 | if (val != AK8974_SELFTEST_IDLE) { |
| 445 | dev_err(dev, "selftest not idle after test (%02x)\n", val); |
| 446 | return -EIO; |
| 447 | } |
| 448 | dev_dbg(dev, "passed self-test\n"); |
| 449 | |
| 450 | return 0; |
| 451 | } |
| 452 | |
| 453 | static void ak8974_read_calib_data(struct ak8974 *ak8974, unsigned int reg, |
| 454 | __le16 *tab, size_t tab_size) |
| 455 | { |
| 456 | int ret = regmap_bulk_read(ak8974->map, reg, tab, tab_size); |
| 457 | if (ret) { |
| 458 | memset(tab, 0xFF, tab_size); |
| 459 | dev_warn(&ak8974->i2c->dev, |
| 460 | "can't read calibration data (regs %u..%zu): %d\n", |
| 461 | reg, reg + tab_size - 1, ret); |
| 462 | } else { |
| 463 | add_device_randomness(tab, tab_size); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | static int ak8974_detect(struct ak8974 *ak8974) |
| 468 | { |
| 469 | unsigned int whoami; |
| 470 | const char *name; |
| 471 | int ret; |
| 472 | unsigned int fw; |
| 473 | u16 sn; |
| 474 | |
| 475 | ret = regmap_read(ak8974->map, AK8974_WHOAMI, &whoami); |
| 476 | if (ret) |
| 477 | return ret; |
| 478 | |
| 479 | name = "ami305"; |
| 480 | |
| 481 | switch (whoami) { |
| 482 | case AK8974_WHOAMI_VALUE_AMI306: |
| 483 | name = "ami306"; |
| 484 | /* fall-through */ |
| 485 | case AK8974_WHOAMI_VALUE_AMI305: |
| 486 | ret = regmap_read(ak8974->map, AMI305_VER, &fw); |
| 487 | if (ret) |
| 488 | return ret; |
| 489 | fw &= 0x7f; /* only bits 0 thru 6 valid */ |
| 490 | ret = ak8974_get_u16_val(ak8974, AMI305_SN, &sn); |
| 491 | if (ret) |
| 492 | return ret; |
| 493 | add_device_randomness(&sn, sizeof(sn)); |
| 494 | dev_info(&ak8974->i2c->dev, |
| 495 | "detected %s, FW ver %02x, S/N: %04x\n", |
| 496 | name, fw, sn); |
| 497 | break; |
| 498 | case AK8974_WHOAMI_VALUE_AK8974: |
| 499 | name = "ak8974"; |
| 500 | dev_info(&ak8974->i2c->dev, "detected AK8974\n"); |
| 501 | break; |
| 502 | default: |
| 503 | dev_err(&ak8974->i2c->dev, "unsupported device (%02x) ", |
| 504 | whoami); |
| 505 | return -ENODEV; |
| 506 | } |
| 507 | |
| 508 | ak8974->name = name; |
| 509 | ak8974->variant = whoami; |
| 510 | |
| 511 | if (whoami == AK8974_WHOAMI_VALUE_AMI306) { |
| 512 | __le16 fab_data1[9], fab_data2[3]; |
| 513 | int i; |
| 514 | |
| 515 | ak8974_read_calib_data(ak8974, AMI306_FINEOUTPUT_X, |
| 516 | fab_data1, sizeof(fab_data1)); |
| 517 | ak8974_read_calib_data(ak8974, AMI306_OFFZERO_X, |
| 518 | fab_data2, sizeof(fab_data2)); |
| 519 | |
| 520 | for (i = 0; i < 3; ++i) { |
| 521 | static const char axis[3] = "XYZ"; |
| 522 | static const char pgaxis[6] = "ZYZXYX"; |
| 523 | unsigned offz = le16_to_cpu(fab_data2[i]) & 0x7F; |
| 524 | unsigned fine = le16_to_cpu(fab_data1[i]); |
| 525 | unsigned sens = le16_to_cpu(fab_data1[i + 3]); |
| 526 | unsigned pgain1 = le16_to_cpu(fab_data1[i + 6]); |
| 527 | unsigned pgain2 = pgain1 >> 8; |
| 528 | |
| 529 | pgain1 &= 0xFF; |
| 530 | |
| 531 | dev_info(&ak8974->i2c->dev, |
| 532 | "factory calibration for axis %c: offz=%u sens=%u fine=%u pga%c=%u pga%c=%u\n", |
| 533 | axis[i], offz, sens, fine, pgaxis[i * 2], |
| 534 | pgain1, pgaxis[i * 2 + 1], pgain2); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | return 0; |
| 539 | } |
| 540 | |
| 541 | static int ak8974_read_raw(struct iio_dev *indio_dev, |
| 542 | struct iio_chan_spec const *chan, |
| 543 | int *val, int *val2, |
| 544 | long mask) |
| 545 | { |
| 546 | struct ak8974 *ak8974 = iio_priv(indio_dev); |
| 547 | __le16 hw_values[3]; |
| 548 | int ret = -EINVAL; |
| 549 | |
| 550 | pm_runtime_get_sync(&ak8974->i2c->dev); |
| 551 | mutex_lock(&ak8974->lock); |
| 552 | |
| 553 | switch (mask) { |
| 554 | case IIO_CHAN_INFO_RAW: |
| 555 | if (chan->address > 2) { |
| 556 | dev_err(&ak8974->i2c->dev, "faulty channel address\n"); |
| 557 | ret = -EIO; |
| 558 | goto out_unlock; |
| 559 | } |
| 560 | ret = ak8974_trigmeas(ak8974); |
| 561 | if (ret) |
| 562 | goto out_unlock; |
| 563 | ret = ak8974_getresult(ak8974, hw_values); |
| 564 | if (ret) |
| 565 | goto out_unlock; |
| 566 | |
| 567 | /* |
| 568 | * We read all axes and discard all but one, for optimized |
| 569 | * reading, use the triggered buffer. |
| 570 | */ |
| 571 | *val = (s16)le16_to_cpu(hw_values[chan->address]); |
| 572 | |
| 573 | ret = IIO_VAL_INT; |
| 574 | } |
| 575 | |
| 576 | out_unlock: |
| 577 | mutex_unlock(&ak8974->lock); |
| 578 | pm_runtime_mark_last_busy(&ak8974->i2c->dev); |
| 579 | pm_runtime_put_autosuspend(&ak8974->i2c->dev); |
| 580 | |
| 581 | return ret; |
| 582 | } |
| 583 | |
| 584 | static void ak8974_fill_buffer(struct iio_dev *indio_dev) |
| 585 | { |
| 586 | struct ak8974 *ak8974 = iio_priv(indio_dev); |
| 587 | int ret; |
| 588 | |
| 589 | pm_runtime_get_sync(&ak8974->i2c->dev); |
| 590 | mutex_lock(&ak8974->lock); |
| 591 | |
| 592 | ret = ak8974_trigmeas(ak8974); |
| 593 | if (ret) { |
| 594 | dev_err(&ak8974->i2c->dev, "error triggering measure\n"); |
| 595 | goto out_unlock; |
| 596 | } |
| 597 | ret = ak8974_getresult(ak8974, ak8974->scan.channels); |
| 598 | if (ret) { |
| 599 | dev_err(&ak8974->i2c->dev, "error getting measures\n"); |
| 600 | goto out_unlock; |
| 601 | } |
| 602 | |
| 603 | iio_push_to_buffers_with_timestamp(indio_dev, &ak8974->scan, |
| 604 | iio_get_time_ns(indio_dev)); |
| 605 | |
| 606 | out_unlock: |
| 607 | mutex_unlock(&ak8974->lock); |
| 608 | pm_runtime_mark_last_busy(&ak8974->i2c->dev); |
| 609 | pm_runtime_put_autosuspend(&ak8974->i2c->dev); |
| 610 | } |
| 611 | |
| 612 | static irqreturn_t ak8974_handle_trigger(int irq, void *p) |
| 613 | { |
| 614 | const struct iio_poll_func *pf = p; |
| 615 | struct iio_dev *indio_dev = pf->indio_dev; |
| 616 | |
| 617 | ak8974_fill_buffer(indio_dev); |
| 618 | iio_trigger_notify_done(indio_dev->trig); |
| 619 | |
| 620 | return IRQ_HANDLED; |
| 621 | } |
| 622 | |
| 623 | static const struct iio_mount_matrix * |
| 624 | ak8974_get_mount_matrix(const struct iio_dev *indio_dev, |
| 625 | const struct iio_chan_spec *chan) |
| 626 | { |
| 627 | struct ak8974 *ak8974 = iio_priv(indio_dev); |
| 628 | |
| 629 | return &ak8974->orientation; |
| 630 | } |
| 631 | |
| 632 | static const struct iio_chan_spec_ext_info ak8974_ext_info[] = { |
| 633 | IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, ak8974_get_mount_matrix), |
| 634 | { }, |
| 635 | }; |
| 636 | |
| 637 | #define AK8974_AXIS_CHANNEL(axis, index) \ |
| 638 | { \ |
| 639 | .type = IIO_MAGN, \ |
| 640 | .modified = 1, \ |
| 641 | .channel2 = IIO_MOD_##axis, \ |
| 642 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 643 | .ext_info = ak8974_ext_info, \ |
| 644 | .address = index, \ |
| 645 | .scan_index = index, \ |
| 646 | .scan_type = { \ |
| 647 | .sign = 's', \ |
| 648 | .realbits = 16, \ |
| 649 | .storagebits = 16, \ |
| 650 | .endianness = IIO_LE \ |
| 651 | }, \ |
| 652 | } |
| 653 | |
| 654 | static const struct iio_chan_spec ak8974_channels[] = { |
| 655 | AK8974_AXIS_CHANNEL(X, 0), |
| 656 | AK8974_AXIS_CHANNEL(Y, 1), |
| 657 | AK8974_AXIS_CHANNEL(Z, 2), |
| 658 | IIO_CHAN_SOFT_TIMESTAMP(3), |
| 659 | }; |
| 660 | |
| 661 | static const unsigned long ak8974_scan_masks[] = { 0x7, 0 }; |
| 662 | |
| 663 | static const struct iio_info ak8974_info = { |
| 664 | .read_raw = &ak8974_read_raw, |
| 665 | .driver_module = THIS_MODULE, |
| 666 | }; |
| 667 | |
| 668 | static bool ak8974_writeable_reg(struct device *dev, unsigned int reg) |
| 669 | { |
| 670 | struct i2c_client *i2c = to_i2c_client(dev); |
| 671 | struct iio_dev *indio_dev = i2c_get_clientdata(i2c); |
| 672 | struct ak8974 *ak8974 = iio_priv(indio_dev); |
| 673 | |
| 674 | switch (reg) { |
| 675 | case AK8974_CTRL1: |
| 676 | case AK8974_CTRL2: |
| 677 | case AK8974_CTRL3: |
| 678 | case AK8974_INT_CTRL: |
| 679 | case AK8974_INT_THRES: |
| 680 | case AK8974_INT_THRES + 1: |
| 681 | case AK8974_PRESET: |
| 682 | case AK8974_PRESET + 1: |
| 683 | return true; |
| 684 | case AK8974_OFFSET_X: |
| 685 | case AK8974_OFFSET_X + 1: |
| 686 | case AK8974_OFFSET_Y: |
| 687 | case AK8974_OFFSET_Y + 1: |
| 688 | case AK8974_OFFSET_Z: |
| 689 | case AK8974_OFFSET_Z + 1: |
| 690 | if (ak8974->variant == AK8974_WHOAMI_VALUE_AK8974) |
| 691 | return true; |
| 692 | return false; |
| 693 | case AMI305_OFFSET_X: |
| 694 | case AMI305_OFFSET_X + 1: |
| 695 | case AMI305_OFFSET_Y: |
| 696 | case AMI305_OFFSET_Y + 1: |
| 697 | case AMI305_OFFSET_Z: |
| 698 | case AMI305_OFFSET_Z + 1: |
| 699 | return ak8974->variant == AK8974_WHOAMI_VALUE_AMI305 || |
| 700 | ak8974->variant == AK8974_WHOAMI_VALUE_AMI306; |
| 701 | case AMI306_CTRL4: |
| 702 | case AMI306_CTRL4 + 1: |
| 703 | return ak8974->variant == AK8974_WHOAMI_VALUE_AMI306; |
| 704 | default: |
| 705 | return false; |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | static bool ak8974_precious_reg(struct device *dev, unsigned int reg) |
| 710 | { |
| 711 | return reg == AK8974_INT_CLEAR; |
| 712 | } |
| 713 | |
| 714 | static const struct regmap_config ak8974_regmap_config = { |
| 715 | .reg_bits = 8, |
| 716 | .val_bits = 8, |
| 717 | .max_register = 0xff, |
| 718 | .writeable_reg = ak8974_writeable_reg, |
| 719 | .precious_reg = ak8974_precious_reg, |
| 720 | }; |
| 721 | |
| 722 | static int ak8974_probe(struct i2c_client *i2c, |
| 723 | const struct i2c_device_id *id) |
| 724 | { |
| 725 | struct iio_dev *indio_dev; |
| 726 | struct ak8974 *ak8974; |
| 727 | unsigned long irq_trig; |
| 728 | int irq = i2c->irq; |
| 729 | int ret; |
| 730 | |
| 731 | /* Register with IIO */ |
| 732 | indio_dev = devm_iio_device_alloc(&i2c->dev, sizeof(*ak8974)); |
| 733 | if (indio_dev == NULL) |
| 734 | return -ENOMEM; |
| 735 | |
| 736 | ak8974 = iio_priv(indio_dev); |
| 737 | i2c_set_clientdata(i2c, indio_dev); |
| 738 | ak8974->i2c = i2c; |
| 739 | mutex_init(&ak8974->lock); |
| 740 | |
| 741 | ret = of_iio_read_mount_matrix(&i2c->dev, |
| 742 | "mount-matrix", |
| 743 | &ak8974->orientation); |
| 744 | if (ret) |
| 745 | return ret; |
| 746 | |
| 747 | ak8974->regs[0].supply = ak8974_reg_avdd; |
| 748 | ak8974->regs[1].supply = ak8974_reg_dvdd; |
| 749 | |
| 750 | ret = devm_regulator_bulk_get(&i2c->dev, |
| 751 | ARRAY_SIZE(ak8974->regs), |
| 752 | ak8974->regs); |
| 753 | if (ret < 0) { |
| 754 | dev_err(&i2c->dev, "cannot get regulators\n"); |
| 755 | return ret; |
| 756 | } |
| 757 | |
| 758 | ret = regulator_bulk_enable(ARRAY_SIZE(ak8974->regs), ak8974->regs); |
| 759 | if (ret < 0) { |
| 760 | dev_err(&i2c->dev, "cannot enable regulators\n"); |
| 761 | return ret; |
| 762 | } |
| 763 | |
| 764 | /* Take runtime PM online */ |
| 765 | pm_runtime_get_noresume(&i2c->dev); |
| 766 | pm_runtime_set_active(&i2c->dev); |
| 767 | pm_runtime_enable(&i2c->dev); |
| 768 | |
| 769 | ak8974->map = devm_regmap_init_i2c(i2c, &ak8974_regmap_config); |
| 770 | if (IS_ERR(ak8974->map)) { |
| 771 | dev_err(&i2c->dev, "failed to allocate register map\n"); |
| 772 | pm_runtime_put_noidle(&i2c->dev); |
| 773 | pm_runtime_disable(&i2c->dev); |
| 774 | return PTR_ERR(ak8974->map); |
| 775 | } |
| 776 | |
| 777 | ret = ak8974_set_power(ak8974, AK8974_PWR_ON); |
| 778 | if (ret) { |
| 779 | dev_err(&i2c->dev, "could not power on\n"); |
| 780 | goto disable_pm; |
| 781 | } |
| 782 | |
| 783 | ret = ak8974_detect(ak8974); |
| 784 | if (ret) { |
| 785 | dev_err(&i2c->dev, "neither AK8974 nor AMI30x found\n"); |
| 786 | goto disable_pm; |
| 787 | } |
| 788 | |
| 789 | ret = ak8974_selftest(ak8974); |
| 790 | if (ret) |
| 791 | dev_err(&i2c->dev, "selftest failed (continuing anyway)\n"); |
| 792 | |
| 793 | ret = ak8974_reset(ak8974); |
| 794 | if (ret) { |
| 795 | dev_err(&i2c->dev, "AK8974 reset failed\n"); |
| 796 | goto disable_pm; |
| 797 | } |
| 798 | |
| 799 | indio_dev->dev.parent = &i2c->dev; |
| 800 | indio_dev->channels = ak8974_channels; |
| 801 | indio_dev->num_channels = ARRAY_SIZE(ak8974_channels); |
| 802 | indio_dev->info = &ak8974_info; |
| 803 | indio_dev->available_scan_masks = ak8974_scan_masks; |
| 804 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 805 | indio_dev->name = ak8974->name; |
| 806 | |
| 807 | ret = iio_triggered_buffer_setup(indio_dev, NULL, |
| 808 | ak8974_handle_trigger, |
| 809 | NULL); |
| 810 | if (ret) { |
| 811 | dev_err(&i2c->dev, "triggered buffer setup failed\n"); |
| 812 | goto disable_pm; |
| 813 | } |
| 814 | |
| 815 | /* If we have a valid DRDY IRQ, make use of it */ |
| 816 | if (irq > 0) { |
| 817 | irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq)); |
| 818 | if (irq_trig == IRQF_TRIGGER_RISING) { |
| 819 | dev_info(&i2c->dev, "enable rising edge DRDY IRQ\n"); |
| 820 | } else if (irq_trig == IRQF_TRIGGER_FALLING) { |
| 821 | ak8974->drdy_active_low = true; |
| 822 | dev_info(&i2c->dev, "enable falling edge DRDY IRQ\n"); |
| 823 | } else { |
| 824 | irq_trig = IRQF_TRIGGER_RISING; |
| 825 | } |
| 826 | irq_trig |= IRQF_ONESHOT; |
| 827 | irq_trig |= IRQF_SHARED; |
| 828 | |
| 829 | ret = devm_request_threaded_irq(&i2c->dev, |
| 830 | irq, |
| 831 | ak8974_drdy_irq, |
| 832 | ak8974_drdy_irq_thread, |
| 833 | irq_trig, |
| 834 | ak8974->name, |
| 835 | ak8974); |
| 836 | if (ret) { |
| 837 | dev_err(&i2c->dev, "unable to request DRDY IRQ " |
| 838 | "- proceeding without IRQ\n"); |
| 839 | goto no_irq; |
| 840 | } |
| 841 | ak8974->drdy_irq = true; |
| 842 | } |
| 843 | |
| 844 | no_irq: |
| 845 | ret = iio_device_register(indio_dev); |
| 846 | if (ret) { |
| 847 | dev_err(&i2c->dev, "device register failed\n"); |
| 848 | goto cleanup_buffer; |
| 849 | } |
| 850 | |
| 851 | pm_runtime_set_autosuspend_delay(&i2c->dev, |
| 852 | AK8974_AUTOSUSPEND_DELAY); |
| 853 | pm_runtime_use_autosuspend(&i2c->dev); |
| 854 | pm_runtime_put(&i2c->dev); |
| 855 | |
| 856 | return 0; |
| 857 | |
| 858 | cleanup_buffer: |
| 859 | iio_triggered_buffer_cleanup(indio_dev); |
| 860 | disable_pm: |
| 861 | pm_runtime_put_noidle(&i2c->dev); |
| 862 | pm_runtime_disable(&i2c->dev); |
| 863 | ak8974_set_power(ak8974, AK8974_PWR_OFF); |
| 864 | regulator_bulk_disable(ARRAY_SIZE(ak8974->regs), ak8974->regs); |
| 865 | |
| 866 | return ret; |
| 867 | } |
| 868 | |
| 869 | static int ak8974_remove(struct i2c_client *i2c) |
| 870 | { |
| 871 | struct iio_dev *indio_dev = i2c_get_clientdata(i2c); |
| 872 | struct ak8974 *ak8974 = iio_priv(indio_dev); |
| 873 | |
| 874 | iio_device_unregister(indio_dev); |
| 875 | iio_triggered_buffer_cleanup(indio_dev); |
| 876 | pm_runtime_get_sync(&i2c->dev); |
| 877 | pm_runtime_put_noidle(&i2c->dev); |
| 878 | pm_runtime_disable(&i2c->dev); |
| 879 | ak8974_set_power(ak8974, AK8974_PWR_OFF); |
| 880 | regulator_bulk_disable(ARRAY_SIZE(ak8974->regs), ak8974->regs); |
| 881 | |
| 882 | return 0; |
| 883 | } |
| 884 | |
| 885 | static int __maybe_unused ak8974_runtime_suspend(struct device *dev) |
| 886 | { |
| 887 | struct ak8974 *ak8974 = |
| 888 | iio_priv(i2c_get_clientdata(to_i2c_client(dev))); |
| 889 | |
| 890 | ak8974_set_power(ak8974, AK8974_PWR_OFF); |
| 891 | regulator_bulk_disable(ARRAY_SIZE(ak8974->regs), ak8974->regs); |
| 892 | |
| 893 | return 0; |
| 894 | } |
| 895 | |
| 896 | static int __maybe_unused ak8974_runtime_resume(struct device *dev) |
| 897 | { |
| 898 | struct ak8974 *ak8974 = |
| 899 | iio_priv(i2c_get_clientdata(to_i2c_client(dev))); |
| 900 | int ret; |
| 901 | |
| 902 | ret = regulator_bulk_enable(ARRAY_SIZE(ak8974->regs), ak8974->regs); |
| 903 | if (ret) |
| 904 | return ret; |
| 905 | msleep(AK8974_POWERON_DELAY); |
| 906 | ret = ak8974_set_power(ak8974, AK8974_PWR_ON); |
| 907 | if (ret) |
| 908 | goto out_regulator_disable; |
| 909 | |
| 910 | ret = ak8974_configure(ak8974); |
| 911 | if (ret) |
| 912 | goto out_disable_power; |
| 913 | |
| 914 | return 0; |
| 915 | |
| 916 | out_disable_power: |
| 917 | ak8974_set_power(ak8974, AK8974_PWR_OFF); |
| 918 | out_regulator_disable: |
| 919 | regulator_bulk_disable(ARRAY_SIZE(ak8974->regs), ak8974->regs); |
| 920 | |
| 921 | return ret; |
| 922 | } |
| 923 | |
| 924 | static const struct dev_pm_ops ak8974_dev_pm_ops = { |
| 925 | SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, |
| 926 | pm_runtime_force_resume) |
| 927 | SET_RUNTIME_PM_OPS(ak8974_runtime_suspend, |
| 928 | ak8974_runtime_resume, NULL) |
| 929 | }; |
| 930 | |
| 931 | static const struct i2c_device_id ak8974_id[] = { |
| 932 | {"ami305", 0 }, |
| 933 | {"ami306", 0 }, |
| 934 | {"ak8974", 0 }, |
| 935 | {} |
| 936 | }; |
| 937 | MODULE_DEVICE_TABLE(i2c, ak8974_id); |
| 938 | |
| 939 | static const struct of_device_id ak8974_of_match[] = { |
| 940 | { .compatible = "asahi-kasei,ak8974", }, |
| 941 | {} |
| 942 | }; |
| 943 | MODULE_DEVICE_TABLE(of, ak8974_of_match); |
| 944 | |
| 945 | static struct i2c_driver ak8974_driver = { |
| 946 | .driver = { |
| 947 | .name = "ak8974", |
| 948 | .pm = &ak8974_dev_pm_ops, |
| 949 | .of_match_table = of_match_ptr(ak8974_of_match), |
| 950 | }, |
| 951 | .probe = ak8974_probe, |
| 952 | .remove = ak8974_remove, |
| 953 | .id_table = ak8974_id, |
| 954 | }; |
| 955 | module_i2c_driver(ak8974_driver); |
| 956 | |
| 957 | MODULE_DESCRIPTION("AK8974 and AMI30x 3-axis magnetometer driver"); |
| 958 | MODULE_AUTHOR("Samu Onkalo"); |
| 959 | MODULE_AUTHOR("Linus Walleij"); |
| 960 | MODULE_LICENSE("GPL v2"); |