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
36 AXP288_ADC_BATT_CHRG_I,
37 AXP288_ADC_BATT_DISCHRG_I,
42 struct axp288_adc_info {
44 struct regmap *regmap;
47 static const struct iio_chan_spec axp288_adc_channels[] = {
52 .address = AXP288_TS_ADC_H,
53 .datasheet_name = "TS_PIN",
54 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
59 .address = AXP288_PMIC_ADC_H,
60 .datasheet_name = "PMIC_TEMP",
61 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
66 .address = AXP288_GP_ADC_H,
67 .datasheet_name = "GPADC",
68 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
73 .address = AXP20X_BATT_CHRG_I_H,
74 .datasheet_name = "BATT_CHG_I",
75 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
80 .address = AXP20X_BATT_DISCHRG_I_H,
81 .datasheet_name = "BATT_DISCHRG_I",
82 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
87 .address = AXP20X_BATT_V_H,
88 .datasheet_name = "BATT_V",
89 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
93 #define AXP288_ADC_MAP(_adc_channel_label, _consumer_dev_name, \
96 .adc_channel_label = _adc_channel_label, \
97 .consumer_dev_name = _consumer_dev_name, \
98 .consumer_channel = _consumer_channel, \
101 /* for consumer drivers */
102 static struct iio_map axp288_adc_default_maps[] = {
103 AXP288_ADC_MAP("TS_PIN", "axp288-batt", "axp288-batt-temp"),
104 AXP288_ADC_MAP("PMIC_TEMP", "axp288-pmic", "axp288-pmic-temp"),
105 AXP288_ADC_MAP("GPADC", "axp288-gpadc", "axp288-system-temp"),
106 AXP288_ADC_MAP("BATT_CHG_I", "axp288-chrg", "axp288-chrg-curr"),
107 AXP288_ADC_MAP("BATT_DISCHRG_I", "axp288-chrg", "axp288-chrg-d-curr"),
108 AXP288_ADC_MAP("BATT_V", "axp288-batt", "axp288-batt-volt"),
112 static int axp288_adc_read_channel(int *val, unsigned long address,
113 struct regmap *regmap)
117 if (regmap_bulk_read(regmap, address, buf, 2))
119 *val = (buf[0] << 4) + ((buf[1] >> 4) & 0x0F);
124 static int axp288_adc_read_raw(struct iio_dev *indio_dev,
125 struct iio_chan_spec const *chan,
126 int *val, int *val2, long mask)
129 struct axp288_adc_info *info = iio_priv(indio_dev);
131 mutex_lock(&indio_dev->mlock);
133 case IIO_CHAN_INFO_RAW:
134 ret = axp288_adc_read_channel(val, chan->address, info->regmap);
139 mutex_unlock(&indio_dev->mlock);
144 static const struct iio_info axp288_adc_iio_info = {
145 .read_raw = &axp288_adc_read_raw,
146 .driver_module = THIS_MODULE,
149 static int axp288_adc_probe(struct platform_device *pdev)
152 struct axp288_adc_info *info;
153 struct iio_dev *indio_dev;
154 struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
156 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
160 info = iio_priv(indio_dev);
161 info->irq = platform_get_irq(pdev, 0);
163 dev_err(&pdev->dev, "no irq resource?\n");
166 platform_set_drvdata(pdev, indio_dev);
167 info->regmap = axp20x->regmap;
169 * Set ADC to enabled state at all time, including system suspend.
170 * otherwise internal fuel gauge functionality may be affected.
172 ret = regmap_write(info->regmap, AXP20X_ADC_EN1, AXP288_ADC_EN_MASK);
174 dev_err(&pdev->dev, "unable to enable ADC device\n");
178 indio_dev->dev.parent = &pdev->dev;
179 indio_dev->name = pdev->name;
180 indio_dev->channels = axp288_adc_channels;
181 indio_dev->num_channels = ARRAY_SIZE(axp288_adc_channels);
182 indio_dev->info = &axp288_adc_iio_info;
183 indio_dev->modes = INDIO_DIRECT_MODE;
184 ret = iio_map_array_register(indio_dev, axp288_adc_default_maps);
188 ret = iio_device_register(indio_dev);
190 dev_err(&pdev->dev, "unable to register iio device\n");
191 goto err_array_unregister;
195 err_array_unregister:
196 iio_map_array_unregister(indio_dev);
201 static int axp288_adc_remove(struct platform_device *pdev)
203 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
205 iio_device_unregister(indio_dev);
206 iio_map_array_unregister(indio_dev);
211 static const struct platform_device_id axp288_adc_id_table[] = {
212 { .name = "axp288_adc" },
216 static struct platform_driver axp288_adc_driver = {
217 .probe = axp288_adc_probe,
218 .remove = axp288_adc_remove,
219 .id_table = axp288_adc_id_table,
221 .name = "axp288_adc",
225 MODULE_DEVICE_TABLE(platform, axp288_adc_id_table);
227 module_platform_driver(axp288_adc_driver);
229 MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@linux.intel.com>");
230 MODULE_DESCRIPTION("X-Powers AXP288 ADC Driver");
231 MODULE_LICENSE("GPL");