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/gpio.h>
22 #include <linux/i2c.h>
23 #include <linux/init.h>
24 #include <linux/interrupt.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
28 #include <linux/gpio/consumer.h>
29 #include <linux/power_supply.h>
30 #include <linux/slab.h>
32 #include <linux/power/bq24735-charger.h>
34 #define BQ24735_CHG_OPT 0x12
35 #define BQ24735_CHG_OPT_CHARGE_DISABLE (1 << 0)
36 #define BQ24735_CHG_OPT_AC_PRESENT (1 << 4)
37 #define BQ24735_CHARGE_CURRENT 0x14
38 #define BQ24735_CHARGE_CURRENT_MASK 0x1fc0
39 #define BQ24735_CHARGE_VOLTAGE 0x15
40 #define BQ24735_CHARGE_VOLTAGE_MASK 0x7ff0
41 #define BQ24735_INPUT_CURRENT 0x3f
42 #define BQ24735_INPUT_CURRENT_MASK 0x1f80
43 #define BQ24735_MANUFACTURER_ID 0xfe
44 #define BQ24735_DEVICE_ID 0xff
47 struct power_supply
*charger
;
48 struct power_supply_desc charger_desc
;
49 struct i2c_client
*client
;
50 struct bq24735_platform
*pdata
;
52 struct gpio_desc
*status_gpio
;
53 struct delayed_work poll
;
58 static inline struct bq24735
*to_bq24735(struct power_supply
*psy
)
60 return power_supply_get_drvdata(psy
);
63 static enum power_supply_property bq24735_charger_properties
[] = {
64 POWER_SUPPLY_PROP_STATUS
,
65 POWER_SUPPLY_PROP_ONLINE
,
68 static int bq24735_charger_property_is_writeable(struct power_supply
*psy
,
69 enum power_supply_property psp
)
72 case POWER_SUPPLY_PROP_STATUS
:
81 static inline int bq24735_write_word(struct i2c_client
*client
, u8 reg
,
84 return i2c_smbus_write_word_data(client
, reg
, value
);
87 static inline int bq24735_read_word(struct i2c_client
*client
, u8 reg
)
89 return i2c_smbus_read_word_data(client
, reg
);
92 static int bq24735_update_word(struct i2c_client
*client
, u8 reg
,
98 ret
= bq24735_read_word(client
, reg
);
105 return bq24735_write_word(client
, reg
, tmp
);
108 static int bq24735_config_charger(struct bq24735
*charger
)
110 struct bq24735_platform
*pdata
= charger
->pdata
;
114 if (pdata
->ext_control
)
117 if (pdata
->charge_current
) {
118 value
= pdata
->charge_current
& BQ24735_CHARGE_CURRENT_MASK
;
120 ret
= bq24735_write_word(charger
->client
,
121 BQ24735_CHARGE_CURRENT
, value
);
123 dev_err(&charger
->client
->dev
,
124 "Failed to write charger current : %d\n",
130 if (pdata
->charge_voltage
) {
131 value
= pdata
->charge_voltage
& BQ24735_CHARGE_VOLTAGE_MASK
;
133 ret
= bq24735_write_word(charger
->client
,
134 BQ24735_CHARGE_VOLTAGE
, value
);
136 dev_err(&charger
->client
->dev
,
137 "Failed to write charger voltage : %d\n",
143 if (pdata
->input_current
) {
144 value
= pdata
->input_current
& BQ24735_INPUT_CURRENT_MASK
;
146 ret
= bq24735_write_word(charger
->client
,
147 BQ24735_INPUT_CURRENT
, value
);
149 dev_err(&charger
->client
->dev
,
150 "Failed to write input current : %d\n",
159 static inline int bq24735_enable_charging(struct bq24735
*charger
)
163 if (charger
->pdata
->ext_control
)
166 ret
= bq24735_config_charger(charger
);
170 return bq24735_update_word(charger
->client
, BQ24735_CHG_OPT
,
171 BQ24735_CHG_OPT_CHARGE_DISABLE
, 0);
174 static inline int bq24735_disable_charging(struct bq24735
*charger
)
176 if (charger
->pdata
->ext_control
)
179 return bq24735_update_word(charger
->client
, BQ24735_CHG_OPT
,
180 BQ24735_CHG_OPT_CHARGE_DISABLE
,
181 BQ24735_CHG_OPT_CHARGE_DISABLE
);
184 static bool bq24735_charger_is_present(struct bq24735
*charger
)
186 if (charger
->status_gpio
) {
187 return !gpiod_get_value_cansleep(charger
->status_gpio
);
191 ac
= bq24735_read_word(charger
->client
, BQ24735_CHG_OPT
);
193 dev_dbg(&charger
->client
->dev
,
194 "Failed to read charger options : %d\n",
198 return (ac
& BQ24735_CHG_OPT_AC_PRESENT
) ? true : false;
204 static int bq24735_charger_is_charging(struct bq24735
*charger
)
208 if (!bq24735_charger_is_present(charger
))
211 ret
= bq24735_read_word(charger
->client
, BQ24735_CHG_OPT
);
215 return !(ret
& BQ24735_CHG_OPT_CHARGE_DISABLE
);
218 static void bq24735_update(struct bq24735
*charger
)
220 mutex_lock(&charger
->lock
);
222 if (charger
->charging
&& bq24735_charger_is_present(charger
))
223 bq24735_enable_charging(charger
);
225 bq24735_disable_charging(charger
);
227 mutex_unlock(&charger
->lock
);
229 power_supply_changed(charger
->charger
);
232 static irqreturn_t
bq24735_charger_isr(int irq
, void *devid
)
234 struct power_supply
*psy
= devid
;
235 struct bq24735
*charger
= to_bq24735(psy
);
237 bq24735_update(charger
);
242 static void bq24735_poll(struct work_struct
*work
)
244 struct bq24735
*charger
= container_of(work
, struct bq24735
, poll
.work
);
246 bq24735_update(charger
);
248 schedule_delayed_work(&charger
->poll
,
249 msecs_to_jiffies(charger
->poll_interval
));
252 static int bq24735_charger_get_property(struct power_supply
*psy
,
253 enum power_supply_property psp
,
254 union power_supply_propval
*val
)
256 struct bq24735
*charger
= to_bq24735(psy
);
259 case POWER_SUPPLY_PROP_ONLINE
:
260 val
->intval
= bq24735_charger_is_present(charger
) ? 1 : 0;
262 case POWER_SUPPLY_PROP_STATUS
:
263 switch (bq24735_charger_is_charging(charger
)) {
265 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
268 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
271 val
->intval
= POWER_SUPPLY_STATUS_UNKNOWN
;
282 static int bq24735_charger_set_property(struct power_supply
*psy
,
283 enum power_supply_property psp
,
284 const union power_supply_propval
*val
)
286 struct bq24735
*charger
= to_bq24735(psy
);
290 case POWER_SUPPLY_PROP_STATUS
:
291 switch (val
->intval
) {
292 case POWER_SUPPLY_STATUS_CHARGING
:
293 mutex_lock(&charger
->lock
);
294 charger
->charging
= true;
295 ret
= bq24735_enable_charging(charger
);
296 mutex_unlock(&charger
->lock
);
300 case POWER_SUPPLY_STATUS_DISCHARGING
:
301 case POWER_SUPPLY_STATUS_NOT_CHARGING
:
302 mutex_lock(&charger
->lock
);
303 charger
->charging
= false;
304 ret
= bq24735_disable_charging(charger
);
305 mutex_unlock(&charger
->lock
);
312 power_supply_changed(psy
);
321 static struct bq24735_platform
*bq24735_parse_dt_data(struct i2c_client
*client
)
323 struct bq24735_platform
*pdata
;
324 struct device_node
*np
= client
->dev
.of_node
;
328 pdata
= devm_kzalloc(&client
->dev
, sizeof(*pdata
), GFP_KERNEL
);
330 dev_err(&client
->dev
,
331 "Memory alloc for bq24735 pdata failed\n");
335 ret
= of_property_read_u32(np
, "ti,charge-current", &val
);
337 pdata
->charge_current
= val
;
339 ret
= of_property_read_u32(np
, "ti,charge-voltage", &val
);
341 pdata
->charge_voltage
= val
;
343 ret
= of_property_read_u32(np
, "ti,input-current", &val
);
345 pdata
->input_current
= val
;
347 pdata
->ext_control
= of_property_read_bool(np
, "ti,external-control");
352 static int bq24735_charger_probe(struct i2c_client
*client
,
353 const struct i2c_device_id
*id
)
356 struct bq24735
*charger
;
357 struct power_supply_desc
*supply_desc
;
358 struct power_supply_config psy_cfg
= {};
361 charger
= devm_kzalloc(&client
->dev
, sizeof(*charger
), GFP_KERNEL
);
365 mutex_init(&charger
->lock
);
366 charger
->charging
= true;
367 charger
->pdata
= client
->dev
.platform_data
;
369 if (IS_ENABLED(CONFIG_OF
) && !charger
->pdata
&& client
->dev
.of_node
)
370 charger
->pdata
= bq24735_parse_dt_data(client
);
372 if (!charger
->pdata
) {
373 dev_err(&client
->dev
, "no platform data provided\n");
377 name
= (char *)charger
->pdata
->name
;
379 name
= devm_kasprintf(&client
->dev
, GFP_KERNEL
,
381 dev_name(&client
->dev
));
383 dev_err(&client
->dev
, "Failed to alloc device name\n");
388 charger
->client
= client
;
390 supply_desc
= &charger
->charger_desc
;
392 supply_desc
->name
= name
;
393 supply_desc
->type
= POWER_SUPPLY_TYPE_MAINS
;
394 supply_desc
->properties
= bq24735_charger_properties
;
395 supply_desc
->num_properties
= ARRAY_SIZE(bq24735_charger_properties
);
396 supply_desc
->get_property
= bq24735_charger_get_property
;
397 supply_desc
->set_property
= bq24735_charger_set_property
;
398 supply_desc
->property_is_writeable
=
399 bq24735_charger_property_is_writeable
;
401 psy_cfg
.supplied_to
= charger
->pdata
->supplied_to
;
402 psy_cfg
.num_supplicants
= charger
->pdata
->num_supplicants
;
403 psy_cfg
.of_node
= client
->dev
.of_node
;
404 psy_cfg
.drv_data
= charger
;
406 i2c_set_clientdata(client
, charger
);
408 charger
->status_gpio
= devm_gpiod_get_optional(&client
->dev
,
411 if (IS_ERR(charger
->status_gpio
)) {
412 ret
= PTR_ERR(charger
->status_gpio
);
413 dev_err(&client
->dev
, "Getting gpio failed: %d\n", ret
);
417 if (bq24735_charger_is_present(charger
)) {
418 ret
= bq24735_read_word(client
, BQ24735_MANUFACTURER_ID
);
420 dev_err(&client
->dev
, "Failed to read manufacturer id : %d\n",
423 } else if (ret
!= 0x0040) {
424 dev_err(&client
->dev
,
425 "manufacturer id mismatch. 0x0040 != 0x%04x\n", ret
);
429 ret
= bq24735_read_word(client
, BQ24735_DEVICE_ID
);
431 dev_err(&client
->dev
, "Failed to read device id : %d\n", ret
);
433 } else if (ret
!= 0x000B) {
434 dev_err(&client
->dev
,
435 "device id mismatch. 0x000b != 0x%04x\n", ret
);
439 ret
= bq24735_enable_charging(charger
);
441 dev_err(&client
->dev
, "Failed to enable charging\n");
446 charger
->charger
= devm_power_supply_register(&client
->dev
, supply_desc
,
448 if (IS_ERR(charger
->charger
)) {
449 ret
= PTR_ERR(charger
->charger
);
450 dev_err(&client
->dev
, "Failed to register power supply: %d\n",
456 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
457 NULL
, bq24735_charger_isr
,
458 IRQF_TRIGGER_RISING
|
459 IRQF_TRIGGER_FALLING
|
464 dev_err(&client
->dev
,
465 "Unable to register IRQ %d err %d\n",
470 ret
= device_property_read_u32(&client
->dev
, "poll-interval",
471 &charger
->poll_interval
);
474 if (!charger
->poll_interval
)
477 INIT_DELAYED_WORK(&charger
->poll
, bq24735_poll
);
478 schedule_delayed_work(&charger
->poll
,
479 msecs_to_jiffies(charger
->poll_interval
));
485 static int bq24735_charger_remove(struct i2c_client
*client
)
487 struct bq24735
*charger
= i2c_get_clientdata(client
);
489 if (charger
->poll_interval
)
490 cancel_delayed_work_sync(&charger
->poll
);
495 static const struct i2c_device_id bq24735_charger_id
[] = {
496 { "bq24735-charger", 0 },
499 MODULE_DEVICE_TABLE(i2c
, bq24735_charger_id
);
501 static const struct of_device_id bq24735_match_ids
[] = {
502 { .compatible
= "ti,bq24735", },
505 MODULE_DEVICE_TABLE(of
, bq24735_match_ids
);
507 static struct i2c_driver bq24735_charger_driver
= {
509 .name
= "bq24735-charger",
510 .of_match_table
= bq24735_match_ids
,
512 .probe
= bq24735_charger_probe
,
513 .remove
= bq24735_charger_remove
,
514 .id_table
= bq24735_charger_id
,
517 module_i2c_driver(bq24735_charger_driver
);
519 MODULE_DESCRIPTION("bq24735 battery charging driver");
520 MODULE_AUTHOR("Darbha Sriharsha <dsriharsha@nvidia.com>");
521 MODULE_LICENSE("GPL v2");