Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Samsung Electronics Co., Ltd. |
| 3 | * MyungJoo Ham <myungjoo.ham@samsung.com> |
| 4 | * |
| 5 | * This driver enables to monitor battery health and control charger |
| 6 | * during suspend-to-mem. |
| 7 | * Charger manager depends on other devices. register this later than |
| 8 | * the depending devices. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | **/ |
| 14 | |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 15 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 16 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 17 | #include <linux/io.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/irq.h> |
| 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/rtc.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/workqueue.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/power/charger-manager.h> |
| 26 | #include <linux/regulator/consumer.h> |
Chanwoo Choi | 3950c78 | 2012-09-21 18:49:37 +0900 | [diff] [blame] | 27 | #include <linux/sysfs.h> |
Jonghwa Lee | 856ee61 | 2013-12-18 15:42:35 +0900 | [diff] [blame] | 28 | #include <linux/of.h> |
Jonghwa Lee | 5c49a62 | 2013-12-18 15:42:34 +0900 | [diff] [blame] | 29 | #include <linux/thermal.h> |
| 30 | |
| 31 | /* |
| 32 | * Default termperature threshold for charging. |
| 33 | * Every temperature units are in tenth of centigrade. |
| 34 | */ |
| 35 | #define CM_DEFAULT_RECHARGE_TEMP_DIFF 50 |
| 36 | #define CM_DEFAULT_CHARGE_TEMP_MAX 500 |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 37 | |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 38 | static const char * const default_event_names[] = { |
| 39 | [CM_EVENT_UNKNOWN] = "Unknown", |
| 40 | [CM_EVENT_BATT_FULL] = "Battery Full", |
| 41 | [CM_EVENT_BATT_IN] = "Battery Inserted", |
| 42 | [CM_EVENT_BATT_OUT] = "Battery Pulled Out", |
Jonghwa Lee | 5c49a62 | 2013-12-18 15:42:34 +0900 | [diff] [blame] | 43 | [CM_EVENT_BATT_OVERHEAT] = "Battery Overheat", |
| 44 | [CM_EVENT_BATT_COLD] = "Battery Cold", |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 45 | [CM_EVENT_EXT_PWR_IN_OUT] = "External Power Attach/Detach", |
| 46 | [CM_EVENT_CHG_START_STOP] = "Charging Start/Stop", |
| 47 | [CM_EVENT_OTHERS] = "Other battery events" |
| 48 | }; |
| 49 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 50 | /* |
| 51 | * Regard CM_JIFFIES_SMALL jiffies is small enough to ignore for |
| 52 | * delayed works so that we can run delayed works with CM_JIFFIES_SMALL |
| 53 | * without any delays. |
| 54 | */ |
| 55 | #define CM_JIFFIES_SMALL (2) |
| 56 | |
| 57 | /* If y is valid (> 0) and smaller than x, do x = y */ |
| 58 | #define CM_MIN_VALID(x, y) x = (((y > 0) && ((x) > (y))) ? (y) : (x)) |
| 59 | |
| 60 | /* |
| 61 | * Regard CM_RTC_SMALL (sec) is small enough to ignore error in invoking |
| 62 | * rtc alarm. It should be 2 or larger |
| 63 | */ |
| 64 | #define CM_RTC_SMALL (2) |
| 65 | |
| 66 | #define UEVENT_BUF_SIZE 32 |
| 67 | |
| 68 | static LIST_HEAD(cm_list); |
| 69 | static DEFINE_MUTEX(cm_list_mtx); |
| 70 | |
| 71 | /* About in-suspend (suspend-again) monitoring */ |
Jonghwa Lee | c1155c6 | 2014-12-19 17:55:13 +0900 | [diff] [blame] | 72 | static struct alarm *cm_timer; |
| 73 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 74 | static bool cm_suspended; |
Jonghwa Lee | c1155c6 | 2014-12-19 17:55:13 +0900 | [diff] [blame] | 75 | static bool cm_timer_set; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 76 | static unsigned long cm_suspend_duration_ms; |
| 77 | |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 78 | /* About normal (not suspended) monitoring */ |
| 79 | static unsigned long polling_jiffy = ULONG_MAX; /* ULONG_MAX: no polling */ |
| 80 | static unsigned long next_polling; /* Next appointed polling time */ |
| 81 | static struct workqueue_struct *cm_wq; /* init at driver add */ |
| 82 | static struct delayed_work cm_monitor_work; /* init at driver add */ |
| 83 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 84 | /** |
| 85 | * is_batt_present - See if the battery presents in place. |
| 86 | * @cm: the Charger Manager representing the battery. |
| 87 | */ |
| 88 | static bool is_batt_present(struct charger_manager *cm) |
| 89 | { |
| 90 | union power_supply_propval val; |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame] | 91 | struct power_supply *psy; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 92 | bool present = false; |
| 93 | int i, ret; |
| 94 | |
| 95 | switch (cm->desc->battery_present) { |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 96 | case CM_BATTERY_PRESENT: |
| 97 | present = true; |
| 98 | break; |
| 99 | case CM_NO_BATTERY: |
| 100 | break; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 101 | case CM_FUEL_GAUGE: |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame] | 102 | psy = power_supply_get_by_name(cm->desc->psy_fuel_gauge); |
| 103 | if (!psy) |
| 104 | break; |
| 105 | |
Krzysztof Kozlowski | b70229b | 2015-03-12 08:44:10 +0100 | [diff] [blame] | 106 | ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_PRESENT, |
| 107 | &val); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 108 | if (ret == 0 && val.intval) |
| 109 | present = true; |
Krzysztof Kozlowski | b43eb35 | 2015-03-12 08:44:14 +0100 | [diff] [blame] | 110 | power_supply_put(psy); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 111 | break; |
| 112 | case CM_CHARGER_STAT: |
Krzysztof Kozlowski | cdaf3e1 | 2014-10-13 15:34:31 +0200 | [diff] [blame] | 113 | for (i = 0; cm->desc->psy_charger_stat[i]; i++) { |
| 114 | psy = power_supply_get_by_name( |
| 115 | cm->desc->psy_charger_stat[i]); |
| 116 | if (!psy) { |
| 117 | dev_err(cm->dev, "Cannot find power supply \"%s\"\n", |
| 118 | cm->desc->psy_charger_stat[i]); |
| 119 | continue; |
| 120 | } |
| 121 | |
Krzysztof Kozlowski | b70229b | 2015-03-12 08:44:10 +0100 | [diff] [blame] | 122 | ret = power_supply_get_property(psy, |
| 123 | POWER_SUPPLY_PROP_PRESENT, &val); |
Krzysztof Kozlowski | b43eb35 | 2015-03-12 08:44:14 +0100 | [diff] [blame] | 124 | power_supply_put(psy); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 125 | if (ret == 0 && val.intval) { |
| 126 | present = true; |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | break; |
| 131 | } |
| 132 | |
| 133 | return present; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * is_ext_pwr_online - See if an external power source is attached to charge |
| 138 | * @cm: the Charger Manager representing the battery. |
| 139 | * |
| 140 | * Returns true if at least one of the chargers of the battery has an external |
| 141 | * power source attached to charge the battery regardless of whether it is |
| 142 | * actually charging or not. |
| 143 | */ |
| 144 | static bool is_ext_pwr_online(struct charger_manager *cm) |
| 145 | { |
| 146 | union power_supply_propval val; |
Krzysztof Kozlowski | cdaf3e1 | 2014-10-13 15:34:31 +0200 | [diff] [blame] | 147 | struct power_supply *psy; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 148 | bool online = false; |
| 149 | int i, ret; |
| 150 | |
| 151 | /* If at least one of them has one, it's yes. */ |
Krzysztof Kozlowski | cdaf3e1 | 2014-10-13 15:34:31 +0200 | [diff] [blame] | 152 | for (i = 0; cm->desc->psy_charger_stat[i]; i++) { |
| 153 | psy = power_supply_get_by_name(cm->desc->psy_charger_stat[i]); |
| 154 | if (!psy) { |
| 155 | dev_err(cm->dev, "Cannot find power supply \"%s\"\n", |
| 156 | cm->desc->psy_charger_stat[i]); |
| 157 | continue; |
| 158 | } |
| 159 | |
Krzysztof Kozlowski | b70229b | 2015-03-12 08:44:10 +0100 | [diff] [blame] | 160 | ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_ONLINE, |
| 161 | &val); |
Krzysztof Kozlowski | b43eb35 | 2015-03-12 08:44:14 +0100 | [diff] [blame] | 162 | power_supply_put(psy); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 163 | if (ret == 0 && val.intval) { |
| 164 | online = true; |
| 165 | break; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | return online; |
| 170 | } |
| 171 | |
| 172 | /** |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 173 | * get_batt_uV - Get the voltage level of the battery |
| 174 | * @cm: the Charger Manager representing the battery. |
| 175 | * @uV: the voltage level returned. |
| 176 | * |
| 177 | * Returns 0 if there is no error. |
| 178 | * Returns a negative value on error. |
| 179 | */ |
| 180 | static int get_batt_uV(struct charger_manager *cm, int *uV) |
| 181 | { |
| 182 | union power_supply_propval val; |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame] | 183 | struct power_supply *fuel_gauge; |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 184 | int ret; |
| 185 | |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame] | 186 | fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge); |
| 187 | if (!fuel_gauge) |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 188 | return -ENODEV; |
| 189 | |
Krzysztof Kozlowski | b70229b | 2015-03-12 08:44:10 +0100 | [diff] [blame] | 190 | ret = power_supply_get_property(fuel_gauge, |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 191 | POWER_SUPPLY_PROP_VOLTAGE_NOW, &val); |
Krzysztof Kozlowski | b43eb35 | 2015-03-12 08:44:14 +0100 | [diff] [blame] | 192 | power_supply_put(fuel_gauge); |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 193 | if (ret) |
| 194 | return ret; |
| 195 | |
| 196 | *uV = val.intval; |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | /** |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 201 | * is_charging - Returns true if the battery is being charged. |
| 202 | * @cm: the Charger Manager representing the battery. |
| 203 | */ |
| 204 | static bool is_charging(struct charger_manager *cm) |
| 205 | { |
| 206 | int i, ret; |
| 207 | bool charging = false; |
Krzysztof Kozlowski | cdaf3e1 | 2014-10-13 15:34:31 +0200 | [diff] [blame] | 208 | struct power_supply *psy; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 209 | union power_supply_propval val; |
| 210 | |
| 211 | /* If there is no battery, it cannot be charged */ |
| 212 | if (!is_batt_present(cm)) |
| 213 | return false; |
| 214 | |
| 215 | /* If at least one of the charger is charging, return yes */ |
Krzysztof Kozlowski | cdaf3e1 | 2014-10-13 15:34:31 +0200 | [diff] [blame] | 216 | for (i = 0; cm->desc->psy_charger_stat[i]; i++) { |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 217 | /* 1. The charger sholuld not be DISABLED */ |
| 218 | if (cm->emergency_stop) |
| 219 | continue; |
| 220 | if (!cm->charger_enabled) |
| 221 | continue; |
| 222 | |
Krzysztof Kozlowski | cdaf3e1 | 2014-10-13 15:34:31 +0200 | [diff] [blame] | 223 | psy = power_supply_get_by_name(cm->desc->psy_charger_stat[i]); |
| 224 | if (!psy) { |
| 225 | dev_err(cm->dev, "Cannot find power supply \"%s\"\n", |
| 226 | cm->desc->psy_charger_stat[i]); |
| 227 | continue; |
| 228 | } |
| 229 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 230 | /* 2. The charger should be online (ext-power) */ |
Krzysztof Kozlowski | b70229b | 2015-03-12 08:44:10 +0100 | [diff] [blame] | 231 | ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_ONLINE, |
| 232 | &val); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 233 | if (ret) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 234 | dev_warn(cm->dev, "Cannot read ONLINE value from %s\n", |
| 235 | cm->desc->psy_charger_stat[i]); |
Krzysztof Kozlowski | b43eb35 | 2015-03-12 08:44:14 +0100 | [diff] [blame] | 236 | power_supply_put(psy); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 237 | continue; |
| 238 | } |
Krzysztof Kozlowski | b43eb35 | 2015-03-12 08:44:14 +0100 | [diff] [blame] | 239 | if (val.intval == 0) { |
| 240 | power_supply_put(psy); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 241 | continue; |
Krzysztof Kozlowski | b43eb35 | 2015-03-12 08:44:14 +0100 | [diff] [blame] | 242 | } |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 243 | |
| 244 | /* |
| 245 | * 3. The charger should not be FULL, DISCHARGING, |
| 246 | * or NOT_CHARGING. |
| 247 | */ |
Krzysztof Kozlowski | b70229b | 2015-03-12 08:44:10 +0100 | [diff] [blame] | 248 | ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_STATUS, |
| 249 | &val); |
Krzysztof Kozlowski | b43eb35 | 2015-03-12 08:44:14 +0100 | [diff] [blame] | 250 | power_supply_put(psy); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 251 | if (ret) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 252 | dev_warn(cm->dev, "Cannot read STATUS value from %s\n", |
| 253 | cm->desc->psy_charger_stat[i]); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 254 | continue; |
| 255 | } |
| 256 | if (val.intval == POWER_SUPPLY_STATUS_FULL || |
| 257 | val.intval == POWER_SUPPLY_STATUS_DISCHARGING || |
| 258 | val.intval == POWER_SUPPLY_STATUS_NOT_CHARGING) |
| 259 | continue; |
| 260 | |
| 261 | /* Then, this is charging. */ |
| 262 | charging = true; |
| 263 | break; |
| 264 | } |
| 265 | |
| 266 | return charging; |
| 267 | } |
| 268 | |
| 269 | /** |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 270 | * is_full_charged - Returns true if the battery is fully charged. |
| 271 | * @cm: the Charger Manager representing the battery. |
| 272 | */ |
| 273 | static bool is_full_charged(struct charger_manager *cm) |
| 274 | { |
| 275 | struct charger_desc *desc = cm->desc; |
| 276 | union power_supply_propval val; |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame] | 277 | struct power_supply *fuel_gauge; |
Krzysztof Kozlowski | b43eb35 | 2015-03-12 08:44:14 +0100 | [diff] [blame] | 278 | bool is_full = false; |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 279 | int ret = 0; |
| 280 | int uV; |
| 281 | |
| 282 | /* If there is no battery, it cannot be charged */ |
Chanwoo Choi | 0fa11db | 2012-11-22 16:53:46 +0900 | [diff] [blame] | 283 | if (!is_batt_present(cm)) |
| 284 | return false; |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 285 | |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame] | 286 | fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge); |
| 287 | if (!fuel_gauge) |
| 288 | return false; |
| 289 | |
| 290 | if (desc->fullbatt_full_capacity > 0) { |
Chanwoo Choi | 0fa11db | 2012-11-22 16:53:46 +0900 | [diff] [blame] | 291 | val.intval = 0; |
| 292 | |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 293 | /* Not full if capacity of fuel gauge isn't full */ |
Krzysztof Kozlowski | b70229b | 2015-03-12 08:44:10 +0100 | [diff] [blame] | 294 | ret = power_supply_get_property(fuel_gauge, |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 295 | POWER_SUPPLY_PROP_CHARGE_FULL, &val); |
Krzysztof Kozlowski | b43eb35 | 2015-03-12 08:44:14 +0100 | [diff] [blame] | 296 | if (!ret && val.intval > desc->fullbatt_full_capacity) { |
| 297 | is_full = true; |
| 298 | goto out; |
| 299 | } |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /* Full, if it's over the fullbatt voltage */ |
| 303 | if (desc->fullbatt_uV > 0) { |
| 304 | ret = get_batt_uV(cm, &uV); |
Krzysztof Kozlowski | b43eb35 | 2015-03-12 08:44:14 +0100 | [diff] [blame] | 305 | if (!ret && uV >= desc->fullbatt_uV) { |
| 306 | is_full = true; |
| 307 | goto out; |
| 308 | } |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /* Full, if the capacity is more than fullbatt_soc */ |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame] | 312 | if (desc->fullbatt_soc > 0) { |
Chanwoo Choi | 0fa11db | 2012-11-22 16:53:46 +0900 | [diff] [blame] | 313 | val.intval = 0; |
| 314 | |
Krzysztof Kozlowski | b70229b | 2015-03-12 08:44:10 +0100 | [diff] [blame] | 315 | ret = power_supply_get_property(fuel_gauge, |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 316 | POWER_SUPPLY_PROP_CAPACITY, &val); |
Krzysztof Kozlowski | b43eb35 | 2015-03-12 08:44:14 +0100 | [diff] [blame] | 317 | if (!ret && val.intval >= desc->fullbatt_soc) { |
| 318 | is_full = true; |
| 319 | goto out; |
| 320 | } |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 321 | } |
| 322 | |
Krzysztof Kozlowski | b43eb35 | 2015-03-12 08:44:14 +0100 | [diff] [blame] | 323 | out: |
| 324 | power_supply_put(fuel_gauge); |
| 325 | return is_full; |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /** |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 329 | * is_polling_required - Return true if need to continue polling for this CM. |
| 330 | * @cm: the Charger Manager representing the battery. |
| 331 | */ |
| 332 | static bool is_polling_required(struct charger_manager *cm) |
| 333 | { |
| 334 | switch (cm->desc->polling_mode) { |
| 335 | case CM_POLL_DISABLE: |
| 336 | return false; |
| 337 | case CM_POLL_ALWAYS: |
| 338 | return true; |
| 339 | case CM_POLL_EXTERNAL_POWER_ONLY: |
| 340 | return is_ext_pwr_online(cm); |
| 341 | case CM_POLL_CHARGING_ONLY: |
| 342 | return is_charging(cm); |
| 343 | default: |
| 344 | dev_warn(cm->dev, "Incorrect polling_mode (%d)\n", |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 345 | cm->desc->polling_mode); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | return false; |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * try_charger_enable - Enable/Disable chargers altogether |
| 353 | * @cm: the Charger Manager representing the battery. |
| 354 | * @enable: true: enable / false: disable |
| 355 | * |
| 356 | * Note that Charger Manager keeps the charger enabled regardless whether |
| 357 | * the charger is charging or not (because battery is full or no external |
| 358 | * power source exists) except when CM needs to disable chargers forcibly |
| 359 | * bacause of emergency causes; when the battery is overheated or too cold. |
| 360 | */ |
| 361 | static int try_charger_enable(struct charger_manager *cm, bool enable) |
| 362 | { |
| 363 | int err = 0, i; |
| 364 | struct charger_desc *desc = cm->desc; |
| 365 | |
| 366 | /* Ignore if it's redundent command */ |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 367 | if (enable == cm->charger_enabled) |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 368 | return 0; |
| 369 | |
| 370 | if (enable) { |
| 371 | if (cm->emergency_stop) |
| 372 | return -EAGAIN; |
Chanwoo Choi | 8fcfe08 | 2012-09-20 21:20:05 -0700 | [diff] [blame] | 373 | |
| 374 | /* |
| 375 | * Save start time of charging to limit |
| 376 | * maximum possible charging time. |
| 377 | */ |
| 378 | cm->charging_start_time = ktime_to_ms(ktime_get()); |
| 379 | cm->charging_end_time = 0; |
| 380 | |
Chanwoo Choi | dbb61fc | 2012-07-27 14:01:34 +0900 | [diff] [blame] | 381 | for (i = 0 ; i < desc->num_charger_regulators ; i++) { |
Chanwoo Choi | 3950c78 | 2012-09-21 18:49:37 +0900 | [diff] [blame] | 382 | if (desc->charger_regulators[i].externally_control) |
| 383 | continue; |
| 384 | |
Chanwoo Choi | dbb61fc | 2012-07-27 14:01:34 +0900 | [diff] [blame] | 385 | err = regulator_enable(desc->charger_regulators[i].consumer); |
| 386 | if (err < 0) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 387 | dev_warn(cm->dev, "Cannot enable %s regulator\n", |
| 388 | desc->charger_regulators[i].regulator_name); |
Chanwoo Choi | dbb61fc | 2012-07-27 14:01:34 +0900 | [diff] [blame] | 389 | } |
| 390 | } |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 391 | } else { |
| 392 | /* |
Chanwoo Choi | 8fcfe08 | 2012-09-20 21:20:05 -0700 | [diff] [blame] | 393 | * Save end time of charging to maintain fully charged state |
| 394 | * of battery after full-batt. |
| 395 | */ |
| 396 | cm->charging_start_time = 0; |
| 397 | cm->charging_end_time = ktime_to_ms(ktime_get()); |
| 398 | |
Chanwoo Choi | dbb61fc | 2012-07-27 14:01:34 +0900 | [diff] [blame] | 399 | for (i = 0 ; i < desc->num_charger_regulators ; i++) { |
Chanwoo Choi | 3950c78 | 2012-09-21 18:49:37 +0900 | [diff] [blame] | 400 | if (desc->charger_regulators[i].externally_control) |
| 401 | continue; |
| 402 | |
Chanwoo Choi | dbb61fc | 2012-07-27 14:01:34 +0900 | [diff] [blame] | 403 | err = regulator_disable(desc->charger_regulators[i].consumer); |
| 404 | if (err < 0) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 405 | dev_warn(cm->dev, "Cannot disable %s regulator\n", |
| 406 | desc->charger_regulators[i].regulator_name); |
Chanwoo Choi | dbb61fc | 2012-07-27 14:01:34 +0900 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 410 | /* |
| 411 | * Abnormal battery state - Stop charging forcibly, |
| 412 | * even if charger was enabled at the other places |
| 413 | */ |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 414 | for (i = 0; i < desc->num_charger_regulators; i++) { |
| 415 | if (regulator_is_enabled( |
| 416 | desc->charger_regulators[i].consumer)) { |
| 417 | regulator_force_disable( |
| 418 | desc->charger_regulators[i].consumer); |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 419 | dev_warn(cm->dev, "Disable regulator(%s) forcibly\n", |
| 420 | desc->charger_regulators[i].regulator_name); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | if (!err) |
| 426 | cm->charger_enabled = enable; |
| 427 | |
| 428 | return err; |
| 429 | } |
| 430 | |
| 431 | /** |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 432 | * try_charger_restart - Restart charging. |
| 433 | * @cm: the Charger Manager representing the battery. |
| 434 | * |
| 435 | * Restart charging by turning off and on the charger. |
| 436 | */ |
| 437 | static int try_charger_restart(struct charger_manager *cm) |
| 438 | { |
| 439 | int err; |
| 440 | |
| 441 | if (cm->emergency_stop) |
| 442 | return -EAGAIN; |
| 443 | |
| 444 | err = try_charger_enable(cm, false); |
| 445 | if (err) |
| 446 | return err; |
| 447 | |
| 448 | return try_charger_enable(cm, true); |
| 449 | } |
| 450 | |
| 451 | /** |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 452 | * uevent_notify - Let users know something has changed. |
| 453 | * @cm: the Charger Manager representing the battery. |
| 454 | * @event: the event string. |
| 455 | * |
| 456 | * If @event is null, it implies that uevent_notify is called |
| 457 | * by resume function. When called in the resume function, cm_suspended |
| 458 | * should be already reset to false in order to let uevent_notify |
| 459 | * notify the recent event during the suspend to users. While |
| 460 | * suspended, uevent_notify does not notify users, but tracks |
| 461 | * events so that uevent_notify can notify users later after resumed. |
| 462 | */ |
| 463 | static void uevent_notify(struct charger_manager *cm, const char *event) |
| 464 | { |
| 465 | static char env_str[UEVENT_BUF_SIZE + 1] = ""; |
| 466 | static char env_str_save[UEVENT_BUF_SIZE + 1] = ""; |
| 467 | |
| 468 | if (cm_suspended) { |
| 469 | /* Nothing in suspended-event buffer */ |
| 470 | if (env_str_save[0] == 0) { |
| 471 | if (!strncmp(env_str, event, UEVENT_BUF_SIZE)) |
| 472 | return; /* status not changed */ |
| 473 | strncpy(env_str_save, event, UEVENT_BUF_SIZE); |
| 474 | return; |
| 475 | } |
| 476 | |
| 477 | if (!strncmp(env_str_save, event, UEVENT_BUF_SIZE)) |
| 478 | return; /* Duplicated. */ |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 479 | strncpy(env_str_save, event, UEVENT_BUF_SIZE); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 480 | return; |
| 481 | } |
| 482 | |
| 483 | if (event == NULL) { |
| 484 | /* No messages pending */ |
| 485 | if (!env_str_save[0]) |
| 486 | return; |
| 487 | |
| 488 | strncpy(env_str, env_str_save, UEVENT_BUF_SIZE); |
| 489 | kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE); |
| 490 | env_str_save[0] = 0; |
| 491 | |
| 492 | return; |
| 493 | } |
| 494 | |
| 495 | /* status not changed */ |
| 496 | if (!strncmp(env_str, event, UEVENT_BUF_SIZE)) |
| 497 | return; |
| 498 | |
| 499 | /* save the status and notify the update */ |
| 500 | strncpy(env_str, event, UEVENT_BUF_SIZE); |
| 501 | kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE); |
| 502 | |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 503 | dev_info(cm->dev, "%s\n", event); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | /** |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 507 | * fullbatt_vchk - Check voltage drop some times after "FULL" event. |
| 508 | * @work: the work_struct appointing the function |
| 509 | * |
| 510 | * If a user has designated "fullbatt_vchkdrop_ms/uV" values with |
| 511 | * charger_desc, Charger Manager checks voltage drop after the battery |
| 512 | * "FULL" event. It checks whether the voltage has dropped more than |
| 513 | * fullbatt_vchkdrop_uV by calling this function after fullbatt_vchkrop_ms. |
| 514 | */ |
| 515 | static void fullbatt_vchk(struct work_struct *work) |
| 516 | { |
| 517 | struct delayed_work *dwork = to_delayed_work(work); |
| 518 | struct charger_manager *cm = container_of(dwork, |
| 519 | struct charger_manager, fullbatt_vchk_work); |
| 520 | struct charger_desc *desc = cm->desc; |
| 521 | int batt_uV, err, diff; |
| 522 | |
| 523 | /* remove the appointment for fullbatt_vchk */ |
| 524 | cm->fullbatt_vchk_jiffies_at = 0; |
| 525 | |
| 526 | if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms) |
| 527 | return; |
| 528 | |
| 529 | err = get_batt_uV(cm, &batt_uV); |
| 530 | if (err) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 531 | dev_err(cm->dev, "%s: get_batt_uV error(%d)\n", __func__, err); |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 532 | return; |
| 533 | } |
| 534 | |
Chanwoo Choi | f36b9dd | 2012-11-22 16:53:51 +0900 | [diff] [blame] | 535 | diff = desc->fullbatt_uV - batt_uV; |
| 536 | if (diff < 0) |
| 537 | return; |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 538 | |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 539 | dev_info(cm->dev, "VBATT dropped %duV after full-batt\n", diff); |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 540 | |
| 541 | if (diff > desc->fullbatt_vchkdrop_uV) { |
| 542 | try_charger_restart(cm); |
Chanwoo Choi | 8fcfe08 | 2012-09-20 21:20:05 -0700 | [diff] [blame] | 543 | uevent_notify(cm, "Recharging"); |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | |
| 547 | /** |
Chanwoo Choi | 8fcfe08 | 2012-09-20 21:20:05 -0700 | [diff] [blame] | 548 | * check_charging_duration - Monitor charging/discharging duration |
| 549 | * @cm: the Charger Manager representing the battery. |
| 550 | * |
| 551 | * If whole charging duration exceed 'charging_max_duration_ms', |
| 552 | * cm stop charging to prevent overcharge/overheat. If discharging |
| 553 | * duration exceed 'discharging _max_duration_ms', charger cable is |
| 554 | * attached, after full-batt, cm start charging to maintain fully |
| 555 | * charged state for battery. |
| 556 | */ |
| 557 | static int check_charging_duration(struct charger_manager *cm) |
| 558 | { |
| 559 | struct charger_desc *desc = cm->desc; |
| 560 | u64 curr = ktime_to_ms(ktime_get()); |
| 561 | u64 duration; |
| 562 | int ret = false; |
| 563 | |
| 564 | if (!desc->charging_max_duration_ms && |
| 565 | !desc->discharging_max_duration_ms) |
| 566 | return ret; |
| 567 | |
| 568 | if (cm->charger_enabled) { |
| 569 | duration = curr - cm->charging_start_time; |
| 570 | |
| 571 | if (duration > desc->charging_max_duration_ms) { |
Jonghwa Lee | 856ee61 | 2013-12-18 15:42:35 +0900 | [diff] [blame] | 572 | dev_info(cm->dev, "Charging duration exceed %ums\n", |
Chanwoo Choi | 8fcfe08 | 2012-09-20 21:20:05 -0700 | [diff] [blame] | 573 | desc->charging_max_duration_ms); |
| 574 | uevent_notify(cm, "Discharging"); |
| 575 | try_charger_enable(cm, false); |
| 576 | ret = true; |
| 577 | } |
| 578 | } else if (is_ext_pwr_online(cm) && !cm->charger_enabled) { |
| 579 | duration = curr - cm->charging_end_time; |
| 580 | |
| 581 | if (duration > desc->charging_max_duration_ms && |
| 582 | is_ext_pwr_online(cm)) { |
Jonghwa Lee | 856ee61 | 2013-12-18 15:42:35 +0900 | [diff] [blame] | 583 | dev_info(cm->dev, "Discharging duration exceed %ums\n", |
Chanwoo Choi | 8fcfe08 | 2012-09-20 21:20:05 -0700 | [diff] [blame] | 584 | desc->discharging_max_duration_ms); |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 585 | uevent_notify(cm, "Recharging"); |
Chanwoo Choi | 8fcfe08 | 2012-09-20 21:20:05 -0700 | [diff] [blame] | 586 | try_charger_enable(cm, true); |
| 587 | ret = true; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | return ret; |
| 592 | } |
| 593 | |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame] | 594 | static int cm_get_battery_temperature_by_psy(struct charger_manager *cm, |
| 595 | int *temp) |
| 596 | { |
| 597 | struct power_supply *fuel_gauge; |
Krzysztof Kozlowski | b43eb35 | 2015-03-12 08:44:14 +0100 | [diff] [blame] | 598 | int ret; |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame] | 599 | |
| 600 | fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge); |
| 601 | if (!fuel_gauge) |
| 602 | return -ENODEV; |
| 603 | |
Krzysztof Kozlowski | b43eb35 | 2015-03-12 08:44:14 +0100 | [diff] [blame] | 604 | ret = power_supply_get_property(fuel_gauge, |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame] | 605 | POWER_SUPPLY_PROP_TEMP, |
| 606 | (union power_supply_propval *)temp); |
Krzysztof Kozlowski | b43eb35 | 2015-03-12 08:44:14 +0100 | [diff] [blame] | 607 | power_supply_put(fuel_gauge); |
| 608 | |
| 609 | return ret; |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame] | 610 | } |
| 611 | |
Jonghwa Lee | 5c49a62 | 2013-12-18 15:42:34 +0900 | [diff] [blame] | 612 | static int cm_get_battery_temperature(struct charger_manager *cm, |
| 613 | int *temp) |
| 614 | { |
| 615 | int ret; |
| 616 | |
| 617 | if (!cm->desc->measure_battery_temp) |
| 618 | return -ENODEV; |
| 619 | |
| 620 | #ifdef CONFIG_THERMAL |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame] | 621 | if (cm->tzd_batt) { |
| 622 | ret = thermal_zone_get_temp(cm->tzd_batt, (unsigned long *)temp); |
| 623 | if (!ret) |
| 624 | /* Calibrate temperature unit */ |
| 625 | *temp /= 100; |
| 626 | } else |
Jonghwa Lee | 5c49a62 | 2013-12-18 15:42:34 +0900 | [diff] [blame] | 627 | #endif |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame] | 628 | { |
| 629 | /* if-else continued from CONFIG_THERMAL */ |
| 630 | ret = cm_get_battery_temperature_by_psy(cm, temp); |
| 631 | } |
| 632 | |
Jonghwa Lee | 5c49a62 | 2013-12-18 15:42:34 +0900 | [diff] [blame] | 633 | return ret; |
| 634 | } |
| 635 | |
| 636 | static int cm_check_thermal_status(struct charger_manager *cm) |
| 637 | { |
| 638 | struct charger_desc *desc = cm->desc; |
| 639 | int temp, upper_limit, lower_limit; |
| 640 | int ret = 0; |
| 641 | |
| 642 | ret = cm_get_battery_temperature(cm, &temp); |
| 643 | if (ret) { |
| 644 | /* FIXME: |
| 645 | * No information of battery temperature might |
| 646 | * occur hazadous result. We have to handle it |
| 647 | * depending on battery type. |
| 648 | */ |
| 649 | dev_err(cm->dev, "Failed to get battery temperature\n"); |
| 650 | return 0; |
| 651 | } |
| 652 | |
| 653 | upper_limit = desc->temp_max; |
| 654 | lower_limit = desc->temp_min; |
| 655 | |
| 656 | if (cm->emergency_stop) { |
| 657 | upper_limit -= desc->temp_diff; |
| 658 | lower_limit += desc->temp_diff; |
| 659 | } |
| 660 | |
| 661 | if (temp > upper_limit) |
| 662 | ret = CM_EVENT_BATT_OVERHEAT; |
| 663 | else if (temp < lower_limit) |
| 664 | ret = CM_EVENT_BATT_COLD; |
| 665 | |
| 666 | return ret; |
| 667 | } |
| 668 | |
Chanwoo Choi | 8fcfe08 | 2012-09-20 21:20:05 -0700 | [diff] [blame] | 669 | /** |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 670 | * _cm_monitor - Monitor the temperature and return true for exceptions. |
| 671 | * @cm: the Charger Manager representing the battery. |
| 672 | * |
| 673 | * Returns true if there is an event to notify for the battery. |
| 674 | * (True if the status of "emergency_stop" changes) |
| 675 | */ |
| 676 | static bool _cm_monitor(struct charger_manager *cm) |
| 677 | { |
Jonghwa Lee | 5c49a62 | 2013-12-18 15:42:34 +0900 | [diff] [blame] | 678 | int temp_alrt; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 679 | |
Jonghwa Lee | 5c49a62 | 2013-12-18 15:42:34 +0900 | [diff] [blame] | 680 | temp_alrt = cm_check_thermal_status(cm); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 681 | |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 682 | /* It has been stopped already */ |
Jonghwa Lee | 5c49a62 | 2013-12-18 15:42:34 +0900 | [diff] [blame] | 683 | if (temp_alrt && cm->emergency_stop) |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 684 | return false; |
| 685 | |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 686 | /* |
| 687 | * Check temperature whether overheat or cold. |
| 688 | * If temperature is out of range normal state, stop charging. |
| 689 | */ |
Jonghwa Lee | 5c49a62 | 2013-12-18 15:42:34 +0900 | [diff] [blame] | 690 | if (temp_alrt) { |
| 691 | cm->emergency_stop = temp_alrt; |
| 692 | if (!try_charger_enable(cm, false)) |
| 693 | uevent_notify(cm, default_event_names[temp_alrt]); |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 694 | |
| 695 | /* |
Chanwoo Choi | 8fcfe08 | 2012-09-20 21:20:05 -0700 | [diff] [blame] | 696 | * Check whole charging duration and discharing duration |
| 697 | * after full-batt. |
| 698 | */ |
| 699 | } else if (!cm->emergency_stop && check_charging_duration(cm)) { |
| 700 | dev_dbg(cm->dev, |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 701 | "Charging/Discharging duration is out of range\n"); |
Chanwoo Choi | 8fcfe08 | 2012-09-20 21:20:05 -0700 | [diff] [blame] | 702 | /* |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 703 | * Check dropped voltage of battery. If battery voltage is more |
| 704 | * dropped than fullbatt_vchkdrop_uV after fully charged state, |
| 705 | * charger-manager have to recharge battery. |
| 706 | */ |
| 707 | } else if (!cm->emergency_stop && is_ext_pwr_online(cm) && |
| 708 | !cm->charger_enabled) { |
| 709 | fullbatt_vchk(&cm->fullbatt_vchk_work.work); |
| 710 | |
| 711 | /* |
| 712 | * Check whether fully charged state to protect overcharge |
| 713 | * if charger-manager is charging for battery. |
| 714 | */ |
| 715 | } else if (!cm->emergency_stop && is_full_charged(cm) && |
| 716 | cm->charger_enabled) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 717 | dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged\n"); |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 718 | uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]); |
| 719 | |
| 720 | try_charger_enable(cm, false); |
| 721 | |
| 722 | fullbatt_vchk(&cm->fullbatt_vchk_work.work); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 723 | } else { |
| 724 | cm->emergency_stop = 0; |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 725 | if (is_ext_pwr_online(cm)) { |
| 726 | if (!try_charger_enable(cm, true)) |
| 727 | uevent_notify(cm, "CHARGING"); |
| 728 | } |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | return true; |
| 732 | } |
| 733 | |
| 734 | /** |
| 735 | * cm_monitor - Monitor every battery. |
| 736 | * |
| 737 | * Returns true if there is an event to notify from any of the batteries. |
| 738 | * (True if the status of "emergency_stop" changes) |
| 739 | */ |
| 740 | static bool cm_monitor(void) |
| 741 | { |
| 742 | bool stop = false; |
| 743 | struct charger_manager *cm; |
| 744 | |
| 745 | mutex_lock(&cm_list_mtx); |
| 746 | |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 747 | list_for_each_entry(cm, &cm_list, entry) { |
| 748 | if (_cm_monitor(cm)) |
| 749 | stop = true; |
| 750 | } |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 751 | |
| 752 | mutex_unlock(&cm_list_mtx); |
| 753 | |
| 754 | return stop; |
| 755 | } |
| 756 | |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 757 | /** |
| 758 | * _setup_polling - Setup the next instance of polling. |
| 759 | * @work: work_struct of the function _setup_polling. |
| 760 | */ |
| 761 | static void _setup_polling(struct work_struct *work) |
| 762 | { |
| 763 | unsigned long min = ULONG_MAX; |
| 764 | struct charger_manager *cm; |
| 765 | bool keep_polling = false; |
| 766 | unsigned long _next_polling; |
| 767 | |
| 768 | mutex_lock(&cm_list_mtx); |
| 769 | |
| 770 | list_for_each_entry(cm, &cm_list, entry) { |
| 771 | if (is_polling_required(cm) && cm->desc->polling_interval_ms) { |
| 772 | keep_polling = true; |
| 773 | |
| 774 | if (min > cm->desc->polling_interval_ms) |
| 775 | min = cm->desc->polling_interval_ms; |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | polling_jiffy = msecs_to_jiffies(min); |
| 780 | if (polling_jiffy <= CM_JIFFIES_SMALL) |
| 781 | polling_jiffy = CM_JIFFIES_SMALL + 1; |
| 782 | |
| 783 | if (!keep_polling) |
| 784 | polling_jiffy = ULONG_MAX; |
| 785 | if (polling_jiffy == ULONG_MAX) |
| 786 | goto out; |
| 787 | |
| 788 | WARN(cm_wq == NULL, "charger-manager: workqueue not initialized" |
| 789 | ". try it later. %s\n", __func__); |
| 790 | |
Tejun Heo | 2fbb520 | 2012-12-21 17:56:51 -0800 | [diff] [blame] | 791 | /* |
| 792 | * Use mod_delayed_work() iff the next polling interval should |
| 793 | * occur before the currently scheduled one. If @cm_monitor_work |
| 794 | * isn't active, the end result is the same, so no need to worry |
| 795 | * about stale @next_polling. |
| 796 | */ |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 797 | _next_polling = jiffies + polling_jiffy; |
| 798 | |
Tejun Heo | 2fbb520 | 2012-12-21 17:56:51 -0800 | [diff] [blame] | 799 | if (time_before(_next_polling, next_polling)) { |
Tejun Heo | 41f63c5 | 2012-08-03 10:30:47 -0700 | [diff] [blame] | 800 | mod_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy); |
Tejun Heo | 2fbb520 | 2012-12-21 17:56:51 -0800 | [diff] [blame] | 801 | next_polling = _next_polling; |
| 802 | } else { |
| 803 | if (queue_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy)) |
| 804 | next_polling = _next_polling; |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 805 | } |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 806 | out: |
| 807 | mutex_unlock(&cm_list_mtx); |
| 808 | } |
| 809 | static DECLARE_WORK(setup_polling, _setup_polling); |
| 810 | |
| 811 | /** |
| 812 | * cm_monitor_poller - The Monitor / Poller. |
| 813 | * @work: work_struct of the function cm_monitor_poller |
| 814 | * |
| 815 | * During non-suspended state, cm_monitor_poller is used to poll and monitor |
| 816 | * the batteries. |
| 817 | */ |
| 818 | static void cm_monitor_poller(struct work_struct *work) |
| 819 | { |
| 820 | cm_monitor(); |
| 821 | schedule_work(&setup_polling); |
| 822 | } |
| 823 | |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 824 | /** |
| 825 | * fullbatt_handler - Event handler for CM_EVENT_BATT_FULL |
| 826 | * @cm: the Charger Manager representing the battery. |
| 827 | */ |
| 828 | static void fullbatt_handler(struct charger_manager *cm) |
| 829 | { |
| 830 | struct charger_desc *desc = cm->desc; |
| 831 | |
| 832 | if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms) |
| 833 | goto out; |
| 834 | |
| 835 | if (cm_suspended) |
| 836 | device_set_wakeup_capable(cm->dev, true); |
| 837 | |
Tejun Heo | 41f63c5 | 2012-08-03 10:30:47 -0700 | [diff] [blame] | 838 | mod_delayed_work(cm_wq, &cm->fullbatt_vchk_work, |
| 839 | msecs_to_jiffies(desc->fullbatt_vchkdrop_ms)); |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 840 | cm->fullbatt_vchk_jiffies_at = jiffies + msecs_to_jiffies( |
| 841 | desc->fullbatt_vchkdrop_ms); |
| 842 | |
| 843 | if (cm->fullbatt_vchk_jiffies_at == 0) |
| 844 | cm->fullbatt_vchk_jiffies_at = 1; |
| 845 | |
| 846 | out: |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 847 | dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged\n"); |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 848 | uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]); |
| 849 | } |
| 850 | |
| 851 | /** |
| 852 | * battout_handler - Event handler for CM_EVENT_BATT_OUT |
| 853 | * @cm: the Charger Manager representing the battery. |
| 854 | */ |
| 855 | static void battout_handler(struct charger_manager *cm) |
| 856 | { |
| 857 | if (cm_suspended) |
| 858 | device_set_wakeup_capable(cm->dev, true); |
| 859 | |
| 860 | if (!is_batt_present(cm)) { |
| 861 | dev_emerg(cm->dev, "Battery Pulled Out!\n"); |
| 862 | uevent_notify(cm, default_event_names[CM_EVENT_BATT_OUT]); |
| 863 | } else { |
| 864 | uevent_notify(cm, "Battery Reinserted?"); |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | /** |
| 869 | * misc_event_handler - Handler for other evnets |
| 870 | * @cm: the Charger Manager representing the battery. |
| 871 | * @type: the Charger Manager representing the battery. |
| 872 | */ |
| 873 | static void misc_event_handler(struct charger_manager *cm, |
| 874 | enum cm_event_types type) |
| 875 | { |
| 876 | if (cm_suspended) |
| 877 | device_set_wakeup_capable(cm->dev, true); |
| 878 | |
Tejun Heo | 2fbb520 | 2012-12-21 17:56:51 -0800 | [diff] [blame] | 879 | if (is_polling_required(cm) && cm->desc->polling_interval_ms) |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 880 | schedule_work(&setup_polling); |
| 881 | uevent_notify(cm, default_event_names[type]); |
| 882 | } |
| 883 | |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 884 | static int charger_get_property(struct power_supply *psy, |
| 885 | enum power_supply_property psp, |
| 886 | union power_supply_propval *val) |
| 887 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 888 | struct charger_manager *cm = power_supply_get_drvdata(psy); |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 889 | struct charger_desc *desc = cm->desc; |
Krzysztof Kozlowski | b43eb35 | 2015-03-12 08:44:14 +0100 | [diff] [blame] | 890 | struct power_supply *fuel_gauge = NULL; |
Anton Vorontsov | df58c04 | 2012-03-15 21:01:28 +0400 | [diff] [blame] | 891 | int ret = 0; |
| 892 | int uV; |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 893 | |
| 894 | switch (psp) { |
| 895 | case POWER_SUPPLY_PROP_STATUS: |
| 896 | if (is_charging(cm)) |
| 897 | val->intval = POWER_SUPPLY_STATUS_CHARGING; |
| 898 | else if (is_ext_pwr_online(cm)) |
| 899 | val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 900 | else |
| 901 | val->intval = POWER_SUPPLY_STATUS_DISCHARGING; |
| 902 | break; |
| 903 | case POWER_SUPPLY_PROP_HEALTH: |
| 904 | if (cm->emergency_stop > 0) |
| 905 | val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; |
| 906 | else if (cm->emergency_stop < 0) |
| 907 | val->intval = POWER_SUPPLY_HEALTH_COLD; |
| 908 | else |
| 909 | val->intval = POWER_SUPPLY_HEALTH_GOOD; |
| 910 | break; |
| 911 | case POWER_SUPPLY_PROP_PRESENT: |
| 912 | if (is_batt_present(cm)) |
| 913 | val->intval = 1; |
| 914 | else |
| 915 | val->intval = 0; |
| 916 | break; |
| 917 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
Anton Vorontsov | df58c04 | 2012-03-15 21:01:28 +0400 | [diff] [blame] | 918 | ret = get_batt_uV(cm, &val->intval); |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 919 | break; |
| 920 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame] | 921 | fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge); |
| 922 | if (!fuel_gauge) { |
| 923 | ret = -ENODEV; |
| 924 | break; |
| 925 | } |
Krzysztof Kozlowski | b70229b | 2015-03-12 08:44:10 +0100 | [diff] [blame] | 926 | ret = power_supply_get_property(fuel_gauge, |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 927 | POWER_SUPPLY_PROP_CURRENT_NOW, val); |
| 928 | break; |
| 929 | case POWER_SUPPLY_PROP_TEMP: |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 930 | case POWER_SUPPLY_PROP_TEMP_AMBIENT: |
Jonghwa Lee | 5c49a62 | 2013-12-18 15:42:34 +0900 | [diff] [blame] | 931 | return cm_get_battery_temperature(cm, &val->intval); |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 932 | case POWER_SUPPLY_PROP_CAPACITY: |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 933 | if (!is_batt_present(cm)) { |
| 934 | /* There is no battery. Assume 100% */ |
| 935 | val->intval = 100; |
| 936 | break; |
| 937 | } |
| 938 | |
Krzysztof Kozlowski | b43eb35 | 2015-03-12 08:44:14 +0100 | [diff] [blame] | 939 | fuel_gauge = power_supply_get_by_name(cm->desc->psy_fuel_gauge); |
| 940 | if (!fuel_gauge) { |
| 941 | ret = -ENODEV; |
| 942 | break; |
| 943 | } |
| 944 | |
Krzysztof Kozlowski | b70229b | 2015-03-12 08:44:10 +0100 | [diff] [blame] | 945 | ret = power_supply_get_property(fuel_gauge, |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 946 | POWER_SUPPLY_PROP_CAPACITY, val); |
| 947 | if (ret) |
| 948 | break; |
| 949 | |
| 950 | if (val->intval > 100) { |
| 951 | val->intval = 100; |
| 952 | break; |
| 953 | } |
| 954 | if (val->intval < 0) |
| 955 | val->intval = 0; |
| 956 | |
| 957 | /* Do not adjust SOC when charging: voltage is overrated */ |
| 958 | if (is_charging(cm)) |
| 959 | break; |
| 960 | |
| 961 | /* |
| 962 | * If the capacity value is inconsistent, calibrate it base on |
| 963 | * the battery voltage values and the thresholds given as desc |
| 964 | */ |
| 965 | ret = get_batt_uV(cm, &uV); |
| 966 | if (ret) { |
| 967 | /* Voltage information not available. No calibration */ |
| 968 | ret = 0; |
| 969 | break; |
| 970 | } |
| 971 | |
| 972 | if (desc->fullbatt_uV > 0 && uV >= desc->fullbatt_uV && |
| 973 | !is_charging(cm)) { |
| 974 | val->intval = 100; |
| 975 | break; |
| 976 | } |
| 977 | |
| 978 | break; |
| 979 | case POWER_SUPPLY_PROP_ONLINE: |
| 980 | if (is_ext_pwr_online(cm)) |
| 981 | val->intval = 1; |
| 982 | else |
| 983 | val->intval = 0; |
| 984 | break; |
| 985 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 986 | if (is_full_charged(cm)) |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 987 | val->intval = 1; |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 988 | else |
| 989 | val->intval = 0; |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 990 | ret = 0; |
| 991 | break; |
| 992 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
| 993 | if (is_charging(cm)) { |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame] | 994 | fuel_gauge = power_supply_get_by_name( |
| 995 | cm->desc->psy_fuel_gauge); |
| 996 | if (!fuel_gauge) { |
| 997 | ret = -ENODEV; |
| 998 | break; |
| 999 | } |
| 1000 | |
Krzysztof Kozlowski | b70229b | 2015-03-12 08:44:10 +0100 | [diff] [blame] | 1001 | ret = power_supply_get_property(fuel_gauge, |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1002 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 1003 | val); |
| 1004 | if (ret) { |
| 1005 | val->intval = 1; |
| 1006 | ret = 0; |
| 1007 | } else { |
| 1008 | /* If CHARGE_NOW is supplied, use it */ |
| 1009 | val->intval = (val->intval > 0) ? |
| 1010 | val->intval : 1; |
| 1011 | } |
| 1012 | } else { |
| 1013 | val->intval = 0; |
| 1014 | } |
| 1015 | break; |
| 1016 | default: |
| 1017 | return -EINVAL; |
| 1018 | } |
Krzysztof Kozlowski | b43eb35 | 2015-03-12 08:44:14 +0100 | [diff] [blame] | 1019 | if (fuel_gauge) |
| 1020 | power_supply_put(fuel_gauge); |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1021 | return ret; |
| 1022 | } |
| 1023 | |
| 1024 | #define NUM_CHARGER_PSY_OPTIONAL (4) |
| 1025 | static enum power_supply_property default_charger_props[] = { |
| 1026 | /* Guaranteed to provide */ |
| 1027 | POWER_SUPPLY_PROP_STATUS, |
| 1028 | POWER_SUPPLY_PROP_HEALTH, |
| 1029 | POWER_SUPPLY_PROP_PRESENT, |
| 1030 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 1031 | POWER_SUPPLY_PROP_CAPACITY, |
| 1032 | POWER_SUPPLY_PROP_ONLINE, |
| 1033 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 1034 | /* |
| 1035 | * Optional properties are: |
| 1036 | * POWER_SUPPLY_PROP_CHARGE_NOW, |
| 1037 | * POWER_SUPPLY_PROP_CURRENT_NOW, |
| 1038 | * POWER_SUPPLY_PROP_TEMP, and |
| 1039 | * POWER_SUPPLY_PROP_TEMP_AMBIENT, |
| 1040 | */ |
| 1041 | }; |
| 1042 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1043 | static const struct power_supply_desc psy_default = { |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1044 | .name = "battery", |
| 1045 | .type = POWER_SUPPLY_TYPE_BATTERY, |
| 1046 | .properties = default_charger_props, |
| 1047 | .num_properties = ARRAY_SIZE(default_charger_props), |
| 1048 | .get_property = charger_get_property, |
Krzysztof Kozlowski | ba9c918 | 2014-10-07 17:47:37 +0200 | [diff] [blame] | 1049 | .no_thermal = true, |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1050 | }; |
| 1051 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1052 | /** |
| 1053 | * cm_setup_timer - For in-suspend monitoring setup wakeup alarm |
| 1054 | * for suspend_again. |
| 1055 | * |
| 1056 | * Returns true if the alarm is set for Charger Manager to use. |
| 1057 | * Returns false if |
| 1058 | * cm_setup_timer fails to set an alarm, |
| 1059 | * cm_setup_timer does not need to set an alarm for Charger Manager, |
| 1060 | * or an alarm previously configured is to be used. |
| 1061 | */ |
| 1062 | static bool cm_setup_timer(void) |
| 1063 | { |
| 1064 | struct charger_manager *cm; |
| 1065 | unsigned int wakeup_ms = UINT_MAX; |
Jonghwa Lee | c1155c6 | 2014-12-19 17:55:13 +0900 | [diff] [blame] | 1066 | int timer_req = 0; |
| 1067 | |
| 1068 | if (time_after(next_polling, jiffies)) |
| 1069 | CM_MIN_VALID(wakeup_ms, |
| 1070 | jiffies_to_msecs(next_polling - jiffies)); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1071 | |
| 1072 | mutex_lock(&cm_list_mtx); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1073 | list_for_each_entry(cm, &cm_list, entry) { |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1074 | unsigned int fbchk_ms = 0; |
| 1075 | |
| 1076 | /* fullbatt_vchk is required. setup timer for that */ |
| 1077 | if (cm->fullbatt_vchk_jiffies_at) { |
| 1078 | fbchk_ms = jiffies_to_msecs(cm->fullbatt_vchk_jiffies_at |
| 1079 | - jiffies); |
| 1080 | if (time_is_before_eq_jiffies( |
| 1081 | cm->fullbatt_vchk_jiffies_at) || |
| 1082 | msecs_to_jiffies(fbchk_ms) < CM_JIFFIES_SMALL) { |
| 1083 | fullbatt_vchk(&cm->fullbatt_vchk_work.work); |
| 1084 | fbchk_ms = 0; |
| 1085 | } |
| 1086 | } |
| 1087 | CM_MIN_VALID(wakeup_ms, fbchk_ms); |
| 1088 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1089 | /* Skip if polling is not required for this CM */ |
| 1090 | if (!is_polling_required(cm) && !cm->emergency_stop) |
| 1091 | continue; |
Jonghwa Lee | c1155c6 | 2014-12-19 17:55:13 +0900 | [diff] [blame] | 1092 | timer_req++; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1093 | if (cm->desc->polling_interval_ms == 0) |
| 1094 | continue; |
| 1095 | CM_MIN_VALID(wakeup_ms, cm->desc->polling_interval_ms); |
| 1096 | } |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1097 | mutex_unlock(&cm_list_mtx); |
| 1098 | |
Jonghwa Lee | c1155c6 | 2014-12-19 17:55:13 +0900 | [diff] [blame] | 1099 | if (timer_req && cm_timer) { |
| 1100 | ktime_t now, add; |
| 1101 | |
| 1102 | /* |
| 1103 | * Set alarm with the polling interval (wakeup_ms) |
| 1104 | * The alarm time should be NOW + CM_RTC_SMALL or later. |
| 1105 | */ |
| 1106 | if (wakeup_ms == UINT_MAX || |
| 1107 | wakeup_ms < CM_RTC_SMALL * MSEC_PER_SEC) |
| 1108 | wakeup_ms = 2 * CM_RTC_SMALL * MSEC_PER_SEC; |
| 1109 | |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1110 | pr_info("Charger Manager wakeup timer: %u ms\n", wakeup_ms); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1111 | |
Jonghwa Lee | c1155c6 | 2014-12-19 17:55:13 +0900 | [diff] [blame] | 1112 | now = ktime_get_boottime(); |
| 1113 | add = ktime_set(wakeup_ms / MSEC_PER_SEC, |
| 1114 | (wakeup_ms % MSEC_PER_SEC) * NSEC_PER_MSEC); |
| 1115 | alarm_start(cm_timer, ktime_add(now, add)); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1116 | |
Jonghwa Lee | c1155c6 | 2014-12-19 17:55:13 +0900 | [diff] [blame] | 1117 | cm_suspend_duration_ms = wakeup_ms; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1118 | |
Jonghwa Lee | c1155c6 | 2014-12-19 17:55:13 +0900 | [diff] [blame] | 1119 | return true; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1120 | } |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1121 | return false; |
| 1122 | } |
| 1123 | |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1124 | /** |
| 1125 | * charger_extcon_work - enable/diable charger according to the state |
| 1126 | * of charger cable |
| 1127 | * |
| 1128 | * @work: work_struct of the function charger_extcon_work. |
| 1129 | */ |
| 1130 | static void charger_extcon_work(struct work_struct *work) |
| 1131 | { |
| 1132 | struct charger_cable *cable = |
| 1133 | container_of(work, struct charger_cable, wq); |
Chanwoo Choi | 45cd4fb | 2012-07-12 15:03:29 +0900 | [diff] [blame] | 1134 | int ret; |
| 1135 | |
| 1136 | if (cable->attached && cable->min_uA != 0 && cable->max_uA != 0) { |
| 1137 | ret = regulator_set_current_limit(cable->charger->consumer, |
| 1138 | cable->min_uA, cable->max_uA); |
| 1139 | if (ret < 0) { |
| 1140 | pr_err("Cannot set current limit of %s (%s)\n", |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1141 | cable->charger->regulator_name, cable->name); |
Chanwoo Choi | 45cd4fb | 2012-07-12 15:03:29 +0900 | [diff] [blame] | 1142 | return; |
| 1143 | } |
| 1144 | |
| 1145 | pr_info("Set current limit of %s : %duA ~ %duA\n", |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1146 | cable->charger->regulator_name, |
| 1147 | cable->min_uA, cable->max_uA); |
Chanwoo Choi | 45cd4fb | 2012-07-12 15:03:29 +0900 | [diff] [blame] | 1148 | } |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1149 | |
| 1150 | try_charger_enable(cable->cm, cable->attached); |
| 1151 | } |
| 1152 | |
| 1153 | /** |
| 1154 | * charger_extcon_notifier - receive the state of charger cable |
| 1155 | * when registered cable is attached or detached. |
| 1156 | * |
| 1157 | * @self: the notifier block of the charger_extcon_notifier. |
| 1158 | * @event: the cable state. |
| 1159 | * @ptr: the data pointer of notifier block. |
| 1160 | */ |
| 1161 | static int charger_extcon_notifier(struct notifier_block *self, |
| 1162 | unsigned long event, void *ptr) |
| 1163 | { |
| 1164 | struct charger_cable *cable = |
| 1165 | container_of(self, struct charger_cable, nb); |
| 1166 | |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 1167 | /* |
| 1168 | * The newly state of charger cable. |
| 1169 | * If cable is attached, cable->attached is true. |
| 1170 | */ |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1171 | cable->attached = event; |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 1172 | |
| 1173 | /* |
| 1174 | * Setup monitoring to check battery state |
| 1175 | * when charger cable is attached. |
| 1176 | */ |
| 1177 | if (cable->attached && is_polling_required(cable->cm)) { |
Tejun Heo | 2fbb520 | 2012-12-21 17:56:51 -0800 | [diff] [blame] | 1178 | cancel_work_sync(&setup_polling); |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 1179 | schedule_work(&setup_polling); |
| 1180 | } |
| 1181 | |
| 1182 | /* |
| 1183 | * Setup work for controlling charger(regulator) |
| 1184 | * according to charger cable. |
| 1185 | */ |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1186 | schedule_work(&cable->wq); |
| 1187 | |
| 1188 | return NOTIFY_DONE; |
| 1189 | } |
| 1190 | |
| 1191 | /** |
| 1192 | * charger_extcon_init - register external connector to use it |
| 1193 | * as the charger cable |
| 1194 | * |
| 1195 | * @cm: the Charger Manager representing the battery. |
| 1196 | * @cable: the Charger cable representing the external connector. |
| 1197 | */ |
| 1198 | static int charger_extcon_init(struct charger_manager *cm, |
| 1199 | struct charger_cable *cable) |
| 1200 | { |
| 1201 | int ret = 0; |
| 1202 | |
| 1203 | /* |
| 1204 | * Charger manager use Extcon framework to identify |
| 1205 | * the charger cable among various external connector |
| 1206 | * cable (e.g., TA, USB, MHL, Dock). |
| 1207 | */ |
| 1208 | INIT_WORK(&cable->wq, charger_extcon_work); |
| 1209 | cable->nb.notifier_call = charger_extcon_notifier; |
| 1210 | ret = extcon_register_interest(&cable->extcon_dev, |
| 1211 | cable->extcon_name, cable->name, &cable->nb); |
| 1212 | if (ret < 0) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1213 | pr_info("Cannot register extcon_dev for %s(cable: %s)\n", |
| 1214 | cable->extcon_name, cable->name); |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1215 | ret = -EINVAL; |
| 1216 | } |
| 1217 | |
| 1218 | return ret; |
| 1219 | } |
| 1220 | |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1221 | /** |
| 1222 | * charger_manager_register_extcon - Register extcon device to recevie state |
| 1223 | * of charger cable. |
| 1224 | * @cm: the Charger Manager representing the battery. |
| 1225 | * |
| 1226 | * This function support EXTCON(External Connector) subsystem to detect the |
| 1227 | * state of charger cables for enabling or disabling charger(regulator) and |
| 1228 | * select the charger cable for charging among a number of external cable |
| 1229 | * according to policy of H/W board. |
| 1230 | */ |
| 1231 | static int charger_manager_register_extcon(struct charger_manager *cm) |
| 1232 | { |
| 1233 | struct charger_desc *desc = cm->desc; |
| 1234 | struct charger_regulator *charger; |
| 1235 | int ret = 0; |
| 1236 | int i; |
| 1237 | int j; |
| 1238 | |
| 1239 | for (i = 0; i < desc->num_charger_regulators; i++) { |
| 1240 | charger = &desc->charger_regulators[i]; |
| 1241 | |
| 1242 | charger->consumer = regulator_get(cm->dev, |
| 1243 | charger->regulator_name); |
Jonghwa Lee | 5a6c220 | 2013-06-25 14:18:14 +0900 | [diff] [blame] | 1244 | if (IS_ERR(charger->consumer)) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1245 | dev_err(cm->dev, "Cannot find charger(%s)\n", |
| 1246 | charger->regulator_name); |
Jonghwa Lee | 5a6c220 | 2013-06-25 14:18:14 +0900 | [diff] [blame] | 1247 | return PTR_ERR(charger->consumer); |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1248 | } |
| 1249 | charger->cm = cm; |
| 1250 | |
| 1251 | for (j = 0; j < charger->num_cables; j++) { |
| 1252 | struct charger_cable *cable = &charger->cables[j]; |
| 1253 | |
| 1254 | ret = charger_extcon_init(cm, cable); |
| 1255 | if (ret < 0) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1256 | dev_err(cm->dev, "Cannot initialize charger(%s)\n", |
| 1257 | charger->regulator_name); |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1258 | goto err; |
| 1259 | } |
| 1260 | cable->charger = charger; |
| 1261 | cable->cm = cm; |
| 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | err: |
| 1266 | return ret; |
| 1267 | } |
| 1268 | |
Chanwoo Choi | 3950c78 | 2012-09-21 18:49:37 +0900 | [diff] [blame] | 1269 | /* help function of sysfs node to control charger(regulator) */ |
| 1270 | static ssize_t charger_name_show(struct device *dev, |
| 1271 | struct device_attribute *attr, char *buf) |
| 1272 | { |
| 1273 | struct charger_regulator *charger |
| 1274 | = container_of(attr, struct charger_regulator, attr_name); |
| 1275 | |
| 1276 | return sprintf(buf, "%s\n", charger->regulator_name); |
| 1277 | } |
| 1278 | |
| 1279 | static ssize_t charger_state_show(struct device *dev, |
| 1280 | struct device_attribute *attr, char *buf) |
| 1281 | { |
| 1282 | struct charger_regulator *charger |
| 1283 | = container_of(attr, struct charger_regulator, attr_state); |
| 1284 | int state = 0; |
| 1285 | |
| 1286 | if (!charger->externally_control) |
| 1287 | state = regulator_is_enabled(charger->consumer); |
| 1288 | |
| 1289 | return sprintf(buf, "%s\n", state ? "enabled" : "disabled"); |
| 1290 | } |
| 1291 | |
| 1292 | static ssize_t charger_externally_control_show(struct device *dev, |
| 1293 | struct device_attribute *attr, char *buf) |
| 1294 | { |
| 1295 | struct charger_regulator *charger = container_of(attr, |
| 1296 | struct charger_regulator, attr_externally_control); |
| 1297 | |
| 1298 | return sprintf(buf, "%d\n", charger->externally_control); |
| 1299 | } |
| 1300 | |
| 1301 | static ssize_t charger_externally_control_store(struct device *dev, |
| 1302 | struct device_attribute *attr, const char *buf, |
| 1303 | size_t count) |
| 1304 | { |
| 1305 | struct charger_regulator *charger |
| 1306 | = container_of(attr, struct charger_regulator, |
| 1307 | attr_externally_control); |
| 1308 | struct charger_manager *cm = charger->cm; |
| 1309 | struct charger_desc *desc = cm->desc; |
| 1310 | int i; |
| 1311 | int ret; |
| 1312 | int externally_control; |
| 1313 | int chargers_externally_control = 1; |
| 1314 | |
| 1315 | ret = sscanf(buf, "%d", &externally_control); |
| 1316 | if (ret == 0) { |
| 1317 | ret = -EINVAL; |
| 1318 | return ret; |
| 1319 | } |
| 1320 | |
| 1321 | if (!externally_control) { |
| 1322 | charger->externally_control = 0; |
| 1323 | return count; |
| 1324 | } |
| 1325 | |
| 1326 | for (i = 0; i < desc->num_charger_regulators; i++) { |
| 1327 | if (&desc->charger_regulators[i] != charger && |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1328 | !desc->charger_regulators[i].externally_control) { |
Chanwoo Choi | 3950c78 | 2012-09-21 18:49:37 +0900 | [diff] [blame] | 1329 | /* |
| 1330 | * At least, one charger is controlled by |
| 1331 | * charger-manager |
| 1332 | */ |
| 1333 | chargers_externally_control = 0; |
| 1334 | break; |
| 1335 | } |
| 1336 | } |
| 1337 | |
| 1338 | if (!chargers_externally_control) { |
| 1339 | if (cm->charger_enabled) { |
| 1340 | try_charger_enable(charger->cm, false); |
| 1341 | charger->externally_control = externally_control; |
| 1342 | try_charger_enable(charger->cm, true); |
| 1343 | } else { |
| 1344 | charger->externally_control = externally_control; |
| 1345 | } |
| 1346 | } else { |
| 1347 | dev_warn(cm->dev, |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1348 | "'%s' regulator should be controlled in charger-manager because charger-manager must need at least one charger for charging\n", |
| 1349 | charger->regulator_name); |
Chanwoo Choi | 3950c78 | 2012-09-21 18:49:37 +0900 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | return count; |
| 1353 | } |
| 1354 | |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1355 | /** |
| 1356 | * charger_manager_register_sysfs - Register sysfs entry for each charger |
| 1357 | * @cm: the Charger Manager representing the battery. |
| 1358 | * |
| 1359 | * This function add sysfs entry for charger(regulator) to control charger from |
| 1360 | * user-space. If some development board use one more chargers for charging |
| 1361 | * but only need one charger on specific case which is dependent on user |
| 1362 | * scenario or hardware restrictions, the user enter 1 or 0(zero) to '/sys/ |
| 1363 | * class/power_supply/battery/charger.[index]/externally_control'. For example, |
| 1364 | * if user enter 1 to 'sys/class/power_supply/battery/charger.[index]/ |
| 1365 | * externally_control, this charger isn't controlled from charger-manager and |
| 1366 | * always stay off state of regulator. |
| 1367 | */ |
| 1368 | static int charger_manager_register_sysfs(struct charger_manager *cm) |
| 1369 | { |
| 1370 | struct charger_desc *desc = cm->desc; |
| 1371 | struct charger_regulator *charger; |
| 1372 | int chargers_externally_control = 1; |
| 1373 | char buf[11]; |
| 1374 | char *str; |
| 1375 | int ret = 0; |
| 1376 | int i; |
| 1377 | |
| 1378 | /* Create sysfs entry to control charger(regulator) */ |
| 1379 | for (i = 0; i < desc->num_charger_regulators; i++) { |
| 1380 | charger = &desc->charger_regulators[i]; |
| 1381 | |
| 1382 | snprintf(buf, 10, "charger.%d", i); |
Jonghwa Lee | 883c10a | 2013-10-25 11:47:31 +0900 | [diff] [blame] | 1383 | str = devm_kzalloc(cm->dev, |
| 1384 | sizeof(char) * (strlen(buf) + 1), GFP_KERNEL); |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1385 | if (!str) { |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1386 | ret = -ENOMEM; |
| 1387 | goto err; |
| 1388 | } |
| 1389 | strcpy(str, buf); |
| 1390 | |
| 1391 | charger->attrs[0] = &charger->attr_name.attr; |
| 1392 | charger->attrs[1] = &charger->attr_state.attr; |
| 1393 | charger->attrs[2] = &charger->attr_externally_control.attr; |
| 1394 | charger->attrs[3] = NULL; |
| 1395 | charger->attr_g.name = str; |
| 1396 | charger->attr_g.attrs = charger->attrs; |
| 1397 | |
| 1398 | sysfs_attr_init(&charger->attr_name.attr); |
| 1399 | charger->attr_name.attr.name = "name"; |
| 1400 | charger->attr_name.attr.mode = 0444; |
| 1401 | charger->attr_name.show = charger_name_show; |
| 1402 | |
| 1403 | sysfs_attr_init(&charger->attr_state.attr); |
| 1404 | charger->attr_state.attr.name = "state"; |
| 1405 | charger->attr_state.attr.mode = 0444; |
| 1406 | charger->attr_state.show = charger_state_show; |
| 1407 | |
| 1408 | sysfs_attr_init(&charger->attr_externally_control.attr); |
| 1409 | charger->attr_externally_control.attr.name |
| 1410 | = "externally_control"; |
| 1411 | charger->attr_externally_control.attr.mode = 0644; |
| 1412 | charger->attr_externally_control.show |
| 1413 | = charger_externally_control_show; |
| 1414 | charger->attr_externally_control.store |
| 1415 | = charger_externally_control_store; |
| 1416 | |
| 1417 | if (!desc->charger_regulators[i].externally_control || |
| 1418 | !chargers_externally_control) |
| 1419 | chargers_externally_control = 0; |
| 1420 | |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1421 | dev_info(cm->dev, "'%s' regulator's externally_control is %d\n", |
| 1422 | charger->regulator_name, charger->externally_control); |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1423 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1424 | ret = sysfs_create_group(&cm->charger_psy->dev.kobj, |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1425 | &charger->attr_g); |
| 1426 | if (ret < 0) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1427 | dev_err(cm->dev, "Cannot create sysfs entry of %s regulator\n", |
| 1428 | charger->regulator_name); |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1429 | ret = -EINVAL; |
| 1430 | goto err; |
| 1431 | } |
| 1432 | } |
| 1433 | |
| 1434 | if (chargers_externally_control) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1435 | dev_err(cm->dev, "Cannot register regulator because charger-manager must need at least one charger for charging battery\n"); |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1436 | ret = -EINVAL; |
| 1437 | goto err; |
| 1438 | } |
| 1439 | |
| 1440 | err: |
| 1441 | return ret; |
| 1442 | } |
| 1443 | |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame] | 1444 | static int cm_init_thermal_data(struct charger_manager *cm, |
| 1445 | struct power_supply *fuel_gauge) |
Jonghwa Lee | 5c49a62 | 2013-12-18 15:42:34 +0900 | [diff] [blame] | 1446 | { |
| 1447 | struct charger_desc *desc = cm->desc; |
| 1448 | union power_supply_propval val; |
| 1449 | int ret; |
| 1450 | |
| 1451 | /* Verify whether fuel gauge provides battery temperature */ |
Krzysztof Kozlowski | b70229b | 2015-03-12 08:44:10 +0100 | [diff] [blame] | 1452 | ret = power_supply_get_property(fuel_gauge, |
Jonghwa Lee | 5c49a62 | 2013-12-18 15:42:34 +0900 | [diff] [blame] | 1453 | POWER_SUPPLY_PROP_TEMP, &val); |
| 1454 | |
| 1455 | if (!ret) { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1456 | cm->charger_psy_desc.properties[cm->charger_psy_desc.num_properties] = |
Jonghwa Lee | 5c49a62 | 2013-12-18 15:42:34 +0900 | [diff] [blame] | 1457 | POWER_SUPPLY_PROP_TEMP; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1458 | cm->charger_psy_desc.num_properties++; |
Jonghwa Lee | 5c49a62 | 2013-12-18 15:42:34 +0900 | [diff] [blame] | 1459 | cm->desc->measure_battery_temp = true; |
| 1460 | } |
| 1461 | #ifdef CONFIG_THERMAL |
Jonghwa Lee | 5c49a62 | 2013-12-18 15:42:34 +0900 | [diff] [blame] | 1462 | if (ret && desc->thermal_zone) { |
| 1463 | cm->tzd_batt = |
| 1464 | thermal_zone_get_zone_by_name(desc->thermal_zone); |
| 1465 | if (IS_ERR(cm->tzd_batt)) |
| 1466 | return PTR_ERR(cm->tzd_batt); |
| 1467 | |
| 1468 | /* Use external thermometer */ |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1469 | cm->charger_psy_desc.properties[cm->charger_psy_desc.num_properties] = |
Jonghwa Lee | 5c49a62 | 2013-12-18 15:42:34 +0900 | [diff] [blame] | 1470 | POWER_SUPPLY_PROP_TEMP_AMBIENT; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1471 | cm->charger_psy_desc.num_properties++; |
Jonghwa Lee | 5c49a62 | 2013-12-18 15:42:34 +0900 | [diff] [blame] | 1472 | cm->desc->measure_battery_temp = true; |
| 1473 | ret = 0; |
| 1474 | } |
| 1475 | #endif |
| 1476 | if (cm->desc->measure_battery_temp) { |
| 1477 | /* NOTICE : Default allowable minimum charge temperature is 0 */ |
| 1478 | if (!desc->temp_max) |
| 1479 | desc->temp_max = CM_DEFAULT_CHARGE_TEMP_MAX; |
| 1480 | if (!desc->temp_diff) |
| 1481 | desc->temp_diff = CM_DEFAULT_RECHARGE_TEMP_DIFF; |
| 1482 | } |
| 1483 | |
| 1484 | return ret; |
| 1485 | } |
| 1486 | |
Fabian Frederick | 8fb0885 | 2015-03-16 20:17:12 +0100 | [diff] [blame] | 1487 | static const struct of_device_id charger_manager_match[] = { |
Jonghwa Lee | 856ee61 | 2013-12-18 15:42:35 +0900 | [diff] [blame] | 1488 | { |
| 1489 | .compatible = "charger-manager", |
| 1490 | }, |
| 1491 | {}, |
| 1492 | }; |
| 1493 | |
Anton Vorontsov | 434a09f | 2013-12-23 18:17:05 -0800 | [diff] [blame] | 1494 | static struct charger_desc *of_cm_parse_desc(struct device *dev) |
Jonghwa Lee | 856ee61 | 2013-12-18 15:42:35 +0900 | [diff] [blame] | 1495 | { |
| 1496 | struct charger_desc *desc; |
| 1497 | struct device_node *np = dev->of_node; |
| 1498 | u32 poll_mode = CM_POLL_DISABLE; |
| 1499 | u32 battery_stat = CM_NO_BATTERY; |
| 1500 | int num_chgs = 0; |
| 1501 | |
| 1502 | desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL); |
| 1503 | if (!desc) |
| 1504 | return ERR_PTR(-ENOMEM); |
| 1505 | |
| 1506 | of_property_read_string(np, "cm-name", &desc->psy_name); |
| 1507 | |
| 1508 | of_property_read_u32(np, "cm-poll-mode", &poll_mode); |
| 1509 | desc->polling_mode = poll_mode; |
| 1510 | |
| 1511 | of_property_read_u32(np, "cm-poll-interval", |
| 1512 | &desc->polling_interval_ms); |
| 1513 | |
| 1514 | of_property_read_u32(np, "cm-fullbatt-vchkdrop-ms", |
| 1515 | &desc->fullbatt_vchkdrop_ms); |
| 1516 | of_property_read_u32(np, "cm-fullbatt-vchkdrop-volt", |
| 1517 | &desc->fullbatt_vchkdrop_uV); |
| 1518 | of_property_read_u32(np, "cm-fullbatt-voltage", &desc->fullbatt_uV); |
| 1519 | of_property_read_u32(np, "cm-fullbatt-soc", &desc->fullbatt_soc); |
| 1520 | of_property_read_u32(np, "cm-fullbatt-capacity", |
| 1521 | &desc->fullbatt_full_capacity); |
| 1522 | |
| 1523 | of_property_read_u32(np, "cm-battery-stat", &battery_stat); |
| 1524 | desc->battery_present = battery_stat; |
| 1525 | |
| 1526 | /* chargers */ |
| 1527 | of_property_read_u32(np, "cm-num-chargers", &num_chgs); |
| 1528 | if (num_chgs) { |
| 1529 | /* Allocate empty bin at the tail of array */ |
| 1530 | desc->psy_charger_stat = devm_kzalloc(dev, sizeof(char *) |
| 1531 | * (num_chgs + 1), GFP_KERNEL); |
| 1532 | if (desc->psy_charger_stat) { |
| 1533 | int i; |
| 1534 | for (i = 0; i < num_chgs; i++) |
| 1535 | of_property_read_string_index(np, "cm-chargers", |
| 1536 | i, &desc->psy_charger_stat[i]); |
| 1537 | } else { |
| 1538 | return ERR_PTR(-ENOMEM); |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | of_property_read_string(np, "cm-fuel-gauge", &desc->psy_fuel_gauge); |
| 1543 | |
| 1544 | of_property_read_string(np, "cm-thermal-zone", &desc->thermal_zone); |
| 1545 | |
| 1546 | of_property_read_u32(np, "cm-battery-cold", &desc->temp_min); |
| 1547 | if (of_get_property(np, "cm-battery-cold-in-minus", NULL)) |
| 1548 | desc->temp_min *= -1; |
| 1549 | of_property_read_u32(np, "cm-battery-hot", &desc->temp_max); |
| 1550 | of_property_read_u32(np, "cm-battery-temp-diff", &desc->temp_diff); |
| 1551 | |
| 1552 | of_property_read_u32(np, "cm-charging-max", |
| 1553 | &desc->charging_max_duration_ms); |
| 1554 | of_property_read_u32(np, "cm-discharging-max", |
| 1555 | &desc->discharging_max_duration_ms); |
| 1556 | |
| 1557 | /* battery charger regualtors */ |
| 1558 | desc->num_charger_regulators = of_get_child_count(np); |
| 1559 | if (desc->num_charger_regulators) { |
| 1560 | struct charger_regulator *chg_regs; |
| 1561 | struct device_node *child; |
| 1562 | |
| 1563 | chg_regs = devm_kzalloc(dev, sizeof(*chg_regs) |
| 1564 | * desc->num_charger_regulators, |
| 1565 | GFP_KERNEL); |
| 1566 | if (!chg_regs) |
| 1567 | return ERR_PTR(-ENOMEM); |
| 1568 | |
| 1569 | desc->charger_regulators = chg_regs; |
| 1570 | |
| 1571 | for_each_child_of_node(np, child) { |
| 1572 | struct charger_cable *cables; |
| 1573 | struct device_node *_child; |
| 1574 | |
| 1575 | of_property_read_string(child, "cm-regulator-name", |
| 1576 | &chg_regs->regulator_name); |
| 1577 | |
| 1578 | /* charger cables */ |
| 1579 | chg_regs->num_cables = of_get_child_count(child); |
| 1580 | if (chg_regs->num_cables) { |
| 1581 | cables = devm_kzalloc(dev, sizeof(*cables) |
| 1582 | * chg_regs->num_cables, |
| 1583 | GFP_KERNEL); |
| 1584 | if (!cables) |
| 1585 | return ERR_PTR(-ENOMEM); |
| 1586 | |
| 1587 | chg_regs->cables = cables; |
| 1588 | |
| 1589 | for_each_child_of_node(child, _child) { |
| 1590 | of_property_read_string(_child, |
| 1591 | "cm-cable-name", &cables->name); |
| 1592 | of_property_read_string(_child, |
| 1593 | "cm-cable-extcon", |
| 1594 | &cables->extcon_name); |
| 1595 | of_property_read_u32(_child, |
| 1596 | "cm-cable-min", |
| 1597 | &cables->min_uA); |
| 1598 | of_property_read_u32(_child, |
| 1599 | "cm-cable-max", |
| 1600 | &cables->max_uA); |
| 1601 | cables++; |
| 1602 | } |
| 1603 | } |
| 1604 | chg_regs++; |
| 1605 | } |
| 1606 | } |
| 1607 | return desc; |
| 1608 | } |
| 1609 | |
| 1610 | static inline struct charger_desc *cm_get_drv_data(struct platform_device *pdev) |
| 1611 | { |
| 1612 | if (pdev->dev.of_node) |
| 1613 | return of_cm_parse_desc(&pdev->dev); |
Jingoo Han | 86515b7 | 2014-08-29 12:45:27 +0900 | [diff] [blame] | 1614 | return dev_get_platdata(&pdev->dev); |
Jonghwa Lee | 856ee61 | 2013-12-18 15:42:35 +0900 | [diff] [blame] | 1615 | } |
| 1616 | |
Jonghwa Lee | c1155c6 | 2014-12-19 17:55:13 +0900 | [diff] [blame] | 1617 | static enum alarmtimer_restart cm_timer_func(struct alarm *alarm, ktime_t now) |
| 1618 | { |
| 1619 | cm_timer_set = false; |
| 1620 | return ALARMTIMER_NORESTART; |
| 1621 | } |
| 1622 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1623 | static int charger_manager_probe(struct platform_device *pdev) |
| 1624 | { |
Jonghwa Lee | 856ee61 | 2013-12-18 15:42:35 +0900 | [diff] [blame] | 1625 | struct charger_desc *desc = cm_get_drv_data(pdev); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1626 | struct charger_manager *cm; |
| 1627 | int ret = 0, i = 0; |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1628 | int j = 0; |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1629 | union power_supply_propval val; |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame] | 1630 | struct power_supply *fuel_gauge; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1631 | struct power_supply_config psy_cfg = {}; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1632 | |
Chanwoo Choi | c6738d0 | 2014-08-26 13:41:38 +0900 | [diff] [blame] | 1633 | if (IS_ERR(desc)) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1634 | dev_err(&pdev->dev, "No platform data (desc) found\n"); |
Jonghwa Lee | 883c10a | 2013-10-25 11:47:31 +0900 | [diff] [blame] | 1635 | return -ENODEV; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1636 | } |
| 1637 | |
Jonghwa Lee | 883c10a | 2013-10-25 11:47:31 +0900 | [diff] [blame] | 1638 | cm = devm_kzalloc(&pdev->dev, |
| 1639 | sizeof(struct charger_manager), GFP_KERNEL); |
| 1640 | if (!cm) |
| 1641 | return -ENOMEM; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1642 | |
| 1643 | /* Basic Values. Unspecified are Null or 0 */ |
| 1644 | cm->dev = &pdev->dev; |
Jonghwa Lee | 883c10a | 2013-10-25 11:47:31 +0900 | [diff] [blame] | 1645 | cm->desc = desc; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1646 | psy_cfg.drv_data = cm; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1647 | |
Jonghwa Lee | c1155c6 | 2014-12-19 17:55:13 +0900 | [diff] [blame] | 1648 | /* Initialize alarm timer */ |
| 1649 | if (alarmtimer_get_rtcdev()) { |
| 1650 | cm_timer = devm_kzalloc(cm->dev, sizeof(*cm_timer), GFP_KERNEL); |
| 1651 | alarm_init(cm_timer, ALARM_BOOTTIME, cm_timer_func); |
| 1652 | } |
| 1653 | |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1654 | /* |
| 1655 | * The following two do not need to be errors. |
| 1656 | * Users may intentionally ignore those two features. |
| 1657 | */ |
| 1658 | if (desc->fullbatt_uV == 0) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1659 | dev_info(&pdev->dev, "Ignoring full-battery voltage threshold as it is not supplied\n"); |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1660 | } |
| 1661 | if (!desc->fullbatt_vchkdrop_ms || !desc->fullbatt_vchkdrop_uV) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1662 | dev_info(&pdev->dev, "Disabling full-battery voltage drop checking mechanism as it is not supplied\n"); |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1663 | desc->fullbatt_vchkdrop_ms = 0; |
| 1664 | desc->fullbatt_vchkdrop_uV = 0; |
| 1665 | } |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 1666 | if (desc->fullbatt_soc == 0) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1667 | dev_info(&pdev->dev, "Ignoring full-battery soc(state of charge) threshold as it is not supplied\n"); |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 1668 | } |
| 1669 | if (desc->fullbatt_full_capacity == 0) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1670 | dev_info(&pdev->dev, "Ignoring full-battery full capacity threshold as it is not supplied\n"); |
Chanwoo Choi | 2ed9e9b | 2012-08-21 17:06:52 +0900 | [diff] [blame] | 1671 | } |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1672 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1673 | if (!desc->charger_regulators || desc->num_charger_regulators < 1) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1674 | dev_err(&pdev->dev, "charger_regulators undefined\n"); |
Jonghwa Lee | 883c10a | 2013-10-25 11:47:31 +0900 | [diff] [blame] | 1675 | return -EINVAL; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1676 | } |
| 1677 | |
| 1678 | if (!desc->psy_charger_stat || !desc->psy_charger_stat[0]) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1679 | dev_err(&pdev->dev, "No power supply defined\n"); |
Jonghwa Lee | 883c10a | 2013-10-25 11:47:31 +0900 | [diff] [blame] | 1680 | return -EINVAL; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1681 | } |
| 1682 | |
Krzysztof Kozlowski | 661a888 | 2014-09-26 13:27:03 +0200 | [diff] [blame] | 1683 | if (!desc->psy_fuel_gauge) { |
| 1684 | dev_err(&pdev->dev, "No fuel gauge power supply defined\n"); |
| 1685 | return -EINVAL; |
| 1686 | } |
| 1687 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1688 | /* Counting index only */ |
| 1689 | while (desc->psy_charger_stat[i]) |
| 1690 | i++; |
| 1691 | |
Krzysztof Kozlowski | cdaf3e1 | 2014-10-13 15:34:31 +0200 | [diff] [blame] | 1692 | /* Check if charger's supplies are present at probe */ |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1693 | for (i = 0; desc->psy_charger_stat[i]; i++) { |
Krzysztof Kozlowski | cdaf3e1 | 2014-10-13 15:34:31 +0200 | [diff] [blame] | 1694 | struct power_supply *psy; |
| 1695 | |
| 1696 | psy = power_supply_get_by_name(desc->psy_charger_stat[i]); |
| 1697 | if (!psy) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1698 | dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n", |
| 1699 | desc->psy_charger_stat[i]); |
Jonghwa Lee | 883c10a | 2013-10-25 11:47:31 +0900 | [diff] [blame] | 1700 | return -ENODEV; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1701 | } |
Krzysztof Kozlowski | b43eb35 | 2015-03-12 08:44:14 +0100 | [diff] [blame] | 1702 | power_supply_put(psy); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | if (desc->polling_interval_ms == 0 || |
| 1706 | msecs_to_jiffies(desc->polling_interval_ms) <= CM_JIFFIES_SMALL) { |
| 1707 | dev_err(&pdev->dev, "polling_interval_ms is too small\n"); |
Jonghwa Lee | 883c10a | 2013-10-25 11:47:31 +0900 | [diff] [blame] | 1708 | return -EINVAL; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1709 | } |
| 1710 | |
Chanwoo Choi | 8fcfe08 | 2012-09-20 21:20:05 -0700 | [diff] [blame] | 1711 | if (!desc->charging_max_duration_ms || |
| 1712 | !desc->discharging_max_duration_ms) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1713 | dev_info(&pdev->dev, "Cannot limit charging duration checking mechanism to prevent overcharge/overheat and control discharging duration\n"); |
Chanwoo Choi | 8fcfe08 | 2012-09-20 21:20:05 -0700 | [diff] [blame] | 1714 | desc->charging_max_duration_ms = 0; |
| 1715 | desc->discharging_max_duration_ms = 0; |
| 1716 | } |
| 1717 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1718 | platform_set_drvdata(pdev, cm); |
| 1719 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1720 | memcpy(&cm->charger_psy_desc, &psy_default, sizeof(psy_default)); |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 1721 | |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1722 | if (!desc->psy_name) |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 1723 | strncpy(cm->psy_name_buf, psy_default.name, PSY_NAME_MAX); |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1724 | else |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1725 | strncpy(cm->psy_name_buf, desc->psy_name, PSY_NAME_MAX); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1726 | cm->charger_psy_desc.name = cm->psy_name_buf; |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1727 | |
| 1728 | /* Allocate for psy properties because they may vary */ |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1729 | cm->charger_psy_desc.properties = devm_kzalloc(&pdev->dev, |
Jonghwa Lee | 883c10a | 2013-10-25 11:47:31 +0900 | [diff] [blame] | 1730 | sizeof(enum power_supply_property) |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1731 | * (ARRAY_SIZE(default_charger_props) + |
Jonghwa Lee | 883c10a | 2013-10-25 11:47:31 +0900 | [diff] [blame] | 1732 | NUM_CHARGER_PSY_OPTIONAL), GFP_KERNEL); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1733 | if (!cm->charger_psy_desc.properties) |
Jonghwa Lee | 883c10a | 2013-10-25 11:47:31 +0900 | [diff] [blame] | 1734 | return -ENOMEM; |
| 1735 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1736 | memcpy(cm->charger_psy_desc.properties, default_charger_props, |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1737 | sizeof(enum power_supply_property) * |
| 1738 | ARRAY_SIZE(default_charger_props)); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1739 | cm->charger_psy_desc.num_properties = psy_default.num_properties; |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1740 | |
| 1741 | /* Find which optional psy-properties are available */ |
Krzysztof Kozlowski | b43eb35 | 2015-03-12 08:44:14 +0100 | [diff] [blame] | 1742 | fuel_gauge = power_supply_get_by_name(desc->psy_fuel_gauge); |
| 1743 | if (!fuel_gauge) { |
| 1744 | dev_err(&pdev->dev, "Cannot find power supply \"%s\"\n", |
| 1745 | desc->psy_fuel_gauge); |
| 1746 | return -ENODEV; |
| 1747 | } |
Krzysztof Kozlowski | b70229b | 2015-03-12 08:44:10 +0100 | [diff] [blame] | 1748 | if (!power_supply_get_property(fuel_gauge, |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1749 | POWER_SUPPLY_PROP_CHARGE_NOW, &val)) { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1750 | cm->charger_psy_desc.properties[cm->charger_psy_desc.num_properties] = |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1751 | POWER_SUPPLY_PROP_CHARGE_NOW; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1752 | cm->charger_psy_desc.num_properties++; |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1753 | } |
Krzysztof Kozlowski | b70229b | 2015-03-12 08:44:10 +0100 | [diff] [blame] | 1754 | if (!power_supply_get_property(fuel_gauge, |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1755 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 1756 | &val)) { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1757 | cm->charger_psy_desc.properties[cm->charger_psy_desc.num_properties] = |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1758 | POWER_SUPPLY_PROP_CURRENT_NOW; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1759 | cm->charger_psy_desc.num_properties++; |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1760 | } |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 1761 | |
Krzysztof Kozlowski | bdbe814 | 2014-10-13 15:34:30 +0200 | [diff] [blame] | 1762 | ret = cm_init_thermal_data(cm, fuel_gauge); |
Jonghwa Lee | 5c49a62 | 2013-12-18 15:42:34 +0900 | [diff] [blame] | 1763 | if (ret) { |
| 1764 | dev_err(&pdev->dev, "Failed to initialize thermal data\n"); |
| 1765 | cm->desc->measure_battery_temp = false; |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1766 | } |
Krzysztof Kozlowski | b43eb35 | 2015-03-12 08:44:14 +0100 | [diff] [blame] | 1767 | power_supply_put(fuel_gauge); |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1768 | |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1769 | INIT_DELAYED_WORK(&cm->fullbatt_vchk_work, fullbatt_vchk); |
| 1770 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1771 | cm->charger_psy = power_supply_register(NULL, &cm->charger_psy_desc, |
| 1772 | &psy_cfg); |
| 1773 | if (IS_ERR(cm->charger_psy)) { |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 1774 | dev_err(&pdev->dev, "Cannot register charger-manager with name \"%s\"\n", |
Krzysztof Kozlowski | ecf896b | 2015-03-24 08:58:43 +0100 | [diff] [blame] | 1775 | cm->charger_psy_desc.name); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1776 | return PTR_ERR(cm->charger_psy); |
Donggeun Kim | ad3d13ee | 2011-12-27 18:47:49 +0900 | [diff] [blame] | 1777 | } |
| 1778 | |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1779 | /* Register extcon device for charger cable */ |
| 1780 | ret = charger_manager_register_extcon(cm); |
| 1781 | if (ret < 0) { |
| 1782 | dev_err(&pdev->dev, "Cannot initialize extcon device\n"); |
| 1783 | goto err_reg_extcon; |
Chanwoo Choi | 3950c78 | 2012-09-21 18:49:37 +0900 | [diff] [blame] | 1784 | } |
| 1785 | |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1786 | /* Register sysfs entry for charger(regulator) */ |
| 1787 | ret = charger_manager_register_sysfs(cm); |
| 1788 | if (ret < 0) { |
| 1789 | dev_err(&pdev->dev, |
| 1790 | "Cannot initialize sysfs entry of regulator\n"); |
| 1791 | goto err_reg_sysfs; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1792 | } |
| 1793 | |
| 1794 | /* Add to the list */ |
| 1795 | mutex_lock(&cm_list_mtx); |
| 1796 | list_add(&cm->entry, &cm_list); |
| 1797 | mutex_unlock(&cm_list_mtx); |
| 1798 | |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 1799 | /* |
| 1800 | * Charger-manager is capable of waking up the systme from sleep |
| 1801 | * when event is happend through cm_notify_event() |
| 1802 | */ |
| 1803 | device_init_wakeup(&pdev->dev, true); |
| 1804 | device_set_wakeup_capable(&pdev->dev, false); |
| 1805 | |
Chanwoo Choi | b1022e2 | 2014-08-26 13:41:39 +0900 | [diff] [blame] | 1806 | /* |
| 1807 | * Charger-manager have to check the charging state right after |
| 1808 | * tialization of charger-manager and then update current charging |
| 1809 | * state. |
| 1810 | */ |
| 1811 | cm_monitor(); |
| 1812 | |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1813 | schedule_work(&setup_polling); |
| 1814 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1815 | return 0; |
| 1816 | |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1817 | err_reg_sysfs: |
Chanwoo Choi | 3950c78 | 2012-09-21 18:49:37 +0900 | [diff] [blame] | 1818 | for (i = 0; i < desc->num_charger_regulators; i++) { |
| 1819 | struct charger_regulator *charger; |
| 1820 | |
| 1821 | charger = &desc->charger_regulators[i]; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1822 | sysfs_remove_group(&cm->charger_psy->dev.kobj, |
Chanwoo Choi | 3950c78 | 2012-09-21 18:49:37 +0900 | [diff] [blame] | 1823 | &charger->attr_g); |
Chanwoo Choi | 3950c78 | 2012-09-21 18:49:37 +0900 | [diff] [blame] | 1824 | } |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1825 | err_reg_extcon: |
| 1826 | for (i = 0; i < desc->num_charger_regulators; i++) { |
| 1827 | struct charger_regulator *charger; |
| 1828 | |
| 1829 | charger = &desc->charger_regulators[i]; |
| 1830 | for (j = 0; j < charger->num_cables; j++) { |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1831 | struct charger_cable *cable = &charger->cables[j]; |
Jonghwa Lee | 3cc9d269 | 2013-06-25 14:02:49 +0900 | [diff] [blame] | 1832 | /* Remove notifier block if only edev exists */ |
| 1833 | if (cable->extcon_dev.edev) |
| 1834 | extcon_unregister_interest(&cable->extcon_dev); |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1835 | } |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1836 | |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1837 | regulator_put(desc->charger_regulators[i].consumer); |
Chanwoo Choi | 41468a1 | 2012-11-22 16:54:26 +0900 | [diff] [blame] | 1838 | } |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1839 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1840 | power_supply_unregister(cm->charger_psy); |
Jonghwa Lee | 883c10a | 2013-10-25 11:47:31 +0900 | [diff] [blame] | 1841 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1842 | return ret; |
| 1843 | } |
| 1844 | |
Bill Pemberton | 415ec69 | 2012-11-19 13:26:07 -0500 | [diff] [blame] | 1845 | static int charger_manager_remove(struct platform_device *pdev) |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1846 | { |
| 1847 | struct charger_manager *cm = platform_get_drvdata(pdev); |
| 1848 | struct charger_desc *desc = cm->desc; |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1849 | int i = 0; |
| 1850 | int j = 0; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1851 | |
| 1852 | /* Remove from the list */ |
| 1853 | mutex_lock(&cm_list_mtx); |
| 1854 | list_del(&cm->entry); |
| 1855 | mutex_unlock(&cm_list_mtx); |
| 1856 | |
Tejun Heo | 2fbb520 | 2012-12-21 17:56:51 -0800 | [diff] [blame] | 1857 | cancel_work_sync(&setup_polling); |
| 1858 | cancel_delayed_work_sync(&cm_monitor_work); |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1859 | |
Chanwoo Choi | bee737b | 2012-07-12 15:03:25 +0900 | [diff] [blame] | 1860 | for (i = 0 ; i < desc->num_charger_regulators ; i++) { |
| 1861 | struct charger_regulator *charger |
| 1862 | = &desc->charger_regulators[i]; |
| 1863 | for (j = 0 ; j < charger->num_cables ; j++) { |
| 1864 | struct charger_cable *cable = &charger->cables[j]; |
| 1865 | extcon_unregister_interest(&cable->extcon_dev); |
| 1866 | } |
| 1867 | } |
| 1868 | |
| 1869 | for (i = 0 ; i < desc->num_charger_regulators ; i++) |
| 1870 | regulator_put(desc->charger_regulators[i].consumer); |
| 1871 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1872 | power_supply_unregister(cm->charger_psy); |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1873 | |
| 1874 | try_charger_enable(cm, false); |
| 1875 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1876 | return 0; |
| 1877 | } |
| 1878 | |
Axel Lin | 1bbe24d | 2012-01-11 17:19:45 +0800 | [diff] [blame] | 1879 | static const struct platform_device_id charger_manager_id[] = { |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1880 | { "charger-manager", 0 }, |
| 1881 | { }, |
| 1882 | }; |
Axel Lin | 1bbe24d | 2012-01-11 17:19:45 +0800 | [diff] [blame] | 1883 | MODULE_DEVICE_TABLE(platform, charger_manager_id); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1884 | |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 1885 | static int cm_suspend_noirq(struct device *dev) |
| 1886 | { |
| 1887 | int ret = 0; |
| 1888 | |
| 1889 | if (device_may_wakeup(dev)) { |
| 1890 | device_set_wakeup_capable(dev, false); |
| 1891 | ret = -EAGAIN; |
| 1892 | } |
| 1893 | |
| 1894 | return ret; |
| 1895 | } |
| 1896 | |
Jonghwa Lee | c1155c6 | 2014-12-19 17:55:13 +0900 | [diff] [blame] | 1897 | static bool cm_need_to_awake(void) |
| 1898 | { |
| 1899 | struct charger_manager *cm; |
| 1900 | |
| 1901 | if (cm_timer) |
| 1902 | return false; |
| 1903 | |
| 1904 | mutex_lock(&cm_list_mtx); |
| 1905 | list_for_each_entry(cm, &cm_list, entry) { |
| 1906 | if (is_charging(cm)) { |
| 1907 | mutex_unlock(&cm_list_mtx); |
| 1908 | return true; |
| 1909 | } |
| 1910 | } |
| 1911 | mutex_unlock(&cm_list_mtx); |
| 1912 | |
| 1913 | return false; |
| 1914 | } |
| 1915 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1916 | static int cm_suspend_prepare(struct device *dev) |
| 1917 | { |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 1918 | struct charger_manager *cm = dev_get_drvdata(dev); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1919 | |
Jonghwa Lee | c1155c6 | 2014-12-19 17:55:13 +0900 | [diff] [blame] | 1920 | if (cm_need_to_awake()) |
| 1921 | return -EBUSY; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1922 | |
Jonghwa Lee | c1155c6 | 2014-12-19 17:55:13 +0900 | [diff] [blame] | 1923 | if (!cm_suspended) |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1924 | cm_suspended = true; |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1925 | |
Jonghwa Lee | c1155c6 | 2014-12-19 17:55:13 +0900 | [diff] [blame] | 1926 | cm_timer_set = cm_setup_timer(); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1927 | |
Jonghwa Lee | c1155c6 | 2014-12-19 17:55:13 +0900 | [diff] [blame] | 1928 | if (cm_timer_set) { |
| 1929 | cancel_work_sync(&setup_polling); |
| 1930 | cancel_delayed_work_sync(&cm_monitor_work); |
| 1931 | cancel_delayed_work(&cm->fullbatt_vchk_work); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | return 0; |
| 1935 | } |
| 1936 | |
| 1937 | static void cm_suspend_complete(struct device *dev) |
| 1938 | { |
Axel Lin | bb2a95c | 2012-01-12 12:56:35 +0800 | [diff] [blame] | 1939 | struct charger_manager *cm = dev_get_drvdata(dev); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1940 | |
Jonghwa Lee | c1155c6 | 2014-12-19 17:55:13 +0900 | [diff] [blame] | 1941 | if (cm_suspended) |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1942 | cm_suspended = false; |
Jonghwa Lee | c1155c6 | 2014-12-19 17:55:13 +0900 | [diff] [blame] | 1943 | |
| 1944 | if (cm_timer_set) { |
| 1945 | ktime_t remain; |
| 1946 | |
| 1947 | alarm_cancel(cm_timer); |
| 1948 | cm_timer_set = false; |
| 1949 | remain = alarm_expires_remaining(cm_timer); |
| 1950 | cm_suspend_duration_ms -= ktime_to_ms(remain); |
| 1951 | schedule_work(&setup_polling); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1952 | } |
| 1953 | |
Jonghwa Lee | c1155c6 | 2014-12-19 17:55:13 +0900 | [diff] [blame] | 1954 | _cm_monitor(cm); |
| 1955 | |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1956 | /* Re-enqueue delayed work (fullbatt_vchk_work) */ |
| 1957 | if (cm->fullbatt_vchk_jiffies_at) { |
| 1958 | unsigned long delay = 0; |
| 1959 | unsigned long now = jiffies + CM_JIFFIES_SMALL; |
| 1960 | |
| 1961 | if (time_after_eq(now, cm->fullbatt_vchk_jiffies_at)) { |
| 1962 | delay = (unsigned long)((long)now |
| 1963 | - (long)(cm->fullbatt_vchk_jiffies_at)); |
| 1964 | delay = jiffies_to_msecs(delay); |
| 1965 | } else { |
| 1966 | delay = 0; |
| 1967 | } |
| 1968 | |
| 1969 | /* |
Jonghwa Lee | c1155c6 | 2014-12-19 17:55:13 +0900 | [diff] [blame] | 1970 | * Account for cm_suspend_duration_ms with assuming that |
| 1971 | * timer stops in suspend. |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1972 | */ |
Jonghwa Lee | c1155c6 | 2014-12-19 17:55:13 +0900 | [diff] [blame] | 1973 | if (delay > cm_suspend_duration_ms) |
| 1974 | delay -= cm_suspend_duration_ms; |
| 1975 | else |
| 1976 | delay = 0; |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 1977 | |
| 1978 | queue_delayed_work(cm_wq, &cm->fullbatt_vchk_work, |
| 1979 | msecs_to_jiffies(delay)); |
| 1980 | } |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 1981 | device_set_wakeup_capable(cm->dev, false); |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1982 | } |
| 1983 | |
| 1984 | static const struct dev_pm_ops charger_manager_pm = { |
| 1985 | .prepare = cm_suspend_prepare, |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 1986 | .suspend_noirq = cm_suspend_noirq, |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1987 | .complete = cm_suspend_complete, |
| 1988 | }; |
| 1989 | |
| 1990 | static struct platform_driver charger_manager_driver = { |
| 1991 | .driver = { |
| 1992 | .name = "charger-manager", |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1993 | .pm = &charger_manager_pm, |
Jonghwa Lee | 856ee61 | 2013-12-18 15:42:35 +0900 | [diff] [blame] | 1994 | .of_match_table = charger_manager_match, |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1995 | }, |
| 1996 | .probe = charger_manager_probe, |
Bill Pemberton | 28ea73f | 2012-11-19 13:20:40 -0500 | [diff] [blame] | 1997 | .remove = charger_manager_remove, |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 1998 | .id_table = charger_manager_id, |
| 1999 | }; |
| 2000 | |
| 2001 | static int __init charger_manager_init(void) |
| 2002 | { |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 2003 | cm_wq = create_freezable_workqueue("charger_manager"); |
| 2004 | INIT_DELAYED_WORK(&cm_monitor_work, cm_monitor_poller); |
| 2005 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 2006 | return platform_driver_register(&charger_manager_driver); |
| 2007 | } |
| 2008 | late_initcall(charger_manager_init); |
| 2009 | |
| 2010 | static void __exit charger_manager_cleanup(void) |
| 2011 | { |
Chanwoo Choi | d829dc7 | 2012-05-05 06:24:10 -0700 | [diff] [blame] | 2012 | destroy_workqueue(cm_wq); |
| 2013 | cm_wq = NULL; |
| 2014 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 2015 | platform_driver_unregister(&charger_manager_driver); |
| 2016 | } |
| 2017 | module_exit(charger_manager_cleanup); |
| 2018 | |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 2019 | /** |
| 2020 | * find_power_supply - find the associated power_supply of charger |
| 2021 | * @cm: the Charger Manager representing the battery |
| 2022 | * @psy: pointer to instance of charger's power_supply |
| 2023 | */ |
| 2024 | static bool find_power_supply(struct charger_manager *cm, |
| 2025 | struct power_supply *psy) |
| 2026 | { |
| 2027 | int i; |
| 2028 | bool found = false; |
| 2029 | |
Krzysztof Kozlowski | cdaf3e1 | 2014-10-13 15:34:31 +0200 | [diff] [blame] | 2030 | for (i = 0; cm->desc->psy_charger_stat[i]; i++) { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 2031 | if (!strcmp(psy->desc->name, cm->desc->psy_charger_stat[i])) { |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 2032 | found = true; |
| 2033 | break; |
| 2034 | } |
| 2035 | } |
| 2036 | |
| 2037 | return found; |
| 2038 | } |
| 2039 | |
| 2040 | /** |
| 2041 | * cm_notify_event - charger driver notify Charger Manager of charger event |
| 2042 | * @psy: pointer to instance of charger's power_supply |
| 2043 | * @type: type of charger event |
| 2044 | * @msg: optional message passed to uevent_notify fuction |
| 2045 | */ |
| 2046 | void cm_notify_event(struct power_supply *psy, enum cm_event_types type, |
| 2047 | char *msg) |
| 2048 | { |
| 2049 | struct charger_manager *cm; |
| 2050 | bool found_power_supply = false; |
| 2051 | |
| 2052 | if (psy == NULL) |
| 2053 | return; |
| 2054 | |
| 2055 | mutex_lock(&cm_list_mtx); |
| 2056 | list_for_each_entry(cm, &cm_list, entry) { |
| 2057 | found_power_supply = find_power_supply(cm, psy); |
| 2058 | if (found_power_supply) |
| 2059 | break; |
| 2060 | } |
| 2061 | mutex_unlock(&cm_list_mtx); |
| 2062 | |
| 2063 | if (!found_power_supply) |
| 2064 | return; |
| 2065 | |
| 2066 | switch (type) { |
| 2067 | case CM_EVENT_BATT_FULL: |
| 2068 | fullbatt_handler(cm); |
| 2069 | break; |
| 2070 | case CM_EVENT_BATT_OUT: |
| 2071 | battout_handler(cm); |
| 2072 | break; |
| 2073 | case CM_EVENT_BATT_IN: |
| 2074 | case CM_EVENT_EXT_PWR_IN_OUT ... CM_EVENT_CHG_START_STOP: |
| 2075 | misc_event_handler(cm, type); |
| 2076 | break; |
| 2077 | case CM_EVENT_UNKNOWN: |
| 2078 | case CM_EVENT_OTHERS: |
| 2079 | uevent_notify(cm, msg ? msg : default_event_names[type]); |
| 2080 | break; |
| 2081 | default: |
Joe Perches | e5409cb | 2013-06-06 18:25:12 -0700 | [diff] [blame] | 2082 | dev_err(cm->dev, "%s: type not specified\n", __func__); |
Chanwoo Choi | dfeccb1 | 2012-05-05 06:26:47 -0700 | [diff] [blame] | 2083 | break; |
| 2084 | } |
| 2085 | } |
| 2086 | EXPORT_SYMBOL_GPL(cm_notify_event); |
| 2087 | |
Donggeun Kim | 3bb3dbb | 2011-12-27 18:47:48 +0900 | [diff] [blame] | 2088 | MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); |
| 2089 | MODULE_DESCRIPTION("Charger Manager"); |
| 2090 | MODULE_LICENSE("GPL"); |