]>
Commit | Line | Data |
---|---|---|
6f8da5df RK |
1 | /* |
2 | * Battery charger driver for TI's tps65090 | |
3 | * | |
4 | * Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved. | |
5 | ||
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms and conditions of the GNU General Public License, | |
8 | * version 2, as published by the Free Software Foundation. | |
9 | ||
10 | * This program is distributed in the hope it will be useful, but WITHOUT | |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | * more details. | |
14 | ||
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
e47bcba4 | 18 | #include <linux/delay.h> |
6f8da5df RK |
19 | #include <linux/err.h> |
20 | #include <linux/init.h> | |
21 | #include <linux/interrupt.h> | |
22 | #include <linux/kernel.h> | |
23 | #include <linux/module.h> | |
e47bcba4 | 24 | #include <linux/of_device.h> |
6f8da5df RK |
25 | #include <linux/platform_device.h> |
26 | #include <linux/power_supply.h> | |
e47bcba4 MB |
27 | #include <linux/slab.h> |
28 | ||
6f8da5df RK |
29 | #include <linux/mfd/tps65090.h> |
30 | ||
6f8da5df RK |
31 | #define TPS65090_CHARGER_ENABLE BIT(0) |
32 | #define TPS65090_VACG BIT(1) | |
33 | #define TPS65090_NOITERM BIT(5) | |
34 | ||
35 | struct tps65090_charger { | |
36 | struct device *dev; | |
37 | int ac_online; | |
38 | int prev_ac_online; | |
39 | int irq; | |
40 | struct power_supply ac; | |
41 | struct tps65090_platform_data *pdata; | |
42 | }; | |
43 | ||
44 | static enum power_supply_property tps65090_ac_props[] = { | |
45 | POWER_SUPPLY_PROP_ONLINE, | |
46 | }; | |
47 | ||
48 | static int tps65090_low_chrg_current(struct tps65090_charger *charger) | |
49 | { | |
50 | int ret; | |
51 | ||
52 | ret = tps65090_write(charger->dev->parent, TPS65090_REG_CG_CTRL5, | |
53 | TPS65090_NOITERM); | |
54 | if (ret < 0) { | |
55 | dev_err(charger->dev, "%s(): error reading in register 0x%x\n", | |
56 | __func__, TPS65090_REG_CG_CTRL5); | |
57 | return ret; | |
58 | } | |
59 | return 0; | |
60 | } | |
61 | ||
e2414217 | 62 | static int tps65090_enable_charging(struct tps65090_charger *charger) |
6f8da5df RK |
63 | { |
64 | int ret; | |
65 | uint8_t ctrl0 = 0; | |
66 | ||
67 | ret = tps65090_read(charger->dev->parent, TPS65090_REG_CG_CTRL0, | |
68 | &ctrl0); | |
69 | if (ret < 0) { | |
70 | dev_err(charger->dev, "%s(): error reading in register 0x%x\n", | |
71 | __func__, TPS65090_REG_CG_CTRL0); | |
72 | return ret; | |
73 | } | |
74 | ||
75 | ret = tps65090_write(charger->dev->parent, TPS65090_REG_CG_CTRL0, | |
76 | (ctrl0 | TPS65090_CHARGER_ENABLE)); | |
77 | if (ret < 0) { | |
e2414217 | 78 | dev_err(charger->dev, "%s(): error writing in register 0x%x\n", |
6f8da5df RK |
79 | __func__, TPS65090_REG_CG_CTRL0); |
80 | return ret; | |
81 | } | |
82 | return 0; | |
83 | } | |
84 | ||
85 | static int tps65090_config_charger(struct tps65090_charger *charger) | |
86 | { | |
e2414217 | 87 | uint8_t intrmask = 0; |
6f8da5df RK |
88 | int ret; |
89 | ||
90 | if (charger->pdata->enable_low_current_chrg) { | |
91 | ret = tps65090_low_chrg_current(charger); | |
92 | if (ret < 0) { | |
93 | dev_err(charger->dev, | |
94 | "error configuring low charge current\n"); | |
95 | return ret; | |
96 | } | |
97 | } | |
98 | ||
e2414217 AC |
99 | /* Enable the VACG interrupt for AC power detect */ |
100 | ret = tps65090_read(charger->dev->parent, TPS65090_REG_INTR_MASK, | |
101 | &intrmask); | |
102 | if (ret < 0) { | |
103 | dev_err(charger->dev, "%s(): error reading in register 0x%x\n", | |
104 | __func__, TPS65090_REG_INTR_MASK); | |
105 | return ret; | |
106 | } | |
107 | ||
108 | ret = tps65090_write(charger->dev->parent, TPS65090_REG_INTR_MASK, | |
109 | (intrmask | TPS65090_VACG)); | |
110 | if (ret < 0) { | |
111 | dev_err(charger->dev, "%s(): error writing in register 0x%x\n", | |
112 | __func__, TPS65090_REG_CG_CTRL0); | |
113 | return ret; | |
114 | } | |
115 | ||
6f8da5df RK |
116 | return 0; |
117 | } | |
118 | ||
119 | static int tps65090_ac_get_property(struct power_supply *psy, | |
120 | enum power_supply_property psp, | |
121 | union power_supply_propval *val) | |
122 | { | |
123 | struct tps65090_charger *charger = container_of(psy, | |
124 | struct tps65090_charger, ac); | |
125 | ||
126 | if (psp == POWER_SUPPLY_PROP_ONLINE) { | |
127 | val->intval = charger->ac_online; | |
128 | charger->prev_ac_online = charger->ac_online; | |
129 | return 0; | |
130 | } | |
131 | return -EINVAL; | |
132 | } | |
133 | ||
134 | static irqreturn_t tps65090_charger_isr(int irq, void *dev_id) | |
135 | { | |
136 | struct tps65090_charger *charger = dev_id; | |
137 | int ret; | |
138 | uint8_t status1 = 0; | |
139 | uint8_t intrsts = 0; | |
140 | ||
141 | ret = tps65090_read(charger->dev->parent, TPS65090_REG_CG_STATUS1, | |
142 | &status1); | |
143 | if (ret < 0) { | |
144 | dev_err(charger->dev, "%s(): Error in reading reg 0x%x\n", | |
145 | __func__, TPS65090_REG_CG_STATUS1); | |
146 | return IRQ_HANDLED; | |
147 | } | |
148 | msleep(75); | |
149 | ret = tps65090_read(charger->dev->parent, TPS65090_REG_INTR_STS, | |
150 | &intrsts); | |
151 | if (ret < 0) { | |
152 | dev_err(charger->dev, "%s(): Error in reading reg 0x%x\n", | |
153 | __func__, TPS65090_REG_INTR_STS); | |
154 | return IRQ_HANDLED; | |
155 | } | |
156 | ||
157 | if (intrsts & TPS65090_VACG) { | |
e2414217 | 158 | ret = tps65090_enable_charging(charger); |
6f8da5df RK |
159 | if (ret < 0) |
160 | return IRQ_HANDLED; | |
161 | charger->ac_online = 1; | |
162 | } else { | |
163 | charger->ac_online = 0; | |
164 | } | |
165 | ||
e2414217 AC |
166 | /* Clear interrupts. */ |
167 | ret = tps65090_write(charger->dev->parent, TPS65090_REG_INTR_STS, 0x00); | |
168 | if (ret < 0) { | |
169 | dev_err(charger->dev, "%s(): Error in writing reg 0x%x\n", | |
170 | __func__, TPS65090_REG_INTR_STS); | |
171 | } | |
172 | ||
6f8da5df RK |
173 | if (charger->prev_ac_online != charger->ac_online) |
174 | power_supply_changed(&charger->ac); | |
175 | ||
176 | return IRQ_HANDLED; | |
177 | } | |
178 | ||
6f8da5df RK |
179 | static struct tps65090_platform_data * |
180 | tps65090_parse_dt_charger_data(struct platform_device *pdev) | |
181 | { | |
182 | struct tps65090_platform_data *pdata; | |
594f8f88 | 183 | struct device_node *np = pdev->dev.of_node; |
6f8da5df RK |
184 | unsigned int prop; |
185 | ||
186 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); | |
187 | if (!pdata) { | |
188 | dev_err(&pdev->dev, "Memory alloc for tps65090_pdata failed\n"); | |
189 | return NULL; | |
190 | } | |
191 | ||
192 | prop = of_property_read_bool(np, "ti,enable-low-current-chrg"); | |
193 | pdata->enable_low_current_chrg = prop; | |
194 | ||
195 | pdata->irq_base = -1; | |
196 | ||
197 | return pdata; | |
198 | ||
199 | } | |
6f8da5df RK |
200 | |
201 | static int tps65090_charger_probe(struct platform_device *pdev) | |
202 | { | |
6f8da5df RK |
203 | struct tps65090_charger *cdata; |
204 | struct tps65090_platform_data *pdata; | |
205 | uint8_t status1 = 0; | |
206 | int ret; | |
207 | int irq; | |
208 | ||
209 | pdata = dev_get_platdata(pdev->dev.parent); | |
210 | ||
e47bcba4 | 211 | if (IS_ENABLED(CONFIG_OF) && !pdata && pdev->dev.of_node) |
6f8da5df RK |
212 | pdata = tps65090_parse_dt_charger_data(pdev); |
213 | ||
214 | if (!pdata) { | |
215 | dev_err(&pdev->dev, "%s():no platform data available\n", | |
216 | __func__); | |
217 | return -ENODEV; | |
218 | } | |
219 | ||
220 | cdata = devm_kzalloc(&pdev->dev, sizeof(*cdata), GFP_KERNEL); | |
221 | if (!cdata) { | |
222 | dev_err(&pdev->dev, "failed to allocate memory status\n"); | |
223 | return -ENOMEM; | |
224 | } | |
225 | ||
e0835879 | 226 | platform_set_drvdata(pdev, cdata); |
6f8da5df RK |
227 | |
228 | cdata->dev = &pdev->dev; | |
229 | cdata->pdata = pdata; | |
230 | ||
231 | cdata->ac.name = "tps65090-ac"; | |
232 | cdata->ac.type = POWER_SUPPLY_TYPE_MAINS; | |
233 | cdata->ac.get_property = tps65090_ac_get_property; | |
234 | cdata->ac.properties = tps65090_ac_props; | |
235 | cdata->ac.num_properties = ARRAY_SIZE(tps65090_ac_props); | |
236 | cdata->ac.supplied_to = pdata->supplied_to; | |
237 | cdata->ac.num_supplicants = pdata->num_supplicants; | |
605860bc | 238 | cdata->ac.of_node = pdev->dev.of_node; |
6f8da5df RK |
239 | |
240 | ret = power_supply_register(&pdev->dev, &cdata->ac); | |
241 | if (ret) { | |
242 | dev_err(&pdev->dev, "failed: power supply register\n"); | |
243 | return ret; | |
244 | } | |
245 | ||
246 | irq = platform_get_irq(pdev, 0); | |
247 | if (irq <= 0) { | |
248 | dev_warn(&pdev->dev, "Unable to get charger irq = %d\n", irq); | |
249 | ret = irq; | |
250 | goto fail_unregister_supply; | |
251 | } | |
252 | ||
253 | cdata->irq = irq; | |
254 | ||
255 | ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, | |
256 | tps65090_charger_isr, 0, "tps65090-charger", cdata); | |
257 | if (ret) { | |
258 | dev_err(cdata->dev, "Unable to register irq %d err %d\n", irq, | |
259 | ret); | |
0ed81393 | 260 | goto fail_unregister_supply; |
6f8da5df RK |
261 | } |
262 | ||
263 | ret = tps65090_config_charger(cdata); | |
264 | if (ret < 0) { | |
265 | dev_err(&pdev->dev, "charger config failed, err %d\n", ret); | |
0ed81393 | 266 | goto fail_unregister_supply; |
6f8da5df RK |
267 | } |
268 | ||
269 | /* Check for charger presence */ | |
270 | ret = tps65090_read(cdata->dev->parent, TPS65090_REG_CG_STATUS1, | |
271 | &status1); | |
272 | if (ret < 0) { | |
273 | dev_err(cdata->dev, "%s(): Error in reading reg 0x%x", __func__, | |
274 | TPS65090_REG_CG_STATUS1); | |
0ed81393 | 275 | goto fail_unregister_supply; |
6f8da5df RK |
276 | } |
277 | ||
278 | if (status1 != 0) { | |
e2414217 | 279 | ret = tps65090_enable_charging(cdata); |
6f8da5df RK |
280 | if (ret < 0) { |
281 | dev_err(cdata->dev, "error enabling charger\n"); | |
0ed81393 | 282 | goto fail_unregister_supply; |
6f8da5df RK |
283 | } |
284 | cdata->ac_online = 1; | |
285 | power_supply_changed(&cdata->ac); | |
286 | } | |
287 | ||
288 | return 0; | |
289 | ||
6f8da5df RK |
290 | fail_unregister_supply: |
291 | power_supply_unregister(&cdata->ac); | |
292 | ||
293 | return ret; | |
294 | } | |
295 | ||
296 | static int tps65090_charger_remove(struct platform_device *pdev) | |
297 | { | |
e0835879 | 298 | struct tps65090_charger *cdata = platform_get_drvdata(pdev); |
6f8da5df | 299 | |
6f8da5df RK |
300 | power_supply_unregister(&cdata->ac); |
301 | ||
302 | return 0; | |
303 | } | |
304 | ||
594f8f88 RK |
305 | static struct of_device_id of_tps65090_charger_match[] = { |
306 | { .compatible = "ti,tps65090-charger", }, | |
307 | { /* end */ } | |
308 | }; | |
309 | ||
6f8da5df RK |
310 | static struct platform_driver tps65090_charger_driver = { |
311 | .driver = { | |
312 | .name = "tps65090-charger", | |
594f8f88 | 313 | .of_match_table = of_tps65090_charger_match, |
6f8da5df RK |
314 | .owner = THIS_MODULE, |
315 | }, | |
316 | .probe = tps65090_charger_probe, | |
317 | .remove = tps65090_charger_remove, | |
318 | }; | |
319 | module_platform_driver(tps65090_charger_driver); | |
320 | ||
321 | MODULE_LICENSE("GPL v2"); | |
322 | MODULE_AUTHOR("Syed Rafiuddin <srafiuddin@nvidia.com>"); | |
323 | MODULE_DESCRIPTION("tps65090 battery charger driver"); |