xref: /OK3568_Linux_fs/kernel/drivers/power/supply/cw2015_battery.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Fuel gauge driver for CellWise 2013 / 2015
4  *
5  * Copyright (C) 2012, RockChip
6  * Copyright (C) 2020, Tobias Schramm
7  *
8  * Authors: xuhuicong <xhc@rock-chips.com>
9  * Authors: Tobias Schramm <t.schramm@manjaro.org>
10  */
11 
12 #include <linux/bits.h>
13 #include <linux/delay.h>
14 #include <linux/i2c.h>
15 #include <linux/gfp.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/power_supply.h>
20 #include <linux/property.h>
21 #include <linux/regmap.h>
22 #include <linux/time.h>
23 #include <linux/workqueue.h>
24 
25 #define CW2015_SIZE_BATINFO		64
26 
27 #define CW2015_RESET_TRIES		5
28 
29 #define CW2015_REG_VERSION		0x00
30 #define CW2015_REG_VCELL		0x02
31 #define CW2015_REG_SOC			0x04
32 #define CW2015_REG_RRT_ALERT		0x06
33 #define CW2015_REG_CONFIG		0x08
34 #define CW2015_REG_MODE			0x0A
35 #define CW2015_REG_BATINFO		0x10
36 
37 #define CW2015_MODE_SLEEP_MASK		GENMASK(7, 6)
38 #define CW2015_MODE_SLEEP		(0x03 << 6)
39 #define CW2015_MODE_NORMAL		(0x00 << 6)
40 #define CW2015_MODE_QUICK_START		(0x03 << 4)
41 #define CW2015_MODE_RESTART		(0x0f << 0)
42 
43 #define CW2015_CONFIG_UPDATE_FLG	(0x01 << 1)
44 #define CW2015_ATHD(x)			((x) << 3)
45 #define CW2015_MASK_ATHD		GENMASK(7, 3)
46 #define CW2015_MASK_SOC			GENMASK(12, 0)
47 
48 /* reset gauge of no valid state of charge could be polled for 40s */
49 #define CW2015_BAT_SOC_ERROR_MS		(40 * MSEC_PER_SEC)
50 /* reset gauge if state of charge stuck for half an hour during charging */
51 #define CW2015_BAT_CHARGING_STUCK_MS	(1800 * MSEC_PER_SEC)
52 
53 /* poll interval from CellWise GPL Android driver example */
54 #define CW2015_DEFAULT_POLL_INTERVAL_MS		8000
55 
56 #define CW2015_AVERAGING_SAMPLES		3
57 
58 struct cw_battery {
59 	struct device *dev;
60 	struct workqueue_struct *battery_workqueue;
61 	struct delayed_work battery_delay_work;
62 	struct regmap *regmap;
63 	struct power_supply *rk_bat;
64 	struct power_supply_battery_info battery;
65 	u8 *bat_profile;
66 
67 	bool charger_attached;
68 	bool battery_changed;
69 
70 	int soc;
71 	int voltage_mv;
72 	int status;
73 	int time_to_empty;
74 	int charge_count;
75 
76 	u32 poll_interval_ms;
77 	u8 alert_level;
78 
79 	bool dual_cell;
80 
81 	unsigned int read_errors;
82 	unsigned int charge_stuck_cnt;
83 };
84 
cw_read_word(struct cw_battery * cw_bat,u8 reg,u16 * val)85 static int cw_read_word(struct cw_battery *cw_bat, u8 reg, u16 *val)
86 {
87 	__be16 value;
88 	int ret;
89 
90 	ret = regmap_bulk_read(cw_bat->regmap, reg, &value, sizeof(value));
91 	if (ret)
92 		return ret;
93 
94 	*val = be16_to_cpu(value);
95 	return 0;
96 }
97 
cw_update_profile(struct cw_battery * cw_bat)98 static int cw_update_profile(struct cw_battery *cw_bat)
99 {
100 	int ret;
101 	unsigned int reg_val;
102 	u8 reset_val;
103 
104 	/* make sure gauge is not in sleep mode */
105 	ret = regmap_read(cw_bat->regmap, CW2015_REG_MODE, &reg_val);
106 	if (ret)
107 		return ret;
108 
109 	reset_val = reg_val;
110 	if ((reg_val & CW2015_MODE_SLEEP_MASK) == CW2015_MODE_SLEEP) {
111 		dev_err(cw_bat->dev,
112 			"Gauge is in sleep mode, can't update battery info\n");
113 		return -EINVAL;
114 	}
115 
116 	/* write new battery info */
117 	ret = regmap_raw_write(cw_bat->regmap, CW2015_REG_BATINFO,
118 			       cw_bat->bat_profile,
119 			       CW2015_SIZE_BATINFO);
120 	if (ret)
121 		return ret;
122 
123 	/* set config update flag  */
124 	reg_val |= CW2015_CONFIG_UPDATE_FLG;
125 	reg_val &= ~CW2015_MASK_ATHD;
126 	reg_val |= CW2015_ATHD(cw_bat->alert_level);
127 	ret = regmap_write(cw_bat->regmap, CW2015_REG_CONFIG, reg_val);
128 	if (ret)
129 		return ret;
130 
131 	/* reset gauge to apply new battery profile */
132 	reset_val &= ~CW2015_MODE_RESTART;
133 	reg_val = reset_val | CW2015_MODE_RESTART;
134 	ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reg_val);
135 	if (ret)
136 		return ret;
137 
138 	/* wait for gauge to reset */
139 	msleep(20);
140 
141 	/* clear reset flag */
142 	ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reset_val);
143 	if (ret)
144 		return ret;
145 
146 	/* wait for gauge to become ready */
147 	ret = regmap_read_poll_timeout(cw_bat->regmap, CW2015_REG_SOC,
148 				       reg_val, reg_val <= 100,
149 				       10 * USEC_PER_MSEC, 10 * USEC_PER_SEC);
150 	if (ret)
151 		dev_err(cw_bat->dev,
152 			"Gauge did not become ready after profile upload\n");
153 	else
154 		dev_dbg(cw_bat->dev, "Battery profile updated\n");
155 
156 	return ret;
157 }
158 
cw_init(struct cw_battery * cw_bat)159 static int cw_init(struct cw_battery *cw_bat)
160 {
161 	int ret;
162 	unsigned int reg_val = CW2015_MODE_SLEEP;
163 
164 	if ((reg_val & CW2015_MODE_SLEEP_MASK) == CW2015_MODE_SLEEP) {
165 		reg_val = CW2015_MODE_NORMAL;
166 		ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reg_val);
167 		if (ret)
168 			return ret;
169 	}
170 
171 	ret = regmap_read(cw_bat->regmap, CW2015_REG_CONFIG, &reg_val);
172 	if (ret)
173 		return ret;
174 
175 	if ((reg_val & CW2015_MASK_ATHD) != CW2015_ATHD(cw_bat->alert_level)) {
176 		dev_dbg(cw_bat->dev, "Setting new alert level\n");
177 		reg_val &= ~CW2015_MASK_ATHD;
178 		reg_val |= ~CW2015_ATHD(cw_bat->alert_level);
179 		ret = regmap_write(cw_bat->regmap, CW2015_REG_CONFIG, reg_val);
180 		if (ret)
181 			return ret;
182 	}
183 
184 	ret = regmap_read(cw_bat->regmap, CW2015_REG_CONFIG, &reg_val);
185 	if (ret)
186 		return ret;
187 
188 	if (!(reg_val & CW2015_CONFIG_UPDATE_FLG)) {
189 		dev_dbg(cw_bat->dev,
190 			"Battery profile not present, uploading battery profile\n");
191 		if (cw_bat->bat_profile) {
192 			ret = cw_update_profile(cw_bat);
193 			if (ret) {
194 				dev_err(cw_bat->dev,
195 					"Failed to upload battery profile\n");
196 				return ret;
197 			}
198 		} else {
199 			dev_warn(cw_bat->dev,
200 				 "No profile specified, continuing without profile\n");
201 		}
202 	} else if (cw_bat->bat_profile) {
203 		u8 bat_info[CW2015_SIZE_BATINFO];
204 
205 		ret = regmap_raw_read(cw_bat->regmap, CW2015_REG_BATINFO,
206 				      bat_info, CW2015_SIZE_BATINFO);
207 		if (ret) {
208 			dev_err(cw_bat->dev,
209 				"Failed to read stored battery profile\n");
210 			return ret;
211 		}
212 
213 		if (memcmp(bat_info, cw_bat->bat_profile, CW2015_SIZE_BATINFO)) {
214 			dev_warn(cw_bat->dev, "Replacing stored battery profile\n");
215 			ret = cw_update_profile(cw_bat);
216 			if (ret)
217 				return ret;
218 		}
219 	} else {
220 		dev_warn(cw_bat->dev,
221 			 "Can't check current battery profile, no profile provided\n");
222 	}
223 
224 	dev_dbg(cw_bat->dev, "Battery profile configured\n");
225 	return 0;
226 }
227 
cw_power_on_reset(struct cw_battery * cw_bat)228 static int cw_power_on_reset(struct cw_battery *cw_bat)
229 {
230 	int ret;
231 	unsigned char reset_val;
232 
233 	reset_val = CW2015_MODE_SLEEP;
234 	ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reset_val);
235 	if (ret)
236 		return ret;
237 
238 	/* wait for gauge to enter sleep */
239 	msleep(20);
240 
241 	reset_val = CW2015_MODE_NORMAL;
242 	ret = regmap_write(cw_bat->regmap, CW2015_REG_MODE, reset_val);
243 	if (ret)
244 		return ret;
245 
246 	ret = cw_init(cw_bat);
247 	if (ret)
248 		return ret;
249 	return 0;
250 }
251 
252 #define HYSTERESIS(current, previous, up, down) \
253 	(((current) < (previous) + (up)) && ((current) > (previous) - (down)))
254 
cw_get_soc(struct cw_battery * cw_bat)255 static int cw_get_soc(struct cw_battery *cw_bat)
256 {
257 	unsigned int soc;
258 	int ret;
259 
260 	ret = regmap_read(cw_bat->regmap, CW2015_REG_SOC, &soc);
261 	if (ret)
262 		return ret;
263 
264 	if (soc > 100) {
265 		int max_error_cycles =
266 			CW2015_BAT_SOC_ERROR_MS / cw_bat->poll_interval_ms;
267 
268 		dev_err(cw_bat->dev, "Invalid SoC %d%%\n", soc);
269 		cw_bat->read_errors++;
270 		if (cw_bat->read_errors > max_error_cycles) {
271 			dev_warn(cw_bat->dev,
272 				 "Too many invalid SoC reports, resetting gauge\n");
273 			cw_power_on_reset(cw_bat);
274 			cw_bat->read_errors = 0;
275 		}
276 		return cw_bat->soc;
277 	}
278 	cw_bat->read_errors = 0;
279 
280 	/* Reset gauge if stuck while charging */
281 	if (cw_bat->status == POWER_SUPPLY_STATUS_CHARGING && soc == cw_bat->soc) {
282 		int max_stuck_cycles =
283 			CW2015_BAT_CHARGING_STUCK_MS / cw_bat->poll_interval_ms;
284 
285 		cw_bat->charge_stuck_cnt++;
286 		if (cw_bat->charge_stuck_cnt > max_stuck_cycles) {
287 			dev_warn(cw_bat->dev,
288 				 "SoC stuck @%u%%, resetting gauge\n", soc);
289 			cw_power_on_reset(cw_bat);
290 			cw_bat->charge_stuck_cnt = 0;
291 		}
292 	} else {
293 		cw_bat->charge_stuck_cnt = 0;
294 	}
295 
296 	/* Ignore voltage dips during charge */
297 	if (cw_bat->charger_attached && HYSTERESIS(soc, cw_bat->soc, 0, 3))
298 		soc = cw_bat->soc;
299 
300 	/* Ignore voltage spikes during discharge */
301 	if (!cw_bat->charger_attached && HYSTERESIS(soc, cw_bat->soc, 3, 0))
302 		soc = cw_bat->soc;
303 
304 	return soc;
305 }
306 
cw_get_voltage(struct cw_battery * cw_bat)307 static int cw_get_voltage(struct cw_battery *cw_bat)
308 {
309 	int ret, i, voltage_mv;
310 	u16 reg_val;
311 	u32 avg = 0;
312 
313 	for (i = 0; i < CW2015_AVERAGING_SAMPLES; i++) {
314 		ret = cw_read_word(cw_bat, CW2015_REG_VCELL, &reg_val);
315 		if (ret)
316 			return ret;
317 
318 		avg += reg_val;
319 	}
320 	avg /= CW2015_AVERAGING_SAMPLES;
321 
322 	/*
323 	 * 305 uV per ADC step
324 	 * Use 312 / 1024  as efficient approximation of 305 / 1000
325 	 * Negligible error of 0.1%
326 	 */
327 	voltage_mv = avg * 312 / 1024;
328 	if (cw_bat->dual_cell)
329 		voltage_mv *= 2;
330 
331 	dev_dbg(cw_bat->dev, "Read voltage: %d mV, raw=0x%04x\n",
332 		voltage_mv, reg_val);
333 	return voltage_mv;
334 }
335 
cw_get_time_to_empty(struct cw_battery * cw_bat)336 static int cw_get_time_to_empty(struct cw_battery *cw_bat)
337 {
338 	int ret;
339 	u16 value16;
340 
341 	ret = cw_read_word(cw_bat, CW2015_REG_RRT_ALERT, &value16);
342 	if (ret)
343 		return ret;
344 
345 	return value16 & CW2015_MASK_SOC;
346 }
347 
cw_update_charge_status(struct cw_battery * cw_bat)348 static void cw_update_charge_status(struct cw_battery *cw_bat)
349 {
350 	int ret;
351 
352 	ret = power_supply_am_i_supplied(cw_bat->rk_bat);
353 	if (ret < 0) {
354 		dev_warn(cw_bat->dev, "Failed to get supply state: %d\n", ret);
355 	} else {
356 		bool charger_attached;
357 
358 		charger_attached = !!ret;
359 		if (cw_bat->charger_attached != charger_attached) {
360 			cw_bat->battery_changed = true;
361 			if (charger_attached)
362 				cw_bat->charge_count++;
363 		}
364 		cw_bat->charger_attached = charger_attached;
365 	}
366 }
367 
cw_update_soc(struct cw_battery * cw_bat)368 static void cw_update_soc(struct cw_battery *cw_bat)
369 {
370 	int soc;
371 
372 	soc = cw_get_soc(cw_bat);
373 	if (soc < 0)
374 		dev_err(cw_bat->dev, "Failed to get SoC from gauge: %d\n", soc);
375 	else if (cw_bat->soc != soc) {
376 		cw_bat->soc = soc;
377 		cw_bat->battery_changed = true;
378 	}
379 }
380 
cw_update_voltage(struct cw_battery * cw_bat)381 static void cw_update_voltage(struct cw_battery *cw_bat)
382 {
383 	int voltage_mv;
384 
385 	voltage_mv = cw_get_voltage(cw_bat);
386 	if (voltage_mv < 0)
387 		dev_err(cw_bat->dev, "Failed to get voltage from gauge: %d\n",
388 			voltage_mv);
389 	else
390 		cw_bat->voltage_mv = voltage_mv;
391 }
392 
cw_update_status(struct cw_battery * cw_bat)393 static void cw_update_status(struct cw_battery *cw_bat)
394 {
395 	int status = POWER_SUPPLY_STATUS_DISCHARGING;
396 
397 	if (cw_bat->charger_attached) {
398 		if (cw_bat->soc >= 100)
399 			status = POWER_SUPPLY_STATUS_FULL;
400 		else
401 			status = POWER_SUPPLY_STATUS_CHARGING;
402 	}
403 
404 	if (cw_bat->status != status)
405 		cw_bat->battery_changed = true;
406 	cw_bat->status = status;
407 }
408 
cw_update_time_to_empty(struct cw_battery * cw_bat)409 static void cw_update_time_to_empty(struct cw_battery *cw_bat)
410 {
411 	int time_to_empty;
412 
413 	time_to_empty = cw_get_time_to_empty(cw_bat);
414 	if (time_to_empty < 0) {
415 		dev_err(cw_bat->dev, "Failed to get time to empty from gauge: %d\n",
416 			time_to_empty);
417 		return;
418 	}
419 	cw_bat->time_to_empty = time_to_empty;
420 }
421 
cw_bat_work(struct work_struct * work)422 static void cw_bat_work(struct work_struct *work)
423 {
424 	struct delayed_work *delay_work;
425 	struct cw_battery *cw_bat;
426 	int ret;
427 	unsigned int reg_val;
428 
429 	delay_work = to_delayed_work(work);
430 	cw_bat = container_of(delay_work, struct cw_battery, battery_delay_work);
431 	ret = regmap_read(cw_bat->regmap, CW2015_REG_MODE, &reg_val);
432 	if (ret) {
433 		dev_err(cw_bat->dev, "Failed to read mode from gauge: %d\n", ret);
434 	} else {
435 		if ((reg_val & CW2015_MODE_SLEEP_MASK) == CW2015_MODE_SLEEP) {
436 			int i;
437 
438 			for (i = 0; i < CW2015_RESET_TRIES; i++) {
439 				if (!cw_power_on_reset(cw_bat))
440 					break;
441 			}
442 		}
443 		cw_update_soc(cw_bat);
444 		cw_update_voltage(cw_bat);
445 		cw_update_charge_status(cw_bat);
446 		cw_update_status(cw_bat);
447 		cw_update_time_to_empty(cw_bat);
448 	}
449 	dev_dbg(cw_bat->dev, "charger_attached = %d\n", cw_bat->charger_attached);
450 	dev_dbg(cw_bat->dev, "status = %d\n", cw_bat->status);
451 	dev_dbg(cw_bat->dev, "soc = %d%%\n", cw_bat->soc);
452 	dev_dbg(cw_bat->dev, "voltage = %dmV\n", cw_bat->voltage_mv);
453 
454 	if (cw_bat->battery_changed)
455 		power_supply_changed(cw_bat->rk_bat);
456 	cw_bat->battery_changed = false;
457 
458 	queue_delayed_work(cw_bat->battery_workqueue,
459 			   &cw_bat->battery_delay_work,
460 			   msecs_to_jiffies(cw_bat->poll_interval_ms));
461 }
462 
cw_battery_valid_time_to_empty(struct cw_battery * cw_bat)463 static bool cw_battery_valid_time_to_empty(struct cw_battery *cw_bat)
464 {
465 	return	cw_bat->time_to_empty > 0 &&
466 		cw_bat->time_to_empty < CW2015_MASK_SOC &&
467 		cw_bat->status == POWER_SUPPLY_STATUS_DISCHARGING;
468 }
469 
cw_get_capacity_leve(struct cw_battery * cw_bat)470 static int cw_get_capacity_leve(struct cw_battery *cw_bat)
471 {
472 	if (cw_bat->soc < 1)
473 		return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
474 	else if (cw_bat->soc <= 20)
475 		return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
476 	else if (cw_bat->soc <= 70)
477 		return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
478 	else if (cw_bat->soc <= 90)
479 		return POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
480 	else
481 		return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
482 }
483 
cw_battery_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)484 static int cw_battery_get_property(struct power_supply *psy,
485 				   enum power_supply_property psp,
486 				   union power_supply_propval *val)
487 {
488 	struct cw_battery *cw_bat;
489 
490 	cw_bat = power_supply_get_drvdata(psy);
491 	switch (psp) {
492 	case POWER_SUPPLY_PROP_CAPACITY:
493 		val->intval = cw_bat->soc;
494 		break;
495 
496 	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
497 		val->intval = cw_get_capacity_leve(cw_bat);
498 		break;
499 
500 	case POWER_SUPPLY_PROP_STATUS:
501 		val->intval = cw_bat->status;
502 		break;
503 
504 	case POWER_SUPPLY_PROP_PRESENT:
505 		val->intval = !!cw_bat->voltage_mv;
506 		break;
507 
508 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
509 		val->intval = cw_bat->voltage_mv * 1000;
510 		break;
511 
512 	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
513 		if (cw_battery_valid_time_to_empty(cw_bat))
514 			val->intval = cw_bat->time_to_empty;
515 		else
516 			val->intval = 0;
517 		break;
518 
519 	case POWER_SUPPLY_PROP_TECHNOLOGY:
520 		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
521 		break;
522 
523 	case POWER_SUPPLY_PROP_CHARGE_COUNTER:
524 		val->intval = cw_bat->charge_count;
525 		break;
526 
527 	case POWER_SUPPLY_PROP_CHARGE_FULL:
528 	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
529 		if (cw_bat->battery.charge_full_design_uah > 0)
530 			val->intval = cw_bat->battery.charge_full_design_uah;
531 		else
532 			val->intval = 0;
533 		break;
534 
535 	case POWER_SUPPLY_PROP_CURRENT_NOW:
536 		if (cw_battery_valid_time_to_empty(cw_bat) &&
537 		    cw_bat->battery.charge_full_design_uah > 0) {
538 			/* calculate remaining capacity */
539 			val->intval = cw_bat->battery.charge_full_design_uah;
540 			val->intval = val->intval * cw_bat->soc / 100;
541 
542 			/* estimate current based on time to empty */
543 			val->intval = 60 * val->intval / cw_bat->time_to_empty;
544 		} else {
545 			val->intval = 0;
546 		}
547 
548 		break;
549 
550 	default:
551 		break;
552 	}
553 	return 0;
554 }
555 
556 static enum power_supply_property cw_battery_properties[] = {
557 	POWER_SUPPLY_PROP_CAPACITY,
558 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
559 	POWER_SUPPLY_PROP_STATUS,
560 	POWER_SUPPLY_PROP_PRESENT,
561 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
562 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
563 	POWER_SUPPLY_PROP_TECHNOLOGY,
564 	POWER_SUPPLY_PROP_CHARGE_COUNTER,
565 	POWER_SUPPLY_PROP_CHARGE_FULL,
566 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
567 	POWER_SUPPLY_PROP_CURRENT_NOW,
568 };
569 
570 static const struct power_supply_desc cw2015_bat_desc = {
571 	.name		= "cw2015-battery",
572 	.type		= POWER_SUPPLY_TYPE_BATTERY,
573 	.properties	= cw_battery_properties,
574 	.num_properties	= ARRAY_SIZE(cw_battery_properties),
575 	.get_property	= cw_battery_get_property,
576 };
577 
cw2015_parse_properties(struct cw_battery * cw_bat)578 static int cw2015_parse_properties(struct cw_battery *cw_bat)
579 {
580 	struct device *dev = cw_bat->dev;
581 	int length;
582 	int ret;
583 
584 	length = device_property_count_u8(dev, "cellwise,battery-profile");
585 	if (length < 0) {
586 		dev_warn(cw_bat->dev,
587 			 "No battery-profile found, using current flash contents\n");
588 	} else if (length != CW2015_SIZE_BATINFO) {
589 		dev_err(cw_bat->dev, "battery-profile must be %d bytes\n",
590 			CW2015_SIZE_BATINFO);
591 		return -EINVAL;
592 	} else {
593 		cw_bat->bat_profile = devm_kzalloc(dev, length, GFP_KERNEL);
594 		if (!cw_bat->bat_profile)
595 			return -ENOMEM;
596 
597 		ret = device_property_read_u8_array(dev,
598 						"cellwise,battery-profile",
599 						cw_bat->bat_profile,
600 						length);
601 		if (ret)
602 			return ret;
603 	}
604 
605 	cw_bat->dual_cell = device_property_read_bool(dev, "cellwise,dual-cell");
606 
607 	ret = device_property_read_u32(dev, "cellwise,monitor-interval-ms",
608 				       &cw_bat->poll_interval_ms);
609 	if (ret) {
610 		dev_dbg(cw_bat->dev, "Using default poll interval\n");
611 		cw_bat->poll_interval_ms = CW2015_DEFAULT_POLL_INTERVAL_MS;
612 	}
613 
614 	return 0;
615 }
616 
617 static const struct regmap_range regmap_ranges_rd_yes[] = {
618 	regmap_reg_range(CW2015_REG_VERSION, CW2015_REG_VERSION),
619 	regmap_reg_range(CW2015_REG_VCELL, CW2015_REG_CONFIG),
620 	regmap_reg_range(CW2015_REG_MODE, CW2015_REG_MODE),
621 	regmap_reg_range(CW2015_REG_BATINFO,
622 			CW2015_REG_BATINFO + CW2015_SIZE_BATINFO - 1),
623 };
624 
625 static const struct regmap_access_table regmap_rd_table = {
626 	.yes_ranges = regmap_ranges_rd_yes,
627 	.n_yes_ranges = 4,
628 };
629 
630 static const struct regmap_range regmap_ranges_wr_yes[] = {
631 	regmap_reg_range(CW2015_REG_RRT_ALERT, CW2015_REG_CONFIG),
632 	regmap_reg_range(CW2015_REG_MODE, CW2015_REG_MODE),
633 	regmap_reg_range(CW2015_REG_BATINFO,
634 			CW2015_REG_BATINFO + CW2015_SIZE_BATINFO - 1),
635 };
636 
637 static const struct regmap_access_table regmap_wr_table = {
638 	.yes_ranges = regmap_ranges_wr_yes,
639 	.n_yes_ranges = 3,
640 };
641 
642 static const struct regmap_range regmap_ranges_vol_yes[] = {
643 	regmap_reg_range(CW2015_REG_VCELL, CW2015_REG_SOC + 1),
644 };
645 
646 static const struct regmap_access_table regmap_vol_table = {
647 	.yes_ranges = regmap_ranges_vol_yes,
648 	.n_yes_ranges = 1,
649 };
650 
651 static const struct regmap_config cw2015_regmap_config = {
652 	.reg_bits = 8,
653 	.val_bits = 8,
654 	.rd_table = &regmap_rd_table,
655 	.wr_table = &regmap_wr_table,
656 	.volatile_table = &regmap_vol_table,
657 	.max_register = CW2015_REG_BATINFO + CW2015_SIZE_BATINFO - 1,
658 };
659 
cw_bat_probe(struct i2c_client * client)660 static int cw_bat_probe(struct i2c_client *client)
661 {
662 	int ret;
663 	struct cw_battery *cw_bat;
664 	struct power_supply_config psy_cfg = { 0 };
665 
666 	cw_bat = devm_kzalloc(&client->dev, sizeof(*cw_bat), GFP_KERNEL);
667 	if (!cw_bat)
668 		return -ENOMEM;
669 
670 	i2c_set_clientdata(client, cw_bat);
671 	cw_bat->dev = &client->dev;
672 	cw_bat->soc = 1;
673 
674 	ret = cw2015_parse_properties(cw_bat);
675 	if (ret) {
676 		dev_err(cw_bat->dev, "Failed to parse cw2015 properties\n");
677 		return ret;
678 	}
679 
680 	cw_bat->regmap = devm_regmap_init_i2c(client, &cw2015_regmap_config);
681 	if (IS_ERR(cw_bat->regmap)) {
682 		dev_err(cw_bat->dev, "Failed to allocate regmap: %ld\n",
683 			PTR_ERR(cw_bat->regmap));
684 		return PTR_ERR(cw_bat->regmap);
685 	}
686 
687 	ret = cw_init(cw_bat);
688 	if (ret) {
689 		dev_err(cw_bat->dev, "Init failed: %d\n", ret);
690 		return ret;
691 	}
692 
693 	psy_cfg.drv_data = cw_bat;
694 	psy_cfg.fwnode = dev_fwnode(cw_bat->dev);
695 
696 	cw_bat->rk_bat = devm_power_supply_register(&client->dev,
697 						    &cw2015_bat_desc,
698 						    &psy_cfg);
699 	if (IS_ERR(cw_bat->rk_bat)) {
700 		/* try again if this happens */
701 		dev_err_probe(&client->dev, PTR_ERR(cw_bat->rk_bat),
702 			"Failed to register power supply\n");
703 		return PTR_ERR(cw_bat->rk_bat);
704 	}
705 
706 	ret = power_supply_get_battery_info(cw_bat->rk_bat, &cw_bat->battery);
707 	if (ret) {
708 		dev_warn(cw_bat->dev,
709 			 "No monitored battery, some properties will be missing\n");
710 	}
711 
712 	cw_bat->battery_workqueue = create_singlethread_workqueue("rk_battery");
713 	INIT_DELAYED_WORK(&cw_bat->battery_delay_work, cw_bat_work);
714 	queue_delayed_work(cw_bat->battery_workqueue,
715 			   &cw_bat->battery_delay_work, msecs_to_jiffies(10));
716 	return 0;
717 }
718 
cw_bat_suspend(struct device * dev)719 static int __maybe_unused cw_bat_suspend(struct device *dev)
720 {
721 	struct i2c_client *client = to_i2c_client(dev);
722 	struct cw_battery *cw_bat = i2c_get_clientdata(client);
723 
724 	cancel_delayed_work_sync(&cw_bat->battery_delay_work);
725 	return 0;
726 }
727 
cw_bat_resume(struct device * dev)728 static int __maybe_unused cw_bat_resume(struct device *dev)
729 {
730 	struct i2c_client *client = to_i2c_client(dev);
731 	struct cw_battery *cw_bat = i2c_get_clientdata(client);
732 
733 	queue_delayed_work(cw_bat->battery_workqueue,
734 			   &cw_bat->battery_delay_work, 0);
735 	return 0;
736 }
737 
738 static SIMPLE_DEV_PM_OPS(cw_bat_pm_ops, cw_bat_suspend, cw_bat_resume);
739 
cw_bat_remove(struct i2c_client * client)740 static int cw_bat_remove(struct i2c_client *client)
741 {
742 	struct cw_battery *cw_bat = i2c_get_clientdata(client);
743 
744 	cancel_delayed_work_sync(&cw_bat->battery_delay_work);
745 	power_supply_put_battery_info(cw_bat->rk_bat, &cw_bat->battery);
746 	return 0;
747 }
748 
749 static const struct i2c_device_id cw_bat_id_table[] = {
750 	{ "cw2015", 0 },
751 	{ }
752 };
753 
754 static const struct of_device_id cw2015_of_match[] = {
755 	{ .compatible = "cellwise,cw2015" },
756 	{ }
757 };
758 MODULE_DEVICE_TABLE(of, cw2015_of_match);
759 
760 static struct i2c_driver cw_bat_driver = {
761 	.driver = {
762 		.name = "cw2015",
763 		.of_match_table = cw2015_of_match,
764 		.pm = &cw_bat_pm_ops,
765 	},
766 	.probe_new = cw_bat_probe,
767 	.remove = cw_bat_remove,
768 	.id_table = cw_bat_id_table,
769 };
770 
771 module_i2c_driver(cw_bat_driver);
772 
773 MODULE_AUTHOR("xhc<xhc@rock-chips.com>");
774 MODULE_AUTHOR("Tobias Schramm <t.schramm@manjaro.org>");
775 MODULE_DESCRIPTION("cw2015/cw2013 battery driver");
776 MODULE_LICENSE("GPL");
777