iommu/rockchip: don't do power domain enable on probe
[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/gpio.h>
19 #include <linux/i2c.h>
20 #include <linux/module.h>
21 #include <linux/power_supply.h>
22
23 static int dbg_enable;
24 module_param_named(dbg_level, dbg_enable, int, 0644);
25
26 #define DBG(args...) \
27         do { \
28                 if (dbg_enable) { \
29                         printk(args); \
30                 } \
31         } while (0)
32
33 struct ec_battery {
34         struct i2c_client       *i2c;
35         struct device           *dev;
36         struct regmap           *regmap;
37         struct power_supply     *bat;
38         struct workqueue_struct *bat_monitor_wq;
39         struct delayed_work     bat_delay_work;
40         u32                     monitor_sec;
41         u32                     bat_mode;
42         u16                     status;
43         int                     current_now;
44         u16                     voltage_now;
45         u16                     rem_capacity;
46         u16                     full_charge_capacity;
47         u16                     design_capacity;
48         int                     temperature_now;
49         int                     soc;
50         bool                    is_charge;
51         bool                    dis_charge;
52         bool                    is_ctitical;
53         bool                    is_battery_low;
54         bool                    is_battery_in;
55         bool                    is_ac_in;
56         struct gpio_desc        *ec_notify_io;
57 };
58
59 enum bat_mode {
60         MODE_BATTARY = 0,
61         MODE_VIRTUAL,
62 };
63
64 /* virtual params */
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
71
72 #define TIMER_MS_COUNTS                 1000
73 #define DEFAULT_MONITOR_SEC             5
74
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)
81
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)
89
90 static int ec_i2c_read(struct ec_battery *bat, u8 cmd, u8 *dest, u16 len)
91 {
92         struct i2c_client *i2c = bat->i2c;
93         int ret;
94         struct i2c_msg msg[2];
95         u8 buf[2];
96
97         buf[0] = cmd; /* EC_GET_BATTERY_INFO_COMMOND; */
98         msg[0].addr = i2c->addr;
99         msg[0].flags = i2c->flags & I2C_M_TEN;
100         msg[0].len = 1;
101         msg[0].buf = buf;
102
103         msg[1].addr = i2c->addr;
104         msg[1].flags = i2c->flags & I2C_M_TEN;
105         msg[1].flags |= I2C_M_RD;
106         msg[1].len = len;
107         msg[1].buf = dest;
108
109         ret = i2c_transfer(i2c->adapter, msg, 2);
110
111         return ret;
112 }
113
114 static void ec_dump_info(struct ec_battery *bat)
115 {
116         int temp;
117
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");
129 }
130
131 static int ec_get_battery_info(struct ec_battery *bat)
132 {
133         u8 buf[13] = {0};
134         u16 voltage2;
135         u16 full_charge_capacity_1;
136         u16 design_capacity;
137         u16 cur;
138         int ret;
139         int soc;
140
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;
147                 if (buf[4] & 0x80) {
148                         bat->current_now = (~cur) & 0xffff;
149                         bat->current_now = -(bat->current_now);
150                 }
151
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;
158                 if (soc > 100)
159                         bat->soc = 100;
160                 else if (soc < 0)
161                         bat->soc = 0;
162                 else
163                         bat->soc = soc;
164         } else {
165                 dev_err(bat->dev, "get battery info from 0x07 erro\n");
166         }
167
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;
175         }
176
177         ec_dump_info(bat);
178
179         return 0;
180 }
181
182 static int ec_get_current(struct ec_battery *bat)
183 {
184         return bat->current_now * 1000;
185 }
186
187 static int ec_get_voltage(struct ec_battery *bat)
188 {
189         return bat->voltage_now * 1000;
190 }
191
192 static int is_ec_bat_exist(struct ec_battery *bat)
193 {
194         int is_exist;
195
196         is_exist = EC_IS_BATTERY_IN(bat->status);
197         return is_exist;
198 }
199
200 static int ec_get_capacity(struct ec_battery *bat)
201 {
202         return bat->soc;
203 }
204
205 static int ec_get_temperature(struct ec_battery *bat)
206 {
207         int temp;
208
209         temp = bat->temperature_now - 2722;
210         return temp;
211 }
212
213 static int ec_bat_chrg_online(struct ec_battery *bat)
214 {
215         return EC_IS_CHARGE(bat->status);
216 }
217
218 #ifdef CONFIG_OF
219 static int ec_bat_parse_dt(struct ec_battery *bat)
220 {
221         int ret;
222         u32 out_value;
223         struct device_node *np = bat->dev->of_node;
224
225         bat->bat_mode = MODE_BATTARY;
226         bat->monitor_sec = DEFAULT_MONITOR_SEC * TIMER_MS_COUNTS;
227
228         ret = of_property_read_u32(np, "virtual_power", &bat->bat_mode);
229         if (ret < 0)
230                 dev_err(bat->dev, "virtual_power missing!\n");
231
232         ret = of_property_read_u32(np, "monitor_sec", &out_value);
233         if (ret < 0)
234                 dev_err(bat->dev, "monitor_sec missing!\n");
235         else
236                 bat->monitor_sec = out_value * TIMER_MS_COUNTS;
237
238         bat->ec_notify_io =
239                 devm_gpiod_get_optional(bat->dev, "ec-notify",
240                                         GPIOD_IN);
241         if (!IS_ERR_OR_NULL(bat->ec_notify_io))
242                 gpiod_direction_output(bat->ec_notify_io, 0);
243
244         return 0;
245 }
246 #else
247 static int ec_bat_parse_dt(struct ec_battery *bat)
248 {
249         return -ENODEV;
250 }
251 #endif
252
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,
261 };
262
263 static int ec_battery_get_property(struct power_supply *psy,
264                                    enum power_supply_property psp,
265                                    union power_supply_propval *val)
266 {
267         struct ec_battery *bat = power_supply_get_drvdata(psy);
268
269         switch (psp) {
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;
274                 break;
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;
279                 break;
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;
284                 break;
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;
289                 break;
290         case POWER_SUPPLY_PROP_HEALTH:
291                 val->intval = POWER_SUPPLY_HEALTH_GOOD;
292                 break;
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;
297                 break;
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;
305                 else
306                         val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
307                 break;
308         default:
309                 return -EINVAL;
310         }
311
312         return 0;
313 }
314
315 static const struct power_supply_desc ec_bat_desc = {
316         .name           = "battery",
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,
321 };
322
323 static int ec_bat_init_power_supply(struct ec_battery *bat)
324 {
325         struct power_supply_config psy_cfg = { .drv_data = bat, };
326
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);
331         }
332
333         return 0;
334 }
335
336 static void ec_bat_power_supply_changed(struct ec_battery *ec_bat)
337 {
338         bool state_changed;
339         static int old_cap = -1;
340         static int old_temperature;
341
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;
347
348         if (state_changed) {
349                 power_supply_changed(ec_bat->bat);
350                 old_cap = ec_get_capacity(ec_bat);
351                 old_temperature = ec_get_temperature(ec_bat);
352         }
353 }
354
355 static void ec_battery_work(struct work_struct *work)
356 {
357         struct ec_battery *ec_bat =
358                 container_of(work, struct ec_battery, bat_delay_work.work);
359
360         ec_get_battery_info(ec_bat);
361         ec_bat_power_supply_changed(ec_bat);
362
363         queue_delayed_work(ec_bat->bat_monitor_wq, &ec_bat->bat_delay_work,
364                            msecs_to_jiffies(ec_bat->monitor_sec));
365 }
366
367 static int ec_charger_probe(struct i2c_client *client,
368                             const struct i2c_device_id *id)
369 {
370         struct ec_battery *ec_bat;
371         int ret;
372
373         ec_bat = devm_kzalloc(&client->dev, sizeof(*ec_bat), GFP_KERNEL);
374         if (!ec_bat)
375                 return -ENOMEM;
376         ec_bat->dev = &client->dev;
377         ec_bat->i2c = client;
378         i2c_set_clientdata(client, ec_bat);
379
380         ret = ec_bat_parse_dt(ec_bat);
381         if (ret < 0) {
382                 dev_err(ec_bat->dev, "parse dt failed!\n");
383                 return ret;
384         }
385
386         ret = ec_bat_init_power_supply(ec_bat);
387         if (ret) {
388                 dev_err(ec_bat->dev, "init power supply fail!\n");
389                 return ret;
390         }
391
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));
399
400         return ret;
401 }
402
403 #ifdef CONFIG_PM_SLEEP
404 static int ec_bat_pm_suspend(struct device *dev)
405 {
406         struct i2c_client *client = to_i2c_client(dev);
407         struct ec_battery *ec_bat = i2c_get_clientdata(client);
408
409         cancel_delayed_work_sync(&ec_bat->bat_delay_work);
410
411         if (!IS_ERR_OR_NULL(ec_bat->ec_notify_io))
412                 gpiod_direction_output(ec_bat->ec_notify_io, 1);
413
414         return 0;
415 }
416
417 static int ec_bat_pm_resume(struct device *dev)
418 {
419         struct i2c_client *client = to_i2c_client(dev);
420         struct ec_battery *ec_bat = i2c_get_clientdata(client);
421
422         if (!IS_ERR_OR_NULL(ec_bat->ec_notify_io))
423                 gpiod_direction_output(ec_bat->ec_notify_io, 0);
424
425         queue_delayed_work(ec_bat->bat_monitor_wq, &ec_bat->bat_delay_work,
426                            msecs_to_jiffies(TIMER_MS_COUNTS * 1));
427
428         return 0;
429 }
430 #endif
431
432 static SIMPLE_DEV_PM_OPS(ec_bat_pm_ops, ec_bat_pm_suspend, ec_bat_pm_resume);
433
434 static const struct i2c_device_id ec_battery_i2c_ids[] = {
435         { "ec_battery" },
436         { },
437 };
438 MODULE_DEVICE_TABLE(i2c, ec_battery_i2c_ids);
439
440 #ifdef CONFIG_OF
441 static const struct of_device_id ec_of_match[] = {
442         { .compatible = "rockchip,ec-battery" },
443         { },
444 };
445 MODULE_DEVICE_TABLE(of, ec_of_match);
446 #else
447 static const struct of_device_id ec_of_match[] = {
448         { },
449 };
450 #endif
451
452 static struct i2c_driver ec_i2c_driver = {
453         .driver = {
454                 .name           = "ec_battery",
455                 .pm             = &ec_bat_pm_ops,
456                 .of_match_table = ec_of_match,
457         },
458         .id_table       = ec_battery_i2c_ids,
459         .probe          = ec_charger_probe,
460 };
461
462 module_i2c_driver(ec_i2c_driver);
463
464 MODULE_LICENSE("GPL");
465 MODULE_ALIAS("platform:ec-charger");
466 MODULE_AUTHOR("Shunqing Chen<csq@rock-chips.com>");