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