treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / iio / magnetometer / ak8975.c
blob3c881541ae72f0a2df69320ec9bb82116696511f
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * A sensor driver for the magnetometer AK8975.
5 * Magnetic compass sensor driver for monitoring magnetic flux information.
7 * Copyright (c) 2010, NVIDIA Corporation.
8 */
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
14 #include <linux/interrupt.h>
15 #include <linux/err.h>
16 #include <linux/mutex.h>
17 #include <linux/delay.h>
18 #include <linux/bitops.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/acpi.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/pm_runtime.h>
24 #include <linux/iio/iio.h>
25 #include <linux/iio/sysfs.h>
26 #include <linux/iio/buffer.h>
27 #include <linux/iio/trigger.h>
28 #include <linux/iio/trigger_consumer.h>
29 #include <linux/iio/triggered_buffer.h>
32 * Register definitions, as well as various shifts and masks to get at the
33 * individual fields of the registers.
35 #define AK8975_REG_WIA 0x00
36 #define AK8975_DEVICE_ID 0x48
38 #define AK8975_REG_INFO 0x01
40 #define AK8975_REG_ST1 0x02
41 #define AK8975_REG_ST1_DRDY_SHIFT 0
42 #define AK8975_REG_ST1_DRDY_MASK (1 << AK8975_REG_ST1_DRDY_SHIFT)
44 #define AK8975_REG_HXL 0x03
45 #define AK8975_REG_HXH 0x04
46 #define AK8975_REG_HYL 0x05
47 #define AK8975_REG_HYH 0x06
48 #define AK8975_REG_HZL 0x07
49 #define AK8975_REG_HZH 0x08
50 #define AK8975_REG_ST2 0x09
51 #define AK8975_REG_ST2_DERR_SHIFT 2
52 #define AK8975_REG_ST2_DERR_MASK (1 << AK8975_REG_ST2_DERR_SHIFT)
54 #define AK8975_REG_ST2_HOFL_SHIFT 3
55 #define AK8975_REG_ST2_HOFL_MASK (1 << AK8975_REG_ST2_HOFL_SHIFT)
57 #define AK8975_REG_CNTL 0x0A
58 #define AK8975_REG_CNTL_MODE_SHIFT 0
59 #define AK8975_REG_CNTL_MODE_MASK (0xF << AK8975_REG_CNTL_MODE_SHIFT)
60 #define AK8975_REG_CNTL_MODE_POWER_DOWN 0x00
61 #define AK8975_REG_CNTL_MODE_ONCE 0x01
62 #define AK8975_REG_CNTL_MODE_SELF_TEST 0x08
63 #define AK8975_REG_CNTL_MODE_FUSE_ROM 0x0F
65 #define AK8975_REG_RSVC 0x0B
66 #define AK8975_REG_ASTC 0x0C
67 #define AK8975_REG_TS1 0x0D
68 #define AK8975_REG_TS2 0x0E
69 #define AK8975_REG_I2CDIS 0x0F
70 #define AK8975_REG_ASAX 0x10
71 #define AK8975_REG_ASAY 0x11
72 #define AK8975_REG_ASAZ 0x12
74 #define AK8975_MAX_REGS AK8975_REG_ASAZ
77 * AK09912 Register definitions
79 #define AK09912_REG_WIA1 0x00
80 #define AK09912_REG_WIA2 0x01
81 #define AK09912_DEVICE_ID 0x04
82 #define AK09911_DEVICE_ID 0x05
84 #define AK09911_REG_INFO1 0x02
85 #define AK09911_REG_INFO2 0x03
87 #define AK09912_REG_ST1 0x10
89 #define AK09912_REG_ST1_DRDY_SHIFT 0
90 #define AK09912_REG_ST1_DRDY_MASK (1 << AK09912_REG_ST1_DRDY_SHIFT)
92 #define AK09912_REG_HXL 0x11
93 #define AK09912_REG_HXH 0x12
94 #define AK09912_REG_HYL 0x13
95 #define AK09912_REG_HYH 0x14
96 #define AK09912_REG_HZL 0x15
97 #define AK09912_REG_HZH 0x16
98 #define AK09912_REG_TMPS 0x17
100 #define AK09912_REG_ST2 0x18
101 #define AK09912_REG_ST2_HOFL_SHIFT 3
102 #define AK09912_REG_ST2_HOFL_MASK (1 << AK09912_REG_ST2_HOFL_SHIFT)
104 #define AK09912_REG_CNTL1 0x30
106 #define AK09912_REG_CNTL2 0x31
107 #define AK09912_REG_CNTL_MODE_POWER_DOWN 0x00
108 #define AK09912_REG_CNTL_MODE_ONCE 0x01
109 #define AK09912_REG_CNTL_MODE_SELF_TEST 0x10
110 #define AK09912_REG_CNTL_MODE_FUSE_ROM 0x1F
111 #define AK09912_REG_CNTL2_MODE_SHIFT 0
112 #define AK09912_REG_CNTL2_MODE_MASK (0x1F << AK09912_REG_CNTL2_MODE_SHIFT)
114 #define AK09912_REG_CNTL3 0x32
116 #define AK09912_REG_TS1 0x33
117 #define AK09912_REG_TS2 0x34
118 #define AK09912_REG_TS3 0x35
119 #define AK09912_REG_I2CDIS 0x36
120 #define AK09912_REG_TS4 0x37
122 #define AK09912_REG_ASAX 0x60
123 #define AK09912_REG_ASAY 0x61
124 #define AK09912_REG_ASAZ 0x62
126 #define AK09912_MAX_REGS AK09912_REG_ASAZ
129 * Miscellaneous values.
131 #define AK8975_MAX_CONVERSION_TIMEOUT 500
132 #define AK8975_CONVERSION_DONE_POLL_TIME 10
133 #define AK8975_DATA_READY_TIMEOUT ((100*HZ)/1000)
136 * Precalculate scale factor (in Gauss units) for each axis and
137 * store in the device data.
139 * This scale factor is axis-dependent, and is derived from 3 calibration
140 * factors ASA(x), ASA(y), and ASA(z).
142 * These ASA values are read from the sensor device at start of day, and
143 * cached in the device context struct.
145 * Adjusting the flux value with the sensitivity adjustment value should be
146 * done via the following formula:
148 * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
149 * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
150 * is the resultant adjusted value.
152 * We reduce the formula to:
154 * Hadj = H * (ASA + 128) / 256
156 * H is in the range of -4096 to 4095. The magnetometer has a range of
157 * +-1229uT. To go from the raw value to uT is:
159 * HuT = H * 1229/4096, or roughly, 3/10.
161 * Since 1uT = 0.01 gauss, our final scale factor becomes:
163 * Hadj = H * ((ASA + 128) / 256) * 3/10 * 1/100
164 * Hadj = H * ((ASA + 128) * 0.003) / 256
166 * Since ASA doesn't change, we cache the resultant scale factor into the
167 * device context in ak8975_setup().
169 * Given we use IIO_VAL_INT_PLUS_MICRO bit when displaying the scale, we
170 * multiply the stored scale value by 1e6.
172 static long ak8975_raw_to_gauss(u16 data)
174 return (((long)data + 128) * 3000) / 256;
178 * For AK8963 and AK09911, same calculation, but the device is less sensitive:
180 * H is in the range of +-8190. The magnetometer has a range of
181 * +-4912uT. To go from the raw value to uT is:
183 * HuT = H * 4912/8190, or roughly, 6/10, instead of 3/10.
186 static long ak8963_09911_raw_to_gauss(u16 data)
188 return (((long)data + 128) * 6000) / 256;
192 * For AK09912, same calculation, except the device is more sensitive:
194 * H is in the range of -32752 to 32752. The magnetometer has a range of
195 * +-4912uT. To go from the raw value to uT is:
197 * HuT = H * 4912/32752, or roughly, 3/20, instead of 3/10.
199 static long ak09912_raw_to_gauss(u16 data)
201 return (((long)data + 128) * 1500) / 256;
204 /* Compatible Asahi Kasei Compass parts */
205 enum asahi_compass_chipset {
206 AKXXXX = 0,
207 AK8975,
208 AK8963,
209 AK09911,
210 AK09912,
213 enum ak_ctrl_reg_addr {
214 ST1,
215 ST2,
216 CNTL,
217 ASA_BASE,
218 MAX_REGS,
219 REGS_END,
222 enum ak_ctrl_reg_mask {
223 ST1_DRDY,
224 ST2_HOFL,
225 ST2_DERR,
226 CNTL_MODE,
227 MASK_END,
230 enum ak_ctrl_mode {
231 POWER_DOWN,
232 MODE_ONCE,
233 SELF_TEST,
234 FUSE_ROM,
235 MODE_END,
238 struct ak_def {
239 enum asahi_compass_chipset type;
240 long (*raw_to_gauss)(u16 data);
241 u16 range;
242 u8 ctrl_regs[REGS_END];
243 u8 ctrl_masks[MASK_END];
244 u8 ctrl_modes[MODE_END];
245 u8 data_regs[3];
248 static const struct ak_def ak_def_array[] = {
250 .type = AK8975,
251 .raw_to_gauss = ak8975_raw_to_gauss,
252 .range = 4096,
253 .ctrl_regs = {
254 AK8975_REG_ST1,
255 AK8975_REG_ST2,
256 AK8975_REG_CNTL,
257 AK8975_REG_ASAX,
258 AK8975_MAX_REGS},
259 .ctrl_masks = {
260 AK8975_REG_ST1_DRDY_MASK,
261 AK8975_REG_ST2_HOFL_MASK,
262 AK8975_REG_ST2_DERR_MASK,
263 AK8975_REG_CNTL_MODE_MASK},
264 .ctrl_modes = {
265 AK8975_REG_CNTL_MODE_POWER_DOWN,
266 AK8975_REG_CNTL_MODE_ONCE,
267 AK8975_REG_CNTL_MODE_SELF_TEST,
268 AK8975_REG_CNTL_MODE_FUSE_ROM},
269 .data_regs = {
270 AK8975_REG_HXL,
271 AK8975_REG_HYL,
272 AK8975_REG_HZL},
275 .type = AK8963,
276 .raw_to_gauss = ak8963_09911_raw_to_gauss,
277 .range = 8190,
278 .ctrl_regs = {
279 AK8975_REG_ST1,
280 AK8975_REG_ST2,
281 AK8975_REG_CNTL,
282 AK8975_REG_ASAX,
283 AK8975_MAX_REGS},
284 .ctrl_masks = {
285 AK8975_REG_ST1_DRDY_MASK,
286 AK8975_REG_ST2_HOFL_MASK,
288 AK8975_REG_CNTL_MODE_MASK},
289 .ctrl_modes = {
290 AK8975_REG_CNTL_MODE_POWER_DOWN,
291 AK8975_REG_CNTL_MODE_ONCE,
292 AK8975_REG_CNTL_MODE_SELF_TEST,
293 AK8975_REG_CNTL_MODE_FUSE_ROM},
294 .data_regs = {
295 AK8975_REG_HXL,
296 AK8975_REG_HYL,
297 AK8975_REG_HZL},
300 .type = AK09911,
301 .raw_to_gauss = ak8963_09911_raw_to_gauss,
302 .range = 8192,
303 .ctrl_regs = {
304 AK09912_REG_ST1,
305 AK09912_REG_ST2,
306 AK09912_REG_CNTL2,
307 AK09912_REG_ASAX,
308 AK09912_MAX_REGS},
309 .ctrl_masks = {
310 AK09912_REG_ST1_DRDY_MASK,
311 AK09912_REG_ST2_HOFL_MASK,
313 AK09912_REG_CNTL2_MODE_MASK},
314 .ctrl_modes = {
315 AK09912_REG_CNTL_MODE_POWER_DOWN,
316 AK09912_REG_CNTL_MODE_ONCE,
317 AK09912_REG_CNTL_MODE_SELF_TEST,
318 AK09912_REG_CNTL_MODE_FUSE_ROM},
319 .data_regs = {
320 AK09912_REG_HXL,
321 AK09912_REG_HYL,
322 AK09912_REG_HZL},
325 .type = AK09912,
326 .raw_to_gauss = ak09912_raw_to_gauss,
327 .range = 32752,
328 .ctrl_regs = {
329 AK09912_REG_ST1,
330 AK09912_REG_ST2,
331 AK09912_REG_CNTL2,
332 AK09912_REG_ASAX,
333 AK09912_MAX_REGS},
334 .ctrl_masks = {
335 AK09912_REG_ST1_DRDY_MASK,
336 AK09912_REG_ST2_HOFL_MASK,
338 AK09912_REG_CNTL2_MODE_MASK},
339 .ctrl_modes = {
340 AK09912_REG_CNTL_MODE_POWER_DOWN,
341 AK09912_REG_CNTL_MODE_ONCE,
342 AK09912_REG_CNTL_MODE_SELF_TEST,
343 AK09912_REG_CNTL_MODE_FUSE_ROM},
344 .data_regs = {
345 AK09912_REG_HXL,
346 AK09912_REG_HYL,
347 AK09912_REG_HZL},
352 * Per-instance context data for the device.
354 struct ak8975_data {
355 struct i2c_client *client;
356 const struct ak_def *def;
357 struct mutex lock;
358 u8 asa[3];
359 long raw_to_gauss[3];
360 struct gpio_desc *eoc_gpiod;
361 int eoc_irq;
362 wait_queue_head_t data_ready_queue;
363 unsigned long flags;
364 u8 cntl_cache;
365 struct iio_mount_matrix orientation;
366 struct regulator *vdd;
367 struct regulator *vid;
370 /* Enable attached power regulator if any. */
371 static int ak8975_power_on(const struct ak8975_data *data)
373 int ret;
375 ret = regulator_enable(data->vdd);
376 if (ret) {
377 dev_warn(&data->client->dev,
378 "Failed to enable specified Vdd supply\n");
379 return ret;
381 ret = regulator_enable(data->vid);
382 if (ret) {
383 dev_warn(&data->client->dev,
384 "Failed to enable specified Vid supply\n");
385 return ret;
388 * According to the datasheet the power supply rise time i 200us
389 * and the minimum wait time before mode setting is 100us, in
390 * total 300 us. Add some margin and say minimum 500us here.
392 usleep_range(500, 1000);
393 return 0;
396 /* Disable attached power regulator if any. */
397 static void ak8975_power_off(const struct ak8975_data *data)
399 regulator_disable(data->vid);
400 regulator_disable(data->vdd);
404 * Return 0 if the i2c device is the one we expect.
405 * return a negative error number otherwise
407 static int ak8975_who_i_am(struct i2c_client *client,
408 enum asahi_compass_chipset type)
410 u8 wia_val[2];
411 int ret;
414 * Signature for each device:
415 * Device | WIA1 | WIA2
416 * AK09912 | DEVICE_ID | AK09912_DEVICE_ID
417 * AK09911 | DEVICE_ID | AK09911_DEVICE_ID
418 * AK8975 | DEVICE_ID | NA
419 * AK8963 | DEVICE_ID | NA
421 ret = i2c_smbus_read_i2c_block_data_or_emulated(
422 client, AK09912_REG_WIA1, 2, wia_val);
423 if (ret < 0) {
424 dev_err(&client->dev, "Error reading WIA\n");
425 return ret;
428 if (wia_val[0] != AK8975_DEVICE_ID)
429 return -ENODEV;
431 switch (type) {
432 case AK8975:
433 case AK8963:
434 return 0;
435 case AK09911:
436 if (wia_val[1] == AK09911_DEVICE_ID)
437 return 0;
438 break;
439 case AK09912:
440 if (wia_val[1] == AK09912_DEVICE_ID)
441 return 0;
442 break;
443 default:
444 dev_err(&client->dev, "Type %d unknown\n", type);
446 return -ENODEV;
450 * Helper function to write to CNTL register.
452 static int ak8975_set_mode(struct ak8975_data *data, enum ak_ctrl_mode mode)
454 u8 regval;
455 int ret;
457 regval = (data->cntl_cache & ~data->def->ctrl_masks[CNTL_MODE]) |
458 data->def->ctrl_modes[mode];
459 ret = i2c_smbus_write_byte_data(data->client,
460 data->def->ctrl_regs[CNTL], regval);
461 if (ret < 0) {
462 return ret;
464 data->cntl_cache = regval;
465 /* After mode change wait atleast 100us */
466 usleep_range(100, 500);
468 return 0;
472 * Handle data ready irq
474 static irqreturn_t ak8975_irq_handler(int irq, void *data)
476 struct ak8975_data *ak8975 = data;
478 set_bit(0, &ak8975->flags);
479 wake_up(&ak8975->data_ready_queue);
481 return IRQ_HANDLED;
485 * Install data ready interrupt handler
487 static int ak8975_setup_irq(struct ak8975_data *data)
489 struct i2c_client *client = data->client;
490 int rc;
491 int irq;
493 init_waitqueue_head(&data->data_ready_queue);
494 clear_bit(0, &data->flags);
495 if (client->irq)
496 irq = client->irq;
497 else
498 irq = gpiod_to_irq(data->eoc_gpiod);
500 rc = devm_request_irq(&client->dev, irq, ak8975_irq_handler,
501 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
502 dev_name(&client->dev), data);
503 if (rc < 0) {
504 dev_err(&client->dev, "irq %d request failed: %d\n", irq, rc);
505 return rc;
508 data->eoc_irq = irq;
510 return rc;
515 * Perform some start-of-day setup, including reading the asa calibration
516 * values and caching them.
518 static int ak8975_setup(struct i2c_client *client)
520 struct iio_dev *indio_dev = i2c_get_clientdata(client);
521 struct ak8975_data *data = iio_priv(indio_dev);
522 int ret;
524 /* Write the fused rom access mode. */
525 ret = ak8975_set_mode(data, FUSE_ROM);
526 if (ret < 0) {
527 dev_err(&client->dev, "Error in setting fuse access mode\n");
528 return ret;
531 /* Get asa data and store in the device data. */
532 ret = i2c_smbus_read_i2c_block_data_or_emulated(
533 client, data->def->ctrl_regs[ASA_BASE],
534 3, data->asa);
535 if (ret < 0) {
536 dev_err(&client->dev, "Not able to read asa data\n");
537 return ret;
540 /* After reading fuse ROM data set power-down mode */
541 ret = ak8975_set_mode(data, POWER_DOWN);
542 if (ret < 0) {
543 dev_err(&client->dev, "Error in setting power-down mode\n");
544 return ret;
547 if (data->eoc_gpiod || client->irq > 0) {
548 ret = ak8975_setup_irq(data);
549 if (ret < 0) {
550 dev_err(&client->dev,
551 "Error setting data ready interrupt\n");
552 return ret;
556 data->raw_to_gauss[0] = data->def->raw_to_gauss(data->asa[0]);
557 data->raw_to_gauss[1] = data->def->raw_to_gauss(data->asa[1]);
558 data->raw_to_gauss[2] = data->def->raw_to_gauss(data->asa[2]);
560 return 0;
563 static int wait_conversion_complete_gpio(struct ak8975_data *data)
565 struct i2c_client *client = data->client;
566 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
567 int ret;
569 /* Wait for the conversion to complete. */
570 while (timeout_ms) {
571 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
572 if (gpiod_get_value(data->eoc_gpiod))
573 break;
574 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
576 if (!timeout_ms) {
577 dev_err(&client->dev, "Conversion timeout happened\n");
578 return -EINVAL;
581 ret = i2c_smbus_read_byte_data(client, data->def->ctrl_regs[ST1]);
582 if (ret < 0)
583 dev_err(&client->dev, "Error in reading ST1\n");
585 return ret;
588 static int wait_conversion_complete_polled(struct ak8975_data *data)
590 struct i2c_client *client = data->client;
591 u8 read_status;
592 u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
593 int ret;
595 /* Wait for the conversion to complete. */
596 while (timeout_ms) {
597 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
598 ret = i2c_smbus_read_byte_data(client,
599 data->def->ctrl_regs[ST1]);
600 if (ret < 0) {
601 dev_err(&client->dev, "Error in reading ST1\n");
602 return ret;
604 read_status = ret;
605 if (read_status)
606 break;
607 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
609 if (!timeout_ms) {
610 dev_err(&client->dev, "Conversion timeout happened\n");
611 return -EINVAL;
614 return read_status;
617 /* Returns 0 if the end of conversion interrupt occured or -ETIME otherwise */
618 static int wait_conversion_complete_interrupt(struct ak8975_data *data)
620 int ret;
622 ret = wait_event_timeout(data->data_ready_queue,
623 test_bit(0, &data->flags),
624 AK8975_DATA_READY_TIMEOUT);
625 clear_bit(0, &data->flags);
627 return ret > 0 ? 0 : -ETIME;
630 static int ak8975_start_read_axis(struct ak8975_data *data,
631 const struct i2c_client *client)
633 /* Set up the device for taking a sample. */
634 int ret = ak8975_set_mode(data, MODE_ONCE);
636 if (ret < 0) {
637 dev_err(&client->dev, "Error in setting operating mode\n");
638 return ret;
641 /* Wait for the conversion to complete. */
642 if (data->eoc_irq)
643 ret = wait_conversion_complete_interrupt(data);
644 else if (data->eoc_gpiod)
645 ret = wait_conversion_complete_gpio(data);
646 else
647 ret = wait_conversion_complete_polled(data);
648 if (ret < 0)
649 return ret;
651 /* This will be executed only for non-interrupt based waiting case */
652 if (ret & data->def->ctrl_masks[ST1_DRDY]) {
653 ret = i2c_smbus_read_byte_data(client,
654 data->def->ctrl_regs[ST2]);
655 if (ret < 0) {
656 dev_err(&client->dev, "Error in reading ST2\n");
657 return ret;
659 if (ret & (data->def->ctrl_masks[ST2_DERR] |
660 data->def->ctrl_masks[ST2_HOFL])) {
661 dev_err(&client->dev, "ST2 status error 0x%x\n", ret);
662 return -EINVAL;
666 return 0;
669 /* Retrieve raw flux value for one of the x, y, or z axis. */
670 static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
672 struct ak8975_data *data = iio_priv(indio_dev);
673 const struct i2c_client *client = data->client;
674 const struct ak_def *def = data->def;
675 __le16 rval;
676 u16 buff;
677 int ret;
679 pm_runtime_get_sync(&data->client->dev);
681 mutex_lock(&data->lock);
683 ret = ak8975_start_read_axis(data, client);
684 if (ret)
685 goto exit;
687 ret = i2c_smbus_read_i2c_block_data_or_emulated(
688 client, def->data_regs[index],
689 sizeof(rval), (u8*)&rval);
690 if (ret < 0)
691 goto exit;
693 mutex_unlock(&data->lock);
695 pm_runtime_mark_last_busy(&data->client->dev);
696 pm_runtime_put_autosuspend(&data->client->dev);
698 /* Swap bytes and convert to valid range. */
699 buff = le16_to_cpu(rval);
700 *val = clamp_t(s16, buff, -def->range, def->range);
701 return IIO_VAL_INT;
703 exit:
704 mutex_unlock(&data->lock);
705 dev_err(&client->dev, "Error in reading axis\n");
706 return ret;
709 static int ak8975_read_raw(struct iio_dev *indio_dev,
710 struct iio_chan_spec const *chan,
711 int *val, int *val2,
712 long mask)
714 struct ak8975_data *data = iio_priv(indio_dev);
716 switch (mask) {
717 case IIO_CHAN_INFO_RAW:
718 return ak8975_read_axis(indio_dev, chan->address, val);
719 case IIO_CHAN_INFO_SCALE:
720 *val = 0;
721 *val2 = data->raw_to_gauss[chan->address];
722 return IIO_VAL_INT_PLUS_MICRO;
724 return -EINVAL;
727 static const struct iio_mount_matrix *
728 ak8975_get_mount_matrix(const struct iio_dev *indio_dev,
729 const struct iio_chan_spec *chan)
731 struct ak8975_data *data = iio_priv(indio_dev);
733 return &data->orientation;
736 static const struct iio_chan_spec_ext_info ak8975_ext_info[] = {
737 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, ak8975_get_mount_matrix),
741 #define AK8975_CHANNEL(axis, index) \
743 .type = IIO_MAGN, \
744 .modified = 1, \
745 .channel2 = IIO_MOD_##axis, \
746 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
747 BIT(IIO_CHAN_INFO_SCALE), \
748 .address = index, \
749 .scan_index = index, \
750 .scan_type = { \
751 .sign = 's', \
752 .realbits = 16, \
753 .storagebits = 16, \
754 .endianness = IIO_CPU \
755 }, \
756 .ext_info = ak8975_ext_info, \
759 static const struct iio_chan_spec ak8975_channels[] = {
760 AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2),
761 IIO_CHAN_SOFT_TIMESTAMP(3),
764 static const unsigned long ak8975_scan_masks[] = { 0x7, 0 };
766 static const struct iio_info ak8975_info = {
767 .read_raw = &ak8975_read_raw,
770 #ifdef CONFIG_ACPI
771 static const struct acpi_device_id ak_acpi_match[] = {
772 {"AK8975", AK8975},
773 {"AK8963", AK8963},
774 {"INVN6500", AK8963},
775 {"AK009911", AK09911},
776 {"AK09911", AK09911},
777 {"AKM9911", AK09911},
778 {"AK09912", AK09912},
781 MODULE_DEVICE_TABLE(acpi, ak_acpi_match);
782 #endif
784 static void ak8975_fill_buffer(struct iio_dev *indio_dev)
786 struct ak8975_data *data = iio_priv(indio_dev);
787 const struct i2c_client *client = data->client;
788 const struct ak_def *def = data->def;
789 int ret;
790 s16 buff[8]; /* 3 x 16 bits axis values + 1 aligned 64 bits timestamp */
791 __le16 fval[3];
793 mutex_lock(&data->lock);
795 ret = ak8975_start_read_axis(data, client);
796 if (ret)
797 goto unlock;
800 * For each axis, read the flux value from the appropriate register
801 * (the register is specified in the iio device attributes).
803 ret = i2c_smbus_read_i2c_block_data_or_emulated(client,
804 def->data_regs[0],
805 3 * sizeof(fval[0]),
806 (u8 *)fval);
807 if (ret < 0)
808 goto unlock;
810 mutex_unlock(&data->lock);
812 /* Clamp to valid range. */
813 buff[0] = clamp_t(s16, le16_to_cpu(fval[0]), -def->range, def->range);
814 buff[1] = clamp_t(s16, le16_to_cpu(fval[1]), -def->range, def->range);
815 buff[2] = clamp_t(s16, le16_to_cpu(fval[2]), -def->range, def->range);
817 iio_push_to_buffers_with_timestamp(indio_dev, buff,
818 iio_get_time_ns(indio_dev));
819 return;
821 unlock:
822 mutex_unlock(&data->lock);
823 dev_err(&client->dev, "Error in reading axes block\n");
826 static irqreturn_t ak8975_handle_trigger(int irq, void *p)
828 const struct iio_poll_func *pf = p;
829 struct iio_dev *indio_dev = pf->indio_dev;
831 ak8975_fill_buffer(indio_dev);
832 iio_trigger_notify_done(indio_dev->trig);
833 return IRQ_HANDLED;
836 static int ak8975_probe(struct i2c_client *client,
837 const struct i2c_device_id *id)
839 struct ak8975_data *data;
840 struct iio_dev *indio_dev;
841 struct gpio_desc *eoc_gpiod;
842 const void *match;
843 unsigned int i;
844 int err;
845 enum asahi_compass_chipset chipset;
846 const char *name = NULL;
849 * Grab and set up the supplied GPIO.
850 * We may not have a GPIO based IRQ to scan, that is fine, we will
851 * poll if so.
853 eoc_gpiod = devm_gpiod_get_optional(&client->dev, NULL, GPIOD_IN);
854 if (IS_ERR(eoc_gpiod))
855 return PTR_ERR(eoc_gpiod);
856 if (eoc_gpiod)
857 gpiod_set_consumer_name(eoc_gpiod, "ak_8975");
859 /* Register with IIO */
860 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
861 if (indio_dev == NULL)
862 return -ENOMEM;
864 data = iio_priv(indio_dev);
865 i2c_set_clientdata(client, indio_dev);
867 data->client = client;
868 data->eoc_gpiod = eoc_gpiod;
869 data->eoc_irq = 0;
871 err = iio_read_mount_matrix(&client->dev, "mount-matrix", &data->orientation);
872 if (err)
873 return err;
875 /* id will be NULL when enumerated via ACPI */
876 match = device_get_match_data(&client->dev);
877 if (match) {
878 chipset = (enum asahi_compass_chipset)(match);
879 name = dev_name(&client->dev);
880 } else if (id) {
881 chipset = (enum asahi_compass_chipset)(id->driver_data);
882 name = id->name;
883 } else
884 return -ENOSYS;
886 for (i = 0; i < ARRAY_SIZE(ak_def_array); i++)
887 if (ak_def_array[i].type == chipset)
888 break;
890 if (i == ARRAY_SIZE(ak_def_array)) {
891 dev_err(&client->dev, "AKM device type unsupported: %d\n",
892 chipset);
893 return -ENODEV;
896 data->def = &ak_def_array[i];
898 /* Fetch the regulators */
899 data->vdd = devm_regulator_get(&client->dev, "vdd");
900 if (IS_ERR(data->vdd))
901 return PTR_ERR(data->vdd);
902 data->vid = devm_regulator_get(&client->dev, "vid");
903 if (IS_ERR(data->vid))
904 return PTR_ERR(data->vid);
906 err = ak8975_power_on(data);
907 if (err)
908 return err;
910 err = ak8975_who_i_am(client, data->def->type);
911 if (err < 0) {
912 dev_err(&client->dev, "Unexpected device\n");
913 goto power_off;
915 dev_dbg(&client->dev, "Asahi compass chip %s\n", name);
917 /* Perform some basic start-of-day setup of the device. */
918 err = ak8975_setup(client);
919 if (err < 0) {
920 dev_err(&client->dev, "%s initialization fails\n", name);
921 goto power_off;
924 mutex_init(&data->lock);
925 indio_dev->dev.parent = &client->dev;
926 indio_dev->channels = ak8975_channels;
927 indio_dev->num_channels = ARRAY_SIZE(ak8975_channels);
928 indio_dev->info = &ak8975_info;
929 indio_dev->available_scan_masks = ak8975_scan_masks;
930 indio_dev->modes = INDIO_DIRECT_MODE;
931 indio_dev->name = name;
933 err = iio_triggered_buffer_setup(indio_dev, NULL, ak8975_handle_trigger,
934 NULL);
935 if (err) {
936 dev_err(&client->dev, "triggered buffer setup failed\n");
937 goto power_off;
940 err = iio_device_register(indio_dev);
941 if (err) {
942 dev_err(&client->dev, "device register failed\n");
943 goto cleanup_buffer;
946 /* Enable runtime PM */
947 pm_runtime_get_noresume(&client->dev);
948 pm_runtime_set_active(&client->dev);
949 pm_runtime_enable(&client->dev);
951 * The device comes online in 500us, so add two orders of magnitude
952 * of delay before autosuspending: 50 ms.
954 pm_runtime_set_autosuspend_delay(&client->dev, 50);
955 pm_runtime_use_autosuspend(&client->dev);
956 pm_runtime_put(&client->dev);
958 return 0;
960 cleanup_buffer:
961 iio_triggered_buffer_cleanup(indio_dev);
962 power_off:
963 ak8975_power_off(data);
964 return err;
967 static int ak8975_remove(struct i2c_client *client)
969 struct iio_dev *indio_dev = i2c_get_clientdata(client);
970 struct ak8975_data *data = iio_priv(indio_dev);
972 pm_runtime_get_sync(&client->dev);
973 pm_runtime_put_noidle(&client->dev);
974 pm_runtime_disable(&client->dev);
975 iio_device_unregister(indio_dev);
976 iio_triggered_buffer_cleanup(indio_dev);
977 ak8975_set_mode(data, POWER_DOWN);
978 ak8975_power_off(data);
980 return 0;
983 #ifdef CONFIG_PM
984 static int ak8975_runtime_suspend(struct device *dev)
986 struct i2c_client *client = to_i2c_client(dev);
987 struct iio_dev *indio_dev = i2c_get_clientdata(client);
988 struct ak8975_data *data = iio_priv(indio_dev);
989 int ret;
991 /* Set the device in power down if it wasn't already */
992 ret = ak8975_set_mode(data, POWER_DOWN);
993 if (ret < 0) {
994 dev_err(&client->dev, "Error in setting power-down mode\n");
995 return ret;
997 /* Next cut the regulators */
998 ak8975_power_off(data);
1000 return 0;
1003 static int ak8975_runtime_resume(struct device *dev)
1005 struct i2c_client *client = to_i2c_client(dev);
1006 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1007 struct ak8975_data *data = iio_priv(indio_dev);
1008 int ret;
1010 /* Take up the regulators */
1011 ak8975_power_on(data);
1013 * We come up in powered down mode, the reading routines will
1014 * put us in the mode to read values later.
1016 ret = ak8975_set_mode(data, POWER_DOWN);
1017 if (ret < 0) {
1018 dev_err(&client->dev, "Error in setting power-down mode\n");
1019 return ret;
1022 return 0;
1024 #endif /* CONFIG_PM */
1026 static const struct dev_pm_ops ak8975_dev_pm_ops = {
1027 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1028 pm_runtime_force_resume)
1029 SET_RUNTIME_PM_OPS(ak8975_runtime_suspend,
1030 ak8975_runtime_resume, NULL)
1033 static const struct i2c_device_id ak8975_id[] = {
1034 {"ak8975", AK8975},
1035 {"ak8963", AK8963},
1036 {"AK8963", AK8963},
1037 {"ak09911", AK09911},
1038 {"ak09912", AK09912},
1042 MODULE_DEVICE_TABLE(i2c, ak8975_id);
1044 static const struct of_device_id ak8975_of_match[] = {
1045 { .compatible = "asahi-kasei,ak8975", },
1046 { .compatible = "ak8975", },
1047 { .compatible = "asahi-kasei,ak8963", },
1048 { .compatible = "ak8963", },
1049 { .compatible = "asahi-kasei,ak09911", },
1050 { .compatible = "ak09911", },
1051 { .compatible = "asahi-kasei,ak09912", },
1052 { .compatible = "ak09912", },
1055 MODULE_DEVICE_TABLE(of, ak8975_of_match);
1057 static struct i2c_driver ak8975_driver = {
1058 .driver = {
1059 .name = "ak8975",
1060 .pm = &ak8975_dev_pm_ops,
1061 .of_match_table = of_match_ptr(ak8975_of_match),
1062 .acpi_match_table = ACPI_PTR(ak_acpi_match),
1064 .probe = ak8975_probe,
1065 .remove = ak8975_remove,
1066 .id_table = ak8975_id,
1068 module_i2c_driver(ak8975_driver);
1070 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
1071 MODULE_DESCRIPTION("AK8975 magnetometer driver");
1072 MODULE_LICENSE("GPL");