WIP FPC-III support
[linux/fpc-iii.git] / drivers / iio / adc / axp20x_adc.c
blob3e0c0233b43156c899398c34b240001097d40058
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>
6 */
8 #include <linux/completion.h>
9 #include <linux/interrupt.h>
10 #include <linux/io.h>
11 #include <linux/module.h>
12 #include <linux/mod_devicetable.h>
13 #include <linux/platform_device.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/property.h>
16 #include <linux/regmap.h>
17 #include <linux/thermal.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/driver.h>
21 #include <linux/iio/machine.h>
22 #include <linux/mfd/axp20x.h>
24 #define AXP20X_ADC_EN1_MASK GENMASK(7, 0)
26 #define AXP20X_ADC_EN2_MASK (GENMASK(3, 2) | BIT(7))
27 #define AXP22X_ADC_EN1_MASK (GENMASK(7, 5) | BIT(0))
29 #define AXP20X_GPIO10_IN_RANGE_GPIO0 BIT(0)
30 #define AXP20X_GPIO10_IN_RANGE_GPIO1 BIT(1)
31 #define AXP20X_GPIO10_IN_RANGE_GPIO0_VAL(x) ((x) & BIT(0))
32 #define AXP20X_GPIO10_IN_RANGE_GPIO1_VAL(x) (((x) & BIT(0)) << 1)
34 #define AXP20X_ADC_RATE_MASK GENMASK(7, 6)
35 #define AXP813_V_I_ADC_RATE_MASK GENMASK(5, 4)
36 #define AXP813_ADC_RATE_MASK (AXP20X_ADC_RATE_MASK | AXP813_V_I_ADC_RATE_MASK)
37 #define AXP20X_ADC_RATE_HZ(x) ((ilog2((x) / 25) << 6) & AXP20X_ADC_RATE_MASK)
38 #define AXP22X_ADC_RATE_HZ(x) ((ilog2((x) / 100) << 6) & AXP20X_ADC_RATE_MASK)
39 #define AXP813_TS_GPIO0_ADC_RATE_HZ(x) AXP20X_ADC_RATE_HZ(x)
40 #define AXP813_V_I_ADC_RATE_HZ(x) ((ilog2((x) / 100) << 4) & AXP813_V_I_ADC_RATE_MASK)
41 #define AXP813_ADC_RATE_HZ(x) (AXP20X_ADC_RATE_HZ(x) | AXP813_V_I_ADC_RATE_HZ(x))
43 #define AXP20X_ADC_CHANNEL(_channel, _name, _type, _reg) \
44 { \
45 .type = _type, \
46 .indexed = 1, \
47 .channel = _channel, \
48 .address = _reg, \
49 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
50 BIT(IIO_CHAN_INFO_SCALE), \
51 .datasheet_name = _name, \
54 #define AXP20X_ADC_CHANNEL_OFFSET(_channel, _name, _type, _reg) \
55 { \
56 .type = _type, \
57 .indexed = 1, \
58 .channel = _channel, \
59 .address = _reg, \
60 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
61 BIT(IIO_CHAN_INFO_SCALE) |\
62 BIT(IIO_CHAN_INFO_OFFSET),\
63 .datasheet_name = _name, \
66 struct axp_data;
68 struct axp20x_adc_iio {
69 struct regmap *regmap;
70 const struct axp_data *data;
73 enum axp20x_adc_channel_v {
74 AXP20X_ACIN_V = 0,
75 AXP20X_VBUS_V,
76 AXP20X_TS_IN,
77 AXP20X_GPIO0_V,
78 AXP20X_GPIO1_V,
79 AXP20X_IPSOUT_V,
80 AXP20X_BATT_V,
83 enum axp20x_adc_channel_i {
84 AXP20X_ACIN_I = 0,
85 AXP20X_VBUS_I,
86 AXP20X_BATT_CHRG_I,
87 AXP20X_BATT_DISCHRG_I,
90 enum axp22x_adc_channel_v {
91 AXP22X_TS_IN = 0,
92 AXP22X_BATT_V,
95 enum axp22x_adc_channel_i {
96 AXP22X_BATT_CHRG_I = 1,
97 AXP22X_BATT_DISCHRG_I,
100 enum axp813_adc_channel_v {
101 AXP813_TS_IN = 0,
102 AXP813_GPIO0_V,
103 AXP813_BATT_V,
106 static struct iio_map axp20x_maps[] = {
108 .consumer_dev_name = "axp20x-usb-power-supply",
109 .consumer_channel = "vbus_v",
110 .adc_channel_label = "vbus_v",
111 }, {
112 .consumer_dev_name = "axp20x-usb-power-supply",
113 .consumer_channel = "vbus_i",
114 .adc_channel_label = "vbus_i",
115 }, {
116 .consumer_dev_name = "axp20x-ac-power-supply",
117 .consumer_channel = "acin_v",
118 .adc_channel_label = "acin_v",
119 }, {
120 .consumer_dev_name = "axp20x-ac-power-supply",
121 .consumer_channel = "acin_i",
122 .adc_channel_label = "acin_i",
123 }, {
124 .consumer_dev_name = "axp20x-battery-power-supply",
125 .consumer_channel = "batt_v",
126 .adc_channel_label = "batt_v",
127 }, {
128 .consumer_dev_name = "axp20x-battery-power-supply",
129 .consumer_channel = "batt_chrg_i",
130 .adc_channel_label = "batt_chrg_i",
131 }, {
132 .consumer_dev_name = "axp20x-battery-power-supply",
133 .consumer_channel = "batt_dischrg_i",
134 .adc_channel_label = "batt_dischrg_i",
135 }, { /* sentinel */ }
138 static struct iio_map axp22x_maps[] = {
140 .consumer_dev_name = "axp20x-battery-power-supply",
141 .consumer_channel = "batt_v",
142 .adc_channel_label = "batt_v",
143 }, {
144 .consumer_dev_name = "axp20x-battery-power-supply",
145 .consumer_channel = "batt_chrg_i",
146 .adc_channel_label = "batt_chrg_i",
147 }, {
148 .consumer_dev_name = "axp20x-battery-power-supply",
149 .consumer_channel = "batt_dischrg_i",
150 .adc_channel_label = "batt_dischrg_i",
151 }, { /* sentinel */ }
155 * Channels are mapped by physical system. Their channels share the same index.
156 * i.e. acin_i is in_current0_raw and acin_v is in_voltage0_raw.
157 * The only exception is for the battery. batt_v will be in_voltage6_raw and
158 * charge current in_current6_raw and discharge current will be in_current7_raw.
160 static const struct iio_chan_spec axp20x_adc_channels[] = {
161 AXP20X_ADC_CHANNEL(AXP20X_ACIN_V, "acin_v", IIO_VOLTAGE,
162 AXP20X_ACIN_V_ADC_H),
163 AXP20X_ADC_CHANNEL(AXP20X_ACIN_I, "acin_i", IIO_CURRENT,
164 AXP20X_ACIN_I_ADC_H),
165 AXP20X_ADC_CHANNEL(AXP20X_VBUS_V, "vbus_v", IIO_VOLTAGE,
166 AXP20X_VBUS_V_ADC_H),
167 AXP20X_ADC_CHANNEL(AXP20X_VBUS_I, "vbus_i", IIO_CURRENT,
168 AXP20X_VBUS_I_ADC_H),
170 .type = IIO_TEMP,
171 .address = AXP20X_TEMP_ADC_H,
172 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
173 BIT(IIO_CHAN_INFO_SCALE) |
174 BIT(IIO_CHAN_INFO_OFFSET),
175 .datasheet_name = "pmic_temp",
177 AXP20X_ADC_CHANNEL_OFFSET(AXP20X_GPIO0_V, "gpio0_v", IIO_VOLTAGE,
178 AXP20X_GPIO0_V_ADC_H),
179 AXP20X_ADC_CHANNEL_OFFSET(AXP20X_GPIO1_V, "gpio1_v", IIO_VOLTAGE,
180 AXP20X_GPIO1_V_ADC_H),
181 AXP20X_ADC_CHANNEL(AXP20X_IPSOUT_V, "ipsout_v", IIO_VOLTAGE,
182 AXP20X_IPSOUT_V_HIGH_H),
183 AXP20X_ADC_CHANNEL(AXP20X_BATT_V, "batt_v", IIO_VOLTAGE,
184 AXP20X_BATT_V_H),
185 AXP20X_ADC_CHANNEL(AXP20X_BATT_CHRG_I, "batt_chrg_i", IIO_CURRENT,
186 AXP20X_BATT_CHRG_I_H),
187 AXP20X_ADC_CHANNEL(AXP20X_BATT_DISCHRG_I, "batt_dischrg_i", IIO_CURRENT,
188 AXP20X_BATT_DISCHRG_I_H),
191 static const struct iio_chan_spec axp22x_adc_channels[] = {
193 .type = IIO_TEMP,
194 .address = AXP22X_PMIC_TEMP_H,
195 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
196 BIT(IIO_CHAN_INFO_SCALE) |
197 BIT(IIO_CHAN_INFO_OFFSET),
198 .datasheet_name = "pmic_temp",
200 AXP20X_ADC_CHANNEL(AXP22X_BATT_V, "batt_v", IIO_VOLTAGE,
201 AXP20X_BATT_V_H),
202 AXP20X_ADC_CHANNEL(AXP22X_BATT_CHRG_I, "batt_chrg_i", IIO_CURRENT,
203 AXP20X_BATT_CHRG_I_H),
204 AXP20X_ADC_CHANNEL(AXP22X_BATT_DISCHRG_I, "batt_dischrg_i", IIO_CURRENT,
205 AXP20X_BATT_DISCHRG_I_H),
208 static const struct iio_chan_spec axp813_adc_channels[] = {
210 .type = IIO_TEMP,
211 .address = AXP22X_PMIC_TEMP_H,
212 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
213 BIT(IIO_CHAN_INFO_SCALE) |
214 BIT(IIO_CHAN_INFO_OFFSET),
215 .datasheet_name = "pmic_temp",
217 AXP20X_ADC_CHANNEL(AXP813_GPIO0_V, "gpio0_v", IIO_VOLTAGE,
218 AXP288_GP_ADC_H),
219 AXP20X_ADC_CHANNEL(AXP813_BATT_V, "batt_v", IIO_VOLTAGE,
220 AXP20X_BATT_V_H),
221 AXP20X_ADC_CHANNEL(AXP22X_BATT_CHRG_I, "batt_chrg_i", IIO_CURRENT,
222 AXP20X_BATT_CHRG_I_H),
223 AXP20X_ADC_CHANNEL(AXP22X_BATT_DISCHRG_I, "batt_dischrg_i", IIO_CURRENT,
224 AXP20X_BATT_DISCHRG_I_H),
227 static int axp20x_adc_raw(struct iio_dev *indio_dev,
228 struct iio_chan_spec const *chan, int *val)
230 struct axp20x_adc_iio *info = iio_priv(indio_dev);
231 int size = 12;
234 * N.B.: Unlike the Chinese datasheets tell, the charging current is
235 * stored on 12 bits, not 13 bits. Only discharging current is on 13
236 * bits.
238 if (chan->type == IIO_CURRENT && chan->channel == AXP20X_BATT_DISCHRG_I)
239 size = 13;
240 else
241 size = 12;
243 *val = axp20x_read_variable_width(info->regmap, chan->address, size);
244 if (*val < 0)
245 return *val;
247 return IIO_VAL_INT;
250 static int axp22x_adc_raw(struct iio_dev *indio_dev,
251 struct iio_chan_spec const *chan, int *val)
253 struct axp20x_adc_iio *info = iio_priv(indio_dev);
254 int size;
257 * N.B.: Unlike the Chinese datasheets tell, the charging current is
258 * stored on 12 bits, not 13 bits. Only discharging current is on 13
259 * bits.
261 if (chan->type == IIO_CURRENT && chan->channel == AXP22X_BATT_DISCHRG_I)
262 size = 13;
263 else
264 size = 12;
266 *val = axp20x_read_variable_width(info->regmap, chan->address, size);
267 if (*val < 0)
268 return *val;
270 return IIO_VAL_INT;
273 static int axp813_adc_raw(struct iio_dev *indio_dev,
274 struct iio_chan_spec const *chan, int *val)
276 struct axp20x_adc_iio *info = iio_priv(indio_dev);
278 *val = axp20x_read_variable_width(info->regmap, chan->address, 12);
279 if (*val < 0)
280 return *val;
282 return IIO_VAL_INT;
285 static int axp20x_adc_scale_voltage(int channel, int *val, int *val2)
287 switch (channel) {
288 case AXP20X_ACIN_V:
289 case AXP20X_VBUS_V:
290 *val = 1;
291 *val2 = 700000;
292 return IIO_VAL_INT_PLUS_MICRO;
294 case AXP20X_GPIO0_V:
295 case AXP20X_GPIO1_V:
296 *val = 0;
297 *val2 = 500000;
298 return IIO_VAL_INT_PLUS_MICRO;
300 case AXP20X_BATT_V:
301 *val = 1;
302 *val2 = 100000;
303 return IIO_VAL_INT_PLUS_MICRO;
305 case AXP20X_IPSOUT_V:
306 *val = 1;
307 *val2 = 400000;
308 return IIO_VAL_INT_PLUS_MICRO;
310 default:
311 return -EINVAL;
315 static int axp813_adc_scale_voltage(int channel, int *val, int *val2)
317 switch (channel) {
318 case AXP813_GPIO0_V:
319 *val = 0;
320 *val2 = 800000;
321 return IIO_VAL_INT_PLUS_MICRO;
323 case AXP813_BATT_V:
324 *val = 1;
325 *val2 = 100000;
326 return IIO_VAL_INT_PLUS_MICRO;
328 default:
329 return -EINVAL;
333 static int axp20x_adc_scale_current(int channel, int *val, int *val2)
335 switch (channel) {
336 case AXP20X_ACIN_I:
337 *val = 0;
338 *val2 = 625000;
339 return IIO_VAL_INT_PLUS_MICRO;
341 case AXP20X_VBUS_I:
342 *val = 0;
343 *val2 = 375000;
344 return IIO_VAL_INT_PLUS_MICRO;
346 case AXP20X_BATT_DISCHRG_I:
347 case AXP20X_BATT_CHRG_I:
348 *val = 0;
349 *val2 = 500000;
350 return IIO_VAL_INT_PLUS_MICRO;
352 default:
353 return -EINVAL;
357 static int axp20x_adc_scale(struct iio_chan_spec const *chan, int *val,
358 int *val2)
360 switch (chan->type) {
361 case IIO_VOLTAGE:
362 return axp20x_adc_scale_voltage(chan->channel, val, val2);
364 case IIO_CURRENT:
365 return axp20x_adc_scale_current(chan->channel, val, val2);
367 case IIO_TEMP:
368 *val = 100;
369 return IIO_VAL_INT;
371 default:
372 return -EINVAL;
376 static int axp22x_adc_scale(struct iio_chan_spec const *chan, int *val,
377 int *val2)
379 switch (chan->type) {
380 case IIO_VOLTAGE:
381 if (chan->channel != AXP22X_BATT_V)
382 return -EINVAL;
384 *val = 1;
385 *val2 = 100000;
386 return IIO_VAL_INT_PLUS_MICRO;
388 case IIO_CURRENT:
389 *val = 0;
390 *val2 = 500000;
391 return IIO_VAL_INT_PLUS_MICRO;
393 case IIO_TEMP:
394 *val = 100;
395 return IIO_VAL_INT;
397 default:
398 return -EINVAL;
402 static int axp813_adc_scale(struct iio_chan_spec const *chan, int *val,
403 int *val2)
405 switch (chan->type) {
406 case IIO_VOLTAGE:
407 return axp813_adc_scale_voltage(chan->channel, val, val2);
409 case IIO_CURRENT:
410 *val = 1;
411 return IIO_VAL_INT;
413 case IIO_TEMP:
414 *val = 100;
415 return IIO_VAL_INT;
417 default:
418 return -EINVAL;
422 static int axp20x_adc_offset_voltage(struct iio_dev *indio_dev, int channel,
423 int *val)
425 struct axp20x_adc_iio *info = iio_priv(indio_dev);
426 int ret;
428 ret = regmap_read(info->regmap, AXP20X_GPIO10_IN_RANGE, val);
429 if (ret < 0)
430 return ret;
432 switch (channel) {
433 case AXP20X_GPIO0_V:
434 *val &= AXP20X_GPIO10_IN_RANGE_GPIO0;
435 break;
437 case AXP20X_GPIO1_V:
438 *val &= AXP20X_GPIO10_IN_RANGE_GPIO1;
439 break;
441 default:
442 return -EINVAL;
445 *val = *val ? 700000 : 0;
447 return IIO_VAL_INT;
450 static int axp20x_adc_offset(struct iio_dev *indio_dev,
451 struct iio_chan_spec const *chan, int *val)
453 switch (chan->type) {
454 case IIO_VOLTAGE:
455 return axp20x_adc_offset_voltage(indio_dev, chan->channel, val);
457 case IIO_TEMP:
458 *val = -1447;
459 return IIO_VAL_INT;
461 default:
462 return -EINVAL;
466 static int axp20x_read_raw(struct iio_dev *indio_dev,
467 struct iio_chan_spec const *chan, int *val,
468 int *val2, long mask)
470 switch (mask) {
471 case IIO_CHAN_INFO_OFFSET:
472 return axp20x_adc_offset(indio_dev, chan, val);
474 case IIO_CHAN_INFO_SCALE:
475 return axp20x_adc_scale(chan, val, val2);
477 case IIO_CHAN_INFO_RAW:
478 return axp20x_adc_raw(indio_dev, chan, val);
480 default:
481 return -EINVAL;
485 static int axp22x_read_raw(struct iio_dev *indio_dev,
486 struct iio_chan_spec const *chan, int *val,
487 int *val2, long mask)
489 switch (mask) {
490 case IIO_CHAN_INFO_OFFSET:
491 *val = -2677;
492 return IIO_VAL_INT;
494 case IIO_CHAN_INFO_SCALE:
495 return axp22x_adc_scale(chan, val, val2);
497 case IIO_CHAN_INFO_RAW:
498 return axp22x_adc_raw(indio_dev, chan, val);
500 default:
501 return -EINVAL;
505 static int axp813_read_raw(struct iio_dev *indio_dev,
506 struct iio_chan_spec const *chan, int *val,
507 int *val2, long mask)
509 switch (mask) {
510 case IIO_CHAN_INFO_OFFSET:
511 *val = -2667;
512 return IIO_VAL_INT;
514 case IIO_CHAN_INFO_SCALE:
515 return axp813_adc_scale(chan, val, val2);
517 case IIO_CHAN_INFO_RAW:
518 return axp813_adc_raw(indio_dev, chan, val);
520 default:
521 return -EINVAL;
525 static int axp20x_write_raw(struct iio_dev *indio_dev,
526 struct iio_chan_spec const *chan, int val, int val2,
527 long mask)
529 struct axp20x_adc_iio *info = iio_priv(indio_dev);
530 unsigned int reg, regval;
533 * The AXP20X PMIC allows the user to choose between 0V and 0.7V offsets
534 * for (independently) GPIO0 and GPIO1 when in ADC mode.
536 if (mask != IIO_CHAN_INFO_OFFSET)
537 return -EINVAL;
539 if (val != 0 && val != 700000)
540 return -EINVAL;
542 val = val ? 1 : 0;
544 switch (chan->channel) {
545 case AXP20X_GPIO0_V:
546 reg = AXP20X_GPIO10_IN_RANGE_GPIO0;
547 regval = AXP20X_GPIO10_IN_RANGE_GPIO0_VAL(val);
548 break;
550 case AXP20X_GPIO1_V:
551 reg = AXP20X_GPIO10_IN_RANGE_GPIO1;
552 regval = AXP20X_GPIO10_IN_RANGE_GPIO1_VAL(val);
553 break;
555 default:
556 return -EINVAL;
559 return regmap_update_bits(info->regmap, AXP20X_GPIO10_IN_RANGE, reg,
560 regval);
563 static const struct iio_info axp20x_adc_iio_info = {
564 .read_raw = axp20x_read_raw,
565 .write_raw = axp20x_write_raw,
568 static const struct iio_info axp22x_adc_iio_info = {
569 .read_raw = axp22x_read_raw,
572 static const struct iio_info axp813_adc_iio_info = {
573 .read_raw = axp813_read_raw,
576 static int axp20x_adc_rate(struct axp20x_adc_iio *info, int rate)
578 return regmap_update_bits(info->regmap, AXP20X_ADC_RATE,
579 AXP20X_ADC_RATE_MASK,
580 AXP20X_ADC_RATE_HZ(rate));
583 static int axp22x_adc_rate(struct axp20x_adc_iio *info, int rate)
585 return regmap_update_bits(info->regmap, AXP20X_ADC_RATE,
586 AXP20X_ADC_RATE_MASK,
587 AXP22X_ADC_RATE_HZ(rate));
590 static int axp813_adc_rate(struct axp20x_adc_iio *info, int rate)
592 return regmap_update_bits(info->regmap, AXP813_ADC_RATE,
593 AXP813_ADC_RATE_MASK,
594 AXP813_ADC_RATE_HZ(rate));
597 struct axp_data {
598 const struct iio_info *iio_info;
599 int num_channels;
600 struct iio_chan_spec const *channels;
601 unsigned long adc_en1_mask;
602 int (*adc_rate)(struct axp20x_adc_iio *info,
603 int rate);
604 bool adc_en2;
605 struct iio_map *maps;
608 static const struct axp_data axp20x_data = {
609 .iio_info = &axp20x_adc_iio_info,
610 .num_channels = ARRAY_SIZE(axp20x_adc_channels),
611 .channels = axp20x_adc_channels,
612 .adc_en1_mask = AXP20X_ADC_EN1_MASK,
613 .adc_rate = axp20x_adc_rate,
614 .adc_en2 = true,
615 .maps = axp20x_maps,
618 static const struct axp_data axp22x_data = {
619 .iio_info = &axp22x_adc_iio_info,
620 .num_channels = ARRAY_SIZE(axp22x_adc_channels),
621 .channels = axp22x_adc_channels,
622 .adc_en1_mask = AXP22X_ADC_EN1_MASK,
623 .adc_rate = axp22x_adc_rate,
624 .adc_en2 = false,
625 .maps = axp22x_maps,
628 static const struct axp_data axp813_data = {
629 .iio_info = &axp813_adc_iio_info,
630 .num_channels = ARRAY_SIZE(axp813_adc_channels),
631 .channels = axp813_adc_channels,
632 .adc_en1_mask = AXP22X_ADC_EN1_MASK,
633 .adc_rate = axp813_adc_rate,
634 .adc_en2 = false,
635 .maps = axp22x_maps,
638 static const struct of_device_id axp20x_adc_of_match[] = {
639 { .compatible = "x-powers,axp209-adc", .data = (void *)&axp20x_data, },
640 { .compatible = "x-powers,axp221-adc", .data = (void *)&axp22x_data, },
641 { .compatible = "x-powers,axp813-adc", .data = (void *)&axp813_data, },
642 { /* sentinel */ }
644 MODULE_DEVICE_TABLE(of, axp20x_adc_of_match);
646 static const struct platform_device_id axp20x_adc_id_match[] = {
647 { .name = "axp20x-adc", .driver_data = (kernel_ulong_t)&axp20x_data, },
648 { .name = "axp22x-adc", .driver_data = (kernel_ulong_t)&axp22x_data, },
649 { .name = "axp813-adc", .driver_data = (kernel_ulong_t)&axp813_data, },
650 { /* sentinel */ },
652 MODULE_DEVICE_TABLE(platform, axp20x_adc_id_match);
654 static int axp20x_probe(struct platform_device *pdev)
656 struct axp20x_adc_iio *info;
657 struct iio_dev *indio_dev;
658 struct axp20x_dev *axp20x_dev;
659 int ret;
661 axp20x_dev = dev_get_drvdata(pdev->dev.parent);
663 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
664 if (!indio_dev)
665 return -ENOMEM;
667 info = iio_priv(indio_dev);
668 platform_set_drvdata(pdev, indio_dev);
670 info->regmap = axp20x_dev->regmap;
671 indio_dev->modes = INDIO_DIRECT_MODE;
673 if (!dev_fwnode(&pdev->dev)) {
674 const struct platform_device_id *id;
676 id = platform_get_device_id(pdev);
677 info->data = (const struct axp_data *)id->driver_data;
678 } else {
679 struct device *dev = &pdev->dev;
681 info->data = device_get_match_data(dev);
684 indio_dev->name = platform_get_device_id(pdev)->name;
685 indio_dev->info = info->data->iio_info;
686 indio_dev->num_channels = info->data->num_channels;
687 indio_dev->channels = info->data->channels;
689 /* Enable the ADCs on IP */
690 regmap_write(info->regmap, AXP20X_ADC_EN1, info->data->adc_en1_mask);
692 if (info->data->adc_en2)
693 /* Enable GPIO0/1 and internal temperature ADCs */
694 regmap_update_bits(info->regmap, AXP20X_ADC_EN2,
695 AXP20X_ADC_EN2_MASK, AXP20X_ADC_EN2_MASK);
697 /* Configure ADCs rate */
698 info->data->adc_rate(info, 100);
700 ret = iio_map_array_register(indio_dev, info->data->maps);
701 if (ret < 0) {
702 dev_err(&pdev->dev, "failed to register IIO maps: %d\n", ret);
703 goto fail_map;
706 ret = iio_device_register(indio_dev);
707 if (ret < 0) {
708 dev_err(&pdev->dev, "could not register the device\n");
709 goto fail_register;
712 return 0;
714 fail_register:
715 iio_map_array_unregister(indio_dev);
717 fail_map:
718 regmap_write(info->regmap, AXP20X_ADC_EN1, 0);
720 if (info->data->adc_en2)
721 regmap_write(info->regmap, AXP20X_ADC_EN2, 0);
723 return ret;
726 static int axp20x_remove(struct platform_device *pdev)
728 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
729 struct axp20x_adc_iio *info = iio_priv(indio_dev);
731 iio_device_unregister(indio_dev);
732 iio_map_array_unregister(indio_dev);
734 regmap_write(info->regmap, AXP20X_ADC_EN1, 0);
736 if (info->data->adc_en2)
737 regmap_write(info->regmap, AXP20X_ADC_EN2, 0);
739 return 0;
742 static struct platform_driver axp20x_adc_driver = {
743 .driver = {
744 .name = "axp20x-adc",
745 .of_match_table = axp20x_adc_of_match,
747 .id_table = axp20x_adc_id_match,
748 .probe = axp20x_probe,
749 .remove = axp20x_remove,
752 module_platform_driver(axp20x_adc_driver);
754 MODULE_DESCRIPTION("ADC driver for AXP20X and AXP22X PMICs");
755 MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
756 MODULE_LICENSE("GPL");