Linus Walleij | 7c94a8b | 2016-07-25 15:54:55 +0200 | [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> |
Michał Mirosław | 408cc6e | 2017-08-17 15:56:11 +0200 | [diff] [blame^] | 23 | #include <linux/random.h> |
Linus Walleij | 7c94a8b | 2016-07-25 15:54:55 +0200 | [diff] [blame] | 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 | |
Michał Mirosław | 21be26f | 2017-08-17 15:56:10 +0200 | [diff] [blame] | 40 | /* These registers are common for AK8974 and AMI30x */ |
Linus Walleij | 7c94a8b | 2016-07-25 15:54:55 +0200 | [diff] [blame] | 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 |
Michał Mirosław | 21be26f | 2017-08-17 15:56:10 +0200 | [diff] [blame] | 48 | #define AK8974_WHOAMI_VALUE_AMI306 0x46 |
Linus Walleij | 7c94a8b | 2016-07-25 15:54:55 +0200 | [diff] [blame] | 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 | |
Michał Mirosław | 21be26f | 2017-08-17 15:56:10 +0200 | [diff] [blame] | 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 | |
Linus Walleij | 7c94a8b | 2016-07-25 15:54:55 +0200 | [diff] [blame] | 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 | }; |
| 188 | |
| 189 | static const char ak8974_reg_avdd[] = "avdd"; |
| 190 | static const char ak8974_reg_dvdd[] = "dvdd"; |
| 191 | |
Michał Mirosław | 21be26f | 2017-08-17 15:56:10 +0200 | [diff] [blame] | 192 | static int ak8974_get_u16_val(struct ak8974 *ak8974, u8 reg, u16 *val) |
| 193 | { |
| 194 | int ret; |
| 195 | __le16 bulk; |
| 196 | |
| 197 | ret = regmap_bulk_read(ak8974->map, reg, &bulk, 2); |
| 198 | if (ret) |
| 199 | return ret; |
| 200 | *val = le16_to_cpu(bulk); |
| 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | static int ak8974_set_u16_val(struct ak8974 *ak8974, u8 reg, u16 val) |
| 206 | { |
| 207 | __le16 bulk = cpu_to_le16(val); |
| 208 | |
| 209 | return regmap_bulk_write(ak8974->map, reg, &bulk, 2); |
| 210 | } |
| 211 | |
Linus Walleij | 7c94a8b | 2016-07-25 15:54:55 +0200 | [diff] [blame] | 212 | static int ak8974_set_power(struct ak8974 *ak8974, bool mode) |
| 213 | { |
| 214 | int ret; |
| 215 | u8 val; |
| 216 | |
| 217 | val = mode ? AK8974_CTRL1_POWER : 0; |
| 218 | val |= AK8974_CTRL1_FORCE_EN; |
| 219 | ret = regmap_write(ak8974->map, AK8974_CTRL1, val); |
| 220 | if (ret < 0) |
| 221 | return ret; |
| 222 | |
| 223 | if (mode) |
| 224 | msleep(AK8974_ACTIVATE_DELAY); |
| 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | static int ak8974_reset(struct ak8974 *ak8974) |
| 230 | { |
| 231 | int ret; |
| 232 | |
| 233 | /* Power on to get register access. Sets CTRL1 reg to reset state */ |
| 234 | ret = ak8974_set_power(ak8974, AK8974_PWR_ON); |
| 235 | if (ret) |
| 236 | return ret; |
| 237 | ret = regmap_write(ak8974->map, AK8974_CTRL2, AK8974_CTRL2_RESDEF); |
| 238 | if (ret) |
| 239 | return ret; |
| 240 | ret = regmap_write(ak8974->map, AK8974_CTRL3, AK8974_CTRL3_RESDEF); |
| 241 | if (ret) |
| 242 | return ret; |
| 243 | ret = regmap_write(ak8974->map, AK8974_INT_CTRL, |
| 244 | AK8974_INT_CTRL_RESDEF); |
| 245 | if (ret) |
| 246 | return ret; |
| 247 | |
| 248 | /* After reset, power off is default state */ |
| 249 | return ak8974_set_power(ak8974, AK8974_PWR_OFF); |
| 250 | } |
| 251 | |
| 252 | static int ak8974_configure(struct ak8974 *ak8974) |
| 253 | { |
| 254 | int ret; |
| 255 | |
| 256 | ret = regmap_write(ak8974->map, AK8974_CTRL2, AK8974_CTRL2_DRDY_EN | |
| 257 | AK8974_CTRL2_INT_EN); |
| 258 | if (ret) |
| 259 | return ret; |
| 260 | ret = regmap_write(ak8974->map, AK8974_CTRL3, 0); |
| 261 | if (ret) |
| 262 | return ret; |
Michał Mirosław | 21be26f | 2017-08-17 15:56:10 +0200 | [diff] [blame] | 263 | if (ak8974->variant == AK8974_WHOAMI_VALUE_AMI306) { |
| 264 | /* magic from datasheet: set high-speed measurement mode */ |
| 265 | ret = ak8974_set_u16_val(ak8974, AMI306_CTRL4, 0xA07E); |
| 266 | if (ret) |
| 267 | return ret; |
| 268 | } |
Linus Walleij | 7c94a8b | 2016-07-25 15:54:55 +0200 | [diff] [blame] | 269 | ret = regmap_write(ak8974->map, AK8974_INT_CTRL, AK8974_INT_CTRL_POL); |
| 270 | if (ret) |
| 271 | return ret; |
| 272 | |
| 273 | return regmap_write(ak8974->map, AK8974_PRESET, 0); |
| 274 | } |
| 275 | |
| 276 | static int ak8974_trigmeas(struct ak8974 *ak8974) |
| 277 | { |
| 278 | unsigned int clear; |
| 279 | u8 mask; |
| 280 | u8 val; |
| 281 | int ret; |
| 282 | |
| 283 | /* Clear any previous measurement overflow status */ |
| 284 | ret = regmap_read(ak8974->map, AK8974_INT_CLEAR, &clear); |
| 285 | if (ret) |
| 286 | return ret; |
| 287 | |
| 288 | /* If we have a DRDY IRQ line, use it */ |
| 289 | if (ak8974->drdy_irq) { |
| 290 | mask = AK8974_CTRL2_INT_EN | |
| 291 | AK8974_CTRL2_DRDY_EN | |
| 292 | AK8974_CTRL2_DRDY_POL; |
| 293 | val = AK8974_CTRL2_DRDY_EN; |
| 294 | |
| 295 | if (!ak8974->drdy_active_low) |
| 296 | val |= AK8974_CTRL2_DRDY_POL; |
| 297 | |
| 298 | init_completion(&ak8974->drdy_complete); |
| 299 | ret = regmap_update_bits(ak8974->map, AK8974_CTRL2, |
| 300 | mask, val); |
| 301 | if (ret) |
| 302 | return ret; |
| 303 | } |
| 304 | |
| 305 | /* Force a measurement */ |
| 306 | return regmap_update_bits(ak8974->map, |
| 307 | AK8974_CTRL3, |
| 308 | AK8974_CTRL3_FORCE, |
| 309 | AK8974_CTRL3_FORCE); |
| 310 | } |
| 311 | |
| 312 | static int ak8974_await_drdy(struct ak8974 *ak8974) |
| 313 | { |
| 314 | int timeout = 2; |
| 315 | unsigned int val; |
| 316 | int ret; |
| 317 | |
| 318 | if (ak8974->drdy_irq) { |
| 319 | ret = wait_for_completion_timeout(&ak8974->drdy_complete, |
| 320 | 1 + msecs_to_jiffies(1000)); |
| 321 | if (!ret) { |
| 322 | dev_err(&ak8974->i2c->dev, |
| 323 | "timeout waiting for DRDY IRQ\n"); |
| 324 | return -ETIMEDOUT; |
| 325 | } |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | /* Default delay-based poll loop */ |
| 330 | do { |
| 331 | msleep(AK8974_MEASTIME); |
| 332 | ret = regmap_read(ak8974->map, AK8974_STATUS, &val); |
| 333 | if (ret < 0) |
| 334 | return ret; |
| 335 | if (val & AK8974_STATUS_DRDY) |
| 336 | return 0; |
| 337 | } while (--timeout); |
Linus Walleij | 7c94a8b | 2016-07-25 15:54:55 +0200 | [diff] [blame] | 338 | |
Colin Ian King | e2eb179 | 2017-01-11 17:49:33 +0000 | [diff] [blame] | 339 | dev_err(&ak8974->i2c->dev, "timeout waiting for DRDY\n"); |
| 340 | return -ETIMEDOUT; |
Linus Walleij | 7c94a8b | 2016-07-25 15:54:55 +0200 | [diff] [blame] | 341 | } |
| 342 | |
Ico Doornekamp | 7f709dc | 2016-10-01 19:30:14 +0200 | [diff] [blame] | 343 | static int ak8974_getresult(struct ak8974 *ak8974, __le16 *result) |
Linus Walleij | 7c94a8b | 2016-07-25 15:54:55 +0200 | [diff] [blame] | 344 | { |
| 345 | unsigned int src; |
| 346 | int ret; |
| 347 | |
| 348 | ret = ak8974_await_drdy(ak8974); |
| 349 | if (ret) |
| 350 | return ret; |
| 351 | ret = regmap_read(ak8974->map, AK8974_INT_SRC, &src); |
| 352 | if (ret < 0) |
| 353 | return ret; |
| 354 | |
| 355 | /* Out of range overflow! Strong magnet close? */ |
| 356 | if (src & AK8974_INT_RANGE) { |
| 357 | dev_err(&ak8974->i2c->dev, |
| 358 | "range overflow in sensor\n"); |
| 359 | return -ERANGE; |
| 360 | } |
| 361 | |
| 362 | ret = regmap_bulk_read(ak8974->map, AK8974_DATA_X, result, 6); |
| 363 | if (ret) |
| 364 | return ret; |
| 365 | |
| 366 | return ret; |
| 367 | } |
| 368 | |
| 369 | static irqreturn_t ak8974_drdy_irq(int irq, void *d) |
| 370 | { |
| 371 | struct ak8974 *ak8974 = d; |
| 372 | |
| 373 | if (!ak8974->drdy_irq) |
| 374 | return IRQ_NONE; |
| 375 | |
| 376 | /* TODO: timestamp here to get good measurement stamps */ |
| 377 | return IRQ_WAKE_THREAD; |
| 378 | } |
| 379 | |
| 380 | static irqreturn_t ak8974_drdy_irq_thread(int irq, void *d) |
| 381 | { |
| 382 | struct ak8974 *ak8974 = d; |
| 383 | unsigned int val; |
| 384 | int ret; |
| 385 | |
| 386 | /* Check if this was a DRDY from us */ |
| 387 | ret = regmap_read(ak8974->map, AK8974_STATUS, &val); |
| 388 | if (ret < 0) { |
| 389 | dev_err(&ak8974->i2c->dev, "error reading DRDY status\n"); |
| 390 | return IRQ_HANDLED; |
| 391 | } |
| 392 | if (val & AK8974_STATUS_DRDY) { |
| 393 | /* Yes this was our IRQ */ |
| 394 | complete(&ak8974->drdy_complete); |
| 395 | return IRQ_HANDLED; |
| 396 | } |
| 397 | |
| 398 | /* We may be on a shared IRQ, let the next client check */ |
| 399 | return IRQ_NONE; |
| 400 | } |
| 401 | |
| 402 | static int ak8974_selftest(struct ak8974 *ak8974) |
| 403 | { |
| 404 | struct device *dev = &ak8974->i2c->dev; |
| 405 | unsigned int val; |
| 406 | int ret; |
| 407 | |
| 408 | ret = regmap_read(ak8974->map, AK8974_SELFTEST, &val); |
| 409 | if (ret) |
| 410 | return ret; |
| 411 | if (val != AK8974_SELFTEST_IDLE) { |
| 412 | dev_err(dev, "selftest not idle before test\n"); |
| 413 | return -EIO; |
| 414 | } |
| 415 | |
| 416 | /* Trigger self-test */ |
| 417 | ret = regmap_update_bits(ak8974->map, |
| 418 | AK8974_CTRL3, |
| 419 | AK8974_CTRL3_SELFTEST, |
| 420 | AK8974_CTRL3_SELFTEST); |
| 421 | if (ret) { |
| 422 | dev_err(dev, "could not write CTRL3\n"); |
| 423 | return ret; |
| 424 | } |
| 425 | |
| 426 | msleep(AK8974_SELFTEST_DELAY); |
| 427 | |
| 428 | ret = regmap_read(ak8974->map, AK8974_SELFTEST, &val); |
| 429 | if (ret) |
| 430 | return ret; |
| 431 | if (val != AK8974_SELFTEST_OK) { |
| 432 | dev_err(dev, "selftest result NOT OK (%02x)\n", val); |
| 433 | return -EIO; |
| 434 | } |
| 435 | |
| 436 | ret = regmap_read(ak8974->map, AK8974_SELFTEST, &val); |
| 437 | if (ret) |
| 438 | return ret; |
| 439 | if (val != AK8974_SELFTEST_IDLE) { |
| 440 | dev_err(dev, "selftest not idle after test (%02x)\n", val); |
| 441 | return -EIO; |
| 442 | } |
| 443 | dev_dbg(dev, "passed self-test\n"); |
| 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | |
Linus Walleij | 7c94a8b | 2016-07-25 15:54:55 +0200 | [diff] [blame] | 448 | static int ak8974_detect(struct ak8974 *ak8974) |
| 449 | { |
| 450 | unsigned int whoami; |
| 451 | const char *name; |
| 452 | int ret; |
| 453 | unsigned int fw; |
| 454 | u16 sn; |
| 455 | |
| 456 | ret = regmap_read(ak8974->map, AK8974_WHOAMI, &whoami); |
| 457 | if (ret) |
| 458 | return ret; |
| 459 | |
Michał Mirosław | 21be26f | 2017-08-17 15:56:10 +0200 | [diff] [blame] | 460 | name = "ami305"; |
| 461 | |
Linus Walleij | 7c94a8b | 2016-07-25 15:54:55 +0200 | [diff] [blame] | 462 | switch (whoami) { |
Michał Mirosław | 21be26f | 2017-08-17 15:56:10 +0200 | [diff] [blame] | 463 | case AK8974_WHOAMI_VALUE_AMI306: |
| 464 | name = "ami306"; |
| 465 | /* fall-through */ |
Linus Walleij | 7c94a8b | 2016-07-25 15:54:55 +0200 | [diff] [blame] | 466 | case AK8974_WHOAMI_VALUE_AMI305: |
Linus Walleij | 7c94a8b | 2016-07-25 15:54:55 +0200 | [diff] [blame] | 467 | ret = regmap_read(ak8974->map, AMI305_VER, &fw); |
| 468 | if (ret) |
| 469 | return ret; |
| 470 | fw &= 0x7f; /* only bits 0 thru 6 valid */ |
| 471 | ret = ak8974_get_u16_val(ak8974, AMI305_SN, &sn); |
| 472 | if (ret) |
| 473 | return ret; |
Michał Mirosław | 408cc6e | 2017-08-17 15:56:11 +0200 | [diff] [blame^] | 474 | add_device_randomness(&sn, sizeof(sn)); |
Linus Walleij | 7c94a8b | 2016-07-25 15:54:55 +0200 | [diff] [blame] | 475 | dev_info(&ak8974->i2c->dev, |
| 476 | "detected %s, FW ver %02x, S/N: %04x\n", |
| 477 | name, fw, sn); |
| 478 | break; |
| 479 | case AK8974_WHOAMI_VALUE_AK8974: |
| 480 | name = "ak8974"; |
| 481 | dev_info(&ak8974->i2c->dev, "detected AK8974\n"); |
| 482 | break; |
| 483 | default: |
| 484 | dev_err(&ak8974->i2c->dev, "unsupported device (%02x) ", |
| 485 | whoami); |
| 486 | return -ENODEV; |
| 487 | } |
| 488 | |
| 489 | ak8974->name = name; |
| 490 | ak8974->variant = whoami; |
| 491 | |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | static int ak8974_read_raw(struct iio_dev *indio_dev, |
| 496 | struct iio_chan_spec const *chan, |
| 497 | int *val, int *val2, |
| 498 | long mask) |
| 499 | { |
| 500 | struct ak8974 *ak8974 = iio_priv(indio_dev); |
Ico Doornekamp | 7f709dc | 2016-10-01 19:30:14 +0200 | [diff] [blame] | 501 | __le16 hw_values[3]; |
Linus Walleij | 7c94a8b | 2016-07-25 15:54:55 +0200 | [diff] [blame] | 502 | int ret = -EINVAL; |
| 503 | |
| 504 | pm_runtime_get_sync(&ak8974->i2c->dev); |
| 505 | mutex_lock(&ak8974->lock); |
| 506 | |
| 507 | switch (mask) { |
| 508 | case IIO_CHAN_INFO_RAW: |
| 509 | if (chan->address > 2) { |
| 510 | dev_err(&ak8974->i2c->dev, "faulty channel address\n"); |
| 511 | ret = -EIO; |
| 512 | goto out_unlock; |
| 513 | } |
| 514 | ret = ak8974_trigmeas(ak8974); |
| 515 | if (ret) |
| 516 | goto out_unlock; |
| 517 | ret = ak8974_getresult(ak8974, hw_values); |
| 518 | if (ret) |
| 519 | goto out_unlock; |
| 520 | |
| 521 | /* |
| 522 | * We read all axes and discard all but one, for optimized |
| 523 | * reading, use the triggered buffer. |
| 524 | */ |
| 525 | *val = le16_to_cpu(hw_values[chan->address]); |
| 526 | |
| 527 | ret = IIO_VAL_INT; |
| 528 | } |
| 529 | |
| 530 | out_unlock: |
| 531 | mutex_unlock(&ak8974->lock); |
| 532 | pm_runtime_mark_last_busy(&ak8974->i2c->dev); |
| 533 | pm_runtime_put_autosuspend(&ak8974->i2c->dev); |
| 534 | |
| 535 | return ret; |
| 536 | } |
| 537 | |
| 538 | static void ak8974_fill_buffer(struct iio_dev *indio_dev) |
| 539 | { |
| 540 | struct ak8974 *ak8974 = iio_priv(indio_dev); |
| 541 | int ret; |
Ico Doornekamp | 7f709dc | 2016-10-01 19:30:14 +0200 | [diff] [blame] | 542 | __le16 hw_values[8]; /* Three axes + 64bit padding */ |
Linus Walleij | 7c94a8b | 2016-07-25 15:54:55 +0200 | [diff] [blame] | 543 | |
| 544 | pm_runtime_get_sync(&ak8974->i2c->dev); |
| 545 | mutex_lock(&ak8974->lock); |
| 546 | |
| 547 | ret = ak8974_trigmeas(ak8974); |
| 548 | if (ret) { |
| 549 | dev_err(&ak8974->i2c->dev, "error triggering measure\n"); |
| 550 | goto out_unlock; |
| 551 | } |
| 552 | ret = ak8974_getresult(ak8974, hw_values); |
| 553 | if (ret) { |
| 554 | dev_err(&ak8974->i2c->dev, "error getting measures\n"); |
| 555 | goto out_unlock; |
| 556 | } |
| 557 | |
| 558 | iio_push_to_buffers_with_timestamp(indio_dev, hw_values, |
| 559 | iio_get_time_ns(indio_dev)); |
| 560 | |
| 561 | out_unlock: |
| 562 | mutex_unlock(&ak8974->lock); |
| 563 | pm_runtime_mark_last_busy(&ak8974->i2c->dev); |
| 564 | pm_runtime_put_autosuspend(&ak8974->i2c->dev); |
| 565 | } |
| 566 | |
| 567 | static irqreturn_t ak8974_handle_trigger(int irq, void *p) |
| 568 | { |
| 569 | const struct iio_poll_func *pf = p; |
| 570 | struct iio_dev *indio_dev = pf->indio_dev; |
| 571 | |
| 572 | ak8974_fill_buffer(indio_dev); |
| 573 | iio_trigger_notify_done(indio_dev->trig); |
| 574 | |
| 575 | return IRQ_HANDLED; |
| 576 | } |
| 577 | |
| 578 | static const struct iio_mount_matrix * |
| 579 | ak8974_get_mount_matrix(const struct iio_dev *indio_dev, |
| 580 | const struct iio_chan_spec *chan) |
| 581 | { |
| 582 | struct ak8974 *ak8974 = iio_priv(indio_dev); |
| 583 | |
| 584 | return &ak8974->orientation; |
| 585 | } |
| 586 | |
| 587 | static const struct iio_chan_spec_ext_info ak8974_ext_info[] = { |
| 588 | IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, ak8974_get_mount_matrix), |
| 589 | { }, |
| 590 | }; |
| 591 | |
| 592 | #define AK8974_AXIS_CHANNEL(axis, index) \ |
| 593 | { \ |
| 594 | .type = IIO_MAGN, \ |
| 595 | .modified = 1, \ |
| 596 | .channel2 = IIO_MOD_##axis, \ |
| 597 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 598 | .ext_info = ak8974_ext_info, \ |
| 599 | .address = index, \ |
| 600 | .scan_index = index, \ |
| 601 | .scan_type = { \ |
| 602 | .sign = 's', \ |
| 603 | .realbits = 16, \ |
| 604 | .storagebits = 16, \ |
| 605 | .endianness = IIO_LE \ |
| 606 | }, \ |
| 607 | } |
| 608 | |
| 609 | static const struct iio_chan_spec ak8974_channels[] = { |
| 610 | AK8974_AXIS_CHANNEL(X, 0), |
| 611 | AK8974_AXIS_CHANNEL(Y, 1), |
| 612 | AK8974_AXIS_CHANNEL(Z, 2), |
| 613 | IIO_CHAN_SOFT_TIMESTAMP(3), |
| 614 | }; |
| 615 | |
| 616 | static const unsigned long ak8974_scan_masks[] = { 0x7, 0 }; |
| 617 | |
| 618 | static const struct iio_info ak8974_info = { |
| 619 | .read_raw = &ak8974_read_raw, |
| 620 | .driver_module = THIS_MODULE, |
| 621 | }; |
| 622 | |
| 623 | static bool ak8974_writeable_reg(struct device *dev, unsigned int reg) |
| 624 | { |
| 625 | struct i2c_client *i2c = to_i2c_client(dev); |
| 626 | struct iio_dev *indio_dev = i2c_get_clientdata(i2c); |
| 627 | struct ak8974 *ak8974 = iio_priv(indio_dev); |
| 628 | |
| 629 | switch (reg) { |
| 630 | case AK8974_CTRL1: |
| 631 | case AK8974_CTRL2: |
| 632 | case AK8974_CTRL3: |
| 633 | case AK8974_INT_CTRL: |
| 634 | case AK8974_INT_THRES: |
| 635 | case AK8974_INT_THRES + 1: |
| 636 | case AK8974_PRESET: |
| 637 | case AK8974_PRESET + 1: |
| 638 | return true; |
| 639 | case AK8974_OFFSET_X: |
| 640 | case AK8974_OFFSET_X + 1: |
| 641 | case AK8974_OFFSET_Y: |
| 642 | case AK8974_OFFSET_Y + 1: |
| 643 | case AK8974_OFFSET_Z: |
| 644 | case AK8974_OFFSET_Z + 1: |
| 645 | if (ak8974->variant == AK8974_WHOAMI_VALUE_AK8974) |
| 646 | return true; |
| 647 | return false; |
| 648 | case AMI305_OFFSET_X: |
| 649 | case AMI305_OFFSET_X + 1: |
| 650 | case AMI305_OFFSET_Y: |
| 651 | case AMI305_OFFSET_Y + 1: |
| 652 | case AMI305_OFFSET_Z: |
| 653 | case AMI305_OFFSET_Z + 1: |
Michał Mirosław | 21be26f | 2017-08-17 15:56:10 +0200 | [diff] [blame] | 654 | return ak8974->variant == AK8974_WHOAMI_VALUE_AMI305 || |
| 655 | ak8974->variant == AK8974_WHOAMI_VALUE_AMI306; |
| 656 | case AMI306_CTRL4: |
| 657 | case AMI306_CTRL4 + 1: |
| 658 | return ak8974->variant == AK8974_WHOAMI_VALUE_AMI306; |
Linus Walleij | 7c94a8b | 2016-07-25 15:54:55 +0200 | [diff] [blame] | 659 | default: |
| 660 | return false; |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | static const struct regmap_config ak8974_regmap_config = { |
| 665 | .reg_bits = 8, |
| 666 | .val_bits = 8, |
| 667 | .max_register = 0xff, |
| 668 | .writeable_reg = ak8974_writeable_reg, |
| 669 | }; |
| 670 | |
| 671 | static int ak8974_probe(struct i2c_client *i2c, |
| 672 | const struct i2c_device_id *id) |
| 673 | { |
| 674 | struct iio_dev *indio_dev; |
| 675 | struct ak8974 *ak8974; |
| 676 | unsigned long irq_trig; |
| 677 | int irq = i2c->irq; |
| 678 | int ret; |
| 679 | |
| 680 | /* Register with IIO */ |
| 681 | indio_dev = devm_iio_device_alloc(&i2c->dev, sizeof(*ak8974)); |
| 682 | if (indio_dev == NULL) |
| 683 | return -ENOMEM; |
| 684 | |
| 685 | ak8974 = iio_priv(indio_dev); |
| 686 | i2c_set_clientdata(i2c, indio_dev); |
| 687 | ak8974->i2c = i2c; |
| 688 | mutex_init(&ak8974->lock); |
| 689 | |
| 690 | ret = of_iio_read_mount_matrix(&i2c->dev, |
| 691 | "mount-matrix", |
| 692 | &ak8974->orientation); |
| 693 | if (ret) |
| 694 | return ret; |
| 695 | |
| 696 | ak8974->regs[0].supply = ak8974_reg_avdd; |
| 697 | ak8974->regs[1].supply = ak8974_reg_dvdd; |
| 698 | |
| 699 | ret = devm_regulator_bulk_get(&i2c->dev, |
| 700 | ARRAY_SIZE(ak8974->regs), |
| 701 | ak8974->regs); |
| 702 | if (ret < 0) { |
| 703 | dev_err(&i2c->dev, "cannot get regulators\n"); |
| 704 | return ret; |
| 705 | } |
| 706 | |
| 707 | ret = regulator_bulk_enable(ARRAY_SIZE(ak8974->regs), ak8974->regs); |
| 708 | if (ret < 0) { |
| 709 | dev_err(&i2c->dev, "cannot enable regulators\n"); |
| 710 | return ret; |
| 711 | } |
| 712 | |
| 713 | /* Take runtime PM online */ |
| 714 | pm_runtime_get_noresume(&i2c->dev); |
| 715 | pm_runtime_set_active(&i2c->dev); |
| 716 | pm_runtime_enable(&i2c->dev); |
| 717 | |
| 718 | ak8974->map = devm_regmap_init_i2c(i2c, &ak8974_regmap_config); |
| 719 | if (IS_ERR(ak8974->map)) { |
| 720 | dev_err(&i2c->dev, "failed to allocate register map\n"); |
| 721 | return PTR_ERR(ak8974->map); |
| 722 | } |
| 723 | |
| 724 | ret = ak8974_set_power(ak8974, AK8974_PWR_ON); |
| 725 | if (ret) { |
| 726 | dev_err(&i2c->dev, "could not power on\n"); |
| 727 | goto power_off; |
| 728 | } |
| 729 | |
| 730 | ret = ak8974_detect(ak8974); |
| 731 | if (ret) { |
Michał Mirosław | 21be26f | 2017-08-17 15:56:10 +0200 | [diff] [blame] | 732 | dev_err(&i2c->dev, "neither AK8974 nor AMI30x found\n"); |
Linus Walleij | 7c94a8b | 2016-07-25 15:54:55 +0200 | [diff] [blame] | 733 | goto power_off; |
| 734 | } |
| 735 | |
| 736 | ret = ak8974_selftest(ak8974); |
| 737 | if (ret) |
| 738 | dev_err(&i2c->dev, "selftest failed (continuing anyway)\n"); |
| 739 | |
| 740 | ret = ak8974_reset(ak8974); |
| 741 | if (ret) { |
| 742 | dev_err(&i2c->dev, "AK8974 reset failed\n"); |
| 743 | goto power_off; |
| 744 | } |
| 745 | |
| 746 | pm_runtime_set_autosuspend_delay(&i2c->dev, |
| 747 | AK8974_AUTOSUSPEND_DELAY); |
| 748 | pm_runtime_use_autosuspend(&i2c->dev); |
| 749 | pm_runtime_put(&i2c->dev); |
| 750 | |
| 751 | indio_dev->dev.parent = &i2c->dev; |
| 752 | indio_dev->channels = ak8974_channels; |
| 753 | indio_dev->num_channels = ARRAY_SIZE(ak8974_channels); |
| 754 | indio_dev->info = &ak8974_info; |
| 755 | indio_dev->available_scan_masks = ak8974_scan_masks; |
| 756 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 757 | indio_dev->name = ak8974->name; |
| 758 | |
| 759 | ret = iio_triggered_buffer_setup(indio_dev, NULL, |
| 760 | ak8974_handle_trigger, |
| 761 | NULL); |
| 762 | if (ret) { |
| 763 | dev_err(&i2c->dev, "triggered buffer setup failed\n"); |
| 764 | goto disable_pm; |
| 765 | } |
| 766 | |
| 767 | /* If we have a valid DRDY IRQ, make use of it */ |
| 768 | if (irq > 0) { |
| 769 | irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq)); |
| 770 | if (irq_trig == IRQF_TRIGGER_RISING) { |
| 771 | dev_info(&i2c->dev, "enable rising edge DRDY IRQ\n"); |
| 772 | } else if (irq_trig == IRQF_TRIGGER_FALLING) { |
| 773 | ak8974->drdy_active_low = true; |
| 774 | dev_info(&i2c->dev, "enable falling edge DRDY IRQ\n"); |
| 775 | } else { |
| 776 | irq_trig = IRQF_TRIGGER_RISING; |
| 777 | } |
| 778 | irq_trig |= IRQF_ONESHOT; |
| 779 | irq_trig |= IRQF_SHARED; |
| 780 | |
| 781 | ret = devm_request_threaded_irq(&i2c->dev, |
| 782 | irq, |
| 783 | ak8974_drdy_irq, |
| 784 | ak8974_drdy_irq_thread, |
| 785 | irq_trig, |
| 786 | ak8974->name, |
| 787 | ak8974); |
| 788 | if (ret) { |
| 789 | dev_err(&i2c->dev, "unable to request DRDY IRQ " |
| 790 | "- proceeding without IRQ\n"); |
| 791 | goto no_irq; |
| 792 | } |
| 793 | ak8974->drdy_irq = true; |
| 794 | } |
| 795 | |
| 796 | no_irq: |
| 797 | ret = iio_device_register(indio_dev); |
| 798 | if (ret) { |
| 799 | dev_err(&i2c->dev, "device register failed\n"); |
| 800 | goto cleanup_buffer; |
| 801 | } |
| 802 | |
| 803 | return 0; |
| 804 | |
| 805 | cleanup_buffer: |
| 806 | iio_triggered_buffer_cleanup(indio_dev); |
| 807 | disable_pm: |
| 808 | pm_runtime_put_noidle(&i2c->dev); |
| 809 | pm_runtime_disable(&i2c->dev); |
| 810 | ak8974_set_power(ak8974, AK8974_PWR_OFF); |
| 811 | power_off: |
| 812 | regulator_bulk_disable(ARRAY_SIZE(ak8974->regs), ak8974->regs); |
| 813 | |
| 814 | return ret; |
| 815 | } |
| 816 | |
Dmitry Torokhov | 3ff861f | 2017-03-01 15:37:57 -0800 | [diff] [blame] | 817 | static int ak8974_remove(struct i2c_client *i2c) |
Linus Walleij | 7c94a8b | 2016-07-25 15:54:55 +0200 | [diff] [blame] | 818 | { |
| 819 | struct iio_dev *indio_dev = i2c_get_clientdata(i2c); |
| 820 | struct ak8974 *ak8974 = iio_priv(indio_dev); |
| 821 | |
| 822 | iio_device_unregister(indio_dev); |
| 823 | iio_triggered_buffer_cleanup(indio_dev); |
| 824 | pm_runtime_get_sync(&i2c->dev); |
| 825 | pm_runtime_put_noidle(&i2c->dev); |
| 826 | pm_runtime_disable(&i2c->dev); |
| 827 | ak8974_set_power(ak8974, AK8974_PWR_OFF); |
| 828 | regulator_bulk_disable(ARRAY_SIZE(ak8974->regs), ak8974->regs); |
| 829 | |
| 830 | return 0; |
| 831 | } |
| 832 | |
Arnd Bergmann | 5bc55ef | 2016-08-26 17:31:19 +0200 | [diff] [blame] | 833 | static int __maybe_unused ak8974_runtime_suspend(struct device *dev) |
Linus Walleij | 7c94a8b | 2016-07-25 15:54:55 +0200 | [diff] [blame] | 834 | { |
| 835 | struct ak8974 *ak8974 = |
| 836 | iio_priv(i2c_get_clientdata(to_i2c_client(dev))); |
| 837 | |
| 838 | ak8974_set_power(ak8974, AK8974_PWR_OFF); |
| 839 | regulator_bulk_disable(ARRAY_SIZE(ak8974->regs), ak8974->regs); |
| 840 | |
| 841 | return 0; |
| 842 | } |
| 843 | |
Arnd Bergmann | 5bc55ef | 2016-08-26 17:31:19 +0200 | [diff] [blame] | 844 | static int __maybe_unused ak8974_runtime_resume(struct device *dev) |
Linus Walleij | 7c94a8b | 2016-07-25 15:54:55 +0200 | [diff] [blame] | 845 | { |
| 846 | struct ak8974 *ak8974 = |
| 847 | iio_priv(i2c_get_clientdata(to_i2c_client(dev))); |
| 848 | int ret; |
| 849 | |
| 850 | ret = regulator_bulk_enable(ARRAY_SIZE(ak8974->regs), ak8974->regs); |
| 851 | if (ret) |
| 852 | return ret; |
| 853 | msleep(AK8974_POWERON_DELAY); |
| 854 | ret = ak8974_set_power(ak8974, AK8974_PWR_ON); |
| 855 | if (ret) |
| 856 | goto out_regulator_disable; |
| 857 | |
| 858 | ret = ak8974_configure(ak8974); |
| 859 | if (ret) |
| 860 | goto out_disable_power; |
| 861 | |
| 862 | return 0; |
| 863 | |
| 864 | out_disable_power: |
| 865 | ak8974_set_power(ak8974, AK8974_PWR_OFF); |
| 866 | out_regulator_disable: |
| 867 | regulator_bulk_disable(ARRAY_SIZE(ak8974->regs), ak8974->regs); |
| 868 | |
| 869 | return ret; |
| 870 | } |
Linus Walleij | 7c94a8b | 2016-07-25 15:54:55 +0200 | [diff] [blame] | 871 | |
| 872 | static const struct dev_pm_ops ak8974_dev_pm_ops = { |
| 873 | SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, |
| 874 | pm_runtime_force_resume) |
| 875 | SET_RUNTIME_PM_OPS(ak8974_runtime_suspend, |
| 876 | ak8974_runtime_resume, NULL) |
| 877 | }; |
| 878 | |
| 879 | static const struct i2c_device_id ak8974_id[] = { |
| 880 | {"ami305", 0 }, |
Michał Mirosław | 21be26f | 2017-08-17 15:56:10 +0200 | [diff] [blame] | 881 | {"ami306", 0 }, |
Linus Walleij | 7c94a8b | 2016-07-25 15:54:55 +0200 | [diff] [blame] | 882 | {"ak8974", 0 }, |
| 883 | {} |
| 884 | }; |
| 885 | MODULE_DEVICE_TABLE(i2c, ak8974_id); |
| 886 | |
| 887 | static const struct of_device_id ak8974_of_match[] = { |
| 888 | { .compatible = "asahi-kasei,ak8974", }, |
| 889 | {} |
| 890 | }; |
| 891 | MODULE_DEVICE_TABLE(of, ak8974_of_match); |
| 892 | |
| 893 | static struct i2c_driver ak8974_driver = { |
| 894 | .driver = { |
| 895 | .name = "ak8974", |
Linus Walleij | 7c94a8b | 2016-07-25 15:54:55 +0200 | [diff] [blame] | 896 | .pm = &ak8974_dev_pm_ops, |
| 897 | .of_match_table = of_match_ptr(ak8974_of_match), |
| 898 | }, |
| 899 | .probe = ak8974_probe, |
Dmitry Torokhov | 3ff861f | 2017-03-01 15:37:57 -0800 | [diff] [blame] | 900 | .remove = ak8974_remove, |
Linus Walleij | 7c94a8b | 2016-07-25 15:54:55 +0200 | [diff] [blame] | 901 | .id_table = ak8974_id, |
| 902 | }; |
| 903 | module_i2c_driver(ak8974_driver); |
| 904 | |
Michał Mirosław | 21be26f | 2017-08-17 15:56:10 +0200 | [diff] [blame] | 905 | MODULE_DESCRIPTION("AK8974 and AMI30x 3-axis magnetometer driver"); |
Linus Walleij | 7c94a8b | 2016-07-25 15:54:55 +0200 | [diff] [blame] | 906 | MODULE_AUTHOR("Samu Onkalo"); |
| 907 | MODULE_AUTHOR("Linus Walleij"); |
| 908 | MODULE_LICENSE("GPL v2"); |