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;
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;
371 /* Enable attached power regulator if any. */
372 static int ak8975_power_on(const struct ak8975_data *data)
376 ret = regulator_enable(data->vdd);
378 dev_warn(&data->client->dev,
379 "Failed to enable specified Vdd supply\n");
382 ret = regulator_enable(data->vid);
384 dev_warn(&data->client->dev,
385 "Failed to enable specified Vid supply\n");
389 gpiod_set_value_cansleep(data->reset_gpiod, 0);
392 * According to the datasheet the power supply rise time is 200us
393 * and the minimum wait time before mode setting is 100us, in
394 * total 300us. Add some margin and say minimum 500us here.
396 usleep_range(500, 1000);
400 /* Disable attached power regulator if any. */
401 static void ak8975_power_off(const struct ak8975_data *data)
403 gpiod_set_value_cansleep(data->reset_gpiod, 1);
405 regulator_disable(data->vid);
406 regulator_disable(data->vdd);
410 * Return 0 if the i2c device is the one we expect.
411 * return a negative error number otherwise
413 static int ak8975_who_i_am(struct i2c_client *client,
414 enum asahi_compass_chipset type)
420 * Signature for each device:
421 * Device | WIA1 | WIA2
422 * AK09912 | DEVICE_ID | AK09912_DEVICE_ID
423 * AK09911 | DEVICE_ID | AK09911_DEVICE_ID
424 * AK8975 | DEVICE_ID | NA
425 * AK8963 | DEVICE_ID | NA
427 ret = i2c_smbus_read_i2c_block_data_or_emulated(
428 client, AK09912_REG_WIA1, 2, wia_val);
430 dev_err(&client->dev, "Error reading WIA\n");
434 if (wia_val[0] != AK8975_DEVICE_ID)
442 if (wia_val[1] == AK09911_DEVICE_ID)
446 if (wia_val[1] == AK09912_DEVICE_ID)
450 dev_err(&client->dev, "Type %d unknown\n", type);
456 * Helper function to write to CNTL register.
458 static int ak8975_set_mode(struct ak8975_data *data, enum ak_ctrl_mode mode)
463 regval = (data->cntl_cache & ~data->def->ctrl_masks[CNTL_MODE]) |
464 data->def->ctrl_modes[mode];
465 ret = i2c_smbus_write_byte_data(data->client,
466 data->def->ctrl_regs[CNTL], regval);
470 data->cntl_cache = regval;
471 /* After mode change wait atleast 100us */
472 usleep_range(100, 500);
478 * Handle data ready irq
480 static irqreturn_t ak8975_irq_handler(int irq, void *data)
482 struct ak8975_data *ak8975 = data;
484 set_bit(0, &ak8975->flags);
485 wake_up(&ak8975->data_ready_queue);
491 * Install data ready interrupt handler
493 static int ak8975_setup_irq(struct ak8975_data *data)
495 struct i2c_client *client = data->client;
499 init_waitqueue_head(&data->data_ready_queue);
500 clear_bit(0, &data->flags);
504 irq = gpiod_to_irq(data->eoc_gpiod);
506 rc = devm_request_irq(&client->dev, irq, ak8975_irq_handler,
507 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
508 dev_name(&client->dev), data);
510 dev_err(&client->dev, "irq %d request failed: %d\n", irq, rc);
521 * Perform some start-of-day setup, including reading the asa calibration
522 * values and caching them.
524 static int ak8975_setup(struct i2c_client *client)
526 struct iio_dev *indio_dev = i2c_get_clientdata(client);
527 struct ak8975_data *data = iio_priv(indio_dev);
530 /* Write the fused rom access mode. */
531 ret = ak8975_set_mode(data, FUSE_ROM);
533 dev_err(&client->dev, "Error in setting fuse access mode\n");
537 /* Get asa data and store in the device data. */
538 ret = i2c_smbus_read_i2c_block_data_or_emulated(
539 client, data->def->ctrl_regs[ASA_BASE],
542 dev_err(&client->dev, "Not able to read asa data\n");
546 /* After reading fuse ROM data set power-down mode */
547 ret = ak8975_set_mode(data, POWER_DOWN);
549 dev_err(&client->dev, "Error in setting power-down mode\n");
553 if (data->eoc_gpiod || client->irq > 0) {
554 ret = ak8975_setup_irq(data);
556 dev_err(&client->dev,
557 "Error setting data ready interrupt\n");
562 data->raw_to_gauss[0] = data->def->raw_to_gauss(data->asa[0]);
563 data->raw_to_gauss[1] = data->def->raw_to_gauss(data->asa[1]);
564 data->raw_to_gauss[2] = data->def->raw_to_gauss(data->asa[2]);
569 static int wait_conversion_complete_gpio(struct ak8975_data *data)
571 struct i2c_client *client = data->client;
572 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
575 /* Wait for the conversion to complete. */
577 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
578 if (gpiod_get_value(data->eoc_gpiod))
580 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
583 dev_err(&client->dev, "Conversion timeout happened\n");
587 ret = i2c_smbus_read_byte_data(client, data->def->ctrl_regs[ST1]);
589 dev_err(&client->dev, "Error in reading ST1\n");
594 static int wait_conversion_complete_polled(struct ak8975_data *data)
596 struct i2c_client *client = data->client;
598 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
601 /* Wait for the conversion to complete. */
603 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
604 ret = i2c_smbus_read_byte_data(client,
605 data->def->ctrl_regs[ST1]);
607 dev_err(&client->dev, "Error in reading ST1\n");
613 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
616 dev_err(&client->dev, "Conversion timeout happened\n");
623 /* Returns 0 if the end of conversion interrupt occured or -ETIME otherwise */
624 static int wait_conversion_complete_interrupt(struct ak8975_data *data)
628 ret = wait_event_timeout(data->data_ready_queue,
629 test_bit(0, &data->flags),
630 AK8975_DATA_READY_TIMEOUT);
631 clear_bit(0, &data->flags);
633 return ret > 0 ? 0 : -ETIME;
636 static int ak8975_start_read_axis(struct ak8975_data *data,
637 const struct i2c_client *client)
639 /* Set up the device for taking a sample. */
640 int ret = ak8975_set_mode(data, MODE_ONCE);
643 dev_err(&client->dev, "Error in setting operating mode\n");
647 /* Wait for the conversion to complete. */
649 ret = wait_conversion_complete_interrupt(data);
650 else if (data->eoc_gpiod)
651 ret = wait_conversion_complete_gpio(data);
653 ret = wait_conversion_complete_polled(data);
657 /* This will be executed only for non-interrupt based waiting case */
658 if (ret & data->def->ctrl_masks[ST1_DRDY]) {
659 ret = i2c_smbus_read_byte_data(client,
660 data->def->ctrl_regs[ST2]);
662 dev_err(&client->dev, "Error in reading ST2\n");
665 if (ret & (data->def->ctrl_masks[ST2_DERR] |
666 data->def->ctrl_masks[ST2_HOFL])) {
667 dev_err(&client->dev, "ST2 status error 0x%x\n", ret);
675 /* Retrieve raw flux value for one of the x, y, or z axis. */
676 static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
678 struct ak8975_data *data = iio_priv(indio_dev);
679 const struct i2c_client *client = data->client;
680 const struct ak_def *def = data->def;
685 pm_runtime_get_sync(&data->client->dev);
687 mutex_lock(&data->lock);
689 ret = ak8975_start_read_axis(data, client);
693 ret = i2c_smbus_read_i2c_block_data_or_emulated(
694 client, def->data_regs[index],
695 sizeof(rval), (u8*)&rval);
699 mutex_unlock(&data->lock);
701 pm_runtime_mark_last_busy(&data->client->dev);
702 pm_runtime_put_autosuspend(&data->client->dev);
704 /* Swap bytes and convert to valid range. */
705 buff = le16_to_cpu(rval);
706 *val = clamp_t(s16, buff, -def->range, def->range);
710 mutex_unlock(&data->lock);
711 dev_err(&client->dev, "Error in reading axis\n");
715 static int ak8975_read_raw(struct iio_dev *indio_dev,
716 struct iio_chan_spec const *chan,
720 struct ak8975_data *data = iio_priv(indio_dev);
723 case IIO_CHAN_INFO_RAW:
724 return ak8975_read_axis(indio_dev, chan->address, val);
725 case IIO_CHAN_INFO_SCALE:
727 *val2 = data->raw_to_gauss[chan->address];
728 return IIO_VAL_INT_PLUS_MICRO;
733 static const struct iio_mount_matrix *
734 ak8975_get_mount_matrix(const struct iio_dev *indio_dev,
735 const struct iio_chan_spec *chan)
737 struct ak8975_data *data = iio_priv(indio_dev);
739 return &data->orientation;
742 static const struct iio_chan_spec_ext_info ak8975_ext_info[] = {
743 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, ak8975_get_mount_matrix),
747 #define AK8975_CHANNEL(axis, index) \
751 .channel2 = IIO_MOD_##axis, \
752 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
753 BIT(IIO_CHAN_INFO_SCALE), \
755 .scan_index = index, \
760 .endianness = IIO_CPU \
762 .ext_info = ak8975_ext_info, \
765 static const struct iio_chan_spec ak8975_channels[] = {
766 AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2),
767 IIO_CHAN_SOFT_TIMESTAMP(3),
770 static const unsigned long ak8975_scan_masks[] = { 0x7, 0 };
772 static const struct iio_info ak8975_info = {
773 .read_raw = &ak8975_read_raw,
777 static const struct acpi_device_id ak_acpi_match[] = {
780 {"INVN6500", AK8963},
781 {"AK009911", AK09911},
782 {"AK09911", AK09911},
783 {"AKM9911", AK09911},
784 {"AK09912", AK09912},
787 MODULE_DEVICE_TABLE(acpi, ak_acpi_match);
790 static void ak8975_fill_buffer(struct iio_dev *indio_dev)
792 struct ak8975_data *data = iio_priv(indio_dev);
793 const struct i2c_client *client = data->client;
794 const struct ak_def *def = data->def;
796 s16 buff[8]; /* 3 x 16 bits axis values + 1 aligned 64 bits timestamp */
799 mutex_lock(&data->lock);
801 ret = ak8975_start_read_axis(data, client);
806 * For each axis, read the flux value from the appropriate register
807 * (the register is specified in the iio device attributes).
809 ret = i2c_smbus_read_i2c_block_data_or_emulated(client,
816 mutex_unlock(&data->lock);
818 /* Clamp to valid range. */
819 buff[0] = clamp_t(s16, le16_to_cpu(fval[0]), -def->range, def->range);
820 buff[1] = clamp_t(s16, le16_to_cpu(fval[1]), -def->range, def->range);
821 buff[2] = clamp_t(s16, le16_to_cpu(fval[2]), -def->range, def->range);
823 iio_push_to_buffers_with_timestamp(indio_dev, buff,
824 iio_get_time_ns(indio_dev));
828 mutex_unlock(&data->lock);
829 dev_err(&client->dev, "Error in reading axes block\n");
832 static irqreturn_t ak8975_handle_trigger(int irq, void *p)
834 const struct iio_poll_func *pf = p;
835 struct iio_dev *indio_dev = pf->indio_dev;
837 ak8975_fill_buffer(indio_dev);
838 iio_trigger_notify_done(indio_dev->trig);
842 static int ak8975_probe(struct i2c_client *client,
843 const struct i2c_device_id *id)
845 struct ak8975_data *data;
846 struct iio_dev *indio_dev;
847 struct gpio_desc *eoc_gpiod;
848 struct gpio_desc *reset_gpiod;
852 enum asahi_compass_chipset chipset;
853 const char *name = NULL;
856 * Grab and set up the supplied GPIO.
857 * We may not have a GPIO based IRQ to scan, that is fine, we will
860 eoc_gpiod = devm_gpiod_get_optional(&client->dev, NULL, GPIOD_IN);
861 if (IS_ERR(eoc_gpiod))
862 return PTR_ERR(eoc_gpiod);
864 gpiod_set_consumer_name(eoc_gpiod, "ak_8975");
867 * According to AK09911 datasheet, if reset GPIO is provided then
868 * deassert reset on ak8975_power_on() and assert reset on
869 * ak8975_power_off().
871 reset_gpiod = devm_gpiod_get_optional(&client->dev,
872 "reset", GPIOD_OUT_HIGH);
873 if (IS_ERR(reset_gpiod))
874 return PTR_ERR(reset_gpiod);
876 /* Register with IIO */
877 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
878 if (indio_dev == NULL)
881 data = iio_priv(indio_dev);
882 i2c_set_clientdata(client, indio_dev);
884 data->client = client;
885 data->eoc_gpiod = eoc_gpiod;
886 data->reset_gpiod = reset_gpiod;
889 err = iio_read_mount_matrix(&client->dev, "mount-matrix", &data->orientation);
893 /* id will be NULL when enumerated via ACPI */
894 match = device_get_match_data(&client->dev);
896 chipset = (enum asahi_compass_chipset)(match);
897 name = dev_name(&client->dev);
899 chipset = (enum asahi_compass_chipset)(id->driver_data);
904 for (i = 0; i < ARRAY_SIZE(ak_def_array); i++)
905 if (ak_def_array[i].type == chipset)
908 if (i == ARRAY_SIZE(ak_def_array)) {
909 dev_err(&client->dev, "AKM device type unsupported: %d\n",
914 data->def = &ak_def_array[i];
916 /* Fetch the regulators */
917 data->vdd = devm_regulator_get(&client->dev, "vdd");
918 if (IS_ERR(data->vdd))
919 return PTR_ERR(data->vdd);
920 data->vid = devm_regulator_get(&client->dev, "vid");
921 if (IS_ERR(data->vid))
922 return PTR_ERR(data->vid);
924 err = ak8975_power_on(data);
928 err = ak8975_who_i_am(client, data->def->type);
930 dev_err(&client->dev, "Unexpected device\n");
933 dev_dbg(&client->dev, "Asahi compass chip %s\n", name);
935 /* Perform some basic start-of-day setup of the device. */
936 err = ak8975_setup(client);
938 dev_err(&client->dev, "%s initialization fails\n", name);
942 mutex_init(&data->lock);
943 indio_dev->channels = ak8975_channels;
944 indio_dev->num_channels = ARRAY_SIZE(ak8975_channels);
945 indio_dev->info = &ak8975_info;
946 indio_dev->available_scan_masks = ak8975_scan_masks;
947 indio_dev->modes = INDIO_DIRECT_MODE;
948 indio_dev->name = name;
950 err = iio_triggered_buffer_setup(indio_dev, NULL, ak8975_handle_trigger,
953 dev_err(&client->dev, "triggered buffer setup failed\n");
957 err = iio_device_register(indio_dev);
959 dev_err(&client->dev, "device register failed\n");
963 /* Enable runtime PM */
964 pm_runtime_get_noresume(&client->dev);
965 pm_runtime_set_active(&client->dev);
966 pm_runtime_enable(&client->dev);
968 * The device comes online in 500us, so add two orders of magnitude
969 * of delay before autosuspending: 50 ms.
971 pm_runtime_set_autosuspend_delay(&client->dev, 50);
972 pm_runtime_use_autosuspend(&client->dev);
973 pm_runtime_put(&client->dev);
978 iio_triggered_buffer_cleanup(indio_dev);
980 ak8975_power_off(data);
984 static int ak8975_remove(struct i2c_client *client)
986 struct iio_dev *indio_dev = i2c_get_clientdata(client);
987 struct ak8975_data *data = iio_priv(indio_dev);
989 pm_runtime_get_sync(&client->dev);
990 pm_runtime_put_noidle(&client->dev);
991 pm_runtime_disable(&client->dev);
992 iio_device_unregister(indio_dev);
993 iio_triggered_buffer_cleanup(indio_dev);
994 ak8975_set_mode(data, POWER_DOWN);
995 ak8975_power_off(data);
1001 static int ak8975_runtime_suspend(struct device *dev)
1003 struct i2c_client *client = to_i2c_client(dev);
1004 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1005 struct ak8975_data *data = iio_priv(indio_dev);
1008 /* Set the device in power down if it wasn't already */
1009 ret = ak8975_set_mode(data, POWER_DOWN);
1011 dev_err(&client->dev, "Error in setting power-down mode\n");
1014 /* Next cut the regulators */
1015 ak8975_power_off(data);
1020 static int ak8975_runtime_resume(struct device *dev)
1022 struct i2c_client *client = to_i2c_client(dev);
1023 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1024 struct ak8975_data *data = iio_priv(indio_dev);
1027 /* Take up the regulators */
1028 ak8975_power_on(data);
1030 * We come up in powered down mode, the reading routines will
1031 * put us in the mode to read values later.
1033 ret = ak8975_set_mode(data, POWER_DOWN);
1035 dev_err(&client->dev, "Error in setting power-down mode\n");
1041 #endif /* CONFIG_PM */
1043 static const struct dev_pm_ops ak8975_dev_pm_ops = {
1044 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1045 pm_runtime_force_resume)
1046 SET_RUNTIME_PM_OPS(ak8975_runtime_suspend,
1047 ak8975_runtime_resume, NULL)
1050 static const struct i2c_device_id ak8975_id[] = {
1054 {"ak09911", AK09911},
1055 {"ak09912", AK09912},
1059 MODULE_DEVICE_TABLE(i2c, ak8975_id);
1061 static const struct of_device_id ak8975_of_match[] = {
1062 { .compatible = "asahi-kasei,ak8975", },
1063 { .compatible = "ak8975", },
1064 { .compatible = "asahi-kasei,ak8963", },
1065 { .compatible = "ak8963", },
1066 { .compatible = "asahi-kasei,ak09911", },
1067 { .compatible = "ak09911", },
1068 { .compatible = "asahi-kasei,ak09912", },
1069 { .compatible = "ak09912", },
1072 MODULE_DEVICE_TABLE(of, ak8975_of_match);
1074 static struct i2c_driver ak8975_driver = {
1077 .pm = &ak8975_dev_pm_ops,
1078 .of_match_table = of_match_ptr(ak8975_of_match),
1079 .acpi_match_table = ACPI_PTR(ak_acpi_match),
1081 .probe = ak8975_probe,
1082 .remove = ak8975_remove,
1083 .id_table = ak8975_id,
1085 module_i2c_driver(ak8975_driver);
1087 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
1088 MODULE_DESCRIPTION("AK8975 magnetometer driver");
1089 MODULE_LICENSE("GPL");