iio:core: mounting matrix support
[linux-2.6-block.git] / drivers / iio / magnetometer / ak8975.c
CommitLineData
3285aae1
AC
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>
94a6d5cf 27#include <linux/interrupt.h>
3285aae1
AC
28#include <linux/err.h>
29#include <linux/mutex.h>
30#include <linux/delay.h>
94a6d5cf 31#include <linux/bitops.h>
3285aae1 32#include <linux/gpio.h>
f4b7f751 33#include <linux/of_gpio.h>
d913971e 34#include <linux/acpi.h>
63d5d525 35#include <linux/regulator/consumer.h>
3285aae1 36
06458e27
JC
37#include <linux/iio/iio.h>
38#include <linux/iio/sysfs.h>
bc11ca4a
GB
39#include <linux/iio/buffer.h>
40#include <linux/iio/trigger.h>
41#include <linux/iio/trigger_consumer.h>
42#include <linux/iio/triggered_buffer.h>
43#include <linux/regulator/consumer.h>
44
3285aae1
AC
45/*
46 * Register definitions, as well as various shifts and masks to get at the
47 * individual fields of the registers.
48 */
49#define AK8975_REG_WIA 0x00
50#define AK8975_DEVICE_ID 0x48
51
52#define AK8975_REG_INFO 0x01
53
54#define AK8975_REG_ST1 0x02
55#define AK8975_REG_ST1_DRDY_SHIFT 0
56#define AK8975_REG_ST1_DRDY_MASK (1 << AK8975_REG_ST1_DRDY_SHIFT)
57
58#define AK8975_REG_HXL 0x03
59#define AK8975_REG_HXH 0x04
60#define AK8975_REG_HYL 0x05
61#define AK8975_REG_HYH 0x06
62#define AK8975_REG_HZL 0x07
63#define AK8975_REG_HZH 0x08
64#define AK8975_REG_ST2 0x09
65#define AK8975_REG_ST2_DERR_SHIFT 2
66#define AK8975_REG_ST2_DERR_MASK (1 << AK8975_REG_ST2_DERR_SHIFT)
67
68#define AK8975_REG_ST2_HOFL_SHIFT 3
69#define AK8975_REG_ST2_HOFL_MASK (1 << AK8975_REG_ST2_HOFL_SHIFT)
70
71#define AK8975_REG_CNTL 0x0A
72#define AK8975_REG_CNTL_MODE_SHIFT 0
73#define AK8975_REG_CNTL_MODE_MASK (0xF << AK8975_REG_CNTL_MODE_SHIFT)
71222bf5
GG
74#define AK8975_REG_CNTL_MODE_POWER_DOWN 0x00
75#define AK8975_REG_CNTL_MODE_ONCE 0x01
76#define AK8975_REG_CNTL_MODE_SELF_TEST 0x08
77#define AK8975_REG_CNTL_MODE_FUSE_ROM 0x0F
3285aae1
AC
78
79#define AK8975_REG_RSVC 0x0B
80#define AK8975_REG_ASTC 0x0C
81#define AK8975_REG_TS1 0x0D
82#define AK8975_REG_TS2 0x0E
83#define AK8975_REG_I2CDIS 0x0F
84#define AK8975_REG_ASAX 0x10
85#define AK8975_REG_ASAY 0x11
86#define AK8975_REG_ASAZ 0x12
87
88#define AK8975_MAX_REGS AK8975_REG_ASAZ
89
57e73a42
GG
90/*
91 * AK09912 Register definitions
92 */
93#define AK09912_REG_WIA1 0x00
94#define AK09912_REG_WIA2 0x01
95#define AK09912_DEVICE_ID 0x04
96#define AK09911_DEVICE_ID 0x05
97
98#define AK09911_REG_INFO1 0x02
99#define AK09911_REG_INFO2 0x03
100
101#define AK09912_REG_ST1 0x10
102
103#define AK09912_REG_ST1_DRDY_SHIFT 0
104#define AK09912_REG_ST1_DRDY_MASK (1 << AK09912_REG_ST1_DRDY_SHIFT)
105
106#define AK09912_REG_HXL 0x11
107#define AK09912_REG_HXH 0x12
108#define AK09912_REG_HYL 0x13
109#define AK09912_REG_HYH 0x14
110#define AK09912_REG_HZL 0x15
111#define AK09912_REG_HZH 0x16
112#define AK09912_REG_TMPS 0x17
113
114#define AK09912_REG_ST2 0x18
115#define AK09912_REG_ST2_HOFL_SHIFT 3
116#define AK09912_REG_ST2_HOFL_MASK (1 << AK09912_REG_ST2_HOFL_SHIFT)
117
118#define AK09912_REG_CNTL1 0x30
119
120#define AK09912_REG_CNTL2 0x31
121#define AK09912_REG_CNTL_MODE_POWER_DOWN 0x00
122#define AK09912_REG_CNTL_MODE_ONCE 0x01
123#define AK09912_REG_CNTL_MODE_SELF_TEST 0x10
124#define AK09912_REG_CNTL_MODE_FUSE_ROM 0x1F
125#define AK09912_REG_CNTL2_MODE_SHIFT 0
126#define AK09912_REG_CNTL2_MODE_MASK (0x1F << AK09912_REG_CNTL2_MODE_SHIFT)
127
128#define AK09912_REG_CNTL3 0x32
129
130#define AK09912_REG_TS1 0x33
131#define AK09912_REG_TS2 0x34
132#define AK09912_REG_TS3 0x35
133#define AK09912_REG_I2CDIS 0x36
134#define AK09912_REG_TS4 0x37
135
136#define AK09912_REG_ASAX 0x60
137#define AK09912_REG_ASAY 0x61
138#define AK09912_REG_ASAZ 0x62
139
140#define AK09912_MAX_REGS AK09912_REG_ASAZ
141
3285aae1
AC
142/*
143 * Miscellaneous values.
144 */
145#define AK8975_MAX_CONVERSION_TIMEOUT 500
146#define AK8975_CONVERSION_DONE_POLL_TIME 10
94a6d5cf 147#define AK8975_DATA_READY_TIMEOUT ((100*HZ)/1000)
286f74c2
GG
148
149/*
150 * Precalculate scale factor (in Gauss units) for each axis and
151 * store in the device data.
152 *
153 * This scale factor is axis-dependent, and is derived from 3 calibration
154 * factors ASA(x), ASA(y), and ASA(z).
155 *
156 * These ASA values are read from the sensor device at start of day, and
157 * cached in the device context struct.
158 *
159 * Adjusting the flux value with the sensitivity adjustment value should be
160 * done via the following formula:
161 *
162 * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
163 * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
164 * is the resultant adjusted value.
165 *
166 * We reduce the formula to:
167 *
168 * Hadj = H * (ASA + 128) / 256
169 *
170 * H is in the range of -4096 to 4095. The magnetometer has a range of
171 * +-1229uT. To go from the raw value to uT is:
172 *
173 * HuT = H * 1229/4096, or roughly, 3/10.
174 *
175 * Since 1uT = 0.01 gauss, our final scale factor becomes:
176 *
177 * Hadj = H * ((ASA + 128) / 256) * 3/10 * 1/100
178 * Hadj = H * ((ASA + 128) * 0.003) / 256
179 *
180 * Since ASA doesn't change, we cache the resultant scale factor into the
181 * device context in ak8975_setup().
182 *
183 * Given we use IIO_VAL_INT_PLUS_MICRO bit when displaying the scale, we
184 * multiply the stored scale value by 1e6.
185 */
186static long ak8975_raw_to_gauss(u16 data)
187{
188 return (((long)data + 128) * 3000) / 256;
189}
190
191/*
57e73a42 192 * For AK8963 and AK09911, same calculation, but the device is less sensitive:
286f74c2
GG
193 *
194 * H is in the range of +-8190. The magnetometer has a range of
195 * +-4912uT. To go from the raw value to uT is:
196 *
197 * HuT = H * 4912/8190, or roughly, 6/10, instead of 3/10.
198 */
57e73a42
GG
199
200static long ak8963_09911_raw_to_gauss(u16 data)
286f74c2
GG
201{
202 return (((long)data + 128) * 6000) / 256;
203}
6027c077 204
57e73a42
GG
205/*
206 * For AK09912, same calculation, except the device is more sensitive:
207 *
208 * H is in the range of -32752 to 32752. The magnetometer has a range of
209 * +-4912uT. To go from the raw value to uT is:
210 *
211 * HuT = H * 4912/32752, or roughly, 3/20, instead of 3/10.
212 */
213static long ak09912_raw_to_gauss(u16 data)
214{
215 return (((long)data + 128) * 1500) / 256;
216}
217
6027c077
SP
218/* Compatible Asahi Kasei Compass parts */
219enum asahi_compass_chipset {
220 AK8975,
221 AK8963,
57e73a42
GG
222 AK09911,
223 AK09912,
286f74c2
GG
224 AK_MAX_TYPE
225};
226
227enum ak_ctrl_reg_addr {
228 ST1,
229 ST2,
230 CNTL,
231 ASA_BASE,
232 MAX_REGS,
233 REGS_END,
234};
235
236enum ak_ctrl_reg_mask {
237 ST1_DRDY,
238 ST2_HOFL,
239 ST2_DERR,
240 CNTL_MODE,
241 MASK_END,
242};
243
244enum ak_ctrl_mode {
245 POWER_DOWN,
246 MODE_ONCE,
247 SELF_TEST,
248 FUSE_ROM,
249 MODE_END,
250};
251
252struct ak_def {
253 enum asahi_compass_chipset type;
254 long (*raw_to_gauss)(u16 data);
255 u16 range;
256 u8 ctrl_regs[REGS_END];
257 u8 ctrl_masks[MASK_END];
258 u8 ctrl_modes[MODE_END];
259 u8 data_regs[3];
260};
261
f78c5f96 262static const struct ak_def ak_def_array[AK_MAX_TYPE] = {
286f74c2
GG
263 {
264 .type = AK8975,
265 .raw_to_gauss = ak8975_raw_to_gauss,
266 .range = 4096,
267 .ctrl_regs = {
268 AK8975_REG_ST1,
269 AK8975_REG_ST2,
270 AK8975_REG_CNTL,
271 AK8975_REG_ASAX,
272 AK8975_MAX_REGS},
273 .ctrl_masks = {
274 AK8975_REG_ST1_DRDY_MASK,
275 AK8975_REG_ST2_HOFL_MASK,
276 AK8975_REG_ST2_DERR_MASK,
277 AK8975_REG_CNTL_MODE_MASK},
278 .ctrl_modes = {
279 AK8975_REG_CNTL_MODE_POWER_DOWN,
280 AK8975_REG_CNTL_MODE_ONCE,
281 AK8975_REG_CNTL_MODE_SELF_TEST,
282 AK8975_REG_CNTL_MODE_FUSE_ROM},
283 .data_regs = {
284 AK8975_REG_HXL,
285 AK8975_REG_HYL,
286 AK8975_REG_HZL},
287 },
288 {
289 .type = AK8963,
57e73a42 290 .raw_to_gauss = ak8963_09911_raw_to_gauss,
286f74c2
GG
291 .range = 8190,
292 .ctrl_regs = {
293 AK8975_REG_ST1,
294 AK8975_REG_ST2,
295 AK8975_REG_CNTL,
296 AK8975_REG_ASAX,
297 AK8975_MAX_REGS},
298 .ctrl_masks = {
299 AK8975_REG_ST1_DRDY_MASK,
300 AK8975_REG_ST2_HOFL_MASK,
301 0,
302 AK8975_REG_CNTL_MODE_MASK},
303 .ctrl_modes = {
304 AK8975_REG_CNTL_MODE_POWER_DOWN,
305 AK8975_REG_CNTL_MODE_ONCE,
306 AK8975_REG_CNTL_MODE_SELF_TEST,
307 AK8975_REG_CNTL_MODE_FUSE_ROM},
308 .data_regs = {
309 AK8975_REG_HXL,
310 AK8975_REG_HYL,
311 AK8975_REG_HZL},
312 },
57e73a42
GG
313 {
314 .type = AK09911,
315 .raw_to_gauss = ak8963_09911_raw_to_gauss,
316 .range = 8192,
317 .ctrl_regs = {
318 AK09912_REG_ST1,
319 AK09912_REG_ST2,
320 AK09912_REG_CNTL2,
321 AK09912_REG_ASAX,
322 AK09912_MAX_REGS},
323 .ctrl_masks = {
324 AK09912_REG_ST1_DRDY_MASK,
325 AK09912_REG_ST2_HOFL_MASK,
326 0,
327 AK09912_REG_CNTL2_MODE_MASK},
328 .ctrl_modes = {
329 AK09912_REG_CNTL_MODE_POWER_DOWN,
330 AK09912_REG_CNTL_MODE_ONCE,
331 AK09912_REG_CNTL_MODE_SELF_TEST,
332 AK09912_REG_CNTL_MODE_FUSE_ROM},
333 .data_regs = {
334 AK09912_REG_HXL,
335 AK09912_REG_HYL,
336 AK09912_REG_HZL},
337 },
338 {
339 .type = AK09912,
340 .raw_to_gauss = ak09912_raw_to_gauss,
341 .range = 32752,
342 .ctrl_regs = {
343 AK09912_REG_ST1,
344 AK09912_REG_ST2,
345 AK09912_REG_CNTL2,
346 AK09912_REG_ASAX,
347 AK09912_MAX_REGS},
348 .ctrl_masks = {
349 AK09912_REG_ST1_DRDY_MASK,
350 AK09912_REG_ST2_HOFL_MASK,
351 0,
352 AK09912_REG_CNTL2_MODE_MASK},
353 .ctrl_modes = {
354 AK09912_REG_CNTL_MODE_POWER_DOWN,
355 AK09912_REG_CNTL_MODE_ONCE,
356 AK09912_REG_CNTL_MODE_SELF_TEST,
357 AK09912_REG_CNTL_MODE_FUSE_ROM},
358 .data_regs = {
359 AK09912_REG_HXL,
360 AK09912_REG_HYL,
361 AK09912_REG_HZL},
362 }
6027c077 363};
3285aae1
AC
364
365/*
366 * Per-instance context data for the device.
367 */
368struct ak8975_data {
369 struct i2c_client *client;
f78c5f96 370 const struct ak_def *def;
3285aae1
AC
371 struct mutex lock;
372 u8 asa[3];
373 long raw_to_gauss[3];
3285aae1 374 int eoc_gpio;
94a6d5cf
JA
375 int eoc_irq;
376 wait_queue_head_t data_ready_queue;
377 unsigned long flags;
286f74c2 378 u8 cntl_cache;
a9b72c90 379 struct regulator *vdd;
694e1b5f
JC
380};
381
a9b72c90
GB
382/* Enable attached power regulator if any. */
383static int ak8975_power_on(struct i2c_client *client)
384{
385 const struct iio_dev *indio_dev = i2c_get_clientdata(client);
386 struct ak8975_data *data = iio_priv(indio_dev);
387 int ret;
388
389 data->vdd = devm_regulator_get(&client->dev, "vdd");
390 if (IS_ERR_OR_NULL(data->vdd)) {
391 ret = PTR_ERR(data->vdd);
392 if (ret == -ENODEV)
393 ret = 0;
394 } else {
395 ret = regulator_enable(data->vdd);
396 }
397
398 if (ret)
399 dev_err(&client->dev, "failed to enable Vdd supply: %d\n", ret);
400 return ret;
401}
402
403/* Disable attached power regulator if any. */
404static void ak8975_power_off(const struct i2c_client *client)
405{
406 const struct iio_dev *indio_dev = i2c_get_clientdata(client);
407 const struct ak8975_data *data = iio_priv(indio_dev);
408
409 if (!IS_ERR_OR_NULL(data->vdd))
410 regulator_disable(data->vdd);
411}
412
57e73a42
GG
413/*
414 * Return 0 if the i2c device is the one we expect.
415 * return a negative error number otherwise
416 */
417static int ak8975_who_i_am(struct i2c_client *client,
418 enum asahi_compass_chipset type)
419{
420 u8 wia_val[2];
421 int ret;
422
423 /*
424 * Signature for each device:
425 * Device | WIA1 | WIA2
426 * AK09912 | DEVICE_ID | AK09912_DEVICE_ID
427 * AK09911 | DEVICE_ID | AK09911_DEVICE_ID
428 * AK8975 | DEVICE_ID | NA
429 * AK8963 | DEVICE_ID | NA
430 */
431 ret = i2c_smbus_read_i2c_block_data(client, AK09912_REG_WIA1,
432 2, wia_val);
433 if (ret < 0) {
434 dev_err(&client->dev, "Error reading WIA\n");
435 return ret;
436 }
437
438 if (wia_val[0] != AK8975_DEVICE_ID)
439 return -ENODEV;
440
441 switch (type) {
442 case AK8975:
443 case AK8963:
444 return 0;
445 case AK09911:
446 if (wia_val[1] == AK09911_DEVICE_ID)
447 return 0;
448 break;
449 case AK09912:
450 if (wia_val[1] == AK09912_DEVICE_ID)
451 return 0;
452 break;
453 default:
454 dev_err(&client->dev, "Type %d unknown\n", type);
455 }
456 return -ENODEV;
457}
458
3285aae1 459/*
286f74c2 460 * Helper function to write to CNTL register.
3285aae1 461 */
286f74c2 462static int ak8975_set_mode(struct ak8975_data *data, enum ak_ctrl_mode mode)
3285aae1 463{
694e1b5f
JC
464 u8 regval;
465 int ret;
3285aae1 466
286f74c2
GG
467 regval = (data->cntl_cache & ~data->def->ctrl_masks[CNTL_MODE]) |
468 data->def->ctrl_modes[mode];
469 ret = i2c_smbus_write_byte_data(data->client,
470 data->def->ctrl_regs[CNTL], regval);
3285aae1 471 if (ret < 0) {
3285aae1
AC
472 return ret;
473 }
286f74c2
GG
474 data->cntl_cache = regval;
475 /* After mode change wait atleast 100us */
476 usleep_range(100, 500);
3285aae1
AC
477
478 return 0;
479}
480
94a6d5cf
JA
481/*
482 * Handle data ready irq
483 */
484static irqreturn_t ak8975_irq_handler(int irq, void *data)
485{
486 struct ak8975_data *ak8975 = data;
487
488 set_bit(0, &ak8975->flags);
489 wake_up(&ak8975->data_ready_queue);
490
491 return IRQ_HANDLED;
492}
493
494/*
495 * Install data ready interrupt handler
496 */
497static int ak8975_setup_irq(struct ak8975_data *data)
498{
499 struct i2c_client *client = data->client;
500 int rc;
501 int irq;
502
503 if (client->irq)
504 irq = client->irq;
505 else
506 irq = gpio_to_irq(data->eoc_gpio);
507
a845a3aa 508 rc = devm_request_irq(&client->dev, irq, ak8975_irq_handler,
71222bf5
GG
509 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
510 dev_name(&client->dev), data);
94a6d5cf
JA
511 if (rc < 0) {
512 dev_err(&client->dev,
513 "irq %d request failed, (gpio %d): %d\n",
514 irq, data->eoc_gpio, rc);
515 return rc;
516 }
517
518 init_waitqueue_head(&data->data_ready_queue);
519 clear_bit(0, &data->flags);
520 data->eoc_irq = irq;
521
522 return rc;
523}
524
525
3285aae1
AC
526/*
527 * Perform some start-of-day setup, including reading the asa calibration
528 * values and caching them.
529 */
530static int ak8975_setup(struct i2c_client *client)
531{
40f32d93
PC
532 struct iio_dev *indio_dev = i2c_get_clientdata(client);
533 struct ak8975_data *data = iio_priv(indio_dev);
3285aae1
AC
534 int ret;
535
3285aae1 536 /* Write the fused rom access mode. */
286f74c2 537 ret = ak8975_set_mode(data, FUSE_ROM);
3285aae1
AC
538 if (ret < 0) {
539 dev_err(&client->dev, "Error in setting fuse access mode\n");
540 return ret;
541 }
542
543 /* Get asa data and store in the device data. */
286f74c2
GG
544 ret = i2c_smbus_read_i2c_block_data(client,
545 data->def->ctrl_regs[ASA_BASE],
c411f600 546 3, data->asa);
3285aae1
AC
547 if (ret < 0) {
548 dev_err(&client->dev, "Not able to read asa data\n");
549 return ret;
550 }
551
040f3e58 552 /* After reading fuse ROM data set power-down mode */
286f74c2 553 ret = ak8975_set_mode(data, POWER_DOWN);
71222bf5
GG
554 if (ret < 0) {
555 dev_err(&client->dev, "Error in setting power-down mode\n");
556 return ret;
557 }
94a6d5cf 558
71222bf5 559 if (data->eoc_gpio > 0 || client->irq > 0) {
94a6d5cf
JA
560 ret = ak8975_setup_irq(data);
561 if (ret < 0) {
562 dev_err(&client->dev,
563 "Error setting data ready interrupt\n");
564 return ret;
565 }
566 }
567
286f74c2
GG
568 data->raw_to_gauss[0] = data->def->raw_to_gauss(data->asa[0]);
569 data->raw_to_gauss[1] = data->def->raw_to_gauss(data->asa[1]);
570 data->raw_to_gauss[2] = data->def->raw_to_gauss(data->asa[2]);
3285aae1
AC
571
572 return 0;
573}
574
01fbb478
AC
575static int wait_conversion_complete_gpio(struct ak8975_data *data)
576{
577 struct i2c_client *client = data->client;
01fbb478
AC
578 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
579 int ret;
580
581 /* Wait for the conversion to complete. */
582 while (timeout_ms) {
583 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
584 if (gpio_get_value(data->eoc_gpio))
585 break;
586 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
587 }
588 if (!timeout_ms) {
589 dev_err(&client->dev, "Conversion timeout happened\n");
590 return -EINVAL;
591 }
592
286f74c2 593 ret = i2c_smbus_read_byte_data(client, data->def->ctrl_regs[ST1]);
c411f600 594 if (ret < 0)
01fbb478 595 dev_err(&client->dev, "Error in reading ST1\n");
c411f600
JC
596
597 return ret;
01fbb478
AC
598}
599
600static int wait_conversion_complete_polled(struct ak8975_data *data)
601{
602 struct i2c_client *client = data->client;
603 u8 read_status;
604 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
605 int ret;
606
607 /* Wait for the conversion to complete. */
608 while (timeout_ms) {
609 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
286f74c2
GG
610 ret = i2c_smbus_read_byte_data(client,
611 data->def->ctrl_regs[ST1]);
01fbb478
AC
612 if (ret < 0) {
613 dev_err(&client->dev, "Error in reading ST1\n");
614 return ret;
615 }
c411f600 616 read_status = ret;
01fbb478
AC
617 if (read_status)
618 break;
619 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
620 }
621 if (!timeout_ms) {
622 dev_err(&client->dev, "Conversion timeout happened\n");
623 return -EINVAL;
624 }
94a6d5cf 625
01fbb478
AC
626 return read_status;
627}
628
94a6d5cf
JA
629/* Returns 0 if the end of conversion interrupt occured or -ETIME otherwise */
630static int wait_conversion_complete_interrupt(struct ak8975_data *data)
631{
632 int ret;
633
634 ret = wait_event_timeout(data->data_ready_queue,
635 test_bit(0, &data->flags),
636 AK8975_DATA_READY_TIMEOUT);
637 clear_bit(0, &data->flags);
638
639 return ret > 0 ? 0 : -ETIME;
640}
641
bc11ca4a
GB
642static int ak8975_start_read_axis(struct ak8975_data *data,
643 const struct i2c_client *client)
3285aae1 644{
3285aae1 645 /* Set up the device for taking a sample. */
bc11ca4a
GB
646 int ret = ak8975_set_mode(data, MODE_ONCE);
647
3285aae1
AC
648 if (ret < 0) {
649 dev_err(&client->dev, "Error in setting operating mode\n");
bc11ca4a 650 return ret;
3285aae1
AC
651 }
652
653 /* Wait for the conversion to complete. */
94a6d5cf
JA
654 if (data->eoc_irq)
655 ret = wait_conversion_complete_interrupt(data);
656 else if (gpio_is_valid(data->eoc_gpio))
01fbb478
AC
657 ret = wait_conversion_complete_gpio(data);
658 else
659 ret = wait_conversion_complete_polled(data);
660 if (ret < 0)
bc11ca4a 661 return ret;
3285aae1 662
94a6d5cf 663 /* This will be executed only for non-interrupt based waiting case */
286f74c2
GG
664 if (ret & data->def->ctrl_masks[ST1_DRDY]) {
665 ret = i2c_smbus_read_byte_data(client,
666 data->def->ctrl_regs[ST2]);
3285aae1
AC
667 if (ret < 0) {
668 dev_err(&client->dev, "Error in reading ST2\n");
bc11ca4a 669 return ret;
3285aae1 670 }
286f74c2
GG
671 if (ret & (data->def->ctrl_masks[ST2_DERR] |
672 data->def->ctrl_masks[ST2_HOFL])) {
c411f600 673 dev_err(&client->dev, "ST2 status error 0x%x\n", ret);
bc11ca4a 674 return -EINVAL;
3285aae1
AC
675 }
676 }
677
bc11ca4a
GB
678 return 0;
679}
680
681/* Retrieve raw flux value for one of the x, y, or z axis. */
682static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
683{
684 struct ak8975_data *data = iio_priv(indio_dev);
685 const struct i2c_client *client = data->client;
686 const struct ak_def *def = data->def;
687 int ret;
688
689 mutex_lock(&data->lock);
690
691 ret = ak8975_start_read_axis(data, client);
692 if (ret)
693 goto exit;
694
695 ret = i2c_smbus_read_word_data(client, def->data_regs[index]);
696 if (ret < 0)
3285aae1 697 goto exit;
3285aae1
AC
698
699 mutex_unlock(&data->lock);
700
3285aae1 701 /* Clamp to valid range. */
bc11ca4a 702 *val = clamp_t(s16, ret, -def->range, def->range);
694e1b5f 703 return IIO_VAL_INT;
3285aae1
AC
704
705exit:
706 mutex_unlock(&data->lock);
bc11ca4a 707 dev_err(&client->dev, "Error in reading axis\n");
3285aae1
AC
708 return ret;
709}
710
694e1b5f
JC
711static int ak8975_read_raw(struct iio_dev *indio_dev,
712 struct iio_chan_spec const *chan,
713 int *val, int *val2,
714 long mask)
715{
716 struct ak8975_data *data = iio_priv(indio_dev);
717
718 switch (mask) {
4d9948b3 719 case IIO_CHAN_INFO_RAW:
694e1b5f 720 return ak8975_read_axis(indio_dev, chan->address, val);
c8a9f805 721 case IIO_CHAN_INFO_SCALE:
bef44abc
BS
722 *val = 0;
723 *val2 = data->raw_to_gauss[chan->address];
724 return IIO_VAL_INT_PLUS_MICRO;
694e1b5f
JC
725 }
726 return -EINVAL;
727}
728
729#define AK8975_CHANNEL(axis, index) \
730 { \
731 .type = IIO_MAGN, \
732 .modified = 1, \
733 .channel2 = IIO_MOD_##axis, \
3a0b4422
JC
734 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
735 BIT(IIO_CHAN_INFO_SCALE), \
694e1b5f 736 .address = index, \
bc11ca4a
GB
737 .scan_index = index, \
738 .scan_type = { \
739 .sign = 's', \
740 .realbits = 16, \
741 .storagebits = 16, \
742 .endianness = IIO_CPU \
743 } \
694e1b5f
JC
744 }
745
746static const struct iio_chan_spec ak8975_channels[] = {
747 AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2),
bc11ca4a 748 IIO_CHAN_SOFT_TIMESTAMP(3),
694e1b5f
JC
749};
750
bc11ca4a
GB
751static const unsigned long ak8975_scan_masks[] = { 0x7, 0 };
752
6fe8135f 753static const struct iio_info ak8975_info = {
694e1b5f 754 .read_raw = &ak8975_read_raw,
6fe8135f
JC
755 .driver_module = THIS_MODULE,
756};
757
d913971e
SP
758static const struct acpi_device_id ak_acpi_match[] = {
759 {"AK8975", AK8975},
760 {"AK8963", AK8963},
761 {"INVN6500", AK8963},
57e73a42
GG
762 {"AK09911", AK09911},
763 {"AK09912", AK09912},
d913971e
SP
764 { },
765};
766MODULE_DEVICE_TABLE(acpi, ak_acpi_match);
767
48edc374
IT
768static const char *ak8975_match_acpi_device(struct device *dev,
769 enum asahi_compass_chipset *chipset)
d913971e
SP
770{
771 const struct acpi_device_id *id;
772
773 id = acpi_match_device(dev->driver->acpi_match_table, dev);
774 if (!id)
775 return NULL;
776 *chipset = (int)id->driver_data;
777
48edc374 778 return dev_name(dev);
d913971e
SP
779}
780
bc11ca4a
GB
781static void ak8975_fill_buffer(struct iio_dev *indio_dev)
782{
783 struct ak8975_data *data = iio_priv(indio_dev);
784 const struct i2c_client *client = data->client;
785 const struct ak_def *def = data->def;
786 int ret;
787 s16 buff[8]; /* 3 x 16 bits axis values + 1 aligned 64 bits timestamp */
788
789 mutex_lock(&data->lock);
790
791 ret = ak8975_start_read_axis(data, client);
792 if (ret)
793 goto unlock;
794
795 /*
796 * For each axis, read the flux value from the appropriate register
797 * (the register is specified in the iio device attributes).
798 */
799 ret = i2c_smbus_read_i2c_block_data_or_emulated(client,
800 def->data_regs[0],
801 3 * sizeof(buff[0]),
802 (u8 *)buff);
803 if (ret < 0)
804 goto unlock;
805
806 mutex_unlock(&data->lock);
807
808 /* Clamp to valid range. */
809 buff[0] = clamp_t(s16, le16_to_cpu(buff[0]), -def->range, def->range);
810 buff[1] = clamp_t(s16, le16_to_cpu(buff[1]), -def->range, def->range);
811 buff[2] = clamp_t(s16, le16_to_cpu(buff[2]), -def->range, def->range);
812
813 iio_push_to_buffers_with_timestamp(indio_dev, buff, iio_get_time_ns());
814 return;
815
816unlock:
817 mutex_unlock(&data->lock);
818 dev_err(&client->dev, "Error in reading axes block\n");
819}
820
821static irqreturn_t ak8975_handle_trigger(int irq, void *p)
822{
823 const struct iio_poll_func *pf = p;
824 struct iio_dev *indio_dev = pf->indio_dev;
825
826 ak8975_fill_buffer(indio_dev);
827 iio_trigger_notify_done(indio_dev->trig);
828 return IRQ_HANDLED;
829}
830
4ae1c61f 831static int ak8975_probe(struct i2c_client *client,
3285aae1
AC
832 const struct i2c_device_id *id)
833{
834 struct ak8975_data *data;
338473c8
JC
835 struct iio_dev *indio_dev;
836 int eoc_gpio;
3285aae1 837 int err;
48edc374 838 const char *name = NULL;
286f74c2 839 enum asahi_compass_chipset chipset;
3285aae1 840
3285aae1 841 /* Grab and set up the supplied GPIO. */
f4b7f751 842 if (client->dev.platform_data)
f6d838d7 843 eoc_gpio = *(int *)(client->dev.platform_data);
f4b7f751
JA
844 else if (client->dev.of_node)
845 eoc_gpio = of_get_gpio(client->dev.of_node, 0);
846 else
847 eoc_gpio = -1;
848
849 if (eoc_gpio == -EPROBE_DEFER)
850 return -EPROBE_DEFER;
3285aae1 851
01fbb478
AC
852 /* We may not have a GPIO based IRQ to scan, that is fine, we will
853 poll if so */
7c6c9368 854 if (gpio_is_valid(eoc_gpio)) {
a845a3aa
BS
855 err = devm_gpio_request_one(&client->dev, eoc_gpio,
856 GPIOF_IN, "ak_8975");
01fbb478
AC
857 if (err < 0) {
858 dev_err(&client->dev,
859 "failed to request GPIO %d, error %d\n",
338473c8 860 eoc_gpio, err);
a845a3aa 861 return err;
01fbb478 862 }
7c6c9368 863 }
3285aae1 864
338473c8 865 /* Register with IIO */
a845a3aa
BS
866 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
867 if (indio_dev == NULL)
868 return -ENOMEM;
869
338473c8 870 data = iio_priv(indio_dev);
40f32d93 871 i2c_set_clientdata(client, indio_dev);
94a6d5cf
JA
872
873 data->client = client;
874 data->eoc_gpio = eoc_gpio;
875 data->eoc_irq = 0;
876
d913971e
SP
877 /* id will be NULL when enumerated via ACPI */
878 if (id) {
286f74c2 879 chipset = (enum asahi_compass_chipset)(id->driver_data);
48edc374 880 name = id->name;
55c0c530 881 } else if (ACPI_HANDLE(&client->dev)) {
286f74c2 882 name = ak8975_match_acpi_device(&client->dev, &chipset);
55c0c530
GB
883 if (!name)
884 return -ENODEV;
26b89d7d 885 } else
a845a3aa
BS
886 return -ENOSYS;
887
286f74c2
GG
888 if (chipset >= AK_MAX_TYPE) {
889 dev_err(&client->dev, "AKM device type unsupported: %d\n",
890 chipset);
891 return -ENODEV;
892 }
893
894 data->def = &ak_def_array[chipset];
a9b72c90
GB
895
896 err = ak8975_power_on(client);
897 if (err)
898 return err;
899
57e73a42
GG
900 err = ak8975_who_i_am(client, data->def->type);
901 if (err < 0) {
902 dev_err(&client->dev, "Unexpected device\n");
a9b72c90 903 goto power_off;
57e73a42 904 }
d913971e 905 dev_dbg(&client->dev, "Asahi compass chip %s\n", name);
6027c077 906
3285aae1
AC
907 /* Perform some basic start-of-day setup of the device. */
908 err = ak8975_setup(client);
909 if (err < 0) {
71222bf5 910 dev_err(&client->dev, "%s initialization fails\n", name);
a9b72c90 911 goto power_off;
3285aae1
AC
912 }
913
338473c8 914 mutex_init(&data->lock);
338473c8 915 indio_dev->dev.parent = &client->dev;
694e1b5f
JC
916 indio_dev->channels = ak8975_channels;
917 indio_dev->num_channels = ARRAY_SIZE(ak8975_channels);
338473c8 918 indio_dev->info = &ak8975_info;
bc11ca4a 919 indio_dev->available_scan_masks = ak8975_scan_masks;
338473c8 920 indio_dev->modes = INDIO_DIRECT_MODE;
d913971e 921 indio_dev->name = name;
a9b72c90 922
bc11ca4a
GB
923 err = iio_triggered_buffer_setup(indio_dev, NULL, ak8975_handle_trigger,
924 NULL);
925 if (err) {
926 dev_err(&client->dev, "triggered buffer setup failed\n");
a9b72c90 927 goto power_off;
bc11ca4a
GB
928 }
929
930 err = iio_device_register(indio_dev);
931 if (err) {
932 dev_err(&client->dev, "device register failed\n");
933 goto cleanup_buffer;
934 }
a9b72c90
GB
935
936 return 0;
937
bc11ca4a
GB
938cleanup_buffer:
939 iio_triggered_buffer_cleanup(indio_dev);
a9b72c90
GB
940power_off:
941 ak8975_power_off(client);
942 return err;
943}
944
945static int ak8975_remove(struct i2c_client *client)
946{
947 struct iio_dev *indio_dev = i2c_get_clientdata(client);
948
949 iio_device_unregister(indio_dev);
bc11ca4a 950 iio_triggered_buffer_cleanup(indio_dev);
a9b72c90
GB
951 ak8975_power_off(client);
952
953 return 0;
3285aae1
AC
954}
955
956static const struct i2c_device_id ak8975_id[] = {
6027c077
SP
957 {"ak8975", AK8975},
958 {"ak8963", AK8963},
2d8339d0 959 {"AK8963", AK8963},
57e73a42
GG
960 {"ak09911", AK09911},
961 {"ak09912", AK09912},
3285aae1
AC
962 {}
963};
964
965MODULE_DEVICE_TABLE(i2c, ak8975_id);
966
54461c30
OJ
967static const struct of_device_id ak8975_of_match[] = {
968 { .compatible = "asahi-kasei,ak8975", },
969 { .compatible = "ak8975", },
286f74c2
GG
970 { .compatible = "asahi-kasei,ak8963", },
971 { .compatible = "ak8963", },
57e73a42
GG
972 { .compatible = "asahi-kasei,ak09911", },
973 { .compatible = "ak09911", },
974 { .compatible = "asahi-kasei,ak09912", },
975 { .compatible = "ak09912", },
286f74c2 976 {}
54461c30
OJ
977};
978MODULE_DEVICE_TABLE(of, ak8975_of_match);
979
3285aae1
AC
980static struct i2c_driver ak8975_driver = {
981 .driver = {
982 .name = "ak8975",
71222bf5 983 .of_match_table = of_match_ptr(ak8975_of_match),
d913971e 984 .acpi_match_table = ACPI_PTR(ak_acpi_match),
3285aae1
AC
985 },
986 .probe = ak8975_probe,
a9b72c90 987 .remove = ak8975_remove,
3285aae1
AC
988 .id_table = ak8975_id,
989};
6e5af184 990module_i2c_driver(ak8975_driver);
3285aae1
AC
991
992MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
993MODULE_DESCRIPTION("AK8975 magnetometer driver");
994MODULE_LICENSE("GPL");