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];
100 static const int ak8975_index_to_reg[] = {
101 AK8975_REG_HXL, AK8975_REG_HYL, AK8975_REG_HZL,
105 * Helper function to write to the I2C device's registers.
107 static int ak8975_write_data(struct i2c_client *client,
108 u8 reg, u8 val, u8 mask, u8 shift)
110 struct iio_dev *indio_dev = i2c_get_clientdata(client);
111 struct ak8975_data *data = iio_priv(indio_dev);
115 regval = (data->reg_cache[reg] & ~mask) | (val << shift);
116 ret = i2c_smbus_write_byte_data(client, reg, regval);
118 dev_err(&client->dev, "Write to device fails status %x\n", ret);
121 data->reg_cache[reg] = regval;
127 * Helper function to read a contiguous set of the I2C device's registers.
129 static int ak8975_read_data(struct i2c_client *client,
130 u8 reg, u8 length, u8 *buffer)
133 struct i2c_msg msg[2] = {
135 .addr = client->addr,
136 .flags = I2C_M_NOSTART,
140 .addr = client->addr,
147 ret = i2c_transfer(client->adapter, msg, 2);
149 dev_err(&client->dev, "Read from device fails\n");
157 * Perform some start-of-day setup, including reading the asa calibration
158 * values and caching them.
160 static int ak8975_setup(struct i2c_client *client)
162 struct iio_dev *indio_dev = i2c_get_clientdata(client);
163 struct ak8975_data *data = iio_priv(indio_dev);
167 /* Confirm that the device we're talking to is really an AK8975. */
168 ret = ak8975_read_data(client, AK8975_REG_WIA, 1, &device_id);
170 dev_err(&client->dev, "Error reading WIA\n");
173 if (device_id != AK8975_DEVICE_ID) {
174 dev_err(&client->dev, "Device ak8975 not found\n");
178 /* Write the fused rom access mode. */
179 ret = ak8975_write_data(client,
181 AK8975_REG_CNTL_MODE_FUSE_ROM,
182 AK8975_REG_CNTL_MODE_MASK,
183 AK8975_REG_CNTL_MODE_SHIFT);
185 dev_err(&client->dev, "Error in setting fuse access mode\n");
189 /* Get asa data and store in the device data. */
190 ret = ak8975_read_data(client, AK8975_REG_ASAX, 3, data->asa);
192 dev_err(&client->dev, "Not able to read asa data\n");
196 /* After reading fuse ROM data set power-down mode */
197 ret = ak8975_write_data(client,
199 AK8975_REG_CNTL_MODE_POWER_DOWN,
200 AK8975_REG_CNTL_MODE_MASK,
201 AK8975_REG_CNTL_MODE_SHIFT);
203 dev_err(&client->dev, "Error in setting power-down mode\n");
208 * Precalculate scale factor (in Gauss units) for each axis and
209 * store in the device data.
211 * This scale factor is axis-dependent, and is derived from 3 calibration
212 * factors ASA(x), ASA(y), and ASA(z).
214 * These ASA values are read from the sensor device at start of day, and
215 * cached in the device context struct.
217 * Adjusting the flux value with the sensitivity adjustment value should be
218 * done via the following formula:
220 * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
222 * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
223 * is the resultant adjusted value.
225 * We reduce the formula to:
227 * Hadj = H * (ASA + 128) / 256
229 * H is in the range of -4096 to 4095. The magnetometer has a range of
230 * +-1229uT. To go from the raw value to uT is:
232 * HuT = H * 1229/4096, or roughly, 3/10.
234 * Since 1uT = 100 gauss, our final scale factor becomes:
236 * Hadj = H * ((ASA + 128) / 256) * 3/10 * 100
237 * Hadj = H * ((ASA + 128) * 30 / 256
239 * Since ASA doesn't change, we cache the resultant scale factor into the
240 * device context in ak8975_setup().
242 data->raw_to_gauss[0] = ((data->asa[0] + 128) * 30) >> 8;
243 data->raw_to_gauss[1] = ((data->asa[1] + 128) * 30) >> 8;
244 data->raw_to_gauss[2] = ((data->asa[2] + 128) * 30) >> 8;
249 static int wait_conversion_complete_gpio(struct ak8975_data *data)
251 struct i2c_client *client = data->client;
253 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
256 /* Wait for the conversion to complete. */
258 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
259 if (gpio_get_value(data->eoc_gpio))
261 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
264 dev_err(&client->dev, "Conversion timeout happened\n");
268 ret = ak8975_read_data(client, AK8975_REG_ST1, 1, &read_status);
270 dev_err(&client->dev, "Error in reading ST1\n");
276 static int wait_conversion_complete_polled(struct ak8975_data *data)
278 struct i2c_client *client = data->client;
280 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
283 /* Wait for the conversion to complete. */
285 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
286 ret = ak8975_read_data(client, AK8975_REG_ST1, 1, &read_status);
288 dev_err(&client->dev, "Error in reading ST1\n");
293 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
296 dev_err(&client->dev, "Conversion timeout happened\n");
303 * Emits the raw flux value for the x, y, or z axis.
305 static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
307 struct ak8975_data *data = iio_priv(indio_dev);
308 struct i2c_client *client = data->client;
314 mutex_lock(&data->lock);
316 /* Set up the device for taking a sample. */
317 ret = ak8975_write_data(client,
319 AK8975_REG_CNTL_MODE_ONCE,
320 AK8975_REG_CNTL_MODE_MASK,
321 AK8975_REG_CNTL_MODE_SHIFT);
323 dev_err(&client->dev, "Error in setting operating mode\n");
327 /* Wait for the conversion to complete. */
328 if (gpio_is_valid(data->eoc_gpio))
329 ret = wait_conversion_complete_gpio(data);
331 ret = wait_conversion_complete_polled(data);
337 if (read_status & AK8975_REG_ST1_DRDY_MASK) {
338 ret = ak8975_read_data(client, AK8975_REG_ST2, 1, &read_status);
340 dev_err(&client->dev, "Error in reading ST2\n");
343 if (read_status & (AK8975_REG_ST2_DERR_MASK |
344 AK8975_REG_ST2_HOFL_MASK)) {
345 dev_err(&client->dev, "ST2 status error 0x%x\n",
352 /* Read the flux value from the appropriate register
353 (the register is specified in the iio device attributes). */
354 ret = ak8975_read_data(client, ak8975_index_to_reg[index],
357 dev_err(&client->dev, "Read axis data fails\n");
361 mutex_unlock(&data->lock);
363 /* Endian conversion of the measured values. */
364 raw = (s16) (le16_to_cpu(meas_reg));
366 /* Clamp to valid range. */
367 raw = clamp_t(s16, raw, -4096, 4095);
372 mutex_unlock(&data->lock);
376 static int ak8975_read_raw(struct iio_dev *indio_dev,
377 struct iio_chan_spec const *chan,
381 struct ak8975_data *data = iio_priv(indio_dev);
384 case IIO_CHAN_INFO_RAW:
385 return ak8975_read_axis(indio_dev, chan->address, val);
386 case IIO_CHAN_INFO_SCALE:
387 *val = data->raw_to_gauss[chan->address];
393 #define AK8975_CHANNEL(axis, index) \
397 .channel2 = IIO_MOD_##axis, \
398 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
399 IIO_CHAN_INFO_SCALE_SEPARATE_BIT, \
403 static const struct iio_chan_spec ak8975_channels[] = {
404 AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2),
407 static const struct iio_info ak8975_info = {
408 .read_raw = &ak8975_read_raw,
409 .driver_module = THIS_MODULE,
412 static int __devinit ak8975_probe(struct i2c_client *client,
413 const struct i2c_device_id *id)
415 struct ak8975_data *data;
416 struct iio_dev *indio_dev;
420 /* Grab and set up the supplied GPIO. */
421 if (client->dev.platform_data == NULL)
424 eoc_gpio = *(int *)(client->dev.platform_data);
426 /* We may not have a GPIO based IRQ to scan, that is fine, we will
428 if (gpio_is_valid(eoc_gpio)) {
429 err = gpio_request_one(eoc_gpio, GPIOF_IN, "ak_8975");
431 dev_err(&client->dev,
432 "failed to request GPIO %d, error %d\n",
438 /* Register with IIO */
439 indio_dev = iio_device_alloc(sizeof(*data));
440 if (indio_dev == NULL) {
444 data = iio_priv(indio_dev);
445 i2c_set_clientdata(client, indio_dev);
446 /* Perform some basic start-of-day setup of the device. */
447 err = ak8975_setup(client);
449 dev_err(&client->dev, "AK8975 initialization fails\n");
453 data->client = client;
454 mutex_init(&data->lock);
455 data->eoc_irq = client->irq;
456 data->eoc_gpio = eoc_gpio;
457 indio_dev->dev.parent = &client->dev;
458 indio_dev->channels = ak8975_channels;
459 indio_dev->num_channels = ARRAY_SIZE(ak8975_channels);
460 indio_dev->info = &ak8975_info;
461 indio_dev->modes = INDIO_DIRECT_MODE;
463 err = iio_device_register(indio_dev);
470 iio_device_free(indio_dev);
472 if (gpio_is_valid(eoc_gpio))
478 static int __devexit ak8975_remove(struct i2c_client *client)
480 struct iio_dev *indio_dev = i2c_get_clientdata(client);
481 struct ak8975_data *data = iio_priv(indio_dev);
483 iio_device_unregister(indio_dev);
485 if (gpio_is_valid(data->eoc_gpio))
486 gpio_free(data->eoc_gpio);
488 iio_device_free(indio_dev);
493 static const struct i2c_device_id ak8975_id[] = {
498 MODULE_DEVICE_TABLE(i2c, ak8975_id);
500 static const struct of_device_id ak8975_of_match[] = {
501 { .compatible = "asahi-kasei,ak8975", },
502 { .compatible = "ak8975", },
505 MODULE_DEVICE_TABLE(of, ak8975_of_match);
507 static struct i2c_driver ak8975_driver = {
510 .of_match_table = ak8975_of_match,
512 .probe = ak8975_probe,
513 .remove = __devexit_p(ak8975_remove),
514 .id_table = ak8975_id,
516 module_i2c_driver(ak8975_driver);
518 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
519 MODULE_DESCRIPTION("AK8975 magnetometer driver");
520 MODULE_LICENSE("GPL");