x86/xen: resume timer irqs early
[linux/fpc-iii.git] / drivers / iio / magnetometer / ak8975.c
blobaeba3bbdadb01a90179e68f9916ce9572ce0a62d
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/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)
88 #define RAW_TO_GAUSS(asa) ((((asa) + 128) * 3000) / 256)
91 * Per-instance context data for the device.
93 struct ak8975_data {
94 struct i2c_client *client;
95 struct attribute_group attrs;
96 struct mutex lock;
97 u8 asa[3];
98 long raw_to_gauss[3];
99 u8 reg_cache[AK8975_MAX_REGS];
100 int eoc_gpio;
101 int eoc_irq;
102 wait_queue_head_t data_ready_queue;
103 unsigned long flags;
106 static const int ak8975_index_to_reg[] = {
107 AK8975_REG_HXL, AK8975_REG_HYL, AK8975_REG_HZL,
111 * Helper function to write to the I2C device's registers.
113 static int ak8975_write_data(struct i2c_client *client,
114 u8 reg, u8 val, u8 mask, u8 shift)
116 struct iio_dev *indio_dev = i2c_get_clientdata(client);
117 struct ak8975_data *data = iio_priv(indio_dev);
118 u8 regval;
119 int ret;
121 regval = (data->reg_cache[reg] & ~mask) | (val << shift);
122 ret = i2c_smbus_write_byte_data(client, reg, regval);
123 if (ret < 0) {
124 dev_err(&client->dev, "Write to device fails status %x\n", ret);
125 return ret;
127 data->reg_cache[reg] = regval;
129 return 0;
133 * Handle data ready irq
135 static irqreturn_t ak8975_irq_handler(int irq, void *data)
137 struct ak8975_data *ak8975 = data;
139 set_bit(0, &ak8975->flags);
140 wake_up(&ak8975->data_ready_queue);
142 return IRQ_HANDLED;
146 * Install data ready interrupt handler
148 static int ak8975_setup_irq(struct ak8975_data *data)
150 struct i2c_client *client = data->client;
151 int rc;
152 int irq;
154 if (client->irq)
155 irq = client->irq;
156 else
157 irq = gpio_to_irq(data->eoc_gpio);
159 rc = request_irq(irq, ak8975_irq_handler,
160 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
161 dev_name(&client->dev), data);
162 if (rc < 0) {
163 dev_err(&client->dev,
164 "irq %d request failed, (gpio %d): %d\n",
165 irq, data->eoc_gpio, rc);
166 return rc;
169 init_waitqueue_head(&data->data_ready_queue);
170 clear_bit(0, &data->flags);
171 data->eoc_irq = irq;
173 return rc;
178 * Perform some start-of-day setup, including reading the asa calibration
179 * values and caching them.
181 static int ak8975_setup(struct i2c_client *client)
183 struct iio_dev *indio_dev = i2c_get_clientdata(client);
184 struct ak8975_data *data = iio_priv(indio_dev);
185 u8 device_id;
186 int ret;
188 /* Confirm that the device we're talking to is really an AK8975. */
189 ret = i2c_smbus_read_byte_data(client, AK8975_REG_WIA);
190 if (ret < 0) {
191 dev_err(&client->dev, "Error reading WIA\n");
192 return ret;
194 device_id = ret;
195 if (device_id != AK8975_DEVICE_ID) {
196 dev_err(&client->dev, "Device ak8975 not found\n");
197 return -ENODEV;
200 /* Write the fused rom access mode. */
201 ret = ak8975_write_data(client,
202 AK8975_REG_CNTL,
203 AK8975_REG_CNTL_MODE_FUSE_ROM,
204 AK8975_REG_CNTL_MODE_MASK,
205 AK8975_REG_CNTL_MODE_SHIFT);
206 if (ret < 0) {
207 dev_err(&client->dev, "Error in setting fuse access mode\n");
208 return ret;
211 /* Get asa data and store in the device data. */
212 ret = i2c_smbus_read_i2c_block_data(client, AK8975_REG_ASAX,
213 3, data->asa);
214 if (ret < 0) {
215 dev_err(&client->dev, "Not able to read asa data\n");
216 return ret;
219 /* After reading fuse ROM data set power-down mode */
220 ret = ak8975_write_data(client,
221 AK8975_REG_CNTL,
222 AK8975_REG_CNTL_MODE_POWER_DOWN,
223 AK8975_REG_CNTL_MODE_MASK,
224 AK8975_REG_CNTL_MODE_SHIFT);
226 if (data->eoc_gpio > 0 || client->irq) {
227 ret = ak8975_setup_irq(data);
228 if (ret < 0) {
229 dev_err(&client->dev,
230 "Error setting data ready interrupt\n");
231 return ret;
235 if (ret < 0) {
236 dev_err(&client->dev, "Error in setting power-down mode\n");
237 return ret;
241 * Precalculate scale factor (in Gauss units) for each axis and
242 * store in the device data.
244 * This scale factor is axis-dependent, and is derived from 3 calibration
245 * factors ASA(x), ASA(y), and ASA(z).
247 * These ASA values are read from the sensor device at start of day, and
248 * cached in the device context struct.
250 * Adjusting the flux value with the sensitivity adjustment value should be
251 * done via the following formula:
253 * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
255 * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
256 * is the resultant adjusted value.
258 * We reduce the formula to:
260 * Hadj = H * (ASA + 128) / 256
262 * H is in the range of -4096 to 4095. The magnetometer has a range of
263 * +-1229uT. To go from the raw value to uT is:
265 * HuT = H * 1229/4096, or roughly, 3/10.
267 * Since 1uT = 100 gauss, our final scale factor becomes:
269 * Hadj = H * ((ASA + 128) / 256) * 3/10 * 1/100
270 * Hadj = H * ((ASA + 128) * 0.003) / 256
272 * Since ASA doesn't change, we cache the resultant scale factor into the
273 * device context in ak8975_setup().
275 data->raw_to_gauss[0] = RAW_TO_GAUSS(data->asa[0]);
276 data->raw_to_gauss[1] = RAW_TO_GAUSS(data->asa[1]);
277 data->raw_to_gauss[2] = RAW_TO_GAUSS(data->asa[2]);
279 return 0;
282 static int wait_conversion_complete_gpio(struct ak8975_data *data)
284 struct i2c_client *client = data->client;
285 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
286 int ret;
288 /* Wait for the conversion to complete. */
289 while (timeout_ms) {
290 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
291 if (gpio_get_value(data->eoc_gpio))
292 break;
293 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
295 if (!timeout_ms) {
296 dev_err(&client->dev, "Conversion timeout happened\n");
297 return -EINVAL;
300 ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST1);
301 if (ret < 0)
302 dev_err(&client->dev, "Error in reading ST1\n");
304 return ret;
307 static int wait_conversion_complete_polled(struct ak8975_data *data)
309 struct i2c_client *client = data->client;
310 u8 read_status;
311 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
312 int ret;
314 /* Wait for the conversion to complete. */
315 while (timeout_ms) {
316 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
317 ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST1);
318 if (ret < 0) {
319 dev_err(&client->dev, "Error in reading ST1\n");
320 return ret;
322 read_status = ret;
323 if (read_status)
324 break;
325 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
327 if (!timeout_ms) {
328 dev_err(&client->dev, "Conversion timeout happened\n");
329 return -EINVAL;
332 return read_status;
335 /* Returns 0 if the end of conversion interrupt occured or -ETIME otherwise */
336 static int wait_conversion_complete_interrupt(struct ak8975_data *data)
338 int ret;
340 ret = wait_event_timeout(data->data_ready_queue,
341 test_bit(0, &data->flags),
342 AK8975_DATA_READY_TIMEOUT);
343 clear_bit(0, &data->flags);
345 return ret > 0 ? 0 : -ETIME;
349 * Emits the raw flux value for the x, y, or z axis.
351 static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
353 struct ak8975_data *data = iio_priv(indio_dev);
354 struct i2c_client *client = data->client;
355 int ret;
357 mutex_lock(&data->lock);
359 /* Set up the device for taking a sample. */
360 ret = ak8975_write_data(client,
361 AK8975_REG_CNTL,
362 AK8975_REG_CNTL_MODE_ONCE,
363 AK8975_REG_CNTL_MODE_MASK,
364 AK8975_REG_CNTL_MODE_SHIFT);
365 if (ret < 0) {
366 dev_err(&client->dev, "Error in setting operating mode\n");
367 goto exit;
370 /* Wait for the conversion to complete. */
371 if (data->eoc_irq)
372 ret = wait_conversion_complete_interrupt(data);
373 else if (gpio_is_valid(data->eoc_gpio))
374 ret = wait_conversion_complete_gpio(data);
375 else
376 ret = wait_conversion_complete_polled(data);
377 if (ret < 0)
378 goto exit;
380 /* This will be executed only for non-interrupt based waiting case */
381 if (ret & AK8975_REG_ST1_DRDY_MASK) {
382 ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST2);
383 if (ret < 0) {
384 dev_err(&client->dev, "Error in reading ST2\n");
385 goto exit;
387 if (ret & (AK8975_REG_ST2_DERR_MASK |
388 AK8975_REG_ST2_HOFL_MASK)) {
389 dev_err(&client->dev, "ST2 status error 0x%x\n", ret);
390 ret = -EINVAL;
391 goto exit;
395 /* Read the flux value from the appropriate register
396 (the register is specified in the iio device attributes). */
397 ret = i2c_smbus_read_word_data(client, ak8975_index_to_reg[index]);
398 if (ret < 0) {
399 dev_err(&client->dev, "Read axis data fails\n");
400 goto exit;
403 mutex_unlock(&data->lock);
405 /* Clamp to valid range. */
406 *val = clamp_t(s16, ret, -4096, 4095);
407 return IIO_VAL_INT;
409 exit:
410 mutex_unlock(&data->lock);
411 return ret;
414 static int ak8975_read_raw(struct iio_dev *indio_dev,
415 struct iio_chan_spec const *chan,
416 int *val, int *val2,
417 long mask)
419 struct ak8975_data *data = iio_priv(indio_dev);
421 switch (mask) {
422 case IIO_CHAN_INFO_RAW:
423 return ak8975_read_axis(indio_dev, chan->address, val);
424 case IIO_CHAN_INFO_SCALE:
425 *val = 0;
426 *val2 = data->raw_to_gauss[chan->address];
427 return IIO_VAL_INT_PLUS_MICRO;
429 return -EINVAL;
432 #define AK8975_CHANNEL(axis, index) \
434 .type = IIO_MAGN, \
435 .modified = 1, \
436 .channel2 = IIO_MOD_##axis, \
437 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
438 BIT(IIO_CHAN_INFO_SCALE), \
439 .address = index, \
442 static const struct iio_chan_spec ak8975_channels[] = {
443 AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2),
446 static const struct iio_info ak8975_info = {
447 .read_raw = &ak8975_read_raw,
448 .driver_module = THIS_MODULE,
451 static int ak8975_probe(struct i2c_client *client,
452 const struct i2c_device_id *id)
454 struct ak8975_data *data;
455 struct iio_dev *indio_dev;
456 int eoc_gpio;
457 int err;
459 /* Grab and set up the supplied GPIO. */
460 if (client->dev.platform_data)
461 eoc_gpio = *(int *)(client->dev.platform_data);
462 else if (client->dev.of_node)
463 eoc_gpio = of_get_gpio(client->dev.of_node, 0);
464 else
465 eoc_gpio = -1;
467 if (eoc_gpio == -EPROBE_DEFER)
468 return -EPROBE_DEFER;
470 /* We may not have a GPIO based IRQ to scan, that is fine, we will
471 poll if so */
472 if (gpio_is_valid(eoc_gpio)) {
473 err = gpio_request_one(eoc_gpio, GPIOF_IN, "ak_8975");
474 if (err < 0) {
475 dev_err(&client->dev,
476 "failed to request GPIO %d, error %d\n",
477 eoc_gpio, err);
478 goto exit;
482 /* Register with IIO */
483 indio_dev = iio_device_alloc(sizeof(*data));
484 if (indio_dev == NULL) {
485 err = -ENOMEM;
486 goto exit_gpio;
488 data = iio_priv(indio_dev);
489 i2c_set_clientdata(client, indio_dev);
491 data->client = client;
492 data->eoc_gpio = eoc_gpio;
493 data->eoc_irq = 0;
495 /* Perform some basic start-of-day setup of the device. */
496 err = ak8975_setup(client);
497 if (err < 0) {
498 dev_err(&client->dev, "AK8975 initialization fails\n");
499 goto exit_free_iio;
502 data->client = client;
503 mutex_init(&data->lock);
504 data->eoc_gpio = eoc_gpio;
505 indio_dev->dev.parent = &client->dev;
506 indio_dev->channels = ak8975_channels;
507 indio_dev->num_channels = ARRAY_SIZE(ak8975_channels);
508 indio_dev->info = &ak8975_info;
509 indio_dev->modes = INDIO_DIRECT_MODE;
511 err = iio_device_register(indio_dev);
512 if (err < 0)
513 goto exit_free_iio;
515 return 0;
517 exit_free_iio:
518 iio_device_free(indio_dev);
519 if (data->eoc_irq)
520 free_irq(data->eoc_irq, data);
521 exit_gpio:
522 if (gpio_is_valid(eoc_gpio))
523 gpio_free(eoc_gpio);
524 exit:
525 return err;
528 static int ak8975_remove(struct i2c_client *client)
530 struct iio_dev *indio_dev = i2c_get_clientdata(client);
531 struct ak8975_data *data = iio_priv(indio_dev);
533 iio_device_unregister(indio_dev);
535 if (data->eoc_irq)
536 free_irq(data->eoc_irq, data);
538 if (gpio_is_valid(data->eoc_gpio))
539 gpio_free(data->eoc_gpio);
541 iio_device_free(indio_dev);
543 return 0;
546 static const struct i2c_device_id ak8975_id[] = {
547 {"ak8975", 0},
551 MODULE_DEVICE_TABLE(i2c, ak8975_id);
553 static const struct of_device_id ak8975_of_match[] = {
554 { .compatible = "asahi-kasei,ak8975", },
555 { .compatible = "ak8975", },
558 MODULE_DEVICE_TABLE(of, ak8975_of_match);
560 static struct i2c_driver ak8975_driver = {
561 .driver = {
562 .name = "ak8975",
563 .of_match_table = ak8975_of_match,
565 .probe = ak8975_probe,
566 .remove = ak8975_remove,
567 .id_table = ak8975_id,
569 module_i2c_driver(ak8975_driver);
571 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
572 MODULE_DESCRIPTION("AK8975 magnetometer driver");
573 MODULE_LICENSE("GPL");