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>
37 * Register definitions, as well as various shifts and masks to get at the
38 * individual fields of the registers.
40 #define AK8975_REG_WIA 0x00
41 #define AK8975_DEVICE_ID 0x48
43 #define AK8975_REG_INFO 0x01
45 #define AK8975_REG_ST1 0x02
46 #define AK8975_REG_ST1_DRDY_SHIFT 0
47 #define AK8975_REG_ST1_DRDY_MASK (1 << AK8975_REG_ST1_DRDY_SHIFT)
49 #define AK8975_REG_HXL 0x03
50 #define AK8975_REG_HXH 0x04
51 #define AK8975_REG_HYL 0x05
52 #define AK8975_REG_HYH 0x06
53 #define AK8975_REG_HZL 0x07
54 #define AK8975_REG_HZH 0x08
55 #define AK8975_REG_ST2 0x09
56 #define AK8975_REG_ST2_DERR_SHIFT 2
57 #define AK8975_REG_ST2_DERR_MASK (1 << AK8975_REG_ST2_DERR_SHIFT)
59 #define AK8975_REG_ST2_HOFL_SHIFT 3
60 #define AK8975_REG_ST2_HOFL_MASK (1 << AK8975_REG_ST2_HOFL_SHIFT)
62 #define AK8975_REG_CNTL 0x0A
63 #define AK8975_REG_CNTL_MODE_SHIFT 0
64 #define AK8975_REG_CNTL_MODE_MASK (0xF << AK8975_REG_CNTL_MODE_SHIFT)
65 #define AK8975_REG_CNTL_MODE_POWER_DOWN 0
66 #define AK8975_REG_CNTL_MODE_ONCE 1
67 #define AK8975_REG_CNTL_MODE_SELF_TEST 8
68 #define AK8975_REG_CNTL_MODE_FUSE_ROM 0xF
70 #define AK8975_REG_RSVC 0x0B
71 #define AK8975_REG_ASTC 0x0C
72 #define AK8975_REG_TS1 0x0D
73 #define AK8975_REG_TS2 0x0E
74 #define AK8975_REG_I2CDIS 0x0F
75 #define AK8975_REG_ASAX 0x10
76 #define AK8975_REG_ASAY 0x11
77 #define AK8975_REG_ASAZ 0x12
79 #define AK8975_MAX_REGS AK8975_REG_ASAZ
82 * Miscellaneous values.
84 #define AK8975_MAX_CONVERSION_TIMEOUT 500
85 #define AK8975_CONVERSION_DONE_POLL_TIME 10
88 * Per-instance context data for the device.
91 struct i2c_client
*client
;
92 struct attribute_group attrs
;
97 u8 reg_cache
[AK8975_MAX_REGS
];
103 * Helper function to write to the I2C device's registers.
105 static int ak8975_write_data(struct i2c_client
*client
,
106 u8 reg
, u8 val
, u8 mask
, u8 shift
)
113 struct ak8975_data
*data
= i2c_get_clientdata(client
);
115 regval
= data
->reg_cache
[reg
];
117 regval
|= val
<< shift
;
122 msg
.addr
= client
->addr
;
127 ret
= i2c_transfer(client
->adapter
, &msg
, 1);
129 dev_err(&client
->dev
, "Write to device fails status %x\n", ret
);
132 data
->reg_cache
[reg
] = regval
;
138 * Helper function to read a contiguous set of the I2C device's registers.
140 static int ak8975_read_data(struct i2c_client
*client
,
141 u8 reg
, u8 length
, u8
*buffer
)
143 struct i2c_msg msg
[2];
149 msg
[0].addr
= client
->addr
;
150 msg
[0].flags
= I2C_M_NOSTART
; /* set repeated start and write */
154 msg
[1].addr
= client
->addr
;
155 msg
[1].flags
= I2C_M_RD
;
159 ret
= i2c_transfer(client
->adapter
, msg
, 2);
161 dev_err(&client
->dev
, "Read from device fails\n");
169 * Perform some start-of-day setup, including reading the asa calibration
170 * values and caching them.
172 static int ak8975_setup(struct i2c_client
*client
)
174 struct ak8975_data
*data
= i2c_get_clientdata(client
);
178 /* Confirm that the device we're talking to is really an AK8975. */
179 ret
= ak8975_read_data(client
, AK8975_REG_WIA
, 1, &device_id
);
181 dev_err(&client
->dev
, "Error reading WIA\n");
184 if (device_id
!= AK8975_DEVICE_ID
) {
185 dev_err(&client
->dev
, "Device ak8975 not found\n");
189 /* Write the fused rom access mode. */
190 ret
= ak8975_write_data(client
,
192 AK8975_REG_CNTL_MODE_FUSE_ROM
,
193 AK8975_REG_CNTL_MODE_MASK
,
194 AK8975_REG_CNTL_MODE_SHIFT
);
196 dev_err(&client
->dev
, "Error in setting fuse access mode\n");
200 /* Get asa data and store in the device data. */
201 ret
= ak8975_read_data(client
, AK8975_REG_ASAX
, 3, data
->asa
);
203 dev_err(&client
->dev
, "Not able to read asa data\n");
207 /* Precalculate scale factor for each axis and
208 store in the device data. */
209 data
->raw_to_gauss
[0] = ((data
->asa
[0] + 128) * 30) >> 8;
210 data
->raw_to_gauss
[1] = ((data
->asa
[1] + 128) * 30) >> 8;
211 data
->raw_to_gauss
[2] = ((data
->asa
[2] + 128) * 30) >> 8;
217 * Shows the device's mode. 0 = off, 1 = on.
219 static ssize_t
show_mode(struct device
*dev
, struct device_attribute
*devattr
,
222 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
223 struct ak8975_data
*data
= iio_priv(indio_dev
);
225 return sprintf(buf
, "%lu\n", data
->mode
);
229 * Sets the device's mode. 0 = off, 1 = on. The device's mode must be on
230 * for the magn raw attributes to be available.
232 static ssize_t
store_mode(struct device
*dev
, struct device_attribute
*devattr
,
233 const char *buf
, size_t count
)
235 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
236 struct ak8975_data
*data
= iio_priv(indio_dev
);
237 struct i2c_client
*client
= data
->client
;
241 /* Convert mode string and do some basic sanity checking on it.
242 only 0 or 1 are valid. */
243 if (strict_strtoul(buf
, 10, &oval
))
247 dev_err(dev
, "mode value is not supported\n");
251 mutex_lock(&data
->lock
);
253 /* Write the mode to the device. */
254 if (data
->mode
!= oval
) {
255 ret
= ak8975_write_data(client
,
258 AK8975_REG_CNTL_MODE_MASK
,
259 AK8975_REG_CNTL_MODE_SHIFT
);
262 dev_err(&client
->dev
, "Error in setting mode\n");
263 mutex_unlock(&data
->lock
);
269 mutex_unlock(&data
->lock
);
275 * Emits the scale factor to bring the raw value into Gauss units.
277 * This scale factor is axis-dependent, and is derived from 3 calibration
278 * factors ASA(x), ASA(y), and ASA(z).
280 * These ASA values are read from the sensor device at start of day, and
281 * cached in the device context struct.
283 * Adjusting the flux value with the sensitivity adjustment value should be
284 * done via the following formula:
286 * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
288 * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
289 * is the resultant adjusted value.
291 * We reduce the formula to:
293 * Hadj = H * (ASA + 128) / 256
295 * H is in the range of -4096 to 4095. The magnetometer has a range of
296 * +-1229uT. To go from the raw value to uT is:
298 * HuT = H * 1229/4096, or roughly, 3/10.
300 * Since 1uT = 100 gauss, our final scale factor becomes:
302 * Hadj = H * ((ASA + 128) / 256) * 3/10 * 100
303 * Hadj = H * ((ASA + 128) * 30 / 256
305 * Since ASA doesn't change, we cache the resultant scale factor into the
306 * device context in ak8975_setup().
308 static ssize_t
show_scale(struct device
*dev
, struct device_attribute
*devattr
,
311 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
312 struct ak8975_data
*data
= iio_priv(indio_dev
);
313 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(devattr
);
315 return sprintf(buf
, "%ld\n", data
->raw_to_gauss
[this_attr
->address
]);
318 static int wait_conversion_complete_gpio(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 if (gpio_get_value(data
->eoc_gpio
))
330 timeout_ms
-= AK8975_CONVERSION_DONE_POLL_TIME
;
333 dev_err(&client
->dev
, "Conversion timeout happened\n");
337 ret
= ak8975_read_data(client
, AK8975_REG_ST1
, 1, &read_status
);
339 dev_err(&client
->dev
, "Error in reading ST1\n");
345 static int wait_conversion_complete_polled(struct ak8975_data
*data
)
347 struct i2c_client
*client
= data
->client
;
349 u32 timeout_ms
= AK8975_MAX_CONVERSION_TIMEOUT
;
352 /* Wait for the conversion to complete. */
354 msleep(AK8975_CONVERSION_DONE_POLL_TIME
);
355 ret
= ak8975_read_data(client
, AK8975_REG_ST1
, 1, &read_status
);
357 dev_err(&client
->dev
, "Error in reading ST1\n");
362 timeout_ms
-= AK8975_CONVERSION_DONE_POLL_TIME
;
365 dev_err(&client
->dev
, "Conversion timeout happened\n");
372 * Emits the raw flux value for the x, y, or z axis.
374 static ssize_t
show_raw(struct device
*dev
, struct device_attribute
*devattr
,
377 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
378 struct ak8975_data
*data
= iio_priv(indio_dev
);
379 struct i2c_client
*client
= data
->client
;
380 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(devattr
);
386 mutex_lock(&data
->lock
);
388 if (data
->mode
== 0) {
389 dev_err(&client
->dev
, "Operating mode is in power down mode\n");
394 /* Set up the device for taking a sample. */
395 ret
= ak8975_write_data(client
,
397 AK8975_REG_CNTL_MODE_ONCE
,
398 AK8975_REG_CNTL_MODE_MASK
,
399 AK8975_REG_CNTL_MODE_SHIFT
);
401 dev_err(&client
->dev
, "Error in setting operating mode\n");
405 /* Wait for the conversion to complete. */
407 ret
= wait_conversion_complete_gpio(data
);
409 ret
= wait_conversion_complete_polled(data
);
415 if (read_status
& AK8975_REG_ST1_DRDY_MASK
) {
416 ret
= ak8975_read_data(client
, AK8975_REG_ST2
, 1, &read_status
);
418 dev_err(&client
->dev
, "Error in reading ST2\n");
421 if (read_status
& (AK8975_REG_ST2_DERR_MASK
|
422 AK8975_REG_ST2_HOFL_MASK
)) {
423 dev_err(&client
->dev
, "ST2 status error 0x%x\n",
430 /* Read the flux value from the appropriate register
431 (the register is specified in the iio device attributes). */
432 ret
= ak8975_read_data(client
, this_attr
->address
, 2, (u8
*)&meas_reg
);
434 dev_err(&client
->dev
, "Read axis data fails\n");
438 mutex_unlock(&data
->lock
);
440 /* Endian conversion of the measured values. */
441 raw
= (s16
) (le16_to_cpu(meas_reg
));
443 /* Clamp to valid range. */
444 raw
= clamp_t(s16
, raw
, -4096, 4095);
446 return sprintf(buf
, "%d\n", raw
);
449 mutex_unlock(&data
->lock
);
453 static IIO_DEVICE_ATTR(mode
, S_IRUGO
| S_IWUSR
, show_mode
, store_mode
, 0);
454 static IIO_DEV_ATTR_MAGN_X_SCALE(S_IRUGO
, show_scale
, NULL
, 0);
455 static IIO_DEV_ATTR_MAGN_Y_SCALE(S_IRUGO
, show_scale
, NULL
, 1);
456 static IIO_DEV_ATTR_MAGN_Z_SCALE(S_IRUGO
, show_scale
, NULL
, 2);
457 static IIO_DEV_ATTR_MAGN_X(show_raw
, AK8975_REG_HXL
);
458 static IIO_DEV_ATTR_MAGN_Y(show_raw
, AK8975_REG_HYL
);
459 static IIO_DEV_ATTR_MAGN_Z(show_raw
, AK8975_REG_HZL
);
461 static struct attribute
*ak8975_attr
[] = {
462 &iio_dev_attr_mode
.dev_attr
.attr
,
463 &iio_dev_attr_magn_x_scale
.dev_attr
.attr
,
464 &iio_dev_attr_magn_y_scale
.dev_attr
.attr
,
465 &iio_dev_attr_magn_z_scale
.dev_attr
.attr
,
466 &iio_dev_attr_magn_x_raw
.dev_attr
.attr
,
467 &iio_dev_attr_magn_y_raw
.dev_attr
.attr
,
468 &iio_dev_attr_magn_z_raw
.dev_attr
.attr
,
472 static struct attribute_group ak8975_attr_group
= {
473 .attrs
= ak8975_attr
,
476 static const struct iio_info ak8975_info
= {
477 .attrs
= &ak8975_attr_group
,
478 .driver_module
= THIS_MODULE
,
481 static int ak8975_probe(struct i2c_client
*client
,
482 const struct i2c_device_id
*id
)
484 struct ak8975_data
*data
;
485 struct iio_dev
*indio_dev
;
489 /* Grab and set up the supplied GPIO. */
490 eoc_gpio
= irq_to_gpio(client
->irq
);
492 /* We may not have a GPIO based IRQ to scan, that is fine, we will
495 err
= gpio_request(eoc_gpio
, "ak_8975");
497 dev_err(&client
->dev
,
498 "failed to request GPIO %d, error %d\n",
503 err
= gpio_direction_input(eoc_gpio
);
505 dev_err(&client
->dev
,
506 "Failed to configure input direction for GPIO %d, error %d\n",
511 eoc_gpio
= 0; /* No GPIO available */
513 /* Register with IIO */
514 indio_dev
= iio_allocate_device(sizeof(*data
));
515 if (indio_dev
== NULL
) {
519 data
= iio_priv(indio_dev
);
520 /* Perform some basic start-of-day setup of the device. */
521 err
= ak8975_setup(client
);
523 dev_err(&client
->dev
, "AK8975 initialization fails\n");
527 i2c_set_clientdata(client
, indio_dev
);
528 data
->client
= client
;
529 mutex_init(&data
->lock
);
530 data
->eoc_irq
= client
->irq
;
531 data
->eoc_gpio
= eoc_gpio
;
532 indio_dev
->dev
.parent
= &client
->dev
;
533 indio_dev
->info
= &ak8975_info
;
534 indio_dev
->modes
= INDIO_DIRECT_MODE
;
536 err
= iio_device_register(indio_dev
);
543 iio_free_device(indio_dev
);
551 static int ak8975_remove(struct i2c_client
*client
)
553 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
554 struct ak8975_data
*data
= iio_priv(indio_dev
);
555 int eoc_gpio
= data
->eoc_gpio
;
557 iio_device_unregister(indio_dev
);
558 iio_free_device(indio_dev
);
566 static const struct i2c_device_id ak8975_id
[] = {
571 MODULE_DEVICE_TABLE(i2c
, ak8975_id
);
573 static struct i2c_driver ak8975_driver
= {
577 .probe
= ak8975_probe
,
578 .remove
= __devexit_p(ak8975_remove
),
579 .id_table
= ak8975_id
,
582 static int __init
ak8975_init(void)
584 return i2c_add_driver(&ak8975_driver
);
587 static void __exit
ak8975_exit(void)
589 i2c_del_driver(&ak8975_driver
);
592 module_init(ak8975_init
);
593 module_exit(ak8975_exit
);
595 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
596 MODULE_DESCRIPTION("AK8975 magnetometer driver");
597 MODULE_LICENSE("GPL");