2 * A sensor driver for the magnetometer AK8975.
4 * Magnetic compass sensor driver for monitoring magnetic flux information.
6 * Copyright (c) 2010, NVIDIA Corporation.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/i2c.h>
27 #include <linux/interrupt.h>
28 #include <linux/err.h>
29 #include <linux/mutex.h>
30 #include <linux/delay.h>
31 #include <linux/bitops.h>
32 #include <linux/gpio.h>
33 #include <linux/of_gpio.h>
34 #include <linux/acpi.h>
35 #include <linux/regulator/consumer.h>
36 #include <linux/pm_runtime.h>
38 #include <linux/iio/iio.h>
39 #include <linux/iio/sysfs.h>
40 #include <linux/iio/buffer.h>
41 #include <linux/iio/trigger.h>
42 #include <linux/iio/trigger_consumer.h>
43 #include <linux/iio/triggered_buffer.h>
45 #include <linux/iio/magnetometer/ak8975.h>
48 * Register definitions, as well as various shifts and masks to get at the
49 * individual fields of the registers.
51 #define AK8975_REG_WIA 0x00
52 #define AK8975_DEVICE_ID 0x48
54 #define AK8975_REG_INFO 0x01
56 #define AK8975_REG_ST1 0x02
57 #define AK8975_REG_ST1_DRDY_SHIFT 0
58 #define AK8975_REG_ST1_DRDY_MASK (1 << AK8975_REG_ST1_DRDY_SHIFT)
60 #define AK8975_REG_HXL 0x03
61 #define AK8975_REG_HXH 0x04
62 #define AK8975_REG_HYL 0x05
63 #define AK8975_REG_HYH 0x06
64 #define AK8975_REG_HZL 0x07
65 #define AK8975_REG_HZH 0x08
66 #define AK8975_REG_ST2 0x09
67 #define AK8975_REG_ST2_DERR_SHIFT 2
68 #define AK8975_REG_ST2_DERR_MASK (1 << AK8975_REG_ST2_DERR_SHIFT)
70 #define AK8975_REG_ST2_HOFL_SHIFT 3
71 #define AK8975_REG_ST2_HOFL_MASK (1 << AK8975_REG_ST2_HOFL_SHIFT)
73 #define AK8975_REG_CNTL 0x0A
74 #define AK8975_REG_CNTL_MODE_SHIFT 0
75 #define AK8975_REG_CNTL_MODE_MASK (0xF << AK8975_REG_CNTL_MODE_SHIFT)
76 #define AK8975_REG_CNTL_MODE_POWER_DOWN 0x00
77 #define AK8975_REG_CNTL_MODE_ONCE 0x01
78 #define AK8975_REG_CNTL_MODE_SELF_TEST 0x08
79 #define AK8975_REG_CNTL_MODE_FUSE_ROM 0x0F
81 #define AK8975_REG_RSVC 0x0B
82 #define AK8975_REG_ASTC 0x0C
83 #define AK8975_REG_TS1 0x0D
84 #define AK8975_REG_TS2 0x0E
85 #define AK8975_REG_I2CDIS 0x0F
86 #define AK8975_REG_ASAX 0x10
87 #define AK8975_REG_ASAY 0x11
88 #define AK8975_REG_ASAZ 0x12
90 #define AK8975_MAX_REGS AK8975_REG_ASAZ
93 * AK09912 Register definitions
95 #define AK09912_REG_WIA1 0x00
96 #define AK09912_REG_WIA2 0x01
97 #define AK09912_DEVICE_ID 0x04
98 #define AK09911_DEVICE_ID 0x05
100 #define AK09911_REG_INFO1 0x02
101 #define AK09911_REG_INFO2 0x03
103 #define AK09912_REG_ST1 0x10
105 #define AK09912_REG_ST1_DRDY_SHIFT 0
106 #define AK09912_REG_ST1_DRDY_MASK (1 << AK09912_REG_ST1_DRDY_SHIFT)
108 #define AK09912_REG_HXL 0x11
109 #define AK09912_REG_HXH 0x12
110 #define AK09912_REG_HYL 0x13
111 #define AK09912_REG_HYH 0x14
112 #define AK09912_REG_HZL 0x15
113 #define AK09912_REG_HZH 0x16
114 #define AK09912_REG_TMPS 0x17
116 #define AK09912_REG_ST2 0x18
117 #define AK09912_REG_ST2_HOFL_SHIFT 3
118 #define AK09912_REG_ST2_HOFL_MASK (1 << AK09912_REG_ST2_HOFL_SHIFT)
120 #define AK09912_REG_CNTL1 0x30
122 #define AK09912_REG_CNTL2 0x31
123 #define AK09912_REG_CNTL_MODE_POWER_DOWN 0x00
124 #define AK09912_REG_CNTL_MODE_ONCE 0x01
125 #define AK09912_REG_CNTL_MODE_SELF_TEST 0x10
126 #define AK09912_REG_CNTL_MODE_FUSE_ROM 0x1F
127 #define AK09912_REG_CNTL2_MODE_SHIFT 0
128 #define AK09912_REG_CNTL2_MODE_MASK (0x1F << AK09912_REG_CNTL2_MODE_SHIFT)
130 #define AK09912_REG_CNTL3 0x32
132 #define AK09912_REG_TS1 0x33
133 #define AK09912_REG_TS2 0x34
134 #define AK09912_REG_TS3 0x35
135 #define AK09912_REG_I2CDIS 0x36
136 #define AK09912_REG_TS4 0x37
138 #define AK09912_REG_ASAX 0x60
139 #define AK09912_REG_ASAY 0x61
140 #define AK09912_REG_ASAZ 0x62
142 #define AK09912_MAX_REGS AK09912_REG_ASAZ
145 * Miscellaneous values.
147 #define AK8975_MAX_CONVERSION_TIMEOUT 500
148 #define AK8975_CONVERSION_DONE_POLL_TIME 10
149 #define AK8975_DATA_READY_TIMEOUT ((100*HZ)/1000)
152 * Precalculate scale factor (in Gauss units) for each axis and
153 * store in the device data.
155 * This scale factor is axis-dependent, and is derived from 3 calibration
156 * factors ASA(x), ASA(y), and ASA(z).
158 * These ASA values are read from the sensor device at start of day, and
159 * cached in the device context struct.
161 * Adjusting the flux value with the sensitivity adjustment value should be
162 * done via the following formula:
164 * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
165 * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
166 * is the resultant adjusted value.
168 * We reduce the formula to:
170 * Hadj = H * (ASA + 128) / 256
172 * H is in the range of -4096 to 4095. The magnetometer has a range of
173 * +-1229uT. To go from the raw value to uT is:
175 * HuT = H * 1229/4096, or roughly, 3/10.
177 * Since 1uT = 0.01 gauss, our final scale factor becomes:
179 * Hadj = H * ((ASA + 128) / 256) * 3/10 * 1/100
180 * Hadj = H * ((ASA + 128) * 0.003) / 256
182 * Since ASA doesn't change, we cache the resultant scale factor into the
183 * device context in ak8975_setup().
185 * Given we use IIO_VAL_INT_PLUS_MICRO bit when displaying the scale, we
186 * multiply the stored scale value by 1e6.
188 static long ak8975_raw_to_gauss(u16 data)
190 return (((long)data + 128) * 3000) / 256;
194 * For AK8963 and AK09911, same calculation, but the device is less sensitive:
196 * H is in the range of +-8190. The magnetometer has a range of
197 * +-4912uT. To go from the raw value to uT is:
199 * HuT = H * 4912/8190, or roughly, 6/10, instead of 3/10.
202 static long ak8963_09911_raw_to_gauss(u16 data)
204 return (((long)data + 128) * 6000) / 256;
208 * For AK09912, same calculation, except the device is more sensitive:
210 * H is in the range of -32752 to 32752. The magnetometer has a range of
211 * +-4912uT. To go from the raw value to uT is:
213 * HuT = H * 4912/32752, or roughly, 3/20, instead of 3/10.
215 static long ak09912_raw_to_gauss(u16 data)
217 return (((long)data + 128) * 1500) / 256;
220 /* Compatible Asahi Kasei Compass parts */
221 enum asahi_compass_chipset {
229 enum ak_ctrl_reg_addr {
238 enum ak_ctrl_reg_mask {
255 enum asahi_compass_chipset type;
256 long (*raw_to_gauss)(u16 data);
258 u8 ctrl_regs[REGS_END];
259 u8 ctrl_masks[MASK_END];
260 u8 ctrl_modes[MODE_END];
264 static const struct ak_def ak_def_array[AK_MAX_TYPE] = {
267 .raw_to_gauss = ak8975_raw_to_gauss,
276 AK8975_REG_ST1_DRDY_MASK,
277 AK8975_REG_ST2_HOFL_MASK,
278 AK8975_REG_ST2_DERR_MASK,
279 AK8975_REG_CNTL_MODE_MASK},
281 AK8975_REG_CNTL_MODE_POWER_DOWN,
282 AK8975_REG_CNTL_MODE_ONCE,
283 AK8975_REG_CNTL_MODE_SELF_TEST,
284 AK8975_REG_CNTL_MODE_FUSE_ROM},
292 .raw_to_gauss = ak8963_09911_raw_to_gauss,
301 AK8975_REG_ST1_DRDY_MASK,
302 AK8975_REG_ST2_HOFL_MASK,
304 AK8975_REG_CNTL_MODE_MASK},
306 AK8975_REG_CNTL_MODE_POWER_DOWN,
307 AK8975_REG_CNTL_MODE_ONCE,
308 AK8975_REG_CNTL_MODE_SELF_TEST,
309 AK8975_REG_CNTL_MODE_FUSE_ROM},
317 .raw_to_gauss = ak8963_09911_raw_to_gauss,
326 AK09912_REG_ST1_DRDY_MASK,
327 AK09912_REG_ST2_HOFL_MASK,
329 AK09912_REG_CNTL2_MODE_MASK},
331 AK09912_REG_CNTL_MODE_POWER_DOWN,
332 AK09912_REG_CNTL_MODE_ONCE,
333 AK09912_REG_CNTL_MODE_SELF_TEST,
334 AK09912_REG_CNTL_MODE_FUSE_ROM},
342 .raw_to_gauss = ak09912_raw_to_gauss,
351 AK09912_REG_ST1_DRDY_MASK,
352 AK09912_REG_ST2_HOFL_MASK,
354 AK09912_REG_CNTL2_MODE_MASK},
356 AK09912_REG_CNTL_MODE_POWER_DOWN,
357 AK09912_REG_CNTL_MODE_ONCE,
358 AK09912_REG_CNTL_MODE_SELF_TEST,
359 AK09912_REG_CNTL_MODE_FUSE_ROM},
368 * Per-instance context data for the device.
371 struct i2c_client *client;
372 const struct ak_def *def;
375 long raw_to_gauss[3];
378 wait_queue_head_t data_ready_queue;
381 struct iio_mount_matrix orientation;
382 struct regulator *vdd;
383 struct regulator *vid;
385 /* Ensure natural alignment of timestamp */
392 /* Enable attached power regulator if any. */
393 static int ak8975_power_on(const struct ak8975_data *data)
397 ret = regulator_enable(data->vdd);
399 dev_warn(&data->client->dev,
400 "Failed to enable specified Vdd supply\n");
403 ret = regulator_enable(data->vid);
405 dev_warn(&data->client->dev,
406 "Failed to enable specified Vid supply\n");
410 * According to the datasheet the power supply rise time i 200us
411 * and the minimum wait time before mode setting is 100us, in
412 * total 300 us. Add some margin and say minimum 500us here.
414 usleep_range(500, 1000);
418 /* Disable attached power regulator if any. */
419 static void ak8975_power_off(const struct ak8975_data *data)
421 regulator_disable(data->vid);
422 regulator_disable(data->vdd);
426 * Return 0 if the i2c device is the one we expect.
427 * return a negative error number otherwise
429 static int ak8975_who_i_am(struct i2c_client *client,
430 enum asahi_compass_chipset type)
436 * Signature for each device:
437 * Device | WIA1 | WIA2
438 * AK09912 | DEVICE_ID | AK09912_DEVICE_ID
439 * AK09911 | DEVICE_ID | AK09911_DEVICE_ID
440 * AK8975 | DEVICE_ID | NA
441 * AK8963 | DEVICE_ID | NA
443 ret = i2c_smbus_read_i2c_block_data_or_emulated(
444 client, AK09912_REG_WIA1, 2, wia_val);
446 dev_err(&client->dev, "Error reading WIA\n");
450 if (wia_val[0] != AK8975_DEVICE_ID)
458 if (wia_val[1] == AK09911_DEVICE_ID)
462 if (wia_val[1] == AK09912_DEVICE_ID)
466 dev_err(&client->dev, "Type %d unknown\n", type);
472 * Helper function to write to CNTL register.
474 static int ak8975_set_mode(struct ak8975_data *data, enum ak_ctrl_mode mode)
479 regval = (data->cntl_cache & ~data->def->ctrl_masks[CNTL_MODE]) |
480 data->def->ctrl_modes[mode];
481 ret = i2c_smbus_write_byte_data(data->client,
482 data->def->ctrl_regs[CNTL], regval);
486 data->cntl_cache = regval;
487 /* After mode change wait atleast 100us */
488 usleep_range(100, 500);
494 * Handle data ready irq
496 static irqreturn_t ak8975_irq_handler(int irq, void *data)
498 struct ak8975_data *ak8975 = data;
500 set_bit(0, &ak8975->flags);
501 wake_up(&ak8975->data_ready_queue);
507 * Install data ready interrupt handler
509 static int ak8975_setup_irq(struct ak8975_data *data)
511 struct i2c_client *client = data->client;
515 init_waitqueue_head(&data->data_ready_queue);
516 clear_bit(0, &data->flags);
520 irq = gpio_to_irq(data->eoc_gpio);
522 rc = devm_request_irq(&client->dev, irq, ak8975_irq_handler,
523 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
524 dev_name(&client->dev), data);
526 dev_err(&client->dev,
527 "irq %d request failed, (gpio %d): %d\n",
528 irq, data->eoc_gpio, rc);
539 * Perform some start-of-day setup, including reading the asa calibration
540 * values and caching them.
542 static int ak8975_setup(struct i2c_client *client)
544 struct iio_dev *indio_dev = i2c_get_clientdata(client);
545 struct ak8975_data *data = iio_priv(indio_dev);
548 /* Write the fused rom access mode. */
549 ret = ak8975_set_mode(data, FUSE_ROM);
551 dev_err(&client->dev, "Error in setting fuse access mode\n");
555 /* Get asa data and store in the device data. */
556 ret = i2c_smbus_read_i2c_block_data_or_emulated(
557 client, data->def->ctrl_regs[ASA_BASE],
560 dev_err(&client->dev, "Not able to read asa data\n");
564 /* After reading fuse ROM data set power-down mode */
565 ret = ak8975_set_mode(data, POWER_DOWN);
567 dev_err(&client->dev, "Error in setting power-down mode\n");
571 if (data->eoc_gpio > 0 || client->irq > 0) {
572 ret = ak8975_setup_irq(data);
574 dev_err(&client->dev,
575 "Error setting data ready interrupt\n");
580 data->raw_to_gauss[0] = data->def->raw_to_gauss(data->asa[0]);
581 data->raw_to_gauss[1] = data->def->raw_to_gauss(data->asa[1]);
582 data->raw_to_gauss[2] = data->def->raw_to_gauss(data->asa[2]);
587 static int wait_conversion_complete_gpio(struct ak8975_data *data)
589 struct i2c_client *client = data->client;
590 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
593 /* Wait for the conversion to complete. */
595 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
596 if (gpio_get_value(data->eoc_gpio))
598 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
601 dev_err(&client->dev, "Conversion timeout happened\n");
605 ret = i2c_smbus_read_byte_data(client, data->def->ctrl_regs[ST1]);
607 dev_err(&client->dev, "Error in reading ST1\n");
612 static int wait_conversion_complete_polled(struct ak8975_data *data)
614 struct i2c_client *client = data->client;
616 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
619 /* Wait for the conversion to complete. */
621 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
622 ret = i2c_smbus_read_byte_data(client,
623 data->def->ctrl_regs[ST1]);
625 dev_err(&client->dev, "Error in reading ST1\n");
631 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
634 dev_err(&client->dev, "Conversion timeout happened\n");
641 /* Returns 0 if the end of conversion interrupt occured or -ETIME otherwise */
642 static int wait_conversion_complete_interrupt(struct ak8975_data *data)
646 ret = wait_event_timeout(data->data_ready_queue,
647 test_bit(0, &data->flags),
648 AK8975_DATA_READY_TIMEOUT);
649 clear_bit(0, &data->flags);
651 return ret > 0 ? 0 : -ETIME;
654 static int ak8975_start_read_axis(struct ak8975_data *data,
655 const struct i2c_client *client)
657 /* Set up the device for taking a sample. */
658 int ret = ak8975_set_mode(data, MODE_ONCE);
661 dev_err(&client->dev, "Error in setting operating mode\n");
665 /* Wait for the conversion to complete. */
667 ret = wait_conversion_complete_interrupt(data);
668 else if (gpio_is_valid(data->eoc_gpio))
669 ret = wait_conversion_complete_gpio(data);
671 ret = wait_conversion_complete_polled(data);
675 /* This will be executed only for non-interrupt based waiting case */
676 if (ret & data->def->ctrl_masks[ST1_DRDY]) {
677 ret = i2c_smbus_read_byte_data(client,
678 data->def->ctrl_regs[ST2]);
680 dev_err(&client->dev, "Error in reading ST2\n");
683 if (ret & (data->def->ctrl_masks[ST2_DERR] |
684 data->def->ctrl_masks[ST2_HOFL])) {
685 dev_err(&client->dev, "ST2 status error 0x%x\n", ret);
693 /* Retrieve raw flux value for one of the x, y, or z axis. */
694 static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
696 struct ak8975_data *data = iio_priv(indio_dev);
697 const struct i2c_client *client = data->client;
698 const struct ak_def *def = data->def;
703 pm_runtime_get_sync(&data->client->dev);
705 mutex_lock(&data->lock);
707 ret = ak8975_start_read_axis(data, client);
711 ret = i2c_smbus_read_i2c_block_data_or_emulated(
712 client, def->data_regs[index],
713 sizeof(rval), (u8*)&rval);
717 mutex_unlock(&data->lock);
719 pm_runtime_mark_last_busy(&data->client->dev);
720 pm_runtime_put_autosuspend(&data->client->dev);
722 /* Swap bytes and convert to valid range. */
723 buff = le16_to_cpu(rval);
724 *val = clamp_t(s16, buff, -def->range, def->range);
728 mutex_unlock(&data->lock);
729 dev_err(&client->dev, "Error in reading axis\n");
733 static int ak8975_read_raw(struct iio_dev *indio_dev,
734 struct iio_chan_spec const *chan,
738 struct ak8975_data *data = iio_priv(indio_dev);
741 case IIO_CHAN_INFO_RAW:
742 return ak8975_read_axis(indio_dev, chan->address, val);
743 case IIO_CHAN_INFO_SCALE:
745 *val2 = data->raw_to_gauss[chan->address];
746 return IIO_VAL_INT_PLUS_MICRO;
751 static const struct iio_mount_matrix *
752 ak8975_get_mount_matrix(const struct iio_dev *indio_dev,
753 const struct iio_chan_spec *chan)
755 return &((struct ak8975_data *)iio_priv(indio_dev))->orientation;
758 static const struct iio_chan_spec_ext_info ak8975_ext_info[] = {
759 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, ak8975_get_mount_matrix),
763 #define AK8975_CHANNEL(axis, index) \
767 .channel2 = IIO_MOD_##axis, \
768 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
769 BIT(IIO_CHAN_INFO_SCALE), \
771 .scan_index = index, \
776 .endianness = IIO_CPU \
778 .ext_info = ak8975_ext_info, \
781 static const struct iio_chan_spec ak8975_channels[] = {
782 AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2),
783 IIO_CHAN_SOFT_TIMESTAMP(3),
786 static const unsigned long ak8975_scan_masks[] = { 0x7, 0 };
788 static const struct iio_info ak8975_info = {
789 .read_raw = &ak8975_read_raw,
790 .driver_module = THIS_MODULE,
793 static const struct acpi_device_id ak_acpi_match[] = {
796 {"INVN6500", AK8963},
797 {"AK09911", AK09911},
798 {"AK09912", AK09912},
801 MODULE_DEVICE_TABLE(acpi, ak_acpi_match);
803 static const char *ak8975_match_acpi_device(struct device *dev,
804 enum asahi_compass_chipset *chipset)
806 const struct acpi_device_id *id;
808 id = acpi_match_device(dev->driver->acpi_match_table, dev);
811 *chipset = (int)id->driver_data;
813 return dev_name(dev);
816 static void ak8975_fill_buffer(struct iio_dev *indio_dev)
818 struct ak8975_data *data = iio_priv(indio_dev);
819 const struct i2c_client *client = data->client;
820 const struct ak_def *def = data->def;
824 mutex_lock(&data->lock);
826 ret = ak8975_start_read_axis(data, client);
831 * For each axis, read the flux value from the appropriate register
832 * (the register is specified in the iio device attributes).
834 ret = i2c_smbus_read_i2c_block_data_or_emulated(client,
841 mutex_unlock(&data->lock);
843 /* Clamp to valid range. */
844 data->scan.channels[0] = clamp_t(s16, le16_to_cpu(fval[0]), -def->range, def->range);
845 data->scan.channels[1] = clamp_t(s16, le16_to_cpu(fval[1]), -def->range, def->range);
846 data->scan.channels[2] = clamp_t(s16, le16_to_cpu(fval[2]), -def->range, def->range);
848 iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
849 iio_get_time_ns(indio_dev));
854 mutex_unlock(&data->lock);
855 dev_err(&client->dev, "Error in reading axes block\n");
858 static irqreturn_t ak8975_handle_trigger(int irq, void *p)
860 const struct iio_poll_func *pf = p;
861 struct iio_dev *indio_dev = pf->indio_dev;
863 ak8975_fill_buffer(indio_dev);
864 iio_trigger_notify_done(indio_dev->trig);
868 static int ak8975_probe(struct i2c_client *client,
869 const struct i2c_device_id *id)
871 struct ak8975_data *data;
872 struct iio_dev *indio_dev;
875 const char *name = NULL;
876 enum asahi_compass_chipset chipset = AK_MAX_TYPE;
877 const struct ak8975_platform_data *pdata =
878 dev_get_platdata(&client->dev);
880 /* Grab and set up the supplied GPIO. */
882 eoc_gpio = pdata->eoc_gpio;
883 else if (client->dev.of_node)
884 eoc_gpio = of_get_gpio(client->dev.of_node, 0);
888 if (eoc_gpio == -EPROBE_DEFER)
889 return -EPROBE_DEFER;
891 /* We may not have a GPIO based IRQ to scan, that is fine, we will
893 if (gpio_is_valid(eoc_gpio)) {
894 err = devm_gpio_request_one(&client->dev, eoc_gpio,
895 GPIOF_IN, "ak_8975");
897 dev_err(&client->dev,
898 "failed to request GPIO %d, error %d\n",
904 /* Register with IIO */
905 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
906 if (indio_dev == NULL)
909 data = iio_priv(indio_dev);
910 i2c_set_clientdata(client, indio_dev);
912 data->client = client;
913 data->eoc_gpio = eoc_gpio;
917 err = of_iio_read_mount_matrix(&client->dev,
923 data->orientation = pdata->orientation;
925 /* id will be NULL when enumerated via ACPI */
927 chipset = (enum asahi_compass_chipset)(id->driver_data);
929 } else if (ACPI_HANDLE(&client->dev)) {
930 name = ak8975_match_acpi_device(&client->dev, &chipset);
936 if (chipset >= AK_MAX_TYPE) {
937 dev_err(&client->dev, "AKM device type unsupported: %d\n",
942 data->def = &ak_def_array[chipset];
944 /* Fetch the regulators */
945 data->vdd = devm_regulator_get(&client->dev, "vdd");
946 if (IS_ERR(data->vdd))
947 return PTR_ERR(data->vdd);
948 data->vid = devm_regulator_get(&client->dev, "vid");
949 if (IS_ERR(data->vid))
950 return PTR_ERR(data->vid);
952 err = ak8975_power_on(data);
956 err = ak8975_who_i_am(client, data->def->type);
958 dev_err(&client->dev, "Unexpected device\n");
961 dev_dbg(&client->dev, "Asahi compass chip %s\n", name);
963 /* Perform some basic start-of-day setup of the device. */
964 err = ak8975_setup(client);
966 dev_err(&client->dev, "%s initialization fails\n", name);
970 mutex_init(&data->lock);
971 indio_dev->dev.parent = &client->dev;
972 indio_dev->channels = ak8975_channels;
973 indio_dev->num_channels = ARRAY_SIZE(ak8975_channels);
974 indio_dev->info = &ak8975_info;
975 indio_dev->available_scan_masks = ak8975_scan_masks;
976 indio_dev->modes = INDIO_DIRECT_MODE;
977 indio_dev->name = name;
979 err = iio_triggered_buffer_setup(indio_dev, NULL, ak8975_handle_trigger,
982 dev_err(&client->dev, "triggered buffer setup failed\n");
986 err = iio_device_register(indio_dev);
988 dev_err(&client->dev, "device register failed\n");
992 /* Enable runtime PM */
993 pm_runtime_get_noresume(&client->dev);
994 pm_runtime_set_active(&client->dev);
995 pm_runtime_enable(&client->dev);
997 * The device comes online in 500us, so add two orders of magnitude
998 * of delay before autosuspending: 50 ms.
1000 pm_runtime_set_autosuspend_delay(&client->dev, 50);
1001 pm_runtime_use_autosuspend(&client->dev);
1002 pm_runtime_put(&client->dev);
1007 iio_triggered_buffer_cleanup(indio_dev);
1009 ak8975_power_off(data);
1013 static int ak8975_remove(struct i2c_client *client)
1015 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1016 struct ak8975_data *data = iio_priv(indio_dev);
1018 pm_runtime_get_sync(&client->dev);
1019 pm_runtime_put_noidle(&client->dev);
1020 pm_runtime_disable(&client->dev);
1021 iio_device_unregister(indio_dev);
1022 iio_triggered_buffer_cleanup(indio_dev);
1023 ak8975_set_mode(data, POWER_DOWN);
1024 ak8975_power_off(data);
1030 static int ak8975_runtime_suspend(struct device *dev)
1032 struct i2c_client *client = to_i2c_client(dev);
1033 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1034 struct ak8975_data *data = iio_priv(indio_dev);
1037 /* Set the device in power down if it wasn't already */
1038 ret = ak8975_set_mode(data, POWER_DOWN);
1040 dev_err(&client->dev, "Error in setting power-down mode\n");
1043 /* Next cut the regulators */
1044 ak8975_power_off(data);
1049 static int ak8975_runtime_resume(struct device *dev)
1051 struct i2c_client *client = to_i2c_client(dev);
1052 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1053 struct ak8975_data *data = iio_priv(indio_dev);
1056 /* Take up the regulators */
1057 ak8975_power_on(data);
1059 * We come up in powered down mode, the reading routines will
1060 * put us in the mode to read values later.
1062 ret = ak8975_set_mode(data, POWER_DOWN);
1064 dev_err(&client->dev, "Error in setting power-down mode\n");
1070 #endif /* CONFIG_PM */
1072 static const struct dev_pm_ops ak8975_dev_pm_ops = {
1073 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1074 pm_runtime_force_resume)
1075 SET_RUNTIME_PM_OPS(ak8975_runtime_suspend,
1076 ak8975_runtime_resume, NULL)
1079 static const struct i2c_device_id ak8975_id[] = {
1083 {"ak09911", AK09911},
1084 {"ak09912", AK09912},
1088 MODULE_DEVICE_TABLE(i2c, ak8975_id);
1090 static const struct of_device_id ak8975_of_match[] = {
1091 { .compatible = "asahi-kasei,ak8975", },
1092 { .compatible = "ak8975", },
1093 { .compatible = "asahi-kasei,ak8963", },
1094 { .compatible = "ak8963", },
1095 { .compatible = "asahi-kasei,ak09911", },
1096 { .compatible = "ak09911", },
1097 { .compatible = "asahi-kasei,ak09912", },
1098 { .compatible = "ak09912", },
1101 MODULE_DEVICE_TABLE(of, ak8975_of_match);
1103 static struct i2c_driver ak8975_driver = {
1106 .pm = &ak8975_dev_pm_ops,
1107 .of_match_table = of_match_ptr(ak8975_of_match),
1108 .acpi_match_table = ACPI_PTR(ak_acpi_match),
1110 .probe = ak8975_probe,
1111 .remove = ak8975_remove,
1112 .id_table = ak8975_id,
1114 module_i2c_driver(ak8975_driver);
1116 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
1117 MODULE_DESCRIPTION("AK8975 magnetometer driver");
1118 MODULE_LICENSE("GPL");