1 // SPDX-License-Identifier: GPL-2.0-only
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.
7 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
8 * Copyright (c) 2010 NVIDIA Corporation.
9 * Copyright (C) 2016 Linaro Ltd.
11 * Author: Samu Onkalo <samu.p.onkalo@nokia.com>
12 * Author: Linus Walleij <linus.walleij@linaro.org>
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>
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>
38 * 16-bit registers are little-endian. LSB is at the address defined below
39 * and MSB is at the next higher address.
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
47 #define AK8974_INFO 0x0D
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
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
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
77 /* Different temperature registers */
78 #define AK8974_TEMP 0x31
79 #define AMI305_TEMP 0x60
81 /* AMI306-specific control register */
82 #define AMI306_CTRL4 0x5C
84 /* AMI306 factory calibration data */
86 /* fine axis sensitivity */
87 #define AMI306_FINEOUTPUT_X 0x90
88 #define AMI306_FINEOUTPUT_Y 0x92
89 #define AMI306_FINEOUTPUT_Z 0x94
91 /* axis sensitivity */
92 #define AMI306_SENS_X 0x96
93 #define AMI306_SENS_Y 0x98
94 #define AMI306_SENS_Z 0x9A
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
104 /* offset at ZERO magnetic field */
105 #define AMI306_OFFZERO_X 0xF8
106 #define AMI306_OFFZERO_Y 0xFA
107 #define AMI306_OFFZERO_Z 0xFC
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) */
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 */
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 */
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)
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
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)
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)
151 /* The AMI305 has elaborate FW version and serial number registers */
152 #define AMI305_VER 0xE8
153 #define AMI305_SN 0xEA
155 #define AK8974_MAX_RANGE 2048
157 #define AK8974_POWERON_DELAY 50
158 #define AK8974_ACTIVATE_DELAY 1
159 #define AK8974_SELFTEST_DELAY 1
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
165 #define AK8974_AUTOSUSPEND_DELAY 5000
167 #define AK8974_MEASTIME 3
169 #define AK8974_PWR_ON 1
170 #define AK8974_PWR_OFF 0
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
187 struct i2c_client
*i2c
;
188 struct iio_mount_matrix orientation
;
190 struct regulator_bulk_data regs
[2];
195 struct completion drdy_complete
;
196 bool drdy_active_low
;
197 /* Ensure timestamp is naturally aligned */
204 static const char ak8974_reg_avdd
[] = "avdd";
205 static const char ak8974_reg_dvdd
[] = "dvdd";
207 static int ak8974_get_u16_val(struct ak8974
*ak8974
, u8 reg
, u16
*val
)
212 ret
= regmap_bulk_read(ak8974
->map
, reg
, &bulk
, 2);
215 *val
= le16_to_cpu(bulk
);
220 static int ak8974_set_u16_val(struct ak8974
*ak8974
, u8 reg
, u16 val
)
222 __le16 bulk
= cpu_to_le16(val
);
224 return regmap_bulk_write(ak8974
->map
, reg
, &bulk
, 2);
227 static int ak8974_set_power(struct ak8974
*ak8974
, bool mode
)
232 val
= mode
? AK8974_CTRL1_POWER
: 0;
233 val
|= AK8974_CTRL1_FORCE_EN
;
234 ret
= regmap_write(ak8974
->map
, AK8974_CTRL1
, val
);
239 msleep(AK8974_ACTIVATE_DELAY
);
244 static int ak8974_reset(struct ak8974
*ak8974
)
248 /* Power on to get register access. Sets CTRL1 reg to reset state */
249 ret
= ak8974_set_power(ak8974
, AK8974_PWR_ON
);
252 ret
= regmap_write(ak8974
->map
, AK8974_CTRL2
, AK8974_CTRL2_RESDEF
);
255 ret
= regmap_write(ak8974
->map
, AK8974_CTRL3
, AK8974_CTRL3_RESDEF
);
258 if (ak8974
->variant
!= AK8974_WHOAMI_VALUE_HSCDTD008A
) {
259 ret
= regmap_write(ak8974
->map
, AK8974_INT_CTRL
,
260 AK8974_INT_CTRL_RESDEF
);
264 ret
= regmap_write(ak8974
->map
, HSCDTD008A_CTRL4
,
265 HSCDTD008A_CTRL4_RESDEF
);
270 /* After reset, power off is default state */
271 return ak8974_set_power(ak8974
, AK8974_PWR_OFF
);
274 static int ak8974_configure(struct ak8974
*ak8974
)
278 ret
= regmap_write(ak8974
->map
, AK8974_CTRL2
, AK8974_CTRL2_DRDY_EN
|
279 AK8974_CTRL2_INT_EN
);
282 ret
= regmap_write(ak8974
->map
, AK8974_CTRL3
, 0);
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);
291 if (ak8974
->variant
== AK8974_WHOAMI_VALUE_HSCDTD008A
)
293 ret
= regmap_write(ak8974
->map
, AK8974_INT_CTRL
, AK8974_INT_CTRL_POL
);
297 return regmap_write(ak8974
->map
, AK8974_PRESET
, 0);
300 static int ak8974_trigmeas(struct ak8974
*ak8974
)
307 /* Clear any previous measurement overflow status */
308 ret
= regmap_read(ak8974
->map
, AK8974_INT_CLEAR
, &clear
);
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
;
319 if (!ak8974
->drdy_active_low
)
320 val
|= AK8974_CTRL2_DRDY_POL
;
322 init_completion(&ak8974
->drdy_complete
);
323 ret
= regmap_update_bits(ak8974
->map
, AK8974_CTRL2
,
329 /* Force a measurement */
330 return regmap_update_bits(ak8974
->map
,
336 static int ak8974_await_drdy(struct ak8974
*ak8974
)
342 if (ak8974
->drdy_irq
) {
343 ret
= wait_for_completion_timeout(&ak8974
->drdy_complete
,
344 1 + msecs_to_jiffies(1000));
346 dev_err(&ak8974
->i2c
->dev
,
347 "timeout waiting for DRDY IRQ\n");
353 /* Default delay-based poll loop */
355 msleep(AK8974_MEASTIME
);
356 ret
= regmap_read(ak8974
->map
, AK8974_STATUS
, &val
);
359 if (val
& AK8974_STATUS_DRDY
)
363 dev_err(&ak8974
->i2c
->dev
, "timeout waiting for DRDY\n");
367 static int ak8974_getresult(struct ak8974
*ak8974
, __le16
*result
)
372 ret
= ak8974_await_drdy(ak8974
);
375 ret
= regmap_read(ak8974
->map
, AK8974_INT_SRC
, &src
);
379 /* Out of range overflow! Strong magnet close? */
380 if (src
& AK8974_INT_RANGE
) {
381 dev_err(&ak8974
->i2c
->dev
,
382 "range overflow in sensor\n");
386 ret
= regmap_bulk_read(ak8974
->map
, AK8974_DATA_X
, result
, 6);
393 static irqreturn_t
ak8974_drdy_irq(int irq
, void *d
)
395 struct ak8974
*ak8974
= d
;
397 if (!ak8974
->drdy_irq
)
400 /* TODO: timestamp here to get good measurement stamps */
401 return IRQ_WAKE_THREAD
;
404 static irqreturn_t
ak8974_drdy_irq_thread(int irq
, void *d
)
406 struct ak8974
*ak8974
= d
;
410 /* Check if this was a DRDY from us */
411 ret
= regmap_read(ak8974
->map
, AK8974_STATUS
, &val
);
413 dev_err(&ak8974
->i2c
->dev
, "error reading DRDY status\n");
416 if (val
& AK8974_STATUS_DRDY
) {
417 /* Yes this was our IRQ */
418 complete(&ak8974
->drdy_complete
);
422 /* We may be on a shared IRQ, let the next client check */
426 static int ak8974_selftest(struct ak8974
*ak8974
)
428 struct device
*dev
= &ak8974
->i2c
->dev
;
432 ret
= regmap_read(ak8974
->map
, AK8974_SELFTEST
, &val
);
435 if (val
!= AK8974_SELFTEST_IDLE
) {
436 dev_err(dev
, "selftest not idle before test\n");
440 /* Trigger self-test */
441 ret
= regmap_update_bits(ak8974
->map
,
443 AK8974_CTRL3_SELFTEST
,
444 AK8974_CTRL3_SELFTEST
);
446 dev_err(dev
, "could not write CTRL3\n");
450 msleep(AK8974_SELFTEST_DELAY
);
452 ret
= regmap_read(ak8974
->map
, AK8974_SELFTEST
, &val
);
455 if (val
!= AK8974_SELFTEST_OK
) {
456 dev_err(dev
, "selftest result NOT OK (%02x)\n", val
);
460 ret
= regmap_read(ak8974
->map
, AK8974_SELFTEST
, &val
);
463 if (val
!= AK8974_SELFTEST_IDLE
) {
464 dev_err(dev
, "selftest not idle after test (%02x)\n", val
);
467 dev_dbg(dev
, "passed self-test\n");
472 static void ak8974_read_calib_data(struct ak8974
*ak8974
, unsigned int reg
,
473 __le16
*tab
, size_t tab_size
)
475 int ret
= regmap_bulk_read(ak8974
->map
, reg
, tab
, tab_size
);
477 memset(tab
, 0xFF, tab_size
);
478 dev_warn(&ak8974
->i2c
->dev
,
479 "can't read calibration data (regs %u..%zu): %d\n",
480 reg
, reg
+ tab_size
- 1, ret
);
482 add_device_randomness(tab
, tab_size
);
486 static int ak8974_detect(struct ak8974
*ak8974
)
494 ret
= regmap_read(ak8974
->map
, AK8974_WHOAMI
, &whoami
);
501 case AK8974_WHOAMI_VALUE_AMI306
:
504 case AK8974_WHOAMI_VALUE_AMI305
:
505 ret
= regmap_read(ak8974
->map
, AMI305_VER
, &fw
);
508 fw
&= 0x7f; /* only bits 0 thru 6 valid */
509 ret
= ak8974_get_u16_val(ak8974
, AMI305_SN
, &sn
);
512 add_device_randomness(&sn
, sizeof(sn
));
513 dev_info(&ak8974
->i2c
->dev
,
514 "detected %s, FW ver %02x, S/N: %04x\n",
517 case AK8974_WHOAMI_VALUE_AK8974
:
519 dev_info(&ak8974
->i2c
->dev
, "detected AK8974\n");
521 case AK8974_WHOAMI_VALUE_HSCDTD008A
:
523 dev_info(&ak8974
->i2c
->dev
, "detected hscdtd008a\n");
526 dev_err(&ak8974
->i2c
->dev
, "unsupported device (%02x) ",
532 ak8974
->variant
= whoami
;
534 if (whoami
== AK8974_WHOAMI_VALUE_AMI306
) {
535 __le16 fab_data1
[9], fab_data2
[3];
538 ak8974_read_calib_data(ak8974
, AMI306_FINEOUTPUT_X
,
539 fab_data1
, sizeof(fab_data1
));
540 ak8974_read_calib_data(ak8974
, AMI306_OFFZERO_X
,
541 fab_data2
, sizeof(fab_data2
));
543 for (i
= 0; i
< 3; ++i
) {
544 static const char axis
[3] = "XYZ";
545 static const char pgaxis
[6] = "ZYZXYX";
546 unsigned offz
= le16_to_cpu(fab_data2
[i
]) & 0x7F;
547 unsigned fine
= le16_to_cpu(fab_data1
[i
]);
548 unsigned sens
= le16_to_cpu(fab_data1
[i
+ 3]);
549 unsigned pgain1
= le16_to_cpu(fab_data1
[i
+ 6]);
550 unsigned pgain2
= pgain1
>> 8;
554 dev_info(&ak8974
->i2c
->dev
,
555 "factory calibration for axis %c: offz=%u sens=%u fine=%u pga%c=%u pga%c=%u\n",
556 axis
[i
], offz
, sens
, fine
, pgaxis
[i
* 2],
557 pgain1
, pgaxis
[i
* 2 + 1], pgain2
);
564 static int ak8974_measure_channel(struct ak8974
*ak8974
, unsigned long address
,
570 pm_runtime_get_sync(&ak8974
->i2c
->dev
);
571 mutex_lock(&ak8974
->lock
);
574 * We read all axes and discard all but one, for optimized
575 * reading, use the triggered buffer.
577 ret
= ak8974_trigmeas(ak8974
);
580 ret
= ak8974_getresult(ak8974
, hw_values
);
584 * This explicit cast to (s16) is necessary as the measurement
585 * is done in 2's complement with positive and negative values.
586 * The follwing assignment to *val will then convert the signed
587 * s16 value to a signed int value.
589 *val
= (s16
)le16_to_cpu(hw_values
[address
]);
591 mutex_unlock(&ak8974
->lock
);
592 pm_runtime_mark_last_busy(&ak8974
->i2c
->dev
);
593 pm_runtime_put_autosuspend(&ak8974
->i2c
->dev
);
598 static int ak8974_read_raw(struct iio_dev
*indio_dev
,
599 struct iio_chan_spec
const *chan
,
603 struct ak8974
*ak8974
= iio_priv(indio_dev
);
607 case IIO_CHAN_INFO_RAW
:
608 if (chan
->address
> 2) {
609 dev_err(&ak8974
->i2c
->dev
, "faulty channel address\n");
612 ret
= ak8974_measure_channel(ak8974
, chan
->address
, val
);
616 case IIO_CHAN_INFO_SCALE
:
617 switch (ak8974
->variant
) {
618 case AK8974_WHOAMI_VALUE_AMI306
:
619 case AK8974_WHOAMI_VALUE_AMI305
:
621 * The datasheet for AMI305 and AMI306, page 6
622 * specifies the range of the sensor to be
627 * 12 bits are used, +/- 2^11
628 * [ -2048 .. 2047 ] (manual page 20)
629 * [ 0xf800 .. 0x07ff ]
632 return IIO_VAL_FRACTIONAL_LOG2
;
633 case AK8974_WHOAMI_VALUE_HSCDTD008A
:
635 * The datasheet for HSCDTF008A, page 3 specifies the
636 * range of the sensor as +/- 2.4 mT per axis, which
637 * corresponds to +/- 2400 uT = +/- 24 Gauss.
641 * 15 bits are used (set up in CTRL4), +/- 2^14
642 * [ -16384 .. 16383 ] (manual page 24)
643 * [ 0xc000 .. 0x3fff ]
646 return IIO_VAL_FRACTIONAL_LOG2
;
648 /* GUESSING +/- 12 Gauss */
650 /* GUESSING 12 bits ADC +/- 2^11 */
652 return IIO_VAL_FRACTIONAL_LOG2
;
656 /* Unknown request */
663 static void ak8974_fill_buffer(struct iio_dev
*indio_dev
)
665 struct ak8974
*ak8974
= iio_priv(indio_dev
);
668 pm_runtime_get_sync(&ak8974
->i2c
->dev
);
669 mutex_lock(&ak8974
->lock
);
671 ret
= ak8974_trigmeas(ak8974
);
673 dev_err(&ak8974
->i2c
->dev
, "error triggering measure\n");
676 ret
= ak8974_getresult(ak8974
, ak8974
->scan
.channels
);
678 dev_err(&ak8974
->i2c
->dev
, "error getting measures\n");
682 iio_push_to_buffers_with_timestamp(indio_dev
, &ak8974
->scan
,
683 iio_get_time_ns(indio_dev
));
686 mutex_unlock(&ak8974
->lock
);
687 pm_runtime_mark_last_busy(&ak8974
->i2c
->dev
);
688 pm_runtime_put_autosuspend(&ak8974
->i2c
->dev
);
691 static irqreturn_t
ak8974_handle_trigger(int irq
, void *p
)
693 const struct iio_poll_func
*pf
= p
;
694 struct iio_dev
*indio_dev
= pf
->indio_dev
;
696 ak8974_fill_buffer(indio_dev
);
697 iio_trigger_notify_done(indio_dev
->trig
);
702 static const struct iio_mount_matrix
*
703 ak8974_get_mount_matrix(const struct iio_dev
*indio_dev
,
704 const struct iio_chan_spec
*chan
)
706 struct ak8974
*ak8974
= iio_priv(indio_dev
);
708 return &ak8974
->orientation
;
711 static const struct iio_chan_spec_ext_info ak8974_ext_info
[] = {
712 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR
, ak8974_get_mount_matrix
),
716 #define AK8974_AXIS_CHANNEL(axis, index, bits) \
720 .channel2 = IIO_MOD_##axis, \
721 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
722 BIT(IIO_CHAN_INFO_SCALE), \
723 .ext_info = ak8974_ext_info, \
725 .scan_index = index, \
730 .endianness = IIO_LE \
735 * We have no datasheet for the AK8974 but we guess that its
736 * ADC is 12 bits. The AMI305 and AMI306 certainly has 12bit
739 static const struct iio_chan_spec ak8974_12_bits_channels
[] = {
740 AK8974_AXIS_CHANNEL(X
, 0, 12),
741 AK8974_AXIS_CHANNEL(Y
, 1, 12),
742 AK8974_AXIS_CHANNEL(Z
, 2, 12),
743 IIO_CHAN_SOFT_TIMESTAMP(3),
747 * The HSCDTD008A has 15 bits resolution the way we set it up
750 static const struct iio_chan_spec ak8974_15_bits_channels
[] = {
751 AK8974_AXIS_CHANNEL(X
, 0, 15),
752 AK8974_AXIS_CHANNEL(Y
, 1, 15),
753 AK8974_AXIS_CHANNEL(Z
, 2, 15),
754 IIO_CHAN_SOFT_TIMESTAMP(3),
757 static const unsigned long ak8974_scan_masks
[] = { 0x7, 0 };
759 static const struct iio_info ak8974_info
= {
760 .read_raw
= &ak8974_read_raw
,
763 static bool ak8974_writeable_reg(struct device
*dev
, unsigned int reg
)
765 struct i2c_client
*i2c
= to_i2c_client(dev
);
766 struct iio_dev
*indio_dev
= i2c_get_clientdata(i2c
);
767 struct ak8974
*ak8974
= iio_priv(indio_dev
);
773 case AK8974_INT_CTRL
:
774 case AK8974_INT_THRES
:
775 case AK8974_INT_THRES
+ 1:
778 case AK8974_PRESET
+ 1:
779 return ak8974
->variant
!= AK8974_WHOAMI_VALUE_HSCDTD008A
;
780 case AK8974_OFFSET_X
:
781 case AK8974_OFFSET_X
+ 1:
782 case AK8974_OFFSET_Y
:
783 case AK8974_OFFSET_Y
+ 1:
784 case AK8974_OFFSET_Z
:
785 case AK8974_OFFSET_Z
+ 1:
786 return ak8974
->variant
== AK8974_WHOAMI_VALUE_AK8974
||
787 ak8974
->variant
== AK8974_WHOAMI_VALUE_HSCDTD008A
;
788 case AMI305_OFFSET_X
:
789 case AMI305_OFFSET_X
+ 1:
790 case AMI305_OFFSET_Y
:
791 case AMI305_OFFSET_Y
+ 1:
792 case AMI305_OFFSET_Z
:
793 case AMI305_OFFSET_Z
+ 1:
794 return ak8974
->variant
== AK8974_WHOAMI_VALUE_AMI305
||
795 ak8974
->variant
== AK8974_WHOAMI_VALUE_AMI306
;
797 case AMI306_CTRL4
+ 1:
798 return ak8974
->variant
== AK8974_WHOAMI_VALUE_AMI306
;
804 static bool ak8974_precious_reg(struct device
*dev
, unsigned int reg
)
806 return reg
== AK8974_INT_CLEAR
;
809 static const struct regmap_config ak8974_regmap_config
= {
812 .max_register
= 0xff,
813 .writeable_reg
= ak8974_writeable_reg
,
814 .precious_reg
= ak8974_precious_reg
,
817 static int ak8974_probe(struct i2c_client
*i2c
,
818 const struct i2c_device_id
*id
)
820 struct iio_dev
*indio_dev
;
821 struct ak8974
*ak8974
;
822 unsigned long irq_trig
;
826 /* Register with IIO */
827 indio_dev
= devm_iio_device_alloc(&i2c
->dev
, sizeof(*ak8974
));
828 if (indio_dev
== NULL
)
831 ak8974
= iio_priv(indio_dev
);
832 i2c_set_clientdata(i2c
, indio_dev
);
834 mutex_init(&ak8974
->lock
);
836 ret
= iio_read_mount_matrix(&i2c
->dev
, "mount-matrix",
837 &ak8974
->orientation
);
841 ak8974
->regs
[0].supply
= ak8974_reg_avdd
;
842 ak8974
->regs
[1].supply
= ak8974_reg_dvdd
;
844 ret
= devm_regulator_bulk_get(&i2c
->dev
,
845 ARRAY_SIZE(ak8974
->regs
),
848 return dev_err_probe(&i2c
->dev
, ret
, "cannot get regulators\n");
850 ret
= regulator_bulk_enable(ARRAY_SIZE(ak8974
->regs
), ak8974
->regs
);
852 dev_err(&i2c
->dev
, "cannot enable regulators\n");
856 /* Take runtime PM online */
857 pm_runtime_get_noresume(&i2c
->dev
);
858 pm_runtime_set_active(&i2c
->dev
);
859 pm_runtime_enable(&i2c
->dev
);
861 ak8974
->map
= devm_regmap_init_i2c(i2c
, &ak8974_regmap_config
);
862 if (IS_ERR(ak8974
->map
)) {
863 dev_err(&i2c
->dev
, "failed to allocate register map\n");
864 pm_runtime_put_noidle(&i2c
->dev
);
865 pm_runtime_disable(&i2c
->dev
);
866 return PTR_ERR(ak8974
->map
);
869 ret
= ak8974_set_power(ak8974
, AK8974_PWR_ON
);
871 dev_err(&i2c
->dev
, "could not power on\n");
875 ret
= ak8974_detect(ak8974
);
877 dev_err(&i2c
->dev
, "neither AK8974 nor AMI30x found\n");
881 ret
= ak8974_selftest(ak8974
);
883 dev_err(&i2c
->dev
, "selftest failed (continuing anyway)\n");
885 ret
= ak8974_reset(ak8974
);
887 dev_err(&i2c
->dev
, "AK8974 reset failed\n");
891 switch (ak8974
->variant
) {
892 case AK8974_WHOAMI_VALUE_AMI306
:
893 case AK8974_WHOAMI_VALUE_AMI305
:
894 indio_dev
->channels
= ak8974_12_bits_channels
;
895 indio_dev
->num_channels
= ARRAY_SIZE(ak8974_12_bits_channels
);
897 case AK8974_WHOAMI_VALUE_HSCDTD008A
:
898 indio_dev
->channels
= ak8974_15_bits_channels
;
899 indio_dev
->num_channels
= ARRAY_SIZE(ak8974_15_bits_channels
);
902 indio_dev
->channels
= ak8974_12_bits_channels
;
903 indio_dev
->num_channels
= ARRAY_SIZE(ak8974_12_bits_channels
);
906 indio_dev
->info
= &ak8974_info
;
907 indio_dev
->available_scan_masks
= ak8974_scan_masks
;
908 indio_dev
->modes
= INDIO_DIRECT_MODE
;
909 indio_dev
->name
= ak8974
->name
;
911 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
912 ak8974_handle_trigger
,
915 dev_err(&i2c
->dev
, "triggered buffer setup failed\n");
919 /* If we have a valid DRDY IRQ, make use of it */
921 irq_trig
= irqd_get_trigger_type(irq_get_irq_data(irq
));
922 if (irq_trig
== IRQF_TRIGGER_RISING
) {
923 dev_info(&i2c
->dev
, "enable rising edge DRDY IRQ\n");
924 } else if (irq_trig
== IRQF_TRIGGER_FALLING
) {
925 ak8974
->drdy_active_low
= true;
926 dev_info(&i2c
->dev
, "enable falling edge DRDY IRQ\n");
928 irq_trig
= IRQF_TRIGGER_RISING
;
930 irq_trig
|= IRQF_ONESHOT
;
931 irq_trig
|= IRQF_SHARED
;
933 ret
= devm_request_threaded_irq(&i2c
->dev
,
936 ak8974_drdy_irq_thread
,
941 dev_err(&i2c
->dev
, "unable to request DRDY IRQ "
942 "- proceeding without IRQ\n");
945 ak8974
->drdy_irq
= true;
949 ret
= iio_device_register(indio_dev
);
951 dev_err(&i2c
->dev
, "device register failed\n");
955 pm_runtime_set_autosuspend_delay(&i2c
->dev
,
956 AK8974_AUTOSUSPEND_DELAY
);
957 pm_runtime_use_autosuspend(&i2c
->dev
);
958 pm_runtime_put(&i2c
->dev
);
963 iio_triggered_buffer_cleanup(indio_dev
);
965 pm_runtime_put_noidle(&i2c
->dev
);
966 pm_runtime_disable(&i2c
->dev
);
967 ak8974_set_power(ak8974
, AK8974_PWR_OFF
);
968 regulator_bulk_disable(ARRAY_SIZE(ak8974
->regs
), ak8974
->regs
);
973 static int ak8974_remove(struct i2c_client
*i2c
)
975 struct iio_dev
*indio_dev
= i2c_get_clientdata(i2c
);
976 struct ak8974
*ak8974
= iio_priv(indio_dev
);
978 iio_device_unregister(indio_dev
);
979 iio_triggered_buffer_cleanup(indio_dev
);
980 pm_runtime_get_sync(&i2c
->dev
);
981 pm_runtime_put_noidle(&i2c
->dev
);
982 pm_runtime_disable(&i2c
->dev
);
983 ak8974_set_power(ak8974
, AK8974_PWR_OFF
);
984 regulator_bulk_disable(ARRAY_SIZE(ak8974
->regs
), ak8974
->regs
);
989 static int __maybe_unused
ak8974_runtime_suspend(struct device
*dev
)
991 struct ak8974
*ak8974
=
992 iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
994 ak8974_set_power(ak8974
, AK8974_PWR_OFF
);
995 regulator_bulk_disable(ARRAY_SIZE(ak8974
->regs
), ak8974
->regs
);
1000 static int __maybe_unused
ak8974_runtime_resume(struct device
*dev
)
1002 struct ak8974
*ak8974
=
1003 iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
1006 ret
= regulator_bulk_enable(ARRAY_SIZE(ak8974
->regs
), ak8974
->regs
);
1009 msleep(AK8974_POWERON_DELAY
);
1010 ret
= ak8974_set_power(ak8974
, AK8974_PWR_ON
);
1012 goto out_regulator_disable
;
1014 ret
= ak8974_configure(ak8974
);
1016 goto out_disable_power
;
1021 ak8974_set_power(ak8974
, AK8974_PWR_OFF
);
1022 out_regulator_disable
:
1023 regulator_bulk_disable(ARRAY_SIZE(ak8974
->regs
), ak8974
->regs
);
1028 static const struct dev_pm_ops ak8974_dev_pm_ops
= {
1029 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
1030 pm_runtime_force_resume
)
1031 SET_RUNTIME_PM_OPS(ak8974_runtime_suspend
,
1032 ak8974_runtime_resume
, NULL
)
1035 static const struct i2c_device_id ak8974_id
[] = {
1042 MODULE_DEVICE_TABLE(i2c
, ak8974_id
);
1044 static const struct of_device_id ak8974_of_match
[] = {
1045 { .compatible
= "asahi-kasei,ak8974", },
1046 { .compatible
= "alps,hscdtd008a", },
1049 MODULE_DEVICE_TABLE(of
, ak8974_of_match
);
1051 static struct i2c_driver ak8974_driver
= {
1054 .pm
= &ak8974_dev_pm_ops
,
1055 .of_match_table
= ak8974_of_match
,
1057 .probe
= ak8974_probe
,
1058 .remove
= ak8974_remove
,
1059 .id_table
= ak8974_id
,
1061 module_i2c_driver(ak8974_driver
);
1063 MODULE_DESCRIPTION("AK8974 and AMI30x 3-axis magnetometer driver");
1064 MODULE_AUTHOR("Samu Onkalo");
1065 MODULE_AUTHOR("Linus Walleij");
1066 MODULE_LICENSE("GPL v2");