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