• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Driver for the Asahi Kasei EMD Corporation AK8974
4  * and Aichi Steel AMI305 magnetometer chips.
5  * Based on a patch from Samu Onkalo and the AK8975 IIO driver.
6  *
7  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
8  * Copyright (c) 2010 NVIDIA Corporation.
9  * Copyright (C) 2016 Linaro Ltd.
10  *
11  * Author: Samu Onkalo <samu.p.onkalo@nokia.com>
12  * Author: Linus Walleij <linus.walleij@linaro.org>
13  */
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/i2c.h>
17 #include <linux/interrupt.h>
18 #include <linux/irq.h> /* For irq_get_irq_data() */
19 #include <linux/completion.h>
20 #include <linux/err.h>
21 #include <linux/mutex.h>
22 #include <linux/delay.h>
23 #include <linux/bitops.h>
24 #include <linux/random.h>
25 #include <linux/regmap.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/pm_runtime.h>
28 
29 #include <linux/iio/iio.h>
30 #include <linux/iio/sysfs.h>
31 #include <linux/iio/buffer.h>
32 #include <linux/iio/trigger.h>
33 #include <linux/iio/trigger_consumer.h>
34 #include <linux/iio/triggered_buffer.h>
35 
36 /*
37  * 16-bit registers are little-endian. LSB is at the address defined below
38  * and MSB is at the next higher address.
39  */
40 
41 /* These registers are common for AK8974 and AMI30x */
42 #define AK8974_SELFTEST		0x0C
43 #define AK8974_SELFTEST_IDLE	0x55
44 #define AK8974_SELFTEST_OK	0xAA
45 
46 #define AK8974_INFO		0x0D
47 
48 #define AK8974_WHOAMI		0x0F
49 #define AK8974_WHOAMI_VALUE_AMI306 0x46
50 #define AK8974_WHOAMI_VALUE_AMI305 0x47
51 #define AK8974_WHOAMI_VALUE_AK8974 0x48
52 
53 #define AK8974_DATA_X		0x10
54 #define AK8974_DATA_Y		0x12
55 #define AK8974_DATA_Z		0x14
56 #define AK8974_INT_SRC		0x16
57 #define AK8974_STATUS		0x18
58 #define AK8974_INT_CLEAR	0x1A
59 #define AK8974_CTRL1		0x1B
60 #define AK8974_CTRL2		0x1C
61 #define AK8974_CTRL3		0x1D
62 #define AK8974_INT_CTRL		0x1E
63 #define AK8974_INT_THRES	0x26  /* Absolute any axis value threshold */
64 #define AK8974_PRESET		0x30
65 
66 /* AK8974-specific offsets */
67 #define AK8974_OFFSET_X		0x20
68 #define AK8974_OFFSET_Y		0x22
69 #define AK8974_OFFSET_Z		0x24
70 /* AMI305-specific offsets */
71 #define AMI305_OFFSET_X		0x6C
72 #define AMI305_OFFSET_Y		0x72
73 #define AMI305_OFFSET_Z		0x78
74 
75 /* Different temperature registers */
76 #define AK8974_TEMP		0x31
77 #define AMI305_TEMP		0x60
78 
79 /* AMI306-specific control register */
80 #define AMI306_CTRL4		0x5C
81 
82 /* AMI306 factory calibration data */
83 
84 /* fine axis sensitivity */
85 #define AMI306_FINEOUTPUT_X	0x90
86 #define AMI306_FINEOUTPUT_Y	0x92
87 #define AMI306_FINEOUTPUT_Z	0x94
88 
89 /* axis sensitivity */
90 #define AMI306_SENS_X		0x96
91 #define AMI306_SENS_Y		0x98
92 #define AMI306_SENS_Z		0x9A
93 
94 /* axis cross-interference */
95 #define AMI306_GAIN_PARA_XZ	0x9C
96 #define AMI306_GAIN_PARA_XY	0x9D
97 #define AMI306_GAIN_PARA_YZ	0x9E
98 #define AMI306_GAIN_PARA_YX	0x9F
99 #define AMI306_GAIN_PARA_ZY	0xA0
100 #define AMI306_GAIN_PARA_ZX	0xA1
101 
102 /* offset at ZERO magnetic field */
103 #define AMI306_OFFZERO_X	0xF8
104 #define AMI306_OFFZERO_Y	0xFA
105 #define AMI306_OFFZERO_Z	0xFC
106 
107 
108 #define AK8974_INT_X_HIGH	BIT(7) /* Axis over +threshold  */
109 #define AK8974_INT_Y_HIGH	BIT(6)
110 #define AK8974_INT_Z_HIGH	BIT(5)
111 #define AK8974_INT_X_LOW	BIT(4) /* Axis below -threshold	*/
112 #define AK8974_INT_Y_LOW	BIT(3)
113 #define AK8974_INT_Z_LOW	BIT(2)
114 #define AK8974_INT_RANGE	BIT(1) /* Range overflow (any axis) */
115 
116 #define AK8974_STATUS_DRDY	BIT(6) /* Data ready */
117 #define AK8974_STATUS_OVERRUN	BIT(5) /* Data overrun */
118 #define AK8974_STATUS_INT	BIT(4) /* Interrupt occurred */
119 
120 #define AK8974_CTRL1_POWER	BIT(7) /* 0 = standby; 1 = active */
121 #define AK8974_CTRL1_RATE	BIT(4) /* 0 = 10 Hz; 1 = 20 Hz	 */
122 #define AK8974_CTRL1_FORCE_EN	BIT(1) /* 0 = normal; 1 = force	 */
123 #define AK8974_CTRL1_MODE2	BIT(0) /* 0 */
124 
125 #define AK8974_CTRL2_INT_EN	BIT(4)  /* 1 = enable interrupts	      */
126 #define AK8974_CTRL2_DRDY_EN	BIT(3)  /* 1 = enable data ready signal */
127 #define AK8974_CTRL2_DRDY_POL	BIT(2)  /* 1 = data ready active high   */
128 #define AK8974_CTRL2_RESDEF	(AK8974_CTRL2_DRDY_POL)
129 
130 #define AK8974_CTRL3_RESET	BIT(7) /* Software reset		  */
131 #define AK8974_CTRL3_FORCE	BIT(6) /* Start forced measurement */
132 #define AK8974_CTRL3_SELFTEST	BIT(4) /* Set selftest register	  */
133 #define AK8974_CTRL3_RESDEF	0x00
134 
135 #define AK8974_INT_CTRL_XEN	BIT(7) /* Enable interrupt for this axis */
136 #define AK8974_INT_CTRL_YEN	BIT(6)
137 #define AK8974_INT_CTRL_ZEN	BIT(5)
138 #define AK8974_INT_CTRL_XYZEN	(BIT(7)|BIT(6)|BIT(5))
139 #define AK8974_INT_CTRL_POL	BIT(3) /* 0 = active low; 1 = active high */
140 #define AK8974_INT_CTRL_PULSE	BIT(1) /* 0 = latched; 1 = pulse (50 usec) */
141 #define AK8974_INT_CTRL_RESDEF	(AK8974_INT_CTRL_XYZEN | AK8974_INT_CTRL_POL)
142 
143 /* The AMI305 has elaborate FW version and serial number registers */
144 #define AMI305_VER		0xE8
145 #define AMI305_SN		0xEA
146 
147 #define AK8974_MAX_RANGE	2048
148 
149 #define AK8974_POWERON_DELAY	50
150 #define AK8974_ACTIVATE_DELAY	1
151 #define AK8974_SELFTEST_DELAY	1
152 /*
153  * Set the autosuspend to two orders of magnitude larger than the poweron
154  * delay to make sane reasonable power tradeoff savings (5 seconds in
155  * this case).
156  */
157 #define AK8974_AUTOSUSPEND_DELAY 5000
158 
159 #define AK8974_MEASTIME		3
160 
161 #define AK8974_PWR_ON		1
162 #define AK8974_PWR_OFF		0
163 
164 /**
165  * struct ak8974 - state container for the AK8974 driver
166  * @i2c: parent I2C client
167  * @orientation: mounting matrix, flipped axis etc
168  * @map: regmap to access the AK8974 registers over I2C
169  * @regs: the avdd and dvdd power regulators
170  * @name: the name of the part
171  * @variant: the whoami ID value (for selecting code paths)
172  * @lock: locks the magnetometer for exclusive use during a measurement
173  * @drdy_irq: uses the DRDY IRQ line
174  * @drdy_complete: completion for DRDY
175  * @drdy_active_low: the DRDY IRQ is active low
176  */
177 struct ak8974 {
178 	struct i2c_client *i2c;
179 	struct iio_mount_matrix orientation;
180 	struct regmap *map;
181 	struct regulator_bulk_data regs[2];
182 	const char *name;
183 	u8 variant;
184 	struct mutex lock;
185 	bool drdy_irq;
186 	struct completion drdy_complete;
187 	bool drdy_active_low;
188 	/* Ensure timestamp is naturally aligned */
189 	struct {
190 		__le16 channels[3];
191 		s64 ts __aligned(8);
192 	} scan;
193 };
194 
195 static const char ak8974_reg_avdd[] = "avdd";
196 static const char ak8974_reg_dvdd[] = "dvdd";
197 
ak8974_get_u16_val(struct ak8974 * ak8974,u8 reg,u16 * val)198 static int ak8974_get_u16_val(struct ak8974 *ak8974, u8 reg, u16 *val)
199 {
200 	int ret;
201 	__le16 bulk;
202 
203 	ret = regmap_bulk_read(ak8974->map, reg, &bulk, 2);
204 	if (ret)
205 		return ret;
206 	*val = le16_to_cpu(bulk);
207 
208 	return 0;
209 }
210 
ak8974_set_u16_val(struct ak8974 * ak8974,u8 reg,u16 val)211 static int ak8974_set_u16_val(struct ak8974 *ak8974, u8 reg, u16 val)
212 {
213 	__le16 bulk = cpu_to_le16(val);
214 
215 	return regmap_bulk_write(ak8974->map, reg, &bulk, 2);
216 }
217 
ak8974_set_power(struct ak8974 * ak8974,bool mode)218 static int ak8974_set_power(struct ak8974 *ak8974, bool mode)
219 {
220 	int ret;
221 	u8 val;
222 
223 	val = mode ? AK8974_CTRL1_POWER : 0;
224 	val |= AK8974_CTRL1_FORCE_EN;
225 	ret = regmap_write(ak8974->map, AK8974_CTRL1, val);
226 	if (ret < 0)
227 		return ret;
228 
229 	if (mode)
230 		msleep(AK8974_ACTIVATE_DELAY);
231 
232 	return 0;
233 }
234 
ak8974_reset(struct ak8974 * ak8974)235 static int ak8974_reset(struct ak8974 *ak8974)
236 {
237 	int ret;
238 
239 	/* Power on to get register access. Sets CTRL1 reg to reset state */
240 	ret = ak8974_set_power(ak8974, AK8974_PWR_ON);
241 	if (ret)
242 		return ret;
243 	ret = regmap_write(ak8974->map, AK8974_CTRL2, AK8974_CTRL2_RESDEF);
244 	if (ret)
245 		return ret;
246 	ret = regmap_write(ak8974->map, AK8974_CTRL3, AK8974_CTRL3_RESDEF);
247 	if (ret)
248 		return ret;
249 	ret = regmap_write(ak8974->map, AK8974_INT_CTRL,
250 			   AK8974_INT_CTRL_RESDEF);
251 	if (ret)
252 		return ret;
253 
254 	/* After reset, power off is default state */
255 	return ak8974_set_power(ak8974, AK8974_PWR_OFF);
256 }
257 
ak8974_configure(struct ak8974 * ak8974)258 static int ak8974_configure(struct ak8974 *ak8974)
259 {
260 	int ret;
261 
262 	ret = regmap_write(ak8974->map, AK8974_CTRL2, AK8974_CTRL2_DRDY_EN |
263 			   AK8974_CTRL2_INT_EN);
264 	if (ret)
265 		return ret;
266 	ret = regmap_write(ak8974->map, AK8974_CTRL3, 0);
267 	if (ret)
268 		return ret;
269 	if (ak8974->variant == AK8974_WHOAMI_VALUE_AMI306) {
270 		/* magic from datasheet: set high-speed measurement mode */
271 		ret = ak8974_set_u16_val(ak8974, AMI306_CTRL4, 0xA07E);
272 		if (ret)
273 			return ret;
274 	}
275 	ret = regmap_write(ak8974->map, AK8974_INT_CTRL, AK8974_INT_CTRL_POL);
276 	if (ret)
277 		return ret;
278 
279 	return regmap_write(ak8974->map, AK8974_PRESET, 0);
280 }
281 
ak8974_trigmeas(struct ak8974 * ak8974)282 static int ak8974_trigmeas(struct ak8974 *ak8974)
283 {
284 	unsigned int clear;
285 	u8 mask;
286 	u8 val;
287 	int ret;
288 
289 	/* Clear any previous measurement overflow status */
290 	ret = regmap_read(ak8974->map, AK8974_INT_CLEAR, &clear);
291 	if (ret)
292 		return ret;
293 
294 	/* If we have a DRDY IRQ line, use it */
295 	if (ak8974->drdy_irq) {
296 		mask = AK8974_CTRL2_INT_EN |
297 			AK8974_CTRL2_DRDY_EN |
298 			AK8974_CTRL2_DRDY_POL;
299 		val = AK8974_CTRL2_DRDY_EN;
300 
301 		if (!ak8974->drdy_active_low)
302 			val |= AK8974_CTRL2_DRDY_POL;
303 
304 		init_completion(&ak8974->drdy_complete);
305 		ret = regmap_update_bits(ak8974->map, AK8974_CTRL2,
306 					 mask, val);
307 		if (ret)
308 			return ret;
309 	}
310 
311 	/* Force a measurement */
312 	return regmap_update_bits(ak8974->map,
313 				  AK8974_CTRL3,
314 				  AK8974_CTRL3_FORCE,
315 				  AK8974_CTRL3_FORCE);
316 }
317 
ak8974_await_drdy(struct ak8974 * ak8974)318 static int ak8974_await_drdy(struct ak8974 *ak8974)
319 {
320 	int timeout = 2;
321 	unsigned int val;
322 	int ret;
323 
324 	if (ak8974->drdy_irq) {
325 		ret = wait_for_completion_timeout(&ak8974->drdy_complete,
326 					1 + msecs_to_jiffies(1000));
327 		if (!ret) {
328 			dev_err(&ak8974->i2c->dev,
329 				"timeout waiting for DRDY IRQ\n");
330 			return -ETIMEDOUT;
331 		}
332 		return 0;
333 	}
334 
335 	/* Default delay-based poll loop */
336 	do {
337 		msleep(AK8974_MEASTIME);
338 		ret = regmap_read(ak8974->map, AK8974_STATUS, &val);
339 		if (ret < 0)
340 			return ret;
341 		if (val & AK8974_STATUS_DRDY)
342 			return 0;
343 	} while (--timeout);
344 
345 	dev_err(&ak8974->i2c->dev, "timeout waiting for DRDY\n");
346 	return -ETIMEDOUT;
347 }
348 
ak8974_getresult(struct ak8974 * ak8974,__le16 * result)349 static int ak8974_getresult(struct ak8974 *ak8974, __le16 *result)
350 {
351 	unsigned int src;
352 	int ret;
353 
354 	ret = ak8974_await_drdy(ak8974);
355 	if (ret)
356 		return ret;
357 	ret = regmap_read(ak8974->map, AK8974_INT_SRC, &src);
358 	if (ret < 0)
359 		return ret;
360 
361 	/* Out of range overflow! Strong magnet close? */
362 	if (src & AK8974_INT_RANGE) {
363 		dev_err(&ak8974->i2c->dev,
364 			"range overflow in sensor\n");
365 		return -ERANGE;
366 	}
367 
368 	ret = regmap_bulk_read(ak8974->map, AK8974_DATA_X, result, 6);
369 	if (ret)
370 		return ret;
371 
372 	return ret;
373 }
374 
ak8974_drdy_irq(int irq,void * d)375 static irqreturn_t ak8974_drdy_irq(int irq, void *d)
376 {
377 	struct ak8974 *ak8974 = d;
378 
379 	if (!ak8974->drdy_irq)
380 		return IRQ_NONE;
381 
382 	/* TODO: timestamp here to get good measurement stamps */
383 	return IRQ_WAKE_THREAD;
384 }
385 
ak8974_drdy_irq_thread(int irq,void * d)386 static irqreturn_t ak8974_drdy_irq_thread(int irq, void *d)
387 {
388 	struct ak8974 *ak8974 = d;
389 	unsigned int val;
390 	int ret;
391 
392 	/* Check if this was a DRDY from us */
393 	ret = regmap_read(ak8974->map, AK8974_STATUS, &val);
394 	if (ret < 0) {
395 		dev_err(&ak8974->i2c->dev, "error reading DRDY status\n");
396 		return IRQ_HANDLED;
397 	}
398 	if (val & AK8974_STATUS_DRDY) {
399 		/* Yes this was our IRQ */
400 		complete(&ak8974->drdy_complete);
401 		return IRQ_HANDLED;
402 	}
403 
404 	/* We may be on a shared IRQ, let the next client check */
405 	return IRQ_NONE;
406 }
407 
ak8974_selftest(struct ak8974 * ak8974)408 static int ak8974_selftest(struct ak8974 *ak8974)
409 {
410 	struct device *dev = &ak8974->i2c->dev;
411 	unsigned int val;
412 	int ret;
413 
414 	ret = regmap_read(ak8974->map, AK8974_SELFTEST, &val);
415 	if (ret)
416 		return ret;
417 	if (val != AK8974_SELFTEST_IDLE) {
418 		dev_err(dev, "selftest not idle before test\n");
419 		return -EIO;
420 	}
421 
422 	/* Trigger self-test */
423 	ret = regmap_update_bits(ak8974->map,
424 			AK8974_CTRL3,
425 			AK8974_CTRL3_SELFTEST,
426 			AK8974_CTRL3_SELFTEST);
427 	if (ret) {
428 		dev_err(dev, "could not write CTRL3\n");
429 		return ret;
430 	}
431 
432 	msleep(AK8974_SELFTEST_DELAY);
433 
434 	ret = regmap_read(ak8974->map, AK8974_SELFTEST, &val);
435 	if (ret)
436 		return ret;
437 	if (val != AK8974_SELFTEST_OK) {
438 		dev_err(dev, "selftest result NOT OK (%02x)\n", val);
439 		return -EIO;
440 	}
441 
442 	ret = regmap_read(ak8974->map, AK8974_SELFTEST, &val);
443 	if (ret)
444 		return ret;
445 	if (val != AK8974_SELFTEST_IDLE) {
446 		dev_err(dev, "selftest not idle after test (%02x)\n", val);
447 		return -EIO;
448 	}
449 	dev_dbg(dev, "passed self-test\n");
450 
451 	return 0;
452 }
453 
ak8974_read_calib_data(struct ak8974 * ak8974,unsigned int reg,__le16 * tab,size_t tab_size)454 static void ak8974_read_calib_data(struct ak8974 *ak8974, unsigned int reg,
455 				   __le16 *tab, size_t tab_size)
456 {
457 	int ret = regmap_bulk_read(ak8974->map, reg, tab, tab_size);
458 	if (ret) {
459 		memset(tab, 0xFF, tab_size);
460 		dev_warn(&ak8974->i2c->dev,
461 			 "can't read calibration data (regs %u..%zu): %d\n",
462 			 reg, reg + tab_size - 1, ret);
463 	} else {
464 		add_device_randomness(tab, tab_size);
465 	}
466 }
467 
ak8974_detect(struct ak8974 * ak8974)468 static int ak8974_detect(struct ak8974 *ak8974)
469 {
470 	unsigned int whoami;
471 	const char *name;
472 	int ret;
473 	unsigned int fw;
474 	u16 sn;
475 
476 	ret = regmap_read(ak8974->map, AK8974_WHOAMI, &whoami);
477 	if (ret)
478 		return ret;
479 
480 	name = "ami305";
481 
482 	switch (whoami) {
483 	case AK8974_WHOAMI_VALUE_AMI306:
484 		name = "ami306";
485 		/* fall-through */
486 	case AK8974_WHOAMI_VALUE_AMI305:
487 		ret = regmap_read(ak8974->map, AMI305_VER, &fw);
488 		if (ret)
489 			return ret;
490 		fw &= 0x7f; /* only bits 0 thru 6 valid */
491 		ret = ak8974_get_u16_val(ak8974, AMI305_SN, &sn);
492 		if (ret)
493 			return ret;
494 		add_device_randomness(&sn, sizeof(sn));
495 		dev_info(&ak8974->i2c->dev,
496 			 "detected %s, FW ver %02x, S/N: %04x\n",
497 			 name, fw, sn);
498 		break;
499 	case AK8974_WHOAMI_VALUE_AK8974:
500 		name = "ak8974";
501 		dev_info(&ak8974->i2c->dev, "detected AK8974\n");
502 		break;
503 	default:
504 		dev_err(&ak8974->i2c->dev, "unsupported device (%02x) ",
505 			whoami);
506 		return -ENODEV;
507 	}
508 
509 	ak8974->name = name;
510 	ak8974->variant = whoami;
511 
512 	if (whoami == AK8974_WHOAMI_VALUE_AMI306) {
513 		__le16 fab_data1[9], fab_data2[3];
514 		int i;
515 
516 		ak8974_read_calib_data(ak8974, AMI306_FINEOUTPUT_X,
517 				       fab_data1, sizeof(fab_data1));
518 		ak8974_read_calib_data(ak8974, AMI306_OFFZERO_X,
519 				       fab_data2, sizeof(fab_data2));
520 
521 		for (i = 0; i < 3; ++i) {
522 			static const char axis[3] = "XYZ";
523 			static const char pgaxis[6] = "ZYZXYX";
524 			unsigned offz = le16_to_cpu(fab_data2[i]) & 0x7F;
525 			unsigned fine = le16_to_cpu(fab_data1[i]);
526 			unsigned sens = le16_to_cpu(fab_data1[i + 3]);
527 			unsigned pgain1 = le16_to_cpu(fab_data1[i + 6]);
528 			unsigned pgain2 = pgain1 >> 8;
529 
530 			pgain1 &= 0xFF;
531 
532 			dev_info(&ak8974->i2c->dev,
533 				 "factory calibration for axis %c: offz=%u sens=%u fine=%u pga%c=%u pga%c=%u\n",
534 				 axis[i], offz, sens, fine, pgaxis[i * 2],
535 				 pgain1, pgaxis[i * 2 + 1], pgain2);
536 		}
537 	}
538 
539 	return 0;
540 }
541 
ak8974_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)542 static int ak8974_read_raw(struct iio_dev *indio_dev,
543 			   struct iio_chan_spec const *chan,
544 			   int *val, int *val2,
545 			   long mask)
546 {
547 	struct ak8974 *ak8974 = iio_priv(indio_dev);
548 	__le16 hw_values[3];
549 	int ret = -EINVAL;
550 
551 	pm_runtime_get_sync(&ak8974->i2c->dev);
552 	mutex_lock(&ak8974->lock);
553 
554 	switch (mask) {
555 	case IIO_CHAN_INFO_RAW:
556 		if (chan->address > 2) {
557 			dev_err(&ak8974->i2c->dev, "faulty channel address\n");
558 			ret = -EIO;
559 			goto out_unlock;
560 		}
561 		ret = ak8974_trigmeas(ak8974);
562 		if (ret)
563 			goto out_unlock;
564 		ret = ak8974_getresult(ak8974, hw_values);
565 		if (ret)
566 			goto out_unlock;
567 
568 		/*
569 		 * We read all axes and discard all but one, for optimized
570 		 * reading, use the triggered buffer.
571 		 */
572 		*val = (s16)le16_to_cpu(hw_values[chan->address]);
573 
574 		ret = IIO_VAL_INT;
575 	}
576 
577  out_unlock:
578 	mutex_unlock(&ak8974->lock);
579 	pm_runtime_mark_last_busy(&ak8974->i2c->dev);
580 	pm_runtime_put_autosuspend(&ak8974->i2c->dev);
581 
582 	return ret;
583 }
584 
ak8974_fill_buffer(struct iio_dev * indio_dev)585 static void ak8974_fill_buffer(struct iio_dev *indio_dev)
586 {
587 	struct ak8974 *ak8974 = iio_priv(indio_dev);
588 	int ret;
589 
590 	pm_runtime_get_sync(&ak8974->i2c->dev);
591 	mutex_lock(&ak8974->lock);
592 
593 	ret = ak8974_trigmeas(ak8974);
594 	if (ret) {
595 		dev_err(&ak8974->i2c->dev, "error triggering measure\n");
596 		goto out_unlock;
597 	}
598 	ret = ak8974_getresult(ak8974, ak8974->scan.channels);
599 	if (ret) {
600 		dev_err(&ak8974->i2c->dev, "error getting measures\n");
601 		goto out_unlock;
602 	}
603 
604 	iio_push_to_buffers_with_timestamp(indio_dev, &ak8974->scan,
605 					   iio_get_time_ns(indio_dev));
606 
607  out_unlock:
608 	mutex_unlock(&ak8974->lock);
609 	pm_runtime_mark_last_busy(&ak8974->i2c->dev);
610 	pm_runtime_put_autosuspend(&ak8974->i2c->dev);
611 }
612 
ak8974_handle_trigger(int irq,void * p)613 static irqreturn_t ak8974_handle_trigger(int irq, void *p)
614 {
615 	const struct iio_poll_func *pf = p;
616 	struct iio_dev *indio_dev = pf->indio_dev;
617 
618 	ak8974_fill_buffer(indio_dev);
619 	iio_trigger_notify_done(indio_dev->trig);
620 
621 	return IRQ_HANDLED;
622 }
623 
624 static const struct iio_mount_matrix *
ak8974_get_mount_matrix(const struct iio_dev * indio_dev,const struct iio_chan_spec * chan)625 ak8974_get_mount_matrix(const struct iio_dev *indio_dev,
626 			const struct iio_chan_spec *chan)
627 {
628 	struct ak8974 *ak8974 = iio_priv(indio_dev);
629 
630 	return &ak8974->orientation;
631 }
632 
633 static const struct iio_chan_spec_ext_info ak8974_ext_info[] = {
634 	IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, ak8974_get_mount_matrix),
635 	{ },
636 };
637 
638 #define AK8974_AXIS_CHANNEL(axis, index)				\
639 	{								\
640 		.type = IIO_MAGN,					\
641 		.modified = 1,						\
642 		.channel2 = IIO_MOD_##axis,				\
643 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
644 		.ext_info = ak8974_ext_info,				\
645 		.address = index,					\
646 		.scan_index = index,					\
647 		.scan_type = {						\
648 			.sign = 's',					\
649 			.realbits = 16,					\
650 			.storagebits = 16,				\
651 			.endianness = IIO_LE				\
652 		},							\
653 	}
654 
655 static const struct iio_chan_spec ak8974_channels[] = {
656 	AK8974_AXIS_CHANNEL(X, 0),
657 	AK8974_AXIS_CHANNEL(Y, 1),
658 	AK8974_AXIS_CHANNEL(Z, 2),
659 	IIO_CHAN_SOFT_TIMESTAMP(3),
660 };
661 
662 static const unsigned long ak8974_scan_masks[] = { 0x7, 0 };
663 
664 static const struct iio_info ak8974_info = {
665 	.read_raw = &ak8974_read_raw,
666 };
667 
ak8974_writeable_reg(struct device * dev,unsigned int reg)668 static bool ak8974_writeable_reg(struct device *dev, unsigned int reg)
669 {
670 	struct i2c_client *i2c = to_i2c_client(dev);
671 	struct iio_dev *indio_dev = i2c_get_clientdata(i2c);
672 	struct ak8974 *ak8974 = iio_priv(indio_dev);
673 
674 	switch (reg) {
675 	case AK8974_CTRL1:
676 	case AK8974_CTRL2:
677 	case AK8974_CTRL3:
678 	case AK8974_INT_CTRL:
679 	case AK8974_INT_THRES:
680 	case AK8974_INT_THRES + 1:
681 	case AK8974_PRESET:
682 	case AK8974_PRESET + 1:
683 		return true;
684 	case AK8974_OFFSET_X:
685 	case AK8974_OFFSET_X + 1:
686 	case AK8974_OFFSET_Y:
687 	case AK8974_OFFSET_Y + 1:
688 	case AK8974_OFFSET_Z:
689 	case AK8974_OFFSET_Z + 1:
690 		if (ak8974->variant == AK8974_WHOAMI_VALUE_AK8974)
691 			return true;
692 		return false;
693 	case AMI305_OFFSET_X:
694 	case AMI305_OFFSET_X + 1:
695 	case AMI305_OFFSET_Y:
696 	case AMI305_OFFSET_Y + 1:
697 	case AMI305_OFFSET_Z:
698 	case AMI305_OFFSET_Z + 1:
699 		return ak8974->variant == AK8974_WHOAMI_VALUE_AMI305 ||
700 		       ak8974->variant == AK8974_WHOAMI_VALUE_AMI306;
701 	case AMI306_CTRL4:
702 	case AMI306_CTRL4 + 1:
703 		return ak8974->variant == AK8974_WHOAMI_VALUE_AMI306;
704 	default:
705 		return false;
706 	}
707 }
708 
ak8974_precious_reg(struct device * dev,unsigned int reg)709 static bool ak8974_precious_reg(struct device *dev, unsigned int reg)
710 {
711 	return reg == AK8974_INT_CLEAR;
712 }
713 
714 static const struct regmap_config ak8974_regmap_config = {
715 	.reg_bits = 8,
716 	.val_bits = 8,
717 	.max_register = 0xff,
718 	.writeable_reg = ak8974_writeable_reg,
719 	.precious_reg = ak8974_precious_reg,
720 };
721 
ak8974_probe(struct i2c_client * i2c,const struct i2c_device_id * id)722 static int ak8974_probe(struct i2c_client *i2c,
723 			const struct i2c_device_id *id)
724 {
725 	struct iio_dev *indio_dev;
726 	struct ak8974 *ak8974;
727 	unsigned long irq_trig;
728 	int irq = i2c->irq;
729 	int ret;
730 
731 	/* Register with IIO */
732 	indio_dev = devm_iio_device_alloc(&i2c->dev, sizeof(*ak8974));
733 	if (indio_dev == NULL)
734 		return -ENOMEM;
735 
736 	ak8974 = iio_priv(indio_dev);
737 	i2c_set_clientdata(i2c, indio_dev);
738 	ak8974->i2c = i2c;
739 	mutex_init(&ak8974->lock);
740 
741 	ret = iio_read_mount_matrix(&i2c->dev, "mount-matrix",
742 				    &ak8974->orientation);
743 	if (ret)
744 		return ret;
745 
746 	ak8974->regs[0].supply = ak8974_reg_avdd;
747 	ak8974->regs[1].supply = ak8974_reg_dvdd;
748 
749 	ret = devm_regulator_bulk_get(&i2c->dev,
750 				      ARRAY_SIZE(ak8974->regs),
751 				      ak8974->regs);
752 	if (ret < 0) {
753 		dev_err(&i2c->dev, "cannot get regulators\n");
754 		return ret;
755 	}
756 
757 	ret = regulator_bulk_enable(ARRAY_SIZE(ak8974->regs), ak8974->regs);
758 	if (ret < 0) {
759 		dev_err(&i2c->dev, "cannot enable regulators\n");
760 		return ret;
761 	}
762 
763 	/* Take runtime PM online */
764 	pm_runtime_get_noresume(&i2c->dev);
765 	pm_runtime_set_active(&i2c->dev);
766 	pm_runtime_enable(&i2c->dev);
767 
768 	ak8974->map = devm_regmap_init_i2c(i2c, &ak8974_regmap_config);
769 	if (IS_ERR(ak8974->map)) {
770 		dev_err(&i2c->dev, "failed to allocate register map\n");
771 		pm_runtime_put_noidle(&i2c->dev);
772 		pm_runtime_disable(&i2c->dev);
773 		return PTR_ERR(ak8974->map);
774 	}
775 
776 	ret = ak8974_set_power(ak8974, AK8974_PWR_ON);
777 	if (ret) {
778 		dev_err(&i2c->dev, "could not power on\n");
779 		goto disable_pm;
780 	}
781 
782 	ret = ak8974_detect(ak8974);
783 	if (ret) {
784 		dev_err(&i2c->dev, "neither AK8974 nor AMI30x found\n");
785 		goto disable_pm;
786 	}
787 
788 	ret = ak8974_selftest(ak8974);
789 	if (ret)
790 		dev_err(&i2c->dev, "selftest failed (continuing anyway)\n");
791 
792 	ret = ak8974_reset(ak8974);
793 	if (ret) {
794 		dev_err(&i2c->dev, "AK8974 reset failed\n");
795 		goto disable_pm;
796 	}
797 
798 	indio_dev->dev.parent = &i2c->dev;
799 	indio_dev->channels = ak8974_channels;
800 	indio_dev->num_channels = ARRAY_SIZE(ak8974_channels);
801 	indio_dev->info = &ak8974_info;
802 	indio_dev->available_scan_masks = ak8974_scan_masks;
803 	indio_dev->modes = INDIO_DIRECT_MODE;
804 	indio_dev->name = ak8974->name;
805 
806 	ret = iio_triggered_buffer_setup(indio_dev, NULL,
807 					 ak8974_handle_trigger,
808 					 NULL);
809 	if (ret) {
810 		dev_err(&i2c->dev, "triggered buffer setup failed\n");
811 		goto disable_pm;
812 	}
813 
814 	/* If we have a valid DRDY IRQ, make use of it */
815 	if (irq > 0) {
816 		irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq));
817 		if (irq_trig == IRQF_TRIGGER_RISING) {
818 			dev_info(&i2c->dev, "enable rising edge DRDY IRQ\n");
819 		} else if (irq_trig == IRQF_TRIGGER_FALLING) {
820 			ak8974->drdy_active_low = true;
821 			dev_info(&i2c->dev, "enable falling edge DRDY IRQ\n");
822 		} else {
823 			irq_trig = IRQF_TRIGGER_RISING;
824 		}
825 		irq_trig |= IRQF_ONESHOT;
826 		irq_trig |= IRQF_SHARED;
827 
828 		ret = devm_request_threaded_irq(&i2c->dev,
829 						irq,
830 						ak8974_drdy_irq,
831 						ak8974_drdy_irq_thread,
832 						irq_trig,
833 						ak8974->name,
834 						ak8974);
835 		if (ret) {
836 			dev_err(&i2c->dev, "unable to request DRDY IRQ "
837 				"- proceeding without IRQ\n");
838 			goto no_irq;
839 		}
840 		ak8974->drdy_irq = true;
841 	}
842 
843 no_irq:
844 	ret = iio_device_register(indio_dev);
845 	if (ret) {
846 		dev_err(&i2c->dev, "device register failed\n");
847 		goto cleanup_buffer;
848 	}
849 
850 	pm_runtime_set_autosuspend_delay(&i2c->dev,
851 					 AK8974_AUTOSUSPEND_DELAY);
852 	pm_runtime_use_autosuspend(&i2c->dev);
853 	pm_runtime_put(&i2c->dev);
854 
855 	return 0;
856 
857 cleanup_buffer:
858 	iio_triggered_buffer_cleanup(indio_dev);
859 disable_pm:
860 	pm_runtime_put_noidle(&i2c->dev);
861 	pm_runtime_disable(&i2c->dev);
862 	ak8974_set_power(ak8974, AK8974_PWR_OFF);
863 	regulator_bulk_disable(ARRAY_SIZE(ak8974->regs), ak8974->regs);
864 
865 	return ret;
866 }
867 
ak8974_remove(struct i2c_client * i2c)868 static int ak8974_remove(struct i2c_client *i2c)
869 {
870 	struct iio_dev *indio_dev = i2c_get_clientdata(i2c);
871 	struct ak8974 *ak8974 = iio_priv(indio_dev);
872 
873 	iio_device_unregister(indio_dev);
874 	iio_triggered_buffer_cleanup(indio_dev);
875 	pm_runtime_get_sync(&i2c->dev);
876 	pm_runtime_put_noidle(&i2c->dev);
877 	pm_runtime_disable(&i2c->dev);
878 	ak8974_set_power(ak8974, AK8974_PWR_OFF);
879 	regulator_bulk_disable(ARRAY_SIZE(ak8974->regs), ak8974->regs);
880 
881 	return 0;
882 }
883 
ak8974_runtime_suspend(struct device * dev)884 static int __maybe_unused ak8974_runtime_suspend(struct device *dev)
885 {
886 	struct ak8974 *ak8974 =
887 		iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
888 
889 	ak8974_set_power(ak8974, AK8974_PWR_OFF);
890 	regulator_bulk_disable(ARRAY_SIZE(ak8974->regs), ak8974->regs);
891 
892 	return 0;
893 }
894 
ak8974_runtime_resume(struct device * dev)895 static int __maybe_unused ak8974_runtime_resume(struct device *dev)
896 {
897 	struct ak8974 *ak8974 =
898 		iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
899 	int ret;
900 
901 	ret = regulator_bulk_enable(ARRAY_SIZE(ak8974->regs), ak8974->regs);
902 	if (ret)
903 		return ret;
904 	msleep(AK8974_POWERON_DELAY);
905 	ret = ak8974_set_power(ak8974, AK8974_PWR_ON);
906 	if (ret)
907 		goto out_regulator_disable;
908 
909 	ret = ak8974_configure(ak8974);
910 	if (ret)
911 		goto out_disable_power;
912 
913 	return 0;
914 
915 out_disable_power:
916 	ak8974_set_power(ak8974, AK8974_PWR_OFF);
917 out_regulator_disable:
918 	regulator_bulk_disable(ARRAY_SIZE(ak8974->regs), ak8974->regs);
919 
920 	return ret;
921 }
922 
923 static const struct dev_pm_ops ak8974_dev_pm_ops = {
924 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
925 				pm_runtime_force_resume)
926 	SET_RUNTIME_PM_OPS(ak8974_runtime_suspend,
927 			   ak8974_runtime_resume, NULL)
928 };
929 
930 static const struct i2c_device_id ak8974_id[] = {
931 	{"ami305", 0 },
932 	{"ami306", 0 },
933 	{"ak8974", 0 },
934 	{}
935 };
936 MODULE_DEVICE_TABLE(i2c, ak8974_id);
937 
938 static const struct of_device_id ak8974_of_match[] = {
939 	{ .compatible = "asahi-kasei,ak8974", },
940 	{}
941 };
942 MODULE_DEVICE_TABLE(of, ak8974_of_match);
943 
944 static struct i2c_driver ak8974_driver = {
945 	.driver	 = {
946 		.name	= "ak8974",
947 		.pm = &ak8974_dev_pm_ops,
948 		.of_match_table = of_match_ptr(ak8974_of_match),
949 	},
950 	.probe	  = ak8974_probe,
951 	.remove	  = ak8974_remove,
952 	.id_table = ak8974_id,
953 };
954 module_i2c_driver(ak8974_driver);
955 
956 MODULE_DESCRIPTION("AK8974 and AMI30x 3-axis magnetometer driver");
957 MODULE_AUTHOR("Samu Onkalo");
958 MODULE_AUTHOR("Linus Walleij");
959 MODULE_LICENSE("GPL v2");
960