2 * axp288_adc.c - X-Powers AXP288 PMIC ADC Driver
4 * Copyright (C) 2014 Intel Corporation
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/device.h>
22 #include <linux/regmap.h>
23 #include <linux/mfd/axp20x.h>
24 #include <linux/platform_device.h>
26 #include <linux/iio/iio.h>
27 #include <linux/iio/machine.h>
28 #include <linux/iio/driver.h>
31 * This mask enables all ADCs except for the battery temp-sensor (TS), that is
32 * left as-is to avoid breaking charging on devices without a temp-sensor.
34 #define AXP288_ADC_EN_MASK 0xF0
35 #define AXP288_ADC_TS_ENABLE 0x01
37 #define AXP288_ADC_TS_CURRENT_ON_OFF_MASK GENMASK(1, 0)
38 #define AXP288_ADC_TS_CURRENT_OFF (0 << 0)
39 #define AXP288_ADC_TS_CURRENT_ON_WHEN_CHARGING (1 << 0)
40 #define AXP288_ADC_TS_CURRENT_ON_ONDEMAND (2 << 0)
41 #define AXP288_ADC_TS_CURRENT_ON (3 << 0)
47 AXP288_ADC_BATT_CHRG_I,
48 AXP288_ADC_BATT_DISCHRG_I,
53 struct axp288_adc_info {
55 struct regmap *regmap;
59 static const struct iio_chan_spec axp288_adc_channels[] = {
64 .address = AXP288_TS_ADC_H,
65 .datasheet_name = "TS_PIN",
66 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
71 .address = AXP288_PMIC_ADC_H,
72 .datasheet_name = "PMIC_TEMP",
73 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
78 .address = AXP288_GP_ADC_H,
79 .datasheet_name = "GPADC",
80 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
85 .address = AXP20X_BATT_CHRG_I_H,
86 .datasheet_name = "BATT_CHG_I",
87 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
92 .address = AXP20X_BATT_DISCHRG_I_H,
93 .datasheet_name = "BATT_DISCHRG_I",
94 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
99 .address = AXP20X_BATT_V_H,
100 .datasheet_name = "BATT_V",
101 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
105 /* for consumer drivers */
106 static struct iio_map axp288_adc_default_maps[] = {
107 IIO_MAP("TS_PIN", "axp288-batt", "axp288-batt-temp"),
108 IIO_MAP("PMIC_TEMP", "axp288-pmic", "axp288-pmic-temp"),
109 IIO_MAP("GPADC", "axp288-gpadc", "axp288-system-temp"),
110 IIO_MAP("BATT_CHG_I", "axp288-chrg", "axp288-chrg-curr"),
111 IIO_MAP("BATT_DISCHRG_I", "axp288-chrg", "axp288-chrg-d-curr"),
112 IIO_MAP("BATT_V", "axp288-batt", "axp288-batt-volt"),
116 static int axp288_adc_read_channel(int *val, unsigned long address,
117 struct regmap *regmap)
121 if (regmap_bulk_read(regmap, address, buf, 2))
123 *val = (buf[0] << 4) + ((buf[1] >> 4) & 0x0F);
129 * The current-source used for the battery temp-sensor (TS) is shared
130 * with the GPADC. For proper fuel-gauge and charger operation the TS
131 * current-source needs to be permanently on. But to read the GPADC we
132 * need to temporary switch the TS current-source to ondemand, so that
133 * the GPADC can use it, otherwise we will always read an all 0 value.
135 static int axp288_adc_set_ts(struct axp288_adc_info *info,
136 unsigned int mode, unsigned long address)
140 /* No need to switch the current-source if the TS pin is disabled */
141 if (!info->ts_enabled)
144 /* Channels other than GPADC do not need the current source */
145 if (address != AXP288_GP_ADC_H)
148 ret = regmap_update_bits(info->regmap, AXP288_ADC_TS_PIN_CTRL,
149 AXP288_ADC_TS_CURRENT_ON_OFF_MASK, mode);
153 /* When switching to the GPADC pin give things some time to settle */
154 if (mode == AXP288_ADC_TS_CURRENT_ON_ONDEMAND)
155 usleep_range(6000, 10000);
160 static int axp288_adc_read_raw(struct iio_dev *indio_dev,
161 struct iio_chan_spec const *chan,
162 int *val, int *val2, long mask)
165 struct axp288_adc_info *info = iio_priv(indio_dev);
167 mutex_lock(&indio_dev->mlock);
169 case IIO_CHAN_INFO_RAW:
170 if (axp288_adc_set_ts(info, AXP288_ADC_TS_CURRENT_ON_ONDEMAND,
172 dev_err(&indio_dev->dev, "GPADC mode\n");
176 ret = axp288_adc_read_channel(val, chan->address, info->regmap);
177 if (axp288_adc_set_ts(info, AXP288_ADC_TS_CURRENT_ON,
179 dev_err(&indio_dev->dev, "TS pin restore\n");
184 mutex_unlock(&indio_dev->mlock);
189 static int axp288_adc_initialize(struct axp288_adc_info *info)
191 int ret, adc_enable_val;
194 * Determine if the TS pin is enabled and set the TS current-source
197 ret = regmap_read(info->regmap, AXP20X_ADC_EN1, &adc_enable_val);
201 if (adc_enable_val & AXP288_ADC_TS_ENABLE) {
202 info->ts_enabled = true;
203 ret = regmap_update_bits(info->regmap, AXP288_ADC_TS_PIN_CTRL,
204 AXP288_ADC_TS_CURRENT_ON_OFF_MASK,
205 AXP288_ADC_TS_CURRENT_ON);
207 info->ts_enabled = false;
208 ret = regmap_update_bits(info->regmap, AXP288_ADC_TS_PIN_CTRL,
209 AXP288_ADC_TS_CURRENT_ON_OFF_MASK,
210 AXP288_ADC_TS_CURRENT_OFF);
215 /* Turn on the ADC for all channels except TS, leave TS as is */
216 return regmap_update_bits(info->regmap, AXP20X_ADC_EN1,
217 AXP288_ADC_EN_MASK, AXP288_ADC_EN_MASK);
220 static const struct iio_info axp288_adc_iio_info = {
221 .read_raw = &axp288_adc_read_raw,
224 static int axp288_adc_probe(struct platform_device *pdev)
227 struct axp288_adc_info *info;
228 struct iio_dev *indio_dev;
229 struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
231 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
235 info = iio_priv(indio_dev);
236 info->irq = platform_get_irq(pdev, 0);
238 dev_err(&pdev->dev, "no irq resource?\n");
241 platform_set_drvdata(pdev, indio_dev);
242 info->regmap = axp20x->regmap;
244 * Set ADC to enabled state at all time, including system suspend.
245 * otherwise internal fuel gauge functionality may be affected.
247 ret = axp288_adc_initialize(info);
249 dev_err(&pdev->dev, "unable to enable ADC device\n");
253 indio_dev->dev.parent = &pdev->dev;
254 indio_dev->name = pdev->name;
255 indio_dev->channels = axp288_adc_channels;
256 indio_dev->num_channels = ARRAY_SIZE(axp288_adc_channels);
257 indio_dev->info = &axp288_adc_iio_info;
258 indio_dev->modes = INDIO_DIRECT_MODE;
259 ret = iio_map_array_register(indio_dev, axp288_adc_default_maps);
263 ret = iio_device_register(indio_dev);
265 dev_err(&pdev->dev, "unable to register iio device\n");
266 goto err_array_unregister;
270 err_array_unregister:
271 iio_map_array_unregister(indio_dev);
276 static int axp288_adc_remove(struct platform_device *pdev)
278 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
280 iio_device_unregister(indio_dev);
281 iio_map_array_unregister(indio_dev);
286 static const struct platform_device_id axp288_adc_id_table[] = {
287 { .name = "axp288_adc" },
291 static struct platform_driver axp288_adc_driver = {
292 .probe = axp288_adc_probe,
293 .remove = axp288_adc_remove,
294 .id_table = axp288_adc_id_table,
296 .name = "axp288_adc",
300 MODULE_DEVICE_TABLE(platform, axp288_adc_id_table);
302 module_platform_driver(axp288_adc_driver);
305 MODULE_DESCRIPTION("X-Powers AXP288 ADC Driver");
306 MODULE_LICENSE("GPL");