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 AK09918_DEVICE_ID 0x0C
82 #define AK09916_DEVICE_ID 0x09
83 #define AK09912_DEVICE_ID 0x04
84 #define AK09911_DEVICE_ID 0x05
86 #define AK09911_REG_INFO1 0x02
87 #define AK09911_REG_INFO2 0x03
89 #define AK09912_REG_ST1 0x10
91 #define AK09912_REG_ST1_DRDY_SHIFT 0
92 #define AK09912_REG_ST1_DRDY_MASK (1 << AK09912_REG_ST1_DRDY_SHIFT)
94 #define AK09912_REG_HXL 0x11
95 #define AK09912_REG_HXH 0x12
96 #define AK09912_REG_HYL 0x13
97 #define AK09912_REG_HYH 0x14
98 #define AK09912_REG_HZL 0x15
99 #define AK09912_REG_HZH 0x16
100 #define AK09912_REG_TMPS 0x17
102 #define AK09912_REG_ST2 0x18
103 #define AK09912_REG_ST2_HOFL_SHIFT 3
104 #define AK09912_REG_ST2_HOFL_MASK (1 << AK09912_REG_ST2_HOFL_SHIFT)
106 #define AK09912_REG_CNTL1 0x30
108 #define AK09912_REG_CNTL2 0x31
109 #define AK09912_REG_CNTL_MODE_POWER_DOWN 0x00
110 #define AK09912_REG_CNTL_MODE_ONCE 0x01
111 #define AK09912_REG_CNTL_MODE_SELF_TEST 0x10
112 #define AK09912_REG_CNTL_MODE_FUSE_ROM 0x1F
113 #define AK09912_REG_CNTL2_MODE_SHIFT 0
114 #define AK09912_REG_CNTL2_MODE_MASK (0x1F << AK09912_REG_CNTL2_MODE_SHIFT)
116 #define AK09912_REG_CNTL3 0x32
118 #define AK09912_REG_TS1 0x33
119 #define AK09912_REG_TS2 0x34
120 #define AK09912_REG_TS3 0x35
121 #define AK09912_REG_I2CDIS 0x36
122 #define AK09912_REG_TS4 0x37
124 #define AK09912_REG_ASAX 0x60
125 #define AK09912_REG_ASAY 0x61
126 #define AK09912_REG_ASAZ 0x62
128 #define AK09912_MAX_REGS AK09912_REG_ASAZ
131 * Miscellaneous values.
133 #define AK8975_MAX_CONVERSION_TIMEOUT 500
134 #define AK8975_CONVERSION_DONE_POLL_TIME 10
135 #define AK8975_DATA_READY_TIMEOUT ((100*HZ)/1000)
138 * Precalculate scale factor (in Gauss units) for each axis and
139 * store in the device data.
141 * This scale factor is axis-dependent, and is derived from 3 calibration
142 * factors ASA(x), ASA(y), and ASA(z).
144 * These ASA values are read from the sensor device at start of day, and
145 * cached in the device context struct.
147 * Adjusting the flux value with the sensitivity adjustment value should be
148 * done via the following formula:
150 * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
151 * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
152 * is the resultant adjusted value.
154 * We reduce the formula to:
156 * Hadj = H * (ASA + 128) / 256
158 * H is in the range of -4096 to 4095. The magnetometer has a range of
159 * +-1229uT. To go from the raw value to uT is:
161 * HuT = H * 1229/4096, or roughly, 3/10.
163 * Since 1uT = 0.01 gauss, our final scale factor becomes:
165 * Hadj = H * ((ASA + 128) / 256) * 3/10 * 1/100
166 * Hadj = H * ((ASA + 128) * 0.003) / 256
168 * Since ASA doesn't change, we cache the resultant scale factor into the
169 * device context in ak8975_setup().
171 * Given we use IIO_VAL_INT_PLUS_MICRO bit when displaying the scale, we
172 * multiply the stored scale value by 1e6.
174 static long ak8975_raw_to_gauss(u16 data
)
176 return (((long)data
+ 128) * 3000) / 256;
180 * For AK8963 and AK09911, same calculation, but the device is less sensitive:
182 * H is in the range of +-8190. The magnetometer has a range of
183 * +-4912uT. To go from the raw value to uT is:
185 * HuT = H * 4912/8190, or roughly, 6/10, instead of 3/10.
188 static long ak8963_09911_raw_to_gauss(u16 data
)
190 return (((long)data
+ 128) * 6000) / 256;
194 * For AK09912, same calculation, except the device is more sensitive:
196 * H is in the range of -32752 to 32752. The magnetometer has a range of
197 * +-4912uT. To go from the raw value to uT is:
199 * HuT = H * 4912/32752, or roughly, 3/20, instead of 3/10.
201 static long ak09912_raw_to_gauss(u16 data
)
203 return (((long)data
+ 128) * 1500) / 256;
206 /* Compatible Asahi Kasei Compass parts */
207 enum asahi_compass_chipset
{
216 enum ak_ctrl_reg_addr
{
225 enum ak_ctrl_reg_mask
{
242 enum asahi_compass_chipset type
;
243 long (*raw_to_gauss
)(u16 data
);
245 u8 ctrl_regs
[REGS_END
];
246 u8 ctrl_masks
[MASK_END
];
247 u8 ctrl_modes
[MODE_END
];
251 static const struct ak_def ak_def_array
[] = {
254 .raw_to_gauss
= ak8975_raw_to_gauss
,
263 AK8975_REG_ST1_DRDY_MASK
,
264 AK8975_REG_ST2_HOFL_MASK
,
265 AK8975_REG_ST2_DERR_MASK
,
266 AK8975_REG_CNTL_MODE_MASK
},
268 AK8975_REG_CNTL_MODE_POWER_DOWN
,
269 AK8975_REG_CNTL_MODE_ONCE
,
270 AK8975_REG_CNTL_MODE_SELF_TEST
,
271 AK8975_REG_CNTL_MODE_FUSE_ROM
},
279 .raw_to_gauss
= ak8963_09911_raw_to_gauss
,
288 AK8975_REG_ST1_DRDY_MASK
,
289 AK8975_REG_ST2_HOFL_MASK
,
291 AK8975_REG_CNTL_MODE_MASK
},
293 AK8975_REG_CNTL_MODE_POWER_DOWN
,
294 AK8975_REG_CNTL_MODE_ONCE
,
295 AK8975_REG_CNTL_MODE_SELF_TEST
,
296 AK8975_REG_CNTL_MODE_FUSE_ROM
},
304 .raw_to_gauss
= ak8963_09911_raw_to_gauss
,
313 AK09912_REG_ST1_DRDY_MASK
,
314 AK09912_REG_ST2_HOFL_MASK
,
316 AK09912_REG_CNTL2_MODE_MASK
},
318 AK09912_REG_CNTL_MODE_POWER_DOWN
,
319 AK09912_REG_CNTL_MODE_ONCE
,
320 AK09912_REG_CNTL_MODE_SELF_TEST
,
321 AK09912_REG_CNTL_MODE_FUSE_ROM
},
329 .raw_to_gauss
= ak09912_raw_to_gauss
,
338 AK09912_REG_ST1_DRDY_MASK
,
339 AK09912_REG_ST2_HOFL_MASK
,
341 AK09912_REG_CNTL2_MODE_MASK
},
343 AK09912_REG_CNTL_MODE_POWER_DOWN
,
344 AK09912_REG_CNTL_MODE_ONCE
,
345 AK09912_REG_CNTL_MODE_SELF_TEST
,
346 AK09912_REG_CNTL_MODE_FUSE_ROM
},
354 .raw_to_gauss
= ak09912_raw_to_gauss
,
363 AK09912_REG_ST1_DRDY_MASK
,
364 AK09912_REG_ST2_HOFL_MASK
,
366 AK09912_REG_CNTL2_MODE_MASK
},
368 AK09912_REG_CNTL_MODE_POWER_DOWN
,
369 AK09912_REG_CNTL_MODE_ONCE
,
370 AK09912_REG_CNTL_MODE_SELF_TEST
,
371 AK09912_REG_CNTL_MODE_FUSE_ROM
},
378 /* ak09918 is register compatible with ak09912 this is for avoid
379 * unknown id messages.
382 .raw_to_gauss
= ak09912_raw_to_gauss
,
391 AK09912_REG_ST1_DRDY_MASK
,
392 AK09912_REG_ST2_HOFL_MASK
,
394 AK09912_REG_CNTL2_MODE_MASK
},
396 AK09912_REG_CNTL_MODE_POWER_DOWN
,
397 AK09912_REG_CNTL_MODE_ONCE
,
398 AK09912_REG_CNTL_MODE_SELF_TEST
,
399 AK09912_REG_CNTL_MODE_FUSE_ROM
},
408 * Per-instance context data for the device.
411 struct i2c_client
*client
;
412 const struct ak_def
*def
;
415 long raw_to_gauss
[3];
416 struct gpio_desc
*eoc_gpiod
;
417 struct gpio_desc
*reset_gpiod
;
419 wait_queue_head_t data_ready_queue
;
422 struct iio_mount_matrix orientation
;
423 struct regulator
*vdd
;
424 struct regulator
*vid
;
426 /* Ensure natural alignment of timestamp */
433 /* Enable attached power regulator if any. */
434 static int ak8975_power_on(const struct ak8975_data
*data
)
438 ret
= regulator_enable(data
->vdd
);
440 dev_warn(&data
->client
->dev
,
441 "Failed to enable specified Vdd supply\n");
444 ret
= regulator_enable(data
->vid
);
446 dev_warn(&data
->client
->dev
,
447 "Failed to enable specified Vid supply\n");
448 regulator_disable(data
->vdd
);
452 gpiod_set_value_cansleep(data
->reset_gpiod
, 0);
455 * According to the datasheet the power supply rise time is 200us
456 * and the minimum wait time before mode setting is 100us, in
457 * total 300us. Add some margin and say minimum 500us here.
459 usleep_range(500, 1000);
463 /* Disable attached power regulator if any. */
464 static void ak8975_power_off(const struct ak8975_data
*data
)
466 gpiod_set_value_cansleep(data
->reset_gpiod
, 1);
468 regulator_disable(data
->vid
);
469 regulator_disable(data
->vdd
);
473 * Return 0 if the i2c device is the one we expect.
474 * return a negative error number otherwise
476 static int ak8975_who_i_am(struct i2c_client
*client
,
477 enum asahi_compass_chipset type
)
483 * Signature for each device:
484 * Device | WIA1 | WIA2
485 * AK09918 | DEVICE_ID_| AK09918_DEVICE_ID
486 * AK09916 | DEVICE_ID_| AK09916_DEVICE_ID
487 * AK09912 | DEVICE_ID | AK09912_DEVICE_ID
488 * AK09911 | DEVICE_ID | AK09911_DEVICE_ID
489 * AK8975 | DEVICE_ID | NA
490 * AK8963 | DEVICE_ID | NA
492 ret
= i2c_smbus_read_i2c_block_data_or_emulated(
493 client
, AK09912_REG_WIA1
, 2, wia_val
);
495 dev_err(&client
->dev
, "Error reading WIA\n");
499 if (wia_val
[0] != AK8975_DEVICE_ID
)
507 if (wia_val
[1] == AK09911_DEVICE_ID
)
511 if (wia_val
[1] == AK09912_DEVICE_ID
)
515 if (wia_val
[1] == AK09916_DEVICE_ID
)
519 if (wia_val
[1] == AK09918_DEVICE_ID
)
524 dev_info(&client
->dev
, "Device ID %x is unknown.\n", wia_val
[1]);
526 * Let driver to probe on unknown id for support more register
527 * compatible variants.
533 * Helper function to write to CNTL register.
535 static int ak8975_set_mode(struct ak8975_data
*data
, enum ak_ctrl_mode mode
)
540 regval
= (data
->cntl_cache
& ~data
->def
->ctrl_masks
[CNTL_MODE
]) |
541 data
->def
->ctrl_modes
[mode
];
542 ret
= i2c_smbus_write_byte_data(data
->client
,
543 data
->def
->ctrl_regs
[CNTL
], regval
);
547 data
->cntl_cache
= regval
;
548 /* After mode change wait atleast 100us */
549 usleep_range(100, 500);
555 * Handle data ready irq
557 static irqreturn_t
ak8975_irq_handler(int irq
, void *data
)
559 struct ak8975_data
*ak8975
= data
;
561 set_bit(0, &ak8975
->flags
);
562 wake_up(&ak8975
->data_ready_queue
);
568 * Install data ready interrupt handler
570 static int ak8975_setup_irq(struct ak8975_data
*data
)
572 struct i2c_client
*client
= data
->client
;
576 init_waitqueue_head(&data
->data_ready_queue
);
577 clear_bit(0, &data
->flags
);
581 irq
= gpiod_to_irq(data
->eoc_gpiod
);
583 rc
= devm_request_irq(&client
->dev
, irq
, ak8975_irq_handler
,
584 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
585 dev_name(&client
->dev
), data
);
587 dev_err(&client
->dev
, "irq %d request failed: %d\n", irq
, rc
);
598 * Perform some start-of-day setup, including reading the asa calibration
599 * values and caching them.
601 static int ak8975_setup(struct i2c_client
*client
)
603 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
604 struct ak8975_data
*data
= iio_priv(indio_dev
);
607 /* Write the fused rom access mode. */
608 ret
= ak8975_set_mode(data
, FUSE_ROM
);
610 dev_err(&client
->dev
, "Error in setting fuse access mode\n");
614 /* Get asa data and store in the device data. */
615 ret
= i2c_smbus_read_i2c_block_data_or_emulated(
616 client
, data
->def
->ctrl_regs
[ASA_BASE
],
619 dev_err(&client
->dev
, "Not able to read asa data\n");
623 /* After reading fuse ROM data set power-down mode */
624 ret
= ak8975_set_mode(data
, POWER_DOWN
);
626 dev_err(&client
->dev
, "Error in setting power-down mode\n");
630 if (data
->eoc_gpiod
|| client
->irq
> 0) {
631 ret
= ak8975_setup_irq(data
);
633 dev_err(&client
->dev
,
634 "Error setting data ready interrupt\n");
639 data
->raw_to_gauss
[0] = data
->def
->raw_to_gauss(data
->asa
[0]);
640 data
->raw_to_gauss
[1] = data
->def
->raw_to_gauss(data
->asa
[1]);
641 data
->raw_to_gauss
[2] = data
->def
->raw_to_gauss(data
->asa
[2]);
646 static int wait_conversion_complete_gpio(struct ak8975_data
*data
)
648 struct i2c_client
*client
= data
->client
;
649 u32 timeout_ms
= AK8975_MAX_CONVERSION_TIMEOUT
;
652 /* Wait for the conversion to complete. */
654 msleep(AK8975_CONVERSION_DONE_POLL_TIME
);
655 if (gpiod_get_value(data
->eoc_gpiod
))
657 timeout_ms
-= AK8975_CONVERSION_DONE_POLL_TIME
;
660 dev_err(&client
->dev
, "Conversion timeout happened\n");
664 ret
= i2c_smbus_read_byte_data(client
, data
->def
->ctrl_regs
[ST1
]);
666 dev_err(&client
->dev
, "Error in reading ST1\n");
671 static int wait_conversion_complete_polled(struct ak8975_data
*data
)
673 struct i2c_client
*client
= data
->client
;
675 u32 timeout_ms
= AK8975_MAX_CONVERSION_TIMEOUT
;
678 /* Wait for the conversion to complete. */
680 msleep(AK8975_CONVERSION_DONE_POLL_TIME
);
681 ret
= i2c_smbus_read_byte_data(client
,
682 data
->def
->ctrl_regs
[ST1
]);
684 dev_err(&client
->dev
, "Error in reading ST1\n");
690 timeout_ms
-= AK8975_CONVERSION_DONE_POLL_TIME
;
693 dev_err(&client
->dev
, "Conversion timeout happened\n");
700 /* Returns 0 if the end of conversion interrupt occured or -ETIME otherwise */
701 static int wait_conversion_complete_interrupt(struct ak8975_data
*data
)
705 ret
= wait_event_timeout(data
->data_ready_queue
,
706 test_bit(0, &data
->flags
),
707 AK8975_DATA_READY_TIMEOUT
);
708 clear_bit(0, &data
->flags
);
710 return ret
> 0 ? 0 : -ETIME
;
713 static int ak8975_start_read_axis(struct ak8975_data
*data
,
714 const struct i2c_client
*client
)
716 /* Set up the device for taking a sample. */
717 int ret
= ak8975_set_mode(data
, MODE_ONCE
);
720 dev_err(&client
->dev
, "Error in setting operating mode\n");
724 /* Wait for the conversion to complete. */
726 ret
= wait_conversion_complete_interrupt(data
);
727 else if (data
->eoc_gpiod
)
728 ret
= wait_conversion_complete_gpio(data
);
730 ret
= wait_conversion_complete_polled(data
);
734 /* Return with zero if the data is ready. */
735 return !data
->def
->ctrl_regs
[ST1_DRDY
];
738 /* Retrieve raw flux value for one of the x, y, or z axis. */
739 static int ak8975_read_axis(struct iio_dev
*indio_dev
, int index
, int *val
)
741 struct ak8975_data
*data
= iio_priv(indio_dev
);
742 const struct i2c_client
*client
= data
->client
;
743 const struct ak_def
*def
= data
->def
;
748 pm_runtime_get_sync(&data
->client
->dev
);
750 mutex_lock(&data
->lock
);
752 ret
= ak8975_start_read_axis(data
, client
);
756 ret
= i2c_smbus_read_i2c_block_data_or_emulated(
757 client
, def
->data_regs
[index
],
758 sizeof(rval
), (u8
*)&rval
);
762 /* Read out ST2 for release lock on measurment data. */
763 ret
= i2c_smbus_read_byte_data(client
, data
->def
->ctrl_regs
[ST2
]);
765 dev_err(&client
->dev
, "Error in reading ST2\n");
769 if (ret
& (data
->def
->ctrl_masks
[ST2_DERR
] |
770 data
->def
->ctrl_masks
[ST2_HOFL
])) {
771 dev_err(&client
->dev
, "ST2 status error 0x%x\n", ret
);
776 mutex_unlock(&data
->lock
);
778 pm_runtime_mark_last_busy(&data
->client
->dev
);
779 pm_runtime_put_autosuspend(&data
->client
->dev
);
781 /* Swap bytes and convert to valid range. */
782 buff
= le16_to_cpu(rval
);
783 *val
= clamp_t(s16
, buff
, -def
->range
, def
->range
);
787 mutex_unlock(&data
->lock
);
788 dev_err(&client
->dev
, "Error in reading axis\n");
792 static int ak8975_read_raw(struct iio_dev
*indio_dev
,
793 struct iio_chan_spec
const *chan
,
797 struct ak8975_data
*data
= iio_priv(indio_dev
);
800 case IIO_CHAN_INFO_RAW
:
801 return ak8975_read_axis(indio_dev
, chan
->address
, val
);
802 case IIO_CHAN_INFO_SCALE
:
804 *val2
= data
->raw_to_gauss
[chan
->address
];
805 return IIO_VAL_INT_PLUS_MICRO
;
810 static const struct iio_mount_matrix
*
811 ak8975_get_mount_matrix(const struct iio_dev
*indio_dev
,
812 const struct iio_chan_spec
*chan
)
814 struct ak8975_data
*data
= iio_priv(indio_dev
);
816 return &data
->orientation
;
819 static const struct iio_chan_spec_ext_info ak8975_ext_info
[] = {
820 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR
, ak8975_get_mount_matrix
),
824 #define AK8975_CHANNEL(axis, index) \
828 .channel2 = IIO_MOD_##axis, \
829 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
830 BIT(IIO_CHAN_INFO_SCALE), \
832 .scan_index = index, \
837 .endianness = IIO_CPU \
839 .ext_info = ak8975_ext_info, \
842 static const struct iio_chan_spec ak8975_channels
[] = {
843 AK8975_CHANNEL(X
, 0), AK8975_CHANNEL(Y
, 1), AK8975_CHANNEL(Z
, 2),
844 IIO_CHAN_SOFT_TIMESTAMP(3),
847 static const unsigned long ak8975_scan_masks
[] = { 0x7, 0 };
849 static const struct iio_info ak8975_info
= {
850 .read_raw
= &ak8975_read_raw
,
853 static void ak8975_fill_buffer(struct iio_dev
*indio_dev
)
855 struct ak8975_data
*data
= iio_priv(indio_dev
);
856 const struct i2c_client
*client
= data
->client
;
857 const struct ak_def
*def
= data
->def
;
861 mutex_lock(&data
->lock
);
863 ret
= ak8975_start_read_axis(data
, client
);
868 * For each axis, read the flux value from the appropriate register
869 * (the register is specified in the iio device attributes).
871 ret
= i2c_smbus_read_i2c_block_data_or_emulated(client
,
878 mutex_unlock(&data
->lock
);
880 /* Clamp to valid range. */
881 data
->scan
.channels
[0] = clamp_t(s16
, le16_to_cpu(fval
[0]), -def
->range
, def
->range
);
882 data
->scan
.channels
[1] = clamp_t(s16
, le16_to_cpu(fval
[1]), -def
->range
, def
->range
);
883 data
->scan
.channels
[2] = clamp_t(s16
, le16_to_cpu(fval
[2]), -def
->range
, def
->range
);
885 iio_push_to_buffers_with_timestamp(indio_dev
, &data
->scan
,
886 iio_get_time_ns(indio_dev
));
891 mutex_unlock(&data
->lock
);
892 dev_err(&client
->dev
, "Error in reading axes block\n");
895 static irqreturn_t
ak8975_handle_trigger(int irq
, void *p
)
897 const struct iio_poll_func
*pf
= p
;
898 struct iio_dev
*indio_dev
= pf
->indio_dev
;
900 ak8975_fill_buffer(indio_dev
);
901 iio_trigger_notify_done(indio_dev
->trig
);
905 static int ak8975_probe(struct i2c_client
*client
)
907 const struct i2c_device_id
*id
= i2c_client_get_device_id(client
);
908 struct ak8975_data
*data
;
909 struct iio_dev
*indio_dev
;
910 struct gpio_desc
*eoc_gpiod
;
911 struct gpio_desc
*reset_gpiod
;
913 const char *name
= NULL
;
916 * Grab and set up the supplied GPIO.
917 * We may not have a GPIO based IRQ to scan, that is fine, we will
920 eoc_gpiod
= devm_gpiod_get_optional(&client
->dev
, NULL
, GPIOD_IN
);
921 if (IS_ERR(eoc_gpiod
))
922 return PTR_ERR(eoc_gpiod
);
924 gpiod_set_consumer_name(eoc_gpiod
, "ak_8975");
927 * According to AK09911 datasheet, if reset GPIO is provided then
928 * deassert reset on ak8975_power_on() and assert reset on
929 * ak8975_power_off().
931 reset_gpiod
= devm_gpiod_get_optional(&client
->dev
,
932 "reset", GPIOD_OUT_HIGH
);
933 if (IS_ERR(reset_gpiod
))
934 return PTR_ERR(reset_gpiod
);
936 /* Register with IIO */
937 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
938 if (indio_dev
== NULL
)
941 data
= iio_priv(indio_dev
);
942 i2c_set_clientdata(client
, indio_dev
);
944 data
->client
= client
;
945 data
->eoc_gpiod
= eoc_gpiod
;
946 data
->reset_gpiod
= reset_gpiod
;
949 err
= iio_read_mount_matrix(&client
->dev
, &data
->orientation
);
953 /* id will be NULL when enumerated via ACPI */
954 data
->def
= i2c_get_match_data(client
);
958 /* If enumerated via firmware node, fix the ABI */
959 if (dev_fwnode(&client
->dev
))
960 name
= dev_name(&client
->dev
);
964 /* Fetch the regulators */
965 data
->vdd
= devm_regulator_get(&client
->dev
, "vdd");
966 if (IS_ERR(data
->vdd
))
967 return PTR_ERR(data
->vdd
);
968 data
->vid
= devm_regulator_get(&client
->dev
, "vid");
969 if (IS_ERR(data
->vid
))
970 return PTR_ERR(data
->vid
);
972 err
= ak8975_power_on(data
);
976 err
= ak8975_who_i_am(client
, data
->def
->type
);
978 dev_err(&client
->dev
, "Unexpected device\n");
981 dev_dbg(&client
->dev
, "Asahi compass chip %s\n", name
);
983 /* Perform some basic start-of-day setup of the device. */
984 err
= ak8975_setup(client
);
986 dev_err(&client
->dev
, "%s initialization fails\n", name
);
990 mutex_init(&data
->lock
);
991 indio_dev
->channels
= ak8975_channels
;
992 indio_dev
->num_channels
= ARRAY_SIZE(ak8975_channels
);
993 indio_dev
->info
= &ak8975_info
;
994 indio_dev
->available_scan_masks
= ak8975_scan_masks
;
995 indio_dev
->modes
= INDIO_DIRECT_MODE
;
996 indio_dev
->name
= name
;
998 err
= iio_triggered_buffer_setup(indio_dev
, NULL
, ak8975_handle_trigger
,
1001 dev_err(&client
->dev
, "triggered buffer setup failed\n");
1005 err
= iio_device_register(indio_dev
);
1007 dev_err(&client
->dev
, "device register failed\n");
1008 goto cleanup_buffer
;
1011 /* Enable runtime PM */
1012 pm_runtime_get_noresume(&client
->dev
);
1013 pm_runtime_set_active(&client
->dev
);
1014 pm_runtime_enable(&client
->dev
);
1016 * The device comes online in 500us, so add two orders of magnitude
1017 * of delay before autosuspending: 50 ms.
1019 pm_runtime_set_autosuspend_delay(&client
->dev
, 50);
1020 pm_runtime_use_autosuspend(&client
->dev
);
1021 pm_runtime_put(&client
->dev
);
1026 iio_triggered_buffer_cleanup(indio_dev
);
1028 ak8975_power_off(data
);
1032 static void ak8975_remove(struct i2c_client
*client
)
1034 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
1035 struct ak8975_data
*data
= iio_priv(indio_dev
);
1037 pm_runtime_get_sync(&client
->dev
);
1038 pm_runtime_put_noidle(&client
->dev
);
1039 pm_runtime_disable(&client
->dev
);
1040 iio_device_unregister(indio_dev
);
1041 iio_triggered_buffer_cleanup(indio_dev
);
1042 ak8975_set_mode(data
, POWER_DOWN
);
1043 ak8975_power_off(data
);
1046 static int ak8975_runtime_suspend(struct device
*dev
)
1048 struct i2c_client
*client
= to_i2c_client(dev
);
1049 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
1050 struct ak8975_data
*data
= iio_priv(indio_dev
);
1053 /* Set the device in power down if it wasn't already */
1054 ret
= ak8975_set_mode(data
, POWER_DOWN
);
1056 dev_err(&client
->dev
, "Error in setting power-down mode\n");
1059 /* Next cut the regulators */
1060 ak8975_power_off(data
);
1065 static int ak8975_runtime_resume(struct device
*dev
)
1067 struct i2c_client
*client
= to_i2c_client(dev
);
1068 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
1069 struct ak8975_data
*data
= iio_priv(indio_dev
);
1072 /* Take up the regulators */
1073 ak8975_power_on(data
);
1075 * We come up in powered down mode, the reading routines will
1076 * put us in the mode to read values later.
1078 ret
= ak8975_set_mode(data
, POWER_DOWN
);
1080 dev_err(&client
->dev
, "Error in setting power-down mode\n");
1087 static DEFINE_RUNTIME_DEV_PM_OPS(ak8975_dev_pm_ops
, ak8975_runtime_suspend
,
1088 ak8975_runtime_resume
, NULL
);
1090 static const struct acpi_device_id ak_acpi_match
[] = {
1091 {"AK8963", (kernel_ulong_t
)&ak_def_array
[AK8963
] },
1092 {"AK8975", (kernel_ulong_t
)&ak_def_array
[AK8975
] },
1093 {"AK009911", (kernel_ulong_t
)&ak_def_array
[AK09911
] },
1094 {"AK09911", (kernel_ulong_t
)&ak_def_array
[AK09911
] },
1095 {"AK09912", (kernel_ulong_t
)&ak_def_array
[AK09912
] },
1096 {"AKM9911", (kernel_ulong_t
)&ak_def_array
[AK09911
] },
1097 {"INVN6500", (kernel_ulong_t
)&ak_def_array
[AK8963
] },
1100 MODULE_DEVICE_TABLE(acpi
, ak_acpi_match
);
1102 static const struct i2c_device_id ak8975_id
[] = {
1103 {"AK8963", (kernel_ulong_t
)&ak_def_array
[AK8963
] },
1104 {"ak8963", (kernel_ulong_t
)&ak_def_array
[AK8963
] },
1105 {"ak8975", (kernel_ulong_t
)&ak_def_array
[AK8975
] },
1106 {"ak09911", (kernel_ulong_t
)&ak_def_array
[AK09911
] },
1107 {"ak09912", (kernel_ulong_t
)&ak_def_array
[AK09912
] },
1108 {"ak09916", (kernel_ulong_t
)&ak_def_array
[AK09916
] },
1109 {"ak09918", (kernel_ulong_t
)&ak_def_array
[AK09918
] },
1112 MODULE_DEVICE_TABLE(i2c
, ak8975_id
);
1114 static const struct of_device_id ak8975_of_match
[] = {
1115 { .compatible
= "asahi-kasei,ak8975", .data
= &ak_def_array
[AK8975
] },
1116 { .compatible
= "ak8975", .data
= &ak_def_array
[AK8975
] },
1117 { .compatible
= "asahi-kasei,ak8963", .data
= &ak_def_array
[AK8963
] },
1118 { .compatible
= "ak8963", .data
= &ak_def_array
[AK8963
] },
1119 { .compatible
= "asahi-kasei,ak09911", .data
= &ak_def_array
[AK09911
] },
1120 { .compatible
= "ak09911", .data
= &ak_def_array
[AK09911
] },
1121 { .compatible
= "asahi-kasei,ak09912", .data
= &ak_def_array
[AK09912
] },
1122 { .compatible
= "ak09912", .data
= &ak_def_array
[AK09912
] },
1123 { .compatible
= "asahi-kasei,ak09916", .data
= &ak_def_array
[AK09916
] },
1124 { .compatible
= "asahi-kasei,ak09918", .data
= &ak_def_array
[AK09918
] },
1127 MODULE_DEVICE_TABLE(of
, ak8975_of_match
);
1129 static struct i2c_driver ak8975_driver
= {
1132 .pm
= pm_ptr(&ak8975_dev_pm_ops
),
1133 .of_match_table
= ak8975_of_match
,
1134 .acpi_match_table
= ak_acpi_match
,
1136 .probe
= ak8975_probe
,
1137 .remove
= ak8975_remove
,
1138 .id_table
= ak8975_id
,
1140 module_i2c_driver(ak8975_driver
);
1142 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
1143 MODULE_DESCRIPTION("AK8975 magnetometer driver");
1144 MODULE_LICENSE("GPL");