1 // SPDX-License-Identifier: GPL-2.0-only
3 * Battery charger driver for TI's tps65090
5 * Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved.
8 #include <linux/delay.h>
10 #include <linux/freezer.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/kthread.h>
15 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/platform_device.h>
18 #include <linux/power_supply.h>
19 #include <linux/slab.h>
21 #include <linux/mfd/tps65090.h>
23 #define TPS65090_CHARGER_ENABLE BIT(0)
24 #define TPS65090_VACG BIT(1)
25 #define TPS65090_NOITERM BIT(5)
27 #define POLL_INTERVAL (HZ * 2) /* Used when no irq */
29 struct tps65090_charger
{
34 struct task_struct
*poll_task
;
36 struct power_supply
*ac
;
37 struct tps65090_platform_data
*pdata
;
40 static enum power_supply_property tps65090_ac_props
[] = {
41 POWER_SUPPLY_PROP_ONLINE
,
44 static int tps65090_low_chrg_current(struct tps65090_charger
*charger
)
48 if (charger
->passive_mode
)
51 ret
= tps65090_write(charger
->dev
->parent
, TPS65090_REG_CG_CTRL5
,
54 dev_err(charger
->dev
, "%s(): error reading in register 0x%x\n",
55 __func__
, TPS65090_REG_CG_CTRL5
);
61 static int tps65090_enable_charging(struct tps65090_charger
*charger
)
66 if (charger
->passive_mode
)
69 ret
= tps65090_read(charger
->dev
->parent
, TPS65090_REG_CG_CTRL0
,
72 dev_err(charger
->dev
, "%s(): error reading in register 0x%x\n",
73 __func__
, TPS65090_REG_CG_CTRL0
);
77 ret
= tps65090_write(charger
->dev
->parent
, TPS65090_REG_CG_CTRL0
,
78 (ctrl0
| TPS65090_CHARGER_ENABLE
));
80 dev_err(charger
->dev
, "%s(): error writing in register 0x%x\n",
81 __func__
, TPS65090_REG_CG_CTRL0
);
87 static int tps65090_config_charger(struct tps65090_charger
*charger
)
92 if (charger
->passive_mode
)
95 if (charger
->pdata
->enable_low_current_chrg
) {
96 ret
= tps65090_low_chrg_current(charger
);
99 "error configuring low charge current\n");
104 /* Enable the VACG interrupt for AC power detect */
105 ret
= tps65090_read(charger
->dev
->parent
, TPS65090_REG_INTR_MASK
,
108 dev_err(charger
->dev
, "%s(): error reading in register 0x%x\n",
109 __func__
, TPS65090_REG_INTR_MASK
);
113 ret
= tps65090_write(charger
->dev
->parent
, TPS65090_REG_INTR_MASK
,
114 (intrmask
| TPS65090_VACG
));
116 dev_err(charger
->dev
, "%s(): error writing in register 0x%x\n",
117 __func__
, TPS65090_REG_CG_CTRL0
);
124 static int tps65090_ac_get_property(struct power_supply
*psy
,
125 enum power_supply_property psp
,
126 union power_supply_propval
*val
)
128 struct tps65090_charger
*charger
= power_supply_get_drvdata(psy
);
130 if (psp
== POWER_SUPPLY_PROP_ONLINE
) {
131 val
->intval
= charger
->ac_online
;
132 charger
->prev_ac_online
= charger
->ac_online
;
138 static irqreturn_t
tps65090_charger_isr(int irq
, void *dev_id
)
140 struct tps65090_charger
*charger
= dev_id
;
145 ret
= tps65090_read(charger
->dev
->parent
, TPS65090_REG_CG_STATUS1
,
148 dev_err(charger
->dev
, "%s(): Error in reading reg 0x%x\n",
149 __func__
, TPS65090_REG_CG_STATUS1
);
153 ret
= tps65090_read(charger
->dev
->parent
, TPS65090_REG_INTR_STS
,
156 dev_err(charger
->dev
, "%s(): Error in reading reg 0x%x\n",
157 __func__
, TPS65090_REG_INTR_STS
);
161 if (intrsts
& TPS65090_VACG
) {
162 ret
= tps65090_enable_charging(charger
);
165 charger
->ac_online
= 1;
167 charger
->ac_online
= 0;
170 /* Clear interrupts. */
171 if (!charger
->passive_mode
) {
172 ret
= tps65090_write(charger
->dev
->parent
,
173 TPS65090_REG_INTR_STS
, 0x00);
175 dev_err(charger
->dev
,
176 "%s(): Error in writing reg 0x%x\n",
177 __func__
, TPS65090_REG_INTR_STS
);
181 if (charger
->prev_ac_online
!= charger
->ac_online
)
182 power_supply_changed(charger
->ac
);
187 static struct tps65090_platform_data
*
188 tps65090_parse_dt_charger_data(struct platform_device
*pdev
)
190 struct tps65090_platform_data
*pdata
;
191 struct device_node
*np
= pdev
->dev
.of_node
;
194 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
196 dev_err(&pdev
->dev
, "Memory alloc for tps65090_pdata failed\n");
200 prop
= of_property_read_bool(np
, "ti,enable-low-current-chrg");
201 pdata
->enable_low_current_chrg
= prop
;
203 pdata
->irq_base
= -1;
209 static int tps65090_charger_poll_task(void *data
)
213 while (!kthread_should_stop()) {
214 schedule_timeout_interruptible(POLL_INTERVAL
);
216 tps65090_charger_isr(-1, data
);
221 static 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
),
229 static int tps65090_charger_probe(struct platform_device
*pdev
)
231 struct tps65090_charger
*cdata
;
232 struct tps65090_platform_data
*pdata
;
233 struct power_supply_config psy_cfg
= {};
238 pdata
= dev_get_platdata(pdev
->dev
.parent
);
240 if (IS_ENABLED(CONFIG_OF
) && !pdata
&& pdev
->dev
.of_node
)
241 pdata
= tps65090_parse_dt_charger_data(pdev
);
244 dev_err(&pdev
->dev
, "%s():no platform data available\n",
249 cdata
= devm_kzalloc(&pdev
->dev
, sizeof(*cdata
), GFP_KERNEL
);
251 dev_err(&pdev
->dev
, "failed to allocate memory status\n");
255 platform_set_drvdata(pdev
, cdata
);
257 cdata
->dev
= &pdev
->dev
;
258 cdata
->pdata
= pdata
;
260 psy_cfg
.supplied_to
= pdata
->supplied_to
;
261 psy_cfg
.num_supplicants
= pdata
->num_supplicants
;
262 psy_cfg
.of_node
= pdev
->dev
.of_node
;
263 psy_cfg
.drv_data
= cdata
;
265 cdata
->ac
= power_supply_register(&pdev
->dev
, &tps65090_charger_desc
,
267 if (IS_ERR(cdata
->ac
)) {
268 dev_err(&pdev
->dev
, "failed: power supply register\n");
269 return PTR_ERR(cdata
->ac
);
272 irq
= platform_get_irq(pdev
, 0);
277 ret
= tps65090_config_charger(cdata
);
279 dev_err(&pdev
->dev
, "charger config failed, err %d\n", ret
);
280 goto fail_unregister_supply
;
283 /* Check for charger presence */
284 ret
= tps65090_read(cdata
->dev
->parent
, TPS65090_REG_CG_STATUS1
,
287 dev_err(cdata
->dev
, "%s(): Error in reading reg 0x%x", __func__
,
288 TPS65090_REG_CG_STATUS1
);
289 goto fail_unregister_supply
;
293 ret
= tps65090_enable_charging(cdata
);
295 dev_err(cdata
->dev
, "error enabling charger\n");
296 goto fail_unregister_supply
;
298 cdata
->ac_online
= 1;
299 power_supply_changed(cdata
->ac
);
303 ret
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
,
304 tps65090_charger_isr
, 0, "tps65090-charger", cdata
);
307 "Unable to register irq %d err %d\n", irq
,
309 goto fail_unregister_supply
;
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
);
318 "Unable to run kthread err %d\n", ret
);
319 goto fail_unregister_supply
;
325 fail_unregister_supply
:
326 power_supply_unregister(cdata
->ac
);
331 static int tps65090_charger_remove(struct platform_device
*pdev
)
333 struct tps65090_charger
*cdata
= platform_get_drvdata(pdev
);
335 if (cdata
->irq
== -ENXIO
)
336 kthread_stop(cdata
->poll_task
);
337 power_supply_unregister(cdata
->ac
);
342 static const struct of_device_id of_tps65090_charger_match
[] = {
343 { .compatible
= "ti,tps65090-charger", },
346 MODULE_DEVICE_TABLE(of
, of_tps65090_charger_match
);
348 static struct platform_driver tps65090_charger_driver
= {
350 .name
= "tps65090-charger",
351 .of_match_table
= of_tps65090_charger_match
,
353 .probe
= tps65090_charger_probe
,
354 .remove
= tps65090_charger_remove
,
356 module_platform_driver(tps65090_charger_driver
);
358 MODULE_LICENSE("GPL v2");
359 MODULE_AUTHOR("Syed Rafiuddin <srafiuddin@nvidia.com>");
360 MODULE_DESCRIPTION("tps65090 battery charger driver");