• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Battery charger driver for TI BQ24735
3  *
4  * Copyright (c) 2013, NVIDIA CORPORATION.  All rights reserved.
5  *
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;
9  *
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
13  * more details.
14  *
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.
18  */
19 
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>
27 #include <linux/of.h>
28 #include <linux/gpio/consumer.h>
29 #include <linux/power_supply.h>
30 #include <linux/slab.h>
31 
32 #include <linux/power/bq24735-charger.h>
33 
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
45 
46 struct bq24735 {
47 	struct power_supply		*charger;
48 	struct power_supply_desc	charger_desc;
49 	struct i2c_client		*client;
50 	struct bq24735_platform		*pdata;
51 	struct mutex			lock;
52 	struct gpio_desc		*status_gpio;
53 	bool				charging;
54 };
55 
to_bq24735(struct power_supply * psy)56 static inline struct bq24735 *to_bq24735(struct power_supply *psy)
57 {
58 	return power_supply_get_drvdata(psy);
59 }
60 
61 static enum power_supply_property bq24735_charger_properties[] = {
62 	POWER_SUPPLY_PROP_STATUS,
63 	POWER_SUPPLY_PROP_ONLINE,
64 };
65 
bq24735_charger_property_is_writeable(struct power_supply * psy,enum power_supply_property psp)66 static int bq24735_charger_property_is_writeable(struct power_supply *psy,
67 						 enum power_supply_property psp)
68 {
69 	switch (psp) {
70 	case POWER_SUPPLY_PROP_STATUS:
71 		return 1;
72 	default:
73 		break;
74 	}
75 
76 	return 0;
77 }
78 
bq24735_write_word(struct i2c_client * client,u8 reg,u16 value)79 static inline int bq24735_write_word(struct i2c_client *client, u8 reg,
80 				     u16 value)
81 {
82 	return i2c_smbus_write_word_data(client, reg, le16_to_cpu(value));
83 }
84 
bq24735_read_word(struct i2c_client * client,u8 reg)85 static inline int bq24735_read_word(struct i2c_client *client, u8 reg)
86 {
87 	s32 ret = i2c_smbus_read_word_data(client, reg);
88 
89 	return ret < 0 ? ret : le16_to_cpu(ret);
90 }
91 
bq24735_update_word(struct i2c_client * client,u8 reg,u16 mask,u16 value)92 static int bq24735_update_word(struct i2c_client *client, u8 reg,
93 			       u16 mask, u16 value)
94 {
95 	unsigned int tmp;
96 	int ret;
97 
98 	ret = bq24735_read_word(client, reg);
99 	if (ret < 0)
100 		return ret;
101 
102 	tmp = ret & ~mask;
103 	tmp |= value & mask;
104 
105 	return bq24735_write_word(client, reg, tmp);
106 }
107 
bq24735_enable_charging(struct bq24735 * charger)108 static inline int bq24735_enable_charging(struct bq24735 *charger)
109 {
110 	if (charger->pdata->ext_control)
111 		return 0;
112 
113 	return bq24735_update_word(charger->client, BQ24735_CHG_OPT,
114 				   BQ24735_CHG_OPT_CHARGE_DISABLE,
115 				   ~BQ24735_CHG_OPT_CHARGE_DISABLE);
116 }
117 
bq24735_disable_charging(struct bq24735 * charger)118 static inline int bq24735_disable_charging(struct bq24735 *charger)
119 {
120 	if (charger->pdata->ext_control)
121 		return 0;
122 
123 	return bq24735_update_word(charger->client, BQ24735_CHG_OPT,
124 				   BQ24735_CHG_OPT_CHARGE_DISABLE,
125 				   BQ24735_CHG_OPT_CHARGE_DISABLE);
126 }
127 
bq24735_config_charger(struct bq24735 * charger)128 static int bq24735_config_charger(struct bq24735 *charger)
129 {
130 	struct bq24735_platform *pdata = charger->pdata;
131 	int ret;
132 	u16 value;
133 
134 	if (pdata->ext_control)
135 		return 0;
136 
137 	if (pdata->charge_current) {
138 		value = pdata->charge_current & BQ24735_CHARGE_CURRENT_MASK;
139 
140 		ret = bq24735_write_word(charger->client,
141 					 BQ24735_CHARGE_CURRENT, value);
142 		if (ret < 0) {
143 			dev_err(&charger->client->dev,
144 				"Failed to write charger current : %d\n",
145 				ret);
146 			return ret;
147 		}
148 	}
149 
150 	if (pdata->charge_voltage) {
151 		value = pdata->charge_voltage & BQ24735_CHARGE_VOLTAGE_MASK;
152 
153 		ret = bq24735_write_word(charger->client,
154 					 BQ24735_CHARGE_VOLTAGE, value);
155 		if (ret < 0) {
156 			dev_err(&charger->client->dev,
157 				"Failed to write charger voltage : %d\n",
158 				ret);
159 			return ret;
160 		}
161 	}
162 
163 	if (pdata->input_current) {
164 		value = pdata->input_current & BQ24735_INPUT_CURRENT_MASK;
165 
166 		ret = bq24735_write_word(charger->client,
167 					 BQ24735_INPUT_CURRENT, value);
168 		if (ret < 0) {
169 			dev_err(&charger->client->dev,
170 				"Failed to write input current : %d\n",
171 				ret);
172 			return ret;
173 		}
174 	}
175 
176 	return 0;
177 }
178 
bq24735_charger_is_present(struct bq24735 * charger)179 static bool bq24735_charger_is_present(struct bq24735 *charger)
180 {
181 	if (charger->status_gpio) {
182 		return !gpiod_get_value_cansleep(charger->status_gpio);
183 	} else {
184 		int ac = 0;
185 
186 		ac = bq24735_read_word(charger->client, BQ24735_CHG_OPT);
187 		if (ac < 0) {
188 			dev_err(&charger->client->dev,
189 				"Failed to read charger options : %d\n",
190 				ac);
191 			return false;
192 		}
193 		return (ac & BQ24735_CHG_OPT_AC_PRESENT) ? true : false;
194 	}
195 
196 	return false;
197 }
198 
bq24735_charger_is_charging(struct bq24735 * charger)199 static int bq24735_charger_is_charging(struct bq24735 *charger)
200 {
201 	int ret;
202 
203 	if (!bq24735_charger_is_present(charger))
204 		return 0;
205 
206 	ret  = bq24735_read_word(charger->client, BQ24735_CHG_OPT);
207 	if (ret < 0)
208 		return ret;
209 
210 	return !(ret & BQ24735_CHG_OPT_CHARGE_DISABLE);
211 }
212 
bq24735_charger_isr(int irq,void * devid)213 static irqreturn_t bq24735_charger_isr(int irq, void *devid)
214 {
215 	struct power_supply *psy = devid;
216 	struct bq24735 *charger = to_bq24735(psy);
217 
218 	mutex_lock(&charger->lock);
219 
220 	if (charger->charging && bq24735_charger_is_present(charger))
221 		bq24735_enable_charging(charger);
222 	else
223 		bq24735_disable_charging(charger);
224 
225 	mutex_unlock(&charger->lock);
226 
227 	power_supply_changed(psy);
228 
229 	return IRQ_HANDLED;
230 }
231 
bq24735_charger_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)232 static int bq24735_charger_get_property(struct power_supply *psy,
233 					enum power_supply_property psp,
234 					union power_supply_propval *val)
235 {
236 	struct bq24735 *charger = to_bq24735(psy);
237 
238 	switch (psp) {
239 	case POWER_SUPPLY_PROP_ONLINE:
240 		val->intval = bq24735_charger_is_present(charger) ? 1 : 0;
241 		break;
242 	case POWER_SUPPLY_PROP_STATUS:
243 		switch (bq24735_charger_is_charging(charger)) {
244 		case 1:
245 			val->intval = POWER_SUPPLY_STATUS_CHARGING;
246 			break;
247 		case 0:
248 			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
249 			break;
250 		default:
251 			val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
252 			break;
253 		}
254 		break;
255 	default:
256 		return -EINVAL;
257 	}
258 
259 	return 0;
260 }
261 
bq24735_charger_set_property(struct power_supply * psy,enum power_supply_property psp,const union power_supply_propval * val)262 static int bq24735_charger_set_property(struct power_supply *psy,
263 					enum power_supply_property psp,
264 					const union power_supply_propval *val)
265 {
266 	struct bq24735 *charger = to_bq24735(psy);
267 	int ret;
268 
269 	switch (psp) {
270 	case POWER_SUPPLY_PROP_STATUS:
271 		switch (val->intval) {
272 		case POWER_SUPPLY_STATUS_CHARGING:
273 			mutex_lock(&charger->lock);
274 			charger->charging = true;
275 			ret = bq24735_enable_charging(charger);
276 			mutex_unlock(&charger->lock);
277 			if (ret)
278 				return ret;
279 			bq24735_config_charger(charger);
280 			break;
281 		case POWER_SUPPLY_STATUS_DISCHARGING:
282 		case POWER_SUPPLY_STATUS_NOT_CHARGING:
283 			mutex_lock(&charger->lock);
284 			charger->charging = false;
285 			ret = bq24735_disable_charging(charger);
286 			mutex_unlock(&charger->lock);
287 			if (ret)
288 				return ret;
289 			break;
290 		default:
291 			return -EINVAL;
292 		}
293 		power_supply_changed(psy);
294 		break;
295 	default:
296 		return -EPERM;
297 	}
298 
299 	return 0;
300 }
301 
bq24735_parse_dt_data(struct i2c_client * client)302 static struct bq24735_platform *bq24735_parse_dt_data(struct i2c_client *client)
303 {
304 	struct bq24735_platform *pdata;
305 	struct device_node *np = client->dev.of_node;
306 	u32 val;
307 	int ret;
308 
309 	pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
310 	if (!pdata) {
311 		dev_err(&client->dev,
312 			"Memory alloc for bq24735 pdata failed\n");
313 		return NULL;
314 	}
315 
316 	ret = of_property_read_u32(np, "ti,charge-current", &val);
317 	if (!ret)
318 		pdata->charge_current = val;
319 
320 	ret = of_property_read_u32(np, "ti,charge-voltage", &val);
321 	if (!ret)
322 		pdata->charge_voltage = val;
323 
324 	ret = of_property_read_u32(np, "ti,input-current", &val);
325 	if (!ret)
326 		pdata->input_current = val;
327 
328 	pdata->ext_control = of_property_read_bool(np, "ti,external-control");
329 
330 	return pdata;
331 }
332 
bq24735_charger_probe(struct i2c_client * client,const struct i2c_device_id * id)333 static int bq24735_charger_probe(struct i2c_client *client,
334 				 const struct i2c_device_id *id)
335 {
336 	int ret;
337 	struct bq24735 *charger;
338 	struct power_supply_desc *supply_desc;
339 	struct power_supply_config psy_cfg = {};
340 	char *name;
341 
342 	charger = devm_kzalloc(&client->dev, sizeof(*charger), GFP_KERNEL);
343 	if (!charger)
344 		return -ENOMEM;
345 
346 	mutex_init(&charger->lock);
347 	charger->charging = true;
348 	charger->pdata = client->dev.platform_data;
349 
350 	if (IS_ENABLED(CONFIG_OF) && !charger->pdata && client->dev.of_node)
351 		charger->pdata = bq24735_parse_dt_data(client);
352 
353 	if (!charger->pdata) {
354 		dev_err(&client->dev, "no platform data provided\n");
355 		return -EINVAL;
356 	}
357 
358 	name = (char *)charger->pdata->name;
359 	if (!name) {
360 		name = devm_kasprintf(&client->dev, GFP_KERNEL,
361 				      "bq24735@%s",
362 				      dev_name(&client->dev));
363 		if (!name) {
364 			dev_err(&client->dev, "Failed to alloc device name\n");
365 			return -ENOMEM;
366 		}
367 	}
368 
369 	charger->client = client;
370 
371 	supply_desc = &charger->charger_desc;
372 
373 	supply_desc->name = name;
374 	supply_desc->type = POWER_SUPPLY_TYPE_MAINS;
375 	supply_desc->properties = bq24735_charger_properties;
376 	supply_desc->num_properties = ARRAY_SIZE(bq24735_charger_properties);
377 	supply_desc->get_property = bq24735_charger_get_property;
378 	supply_desc->set_property = bq24735_charger_set_property;
379 	supply_desc->property_is_writeable =
380 				bq24735_charger_property_is_writeable;
381 
382 	psy_cfg.supplied_to = charger->pdata->supplied_to;
383 	psy_cfg.num_supplicants = charger->pdata->num_supplicants;
384 	psy_cfg.of_node = client->dev.of_node;
385 	psy_cfg.drv_data = charger;
386 
387 	i2c_set_clientdata(client, charger);
388 
389 	charger->status_gpio = devm_gpiod_get_optional(&client->dev,
390 						       "ti,ac-detect",
391 						       GPIOD_IN);
392 	if (IS_ERR(charger->status_gpio)) {
393 		ret = PTR_ERR(charger->status_gpio);
394 		dev_err(&client->dev, "Getting gpio failed: %d\n", ret);
395 		return ret;
396 	}
397 
398 	if (!charger->status_gpio || bq24735_charger_is_present(charger)) {
399 		ret = bq24735_read_word(client, BQ24735_MANUFACTURER_ID);
400 		if (ret < 0) {
401 			dev_err(&client->dev, "Failed to read manufacturer id : %d\n",
402 				ret);
403 			return ret;
404 		} else if (ret != 0x0040) {
405 			dev_err(&client->dev,
406 				"manufacturer id mismatch. 0x0040 != 0x%04x\n", ret);
407 			return -ENODEV;
408 		}
409 
410 		ret = bq24735_read_word(client, BQ24735_DEVICE_ID);
411 		if (ret < 0) {
412 			dev_err(&client->dev, "Failed to read device id : %d\n", ret);
413 			return ret;
414 		} else if (ret != 0x000B) {
415 			dev_err(&client->dev,
416 				"device id mismatch. 0x000b != 0x%04x\n", ret);
417 			return -ENODEV;
418 		}
419 	}
420 
421 	ret = bq24735_config_charger(charger);
422 	if (ret < 0) {
423 		dev_err(&client->dev, "failed in configuring charger");
424 		return ret;
425 	}
426 
427 	/* check for AC adapter presence */
428 	if (bq24735_charger_is_present(charger)) {
429 		ret = bq24735_enable_charging(charger);
430 		if (ret < 0) {
431 			dev_err(&client->dev, "Failed to enable charging\n");
432 			return ret;
433 		}
434 	}
435 
436 	charger->charger = devm_power_supply_register(&client->dev, supply_desc,
437 						      &psy_cfg);
438 	if (IS_ERR(charger->charger)) {
439 		ret = PTR_ERR(charger->charger);
440 		dev_err(&client->dev, "Failed to register power supply: %d\n",
441 			ret);
442 		return ret;
443 	}
444 
445 	if (client->irq) {
446 		ret = devm_request_threaded_irq(&client->dev, client->irq,
447 						NULL, bq24735_charger_isr,
448 						IRQF_TRIGGER_RISING |
449 						IRQF_TRIGGER_FALLING |
450 						IRQF_ONESHOT,
451 						supply_desc->name,
452 						charger->charger);
453 		if (ret) {
454 			dev_err(&client->dev,
455 				"Unable to register IRQ %d err %d\n",
456 				client->irq, ret);
457 			return ret;
458 		}
459 	}
460 
461 	return 0;
462 }
463 
464 static const struct i2c_device_id bq24735_charger_id[] = {
465 	{ "bq24735-charger", 0 },
466 	{}
467 };
468 MODULE_DEVICE_TABLE(i2c, bq24735_charger_id);
469 
470 static const struct of_device_id bq24735_match_ids[] = {
471 	{ .compatible = "ti,bq24735", },
472 	{ /* end */ }
473 };
474 MODULE_DEVICE_TABLE(of, bq24735_match_ids);
475 
476 static struct i2c_driver bq24735_charger_driver = {
477 	.driver = {
478 		.name = "bq24735-charger",
479 		.of_match_table = bq24735_match_ids,
480 	},
481 	.probe = bq24735_charger_probe,
482 	.id_table = bq24735_charger_id,
483 };
484 
485 module_i2c_driver(bq24735_charger_driver);
486 
487 MODULE_DESCRIPTION("bq24735 battery charging driver");
488 MODULE_AUTHOR("Darbha Sriharsha <dsriharsha@nvidia.com>");
489 MODULE_LICENSE("GPL v2");
490