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