blob: 3e8ba97c8169c137eef4f716d0f8f11ff38e89a8 [file] [log] [blame]
Rhyland Klein6f8da5d2013-03-12 18:08:09 -04001/*
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 */
Manish Badarkhee47bcba2013-09-28 10:02:24 +053018#include <linux/delay.h>
Rhyland Klein6f8da5d2013-03-12 18:08:09 -040019#include <linux/err.h>
Doug Anderson193dcce2014-06-20 14:42:03 -070020#include <linux/freezer.h>
Rhyland Klein6f8da5d2013-03-12 18:08:09 -040021#include <linux/init.h>
22#include <linux/interrupt.h>
23#include <linux/kernel.h>
Doug Anderson193dcce2014-06-20 14:42:03 -070024#include <linux/kthread.h>
Rhyland Klein6f8da5d2013-03-12 18:08:09 -040025#include <linux/module.h>
Manish Badarkhee47bcba2013-09-28 10:02:24 +053026#include <linux/of_device.h>
Rhyland Klein6f8da5d2013-03-12 18:08:09 -040027#include <linux/platform_device.h>
28#include <linux/power_supply.h>
Manish Badarkhee47bcba2013-09-28 10:02:24 +053029#include <linux/slab.h>
30
Rhyland Klein6f8da5d2013-03-12 18:08:09 -040031#include <linux/mfd/tps65090.h>
32
Rhyland Klein6f8da5d2013-03-12 18:08:09 -040033#define TPS65090_CHARGER_ENABLE BIT(0)
34#define TPS65090_VACG BIT(1)
35#define TPS65090_NOITERM BIT(5)
36
Doug Anderson193dcce2014-06-20 14:42:03 -070037#define POLL_INTERVAL (HZ * 2) /* Used when no irq */
38
Rhyland Klein6f8da5d2013-03-12 18:08:09 -040039struct tps65090_charger {
40 struct device *dev;
41 int ac_online;
42 int prev_ac_online;
43 int irq;
Doug Anderson193dcce2014-06-20 14:42:03 -070044 struct task_struct *poll_task;
45 bool passive_mode;
Rhyland Klein6f8da5d2013-03-12 18:08:09 -040046 struct power_supply ac;
47 struct tps65090_platform_data *pdata;
48};
49
50static enum power_supply_property tps65090_ac_props[] = {
51 POWER_SUPPLY_PROP_ONLINE,
52};
53
54static int tps65090_low_chrg_current(struct tps65090_charger *charger)
55{
56 int ret;
57
Doug Anderson193dcce2014-06-20 14:42:03 -070058 if (charger->passive_mode)
59 return 0;
60
Rhyland Klein6f8da5d2013-03-12 18:08:09 -040061 ret = tps65090_write(charger->dev->parent, TPS65090_REG_CG_CTRL5,
62 TPS65090_NOITERM);
63 if (ret < 0) {
64 dev_err(charger->dev, "%s(): error reading in register 0x%x\n",
65 __func__, TPS65090_REG_CG_CTRL5);
66 return ret;
67 }
68 return 0;
69}
70
Andrew Chewe2414212013-06-06 14:12:43 -070071static int tps65090_enable_charging(struct tps65090_charger *charger)
Rhyland Klein6f8da5d2013-03-12 18:08:09 -040072{
73 int ret;
74 uint8_t ctrl0 = 0;
75
Doug Anderson193dcce2014-06-20 14:42:03 -070076 if (charger->passive_mode)
77 return 0;
78
Rhyland Klein6f8da5d2013-03-12 18:08:09 -040079 ret = tps65090_read(charger->dev->parent, TPS65090_REG_CG_CTRL0,
80 &ctrl0);
81 if (ret < 0) {
82 dev_err(charger->dev, "%s(): error reading in register 0x%x\n",
83 __func__, TPS65090_REG_CG_CTRL0);
84 return ret;
85 }
86
87 ret = tps65090_write(charger->dev->parent, TPS65090_REG_CG_CTRL0,
88 (ctrl0 | TPS65090_CHARGER_ENABLE));
89 if (ret < 0) {
Andrew Chewe2414212013-06-06 14:12:43 -070090 dev_err(charger->dev, "%s(): error writing in register 0x%x\n",
Rhyland Klein6f8da5d2013-03-12 18:08:09 -040091 __func__, TPS65090_REG_CG_CTRL0);
92 return ret;
93 }
94 return 0;
95}
96
97static int tps65090_config_charger(struct tps65090_charger *charger)
98{
Andrew Chewe2414212013-06-06 14:12:43 -070099 uint8_t intrmask = 0;
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400100 int ret;
101
Doug Anderson193dcce2014-06-20 14:42:03 -0700102 if (charger->passive_mode)
103 return 0;
104
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400105 if (charger->pdata->enable_low_current_chrg) {
106 ret = tps65090_low_chrg_current(charger);
107 if (ret < 0) {
108 dev_err(charger->dev,
109 "error configuring low charge current\n");
110 return ret;
111 }
112 }
113
Andrew Chewe2414212013-06-06 14:12:43 -0700114 /* Enable the VACG interrupt for AC power detect */
115 ret = tps65090_read(charger->dev->parent, TPS65090_REG_INTR_MASK,
116 &intrmask);
117 if (ret < 0) {
118 dev_err(charger->dev, "%s(): error reading in register 0x%x\n",
119 __func__, TPS65090_REG_INTR_MASK);
120 return ret;
121 }
122
123 ret = tps65090_write(charger->dev->parent, TPS65090_REG_INTR_MASK,
124 (intrmask | TPS65090_VACG));
125 if (ret < 0) {
126 dev_err(charger->dev, "%s(): error writing in register 0x%x\n",
127 __func__, TPS65090_REG_CG_CTRL0);
128 return ret;
129 }
130
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400131 return 0;
132}
133
134static int tps65090_ac_get_property(struct power_supply *psy,
135 enum power_supply_property psp,
136 union power_supply_propval *val)
137{
138 struct tps65090_charger *charger = container_of(psy,
139 struct tps65090_charger, ac);
140
141 if (psp == POWER_SUPPLY_PROP_ONLINE) {
142 val->intval = charger->ac_online;
143 charger->prev_ac_online = charger->ac_online;
144 return 0;
145 }
146 return -EINVAL;
147}
148
149static irqreturn_t tps65090_charger_isr(int irq, void *dev_id)
150{
151 struct tps65090_charger *charger = dev_id;
152 int ret;
153 uint8_t status1 = 0;
154 uint8_t intrsts = 0;
155
156 ret = tps65090_read(charger->dev->parent, TPS65090_REG_CG_STATUS1,
157 &status1);
158 if (ret < 0) {
159 dev_err(charger->dev, "%s(): Error in reading reg 0x%x\n",
160 __func__, TPS65090_REG_CG_STATUS1);
161 return IRQ_HANDLED;
162 }
163 msleep(75);
164 ret = tps65090_read(charger->dev->parent, TPS65090_REG_INTR_STS,
165 &intrsts);
166 if (ret < 0) {
167 dev_err(charger->dev, "%s(): Error in reading reg 0x%x\n",
168 __func__, TPS65090_REG_INTR_STS);
169 return IRQ_HANDLED;
170 }
171
172 if (intrsts & TPS65090_VACG) {
Andrew Chewe2414212013-06-06 14:12:43 -0700173 ret = tps65090_enable_charging(charger);
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400174 if (ret < 0)
175 return IRQ_HANDLED;
176 charger->ac_online = 1;
177 } else {
178 charger->ac_online = 0;
179 }
180
Andrew Chewe2414212013-06-06 14:12:43 -0700181 /* Clear interrupts. */
Doug Anderson193dcce2014-06-20 14:42:03 -0700182 if (!charger->passive_mode) {
183 ret = tps65090_write(charger->dev->parent,
184 TPS65090_REG_INTR_STS, 0x00);
185 if (ret < 0) {
186 dev_err(charger->dev,
187 "%s(): Error in writing reg 0x%x\n",
Andrew Chewe2414212013-06-06 14:12:43 -0700188 __func__, TPS65090_REG_INTR_STS);
Doug Anderson193dcce2014-06-20 14:42:03 -0700189 }
Andrew Chewe2414212013-06-06 14:12:43 -0700190 }
191
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400192 if (charger->prev_ac_online != charger->ac_online)
193 power_supply_changed(&charger->ac);
194
195 return IRQ_HANDLED;
196}
197
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400198static struct tps65090_platform_data *
199 tps65090_parse_dt_charger_data(struct platform_device *pdev)
200{
201 struct tps65090_platform_data *pdata;
Rhyland Klein594f8f82013-03-21 16:33:05 -0400202 struct device_node *np = pdev->dev.of_node;
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400203 unsigned int prop;
204
205 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
206 if (!pdata) {
207 dev_err(&pdev->dev, "Memory alloc for tps65090_pdata failed\n");
208 return NULL;
209 }
210
211 prop = of_property_read_bool(np, "ti,enable-low-current-chrg");
212 pdata->enable_low_current_chrg = prop;
213
214 pdata->irq_base = -1;
215
216 return pdata;
217
218}
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400219
Doug Anderson193dcce2014-06-20 14:42:03 -0700220static int tps65090_charger_poll_task(void *data)
221{
222 set_freezable();
223
224 while (!kthread_should_stop()) {
225 schedule_timeout_interruptible(POLL_INTERVAL);
226 try_to_freeze();
227 tps65090_charger_isr(-1, data);
228 }
229 return 0;
230}
231
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400232static int tps65090_charger_probe(struct platform_device *pdev)
233{
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400234 struct tps65090_charger *cdata;
235 struct tps65090_platform_data *pdata;
236 uint8_t status1 = 0;
237 int ret;
238 int irq;
239
240 pdata = dev_get_platdata(pdev->dev.parent);
241
Manish Badarkhee47bcba2013-09-28 10:02:24 +0530242 if (IS_ENABLED(CONFIG_OF) && !pdata && pdev->dev.of_node)
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400243 pdata = tps65090_parse_dt_charger_data(pdev);
244
245 if (!pdata) {
246 dev_err(&pdev->dev, "%s():no platform data available\n",
247 __func__);
248 return -ENODEV;
249 }
250
251 cdata = devm_kzalloc(&pdev->dev, sizeof(*cdata), GFP_KERNEL);
252 if (!cdata) {
253 dev_err(&pdev->dev, "failed to allocate memory status\n");
254 return -ENOMEM;
255 }
256
Jingoo Hane0835872013-05-23 19:34:39 +0900257 platform_set_drvdata(pdev, cdata);
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400258
259 cdata->dev = &pdev->dev;
260 cdata->pdata = pdata;
261
262 cdata->ac.name = "tps65090-ac";
263 cdata->ac.type = POWER_SUPPLY_TYPE_MAINS;
264 cdata->ac.get_property = tps65090_ac_get_property;
265 cdata->ac.properties = tps65090_ac_props;
266 cdata->ac.num_properties = ARRAY_SIZE(tps65090_ac_props);
267 cdata->ac.supplied_to = pdata->supplied_to;
268 cdata->ac.num_supplicants = pdata->num_supplicants;
Rhyland Klein605860b2013-06-10 17:26:41 -0400269 cdata->ac.of_node = pdev->dev.of_node;
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400270
271 ret = power_supply_register(&pdev->dev, &cdata->ac);
272 if (ret) {
273 dev_err(&pdev->dev, "failed: power supply register\n");
274 return ret;
275 }
276
277 irq = platform_get_irq(pdev, 0);
Doug Anderson193dcce2014-06-20 14:42:03 -0700278 if (irq < 0)
279 irq = -ENXIO;
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400280 cdata->irq = irq;
281
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400282 ret = tps65090_config_charger(cdata);
283 if (ret < 0) {
284 dev_err(&pdev->dev, "charger config failed, err %d\n", ret);
Wei Yongjun0ed81392013-09-23 18:04:19 +0800285 goto fail_unregister_supply;
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400286 }
287
288 /* Check for charger presence */
289 ret = tps65090_read(cdata->dev->parent, TPS65090_REG_CG_STATUS1,
290 &status1);
291 if (ret < 0) {
292 dev_err(cdata->dev, "%s(): Error in reading reg 0x%x", __func__,
293 TPS65090_REG_CG_STATUS1);
Wei Yongjun0ed81392013-09-23 18:04:19 +0800294 goto fail_unregister_supply;
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400295 }
296
297 if (status1 != 0) {
Andrew Chewe2414212013-06-06 14:12:43 -0700298 ret = tps65090_enable_charging(cdata);
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400299 if (ret < 0) {
300 dev_err(cdata->dev, "error enabling charger\n");
Wei Yongjun0ed81392013-09-23 18:04:19 +0800301 goto fail_unregister_supply;
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400302 }
303 cdata->ac_online = 1;
304 power_supply_changed(&cdata->ac);
305 }
306
Doug Anderson193dcce2014-06-20 14:42:03 -0700307 if (irq != -ENXIO) {
308 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
309 tps65090_charger_isr, 0, "tps65090-charger", cdata);
310 if (ret) {
311 dev_err(cdata->dev,
312 "Unable to register irq %d err %d\n", irq,
313 ret);
314 goto fail_unregister_supply;
315 }
316 } else {
317 cdata->poll_task = kthread_run(tps65090_charger_poll_task,
318 cdata, "ktps65090charger");
319 cdata->passive_mode = true;
320 if (IS_ERR(cdata->poll_task)) {
321 ret = PTR_ERR(cdata->poll_task);
322 dev_err(cdata->dev,
323 "Unable to run kthread err %d\n", ret);
324 goto fail_unregister_supply;
325 }
326 }
327
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400328 return 0;
329
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400330fail_unregister_supply:
331 power_supply_unregister(&cdata->ac);
332
333 return ret;
334}
335
336static int tps65090_charger_remove(struct platform_device *pdev)
337{
Jingoo Hane0835872013-05-23 19:34:39 +0900338 struct tps65090_charger *cdata = platform_get_drvdata(pdev);
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400339
Doug Anderson193dcce2014-06-20 14:42:03 -0700340 if (cdata->irq == -ENXIO)
341 kthread_stop(cdata->poll_task);
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400342 power_supply_unregister(&cdata->ac);
343
344 return 0;
345}
346
Rhyland Klein594f8f82013-03-21 16:33:05 -0400347static struct of_device_id of_tps65090_charger_match[] = {
348 { .compatible = "ti,tps65090-charger", },
349 { /* end */ }
350};
351
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400352static struct platform_driver tps65090_charger_driver = {
353 .driver = {
354 .name = "tps65090-charger",
Rhyland Klein594f8f82013-03-21 16:33:05 -0400355 .of_match_table = of_tps65090_charger_match,
Rhyland Klein6f8da5d2013-03-12 18:08:09 -0400356 .owner = THIS_MODULE,
357 },
358 .probe = tps65090_charger_probe,
359 .remove = tps65090_charger_remove,
360};
361module_platform_driver(tps65090_charger_driver);
362
363MODULE_LICENSE("GPL v2");
364MODULE_AUTHOR("Syed Rafiuddin <srafiuddin@nvidia.com>");
365MODULE_DESCRIPTION("tps65090 battery charger driver");