1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Spreadtrum Communications Inc.
3 
4 #include <linux/module.h>
5 #include <linux/platform_device.h>
6 #include <linux/power_supply.h>
7 #include <linux/usb/phy.h>
8 #include <linux/regmap.h>
9 #include <linux/notifier.h>
10 #include <linux/of.h>
11 
12 /* PMIC global registers definition */
13 #define SC2731_CHARGE_STATUS		0xedc
14 #define SC2731_CHARGE_FULL		BIT(4)
15 #define SC2731_MODULE_EN1		0xc0c
16 #define SC2731_CHARGE_EN		BIT(5)
17 
18 /* SC2731 switch charger registers definition */
19 #define SC2731_CHG_CFG0			0x0
20 #define SC2731_CHG_CFG1			0x4
21 #define SC2731_CHG_CFG2			0x8
22 #define SC2731_CHG_CFG3			0xc
23 #define SC2731_CHG_CFG4			0x10
24 #define SC2731_CHG_CFG5			0x28
25 
26 /* SC2731_CHG_CFG0 register definition */
27 #define SC2731_PRECHG_RNG_SHIFT		11
28 #define SC2731_PRECHG_RNG_MASK		GENMASK(12, 11)
29 
30 #define SC2731_TERMINATION_VOL_MASK	GENMASK(2, 1)
31 #define SC2731_TERMINATION_VOL_SHIFT	1
32 #define SC2731_TERMINATION_VOL_CAL_MASK	GENMASK(8, 3)
33 #define SC2731_TERMINATION_VOL_CAL_SHIFT	3
34 #define SC2731_TERMINATION_CUR_MASK	GENMASK(2, 0)
35 
36 #define SC2731_CC_EN			BIT(13)
37 #define SC2731_CHARGER_PD		BIT(0)
38 
39 /* SC2731_CHG_CFG1 register definition */
40 #define SC2731_CUR_MASK			GENMASK(5, 0)
41 
42 /* SC2731_CHG_CFG5 register definition */
43 #define SC2731_CUR_LIMIT_SHIFT		8
44 #define SC2731_CUR_LIMIT_MASK		GENMASK(9, 8)
45 
46 /* Default current definition (unit is mA) */
47 #define SC2731_CURRENT_LIMIT_100	100
48 #define SC2731_CURRENT_LIMIT_500	500
49 #define SC2731_CURRENT_LIMIT_900	900
50 #define SC2731_CURRENT_LIMIT_2000	2000
51 #define SC2731_CURRENT_PRECHG		450
52 #define SC2731_CURRENT_STEP		50
53 
54 struct sc2731_charger_info {
55 	struct device *dev;
56 	struct regmap *regmap;
57 	struct usb_phy *usb_phy;
58 	struct notifier_block usb_notify;
59 	struct power_supply *psy_usb;
60 	struct mutex lock;
61 	bool charging;
62 	u32 base;
63 };
64 
65 static void sc2731_charger_stop_charge(struct sc2731_charger_info *info)
66 {
67 	regmap_update_bits(info->regmap, info->base + SC2731_CHG_CFG0,
68 			   SC2731_CC_EN, 0);
69 
70 	regmap_update_bits(info->regmap, info->base + SC2731_CHG_CFG0,
71 			   SC2731_CHARGER_PD, SC2731_CHARGER_PD);
72 }
73 
74 static int sc2731_charger_start_charge(struct sc2731_charger_info *info)
75 {
76 	int ret;
77 
78 	/* Enable charger constant current mode */
79 	ret = regmap_update_bits(info->regmap, info->base + SC2731_CHG_CFG0,
80 				 SC2731_CC_EN, SC2731_CC_EN);
81 	if (ret)
82 		return ret;
83 
84 	/* Start charging */
85 	return regmap_update_bits(info->regmap, info->base + SC2731_CHG_CFG0,
86 				  SC2731_CHARGER_PD, 0);
87 }
88 
89 static int sc2731_charger_set_current_limit(struct sc2731_charger_info *info,
90 					    u32 limit)
91 {
92 	u32 val;
93 
94 	if (limit <= SC2731_CURRENT_LIMIT_100)
95 		val = 0;
96 	else if (limit <= SC2731_CURRENT_LIMIT_500)
97 		val = 3;
98 	else if (limit <= SC2731_CURRENT_LIMIT_900)
99 		val = 2;
100 	else
101 		val = 1;
102 
103 	return regmap_update_bits(info->regmap, info->base + SC2731_CHG_CFG5,
104 				  SC2731_CUR_LIMIT_MASK,
105 				  val << SC2731_CUR_LIMIT_SHIFT);
106 }
107 
108 static int sc2731_charger_set_current(struct sc2731_charger_info *info, u32 cur)
109 {
110 	u32 val;
111 	int ret;
112 
113 	if (cur > SC2731_CURRENT_LIMIT_2000)
114 		cur = SC2731_CURRENT_LIMIT_2000;
115 	else if (cur < SC2731_CURRENT_PRECHG)
116 		cur = SC2731_CURRENT_PRECHG;
117 
118 	/* Calculate the step value, each step is 50 mA */
119 	val = (cur - SC2731_CURRENT_PRECHG) / SC2731_CURRENT_STEP;
120 
121 	/* Set pre-charge current as 450 mA */
122 	ret = regmap_update_bits(info->regmap, info->base + SC2731_CHG_CFG0,
123 				 SC2731_PRECHG_RNG_MASK,
124 				 0x3 << SC2731_PRECHG_RNG_SHIFT);
125 	if (ret)
126 		return ret;
127 
128 	return regmap_update_bits(info->regmap, info->base + SC2731_CHG_CFG1,
129 				  SC2731_CUR_MASK, val);
130 }
131 
132 static int sc2731_charger_get_status(struct sc2731_charger_info *info)
133 {
134 	u32 val;
135 	int ret;
136 
137 	ret = regmap_read(info->regmap, SC2731_CHARGE_STATUS, &val);
138 	if (ret)
139 		return ret;
140 
141 	if (val & SC2731_CHARGE_FULL)
142 		return POWER_SUPPLY_STATUS_FULL;
143 
144 	return POWER_SUPPLY_STATUS_CHARGING;
145 }
146 
147 static int sc2731_charger_get_current(struct sc2731_charger_info *info,
148 				      u32 *cur)
149 {
150 	int ret;
151 	u32 val;
152 
153 	ret = regmap_read(info->regmap, info->base + SC2731_CHG_CFG1, &val);
154 	if (ret)
155 		return ret;
156 
157 	val &= SC2731_CUR_MASK;
158 	*cur = val * SC2731_CURRENT_STEP + SC2731_CURRENT_PRECHG;
159 
160 	return 0;
161 }
162 
163 static int sc2731_charger_get_current_limit(struct sc2731_charger_info *info,
164 					    u32 *cur)
165 {
166 	int ret;
167 	u32 val;
168 
169 	ret = regmap_read(info->regmap, info->base + SC2731_CHG_CFG5, &val);
170 	if (ret)
171 		return ret;
172 
173 	val = (val & SC2731_CUR_LIMIT_MASK) >> SC2731_CUR_LIMIT_SHIFT;
174 
175 	switch (val) {
176 	case 0:
177 		*cur = SC2731_CURRENT_LIMIT_100;
178 		break;
179 
180 	case 1:
181 		*cur = SC2731_CURRENT_LIMIT_2000;
182 		break;
183 
184 	case 2:
185 		*cur = SC2731_CURRENT_LIMIT_900;
186 		break;
187 
188 	case 3:
189 		*cur = SC2731_CURRENT_LIMIT_500;
190 		break;
191 
192 	default:
193 		return -EINVAL;
194 	}
195 
196 	return 0;
197 }
198 
199 static int
200 sc2731_charger_usb_set_property(struct power_supply *psy,
201 				enum power_supply_property psp,
202 				const union power_supply_propval *val)
203 {
204 	struct sc2731_charger_info *info = power_supply_get_drvdata(psy);
205 	int ret;
206 
207 	mutex_lock(&info->lock);
208 
209 	if (!info->charging) {
210 		mutex_unlock(&info->lock);
211 		return -ENODEV;
212 	}
213 
214 	switch (psp) {
215 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
216 		ret = sc2731_charger_set_current(info, val->intval / 1000);
217 		if (ret < 0)
218 			dev_err(info->dev, "set charge current failed\n");
219 		break;
220 
221 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
222 		ret = sc2731_charger_set_current_limit(info,
223 						       val->intval / 1000);
224 		if (ret < 0)
225 			dev_err(info->dev, "set input current limit failed\n");
226 		break;
227 
228 	default:
229 		ret = -EINVAL;
230 	}
231 
232 	mutex_unlock(&info->lock);
233 	return ret;
234 }
235 
236 static int sc2731_charger_usb_get_property(struct power_supply *psy,
237 					   enum power_supply_property psp,
238 					   union power_supply_propval *val)
239 {
240 	struct sc2731_charger_info *info = power_supply_get_drvdata(psy);
241 	int ret = 0;
242 	u32 cur;
243 
244 	mutex_lock(&info->lock);
245 
246 	switch (psp) {
247 	case POWER_SUPPLY_PROP_STATUS:
248 		if (info->charging)
249 			val->intval = sc2731_charger_get_status(info);
250 		else
251 			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
252 		break;
253 
254 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
255 		if (!info->charging) {
256 			val->intval = 0;
257 		} else {
258 			ret = sc2731_charger_get_current(info, &cur);
259 			if (ret)
260 				goto out;
261 
262 			val->intval = cur * 1000;
263 		}
264 		break;
265 
266 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
267 		if (!info->charging) {
268 			val->intval = 0;
269 		} else {
270 			ret = sc2731_charger_get_current_limit(info, &cur);
271 			if (ret)
272 				goto out;
273 
274 			val->intval = cur * 1000;
275 		}
276 		break;
277 
278 	default:
279 		ret = -EINVAL;
280 	}
281 
282 out:
283 	mutex_unlock(&info->lock);
284 	return ret;
285 }
286 
287 static int sc2731_charger_property_is_writeable(struct power_supply *psy,
288 						enum power_supply_property psp)
289 {
290 	int ret;
291 
292 	switch (psp) {
293 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
294 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
295 		ret = 1;
296 		break;
297 
298 	default:
299 		ret = 0;
300 	}
301 
302 	return ret;
303 }
304 
305 static enum power_supply_property sc2731_usb_props[] = {
306 	POWER_SUPPLY_PROP_STATUS,
307 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
308 	POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
309 };
310 
311 static const struct power_supply_desc sc2731_charger_desc = {
312 	.name			= "sc2731_charger",
313 	.type			= POWER_SUPPLY_TYPE_USB,
314 	.properties		= sc2731_usb_props,
315 	.num_properties		= ARRAY_SIZE(sc2731_usb_props),
316 	.get_property		= sc2731_charger_usb_get_property,
317 	.set_property		= sc2731_charger_usb_set_property,
318 	.property_is_writeable	= sc2731_charger_property_is_writeable,
319 };
320 
321 static int sc2731_charger_usb_change(struct notifier_block *nb,
322 				     unsigned long limit, void *data)
323 {
324 	struct sc2731_charger_info *info =
325 		container_of(nb, struct sc2731_charger_info, usb_notify);
326 	int ret = 0;
327 
328 	mutex_lock(&info->lock);
329 
330 	if (limit > 0) {
331 		/* set current limitation and start to charge */
332 		ret = sc2731_charger_set_current_limit(info, limit);
333 		if (ret)
334 			goto out;
335 
336 		ret = sc2731_charger_set_current(info, limit);
337 		if (ret)
338 			goto out;
339 
340 		ret = sc2731_charger_start_charge(info);
341 		if (ret)
342 			goto out;
343 
344 		info->charging = true;
345 	} else {
346 		/* Stop charging */
347 		info->charging = false;
348 		sc2731_charger_stop_charge(info);
349 	}
350 
351 out:
352 	mutex_unlock(&info->lock);
353 	return ret;
354 }
355 
356 static int sc2731_charger_hw_init(struct sc2731_charger_info *info)
357 {
358 	struct power_supply_battery_info bat_info = { };
359 	u32 term_currrent, term_voltage, cur_val, vol_val;
360 	int ret;
361 
362 	/* Enable charger module */
363 	ret = regmap_update_bits(info->regmap, SC2731_MODULE_EN1,
364 				 SC2731_CHARGE_EN, SC2731_CHARGE_EN);
365 	if (ret)
366 		return ret;
367 
368 	ret = power_supply_get_battery_info(info->psy_usb, &bat_info);
369 	if (ret) {
370 		dev_warn(info->dev, "no battery information is supplied\n");
371 
372 		/*
373 		 * If no battery information is supplied, we should set
374 		 * default charge termination current to 120 mA, and default
375 		 * charge termination voltage to 4.35V.
376 		 */
377 		cur_val = 0x2;
378 		vol_val = 0x1;
379 	} else {
380 		term_currrent = bat_info.charge_term_current_ua / 1000;
381 
382 		if (term_currrent <= 90)
383 			cur_val = 0;
384 		else if (term_currrent >= 265)
385 			cur_val = 0x7;
386 		else
387 			cur_val = ((term_currrent - 90) / 25) + 1;
388 
389 		term_voltage = bat_info.constant_charge_voltage_max_uv / 1000;
390 
391 		if (term_voltage > 4500)
392 			term_voltage = 4500;
393 
394 		if (term_voltage > 4200)
395 			vol_val = (term_voltage - 4200) / 100;
396 		else
397 			vol_val = 0;
398 	}
399 
400 	/* Set charge termination current */
401 	ret = regmap_update_bits(info->regmap, info->base + SC2731_CHG_CFG2,
402 				 SC2731_TERMINATION_CUR_MASK, cur_val);
403 	if (ret)
404 		goto error;
405 
406 	/* Set charge termination voltage */
407 	ret = regmap_update_bits(info->regmap, info->base + SC2731_CHG_CFG0,
408 				 SC2731_TERMINATION_VOL_MASK |
409 				 SC2731_TERMINATION_VOL_CAL_MASK,
410 				 (vol_val << SC2731_TERMINATION_VOL_SHIFT) |
411 				 (0x6 << SC2731_TERMINATION_VOL_CAL_SHIFT));
412 	if (ret)
413 		goto error;
414 
415 	return 0;
416 
417 error:
418 	regmap_update_bits(info->regmap, SC2731_MODULE_EN1, SC2731_CHARGE_EN, 0);
419 	return ret;
420 }
421 
422 static int sc2731_charger_probe(struct platform_device *pdev)
423 {
424 	struct device_node *np = pdev->dev.of_node;
425 	struct sc2731_charger_info *info;
426 	struct power_supply_config charger_cfg = { };
427 	int ret;
428 
429 	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
430 	if (!info)
431 		return -ENOMEM;
432 
433 	mutex_init(&info->lock);
434 	info->dev = &pdev->dev;
435 
436 	info->regmap = dev_get_regmap(pdev->dev.parent, NULL);
437 	if (!info->regmap) {
438 		dev_err(&pdev->dev, "failed to get charger regmap\n");
439 		return -ENODEV;
440 	}
441 
442 	ret = of_property_read_u32(np, "reg", &info->base);
443 	if (ret) {
444 		dev_err(&pdev->dev, "failed to get register address\n");
445 		return -ENODEV;
446 	}
447 
448 	charger_cfg.drv_data = info;
449 	charger_cfg.of_node = np;
450 	info->psy_usb = devm_power_supply_register(&pdev->dev,
451 						   &sc2731_charger_desc,
452 						   &charger_cfg);
453 	if (IS_ERR(info->psy_usb)) {
454 		dev_err(&pdev->dev, "failed to register power supply\n");
455 		return PTR_ERR(info->psy_usb);
456 	}
457 
458 	ret = sc2731_charger_hw_init(info);
459 	if (ret)
460 		return ret;
461 
462 	info->usb_phy = devm_usb_get_phy_by_phandle(&pdev->dev, "phys", 0);
463 	if (IS_ERR(info->usb_phy)) {
464 		dev_err(&pdev->dev, "failed to find USB phy\n");
465 		return PTR_ERR(info->usb_phy);
466 	}
467 
468 	info->usb_notify.notifier_call = sc2731_charger_usb_change;
469 	ret = usb_register_notifier(info->usb_phy, &info->usb_notify);
470 	if (ret) {
471 		dev_err(&pdev->dev, "failed to register notifier: %d\n", ret);
472 		return ret;
473 	}
474 
475 	return 0;
476 }
477 
478 static int sc2731_charger_remove(struct platform_device *pdev)
479 {
480 	struct sc2731_charger_info *info = platform_get_drvdata(pdev);
481 
482 	usb_unregister_notifier(info->usb_phy, &info->usb_notify);
483 
484 	return 0;
485 }
486 
487 static const struct of_device_id sc2731_charger_of_match[] = {
488 	{ .compatible = "sprd,sc2731-charger", },
489 	{ }
490 };
491 
492 static struct platform_driver sc2731_charger_driver = {
493 	.driver = {
494 		.name = "sc2731-charger",
495 		.of_match_table = sc2731_charger_of_match,
496 	},
497 	.probe = sc2731_charger_probe,
498 	.remove = sc2731_charger_remove,
499 };
500 
501 module_platform_driver(sc2731_charger_driver);
502 
503 MODULE_DESCRIPTION("Spreadtrum SC2731 Charger Driver");
504 MODULE_LICENSE("GPL v2");
505