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/of_gpio.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
;
55 static inline struct bq24735
*to_bq24735(struct power_supply
*psy
)
57 return power_supply_get_drvdata(psy
);
60 static enum power_supply_property bq24735_charger_properties
[] = {
61 POWER_SUPPLY_PROP_STATUS
,
62 POWER_SUPPLY_PROP_ONLINE
,
65 static int bq24735_charger_property_is_writeable(struct power_supply
*psy
,
66 enum power_supply_property psp
)
69 case POWER_SUPPLY_PROP_STATUS
:
78 static inline int bq24735_write_word(struct i2c_client
*client
, u8 reg
,
81 return i2c_smbus_write_word_data(client
, reg
, le16_to_cpu(value
));
84 static inline int bq24735_read_word(struct i2c_client
*client
, u8 reg
)
86 s32 ret
= i2c_smbus_read_word_data(client
, reg
);
88 return ret
< 0 ? ret
: le16_to_cpu(ret
);
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 inline int bq24735_enable_charging(struct bq24735
*charger
)
109 if (charger
->pdata
->ext_control
)
112 return bq24735_update_word(charger
->client
, BQ24735_CHG_OPT
,
113 BQ24735_CHG_OPT_CHARGE_DISABLE
,
114 ~BQ24735_CHG_OPT_CHARGE_DISABLE
);
117 static inline int bq24735_disable_charging(struct bq24735
*charger
)
119 if (charger
->pdata
->ext_control
)
122 return bq24735_update_word(charger
->client
, BQ24735_CHG_OPT
,
123 BQ24735_CHG_OPT_CHARGE_DISABLE
,
124 BQ24735_CHG_OPT_CHARGE_DISABLE
);
127 static int bq24735_config_charger(struct bq24735
*charger
)
129 struct bq24735_platform
*pdata
= charger
->pdata
;
133 if (pdata
->ext_control
)
136 if (pdata
->charge_current
) {
137 value
= pdata
->charge_current
& BQ24735_CHARGE_CURRENT_MASK
;
139 ret
= bq24735_write_word(charger
->client
,
140 BQ24735_CHARGE_CURRENT
, value
);
142 dev_err(&charger
->client
->dev
,
143 "Failed to write charger current : %d\n",
149 if (pdata
->charge_voltage
) {
150 value
= pdata
->charge_voltage
& BQ24735_CHARGE_VOLTAGE_MASK
;
152 ret
= bq24735_write_word(charger
->client
,
153 BQ24735_CHARGE_VOLTAGE
, value
);
155 dev_err(&charger
->client
->dev
,
156 "Failed to write charger voltage : %d\n",
162 if (pdata
->input_current
) {
163 value
= pdata
->input_current
& BQ24735_INPUT_CURRENT_MASK
;
165 ret
= bq24735_write_word(charger
->client
,
166 BQ24735_INPUT_CURRENT
, value
);
168 dev_err(&charger
->client
->dev
,
169 "Failed to write input current : %d\n",
178 static bool bq24735_charger_is_present(struct bq24735
*charger
)
180 struct bq24735_platform
*pdata
= charger
->pdata
;
183 if (pdata
->status_gpio_valid
) {
184 ret
= gpio_get_value_cansleep(pdata
->status_gpio
);
185 return ret
^= pdata
->status_gpio_active_low
== 0;
189 ac
= bq24735_read_word(charger
->client
, BQ24735_CHG_OPT
);
191 dev_err(&charger
->client
->dev
,
192 "Failed to read charger options : %d\n",
196 return (ac
& BQ24735_CHG_OPT_AC_PRESENT
) ? true : false;
202 static int bq24735_charger_is_charging(struct bq24735
*charger
)
204 int ret
= bq24735_read_word(charger
->client
, BQ24735_CHG_OPT
);
209 return !(ret
& BQ24735_CHG_OPT_CHARGE_DISABLE
);
212 static irqreturn_t
bq24735_charger_isr(int irq
, void *devid
)
214 struct power_supply
*psy
= devid
;
215 struct bq24735
*charger
= to_bq24735(psy
);
217 mutex_lock(&charger
->lock
);
219 if (charger
->charging
&& bq24735_charger_is_present(charger
))
220 bq24735_enable_charging(charger
);
222 bq24735_disable_charging(charger
);
224 mutex_unlock(&charger
->lock
);
226 power_supply_changed(psy
);
231 static int bq24735_charger_get_property(struct power_supply
*psy
,
232 enum power_supply_property psp
,
233 union power_supply_propval
*val
)
235 struct bq24735
*charger
= to_bq24735(psy
);
238 case POWER_SUPPLY_PROP_ONLINE
:
239 val
->intval
= bq24735_charger_is_present(charger
) ? 1 : 0;
241 case POWER_SUPPLY_PROP_STATUS
:
242 switch (bq24735_charger_is_charging(charger
)) {
244 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
247 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
250 val
->intval
= POWER_SUPPLY_STATUS_UNKNOWN
;
261 static int bq24735_charger_set_property(struct power_supply
*psy
,
262 enum power_supply_property psp
,
263 const union power_supply_propval
*val
)
265 struct bq24735
*charger
= to_bq24735(psy
);
269 case POWER_SUPPLY_PROP_STATUS
:
270 switch (val
->intval
) {
271 case POWER_SUPPLY_STATUS_CHARGING
:
272 mutex_lock(&charger
->lock
);
273 charger
->charging
= true;
274 ret
= bq24735_enable_charging(charger
);
275 mutex_unlock(&charger
->lock
);
278 bq24735_config_charger(charger
);
280 case POWER_SUPPLY_STATUS_DISCHARGING
:
281 case POWER_SUPPLY_STATUS_NOT_CHARGING
:
282 mutex_lock(&charger
->lock
);
283 charger
->charging
= false;
284 ret
= bq24735_disable_charging(charger
);
285 mutex_unlock(&charger
->lock
);
292 power_supply_changed(psy
);
301 static struct bq24735_platform
*bq24735_parse_dt_data(struct i2c_client
*client
)
303 struct bq24735_platform
*pdata
;
304 struct device_node
*np
= client
->dev
.of_node
;
307 enum of_gpio_flags flags
;
309 pdata
= devm_kzalloc(&client
->dev
, sizeof(*pdata
), GFP_KERNEL
);
311 dev_err(&client
->dev
,
312 "Memory alloc for bq24735 pdata failed\n");
316 pdata
->status_gpio
= of_get_named_gpio_flags(np
, "ti,ac-detect-gpios",
319 if (flags
& OF_GPIO_ACTIVE_LOW
)
320 pdata
->status_gpio_active_low
= 1;
322 ret
= of_property_read_u32(np
, "ti,charge-current", &val
);
324 pdata
->charge_current
= val
;
326 ret
= of_property_read_u32(np
, "ti,charge-voltage", &val
);
328 pdata
->charge_voltage
= val
;
330 ret
= of_property_read_u32(np
, "ti,input-current", &val
);
332 pdata
->input_current
= val
;
334 pdata
->ext_control
= of_property_read_bool(np
, "ti,external-control");
339 static int bq24735_charger_probe(struct i2c_client
*client
,
340 const struct i2c_device_id
*id
)
343 struct bq24735
*charger
;
344 struct power_supply_desc
*supply_desc
;
345 struct power_supply_config psy_cfg
= {};
348 charger
= devm_kzalloc(&client
->dev
, sizeof(*charger
), GFP_KERNEL
);
352 mutex_init(&charger
->lock
);
353 charger
->charging
= true;
354 charger
->pdata
= client
->dev
.platform_data
;
356 if (IS_ENABLED(CONFIG_OF
) && !charger
->pdata
&& client
->dev
.of_node
)
357 charger
->pdata
= bq24735_parse_dt_data(client
);
359 if (!charger
->pdata
) {
360 dev_err(&client
->dev
, "no platform data provided\n");
364 name
= (char *)charger
->pdata
->name
;
366 name
= devm_kasprintf(&client
->dev
, GFP_KERNEL
,
368 dev_name(&client
->dev
));
370 dev_err(&client
->dev
, "Failed to alloc device name\n");
375 charger
->client
= client
;
377 supply_desc
= &charger
->charger_desc
;
379 supply_desc
->name
= name
;
380 supply_desc
->type
= POWER_SUPPLY_TYPE_MAINS
;
381 supply_desc
->properties
= bq24735_charger_properties
;
382 supply_desc
->num_properties
= ARRAY_SIZE(bq24735_charger_properties
);
383 supply_desc
->get_property
= bq24735_charger_get_property
;
384 supply_desc
->set_property
= bq24735_charger_set_property
;
385 supply_desc
->property_is_writeable
=
386 bq24735_charger_property_is_writeable
;
388 psy_cfg
.supplied_to
= charger
->pdata
->supplied_to
;
389 psy_cfg
.num_supplicants
= charger
->pdata
->num_supplicants
;
390 psy_cfg
.of_node
= client
->dev
.of_node
;
391 psy_cfg
.drv_data
= charger
;
393 i2c_set_clientdata(client
, charger
);
395 if (gpio_is_valid(charger
->pdata
->status_gpio
)) {
396 ret
= devm_gpio_request(&client
->dev
,
397 charger
->pdata
->status_gpio
,
400 dev_err(&client
->dev
,
401 "Failed GPIO request for GPIO %d: %d\n",
402 charger
->pdata
->status_gpio
, ret
);
405 charger
->pdata
->status_gpio_valid
= !ret
;
408 if (!charger
->pdata
->status_gpio_valid
409 || bq24735_charger_is_present(charger
)) {
410 ret
= bq24735_read_word(client
, BQ24735_MANUFACTURER_ID
);
412 dev_err(&client
->dev
, "Failed to read manufacturer id : %d\n",
415 } else if (ret
!= 0x0040) {
416 dev_err(&client
->dev
,
417 "manufacturer id mismatch. 0x0040 != 0x%04x\n", ret
);
421 ret
= bq24735_read_word(client
, BQ24735_DEVICE_ID
);
423 dev_err(&client
->dev
, "Failed to read device id : %d\n", ret
);
425 } else if (ret
!= 0x000B) {
426 dev_err(&client
->dev
,
427 "device id mismatch. 0x000b != 0x%04x\n", ret
);
432 ret
= bq24735_config_charger(charger
);
434 dev_err(&client
->dev
, "failed in configuring charger");
438 /* check for AC adapter presence */
439 if (bq24735_charger_is_present(charger
)) {
440 ret
= bq24735_enable_charging(charger
);
442 dev_err(&client
->dev
, "Failed to enable charging\n");
447 charger
->charger
= devm_power_supply_register(&client
->dev
, supply_desc
,
449 if (IS_ERR(charger
->charger
)) {
450 ret
= PTR_ERR(charger
->charger
);
451 dev_err(&client
->dev
, "Failed to register power supply: %d\n",
457 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
458 NULL
, bq24735_charger_isr
,
459 IRQF_TRIGGER_RISING
|
460 IRQF_TRIGGER_FALLING
|
465 dev_err(&client
->dev
,
466 "Unable to register IRQ %d err %d\n",
475 static const struct i2c_device_id bq24735_charger_id
[] = {
476 { "bq24735-charger", 0 },
479 MODULE_DEVICE_TABLE(i2c
, bq24735_charger_id
);
481 static const struct of_device_id bq24735_match_ids
[] = {
482 { .compatible
= "ti,bq24735", },
485 MODULE_DEVICE_TABLE(of
, bq24735_match_ids
);
487 static struct i2c_driver bq24735_charger_driver
= {
489 .name
= "bq24735-charger",
490 .of_match_table
= bq24735_match_ids
,
492 .probe
= bq24735_charger_probe
,
493 .id_table
= bq24735_charger_id
,
496 module_i2c_driver(bq24735_charger_driver
);
498 MODULE_DESCRIPTION("bq24735 battery charging driver");
499 MODULE_AUTHOR("Darbha Sriharsha <dsriharsha@nvidia.com>");
500 MODULE_LICENSE("GPL v2");