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