1 // SPDX-License-Identifier: GPL-2.0
2 // Battery charger driver for TI's tps65217
4 // Copyright (C) 2015 Collabora Ltd.
5 // Author: Enric Balletbo i Serra <enric.balletbo@collabora.com>
8 * Battery charger driver for TI's tps65217
10 #include <linux/kernel.h>
11 #include <linux/kthread.h>
12 #include <linux/device.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
17 #include <linux/slab.h>
18 #include <linux/err.h>
20 #include <linux/of_device.h>
21 #include <linux/power_supply.h>
23 #include <linux/mfd/core.h>
24 #include <linux/mfd/tps65217.h>
26 #define CHARGER_STATUS_PRESENT (TPS65217_STATUS_ACPWR | TPS65217_STATUS_USBPWR)
27 #define NUM_CHARGER_IRQS 2
28 #define POLL_INTERVAL (HZ * 2)
30 struct tps65217_charger
{
33 struct power_supply
*psy
;
38 struct task_struct
*poll_task
;
41 static enum power_supply_property tps65217_charger_props
[] = {
42 POWER_SUPPLY_PROP_ONLINE
,
45 static int tps65217_config_charger(struct tps65217_charger
*charger
)
50 * tps65217 rev. G, p. 31 (see p. 32 for NTC schematic)
52 * The device can be configured to support a 100k NTC (B = 3960) by
53 * setting the the NTC_TYPE bit in register CHGCONFIG1 to 1. However it
54 * is not recommended to do so. In sleep mode, the charger continues
55 * charging the battery, but all register values are reset to default
56 * values. Therefore, the charger would get the wrong temperature
57 * information. If 100k NTC setting is required, please contact the
60 * ATTENTION, conflicting information, from p. 46
62 * NTC TYPE (for battery temperature measurement)
63 * 0 – 100k (curve 1, B = 3960)
64 * 1 – 10k (curve 2, B = 3480) (default on reset)
67 ret
= tps65217_clear_bits(charger
->tps
, TPS65217_REG_CHGCONFIG1
,
68 TPS65217_CHGCONFIG1_NTC_TYPE
,
69 TPS65217_PROTECT_NONE
);
72 "failed to set 100k NTC setting: %d\n", ret
);
79 static int tps65217_enable_charging(struct tps65217_charger
*charger
)
83 /* charger already enabled */
87 dev_dbg(charger
->dev
, "%s: enable charging\n", __func__
);
88 ret
= tps65217_set_bits(charger
->tps
, TPS65217_REG_CHGCONFIG1
,
89 TPS65217_CHGCONFIG1_CHG_EN
,
90 TPS65217_CHGCONFIG1_CHG_EN
,
91 TPS65217_PROTECT_NONE
);
94 "%s: Error in writing CHG_EN in reg 0x%x: %d\n",
95 __func__
, TPS65217_REG_CHGCONFIG1
, ret
);
104 static int tps65217_charger_get_property(struct power_supply
*psy
,
105 enum power_supply_property psp
,
106 union power_supply_propval
*val
)
108 struct tps65217_charger
*charger
= power_supply_get_drvdata(psy
);
110 if (psp
== POWER_SUPPLY_PROP_ONLINE
) {
111 val
->intval
= charger
->online
;
117 static irqreturn_t
tps65217_charger_irq(int irq
, void *dev
)
120 struct tps65217_charger
*charger
= dev
;
122 charger
->prev_online
= charger
->online
;
124 ret
= tps65217_reg_read(charger
->tps
, TPS65217_REG_STATUS
, &val
);
126 dev_err(charger
->dev
, "%s: Error in reading reg 0x%x\n",
127 __func__
, TPS65217_REG_STATUS
);
131 dev_dbg(charger
->dev
, "%s: 0x%x\n", __func__
, val
);
133 /* check for charger status bit */
134 if (val
& CHARGER_STATUS_PRESENT
) {
135 ret
= tps65217_enable_charging(charger
);
137 dev_err(charger
->dev
,
138 "failed to enable charger: %d\n", ret
);
145 if (charger
->prev_online
!= charger
->online
)
146 power_supply_changed(charger
->psy
);
148 ret
= tps65217_reg_read(charger
->tps
, TPS65217_REG_CHGCONFIG0
, &val
);
150 dev_err(charger
->dev
, "%s: Error in reading reg 0x%x\n",
151 __func__
, TPS65217_REG_CHGCONFIG0
);
155 if (val
& TPS65217_CHGCONFIG0_ACTIVE
)
156 dev_dbg(charger
->dev
, "%s: charger is charging\n", __func__
);
158 dev_dbg(charger
->dev
,
159 "%s: charger is NOT charging\n", __func__
);
164 static int tps65217_charger_poll_task(void *data
)
168 while (!kthread_should_stop()) {
169 schedule_timeout_interruptible(POLL_INTERVAL
);
171 tps65217_charger_irq(-1, data
);
176 static const struct power_supply_desc tps65217_charger_desc
= {
177 .name
= "tps65217-charger",
178 .type
= POWER_SUPPLY_TYPE_MAINS
,
179 .get_property
= tps65217_charger_get_property
,
180 .properties
= tps65217_charger_props
,
181 .num_properties
= ARRAY_SIZE(tps65217_charger_props
),
184 static int tps65217_charger_probe(struct platform_device
*pdev
)
186 struct tps65217
*tps
= dev_get_drvdata(pdev
->dev
.parent
);
187 struct tps65217_charger
*charger
;
188 struct power_supply_config cfg
= {};
189 struct task_struct
*poll_task
;
190 int irq
[NUM_CHARGER_IRQS
];
194 charger
= devm_kzalloc(&pdev
->dev
, sizeof(*charger
), GFP_KERNEL
);
198 platform_set_drvdata(pdev
, charger
);
200 charger
->dev
= &pdev
->dev
;
202 cfg
.of_node
= pdev
->dev
.of_node
;
203 cfg
.drv_data
= charger
;
205 charger
->psy
= devm_power_supply_register(&pdev
->dev
,
206 &tps65217_charger_desc
,
208 if (IS_ERR(charger
->psy
)) {
209 dev_err(&pdev
->dev
, "failed: power supply register\n");
210 return PTR_ERR(charger
->psy
);
213 irq
[0] = platform_get_irq_byname(pdev
, "USB");
214 irq
[1] = platform_get_irq_byname(pdev
, "AC");
216 ret
= tps65217_config_charger(charger
);
218 dev_err(charger
->dev
, "charger config failed, err %d\n", ret
);
222 /* Create a polling thread if an interrupt is invalid */
223 if (irq
[0] < 0 || irq
[1] < 0) {
224 poll_task
= kthread_run(tps65217_charger_poll_task
,
225 charger
, "ktps65217charger");
226 if (IS_ERR(poll_task
)) {
227 ret
= PTR_ERR(poll_task
);
228 dev_err(charger
->dev
,
229 "Unable to run kthread err %d\n", ret
);
233 charger
->poll_task
= poll_task
;
237 /* Create IRQ threads for charger interrupts */
238 for (i
= 0; i
< NUM_CHARGER_IRQS
; i
++) {
239 ret
= devm_request_threaded_irq(&pdev
->dev
, irq
[i
], NULL
,
240 tps65217_charger_irq
,
241 0, "tps65217-charger",
244 dev_err(charger
->dev
,
245 "Unable to register irq %d err %d\n", irq
[i
],
250 /* Check current state */
251 tps65217_charger_irq(-1, charger
);
257 static int tps65217_charger_remove(struct platform_device
*pdev
)
259 struct tps65217_charger
*charger
= platform_get_drvdata(pdev
);
261 if (charger
->poll_task
)
262 kthread_stop(charger
->poll_task
);
267 static const struct of_device_id tps65217_charger_match_table
[] = {
268 { .compatible
= "ti,tps65217-charger", },
271 MODULE_DEVICE_TABLE(of
, tps65217_charger_match_table
);
273 static struct platform_driver tps65217_charger_driver
= {
274 .probe
= tps65217_charger_probe
,
275 .remove
= tps65217_charger_remove
,
277 .name
= "tps65217-charger",
278 .of_match_table
= of_match_ptr(tps65217_charger_match_table
),
282 module_platform_driver(tps65217_charger_driver
);
284 MODULE_LICENSE("GPL v2");
285 MODULE_AUTHOR("Enric Balletbo Serra <enric.balletbo@collabora.com>");
286 MODULE_DESCRIPTION("TPS65217 battery charger driver");