Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) ST-Ericsson SA 2012 |
| 3 | * |
| 4 | * Charger driver for AB8500 |
| 5 | * |
| 6 | * License Terms: GNU General Public License v2 |
| 7 | * Author: |
| 8 | * Johan Palsson <johan.palsson@stericsson.com> |
| 9 | * Karl Komierowski <karl.komierowski@stericsson.com> |
| 10 | * Arun R Murthy <arun.murthy@stericsson.com> |
| 11 | */ |
| 12 | |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/device.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/power_supply.h> |
| 21 | #include <linux/completion.h> |
| 22 | #include <linux/regulator/consumer.h> |
| 23 | #include <linux/err.h> |
| 24 | #include <linux/workqueue.h> |
| 25 | #include <linux/kobject.h> |
Rajanikanth H.V | 4aef72d | 2012-11-18 19:17:47 -0800 | [diff] [blame] | 26 | #include <linux/of.h> |
| 27 | #include <linux/mfd/core.h> |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 28 | #include <linux/mfd/abx500/ab8500.h> |
| 29 | #include <linux/mfd/abx500.h> |
| 30 | #include <linux/mfd/abx500/ab8500-bm.h> |
| 31 | #include <linux/mfd/abx500/ab8500-gpadc.h> |
| 32 | #include <linux/mfd/abx500/ux500_chargalg.h> |
| 33 | #include <linux/usb/otg.h> |
Lee Jones | b269fff | 2013-01-11 13:12:51 +0000 | [diff] [blame] | 34 | #include <linux/mutex.h> |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 35 | |
| 36 | /* Charger constants */ |
| 37 | #define NO_PW_CONN 0 |
| 38 | #define AC_PW_CONN 1 |
| 39 | #define USB_PW_CONN 2 |
| 40 | |
| 41 | #define MAIN_WDOG_ENA 0x01 |
| 42 | #define MAIN_WDOG_KICK 0x02 |
| 43 | #define MAIN_WDOG_DIS 0x00 |
| 44 | #define CHARG_WD_KICK 0x01 |
| 45 | #define MAIN_CH_ENA 0x01 |
| 46 | #define MAIN_CH_NO_OVERSHOOT_ENA_N 0x02 |
| 47 | #define USB_CH_ENA 0x01 |
| 48 | #define USB_CHG_NO_OVERSHOOT_ENA_N 0x02 |
| 49 | #define MAIN_CH_DET 0x01 |
| 50 | #define MAIN_CH_CV_ON 0x04 |
| 51 | #define USB_CH_CV_ON 0x08 |
| 52 | #define VBUS_DET_DBNC100 0x02 |
| 53 | #define VBUS_DET_DBNC1 0x01 |
| 54 | #define OTP_ENABLE_WD 0x01 |
| 55 | |
| 56 | #define MAIN_CH_INPUT_CURR_SHIFT 4 |
| 57 | #define VBUS_IN_CURR_LIM_SHIFT 4 |
| 58 | |
| 59 | #define LED_INDICATOR_PWM_ENA 0x01 |
| 60 | #define LED_INDICATOR_PWM_DIS 0x00 |
| 61 | #define LED_IND_CUR_5MA 0x04 |
| 62 | #define LED_INDICATOR_PWM_DUTY_252_256 0xBF |
| 63 | |
| 64 | /* HW failure constants */ |
| 65 | #define MAIN_CH_TH_PROT 0x02 |
| 66 | #define VBUS_CH_NOK 0x08 |
| 67 | #define USB_CH_TH_PROT 0x02 |
| 68 | #define VBUS_OVV_TH 0x01 |
| 69 | #define MAIN_CH_NOK 0x01 |
| 70 | #define VBUS_DET 0x80 |
| 71 | |
Lee Jones | b269fff | 2013-01-11 13:12:51 +0000 | [diff] [blame] | 72 | #define MAIN_CH_STATUS2_MAINCHGDROP 0x80 |
| 73 | #define MAIN_CH_STATUS2_MAINCHARGERDETDBNC 0x40 |
| 74 | #define USB_CH_VBUSDROP 0x40 |
| 75 | #define USB_CH_VBUSDETDBNC 0x01 |
| 76 | |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 77 | /* UsbLineStatus register bit masks */ |
| 78 | #define AB8500_USB_LINK_STATUS 0x78 |
| 79 | #define AB8500_STD_HOST_SUSP 0x18 |
| 80 | |
| 81 | /* Watchdog timeout constant */ |
| 82 | #define WD_TIMER 0x30 /* 4min */ |
| 83 | #define WD_KICK_INTERVAL (60 * HZ) |
| 84 | |
| 85 | /* Lowest charger voltage is 3.39V -> 0x4E */ |
| 86 | #define LOW_VOLT_REG 0x4E |
| 87 | |
Johan Bjornstedt | f8e96df | 2012-01-18 12:44:55 +0100 | [diff] [blame] | 88 | /* Step up/down delay in us */ |
| 89 | #define STEP_UDELAY 1000 |
| 90 | |
Lee Jones | b269fff | 2013-01-11 13:12:51 +0000 | [diff] [blame] | 91 | #define CHARGER_STATUS_POLL 10 /* in ms */ |
| 92 | |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 93 | /* UsbLineStatus register - usb types */ |
| 94 | enum ab8500_charger_link_status { |
| 95 | USB_STAT_NOT_CONFIGURED, |
| 96 | USB_STAT_STD_HOST_NC, |
| 97 | USB_STAT_STD_HOST_C_NS, |
| 98 | USB_STAT_STD_HOST_C_S, |
| 99 | USB_STAT_HOST_CHG_NM, |
| 100 | USB_STAT_HOST_CHG_HS, |
| 101 | USB_STAT_HOST_CHG_HS_CHIRP, |
| 102 | USB_STAT_DEDICATED_CHG, |
| 103 | USB_STAT_ACA_RID_A, |
| 104 | USB_STAT_ACA_RID_B, |
| 105 | USB_STAT_ACA_RID_C_NM, |
| 106 | USB_STAT_ACA_RID_C_HS, |
| 107 | USB_STAT_ACA_RID_C_HS_CHIRP, |
| 108 | USB_STAT_HM_IDGND, |
| 109 | USB_STAT_RESERVED, |
| 110 | USB_STAT_NOT_VALID_LINK, |
| 111 | }; |
| 112 | |
| 113 | enum ab8500_usb_state { |
| 114 | AB8500_BM_USB_STATE_RESET_HS, /* HighSpeed Reset */ |
| 115 | AB8500_BM_USB_STATE_RESET_FS, /* FullSpeed/LowSpeed Reset */ |
| 116 | AB8500_BM_USB_STATE_CONFIGURED, |
| 117 | AB8500_BM_USB_STATE_SUSPEND, |
| 118 | AB8500_BM_USB_STATE_RESUME, |
| 119 | AB8500_BM_USB_STATE_MAX, |
| 120 | }; |
| 121 | |
| 122 | /* VBUS input current limits supported in AB8500 in mA */ |
| 123 | #define USB_CH_IP_CUR_LVL_0P05 50 |
| 124 | #define USB_CH_IP_CUR_LVL_0P09 98 |
| 125 | #define USB_CH_IP_CUR_LVL_0P19 193 |
| 126 | #define USB_CH_IP_CUR_LVL_0P29 290 |
| 127 | #define USB_CH_IP_CUR_LVL_0P38 380 |
| 128 | #define USB_CH_IP_CUR_LVL_0P45 450 |
| 129 | #define USB_CH_IP_CUR_LVL_0P5 500 |
| 130 | #define USB_CH_IP_CUR_LVL_0P6 600 |
| 131 | #define USB_CH_IP_CUR_LVL_0P7 700 |
| 132 | #define USB_CH_IP_CUR_LVL_0P8 800 |
| 133 | #define USB_CH_IP_CUR_LVL_0P9 900 |
| 134 | #define USB_CH_IP_CUR_LVL_1P0 1000 |
| 135 | #define USB_CH_IP_CUR_LVL_1P1 1100 |
| 136 | #define USB_CH_IP_CUR_LVL_1P3 1300 |
| 137 | #define USB_CH_IP_CUR_LVL_1P4 1400 |
| 138 | #define USB_CH_IP_CUR_LVL_1P5 1500 |
| 139 | |
| 140 | #define VBAT_TRESH_IP_CUR_RED 3800 |
| 141 | |
| 142 | #define to_ab8500_charger_usb_device_info(x) container_of((x), \ |
| 143 | struct ab8500_charger, usb_chg) |
| 144 | #define to_ab8500_charger_ac_device_info(x) container_of((x), \ |
| 145 | struct ab8500_charger, ac_chg) |
| 146 | |
| 147 | /** |
| 148 | * struct ab8500_charger_interrupts - ab8500 interupts |
| 149 | * @name: name of the interrupt |
| 150 | * @isr function pointer to the isr |
| 151 | */ |
| 152 | struct ab8500_charger_interrupts { |
| 153 | char *name; |
| 154 | irqreturn_t (*isr)(int irq, void *data); |
| 155 | }; |
| 156 | |
| 157 | struct ab8500_charger_info { |
| 158 | int charger_connected; |
| 159 | int charger_online; |
| 160 | int charger_voltage; |
| 161 | int cv_active; |
| 162 | bool wd_expired; |
Jonas Aaberg | a864c5a | 2013-01-11 13:12:53 +0000 | [diff] [blame^] | 163 | int charger_current; |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | struct ab8500_charger_event_flags { |
| 167 | bool mainextchnotok; |
| 168 | bool main_thermal_prot; |
| 169 | bool usb_thermal_prot; |
| 170 | bool vbus_ovv; |
| 171 | bool usbchargernotok; |
| 172 | bool chgwdexp; |
| 173 | bool vbus_collapse; |
| 174 | }; |
| 175 | |
| 176 | struct ab8500_charger_usb_state { |
| 177 | bool usb_changed; |
| 178 | int usb_current; |
| 179 | enum ab8500_usb_state state; |
| 180 | spinlock_t usb_lock; |
| 181 | }; |
| 182 | |
| 183 | /** |
| 184 | * struct ab8500_charger - ab8500 Charger device information |
| 185 | * @dev: Pointer to the structure device |
| 186 | * @max_usb_in_curr: Max USB charger input current |
| 187 | * @vbus_detected: VBUS detected |
| 188 | * @vbus_detected_start: |
| 189 | * VBUS detected during startup |
| 190 | * @ac_conn: This will be true when the AC charger has been plugged |
| 191 | * @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC |
| 192 | * charger is enabled |
| 193 | * @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB |
| 194 | * charger is enabled |
| 195 | * @vbat Battery voltage |
| 196 | * @old_vbat Previously measured battery voltage |
| 197 | * @autopower Indicate if we should have automatic pwron after pwrloss |
Rajanikanth H.V | 4aef72d | 2012-11-18 19:17:47 -0800 | [diff] [blame] | 198 | * @autopower_cfg platform specific power config support for "pwron after pwrloss" |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 199 | * @parent: Pointer to the struct ab8500 |
| 200 | * @gpadc: Pointer to the struct gpadc |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 201 | * @bm: Platform specific battery management information |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 202 | * @flags: Structure for information about events triggered |
| 203 | * @usb_state: Structure for usb stack information |
| 204 | * @ac_chg: AC charger power supply |
| 205 | * @usb_chg: USB charger power supply |
| 206 | * @ac: Structure that holds the AC charger properties |
| 207 | * @usb: Structure that holds the USB charger properties |
| 208 | * @regu: Pointer to the struct regulator |
| 209 | * @charger_wq: Work queue for the IRQs and checking HW state |
| 210 | * @check_vbat_work Work for checking vbat threshold to adjust vbus current |
| 211 | * @check_hw_failure_work: Work for checking HW state |
| 212 | * @check_usbchgnotok_work: Work for checking USB charger not ok status |
| 213 | * @kick_wd_work: Work for kicking the charger watchdog in case |
| 214 | * of ABB rev 1.* due to the watchog logic bug |
Lee Jones | b269fff | 2013-01-11 13:12:51 +0000 | [diff] [blame] | 215 | * @ac_charger_attached_work: Work for checking if AC charger is still |
| 216 | * connected |
| 217 | * @usb_charger_attached_work: Work for checking if USB charger is still |
| 218 | * connected |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 219 | * @ac_work: Work for checking AC charger connection |
| 220 | * @detect_usb_type_work: Work for detecting the USB type connected |
| 221 | * @usb_link_status_work: Work for checking the new USB link status |
| 222 | * @usb_state_changed_work: Work for checking USB state |
| 223 | * @check_main_thermal_prot_work: |
| 224 | * Work for checking Main thermal status |
| 225 | * @check_usb_thermal_prot_work: |
| 226 | * Work for checking USB thermal status |
Lee Jones | b269fff | 2013-01-11 13:12:51 +0000 | [diff] [blame] | 227 | * @charger_attached_mutex: For controlling the wakelock |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 228 | */ |
| 229 | struct ab8500_charger { |
| 230 | struct device *dev; |
| 231 | int max_usb_in_curr; |
| 232 | bool vbus_detected; |
| 233 | bool vbus_detected_start; |
| 234 | bool ac_conn; |
| 235 | bool vddadc_en_ac; |
| 236 | bool vddadc_en_usb; |
| 237 | int vbat; |
| 238 | int old_vbat; |
| 239 | bool autopower; |
Rajanikanth H.V | 4aef72d | 2012-11-18 19:17:47 -0800 | [diff] [blame] | 240 | bool autopower_cfg; |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 241 | struct ab8500 *parent; |
| 242 | struct ab8500_gpadc *gpadc; |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 243 | struct abx500_bm_data *bm; |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 244 | struct ab8500_charger_event_flags flags; |
| 245 | struct ab8500_charger_usb_state usb_state; |
| 246 | struct ux500_charger ac_chg; |
| 247 | struct ux500_charger usb_chg; |
| 248 | struct ab8500_charger_info ac; |
| 249 | struct ab8500_charger_info usb; |
| 250 | struct regulator *regu; |
| 251 | struct workqueue_struct *charger_wq; |
| 252 | struct delayed_work check_vbat_work; |
| 253 | struct delayed_work check_hw_failure_work; |
| 254 | struct delayed_work check_usbchgnotok_work; |
| 255 | struct delayed_work kick_wd_work; |
Lee Jones | b269fff | 2013-01-11 13:12:51 +0000 | [diff] [blame] | 256 | struct delayed_work ac_charger_attached_work; |
| 257 | struct delayed_work usb_charger_attached_work; |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 258 | struct work_struct ac_work; |
| 259 | struct work_struct detect_usb_type_work; |
| 260 | struct work_struct usb_link_status_work; |
| 261 | struct work_struct usb_state_changed_work; |
| 262 | struct work_struct check_main_thermal_prot_work; |
| 263 | struct work_struct check_usb_thermal_prot_work; |
Anton Vorontsov | efd71c8 | 2012-03-14 04:22:17 +0400 | [diff] [blame] | 264 | struct usb_phy *usb_phy; |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 265 | struct notifier_block nb; |
Lee Jones | b269fff | 2013-01-11 13:12:51 +0000 | [diff] [blame] | 266 | struct mutex charger_attached_mutex; |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 267 | }; |
| 268 | |
| 269 | /* AC properties */ |
| 270 | static enum power_supply_property ab8500_charger_ac_props[] = { |
| 271 | POWER_SUPPLY_PROP_HEALTH, |
| 272 | POWER_SUPPLY_PROP_PRESENT, |
| 273 | POWER_SUPPLY_PROP_ONLINE, |
| 274 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 275 | POWER_SUPPLY_PROP_VOLTAGE_AVG, |
| 276 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 277 | }; |
| 278 | |
| 279 | /* USB properties */ |
| 280 | static enum power_supply_property ab8500_charger_usb_props[] = { |
| 281 | POWER_SUPPLY_PROP_HEALTH, |
| 282 | POWER_SUPPLY_PROP_CURRENT_AVG, |
| 283 | POWER_SUPPLY_PROP_PRESENT, |
| 284 | POWER_SUPPLY_PROP_ONLINE, |
| 285 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 286 | POWER_SUPPLY_PROP_VOLTAGE_AVG, |
| 287 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 288 | }; |
| 289 | |
Paer-Olof Haakansson | defbfa9 | 2013-01-11 13:12:49 +0000 | [diff] [blame] | 290 | /* |
| 291 | * Function for enabling and disabling sw fallback mode |
| 292 | * should always be disabled when no charger is connected. |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 293 | */ |
Paer-Olof Haakansson | defbfa9 | 2013-01-11 13:12:49 +0000 | [diff] [blame] | 294 | static void ab8500_enable_disable_sw_fallback(struct ab8500_charger *di, |
| 295 | bool fallback) |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 296 | { |
| 297 | u8 reg; |
| 298 | int ret; |
| 299 | |
Paer-Olof Haakansson | defbfa9 | 2013-01-11 13:12:49 +0000 | [diff] [blame] | 300 | dev_dbg(di->dev, "SW Fallback: %d\n", fallback); |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 301 | |
Paer-Olof Haakansson | defbfa9 | 2013-01-11 13:12:49 +0000 | [diff] [blame] | 302 | /* read the register containing fallback bit */ |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 303 | ret = abx500_get_register_interruptible(di->dev, 0x15, 0x00, ®); |
| 304 | if (ret) { |
| 305 | dev_err(di->dev, "%d write failed\n", __LINE__); |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | /* enable the OPT emulation registers */ |
| 310 | ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2); |
| 311 | if (ret) { |
| 312 | dev_err(di->dev, "%d write failed\n", __LINE__); |
| 313 | return; |
| 314 | } |
| 315 | |
Paer-Olof Haakansson | defbfa9 | 2013-01-11 13:12:49 +0000 | [diff] [blame] | 316 | if (fallback) |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 317 | reg |= 0x8; |
| 318 | else |
| 319 | reg &= ~0x8; |
| 320 | |
Paer-Olof Haakansson | defbfa9 | 2013-01-11 13:12:49 +0000 | [diff] [blame] | 321 | /* write back the changed fallback bit value to register */ |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 322 | ret = abx500_set_register_interruptible(di->dev, 0x15, 0x00, reg); |
| 323 | if (ret) { |
| 324 | dev_err(di->dev, "%d write failed\n", __LINE__); |
| 325 | return; |
| 326 | } |
| 327 | |
| 328 | /* disable the set OTP registers again */ |
| 329 | ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0); |
| 330 | if (ret) { |
| 331 | dev_err(di->dev, "%d write failed\n", __LINE__); |
| 332 | return; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * ab8500_power_supply_changed - a wrapper with local extentions for |
| 338 | * power_supply_changed |
| 339 | * @di: pointer to the ab8500_charger structure |
| 340 | * @psy: pointer to power_supply_that have changed. |
| 341 | * |
| 342 | */ |
| 343 | static void ab8500_power_supply_changed(struct ab8500_charger *di, |
| 344 | struct power_supply *psy) |
| 345 | { |
Rajanikanth H.V | 4aef72d | 2012-11-18 19:17:47 -0800 | [diff] [blame] | 346 | if (di->autopower_cfg) { |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 347 | if (!di->usb.charger_connected && |
| 348 | !di->ac.charger_connected && |
| 349 | di->autopower) { |
| 350 | di->autopower = false; |
Paer-Olof Haakansson | defbfa9 | 2013-01-11 13:12:49 +0000 | [diff] [blame] | 351 | ab8500_enable_disable_sw_fallback(di, false); |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 352 | } else if (!di->autopower && |
| 353 | (di->ac.charger_connected || |
| 354 | di->usb.charger_connected)) { |
| 355 | di->autopower = true; |
Paer-Olof Haakansson | defbfa9 | 2013-01-11 13:12:49 +0000 | [diff] [blame] | 356 | ab8500_enable_disable_sw_fallback(di, true); |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | power_supply_changed(psy); |
| 360 | } |
| 361 | |
| 362 | static void ab8500_charger_set_usb_connected(struct ab8500_charger *di, |
| 363 | bool connected) |
| 364 | { |
| 365 | if (connected != di->usb.charger_connected) { |
| 366 | dev_dbg(di->dev, "USB connected:%i\n", connected); |
| 367 | di->usb.charger_connected = connected; |
| 368 | sysfs_notify(&di->usb_chg.psy.dev->kobj, NULL, "present"); |
Lee Jones | b269fff | 2013-01-11 13:12:51 +0000 | [diff] [blame] | 369 | |
| 370 | if (connected) { |
| 371 | mutex_lock(&di->charger_attached_mutex); |
| 372 | mutex_unlock(&di->charger_attached_mutex); |
| 373 | |
| 374 | queue_delayed_work(di->charger_wq, |
| 375 | &di->usb_charger_attached_work, |
| 376 | HZ); |
| 377 | } else { |
| 378 | cancel_delayed_work_sync(&di->usb_charger_attached_work); |
| 379 | mutex_lock(&di->charger_attached_mutex); |
| 380 | mutex_unlock(&di->charger_attached_mutex); |
| 381 | } |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * ab8500_charger_get_ac_voltage() - get ac charger voltage |
| 387 | * @di: pointer to the ab8500_charger structure |
| 388 | * |
| 389 | * Returns ac charger voltage (on success) |
| 390 | */ |
| 391 | static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di) |
| 392 | { |
| 393 | int vch; |
| 394 | |
| 395 | /* Only measure voltage if the charger is connected */ |
| 396 | if (di->ac.charger_connected) { |
| 397 | vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V); |
| 398 | if (vch < 0) |
| 399 | dev_err(di->dev, "%s gpadc conv failed,\n", __func__); |
| 400 | } else { |
| 401 | vch = 0; |
| 402 | } |
| 403 | return vch; |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * ab8500_charger_ac_cv() - check if the main charger is in CV mode |
| 408 | * @di: pointer to the ab8500_charger structure |
| 409 | * |
| 410 | * Returns ac charger CV mode (on success) else error code |
| 411 | */ |
| 412 | static int ab8500_charger_ac_cv(struct ab8500_charger *di) |
| 413 | { |
| 414 | u8 val; |
| 415 | int ret = 0; |
| 416 | |
| 417 | /* Only check CV mode if the charger is online */ |
| 418 | if (di->ac.charger_online) { |
| 419 | ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER, |
| 420 | AB8500_CH_STATUS1_REG, &val); |
| 421 | if (ret < 0) { |
| 422 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); |
| 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | if (val & MAIN_CH_CV_ON) |
| 427 | ret = 1; |
| 428 | else |
| 429 | ret = 0; |
| 430 | } |
| 431 | |
| 432 | return ret; |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * ab8500_charger_get_vbus_voltage() - get vbus voltage |
| 437 | * @di: pointer to the ab8500_charger structure |
| 438 | * |
| 439 | * This function returns the vbus voltage. |
| 440 | * Returns vbus voltage (on success) |
| 441 | */ |
| 442 | static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di) |
| 443 | { |
| 444 | int vch; |
| 445 | |
| 446 | /* Only measure voltage if the charger is connected */ |
| 447 | if (di->usb.charger_connected) { |
| 448 | vch = ab8500_gpadc_convert(di->gpadc, VBUS_V); |
| 449 | if (vch < 0) |
| 450 | dev_err(di->dev, "%s gpadc conv failed\n", __func__); |
| 451 | } else { |
| 452 | vch = 0; |
| 453 | } |
| 454 | return vch; |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * ab8500_charger_get_usb_current() - get usb charger current |
| 459 | * @di: pointer to the ab8500_charger structure |
| 460 | * |
| 461 | * This function returns the usb charger current. |
| 462 | * Returns usb current (on success) and error code on failure |
| 463 | */ |
| 464 | static int ab8500_charger_get_usb_current(struct ab8500_charger *di) |
| 465 | { |
| 466 | int ich; |
| 467 | |
| 468 | /* Only measure current if the charger is online */ |
| 469 | if (di->usb.charger_online) { |
| 470 | ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C); |
| 471 | if (ich < 0) |
| 472 | dev_err(di->dev, "%s gpadc conv failed\n", __func__); |
| 473 | } else { |
| 474 | ich = 0; |
| 475 | } |
| 476 | return ich; |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * ab8500_charger_get_ac_current() - get ac charger current |
| 481 | * @di: pointer to the ab8500_charger structure |
| 482 | * |
| 483 | * This function returns the ac charger current. |
| 484 | * Returns ac current (on success) and error code on failure. |
| 485 | */ |
| 486 | static int ab8500_charger_get_ac_current(struct ab8500_charger *di) |
| 487 | { |
| 488 | int ich; |
| 489 | |
| 490 | /* Only measure current if the charger is online */ |
| 491 | if (di->ac.charger_online) { |
| 492 | ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C); |
| 493 | if (ich < 0) |
| 494 | dev_err(di->dev, "%s gpadc conv failed\n", __func__); |
| 495 | } else { |
| 496 | ich = 0; |
| 497 | } |
| 498 | return ich; |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * ab8500_charger_usb_cv() - check if the usb charger is in CV mode |
| 503 | * @di: pointer to the ab8500_charger structure |
| 504 | * |
| 505 | * Returns ac charger CV mode (on success) else error code |
| 506 | */ |
| 507 | static int ab8500_charger_usb_cv(struct ab8500_charger *di) |
| 508 | { |
| 509 | int ret; |
| 510 | u8 val; |
| 511 | |
| 512 | /* Only check CV mode if the charger is online */ |
| 513 | if (di->usb.charger_online) { |
| 514 | ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER, |
| 515 | AB8500_CH_USBCH_STAT1_REG, &val); |
| 516 | if (ret < 0) { |
| 517 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); |
| 518 | return 0; |
| 519 | } |
| 520 | |
| 521 | if (val & USB_CH_CV_ON) |
| 522 | ret = 1; |
| 523 | else |
| 524 | ret = 0; |
| 525 | } else { |
| 526 | ret = 0; |
| 527 | } |
| 528 | |
| 529 | return ret; |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * ab8500_charger_detect_chargers() - Detect the connected chargers |
| 534 | * @di: pointer to the ab8500_charger structure |
| 535 | * |
| 536 | * Returns the type of charger connected. |
| 537 | * For USB it will not mean we can actually charge from it |
| 538 | * but that there is a USB cable connected that we have to |
| 539 | * identify. This is used during startup when we don't get |
| 540 | * interrupts of the charger detection |
| 541 | * |
| 542 | * Returns an integer value, that means, |
| 543 | * NO_PW_CONN no power supply is connected |
| 544 | * AC_PW_CONN if the AC power supply is connected |
| 545 | * USB_PW_CONN if the USB power supply is connected |
| 546 | * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected |
| 547 | */ |
| 548 | static int ab8500_charger_detect_chargers(struct ab8500_charger *di) |
| 549 | { |
| 550 | int result = NO_PW_CONN; |
| 551 | int ret; |
| 552 | u8 val; |
| 553 | |
| 554 | /* Check for AC charger */ |
| 555 | ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER, |
| 556 | AB8500_CH_STATUS1_REG, &val); |
| 557 | if (ret < 0) { |
| 558 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); |
| 559 | return ret; |
| 560 | } |
| 561 | |
| 562 | if (val & MAIN_CH_DET) |
| 563 | result = AC_PW_CONN; |
| 564 | |
| 565 | /* Check for USB charger */ |
| 566 | ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER, |
| 567 | AB8500_CH_USBCH_STAT1_REG, &val); |
| 568 | if (ret < 0) { |
| 569 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); |
| 570 | return ret; |
| 571 | } |
| 572 | |
| 573 | if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100)) |
| 574 | result |= USB_PW_CONN; |
| 575 | |
| 576 | return result; |
| 577 | } |
| 578 | |
| 579 | /** |
| 580 | * ab8500_charger_max_usb_curr() - get the max curr for the USB type |
| 581 | * @di: pointer to the ab8500_charger structure |
| 582 | * @link_status: the identified USB type |
| 583 | * |
| 584 | * Get the maximum current that is allowed to be drawn from the host |
| 585 | * based on the USB type. |
| 586 | * Returns error code in case of failure else 0 on success |
| 587 | */ |
| 588 | static int ab8500_charger_max_usb_curr(struct ab8500_charger *di, |
| 589 | enum ab8500_charger_link_status link_status) |
| 590 | { |
| 591 | int ret = 0; |
| 592 | |
| 593 | switch (link_status) { |
| 594 | case USB_STAT_STD_HOST_NC: |
| 595 | case USB_STAT_STD_HOST_C_NS: |
| 596 | case USB_STAT_STD_HOST_C_S: |
| 597 | dev_dbg(di->dev, "USB Type - Standard host is " |
| 598 | "detected through USB driver\n"); |
| 599 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09; |
| 600 | break; |
| 601 | case USB_STAT_HOST_CHG_HS_CHIRP: |
| 602 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5; |
| 603 | break; |
| 604 | case USB_STAT_HOST_CHG_HS: |
| 605 | case USB_STAT_ACA_RID_C_HS: |
| 606 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P9; |
| 607 | break; |
| 608 | case USB_STAT_ACA_RID_A: |
| 609 | /* |
| 610 | * Dedicated charger level minus maximum current accessory |
| 611 | * can consume (300mA). Closest level is 1100mA |
| 612 | */ |
| 613 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P1; |
| 614 | break; |
| 615 | case USB_STAT_ACA_RID_B: |
| 616 | /* |
| 617 | * Dedicated charger level minus 120mA (20mA for ACA and |
| 618 | * 100mA for potential accessory). Closest level is 1300mA |
| 619 | */ |
| 620 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P3; |
| 621 | break; |
| 622 | case USB_STAT_DEDICATED_CHG: |
| 623 | case USB_STAT_HOST_CHG_NM: |
| 624 | case USB_STAT_ACA_RID_C_HS_CHIRP: |
| 625 | case USB_STAT_ACA_RID_C_NM: |
| 626 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5; |
| 627 | break; |
| 628 | case USB_STAT_RESERVED: |
| 629 | /* |
| 630 | * This state is used to indicate that VBUS has dropped below |
| 631 | * the detection level 4 times in a row. This is due to the |
| 632 | * charger output current is set to high making the charger |
| 633 | * voltage collapse. This have to be propagated through to |
| 634 | * chargalg. This is done using the property |
| 635 | * POWER_SUPPLY_PROP_CURRENT_AVG = 1 |
| 636 | */ |
| 637 | di->flags.vbus_collapse = true; |
| 638 | dev_dbg(di->dev, "USB Type - USB_STAT_RESERVED " |
| 639 | "VBUS has collapsed\n"); |
| 640 | ret = -1; |
| 641 | break; |
| 642 | case USB_STAT_HM_IDGND: |
| 643 | case USB_STAT_NOT_CONFIGURED: |
| 644 | case USB_STAT_NOT_VALID_LINK: |
| 645 | dev_err(di->dev, "USB Type - Charging not allowed\n"); |
| 646 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05; |
| 647 | ret = -ENXIO; |
| 648 | break; |
| 649 | default: |
| 650 | dev_err(di->dev, "USB Type - Unknown\n"); |
| 651 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05; |
| 652 | ret = -ENXIO; |
| 653 | break; |
| 654 | }; |
| 655 | |
| 656 | dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", |
| 657 | link_status, di->max_usb_in_curr); |
| 658 | |
| 659 | return ret; |
| 660 | } |
| 661 | |
| 662 | /** |
| 663 | * ab8500_charger_read_usb_type() - read the type of usb connected |
| 664 | * @di: pointer to the ab8500_charger structure |
| 665 | * |
| 666 | * Detect the type of the plugged USB |
| 667 | * Returns error code in case of failure else 0 on success |
| 668 | */ |
| 669 | static int ab8500_charger_read_usb_type(struct ab8500_charger *di) |
| 670 | { |
| 671 | int ret; |
| 672 | u8 val; |
| 673 | |
| 674 | ret = abx500_get_register_interruptible(di->dev, |
| 675 | AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val); |
| 676 | if (ret < 0) { |
| 677 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); |
| 678 | return ret; |
| 679 | } |
| 680 | ret = abx500_get_register_interruptible(di->dev, AB8500_USB, |
| 681 | AB8500_USB_LINE_STAT_REG, &val); |
| 682 | if (ret < 0) { |
| 683 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); |
| 684 | return ret; |
| 685 | } |
| 686 | |
| 687 | /* get the USB type */ |
| 688 | val = (val & AB8500_USB_LINK_STATUS) >> 3; |
| 689 | ret = ab8500_charger_max_usb_curr(di, |
| 690 | (enum ab8500_charger_link_status) val); |
| 691 | |
| 692 | return ret; |
| 693 | } |
| 694 | |
| 695 | /** |
| 696 | * ab8500_charger_detect_usb_type() - get the type of usb connected |
| 697 | * @di: pointer to the ab8500_charger structure |
| 698 | * |
| 699 | * Detect the type of the plugged USB |
| 700 | * Returns error code in case of failure else 0 on success |
| 701 | */ |
| 702 | static int ab8500_charger_detect_usb_type(struct ab8500_charger *di) |
| 703 | { |
| 704 | int i, ret; |
| 705 | u8 val; |
| 706 | |
| 707 | /* |
| 708 | * On getting the VBUS rising edge detect interrupt there |
| 709 | * is a 250ms delay after which the register UsbLineStatus |
| 710 | * is filled with valid data. |
| 711 | */ |
| 712 | for (i = 0; i < 10; i++) { |
| 713 | msleep(250); |
| 714 | ret = abx500_get_register_interruptible(di->dev, |
| 715 | AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, |
| 716 | &val); |
| 717 | if (ret < 0) { |
| 718 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); |
| 719 | return ret; |
| 720 | } |
| 721 | ret = abx500_get_register_interruptible(di->dev, AB8500_USB, |
| 722 | AB8500_USB_LINE_STAT_REG, &val); |
| 723 | if (ret < 0) { |
| 724 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); |
| 725 | return ret; |
| 726 | } |
| 727 | /* |
| 728 | * Until the IT source register is read the UsbLineStatus |
| 729 | * register is not updated, hence doing the same |
| 730 | * Revisit this: |
| 731 | */ |
| 732 | |
| 733 | /* get the USB type */ |
| 734 | val = (val & AB8500_USB_LINK_STATUS) >> 3; |
| 735 | if (val) |
| 736 | break; |
| 737 | } |
| 738 | ret = ab8500_charger_max_usb_curr(di, |
| 739 | (enum ab8500_charger_link_status) val); |
| 740 | |
| 741 | return ret; |
| 742 | } |
| 743 | |
| 744 | /* |
| 745 | * This array maps the raw hex value to charger voltage used by the AB8500 |
| 746 | * Values taken from the UM0836 |
| 747 | */ |
| 748 | static int ab8500_charger_voltage_map[] = { |
| 749 | 3500 , |
| 750 | 3525 , |
| 751 | 3550 , |
| 752 | 3575 , |
| 753 | 3600 , |
| 754 | 3625 , |
| 755 | 3650 , |
| 756 | 3675 , |
| 757 | 3700 , |
| 758 | 3725 , |
| 759 | 3750 , |
| 760 | 3775 , |
| 761 | 3800 , |
| 762 | 3825 , |
| 763 | 3850 , |
| 764 | 3875 , |
| 765 | 3900 , |
| 766 | 3925 , |
| 767 | 3950 , |
| 768 | 3975 , |
| 769 | 4000 , |
| 770 | 4025 , |
| 771 | 4050 , |
| 772 | 4060 , |
| 773 | 4070 , |
| 774 | 4080 , |
| 775 | 4090 , |
| 776 | 4100 , |
| 777 | 4110 , |
| 778 | 4120 , |
| 779 | 4130 , |
| 780 | 4140 , |
| 781 | 4150 , |
| 782 | 4160 , |
| 783 | 4170 , |
| 784 | 4180 , |
| 785 | 4190 , |
| 786 | 4200 , |
| 787 | 4210 , |
| 788 | 4220 , |
| 789 | 4230 , |
| 790 | 4240 , |
| 791 | 4250 , |
| 792 | 4260 , |
| 793 | 4270 , |
| 794 | 4280 , |
| 795 | 4290 , |
| 796 | 4300 , |
| 797 | 4310 , |
| 798 | 4320 , |
| 799 | 4330 , |
| 800 | 4340 , |
| 801 | 4350 , |
| 802 | 4360 , |
| 803 | 4370 , |
| 804 | 4380 , |
| 805 | 4390 , |
| 806 | 4400 , |
| 807 | 4410 , |
| 808 | 4420 , |
| 809 | 4430 , |
| 810 | 4440 , |
| 811 | 4450 , |
| 812 | 4460 , |
| 813 | 4470 , |
| 814 | 4480 , |
| 815 | 4490 , |
| 816 | 4500 , |
| 817 | 4510 , |
| 818 | 4520 , |
| 819 | 4530 , |
| 820 | 4540 , |
| 821 | 4550 , |
| 822 | 4560 , |
| 823 | 4570 , |
| 824 | 4580 , |
| 825 | 4590 , |
| 826 | 4600 , |
| 827 | }; |
| 828 | |
| 829 | /* |
| 830 | * This array maps the raw hex value to charger current used by the AB8500 |
| 831 | * Values taken from the UM0836 |
| 832 | */ |
| 833 | static int ab8500_charger_current_map[] = { |
| 834 | 100 , |
| 835 | 200 , |
| 836 | 300 , |
| 837 | 400 , |
| 838 | 500 , |
| 839 | 600 , |
| 840 | 700 , |
| 841 | 800 , |
| 842 | 900 , |
| 843 | 1000 , |
| 844 | 1100 , |
| 845 | 1200 , |
| 846 | 1300 , |
| 847 | 1400 , |
| 848 | 1500 , |
| 849 | }; |
| 850 | |
| 851 | /* |
| 852 | * This array maps the raw hex value to VBUS input current used by the AB8500 |
| 853 | * Values taken from the UM0836 |
| 854 | */ |
| 855 | static int ab8500_charger_vbus_in_curr_map[] = { |
| 856 | USB_CH_IP_CUR_LVL_0P05, |
| 857 | USB_CH_IP_CUR_LVL_0P09, |
| 858 | USB_CH_IP_CUR_LVL_0P19, |
| 859 | USB_CH_IP_CUR_LVL_0P29, |
| 860 | USB_CH_IP_CUR_LVL_0P38, |
| 861 | USB_CH_IP_CUR_LVL_0P45, |
| 862 | USB_CH_IP_CUR_LVL_0P5, |
| 863 | USB_CH_IP_CUR_LVL_0P6, |
| 864 | USB_CH_IP_CUR_LVL_0P7, |
| 865 | USB_CH_IP_CUR_LVL_0P8, |
| 866 | USB_CH_IP_CUR_LVL_0P9, |
| 867 | USB_CH_IP_CUR_LVL_1P0, |
| 868 | USB_CH_IP_CUR_LVL_1P1, |
| 869 | USB_CH_IP_CUR_LVL_1P3, |
| 870 | USB_CH_IP_CUR_LVL_1P4, |
| 871 | USB_CH_IP_CUR_LVL_1P5, |
| 872 | }; |
| 873 | |
| 874 | static int ab8500_voltage_to_regval(int voltage) |
| 875 | { |
| 876 | int i; |
| 877 | |
| 878 | /* Special case for voltage below 3.5V */ |
| 879 | if (voltage < ab8500_charger_voltage_map[0]) |
| 880 | return LOW_VOLT_REG; |
| 881 | |
| 882 | for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) { |
| 883 | if (voltage < ab8500_charger_voltage_map[i]) |
| 884 | return i - 1; |
| 885 | } |
| 886 | |
| 887 | /* If not last element, return error */ |
| 888 | i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1; |
| 889 | if (voltage == ab8500_charger_voltage_map[i]) |
| 890 | return i; |
| 891 | else |
| 892 | return -1; |
| 893 | } |
| 894 | |
| 895 | static int ab8500_current_to_regval(int curr) |
| 896 | { |
| 897 | int i; |
| 898 | |
| 899 | if (curr < ab8500_charger_current_map[0]) |
| 900 | return 0; |
| 901 | |
| 902 | for (i = 0; i < ARRAY_SIZE(ab8500_charger_current_map); i++) { |
| 903 | if (curr < ab8500_charger_current_map[i]) |
| 904 | return i - 1; |
| 905 | } |
| 906 | |
| 907 | /* If not last element, return error */ |
| 908 | i = ARRAY_SIZE(ab8500_charger_current_map) - 1; |
| 909 | if (curr == ab8500_charger_current_map[i]) |
| 910 | return i; |
| 911 | else |
| 912 | return -1; |
| 913 | } |
| 914 | |
| 915 | static int ab8500_vbus_in_curr_to_regval(int curr) |
| 916 | { |
| 917 | int i; |
| 918 | |
| 919 | if (curr < ab8500_charger_vbus_in_curr_map[0]) |
| 920 | return 0; |
| 921 | |
| 922 | for (i = 0; i < ARRAY_SIZE(ab8500_charger_vbus_in_curr_map); i++) { |
| 923 | if (curr < ab8500_charger_vbus_in_curr_map[i]) |
| 924 | return i - 1; |
| 925 | } |
| 926 | |
| 927 | /* If not last element, return error */ |
| 928 | i = ARRAY_SIZE(ab8500_charger_vbus_in_curr_map) - 1; |
| 929 | if (curr == ab8500_charger_vbus_in_curr_map[i]) |
| 930 | return i; |
| 931 | else |
| 932 | return -1; |
| 933 | } |
| 934 | |
| 935 | /** |
| 936 | * ab8500_charger_get_usb_cur() - get usb current |
| 937 | * @di: pointer to the ab8500_charger structre |
| 938 | * |
| 939 | * The usb stack provides the maximum current that can be drawn from |
| 940 | * the standard usb host. This will be in mA. |
| 941 | * This function converts current in mA to a value that can be written |
| 942 | * to the register. Returns -1 if charging is not allowed |
| 943 | */ |
| 944 | static int ab8500_charger_get_usb_cur(struct ab8500_charger *di) |
| 945 | { |
| 946 | switch (di->usb_state.usb_current) { |
| 947 | case 100: |
| 948 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09; |
| 949 | break; |
| 950 | case 200: |
| 951 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P19; |
| 952 | break; |
| 953 | case 300: |
| 954 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P29; |
| 955 | break; |
| 956 | case 400: |
| 957 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P38; |
| 958 | break; |
| 959 | case 500: |
| 960 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5; |
| 961 | break; |
| 962 | default: |
| 963 | di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05; |
| 964 | return -1; |
| 965 | break; |
| 966 | }; |
| 967 | return 0; |
| 968 | } |
| 969 | |
| 970 | /** |
Johan Bjornstedt | f8e96df | 2012-01-18 12:44:55 +0100 | [diff] [blame] | 971 | * ab8500_charger_set_current() - set charger current |
| 972 | * @di: pointer to the ab8500_charger structure |
| 973 | * @ich: charger current, in mA |
| 974 | * @reg: select what charger register to set |
| 975 | * |
| 976 | * Set charger current. |
| 977 | * There is no state machine in the AB to step up/down the charger |
| 978 | * current to avoid dips and spikes on MAIN, VBUS and VBAT when |
| 979 | * charging is started. Instead we need to implement |
| 980 | * this charger current step-up/down here. |
| 981 | * Returns error code in case of failure else 0(on success) |
| 982 | */ |
| 983 | static int ab8500_charger_set_current(struct ab8500_charger *di, |
| 984 | int ich, int reg) |
| 985 | { |
| 986 | int ret, i; |
| 987 | int curr_index, prev_curr_index, shift_value; |
| 988 | u8 reg_value; |
| 989 | |
| 990 | switch (reg) { |
| 991 | case AB8500_MCH_IPT_CURLVL_REG: |
| 992 | shift_value = MAIN_CH_INPUT_CURR_SHIFT; |
| 993 | curr_index = ab8500_current_to_regval(ich); |
| 994 | break; |
| 995 | case AB8500_USBCH_IPT_CRNTLVL_REG: |
| 996 | shift_value = VBUS_IN_CURR_LIM_SHIFT; |
| 997 | curr_index = ab8500_vbus_in_curr_to_regval(ich); |
| 998 | break; |
| 999 | case AB8500_CH_OPT_CRNTLVL_REG: |
| 1000 | shift_value = 0; |
| 1001 | curr_index = ab8500_current_to_regval(ich); |
| 1002 | break; |
| 1003 | default: |
| 1004 | dev_err(di->dev, "%s current register not valid\n", __func__); |
| 1005 | return -ENXIO; |
| 1006 | } |
| 1007 | |
| 1008 | if (curr_index < 0) { |
| 1009 | dev_err(di->dev, "requested current limit out-of-range\n"); |
| 1010 | return -ENXIO; |
| 1011 | } |
| 1012 | |
| 1013 | ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER, |
| 1014 | reg, ®_value); |
| 1015 | if (ret < 0) { |
| 1016 | dev_err(di->dev, "%s read failed\n", __func__); |
| 1017 | return ret; |
| 1018 | } |
| 1019 | prev_curr_index = (reg_value >> shift_value); |
| 1020 | |
| 1021 | /* only update current if it's been changed */ |
| 1022 | if (prev_curr_index == curr_index) |
| 1023 | return 0; |
| 1024 | |
| 1025 | dev_dbg(di->dev, "%s set charger current: %d mA for reg: 0x%02x\n", |
| 1026 | __func__, ich, reg); |
| 1027 | |
| 1028 | if (prev_curr_index > curr_index) { |
| 1029 | for (i = prev_curr_index - 1; i >= curr_index; i--) { |
| 1030 | ret = abx500_set_register_interruptible(di->dev, |
| 1031 | AB8500_CHARGER, reg, (u8) i << shift_value); |
| 1032 | if (ret) { |
| 1033 | dev_err(di->dev, "%s write failed\n", __func__); |
| 1034 | return ret; |
| 1035 | } |
| 1036 | usleep_range(STEP_UDELAY, STEP_UDELAY * 2); |
| 1037 | } |
| 1038 | } else { |
| 1039 | for (i = prev_curr_index + 1; i <= curr_index; i++) { |
| 1040 | ret = abx500_set_register_interruptible(di->dev, |
| 1041 | AB8500_CHARGER, reg, (u8) i << shift_value); |
| 1042 | if (ret) { |
| 1043 | dev_err(di->dev, "%s write failed\n", __func__); |
| 1044 | return ret; |
| 1045 | } |
| 1046 | usleep_range(STEP_UDELAY, STEP_UDELAY * 2); |
| 1047 | } |
| 1048 | } |
| 1049 | return ret; |
| 1050 | } |
| 1051 | |
| 1052 | /** |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 1053 | * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit |
| 1054 | * @di: pointer to the ab8500_charger structure |
| 1055 | * @ich_in: charger input current limit |
| 1056 | * |
| 1057 | * Sets the current that can be drawn from the USB host |
| 1058 | * Returns error code in case of failure else 0(on success) |
| 1059 | */ |
| 1060 | static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di, |
| 1061 | int ich_in) |
| 1062 | { |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 1063 | int min_value; |
| 1064 | |
| 1065 | /* We should always use to lowest current limit */ |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 1066 | min_value = min(di->bm->chg_params->usb_curr_max, ich_in); |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 1067 | |
| 1068 | switch (min_value) { |
| 1069 | case 100: |
| 1070 | if (di->vbat < VBAT_TRESH_IP_CUR_RED) |
| 1071 | min_value = USB_CH_IP_CUR_LVL_0P05; |
| 1072 | break; |
| 1073 | case 500: |
| 1074 | if (di->vbat < VBAT_TRESH_IP_CUR_RED) |
| 1075 | min_value = USB_CH_IP_CUR_LVL_0P45; |
| 1076 | break; |
| 1077 | default: |
| 1078 | break; |
| 1079 | } |
| 1080 | |
Johan Bjornstedt | f8e96df | 2012-01-18 12:44:55 +0100 | [diff] [blame] | 1081 | return ab8500_charger_set_current(di, min_value, |
| 1082 | AB8500_USBCH_IPT_CRNTLVL_REG); |
| 1083 | } |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 1084 | |
Johan Bjornstedt | f8e96df | 2012-01-18 12:44:55 +0100 | [diff] [blame] | 1085 | /** |
| 1086 | * ab8500_charger_set_main_in_curr() - set main charger input current |
| 1087 | * @di: pointer to the ab8500_charger structure |
| 1088 | * @ich_in: input charger current, in mA |
| 1089 | * |
| 1090 | * Set main charger input current. |
| 1091 | * Returns error code in case of failure else 0(on success) |
| 1092 | */ |
| 1093 | static int ab8500_charger_set_main_in_curr(struct ab8500_charger *di, |
| 1094 | int ich_in) |
| 1095 | { |
| 1096 | return ab8500_charger_set_current(di, ich_in, |
| 1097 | AB8500_MCH_IPT_CURLVL_REG); |
| 1098 | } |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 1099 | |
Johan Bjornstedt | f8e96df | 2012-01-18 12:44:55 +0100 | [diff] [blame] | 1100 | /** |
| 1101 | * ab8500_charger_set_output_curr() - set charger output current |
| 1102 | * @di: pointer to the ab8500_charger structure |
| 1103 | * @ich_out: output charger current, in mA |
| 1104 | * |
| 1105 | * Set charger output current. |
| 1106 | * Returns error code in case of failure else 0(on success) |
| 1107 | */ |
| 1108 | static int ab8500_charger_set_output_curr(struct ab8500_charger *di, |
| 1109 | int ich_out) |
| 1110 | { |
| 1111 | return ab8500_charger_set_current(di, ich_out, |
| 1112 | AB8500_CH_OPT_CRNTLVL_REG); |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | /** |
| 1116 | * ab8500_charger_led_en() - turn on/off chargign led |
| 1117 | * @di: pointer to the ab8500_charger structure |
| 1118 | * @on: flag to turn on/off the chargign led |
| 1119 | * |
| 1120 | * Power ON/OFF charging LED indication |
| 1121 | * Returns error code in case of failure else 0(on success) |
| 1122 | */ |
| 1123 | static int ab8500_charger_led_en(struct ab8500_charger *di, int on) |
| 1124 | { |
| 1125 | int ret; |
| 1126 | |
| 1127 | if (on) { |
| 1128 | /* Power ON charging LED indicator, set LED current to 5mA */ |
| 1129 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, |
| 1130 | AB8500_LED_INDICATOR_PWM_CTRL, |
| 1131 | (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA)); |
| 1132 | if (ret) { |
| 1133 | dev_err(di->dev, "Power ON LED failed\n"); |
| 1134 | return ret; |
| 1135 | } |
| 1136 | /* LED indicator PWM duty cycle 252/256 */ |
| 1137 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, |
| 1138 | AB8500_LED_INDICATOR_PWM_DUTY, |
| 1139 | LED_INDICATOR_PWM_DUTY_252_256); |
| 1140 | if (ret) { |
| 1141 | dev_err(di->dev, "Set LED PWM duty cycle failed\n"); |
| 1142 | return ret; |
| 1143 | } |
| 1144 | } else { |
| 1145 | /* Power off charging LED indicator */ |
| 1146 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, |
| 1147 | AB8500_LED_INDICATOR_PWM_CTRL, |
| 1148 | LED_INDICATOR_PWM_DIS); |
| 1149 | if (ret) { |
| 1150 | dev_err(di->dev, "Power-off LED failed\n"); |
| 1151 | return ret; |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | return ret; |
| 1156 | } |
| 1157 | |
| 1158 | /** |
| 1159 | * ab8500_charger_ac_en() - enable or disable ac charging |
| 1160 | * @di: pointer to the ab8500_charger structure |
| 1161 | * @enable: enable/disable flag |
| 1162 | * @vset: charging voltage |
| 1163 | * @iset: charging current |
| 1164 | * |
| 1165 | * Enable/Disable AC/Mains charging and turns on/off the charging led |
| 1166 | * respectively. |
| 1167 | **/ |
| 1168 | static int ab8500_charger_ac_en(struct ux500_charger *charger, |
| 1169 | int enable, int vset, int iset) |
| 1170 | { |
| 1171 | int ret; |
| 1172 | int volt_index; |
| 1173 | int curr_index; |
| 1174 | int input_curr_index; |
| 1175 | u8 overshoot = 0; |
| 1176 | |
| 1177 | struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger); |
| 1178 | |
| 1179 | if (enable) { |
| 1180 | /* Check if AC is connected */ |
| 1181 | if (!di->ac.charger_connected) { |
| 1182 | dev_err(di->dev, "AC charger not connected\n"); |
| 1183 | return -ENXIO; |
| 1184 | } |
| 1185 | |
| 1186 | /* Enable AC charging */ |
| 1187 | dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset); |
| 1188 | |
| 1189 | /* |
| 1190 | * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts |
| 1191 | * will be triggered everytime we enable the VDD ADC supply. |
| 1192 | * This will turn off charging for a short while. |
| 1193 | * It can be avoided by having the supply on when |
| 1194 | * there is a charger enabled. Normally the VDD ADC supply |
| 1195 | * is enabled everytime a GPADC conversion is triggered. We will |
| 1196 | * force it to be enabled from this driver to have |
| 1197 | * the GPADC module independant of the AB8500 chargers |
| 1198 | */ |
| 1199 | if (!di->vddadc_en_ac) { |
| 1200 | regulator_enable(di->regu); |
| 1201 | di->vddadc_en_ac = true; |
| 1202 | } |
| 1203 | |
| 1204 | /* Check if the requested voltage or current is valid */ |
| 1205 | volt_index = ab8500_voltage_to_regval(vset); |
| 1206 | curr_index = ab8500_current_to_regval(iset); |
| 1207 | input_curr_index = ab8500_current_to_regval( |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 1208 | di->bm->chg_params->ac_curr_max); |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 1209 | if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) { |
| 1210 | dev_err(di->dev, |
| 1211 | "Charger voltage or current too high, " |
| 1212 | "charging not started\n"); |
| 1213 | return -ENXIO; |
| 1214 | } |
| 1215 | |
| 1216 | /* ChVoltLevel: maximum battery charging voltage */ |
| 1217 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, |
| 1218 | AB8500_CH_VOLT_LVL_REG, (u8) volt_index); |
| 1219 | if (ret) { |
| 1220 | dev_err(di->dev, "%s write failed\n", __func__); |
| 1221 | return ret; |
| 1222 | } |
| 1223 | /* MainChInputCurr: current that can be drawn from the charger*/ |
Johan Bjornstedt | f8e96df | 2012-01-18 12:44:55 +0100 | [diff] [blame] | 1224 | ret = ab8500_charger_set_main_in_curr(di, |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 1225 | di->bm->chg_params->ac_curr_max); |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 1226 | if (ret) { |
Johan Bjornstedt | f8e96df | 2012-01-18 12:44:55 +0100 | [diff] [blame] | 1227 | dev_err(di->dev, "%s Failed to set MainChInputCurr\n", |
| 1228 | __func__); |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 1229 | return ret; |
| 1230 | } |
| 1231 | /* ChOutputCurentLevel: protected output current */ |
Johan Bjornstedt | f8e96df | 2012-01-18 12:44:55 +0100 | [diff] [blame] | 1232 | ret = ab8500_charger_set_output_curr(di, iset); |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 1233 | if (ret) { |
Johan Bjornstedt | f8e96df | 2012-01-18 12:44:55 +0100 | [diff] [blame] | 1234 | dev_err(di->dev, "%s " |
| 1235 | "Failed to set ChOutputCurentLevel\n", |
| 1236 | __func__); |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 1237 | return ret; |
| 1238 | } |
| 1239 | |
| 1240 | /* Check if VBAT overshoot control should be enabled */ |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 1241 | if (!di->bm->enable_overshoot) |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 1242 | overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N; |
| 1243 | |
| 1244 | /* Enable Main Charger */ |
| 1245 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, |
| 1246 | AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot); |
| 1247 | if (ret) { |
| 1248 | dev_err(di->dev, "%s write failed\n", __func__); |
| 1249 | return ret; |
| 1250 | } |
| 1251 | |
| 1252 | /* Power on charging LED indication */ |
| 1253 | ret = ab8500_charger_led_en(di, true); |
| 1254 | if (ret < 0) |
| 1255 | dev_err(di->dev, "failed to enable LED\n"); |
| 1256 | |
| 1257 | di->ac.charger_online = 1; |
| 1258 | } else { |
| 1259 | /* Disable AC charging */ |
| 1260 | if (is_ab8500_1p1_or_earlier(di->parent)) { |
| 1261 | /* |
| 1262 | * For ABB revision 1.0 and 1.1 there is a bug in the |
| 1263 | * watchdog logic. That means we have to continously |
| 1264 | * kick the charger watchdog even when no charger is |
| 1265 | * connected. This is only valid once the AC charger |
| 1266 | * has been enabled. This is a bug that is not handled |
| 1267 | * by the algorithm and the watchdog have to be kicked |
| 1268 | * by the charger driver when the AC charger |
| 1269 | * is disabled |
| 1270 | */ |
| 1271 | if (di->ac_conn) { |
| 1272 | queue_delayed_work(di->charger_wq, |
| 1273 | &di->kick_wd_work, |
| 1274 | round_jiffies(WD_KICK_INTERVAL)); |
| 1275 | } |
| 1276 | |
| 1277 | /* |
| 1278 | * We can't turn off charging completely |
| 1279 | * due to a bug in AB8500 cut1. |
| 1280 | * If we do, charging will not start again. |
| 1281 | * That is why we set the lowest voltage |
| 1282 | * and current possible |
| 1283 | */ |
| 1284 | ret = abx500_set_register_interruptible(di->dev, |
| 1285 | AB8500_CHARGER, |
| 1286 | AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5); |
| 1287 | if (ret) { |
| 1288 | dev_err(di->dev, |
| 1289 | "%s write failed\n", __func__); |
| 1290 | return ret; |
| 1291 | } |
| 1292 | |
Johan Bjornstedt | f8e96df | 2012-01-18 12:44:55 +0100 | [diff] [blame] | 1293 | ret = ab8500_charger_set_output_curr(di, 0); |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 1294 | if (ret) { |
Johan Bjornstedt | f8e96df | 2012-01-18 12:44:55 +0100 | [diff] [blame] | 1295 | dev_err(di->dev, "%s " |
| 1296 | "Failed to set ChOutputCurentLevel\n", |
| 1297 | __func__); |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 1298 | return ret; |
| 1299 | } |
| 1300 | } else { |
| 1301 | ret = abx500_set_register_interruptible(di->dev, |
| 1302 | AB8500_CHARGER, |
| 1303 | AB8500_MCH_CTRL1, 0); |
| 1304 | if (ret) { |
| 1305 | dev_err(di->dev, |
| 1306 | "%s write failed\n", __func__); |
| 1307 | return ret; |
| 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | ret = ab8500_charger_led_en(di, false); |
| 1312 | if (ret < 0) |
| 1313 | dev_err(di->dev, "failed to disable LED\n"); |
| 1314 | |
| 1315 | di->ac.charger_online = 0; |
| 1316 | di->ac.wd_expired = false; |
| 1317 | |
| 1318 | /* Disable regulator if enabled */ |
| 1319 | if (di->vddadc_en_ac) { |
| 1320 | regulator_disable(di->regu); |
| 1321 | di->vddadc_en_ac = false; |
| 1322 | } |
| 1323 | |
| 1324 | dev_dbg(di->dev, "%s Disabled AC charging\n", __func__); |
| 1325 | } |
| 1326 | ab8500_power_supply_changed(di, &di->ac_chg.psy); |
| 1327 | |
| 1328 | return ret; |
| 1329 | } |
| 1330 | |
| 1331 | /** |
| 1332 | * ab8500_charger_usb_en() - enable usb charging |
| 1333 | * @di: pointer to the ab8500_charger structure |
| 1334 | * @enable: enable/disable flag |
| 1335 | * @vset: charging voltage |
| 1336 | * @ich_out: charger output current |
| 1337 | * |
| 1338 | * Enable/Disable USB charging and turns on/off the charging led respectively. |
| 1339 | * Returns error code in case of failure else 0(on success) |
| 1340 | */ |
| 1341 | static int ab8500_charger_usb_en(struct ux500_charger *charger, |
| 1342 | int enable, int vset, int ich_out) |
| 1343 | { |
| 1344 | int ret; |
| 1345 | int volt_index; |
| 1346 | int curr_index; |
| 1347 | u8 overshoot = 0; |
| 1348 | |
| 1349 | struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger); |
| 1350 | |
| 1351 | if (enable) { |
| 1352 | /* Check if USB is connected */ |
| 1353 | if (!di->usb.charger_connected) { |
| 1354 | dev_err(di->dev, "USB charger not connected\n"); |
| 1355 | return -ENXIO; |
| 1356 | } |
| 1357 | |
| 1358 | /* |
| 1359 | * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts |
| 1360 | * will be triggered everytime we enable the VDD ADC supply. |
| 1361 | * This will turn off charging for a short while. |
| 1362 | * It can be avoided by having the supply on when |
| 1363 | * there is a charger enabled. Normally the VDD ADC supply |
| 1364 | * is enabled everytime a GPADC conversion is triggered. We will |
| 1365 | * force it to be enabled from this driver to have |
| 1366 | * the GPADC module independant of the AB8500 chargers |
| 1367 | */ |
| 1368 | if (!di->vddadc_en_usb) { |
| 1369 | regulator_enable(di->regu); |
| 1370 | di->vddadc_en_usb = true; |
| 1371 | } |
| 1372 | |
| 1373 | /* Enable USB charging */ |
| 1374 | dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out); |
| 1375 | |
| 1376 | /* Check if the requested voltage or current is valid */ |
| 1377 | volt_index = ab8500_voltage_to_regval(vset); |
| 1378 | curr_index = ab8500_current_to_regval(ich_out); |
| 1379 | if (volt_index < 0 || curr_index < 0) { |
| 1380 | dev_err(di->dev, |
| 1381 | "Charger voltage or current too high, " |
| 1382 | "charging not started\n"); |
| 1383 | return -ENXIO; |
| 1384 | } |
| 1385 | |
| 1386 | /* ChVoltLevel: max voltage upto which battery can be charged */ |
| 1387 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, |
| 1388 | AB8500_CH_VOLT_LVL_REG, (u8) volt_index); |
| 1389 | if (ret) { |
| 1390 | dev_err(di->dev, "%s write failed\n", __func__); |
| 1391 | return ret; |
| 1392 | } |
| 1393 | /* USBChInputCurr: current that can be drawn from the usb */ |
| 1394 | ret = ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr); |
| 1395 | if (ret) { |
| 1396 | dev_err(di->dev, "setting USBChInputCurr failed\n"); |
| 1397 | return ret; |
| 1398 | } |
| 1399 | /* ChOutputCurentLevel: protected output current */ |
Johan Bjornstedt | f8e96df | 2012-01-18 12:44:55 +0100 | [diff] [blame] | 1400 | ret = ab8500_charger_set_output_curr(di, ich_out); |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 1401 | if (ret) { |
Johan Bjornstedt | f8e96df | 2012-01-18 12:44:55 +0100 | [diff] [blame] | 1402 | dev_err(di->dev, "%s " |
| 1403 | "Failed to set ChOutputCurentLevel\n", |
| 1404 | __func__); |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 1405 | return ret; |
| 1406 | } |
| 1407 | /* Check if VBAT overshoot control should be enabled */ |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 1408 | if (!di->bm->enable_overshoot) |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 1409 | overshoot = USB_CHG_NO_OVERSHOOT_ENA_N; |
| 1410 | |
| 1411 | /* Enable USB Charger */ |
| 1412 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, |
| 1413 | AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot); |
| 1414 | if (ret) { |
| 1415 | dev_err(di->dev, "%s write failed\n", __func__); |
| 1416 | return ret; |
| 1417 | } |
| 1418 | |
| 1419 | /* If success power on charging LED indication */ |
| 1420 | ret = ab8500_charger_led_en(di, true); |
| 1421 | if (ret < 0) |
| 1422 | dev_err(di->dev, "failed to enable LED\n"); |
| 1423 | |
| 1424 | queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ); |
| 1425 | |
| 1426 | di->usb.charger_online = 1; |
| 1427 | } else { |
| 1428 | /* Disable USB charging */ |
| 1429 | ret = abx500_set_register_interruptible(di->dev, |
| 1430 | AB8500_CHARGER, |
| 1431 | AB8500_USBCH_CTRL1_REG, 0); |
| 1432 | if (ret) { |
| 1433 | dev_err(di->dev, |
| 1434 | "%s write failed\n", __func__); |
| 1435 | return ret; |
| 1436 | } |
| 1437 | |
| 1438 | ret = ab8500_charger_led_en(di, false); |
| 1439 | if (ret < 0) |
| 1440 | dev_err(di->dev, "failed to disable LED\n"); |
| 1441 | |
| 1442 | di->usb.charger_online = 0; |
| 1443 | di->usb.wd_expired = false; |
| 1444 | |
| 1445 | /* Disable regulator if enabled */ |
| 1446 | if (di->vddadc_en_usb) { |
| 1447 | regulator_disable(di->regu); |
| 1448 | di->vddadc_en_usb = false; |
| 1449 | } |
| 1450 | |
| 1451 | dev_dbg(di->dev, "%s Disabled USB charging\n", __func__); |
| 1452 | |
| 1453 | /* Cancel any pending Vbat check work */ |
| 1454 | if (delayed_work_pending(&di->check_vbat_work)) |
| 1455 | cancel_delayed_work(&di->check_vbat_work); |
| 1456 | |
| 1457 | } |
| 1458 | ab8500_power_supply_changed(di, &di->usb_chg.psy); |
| 1459 | |
| 1460 | return ret; |
| 1461 | } |
| 1462 | |
| 1463 | /** |
| 1464 | * ab8500_charger_watchdog_kick() - kick charger watchdog |
| 1465 | * @di: pointer to the ab8500_charger structure |
| 1466 | * |
| 1467 | * Kick charger watchdog |
| 1468 | * Returns error code in case of failure else 0(on success) |
| 1469 | */ |
| 1470 | static int ab8500_charger_watchdog_kick(struct ux500_charger *charger) |
| 1471 | { |
| 1472 | int ret; |
| 1473 | struct ab8500_charger *di; |
| 1474 | |
| 1475 | if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS) |
| 1476 | di = to_ab8500_charger_ac_device_info(charger); |
| 1477 | else if (charger->psy.type == POWER_SUPPLY_TYPE_USB) |
| 1478 | di = to_ab8500_charger_usb_device_info(charger); |
| 1479 | else |
| 1480 | return -ENXIO; |
| 1481 | |
| 1482 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, |
| 1483 | AB8500_CHARG_WD_CTRL, CHARG_WD_KICK); |
| 1484 | if (ret) |
| 1485 | dev_err(di->dev, "Failed to kick WD!\n"); |
| 1486 | |
| 1487 | return ret; |
| 1488 | } |
| 1489 | |
| 1490 | /** |
| 1491 | * ab8500_charger_update_charger_current() - update charger current |
| 1492 | * @di: pointer to the ab8500_charger structure |
| 1493 | * |
| 1494 | * Update the charger output current for the specified charger |
| 1495 | * Returns error code in case of failure else 0(on success) |
| 1496 | */ |
| 1497 | static int ab8500_charger_update_charger_current(struct ux500_charger *charger, |
| 1498 | int ich_out) |
| 1499 | { |
| 1500 | int ret; |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 1501 | struct ab8500_charger *di; |
| 1502 | |
| 1503 | if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS) |
| 1504 | di = to_ab8500_charger_ac_device_info(charger); |
| 1505 | else if (charger->psy.type == POWER_SUPPLY_TYPE_USB) |
| 1506 | di = to_ab8500_charger_usb_device_info(charger); |
| 1507 | else |
| 1508 | return -ENXIO; |
| 1509 | |
Johan Bjornstedt | f8e96df | 2012-01-18 12:44:55 +0100 | [diff] [blame] | 1510 | ret = ab8500_charger_set_output_curr(di, ich_out); |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 1511 | if (ret) { |
Johan Bjornstedt | f8e96df | 2012-01-18 12:44:55 +0100 | [diff] [blame] | 1512 | dev_err(di->dev, "%s " |
| 1513 | "Failed to set ChOutputCurentLevel\n", |
| 1514 | __func__); |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 1515 | return ret; |
| 1516 | } |
| 1517 | |
| 1518 | /* Reset the main and usb drop input current measurement counter */ |
| 1519 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, |
| 1520 | AB8500_CHARGER_CTRL, |
| 1521 | 0x1); |
| 1522 | if (ret) { |
| 1523 | dev_err(di->dev, "%s write failed\n", __func__); |
| 1524 | return ret; |
| 1525 | } |
| 1526 | |
| 1527 | return ret; |
| 1528 | } |
| 1529 | |
| 1530 | static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data) |
| 1531 | { |
| 1532 | struct power_supply *psy; |
| 1533 | struct power_supply *ext; |
| 1534 | struct ab8500_charger *di; |
| 1535 | union power_supply_propval ret; |
| 1536 | int i, j; |
| 1537 | bool psy_found = false; |
| 1538 | struct ux500_charger *usb_chg; |
| 1539 | |
| 1540 | usb_chg = (struct ux500_charger *)data; |
| 1541 | psy = &usb_chg->psy; |
| 1542 | |
| 1543 | di = to_ab8500_charger_usb_device_info(usb_chg); |
| 1544 | |
| 1545 | ext = dev_get_drvdata(dev); |
| 1546 | |
| 1547 | /* For all psy where the driver name appears in any supplied_to */ |
| 1548 | for (i = 0; i < ext->num_supplicants; i++) { |
| 1549 | if (!strcmp(ext->supplied_to[i], psy->name)) |
| 1550 | psy_found = true; |
| 1551 | } |
| 1552 | |
| 1553 | if (!psy_found) |
| 1554 | return 0; |
| 1555 | |
| 1556 | /* Go through all properties for the psy */ |
| 1557 | for (j = 0; j < ext->num_properties; j++) { |
| 1558 | enum power_supply_property prop; |
| 1559 | prop = ext->properties[j]; |
| 1560 | |
| 1561 | if (ext->get_property(ext, prop, &ret)) |
| 1562 | continue; |
| 1563 | |
| 1564 | switch (prop) { |
| 1565 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 1566 | switch (ext->type) { |
| 1567 | case POWER_SUPPLY_TYPE_BATTERY: |
| 1568 | di->vbat = ret.intval / 1000; |
| 1569 | break; |
| 1570 | default: |
| 1571 | break; |
| 1572 | } |
| 1573 | break; |
| 1574 | default: |
| 1575 | break; |
| 1576 | } |
| 1577 | } |
| 1578 | return 0; |
| 1579 | } |
| 1580 | |
| 1581 | /** |
| 1582 | * ab8500_charger_check_vbat_work() - keep vbus current within spec |
| 1583 | * @work pointer to the work_struct structure |
| 1584 | * |
| 1585 | * Due to a asic bug it is necessary to lower the input current to the vbus |
| 1586 | * charger when charging with at some specific levels. This issue is only valid |
| 1587 | * for below a certain battery voltage. This function makes sure that the |
| 1588 | * the allowed current limit isn't exceeded. |
| 1589 | */ |
| 1590 | static void ab8500_charger_check_vbat_work(struct work_struct *work) |
| 1591 | { |
| 1592 | int t = 10; |
| 1593 | struct ab8500_charger *di = container_of(work, |
| 1594 | struct ab8500_charger, check_vbat_work.work); |
| 1595 | |
| 1596 | class_for_each_device(power_supply_class, NULL, |
| 1597 | &di->usb_chg.psy, ab8500_charger_get_ext_psy_data); |
| 1598 | |
| 1599 | /* First run old_vbat is 0. */ |
| 1600 | if (di->old_vbat == 0) |
| 1601 | di->old_vbat = di->vbat; |
| 1602 | |
| 1603 | if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED && |
| 1604 | di->vbat <= VBAT_TRESH_IP_CUR_RED) || |
| 1605 | (di->old_vbat > VBAT_TRESH_IP_CUR_RED && |
| 1606 | di->vbat > VBAT_TRESH_IP_CUR_RED))) { |
| 1607 | |
| 1608 | dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d," |
| 1609 | " old: %d\n", di->max_usb_in_curr, di->vbat, |
| 1610 | di->old_vbat); |
| 1611 | ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr); |
| 1612 | power_supply_changed(&di->usb_chg.psy); |
| 1613 | } |
| 1614 | |
| 1615 | di->old_vbat = di->vbat; |
| 1616 | |
| 1617 | /* |
| 1618 | * No need to check the battery voltage every second when not close to |
| 1619 | * the threshold. |
| 1620 | */ |
| 1621 | if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) && |
| 1622 | (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100))) |
| 1623 | t = 1; |
| 1624 | |
| 1625 | queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ); |
| 1626 | } |
| 1627 | |
| 1628 | /** |
| 1629 | * ab8500_charger_check_hw_failure_work() - check main charger failure |
| 1630 | * @work: pointer to the work_struct structure |
| 1631 | * |
| 1632 | * Work queue function for checking the main charger status |
| 1633 | */ |
| 1634 | static void ab8500_charger_check_hw_failure_work(struct work_struct *work) |
| 1635 | { |
| 1636 | int ret; |
| 1637 | u8 reg_value; |
| 1638 | |
| 1639 | struct ab8500_charger *di = container_of(work, |
| 1640 | struct ab8500_charger, check_hw_failure_work.work); |
| 1641 | |
| 1642 | /* Check if the status bits for HW failure is still active */ |
| 1643 | if (di->flags.mainextchnotok) { |
| 1644 | ret = abx500_get_register_interruptible(di->dev, |
| 1645 | AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value); |
| 1646 | if (ret < 0) { |
| 1647 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); |
| 1648 | return; |
| 1649 | } |
| 1650 | if (!(reg_value & MAIN_CH_NOK)) { |
| 1651 | di->flags.mainextchnotok = false; |
| 1652 | ab8500_power_supply_changed(di, &di->ac_chg.psy); |
| 1653 | } |
| 1654 | } |
| 1655 | if (di->flags.vbus_ovv) { |
| 1656 | ret = abx500_get_register_interruptible(di->dev, |
| 1657 | AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, |
| 1658 | ®_value); |
| 1659 | if (ret < 0) { |
| 1660 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); |
| 1661 | return; |
| 1662 | } |
| 1663 | if (!(reg_value & VBUS_OVV_TH)) { |
| 1664 | di->flags.vbus_ovv = false; |
| 1665 | ab8500_power_supply_changed(di, &di->usb_chg.psy); |
| 1666 | } |
| 1667 | } |
| 1668 | /* If we still have a failure, schedule a new check */ |
| 1669 | if (di->flags.mainextchnotok || di->flags.vbus_ovv) { |
| 1670 | queue_delayed_work(di->charger_wq, |
| 1671 | &di->check_hw_failure_work, round_jiffies(HZ)); |
| 1672 | } |
| 1673 | } |
| 1674 | |
| 1675 | /** |
| 1676 | * ab8500_charger_kick_watchdog_work() - kick the watchdog |
| 1677 | * @work: pointer to the work_struct structure |
| 1678 | * |
| 1679 | * Work queue function for kicking the charger watchdog. |
| 1680 | * |
| 1681 | * For ABB revision 1.0 and 1.1 there is a bug in the watchdog |
| 1682 | * logic. That means we have to continously kick the charger |
| 1683 | * watchdog even when no charger is connected. This is only |
| 1684 | * valid once the AC charger has been enabled. This is |
| 1685 | * a bug that is not handled by the algorithm and the |
| 1686 | * watchdog have to be kicked by the charger driver |
| 1687 | * when the AC charger is disabled |
| 1688 | */ |
| 1689 | static void ab8500_charger_kick_watchdog_work(struct work_struct *work) |
| 1690 | { |
| 1691 | int ret; |
| 1692 | |
| 1693 | struct ab8500_charger *di = container_of(work, |
| 1694 | struct ab8500_charger, kick_wd_work.work); |
| 1695 | |
| 1696 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, |
| 1697 | AB8500_CHARG_WD_CTRL, CHARG_WD_KICK); |
| 1698 | if (ret) |
| 1699 | dev_err(di->dev, "Failed to kick WD!\n"); |
| 1700 | |
| 1701 | /* Schedule a new watchdog kick */ |
| 1702 | queue_delayed_work(di->charger_wq, |
| 1703 | &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL)); |
| 1704 | } |
| 1705 | |
| 1706 | /** |
| 1707 | * ab8500_charger_ac_work() - work to get and set main charger status |
| 1708 | * @work: pointer to the work_struct structure |
| 1709 | * |
| 1710 | * Work queue function for checking the main charger status |
| 1711 | */ |
| 1712 | static void ab8500_charger_ac_work(struct work_struct *work) |
| 1713 | { |
| 1714 | int ret; |
| 1715 | |
| 1716 | struct ab8500_charger *di = container_of(work, |
| 1717 | struct ab8500_charger, ac_work); |
| 1718 | |
| 1719 | /* |
| 1720 | * Since we can't be sure that the events are received |
| 1721 | * synchronously, we have the check if the main charger is |
| 1722 | * connected by reading the status register |
| 1723 | */ |
| 1724 | ret = ab8500_charger_detect_chargers(di); |
| 1725 | if (ret < 0) |
| 1726 | return; |
| 1727 | |
| 1728 | if (ret & AC_PW_CONN) { |
| 1729 | di->ac.charger_connected = 1; |
| 1730 | di->ac_conn = true; |
| 1731 | } else { |
| 1732 | di->ac.charger_connected = 0; |
| 1733 | } |
| 1734 | |
| 1735 | ab8500_power_supply_changed(di, &di->ac_chg.psy); |
| 1736 | sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present"); |
| 1737 | } |
| 1738 | |
Lee Jones | b269fff | 2013-01-11 13:12:51 +0000 | [diff] [blame] | 1739 | static void ab8500_charger_usb_attached_work(struct work_struct *work) |
| 1740 | { |
| 1741 | struct ab8500_charger *di = container_of(work, |
| 1742 | struct ab8500_charger, |
| 1743 | usb_charger_attached_work.work); |
| 1744 | int usbch = (USB_CH_VBUSDROP | USB_CH_VBUSDETDBNC); |
| 1745 | int ret, i; |
| 1746 | u8 statval; |
| 1747 | |
| 1748 | for (i = 0; i < 10; i++) { |
| 1749 | ret = abx500_get_register_interruptible(di->dev, |
| 1750 | AB8500_CHARGER, |
| 1751 | AB8500_CH_USBCH_STAT1_REG, |
| 1752 | &statval); |
| 1753 | if (ret < 0) { |
| 1754 | dev_err(di->dev, "ab8500 read failed %d\n", __LINE__); |
| 1755 | goto reschedule; |
| 1756 | } |
| 1757 | if ((statval & usbch) != usbch) |
| 1758 | goto reschedule; |
| 1759 | |
| 1760 | msleep(CHARGER_STATUS_POLL); |
| 1761 | } |
| 1762 | |
| 1763 | ab8500_charger_usb_en(&di->usb_chg, 0, 0, 0); |
| 1764 | |
| 1765 | mutex_lock(&di->charger_attached_mutex); |
| 1766 | mutex_unlock(&di->charger_attached_mutex); |
| 1767 | |
| 1768 | return; |
| 1769 | |
| 1770 | reschedule: |
| 1771 | queue_delayed_work(di->charger_wq, |
| 1772 | &di->usb_charger_attached_work, |
| 1773 | HZ); |
| 1774 | } |
| 1775 | |
| 1776 | static void ab8500_charger_ac_attached_work(struct work_struct *work) |
| 1777 | { |
| 1778 | |
| 1779 | struct ab8500_charger *di = container_of(work, |
| 1780 | struct ab8500_charger, |
| 1781 | ac_charger_attached_work.work); |
| 1782 | int mainch = (MAIN_CH_STATUS2_MAINCHGDROP | |
| 1783 | MAIN_CH_STATUS2_MAINCHARGERDETDBNC); |
| 1784 | int ret, i; |
| 1785 | u8 statval; |
| 1786 | |
| 1787 | for (i = 0; i < 10; i++) { |
| 1788 | ret = abx500_get_register_interruptible(di->dev, |
| 1789 | AB8500_CHARGER, |
| 1790 | AB8500_CH_STATUS2_REG, |
| 1791 | &statval); |
| 1792 | if (ret < 0) { |
| 1793 | dev_err(di->dev, "ab8500 read failed %d\n", __LINE__); |
| 1794 | goto reschedule; |
| 1795 | } |
| 1796 | |
| 1797 | if ((statval & mainch) != mainch) |
| 1798 | goto reschedule; |
| 1799 | |
| 1800 | msleep(CHARGER_STATUS_POLL); |
| 1801 | } |
| 1802 | |
| 1803 | ab8500_charger_ac_en(&di->ac_chg, 0, 0, 0); |
| 1804 | queue_work(di->charger_wq, &di->ac_work); |
| 1805 | |
| 1806 | mutex_lock(&di->charger_attached_mutex); |
| 1807 | mutex_unlock(&di->charger_attached_mutex); |
| 1808 | |
| 1809 | return; |
| 1810 | |
| 1811 | reschedule: |
| 1812 | queue_delayed_work(di->charger_wq, |
| 1813 | &di->ac_charger_attached_work, |
| 1814 | HZ); |
| 1815 | } |
| 1816 | |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 1817 | /** |
| 1818 | * ab8500_charger_detect_usb_type_work() - work to detect USB type |
| 1819 | * @work: Pointer to the work_struct structure |
| 1820 | * |
| 1821 | * Detect the type of USB plugged |
| 1822 | */ |
Anton Vorontsov | 64eb9b0 | 2012-03-14 04:43:11 +0400 | [diff] [blame] | 1823 | static void ab8500_charger_detect_usb_type_work(struct work_struct *work) |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 1824 | { |
| 1825 | int ret; |
| 1826 | |
| 1827 | struct ab8500_charger *di = container_of(work, |
| 1828 | struct ab8500_charger, detect_usb_type_work); |
| 1829 | |
| 1830 | /* |
| 1831 | * Since we can't be sure that the events are received |
| 1832 | * synchronously, we have the check if is |
| 1833 | * connected by reading the status register |
| 1834 | */ |
| 1835 | ret = ab8500_charger_detect_chargers(di); |
| 1836 | if (ret < 0) |
| 1837 | return; |
| 1838 | |
| 1839 | if (!(ret & USB_PW_CONN)) { |
| 1840 | di->vbus_detected = 0; |
| 1841 | ab8500_charger_set_usb_connected(di, false); |
| 1842 | ab8500_power_supply_changed(di, &di->usb_chg.psy); |
| 1843 | } else { |
| 1844 | di->vbus_detected = 1; |
| 1845 | |
| 1846 | if (is_ab8500_1p1_or_earlier(di->parent)) { |
| 1847 | ret = ab8500_charger_detect_usb_type(di); |
| 1848 | if (!ret) { |
| 1849 | ab8500_charger_set_usb_connected(di, true); |
| 1850 | ab8500_power_supply_changed(di, |
| 1851 | &di->usb_chg.psy); |
| 1852 | } |
| 1853 | } else { |
| 1854 | /* For ABB cut2.0 and onwards we have an IRQ, |
| 1855 | * USB_LINK_STATUS that will be triggered when the USB |
| 1856 | * link status changes. The exception is USB connected |
| 1857 | * during startup. Then we don't get a |
| 1858 | * USB_LINK_STATUS IRQ |
| 1859 | */ |
| 1860 | if (di->vbus_detected_start) { |
| 1861 | di->vbus_detected_start = false; |
| 1862 | ret = ab8500_charger_detect_usb_type(di); |
| 1863 | if (!ret) { |
| 1864 | ab8500_charger_set_usb_connected(di, |
| 1865 | true); |
| 1866 | ab8500_power_supply_changed(di, |
| 1867 | &di->usb_chg.psy); |
| 1868 | } |
| 1869 | } |
| 1870 | } |
| 1871 | } |
| 1872 | } |
| 1873 | |
| 1874 | /** |
| 1875 | * ab8500_charger_usb_link_status_work() - work to detect USB type |
| 1876 | * @work: pointer to the work_struct structure |
| 1877 | * |
| 1878 | * Detect the type of USB plugged |
| 1879 | */ |
| 1880 | static void ab8500_charger_usb_link_status_work(struct work_struct *work) |
| 1881 | { |
| 1882 | int ret; |
| 1883 | |
| 1884 | struct ab8500_charger *di = container_of(work, |
| 1885 | struct ab8500_charger, usb_link_status_work); |
| 1886 | |
| 1887 | /* |
| 1888 | * Since we can't be sure that the events are received |
| 1889 | * synchronously, we have the check if is |
| 1890 | * connected by reading the status register |
| 1891 | */ |
| 1892 | ret = ab8500_charger_detect_chargers(di); |
| 1893 | if (ret < 0) |
| 1894 | return; |
| 1895 | |
| 1896 | if (!(ret & USB_PW_CONN)) { |
| 1897 | di->vbus_detected = 0; |
| 1898 | ab8500_charger_set_usb_connected(di, false); |
| 1899 | ab8500_power_supply_changed(di, &di->usb_chg.psy); |
| 1900 | } else { |
| 1901 | di->vbus_detected = 1; |
| 1902 | ret = ab8500_charger_read_usb_type(di); |
| 1903 | if (!ret) { |
| 1904 | /* Update maximum input current */ |
| 1905 | ret = ab8500_charger_set_vbus_in_curr(di, |
| 1906 | di->max_usb_in_curr); |
| 1907 | if (ret) |
| 1908 | return; |
| 1909 | |
| 1910 | ab8500_charger_set_usb_connected(di, true); |
| 1911 | ab8500_power_supply_changed(di, &di->usb_chg.psy); |
| 1912 | } else if (ret == -ENXIO) { |
| 1913 | /* No valid charger type detected */ |
| 1914 | ab8500_charger_set_usb_connected(di, false); |
| 1915 | ab8500_power_supply_changed(di, &di->usb_chg.psy); |
| 1916 | } |
| 1917 | } |
| 1918 | } |
| 1919 | |
| 1920 | static void ab8500_charger_usb_state_changed_work(struct work_struct *work) |
| 1921 | { |
| 1922 | int ret; |
| 1923 | unsigned long flags; |
| 1924 | |
| 1925 | struct ab8500_charger *di = container_of(work, |
| 1926 | struct ab8500_charger, usb_state_changed_work); |
| 1927 | |
| 1928 | if (!di->vbus_detected) |
| 1929 | return; |
| 1930 | |
| 1931 | spin_lock_irqsave(&di->usb_state.usb_lock, flags); |
| 1932 | di->usb_state.usb_changed = false; |
| 1933 | spin_unlock_irqrestore(&di->usb_state.usb_lock, flags); |
| 1934 | |
| 1935 | /* |
| 1936 | * wait for some time until you get updates from the usb stack |
| 1937 | * and negotiations are completed |
| 1938 | */ |
| 1939 | msleep(250); |
| 1940 | |
| 1941 | if (di->usb_state.usb_changed) |
| 1942 | return; |
| 1943 | |
| 1944 | dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n", |
| 1945 | __func__, di->usb_state.state, di->usb_state.usb_current); |
| 1946 | |
| 1947 | switch (di->usb_state.state) { |
| 1948 | case AB8500_BM_USB_STATE_RESET_HS: |
| 1949 | case AB8500_BM_USB_STATE_RESET_FS: |
| 1950 | case AB8500_BM_USB_STATE_SUSPEND: |
| 1951 | case AB8500_BM_USB_STATE_MAX: |
| 1952 | ab8500_charger_set_usb_connected(di, false); |
| 1953 | ab8500_power_supply_changed(di, &di->usb_chg.psy); |
| 1954 | break; |
| 1955 | |
| 1956 | case AB8500_BM_USB_STATE_RESUME: |
| 1957 | /* |
| 1958 | * when suspend->resume there should be delay |
| 1959 | * of 1sec for enabling charging |
| 1960 | */ |
| 1961 | msleep(1000); |
| 1962 | /* Intentional fall through */ |
| 1963 | case AB8500_BM_USB_STATE_CONFIGURED: |
| 1964 | /* |
| 1965 | * USB is configured, enable charging with the charging |
| 1966 | * input current obtained from USB driver |
| 1967 | */ |
| 1968 | if (!ab8500_charger_get_usb_cur(di)) { |
| 1969 | /* Update maximum input current */ |
| 1970 | ret = ab8500_charger_set_vbus_in_curr(di, |
| 1971 | di->max_usb_in_curr); |
| 1972 | if (ret) |
| 1973 | return; |
| 1974 | |
| 1975 | ab8500_charger_set_usb_connected(di, true); |
| 1976 | ab8500_power_supply_changed(di, &di->usb_chg.psy); |
| 1977 | } |
| 1978 | break; |
| 1979 | |
| 1980 | default: |
| 1981 | break; |
| 1982 | }; |
| 1983 | } |
| 1984 | |
| 1985 | /** |
| 1986 | * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status |
| 1987 | * @work: pointer to the work_struct structure |
| 1988 | * |
| 1989 | * Work queue function for checking the USB charger Not OK status |
| 1990 | */ |
| 1991 | static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work) |
| 1992 | { |
| 1993 | int ret; |
| 1994 | u8 reg_value; |
| 1995 | bool prev_status; |
| 1996 | |
| 1997 | struct ab8500_charger *di = container_of(work, |
| 1998 | struct ab8500_charger, check_usbchgnotok_work.work); |
| 1999 | |
| 2000 | /* Check if the status bit for usbchargernotok is still active */ |
| 2001 | ret = abx500_get_register_interruptible(di->dev, |
| 2002 | AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value); |
| 2003 | if (ret < 0) { |
| 2004 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); |
| 2005 | return; |
| 2006 | } |
| 2007 | prev_status = di->flags.usbchargernotok; |
| 2008 | |
| 2009 | if (reg_value & VBUS_CH_NOK) { |
| 2010 | di->flags.usbchargernotok = true; |
| 2011 | /* Check again in 1sec */ |
| 2012 | queue_delayed_work(di->charger_wq, |
| 2013 | &di->check_usbchgnotok_work, HZ); |
| 2014 | } else { |
| 2015 | di->flags.usbchargernotok = false; |
| 2016 | di->flags.vbus_collapse = false; |
| 2017 | } |
| 2018 | |
| 2019 | if (prev_status != di->flags.usbchargernotok) |
| 2020 | ab8500_power_supply_changed(di, &di->usb_chg.psy); |
| 2021 | } |
| 2022 | |
| 2023 | /** |
| 2024 | * ab8500_charger_check_main_thermal_prot_work() - check main thermal status |
| 2025 | * @work: pointer to the work_struct structure |
| 2026 | * |
| 2027 | * Work queue function for checking the Main thermal prot status |
| 2028 | */ |
| 2029 | static void ab8500_charger_check_main_thermal_prot_work( |
| 2030 | struct work_struct *work) |
| 2031 | { |
| 2032 | int ret; |
| 2033 | u8 reg_value; |
| 2034 | |
| 2035 | struct ab8500_charger *di = container_of(work, |
| 2036 | struct ab8500_charger, check_main_thermal_prot_work); |
| 2037 | |
| 2038 | /* Check if the status bit for main_thermal_prot is still active */ |
| 2039 | ret = abx500_get_register_interruptible(di->dev, |
| 2040 | AB8500_CHARGER, AB8500_CH_STATUS2_REG, ®_value); |
| 2041 | if (ret < 0) { |
| 2042 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); |
| 2043 | return; |
| 2044 | } |
| 2045 | if (reg_value & MAIN_CH_TH_PROT) |
| 2046 | di->flags.main_thermal_prot = true; |
| 2047 | else |
| 2048 | di->flags.main_thermal_prot = false; |
| 2049 | |
| 2050 | ab8500_power_supply_changed(di, &di->ac_chg.psy); |
| 2051 | } |
| 2052 | |
| 2053 | /** |
| 2054 | * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status |
| 2055 | * @work: pointer to the work_struct structure |
| 2056 | * |
| 2057 | * Work queue function for checking the USB thermal prot status |
| 2058 | */ |
| 2059 | static void ab8500_charger_check_usb_thermal_prot_work( |
| 2060 | struct work_struct *work) |
| 2061 | { |
| 2062 | int ret; |
| 2063 | u8 reg_value; |
| 2064 | |
| 2065 | struct ab8500_charger *di = container_of(work, |
| 2066 | struct ab8500_charger, check_usb_thermal_prot_work); |
| 2067 | |
| 2068 | /* Check if the status bit for usb_thermal_prot is still active */ |
| 2069 | ret = abx500_get_register_interruptible(di->dev, |
| 2070 | AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, ®_value); |
| 2071 | if (ret < 0) { |
| 2072 | dev_err(di->dev, "%s ab8500 read failed\n", __func__); |
| 2073 | return; |
| 2074 | } |
| 2075 | if (reg_value & USB_CH_TH_PROT) |
| 2076 | di->flags.usb_thermal_prot = true; |
| 2077 | else |
| 2078 | di->flags.usb_thermal_prot = false; |
| 2079 | |
| 2080 | ab8500_power_supply_changed(di, &di->usb_chg.psy); |
| 2081 | } |
| 2082 | |
| 2083 | /** |
| 2084 | * ab8500_charger_mainchunplugdet_handler() - main charger unplugged |
| 2085 | * @irq: interrupt number |
| 2086 | * @_di: pointer to the ab8500_charger structure |
| 2087 | * |
| 2088 | * Returns IRQ status(IRQ_HANDLED) |
| 2089 | */ |
| 2090 | static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di) |
| 2091 | { |
| 2092 | struct ab8500_charger *di = _di; |
| 2093 | |
| 2094 | dev_dbg(di->dev, "Main charger unplugged\n"); |
| 2095 | queue_work(di->charger_wq, &di->ac_work); |
| 2096 | |
Lee Jones | b269fff | 2013-01-11 13:12:51 +0000 | [diff] [blame] | 2097 | cancel_delayed_work_sync(&di->ac_charger_attached_work); |
| 2098 | mutex_lock(&di->charger_attached_mutex); |
| 2099 | mutex_unlock(&di->charger_attached_mutex); |
| 2100 | |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2101 | return IRQ_HANDLED; |
| 2102 | } |
| 2103 | |
| 2104 | /** |
| 2105 | * ab8500_charger_mainchplugdet_handler() - main charger plugged |
| 2106 | * @irq: interrupt number |
| 2107 | * @_di: pointer to the ab8500_charger structure |
| 2108 | * |
| 2109 | * Returns IRQ status(IRQ_HANDLED) |
| 2110 | */ |
| 2111 | static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di) |
| 2112 | { |
| 2113 | struct ab8500_charger *di = _di; |
| 2114 | |
| 2115 | dev_dbg(di->dev, "Main charger plugged\n"); |
| 2116 | queue_work(di->charger_wq, &di->ac_work); |
| 2117 | |
Lee Jones | b269fff | 2013-01-11 13:12:51 +0000 | [diff] [blame] | 2118 | mutex_lock(&di->charger_attached_mutex); |
| 2119 | mutex_unlock(&di->charger_attached_mutex); |
| 2120 | queue_delayed_work(di->charger_wq, |
| 2121 | &di->ac_charger_attached_work, |
| 2122 | HZ); |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2123 | return IRQ_HANDLED; |
| 2124 | } |
| 2125 | |
| 2126 | /** |
| 2127 | * ab8500_charger_mainextchnotok_handler() - main charger not ok |
| 2128 | * @irq: interrupt number |
| 2129 | * @_di: pointer to the ab8500_charger structure |
| 2130 | * |
| 2131 | * Returns IRQ status(IRQ_HANDLED) |
| 2132 | */ |
| 2133 | static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di) |
| 2134 | { |
| 2135 | struct ab8500_charger *di = _di; |
| 2136 | |
| 2137 | dev_dbg(di->dev, "Main charger not ok\n"); |
| 2138 | di->flags.mainextchnotok = true; |
| 2139 | ab8500_power_supply_changed(di, &di->ac_chg.psy); |
| 2140 | |
| 2141 | /* Schedule a new HW failure check */ |
| 2142 | queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0); |
| 2143 | |
| 2144 | return IRQ_HANDLED; |
| 2145 | } |
| 2146 | |
| 2147 | /** |
| 2148 | * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger |
| 2149 | * thermal protection threshold |
| 2150 | * @irq: interrupt number |
| 2151 | * @_di: pointer to the ab8500_charger structure |
| 2152 | * |
| 2153 | * Returns IRQ status(IRQ_HANDLED) |
| 2154 | */ |
| 2155 | static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di) |
| 2156 | { |
| 2157 | struct ab8500_charger *di = _di; |
| 2158 | |
| 2159 | dev_dbg(di->dev, |
| 2160 | "Die temp above Main charger thermal protection threshold\n"); |
| 2161 | queue_work(di->charger_wq, &di->check_main_thermal_prot_work); |
| 2162 | |
| 2163 | return IRQ_HANDLED; |
| 2164 | } |
| 2165 | |
| 2166 | /** |
| 2167 | * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger |
| 2168 | * thermal protection threshold |
| 2169 | * @irq: interrupt number |
| 2170 | * @_di: pointer to the ab8500_charger structure |
| 2171 | * |
| 2172 | * Returns IRQ status(IRQ_HANDLED) |
| 2173 | */ |
| 2174 | static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di) |
| 2175 | { |
| 2176 | struct ab8500_charger *di = _di; |
| 2177 | |
| 2178 | dev_dbg(di->dev, |
| 2179 | "Die temp ok for Main charger thermal protection threshold\n"); |
| 2180 | queue_work(di->charger_wq, &di->check_main_thermal_prot_work); |
| 2181 | |
| 2182 | return IRQ_HANDLED; |
| 2183 | } |
| 2184 | |
| 2185 | /** |
| 2186 | * ab8500_charger_vbusdetf_handler() - VBUS falling detected |
| 2187 | * @irq: interrupt number |
| 2188 | * @_di: pointer to the ab8500_charger structure |
| 2189 | * |
| 2190 | * Returns IRQ status(IRQ_HANDLED) |
| 2191 | */ |
| 2192 | static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di) |
| 2193 | { |
| 2194 | struct ab8500_charger *di = _di; |
| 2195 | |
| 2196 | dev_dbg(di->dev, "VBUS falling detected\n"); |
| 2197 | queue_work(di->charger_wq, &di->detect_usb_type_work); |
| 2198 | |
| 2199 | return IRQ_HANDLED; |
| 2200 | } |
| 2201 | |
| 2202 | /** |
| 2203 | * ab8500_charger_vbusdetr_handler() - VBUS rising detected |
| 2204 | * @irq: interrupt number |
| 2205 | * @_di: pointer to the ab8500_charger structure |
| 2206 | * |
| 2207 | * Returns IRQ status(IRQ_HANDLED) |
| 2208 | */ |
| 2209 | static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di) |
| 2210 | { |
| 2211 | struct ab8500_charger *di = _di; |
| 2212 | |
| 2213 | di->vbus_detected = true; |
| 2214 | dev_dbg(di->dev, "VBUS rising detected\n"); |
| 2215 | queue_work(di->charger_wq, &di->detect_usb_type_work); |
| 2216 | |
| 2217 | return IRQ_HANDLED; |
| 2218 | } |
| 2219 | |
| 2220 | /** |
| 2221 | * ab8500_charger_usblinkstatus_handler() - USB link status has changed |
| 2222 | * @irq: interrupt number |
| 2223 | * @_di: pointer to the ab8500_charger structure |
| 2224 | * |
| 2225 | * Returns IRQ status(IRQ_HANDLED) |
| 2226 | */ |
| 2227 | static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di) |
| 2228 | { |
| 2229 | struct ab8500_charger *di = _di; |
| 2230 | |
| 2231 | dev_dbg(di->dev, "USB link status changed\n"); |
| 2232 | |
| 2233 | queue_work(di->charger_wq, &di->usb_link_status_work); |
| 2234 | |
| 2235 | return IRQ_HANDLED; |
| 2236 | } |
| 2237 | |
| 2238 | /** |
| 2239 | * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger |
| 2240 | * thermal protection threshold |
| 2241 | * @irq: interrupt number |
| 2242 | * @_di: pointer to the ab8500_charger structure |
| 2243 | * |
| 2244 | * Returns IRQ status(IRQ_HANDLED) |
| 2245 | */ |
| 2246 | static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di) |
| 2247 | { |
| 2248 | struct ab8500_charger *di = _di; |
| 2249 | |
| 2250 | dev_dbg(di->dev, |
| 2251 | "Die temp above USB charger thermal protection threshold\n"); |
| 2252 | queue_work(di->charger_wq, &di->check_usb_thermal_prot_work); |
| 2253 | |
| 2254 | return IRQ_HANDLED; |
| 2255 | } |
| 2256 | |
| 2257 | /** |
| 2258 | * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger |
| 2259 | * thermal protection threshold |
| 2260 | * @irq: interrupt number |
| 2261 | * @_di: pointer to the ab8500_charger structure |
| 2262 | * |
| 2263 | * Returns IRQ status(IRQ_HANDLED) |
| 2264 | */ |
| 2265 | static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di) |
| 2266 | { |
| 2267 | struct ab8500_charger *di = _di; |
| 2268 | |
| 2269 | dev_dbg(di->dev, |
| 2270 | "Die temp ok for USB charger thermal protection threshold\n"); |
| 2271 | queue_work(di->charger_wq, &di->check_usb_thermal_prot_work); |
| 2272 | |
| 2273 | return IRQ_HANDLED; |
| 2274 | } |
| 2275 | |
| 2276 | /** |
| 2277 | * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected |
| 2278 | * @irq: interrupt number |
| 2279 | * @_di: pointer to the ab8500_charger structure |
| 2280 | * |
| 2281 | * Returns IRQ status(IRQ_HANDLED) |
| 2282 | */ |
| 2283 | static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di) |
| 2284 | { |
| 2285 | struct ab8500_charger *di = _di; |
| 2286 | |
| 2287 | dev_dbg(di->dev, "Not allowed USB charger detected\n"); |
| 2288 | queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0); |
| 2289 | |
| 2290 | return IRQ_HANDLED; |
| 2291 | } |
| 2292 | |
| 2293 | /** |
| 2294 | * ab8500_charger_chwdexp_handler() - Charger watchdog expired |
| 2295 | * @irq: interrupt number |
| 2296 | * @_di: pointer to the ab8500_charger structure |
| 2297 | * |
| 2298 | * Returns IRQ status(IRQ_HANDLED) |
| 2299 | */ |
| 2300 | static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di) |
| 2301 | { |
| 2302 | struct ab8500_charger *di = _di; |
| 2303 | |
| 2304 | dev_dbg(di->dev, "Charger watchdog expired\n"); |
| 2305 | |
| 2306 | /* |
| 2307 | * The charger that was online when the watchdog expired |
| 2308 | * needs to be restarted for charging to start again |
| 2309 | */ |
| 2310 | if (di->ac.charger_online) { |
| 2311 | di->ac.wd_expired = true; |
| 2312 | ab8500_power_supply_changed(di, &di->ac_chg.psy); |
| 2313 | } |
| 2314 | if (di->usb.charger_online) { |
| 2315 | di->usb.wd_expired = true; |
| 2316 | ab8500_power_supply_changed(di, &di->usb_chg.psy); |
| 2317 | } |
| 2318 | |
| 2319 | return IRQ_HANDLED; |
| 2320 | } |
| 2321 | |
| 2322 | /** |
| 2323 | * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected |
| 2324 | * @irq: interrupt number |
| 2325 | * @_di: pointer to the ab8500_charger structure |
| 2326 | * |
| 2327 | * Returns IRQ status(IRQ_HANDLED) |
| 2328 | */ |
| 2329 | static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di) |
| 2330 | { |
| 2331 | struct ab8500_charger *di = _di; |
| 2332 | |
| 2333 | dev_dbg(di->dev, "VBUS overvoltage detected\n"); |
| 2334 | di->flags.vbus_ovv = true; |
| 2335 | ab8500_power_supply_changed(di, &di->usb_chg.psy); |
| 2336 | |
| 2337 | /* Schedule a new HW failure check */ |
| 2338 | queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0); |
| 2339 | |
| 2340 | return IRQ_HANDLED; |
| 2341 | } |
| 2342 | |
| 2343 | /** |
| 2344 | * ab8500_charger_ac_get_property() - get the ac/mains properties |
| 2345 | * @psy: pointer to the power_supply structure |
| 2346 | * @psp: pointer to the power_supply_property structure |
| 2347 | * @val: pointer to the power_supply_propval union |
| 2348 | * |
| 2349 | * This function gets called when an application tries to get the ac/mains |
| 2350 | * properties by reading the sysfs files. |
| 2351 | * AC/Mains properties are online, present and voltage. |
| 2352 | * online: ac/mains charging is in progress or not |
| 2353 | * present: presence of the ac/mains |
| 2354 | * voltage: AC/Mains voltage |
| 2355 | * Returns error code in case of failure else 0(on success) |
| 2356 | */ |
| 2357 | static int ab8500_charger_ac_get_property(struct power_supply *psy, |
| 2358 | enum power_supply_property psp, |
| 2359 | union power_supply_propval *val) |
| 2360 | { |
| 2361 | struct ab8500_charger *di; |
Jonas Aaberg | a864c5a | 2013-01-11 13:12:53 +0000 | [diff] [blame^] | 2362 | int ret; |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2363 | |
| 2364 | di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy)); |
| 2365 | |
| 2366 | switch (psp) { |
| 2367 | case POWER_SUPPLY_PROP_HEALTH: |
| 2368 | if (di->flags.mainextchnotok) |
| 2369 | val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; |
| 2370 | else if (di->ac.wd_expired || di->usb.wd_expired) |
| 2371 | val->intval = POWER_SUPPLY_HEALTH_DEAD; |
| 2372 | else if (di->flags.main_thermal_prot) |
| 2373 | val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; |
| 2374 | else |
| 2375 | val->intval = POWER_SUPPLY_HEALTH_GOOD; |
| 2376 | break; |
| 2377 | case POWER_SUPPLY_PROP_ONLINE: |
| 2378 | val->intval = di->ac.charger_online; |
| 2379 | break; |
| 2380 | case POWER_SUPPLY_PROP_PRESENT: |
| 2381 | val->intval = di->ac.charger_connected; |
| 2382 | break; |
| 2383 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
Jonas Aaberg | a864c5a | 2013-01-11 13:12:53 +0000 | [diff] [blame^] | 2384 | ret = ab8500_charger_get_ac_voltage(di); |
| 2385 | if (ret >= 0) |
| 2386 | di->ac.charger_voltage = ret; |
| 2387 | /* On error, use previous value */ |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2388 | val->intval = di->ac.charger_voltage * 1000; |
| 2389 | break; |
| 2390 | case POWER_SUPPLY_PROP_VOLTAGE_AVG: |
| 2391 | /* |
| 2392 | * This property is used to indicate when CV mode is entered |
| 2393 | * for the AC charger |
| 2394 | */ |
| 2395 | di->ac.cv_active = ab8500_charger_ac_cv(di); |
| 2396 | val->intval = di->ac.cv_active; |
| 2397 | break; |
| 2398 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
Jonas Aaberg | a864c5a | 2013-01-11 13:12:53 +0000 | [diff] [blame^] | 2399 | ret = ab8500_charger_get_ac_current(di); |
| 2400 | if (ret >= 0) |
| 2401 | di->ac.charger_current = ret; |
| 2402 | val->intval = di->ac.charger_current * 1000; |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2403 | break; |
| 2404 | default: |
| 2405 | return -EINVAL; |
| 2406 | } |
| 2407 | return 0; |
| 2408 | } |
| 2409 | |
| 2410 | /** |
| 2411 | * ab8500_charger_usb_get_property() - get the usb properties |
| 2412 | * @psy: pointer to the power_supply structure |
| 2413 | * @psp: pointer to the power_supply_property structure |
| 2414 | * @val: pointer to the power_supply_propval union |
| 2415 | * |
| 2416 | * This function gets called when an application tries to get the usb |
| 2417 | * properties by reading the sysfs files. |
| 2418 | * USB properties are online, present and voltage. |
| 2419 | * online: usb charging is in progress or not |
| 2420 | * present: presence of the usb |
| 2421 | * voltage: vbus voltage |
| 2422 | * Returns error code in case of failure else 0(on success) |
| 2423 | */ |
| 2424 | static int ab8500_charger_usb_get_property(struct power_supply *psy, |
| 2425 | enum power_supply_property psp, |
| 2426 | union power_supply_propval *val) |
| 2427 | { |
| 2428 | struct ab8500_charger *di; |
Jonas Aaberg | a864c5a | 2013-01-11 13:12:53 +0000 | [diff] [blame^] | 2429 | int ret; |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2430 | |
| 2431 | di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy)); |
| 2432 | |
| 2433 | switch (psp) { |
| 2434 | case POWER_SUPPLY_PROP_HEALTH: |
| 2435 | if (di->flags.usbchargernotok) |
| 2436 | val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; |
| 2437 | else if (di->ac.wd_expired || di->usb.wd_expired) |
| 2438 | val->intval = POWER_SUPPLY_HEALTH_DEAD; |
| 2439 | else if (di->flags.usb_thermal_prot) |
| 2440 | val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; |
| 2441 | else if (di->flags.vbus_ovv) |
| 2442 | val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE; |
| 2443 | else |
| 2444 | val->intval = POWER_SUPPLY_HEALTH_GOOD; |
| 2445 | break; |
| 2446 | case POWER_SUPPLY_PROP_ONLINE: |
| 2447 | val->intval = di->usb.charger_online; |
| 2448 | break; |
| 2449 | case POWER_SUPPLY_PROP_PRESENT: |
| 2450 | val->intval = di->usb.charger_connected; |
| 2451 | break; |
| 2452 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
Jonas Aaberg | a864c5a | 2013-01-11 13:12:53 +0000 | [diff] [blame^] | 2453 | ret = ab8500_charger_get_vbus_voltage(di); |
| 2454 | if (ret >= 0) |
| 2455 | di->usb.charger_voltage = ret; |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2456 | val->intval = di->usb.charger_voltage * 1000; |
| 2457 | break; |
| 2458 | case POWER_SUPPLY_PROP_VOLTAGE_AVG: |
| 2459 | /* |
| 2460 | * This property is used to indicate when CV mode is entered |
| 2461 | * for the USB charger |
| 2462 | */ |
| 2463 | di->usb.cv_active = ab8500_charger_usb_cv(di); |
| 2464 | val->intval = di->usb.cv_active; |
| 2465 | break; |
| 2466 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
Jonas Aaberg | a864c5a | 2013-01-11 13:12:53 +0000 | [diff] [blame^] | 2467 | ret = ab8500_charger_get_usb_current(di); |
| 2468 | if (ret >= 0) |
| 2469 | di->usb.charger_current = ret; |
| 2470 | val->intval = di->usb.charger_current * 1000; |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2471 | break; |
| 2472 | case POWER_SUPPLY_PROP_CURRENT_AVG: |
| 2473 | /* |
| 2474 | * This property is used to indicate when VBUS has collapsed |
| 2475 | * due to too high output current from the USB charger |
| 2476 | */ |
| 2477 | if (di->flags.vbus_collapse) |
| 2478 | val->intval = 1; |
| 2479 | else |
| 2480 | val->intval = 0; |
| 2481 | break; |
| 2482 | default: |
| 2483 | return -EINVAL; |
| 2484 | } |
| 2485 | return 0; |
| 2486 | } |
| 2487 | |
| 2488 | /** |
| 2489 | * ab8500_charger_init_hw_registers() - Set up charger related registers |
| 2490 | * @di: pointer to the ab8500_charger structure |
| 2491 | * |
| 2492 | * Set up charger OVV, watchdog and maximum voltage registers as well as |
| 2493 | * charging of the backup battery |
| 2494 | */ |
| 2495 | static int ab8500_charger_init_hw_registers(struct ab8500_charger *di) |
| 2496 | { |
| 2497 | int ret = 0; |
| 2498 | |
| 2499 | /* Setup maximum charger current and voltage for ABB cut2.0 */ |
| 2500 | if (!is_ab8500_1p1_or_earlier(di->parent)) { |
| 2501 | ret = abx500_set_register_interruptible(di->dev, |
| 2502 | AB8500_CHARGER, |
| 2503 | AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6); |
| 2504 | if (ret) { |
| 2505 | dev_err(di->dev, |
| 2506 | "failed to set CH_VOLT_LVL_MAX_REG\n"); |
| 2507 | goto out; |
| 2508 | } |
| 2509 | |
| 2510 | ret = abx500_set_register_interruptible(di->dev, |
| 2511 | AB8500_CHARGER, |
| 2512 | AB8500_CH_OPT_CRNTLVL_MAX_REG, CH_OP_CUR_LVL_1P6); |
| 2513 | if (ret) { |
| 2514 | dev_err(di->dev, |
| 2515 | "failed to set CH_OPT_CRNTLVL_MAX_REG\n"); |
| 2516 | goto out; |
| 2517 | } |
| 2518 | } |
| 2519 | |
| 2520 | /* VBUS OVV set to 6.3V and enable automatic current limitiation */ |
| 2521 | ret = abx500_set_register_interruptible(di->dev, |
| 2522 | AB8500_CHARGER, |
| 2523 | AB8500_USBCH_CTRL2_REG, |
| 2524 | VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA); |
| 2525 | if (ret) { |
| 2526 | dev_err(di->dev, "failed to set VBUS OVV\n"); |
| 2527 | goto out; |
| 2528 | } |
| 2529 | |
| 2530 | /* Enable main watchdog in OTP */ |
| 2531 | ret = abx500_set_register_interruptible(di->dev, |
| 2532 | AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD); |
| 2533 | if (ret) { |
| 2534 | dev_err(di->dev, "failed to enable main WD in OTP\n"); |
| 2535 | goto out; |
| 2536 | } |
| 2537 | |
| 2538 | /* Enable main watchdog */ |
| 2539 | ret = abx500_set_register_interruptible(di->dev, |
| 2540 | AB8500_SYS_CTRL2_BLOCK, |
| 2541 | AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA); |
| 2542 | if (ret) { |
| 2543 | dev_err(di->dev, "faile to enable main watchdog\n"); |
| 2544 | goto out; |
| 2545 | } |
| 2546 | |
| 2547 | /* |
| 2548 | * Due to internal synchronisation, Enable and Kick watchdog bits |
| 2549 | * cannot be enabled in a single write. |
| 2550 | * A minimum delay of 2*32 kHz period (62.5µs) must be inserted |
| 2551 | * between writing Enable then Kick bits. |
| 2552 | */ |
| 2553 | udelay(63); |
| 2554 | |
| 2555 | /* Kick main watchdog */ |
| 2556 | ret = abx500_set_register_interruptible(di->dev, |
| 2557 | AB8500_SYS_CTRL2_BLOCK, |
| 2558 | AB8500_MAIN_WDOG_CTRL_REG, |
| 2559 | (MAIN_WDOG_ENA | MAIN_WDOG_KICK)); |
| 2560 | if (ret) { |
| 2561 | dev_err(di->dev, "failed to kick main watchdog\n"); |
| 2562 | goto out; |
| 2563 | } |
| 2564 | |
| 2565 | /* Disable main watchdog */ |
| 2566 | ret = abx500_set_register_interruptible(di->dev, |
| 2567 | AB8500_SYS_CTRL2_BLOCK, |
| 2568 | AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS); |
| 2569 | if (ret) { |
| 2570 | dev_err(di->dev, "failed to disable main watchdog\n"); |
| 2571 | goto out; |
| 2572 | } |
| 2573 | |
| 2574 | /* Set watchdog timeout */ |
| 2575 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, |
| 2576 | AB8500_CH_WD_TIMER_REG, WD_TIMER); |
| 2577 | if (ret) { |
| 2578 | dev_err(di->dev, "failed to set charger watchdog timeout\n"); |
| 2579 | goto out; |
| 2580 | } |
| 2581 | |
| 2582 | /* Backup battery voltage and current */ |
| 2583 | ret = abx500_set_register_interruptible(di->dev, |
| 2584 | AB8500_RTC, |
| 2585 | AB8500_RTC_BACKUP_CHG_REG, |
Lee Jones | b0284de | 2012-11-30 10:09:42 +0000 | [diff] [blame] | 2586 | di->bm->bkup_bat_v | |
| 2587 | di->bm->bkup_bat_i); |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2588 | if (ret) { |
| 2589 | dev_err(di->dev, "failed to setup backup battery charging\n"); |
| 2590 | goto out; |
| 2591 | } |
| 2592 | |
| 2593 | /* Enable backup battery charging */ |
| 2594 | abx500_mask_and_set_register_interruptible(di->dev, |
| 2595 | AB8500_RTC, AB8500_RTC_CTRL_REG, |
| 2596 | RTC_BUP_CH_ENA, RTC_BUP_CH_ENA); |
| 2597 | if (ret < 0) |
| 2598 | dev_err(di->dev, "%s mask and set failed\n", __func__); |
| 2599 | |
| 2600 | out: |
| 2601 | return ret; |
| 2602 | } |
| 2603 | |
| 2604 | /* |
| 2605 | * ab8500 charger driver interrupts and their respective isr |
| 2606 | */ |
| 2607 | static struct ab8500_charger_interrupts ab8500_charger_irq[] = { |
| 2608 | {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler}, |
| 2609 | {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler}, |
| 2610 | {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler}, |
| 2611 | {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler}, |
| 2612 | {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler}, |
| 2613 | {"VBUS_DET_F", ab8500_charger_vbusdetf_handler}, |
| 2614 | {"VBUS_DET_R", ab8500_charger_vbusdetr_handler}, |
| 2615 | {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler}, |
| 2616 | {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler}, |
| 2617 | {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler}, |
| 2618 | {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler}, |
| 2619 | {"VBUS_OVV", ab8500_charger_vbusovv_handler}, |
| 2620 | {"CH_WD_EXP", ab8500_charger_chwdexp_handler}, |
| 2621 | }; |
| 2622 | |
| 2623 | static int ab8500_charger_usb_notifier_call(struct notifier_block *nb, |
| 2624 | unsigned long event, void *power) |
| 2625 | { |
| 2626 | struct ab8500_charger *di = |
| 2627 | container_of(nb, struct ab8500_charger, nb); |
| 2628 | enum ab8500_usb_state bm_usb_state; |
| 2629 | unsigned mA = *((unsigned *)power); |
| 2630 | |
| 2631 | if (event != USB_EVENT_VBUS) { |
| 2632 | dev_dbg(di->dev, "not a standard host, returning\n"); |
| 2633 | return NOTIFY_DONE; |
| 2634 | } |
| 2635 | |
| 2636 | /* TODO: State is fabricate here. See if charger really needs USB |
| 2637 | * state or if mA is enough |
| 2638 | */ |
| 2639 | if ((di->usb_state.usb_current == 2) && (mA > 2)) |
| 2640 | bm_usb_state = AB8500_BM_USB_STATE_RESUME; |
| 2641 | else if (mA == 0) |
| 2642 | bm_usb_state = AB8500_BM_USB_STATE_RESET_HS; |
| 2643 | else if (mA == 2) |
| 2644 | bm_usb_state = AB8500_BM_USB_STATE_SUSPEND; |
| 2645 | else if (mA >= 8) /* 8, 100, 500 */ |
| 2646 | bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED; |
| 2647 | else /* Should never occur */ |
| 2648 | bm_usb_state = AB8500_BM_USB_STATE_RESET_FS; |
| 2649 | |
| 2650 | dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n", |
| 2651 | __func__, bm_usb_state, mA); |
| 2652 | |
| 2653 | spin_lock(&di->usb_state.usb_lock); |
| 2654 | di->usb_state.usb_changed = true; |
| 2655 | spin_unlock(&di->usb_state.usb_lock); |
| 2656 | |
| 2657 | di->usb_state.state = bm_usb_state; |
| 2658 | di->usb_state.usb_current = mA; |
| 2659 | |
| 2660 | queue_work(di->charger_wq, &di->usb_state_changed_work); |
| 2661 | |
| 2662 | return NOTIFY_OK; |
| 2663 | } |
| 2664 | |
| 2665 | #if defined(CONFIG_PM) |
| 2666 | static int ab8500_charger_resume(struct platform_device *pdev) |
| 2667 | { |
| 2668 | int ret; |
| 2669 | struct ab8500_charger *di = platform_get_drvdata(pdev); |
| 2670 | |
| 2671 | /* |
| 2672 | * For ABB revision 1.0 and 1.1 there is a bug in the watchdog |
| 2673 | * logic. That means we have to continously kick the charger |
| 2674 | * watchdog even when no charger is connected. This is only |
| 2675 | * valid once the AC charger has been enabled. This is |
| 2676 | * a bug that is not handled by the algorithm and the |
| 2677 | * watchdog have to be kicked by the charger driver |
| 2678 | * when the AC charger is disabled |
| 2679 | */ |
| 2680 | if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) { |
| 2681 | ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, |
| 2682 | AB8500_CHARG_WD_CTRL, CHARG_WD_KICK); |
| 2683 | if (ret) |
| 2684 | dev_err(di->dev, "Failed to kick WD!\n"); |
| 2685 | |
| 2686 | /* If not already pending start a new timer */ |
| 2687 | if (!delayed_work_pending( |
| 2688 | &di->kick_wd_work)) { |
| 2689 | queue_delayed_work(di->charger_wq, &di->kick_wd_work, |
| 2690 | round_jiffies(WD_KICK_INTERVAL)); |
| 2691 | } |
| 2692 | } |
| 2693 | |
| 2694 | /* If we still have a HW failure, schedule a new check */ |
| 2695 | if (di->flags.mainextchnotok || di->flags.vbus_ovv) { |
| 2696 | queue_delayed_work(di->charger_wq, |
| 2697 | &di->check_hw_failure_work, 0); |
| 2698 | } |
| 2699 | |
| 2700 | return 0; |
| 2701 | } |
| 2702 | |
| 2703 | static int ab8500_charger_suspend(struct platform_device *pdev, |
| 2704 | pm_message_t state) |
| 2705 | { |
| 2706 | struct ab8500_charger *di = platform_get_drvdata(pdev); |
| 2707 | |
| 2708 | /* Cancel any pending HW failure check */ |
| 2709 | if (delayed_work_pending(&di->check_hw_failure_work)) |
| 2710 | cancel_delayed_work(&di->check_hw_failure_work); |
| 2711 | |
| 2712 | return 0; |
| 2713 | } |
| 2714 | #else |
| 2715 | #define ab8500_charger_suspend NULL |
| 2716 | #define ab8500_charger_resume NULL |
| 2717 | #endif |
| 2718 | |
Bill Pemberton | 415ec69 | 2012-11-19 13:26:07 -0500 | [diff] [blame] | 2719 | static int ab8500_charger_remove(struct platform_device *pdev) |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2720 | { |
| 2721 | struct ab8500_charger *di = platform_get_drvdata(pdev); |
| 2722 | int i, irq, ret; |
| 2723 | |
| 2724 | /* Disable AC charging */ |
| 2725 | ab8500_charger_ac_en(&di->ac_chg, false, 0, 0); |
| 2726 | |
| 2727 | /* Disable USB charging */ |
| 2728 | ab8500_charger_usb_en(&di->usb_chg, false, 0, 0); |
| 2729 | |
| 2730 | /* Disable interrupts */ |
| 2731 | for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) { |
| 2732 | irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name); |
| 2733 | free_irq(irq, di); |
| 2734 | } |
| 2735 | |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2736 | /* Backup battery voltage and current disable */ |
| 2737 | ret = abx500_mask_and_set_register_interruptible(di->dev, |
| 2738 | AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0); |
| 2739 | if (ret < 0) |
| 2740 | dev_err(di->dev, "%s mask and set failed\n", __func__); |
| 2741 | |
Anton Vorontsov | efd71c8 | 2012-03-14 04:22:17 +0400 | [diff] [blame] | 2742 | usb_unregister_notifier(di->usb_phy, &di->nb); |
Kishon Vijay Abraham I | 721002e | 2012-06-22 17:02:45 +0530 | [diff] [blame] | 2743 | usb_put_phy(di->usb_phy); |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2744 | |
| 2745 | /* Delete the work queue */ |
| 2746 | destroy_workqueue(di->charger_wq); |
| 2747 | |
| 2748 | flush_scheduled_work(); |
| 2749 | power_supply_unregister(&di->usb_chg.psy); |
| 2750 | power_supply_unregister(&di->ac_chg.psy); |
| 2751 | platform_set_drvdata(pdev, NULL); |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2752 | |
| 2753 | return 0; |
| 2754 | } |
| 2755 | |
Rajanikanth H.V | 4aef72d | 2012-11-18 19:17:47 -0800 | [diff] [blame] | 2756 | static char *supply_interface[] = { |
| 2757 | "ab8500_chargalg", |
| 2758 | "ab8500_fg", |
| 2759 | "ab8500_btemp", |
| 2760 | }; |
| 2761 | |
Bill Pemberton | c8afa64 | 2012-11-19 13:22:23 -0500 | [diff] [blame] | 2762 | static int ab8500_charger_probe(struct platform_device *pdev) |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2763 | { |
Rajanikanth H.V | 4aef72d | 2012-11-18 19:17:47 -0800 | [diff] [blame] | 2764 | struct device_node *np = pdev->dev.of_node; |
Lee Jones | 7722b79 | 2012-11-30 10:56:28 +0000 | [diff] [blame] | 2765 | struct abx500_bm_data *plat = pdev->dev.platform_data; |
Lee Jones | 2aac3de | 2012-05-05 04:38:19 -0700 | [diff] [blame] | 2766 | struct ab8500_charger *di; |
Lee Jones | b269fff | 2013-01-11 13:12:51 +0000 | [diff] [blame] | 2767 | int irq, i, charger_status, ret = 0, ch_stat; |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2768 | |
Rajanikanth H.V | 4aef72d | 2012-11-18 19:17:47 -0800 | [diff] [blame] | 2769 | di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL); |
| 2770 | if (!di) { |
| 2771 | dev_err(&pdev->dev, "%s no mem for ab8500_charger\n", __func__); |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2772 | return -ENOMEM; |
Rajanikanth H.V | 4aef72d | 2012-11-18 19:17:47 -0800 | [diff] [blame] | 2773 | } |
Lee Jones | 7722b79 | 2012-11-30 10:56:28 +0000 | [diff] [blame] | 2774 | |
| 2775 | if (!plat) { |
| 2776 | dev_err(&pdev->dev, "no battery management data supplied\n"); |
| 2777 | return -EINVAL; |
Rajanikanth H.V | 4aef72d | 2012-11-18 19:17:47 -0800 | [diff] [blame] | 2778 | } |
Lee Jones | 7722b79 | 2012-11-30 10:56:28 +0000 | [diff] [blame] | 2779 | di->bm = plat; |
| 2780 | |
| 2781 | if (np) { |
| 2782 | ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm); |
| 2783 | if (ret) { |
| 2784 | dev_err(&pdev->dev, "failed to get battery information\n"); |
| 2785 | return ret; |
| 2786 | } |
| 2787 | di->autopower_cfg = of_property_read_bool(np, "autopower_cfg"); |
| 2788 | } else |
| 2789 | di->autopower_cfg = false; |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2790 | |
| 2791 | /* get parent data */ |
| 2792 | di->dev = &pdev->dev; |
| 2793 | di->parent = dev_get_drvdata(pdev->dev.parent); |
| 2794 | di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); |
| 2795 | |
| 2796 | /* initialize lock */ |
| 2797 | spin_lock_init(&di->usb_state.usb_lock); |
| 2798 | |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2799 | di->autopower = false; |
| 2800 | |
| 2801 | /* AC supply */ |
| 2802 | /* power_supply base class */ |
| 2803 | di->ac_chg.psy.name = "ab8500_ac"; |
| 2804 | di->ac_chg.psy.type = POWER_SUPPLY_TYPE_MAINS; |
| 2805 | di->ac_chg.psy.properties = ab8500_charger_ac_props; |
| 2806 | di->ac_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_ac_props); |
| 2807 | di->ac_chg.psy.get_property = ab8500_charger_ac_get_property; |
Rajanikanth H.V | 4aef72d | 2012-11-18 19:17:47 -0800 | [diff] [blame] | 2808 | di->ac_chg.psy.supplied_to = supply_interface; |
| 2809 | di->ac_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface), |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2810 | /* ux500_charger sub-class */ |
| 2811 | di->ac_chg.ops.enable = &ab8500_charger_ac_en; |
| 2812 | di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick; |
| 2813 | di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current; |
| 2814 | di->ac_chg.max_out_volt = ab8500_charger_voltage_map[ |
| 2815 | ARRAY_SIZE(ab8500_charger_voltage_map) - 1]; |
| 2816 | di->ac_chg.max_out_curr = ab8500_charger_current_map[ |
| 2817 | ARRAY_SIZE(ab8500_charger_current_map) - 1]; |
| 2818 | |
| 2819 | /* USB supply */ |
| 2820 | /* power_supply base class */ |
| 2821 | di->usb_chg.psy.name = "ab8500_usb"; |
| 2822 | di->usb_chg.psy.type = POWER_SUPPLY_TYPE_USB; |
| 2823 | di->usb_chg.psy.properties = ab8500_charger_usb_props; |
| 2824 | di->usb_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_usb_props); |
| 2825 | di->usb_chg.psy.get_property = ab8500_charger_usb_get_property; |
Rajanikanth H.V | 4aef72d | 2012-11-18 19:17:47 -0800 | [diff] [blame] | 2826 | di->usb_chg.psy.supplied_to = supply_interface; |
| 2827 | di->usb_chg.psy.num_supplicants = ARRAY_SIZE(supply_interface), |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2828 | /* ux500_charger sub-class */ |
| 2829 | di->usb_chg.ops.enable = &ab8500_charger_usb_en; |
| 2830 | di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick; |
| 2831 | di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current; |
| 2832 | di->usb_chg.max_out_volt = ab8500_charger_voltage_map[ |
| 2833 | ARRAY_SIZE(ab8500_charger_voltage_map) - 1]; |
| 2834 | di->usb_chg.max_out_curr = ab8500_charger_current_map[ |
| 2835 | ARRAY_SIZE(ab8500_charger_current_map) - 1]; |
| 2836 | |
| 2837 | |
| 2838 | /* Create a work queue for the charger */ |
| 2839 | di->charger_wq = |
| 2840 | create_singlethread_workqueue("ab8500_charger_wq"); |
| 2841 | if (di->charger_wq == NULL) { |
| 2842 | dev_err(di->dev, "failed to create work queue\n"); |
Rajanikanth H.V | 4aef72d | 2012-11-18 19:17:47 -0800 | [diff] [blame] | 2843 | return -ENOMEM; |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2844 | } |
| 2845 | |
Lee Jones | b269fff | 2013-01-11 13:12:51 +0000 | [diff] [blame] | 2846 | mutex_init(&di->charger_attached_mutex); |
| 2847 | |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2848 | /* Init work for HW failure check */ |
Tejun Heo | 203b42f | 2012-08-21 13:18:23 -0700 | [diff] [blame] | 2849 | INIT_DEFERRABLE_WORK(&di->check_hw_failure_work, |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2850 | ab8500_charger_check_hw_failure_work); |
Tejun Heo | 203b42f | 2012-08-21 13:18:23 -0700 | [diff] [blame] | 2851 | INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work, |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2852 | ab8500_charger_check_usbchargernotok_work); |
| 2853 | |
Lee Jones | b269fff | 2013-01-11 13:12:51 +0000 | [diff] [blame] | 2854 | INIT_DELAYED_WORK(&di->ac_charger_attached_work, |
| 2855 | ab8500_charger_ac_attached_work); |
| 2856 | INIT_DELAYED_WORK(&di->usb_charger_attached_work, |
| 2857 | ab8500_charger_usb_attached_work); |
| 2858 | |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2859 | /* |
| 2860 | * For ABB revision 1.0 and 1.1 there is a bug in the watchdog |
| 2861 | * logic. That means we have to continously kick the charger |
| 2862 | * watchdog even when no charger is connected. This is only |
| 2863 | * valid once the AC charger has been enabled. This is |
| 2864 | * a bug that is not handled by the algorithm and the |
| 2865 | * watchdog have to be kicked by the charger driver |
| 2866 | * when the AC charger is disabled |
| 2867 | */ |
Tejun Heo | 203b42f | 2012-08-21 13:18:23 -0700 | [diff] [blame] | 2868 | INIT_DEFERRABLE_WORK(&di->kick_wd_work, |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2869 | ab8500_charger_kick_watchdog_work); |
| 2870 | |
Tejun Heo | 203b42f | 2012-08-21 13:18:23 -0700 | [diff] [blame] | 2871 | INIT_DEFERRABLE_WORK(&di->check_vbat_work, |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2872 | ab8500_charger_check_vbat_work); |
| 2873 | |
| 2874 | /* Init work for charger detection */ |
| 2875 | INIT_WORK(&di->usb_link_status_work, |
| 2876 | ab8500_charger_usb_link_status_work); |
| 2877 | INIT_WORK(&di->ac_work, ab8500_charger_ac_work); |
| 2878 | INIT_WORK(&di->detect_usb_type_work, |
| 2879 | ab8500_charger_detect_usb_type_work); |
| 2880 | |
| 2881 | INIT_WORK(&di->usb_state_changed_work, |
| 2882 | ab8500_charger_usb_state_changed_work); |
| 2883 | |
| 2884 | /* Init work for checking HW status */ |
| 2885 | INIT_WORK(&di->check_main_thermal_prot_work, |
| 2886 | ab8500_charger_check_main_thermal_prot_work); |
| 2887 | INIT_WORK(&di->check_usb_thermal_prot_work, |
| 2888 | ab8500_charger_check_usb_thermal_prot_work); |
| 2889 | |
| 2890 | /* |
| 2891 | * VDD ADC supply needs to be enabled from this driver when there |
| 2892 | * is a charger connected to avoid erroneous BTEMP_HIGH/LOW |
| 2893 | * interrupts during charging |
| 2894 | */ |
Sachin Kamat | 8feffd1 | 2012-12-07 17:23:28 +0530 | [diff] [blame] | 2895 | di->regu = devm_regulator_get(di->dev, "vddadc"); |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2896 | if (IS_ERR(di->regu)) { |
| 2897 | ret = PTR_ERR(di->regu); |
| 2898 | dev_err(di->dev, "failed to get vddadc regulator\n"); |
| 2899 | goto free_charger_wq; |
| 2900 | } |
| 2901 | |
| 2902 | |
| 2903 | /* Initialize OVV, and other registers */ |
| 2904 | ret = ab8500_charger_init_hw_registers(di); |
| 2905 | if (ret) { |
| 2906 | dev_err(di->dev, "failed to initialize ABB registers\n"); |
Sachin Kamat | 8feffd1 | 2012-12-07 17:23:28 +0530 | [diff] [blame] | 2907 | goto free_charger_wq; |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2908 | } |
| 2909 | |
| 2910 | /* Register AC charger class */ |
| 2911 | ret = power_supply_register(di->dev, &di->ac_chg.psy); |
| 2912 | if (ret) { |
| 2913 | dev_err(di->dev, "failed to register AC charger\n"); |
Sachin Kamat | 8feffd1 | 2012-12-07 17:23:28 +0530 | [diff] [blame] | 2914 | goto free_charger_wq; |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2915 | } |
| 2916 | |
| 2917 | /* Register USB charger class */ |
| 2918 | ret = power_supply_register(di->dev, &di->usb_chg.psy); |
| 2919 | if (ret) { |
| 2920 | dev_err(di->dev, "failed to register USB charger\n"); |
| 2921 | goto free_ac; |
| 2922 | } |
| 2923 | |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame] | 2924 | di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2); |
Kishon Vijay Abraham I | ded017e | 2012-06-26 17:40:32 +0530 | [diff] [blame] | 2925 | if (IS_ERR_OR_NULL(di->usb_phy)) { |
Anton Vorontsov | efd71c8 | 2012-03-14 04:22:17 +0400 | [diff] [blame] | 2926 | dev_err(di->dev, "failed to get usb transceiver\n"); |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2927 | ret = -EINVAL; |
| 2928 | goto free_usb; |
| 2929 | } |
| 2930 | di->nb.notifier_call = ab8500_charger_usb_notifier_call; |
Anton Vorontsov | efd71c8 | 2012-03-14 04:22:17 +0400 | [diff] [blame] | 2931 | ret = usb_register_notifier(di->usb_phy, &di->nb); |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2932 | if (ret) { |
Anton Vorontsov | efd71c8 | 2012-03-14 04:22:17 +0400 | [diff] [blame] | 2933 | dev_err(di->dev, "failed to register usb notifier\n"); |
| 2934 | goto put_usb_phy; |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2935 | } |
| 2936 | |
| 2937 | /* Identify the connected charger types during startup */ |
| 2938 | charger_status = ab8500_charger_detect_chargers(di); |
| 2939 | if (charger_status & AC_PW_CONN) { |
| 2940 | di->ac.charger_connected = 1; |
| 2941 | di->ac_conn = true; |
| 2942 | ab8500_power_supply_changed(di, &di->ac_chg.psy); |
| 2943 | sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present"); |
| 2944 | } |
| 2945 | |
| 2946 | if (charger_status & USB_PW_CONN) { |
| 2947 | dev_dbg(di->dev, "VBUS Detect during startup\n"); |
| 2948 | di->vbus_detected = true; |
| 2949 | di->vbus_detected_start = true; |
| 2950 | queue_work(di->charger_wq, |
| 2951 | &di->detect_usb_type_work); |
| 2952 | } |
| 2953 | |
| 2954 | /* Register interrupts */ |
| 2955 | for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) { |
| 2956 | irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name); |
| 2957 | ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr, |
| 2958 | IRQF_SHARED | IRQF_NO_SUSPEND, |
| 2959 | ab8500_charger_irq[i].name, di); |
| 2960 | |
| 2961 | if (ret != 0) { |
| 2962 | dev_err(di->dev, "failed to request %s IRQ %d: %d\n" |
| 2963 | , ab8500_charger_irq[i].name, irq, ret); |
| 2964 | goto free_irq; |
| 2965 | } |
| 2966 | dev_dbg(di->dev, "Requested %s IRQ %d: %d\n", |
| 2967 | ab8500_charger_irq[i].name, irq, ret); |
| 2968 | } |
| 2969 | |
| 2970 | platform_set_drvdata(pdev, di); |
| 2971 | |
Lee Jones | b269fff | 2013-01-11 13:12:51 +0000 | [diff] [blame] | 2972 | mutex_lock(&di->charger_attached_mutex); |
| 2973 | |
| 2974 | ch_stat = ab8500_charger_detect_chargers(di); |
| 2975 | |
| 2976 | if ((ch_stat & AC_PW_CONN) == AC_PW_CONN) { |
| 2977 | queue_delayed_work(di->charger_wq, |
| 2978 | &di->ac_charger_attached_work, |
| 2979 | HZ); |
| 2980 | } |
| 2981 | if ((ch_stat & USB_PW_CONN) == USB_PW_CONN) { |
| 2982 | queue_delayed_work(di->charger_wq, |
| 2983 | &di->usb_charger_attached_work, |
| 2984 | HZ); |
| 2985 | } |
| 2986 | |
| 2987 | mutex_unlock(&di->charger_attached_mutex); |
| 2988 | |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2989 | return ret; |
| 2990 | |
| 2991 | free_irq: |
Anton Vorontsov | efd71c8 | 2012-03-14 04:22:17 +0400 | [diff] [blame] | 2992 | usb_unregister_notifier(di->usb_phy, &di->nb); |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 2993 | |
| 2994 | /* We also have to free all successfully registered irqs */ |
| 2995 | for (i = i - 1; i >= 0; i--) { |
| 2996 | irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name); |
| 2997 | free_irq(irq, di); |
| 2998 | } |
Anton Vorontsov | efd71c8 | 2012-03-14 04:22:17 +0400 | [diff] [blame] | 2999 | put_usb_phy: |
Kishon Vijay Abraham I | 721002e | 2012-06-22 17:02:45 +0530 | [diff] [blame] | 3000 | usb_put_phy(di->usb_phy); |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 3001 | free_usb: |
| 3002 | power_supply_unregister(&di->usb_chg.psy); |
| 3003 | free_ac: |
| 3004 | power_supply_unregister(&di->ac_chg.psy); |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 3005 | free_charger_wq: |
| 3006 | destroy_workqueue(di->charger_wq); |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 3007 | return ret; |
| 3008 | } |
| 3009 | |
Rajanikanth H.V | 4aef72d | 2012-11-18 19:17:47 -0800 | [diff] [blame] | 3010 | static const struct of_device_id ab8500_charger_match[] = { |
| 3011 | { .compatible = "stericsson,ab8500-charger", }, |
| 3012 | { }, |
| 3013 | }; |
| 3014 | |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 3015 | static struct platform_driver ab8500_charger_driver = { |
| 3016 | .probe = ab8500_charger_probe, |
Bill Pemberton | 28ea73f | 2012-11-19 13:20:40 -0500 | [diff] [blame] | 3017 | .remove = ab8500_charger_remove, |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 3018 | .suspend = ab8500_charger_suspend, |
| 3019 | .resume = ab8500_charger_resume, |
| 3020 | .driver = { |
| 3021 | .name = "ab8500-charger", |
| 3022 | .owner = THIS_MODULE, |
Rajanikanth H.V | 4aef72d | 2012-11-18 19:17:47 -0800 | [diff] [blame] | 3023 | .of_match_table = ab8500_charger_match, |
Arun Murthy | 84edbee | 2012-02-29 21:54:26 +0530 | [diff] [blame] | 3024 | }, |
| 3025 | }; |
| 3026 | |
| 3027 | static int __init ab8500_charger_init(void) |
| 3028 | { |
| 3029 | return platform_driver_register(&ab8500_charger_driver); |
| 3030 | } |
| 3031 | |
| 3032 | static void __exit ab8500_charger_exit(void) |
| 3033 | { |
| 3034 | platform_driver_unregister(&ab8500_charger_driver); |
| 3035 | } |
| 3036 | |
| 3037 | subsys_initcall_sync(ab8500_charger_init); |
| 3038 | module_exit(ab8500_charger_exit); |
| 3039 | |
| 3040 | MODULE_LICENSE("GPL v2"); |
| 3041 | MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy"); |
| 3042 | MODULE_ALIAS("platform:ab8500-charger"); |
| 3043 | MODULE_DESCRIPTION("AB8500 charger management driver"); |