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