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>
30 #define AXP288_ADC_EN_MASK 0xF1
31 #define AXP288_ADC_TS_PIN_GPADC 0xF2
32 #define AXP288_ADC_TS_PIN_ON 0xF3
38 AXP288_ADC_BATT_CHRG_I
,
39 AXP288_ADC_BATT_DISCHRG_I
,
44 struct axp288_adc_info
{
46 struct regmap
*regmap
;
49 static const struct iio_chan_spec axp288_adc_channels
[] = {
54 .address
= AXP288_TS_ADC_H
,
55 .datasheet_name
= "TS_PIN",
56 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
61 .address
= AXP288_PMIC_ADC_H
,
62 .datasheet_name
= "PMIC_TEMP",
63 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
68 .address
= AXP288_GP_ADC_H
,
69 .datasheet_name
= "GPADC",
70 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
75 .address
= AXP20X_BATT_CHRG_I_H
,
76 .datasheet_name
= "BATT_CHG_I",
77 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
82 .address
= AXP20X_BATT_DISCHRG_I_H
,
83 .datasheet_name
= "BATT_DISCHRG_I",
84 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
89 .address
= AXP20X_BATT_V_H
,
90 .datasheet_name
= "BATT_V",
91 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
95 /* for consumer drivers */
96 static struct iio_map axp288_adc_default_maps
[] = {
97 IIO_MAP("TS_PIN", "axp288-batt", "axp288-batt-temp"),
98 IIO_MAP("PMIC_TEMP", "axp288-pmic", "axp288-pmic-temp"),
99 IIO_MAP("GPADC", "axp288-gpadc", "axp288-system-temp"),
100 IIO_MAP("BATT_CHG_I", "axp288-chrg", "axp288-chrg-curr"),
101 IIO_MAP("BATT_DISCHRG_I", "axp288-chrg", "axp288-chrg-d-curr"),
102 IIO_MAP("BATT_V", "axp288-batt", "axp288-batt-volt"),
106 static int axp288_adc_read_channel(int *val
, unsigned long address
,
107 struct regmap
*regmap
)
111 if (regmap_bulk_read(regmap
, address
, buf
, 2))
113 *val
= (buf
[0] << 4) + ((buf
[1] >> 4) & 0x0F);
118 static int axp288_adc_set_ts(struct regmap
*regmap
, unsigned int mode
,
119 unsigned long address
)
123 /* channels other than GPADC do not need to switch TS pin */
124 if (address
!= AXP288_GP_ADC_H
)
127 ret
= regmap_write(regmap
, AXP288_ADC_TS_PIN_CTRL
, mode
);
131 /* When switching to the GPADC pin give things some time to settle */
132 if (mode
== AXP288_ADC_TS_PIN_GPADC
)
133 usleep_range(6000, 10000);
138 static int axp288_adc_read_raw(struct iio_dev
*indio_dev
,
139 struct iio_chan_spec
const *chan
,
140 int *val
, int *val2
, long mask
)
143 struct axp288_adc_info
*info
= iio_priv(indio_dev
);
145 mutex_lock(&indio_dev
->mlock
);
147 case IIO_CHAN_INFO_RAW
:
148 if (axp288_adc_set_ts(info
->regmap
, AXP288_ADC_TS_PIN_GPADC
,
150 dev_err(&indio_dev
->dev
, "GPADC mode\n");
154 ret
= axp288_adc_read_channel(val
, chan
->address
, info
->regmap
);
155 if (axp288_adc_set_ts(info
->regmap
, AXP288_ADC_TS_PIN_ON
,
157 dev_err(&indio_dev
->dev
, "TS pin restore\n");
162 mutex_unlock(&indio_dev
->mlock
);
167 static int axp288_adc_set_state(struct regmap
*regmap
)
169 /* ADC should be always enabled for internal FG to function */
170 if (regmap_write(regmap
, AXP288_ADC_TS_PIN_CTRL
, AXP288_ADC_TS_PIN_ON
))
173 return regmap_write(regmap
, AXP20X_ADC_EN1
, AXP288_ADC_EN_MASK
);
176 static const struct iio_info axp288_adc_iio_info
= {
177 .read_raw
= &axp288_adc_read_raw
,
180 static int axp288_adc_probe(struct platform_device
*pdev
)
183 struct axp288_adc_info
*info
;
184 struct iio_dev
*indio_dev
;
185 struct axp20x_dev
*axp20x
= dev_get_drvdata(pdev
->dev
.parent
);
187 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*info
));
191 info
= iio_priv(indio_dev
);
192 info
->irq
= platform_get_irq(pdev
, 0);
194 dev_err(&pdev
->dev
, "no irq resource?\n");
197 platform_set_drvdata(pdev
, indio_dev
);
198 info
->regmap
= axp20x
->regmap
;
200 * Set ADC to enabled state at all time, including system suspend.
201 * otherwise internal fuel gauge functionality may be affected.
203 ret
= axp288_adc_set_state(axp20x
->regmap
);
205 dev_err(&pdev
->dev
, "unable to enable ADC device\n");
209 indio_dev
->dev
.parent
= &pdev
->dev
;
210 indio_dev
->name
= pdev
->name
;
211 indio_dev
->channels
= axp288_adc_channels
;
212 indio_dev
->num_channels
= ARRAY_SIZE(axp288_adc_channels
);
213 indio_dev
->info
= &axp288_adc_iio_info
;
214 indio_dev
->modes
= INDIO_DIRECT_MODE
;
215 ret
= iio_map_array_register(indio_dev
, axp288_adc_default_maps
);
219 ret
= iio_device_register(indio_dev
);
221 dev_err(&pdev
->dev
, "unable to register iio device\n");
222 goto err_array_unregister
;
226 err_array_unregister
:
227 iio_map_array_unregister(indio_dev
);
232 static int axp288_adc_remove(struct platform_device
*pdev
)
234 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
236 iio_device_unregister(indio_dev
);
237 iio_map_array_unregister(indio_dev
);
242 static const struct platform_device_id axp288_adc_id_table
[] = {
243 { .name
= "axp288_adc" },
247 static struct platform_driver axp288_adc_driver
= {
248 .probe
= axp288_adc_probe
,
249 .remove
= axp288_adc_remove
,
250 .id_table
= axp288_adc_id_table
,
252 .name
= "axp288_adc",
256 MODULE_DEVICE_TABLE(platform
, axp288_adc_id_table
);
258 module_platform_driver(axp288_adc_driver
);
260 MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@linux.intel.com>");
261 MODULE_DESCRIPTION("X-Powers AXP288 ADC Driver");
262 MODULE_LICENSE("GPL");