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