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>
36 #include <linux/iio/iio.h>
37 #include <linux/iio/sysfs.h>
39 * Register definitions, as well as various shifts and masks to get at the
40 * individual fields of the registers.
42 #define AK8975_REG_WIA 0x00
43 #define AK8975_DEVICE_ID 0x48
45 #define AK8975_REG_INFO 0x01
47 #define AK8975_REG_ST1 0x02
48 #define AK8975_REG_ST1_DRDY_SHIFT 0
49 #define AK8975_REG_ST1_DRDY_MASK (1 << AK8975_REG_ST1_DRDY_SHIFT)
51 #define AK8975_REG_HXL 0x03
52 #define AK8975_REG_HXH 0x04
53 #define AK8975_REG_HYL 0x05
54 #define AK8975_REG_HYH 0x06
55 #define AK8975_REG_HZL 0x07
56 #define AK8975_REG_HZH 0x08
57 #define AK8975_REG_ST2 0x09
58 #define AK8975_REG_ST2_DERR_SHIFT 2
59 #define AK8975_REG_ST2_DERR_MASK (1 << AK8975_REG_ST2_DERR_SHIFT)
61 #define AK8975_REG_ST2_HOFL_SHIFT 3
62 #define AK8975_REG_ST2_HOFL_MASK (1 << AK8975_REG_ST2_HOFL_SHIFT)
64 #define AK8975_REG_CNTL 0x0A
65 #define AK8975_REG_CNTL_MODE_SHIFT 0
66 #define AK8975_REG_CNTL_MODE_MASK (0xF << AK8975_REG_CNTL_MODE_SHIFT)
67 #define AK8975_REG_CNTL_MODE_POWER_DOWN 0
68 #define AK8975_REG_CNTL_MODE_ONCE 1
69 #define AK8975_REG_CNTL_MODE_SELF_TEST 8
70 #define AK8975_REG_CNTL_MODE_FUSE_ROM 0xF
72 #define AK8975_REG_RSVC 0x0B
73 #define AK8975_REG_ASTC 0x0C
74 #define AK8975_REG_TS1 0x0D
75 #define AK8975_REG_TS2 0x0E
76 #define AK8975_REG_I2CDIS 0x0F
77 #define AK8975_REG_ASAX 0x10
78 #define AK8975_REG_ASAY 0x11
79 #define AK8975_REG_ASAZ 0x12
81 #define AK8975_MAX_REGS AK8975_REG_ASAZ
84 * Miscellaneous values.
86 #define AK8975_MAX_CONVERSION_TIMEOUT 500
87 #define AK8975_CONVERSION_DONE_POLL_TIME 10
88 #define AK8975_DATA_READY_TIMEOUT ((100*HZ)/1000)
89 #define RAW_TO_GAUSS_8975(asa) ((((asa) + 128) * 3000) / 256)
90 #define RAW_TO_GAUSS_8963(asa) ((((asa) + 128) * 6000) / 256)
92 /* Compatible Asahi Kasei Compass parts */
93 enum asahi_compass_chipset {
99 * Per-instance context data for the device.
102 struct i2c_client *client;
103 struct attribute_group attrs;
106 long raw_to_gauss[3];
107 u8 reg_cache[AK8975_MAX_REGS];
110 wait_queue_head_t data_ready_queue;
112 enum asahi_compass_chipset chipset;
115 static const int ak8975_index_to_reg[] = {
116 AK8975_REG_HXL, AK8975_REG_HYL, AK8975_REG_HZL,
120 * Helper function to write to the I2C device's registers.
122 static int ak8975_write_data(struct i2c_client *client,
123 u8 reg, u8 val, u8 mask, u8 shift)
125 struct iio_dev *indio_dev = i2c_get_clientdata(client);
126 struct ak8975_data *data = iio_priv(indio_dev);
130 regval = (data->reg_cache[reg] & ~mask) | (val << shift);
131 ret = i2c_smbus_write_byte_data(client, reg, regval);
133 dev_err(&client->dev, "Write to device fails status %x\n", ret);
136 data->reg_cache[reg] = regval;
142 * Handle data ready irq
144 static irqreturn_t ak8975_irq_handler(int irq, void *data)
146 struct ak8975_data *ak8975 = data;
148 set_bit(0, &ak8975->flags);
149 wake_up(&ak8975->data_ready_queue);
155 * Install data ready interrupt handler
157 static int ak8975_setup_irq(struct ak8975_data *data)
159 struct i2c_client *client = data->client;
166 irq = gpio_to_irq(data->eoc_gpio);
168 rc = devm_request_irq(&client->dev, irq, ak8975_irq_handler,
169 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
170 dev_name(&client->dev), data);
172 dev_err(&client->dev,
173 "irq %d request failed, (gpio %d): %d\n",
174 irq, data->eoc_gpio, rc);
178 init_waitqueue_head(&data->data_ready_queue);
179 clear_bit(0, &data->flags);
187 * Perform some start-of-day setup, including reading the asa calibration
188 * values and caching them.
190 static int ak8975_setup(struct i2c_client *client)
192 struct iio_dev *indio_dev = i2c_get_clientdata(client);
193 struct ak8975_data *data = iio_priv(indio_dev);
197 /* Confirm that the device we're talking to is really an AK8975. */
198 ret = i2c_smbus_read_byte_data(client, AK8975_REG_WIA);
200 dev_err(&client->dev, "Error reading WIA\n");
204 if (device_id != AK8975_DEVICE_ID) {
205 dev_err(&client->dev, "Device ak8975 not found\n");
209 /* Write the fused rom access mode. */
210 ret = ak8975_write_data(client,
212 AK8975_REG_CNTL_MODE_FUSE_ROM,
213 AK8975_REG_CNTL_MODE_MASK,
214 AK8975_REG_CNTL_MODE_SHIFT);
216 dev_err(&client->dev, "Error in setting fuse access mode\n");
220 /* Get asa data and store in the device data. */
221 ret = i2c_smbus_read_i2c_block_data(client, AK8975_REG_ASAX,
224 dev_err(&client->dev, "Not able to read asa data\n");
228 /* After reading fuse ROM data set power-down mode */
229 ret = ak8975_write_data(client,
231 AK8975_REG_CNTL_MODE_POWER_DOWN,
232 AK8975_REG_CNTL_MODE_MASK,
233 AK8975_REG_CNTL_MODE_SHIFT);
235 if (data->eoc_gpio > 0 || client->irq) {
236 ret = ak8975_setup_irq(data);
238 dev_err(&client->dev,
239 "Error setting data ready interrupt\n");
245 dev_err(&client->dev, "Error in setting power-down mode\n");
250 * Precalculate scale factor (in Gauss units) for each axis and
251 * store in the device data.
253 * This scale factor is axis-dependent, and is derived from 3 calibration
254 * factors ASA(x), ASA(y), and ASA(z).
256 * These ASA values are read from the sensor device at start of day, and
257 * cached in the device context struct.
259 * Adjusting the flux value with the sensitivity adjustment value should be
260 * done via the following formula:
262 * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
264 * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
265 * is the resultant adjusted value.
267 * We reduce the formula to:
269 * Hadj = H * (ASA + 128) / 256
271 * H is in the range of -4096 to 4095. The magnetometer has a range of
272 * +-1229uT. To go from the raw value to uT is:
274 * HuT = H * 1229/4096, or roughly, 3/10.
276 * Since 1uT = 0.01 gauss, our final scale factor becomes:
278 * Hadj = H * ((ASA + 128) / 256) * 3/10 * 1/100
279 * Hadj = H * ((ASA + 128) * 0.003) / 256
281 * Since ASA doesn't change, we cache the resultant scale factor into the
282 * device context in ak8975_setup().
284 if (data->chipset == AK8963) {
286 * H range is +-8190 and magnetometer range is +-4912.
287 * So HuT using the above explanation for 8975,
288 * 4912/8190 = ~ 6/10.
289 * So the Hadj should use 6/10 instead of 3/10.
291 data->raw_to_gauss[0] = RAW_TO_GAUSS_8963(data->asa[0]);
292 data->raw_to_gauss[1] = RAW_TO_GAUSS_8963(data->asa[1]);
293 data->raw_to_gauss[2] = RAW_TO_GAUSS_8963(data->asa[2]);
295 data->raw_to_gauss[0] = RAW_TO_GAUSS_8975(data->asa[0]);
296 data->raw_to_gauss[1] = RAW_TO_GAUSS_8975(data->asa[1]);
297 data->raw_to_gauss[2] = RAW_TO_GAUSS_8975(data->asa[2]);
303 static int wait_conversion_complete_gpio(struct ak8975_data *data)
305 struct i2c_client *client = data->client;
306 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
309 /* Wait for the conversion to complete. */
311 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
312 if (gpio_get_value(data->eoc_gpio))
314 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
317 dev_err(&client->dev, "Conversion timeout happened\n");
321 ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST1);
323 dev_err(&client->dev, "Error in reading ST1\n");
328 static int wait_conversion_complete_polled(struct ak8975_data *data)
330 struct i2c_client *client = data->client;
332 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
335 /* Wait for the conversion to complete. */
337 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
338 ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST1);
340 dev_err(&client->dev, "Error in reading ST1\n");
346 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
349 dev_err(&client->dev, "Conversion timeout happened\n");
356 /* Returns 0 if the end of conversion interrupt occured or -ETIME otherwise */
357 static int wait_conversion_complete_interrupt(struct ak8975_data *data)
361 ret = wait_event_timeout(data->data_ready_queue,
362 test_bit(0, &data->flags),
363 AK8975_DATA_READY_TIMEOUT);
364 clear_bit(0, &data->flags);
366 return ret > 0 ? 0 : -ETIME;
370 * Emits the raw flux value for the x, y, or z axis.
372 static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
374 struct ak8975_data *data = iio_priv(indio_dev);
375 struct i2c_client *client = data->client;
378 mutex_lock(&data->lock);
380 /* Set up the device for taking a sample. */
381 ret = ak8975_write_data(client,
383 AK8975_REG_CNTL_MODE_ONCE,
384 AK8975_REG_CNTL_MODE_MASK,
385 AK8975_REG_CNTL_MODE_SHIFT);
387 dev_err(&client->dev, "Error in setting operating mode\n");
391 /* Wait for the conversion to complete. */
393 ret = wait_conversion_complete_interrupt(data);
394 else if (gpio_is_valid(data->eoc_gpio))
395 ret = wait_conversion_complete_gpio(data);
397 ret = wait_conversion_complete_polled(data);
401 /* This will be executed only for non-interrupt based waiting case */
402 if (ret & AK8975_REG_ST1_DRDY_MASK) {
403 ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST2);
405 dev_err(&client->dev, "Error in reading ST2\n");
408 if (ret & (AK8975_REG_ST2_DERR_MASK |
409 AK8975_REG_ST2_HOFL_MASK)) {
410 dev_err(&client->dev, "ST2 status error 0x%x\n", ret);
416 /* Read the flux value from the appropriate register
417 (the register is specified in the iio device attributes). */
418 ret = i2c_smbus_read_word_data(client, ak8975_index_to_reg[index]);
420 dev_err(&client->dev, "Read axis data fails\n");
424 mutex_unlock(&data->lock);
426 /* Clamp to valid range. */
427 *val = clamp_t(s16, ret, -4096, 4095);
431 mutex_unlock(&data->lock);
435 static int ak8975_read_raw(struct iio_dev *indio_dev,
436 struct iio_chan_spec const *chan,
440 struct ak8975_data *data = iio_priv(indio_dev);
443 case IIO_CHAN_INFO_RAW:
444 return ak8975_read_axis(indio_dev, chan->address, val);
445 case IIO_CHAN_INFO_SCALE:
447 *val2 = data->raw_to_gauss[chan->address];
448 return IIO_VAL_INT_PLUS_MICRO;
453 #define AK8975_CHANNEL(axis, index) \
457 .channel2 = IIO_MOD_##axis, \
458 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
459 BIT(IIO_CHAN_INFO_SCALE), \
463 static const struct iio_chan_spec ak8975_channels[] = {
464 AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2),
467 static const struct iio_info ak8975_info = {
468 .read_raw = &ak8975_read_raw,
469 .driver_module = THIS_MODULE,
472 static const struct acpi_device_id ak_acpi_match[] = {
475 {"INVN6500", AK8963},
478 MODULE_DEVICE_TABLE(acpi, ak_acpi_match);
480 static char *ak8975_match_acpi_device(struct device *dev,
481 enum asahi_compass_chipset *chipset)
483 const struct acpi_device_id *id;
485 id = acpi_match_device(dev->driver->acpi_match_table, dev);
488 *chipset = (int)id->driver_data;
490 return (char *)dev_name(dev);
493 static int ak8975_probe(struct i2c_client *client,
494 const struct i2c_device_id *id)
496 struct ak8975_data *data;
497 struct iio_dev *indio_dev;
502 /* Grab and set up the supplied GPIO. */
503 if (client->dev.platform_data)
504 eoc_gpio = *(int *)(client->dev.platform_data);
505 else if (client->dev.of_node)
506 eoc_gpio = of_get_gpio(client->dev.of_node, 0);
510 if (eoc_gpio == -EPROBE_DEFER)
511 return -EPROBE_DEFER;
513 /* We may not have a GPIO based IRQ to scan, that is fine, we will
515 if (gpio_is_valid(eoc_gpio)) {
516 err = devm_gpio_request_one(&client->dev, eoc_gpio,
517 GPIOF_IN, "ak_8975");
519 dev_err(&client->dev,
520 "failed to request GPIO %d, error %d\n",
526 /* Register with IIO */
527 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
528 if (indio_dev == NULL)
531 data = iio_priv(indio_dev);
532 i2c_set_clientdata(client, indio_dev);
534 data->client = client;
535 data->eoc_gpio = eoc_gpio;
538 /* id will be NULL when enumerated via ACPI */
541 (enum asahi_compass_chipset)(id->driver_data);
542 name = (char *) id->name;
543 } else if (ACPI_HANDLE(&client->dev))
544 name = ak8975_match_acpi_device(&client->dev, &data->chipset);
548 dev_dbg(&client->dev, "Asahi compass chip %s\n", name);
550 /* Perform some basic start-of-day setup of the device. */
551 err = ak8975_setup(client);
553 dev_err(&client->dev, "AK8975 initialization fails\n");
557 data->client = client;
558 mutex_init(&data->lock);
559 data->eoc_gpio = eoc_gpio;
560 indio_dev->dev.parent = &client->dev;
561 indio_dev->channels = ak8975_channels;
562 indio_dev->num_channels = ARRAY_SIZE(ak8975_channels);
563 indio_dev->info = &ak8975_info;
564 indio_dev->modes = INDIO_DIRECT_MODE;
565 indio_dev->name = name;
566 err = devm_iio_device_register(&client->dev, indio_dev);
573 static const struct i2c_device_id ak8975_id[] = {
579 MODULE_DEVICE_TABLE(i2c, ak8975_id);
581 static const struct of_device_id ak8975_of_match[] = {
582 { .compatible = "asahi-kasei,ak8975", },
583 { .compatible = "ak8975", },
586 MODULE_DEVICE_TABLE(of, ak8975_of_match);
588 static struct i2c_driver ak8975_driver = {
591 .of_match_table = ak8975_of_match,
592 .acpi_match_table = ACPI_PTR(ak_acpi_match),
594 .probe = ak8975_probe,
595 .id_table = ak8975_id,
597 module_i2c_driver(ak8975_driver);
599 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
600 MODULE_DESCRIPTION("AK8975 magnetometer driver");
601 MODULE_LICENSE("GPL");