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