Realtek cr: Add autosuspend function.
[zen-stable.git] / drivers / staging / iio / magnetometer / ak8975.c
blob700f96c7027394f9c12b9960f2431886e153200f
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 iio_dev *indio_dev;
93 struct attribute_group attrs;
94 struct mutex lock;
95 u8 asa[3];
96 long raw_to_gauss[3];
97 unsigned long mode;
98 u8 reg_cache[AK8975_MAX_REGS];
99 int eoc_gpio;
100 int eoc_irq;
104 * Helper function to write to the I2C device's registers.
106 static int ak8975_write_data(struct i2c_client *client,
107 u8 reg, u8 val, u8 mask, u8 shift)
109 u8 regval;
110 struct i2c_msg msg;
111 u8 w_data[2];
112 int ret = 0;
114 struct ak8975_data *data = i2c_get_clientdata(client);
116 regval = data->reg_cache[reg];
117 regval &= ~mask;
118 regval |= val << shift;
120 w_data[0] = reg;
121 w_data[1] = regval;
123 msg.addr = client->addr;
124 msg.flags = 0;
125 msg.len = 2;
126 msg.buf = w_data;
128 ret = i2c_transfer(client->adapter, &msg, 1);
129 if (ret < 0) {
130 dev_err(&client->dev, "Write to device fails status %x\n", ret);
131 return ret;
133 data->reg_cache[reg] = regval;
135 return 0;
139 * Helper function to read a contiguous set of the I2C device's registers.
141 static int ak8975_read_data(struct i2c_client *client,
142 u8 reg, u8 length, u8 *buffer)
144 struct i2c_msg msg[2];
145 u8 w_data[2];
146 int ret;
148 w_data[0] = reg;
150 msg[0].addr = client->addr;
151 msg[0].flags = I2C_M_NOSTART; /* set repeated start and write */
152 msg[0].len = 1;
153 msg[0].buf = w_data;
155 msg[1].addr = client->addr;
156 msg[1].flags = I2C_M_RD;
157 msg[1].len = length;
158 msg[1].buf = buffer;
160 ret = i2c_transfer(client->adapter, msg, 2);
161 if (ret < 0) {
162 dev_err(&client->dev, "Read from device fails\n");
163 return ret;
166 return 0;
170 * Perform some start-of-day setup, including reading the asa calibration
171 * values and caching them.
173 static int ak8975_setup(struct i2c_client *client)
175 struct ak8975_data *data = i2c_get_clientdata(client);
176 u8 device_id;
177 int ret;
179 /* Confirm that the device we're talking to is really an AK8975. */
180 ret = ak8975_read_data(client, AK8975_REG_WIA, 1, &device_id);
181 if (ret < 0) {
182 dev_err(&client->dev, "Error reading WIA\n");
183 return ret;
185 if (device_id != AK8975_DEVICE_ID) {
186 dev_err(&client->dev, "Device ak8975 not found\n");
187 return -ENODEV;
190 /* Write the fused rom access mode. */
191 ret = ak8975_write_data(client,
192 AK8975_REG_CNTL,
193 AK8975_REG_CNTL_MODE_FUSE_ROM,
194 AK8975_REG_CNTL_MODE_MASK,
195 AK8975_REG_CNTL_MODE_SHIFT);
196 if (ret < 0) {
197 dev_err(&client->dev, "Error in setting fuse access mode\n");
198 return ret;
201 /* Get asa data and store in the device data. */
202 ret = ak8975_read_data(client, AK8975_REG_ASAX, 3, data->asa);
203 if (ret < 0) {
204 dev_err(&client->dev, "Not able to read asa data\n");
205 return ret;
208 /* Precalculate scale factor for each axis and
209 store in the device data. */
210 data->raw_to_gauss[0] = ((data->asa[0] + 128) * 30) >> 8;
211 data->raw_to_gauss[1] = ((data->asa[1] + 128) * 30) >> 8;
212 data->raw_to_gauss[2] = ((data->asa[2] + 128) * 30) >> 8;
214 return 0;
218 * Shows the device's mode. 0 = off, 1 = on.
220 static ssize_t show_mode(struct device *dev, struct device_attribute *devattr,
221 char *buf)
223 struct iio_dev *indio_dev = dev_get_drvdata(dev);
224 struct ak8975_data *data = indio_dev->dev_data;
226 return sprintf(buf, "%lu\n", data->mode);
230 * Sets the device's mode. 0 = off, 1 = on. The device's mode must be on
231 * for the magn raw attributes to be available.
233 static ssize_t store_mode(struct device *dev, struct device_attribute *devattr,
234 const char *buf, size_t count)
236 struct iio_dev *indio_dev = dev_get_drvdata(dev);
237 struct ak8975_data *data = indio_dev->dev_data;
238 struct i2c_client *client = data->client;
239 unsigned long oval;
240 int ret;
242 /* Convert mode string and do some basic sanity checking on it.
243 only 0 or 1 are valid. */
244 if (strict_strtoul(buf, 10, &oval))
245 return -EINVAL;
247 if (oval > 1) {
248 dev_err(dev, "mode value is not supported\n");
249 return -EINVAL;
252 mutex_lock(&data->lock);
254 /* Write the mode to the device. */
255 if (data->mode != oval) {
256 ret = ak8975_write_data(client,
257 AK8975_REG_CNTL,
258 (u8)oval,
259 AK8975_REG_CNTL_MODE_MASK,
260 AK8975_REG_CNTL_MODE_SHIFT);
262 if (ret < 0) {
263 dev_err(&client->dev, "Error in setting mode\n");
264 mutex_unlock(&data->lock);
265 return ret;
267 data->mode = oval;
270 mutex_unlock(&data->lock);
272 return count;
276 * Emits the scale factor to bring the raw value into Gauss units.
278 * This scale factor is axis-dependent, and is derived from 3 calibration
279 * factors ASA(x), ASA(y), and ASA(z).
281 * These ASA values are read from the sensor device at start of day, and
282 * cached in the device context struct.
284 * Adjusting the flux value with the sensitivity adjustment value should be
285 * done via the following formula:
287 * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
289 * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
290 * is the resultant adjusted value.
292 * We reduce the formula to:
294 * Hadj = H * (ASA + 128) / 256
296 * H is in the range of -4096 to 4095. The magnetometer has a range of
297 * +-1229uT. To go from the raw value to uT is:
299 * HuT = H * 1229/4096, or roughly, 3/10.
301 * Since 1uT = 100 gauss, our final scale factor becomes:
303 * Hadj = H * ((ASA + 128) / 256) * 3/10 * 100
304 * Hadj = H * ((ASA + 128) * 30 / 256
306 * Since ASA doesn't change, we cache the resultant scale factor into the
307 * device context in ak8975_setup().
309 static ssize_t show_scale(struct device *dev, struct device_attribute *devattr,
310 char *buf)
312 struct iio_dev *indio_dev = dev_get_drvdata(dev);
313 struct ak8975_data *data = indio_dev->dev_data;
314 struct iio_dev_attr *this_attr = to_iio_dev_attr(devattr);
316 return sprintf(buf, "%ld\n", data->raw_to_gauss[this_attr->address]);
319 static int wait_conversion_complete_gpio(struct ak8975_data *data)
321 struct i2c_client *client = data->client;
322 u8 read_status;
323 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
324 int ret;
326 /* Wait for the conversion to complete. */
327 while (timeout_ms) {
328 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
329 if (gpio_get_value(data->eoc_gpio))
330 break;
331 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
333 if (!timeout_ms) {
334 dev_err(&client->dev, "Conversion timeout happened\n");
335 return -EINVAL;
338 ret = ak8975_read_data(client, AK8975_REG_ST1, 1, &read_status);
339 if (ret < 0) {
340 dev_err(&client->dev, "Error in reading ST1\n");
341 return ret;
343 return read_status;
346 static int wait_conversion_complete_polled(struct ak8975_data *data)
348 struct i2c_client *client = data->client;
349 u8 read_status;
350 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
351 int ret;
353 /* Wait for the conversion to complete. */
354 while (timeout_ms) {
355 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
356 ret = ak8975_read_data(client, AK8975_REG_ST1, 1, &read_status);
357 if (ret < 0) {
358 dev_err(&client->dev, "Error in reading ST1\n");
359 return ret;
361 if (read_status)
362 break;
363 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
365 if (!timeout_ms) {
366 dev_err(&client->dev, "Conversion timeout happened\n");
367 return -EINVAL;
369 return read_status;
373 * Emits the raw flux value for the x, y, or z axis.
375 static ssize_t show_raw(struct device *dev, struct device_attribute *devattr,
376 char *buf)
378 struct iio_dev *indio_dev = dev_get_drvdata(dev);
379 struct ak8975_data *data = indio_dev->dev_data;
380 struct i2c_client *client = data->client;
381 struct iio_dev_attr *this_attr = to_iio_dev_attr(devattr);
382 u16 meas_reg;
383 s16 raw;
384 u8 read_status;
385 int ret;
387 mutex_lock(&data->lock);
389 if (data->mode == 0) {
390 dev_err(&client->dev, "Operating mode is in power down mode\n");
391 ret = -EBUSY;
392 goto exit;
395 /* Set up the device for taking a sample. */
396 ret = ak8975_write_data(client,
397 AK8975_REG_CNTL,
398 AK8975_REG_CNTL_MODE_ONCE,
399 AK8975_REG_CNTL_MODE_MASK,
400 AK8975_REG_CNTL_MODE_SHIFT);
401 if (ret < 0) {
402 dev_err(&client->dev, "Error in setting operating mode\n");
403 goto exit;
406 /* Wait for the conversion to complete. */
407 if (data->eoc_gpio)
408 ret = wait_conversion_complete_gpio(data);
409 else
410 ret = wait_conversion_complete_polled(data);
411 if (ret < 0)
412 goto exit;
414 read_status = ret;
416 if (read_status & AK8975_REG_ST1_DRDY_MASK) {
417 ret = ak8975_read_data(client, AK8975_REG_ST2, 1, &read_status);
418 if (ret < 0) {
419 dev_err(&client->dev, "Error in reading ST2\n");
420 goto exit;
422 if (read_status & (AK8975_REG_ST2_DERR_MASK |
423 AK8975_REG_ST2_HOFL_MASK)) {
424 dev_err(&client->dev, "ST2 status error 0x%x\n",
425 read_status);
426 ret = -EINVAL;
427 goto exit;
431 /* Read the flux value from the appropriate register
432 (the register is specified in the iio device attributes). */
433 ret = ak8975_read_data(client, this_attr->address, 2, (u8 *)&meas_reg);
434 if (ret < 0) {
435 dev_err(&client->dev, "Read axis data fails\n");
436 goto exit;
439 mutex_unlock(&data->lock);
441 /* Endian conversion of the measured values. */
442 raw = (s16) (le16_to_cpu(meas_reg));
444 /* Clamp to valid range. */
445 raw = clamp_t(s16, raw, -4096, 4095);
447 return sprintf(buf, "%d\n", raw);
449 exit:
450 mutex_unlock(&data->lock);
451 return ret;
454 static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, show_mode, store_mode, 0);
455 static IIO_DEV_ATTR_MAGN_X_SCALE(S_IRUGO, show_scale, NULL, 0);
456 static IIO_DEV_ATTR_MAGN_Y_SCALE(S_IRUGO, show_scale, NULL, 1);
457 static IIO_DEV_ATTR_MAGN_Z_SCALE(S_IRUGO, show_scale, NULL, 2);
458 static IIO_DEV_ATTR_MAGN_X(show_raw, AK8975_REG_HXL);
459 static IIO_DEV_ATTR_MAGN_Y(show_raw, AK8975_REG_HYL);
460 static IIO_DEV_ATTR_MAGN_Z(show_raw, AK8975_REG_HZL);
462 static struct attribute *ak8975_attr[] = {
463 &iio_dev_attr_mode.dev_attr.attr,
464 &iio_dev_attr_magn_x_scale.dev_attr.attr,
465 &iio_dev_attr_magn_y_scale.dev_attr.attr,
466 &iio_dev_attr_magn_z_scale.dev_attr.attr,
467 &iio_dev_attr_magn_x_raw.dev_attr.attr,
468 &iio_dev_attr_magn_y_raw.dev_attr.attr,
469 &iio_dev_attr_magn_z_raw.dev_attr.attr,
470 NULL
473 static struct attribute_group ak8975_attr_group = {
474 .attrs = ak8975_attr,
477 static const struct iio_info ak8975_info = {
478 .attrs = &ak8975_attr_group,
479 .driver_module = THIS_MODULE,
482 static int ak8975_probe(struct i2c_client *client,
483 const struct i2c_device_id *id)
485 struct ak8975_data *data;
486 int err;
488 /* Allocate our device context. */
489 data = kzalloc(sizeof(struct ak8975_data), GFP_KERNEL);
490 if (!data) {
491 dev_err(&client->dev, "Memory allocation fails\n");
492 err = -ENOMEM;
493 goto exit;
496 i2c_set_clientdata(client, data);
497 data->client = client;
499 mutex_init(&data->lock);
501 /* Grab and set up the supplied GPIO. */
502 data->eoc_irq = client->irq;
503 data->eoc_gpio = irq_to_gpio(client->irq);
505 /* We may not have a GPIO based IRQ to scan, that is fine, we will
506 poll if so */
507 if (data->eoc_gpio > 0) {
508 err = gpio_request(data->eoc_gpio, "ak_8975");
509 if (err < 0) {
510 dev_err(&client->dev,
511 "failed to request GPIO %d, error %d\n",
512 data->eoc_gpio, err);
513 goto exit_free;
516 err = gpio_direction_input(data->eoc_gpio);
517 if (err < 0) {
518 dev_err(&client->dev,
519 "Failed to configure input direction for GPIO %d, error %d\n",
520 data->eoc_gpio, err);
521 goto exit_gpio;
523 } else
524 data->eoc_gpio = 0; /* No GPIO available */
526 /* Perform some basic start-of-day setup of the device. */
527 err = ak8975_setup(client);
528 if (err < 0) {
529 dev_err(&client->dev, "AK8975 initialization fails\n");
530 goto exit_gpio;
533 /* Register with IIO */
534 data->indio_dev = iio_allocate_device(0);
535 if (data->indio_dev == NULL) {
536 err = -ENOMEM;
537 goto exit_gpio;
540 data->indio_dev->dev.parent = &client->dev;
541 data->indio_dev->info = &ak8975_info;
542 data->indio_dev->dev_data = (void *)(data);
543 data->indio_dev->modes = INDIO_DIRECT_MODE;
545 err = iio_device_register(data->indio_dev);
546 if (err < 0)
547 goto exit_free_iio;
549 return 0;
551 exit_free_iio:
552 iio_free_device(data->indio_dev);
553 exit_gpio:
554 if (data->eoc_gpio)
555 gpio_free(data->eoc_gpio);
556 exit_free:
557 kfree(data);
558 exit:
559 return err;
562 static int ak8975_remove(struct i2c_client *client)
564 struct ak8975_data *data = i2c_get_clientdata(client);
566 iio_device_unregister(data->indio_dev);
567 iio_free_device(data->indio_dev);
569 if (data->eoc_gpio)
570 gpio_free(data->eoc_gpio);
572 kfree(data);
574 return 0;
577 static const struct i2c_device_id ak8975_id[] = {
578 {"ak8975", 0},
582 MODULE_DEVICE_TABLE(i2c, ak8975_id);
584 static struct i2c_driver ak8975_driver = {
585 .driver = {
586 .name = "ak8975",
588 .probe = ak8975_probe,
589 .remove = __devexit_p(ak8975_remove),
590 .id_table = ak8975_id,
593 static int __init ak8975_init(void)
595 return i2c_add_driver(&ak8975_driver);
598 static void __exit ak8975_exit(void)
600 i2c_del_driver(&ak8975_driver);
603 module_init(ak8975_init);
604 module_exit(ak8975_exit);
606 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
607 MODULE_DESCRIPTION("AK8975 magnetometer driver");
608 MODULE_LICENSE("GPL");