Merge branch 'upstream' of git://git.linux-mips.org/pub/scm/upstream-linus
[linux-btrfs-devel.git] / drivers / staging / iio / magnetometer / ak8975.c
blob33919e87e7ce53621b590deb829b422546e7ac91
1 /*
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
16 * more details.
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 "../iio.h"
34 #include "magnet.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.
90 struct ak8975_data {
91 struct i2c_client *client;
92 struct attribute_group attrs;
93 struct mutex lock;
94 u8 asa[3];
95 long raw_to_gauss[3];
96 unsigned long mode;
97 u8 reg_cache[AK8975_MAX_REGS];
98 int eoc_gpio;
99 int eoc_irq;
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)
108 u8 regval;
109 struct i2c_msg msg;
110 u8 w_data[2];
111 int ret = 0;
113 struct ak8975_data *data = i2c_get_clientdata(client);
115 regval = data->reg_cache[reg];
116 regval &= ~mask;
117 regval |= val << shift;
119 w_data[0] = reg;
120 w_data[1] = regval;
122 msg.addr = client->addr;
123 msg.flags = 0;
124 msg.len = 2;
125 msg.buf = w_data;
127 ret = i2c_transfer(client->adapter, &msg, 1);
128 if (ret < 0) {
129 dev_err(&client->dev, "Write to device fails status %x\n", ret);
130 return ret;
132 data->reg_cache[reg] = regval;
134 return 0;
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];
144 u8 w_data[2];
145 int ret;
147 w_data[0] = reg;
149 msg[0].addr = client->addr;
150 msg[0].flags = I2C_M_NOSTART; /* set repeated start and write */
151 msg[0].len = 1;
152 msg[0].buf = w_data;
154 msg[1].addr = client->addr;
155 msg[1].flags = I2C_M_RD;
156 msg[1].len = length;
157 msg[1].buf = buffer;
159 ret = i2c_transfer(client->adapter, msg, 2);
160 if (ret < 0) {
161 dev_err(&client->dev, "Read from device fails\n");
162 return ret;
165 return 0;
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);
175 u8 device_id;
176 int ret;
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);
180 if (ret < 0) {
181 dev_err(&client->dev, "Error reading WIA\n");
182 return ret;
184 if (device_id != AK8975_DEVICE_ID) {
185 dev_err(&client->dev, "Device ak8975 not found\n");
186 return -ENODEV;
189 /* Write the fused rom access mode. */
190 ret = ak8975_write_data(client,
191 AK8975_REG_CNTL,
192 AK8975_REG_CNTL_MODE_FUSE_ROM,
193 AK8975_REG_CNTL_MODE_MASK,
194 AK8975_REG_CNTL_MODE_SHIFT);
195 if (ret < 0) {
196 dev_err(&client->dev, "Error in setting fuse access mode\n");
197 return ret;
200 /* Get asa data and store in the device data. */
201 ret = ak8975_read_data(client, AK8975_REG_ASAX, 3, data->asa);
202 if (ret < 0) {
203 dev_err(&client->dev, "Not able to read asa data\n");
204 return ret;
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;
213 return 0;
217 * Shows the device's mode. 0 = off, 1 = on.
219 static ssize_t show_mode(struct device *dev, struct device_attribute *devattr,
220 char *buf)
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;
238 unsigned long oval;
239 int ret;
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))
244 return -EINVAL;
246 if (oval > 1) {
247 dev_err(dev, "mode value is not supported\n");
248 return -EINVAL;
251 mutex_lock(&data->lock);
253 /* Write the mode to the device. */
254 if (data->mode != oval) {
255 ret = ak8975_write_data(client,
256 AK8975_REG_CNTL,
257 (u8)oval,
258 AK8975_REG_CNTL_MODE_MASK,
259 AK8975_REG_CNTL_MODE_SHIFT);
261 if (ret < 0) {
262 dev_err(&client->dev, "Error in setting mode\n");
263 mutex_unlock(&data->lock);
264 return ret;
266 data->mode = oval;
269 mutex_unlock(&data->lock);
271 return count;
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,
309 char *buf)
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;
321 u8 read_status;
322 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
323 int ret;
325 /* Wait for the conversion to complete. */
326 while (timeout_ms) {
327 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
328 if (gpio_get_value(data->eoc_gpio))
329 break;
330 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
332 if (!timeout_ms) {
333 dev_err(&client->dev, "Conversion timeout happened\n");
334 return -EINVAL;
337 ret = ak8975_read_data(client, AK8975_REG_ST1, 1, &read_status);
338 if (ret < 0) {
339 dev_err(&client->dev, "Error in reading ST1\n");
340 return ret;
342 return read_status;
345 static int wait_conversion_complete_polled(struct ak8975_data *data)
347 struct i2c_client *client = data->client;
348 u8 read_status;
349 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
350 int ret;
352 /* Wait for the conversion to complete. */
353 while (timeout_ms) {
354 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
355 ret = ak8975_read_data(client, AK8975_REG_ST1, 1, &read_status);
356 if (ret < 0) {
357 dev_err(&client->dev, "Error in reading ST1\n");
358 return ret;
360 if (read_status)
361 break;
362 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
364 if (!timeout_ms) {
365 dev_err(&client->dev, "Conversion timeout happened\n");
366 return -EINVAL;
368 return read_status;
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,
375 char *buf)
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);
381 u16 meas_reg;
382 s16 raw;
383 u8 read_status;
384 int ret;
386 mutex_lock(&data->lock);
388 if (data->mode == 0) {
389 dev_err(&client->dev, "Operating mode is in power down mode\n");
390 ret = -EBUSY;
391 goto exit;
394 /* Set up the device for taking a sample. */
395 ret = ak8975_write_data(client,
396 AK8975_REG_CNTL,
397 AK8975_REG_CNTL_MODE_ONCE,
398 AK8975_REG_CNTL_MODE_MASK,
399 AK8975_REG_CNTL_MODE_SHIFT);
400 if (ret < 0) {
401 dev_err(&client->dev, "Error in setting operating mode\n");
402 goto exit;
405 /* Wait for the conversion to complete. */
406 if (data->eoc_gpio)
407 ret = wait_conversion_complete_gpio(data);
408 else
409 ret = wait_conversion_complete_polled(data);
410 if (ret < 0)
411 goto exit;
413 read_status = ret;
415 if (read_status & AK8975_REG_ST1_DRDY_MASK) {
416 ret = ak8975_read_data(client, AK8975_REG_ST2, 1, &read_status);
417 if (ret < 0) {
418 dev_err(&client->dev, "Error in reading ST2\n");
419 goto exit;
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",
424 read_status);
425 ret = -EINVAL;
426 goto exit;
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);
433 if (ret < 0) {
434 dev_err(&client->dev, "Read axis data fails\n");
435 goto exit;
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);
448 exit:
449 mutex_unlock(&data->lock);
450 return ret;
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,
469 NULL
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;
486 int eoc_gpio;
487 int err;
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
493 poll if so */
494 if (eoc_gpio > 0) {
495 err = gpio_request(eoc_gpio, "ak_8975");
496 if (err < 0) {
497 dev_err(&client->dev,
498 "failed to request GPIO %d, error %d\n",
499 eoc_gpio, err);
500 goto exit;
503 err = gpio_direction_input(eoc_gpio);
504 if (err < 0) {
505 dev_err(&client->dev,
506 "Failed to configure input direction for GPIO %d, error %d\n",
507 eoc_gpio, err);
508 goto exit_gpio;
510 } else
511 eoc_gpio = 0; /* No GPIO available */
513 /* Register with IIO */
514 indio_dev = iio_allocate_device(sizeof(*data));
515 if (indio_dev == NULL) {
516 err = -ENOMEM;
517 goto exit_gpio;
519 data = iio_priv(indio_dev);
520 /* Perform some basic start-of-day setup of the device. */
521 err = ak8975_setup(client);
522 if (err < 0) {
523 dev_err(&client->dev, "AK8975 initialization fails\n");
524 goto exit_gpio;
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);
537 if (err < 0)
538 goto exit_free_iio;
540 return 0;
542 exit_free_iio:
543 iio_free_device(indio_dev);
544 exit_gpio:
545 if (eoc_gpio)
546 gpio_free(eoc_gpio);
547 exit:
548 return err;
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);
560 if (eoc_gpio)
561 gpio_free(eoc_gpio);
563 return 0;
566 static const struct i2c_device_id ak8975_id[] = {
567 {"ak8975", 0},
571 MODULE_DEVICE_TABLE(i2c, ak8975_id);
573 static struct i2c_driver ak8975_driver = {
574 .driver = {
575 .name = "ak8975",
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");