blob: 896b13e39daedd61b6d8acbad6d2873df2473cd3 [file] [log] [blame]
Andrew Chew3285aae2010-09-08 22:02:17 -07001/*
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>
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +010027#include <linux/interrupt.h>
Andrew Chew3285aae2010-09-08 22:02:17 -070028#include <linux/err.h>
29#include <linux/mutex.h>
30#include <linux/delay.h>
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +010031#include <linux/bitops.h>
Andrew Chew3285aae2010-09-08 22:02:17 -070032#include <linux/gpio.h>
Jacek Anaszewskif4b7f752013-05-07 11:41:00 +010033#include <linux/of_gpio.h>
Srinivas Pandruvadad9139712014-03-19 16:56:00 +000034#include <linux/acpi.h>
Andrew Chew3285aae2010-09-08 22:02:17 -070035
Jonathan Cameron06458e22012-04-25 15:54:58 +010036#include <linux/iio/iio.h>
37#include <linux/iio/sysfs.h>
Andrew Chew3285aae2010-09-08 22:02:17 -070038/*
39 * Register definitions, as well as various shifts and masks to get at the
40 * individual fields of the registers.
41 */
42#define AK8975_REG_WIA 0x00
43#define AK8975_DEVICE_ID 0x48
44
45#define AK8975_REG_INFO 0x01
46
47#define AK8975_REG_ST1 0x02
48#define AK8975_REG_ST1_DRDY_SHIFT 0
49#define AK8975_REG_ST1_DRDY_MASK (1 << AK8975_REG_ST1_DRDY_SHIFT)
50
51#define AK8975_REG_HXL 0x03
52#define AK8975_REG_HXH 0x04
53#define AK8975_REG_HYL 0x05
54#define AK8975_REG_HYH 0x06
55#define AK8975_REG_HZL 0x07
56#define AK8975_REG_HZH 0x08
57#define AK8975_REG_ST2 0x09
58#define AK8975_REG_ST2_DERR_SHIFT 2
59#define AK8975_REG_ST2_DERR_MASK (1 << AK8975_REG_ST2_DERR_SHIFT)
60
61#define AK8975_REG_ST2_HOFL_SHIFT 3
62#define AK8975_REG_ST2_HOFL_MASK (1 << AK8975_REG_ST2_HOFL_SHIFT)
63
64#define AK8975_REG_CNTL 0x0A
65#define AK8975_REG_CNTL_MODE_SHIFT 0
66#define AK8975_REG_CNTL_MODE_MASK (0xF << AK8975_REG_CNTL_MODE_SHIFT)
Gwendal Grignou71222bf2014-11-21 10:45:47 -080067#define AK8975_REG_CNTL_MODE_POWER_DOWN 0x00
68#define AK8975_REG_CNTL_MODE_ONCE 0x01
69#define AK8975_REG_CNTL_MODE_SELF_TEST 0x08
70#define AK8975_REG_CNTL_MODE_FUSE_ROM 0x0F
Andrew Chew3285aae2010-09-08 22:02:17 -070071
72#define AK8975_REG_RSVC 0x0B
73#define AK8975_REG_ASTC 0x0C
74#define AK8975_REG_TS1 0x0D
75#define AK8975_REG_TS2 0x0E
76#define AK8975_REG_I2CDIS 0x0F
77#define AK8975_REG_ASAX 0x10
78#define AK8975_REG_ASAY 0x11
79#define AK8975_REG_ASAZ 0x12
80
81#define AK8975_MAX_REGS AK8975_REG_ASAZ
82
83/*
Gwendal Grignou57e73a42014-11-21 10:45:49 -080084 * AK09912 Register definitions
85 */
86#define AK09912_REG_WIA1 0x00
87#define AK09912_REG_WIA2 0x01
88#define AK09912_DEVICE_ID 0x04
89#define AK09911_DEVICE_ID 0x05
90
91#define AK09911_REG_INFO1 0x02
92#define AK09911_REG_INFO2 0x03
93
94#define AK09912_REG_ST1 0x10
95
96#define AK09912_REG_ST1_DRDY_SHIFT 0
97#define AK09912_REG_ST1_DRDY_MASK (1 << AK09912_REG_ST1_DRDY_SHIFT)
98
99#define AK09912_REG_HXL 0x11
100#define AK09912_REG_HXH 0x12
101#define AK09912_REG_HYL 0x13
102#define AK09912_REG_HYH 0x14
103#define AK09912_REG_HZL 0x15
104#define AK09912_REG_HZH 0x16
105#define AK09912_REG_TMPS 0x17
106
107#define AK09912_REG_ST2 0x18
108#define AK09912_REG_ST2_HOFL_SHIFT 3
109#define AK09912_REG_ST2_HOFL_MASK (1 << AK09912_REG_ST2_HOFL_SHIFT)
110
111#define AK09912_REG_CNTL1 0x30
112
113#define AK09912_REG_CNTL2 0x31
114#define AK09912_REG_CNTL_MODE_POWER_DOWN 0x00
115#define AK09912_REG_CNTL_MODE_ONCE 0x01
116#define AK09912_REG_CNTL_MODE_SELF_TEST 0x10
117#define AK09912_REG_CNTL_MODE_FUSE_ROM 0x1F
118#define AK09912_REG_CNTL2_MODE_SHIFT 0
119#define AK09912_REG_CNTL2_MODE_MASK (0x1F << AK09912_REG_CNTL2_MODE_SHIFT)
120
121#define AK09912_REG_CNTL3 0x32
122
123#define AK09912_REG_TS1 0x33
124#define AK09912_REG_TS2 0x34
125#define AK09912_REG_TS3 0x35
126#define AK09912_REG_I2CDIS 0x36
127#define AK09912_REG_TS4 0x37
128
129#define AK09912_REG_ASAX 0x60
130#define AK09912_REG_ASAY 0x61
131#define AK09912_REG_ASAZ 0x62
132
133#define AK09912_MAX_REGS AK09912_REG_ASAZ
134
135/*
Andrew Chew3285aae2010-09-08 22:02:17 -0700136 * Miscellaneous values.
137 */
138#define AK8975_MAX_CONVERSION_TIMEOUT 500
139#define AK8975_CONVERSION_DONE_POLL_TIME 10
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100140#define AK8975_DATA_READY_TIMEOUT ((100*HZ)/1000)
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800141
142/*
143 * Precalculate scale factor (in Gauss units) for each axis and
144 * store in the device data.
145 *
146 * This scale factor is axis-dependent, and is derived from 3 calibration
147 * factors ASA(x), ASA(y), and ASA(z).
148 *
149 * These ASA values are read from the sensor device at start of day, and
150 * cached in the device context struct.
151 *
152 * Adjusting the flux value with the sensitivity adjustment value should be
153 * done via the following formula:
154 *
155 * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
156 * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
157 * is the resultant adjusted value.
158 *
159 * We reduce the formula to:
160 *
161 * Hadj = H * (ASA + 128) / 256
162 *
163 * H is in the range of -4096 to 4095. The magnetometer has a range of
164 * +-1229uT. To go from the raw value to uT is:
165 *
166 * HuT = H * 1229/4096, or roughly, 3/10.
167 *
168 * Since 1uT = 0.01 gauss, our final scale factor becomes:
169 *
170 * Hadj = H * ((ASA + 128) / 256) * 3/10 * 1/100
171 * Hadj = H * ((ASA + 128) * 0.003) / 256
172 *
173 * Since ASA doesn't change, we cache the resultant scale factor into the
174 * device context in ak8975_setup().
175 *
176 * Given we use IIO_VAL_INT_PLUS_MICRO bit when displaying the scale, we
177 * multiply the stored scale value by 1e6.
178 */
179static long ak8975_raw_to_gauss(u16 data)
180{
181 return (((long)data + 128) * 3000) / 256;
182}
183
184/*
Gwendal Grignou57e73a42014-11-21 10:45:49 -0800185 * For AK8963 and AK09911, same calculation, but the device is less sensitive:
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800186 *
187 * H is in the range of +-8190. The magnetometer has a range of
188 * +-4912uT. To go from the raw value to uT is:
189 *
190 * HuT = H * 4912/8190, or roughly, 6/10, instead of 3/10.
191 */
Gwendal Grignou57e73a42014-11-21 10:45:49 -0800192
193static long ak8963_09911_raw_to_gauss(u16 data)
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800194{
195 return (((long)data + 128) * 6000) / 256;
196}
Srinivas Pandruvada6027c072014-03-19 16:56:00 +0000197
Gwendal Grignou57e73a42014-11-21 10:45:49 -0800198/*
199 * For AK09912, same calculation, except the device is more sensitive:
200 *
201 * H is in the range of -32752 to 32752. The magnetometer has a range of
202 * +-4912uT. To go from the raw value to uT is:
203 *
204 * HuT = H * 4912/32752, or roughly, 3/20, instead of 3/10.
205 */
206static long ak09912_raw_to_gauss(u16 data)
207{
208 return (((long)data + 128) * 1500) / 256;
209}
210
Srinivas Pandruvada6027c072014-03-19 16:56:00 +0000211/* Compatible Asahi Kasei Compass parts */
212enum asahi_compass_chipset {
213 AK8975,
214 AK8963,
Gwendal Grignou57e73a42014-11-21 10:45:49 -0800215 AK09911,
216 AK09912,
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800217 AK_MAX_TYPE
218};
219
220enum ak_ctrl_reg_addr {
221 ST1,
222 ST2,
223 CNTL,
224 ASA_BASE,
225 MAX_REGS,
226 REGS_END,
227};
228
229enum ak_ctrl_reg_mask {
230 ST1_DRDY,
231 ST2_HOFL,
232 ST2_DERR,
233 CNTL_MODE,
234 MASK_END,
235};
236
237enum ak_ctrl_mode {
238 POWER_DOWN,
239 MODE_ONCE,
240 SELF_TEST,
241 FUSE_ROM,
242 MODE_END,
243};
244
245struct ak_def {
246 enum asahi_compass_chipset type;
247 long (*raw_to_gauss)(u16 data);
248 u16 range;
249 u8 ctrl_regs[REGS_END];
250 u8 ctrl_masks[MASK_END];
251 u8 ctrl_modes[MODE_END];
252 u8 data_regs[3];
253};
254
Julia Lawallf78c5f92016-01-05 15:56:42 +0100255static const struct ak_def ak_def_array[AK_MAX_TYPE] = {
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800256 {
257 .type = AK8975,
258 .raw_to_gauss = ak8975_raw_to_gauss,
259 .range = 4096,
260 .ctrl_regs = {
261 AK8975_REG_ST1,
262 AK8975_REG_ST2,
263 AK8975_REG_CNTL,
264 AK8975_REG_ASAX,
265 AK8975_MAX_REGS},
266 .ctrl_masks = {
267 AK8975_REG_ST1_DRDY_MASK,
268 AK8975_REG_ST2_HOFL_MASK,
269 AK8975_REG_ST2_DERR_MASK,
270 AK8975_REG_CNTL_MODE_MASK},
271 .ctrl_modes = {
272 AK8975_REG_CNTL_MODE_POWER_DOWN,
273 AK8975_REG_CNTL_MODE_ONCE,
274 AK8975_REG_CNTL_MODE_SELF_TEST,
275 AK8975_REG_CNTL_MODE_FUSE_ROM},
276 .data_regs = {
277 AK8975_REG_HXL,
278 AK8975_REG_HYL,
279 AK8975_REG_HZL},
280 },
281 {
282 .type = AK8963,
Gwendal Grignou57e73a42014-11-21 10:45:49 -0800283 .raw_to_gauss = ak8963_09911_raw_to_gauss,
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800284 .range = 8190,
285 .ctrl_regs = {
286 AK8975_REG_ST1,
287 AK8975_REG_ST2,
288 AK8975_REG_CNTL,
289 AK8975_REG_ASAX,
290 AK8975_MAX_REGS},
291 .ctrl_masks = {
292 AK8975_REG_ST1_DRDY_MASK,
293 AK8975_REG_ST2_HOFL_MASK,
294 0,
295 AK8975_REG_CNTL_MODE_MASK},
296 .ctrl_modes = {
297 AK8975_REG_CNTL_MODE_POWER_DOWN,
298 AK8975_REG_CNTL_MODE_ONCE,
299 AK8975_REG_CNTL_MODE_SELF_TEST,
300 AK8975_REG_CNTL_MODE_FUSE_ROM},
301 .data_regs = {
302 AK8975_REG_HXL,
303 AK8975_REG_HYL,
304 AK8975_REG_HZL},
305 },
Gwendal Grignou57e73a42014-11-21 10:45:49 -0800306 {
307 .type = AK09911,
308 .raw_to_gauss = ak8963_09911_raw_to_gauss,
309 .range = 8192,
310 .ctrl_regs = {
311 AK09912_REG_ST1,
312 AK09912_REG_ST2,
313 AK09912_REG_CNTL2,
314 AK09912_REG_ASAX,
315 AK09912_MAX_REGS},
316 .ctrl_masks = {
317 AK09912_REG_ST1_DRDY_MASK,
318 AK09912_REG_ST2_HOFL_MASK,
319 0,
320 AK09912_REG_CNTL2_MODE_MASK},
321 .ctrl_modes = {
322 AK09912_REG_CNTL_MODE_POWER_DOWN,
323 AK09912_REG_CNTL_MODE_ONCE,
324 AK09912_REG_CNTL_MODE_SELF_TEST,
325 AK09912_REG_CNTL_MODE_FUSE_ROM},
326 .data_regs = {
327 AK09912_REG_HXL,
328 AK09912_REG_HYL,
329 AK09912_REG_HZL},
330 },
331 {
332 .type = AK09912,
333 .raw_to_gauss = ak09912_raw_to_gauss,
334 .range = 32752,
335 .ctrl_regs = {
336 AK09912_REG_ST1,
337 AK09912_REG_ST2,
338 AK09912_REG_CNTL2,
339 AK09912_REG_ASAX,
340 AK09912_MAX_REGS},
341 .ctrl_masks = {
342 AK09912_REG_ST1_DRDY_MASK,
343 AK09912_REG_ST2_HOFL_MASK,
344 0,
345 AK09912_REG_CNTL2_MODE_MASK},
346 .ctrl_modes = {
347 AK09912_REG_CNTL_MODE_POWER_DOWN,
348 AK09912_REG_CNTL_MODE_ONCE,
349 AK09912_REG_CNTL_MODE_SELF_TEST,
350 AK09912_REG_CNTL_MODE_FUSE_ROM},
351 .data_regs = {
352 AK09912_REG_HXL,
353 AK09912_REG_HYL,
354 AK09912_REG_HZL},
355 }
Srinivas Pandruvada6027c072014-03-19 16:56:00 +0000356};
Andrew Chew3285aae2010-09-08 22:02:17 -0700357
358/*
359 * Per-instance context data for the device.
360 */
361struct ak8975_data {
362 struct i2c_client *client;
Julia Lawallf78c5f92016-01-05 15:56:42 +0100363 const struct ak_def *def;
Andrew Chew3285aae2010-09-08 22:02:17 -0700364 struct mutex lock;
365 u8 asa[3];
366 long raw_to_gauss[3];
Andrew Chew3285aae2010-09-08 22:02:17 -0700367 int eoc_gpio;
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100368 int eoc_irq;
369 wait_queue_head_t data_ready_queue;
370 unsigned long flags;
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800371 u8 cntl_cache;
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100372};
373
Andrew Chew3285aae2010-09-08 22:02:17 -0700374/*
Gwendal Grignou57e73a42014-11-21 10:45:49 -0800375 * Return 0 if the i2c device is the one we expect.
376 * return a negative error number otherwise
377 */
378static int ak8975_who_i_am(struct i2c_client *client,
379 enum asahi_compass_chipset type)
380{
381 u8 wia_val[2];
382 int ret;
383
384 /*
385 * Signature for each device:
386 * Device | WIA1 | WIA2
387 * AK09912 | DEVICE_ID | AK09912_DEVICE_ID
388 * AK09911 | DEVICE_ID | AK09911_DEVICE_ID
389 * AK8975 | DEVICE_ID | NA
390 * AK8963 | DEVICE_ID | NA
391 */
392 ret = i2c_smbus_read_i2c_block_data(client, AK09912_REG_WIA1,
393 2, wia_val);
394 if (ret < 0) {
395 dev_err(&client->dev, "Error reading WIA\n");
396 return ret;
397 }
398
399 if (wia_val[0] != AK8975_DEVICE_ID)
400 return -ENODEV;
401
402 switch (type) {
403 case AK8975:
404 case AK8963:
405 return 0;
406 case AK09911:
407 if (wia_val[1] == AK09911_DEVICE_ID)
408 return 0;
409 break;
410 case AK09912:
411 if (wia_val[1] == AK09912_DEVICE_ID)
412 return 0;
413 break;
414 default:
415 dev_err(&client->dev, "Type %d unknown\n", type);
416 }
417 return -ENODEV;
418}
419
420/*
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800421 * Helper function to write to CNTL register.
Andrew Chew3285aae2010-09-08 22:02:17 -0700422 */
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800423static int ak8975_set_mode(struct ak8975_data *data, enum ak_ctrl_mode mode)
Andrew Chew3285aae2010-09-08 22:02:17 -0700424{
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100425 u8 regval;
426 int ret;
Andrew Chew3285aae2010-09-08 22:02:17 -0700427
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800428 regval = (data->cntl_cache & ~data->def->ctrl_masks[CNTL_MODE]) |
429 data->def->ctrl_modes[mode];
430 ret = i2c_smbus_write_byte_data(data->client,
431 data->def->ctrl_regs[CNTL], regval);
Andrew Chew3285aae2010-09-08 22:02:17 -0700432 if (ret < 0) {
Andrew Chew3285aae2010-09-08 22:02:17 -0700433 return ret;
434 }
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800435 data->cntl_cache = regval;
436 /* After mode change wait atleast 100us */
437 usleep_range(100, 500);
Andrew Chew3285aae2010-09-08 22:02:17 -0700438
439 return 0;
440}
441
442/*
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100443 * Handle data ready irq
444 */
445static irqreturn_t ak8975_irq_handler(int irq, void *data)
446{
447 struct ak8975_data *ak8975 = data;
448
449 set_bit(0, &ak8975->flags);
450 wake_up(&ak8975->data_ready_queue);
451
452 return IRQ_HANDLED;
453}
454
455/*
456 * Install data ready interrupt handler
457 */
458static int ak8975_setup_irq(struct ak8975_data *data)
459{
460 struct i2c_client *client = data->client;
461 int rc;
462 int irq;
463
464 if (client->irq)
465 irq = client->irq;
466 else
467 irq = gpio_to_irq(data->eoc_gpio);
468
Beomho Seoa845a3a2014-06-23 13:34:00 +0100469 rc = devm_request_irq(&client->dev, irq, ak8975_irq_handler,
Gwendal Grignou71222bf2014-11-21 10:45:47 -0800470 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
471 dev_name(&client->dev), data);
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100472 if (rc < 0) {
473 dev_err(&client->dev,
474 "irq %d request failed, (gpio %d): %d\n",
475 irq, data->eoc_gpio, rc);
476 return rc;
477 }
478
479 init_waitqueue_head(&data->data_ready_queue);
480 clear_bit(0, &data->flags);
481 data->eoc_irq = irq;
482
483 return rc;
484}
485
486
487/*
Andrew Chew3285aae2010-09-08 22:02:17 -0700488 * Perform some start-of-day setup, including reading the asa calibration
489 * values and caching them.
490 */
491static int ak8975_setup(struct i2c_client *client)
492{
Preetham Chandru40f32d92012-04-09 18:05:01 +0530493 struct iio_dev *indio_dev = i2c_get_clientdata(client);
494 struct ak8975_data *data = iio_priv(indio_dev);
Andrew Chew3285aae2010-09-08 22:02:17 -0700495 int ret;
496
Andrew Chew3285aae2010-09-08 22:02:17 -0700497 /* Write the fused rom access mode. */
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800498 ret = ak8975_set_mode(data, FUSE_ROM);
Andrew Chew3285aae2010-09-08 22:02:17 -0700499 if (ret < 0) {
500 dev_err(&client->dev, "Error in setting fuse access mode\n");
501 return ret;
502 }
503
504 /* Get asa data and store in the device data. */
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800505 ret = i2c_smbus_read_i2c_block_data(client,
506 data->def->ctrl_regs[ASA_BASE],
Jonathan Cameronc411f602013-03-24 16:58:00 +0000507 3, data->asa);
Andrew Chew3285aae2010-09-08 22:02:17 -0700508 if (ret < 0) {
509 dev_err(&client->dev, "Not able to read asa data\n");
510 return ret;
511 }
512
Leed Aguilar040f3e52012-06-06 16:14:56 -0400513 /* After reading fuse ROM data set power-down mode */
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800514 ret = ak8975_set_mode(data, POWER_DOWN);
Gwendal Grignou71222bf2014-11-21 10:45:47 -0800515 if (ret < 0) {
516 dev_err(&client->dev, "Error in setting power-down mode\n");
517 return ret;
518 }
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100519
Gwendal Grignou71222bf2014-11-21 10:45:47 -0800520 if (data->eoc_gpio > 0 || client->irq > 0) {
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100521 ret = ak8975_setup_irq(data);
522 if (ret < 0) {
523 dev_err(&client->dev,
524 "Error setting data ready interrupt\n");
525 return ret;
526 }
527 }
528
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800529 data->raw_to_gauss[0] = data->def->raw_to_gauss(data->asa[0]);
530 data->raw_to_gauss[1] = data->def->raw_to_gauss(data->asa[1]);
531 data->raw_to_gauss[2] = data->def->raw_to_gauss(data->asa[2]);
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100532
533 return 0;
534}
535
Alan Cox01fbb472011-04-06 13:31:40 +0100536static int wait_conversion_complete_gpio(struct ak8975_data *data)
537{
538 struct i2c_client *client = data->client;
Alan Cox01fbb472011-04-06 13:31:40 +0100539 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
540 int ret;
541
542 /* Wait for the conversion to complete. */
543 while (timeout_ms) {
544 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
545 if (gpio_get_value(data->eoc_gpio))
546 break;
547 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
548 }
549 if (!timeout_ms) {
550 dev_err(&client->dev, "Conversion timeout happened\n");
551 return -EINVAL;
552 }
553
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800554 ret = i2c_smbus_read_byte_data(client, data->def->ctrl_regs[ST1]);
Jonathan Cameronc411f602013-03-24 16:58:00 +0000555 if (ret < 0)
Alan Cox01fbb472011-04-06 13:31:40 +0100556 dev_err(&client->dev, "Error in reading ST1\n");
Jonathan Cameronc411f602013-03-24 16:58:00 +0000557
558 return ret;
Alan Cox01fbb472011-04-06 13:31:40 +0100559}
560
561static int wait_conversion_complete_polled(struct ak8975_data *data)
562{
563 struct i2c_client *client = data->client;
564 u8 read_status;
565 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
566 int ret;
567
568 /* Wait for the conversion to complete. */
569 while (timeout_ms) {
570 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800571 ret = i2c_smbus_read_byte_data(client,
572 data->def->ctrl_regs[ST1]);
Alan Cox01fbb472011-04-06 13:31:40 +0100573 if (ret < 0) {
574 dev_err(&client->dev, "Error in reading ST1\n");
575 return ret;
576 }
Jonathan Cameronc411f602013-03-24 16:58:00 +0000577 read_status = ret;
Alan Cox01fbb472011-04-06 13:31:40 +0100578 if (read_status)
579 break;
580 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
581 }
582 if (!timeout_ms) {
583 dev_err(&client->dev, "Conversion timeout happened\n");
584 return -EINVAL;
585 }
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100586
Alan Cox01fbb472011-04-06 13:31:40 +0100587 return read_status;
588}
589
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100590/* Returns 0 if the end of conversion interrupt occured or -ETIME otherwise */
591static int wait_conversion_complete_interrupt(struct ak8975_data *data)
592{
593 int ret;
594
595 ret = wait_event_timeout(data->data_ready_queue,
596 test_bit(0, &data->flags),
597 AK8975_DATA_READY_TIMEOUT);
598 clear_bit(0, &data->flags);
599
600 return ret > 0 ? 0 : -ETIME;
601}
602
Andrew Chew3285aae2010-09-08 22:02:17 -0700603/*
604 * Emits the raw flux value for the x, y, or z axis.
605 */
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100606static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
Andrew Chew3285aae2010-09-08 22:02:17 -0700607{
Jonathan Cameron338473c2011-06-27 13:07:54 +0100608 struct ak8975_data *data = iio_priv(indio_dev);
Andrew Chew3285aae2010-09-08 22:02:17 -0700609 struct i2c_client *client = data->client;
Andrew Chew3285aae2010-09-08 22:02:17 -0700610 int ret;
611
612 mutex_lock(&data->lock);
613
Andrew Chew3285aae2010-09-08 22:02:17 -0700614 /* Set up the device for taking a sample. */
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800615 ret = ak8975_set_mode(data, MODE_ONCE);
Andrew Chew3285aae2010-09-08 22:02:17 -0700616 if (ret < 0) {
617 dev_err(&client->dev, "Error in setting operating mode\n");
618 goto exit;
619 }
620
621 /* Wait for the conversion to complete. */
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100622 if (data->eoc_irq)
623 ret = wait_conversion_complete_interrupt(data);
624 else if (gpio_is_valid(data->eoc_gpio))
Alan Cox01fbb472011-04-06 13:31:40 +0100625 ret = wait_conversion_complete_gpio(data);
626 else
627 ret = wait_conversion_complete_polled(data);
628 if (ret < 0)
Andrew Chew3285aae2010-09-08 22:02:17 -0700629 goto exit;
Andrew Chew3285aae2010-09-08 22:02:17 -0700630
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100631 /* This will be executed only for non-interrupt based waiting case */
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800632 if (ret & data->def->ctrl_masks[ST1_DRDY]) {
633 ret = i2c_smbus_read_byte_data(client,
634 data->def->ctrl_regs[ST2]);
Andrew Chew3285aae2010-09-08 22:02:17 -0700635 if (ret < 0) {
636 dev_err(&client->dev, "Error in reading ST2\n");
637 goto exit;
638 }
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800639 if (ret & (data->def->ctrl_masks[ST2_DERR] |
640 data->def->ctrl_masks[ST2_HOFL])) {
Jonathan Cameronc411f602013-03-24 16:58:00 +0000641 dev_err(&client->dev, "ST2 status error 0x%x\n", ret);
Andrew Chew3285aae2010-09-08 22:02:17 -0700642 ret = -EINVAL;
643 goto exit;
644 }
645 }
646
647 /* Read the flux value from the appropriate register
648 (the register is specified in the iio device attributes). */
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800649 ret = i2c_smbus_read_word_data(client, data->def->data_regs[index]);
Andrew Chew3285aae2010-09-08 22:02:17 -0700650 if (ret < 0) {
651 dev_err(&client->dev, "Read axis data fails\n");
652 goto exit;
653 }
654
655 mutex_unlock(&data->lock);
656
Andrew Chew3285aae2010-09-08 22:02:17 -0700657 /* Clamp to valid range. */
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800658 *val = clamp_t(s16, ret, -data->def->range, data->def->range);
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100659 return IIO_VAL_INT;
Andrew Chew3285aae2010-09-08 22:02:17 -0700660
661exit:
662 mutex_unlock(&data->lock);
663 return ret;
664}
665
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100666static int ak8975_read_raw(struct iio_dev *indio_dev,
667 struct iio_chan_spec const *chan,
668 int *val, int *val2,
669 long mask)
670{
671 struct ak8975_data *data = iio_priv(indio_dev);
672
673 switch (mask) {
Jonathan Cameron4d9948b2012-04-15 17:41:23 +0100674 case IIO_CHAN_INFO_RAW:
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100675 return ak8975_read_axis(indio_dev, chan->address, val);
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100676 case IIO_CHAN_INFO_SCALE:
Beomho Seobef44ab2014-04-02 09:15:00 +0100677 *val = 0;
678 *val2 = data->raw_to_gauss[chan->address];
679 return IIO_VAL_INT_PLUS_MICRO;
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100680 }
681 return -EINVAL;
682}
683
684#define AK8975_CHANNEL(axis, index) \
685 { \
686 .type = IIO_MAGN, \
687 .modified = 1, \
688 .channel2 = IIO_MOD_##axis, \
Jonathan Cameron3a0b4422013-02-27 19:40:48 +0000689 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
690 BIT(IIO_CHAN_INFO_SCALE), \
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100691 .address = index, \
692 }
693
694static const struct iio_chan_spec ak8975_channels[] = {
695 AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2),
696};
697
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100698static const struct iio_info ak8975_info = {
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100699 .read_raw = &ak8975_read_raw,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100700 .driver_module = THIS_MODULE,
701};
702
Srinivas Pandruvadad9139712014-03-19 16:56:00 +0000703static const struct acpi_device_id ak_acpi_match[] = {
704 {"AK8975", AK8975},
705 {"AK8963", AK8963},
706 {"INVN6500", AK8963},
Gwendal Grignou57e73a42014-11-21 10:45:49 -0800707 {"AK09911", AK09911},
708 {"AK09912", AK09912},
Srinivas Pandruvadad9139712014-03-19 16:56:00 +0000709 { },
710};
711MODULE_DEVICE_TABLE(acpi, ak_acpi_match);
712
Irina Tirdea48edc372014-08-09 15:18:00 +0100713static const char *ak8975_match_acpi_device(struct device *dev,
714 enum asahi_compass_chipset *chipset)
Srinivas Pandruvadad9139712014-03-19 16:56:00 +0000715{
716 const struct acpi_device_id *id;
717
718 id = acpi_match_device(dev->driver->acpi_match_table, dev);
719 if (!id)
720 return NULL;
721 *chipset = (int)id->driver_data;
722
Irina Tirdea48edc372014-08-09 15:18:00 +0100723 return dev_name(dev);
Srinivas Pandruvadad9139712014-03-19 16:56:00 +0000724}
725
Bill Pemberton4ae1c612012-11-19 13:21:57 -0500726static int ak8975_probe(struct i2c_client *client,
Andrew Chew3285aae2010-09-08 22:02:17 -0700727 const struct i2c_device_id *id)
728{
729 struct ak8975_data *data;
Jonathan Cameron338473c2011-06-27 13:07:54 +0100730 struct iio_dev *indio_dev;
731 int eoc_gpio;
Andrew Chew3285aae2010-09-08 22:02:17 -0700732 int err;
Irina Tirdea48edc372014-08-09 15:18:00 +0100733 const char *name = NULL;
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800734 enum asahi_compass_chipset chipset;
Andrew Chew3285aae2010-09-08 22:02:17 -0700735
Andrew Chew3285aae2010-09-08 22:02:17 -0700736 /* Grab and set up the supplied GPIO. */
Jacek Anaszewskif4b7f752013-05-07 11:41:00 +0100737 if (client->dev.platform_data)
Jonathan Cameronf6d838d2011-09-21 11:15:59 +0100738 eoc_gpio = *(int *)(client->dev.platform_data);
Jacek Anaszewskif4b7f752013-05-07 11:41:00 +0100739 else if (client->dev.of_node)
740 eoc_gpio = of_get_gpio(client->dev.of_node, 0);
741 else
742 eoc_gpio = -1;
743
744 if (eoc_gpio == -EPROBE_DEFER)
745 return -EPROBE_DEFER;
Andrew Chew3285aae2010-09-08 22:02:17 -0700746
Alan Cox01fbb472011-04-06 13:31:40 +0100747 /* We may not have a GPIO based IRQ to scan, that is fine, we will
748 poll if so */
Stephen Warren7c6c9362011-09-21 11:16:00 +0100749 if (gpio_is_valid(eoc_gpio)) {
Beomho Seoa845a3a2014-06-23 13:34:00 +0100750 err = devm_gpio_request_one(&client->dev, eoc_gpio,
751 GPIOF_IN, "ak_8975");
Alan Cox01fbb472011-04-06 13:31:40 +0100752 if (err < 0) {
753 dev_err(&client->dev,
754 "failed to request GPIO %d, error %d\n",
Jonathan Cameron338473c2011-06-27 13:07:54 +0100755 eoc_gpio, err);
Beomho Seoa845a3a2014-06-23 13:34:00 +0100756 return err;
Alan Cox01fbb472011-04-06 13:31:40 +0100757 }
Stephen Warren7c6c9362011-09-21 11:16:00 +0100758 }
Andrew Chew3285aae2010-09-08 22:02:17 -0700759
Jonathan Cameron338473c2011-06-27 13:07:54 +0100760 /* Register with IIO */
Beomho Seoa845a3a2014-06-23 13:34:00 +0100761 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
762 if (indio_dev == NULL)
763 return -ENOMEM;
764
Jonathan Cameron338473c2011-06-27 13:07:54 +0100765 data = iio_priv(indio_dev);
Preetham Chandru40f32d92012-04-09 18:05:01 +0530766 i2c_set_clientdata(client, indio_dev);
Jacek Anaszewski94a6d5c2013-05-07 11:41:00 +0100767
768 data->client = client;
769 data->eoc_gpio = eoc_gpio;
770 data->eoc_irq = 0;
771
Srinivas Pandruvadad9139712014-03-19 16:56:00 +0000772 /* id will be NULL when enumerated via ACPI */
773 if (id) {
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800774 chipset = (enum asahi_compass_chipset)(id->driver_data);
Irina Tirdea48edc372014-08-09 15:18:00 +0100775 name = id->name;
Gregor Boirie55c0c532016-03-03 11:44:03 +0100776 } else if (ACPI_HANDLE(&client->dev)) {
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800777 name = ak8975_match_acpi_device(&client->dev, &chipset);
Gregor Boirie55c0c532016-03-03 11:44:03 +0100778 if (!name)
779 return -ENODEV;
780 }
Beomho Seoa845a3a2014-06-23 13:34:00 +0100781 else
782 return -ENOSYS;
783
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800784 if (chipset >= AK_MAX_TYPE) {
785 dev_err(&client->dev, "AKM device type unsupported: %d\n",
786 chipset);
787 return -ENODEV;
788 }
789
790 data->def = &ak_def_array[chipset];
Gwendal Grignou57e73a42014-11-21 10:45:49 -0800791 err = ak8975_who_i_am(client, data->def->type);
792 if (err < 0) {
793 dev_err(&client->dev, "Unexpected device\n");
794 return err;
795 }
Srinivas Pandruvadad9139712014-03-19 16:56:00 +0000796 dev_dbg(&client->dev, "Asahi compass chip %s\n", name);
Srinivas Pandruvada6027c072014-03-19 16:56:00 +0000797
Andrew Chew3285aae2010-09-08 22:02:17 -0700798 /* Perform some basic start-of-day setup of the device. */
799 err = ak8975_setup(client);
800 if (err < 0) {
Gwendal Grignou71222bf2014-11-21 10:45:47 -0800801 dev_err(&client->dev, "%s initialization fails\n", name);
Beomho Seoa845a3a2014-06-23 13:34:00 +0100802 return err;
Andrew Chew3285aae2010-09-08 22:02:17 -0700803 }
804
Jonathan Cameron338473c2011-06-27 13:07:54 +0100805 mutex_init(&data->lock);
Jonathan Cameron338473c2011-06-27 13:07:54 +0100806 indio_dev->dev.parent = &client->dev;
Jonathan Cameron694e1b52011-08-12 17:48:03 +0100807 indio_dev->channels = ak8975_channels;
808 indio_dev->num_channels = ARRAY_SIZE(ak8975_channels);
Jonathan Cameron338473c2011-06-27 13:07:54 +0100809 indio_dev->info = &ak8975_info;
810 indio_dev->modes = INDIO_DIRECT_MODE;
Srinivas Pandruvadad9139712014-03-19 16:56:00 +0000811 indio_dev->name = name;
Gwendal Grignou71222bf2014-11-21 10:45:47 -0800812 return devm_iio_device_register(&client->dev, indio_dev);
Andrew Chew3285aae2010-09-08 22:02:17 -0700813}
814
815static const struct i2c_device_id ak8975_id[] = {
Srinivas Pandruvada6027c072014-03-19 16:56:00 +0000816 {"ak8975", AK8975},
817 {"ak8963", AK8963},
Srinivas Pandruvada2d8339d2015-01-09 15:30:38 -0800818 {"AK8963", AK8963},
Gwendal Grignou57e73a42014-11-21 10:45:49 -0800819 {"ak09911", AK09911},
820 {"ak09912", AK09912},
Andrew Chew3285aae2010-09-08 22:02:17 -0700821 {}
822};
823
824MODULE_DEVICE_TABLE(i2c, ak8975_id);
825
Olof Johansson54461c32011-12-22 18:46:42 -0800826static const struct of_device_id ak8975_of_match[] = {
827 { .compatible = "asahi-kasei,ak8975", },
828 { .compatible = "ak8975", },
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800829 { .compatible = "asahi-kasei,ak8963", },
830 { .compatible = "ak8963", },
Gwendal Grignou57e73a42014-11-21 10:45:49 -0800831 { .compatible = "asahi-kasei,ak09911", },
832 { .compatible = "ak09911", },
833 { .compatible = "asahi-kasei,ak09912", },
834 { .compatible = "ak09912", },
Gwendal Grignou286f74c2014-11-21 10:45:48 -0800835 {}
Olof Johansson54461c32011-12-22 18:46:42 -0800836};
837MODULE_DEVICE_TABLE(of, ak8975_of_match);
838
Andrew Chew3285aae2010-09-08 22:02:17 -0700839static struct i2c_driver ak8975_driver = {
840 .driver = {
841 .name = "ak8975",
Gwendal Grignou71222bf2014-11-21 10:45:47 -0800842 .of_match_table = of_match_ptr(ak8975_of_match),
Srinivas Pandruvadad9139712014-03-19 16:56:00 +0000843 .acpi_match_table = ACPI_PTR(ak_acpi_match),
Andrew Chew3285aae2010-09-08 22:02:17 -0700844 },
845 .probe = ak8975_probe,
Andrew Chew3285aae2010-09-08 22:02:17 -0700846 .id_table = ak8975_id,
847};
Lars-Peter Clausen6e5af182011-11-16 10:13:38 +0100848module_i2c_driver(ak8975_driver);
Andrew Chew3285aae2010-09-08 22:02:17 -0700849
850MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
851MODULE_DESCRIPTION("AK8975 magnetometer driver");
852MODULE_LICENSE("GPL");