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