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/err.h>
28 #include <linux/mutex.h>
29 #include <linux/delay.h>
31 #include <linux/gpio.h>
33 #include <linux/iio/iio.h>
34 #include <linux/iio/sysfs.h>
36 * Register definitions, as well as various shifts and masks to get at the
37 * individual fields of the registers.
39 #define AK8975_REG_WIA 0x00
40 #define AK8975_DEVICE_ID 0x48
42 #define AK8975_REG_INFO 0x01
44 #define AK8975_REG_ST1 0x02
45 #define AK8975_REG_ST1_DRDY_SHIFT 0
46 #define AK8975_REG_ST1_DRDY_MASK (1 << AK8975_REG_ST1_DRDY_SHIFT)
48 #define AK8975_REG_HXL 0x03
49 #define AK8975_REG_HXH 0x04
50 #define AK8975_REG_HYL 0x05
51 #define AK8975_REG_HYH 0x06
52 #define AK8975_REG_HZL 0x07
53 #define AK8975_REG_HZH 0x08
54 #define AK8975_REG_ST2 0x09
55 #define AK8975_REG_ST2_DERR_SHIFT 2
56 #define AK8975_REG_ST2_DERR_MASK (1 << AK8975_REG_ST2_DERR_SHIFT)
58 #define AK8975_REG_ST2_HOFL_SHIFT 3
59 #define AK8975_REG_ST2_HOFL_MASK (1 << AK8975_REG_ST2_HOFL_SHIFT)
61 #define AK8975_REG_CNTL 0x0A
62 #define AK8975_REG_CNTL_MODE_SHIFT 0
63 #define AK8975_REG_CNTL_MODE_MASK (0xF << AK8975_REG_CNTL_MODE_SHIFT)
64 #define AK8975_REG_CNTL_MODE_POWER_DOWN 0
65 #define AK8975_REG_CNTL_MODE_ONCE 1
66 #define AK8975_REG_CNTL_MODE_SELF_TEST 8
67 #define AK8975_REG_CNTL_MODE_FUSE_ROM 0xF
69 #define AK8975_REG_RSVC 0x0B
70 #define AK8975_REG_ASTC 0x0C
71 #define AK8975_REG_TS1 0x0D
72 #define AK8975_REG_TS2 0x0E
73 #define AK8975_REG_I2CDIS 0x0F
74 #define AK8975_REG_ASAX 0x10
75 #define AK8975_REG_ASAY 0x11
76 #define AK8975_REG_ASAZ 0x12
78 #define AK8975_MAX_REGS AK8975_REG_ASAZ
81 * Miscellaneous values.
83 #define AK8975_MAX_CONVERSION_TIMEOUT 500
84 #define AK8975_CONVERSION_DONE_POLL_TIME 10
87 * Per-instance context data for the device.
90 struct i2c_client *client;
91 struct attribute_group attrs;
95 u8 reg_cache[AK8975_MAX_REGS];
99 static const int ak8975_index_to_reg[] = {
100 AK8975_REG_HXL, AK8975_REG_HYL, AK8975_REG_HZL,
104 * Helper function to write to the I2C device's registers.
106 static int ak8975_write_data(struct i2c_client *client,
107 u8 reg, u8 val, u8 mask, u8 shift)
109 struct iio_dev *indio_dev = i2c_get_clientdata(client);
110 struct ak8975_data *data = iio_priv(indio_dev);
114 regval = (data->reg_cache[reg] & ~mask) | (val << shift);
115 ret = i2c_smbus_write_byte_data(client, reg, regval);
117 dev_err(&client->dev, "Write to device fails status %x\n", ret);
120 data->reg_cache[reg] = regval;
126 * Perform some start-of-day setup, including reading the asa calibration
127 * values and caching them.
129 static int ak8975_setup(struct i2c_client *client)
131 struct iio_dev *indio_dev = i2c_get_clientdata(client);
132 struct ak8975_data *data = iio_priv(indio_dev);
136 /* Confirm that the device we're talking to is really an AK8975. */
137 ret = i2c_smbus_read_byte_data(client, AK8975_REG_WIA);
139 dev_err(&client->dev, "Error reading WIA\n");
143 if (device_id != AK8975_DEVICE_ID) {
144 dev_err(&client->dev, "Device ak8975 not found\n");
148 /* Write the fused rom access mode. */
149 ret = ak8975_write_data(client,
151 AK8975_REG_CNTL_MODE_FUSE_ROM,
152 AK8975_REG_CNTL_MODE_MASK,
153 AK8975_REG_CNTL_MODE_SHIFT);
155 dev_err(&client->dev, "Error in setting fuse access mode\n");
159 /* Get asa data and store in the device data. */
160 ret = i2c_smbus_read_i2c_block_data(client, AK8975_REG_ASAX,
163 dev_err(&client->dev, "Not able to read asa data\n");
167 /* After reading fuse ROM data set power-down mode */
168 ret = ak8975_write_data(client,
170 AK8975_REG_CNTL_MODE_POWER_DOWN,
171 AK8975_REG_CNTL_MODE_MASK,
172 AK8975_REG_CNTL_MODE_SHIFT);
174 dev_err(&client->dev, "Error in setting power-down mode\n");
179 * Precalculate scale factor (in Gauss units) for each axis and
180 * store in the device data.
182 * This scale factor is axis-dependent, and is derived from 3 calibration
183 * factors ASA(x), ASA(y), and ASA(z).
185 * These ASA values are read from the sensor device at start of day, and
186 * cached in the device context struct.
188 * Adjusting the flux value with the sensitivity adjustment value should be
189 * done via the following formula:
191 * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
193 * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
194 * is the resultant adjusted value.
196 * We reduce the formula to:
198 * Hadj = H * (ASA + 128) / 256
200 * H is in the range of -4096 to 4095. The magnetometer has a range of
201 * +-1229uT. To go from the raw value to uT is:
203 * HuT = H * 1229/4096, or roughly, 3/10.
205 * Since 1uT = 100 gauss, our final scale factor becomes:
207 * Hadj = H * ((ASA + 128) / 256) * 3/10 * 100
208 * Hadj = H * ((ASA + 128) * 30 / 256
210 * Since ASA doesn't change, we cache the resultant scale factor into the
211 * device context in ak8975_setup().
213 data->raw_to_gauss[0] = ((data->asa[0] + 128) * 30) >> 8;
214 data->raw_to_gauss[1] = ((data->asa[1] + 128) * 30) >> 8;
215 data->raw_to_gauss[2] = ((data->asa[2] + 128) * 30) >> 8;
220 static int wait_conversion_complete_gpio(struct ak8975_data *data)
222 struct i2c_client *client = data->client;
223 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
226 /* Wait for the conversion to complete. */
228 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
229 if (gpio_get_value(data->eoc_gpio))
231 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
234 dev_err(&client->dev, "Conversion timeout happened\n");
238 ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST1);
240 dev_err(&client->dev, "Error in reading ST1\n");
245 static int wait_conversion_complete_polled(struct ak8975_data *data)
247 struct i2c_client *client = data->client;
249 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
252 /* Wait for the conversion to complete. */
254 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
255 ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST1);
257 dev_err(&client->dev, "Error in reading ST1\n");
263 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
266 dev_err(&client->dev, "Conversion timeout happened\n");
273 * Emits the raw flux value for the x, y, or z axis.
275 static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
277 struct ak8975_data *data = iio_priv(indio_dev);
278 struct i2c_client *client = data->client;
283 mutex_lock(&data->lock);
285 /* Set up the device for taking a sample. */
286 ret = ak8975_write_data(client,
288 AK8975_REG_CNTL_MODE_ONCE,
289 AK8975_REG_CNTL_MODE_MASK,
290 AK8975_REG_CNTL_MODE_SHIFT);
292 dev_err(&client->dev, "Error in setting operating mode\n");
296 /* Wait for the conversion to complete. */
297 if (gpio_is_valid(data->eoc_gpio))
298 ret = wait_conversion_complete_gpio(data);
300 ret = wait_conversion_complete_polled(data);
304 if (ret & AK8975_REG_ST1_DRDY_MASK) {
305 ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST2);
307 dev_err(&client->dev, "Error in reading ST2\n");
310 if (ret & (AK8975_REG_ST2_DERR_MASK |
311 AK8975_REG_ST2_HOFL_MASK)) {
312 dev_err(&client->dev, "ST2 status error 0x%x\n", ret);
318 /* Read the flux value from the appropriate register
319 (the register is specified in the iio device attributes). */
320 ret = i2c_smbus_read_word_data(client, ak8975_index_to_reg[index]);
322 dev_err(&client->dev, "Read axis data fails\n");
327 mutex_unlock(&data->lock);
329 /* Endian conversion of the measured values. */
330 raw = (s16) (le16_to_cpu(meas_reg));
332 /* Clamp to valid range. */
333 raw = clamp_t(s16, raw, -4096, 4095);
338 mutex_unlock(&data->lock);
342 static int ak8975_read_raw(struct iio_dev *indio_dev,
343 struct iio_chan_spec const *chan,
347 struct ak8975_data *data = iio_priv(indio_dev);
350 case IIO_CHAN_INFO_RAW:
351 return ak8975_read_axis(indio_dev, chan->address, val);
352 case IIO_CHAN_INFO_SCALE:
353 *val = data->raw_to_gauss[chan->address];
359 #define AK8975_CHANNEL(axis, index) \
363 .channel2 = IIO_MOD_##axis, \
364 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
365 BIT(IIO_CHAN_INFO_SCALE), \
369 static const struct iio_chan_spec ak8975_channels[] = {
370 AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2),
373 static const struct iio_info ak8975_info = {
374 .read_raw = &ak8975_read_raw,
375 .driver_module = THIS_MODULE,
378 static int ak8975_probe(struct i2c_client *client,
379 const struct i2c_device_id *id)
381 struct ak8975_data *data;
382 struct iio_dev *indio_dev;
386 /* Grab and set up the supplied GPIO. */
387 if (client->dev.platform_data == NULL)
390 eoc_gpio = *(int *)(client->dev.platform_data);
392 /* We may not have a GPIO based IRQ to scan, that is fine, we will
394 if (gpio_is_valid(eoc_gpio)) {
395 err = gpio_request_one(eoc_gpio, GPIOF_IN, "ak_8975");
397 dev_err(&client->dev,
398 "failed to request GPIO %d, error %d\n",
404 /* Register with IIO */
405 indio_dev = iio_device_alloc(sizeof(*data));
406 if (indio_dev == NULL) {
410 data = iio_priv(indio_dev);
411 i2c_set_clientdata(client, indio_dev);
412 /* Perform some basic start-of-day setup of the device. */
413 err = ak8975_setup(client);
415 dev_err(&client->dev, "AK8975 initialization fails\n");
419 data->client = client;
420 mutex_init(&data->lock);
421 data->eoc_gpio = eoc_gpio;
422 indio_dev->dev.parent = &client->dev;
423 indio_dev->channels = ak8975_channels;
424 indio_dev->num_channels = ARRAY_SIZE(ak8975_channels);
425 indio_dev->info = &ak8975_info;
426 indio_dev->modes = INDIO_DIRECT_MODE;
428 err = iio_device_register(indio_dev);
435 iio_device_free(indio_dev);
437 if (gpio_is_valid(eoc_gpio))
443 static int ak8975_remove(struct i2c_client *client)
445 struct iio_dev *indio_dev = i2c_get_clientdata(client);
446 struct ak8975_data *data = iio_priv(indio_dev);
448 iio_device_unregister(indio_dev);
450 if (gpio_is_valid(data->eoc_gpio))
451 gpio_free(data->eoc_gpio);
453 iio_device_free(indio_dev);
458 static const struct i2c_device_id ak8975_id[] = {
463 MODULE_DEVICE_TABLE(i2c, ak8975_id);
465 static const struct of_device_id ak8975_of_match[] = {
466 { .compatible = "asahi-kasei,ak8975", },
467 { .compatible = "ak8975", },
470 MODULE_DEVICE_TABLE(of, ak8975_of_match);
472 static struct i2c_driver ak8975_driver = {
475 .of_match_table = ak8975_of_match,
477 .probe = ak8975_probe,
478 .remove = ak8975_remove,
479 .id_table = ak8975_id,
481 module_i2c_driver(ak8975_driver);
483 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
484 MODULE_DESCRIPTION("AK8975 magnetometer driver");
485 MODULE_LICENSE("GPL");