xref: /linux/drivers/power/supply/rk817_charger.c (revision 448ecd5771e255629bef0fb16c9b78c4bbd7bd56)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Charger Driver for Rockchip rk817
4  *
5  * Copyright (c) 2021 Maya Matuszczyk <maccraft123mc@gmail.com>
6  *
7  * Authors: Maya Matuszczyk <maccraft123mc@gmail.com>
8  *	    Chris Morgan <macromorgan@hotmail.com>
9  */
10 
11 #include <linux/unaligned.h>
12 #include <linux/devm-helpers.h>
13 #include <linux/mfd/rk808.h>
14 #include <linux/irq.h>
15 #include <linux/of.h>
16 #include <linux/platform_device.h>
17 #include <linux/power_supply.h>
18 #include <linux/regmap.h>
19 
20 /* Charging statuses reported by hardware register */
21 enum rk817_charge_status {
22 	CHRG_OFF,
23 	DEAD_CHRG,
24 	TRICKLE_CHRG,
25 	CC_OR_CV_CHRG,
26 	CHARGE_FINISH,
27 	USB_OVER_VOL,
28 	BAT_TMP_ERR,
29 	BAT_TIM_ERR,
30 };
31 
32 /*
33  * Max charging current read to/written from hardware register.
34  * Note how highest value corresponding to 0x7 is the lowest
35  * current, this is per the datasheet.
36  */
37 enum rk817_chg_cur {
38 	CHG_1A,
39 	CHG_1_5A,
40 	CHG_2A,
41 	CHG_2_5A,
42 	CHG_2_75A,
43 	CHG_3A,
44 	CHG_3_5A,
45 	CHG_0_5A,
46 };
47 
48 struct rk817_charger {
49 	struct device *dev;
50 	struct rk808 *rk808;
51 
52 	struct power_supply *bat_ps;
53 	struct power_supply *chg_ps;
54 	bool plugged_in;
55 	bool battery_present;
56 
57 	/*
58 	 * voltage_k and voltage_b values are used to calibrate the ADC
59 	 * voltage readings. While they are documented in the BSP kernel and
60 	 * datasheet as voltage_k and voltage_b, there is no further
61 	 * information explaining them in more detail.
62 	 */
63 
64 	uint32_t voltage_k;
65 	uint32_t voltage_b;
66 
67 	/*
68 	 * soc - state of charge - like the BSP this is stored as a percentage,
69 	 * to the thousandth. BSP has a display state of charge (dsoc) and a
70 	 * remaining state of charge (rsoc). This value will be used for both
71 	 * purposes here so we don't do any fancy math to try and "smooth" the
72 	 * charge and just report it as it is. Note for example an soc of 100
73 	 * is stored as 100000, an soc of 50 is stored as 50000, etc.
74 	 */
75 	int soc;
76 
77 	/*
78 	 * Capacity of battery when fully charged, equal or less than design
79 	 * capacity depending upon wear. BSP kernel saves to nvram in mAh,
80 	 * so this value is in mAh not the standard uAh.
81 	 */
82 	int fcc_mah;
83 
84 	/*
85 	 * Calibrate the SOC on a fully charged battery, this way we can use
86 	 * the calibrated SOC value to correct for columb counter drift.
87 	 */
88 	bool soc_cal;
89 
90 	/* Implementation specific immutable properties from device tree */
91 	int res_div;
92 	int sleep_enter_current_ua;
93 	int sleep_filter_current_ua;
94 	int bat_charge_full_design_uah;
95 	int bat_voltage_min_design_uv;
96 	int bat_voltage_max_design_uv;
97 
98 	/* Values updated periodically by driver for display. */
99 	int charge_now_uah;
100 	int volt_avg_uv;
101 	int cur_avg_ua;
102 	int max_chg_cur_ua;
103 	int max_chg_volt_uv;
104 	int charge_status;
105 	int charger_input_volt_avg_uv;
106 
107 	/* Work queue to periodically update values. */
108 	struct delayed_work work;
109 };
110 
111 /* ADC coefficients extracted from BSP kernel */
112 #define ADC_TO_CURRENT(adc_value, res_div)	\
113 	(adc_value * 172 / res_div)
114 
115 #define CURRENT_TO_ADC(current, samp_res)	\
116 	(current * samp_res / 172)
117 
118 #define CHARGE_TO_ADC(capacity, res_div)	\
119 	(capacity * res_div * 3600 / 172 * 1000)
120 
121 #define ADC_TO_CHARGE_UAH(adc_value, res_div)	\
122 	(adc_value / 3600 * 172 / res_div)
123 
rk817_chg_cur_to_reg(u32 chg_cur_ma)124 static int rk817_chg_cur_to_reg(u32 chg_cur_ma)
125 {
126 	if (chg_cur_ma >= 3500)
127 		return CHG_3_5A;
128 	else if (chg_cur_ma >= 3000)
129 		return CHG_3A;
130 	else if (chg_cur_ma >= 2750)
131 		return CHG_2_75A;
132 	else if (chg_cur_ma >= 2500)
133 		return CHG_2_5A;
134 	else if (chg_cur_ma >= 2000)
135 		return CHG_2A;
136 	else if (chg_cur_ma >= 1500)
137 		return CHG_1_5A;
138 	else if (chg_cur_ma >= 1000)
139 		return CHG_1A;
140 	else if (chg_cur_ma >= 500)
141 		return CHG_0_5A;
142 	else
143 		return -EINVAL;
144 }
145 
rk817_chg_cur_from_reg(u8 reg)146 static int rk817_chg_cur_from_reg(u8 reg)
147 {
148 	switch (reg) {
149 	case CHG_0_5A:
150 		return 500000;
151 	case CHG_1A:
152 		return 1000000;
153 	case CHG_1_5A:
154 		return 1500000;
155 	case CHG_2A:
156 		return 2000000;
157 	case CHG_2_5A:
158 		return 2500000;
159 	case CHG_2_75A:
160 		return 2750000;
161 	case CHG_3A:
162 		return 3000000;
163 	case CHG_3_5A:
164 		return 3500000;
165 	default:
166 		return -EINVAL;
167 	}
168 }
169 
rk817_bat_calib_vol(struct rk817_charger * charger)170 static void rk817_bat_calib_vol(struct rk817_charger *charger)
171 {
172 	uint32_t vcalib0 = 0;
173 	uint32_t vcalib1 = 0;
174 	u8 bulk_reg[2];
175 
176 	/* calibrate voltage */
177 	regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_VCALIB0_H,
178 			 bulk_reg, 2);
179 	vcalib0 = get_unaligned_be16(bulk_reg);
180 
181 	regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_VCALIB1_H,
182 			 bulk_reg, 2);
183 	vcalib1 = get_unaligned_be16(bulk_reg);
184 
185 	/* values were taken from BSP kernel */
186 	charger->voltage_k = (4025 - 2300) * 1000 /
187 			     ((vcalib1 - vcalib0) ? (vcalib1 - vcalib0) : 1);
188 	charger->voltage_b = 4025 - (charger->voltage_k * vcalib1) / 1000;
189 }
190 
rk817_bat_calib_cur(struct rk817_charger * charger)191 static void rk817_bat_calib_cur(struct rk817_charger *charger)
192 {
193 	u8 bulk_reg[2];
194 
195 	/* calibrate current */
196 	regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_IOFFSET_H,
197 			 bulk_reg, 2);
198 	regmap_bulk_write(charger->rk808->regmap, RK817_GAS_GAUGE_CAL_OFFSET_H,
199 			  bulk_reg, 2);
200 }
201 
202 /*
203  * note that only the fcc_mah is really used by this driver, the other values
204  * are to ensure we can remain backwards compatible with the BSP kernel.
205  */
rk817_record_battery_nvram_values(struct rk817_charger * charger)206 static int rk817_record_battery_nvram_values(struct rk817_charger *charger)
207 {
208 	u8 bulk_reg[3];
209 	int ret, rsoc;
210 
211 	/*
212 	 * write the soc value to the nvram location used by the BSP kernel
213 	 * for the dsoc value.
214 	 */
215 	put_unaligned_le24(charger->soc, bulk_reg);
216 	ret = regmap_bulk_write(charger->rk808->regmap, RK817_GAS_GAUGE_BAT_R1,
217 				bulk_reg, 3);
218 	if (ret < 0)
219 		return ret;
220 	/*
221 	 * write the remaining capacity in mah to the nvram location used by
222 	 * the BSP kernel for the rsoc value.
223 	 */
224 	rsoc = (charger->soc * charger->fcc_mah) / 100000;
225 	put_unaligned_le24(rsoc, bulk_reg);
226 	ret = regmap_bulk_write(charger->rk808->regmap, RK817_GAS_GAUGE_DATA0,
227 				bulk_reg, 3);
228 	if (ret < 0)
229 		return ret;
230 	/* write the fcc_mah in mAh, just as the BSP kernel does. */
231 	put_unaligned_le24(charger->fcc_mah, bulk_reg);
232 	ret = regmap_bulk_write(charger->rk808->regmap, RK817_GAS_GAUGE_DATA3,
233 				bulk_reg, 3);
234 	if (ret < 0)
235 		return ret;
236 
237 	return 0;
238 }
239 
rk817_bat_calib_cap(struct rk817_charger * charger)240 static int rk817_bat_calib_cap(struct rk817_charger *charger)
241 {
242 	struct rk808 *rk808 = charger->rk808;
243 	int charge_now, charge_now_adc;
244 	u8 bulk_reg[4];
245 
246 	/* Don't do anything if there's no battery. */
247 	if (!charger->battery_present)
248 		return 0;
249 
250 	/*
251 	 * When resuming from suspend, sometimes the voltage value would be
252 	 * incorrect. BSP would simply wait two seconds and try reading the
253 	 * values again. Do not do any sort of calibration activity when the
254 	 * reported value is incorrect. The next scheduled update of battery
255 	 * vaules should then return valid data and the driver can continue.
256 	 * Use 2.7v as the sanity value because per the datasheet the PMIC
257 	 * can in no way support a battery voltage lower than this. BSP only
258 	 * checked for values too low, but I'm adding in a check for values
259 	 * too high just in case; again the PMIC can in no way support
260 	 * voltages above 4.45v, so this seems like a good value.
261 	 */
262 	if ((charger->volt_avg_uv < 2700000) || (charger->volt_avg_uv > 4450000)) {
263 		dev_dbg(charger->dev,
264 			"Battery voltage of %d is invalid, ignoring.\n",
265 			charger->volt_avg_uv);
266 		return -EINVAL;
267 	}
268 
269 	/* Calibrate the soc and fcc on a fully charged battery */
270 
271 	if (charger->charge_status == CHARGE_FINISH && (!charger->soc_cal)) {
272 		/*
273 		 * soc should be 100000 and columb counter should show the full
274 		 * charge capacity. Note that if the device is unplugged for a
275 		 * period of several days the columb counter will have a large
276 		 * margin of error, so setting it back to the full charge on
277 		 * a completed charge cycle should correct this (my device was
278 		 * showing 33% battery after 3 days unplugged when it should
279 		 * have been closer to 95% based on voltage and charge
280 		 * current).
281 		 */
282 
283 		charger->soc = 100000;
284 		charge_now_adc = CHARGE_TO_ADC(charger->fcc_mah,
285 					       charger->res_div);
286 		put_unaligned_be32(charge_now_adc, bulk_reg);
287 		regmap_bulk_write(rk808->regmap, RK817_GAS_GAUGE_Q_INIT_H3,
288 				  bulk_reg, 4);
289 
290 		charger->soc_cal = 1;
291 		dev_dbg(charger->dev,
292 			"Fully charged. SOC is %d, full capacity is %d\n",
293 			charger->soc, charger->fcc_mah * 1000);
294 	}
295 
296 	/*
297 	 * The columb counter can drift up slightly, so we should correct for
298 	 * it. But don't correct it until we're at 100% soc.
299 	 */
300 	if (charger->charge_status == CHARGE_FINISH && charger->soc_cal) {
301 		regmap_bulk_read(rk808->regmap, RK817_GAS_GAUGE_Q_PRES_H3,
302 				 bulk_reg, 4);
303 		charge_now_adc = get_unaligned_be32(bulk_reg);
304 		if (charge_now_adc < 0)
305 			return charge_now_adc;
306 		charge_now = ADC_TO_CHARGE_UAH(charge_now_adc,
307 					       charger->res_div);
308 
309 		/*
310 		 * Re-init columb counter with updated values to correct drift.
311 		 */
312 		if (charge_now / 1000 > charger->fcc_mah) {
313 			dev_dbg(charger->dev,
314 				"Recalibrating columb counter to %d uah\n",
315 				charge_now);
316 			/*
317 			 * Order of operations matters here to ensure we keep
318 			 * enough precision until the last step to keep from
319 			 * making needless updates to columb counter.
320 			 */
321 			charge_now_adc = CHARGE_TO_ADC(charger->fcc_mah,
322 					 charger->res_div);
323 			put_unaligned_be32(charge_now_adc, bulk_reg);
324 			regmap_bulk_write(rk808->regmap,
325 					  RK817_GAS_GAUGE_Q_INIT_H3,
326 					  bulk_reg, 4);
327 		}
328 	}
329 
330 	rk817_record_battery_nvram_values(charger);
331 
332 	return 0;
333 }
334 
rk817_read_props(struct rk817_charger * charger)335 static void rk817_read_props(struct rk817_charger *charger)
336 {
337 	int tmp, reg;
338 	u8 bulk_reg[4];
339 
340 	/*
341 	 * Recalibrate voltage and current readings if we need to BSP does both
342 	 * on CUR_CALIB_UPD, ignoring VOL_CALIB_UPD. Curiously enough, both
343 	 * documentation and the BSP show that you perform an update if bit 7
344 	 * is 1, but you clear the status by writing a 1 to bit 7.
345 	 */
346 	regmap_read(charger->rk808->regmap, RK817_GAS_GAUGE_ADC_CONFIG1, &reg);
347 	if (reg & RK817_VOL_CUR_CALIB_UPD) {
348 		rk817_bat_calib_cur(charger);
349 		rk817_bat_calib_vol(charger);
350 		regmap_write_bits(charger->rk808->regmap,
351 				  RK817_GAS_GAUGE_ADC_CONFIG1,
352 				  RK817_VOL_CUR_CALIB_UPD,
353 				  RK817_VOL_CUR_CALIB_UPD);
354 	}
355 
356 	/* Update reported charge. */
357 	regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_Q_PRES_H3,
358 			 bulk_reg, 4);
359 	tmp = get_unaligned_be32(bulk_reg);
360 	charger->charge_now_uah = ADC_TO_CHARGE_UAH(tmp, charger->res_div);
361 	if (charger->charge_now_uah < 0)
362 		charger->charge_now_uah = 0;
363 	if (charger->charge_now_uah > charger->fcc_mah * 1000)
364 		charger->charge_now_uah = charger->fcc_mah * 1000;
365 
366 	/* Update soc based on reported charge. */
367 	charger->soc = charger->charge_now_uah * 100 / charger->fcc_mah;
368 
369 	/* Update reported voltage. */
370 	regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_BAT_VOL_H,
371 			 bulk_reg, 2);
372 	tmp = get_unaligned_be16(bulk_reg);
373 	charger->volt_avg_uv = (charger->voltage_k * tmp) + 1000 *
374 				charger->voltage_b;
375 
376 	/*
377 	 * Update reported current. Note value from registers is a signed 16
378 	 * bit int.
379 	 */
380 	regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_BAT_CUR_H,
381 			 bulk_reg, 2);
382 	tmp = (short int)get_unaligned_be16(bulk_reg);
383 	charger->cur_avg_ua = ADC_TO_CURRENT(tmp, charger->res_div);
384 
385 	/*
386 	 * Update the max charge current. This value shouldn't change, but we
387 	 * can read it to report what the PMIC says it is instead of simply
388 	 * returning the default value.
389 	 */
390 	regmap_read(charger->rk808->regmap, RK817_PMIC_CHRG_OUT, &reg);
391 	charger->max_chg_cur_ua =
392 		rk817_chg_cur_from_reg(reg & RK817_CHRG_CUR_SEL);
393 
394 	/*
395 	 * Update max charge voltage. Like the max charge current this value
396 	 * shouldn't change, but we can report what the PMIC says.
397 	 */
398 	regmap_read(charger->rk808->regmap, RK817_PMIC_CHRG_OUT, &reg);
399 	charger->max_chg_volt_uv = ((((reg & RK817_CHRG_VOL_SEL) >> 4) *
400 				    50000) + 4100000);
401 
402 	/* Check if battery still present. */
403 	regmap_read(charger->rk808->regmap, RK817_PMIC_CHRG_STS, &reg);
404 	charger->battery_present = (reg & RK817_BAT_EXS);
405 
406 	/* Get which type of charge we are using (if any). */
407 	regmap_read(charger->rk808->regmap, RK817_PMIC_CHRG_STS, &reg);
408 	charger->charge_status = (reg >> 4) & 0x07;
409 
410 	/*
411 	 * Get charger input voltage. Note that on my example hardware (an
412 	 * Odroid Go Advance) the voltage of the power connector is measured
413 	 * on the register labelled USB in the datasheet; I don't know if this
414 	 * is how it is designed or just a quirk of the implementation. I
415 	 * believe this will also measure the voltage of the USB output when in
416 	 * OTG mode, if that is the case we may need to change this in the
417 	 * future to return 0 if the power supply status is offline (I can't
418 	 * test this with my current implementation. Also, when the voltage
419 	 * should be zero sometimes the ADC still shows a single bit (which
420 	 * would register as 20000uv). When this happens set it to 0.
421 	 */
422 	regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_USB_VOL_H,
423 			 bulk_reg, 2);
424 	reg = get_unaligned_be16(bulk_reg);
425 	if (reg > 1) {
426 		tmp = ((charger->voltage_k * reg / 1000 + charger->voltage_b) *
427 		       60 / 46);
428 		charger->charger_input_volt_avg_uv = tmp * 1000;
429 	} else {
430 		charger->charger_input_volt_avg_uv = 0;
431 	}
432 
433 	/* Calibrate battery capacity and soc. */
434 	rk817_bat_calib_cap(charger);
435 }
436 
rk817_bat_get_prop(struct power_supply * ps,enum power_supply_property prop,union power_supply_propval * val)437 static int rk817_bat_get_prop(struct power_supply *ps,
438 		enum power_supply_property prop,
439 		union power_supply_propval *val)
440 {
441 	struct rk817_charger *charger = power_supply_get_drvdata(ps);
442 
443 	switch (prop) {
444 	case POWER_SUPPLY_PROP_PRESENT:
445 		val->intval = charger->battery_present;
446 		break;
447 	case POWER_SUPPLY_PROP_STATUS:
448 		if (charger->cur_avg_ua < 0) {
449 			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
450 			break;
451 		}
452 		switch (charger->charge_status) {
453 		case CHRG_OFF:
454 			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
455 			break;
456 		/*
457 		 * Dead charge is documented, but not explained. I never
458 		 * observed it but assume it's a pre-charge for a dead
459 		 * battery.
460 		 */
461 		case DEAD_CHRG:
462 		case TRICKLE_CHRG:
463 		case CC_OR_CV_CHRG:
464 			val->intval = POWER_SUPPLY_STATUS_CHARGING;
465 			break;
466 		case CHARGE_FINISH:
467 			val->intval = POWER_SUPPLY_STATUS_FULL;
468 			break;
469 		default:
470 			val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
471 			return -EINVAL;
472 
473 		}
474 		break;
475 	case POWER_SUPPLY_PROP_CHARGE_TYPE:
476 		switch (charger->charge_status) {
477 		case CHRG_OFF:
478 		case CHARGE_FINISH:
479 			val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
480 			break;
481 		case TRICKLE_CHRG:
482 			val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
483 			break;
484 		case DEAD_CHRG:
485 		case CC_OR_CV_CHRG:
486 			val->intval = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
487 			break;
488 		default:
489 			val->intval = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
490 			break;
491 		}
492 		break;
493 	case POWER_SUPPLY_PROP_CHARGE_FULL:
494 		val->intval = charger->fcc_mah * 1000;
495 		break;
496 	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
497 		val->intval = charger->bat_charge_full_design_uah;
498 		break;
499 	case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
500 		val->intval = 0;
501 		break;
502 	case POWER_SUPPLY_PROP_CHARGE_NOW:
503 		val->intval = charger->charge_now_uah;
504 		break;
505 	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
506 		val->intval = charger->bat_voltage_min_design_uv;
507 		break;
508 	case POWER_SUPPLY_PROP_CAPACITY:
509 		/* Add 500 so that values like 99999 are 100% not 99%. */
510 		val->intval = (charger->soc + 500) / 1000;
511 		if (val->intval > 100)
512 			val->intval = 100;
513 		if (val->intval < 0)
514 			val->intval = 0;
515 		break;
516 	case POWER_SUPPLY_PROP_VOLTAGE_AVG:
517 		val->intval = charger->volt_avg_uv;
518 		break;
519 	case POWER_SUPPLY_PROP_CURRENT_AVG:
520 		val->intval = charger->cur_avg_ua;
521 		break;
522 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
523 		val->intval = charger->max_chg_cur_ua;
524 		break;
525 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
526 		val->intval = charger->max_chg_volt_uv;
527 		break;
528 	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
529 		val->intval = charger->bat_voltage_max_design_uv;
530 		break;
531 	default:
532 		return -EINVAL;
533 	}
534 	return 0;
535 }
536 
rk817_chg_get_prop(struct power_supply * ps,enum power_supply_property prop,union power_supply_propval * val)537 static int rk817_chg_get_prop(struct power_supply *ps,
538 			      enum power_supply_property prop,
539 			      union power_supply_propval *val)
540 {
541 	struct rk817_charger *charger = power_supply_get_drvdata(ps);
542 
543 	switch (prop) {
544 	case POWER_SUPPLY_PROP_ONLINE:
545 		val->intval = charger->plugged_in;
546 		break;
547 	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
548 		/* max voltage from datasheet at 5.5v (default 5.0v) */
549 		val->intval = 5500000;
550 		break;
551 	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
552 		/* min voltage from datasheet at 3.8v (default 5.0v) */
553 		val->intval = 3800000;
554 		break;
555 	case POWER_SUPPLY_PROP_VOLTAGE_AVG:
556 		val->intval = charger->charger_input_volt_avg_uv;
557 		break;
558 	/*
559 	 * While it's possible that other implementations could use different
560 	 * USB types, the current implementation for this PMIC (the Odroid Go
561 	 * Advance) only uses a dedicated charging port with no rx/tx lines.
562 	 */
563 	case POWER_SUPPLY_PROP_USB_TYPE:
564 		val->intval = POWER_SUPPLY_USB_TYPE_DCP;
565 		break;
566 	default:
567 		return -EINVAL;
568 	}
569 	return 0;
570 
571 }
572 
rk817_plug_in_isr(int irq,void * cg)573 static irqreturn_t rk817_plug_in_isr(int irq, void *cg)
574 {
575 	struct rk817_charger *charger;
576 
577 	charger = (struct rk817_charger *)cg;
578 	charger->plugged_in = 1;
579 	power_supply_changed(charger->chg_ps);
580 	power_supply_changed(charger->bat_ps);
581 	/* try to recalibrate capacity if we hit full charge. */
582 	charger->soc_cal = 0;
583 
584 	rk817_read_props(charger);
585 
586 	dev_dbg(charger->dev, "Power Cord Inserted\n");
587 
588 	return IRQ_HANDLED;
589 }
590 
rk817_plug_out_isr(int irq,void * cg)591 static irqreturn_t rk817_plug_out_isr(int irq, void *cg)
592 {
593 	struct rk817_charger *charger;
594 	struct rk808 *rk808;
595 
596 	charger = (struct rk817_charger *)cg;
597 	rk808 = charger->rk808;
598 	charger->plugged_in = 0;
599 	power_supply_changed(charger->bat_ps);
600 	power_supply_changed(charger->chg_ps);
601 
602 	/*
603 	 * For some reason the bits of RK817_PMIC_CHRG_IN reset whenever the
604 	 * power cord is unplugged. This was not documented in the BSP kernel
605 	 * or the datasheet and only discovered by trial and error. Set minimum
606 	 * USB input voltage to 4.5v and enable USB voltage input limit.
607 	 */
608 	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_IN,
609 			  RK817_USB_VLIM_SEL, (0x05 << 4));
610 	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_IN, RK817_USB_VLIM_EN,
611 			  (0x01 << 7));
612 
613 	/*
614 	 * Set average USB input current limit to 1.5A and enable USB current
615 	 * input limit.
616 	 */
617 	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_IN,
618 			  RK817_USB_ILIM_SEL, 0x03);
619 	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_IN, RK817_USB_ILIM_EN,
620 			  (0x01 << 3));
621 
622 	rk817_read_props(charger);
623 
624 	dev_dbg(charger->dev, "Power Cord Removed\n");
625 
626 	return IRQ_HANDLED;
627 }
628 
rk817_bat_set_prop(struct power_supply * ps,enum power_supply_property prop,const union power_supply_propval * val)629 static int rk817_bat_set_prop(struct power_supply *ps,
630 			      enum power_supply_property prop,
631 			      const union power_supply_propval *val)
632 {
633 	struct rk817_charger *charger = power_supply_get_drvdata(ps);
634 
635 	switch (prop) {
636 	case POWER_SUPPLY_PROP_CHARGE_FULL:
637 		if ((val->intval < 500000) ||
638 			(val->intval > charger->bat_charge_full_design_uah))
639 			return -EINVAL;
640 		charger->fcc_mah = val->intval / 1000;
641 		return rk817_bat_calib_cap(charger);
642 	default:
643 		return -EINVAL;
644 	}
645 }
646 
647 static enum power_supply_property rk817_bat_props[] = {
648 	POWER_SUPPLY_PROP_PRESENT,
649 	POWER_SUPPLY_PROP_STATUS,
650 	POWER_SUPPLY_PROP_CHARGE_TYPE,
651 	POWER_SUPPLY_PROP_CHARGE_FULL,
652 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
653 	POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN,
654 	POWER_SUPPLY_PROP_CHARGE_NOW,
655 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
656 	POWER_SUPPLY_PROP_VOLTAGE_AVG,
657 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
658 	POWER_SUPPLY_PROP_CURRENT_AVG,
659 	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
660 	POWER_SUPPLY_PROP_CAPACITY,
661 	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
662 };
663 
664 static enum power_supply_property rk817_chg_props[] = {
665 	POWER_SUPPLY_PROP_ONLINE,
666 	POWER_SUPPLY_PROP_USB_TYPE,
667 	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
668 	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
669 	POWER_SUPPLY_PROP_VOLTAGE_AVG,
670 };
671 
rk817_bat_prop_writeable(struct power_supply * psy,enum power_supply_property psp)672 static int rk817_bat_prop_writeable(struct power_supply *psy,
673 				    enum power_supply_property psp)
674 {
675 	switch (psp) {
676 	case POWER_SUPPLY_PROP_CHARGE_FULL:
677 		return 1;
678 	default:
679 		return 0;
680 	}
681 }
682 
683 static const struct power_supply_desc rk817_bat_desc = {
684 	.name = "rk817-battery",
685 	.type = POWER_SUPPLY_TYPE_BATTERY,
686 	.properties = rk817_bat_props,
687 	.property_is_writeable	= rk817_bat_prop_writeable,
688 	.num_properties = ARRAY_SIZE(rk817_bat_props),
689 	.get_property = rk817_bat_get_prop,
690 	.set_property = rk817_bat_set_prop,
691 };
692 
693 static const struct power_supply_desc rk817_chg_desc = {
694 	.name = "rk817-charger",
695 	.type = POWER_SUPPLY_TYPE_USB,
696 	.usb_types = BIT(POWER_SUPPLY_USB_TYPE_DCP) |
697 		     BIT(POWER_SUPPLY_USB_TYPE_UNKNOWN),
698 	.properties = rk817_chg_props,
699 	.num_properties = ARRAY_SIZE(rk817_chg_props),
700 	.get_property = rk817_chg_get_prop,
701 };
702 
rk817_read_battery_nvram_values(struct rk817_charger * charger)703 static int rk817_read_battery_nvram_values(struct rk817_charger *charger)
704 {
705 	u8 bulk_reg[3];
706 	int ret;
707 
708 	/* Read the nvram data for full charge capacity. */
709 	ret = regmap_bulk_read(charger->rk808->regmap,
710 			       RK817_GAS_GAUGE_DATA3, bulk_reg, 3);
711 	if (ret < 0)
712 		return ret;
713 	charger->fcc_mah = get_unaligned_le24(bulk_reg);
714 
715 	/*
716 	 * Sanity checking for values equal to zero or less than would be
717 	 * practical for this device (BSP Kernel assumes 500mAH or less) for
718 	 * practicality purposes. Also check if the value is too large and
719 	 * correct it.
720 	 */
721 	if ((charger->fcc_mah < 500) ||
722 	   ((charger->fcc_mah * 1000) > charger->bat_charge_full_design_uah)) {
723 		dev_info(charger->dev,
724 			 "Invalid NVRAM max charge, setting to %u uAH\n",
725 			 charger->bat_charge_full_design_uah);
726 		charger->fcc_mah = charger->bat_charge_full_design_uah / 1000;
727 	}
728 
729 	/*
730 	 * Read the nvram for state of charge. Sanity check for values greater
731 	 * than 100 (10000) or less than 0, because other things (BSP kernels,
732 	 * U-Boot, or even i2cset) can write to this register. If the value is
733 	 * off it should get corrected automatically when the voltage drops to
734 	 * the min (soc is 0) or when the battery is full (soc is 100).
735 	 */
736 	ret = regmap_bulk_read(charger->rk808->regmap,
737 			       RK817_GAS_GAUGE_BAT_R1, bulk_reg, 3);
738 	if (ret < 0)
739 		return ret;
740 	charger->soc = get_unaligned_le24(bulk_reg);
741 	if (charger->soc > 10000)
742 		charger->soc = 10000;
743 	if (charger->soc < 0)
744 		charger->soc = 0;
745 
746 	return 0;
747 }
748 
749 static int
rk817_read_or_set_full_charge_on_boot(struct rk817_charger * charger,struct power_supply_battery_info * bat_info)750 rk817_read_or_set_full_charge_on_boot(struct rk817_charger *charger,
751 				struct power_supply_battery_info *bat_info)
752 {
753 	struct rk808 *rk808 = charger->rk808;
754 	u8 bulk_reg[4];
755 	u32 boot_voltage, boot_charge_mah;
756 	int ret, reg, off_time, tmp;
757 	bool first_boot;
758 
759 	/*
760 	 * Check if the battery is uninitalized. If it is, the columb counter
761 	 * needs to be set up.
762 	 */
763 	ret = regmap_read(rk808->regmap, RK817_GAS_GAUGE_GG_STS, &reg);
764 	if (ret < 0)
765 		return ret;
766 	first_boot = reg & RK817_BAT_CON;
767 	/*
768 	 * If the battery is uninitialized, use the poweron voltage and an ocv
769 	 * lookup to guess our charge. The number won't be very accurate until
770 	 * we hit either our minimum voltage (0%) or full charge (100%).
771 	 */
772 	if (first_boot) {
773 		regmap_bulk_read(rk808->regmap, RK817_GAS_GAUGE_PWRON_VOL_H,
774 				 bulk_reg, 2);
775 		tmp = get_unaligned_be16(bulk_reg);
776 		boot_voltage = (charger->voltage_k * tmp) +
777 				1000 * charger->voltage_b;
778 		/*
779 		 * Since only implementation has no working thermistor, assume
780 		 * 20C for OCV lookup. If lookup fails, report error with OCV
781 		 * table.
782 		 */
783 		charger->soc = power_supply_batinfo_ocv2cap(bat_info,
784 							    boot_voltage,
785 							    20) * 1000;
786 		if (charger->soc < 0)
787 			charger->soc = 0;
788 
789 		/* Guess that full charge capacity is the design capacity */
790 		charger->fcc_mah = charger->bat_charge_full_design_uah / 1000;
791 		/*
792 		 * Set battery as "set up". BSP driver uses this value even
793 		 * though datasheet claims it's a read-only value.
794 		 */
795 		regmap_write_bits(rk808->regmap, RK817_GAS_GAUGE_GG_STS,
796 				  RK817_BAT_CON, 0);
797 		/* Save nvram values */
798 		ret = rk817_record_battery_nvram_values(charger);
799 		if (ret < 0)
800 			return ret;
801 	} else {
802 		ret = rk817_read_battery_nvram_values(charger);
803 		if (ret < 0)
804 			return ret;
805 
806 		regmap_bulk_read(rk808->regmap, RK817_GAS_GAUGE_Q_PRES_H3,
807 				 bulk_reg, 4);
808 		tmp = get_unaligned_be32(bulk_reg);
809 		if (tmp < 0)
810 			tmp = 0;
811 		boot_charge_mah = ADC_TO_CHARGE_UAH(tmp,
812 						    charger->res_div) / 1000;
813 		/*
814 		 * Check if the columb counter has been off for more than 30
815 		 * minutes as it tends to drift downward. If so, re-init soc
816 		 * with the boot voltage instead. Note the unit values for the
817 		 * OFF_CNT register appear to be in decaminutes and stops
818 		 * counting at 2550 (0xFF) minutes. BSP kernel used OCV, but
819 		 * for me occasionally that would show invalid values. Boot
820 		 * voltage is only accurate for me on first poweron (not
821 		 * reboots), but we shouldn't ever encounter an OFF_CNT more
822 		 * than 0 on a reboot anyway.
823 		 */
824 		regmap_read(rk808->regmap, RK817_GAS_GAUGE_OFF_CNT, &off_time);
825 		if (off_time >= 3) {
826 			regmap_bulk_read(rk808->regmap,
827 					 RK817_GAS_GAUGE_PWRON_VOL_H,
828 					 bulk_reg, 2);
829 			tmp = get_unaligned_be16(bulk_reg);
830 			boot_voltage = (charger->voltage_k * tmp) +
831 					1000 * charger->voltage_b;
832 			charger->soc =
833 				power_supply_batinfo_ocv2cap(bat_info,
834 							     boot_voltage,
835 							     20) * 1000;
836 		} else {
837 			charger->soc = (boot_charge_mah * 1000 * 100 /
838 					charger->fcc_mah);
839 		}
840 	}
841 
842 	/*
843 	 * Now we have our full charge capacity and soc, init the columb
844 	 * counter.
845 	 */
846 	boot_charge_mah = charger->soc * charger->fcc_mah / 100 / 1000;
847 	if (boot_charge_mah > charger->fcc_mah)
848 		boot_charge_mah = charger->fcc_mah;
849 	tmp = CHARGE_TO_ADC(boot_charge_mah, charger->res_div);
850 	put_unaligned_be32(tmp, bulk_reg);
851 	ret = regmap_bulk_write(rk808->regmap, RK817_GAS_GAUGE_Q_INIT_H3,
852 			  bulk_reg, 4);
853 	if (ret < 0)
854 		return ret;
855 
856 	/* Set QMAX value to max design capacity. */
857 	tmp = CHARGE_TO_ADC((charger->bat_charge_full_design_uah / 1000),
858 			    charger->res_div);
859 	put_unaligned_be32(tmp, bulk_reg);
860 	ret = regmap_bulk_write(rk808->regmap, RK817_GAS_GAUGE_Q_MAX_H3,
861 				bulk_reg, 4);
862 	if (ret < 0)
863 		return ret;
864 
865 	return 0;
866 }
867 
rk817_battery_init(struct rk817_charger * charger,struct power_supply_battery_info * bat_info)868 static int rk817_battery_init(struct rk817_charger *charger,
869 			      struct power_supply_battery_info *bat_info)
870 {
871 	struct rk808 *rk808 = charger->rk808;
872 	u32 tmp, max_chg_vol_mv, max_chg_cur_ma;
873 	u8 max_chg_vol_reg, chg_term_i_reg;
874 	int ret, chg_term_ma, max_chg_cur_reg;
875 	u8 bulk_reg[2];
876 
877 	/* Get initial plug state */
878 	regmap_read(rk808->regmap, RK817_SYS_STS, &tmp);
879 	charger->plugged_in = (tmp & RK817_PLUG_IN_STS);
880 
881 	/*
882 	 * Turn on all ADC functions to measure battery, USB, and sys voltage,
883 	 * as well as batt temp. Note only tested implementation so far does
884 	 * not use a battery with a thermistor.
885 	 */
886 	regmap_write(rk808->regmap, RK817_GAS_GAUGE_ADC_CONFIG0, 0xfc);
887 
888 	/*
889 	 * Set relax mode voltage sampling interval and ADC offset calibration
890 	 * interval to 8 minutes to mirror BSP kernel. Set voltage and current
891 	 * modes to average to mirror BSP kernel.
892 	 */
893 	regmap_write(rk808->regmap, RK817_GAS_GAUGE_GG_CON, 0x04);
894 
895 	/* Calibrate voltage like the BSP does here. */
896 	rk817_bat_calib_vol(charger);
897 
898 	/* Write relax threshold, derived from sleep enter current. */
899 	tmp = CURRENT_TO_ADC(charger->sleep_enter_current_ua,
900 			     charger->res_div);
901 	put_unaligned_be16(tmp, bulk_reg);
902 	regmap_bulk_write(rk808->regmap, RK817_GAS_GAUGE_RELAX_THRE_H,
903 			  bulk_reg, 2);
904 
905 	/* Write sleep sample current, derived from sleep filter current. */
906 	tmp = CURRENT_TO_ADC(charger->sleep_filter_current_ua,
907 			     charger->res_div);
908 	put_unaligned_be16(tmp, bulk_reg);
909 	regmap_bulk_write(rk808->regmap, RK817_GAS_GAUGE_SLEEP_CON_SAMP_CUR_H,
910 			  bulk_reg, 2);
911 
912 	/* Restart battery relax voltage */
913 	regmap_write_bits(rk808->regmap, RK817_GAS_GAUGE_GG_STS,
914 			  RK817_RELAX_VOL_UPD, (0x0 << 2));
915 
916 	/*
917 	 * Set OCV Threshold Voltage to 127.5mV. This was hard coded like this
918 	 * in the BSP.
919 	 */
920 	regmap_write(rk808->regmap, RK817_GAS_GAUGE_OCV_THRE_VOL, 0xff);
921 
922 	/*
923 	 * Set maximum charging voltage to battery max voltage. Trying to be
924 	 * incredibly safe with these value, as setting them wrong could
925 	 * overcharge the battery, which would be very bad.
926 	 */
927 	max_chg_vol_mv = bat_info->constant_charge_voltage_max_uv / 1000;
928 	max_chg_cur_ma = bat_info->constant_charge_current_max_ua / 1000;
929 
930 	if (max_chg_vol_mv < 4100) {
931 		return dev_err_probe(charger->dev, -EINVAL,
932 		       "invalid max charger voltage, value %u unsupported\n",
933 			max_chg_vol_mv * 1000);
934 	}
935 	if (max_chg_vol_mv > 4450) {
936 		dev_info(charger->dev,
937 			 "Setting max charge voltage to 4450000uv\n");
938 		max_chg_vol_mv = 4450;
939 	}
940 
941 	if (max_chg_cur_ma < 500) {
942 		return dev_err_probe(charger->dev, -EINVAL,
943 		       "invalid max charger current, value %u unsupported\n",
944 		       max_chg_cur_ma * 1000);
945 	}
946 	if (max_chg_cur_ma > 3500)
947 		dev_info(charger->dev,
948 			 "Setting max charge current to 3500000ua\n");
949 
950 	/*
951 	 * Now that the values are sanity checked, if we subtract 4100 from the
952 	 * max voltage and divide by 50, we conviently get the exact value for
953 	 * the registers, which are 4.1v, 4.15v, 4.2v, 4.25v, 4.3v, 4.35v,
954 	 * 4.4v, and 4.45v; these correspond to values 0x00 through 0x07.
955 	 */
956 	max_chg_vol_reg = (max_chg_vol_mv - 4100) / 50;
957 
958 	max_chg_cur_reg = rk817_chg_cur_to_reg(max_chg_cur_ma);
959 
960 	if (max_chg_vol_reg < 0 || max_chg_vol_reg > 7) {
961 		return dev_err_probe(charger->dev, -EINVAL,
962 		       "invalid max charger voltage, value %u unsupported\n",
963 		       max_chg_vol_mv * 1000);
964 	}
965 	if (max_chg_cur_reg < 0 || max_chg_cur_reg > 7) {
966 		return dev_err_probe(charger->dev, -EINVAL,
967 		       "invalid max charger current, value %u unsupported\n",
968 		       max_chg_cur_ma * 1000);
969 	}
970 
971 	/*
972 	 * Write the values to the registers, and deliver an emergency warning
973 	 * in the event they are not written correctly.
974 	 */
975 	ret = regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_OUT,
976 				RK817_CHRG_VOL_SEL, (max_chg_vol_reg << 4));
977 	if (ret) {
978 		dev_emerg(charger->dev,
979 			  "Danger, unable to set max charger voltage: %u\n",
980 			  ret);
981 	}
982 
983 	ret = regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_OUT,
984 				RK817_CHRG_CUR_SEL, max_chg_cur_reg);
985 	if (ret) {
986 		dev_emerg(charger->dev,
987 			  "Danger, unable to set max charger current: %u\n",
988 			  ret);
989 	}
990 
991 	/* Set charge finishing mode to analog */
992 	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_TERM,
993 			  RK817_CHRG_TERM_ANA_DIG, (0x0 << 2));
994 
995 	/*
996 	 * Set charge finish current, warn if value not in range and keep
997 	 * default.
998 	 */
999 	chg_term_ma = bat_info->charge_term_current_ua / 1000;
1000 	if (chg_term_ma < 150 || chg_term_ma > 400) {
1001 		dev_warn(charger->dev,
1002 			 "Invalid charge termination %u, keeping default\n",
1003 			 chg_term_ma * 1000);
1004 		chg_term_ma = 200;
1005 	}
1006 
1007 	/*
1008 	 * Values of 150ma, 200ma, 300ma, and 400ma correspond to 00, 01, 10,
1009 	 * and 11.
1010 	 */
1011 	chg_term_i_reg = (chg_term_ma - 100) / 100;
1012 	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_TERM,
1013 			  RK817_CHRG_TERM_ANA_SEL, chg_term_i_reg);
1014 
1015 	ret = rk817_read_or_set_full_charge_on_boot(charger, bat_info);
1016 	if (ret < 0)
1017 		return ret;
1018 
1019 	/*
1020 	 * Set minimum USB input voltage to 4.5v and enable USB voltage input
1021 	 * limit.
1022 	 */
1023 	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_IN,
1024 			  RK817_USB_VLIM_SEL, (0x05 << 4));
1025 	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_IN, RK817_USB_VLIM_EN,
1026 			  (0x01 << 7));
1027 
1028 	/*
1029 	 * Set average USB input current limit to 1.5A and enable USB current
1030 	 * input limit.
1031 	 */
1032 	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_IN,
1033 			  RK817_USB_ILIM_SEL, 0x03);
1034 	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_IN, RK817_USB_ILIM_EN,
1035 			  (0x01 << 3));
1036 
1037 	return 0;
1038 }
1039 
rk817_charging_monitor(struct work_struct * work)1040 static void rk817_charging_monitor(struct work_struct *work)
1041 {
1042 	struct rk817_charger *charger;
1043 
1044 	charger = container_of(work, struct rk817_charger, work.work);
1045 
1046 	rk817_read_props(charger);
1047 
1048 	/* Run every 8 seconds like the BSP driver did. */
1049 	queue_delayed_work(system_wq, &charger->work, msecs_to_jiffies(8000));
1050 }
1051 
rk817_cleanup_node(void * data)1052 static void rk817_cleanup_node(void *data)
1053 {
1054 	struct device_node *node = data;
1055 
1056 	of_node_put(node);
1057 }
1058 
rk817_charger_probe(struct platform_device * pdev)1059 static int rk817_charger_probe(struct platform_device *pdev)
1060 {
1061 	struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
1062 	struct rk817_charger *charger;
1063 	struct device_node *node;
1064 	struct power_supply_battery_info *bat_info;
1065 	struct device *dev = &pdev->dev;
1066 	struct power_supply_config pscfg = {};
1067 	int plugin_irq, plugout_irq;
1068 	int of_value;
1069 	int ret;
1070 
1071 	node = of_get_child_by_name(dev->parent->of_node, "charger");
1072 	if (!node)
1073 		return -ENODEV;
1074 
1075 	ret = devm_add_action_or_reset(&pdev->dev, rk817_cleanup_node, node);
1076 	if (ret)
1077 		return ret;
1078 
1079 	charger = devm_kzalloc(&pdev->dev, sizeof(*charger), GFP_KERNEL);
1080 	if (!charger)
1081 		return -ENOMEM;
1082 
1083 	charger->rk808 = rk808;
1084 
1085 	charger->dev = &pdev->dev;
1086 	platform_set_drvdata(pdev, charger);
1087 
1088 	rk817_bat_calib_vol(charger);
1089 
1090 	pscfg.drv_data = charger;
1091 	pscfg.of_node = node;
1092 
1093 	/*
1094 	 * Get sample resistor value. Note only values of 10000 or 20000
1095 	 * microohms are allowed. Schematic for my test implementation (an
1096 	 * Odroid Go Advance) shows a 10 milliohm resistor for reference.
1097 	 */
1098 	ret = of_property_read_u32(node, "rockchip,resistor-sense-micro-ohms",
1099 				   &of_value);
1100 	if (ret < 0) {
1101 		return dev_err_probe(dev, ret,
1102 				     "Error reading sample resistor value\n");
1103 	}
1104 	/*
1105 	 * Store as a 1 or a 2, since all we really use the value for is as a
1106 	 * divisor in some calculations.
1107 	 */
1108 	charger->res_div = (of_value == 20000) ? 2 : 1;
1109 
1110 	/*
1111 	 * Get sleep enter current value. Not sure what this value is for
1112 	 * other than to help calibrate the relax threshold.
1113 	 */
1114 	ret = of_property_read_u32(node,
1115 				   "rockchip,sleep-enter-current-microamp",
1116 				   &of_value);
1117 	if (ret < 0) {
1118 		return dev_err_probe(dev, ret,
1119 				     "Error reading sleep enter cur value\n");
1120 	}
1121 	charger->sleep_enter_current_ua = of_value;
1122 
1123 	/* Get sleep filter current value */
1124 	ret = of_property_read_u32(node,
1125 				   "rockchip,sleep-filter-current-microamp",
1126 				   &of_value);
1127 	if (ret < 0) {
1128 		return dev_err_probe(dev, ret,
1129 				     "Error reading sleep filter cur value\n");
1130 	}
1131 
1132 	charger->sleep_filter_current_ua = of_value;
1133 
1134 	charger->bat_ps = devm_power_supply_register(&pdev->dev,
1135 						     &rk817_bat_desc, &pscfg);
1136 	if (IS_ERR(charger->bat_ps))
1137 		return dev_err_probe(dev, -EINVAL,
1138 				     "Battery failed to probe\n");
1139 
1140 	charger->chg_ps = devm_power_supply_register(&pdev->dev,
1141 						     &rk817_chg_desc, &pscfg);
1142 	if (IS_ERR(charger->chg_ps))
1143 		return dev_err_probe(dev, -EINVAL,
1144 				     "Charger failed to probe\n");
1145 
1146 	ret = power_supply_get_battery_info(charger->bat_ps,
1147 					    &bat_info);
1148 	if (ret) {
1149 		return dev_err_probe(dev, ret,
1150 				     "Unable to get battery info\n");
1151 	}
1152 
1153 	if ((bat_info->charge_full_design_uah <= 0) ||
1154 	    (bat_info->voltage_min_design_uv <= 0) ||
1155 	    (bat_info->voltage_max_design_uv <= 0) ||
1156 	    (bat_info->constant_charge_voltage_max_uv <= 0) ||
1157 	    (bat_info->constant_charge_current_max_ua <= 0) ||
1158 	    (bat_info->charge_term_current_ua <= 0)) {
1159 		return dev_err_probe(dev, -EINVAL,
1160 				     "Required bat info missing or invalid\n");
1161 	}
1162 
1163 	charger->bat_charge_full_design_uah = bat_info->charge_full_design_uah;
1164 	charger->bat_voltage_min_design_uv = bat_info->voltage_min_design_uv;
1165 	charger->bat_voltage_max_design_uv = bat_info->voltage_max_design_uv;
1166 
1167 	/*
1168 	 * Has to run after power_supply_get_battery_info as it depends on some
1169 	 * values discovered from that routine.
1170 	 */
1171 	ret = rk817_battery_init(charger, bat_info);
1172 	if (ret)
1173 		return ret;
1174 
1175 	power_supply_put_battery_info(charger->bat_ps, bat_info);
1176 
1177 	plugin_irq = platform_get_irq(pdev, 0);
1178 	if (plugin_irq < 0)
1179 		return plugin_irq;
1180 
1181 	plugout_irq = platform_get_irq(pdev, 1);
1182 	if (plugout_irq < 0)
1183 		return plugout_irq;
1184 
1185 	ret = devm_request_threaded_irq(charger->dev, plugin_irq, NULL,
1186 					rk817_plug_in_isr,
1187 					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
1188 					"rk817_plug_in", charger);
1189 	if (ret) {
1190 		return dev_err_probe(&pdev->dev, ret,
1191 				      "plug_in_irq request failed!\n");
1192 	}
1193 
1194 	ret = devm_request_threaded_irq(charger->dev, plugout_irq, NULL,
1195 					rk817_plug_out_isr,
1196 					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
1197 					"rk817_plug_out", charger);
1198 	if (ret) {
1199 		return dev_err_probe(&pdev->dev, ret,
1200 				     "plug_out_irq request failed!\n");
1201 	}
1202 
1203 	ret = devm_delayed_work_autocancel(&pdev->dev, &charger->work,
1204 					   rk817_charging_monitor);
1205 	if (ret)
1206 		return ret;
1207 
1208 	/* Force the first update immediately. */
1209 	mod_delayed_work(system_wq, &charger->work, 0);
1210 
1211 	return 0;
1212 }
1213 
rk817_suspend(struct device * dev)1214 static int __maybe_unused rk817_suspend(struct device *dev)
1215 {
1216 	struct rk817_charger *charger = dev_get_drvdata(dev);
1217 
1218 	cancel_delayed_work_sync(&charger->work);
1219 
1220 	return 0;
1221 }
1222 
rk817_resume(struct device * dev)1223 static int __maybe_unused rk817_resume(struct device *dev)
1224 {
1225 
1226 	struct rk817_charger *charger = dev_get_drvdata(dev);
1227 
1228 	/* force an immediate update */
1229 	mod_delayed_work(system_wq, &charger->work, 0);
1230 
1231 	return 0;
1232 }
1233 
1234 static SIMPLE_DEV_PM_OPS(rk817_charger_pm, rk817_suspend, rk817_resume);
1235 
1236 static struct platform_driver rk817_charger_driver = {
1237 	.probe    = rk817_charger_probe,
1238 	.driver   = {
1239 		.name  = "rk817-charger",
1240 		.pm		= &rk817_charger_pm,
1241 	},
1242 };
1243 module_platform_driver(rk817_charger_driver);
1244 
1245 MODULE_DESCRIPTION("Battery power supply driver for RK817 PMIC");
1246 MODULE_AUTHOR("Maya Matuszczyk <maccraft123mc@gmail.com>");
1247 MODULE_AUTHOR("Chris Morgan <macromorgan@hotmail.com>");
1248 MODULE_LICENSE("GPL");
1249 MODULE_ALIAS("platform:rk817-charger");
1250