Merge remote-tracking branch 'lsk/v3.10/topic/libfdt' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.git] / drivers / 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 <linux/iio/iio.h>
34 #include <linux/iio/sysfs.h>
35 /*
36  * Register definitions, as well as various shifts and masks to get at the
37  * individual fields of the registers.
38  */
39 #define AK8975_REG_WIA                  0x00
40 #define AK8975_DEVICE_ID                0x48
41
42 #define AK8975_REG_INFO                 0x01
43
44 #define AK8975_REG_ST1                  0x02
45 #define AK8975_REG_ST1_DRDY_SHIFT       0
46 #define AK8975_REG_ST1_DRDY_MASK        (1 << AK8975_REG_ST1_DRDY_SHIFT)
47
48 #define AK8975_REG_HXL                  0x03
49 #define AK8975_REG_HXH                  0x04
50 #define AK8975_REG_HYL                  0x05
51 #define AK8975_REG_HYH                  0x06
52 #define AK8975_REG_HZL                  0x07
53 #define AK8975_REG_HZH                  0x08
54 #define AK8975_REG_ST2                  0x09
55 #define AK8975_REG_ST2_DERR_SHIFT       2
56 #define AK8975_REG_ST2_DERR_MASK        (1 << AK8975_REG_ST2_DERR_SHIFT)
57
58 #define AK8975_REG_ST2_HOFL_SHIFT       3
59 #define AK8975_REG_ST2_HOFL_MASK        (1 << AK8975_REG_ST2_HOFL_SHIFT)
60
61 #define AK8975_REG_CNTL                 0x0A
62 #define AK8975_REG_CNTL_MODE_SHIFT      0
63 #define AK8975_REG_CNTL_MODE_MASK       (0xF << AK8975_REG_CNTL_MODE_SHIFT)
64 #define AK8975_REG_CNTL_MODE_POWER_DOWN 0
65 #define AK8975_REG_CNTL_MODE_ONCE       1
66 #define AK8975_REG_CNTL_MODE_SELF_TEST  8
67 #define AK8975_REG_CNTL_MODE_FUSE_ROM   0xF
68
69 #define AK8975_REG_RSVC                 0x0B
70 #define AK8975_REG_ASTC                 0x0C
71 #define AK8975_REG_TS1                  0x0D
72 #define AK8975_REG_TS2                  0x0E
73 #define AK8975_REG_I2CDIS               0x0F
74 #define AK8975_REG_ASAX                 0x10
75 #define AK8975_REG_ASAY                 0x11
76 #define AK8975_REG_ASAZ                 0x12
77
78 #define AK8975_MAX_REGS                 AK8975_REG_ASAZ
79
80 /*
81  * Miscellaneous values.
82  */
83 #define AK8975_MAX_CONVERSION_TIMEOUT   500
84 #define AK8975_CONVERSION_DONE_POLL_TIME 10
85
86 /*
87  * Per-instance context data for the device.
88  */
89 struct ak8975_data {
90         struct i2c_client       *client;
91         struct attribute_group  attrs;
92         struct mutex            lock;
93         u8                      asa[3];
94         long                    raw_to_gauss[3];
95         u8                      reg_cache[AK8975_MAX_REGS];
96         int                     eoc_gpio;
97 };
98
99 static const int ak8975_index_to_reg[] = {
100         AK8975_REG_HXL, AK8975_REG_HYL, AK8975_REG_HZL,
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         struct iio_dev *indio_dev = i2c_get_clientdata(client);
110         struct ak8975_data *data = iio_priv(indio_dev);
111         u8 regval;
112         int ret;
113
114         regval = (data->reg_cache[reg] & ~mask) | (val << shift);
115         ret = i2c_smbus_write_byte_data(client, reg, regval);
116         if (ret < 0) {
117                 dev_err(&client->dev, "Write to device fails status %x\n", ret);
118                 return ret;
119         }
120         data->reg_cache[reg] = regval;
121
122         return 0;
123 }
124
125 /*
126  * Perform some start-of-day setup, including reading the asa calibration
127  * values and caching them.
128  */
129 static int ak8975_setup(struct i2c_client *client)
130 {
131         struct iio_dev *indio_dev = i2c_get_clientdata(client);
132         struct ak8975_data *data = iio_priv(indio_dev);
133         u8 device_id;
134         int ret;
135
136         /* Confirm that the device we're talking to is really an AK8975. */
137         ret = i2c_smbus_read_byte_data(client, AK8975_REG_WIA);
138         if (ret < 0) {
139                 dev_err(&client->dev, "Error reading WIA\n");
140                 return ret;
141         }
142         device_id = ret;
143         if (device_id != AK8975_DEVICE_ID) {
144                 dev_err(&client->dev, "Device ak8975 not found\n");
145                 return -ENODEV;
146         }
147
148         /* Write the fused rom access mode. */
149         ret = ak8975_write_data(client,
150                                 AK8975_REG_CNTL,
151                                 AK8975_REG_CNTL_MODE_FUSE_ROM,
152                                 AK8975_REG_CNTL_MODE_MASK,
153                                 AK8975_REG_CNTL_MODE_SHIFT);
154         if (ret < 0) {
155                 dev_err(&client->dev, "Error in setting fuse access mode\n");
156                 return ret;
157         }
158
159         /* Get asa data and store in the device data. */
160         ret = i2c_smbus_read_i2c_block_data(client, AK8975_REG_ASAX,
161                                             3, data->asa);
162         if (ret < 0) {
163                 dev_err(&client->dev, "Not able to read asa data\n");
164                 return ret;
165         }
166
167         /* After reading fuse ROM data set power-down mode */
168         ret = ak8975_write_data(client,
169                                 AK8975_REG_CNTL,
170                                 AK8975_REG_CNTL_MODE_POWER_DOWN,
171                                 AK8975_REG_CNTL_MODE_MASK,
172                                 AK8975_REG_CNTL_MODE_SHIFT);
173         if (ret < 0) {
174                 dev_err(&client->dev, "Error in setting power-down mode\n");
175                 return ret;
176         }
177
178 /*
179  * Precalculate scale factor (in Gauss units) for each axis and
180  * store in the device data.
181  *
182  * This scale factor is axis-dependent, and is derived from 3 calibration
183  * factors ASA(x), ASA(y), and ASA(z).
184  *
185  * These ASA values are read from the sensor device at start of day, and
186  * cached in the device context struct.
187  *
188  * Adjusting the flux value with the sensitivity adjustment value should be
189  * done via the following formula:
190  *
191  * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
192  *
193  * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
194  * is the resultant adjusted value.
195  *
196  * We reduce the formula to:
197  *
198  * Hadj = H * (ASA + 128) / 256
199  *
200  * H is in the range of -4096 to 4095.  The magnetometer has a range of
201  * +-1229uT.  To go from the raw value to uT is:
202  *
203  * HuT = H * 1229/4096, or roughly, 3/10.
204  *
205  * Since 1uT = 100 gauss, our final scale factor becomes:
206  *
207  * Hadj = H * ((ASA + 128) / 256) * 3/10 * 100
208  * Hadj = H * ((ASA + 128) * 30 / 256
209  *
210  * Since ASA doesn't change, we cache the resultant scale factor into the
211  * device context in ak8975_setup().
212  */
213         data->raw_to_gauss[0] = ((data->asa[0] + 128) * 30) >> 8;
214         data->raw_to_gauss[1] = ((data->asa[1] + 128) * 30) >> 8;
215         data->raw_to_gauss[2] = ((data->asa[2] + 128) * 30) >> 8;
216
217         return 0;
218 }
219
220 static int wait_conversion_complete_gpio(struct ak8975_data *data)
221 {
222         struct i2c_client *client = data->client;
223         u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
224         int ret;
225
226         /* Wait for the conversion to complete. */
227         while (timeout_ms) {
228                 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
229                 if (gpio_get_value(data->eoc_gpio))
230                         break;
231                 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
232         }
233         if (!timeout_ms) {
234                 dev_err(&client->dev, "Conversion timeout happened\n");
235                 return -EINVAL;
236         }
237
238         ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST1);
239         if (ret < 0)
240                 dev_err(&client->dev, "Error in reading ST1\n");
241
242         return ret;
243 }
244
245 static int wait_conversion_complete_polled(struct ak8975_data *data)
246 {
247         struct i2c_client *client = data->client;
248         u8 read_status;
249         u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
250         int ret;
251
252         /* Wait for the conversion to complete. */
253         while (timeout_ms) {
254                 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
255                 ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST1);
256                 if (ret < 0) {
257                         dev_err(&client->dev, "Error in reading ST1\n");
258                         return ret;
259                 }
260                 read_status = ret;
261                 if (read_status)
262                         break;
263                 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
264         }
265         if (!timeout_ms) {
266                 dev_err(&client->dev, "Conversion timeout happened\n");
267                 return -EINVAL;
268         }
269         return read_status;
270 }
271
272 /*
273  * Emits the raw flux value for the x, y, or z axis.
274  */
275 static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
276 {
277         struct ak8975_data *data = iio_priv(indio_dev);
278         struct i2c_client *client = data->client;
279         int ret;
280
281         mutex_lock(&data->lock);
282
283         /* Set up the device for taking a sample. */
284         ret = ak8975_write_data(client,
285                                 AK8975_REG_CNTL,
286                                 AK8975_REG_CNTL_MODE_ONCE,
287                                 AK8975_REG_CNTL_MODE_MASK,
288                                 AK8975_REG_CNTL_MODE_SHIFT);
289         if (ret < 0) {
290                 dev_err(&client->dev, "Error in setting operating mode\n");
291                 goto exit;
292         }
293
294         /* Wait for the conversion to complete. */
295         if (gpio_is_valid(data->eoc_gpio))
296                 ret = wait_conversion_complete_gpio(data);
297         else
298                 ret = wait_conversion_complete_polled(data);
299         if (ret < 0)
300                 goto exit;
301
302         if (ret & AK8975_REG_ST1_DRDY_MASK) {
303                 ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST2);
304                 if (ret < 0) {
305                         dev_err(&client->dev, "Error in reading ST2\n");
306                         goto exit;
307                 }
308                 if (ret & (AK8975_REG_ST2_DERR_MASK |
309                            AK8975_REG_ST2_HOFL_MASK)) {
310                         dev_err(&client->dev, "ST2 status error 0x%x\n", ret);
311                         ret = -EINVAL;
312                         goto exit;
313                 }
314         }
315
316         /* Read the flux value from the appropriate register
317            (the register is specified in the iio device attributes). */
318         ret = i2c_smbus_read_word_data(client, ak8975_index_to_reg[index]);
319         if (ret < 0) {
320                 dev_err(&client->dev, "Read axis data fails\n");
321                 goto exit;
322         }
323
324         mutex_unlock(&data->lock);
325
326         /* Clamp to valid range. */
327         *val = clamp_t(s16, ret, -4096, 4095);
328         return IIO_VAL_INT;
329
330 exit:
331         mutex_unlock(&data->lock);
332         return ret;
333 }
334
335 static int ak8975_read_raw(struct iio_dev *indio_dev,
336                            struct iio_chan_spec const *chan,
337                            int *val, int *val2,
338                            long mask)
339 {
340         struct ak8975_data *data = iio_priv(indio_dev);
341
342         switch (mask) {
343         case IIO_CHAN_INFO_RAW:
344                 return ak8975_read_axis(indio_dev, chan->address, val);
345         case IIO_CHAN_INFO_SCALE:
346                 *val = data->raw_to_gauss[chan->address];
347                 return IIO_VAL_INT;
348         }
349         return -EINVAL;
350 }
351
352 #define AK8975_CHANNEL(axis, index)                                     \
353         {                                                               \
354                 .type = IIO_MAGN,                                       \
355                 .modified = 1,                                          \
356                 .channel2 = IIO_MOD_##axis,                             \
357                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |          \
358                              BIT(IIO_CHAN_INFO_SCALE),                  \
359                 .address = index,                                       \
360         }
361
362 static const struct iio_chan_spec ak8975_channels[] = {
363         AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2),
364 };
365
366 static const struct iio_info ak8975_info = {
367         .read_raw = &ak8975_read_raw,
368         .driver_module = THIS_MODULE,
369 };
370
371 static int ak8975_probe(struct i2c_client *client,
372                         const struct i2c_device_id *id)
373 {
374         struct ak8975_data *data;
375         struct iio_dev *indio_dev;
376         int eoc_gpio;
377         int err;
378
379         /* Grab and set up the supplied GPIO. */
380         if (client->dev.platform_data == NULL)
381                 eoc_gpio = -1;
382         else
383                 eoc_gpio = *(int *)(client->dev.platform_data);
384
385         /* We may not have a GPIO based IRQ to scan, that is fine, we will
386            poll if so */
387         if (gpio_is_valid(eoc_gpio)) {
388                 err = gpio_request_one(eoc_gpio, GPIOF_IN, "ak_8975");
389                 if (err < 0) {
390                         dev_err(&client->dev,
391                                 "failed to request GPIO %d, error %d\n",
392                                                         eoc_gpio, err);
393                         goto exit;
394                 }
395         }
396
397         /* Register with IIO */
398         indio_dev = iio_device_alloc(sizeof(*data));
399         if (indio_dev == NULL) {
400                 err = -ENOMEM;
401                 goto exit_gpio;
402         }
403         data = iio_priv(indio_dev);
404         i2c_set_clientdata(client, indio_dev);
405         /* Perform some basic start-of-day setup of the device. */
406         err = ak8975_setup(client);
407         if (err < 0) {
408                 dev_err(&client->dev, "AK8975 initialization fails\n");
409                 goto exit_free_iio;
410         }
411
412         data->client = client;
413         mutex_init(&data->lock);
414         data->eoc_gpio = eoc_gpio;
415         indio_dev->dev.parent = &client->dev;
416         indio_dev->channels = ak8975_channels;
417         indio_dev->num_channels = ARRAY_SIZE(ak8975_channels);
418         indio_dev->info = &ak8975_info;
419         indio_dev->modes = INDIO_DIRECT_MODE;
420
421         err = iio_device_register(indio_dev);
422         if (err < 0)
423                 goto exit_free_iio;
424
425         return 0;
426
427 exit_free_iio:
428         iio_device_free(indio_dev);
429 exit_gpio:
430         if (gpio_is_valid(eoc_gpio))
431                 gpio_free(eoc_gpio);
432 exit:
433         return err;
434 }
435
436 static int ak8975_remove(struct i2c_client *client)
437 {
438         struct iio_dev *indio_dev = i2c_get_clientdata(client);
439         struct ak8975_data *data = iio_priv(indio_dev);
440
441         iio_device_unregister(indio_dev);
442
443         if (gpio_is_valid(data->eoc_gpio))
444                 gpio_free(data->eoc_gpio);
445
446         iio_device_free(indio_dev);
447
448         return 0;
449 }
450
451 static const struct i2c_device_id ak8975_id[] = {
452         {"ak8975", 0},
453         {}
454 };
455
456 MODULE_DEVICE_TABLE(i2c, ak8975_id);
457
458 static const struct of_device_id ak8975_of_match[] = {
459         { .compatible = "asahi-kasei,ak8975", },
460         { .compatible = "ak8975", },
461         { }
462 };
463 MODULE_DEVICE_TABLE(of, ak8975_of_match);
464
465 static struct i2c_driver ak8975_driver = {
466         .driver = {
467                 .name   = "ak8975",
468                 .of_match_table = ak8975_of_match,
469         },
470         .probe          = ak8975_probe,
471         .remove         = ak8975_remove,
472         .id_table       = ak8975_id,
473 };
474 module_i2c_driver(ak8975_driver);
475
476 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
477 MODULE_DESCRIPTION("AK8975 magnetometer driver");
478 MODULE_LICENSE("GPL");