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.
6 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
7 * Copyright (c) 2010 NVIDIA Corporation.
8 * Copyright (C) 2016 Linaro Ltd.
10 * Author: Samu Onkalo <samu.p.onkalo@nokia.com>
11 * Author: Linus Walleij <linus.walleij@linaro.org>
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>
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>
36 * 16-bit registers are little-endian. LSB is at the address defined below
37 * and MSB is at the next higher address.
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
45 #define AK8974_INFO 0x0D
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
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
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
74 /* Different temperature registers */
75 #define AK8974_TEMP 0x31
76 #define AMI305_TEMP 0x60
78 /* AMI306-specific control register */
79 #define AMI306_CTRL4 0x5C
81 /* AMI306 factory calibration data */
83 /* fine axis sensitivity */
84 #define AMI306_FINEOUTPUT_X 0x90
85 #define AMI306_FINEOUTPUT_Y 0x92
86 #define AMI306_FINEOUTPUT_Z 0x94
88 /* axis sensitivity */
89 #define AMI306_SENS_X 0x96
90 #define AMI306_SENS_Y 0x98
91 #define AMI306_SENS_Z 0x9A
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
101 /* offset at ZERO magnetic field */
102 #define AMI306_OFFZERO_X 0xF8
103 #define AMI306_OFFZERO_Y 0xFA
104 #define AMI306_OFFZERO_Z 0xFC
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) */
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 */
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 */
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)
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
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)
142 /* The AMI305 has elaborate FW version and serial number registers */
143 #define AMI305_VER 0xE8
144 #define AMI305_SN 0xEA
146 #define AK8974_MAX_RANGE 2048
148 #define AK8974_POWERON_DELAY 50
149 #define AK8974_ACTIVATE_DELAY 1
150 #define AK8974_SELFTEST_DELAY 1
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
156 #define AK8974_AUTOSUSPEND_DELAY 5000
158 #define AK8974_MEASTIME 3
160 #define AK8974_PWR_ON 1
161 #define AK8974_PWR_OFF 0
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
177 struct i2c_client
*i2c
;
178 struct iio_mount_matrix orientation
;
180 struct regulator_bulk_data regs
[2];
185 struct completion drdy_complete
;
186 bool drdy_active_low
;
189 static const char ak8974_reg_avdd
[] = "avdd";
190 static const char ak8974_reg_dvdd
[] = "dvdd";
192 static int ak8974_get_u16_val(struct ak8974
*ak8974
, u8 reg
, u16
*val
)
197 ret
= regmap_bulk_read(ak8974
->map
, reg
, &bulk
, 2);
200 *val
= le16_to_cpu(bulk
);
205 static int ak8974_set_u16_val(struct ak8974
*ak8974
, u8 reg
, u16 val
)
207 __le16 bulk
= cpu_to_le16(val
);
209 return regmap_bulk_write(ak8974
->map
, reg
, &bulk
, 2);
212 static int ak8974_set_power(struct ak8974
*ak8974
, bool mode
)
217 val
= mode
? AK8974_CTRL1_POWER
: 0;
218 val
|= AK8974_CTRL1_FORCE_EN
;
219 ret
= regmap_write(ak8974
->map
, AK8974_CTRL1
, val
);
224 msleep(AK8974_ACTIVATE_DELAY
);
229 static int ak8974_reset(struct ak8974
*ak8974
)
233 /* Power on to get register access. Sets CTRL1 reg to reset state */
234 ret
= ak8974_set_power(ak8974
, AK8974_PWR_ON
);
237 ret
= regmap_write(ak8974
->map
, AK8974_CTRL2
, AK8974_CTRL2_RESDEF
);
240 ret
= regmap_write(ak8974
->map
, AK8974_CTRL3
, AK8974_CTRL3_RESDEF
);
243 ret
= regmap_write(ak8974
->map
, AK8974_INT_CTRL
,
244 AK8974_INT_CTRL_RESDEF
);
248 /* After reset, power off is default state */
249 return ak8974_set_power(ak8974
, AK8974_PWR_OFF
);
252 static int ak8974_configure(struct ak8974
*ak8974
)
256 ret
= regmap_write(ak8974
->map
, AK8974_CTRL2
, AK8974_CTRL2_DRDY_EN
|
257 AK8974_CTRL2_INT_EN
);
260 ret
= regmap_write(ak8974
->map
, AK8974_CTRL3
, 0);
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);
269 ret
= regmap_write(ak8974
->map
, AK8974_INT_CTRL
, AK8974_INT_CTRL_POL
);
273 return regmap_write(ak8974
->map
, AK8974_PRESET
, 0);
276 static int ak8974_trigmeas(struct ak8974
*ak8974
)
283 /* Clear any previous measurement overflow status */
284 ret
= regmap_read(ak8974
->map
, AK8974_INT_CLEAR
, &clear
);
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
;
295 if (!ak8974
->drdy_active_low
)
296 val
|= AK8974_CTRL2_DRDY_POL
;
298 init_completion(&ak8974
->drdy_complete
);
299 ret
= regmap_update_bits(ak8974
->map
, AK8974_CTRL2
,
305 /* Force a measurement */
306 return regmap_update_bits(ak8974
->map
,
312 static int ak8974_await_drdy(struct ak8974
*ak8974
)
318 if (ak8974
->drdy_irq
) {
319 ret
= wait_for_completion_timeout(&ak8974
->drdy_complete
,
320 1 + msecs_to_jiffies(1000));
322 dev_err(&ak8974
->i2c
->dev
,
323 "timeout waiting for DRDY IRQ\n");
329 /* Default delay-based poll loop */
331 msleep(AK8974_MEASTIME
);
332 ret
= regmap_read(ak8974
->map
, AK8974_STATUS
, &val
);
335 if (val
& AK8974_STATUS_DRDY
)
339 dev_err(&ak8974
->i2c
->dev
, "timeout waiting for DRDY\n");
343 static int ak8974_getresult(struct ak8974
*ak8974
, __le16
*result
)
348 ret
= ak8974_await_drdy(ak8974
);
351 ret
= regmap_read(ak8974
->map
, AK8974_INT_SRC
, &src
);
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");
362 ret
= regmap_bulk_read(ak8974
->map
, AK8974_DATA_X
, result
, 6);
369 static irqreturn_t
ak8974_drdy_irq(int irq
, void *d
)
371 struct ak8974
*ak8974
= d
;
373 if (!ak8974
->drdy_irq
)
376 /* TODO: timestamp here to get good measurement stamps */
377 return IRQ_WAKE_THREAD
;
380 static irqreturn_t
ak8974_drdy_irq_thread(int irq
, void *d
)
382 struct ak8974
*ak8974
= d
;
386 /* Check if this was a DRDY from us */
387 ret
= regmap_read(ak8974
->map
, AK8974_STATUS
, &val
);
389 dev_err(&ak8974
->i2c
->dev
, "error reading DRDY status\n");
392 if (val
& AK8974_STATUS_DRDY
) {
393 /* Yes this was our IRQ */
394 complete(&ak8974
->drdy_complete
);
398 /* We may be on a shared IRQ, let the next client check */
402 static int ak8974_selftest(struct ak8974
*ak8974
)
404 struct device
*dev
= &ak8974
->i2c
->dev
;
408 ret
= regmap_read(ak8974
->map
, AK8974_SELFTEST
, &val
);
411 if (val
!= AK8974_SELFTEST_IDLE
) {
412 dev_err(dev
, "selftest not idle before test\n");
416 /* Trigger self-test */
417 ret
= regmap_update_bits(ak8974
->map
,
419 AK8974_CTRL3_SELFTEST
,
420 AK8974_CTRL3_SELFTEST
);
422 dev_err(dev
, "could not write CTRL3\n");
426 msleep(AK8974_SELFTEST_DELAY
);
428 ret
= regmap_read(ak8974
->map
, AK8974_SELFTEST
, &val
);
431 if (val
!= AK8974_SELFTEST_OK
) {
432 dev_err(dev
, "selftest result NOT OK (%02x)\n", val
);
436 ret
= regmap_read(ak8974
->map
, AK8974_SELFTEST
, &val
);
439 if (val
!= AK8974_SELFTEST_IDLE
) {
440 dev_err(dev
, "selftest not idle after test (%02x)\n", val
);
443 dev_dbg(dev
, "passed self-test\n");
448 static void ak8974_read_calib_data(struct ak8974
*ak8974
, unsigned int reg
,
449 __le16
*tab
, size_t tab_size
)
451 int ret
= regmap_bulk_read(ak8974
->map
, reg
, tab
, tab_size
);
453 memset(tab
, 0xFF, tab_size
);
454 dev_warn(&ak8974
->i2c
->dev
,
455 "can't read calibration data (regs %u..%zu): %d\n",
456 reg
, reg
+ tab_size
- 1, ret
);
458 add_device_randomness(tab
, tab_size
);
462 static int ak8974_detect(struct ak8974
*ak8974
)
470 ret
= regmap_read(ak8974
->map
, AK8974_WHOAMI
, &whoami
);
477 case AK8974_WHOAMI_VALUE_AMI306
:
480 case AK8974_WHOAMI_VALUE_AMI305
:
481 ret
= regmap_read(ak8974
->map
, AMI305_VER
, &fw
);
484 fw
&= 0x7f; /* only bits 0 thru 6 valid */
485 ret
= ak8974_get_u16_val(ak8974
, AMI305_SN
, &sn
);
488 add_device_randomness(&sn
, sizeof(sn
));
489 dev_info(&ak8974
->i2c
->dev
,
490 "detected %s, FW ver %02x, S/N: %04x\n",
493 case AK8974_WHOAMI_VALUE_AK8974
:
495 dev_info(&ak8974
->i2c
->dev
, "detected AK8974\n");
498 dev_err(&ak8974
->i2c
->dev
, "unsupported device (%02x) ",
504 ak8974
->variant
= whoami
;
506 if (whoami
== AK8974_WHOAMI_VALUE_AMI306
) {
507 __le16 fab_data1
[9], fab_data2
[3];
510 ak8974_read_calib_data(ak8974
, AMI306_FINEOUTPUT_X
,
511 fab_data1
, sizeof(fab_data1
));
512 ak8974_read_calib_data(ak8974
, AMI306_OFFZERO_X
,
513 fab_data2
, sizeof(fab_data2
));
515 for (i
= 0; i
< 3; ++i
) {
516 static const char axis
[3] = "XYZ";
517 static const char pgaxis
[6] = "ZYZXYX";
518 unsigned offz
= le16_to_cpu(fab_data2
[i
]) & 0x7F;
519 unsigned fine
= le16_to_cpu(fab_data1
[i
]);
520 unsigned sens
= le16_to_cpu(fab_data1
[i
+ 3]);
521 unsigned pgain1
= le16_to_cpu(fab_data1
[i
+ 6]);
522 unsigned pgain2
= pgain1
>> 8;
526 dev_info(&ak8974
->i2c
->dev
,
527 "factory calibration for axis %c: offz=%u sens=%u fine=%u pga%c=%u pga%c=%u\n",
528 axis
[i
], offz
, sens
, fine
, pgaxis
[i
* 2],
529 pgain1
, pgaxis
[i
* 2 + 1], pgain2
);
536 static int ak8974_read_raw(struct iio_dev
*indio_dev
,
537 struct iio_chan_spec
const *chan
,
541 struct ak8974
*ak8974
= iio_priv(indio_dev
);
545 pm_runtime_get_sync(&ak8974
->i2c
->dev
);
546 mutex_lock(&ak8974
->lock
);
549 case IIO_CHAN_INFO_RAW
:
550 if (chan
->address
> 2) {
551 dev_err(&ak8974
->i2c
->dev
, "faulty channel address\n");
555 ret
= ak8974_trigmeas(ak8974
);
558 ret
= ak8974_getresult(ak8974
, hw_values
);
563 * We read all axes and discard all but one, for optimized
564 * reading, use the triggered buffer.
566 *val
= le16_to_cpu(hw_values
[chan
->address
]);
572 mutex_unlock(&ak8974
->lock
);
573 pm_runtime_mark_last_busy(&ak8974
->i2c
->dev
);
574 pm_runtime_put_autosuspend(&ak8974
->i2c
->dev
);
579 static void ak8974_fill_buffer(struct iio_dev
*indio_dev
)
581 struct ak8974
*ak8974
= iio_priv(indio_dev
);
583 __le16 hw_values
[8]; /* Three axes + 64bit padding */
585 pm_runtime_get_sync(&ak8974
->i2c
->dev
);
586 mutex_lock(&ak8974
->lock
);
588 ret
= ak8974_trigmeas(ak8974
);
590 dev_err(&ak8974
->i2c
->dev
, "error triggering measure\n");
593 ret
= ak8974_getresult(ak8974
, hw_values
);
595 dev_err(&ak8974
->i2c
->dev
, "error getting measures\n");
599 iio_push_to_buffers_with_timestamp(indio_dev
, hw_values
,
600 iio_get_time_ns(indio_dev
));
603 mutex_unlock(&ak8974
->lock
);
604 pm_runtime_mark_last_busy(&ak8974
->i2c
->dev
);
605 pm_runtime_put_autosuspend(&ak8974
->i2c
->dev
);
608 static irqreturn_t
ak8974_handle_trigger(int irq
, void *p
)
610 const struct iio_poll_func
*pf
= p
;
611 struct iio_dev
*indio_dev
= pf
->indio_dev
;
613 ak8974_fill_buffer(indio_dev
);
614 iio_trigger_notify_done(indio_dev
->trig
);
619 static const struct iio_mount_matrix
*
620 ak8974_get_mount_matrix(const struct iio_dev
*indio_dev
,
621 const struct iio_chan_spec
*chan
)
623 struct ak8974
*ak8974
= iio_priv(indio_dev
);
625 return &ak8974
->orientation
;
628 static const struct iio_chan_spec_ext_info ak8974_ext_info
[] = {
629 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR
, ak8974_get_mount_matrix
),
633 #define AK8974_AXIS_CHANNEL(axis, index) \
637 .channel2 = IIO_MOD_##axis, \
638 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
639 .ext_info = ak8974_ext_info, \
641 .scan_index = index, \
646 .endianness = IIO_LE \
650 static const struct iio_chan_spec ak8974_channels
[] = {
651 AK8974_AXIS_CHANNEL(X
, 0),
652 AK8974_AXIS_CHANNEL(Y
, 1),
653 AK8974_AXIS_CHANNEL(Z
, 2),
654 IIO_CHAN_SOFT_TIMESTAMP(3),
657 static const unsigned long ak8974_scan_masks
[] = { 0x7, 0 };
659 static const struct iio_info ak8974_info
= {
660 .read_raw
= &ak8974_read_raw
,
663 static bool ak8974_writeable_reg(struct device
*dev
, unsigned int reg
)
665 struct i2c_client
*i2c
= to_i2c_client(dev
);
666 struct iio_dev
*indio_dev
= i2c_get_clientdata(i2c
);
667 struct ak8974
*ak8974
= iio_priv(indio_dev
);
673 case AK8974_INT_CTRL
:
674 case AK8974_INT_THRES
:
675 case AK8974_INT_THRES
+ 1:
677 case AK8974_PRESET
+ 1:
679 case AK8974_OFFSET_X
:
680 case AK8974_OFFSET_X
+ 1:
681 case AK8974_OFFSET_Y
:
682 case AK8974_OFFSET_Y
+ 1:
683 case AK8974_OFFSET_Z
:
684 case AK8974_OFFSET_Z
+ 1:
685 if (ak8974
->variant
== AK8974_WHOAMI_VALUE_AK8974
)
688 case AMI305_OFFSET_X
:
689 case AMI305_OFFSET_X
+ 1:
690 case AMI305_OFFSET_Y
:
691 case AMI305_OFFSET_Y
+ 1:
692 case AMI305_OFFSET_Z
:
693 case AMI305_OFFSET_Z
+ 1:
694 return ak8974
->variant
== AK8974_WHOAMI_VALUE_AMI305
||
695 ak8974
->variant
== AK8974_WHOAMI_VALUE_AMI306
;
697 case AMI306_CTRL4
+ 1:
698 return ak8974
->variant
== AK8974_WHOAMI_VALUE_AMI306
;
704 static bool ak8974_precious_reg(struct device
*dev
, unsigned int reg
)
706 return reg
== AK8974_INT_CLEAR
;
709 static const struct regmap_config ak8974_regmap_config
= {
712 .max_register
= 0xff,
713 .writeable_reg
= ak8974_writeable_reg
,
714 .precious_reg
= ak8974_precious_reg
,
717 static int ak8974_probe(struct i2c_client
*i2c
,
718 const struct i2c_device_id
*id
)
720 struct iio_dev
*indio_dev
;
721 struct ak8974
*ak8974
;
722 unsigned long irq_trig
;
726 /* Register with IIO */
727 indio_dev
= devm_iio_device_alloc(&i2c
->dev
, sizeof(*ak8974
));
728 if (indio_dev
== NULL
)
731 ak8974
= iio_priv(indio_dev
);
732 i2c_set_clientdata(i2c
, indio_dev
);
734 mutex_init(&ak8974
->lock
);
736 ret
= of_iio_read_mount_matrix(&i2c
->dev
,
738 &ak8974
->orientation
);
742 ak8974
->regs
[0].supply
= ak8974_reg_avdd
;
743 ak8974
->regs
[1].supply
= ak8974_reg_dvdd
;
745 ret
= devm_regulator_bulk_get(&i2c
->dev
,
746 ARRAY_SIZE(ak8974
->regs
),
749 dev_err(&i2c
->dev
, "cannot get regulators\n");
753 ret
= regulator_bulk_enable(ARRAY_SIZE(ak8974
->regs
), ak8974
->regs
);
755 dev_err(&i2c
->dev
, "cannot enable regulators\n");
759 /* Take runtime PM online */
760 pm_runtime_get_noresume(&i2c
->dev
);
761 pm_runtime_set_active(&i2c
->dev
);
762 pm_runtime_enable(&i2c
->dev
);
764 ak8974
->map
= devm_regmap_init_i2c(i2c
, &ak8974_regmap_config
);
765 if (IS_ERR(ak8974
->map
)) {
766 dev_err(&i2c
->dev
, "failed to allocate register map\n");
767 return PTR_ERR(ak8974
->map
);
770 ret
= ak8974_set_power(ak8974
, AK8974_PWR_ON
);
772 dev_err(&i2c
->dev
, "could not power on\n");
776 ret
= ak8974_detect(ak8974
);
778 dev_err(&i2c
->dev
, "neither AK8974 nor AMI30x found\n");
782 ret
= ak8974_selftest(ak8974
);
784 dev_err(&i2c
->dev
, "selftest failed (continuing anyway)\n");
786 ret
= ak8974_reset(ak8974
);
788 dev_err(&i2c
->dev
, "AK8974 reset failed\n");
792 pm_runtime_set_autosuspend_delay(&i2c
->dev
,
793 AK8974_AUTOSUSPEND_DELAY
);
794 pm_runtime_use_autosuspend(&i2c
->dev
);
795 pm_runtime_put(&i2c
->dev
);
797 indio_dev
->dev
.parent
= &i2c
->dev
;
798 indio_dev
->channels
= ak8974_channels
;
799 indio_dev
->num_channels
= ARRAY_SIZE(ak8974_channels
);
800 indio_dev
->info
= &ak8974_info
;
801 indio_dev
->available_scan_masks
= ak8974_scan_masks
;
802 indio_dev
->modes
= INDIO_DIRECT_MODE
;
803 indio_dev
->name
= ak8974
->name
;
805 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
806 ak8974_handle_trigger
,
809 dev_err(&i2c
->dev
, "triggered buffer setup failed\n");
813 /* If we have a valid DRDY IRQ, make use of it */
815 irq_trig
= irqd_get_trigger_type(irq_get_irq_data(irq
));
816 if (irq_trig
== IRQF_TRIGGER_RISING
) {
817 dev_info(&i2c
->dev
, "enable rising edge DRDY IRQ\n");
818 } else if (irq_trig
== IRQF_TRIGGER_FALLING
) {
819 ak8974
->drdy_active_low
= true;
820 dev_info(&i2c
->dev
, "enable falling edge DRDY IRQ\n");
822 irq_trig
= IRQF_TRIGGER_RISING
;
824 irq_trig
|= IRQF_ONESHOT
;
825 irq_trig
|= IRQF_SHARED
;
827 ret
= devm_request_threaded_irq(&i2c
->dev
,
830 ak8974_drdy_irq_thread
,
835 dev_err(&i2c
->dev
, "unable to request DRDY IRQ "
836 "- proceeding without IRQ\n");
839 ak8974
->drdy_irq
= true;
843 ret
= iio_device_register(indio_dev
);
845 dev_err(&i2c
->dev
, "device register failed\n");
852 iio_triggered_buffer_cleanup(indio_dev
);
854 pm_runtime_put_noidle(&i2c
->dev
);
855 pm_runtime_disable(&i2c
->dev
);
856 ak8974_set_power(ak8974
, AK8974_PWR_OFF
);
858 regulator_bulk_disable(ARRAY_SIZE(ak8974
->regs
), ak8974
->regs
);
863 static int ak8974_remove(struct i2c_client
*i2c
)
865 struct iio_dev
*indio_dev
= i2c_get_clientdata(i2c
);
866 struct ak8974
*ak8974
= iio_priv(indio_dev
);
868 iio_device_unregister(indio_dev
);
869 iio_triggered_buffer_cleanup(indio_dev
);
870 pm_runtime_get_sync(&i2c
->dev
);
871 pm_runtime_put_noidle(&i2c
->dev
);
872 pm_runtime_disable(&i2c
->dev
);
873 ak8974_set_power(ak8974
, AK8974_PWR_OFF
);
874 regulator_bulk_disable(ARRAY_SIZE(ak8974
->regs
), ak8974
->regs
);
879 static int __maybe_unused
ak8974_runtime_suspend(struct device
*dev
)
881 struct ak8974
*ak8974
=
882 iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
884 ak8974_set_power(ak8974
, AK8974_PWR_OFF
);
885 regulator_bulk_disable(ARRAY_SIZE(ak8974
->regs
), ak8974
->regs
);
890 static int __maybe_unused
ak8974_runtime_resume(struct device
*dev
)
892 struct ak8974
*ak8974
=
893 iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
896 ret
= regulator_bulk_enable(ARRAY_SIZE(ak8974
->regs
), ak8974
->regs
);
899 msleep(AK8974_POWERON_DELAY
);
900 ret
= ak8974_set_power(ak8974
, AK8974_PWR_ON
);
902 goto out_regulator_disable
;
904 ret
= ak8974_configure(ak8974
);
906 goto out_disable_power
;
911 ak8974_set_power(ak8974
, AK8974_PWR_OFF
);
912 out_regulator_disable
:
913 regulator_bulk_disable(ARRAY_SIZE(ak8974
->regs
), ak8974
->regs
);
918 static const struct dev_pm_ops ak8974_dev_pm_ops
= {
919 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
920 pm_runtime_force_resume
)
921 SET_RUNTIME_PM_OPS(ak8974_runtime_suspend
,
922 ak8974_runtime_resume
, NULL
)
925 static const struct i2c_device_id ak8974_id
[] = {
931 MODULE_DEVICE_TABLE(i2c
, ak8974_id
);
933 static const struct of_device_id ak8974_of_match
[] = {
934 { .compatible
= "asahi-kasei,ak8974", },
937 MODULE_DEVICE_TABLE(of
, ak8974_of_match
);
939 static struct i2c_driver ak8974_driver
= {
942 .pm
= &ak8974_dev_pm_ops
,
943 .of_match_table
= of_match_ptr(ak8974_of_match
),
945 .probe
= ak8974_probe
,
946 .remove
= ak8974_remove
,
947 .id_table
= ak8974_id
,
949 module_i2c_driver(ak8974_driver
);
951 MODULE_DESCRIPTION("AK8974 and AMI30x 3-axis magnetometer driver");
952 MODULE_AUTHOR("Samu Onkalo");
953 MODULE_AUTHOR("Linus Walleij");
954 MODULE_LICENSE("GPL v2");