GNU Linux-libre 4.14.251-gnu1
[releases.git] / drivers / iio / adc / axp288_adc.c
1 /*
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/dmi.h>
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/device.h>
23 #include <linux/regmap.h>
24 #include <linux/mfd/axp20x.h>
25 #include <linux/platform_device.h>
26
27 #include <linux/iio/iio.h>
28 #include <linux/iio/machine.h>
29 #include <linux/iio/driver.h>
30
31 /*
32  * This mask enables all ADCs except for the battery temp-sensor (TS), that is
33  * left as-is to avoid breaking charging on devices without a temp-sensor.
34  */
35 #define AXP288_ADC_EN_MASK                              0xF0
36 #define AXP288_ADC_TS_ENABLE                            0x01
37
38 #define AXP288_ADC_TS_BIAS_MASK                         GENMASK(5, 4)
39 #define AXP288_ADC_TS_BIAS_20UA                         (0 << 4)
40 #define AXP288_ADC_TS_BIAS_40UA                         (1 << 4)
41 #define AXP288_ADC_TS_BIAS_60UA                         (2 << 4)
42 #define AXP288_ADC_TS_BIAS_80UA                         (3 << 4)
43 #define AXP288_ADC_TS_CURRENT_ON_OFF_MASK               GENMASK(1, 0)
44 #define AXP288_ADC_TS_CURRENT_OFF                       (0 << 0)
45 #define AXP288_ADC_TS_CURRENT_ON_WHEN_CHARGING          (1 << 0)
46 #define AXP288_ADC_TS_CURRENT_ON_ONDEMAND               (2 << 0)
47 #define AXP288_ADC_TS_CURRENT_ON                        (3 << 0)
48
49 enum axp288_adc_id {
50         AXP288_ADC_TS,
51         AXP288_ADC_PMIC,
52         AXP288_ADC_GP,
53         AXP288_ADC_BATT_CHRG_I,
54         AXP288_ADC_BATT_DISCHRG_I,
55         AXP288_ADC_BATT_V,
56         AXP288_ADC_NR_CHAN,
57 };
58
59 struct axp288_adc_info {
60         int irq;
61         struct regmap *regmap;
62         bool ts_enabled;
63 };
64
65 static const struct iio_chan_spec axp288_adc_channels[] = {
66         {
67                 .indexed = 1,
68                 .type = IIO_TEMP,
69                 .channel = 0,
70                 .address = AXP288_TS_ADC_H,
71                 .datasheet_name = "TS_PIN",
72                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
73         }, {
74                 .indexed = 1,
75                 .type = IIO_TEMP,
76                 .channel = 1,
77                 .address = AXP288_PMIC_ADC_H,
78                 .datasheet_name = "PMIC_TEMP",
79                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
80         }, {
81                 .indexed = 1,
82                 .type = IIO_TEMP,
83                 .channel = 2,
84                 .address = AXP288_GP_ADC_H,
85                 .datasheet_name = "GPADC",
86                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
87         }, {
88                 .indexed = 1,
89                 .type = IIO_CURRENT,
90                 .channel = 3,
91                 .address = AXP20X_BATT_CHRG_I_H,
92                 .datasheet_name = "BATT_CHG_I",
93                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
94         }, {
95                 .indexed = 1,
96                 .type = IIO_CURRENT,
97                 .channel = 4,
98                 .address = AXP20X_BATT_DISCHRG_I_H,
99                 .datasheet_name = "BATT_DISCHRG_I",
100                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
101         }, {
102                 .indexed = 1,
103                 .type = IIO_VOLTAGE,
104                 .channel = 5,
105                 .address = AXP20X_BATT_V_H,
106                 .datasheet_name = "BATT_V",
107                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
108         },
109 };
110
111 #define AXP288_ADC_MAP(_adc_channel_label, _consumer_dev_name,  \
112                 _consumer_channel)                              \
113         {                                                       \
114                 .adc_channel_label = _adc_channel_label,        \
115                 .consumer_dev_name = _consumer_dev_name,        \
116                 .consumer_channel = _consumer_channel,          \
117         }
118
119 /* for consumer drivers */
120 static struct iio_map axp288_adc_default_maps[] = {
121         AXP288_ADC_MAP("TS_PIN", "axp288-batt", "axp288-batt-temp"),
122         AXP288_ADC_MAP("PMIC_TEMP", "axp288-pmic", "axp288-pmic-temp"),
123         AXP288_ADC_MAP("GPADC", "axp288-gpadc", "axp288-system-temp"),
124         AXP288_ADC_MAP("BATT_CHG_I", "axp288-chrg", "axp288-chrg-curr"),
125         AXP288_ADC_MAP("BATT_DISCHRG_I", "axp288-chrg", "axp288-chrg-d-curr"),
126         AXP288_ADC_MAP("BATT_V", "axp288-batt", "axp288-batt-volt"),
127         {},
128 };
129
130 static int axp288_adc_read_channel(int *val, unsigned long address,
131                                 struct regmap *regmap)
132 {
133         u8 buf[2];
134
135         if (regmap_bulk_read(regmap, address, buf, 2))
136                 return -EIO;
137         *val = (buf[0] << 4) + ((buf[1] >> 4) & 0x0F);
138
139         return IIO_VAL_INT;
140 }
141
142 /*
143  * The current-source used for the battery temp-sensor (TS) is shared
144  * with the GPADC. For proper fuel-gauge and charger operation the TS
145  * current-source needs to be permanently on. But to read the GPADC we
146  * need to temporary switch the TS current-source to ondemand, so that
147  * the GPADC can use it, otherwise we will always read an all 0 value.
148  */
149 static int axp288_adc_set_ts(struct axp288_adc_info *info,
150                              unsigned int mode, unsigned long address)
151 {
152         int ret;
153
154         /* No need to switch the current-source if the TS pin is disabled */
155         if (!info->ts_enabled)
156                 return 0;
157
158         /* Channels other than GPADC do not need the current source */
159         if (address != AXP288_GP_ADC_H)
160                 return 0;
161
162         ret = regmap_update_bits(info->regmap, AXP288_ADC_TS_PIN_CTRL,
163                                  AXP288_ADC_TS_CURRENT_ON_OFF_MASK, mode);
164         if (ret)
165                 return ret;
166
167         /* When switching to the GPADC pin give things some time to settle */
168         if (mode == AXP288_ADC_TS_CURRENT_ON_ONDEMAND)
169                 usleep_range(6000, 10000);
170
171         return 0;
172 }
173
174 static int axp288_adc_read_raw(struct iio_dev *indio_dev,
175                         struct iio_chan_spec const *chan,
176                         int *val, int *val2, long mask)
177 {
178         int ret;
179         struct axp288_adc_info *info = iio_priv(indio_dev);
180
181         mutex_lock(&indio_dev->mlock);
182         switch (mask) {
183         case IIO_CHAN_INFO_RAW:
184                 if (axp288_adc_set_ts(info, AXP288_ADC_TS_CURRENT_ON_ONDEMAND,
185                                         chan->address)) {
186                         dev_err(&indio_dev->dev, "GPADC mode\n");
187                         ret = -EINVAL;
188                         break;
189                 }
190                 ret = axp288_adc_read_channel(val, chan->address, info->regmap);
191                 if (axp288_adc_set_ts(info, AXP288_ADC_TS_CURRENT_ON,
192                                                 chan->address))
193                         dev_err(&indio_dev->dev, "TS pin restore\n");
194                 break;
195         default:
196                 ret = -EINVAL;
197         }
198         mutex_unlock(&indio_dev->mlock);
199
200         return ret;
201 }
202
203 /*
204  * We rely on the machine's firmware to correctly setup the TS pin bias current
205  * at boot. This lists systems with broken fw where we need to set it ourselves.
206  */
207 static const struct dmi_system_id axp288_adc_ts_bias_override[] = {
208         {
209                 /* Lenovo Ideapad 100S (11 inch) */
210                 .matches = {
211                   DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
212                   DMI_MATCH(DMI_PRODUCT_VERSION, "Lenovo ideapad 100S-11IBY"),
213                 },
214                 .driver_data = (void *)(uintptr_t)AXP288_ADC_TS_BIAS_80UA,
215         },
216         {}
217 };
218
219 static int axp288_adc_initialize(struct axp288_adc_info *info)
220 {
221         const struct dmi_system_id *bias_override;
222         int ret, adc_enable_val;
223
224         bias_override = dmi_first_match(axp288_adc_ts_bias_override);
225         if (bias_override) {
226                 ret = regmap_update_bits(info->regmap, AXP288_ADC_TS_PIN_CTRL,
227                                          AXP288_ADC_TS_BIAS_MASK,
228                                          (uintptr_t)bias_override->driver_data);
229                 if (ret)
230                         return ret;
231         }
232
233         /*
234          * Determine if the TS pin is enabled and set the TS current-source
235          * accordingly.
236          */
237         ret = regmap_read(info->regmap, AXP20X_ADC_EN1, &adc_enable_val);
238         if (ret)
239                 return ret;
240
241         if (adc_enable_val & AXP288_ADC_TS_ENABLE) {
242                 info->ts_enabled = true;
243                 ret = regmap_update_bits(info->regmap, AXP288_ADC_TS_PIN_CTRL,
244                                          AXP288_ADC_TS_CURRENT_ON_OFF_MASK,
245                                          AXP288_ADC_TS_CURRENT_ON);
246         } else {
247                 info->ts_enabled = false;
248                 ret = regmap_update_bits(info->regmap, AXP288_ADC_TS_PIN_CTRL,
249                                          AXP288_ADC_TS_CURRENT_ON_OFF_MASK,
250                                          AXP288_ADC_TS_CURRENT_OFF);
251         }
252         if (ret)
253                 return ret;
254
255         /* Turn on the ADC for all channels except TS, leave TS as is */
256         return regmap_update_bits(info->regmap, AXP20X_ADC_EN1,
257                                   AXP288_ADC_EN_MASK, AXP288_ADC_EN_MASK);
258 }
259
260 static const struct iio_info axp288_adc_iio_info = {
261         .read_raw = &axp288_adc_read_raw,
262         .driver_module = THIS_MODULE,
263 };
264
265 static int axp288_adc_probe(struct platform_device *pdev)
266 {
267         int ret;
268         struct axp288_adc_info *info;
269         struct iio_dev *indio_dev;
270         struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
271
272         indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*info));
273         if (!indio_dev)
274                 return -ENOMEM;
275
276         info = iio_priv(indio_dev);
277         info->irq = platform_get_irq(pdev, 0);
278         if (info->irq < 0) {
279                 dev_err(&pdev->dev, "no irq resource?\n");
280                 return info->irq;
281         }
282         platform_set_drvdata(pdev, indio_dev);
283         info->regmap = axp20x->regmap;
284         /*
285          * Set ADC to enabled state at all time, including system suspend.
286          * otherwise internal fuel gauge functionality may be affected.
287          */
288         ret = axp288_adc_initialize(info);
289         if (ret) {
290                 dev_err(&pdev->dev, "unable to enable ADC device\n");
291                 return ret;
292         }
293
294         indio_dev->dev.parent = &pdev->dev;
295         indio_dev->name = pdev->name;
296         indio_dev->channels = axp288_adc_channels;
297         indio_dev->num_channels = ARRAY_SIZE(axp288_adc_channels);
298         indio_dev->info = &axp288_adc_iio_info;
299         indio_dev->modes = INDIO_DIRECT_MODE;
300         ret = iio_map_array_register(indio_dev, axp288_adc_default_maps);
301         if (ret < 0)
302                 return ret;
303
304         ret = iio_device_register(indio_dev);
305         if (ret < 0) {
306                 dev_err(&pdev->dev, "unable to register iio device\n");
307                 goto err_array_unregister;
308         }
309         return 0;
310
311 err_array_unregister:
312         iio_map_array_unregister(indio_dev);
313
314         return ret;
315 }
316
317 static int axp288_adc_remove(struct platform_device *pdev)
318 {
319         struct iio_dev *indio_dev = platform_get_drvdata(pdev);
320
321         iio_device_unregister(indio_dev);
322         iio_map_array_unregister(indio_dev);
323
324         return 0;
325 }
326
327 static const struct platform_device_id axp288_adc_id_table[] = {
328         { .name = "axp288_adc" },
329         {},
330 };
331
332 static struct platform_driver axp288_adc_driver = {
333         .probe = axp288_adc_probe,
334         .remove = axp288_adc_remove,
335         .id_table = axp288_adc_id_table,
336         .driver = {
337                 .name = "axp288_adc",
338         },
339 };
340
341 MODULE_DEVICE_TABLE(platform, axp288_adc_id_table);
342
343 module_platform_driver(axp288_adc_driver);
344
345 MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@linux.intel.com>");
346 MODULE_DESCRIPTION("X-Powers AXP288 ADC Driver");
347 MODULE_LICENSE("GPL");