80c0f41370767fd8563f321cea626516c9d6742d
[deliverable/linux.git] / drivers / staging / iio / magnetometer / ak8975.c
1 /*
2 * A sensor driver for the magnetometer AK8975.
3 *
4 * Magnetic compass sensor driver for monitoring magnetic flux information.
5 *
6 * Copyright (c) 2010, NVIDIA Corporation.
7 *
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.
12 *
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.
17 *
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.
21 */
22
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>
30
31 #include <linux/gpio.h>
32 #include <linux/input/ak8975.h>
33
34 #include "../iio.h"
35 #include "magnet.h"
36
37 /*
38 * Register definitions, as well as various shifts and masks to get at the
39 * individual fields of the registers.
40 */
41 #define AK8975_REG_WIA 0x00
42 #define AK8975_DEVICE_ID 0x48
43
44 #define AK8975_REG_INFO 0x01
45
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)
49
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)
59
60 #define AK8975_REG_ST2_HOFL_SHIFT 3
61 #define AK8975_REG_ST2_HOFL_MASK (1 << AK8975_REG_ST2_HOFL_SHIFT)
62
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
70
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
79
80 #define AK8975_MAX_REGS AK8975_REG_ASAZ
81
82 /*
83 * Miscellaneous values.
84 */
85 #define AK8975_MAX_CONVERSION_TIMEOUT 500
86 #define AK8975_CONVERSION_DONE_POLL_TIME 10
87
88 /*
89 * Per-instance context data for the device.
90 */
91 struct ak8975_data {
92 struct i2c_client *client;
93 struct iio_dev *indio_dev;
94 struct attribute_group attrs;
95 struct mutex lock;
96 u8 asa[3];
97 long raw_to_gauss[3];
98 unsigned long mode;
99 u8 reg_cache[AK8975_MAX_REGS];
100 int eoc_gpio;
101 int eoc_irq;
102 };
103
104 /*
105 * Helper function to write to the I2C device's registers.
106 */
107 static int ak8975_write_data(struct i2c_client *client,
108 u8 reg, u8 val, u8 mask, u8 shift)
109 {
110 u8 regval;
111 struct i2c_msg msg;
112 u8 w_data[2];
113 int ret = 0;
114
115 struct ak8975_data *data = i2c_get_clientdata(client);
116
117 regval = data->reg_cache[reg];
118 regval &= ~mask;
119 regval |= val << shift;
120
121 w_data[0] = reg;
122 w_data[1] = regval;
123
124 msg.addr = client->addr;
125 msg.flags = 0;
126 msg.len = 2;
127 msg.buf = w_data;
128
129 ret = i2c_transfer(client->adapter, &msg, 1);
130 if (ret < 0) {
131 dev_err(&client->dev, "Write to device fails status %x\n", ret);
132 return ret;
133 }
134 data->reg_cache[reg] = regval;
135
136 return 0;
137 }
138
139 /*
140 * Helper function to read a contiguous set of the I2C device's registers.
141 */
142 static int ak8975_read_data(struct i2c_client *client,
143 u8 reg, u8 length, u8 *buffer)
144 {
145 struct i2c_msg msg[2];
146 u8 w_data[2];
147 int ret;
148
149 w_data[0] = reg;
150
151 msg[0].addr = client->addr;
152 msg[0].flags = I2C_M_NOSTART; /* set repeated start and write */
153 msg[0].len = 1;
154 msg[0].buf = w_data;
155
156 msg[1].addr = client->addr;
157 msg[1].flags = I2C_M_RD;
158 msg[1].len = length;
159 msg[1].buf = buffer;
160
161 ret = i2c_transfer(client->adapter, msg, 2);
162 if (ret < 0) {
163 dev_err(&client->dev, "Read from device fails\n");
164 return ret;
165 }
166
167 return 0;
168 }
169
170 /*
171 * Perform some start-of-day setup, including reading the asa calibration
172 * values and caching them.
173 */
174 static int ak8975_setup(struct i2c_client *client)
175 {
176 struct ak8975_data *data = i2c_get_clientdata(client);
177 u8 device_id;
178 int ret;
179
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);
182 if (ret < 0) {
183 dev_err(&client->dev, "Error reading WIA\n");
184 return ret;
185 }
186 if (device_id != AK8975_DEVICE_ID) {
187 dev_err(&client->dev, "Device ak8975 not found\n");
188 return -ENODEV;
189 }
190
191 /* Write the fused rom access mode. */
192 ret = ak8975_write_data(client,
193 AK8975_REG_CNTL,
194 AK8975_REG_CNTL_MODE_FUSE_ROM,
195 AK8975_REG_CNTL_MODE_MASK,
196 AK8975_REG_CNTL_MODE_SHIFT);
197 if (ret < 0) {
198 dev_err(&client->dev, "Error in setting fuse access mode\n");
199 return ret;
200 }
201
202 /* Get asa data and store in the device data. */
203 ret = ak8975_read_data(client, AK8975_REG_ASAX, 3, data->asa);
204 if (ret < 0) {
205 dev_err(&client->dev, "Not able to read asa data\n");
206 return ret;
207 }
208
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;
214
215 return 0;
216 }
217
218 /*
219 * Shows the device's mode. 0 = off, 1 = on.
220 */
221 static ssize_t show_mode(struct device *dev, struct device_attribute *devattr,
222 char *buf)
223 {
224 struct iio_dev *indio_dev = dev_get_drvdata(dev);
225 struct ak8975_data *data = indio_dev->dev_data;
226
227 return sprintf(buf, "%lu\n", data->mode);
228 }
229
230 /*
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.
233 */
234 static ssize_t store_mode(struct device *dev, struct device_attribute *devattr,
235 const char *buf, size_t count)
236 {
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;
240 unsigned long oval;
241 int ret;
242
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))
246 return -EINVAL;
247
248 if (oval > 1) {
249 dev_err(dev, "mode value is not supported\n");
250 return -EINVAL;
251 }
252
253 mutex_lock(&data->lock);
254
255 /* Write the mode to the device. */
256 if (data->mode != oval) {
257 ret = ak8975_write_data(client,
258 AK8975_REG_CNTL,
259 (u8)oval,
260 AK8975_REG_CNTL_MODE_MASK,
261 AK8975_REG_CNTL_MODE_SHIFT);
262
263 if (ret < 0) {
264 dev_err(&client->dev, "Error in setting mode\n");
265 mutex_unlock(&data->lock);
266 return ret;
267 }
268 data->mode = oval;
269 }
270
271 mutex_unlock(&data->lock);
272
273 return count;
274 }
275
276 /*
277 * Emits the scale factor to bring the raw value into Gauss units.
278 *
279 * This scale factor is axis-dependent, and is derived from 3 calibration
280 * factors ASA(x), ASA(y), and ASA(z).
281 *
282 * These ASA values are read from the sensor device at start of day, and
283 * cached in the device context struct.
284 *
285 * Adjusting the flux value with the sensitivity adjustment value should be
286 * done via the following formula:
287 *
288 * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
289 *
290 * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
291 * is the resultant adjusted value.
292 *
293 * We reduce the formula to:
294 *
295 * Hadj = H * (ASA + 128) / 256
296 *
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:
299 *
300 * HuT = H * 1229/4096, or roughly, 3/10.
301 *
302 * Since 1uT = 100 gauss, our final scale factor becomes:
303 *
304 * Hadj = H * ((ASA + 128) / 256) * 3/10 * 100
305 * Hadj = H * ((ASA + 128) * 30 / 256
306 *
307 * Since ASA doesn't change, we cache the resultant scale factor into the
308 * device context in ak8975_setup().
309 */
310 static ssize_t show_scale(struct device *dev, struct device_attribute *devattr,
311 char *buf)
312 {
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);
316
317 return sprintf(buf, "%ld\n", data->raw_to_gauss[this_attr->address]);
318 }
319
320 /*
321 * Emits the raw flux value for the x, y, or z axis.
322 */
323 static ssize_t show_raw(struct device *dev, struct device_attribute *devattr,
324 char *buf)
325 {
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;
331 u16 meas_reg;
332 s16 raw;
333 u8 read_status;
334 int ret;
335
336 mutex_lock(&data->lock);
337
338 if (data->mode == 0) {
339 dev_err(&client->dev, "Operating mode is in power down mode\n");
340 ret = -EBUSY;
341 goto exit;
342 }
343
344 /* Set up the device for taking a sample. */
345 ret = ak8975_write_data(client,
346 AK8975_REG_CNTL,
347 AK8975_REG_CNTL_MODE_ONCE,
348 AK8975_REG_CNTL_MODE_MASK,
349 AK8975_REG_CNTL_MODE_SHIFT);
350 if (ret < 0) {
351 dev_err(&client->dev, "Error in setting operating mode\n");
352 goto exit;
353 }
354
355 /* Wait for the conversion to complete. */
356 while (timeout_ms) {
357 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
358 if (gpio_get_value(data->eoc_gpio))
359 break;
360 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
361 }
362 if (!timeout_ms) {
363 dev_err(&client->dev, "Conversion timeout happened\n");
364 ret = -EINVAL;
365 goto exit;
366 }
367
368 ret = ak8975_read_data(client, AK8975_REG_ST1, 1, &read_status);
369 if (ret < 0) {
370 dev_err(&client->dev, "Error in reading ST1\n");
371 goto exit;
372 }
373
374 if (read_status & AK8975_REG_ST1_DRDY_MASK) {
375 ret = ak8975_read_data(client, AK8975_REG_ST2, 1, &read_status);
376 if (ret < 0) {
377 dev_err(&client->dev, "Error in reading ST2\n");
378 goto exit;
379 }
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",
383 read_status);
384 ret = -EINVAL;
385 goto exit;
386 }
387 }
388
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);
392 if (ret < 0) {
393 dev_err(&client->dev, "Read axis data fails\n");
394 goto exit;
395 }
396
397 mutex_unlock(&data->lock);
398
399 /* Endian conversion of the measured values. */
400 raw = (s16) (le16_to_cpu(meas_reg));
401
402 /* Clamp to valid range. */
403 raw = clamp_t(s16, raw, -4096, 4095);
404
405 return sprintf(buf, "%d\n", raw);
406
407 exit:
408 mutex_unlock(&data->lock);
409 return ret;
410 }
411
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);
419
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,
428 NULL
429 };
430
431 static struct attribute_group ak8975_attr_group = {
432 .attrs = ak8975_attr,
433 };
434
435 static int ak8975_probe(struct i2c_client *client,
436 const struct i2c_device_id *id)
437 {
438 struct ak8975_data *data;
439 struct ak8975_platform_data *pdata;
440 int err;
441
442 /* Allocate our device context. */
443 data = kzalloc(sizeof(struct ak8975_data), GFP_KERNEL);
444 if (!data) {
445 dev_err(&client->dev, "Memory allocation fails\n");
446 err = -ENOMEM;
447 goto exit;
448 }
449
450 i2c_set_clientdata(client, data);
451 data->client = client;
452
453 mutex_init(&data->lock);
454
455 /* Grab and set up the supplied GPIO. */
456 data->eoc_irq = client->irq;
457 pdata = client->dev.platform_data;
458 if (pdata)
459 data->eoc_gpio = pdata->gpio;
460 else
461 data->eoc_gpio = irq_to_gpio(client->irq);
462
463 if (!data->eoc_gpio) {
464 dev_err(&client->dev, "failed, no valid GPIO\n");
465 err = -EINVAL;
466 goto exit_free;
467 }
468
469 err = gpio_request(data->eoc_gpio, "ak_8975");
470 if (err < 0) {
471 dev_err(&client->dev, "failed to request GPIO %d, error %d\n",
472 data->eoc_gpio, err);
473 goto exit_free;
474 }
475
476 err = gpio_direction_input(data->eoc_gpio);
477 if (err < 0) {
478 dev_err(&client->dev, "Failed to configure input direction for"
479 " GPIO %d, error %d\n", data->eoc_gpio, err);
480 goto exit_gpio;
481 }
482
483 /* Perform some basic start-of-day setup of the device. */
484 err = ak8975_setup(client);
485 if (err < 0) {
486 dev_err(&client->dev, "AK8975 initialization fails\n");
487 goto exit_gpio;
488 }
489
490 /* Register with IIO */
491 data->indio_dev = iio_allocate_device();
492 if (data->indio_dev == NULL) {
493 err = -ENOMEM;
494 goto exit_gpio;
495 }
496
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;
502
503 err = iio_device_register(data->indio_dev);
504 if (err < 0)
505 goto exit_free_iio;
506
507 return 0;
508
509 exit_free_iio:
510 iio_free_device(data->indio_dev);
511 exit_gpio:
512 gpio_free(data->eoc_gpio);
513 exit_free:
514 kfree(data);
515 exit:
516 return err;
517 }
518
519 static int ak8975_remove(struct i2c_client *client)
520 {
521 struct ak8975_data *data = i2c_get_clientdata(client);
522
523 iio_device_unregister(data->indio_dev);
524 iio_free_device(data->indio_dev);
525
526 gpio_free(data->eoc_gpio);
527
528 kfree(data);
529
530 return 0;
531 }
532
533 static const struct i2c_device_id ak8975_id[] = {
534 {"ak8975", 0},
535 {}
536 };
537
538 MODULE_DEVICE_TABLE(i2c, ak8975_id);
539
540 static struct i2c_driver ak8975_driver = {
541 .driver = {
542 .name = "ak8975",
543 },
544 .probe = ak8975_probe,
545 .remove = __devexit_p(ak8975_remove),
546 .id_table = ak8975_id,
547 };
548
549 static int __init ak8975_init(void)
550 {
551 return i2c_add_driver(&ak8975_driver);
552 }
553
554 static void __exit ak8975_exit(void)
555 {
556 i2c_del_driver(&ak8975_driver);
557 }
558
559 module_init(ak8975_init);
560 module_exit(ak8975_exit);
561
562 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
563 MODULE_DESCRIPTION("AK8975 magnetometer driver");
564 MODULE_LICENSE("GPL");
This page took 0.051992 seconds and 4 git commands to generate.