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/regmap.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/pm_runtime.h>
27 #include <linux/iio/iio.h>
28 #include <linux/iio/sysfs.h>
29 #include <linux/iio/buffer.h>
30 #include <linux/iio/trigger.h>
31 #include <linux/iio/trigger_consumer.h>
32 #include <linux/iio/triggered_buffer.h>
35 * 16-bit registers are little-endian. LSB is at the address defined below
36 * and MSB is at the next higher address.
39 /* These registers are common for AK8974 and AMI305 */
40 #define AK8974_SELFTEST 0x0C
41 #define AK8974_SELFTEST_IDLE 0x55
42 #define AK8974_SELFTEST_OK 0xAA
44 #define AK8974_INFO 0x0D
46 #define AK8974_WHOAMI 0x0F
47 #define AK8974_WHOAMI_VALUE_AMI305 0x47
48 #define AK8974_WHOAMI_VALUE_AK8974 0x48
50 #define AK8974_DATA_X 0x10
51 #define AK8974_DATA_Y 0x12
52 #define AK8974_DATA_Z 0x14
53 #define AK8974_INT_SRC 0x16
54 #define AK8974_STATUS 0x18
55 #define AK8974_INT_CLEAR 0x1A
56 #define AK8974_CTRL1 0x1B
57 #define AK8974_CTRL2 0x1C
58 #define AK8974_CTRL3 0x1D
59 #define AK8974_INT_CTRL 0x1E
60 #define AK8974_INT_THRES 0x26 /* Absolute any axis value threshold */
61 #define AK8974_PRESET 0x30
63 /* AK8974-specific offsets */
64 #define AK8974_OFFSET_X 0x20
65 #define AK8974_OFFSET_Y 0x22
66 #define AK8974_OFFSET_Z 0x24
67 /* AMI305-specific offsets */
68 #define AMI305_OFFSET_X 0x6C
69 #define AMI305_OFFSET_Y 0x72
70 #define AMI305_OFFSET_Z 0x78
72 /* Different temperature registers */
73 #define AK8974_TEMP 0x31
74 #define AMI305_TEMP 0x60
76 #define AK8974_INT_X_HIGH BIT(7) /* Axis over +threshold */
77 #define AK8974_INT_Y_HIGH BIT(6)
78 #define AK8974_INT_Z_HIGH BIT(5)
79 #define AK8974_INT_X_LOW BIT(4) /* Axis below -threshold */
80 #define AK8974_INT_Y_LOW BIT(3)
81 #define AK8974_INT_Z_LOW BIT(2)
82 #define AK8974_INT_RANGE BIT(1) /* Range overflow (any axis) */
84 #define AK8974_STATUS_DRDY BIT(6) /* Data ready */
85 #define AK8974_STATUS_OVERRUN BIT(5) /* Data overrun */
86 #define AK8974_STATUS_INT BIT(4) /* Interrupt occurred */
88 #define AK8974_CTRL1_POWER BIT(7) /* 0 = standby; 1 = active */
89 #define AK8974_CTRL1_RATE BIT(4) /* 0 = 10 Hz; 1 = 20 Hz */
90 #define AK8974_CTRL1_FORCE_EN BIT(1) /* 0 = normal; 1 = force */
91 #define AK8974_CTRL1_MODE2 BIT(0) /* 0 */
93 #define AK8974_CTRL2_INT_EN BIT(4) /* 1 = enable interrupts */
94 #define AK8974_CTRL2_DRDY_EN BIT(3) /* 1 = enable data ready signal */
95 #define AK8974_CTRL2_DRDY_POL BIT(2) /* 1 = data ready active high */
96 #define AK8974_CTRL2_RESDEF (AK8974_CTRL2_DRDY_POL)
98 #define AK8974_CTRL3_RESET BIT(7) /* Software reset */
99 #define AK8974_CTRL3_FORCE BIT(6) /* Start forced measurement */
100 #define AK8974_CTRL3_SELFTEST BIT(4) /* Set selftest register */
101 #define AK8974_CTRL3_RESDEF 0x00
103 #define AK8974_INT_CTRL_XEN BIT(7) /* Enable interrupt for this axis */
104 #define AK8974_INT_CTRL_YEN BIT(6)
105 #define AK8974_INT_CTRL_ZEN BIT(5)
106 #define AK8974_INT_CTRL_XYZEN (BIT(7)|BIT(6)|BIT(5))
107 #define AK8974_INT_CTRL_POL BIT(3) /* 0 = active low; 1 = active high */
108 #define AK8974_INT_CTRL_PULSE BIT(1) /* 0 = latched; 1 = pulse (50 usec) */
109 #define AK8974_INT_CTRL_RESDEF (AK8974_INT_CTRL_XYZEN | AK8974_INT_CTRL_POL)
111 /* The AMI305 has elaborate FW version and serial number registers */
112 #define AMI305_VER 0xE8
113 #define AMI305_SN 0xEA
115 #define AK8974_MAX_RANGE 2048
117 #define AK8974_POWERON_DELAY 50
118 #define AK8974_ACTIVATE_DELAY 1
119 #define AK8974_SELFTEST_DELAY 1
121 * Set the autosuspend to two orders of magnitude larger than the poweron
122 * delay to make sane reasonable power tradeoff savings (5 seconds in
125 #define AK8974_AUTOSUSPEND_DELAY 5000
127 #define AK8974_MEASTIME 3
129 #define AK8974_PWR_ON 1
130 #define AK8974_PWR_OFF 0
133 * struct ak8974 - state container for the AK8974 driver
134 * @i2c: parent I2C client
135 * @orientation: mounting matrix, flipped axis etc
136 * @map: regmap to access the AK8974 registers over I2C
137 * @regs: the avdd and dvdd power regulators
138 * @name: the name of the part
139 * @variant: the whoami ID value (for selecting code paths)
140 * @lock: locks the magnetometer for exclusive use during a measurement
141 * @drdy_irq: uses the DRDY IRQ line
142 * @drdy_complete: completion for DRDY
143 * @drdy_active_low: the DRDY IRQ is active low
146 struct i2c_client
*i2c
;
147 struct iio_mount_matrix orientation
;
149 struct regulator_bulk_data regs
[2];
154 struct completion drdy_complete
;
155 bool drdy_active_low
;
158 static const char ak8974_reg_avdd
[] = "avdd";
159 static const char ak8974_reg_dvdd
[] = "dvdd";
161 static int ak8974_set_power(struct ak8974
*ak8974
, bool mode
)
166 val
= mode
? AK8974_CTRL1_POWER
: 0;
167 val
|= AK8974_CTRL1_FORCE_EN
;
168 ret
= regmap_write(ak8974
->map
, AK8974_CTRL1
, val
);
173 msleep(AK8974_ACTIVATE_DELAY
);
178 static int ak8974_reset(struct ak8974
*ak8974
)
182 /* Power on to get register access. Sets CTRL1 reg to reset state */
183 ret
= ak8974_set_power(ak8974
, AK8974_PWR_ON
);
186 ret
= regmap_write(ak8974
->map
, AK8974_CTRL2
, AK8974_CTRL2_RESDEF
);
189 ret
= regmap_write(ak8974
->map
, AK8974_CTRL3
, AK8974_CTRL3_RESDEF
);
192 ret
= regmap_write(ak8974
->map
, AK8974_INT_CTRL
,
193 AK8974_INT_CTRL_RESDEF
);
197 /* After reset, power off is default state */
198 return ak8974_set_power(ak8974
, AK8974_PWR_OFF
);
201 static int ak8974_configure(struct ak8974
*ak8974
)
205 ret
= regmap_write(ak8974
->map
, AK8974_CTRL2
, AK8974_CTRL2_DRDY_EN
|
206 AK8974_CTRL2_INT_EN
);
209 ret
= regmap_write(ak8974
->map
, AK8974_CTRL3
, 0);
212 ret
= regmap_write(ak8974
->map
, AK8974_INT_CTRL
, AK8974_INT_CTRL_POL
);
216 return regmap_write(ak8974
->map
, AK8974_PRESET
, 0);
219 static int ak8974_trigmeas(struct ak8974
*ak8974
)
226 /* Clear any previous measurement overflow status */
227 ret
= regmap_read(ak8974
->map
, AK8974_INT_CLEAR
, &clear
);
231 /* If we have a DRDY IRQ line, use it */
232 if (ak8974
->drdy_irq
) {
233 mask
= AK8974_CTRL2_INT_EN
|
234 AK8974_CTRL2_DRDY_EN
|
235 AK8974_CTRL2_DRDY_POL
;
236 val
= AK8974_CTRL2_DRDY_EN
;
238 if (!ak8974
->drdy_active_low
)
239 val
|= AK8974_CTRL2_DRDY_POL
;
241 init_completion(&ak8974
->drdy_complete
);
242 ret
= regmap_update_bits(ak8974
->map
, AK8974_CTRL2
,
248 /* Force a measurement */
249 return regmap_update_bits(ak8974
->map
,
255 static int ak8974_await_drdy(struct ak8974
*ak8974
)
261 if (ak8974
->drdy_irq
) {
262 ret
= wait_for_completion_timeout(&ak8974
->drdy_complete
,
263 1 + msecs_to_jiffies(1000));
265 dev_err(&ak8974
->i2c
->dev
,
266 "timeout waiting for DRDY IRQ\n");
272 /* Default delay-based poll loop */
274 msleep(AK8974_MEASTIME
);
275 ret
= regmap_read(ak8974
->map
, AK8974_STATUS
, &val
);
278 if (val
& AK8974_STATUS_DRDY
)
282 dev_err(&ak8974
->i2c
->dev
,
283 "timeout waiting for DRDY\n");
290 static int ak8974_getresult(struct ak8974
*ak8974
, s16
*result
)
295 ret
= ak8974_await_drdy(ak8974
);
298 ret
= regmap_read(ak8974
->map
, AK8974_INT_SRC
, &src
);
302 /* Out of range overflow! Strong magnet close? */
303 if (src
& AK8974_INT_RANGE
) {
304 dev_err(&ak8974
->i2c
->dev
,
305 "range overflow in sensor\n");
309 ret
= regmap_bulk_read(ak8974
->map
, AK8974_DATA_X
, result
, 6);
316 static irqreturn_t
ak8974_drdy_irq(int irq
, void *d
)
318 struct ak8974
*ak8974
= d
;
320 if (!ak8974
->drdy_irq
)
323 /* TODO: timestamp here to get good measurement stamps */
324 return IRQ_WAKE_THREAD
;
327 static irqreturn_t
ak8974_drdy_irq_thread(int irq
, void *d
)
329 struct ak8974
*ak8974
= d
;
333 /* Check if this was a DRDY from us */
334 ret
= regmap_read(ak8974
->map
, AK8974_STATUS
, &val
);
336 dev_err(&ak8974
->i2c
->dev
, "error reading DRDY status\n");
339 if (val
& AK8974_STATUS_DRDY
) {
340 /* Yes this was our IRQ */
341 complete(&ak8974
->drdy_complete
);
345 /* We may be on a shared IRQ, let the next client check */
349 static int ak8974_selftest(struct ak8974
*ak8974
)
351 struct device
*dev
= &ak8974
->i2c
->dev
;
355 ret
= regmap_read(ak8974
->map
, AK8974_SELFTEST
, &val
);
358 if (val
!= AK8974_SELFTEST_IDLE
) {
359 dev_err(dev
, "selftest not idle before test\n");
363 /* Trigger self-test */
364 ret
= regmap_update_bits(ak8974
->map
,
366 AK8974_CTRL3_SELFTEST
,
367 AK8974_CTRL3_SELFTEST
);
369 dev_err(dev
, "could not write CTRL3\n");
373 msleep(AK8974_SELFTEST_DELAY
);
375 ret
= regmap_read(ak8974
->map
, AK8974_SELFTEST
, &val
);
378 if (val
!= AK8974_SELFTEST_OK
) {
379 dev_err(dev
, "selftest result NOT OK (%02x)\n", val
);
383 ret
= regmap_read(ak8974
->map
, AK8974_SELFTEST
, &val
);
386 if (val
!= AK8974_SELFTEST_IDLE
) {
387 dev_err(dev
, "selftest not idle after test (%02x)\n", val
);
390 dev_dbg(dev
, "passed self-test\n");
395 static int ak8974_get_u16_val(struct ak8974
*ak8974
, u8 reg
, u16
*val
)
400 ret
= regmap_bulk_read(ak8974
->map
, reg
, &bulk
, 2);
403 *val
= le16_to_cpu(bulk
);
408 static int ak8974_detect(struct ak8974
*ak8974
)
416 ret
= regmap_read(ak8974
->map
, AK8974_WHOAMI
, &whoami
);
421 case AK8974_WHOAMI_VALUE_AMI305
:
423 ret
= regmap_read(ak8974
->map
, AMI305_VER
, &fw
);
426 fw
&= 0x7f; /* only bits 0 thru 6 valid */
427 ret
= ak8974_get_u16_val(ak8974
, AMI305_SN
, &sn
);
430 dev_info(&ak8974
->i2c
->dev
,
431 "detected %s, FW ver %02x, S/N: %04x\n",
434 case AK8974_WHOAMI_VALUE_AK8974
:
436 dev_info(&ak8974
->i2c
->dev
, "detected AK8974\n");
439 dev_err(&ak8974
->i2c
->dev
, "unsupported device (%02x) ",
445 ak8974
->variant
= whoami
;
450 static int ak8974_read_raw(struct iio_dev
*indio_dev
,
451 struct iio_chan_spec
const *chan
,
455 struct ak8974
*ak8974
= iio_priv(indio_dev
);
459 pm_runtime_get_sync(&ak8974
->i2c
->dev
);
460 mutex_lock(&ak8974
->lock
);
463 case IIO_CHAN_INFO_RAW
:
464 if (chan
->address
> 2) {
465 dev_err(&ak8974
->i2c
->dev
, "faulty channel address\n");
469 ret
= ak8974_trigmeas(ak8974
);
472 ret
= ak8974_getresult(ak8974
, hw_values
);
477 * We read all axes and discard all but one, for optimized
478 * reading, use the triggered buffer.
480 *val
= le16_to_cpu(hw_values
[chan
->address
]);
486 mutex_unlock(&ak8974
->lock
);
487 pm_runtime_mark_last_busy(&ak8974
->i2c
->dev
);
488 pm_runtime_put_autosuspend(&ak8974
->i2c
->dev
);
493 static void ak8974_fill_buffer(struct iio_dev
*indio_dev
)
495 struct ak8974
*ak8974
= iio_priv(indio_dev
);
497 s16 hw_values
[8]; /* Three axes + 64bit padding */
499 pm_runtime_get_sync(&ak8974
->i2c
->dev
);
500 mutex_lock(&ak8974
->lock
);
502 ret
= ak8974_trigmeas(ak8974
);
504 dev_err(&ak8974
->i2c
->dev
, "error triggering measure\n");
507 ret
= ak8974_getresult(ak8974
, hw_values
);
509 dev_err(&ak8974
->i2c
->dev
, "error getting measures\n");
513 iio_push_to_buffers_with_timestamp(indio_dev
, hw_values
,
514 iio_get_time_ns(indio_dev
));
517 mutex_unlock(&ak8974
->lock
);
518 pm_runtime_mark_last_busy(&ak8974
->i2c
->dev
);
519 pm_runtime_put_autosuspend(&ak8974
->i2c
->dev
);
522 static irqreturn_t
ak8974_handle_trigger(int irq
, void *p
)
524 const struct iio_poll_func
*pf
= p
;
525 struct iio_dev
*indio_dev
= pf
->indio_dev
;
527 ak8974_fill_buffer(indio_dev
);
528 iio_trigger_notify_done(indio_dev
->trig
);
533 static const struct iio_mount_matrix
*
534 ak8974_get_mount_matrix(const struct iio_dev
*indio_dev
,
535 const struct iio_chan_spec
*chan
)
537 struct ak8974
*ak8974
= iio_priv(indio_dev
);
539 return &ak8974
->orientation
;
542 static const struct iio_chan_spec_ext_info ak8974_ext_info
[] = {
543 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR
, ak8974_get_mount_matrix
),
547 #define AK8974_AXIS_CHANNEL(axis, index) \
551 .channel2 = IIO_MOD_##axis, \
552 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
553 .ext_info = ak8974_ext_info, \
555 .scan_index = index, \
560 .endianness = IIO_LE \
564 static const struct iio_chan_spec ak8974_channels
[] = {
565 AK8974_AXIS_CHANNEL(X
, 0),
566 AK8974_AXIS_CHANNEL(Y
, 1),
567 AK8974_AXIS_CHANNEL(Z
, 2),
568 IIO_CHAN_SOFT_TIMESTAMP(3),
571 static const unsigned long ak8974_scan_masks
[] = { 0x7, 0 };
573 static const struct iio_info ak8974_info
= {
574 .read_raw
= &ak8974_read_raw
,
575 .driver_module
= THIS_MODULE
,
578 static bool ak8974_writeable_reg(struct device
*dev
, unsigned int reg
)
580 struct i2c_client
*i2c
= to_i2c_client(dev
);
581 struct iio_dev
*indio_dev
= i2c_get_clientdata(i2c
);
582 struct ak8974
*ak8974
= iio_priv(indio_dev
);
588 case AK8974_INT_CTRL
:
589 case AK8974_INT_THRES
:
590 case AK8974_INT_THRES
+ 1:
592 case AK8974_PRESET
+ 1:
594 case AK8974_OFFSET_X
:
595 case AK8974_OFFSET_X
+ 1:
596 case AK8974_OFFSET_Y
:
597 case AK8974_OFFSET_Y
+ 1:
598 case AK8974_OFFSET_Z
:
599 case AK8974_OFFSET_Z
+ 1:
600 if (ak8974
->variant
== AK8974_WHOAMI_VALUE_AK8974
)
603 case AMI305_OFFSET_X
:
604 case AMI305_OFFSET_X
+ 1:
605 case AMI305_OFFSET_Y
:
606 case AMI305_OFFSET_Y
+ 1:
607 case AMI305_OFFSET_Z
:
608 case AMI305_OFFSET_Z
+ 1:
609 if (ak8974
->variant
== AK8974_WHOAMI_VALUE_AMI305
)
617 static const struct regmap_config ak8974_regmap_config
= {
620 .max_register
= 0xff,
621 .writeable_reg
= ak8974_writeable_reg
,
624 static int ak8974_probe(struct i2c_client
*i2c
,
625 const struct i2c_device_id
*id
)
627 struct iio_dev
*indio_dev
;
628 struct ak8974
*ak8974
;
629 unsigned long irq_trig
;
633 /* Register with IIO */
634 indio_dev
= devm_iio_device_alloc(&i2c
->dev
, sizeof(*ak8974
));
635 if (indio_dev
== NULL
)
638 ak8974
= iio_priv(indio_dev
);
639 i2c_set_clientdata(i2c
, indio_dev
);
641 mutex_init(&ak8974
->lock
);
643 ret
= of_iio_read_mount_matrix(&i2c
->dev
,
645 &ak8974
->orientation
);
649 ak8974
->regs
[0].supply
= ak8974_reg_avdd
;
650 ak8974
->regs
[1].supply
= ak8974_reg_dvdd
;
652 ret
= devm_regulator_bulk_get(&i2c
->dev
,
653 ARRAY_SIZE(ak8974
->regs
),
656 dev_err(&i2c
->dev
, "cannot get regulators\n");
660 ret
= regulator_bulk_enable(ARRAY_SIZE(ak8974
->regs
), ak8974
->regs
);
662 dev_err(&i2c
->dev
, "cannot enable regulators\n");
666 /* Take runtime PM online */
667 pm_runtime_get_noresume(&i2c
->dev
);
668 pm_runtime_set_active(&i2c
->dev
);
669 pm_runtime_enable(&i2c
->dev
);
671 ak8974
->map
= devm_regmap_init_i2c(i2c
, &ak8974_regmap_config
);
672 if (IS_ERR(ak8974
->map
)) {
673 dev_err(&i2c
->dev
, "failed to allocate register map\n");
674 return PTR_ERR(ak8974
->map
);
677 ret
= ak8974_set_power(ak8974
, AK8974_PWR_ON
);
679 dev_err(&i2c
->dev
, "could not power on\n");
683 ret
= ak8974_detect(ak8974
);
685 dev_err(&i2c
->dev
, "neither AK8974 nor AMI305 found\n");
689 ret
= ak8974_selftest(ak8974
);
691 dev_err(&i2c
->dev
, "selftest failed (continuing anyway)\n");
693 ret
= ak8974_reset(ak8974
);
695 dev_err(&i2c
->dev
, "AK8974 reset failed\n");
699 pm_runtime_set_autosuspend_delay(&i2c
->dev
,
700 AK8974_AUTOSUSPEND_DELAY
);
701 pm_runtime_use_autosuspend(&i2c
->dev
);
702 pm_runtime_put(&i2c
->dev
);
704 indio_dev
->dev
.parent
= &i2c
->dev
;
705 indio_dev
->channels
= ak8974_channels
;
706 indio_dev
->num_channels
= ARRAY_SIZE(ak8974_channels
);
707 indio_dev
->info
= &ak8974_info
;
708 indio_dev
->available_scan_masks
= ak8974_scan_masks
;
709 indio_dev
->modes
= INDIO_DIRECT_MODE
;
710 indio_dev
->name
= ak8974
->name
;
712 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
713 ak8974_handle_trigger
,
716 dev_err(&i2c
->dev
, "triggered buffer setup failed\n");
720 /* If we have a valid DRDY IRQ, make use of it */
722 irq_trig
= irqd_get_trigger_type(irq_get_irq_data(irq
));
723 if (irq_trig
== IRQF_TRIGGER_RISING
) {
724 dev_info(&i2c
->dev
, "enable rising edge DRDY IRQ\n");
725 } else if (irq_trig
== IRQF_TRIGGER_FALLING
) {
726 ak8974
->drdy_active_low
= true;
727 dev_info(&i2c
->dev
, "enable falling edge DRDY IRQ\n");
729 irq_trig
= IRQF_TRIGGER_RISING
;
731 irq_trig
|= IRQF_ONESHOT
;
732 irq_trig
|= IRQF_SHARED
;
734 ret
= devm_request_threaded_irq(&i2c
->dev
,
737 ak8974_drdy_irq_thread
,
742 dev_err(&i2c
->dev
, "unable to request DRDY IRQ "
743 "- proceeding without IRQ\n");
746 ak8974
->drdy_irq
= true;
750 ret
= iio_device_register(indio_dev
);
752 dev_err(&i2c
->dev
, "device register failed\n");
759 iio_triggered_buffer_cleanup(indio_dev
);
761 pm_runtime_put_noidle(&i2c
->dev
);
762 pm_runtime_disable(&i2c
->dev
);
763 ak8974_set_power(ak8974
, AK8974_PWR_OFF
);
765 regulator_bulk_disable(ARRAY_SIZE(ak8974
->regs
), ak8974
->regs
);
770 static int __exit
ak8974_remove(struct i2c_client
*i2c
)
772 struct iio_dev
*indio_dev
= i2c_get_clientdata(i2c
);
773 struct ak8974
*ak8974
= iio_priv(indio_dev
);
775 iio_device_unregister(indio_dev
);
776 iio_triggered_buffer_cleanup(indio_dev
);
777 pm_runtime_get_sync(&i2c
->dev
);
778 pm_runtime_put_noidle(&i2c
->dev
);
779 pm_runtime_disable(&i2c
->dev
);
780 ak8974_set_power(ak8974
, AK8974_PWR_OFF
);
781 regulator_bulk_disable(ARRAY_SIZE(ak8974
->regs
), ak8974
->regs
);
786 static int __maybe_unused
ak8974_runtime_suspend(struct device
*dev
)
788 struct ak8974
*ak8974
=
789 iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
791 ak8974_set_power(ak8974
, AK8974_PWR_OFF
);
792 regulator_bulk_disable(ARRAY_SIZE(ak8974
->regs
), ak8974
->regs
);
797 static int __maybe_unused
ak8974_runtime_resume(struct device
*dev
)
799 struct ak8974
*ak8974
=
800 iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
803 ret
= regulator_bulk_enable(ARRAY_SIZE(ak8974
->regs
), ak8974
->regs
);
806 msleep(AK8974_POWERON_DELAY
);
807 ret
= ak8974_set_power(ak8974
, AK8974_PWR_ON
);
809 goto out_regulator_disable
;
811 ret
= ak8974_configure(ak8974
);
813 goto out_disable_power
;
818 ak8974_set_power(ak8974
, AK8974_PWR_OFF
);
819 out_regulator_disable
:
820 regulator_bulk_disable(ARRAY_SIZE(ak8974
->regs
), ak8974
->regs
);
825 static const struct dev_pm_ops ak8974_dev_pm_ops
= {
826 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
827 pm_runtime_force_resume
)
828 SET_RUNTIME_PM_OPS(ak8974_runtime_suspend
,
829 ak8974_runtime_resume
, NULL
)
832 static const struct i2c_device_id ak8974_id
[] = {
837 MODULE_DEVICE_TABLE(i2c
, ak8974_id
);
839 static const struct of_device_id ak8974_of_match
[] = {
840 { .compatible
= "asahi-kasei,ak8974", },
843 MODULE_DEVICE_TABLE(of
, ak8974_of_match
);
845 static struct i2c_driver ak8974_driver
= {
848 .pm
= &ak8974_dev_pm_ops
,
849 .of_match_table
= of_match_ptr(ak8974_of_match
),
851 .probe
= ak8974_probe
,
852 .remove
= __exit_p(ak8974_remove
),
853 .id_table
= ak8974_id
,
855 module_i2c_driver(ak8974_driver
);
857 MODULE_DESCRIPTION("AK8974 and AMI305 3-axis magnetometer driver");
858 MODULE_AUTHOR("Samu Onkalo");
859 MODULE_AUTHOR("Linus Walleij");
860 MODULE_LICENSE("GPL v2");