1 /* ADC driver for AXP20X and AXP22X PMICs
3 * Copyright (c) 2016 Free Electrons NextThing Co.
4 * Quentin Schulz <quentin.schulz@free-electrons.com>
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License version 2 as published by the
8 * Free Software Foundation.
11 #include <linux/completion.h>
12 #include <linux/interrupt.h>
14 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/regmap.h>
20 #include <linux/thermal.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/driver.h>
24 #include <linux/iio/machine.h>
25 #include <linux/mfd/axp20x.h>
27 #define AXP20X_ADC_EN1_MASK GENMASK(7, 0)
29 #define AXP20X_ADC_EN2_MASK (GENMASK(3, 2) | BIT(7))
30 #define AXP22X_ADC_EN1_MASK (GENMASK(7, 5) | BIT(0))
32 #define AXP20X_GPIO10_IN_RANGE_GPIO0 BIT(0)
33 #define AXP20X_GPIO10_IN_RANGE_GPIO1 BIT(1)
34 #define AXP20X_GPIO10_IN_RANGE_GPIO0_VAL(x) ((x) & BIT(0))
35 #define AXP20X_GPIO10_IN_RANGE_GPIO1_VAL(x) (((x) & BIT(0)) << 1)
37 #define AXP20X_ADC_RATE_MASK GENMASK(7, 6)
38 #define AXP813_V_I_ADC_RATE_MASK GENMASK(5, 4)
39 #define AXP813_ADC_RATE_MASK (AXP20X_ADC_RATE_MASK | AXP813_V_I_ADC_RATE_MASK)
40 #define AXP20X_ADC_RATE_HZ(x) ((ilog2((x) / 25) << 6) & AXP20X_ADC_RATE_MASK)
41 #define AXP22X_ADC_RATE_HZ(x) ((ilog2((x) / 100) << 6) & AXP20X_ADC_RATE_MASK)
42 #define AXP813_TS_GPIO0_ADC_RATE_HZ(x) AXP20X_ADC_RATE_HZ(x)
43 #define AXP813_V_I_ADC_RATE_HZ(x) ((ilog2((x) / 100) << 4) & AXP813_V_I_ADC_RATE_MASK)
44 #define AXP813_ADC_RATE_HZ(x) (AXP20X_ADC_RATE_HZ(x) | AXP813_V_I_ADC_RATE_HZ(x))
46 #define AXP20X_ADC_CHANNEL(_channel, _name, _type, _reg) \
50 .channel = _channel, \
52 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
53 BIT(IIO_CHAN_INFO_SCALE), \
54 .datasheet_name = _name, \
57 #define AXP20X_ADC_CHANNEL_OFFSET(_channel, _name, _type, _reg) \
61 .channel = _channel, \
63 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
64 BIT(IIO_CHAN_INFO_SCALE) |\
65 BIT(IIO_CHAN_INFO_OFFSET),\
66 .datasheet_name = _name, \
71 struct axp20x_adc_iio {
72 struct regmap *regmap;
73 struct axp_data *data;
76 enum axp20x_adc_channel_v {
86 enum axp20x_adc_channel_i {
90 AXP20X_BATT_DISCHRG_I,
93 enum axp22x_adc_channel_v {
98 enum axp22x_adc_channel_i {
99 AXP22X_BATT_CHRG_I = 1,
100 AXP22X_BATT_DISCHRG_I,
103 enum axp813_adc_channel_v {
109 static struct iio_map axp20x_maps[] = {
111 .consumer_dev_name = "axp20x-usb-power-supply",
112 .consumer_channel = "vbus_v",
113 .adc_channel_label = "vbus_v",
115 .consumer_dev_name = "axp20x-usb-power-supply",
116 .consumer_channel = "vbus_i",
117 .adc_channel_label = "vbus_i",
119 .consumer_dev_name = "axp20x-ac-power-supply",
120 .consumer_channel = "acin_v",
121 .adc_channel_label = "acin_v",
123 .consumer_dev_name = "axp20x-ac-power-supply",
124 .consumer_channel = "acin_i",
125 .adc_channel_label = "acin_i",
127 .consumer_dev_name = "axp20x-battery-power-supply",
128 .consumer_channel = "batt_v",
129 .adc_channel_label = "batt_v",
131 .consumer_dev_name = "axp20x-battery-power-supply",
132 .consumer_channel = "batt_chrg_i",
133 .adc_channel_label = "batt_chrg_i",
135 .consumer_dev_name = "axp20x-battery-power-supply",
136 .consumer_channel = "batt_dischrg_i",
137 .adc_channel_label = "batt_dischrg_i",
138 }, { /* sentinel */ }
141 static struct iio_map axp22x_maps[] = {
143 .consumer_dev_name = "axp20x-battery-power-supply",
144 .consumer_channel = "batt_v",
145 .adc_channel_label = "batt_v",
147 .consumer_dev_name = "axp20x-battery-power-supply",
148 .consumer_channel = "batt_chrg_i",
149 .adc_channel_label = "batt_chrg_i",
151 .consumer_dev_name = "axp20x-battery-power-supply",
152 .consumer_channel = "batt_dischrg_i",
153 .adc_channel_label = "batt_dischrg_i",
154 }, { /* sentinel */ }
158 * Channels are mapped by physical system. Their channels share the same index.
159 * i.e. acin_i is in_current0_raw and acin_v is in_voltage0_raw.
160 * The only exception is for the battery. batt_v will be in_voltage6_raw and
161 * charge current in_current6_raw and discharge current will be in_current7_raw.
163 static const struct iio_chan_spec axp20x_adc_channels[] = {
164 AXP20X_ADC_CHANNEL(AXP20X_ACIN_V, "acin_v", IIO_VOLTAGE,
165 AXP20X_ACIN_V_ADC_H),
166 AXP20X_ADC_CHANNEL(AXP20X_ACIN_I, "acin_i", IIO_CURRENT,
167 AXP20X_ACIN_I_ADC_H),
168 AXP20X_ADC_CHANNEL(AXP20X_VBUS_V, "vbus_v", IIO_VOLTAGE,
169 AXP20X_VBUS_V_ADC_H),
170 AXP20X_ADC_CHANNEL(AXP20X_VBUS_I, "vbus_i", IIO_CURRENT,
171 AXP20X_VBUS_I_ADC_H),
174 .address = AXP20X_TEMP_ADC_H,
175 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
176 BIT(IIO_CHAN_INFO_SCALE) |
177 BIT(IIO_CHAN_INFO_OFFSET),
178 .datasheet_name = "pmic_temp",
180 AXP20X_ADC_CHANNEL_OFFSET(AXP20X_GPIO0_V, "gpio0_v", IIO_VOLTAGE,
181 AXP20X_GPIO0_V_ADC_H),
182 AXP20X_ADC_CHANNEL_OFFSET(AXP20X_GPIO1_V, "gpio1_v", IIO_VOLTAGE,
183 AXP20X_GPIO1_V_ADC_H),
184 AXP20X_ADC_CHANNEL(AXP20X_IPSOUT_V, "ipsout_v", IIO_VOLTAGE,
185 AXP20X_IPSOUT_V_HIGH_H),
186 AXP20X_ADC_CHANNEL(AXP20X_BATT_V, "batt_v", IIO_VOLTAGE,
188 AXP20X_ADC_CHANNEL(AXP20X_BATT_CHRG_I, "batt_chrg_i", IIO_CURRENT,
189 AXP20X_BATT_CHRG_I_H),
190 AXP20X_ADC_CHANNEL(AXP20X_BATT_DISCHRG_I, "batt_dischrg_i", IIO_CURRENT,
191 AXP20X_BATT_DISCHRG_I_H),
194 static const struct iio_chan_spec axp22x_adc_channels[] = {
197 .address = AXP22X_PMIC_TEMP_H,
198 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
199 BIT(IIO_CHAN_INFO_SCALE) |
200 BIT(IIO_CHAN_INFO_OFFSET),
201 .datasheet_name = "pmic_temp",
203 AXP20X_ADC_CHANNEL(AXP22X_BATT_V, "batt_v", IIO_VOLTAGE,
205 AXP20X_ADC_CHANNEL(AXP22X_BATT_CHRG_I, "batt_chrg_i", IIO_CURRENT,
206 AXP20X_BATT_CHRG_I_H),
207 AXP20X_ADC_CHANNEL(AXP22X_BATT_DISCHRG_I, "batt_dischrg_i", IIO_CURRENT,
208 AXP20X_BATT_DISCHRG_I_H),
211 static const struct iio_chan_spec axp813_adc_channels[] = {
214 .address = AXP22X_PMIC_TEMP_H,
215 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
216 BIT(IIO_CHAN_INFO_SCALE) |
217 BIT(IIO_CHAN_INFO_OFFSET),
218 .datasheet_name = "pmic_temp",
220 AXP20X_ADC_CHANNEL(AXP813_GPIO0_V, "gpio0_v", IIO_VOLTAGE,
222 AXP20X_ADC_CHANNEL(AXP813_BATT_V, "batt_v", IIO_VOLTAGE,
224 AXP20X_ADC_CHANNEL(AXP22X_BATT_CHRG_I, "batt_chrg_i", IIO_CURRENT,
225 AXP20X_BATT_CHRG_I_H),
226 AXP20X_ADC_CHANNEL(AXP22X_BATT_DISCHRG_I, "batt_dischrg_i", IIO_CURRENT,
227 AXP20X_BATT_DISCHRG_I_H),
230 static int axp20x_adc_raw(struct iio_dev *indio_dev,
231 struct iio_chan_spec const *chan, int *val)
233 struct axp20x_adc_iio *info = iio_priv(indio_dev);
237 * N.B.: Unlike the Chinese datasheets tell, the charging current is
238 * stored on 12 bits, not 13 bits. Only discharging current is on 13
241 if (chan->type == IIO_CURRENT && chan->channel == AXP20X_BATT_DISCHRG_I)
246 *val = axp20x_read_variable_width(info->regmap, chan->address, size);
253 static int axp22x_adc_raw(struct iio_dev *indio_dev,
254 struct iio_chan_spec const *chan, int *val)
256 struct axp20x_adc_iio *info = iio_priv(indio_dev);
258 *val = axp20x_read_variable_width(info->regmap, chan->address, 12);
265 static int axp813_adc_raw(struct iio_dev *indio_dev,
266 struct iio_chan_spec const *chan, int *val)
268 struct axp20x_adc_iio *info = iio_priv(indio_dev);
270 *val = axp20x_read_variable_width(info->regmap, chan->address, 12);
277 static int axp20x_adc_scale_voltage(int channel, int *val, int *val2)
284 return IIO_VAL_INT_PLUS_MICRO;
290 return IIO_VAL_INT_PLUS_MICRO;
295 return IIO_VAL_INT_PLUS_MICRO;
297 case AXP20X_IPSOUT_V:
300 return IIO_VAL_INT_PLUS_MICRO;
307 static int axp813_adc_scale_voltage(int channel, int *val, int *val2)
313 return IIO_VAL_INT_PLUS_MICRO;
318 return IIO_VAL_INT_PLUS_MICRO;
325 static int axp20x_adc_scale_current(int channel, int *val, int *val2)
331 return IIO_VAL_INT_PLUS_MICRO;
336 return IIO_VAL_INT_PLUS_MICRO;
338 case AXP20X_BATT_DISCHRG_I:
339 case AXP20X_BATT_CHRG_I:
342 return IIO_VAL_INT_PLUS_MICRO;
349 static int axp20x_adc_scale(struct iio_chan_spec const *chan, int *val,
352 switch (chan->type) {
354 return axp20x_adc_scale_voltage(chan->channel, val, val2);
357 return axp20x_adc_scale_current(chan->channel, val, val2);
368 static int axp22x_adc_scale(struct iio_chan_spec const *chan, int *val,
371 switch (chan->type) {
373 if (chan->channel != AXP22X_BATT_V)
378 return IIO_VAL_INT_PLUS_MICRO;
393 static int axp813_adc_scale(struct iio_chan_spec const *chan, int *val,
396 switch (chan->type) {
398 return axp813_adc_scale_voltage(chan->channel, val, val2);
413 static int axp20x_adc_offset_voltage(struct iio_dev *indio_dev, int channel,
416 struct axp20x_adc_iio *info = iio_priv(indio_dev);
419 ret = regmap_read(info->regmap, AXP20X_GPIO10_IN_RANGE, val);
425 *val &= AXP20X_GPIO10_IN_RANGE_GPIO0;
429 *val &= AXP20X_GPIO10_IN_RANGE_GPIO1;
436 *val = *val ? 700000 : 0;
441 static int axp20x_adc_offset(struct iio_dev *indio_dev,
442 struct iio_chan_spec const *chan, int *val)
444 switch (chan->type) {
446 return axp20x_adc_offset_voltage(indio_dev, chan->channel, val);
457 static int axp20x_read_raw(struct iio_dev *indio_dev,
458 struct iio_chan_spec const *chan, int *val,
459 int *val2, long mask)
462 case IIO_CHAN_INFO_OFFSET:
463 return axp20x_adc_offset(indio_dev, chan, val);
465 case IIO_CHAN_INFO_SCALE:
466 return axp20x_adc_scale(chan, val, val2);
468 case IIO_CHAN_INFO_RAW:
469 return axp20x_adc_raw(indio_dev, chan, val);
476 static int axp22x_read_raw(struct iio_dev *indio_dev,
477 struct iio_chan_spec const *chan, int *val,
478 int *val2, long mask)
481 case IIO_CHAN_INFO_OFFSET:
485 case IIO_CHAN_INFO_SCALE:
486 return axp22x_adc_scale(chan, val, val2);
488 case IIO_CHAN_INFO_RAW:
489 return axp22x_adc_raw(indio_dev, chan, val);
496 static int axp813_read_raw(struct iio_dev *indio_dev,
497 struct iio_chan_spec const *chan, int *val,
498 int *val2, long mask)
501 case IIO_CHAN_INFO_OFFSET:
505 case IIO_CHAN_INFO_SCALE:
506 return axp813_adc_scale(chan, val, val2);
508 case IIO_CHAN_INFO_RAW:
509 return axp813_adc_raw(indio_dev, chan, val);
516 static int axp20x_write_raw(struct iio_dev *indio_dev,
517 struct iio_chan_spec const *chan, int val, int val2,
520 struct axp20x_adc_iio *info = iio_priv(indio_dev);
521 unsigned int reg, regval;
524 * The AXP20X PMIC allows the user to choose between 0V and 0.7V offsets
525 * for (independently) GPIO0 and GPIO1 when in ADC mode.
527 if (mask != IIO_CHAN_INFO_OFFSET)
530 if (val != 0 && val != 700000)
535 switch (chan->channel) {
537 reg = AXP20X_GPIO10_IN_RANGE_GPIO0;
538 regval = AXP20X_GPIO10_IN_RANGE_GPIO0_VAL(val);
542 reg = AXP20X_GPIO10_IN_RANGE_GPIO1;
543 regval = AXP20X_GPIO10_IN_RANGE_GPIO1_VAL(val);
550 return regmap_update_bits(info->regmap, AXP20X_GPIO10_IN_RANGE, reg,
554 static const struct iio_info axp20x_adc_iio_info = {
555 .read_raw = axp20x_read_raw,
556 .write_raw = axp20x_write_raw,
559 static const struct iio_info axp22x_adc_iio_info = {
560 .read_raw = axp22x_read_raw,
563 static const struct iio_info axp813_adc_iio_info = {
564 .read_raw = axp813_read_raw,
567 static int axp20x_adc_rate(struct axp20x_adc_iio *info, int rate)
569 return regmap_update_bits(info->regmap, AXP20X_ADC_RATE,
570 AXP20X_ADC_RATE_MASK,
571 AXP20X_ADC_RATE_HZ(rate));
574 static int axp22x_adc_rate(struct axp20x_adc_iio *info, int rate)
576 return regmap_update_bits(info->regmap, AXP20X_ADC_RATE,
577 AXP20X_ADC_RATE_MASK,
578 AXP22X_ADC_RATE_HZ(rate));
581 static int axp813_adc_rate(struct axp20x_adc_iio *info, int rate)
583 return regmap_update_bits(info->regmap, AXP813_ADC_RATE,
584 AXP813_ADC_RATE_MASK,
585 AXP813_ADC_RATE_HZ(rate));
589 const struct iio_info *iio_info;
591 struct iio_chan_spec const *channels;
592 unsigned long adc_en1_mask;
593 int (*adc_rate)(struct axp20x_adc_iio *info,
596 struct iio_map *maps;
599 static const struct axp_data axp20x_data = {
600 .iio_info = &axp20x_adc_iio_info,
601 .num_channels = ARRAY_SIZE(axp20x_adc_channels),
602 .channels = axp20x_adc_channels,
603 .adc_en1_mask = AXP20X_ADC_EN1_MASK,
604 .adc_rate = axp20x_adc_rate,
609 static const struct axp_data axp22x_data = {
610 .iio_info = &axp22x_adc_iio_info,
611 .num_channels = ARRAY_SIZE(axp22x_adc_channels),
612 .channels = axp22x_adc_channels,
613 .adc_en1_mask = AXP22X_ADC_EN1_MASK,
614 .adc_rate = axp22x_adc_rate,
619 static const struct axp_data axp813_data = {
620 .iio_info = &axp813_adc_iio_info,
621 .num_channels = ARRAY_SIZE(axp813_adc_channels),
622 .channels = axp813_adc_channels,
623 .adc_en1_mask = AXP22X_ADC_EN1_MASK,
624 .adc_rate = axp813_adc_rate,
629 static const struct of_device_id axp20x_adc_of_match[] = {
630 { .compatible = "x-powers,axp209-adc", .data = (void *)&axp20x_data, },
631 { .compatible = "x-powers,axp221-adc", .data = (void *)&axp22x_data, },
632 { .compatible = "x-powers,axp813-adc", .data = (void *)&axp813_data, },
635 MODULE_DEVICE_TABLE(of, axp20x_adc_of_match);
637 static const struct platform_device_id axp20x_adc_id_match[] = {
638 { .name = "axp20x-adc", .driver_data = (kernel_ulong_t)&axp20x_data, },
639 { .name = "axp22x-adc", .driver_data = (kernel_ulong_t)&axp22x_data, },
640 { .name = "axp813-adc", .driver_data = (kernel_ulong_t)&axp813_data, },
643 MODULE_DEVICE_TABLE(platform, axp20x_adc_id_match);
645 static int axp20x_probe(struct platform_device *pdev)
647 struct axp20x_adc_iio *info;
648 struct iio_dev *indio_dev;
649 struct axp20x_dev *axp20x_dev;
652 axp20x_dev = dev_get_drvdata(pdev->dev.parent);
654 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
658 info = iio_priv(indio_dev);
659 platform_set_drvdata(pdev, indio_dev);
661 info->regmap = axp20x_dev->regmap;
662 indio_dev->dev.parent = &pdev->dev;
663 indio_dev->dev.of_node = pdev->dev.of_node;
664 indio_dev->modes = INDIO_DIRECT_MODE;
666 if (!pdev->dev.of_node) {
667 const struct platform_device_id *id;
669 id = platform_get_device_id(pdev);
670 info->data = (struct axp_data *)id->driver_data;
672 struct device *dev = &pdev->dev;
674 info->data = (struct axp_data *)of_device_get_match_data(dev);
677 indio_dev->name = platform_get_device_id(pdev)->name;
678 indio_dev->info = info->data->iio_info;
679 indio_dev->num_channels = info->data->num_channels;
680 indio_dev->channels = info->data->channels;
682 /* Enable the ADCs on IP */
683 regmap_write(info->regmap, AXP20X_ADC_EN1, info->data->adc_en1_mask);
685 if (info->data->adc_en2)
686 /* Enable GPIO0/1 and internal temperature ADCs */
687 regmap_update_bits(info->regmap, AXP20X_ADC_EN2,
688 AXP20X_ADC_EN2_MASK, AXP20X_ADC_EN2_MASK);
690 /* Configure ADCs rate */
691 info->data->adc_rate(info, 100);
693 ret = iio_map_array_register(indio_dev, info->data->maps);
695 dev_err(&pdev->dev, "failed to register IIO maps: %d\n", ret);
699 ret = iio_device_register(indio_dev);
701 dev_err(&pdev->dev, "could not register the device\n");
708 iio_map_array_unregister(indio_dev);
711 regmap_write(info->regmap, AXP20X_ADC_EN1, 0);
713 if (info->data->adc_en2)
714 regmap_write(info->regmap, AXP20X_ADC_EN2, 0);
719 static int axp20x_remove(struct platform_device *pdev)
721 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
722 struct axp20x_adc_iio *info = iio_priv(indio_dev);
724 iio_device_unregister(indio_dev);
725 iio_map_array_unregister(indio_dev);
727 regmap_write(info->regmap, AXP20X_ADC_EN1, 0);
729 if (info->data->adc_en2)
730 regmap_write(info->regmap, AXP20X_ADC_EN2, 0);
735 static struct platform_driver axp20x_adc_driver = {
737 .name = "axp20x-adc",
738 .of_match_table = of_match_ptr(axp20x_adc_of_match),
740 .id_table = axp20x_adc_id_match,
741 .probe = axp20x_probe,
742 .remove = axp20x_remove,
745 module_platform_driver(axp20x_adc_driver);
747 MODULE_DESCRIPTION("ADC driver for AXP20X and AXP22X PMICs");
748 MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
749 MODULE_LICENSE("GPL");