Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) ST-Ericsson SA 2012 |
| 3 | * |
| 4 | * Charging algorithm driver for abx500 variants |
| 5 | * |
| 6 | * License Terms: GNU General Public License v2 |
| 7 | * Authors: |
| 8 | * Johan Palsson <johan.palsson@stericsson.com> |
| 9 | * Karl Komierowski <karl.komierowski@stericsson.com> |
| 10 | * Arun R Murthy <arun.murthy@stericsson.com> |
| 11 | */ |
| 12 | |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/device.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/power_supply.h> |
| 21 | #include <linux/completion.h> |
| 22 | #include <linux/workqueue.h> |
| 23 | #include <linux/kobject.h> |
Rajanikanth H.V | a12810a | 2012-10-31 15:40:33 +0000 | [diff] [blame] | 24 | #include <linux/of.h> |
| 25 | #include <linux/mfd/core.h> |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 26 | #include <linux/mfd/abx500.h> |
| 27 | #include <linux/mfd/abx500/ux500_chargalg.h> |
| 28 | #include <linux/mfd/abx500/ab8500-bm.h> |
Lee Jones | 8891716 | 2013-02-13 11:39:19 +0000 | [diff] [blame] | 29 | #include <linux/notifier.h> |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 30 | |
| 31 | /* Watchdog kick interval */ |
| 32 | #define CHG_WD_INTERVAL (6 * HZ) |
| 33 | |
| 34 | /* End-of-charge criteria counter */ |
| 35 | #define EOC_COND_CNT 10 |
| 36 | |
Lee Jones | db43e6c | 2013-02-14 12:39:15 +0000 | [diff] [blame] | 37 | /* Plus margin for the low battery threshold */ |
| 38 | #define BAT_PLUS_MARGIN (100) |
| 39 | |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 40 | #define to_abx500_chargalg_device_info(x) container_of((x), \ |
| 41 | struct abx500_chargalg, chargalg_psy); |
| 42 | |
| 43 | enum abx500_chargers { |
| 44 | NO_CHG, |
| 45 | AC_CHG, |
| 46 | USB_CHG, |
| 47 | }; |
| 48 | |
| 49 | struct abx500_chargalg_charger_info { |
| 50 | enum abx500_chargers conn_chg; |
| 51 | enum abx500_chargers prev_conn_chg; |
| 52 | enum abx500_chargers online_chg; |
| 53 | enum abx500_chargers prev_online_chg; |
| 54 | enum abx500_chargers charger_type; |
| 55 | bool usb_chg_ok; |
| 56 | bool ac_chg_ok; |
| 57 | int usb_volt; |
| 58 | int usb_curr; |
| 59 | int ac_volt; |
| 60 | int ac_curr; |
| 61 | int usb_vset; |
| 62 | int usb_iset; |
| 63 | int ac_vset; |
| 64 | int ac_iset; |
| 65 | }; |
| 66 | |
| 67 | struct abx500_chargalg_suspension_status { |
| 68 | bool suspended_change; |
| 69 | bool ac_suspended; |
| 70 | bool usb_suspended; |
| 71 | }; |
| 72 | |
| 73 | struct abx500_chargalg_battery_data { |
| 74 | int temp; |
| 75 | int volt; |
| 76 | int avg_curr; |
| 77 | int inst_curr; |
| 78 | int percent; |
| 79 | }; |
| 80 | |
| 81 | enum abx500_chargalg_states { |
| 82 | STATE_HANDHELD_INIT, |
| 83 | STATE_HANDHELD, |
| 84 | STATE_CHG_NOT_OK_INIT, |
| 85 | STATE_CHG_NOT_OK, |
| 86 | STATE_HW_TEMP_PROTECT_INIT, |
| 87 | STATE_HW_TEMP_PROTECT, |
| 88 | STATE_NORMAL_INIT, |
Lee Jones | db43e6c | 2013-02-14 12:39:15 +0000 | [diff] [blame] | 89 | STATE_USB_PP_PRE_CHARGE, |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 90 | STATE_NORMAL, |
| 91 | STATE_WAIT_FOR_RECHARGE_INIT, |
| 92 | STATE_WAIT_FOR_RECHARGE, |
| 93 | STATE_MAINTENANCE_A_INIT, |
| 94 | STATE_MAINTENANCE_A, |
| 95 | STATE_MAINTENANCE_B_INIT, |
| 96 | STATE_MAINTENANCE_B, |
| 97 | STATE_TEMP_UNDEROVER_INIT, |
| 98 | STATE_TEMP_UNDEROVER, |
| 99 | STATE_TEMP_LOWHIGH_INIT, |
| 100 | STATE_TEMP_LOWHIGH, |
| 101 | STATE_SUSPENDED_INIT, |
| 102 | STATE_SUSPENDED, |
| 103 | STATE_OVV_PROTECT_INIT, |
| 104 | STATE_OVV_PROTECT, |
| 105 | STATE_SAFETY_TIMER_EXPIRED_INIT, |
| 106 | STATE_SAFETY_TIMER_EXPIRED, |
| 107 | STATE_BATT_REMOVED_INIT, |
| 108 | STATE_BATT_REMOVED, |
| 109 | STATE_WD_EXPIRED_INIT, |
| 110 | STATE_WD_EXPIRED, |
| 111 | }; |
| 112 | |
| 113 | static const char *states[] = { |
| 114 | "HANDHELD_INIT", |
| 115 | "HANDHELD", |
| 116 | "CHG_NOT_OK_INIT", |
| 117 | "CHG_NOT_OK", |
| 118 | "HW_TEMP_PROTECT_INIT", |
| 119 | "HW_TEMP_PROTECT", |
| 120 | "NORMAL_INIT", |
Lee Jones | db43e6c | 2013-02-14 12:39:15 +0000 | [diff] [blame] | 121 | "USB_PP_PRE_CHARGE", |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 122 | "NORMAL", |
| 123 | "WAIT_FOR_RECHARGE_INIT", |
| 124 | "WAIT_FOR_RECHARGE", |
| 125 | "MAINTENANCE_A_INIT", |
| 126 | "MAINTENANCE_A", |
| 127 | "MAINTENANCE_B_INIT", |
| 128 | "MAINTENANCE_B", |
| 129 | "TEMP_UNDEROVER_INIT", |
| 130 | "TEMP_UNDEROVER", |
| 131 | "TEMP_LOWHIGH_INIT", |
| 132 | "TEMP_LOWHIGH", |
| 133 | "SUSPENDED_INIT", |
| 134 | "SUSPENDED", |
| 135 | "OVV_PROTECT_INIT", |
| 136 | "OVV_PROTECT", |
| 137 | "SAFETY_TIMER_EXPIRED_INIT", |
| 138 | "SAFETY_TIMER_EXPIRED", |
| 139 | "BATT_REMOVED_INIT", |
| 140 | "BATT_REMOVED", |
| 141 | "WD_EXPIRED_INIT", |
| 142 | "WD_EXPIRED", |
| 143 | }; |
| 144 | |
| 145 | struct abx500_chargalg_events { |
| 146 | bool batt_unknown; |
| 147 | bool mainextchnotok; |
| 148 | bool batt_ovv; |
| 149 | bool batt_rem; |
| 150 | bool btemp_underover; |
| 151 | bool btemp_lowhigh; |
| 152 | bool main_thermal_prot; |
| 153 | bool usb_thermal_prot; |
| 154 | bool main_ovv; |
| 155 | bool vbus_ovv; |
| 156 | bool usbchargernotok; |
| 157 | bool safety_timer_expired; |
| 158 | bool maintenance_timer_expired; |
| 159 | bool ac_wd_expired; |
| 160 | bool usb_wd_expired; |
| 161 | bool ac_cv_active; |
| 162 | bool usb_cv_active; |
| 163 | bool vbus_collapsed; |
| 164 | }; |
| 165 | |
| 166 | /** |
| 167 | * struct abx500_charge_curr_maximization - Charger maximization parameters |
| 168 | * @original_iset: the non optimized/maximised charger current |
| 169 | * @current_iset: the charging current used at this moment |
| 170 | * @test_delta_i: the delta between the current we want to charge and the |
| 171 | current that is really going into the battery |
| 172 | * @condition_cnt: number of iterations needed before a new charger current |
| 173 | is set |
| 174 | * @max_current: maximum charger current |
| 175 | * @wait_cnt: to avoid too fast current step down in case of charger |
| 176 | * voltage collapse, we insert this delay between step |
| 177 | * down |
| 178 | * @level: tells in how many steps the charging current has been |
| 179 | increased |
| 180 | */ |
| 181 | struct abx500_charge_curr_maximization { |
| 182 | int original_iset; |
| 183 | int current_iset; |
| 184 | int test_delta_i; |
| 185 | int condition_cnt; |
| 186 | int max_current; |
| 187 | int wait_cnt; |
| 188 | u8 level; |
| 189 | }; |
| 190 | |
| 191 | enum maxim_ret { |
| 192 | MAXIM_RET_NOACTION, |
| 193 | MAXIM_RET_CHANGE, |
| 194 | MAXIM_RET_IBAT_TOO_HIGH, |
| 195 | }; |
| 196 | |
| 197 | /** |
| 198 | * struct abx500_chargalg - abx500 Charging algorithm device information |
| 199 | * @dev: pointer to the structure device |
| 200 | * @charge_status: battery operating status |
| 201 | * @eoc_cnt: counter used to determine end-of_charge |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 202 | * @maintenance_chg: indicate if maintenance charge is active |
| 203 | * @t_hyst_norm temperature hysteresis when the temperature has been |
| 204 | * over or under normal limits |
| 205 | * @t_hyst_lowhigh temperature hysteresis when the temperature has been |
| 206 | * over or under the high or low limits |
| 207 | * @charge_state: current state of the charging algorithm |
| 208 | * @ccm charging current maximization parameters |
| 209 | * @chg_info: information about connected charger types |
| 210 | * @batt_data: data of the battery |
| 211 | * @susp_status: current charger suspension status |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 212 | * @bm: Platform specific battery management information |
Lee Jones | 330b7eb | 2013-02-15 10:53:57 +0000 | [diff] [blame] | 213 | * @parent: pointer to the struct abx500 |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 214 | * @chargalg_psy: structure that holds the battery properties exposed by |
| 215 | * the charging algorithm |
| 216 | * @events: structure for information about events triggered |
| 217 | * @chargalg_wq: work queue for running the charging algorithm |
| 218 | * @chargalg_periodic_work: work to run the charging algorithm periodically |
| 219 | * @chargalg_wd_work: work to kick the charger watchdog periodically |
| 220 | * @chargalg_work: work to run the charging algorithm instantly |
| 221 | * @safety_timer: charging safety timer |
| 222 | * @maintenance_timer: maintenance charging timer |
| 223 | * @chargalg_kobject: structure of type kobject |
| 224 | */ |
| 225 | struct abx500_chargalg { |
| 226 | struct device *dev; |
| 227 | int charge_status; |
| 228 | int eoc_cnt; |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 229 | bool maintenance_chg; |
| 230 | int t_hyst_norm; |
| 231 | int t_hyst_lowhigh; |
| 232 | enum abx500_chargalg_states charge_state; |
| 233 | struct abx500_charge_curr_maximization ccm; |
| 234 | struct abx500_chargalg_charger_info chg_info; |
| 235 | struct abx500_chargalg_battery_data batt_data; |
| 236 | struct abx500_chargalg_suspension_status susp_status; |
Lee Jones | 330b7eb | 2013-02-15 10:53:57 +0000 | [diff] [blame] | 237 | struct ab8500 *parent; |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 238 | struct abx500_bm_data *bm; |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 239 | struct power_supply chargalg_psy; |
| 240 | struct ux500_charger *ac_chg; |
| 241 | struct ux500_charger *usb_chg; |
| 242 | struct abx500_chargalg_events events; |
| 243 | struct workqueue_struct *chargalg_wq; |
| 244 | struct delayed_work chargalg_periodic_work; |
| 245 | struct delayed_work chargalg_wd_work; |
| 246 | struct work_struct chargalg_work; |
| 247 | struct timer_list safety_timer; |
| 248 | struct timer_list maintenance_timer; |
| 249 | struct kobject chargalg_kobject; |
| 250 | }; |
| 251 | |
Lee Jones | 8891716 | 2013-02-13 11:39:19 +0000 | [diff] [blame] | 252 | /*External charger prepare notifier*/ |
| 253 | BLOCKING_NOTIFIER_HEAD(charger_notifier_list); |
| 254 | |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 255 | /* Main battery properties */ |
| 256 | static enum power_supply_property abx500_chargalg_props[] = { |
| 257 | POWER_SUPPLY_PROP_STATUS, |
| 258 | POWER_SUPPLY_PROP_HEALTH, |
| 259 | }; |
| 260 | |
| 261 | /** |
| 262 | * abx500_chargalg_safety_timer_expired() - Expiration of the safety timer |
| 263 | * @data: pointer to the abx500_chargalg structure |
| 264 | * |
| 265 | * This function gets called when the safety timer for the charger |
| 266 | * expires |
| 267 | */ |
| 268 | static void abx500_chargalg_safety_timer_expired(unsigned long data) |
| 269 | { |
| 270 | struct abx500_chargalg *di = (struct abx500_chargalg *) data; |
| 271 | dev_err(di->dev, "Safety timer expired\n"); |
| 272 | di->events.safety_timer_expired = true; |
| 273 | |
| 274 | /* Trigger execution of the algorithm instantly */ |
| 275 | queue_work(di->chargalg_wq, &di->chargalg_work); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * abx500_chargalg_maintenance_timer_expired() - Expiration of |
| 280 | * the maintenance timer |
| 281 | * @i: pointer to the abx500_chargalg structure |
| 282 | * |
| 283 | * This function gets called when the maintenence timer |
| 284 | * expires |
| 285 | */ |
| 286 | static void abx500_chargalg_maintenance_timer_expired(unsigned long data) |
| 287 | { |
| 288 | |
| 289 | struct abx500_chargalg *di = (struct abx500_chargalg *) data; |
| 290 | dev_dbg(di->dev, "Maintenance timer expired\n"); |
| 291 | di->events.maintenance_timer_expired = true; |
| 292 | |
| 293 | /* Trigger execution of the algorithm instantly */ |
| 294 | queue_work(di->chargalg_wq, &di->chargalg_work); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * abx500_chargalg_state_to() - Change charge state |
| 299 | * @di: pointer to the abx500_chargalg structure |
| 300 | * |
| 301 | * This function gets called when a charge state change should occur |
| 302 | */ |
| 303 | static void abx500_chargalg_state_to(struct abx500_chargalg *di, |
| 304 | enum abx500_chargalg_states state) |
| 305 | { |
| 306 | dev_dbg(di->dev, |
| 307 | "State changed: %s (From state: [%d] %s =to=> [%d] %s )\n", |
| 308 | di->charge_state == state ? "NO" : "YES", |
| 309 | di->charge_state, |
| 310 | states[di->charge_state], |
| 311 | state, |
| 312 | states[state]); |
| 313 | |
| 314 | di->charge_state = state; |
| 315 | } |
| 316 | |
Lee Jones | 4dcdf57 | 2013-02-14 09:24:10 +0000 | [diff] [blame] | 317 | static int abx500_chargalg_check_charger_enable(struct abx500_chargalg *di) |
| 318 | { |
| 319 | switch (di->charge_state) { |
| 320 | case STATE_NORMAL: |
| 321 | case STATE_MAINTENANCE_A: |
| 322 | case STATE_MAINTENANCE_B: |
| 323 | break; |
| 324 | default: |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | if (di->chg_info.charger_type & USB_CHG) { |
| 329 | return di->usb_chg->ops.check_enable(di->usb_chg, |
| 330 | di->bm->bat_type[di->bm->batt_id].normal_vol_lvl, |
| 331 | di->bm->bat_type[di->bm->batt_id].normal_cur_lvl); |
| 332 | } else if ((di->chg_info.charger_type & AC_CHG) && |
| 333 | !(di->ac_chg->external)) { |
| 334 | return di->ac_chg->ops.check_enable(di->ac_chg, |
| 335 | di->bm->bat_type[di->bm->batt_id].normal_vol_lvl, |
| 336 | di->bm->bat_type[di->bm->batt_id].normal_cur_lvl); |
| 337 | } |
| 338 | return 0; |
| 339 | } |
| 340 | |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 341 | /** |
| 342 | * abx500_chargalg_check_charger_connection() - Check charger connection change |
| 343 | * @di: pointer to the abx500_chargalg structure |
| 344 | * |
| 345 | * This function will check if there is a change in the charger connection |
| 346 | * and change charge state accordingly. AC has precedence over USB. |
| 347 | */ |
| 348 | static int abx500_chargalg_check_charger_connection(struct abx500_chargalg *di) |
| 349 | { |
| 350 | if (di->chg_info.conn_chg != di->chg_info.prev_conn_chg || |
| 351 | di->susp_status.suspended_change) { |
| 352 | /* |
| 353 | * Charger state changed or suspension |
| 354 | * has changed since last update |
| 355 | */ |
| 356 | if ((di->chg_info.conn_chg & AC_CHG) && |
| 357 | !di->susp_status.ac_suspended) { |
| 358 | dev_dbg(di->dev, "Charging source is AC\n"); |
| 359 | if (di->chg_info.charger_type != AC_CHG) { |
| 360 | di->chg_info.charger_type = AC_CHG; |
| 361 | abx500_chargalg_state_to(di, STATE_NORMAL_INIT); |
| 362 | } |
| 363 | } else if ((di->chg_info.conn_chg & USB_CHG) && |
| 364 | !di->susp_status.usb_suspended) { |
| 365 | dev_dbg(di->dev, "Charging source is USB\n"); |
| 366 | di->chg_info.charger_type = USB_CHG; |
| 367 | abx500_chargalg_state_to(di, STATE_NORMAL_INIT); |
| 368 | } else if (di->chg_info.conn_chg && |
| 369 | (di->susp_status.ac_suspended || |
| 370 | di->susp_status.usb_suspended)) { |
| 371 | dev_dbg(di->dev, "Charging is suspended\n"); |
| 372 | di->chg_info.charger_type = NO_CHG; |
| 373 | abx500_chargalg_state_to(di, STATE_SUSPENDED_INIT); |
| 374 | } else { |
| 375 | dev_dbg(di->dev, "Charging source is OFF\n"); |
| 376 | di->chg_info.charger_type = NO_CHG; |
| 377 | abx500_chargalg_state_to(di, STATE_HANDHELD_INIT); |
| 378 | } |
| 379 | di->chg_info.prev_conn_chg = di->chg_info.conn_chg; |
| 380 | di->susp_status.suspended_change = false; |
| 381 | } |
| 382 | return di->chg_info.conn_chg; |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * abx500_chargalg_start_safety_timer() - Start charging safety timer |
| 387 | * @di: pointer to the abx500_chargalg structure |
| 388 | * |
| 389 | * The safety timer is used to avoid overcharging of old or bad batteries. |
| 390 | * There are different timers for AC and USB |
| 391 | */ |
| 392 | static void abx500_chargalg_start_safety_timer(struct abx500_chargalg *di) |
| 393 | { |
| 394 | unsigned long timer_expiration = 0; |
| 395 | |
| 396 | switch (di->chg_info.charger_type) { |
| 397 | case AC_CHG: |
| 398 | timer_expiration = |
| 399 | round_jiffies(jiffies + |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 400 | (di->bm->main_safety_tmr_h * 3600 * HZ)); |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 401 | break; |
| 402 | |
| 403 | case USB_CHG: |
| 404 | timer_expiration = |
| 405 | round_jiffies(jiffies + |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 406 | (di->bm->usb_safety_tmr_h * 3600 * HZ)); |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 407 | break; |
| 408 | |
| 409 | default: |
| 410 | dev_err(di->dev, "Unknown charger to charge from\n"); |
| 411 | break; |
| 412 | } |
| 413 | |
| 414 | di->events.safety_timer_expired = false; |
| 415 | di->safety_timer.expires = timer_expiration; |
| 416 | if (!timer_pending(&di->safety_timer)) |
| 417 | add_timer(&di->safety_timer); |
| 418 | else |
| 419 | mod_timer(&di->safety_timer, timer_expiration); |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * abx500_chargalg_stop_safety_timer() - Stop charging safety timer |
| 424 | * @di: pointer to the abx500_chargalg structure |
| 425 | * |
| 426 | * The safety timer is stopped whenever the NORMAL state is exited |
| 427 | */ |
| 428 | static void abx500_chargalg_stop_safety_timer(struct abx500_chargalg *di) |
| 429 | { |
| 430 | di->events.safety_timer_expired = false; |
| 431 | del_timer(&di->safety_timer); |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * abx500_chargalg_start_maintenance_timer() - Start charging maintenance timer |
| 436 | * @di: pointer to the abx500_chargalg structure |
| 437 | * @duration: duration of ther maintenance timer in hours |
| 438 | * |
| 439 | * The maintenance timer is used to maintain the charge in the battery once |
| 440 | * the battery is considered full. These timers are chosen to match the |
| 441 | * discharge curve of the battery |
| 442 | */ |
| 443 | static void abx500_chargalg_start_maintenance_timer(struct abx500_chargalg *di, |
| 444 | int duration) |
| 445 | { |
| 446 | unsigned long timer_expiration; |
| 447 | |
| 448 | /* Convert from hours to jiffies */ |
| 449 | timer_expiration = round_jiffies(jiffies + (duration * 3600 * HZ)); |
| 450 | |
| 451 | di->events.maintenance_timer_expired = false; |
| 452 | di->maintenance_timer.expires = timer_expiration; |
| 453 | if (!timer_pending(&di->maintenance_timer)) |
| 454 | add_timer(&di->maintenance_timer); |
| 455 | else |
| 456 | mod_timer(&di->maintenance_timer, timer_expiration); |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * abx500_chargalg_stop_maintenance_timer() - Stop maintenance timer |
| 461 | * @di: pointer to the abx500_chargalg structure |
| 462 | * |
| 463 | * The maintenance timer is stopped whenever maintenance ends or when another |
| 464 | * state is entered |
| 465 | */ |
| 466 | static void abx500_chargalg_stop_maintenance_timer(struct abx500_chargalg *di) |
| 467 | { |
| 468 | di->events.maintenance_timer_expired = false; |
| 469 | del_timer(&di->maintenance_timer); |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * abx500_chargalg_kick_watchdog() - Kick charger watchdog |
| 474 | * @di: pointer to the abx500_chargalg structure |
| 475 | * |
| 476 | * The charger watchdog have to be kicked periodically whenever the charger is |
| 477 | * on, else the ABB will reset the system |
| 478 | */ |
| 479 | static int abx500_chargalg_kick_watchdog(struct abx500_chargalg *di) |
| 480 | { |
| 481 | /* Check if charger exists and kick watchdog if charging */ |
| 482 | if (di->ac_chg && di->ac_chg->ops.kick_wd && |
Loic Pallardy | e07a564 | 2012-05-10 15:33:56 +0200 | [diff] [blame] | 483 | di->chg_info.online_chg & AC_CHG) { |
| 484 | /* |
| 485 | * If AB charger watchdog expired, pm2xxx charging |
| 486 | * gets disabled. To be safe, kick both AB charger watchdog |
| 487 | * and pm2xxx watchdog. |
| 488 | */ |
| 489 | if (di->ac_chg->external && |
| 490 | di->usb_chg && di->usb_chg->ops.kick_wd) |
| 491 | di->usb_chg->ops.kick_wd(di->usb_chg); |
| 492 | |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 493 | return di->ac_chg->ops.kick_wd(di->ac_chg); |
Loic Pallardy | e07a564 | 2012-05-10 15:33:56 +0200 | [diff] [blame] | 494 | } |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 495 | else if (di->usb_chg && di->usb_chg->ops.kick_wd && |
| 496 | di->chg_info.online_chg & USB_CHG) |
| 497 | return di->usb_chg->ops.kick_wd(di->usb_chg); |
| 498 | |
| 499 | return -ENXIO; |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * abx500_chargalg_ac_en() - Turn on/off the AC charger |
| 504 | * @di: pointer to the abx500_chargalg structure |
| 505 | * @enable: charger on/off |
| 506 | * @vset: requested charger output voltage |
| 507 | * @iset: requested charger output current |
| 508 | * |
| 509 | * The AC charger will be turned on/off with the requested charge voltage and |
| 510 | * current |
| 511 | */ |
| 512 | static int abx500_chargalg_ac_en(struct abx500_chargalg *di, int enable, |
| 513 | int vset, int iset) |
| 514 | { |
Lee Jones | 8891716 | 2013-02-13 11:39:19 +0000 | [diff] [blame] | 515 | static int abx500_chargalg_ex_ac_enable_toggle; |
| 516 | |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 517 | if (!di->ac_chg || !di->ac_chg->ops.enable) |
| 518 | return -ENXIO; |
| 519 | |
| 520 | /* Select maximum of what both the charger and the battery supports */ |
| 521 | if (di->ac_chg->max_out_volt) |
| 522 | vset = min(vset, di->ac_chg->max_out_volt); |
| 523 | if (di->ac_chg->max_out_curr) |
| 524 | iset = min(iset, di->ac_chg->max_out_curr); |
| 525 | |
| 526 | di->chg_info.ac_iset = iset; |
| 527 | di->chg_info.ac_vset = vset; |
| 528 | |
Lee Jones | 8891716 | 2013-02-13 11:39:19 +0000 | [diff] [blame] | 529 | /* Enable external charger */ |
| 530 | if (enable && di->ac_chg->external && |
| 531 | !abx500_chargalg_ex_ac_enable_toggle) { |
| 532 | blocking_notifier_call_chain(&charger_notifier_list, |
| 533 | 0, di->dev); |
| 534 | abx500_chargalg_ex_ac_enable_toggle++; |
| 535 | } |
| 536 | |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 537 | return di->ac_chg->ops.enable(di->ac_chg, enable, vset, iset); |
| 538 | } |
| 539 | |
| 540 | /** |
| 541 | * abx500_chargalg_usb_en() - Turn on/off the USB charger |
| 542 | * @di: pointer to the abx500_chargalg structure |
| 543 | * @enable: charger on/off |
| 544 | * @vset: requested charger output voltage |
| 545 | * @iset: requested charger output current |
| 546 | * |
| 547 | * The USB charger will be turned on/off with the requested charge voltage and |
| 548 | * current |
| 549 | */ |
| 550 | static int abx500_chargalg_usb_en(struct abx500_chargalg *di, int enable, |
| 551 | int vset, int iset) |
| 552 | { |
| 553 | if (!di->usb_chg || !di->usb_chg->ops.enable) |
| 554 | return -ENXIO; |
| 555 | |
| 556 | /* Select maximum of what both the charger and the battery supports */ |
| 557 | if (di->usb_chg->max_out_volt) |
| 558 | vset = min(vset, di->usb_chg->max_out_volt); |
| 559 | if (di->usb_chg->max_out_curr) |
| 560 | iset = min(iset, di->usb_chg->max_out_curr); |
| 561 | |
| 562 | di->chg_info.usb_iset = iset; |
| 563 | di->chg_info.usb_vset = vset; |
| 564 | |
| 565 | return di->usb_chg->ops.enable(di->usb_chg, enable, vset, iset); |
| 566 | } |
| 567 | |
Lee Jones | db43e6c | 2013-02-14 12:39:15 +0000 | [diff] [blame] | 568 | /** |
| 569 | * ab8540_chargalg_usb_pp_en() - Enable/ disable USB power path |
| 570 | * @di: pointer to the abx500_chargalg structure |
| 571 | * @enable: power path enable/disable |
| 572 | * |
| 573 | * The USB power path will be enable/ disable |
| 574 | */ |
| 575 | static int ab8540_chargalg_usb_pp_en(struct abx500_chargalg *di, bool enable) |
| 576 | { |
| 577 | if (!di->usb_chg || !di->usb_chg->ops.pp_enable) |
| 578 | return -ENXIO; |
| 579 | |
| 580 | return di->usb_chg->ops.pp_enable(di->usb_chg, enable); |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * ab8540_chargalg_usb_pre_chg_en() - Enable/ disable USB pre-charge |
| 585 | * @di: pointer to the abx500_chargalg structure |
| 586 | * @enable: USB pre-charge enable/disable |
| 587 | * |
| 588 | * The USB USB pre-charge will be enable/ disable |
| 589 | */ |
| 590 | static int ab8540_chargalg_usb_pre_chg_en(struct abx500_chargalg *di, |
| 591 | bool enable) |
| 592 | { |
| 593 | if (!di->usb_chg || !di->usb_chg->ops.pre_chg_enable) |
| 594 | return -ENXIO; |
| 595 | |
| 596 | return di->usb_chg->ops.pre_chg_enable(di->usb_chg, enable); |
| 597 | } |
| 598 | |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 599 | /** |
| 600 | * abx500_chargalg_update_chg_curr() - Update charger current |
| 601 | * @di: pointer to the abx500_chargalg structure |
| 602 | * @iset: requested charger output current |
| 603 | * |
| 604 | * The charger output current will be updated for the charger |
| 605 | * that is currently in use |
| 606 | */ |
| 607 | static int abx500_chargalg_update_chg_curr(struct abx500_chargalg *di, |
| 608 | int iset) |
| 609 | { |
| 610 | /* Check if charger exists and update current if charging */ |
| 611 | if (di->ac_chg && di->ac_chg->ops.update_curr && |
| 612 | di->chg_info.charger_type & AC_CHG) { |
| 613 | /* |
| 614 | * Select maximum of what both the charger |
| 615 | * and the battery supports |
| 616 | */ |
| 617 | if (di->ac_chg->max_out_curr) |
| 618 | iset = min(iset, di->ac_chg->max_out_curr); |
| 619 | |
| 620 | di->chg_info.ac_iset = iset; |
| 621 | |
| 622 | return di->ac_chg->ops.update_curr(di->ac_chg, iset); |
| 623 | } else if (di->usb_chg && di->usb_chg->ops.update_curr && |
| 624 | di->chg_info.charger_type & USB_CHG) { |
| 625 | /* |
| 626 | * Select maximum of what both the charger |
| 627 | * and the battery supports |
| 628 | */ |
| 629 | if (di->usb_chg->max_out_curr) |
| 630 | iset = min(iset, di->usb_chg->max_out_curr); |
| 631 | |
| 632 | di->chg_info.usb_iset = iset; |
| 633 | |
| 634 | return di->usb_chg->ops.update_curr(di->usb_chg, iset); |
| 635 | } |
| 636 | |
| 637 | return -ENXIO; |
| 638 | } |
| 639 | |
| 640 | /** |
| 641 | * abx500_chargalg_stop_charging() - Stop charging |
| 642 | * @di: pointer to the abx500_chargalg structure |
| 643 | * |
| 644 | * This function is called from any state where charging should be stopped. |
| 645 | * All charging is disabled and all status parameters and timers are changed |
| 646 | * accordingly |
| 647 | */ |
| 648 | static void abx500_chargalg_stop_charging(struct abx500_chargalg *di) |
| 649 | { |
| 650 | abx500_chargalg_ac_en(di, false, 0, 0); |
| 651 | abx500_chargalg_usb_en(di, false, 0, 0); |
| 652 | abx500_chargalg_stop_safety_timer(di); |
| 653 | abx500_chargalg_stop_maintenance_timer(di); |
| 654 | di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 655 | di->maintenance_chg = false; |
| 656 | cancel_delayed_work(&di->chargalg_wd_work); |
| 657 | power_supply_changed(&di->chargalg_psy); |
| 658 | } |
| 659 | |
| 660 | /** |
| 661 | * abx500_chargalg_hold_charging() - Pauses charging |
| 662 | * @di: pointer to the abx500_chargalg structure |
| 663 | * |
| 664 | * This function is called in the case where maintenance charging has been |
| 665 | * disabled and instead a battery voltage mode is entered to check when the |
| 666 | * battery voltage has reached a certain recharge voltage |
| 667 | */ |
| 668 | static void abx500_chargalg_hold_charging(struct abx500_chargalg *di) |
| 669 | { |
| 670 | abx500_chargalg_ac_en(di, false, 0, 0); |
| 671 | abx500_chargalg_usb_en(di, false, 0, 0); |
| 672 | abx500_chargalg_stop_safety_timer(di); |
| 673 | abx500_chargalg_stop_maintenance_timer(di); |
| 674 | di->charge_status = POWER_SUPPLY_STATUS_CHARGING; |
| 675 | di->maintenance_chg = false; |
| 676 | cancel_delayed_work(&di->chargalg_wd_work); |
| 677 | power_supply_changed(&di->chargalg_psy); |
| 678 | } |
| 679 | |
| 680 | /** |
| 681 | * abx500_chargalg_start_charging() - Start the charger |
| 682 | * @di: pointer to the abx500_chargalg structure |
| 683 | * @vset: requested charger output voltage |
| 684 | * @iset: requested charger output current |
| 685 | * |
| 686 | * A charger will be enabled depending on the requested charger type that was |
| 687 | * detected previously. |
| 688 | */ |
| 689 | static void abx500_chargalg_start_charging(struct abx500_chargalg *di, |
| 690 | int vset, int iset) |
| 691 | { |
| 692 | switch (di->chg_info.charger_type) { |
| 693 | case AC_CHG: |
| 694 | dev_dbg(di->dev, |
| 695 | "AC parameters: Vset %d, Ich %d\n", vset, iset); |
| 696 | abx500_chargalg_usb_en(di, false, 0, 0); |
| 697 | abx500_chargalg_ac_en(di, true, vset, iset); |
| 698 | break; |
| 699 | |
| 700 | case USB_CHG: |
| 701 | dev_dbg(di->dev, |
| 702 | "USB parameters: Vset %d, Ich %d\n", vset, iset); |
| 703 | abx500_chargalg_ac_en(di, false, 0, 0); |
| 704 | abx500_chargalg_usb_en(di, true, vset, iset); |
| 705 | break; |
| 706 | |
| 707 | default: |
| 708 | dev_err(di->dev, "Unknown charger to charge from\n"); |
| 709 | break; |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | /** |
| 714 | * abx500_chargalg_check_temp() - Check battery temperature ranges |
| 715 | * @di: pointer to the abx500_chargalg structure |
| 716 | * |
| 717 | * The battery temperature is checked against the predefined limits and the |
| 718 | * charge state is changed accordingly |
| 719 | */ |
| 720 | static void abx500_chargalg_check_temp(struct abx500_chargalg *di) |
| 721 | { |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 722 | if (di->batt_data.temp > (di->bm->temp_low + di->t_hyst_norm) && |
| 723 | di->batt_data.temp < (di->bm->temp_high - di->t_hyst_norm)) { |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 724 | /* Temp OK! */ |
| 725 | di->events.btemp_underover = false; |
| 726 | di->events.btemp_lowhigh = false; |
| 727 | di->t_hyst_norm = 0; |
| 728 | di->t_hyst_lowhigh = 0; |
| 729 | } else { |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 730 | if (((di->batt_data.temp >= di->bm->temp_high) && |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 731 | (di->batt_data.temp < |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 732 | (di->bm->temp_over - di->t_hyst_lowhigh))) || |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 733 | ((di->batt_data.temp > |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 734 | (di->bm->temp_under + di->t_hyst_lowhigh)) && |
| 735 | (di->batt_data.temp <= di->bm->temp_low))) { |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 736 | /* TEMP minor!!!!! */ |
| 737 | di->events.btemp_underover = false; |
| 738 | di->events.btemp_lowhigh = true; |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 739 | di->t_hyst_norm = di->bm->temp_hysteresis; |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 740 | di->t_hyst_lowhigh = 0; |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 741 | } else if (di->batt_data.temp <= di->bm->temp_under || |
| 742 | di->batt_data.temp >= di->bm->temp_over) { |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 743 | /* TEMP major!!!!! */ |
| 744 | di->events.btemp_underover = true; |
| 745 | di->events.btemp_lowhigh = false; |
| 746 | di->t_hyst_norm = 0; |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 747 | di->t_hyst_lowhigh = di->bm->temp_hysteresis; |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 748 | } else { |
| 749 | /* Within hysteresis */ |
| 750 | dev_dbg(di->dev, "Within hysteresis limit temp: %d " |
| 751 | "hyst_lowhigh %d, hyst normal %d\n", |
| 752 | di->batt_data.temp, di->t_hyst_lowhigh, |
| 753 | di->t_hyst_norm); |
| 754 | } |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | /** |
| 759 | * abx500_chargalg_check_charger_voltage() - Check charger voltage |
| 760 | * @di: pointer to the abx500_chargalg structure |
| 761 | * |
| 762 | * Charger voltage is checked against maximum limit |
| 763 | */ |
| 764 | static void abx500_chargalg_check_charger_voltage(struct abx500_chargalg *di) |
| 765 | { |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 766 | if (di->chg_info.usb_volt > di->bm->chg_params->usb_volt_max) |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 767 | di->chg_info.usb_chg_ok = false; |
| 768 | else |
| 769 | di->chg_info.usb_chg_ok = true; |
| 770 | |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 771 | if (di->chg_info.ac_volt > di->bm->chg_params->ac_volt_max) |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 772 | di->chg_info.ac_chg_ok = false; |
| 773 | else |
| 774 | di->chg_info.ac_chg_ok = true; |
| 775 | |
| 776 | } |
| 777 | |
| 778 | /** |
| 779 | * abx500_chargalg_end_of_charge() - Check if end-of-charge criteria is fulfilled |
| 780 | * @di: pointer to the abx500_chargalg structure |
| 781 | * |
| 782 | * End-of-charge criteria is fulfilled when the battery voltage is above a |
| 783 | * certain limit and the battery current is below a certain limit for a |
| 784 | * predefined number of consecutive seconds. If true, the battery is full |
| 785 | */ |
| 786 | static void abx500_chargalg_end_of_charge(struct abx500_chargalg *di) |
| 787 | { |
| 788 | if (di->charge_status == POWER_SUPPLY_STATUS_CHARGING && |
| 789 | di->charge_state == STATE_NORMAL && |
| 790 | !di->maintenance_chg && (di->batt_data.volt >= |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 791 | di->bm->bat_type[di->bm->batt_id].termination_vol || |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 792 | di->events.usb_cv_active || di->events.ac_cv_active) && |
| 793 | di->batt_data.avg_curr < |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 794 | di->bm->bat_type[di->bm->batt_id].termination_curr && |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 795 | di->batt_data.avg_curr > 0) { |
| 796 | if (++di->eoc_cnt >= EOC_COND_CNT) { |
| 797 | di->eoc_cnt = 0; |
Lee Jones | db43e6c | 2013-02-14 12:39:15 +0000 | [diff] [blame] | 798 | if ((di->chg_info.charger_type & USB_CHG) && |
| 799 | (di->usb_chg->power_path)) |
| 800 | ab8540_chargalg_usb_pp_en(di, true); |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 801 | di->charge_status = POWER_SUPPLY_STATUS_FULL; |
| 802 | di->maintenance_chg = true; |
| 803 | dev_dbg(di->dev, "EOC reached!\n"); |
| 804 | power_supply_changed(&di->chargalg_psy); |
| 805 | } else { |
| 806 | dev_dbg(di->dev, |
| 807 | " EOC limit reached for the %d" |
| 808 | " time, out of %d before EOC\n", |
| 809 | di->eoc_cnt, |
| 810 | EOC_COND_CNT); |
| 811 | } |
| 812 | } else { |
| 813 | di->eoc_cnt = 0; |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | static void init_maxim_chg_curr(struct abx500_chargalg *di) |
| 818 | { |
| 819 | di->ccm.original_iset = |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 820 | di->bm->bat_type[di->bm->batt_id].normal_cur_lvl; |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 821 | di->ccm.current_iset = |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 822 | di->bm->bat_type[di->bm->batt_id].normal_cur_lvl; |
| 823 | di->ccm.test_delta_i = di->bm->maxi->charger_curr_step; |
| 824 | di->ccm.max_current = di->bm->maxi->chg_curr; |
| 825 | di->ccm.condition_cnt = di->bm->maxi->wait_cycles; |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 826 | di->ccm.level = 0; |
| 827 | } |
| 828 | |
| 829 | /** |
| 830 | * abx500_chargalg_chg_curr_maxim - increases the charger current to |
| 831 | * compensate for the system load |
| 832 | * @di pointer to the abx500_chargalg structure |
| 833 | * |
| 834 | * This maximization function is used to raise the charger current to get the |
| 835 | * battery current as close to the optimal value as possible. The battery |
| 836 | * current during charging is affected by the system load |
| 837 | */ |
| 838 | static enum maxim_ret abx500_chargalg_chg_curr_maxim(struct abx500_chargalg *di) |
| 839 | { |
| 840 | int delta_i; |
| 841 | |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 842 | if (!di->bm->maxi->ena_maxi) |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 843 | return MAXIM_RET_NOACTION; |
| 844 | |
| 845 | delta_i = di->ccm.original_iset - di->batt_data.inst_curr; |
| 846 | |
| 847 | if (di->events.vbus_collapsed) { |
| 848 | dev_dbg(di->dev, "Charger voltage has collapsed %d\n", |
| 849 | di->ccm.wait_cnt); |
| 850 | if (di->ccm.wait_cnt == 0) { |
| 851 | dev_dbg(di->dev, "lowering current\n"); |
| 852 | di->ccm.wait_cnt++; |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 853 | di->ccm.condition_cnt = di->bm->maxi->wait_cycles; |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 854 | di->ccm.max_current = |
| 855 | di->ccm.current_iset - di->ccm.test_delta_i; |
| 856 | di->ccm.current_iset = di->ccm.max_current; |
| 857 | di->ccm.level--; |
| 858 | return MAXIM_RET_CHANGE; |
| 859 | } else { |
| 860 | dev_dbg(di->dev, "waiting\n"); |
| 861 | /* Let's go in here twice before lowering curr again */ |
| 862 | di->ccm.wait_cnt = (di->ccm.wait_cnt + 1) % 3; |
| 863 | return MAXIM_RET_NOACTION; |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | di->ccm.wait_cnt = 0; |
| 868 | |
| 869 | if ((di->batt_data.inst_curr > di->ccm.original_iset)) { |
| 870 | dev_dbg(di->dev, " Maximization Ibat (%dmA) too high" |
| 871 | " (limit %dmA) (current iset: %dmA)!\n", |
| 872 | di->batt_data.inst_curr, di->ccm.original_iset, |
| 873 | di->ccm.current_iset); |
| 874 | |
| 875 | if (di->ccm.current_iset == di->ccm.original_iset) |
| 876 | return MAXIM_RET_NOACTION; |
| 877 | |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 878 | di->ccm.condition_cnt = di->bm->maxi->wait_cycles; |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 879 | di->ccm.current_iset = di->ccm.original_iset; |
| 880 | di->ccm.level = 0; |
| 881 | |
| 882 | return MAXIM_RET_IBAT_TOO_HIGH; |
| 883 | } |
| 884 | |
| 885 | if (delta_i > di->ccm.test_delta_i && |
| 886 | (di->ccm.current_iset + di->ccm.test_delta_i) < |
| 887 | di->ccm.max_current) { |
| 888 | if (di->ccm.condition_cnt-- == 0) { |
| 889 | /* Increse the iset with cco.test_delta_i */ |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 890 | di->ccm.condition_cnt = di->bm->maxi->wait_cycles; |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 891 | di->ccm.current_iset += di->ccm.test_delta_i; |
| 892 | di->ccm.level++; |
| 893 | dev_dbg(di->dev, " Maximization needed, increase" |
| 894 | " with %d mA to %dmA (Optimal ibat: %d)" |
| 895 | " Level %d\n", |
| 896 | di->ccm.test_delta_i, |
| 897 | di->ccm.current_iset, |
| 898 | di->ccm.original_iset, |
| 899 | di->ccm.level); |
| 900 | return MAXIM_RET_CHANGE; |
| 901 | } else { |
| 902 | return MAXIM_RET_NOACTION; |
| 903 | } |
| 904 | } else { |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 905 | di->ccm.condition_cnt = di->bm->maxi->wait_cycles; |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 906 | return MAXIM_RET_NOACTION; |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | static void handle_maxim_chg_curr(struct abx500_chargalg *di) |
| 911 | { |
| 912 | enum maxim_ret ret; |
| 913 | int result; |
| 914 | |
| 915 | ret = abx500_chargalg_chg_curr_maxim(di); |
| 916 | switch (ret) { |
| 917 | case MAXIM_RET_CHANGE: |
| 918 | result = abx500_chargalg_update_chg_curr(di, |
| 919 | di->ccm.current_iset); |
| 920 | if (result) |
| 921 | dev_err(di->dev, "failed to set chg curr\n"); |
| 922 | break; |
| 923 | case MAXIM_RET_IBAT_TOO_HIGH: |
| 924 | result = abx500_chargalg_update_chg_curr(di, |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 925 | di->bm->bat_type[di->bm->batt_id].normal_cur_lvl); |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 926 | if (result) |
| 927 | dev_err(di->dev, "failed to set chg curr\n"); |
| 928 | break; |
| 929 | |
| 930 | case MAXIM_RET_NOACTION: |
| 931 | default: |
| 932 | /* Do nothing..*/ |
| 933 | break; |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | static int abx500_chargalg_get_ext_psy_data(struct device *dev, void *data) |
| 938 | { |
| 939 | struct power_supply *psy; |
| 940 | struct power_supply *ext; |
| 941 | struct abx500_chargalg *di; |
| 942 | union power_supply_propval ret; |
| 943 | int i, j; |
| 944 | bool psy_found = false; |
Marcus Cooper | ea40240 | 2013-01-11 13:12:54 +0000 | [diff] [blame] | 945 | bool capacity_updated = false; |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 946 | |
| 947 | psy = (struct power_supply *)data; |
| 948 | ext = dev_get_drvdata(dev); |
| 949 | di = to_abx500_chargalg_device_info(psy); |
| 950 | /* For all psy where the driver name appears in any supplied_to */ |
| 951 | for (i = 0; i < ext->num_supplicants; i++) { |
| 952 | if (!strcmp(ext->supplied_to[i], psy->name)) |
| 953 | psy_found = true; |
| 954 | } |
| 955 | if (!psy_found) |
| 956 | return 0; |
| 957 | |
Marcus Cooper | ea40240 | 2013-01-11 13:12:54 +0000 | [diff] [blame] | 958 | /* |
| 959 | * If external is not registering 'POWER_SUPPLY_PROP_CAPACITY' to its |
| 960 | * property because of handling that sysfs entry on its own, this is |
| 961 | * the place to get the battery capacity. |
| 962 | */ |
| 963 | if (!ext->get_property(ext, POWER_SUPPLY_PROP_CAPACITY, &ret)) { |
| 964 | di->batt_data.percent = ret.intval; |
| 965 | capacity_updated = true; |
| 966 | } |
| 967 | |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 968 | /* Go through all properties for the psy */ |
| 969 | for (j = 0; j < ext->num_properties; j++) { |
| 970 | enum power_supply_property prop; |
| 971 | prop = ext->properties[j]; |
| 972 | |
| 973 | /* Initialize chargers if not already done */ |
| 974 | if (!di->ac_chg && |
| 975 | ext->type == POWER_SUPPLY_TYPE_MAINS) |
| 976 | di->ac_chg = psy_to_ux500_charger(ext); |
| 977 | else if (!di->usb_chg && |
| 978 | ext->type == POWER_SUPPLY_TYPE_USB) |
| 979 | di->usb_chg = psy_to_ux500_charger(ext); |
| 980 | |
| 981 | if (ext->get_property(ext, prop, &ret)) |
| 982 | continue; |
| 983 | switch (prop) { |
| 984 | case POWER_SUPPLY_PROP_PRESENT: |
| 985 | switch (ext->type) { |
| 986 | case POWER_SUPPLY_TYPE_BATTERY: |
| 987 | /* Battery present */ |
| 988 | if (ret.intval) |
| 989 | di->events.batt_rem = false; |
| 990 | /* Battery removed */ |
| 991 | else |
| 992 | di->events.batt_rem = true; |
| 993 | break; |
| 994 | case POWER_SUPPLY_TYPE_MAINS: |
| 995 | /* AC disconnected */ |
| 996 | if (!ret.intval && |
| 997 | (di->chg_info.conn_chg & AC_CHG)) { |
| 998 | di->chg_info.prev_conn_chg = |
| 999 | di->chg_info.conn_chg; |
| 1000 | di->chg_info.conn_chg &= ~AC_CHG; |
| 1001 | } |
| 1002 | /* AC connected */ |
| 1003 | else if (ret.intval && |
| 1004 | !(di->chg_info.conn_chg & AC_CHG)) { |
| 1005 | di->chg_info.prev_conn_chg = |
| 1006 | di->chg_info.conn_chg; |
| 1007 | di->chg_info.conn_chg |= AC_CHG; |
| 1008 | } |
| 1009 | break; |
| 1010 | case POWER_SUPPLY_TYPE_USB: |
| 1011 | /* USB disconnected */ |
| 1012 | if (!ret.intval && |
| 1013 | (di->chg_info.conn_chg & USB_CHG)) { |
| 1014 | di->chg_info.prev_conn_chg = |
| 1015 | di->chg_info.conn_chg; |
| 1016 | di->chg_info.conn_chg &= ~USB_CHG; |
| 1017 | } |
| 1018 | /* USB connected */ |
| 1019 | else if (ret.intval && |
| 1020 | !(di->chg_info.conn_chg & USB_CHG)) { |
| 1021 | di->chg_info.prev_conn_chg = |
| 1022 | di->chg_info.conn_chg; |
| 1023 | di->chg_info.conn_chg |= USB_CHG; |
| 1024 | } |
| 1025 | break; |
| 1026 | default: |
| 1027 | break; |
| 1028 | } |
| 1029 | break; |
| 1030 | |
| 1031 | case POWER_SUPPLY_PROP_ONLINE: |
| 1032 | switch (ext->type) { |
| 1033 | case POWER_SUPPLY_TYPE_BATTERY: |
| 1034 | break; |
| 1035 | case POWER_SUPPLY_TYPE_MAINS: |
| 1036 | /* AC offline */ |
| 1037 | if (!ret.intval && |
| 1038 | (di->chg_info.online_chg & AC_CHG)) { |
| 1039 | di->chg_info.prev_online_chg = |
| 1040 | di->chg_info.online_chg; |
| 1041 | di->chg_info.online_chg &= ~AC_CHG; |
| 1042 | } |
| 1043 | /* AC online */ |
| 1044 | else if (ret.intval && |
| 1045 | !(di->chg_info.online_chg & AC_CHG)) { |
| 1046 | di->chg_info.prev_online_chg = |
| 1047 | di->chg_info.online_chg; |
| 1048 | di->chg_info.online_chg |= AC_CHG; |
| 1049 | queue_delayed_work(di->chargalg_wq, |
| 1050 | &di->chargalg_wd_work, 0); |
| 1051 | } |
| 1052 | break; |
| 1053 | case POWER_SUPPLY_TYPE_USB: |
| 1054 | /* USB offline */ |
| 1055 | if (!ret.intval && |
| 1056 | (di->chg_info.online_chg & USB_CHG)) { |
| 1057 | di->chg_info.prev_online_chg = |
| 1058 | di->chg_info.online_chg; |
| 1059 | di->chg_info.online_chg &= ~USB_CHG; |
| 1060 | } |
| 1061 | /* USB online */ |
| 1062 | else if (ret.intval && |
| 1063 | !(di->chg_info.online_chg & USB_CHG)) { |
| 1064 | di->chg_info.prev_online_chg = |
| 1065 | di->chg_info.online_chg; |
| 1066 | di->chg_info.online_chg |= USB_CHG; |
| 1067 | queue_delayed_work(di->chargalg_wq, |
| 1068 | &di->chargalg_wd_work, 0); |
| 1069 | } |
| 1070 | break; |
| 1071 | default: |
| 1072 | break; |
| 1073 | } |
| 1074 | break; |
| 1075 | |
| 1076 | case POWER_SUPPLY_PROP_HEALTH: |
| 1077 | switch (ext->type) { |
| 1078 | case POWER_SUPPLY_TYPE_BATTERY: |
| 1079 | break; |
| 1080 | case POWER_SUPPLY_TYPE_MAINS: |
| 1081 | switch (ret.intval) { |
| 1082 | case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE: |
| 1083 | di->events.mainextchnotok = true; |
| 1084 | di->events.main_thermal_prot = false; |
| 1085 | di->events.main_ovv = false; |
| 1086 | di->events.ac_wd_expired = false; |
| 1087 | break; |
| 1088 | case POWER_SUPPLY_HEALTH_DEAD: |
| 1089 | di->events.ac_wd_expired = true; |
| 1090 | di->events.mainextchnotok = false; |
| 1091 | di->events.main_ovv = false; |
| 1092 | di->events.main_thermal_prot = false; |
| 1093 | break; |
| 1094 | case POWER_SUPPLY_HEALTH_COLD: |
| 1095 | case POWER_SUPPLY_HEALTH_OVERHEAT: |
| 1096 | di->events.main_thermal_prot = true; |
| 1097 | di->events.mainextchnotok = false; |
| 1098 | di->events.main_ovv = false; |
| 1099 | di->events.ac_wd_expired = false; |
| 1100 | break; |
| 1101 | case POWER_SUPPLY_HEALTH_OVERVOLTAGE: |
| 1102 | di->events.main_ovv = true; |
| 1103 | di->events.mainextchnotok = false; |
| 1104 | di->events.main_thermal_prot = false; |
| 1105 | di->events.ac_wd_expired = false; |
| 1106 | break; |
| 1107 | case POWER_SUPPLY_HEALTH_GOOD: |
| 1108 | di->events.main_thermal_prot = false; |
| 1109 | di->events.mainextchnotok = false; |
| 1110 | di->events.main_ovv = false; |
| 1111 | di->events.ac_wd_expired = false; |
| 1112 | break; |
| 1113 | default: |
| 1114 | break; |
| 1115 | } |
| 1116 | break; |
| 1117 | |
| 1118 | case POWER_SUPPLY_TYPE_USB: |
| 1119 | switch (ret.intval) { |
| 1120 | case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE: |
| 1121 | di->events.usbchargernotok = true; |
| 1122 | di->events.usb_thermal_prot = false; |
| 1123 | di->events.vbus_ovv = false; |
| 1124 | di->events.usb_wd_expired = false; |
| 1125 | break; |
| 1126 | case POWER_SUPPLY_HEALTH_DEAD: |
| 1127 | di->events.usb_wd_expired = true; |
| 1128 | di->events.usbchargernotok = false; |
| 1129 | di->events.usb_thermal_prot = false; |
| 1130 | di->events.vbus_ovv = false; |
| 1131 | break; |
| 1132 | case POWER_SUPPLY_HEALTH_COLD: |
| 1133 | case POWER_SUPPLY_HEALTH_OVERHEAT: |
| 1134 | di->events.usb_thermal_prot = true; |
| 1135 | di->events.usbchargernotok = false; |
| 1136 | di->events.vbus_ovv = false; |
| 1137 | di->events.usb_wd_expired = false; |
| 1138 | break; |
| 1139 | case POWER_SUPPLY_HEALTH_OVERVOLTAGE: |
| 1140 | di->events.vbus_ovv = true; |
| 1141 | di->events.usbchargernotok = false; |
| 1142 | di->events.usb_thermal_prot = false; |
| 1143 | di->events.usb_wd_expired = false; |
| 1144 | break; |
| 1145 | case POWER_SUPPLY_HEALTH_GOOD: |
| 1146 | di->events.usbchargernotok = false; |
| 1147 | di->events.usb_thermal_prot = false; |
| 1148 | di->events.vbus_ovv = false; |
| 1149 | di->events.usb_wd_expired = false; |
| 1150 | break; |
| 1151 | default: |
| 1152 | break; |
| 1153 | } |
| 1154 | default: |
| 1155 | break; |
| 1156 | } |
| 1157 | break; |
| 1158 | |
| 1159 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 1160 | switch (ext->type) { |
| 1161 | case POWER_SUPPLY_TYPE_BATTERY: |
| 1162 | di->batt_data.volt = ret.intval / 1000; |
| 1163 | break; |
| 1164 | case POWER_SUPPLY_TYPE_MAINS: |
| 1165 | di->chg_info.ac_volt = ret.intval / 1000; |
| 1166 | break; |
| 1167 | case POWER_SUPPLY_TYPE_USB: |
| 1168 | di->chg_info.usb_volt = ret.intval / 1000; |
| 1169 | break; |
| 1170 | default: |
| 1171 | break; |
| 1172 | } |
| 1173 | break; |
| 1174 | |
| 1175 | case POWER_SUPPLY_PROP_VOLTAGE_AVG: |
| 1176 | switch (ext->type) { |
| 1177 | case POWER_SUPPLY_TYPE_MAINS: |
| 1178 | /* AVG is used to indicate when we are |
| 1179 | * in CV mode */ |
| 1180 | if (ret.intval) |
| 1181 | di->events.ac_cv_active = true; |
| 1182 | else |
| 1183 | di->events.ac_cv_active = false; |
| 1184 | |
| 1185 | break; |
| 1186 | case POWER_SUPPLY_TYPE_USB: |
| 1187 | /* AVG is used to indicate when we are |
| 1188 | * in CV mode */ |
| 1189 | if (ret.intval) |
| 1190 | di->events.usb_cv_active = true; |
| 1191 | else |
| 1192 | di->events.usb_cv_active = false; |
| 1193 | |
| 1194 | break; |
| 1195 | default: |
| 1196 | break; |
| 1197 | } |
| 1198 | break; |
| 1199 | |
| 1200 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 1201 | switch (ext->type) { |
| 1202 | case POWER_SUPPLY_TYPE_BATTERY: |
| 1203 | if (ret.intval) |
| 1204 | di->events.batt_unknown = false; |
| 1205 | else |
| 1206 | di->events.batt_unknown = true; |
| 1207 | |
| 1208 | break; |
| 1209 | default: |
| 1210 | break; |
| 1211 | } |
| 1212 | break; |
| 1213 | |
| 1214 | case POWER_SUPPLY_PROP_TEMP: |
| 1215 | di->batt_data.temp = ret.intval / 10; |
| 1216 | break; |
| 1217 | |
| 1218 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 1219 | switch (ext->type) { |
| 1220 | case POWER_SUPPLY_TYPE_MAINS: |
| 1221 | di->chg_info.ac_curr = |
| 1222 | ret.intval / 1000; |
| 1223 | break; |
| 1224 | case POWER_SUPPLY_TYPE_USB: |
| 1225 | di->chg_info.usb_curr = |
| 1226 | ret.intval / 1000; |
| 1227 | break; |
| 1228 | case POWER_SUPPLY_TYPE_BATTERY: |
| 1229 | di->batt_data.inst_curr = ret.intval / 1000; |
| 1230 | break; |
| 1231 | default: |
| 1232 | break; |
| 1233 | } |
| 1234 | break; |
| 1235 | |
| 1236 | case POWER_SUPPLY_PROP_CURRENT_AVG: |
| 1237 | switch (ext->type) { |
| 1238 | case POWER_SUPPLY_TYPE_BATTERY: |
| 1239 | di->batt_data.avg_curr = ret.intval / 1000; |
| 1240 | break; |
| 1241 | case POWER_SUPPLY_TYPE_USB: |
| 1242 | if (ret.intval) |
| 1243 | di->events.vbus_collapsed = true; |
| 1244 | else |
| 1245 | di->events.vbus_collapsed = false; |
| 1246 | break; |
| 1247 | default: |
| 1248 | break; |
| 1249 | } |
| 1250 | break; |
| 1251 | case POWER_SUPPLY_PROP_CAPACITY: |
Marcus Cooper | ea40240 | 2013-01-11 13:12:54 +0000 | [diff] [blame] | 1252 | if (!capacity_updated) |
| 1253 | di->batt_data.percent = ret.intval; |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 1254 | break; |
| 1255 | default: |
| 1256 | break; |
| 1257 | } |
| 1258 | } |
| 1259 | return 0; |
| 1260 | } |
| 1261 | |
| 1262 | /** |
| 1263 | * abx500_chargalg_external_power_changed() - callback for power supply changes |
| 1264 | * @psy: pointer to the structure power_supply |
| 1265 | * |
| 1266 | * This function is the entry point of the pointer external_power_changed |
| 1267 | * of the structure power_supply. |
| 1268 | * This function gets executed when there is a change in any external power |
| 1269 | * supply that this driver needs to be notified of. |
| 1270 | */ |
| 1271 | static void abx500_chargalg_external_power_changed(struct power_supply *psy) |
| 1272 | { |
| 1273 | struct abx500_chargalg *di = to_abx500_chargalg_device_info(psy); |
| 1274 | |
| 1275 | /* |
| 1276 | * Trigger execution of the algorithm instantly and read |
| 1277 | * all power_supply properties there instead |
| 1278 | */ |
| 1279 | queue_work(di->chargalg_wq, &di->chargalg_work); |
| 1280 | } |
| 1281 | |
| 1282 | /** |
| 1283 | * abx500_chargalg_algorithm() - Main function for the algorithm |
| 1284 | * @di: pointer to the abx500_chargalg structure |
| 1285 | * |
| 1286 | * This is the main control function for the charging algorithm. |
| 1287 | * It is called periodically or when something happens that will |
| 1288 | * trigger a state change |
| 1289 | */ |
| 1290 | static void abx500_chargalg_algorithm(struct abx500_chargalg *di) |
| 1291 | { |
| 1292 | int charger_status; |
Lee Jones | 4dcdf57 | 2013-02-14 09:24:10 +0000 | [diff] [blame] | 1293 | int ret; |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 1294 | |
| 1295 | /* Collect data from all power_supply class devices */ |
| 1296 | class_for_each_device(power_supply_class, NULL, |
| 1297 | &di->chargalg_psy, abx500_chargalg_get_ext_psy_data); |
| 1298 | |
| 1299 | abx500_chargalg_end_of_charge(di); |
| 1300 | abx500_chargalg_check_temp(di); |
| 1301 | abx500_chargalg_check_charger_voltage(di); |
| 1302 | |
| 1303 | charger_status = abx500_chargalg_check_charger_connection(di); |
Lee Jones | 4dcdf57 | 2013-02-14 09:24:10 +0000 | [diff] [blame] | 1304 | |
| 1305 | if (is_ab8500(di->parent)) { |
| 1306 | ret = abx500_chargalg_check_charger_enable(di); |
| 1307 | if (ret < 0) |
| 1308 | dev_err(di->dev, "Checking charger is enabled error" |
| 1309 | ": Returned Value %d\n", ret); |
| 1310 | } |
| 1311 | |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 1312 | /* |
| 1313 | * First check if we have a charger connected. |
| 1314 | * Also we don't allow charging of unknown batteries if configured |
| 1315 | * this way |
| 1316 | */ |
| 1317 | if (!charger_status || |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 1318 | (di->events.batt_unknown && !di->bm->chg_unknown_bat)) { |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 1319 | if (di->charge_state != STATE_HANDHELD) { |
| 1320 | di->events.safety_timer_expired = false; |
| 1321 | abx500_chargalg_state_to(di, STATE_HANDHELD_INIT); |
| 1322 | } |
| 1323 | } |
| 1324 | |
| 1325 | /* If suspended, we should not continue checking the flags */ |
| 1326 | else if (di->charge_state == STATE_SUSPENDED_INIT || |
| 1327 | di->charge_state == STATE_SUSPENDED) { |
| 1328 | /* We don't do anything here, just don,t continue */ |
| 1329 | } |
| 1330 | |
| 1331 | /* Safety timer expiration */ |
| 1332 | else if (di->events.safety_timer_expired) { |
| 1333 | if (di->charge_state != STATE_SAFETY_TIMER_EXPIRED) |
| 1334 | abx500_chargalg_state_to(di, |
| 1335 | STATE_SAFETY_TIMER_EXPIRED_INIT); |
| 1336 | } |
| 1337 | /* |
| 1338 | * Check if any interrupts has occured |
| 1339 | * that will prevent us from charging |
| 1340 | */ |
| 1341 | |
| 1342 | /* Battery removed */ |
| 1343 | else if (di->events.batt_rem) { |
| 1344 | if (di->charge_state != STATE_BATT_REMOVED) |
| 1345 | abx500_chargalg_state_to(di, STATE_BATT_REMOVED_INIT); |
| 1346 | } |
| 1347 | /* Main or USB charger not ok. */ |
| 1348 | else if (di->events.mainextchnotok || di->events.usbchargernotok) { |
| 1349 | /* |
| 1350 | * If vbus_collapsed is set, we have to lower the charger |
| 1351 | * current, which is done in the normal state below |
| 1352 | */ |
| 1353 | if (di->charge_state != STATE_CHG_NOT_OK && |
| 1354 | !di->events.vbus_collapsed) |
| 1355 | abx500_chargalg_state_to(di, STATE_CHG_NOT_OK_INIT); |
| 1356 | } |
| 1357 | /* VBUS, Main or VBAT OVV. */ |
| 1358 | else if (di->events.vbus_ovv || |
| 1359 | di->events.main_ovv || |
| 1360 | di->events.batt_ovv || |
| 1361 | !di->chg_info.usb_chg_ok || |
| 1362 | !di->chg_info.ac_chg_ok) { |
| 1363 | if (di->charge_state != STATE_OVV_PROTECT) |
| 1364 | abx500_chargalg_state_to(di, STATE_OVV_PROTECT_INIT); |
| 1365 | } |
| 1366 | /* USB Thermal, stop charging */ |
| 1367 | else if (di->events.main_thermal_prot || |
| 1368 | di->events.usb_thermal_prot) { |
| 1369 | if (di->charge_state != STATE_HW_TEMP_PROTECT) |
| 1370 | abx500_chargalg_state_to(di, |
| 1371 | STATE_HW_TEMP_PROTECT_INIT); |
| 1372 | } |
| 1373 | /* Battery temp over/under */ |
| 1374 | else if (di->events.btemp_underover) { |
| 1375 | if (di->charge_state != STATE_TEMP_UNDEROVER) |
| 1376 | abx500_chargalg_state_to(di, |
| 1377 | STATE_TEMP_UNDEROVER_INIT); |
| 1378 | } |
| 1379 | /* Watchdog expired */ |
| 1380 | else if (di->events.ac_wd_expired || |
| 1381 | di->events.usb_wd_expired) { |
| 1382 | if (di->charge_state != STATE_WD_EXPIRED) |
| 1383 | abx500_chargalg_state_to(di, STATE_WD_EXPIRED_INIT); |
| 1384 | } |
| 1385 | /* Battery temp high/low */ |
| 1386 | else if (di->events.btemp_lowhigh) { |
| 1387 | if (di->charge_state != STATE_TEMP_LOWHIGH) |
| 1388 | abx500_chargalg_state_to(di, STATE_TEMP_LOWHIGH_INIT); |
| 1389 | } |
| 1390 | |
| 1391 | dev_dbg(di->dev, |
| 1392 | "[CHARGALG] Vb %d Ib_avg %d Ib_inst %d Tb %d Cap %d Maint %d " |
| 1393 | "State %s Active_chg %d Chg_status %d AC %d USB %d " |
| 1394 | "AC_online %d USB_online %d AC_CV %d USB_CV %d AC_I %d " |
| 1395 | "USB_I %d AC_Vset %d AC_Iset %d USB_Vset %d USB_Iset %d\n", |
| 1396 | di->batt_data.volt, |
| 1397 | di->batt_data.avg_curr, |
| 1398 | di->batt_data.inst_curr, |
| 1399 | di->batt_data.temp, |
| 1400 | di->batt_data.percent, |
| 1401 | di->maintenance_chg, |
| 1402 | states[di->charge_state], |
| 1403 | di->chg_info.charger_type, |
| 1404 | di->charge_status, |
| 1405 | di->chg_info.conn_chg & AC_CHG, |
| 1406 | di->chg_info.conn_chg & USB_CHG, |
| 1407 | di->chg_info.online_chg & AC_CHG, |
| 1408 | di->chg_info.online_chg & USB_CHG, |
| 1409 | di->events.ac_cv_active, |
| 1410 | di->events.usb_cv_active, |
| 1411 | di->chg_info.ac_curr, |
| 1412 | di->chg_info.usb_curr, |
| 1413 | di->chg_info.ac_vset, |
| 1414 | di->chg_info.ac_iset, |
| 1415 | di->chg_info.usb_vset, |
| 1416 | di->chg_info.usb_iset); |
| 1417 | |
| 1418 | switch (di->charge_state) { |
| 1419 | case STATE_HANDHELD_INIT: |
| 1420 | abx500_chargalg_stop_charging(di); |
| 1421 | di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING; |
| 1422 | abx500_chargalg_state_to(di, STATE_HANDHELD); |
| 1423 | /* Intentional fallthrough */ |
| 1424 | |
| 1425 | case STATE_HANDHELD: |
| 1426 | break; |
| 1427 | |
| 1428 | case STATE_SUSPENDED_INIT: |
| 1429 | if (di->susp_status.ac_suspended) |
| 1430 | abx500_chargalg_ac_en(di, false, 0, 0); |
| 1431 | if (di->susp_status.usb_suspended) |
| 1432 | abx500_chargalg_usb_en(di, false, 0, 0); |
| 1433 | abx500_chargalg_stop_safety_timer(di); |
| 1434 | abx500_chargalg_stop_maintenance_timer(di); |
| 1435 | di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 1436 | di->maintenance_chg = false; |
| 1437 | abx500_chargalg_state_to(di, STATE_SUSPENDED); |
| 1438 | power_supply_changed(&di->chargalg_psy); |
| 1439 | /* Intentional fallthrough */ |
| 1440 | |
| 1441 | case STATE_SUSPENDED: |
| 1442 | /* CHARGING is suspended */ |
| 1443 | break; |
| 1444 | |
| 1445 | case STATE_BATT_REMOVED_INIT: |
| 1446 | abx500_chargalg_stop_charging(di); |
| 1447 | abx500_chargalg_state_to(di, STATE_BATT_REMOVED); |
| 1448 | /* Intentional fallthrough */ |
| 1449 | |
| 1450 | case STATE_BATT_REMOVED: |
| 1451 | if (!di->events.batt_rem) |
| 1452 | abx500_chargalg_state_to(di, STATE_NORMAL_INIT); |
| 1453 | break; |
| 1454 | |
| 1455 | case STATE_HW_TEMP_PROTECT_INIT: |
| 1456 | abx500_chargalg_stop_charging(di); |
| 1457 | abx500_chargalg_state_to(di, STATE_HW_TEMP_PROTECT); |
| 1458 | /* Intentional fallthrough */ |
| 1459 | |
| 1460 | case STATE_HW_TEMP_PROTECT: |
| 1461 | if (!di->events.main_thermal_prot && |
| 1462 | !di->events.usb_thermal_prot) |
| 1463 | abx500_chargalg_state_to(di, STATE_NORMAL_INIT); |
| 1464 | break; |
| 1465 | |
| 1466 | case STATE_OVV_PROTECT_INIT: |
| 1467 | abx500_chargalg_stop_charging(di); |
| 1468 | abx500_chargalg_state_to(di, STATE_OVV_PROTECT); |
| 1469 | /* Intentional fallthrough */ |
| 1470 | |
| 1471 | case STATE_OVV_PROTECT: |
| 1472 | if (!di->events.vbus_ovv && |
| 1473 | !di->events.main_ovv && |
| 1474 | !di->events.batt_ovv && |
| 1475 | di->chg_info.usb_chg_ok && |
| 1476 | di->chg_info.ac_chg_ok) |
| 1477 | abx500_chargalg_state_to(di, STATE_NORMAL_INIT); |
| 1478 | break; |
| 1479 | |
| 1480 | case STATE_CHG_NOT_OK_INIT: |
| 1481 | abx500_chargalg_stop_charging(di); |
| 1482 | abx500_chargalg_state_to(di, STATE_CHG_NOT_OK); |
| 1483 | /* Intentional fallthrough */ |
| 1484 | |
| 1485 | case STATE_CHG_NOT_OK: |
| 1486 | if (!di->events.mainextchnotok && |
| 1487 | !di->events.usbchargernotok) |
| 1488 | abx500_chargalg_state_to(di, STATE_NORMAL_INIT); |
| 1489 | break; |
| 1490 | |
| 1491 | case STATE_SAFETY_TIMER_EXPIRED_INIT: |
| 1492 | abx500_chargalg_stop_charging(di); |
| 1493 | abx500_chargalg_state_to(di, STATE_SAFETY_TIMER_EXPIRED); |
| 1494 | /* Intentional fallthrough */ |
| 1495 | |
| 1496 | case STATE_SAFETY_TIMER_EXPIRED: |
| 1497 | /* We exit this state when charger is removed */ |
| 1498 | break; |
| 1499 | |
| 1500 | case STATE_NORMAL_INIT: |
Lee Jones | db43e6c | 2013-02-14 12:39:15 +0000 | [diff] [blame] | 1501 | if ((di->chg_info.charger_type & USB_CHG) && |
| 1502 | di->usb_chg->power_path) { |
| 1503 | if (di->batt_data.volt > |
| 1504 | (di->bm->fg_params->lowbat_threshold + |
| 1505 | BAT_PLUS_MARGIN)) { |
| 1506 | ab8540_chargalg_usb_pre_chg_en(di, false); |
| 1507 | ab8540_chargalg_usb_pp_en(di, false); |
| 1508 | } else { |
| 1509 | ab8540_chargalg_usb_pp_en(di, true); |
| 1510 | ab8540_chargalg_usb_pre_chg_en(di, true); |
| 1511 | abx500_chargalg_state_to(di, |
| 1512 | STATE_USB_PP_PRE_CHARGE); |
| 1513 | break; |
| 1514 | } |
| 1515 | } |
| 1516 | |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 1517 | abx500_chargalg_start_charging(di, |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 1518 | di->bm->bat_type[di->bm->batt_id].normal_vol_lvl, |
| 1519 | di->bm->bat_type[di->bm->batt_id].normal_cur_lvl); |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 1520 | abx500_chargalg_state_to(di, STATE_NORMAL); |
| 1521 | abx500_chargalg_start_safety_timer(di); |
| 1522 | abx500_chargalg_stop_maintenance_timer(di); |
| 1523 | init_maxim_chg_curr(di); |
| 1524 | di->charge_status = POWER_SUPPLY_STATUS_CHARGING; |
| 1525 | di->eoc_cnt = 0; |
| 1526 | di->maintenance_chg = false; |
| 1527 | power_supply_changed(&di->chargalg_psy); |
| 1528 | |
| 1529 | break; |
| 1530 | |
Lee Jones | db43e6c | 2013-02-14 12:39:15 +0000 | [diff] [blame] | 1531 | case STATE_USB_PP_PRE_CHARGE: |
| 1532 | if (di->batt_data.volt > |
| 1533 | (di->bm->fg_params->lowbat_threshold + |
| 1534 | BAT_PLUS_MARGIN)) |
| 1535 | abx500_chargalg_state_to(di, STATE_NORMAL_INIT); |
| 1536 | break; |
| 1537 | |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 1538 | case STATE_NORMAL: |
| 1539 | handle_maxim_chg_curr(di); |
| 1540 | if (di->charge_status == POWER_SUPPLY_STATUS_FULL && |
| 1541 | di->maintenance_chg) { |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 1542 | if (di->bm->no_maintenance) |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 1543 | abx500_chargalg_state_to(di, |
| 1544 | STATE_WAIT_FOR_RECHARGE_INIT); |
| 1545 | else |
| 1546 | abx500_chargalg_state_to(di, |
| 1547 | STATE_MAINTENANCE_A_INIT); |
| 1548 | } |
| 1549 | break; |
| 1550 | |
| 1551 | /* This state will be used when the maintenance state is disabled */ |
| 1552 | case STATE_WAIT_FOR_RECHARGE_INIT: |
| 1553 | abx500_chargalg_hold_charging(di); |
| 1554 | abx500_chargalg_state_to(di, STATE_WAIT_FOR_RECHARGE); |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 1555 | /* Intentional fallthrough */ |
| 1556 | |
| 1557 | case STATE_WAIT_FOR_RECHARGE: |
Marcus Cooper | ea40240 | 2013-01-11 13:12:54 +0000 | [diff] [blame] | 1558 | if (di->batt_data.percent <= |
| 1559 | di->bm->bat_type[di->bm->batt_id]. |
| 1560 | recharge_cap) |
| 1561 | abx500_chargalg_state_to(di, STATE_NORMAL_INIT); |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 1562 | break; |
| 1563 | |
| 1564 | case STATE_MAINTENANCE_A_INIT: |
| 1565 | abx500_chargalg_stop_safety_timer(di); |
| 1566 | abx500_chargalg_start_maintenance_timer(di, |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 1567 | di->bm->bat_type[ |
| 1568 | di->bm->batt_id].maint_a_chg_timer_h); |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 1569 | abx500_chargalg_start_charging(di, |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 1570 | di->bm->bat_type[ |
| 1571 | di->bm->batt_id].maint_a_vol_lvl, |
| 1572 | di->bm->bat_type[ |
| 1573 | di->bm->batt_id].maint_a_cur_lvl); |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 1574 | abx500_chargalg_state_to(di, STATE_MAINTENANCE_A); |
| 1575 | power_supply_changed(&di->chargalg_psy); |
| 1576 | /* Intentional fallthrough*/ |
| 1577 | |
| 1578 | case STATE_MAINTENANCE_A: |
| 1579 | if (di->events.maintenance_timer_expired) { |
| 1580 | abx500_chargalg_stop_maintenance_timer(di); |
| 1581 | abx500_chargalg_state_to(di, STATE_MAINTENANCE_B_INIT); |
| 1582 | } |
| 1583 | break; |
| 1584 | |
| 1585 | case STATE_MAINTENANCE_B_INIT: |
| 1586 | abx500_chargalg_start_maintenance_timer(di, |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 1587 | di->bm->bat_type[ |
| 1588 | di->bm->batt_id].maint_b_chg_timer_h); |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 1589 | abx500_chargalg_start_charging(di, |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 1590 | di->bm->bat_type[ |
| 1591 | di->bm->batt_id].maint_b_vol_lvl, |
| 1592 | di->bm->bat_type[ |
| 1593 | di->bm->batt_id].maint_b_cur_lvl); |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 1594 | abx500_chargalg_state_to(di, STATE_MAINTENANCE_B); |
| 1595 | power_supply_changed(&di->chargalg_psy); |
| 1596 | /* Intentional fallthrough*/ |
| 1597 | |
| 1598 | case STATE_MAINTENANCE_B: |
| 1599 | if (di->events.maintenance_timer_expired) { |
| 1600 | abx500_chargalg_stop_maintenance_timer(di); |
| 1601 | abx500_chargalg_state_to(di, STATE_NORMAL_INIT); |
| 1602 | } |
| 1603 | break; |
| 1604 | |
| 1605 | case STATE_TEMP_LOWHIGH_INIT: |
| 1606 | abx500_chargalg_start_charging(di, |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 1607 | di->bm->bat_type[ |
| 1608 | di->bm->batt_id].low_high_vol_lvl, |
| 1609 | di->bm->bat_type[ |
| 1610 | di->bm->batt_id].low_high_cur_lvl); |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 1611 | abx500_chargalg_stop_maintenance_timer(di); |
| 1612 | di->charge_status = POWER_SUPPLY_STATUS_CHARGING; |
| 1613 | abx500_chargalg_state_to(di, STATE_TEMP_LOWHIGH); |
| 1614 | power_supply_changed(&di->chargalg_psy); |
| 1615 | /* Intentional fallthrough */ |
| 1616 | |
| 1617 | case STATE_TEMP_LOWHIGH: |
| 1618 | if (!di->events.btemp_lowhigh) |
| 1619 | abx500_chargalg_state_to(di, STATE_NORMAL_INIT); |
| 1620 | break; |
| 1621 | |
| 1622 | case STATE_WD_EXPIRED_INIT: |
| 1623 | abx500_chargalg_stop_charging(di); |
| 1624 | abx500_chargalg_state_to(di, STATE_WD_EXPIRED); |
| 1625 | /* Intentional fallthrough */ |
| 1626 | |
| 1627 | case STATE_WD_EXPIRED: |
| 1628 | if (!di->events.ac_wd_expired && |
| 1629 | !di->events.usb_wd_expired) |
| 1630 | abx500_chargalg_state_to(di, STATE_NORMAL_INIT); |
| 1631 | break; |
| 1632 | |
| 1633 | case STATE_TEMP_UNDEROVER_INIT: |
| 1634 | abx500_chargalg_stop_charging(di); |
| 1635 | abx500_chargalg_state_to(di, STATE_TEMP_UNDEROVER); |
| 1636 | /* Intentional fallthrough */ |
| 1637 | |
| 1638 | case STATE_TEMP_UNDEROVER: |
| 1639 | if (!di->events.btemp_underover) |
| 1640 | abx500_chargalg_state_to(di, STATE_NORMAL_INIT); |
| 1641 | break; |
| 1642 | } |
| 1643 | |
| 1644 | /* Start charging directly if the new state is a charge state */ |
| 1645 | if (di->charge_state == STATE_NORMAL_INIT || |
| 1646 | di->charge_state == STATE_MAINTENANCE_A_INIT || |
| 1647 | di->charge_state == STATE_MAINTENANCE_B_INIT) |
| 1648 | queue_work(di->chargalg_wq, &di->chargalg_work); |
| 1649 | } |
| 1650 | |
| 1651 | /** |
| 1652 | * abx500_chargalg_periodic_work() - Periodic work for the algorithm |
| 1653 | * @work: pointer to the work_struct structure |
| 1654 | * |
| 1655 | * Work queue function for the charging algorithm |
| 1656 | */ |
| 1657 | static void abx500_chargalg_periodic_work(struct work_struct *work) |
| 1658 | { |
| 1659 | struct abx500_chargalg *di = container_of(work, |
| 1660 | struct abx500_chargalg, chargalg_periodic_work.work); |
| 1661 | |
| 1662 | abx500_chargalg_algorithm(di); |
| 1663 | |
| 1664 | /* |
| 1665 | * If a charger is connected then the battery has to be monitored |
| 1666 | * frequently, else the work can be delayed. |
| 1667 | */ |
| 1668 | if (di->chg_info.conn_chg) |
| 1669 | queue_delayed_work(di->chargalg_wq, |
| 1670 | &di->chargalg_periodic_work, |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 1671 | di->bm->interval_charging * HZ); |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 1672 | else |
| 1673 | queue_delayed_work(di->chargalg_wq, |
| 1674 | &di->chargalg_periodic_work, |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 1675 | di->bm->interval_not_charging * HZ); |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 1676 | } |
| 1677 | |
| 1678 | /** |
| 1679 | * abx500_chargalg_wd_work() - periodic work to kick the charger watchdog |
| 1680 | * @work: pointer to the work_struct structure |
| 1681 | * |
| 1682 | * Work queue function for kicking the charger watchdog |
| 1683 | */ |
| 1684 | static void abx500_chargalg_wd_work(struct work_struct *work) |
| 1685 | { |
| 1686 | int ret; |
| 1687 | struct abx500_chargalg *di = container_of(work, |
| 1688 | struct abx500_chargalg, chargalg_wd_work.work); |
| 1689 | |
| 1690 | dev_dbg(di->dev, "abx500_chargalg_wd_work\n"); |
| 1691 | |
| 1692 | ret = abx500_chargalg_kick_watchdog(di); |
| 1693 | if (ret < 0) |
| 1694 | dev_err(di->dev, "failed to kick watchdog\n"); |
| 1695 | |
| 1696 | queue_delayed_work(di->chargalg_wq, |
| 1697 | &di->chargalg_wd_work, CHG_WD_INTERVAL); |
| 1698 | } |
| 1699 | |
| 1700 | /** |
| 1701 | * abx500_chargalg_work() - Work to run the charging algorithm instantly |
| 1702 | * @work: pointer to the work_struct structure |
| 1703 | * |
| 1704 | * Work queue function for calling the charging algorithm |
| 1705 | */ |
| 1706 | static void abx500_chargalg_work(struct work_struct *work) |
| 1707 | { |
| 1708 | struct abx500_chargalg *di = container_of(work, |
| 1709 | struct abx500_chargalg, chargalg_work); |
| 1710 | |
| 1711 | abx500_chargalg_algorithm(di); |
| 1712 | } |
| 1713 | |
| 1714 | /** |
| 1715 | * abx500_chargalg_get_property() - get the chargalg properties |
| 1716 | * @psy: pointer to the power_supply structure |
| 1717 | * @psp: pointer to the power_supply_property structure |
| 1718 | * @val: pointer to the power_supply_propval union |
| 1719 | * |
| 1720 | * This function gets called when an application tries to get the |
| 1721 | * chargalg properties by reading the sysfs files. |
| 1722 | * status: charging/discharging/full/unknown |
| 1723 | * health: health of the battery |
| 1724 | * Returns error code in case of failure else 0 on success |
| 1725 | */ |
| 1726 | static int abx500_chargalg_get_property(struct power_supply *psy, |
| 1727 | enum power_supply_property psp, |
| 1728 | union power_supply_propval *val) |
| 1729 | { |
| 1730 | struct abx500_chargalg *di; |
| 1731 | |
| 1732 | di = to_abx500_chargalg_device_info(psy); |
| 1733 | |
| 1734 | switch (psp) { |
| 1735 | case POWER_SUPPLY_PROP_STATUS: |
| 1736 | val->intval = di->charge_status; |
| 1737 | break; |
| 1738 | case POWER_SUPPLY_PROP_HEALTH: |
| 1739 | if (di->events.batt_ovv) { |
| 1740 | val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE; |
| 1741 | } else if (di->events.btemp_underover) { |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 1742 | if (di->batt_data.temp <= di->bm->temp_under) |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 1743 | val->intval = POWER_SUPPLY_HEALTH_COLD; |
| 1744 | else |
| 1745 | val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; |
Lee Jones | d80108f | 2013-01-17 13:49:45 +0000 | [diff] [blame] | 1746 | } else if (di->charge_state == STATE_SAFETY_TIMER_EXPIRED || |
| 1747 | di->charge_state == STATE_SAFETY_TIMER_EXPIRED_INIT) { |
| 1748 | val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 1749 | } else { |
| 1750 | val->intval = POWER_SUPPLY_HEALTH_GOOD; |
| 1751 | } |
| 1752 | break; |
| 1753 | default: |
| 1754 | return -EINVAL; |
| 1755 | } |
| 1756 | return 0; |
| 1757 | } |
| 1758 | |
| 1759 | /* Exposure to the sysfs interface */ |
| 1760 | |
| 1761 | /** |
Lee Jones | c9ade0f | 2013-01-21 11:22:56 +0000 | [diff] [blame] | 1762 | * abx500_chargalg_sysfs_show() - sysfs show operations |
| 1763 | * @kobj: pointer to the struct kobject |
| 1764 | * @attr: pointer to the struct attribute |
| 1765 | * @buf: buffer that holds the parameter to send to userspace |
| 1766 | * |
| 1767 | * Returns a buffer to be displayed in user space |
| 1768 | */ |
| 1769 | static ssize_t abx500_chargalg_sysfs_show(struct kobject *kobj, |
| 1770 | struct attribute *attr, char *buf) |
| 1771 | { |
| 1772 | struct abx500_chargalg *di = container_of(kobj, |
| 1773 | struct abx500_chargalg, chargalg_kobject); |
| 1774 | |
| 1775 | return sprintf(buf, "%d\n", |
| 1776 | di->susp_status.ac_suspended && |
| 1777 | di->susp_status.usb_suspended); |
| 1778 | } |
| 1779 | |
| 1780 | /** |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 1781 | * abx500_chargalg_sysfs_charger() - sysfs store operations |
| 1782 | * @kobj: pointer to the struct kobject |
| 1783 | * @attr: pointer to the struct attribute |
| 1784 | * @buf: buffer that holds the parameter passed from userspace |
| 1785 | * @length: length of the parameter passed |
| 1786 | * |
| 1787 | * Returns length of the buffer(input taken from user space) on success |
| 1788 | * else error code on failure |
| 1789 | * The operation to be performed on passing the parameters from the user space. |
| 1790 | */ |
| 1791 | static ssize_t abx500_chargalg_sysfs_charger(struct kobject *kobj, |
| 1792 | struct attribute *attr, const char *buf, size_t length) |
| 1793 | { |
| 1794 | struct abx500_chargalg *di = container_of(kobj, |
| 1795 | struct abx500_chargalg, chargalg_kobject); |
| 1796 | long int param; |
| 1797 | int ac_usb; |
| 1798 | int ret; |
| 1799 | char entry = *attr->name; |
| 1800 | |
| 1801 | switch (entry) { |
| 1802 | case 'c': |
| 1803 | ret = strict_strtol(buf, 10, ¶m); |
| 1804 | if (ret < 0) |
| 1805 | return ret; |
| 1806 | |
| 1807 | ac_usb = param; |
| 1808 | switch (ac_usb) { |
| 1809 | case 0: |
| 1810 | /* Disable charging */ |
| 1811 | di->susp_status.ac_suspended = true; |
| 1812 | di->susp_status.usb_suspended = true; |
| 1813 | di->susp_status.suspended_change = true; |
| 1814 | /* Trigger a state change */ |
| 1815 | queue_work(di->chargalg_wq, |
| 1816 | &di->chargalg_work); |
| 1817 | break; |
| 1818 | case 1: |
| 1819 | /* Enable AC Charging */ |
| 1820 | di->susp_status.ac_suspended = false; |
| 1821 | di->susp_status.suspended_change = true; |
| 1822 | /* Trigger a state change */ |
| 1823 | queue_work(di->chargalg_wq, |
| 1824 | &di->chargalg_work); |
| 1825 | break; |
| 1826 | case 2: |
| 1827 | /* Enable USB charging */ |
| 1828 | di->susp_status.usb_suspended = false; |
| 1829 | di->susp_status.suspended_change = true; |
| 1830 | /* Trigger a state change */ |
| 1831 | queue_work(di->chargalg_wq, |
| 1832 | &di->chargalg_work); |
| 1833 | break; |
| 1834 | default: |
| 1835 | dev_info(di->dev, "Wrong input\n" |
| 1836 | "Enter 0. Disable AC/USB Charging\n" |
| 1837 | "1. Enable AC charging\n" |
| 1838 | "2. Enable USB Charging\n"); |
| 1839 | }; |
| 1840 | break; |
| 1841 | }; |
| 1842 | return strlen(buf); |
| 1843 | } |
| 1844 | |
| 1845 | static struct attribute abx500_chargalg_en_charger = \ |
| 1846 | { |
| 1847 | .name = "chargalg", |
Lee Jones | c9ade0f | 2013-01-21 11:22:56 +0000 | [diff] [blame] | 1848 | .mode = S_IRUGO | S_IWUSR, |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 1849 | }; |
| 1850 | |
| 1851 | static struct attribute *abx500_chargalg_chg[] = { |
| 1852 | &abx500_chargalg_en_charger, |
| 1853 | NULL |
| 1854 | }; |
| 1855 | |
Anton Vorontsov | 64eb9b0 | 2012-03-14 04:43:11 +0400 | [diff] [blame] | 1856 | static const struct sysfs_ops abx500_chargalg_sysfs_ops = { |
Lee Jones | c9ade0f | 2013-01-21 11:22:56 +0000 | [diff] [blame] | 1857 | .show = abx500_chargalg_sysfs_show, |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 1858 | .store = abx500_chargalg_sysfs_charger, |
| 1859 | }; |
| 1860 | |
| 1861 | static struct kobj_type abx500_chargalg_ktype = { |
| 1862 | .sysfs_ops = &abx500_chargalg_sysfs_ops, |
| 1863 | .default_attrs = abx500_chargalg_chg, |
| 1864 | }; |
| 1865 | |
| 1866 | /** |
| 1867 | * abx500_chargalg_sysfs_exit() - de-init of sysfs entry |
| 1868 | * @di: pointer to the struct abx500_chargalg |
| 1869 | * |
| 1870 | * This function removes the entry in sysfs. |
| 1871 | */ |
| 1872 | static void abx500_chargalg_sysfs_exit(struct abx500_chargalg *di) |
| 1873 | { |
| 1874 | kobject_del(&di->chargalg_kobject); |
| 1875 | } |
| 1876 | |
| 1877 | /** |
| 1878 | * abx500_chargalg_sysfs_init() - init of sysfs entry |
| 1879 | * @di: pointer to the struct abx500_chargalg |
| 1880 | * |
| 1881 | * This function adds an entry in sysfs. |
| 1882 | * Returns error code in case of failure else 0(on success) |
| 1883 | */ |
| 1884 | static int abx500_chargalg_sysfs_init(struct abx500_chargalg *di) |
| 1885 | { |
| 1886 | int ret = 0; |
| 1887 | |
| 1888 | ret = kobject_init_and_add(&di->chargalg_kobject, |
| 1889 | &abx500_chargalg_ktype, |
| 1890 | NULL, "abx500_chargalg"); |
| 1891 | if (ret < 0) |
| 1892 | dev_err(di->dev, "failed to create sysfs entry\n"); |
| 1893 | |
| 1894 | return ret; |
| 1895 | } |
| 1896 | /* Exposure to the sysfs interface <<END>> */ |
| 1897 | |
| 1898 | #if defined(CONFIG_PM) |
| 1899 | static int abx500_chargalg_resume(struct platform_device *pdev) |
| 1900 | { |
| 1901 | struct abx500_chargalg *di = platform_get_drvdata(pdev); |
| 1902 | |
| 1903 | /* Kick charger watchdog if charging (any charger online) */ |
| 1904 | if (di->chg_info.online_chg) |
| 1905 | queue_delayed_work(di->chargalg_wq, &di->chargalg_wd_work, 0); |
| 1906 | |
| 1907 | /* |
| 1908 | * Run the charging algorithm directly to be sure we don't |
| 1909 | * do it too seldom |
| 1910 | */ |
| 1911 | queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0); |
| 1912 | |
| 1913 | return 0; |
| 1914 | } |
| 1915 | |
| 1916 | static int abx500_chargalg_suspend(struct platform_device *pdev, |
| 1917 | pm_message_t state) |
| 1918 | { |
| 1919 | struct abx500_chargalg *di = platform_get_drvdata(pdev); |
| 1920 | |
| 1921 | if (di->chg_info.online_chg) |
| 1922 | cancel_delayed_work_sync(&di->chargalg_wd_work); |
| 1923 | |
| 1924 | cancel_delayed_work_sync(&di->chargalg_periodic_work); |
| 1925 | |
| 1926 | return 0; |
| 1927 | } |
| 1928 | #else |
| 1929 | #define abx500_chargalg_suspend NULL |
| 1930 | #define abx500_chargalg_resume NULL |
| 1931 | #endif |
| 1932 | |
Bill Pemberton | 415ec69 | 2012-11-19 13:26:07 -0500 | [diff] [blame] | 1933 | static int abx500_chargalg_remove(struct platform_device *pdev) |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 1934 | { |
| 1935 | struct abx500_chargalg *di = platform_get_drvdata(pdev); |
| 1936 | |
| 1937 | /* sysfs interface to enable/disbale charging from user space */ |
| 1938 | abx500_chargalg_sysfs_exit(di); |
| 1939 | |
| 1940 | /* Delete the work queue */ |
| 1941 | destroy_workqueue(di->chargalg_wq); |
| 1942 | |
| 1943 | flush_scheduled_work(); |
| 1944 | power_supply_unregister(&di->chargalg_psy); |
| 1945 | platform_set_drvdata(pdev, NULL); |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 1946 | |
| 1947 | return 0; |
| 1948 | } |
| 1949 | |
Rajanikanth H.V | a12810a | 2012-10-31 15:40:33 +0000 | [diff] [blame] | 1950 | static char *supply_interface[] = { |
| 1951 | "ab8500_fg", |
| 1952 | }; |
| 1953 | |
Bill Pemberton | c8afa64 | 2012-11-19 13:22:23 -0500 | [diff] [blame] | 1954 | static int abx500_chargalg_probe(struct platform_device *pdev) |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 1955 | { |
Rajanikanth H.V | a12810a | 2012-10-31 15:40:33 +0000 | [diff] [blame] | 1956 | struct device_node *np = pdev->dev.of_node; |
Lee Jones | bdc56b4 | 2012-11-30 10:57:14 +0000 | [diff] [blame] | 1957 | struct abx500_bm_data *plat = pdev->dev.platform_data; |
Rajanikanth H.V | a12810a | 2012-10-31 15:40:33 +0000 | [diff] [blame] | 1958 | struct abx500_chargalg *di; |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 1959 | int ret = 0; |
| 1960 | |
Rajanikanth H.V | a12810a | 2012-10-31 15:40:33 +0000 | [diff] [blame] | 1961 | di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL); |
| 1962 | if (!di) { |
| 1963 | dev_err(&pdev->dev, "%s no mem for ab8500_chargalg\n", __func__); |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 1964 | return -ENOMEM; |
Rajanikanth H.V | a12810a | 2012-10-31 15:40:33 +0000 | [diff] [blame] | 1965 | } |
Lee Jones | bdc56b4 | 2012-11-30 10:57:14 +0000 | [diff] [blame] | 1966 | |
| 1967 | if (!plat) { |
| 1968 | dev_err(&pdev->dev, "no battery management data supplied\n"); |
| 1969 | return -EINVAL; |
| 1970 | } |
| 1971 | di->bm = plat; |
| 1972 | |
| 1973 | if (np) { |
| 1974 | ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm); |
| 1975 | if (ret) { |
| 1976 | dev_err(&pdev->dev, "failed to get battery information\n"); |
| 1977 | return ret; |
Rajanikanth H.V | a12810a | 2012-10-31 15:40:33 +0000 | [diff] [blame] | 1978 | } |
Rajanikanth H.V | a12810a | 2012-10-31 15:40:33 +0000 | [diff] [blame] | 1979 | } |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 1980 | |
Lee Jones | 330b7eb | 2013-02-15 10:53:57 +0000 | [diff] [blame] | 1981 | /* get device struct and parent */ |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 1982 | di->dev = &pdev->dev; |
Lee Jones | 330b7eb | 2013-02-15 10:53:57 +0000 | [diff] [blame] | 1983 | di->parent = dev_get_drvdata(pdev->dev.parent); |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 1984 | |
| 1985 | /* chargalg supply */ |
| 1986 | di->chargalg_psy.name = "abx500_chargalg"; |
| 1987 | di->chargalg_psy.type = POWER_SUPPLY_TYPE_BATTERY; |
| 1988 | di->chargalg_psy.properties = abx500_chargalg_props; |
| 1989 | di->chargalg_psy.num_properties = ARRAY_SIZE(abx500_chargalg_props); |
| 1990 | di->chargalg_psy.get_property = abx500_chargalg_get_property; |
Rajanikanth H.V | a12810a | 2012-10-31 15:40:33 +0000 | [diff] [blame] | 1991 | di->chargalg_psy.supplied_to = supply_interface; |
| 1992 | di->chargalg_psy.num_supplicants = ARRAY_SIZE(supply_interface), |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 1993 | di->chargalg_psy.external_power_changed = |
| 1994 | abx500_chargalg_external_power_changed; |
| 1995 | |
| 1996 | /* Initilialize safety timer */ |
| 1997 | init_timer(&di->safety_timer); |
| 1998 | di->safety_timer.function = abx500_chargalg_safety_timer_expired; |
| 1999 | di->safety_timer.data = (unsigned long) di; |
| 2000 | |
| 2001 | /* Initilialize maintenance timer */ |
| 2002 | init_timer(&di->maintenance_timer); |
| 2003 | di->maintenance_timer.function = |
| 2004 | abx500_chargalg_maintenance_timer_expired; |
| 2005 | di->maintenance_timer.data = (unsigned long) di; |
| 2006 | |
| 2007 | /* Create a work queue for the chargalg */ |
| 2008 | di->chargalg_wq = |
| 2009 | create_singlethread_workqueue("abx500_chargalg_wq"); |
| 2010 | if (di->chargalg_wq == NULL) { |
| 2011 | dev_err(di->dev, "failed to create work queue\n"); |
Rajanikanth H.V | a12810a | 2012-10-31 15:40:33 +0000 | [diff] [blame] | 2012 | return -ENOMEM; |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 2013 | } |
| 2014 | |
| 2015 | /* Init work for chargalg */ |
Tejun Heo | 203b42f | 2012-08-21 13:18:23 -0700 | [diff] [blame] | 2016 | INIT_DEFERRABLE_WORK(&di->chargalg_periodic_work, |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 2017 | abx500_chargalg_periodic_work); |
Tejun Heo | 203b42f | 2012-08-21 13:18:23 -0700 | [diff] [blame] | 2018 | INIT_DEFERRABLE_WORK(&di->chargalg_wd_work, |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 2019 | abx500_chargalg_wd_work); |
| 2020 | |
| 2021 | /* Init work for chargalg */ |
| 2022 | INIT_WORK(&di->chargalg_work, abx500_chargalg_work); |
| 2023 | |
| 2024 | /* To detect charger at startup */ |
| 2025 | di->chg_info.prev_conn_chg = -1; |
| 2026 | |
| 2027 | /* Register chargalg power supply class */ |
| 2028 | ret = power_supply_register(di->dev, &di->chargalg_psy); |
| 2029 | if (ret) { |
| 2030 | dev_err(di->dev, "failed to register chargalg psy\n"); |
| 2031 | goto free_chargalg_wq; |
| 2032 | } |
| 2033 | |
| 2034 | platform_set_drvdata(pdev, di); |
| 2035 | |
| 2036 | /* sysfs interface to enable/disable charging from user space */ |
| 2037 | ret = abx500_chargalg_sysfs_init(di); |
| 2038 | if (ret) { |
| 2039 | dev_err(di->dev, "failed to create sysfs entry\n"); |
| 2040 | goto free_psy; |
| 2041 | } |
| 2042 | |
| 2043 | /* Run the charging algorithm */ |
| 2044 | queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0); |
| 2045 | |
| 2046 | dev_info(di->dev, "probe success\n"); |
| 2047 | return ret; |
| 2048 | |
| 2049 | free_psy: |
| 2050 | power_supply_unregister(&di->chargalg_psy); |
| 2051 | free_chargalg_wq: |
| 2052 | destroy_workqueue(di->chargalg_wq); |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 2053 | return ret; |
| 2054 | } |
| 2055 | |
Rajanikanth H.V | a12810a | 2012-10-31 15:40:33 +0000 | [diff] [blame] | 2056 | static const struct of_device_id ab8500_chargalg_match[] = { |
| 2057 | { .compatible = "stericsson,ab8500-chargalg", }, |
| 2058 | { }, |
| 2059 | }; |
| 2060 | |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 2061 | static struct platform_driver abx500_chargalg_driver = { |
| 2062 | .probe = abx500_chargalg_probe, |
Bill Pemberton | 28ea73f | 2012-11-19 13:20:40 -0500 | [diff] [blame] | 2063 | .remove = abx500_chargalg_remove, |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 2064 | .suspend = abx500_chargalg_suspend, |
| 2065 | .resume = abx500_chargalg_resume, |
| 2066 | .driver = { |
Rajanikanth H.V | a12810a | 2012-10-31 15:40:33 +0000 | [diff] [blame] | 2067 | .name = "ab8500-chargalg", |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 2068 | .owner = THIS_MODULE, |
Rajanikanth H.V | a12810a | 2012-10-31 15:40:33 +0000 | [diff] [blame] | 2069 | .of_match_table = ab8500_chargalg_match, |
Arun Murthy | 1668f81 | 2012-02-29 21:54:25 +0530 | [diff] [blame] | 2070 | }, |
| 2071 | }; |
| 2072 | |
| 2073 | static int __init abx500_chargalg_init(void) |
| 2074 | { |
| 2075 | return platform_driver_register(&abx500_chargalg_driver); |
| 2076 | } |
| 2077 | |
| 2078 | static void __exit abx500_chargalg_exit(void) |
| 2079 | { |
| 2080 | platform_driver_unregister(&abx500_chargalg_driver); |
| 2081 | } |
| 2082 | |
| 2083 | module_init(abx500_chargalg_init); |
| 2084 | module_exit(abx500_chargalg_exit); |
| 2085 | |
| 2086 | MODULE_LICENSE("GPL v2"); |
| 2087 | MODULE_AUTHOR("Johan Palsson, Karl Komierowski"); |
| 2088 | MODULE_ALIAS("platform:abx500-chargalg"); |
| 2089 | MODULE_DESCRIPTION("abx500 battery charging algorithm"); |