1 // SPDX-License-Identifier: GPL-2.0-only
2 /* ADC driver for AXP20X and AXP22X PMICs
4 * Copyright (c) 2016 Free Electrons NextThing Co.
8 #include <linux/bitfield.h>
9 #include <linux/completion.h>
10 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/mod_devicetable.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/property.h>
17 #include <linux/regmap.h>
18 #include <linux/thermal.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/driver.h>
22 #include <linux/iio/machine.h>
23 #include <linux/mfd/axp20x.h>
25 #define AXP192_ADC_EN1_MASK GENMASK(7, 0)
26 #define AXP192_ADC_EN2_MASK (GENMASK(3, 0) | BIT(7))
28 #define AXP20X_ADC_EN1_MASK GENMASK(7, 0)
29 #define AXP20X_ADC_EN2_MASK (GENMASK(3, 2) | BIT(7))
31 #define AXP22X_ADC_EN1_MASK (GENMASK(7, 5) | BIT(0))
33 #define AXP192_GPIO30_IN_RANGE_GPIO0 BIT(0)
34 #define AXP192_GPIO30_IN_RANGE_GPIO1 BIT(1)
35 #define AXP192_GPIO30_IN_RANGE_GPIO2 BIT(2)
36 #define AXP192_GPIO30_IN_RANGE_GPIO3 BIT(3)
38 #define AXP20X_GPIO10_IN_RANGE_GPIO0 BIT(0)
39 #define AXP20X_GPIO10_IN_RANGE_GPIO1 BIT(1)
41 #define AXP20X_ADC_RATE_MASK GENMASK(7, 6)
42 #define AXP20X_ADC_RATE_HZ(x) ((ilog2((x) / 25) << 6) & AXP20X_ADC_RATE_MASK)
44 #define AXP22X_ADC_RATE_HZ(x) ((ilog2((x) / 100) << 6) & AXP20X_ADC_RATE_MASK)
46 #define AXP813_V_I_ADC_RATE_MASK GENMASK(5, 4)
47 #define AXP813_ADC_RATE_MASK (AXP20X_ADC_RATE_MASK | AXP813_V_I_ADC_RATE_MASK)
48 #define AXP813_TS_GPIO0_ADC_RATE_HZ(x) AXP20X_ADC_RATE_HZ(x)
49 #define AXP813_V_I_ADC_RATE_HZ(x) ((ilog2((x) / 100) << 4) & AXP813_V_I_ADC_RATE_MASK)
50 #define AXP813_ADC_RATE_HZ(x) (AXP20X_ADC_RATE_HZ(x) | AXP813_V_I_ADC_RATE_HZ(x))
52 #define AXP20X_ADC_CHANNEL(_channel, _name, _type, _reg) \
56 .channel = _channel, \
58 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
59 BIT(IIO_CHAN_INFO_SCALE), \
60 .datasheet_name = _name, \
63 #define AXP20X_ADC_CHANNEL_OFFSET(_channel, _name, _type, _reg) \
67 .channel = _channel, \
69 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
70 BIT(IIO_CHAN_INFO_SCALE) |\
71 BIT(IIO_CHAN_INFO_OFFSET),\
72 .datasheet_name = _name, \
77 struct axp20x_adc_iio {
78 struct regmap *regmap;
79 const struct axp_data *data;
82 enum axp192_adc_channel_v {
94 enum axp192_adc_channel_i {
98 AXP192_BATT_DISCHRG_I,
101 enum axp20x_adc_channel_v {
111 enum axp20x_adc_channel_i {
115 AXP20X_BATT_DISCHRG_I,
118 enum axp22x_adc_channel_v {
123 enum axp22x_adc_channel_i {
124 AXP22X_BATT_CHRG_I = 1,
125 AXP22X_BATT_DISCHRG_I,
128 enum axp813_adc_channel_v {
134 static struct iio_map axp20x_maps[] = {
136 .consumer_dev_name = "axp20x-usb-power-supply",
137 .consumer_channel = "vbus_v",
138 .adc_channel_label = "vbus_v",
140 .consumer_dev_name = "axp20x-usb-power-supply",
141 .consumer_channel = "vbus_i",
142 .adc_channel_label = "vbus_i",
144 .consumer_dev_name = "axp20x-ac-power-supply",
145 .consumer_channel = "acin_v",
146 .adc_channel_label = "acin_v",
148 .consumer_dev_name = "axp20x-ac-power-supply",
149 .consumer_channel = "acin_i",
150 .adc_channel_label = "acin_i",
152 .consumer_dev_name = "axp20x-battery-power-supply",
153 .consumer_channel = "batt_v",
154 .adc_channel_label = "batt_v",
156 .consumer_dev_name = "axp20x-battery-power-supply",
157 .consumer_channel = "batt_chrg_i",
158 .adc_channel_label = "batt_chrg_i",
160 .consumer_dev_name = "axp20x-battery-power-supply",
161 .consumer_channel = "batt_dischrg_i",
162 .adc_channel_label = "batt_dischrg_i",
163 }, { /* sentinel */ }
166 static struct iio_map axp22x_maps[] = {
168 .consumer_dev_name = "axp20x-battery-power-supply",
169 .consumer_channel = "batt_v",
170 .adc_channel_label = "batt_v",
172 .consumer_dev_name = "axp20x-battery-power-supply",
173 .consumer_channel = "batt_chrg_i",
174 .adc_channel_label = "batt_chrg_i",
176 .consumer_dev_name = "axp20x-battery-power-supply",
177 .consumer_channel = "batt_dischrg_i",
178 .adc_channel_label = "batt_dischrg_i",
179 }, { /* sentinel */ }
183 * Channels are mapped by physical system. Their channels share the same index.
184 * i.e. acin_i is in_current0_raw and acin_v is in_voltage0_raw.
185 * The only exception is for the battery. batt_v will be in_voltage6_raw and
186 * charge current in_current6_raw and discharge current will be in_current7_raw.
188 static const struct iio_chan_spec axp192_adc_channels[] = {
189 AXP20X_ADC_CHANNEL(AXP192_ACIN_V, "acin_v", IIO_VOLTAGE,
190 AXP20X_ACIN_V_ADC_H),
191 AXP20X_ADC_CHANNEL(AXP192_ACIN_I, "acin_i", IIO_CURRENT,
192 AXP20X_ACIN_I_ADC_H),
193 AXP20X_ADC_CHANNEL(AXP192_VBUS_V, "vbus_v", IIO_VOLTAGE,
194 AXP20X_VBUS_V_ADC_H),
195 AXP20X_ADC_CHANNEL(AXP192_VBUS_I, "vbus_i", IIO_CURRENT,
196 AXP20X_VBUS_I_ADC_H),
199 .address = AXP20X_TEMP_ADC_H,
200 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
201 BIT(IIO_CHAN_INFO_SCALE) |
202 BIT(IIO_CHAN_INFO_OFFSET),
203 .datasheet_name = "pmic_temp",
205 AXP20X_ADC_CHANNEL_OFFSET(AXP192_GPIO0_V, "gpio0_v", IIO_VOLTAGE,
206 AXP20X_GPIO0_V_ADC_H),
207 AXP20X_ADC_CHANNEL_OFFSET(AXP192_GPIO1_V, "gpio1_v", IIO_VOLTAGE,
208 AXP20X_GPIO1_V_ADC_H),
209 AXP20X_ADC_CHANNEL_OFFSET(AXP192_GPIO2_V, "gpio2_v", IIO_VOLTAGE,
210 AXP192_GPIO2_V_ADC_H),
211 AXP20X_ADC_CHANNEL_OFFSET(AXP192_GPIO3_V, "gpio3_v", IIO_VOLTAGE,
212 AXP192_GPIO3_V_ADC_H),
213 AXP20X_ADC_CHANNEL(AXP192_IPSOUT_V, "ipsout_v", IIO_VOLTAGE,
214 AXP20X_IPSOUT_V_HIGH_H),
215 AXP20X_ADC_CHANNEL(AXP192_BATT_V, "batt_v", IIO_VOLTAGE,
217 AXP20X_ADC_CHANNEL(AXP192_BATT_CHRG_I, "batt_chrg_i", IIO_CURRENT,
218 AXP20X_BATT_CHRG_I_H),
219 AXP20X_ADC_CHANNEL(AXP192_BATT_DISCHRG_I, "batt_dischrg_i", IIO_CURRENT,
220 AXP20X_BATT_DISCHRG_I_H),
221 AXP20X_ADC_CHANNEL(AXP192_TS_IN, "ts_v", IIO_VOLTAGE,
225 static const struct iio_chan_spec axp20x_adc_channels[] = {
226 AXP20X_ADC_CHANNEL(AXP20X_ACIN_V, "acin_v", IIO_VOLTAGE,
227 AXP20X_ACIN_V_ADC_H),
228 AXP20X_ADC_CHANNEL(AXP20X_ACIN_I, "acin_i", IIO_CURRENT,
229 AXP20X_ACIN_I_ADC_H),
230 AXP20X_ADC_CHANNEL(AXP20X_VBUS_V, "vbus_v", IIO_VOLTAGE,
231 AXP20X_VBUS_V_ADC_H),
232 AXP20X_ADC_CHANNEL(AXP20X_VBUS_I, "vbus_i", IIO_CURRENT,
233 AXP20X_VBUS_I_ADC_H),
236 .address = AXP20X_TEMP_ADC_H,
237 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
238 BIT(IIO_CHAN_INFO_SCALE) |
239 BIT(IIO_CHAN_INFO_OFFSET),
240 .datasheet_name = "pmic_temp",
242 AXP20X_ADC_CHANNEL_OFFSET(AXP20X_GPIO0_V, "gpio0_v", IIO_VOLTAGE,
243 AXP20X_GPIO0_V_ADC_H),
244 AXP20X_ADC_CHANNEL_OFFSET(AXP20X_GPIO1_V, "gpio1_v", IIO_VOLTAGE,
245 AXP20X_GPIO1_V_ADC_H),
246 AXP20X_ADC_CHANNEL(AXP20X_IPSOUT_V, "ipsout_v", IIO_VOLTAGE,
247 AXP20X_IPSOUT_V_HIGH_H),
248 AXP20X_ADC_CHANNEL(AXP20X_BATT_V, "batt_v", IIO_VOLTAGE,
250 AXP20X_ADC_CHANNEL(AXP20X_BATT_CHRG_I, "batt_chrg_i", IIO_CURRENT,
251 AXP20X_BATT_CHRG_I_H),
252 AXP20X_ADC_CHANNEL(AXP20X_BATT_DISCHRG_I, "batt_dischrg_i", IIO_CURRENT,
253 AXP20X_BATT_DISCHRG_I_H),
254 AXP20X_ADC_CHANNEL(AXP20X_TS_IN, "ts_v", IIO_VOLTAGE,
258 static const struct iio_chan_spec axp22x_adc_channels[] = {
261 .address = AXP22X_PMIC_TEMP_H,
262 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
263 BIT(IIO_CHAN_INFO_SCALE) |
264 BIT(IIO_CHAN_INFO_OFFSET),
265 .datasheet_name = "pmic_temp",
267 AXP20X_ADC_CHANNEL(AXP22X_BATT_V, "batt_v", IIO_VOLTAGE,
269 AXP20X_ADC_CHANNEL(AXP22X_BATT_CHRG_I, "batt_chrg_i", IIO_CURRENT,
270 AXP20X_BATT_CHRG_I_H),
271 AXP20X_ADC_CHANNEL(AXP22X_BATT_DISCHRG_I, "batt_dischrg_i", IIO_CURRENT,
272 AXP20X_BATT_DISCHRG_I_H),
273 AXP20X_ADC_CHANNEL(AXP22X_TS_IN, "ts_v", IIO_VOLTAGE,
277 static const struct iio_chan_spec axp813_adc_channels[] = {
280 .address = AXP22X_PMIC_TEMP_H,
281 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
282 BIT(IIO_CHAN_INFO_SCALE) |
283 BIT(IIO_CHAN_INFO_OFFSET),
284 .datasheet_name = "pmic_temp",
286 AXP20X_ADC_CHANNEL(AXP813_GPIO0_V, "gpio0_v", IIO_VOLTAGE,
288 AXP20X_ADC_CHANNEL(AXP813_BATT_V, "batt_v", IIO_VOLTAGE,
290 AXP20X_ADC_CHANNEL(AXP22X_BATT_CHRG_I, "batt_chrg_i", IIO_CURRENT,
291 AXP20X_BATT_CHRG_I_H),
292 AXP20X_ADC_CHANNEL(AXP22X_BATT_DISCHRG_I, "batt_dischrg_i", IIO_CURRENT,
293 AXP20X_BATT_DISCHRG_I_H),
294 AXP20X_ADC_CHANNEL(AXP813_TS_IN, "ts_v", IIO_VOLTAGE,
298 static int axp192_adc_raw(struct iio_dev *indio_dev,
299 struct iio_chan_spec const *chan, int *val)
301 struct axp20x_adc_iio *info = iio_priv(indio_dev);
304 if (chan->type == IIO_CURRENT &&
305 (chan->channel == AXP192_BATT_CHRG_I ||
306 chan->channel == AXP192_BATT_DISCHRG_I))
311 ret = axp20x_read_variable_width(info->regmap, chan->address, size);
319 static int axp20x_adc_raw(struct iio_dev *indio_dev,
320 struct iio_chan_spec const *chan, int *val)
322 struct axp20x_adc_iio *info = iio_priv(indio_dev);
326 * N.B.: Unlike the Chinese datasheets tell, the charging current is
327 * stored on 12 bits, not 13 bits. Only discharging current is on 13
330 if (chan->type == IIO_CURRENT && chan->channel == AXP20X_BATT_DISCHRG_I)
335 ret = axp20x_read_variable_width(info->regmap, chan->address, size);
343 static int axp22x_adc_raw(struct iio_dev *indio_dev,
344 struct iio_chan_spec const *chan, int *val)
346 struct axp20x_adc_iio *info = iio_priv(indio_dev);
349 ret = axp20x_read_variable_width(info->regmap, chan->address, 12);
357 static int axp813_adc_raw(struct iio_dev *indio_dev,
358 struct iio_chan_spec const *chan, int *val)
360 struct axp20x_adc_iio *info = iio_priv(indio_dev);
363 ret = axp20x_read_variable_width(info->regmap, chan->address, 12);
371 static int axp192_adc_scale_voltage(int channel, int *val, int *val2)
378 return IIO_VAL_INT_PLUS_MICRO;
386 return IIO_VAL_INT_PLUS_MICRO;
391 return IIO_VAL_INT_PLUS_MICRO;
393 case AXP192_IPSOUT_V:
396 return IIO_VAL_INT_PLUS_MICRO;
402 return IIO_VAL_INT_PLUS_MICRO;
409 static int axp20x_adc_scale_voltage(int channel, int *val, int *val2)
416 return IIO_VAL_INT_PLUS_MICRO;
422 return IIO_VAL_INT_PLUS_MICRO;
427 return IIO_VAL_INT_PLUS_MICRO;
429 case AXP20X_IPSOUT_V:
432 return IIO_VAL_INT_PLUS_MICRO;
438 return IIO_VAL_INT_PLUS_MICRO;
445 static int axp22x_adc_scale_voltage(int channel, int *val, int *val2)
452 return IIO_VAL_INT_PLUS_MICRO;
458 return IIO_VAL_INT_PLUS_MICRO;
464 static int axp813_adc_scale_voltage(int channel, int *val, int *val2)
470 return IIO_VAL_INT_PLUS_MICRO;
475 return IIO_VAL_INT_PLUS_MICRO;
481 return IIO_VAL_INT_PLUS_MICRO;
488 static int axp20x_adc_scale_current(int channel, int *val, int *val2)
494 return IIO_VAL_INT_PLUS_MICRO;
499 return IIO_VAL_INT_PLUS_MICRO;
501 case AXP20X_BATT_DISCHRG_I:
502 case AXP20X_BATT_CHRG_I:
505 return IIO_VAL_INT_PLUS_MICRO;
512 static int axp192_adc_scale(struct iio_chan_spec const *chan, int *val,
515 switch (chan->type) {
517 return axp192_adc_scale_voltage(chan->channel, val, val2);
521 * AXP192 current channels are identical to the AXP20x,
522 * therefore we can re-use the scaling function.
524 return axp20x_adc_scale_current(chan->channel, val, val2);
535 static int axp20x_adc_scale(struct iio_chan_spec const *chan, int *val,
538 switch (chan->type) {
540 return axp20x_adc_scale_voltage(chan->channel, val, val2);
543 return axp20x_adc_scale_current(chan->channel, val, val2);
554 static int axp22x_adc_scale(struct iio_chan_spec const *chan, int *val,
557 switch (chan->type) {
559 return axp22x_adc_scale_voltage(chan->channel, val, val2);
574 static int axp813_adc_scale(struct iio_chan_spec const *chan, int *val,
577 switch (chan->type) {
579 return axp813_adc_scale_voltage(chan->channel, val, val2);
594 static int axp192_adc_offset_voltage(struct iio_dev *indio_dev, int channel,
597 struct axp20x_adc_iio *info = iio_priv(indio_dev);
601 ret = regmap_read(info->regmap, AXP192_GPIO30_IN_RANGE, ®val);
607 regval = FIELD_GET(AXP192_GPIO30_IN_RANGE_GPIO0, regval);
611 regval = FIELD_GET(AXP192_GPIO30_IN_RANGE_GPIO1, regval);
615 regval = FIELD_GET(AXP192_GPIO30_IN_RANGE_GPIO2, regval);
619 regval = FIELD_GET(AXP192_GPIO30_IN_RANGE_GPIO3, regval);
626 *val = regval ? 700000 : 0;
630 static int axp20x_adc_offset_voltage(struct iio_dev *indio_dev, int channel,
633 struct axp20x_adc_iio *info = iio_priv(indio_dev);
637 ret = regmap_read(info->regmap, AXP20X_GPIO10_IN_RANGE, ®val);
643 regval = FIELD_GET(AXP20X_GPIO10_IN_RANGE_GPIO0, regval);
647 regval = FIELD_GET(AXP20X_GPIO10_IN_RANGE_GPIO1, regval);
654 *val = regval ? 700000 : 0;
658 static int axp192_adc_offset(struct iio_dev *indio_dev,
659 struct iio_chan_spec const *chan, int *val)
661 switch (chan->type) {
663 return axp192_adc_offset_voltage(indio_dev, chan->channel, val);
674 static int axp20x_adc_offset(struct iio_dev *indio_dev,
675 struct iio_chan_spec const *chan, int *val)
677 switch (chan->type) {
679 return axp20x_adc_offset_voltage(indio_dev, chan->channel, val);
690 static int axp192_read_raw(struct iio_dev *indio_dev,
691 struct iio_chan_spec const *chan, int *val,
692 int *val2, long mask)
695 case IIO_CHAN_INFO_OFFSET:
696 return axp192_adc_offset(indio_dev, chan, val);
698 case IIO_CHAN_INFO_SCALE:
699 return axp192_adc_scale(chan, val, val2);
701 case IIO_CHAN_INFO_RAW:
702 return axp192_adc_raw(indio_dev, chan, val);
709 static int axp20x_read_raw(struct iio_dev *indio_dev,
710 struct iio_chan_spec const *chan, int *val,
711 int *val2, long mask)
714 case IIO_CHAN_INFO_OFFSET:
715 return axp20x_adc_offset(indio_dev, chan, val);
717 case IIO_CHAN_INFO_SCALE:
718 return axp20x_adc_scale(chan, val, val2);
720 case IIO_CHAN_INFO_RAW:
721 return axp20x_adc_raw(indio_dev, chan, val);
728 static int axp22x_read_raw(struct iio_dev *indio_dev,
729 struct iio_chan_spec const *chan, int *val,
730 int *val2, long mask)
733 case IIO_CHAN_INFO_OFFSET:
734 /* For PMIC temp only */
738 case IIO_CHAN_INFO_SCALE:
739 return axp22x_adc_scale(chan, val, val2);
741 case IIO_CHAN_INFO_RAW:
742 return axp22x_adc_raw(indio_dev, chan, val);
749 static int axp813_read_raw(struct iio_dev *indio_dev,
750 struct iio_chan_spec const *chan, int *val,
751 int *val2, long mask)
754 case IIO_CHAN_INFO_OFFSET:
758 case IIO_CHAN_INFO_SCALE:
759 return axp813_adc_scale(chan, val, val2);
761 case IIO_CHAN_INFO_RAW:
762 return axp813_adc_raw(indio_dev, chan, val);
769 static int axp192_write_raw(struct iio_dev *indio_dev,
770 struct iio_chan_spec const *chan, int val, int val2,
773 struct axp20x_adc_iio *info = iio_priv(indio_dev);
774 unsigned int regmask, regval;
777 * The AXP192 PMIC allows the user to choose between 0V and 0.7V offsets
778 * for (independently) GPIO0-3 when in ADC mode.
780 if (mask != IIO_CHAN_INFO_OFFSET)
783 if (val != 0 && val != 700000)
786 switch (chan->channel) {
788 regmask = AXP192_GPIO30_IN_RANGE_GPIO0;
789 regval = FIELD_PREP(AXP192_GPIO30_IN_RANGE_GPIO0, !!val);
793 regmask = AXP192_GPIO30_IN_RANGE_GPIO1;
794 regval = FIELD_PREP(AXP192_GPIO30_IN_RANGE_GPIO1, !!val);
798 regmask = AXP192_GPIO30_IN_RANGE_GPIO2;
799 regval = FIELD_PREP(AXP192_GPIO30_IN_RANGE_GPIO2, !!val);
803 regmask = AXP192_GPIO30_IN_RANGE_GPIO3;
804 regval = FIELD_PREP(AXP192_GPIO30_IN_RANGE_GPIO3, !!val);
811 return regmap_update_bits(info->regmap, AXP192_GPIO30_IN_RANGE, regmask, regval);
814 static int axp20x_write_raw(struct iio_dev *indio_dev,
815 struct iio_chan_spec const *chan, int val, int val2,
818 struct axp20x_adc_iio *info = iio_priv(indio_dev);
819 unsigned int regmask, regval;
822 * The AXP20X PMIC allows the user to choose between 0V and 0.7V offsets
823 * for (independently) GPIO0 and GPIO1 when in ADC mode.
825 if (mask != IIO_CHAN_INFO_OFFSET)
828 if (val != 0 && val != 700000)
831 switch (chan->channel) {
833 regmask = AXP20X_GPIO10_IN_RANGE_GPIO0;
834 regval = FIELD_PREP(AXP20X_GPIO10_IN_RANGE_GPIO0, !!val);
838 regmask = AXP20X_GPIO10_IN_RANGE_GPIO1;
839 regval = FIELD_PREP(AXP20X_GPIO10_IN_RANGE_GPIO1, !!val);
846 return regmap_update_bits(info->regmap, AXP20X_GPIO10_IN_RANGE, regmask, regval);
849 static const struct iio_info axp192_adc_iio_info = {
850 .read_raw = axp192_read_raw,
851 .write_raw = axp192_write_raw,
854 static const struct iio_info axp20x_adc_iio_info = {
855 .read_raw = axp20x_read_raw,
856 .write_raw = axp20x_write_raw,
859 static const struct iio_info axp22x_adc_iio_info = {
860 .read_raw = axp22x_read_raw,
863 static const struct iio_info axp813_adc_iio_info = {
864 .read_raw = axp813_read_raw,
867 static int axp20x_adc_rate(struct axp20x_adc_iio *info, int rate)
869 return regmap_update_bits(info->regmap, AXP20X_ADC_RATE,
870 AXP20X_ADC_RATE_MASK,
871 AXP20X_ADC_RATE_HZ(rate));
874 static int axp22x_adc_rate(struct axp20x_adc_iio *info, int rate)
876 return regmap_update_bits(info->regmap, AXP20X_ADC_RATE,
877 AXP20X_ADC_RATE_MASK,
878 AXP22X_ADC_RATE_HZ(rate));
881 static int axp813_adc_rate(struct axp20x_adc_iio *info, int rate)
883 return regmap_update_bits(info->regmap, AXP813_ADC_RATE,
884 AXP813_ADC_RATE_MASK,
885 AXP813_ADC_RATE_HZ(rate));
889 const struct iio_info *iio_info;
891 struct iio_chan_spec const *channels;
892 unsigned long adc_en1_mask;
893 unsigned long adc_en2_mask;
894 int (*adc_rate)(struct axp20x_adc_iio *info,
896 struct iio_map *maps;
899 static const struct axp_data axp192_data = {
900 .iio_info = &axp192_adc_iio_info,
901 .num_channels = ARRAY_SIZE(axp192_adc_channels),
902 .channels = axp192_adc_channels,
903 .adc_en1_mask = AXP192_ADC_EN1_MASK,
904 .adc_en2_mask = AXP192_ADC_EN2_MASK,
905 .adc_rate = axp20x_adc_rate,
909 static const struct axp_data axp20x_data = {
910 .iio_info = &axp20x_adc_iio_info,
911 .num_channels = ARRAY_SIZE(axp20x_adc_channels),
912 .channels = axp20x_adc_channels,
913 .adc_en1_mask = AXP20X_ADC_EN1_MASK,
914 .adc_en2_mask = AXP20X_ADC_EN2_MASK,
915 .adc_rate = axp20x_adc_rate,
919 static const struct axp_data axp22x_data = {
920 .iio_info = &axp22x_adc_iio_info,
921 .num_channels = ARRAY_SIZE(axp22x_adc_channels),
922 .channels = axp22x_adc_channels,
923 .adc_en1_mask = AXP22X_ADC_EN1_MASK,
924 .adc_rate = axp22x_adc_rate,
928 static const struct axp_data axp813_data = {
929 .iio_info = &axp813_adc_iio_info,
930 .num_channels = ARRAY_SIZE(axp813_adc_channels),
931 .channels = axp813_adc_channels,
932 .adc_en1_mask = AXP22X_ADC_EN1_MASK,
933 .adc_rate = axp813_adc_rate,
937 static const struct of_device_id axp20x_adc_of_match[] = {
938 { .compatible = "x-powers,axp192-adc", .data = (void *)&axp192_data, },
939 { .compatible = "x-powers,axp209-adc", .data = (void *)&axp20x_data, },
940 { .compatible = "x-powers,axp221-adc", .data = (void *)&axp22x_data, },
941 { .compatible = "x-powers,axp813-adc", .data = (void *)&axp813_data, },
944 MODULE_DEVICE_TABLE(of, axp20x_adc_of_match);
946 static const struct platform_device_id axp20x_adc_id_match[] = {
947 { .name = "axp192-adc", .driver_data = (kernel_ulong_t)&axp192_data, },
948 { .name = "axp20x-adc", .driver_data = (kernel_ulong_t)&axp20x_data, },
949 { .name = "axp22x-adc", .driver_data = (kernel_ulong_t)&axp22x_data, },
950 { .name = "axp813-adc", .driver_data = (kernel_ulong_t)&axp813_data, },
953 MODULE_DEVICE_TABLE(platform, axp20x_adc_id_match);
955 static int axp20x_probe(struct platform_device *pdev)
957 struct axp20x_adc_iio *info;
958 struct iio_dev *indio_dev;
959 struct axp20x_dev *axp20x_dev;
962 axp20x_dev = dev_get_drvdata(pdev->dev.parent);
964 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
968 info = iio_priv(indio_dev);
969 platform_set_drvdata(pdev, indio_dev);
971 info->regmap = axp20x_dev->regmap;
972 indio_dev->modes = INDIO_DIRECT_MODE;
974 if (!dev_fwnode(&pdev->dev)) {
975 const struct platform_device_id *id;
977 id = platform_get_device_id(pdev);
978 info->data = (const struct axp_data *)id->driver_data;
980 struct device *dev = &pdev->dev;
982 info->data = device_get_match_data(dev);
985 indio_dev->name = platform_get_device_id(pdev)->name;
986 indio_dev->info = info->data->iio_info;
987 indio_dev->num_channels = info->data->num_channels;
988 indio_dev->channels = info->data->channels;
990 /* Enable the ADCs on IP */
991 regmap_write(info->regmap, AXP20X_ADC_EN1, info->data->adc_en1_mask);
993 if (info->data->adc_en2_mask)
994 regmap_set_bits(info->regmap, AXP20X_ADC_EN2,
995 info->data->adc_en2_mask);
997 /* Configure ADCs rate */
998 info->data->adc_rate(info, 100);
1000 ret = iio_map_array_register(indio_dev, info->data->maps);
1002 dev_err(&pdev->dev, "failed to register IIO maps: %d\n", ret);
1006 ret = iio_device_register(indio_dev);
1008 dev_err(&pdev->dev, "could not register the device\n");
1015 iio_map_array_unregister(indio_dev);
1018 regmap_write(info->regmap, AXP20X_ADC_EN1, 0);
1020 if (info->data->adc_en2_mask)
1021 regmap_write(info->regmap, AXP20X_ADC_EN2, 0);
1026 static void axp20x_remove(struct platform_device *pdev)
1028 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
1029 struct axp20x_adc_iio *info = iio_priv(indio_dev);
1031 iio_device_unregister(indio_dev);
1032 iio_map_array_unregister(indio_dev);
1034 regmap_write(info->regmap, AXP20X_ADC_EN1, 0);
1036 if (info->data->adc_en2_mask)
1037 regmap_write(info->regmap, AXP20X_ADC_EN2, 0);
1040 static struct platform_driver axp20x_adc_driver = {
1042 .name = "axp20x-adc",
1043 .of_match_table = axp20x_adc_of_match,
1045 .id_table = axp20x_adc_id_match,
1046 .probe = axp20x_probe,
1047 .remove_new = axp20x_remove,
1050 module_platform_driver(axp20x_adc_driver);
1052 MODULE_DESCRIPTION("ADC driver for AXP20X and AXP22X PMICs");
1054 MODULE_LICENSE("GPL");