1 /*
2 * intel_pmic_xpower.c - XPower AXP288 PMIC operation region driver
3 *
4 * Copyright (C) 2014 Intel Corporation. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16 #include <linux/init.h>
17 #include <linux/acpi.h>
18 #include <linux/mfd/axp20x.h>
19 #include <linux/regmap.h>
20 #include <linux/platform_device.h>
21 #include "intel_pmic.h"
22
23 #define XPOWER_GPADC_LOW 0x5b
24 #define XPOWER_GPI1_CTRL 0x92
25
26 #define GPI1_LDO_MASK GENMASK(2, 0)
27 #define GPI1_LDO_ON (3 << 0)
28 #define GPI1_LDO_OFF (4 << 0)
29
30 #define AXP288_ADC_TS_CURRENT_ON_OFF_MASK GENMASK(1, 0)
31 #define AXP288_ADC_TS_CURRENT_OFF (0 << 0)
32 #define AXP288_ADC_TS_CURRENT_ON_WHEN_CHARGING (1 << 0)
33 #define AXP288_ADC_TS_CURRENT_ON_ONDEMAND (2 << 0)
34 #define AXP288_ADC_TS_CURRENT_ON (3 << 0)
35
36 static struct pmic_table power_table[] = {
37 {
38 .address = 0x00,
39 .reg = 0x13,
40 .bit = 0x05,
41 }, /* ALD1 */
42 {
43 .address = 0x04,
44 .reg = 0x13,
45 .bit = 0x06,
46 }, /* ALD2 */
47 {
48 .address = 0x08,
49 .reg = 0x13,
50 .bit = 0x07,
51 }, /* ALD3 */
52 {
53 .address = 0x0c,
54 .reg = 0x12,
55 .bit = 0x03,
56 }, /* DLD1 */
57 {
58 .address = 0x10,
59 .reg = 0x12,
60 .bit = 0x04,
61 }, /* DLD2 */
62 {
63 .address = 0x14,
64 .reg = 0x12,
65 .bit = 0x05,
66 }, /* DLD3 */
67 {
68 .address = 0x18,
69 .reg = 0x12,
70 .bit = 0x06,
71 }, /* DLD4 */
72 {
73 .address = 0x1c,
74 .reg = 0x12,
75 .bit = 0x00,
76 }, /* ELD1 */
77 {
78 .address = 0x20,
79 .reg = 0x12,
80 .bit = 0x01,
81 }, /* ELD2 */
82 {
83 .address = 0x24,
84 .reg = 0x12,
85 .bit = 0x02,
86 }, /* ELD3 */
87 {
88 .address = 0x28,
89 .reg = 0x13,
90 .bit = 0x02,
91 }, /* FLD1 */
92 {
93 .address = 0x2c,
94 .reg = 0x13,
95 .bit = 0x03,
96 }, /* FLD2 */
97 {
98 .address = 0x30,
99 .reg = 0x13,
100 .bit = 0x04,
101 }, /* FLD3 */
102 {
103 .address = 0x34,
104 .reg = 0x10,
105 .bit = 0x03,
106 }, /* BUC1 */
107 {
108 .address = 0x38,
109 .reg = 0x10,
110 .bit = 0x06,
111 }, /* BUC2 */
112 {
113 .address = 0x3c,
114 .reg = 0x10,
115 .bit = 0x05,
116 }, /* BUC3 */
117 {
118 .address = 0x40,
119 .reg = 0x10,
120 .bit = 0x04,
121 }, /* BUC4 */
122 {
123 .address = 0x44,
124 .reg = 0x10,
125 .bit = 0x01,
126 }, /* BUC5 */
127 {
128 .address = 0x48,
129 .reg = 0x10,
130 .bit = 0x00
131 }, /* BUC6 */
132 {
133 .address = 0x4c,
134 .reg = 0x92,
135 }, /* GPI1 */
136 };
137
138 /* TMP0 - TMP5 are the same, all from GPADC */
139 static struct pmic_table thermal_table[] = {
140 {
141 .address = 0x00,
142 .reg = XPOWER_GPADC_LOW
143 },
144 {
145 .address = 0x0c,
146 .reg = XPOWER_GPADC_LOW
147 },
148 {
149 .address = 0x18,
150 .reg = XPOWER_GPADC_LOW
151 },
152 {
153 .address = 0x24,
154 .reg = XPOWER_GPADC_LOW
155 },
156 {
157 .address = 0x30,
158 .reg = XPOWER_GPADC_LOW
159 },
160 {
161 .address = 0x3c,
162 .reg = XPOWER_GPADC_LOW
163 },
164 };
165
intel_xpower_pmic_get_power(struct regmap * regmap,int reg,int bit,u64 * value)166 static int intel_xpower_pmic_get_power(struct regmap *regmap, int reg,
167 int bit, u64 *value)
168 {
169 int data;
170
171 if (regmap_read(regmap, reg, &data))
172 return -EIO;
173
174 /* GPIO1 LDO regulator needs special handling */
175 if (reg == XPOWER_GPI1_CTRL)
176 *value = ((data & GPI1_LDO_MASK) == GPI1_LDO_ON);
177 else
178 *value = (data & BIT(bit)) ? 1 : 0;
179
180 return 0;
181 }
182
intel_xpower_pmic_update_power(struct regmap * regmap,int reg,int bit,bool on)183 static int intel_xpower_pmic_update_power(struct regmap *regmap, int reg,
184 int bit, bool on)
185 {
186 int data;
187
188 /* GPIO1 LDO regulator needs special handling */
189 if (reg == XPOWER_GPI1_CTRL)
190 return regmap_update_bits(regmap, reg, GPI1_LDO_MASK,
191 on ? GPI1_LDO_ON : GPI1_LDO_OFF);
192
193 if (regmap_read(regmap, reg, &data))
194 return -EIO;
195
196 if (on)
197 data |= BIT(bit);
198 else
199 data &= ~BIT(bit);
200
201 if (regmap_write(regmap, reg, data))
202 return -EIO;
203
204 return 0;
205 }
206
207 /**
208 * intel_xpower_pmic_get_raw_temp(): Get raw temperature reading from the PMIC
209 *
210 * @regmap: regmap of the PMIC device
211 * @reg: register to get the reading
212 *
213 * Return a positive value on success, errno on failure.
214 */
intel_xpower_pmic_get_raw_temp(struct regmap * regmap,int reg)215 static int intel_xpower_pmic_get_raw_temp(struct regmap *regmap, int reg)
216 {
217 int ret, adc_ts_pin_ctrl;
218 u8 buf[2];
219
220 /*
221 * The current-source used for the battery temp-sensor (TS) is shared
222 * with the GPADC. For proper fuel-gauge and charger operation the TS
223 * current-source needs to be permanently on. But to read the GPADC we
224 * need to temporary switch the TS current-source to ondemand, so that
225 * the GPADC can use it, otherwise we will always read an all 0 value.
226 *
227 * Note that the switching from on to on-ondemand is not necessary
228 * when the TS current-source is off (this happens on devices which
229 * do not use the TS-pin).
230 */
231 ret = regmap_read(regmap, AXP288_ADC_TS_PIN_CTRL, &adc_ts_pin_ctrl);
232 if (ret)
233 return ret;
234
235 if (adc_ts_pin_ctrl & AXP288_ADC_TS_CURRENT_ON_OFF_MASK) {
236 ret = regmap_update_bits(regmap, AXP288_ADC_TS_PIN_CTRL,
237 AXP288_ADC_TS_CURRENT_ON_OFF_MASK,
238 AXP288_ADC_TS_CURRENT_ON_ONDEMAND);
239 if (ret)
240 return ret;
241
242 /* Wait a bit after switching the current-source */
243 usleep_range(6000, 10000);
244 }
245
246 ret = regmap_bulk_read(regmap, AXP288_GP_ADC_H, buf, 2);
247 if (ret == 0)
248 ret = (buf[0] << 4) + ((buf[1] >> 4) & 0x0f);
249
250 if (adc_ts_pin_ctrl & AXP288_ADC_TS_CURRENT_ON_OFF_MASK) {
251 regmap_update_bits(regmap, AXP288_ADC_TS_PIN_CTRL,
252 AXP288_ADC_TS_CURRENT_ON_OFF_MASK,
253 AXP288_ADC_TS_CURRENT_ON);
254 }
255
256 return ret;
257 }
258
259 static struct intel_pmic_opregion_data intel_xpower_pmic_opregion_data = {
260 .get_power = intel_xpower_pmic_get_power,
261 .update_power = intel_xpower_pmic_update_power,
262 .get_raw_temp = intel_xpower_pmic_get_raw_temp,
263 .power_table = power_table,
264 .power_table_count = ARRAY_SIZE(power_table),
265 .thermal_table = thermal_table,
266 .thermal_table_count = ARRAY_SIZE(thermal_table),
267 };
268
intel_xpower_pmic_gpio_handler(u32 function,acpi_physical_address address,u32 bit_width,u64 * value,void * handler_context,void * region_context)269 static acpi_status intel_xpower_pmic_gpio_handler(u32 function,
270 acpi_physical_address address, u32 bit_width, u64 *value,
271 void *handler_context, void *region_context)
272 {
273 return AE_OK;
274 }
275
intel_xpower_pmic_opregion_probe(struct platform_device * pdev)276 static int intel_xpower_pmic_opregion_probe(struct platform_device *pdev)
277 {
278 struct device *parent = pdev->dev.parent;
279 struct axp20x_dev *axp20x = dev_get_drvdata(parent);
280 acpi_status status;
281 int result;
282
283 status = acpi_install_address_space_handler(ACPI_HANDLE(parent),
284 ACPI_ADR_SPACE_GPIO, intel_xpower_pmic_gpio_handler,
285 NULL, NULL);
286 if (ACPI_FAILURE(status))
287 return -ENODEV;
288
289 result = intel_pmic_install_opregion_handler(&pdev->dev,
290 ACPI_HANDLE(parent), axp20x->regmap,
291 &intel_xpower_pmic_opregion_data);
292 if (result)
293 acpi_remove_address_space_handler(ACPI_HANDLE(parent),
294 ACPI_ADR_SPACE_GPIO,
295 intel_xpower_pmic_gpio_handler);
296
297 return result;
298 }
299
300 static struct platform_driver intel_xpower_pmic_opregion_driver = {
301 .probe = intel_xpower_pmic_opregion_probe,
302 .driver = {
303 .name = "axp288_pmic_acpi",
304 },
305 };
306
intel_xpower_pmic_opregion_driver_init(void)307 static int __init intel_xpower_pmic_opregion_driver_init(void)
308 {
309 return platform_driver_register(&intel_xpower_pmic_opregion_driver);
310 }
311 device_initcall(intel_xpower_pmic_opregion_driver_init);
312