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/module.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
14 #include <linux/interrupt.h>
15 #include <linux/err.h>
16 #include <linux/mutex.h>
17 #include <linux/delay.h>
18 #include <linux/bitops.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/acpi.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/pm_runtime.h>
23 
24 #include <linux/iio/iio.h>
25 #include <linux/iio/sysfs.h>
26 #include <linux/iio/buffer.h>
27 #include <linux/iio/trigger.h>
28 #include <linux/iio/trigger_consumer.h>
29 #include <linux/iio/triggered_buffer.h>
30 
31 #include <linux/iio/magnetometer/ak8975.h>
32 
33 /*
34  * Register definitions, as well as various shifts and masks to get at the
35  * individual fields of the registers.
36  */
37 #define AK8975_REG_WIA			0x00
38 #define AK8975_DEVICE_ID		0x48
39 
40 #define AK8975_REG_INFO			0x01
41 
42 #define AK8975_REG_ST1			0x02
43 #define AK8975_REG_ST1_DRDY_SHIFT	0
44 #define AK8975_REG_ST1_DRDY_MASK	(1 << AK8975_REG_ST1_DRDY_SHIFT)
45 
46 #define AK8975_REG_HXL			0x03
47 #define AK8975_REG_HXH			0x04
48 #define AK8975_REG_HYL			0x05
49 #define AK8975_REG_HYH			0x06
50 #define AK8975_REG_HZL			0x07
51 #define AK8975_REG_HZH			0x08
52 #define AK8975_REG_ST2			0x09
53 #define AK8975_REG_ST2_DERR_SHIFT	2
54 #define AK8975_REG_ST2_DERR_MASK	(1 << AK8975_REG_ST2_DERR_SHIFT)
55 
56 #define AK8975_REG_ST2_HOFL_SHIFT	3
57 #define AK8975_REG_ST2_HOFL_MASK	(1 << AK8975_REG_ST2_HOFL_SHIFT)
58 
59 #define AK8975_REG_CNTL			0x0A
60 #define AK8975_REG_CNTL_MODE_SHIFT	0
61 #define AK8975_REG_CNTL_MODE_MASK	(0xF << AK8975_REG_CNTL_MODE_SHIFT)
62 #define AK8975_REG_CNTL_MODE_POWER_DOWN	0x00
63 #define AK8975_REG_CNTL_MODE_ONCE	0x01
64 #define AK8975_REG_CNTL_MODE_SELF_TEST	0x08
65 #define AK8975_REG_CNTL_MODE_FUSE_ROM	0x0F
66 
67 #define AK8975_REG_RSVC			0x0B
68 #define AK8975_REG_ASTC			0x0C
69 #define AK8975_REG_TS1			0x0D
70 #define AK8975_REG_TS2			0x0E
71 #define AK8975_REG_I2CDIS		0x0F
72 #define AK8975_REG_ASAX			0x10
73 #define AK8975_REG_ASAY			0x11
74 #define AK8975_REG_ASAZ			0x12
75 
76 #define AK8975_MAX_REGS			AK8975_REG_ASAZ
77 
78 /*
79  * AK09912 Register definitions
80  */
81 #define AK09912_REG_WIA1		0x00
82 #define AK09912_REG_WIA2		0x01
83 #define AK09912_DEVICE_ID		0x04
84 #define AK09911_DEVICE_ID		0x05
85 
86 #define AK09911_REG_INFO1		0x02
87 #define AK09911_REG_INFO2		0x03
88 
89 #define AK09912_REG_ST1			0x10
90 
91 #define AK09912_REG_ST1_DRDY_SHIFT	0
92 #define AK09912_REG_ST1_DRDY_MASK	(1 << AK09912_REG_ST1_DRDY_SHIFT)
93 
94 #define AK09912_REG_HXL			0x11
95 #define AK09912_REG_HXH			0x12
96 #define AK09912_REG_HYL			0x13
97 #define AK09912_REG_HYH			0x14
98 #define AK09912_REG_HZL			0x15
99 #define AK09912_REG_HZH			0x16
100 #define AK09912_REG_TMPS		0x17
101 
102 #define AK09912_REG_ST2			0x18
103 #define AK09912_REG_ST2_HOFL_SHIFT	3
104 #define AK09912_REG_ST2_HOFL_MASK	(1 << AK09912_REG_ST2_HOFL_SHIFT)
105 
106 #define AK09912_REG_CNTL1		0x30
107 
108 #define AK09912_REG_CNTL2		0x31
109 #define AK09912_REG_CNTL_MODE_POWER_DOWN	0x00
110 #define AK09912_REG_CNTL_MODE_ONCE	0x01
111 #define AK09912_REG_CNTL_MODE_SELF_TEST	0x10
112 #define AK09912_REG_CNTL_MODE_FUSE_ROM	0x1F
113 #define AK09912_REG_CNTL2_MODE_SHIFT	0
114 #define AK09912_REG_CNTL2_MODE_MASK	(0x1F << AK09912_REG_CNTL2_MODE_SHIFT)
115 
116 #define AK09912_REG_CNTL3		0x32
117 
118 #define AK09912_REG_TS1			0x33
119 #define AK09912_REG_TS2			0x34
120 #define AK09912_REG_TS3			0x35
121 #define AK09912_REG_I2CDIS		0x36
122 #define AK09912_REG_TS4			0x37
123 
124 #define AK09912_REG_ASAX		0x60
125 #define AK09912_REG_ASAY		0x61
126 #define AK09912_REG_ASAZ		0x62
127 
128 #define AK09912_MAX_REGS		AK09912_REG_ASAZ
129 
130 /*
131  * Miscellaneous values.
132  */
133 #define AK8975_MAX_CONVERSION_TIMEOUT	500
134 #define AK8975_CONVERSION_DONE_POLL_TIME 10
135 #define AK8975_DATA_READY_TIMEOUT	((100*HZ)/1000)
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 	AK_MAX_TYPE
213 };
214 
215 enum ak_ctrl_reg_addr {
216 	ST1,
217 	ST2,
218 	CNTL,
219 	ASA_BASE,
220 	MAX_REGS,
221 	REGS_END,
222 };
223 
224 enum ak_ctrl_reg_mask {
225 	ST1_DRDY,
226 	ST2_HOFL,
227 	ST2_DERR,
228 	CNTL_MODE,
229 	MASK_END,
230 };
231 
232 enum ak_ctrl_mode {
233 	POWER_DOWN,
234 	MODE_ONCE,
235 	SELF_TEST,
236 	FUSE_ROM,
237 	MODE_END,
238 };
239 
240 struct ak_def {
241 	enum asahi_compass_chipset type;
242 	long (*raw_to_gauss)(u16 data);
243 	u16 range;
244 	u8 ctrl_regs[REGS_END];
245 	u8 ctrl_masks[MASK_END];
246 	u8 ctrl_modes[MODE_END];
247 	u8 data_regs[3];
248 };
249 
250 static const struct ak_def ak_def_array[AK_MAX_TYPE] = {
251 	{
252 		.type = AK8975,
253 		.raw_to_gauss = ak8975_raw_to_gauss,
254 		.range = 4096,
255 		.ctrl_regs = {
256 			AK8975_REG_ST1,
257 			AK8975_REG_ST2,
258 			AK8975_REG_CNTL,
259 			AK8975_REG_ASAX,
260 			AK8975_MAX_REGS},
261 		.ctrl_masks = {
262 			AK8975_REG_ST1_DRDY_MASK,
263 			AK8975_REG_ST2_HOFL_MASK,
264 			AK8975_REG_ST2_DERR_MASK,
265 			AK8975_REG_CNTL_MODE_MASK},
266 		.ctrl_modes = {
267 			AK8975_REG_CNTL_MODE_POWER_DOWN,
268 			AK8975_REG_CNTL_MODE_ONCE,
269 			AK8975_REG_CNTL_MODE_SELF_TEST,
270 			AK8975_REG_CNTL_MODE_FUSE_ROM},
271 		.data_regs = {
272 			AK8975_REG_HXL,
273 			AK8975_REG_HYL,
274 			AK8975_REG_HZL},
275 	},
276 	{
277 		.type = AK8963,
278 		.raw_to_gauss = ak8963_09911_raw_to_gauss,
279 		.range = 8190,
280 		.ctrl_regs = {
281 			AK8975_REG_ST1,
282 			AK8975_REG_ST2,
283 			AK8975_REG_CNTL,
284 			AK8975_REG_ASAX,
285 			AK8975_MAX_REGS},
286 		.ctrl_masks = {
287 			AK8975_REG_ST1_DRDY_MASK,
288 			AK8975_REG_ST2_HOFL_MASK,
289 			0,
290 			AK8975_REG_CNTL_MODE_MASK},
291 		.ctrl_modes = {
292 			AK8975_REG_CNTL_MODE_POWER_DOWN,
293 			AK8975_REG_CNTL_MODE_ONCE,
294 			AK8975_REG_CNTL_MODE_SELF_TEST,
295 			AK8975_REG_CNTL_MODE_FUSE_ROM},
296 		.data_regs = {
297 			AK8975_REG_HXL,
298 			AK8975_REG_HYL,
299 			AK8975_REG_HZL},
300 	},
301 	{
302 		.type = AK09911,
303 		.raw_to_gauss = ak8963_09911_raw_to_gauss,
304 		.range = 8192,
305 		.ctrl_regs = {
306 			AK09912_REG_ST1,
307 			AK09912_REG_ST2,
308 			AK09912_REG_CNTL2,
309 			AK09912_REG_ASAX,
310 			AK09912_MAX_REGS},
311 		.ctrl_masks = {
312 			AK09912_REG_ST1_DRDY_MASK,
313 			AK09912_REG_ST2_HOFL_MASK,
314 			0,
315 			AK09912_REG_CNTL2_MODE_MASK},
316 		.ctrl_modes = {
317 			AK09912_REG_CNTL_MODE_POWER_DOWN,
318 			AK09912_REG_CNTL_MODE_ONCE,
319 			AK09912_REG_CNTL_MODE_SELF_TEST,
320 			AK09912_REG_CNTL_MODE_FUSE_ROM},
321 		.data_regs = {
322 			AK09912_REG_HXL,
323 			AK09912_REG_HYL,
324 			AK09912_REG_HZL},
325 	},
326 	{
327 		.type = AK09912,
328 		.raw_to_gauss = ak09912_raw_to_gauss,
329 		.range = 32752,
330 		.ctrl_regs = {
331 			AK09912_REG_ST1,
332 			AK09912_REG_ST2,
333 			AK09912_REG_CNTL2,
334 			AK09912_REG_ASAX,
335 			AK09912_MAX_REGS},
336 		.ctrl_masks = {
337 			AK09912_REG_ST1_DRDY_MASK,
338 			AK09912_REG_ST2_HOFL_MASK,
339 			0,
340 			AK09912_REG_CNTL2_MODE_MASK},
341 		.ctrl_modes = {
342 			AK09912_REG_CNTL_MODE_POWER_DOWN,
343 			AK09912_REG_CNTL_MODE_ONCE,
344 			AK09912_REG_CNTL_MODE_SELF_TEST,
345 			AK09912_REG_CNTL_MODE_FUSE_ROM},
346 		.data_regs = {
347 			AK09912_REG_HXL,
348 			AK09912_REG_HYL,
349 			AK09912_REG_HZL},
350 	}
351 };
352 
353 /*
354  * Per-instance context data for the device.
355  */
356 struct ak8975_data {
357 	struct i2c_client	*client;
358 	const struct ak_def	*def;
359 	struct mutex		lock;
360 	u8			asa[3];
361 	long			raw_to_gauss[3];
362 	struct gpio_desc	*eoc_gpiod;
363 	int			eoc_irq;
364 	wait_queue_head_t	data_ready_queue;
365 	unsigned long		flags;
366 	u8			cntl_cache;
367 	struct iio_mount_matrix orientation;
368 	struct regulator	*vdd;
369 	struct regulator	*vid;
370 };
371 
372 /* Enable attached power regulator if any. */
373 static int ak8975_power_on(const struct ak8975_data *data)
374 {
375 	int ret;
376 
377 	ret = regulator_enable(data->vdd);
378 	if (ret) {
379 		dev_warn(&data->client->dev,
380 			 "Failed to enable specified Vdd supply\n");
381 		return ret;
382 	}
383 	ret = regulator_enable(data->vid);
384 	if (ret) {
385 		dev_warn(&data->client->dev,
386 			 "Failed to enable specified Vid supply\n");
387 		return ret;
388 	}
389 	/*
390 	 * According to the datasheet the power supply rise time i 200us
391 	 * and the minimum wait time before mode setting is 100us, in
392 	 * total 300 us. Add some margin and say minimum 500us here.
393 	 */
394 	usleep_range(500, 1000);
395 	return 0;
396 }
397 
398 /* Disable attached power regulator if any. */
399 static void ak8975_power_off(const struct ak8975_data *data)
400 {
401 	regulator_disable(data->vid);
402 	regulator_disable(data->vdd);
403 }
404 
405 /*
406  * Return 0 if the i2c device is the one we expect.
407  * return a negative error number otherwise
408  */
409 static int ak8975_who_i_am(struct i2c_client *client,
410 			   enum asahi_compass_chipset type)
411 {
412 	u8 wia_val[2];
413 	int ret;
414 
415 	/*
416 	 * Signature for each device:
417 	 * Device   |  WIA1      |  WIA2
418 	 * AK09912  |  DEVICE_ID |  AK09912_DEVICE_ID
419 	 * AK09911  |  DEVICE_ID |  AK09911_DEVICE_ID
420 	 * AK8975   |  DEVICE_ID |  NA
421 	 * AK8963   |  DEVICE_ID |  NA
422 	 */
423 	ret = i2c_smbus_read_i2c_block_data_or_emulated(
424 			client, AK09912_REG_WIA1, 2, wia_val);
425 	if (ret < 0) {
426 		dev_err(&client->dev, "Error reading WIA\n");
427 		return ret;
428 	}
429 
430 	if (wia_val[0] != AK8975_DEVICE_ID)
431 		return -ENODEV;
432 
433 	switch (type) {
434 	case AK8975:
435 	case AK8963:
436 		return 0;
437 	case AK09911:
438 		if (wia_val[1] == AK09911_DEVICE_ID)
439 			return 0;
440 		break;
441 	case AK09912:
442 		if (wia_val[1] == AK09912_DEVICE_ID)
443 			return 0;
444 		break;
445 	default:
446 		dev_err(&client->dev, "Type %d unknown\n", type);
447 	}
448 	return -ENODEV;
449 }
450 
451 /*
452  * Helper function to write to CNTL register.
453  */
454 static int ak8975_set_mode(struct ak8975_data *data, enum ak_ctrl_mode mode)
455 {
456 	u8 regval;
457 	int ret;
458 
459 	regval = (data->cntl_cache & ~data->def->ctrl_masks[CNTL_MODE]) |
460 		 data->def->ctrl_modes[mode];
461 	ret = i2c_smbus_write_byte_data(data->client,
462 					data->def->ctrl_regs[CNTL], regval);
463 	if (ret < 0) {
464 		return ret;
465 	}
466 	data->cntl_cache = regval;
467 	/* After mode change wait atleast 100us */
468 	usleep_range(100, 500);
469 
470 	return 0;
471 }
472 
473 /*
474  * Handle data ready irq
475  */
476 static irqreturn_t ak8975_irq_handler(int irq, void *data)
477 {
478 	struct ak8975_data *ak8975 = data;
479 
480 	set_bit(0, &ak8975->flags);
481 	wake_up(&ak8975->data_ready_queue);
482 
483 	return IRQ_HANDLED;
484 }
485 
486 /*
487  * Install data ready interrupt handler
488  */
489 static int ak8975_setup_irq(struct ak8975_data *data)
490 {
491 	struct i2c_client *client = data->client;
492 	int rc;
493 	int irq;
494 
495 	init_waitqueue_head(&data->data_ready_queue);
496 	clear_bit(0, &data->flags);
497 	if (client->irq)
498 		irq = client->irq;
499 	else
500 		irq = gpiod_to_irq(data->eoc_gpiod);
501 
502 	rc = devm_request_irq(&client->dev, irq, ak8975_irq_handler,
503 			      IRQF_TRIGGER_RISING | IRQF_ONESHOT,
504 			      dev_name(&client->dev), data);
505 	if (rc < 0) {
506 		dev_err(&client->dev, "irq %d request failed: %d\n", irq, rc);
507 		return rc;
508 	}
509 
510 	data->eoc_irq = irq;
511 
512 	return rc;
513 }
514 
515 
516 /*
517  * Perform some start-of-day setup, including reading the asa calibration
518  * values and caching them.
519  */
520 static int ak8975_setup(struct i2c_client *client)
521 {
522 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
523 	struct ak8975_data *data = iio_priv(indio_dev);
524 	int ret;
525 
526 	/* Write the fused rom access mode. */
527 	ret = ak8975_set_mode(data, FUSE_ROM);
528 	if (ret < 0) {
529 		dev_err(&client->dev, "Error in setting fuse access mode\n");
530 		return ret;
531 	}
532 
533 	/* Get asa data and store in the device data. */
534 	ret = i2c_smbus_read_i2c_block_data_or_emulated(
535 			client, data->def->ctrl_regs[ASA_BASE],
536 			3, data->asa);
537 	if (ret < 0) {
538 		dev_err(&client->dev, "Not able to read asa data\n");
539 		return ret;
540 	}
541 
542 	/* After reading fuse ROM data set power-down mode */
543 	ret = ak8975_set_mode(data, POWER_DOWN);
544 	if (ret < 0) {
545 		dev_err(&client->dev, "Error in setting power-down mode\n");
546 		return ret;
547 	}
548 
549 	if (data->eoc_gpiod || client->irq > 0) {
550 		ret = ak8975_setup_irq(data);
551 		if (ret < 0) {
552 			dev_err(&client->dev,
553 				"Error setting data ready interrupt\n");
554 			return ret;
555 		}
556 	}
557 
558 	data->raw_to_gauss[0] = data->def->raw_to_gauss(data->asa[0]);
559 	data->raw_to_gauss[1] = data->def->raw_to_gauss(data->asa[1]);
560 	data->raw_to_gauss[2] = data->def->raw_to_gauss(data->asa[2]);
561 
562 	return 0;
563 }
564 
565 static int wait_conversion_complete_gpio(struct ak8975_data *data)
566 {
567 	struct i2c_client *client = data->client;
568 	u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
569 	int ret;
570 
571 	/* Wait for the conversion to complete. */
572 	while (timeout_ms) {
573 		msleep(AK8975_CONVERSION_DONE_POLL_TIME);
574 		if (gpiod_get_value(data->eoc_gpiod))
575 			break;
576 		timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
577 	}
578 	if (!timeout_ms) {
579 		dev_err(&client->dev, "Conversion timeout happened\n");
580 		return -EINVAL;
581 	}
582 
583 	ret = i2c_smbus_read_byte_data(client, data->def->ctrl_regs[ST1]);
584 	if (ret < 0)
585 		dev_err(&client->dev, "Error in reading ST1\n");
586 
587 	return ret;
588 }
589 
590 static int wait_conversion_complete_polled(struct ak8975_data *data)
591 {
592 	struct i2c_client *client = data->client;
593 	u8 read_status;
594 	u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
595 	int ret;
596 
597 	/* Wait for the conversion to complete. */
598 	while (timeout_ms) {
599 		msleep(AK8975_CONVERSION_DONE_POLL_TIME);
600 		ret = i2c_smbus_read_byte_data(client,
601 					       data->def->ctrl_regs[ST1]);
602 		if (ret < 0) {
603 			dev_err(&client->dev, "Error in reading ST1\n");
604 			return ret;
605 		}
606 		read_status = ret;
607 		if (read_status)
608 			break;
609 		timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
610 	}
611 	if (!timeout_ms) {
612 		dev_err(&client->dev, "Conversion timeout happened\n");
613 		return -EINVAL;
614 	}
615 
616 	return read_status;
617 }
618 
619 /* Returns 0 if the end of conversion interrupt occured or -ETIME otherwise */
620 static int wait_conversion_complete_interrupt(struct ak8975_data *data)
621 {
622 	int ret;
623 
624 	ret = wait_event_timeout(data->data_ready_queue,
625 				 test_bit(0, &data->flags),
626 				 AK8975_DATA_READY_TIMEOUT);
627 	clear_bit(0, &data->flags);
628 
629 	return ret > 0 ? 0 : -ETIME;
630 }
631 
632 static int ak8975_start_read_axis(struct ak8975_data *data,
633 				  const struct i2c_client *client)
634 {
635 	/* Set up the device for taking a sample. */
636 	int ret = ak8975_set_mode(data, MODE_ONCE);
637 
638 	if (ret < 0) {
639 		dev_err(&client->dev, "Error in setting operating mode\n");
640 		return ret;
641 	}
642 
643 	/* Wait for the conversion to complete. */
644 	if (data->eoc_irq)
645 		ret = wait_conversion_complete_interrupt(data);
646 	else if (data->eoc_gpiod)
647 		ret = wait_conversion_complete_gpio(data);
648 	else
649 		ret = wait_conversion_complete_polled(data);
650 	if (ret < 0)
651 		return ret;
652 
653 	/* This will be executed only for non-interrupt based waiting case */
654 	if (ret & data->def->ctrl_masks[ST1_DRDY]) {
655 		ret = i2c_smbus_read_byte_data(client,
656 					       data->def->ctrl_regs[ST2]);
657 		if (ret < 0) {
658 			dev_err(&client->dev, "Error in reading ST2\n");
659 			return ret;
660 		}
661 		if (ret & (data->def->ctrl_masks[ST2_DERR] |
662 			   data->def->ctrl_masks[ST2_HOFL])) {
663 			dev_err(&client->dev, "ST2 status error 0x%x\n", ret);
664 			return -EINVAL;
665 		}
666 	}
667 
668 	return 0;
669 }
670 
671 /* Retrieve raw flux value for one of the x, y, or z axis.  */
672 static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
673 {
674 	struct ak8975_data *data = iio_priv(indio_dev);
675 	const struct i2c_client *client = data->client;
676 	const struct ak_def *def = data->def;
677 	__le16 rval;
678 	u16 buff;
679 	int ret;
680 
681 	pm_runtime_get_sync(&data->client->dev);
682 
683 	mutex_lock(&data->lock);
684 
685 	ret = ak8975_start_read_axis(data, client);
686 	if (ret)
687 		goto exit;
688 
689 	ret = i2c_smbus_read_i2c_block_data_or_emulated(
690 			client, def->data_regs[index],
691 			sizeof(rval), (u8*)&rval);
692 	if (ret < 0)
693 		goto exit;
694 
695 	mutex_unlock(&data->lock);
696 
697 	pm_runtime_mark_last_busy(&data->client->dev);
698 	pm_runtime_put_autosuspend(&data->client->dev);
699 
700 	/* Swap bytes and convert to valid range. */
701 	buff = le16_to_cpu(rval);
702 	*val = clamp_t(s16, buff, -def->range, def->range);
703 	return IIO_VAL_INT;
704 
705 exit:
706 	mutex_unlock(&data->lock);
707 	dev_err(&client->dev, "Error in reading axis\n");
708 	return ret;
709 }
710 
711 static 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) {
719 	case IIO_CHAN_INFO_RAW:
720 		return ak8975_read_axis(indio_dev, chan->address, val);
721 	case IIO_CHAN_INFO_SCALE:
722 		*val = 0;
723 		*val2 = data->raw_to_gauss[chan->address];
724 		return IIO_VAL_INT_PLUS_MICRO;
725 	}
726 	return -EINVAL;
727 }
728 
729 static const struct iio_mount_matrix *
730 ak8975_get_mount_matrix(const struct iio_dev *indio_dev,
731 			const struct iio_chan_spec *chan)
732 {
733 	struct ak8975_data *data = iio_priv(indio_dev);
734 
735 	return &data->orientation;
736 }
737 
738 static const struct iio_chan_spec_ext_info ak8975_ext_info[] = {
739 	IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, ak8975_get_mount_matrix),
740 	{ }
741 };
742 
743 #define AK8975_CHANNEL(axis, index)					\
744 	{								\
745 		.type = IIO_MAGN,					\
746 		.modified = 1,						\
747 		.channel2 = IIO_MOD_##axis,				\
748 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |		\
749 			     BIT(IIO_CHAN_INFO_SCALE),			\
750 		.address = index,					\
751 		.scan_index = index,					\
752 		.scan_type = {						\
753 			.sign = 's',					\
754 			.realbits = 16,					\
755 			.storagebits = 16,				\
756 			.endianness = IIO_CPU				\
757 		},							\
758 		.ext_info = ak8975_ext_info,				\
759 	}
760 
761 static const struct iio_chan_spec ak8975_channels[] = {
762 	AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2),
763 	IIO_CHAN_SOFT_TIMESTAMP(3),
764 };
765 
766 static const unsigned long ak8975_scan_masks[] = { 0x7, 0 };
767 
768 static const struct iio_info ak8975_info = {
769 	.read_raw = &ak8975_read_raw,
770 };
771 
772 #ifdef CONFIG_ACPI
773 static const struct acpi_device_id ak_acpi_match[] = {
774 	{"AK8975", AK8975},
775 	{"AK8963", AK8963},
776 	{"INVN6500", AK8963},
777 	{"AK009911", AK09911},
778 	{"AK09911", AK09911},
779 	{"AKM9911", AK09911},
780 	{"AK09912", AK09912},
781 	{ }
782 };
783 MODULE_DEVICE_TABLE(acpi, ak_acpi_match);
784 #endif
785 
786 static const char *ak8975_match_acpi_device(struct device *dev,
787 					    enum asahi_compass_chipset *chipset)
788 {
789 	const struct acpi_device_id *id;
790 
791 	id = acpi_match_device(dev->driver->acpi_match_table, dev);
792 	if (!id)
793 		return NULL;
794 	*chipset = (int)id->driver_data;
795 
796 	return dev_name(dev);
797 }
798 
799 static void ak8975_fill_buffer(struct iio_dev *indio_dev)
800 {
801 	struct ak8975_data *data = iio_priv(indio_dev);
802 	const struct i2c_client *client = data->client;
803 	const struct ak_def *def = data->def;
804 	int ret;
805 	s16 buff[8]; /* 3 x 16 bits axis values + 1 aligned 64 bits timestamp */
806 	__le16 fval[3];
807 
808 	mutex_lock(&data->lock);
809 
810 	ret = ak8975_start_read_axis(data, client);
811 	if (ret)
812 		goto unlock;
813 
814 	/*
815 	 * For each axis, read the flux value from the appropriate register
816 	 * (the register is specified in the iio device attributes).
817 	 */
818 	ret = i2c_smbus_read_i2c_block_data_or_emulated(client,
819 							def->data_regs[0],
820 							3 * sizeof(fval[0]),
821 							(u8 *)fval);
822 	if (ret < 0)
823 		goto unlock;
824 
825 	mutex_unlock(&data->lock);
826 
827 	/* Clamp to valid range. */
828 	buff[0] = clamp_t(s16, le16_to_cpu(fval[0]), -def->range, def->range);
829 	buff[1] = clamp_t(s16, le16_to_cpu(fval[1]), -def->range, def->range);
830 	buff[2] = clamp_t(s16, le16_to_cpu(fval[2]), -def->range, def->range);
831 
832 	iio_push_to_buffers_with_timestamp(indio_dev, buff,
833 					   iio_get_time_ns(indio_dev));
834 	return;
835 
836 unlock:
837 	mutex_unlock(&data->lock);
838 	dev_err(&client->dev, "Error in reading axes block\n");
839 }
840 
841 static irqreturn_t ak8975_handle_trigger(int irq, void *p)
842 {
843 	const struct iio_poll_func *pf = p;
844 	struct iio_dev *indio_dev = pf->indio_dev;
845 
846 	ak8975_fill_buffer(indio_dev);
847 	iio_trigger_notify_done(indio_dev->trig);
848 	return IRQ_HANDLED;
849 }
850 
851 static int ak8975_probe(struct i2c_client *client,
852 			const struct i2c_device_id *id)
853 {
854 	struct ak8975_data *data;
855 	struct iio_dev *indio_dev;
856 	struct gpio_desc *eoc_gpiod;
857 	int err;
858 	const char *name = NULL;
859 	enum asahi_compass_chipset chipset = AK_MAX_TYPE;
860 	const struct ak8975_platform_data *pdata =
861 		dev_get_platdata(&client->dev);
862 
863 	/*
864 	 * Grab and set up the supplied GPIO.
865 	 * We may not have a GPIO based IRQ to scan, that is fine, we will
866 	 * poll if so.
867 	 */
868 	eoc_gpiod = devm_gpiod_get_optional(&client->dev, NULL, GPIOD_IN);
869 	if (IS_ERR(eoc_gpiod))
870 		return PTR_ERR(eoc_gpiod);
871 	if (eoc_gpiod)
872 		gpiod_set_consumer_name(eoc_gpiod, "ak_8975");
873 
874 	/* Register with IIO */
875 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
876 	if (indio_dev == NULL)
877 		return -ENOMEM;
878 
879 	data = iio_priv(indio_dev);
880 	i2c_set_clientdata(client, indio_dev);
881 
882 	data->client = client;
883 	data->eoc_gpiod = eoc_gpiod;
884 	data->eoc_irq = 0;
885 
886 	if (!pdata) {
887 		err = iio_read_mount_matrix(&client->dev, "mount-matrix",
888 					    &data->orientation);
889 		if (err)
890 			return err;
891 	} else
892 		data->orientation = pdata->orientation;
893 
894 	/* id will be NULL when enumerated via ACPI */
895 	if (id) {
896 		chipset = (enum asahi_compass_chipset)(id->driver_data);
897 		name = id->name;
898 	} else if (ACPI_HANDLE(&client->dev)) {
899 		name = ak8975_match_acpi_device(&client->dev, &chipset);
900 		if (!name)
901 			return -ENODEV;
902 	} else
903 		return -ENOSYS;
904 
905 	if (chipset >= AK_MAX_TYPE) {
906 		dev_err(&client->dev, "AKM device type unsupported: %d\n",
907 			chipset);
908 		return -ENODEV;
909 	}
910 
911 	data->def = &ak_def_array[chipset];
912 
913 	/* Fetch the regulators */
914 	data->vdd = devm_regulator_get(&client->dev, "vdd");
915 	if (IS_ERR(data->vdd))
916 		return PTR_ERR(data->vdd);
917 	data->vid = devm_regulator_get(&client->dev, "vid");
918 	if (IS_ERR(data->vid))
919 		return PTR_ERR(data->vid);
920 
921 	err = ak8975_power_on(data);
922 	if (err)
923 		return err;
924 
925 	err = ak8975_who_i_am(client, data->def->type);
926 	if (err < 0) {
927 		dev_err(&client->dev, "Unexpected device\n");
928 		goto power_off;
929 	}
930 	dev_dbg(&client->dev, "Asahi compass chip %s\n", name);
931 
932 	/* Perform some basic start-of-day setup of the device. */
933 	err = ak8975_setup(client);
934 	if (err < 0) {
935 		dev_err(&client->dev, "%s initialization fails\n", name);
936 		goto power_off;
937 	}
938 
939 	mutex_init(&data->lock);
940 	indio_dev->dev.parent = &client->dev;
941 	indio_dev->channels = ak8975_channels;
942 	indio_dev->num_channels = ARRAY_SIZE(ak8975_channels);
943 	indio_dev->info = &ak8975_info;
944 	indio_dev->available_scan_masks = ak8975_scan_masks;
945 	indio_dev->modes = INDIO_DIRECT_MODE;
946 	indio_dev->name = name;
947 
948 	err = iio_triggered_buffer_setup(indio_dev, NULL, ak8975_handle_trigger,
949 					 NULL);
950 	if (err) {
951 		dev_err(&client->dev, "triggered buffer setup failed\n");
952 		goto power_off;
953 	}
954 
955 	err = iio_device_register(indio_dev);
956 	if (err) {
957 		dev_err(&client->dev, "device register failed\n");
958 		goto cleanup_buffer;
959 	}
960 
961 	/* Enable runtime PM */
962 	pm_runtime_get_noresume(&client->dev);
963 	pm_runtime_set_active(&client->dev);
964 	pm_runtime_enable(&client->dev);
965 	/*
966 	 * The device comes online in 500us, so add two orders of magnitude
967 	 * of delay before autosuspending: 50 ms.
968 	 */
969 	pm_runtime_set_autosuspend_delay(&client->dev, 50);
970 	pm_runtime_use_autosuspend(&client->dev);
971 	pm_runtime_put(&client->dev);
972 
973 	return 0;
974 
975 cleanup_buffer:
976 	iio_triggered_buffer_cleanup(indio_dev);
977 power_off:
978 	ak8975_power_off(data);
979 	return err;
980 }
981 
982 static int ak8975_remove(struct i2c_client *client)
983 {
984 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
985 	struct ak8975_data *data = iio_priv(indio_dev);
986 
987 	pm_runtime_get_sync(&client->dev);
988 	pm_runtime_put_noidle(&client->dev);
989 	pm_runtime_disable(&client->dev);
990 	iio_device_unregister(indio_dev);
991 	iio_triggered_buffer_cleanup(indio_dev);
992 	ak8975_set_mode(data, POWER_DOWN);
993 	ak8975_power_off(data);
994 
995 	return 0;
996 }
997 
998 #ifdef CONFIG_PM
999 static int ak8975_runtime_suspend(struct device *dev)
1000 {
1001 	struct i2c_client *client = to_i2c_client(dev);
1002 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
1003 	struct ak8975_data *data = iio_priv(indio_dev);
1004 	int ret;
1005 
1006 	/* Set the device in power down if it wasn't already */
1007 	ret = ak8975_set_mode(data, POWER_DOWN);
1008 	if (ret < 0) {
1009 		dev_err(&client->dev, "Error in setting power-down mode\n");
1010 		return ret;
1011 	}
1012 	/* Next cut the regulators */
1013 	ak8975_power_off(data);
1014 
1015 	return 0;
1016 }
1017 
1018 static int ak8975_runtime_resume(struct device *dev)
1019 {
1020 	struct i2c_client *client = to_i2c_client(dev);
1021 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
1022 	struct ak8975_data *data = iio_priv(indio_dev);
1023 	int ret;
1024 
1025 	/* Take up the regulators */
1026 	ak8975_power_on(data);
1027 	/*
1028 	 * We come up in powered down mode, the reading routines will
1029 	 * put us in the mode to read values later.
1030 	 */
1031 	ret = ak8975_set_mode(data, POWER_DOWN);
1032 	if (ret < 0) {
1033 		dev_err(&client->dev, "Error in setting power-down mode\n");
1034 		return ret;
1035 	}
1036 
1037 	return 0;
1038 }
1039 #endif /* CONFIG_PM */
1040 
1041 static const struct dev_pm_ops ak8975_dev_pm_ops = {
1042 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1043 				pm_runtime_force_resume)
1044 	SET_RUNTIME_PM_OPS(ak8975_runtime_suspend,
1045 			   ak8975_runtime_resume, NULL)
1046 };
1047 
1048 static const struct i2c_device_id ak8975_id[] = {
1049 	{"ak8975", AK8975},
1050 	{"ak8963", AK8963},
1051 	{"AK8963", AK8963},
1052 	{"ak09911", AK09911},
1053 	{"ak09912", AK09912},
1054 	{}
1055 };
1056 
1057 MODULE_DEVICE_TABLE(i2c, ak8975_id);
1058 
1059 static const struct of_device_id ak8975_of_match[] = {
1060 	{ .compatible = "asahi-kasei,ak8975", },
1061 	{ .compatible = "ak8975", },
1062 	{ .compatible = "asahi-kasei,ak8963", },
1063 	{ .compatible = "ak8963", },
1064 	{ .compatible = "asahi-kasei,ak09911", },
1065 	{ .compatible = "ak09911", },
1066 	{ .compatible = "asahi-kasei,ak09912", },
1067 	{ .compatible = "ak09912", },
1068 	{}
1069 };
1070 MODULE_DEVICE_TABLE(of, ak8975_of_match);
1071 
1072 static struct i2c_driver ak8975_driver = {
1073 	.driver = {
1074 		.name	= "ak8975",
1075 		.pm = &ak8975_dev_pm_ops,
1076 		.of_match_table = of_match_ptr(ak8975_of_match),
1077 		.acpi_match_table = ACPI_PTR(ak_acpi_match),
1078 	},
1079 	.probe		= ak8975_probe,
1080 	.remove		= ak8975_remove,
1081 	.id_table	= ak8975_id,
1082 };
1083 module_i2c_driver(ak8975_driver);
1084 
1085 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
1086 MODULE_DESCRIPTION("AK8975 magnetometer driver");
1087 MODULE_LICENSE("GPL");
1088