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/mod_devicetable.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/i2c.h>
15 #include <linux/interrupt.h>
16 #include <linux/err.h>
17 #include <linux/mutex.h>
18 #include <linux/delay.h>
19 #include <linux/bitops.h>
20 #include <linux/gpio/consumer.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
;
361 struct gpio_desc
*reset_gpiod
;
363 wait_queue_head_t data_ready_queue
;
366 struct iio_mount_matrix orientation
;
367 struct regulator
*vdd
;
368 struct regulator
*vid
;
370 /* Ensure natural alignment of timestamp */
377 /* Enable attached power regulator if any. */
378 static int ak8975_power_on(const struct ak8975_data
*data
)
382 ret
= regulator_enable(data
->vdd
);
384 dev_warn(&data
->client
->dev
,
385 "Failed to enable specified Vdd supply\n");
388 ret
= regulator_enable(data
->vid
);
390 dev_warn(&data
->client
->dev
,
391 "Failed to enable specified Vid supply\n");
395 gpiod_set_value_cansleep(data
->reset_gpiod
, 0);
398 * According to the datasheet the power supply rise time is 200us
399 * and the minimum wait time before mode setting is 100us, in
400 * total 300us. Add some margin and say minimum 500us here.
402 usleep_range(500, 1000);
406 /* Disable attached power regulator if any. */
407 static void ak8975_power_off(const struct ak8975_data
*data
)
409 gpiod_set_value_cansleep(data
->reset_gpiod
, 1);
411 regulator_disable(data
->vid
);
412 regulator_disable(data
->vdd
);
416 * Return 0 if the i2c device is the one we expect.
417 * return a negative error number otherwise
419 static int ak8975_who_i_am(struct i2c_client
*client
,
420 enum asahi_compass_chipset type
)
426 * Signature for each device:
427 * Device | WIA1 | WIA2
428 * AK09912 | DEVICE_ID | AK09912_DEVICE_ID
429 * AK09911 | DEVICE_ID | AK09911_DEVICE_ID
430 * AK8975 | DEVICE_ID | NA
431 * AK8963 | DEVICE_ID | NA
433 ret
= i2c_smbus_read_i2c_block_data_or_emulated(
434 client
, AK09912_REG_WIA1
, 2, wia_val
);
436 dev_err(&client
->dev
, "Error reading WIA\n");
440 if (wia_val
[0] != AK8975_DEVICE_ID
)
448 if (wia_val
[1] == AK09911_DEVICE_ID
)
452 if (wia_val
[1] == AK09912_DEVICE_ID
)
456 dev_err(&client
->dev
, "Type %d unknown\n", type
);
462 * Helper function to write to CNTL register.
464 static int ak8975_set_mode(struct ak8975_data
*data
, enum ak_ctrl_mode mode
)
469 regval
= (data
->cntl_cache
& ~data
->def
->ctrl_masks
[CNTL_MODE
]) |
470 data
->def
->ctrl_modes
[mode
];
471 ret
= i2c_smbus_write_byte_data(data
->client
,
472 data
->def
->ctrl_regs
[CNTL
], regval
);
476 data
->cntl_cache
= regval
;
477 /* After mode change wait atleast 100us */
478 usleep_range(100, 500);
484 * Handle data ready irq
486 static irqreturn_t
ak8975_irq_handler(int irq
, void *data
)
488 struct ak8975_data
*ak8975
= data
;
490 set_bit(0, &ak8975
->flags
);
491 wake_up(&ak8975
->data_ready_queue
);
497 * Install data ready interrupt handler
499 static int ak8975_setup_irq(struct ak8975_data
*data
)
501 struct i2c_client
*client
= data
->client
;
505 init_waitqueue_head(&data
->data_ready_queue
);
506 clear_bit(0, &data
->flags
);
510 irq
= gpiod_to_irq(data
->eoc_gpiod
);
512 rc
= devm_request_irq(&client
->dev
, irq
, ak8975_irq_handler
,
513 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
514 dev_name(&client
->dev
), data
);
516 dev_err(&client
->dev
, "irq %d request failed: %d\n", irq
, rc
);
527 * Perform some start-of-day setup, including reading the asa calibration
528 * values and caching them.
530 static int ak8975_setup(struct i2c_client
*client
)
532 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
533 struct ak8975_data
*data
= iio_priv(indio_dev
);
536 /* Write the fused rom access mode. */
537 ret
= ak8975_set_mode(data
, FUSE_ROM
);
539 dev_err(&client
->dev
, "Error in setting fuse access mode\n");
543 /* Get asa data and store in the device data. */
544 ret
= i2c_smbus_read_i2c_block_data_or_emulated(
545 client
, data
->def
->ctrl_regs
[ASA_BASE
],
548 dev_err(&client
->dev
, "Not able to read asa data\n");
552 /* After reading fuse ROM data set power-down mode */
553 ret
= ak8975_set_mode(data
, POWER_DOWN
);
555 dev_err(&client
->dev
, "Error in setting power-down mode\n");
559 if (data
->eoc_gpiod
|| client
->irq
> 0) {
560 ret
= ak8975_setup_irq(data
);
562 dev_err(&client
->dev
,
563 "Error setting data ready interrupt\n");
568 data
->raw_to_gauss
[0] = data
->def
->raw_to_gauss(data
->asa
[0]);
569 data
->raw_to_gauss
[1] = data
->def
->raw_to_gauss(data
->asa
[1]);
570 data
->raw_to_gauss
[2] = data
->def
->raw_to_gauss(data
->asa
[2]);
575 static int wait_conversion_complete_gpio(struct ak8975_data
*data
)
577 struct i2c_client
*client
= data
->client
;
578 u32 timeout_ms
= AK8975_MAX_CONVERSION_TIMEOUT
;
581 /* Wait for the conversion to complete. */
583 msleep(AK8975_CONVERSION_DONE_POLL_TIME
);
584 if (gpiod_get_value(data
->eoc_gpiod
))
586 timeout_ms
-= AK8975_CONVERSION_DONE_POLL_TIME
;
589 dev_err(&client
->dev
, "Conversion timeout happened\n");
593 ret
= i2c_smbus_read_byte_data(client
, data
->def
->ctrl_regs
[ST1
]);
595 dev_err(&client
->dev
, "Error in reading ST1\n");
600 static int wait_conversion_complete_polled(struct ak8975_data
*data
)
602 struct i2c_client
*client
= data
->client
;
604 u32 timeout_ms
= AK8975_MAX_CONVERSION_TIMEOUT
;
607 /* Wait for the conversion to complete. */
609 msleep(AK8975_CONVERSION_DONE_POLL_TIME
);
610 ret
= i2c_smbus_read_byte_data(client
,
611 data
->def
->ctrl_regs
[ST1
]);
613 dev_err(&client
->dev
, "Error in reading ST1\n");
619 timeout_ms
-= AK8975_CONVERSION_DONE_POLL_TIME
;
622 dev_err(&client
->dev
, "Conversion timeout happened\n");
629 /* Returns 0 if the end of conversion interrupt occured or -ETIME otherwise */
630 static int wait_conversion_complete_interrupt(struct ak8975_data
*data
)
634 ret
= wait_event_timeout(data
->data_ready_queue
,
635 test_bit(0, &data
->flags
),
636 AK8975_DATA_READY_TIMEOUT
);
637 clear_bit(0, &data
->flags
);
639 return ret
> 0 ? 0 : -ETIME
;
642 static int ak8975_start_read_axis(struct ak8975_data
*data
,
643 const struct i2c_client
*client
)
645 /* Set up the device for taking a sample. */
646 int ret
= ak8975_set_mode(data
, MODE_ONCE
);
649 dev_err(&client
->dev
, "Error in setting operating mode\n");
653 /* Wait for the conversion to complete. */
655 ret
= wait_conversion_complete_interrupt(data
);
656 else if (data
->eoc_gpiod
)
657 ret
= wait_conversion_complete_gpio(data
);
659 ret
= wait_conversion_complete_polled(data
);
663 /* This will be executed only for non-interrupt based waiting case */
664 if (ret
& data
->def
->ctrl_masks
[ST1_DRDY
]) {
665 ret
= i2c_smbus_read_byte_data(client
,
666 data
->def
->ctrl_regs
[ST2
]);
668 dev_err(&client
->dev
, "Error in reading ST2\n");
671 if (ret
& (data
->def
->ctrl_masks
[ST2_DERR
] |
672 data
->def
->ctrl_masks
[ST2_HOFL
])) {
673 dev_err(&client
->dev
, "ST2 status error 0x%x\n", ret
);
681 /* Retrieve raw flux value for one of the x, y, or z axis. */
682 static int ak8975_read_axis(struct iio_dev
*indio_dev
, int index
, int *val
)
684 struct ak8975_data
*data
= iio_priv(indio_dev
);
685 const struct i2c_client
*client
= data
->client
;
686 const struct ak_def
*def
= data
->def
;
691 pm_runtime_get_sync(&data
->client
->dev
);
693 mutex_lock(&data
->lock
);
695 ret
= ak8975_start_read_axis(data
, client
);
699 ret
= i2c_smbus_read_i2c_block_data_or_emulated(
700 client
, def
->data_regs
[index
],
701 sizeof(rval
), (u8
*)&rval
);
705 mutex_unlock(&data
->lock
);
707 pm_runtime_mark_last_busy(&data
->client
->dev
);
708 pm_runtime_put_autosuspend(&data
->client
->dev
);
710 /* Swap bytes and convert to valid range. */
711 buff
= le16_to_cpu(rval
);
712 *val
= clamp_t(s16
, buff
, -def
->range
, def
->range
);
716 mutex_unlock(&data
->lock
);
717 dev_err(&client
->dev
, "Error in reading axis\n");
721 static int ak8975_read_raw(struct iio_dev
*indio_dev
,
722 struct iio_chan_spec
const *chan
,
726 struct ak8975_data
*data
= iio_priv(indio_dev
);
729 case IIO_CHAN_INFO_RAW
:
730 return ak8975_read_axis(indio_dev
, chan
->address
, val
);
731 case IIO_CHAN_INFO_SCALE
:
733 *val2
= data
->raw_to_gauss
[chan
->address
];
734 return IIO_VAL_INT_PLUS_MICRO
;
739 static const struct iio_mount_matrix
*
740 ak8975_get_mount_matrix(const struct iio_dev
*indio_dev
,
741 const struct iio_chan_spec
*chan
)
743 struct ak8975_data
*data
= iio_priv(indio_dev
);
745 return &data
->orientation
;
748 static const struct iio_chan_spec_ext_info ak8975_ext_info
[] = {
749 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR
, ak8975_get_mount_matrix
),
753 #define AK8975_CHANNEL(axis, index) \
757 .channel2 = IIO_MOD_##axis, \
758 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
759 BIT(IIO_CHAN_INFO_SCALE), \
761 .scan_index = index, \
766 .endianness = IIO_CPU \
768 .ext_info = ak8975_ext_info, \
771 static const struct iio_chan_spec ak8975_channels
[] = {
772 AK8975_CHANNEL(X
, 0), AK8975_CHANNEL(Y
, 1), AK8975_CHANNEL(Z
, 2),
773 IIO_CHAN_SOFT_TIMESTAMP(3),
776 static const unsigned long ak8975_scan_masks
[] = { 0x7, 0 };
778 static const struct iio_info ak8975_info
= {
779 .read_raw
= &ak8975_read_raw
,
782 static const struct acpi_device_id ak_acpi_match
[] = {
785 {"INVN6500", AK8963
},
786 {"AK009911", AK09911
},
787 {"AK09911", AK09911
},
788 {"AKM9911", AK09911
},
789 {"AK09912", AK09912
},
792 MODULE_DEVICE_TABLE(acpi
, ak_acpi_match
);
794 static void ak8975_fill_buffer(struct iio_dev
*indio_dev
)
796 struct ak8975_data
*data
= iio_priv(indio_dev
);
797 const struct i2c_client
*client
= data
->client
;
798 const struct ak_def
*def
= data
->def
;
802 mutex_lock(&data
->lock
);
804 ret
= ak8975_start_read_axis(data
, client
);
809 * For each axis, read the flux value from the appropriate register
810 * (the register is specified in the iio device attributes).
812 ret
= i2c_smbus_read_i2c_block_data_or_emulated(client
,
819 mutex_unlock(&data
->lock
);
821 /* Clamp to valid range. */
822 data
->scan
.channels
[0] = clamp_t(s16
, le16_to_cpu(fval
[0]), -def
->range
, def
->range
);
823 data
->scan
.channels
[1] = clamp_t(s16
, le16_to_cpu(fval
[1]), -def
->range
, def
->range
);
824 data
->scan
.channels
[2] = clamp_t(s16
, le16_to_cpu(fval
[2]), -def
->range
, def
->range
);
826 iio_push_to_buffers_with_timestamp(indio_dev
, &data
->scan
,
827 iio_get_time_ns(indio_dev
));
832 mutex_unlock(&data
->lock
);
833 dev_err(&client
->dev
, "Error in reading axes block\n");
836 static irqreturn_t
ak8975_handle_trigger(int irq
, void *p
)
838 const struct iio_poll_func
*pf
= p
;
839 struct iio_dev
*indio_dev
= pf
->indio_dev
;
841 ak8975_fill_buffer(indio_dev
);
842 iio_trigger_notify_done(indio_dev
->trig
);
846 static int ak8975_probe(struct i2c_client
*client
,
847 const struct i2c_device_id
*id
)
849 struct ak8975_data
*data
;
850 struct iio_dev
*indio_dev
;
851 struct gpio_desc
*eoc_gpiod
;
852 struct gpio_desc
*reset_gpiod
;
856 enum asahi_compass_chipset chipset
;
857 const char *name
= NULL
;
860 * Grab and set up the supplied GPIO.
861 * We may not have a GPIO based IRQ to scan, that is fine, we will
864 eoc_gpiod
= devm_gpiod_get_optional(&client
->dev
, NULL
, GPIOD_IN
);
865 if (IS_ERR(eoc_gpiod
))
866 return PTR_ERR(eoc_gpiod
);
868 gpiod_set_consumer_name(eoc_gpiod
, "ak_8975");
871 * According to AK09911 datasheet, if reset GPIO is provided then
872 * deassert reset on ak8975_power_on() and assert reset on
873 * ak8975_power_off().
875 reset_gpiod
= devm_gpiod_get_optional(&client
->dev
,
876 "reset", GPIOD_OUT_HIGH
);
877 if (IS_ERR(reset_gpiod
))
878 return PTR_ERR(reset_gpiod
);
880 /* Register with IIO */
881 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
882 if (indio_dev
== NULL
)
885 data
= iio_priv(indio_dev
);
886 i2c_set_clientdata(client
, indio_dev
);
888 data
->client
= client
;
889 data
->eoc_gpiod
= eoc_gpiod
;
890 data
->reset_gpiod
= reset_gpiod
;
893 err
= iio_read_mount_matrix(&client
->dev
, "mount-matrix", &data
->orientation
);
897 /* id will be NULL when enumerated via ACPI */
898 match
= device_get_match_data(&client
->dev
);
900 chipset
= (enum asahi_compass_chipset
)(match
);
901 name
= dev_name(&client
->dev
);
903 chipset
= (enum asahi_compass_chipset
)(id
->driver_data
);
908 for (i
= 0; i
< ARRAY_SIZE(ak_def_array
); i
++)
909 if (ak_def_array
[i
].type
== chipset
)
912 if (i
== ARRAY_SIZE(ak_def_array
)) {
913 dev_err(&client
->dev
, "AKM device type unsupported: %d\n",
918 data
->def
= &ak_def_array
[i
];
920 /* Fetch the regulators */
921 data
->vdd
= devm_regulator_get(&client
->dev
, "vdd");
922 if (IS_ERR(data
->vdd
))
923 return PTR_ERR(data
->vdd
);
924 data
->vid
= devm_regulator_get(&client
->dev
, "vid");
925 if (IS_ERR(data
->vid
))
926 return PTR_ERR(data
->vid
);
928 err
= ak8975_power_on(data
);
932 err
= ak8975_who_i_am(client
, data
->def
->type
);
934 dev_err(&client
->dev
, "Unexpected device\n");
937 dev_dbg(&client
->dev
, "Asahi compass chip %s\n", name
);
939 /* Perform some basic start-of-day setup of the device. */
940 err
= ak8975_setup(client
);
942 dev_err(&client
->dev
, "%s initialization fails\n", name
);
946 mutex_init(&data
->lock
);
947 indio_dev
->channels
= ak8975_channels
;
948 indio_dev
->num_channels
= ARRAY_SIZE(ak8975_channels
);
949 indio_dev
->info
= &ak8975_info
;
950 indio_dev
->available_scan_masks
= ak8975_scan_masks
;
951 indio_dev
->modes
= INDIO_DIRECT_MODE
;
952 indio_dev
->name
= name
;
954 err
= iio_triggered_buffer_setup(indio_dev
, NULL
, ak8975_handle_trigger
,
957 dev_err(&client
->dev
, "triggered buffer setup failed\n");
961 err
= iio_device_register(indio_dev
);
963 dev_err(&client
->dev
, "device register failed\n");
967 /* Enable runtime PM */
968 pm_runtime_get_noresume(&client
->dev
);
969 pm_runtime_set_active(&client
->dev
);
970 pm_runtime_enable(&client
->dev
);
972 * The device comes online in 500us, so add two orders of magnitude
973 * of delay before autosuspending: 50 ms.
975 pm_runtime_set_autosuspend_delay(&client
->dev
, 50);
976 pm_runtime_use_autosuspend(&client
->dev
);
977 pm_runtime_put(&client
->dev
);
982 iio_triggered_buffer_cleanup(indio_dev
);
984 ak8975_power_off(data
);
988 static int ak8975_remove(struct i2c_client
*client
)
990 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
991 struct ak8975_data
*data
= iio_priv(indio_dev
);
993 pm_runtime_get_sync(&client
->dev
);
994 pm_runtime_put_noidle(&client
->dev
);
995 pm_runtime_disable(&client
->dev
);
996 iio_device_unregister(indio_dev
);
997 iio_triggered_buffer_cleanup(indio_dev
);
998 ak8975_set_mode(data
, POWER_DOWN
);
999 ak8975_power_off(data
);
1005 static int ak8975_runtime_suspend(struct device
*dev
)
1007 struct i2c_client
*client
= to_i2c_client(dev
);
1008 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
1009 struct ak8975_data
*data
= iio_priv(indio_dev
);
1012 /* Set the device in power down if it wasn't already */
1013 ret
= ak8975_set_mode(data
, POWER_DOWN
);
1015 dev_err(&client
->dev
, "Error in setting power-down mode\n");
1018 /* Next cut the regulators */
1019 ak8975_power_off(data
);
1024 static int ak8975_runtime_resume(struct device
*dev
)
1026 struct i2c_client
*client
= to_i2c_client(dev
);
1027 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
1028 struct ak8975_data
*data
= iio_priv(indio_dev
);
1031 /* Take up the regulators */
1032 ak8975_power_on(data
);
1034 * We come up in powered down mode, the reading routines will
1035 * put us in the mode to read values later.
1037 ret
= ak8975_set_mode(data
, POWER_DOWN
);
1039 dev_err(&client
->dev
, "Error in setting power-down mode\n");
1045 #endif /* CONFIG_PM */
1047 static const struct dev_pm_ops ak8975_dev_pm_ops
= {
1048 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
1049 pm_runtime_force_resume
)
1050 SET_RUNTIME_PM_OPS(ak8975_runtime_suspend
,
1051 ak8975_runtime_resume
, NULL
)
1054 static const struct i2c_device_id ak8975_id
[] = {
1058 {"ak09911", AK09911
},
1059 {"ak09912", AK09912
},
1063 MODULE_DEVICE_TABLE(i2c
, ak8975_id
);
1065 static const struct of_device_id ak8975_of_match
[] = {
1066 { .compatible
= "asahi-kasei,ak8975", },
1067 { .compatible
= "ak8975", },
1068 { .compatible
= "asahi-kasei,ak8963", },
1069 { .compatible
= "ak8963", },
1070 { .compatible
= "asahi-kasei,ak09911", },
1071 { .compatible
= "ak09911", },
1072 { .compatible
= "asahi-kasei,ak09912", },
1073 { .compatible
= "ak09912", },
1076 MODULE_DEVICE_TABLE(of
, ak8975_of_match
);
1078 static struct i2c_driver ak8975_driver
= {
1081 .pm
= &ak8975_dev_pm_ops
,
1082 .of_match_table
= ak8975_of_match
,
1083 .acpi_match_table
= ak_acpi_match
,
1085 .probe
= ak8975_probe
,
1086 .remove
= ak8975_remove
,
1087 .id_table
= ak8975_id
,
1089 module_i2c_driver(ak8975_driver
);
1091 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
1092 MODULE_DESCRIPTION("AK8975 magnetometer driver");
1093 MODULE_LICENSE("GPL");