1 // SPDX-License-Identifier: GPL-2.0-only
3 * axp288_adc.c - X-Powers AXP288 PMIC ADC Driver
5 * Copyright (C) 2014 Intel Corporation
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 #include <linux/dmi.h>
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/device.h>
14 #include <linux/regmap.h>
15 #include <linux/mfd/axp20x.h>
16 #include <linux/platform_device.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/machine.h>
20 #include <linux/iio/driver.h>
23 * This mask enables all ADCs except for the battery temp-sensor (TS), that is
24 * left as-is to avoid breaking charging on devices without a temp-sensor.
26 #define AXP288_ADC_EN_MASK 0xF0
27 #define AXP288_ADC_TS_ENABLE 0x01
29 #define AXP288_ADC_TS_BIAS_MASK GENMASK(5, 4)
30 #define AXP288_ADC_TS_BIAS_20UA (0 << 4)
31 #define AXP288_ADC_TS_BIAS_40UA (1 << 4)
32 #define AXP288_ADC_TS_BIAS_60UA (2 << 4)
33 #define AXP288_ADC_TS_BIAS_80UA (3 << 4)
34 #define AXP288_ADC_TS_CURRENT_ON_OFF_MASK GENMASK(1, 0)
35 #define AXP288_ADC_TS_CURRENT_OFF (0 << 0)
36 #define AXP288_ADC_TS_CURRENT_ON_WHEN_CHARGING (1 << 0)
37 #define AXP288_ADC_TS_CURRENT_ON_ONDEMAND (2 << 0)
38 #define AXP288_ADC_TS_CURRENT_ON (3 << 0)
44 AXP288_ADC_BATT_CHRG_I
,
45 AXP288_ADC_BATT_DISCHRG_I
,
50 struct axp288_adc_info
{
52 struct regmap
*regmap
;
56 static const struct iio_chan_spec axp288_adc_channels
[] = {
61 .address
= AXP288_TS_ADC_H
,
62 .datasheet_name
= "TS_PIN",
63 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
68 .address
= AXP288_PMIC_ADC_H
,
69 .datasheet_name
= "PMIC_TEMP",
70 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
75 .address
= AXP288_GP_ADC_H
,
76 .datasheet_name
= "GPADC",
77 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
82 .address
= AXP20X_BATT_CHRG_I_H
,
83 .datasheet_name
= "BATT_CHG_I",
84 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
89 .address
= AXP20X_BATT_DISCHRG_I_H
,
90 .datasheet_name
= "BATT_DISCHRG_I",
91 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
96 .address
= AXP20X_BATT_V_H
,
97 .datasheet_name
= "BATT_V",
98 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
102 /* for consumer drivers */
103 static struct iio_map axp288_adc_default_maps
[] = {
104 IIO_MAP("TS_PIN", "axp288-batt", "axp288-batt-temp"),
105 IIO_MAP("PMIC_TEMP", "axp288-pmic", "axp288-pmic-temp"),
106 IIO_MAP("GPADC", "axp288-gpadc", "axp288-system-temp"),
107 IIO_MAP("BATT_CHG_I", "axp288-chrg", "axp288-chrg-curr"),
108 IIO_MAP("BATT_DISCHRG_I", "axp288-chrg", "axp288-chrg-d-curr"),
109 IIO_MAP("BATT_V", "axp288-batt", "axp288-batt-volt"),
113 static int axp288_adc_read_channel(int *val
, unsigned long address
,
114 struct regmap
*regmap
)
118 if (regmap_bulk_read(regmap
, address
, buf
, 2))
120 *val
= (buf
[0] << 4) + ((buf
[1] >> 4) & 0x0F);
126 * The current-source used for the battery temp-sensor (TS) is shared
127 * with the GPADC. For proper fuel-gauge and charger operation the TS
128 * current-source needs to be permanently on. But to read the GPADC we
129 * need to temporary switch the TS current-source to ondemand, so that
130 * the GPADC can use it, otherwise we will always read an all 0 value.
132 static int axp288_adc_set_ts(struct axp288_adc_info
*info
,
133 unsigned int mode
, unsigned long address
)
137 /* No need to switch the current-source if the TS pin is disabled */
138 if (!info
->ts_enabled
)
141 /* Channels other than GPADC do not need the current source */
142 if (address
!= AXP288_GP_ADC_H
)
145 ret
= regmap_update_bits(info
->regmap
, AXP288_ADC_TS_PIN_CTRL
,
146 AXP288_ADC_TS_CURRENT_ON_OFF_MASK
, mode
);
150 /* When switching to the GPADC pin give things some time to settle */
151 if (mode
== AXP288_ADC_TS_CURRENT_ON_ONDEMAND
)
152 usleep_range(6000, 10000);
157 static int axp288_adc_read_raw(struct iio_dev
*indio_dev
,
158 struct iio_chan_spec
const *chan
,
159 int *val
, int *val2
, long mask
)
162 struct axp288_adc_info
*info
= iio_priv(indio_dev
);
164 mutex_lock(&indio_dev
->mlock
);
166 case IIO_CHAN_INFO_RAW
:
167 if (axp288_adc_set_ts(info
, AXP288_ADC_TS_CURRENT_ON_ONDEMAND
,
169 dev_err(&indio_dev
->dev
, "GPADC mode\n");
173 ret
= axp288_adc_read_channel(val
, chan
->address
, info
->regmap
);
174 if (axp288_adc_set_ts(info
, AXP288_ADC_TS_CURRENT_ON
,
176 dev_err(&indio_dev
->dev
, "TS pin restore\n");
181 mutex_unlock(&indio_dev
->mlock
);
187 * We rely on the machine's firmware to correctly setup the TS pin bias current
188 * at boot. This lists systems with broken fw where we need to set it ourselves.
190 static const struct dmi_system_id axp288_adc_ts_bias_override
[] = {
192 /* Lenovo Ideapad 100S (11 inch) */
194 DMI_MATCH(DMI_SYS_VENDOR
, "LENOVO"),
195 DMI_MATCH(DMI_PRODUCT_VERSION
, "Lenovo ideapad 100S-11IBY"),
197 .driver_data
= (void *)(uintptr_t)AXP288_ADC_TS_BIAS_80UA
,
202 static int axp288_adc_initialize(struct axp288_adc_info
*info
)
204 const struct dmi_system_id
*bias_override
;
205 int ret
, adc_enable_val
;
207 bias_override
= dmi_first_match(axp288_adc_ts_bias_override
);
209 ret
= regmap_update_bits(info
->regmap
, AXP288_ADC_TS_PIN_CTRL
,
210 AXP288_ADC_TS_BIAS_MASK
,
211 (uintptr_t)bias_override
->driver_data
);
217 * Determine if the TS pin is enabled and set the TS current-source
220 ret
= regmap_read(info
->regmap
, AXP20X_ADC_EN1
, &adc_enable_val
);
224 if (adc_enable_val
& AXP288_ADC_TS_ENABLE
) {
225 info
->ts_enabled
= true;
226 ret
= regmap_update_bits(info
->regmap
, AXP288_ADC_TS_PIN_CTRL
,
227 AXP288_ADC_TS_CURRENT_ON_OFF_MASK
,
228 AXP288_ADC_TS_CURRENT_ON
);
230 info
->ts_enabled
= false;
231 ret
= regmap_update_bits(info
->regmap
, AXP288_ADC_TS_PIN_CTRL
,
232 AXP288_ADC_TS_CURRENT_ON_OFF_MASK
,
233 AXP288_ADC_TS_CURRENT_OFF
);
238 /* Turn on the ADC for all channels except TS, leave TS as is */
239 return regmap_update_bits(info
->regmap
, AXP20X_ADC_EN1
,
240 AXP288_ADC_EN_MASK
, AXP288_ADC_EN_MASK
);
243 static const struct iio_info axp288_adc_iio_info
= {
244 .read_raw
= &axp288_adc_read_raw
,
247 static int axp288_adc_probe(struct platform_device
*pdev
)
250 struct axp288_adc_info
*info
;
251 struct iio_dev
*indio_dev
;
252 struct axp20x_dev
*axp20x
= dev_get_drvdata(pdev
->dev
.parent
);
254 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*info
));
258 info
= iio_priv(indio_dev
);
259 info
->irq
= platform_get_irq(pdev
, 0);
262 platform_set_drvdata(pdev
, indio_dev
);
263 info
->regmap
= axp20x
->regmap
;
265 * Set ADC to enabled state at all time, including system suspend.
266 * otherwise internal fuel gauge functionality may be affected.
268 ret
= axp288_adc_initialize(info
);
270 dev_err(&pdev
->dev
, "unable to enable ADC device\n");
274 indio_dev
->name
= pdev
->name
;
275 indio_dev
->channels
= axp288_adc_channels
;
276 indio_dev
->num_channels
= ARRAY_SIZE(axp288_adc_channels
);
277 indio_dev
->info
= &axp288_adc_iio_info
;
278 indio_dev
->modes
= INDIO_DIRECT_MODE
;
279 ret
= iio_map_array_register(indio_dev
, axp288_adc_default_maps
);
283 ret
= iio_device_register(indio_dev
);
285 dev_err(&pdev
->dev
, "unable to register iio device\n");
286 goto err_array_unregister
;
290 err_array_unregister
:
291 iio_map_array_unregister(indio_dev
);
296 static int axp288_adc_remove(struct platform_device
*pdev
)
298 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
300 iio_device_unregister(indio_dev
);
301 iio_map_array_unregister(indio_dev
);
306 static const struct platform_device_id axp288_adc_id_table
[] = {
307 { .name
= "axp288_adc" },
311 static struct platform_driver axp288_adc_driver
= {
312 .probe
= axp288_adc_probe
,
313 .remove
= axp288_adc_remove
,
314 .id_table
= axp288_adc_id_table
,
316 .name
= "axp288_adc",
320 MODULE_DEVICE_TABLE(platform
, axp288_adc_id_table
);
322 module_platform_driver(axp288_adc_driver
);
324 MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@linux.intel.com>");
325 MODULE_DESCRIPTION("X-Powers AXP288 ADC Driver");
326 MODULE_LICENSE("GPL");