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>
37 #include <linux/iio/iio.h>
38 #include <linux/iio/sysfs.h>
39 #include <linux/iio/buffer.h>
40 #include <linux/iio/trigger.h>
41 #include <linux/iio/trigger_consumer.h>
42 #include <linux/iio/triggered_buffer.h>
44 #include <linux/iio/magnetometer/ak8975.h>
47 * Register definitions, as well as various shifts and masks to get at the
48 * individual fields of the registers.
50 #define AK8975_REG_WIA 0x00
51 #define AK8975_DEVICE_ID 0x48
53 #define AK8975_REG_INFO 0x01
55 #define AK8975_REG_ST1 0x02
56 #define AK8975_REG_ST1_DRDY_SHIFT 0
57 #define AK8975_REG_ST1_DRDY_MASK (1 << AK8975_REG_ST1_DRDY_SHIFT)
59 #define AK8975_REG_HXL 0x03
60 #define AK8975_REG_HXH 0x04
61 #define AK8975_REG_HYL 0x05
62 #define AK8975_REG_HYH 0x06
63 #define AK8975_REG_HZL 0x07
64 #define AK8975_REG_HZH 0x08
65 #define AK8975_REG_ST2 0x09
66 #define AK8975_REG_ST2_DERR_SHIFT 2
67 #define AK8975_REG_ST2_DERR_MASK (1 << AK8975_REG_ST2_DERR_SHIFT)
69 #define AK8975_REG_ST2_HOFL_SHIFT 3
70 #define AK8975_REG_ST2_HOFL_MASK (1 << AK8975_REG_ST2_HOFL_SHIFT)
72 #define AK8975_REG_CNTL 0x0A
73 #define AK8975_REG_CNTL_MODE_SHIFT 0
74 #define AK8975_REG_CNTL_MODE_MASK (0xF << AK8975_REG_CNTL_MODE_SHIFT)
75 #define AK8975_REG_CNTL_MODE_POWER_DOWN 0x00
76 #define AK8975_REG_CNTL_MODE_ONCE 0x01
77 #define AK8975_REG_CNTL_MODE_SELF_TEST 0x08
78 #define AK8975_REG_CNTL_MODE_FUSE_ROM 0x0F
80 #define AK8975_REG_RSVC 0x0B
81 #define AK8975_REG_ASTC 0x0C
82 #define AK8975_REG_TS1 0x0D
83 #define AK8975_REG_TS2 0x0E
84 #define AK8975_REG_I2CDIS 0x0F
85 #define AK8975_REG_ASAX 0x10
86 #define AK8975_REG_ASAY 0x11
87 #define AK8975_REG_ASAZ 0x12
89 #define AK8975_MAX_REGS AK8975_REG_ASAZ
92 * AK09912 Register definitions
94 #define AK09912_REG_WIA1 0x00
95 #define AK09912_REG_WIA2 0x01
96 #define AK09912_DEVICE_ID 0x04
97 #define AK09911_DEVICE_ID 0x05
99 #define AK09911_REG_INFO1 0x02
100 #define AK09911_REG_INFO2 0x03
102 #define AK09912_REG_ST1 0x10
104 #define AK09912_REG_ST1_DRDY_SHIFT 0
105 #define AK09912_REG_ST1_DRDY_MASK (1 << AK09912_REG_ST1_DRDY_SHIFT)
107 #define AK09912_REG_HXL 0x11
108 #define AK09912_REG_HXH 0x12
109 #define AK09912_REG_HYL 0x13
110 #define AK09912_REG_HYH 0x14
111 #define AK09912_REG_HZL 0x15
112 #define AK09912_REG_HZH 0x16
113 #define AK09912_REG_TMPS 0x17
115 #define AK09912_REG_ST2 0x18
116 #define AK09912_REG_ST2_HOFL_SHIFT 3
117 #define AK09912_REG_ST2_HOFL_MASK (1 << AK09912_REG_ST2_HOFL_SHIFT)
119 #define AK09912_REG_CNTL1 0x30
121 #define AK09912_REG_CNTL2 0x31
122 #define AK09912_REG_CNTL_MODE_POWER_DOWN 0x00
123 #define AK09912_REG_CNTL_MODE_ONCE 0x01
124 #define AK09912_REG_CNTL_MODE_SELF_TEST 0x10
125 #define AK09912_REG_CNTL_MODE_FUSE_ROM 0x1F
126 #define AK09912_REG_CNTL2_MODE_SHIFT 0
127 #define AK09912_REG_CNTL2_MODE_MASK (0x1F << AK09912_REG_CNTL2_MODE_SHIFT)
129 #define AK09912_REG_CNTL3 0x32
131 #define AK09912_REG_TS1 0x33
132 #define AK09912_REG_TS2 0x34
133 #define AK09912_REG_TS3 0x35
134 #define AK09912_REG_I2CDIS 0x36
135 #define AK09912_REG_TS4 0x37
137 #define AK09912_REG_ASAX 0x60
138 #define AK09912_REG_ASAY 0x61
139 #define AK09912_REG_ASAZ 0x62
141 #define AK09912_MAX_REGS AK09912_REG_ASAZ
144 * Miscellaneous values.
146 #define AK8975_MAX_CONVERSION_TIMEOUT 500
147 #define AK8975_CONVERSION_DONE_POLL_TIME 10
148 #define AK8975_DATA_READY_TIMEOUT ((100*HZ)/1000)
151 * Precalculate scale factor (in Gauss units) for each axis and
152 * store in the device data.
154 * This scale factor is axis-dependent, and is derived from 3 calibration
155 * factors ASA(x), ASA(y), and ASA(z).
157 * These ASA values are read from the sensor device at start of day, and
158 * cached in the device context struct.
160 * Adjusting the flux value with the sensitivity adjustment value should be
161 * done via the following formula:
163 * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
164 * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
165 * is the resultant adjusted value.
167 * We reduce the formula to:
169 * Hadj = H * (ASA + 128) / 256
171 * H is in the range of -4096 to 4095. The magnetometer has a range of
172 * +-1229uT. To go from the raw value to uT is:
174 * HuT = H * 1229/4096, or roughly, 3/10.
176 * Since 1uT = 0.01 gauss, our final scale factor becomes:
178 * Hadj = H * ((ASA + 128) / 256) * 3/10 * 1/100
179 * Hadj = H * ((ASA + 128) * 0.003) / 256
181 * Since ASA doesn't change, we cache the resultant scale factor into the
182 * device context in ak8975_setup().
184 * Given we use IIO_VAL_INT_PLUS_MICRO bit when displaying the scale, we
185 * multiply the stored scale value by 1e6.
187 static long ak8975_raw_to_gauss(u16 data
)
189 return (((long)data
+ 128) * 3000) / 256;
193 * For AK8963 and AK09911, same calculation, but the device is less sensitive:
195 * H is in the range of +-8190. The magnetometer has a range of
196 * +-4912uT. To go from the raw value to uT is:
198 * HuT = H * 4912/8190, or roughly, 6/10, instead of 3/10.
201 static long ak8963_09911_raw_to_gauss(u16 data
)
203 return (((long)data
+ 128) * 6000) / 256;
207 * For AK09912, same calculation, except the device is more sensitive:
209 * H is in the range of -32752 to 32752. The magnetometer has a range of
210 * +-4912uT. To go from the raw value to uT is:
212 * HuT = H * 4912/32752, or roughly, 3/20, instead of 3/10.
214 static long ak09912_raw_to_gauss(u16 data
)
216 return (((long)data
+ 128) * 1500) / 256;
219 /* Compatible Asahi Kasei Compass parts */
220 enum asahi_compass_chipset
{
228 enum ak_ctrl_reg_addr
{
237 enum ak_ctrl_reg_mask
{
254 enum asahi_compass_chipset type
;
255 long (*raw_to_gauss
)(u16 data
);
257 u8 ctrl_regs
[REGS_END
];
258 u8 ctrl_masks
[MASK_END
];
259 u8 ctrl_modes
[MODE_END
];
263 static const struct ak_def ak_def_array
[AK_MAX_TYPE
] = {
266 .raw_to_gauss
= ak8975_raw_to_gauss
,
275 AK8975_REG_ST1_DRDY_MASK
,
276 AK8975_REG_ST2_HOFL_MASK
,
277 AK8975_REG_ST2_DERR_MASK
,
278 AK8975_REG_CNTL_MODE_MASK
},
280 AK8975_REG_CNTL_MODE_POWER_DOWN
,
281 AK8975_REG_CNTL_MODE_ONCE
,
282 AK8975_REG_CNTL_MODE_SELF_TEST
,
283 AK8975_REG_CNTL_MODE_FUSE_ROM
},
291 .raw_to_gauss
= ak8963_09911_raw_to_gauss
,
300 AK8975_REG_ST1_DRDY_MASK
,
301 AK8975_REG_ST2_HOFL_MASK
,
303 AK8975_REG_CNTL_MODE_MASK
},
305 AK8975_REG_CNTL_MODE_POWER_DOWN
,
306 AK8975_REG_CNTL_MODE_ONCE
,
307 AK8975_REG_CNTL_MODE_SELF_TEST
,
308 AK8975_REG_CNTL_MODE_FUSE_ROM
},
316 .raw_to_gauss
= ak8963_09911_raw_to_gauss
,
325 AK09912_REG_ST1_DRDY_MASK
,
326 AK09912_REG_ST2_HOFL_MASK
,
328 AK09912_REG_CNTL2_MODE_MASK
},
330 AK09912_REG_CNTL_MODE_POWER_DOWN
,
331 AK09912_REG_CNTL_MODE_ONCE
,
332 AK09912_REG_CNTL_MODE_SELF_TEST
,
333 AK09912_REG_CNTL_MODE_FUSE_ROM
},
341 .raw_to_gauss
= ak09912_raw_to_gauss
,
350 AK09912_REG_ST1_DRDY_MASK
,
351 AK09912_REG_ST2_HOFL_MASK
,
353 AK09912_REG_CNTL2_MODE_MASK
},
355 AK09912_REG_CNTL_MODE_POWER_DOWN
,
356 AK09912_REG_CNTL_MODE_ONCE
,
357 AK09912_REG_CNTL_MODE_SELF_TEST
,
358 AK09912_REG_CNTL_MODE_FUSE_ROM
},
367 * Per-instance context data for the device.
370 struct i2c_client
*client
;
371 const struct ak_def
*def
;
374 long raw_to_gauss
[3];
377 wait_queue_head_t data_ready_queue
;
380 struct iio_mount_matrix orientation
;
381 struct regulator
*vdd
;
384 /* Enable attached power regulator if any. */
385 static int ak8975_power_on(struct i2c_client
*client
)
387 const struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
388 struct ak8975_data
*data
= iio_priv(indio_dev
);
391 data
->vdd
= devm_regulator_get(&client
->dev
, "vdd");
392 if (IS_ERR_OR_NULL(data
->vdd
)) {
393 ret
= PTR_ERR(data
->vdd
);
397 ret
= regulator_enable(data
->vdd
);
401 dev_err(&client
->dev
, "failed to enable Vdd supply: %d\n", ret
);
405 /* Disable attached power regulator if any. */
406 static void ak8975_power_off(const struct i2c_client
*client
)
408 const struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
409 const struct ak8975_data
*data
= iio_priv(indio_dev
);
411 if (!IS_ERR_OR_NULL(data
->vdd
))
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(client
, AK09912_REG_WIA1
,
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
= gpio_to_irq(data
->eoc_gpio
);
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
,
517 "irq %d request failed, (gpio %d): %d\n",
518 irq
, data
->eoc_gpio
, rc
);
529 * Perform some start-of-day setup, including reading the asa calibration
530 * values and caching them.
532 static int ak8975_setup(struct i2c_client
*client
)
534 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
535 struct ak8975_data
*data
= iio_priv(indio_dev
);
538 /* Write the fused rom access mode. */
539 ret
= ak8975_set_mode(data
, FUSE_ROM
);
541 dev_err(&client
->dev
, "Error in setting fuse access mode\n");
545 /* Get asa data and store in the device data. */
546 ret
= i2c_smbus_read_i2c_block_data(client
,
547 data
->def
->ctrl_regs
[ASA_BASE
],
550 dev_err(&client
->dev
, "Not able to read asa data\n");
554 /* After reading fuse ROM data set power-down mode */
555 ret
= ak8975_set_mode(data
, POWER_DOWN
);
557 dev_err(&client
->dev
, "Error in setting power-down mode\n");
561 if (data
->eoc_gpio
> 0 || client
->irq
> 0) {
562 ret
= ak8975_setup_irq(data
);
564 dev_err(&client
->dev
,
565 "Error setting data ready interrupt\n");
570 data
->raw_to_gauss
[0] = data
->def
->raw_to_gauss(data
->asa
[0]);
571 data
->raw_to_gauss
[1] = data
->def
->raw_to_gauss(data
->asa
[1]);
572 data
->raw_to_gauss
[2] = data
->def
->raw_to_gauss(data
->asa
[2]);
577 static int wait_conversion_complete_gpio(struct ak8975_data
*data
)
579 struct i2c_client
*client
= data
->client
;
580 u32 timeout_ms
= AK8975_MAX_CONVERSION_TIMEOUT
;
583 /* Wait for the conversion to complete. */
585 msleep(AK8975_CONVERSION_DONE_POLL_TIME
);
586 if (gpio_get_value(data
->eoc_gpio
))
588 timeout_ms
-= AK8975_CONVERSION_DONE_POLL_TIME
;
591 dev_err(&client
->dev
, "Conversion timeout happened\n");
595 ret
= i2c_smbus_read_byte_data(client
, data
->def
->ctrl_regs
[ST1
]);
597 dev_err(&client
->dev
, "Error in reading ST1\n");
602 static int wait_conversion_complete_polled(struct ak8975_data
*data
)
604 struct i2c_client
*client
= data
->client
;
606 u32 timeout_ms
= AK8975_MAX_CONVERSION_TIMEOUT
;
609 /* Wait for the conversion to complete. */
611 msleep(AK8975_CONVERSION_DONE_POLL_TIME
);
612 ret
= i2c_smbus_read_byte_data(client
,
613 data
->def
->ctrl_regs
[ST1
]);
615 dev_err(&client
->dev
, "Error in reading ST1\n");
621 timeout_ms
-= AK8975_CONVERSION_DONE_POLL_TIME
;
624 dev_err(&client
->dev
, "Conversion timeout happened\n");
631 /* Returns 0 if the end of conversion interrupt occured or -ETIME otherwise */
632 static int wait_conversion_complete_interrupt(struct ak8975_data
*data
)
636 ret
= wait_event_timeout(data
->data_ready_queue
,
637 test_bit(0, &data
->flags
),
638 AK8975_DATA_READY_TIMEOUT
);
639 clear_bit(0, &data
->flags
);
641 return ret
> 0 ? 0 : -ETIME
;
644 static int ak8975_start_read_axis(struct ak8975_data
*data
,
645 const struct i2c_client
*client
)
647 /* Set up the device for taking a sample. */
648 int ret
= ak8975_set_mode(data
, MODE_ONCE
);
651 dev_err(&client
->dev
, "Error in setting operating mode\n");
655 /* Wait for the conversion to complete. */
657 ret
= wait_conversion_complete_interrupt(data
);
658 else if (gpio_is_valid(data
->eoc_gpio
))
659 ret
= wait_conversion_complete_gpio(data
);
661 ret
= wait_conversion_complete_polled(data
);
665 /* This will be executed only for non-interrupt based waiting case */
666 if (ret
& data
->def
->ctrl_masks
[ST1_DRDY
]) {
667 ret
= i2c_smbus_read_byte_data(client
,
668 data
->def
->ctrl_regs
[ST2
]);
670 dev_err(&client
->dev
, "Error in reading ST2\n");
673 if (ret
& (data
->def
->ctrl_masks
[ST2_DERR
] |
674 data
->def
->ctrl_masks
[ST2_HOFL
])) {
675 dev_err(&client
->dev
, "ST2 status error 0x%x\n", ret
);
683 /* Retrieve raw flux value for one of the x, y, or z axis. */
684 static int ak8975_read_axis(struct iio_dev
*indio_dev
, int index
, int *val
)
686 struct ak8975_data
*data
= iio_priv(indio_dev
);
687 const struct i2c_client
*client
= data
->client
;
688 const struct ak_def
*def
= data
->def
;
691 mutex_lock(&data
->lock
);
693 ret
= ak8975_start_read_axis(data
, client
);
697 ret
= i2c_smbus_read_word_data(client
, def
->data_regs
[index
]);
701 mutex_unlock(&data
->lock
);
703 /* Clamp to valid range. */
704 *val
= clamp_t(s16
, ret
, -def
->range
, def
->range
);
708 mutex_unlock(&data
->lock
);
709 dev_err(&client
->dev
, "Error in reading axis\n");
713 static int ak8975_read_raw(struct iio_dev
*indio_dev
,
714 struct iio_chan_spec
const *chan
,
718 struct ak8975_data
*data
= iio_priv(indio_dev
);
721 case IIO_CHAN_INFO_RAW
:
722 return ak8975_read_axis(indio_dev
, chan
->address
, val
);
723 case IIO_CHAN_INFO_SCALE
:
725 *val2
= data
->raw_to_gauss
[chan
->address
];
726 return IIO_VAL_INT_PLUS_MICRO
;
731 static const struct iio_mount_matrix
*
732 ak8975_get_mount_matrix(const struct iio_dev
*indio_dev
,
733 const struct iio_chan_spec
*chan
)
735 return &((struct ak8975_data
*)iio_priv(indio_dev
))->orientation
;
738 static const struct iio_chan_spec_ext_info ak8975_ext_info
[] = {
739 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR
, ak8975_get_mount_matrix
),
743 #define AK8975_CHANNEL(axis, index) \
747 .channel2 = IIO_MOD_##axis, \
748 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
749 BIT(IIO_CHAN_INFO_SCALE), \
751 .scan_index = index, \
756 .endianness = IIO_CPU \
758 .ext_info = ak8975_ext_info, \
761 static const struct iio_chan_spec ak8975_channels
[] = {
762 AK8975_CHANNEL(X
, 0), AK8975_CHANNEL(Y
, 1), AK8975_CHANNEL(Z
, 2),
763 IIO_CHAN_SOFT_TIMESTAMP(3),
766 static const unsigned long ak8975_scan_masks
[] = { 0x7, 0 };
768 static const struct iio_info ak8975_info
= {
769 .read_raw
= &ak8975_read_raw
,
770 .driver_module
= THIS_MODULE
,
773 static const struct acpi_device_id ak_acpi_match
[] = {
776 {"INVN6500", AK8963
},
777 {"AK09911", AK09911
},
778 {"AK09912", AK09912
},
781 MODULE_DEVICE_TABLE(acpi
, ak_acpi_match
);
783 static const char *ak8975_match_acpi_device(struct device
*dev
,
784 enum asahi_compass_chipset
*chipset
)
786 const struct acpi_device_id
*id
;
788 id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
791 *chipset
= (int)id
->driver_data
;
793 return dev_name(dev
);
796 static void ak8975_fill_buffer(struct iio_dev
*indio_dev
)
798 struct ak8975_data
*data
= iio_priv(indio_dev
);
799 const struct i2c_client
*client
= data
->client
;
800 const struct ak_def
*def
= data
->def
;
802 s16 buff
[8]; /* 3 x 16 bits axis values + 1 aligned 64 bits timestamp */
804 mutex_lock(&data
->lock
);
806 ret
= ak8975_start_read_axis(data
, client
);
811 * For each axis, read the flux value from the appropriate register
812 * (the register is specified in the iio device attributes).
814 ret
= i2c_smbus_read_i2c_block_data_or_emulated(client
,
821 mutex_unlock(&data
->lock
);
823 /* Clamp to valid range. */
824 buff
[0] = clamp_t(s16
, le16_to_cpu(buff
[0]), -def
->range
, def
->range
);
825 buff
[1] = clamp_t(s16
, le16_to_cpu(buff
[1]), -def
->range
, def
->range
);
826 buff
[2] = clamp_t(s16
, le16_to_cpu(buff
[2]), -def
->range
, def
->range
);
828 iio_push_to_buffers_with_timestamp(indio_dev
, buff
, iio_get_time_ns());
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
;
853 const char *name
= NULL
;
854 enum asahi_compass_chipset chipset
= AK_MAX_TYPE
;
855 const struct ak8975_platform_data
*pdata
=
856 dev_get_platdata(&client
->dev
);
858 /* Grab and set up the supplied GPIO. */
860 eoc_gpio
= pdata
->eoc_gpio
;
861 else if (client
->dev
.of_node
)
862 eoc_gpio
= of_get_gpio(client
->dev
.of_node
, 0);
866 if (eoc_gpio
== -EPROBE_DEFER
)
867 return -EPROBE_DEFER
;
869 /* We may not have a GPIO based IRQ to scan, that is fine, we will
871 if (gpio_is_valid(eoc_gpio
)) {
872 err
= devm_gpio_request_one(&client
->dev
, eoc_gpio
,
873 GPIOF_IN
, "ak_8975");
875 dev_err(&client
->dev
,
876 "failed to request GPIO %d, error %d\n",
882 /* Register with IIO */
883 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
884 if (indio_dev
== NULL
)
887 data
= iio_priv(indio_dev
);
888 i2c_set_clientdata(client
, indio_dev
);
890 data
->client
= client
;
891 data
->eoc_gpio
= eoc_gpio
;
895 err
= of_iio_read_mount_matrix(&client
->dev
,
901 data
->orientation
= pdata
->orientation
;
903 /* id will be NULL when enumerated via ACPI */
905 chipset
= (enum asahi_compass_chipset
)(id
->driver_data
);
907 } else if (ACPI_HANDLE(&client
->dev
)) {
908 name
= ak8975_match_acpi_device(&client
->dev
, &chipset
);
914 if (chipset
>= AK_MAX_TYPE
) {
915 dev_err(&client
->dev
, "AKM device type unsupported: %d\n",
920 data
->def
= &ak_def_array
[chipset
];
922 err
= ak8975_power_on(client
);
926 err
= ak8975_who_i_am(client
, data
->def
->type
);
928 dev_err(&client
->dev
, "Unexpected device\n");
931 dev_dbg(&client
->dev
, "Asahi compass chip %s\n", name
);
933 /* Perform some basic start-of-day setup of the device. */
934 err
= ak8975_setup(client
);
936 dev_err(&client
->dev
, "%s initialization fails\n", name
);
940 mutex_init(&data
->lock
);
941 indio_dev
->dev
.parent
= &client
->dev
;
942 indio_dev
->channels
= ak8975_channels
;
943 indio_dev
->num_channels
= ARRAY_SIZE(ak8975_channels
);
944 indio_dev
->info
= &ak8975_info
;
945 indio_dev
->available_scan_masks
= ak8975_scan_masks
;
946 indio_dev
->modes
= INDIO_DIRECT_MODE
;
947 indio_dev
->name
= name
;
949 err
= iio_triggered_buffer_setup(indio_dev
, NULL
, ak8975_handle_trigger
,
952 dev_err(&client
->dev
, "triggered buffer setup failed\n");
956 err
= iio_device_register(indio_dev
);
958 dev_err(&client
->dev
, "device register failed\n");
965 iio_triggered_buffer_cleanup(indio_dev
);
967 ak8975_power_off(client
);
971 static int ak8975_remove(struct i2c_client
*client
)
973 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
975 iio_device_unregister(indio_dev
);
976 iio_triggered_buffer_cleanup(indio_dev
);
977 ak8975_power_off(client
);
982 static const struct i2c_device_id ak8975_id
[] = {
986 {"ak09911", AK09911
},
987 {"ak09912", AK09912
},
991 MODULE_DEVICE_TABLE(i2c
, ak8975_id
);
993 static const struct of_device_id ak8975_of_match
[] = {
994 { .compatible
= "asahi-kasei,ak8975", },
995 { .compatible
= "ak8975", },
996 { .compatible
= "asahi-kasei,ak8963", },
997 { .compatible
= "ak8963", },
998 { .compatible
= "asahi-kasei,ak09911", },
999 { .compatible
= "ak09911", },
1000 { .compatible
= "asahi-kasei,ak09912", },
1001 { .compatible
= "ak09912", },
1004 MODULE_DEVICE_TABLE(of
, ak8975_of_match
);
1006 static struct i2c_driver ak8975_driver
= {
1009 .of_match_table
= of_match_ptr(ak8975_of_match
),
1010 .acpi_match_table
= ACPI_PTR(ak_acpi_match
),
1012 .probe
= ak8975_probe
,
1013 .remove
= ak8975_remove
,
1014 .id_table
= ak8975_id
,
1016 module_i2c_driver(ak8975_driver
);
1018 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
1019 MODULE_DESCRIPTION("AK8975 magnetometer driver");
1020 MODULE_LICENSE("GPL");