2 * Battery charger driver for TI's tps65217
4 * Copyright (c) 2015, Collabora Ltd.
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/>.
20 * Battery charger driver for TI's tps65217
22 #include <linux/kernel.h>
23 #include <linux/kthread.h>
24 #include <linux/device.h>
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/init.h>
28 #include <linux/interrupt.h>
29 #include <linux/slab.h>
30 #include <linux/err.h>
32 #include <linux/of_device.h>
33 #include <linux/power_supply.h>
35 #include <linux/mfd/core.h>
36 #include <linux/mfd/tps65217.h>
38 #define POLL_INTERVAL (HZ * 2)
40 struct tps65217_charger
{
43 struct power_supply
*ac
;
48 struct task_struct
*poll_task
;
53 static enum power_supply_property tps65217_ac_props
[] = {
54 POWER_SUPPLY_PROP_ONLINE
,
57 static int tps65217_config_charger(struct tps65217_charger
*charger
)
61 dev_dbg(charger
->dev
, "%s\n", __func__
);
64 * tps65217 rev. G, p. 31 (see p. 32 for NTC schematic)
66 * The device can be configured to support a 100k NTC (B = 3960) by
67 * setting the the NTC_TYPE bit in register CHGCONFIG1 to 1. However it
68 * is not recommended to do so. In sleep mode, the charger continues
69 * charging the battery, but all register values are reset to default
70 * values. Therefore, the charger would get the wrong temperature
71 * information. If 100k NTC setting is required, please contact the
74 * ATTENTION, conflicting information, from p. 46
76 * NTC TYPE (for battery temperature measurement)
77 * 0 – 100k (curve 1, B = 3960)
78 * 1 – 10k (curve 2, B = 3480) (default on reset)
81 ret
= tps65217_clear_bits(charger
->tps
, TPS65217_REG_CHGCONFIG1
,
82 TPS65217_CHGCONFIG1_NTC_TYPE
,
83 TPS65217_PROTECT_NONE
);
86 "failed to set 100k NTC setting: %d\n", ret
);
93 static int tps65217_enable_charging(struct tps65217_charger
*charger
)
97 /* charger already enabled */
98 if (charger
->ac_online
)
101 dev_dbg(charger
->dev
, "%s: enable charging\n", __func__
);
102 ret
= tps65217_set_bits(charger
->tps
, TPS65217_REG_CHGCONFIG1
,
103 TPS65217_CHGCONFIG1_CHG_EN
,
104 TPS65217_CHGCONFIG1_CHG_EN
,
105 TPS65217_PROTECT_NONE
);
107 dev_err(charger
->dev
,
108 "%s: Error in writing CHG_EN in reg 0x%x: %d\n",
109 __func__
, TPS65217_REG_CHGCONFIG1
, ret
);
113 charger
->ac_online
= 1;
118 static int tps65217_ac_get_property(struct power_supply
*psy
,
119 enum power_supply_property psp
,
120 union power_supply_propval
*val
)
122 struct tps65217_charger
*charger
= power_supply_get_drvdata(psy
);
124 if (psp
== POWER_SUPPLY_PROP_ONLINE
) {
125 val
->intval
= charger
->ac_online
;
131 static irqreturn_t
tps65217_charger_irq(int irq
, void *dev
)
134 struct tps65217_charger
*charger
= dev
;
136 charger
->prev_ac_online
= charger
->ac_online
;
138 ret
= tps65217_reg_read(charger
->tps
, TPS65217_REG_STATUS
, &val
);
140 dev_err(charger
->dev
, "%s: Error in reading reg 0x%x\n",
141 __func__
, TPS65217_REG_STATUS
);
145 dev_dbg(charger
->dev
, "%s: 0x%x\n", __func__
, val
);
147 /* check for AC status bit */
148 if (val
& TPS65217_STATUS_ACPWR
) {
149 ret
= tps65217_enable_charging(charger
);
151 dev_err(charger
->dev
,
152 "failed to enable charger: %d\n", ret
);
156 charger
->ac_online
= 0;
159 if (charger
->prev_ac_online
!= charger
->ac_online
)
160 power_supply_changed(charger
->ac
);
162 ret
= tps65217_reg_read(charger
->tps
, TPS65217_REG_CHGCONFIG0
, &val
);
164 dev_err(charger
->dev
, "%s: Error in reading reg 0x%x\n",
165 __func__
, TPS65217_REG_CHGCONFIG0
);
169 if (val
& TPS65217_CHGCONFIG0_ACTIVE
)
170 dev_dbg(charger
->dev
, "%s: charger is charging\n", __func__
);
172 dev_dbg(charger
->dev
,
173 "%s: charger is NOT charging\n", __func__
);
178 static int tps65217_charger_poll_task(void *data
)
182 while (!kthread_should_stop()) {
183 schedule_timeout_interruptible(POLL_INTERVAL
);
185 tps65217_charger_irq(-1, data
);
190 static const struct power_supply_desc tps65217_charger_desc
= {
191 .name
= "tps65217-ac",
192 .type
= POWER_SUPPLY_TYPE_MAINS
,
193 .get_property
= tps65217_ac_get_property
,
194 .properties
= tps65217_ac_props
,
195 .num_properties
= ARRAY_SIZE(tps65217_ac_props
),
198 static int tps65217_charger_probe(struct platform_device
*pdev
)
200 struct tps65217
*tps
= dev_get_drvdata(pdev
->dev
.parent
);
201 struct tps65217_charger
*charger
;
202 struct power_supply_config cfg
= {};
206 dev_dbg(&pdev
->dev
, "%s\n", __func__
);
208 charger
= devm_kzalloc(&pdev
->dev
, sizeof(*charger
), GFP_KERNEL
);
212 platform_set_drvdata(pdev
, charger
);
214 charger
->dev
= &pdev
->dev
;
216 cfg
.of_node
= pdev
->dev
.of_node
;
217 cfg
.drv_data
= charger
;
219 charger
->ac
= devm_power_supply_register(&pdev
->dev
,
220 &tps65217_charger_desc
,
222 if (IS_ERR(charger
->ac
)) {
223 dev_err(&pdev
->dev
, "failed: power supply register\n");
224 return PTR_ERR(charger
->ac
);
227 irq
= platform_get_irq_byname(pdev
, "AC");
232 ret
= tps65217_config_charger(charger
);
234 dev_err(charger
->dev
, "charger config failed, err %d\n", ret
);
239 ret
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
,
240 tps65217_charger_irq
,
241 0, "tps65217-charger",
244 dev_err(charger
->dev
,
245 "Unable to register irq %d err %d\n", irq
,
250 /* Check current state */
251 tps65217_charger_irq(irq
, charger
);
253 charger
->poll_task
= kthread_run(tps65217_charger_poll_task
,
254 charger
, "ktps65217charger");
255 if (IS_ERR(charger
->poll_task
)) {
256 ret
= PTR_ERR(charger
->poll_task
);
257 dev_err(charger
->dev
,
258 "Unable to run kthread err %d\n", ret
);
266 static int tps65217_charger_remove(struct platform_device
*pdev
)
268 struct tps65217_charger
*charger
= platform_get_drvdata(pdev
);
270 if (charger
->irq
== -ENXIO
)
271 kthread_stop(charger
->poll_task
);
276 static const struct of_device_id tps65217_charger_match_table
[] = {
277 { .compatible
= "ti,tps65217-charger", },
280 MODULE_DEVICE_TABLE(of
, tps65217_charger_match_table
);
282 static struct platform_driver tps65217_charger_driver
= {
283 .probe
= tps65217_charger_probe
,
284 .remove
= tps65217_charger_remove
,
286 .name
= "tps65217-charger",
287 .of_match_table
= of_match_ptr(tps65217_charger_match_table
),
291 module_platform_driver(tps65217_charger_driver
);
293 MODULE_LICENSE("GPL v2");
294 MODULE_AUTHOR("Enric Balletbo Serra <enric.balletbo@collabora.com>");
295 MODULE_DESCRIPTION("TPS65217 battery charger driver");