Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 1 | /* |
| 2 | * TWL4030/TPS65950 BCI (Battery Charger Interface) driver |
| 3 | * |
| 4 | * Copyright (C) 2010 Gražvydas Ignotas <notasas@gmail.com> |
| 5 | * |
| 6 | * based on twl4030_bci_battery.c by TI |
| 7 | * Copyright (C) 2008 Texas Instruments, Inc. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/slab.h> |
Kishon Vijay Abraham I | ded017e | 2012-06-26 17:40:32 +0530 | [diff] [blame] | 18 | #include <linux/err.h> |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/i2c/twl.h> |
| 22 | #include <linux/power_supply.h> |
| 23 | #include <linux/notifier.h> |
| 24 | #include <linux/usb/otg.h> |
NeilBrown | e4ae537 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 25 | #include <linux/i2c/twl4030-madc.h> |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 26 | |
NeilBrown | 7f4a633 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 27 | #define TWL4030_BCIMDEN 0x00 |
| 28 | #define TWL4030_BCIMDKEY 0x01 |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 29 | #define TWL4030_BCIMSTATEC 0x02 |
| 30 | #define TWL4030_BCIICHG 0x08 |
| 31 | #define TWL4030_BCIVAC 0x0a |
| 32 | #define TWL4030_BCIVBUS 0x0c |
Nishanth Menon | 61a7784 | 2014-05-28 16:46:49 -0500 | [diff] [blame] | 33 | #define TWL4030_BCIMFSTS3 0x0F |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 34 | #define TWL4030_BCIMFSTS4 0x10 |
| 35 | #define TWL4030_BCICTL1 0x23 |
NeilBrown | 210d4bc | 2012-05-09 07:40:40 +1000 | [diff] [blame] | 36 | #define TWL4030_BB_CFG 0x12 |
NeilBrown | 1098cb5 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 37 | #define TWL4030_BCIIREF1 0x27 |
| 38 | #define TWL4030_BCIIREF2 0x28 |
| 39 | #define TWL4030_BCIMFKEY 0x11 |
NeilBrown | 7f4a633 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 40 | #define TWL4030_BCIMFEN3 0x14 |
NeilBrown | 1098cb5 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 41 | #define TWL4030_BCIMFTH8 0x1d |
| 42 | #define TWL4030_BCIMFTH9 0x1e |
NeilBrown | 7f4a633 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 43 | #define TWL4030_BCIWDKEY 0x21 |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 44 | |
Nishanth Menon | 61a7784 | 2014-05-28 16:46:49 -0500 | [diff] [blame] | 45 | #define TWL4030_BCIMFSTS1 0x01 |
| 46 | |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 47 | #define TWL4030_BCIAUTOWEN BIT(5) |
| 48 | #define TWL4030_CONFIG_DONE BIT(4) |
NeilBrown | 7f4a633 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 49 | #define TWL4030_CVENAC BIT(2) |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 50 | #define TWL4030_BCIAUTOUSB BIT(1) |
| 51 | #define TWL4030_BCIAUTOAC BIT(0) |
| 52 | #define TWL4030_CGAIN BIT(5) |
| 53 | #define TWL4030_USBFASTMCHG BIT(2) |
| 54 | #define TWL4030_STS_VBUS BIT(7) |
| 55 | #define TWL4030_STS_USB_ID BIT(2) |
NeilBrown | 210d4bc | 2012-05-09 07:40:40 +1000 | [diff] [blame] | 56 | #define TWL4030_BBCHEN BIT(4) |
Il Han | 94b05ca | 2012-08-17 16:49:43 +0900 | [diff] [blame] | 57 | #define TWL4030_BBSEL_MASK 0x0c |
| 58 | #define TWL4030_BBSEL_2V5 0x00 |
| 59 | #define TWL4030_BBSEL_3V0 0x04 |
| 60 | #define TWL4030_BBSEL_3V1 0x08 |
| 61 | #define TWL4030_BBSEL_3V2 0x0c |
| 62 | #define TWL4030_BBISEL_MASK 0x03 |
| 63 | #define TWL4030_BBISEL_25uA 0x00 |
| 64 | #define TWL4030_BBISEL_150uA 0x01 |
| 65 | #define TWL4030_BBISEL_500uA 0x02 |
| 66 | #define TWL4030_BBISEL_1000uA 0x03 |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 67 | |
Nishanth Menon | 61a7784 | 2014-05-28 16:46:49 -0500 | [diff] [blame] | 68 | #define TWL4030_BATSTSPCHG BIT(2) |
| 69 | #define TWL4030_BATSTSMCHG BIT(6) |
| 70 | |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 71 | /* BCI interrupts */ |
| 72 | #define TWL4030_WOVF BIT(0) /* Watchdog overflow */ |
| 73 | #define TWL4030_TMOVF BIT(1) /* Timer overflow */ |
| 74 | #define TWL4030_ICHGHIGH BIT(2) /* Battery charge current high */ |
| 75 | #define TWL4030_ICHGLOW BIT(3) /* Battery cc. low / FSM state change */ |
| 76 | #define TWL4030_ICHGEOC BIT(4) /* Battery current end-of-charge */ |
| 77 | #define TWL4030_TBATOR2 BIT(5) /* Battery temperature out of range 2 */ |
| 78 | #define TWL4030_TBATOR1 BIT(6) /* Battery temperature out of range 1 */ |
| 79 | #define TWL4030_BATSTS BIT(7) /* Battery status */ |
| 80 | |
| 81 | #define TWL4030_VBATLVL BIT(0) /* VBAT level */ |
| 82 | #define TWL4030_VBATOV BIT(1) /* VBAT overvoltage */ |
| 83 | #define TWL4030_VBUSOV BIT(2) /* VBUS overvoltage */ |
| 84 | #define TWL4030_ACCHGOV BIT(3) /* Ac charger overvoltage */ |
| 85 | |
| 86 | #define TWL4030_MSTATEC_USB BIT(4) |
| 87 | #define TWL4030_MSTATEC_AC BIT(5) |
| 88 | #define TWL4030_MSTATEC_MASK 0x0f |
| 89 | #define TWL4030_MSTATEC_QUICK1 0x02 |
| 90 | #define TWL4030_MSTATEC_QUICK7 0x07 |
| 91 | #define TWL4030_MSTATEC_COMPLETE1 0x0b |
| 92 | #define TWL4030_MSTATEC_COMPLETE4 0x0e |
| 93 | |
Grazvydas Ignotas | e11fc21 | 2015-09-05 02:32:34 +0300 | [diff] [blame] | 94 | #if IS_REACHABLE(CONFIG_TWL4030_MADC) |
NeilBrown | b49d15d | 2015-08-07 13:45:25 +1000 | [diff] [blame] | 95 | /* |
| 96 | * If AC (Accessory Charger) voltage exceeds 4.5V (MADC 11) |
| 97 | * then AC is available. |
| 98 | */ |
| 99 | static inline int ac_available(void) |
| 100 | { |
| 101 | return twl4030_get_madc_conversion(11) > 4500; |
| 102 | } |
| 103 | #else |
| 104 | static inline int ac_available(void) |
| 105 | { |
| 106 | return 0; |
| 107 | } |
| 108 | #endif |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 109 | static bool allow_usb; |
Grazvydas Ignotas | 28d48f0 | 2011-06-03 14:43:11 +0300 | [diff] [blame] | 110 | module_param(allow_usb, bool, 0644); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 111 | MODULE_PARM_DESC(allow_usb, "Allow USB charge drawing default current"); |
| 112 | |
| 113 | struct twl4030_bci { |
| 114 | struct device *dev; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 115 | struct power_supply *ac; |
| 116 | struct power_supply *usb; |
Heikki Krogerus | 8675381 | 2012-02-13 13:24:02 +0200 | [diff] [blame] | 117 | struct usb_phy *transceiver; |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 118 | struct notifier_block usb_nb; |
Heikki Krogerus | d6ccc44 | 2011-02-25 13:56:36 +0200 | [diff] [blame] | 119 | struct work_struct work; |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 120 | int irq_chg; |
| 121 | int irq_bci; |
NeilBrown | ab37813 | 2012-05-09 07:40:40 +1000 | [diff] [blame] | 122 | int usb_enabled; |
Heikki Krogerus | d6ccc44 | 2011-02-25 13:56:36 +0200 | [diff] [blame] | 123 | |
NeilBrown | 1098cb5 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 124 | /* |
NeilBrown | e4ae537 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 125 | * ichg_* and *_cur values in uA. If any are 'large', we set |
| 126 | * CGAIN to '1' which doubles the range for half the |
| 127 | * precision. |
NeilBrown | 1098cb5 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 128 | */ |
NeilBrown | e4ae537 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 129 | unsigned int ichg_eoc, ichg_lo, ichg_hi; |
| 130 | unsigned int usb_cur, ac_cur; |
| 131 | bool ac_is_active; |
NeilBrown | b04b908 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 132 | int usb_mode, ac_mode; /* charging mode requested */ |
NeilBrown | 22d4c33 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 133 | #define CHARGE_OFF 0 |
| 134 | #define CHARGE_AUTO 1 |
NeilBrown | 7f4a633 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 135 | #define CHARGE_LINEAR 2 |
NeilBrown | 1098cb5 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 136 | |
NeilBrown | 21ae404 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 137 | /* When setting the USB current we slowly increase the |
| 138 | * requested current until target is reached or the voltage |
| 139 | * drops below 4.75V. In the latter case we step back one |
| 140 | * step. |
| 141 | */ |
| 142 | unsigned int usb_cur_target; |
| 143 | struct delayed_work current_worker; |
| 144 | #define USB_CUR_STEP 20000 /* 20mA at a time */ |
| 145 | #define USB_MIN_VOLT 4750000 /* 4.75V */ |
| 146 | #define USB_CUR_DELAY msecs_to_jiffies(100) |
| 147 | #define USB_MAX_CURRENT 1700000 /* TWL4030 caps at 1.7A */ |
| 148 | |
Heikki Krogerus | d6ccc44 | 2011-02-25 13:56:36 +0200 | [diff] [blame] | 149 | unsigned long event; |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 150 | }; |
| 151 | |
NeilBrown | 22d4c33 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 152 | /* strings for 'usb_mode' values */ |
NeilBrown | 7f4a633 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 153 | static char *modes[] = { "off", "auto", "continuous" }; |
NeilBrown | 22d4c33 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 154 | |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 155 | /* |
| 156 | * clear and set bits on an given register on a given module |
| 157 | */ |
| 158 | static int twl4030_clear_set(u8 mod_no, u8 clear, u8 set, u8 reg) |
| 159 | { |
| 160 | u8 val = 0; |
| 161 | int ret; |
| 162 | |
| 163 | ret = twl_i2c_read_u8(mod_no, &val, reg); |
| 164 | if (ret) |
| 165 | return ret; |
| 166 | |
| 167 | val &= ~clear; |
| 168 | val |= set; |
| 169 | |
| 170 | return twl_i2c_write_u8(mod_no, val, reg); |
| 171 | } |
| 172 | |
| 173 | static int twl4030_bci_read(u8 reg, u8 *val) |
| 174 | { |
Peter Ujfalusi | e9f14c1 | 2012-11-13 10:42:11 +0100 | [diff] [blame] | 175 | return twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, val, reg); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | static int twl4030_clear_set_boot_bci(u8 clear, u8 set) |
| 179 | { |
Peter Ujfalusi | e9f14c1 | 2012-11-13 10:42:11 +0100 | [diff] [blame] | 180 | return twl4030_clear_set(TWL_MODULE_PM_MASTER, clear, |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 181 | TWL4030_CONFIG_DONE | TWL4030_BCIAUTOWEN | set, |
| 182 | TWL4030_PM_MASTER_BOOT_BCI); |
| 183 | } |
| 184 | |
| 185 | static int twl4030bci_read_adc_val(u8 reg) |
| 186 | { |
| 187 | int ret, temp; |
| 188 | u8 val; |
| 189 | |
| 190 | /* read MSB */ |
| 191 | ret = twl4030_bci_read(reg + 1, &val); |
| 192 | if (ret) |
| 193 | return ret; |
| 194 | |
| 195 | temp = (int)(val & 0x03) << 8; |
| 196 | |
| 197 | /* read LSB */ |
| 198 | ret = twl4030_bci_read(reg, &val); |
| 199 | if (ret) |
| 200 | return ret; |
| 201 | |
| 202 | return temp | val; |
| 203 | } |
| 204 | |
| 205 | /* |
Nishanth Menon | 61a7784 | 2014-05-28 16:46:49 -0500 | [diff] [blame] | 206 | * Check if Battery Pack was present |
| 207 | */ |
| 208 | static int twl4030_is_battery_present(struct twl4030_bci *bci) |
| 209 | { |
| 210 | int ret; |
| 211 | u8 val = 0; |
| 212 | |
| 213 | /* Battery presence in Main charge? */ |
| 214 | ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val, TWL4030_BCIMFSTS3); |
| 215 | if (ret) |
| 216 | return ret; |
| 217 | if (val & TWL4030_BATSTSMCHG) |
| 218 | return 0; |
| 219 | |
| 220 | /* |
| 221 | * OK, It could be that bootloader did not enable main charger, |
| 222 | * pre-charge is h/w auto. So, Battery presence in Pre-charge? |
| 223 | */ |
| 224 | ret = twl_i2c_read_u8(TWL4030_MODULE_PRECHARGE, &val, |
| 225 | TWL4030_BCIMFSTS1); |
| 226 | if (ret) |
| 227 | return ret; |
| 228 | if (val & TWL4030_BATSTSPCHG) |
| 229 | return 0; |
| 230 | |
| 231 | return -ENODEV; |
| 232 | } |
| 233 | |
| 234 | /* |
NeilBrown | 3b542f0 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 235 | * TI provided formulas: |
| 236 | * CGAIN == 0: ICHG = (BCIICHG * 1.7) / (2^10 - 1) - 0.85 |
| 237 | * CGAIN == 1: ICHG = (BCIICHG * 3.4) / (2^10 - 1) - 1.7 |
| 238 | * Here we use integer approximation of: |
| 239 | * CGAIN == 0: val * 1.6618 - 0.85 * 1000 |
| 240 | * CGAIN == 1: (val * 1.6618 - 0.85 * 1000) * 2 |
| 241 | */ |
| 242 | /* |
| 243 | * convert twl register value for currents into uA |
| 244 | */ |
| 245 | static int regval2ua(int regval, bool cgain) |
| 246 | { |
| 247 | if (cgain) |
| 248 | return (regval * 16618 - 8500 * 1000) / 5; |
| 249 | else |
| 250 | return (regval * 16618 - 8500 * 1000) / 10; |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * convert uA currents into twl register value |
| 255 | */ |
| 256 | static int ua2regval(int ua, bool cgain) |
| 257 | { |
| 258 | int ret; |
| 259 | if (cgain) |
| 260 | ua /= 2; |
| 261 | ret = (ua * 10 + 8500 * 1000) / 16618; |
| 262 | /* rounding problems */ |
| 263 | if (ret < 512) |
| 264 | ret = 512; |
| 265 | return ret; |
| 266 | } |
| 267 | |
NeilBrown | 1098cb5 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 268 | static int twl4030_charger_update_current(struct twl4030_bci *bci) |
| 269 | { |
| 270 | int status; |
NeilBrown | e4ae537 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 271 | int cur; |
NeilBrown | 1098cb5 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 272 | unsigned reg, cur_reg; |
| 273 | u8 bcictl1, oldreg, fullreg; |
| 274 | bool cgain = false; |
| 275 | u8 boot_bci; |
| 276 | |
NeilBrown | e4ae537 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 277 | /* |
| 278 | * If AC (Accessory Charger) voltage exceeds 4.5V (MADC 11) |
| 279 | * and AC is enabled, set current for 'ac' |
| 280 | */ |
NeilBrown | b49d15d | 2015-08-07 13:45:25 +1000 | [diff] [blame] | 281 | if (ac_available()) { |
NeilBrown | e4ae537 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 282 | cur = bci->ac_cur; |
| 283 | bci->ac_is_active = true; |
| 284 | } else { |
| 285 | cur = bci->usb_cur; |
| 286 | bci->ac_is_active = false; |
NeilBrown | 21ae404 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 287 | if (cur > bci->usb_cur_target) { |
| 288 | cur = bci->usb_cur_target; |
| 289 | bci->usb_cur = cur; |
| 290 | } |
| 291 | if (cur < bci->usb_cur_target) |
| 292 | schedule_delayed_work(&bci->current_worker, USB_CUR_DELAY); |
NeilBrown | e4ae537 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 293 | } |
| 294 | |
NeilBrown | 1098cb5 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 295 | /* First, check thresholds and see if cgain is needed */ |
| 296 | if (bci->ichg_eoc >= 200000) |
| 297 | cgain = true; |
| 298 | if (bci->ichg_lo >= 400000) |
| 299 | cgain = true; |
| 300 | if (bci->ichg_hi >= 820000) |
| 301 | cgain = true; |
NeilBrown | e4ae537 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 302 | if (cur > 852000) |
NeilBrown | 1098cb5 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 303 | cgain = true; |
| 304 | |
| 305 | status = twl4030_bci_read(TWL4030_BCICTL1, &bcictl1); |
| 306 | if (status < 0) |
| 307 | return status; |
| 308 | if (twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &boot_bci, |
| 309 | TWL4030_PM_MASTER_BOOT_BCI) < 0) |
| 310 | boot_bci = 0; |
| 311 | boot_bci &= 7; |
| 312 | |
| 313 | if ((!!cgain) != !!(bcictl1 & TWL4030_CGAIN)) |
| 314 | /* Need to turn for charging while we change the |
| 315 | * CGAIN bit. Leave it off while everything is |
| 316 | * updated. |
| 317 | */ |
| 318 | twl4030_clear_set_boot_bci(boot_bci, 0); |
| 319 | |
| 320 | /* |
| 321 | * For ichg_eoc, the hardware only supports reg values matching |
| 322 | * 100XXXX000, and requires the XXXX be stored in the high nibble |
| 323 | * of TWL4030_BCIMFTH8. |
| 324 | */ |
| 325 | reg = ua2regval(bci->ichg_eoc, cgain); |
| 326 | if (reg > 0x278) |
| 327 | reg = 0x278; |
| 328 | if (reg < 0x200) |
| 329 | reg = 0x200; |
| 330 | reg = (reg >> 3) & 0xf; |
| 331 | fullreg = reg << 4; |
| 332 | |
| 333 | /* |
| 334 | * For ichg_lo, reg value must match 10XXXX0000. |
| 335 | * XXXX is stored in low nibble of TWL4030_BCIMFTH8. |
| 336 | */ |
| 337 | reg = ua2regval(bci->ichg_lo, cgain); |
| 338 | if (reg > 0x2F0) |
| 339 | reg = 0x2F0; |
| 340 | if (reg < 0x200) |
| 341 | reg = 0x200; |
| 342 | reg = (reg >> 4) & 0xf; |
| 343 | fullreg |= reg; |
| 344 | |
| 345 | /* ichg_eoc and ichg_lo live in same register */ |
| 346 | status = twl4030_bci_read(TWL4030_BCIMFTH8, &oldreg); |
| 347 | if (status < 0) |
| 348 | return status; |
| 349 | if (oldreg != fullreg) { |
| 350 | status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xF4, |
| 351 | TWL4030_BCIMFKEY); |
| 352 | if (status < 0) |
| 353 | return status; |
| 354 | twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, |
| 355 | fullreg, TWL4030_BCIMFTH8); |
| 356 | } |
| 357 | |
| 358 | /* ichg_hi threshold must be 1XXXX01100 (I think) */ |
| 359 | reg = ua2regval(bci->ichg_hi, cgain); |
| 360 | if (reg > 0x3E0) |
| 361 | reg = 0x3E0; |
| 362 | if (reg < 0x200) |
| 363 | reg = 0x200; |
| 364 | fullreg = (reg >> 5) & 0xF; |
| 365 | fullreg <<= 4; |
| 366 | status = twl4030_bci_read(TWL4030_BCIMFTH9, &oldreg); |
| 367 | if (status < 0) |
| 368 | return status; |
| 369 | if ((oldreg & 0xF0) != fullreg) { |
| 370 | fullreg |= (oldreg & 0x0F); |
| 371 | status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xE7, |
| 372 | TWL4030_BCIMFKEY); |
| 373 | if (status < 0) |
| 374 | return status; |
| 375 | twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, |
| 376 | fullreg, TWL4030_BCIMFTH9); |
| 377 | } |
| 378 | |
| 379 | /* |
| 380 | * And finally, set the current. This is stored in |
| 381 | * two registers. |
| 382 | */ |
NeilBrown | e4ae537 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 383 | reg = ua2regval(cur, cgain); |
NeilBrown | 1098cb5 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 384 | /* we have only 10 bits */ |
| 385 | if (reg > 0x3ff) |
| 386 | reg = 0x3ff; |
| 387 | status = twl4030_bci_read(TWL4030_BCIIREF1, &oldreg); |
| 388 | if (status < 0) |
| 389 | return status; |
| 390 | cur_reg = oldreg; |
| 391 | status = twl4030_bci_read(TWL4030_BCIIREF2, &oldreg); |
| 392 | if (status < 0) |
| 393 | return status; |
| 394 | cur_reg |= oldreg << 8; |
| 395 | if (reg != oldreg) { |
| 396 | /* disable write protection for one write access for |
| 397 | * BCIIREF */ |
| 398 | status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xE7, |
| 399 | TWL4030_BCIMFKEY); |
| 400 | if (status < 0) |
| 401 | return status; |
| 402 | status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, |
| 403 | (reg & 0x100) ? 3 : 2, |
| 404 | TWL4030_BCIIREF2); |
| 405 | if (status < 0) |
| 406 | return status; |
| 407 | /* disable write protection for one write access for |
| 408 | * BCIIREF */ |
| 409 | status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xE7, |
| 410 | TWL4030_BCIMFKEY); |
| 411 | if (status < 0) |
| 412 | return status; |
| 413 | status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, |
| 414 | reg & 0xff, |
| 415 | TWL4030_BCIIREF1); |
| 416 | } |
| 417 | if ((!!cgain) != !!(bcictl1 & TWL4030_CGAIN)) { |
| 418 | /* Flip CGAIN and re-enable charging */ |
| 419 | bcictl1 ^= TWL4030_CGAIN; |
| 420 | twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, |
| 421 | bcictl1, TWL4030_BCICTL1); |
| 422 | twl4030_clear_set_boot_bci(0, boot_bci); |
| 423 | } |
| 424 | return 0; |
| 425 | } |
| 426 | |
NeilBrown | 21ae404 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 427 | static int twl4030_charger_get_current(void); |
| 428 | |
| 429 | static void twl4030_current_worker(struct work_struct *data) |
| 430 | { |
| 431 | int v, curr; |
| 432 | int res; |
| 433 | struct twl4030_bci *bci = container_of(data, struct twl4030_bci, |
| 434 | current_worker.work); |
| 435 | |
| 436 | res = twl4030bci_read_adc_val(TWL4030_BCIVBUS); |
| 437 | if (res < 0) |
| 438 | v = 0; |
| 439 | else |
| 440 | /* BCIVBUS uses ADCIN8, 7/1023 V/step */ |
| 441 | v = res * 6843; |
| 442 | curr = twl4030_charger_get_current(); |
| 443 | |
| 444 | dev_dbg(bci->dev, "v=%d cur=%d limit=%d target=%d\n", v, curr, |
| 445 | bci->usb_cur, bci->usb_cur_target); |
| 446 | |
| 447 | if (v < USB_MIN_VOLT) { |
| 448 | /* Back up and stop adjusting. */ |
| 449 | bci->usb_cur -= USB_CUR_STEP; |
| 450 | bci->usb_cur_target = bci->usb_cur; |
| 451 | } else if (bci->usb_cur >= bci->usb_cur_target || |
| 452 | bci->usb_cur + USB_CUR_STEP > USB_MAX_CURRENT) { |
| 453 | /* Reached target and voltage is OK - stop */ |
| 454 | return; |
| 455 | } else { |
| 456 | bci->usb_cur += USB_CUR_STEP; |
| 457 | schedule_delayed_work(&bci->current_worker, USB_CUR_DELAY); |
| 458 | } |
| 459 | twl4030_charger_update_current(bci); |
| 460 | } |
| 461 | |
NeilBrown | 3b542f0 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 462 | /* |
NeilBrown | 9c645d2 | 2012-05-09 07:40:39 +1000 | [diff] [blame] | 463 | * Enable/Disable USB Charge functionality. |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 464 | */ |
| 465 | static int twl4030_charger_enable_usb(struct twl4030_bci *bci, bool enable) |
| 466 | { |
| 467 | int ret; |
| 468 | |
NeilBrown | 22d4c33 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 469 | if (bci->usb_mode == CHARGE_OFF) |
| 470 | enable = false; |
NeilBrown | e57c4a6 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 471 | if (enable && !IS_ERR_OR_NULL(bci->transceiver)) { |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 472 | |
NeilBrown | e4ae537 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 473 | twl4030_charger_update_current(bci); |
| 474 | |
NeilBrown | e57c4a6 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 475 | /* Need to keep phy powered */ |
NeilBrown | ab37813 | 2012-05-09 07:40:40 +1000 | [diff] [blame] | 476 | if (!bci->usb_enabled) { |
NeilBrown | e57c4a6 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 477 | pm_runtime_get_sync(bci->transceiver->dev); |
NeilBrown | ab37813 | 2012-05-09 07:40:40 +1000 | [diff] [blame] | 478 | bci->usb_enabled = 1; |
| 479 | } |
| 480 | |
NeilBrown | 7f4a633 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 481 | if (bci->usb_mode == CHARGE_AUTO) |
| 482 | /* forcing the field BCIAUTOUSB (BOOT_BCI[1]) to 1 */ |
| 483 | ret = twl4030_clear_set_boot_bci(0, TWL4030_BCIAUTOUSB); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 484 | |
| 485 | /* forcing USBFASTMCHG(BCIMFSTS4[2]) to 1 */ |
Peter Ujfalusi | e9f14c1 | 2012-11-13 10:42:11 +0100 | [diff] [blame] | 486 | ret = twl4030_clear_set(TWL_MODULE_MAIN_CHARGE, 0, |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 487 | TWL4030_USBFASTMCHG, TWL4030_BCIMFSTS4); |
NeilBrown | 7f4a633 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 488 | if (bci->usb_mode == CHARGE_LINEAR) { |
| 489 | twl4030_clear_set_boot_bci(TWL4030_BCIAUTOAC|TWL4030_CVENAC, 0); |
| 490 | /* Watch dog key: WOVF acknowledge */ |
| 491 | ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x33, |
| 492 | TWL4030_BCIWDKEY); |
| 493 | /* 0x24 + EKEY6: off mode */ |
| 494 | ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x2a, |
| 495 | TWL4030_BCIMDKEY); |
| 496 | /* EKEY2: Linear charge: USB path */ |
| 497 | ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x26, |
| 498 | TWL4030_BCIMDKEY); |
| 499 | /* WDKEY5: stop watchdog count */ |
| 500 | ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xf3, |
| 501 | TWL4030_BCIWDKEY); |
| 502 | /* enable MFEN3 access */ |
| 503 | ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x9c, |
| 504 | TWL4030_BCIMFKEY); |
| 505 | /* ICHGEOCEN - end-of-charge monitor (current < 80mA) |
| 506 | * (charging continues) |
| 507 | * ICHGLOWEN - current level monitor (charge continues) |
| 508 | * don't monitor over-current or heat save |
| 509 | */ |
| 510 | ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xf0, |
| 511 | TWL4030_BCIMFEN3); |
| 512 | } |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 513 | } else { |
| 514 | ret = twl4030_clear_set_boot_bci(TWL4030_BCIAUTOUSB, 0); |
NeilBrown | 7f4a633 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 515 | ret |= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x2a, |
| 516 | TWL4030_BCIMDKEY); |
NeilBrown | ab37813 | 2012-05-09 07:40:40 +1000 | [diff] [blame] | 517 | if (bci->usb_enabled) { |
NeilBrown | e57c4a6 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 518 | pm_runtime_mark_last_busy(bci->transceiver->dev); |
| 519 | pm_runtime_put_autosuspend(bci->transceiver->dev); |
NeilBrown | ab37813 | 2012-05-09 07:40:40 +1000 | [diff] [blame] | 520 | bci->usb_enabled = 0; |
| 521 | } |
NeilBrown | 21ae404 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 522 | bci->usb_cur = 0; |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | return ret; |
| 526 | } |
| 527 | |
| 528 | /* |
| 529 | * Enable/Disable AC Charge funtionality. |
| 530 | */ |
NeilBrown | b04b908 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 531 | static int twl4030_charger_enable_ac(struct twl4030_bci *bci, bool enable) |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 532 | { |
| 533 | int ret; |
| 534 | |
NeilBrown | b04b908 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 535 | if (bci->ac_mode == CHARGE_OFF) |
| 536 | enable = false; |
| 537 | |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 538 | if (enable) |
| 539 | ret = twl4030_clear_set_boot_bci(0, TWL4030_BCIAUTOAC); |
| 540 | else |
| 541 | ret = twl4030_clear_set_boot_bci(TWL4030_BCIAUTOAC, 0); |
| 542 | |
| 543 | return ret; |
| 544 | } |
| 545 | |
| 546 | /* |
NeilBrown | 210d4bc | 2012-05-09 07:40:40 +1000 | [diff] [blame] | 547 | * Enable/Disable charging of Backup Battery. |
| 548 | */ |
| 549 | static int twl4030_charger_enable_backup(int uvolt, int uamp) |
| 550 | { |
| 551 | int ret; |
| 552 | u8 flags; |
| 553 | |
| 554 | if (uvolt < 2500000 || |
| 555 | uamp < 25) { |
| 556 | /* disable charging of backup battery */ |
Peter Ujfalusi | e9f14c1 | 2012-11-13 10:42:11 +0100 | [diff] [blame] | 557 | ret = twl4030_clear_set(TWL_MODULE_PM_RECEIVER, |
NeilBrown | 210d4bc | 2012-05-09 07:40:40 +1000 | [diff] [blame] | 558 | TWL4030_BBCHEN, 0, TWL4030_BB_CFG); |
| 559 | return ret; |
| 560 | } |
| 561 | |
| 562 | flags = TWL4030_BBCHEN; |
| 563 | if (uvolt >= 3200000) |
| 564 | flags |= TWL4030_BBSEL_3V2; |
| 565 | else if (uvolt >= 3100000) |
| 566 | flags |= TWL4030_BBSEL_3V1; |
| 567 | else if (uvolt >= 3000000) |
| 568 | flags |= TWL4030_BBSEL_3V0; |
| 569 | else |
| 570 | flags |= TWL4030_BBSEL_2V5; |
| 571 | |
| 572 | if (uamp >= 1000) |
| 573 | flags |= TWL4030_BBISEL_1000uA; |
| 574 | else if (uamp >= 500) |
| 575 | flags |= TWL4030_BBISEL_500uA; |
| 576 | else if (uamp >= 150) |
| 577 | flags |= TWL4030_BBISEL_150uA; |
| 578 | else |
| 579 | flags |= TWL4030_BBISEL_25uA; |
| 580 | |
Peter Ujfalusi | e9f14c1 | 2012-11-13 10:42:11 +0100 | [diff] [blame] | 581 | ret = twl4030_clear_set(TWL_MODULE_PM_RECEIVER, |
NeilBrown | 210d4bc | 2012-05-09 07:40:40 +1000 | [diff] [blame] | 582 | TWL4030_BBSEL_MASK | TWL4030_BBISEL_MASK, |
| 583 | flags, |
| 584 | TWL4030_BB_CFG); |
| 585 | |
| 586 | return ret; |
| 587 | } |
| 588 | |
| 589 | /* |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 590 | * TWL4030 CHG_PRES (AC charger presence) events |
| 591 | */ |
| 592 | static irqreturn_t twl4030_charger_interrupt(int irq, void *arg) |
| 593 | { |
| 594 | struct twl4030_bci *bci = arg; |
| 595 | |
| 596 | dev_dbg(bci->dev, "CHG_PRES irq\n"); |
NeilBrown | aca3c35 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 597 | /* reset current on each 'plug' event */ |
| 598 | bci->ac_cur = 500000; |
NeilBrown | e4ae537 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 599 | twl4030_charger_update_current(bci); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 600 | power_supply_changed(bci->ac); |
| 601 | power_supply_changed(bci->usb); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 602 | |
| 603 | return IRQ_HANDLED; |
| 604 | } |
| 605 | |
| 606 | /* |
| 607 | * TWL4030 BCI monitoring events |
| 608 | */ |
| 609 | static irqreturn_t twl4030_bci_interrupt(int irq, void *arg) |
| 610 | { |
| 611 | struct twl4030_bci *bci = arg; |
| 612 | u8 irqs1, irqs2; |
| 613 | int ret; |
| 614 | |
| 615 | ret = twl_i2c_read_u8(TWL4030_MODULE_INTERRUPTS, &irqs1, |
| 616 | TWL4030_INTERRUPTS_BCIISR1A); |
| 617 | if (ret < 0) |
| 618 | return IRQ_HANDLED; |
| 619 | |
| 620 | ret = twl_i2c_read_u8(TWL4030_MODULE_INTERRUPTS, &irqs2, |
| 621 | TWL4030_INTERRUPTS_BCIISR2A); |
| 622 | if (ret < 0) |
| 623 | return IRQ_HANDLED; |
| 624 | |
| 625 | dev_dbg(bci->dev, "BCI irq %02x %02x\n", irqs2, irqs1); |
| 626 | |
| 627 | if (irqs1 & (TWL4030_ICHGLOW | TWL4030_ICHGEOC)) { |
| 628 | /* charger state change, inform the core */ |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 629 | power_supply_changed(bci->ac); |
| 630 | power_supply_changed(bci->usb); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 631 | } |
NeilBrown | e4ae537 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 632 | twl4030_charger_update_current(bci); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 633 | |
| 634 | /* various monitoring events, for now we just log them here */ |
| 635 | if (irqs1 & (TWL4030_TBATOR2 | TWL4030_TBATOR1)) |
| 636 | dev_warn(bci->dev, "battery temperature out of range\n"); |
| 637 | |
| 638 | if (irqs1 & TWL4030_BATSTS) |
| 639 | dev_crit(bci->dev, "battery disconnected\n"); |
| 640 | |
| 641 | if (irqs2 & TWL4030_VBATOV) |
| 642 | dev_crit(bci->dev, "VBAT overvoltage\n"); |
| 643 | |
| 644 | if (irqs2 & TWL4030_VBUSOV) |
| 645 | dev_crit(bci->dev, "VBUS overvoltage\n"); |
| 646 | |
| 647 | if (irqs2 & TWL4030_ACCHGOV) |
| 648 | dev_crit(bci->dev, "Ac charger overvoltage\n"); |
| 649 | |
| 650 | return IRQ_HANDLED; |
| 651 | } |
| 652 | |
NeilBrown | aca3c35 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 653 | /* |
| 654 | * Provide "max_current" attribute in sysfs. |
| 655 | */ |
| 656 | static ssize_t |
| 657 | twl4030_bci_max_current_store(struct device *dev, struct device_attribute *attr, |
| 658 | const char *buf, size_t n) |
| 659 | { |
| 660 | struct twl4030_bci *bci = dev_get_drvdata(dev->parent); |
| 661 | int cur = 0; |
| 662 | int status = 0; |
| 663 | status = kstrtoint(buf, 10, &cur); |
| 664 | if (status) |
| 665 | return status; |
| 666 | if (cur < 0) |
| 667 | return -EINVAL; |
| 668 | if (dev == &bci->ac->dev) |
| 669 | bci->ac_cur = cur; |
| 670 | else |
NeilBrown | 21ae404 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 671 | bci->usb_cur_target = cur; |
NeilBrown | aca3c35 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 672 | |
| 673 | twl4030_charger_update_current(bci); |
| 674 | return n; |
| 675 | } |
| 676 | |
| 677 | /* |
| 678 | * sysfs max_current show |
| 679 | */ |
| 680 | static ssize_t twl4030_bci_max_current_show(struct device *dev, |
| 681 | struct device_attribute *attr, char *buf) |
| 682 | { |
| 683 | int status = 0; |
| 684 | int cur = -1; |
| 685 | u8 bcictl1; |
| 686 | struct twl4030_bci *bci = dev_get_drvdata(dev->parent); |
| 687 | |
| 688 | if (dev == &bci->ac->dev) { |
| 689 | if (!bci->ac_is_active) |
| 690 | cur = bci->ac_cur; |
| 691 | } else { |
| 692 | if (bci->ac_is_active) |
NeilBrown | 21ae404 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 693 | cur = bci->usb_cur_target; |
NeilBrown | aca3c35 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 694 | } |
| 695 | if (cur < 0) { |
| 696 | cur = twl4030bci_read_adc_val(TWL4030_BCIIREF1); |
| 697 | if (cur < 0) |
| 698 | return cur; |
| 699 | status = twl4030_bci_read(TWL4030_BCICTL1, &bcictl1); |
| 700 | if (status < 0) |
| 701 | return status; |
| 702 | cur = regval2ua(cur, bcictl1 & TWL4030_CGAIN); |
| 703 | } |
| 704 | return scnprintf(buf, PAGE_SIZE, "%u\n", cur); |
| 705 | } |
| 706 | |
| 707 | static DEVICE_ATTR(max_current, 0644, twl4030_bci_max_current_show, |
| 708 | twl4030_bci_max_current_store); |
| 709 | |
Heikki Krogerus | d6ccc44 | 2011-02-25 13:56:36 +0200 | [diff] [blame] | 710 | static void twl4030_bci_usb_work(struct work_struct *data) |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 711 | { |
Heikki Krogerus | d6ccc44 | 2011-02-25 13:56:36 +0200 | [diff] [blame] | 712 | struct twl4030_bci *bci = container_of(data, struct twl4030_bci, work); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 713 | |
Heikki Krogerus | d6ccc44 | 2011-02-25 13:56:36 +0200 | [diff] [blame] | 714 | switch (bci->event) { |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 715 | case USB_EVENT_VBUS: |
| 716 | case USB_EVENT_CHARGER: |
| 717 | twl4030_charger_enable_usb(bci, true); |
| 718 | break; |
| 719 | case USB_EVENT_NONE: |
| 720 | twl4030_charger_enable_usb(bci, false); |
| 721 | break; |
| 722 | } |
Heikki Krogerus | d6ccc44 | 2011-02-25 13:56:36 +0200 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | static int twl4030_bci_usb_ncb(struct notifier_block *nb, unsigned long val, |
| 726 | void *priv) |
| 727 | { |
Heikki Krogerus | fcc8ebc | 2012-02-13 13:24:16 +0200 | [diff] [blame] | 728 | struct twl4030_bci *bci = container_of(nb, struct twl4030_bci, usb_nb); |
Heikki Krogerus | d6ccc44 | 2011-02-25 13:56:36 +0200 | [diff] [blame] | 729 | |
| 730 | dev_dbg(bci->dev, "OTG notify %lu\n", val); |
| 731 | |
NeilBrown | aca3c35 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 732 | /* reset current on each 'plug' event */ |
| 733 | if (allow_usb) |
NeilBrown | 21ae404 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 734 | bci->usb_cur_target = 500000; |
NeilBrown | aca3c35 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 735 | else |
NeilBrown | 21ae404 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 736 | bci->usb_cur_target = 100000; |
NeilBrown | aca3c35 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 737 | |
Heikki Krogerus | d6ccc44 | 2011-02-25 13:56:36 +0200 | [diff] [blame] | 738 | bci->event = val; |
| 739 | schedule_work(&bci->work); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 740 | |
| 741 | return NOTIFY_OK; |
| 742 | } |
| 743 | |
NeilBrown | 22d4c33 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 744 | /* |
| 745 | * sysfs charger enabled store |
| 746 | */ |
| 747 | static ssize_t |
| 748 | twl4030_bci_mode_store(struct device *dev, struct device_attribute *attr, |
| 749 | const char *buf, size_t n) |
| 750 | { |
| 751 | struct twl4030_bci *bci = dev_get_drvdata(dev->parent); |
| 752 | int mode; |
| 753 | int status; |
| 754 | |
| 755 | if (sysfs_streq(buf, modes[0])) |
| 756 | mode = 0; |
| 757 | else if (sysfs_streq(buf, modes[1])) |
| 758 | mode = 1; |
NeilBrown | 7f4a633 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 759 | else if (sysfs_streq(buf, modes[2])) |
| 760 | mode = 2; |
NeilBrown | 22d4c33 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 761 | else |
| 762 | return -EINVAL; |
NeilBrown | b04b908 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 763 | if (dev == &bci->ac->dev) { |
| 764 | if (mode == 2) |
| 765 | return -EINVAL; |
| 766 | twl4030_charger_enable_ac(bci, false); |
| 767 | bci->ac_mode = mode; |
| 768 | status = twl4030_charger_enable_ac(bci, true); |
| 769 | } else { |
| 770 | twl4030_charger_enable_usb(bci, false); |
| 771 | bci->usb_mode = mode; |
| 772 | status = twl4030_charger_enable_usb(bci, true); |
| 773 | } |
NeilBrown | 22d4c33 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 774 | return (status == 0) ? n : status; |
| 775 | } |
| 776 | |
| 777 | /* |
| 778 | * sysfs charger enabled show |
| 779 | */ |
| 780 | static ssize_t |
| 781 | twl4030_bci_mode_show(struct device *dev, |
| 782 | struct device_attribute *attr, char *buf) |
| 783 | { |
| 784 | struct twl4030_bci *bci = dev_get_drvdata(dev->parent); |
| 785 | int len = 0; |
| 786 | int i; |
NeilBrown | b04b908 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 787 | int mode = bci->usb_mode; |
| 788 | |
| 789 | if (dev == &bci->ac->dev) |
| 790 | mode = bci->ac_mode; |
NeilBrown | 22d4c33 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 791 | |
| 792 | for (i = 0; i < ARRAY_SIZE(modes); i++) |
NeilBrown | b04b908 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 793 | if (mode == i) |
NeilBrown | 22d4c33 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 794 | len += snprintf(buf+len, PAGE_SIZE-len, |
| 795 | "[%s] ", modes[i]); |
| 796 | else |
| 797 | len += snprintf(buf+len, PAGE_SIZE-len, |
| 798 | "%s ", modes[i]); |
| 799 | buf[len-1] = '\n'; |
| 800 | return len; |
| 801 | } |
| 802 | static DEVICE_ATTR(mode, 0644, twl4030_bci_mode_show, |
| 803 | twl4030_bci_mode_store); |
| 804 | |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 805 | static int twl4030_charger_get_current(void) |
| 806 | { |
| 807 | int curr; |
| 808 | int ret; |
| 809 | u8 bcictl1; |
| 810 | |
| 811 | curr = twl4030bci_read_adc_val(TWL4030_BCIICHG); |
| 812 | if (curr < 0) |
| 813 | return curr; |
| 814 | |
| 815 | ret = twl4030_bci_read(TWL4030_BCICTL1, &bcictl1); |
| 816 | if (ret) |
| 817 | return ret; |
| 818 | |
NeilBrown | 3b542f0 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 819 | return regval2ua(curr, bcictl1 & TWL4030_CGAIN); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | /* |
| 823 | * Returns the main charge FSM state |
| 824 | * Or < 0 on failure. |
| 825 | */ |
| 826 | static int twl4030bci_state(struct twl4030_bci *bci) |
| 827 | { |
| 828 | int ret; |
| 829 | u8 state; |
| 830 | |
| 831 | ret = twl4030_bci_read(TWL4030_BCIMSTATEC, &state); |
| 832 | if (ret) { |
| 833 | pr_err("twl4030_bci: error reading BCIMSTATEC\n"); |
| 834 | return ret; |
| 835 | } |
| 836 | |
| 837 | dev_dbg(bci->dev, "state: %02x\n", state); |
| 838 | |
| 839 | return state; |
| 840 | } |
| 841 | |
| 842 | static int twl4030_bci_state_to_status(int state) |
| 843 | { |
| 844 | state &= TWL4030_MSTATEC_MASK; |
| 845 | if (TWL4030_MSTATEC_QUICK1 <= state && state <= TWL4030_MSTATEC_QUICK7) |
| 846 | return POWER_SUPPLY_STATUS_CHARGING; |
| 847 | else if (TWL4030_MSTATEC_COMPLETE1 <= state && |
| 848 | state <= TWL4030_MSTATEC_COMPLETE4) |
| 849 | return POWER_SUPPLY_STATUS_FULL; |
| 850 | else |
| 851 | return POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 852 | } |
| 853 | |
| 854 | static int twl4030_bci_get_property(struct power_supply *psy, |
| 855 | enum power_supply_property psp, |
| 856 | union power_supply_propval *val) |
| 857 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 858 | struct twl4030_bci *bci = dev_get_drvdata(psy->dev.parent); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 859 | int is_charging; |
| 860 | int state; |
| 861 | int ret; |
| 862 | |
| 863 | state = twl4030bci_state(bci); |
| 864 | if (state < 0) |
| 865 | return state; |
| 866 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 867 | if (psy->desc->type == POWER_SUPPLY_TYPE_USB) |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 868 | is_charging = state & TWL4030_MSTATEC_USB; |
| 869 | else |
| 870 | is_charging = state & TWL4030_MSTATEC_AC; |
NeilBrown | 7f4a633 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 871 | if (!is_charging) { |
| 872 | u8 s; |
| 873 | twl4030_bci_read(TWL4030_BCIMDEN, &s); |
| 874 | if (psy->desc->type == POWER_SUPPLY_TYPE_USB) |
| 875 | is_charging = s & 1; |
| 876 | else |
| 877 | is_charging = s & 2; |
| 878 | if (is_charging) |
| 879 | /* A little white lie */ |
| 880 | state = TWL4030_MSTATEC_QUICK1; |
| 881 | } |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 882 | |
| 883 | switch (psp) { |
| 884 | case POWER_SUPPLY_PROP_STATUS: |
| 885 | if (is_charging) |
| 886 | val->intval = twl4030_bci_state_to_status(state); |
| 887 | else |
| 888 | val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 889 | break; |
| 890 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 891 | /* charging must be active for meaningful result */ |
| 892 | if (!is_charging) |
| 893 | return -ENODATA; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 894 | if (psy->desc->type == POWER_SUPPLY_TYPE_USB) { |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 895 | ret = twl4030bci_read_adc_val(TWL4030_BCIVBUS); |
| 896 | if (ret < 0) |
| 897 | return ret; |
| 898 | /* BCIVBUS uses ADCIN8, 7/1023 V/step */ |
| 899 | val->intval = ret * 6843; |
| 900 | } else { |
| 901 | ret = twl4030bci_read_adc_val(TWL4030_BCIVAC); |
| 902 | if (ret < 0) |
| 903 | return ret; |
| 904 | /* BCIVAC uses ADCIN11, 10/1023 V/step */ |
| 905 | val->intval = ret * 9775; |
| 906 | } |
| 907 | break; |
| 908 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 909 | if (!is_charging) |
| 910 | return -ENODATA; |
| 911 | /* current measurement is shared between AC and USB */ |
| 912 | ret = twl4030_charger_get_current(); |
| 913 | if (ret < 0) |
| 914 | return ret; |
| 915 | val->intval = ret; |
| 916 | break; |
| 917 | case POWER_SUPPLY_PROP_ONLINE: |
| 918 | val->intval = is_charging && |
| 919 | twl4030_bci_state_to_status(state) != |
| 920 | POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 921 | break; |
| 922 | default: |
| 923 | return -EINVAL; |
| 924 | } |
| 925 | |
| 926 | return 0; |
| 927 | } |
| 928 | |
| 929 | static enum power_supply_property twl4030_charger_props[] = { |
| 930 | POWER_SUPPLY_PROP_STATUS, |
| 931 | POWER_SUPPLY_PROP_ONLINE, |
| 932 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 933 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 934 | }; |
| 935 | |
NeilBrown | ec0b380 | 2013-10-31 17:05:50 +1100 | [diff] [blame] | 936 | #ifdef CONFIG_OF |
| 937 | static const struct twl4030_bci_platform_data * |
| 938 | twl4030_bci_parse_dt(struct device *dev) |
| 939 | { |
| 940 | struct device_node *np = dev->of_node; |
| 941 | struct twl4030_bci_platform_data *pdata; |
| 942 | u32 num; |
| 943 | |
| 944 | if (!np) |
| 945 | return NULL; |
| 946 | pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); |
| 947 | if (!pdata) |
| 948 | return pdata; |
| 949 | |
| 950 | if (of_property_read_u32(np, "ti,bb-uvolt", &num) == 0) |
| 951 | pdata->bb_uvolt = num; |
| 952 | if (of_property_read_u32(np, "ti,bb-uamp", &num) == 0) |
| 953 | pdata->bb_uamp = num; |
| 954 | return pdata; |
| 955 | } |
| 956 | #else |
| 957 | static inline const struct twl4030_bci_platform_data * |
| 958 | twl4030_bci_parse_dt(struct device *dev) |
| 959 | { |
| 960 | return NULL; |
| 961 | } |
| 962 | #endif |
| 963 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 964 | static const struct power_supply_desc twl4030_bci_ac_desc = { |
| 965 | .name = "twl4030_ac", |
| 966 | .type = POWER_SUPPLY_TYPE_MAINS, |
| 967 | .properties = twl4030_charger_props, |
| 968 | .num_properties = ARRAY_SIZE(twl4030_charger_props), |
| 969 | .get_property = twl4030_bci_get_property, |
| 970 | }; |
| 971 | |
| 972 | static const struct power_supply_desc twl4030_bci_usb_desc = { |
| 973 | .name = "twl4030_usb", |
| 974 | .type = POWER_SUPPLY_TYPE_USB, |
| 975 | .properties = twl4030_charger_props, |
| 976 | .num_properties = ARRAY_SIZE(twl4030_charger_props), |
| 977 | .get_property = twl4030_bci_get_property, |
| 978 | }; |
| 979 | |
Pavel Machek | 7396f70 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 980 | static int twl4030_bci_probe(struct platform_device *pdev) |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 981 | { |
| 982 | struct twl4030_bci *bci; |
NeilBrown | ec0b380 | 2013-10-31 17:05:50 +1100 | [diff] [blame] | 983 | const struct twl4030_bci_platform_data *pdata = pdev->dev.platform_data; |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 984 | int ret; |
Grazvydas Ignotas | 28d48f0 | 2011-06-03 14:43:11 +0300 | [diff] [blame] | 985 | u32 reg; |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 986 | |
NeilBrown | 325b50a | 2015-03-23 10:20:28 +1100 | [diff] [blame] | 987 | bci = devm_kzalloc(&pdev->dev, sizeof(*bci), GFP_KERNEL); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 988 | if (bci == NULL) |
| 989 | return -ENOMEM; |
| 990 | |
NeilBrown | ec0b380 | 2013-10-31 17:05:50 +1100 | [diff] [blame] | 991 | if (!pdata) |
| 992 | pdata = twl4030_bci_parse_dt(&pdev->dev); |
| 993 | |
NeilBrown | 1098cb5 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 994 | bci->ichg_eoc = 80100; /* Stop charging when current drops to here */ |
| 995 | bci->ichg_lo = 241000; /* Low threshold */ |
| 996 | bci->ichg_hi = 500000; /* High threshold */ |
NeilBrown | e4ae537 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 997 | bci->ac_cur = 500000; /* 500mA */ |
NeilBrown | 1098cb5 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 998 | if (allow_usb) |
NeilBrown | 21ae404 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 999 | bci->usb_cur_target = 500000; /* 500mA */ |
NeilBrown | 1098cb5 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 1000 | else |
NeilBrown | 21ae404 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 1001 | bci->usb_cur_target = 100000; /* 100mA */ |
NeilBrown | 22d4c33 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 1002 | bci->usb_mode = CHARGE_AUTO; |
NeilBrown | b04b908 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 1003 | bci->ac_mode = CHARGE_AUTO; |
NeilBrown | 1098cb5 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 1004 | |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 1005 | bci->dev = &pdev->dev; |
| 1006 | bci->irq_chg = platform_get_irq(pdev, 0); |
| 1007 | bci->irq_bci = platform_get_irq(pdev, 1); |
| 1008 | |
Nishanth Menon | 61a7784 | 2014-05-28 16:46:49 -0500 | [diff] [blame] | 1009 | /* Only proceed further *IF* battery is physically present */ |
| 1010 | ret = twl4030_is_battery_present(bci); |
| 1011 | if (ret) { |
| 1012 | dev_crit(&pdev->dev, "Battery was not detected:%d\n", ret); |
NeilBrown | 325b50a | 2015-03-23 10:20:28 +1100 | [diff] [blame] | 1013 | return ret; |
Nishanth Menon | 61a7784 | 2014-05-28 16:46:49 -0500 | [diff] [blame] | 1014 | } |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 1015 | |
Nishanth Menon | 61a7784 | 2014-05-28 16:46:49 -0500 | [diff] [blame] | 1016 | platform_set_drvdata(pdev, bci); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 1017 | |
NeilBrown | 325b50a | 2015-03-23 10:20:28 +1100 | [diff] [blame] | 1018 | bci->ac = devm_power_supply_register(&pdev->dev, &twl4030_bci_ac_desc, |
| 1019 | NULL); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1020 | if (IS_ERR(bci->ac)) { |
| 1021 | ret = PTR_ERR(bci->ac); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 1022 | dev_err(&pdev->dev, "failed to register ac: %d\n", ret); |
NeilBrown | 325b50a | 2015-03-23 10:20:28 +1100 | [diff] [blame] | 1023 | return ret; |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 1024 | } |
| 1025 | |
NeilBrown | 325b50a | 2015-03-23 10:20:28 +1100 | [diff] [blame] | 1026 | bci->usb = devm_power_supply_register(&pdev->dev, &twl4030_bci_usb_desc, |
| 1027 | NULL); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1028 | if (IS_ERR(bci->usb)) { |
| 1029 | ret = PTR_ERR(bci->usb); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 1030 | dev_err(&pdev->dev, "failed to register usb: %d\n", ret); |
NeilBrown | 325b50a | 2015-03-23 10:20:28 +1100 | [diff] [blame] | 1031 | return ret; |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 1032 | } |
| 1033 | |
NeilBrown | 1b7cac2 | 2015-03-23 10:20:28 +1100 | [diff] [blame] | 1034 | ret = devm_request_threaded_irq(&pdev->dev, bci->irq_chg, NULL, |
Fengguang Wu | a2778b0 | 2012-08-23 19:56:43 +0800 | [diff] [blame] | 1035 | twl4030_charger_interrupt, IRQF_ONESHOT, pdev->name, |
| 1036 | bci); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 1037 | if (ret < 0) { |
| 1038 | dev_err(&pdev->dev, "could not request irq %d, status %d\n", |
| 1039 | bci->irq_chg, ret); |
NeilBrown | 325b50a | 2015-03-23 10:20:28 +1100 | [diff] [blame] | 1040 | return ret; |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 1041 | } |
| 1042 | |
NeilBrown | 1b7cac2 | 2015-03-23 10:20:28 +1100 | [diff] [blame] | 1043 | ret = devm_request_threaded_irq(&pdev->dev, bci->irq_bci, NULL, |
Fengguang Wu | a2778b0 | 2012-08-23 19:56:43 +0800 | [diff] [blame] | 1044 | twl4030_bci_interrupt, IRQF_ONESHOT, pdev->name, bci); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 1045 | if (ret < 0) { |
| 1046 | dev_err(&pdev->dev, "could not request irq %d, status %d\n", |
| 1047 | bci->irq_bci, ret); |
NeilBrown | 325b50a | 2015-03-23 10:20:28 +1100 | [diff] [blame] | 1048 | return ret; |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 1049 | } |
| 1050 | |
Heikki Krogerus | d6ccc44 | 2011-02-25 13:56:36 +0200 | [diff] [blame] | 1051 | INIT_WORK(&bci->work, twl4030_bci_usb_work); |
NeilBrown | 21ae404 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 1052 | INIT_DELAYED_WORK(&bci->current_worker, twl4030_current_worker); |
Heikki Krogerus | d6ccc44 | 2011-02-25 13:56:36 +0200 | [diff] [blame] | 1053 | |
NeilBrown | f5e4edb | 2015-03-23 09:52:48 +1100 | [diff] [blame] | 1054 | bci->usb_nb.notifier_call = twl4030_bci_usb_ncb; |
| 1055 | if (bci->dev->of_node) { |
| 1056 | struct device_node *phynode; |
| 1057 | |
| 1058 | phynode = of_find_compatible_node(bci->dev->of_node->parent, |
| 1059 | NULL, "ti,twl4030-usb"); |
Sebastian Reichel | aefc574b | 2015-09-10 10:00:55 +0200 | [diff] [blame] | 1060 | if (phynode) |
NeilBrown | f5e4edb | 2015-03-23 09:52:48 +1100 | [diff] [blame] | 1061 | bci->transceiver = devm_usb_get_phy_by_node( |
| 1062 | bci->dev, phynode, &bci->usb_nb); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | /* Enable interrupts now. */ |
Grazvydas Ignotas | 28d48f0 | 2011-06-03 14:43:11 +0300 | [diff] [blame] | 1066 | reg = ~(u32)(TWL4030_ICHGLOW | TWL4030_ICHGEOC | TWL4030_TBATOR2 | |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 1067 | TWL4030_TBATOR1 | TWL4030_BATSTS); |
| 1068 | ret = twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, reg, |
| 1069 | TWL4030_INTERRUPTS_BCIIMR1A); |
| 1070 | if (ret < 0) { |
| 1071 | dev_err(&pdev->dev, "failed to unmask interrupts: %d\n", ret); |
NeilBrown | 325b50a | 2015-03-23 10:20:28 +1100 | [diff] [blame] | 1072 | return ret; |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 1073 | } |
| 1074 | |
Grazvydas Ignotas | 28d48f0 | 2011-06-03 14:43:11 +0300 | [diff] [blame] | 1075 | reg = ~(u32)(TWL4030_VBATOV | TWL4030_VBUSOV | TWL4030_ACCHGOV); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 1076 | ret = twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, reg, |
| 1077 | TWL4030_INTERRUPTS_BCIIMR2A); |
| 1078 | if (ret < 0) |
| 1079 | dev_warn(&pdev->dev, "failed to unmask interrupts: %d\n", ret); |
| 1080 | |
NeilBrown | 1098cb5 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 1081 | twl4030_charger_update_current(bci); |
NeilBrown | aca3c35 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 1082 | if (device_create_file(&bci->usb->dev, &dev_attr_max_current)) |
| 1083 | dev_warn(&pdev->dev, "could not create sysfs file\n"); |
NeilBrown | 22d4c33 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 1084 | if (device_create_file(&bci->usb->dev, &dev_attr_mode)) |
| 1085 | dev_warn(&pdev->dev, "could not create sysfs file\n"); |
NeilBrown | b04b908 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 1086 | if (device_create_file(&bci->ac->dev, &dev_attr_mode)) |
| 1087 | dev_warn(&pdev->dev, "could not create sysfs file\n"); |
NeilBrown | aca3c35 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 1088 | if (device_create_file(&bci->ac->dev, &dev_attr_max_current)) |
| 1089 | dev_warn(&pdev->dev, "could not create sysfs file\n"); |
| 1090 | |
NeilBrown | b04b908 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 1091 | twl4030_charger_enable_ac(bci, true); |
NeilBrown | 6e37ec8c | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 1092 | if (!IS_ERR_OR_NULL(bci->transceiver)) |
| 1093 | twl4030_bci_usb_ncb(&bci->usb_nb, |
| 1094 | bci->transceiver->last_event, |
| 1095 | NULL); |
| 1096 | else |
| 1097 | twl4030_charger_enable_usb(bci, false); |
NeilBrown | ec0b380 | 2013-10-31 17:05:50 +1100 | [diff] [blame] | 1098 | if (pdata) |
| 1099 | twl4030_charger_enable_backup(pdata->bb_uvolt, |
| 1100 | pdata->bb_uamp); |
| 1101 | else |
| 1102 | twl4030_charger_enable_backup(0, 0); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 1103 | |
| 1104 | return 0; |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | static int __exit twl4030_bci_remove(struct platform_device *pdev) |
| 1108 | { |
| 1109 | struct twl4030_bci *bci = platform_get_drvdata(pdev); |
| 1110 | |
NeilBrown | b04b908 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 1111 | twl4030_charger_enable_ac(bci, false); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 1112 | twl4030_charger_enable_usb(bci, false); |
NeilBrown | 210d4bc | 2012-05-09 07:40:40 +1000 | [diff] [blame] | 1113 | twl4030_charger_enable_backup(0, 0); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 1114 | |
NeilBrown | aca3c35 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 1115 | device_remove_file(&bci->usb->dev, &dev_attr_max_current); |
NeilBrown | 22d4c33 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 1116 | device_remove_file(&bci->usb->dev, &dev_attr_mode); |
NeilBrown | aca3c35 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 1117 | device_remove_file(&bci->ac->dev, &dev_attr_max_current); |
NeilBrown | b04b908 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 1118 | device_remove_file(&bci->ac->dev, &dev_attr_mode); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 1119 | /* mask interrupts */ |
| 1120 | twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, 0xff, |
| 1121 | TWL4030_INTERRUPTS_BCIIMR1A); |
| 1122 | twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, 0xff, |
| 1123 | TWL4030_INTERRUPTS_BCIIMR2A); |
| 1124 | |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 1125 | return 0; |
| 1126 | } |
| 1127 | |
NeilBrown | ec0b380 | 2013-10-31 17:05:50 +1100 | [diff] [blame] | 1128 | static const struct of_device_id twl_bci_of_match[] = { |
| 1129 | {.compatible = "ti,twl4030-bci", }, |
| 1130 | { } |
| 1131 | }; |
| 1132 | MODULE_DEVICE_TABLE(of, twl_bci_of_match); |
| 1133 | |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 1134 | static struct platform_driver twl4030_bci_driver = { |
Pavel Machek | 7396f70 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 1135 | .probe = twl4030_bci_probe, |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 1136 | .driver = { |
| 1137 | .name = "twl4030_bci", |
NeilBrown | ec0b380 | 2013-10-31 17:05:50 +1100 | [diff] [blame] | 1138 | .of_match_table = of_match_ptr(twl_bci_of_match), |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 1139 | }, |
| 1140 | .remove = __exit_p(twl4030_bci_remove), |
| 1141 | }; |
Pavel Machek | 7396f70 | 2015-07-30 10:11:24 +1000 | [diff] [blame] | 1142 | module_platform_driver(twl4030_bci_driver); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 1143 | |
Grazvydas Ignotas | 28d48f0 | 2011-06-03 14:43:11 +0300 | [diff] [blame] | 1144 | MODULE_AUTHOR("Gražvydas Ignotas"); |
Grazvydas Ignotas | 2e727f1 | 2010-09-27 23:18:24 +0300 | [diff] [blame] | 1145 | MODULE_DESCRIPTION("TWL4030 Battery Charger Interface driver"); |
| 1146 | MODULE_LICENSE("GPL"); |
| 1147 | MODULE_ALIAS("platform:twl4030_bci"); |