Skip to content

Commit 0250b83

Browse files
Shunqing Chenrkhuangtao
authored andcommitted
power: rk818: add power_supply property for CTS.
Android8.0 CTS needs to get max charging current, max charging voltage and battery charge counter. Change-Id: Ia0e6589e92f24ece179299040336e050b21258b1 Signed-off-by: Shunqing Chen <csq@rock-chips.com>
1 parent a3c7958 commit 0250b83

2 files changed

Lines changed: 103 additions & 0 deletions

File tree

drivers/power/rk818_battery.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ struct rk818_battery {
174174
bool is_initialized;
175175
bool is_first_power_on;
176176
u8 res_div;
177+
int current_max;
178+
int voltage_max;
177179
int current_avg;
178180
int voltage_avg;
179181
int voltage_ocv;
@@ -250,6 +252,8 @@ struct rk818_battery {
250252
int dbg_calc_rsoc;
251253
u8 ac_in;
252254
u8 usb_in;
255+
int is_charging;
256+
unsigned long charge_count;
253257
};
254258

255259
#define DIV(x) ((x) ? (x) : 1)
@@ -877,6 +881,9 @@ static enum power_supply_property rk818_bat_props[] = {
877881
POWER_SUPPLY_PROP_CAPACITY,
878882
POWER_SUPPLY_PROP_TEMP,
879883
POWER_SUPPLY_PROP_STATUS,
884+
POWER_SUPPLY_PROP_CHARGE_COUNTER,
885+
POWER_SUPPLY_PROP_VOLTAGE_MAX,
886+
POWER_SUPPLY_PROP_CURRENT_MAX,
880887
};
881888

882889
static int rk818_bat_get_usb_psy(struct device *dev, void *data)
@@ -991,6 +998,15 @@ static int rk818_battery_get_property(struct power_supply *psy,
991998
else
992999
val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
9931000
break;
1001+
case POWER_SUPPLY_PROP_CHARGE_COUNTER:
1002+
val->intval = di->charge_count;
1003+
break;
1004+
case POWER_SUPPLY_PROP_VOLTAGE_MAX:
1005+
val->intval = di->voltage_max;
1006+
break;
1007+
case POWER_SUPPLY_PROP_CURRENT_MAX:
1008+
val->intval = di->current_max;
1009+
break;
9941010
default:
9951011
return -EINVAL;
9961012
}
@@ -2555,12 +2571,24 @@ static void rk818_bat_rsoc_daemon(struct rk818_battery *di)
25552571

25562572
static void rk818_bat_update_info(struct rk818_battery *di)
25572573
{
2574+
int is_charging;
2575+
25582576
di->voltage_avg = rk818_bat_get_avg_voltage(di);
25592577
di->current_avg = rk818_bat_get_avg_current(di);
25602578
di->voltage_relax = rk818_bat_get_relax_voltage(di);
25612579
di->rsoc = rk818_bat_get_rsoc(di);
25622580
di->remain_cap = rk818_bat_get_coulomb_cap(di);
25632581
di->chrg_status = rk818_bat_get_chrg_status(di);
2582+
is_charging = rk818_bat_get_charge_state(di);
2583+
if (is_charging != di->is_charging) {
2584+
di->is_charging = is_charging;
2585+
if (is_charging)
2586+
di->charge_count++;
2587+
}
2588+
if (di->voltage_avg > di->voltage_max)
2589+
di->voltage_max = di->voltage_avg;
2590+
if (di->current_avg > di->current_max)
2591+
di->current_max = di->current_avg;
25642592

25652593
/* smooth charge */
25662594
if (di->remain_cap > di->fcc) {

drivers/power/rk818_charger.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ struct rk818_charger {
134134
struct regmap *regmap;
135135
struct power_supply *ac_psy;
136136
struct power_supply *usb_psy;
137+
struct power_supply *bat_psy;
137138
struct extcon_dev *cable_edev;
138139
struct charger_platform_data *pdata;
139140
struct workqueue_struct *usb_charger_wq;
@@ -297,14 +298,76 @@ static int rk818_cg_lowpwr_check(struct rk818_charger *cg)
297298
return fake_offline;
298299
}
299300

301+
static int rk818_cg_get_bat_psy(struct device *dev, void *data)
302+
{
303+
struct rk818_charger *cg = data;
304+
struct power_supply *psy = dev_get_drvdata(dev);
305+
306+
if (psy->desc->type == POWER_SUPPLY_TYPE_BATTERY) {
307+
cg->bat_psy = psy;
308+
return 1;
309+
}
310+
311+
return 0;
312+
}
313+
314+
static void rk818_cg_get_psy(struct rk818_charger *cg)
315+
{
316+
if (!cg->bat_psy)
317+
class_for_each_device(power_supply_class, NULL, (void *)cg,
318+
rk818_cg_get_bat_psy);
319+
}
320+
321+
static int rk818_cg_get_bat_max_cur(struct rk818_charger *cg)
322+
{
323+
union power_supply_propval val;
324+
int ret;
325+
326+
rk818_cg_get_psy(cg);
327+
328+
if (!cg->bat_psy)
329+
return cg->pdata->max_chrg_current;
330+
331+
ret = cg->bat_psy->desc->get_property(cg->bat_psy,
332+
POWER_SUPPLY_PROP_CURRENT_MAX,
333+
&val);
334+
if (!ret && val.intval)
335+
return val.intval;
336+
337+
return cg->pdata->max_chrg_current;
338+
}
339+
340+
static int rk818_cg_get_bat_max_vol(struct rk818_charger *cg)
341+
{
342+
union power_supply_propval val;
343+
int ret;
344+
345+
rk818_cg_get_psy(cg);
346+
347+
if (!cg->bat_psy)
348+
return cg->pdata->max_chrg_voltage;
349+
350+
ret = cg->bat_psy->desc->get_property(cg->bat_psy,
351+
POWER_SUPPLY_PROP_VOLTAGE_MAX,
352+
&val);
353+
if (!ret && val.intval)
354+
return val.intval;
355+
356+
return cg->pdata->max_chrg_voltage;
357+
}
358+
300359
static enum power_supply_property rk818_ac_props[] = {
301360
POWER_SUPPLY_PROP_ONLINE,
302361
POWER_SUPPLY_PROP_STATUS,
362+
POWER_SUPPLY_PROP_VOLTAGE_MAX,
363+
POWER_SUPPLY_PROP_CURRENT_MAX,
303364
};
304365

305366
static enum power_supply_property rk818_usb_props[] = {
306367
POWER_SUPPLY_PROP_ONLINE,
307368
POWER_SUPPLY_PROP_STATUS,
369+
POWER_SUPPLY_PROP_VOLTAGE_MAX,
370+
POWER_SUPPLY_PROP_CURRENT_MAX,
308371
};
309372

310373
static int rk818_cg_ac_get_property(struct power_supply *psy,
@@ -338,6 +401,12 @@ static int rk818_cg_ac_get_property(struct power_supply *psy,
338401

339402
DBG("report prop: %d\n", val->intval);
340403
break;
404+
case POWER_SUPPLY_PROP_VOLTAGE_MAX:
405+
val->intval = rk818_cg_get_bat_max_vol(cg);
406+
break;
407+
case POWER_SUPPLY_PROP_CURRENT_MAX:
408+
val->intval = rk818_cg_get_bat_max_cur(cg);
409+
break;
341410
default:
342411
ret = -EINVAL;
343412
break;
@@ -377,6 +446,12 @@ static int rk818_cg_usb_get_property(struct power_supply *psy,
377446

378447
DBG("report prop: %d\n", val->intval);
379448
break;
449+
case POWER_SUPPLY_PROP_VOLTAGE_MAX:
450+
val->intval = rk818_cg_get_bat_max_vol(cg);
451+
break;
452+
case POWER_SUPPLY_PROP_CURRENT_MAX:
453+
val->intval = rk818_cg_get_bat_max_cur(cg);
454+
break;
380455
default:
381456
ret = -EINVAL;
382457
break;

0 commit comments

Comments
 (0)