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 const struct iio_map axp20x_maps
[] = {
159 IIO_MAP("vbus_v", "axp20x-usb-power-supply", "vbus_v"),
160 IIO_MAP("vbus_i", "axp20x-usb-power-supply", "vbus_i"),
161 IIO_MAP("acin_v", "axp20x-ac-power-supply", "acin_v"),
162 IIO_MAP("acin_i", "axp20x-ac-power-supply", "acin_i"),
163 IIO_MAP("batt_v", "axp20x-battery-power-supply", "batt_v"),
164 IIO_MAP("batt_chrg_i", "axp20x-battery-power-supply", "batt_chrg_i"),
165 IIO_MAP("batt_dischrg_i", "axp20x-battery-power-supply", "batt_dischrg_i"),
169 static const struct iio_map axp22x_maps
[] = {
170 IIO_MAP("batt_v", "axp20x-battery-power-supply", "batt_v"),
171 IIO_MAP("batt_chrg_i", "axp20x-battery-power-supply", "batt_chrg_i"),
172 IIO_MAP("batt_dischrg_i", "axp20x-battery-power-supply", "batt_dischrg_i"),
176 static struct iio_map axp717_maps
[] = {
178 .consumer_dev_name
= "axp20x-usb-power-supply",
179 .consumer_channel
= "vbus_v",
180 .adc_channel_label
= "vbus_v",
182 .consumer_dev_name
= "axp20x-battery-power-supply",
183 .consumer_channel
= "batt_v",
184 .adc_channel_label
= "batt_v",
186 .consumer_dev_name
= "axp20x-battery-power-supply",
187 .consumer_channel
= "batt_chrg_i",
188 .adc_channel_label
= "batt_chrg_i",
193 * Channels are mapped by physical system. Their channels share the same index.
194 * i.e. acin_i is in_current0_raw and acin_v is in_voltage0_raw.
195 * The only exception is for the battery. batt_v will be in_voltage6_raw and
196 * charge current in_current6_raw and discharge current will be in_current7_raw.
198 static const struct iio_chan_spec axp192_adc_channels
[] = {
199 AXP20X_ADC_CHANNEL(AXP192_ACIN_V
, "acin_v", IIO_VOLTAGE
,
200 AXP20X_ACIN_V_ADC_H
),
201 AXP20X_ADC_CHANNEL(AXP192_ACIN_I
, "acin_i", IIO_CURRENT
,
202 AXP20X_ACIN_I_ADC_H
),
203 AXP20X_ADC_CHANNEL(AXP192_VBUS_V
, "vbus_v", IIO_VOLTAGE
,
204 AXP20X_VBUS_V_ADC_H
),
205 AXP20X_ADC_CHANNEL(AXP192_VBUS_I
, "vbus_i", IIO_CURRENT
,
206 AXP20X_VBUS_I_ADC_H
),
209 .address
= AXP20X_TEMP_ADC_H
,
210 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
211 BIT(IIO_CHAN_INFO_SCALE
) |
212 BIT(IIO_CHAN_INFO_OFFSET
),
213 .datasheet_name
= "pmic_temp",
215 AXP20X_ADC_CHANNEL_OFFSET(AXP192_GPIO0_V
, "gpio0_v", IIO_VOLTAGE
,
216 AXP20X_GPIO0_V_ADC_H
),
217 AXP20X_ADC_CHANNEL_OFFSET(AXP192_GPIO1_V
, "gpio1_v", IIO_VOLTAGE
,
218 AXP20X_GPIO1_V_ADC_H
),
219 AXP20X_ADC_CHANNEL_OFFSET(AXP192_GPIO2_V
, "gpio2_v", IIO_VOLTAGE
,
220 AXP192_GPIO2_V_ADC_H
),
221 AXP20X_ADC_CHANNEL_OFFSET(AXP192_GPIO3_V
, "gpio3_v", IIO_VOLTAGE
,
222 AXP192_GPIO3_V_ADC_H
),
223 AXP20X_ADC_CHANNEL(AXP192_IPSOUT_V
, "ipsout_v", IIO_VOLTAGE
,
224 AXP20X_IPSOUT_V_HIGH_H
),
225 AXP20X_ADC_CHANNEL(AXP192_BATT_V
, "batt_v", IIO_VOLTAGE
,
227 AXP20X_ADC_CHANNEL(AXP192_BATT_CHRG_I
, "batt_chrg_i", IIO_CURRENT
,
228 AXP20X_BATT_CHRG_I_H
),
229 AXP20X_ADC_CHANNEL(AXP192_BATT_DISCHRG_I
, "batt_dischrg_i", IIO_CURRENT
,
230 AXP20X_BATT_DISCHRG_I_H
),
231 AXP20X_ADC_CHANNEL(AXP192_TS_IN
, "ts_v", IIO_VOLTAGE
,
235 static const struct iio_chan_spec axp20x_adc_channels
[] = {
236 AXP20X_ADC_CHANNEL(AXP20X_ACIN_V
, "acin_v", IIO_VOLTAGE
,
237 AXP20X_ACIN_V_ADC_H
),
238 AXP20X_ADC_CHANNEL(AXP20X_ACIN_I
, "acin_i", IIO_CURRENT
,
239 AXP20X_ACIN_I_ADC_H
),
240 AXP20X_ADC_CHANNEL(AXP20X_VBUS_V
, "vbus_v", IIO_VOLTAGE
,
241 AXP20X_VBUS_V_ADC_H
),
242 AXP20X_ADC_CHANNEL(AXP20X_VBUS_I
, "vbus_i", IIO_CURRENT
,
243 AXP20X_VBUS_I_ADC_H
),
246 .address
= AXP20X_TEMP_ADC_H
,
247 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
248 BIT(IIO_CHAN_INFO_SCALE
) |
249 BIT(IIO_CHAN_INFO_OFFSET
),
250 .datasheet_name
= "pmic_temp",
252 AXP20X_ADC_CHANNEL_OFFSET(AXP20X_GPIO0_V
, "gpio0_v", IIO_VOLTAGE
,
253 AXP20X_GPIO0_V_ADC_H
),
254 AXP20X_ADC_CHANNEL_OFFSET(AXP20X_GPIO1_V
, "gpio1_v", IIO_VOLTAGE
,
255 AXP20X_GPIO1_V_ADC_H
),
256 AXP20X_ADC_CHANNEL(AXP20X_IPSOUT_V
, "ipsout_v", IIO_VOLTAGE
,
257 AXP20X_IPSOUT_V_HIGH_H
),
258 AXP20X_ADC_CHANNEL(AXP20X_BATT_V
, "batt_v", IIO_VOLTAGE
,
260 AXP20X_ADC_CHANNEL(AXP20X_BATT_CHRG_I
, "batt_chrg_i", IIO_CURRENT
,
261 AXP20X_BATT_CHRG_I_H
),
262 AXP20X_ADC_CHANNEL(AXP20X_BATT_DISCHRG_I
, "batt_dischrg_i", IIO_CURRENT
,
263 AXP20X_BATT_DISCHRG_I_H
),
264 AXP20X_ADC_CHANNEL(AXP20X_TS_IN
, "ts_v", IIO_VOLTAGE
,
268 static const struct iio_chan_spec axp22x_adc_channels
[] = {
271 .address
= AXP22X_PMIC_TEMP_H
,
272 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
273 BIT(IIO_CHAN_INFO_SCALE
) |
274 BIT(IIO_CHAN_INFO_OFFSET
),
275 .datasheet_name
= "pmic_temp",
277 AXP20X_ADC_CHANNEL(AXP22X_BATT_V
, "batt_v", IIO_VOLTAGE
,
279 AXP20X_ADC_CHANNEL(AXP22X_BATT_CHRG_I
, "batt_chrg_i", IIO_CURRENT
,
280 AXP20X_BATT_CHRG_I_H
),
281 AXP20X_ADC_CHANNEL(AXP22X_BATT_DISCHRG_I
, "batt_dischrg_i", IIO_CURRENT
,
282 AXP20X_BATT_DISCHRG_I_H
),
283 AXP20X_ADC_CHANNEL(AXP22X_TS_IN
, "ts_v", IIO_VOLTAGE
,
288 * Scale and offset is unknown for temp, ts, batt_chrg_i, vmid_v, and
289 * bkup_batt_v channels. Leaving scale and offset undefined for now.
291 static const struct iio_chan_spec axp717_adc_channels
[] = {
292 AXP20X_ADC_CHANNEL(AXP717_BATT_V
, "batt_v", IIO_VOLTAGE
,
294 AXP20X_ADC_CHANNEL(AXP717_TS_IN
, "ts_v", IIO_VOLTAGE
,
296 AXP20X_ADC_CHANNEL(AXP717_VBUS_V
, "vbus_v", IIO_VOLTAGE
,
298 AXP20X_ADC_CHANNEL(AXP717_VSYS_V
, "vsys_v", IIO_VOLTAGE
,
300 AXP20X_ADC_CHANNEL(AXP717_DIE_TEMP_V
, "pmic_temp", IIO_TEMP
,
302 AXP20X_ADC_CHANNEL(AXP717_BATT_CHRG_I
, "batt_chrg_i", IIO_CURRENT
,
303 AXP717_BATT_CHRG_I_H
),
304 AXP20X_ADC_CHANNEL(AXP717_VMID_V
, "vmid_v", IIO_VOLTAGE
,
306 AXP20X_ADC_CHANNEL(AXP717_BKUP_BATT_V
, "bkup_batt_v", IIO_VOLTAGE
,
310 static const struct iio_chan_spec axp813_adc_channels
[] = {
313 .address
= AXP22X_PMIC_TEMP_H
,
314 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
315 BIT(IIO_CHAN_INFO_SCALE
) |
316 BIT(IIO_CHAN_INFO_OFFSET
),
317 .datasheet_name
= "pmic_temp",
319 AXP20X_ADC_CHANNEL(AXP813_GPIO0_V
, "gpio0_v", IIO_VOLTAGE
,
321 AXP20X_ADC_CHANNEL(AXP813_BATT_V
, "batt_v", IIO_VOLTAGE
,
323 AXP20X_ADC_CHANNEL(AXP22X_BATT_CHRG_I
, "batt_chrg_i", IIO_CURRENT
,
324 AXP20X_BATT_CHRG_I_H
),
325 AXP20X_ADC_CHANNEL(AXP22X_BATT_DISCHRG_I
, "batt_dischrg_i", IIO_CURRENT
,
326 AXP20X_BATT_DISCHRG_I_H
),
327 AXP20X_ADC_CHANNEL(AXP813_TS_IN
, "ts_v", IIO_VOLTAGE
,
331 static int axp192_adc_raw(struct iio_dev
*indio_dev
,
332 struct iio_chan_spec
const *chan
, int *val
)
334 struct axp20x_adc_iio
*info
= iio_priv(indio_dev
);
337 if (chan
->type
== IIO_CURRENT
&&
338 (chan
->channel
== AXP192_BATT_CHRG_I
||
339 chan
->channel
== AXP192_BATT_DISCHRG_I
))
344 ret
= axp20x_read_variable_width(info
->regmap
, chan
->address
, size
);
352 static int axp20x_adc_raw(struct iio_dev
*indio_dev
,
353 struct iio_chan_spec
const *chan
, int *val
)
355 struct axp20x_adc_iio
*info
= iio_priv(indio_dev
);
359 * N.B.: Unlike the Chinese datasheets tell, the charging current is
360 * stored on 12 bits, not 13 bits. Only discharging current is on 13
363 if (chan
->type
== IIO_CURRENT
&& chan
->channel
== AXP20X_BATT_DISCHRG_I
)
368 ret
= axp20x_read_variable_width(info
->regmap
, chan
->address
, size
);
376 static int axp22x_adc_raw(struct iio_dev
*indio_dev
,
377 struct iio_chan_spec
const *chan
, int *val
)
379 struct axp20x_adc_iio
*info
= iio_priv(indio_dev
);
382 ret
= axp20x_read_variable_width(info
->regmap
, chan
->address
, 12);
390 static int axp717_adc_raw(struct iio_dev
*indio_dev
,
391 struct iio_chan_spec
const *chan
, int *val
)
393 struct axp20x_adc_iio
*info
= iio_priv(indio_dev
);
398 * A generic "ADC data" channel is used for TS, tdie, vmid,
399 * and vbackup. This channel must both first be enabled and
400 * also selected before it can be read.
402 switch (chan
->channel
) {
404 regmap_write(info
->regmap
, AXP717_ADC_DATA_SEL
,
407 case AXP717_DIE_TEMP_V
:
408 regmap_write(info
->regmap
, AXP717_ADC_DATA_SEL
,
409 AXP717_ADC_DATA_TEMP
);
412 regmap_write(info
->regmap
, AXP717_ADC_DATA_SEL
,
413 AXP717_ADC_DATA_VMID
);
415 case AXP717_BKUP_BATT_V
:
416 regmap_write(info
->regmap
, AXP717_ADC_DATA_SEL
,
417 AXP717_ADC_DATA_BKUP_BATT
);
424 * All channels are 14 bits, with the first 2 bits on the high
425 * register reserved and the remaining bits as the ADC value.
427 ret
= regmap_bulk_read(info
->regmap
, chan
->address
, bulk_reg
, 2);
431 *val
= FIELD_GET(AXP717_ADC_DATA_MASK
, get_unaligned_be16(bulk_reg
));
435 static int axp813_adc_raw(struct iio_dev
*indio_dev
,
436 struct iio_chan_spec
const *chan
, int *val
)
438 struct axp20x_adc_iio
*info
= iio_priv(indio_dev
);
441 ret
= axp20x_read_variable_width(info
->regmap
, chan
->address
, 12);
449 static int axp192_adc_scale_voltage(int channel
, int *val
, int *val2
)
456 return IIO_VAL_INT_PLUS_MICRO
;
464 return IIO_VAL_INT_PLUS_MICRO
;
469 return IIO_VAL_INT_PLUS_MICRO
;
471 case AXP192_IPSOUT_V
:
474 return IIO_VAL_INT_PLUS_MICRO
;
480 return IIO_VAL_INT_PLUS_MICRO
;
487 static int axp20x_adc_scale_voltage(int channel
, int *val
, int *val2
)
494 return IIO_VAL_INT_PLUS_MICRO
;
500 return IIO_VAL_INT_PLUS_MICRO
;
505 return IIO_VAL_INT_PLUS_MICRO
;
507 case AXP20X_IPSOUT_V
:
510 return IIO_VAL_INT_PLUS_MICRO
;
516 return IIO_VAL_INT_PLUS_MICRO
;
523 static int axp22x_adc_scale_voltage(int channel
, int *val
, int *val2
)
530 return IIO_VAL_INT_PLUS_MICRO
;
536 return IIO_VAL_INT_PLUS_MICRO
;
542 static int axp813_adc_scale_voltage(int channel
, int *val
, int *val2
)
548 return IIO_VAL_INT_PLUS_MICRO
;
553 return IIO_VAL_INT_PLUS_MICRO
;
559 return IIO_VAL_INT_PLUS_MICRO
;
566 static int axp20x_adc_scale_current(int channel
, int *val
, int *val2
)
572 return IIO_VAL_INT_PLUS_MICRO
;
577 return IIO_VAL_INT_PLUS_MICRO
;
579 case AXP20X_BATT_DISCHRG_I
:
580 case AXP20X_BATT_CHRG_I
:
583 return IIO_VAL_INT_PLUS_MICRO
;
590 static int axp192_adc_scale(struct iio_chan_spec
const *chan
, int *val
,
593 switch (chan
->type
) {
595 return axp192_adc_scale_voltage(chan
->channel
, val
, val2
);
599 * AXP192 current channels are identical to the AXP20x,
600 * therefore we can re-use the scaling function.
602 return axp20x_adc_scale_current(chan
->channel
, val
, val2
);
613 static int axp20x_adc_scale(struct iio_chan_spec
const *chan
, int *val
,
616 switch (chan
->type
) {
618 return axp20x_adc_scale_voltage(chan
->channel
, val
, val2
);
621 return axp20x_adc_scale_current(chan
->channel
, val
, val2
);
632 static int axp22x_adc_scale(struct iio_chan_spec
const *chan
, int *val
,
635 switch (chan
->type
) {
637 return axp22x_adc_scale_voltage(chan
->channel
, val
, val2
);
652 static int axp717_adc_scale(struct iio_chan_spec
const *chan
, int *val
,
655 switch (chan
->type
) {
673 static int axp813_adc_scale(struct iio_chan_spec
const *chan
, int *val
,
676 switch (chan
->type
) {
678 return axp813_adc_scale_voltage(chan
->channel
, val
, val2
);
693 static int axp192_adc_offset_voltage(struct iio_dev
*indio_dev
, int channel
,
696 struct axp20x_adc_iio
*info
= iio_priv(indio_dev
);
700 ret
= regmap_read(info
->regmap
, AXP192_GPIO30_IN_RANGE
, ®val
);
706 regval
= FIELD_GET(AXP192_GPIO30_IN_RANGE_GPIO0
, regval
);
710 regval
= FIELD_GET(AXP192_GPIO30_IN_RANGE_GPIO1
, regval
);
714 regval
= FIELD_GET(AXP192_GPIO30_IN_RANGE_GPIO2
, regval
);
718 regval
= FIELD_GET(AXP192_GPIO30_IN_RANGE_GPIO3
, regval
);
725 *val
= regval
? 700000 : 0;
729 static int axp20x_adc_offset_voltage(struct iio_dev
*indio_dev
, int channel
,
732 struct axp20x_adc_iio
*info
= iio_priv(indio_dev
);
736 ret
= regmap_read(info
->regmap
, AXP20X_GPIO10_IN_RANGE
, ®val
);
742 regval
= FIELD_GET(AXP20X_GPIO10_IN_RANGE_GPIO0
, regval
);
746 regval
= FIELD_GET(AXP20X_GPIO10_IN_RANGE_GPIO1
, regval
);
753 *val
= regval
? 700000 : 0;
757 static int axp192_adc_offset(struct iio_dev
*indio_dev
,
758 struct iio_chan_spec
const *chan
, int *val
)
760 switch (chan
->type
) {
762 return axp192_adc_offset_voltage(indio_dev
, chan
->channel
, val
);
773 static int axp20x_adc_offset(struct iio_dev
*indio_dev
,
774 struct iio_chan_spec
const *chan
, int *val
)
776 switch (chan
->type
) {
778 return axp20x_adc_offset_voltage(indio_dev
, chan
->channel
, val
);
789 static int axp192_read_raw(struct iio_dev
*indio_dev
,
790 struct iio_chan_spec
const *chan
, int *val
,
791 int *val2
, long mask
)
794 case IIO_CHAN_INFO_OFFSET
:
795 return axp192_adc_offset(indio_dev
, chan
, val
);
797 case IIO_CHAN_INFO_SCALE
:
798 return axp192_adc_scale(chan
, val
, val2
);
800 case IIO_CHAN_INFO_RAW
:
801 return axp192_adc_raw(indio_dev
, chan
, val
);
808 static int axp20x_read_raw(struct iio_dev
*indio_dev
,
809 struct iio_chan_spec
const *chan
, int *val
,
810 int *val2
, long mask
)
813 case IIO_CHAN_INFO_OFFSET
:
814 return axp20x_adc_offset(indio_dev
, chan
, val
);
816 case IIO_CHAN_INFO_SCALE
:
817 return axp20x_adc_scale(chan
, val
, val2
);
819 case IIO_CHAN_INFO_RAW
:
820 return axp20x_adc_raw(indio_dev
, chan
, val
);
827 static int axp22x_read_raw(struct iio_dev
*indio_dev
,
828 struct iio_chan_spec
const *chan
, int *val
,
829 int *val2
, long mask
)
832 case IIO_CHAN_INFO_OFFSET
:
833 /* For PMIC temp only */
837 case IIO_CHAN_INFO_SCALE
:
838 return axp22x_adc_scale(chan
, val
, val2
);
840 case IIO_CHAN_INFO_RAW
:
841 return axp22x_adc_raw(indio_dev
, chan
, val
);
848 static int axp717_read_raw(struct iio_dev
*indio_dev
,
849 struct iio_chan_spec
const *chan
, int *val
,
850 int *val2
, long mask
)
853 case IIO_CHAN_INFO_SCALE
:
854 return axp717_adc_scale(chan
, val
, val2
);
856 case IIO_CHAN_INFO_RAW
:
857 return axp717_adc_raw(indio_dev
, chan
, val
);
864 static int axp813_read_raw(struct iio_dev
*indio_dev
,
865 struct iio_chan_spec
const *chan
, int *val
,
866 int *val2
, long mask
)
869 case IIO_CHAN_INFO_OFFSET
:
873 case IIO_CHAN_INFO_SCALE
:
874 return axp813_adc_scale(chan
, val
, val2
);
876 case IIO_CHAN_INFO_RAW
:
877 return axp813_adc_raw(indio_dev
, chan
, val
);
884 static int axp192_write_raw(struct iio_dev
*indio_dev
,
885 struct iio_chan_spec
const *chan
, int val
, int val2
,
888 struct axp20x_adc_iio
*info
= iio_priv(indio_dev
);
889 unsigned int regmask
, regval
;
892 * The AXP192 PMIC allows the user to choose between 0V and 0.7V offsets
893 * for (independently) GPIO0-3 when in ADC mode.
895 if (mask
!= IIO_CHAN_INFO_OFFSET
)
898 if (val
!= 0 && val
!= 700000)
901 switch (chan
->channel
) {
903 regmask
= AXP192_GPIO30_IN_RANGE_GPIO0
;
904 regval
= FIELD_PREP(AXP192_GPIO30_IN_RANGE_GPIO0
, !!val
);
908 regmask
= AXP192_GPIO30_IN_RANGE_GPIO1
;
909 regval
= FIELD_PREP(AXP192_GPIO30_IN_RANGE_GPIO1
, !!val
);
913 regmask
= AXP192_GPIO30_IN_RANGE_GPIO2
;
914 regval
= FIELD_PREP(AXP192_GPIO30_IN_RANGE_GPIO2
, !!val
);
918 regmask
= AXP192_GPIO30_IN_RANGE_GPIO3
;
919 regval
= FIELD_PREP(AXP192_GPIO30_IN_RANGE_GPIO3
, !!val
);
926 return regmap_update_bits(info
->regmap
, AXP192_GPIO30_IN_RANGE
, regmask
, regval
);
929 static int axp20x_write_raw(struct iio_dev
*indio_dev
,
930 struct iio_chan_spec
const *chan
, int val
, int val2
,
933 struct axp20x_adc_iio
*info
= iio_priv(indio_dev
);
934 unsigned int regmask
, regval
;
937 * The AXP20X PMIC allows the user to choose between 0V and 0.7V offsets
938 * for (independently) GPIO0 and GPIO1 when in ADC mode.
940 if (mask
!= IIO_CHAN_INFO_OFFSET
)
943 if (val
!= 0 && val
!= 700000)
946 switch (chan
->channel
) {
948 regmask
= AXP20X_GPIO10_IN_RANGE_GPIO0
;
949 regval
= FIELD_PREP(AXP20X_GPIO10_IN_RANGE_GPIO0
, !!val
);
953 regmask
= AXP20X_GPIO10_IN_RANGE_GPIO1
;
954 regval
= FIELD_PREP(AXP20X_GPIO10_IN_RANGE_GPIO1
, !!val
);
961 return regmap_update_bits(info
->regmap
, AXP20X_GPIO10_IN_RANGE
, regmask
, regval
);
964 static const struct iio_info axp192_adc_iio_info
= {
965 .read_raw
= axp192_read_raw
,
966 .write_raw
= axp192_write_raw
,
969 static const struct iio_info axp20x_adc_iio_info
= {
970 .read_raw
= axp20x_read_raw
,
971 .write_raw
= axp20x_write_raw
,
974 static const struct iio_info axp22x_adc_iio_info
= {
975 .read_raw
= axp22x_read_raw
,
978 static const struct iio_info axp717_adc_iio_info
= {
979 .read_raw
= axp717_read_raw
,
982 static const struct iio_info axp813_adc_iio_info
= {
983 .read_raw
= axp813_read_raw
,
986 static int axp20x_adc_rate(struct axp20x_adc_iio
*info
, int rate
)
988 return regmap_update_bits(info
->regmap
, AXP20X_ADC_RATE
,
989 AXP20X_ADC_RATE_MASK
,
990 AXP20X_ADC_RATE_HZ(rate
));
993 static int axp22x_adc_rate(struct axp20x_adc_iio
*info
, int rate
)
995 return regmap_update_bits(info
->regmap
, AXP20X_ADC_RATE
,
996 AXP20X_ADC_RATE_MASK
,
997 AXP22X_ADC_RATE_HZ(rate
));
1000 static int axp813_adc_rate(struct axp20x_adc_iio
*info
, int rate
)
1002 return regmap_update_bits(info
->regmap
, AXP813_ADC_RATE
,
1003 AXP813_ADC_RATE_MASK
,
1004 AXP813_ADC_RATE_HZ(rate
));
1008 const struct iio_info
*iio_info
;
1010 struct iio_chan_spec
const *channels
;
1011 unsigned long adc_en1
;
1012 unsigned long adc_en1_mask
;
1013 unsigned long adc_en2
;
1014 unsigned long adc_en2_mask
;
1015 int (*adc_rate
)(struct axp20x_adc_iio
*info
,
1017 const struct iio_map
*maps
;
1020 static const struct axp_data axp192_data
= {
1021 .iio_info
= &axp192_adc_iio_info
,
1022 .num_channels
= ARRAY_SIZE(axp192_adc_channels
),
1023 .channels
= axp192_adc_channels
,
1024 .adc_en1_mask
= AXP192_ADC_EN1_MASK
,
1025 .adc_en2_mask
= AXP192_ADC_EN2_MASK
,
1026 .adc_rate
= axp20x_adc_rate
,
1027 .maps
= axp20x_maps
,
1030 static const struct axp_data axp20x_data
= {
1031 .iio_info
= &axp20x_adc_iio_info
,
1032 .num_channels
= ARRAY_SIZE(axp20x_adc_channels
),
1033 .channels
= axp20x_adc_channels
,
1034 .adc_en1
= AXP20X_ADC_EN1
,
1035 .adc_en1_mask
= AXP20X_ADC_EN1_MASK
,
1036 .adc_en2
= AXP20X_ADC_EN2
,
1037 .adc_en2_mask
= AXP20X_ADC_EN2_MASK
,
1038 .adc_rate
= axp20x_adc_rate
,
1039 .maps
= axp20x_maps
,
1042 static const struct axp_data axp22x_data
= {
1043 .iio_info
= &axp22x_adc_iio_info
,
1044 .num_channels
= ARRAY_SIZE(axp22x_adc_channels
),
1045 .channels
= axp22x_adc_channels
,
1046 .adc_en1
= AXP20X_ADC_EN1
,
1047 .adc_en1_mask
= AXP22X_ADC_EN1_MASK
,
1048 .adc_rate
= axp22x_adc_rate
,
1049 .maps
= axp22x_maps
,
1052 static const struct axp_data axp717_data
= {
1053 .iio_info
= &axp717_adc_iio_info
,
1054 .num_channels
= ARRAY_SIZE(axp717_adc_channels
),
1055 .channels
= axp717_adc_channels
,
1056 .adc_en1
= AXP717_ADC_CH_EN_CONTROL
,
1057 .adc_en1_mask
= AXP717_ADC_EN1_MASK
,
1058 .maps
= axp717_maps
,
1061 static const struct axp_data axp813_data
= {
1062 .iio_info
= &axp813_adc_iio_info
,
1063 .num_channels
= ARRAY_SIZE(axp813_adc_channels
),
1064 .channels
= axp813_adc_channels
,
1065 .adc_en1
= AXP20X_ADC_EN1
,
1066 .adc_en1_mask
= AXP22X_ADC_EN1_MASK
,
1067 .adc_rate
= axp813_adc_rate
,
1068 .maps
= axp22x_maps
,
1071 static const struct of_device_id axp20x_adc_of_match
[] = {
1072 { .compatible
= "x-powers,axp192-adc", .data
= (void *)&axp192_data
, },
1073 { .compatible
= "x-powers,axp209-adc", .data
= (void *)&axp20x_data
, },
1074 { .compatible
= "x-powers,axp221-adc", .data
= (void *)&axp22x_data
, },
1075 { .compatible
= "x-powers,axp717-adc", .data
= (void *)&axp717_data
, },
1076 { .compatible
= "x-powers,axp813-adc", .data
= (void *)&axp813_data
, },
1079 MODULE_DEVICE_TABLE(of
, axp20x_adc_of_match
);
1081 static const struct platform_device_id axp20x_adc_id_match
[] = {
1082 { .name
= "axp192-adc", .driver_data
= (kernel_ulong_t
)&axp192_data
, },
1083 { .name
= "axp20x-adc", .driver_data
= (kernel_ulong_t
)&axp20x_data
, },
1084 { .name
= "axp22x-adc", .driver_data
= (kernel_ulong_t
)&axp22x_data
, },
1085 { .name
= "axp717-adc", .driver_data
= (kernel_ulong_t
)&axp717_data
, },
1086 { .name
= "axp813-adc", .driver_data
= (kernel_ulong_t
)&axp813_data
, },
1089 MODULE_DEVICE_TABLE(platform
, axp20x_adc_id_match
);
1091 static int axp20x_probe(struct platform_device
*pdev
)
1093 struct axp20x_adc_iio
*info
;
1094 struct iio_dev
*indio_dev
;
1095 struct axp20x_dev
*axp20x_dev
;
1098 axp20x_dev
= dev_get_drvdata(pdev
->dev
.parent
);
1100 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*info
));
1104 info
= iio_priv(indio_dev
);
1105 platform_set_drvdata(pdev
, indio_dev
);
1107 info
->regmap
= axp20x_dev
->regmap
;
1108 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1110 if (!dev_fwnode(&pdev
->dev
)) {
1111 const struct platform_device_id
*id
;
1113 id
= platform_get_device_id(pdev
);
1114 info
->data
= (const struct axp_data
*)id
->driver_data
;
1116 struct device
*dev
= &pdev
->dev
;
1118 info
->data
= device_get_match_data(dev
);
1121 indio_dev
->name
= platform_get_device_id(pdev
)->name
;
1122 indio_dev
->info
= info
->data
->iio_info
;
1123 indio_dev
->num_channels
= info
->data
->num_channels
;
1124 indio_dev
->channels
= info
->data
->channels
;
1126 /* Enable the ADCs on IP */
1127 regmap_write(info
->regmap
, info
->data
->adc_en1
,
1128 info
->data
->adc_en1_mask
);
1130 if (info
->data
->adc_en2_mask
)
1131 regmap_set_bits(info
->regmap
, info
->data
->adc_en2
,
1132 info
->data
->adc_en2_mask
);
1134 /* Configure ADCs rate */
1135 if (info
->data
->adc_rate
)
1136 info
->data
->adc_rate(info
, 100);
1138 ret
= iio_map_array_register(indio_dev
, info
->data
->maps
);
1140 dev_err(&pdev
->dev
, "failed to register IIO maps: %d\n", ret
);
1144 ret
= iio_device_register(indio_dev
);
1146 dev_err(&pdev
->dev
, "could not register the device\n");
1153 iio_map_array_unregister(indio_dev
);
1156 regmap_write(info
->regmap
, info
->data
->adc_en1
, 0);
1158 if (info
->data
->adc_en2_mask
)
1159 regmap_write(info
->regmap
, info
->data
->adc_en2
, 0);
1164 static void axp20x_remove(struct platform_device
*pdev
)
1166 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
1167 struct axp20x_adc_iio
*info
= iio_priv(indio_dev
);
1169 iio_device_unregister(indio_dev
);
1170 iio_map_array_unregister(indio_dev
);
1172 regmap_write(info
->regmap
, info
->data
->adc_en1
, 0);
1174 if (info
->data
->adc_en2_mask
)
1175 regmap_write(info
->regmap
, info
->data
->adc_en2
, 0);
1178 static struct platform_driver axp20x_adc_driver
= {
1180 .name
= "axp20x-adc",
1181 .of_match_table
= axp20x_adc_of_match
,
1183 .id_table
= axp20x_adc_id_match
,
1184 .probe
= axp20x_probe
,
1185 .remove
= axp20x_remove
,
1188 module_platform_driver(axp20x_adc_driver
);
1190 MODULE_DESCRIPTION("ADC driver for AXP20X and AXP22X PMICs");
1191 MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
1192 MODULE_LICENSE("GPL");