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/interrupt.h>
28 #include <linux/err.h>
29 #include <linux/mutex.h>
30 #include <linux/delay.h>
31 #include <linux/bitops.h>
32 #include <linux/gpio.h>
33 #include <linux/of_gpio.h>
35 #include <linux/iio/iio.h>
36 #include <linux/iio/sysfs.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
87 #define AK8975_DATA_READY_TIMEOUT ((100*HZ)/1000)
90 * Per-instance context data for the device.
93 struct i2c_client *client;
94 struct attribute_group attrs;
98 u8 reg_cache[AK8975_MAX_REGS];
101 wait_queue_head_t data_ready_queue;
105 static const int ak8975_index_to_reg[] = {
106 AK8975_REG_HXL, AK8975_REG_HYL, AK8975_REG_HZL,
110 * Helper function to write to the I2C device's registers.
112 static int ak8975_write_data(struct i2c_client *client,
113 u8 reg, u8 val, u8 mask, u8 shift)
115 struct iio_dev *indio_dev = i2c_get_clientdata(client);
116 struct ak8975_data *data = iio_priv(indio_dev);
120 regval = (data->reg_cache[reg] & ~mask) | (val << shift);
121 ret = i2c_smbus_write_byte_data(client, reg, regval);
123 dev_err(&client->dev, "Write to device fails status %x\n", ret);
126 data->reg_cache[reg] = regval;
132 * Handle data ready irq
134 static irqreturn_t ak8975_irq_handler(int irq, void *data)
136 struct ak8975_data *ak8975 = data;
138 set_bit(0, &ak8975->flags);
139 wake_up(&ak8975->data_ready_queue);
145 * Install data ready interrupt handler
147 static int ak8975_setup_irq(struct ak8975_data *data)
149 struct i2c_client *client = data->client;
156 irq = gpio_to_irq(data->eoc_gpio);
158 rc = request_irq(irq, ak8975_irq_handler,
159 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
160 dev_name(&client->dev), data);
162 dev_err(&client->dev,
163 "irq %d request failed, (gpio %d): %d\n",
164 irq, data->eoc_gpio, rc);
168 init_waitqueue_head(&data->data_ready_queue);
169 clear_bit(0, &data->flags);
177 * Perform some start-of-day setup, including reading the asa calibration
178 * values and caching them.
180 static int ak8975_setup(struct i2c_client *client)
182 struct iio_dev *indio_dev = i2c_get_clientdata(client);
183 struct ak8975_data *data = iio_priv(indio_dev);
187 /* Confirm that the device we're talking to is really an AK8975. */
188 ret = i2c_smbus_read_byte_data(client, AK8975_REG_WIA);
190 dev_err(&client->dev, "Error reading WIA\n");
194 if (device_id != AK8975_DEVICE_ID) {
195 dev_err(&client->dev, "Device ak8975 not found\n");
199 /* Write the fused rom access mode. */
200 ret = ak8975_write_data(client,
202 AK8975_REG_CNTL_MODE_FUSE_ROM,
203 AK8975_REG_CNTL_MODE_MASK,
204 AK8975_REG_CNTL_MODE_SHIFT);
206 dev_err(&client->dev, "Error in setting fuse access mode\n");
210 /* Get asa data and store in the device data. */
211 ret = i2c_smbus_read_i2c_block_data(client, AK8975_REG_ASAX,
214 dev_err(&client->dev, "Not able to read asa data\n");
218 /* After reading fuse ROM data set power-down mode */
219 ret = ak8975_write_data(client,
221 AK8975_REG_CNTL_MODE_POWER_DOWN,
222 AK8975_REG_CNTL_MODE_MASK,
223 AK8975_REG_CNTL_MODE_SHIFT);
225 if (data->eoc_gpio > 0 || client->irq) {
226 ret = ak8975_setup_irq(data);
228 dev_err(&client->dev,
229 "Error setting data ready interrupt\n");
235 dev_err(&client->dev, "Error in setting power-down mode\n");
240 * Precalculate scale factor (in Gauss units) for each axis and
241 * store in the device data.
243 * This scale factor is axis-dependent, and is derived from 3 calibration
244 * factors ASA(x), ASA(y), and ASA(z).
246 * These ASA values are read from the sensor device at start of day, and
247 * cached in the device context struct.
249 * Adjusting the flux value with the sensitivity adjustment value should be
250 * done via the following formula:
252 * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
254 * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
255 * is the resultant adjusted value.
257 * We reduce the formula to:
259 * Hadj = H * (ASA + 128) / 256
261 * H is in the range of -4096 to 4095. The magnetometer has a range of
262 * +-1229uT. To go from the raw value to uT is:
264 * HuT = H * 1229/4096, or roughly, 3/10.
266 * Since 1uT = 0.01 gauss, our final scale factor becomes:
268 * Hadj = H * ((ASA + 128) / 256) * 3/10 * 100
269 * Hadj = H * ((ASA + 128) * 30 / 256
271 * Since ASA doesn't change, we cache the resultant scale factor into the
272 * device context in ak8975_setup().
274 data->raw_to_gauss[0] = ((data->asa[0] + 128) * 30) >> 8;
275 data->raw_to_gauss[1] = ((data->asa[1] + 128) * 30) >> 8;
276 data->raw_to_gauss[2] = ((data->asa[2] + 128) * 30) >> 8;
281 static int wait_conversion_complete_gpio(struct ak8975_data *data)
283 struct i2c_client *client = data->client;
284 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
287 /* Wait for the conversion to complete. */
289 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
290 if (gpio_get_value(data->eoc_gpio))
292 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
295 dev_err(&client->dev, "Conversion timeout happened\n");
299 ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST1);
301 dev_err(&client->dev, "Error in reading ST1\n");
306 static int wait_conversion_complete_polled(struct ak8975_data *data)
308 struct i2c_client *client = data->client;
310 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
313 /* Wait for the conversion to complete. */
315 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
316 ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST1);
318 dev_err(&client->dev, "Error in reading ST1\n");
324 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
327 dev_err(&client->dev, "Conversion timeout happened\n");
334 /* Returns 0 if the end of conversion interrupt occured or -ETIME otherwise */
335 static int wait_conversion_complete_interrupt(struct ak8975_data *data)
339 ret = wait_event_timeout(data->data_ready_queue,
340 test_bit(0, &data->flags),
341 AK8975_DATA_READY_TIMEOUT);
342 clear_bit(0, &data->flags);
344 return ret > 0 ? 0 : -ETIME;
348 * Emits the raw flux value for the x, y, or z axis.
350 static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
352 struct ak8975_data *data = iio_priv(indio_dev);
353 struct i2c_client *client = data->client;
358 mutex_lock(&data->lock);
360 /* Set up the device for taking a sample. */
361 ret = ak8975_write_data(client,
363 AK8975_REG_CNTL_MODE_ONCE,
364 AK8975_REG_CNTL_MODE_MASK,
365 AK8975_REG_CNTL_MODE_SHIFT);
367 dev_err(&client->dev, "Error in setting operating mode\n");
371 /* Wait for the conversion to complete. */
373 ret = wait_conversion_complete_interrupt(data);
374 else if (gpio_is_valid(data->eoc_gpio))
375 ret = wait_conversion_complete_gpio(data);
377 ret = wait_conversion_complete_polled(data);
381 /* This will be executed only for non-interrupt based waiting case */
382 if (ret & AK8975_REG_ST1_DRDY_MASK) {
383 ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST2);
385 dev_err(&client->dev, "Error in reading ST2\n");
388 if (ret & (AK8975_REG_ST2_DERR_MASK |
389 AK8975_REG_ST2_HOFL_MASK)) {
390 dev_err(&client->dev, "ST2 status error 0x%x\n", ret);
396 /* Read the flux value from the appropriate register
397 (the register is specified in the iio device attributes). */
398 ret = i2c_smbus_read_word_data(client, ak8975_index_to_reg[index]);
400 dev_err(&client->dev, "Read axis data fails\n");
405 mutex_unlock(&data->lock);
407 /* Endian conversion of the measured values. */
408 raw = (s16) (le16_to_cpu(meas_reg));
410 /* Clamp to valid range. */
411 raw = clamp_t(s16, raw, -4096, 4095);
416 mutex_unlock(&data->lock);
420 static int ak8975_read_raw(struct iio_dev *indio_dev,
421 struct iio_chan_spec const *chan,
425 struct ak8975_data *data = iio_priv(indio_dev);
428 case IIO_CHAN_INFO_RAW:
429 return ak8975_read_axis(indio_dev, chan->address, val);
430 case IIO_CHAN_INFO_SCALE:
431 *val = data->raw_to_gauss[chan->address];
437 #define AK8975_CHANNEL(axis, index) \
441 .channel2 = IIO_MOD_##axis, \
442 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
443 BIT(IIO_CHAN_INFO_SCALE), \
447 static const struct iio_chan_spec ak8975_channels[] = {
448 AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2),
451 static const struct iio_info ak8975_info = {
452 .read_raw = &ak8975_read_raw,
453 .driver_module = THIS_MODULE,
456 static int ak8975_probe(struct i2c_client *client,
457 const struct i2c_device_id *id)
459 struct ak8975_data *data;
460 struct iio_dev *indio_dev;
464 /* Grab and set up the supplied GPIO. */
465 if (client->dev.platform_data)
466 eoc_gpio = *(int *)(client->dev.platform_data);
467 else if (client->dev.of_node)
468 eoc_gpio = of_get_gpio(client->dev.of_node, 0);
472 if (eoc_gpio == -EPROBE_DEFER)
473 return -EPROBE_DEFER;
475 /* We may not have a GPIO based IRQ to scan, that is fine, we will
477 if (gpio_is_valid(eoc_gpio)) {
478 err = gpio_request_one(eoc_gpio, GPIOF_IN, "ak_8975");
480 dev_err(&client->dev,
481 "failed to request GPIO %d, error %d\n",
487 /* Register with IIO */
488 indio_dev = iio_device_alloc(sizeof(*data));
489 if (indio_dev == NULL) {
493 data = iio_priv(indio_dev);
494 i2c_set_clientdata(client, indio_dev);
496 data->client = client;
497 data->eoc_gpio = eoc_gpio;
500 /* Perform some basic start-of-day setup of the device. */
501 err = ak8975_setup(client);
503 dev_err(&client->dev, "AK8975 initialization fails\n");
507 data->client = client;
508 mutex_init(&data->lock);
509 data->eoc_gpio = eoc_gpio;
510 indio_dev->dev.parent = &client->dev;
511 indio_dev->channels = ak8975_channels;
512 indio_dev->num_channels = ARRAY_SIZE(ak8975_channels);
513 indio_dev->info = &ak8975_info;
514 indio_dev->modes = INDIO_DIRECT_MODE;
516 err = iio_device_register(indio_dev);
523 iio_device_free(indio_dev);
525 free_irq(data->eoc_irq, data);
527 if (gpio_is_valid(eoc_gpio))
533 static int ak8975_remove(struct i2c_client *client)
535 struct iio_dev *indio_dev = i2c_get_clientdata(client);
536 struct ak8975_data *data = iio_priv(indio_dev);
538 iio_device_unregister(indio_dev);
541 free_irq(data->eoc_irq, data);
543 if (gpio_is_valid(data->eoc_gpio))
544 gpio_free(data->eoc_gpio);
546 iio_device_free(indio_dev);
551 static const struct i2c_device_id ak8975_id[] = {
556 MODULE_DEVICE_TABLE(i2c, ak8975_id);
558 static const struct of_device_id ak8975_of_match[] = {
559 { .compatible = "asahi-kasei,ak8975", },
560 { .compatible = "ak8975", },
563 MODULE_DEVICE_TABLE(of, ak8975_of_match);
565 static struct i2c_driver ak8975_driver = {
568 .of_match_table = ak8975_of_match,
570 .probe = ak8975_probe,
571 .remove = ak8975_remove,
572 .id_table = ak8975_id,
574 module_i2c_driver(ak8975_driver);
576 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
577 MODULE_DESCRIPTION("AK8975 magnetometer driver");
578 MODULE_LICENSE("GPL");