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
;
53 static inline struct bq24735
*to_bq24735(struct power_supply
*psy
)
55 return power_supply_get_drvdata(psy
);
58 static enum power_supply_property bq24735_charger_properties
[] = {
59 POWER_SUPPLY_PROP_ONLINE
,
62 static inline int bq24735_write_word(struct i2c_client
*client
, u8 reg
,
65 return i2c_smbus_write_word_data(client
, reg
, le16_to_cpu(value
));
68 static inline int bq24735_read_word(struct i2c_client
*client
, u8 reg
)
70 s32 ret
= i2c_smbus_read_word_data(client
, reg
);
72 return ret
< 0 ? ret
: le16_to_cpu(ret
);
75 static int bq24735_update_word(struct i2c_client
*client
, u8 reg
,
81 ret
= bq24735_read_word(client
, reg
);
88 return bq24735_write_word(client
, reg
, tmp
);
91 static inline int bq24735_enable_charging(struct bq24735
*charger
)
93 return bq24735_update_word(charger
->client
, BQ24735_CHG_OPT
,
94 BQ24735_CHG_OPT_CHARGE_DISABLE
,
95 ~BQ24735_CHG_OPT_CHARGE_DISABLE
);
98 static inline int bq24735_disable_charging(struct bq24735
*charger
)
100 return bq24735_update_word(charger
->client
, BQ24735_CHG_OPT
,
101 BQ24735_CHG_OPT_CHARGE_DISABLE
,
102 BQ24735_CHG_OPT_CHARGE_DISABLE
);
105 static int bq24735_config_charger(struct bq24735
*charger
)
107 struct bq24735_platform
*pdata
= charger
->pdata
;
111 if (pdata
->charge_current
) {
112 value
= pdata
->charge_current
& BQ24735_CHARGE_CURRENT_MASK
;
114 ret
= bq24735_write_word(charger
->client
,
115 BQ24735_CHARGE_CURRENT
, value
);
117 dev_err(&charger
->client
->dev
,
118 "Failed to write charger current : %d\n",
124 if (pdata
->charge_voltage
) {
125 value
= pdata
->charge_voltage
& BQ24735_CHARGE_VOLTAGE_MASK
;
127 ret
= bq24735_write_word(charger
->client
,
128 BQ24735_CHARGE_VOLTAGE
, value
);
130 dev_err(&charger
->client
->dev
,
131 "Failed to write charger voltage : %d\n",
137 if (pdata
->input_current
) {
138 value
= pdata
->input_current
& BQ24735_INPUT_CURRENT_MASK
;
140 ret
= bq24735_write_word(charger
->client
,
141 BQ24735_INPUT_CURRENT
, value
);
143 dev_err(&charger
->client
->dev
,
144 "Failed to write input current : %d\n",
153 static bool bq24735_charger_is_present(struct bq24735
*charger
)
155 struct bq24735_platform
*pdata
= charger
->pdata
;
158 if (pdata
->status_gpio_valid
) {
159 ret
= gpio_get_value_cansleep(pdata
->status_gpio
);
160 return ret
^= pdata
->status_gpio_active_low
== 0;
164 ac
= bq24735_read_word(charger
->client
, BQ24735_CHG_OPT
);
166 dev_err(&charger
->client
->dev
,
167 "Failed to read charger options : %d\n",
171 return (ac
& BQ24735_CHG_OPT_AC_PRESENT
) ? true : false;
177 static irqreturn_t
bq24735_charger_isr(int irq
, void *devid
)
179 struct power_supply
*psy
= devid
;
180 struct bq24735
*charger
= to_bq24735(psy
);
182 if (bq24735_charger_is_present(charger
))
183 bq24735_enable_charging(charger
);
185 bq24735_disable_charging(charger
);
187 power_supply_changed(psy
);
192 static int bq24735_charger_get_property(struct power_supply
*psy
,
193 enum power_supply_property psp
,
194 union power_supply_propval
*val
)
196 struct bq24735
*charger
= to_bq24735(psy
);
199 case POWER_SUPPLY_PROP_ONLINE
:
200 val
->intval
= bq24735_charger_is_present(charger
) ? 1 : 0;
209 static struct bq24735_platform
*bq24735_parse_dt_data(struct i2c_client
*client
)
211 struct bq24735_platform
*pdata
;
212 struct device_node
*np
= client
->dev
.of_node
;
215 enum of_gpio_flags flags
;
217 pdata
= devm_kzalloc(&client
->dev
, sizeof(*pdata
), GFP_KERNEL
);
219 dev_err(&client
->dev
,
220 "Memory alloc for bq24735 pdata failed\n");
224 pdata
->status_gpio
= of_get_named_gpio_flags(np
, "ti,ac-detect-gpios",
227 if (flags
& OF_GPIO_ACTIVE_LOW
)
228 pdata
->status_gpio_active_low
= 1;
230 ret
= of_property_read_u32(np
, "ti,charge-current", &val
);
232 pdata
->charge_current
= val
;
234 ret
= of_property_read_u32(np
, "ti,charge-voltage", &val
);
236 pdata
->charge_voltage
= val
;
238 ret
= of_property_read_u32(np
, "ti,input-current", &val
);
240 pdata
->input_current
= val
;
245 static int bq24735_charger_probe(struct i2c_client
*client
,
246 const struct i2c_device_id
*id
)
249 struct bq24735
*charger
;
250 struct power_supply_desc
*supply_desc
;
251 struct power_supply_config psy_cfg
= {};
254 charger
= devm_kzalloc(&client
->dev
, sizeof(*charger
), GFP_KERNEL
);
258 charger
->pdata
= client
->dev
.platform_data
;
260 if (IS_ENABLED(CONFIG_OF
) && !charger
->pdata
&& client
->dev
.of_node
)
261 charger
->pdata
= bq24735_parse_dt_data(client
);
263 if (!charger
->pdata
) {
264 dev_err(&client
->dev
, "no platform data provided\n");
268 name
= (char *)charger
->pdata
->name
;
270 name
= kasprintf(GFP_KERNEL
, "bq24735@%s",
271 dev_name(&client
->dev
));
273 dev_err(&client
->dev
, "Failed to alloc device name\n");
278 charger
->client
= client
;
280 supply_desc
= &charger
->charger_desc
;
282 supply_desc
->name
= name
;
283 supply_desc
->type
= POWER_SUPPLY_TYPE_MAINS
;
284 supply_desc
->properties
= bq24735_charger_properties
;
285 supply_desc
->num_properties
= ARRAY_SIZE(bq24735_charger_properties
);
286 supply_desc
->get_property
= bq24735_charger_get_property
;
288 psy_cfg
.supplied_to
= charger
->pdata
->supplied_to
;
289 psy_cfg
.num_supplicants
= charger
->pdata
->num_supplicants
;
290 psy_cfg
.of_node
= client
->dev
.of_node
;
291 psy_cfg
.drv_data
= charger
;
293 i2c_set_clientdata(client
, charger
);
295 ret
= bq24735_read_word(client
, BQ24735_MANUFACTURER_ID
);
297 dev_err(&client
->dev
, "Failed to read manufacturer id : %d\n",
300 } else if (ret
!= 0x0040) {
301 dev_err(&client
->dev
,
302 "manufacturer id mismatch. 0x0040 != 0x%04x\n", ret
);
307 ret
= bq24735_read_word(client
, BQ24735_DEVICE_ID
);
309 dev_err(&client
->dev
, "Failed to read device id : %d\n", ret
);
311 } else if (ret
!= 0x000B) {
312 dev_err(&client
->dev
,
313 "device id mismatch. 0x000b != 0x%04x\n", ret
);
318 if (gpio_is_valid(charger
->pdata
->status_gpio
)) {
319 ret
= devm_gpio_request(&client
->dev
,
320 charger
->pdata
->status_gpio
,
323 dev_err(&client
->dev
,
324 "Failed GPIO request for GPIO %d: %d\n",
325 charger
->pdata
->status_gpio
, ret
);
328 charger
->pdata
->status_gpio_valid
= !ret
;
331 ret
= bq24735_config_charger(charger
);
333 dev_err(&client
->dev
, "failed in configuring charger");
337 /* check for AC adapter presence */
338 if (bq24735_charger_is_present(charger
)) {
339 ret
= bq24735_enable_charging(charger
);
341 dev_err(&client
->dev
, "Failed to enable charging\n");
346 charger
->charger
= power_supply_register(&client
->dev
, supply_desc
,
348 if (IS_ERR(charger
->charger
)) {
349 ret
= PTR_ERR(charger
->charger
);
350 dev_err(&client
->dev
, "Failed to register power supply: %d\n",
356 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
357 NULL
, bq24735_charger_isr
,
358 IRQF_TRIGGER_RISING
|
359 IRQF_TRIGGER_FALLING
|
364 dev_err(&client
->dev
,
365 "Unable to register IRQ %d err %d\n",
367 goto err_unregister_supply
;
372 err_unregister_supply
:
373 power_supply_unregister(charger
->charger
);
375 if (name
!= charger
->pdata
->name
)
381 static int bq24735_charger_remove(struct i2c_client
*client
)
383 struct bq24735
*charger
= i2c_get_clientdata(client
);
385 if (charger
->client
->irq
)
386 devm_free_irq(&charger
->client
->dev
, charger
->client
->irq
,
389 power_supply_unregister(charger
->charger
);
391 if (charger
->charger_desc
.name
!= charger
->pdata
->name
)
392 kfree(charger
->charger_desc
.name
);
397 static const struct i2c_device_id bq24735_charger_id
[] = {
398 { "bq24735-charger", 0 },
401 MODULE_DEVICE_TABLE(i2c
, bq24735_charger_id
);
403 static const struct of_device_id bq24735_match_ids
[] = {
404 { .compatible
= "ti,bq24735", },
407 MODULE_DEVICE_TABLE(of
, bq24735_match_ids
);
409 static struct i2c_driver bq24735_charger_driver
= {
411 .name
= "bq24735-charger",
412 .owner
= THIS_MODULE
,
413 .of_match_table
= bq24735_match_ids
,
415 .probe
= bq24735_charger_probe
,
416 .remove
= bq24735_charger_remove
,
417 .id_table
= bq24735_charger_id
,
420 module_i2c_driver(bq24735_charger_driver
);
422 MODULE_DESCRIPTION("bq24735 battery charging driver");
423 MODULE_AUTHOR("Darbha Sriharsha <dsriharsha@nvidia.com>");
424 MODULE_LICENSE("GPL v2");