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