xref: /linux/drivers/iio/magnetometer/ak8975.c (revision f68afce8e8a74f52a879f56f607dfedf552b5ab5)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * A sensor driver for the magnetometer AK8975.
4  *
5  * Magnetic compass sensor driver for monitoring magnetic flux information.
6  *
7  * Copyright (c) 2010, NVIDIA Corporation.
8  */
9 
10 #include <linux/array_size.h>
11 #include <linux/bitops.h>
12 #include <linux/delay.h>
13 #include <linux/dev_printk.h>
14 #include <linux/err.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/i2c.h>
17 #include <linux/interrupt.h>
18 #include <linux/iopoll.h>
19 #include <linux/jiffies.h>
20 #include <linux/minmax.h>
21 #include <linux/mod_devicetable.h>
22 #include <linux/module.h>
23 #include <linux/mutex.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/property.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/time.h>
28 #include <linux/types.h>
29 #include <linux/wait.h>
30 
31 #include <asm/byteorder.h>
32 
33 #include <linux/iio/buffer.h>
34 #include <linux/iio/iio.h>
35 #include <linux/iio/trigger_consumer.h>
36 #include <linux/iio/triggered_buffer.h>
37 
38 /*
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)
67 #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
71 
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 /*
84  * AK09912 Register definitions
85  */
86 #define AK09912_REG_WIA1		0x00
87 #define AK09912_REG_WIA2		0x01
88 #define AK09918_DEVICE_ID		0x0C
89 #define AK09916_DEVICE_ID		0x09
90 #define AK09912_DEVICE_ID		0x04
91 #define AK09911_DEVICE_ID		0x05
92 
93 #define AK09911_REG_INFO1		0x02
94 #define AK09911_REG_INFO2		0x03
95 
96 #define AK09912_REG_ST1			0x10
97 
98 #define AK09912_REG_ST1_DRDY_SHIFT	0
99 #define AK09912_REG_ST1_DRDY_MASK	(1 << AK09912_REG_ST1_DRDY_SHIFT)
100 
101 #define AK09912_REG_HXL			0x11
102 #define AK09912_REG_HXH			0x12
103 #define AK09912_REG_HYL			0x13
104 #define AK09912_REG_HYH			0x14
105 #define AK09912_REG_HZL			0x15
106 #define AK09912_REG_HZH			0x16
107 #define AK09912_REG_TMPS		0x17
108 
109 #define AK09912_REG_ST2			0x18
110 #define AK09912_REG_ST2_HOFL_SHIFT	3
111 #define AK09912_REG_ST2_HOFL_MASK	(1 << AK09912_REG_ST2_HOFL_SHIFT)
112 
113 #define AK09912_REG_CNTL1		0x30
114 
115 #define AK09912_REG_CNTL2		0x31
116 #define AK09912_REG_CNTL_MODE_POWER_DOWN	0x00
117 #define AK09912_REG_CNTL_MODE_ONCE	0x01
118 #define AK09912_REG_CNTL_MODE_SELF_TEST	0x10
119 #define AK09912_REG_CNTL_MODE_FUSE_ROM	0x1F
120 #define AK09912_REG_CNTL2_MODE_SHIFT	0
121 #define AK09912_REG_CNTL2_MODE_MASK	(0x1F << AK09912_REG_CNTL2_MODE_SHIFT)
122 
123 #define AK09912_REG_CNTL3		0x32
124 
125 #define AK09912_REG_TS1			0x33
126 #define AK09912_REG_TS2			0x34
127 #define AK09912_REG_TS3			0x35
128 #define AK09912_REG_I2CDIS		0x36
129 #define AK09912_REG_TS4			0x37
130 
131 #define AK09912_REG_ASAX		0x60
132 #define AK09912_REG_ASAY		0x61
133 #define AK09912_REG_ASAZ		0x62
134 
135 #define AK09912_MAX_REGS		AK09912_REG_ASAZ
136 
137 /*
138  * Precalculate scale factor (in Gauss units) for each axis and
139  * store in the device data.
140  *
141  * This scale factor is axis-dependent, and is derived from 3 calibration
142  * factors ASA(x), ASA(y), and ASA(z).
143  *
144  * These ASA values are read from the sensor device at start of day, and
145  * cached in the device context struct.
146  *
147  * Adjusting the flux value with the sensitivity adjustment value should be
148  * done via the following formula:
149  *
150  * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
151  * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
152  * is the resultant adjusted value.
153  *
154  * We reduce the formula to:
155  *
156  * Hadj = H * (ASA + 128) / 256
157  *
158  * H is in the range of -4096 to 4095.  The magnetometer has a range of
159  * +-1229uT.  To go from the raw value to uT is:
160  *
161  * HuT = H * 1229/4096, or roughly, 3/10.
162  *
163  * Since 1uT = 0.01 gauss, our final scale factor becomes:
164  *
165  * Hadj = H * ((ASA + 128) / 256) * 3/10 * 1/100
166  * Hadj = H * ((ASA + 128) * 0.003) / 256
167  *
168  * Since ASA doesn't change, we cache the resultant scale factor into the
169  * device context in ak8975_setup().
170  *
171  * Given we use IIO_VAL_INT_PLUS_MICRO bit when displaying the scale, we
172  * multiply the stored scale value by 1e6.
173  */
174 static long ak8975_raw_to_gauss(u16 data)
175 {
176 	return (((long)data + 128) * 3000) / 256;
177 }
178 
179 /*
180  * For AK8963 and AK09911, same calculation, but the device is less sensitive:
181  *
182  * H is in the range of +-8190.  The magnetometer has a range of
183  * +-4912uT.  To go from the raw value to uT is:
184  *
185  * HuT = H * 4912/8190, or roughly, 6/10, instead of 3/10.
186  */
187 
188 static long ak8963_09911_raw_to_gauss(u16 data)
189 {
190 	return (((long)data + 128) * 6000) / 256;
191 }
192 
193 /*
194  * For AK09912, same calculation, except the device is more sensitive:
195  *
196  * H is in the range of -32752 to 32752.  The magnetometer has a range of
197  * +-4912uT.  To go from the raw value to uT is:
198  *
199  * HuT = H * 4912/32752, or roughly, 3/20, instead of 3/10.
200  */
201 static long ak09912_raw_to_gauss(u16 data)
202 {
203 	return (((long)data + 128) * 1500) / 256;
204 }
205 
206 /* Compatible Asahi Kasei Compass parts */
207 enum asahi_compass_chipset {
208 	AK8975,
209 	AK8963,
210 	AK09911,
211 	AK09912,
212 	AK09916,
213 	AK09918,
214 };
215 
216 enum ak_ctrl_reg_addr {
217 	ST1,
218 	ST2,
219 	CNTL,
220 	ASA_BASE,
221 	MAX_REGS,
222 	REGS_END,
223 };
224 
225 enum ak_ctrl_reg_mask {
226 	ST1_DRDY,
227 	ST2_HOFL,
228 	ST2_DERR,
229 	CNTL_MODE,
230 	MASK_END,
231 };
232 
233 enum ak_ctrl_mode {
234 	POWER_DOWN,
235 	MODE_ONCE,
236 	SELF_TEST,
237 	FUSE_ROM,
238 	MODE_END,
239 };
240 
241 struct ak_def {
242 	enum asahi_compass_chipset type;
243 	long (*raw_to_gauss)(u16 data);
244 	u16 range;
245 	u8 ctrl_regs[REGS_END];
246 	u8 ctrl_masks[MASK_END];
247 	u8 ctrl_modes[MODE_END];
248 	u8 data_regs[3];
249 };
250 
251 static const struct ak_def ak_def_array[] = {
252 	[AK8975] = {
253 		.type = AK8975,
254 		.raw_to_gauss = ak8975_raw_to_gauss,
255 		.range = 4096,
256 		.ctrl_regs = {
257 			AK8975_REG_ST1,
258 			AK8975_REG_ST2,
259 			AK8975_REG_CNTL,
260 			AK8975_REG_ASAX,
261 			AK8975_MAX_REGS},
262 		.ctrl_masks = {
263 			AK8975_REG_ST1_DRDY_MASK,
264 			AK8975_REG_ST2_HOFL_MASK,
265 			AK8975_REG_ST2_DERR_MASK,
266 			AK8975_REG_CNTL_MODE_MASK},
267 		.ctrl_modes = {
268 			AK8975_REG_CNTL_MODE_POWER_DOWN,
269 			AK8975_REG_CNTL_MODE_ONCE,
270 			AK8975_REG_CNTL_MODE_SELF_TEST,
271 			AK8975_REG_CNTL_MODE_FUSE_ROM},
272 		.data_regs = {
273 			AK8975_REG_HXL,
274 			AK8975_REG_HYL,
275 			AK8975_REG_HZL},
276 	},
277 	[AK8963] = {
278 		.type = AK8963,
279 		.raw_to_gauss = ak8963_09911_raw_to_gauss,
280 		.range = 8190,
281 		.ctrl_regs = {
282 			AK8975_REG_ST1,
283 			AK8975_REG_ST2,
284 			AK8975_REG_CNTL,
285 			AK8975_REG_ASAX,
286 			AK8975_MAX_REGS},
287 		.ctrl_masks = {
288 			AK8975_REG_ST1_DRDY_MASK,
289 			AK8975_REG_ST2_HOFL_MASK,
290 			0,
291 			AK8975_REG_CNTL_MODE_MASK},
292 		.ctrl_modes = {
293 			AK8975_REG_CNTL_MODE_POWER_DOWN,
294 			AK8975_REG_CNTL_MODE_ONCE,
295 			AK8975_REG_CNTL_MODE_SELF_TEST,
296 			AK8975_REG_CNTL_MODE_FUSE_ROM},
297 		.data_regs = {
298 			AK8975_REG_HXL,
299 			AK8975_REG_HYL,
300 			AK8975_REG_HZL},
301 	},
302 	[AK09911] = {
303 		.type = AK09911,
304 		.raw_to_gauss = ak8963_09911_raw_to_gauss,
305 		.range = 8192,
306 		.ctrl_regs = {
307 			AK09912_REG_ST1,
308 			AK09912_REG_ST2,
309 			AK09912_REG_CNTL2,
310 			AK09912_REG_ASAX,
311 			AK09912_MAX_REGS},
312 		.ctrl_masks = {
313 			AK09912_REG_ST1_DRDY_MASK,
314 			AK09912_REG_ST2_HOFL_MASK,
315 			0,
316 			AK09912_REG_CNTL2_MODE_MASK},
317 		.ctrl_modes = {
318 			AK09912_REG_CNTL_MODE_POWER_DOWN,
319 			AK09912_REG_CNTL_MODE_ONCE,
320 			AK09912_REG_CNTL_MODE_SELF_TEST,
321 			AK09912_REG_CNTL_MODE_FUSE_ROM},
322 		.data_regs = {
323 			AK09912_REG_HXL,
324 			AK09912_REG_HYL,
325 			AK09912_REG_HZL},
326 	},
327 	[AK09912] = {
328 		.type = AK09912,
329 		.raw_to_gauss = ak09912_raw_to_gauss,
330 		.range = 32752,
331 		.ctrl_regs = {
332 			AK09912_REG_ST1,
333 			AK09912_REG_ST2,
334 			AK09912_REG_CNTL2,
335 			AK09912_REG_ASAX,
336 			AK09912_MAX_REGS},
337 		.ctrl_masks = {
338 			AK09912_REG_ST1_DRDY_MASK,
339 			AK09912_REG_ST2_HOFL_MASK,
340 			0,
341 			AK09912_REG_CNTL2_MODE_MASK},
342 		.ctrl_modes = {
343 			AK09912_REG_CNTL_MODE_POWER_DOWN,
344 			AK09912_REG_CNTL_MODE_ONCE,
345 			AK09912_REG_CNTL_MODE_SELF_TEST,
346 			AK09912_REG_CNTL_MODE_FUSE_ROM},
347 		.data_regs = {
348 			AK09912_REG_HXL,
349 			AK09912_REG_HYL,
350 			AK09912_REG_HZL},
351 	},
352 	[AK09916] = {
353 		.type = AK09916,
354 		.raw_to_gauss = ak09912_raw_to_gauss,
355 		.range = 32752,
356 		.ctrl_regs = {
357 			AK09912_REG_ST1,
358 			AK09912_REG_ST2,
359 			AK09912_REG_CNTL2,
360 			AK09912_REG_ASAX,
361 			AK09912_MAX_REGS},
362 		.ctrl_masks = {
363 			AK09912_REG_ST1_DRDY_MASK,
364 			AK09912_REG_ST2_HOFL_MASK,
365 			0,
366 			AK09912_REG_CNTL2_MODE_MASK},
367 		.ctrl_modes = {
368 			AK09912_REG_CNTL_MODE_POWER_DOWN,
369 			AK09912_REG_CNTL_MODE_ONCE,
370 			AK09912_REG_CNTL_MODE_SELF_TEST,
371 			AK09912_REG_CNTL_MODE_FUSE_ROM},
372 		.data_regs = {
373 			AK09912_REG_HXL,
374 			AK09912_REG_HYL,
375 			AK09912_REG_HZL},
376 	},
377 	[AK09918] = {
378 		/* ak09918 is register compatible with ak09912 this is for avoid
379 		 * unknown id messages.
380 		 */
381 		.type = AK09918,
382 		.raw_to_gauss = ak09912_raw_to_gauss,
383 		.range = 32752,
384 		.ctrl_regs = {
385 			AK09912_REG_ST1,
386 			AK09912_REG_ST2,
387 			AK09912_REG_CNTL2,
388 			AK09912_REG_ASAX,
389 			AK09912_MAX_REGS},
390 		.ctrl_masks = {
391 			AK09912_REG_ST1_DRDY_MASK,
392 			AK09912_REG_ST2_HOFL_MASK,
393 			0,
394 			AK09912_REG_CNTL2_MODE_MASK},
395 		.ctrl_modes = {
396 			AK09912_REG_CNTL_MODE_POWER_DOWN,
397 			AK09912_REG_CNTL_MODE_ONCE,
398 			AK09912_REG_CNTL_MODE_SELF_TEST,
399 			AK09912_REG_CNTL_MODE_FUSE_ROM},
400 		.data_regs = {
401 			AK09912_REG_HXL,
402 			AK09912_REG_HYL,
403 			AK09912_REG_HZL},
404 	}
405 };
406 
407 /*
408  * Per-instance context data for the device.
409  */
410 struct ak8975_data {
411 	struct i2c_client	*client;
412 	const struct ak_def	*def;
413 	struct mutex		lock;
414 	u8			asa[3];
415 	long			raw_to_gauss[3];
416 	struct gpio_desc	*eoc_gpiod;
417 	struct gpio_desc	*reset_gpiod;
418 	int			eoc_irq;
419 	wait_queue_head_t	data_ready_queue;
420 	unsigned long		flags;
421 	u8			cntl_cache;
422 	struct iio_mount_matrix orientation;
423 	struct regulator	*vdd;
424 	struct regulator	*vid;
425 
426 	/* Ensure natural alignment of timestamp */
427 	struct {
428 		s16 channels[3];
429 		aligned_s64 ts;
430 	} scan;
431 };
432 
433 /* Enable attached power regulator if any. */
434 static int ak8975_power_on(const struct ak8975_data *data)
435 {
436 	int ret;
437 
438 	ret = regulator_enable(data->vdd);
439 	if (ret) {
440 		dev_warn(&data->client->dev,
441 			 "Failed to enable specified Vdd supply\n");
442 		return ret;
443 	}
444 	ret = regulator_enable(data->vid);
445 	if (ret) {
446 		dev_warn(&data->client->dev,
447 			 "Failed to enable specified Vid supply\n");
448 		regulator_disable(data->vdd);
449 		return ret;
450 	}
451 
452 	gpiod_set_value_cansleep(data->reset_gpiod, 0);
453 
454 	/*
455 	 * According to the datasheet the power supply rise time is 200us
456 	 * and the minimum wait time before mode setting is 100us, in
457 	 * total 300us. Add some margin and say minimum 500us here.
458 	 */
459 	fsleep(500);
460 
461 	return 0;
462 }
463 
464 /* Disable attached power regulator if any. */
465 static void ak8975_power_off(const struct ak8975_data *data)
466 {
467 	gpiod_set_value_cansleep(data->reset_gpiod, 1);
468 
469 	regulator_disable(data->vid);
470 	regulator_disable(data->vdd);
471 }
472 
473 /*
474  * Return 0 if the i2c device is the one we expect.
475  * return a negative error number otherwise
476  */
477 static int ak8975_who_i_am(const struct ak8975_data *data,
478 			   enum asahi_compass_chipset type)
479 {
480 	struct i2c_client *client = data->client;
481 	u8 wia_val[2];
482 	int ret;
483 
484 	/*
485 	 * Signature for each device:
486 	 * Device   |  WIA1      |  WIA2
487 	 * AK09918  |  DEVICE_ID_|  AK09918_DEVICE_ID
488 	 * AK09916  |  DEVICE_ID_|  AK09916_DEVICE_ID
489 	 * AK09912  |  DEVICE_ID |  AK09912_DEVICE_ID
490 	 * AK09911  |  DEVICE_ID |  AK09911_DEVICE_ID
491 	 * AK8975   |  DEVICE_ID |  NA
492 	 * AK8963   |  DEVICE_ID |  NA
493 	 */
494 	ret = i2c_smbus_read_i2c_block_data_or_emulated(client,
495 							AK09912_REG_WIA1,
496 							sizeof(wia_val),
497 							wia_val);
498 	if (ret < 0) {
499 		dev_err(&client->dev, "Error reading WIA\n");
500 		return ret;
501 	}
502 	if (ret != sizeof(wia_val)) {
503 		dev_err(&client->dev, "Error reading WIA\n");
504 		return -EIO;
505 	}
506 
507 	if (wia_val[0] != AK8975_DEVICE_ID)
508 		return -ENODEV;
509 
510 	switch (type) {
511 	case AK8975:
512 	case AK8963:
513 		return 0;
514 	case AK09911:
515 		if (wia_val[1] == AK09911_DEVICE_ID)
516 			return 0;
517 		break;
518 	case AK09912:
519 		if (wia_val[1] == AK09912_DEVICE_ID)
520 			return 0;
521 		break;
522 	case AK09916:
523 		if (wia_val[1] == AK09916_DEVICE_ID)
524 			return 0;
525 		break;
526 	case AK09918:
527 		if (wia_val[1] == AK09918_DEVICE_ID)
528 			return 0;
529 		break;
530 	}
531 
532 	dev_info(&client->dev, "Device ID %x is unknown.\n", wia_val[1]);
533 	/*
534 	 * Let driver to probe on unknown id for support more register
535 	 * compatible variants.
536 	 */
537 	return 0;
538 }
539 
540 /*
541  * Helper function to write to CNTL register.
542  */
543 static int ak8975_set_mode(struct ak8975_data *data, enum ak_ctrl_mode mode)
544 {
545 	u8 regval;
546 	int ret;
547 
548 	regval = (data->cntl_cache & ~data->def->ctrl_masks[CNTL_MODE]) |
549 		 data->def->ctrl_modes[mode];
550 	ret = i2c_smbus_write_byte_data(data->client,
551 					data->def->ctrl_regs[CNTL], regval);
552 	if (ret < 0)
553 		return ret;
554 
555 	data->cntl_cache = regval;
556 	/* After mode change wait at least 100us */
557 	fsleep(100);
558 
559 	return 0;
560 }
561 
562 /*
563  * Handle data ready irq
564  */
565 static irqreturn_t ak8975_irq_handler(int irq, void *data)
566 {
567 	struct ak8975_data *ak8975 = data;
568 
569 	set_bit(0, &ak8975->flags);
570 	wake_up(&ak8975->data_ready_queue);
571 
572 	return IRQ_HANDLED;
573 }
574 
575 /*
576  * Install data ready interrupt handler
577  */
578 static int ak8975_setup_irq(struct ak8975_data *data)
579 {
580 	struct i2c_client *client = data->client;
581 	int irq;
582 	int ret;
583 
584 	init_waitqueue_head(&data->data_ready_queue);
585 	clear_bit(0, &data->flags);
586 	if (client->irq)
587 		irq = client->irq;
588 	else
589 		irq = gpiod_to_irq(data->eoc_gpiod);
590 
591 	ret = devm_request_irq(&client->dev, irq, ak8975_irq_handler,
592 			       IRQF_TRIGGER_RISING,
593 			       dev_name(&client->dev), data);
594 	if (ret)
595 		return ret;
596 
597 	data->eoc_irq = irq;
598 
599 	return 0;
600 }
601 
602 /*
603  * Perform some start-of-day setup, including reading the asa calibration
604  * values and caching them.
605  */
606 static int ak8975_setup(struct ak8975_data *data)
607 {
608 	struct i2c_client *client = data->client;
609 	int ret;
610 
611 	/* Write the fused rom access mode. */
612 	ret = ak8975_set_mode(data, FUSE_ROM);
613 	if (ret < 0) {
614 		dev_err(&client->dev, "Error in setting fuse access mode\n");
615 		return ret;
616 	}
617 
618 	/* Get asa data and store in the device data. */
619 	ret = i2c_smbus_read_i2c_block_data_or_emulated(client,
620 							data->def->ctrl_regs[ASA_BASE],
621 							sizeof(data->asa),
622 							data->asa);
623 	if (ret < 0) {
624 		dev_err(&client->dev, "Not able to read asa data\n");
625 		return ret;
626 	}
627 	if (ret != sizeof(data->asa)) {
628 		dev_err(&client->dev, "Error reading asa data\n");
629 		return -EIO;
630 	}
631 
632 	/* After reading fuse ROM data set power-down mode */
633 	ret = ak8975_set_mode(data, POWER_DOWN);
634 	if (ret < 0) {
635 		dev_err(&client->dev, "Error in setting power-down mode\n");
636 		return ret;
637 	}
638 
639 	if (data->eoc_gpiod || client->irq > 0) {
640 		ret = ak8975_setup_irq(data);
641 		if (ret < 0) {
642 			dev_err(&client->dev,
643 				"Error setting data ready interrupt\n");
644 			return ret;
645 		}
646 	}
647 
648 	data->raw_to_gauss[0] = data->def->raw_to_gauss(data->asa[0]);
649 	data->raw_to_gauss[1] = data->def->raw_to_gauss(data->asa[1]);
650 	data->raw_to_gauss[2] = data->def->raw_to_gauss(data->asa[2]);
651 
652 	return 0;
653 }
654 
655 static int wait_conversion_complete_gpio(struct ak8975_data *data,
656 					 unsigned int poll_ms,
657 					 unsigned int timeout_ms)
658 {
659 	struct i2c_client *client = data->client;
660 	int ret;
661 	int val;
662 
663 	/* Wait for the conversion to complete. */
664 	ret = readx_poll_timeout(gpiod_get_value, data->eoc_gpiod, val, val != 0,
665 				 poll_ms * USEC_PER_MSEC,
666 				 timeout_ms * USEC_PER_MSEC);
667 	if (ret)
668 		return ret;
669 	if (val < 0) {
670 		dev_err(&client->dev, "Error in reading GPIOD\n");
671 		return val;
672 	}
673 
674 	ret = i2c_smbus_read_byte_data(client, data->def->ctrl_regs[ST1]);
675 	if (ret < 0)
676 		dev_err(&client->dev, "Error in reading ST1\n");
677 
678 	return ret;
679 }
680 
681 static int wait_conversion_complete_polled(struct ak8975_data *data,
682 					   unsigned int poll_ms,
683 					   unsigned int timeout_ms)
684 {
685 	struct i2c_client *client = data->client;
686 	int ret;
687 	int val;
688 
689 	/* Wait for the conversion to complete. */
690 	ret = read_poll_timeout(i2c_smbus_read_byte_data, val, val != 0,
691 				poll_ms * USEC_PER_MSEC,
692 				timeout_ms * USEC_PER_MSEC,
693 				true,
694 				client, data->def->ctrl_regs[ST1]);
695 	if (ret)
696 		return ret;
697 	if (val < 0)
698 		dev_err(&client->dev, "Error in reading ST1\n");
699 
700 	return val;
701 }
702 
703 /* Returns 0 if the end of conversion interrupt occurred or -ETIMEDOUT otherwise */
704 static int wait_conversion_complete_interrupt(struct ak8975_data *data,
705 					      unsigned int timeout_ms)
706 {
707 	int ret;
708 
709 	ret = wait_event_timeout(data->data_ready_queue,
710 				 test_bit(0, &data->flags),
711 				 msecs_to_jiffies(timeout_ms));
712 	clear_bit(0, &data->flags);
713 
714 	return ret > 0 ? 0 : -ETIMEDOUT;
715 }
716 
717 static int ak8975_start_read_axis(struct ak8975_data *data)
718 {
719 	struct i2c_client *client = data->client;
720 	int ret;
721 
722 	/* Set up the device for taking a sample. */
723 	ret = ak8975_set_mode(data, MODE_ONCE);
724 	if (ret < 0) {
725 		dev_err(&client->dev, "Error in setting operating mode\n");
726 		return ret;
727 	}
728 
729 	/* Wait for the conversion to complete. */
730 	if (data->eoc_irq)
731 		ret = wait_conversion_complete_interrupt(data, 100);
732 	else if (data->eoc_gpiod)
733 		ret = wait_conversion_complete_gpio(data, 10, 500);
734 	else
735 		ret = wait_conversion_complete_polled(data, 10, 500);
736 	if (ret < 0)
737 		return ret;
738 
739 	/* Return with zero if the data is ready. */
740 	return !data->def->ctrl_regs[ST1_DRDY];
741 }
742 
743 /* Retrieve raw flux value for one of the x, y, or z axis.  */
744 static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
745 {
746 	struct ak8975_data *data = iio_priv(indio_dev);
747 	const struct i2c_client *client = data->client;
748 	const struct ak_def *def = data->def;
749 	__le16 rval;
750 	int ret;
751 
752 	pm_runtime_get_sync(&data->client->dev);
753 
754 	mutex_lock(&data->lock);
755 
756 	ret = ak8975_start_read_axis(data);
757 	if (ret)
758 		goto exit;
759 
760 	ret = i2c_smbus_read_i2c_block_data_or_emulated(client,
761 							def->data_regs[index],
762 							sizeof(rval),
763 							(u8 *)&rval);
764 	if (ret < 0)
765 		goto exit;
766 	if (ret != sizeof(rval)) {
767 		ret = -EIO;
768 		goto exit;
769 	}
770 
771 	/* Read out ST2 for release lock on measurement data. */
772 	ret = i2c_smbus_read_byte_data(client, data->def->ctrl_regs[ST2]);
773 	if (ret < 0) {
774 		dev_err(&client->dev, "Error in reading ST2\n");
775 		goto exit;
776 	}
777 
778 	if (ret & (data->def->ctrl_masks[ST2_DERR] |
779 		   data->def->ctrl_masks[ST2_HOFL])) {
780 		dev_err(&client->dev, "ST2 status error 0x%x\n", ret);
781 		ret = -EINVAL;
782 		goto exit;
783 	}
784 
785 	mutex_unlock(&data->lock);
786 
787 	pm_runtime_put_autosuspend(&data->client->dev);
788 
789 	/* Swap bytes and convert to valid range. */
790 	*val = clamp_t(s16, le16_to_cpu(rval), -def->range, def->range);
791 
792 	return IIO_VAL_INT;
793 
794 exit:
795 	mutex_unlock(&data->lock);
796 	pm_runtime_put_autosuspend(&data->client->dev);
797 	dev_err(&client->dev, "Error in reading axis\n");
798 	return ret;
799 }
800 
801 static int ak8975_read_raw(struct iio_dev *indio_dev,
802 			   struct iio_chan_spec const *chan,
803 			   int *val, int *val2,
804 			   long mask)
805 {
806 	struct ak8975_data *data = iio_priv(indio_dev);
807 
808 	switch (mask) {
809 	case IIO_CHAN_INFO_RAW:
810 		return ak8975_read_axis(indio_dev, chan->address, val);
811 	case IIO_CHAN_INFO_SCALE:
812 		*val = 0;
813 		*val2 = data->raw_to_gauss[chan->address];
814 		return IIO_VAL_INT_PLUS_MICRO;
815 	}
816 	return -EINVAL;
817 }
818 
819 static const struct iio_mount_matrix *
820 ak8975_get_mount_matrix(const struct iio_dev *indio_dev,
821 			const struct iio_chan_spec *chan)
822 {
823 	struct ak8975_data *data = iio_priv(indio_dev);
824 
825 	return &data->orientation;
826 }
827 
828 static const struct iio_chan_spec_ext_info ak8975_ext_info[] = {
829 	IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, ak8975_get_mount_matrix),
830 	{ }
831 };
832 
833 #define AK8975_CHANNEL(axis, index)					\
834 	{								\
835 		.type = IIO_MAGN,					\
836 		.modified = 1,						\
837 		.channel2 = IIO_MOD_##axis,				\
838 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |		\
839 			     BIT(IIO_CHAN_INFO_SCALE),			\
840 		.address = index,					\
841 		.scan_index = index,					\
842 		.scan_type = {						\
843 			.sign = 's',					\
844 			.realbits = 16,					\
845 			.storagebits = 16,				\
846 			.endianness = IIO_CPU				\
847 		},							\
848 		.ext_info = ak8975_ext_info,				\
849 	}
850 
851 static const struct iio_chan_spec ak8975_channels[] = {
852 	AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2),
853 	IIO_CHAN_SOFT_TIMESTAMP(3),
854 };
855 
856 static const unsigned long ak8975_scan_masks[] = { 0x7, 0 };
857 
858 static const struct iio_info ak8975_info = {
859 	.read_raw = &ak8975_read_raw,
860 };
861 
862 static void ak8975_fill_buffer(struct iio_dev *indio_dev)
863 {
864 	struct ak8975_data *data = iio_priv(indio_dev);
865 	const struct i2c_client *client = data->client;
866 	const struct ak_def *def = data->def;
867 	int ret;
868 	__le16 fval[3];
869 
870 	mutex_lock(&data->lock);
871 
872 	ret = ak8975_start_read_axis(data);
873 	if (ret)
874 		goto unlock;
875 
876 	/*
877 	 * For each axis, read the flux value from the appropriate register
878 	 * (the register is specified in the iio device attributes).
879 	 */
880 	ret = i2c_smbus_read_i2c_block_data_or_emulated(client,
881 							def->data_regs[0],
882 							sizeof(fval),
883 							(u8 *)fval);
884 	if (ret < 0)
885 		goto unlock;
886 	if (ret != sizeof(fval))
887 		goto unlock;
888 
889 	mutex_unlock(&data->lock);
890 
891 	/* Clamp to valid range. */
892 	data->scan.channels[0] = clamp_t(s16, le16_to_cpu(fval[0]), -def->range, def->range);
893 	data->scan.channels[1] = clamp_t(s16, le16_to_cpu(fval[1]), -def->range, def->range);
894 	data->scan.channels[2] = clamp_t(s16, le16_to_cpu(fval[2]), -def->range, def->range);
895 
896 	iio_push_to_buffers_with_ts(indio_dev, &data->scan, sizeof(data->scan),
897 				    iio_get_time_ns(indio_dev));
898 
899 	return;
900 
901 unlock:
902 	mutex_unlock(&data->lock);
903 	dev_err(&client->dev, "Error in reading axes block\n");
904 }
905 
906 static irqreturn_t ak8975_handle_trigger(int irq, void *p)
907 {
908 	const struct iio_poll_func *pf = p;
909 	struct iio_dev *indio_dev = pf->indio_dev;
910 
911 	ak8975_fill_buffer(indio_dev);
912 	iio_trigger_notify_done(indio_dev->trig);
913 	return IRQ_HANDLED;
914 }
915 
916 static int ak8975_buffer_preenable(struct iio_dev *indio_dev)
917 {
918 	struct ak8975_data *data = iio_priv(indio_dev);
919 	struct device *dev = &data->client->dev;
920 
921 	return pm_runtime_resume_and_get(dev);
922 }
923 
924 static int ak8975_buffer_postdisable(struct iio_dev *indio_dev)
925 {
926 	struct ak8975_data *data = iio_priv(indio_dev);
927 	struct device *dev = &data->client->dev;
928 
929 	pm_runtime_put_autosuspend(dev);
930 
931 	return 0;
932 }
933 
934 static const struct iio_buffer_setup_ops ak8975_buffer_setup_ops = {
935 	.preenable = ak8975_buffer_preenable,
936 	.postdisable = ak8975_buffer_postdisable,
937 };
938 static int ak8975_probe(struct i2c_client *client)
939 {
940 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
941 	struct ak8975_data *data;
942 	struct iio_dev *indio_dev;
943 	struct gpio_desc *eoc_gpiod;
944 	struct gpio_desc *reset_gpiod;
945 	const char *name = NULL;
946 	int ret;
947 
948 	/*
949 	 * Grab and set up the supplied GPIO.
950 	 * We may not have a GPIO based IRQ to scan, that is fine, we will
951 	 * poll if so.
952 	 */
953 	eoc_gpiod = devm_gpiod_get_optional(&client->dev, NULL, GPIOD_IN);
954 	if (IS_ERR(eoc_gpiod))
955 		return PTR_ERR(eoc_gpiod);
956 	gpiod_set_consumer_name(eoc_gpiod, "ak_8975");
957 
958 	/*
959 	 * According to AK09911 datasheet, if reset GPIO is provided then
960 	 * deassert reset on ak8975_power_on() and assert reset on
961 	 * ak8975_power_off().
962 	 */
963 	reset_gpiod = devm_gpiod_get_optional(&client->dev,
964 					      "reset", GPIOD_OUT_HIGH);
965 	if (IS_ERR(reset_gpiod))
966 		return PTR_ERR(reset_gpiod);
967 
968 	/* Register with IIO */
969 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
970 	if (indio_dev == NULL)
971 		return -ENOMEM;
972 
973 	data = iio_priv(indio_dev);
974 	i2c_set_clientdata(client, indio_dev);
975 
976 	data->client = client;
977 	data->eoc_gpiod = eoc_gpiod;
978 	data->reset_gpiod = reset_gpiod;
979 	data->eoc_irq = 0;
980 
981 	ret = iio_read_mount_matrix(&client->dev, &data->orientation);
982 	if (ret)
983 		return ret;
984 
985 	/* id will be NULL when enumerated via ACPI */
986 	data->def = i2c_get_match_data(client);
987 	if (!data->def)
988 		return -ENODEV;
989 
990 	/* If enumerated via firmware node, fix the ABI */
991 	if (dev_fwnode(&client->dev))
992 		name = dev_name(&client->dev);
993 	else
994 		name = id->name;
995 
996 	/* Fetch the regulators */
997 	data->vdd = devm_regulator_get(&client->dev, "vdd");
998 	if (IS_ERR(data->vdd))
999 		return PTR_ERR(data->vdd);
1000 	data->vid = devm_regulator_get(&client->dev, "vid");
1001 	if (IS_ERR(data->vid))
1002 		return PTR_ERR(data->vid);
1003 
1004 	ret = ak8975_power_on(data);
1005 	if (ret)
1006 		return ret;
1007 
1008 	ret = ak8975_who_i_am(data, data->def->type);
1009 	if (ret) {
1010 		dev_err(&client->dev, "Unexpected device\n");
1011 		goto power_off;
1012 	}
1013 	dev_dbg(&client->dev, "Asahi compass chip %s\n", name);
1014 
1015 	/* Perform some basic start-of-day setup of the device. */
1016 	ret = ak8975_setup(data);
1017 	if (ret) {
1018 		dev_err(&client->dev, "%s initialization fails\n", name);
1019 		goto power_off;
1020 	}
1021 
1022 	mutex_init(&data->lock);
1023 	indio_dev->channels = ak8975_channels;
1024 	indio_dev->num_channels = ARRAY_SIZE(ak8975_channels);
1025 	indio_dev->info = &ak8975_info;
1026 	indio_dev->available_scan_masks = ak8975_scan_masks;
1027 	indio_dev->modes = INDIO_DIRECT_MODE;
1028 	indio_dev->name = name;
1029 
1030 	ret = iio_triggered_buffer_setup(indio_dev, NULL, ak8975_handle_trigger,
1031 					 &ak8975_buffer_setup_ops);
1032 	if (ret) {
1033 		dev_err(&client->dev, "triggered buffer setup failed\n");
1034 		goto power_off;
1035 	}
1036 
1037 	ret = iio_device_register(indio_dev);
1038 	if (ret) {
1039 		dev_err(&client->dev, "device register failed\n");
1040 		goto cleanup_buffer;
1041 	}
1042 
1043 	/* Enable runtime PM */
1044 	pm_runtime_get_noresume(&client->dev);
1045 	pm_runtime_set_active(&client->dev);
1046 	pm_runtime_enable(&client->dev);
1047 	/*
1048 	 * The device comes online in 500us, so add two orders of magnitude
1049 	 * of delay before autosuspending: 50 ms.
1050 	 */
1051 	pm_runtime_set_autosuspend_delay(&client->dev, 50);
1052 	pm_runtime_use_autosuspend(&client->dev);
1053 	pm_runtime_put(&client->dev);
1054 
1055 	return 0;
1056 
1057 cleanup_buffer:
1058 	iio_triggered_buffer_cleanup(indio_dev);
1059 power_off:
1060 	ak8975_power_off(data);
1061 	return ret;
1062 }
1063 
1064 static void ak8975_remove(struct i2c_client *client)
1065 {
1066 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
1067 	struct ak8975_data *data = iio_priv(indio_dev);
1068 
1069 	pm_runtime_get_sync(&client->dev);
1070 	pm_runtime_put_noidle(&client->dev);
1071 	pm_runtime_disable(&client->dev);
1072 	iio_device_unregister(indio_dev);
1073 	iio_triggered_buffer_cleanup(indio_dev);
1074 	ak8975_set_mode(data, POWER_DOWN);
1075 	ak8975_power_off(data);
1076 }
1077 
1078 static int ak8975_runtime_suspend(struct device *dev)
1079 {
1080 	struct i2c_client *client = to_i2c_client(dev);
1081 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
1082 	struct ak8975_data *data = iio_priv(indio_dev);
1083 	int ret;
1084 
1085 	/* Set the device in power down if it wasn't already */
1086 	ret = ak8975_set_mode(data, POWER_DOWN);
1087 	if (ret < 0) {
1088 		dev_err(&client->dev, "Error in setting power-down mode\n");
1089 		return ret;
1090 	}
1091 	/* Next cut the regulators */
1092 	ak8975_power_off(data);
1093 
1094 	return 0;
1095 }
1096 
1097 static int ak8975_runtime_resume(struct device *dev)
1098 {
1099 	struct i2c_client *client = to_i2c_client(dev);
1100 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
1101 	struct ak8975_data *data = iio_priv(indio_dev);
1102 	int ret;
1103 
1104 	/* Take up the regulators */
1105 	ak8975_power_on(data);
1106 	/*
1107 	 * We come up in powered down mode, the reading routines will
1108 	 * put us in the mode to read values later.
1109 	 */
1110 	ret = ak8975_set_mode(data, POWER_DOWN);
1111 	if (ret < 0) {
1112 		dev_err(&client->dev, "Error in setting power-down mode\n");
1113 		return ret;
1114 	}
1115 
1116 	return 0;
1117 }
1118 
1119 static DEFINE_RUNTIME_DEV_PM_OPS(ak8975_dev_pm_ops, ak8975_runtime_suspend,
1120 				 ak8975_runtime_resume, NULL);
1121 
1122 static const struct acpi_device_id ak_acpi_match[] = {
1123 	{"AK8963", (kernel_ulong_t)&ak_def_array[AK8963] },
1124 	{"AK8975", (kernel_ulong_t)&ak_def_array[AK8975] },
1125 	{"AK009911", (kernel_ulong_t)&ak_def_array[AK09911] },
1126 	{"AK09911", (kernel_ulong_t)&ak_def_array[AK09911] },
1127 	{"AK09912", (kernel_ulong_t)&ak_def_array[AK09912] },
1128 	{"AKM9911", (kernel_ulong_t)&ak_def_array[AK09911] },
1129 	{"INVN6500", (kernel_ulong_t)&ak_def_array[AK8963] },
1130 	{ }
1131 };
1132 MODULE_DEVICE_TABLE(acpi, ak_acpi_match);
1133 
1134 static const struct i2c_device_id ak8975_id[] = {
1135 	{ .name = "AK8963", .driver_data = (kernel_ulong_t)&ak_def_array[AK8963] },
1136 	{ .name = "ak8963", .driver_data = (kernel_ulong_t)&ak_def_array[AK8963] },
1137 	{ .name = "ak8975", .driver_data = (kernel_ulong_t)&ak_def_array[AK8975] },
1138 	{ .name = "ak09911", .driver_data = (kernel_ulong_t)&ak_def_array[AK09911] },
1139 	{ .name = "ak09912", .driver_data = (kernel_ulong_t)&ak_def_array[AK09912] },
1140 	{ .name = "ak09916", .driver_data = (kernel_ulong_t)&ak_def_array[AK09916] },
1141 	{ .name = "ak09918", .driver_data = (kernel_ulong_t)&ak_def_array[AK09918] },
1142 	{ }
1143 };
1144 MODULE_DEVICE_TABLE(i2c, ak8975_id);
1145 
1146 static const struct of_device_id ak8975_of_match[] = {
1147 	{ .compatible = "asahi-kasei,ak8975", .data = &ak_def_array[AK8975] },
1148 	{ .compatible = "ak8975", .data = &ak_def_array[AK8975] },
1149 	{ .compatible = "asahi-kasei,ak8963", .data = &ak_def_array[AK8963] },
1150 	{ .compatible = "ak8963", .data = &ak_def_array[AK8963] },
1151 	{ .compatible = "asahi-kasei,ak09911", .data = &ak_def_array[AK09911] },
1152 	{ .compatible = "ak09911", .data = &ak_def_array[AK09911] },
1153 	{ .compatible = "asahi-kasei,ak09912", .data = &ak_def_array[AK09912] },
1154 	{ .compatible = "ak09912", .data = &ak_def_array[AK09912] },
1155 	{ .compatible = "asahi-kasei,ak09916", .data = &ak_def_array[AK09916] },
1156 	{ .compatible = "asahi-kasei,ak09918", .data = &ak_def_array[AK09918] },
1157 	{ }
1158 };
1159 MODULE_DEVICE_TABLE(of, ak8975_of_match);
1160 
1161 static struct i2c_driver ak8975_driver = {
1162 	.driver = {
1163 		.name	= "ak8975",
1164 		.pm = pm_ptr(&ak8975_dev_pm_ops),
1165 		.of_match_table = ak8975_of_match,
1166 		.acpi_match_table = ak_acpi_match,
1167 	},
1168 	.probe		= ak8975_probe,
1169 	.remove		= ak8975_remove,
1170 	.id_table	= ak8975_id,
1171 };
1172 module_i2c_driver(ak8975_driver);
1173 
1174 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
1175 MODULE_DESCRIPTION("AK8975 magnetometer driver");
1176 MODULE_LICENSE("GPL");
1177