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>
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
;
96 u8 reg_cache
[AK8975_MAX_REGS
];
101 static const int ak8975_index_to_reg
[] = {
102 AK8975_REG_HXL
, AK8975_REG_HYL
, AK8975_REG_HZL
,
106 * Helper function to write to the I2C device's registers.
108 static int ak8975_write_data(struct i2c_client
*client
,
109 u8 reg
, u8 val
, u8 mask
, u8 shift
)
111 struct ak8975_data
*data
= i2c_get_clientdata(client
);
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 ak8975_data
*data
= i2c_get_clientdata(client
);
166 /* Confirm that the device we're talking to is really an AK8975. */
167 ret
= ak8975_read_data(client
, AK8975_REG_WIA
, 1, &device_id
);
169 dev_err(&client
->dev
, "Error reading WIA\n");
172 if (device_id
!= AK8975_DEVICE_ID
) {
173 dev_err(&client
->dev
, "Device ak8975 not found\n");
177 /* Write the fused rom access mode. */
178 ret
= ak8975_write_data(client
,
180 AK8975_REG_CNTL_MODE_FUSE_ROM
,
181 AK8975_REG_CNTL_MODE_MASK
,
182 AK8975_REG_CNTL_MODE_SHIFT
);
184 dev_err(&client
->dev
, "Error in setting fuse access mode\n");
188 /* Get asa data and store in the device data. */
189 ret
= ak8975_read_data(client
, AK8975_REG_ASAX
, 3, data
->asa
);
191 dev_err(&client
->dev
, "Not able to read asa data\n");
196 * Precalculate scale factor (in Gauss units) for each axis and
197 * store in the device data.
199 * This scale factor is axis-dependent, and is derived from 3 calibration
200 * factors ASA(x), ASA(y), and ASA(z).
202 * These ASA values are read from the sensor device at start of day, and
203 * cached in the device context struct.
205 * Adjusting the flux value with the sensitivity adjustment value should be
206 * done via the following formula:
208 * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
210 * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
211 * is the resultant adjusted value.
213 * We reduce the formula to:
215 * Hadj = H * (ASA + 128) / 256
217 * H is in the range of -4096 to 4095. The magnetometer has a range of
218 * +-1229uT. To go from the raw value to uT is:
220 * HuT = H * 1229/4096, or roughly, 3/10.
222 * Since 1uT = 100 gauss, our final scale factor becomes:
224 * Hadj = H * ((ASA + 128) / 256) * 3/10 * 100
225 * Hadj = H * ((ASA + 128) * 30 / 256
227 * Since ASA doesn't change, we cache the resultant scale factor into the
228 * device context in ak8975_setup().
230 data
->raw_to_gauss
[0] = ((data
->asa
[0] + 128) * 30) >> 8;
231 data
->raw_to_gauss
[1] = ((data
->asa
[1] + 128) * 30) >> 8;
232 data
->raw_to_gauss
[2] = ((data
->asa
[2] + 128) * 30) >> 8;
238 * Shows the device's mode. 0 = off, 1 = on.
240 static ssize_t
show_mode(struct device
*dev
, struct device_attribute
*devattr
,
243 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
244 struct ak8975_data
*data
= iio_priv(indio_dev
);
246 return sprintf(buf
, "%u\n", data
->mode
);
250 * Sets the device's mode. 0 = off, 1 = on. The device's mode must be on
251 * for the magn raw attributes to be available.
253 static ssize_t
store_mode(struct device
*dev
, struct device_attribute
*devattr
,
254 const char *buf
, size_t count
)
256 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
257 struct ak8975_data
*data
= iio_priv(indio_dev
);
258 struct i2c_client
*client
= data
->client
;
262 /* Convert mode string and do some basic sanity checking on it.
263 only 0 or 1 are valid. */
264 ret
= strtobool(buf
, &value
);
268 mutex_lock(&data
->lock
);
270 /* Write the mode to the device. */
271 if (data
->mode
!= value
) {
272 ret
= ak8975_write_data(client
,
275 AK8975_REG_CNTL_MODE_MASK
,
276 AK8975_REG_CNTL_MODE_SHIFT
);
279 dev_err(&client
->dev
, "Error in setting mode\n");
280 mutex_unlock(&data
->lock
);
286 mutex_unlock(&data
->lock
);
291 static int wait_conversion_complete_gpio(struct ak8975_data
*data
)
293 struct i2c_client
*client
= data
->client
;
295 u32 timeout_ms
= AK8975_MAX_CONVERSION_TIMEOUT
;
298 /* Wait for the conversion to complete. */
300 msleep(AK8975_CONVERSION_DONE_POLL_TIME
);
301 if (gpio_get_value(data
->eoc_gpio
))
303 timeout_ms
-= AK8975_CONVERSION_DONE_POLL_TIME
;
306 dev_err(&client
->dev
, "Conversion timeout happened\n");
310 ret
= ak8975_read_data(client
, AK8975_REG_ST1
, 1, &read_status
);
312 dev_err(&client
->dev
, "Error in reading ST1\n");
318 static int wait_conversion_complete_polled(struct ak8975_data
*data
)
320 struct i2c_client
*client
= data
->client
;
322 u32 timeout_ms
= AK8975_MAX_CONVERSION_TIMEOUT
;
325 /* Wait for the conversion to complete. */
327 msleep(AK8975_CONVERSION_DONE_POLL_TIME
);
328 ret
= ak8975_read_data(client
, AK8975_REG_ST1
, 1, &read_status
);
330 dev_err(&client
->dev
, "Error in reading ST1\n");
335 timeout_ms
-= AK8975_CONVERSION_DONE_POLL_TIME
;
338 dev_err(&client
->dev
, "Conversion timeout happened\n");
345 * Emits the raw flux value for the x, y, or z axis.
347 static int ak8975_read_axis(struct iio_dev
*indio_dev
, int index
, int *val
)
349 struct ak8975_data
*data
= iio_priv(indio_dev
);
350 struct i2c_client
*client
= data
->client
;
356 mutex_lock(&data
->lock
);
358 if (data
->mode
== 0) {
359 dev_err(&client
->dev
, "Operating mode is in power down mode\n");
364 /* Set up the device for taking a sample. */
365 ret
= ak8975_write_data(client
,
367 AK8975_REG_CNTL_MODE_ONCE
,
368 AK8975_REG_CNTL_MODE_MASK
,
369 AK8975_REG_CNTL_MODE_SHIFT
);
371 dev_err(&client
->dev
, "Error in setting operating mode\n");
375 /* Wait for the conversion to complete. */
376 if (gpio_is_valid(data
->eoc_gpio
))
377 ret
= wait_conversion_complete_gpio(data
);
379 ret
= wait_conversion_complete_polled(data
);
385 if (read_status
& AK8975_REG_ST1_DRDY_MASK
) {
386 ret
= ak8975_read_data(client
, AK8975_REG_ST2
, 1, &read_status
);
388 dev_err(&client
->dev
, "Error in reading ST2\n");
391 if (read_status
& (AK8975_REG_ST2_DERR_MASK
|
392 AK8975_REG_ST2_HOFL_MASK
)) {
393 dev_err(&client
->dev
, "ST2 status error 0x%x\n",
400 /* Read the flux value from the appropriate register
401 (the register is specified in the iio device attributes). */
402 ret
= ak8975_read_data(client
, ak8975_index_to_reg
[index
],
405 dev_err(&client
->dev
, "Read axis data fails\n");
409 mutex_unlock(&data
->lock
);
411 /* Endian conversion of the measured values. */
412 raw
= (s16
) (le16_to_cpu(meas_reg
));
414 /* Clamp to valid range. */
415 raw
= clamp_t(s16
, raw
, -4096, 4095);
420 mutex_unlock(&data
->lock
);
424 static int ak8975_read_raw(struct iio_dev
*indio_dev
,
425 struct iio_chan_spec
const *chan
,
429 struct ak8975_data
*data
= iio_priv(indio_dev
);
433 return ak8975_read_axis(indio_dev
, chan
->address
, val
);
434 case (1 << IIO_CHAN_INFO_SCALE_SEPARATE
):
435 *val
= data
->raw_to_gauss
[chan
->address
];
441 #define AK8975_CHANNEL(axis, index) \
445 .channel2 = IIO_MOD_##axis, \
446 .info_mask = (1 << IIO_CHAN_INFO_SCALE_SEPARATE), \
450 static const struct iio_chan_spec ak8975_channels
[] = {
451 AK8975_CHANNEL(X
, 0), AK8975_CHANNEL(Y
, 1), AK8975_CHANNEL(Z
, 2),
454 static IIO_DEVICE_ATTR(mode
, S_IRUGO
| S_IWUSR
, show_mode
, store_mode
, 0);
456 static struct attribute
*ak8975_attr
[] = {
457 &iio_dev_attr_mode
.dev_attr
.attr
,
461 static struct attribute_group ak8975_attr_group
= {
462 .attrs
= ak8975_attr
,
465 static const struct iio_info ak8975_info
= {
466 .attrs
= &ak8975_attr_group
,
467 .read_raw
= &ak8975_read_raw
,
468 .driver_module
= THIS_MODULE
,
471 static int ak8975_probe(struct i2c_client
*client
,
472 const struct i2c_device_id
*id
)
474 struct ak8975_data
*data
;
475 struct iio_dev
*indio_dev
;
479 /* Grab and set up the supplied GPIO. */
480 if (client
->dev
.platform_data
== NULL
)
483 eoc_gpio
= *(int *)(client
->dev
.platform_data
);
485 /* We may not have a GPIO based IRQ to scan, that is fine, we will
487 if (gpio_is_valid(eoc_gpio
)) {
488 err
= gpio_request(eoc_gpio
, "ak_8975");
490 dev_err(&client
->dev
,
491 "failed to request GPIO %d, error %d\n",
496 err
= gpio_direction_input(eoc_gpio
);
498 dev_err(&client
->dev
,
499 "Failed to configure input direction for GPIO %d, error %d\n",
505 /* Register with IIO */
506 indio_dev
= iio_allocate_device(sizeof(*data
));
507 if (indio_dev
== NULL
) {
511 data
= iio_priv(indio_dev
);
512 /* Perform some basic start-of-day setup of the device. */
513 err
= ak8975_setup(client
);
515 dev_err(&client
->dev
, "AK8975 initialization fails\n");
519 i2c_set_clientdata(client
, indio_dev
);
520 data
->client
= client
;
521 mutex_init(&data
->lock
);
522 data
->eoc_irq
= client
->irq
;
523 data
->eoc_gpio
= eoc_gpio
;
524 indio_dev
->dev
.parent
= &client
->dev
;
525 indio_dev
->channels
= ak8975_channels
;
526 indio_dev
->num_channels
= ARRAY_SIZE(ak8975_channels
);
527 indio_dev
->info
= &ak8975_info
;
528 indio_dev
->modes
= INDIO_DIRECT_MODE
;
530 err
= iio_device_register(indio_dev
);
537 iio_free_device(indio_dev
);
539 if (gpio_is_valid(eoc_gpio
))
545 static int ak8975_remove(struct i2c_client
*client
)
547 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
548 struct ak8975_data
*data
= iio_priv(indio_dev
);
550 iio_device_unregister(indio_dev
);
552 if (gpio_is_valid(data
->eoc_gpio
))
553 gpio_free(data
->eoc_gpio
);
555 iio_free_device(indio_dev
);
560 static const struct i2c_device_id ak8975_id
[] = {
565 MODULE_DEVICE_TABLE(i2c
, ak8975_id
);
567 static struct i2c_driver ak8975_driver
= {
571 .probe
= ak8975_probe
,
572 .remove
= __devexit_p(ak8975_remove
),
573 .id_table
= ak8975_id
,
576 static int __init
ak8975_init(void)
578 return i2c_add_driver(&ak8975_driver
);
581 static void __exit
ak8975_exit(void)
583 i2c_del_driver(&ak8975_driver
);
586 module_init(ak8975_init
);
587 module_exit(ak8975_exit
);
589 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
590 MODULE_DESCRIPTION("AK8975 magnetometer driver");
591 MODULE_LICENSE("GPL");