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 <asm/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 
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 
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 
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 
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  */
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 
240 static int rk817_bat_calib_cap(struct rk817_charger *charger)
241 {
242 	struct rk808 *rk808 = charger->rk808;
243 	int tmp, charge_now, charge_now_adc, volt_avg;
244 	u8 bulk_reg[4];
245 
246 	/* Calibrate the soc and fcc on a fully charged battery */
247 
248 	if (charger->charge_status == CHARGE_FINISH && (!charger->soc_cal)) {
249 		/*
250 		 * soc should be 100000 and columb counter should show the full
251 		 * charge capacity. Note that if the device is unplugged for a
252 		 * period of several days the columb counter will have a large
253 		 * margin of error, so setting it back to the full charge on
254 		 * a completed charge cycle should correct this (my device was
255 		 * showing 33% battery after 3 days unplugged when it should
256 		 * have been closer to 95% based on voltage and charge
257 		 * current).
258 		 */
259 
260 		charger->soc = 100000;
261 		charge_now_adc = CHARGE_TO_ADC(charger->fcc_mah,
262 					       charger->res_div);
263 		put_unaligned_be32(charge_now_adc, bulk_reg);
264 		regmap_bulk_write(rk808->regmap, RK817_GAS_GAUGE_Q_INIT_H3,
265 				  bulk_reg, 4);
266 
267 		charger->soc_cal = 1;
268 		dev_dbg(charger->dev,
269 			"Fully charged. SOC is %d, full capacity is %d\n",
270 			charger->soc, charger->fcc_mah * 1000);
271 	}
272 
273 	/*
274 	 * The columb counter can drift up slightly, so we should correct for
275 	 * it. But don't correct it until we're at 100% soc.
276 	 */
277 	if (charger->charge_status == CHARGE_FINISH && charger->soc_cal) {
278 		regmap_bulk_read(rk808->regmap, RK817_GAS_GAUGE_Q_PRES_H3,
279 				 bulk_reg, 4);
280 		charge_now_adc = get_unaligned_be32(bulk_reg);
281 		if (charge_now_adc < 0)
282 			return charge_now_adc;
283 		charge_now = ADC_TO_CHARGE_UAH(charge_now_adc,
284 					       charger->res_div);
285 
286 		/*
287 		 * Re-init columb counter with updated values to correct drift.
288 		 */
289 		if (charge_now / 1000 > charger->fcc_mah) {
290 			dev_dbg(charger->dev,
291 				"Recalibrating columb counter to %d uah\n",
292 				charge_now);
293 			/*
294 			 * Order of operations matters here to ensure we keep
295 			 * enough precision until the last step to keep from
296 			 * making needless updates to columb counter.
297 			 */
298 			charge_now_adc = CHARGE_TO_ADC(charger->fcc_mah,
299 					 charger->res_div);
300 			put_unaligned_be32(charge_now_adc, bulk_reg);
301 			regmap_bulk_write(rk808->regmap,
302 					  RK817_GAS_GAUGE_Q_INIT_H3,
303 					  bulk_reg, 4);
304 		}
305 	}
306 
307 	/*
308 	 * Calibrate the fully charged capacity when we previously had a full
309 	 * battery (soc_cal = 1) and are now empty (at or below minimum design
310 	 * voltage). If our columb counter is still positive, subtract that
311 	 * from our fcc value to get a calibrated fcc, and if our columb
312 	 * counter is negative add that to our fcc (but not to exceed our
313 	 * design capacity).
314 	 */
315 	regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_BAT_VOL_H,
316 			 bulk_reg, 2);
317 	tmp = get_unaligned_be16(bulk_reg);
318 	volt_avg = (charger->voltage_k * tmp) + 1000 * charger->voltage_b;
319 	if (volt_avg <= charger->bat_voltage_min_design_uv &&
320 	    charger->soc_cal) {
321 		regmap_bulk_read(rk808->regmap, RK817_GAS_GAUGE_Q_PRES_H3,
322 				 bulk_reg, 4);
323 		charge_now_adc = get_unaligned_be32(bulk_reg);
324 		charge_now = ADC_TO_CHARGE_UAH(charge_now_adc,
325 					       charger->res_div);
326 		/*
327 		 * Note, if charge_now is negative this will add it (what we
328 		 * want) and if it's positive this will subtract (also what
329 		 * we want).
330 		 */
331 		charger->fcc_mah = charger->fcc_mah - (charge_now / 1000);
332 
333 		dev_dbg(charger->dev,
334 			"Recalibrating full charge capacity to %d uah\n",
335 			charger->fcc_mah * 1000);
336 	}
337 
338 	rk817_record_battery_nvram_values(charger);
339 
340 	return 0;
341 }
342 
343 static void rk817_read_props(struct rk817_charger *charger)
344 {
345 	int tmp, reg;
346 	u8 bulk_reg[4];
347 
348 	/*
349 	 * Recalibrate voltage and current readings if we need to BSP does both
350 	 * on CUR_CALIB_UPD, ignoring VOL_CALIB_UPD. Curiously enough, both
351 	 * documentation and the BSP show that you perform an update if bit 7
352 	 * is 1, but you clear the status by writing a 1 to bit 7.
353 	 */
354 	regmap_read(charger->rk808->regmap, RK817_GAS_GAUGE_ADC_CONFIG1, &reg);
355 	if (reg & RK817_VOL_CUR_CALIB_UPD) {
356 		rk817_bat_calib_cur(charger);
357 		rk817_bat_calib_vol(charger);
358 		regmap_write_bits(charger->rk808->regmap,
359 				  RK817_GAS_GAUGE_ADC_CONFIG1,
360 				  RK817_VOL_CUR_CALIB_UPD,
361 				  RK817_VOL_CUR_CALIB_UPD);
362 	}
363 
364 	/* Update reported charge. */
365 	regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_Q_PRES_H3,
366 			 bulk_reg, 4);
367 	tmp = get_unaligned_be32(bulk_reg);
368 	charger->charge_now_uah = ADC_TO_CHARGE_UAH(tmp, charger->res_div);
369 	if (charger->charge_now_uah < 0)
370 		charger->charge_now_uah = 0;
371 	if (charger->charge_now_uah > charger->fcc_mah * 1000)
372 		charger->charge_now_uah = charger->fcc_mah * 1000;
373 
374 	/* Update soc based on reported charge. */
375 	charger->soc = charger->charge_now_uah * 100 / charger->fcc_mah;
376 
377 	/* Update reported voltage. */
378 	regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_BAT_VOL_H,
379 			 bulk_reg, 2);
380 	tmp = get_unaligned_be16(bulk_reg);
381 	charger->volt_avg_uv = (charger->voltage_k * tmp) + 1000 *
382 				charger->voltage_b;
383 
384 	/*
385 	 * Update reported current. Note value from registers is a signed 16
386 	 * bit int.
387 	 */
388 	regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_BAT_CUR_H,
389 			 bulk_reg, 2);
390 	tmp = (short int)get_unaligned_be16(bulk_reg);
391 	charger->cur_avg_ua = ADC_TO_CURRENT(tmp, charger->res_div);
392 
393 	/*
394 	 * Update the max charge current. This value shouldn't change, but we
395 	 * can read it to report what the PMIC says it is instead of simply
396 	 * returning the default value.
397 	 */
398 	regmap_read(charger->rk808->regmap, RK817_PMIC_CHRG_OUT, &reg);
399 	charger->max_chg_cur_ua =
400 		rk817_chg_cur_from_reg(reg & RK817_CHRG_CUR_SEL);
401 
402 	/*
403 	 * Update max charge voltage. Like the max charge current this value
404 	 * shouldn't change, but we can report what the PMIC says.
405 	 */
406 	regmap_read(charger->rk808->regmap, RK817_PMIC_CHRG_OUT, &reg);
407 	charger->max_chg_volt_uv = ((((reg & RK817_CHRG_VOL_SEL) >> 4) *
408 				    50000) + 4100000);
409 
410 	/* Check if battery still present. */
411 	regmap_read(charger->rk808->regmap, RK817_PMIC_CHRG_STS, &reg);
412 	charger->battery_present = (reg & RK817_BAT_EXS);
413 
414 	/* Get which type of charge we are using (if any). */
415 	regmap_read(charger->rk808->regmap, RK817_PMIC_CHRG_STS, &reg);
416 	charger->charge_status = (reg >> 4) & 0x07;
417 
418 	/*
419 	 * Get charger input voltage. Note that on my example hardware (an
420 	 * Odroid Go Advance) the voltage of the power connector is measured
421 	 * on the register labelled USB in the datasheet; I don't know if this
422 	 * is how it is designed or just a quirk of the implementation. I
423 	 * believe this will also measure the voltage of the USB output when in
424 	 * OTG mode, if that is the case we may need to change this in the
425 	 * future to return 0 if the power supply status is offline (I can't
426 	 * test this with my current implementation. Also, when the voltage
427 	 * should be zero sometimes the ADC still shows a single bit (which
428 	 * would register as 20000uv). When this happens set it to 0.
429 	 */
430 	regmap_bulk_read(charger->rk808->regmap, RK817_GAS_GAUGE_USB_VOL_H,
431 			 bulk_reg, 2);
432 	reg = get_unaligned_be16(bulk_reg);
433 	if (reg > 1) {
434 		tmp = ((charger->voltage_k * reg / 1000 + charger->voltage_b) *
435 		       60 / 46);
436 		charger->charger_input_volt_avg_uv = tmp * 1000;
437 	} else {
438 		charger->charger_input_volt_avg_uv = 0;
439 	}
440 
441 	/* Calibrate battery capacity and soc. */
442 	rk817_bat_calib_cap(charger);
443 }
444 
445 static int rk817_bat_get_prop(struct power_supply *ps,
446 		enum power_supply_property prop,
447 		union power_supply_propval *val)
448 {
449 	struct rk817_charger *charger = power_supply_get_drvdata(ps);
450 
451 	switch (prop) {
452 	case POWER_SUPPLY_PROP_PRESENT:
453 		val->intval = charger->battery_present;
454 		break;
455 	case POWER_SUPPLY_PROP_STATUS:
456 		if (charger->cur_avg_ua < 0) {
457 			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
458 			break;
459 		}
460 		switch (charger->charge_status) {
461 		case CHRG_OFF:
462 			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
463 			break;
464 		/*
465 		 * Dead charge is documented, but not explained. I never
466 		 * observed it but assume it's a pre-charge for a dead
467 		 * battery.
468 		 */
469 		case DEAD_CHRG:
470 		case TRICKLE_CHRG:
471 		case CC_OR_CV_CHRG:
472 			val->intval = POWER_SUPPLY_STATUS_CHARGING;
473 			break;
474 		case CHARGE_FINISH:
475 			val->intval = POWER_SUPPLY_STATUS_FULL;
476 			break;
477 		default:
478 			val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
479 			return -EINVAL;
480 
481 		}
482 		break;
483 	case POWER_SUPPLY_PROP_CHARGE_TYPE:
484 		switch (charger->charge_status) {
485 		case CHRG_OFF:
486 		case CHARGE_FINISH:
487 			val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
488 			break;
489 		case TRICKLE_CHRG:
490 			val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
491 			break;
492 		case DEAD_CHRG:
493 		case CC_OR_CV_CHRG:
494 			val->intval = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
495 			break;
496 		default:
497 			val->intval = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
498 			break;
499 		}
500 		break;
501 	case POWER_SUPPLY_PROP_CHARGE_FULL:
502 		val->intval = charger->fcc_mah * 1000;
503 		break;
504 	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
505 		val->intval = charger->bat_charge_full_design_uah;
506 		break;
507 	case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
508 		val->intval = 0;
509 		break;
510 	case POWER_SUPPLY_PROP_CHARGE_NOW:
511 		val->intval = charger->charge_now_uah;
512 		break;
513 	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
514 		val->intval = charger->bat_voltage_min_design_uv;
515 		break;
516 	case POWER_SUPPLY_PROP_CAPACITY:
517 		/* Add 500 so that values like 99999 are 100% not 99%. */
518 		val->intval = (charger->soc + 500) / 1000;
519 		if (val->intval > 100)
520 			val->intval = 100;
521 		if (val->intval < 0)
522 			val->intval = 0;
523 		break;
524 	case POWER_SUPPLY_PROP_VOLTAGE_AVG:
525 		val->intval = charger->volt_avg_uv;
526 		break;
527 	case POWER_SUPPLY_PROP_CURRENT_AVG:
528 		val->intval = charger->cur_avg_ua;
529 		break;
530 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
531 		val->intval = charger->max_chg_cur_ua;
532 		break;
533 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
534 		val->intval = charger->max_chg_volt_uv;
535 		break;
536 	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
537 		val->intval = charger->bat_voltage_max_design_uv;
538 		break;
539 	default:
540 		return -EINVAL;
541 	}
542 	return 0;
543 }
544 
545 static int rk817_chg_get_prop(struct power_supply *ps,
546 			      enum power_supply_property prop,
547 			      union power_supply_propval *val)
548 {
549 	struct rk817_charger *charger = power_supply_get_drvdata(ps);
550 
551 	switch (prop) {
552 	case POWER_SUPPLY_PROP_ONLINE:
553 		val->intval = charger->plugged_in;
554 		break;
555 	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
556 		/* max voltage from datasheet at 5.5v (default 5.0v) */
557 		val->intval = 5500000;
558 		break;
559 	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
560 		/* min voltage from datasheet at 3.8v (default 5.0v) */
561 		val->intval = 3800000;
562 		break;
563 	case POWER_SUPPLY_PROP_VOLTAGE_AVG:
564 		val->intval = charger->charger_input_volt_avg_uv;
565 		break;
566 	/*
567 	 * While it's possible that other implementations could use different
568 	 * USB types, the current implementation for this PMIC (the Odroid Go
569 	 * Advance) only uses a dedicated charging port with no rx/tx lines.
570 	 */
571 	case POWER_SUPPLY_PROP_USB_TYPE:
572 		val->intval = POWER_SUPPLY_USB_TYPE_DCP;
573 		break;
574 	default:
575 		return -EINVAL;
576 	}
577 	return 0;
578 
579 }
580 
581 static irqreturn_t rk817_plug_in_isr(int irq, void *cg)
582 {
583 	struct rk817_charger *charger;
584 
585 	charger = (struct rk817_charger *)cg;
586 	charger->plugged_in = 1;
587 	power_supply_changed(charger->chg_ps);
588 	power_supply_changed(charger->bat_ps);
589 	/* try to recalibrate capacity if we hit full charge. */
590 	charger->soc_cal = 0;
591 
592 	rk817_read_props(charger);
593 
594 	dev_dbg(charger->dev, "Power Cord Inserted\n");
595 
596 	return IRQ_HANDLED;
597 }
598 
599 static irqreturn_t rk817_plug_out_isr(int irq, void *cg)
600 {
601 	struct rk817_charger *charger;
602 	struct rk808 *rk808;
603 
604 	charger = (struct rk817_charger *)cg;
605 	rk808 = charger->rk808;
606 	charger->plugged_in = 0;
607 	power_supply_changed(charger->bat_ps);
608 	power_supply_changed(charger->chg_ps);
609 
610 	/*
611 	 * For some reason the bits of RK817_PMIC_CHRG_IN reset whenever the
612 	 * power cord is unplugged. This was not documented in the BSP kernel
613 	 * or the datasheet and only discovered by trial and error. Set minimum
614 	 * USB input voltage to 4.5v and enable USB voltage input limit.
615 	 */
616 	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_IN,
617 			  RK817_USB_VLIM_SEL, (0x05 << 4));
618 	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_IN, RK817_USB_VLIM_EN,
619 			  (0x01 << 7));
620 
621 	/*
622 	 * Set average USB input current limit to 1.5A and enable USB current
623 	 * input limit.
624 	 */
625 	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_IN,
626 			  RK817_USB_ILIM_SEL, 0x03);
627 	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_IN, RK817_USB_ILIM_EN,
628 			  (0x01 << 3));
629 
630 	rk817_read_props(charger);
631 
632 	dev_dbg(charger->dev, "Power Cord Removed\n");
633 
634 	return IRQ_HANDLED;
635 }
636 
637 static enum power_supply_property rk817_bat_props[] = {
638 	POWER_SUPPLY_PROP_PRESENT,
639 	POWER_SUPPLY_PROP_STATUS,
640 	POWER_SUPPLY_PROP_CHARGE_TYPE,
641 	POWER_SUPPLY_PROP_CHARGE_FULL,
642 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
643 	POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN,
644 	POWER_SUPPLY_PROP_CHARGE_NOW,
645 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
646 	POWER_SUPPLY_PROP_VOLTAGE_AVG,
647 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
648 	POWER_SUPPLY_PROP_CURRENT_AVG,
649 	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
650 	POWER_SUPPLY_PROP_CAPACITY,
651 	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
652 };
653 
654 static enum power_supply_property rk817_chg_props[] = {
655 	POWER_SUPPLY_PROP_ONLINE,
656 	POWER_SUPPLY_PROP_USB_TYPE,
657 	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
658 	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
659 	POWER_SUPPLY_PROP_VOLTAGE_AVG,
660 };
661 
662 static enum power_supply_usb_type rk817_usb_type[] = {
663 	POWER_SUPPLY_USB_TYPE_DCP,
664 	POWER_SUPPLY_USB_TYPE_UNKNOWN,
665 };
666 
667 static const struct power_supply_desc rk817_bat_desc = {
668 	.name = "rk817-battery",
669 	.type = POWER_SUPPLY_TYPE_BATTERY,
670 	.properties = rk817_bat_props,
671 	.num_properties = ARRAY_SIZE(rk817_bat_props),
672 	.get_property = rk817_bat_get_prop,
673 };
674 
675 static const struct power_supply_desc rk817_chg_desc = {
676 	.name = "rk817-charger",
677 	.type = POWER_SUPPLY_TYPE_USB,
678 	.usb_types = rk817_usb_type,
679 	.num_usb_types = ARRAY_SIZE(rk817_usb_type),
680 	.properties = rk817_chg_props,
681 	.num_properties = ARRAY_SIZE(rk817_chg_props),
682 	.get_property = rk817_chg_get_prop,
683 };
684 
685 static int rk817_read_battery_nvram_values(struct rk817_charger *charger)
686 {
687 	u8 bulk_reg[3];
688 	int ret;
689 
690 	/* Read the nvram data for full charge capacity. */
691 	ret = regmap_bulk_read(charger->rk808->regmap,
692 			       RK817_GAS_GAUGE_DATA3, bulk_reg, 3);
693 	if (ret < 0)
694 		return ret;
695 	charger->fcc_mah = get_unaligned_le24(bulk_reg);
696 
697 	/*
698 	 * Sanity checking for values equal to zero or less than would be
699 	 * practical for this device (BSP Kernel assumes 500mAH or less) for
700 	 * practicality purposes. Also check if the value is too large and
701 	 * correct it.
702 	 */
703 	if ((charger->fcc_mah < 500) ||
704 	   ((charger->fcc_mah * 1000) > charger->bat_charge_full_design_uah)) {
705 		dev_info(charger->dev,
706 			 "Invalid NVRAM max charge, setting to %u uAH\n",
707 			 charger->bat_charge_full_design_uah);
708 		charger->fcc_mah = charger->bat_charge_full_design_uah / 1000;
709 	}
710 
711 	/*
712 	 * Read the nvram for state of charge. Sanity check for values greater
713 	 * than 100 (10000). If the value is off it should get corrected
714 	 * automatically when the voltage drops to the min (soc is 0) or when
715 	 * the battery is full (soc is 100).
716 	 */
717 	ret = regmap_bulk_read(charger->rk808->regmap,
718 			       RK817_GAS_GAUGE_BAT_R1, bulk_reg, 3);
719 	if (ret < 0)
720 		return ret;
721 	charger->soc = get_unaligned_le24(bulk_reg);
722 	if (charger->soc > 10000)
723 		charger->soc = 10000;
724 
725 	return 0;
726 }
727 
728 static int
729 rk817_read_or_set_full_charge_on_boot(struct rk817_charger *charger,
730 				struct power_supply_battery_info *bat_info)
731 {
732 	struct rk808 *rk808 = charger->rk808;
733 	u8 bulk_reg[4];
734 	u32 boot_voltage, boot_charge_mah, tmp;
735 	int ret, reg, off_time;
736 	bool first_boot;
737 
738 	/*
739 	 * Check if the battery is uninitalized. If it is, the columb counter
740 	 * needs to be set up.
741 	 */
742 	ret = regmap_read(rk808->regmap, RK817_GAS_GAUGE_GG_STS, &reg);
743 	if (ret < 0)
744 		return ret;
745 	first_boot = reg & RK817_BAT_CON;
746 	/*
747 	 * If the battery is uninitialized, use the poweron voltage and an ocv
748 	 * lookup to guess our charge. The number won't be very accurate until
749 	 * we hit either our minimum voltage (0%) or full charge (100%).
750 	 */
751 	if (first_boot) {
752 		regmap_bulk_read(rk808->regmap, RK817_GAS_GAUGE_PWRON_VOL_H,
753 				 bulk_reg, 2);
754 		tmp = get_unaligned_be16(bulk_reg);
755 		boot_voltage = (charger->voltage_k * tmp) +
756 				1000 * charger->voltage_b;
757 		/*
758 		 * Since only implementation has no working thermistor, assume
759 		 * 20C for OCV lookup. If lookup fails, report error with OCV
760 		 * table.
761 		 */
762 		charger->soc = power_supply_batinfo_ocv2cap(bat_info,
763 							    boot_voltage,
764 							    20) * 1000;
765 		if (charger->soc < 0)
766 			charger->soc = 0;
767 
768 		/* Guess that full charge capacity is the design capacity */
769 		charger->fcc_mah = charger->bat_charge_full_design_uah / 1000;
770 		/*
771 		 * Set battery as "set up". BSP driver uses this value even
772 		 * though datasheet claims it's a read-only value.
773 		 */
774 		regmap_write_bits(rk808->regmap, RK817_GAS_GAUGE_GG_STS,
775 				  RK817_BAT_CON, 0);
776 		/* Save nvram values */
777 		ret = rk817_record_battery_nvram_values(charger);
778 		if (ret < 0)
779 			return ret;
780 	} else {
781 		ret = rk817_read_battery_nvram_values(charger);
782 		if (ret < 0)
783 			return ret;
784 
785 		regmap_bulk_read(rk808->regmap, RK817_GAS_GAUGE_Q_PRES_H3,
786 				 bulk_reg, 4);
787 		tmp = get_unaligned_be32(bulk_reg);
788 		boot_charge_mah = ADC_TO_CHARGE_UAH(tmp,
789 						    charger->res_div) / 1000;
790 		/*
791 		 * Check if the columb counter has been off for more than 300
792 		 * minutes as it tends to drift downward. If so, re-init soc
793 		 * with the boot voltage instead. Note the unit values for the
794 		 * OFF_CNT register appear to be in decaminutes and stops
795 		 * counting at 2550 (0xFF) minutes. BSP kernel used OCV, but
796 		 * for me occasionally that would show invalid values. Boot
797 		 * voltage is only accurate for me on first poweron (not
798 		 * reboots), but we shouldn't ever encounter an OFF_CNT more
799 		 * than 0 on a reboot anyway.
800 		 */
801 		regmap_read(rk808->regmap, RK817_GAS_GAUGE_OFF_CNT, &off_time);
802 		if (off_time >= 30) {
803 			regmap_bulk_read(rk808->regmap,
804 					 RK817_GAS_GAUGE_PWRON_VOL_H,
805 					 bulk_reg, 2);
806 			tmp = get_unaligned_be16(bulk_reg);
807 			boot_voltage = (charger->voltage_k * tmp) +
808 					1000 * charger->voltage_b;
809 			charger->soc =
810 				power_supply_batinfo_ocv2cap(bat_info,
811 							     boot_voltage,
812 							     20) * 1000;
813 		} else {
814 			charger->soc = (boot_charge_mah * 1000 * 100 /
815 					charger->fcc_mah);
816 		}
817 	}
818 
819 	regmap_bulk_read(rk808->regmap, RK817_GAS_GAUGE_PWRON_VOL_H,
820 			 bulk_reg, 2);
821 	tmp = get_unaligned_be16(bulk_reg);
822 	boot_voltage = (charger->voltage_k * tmp) + 1000 * charger->voltage_b;
823 	regmap_bulk_read(rk808->regmap, RK817_GAS_GAUGE_Q_PRES_H3,
824 			 bulk_reg, 4);
825 	tmp = get_unaligned_be32(bulk_reg);
826 	boot_charge_mah = ADC_TO_CHARGE_UAH(tmp, charger->res_div) / 1000;
827 	regmap_bulk_read(rk808->regmap, RK817_GAS_GAUGE_OCV_VOL_H,
828 			 bulk_reg, 2);
829 	tmp = get_unaligned_be16(bulk_reg);
830 	boot_voltage = (charger->voltage_k * tmp) + 1000 * charger->voltage_b;
831 
832 	/*
833 	 * Now we have our full charge capacity and soc, init the columb
834 	 * counter.
835 	 */
836 	boot_charge_mah = charger->soc * charger->fcc_mah / 100 / 1000;
837 	if (boot_charge_mah > charger->fcc_mah)
838 		boot_charge_mah = charger->fcc_mah;
839 	tmp = CHARGE_TO_ADC(boot_charge_mah, charger->res_div);
840 	put_unaligned_be32(tmp, bulk_reg);
841 	ret = regmap_bulk_write(rk808->regmap, RK817_GAS_GAUGE_Q_INIT_H3,
842 			  bulk_reg, 4);
843 	if (ret < 0)
844 		return ret;
845 
846 	/* Set QMAX value to max design capacity. */
847 	tmp = CHARGE_TO_ADC((charger->bat_charge_full_design_uah / 1000),
848 			    charger->res_div);
849 	put_unaligned_be32(tmp, bulk_reg);
850 	ret = regmap_bulk_write(rk808->regmap, RK817_GAS_GAUGE_Q_MAX_H3,
851 				bulk_reg, 4);
852 	if (ret < 0)
853 		return ret;
854 
855 	return 0;
856 }
857 
858 static int rk817_battery_init(struct rk817_charger *charger,
859 			      struct power_supply_battery_info *bat_info)
860 {
861 	struct rk808 *rk808 = charger->rk808;
862 	u32 tmp, max_chg_vol_mv, max_chg_cur_ma;
863 	u8 max_chg_vol_reg, chg_term_i_reg;
864 	int ret, chg_term_ma, max_chg_cur_reg;
865 	u8 bulk_reg[2];
866 
867 	/* Get initial plug state */
868 	regmap_read(rk808->regmap, RK817_SYS_STS, &tmp);
869 	charger->plugged_in = (tmp & RK817_PLUG_IN_STS);
870 
871 	/*
872 	 * Turn on all ADC functions to measure battery, USB, and sys voltage,
873 	 * as well as batt temp. Note only tested implementation so far does
874 	 * not use a battery with a thermistor.
875 	 */
876 	regmap_write(rk808->regmap, RK817_GAS_GAUGE_ADC_CONFIG0, 0xfc);
877 
878 	/*
879 	 * Set relax mode voltage sampling interval and ADC offset calibration
880 	 * interval to 8 minutes to mirror BSP kernel. Set voltage and current
881 	 * modes to average to mirror BSP kernel.
882 	 */
883 	regmap_write(rk808->regmap, RK817_GAS_GAUGE_GG_CON, 0x04);
884 
885 	/* Calibrate voltage like the BSP does here. */
886 	rk817_bat_calib_vol(charger);
887 
888 	/* Write relax threshold, derived from sleep enter current. */
889 	tmp = CURRENT_TO_ADC(charger->sleep_enter_current_ua,
890 			     charger->res_div);
891 	put_unaligned_be16(tmp, bulk_reg);
892 	regmap_bulk_write(rk808->regmap, RK817_GAS_GAUGE_RELAX_THRE_H,
893 			  bulk_reg, 2);
894 
895 	/* Write sleep sample current, derived from sleep filter current. */
896 	tmp = CURRENT_TO_ADC(charger->sleep_filter_current_ua,
897 			     charger->res_div);
898 	put_unaligned_be16(tmp, bulk_reg);
899 	regmap_bulk_write(rk808->regmap, RK817_GAS_GAUGE_SLEEP_CON_SAMP_CUR_H,
900 			  bulk_reg, 2);
901 
902 	/* Restart battery relax voltage */
903 	regmap_write_bits(rk808->regmap, RK817_GAS_GAUGE_GG_STS,
904 			  RK817_RELAX_VOL_UPD, (0x0 << 2));
905 
906 	/*
907 	 * Set OCV Threshold Voltage to 127.5mV. This was hard coded like this
908 	 * in the BSP.
909 	 */
910 	regmap_write(rk808->regmap, RK817_GAS_GAUGE_OCV_THRE_VOL, 0xff);
911 
912 	/*
913 	 * Set maximum charging voltage to battery max voltage. Trying to be
914 	 * incredibly safe with these value, as setting them wrong could
915 	 * overcharge the battery, which would be very bad.
916 	 */
917 	max_chg_vol_mv = bat_info->constant_charge_voltage_max_uv / 1000;
918 	max_chg_cur_ma = bat_info->constant_charge_current_max_ua / 1000;
919 
920 	if (max_chg_vol_mv < 4100) {
921 		return dev_err_probe(charger->dev, -EINVAL,
922 		       "invalid max charger voltage, value %u unsupported\n",
923 			max_chg_vol_mv * 1000);
924 	}
925 	if (max_chg_vol_mv > 4450) {
926 		dev_info(charger->dev,
927 			 "Setting max charge voltage to 4450000uv\n");
928 		max_chg_vol_mv = 4450;
929 	}
930 
931 	if (max_chg_cur_ma < 500) {
932 		return dev_err_probe(charger->dev, -EINVAL,
933 		       "invalid max charger current, value %u unsupported\n",
934 		       max_chg_cur_ma * 1000);
935 	}
936 	if (max_chg_cur_ma > 3500)
937 		dev_info(charger->dev,
938 			 "Setting max charge current to 3500000ua\n");
939 
940 	/*
941 	 * Now that the values are sanity checked, if we subtract 4100 from the
942 	 * max voltage and divide by 50, we conviently get the exact value for
943 	 * the registers, which are 4.1v, 4.15v, 4.2v, 4.25v, 4.3v, 4.35v,
944 	 * 4.4v, and 4.45v; these correspond to values 0x00 through 0x07.
945 	 */
946 	max_chg_vol_reg = (max_chg_vol_mv - 4100) / 50;
947 
948 	max_chg_cur_reg = rk817_chg_cur_to_reg(max_chg_cur_ma);
949 
950 	if (max_chg_vol_reg < 0 || max_chg_vol_reg > 7) {
951 		return dev_err_probe(charger->dev, -EINVAL,
952 		       "invalid max charger voltage, value %u unsupported\n",
953 		       max_chg_vol_mv * 1000);
954 	}
955 	if (max_chg_cur_reg < 0 || max_chg_cur_reg > 7) {
956 		return dev_err_probe(charger->dev, -EINVAL,
957 		       "invalid max charger current, value %u unsupported\n",
958 		       max_chg_cur_ma * 1000);
959 	}
960 
961 	/*
962 	 * Write the values to the registers, and deliver an emergency warning
963 	 * in the event they are not written correctly.
964 	 */
965 	ret = regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_OUT,
966 				RK817_CHRG_VOL_SEL, (max_chg_vol_reg << 4));
967 	if (ret) {
968 		dev_emerg(charger->dev,
969 			  "Danger, unable to set max charger voltage: %u\n",
970 			  ret);
971 	}
972 
973 	ret = regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_OUT,
974 				RK817_CHRG_CUR_SEL, max_chg_cur_reg);
975 	if (ret) {
976 		dev_emerg(charger->dev,
977 			  "Danger, unable to set max charger current: %u\n",
978 			  ret);
979 	}
980 
981 	/* Set charge finishing mode to analog */
982 	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_TERM,
983 			  RK817_CHRG_TERM_ANA_DIG, (0x0 << 2));
984 
985 	/*
986 	 * Set charge finish current, warn if value not in range and keep
987 	 * default.
988 	 */
989 	chg_term_ma = bat_info->charge_term_current_ua / 1000;
990 	if (chg_term_ma < 150 || chg_term_ma > 400) {
991 		dev_warn(charger->dev,
992 			 "Invalid charge termination %u, keeping default\n",
993 			 chg_term_ma * 1000);
994 		chg_term_ma = 200;
995 	}
996 
997 	/*
998 	 * Values of 150ma, 200ma, 300ma, and 400ma correspond to 00, 01, 10,
999 	 * and 11.
1000 	 */
1001 	chg_term_i_reg = (chg_term_ma - 100) / 100;
1002 	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_TERM,
1003 			  RK817_CHRG_TERM_ANA_SEL, chg_term_i_reg);
1004 
1005 	ret = rk817_read_or_set_full_charge_on_boot(charger, bat_info);
1006 	if (ret < 0)
1007 		return ret;
1008 
1009 	/*
1010 	 * Set minimum USB input voltage to 4.5v and enable USB voltage input
1011 	 * limit.
1012 	 */
1013 	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_IN,
1014 			  RK817_USB_VLIM_SEL, (0x05 << 4));
1015 	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_IN, RK817_USB_VLIM_EN,
1016 			  (0x01 << 7));
1017 
1018 	/*
1019 	 * Set average USB input current limit to 1.5A and enable USB current
1020 	 * input limit.
1021 	 */
1022 	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_IN,
1023 			  RK817_USB_ILIM_SEL, 0x03);
1024 	regmap_write_bits(rk808->regmap, RK817_PMIC_CHRG_IN, RK817_USB_ILIM_EN,
1025 			  (0x01 << 3));
1026 
1027 	return 0;
1028 }
1029 
1030 static void rk817_charging_monitor(struct work_struct *work)
1031 {
1032 	struct rk817_charger *charger;
1033 
1034 	charger = container_of(work, struct rk817_charger, work.work);
1035 
1036 	rk817_read_props(charger);
1037 
1038 	/* Run every 8 seconds like the BSP driver did. */
1039 	queue_delayed_work(system_wq, &charger->work, msecs_to_jiffies(8000));
1040 }
1041 
1042 static int rk817_charger_probe(struct platform_device *pdev)
1043 {
1044 	struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent);
1045 	struct rk817_charger *charger;
1046 	struct device_node *node;
1047 	struct power_supply_battery_info *bat_info;
1048 	struct device *dev = &pdev->dev;
1049 	struct power_supply_config pscfg = {};
1050 	int plugin_irq, plugout_irq;
1051 	int of_value;
1052 	int ret;
1053 
1054 	node = of_get_child_by_name(dev->parent->of_node, "charger");
1055 	if (!node)
1056 		return -ENODEV;
1057 
1058 	charger = devm_kzalloc(&pdev->dev, sizeof(*charger), GFP_KERNEL);
1059 	if (!charger) {
1060 		of_node_put(node);
1061 		return -ENOMEM;
1062 	}
1063 
1064 	charger->rk808 = rk808;
1065 
1066 	charger->dev = &pdev->dev;
1067 	platform_set_drvdata(pdev, charger);
1068 
1069 	rk817_bat_calib_vol(charger);
1070 
1071 	pscfg.drv_data = charger;
1072 	pscfg.of_node = node;
1073 
1074 	/*
1075 	 * Get sample resistor value. Note only values of 10000 or 20000
1076 	 * microohms are allowed. Schematic for my test implementation (an
1077 	 * Odroid Go Advance) shows a 10 milliohm resistor for reference.
1078 	 */
1079 	ret = of_property_read_u32(node, "rockchip,resistor-sense-micro-ohms",
1080 				   &of_value);
1081 	if (ret < 0) {
1082 		return dev_err_probe(dev, ret,
1083 				     "Error reading sample resistor value\n");
1084 	}
1085 	/*
1086 	 * Store as a 1 or a 2, since all we really use the value for is as a
1087 	 * divisor in some calculations.
1088 	 */
1089 	charger->res_div = (of_value == 20000) ? 2 : 1;
1090 
1091 	/*
1092 	 * Get sleep enter current value. Not sure what this value is for
1093 	 * other than to help calibrate the relax threshold.
1094 	 */
1095 	ret = of_property_read_u32(node,
1096 				   "rockchip,sleep-enter-current-microamp",
1097 				   &of_value);
1098 	if (ret < 0) {
1099 		return dev_err_probe(dev, ret,
1100 				     "Error reading sleep enter cur value\n");
1101 	}
1102 	charger->sleep_enter_current_ua = of_value;
1103 
1104 	/* Get sleep filter current value */
1105 	ret = of_property_read_u32(node,
1106 				   "rockchip,sleep-filter-current-microamp",
1107 				   &of_value);
1108 	if (ret < 0) {
1109 		return dev_err_probe(dev, ret,
1110 				     "Error reading sleep filter cur value\n");
1111 	}
1112 
1113 	charger->sleep_filter_current_ua = of_value;
1114 
1115 	charger->bat_ps = devm_power_supply_register(&pdev->dev,
1116 						     &rk817_bat_desc, &pscfg);
1117 	if (IS_ERR(charger->bat_ps))
1118 		return dev_err_probe(dev, -EINVAL,
1119 				     "Battery failed to probe\n");
1120 
1121 	charger->chg_ps = devm_power_supply_register(&pdev->dev,
1122 						     &rk817_chg_desc, &pscfg);
1123 	if (IS_ERR(charger->chg_ps))
1124 		return dev_err_probe(dev, -EINVAL,
1125 				     "Charger failed to probe\n");
1126 
1127 	ret = power_supply_get_battery_info(charger->bat_ps,
1128 					    &bat_info);
1129 	if (ret) {
1130 		return dev_err_probe(dev, ret,
1131 				     "Unable to get battery info: %d\n", ret);
1132 	}
1133 
1134 	if ((bat_info->charge_full_design_uah <= 0) ||
1135 	    (bat_info->voltage_min_design_uv <= 0) ||
1136 	    (bat_info->voltage_max_design_uv <= 0) ||
1137 	    (bat_info->constant_charge_voltage_max_uv <= 0) ||
1138 	    (bat_info->constant_charge_current_max_ua <= 0) ||
1139 	    (bat_info->charge_term_current_ua <= 0)) {
1140 		return dev_err_probe(dev, -EINVAL,
1141 				     "Required bat info missing or invalid\n");
1142 	}
1143 
1144 	charger->bat_charge_full_design_uah = bat_info->charge_full_design_uah;
1145 	charger->bat_voltage_min_design_uv = bat_info->voltage_min_design_uv;
1146 	charger->bat_voltage_max_design_uv = bat_info->voltage_max_design_uv;
1147 
1148 	/*
1149 	 * Has to run after power_supply_get_battery_info as it depends on some
1150 	 * values discovered from that routine.
1151 	 */
1152 	ret = rk817_battery_init(charger, bat_info);
1153 	if (ret)
1154 		return ret;
1155 
1156 	power_supply_put_battery_info(charger->bat_ps, bat_info);
1157 
1158 	plugin_irq = platform_get_irq(pdev, 0);
1159 	if (plugin_irq < 0)
1160 		return plugin_irq;
1161 
1162 	plugout_irq = platform_get_irq(pdev, 1);
1163 	if (plugout_irq < 0)
1164 		return plugout_irq;
1165 
1166 	ret = devm_request_threaded_irq(charger->dev, plugin_irq, NULL,
1167 					rk817_plug_in_isr,
1168 					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
1169 					"rk817_plug_in", charger);
1170 	if (ret) {
1171 		return dev_err_probe(&pdev->dev, ret,
1172 				      "plug_in_irq request failed!\n");
1173 	}
1174 
1175 	ret = devm_request_threaded_irq(charger->dev, plugout_irq, NULL,
1176 					rk817_plug_out_isr,
1177 					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
1178 					"rk817_plug_out", charger);
1179 	if (ret) {
1180 		return dev_err_probe(&pdev->dev, ret,
1181 				     "plug_out_irq request failed!\n");
1182 	}
1183 
1184 	ret = devm_delayed_work_autocancel(&pdev->dev, &charger->work,
1185 					   rk817_charging_monitor);
1186 	if (ret)
1187 		return ret;
1188 
1189 	/* Force the first update immediately. */
1190 	mod_delayed_work(system_wq, &charger->work, 0);
1191 
1192 	return 0;
1193 }
1194 
1195 
1196 static struct platform_driver rk817_charger_driver = {
1197 	.probe    = rk817_charger_probe,
1198 	.driver   = {
1199 		.name  = "rk817-charger",
1200 	},
1201 };
1202 module_platform_driver(rk817_charger_driver);
1203 
1204 MODULE_DESCRIPTION("Battery power supply driver for RK817 PMIC");
1205 MODULE_AUTHOR("Maya Matuszczyk <maccraft123mc@gmail.com>");
1206 MODULE_AUTHOR("Chris Morgan <macromorgan@hotmail.com>");
1207 MODULE_LICENSE("GPL");
1208