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/devm-helpers.h>
21 #include <linux/err.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 /* BQ24735 available commands and their respective masks */
35 #define BQ24735_CHARGE_OPT 0x12
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
45 /* ChargeOptions bits of interest */
46 #define BQ24735_CHARGE_OPT_CHG_DISABLE (1 << 0)
47 #define BQ24735_CHARGE_OPT_AC_PRESENT (1 << 4)
50 struct power_supply
*charger
;
51 struct power_supply_desc charger_desc
;
52 struct i2c_client
*client
;
53 struct bq24735_platform
*pdata
;
55 struct gpio_desc
*status_gpio
;
56 struct delayed_work poll
;
61 static inline struct bq24735
*to_bq24735(struct power_supply
*psy
)
63 return power_supply_get_drvdata(psy
);
66 static enum power_supply_property bq24735_charger_properties
[] = {
67 POWER_SUPPLY_PROP_STATUS
,
68 POWER_SUPPLY_PROP_ONLINE
,
71 static int bq24735_charger_property_is_writeable(struct power_supply
*psy
,
72 enum power_supply_property psp
)
75 case POWER_SUPPLY_PROP_STATUS
:
84 static inline int bq24735_write_word(struct i2c_client
*client
, u8 reg
,
87 return i2c_smbus_write_word_data(client
, reg
, value
);
90 static inline int bq24735_read_word(struct i2c_client
*client
, u8 reg
)
92 return i2c_smbus_read_word_data(client
, reg
);
95 static int bq24735_update_word(struct i2c_client
*client
, u8 reg
,
101 ret
= bq24735_read_word(client
, reg
);
108 return bq24735_write_word(client
, reg
, tmp
);
111 static int bq24735_config_charger(struct bq24735
*charger
)
113 struct bq24735_platform
*pdata
= charger
->pdata
;
117 if (pdata
->ext_control
)
120 if (pdata
->charge_current
) {
121 value
= pdata
->charge_current
& BQ24735_CHARGE_CURRENT_MASK
;
123 ret
= bq24735_write_word(charger
->client
,
124 BQ24735_CHARGE_CURRENT
, value
);
126 dev_err(&charger
->client
->dev
,
127 "Failed to write charger current : %d\n",
133 if (pdata
->charge_voltage
) {
134 value
= pdata
->charge_voltage
& BQ24735_CHARGE_VOLTAGE_MASK
;
136 ret
= bq24735_write_word(charger
->client
,
137 BQ24735_CHARGE_VOLTAGE
, value
);
139 dev_err(&charger
->client
->dev
,
140 "Failed to write charger voltage : %d\n",
146 if (pdata
->input_current
) {
147 value
= pdata
->input_current
& BQ24735_INPUT_CURRENT_MASK
;
149 ret
= bq24735_write_word(charger
->client
,
150 BQ24735_INPUT_CURRENT
, value
);
152 dev_err(&charger
->client
->dev
,
153 "Failed to write input current : %d\n",
162 static inline int bq24735_enable_charging(struct bq24735
*charger
)
166 if (charger
->pdata
->ext_control
)
169 ret
= bq24735_config_charger(charger
);
173 return bq24735_update_word(charger
->client
, BQ24735_CHARGE_OPT
,
174 BQ24735_CHARGE_OPT_CHG_DISABLE
, 0);
177 static inline int bq24735_disable_charging(struct bq24735
*charger
)
179 if (charger
->pdata
->ext_control
)
182 return bq24735_update_word(charger
->client
, BQ24735_CHARGE_OPT
,
183 BQ24735_CHARGE_OPT_CHG_DISABLE
,
184 BQ24735_CHARGE_OPT_CHG_DISABLE
);
187 static bool bq24735_charger_is_present(struct bq24735
*charger
)
189 if (charger
->status_gpio
) {
190 return !gpiod_get_value_cansleep(charger
->status_gpio
);
194 ac
= bq24735_read_word(charger
->client
, BQ24735_CHARGE_OPT
);
196 dev_dbg(&charger
->client
->dev
,
197 "Failed to read charger options : %d\n",
201 return (ac
& BQ24735_CHARGE_OPT_AC_PRESENT
) ? true : false;
207 static int bq24735_charger_is_charging(struct bq24735
*charger
)
211 if (!bq24735_charger_is_present(charger
))
214 ret
= bq24735_read_word(charger
->client
, BQ24735_CHARGE_OPT
);
218 return !(ret
& BQ24735_CHARGE_OPT_CHG_DISABLE
);
221 static void bq24735_update(struct bq24735
*charger
)
223 mutex_lock(&charger
->lock
);
225 if (charger
->charging
&& bq24735_charger_is_present(charger
))
226 bq24735_enable_charging(charger
);
228 bq24735_disable_charging(charger
);
230 mutex_unlock(&charger
->lock
);
232 power_supply_changed(charger
->charger
);
235 static irqreturn_t
bq24735_charger_isr(int irq
, void *devid
)
237 struct power_supply
*psy
= devid
;
238 struct bq24735
*charger
= to_bq24735(psy
);
240 bq24735_update(charger
);
245 static void bq24735_poll(struct work_struct
*work
)
247 struct bq24735
*charger
= container_of(work
, struct bq24735
, poll
.work
);
249 bq24735_update(charger
);
251 schedule_delayed_work(&charger
->poll
,
252 msecs_to_jiffies(charger
->poll_interval
));
255 static int bq24735_charger_get_property(struct power_supply
*psy
,
256 enum power_supply_property psp
,
257 union power_supply_propval
*val
)
259 struct bq24735
*charger
= to_bq24735(psy
);
262 case POWER_SUPPLY_PROP_ONLINE
:
263 val
->intval
= bq24735_charger_is_present(charger
) ? 1 : 0;
265 case POWER_SUPPLY_PROP_STATUS
:
266 switch (bq24735_charger_is_charging(charger
)) {
268 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
271 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
274 val
->intval
= POWER_SUPPLY_STATUS_UNKNOWN
;
285 static int bq24735_charger_set_property(struct power_supply
*psy
,
286 enum power_supply_property psp
,
287 const union power_supply_propval
*val
)
289 struct bq24735
*charger
= to_bq24735(psy
);
293 case POWER_SUPPLY_PROP_STATUS
:
294 switch (val
->intval
) {
295 case POWER_SUPPLY_STATUS_CHARGING
:
296 mutex_lock(&charger
->lock
);
297 charger
->charging
= true;
298 ret
= bq24735_enable_charging(charger
);
299 mutex_unlock(&charger
->lock
);
303 case POWER_SUPPLY_STATUS_DISCHARGING
:
304 case POWER_SUPPLY_STATUS_NOT_CHARGING
:
305 mutex_lock(&charger
->lock
);
306 charger
->charging
= false;
307 ret
= bq24735_disable_charging(charger
);
308 mutex_unlock(&charger
->lock
);
315 power_supply_changed(psy
);
324 static struct bq24735_platform
*bq24735_parse_dt_data(struct i2c_client
*client
)
326 struct bq24735_platform
*pdata
;
327 struct device_node
*np
= client
->dev
.of_node
;
331 pdata
= devm_kzalloc(&client
->dev
, sizeof(*pdata
), GFP_KERNEL
);
333 dev_err(&client
->dev
,
334 "Memory alloc for bq24735 pdata failed\n");
338 ret
= of_property_read_u32(np
, "ti,charge-current", &val
);
340 pdata
->charge_current
= val
;
342 ret
= of_property_read_u32(np
, "ti,charge-voltage", &val
);
344 pdata
->charge_voltage
= val
;
346 ret
= of_property_read_u32(np
, "ti,input-current", &val
);
348 pdata
->input_current
= val
;
350 pdata
->ext_control
= of_property_read_bool(np
, "ti,external-control");
355 static int bq24735_charger_probe(struct i2c_client
*client
)
358 struct bq24735
*charger
;
359 struct power_supply_desc
*supply_desc
;
360 struct power_supply_config psy_cfg
= {};
363 charger
= devm_kzalloc(&client
->dev
, sizeof(*charger
), GFP_KERNEL
);
367 mutex_init(&charger
->lock
);
368 charger
->charging
= true;
369 charger
->pdata
= client
->dev
.platform_data
;
371 if (IS_ENABLED(CONFIG_OF
) && !charger
->pdata
&& client
->dev
.of_node
)
372 charger
->pdata
= bq24735_parse_dt_data(client
);
374 if (!charger
->pdata
) {
375 dev_err(&client
->dev
, "no platform data provided\n");
379 name
= (char *)charger
->pdata
->name
;
381 name
= devm_kasprintf(&client
->dev
, GFP_KERNEL
,
383 dev_name(&client
->dev
));
385 dev_err(&client
->dev
, "Failed to alloc device name\n");
390 charger
->client
= client
;
392 supply_desc
= &charger
->charger_desc
;
394 supply_desc
->name
= name
;
395 supply_desc
->type
= POWER_SUPPLY_TYPE_MAINS
;
396 supply_desc
->properties
= bq24735_charger_properties
;
397 supply_desc
->num_properties
= ARRAY_SIZE(bq24735_charger_properties
);
398 supply_desc
->get_property
= bq24735_charger_get_property
;
399 supply_desc
->set_property
= bq24735_charger_set_property
;
400 supply_desc
->property_is_writeable
=
401 bq24735_charger_property_is_writeable
;
403 psy_cfg
.supplied_to
= charger
->pdata
->supplied_to
;
404 psy_cfg
.num_supplicants
= charger
->pdata
->num_supplicants
;
405 psy_cfg
.of_node
= client
->dev
.of_node
;
406 psy_cfg
.drv_data
= charger
;
408 i2c_set_clientdata(client
, charger
);
410 charger
->status_gpio
= devm_gpiod_get_optional(&client
->dev
,
413 if (IS_ERR(charger
->status_gpio
)) {
414 ret
= PTR_ERR(charger
->status_gpio
);
415 dev_err(&client
->dev
, "Getting gpio failed: %d\n", ret
);
419 if (bq24735_charger_is_present(charger
)) {
420 ret
= bq24735_read_word(client
, BQ24735_MANUFACTURER_ID
);
422 dev_err(&client
->dev
, "Failed to read manufacturer id : %d\n",
425 } else if (ret
!= 0x0040) {
426 dev_err(&client
->dev
,
427 "manufacturer id mismatch. 0x0040 != 0x%04x\n", ret
);
431 ret
= bq24735_read_word(client
, BQ24735_DEVICE_ID
);
433 dev_err(&client
->dev
, "Failed to read device id : %d\n", ret
);
435 } else if (ret
!= 0x000B) {
436 dev_err(&client
->dev
,
437 "device id mismatch. 0x000b != 0x%04x\n", ret
);
441 ret
= bq24735_enable_charging(charger
);
443 dev_err(&client
->dev
, "Failed to enable charging\n");
448 charger
->charger
= devm_power_supply_register(&client
->dev
, supply_desc
,
450 if (IS_ERR(charger
->charger
)) {
451 ret
= PTR_ERR(charger
->charger
);
452 dev_err(&client
->dev
, "Failed to register power supply: %d\n",
458 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
459 NULL
, bq24735_charger_isr
,
460 IRQF_TRIGGER_RISING
|
461 IRQF_TRIGGER_FALLING
|
466 dev_err(&client
->dev
,
467 "Unable to register IRQ %d err %d\n",
472 ret
= device_property_read_u32(&client
->dev
, "poll-interval",
473 &charger
->poll_interval
);
476 if (!charger
->poll_interval
)
479 ret
= devm_delayed_work_autocancel(&client
->dev
, &charger
->poll
,
484 schedule_delayed_work(&charger
->poll
,
485 msecs_to_jiffies(charger
->poll_interval
));
491 static const struct i2c_device_id bq24735_charger_id
[] = {
492 { "bq24735-charger" },
495 MODULE_DEVICE_TABLE(i2c
, bq24735_charger_id
);
497 static const struct of_device_id bq24735_match_ids
[] = {
498 { .compatible
= "ti,bq24735", },
501 MODULE_DEVICE_TABLE(of
, bq24735_match_ids
);
503 static struct i2c_driver bq24735_charger_driver
= {
505 .name
= "bq24735-charger",
506 .of_match_table
= bq24735_match_ids
,
508 .probe
= bq24735_charger_probe
,
509 .id_table
= bq24735_charger_id
,
512 module_i2c_driver(bq24735_charger_driver
);
514 MODULE_DESCRIPTION("bq24735 battery charging driver");
515 MODULE_AUTHOR("Darbha Sriharsha <dsriharsha@nvidia.com>");
516 MODULE_LICENSE("GPL v2");