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>
32 #include <linux/input/ak8975.h>
38 * Register definitions, as well as various shifts and masks to get at the
39 * individual fields of the registers.
41 #define AK8975_REG_WIA 0x00
42 #define AK8975_DEVICE_ID 0x48
44 #define AK8975_REG_INFO 0x01
46 #define AK8975_REG_ST1 0x02
47 #define AK8975_REG_ST1_DRDY_SHIFT 0
48 #define AK8975_REG_ST1_DRDY_MASK (1 << AK8975_REG_ST1_DRDY_SHIFT)
50 #define AK8975_REG_HXL 0x03
51 #define AK8975_REG_HXH 0x04
52 #define AK8975_REG_HYL 0x05
53 #define AK8975_REG_HYH 0x06
54 #define AK8975_REG_HZL 0x07
55 #define AK8975_REG_HZH 0x08
56 #define AK8975_REG_ST2 0x09
57 #define AK8975_REG_ST2_DERR_SHIFT 2
58 #define AK8975_REG_ST2_DERR_MASK (1 << AK8975_REG_ST2_DERR_SHIFT)
60 #define AK8975_REG_ST2_HOFL_SHIFT 3
61 #define AK8975_REG_ST2_HOFL_MASK (1 << AK8975_REG_ST2_HOFL_SHIFT)
63 #define AK8975_REG_CNTL 0x0A
64 #define AK8975_REG_CNTL_MODE_SHIFT 0
65 #define AK8975_REG_CNTL_MODE_MASK (0xF << AK8975_REG_CNTL_MODE_SHIFT)
66 #define AK8975_REG_CNTL_MODE_POWER_DOWN 0
67 #define AK8975_REG_CNTL_MODE_ONCE 1
68 #define AK8975_REG_CNTL_MODE_SELF_TEST 8
69 #define AK8975_REG_CNTL_MODE_FUSE_ROM 0xF
71 #define AK8975_REG_RSVC 0x0B
72 #define AK8975_REG_ASTC 0x0C
73 #define AK8975_REG_TS1 0x0D
74 #define AK8975_REG_TS2 0x0E
75 #define AK8975_REG_I2CDIS 0x0F
76 #define AK8975_REG_ASAX 0x10
77 #define AK8975_REG_ASAY 0x11
78 #define AK8975_REG_ASAZ 0x12
80 #define AK8975_MAX_REGS AK8975_REG_ASAZ
83 * Miscellaneous values.
85 #define AK8975_MAX_CONVERSION_TIMEOUT 500
86 #define AK8975_CONVERSION_DONE_POLL_TIME 10
89 * Per-instance context data for the device.
92 struct i2c_client
*client
;
93 struct iio_dev
*indio_dev
;
94 struct attribute_group attrs
;
99 u8 reg_cache
[AK8975_MAX_REGS
];
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
)
115 struct ak8975_data
*data
= i2c_get_clientdata(client
);
117 regval
= data
->reg_cache
[reg
];
119 regval
|= val
<< shift
;
124 msg
.addr
= client
->addr
;
129 ret
= i2c_transfer(client
->adapter
, &msg
, 1);
131 dev_err(&client
->dev
, "Write to device fails status %x\n", ret
);
134 data
->reg_cache
[reg
] = regval
;
140 * Helper function to read a contiguous set of the I2C device's registers.
142 static int ak8975_read_data(struct i2c_client
*client
,
143 u8 reg
, u8 length
, u8
*buffer
)
145 struct i2c_msg msg
[2];
151 msg
[0].addr
= client
->addr
;
152 msg
[0].flags
= I2C_M_NOSTART
; /* set repeated start and write */
156 msg
[1].addr
= client
->addr
;
157 msg
[1].flags
= I2C_M_RD
;
161 ret
= i2c_transfer(client
->adapter
, msg
, 2);
163 dev_err(&client
->dev
, "Read from device fails\n");
171 * Perform some start-of-day setup, including reading the asa calibration
172 * values and caching them.
174 static int ak8975_setup(struct i2c_client
*client
)
176 struct ak8975_data
*data
= i2c_get_clientdata(client
);
180 /* Confirm that the device we're talking to is really an AK8975. */
181 ret
= ak8975_read_data(client
, AK8975_REG_WIA
, 1, &device_id
);
183 dev_err(&client
->dev
, "Error reading WIA\n");
186 if (device_id
!= AK8975_DEVICE_ID
) {
187 dev_err(&client
->dev
, "Device ak8975 not found\n");
191 /* Write the fused rom access mode. */
192 ret
= ak8975_write_data(client
,
194 AK8975_REG_CNTL_MODE_FUSE_ROM
,
195 AK8975_REG_CNTL_MODE_MASK
,
196 AK8975_REG_CNTL_MODE_SHIFT
);
198 dev_err(&client
->dev
, "Error in setting fuse access mode\n");
202 /* Get asa data and store in the device data. */
203 ret
= ak8975_read_data(client
, AK8975_REG_ASAX
, 3, data
->asa
);
205 dev_err(&client
->dev
, "Not able to read asa data\n");
209 /* Precalculate scale factor for each axis and
210 store in the device data. */
211 data
->raw_to_gauss
[0] = ((data
->asa
[0] + 128) * 30) >> 8;
212 data
->raw_to_gauss
[1] = ((data
->asa
[1] + 128) * 30) >> 8;
213 data
->raw_to_gauss
[2] = ((data
->asa
[2] + 128) * 30) >> 8;
219 * Shows the device's mode. 0 = off, 1 = on.
221 static ssize_t
show_mode(struct device
*dev
, struct device_attribute
*devattr
,
224 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
225 struct ak8975_data
*data
= indio_dev
->dev_data
;
227 return sprintf(buf
, "%lu\n", data
->mode
);
231 * Sets the device's mode. 0 = off, 1 = on. The device's mode must be on
232 * for the magn raw attributes to be available.
234 static ssize_t
store_mode(struct device
*dev
, struct device_attribute
*devattr
,
235 const char *buf
, size_t count
)
237 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
238 struct ak8975_data
*data
= indio_dev
->dev_data
;
239 struct i2c_client
*client
= data
->client
;
243 /* Convert mode string and do some basic sanity checking on it.
244 only 0 or 1 are valid. */
245 if (strict_strtoul(buf
, 10, &oval
))
249 dev_err(dev
, "mode value is not supported\n");
253 mutex_lock(&data
->lock
);
255 /* Write the mode to the device. */
256 if (data
->mode
!= oval
) {
257 ret
= ak8975_write_data(client
,
260 AK8975_REG_CNTL_MODE_MASK
,
261 AK8975_REG_CNTL_MODE_SHIFT
);
264 dev_err(&client
->dev
, "Error in setting mode\n");
265 mutex_unlock(&data
->lock
);
271 mutex_unlock(&data
->lock
);
277 * Emits the scale factor to bring the raw value into Gauss units.
279 * This scale factor is axis-dependent, and is derived from 3 calibration
280 * factors ASA(x), ASA(y), and ASA(z).
282 * These ASA values are read from the sensor device at start of day, and
283 * cached in the device context struct.
285 * Adjusting the flux value with the sensitivity adjustment value should be
286 * done via the following formula:
288 * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
290 * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
291 * is the resultant adjusted value.
293 * We reduce the formula to:
295 * Hadj = H * (ASA + 128) / 256
297 * H is in the range of -4096 to 4095. The magnetometer has a range of
298 * +-1229uT. To go from the raw value to uT is:
300 * HuT = H * 1229/4096, or roughly, 3/10.
302 * Since 1uT = 100 gauss, our final scale factor becomes:
304 * Hadj = H * ((ASA + 128) / 256) * 3/10 * 100
305 * Hadj = H * ((ASA + 128) * 30 / 256
307 * Since ASA doesn't change, we cache the resultant scale factor into the
308 * device context in ak8975_setup().
310 static ssize_t
show_scale(struct device
*dev
, struct device_attribute
*devattr
,
313 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
314 struct ak8975_data
*data
= indio_dev
->dev_data
;
315 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(devattr
);
317 return sprintf(buf
, "%ld\n", data
->raw_to_gauss
[this_attr
->address
]);
321 * Emits the raw flux value for the x, y, or z axis.
323 static ssize_t
show_raw(struct device
*dev
, struct device_attribute
*devattr
,
326 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
327 struct ak8975_data
*data
= indio_dev
->dev_data
;
328 struct i2c_client
*client
= data
->client
;
329 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(devattr
);
330 u32 timeout_ms
= AK8975_MAX_CONVERSION_TIMEOUT
;
336 mutex_lock(&data
->lock
);
338 if (data
->mode
== 0) {
339 dev_err(&client
->dev
, "Operating mode is in power down mode\n");
344 /* Set up the device for taking a sample. */
345 ret
= ak8975_write_data(client
,
347 AK8975_REG_CNTL_MODE_ONCE
,
348 AK8975_REG_CNTL_MODE_MASK
,
349 AK8975_REG_CNTL_MODE_SHIFT
);
351 dev_err(&client
->dev
, "Error in setting operating mode\n");
355 /* Wait for the conversion to complete. */
357 msleep(AK8975_CONVERSION_DONE_POLL_TIME
);
358 if (gpio_get_value(data
->eoc_gpio
))
360 timeout_ms
-= AK8975_CONVERSION_DONE_POLL_TIME
;
363 dev_err(&client
->dev
, "Conversion timeout happened\n");
368 ret
= ak8975_read_data(client
, AK8975_REG_ST1
, 1, &read_status
);
370 dev_err(&client
->dev
, "Error in reading ST1\n");
374 if (read_status
& AK8975_REG_ST1_DRDY_MASK
) {
375 ret
= ak8975_read_data(client
, AK8975_REG_ST2
, 1, &read_status
);
377 dev_err(&client
->dev
, "Error in reading ST2\n");
380 if (read_status
& (AK8975_REG_ST2_DERR_MASK
|
381 AK8975_REG_ST2_HOFL_MASK
)) {
382 dev_err(&client
->dev
, "ST2 status error 0x%x\n",
389 /* Read the flux value from the appropriate register
390 (the register is specified in the iio device attributes). */
391 ret
= ak8975_read_data(client
, this_attr
->address
, 2, (u8
*)&meas_reg
);
393 dev_err(&client
->dev
, "Read axis data fails\n");
397 mutex_unlock(&data
->lock
);
399 /* Endian conversion of the measured values. */
400 raw
= (s16
) (le16_to_cpu(meas_reg
));
402 /* Clamp to valid range. */
403 raw
= clamp_t(s16
, raw
, -4096, 4095);
405 return sprintf(buf
, "%d\n", raw
);
408 mutex_unlock(&data
->lock
);
412 static IIO_DEVICE_ATTR(mode
, S_IRUGO
| S_IWUSR
, show_mode
, store_mode
, 0);
413 static IIO_DEV_ATTR_MAGN_X_SCALE(S_IRUGO
, show_scale
, NULL
, 0);
414 static IIO_DEV_ATTR_MAGN_Y_SCALE(S_IRUGO
, show_scale
, NULL
, 1);
415 static IIO_DEV_ATTR_MAGN_Z_SCALE(S_IRUGO
, show_scale
, NULL
, 2);
416 static IIO_DEV_ATTR_MAGN_X(show_raw
, AK8975_REG_HXL
);
417 static IIO_DEV_ATTR_MAGN_Y(show_raw
, AK8975_REG_HYL
);
418 static IIO_DEV_ATTR_MAGN_Z(show_raw
, AK8975_REG_HZL
);
420 static struct attribute
*ak8975_attr
[] = {
421 &iio_dev_attr_mode
.dev_attr
.attr
,
422 &iio_dev_attr_magn_x_scale
.dev_attr
.attr
,
423 &iio_dev_attr_magn_y_scale
.dev_attr
.attr
,
424 &iio_dev_attr_magn_z_scale
.dev_attr
.attr
,
425 &iio_dev_attr_magn_x_raw
.dev_attr
.attr
,
426 &iio_dev_attr_magn_y_raw
.dev_attr
.attr
,
427 &iio_dev_attr_magn_z_raw
.dev_attr
.attr
,
431 static struct attribute_group ak8975_attr_group
= {
432 .attrs
= ak8975_attr
,
435 static int ak8975_probe(struct i2c_client
*client
,
436 const struct i2c_device_id
*id
)
438 struct ak8975_data
*data
;
439 struct ak8975_platform_data
*pdata
;
442 /* Allocate our device context. */
443 data
= kzalloc(sizeof(struct ak8975_data
), GFP_KERNEL
);
445 dev_err(&client
->dev
, "Memory allocation fails\n");
450 i2c_set_clientdata(client
, data
);
451 data
->client
= client
;
453 mutex_init(&data
->lock
);
455 /* Grab and set up the supplied GPIO. */
456 data
->eoc_irq
= client
->irq
;
457 pdata
= client
->dev
.platform_data
;
459 data
->eoc_gpio
= pdata
->gpio
;
461 data
->eoc_gpio
= irq_to_gpio(client
->irq
);
463 if (!data
->eoc_gpio
) {
464 dev_err(&client
->dev
, "failed, no valid GPIO\n");
469 err
= gpio_request(data
->eoc_gpio
, "ak_8975");
471 dev_err(&client
->dev
, "failed to request GPIO %d, error %d\n",
472 data
->eoc_gpio
, err
);
476 err
= gpio_direction_input(data
->eoc_gpio
);
478 dev_err(&client
->dev
, "Failed to configure input direction for"
479 " GPIO %d, error %d\n", data
->eoc_gpio
, err
);
483 /* Perform some basic start-of-day setup of the device. */
484 err
= ak8975_setup(client
);
486 dev_err(&client
->dev
, "AK8975 initialization fails\n");
490 /* Register with IIO */
491 data
->indio_dev
= iio_allocate_device();
492 if (data
->indio_dev
== NULL
) {
497 data
->indio_dev
->dev
.parent
= &client
->dev
;
498 data
->indio_dev
->attrs
= &ak8975_attr_group
;
499 data
->indio_dev
->dev_data
= (void *)(data
);
500 data
->indio_dev
->driver_module
= THIS_MODULE
;
501 data
->indio_dev
->modes
= INDIO_DIRECT_MODE
;
503 err
= iio_device_register(data
->indio_dev
);
510 iio_free_device(data
->indio_dev
);
512 gpio_free(data
->eoc_gpio
);
519 static int ak8975_remove(struct i2c_client
*client
)
521 struct ak8975_data
*data
= i2c_get_clientdata(client
);
523 iio_device_unregister(data
->indio_dev
);
524 iio_free_device(data
->indio_dev
);
526 gpio_free(data
->eoc_gpio
);
533 static const struct i2c_device_id ak8975_id
[] = {
538 MODULE_DEVICE_TABLE(i2c
, ak8975_id
);
540 static struct i2c_driver ak8975_driver
= {
544 .probe
= ak8975_probe
,
545 .remove
= __devexit_p(ak8975_remove
),
546 .id_table
= ak8975_id
,
549 static int __init
ak8975_init(void)
551 return i2c_add_driver(&ak8975_driver
);
554 static void __exit
ak8975_exit(void)
556 i2c_del_driver(&ak8975_driver
);
559 module_init(ak8975_init
);
560 module_exit(ak8975_exit
);
562 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
563 MODULE_DESCRIPTION("AK8975 magnetometer driver");
564 MODULE_LICENSE("GPL");