4 * Copyright (C) 2016 Rockchip Electronics Co., Ltd
5 * Shunqing Chen <csq@rock-chips.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 #include <linux/gpio.h>
19 #include <linux/i2c.h>
20 #include <linux/module.h>
21 #include <linux/power_supply.h>
23 static int dbg_enable;
24 module_param_named(dbg_level, dbg_enable, int, 0644);
26 #define DBG(args...) \
34 struct i2c_client *i2c;
36 struct regmap *regmap;
37 struct power_supply *bat;
38 struct workqueue_struct *bat_monitor_wq;
39 struct delayed_work bat_delay_work;
46 u16 full_charge_capacity;
56 struct gpio_desc *ec_notify_io;
65 #define VIRTUAL_CURRENT 1000
66 #define VIRTUAL_VOLTAGE 3888
67 #define VIRTUAL_SOC 66
68 #define VIRTUAL_PRESET 1
69 #define VIRTUAL_TEMPERATURE 188
70 #define VIRTUAL_STATUS POWER_SUPPLY_STATUS_CHARGING
72 #define TIMER_MS_COUNTS 1000
73 #define DEFAULT_MONITOR_SEC 5
75 #define EC_GET_VERSION_COMMOND 0x10
76 #define EC_GET_VERSION_INFO_NUM (5)
77 #define EC_GET_BATTERY_INFO_COMMOND 0x07
78 #define EC_GET_PARAMETER_NUM (13)
79 #define EC_GET_BATTERY_OTHER_COMMOND 0x08
80 #define EC_GET_BATTERYINFO_NUM (7)
82 #define EC_GET_BIT(a, b) (((a) & (1 << (b))) ? 1 : 0)
83 #define EC_DIS_CHARGE(a) EC_GET_BIT(a, 0)
84 #define EC_IS_CHARGE(a) EC_GET_BIT(a, 1)
85 #define EC_IS_CRITICAL(a) EC_GET_BIT(a, 2)
86 #define EC_IS_BATTERY_LOW(a) EC_GET_BIT(a, 3)
87 #define EC_IS_BATTERY_IN(a) EC_GET_BIT(a, 6)
88 #define EC_IS_AC_IN(a) EC_GET_BIT(a, 7)
90 static int ec_i2c_read(struct ec_battery *bat, u8 cmd, u8 *dest, u16 len)
92 struct i2c_client *i2c = bat->i2c;
94 struct i2c_msg msg[2];
97 buf[0] = cmd; /* EC_GET_BATTERY_INFO_COMMOND; */
98 msg[0].addr = i2c->addr;
99 msg[0].flags = i2c->flags & I2C_M_TEN;
103 msg[1].addr = i2c->addr;
104 msg[1].flags = i2c->flags & I2C_M_TEN;
105 msg[1].flags |= I2C_M_RD;
109 ret = i2c_transfer(i2c->adapter, msg, 2);
114 static void ec_dump_info(struct ec_battery *bat)
118 DBG("==========================================\n");
119 DBG("battery status: %x\n", bat->status);
120 temp = bat->temperature_now / 10;
121 DBG("Temp: %d K (%d C))\n", temp, (temp - 272));
122 DBG("current_now: %d ma\n", bat->current_now);
123 DBG("voltage_now: %d mv\n", bat->voltage_now);
124 DBG("Charge: %d %%\n", bat->soc);
125 DBG("Remaining: %d mAh\n", bat->rem_capacity);
126 DBG("Cap-full: %d mAh\n", bat->full_charge_capacity);
127 DBG("Design: %d mAh\n", bat->design_capacity);
128 DBG("==========================================\n");
131 static int ec_get_battery_info(struct ec_battery *bat)
135 u16 full_charge_capacity_1;
141 ret = ec_i2c_read(bat, EC_GET_BATTERY_INFO_COMMOND, buf,
142 EC_GET_PARAMETER_NUM);
143 if ((EC_GET_PARAMETER_NUM - 1) == buf[0]) {
144 bat->status = buf[2] << 8 | buf[1];
145 cur = (buf[4] << 8 | buf[3]);
146 bat->current_now = cur;
148 bat->current_now = (~cur) & 0xffff;
149 bat->current_now = -(bat->current_now);
152 bat->rem_capacity = buf[6] << 8 | buf[5];
153 bat->voltage_now = buf[8] << 8 | buf[7];
154 bat->full_charge_capacity = buf[10] << 8 | buf[9];
155 bat->temperature_now = buf[12] << 8 | buf[11];
156 soc = (bat->rem_capacity + bat->full_charge_capacity / 101) *
157 100 / bat->full_charge_capacity;
165 dev_err(bat->dev, "get battery info from 0x07 erro\n");
168 ret = ec_i2c_read(bat, EC_GET_BATTERY_OTHER_COMMOND, buf,
169 EC_GET_BATTERYINFO_NUM);
170 if ((EC_GET_BATTERYINFO_NUM - 1) == buf[0]) {
171 full_charge_capacity_1 = buf[2] << 8 | buf[1];
172 voltage2 = buf[4] << 8 | buf[3]; /* the same to uppo */
173 design_capacity = buf[6] << 8 | buf[5]; /* the same to uppo */
174 bat->design_capacity = design_capacity;
182 static int ec_get_current(struct ec_battery *bat)
184 return bat->current_now * 1000;
187 static int ec_get_voltage(struct ec_battery *bat)
189 return bat->voltage_now * 1000;
192 static int is_ec_bat_exist(struct ec_battery *bat)
196 is_exist = EC_IS_BATTERY_IN(bat->status);
200 static int ec_get_capacity(struct ec_battery *bat)
205 static int ec_get_temperature(struct ec_battery *bat)
209 temp = bat->temperature_now - 2722;
213 static int ec_bat_chrg_online(struct ec_battery *bat)
215 return EC_IS_CHARGE(bat->status);
219 static int ec_bat_parse_dt(struct ec_battery *bat)
223 struct device_node *np = bat->dev->of_node;
225 bat->bat_mode = MODE_BATTARY;
226 bat->monitor_sec = DEFAULT_MONITOR_SEC * TIMER_MS_COUNTS;
228 ret = of_property_read_u32(np, "virtual_power", &bat->bat_mode);
230 dev_err(bat->dev, "virtual_power missing!\n");
232 ret = of_property_read_u32(np, "monitor_sec", &out_value);
234 dev_err(bat->dev, "monitor_sec missing!\n");
236 bat->monitor_sec = out_value * TIMER_MS_COUNTS;
239 devm_gpiod_get_optional(bat->dev, "ec-notify",
241 if (!IS_ERR_OR_NULL(bat->ec_notify_io))
242 gpiod_direction_output(bat->ec_notify_io, 0);
247 static int ec_bat_parse_dt(struct ec_battery *bat)
253 static enum power_supply_property ec_bat_props[] = {
254 POWER_SUPPLY_PROP_CURRENT_NOW,
255 POWER_SUPPLY_PROP_VOLTAGE_NOW,
256 POWER_SUPPLY_PROP_PRESENT,
257 POWER_SUPPLY_PROP_HEALTH,
258 POWER_SUPPLY_PROP_CAPACITY,
259 POWER_SUPPLY_PROP_TEMP,
260 POWER_SUPPLY_PROP_STATUS,
263 static int ec_battery_get_property(struct power_supply *psy,
264 enum power_supply_property psp,
265 union power_supply_propval *val)
267 struct ec_battery *bat = power_supply_get_drvdata(psy);
270 case POWER_SUPPLY_PROP_CURRENT_NOW:
271 val->intval = ec_get_current(bat);/*uA*/
272 if (bat->bat_mode == MODE_VIRTUAL)
273 val->intval = VIRTUAL_CURRENT * 1000;
275 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
276 val->intval = ec_get_voltage(bat);/*uV*/
277 if (bat->bat_mode == MODE_VIRTUAL)
278 val->intval = VIRTUAL_VOLTAGE * 1000;
280 case POWER_SUPPLY_PROP_PRESENT:
281 val->intval = is_ec_bat_exist(bat);
282 if (bat->bat_mode == MODE_VIRTUAL)
283 val->intval = VIRTUAL_PRESET;
285 case POWER_SUPPLY_PROP_CAPACITY:
286 val->intval = ec_get_capacity(bat);
287 if (bat->bat_mode == MODE_VIRTUAL)
288 val->intval = VIRTUAL_SOC;
290 case POWER_SUPPLY_PROP_HEALTH:
291 val->intval = POWER_SUPPLY_HEALTH_GOOD;
293 case POWER_SUPPLY_PROP_TEMP:
294 val->intval = ec_get_temperature(bat);
295 if (bat->bat_mode == MODE_VIRTUAL)
296 val->intval = VIRTUAL_TEMPERATURE;
298 case POWER_SUPPLY_PROP_STATUS:
299 if (bat->bat_mode == MODE_VIRTUAL)
300 val->intval = VIRTUAL_STATUS;
301 else if (ec_get_capacity(bat) == 100)
302 val->intval = POWER_SUPPLY_STATUS_FULL;
303 else if (ec_bat_chrg_online(bat))
304 val->intval = POWER_SUPPLY_STATUS_CHARGING;
306 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
315 static const struct power_supply_desc ec_bat_desc = {
317 .type = POWER_SUPPLY_TYPE_BATTERY,
318 .properties = ec_bat_props,
319 .num_properties = ARRAY_SIZE(ec_bat_props),
320 .get_property = ec_battery_get_property,
323 static int ec_bat_init_power_supply(struct ec_battery *bat)
325 struct power_supply_config psy_cfg = { .drv_data = bat, };
327 bat->bat = power_supply_register(bat->dev, &ec_bat_desc, &psy_cfg);
328 if (IS_ERR(bat->bat)) {
329 dev_err(bat->dev, "register bat power supply fail\n");
330 return PTR_ERR(bat->bat);
336 static void ec_bat_power_supply_changed(struct ec_battery *ec_bat)
339 static int old_cap = -1;
340 static int old_temperature;
342 state_changed = false;
343 if (ec_get_capacity(ec_bat) != old_cap)
344 state_changed = true;
345 else if (ec_get_temperature(ec_bat) != old_temperature)
346 state_changed = true;
349 power_supply_changed(ec_bat->bat);
350 old_cap = ec_get_capacity(ec_bat);
351 old_temperature = ec_get_temperature(ec_bat);
355 static void ec_battery_work(struct work_struct *work)
357 struct ec_battery *ec_bat =
358 container_of(work, struct ec_battery, bat_delay_work.work);
360 ec_get_battery_info(ec_bat);
361 ec_bat_power_supply_changed(ec_bat);
363 queue_delayed_work(ec_bat->bat_monitor_wq, &ec_bat->bat_delay_work,
364 msecs_to_jiffies(ec_bat->monitor_sec));
367 static int ec_charger_probe(struct i2c_client *client,
368 const struct i2c_device_id *id)
370 struct ec_battery *ec_bat;
373 ec_bat = devm_kzalloc(&client->dev, sizeof(*ec_bat), GFP_KERNEL);
376 ec_bat->dev = &client->dev;
377 ec_bat->i2c = client;
378 i2c_set_clientdata(client, ec_bat);
380 ret = ec_bat_parse_dt(ec_bat);
382 dev_err(ec_bat->dev, "parse dt failed!\n");
386 ret = ec_bat_init_power_supply(ec_bat);
388 dev_err(ec_bat->dev, "init power supply fail!\n");
392 ec_bat->bat_monitor_wq =
393 alloc_ordered_workqueue("%s",
394 WQ_MEM_RECLAIM | WQ_FREEZABLE,
395 "ec-bat-monitor-wq");
396 INIT_DELAYED_WORK(&ec_bat->bat_delay_work, ec_battery_work);
397 queue_delayed_work(ec_bat->bat_monitor_wq, &ec_bat->bat_delay_work,
398 msecs_to_jiffies(TIMER_MS_COUNTS * 5));
403 #ifdef CONFIG_PM_SLEEP
404 static int ec_bat_pm_suspend(struct device *dev)
406 struct i2c_client *client = to_i2c_client(dev);
407 struct ec_battery *ec_bat = i2c_get_clientdata(client);
409 cancel_delayed_work_sync(&ec_bat->bat_delay_work);
411 if (!IS_ERR_OR_NULL(ec_bat->ec_notify_io))
412 gpiod_direction_output(ec_bat->ec_notify_io, 1);
417 static int ec_bat_pm_resume(struct device *dev)
419 struct i2c_client *client = to_i2c_client(dev);
420 struct ec_battery *ec_bat = i2c_get_clientdata(client);
422 if (!IS_ERR_OR_NULL(ec_bat->ec_notify_io))
423 gpiod_direction_output(ec_bat->ec_notify_io, 0);
425 queue_delayed_work(ec_bat->bat_monitor_wq, &ec_bat->bat_delay_work,
426 msecs_to_jiffies(TIMER_MS_COUNTS * 1));
432 static SIMPLE_DEV_PM_OPS(ec_bat_pm_ops, ec_bat_pm_suspend, ec_bat_pm_resume);
434 static const struct i2c_device_id ec_battery_i2c_ids[] = {
438 MODULE_DEVICE_TABLE(i2c, ec_battery_i2c_ids);
441 static const struct of_device_id ec_of_match[] = {
442 { .compatible = "rockchip,ec-battery" },
445 MODULE_DEVICE_TABLE(of, ec_of_match);
447 static const struct of_device_id ec_of_match[] = {
452 static struct i2c_driver ec_i2c_driver = {
454 .name = "ec_battery",
455 .pm = &ec_bat_pm_ops,
456 .of_match_table = ec_of_match,
458 .id_table = ec_battery_i2c_ids,
459 .probe = ec_charger_probe,
462 module_i2c_driver(ec_i2c_driver);
464 MODULE_LICENSE("GPL");
465 MODULE_ALIAS("platform:ec-charger");
466 MODULE_AUTHOR("Shunqing Chen<csq@rock-chips.com>");