2 * Battery charger driver for TI BQ24735
4 * Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation;
10 * This program is distributed in the hope that 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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include <linux/err.h>
21 #include <linux/i2c.h>
22 #include <linux/init.h>
23 #include <linux/interrupt.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
27 #include <linux/gpio/consumer.h>
28 #include <linux/power_supply.h>
29 #include <linux/slab.h>
31 #include <linux/power/bq24735-charger.h>
33 #define BQ24735_CHG_OPT 0x12
34 #define BQ24735_CHG_OPT_CHARGE_DISABLE (1 << 0)
35 #define BQ24735_CHG_OPT_AC_PRESENT (1 << 4)
36 #define BQ24735_CHARGE_CURRENT 0x14
37 #define BQ24735_CHARGE_CURRENT_MASK 0x1fc0
38 #define BQ24735_CHARGE_VOLTAGE 0x15
39 #define BQ24735_CHARGE_VOLTAGE_MASK 0x7ff0
40 #define BQ24735_INPUT_CURRENT 0x3f
41 #define BQ24735_INPUT_CURRENT_MASK 0x1f80
42 #define BQ24735_MANUFACTURER_ID 0xfe
43 #define BQ24735_DEVICE_ID 0xff
46 struct power_supply
*charger
;
47 struct power_supply_desc charger_desc
;
48 struct i2c_client
*client
;
49 struct bq24735_platform
*pdata
;
51 struct gpio_desc
*status_gpio
;
52 struct delayed_work poll
;
57 static inline struct bq24735
*to_bq24735(struct power_supply
*psy
)
59 return power_supply_get_drvdata(psy
);
62 static enum power_supply_property bq24735_charger_properties
[] = {
63 POWER_SUPPLY_PROP_STATUS
,
64 POWER_SUPPLY_PROP_ONLINE
,
67 static int bq24735_charger_property_is_writeable(struct power_supply
*psy
,
68 enum power_supply_property psp
)
71 case POWER_SUPPLY_PROP_STATUS
:
80 static inline int bq24735_write_word(struct i2c_client
*client
, u8 reg
,
83 return i2c_smbus_write_word_data(client
, reg
, value
);
86 static inline int bq24735_read_word(struct i2c_client
*client
, u8 reg
)
88 return i2c_smbus_read_word_data(client
, reg
);
91 static int bq24735_update_word(struct i2c_client
*client
, u8 reg
,
97 ret
= bq24735_read_word(client
, reg
);
104 return bq24735_write_word(client
, reg
, tmp
);
107 static int bq24735_config_charger(struct bq24735
*charger
)
109 struct bq24735_platform
*pdata
= charger
->pdata
;
113 if (pdata
->ext_control
)
116 if (pdata
->charge_current
) {
117 value
= pdata
->charge_current
& BQ24735_CHARGE_CURRENT_MASK
;
119 ret
= bq24735_write_word(charger
->client
,
120 BQ24735_CHARGE_CURRENT
, value
);
122 dev_err(&charger
->client
->dev
,
123 "Failed to write charger current : %d\n",
129 if (pdata
->charge_voltage
) {
130 value
= pdata
->charge_voltage
& BQ24735_CHARGE_VOLTAGE_MASK
;
132 ret
= bq24735_write_word(charger
->client
,
133 BQ24735_CHARGE_VOLTAGE
, value
);
135 dev_err(&charger
->client
->dev
,
136 "Failed to write charger voltage : %d\n",
142 if (pdata
->input_current
) {
143 value
= pdata
->input_current
& BQ24735_INPUT_CURRENT_MASK
;
145 ret
= bq24735_write_word(charger
->client
,
146 BQ24735_INPUT_CURRENT
, value
);
148 dev_err(&charger
->client
->dev
,
149 "Failed to write input current : %d\n",
158 static inline int bq24735_enable_charging(struct bq24735
*charger
)
162 if (charger
->pdata
->ext_control
)
165 ret
= bq24735_config_charger(charger
);
169 return bq24735_update_word(charger
->client
, BQ24735_CHG_OPT
,
170 BQ24735_CHG_OPT_CHARGE_DISABLE
, 0);
173 static inline int bq24735_disable_charging(struct bq24735
*charger
)
175 if (charger
->pdata
->ext_control
)
178 return bq24735_update_word(charger
->client
, BQ24735_CHG_OPT
,
179 BQ24735_CHG_OPT_CHARGE_DISABLE
,
180 BQ24735_CHG_OPT_CHARGE_DISABLE
);
183 static bool bq24735_charger_is_present(struct bq24735
*charger
)
185 if (charger
->status_gpio
) {
186 return !gpiod_get_value_cansleep(charger
->status_gpio
);
190 ac
= bq24735_read_word(charger
->client
, BQ24735_CHG_OPT
);
192 dev_dbg(&charger
->client
->dev
,
193 "Failed to read charger options : %d\n",
197 return (ac
& BQ24735_CHG_OPT_AC_PRESENT
) ? true : false;
203 static int bq24735_charger_is_charging(struct bq24735
*charger
)
207 if (!bq24735_charger_is_present(charger
))
210 ret
= bq24735_read_word(charger
->client
, BQ24735_CHG_OPT
);
214 return !(ret
& BQ24735_CHG_OPT_CHARGE_DISABLE
);
217 static void bq24735_update(struct bq24735
*charger
)
219 mutex_lock(&charger
->lock
);
221 if (charger
->charging
&& bq24735_charger_is_present(charger
))
222 bq24735_enable_charging(charger
);
224 bq24735_disable_charging(charger
);
226 mutex_unlock(&charger
->lock
);
228 power_supply_changed(charger
->charger
);
231 static irqreturn_t
bq24735_charger_isr(int irq
, void *devid
)
233 struct power_supply
*psy
= devid
;
234 struct bq24735
*charger
= to_bq24735(psy
);
236 bq24735_update(charger
);
241 static void bq24735_poll(struct work_struct
*work
)
243 struct bq24735
*charger
= container_of(work
, struct bq24735
, poll
.work
);
245 bq24735_update(charger
);
247 schedule_delayed_work(&charger
->poll
,
248 msecs_to_jiffies(charger
->poll_interval
));
251 static int bq24735_charger_get_property(struct power_supply
*psy
,
252 enum power_supply_property psp
,
253 union power_supply_propval
*val
)
255 struct bq24735
*charger
= to_bq24735(psy
);
258 case POWER_SUPPLY_PROP_ONLINE
:
259 val
->intval
= bq24735_charger_is_present(charger
) ? 1 : 0;
261 case POWER_SUPPLY_PROP_STATUS
:
262 switch (bq24735_charger_is_charging(charger
)) {
264 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
267 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
270 val
->intval
= POWER_SUPPLY_STATUS_UNKNOWN
;
281 static int bq24735_charger_set_property(struct power_supply
*psy
,
282 enum power_supply_property psp
,
283 const union power_supply_propval
*val
)
285 struct bq24735
*charger
= to_bq24735(psy
);
289 case POWER_SUPPLY_PROP_STATUS
:
290 switch (val
->intval
) {
291 case POWER_SUPPLY_STATUS_CHARGING
:
292 mutex_lock(&charger
->lock
);
293 charger
->charging
= true;
294 ret
= bq24735_enable_charging(charger
);
295 mutex_unlock(&charger
->lock
);
299 case POWER_SUPPLY_STATUS_DISCHARGING
:
300 case POWER_SUPPLY_STATUS_NOT_CHARGING
:
301 mutex_lock(&charger
->lock
);
302 charger
->charging
= false;
303 ret
= bq24735_disable_charging(charger
);
304 mutex_unlock(&charger
->lock
);
311 power_supply_changed(psy
);
320 static struct bq24735_platform
*bq24735_parse_dt_data(struct i2c_client
*client
)
322 struct bq24735_platform
*pdata
;
323 struct device_node
*np
= client
->dev
.of_node
;
327 pdata
= devm_kzalloc(&client
->dev
, sizeof(*pdata
), GFP_KERNEL
);
329 dev_err(&client
->dev
,
330 "Memory alloc for bq24735 pdata failed\n");
334 ret
= of_property_read_u32(np
, "ti,charge-current", &val
);
336 pdata
->charge_current
= val
;
338 ret
= of_property_read_u32(np
, "ti,charge-voltage", &val
);
340 pdata
->charge_voltage
= val
;
342 ret
= of_property_read_u32(np
, "ti,input-current", &val
);
344 pdata
->input_current
= val
;
346 pdata
->ext_control
= of_property_read_bool(np
, "ti,external-control");
351 static int bq24735_charger_probe(struct i2c_client
*client
,
352 const struct i2c_device_id
*id
)
355 struct bq24735
*charger
;
356 struct power_supply_desc
*supply_desc
;
357 struct power_supply_config psy_cfg
= {};
360 charger
= devm_kzalloc(&client
->dev
, sizeof(*charger
), GFP_KERNEL
);
364 mutex_init(&charger
->lock
);
365 charger
->charging
= true;
366 charger
->pdata
= client
->dev
.platform_data
;
368 if (IS_ENABLED(CONFIG_OF
) && !charger
->pdata
&& client
->dev
.of_node
)
369 charger
->pdata
= bq24735_parse_dt_data(client
);
371 if (!charger
->pdata
) {
372 dev_err(&client
->dev
, "no platform data provided\n");
376 name
= (char *)charger
->pdata
->name
;
378 name
= devm_kasprintf(&client
->dev
, GFP_KERNEL
,
380 dev_name(&client
->dev
));
382 dev_err(&client
->dev
, "Failed to alloc device name\n");
387 charger
->client
= client
;
389 supply_desc
= &charger
->charger_desc
;
391 supply_desc
->name
= name
;
392 supply_desc
->type
= POWER_SUPPLY_TYPE_MAINS
;
393 supply_desc
->properties
= bq24735_charger_properties
;
394 supply_desc
->num_properties
= ARRAY_SIZE(bq24735_charger_properties
);
395 supply_desc
->get_property
= bq24735_charger_get_property
;
396 supply_desc
->set_property
= bq24735_charger_set_property
;
397 supply_desc
->property_is_writeable
=
398 bq24735_charger_property_is_writeable
;
400 psy_cfg
.supplied_to
= charger
->pdata
->supplied_to
;
401 psy_cfg
.num_supplicants
= charger
->pdata
->num_supplicants
;
402 psy_cfg
.of_node
= client
->dev
.of_node
;
403 psy_cfg
.drv_data
= charger
;
405 i2c_set_clientdata(client
, charger
);
407 charger
->status_gpio
= devm_gpiod_get_optional(&client
->dev
,
410 if (IS_ERR(charger
->status_gpio
)) {
411 ret
= PTR_ERR(charger
->status_gpio
);
412 dev_err(&client
->dev
, "Getting gpio failed: %d\n", ret
);
416 if (bq24735_charger_is_present(charger
)) {
417 ret
= bq24735_read_word(client
, BQ24735_MANUFACTURER_ID
);
419 dev_err(&client
->dev
, "Failed to read manufacturer id : %d\n",
422 } else if (ret
!= 0x0040) {
423 dev_err(&client
->dev
,
424 "manufacturer id mismatch. 0x0040 != 0x%04x\n", ret
);
428 ret
= bq24735_read_word(client
, BQ24735_DEVICE_ID
);
430 dev_err(&client
->dev
, "Failed to read device id : %d\n", ret
);
432 } else if (ret
!= 0x000B) {
433 dev_err(&client
->dev
,
434 "device id mismatch. 0x000b != 0x%04x\n", ret
);
438 ret
= bq24735_enable_charging(charger
);
440 dev_err(&client
->dev
, "Failed to enable charging\n");
445 charger
->charger
= devm_power_supply_register(&client
->dev
, supply_desc
,
447 if (IS_ERR(charger
->charger
)) {
448 ret
= PTR_ERR(charger
->charger
);
449 dev_err(&client
->dev
, "Failed to register power supply: %d\n",
455 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
456 NULL
, bq24735_charger_isr
,
457 IRQF_TRIGGER_RISING
|
458 IRQF_TRIGGER_FALLING
|
463 dev_err(&client
->dev
,
464 "Unable to register IRQ %d err %d\n",
469 ret
= device_property_read_u32(&client
->dev
, "poll-interval",
470 &charger
->poll_interval
);
473 if (!charger
->poll_interval
)
476 INIT_DELAYED_WORK(&charger
->poll
, bq24735_poll
);
477 schedule_delayed_work(&charger
->poll
,
478 msecs_to_jiffies(charger
->poll_interval
));
484 static int bq24735_charger_remove(struct i2c_client
*client
)
486 struct bq24735
*charger
= i2c_get_clientdata(client
);
488 if (charger
->poll_interval
)
489 cancel_delayed_work_sync(&charger
->poll
);
494 static const struct i2c_device_id bq24735_charger_id
[] = {
495 { "bq24735-charger", 0 },
498 MODULE_DEVICE_TABLE(i2c
, bq24735_charger_id
);
500 static const struct of_device_id bq24735_match_ids
[] = {
501 { .compatible
= "ti,bq24735", },
504 MODULE_DEVICE_TABLE(of
, bq24735_match_ids
);
506 static struct i2c_driver bq24735_charger_driver
= {
508 .name
= "bq24735-charger",
509 .of_match_table
= bq24735_match_ids
,
511 .probe
= bq24735_charger_probe
,
512 .remove
= bq24735_charger_remove
,
513 .id_table
= bq24735_charger_id
,
516 module_i2c_driver(bq24735_charger_driver
);
518 MODULE_DESCRIPTION("bq24735 battery charging driver");
519 MODULE_AUTHOR("Darbha Sriharsha <dsriharsha@nvidia.com>");
520 MODULE_LICENSE("GPL v2");