power: add ec battery driver
[firefly-linux-kernel-4.4.55.git] / drivers / power / ec_battery.c
1 /*
2  * ec battery driver
3  *
4  * Copyright (C) 2016 Rockchip Electronics Co., Ltd
5  * Shunqing Chen <csq@rock-chips.com>
6  *
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.
10  *
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
14  * more details.
15  *
16  */
17
18 #include <linux/i2c.h>
19 #include <linux/module.h>
20 #include <linux/power_supply.h>
21
22 static int dbg_enable;
23 module_param_named(dbg_level, dbg_enable, int, 0644);
24
25 #define DBG(args...) \
26         do { \
27                 if (dbg_enable) { \
28                         printk(args); \
29                 } \
30         } while (0)
31
32 struct ec_battery {
33         struct i2c_client       *i2c;
34         struct device           *dev;
35         struct regmap           *regmap;
36         struct power_supply     *bat;
37         struct workqueue_struct *bat_monitor_wq;
38         struct delayed_work     bat_delay_work;
39         u32                     monitor_sec;
40         u32                     bat_mode;
41         u16                     status;
42         int                     current_now;
43         u16                     voltage_now;
44         u16                     rem_capacity;
45         u16                     full_charge_capacity;
46         u16                     design_capacity;
47         int                     temperature_now;
48         int                     soc;
49         bool                    is_charge;
50         bool                    dis_charge;
51         bool                    is_ctitical;
52         bool                    is_battery_low;
53         bool                    is_battery_in;
54         bool                    is_ac_in;
55 };
56
57 enum bat_mode {
58         MODE_BATTARY = 0,
59         MODE_VIRTUAL,
60 };
61
62 /* virtual params */
63 #define VIRTUAL_CURRENT                 1000
64 #define VIRTUAL_VOLTAGE                 3888
65 #define VIRTUAL_SOC                     66
66 #define VIRTUAL_PRESET                  1
67 #define VIRTUAL_TEMPERATURE             188
68 #define VIRTUAL_STATUS                  POWER_SUPPLY_STATUS_CHARGING
69
70 #define TIMER_MS_COUNTS                 1000
71 #define DEFAULT_MONITOR_SEC             5
72
73 #define EC_GET_VERSION_COMMOND          0x10
74 #define EC_GET_VERSION_INFO_NUM         (5)
75 #define EC_GET_BATTERY_INFO_COMMOND     0x07
76 #define EC_GET_PARAMETER_NUM            (13)
77 #define EC_GET_BATTERY_OTHER_COMMOND    0x08
78 #define EC_GET_BATTERYINFO_NUM          (7)
79
80 #define EC_GET_BIT(a, b)        (((a) & (1 << (b))) ? 1 : 0)
81 #define EC_DIS_CHARGE(a)        EC_GET_BIT(a, 0)
82 #define EC_IS_CHARGE(a)         EC_GET_BIT(a, 1)
83 #define EC_IS_CRITICAL(a)       EC_GET_BIT(a, 2)
84 #define EC_IS_BATTERY_LOW(a)    EC_GET_BIT(a, 3)
85 #define EC_IS_BATTERY_IN(a)     EC_GET_BIT(a, 6)
86 #define EC_IS_AC_IN(a)          EC_GET_BIT(a, 7)
87
88 static int ec_i2c_read(struct ec_battery *bat, u8 cmd, u8 *dest, u16 len)
89 {
90         struct i2c_client *i2c = bat->i2c;
91         int ret;
92         struct i2c_msg msg[2];
93         u8 buf[2];
94
95         buf[0] = cmd; /* EC_GET_BATTERY_INFO_COMMOND; */
96         msg[0].addr = i2c->addr;
97         msg[0].flags = i2c->flags & I2C_M_TEN;
98         msg[0].len = 1;
99         msg[0].buf = buf;
100
101         msg[1].addr = i2c->addr;
102         msg[1].flags = i2c->flags & I2C_M_TEN;
103         msg[1].flags |= I2C_M_RD;
104         msg[1].len = len;
105         msg[1].buf = dest;
106
107         ret = i2c_transfer(i2c->adapter, msg, 2);
108
109         return ret;
110 }
111
112 static void ec_dump_info(struct ec_battery *bat)
113 {
114         int temp;
115
116         DBG("==========================================\n");
117         DBG("battery status: %x\n", bat->status);
118         temp = bat->temperature_now / 10;
119         DBG("Temp: %d K (%d C))\n", temp, (temp - 272));
120         DBG("current_now: %d ma\n", bat->current_now);
121         DBG("voltage_now: %d mv\n", bat->voltage_now);
122         DBG("Charge:    %d %%\n", bat->soc);
123         DBG("Remaining: %d mAh\n", bat->rem_capacity);
124         DBG("Cap-full:  %d mAh\n", bat->full_charge_capacity);
125         DBG("Design:    %d mAh\n", bat->design_capacity);
126         DBG("==========================================\n");
127 }
128
129 static int ec_get_battery_info(struct ec_battery *bat)
130 {
131         u8 buf[13] = {0};
132         u16 voltage2;
133         u16 full_charge_capacity_1;
134         u16 design_capacity;
135         int ret;
136         int soc;
137
138         ret = ec_i2c_read(bat, EC_GET_BATTERY_INFO_COMMOND, buf,
139                           EC_GET_PARAMETER_NUM);
140         if ((EC_GET_PARAMETER_NUM - 1) == buf[0]) {
141                 bat->status = buf[2] << 8 | buf[1];
142                 bat->current_now = buf[4] << 8 | buf[3];
143                 bat->rem_capacity = buf[6] << 8 | buf[5];
144                 bat->voltage_now = buf[8] << 8 | buf[7];
145                 bat->full_charge_capacity = buf[10] << 8 | buf[9];
146                 bat->temperature_now = buf[12] << 8 | buf[11];
147                 soc = (bat->rem_capacity + bat->full_charge_capacity / 101) *
148                         100 / bat->full_charge_capacity;
149                 if (soc > 100)
150                         bat->soc = 100;
151                 else if (soc < 0)
152                         bat->soc = 0;
153                 else
154                         bat->soc = soc;
155         } else {
156                 dev_err(bat->dev, "get battery info from 0x07 erro\n");
157         }
158
159         ret = ec_i2c_read(bat, EC_GET_BATTERY_OTHER_COMMOND, buf,
160                           EC_GET_BATTERYINFO_NUM);
161         full_charge_capacity_1 = buf[2] << 8 | buf[1];
162         voltage2 = buf[4] << 8 | buf[3];        /* the same to uppo */
163         design_capacity = buf[6] << 8 | buf[5]; /* the same to uppo */
164         bat->design_capacity = design_capacity;
165
166         ec_dump_info(bat);
167
168         return 0;
169 }
170
171 static int ec_get_current(struct ec_battery *bat)
172 {
173         return bat->current_now * 1000;
174 }
175
176 static int ec_get_voltage(struct ec_battery *bat)
177 {
178         return bat->voltage_now * 1000;
179 }
180
181 static int is_ec_bat_exist(struct ec_battery *bat)
182 {
183         int is_exist;
184
185         is_exist = EC_IS_BATTERY_IN(bat->status);
186         return is_exist;
187 }
188
189 static int ec_get_capacity(struct ec_battery *bat)
190 {
191         return bat->soc;
192 }
193
194 static int ec_get_temperature(struct ec_battery *bat)
195 {
196         int temp;
197
198         temp = bat->temperature_now - 2722;
199         return temp;
200 }
201
202 static int ec_bat_chrg_online(struct ec_battery *bat)
203 {
204         return EC_IS_CHARGE(bat->status);
205 }
206
207 #ifdef CONFIG_OF
208 static int ec_bat_parse_dt(struct ec_battery *bat)
209 {
210         int ret;
211         u32 out_value;
212         struct device_node *np = bat->dev->of_node;
213
214         bat->bat_mode = MODE_BATTARY;
215         bat->monitor_sec = DEFAULT_MONITOR_SEC * TIMER_MS_COUNTS;
216
217         ret = of_property_read_u32(np, "virtual_power", &bat->bat_mode);
218         if (ret < 0)
219                 dev_err(bat->dev, "virtual_power missing!\n");
220
221         ret = of_property_read_u32(np, "monitor_sec", &out_value);
222         if (ret < 0)
223                 dev_err(bat->dev, "monitor_sec missing!\n");
224         else
225                 bat->monitor_sec = out_value * TIMER_MS_COUNTS;
226
227         return 0;
228 }
229 #else
230 static int ec_bat_parse_dt(struct ec_battery *bat)
231 {
232         return -ENODEV;
233 }
234 #endif
235
236 static enum power_supply_property ec_bat_props[] = {
237         POWER_SUPPLY_PROP_CURRENT_NOW,
238         POWER_SUPPLY_PROP_VOLTAGE_NOW,
239         POWER_SUPPLY_PROP_PRESENT,
240         POWER_SUPPLY_PROP_HEALTH,
241         POWER_SUPPLY_PROP_CAPACITY,
242         POWER_SUPPLY_PROP_TEMP,
243         POWER_SUPPLY_PROP_STATUS,
244 };
245
246 static int ec_battery_get_property(struct power_supply *psy,
247                                    enum power_supply_property psp,
248                                    union power_supply_propval *val)
249 {
250         struct ec_battery *bat = power_supply_get_drvdata(psy);
251
252         switch (psp) {
253         case POWER_SUPPLY_PROP_CURRENT_NOW:
254                 val->intval = ec_get_current(bat);/*uA*/
255                 if (bat->bat_mode == MODE_VIRTUAL)
256                         val->intval = VIRTUAL_CURRENT * 1000;
257                 break;
258         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
259                 val->intval = ec_get_voltage(bat);/*uV*/
260                 if (bat->bat_mode == MODE_VIRTUAL)
261                         val->intval = VIRTUAL_VOLTAGE * 1000;
262                 break;
263         case POWER_SUPPLY_PROP_PRESENT:
264                 val->intval = is_ec_bat_exist(bat);
265                 if (bat->bat_mode == MODE_VIRTUAL)
266                         val->intval = VIRTUAL_PRESET;
267                 break;
268         case POWER_SUPPLY_PROP_CAPACITY:
269                 val->intval = ec_get_capacity(bat);
270                 if (bat->bat_mode == MODE_VIRTUAL)
271                         val->intval = VIRTUAL_SOC;
272                 break;
273         case POWER_SUPPLY_PROP_HEALTH:
274                 val->intval = POWER_SUPPLY_HEALTH_GOOD;
275                 break;
276         case POWER_SUPPLY_PROP_TEMP:
277                 val->intval = ec_get_temperature(bat);
278                 if (bat->bat_mode == MODE_VIRTUAL)
279                         val->intval = VIRTUAL_TEMPERATURE;
280                 break;
281         case POWER_SUPPLY_PROP_STATUS:
282                 if (bat->bat_mode == MODE_VIRTUAL)
283                         val->intval = VIRTUAL_STATUS;
284                 else if (ec_get_capacity(bat) == 100)
285                         val->intval = POWER_SUPPLY_STATUS_FULL;
286                 else if (ec_bat_chrg_online(bat))
287                         val->intval = POWER_SUPPLY_STATUS_CHARGING;
288                 else
289                         val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
290                 break;
291         default:
292                 return -EINVAL;
293         }
294
295         return 0;
296 }
297
298 static const struct power_supply_desc ec_bat_desc = {
299         .name           = "battery",
300         .type           = POWER_SUPPLY_TYPE_BATTERY,
301         .properties     = ec_bat_props,
302         .num_properties = ARRAY_SIZE(ec_bat_props),
303         .get_property   = ec_battery_get_property,
304 };
305
306 static int ec_bat_init_power_supply(struct ec_battery *bat)
307 {
308         struct power_supply_config psy_cfg = { .drv_data = bat, };
309
310         bat->bat = power_supply_register(bat->dev, &ec_bat_desc, &psy_cfg);
311         if (IS_ERR(bat->bat)) {
312                 dev_err(bat->dev, "register bat power supply fail\n");
313                 return PTR_ERR(bat->bat);
314         }
315
316         return 0;
317 }
318
319 static void ec_bat_power_supply_changed(struct ec_battery *ec_bat)
320 {
321         bool state_changed;
322         static int old_cap = -1;
323         static int old_temperature;
324
325         state_changed = false;
326         if (ec_get_capacity(ec_bat) != old_cap)
327                 state_changed = true;
328         else if (ec_get_temperature(ec_bat) != old_temperature)
329                 state_changed = true;
330
331         if (state_changed) {
332                 power_supply_changed(ec_bat->bat);
333                 old_cap = ec_get_capacity(ec_bat);
334                 old_temperature = ec_get_temperature(ec_bat);
335         }
336 }
337
338 static void ec_battery_work(struct work_struct *work)
339 {
340         struct ec_battery *ec_bat =
341                 container_of(work, struct ec_battery, bat_delay_work.work);
342
343         ec_get_battery_info(ec_bat);
344         ec_bat_power_supply_changed(ec_bat);
345
346         queue_delayed_work(ec_bat->bat_monitor_wq, &ec_bat->bat_delay_work,
347                            msecs_to_jiffies(ec_bat->monitor_sec));
348 }
349
350 static int ec_charger_probe(struct i2c_client *client,
351                             const struct i2c_device_id *id)
352 {
353         struct ec_battery *ec_bat;
354         int ret;
355
356         ec_bat = devm_kzalloc(&client->dev, sizeof(*ec_bat), GFP_KERNEL);
357         if (!ec_bat)
358                 return -ENOMEM;
359         ec_bat->dev = &client->dev;
360         ec_bat->i2c = client;
361         i2c_set_clientdata(client, ec_bat);
362
363         ret = ec_bat_parse_dt(ec_bat);
364         if (ret < 0) {
365                 dev_err(ec_bat->dev, "parse dt failed!\n");
366                 return ret;
367         }
368
369         ret = ec_bat_init_power_supply(ec_bat);
370         if (ret) {
371                 dev_err(ec_bat->dev, "init power supply fail!\n");
372                 return ret;
373         }
374
375         ec_bat->bat_monitor_wq =
376                 alloc_ordered_workqueue("%s",
377                                         WQ_MEM_RECLAIM | WQ_FREEZABLE,
378                                         "ec-bat-monitor-wq");
379         INIT_DELAYED_WORK(&ec_bat->bat_delay_work, ec_battery_work);
380         queue_delayed_work(ec_bat->bat_monitor_wq, &ec_bat->bat_delay_work,
381                            msecs_to_jiffies(TIMER_MS_COUNTS * 5));
382
383         return ret;
384 }
385
386 static const struct i2c_device_id ec_battery_i2c_ids[] = {
387         { "ec_battery" },
388         { },
389 };
390 MODULE_DEVICE_TABLE(i2c, ec_battery_i2c_ids);
391
392 #ifdef CONFIG_OF
393 static const struct of_device_id ec_of_match[] = {
394         { .compatible = "rockchip,ec-battery" },
395         { },
396 };
397 MODULE_DEVICE_TABLE(of, ec_of_match);
398 #else
399 static const struct of_device_id ec_of_match[] = {
400         { },
401 };
402 #endif
403
404 static struct i2c_driver ec_i2c_driver = {
405         .driver = {
406                 .name = "ec_battery",
407                 .of_match_table = ec_of_match,
408         },
409         .id_table       = ec_battery_i2c_ids,
410         .probe          = ec_charger_probe,
411 };
412
413 module_i2c_driver(ec_i2c_driver);
414
415 MODULE_LICENSE("GPL");
416 MODULE_ALIAS("platform:ec-charger");
417 MODULE_AUTHOR("Shunqing Chen<csq@rock-chips.com>");