1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * A sensor driver for the magnetometer AK8975.
5 * Magnetic compass sensor driver for monitoring magnetic flux information.
7 * Copyright (c) 2010, NVIDIA Corporation.
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
14 #include <linux/interrupt.h>
15 #include <linux/err.h>
16 #include <linux/mutex.h>
17 #include <linux/delay.h>
18 #include <linux/bitops.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/acpi.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/pm_runtime.h>
24 #include <linux/iio/iio.h>
25 #include <linux/iio/sysfs.h>
26 #include <linux/iio/buffer.h>
27 #include <linux/iio/trigger.h>
28 #include <linux/iio/trigger_consumer.h>
29 #include <linux/iio/triggered_buffer.h>
32 * Register definitions, as well as various shifts and masks to get at the
33 * individual fields of the registers.
35 #define AK8975_REG_WIA 0x00
36 #define AK8975_DEVICE_ID 0x48
38 #define AK8975_REG_INFO 0x01
40 #define AK8975_REG_ST1 0x02
41 #define AK8975_REG_ST1_DRDY_SHIFT 0
42 #define AK8975_REG_ST1_DRDY_MASK (1 << AK8975_REG_ST1_DRDY_SHIFT)
44 #define AK8975_REG_HXL 0x03
45 #define AK8975_REG_HXH 0x04
46 #define AK8975_REG_HYL 0x05
47 #define AK8975_REG_HYH 0x06
48 #define AK8975_REG_HZL 0x07
49 #define AK8975_REG_HZH 0x08
50 #define AK8975_REG_ST2 0x09
51 #define AK8975_REG_ST2_DERR_SHIFT 2
52 #define AK8975_REG_ST2_DERR_MASK (1 << AK8975_REG_ST2_DERR_SHIFT)
54 #define AK8975_REG_ST2_HOFL_SHIFT 3
55 #define AK8975_REG_ST2_HOFL_MASK (1 << AK8975_REG_ST2_HOFL_SHIFT)
57 #define AK8975_REG_CNTL 0x0A
58 #define AK8975_REG_CNTL_MODE_SHIFT 0
59 #define AK8975_REG_CNTL_MODE_MASK (0xF << AK8975_REG_CNTL_MODE_SHIFT)
60 #define AK8975_REG_CNTL_MODE_POWER_DOWN 0x00
61 #define AK8975_REG_CNTL_MODE_ONCE 0x01
62 #define AK8975_REG_CNTL_MODE_SELF_TEST 0x08
63 #define AK8975_REG_CNTL_MODE_FUSE_ROM 0x0F
65 #define AK8975_REG_RSVC 0x0B
66 #define AK8975_REG_ASTC 0x0C
67 #define AK8975_REG_TS1 0x0D
68 #define AK8975_REG_TS2 0x0E
69 #define AK8975_REG_I2CDIS 0x0F
70 #define AK8975_REG_ASAX 0x10
71 #define AK8975_REG_ASAY 0x11
72 #define AK8975_REG_ASAZ 0x12
74 #define AK8975_MAX_REGS AK8975_REG_ASAZ
77 * AK09912 Register definitions
79 #define AK09912_REG_WIA1 0x00
80 #define AK09912_REG_WIA2 0x01
81 #define AK09912_DEVICE_ID 0x04
82 #define AK09911_DEVICE_ID 0x05
84 #define AK09911_REG_INFO1 0x02
85 #define AK09911_REG_INFO2 0x03
87 #define AK09912_REG_ST1 0x10
89 #define AK09912_REG_ST1_DRDY_SHIFT 0
90 #define AK09912_REG_ST1_DRDY_MASK (1 << AK09912_REG_ST1_DRDY_SHIFT)
92 #define AK09912_REG_HXL 0x11
93 #define AK09912_REG_HXH 0x12
94 #define AK09912_REG_HYL 0x13
95 #define AK09912_REG_HYH 0x14
96 #define AK09912_REG_HZL 0x15
97 #define AK09912_REG_HZH 0x16
98 #define AK09912_REG_TMPS 0x17
100 #define AK09912_REG_ST2 0x18
101 #define AK09912_REG_ST2_HOFL_SHIFT 3
102 #define AK09912_REG_ST2_HOFL_MASK (1 << AK09912_REG_ST2_HOFL_SHIFT)
104 #define AK09912_REG_CNTL1 0x30
106 #define AK09912_REG_CNTL2 0x31
107 #define AK09912_REG_CNTL_MODE_POWER_DOWN 0x00
108 #define AK09912_REG_CNTL_MODE_ONCE 0x01
109 #define AK09912_REG_CNTL_MODE_SELF_TEST 0x10
110 #define AK09912_REG_CNTL_MODE_FUSE_ROM 0x1F
111 #define AK09912_REG_CNTL2_MODE_SHIFT 0
112 #define AK09912_REG_CNTL2_MODE_MASK (0x1F << AK09912_REG_CNTL2_MODE_SHIFT)
114 #define AK09912_REG_CNTL3 0x32
116 #define AK09912_REG_TS1 0x33
117 #define AK09912_REG_TS2 0x34
118 #define AK09912_REG_TS3 0x35
119 #define AK09912_REG_I2CDIS 0x36
120 #define AK09912_REG_TS4 0x37
122 #define AK09912_REG_ASAX 0x60
123 #define AK09912_REG_ASAY 0x61
124 #define AK09912_REG_ASAZ 0x62
126 #define AK09912_MAX_REGS AK09912_REG_ASAZ
129 * Miscellaneous values.
131 #define AK8975_MAX_CONVERSION_TIMEOUT 500
132 #define AK8975_CONVERSION_DONE_POLL_TIME 10
133 #define AK8975_DATA_READY_TIMEOUT ((100*HZ)/1000)
136 * Precalculate scale factor (in Gauss units) for each axis and
137 * store in the device data.
139 * This scale factor is axis-dependent, and is derived from 3 calibration
140 * factors ASA(x), ASA(y), and ASA(z).
142 * These ASA values are read from the sensor device at start of day, and
143 * cached in the device context struct.
145 * Adjusting the flux value with the sensitivity adjustment value should be
146 * done via the following formula:
148 * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
149 * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
150 * is the resultant adjusted value.
152 * We reduce the formula to:
154 * Hadj = H * (ASA + 128) / 256
156 * H is in the range of -4096 to 4095. The magnetometer has a range of
157 * +-1229uT. To go from the raw value to uT is:
159 * HuT = H * 1229/4096, or roughly, 3/10.
161 * Since 1uT = 0.01 gauss, our final scale factor becomes:
163 * Hadj = H * ((ASA + 128) / 256) * 3/10 * 1/100
164 * Hadj = H * ((ASA + 128) * 0.003) / 256
166 * Since ASA doesn't change, we cache the resultant scale factor into the
167 * device context in ak8975_setup().
169 * Given we use IIO_VAL_INT_PLUS_MICRO bit when displaying the scale, we
170 * multiply the stored scale value by 1e6.
172 static long ak8975_raw_to_gauss(u16 data
)
174 return (((long)data
+ 128) * 3000) / 256;
178 * For AK8963 and AK09911, same calculation, but the device is less sensitive:
180 * H is in the range of +-8190. The magnetometer has a range of
181 * +-4912uT. To go from the raw value to uT is:
183 * HuT = H * 4912/8190, or roughly, 6/10, instead of 3/10.
186 static long ak8963_09911_raw_to_gauss(u16 data
)
188 return (((long)data
+ 128) * 6000) / 256;
192 * For AK09912, same calculation, except the device is more sensitive:
194 * H is in the range of -32752 to 32752. The magnetometer has a range of
195 * +-4912uT. To go from the raw value to uT is:
197 * HuT = H * 4912/32752, or roughly, 3/20, instead of 3/10.
199 static long ak09912_raw_to_gauss(u16 data
)
201 return (((long)data
+ 128) * 1500) / 256;
204 /* Compatible Asahi Kasei Compass parts */
205 enum asahi_compass_chipset
{
213 enum ak_ctrl_reg_addr
{
222 enum ak_ctrl_reg_mask
{
239 enum asahi_compass_chipset type
;
240 long (*raw_to_gauss
)(u16 data
);
242 u8 ctrl_regs
[REGS_END
];
243 u8 ctrl_masks
[MASK_END
];
244 u8 ctrl_modes
[MODE_END
];
248 static const struct ak_def ak_def_array
[] = {
251 .raw_to_gauss
= ak8975_raw_to_gauss
,
260 AK8975_REG_ST1_DRDY_MASK
,
261 AK8975_REG_ST2_HOFL_MASK
,
262 AK8975_REG_ST2_DERR_MASK
,
263 AK8975_REG_CNTL_MODE_MASK
},
265 AK8975_REG_CNTL_MODE_POWER_DOWN
,
266 AK8975_REG_CNTL_MODE_ONCE
,
267 AK8975_REG_CNTL_MODE_SELF_TEST
,
268 AK8975_REG_CNTL_MODE_FUSE_ROM
},
276 .raw_to_gauss
= ak8963_09911_raw_to_gauss
,
285 AK8975_REG_ST1_DRDY_MASK
,
286 AK8975_REG_ST2_HOFL_MASK
,
288 AK8975_REG_CNTL_MODE_MASK
},
290 AK8975_REG_CNTL_MODE_POWER_DOWN
,
291 AK8975_REG_CNTL_MODE_ONCE
,
292 AK8975_REG_CNTL_MODE_SELF_TEST
,
293 AK8975_REG_CNTL_MODE_FUSE_ROM
},
301 .raw_to_gauss
= ak8963_09911_raw_to_gauss
,
310 AK09912_REG_ST1_DRDY_MASK
,
311 AK09912_REG_ST2_HOFL_MASK
,
313 AK09912_REG_CNTL2_MODE_MASK
},
315 AK09912_REG_CNTL_MODE_POWER_DOWN
,
316 AK09912_REG_CNTL_MODE_ONCE
,
317 AK09912_REG_CNTL_MODE_SELF_TEST
,
318 AK09912_REG_CNTL_MODE_FUSE_ROM
},
326 .raw_to_gauss
= ak09912_raw_to_gauss
,
335 AK09912_REG_ST1_DRDY_MASK
,
336 AK09912_REG_ST2_HOFL_MASK
,
338 AK09912_REG_CNTL2_MODE_MASK
},
340 AK09912_REG_CNTL_MODE_POWER_DOWN
,
341 AK09912_REG_CNTL_MODE_ONCE
,
342 AK09912_REG_CNTL_MODE_SELF_TEST
,
343 AK09912_REG_CNTL_MODE_FUSE_ROM
},
352 * Per-instance context data for the device.
355 struct i2c_client
*client
;
356 const struct ak_def
*def
;
359 long raw_to_gauss
[3];
360 struct gpio_desc
*eoc_gpiod
;
362 wait_queue_head_t data_ready_queue
;
365 struct iio_mount_matrix orientation
;
366 struct regulator
*vdd
;
367 struct regulator
*vid
;
370 /* Enable attached power regulator if any. */
371 static int ak8975_power_on(const struct ak8975_data
*data
)
375 ret
= regulator_enable(data
->vdd
);
377 dev_warn(&data
->client
->dev
,
378 "Failed to enable specified Vdd supply\n");
381 ret
= regulator_enable(data
->vid
);
383 dev_warn(&data
->client
->dev
,
384 "Failed to enable specified Vid supply\n");
388 * According to the datasheet the power supply rise time i 200us
389 * and the minimum wait time before mode setting is 100us, in
390 * total 300 us. Add some margin and say minimum 500us here.
392 usleep_range(500, 1000);
396 /* Disable attached power regulator if any. */
397 static void ak8975_power_off(const struct ak8975_data
*data
)
399 regulator_disable(data
->vid
);
400 regulator_disable(data
->vdd
);
404 * Return 0 if the i2c device is the one we expect.
405 * return a negative error number otherwise
407 static int ak8975_who_i_am(struct i2c_client
*client
,
408 enum asahi_compass_chipset type
)
414 * Signature for each device:
415 * Device | WIA1 | WIA2
416 * AK09912 | DEVICE_ID | AK09912_DEVICE_ID
417 * AK09911 | DEVICE_ID | AK09911_DEVICE_ID
418 * AK8975 | DEVICE_ID | NA
419 * AK8963 | DEVICE_ID | NA
421 ret
= i2c_smbus_read_i2c_block_data_or_emulated(
422 client
, AK09912_REG_WIA1
, 2, wia_val
);
424 dev_err(&client
->dev
, "Error reading WIA\n");
428 if (wia_val
[0] != AK8975_DEVICE_ID
)
436 if (wia_val
[1] == AK09911_DEVICE_ID
)
440 if (wia_val
[1] == AK09912_DEVICE_ID
)
444 dev_err(&client
->dev
, "Type %d unknown\n", type
);
450 * Helper function to write to CNTL register.
452 static int ak8975_set_mode(struct ak8975_data
*data
, enum ak_ctrl_mode mode
)
457 regval
= (data
->cntl_cache
& ~data
->def
->ctrl_masks
[CNTL_MODE
]) |
458 data
->def
->ctrl_modes
[mode
];
459 ret
= i2c_smbus_write_byte_data(data
->client
,
460 data
->def
->ctrl_regs
[CNTL
], regval
);
464 data
->cntl_cache
= regval
;
465 /* After mode change wait atleast 100us */
466 usleep_range(100, 500);
472 * Handle data ready irq
474 static irqreturn_t
ak8975_irq_handler(int irq
, void *data
)
476 struct ak8975_data
*ak8975
= data
;
478 set_bit(0, &ak8975
->flags
);
479 wake_up(&ak8975
->data_ready_queue
);
485 * Install data ready interrupt handler
487 static int ak8975_setup_irq(struct ak8975_data
*data
)
489 struct i2c_client
*client
= data
->client
;
493 init_waitqueue_head(&data
->data_ready_queue
);
494 clear_bit(0, &data
->flags
);
498 irq
= gpiod_to_irq(data
->eoc_gpiod
);
500 rc
= devm_request_irq(&client
->dev
, irq
, ak8975_irq_handler
,
501 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
502 dev_name(&client
->dev
), data
);
504 dev_err(&client
->dev
, "irq %d request failed: %d\n", irq
, rc
);
515 * Perform some start-of-day setup, including reading the asa calibration
516 * values and caching them.
518 static int ak8975_setup(struct i2c_client
*client
)
520 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
521 struct ak8975_data
*data
= iio_priv(indio_dev
);
524 /* Write the fused rom access mode. */
525 ret
= ak8975_set_mode(data
, FUSE_ROM
);
527 dev_err(&client
->dev
, "Error in setting fuse access mode\n");
531 /* Get asa data and store in the device data. */
532 ret
= i2c_smbus_read_i2c_block_data_or_emulated(
533 client
, data
->def
->ctrl_regs
[ASA_BASE
],
536 dev_err(&client
->dev
, "Not able to read asa data\n");
540 /* After reading fuse ROM data set power-down mode */
541 ret
= ak8975_set_mode(data
, POWER_DOWN
);
543 dev_err(&client
->dev
, "Error in setting power-down mode\n");
547 if (data
->eoc_gpiod
|| client
->irq
> 0) {
548 ret
= ak8975_setup_irq(data
);
550 dev_err(&client
->dev
,
551 "Error setting data ready interrupt\n");
556 data
->raw_to_gauss
[0] = data
->def
->raw_to_gauss(data
->asa
[0]);
557 data
->raw_to_gauss
[1] = data
->def
->raw_to_gauss(data
->asa
[1]);
558 data
->raw_to_gauss
[2] = data
->def
->raw_to_gauss(data
->asa
[2]);
563 static int wait_conversion_complete_gpio(struct ak8975_data
*data
)
565 struct i2c_client
*client
= data
->client
;
566 u32 timeout_ms
= AK8975_MAX_CONVERSION_TIMEOUT
;
569 /* Wait for the conversion to complete. */
571 msleep(AK8975_CONVERSION_DONE_POLL_TIME
);
572 if (gpiod_get_value(data
->eoc_gpiod
))
574 timeout_ms
-= AK8975_CONVERSION_DONE_POLL_TIME
;
577 dev_err(&client
->dev
, "Conversion timeout happened\n");
581 ret
= i2c_smbus_read_byte_data(client
, data
->def
->ctrl_regs
[ST1
]);
583 dev_err(&client
->dev
, "Error in reading ST1\n");
588 static int wait_conversion_complete_polled(struct ak8975_data
*data
)
590 struct i2c_client
*client
= data
->client
;
592 u32 timeout_ms
= AK8975_MAX_CONVERSION_TIMEOUT
;
595 /* Wait for the conversion to complete. */
597 msleep(AK8975_CONVERSION_DONE_POLL_TIME
);
598 ret
= i2c_smbus_read_byte_data(client
,
599 data
->def
->ctrl_regs
[ST1
]);
601 dev_err(&client
->dev
, "Error in reading ST1\n");
607 timeout_ms
-= AK8975_CONVERSION_DONE_POLL_TIME
;
610 dev_err(&client
->dev
, "Conversion timeout happened\n");
617 /* Returns 0 if the end of conversion interrupt occured or -ETIME otherwise */
618 static int wait_conversion_complete_interrupt(struct ak8975_data
*data
)
622 ret
= wait_event_timeout(data
->data_ready_queue
,
623 test_bit(0, &data
->flags
),
624 AK8975_DATA_READY_TIMEOUT
);
625 clear_bit(0, &data
->flags
);
627 return ret
> 0 ? 0 : -ETIME
;
630 static int ak8975_start_read_axis(struct ak8975_data
*data
,
631 const struct i2c_client
*client
)
633 /* Set up the device for taking a sample. */
634 int ret
= ak8975_set_mode(data
, MODE_ONCE
);
637 dev_err(&client
->dev
, "Error in setting operating mode\n");
641 /* Wait for the conversion to complete. */
643 ret
= wait_conversion_complete_interrupt(data
);
644 else if (data
->eoc_gpiod
)
645 ret
= wait_conversion_complete_gpio(data
);
647 ret
= wait_conversion_complete_polled(data
);
651 /* This will be executed only for non-interrupt based waiting case */
652 if (ret
& data
->def
->ctrl_masks
[ST1_DRDY
]) {
653 ret
= i2c_smbus_read_byte_data(client
,
654 data
->def
->ctrl_regs
[ST2
]);
656 dev_err(&client
->dev
, "Error in reading ST2\n");
659 if (ret
& (data
->def
->ctrl_masks
[ST2_DERR
] |
660 data
->def
->ctrl_masks
[ST2_HOFL
])) {
661 dev_err(&client
->dev
, "ST2 status error 0x%x\n", ret
);
669 /* Retrieve raw flux value for one of the x, y, or z axis. */
670 static int ak8975_read_axis(struct iio_dev
*indio_dev
, int index
, int *val
)
672 struct ak8975_data
*data
= iio_priv(indio_dev
);
673 const struct i2c_client
*client
= data
->client
;
674 const struct ak_def
*def
= data
->def
;
679 pm_runtime_get_sync(&data
->client
->dev
);
681 mutex_lock(&data
->lock
);
683 ret
= ak8975_start_read_axis(data
, client
);
687 ret
= i2c_smbus_read_i2c_block_data_or_emulated(
688 client
, def
->data_regs
[index
],
689 sizeof(rval
), (u8
*)&rval
);
693 mutex_unlock(&data
->lock
);
695 pm_runtime_mark_last_busy(&data
->client
->dev
);
696 pm_runtime_put_autosuspend(&data
->client
->dev
);
698 /* Swap bytes and convert to valid range. */
699 buff
= le16_to_cpu(rval
);
700 *val
= clamp_t(s16
, buff
, -def
->range
, def
->range
);
704 mutex_unlock(&data
->lock
);
705 dev_err(&client
->dev
, "Error in reading axis\n");
709 static int ak8975_read_raw(struct iio_dev
*indio_dev
,
710 struct iio_chan_spec
const *chan
,
714 struct ak8975_data
*data
= iio_priv(indio_dev
);
717 case IIO_CHAN_INFO_RAW
:
718 return ak8975_read_axis(indio_dev
, chan
->address
, val
);
719 case IIO_CHAN_INFO_SCALE
:
721 *val2
= data
->raw_to_gauss
[chan
->address
];
722 return IIO_VAL_INT_PLUS_MICRO
;
727 static const struct iio_mount_matrix
*
728 ak8975_get_mount_matrix(const struct iio_dev
*indio_dev
,
729 const struct iio_chan_spec
*chan
)
731 struct ak8975_data
*data
= iio_priv(indio_dev
);
733 return &data
->orientation
;
736 static const struct iio_chan_spec_ext_info ak8975_ext_info
[] = {
737 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR
, ak8975_get_mount_matrix
),
741 #define AK8975_CHANNEL(axis, index) \
745 .channel2 = IIO_MOD_##axis, \
746 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
747 BIT(IIO_CHAN_INFO_SCALE), \
749 .scan_index = index, \
754 .endianness = IIO_CPU \
756 .ext_info = ak8975_ext_info, \
759 static const struct iio_chan_spec ak8975_channels
[] = {
760 AK8975_CHANNEL(X
, 0), AK8975_CHANNEL(Y
, 1), AK8975_CHANNEL(Z
, 2),
761 IIO_CHAN_SOFT_TIMESTAMP(3),
764 static const unsigned long ak8975_scan_masks
[] = { 0x7, 0 };
766 static const struct iio_info ak8975_info
= {
767 .read_raw
= &ak8975_read_raw
,
771 static const struct acpi_device_id ak_acpi_match
[] = {
774 {"INVN6500", AK8963
},
775 {"AK009911", AK09911
},
776 {"AK09911", AK09911
},
777 {"AKM9911", AK09911
},
778 {"AK09912", AK09912
},
781 MODULE_DEVICE_TABLE(acpi
, ak_acpi_match
);
784 static void ak8975_fill_buffer(struct iio_dev
*indio_dev
)
786 struct ak8975_data
*data
= iio_priv(indio_dev
);
787 const struct i2c_client
*client
= data
->client
;
788 const struct ak_def
*def
= data
->def
;
790 s16 buff
[8]; /* 3 x 16 bits axis values + 1 aligned 64 bits timestamp */
793 mutex_lock(&data
->lock
);
795 ret
= ak8975_start_read_axis(data
, client
);
800 * For each axis, read the flux value from the appropriate register
801 * (the register is specified in the iio device attributes).
803 ret
= i2c_smbus_read_i2c_block_data_or_emulated(client
,
810 mutex_unlock(&data
->lock
);
812 /* Clamp to valid range. */
813 buff
[0] = clamp_t(s16
, le16_to_cpu(fval
[0]), -def
->range
, def
->range
);
814 buff
[1] = clamp_t(s16
, le16_to_cpu(fval
[1]), -def
->range
, def
->range
);
815 buff
[2] = clamp_t(s16
, le16_to_cpu(fval
[2]), -def
->range
, def
->range
);
817 iio_push_to_buffers_with_timestamp(indio_dev
, buff
,
818 iio_get_time_ns(indio_dev
));
822 mutex_unlock(&data
->lock
);
823 dev_err(&client
->dev
, "Error in reading axes block\n");
826 static irqreturn_t
ak8975_handle_trigger(int irq
, void *p
)
828 const struct iio_poll_func
*pf
= p
;
829 struct iio_dev
*indio_dev
= pf
->indio_dev
;
831 ak8975_fill_buffer(indio_dev
);
832 iio_trigger_notify_done(indio_dev
->trig
);
836 static int ak8975_probe(struct i2c_client
*client
,
837 const struct i2c_device_id
*id
)
839 struct ak8975_data
*data
;
840 struct iio_dev
*indio_dev
;
841 struct gpio_desc
*eoc_gpiod
;
845 enum asahi_compass_chipset chipset
;
846 const char *name
= NULL
;
849 * Grab and set up the supplied GPIO.
850 * We may not have a GPIO based IRQ to scan, that is fine, we will
853 eoc_gpiod
= devm_gpiod_get_optional(&client
->dev
, NULL
, GPIOD_IN
);
854 if (IS_ERR(eoc_gpiod
))
855 return PTR_ERR(eoc_gpiod
);
857 gpiod_set_consumer_name(eoc_gpiod
, "ak_8975");
859 /* Register with IIO */
860 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
861 if (indio_dev
== NULL
)
864 data
= iio_priv(indio_dev
);
865 i2c_set_clientdata(client
, indio_dev
);
867 data
->client
= client
;
868 data
->eoc_gpiod
= eoc_gpiod
;
871 err
= iio_read_mount_matrix(&client
->dev
, "mount-matrix", &data
->orientation
);
875 /* id will be NULL when enumerated via ACPI */
876 match
= device_get_match_data(&client
->dev
);
878 chipset
= (enum asahi_compass_chipset
)(match
);
879 name
= dev_name(&client
->dev
);
881 chipset
= (enum asahi_compass_chipset
)(id
->driver_data
);
886 for (i
= 0; i
< ARRAY_SIZE(ak_def_array
); i
++)
887 if (ak_def_array
[i
].type
== chipset
)
890 if (i
== ARRAY_SIZE(ak_def_array
)) {
891 dev_err(&client
->dev
, "AKM device type unsupported: %d\n",
896 data
->def
= &ak_def_array
[i
];
898 /* Fetch the regulators */
899 data
->vdd
= devm_regulator_get(&client
->dev
, "vdd");
900 if (IS_ERR(data
->vdd
))
901 return PTR_ERR(data
->vdd
);
902 data
->vid
= devm_regulator_get(&client
->dev
, "vid");
903 if (IS_ERR(data
->vid
))
904 return PTR_ERR(data
->vid
);
906 err
= ak8975_power_on(data
);
910 err
= ak8975_who_i_am(client
, data
->def
->type
);
912 dev_err(&client
->dev
, "Unexpected device\n");
915 dev_dbg(&client
->dev
, "Asahi compass chip %s\n", name
);
917 /* Perform some basic start-of-day setup of the device. */
918 err
= ak8975_setup(client
);
920 dev_err(&client
->dev
, "%s initialization fails\n", name
);
924 mutex_init(&data
->lock
);
925 indio_dev
->dev
.parent
= &client
->dev
;
926 indio_dev
->channels
= ak8975_channels
;
927 indio_dev
->num_channels
= ARRAY_SIZE(ak8975_channels
);
928 indio_dev
->info
= &ak8975_info
;
929 indio_dev
->available_scan_masks
= ak8975_scan_masks
;
930 indio_dev
->modes
= INDIO_DIRECT_MODE
;
931 indio_dev
->name
= name
;
933 err
= iio_triggered_buffer_setup(indio_dev
, NULL
, ak8975_handle_trigger
,
936 dev_err(&client
->dev
, "triggered buffer setup failed\n");
940 err
= iio_device_register(indio_dev
);
942 dev_err(&client
->dev
, "device register failed\n");
946 /* Enable runtime PM */
947 pm_runtime_get_noresume(&client
->dev
);
948 pm_runtime_set_active(&client
->dev
);
949 pm_runtime_enable(&client
->dev
);
951 * The device comes online in 500us, so add two orders of magnitude
952 * of delay before autosuspending: 50 ms.
954 pm_runtime_set_autosuspend_delay(&client
->dev
, 50);
955 pm_runtime_use_autosuspend(&client
->dev
);
956 pm_runtime_put(&client
->dev
);
961 iio_triggered_buffer_cleanup(indio_dev
);
963 ak8975_power_off(data
);
967 static int ak8975_remove(struct i2c_client
*client
)
969 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
970 struct ak8975_data
*data
= iio_priv(indio_dev
);
972 pm_runtime_get_sync(&client
->dev
);
973 pm_runtime_put_noidle(&client
->dev
);
974 pm_runtime_disable(&client
->dev
);
975 iio_device_unregister(indio_dev
);
976 iio_triggered_buffer_cleanup(indio_dev
);
977 ak8975_set_mode(data
, POWER_DOWN
);
978 ak8975_power_off(data
);
984 static int ak8975_runtime_suspend(struct device
*dev
)
986 struct i2c_client
*client
= to_i2c_client(dev
);
987 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
988 struct ak8975_data
*data
= iio_priv(indio_dev
);
991 /* Set the device in power down if it wasn't already */
992 ret
= ak8975_set_mode(data
, POWER_DOWN
);
994 dev_err(&client
->dev
, "Error in setting power-down mode\n");
997 /* Next cut the regulators */
998 ak8975_power_off(data
);
1003 static int ak8975_runtime_resume(struct device
*dev
)
1005 struct i2c_client
*client
= to_i2c_client(dev
);
1006 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
1007 struct ak8975_data
*data
= iio_priv(indio_dev
);
1010 /* Take up the regulators */
1011 ak8975_power_on(data
);
1013 * We come up in powered down mode, the reading routines will
1014 * put us in the mode to read values later.
1016 ret
= ak8975_set_mode(data
, POWER_DOWN
);
1018 dev_err(&client
->dev
, "Error in setting power-down mode\n");
1024 #endif /* CONFIG_PM */
1026 static const struct dev_pm_ops ak8975_dev_pm_ops
= {
1027 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
1028 pm_runtime_force_resume
)
1029 SET_RUNTIME_PM_OPS(ak8975_runtime_suspend
,
1030 ak8975_runtime_resume
, NULL
)
1033 static const struct i2c_device_id ak8975_id
[] = {
1037 {"ak09911", AK09911
},
1038 {"ak09912", AK09912
},
1042 MODULE_DEVICE_TABLE(i2c
, ak8975_id
);
1044 static const struct of_device_id ak8975_of_match
[] = {
1045 { .compatible
= "asahi-kasei,ak8975", },
1046 { .compatible
= "ak8975", },
1047 { .compatible
= "asahi-kasei,ak8963", },
1048 { .compatible
= "ak8963", },
1049 { .compatible
= "asahi-kasei,ak09911", },
1050 { .compatible
= "ak09911", },
1051 { .compatible
= "asahi-kasei,ak09912", },
1052 { .compatible
= "ak09912", },
1055 MODULE_DEVICE_TABLE(of
, ak8975_of_match
);
1057 static struct i2c_driver ak8975_driver
= {
1060 .pm
= &ak8975_dev_pm_ops
,
1061 .of_match_table
= of_match_ptr(ak8975_of_match
),
1062 .acpi_match_table
= ACPI_PTR(ak_acpi_match
),
1064 .probe
= ak8975_probe
,
1065 .remove
= ak8975_remove
,
1066 .id_table
= ak8975_id
,
1068 module_i2c_driver(ak8975_driver
);
1070 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
1071 MODULE_DESCRIPTION("AK8975 magnetometer driver");
1072 MODULE_LICENSE("GPL");