blob: 0990b2fa6cd8d339a9fa2394c2206f692668a48e [file] [log] [blame]
Thomas Gleixner9952f692019-05-28 10:10:04 -07001// SPDX-License-Identifier: GPL-2.0-only
Rhyland Klein6f8da5d2013-03-12 18:08:09 -04002/*
3 * Battery charger driver for TI's tps65090
4 *
5 * Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved.
6
Rhyland Klein6f8da5d2013-03-12 18:08:09 -04007 */
Manish Badarkhee47bcba2013-09-28 10:02:24 +05308#include <linux/delay.h>
Rhyland Klein6f8da5d2013-03-12 18:08:09 -04009#include <linux/err.h>
Doug Anderson193dcce2014-06-20 14:42:03 -070010#include <linux/freezer.h>
Rhyland Klein6f8da5d2013-03-12 18:08:09 -040011#include <linux/init.h>
12#include <linux/interrupt.h>
13#include <linux/kernel.h>
Doug Anderson193dcce2014-06-20 14:42:03 -070014#include <linux/kthread.h>
Rhyland Klein6f8da5d2013-03-12 18:08:09 -040015#include <linux/module.h>
Manish Badarkhee47bcba2013-09-28 10:02:24 +053016#include <linux/of_device.h>
Rhyland Klein6f8da5d2013-03-12 18:08:09 -040017#include <linux/platform_device.h>
18#include <linux/power_supply.h>
Manish Badarkhee47bcba2013-09-28 10:02:24 +053019#include <linux/slab.h>
20
Rhyland Klein6f8da5d2013-03-12 18:08:09 -040021#include <linux/mfd/tps65090.h>
22
Rhyland Klein6f8da5d2013-03-12 18:08:09 -040023#define TPS65090_CHARGER_ENABLE BIT(0)
24#define TPS65090_VACG BIT(1)
25#define TPS65090_NOITERM BIT(5)
26
Doug Anderson193dcce2014-06-20 14:42:03 -070027#define POLL_INTERVAL (HZ * 2) /* Used when no irq */
28
Rhyland Klein6f8da5d2013-03-12 18:08:09 -040029struct tps65090_charger {
30 struct device *dev;
31 int ac_online;
32 int prev_ac_online;
33 int irq;
Doug Anderson193dcce2014-06-20 14:42:03 -070034 struct task_struct *poll_task;
35 bool passive_mode;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010036 struct power_supply *ac;
Rhyland Klein6f8da5d2013-03-12 18:08:09 -040037 struct tps65090_platform_data *pdata;
38};
39
40static enum power_supply_property tps65090_ac_props[] = {
41 POWER_SUPPLY_PROP_ONLINE,
42};
43
44static int tps65090_low_chrg_current(struct tps65090_charger *charger)
45{
46 int ret;
47
Doug Anderson193dcce2014-06-20 14:42:03 -070048 if (charger->passive_mode)
49 return 0;
50
Rhyland Klein6f8da5d2013-03-12 18:08:09 -040051 ret = tps65090_write(charger->dev->parent, TPS65090_REG_CG_CTRL5,
52 TPS65090_NOITERM);
53 if (ret < 0) {
54 dev_err(charger->dev, "%s(): error reading in register 0x%x\n",
55 __func__, TPS65090_REG_CG_CTRL5);
56 return ret;
57 }
58 return 0;
59}
60
Andrew Chewe2414212013-06-06 14:12:43 -070061static int tps65090_enable_charging(struct tps65090_charger *charger)
Rhyland Klein6f8da5d2013-03-12 18:08:09 -040062{
63 int ret;
64 uint8_t ctrl0 = 0;
65
Doug Anderson193dcce2014-06-20 14:42:03 -070066 if (charger->passive_mode)
67 return 0;
68
Rhyland Klein6f8da5d2013-03-12 18:08:09 -040069 ret = tps65090_read(charger->dev->parent, TPS65090_REG_CG_CTRL0,
70 &ctrl0);
71 if (ret < 0) {
72 dev_err(charger->dev, "%s(): error reading in register 0x%x\n",
73 __func__, TPS65090_REG_CG_CTRL0);
74 return ret;
75 }
76
77 ret = tps65090_write(charger->dev->parent, TPS65090_REG_CG_CTRL0,
78 (ctrl0 | TPS65090_CHARGER_ENABLE));
79 if (ret < 0) {
Andrew Chewe2414212013-06-06 14:12:43 -070080 dev_err(charger->dev, "%s(): error writing in register 0x%x\n",
Rhyland Klein6f8da5d2013-03-12 18:08:09 -040081 __func__, TPS65090_REG_CG_CTRL0);
82 return ret;
83 }
84 return 0;
85}
86
87static int tps65090_config_charger(struct tps65090_charger *charger)
88{
Andrew Chewe2414212013-06-06 14:12:43 -070089 uint8_t intrmask = 0;
Rhyland Klein6f8da5d2013-03-12 18:08:09 -040090 int ret;
91
Doug Anderson193dcce2014-06-20 14:42:03 -070092 if (charger->passive_mode)
93 return 0;
94
Rhyland Klein6f8da5d2013-03-12 18:08:09 -040095 if (charger->pdata->enable_low_current_chrg) {
96 ret = tps65090_low_chrg_current(charger);
97 if (ret < 0) {
98 dev_err(charger->dev,
99 "error configuring low charge current\n");
100 return ret;
101 }
102 }
103
Andrew Chewe2414212013-06-06 14:12:43 -0700104 /* Enable the VACG interrupt for AC power detect */
105 ret = tps65090_read(charger->dev->parent, TPS65090_REG_INTR_MASK,
106 &intrmask);
107 if (ret < 0) {
108 dev_err(charger->dev, "%s(): error reading in register 0x%x\n",
109 __func__, TPS65090_REG_INTR_MASK);
110 return ret;
111 }
112
113 ret = tps65090_write(charger->dev->parent, TPS65090_REG_INTR_MASK,
114 (intrmask | TPS65090_VACG));
115 if (ret < 0) {
116 dev_err(charger->dev, "%s(): error writing in register 0x%x\n",
117 __func__, TPS65090_REG_CG_CTRL0);
118 return ret;
119 }
120
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400121 return 0;
122}
123
124static int tps65090_ac_get_property(struct power_supply *psy,
125 enum power_supply_property psp,
126 union power_supply_propval *val)
127{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100128 struct tps65090_charger *charger = power_supply_get_drvdata(psy);
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400129
130 if (psp == POWER_SUPPLY_PROP_ONLINE) {
131 val->intval = charger->ac_online;
132 charger->prev_ac_online = charger->ac_online;
133 return 0;
134 }
135 return -EINVAL;
136}
137
138static irqreturn_t tps65090_charger_isr(int irq, void *dev_id)
139{
140 struct tps65090_charger *charger = dev_id;
141 int ret;
142 uint8_t status1 = 0;
143 uint8_t intrsts = 0;
144
145 ret = tps65090_read(charger->dev->parent, TPS65090_REG_CG_STATUS1,
146 &status1);
147 if (ret < 0) {
148 dev_err(charger->dev, "%s(): Error in reading reg 0x%x\n",
149 __func__, TPS65090_REG_CG_STATUS1);
150 return IRQ_HANDLED;
151 }
152 msleep(75);
153 ret = tps65090_read(charger->dev->parent, TPS65090_REG_INTR_STS,
154 &intrsts);
155 if (ret < 0) {
156 dev_err(charger->dev, "%s(): Error in reading reg 0x%x\n",
157 __func__, TPS65090_REG_INTR_STS);
158 return IRQ_HANDLED;
159 }
160
161 if (intrsts & TPS65090_VACG) {
Andrew Chewe2414212013-06-06 14:12:43 -0700162 ret = tps65090_enable_charging(charger);
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400163 if (ret < 0)
164 return IRQ_HANDLED;
165 charger->ac_online = 1;
166 } else {
167 charger->ac_online = 0;
168 }
169
Andrew Chewe2414212013-06-06 14:12:43 -0700170 /* Clear interrupts. */
Doug Anderson193dcce2014-06-20 14:42:03 -0700171 if (!charger->passive_mode) {
172 ret = tps65090_write(charger->dev->parent,
173 TPS65090_REG_INTR_STS, 0x00);
174 if (ret < 0) {
175 dev_err(charger->dev,
176 "%s(): Error in writing reg 0x%x\n",
Andrew Chewe2414212013-06-06 14:12:43 -0700177 __func__, TPS65090_REG_INTR_STS);
Doug Anderson193dcce2014-06-20 14:42:03 -0700178 }
Andrew Chewe2414212013-06-06 14:12:43 -0700179 }
180
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400181 if (charger->prev_ac_online != charger->ac_online)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100182 power_supply_changed(charger->ac);
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400183
184 return IRQ_HANDLED;
185}
186
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400187static struct tps65090_platform_data *
188 tps65090_parse_dt_charger_data(struct platform_device *pdev)
189{
190 struct tps65090_platform_data *pdata;
Rhyland Klein594f8f82013-03-21 16:33:05 -0400191 struct device_node *np = pdev->dev.of_node;
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400192 unsigned int prop;
193
194 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
195 if (!pdata) {
196 dev_err(&pdev->dev, "Memory alloc for tps65090_pdata failed\n");
197 return NULL;
198 }
199
200 prop = of_property_read_bool(np, "ti,enable-low-current-chrg");
201 pdata->enable_low_current_chrg = prop;
202
203 pdata->irq_base = -1;
204
205 return pdata;
206
207}
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400208
Doug Anderson193dcce2014-06-20 14:42:03 -0700209static int tps65090_charger_poll_task(void *data)
210{
211 set_freezable();
212
213 while (!kthread_should_stop()) {
214 schedule_timeout_interruptible(POLL_INTERVAL);
215 try_to_freeze();
216 tps65090_charger_isr(-1, data);
217 }
218 return 0;
219}
220
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100221static const struct power_supply_desc tps65090_charger_desc = {
222 .name = "tps65090-ac",
223 .type = POWER_SUPPLY_TYPE_MAINS,
224 .get_property = tps65090_ac_get_property,
225 .properties = tps65090_ac_props,
226 .num_properties = ARRAY_SIZE(tps65090_ac_props),
227};
228
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400229static int tps65090_charger_probe(struct platform_device *pdev)
230{
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400231 struct tps65090_charger *cdata;
232 struct tps65090_platform_data *pdata;
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +0100233 struct power_supply_config psy_cfg = {};
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400234 uint8_t status1 = 0;
235 int ret;
236 int irq;
237
238 pdata = dev_get_platdata(pdev->dev.parent);
239
Manish Badarkhee47bcba2013-09-28 10:02:24 +0530240 if (IS_ENABLED(CONFIG_OF) && !pdata && pdev->dev.of_node)
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400241 pdata = tps65090_parse_dt_charger_data(pdev);
242
243 if (!pdata) {
244 dev_err(&pdev->dev, "%s():no platform data available\n",
245 __func__);
246 return -ENODEV;
247 }
248
249 cdata = devm_kzalloc(&pdev->dev, sizeof(*cdata), GFP_KERNEL);
250 if (!cdata) {
251 dev_err(&pdev->dev, "failed to allocate memory status\n");
252 return -ENOMEM;
253 }
254
Jingoo Hane0835872013-05-23 19:34:39 +0900255 platform_set_drvdata(pdev, cdata);
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400256
257 cdata->dev = &pdev->dev;
258 cdata->pdata = pdata;
259
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +0100260 psy_cfg.supplied_to = pdata->supplied_to;
261 psy_cfg.num_supplicants = pdata->num_supplicants;
262 psy_cfg.of_node = pdev->dev.of_node;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100263 psy_cfg.drv_data = cdata;
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +0100264
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100265 cdata->ac = power_supply_register(&pdev->dev, &tps65090_charger_desc,
266 &psy_cfg);
267 if (IS_ERR(cdata->ac)) {
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400268 dev_err(&pdev->dev, "failed: power supply register\n");
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100269 return PTR_ERR(cdata->ac);
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400270 }
271
272 irq = platform_get_irq(pdev, 0);
Doug Anderson193dcce2014-06-20 14:42:03 -0700273 if (irq < 0)
274 irq = -ENXIO;
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400275 cdata->irq = irq;
276
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400277 ret = tps65090_config_charger(cdata);
278 if (ret < 0) {
279 dev_err(&pdev->dev, "charger config failed, err %d\n", ret);
Wei Yongjun0ed81392013-09-23 18:04:19 +0800280 goto fail_unregister_supply;
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400281 }
282
283 /* Check for charger presence */
284 ret = tps65090_read(cdata->dev->parent, TPS65090_REG_CG_STATUS1,
285 &status1);
286 if (ret < 0) {
287 dev_err(cdata->dev, "%s(): Error in reading reg 0x%x", __func__,
288 TPS65090_REG_CG_STATUS1);
Wei Yongjun0ed81392013-09-23 18:04:19 +0800289 goto fail_unregister_supply;
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400290 }
291
292 if (status1 != 0) {
Andrew Chewe2414212013-06-06 14:12:43 -0700293 ret = tps65090_enable_charging(cdata);
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400294 if (ret < 0) {
295 dev_err(cdata->dev, "error enabling charger\n");
Wei Yongjun0ed81392013-09-23 18:04:19 +0800296 goto fail_unregister_supply;
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400297 }
298 cdata->ac_online = 1;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100299 power_supply_changed(cdata->ac);
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400300 }
301
Doug Anderson193dcce2014-06-20 14:42:03 -0700302 if (irq != -ENXIO) {
303 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
dongjian983d10e2021-03-22 19:21:33 +0800304 tps65090_charger_isr, IRQF_ONESHOT, "tps65090-charger", cdata);
Doug Anderson193dcce2014-06-20 14:42:03 -0700305 if (ret) {
306 dev_err(cdata->dev,
307 "Unable to register irq %d err %d\n", irq,
308 ret);
309 goto fail_unregister_supply;
310 }
311 } else {
312 cdata->poll_task = kthread_run(tps65090_charger_poll_task,
313 cdata, "ktps65090charger");
314 cdata->passive_mode = true;
315 if (IS_ERR(cdata->poll_task)) {
316 ret = PTR_ERR(cdata->poll_task);
317 dev_err(cdata->dev,
318 "Unable to run kthread err %d\n", ret);
319 goto fail_unregister_supply;
320 }
321 }
322
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400323 return 0;
324
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400325fail_unregister_supply:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100326 power_supply_unregister(cdata->ac);
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400327
328 return ret;
329}
330
331static int tps65090_charger_remove(struct platform_device *pdev)
332{
Jingoo Hane0835872013-05-23 19:34:39 +0900333 struct tps65090_charger *cdata = platform_get_drvdata(pdev);
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400334
Doug Anderson193dcce2014-06-20 14:42:03 -0700335 if (cdata->irq == -ENXIO)
336 kthread_stop(cdata->poll_task);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100337 power_supply_unregister(cdata->ac);
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400338
339 return 0;
340}
341
Fabian Frederick8fb08852015-03-16 20:17:12 +0100342static const struct of_device_id of_tps65090_charger_match[] = {
Rhyland Klein594f8f82013-03-21 16:33:05 -0400343 { .compatible = "ti,tps65090-charger", },
344 { /* end */ }
345};
Luis de Bethencourtc72b7bf2015-09-18 18:54:07 +0200346MODULE_DEVICE_TABLE(of, of_tps65090_charger_match);
Rhyland Klein594f8f82013-03-21 16:33:05 -0400347
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400348static struct platform_driver tps65090_charger_driver = {
349 .driver = {
350 .name = "tps65090-charger",
Rhyland Klein594f8f82013-03-21 16:33:05 -0400351 .of_match_table = of_tps65090_charger_match,
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400352 },
353 .probe = tps65090_charger_probe,
354 .remove = tps65090_charger_remove,
355};
356module_platform_driver(tps65090_charger_driver);
357
358MODULE_LICENSE("GPL v2");
359MODULE_AUTHOR("Syed Rafiuddin <srafiuddin@nvidia.com>");
360MODULE_DESCRIPTION("tps65090 battery charger driver");