blob: 64799ad7ebad02a797607470678aa500842ed99f [file] [log] [blame]
Jacob Pande89bd72014-10-06 21:17:15 -07001/*
2 * axp288_adc.c - X-Powers AXP288 PMIC ADC Driver
3 *
4 * Copyright (C) 2014 Intel Corporation
5 *
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7 *
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.
11 *
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.
16 *
17 */
18
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>
25
26#include <linux/iio/iio.h>
27#include <linux/iio/machine.h>
28#include <linux/iio/driver.h>
29
30#define AXP288_ADC_EN_MASK 0xF1
Jacob Pande89bd72014-10-06 21:17:15 -070031
32enum axp288_adc_id {
33 AXP288_ADC_TS,
34 AXP288_ADC_PMIC,
35 AXP288_ADC_GP,
36 AXP288_ADC_BATT_CHRG_I,
37 AXP288_ADC_BATT_DISCHRG_I,
38 AXP288_ADC_BATT_V,
39 AXP288_ADC_NR_CHAN,
40};
41
42struct axp288_adc_info {
43 int irq;
44 struct regmap *regmap;
45};
46
Colin Ian King7ca65742016-01-22 17:49:22 +000047static const struct iio_chan_spec axp288_adc_channels[] = {
Jacob Pande89bd72014-10-06 21:17:15 -070048 {
49 .indexed = 1,
50 .type = IIO_TEMP,
51 .channel = 0,
52 .address = AXP288_TS_ADC_H,
53 .datasheet_name = "TS_PIN",
Jacob Pand0716b02015-04-06 11:38:20 -070054 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
Jacob Pande89bd72014-10-06 21:17:15 -070055 }, {
56 .indexed = 1,
57 .type = IIO_TEMP,
58 .channel = 1,
59 .address = AXP288_PMIC_ADC_H,
60 .datasheet_name = "PMIC_TEMP",
Jacob Pand0716b02015-04-06 11:38:20 -070061 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
Jacob Pande89bd72014-10-06 21:17:15 -070062 }, {
63 .indexed = 1,
64 .type = IIO_TEMP,
65 .channel = 2,
66 .address = AXP288_GP_ADC_H,
67 .datasheet_name = "GPADC",
Jacob Pand0716b02015-04-06 11:38:20 -070068 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
Jacob Pande89bd72014-10-06 21:17:15 -070069 }, {
70 .indexed = 1,
71 .type = IIO_CURRENT,
72 .channel = 3,
73 .address = AXP20X_BATT_CHRG_I_H,
74 .datasheet_name = "BATT_CHG_I",
Jacob Pand0716b02015-04-06 11:38:20 -070075 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
Jacob Pande89bd72014-10-06 21:17:15 -070076 }, {
77 .indexed = 1,
78 .type = IIO_CURRENT,
79 .channel = 4,
80 .address = AXP20X_BATT_DISCHRG_I_H,
81 .datasheet_name = "BATT_DISCHRG_I",
Jacob Pand0716b02015-04-06 11:38:20 -070082 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
Jacob Pande89bd72014-10-06 21:17:15 -070083 }, {
84 .indexed = 1,
85 .type = IIO_VOLTAGE,
86 .channel = 5,
87 .address = AXP20X_BATT_V_H,
88 .datasheet_name = "BATT_V",
Jacob Pand0716b02015-04-06 11:38:20 -070089 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
Jacob Pande89bd72014-10-06 21:17:15 -070090 },
91};
92
93#define AXP288_ADC_MAP(_adc_channel_label, _consumer_dev_name, \
94 _consumer_channel) \
95 { \
96 .adc_channel_label = _adc_channel_label, \
97 .consumer_dev_name = _consumer_dev_name, \
98 .consumer_channel = _consumer_channel, \
99 }
100
101/* for consumer drivers */
102static 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"),
109 {},
110};
111
112static int axp288_adc_read_channel(int *val, unsigned long address,
113 struct regmap *regmap)
114{
115 u8 buf[2];
116
117 if (regmap_bulk_read(regmap, address, buf, 2))
118 return -EIO;
119 *val = (buf[0] << 4) + ((buf[1] >> 4) & 0x0F);
120
121 return IIO_VAL_INT;
122}
123
Jacob Pande89bd72014-10-06 21:17:15 -0700124static int axp288_adc_read_raw(struct iio_dev *indio_dev,
125 struct iio_chan_spec const *chan,
126 int *val, int *val2, long mask)
127{
128 int ret;
129 struct axp288_adc_info *info = iio_priv(indio_dev);
130
131 mutex_lock(&indio_dev->mlock);
132 switch (mask) {
133 case IIO_CHAN_INFO_RAW:
Jacob Pande89bd72014-10-06 21:17:15 -0700134 ret = axp288_adc_read_channel(val, chan->address, info->regmap);
Jacob Pande89bd72014-10-06 21:17:15 -0700135 break;
Jacob Pande89bd72014-10-06 21:17:15 -0700136 default:
137 ret = -EINVAL;
138 }
139 mutex_unlock(&indio_dev->mlock);
140
141 return ret;
142}
143
Jacob Pande89bd72014-10-06 21:17:15 -0700144static const struct iio_info axp288_adc_iio_info = {
145 .read_raw = &axp288_adc_read_raw,
146 .driver_module = THIS_MODULE,
147};
148
149static int axp288_adc_probe(struct platform_device *pdev)
150{
151 int ret;
152 struct axp288_adc_info *info;
153 struct iio_dev *indio_dev;
154 struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
155
156 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
157 if (!indio_dev)
158 return -ENOMEM;
159
160 info = iio_priv(indio_dev);
161 info->irq = platform_get_irq(pdev, 0);
162 if (info->irq < 0) {
163 dev_err(&pdev->dev, "no irq resource?\n");
164 return info->irq;
165 }
166 platform_set_drvdata(pdev, indio_dev);
167 info->regmap = axp20x->regmap;
168 /*
169 * Set ADC to enabled state at all time, including system suspend.
170 * otherwise internal fuel gauge functionality may be affected.
171 */
Hans de Goedeff9b5602016-12-14 14:55:25 +0100172 ret = regmap_write(info->regmap, AXP20X_ADC_EN1, AXP288_ADC_EN_MASK);
Jacob Pande89bd72014-10-06 21:17:15 -0700173 if (ret) {
174 dev_err(&pdev->dev, "unable to enable ADC device\n");
175 return ret;
176 }
177
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);
185 if (ret < 0)
186 return ret;
187
188 ret = iio_device_register(indio_dev);
189 if (ret < 0) {
190 dev_err(&pdev->dev, "unable to register iio device\n");
191 goto err_array_unregister;
192 }
193 return 0;
194
195err_array_unregister:
196 iio_map_array_unregister(indio_dev);
197
198 return ret;
199}
200
201static int axp288_adc_remove(struct platform_device *pdev)
202{
203 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
204
205 iio_device_unregister(indio_dev);
206 iio_map_array_unregister(indio_dev);
207
208 return 0;
209}
210
Krzysztof Kozlowskie6821732015-05-02 00:53:31 +0900211static const struct platform_device_id axp288_adc_id_table[] = {
Aaron Lu29ec0a22014-11-11 11:30:08 -0800212 { .name = "axp288_adc" },
213 {},
214};
215
Jacob Pande89bd72014-10-06 21:17:15 -0700216static struct platform_driver axp288_adc_driver = {
217 .probe = axp288_adc_probe,
218 .remove = axp288_adc_remove,
Aaron Lu29ec0a22014-11-11 11:30:08 -0800219 .id_table = axp288_adc_id_table,
Jacob Pande89bd72014-10-06 21:17:15 -0700220 .driver = {
221 .name = "axp288_adc",
Jacob Pande89bd72014-10-06 21:17:15 -0700222 },
223};
224
Aaron Lu29ec0a22014-11-11 11:30:08 -0800225MODULE_DEVICE_TABLE(platform, axp288_adc_id_table);
226
Jacob Pande89bd72014-10-06 21:17:15 -0700227module_platform_driver(axp288_adc_driver);
228
229MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@linux.intel.com>");
230MODULE_DESCRIPTION("X-Powers AXP288 ADC Driver");
231MODULE_LICENSE("GPL");