]> Pileus Git - ~andy/linux/blob - drivers/staging/iio/magnetometer/ak8975.c
Merge branch 'iommu-for-tony' of git://github.com/ohadbc/omap-iommu into devel-fixes
[~andy/linux] / 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
33 #include "../iio.h"
34 #include "magnet.h"
35
36 /*
37  * Register definitions, as well as various shifts and masks to get at the
38  * individual fields of the registers.
39  */
40 #define AK8975_REG_WIA                  0x00
41 #define AK8975_DEVICE_ID                0x48
42
43 #define AK8975_REG_INFO                 0x01
44
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)
48
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)
58
59 #define AK8975_REG_ST2_HOFL_SHIFT       3
60 #define AK8975_REG_ST2_HOFL_MASK        (1 << AK8975_REG_ST2_HOFL_SHIFT)
61
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
69
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
78
79 #define AK8975_MAX_REGS                 AK8975_REG_ASAZ
80
81 /*
82  * Miscellaneous values.
83  */
84 #define AK8975_MAX_CONVERSION_TIMEOUT   500
85 #define AK8975_CONVERSION_DONE_POLL_TIME 10
86
87 /*
88  * Per-instance context data for the device.
89  */
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;
101 };
102
103 /*
104  * Helper function to write to the I2C device's registers.
105  */
106 static int ak8975_write_data(struct i2c_client *client,
107                              u8 reg, u8 val, u8 mask, u8 shift)
108 {
109         u8 regval;
110         struct i2c_msg msg;
111         u8 w_data[2];
112         int ret = 0;
113
114         struct ak8975_data *data = i2c_get_clientdata(client);
115
116         regval = data->reg_cache[reg];
117         regval &= ~mask;
118         regval |= val << shift;
119
120         w_data[0] = reg;
121         w_data[1] = regval;
122
123         msg.addr = client->addr;
124         msg.flags = 0;
125         msg.len = 2;
126         msg.buf = w_data;
127
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;
132         }
133         data->reg_cache[reg] = regval;
134
135         return 0;
136 }
137
138 /*
139  * Helper function to read a contiguous set of the I2C device's registers.
140  */
141 static int ak8975_read_data(struct i2c_client *client,
142                             u8 reg, u8 length, u8 *buffer)
143 {
144         struct i2c_msg msg[2];
145         u8 w_data[2];
146         int ret;
147
148         w_data[0] = reg;
149
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;
154
155         msg[1].addr = client->addr;
156         msg[1].flags = I2C_M_RD;
157         msg[1].len = length;
158         msg[1].buf = buffer;
159
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;
164         }
165
166         return 0;
167 }
168
169 /*
170  * Perform some start-of-day setup, including reading the asa calibration
171  * values and caching them.
172  */
173 static int ak8975_setup(struct i2c_client *client)
174 {
175         struct ak8975_data *data = i2c_get_clientdata(client);
176         u8 device_id;
177         int ret;
178
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;
184         }
185         if (device_id != AK8975_DEVICE_ID) {
186                 dev_err(&client->dev, "Device ak8975 not found\n");
187                 return -ENODEV;
188         }
189
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;
199         }
200
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;
206         }
207
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;
213
214         return 0;
215 }
216
217 /*
218  * Shows the device's mode.  0 = off, 1 = on.
219  */
220 static ssize_t show_mode(struct device *dev, struct device_attribute *devattr,
221                          char *buf)
222 {
223         struct iio_dev *indio_dev = dev_get_drvdata(dev);
224         struct ak8975_data *data = indio_dev->dev_data;
225
226         return sprintf(buf, "%lu\n", data->mode);
227 }
228
229 /*
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.
232  */
233 static ssize_t store_mode(struct device *dev, struct device_attribute *devattr,
234                           const char *buf, size_t count)
235 {
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;
241
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;
246
247         if (oval > 1) {
248                 dev_err(dev, "mode value is not supported\n");
249                 return -EINVAL;
250         }
251
252         mutex_lock(&data->lock);
253
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);
261
262                 if (ret < 0) {
263                         dev_err(&client->dev, "Error in setting mode\n");
264                         mutex_unlock(&data->lock);
265                         return ret;
266                 }
267                 data->mode = oval;
268         }
269
270         mutex_unlock(&data->lock);
271
272         return count;
273 }
274
275 /*
276  * Emits the scale factor to bring the raw value into Gauss units.
277  *
278  * This scale factor is axis-dependent, and is derived from 3 calibration
279  * factors ASA(x), ASA(y), and ASA(z).
280  *
281  * These ASA values are read from the sensor device at start of day, and
282  * cached in the device context struct.
283  *
284  * Adjusting the flux value with the sensitivity adjustment value should be
285  * done via the following formula:
286  *
287  * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
288  *
289  * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
290  * is the resultant adjusted value.
291  *
292  * We reduce the formula to:
293  *
294  * Hadj = H * (ASA + 128) / 256
295  *
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:
298  *
299  * HuT = H * 1229/4096, or roughly, 3/10.
300  *
301  * Since 1uT = 100 gauss, our final scale factor becomes:
302  *
303  * Hadj = H * ((ASA + 128) / 256) * 3/10 * 100
304  * Hadj = H * ((ASA + 128) * 30 / 256
305  *
306  * Since ASA doesn't change, we cache the resultant scale factor into the
307  * device context in ak8975_setup().
308  */
309 static ssize_t show_scale(struct device *dev, struct device_attribute *devattr,
310                           char *buf)
311 {
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);
315
316         return sprintf(buf, "%ld\n", data->raw_to_gauss[this_attr->address]);
317 }
318
319 static int wait_conversion_complete_gpio(struct ak8975_data *data)
320 {
321         struct i2c_client *client = data->client;
322         u8 read_status;
323         u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
324         int ret;
325
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;
332         }
333         if (!timeout_ms) {
334                 dev_err(&client->dev, "Conversion timeout happened\n");
335                 return -EINVAL;
336         }
337
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;
342         }
343         return read_status;
344 }
345
346 static int wait_conversion_complete_polled(struct ak8975_data *data)
347 {
348         struct i2c_client *client = data->client;
349         u8 read_status;
350         u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
351         int ret;
352
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;
360                 }
361                 if (read_status)
362                         break;
363                 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
364         }
365         if (!timeout_ms) {
366                 dev_err(&client->dev, "Conversion timeout happened\n");
367                 return -EINVAL;
368         }
369         return read_status;
370 }
371
372 /*
373  * Emits the raw flux value for the x, y, or z axis.
374  */
375 static ssize_t show_raw(struct device *dev, struct device_attribute *devattr,
376                         char *buf)
377 {
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;
386
387         mutex_lock(&data->lock);
388
389         if (data->mode == 0) {
390                 dev_err(&client->dev, "Operating mode is in power down mode\n");
391                 ret = -EBUSY;
392                 goto exit;
393         }
394
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;
404         }
405
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;
413
414         read_status = ret;
415
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;
421                 }
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;
428                 }
429         }
430
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;
437         }
438
439         mutex_unlock(&data->lock);
440
441         /* Endian conversion of the measured values. */
442         raw = (s16) (le16_to_cpu(meas_reg));
443
444         /* Clamp to valid range. */
445         raw = clamp_t(s16, raw, -4096, 4095);
446
447         return sprintf(buf, "%d\n", raw);
448
449 exit:
450         mutex_unlock(&data->lock);
451         return ret;
452 }
453
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);
461
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
471 };
472
473 static struct attribute_group ak8975_attr_group = {
474         .attrs = ak8975_attr,
475 };
476
477 static const struct iio_info ak8975_info = {
478         .attrs = &ak8975_attr_group,
479         .driver_module = THIS_MODULE,
480 };
481
482 static int ak8975_probe(struct i2c_client *client,
483                         const struct i2c_device_id *id)
484 {
485         struct ak8975_data *data;
486         int err;
487
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;
494         }
495
496         i2c_set_clientdata(client, data);
497         data->client = client;
498
499         mutex_init(&data->lock);
500
501         /* Grab and set up the supplied GPIO. */
502         data->eoc_irq = client->irq;
503         data->eoc_gpio = irq_to_gpio(client->irq);
504
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;
514                 }
515
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;
522                 }
523         } else
524                 data->eoc_gpio = 0;     /* No GPIO available */
525
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;
531         }
532
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;
538         }
539
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;
544
545         err = iio_device_register(data->indio_dev);
546         if (err < 0)
547                 goto exit_free_iio;
548
549         return 0;
550
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;
560 }
561
562 static int ak8975_remove(struct i2c_client *client)
563 {
564         struct ak8975_data *data = i2c_get_clientdata(client);
565
566         iio_device_unregister(data->indio_dev);
567         iio_free_device(data->indio_dev);
568
569         if (data->eoc_gpio)
570                 gpio_free(data->eoc_gpio);
571
572         kfree(data);
573
574         return 0;
575 }
576
577 static const struct i2c_device_id ak8975_id[] = {
578         {"ak8975", 0},
579         {}
580 };
581
582 MODULE_DEVICE_TABLE(i2c, ak8975_id);
583
584 static struct i2c_driver ak8975_driver = {
585         .driver = {
586                 .name   = "ak8975",
587         },
588         .probe          = ak8975_probe,
589         .remove         = __devexit_p(ak8975_remove),
590         .id_table       = ak8975_id,
591 };
592
593 static int __init ak8975_init(void)
594 {
595         return i2c_add_driver(&ak8975_driver);
596 }
597
598 static void __exit ak8975_exit(void)
599 {
600         i2c_del_driver(&ak8975_driver);
601 }
602
603 module_init(ak8975_init);
604 module_exit(ak8975_exit);
605
606 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
607 MODULE_DESCRIPTION("AK8975 magnetometer driver");
608 MODULE_LICENSE("GPL");