1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * A sensor driver for the magnetometer AK8975.
5 * Magnetic compass sensor driver for monitoring magnetic flux information.
7 * Copyright (c) 2010, NVIDIA Corporation.
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.h>
20 #include <linux/of_gpio.h>
21 #include <linux/acpi.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/pm_runtime.h>
25 #include <linux/iio/iio.h>
26 #include <linux/iio/sysfs.h>
27 #include <linux/iio/buffer.h>
28 #include <linux/iio/trigger.h>
29 #include <linux/iio/trigger_consumer.h>
30 #include <linux/iio/triggered_buffer.h>
32 #include <linux/iio/magnetometer/ak8975.h>
35 * Register definitions, as well as various shifts and masks to get at the
36 * individual fields of the registers.
38 #define AK8975_REG_WIA 0x00
39 #define AK8975_DEVICE_ID 0x48
41 #define AK8975_REG_INFO 0x01
43 #define AK8975_REG_ST1 0x02
44 #define AK8975_REG_ST1_DRDY_SHIFT 0
45 #define AK8975_REG_ST1_DRDY_MASK (1 << AK8975_REG_ST1_DRDY_SHIFT)
47 #define AK8975_REG_HXL 0x03
48 #define AK8975_REG_HXH 0x04
49 #define AK8975_REG_HYL 0x05
50 #define AK8975_REG_HYH 0x06
51 #define AK8975_REG_HZL 0x07
52 #define AK8975_REG_HZH 0x08
53 #define AK8975_REG_ST2 0x09
54 #define AK8975_REG_ST2_DERR_SHIFT 2
55 #define AK8975_REG_ST2_DERR_MASK (1 << AK8975_REG_ST2_DERR_SHIFT)
57 #define AK8975_REG_ST2_HOFL_SHIFT 3
58 #define AK8975_REG_ST2_HOFL_MASK (1 << AK8975_REG_ST2_HOFL_SHIFT)
60 #define AK8975_REG_CNTL 0x0A
61 #define AK8975_REG_CNTL_MODE_SHIFT 0
62 #define AK8975_REG_CNTL_MODE_MASK (0xF << AK8975_REG_CNTL_MODE_SHIFT)
63 #define AK8975_REG_CNTL_MODE_POWER_DOWN 0x00
64 #define AK8975_REG_CNTL_MODE_ONCE 0x01
65 #define AK8975_REG_CNTL_MODE_SELF_TEST 0x08
66 #define AK8975_REG_CNTL_MODE_FUSE_ROM 0x0F
68 #define AK8975_REG_RSVC 0x0B
69 #define AK8975_REG_ASTC 0x0C
70 #define AK8975_REG_TS1 0x0D
71 #define AK8975_REG_TS2 0x0E
72 #define AK8975_REG_I2CDIS 0x0F
73 #define AK8975_REG_ASAX 0x10
74 #define AK8975_REG_ASAY 0x11
75 #define AK8975_REG_ASAZ 0x12
77 #define AK8975_MAX_REGS AK8975_REG_ASAZ
80 * AK09912 Register definitions
82 #define AK09912_REG_WIA1 0x00
83 #define AK09912_REG_WIA2 0x01
84 #define AK09912_DEVICE_ID 0x04
85 #define AK09911_DEVICE_ID 0x05
87 #define AK09911_REG_INFO1 0x02
88 #define AK09911_REG_INFO2 0x03
90 #define AK09912_REG_ST1 0x10
92 #define AK09912_REG_ST1_DRDY_SHIFT 0
93 #define AK09912_REG_ST1_DRDY_MASK (1 << AK09912_REG_ST1_DRDY_SHIFT)
95 #define AK09912_REG_HXL 0x11
96 #define AK09912_REG_HXH 0x12
97 #define AK09912_REG_HYL 0x13
98 #define AK09912_REG_HYH 0x14
99 #define AK09912_REG_HZL 0x15
100 #define AK09912_REG_HZH 0x16
101 #define AK09912_REG_TMPS 0x17
103 #define AK09912_REG_ST2 0x18
104 #define AK09912_REG_ST2_HOFL_SHIFT 3
105 #define AK09912_REG_ST2_HOFL_MASK (1 << AK09912_REG_ST2_HOFL_SHIFT)
107 #define AK09912_REG_CNTL1 0x30
109 #define AK09912_REG_CNTL2 0x31
110 #define AK09912_REG_CNTL_MODE_POWER_DOWN 0x00
111 #define AK09912_REG_CNTL_MODE_ONCE 0x01
112 #define AK09912_REG_CNTL_MODE_SELF_TEST 0x10
113 #define AK09912_REG_CNTL_MODE_FUSE_ROM 0x1F
114 #define AK09912_REG_CNTL2_MODE_SHIFT 0
115 #define AK09912_REG_CNTL2_MODE_MASK (0x1F << AK09912_REG_CNTL2_MODE_SHIFT)
117 #define AK09912_REG_CNTL3 0x32
119 #define AK09912_REG_TS1 0x33
120 #define AK09912_REG_TS2 0x34
121 #define AK09912_REG_TS3 0x35
122 #define AK09912_REG_I2CDIS 0x36
123 #define AK09912_REG_TS4 0x37
125 #define AK09912_REG_ASAX 0x60
126 #define AK09912_REG_ASAY 0x61
127 #define AK09912_REG_ASAZ 0x62
129 #define AK09912_MAX_REGS AK09912_REG_ASAZ
132 * Miscellaneous values.
134 #define AK8975_MAX_CONVERSION_TIMEOUT 500
135 #define AK8975_CONVERSION_DONE_POLL_TIME 10
136 #define AK8975_DATA_READY_TIMEOUT ((100*HZ)/1000)
139 * Precalculate scale factor (in Gauss units) for each axis and
140 * store in the device data.
142 * This scale factor is axis-dependent, and is derived from 3 calibration
143 * factors ASA(x), ASA(y), and ASA(z).
145 * These ASA values are read from the sensor device at start of day, and
146 * cached in the device context struct.
148 * Adjusting the flux value with the sensitivity adjustment value should be
149 * done via the following formula:
151 * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
152 * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
153 * is the resultant adjusted value.
155 * We reduce the formula to:
157 * Hadj = H * (ASA + 128) / 256
159 * H is in the range of -4096 to 4095. The magnetometer has a range of
160 * +-1229uT. To go from the raw value to uT is:
162 * HuT = H * 1229/4096, or roughly, 3/10.
164 * Since 1uT = 0.01 gauss, our final scale factor becomes:
166 * Hadj = H * ((ASA + 128) / 256) * 3/10 * 1/100
167 * Hadj = H * ((ASA + 128) * 0.003) / 256
169 * Since ASA doesn't change, we cache the resultant scale factor into the
170 * device context in ak8975_setup().
172 * Given we use IIO_VAL_INT_PLUS_MICRO bit when displaying the scale, we
173 * multiply the stored scale value by 1e6.
175 static long ak8975_raw_to_gauss(u16 data
)
177 return (((long)data
+ 128) * 3000) / 256;
181 * For AK8963 and AK09911, same calculation, but the device is less sensitive:
183 * H is in the range of +-8190. The magnetometer has a range of
184 * +-4912uT. To go from the raw value to uT is:
186 * HuT = H * 4912/8190, or roughly, 6/10, instead of 3/10.
189 static long ak8963_09911_raw_to_gauss(u16 data
)
191 return (((long)data
+ 128) * 6000) / 256;
195 * For AK09912, same calculation, except the device is more sensitive:
197 * H is in the range of -32752 to 32752. The magnetometer has a range of
198 * +-4912uT. To go from the raw value to uT is:
200 * HuT = H * 4912/32752, or roughly, 3/20, instead of 3/10.
202 static long ak09912_raw_to_gauss(u16 data
)
204 return (((long)data
+ 128) * 1500) / 256;
207 /* Compatible Asahi Kasei Compass parts */
208 enum asahi_compass_chipset
{
216 enum ak_ctrl_reg_addr
{
225 enum ak_ctrl_reg_mask
{
242 enum asahi_compass_chipset type
;
243 long (*raw_to_gauss
)(u16 data
);
245 u8 ctrl_regs
[REGS_END
];
246 u8 ctrl_masks
[MASK_END
];
247 u8 ctrl_modes
[MODE_END
];
251 static const struct ak_def ak_def_array
[AK_MAX_TYPE
] = {
254 .raw_to_gauss
= ak8975_raw_to_gauss
,
263 AK8975_REG_ST1_DRDY_MASK
,
264 AK8975_REG_ST2_HOFL_MASK
,
265 AK8975_REG_ST2_DERR_MASK
,
266 AK8975_REG_CNTL_MODE_MASK
},
268 AK8975_REG_CNTL_MODE_POWER_DOWN
,
269 AK8975_REG_CNTL_MODE_ONCE
,
270 AK8975_REG_CNTL_MODE_SELF_TEST
,
271 AK8975_REG_CNTL_MODE_FUSE_ROM
},
279 .raw_to_gauss
= ak8963_09911_raw_to_gauss
,
288 AK8975_REG_ST1_DRDY_MASK
,
289 AK8975_REG_ST2_HOFL_MASK
,
291 AK8975_REG_CNTL_MODE_MASK
},
293 AK8975_REG_CNTL_MODE_POWER_DOWN
,
294 AK8975_REG_CNTL_MODE_ONCE
,
295 AK8975_REG_CNTL_MODE_SELF_TEST
,
296 AK8975_REG_CNTL_MODE_FUSE_ROM
},
304 .raw_to_gauss
= ak8963_09911_raw_to_gauss
,
313 AK09912_REG_ST1_DRDY_MASK
,
314 AK09912_REG_ST2_HOFL_MASK
,
316 AK09912_REG_CNTL2_MODE_MASK
},
318 AK09912_REG_CNTL_MODE_POWER_DOWN
,
319 AK09912_REG_CNTL_MODE_ONCE
,
320 AK09912_REG_CNTL_MODE_SELF_TEST
,
321 AK09912_REG_CNTL_MODE_FUSE_ROM
},
329 .raw_to_gauss
= ak09912_raw_to_gauss
,
338 AK09912_REG_ST1_DRDY_MASK
,
339 AK09912_REG_ST2_HOFL_MASK
,
341 AK09912_REG_CNTL2_MODE_MASK
},
343 AK09912_REG_CNTL_MODE_POWER_DOWN
,
344 AK09912_REG_CNTL_MODE_ONCE
,
345 AK09912_REG_CNTL_MODE_SELF_TEST
,
346 AK09912_REG_CNTL_MODE_FUSE_ROM
},
355 * Per-instance context data for the device.
358 struct i2c_client
*client
;
359 const struct ak_def
*def
;
362 long raw_to_gauss
[3];
365 wait_queue_head_t data_ready_queue
;
368 struct iio_mount_matrix orientation
;
369 struct regulator
*vdd
;
370 struct regulator
*vid
;
373 /* Enable attached power regulator if any. */
374 static int ak8975_power_on(const struct ak8975_data
*data
)
378 ret
= regulator_enable(data
->vdd
);
380 dev_warn(&data
->client
->dev
,
381 "Failed to enable specified Vdd supply\n");
384 ret
= regulator_enable(data
->vid
);
386 dev_warn(&data
->client
->dev
,
387 "Failed to enable specified Vid supply\n");
391 * According to the datasheet the power supply rise time i 200us
392 * and the minimum wait time before mode setting is 100us, in
393 * total 300 us. Add some margin and say minimum 500us here.
395 usleep_range(500, 1000);
399 /* Disable attached power regulator if any. */
400 static void ak8975_power_off(const struct ak8975_data
*data
)
402 regulator_disable(data
->vid
);
403 regulator_disable(data
->vdd
);
407 * Return 0 if the i2c device is the one we expect.
408 * return a negative error number otherwise
410 static int ak8975_who_i_am(struct i2c_client
*client
,
411 enum asahi_compass_chipset type
)
417 * Signature for each device:
418 * Device | WIA1 | WIA2
419 * AK09912 | DEVICE_ID | AK09912_DEVICE_ID
420 * AK09911 | DEVICE_ID | AK09911_DEVICE_ID
421 * AK8975 | DEVICE_ID | NA
422 * AK8963 | DEVICE_ID | NA
424 ret
= i2c_smbus_read_i2c_block_data_or_emulated(
425 client
, AK09912_REG_WIA1
, 2, wia_val
);
427 dev_err(&client
->dev
, "Error reading WIA\n");
431 if (wia_val
[0] != AK8975_DEVICE_ID
)
439 if (wia_val
[1] == AK09911_DEVICE_ID
)
443 if (wia_val
[1] == AK09912_DEVICE_ID
)
447 dev_err(&client
->dev
, "Type %d unknown\n", type
);
453 * Helper function to write to CNTL register.
455 static int ak8975_set_mode(struct ak8975_data
*data
, enum ak_ctrl_mode mode
)
460 regval
= (data
->cntl_cache
& ~data
->def
->ctrl_masks
[CNTL_MODE
]) |
461 data
->def
->ctrl_modes
[mode
];
462 ret
= i2c_smbus_write_byte_data(data
->client
,
463 data
->def
->ctrl_regs
[CNTL
], regval
);
467 data
->cntl_cache
= regval
;
468 /* After mode change wait atleast 100us */
469 usleep_range(100, 500);
475 * Handle data ready irq
477 static irqreturn_t
ak8975_irq_handler(int irq
, void *data
)
479 struct ak8975_data
*ak8975
= data
;
481 set_bit(0, &ak8975
->flags
);
482 wake_up(&ak8975
->data_ready_queue
);
488 * Install data ready interrupt handler
490 static int ak8975_setup_irq(struct ak8975_data
*data
)
492 struct i2c_client
*client
= data
->client
;
496 init_waitqueue_head(&data
->data_ready_queue
);
497 clear_bit(0, &data
->flags
);
501 irq
= gpio_to_irq(data
->eoc_gpio
);
503 rc
= devm_request_irq(&client
->dev
, irq
, ak8975_irq_handler
,
504 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
505 dev_name(&client
->dev
), data
);
507 dev_err(&client
->dev
,
508 "irq %d request failed, (gpio %d): %d\n",
509 irq
, data
->eoc_gpio
, rc
);
520 * Perform some start-of-day setup, including reading the asa calibration
521 * values and caching them.
523 static int ak8975_setup(struct i2c_client
*client
)
525 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
526 struct ak8975_data
*data
= iio_priv(indio_dev
);
529 /* Write the fused rom access mode. */
530 ret
= ak8975_set_mode(data
, FUSE_ROM
);
532 dev_err(&client
->dev
, "Error in setting fuse access mode\n");
536 /* Get asa data and store in the device data. */
537 ret
= i2c_smbus_read_i2c_block_data_or_emulated(
538 client
, data
->def
->ctrl_regs
[ASA_BASE
],
541 dev_err(&client
->dev
, "Not able to read asa data\n");
545 /* After reading fuse ROM data set power-down mode */
546 ret
= ak8975_set_mode(data
, POWER_DOWN
);
548 dev_err(&client
->dev
, "Error in setting power-down mode\n");
552 if (data
->eoc_gpio
> 0 || client
->irq
> 0) {
553 ret
= ak8975_setup_irq(data
);
555 dev_err(&client
->dev
,
556 "Error setting data ready interrupt\n");
561 data
->raw_to_gauss
[0] = data
->def
->raw_to_gauss(data
->asa
[0]);
562 data
->raw_to_gauss
[1] = data
->def
->raw_to_gauss(data
->asa
[1]);
563 data
->raw_to_gauss
[2] = data
->def
->raw_to_gauss(data
->asa
[2]);
568 static int wait_conversion_complete_gpio(struct ak8975_data
*data
)
570 struct i2c_client
*client
= data
->client
;
571 u32 timeout_ms
= AK8975_MAX_CONVERSION_TIMEOUT
;
574 /* Wait for the conversion to complete. */
576 msleep(AK8975_CONVERSION_DONE_POLL_TIME
);
577 if (gpio_get_value(data
->eoc_gpio
))
579 timeout_ms
-= AK8975_CONVERSION_DONE_POLL_TIME
;
582 dev_err(&client
->dev
, "Conversion timeout happened\n");
586 ret
= i2c_smbus_read_byte_data(client
, data
->def
->ctrl_regs
[ST1
]);
588 dev_err(&client
->dev
, "Error in reading ST1\n");
593 static int wait_conversion_complete_polled(struct ak8975_data
*data
)
595 struct i2c_client
*client
= data
->client
;
597 u32 timeout_ms
= AK8975_MAX_CONVERSION_TIMEOUT
;
600 /* Wait for the conversion to complete. */
602 msleep(AK8975_CONVERSION_DONE_POLL_TIME
);
603 ret
= i2c_smbus_read_byte_data(client
,
604 data
->def
->ctrl_regs
[ST1
]);
606 dev_err(&client
->dev
, "Error in reading ST1\n");
612 timeout_ms
-= AK8975_CONVERSION_DONE_POLL_TIME
;
615 dev_err(&client
->dev
, "Conversion timeout happened\n");
622 /* Returns 0 if the end of conversion interrupt occured or -ETIME otherwise */
623 static int wait_conversion_complete_interrupt(struct ak8975_data
*data
)
627 ret
= wait_event_timeout(data
->data_ready_queue
,
628 test_bit(0, &data
->flags
),
629 AK8975_DATA_READY_TIMEOUT
);
630 clear_bit(0, &data
->flags
);
632 return ret
> 0 ? 0 : -ETIME
;
635 static int ak8975_start_read_axis(struct ak8975_data
*data
,
636 const struct i2c_client
*client
)
638 /* Set up the device for taking a sample. */
639 int ret
= ak8975_set_mode(data
, MODE_ONCE
);
642 dev_err(&client
->dev
, "Error in setting operating mode\n");
646 /* Wait for the conversion to complete. */
648 ret
= wait_conversion_complete_interrupt(data
);
649 else if (gpio_is_valid(data
->eoc_gpio
))
650 ret
= wait_conversion_complete_gpio(data
);
652 ret
= wait_conversion_complete_polled(data
);
656 /* This will be executed only for non-interrupt based waiting case */
657 if (ret
& data
->def
->ctrl_masks
[ST1_DRDY
]) {
658 ret
= i2c_smbus_read_byte_data(client
,
659 data
->def
->ctrl_regs
[ST2
]);
661 dev_err(&client
->dev
, "Error in reading ST2\n");
664 if (ret
& (data
->def
->ctrl_masks
[ST2_DERR
] |
665 data
->def
->ctrl_masks
[ST2_HOFL
])) {
666 dev_err(&client
->dev
, "ST2 status error 0x%x\n", ret
);
674 /* Retrieve raw flux value for one of the x, y, or z axis. */
675 static int ak8975_read_axis(struct iio_dev
*indio_dev
, int index
, int *val
)
677 struct ak8975_data
*data
= iio_priv(indio_dev
);
678 const struct i2c_client
*client
= data
->client
;
679 const struct ak_def
*def
= data
->def
;
684 pm_runtime_get_sync(&data
->client
->dev
);
686 mutex_lock(&data
->lock
);
688 ret
= ak8975_start_read_axis(data
, client
);
692 ret
= i2c_smbus_read_i2c_block_data_or_emulated(
693 client
, def
->data_regs
[index
],
694 sizeof(rval
), (u8
*)&rval
);
698 mutex_unlock(&data
->lock
);
700 pm_runtime_mark_last_busy(&data
->client
->dev
);
701 pm_runtime_put_autosuspend(&data
->client
->dev
);
703 /* Swap bytes and convert to valid range. */
704 buff
= le16_to_cpu(rval
);
705 *val
= clamp_t(s16
, buff
, -def
->range
, def
->range
);
709 mutex_unlock(&data
->lock
);
710 dev_err(&client
->dev
, "Error in reading axis\n");
714 static int ak8975_read_raw(struct iio_dev
*indio_dev
,
715 struct iio_chan_spec
const *chan
,
719 struct ak8975_data
*data
= iio_priv(indio_dev
);
722 case IIO_CHAN_INFO_RAW
:
723 return ak8975_read_axis(indio_dev
, chan
->address
, val
);
724 case IIO_CHAN_INFO_SCALE
:
726 *val2
= data
->raw_to_gauss
[chan
->address
];
727 return IIO_VAL_INT_PLUS_MICRO
;
732 static const struct iio_mount_matrix
*
733 ak8975_get_mount_matrix(const struct iio_dev
*indio_dev
,
734 const struct iio_chan_spec
*chan
)
736 struct ak8975_data
*data
= iio_priv(indio_dev
);
738 return &data
->orientation
;
741 static const struct iio_chan_spec_ext_info ak8975_ext_info
[] = {
742 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR
, ak8975_get_mount_matrix
),
746 #define AK8975_CHANNEL(axis, index) \
750 .channel2 = IIO_MOD_##axis, \
751 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
752 BIT(IIO_CHAN_INFO_SCALE), \
754 .scan_index = index, \
759 .endianness = IIO_CPU \
761 .ext_info = ak8975_ext_info, \
764 static const struct iio_chan_spec ak8975_channels
[] = {
765 AK8975_CHANNEL(X
, 0), AK8975_CHANNEL(Y
, 1), AK8975_CHANNEL(Z
, 2),
766 IIO_CHAN_SOFT_TIMESTAMP(3),
769 static const unsigned long ak8975_scan_masks
[] = { 0x7, 0 };
771 static const struct iio_info ak8975_info
= {
772 .read_raw
= &ak8975_read_raw
,
776 static const struct acpi_device_id ak_acpi_match
[] = {
779 {"INVN6500", AK8963
},
780 {"AK009911", AK09911
},
781 {"AK09911", AK09911
},
782 {"AKM9911", AK09911
},
783 {"AK09912", AK09912
},
786 MODULE_DEVICE_TABLE(acpi
, ak_acpi_match
);
789 static const char *ak8975_match_acpi_device(struct device
*dev
,
790 enum asahi_compass_chipset
*chipset
)
792 const struct acpi_device_id
*id
;
794 id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
797 *chipset
= (int)id
->driver_data
;
799 return dev_name(dev
);
802 static void ak8975_fill_buffer(struct iio_dev
*indio_dev
)
804 struct ak8975_data
*data
= iio_priv(indio_dev
);
805 const struct i2c_client
*client
= data
->client
;
806 const struct ak_def
*def
= data
->def
;
808 s16 buff
[8]; /* 3 x 16 bits axis values + 1 aligned 64 bits timestamp */
811 mutex_lock(&data
->lock
);
813 ret
= ak8975_start_read_axis(data
, client
);
818 * For each axis, read the flux value from the appropriate register
819 * (the register is specified in the iio device attributes).
821 ret
= i2c_smbus_read_i2c_block_data_or_emulated(client
,
828 mutex_unlock(&data
->lock
);
830 /* Clamp to valid range. */
831 buff
[0] = clamp_t(s16
, le16_to_cpu(fval
[0]), -def
->range
, def
->range
);
832 buff
[1] = clamp_t(s16
, le16_to_cpu(fval
[1]), -def
->range
, def
->range
);
833 buff
[2] = clamp_t(s16
, le16_to_cpu(fval
[2]), -def
->range
, def
->range
);
835 iio_push_to_buffers_with_timestamp(indio_dev
, buff
,
836 iio_get_time_ns(indio_dev
));
840 mutex_unlock(&data
->lock
);
841 dev_err(&client
->dev
, "Error in reading axes block\n");
844 static irqreturn_t
ak8975_handle_trigger(int irq
, void *p
)
846 const struct iio_poll_func
*pf
= p
;
847 struct iio_dev
*indio_dev
= pf
->indio_dev
;
849 ak8975_fill_buffer(indio_dev
);
850 iio_trigger_notify_done(indio_dev
->trig
);
854 static int ak8975_probe(struct i2c_client
*client
,
855 const struct i2c_device_id
*id
)
857 struct ak8975_data
*data
;
858 struct iio_dev
*indio_dev
;
861 const char *name
= NULL
;
862 enum asahi_compass_chipset chipset
= AK_MAX_TYPE
;
863 const struct ak8975_platform_data
*pdata
=
864 dev_get_platdata(&client
->dev
);
866 /* Grab and set up the supplied GPIO. */
868 eoc_gpio
= pdata
->eoc_gpio
;
869 else if (client
->dev
.of_node
)
870 eoc_gpio
= of_get_gpio(client
->dev
.of_node
, 0);
874 if (eoc_gpio
== -EPROBE_DEFER
)
875 return -EPROBE_DEFER
;
877 /* We may not have a GPIO based IRQ to scan, that is fine, we will
879 if (gpio_is_valid(eoc_gpio
)) {
880 err
= devm_gpio_request_one(&client
->dev
, eoc_gpio
,
881 GPIOF_IN
, "ak_8975");
883 dev_err(&client
->dev
,
884 "failed to request GPIO %d, error %d\n",
890 /* Register with IIO */
891 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
892 if (indio_dev
== NULL
)
895 data
= iio_priv(indio_dev
);
896 i2c_set_clientdata(client
, indio_dev
);
898 data
->client
= client
;
899 data
->eoc_gpio
= eoc_gpio
;
903 err
= iio_read_mount_matrix(&client
->dev
, "mount-matrix",
908 data
->orientation
= pdata
->orientation
;
910 /* id will be NULL when enumerated via ACPI */
912 chipset
= (enum asahi_compass_chipset
)(id
->driver_data
);
914 } else if (ACPI_HANDLE(&client
->dev
)) {
915 name
= ak8975_match_acpi_device(&client
->dev
, &chipset
);
921 if (chipset
>= AK_MAX_TYPE
) {
922 dev_err(&client
->dev
, "AKM device type unsupported: %d\n",
927 data
->def
= &ak_def_array
[chipset
];
929 /* Fetch the regulators */
930 data
->vdd
= devm_regulator_get(&client
->dev
, "vdd");
931 if (IS_ERR(data
->vdd
))
932 return PTR_ERR(data
->vdd
);
933 data
->vid
= devm_regulator_get(&client
->dev
, "vid");
934 if (IS_ERR(data
->vid
))
935 return PTR_ERR(data
->vid
);
937 err
= ak8975_power_on(data
);
941 err
= ak8975_who_i_am(client
, data
->def
->type
);
943 dev_err(&client
->dev
, "Unexpected device\n");
946 dev_dbg(&client
->dev
, "Asahi compass chip %s\n", name
);
948 /* Perform some basic start-of-day setup of the device. */
949 err
= ak8975_setup(client
);
951 dev_err(&client
->dev
, "%s initialization fails\n", name
);
955 mutex_init(&data
->lock
);
956 indio_dev
->dev
.parent
= &client
->dev
;
957 indio_dev
->channels
= ak8975_channels
;
958 indio_dev
->num_channels
= ARRAY_SIZE(ak8975_channels
);
959 indio_dev
->info
= &ak8975_info
;
960 indio_dev
->available_scan_masks
= ak8975_scan_masks
;
961 indio_dev
->modes
= INDIO_DIRECT_MODE
;
962 indio_dev
->name
= name
;
964 err
= iio_triggered_buffer_setup(indio_dev
, NULL
, ak8975_handle_trigger
,
967 dev_err(&client
->dev
, "triggered buffer setup failed\n");
971 err
= iio_device_register(indio_dev
);
973 dev_err(&client
->dev
, "device register failed\n");
977 /* Enable runtime PM */
978 pm_runtime_get_noresume(&client
->dev
);
979 pm_runtime_set_active(&client
->dev
);
980 pm_runtime_enable(&client
->dev
);
982 * The device comes online in 500us, so add two orders of magnitude
983 * of delay before autosuspending: 50 ms.
985 pm_runtime_set_autosuspend_delay(&client
->dev
, 50);
986 pm_runtime_use_autosuspend(&client
->dev
);
987 pm_runtime_put(&client
->dev
);
992 iio_triggered_buffer_cleanup(indio_dev
);
994 ak8975_power_off(data
);
998 static int ak8975_remove(struct i2c_client
*client
)
1000 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
1001 struct ak8975_data
*data
= iio_priv(indio_dev
);
1003 pm_runtime_get_sync(&client
->dev
);
1004 pm_runtime_put_noidle(&client
->dev
);
1005 pm_runtime_disable(&client
->dev
);
1006 iio_device_unregister(indio_dev
);
1007 iio_triggered_buffer_cleanup(indio_dev
);
1008 ak8975_set_mode(data
, POWER_DOWN
);
1009 ak8975_power_off(data
);
1015 static int ak8975_runtime_suspend(struct device
*dev
)
1017 struct i2c_client
*client
= to_i2c_client(dev
);
1018 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
1019 struct ak8975_data
*data
= iio_priv(indio_dev
);
1022 /* Set the device in power down if it wasn't already */
1023 ret
= ak8975_set_mode(data
, POWER_DOWN
);
1025 dev_err(&client
->dev
, "Error in setting power-down mode\n");
1028 /* Next cut the regulators */
1029 ak8975_power_off(data
);
1034 static int ak8975_runtime_resume(struct device
*dev
)
1036 struct i2c_client
*client
= to_i2c_client(dev
);
1037 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
1038 struct ak8975_data
*data
= iio_priv(indio_dev
);
1041 /* Take up the regulators */
1042 ak8975_power_on(data
);
1044 * We come up in powered down mode, the reading routines will
1045 * put us in the mode to read values later.
1047 ret
= ak8975_set_mode(data
, POWER_DOWN
);
1049 dev_err(&client
->dev
, "Error in setting power-down mode\n");
1055 #endif /* CONFIG_PM */
1057 static const struct dev_pm_ops ak8975_dev_pm_ops
= {
1058 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
1059 pm_runtime_force_resume
)
1060 SET_RUNTIME_PM_OPS(ak8975_runtime_suspend
,
1061 ak8975_runtime_resume
, NULL
)
1064 static const struct i2c_device_id ak8975_id
[] = {
1068 {"ak09911", AK09911
},
1069 {"ak09912", AK09912
},
1073 MODULE_DEVICE_TABLE(i2c
, ak8975_id
);
1075 static const struct of_device_id ak8975_of_match
[] = {
1076 { .compatible
= "asahi-kasei,ak8975", },
1077 { .compatible
= "ak8975", },
1078 { .compatible
= "asahi-kasei,ak8963", },
1079 { .compatible
= "ak8963", },
1080 { .compatible
= "asahi-kasei,ak09911", },
1081 { .compatible
= "ak09911", },
1082 { .compatible
= "asahi-kasei,ak09912", },
1083 { .compatible
= "ak09912", },
1086 MODULE_DEVICE_TABLE(of
, ak8975_of_match
);
1088 static struct i2c_driver ak8975_driver
= {
1091 .pm
= &ak8975_dev_pm_ops
,
1092 .of_match_table
= of_match_ptr(ak8975_of_match
),
1093 .acpi_match_table
= ACPI_PTR(ak_acpi_match
),
1095 .probe
= ak8975_probe
,
1096 .remove
= ak8975_remove
,
1097 .id_table
= ak8975_id
,
1099 module_i2c_driver(ak8975_driver
);
1101 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
1102 MODULE_DESCRIPTION("AK8975 magnetometer driver");
1103 MODULE_LICENSE("GPL");