2 * Battery charger driver for TI's tps65090
4 * Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved.
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.
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
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/>.
18 #include <linux/delay.h>
19 #include <linux/err.h>
20 #include <linux/freezer.h>
21 #include <linux/init.h>
22 #include <linux/interrupt.h>
23 #include <linux/kernel.h>
24 #include <linux/kthread.h>
25 #include <linux/module.h>
26 #include <linux/of_device.h>
27 #include <linux/platform_device.h>
28 #include <linux/power_supply.h>
29 #include <linux/slab.h>
31 #include <linux/mfd/tps65090.h>
33 #define TPS65090_CHARGER_ENABLE BIT(0)
34 #define TPS65090_VACG BIT(1)
35 #define TPS65090_NOITERM BIT(5)
37 #define POLL_INTERVAL (HZ * 2) /* Used when no irq */
39 struct tps65090_charger {
44 struct task_struct *poll_task;
46 struct power_supply ac;
47 struct tps65090_platform_data *pdata;
50 static enum power_supply_property tps65090_ac_props[] = {
51 POWER_SUPPLY_PROP_ONLINE,
54 static int tps65090_low_chrg_current(struct tps65090_charger *charger)
58 if (charger->passive_mode)
61 ret = tps65090_write(charger->dev->parent, TPS65090_REG_CG_CTRL5,
64 dev_err(charger->dev, "%s(): error reading in register 0x%x\n",
65 __func__, TPS65090_REG_CG_CTRL5);
71 static int tps65090_enable_charging(struct tps65090_charger *charger)
76 if (charger->passive_mode)
79 ret = tps65090_read(charger->dev->parent, TPS65090_REG_CG_CTRL0,
82 dev_err(charger->dev, "%s(): error reading in register 0x%x\n",
83 __func__, TPS65090_REG_CG_CTRL0);
87 ret = tps65090_write(charger->dev->parent, TPS65090_REG_CG_CTRL0,
88 (ctrl0 | TPS65090_CHARGER_ENABLE));
90 dev_err(charger->dev, "%s(): error writing in register 0x%x\n",
91 __func__, TPS65090_REG_CG_CTRL0);
97 static int tps65090_config_charger(struct tps65090_charger *charger)
102 if (charger->passive_mode)
105 if (charger->pdata->enable_low_current_chrg) {
106 ret = tps65090_low_chrg_current(charger);
108 dev_err(charger->dev,
109 "error configuring low charge current\n");
114 /* Enable the VACG interrupt for AC power detect */
115 ret = tps65090_read(charger->dev->parent, TPS65090_REG_INTR_MASK,
118 dev_err(charger->dev, "%s(): error reading in register 0x%x\n",
119 __func__, TPS65090_REG_INTR_MASK);
123 ret = tps65090_write(charger->dev->parent, TPS65090_REG_INTR_MASK,
124 (intrmask | TPS65090_VACG));
126 dev_err(charger->dev, "%s(): error writing in register 0x%x\n",
127 __func__, TPS65090_REG_CG_CTRL0);
134 static int tps65090_ac_get_property(struct power_supply *psy,
135 enum power_supply_property psp,
136 union power_supply_propval *val)
138 struct tps65090_charger *charger = container_of(psy,
139 struct tps65090_charger, ac);
141 if (psp == POWER_SUPPLY_PROP_ONLINE) {
142 val->intval = charger->ac_online;
143 charger->prev_ac_online = charger->ac_online;
149 static irqreturn_t tps65090_charger_isr(int irq, void *dev_id)
151 struct tps65090_charger *charger = dev_id;
156 ret = tps65090_read(charger->dev->parent, TPS65090_REG_CG_STATUS1,
159 dev_err(charger->dev, "%s(): Error in reading reg 0x%x\n",
160 __func__, TPS65090_REG_CG_STATUS1);
164 ret = tps65090_read(charger->dev->parent, TPS65090_REG_INTR_STS,
167 dev_err(charger->dev, "%s(): Error in reading reg 0x%x\n",
168 __func__, TPS65090_REG_INTR_STS);
172 if (intrsts & TPS65090_VACG) {
173 ret = tps65090_enable_charging(charger);
176 charger->ac_online = 1;
178 charger->ac_online = 0;
181 /* Clear interrupts. */
182 if (!charger->passive_mode) {
183 ret = tps65090_write(charger->dev->parent,
184 TPS65090_REG_INTR_STS, 0x00);
186 dev_err(charger->dev,
187 "%s(): Error in writing reg 0x%x\n",
188 __func__, TPS65090_REG_INTR_STS);
192 if (charger->prev_ac_online != charger->ac_online)
193 power_supply_changed(&charger->ac);
198 static struct tps65090_platform_data *
199 tps65090_parse_dt_charger_data(struct platform_device *pdev)
201 struct tps65090_platform_data *pdata;
202 struct device_node *np = pdev->dev.of_node;
205 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
207 dev_err(&pdev->dev, "Memory alloc for tps65090_pdata failed\n");
211 prop = of_property_read_bool(np, "ti,enable-low-current-chrg");
212 pdata->enable_low_current_chrg = prop;
214 pdata->irq_base = -1;
220 static int tps65090_charger_poll_task(void *data)
224 while (!kthread_should_stop()) {
225 schedule_timeout_interruptible(POLL_INTERVAL);
227 tps65090_charger_isr(-1, data);
232 static int tps65090_charger_probe(struct platform_device *pdev)
234 struct tps65090_charger *cdata;
235 struct tps65090_platform_data *pdata;
236 struct power_supply_config psy_cfg = {};
241 pdata = dev_get_platdata(pdev->dev.parent);
243 if (IS_ENABLED(CONFIG_OF) && !pdata && pdev->dev.of_node)
244 pdata = tps65090_parse_dt_charger_data(pdev);
247 dev_err(&pdev->dev, "%s():no platform data available\n",
252 cdata = devm_kzalloc(&pdev->dev, sizeof(*cdata), GFP_KERNEL);
254 dev_err(&pdev->dev, "failed to allocate memory status\n");
258 platform_set_drvdata(pdev, cdata);
260 cdata->dev = &pdev->dev;
261 cdata->pdata = pdata;
263 cdata->ac.name = "tps65090-ac";
264 cdata->ac.type = POWER_SUPPLY_TYPE_MAINS;
265 cdata->ac.get_property = tps65090_ac_get_property;
266 cdata->ac.properties = tps65090_ac_props;
267 cdata->ac.num_properties = ARRAY_SIZE(tps65090_ac_props);
269 psy_cfg.supplied_to = pdata->supplied_to;
270 psy_cfg.num_supplicants = pdata->num_supplicants;
271 psy_cfg.of_node = pdev->dev.of_node;
273 ret = power_supply_register(&pdev->dev, &cdata->ac, &psy_cfg);
275 dev_err(&pdev->dev, "failed: power supply register\n");
279 irq = platform_get_irq(pdev, 0);
284 ret = tps65090_config_charger(cdata);
286 dev_err(&pdev->dev, "charger config failed, err %d\n", ret);
287 goto fail_unregister_supply;
290 /* Check for charger presence */
291 ret = tps65090_read(cdata->dev->parent, TPS65090_REG_CG_STATUS1,
294 dev_err(cdata->dev, "%s(): Error in reading reg 0x%x", __func__,
295 TPS65090_REG_CG_STATUS1);
296 goto fail_unregister_supply;
300 ret = tps65090_enable_charging(cdata);
302 dev_err(cdata->dev, "error enabling charger\n");
303 goto fail_unregister_supply;
305 cdata->ac_online = 1;
306 power_supply_changed(&cdata->ac);
310 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
311 tps65090_charger_isr, 0, "tps65090-charger", cdata);
314 "Unable to register irq %d err %d\n", irq,
316 goto fail_unregister_supply;
319 cdata->poll_task = kthread_run(tps65090_charger_poll_task,
320 cdata, "ktps65090charger");
321 cdata->passive_mode = true;
322 if (IS_ERR(cdata->poll_task)) {
323 ret = PTR_ERR(cdata->poll_task);
325 "Unable to run kthread err %d\n", ret);
326 goto fail_unregister_supply;
332 fail_unregister_supply:
333 power_supply_unregister(&cdata->ac);
338 static int tps65090_charger_remove(struct platform_device *pdev)
340 struct tps65090_charger *cdata = platform_get_drvdata(pdev);
342 if (cdata->irq == -ENXIO)
343 kthread_stop(cdata->poll_task);
344 power_supply_unregister(&cdata->ac);
349 static struct of_device_id of_tps65090_charger_match[] = {
350 { .compatible = "ti,tps65090-charger", },
354 static struct platform_driver tps65090_charger_driver = {
356 .name = "tps65090-charger",
357 .of_match_table = of_tps65090_charger_match,
359 .probe = tps65090_charger_probe,
360 .remove = tps65090_charger_remove,
362 module_platform_driver(tps65090_charger_driver);
364 MODULE_LICENSE("GPL v2");
365 MODULE_AUTHOR("Syed Rafiuddin <srafiuddin@nvidia.com>");
366 MODULE_DESCRIPTION("tps65090 battery charger driver");