1 // SPDX-License-Identifier: GPL-2.0-only
2 /* ADC driver for AXP20X and AXP22X PMICs
4 * Copyright (c) 2016 Free Electrons NextThing Co.
5 * Quentin Schulz <quentin.schulz@free-electrons.com>
8 #include <linux/unaligned.h>
9 #include <linux/bitfield.h>
10 #include <linux/completion.h>
11 #include <linux/interrupt.h>
13 #include <linux/module.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/property.h>
18 #include <linux/regmap.h>
19 #include <linux/thermal.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/driver.h>
23 #include <linux/iio/machine.h>
24 #include <linux/mfd/axp20x.h>
26 #define AXP192_ADC_EN1_MASK GENMASK(7, 0)
27 #define AXP192_ADC_EN2_MASK (GENMASK(3, 0) | BIT(7))
29 #define AXP20X_ADC_EN1_MASK GENMASK(7, 0)
30 #define AXP20X_ADC_EN2_MASK (GENMASK(3, 2) | BIT(7))
32 #define AXP22X_ADC_EN1_MASK (GENMASK(7, 5) | BIT(0))
34 #define AXP717_ADC_EN1_MASK GENMASK(7, 0)
36 #define AXP192_GPIO30_IN_RANGE_GPIO0 BIT(0)
37 #define AXP192_GPIO30_IN_RANGE_GPIO1 BIT(1)
38 #define AXP192_GPIO30_IN_RANGE_GPIO2 BIT(2)
39 #define AXP192_GPIO30_IN_RANGE_GPIO3 BIT(3)
41 #define AXP20X_GPIO10_IN_RANGE_GPIO0 BIT(0)
42 #define AXP20X_GPIO10_IN_RANGE_GPIO1 BIT(1)
44 #define AXP20X_ADC_RATE_MASK GENMASK(7, 6)
45 #define AXP20X_ADC_RATE_HZ(x) ((ilog2((x) / 25) << 6) & AXP20X_ADC_RATE_MASK)
47 #define AXP22X_ADC_RATE_HZ(x) ((ilog2((x) / 100) << 6) & AXP20X_ADC_RATE_MASK)
49 #define AXP717_ADC_DATA_TS 0x00
50 #define AXP717_ADC_DATA_TEMP 0x01
51 #define AXP717_ADC_DATA_VMID 0x02
52 #define AXP717_ADC_DATA_BKUP_BATT 0x03
54 #define AXP717_ADC_DATA_MASK GENMASK(13, 0)
56 #define AXP813_V_I_ADC_RATE_MASK GENMASK(5, 4)
57 #define AXP813_ADC_RATE_MASK (AXP20X_ADC_RATE_MASK | AXP813_V_I_ADC_RATE_MASK)
58 #define AXP813_TS_GPIO0_ADC_RATE_HZ(x) AXP20X_ADC_RATE_HZ(x)
59 #define AXP813_V_I_ADC_RATE_HZ(x) ((ilog2((x) / 100) << 4) & AXP813_V_I_ADC_RATE_MASK)
60 #define AXP813_ADC_RATE_HZ(x) (AXP20X_ADC_RATE_HZ(x) | AXP813_V_I_ADC_RATE_HZ(x))
62 #define AXP20X_ADC_CHANNEL(_channel, _name, _type, _reg) \
66 .channel = _channel, \
68 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
69 BIT(IIO_CHAN_INFO_SCALE), \
70 .datasheet_name = _name, \
73 #define AXP20X_ADC_CHANNEL_OFFSET(_channel, _name, _type, _reg) \
77 .channel = _channel, \
79 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
80 BIT(IIO_CHAN_INFO_SCALE) |\
81 BIT(IIO_CHAN_INFO_OFFSET),\
82 .datasheet_name = _name, \
87 struct axp20x_adc_iio
{
88 struct regmap
*regmap
;
89 const struct axp_data
*data
;
92 enum axp192_adc_channel_v
{
104 enum axp192_adc_channel_i
{
108 AXP192_BATT_DISCHRG_I
,
111 enum axp20x_adc_channel_v
{
121 enum axp20x_adc_channel_i
{
125 AXP20X_BATT_DISCHRG_I
,
128 enum axp22x_adc_channel_v
{
133 enum axp22x_adc_channel_i
{
134 AXP22X_BATT_CHRG_I
= 1,
135 AXP22X_BATT_DISCHRG_I
,
138 enum axp717_adc_channel_v
{
148 enum axp717_adc_channel_i
{
149 AXP717_BATT_CHRG_I
= 5,
152 enum axp813_adc_channel_v
{
158 static struct iio_map axp20x_maps
[] = {
160 .consumer_dev_name
= "axp20x-usb-power-supply",
161 .consumer_channel
= "vbus_v",
162 .adc_channel_label
= "vbus_v",
164 .consumer_dev_name
= "axp20x-usb-power-supply",
165 .consumer_channel
= "vbus_i",
166 .adc_channel_label
= "vbus_i",
168 .consumer_dev_name
= "axp20x-ac-power-supply",
169 .consumer_channel
= "acin_v",
170 .adc_channel_label
= "acin_v",
172 .consumer_dev_name
= "axp20x-ac-power-supply",
173 .consumer_channel
= "acin_i",
174 .adc_channel_label
= "acin_i",
176 .consumer_dev_name
= "axp20x-battery-power-supply",
177 .consumer_channel
= "batt_v",
178 .adc_channel_label
= "batt_v",
180 .consumer_dev_name
= "axp20x-battery-power-supply",
181 .consumer_channel
= "batt_chrg_i",
182 .adc_channel_label
= "batt_chrg_i",
184 .consumer_dev_name
= "axp20x-battery-power-supply",
185 .consumer_channel
= "batt_dischrg_i",
186 .adc_channel_label
= "batt_dischrg_i",
187 }, { /* sentinel */ }
190 static struct iio_map axp22x_maps
[] = {
192 .consumer_dev_name
= "axp20x-battery-power-supply",
193 .consumer_channel
= "batt_v",
194 .adc_channel_label
= "batt_v",
196 .consumer_dev_name
= "axp20x-battery-power-supply",
197 .consumer_channel
= "batt_chrg_i",
198 .adc_channel_label
= "batt_chrg_i",
200 .consumer_dev_name
= "axp20x-battery-power-supply",
201 .consumer_channel
= "batt_dischrg_i",
202 .adc_channel_label
= "batt_dischrg_i",
203 }, { /* sentinel */ }
206 static struct iio_map axp717_maps
[] = {
208 .consumer_dev_name
= "axp20x-usb-power-supply",
209 .consumer_channel
= "vbus_v",
210 .adc_channel_label
= "vbus_v",
212 .consumer_dev_name
= "axp20x-battery-power-supply",
213 .consumer_channel
= "batt_v",
214 .adc_channel_label
= "batt_v",
216 .consumer_dev_name
= "axp20x-battery-power-supply",
217 .consumer_channel
= "batt_chrg_i",
218 .adc_channel_label
= "batt_chrg_i",
223 * Channels are mapped by physical system. Their channels share the same index.
224 * i.e. acin_i is in_current0_raw and acin_v is in_voltage0_raw.
225 * The only exception is for the battery. batt_v will be in_voltage6_raw and
226 * charge current in_current6_raw and discharge current will be in_current7_raw.
228 static const struct iio_chan_spec axp192_adc_channels
[] = {
229 AXP20X_ADC_CHANNEL(AXP192_ACIN_V
, "acin_v", IIO_VOLTAGE
,
230 AXP20X_ACIN_V_ADC_H
),
231 AXP20X_ADC_CHANNEL(AXP192_ACIN_I
, "acin_i", IIO_CURRENT
,
232 AXP20X_ACIN_I_ADC_H
),
233 AXP20X_ADC_CHANNEL(AXP192_VBUS_V
, "vbus_v", IIO_VOLTAGE
,
234 AXP20X_VBUS_V_ADC_H
),
235 AXP20X_ADC_CHANNEL(AXP192_VBUS_I
, "vbus_i", IIO_CURRENT
,
236 AXP20X_VBUS_I_ADC_H
),
239 .address
= AXP20X_TEMP_ADC_H
,
240 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
241 BIT(IIO_CHAN_INFO_SCALE
) |
242 BIT(IIO_CHAN_INFO_OFFSET
),
243 .datasheet_name
= "pmic_temp",
245 AXP20X_ADC_CHANNEL_OFFSET(AXP192_GPIO0_V
, "gpio0_v", IIO_VOLTAGE
,
246 AXP20X_GPIO0_V_ADC_H
),
247 AXP20X_ADC_CHANNEL_OFFSET(AXP192_GPIO1_V
, "gpio1_v", IIO_VOLTAGE
,
248 AXP20X_GPIO1_V_ADC_H
),
249 AXP20X_ADC_CHANNEL_OFFSET(AXP192_GPIO2_V
, "gpio2_v", IIO_VOLTAGE
,
250 AXP192_GPIO2_V_ADC_H
),
251 AXP20X_ADC_CHANNEL_OFFSET(AXP192_GPIO3_V
, "gpio3_v", IIO_VOLTAGE
,
252 AXP192_GPIO3_V_ADC_H
),
253 AXP20X_ADC_CHANNEL(AXP192_IPSOUT_V
, "ipsout_v", IIO_VOLTAGE
,
254 AXP20X_IPSOUT_V_HIGH_H
),
255 AXP20X_ADC_CHANNEL(AXP192_BATT_V
, "batt_v", IIO_VOLTAGE
,
257 AXP20X_ADC_CHANNEL(AXP192_BATT_CHRG_I
, "batt_chrg_i", IIO_CURRENT
,
258 AXP20X_BATT_CHRG_I_H
),
259 AXP20X_ADC_CHANNEL(AXP192_BATT_DISCHRG_I
, "batt_dischrg_i", IIO_CURRENT
,
260 AXP20X_BATT_DISCHRG_I_H
),
261 AXP20X_ADC_CHANNEL(AXP192_TS_IN
, "ts_v", IIO_VOLTAGE
,
265 static const struct iio_chan_spec axp20x_adc_channels
[] = {
266 AXP20X_ADC_CHANNEL(AXP20X_ACIN_V
, "acin_v", IIO_VOLTAGE
,
267 AXP20X_ACIN_V_ADC_H
),
268 AXP20X_ADC_CHANNEL(AXP20X_ACIN_I
, "acin_i", IIO_CURRENT
,
269 AXP20X_ACIN_I_ADC_H
),
270 AXP20X_ADC_CHANNEL(AXP20X_VBUS_V
, "vbus_v", IIO_VOLTAGE
,
271 AXP20X_VBUS_V_ADC_H
),
272 AXP20X_ADC_CHANNEL(AXP20X_VBUS_I
, "vbus_i", IIO_CURRENT
,
273 AXP20X_VBUS_I_ADC_H
),
276 .address
= AXP20X_TEMP_ADC_H
,
277 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
278 BIT(IIO_CHAN_INFO_SCALE
) |
279 BIT(IIO_CHAN_INFO_OFFSET
),
280 .datasheet_name
= "pmic_temp",
282 AXP20X_ADC_CHANNEL_OFFSET(AXP20X_GPIO0_V
, "gpio0_v", IIO_VOLTAGE
,
283 AXP20X_GPIO0_V_ADC_H
),
284 AXP20X_ADC_CHANNEL_OFFSET(AXP20X_GPIO1_V
, "gpio1_v", IIO_VOLTAGE
,
285 AXP20X_GPIO1_V_ADC_H
),
286 AXP20X_ADC_CHANNEL(AXP20X_IPSOUT_V
, "ipsout_v", IIO_VOLTAGE
,
287 AXP20X_IPSOUT_V_HIGH_H
),
288 AXP20X_ADC_CHANNEL(AXP20X_BATT_V
, "batt_v", IIO_VOLTAGE
,
290 AXP20X_ADC_CHANNEL(AXP20X_BATT_CHRG_I
, "batt_chrg_i", IIO_CURRENT
,
291 AXP20X_BATT_CHRG_I_H
),
292 AXP20X_ADC_CHANNEL(AXP20X_BATT_DISCHRG_I
, "batt_dischrg_i", IIO_CURRENT
,
293 AXP20X_BATT_DISCHRG_I_H
),
294 AXP20X_ADC_CHANNEL(AXP20X_TS_IN
, "ts_v", IIO_VOLTAGE
,
298 static const struct iio_chan_spec axp22x_adc_channels
[] = {
301 .address
= AXP22X_PMIC_TEMP_H
,
302 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
303 BIT(IIO_CHAN_INFO_SCALE
) |
304 BIT(IIO_CHAN_INFO_OFFSET
),
305 .datasheet_name
= "pmic_temp",
307 AXP20X_ADC_CHANNEL(AXP22X_BATT_V
, "batt_v", IIO_VOLTAGE
,
309 AXP20X_ADC_CHANNEL(AXP22X_BATT_CHRG_I
, "batt_chrg_i", IIO_CURRENT
,
310 AXP20X_BATT_CHRG_I_H
),
311 AXP20X_ADC_CHANNEL(AXP22X_BATT_DISCHRG_I
, "batt_dischrg_i", IIO_CURRENT
,
312 AXP20X_BATT_DISCHRG_I_H
),
313 AXP20X_ADC_CHANNEL(AXP22X_TS_IN
, "ts_v", IIO_VOLTAGE
,
318 * Scale and offset is unknown for temp, ts, batt_chrg_i, vmid_v, and
319 * bkup_batt_v channels. Leaving scale and offset undefined for now.
321 static const struct iio_chan_spec axp717_adc_channels
[] = {
322 AXP20X_ADC_CHANNEL(AXP717_BATT_V
, "batt_v", IIO_VOLTAGE
,
324 AXP20X_ADC_CHANNEL(AXP717_TS_IN
, "ts_v", IIO_VOLTAGE
,
326 AXP20X_ADC_CHANNEL(AXP717_VBUS_V
, "vbus_v", IIO_VOLTAGE
,
328 AXP20X_ADC_CHANNEL(AXP717_VSYS_V
, "vsys_v", IIO_VOLTAGE
,
330 AXP20X_ADC_CHANNEL(AXP717_DIE_TEMP_V
, "pmic_temp", IIO_TEMP
,
332 AXP20X_ADC_CHANNEL(AXP717_BATT_CHRG_I
, "batt_chrg_i", IIO_CURRENT
,
333 AXP717_BATT_CHRG_I_H
),
334 AXP20X_ADC_CHANNEL(AXP717_VMID_V
, "vmid_v", IIO_VOLTAGE
,
336 AXP20X_ADC_CHANNEL(AXP717_BKUP_BATT_V
, "bkup_batt_v", IIO_VOLTAGE
,
340 static const struct iio_chan_spec axp813_adc_channels
[] = {
343 .address
= AXP22X_PMIC_TEMP_H
,
344 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
345 BIT(IIO_CHAN_INFO_SCALE
) |
346 BIT(IIO_CHAN_INFO_OFFSET
),
347 .datasheet_name
= "pmic_temp",
349 AXP20X_ADC_CHANNEL(AXP813_GPIO0_V
, "gpio0_v", IIO_VOLTAGE
,
351 AXP20X_ADC_CHANNEL(AXP813_BATT_V
, "batt_v", IIO_VOLTAGE
,
353 AXP20X_ADC_CHANNEL(AXP22X_BATT_CHRG_I
, "batt_chrg_i", IIO_CURRENT
,
354 AXP20X_BATT_CHRG_I_H
),
355 AXP20X_ADC_CHANNEL(AXP22X_BATT_DISCHRG_I
, "batt_dischrg_i", IIO_CURRENT
,
356 AXP20X_BATT_DISCHRG_I_H
),
357 AXP20X_ADC_CHANNEL(AXP813_TS_IN
, "ts_v", IIO_VOLTAGE
,
361 static int axp192_adc_raw(struct iio_dev
*indio_dev
,
362 struct iio_chan_spec
const *chan
, int *val
)
364 struct axp20x_adc_iio
*info
= iio_priv(indio_dev
);
367 if (chan
->type
== IIO_CURRENT
&&
368 (chan
->channel
== AXP192_BATT_CHRG_I
||
369 chan
->channel
== AXP192_BATT_DISCHRG_I
))
374 ret
= axp20x_read_variable_width(info
->regmap
, chan
->address
, size
);
382 static int axp20x_adc_raw(struct iio_dev
*indio_dev
,
383 struct iio_chan_spec
const *chan
, int *val
)
385 struct axp20x_adc_iio
*info
= iio_priv(indio_dev
);
389 * N.B.: Unlike the Chinese datasheets tell, the charging current is
390 * stored on 12 bits, not 13 bits. Only discharging current is on 13
393 if (chan
->type
== IIO_CURRENT
&& chan
->channel
== AXP20X_BATT_DISCHRG_I
)
398 ret
= axp20x_read_variable_width(info
->regmap
, chan
->address
, size
);
406 static int axp22x_adc_raw(struct iio_dev
*indio_dev
,
407 struct iio_chan_spec
const *chan
, int *val
)
409 struct axp20x_adc_iio
*info
= iio_priv(indio_dev
);
412 ret
= axp20x_read_variable_width(info
->regmap
, chan
->address
, 12);
420 static int axp717_adc_raw(struct iio_dev
*indio_dev
,
421 struct iio_chan_spec
const *chan
, int *val
)
423 struct axp20x_adc_iio
*info
= iio_priv(indio_dev
);
428 * A generic "ADC data" channel is used for TS, tdie, vmid,
429 * and vbackup. This channel must both first be enabled and
430 * also selected before it can be read.
432 switch (chan
->channel
) {
434 regmap_write(info
->regmap
, AXP717_ADC_DATA_SEL
,
437 case AXP717_DIE_TEMP_V
:
438 regmap_write(info
->regmap
, AXP717_ADC_DATA_SEL
,
439 AXP717_ADC_DATA_TEMP
);
442 regmap_write(info
->regmap
, AXP717_ADC_DATA_SEL
,
443 AXP717_ADC_DATA_VMID
);
445 case AXP717_BKUP_BATT_V
:
446 regmap_write(info
->regmap
, AXP717_ADC_DATA_SEL
,
447 AXP717_ADC_DATA_BKUP_BATT
);
454 * All channels are 14 bits, with the first 2 bits on the high
455 * register reserved and the remaining bits as the ADC value.
457 ret
= regmap_bulk_read(info
->regmap
, chan
->address
, bulk_reg
, 2);
461 *val
= FIELD_GET(AXP717_ADC_DATA_MASK
, get_unaligned_be16(bulk_reg
));
465 static int axp813_adc_raw(struct iio_dev
*indio_dev
,
466 struct iio_chan_spec
const *chan
, int *val
)
468 struct axp20x_adc_iio
*info
= iio_priv(indio_dev
);
471 ret
= axp20x_read_variable_width(info
->regmap
, chan
->address
, 12);
479 static int axp192_adc_scale_voltage(int channel
, int *val
, int *val2
)
486 return IIO_VAL_INT_PLUS_MICRO
;
494 return IIO_VAL_INT_PLUS_MICRO
;
499 return IIO_VAL_INT_PLUS_MICRO
;
501 case AXP192_IPSOUT_V
:
504 return IIO_VAL_INT_PLUS_MICRO
;
510 return IIO_VAL_INT_PLUS_MICRO
;
517 static int axp20x_adc_scale_voltage(int channel
, int *val
, int *val2
)
524 return IIO_VAL_INT_PLUS_MICRO
;
530 return IIO_VAL_INT_PLUS_MICRO
;
535 return IIO_VAL_INT_PLUS_MICRO
;
537 case AXP20X_IPSOUT_V
:
540 return IIO_VAL_INT_PLUS_MICRO
;
546 return IIO_VAL_INT_PLUS_MICRO
;
553 static int axp22x_adc_scale_voltage(int channel
, int *val
, int *val2
)
560 return IIO_VAL_INT_PLUS_MICRO
;
566 return IIO_VAL_INT_PLUS_MICRO
;
572 static int axp813_adc_scale_voltage(int channel
, int *val
, int *val2
)
578 return IIO_VAL_INT_PLUS_MICRO
;
583 return IIO_VAL_INT_PLUS_MICRO
;
589 return IIO_VAL_INT_PLUS_MICRO
;
596 static int axp20x_adc_scale_current(int channel
, int *val
, int *val2
)
602 return IIO_VAL_INT_PLUS_MICRO
;
607 return IIO_VAL_INT_PLUS_MICRO
;
609 case AXP20X_BATT_DISCHRG_I
:
610 case AXP20X_BATT_CHRG_I
:
613 return IIO_VAL_INT_PLUS_MICRO
;
620 static int axp192_adc_scale(struct iio_chan_spec
const *chan
, int *val
,
623 switch (chan
->type
) {
625 return axp192_adc_scale_voltage(chan
->channel
, val
, val2
);
629 * AXP192 current channels are identical to the AXP20x,
630 * therefore we can re-use the scaling function.
632 return axp20x_adc_scale_current(chan
->channel
, val
, val2
);
643 static int axp20x_adc_scale(struct iio_chan_spec
const *chan
, int *val
,
646 switch (chan
->type
) {
648 return axp20x_adc_scale_voltage(chan
->channel
, val
, val2
);
651 return axp20x_adc_scale_current(chan
->channel
, val
, val2
);
662 static int axp22x_adc_scale(struct iio_chan_spec
const *chan
, int *val
,
665 switch (chan
->type
) {
667 return axp22x_adc_scale_voltage(chan
->channel
, val
, val2
);
682 static int axp717_adc_scale(struct iio_chan_spec
const *chan
, int *val
,
685 switch (chan
->type
) {
703 static int axp813_adc_scale(struct iio_chan_spec
const *chan
, int *val
,
706 switch (chan
->type
) {
708 return axp813_adc_scale_voltage(chan
->channel
, val
, val2
);
723 static int axp192_adc_offset_voltage(struct iio_dev
*indio_dev
, int channel
,
726 struct axp20x_adc_iio
*info
= iio_priv(indio_dev
);
730 ret
= regmap_read(info
->regmap
, AXP192_GPIO30_IN_RANGE
, ®val
);
736 regval
= FIELD_GET(AXP192_GPIO30_IN_RANGE_GPIO0
, regval
);
740 regval
= FIELD_GET(AXP192_GPIO30_IN_RANGE_GPIO1
, regval
);
744 regval
= FIELD_GET(AXP192_GPIO30_IN_RANGE_GPIO2
, regval
);
748 regval
= FIELD_GET(AXP192_GPIO30_IN_RANGE_GPIO3
, regval
);
755 *val
= regval
? 700000 : 0;
759 static int axp20x_adc_offset_voltage(struct iio_dev
*indio_dev
, int channel
,
762 struct axp20x_adc_iio
*info
= iio_priv(indio_dev
);
766 ret
= regmap_read(info
->regmap
, AXP20X_GPIO10_IN_RANGE
, ®val
);
772 regval
= FIELD_GET(AXP20X_GPIO10_IN_RANGE_GPIO0
, regval
);
776 regval
= FIELD_GET(AXP20X_GPIO10_IN_RANGE_GPIO1
, regval
);
783 *val
= regval
? 700000 : 0;
787 static int axp192_adc_offset(struct iio_dev
*indio_dev
,
788 struct iio_chan_spec
const *chan
, int *val
)
790 switch (chan
->type
) {
792 return axp192_adc_offset_voltage(indio_dev
, chan
->channel
, val
);
803 static int axp20x_adc_offset(struct iio_dev
*indio_dev
,
804 struct iio_chan_spec
const *chan
, int *val
)
806 switch (chan
->type
) {
808 return axp20x_adc_offset_voltage(indio_dev
, chan
->channel
, val
);
819 static int axp192_read_raw(struct iio_dev
*indio_dev
,
820 struct iio_chan_spec
const *chan
, int *val
,
821 int *val2
, long mask
)
824 case IIO_CHAN_INFO_OFFSET
:
825 return axp192_adc_offset(indio_dev
, chan
, val
);
827 case IIO_CHAN_INFO_SCALE
:
828 return axp192_adc_scale(chan
, val
, val2
);
830 case IIO_CHAN_INFO_RAW
:
831 return axp192_adc_raw(indio_dev
, chan
, val
);
838 static int axp20x_read_raw(struct iio_dev
*indio_dev
,
839 struct iio_chan_spec
const *chan
, int *val
,
840 int *val2
, long mask
)
843 case IIO_CHAN_INFO_OFFSET
:
844 return axp20x_adc_offset(indio_dev
, chan
, val
);
846 case IIO_CHAN_INFO_SCALE
:
847 return axp20x_adc_scale(chan
, val
, val2
);
849 case IIO_CHAN_INFO_RAW
:
850 return axp20x_adc_raw(indio_dev
, chan
, val
);
857 static int axp22x_read_raw(struct iio_dev
*indio_dev
,
858 struct iio_chan_spec
const *chan
, int *val
,
859 int *val2
, long mask
)
862 case IIO_CHAN_INFO_OFFSET
:
863 /* For PMIC temp only */
867 case IIO_CHAN_INFO_SCALE
:
868 return axp22x_adc_scale(chan
, val
, val2
);
870 case IIO_CHAN_INFO_RAW
:
871 return axp22x_adc_raw(indio_dev
, chan
, val
);
878 static int axp717_read_raw(struct iio_dev
*indio_dev
,
879 struct iio_chan_spec
const *chan
, int *val
,
880 int *val2
, long mask
)
883 case IIO_CHAN_INFO_SCALE
:
884 return axp717_adc_scale(chan
, val
, val2
);
886 case IIO_CHAN_INFO_RAW
:
887 return axp717_adc_raw(indio_dev
, chan
, val
);
894 static int axp813_read_raw(struct iio_dev
*indio_dev
,
895 struct iio_chan_spec
const *chan
, int *val
,
896 int *val2
, long mask
)
899 case IIO_CHAN_INFO_OFFSET
:
903 case IIO_CHAN_INFO_SCALE
:
904 return axp813_adc_scale(chan
, val
, val2
);
906 case IIO_CHAN_INFO_RAW
:
907 return axp813_adc_raw(indio_dev
, chan
, val
);
914 static int axp192_write_raw(struct iio_dev
*indio_dev
,
915 struct iio_chan_spec
const *chan
, int val
, int val2
,
918 struct axp20x_adc_iio
*info
= iio_priv(indio_dev
);
919 unsigned int regmask
, regval
;
922 * The AXP192 PMIC allows the user to choose between 0V and 0.7V offsets
923 * for (independently) GPIO0-3 when in ADC mode.
925 if (mask
!= IIO_CHAN_INFO_OFFSET
)
928 if (val
!= 0 && val
!= 700000)
931 switch (chan
->channel
) {
933 regmask
= AXP192_GPIO30_IN_RANGE_GPIO0
;
934 regval
= FIELD_PREP(AXP192_GPIO30_IN_RANGE_GPIO0
, !!val
);
938 regmask
= AXP192_GPIO30_IN_RANGE_GPIO1
;
939 regval
= FIELD_PREP(AXP192_GPIO30_IN_RANGE_GPIO1
, !!val
);
943 regmask
= AXP192_GPIO30_IN_RANGE_GPIO2
;
944 regval
= FIELD_PREP(AXP192_GPIO30_IN_RANGE_GPIO2
, !!val
);
948 regmask
= AXP192_GPIO30_IN_RANGE_GPIO3
;
949 regval
= FIELD_PREP(AXP192_GPIO30_IN_RANGE_GPIO3
, !!val
);
956 return regmap_update_bits(info
->regmap
, AXP192_GPIO30_IN_RANGE
, regmask
, regval
);
959 static int axp20x_write_raw(struct iio_dev
*indio_dev
,
960 struct iio_chan_spec
const *chan
, int val
, int val2
,
963 struct axp20x_adc_iio
*info
= iio_priv(indio_dev
);
964 unsigned int regmask
, regval
;
967 * The AXP20X PMIC allows the user to choose between 0V and 0.7V offsets
968 * for (independently) GPIO0 and GPIO1 when in ADC mode.
970 if (mask
!= IIO_CHAN_INFO_OFFSET
)
973 if (val
!= 0 && val
!= 700000)
976 switch (chan
->channel
) {
978 regmask
= AXP20X_GPIO10_IN_RANGE_GPIO0
;
979 regval
= FIELD_PREP(AXP20X_GPIO10_IN_RANGE_GPIO0
, !!val
);
983 regmask
= AXP20X_GPIO10_IN_RANGE_GPIO1
;
984 regval
= FIELD_PREP(AXP20X_GPIO10_IN_RANGE_GPIO1
, !!val
);
991 return regmap_update_bits(info
->regmap
, AXP20X_GPIO10_IN_RANGE
, regmask
, regval
);
994 static const struct iio_info axp192_adc_iio_info
= {
995 .read_raw
= axp192_read_raw
,
996 .write_raw
= axp192_write_raw
,
999 static const struct iio_info axp20x_adc_iio_info
= {
1000 .read_raw
= axp20x_read_raw
,
1001 .write_raw
= axp20x_write_raw
,
1004 static const struct iio_info axp22x_adc_iio_info
= {
1005 .read_raw
= axp22x_read_raw
,
1008 static const struct iio_info axp717_adc_iio_info
= {
1009 .read_raw
= axp717_read_raw
,
1012 static const struct iio_info axp813_adc_iio_info
= {
1013 .read_raw
= axp813_read_raw
,
1016 static int axp20x_adc_rate(struct axp20x_adc_iio
*info
, int rate
)
1018 return regmap_update_bits(info
->regmap
, AXP20X_ADC_RATE
,
1019 AXP20X_ADC_RATE_MASK
,
1020 AXP20X_ADC_RATE_HZ(rate
));
1023 static int axp22x_adc_rate(struct axp20x_adc_iio
*info
, int rate
)
1025 return regmap_update_bits(info
->regmap
, AXP20X_ADC_RATE
,
1026 AXP20X_ADC_RATE_MASK
,
1027 AXP22X_ADC_RATE_HZ(rate
));
1030 static int axp813_adc_rate(struct axp20x_adc_iio
*info
, int rate
)
1032 return regmap_update_bits(info
->regmap
, AXP813_ADC_RATE
,
1033 AXP813_ADC_RATE_MASK
,
1034 AXP813_ADC_RATE_HZ(rate
));
1038 const struct iio_info
*iio_info
;
1040 struct iio_chan_spec
const *channels
;
1041 unsigned long adc_en1
;
1042 unsigned long adc_en1_mask
;
1043 unsigned long adc_en2
;
1044 unsigned long adc_en2_mask
;
1045 int (*adc_rate
)(struct axp20x_adc_iio
*info
,
1047 struct iio_map
*maps
;
1050 static const struct axp_data axp192_data
= {
1051 .iio_info
= &axp192_adc_iio_info
,
1052 .num_channels
= ARRAY_SIZE(axp192_adc_channels
),
1053 .channels
= axp192_adc_channels
,
1054 .adc_en1_mask
= AXP192_ADC_EN1_MASK
,
1055 .adc_en2_mask
= AXP192_ADC_EN2_MASK
,
1056 .adc_rate
= axp20x_adc_rate
,
1057 .maps
= axp20x_maps
,
1060 static const struct axp_data axp20x_data
= {
1061 .iio_info
= &axp20x_adc_iio_info
,
1062 .num_channels
= ARRAY_SIZE(axp20x_adc_channels
),
1063 .channels
= axp20x_adc_channels
,
1064 .adc_en1
= AXP20X_ADC_EN1
,
1065 .adc_en1_mask
= AXP20X_ADC_EN1_MASK
,
1066 .adc_en2
= AXP20X_ADC_EN2
,
1067 .adc_en2_mask
= AXP20X_ADC_EN2_MASK
,
1068 .adc_rate
= axp20x_adc_rate
,
1069 .maps
= axp20x_maps
,
1072 static const struct axp_data axp22x_data
= {
1073 .iio_info
= &axp22x_adc_iio_info
,
1074 .num_channels
= ARRAY_SIZE(axp22x_adc_channels
),
1075 .channels
= axp22x_adc_channels
,
1076 .adc_en1
= AXP20X_ADC_EN1
,
1077 .adc_en1_mask
= AXP22X_ADC_EN1_MASK
,
1078 .adc_rate
= axp22x_adc_rate
,
1079 .maps
= axp22x_maps
,
1082 static const struct axp_data axp717_data
= {
1083 .iio_info
= &axp717_adc_iio_info
,
1084 .num_channels
= ARRAY_SIZE(axp717_adc_channels
),
1085 .channels
= axp717_adc_channels
,
1086 .adc_en1
= AXP717_ADC_CH_EN_CONTROL
,
1087 .adc_en1_mask
= AXP717_ADC_EN1_MASK
,
1088 .maps
= axp717_maps
,
1091 static const struct axp_data axp813_data
= {
1092 .iio_info
= &axp813_adc_iio_info
,
1093 .num_channels
= ARRAY_SIZE(axp813_adc_channels
),
1094 .channels
= axp813_adc_channels
,
1095 .adc_en1
= AXP20X_ADC_EN1
,
1096 .adc_en1_mask
= AXP22X_ADC_EN1_MASK
,
1097 .adc_rate
= axp813_adc_rate
,
1098 .maps
= axp22x_maps
,
1101 static const struct of_device_id axp20x_adc_of_match
[] = {
1102 { .compatible
= "x-powers,axp192-adc", .data
= (void *)&axp192_data
, },
1103 { .compatible
= "x-powers,axp209-adc", .data
= (void *)&axp20x_data
, },
1104 { .compatible
= "x-powers,axp221-adc", .data
= (void *)&axp22x_data
, },
1105 { .compatible
= "x-powers,axp717-adc", .data
= (void *)&axp717_data
, },
1106 { .compatible
= "x-powers,axp813-adc", .data
= (void *)&axp813_data
, },
1109 MODULE_DEVICE_TABLE(of
, axp20x_adc_of_match
);
1111 static const struct platform_device_id axp20x_adc_id_match
[] = {
1112 { .name
= "axp192-adc", .driver_data
= (kernel_ulong_t
)&axp192_data
, },
1113 { .name
= "axp20x-adc", .driver_data
= (kernel_ulong_t
)&axp20x_data
, },
1114 { .name
= "axp22x-adc", .driver_data
= (kernel_ulong_t
)&axp22x_data
, },
1115 { .name
= "axp717-adc", .driver_data
= (kernel_ulong_t
)&axp717_data
, },
1116 { .name
= "axp813-adc", .driver_data
= (kernel_ulong_t
)&axp813_data
, },
1119 MODULE_DEVICE_TABLE(platform
, axp20x_adc_id_match
);
1121 static int axp20x_probe(struct platform_device
*pdev
)
1123 struct axp20x_adc_iio
*info
;
1124 struct iio_dev
*indio_dev
;
1125 struct axp20x_dev
*axp20x_dev
;
1128 axp20x_dev
= dev_get_drvdata(pdev
->dev
.parent
);
1130 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*info
));
1134 info
= iio_priv(indio_dev
);
1135 platform_set_drvdata(pdev
, indio_dev
);
1137 info
->regmap
= axp20x_dev
->regmap
;
1138 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1140 if (!dev_fwnode(&pdev
->dev
)) {
1141 const struct platform_device_id
*id
;
1143 id
= platform_get_device_id(pdev
);
1144 info
->data
= (const struct axp_data
*)id
->driver_data
;
1146 struct device
*dev
= &pdev
->dev
;
1148 info
->data
= device_get_match_data(dev
);
1151 indio_dev
->name
= platform_get_device_id(pdev
)->name
;
1152 indio_dev
->info
= info
->data
->iio_info
;
1153 indio_dev
->num_channels
= info
->data
->num_channels
;
1154 indio_dev
->channels
= info
->data
->channels
;
1156 /* Enable the ADCs on IP */
1157 regmap_write(info
->regmap
, info
->data
->adc_en1
,
1158 info
->data
->adc_en1_mask
);
1160 if (info
->data
->adc_en2_mask
)
1161 regmap_set_bits(info
->regmap
, info
->data
->adc_en2
,
1162 info
->data
->adc_en2_mask
);
1164 /* Configure ADCs rate */
1165 if (info
->data
->adc_rate
)
1166 info
->data
->adc_rate(info
, 100);
1168 ret
= iio_map_array_register(indio_dev
, info
->data
->maps
);
1170 dev_err(&pdev
->dev
, "failed to register IIO maps: %d\n", ret
);
1174 ret
= iio_device_register(indio_dev
);
1176 dev_err(&pdev
->dev
, "could not register the device\n");
1183 iio_map_array_unregister(indio_dev
);
1186 regmap_write(info
->regmap
, info
->data
->adc_en1
, 0);
1188 if (info
->data
->adc_en2_mask
)
1189 regmap_write(info
->regmap
, info
->data
->adc_en2
, 0);
1194 static void axp20x_remove(struct platform_device
*pdev
)
1196 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
1197 struct axp20x_adc_iio
*info
= iio_priv(indio_dev
);
1199 iio_device_unregister(indio_dev
);
1200 iio_map_array_unregister(indio_dev
);
1202 regmap_write(info
->regmap
, info
->data
->adc_en1
, 0);
1204 if (info
->data
->adc_en2_mask
)
1205 regmap_write(info
->regmap
, info
->data
->adc_en2
, 0);
1208 static struct platform_driver axp20x_adc_driver
= {
1210 .name
= "axp20x-adc",
1211 .of_match_table
= axp20x_adc_of_match
,
1213 .id_table
= axp20x_adc_id_match
,
1214 .probe
= axp20x_probe
,
1215 .remove_new
= axp20x_remove
,
1218 module_platform_driver(axp20x_adc_driver
);
1220 MODULE_DESCRIPTION("ADC driver for AXP20X and AXP22X PMICs");
1221 MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
1222 MODULE_LICENSE("GPL");