Krzysztof Kozlowski | 1213a36 | 2018-08-07 18:21:22 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | // |
| 3 | // extcon-max77843.c - Maxim MAX77843 extcon driver to support |
| 4 | // MUIC(Micro USB Interface Controller) |
| 5 | // |
| 6 | // Copyright (C) 2015 Samsung Electronics |
| 7 | // Author: Jaewon Kim <jaewon02.kim@samsung.com> |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 8 | |
Chanwoo Choi | 176aa36 | 2017-09-21 12:11:24 +0900 | [diff] [blame] | 9 | #include <linux/extcon-provider.h> |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 10 | #include <linux/i2c.h> |
| 11 | #include <linux/interrupt.h> |
| 12 | #include <linux/kernel.h> |
Krzysztof Kozlowski | bc1aadc | 2015-07-15 21:59:51 +0900 | [diff] [blame] | 13 | #include <linux/mfd/max77693-common.h> |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 14 | #include <linux/mfd/max77843-private.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/workqueue.h> |
| 18 | |
| 19 | #define DELAY_MS_DEFAULT 15000 /* unit: millisecond */ |
| 20 | |
| 21 | enum max77843_muic_status { |
| 22 | MAX77843_MUIC_STATUS1 = 0, |
| 23 | MAX77843_MUIC_STATUS2, |
| 24 | MAX77843_MUIC_STATUS3, |
| 25 | |
| 26 | MAX77843_MUIC_STATUS_NUM, |
| 27 | }; |
| 28 | |
| 29 | struct max77843_muic_info { |
| 30 | struct device *dev; |
Krzysztof Kozlowski | bc1aadc | 2015-07-15 21:59:51 +0900 | [diff] [blame] | 31 | struct max77693_dev *max77843; |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 32 | struct extcon_dev *edev; |
| 33 | |
| 34 | struct mutex mutex; |
| 35 | struct work_struct irq_work; |
| 36 | struct delayed_work wq_detcable; |
| 37 | |
| 38 | u8 status[MAX77843_MUIC_STATUS_NUM]; |
| 39 | int prev_cable_type; |
| 40 | int prev_chg_type; |
| 41 | int prev_gnd_type; |
| 42 | |
| 43 | bool irq_adc; |
| 44 | bool irq_chg; |
| 45 | }; |
| 46 | |
| 47 | enum max77843_muic_cable_group { |
| 48 | MAX77843_CABLE_GROUP_ADC = 0, |
| 49 | MAX77843_CABLE_GROUP_ADC_GND, |
| 50 | MAX77843_CABLE_GROUP_CHG, |
| 51 | }; |
| 52 | |
| 53 | enum max77843_muic_adc_debounce_time { |
| 54 | MAX77843_DEBOUNCE_TIME_5MS = 0, |
| 55 | MAX77843_DEBOUNCE_TIME_10MS, |
| 56 | MAX77843_DEBOUNCE_TIME_25MS, |
| 57 | MAX77843_DEBOUNCE_TIME_38_62MS, |
| 58 | }; |
| 59 | |
| 60 | /* Define accessory cable type */ |
| 61 | enum max77843_muic_accessory_type { |
| 62 | MAX77843_MUIC_ADC_GROUND = 0, |
| 63 | MAX77843_MUIC_ADC_SEND_END_BUTTON, |
| 64 | MAX77843_MUIC_ADC_REMOTE_S1_BUTTON, |
| 65 | MAX77843_MUIC_ADC_REMOTE_S2_BUTTON, |
| 66 | MAX77843_MUIC_ADC_REMOTE_S3_BUTTON, |
| 67 | MAX77843_MUIC_ADC_REMOTE_S4_BUTTON, |
| 68 | MAX77843_MUIC_ADC_REMOTE_S5_BUTTON, |
| 69 | MAX77843_MUIC_ADC_REMOTE_S6_BUTTON, |
| 70 | MAX77843_MUIC_ADC_REMOTE_S7_BUTTON, |
| 71 | MAX77843_MUIC_ADC_REMOTE_S8_BUTTON, |
| 72 | MAX77843_MUIC_ADC_REMOTE_S9_BUTTON, |
| 73 | MAX77843_MUIC_ADC_REMOTE_S10_BUTTON, |
| 74 | MAX77843_MUIC_ADC_REMOTE_S11_BUTTON, |
| 75 | MAX77843_MUIC_ADC_REMOTE_S12_BUTTON, |
| 76 | MAX77843_MUIC_ADC_RESERVED_ACC_1, |
| 77 | MAX77843_MUIC_ADC_RESERVED_ACC_2, |
Marek Szyprowski | 4a4a8714 | 2017-10-18 11:56:22 +0200 | [diff] [blame] | 78 | MAX77843_MUIC_ADC_RESERVED_ACC_3, /* SmartDock */ |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 79 | MAX77843_MUIC_ADC_RESERVED_ACC_4, |
| 80 | MAX77843_MUIC_ADC_RESERVED_ACC_5, |
| 81 | MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE2, |
| 82 | MAX77843_MUIC_ADC_PHONE_POWERED_DEV, |
| 83 | MAX77843_MUIC_ADC_TTY_CONVERTER, |
| 84 | MAX77843_MUIC_ADC_UART_CABLE, |
| 85 | MAX77843_MUIC_ADC_CEA936A_TYPE1_CHG, |
| 86 | MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF, |
| 87 | MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON, |
| 88 | MAX77843_MUIC_ADC_AV_CABLE_NOLOAD, |
| 89 | MAX77843_MUIC_ADC_CEA936A_TYPE2_CHG, |
| 90 | MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF, |
| 91 | MAX77843_MUIC_ADC_FACTORY_MODE_UART_ON, |
| 92 | MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE1, |
| 93 | MAX77843_MUIC_ADC_OPEN, |
| 94 | |
Srikant Ritolia | af57fa4 | 2016-12-07 17:29:39 +0530 | [diff] [blame] | 95 | /* |
| 96 | * The below accessories should check |
| 97 | * not only ADC value but also ADC1K and VBVolt value. |
| 98 | */ |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 99 | /* Offset|ADC1K|VBVolt| */ |
| 100 | MAX77843_MUIC_GND_USB_HOST = 0x100, /* 0x1| 0| 0| */ |
| 101 | MAX77843_MUIC_GND_USB_HOST_VB = 0x101, /* 0x1| 0| 1| */ |
| 102 | MAX77843_MUIC_GND_MHL = 0x102, /* 0x1| 1| 0| */ |
| 103 | MAX77843_MUIC_GND_MHL_VB = 0x103, /* 0x1| 1| 1| */ |
| 104 | }; |
| 105 | |
| 106 | /* Define charger cable type */ |
| 107 | enum max77843_muic_charger_type { |
| 108 | MAX77843_MUIC_CHG_NONE = 0, |
| 109 | MAX77843_MUIC_CHG_USB, |
| 110 | MAX77843_MUIC_CHG_DOWNSTREAM, |
| 111 | MAX77843_MUIC_CHG_DEDICATED, |
| 112 | MAX77843_MUIC_CHG_SPECIAL_500MA, |
| 113 | MAX77843_MUIC_CHG_SPECIAL_1A, |
| 114 | MAX77843_MUIC_CHG_SPECIAL_BIAS, |
| 115 | MAX77843_MUIC_CHG_RESERVED, |
| 116 | MAX77843_MUIC_CHG_GND, |
Marek Szyprowski | 4a4a8714 | 2017-10-18 11:56:22 +0200 | [diff] [blame] | 117 | MAX77843_MUIC_CHG_DOCK, |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 118 | }; |
| 119 | |
Chanwoo Choi | 73b6ecd | 2015-06-12 11:10:06 +0900 | [diff] [blame] | 120 | static const unsigned int max77843_extcon_cable[] = { |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 121 | EXTCON_USB, |
| 122 | EXTCON_USB_HOST, |
Chanwoo Choi | 8b45b6a | 2015-11-09 10:10:15 +0900 | [diff] [blame] | 123 | EXTCON_CHG_USB_SDP, |
Chanwoo Choi | 11eecf9 | 2015-10-03 14:15:13 +0900 | [diff] [blame] | 124 | EXTCON_CHG_USB_DCP, |
| 125 | EXTCON_CHG_USB_CDP, |
| 126 | EXTCON_CHG_USB_FAST, |
| 127 | EXTCON_CHG_USB_SLOW, |
| 128 | EXTCON_DISP_MHL, |
Marek Szyprowski | 4a4a8714 | 2017-10-18 11:56:22 +0200 | [diff] [blame] | 129 | EXTCON_DOCK, |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 130 | EXTCON_JIG, |
| 131 | EXTCON_NONE, |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | struct max77843_muic_irq { |
| 135 | unsigned int irq; |
| 136 | const char *name; |
| 137 | unsigned int virq; |
| 138 | }; |
| 139 | |
| 140 | static struct max77843_muic_irq max77843_muic_irqs[] = { |
| 141 | { MAX77843_MUIC_IRQ_INT1_ADC, "MUIC-ADC" }, |
| 142 | { MAX77843_MUIC_IRQ_INT1_ADCERROR, "MUIC-ADC_ERROR" }, |
| 143 | { MAX77843_MUIC_IRQ_INT1_ADC1K, "MUIC-ADC1K" }, |
| 144 | { MAX77843_MUIC_IRQ_INT2_CHGTYP, "MUIC-CHGTYP" }, |
| 145 | { MAX77843_MUIC_IRQ_INT2_CHGDETRUN, "MUIC-CHGDETRUN" }, |
| 146 | { MAX77843_MUIC_IRQ_INT2_DCDTMR, "MUIC-DCDTMR" }, |
| 147 | { MAX77843_MUIC_IRQ_INT2_DXOVP, "MUIC-DXOVP" }, |
| 148 | { MAX77843_MUIC_IRQ_INT2_VBVOLT, "MUIC-VBVOLT" }, |
| 149 | { MAX77843_MUIC_IRQ_INT3_VBADC, "MUIC-VBADC" }, |
| 150 | { MAX77843_MUIC_IRQ_INT3_VDNMON, "MUIC-VDNMON" }, |
| 151 | { MAX77843_MUIC_IRQ_INT3_DNRES, "MUIC-DNRES" }, |
| 152 | { MAX77843_MUIC_IRQ_INT3_MPNACK, "MUIC-MPNACK"}, |
| 153 | { MAX77843_MUIC_IRQ_INT3_MRXBUFOW, "MUIC-MRXBUFOW"}, |
| 154 | { MAX77843_MUIC_IRQ_INT3_MRXTRF, "MUIC-MRXTRF"}, |
| 155 | { MAX77843_MUIC_IRQ_INT3_MRXPERR, "MUIC-MRXPERR"}, |
| 156 | { MAX77843_MUIC_IRQ_INT3_MRXRDY, "MUIC-MRXRDY"}, |
| 157 | }; |
| 158 | |
| 159 | static const struct regmap_config max77843_muic_regmap_config = { |
| 160 | .reg_bits = 8, |
| 161 | .val_bits = 8, |
| 162 | .max_register = MAX77843_MUIC_REG_END, |
| 163 | }; |
| 164 | |
| 165 | static const struct regmap_irq max77843_muic_irq[] = { |
| 166 | /* INT1 interrupt */ |
| 167 | { .reg_offset = 0, .mask = MAX77843_MUIC_ADC, }, |
| 168 | { .reg_offset = 0, .mask = MAX77843_MUIC_ADCERROR, }, |
| 169 | { .reg_offset = 0, .mask = MAX77843_MUIC_ADC1K, }, |
| 170 | |
| 171 | /* INT2 interrupt */ |
| 172 | { .reg_offset = 1, .mask = MAX77843_MUIC_CHGTYP, }, |
| 173 | { .reg_offset = 1, .mask = MAX77843_MUIC_CHGDETRUN, }, |
| 174 | { .reg_offset = 1, .mask = MAX77843_MUIC_DCDTMR, }, |
| 175 | { .reg_offset = 1, .mask = MAX77843_MUIC_DXOVP, }, |
| 176 | { .reg_offset = 1, .mask = MAX77843_MUIC_VBVOLT, }, |
| 177 | |
| 178 | /* INT3 interrupt */ |
| 179 | { .reg_offset = 2, .mask = MAX77843_MUIC_VBADC, }, |
| 180 | { .reg_offset = 2, .mask = MAX77843_MUIC_VDNMON, }, |
| 181 | { .reg_offset = 2, .mask = MAX77843_MUIC_DNRES, }, |
| 182 | { .reg_offset = 2, .mask = MAX77843_MUIC_MPNACK, }, |
| 183 | { .reg_offset = 2, .mask = MAX77843_MUIC_MRXBUFOW, }, |
| 184 | { .reg_offset = 2, .mask = MAX77843_MUIC_MRXTRF, }, |
| 185 | { .reg_offset = 2, .mask = MAX77843_MUIC_MRXPERR, }, |
| 186 | { .reg_offset = 2, .mask = MAX77843_MUIC_MRXRDY, }, |
| 187 | }; |
| 188 | |
| 189 | static const struct regmap_irq_chip max77843_muic_irq_chip = { |
| 190 | .name = "max77843-muic", |
| 191 | .status_base = MAX77843_MUIC_REG_INT1, |
| 192 | .mask_base = MAX77843_MUIC_REG_INTMASK1, |
| 193 | .mask_invert = true, |
| 194 | .num_regs = 3, |
| 195 | .irqs = max77843_muic_irq, |
| 196 | .num_irqs = ARRAY_SIZE(max77843_muic_irq), |
| 197 | }; |
| 198 | |
| 199 | static int max77843_muic_set_path(struct max77843_muic_info *info, |
Marek Szyprowski | 4a4a8714 | 2017-10-18 11:56:22 +0200 | [diff] [blame] | 200 | u8 val, bool attached, bool nobccomp) |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 201 | { |
Krzysztof Kozlowski | bc1aadc | 2015-07-15 21:59:51 +0900 | [diff] [blame] | 202 | struct max77693_dev *max77843 = info->max77843; |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 203 | int ret = 0; |
| 204 | unsigned int ctrl1, ctrl2; |
| 205 | |
| 206 | if (attached) |
| 207 | ctrl1 = val; |
| 208 | else |
Krzysztof Kozlowski | 309a3e0 | 2015-07-15 21:59:53 +0900 | [diff] [blame] | 209 | ctrl1 = MAX77843_MUIC_CONTROL1_SW_OPEN; |
Marek Szyprowski | 4a4a8714 | 2017-10-18 11:56:22 +0200 | [diff] [blame] | 210 | if (nobccomp) { |
| 211 | /* Disable BC1.2 protocol and force manual switch control */ |
| 212 | ctrl1 |= MAX77843_MUIC_CONTROL1_NOBCCOMP_MASK; |
| 213 | } |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 214 | |
| 215 | ret = regmap_update_bits(max77843->regmap_muic, |
| 216 | MAX77843_MUIC_REG_CONTROL1, |
Marek Szyprowski | 4a4a8714 | 2017-10-18 11:56:22 +0200 | [diff] [blame] | 217 | MAX77843_MUIC_CONTROL1_COM_SW | |
| 218 | MAX77843_MUIC_CONTROL1_NOBCCOMP_MASK, |
| 219 | ctrl1); |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 220 | if (ret < 0) { |
| 221 | dev_err(info->dev, "Cannot switch MUIC port\n"); |
| 222 | return ret; |
| 223 | } |
| 224 | |
| 225 | if (attached) |
| 226 | ctrl2 = MAX77843_MUIC_CONTROL2_CPEN_MASK; |
| 227 | else |
| 228 | ctrl2 = MAX77843_MUIC_CONTROL2_LOWPWR_MASK; |
| 229 | |
| 230 | ret = regmap_update_bits(max77843->regmap_muic, |
| 231 | MAX77843_MUIC_REG_CONTROL2, |
| 232 | MAX77843_MUIC_CONTROL2_LOWPWR_MASK | |
| 233 | MAX77843_MUIC_CONTROL2_CPEN_MASK, ctrl2); |
| 234 | if (ret < 0) { |
| 235 | dev_err(info->dev, "Cannot update lowpower mode\n"); |
| 236 | return ret; |
| 237 | } |
| 238 | |
| 239 | dev_dbg(info->dev, |
| 240 | "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n", |
| 241 | ctrl1, ctrl2, attached ? "attached" : "detached"); |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
Marek Szyprowski | 7b965110 | 2017-10-18 11:56:21 +0200 | [diff] [blame] | 246 | static void max77843_charger_set_otg_vbus(struct max77843_muic_info *info, |
| 247 | bool on) |
| 248 | { |
| 249 | struct max77693_dev *max77843 = info->max77843; |
| 250 | unsigned int cnfg00; |
| 251 | |
| 252 | if (on) |
| 253 | cnfg00 = MAX77843_CHG_OTG_MASK | MAX77843_CHG_BOOST_MASK; |
| 254 | else |
| 255 | cnfg00 = MAX77843_CHG_ENABLE | MAX77843_CHG_BUCK_MASK; |
| 256 | |
| 257 | regmap_update_bits(max77843->regmap_chg, MAX77843_CHG_REG_CHG_CNFG_00, |
| 258 | MAX77843_CHG_MODE_MASK, cnfg00); |
| 259 | } |
| 260 | |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 261 | static int max77843_muic_get_cable_type(struct max77843_muic_info *info, |
| 262 | enum max77843_muic_cable_group group, bool *attached) |
| 263 | { |
| 264 | int adc, chg_type, cable_type, gnd_type; |
| 265 | |
| 266 | adc = info->status[MAX77843_MUIC_STATUS1] & |
| 267 | MAX77843_MUIC_STATUS1_ADC_MASK; |
Krzysztof Kozlowski | 309a3e0 | 2015-07-15 21:59:53 +0900 | [diff] [blame] | 268 | adc >>= MAX77843_MUIC_STATUS1_ADC_SHIFT; |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 269 | |
| 270 | switch (group) { |
| 271 | case MAX77843_CABLE_GROUP_ADC: |
| 272 | if (adc == MAX77843_MUIC_ADC_OPEN) { |
| 273 | *attached = false; |
| 274 | cable_type = info->prev_cable_type; |
| 275 | info->prev_cable_type = MAX77843_MUIC_ADC_OPEN; |
| 276 | } else { |
| 277 | *attached = true; |
| 278 | cable_type = info->prev_cable_type = adc; |
| 279 | } |
| 280 | break; |
| 281 | case MAX77843_CABLE_GROUP_CHG: |
| 282 | chg_type = info->status[MAX77843_MUIC_STATUS2] & |
| 283 | MAX77843_MUIC_STATUS2_CHGTYP_MASK; |
| 284 | |
| 285 | /* Check GROUND accessory with charger cable */ |
| 286 | if (adc == MAX77843_MUIC_ADC_GROUND) { |
| 287 | if (chg_type == MAX77843_MUIC_CHG_NONE) { |
Srikant Ritolia | af57fa4 | 2016-12-07 17:29:39 +0530 | [diff] [blame] | 288 | /* |
| 289 | * The following state when charger cable is |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 290 | * disconnected but the GROUND accessory still |
Srikant Ritolia | af57fa4 | 2016-12-07 17:29:39 +0530 | [diff] [blame] | 291 | * connected. |
| 292 | */ |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 293 | *attached = false; |
| 294 | cable_type = info->prev_chg_type; |
| 295 | info->prev_chg_type = MAX77843_MUIC_CHG_NONE; |
| 296 | } else { |
| 297 | |
Srikant Ritolia | af57fa4 | 2016-12-07 17:29:39 +0530 | [diff] [blame] | 298 | /* |
| 299 | * The following state when charger cable is |
| 300 | * connected on the GROUND accessory. |
| 301 | */ |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 302 | *attached = true; |
| 303 | cable_type = MAX77843_MUIC_CHG_GND; |
| 304 | info->prev_chg_type = MAX77843_MUIC_CHG_GND; |
| 305 | } |
| 306 | break; |
| 307 | } |
| 308 | |
Marek Szyprowski | 4a4a8714 | 2017-10-18 11:56:22 +0200 | [diff] [blame] | 309 | if (adc == MAX77843_MUIC_ADC_RESERVED_ACC_3) { /* SmartDock */ |
| 310 | if (chg_type == MAX77843_MUIC_CHG_NONE) { |
| 311 | *attached = false; |
| 312 | cable_type = info->prev_chg_type; |
| 313 | info->prev_chg_type = MAX77843_MUIC_CHG_NONE; |
| 314 | } else { |
| 315 | *attached = true; |
| 316 | cable_type = MAX77843_MUIC_CHG_DOCK; |
| 317 | info->prev_chg_type = MAX77843_MUIC_CHG_DOCK; |
| 318 | } |
| 319 | break; |
| 320 | } |
| 321 | |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 322 | if (chg_type == MAX77843_MUIC_CHG_NONE) { |
| 323 | *attached = false; |
| 324 | cable_type = info->prev_chg_type; |
| 325 | info->prev_chg_type = MAX77843_MUIC_CHG_NONE; |
| 326 | } else { |
| 327 | *attached = true; |
| 328 | cable_type = info->prev_chg_type = chg_type; |
| 329 | } |
| 330 | break; |
| 331 | case MAX77843_CABLE_GROUP_ADC_GND: |
| 332 | if (adc == MAX77843_MUIC_ADC_OPEN) { |
| 333 | *attached = false; |
| 334 | cable_type = info->prev_gnd_type; |
| 335 | info->prev_gnd_type = MAX77843_MUIC_ADC_OPEN; |
| 336 | } else { |
| 337 | *attached = true; |
| 338 | |
Srikant Ritolia | af57fa4 | 2016-12-07 17:29:39 +0530 | [diff] [blame] | 339 | /* |
| 340 | * Offset|ADC1K|VBVolt| |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 341 | * 0x1| 0| 0| USB-HOST |
| 342 | * 0x1| 0| 1| USB-HOST with VB |
| 343 | * 0x1| 1| 0| MHL |
Srikant Ritolia | af57fa4 | 2016-12-07 17:29:39 +0530 | [diff] [blame] | 344 | * 0x1| 1| 1| MHL with VB |
| 345 | */ |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 346 | /* Get ADC1K register bit */ |
| 347 | gnd_type = (info->status[MAX77843_MUIC_STATUS1] & |
| 348 | MAX77843_MUIC_STATUS1_ADC1K_MASK); |
| 349 | |
| 350 | /* Get VBVolt register bit */ |
| 351 | gnd_type |= (info->status[MAX77843_MUIC_STATUS2] & |
| 352 | MAX77843_MUIC_STATUS2_VBVOLT_MASK); |
Krzysztof Kozlowski | 309a3e0 | 2015-07-15 21:59:53 +0900 | [diff] [blame] | 353 | gnd_type >>= MAX77843_MUIC_STATUS2_VBVOLT_SHIFT; |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 354 | |
| 355 | /* Offset of GND cable */ |
| 356 | gnd_type |= MAX77843_MUIC_GND_USB_HOST; |
| 357 | cable_type = info->prev_gnd_type = gnd_type; |
| 358 | } |
| 359 | break; |
| 360 | default: |
| 361 | dev_err(info->dev, "Unknown cable group (%d)\n", group); |
| 362 | cable_type = -EINVAL; |
| 363 | break; |
| 364 | } |
| 365 | |
| 366 | return cable_type; |
| 367 | } |
| 368 | |
| 369 | static int max77843_muic_adc_gnd_handler(struct max77843_muic_info *info) |
| 370 | { |
| 371 | int ret, gnd_cable_type; |
| 372 | bool attached; |
| 373 | |
| 374 | gnd_cable_type = max77843_muic_get_cable_type(info, |
| 375 | MAX77843_CABLE_GROUP_ADC_GND, &attached); |
| 376 | dev_dbg(info->dev, "external connector is %s (gnd:0x%02x)\n", |
| 377 | attached ? "attached" : "detached", gnd_cable_type); |
| 378 | |
| 379 | switch (gnd_cable_type) { |
| 380 | case MAX77843_MUIC_GND_USB_HOST: |
| 381 | case MAX77843_MUIC_GND_USB_HOST_VB: |
Krzysztof Kozlowski | 309a3e0 | 2015-07-15 21:59:53 +0900 | [diff] [blame] | 382 | ret = max77843_muic_set_path(info, |
| 383 | MAX77843_MUIC_CONTROL1_SW_USB, |
Marek Szyprowski | 4a4a8714 | 2017-10-18 11:56:22 +0200 | [diff] [blame] | 384 | attached, false); |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 385 | if (ret < 0) |
| 386 | return ret; |
| 387 | |
Chanwoo Choi | 8670b45 | 2016-08-16 15:55:34 +0900 | [diff] [blame] | 388 | extcon_set_state_sync(info->edev, EXTCON_USB_HOST, attached); |
Marek Szyprowski | 7b965110 | 2017-10-18 11:56:21 +0200 | [diff] [blame] | 389 | max77843_charger_set_otg_vbus(info, attached); |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 390 | break; |
| 391 | case MAX77843_MUIC_GND_MHL_VB: |
| 392 | case MAX77843_MUIC_GND_MHL: |
Krzysztof Kozlowski | 309a3e0 | 2015-07-15 21:59:53 +0900 | [diff] [blame] | 393 | ret = max77843_muic_set_path(info, |
| 394 | MAX77843_MUIC_CONTROL1_SW_OPEN, |
Marek Szyprowski | 4a4a8714 | 2017-10-18 11:56:22 +0200 | [diff] [blame] | 395 | attached, false); |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 396 | if (ret < 0) |
| 397 | return ret; |
| 398 | |
Chanwoo Choi | 8670b45 | 2016-08-16 15:55:34 +0900 | [diff] [blame] | 399 | extcon_set_state_sync(info->edev, EXTCON_DISP_MHL, attached); |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 400 | break; |
| 401 | default: |
| 402 | dev_err(info->dev, "failed to detect %s accessory(gnd:0x%x)\n", |
| 403 | attached ? "attached" : "detached", gnd_cable_type); |
| 404 | return -EINVAL; |
| 405 | } |
| 406 | |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | static int max77843_muic_jig_handler(struct max77843_muic_info *info, |
| 411 | int cable_type, bool attached) |
| 412 | { |
| 413 | int ret; |
Krzysztof Kozlowski | 309a3e0 | 2015-07-15 21:59:53 +0900 | [diff] [blame] | 414 | u8 path = MAX77843_MUIC_CONTROL1_SW_OPEN; |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 415 | |
| 416 | dev_dbg(info->dev, "external connector is %s (adc:0x%02x)\n", |
| 417 | attached ? "attached" : "detached", cable_type); |
| 418 | |
| 419 | switch (cable_type) { |
| 420 | case MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF: |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 421 | case MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON: |
Krzysztof Kozlowski | 309a3e0 | 2015-07-15 21:59:53 +0900 | [diff] [blame] | 422 | path = MAX77843_MUIC_CONTROL1_SW_USB; |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 423 | break; |
| 424 | case MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF: |
Krzysztof Kozlowski | 309a3e0 | 2015-07-15 21:59:53 +0900 | [diff] [blame] | 425 | path = MAX77843_MUIC_CONTROL1_SW_UART; |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 426 | break; |
| 427 | default: |
Chanwoo Choi | 41b3c01 | 2015-04-24 19:50:48 +0900 | [diff] [blame] | 428 | return -EINVAL; |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 429 | } |
| 430 | |
Marek Szyprowski | 4a4a8714 | 2017-10-18 11:56:22 +0200 | [diff] [blame] | 431 | ret = max77843_muic_set_path(info, path, attached, false); |
Chanwoo Choi | 41b3c01 | 2015-04-24 19:50:48 +0900 | [diff] [blame] | 432 | if (ret < 0) |
| 433 | return ret; |
| 434 | |
Chanwoo Choi | 8670b45 | 2016-08-16 15:55:34 +0900 | [diff] [blame] | 435 | extcon_set_state_sync(info->edev, EXTCON_JIG, attached); |
Chanwoo Choi | 41b3c01 | 2015-04-24 19:50:48 +0900 | [diff] [blame] | 436 | |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 437 | return 0; |
| 438 | } |
| 439 | |
Marek Szyprowski | 4a4a8714 | 2017-10-18 11:56:22 +0200 | [diff] [blame] | 440 | static int max77843_muic_dock_handler(struct max77843_muic_info *info, |
| 441 | bool attached) |
| 442 | { |
| 443 | int ret; |
| 444 | |
| 445 | dev_dbg(info->dev, "external connector is %s (adc: 0x10)\n", |
| 446 | attached ? "attached" : "detached"); |
| 447 | |
| 448 | ret = max77843_muic_set_path(info, MAX77843_MUIC_CONTROL1_SW_USB, |
| 449 | attached, attached); |
| 450 | if (ret < 0) |
| 451 | return ret; |
| 452 | |
| 453 | extcon_set_state_sync(info->edev, EXTCON_DISP_MHL, attached); |
| 454 | extcon_set_state_sync(info->edev, EXTCON_USB_HOST, attached); |
| 455 | extcon_set_state_sync(info->edev, EXTCON_DOCK, attached); |
| 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 460 | static int max77843_muic_adc_handler(struct max77843_muic_info *info) |
| 461 | { |
| 462 | int ret, cable_type; |
| 463 | bool attached; |
| 464 | |
| 465 | cable_type = max77843_muic_get_cable_type(info, |
| 466 | MAX77843_CABLE_GROUP_ADC, &attached); |
| 467 | |
| 468 | dev_dbg(info->dev, |
| 469 | "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n", |
| 470 | attached ? "attached" : "detached", cable_type, |
| 471 | info->prev_cable_type); |
| 472 | |
| 473 | switch (cable_type) { |
Marek Szyprowski | 4a4a8714 | 2017-10-18 11:56:22 +0200 | [diff] [blame] | 474 | case MAX77843_MUIC_ADC_RESERVED_ACC_3: /* SmartDock */ |
| 475 | ret = max77843_muic_dock_handler(info, attached); |
| 476 | if (ret < 0) |
| 477 | return ret; |
| 478 | break; |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 479 | case MAX77843_MUIC_ADC_GROUND: |
| 480 | ret = max77843_muic_adc_gnd_handler(info); |
| 481 | if (ret < 0) |
| 482 | return ret; |
| 483 | break; |
| 484 | case MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF: |
| 485 | case MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON: |
| 486 | case MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF: |
| 487 | ret = max77843_muic_jig_handler(info, cable_type, attached); |
| 488 | if (ret < 0) |
| 489 | return ret; |
| 490 | break; |
| 491 | case MAX77843_MUIC_ADC_SEND_END_BUTTON: |
| 492 | case MAX77843_MUIC_ADC_REMOTE_S1_BUTTON: |
| 493 | case MAX77843_MUIC_ADC_REMOTE_S2_BUTTON: |
| 494 | case MAX77843_MUIC_ADC_REMOTE_S3_BUTTON: |
| 495 | case MAX77843_MUIC_ADC_REMOTE_S4_BUTTON: |
| 496 | case MAX77843_MUIC_ADC_REMOTE_S5_BUTTON: |
| 497 | case MAX77843_MUIC_ADC_REMOTE_S6_BUTTON: |
| 498 | case MAX77843_MUIC_ADC_REMOTE_S7_BUTTON: |
| 499 | case MAX77843_MUIC_ADC_REMOTE_S8_BUTTON: |
| 500 | case MAX77843_MUIC_ADC_REMOTE_S9_BUTTON: |
| 501 | case MAX77843_MUIC_ADC_REMOTE_S10_BUTTON: |
| 502 | case MAX77843_MUIC_ADC_REMOTE_S11_BUTTON: |
| 503 | case MAX77843_MUIC_ADC_REMOTE_S12_BUTTON: |
| 504 | case MAX77843_MUIC_ADC_RESERVED_ACC_1: |
| 505 | case MAX77843_MUIC_ADC_RESERVED_ACC_2: |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 506 | case MAX77843_MUIC_ADC_RESERVED_ACC_4: |
| 507 | case MAX77843_MUIC_ADC_RESERVED_ACC_5: |
| 508 | case MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE2: |
| 509 | case MAX77843_MUIC_ADC_PHONE_POWERED_DEV: |
| 510 | case MAX77843_MUIC_ADC_TTY_CONVERTER: |
| 511 | case MAX77843_MUIC_ADC_UART_CABLE: |
| 512 | case MAX77843_MUIC_ADC_CEA936A_TYPE1_CHG: |
| 513 | case MAX77843_MUIC_ADC_AV_CABLE_NOLOAD: |
| 514 | case MAX77843_MUIC_ADC_CEA936A_TYPE2_CHG: |
| 515 | case MAX77843_MUIC_ADC_FACTORY_MODE_UART_ON: |
| 516 | case MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE1: |
| 517 | case MAX77843_MUIC_ADC_OPEN: |
| 518 | dev_err(info->dev, |
| 519 | "accessory is %s but it isn't used (adc:0x%x)\n", |
| 520 | attached ? "attached" : "detached", cable_type); |
| 521 | return -EAGAIN; |
| 522 | default: |
| 523 | dev_err(info->dev, |
| 524 | "failed to detect %s accessory (adc:0x%x)\n", |
| 525 | attached ? "attached" : "detached", cable_type); |
| 526 | return -EINVAL; |
| 527 | } |
| 528 | |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | static int max77843_muic_chg_handler(struct max77843_muic_info *info) |
| 533 | { |
| 534 | int ret, chg_type, gnd_type; |
| 535 | bool attached; |
| 536 | |
| 537 | chg_type = max77843_muic_get_cable_type(info, |
| 538 | MAX77843_CABLE_GROUP_CHG, &attached); |
| 539 | |
| 540 | dev_dbg(info->dev, |
| 541 | "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n", |
| 542 | attached ? "attached" : "detached", |
| 543 | chg_type, info->prev_chg_type); |
| 544 | |
| 545 | switch (chg_type) { |
| 546 | case MAX77843_MUIC_CHG_USB: |
Krzysztof Kozlowski | 309a3e0 | 2015-07-15 21:59:53 +0900 | [diff] [blame] | 547 | ret = max77843_muic_set_path(info, |
| 548 | MAX77843_MUIC_CONTROL1_SW_USB, |
Marek Szyprowski | 4a4a8714 | 2017-10-18 11:56:22 +0200 | [diff] [blame] | 549 | attached, false); |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 550 | if (ret < 0) |
| 551 | return ret; |
| 552 | |
Chanwoo Choi | 8670b45 | 2016-08-16 15:55:34 +0900 | [diff] [blame] | 553 | extcon_set_state_sync(info->edev, EXTCON_USB, attached); |
| 554 | extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SDP, |
Chanwoo Choi | 8b45b6a | 2015-11-09 10:10:15 +0900 | [diff] [blame] | 555 | attached); |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 556 | break; |
| 557 | case MAX77843_MUIC_CHG_DOWNSTREAM: |
Krzysztof Kozlowski | 309a3e0 | 2015-07-15 21:59:53 +0900 | [diff] [blame] | 558 | ret = max77843_muic_set_path(info, |
| 559 | MAX77843_MUIC_CONTROL1_SW_OPEN, |
Marek Szyprowski | 4a4a8714 | 2017-10-18 11:56:22 +0200 | [diff] [blame] | 560 | attached, false); |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 561 | if (ret < 0) |
| 562 | return ret; |
| 563 | |
Chanwoo Choi | 8670b45 | 2016-08-16 15:55:34 +0900 | [diff] [blame] | 564 | extcon_set_state_sync(info->edev, EXTCON_CHG_USB_CDP, |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 565 | attached); |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 566 | break; |
| 567 | case MAX77843_MUIC_CHG_DEDICATED: |
Krzysztof Kozlowski | 309a3e0 | 2015-07-15 21:59:53 +0900 | [diff] [blame] | 568 | ret = max77843_muic_set_path(info, |
| 569 | MAX77843_MUIC_CONTROL1_SW_OPEN, |
Marek Szyprowski | 4a4a8714 | 2017-10-18 11:56:22 +0200 | [diff] [blame] | 570 | attached, false); |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 571 | if (ret < 0) |
| 572 | return ret; |
| 573 | |
Chanwoo Choi | 8670b45 | 2016-08-16 15:55:34 +0900 | [diff] [blame] | 574 | extcon_set_state_sync(info->edev, EXTCON_CHG_USB_DCP, |
Chanwoo Choi | 11eecf9 | 2015-10-03 14:15:13 +0900 | [diff] [blame] | 575 | attached); |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 576 | break; |
| 577 | case MAX77843_MUIC_CHG_SPECIAL_500MA: |
Krzysztof Kozlowski | 309a3e0 | 2015-07-15 21:59:53 +0900 | [diff] [blame] | 578 | ret = max77843_muic_set_path(info, |
| 579 | MAX77843_MUIC_CONTROL1_SW_OPEN, |
Marek Szyprowski | 4a4a8714 | 2017-10-18 11:56:22 +0200 | [diff] [blame] | 580 | attached, false); |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 581 | if (ret < 0) |
| 582 | return ret; |
| 583 | |
Chanwoo Choi | 8670b45 | 2016-08-16 15:55:34 +0900 | [diff] [blame] | 584 | extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SLOW, |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 585 | attached); |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 586 | break; |
| 587 | case MAX77843_MUIC_CHG_SPECIAL_1A: |
Krzysztof Kozlowski | 309a3e0 | 2015-07-15 21:59:53 +0900 | [diff] [blame] | 588 | ret = max77843_muic_set_path(info, |
| 589 | MAX77843_MUIC_CONTROL1_SW_OPEN, |
Marek Szyprowski | 4a4a8714 | 2017-10-18 11:56:22 +0200 | [diff] [blame] | 590 | attached, false); |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 591 | if (ret < 0) |
| 592 | return ret; |
| 593 | |
Chanwoo Choi | 8670b45 | 2016-08-16 15:55:34 +0900 | [diff] [blame] | 594 | extcon_set_state_sync(info->edev, EXTCON_CHG_USB_FAST, |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 595 | attached); |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 596 | break; |
| 597 | case MAX77843_MUIC_CHG_GND: |
| 598 | gnd_type = max77843_muic_get_cable_type(info, |
| 599 | MAX77843_CABLE_GROUP_ADC_GND, &attached); |
| 600 | |
| 601 | /* Charger cable on MHL accessory is attach or detach */ |
| 602 | if (gnd_type == MAX77843_MUIC_GND_MHL_VB) |
Chanwoo Choi | 8670b45 | 2016-08-16 15:55:34 +0900 | [diff] [blame] | 603 | extcon_set_state_sync(info->edev, EXTCON_CHG_USB_DCP, |
Chanwoo Choi | 11eecf9 | 2015-10-03 14:15:13 +0900 | [diff] [blame] | 604 | true); |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 605 | else if (gnd_type == MAX77843_MUIC_GND_MHL) |
Chanwoo Choi | 8670b45 | 2016-08-16 15:55:34 +0900 | [diff] [blame] | 606 | extcon_set_state_sync(info->edev, EXTCON_CHG_USB_DCP, |
Chanwoo Choi | 11eecf9 | 2015-10-03 14:15:13 +0900 | [diff] [blame] | 607 | false); |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 608 | break; |
Marek Szyprowski | 4a4a8714 | 2017-10-18 11:56:22 +0200 | [diff] [blame] | 609 | case MAX77843_MUIC_CHG_DOCK: |
| 610 | extcon_set_state_sync(info->edev, EXTCON_CHG_USB_DCP, attached); |
| 611 | break; |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 612 | case MAX77843_MUIC_CHG_NONE: |
| 613 | break; |
| 614 | default: |
| 615 | dev_err(info->dev, |
| 616 | "failed to detect %s accessory (chg_type:0x%x)\n", |
| 617 | attached ? "attached" : "detached", chg_type); |
| 618 | |
Krzysztof Kozlowski | 309a3e0 | 2015-07-15 21:59:53 +0900 | [diff] [blame] | 619 | max77843_muic_set_path(info, MAX77843_MUIC_CONTROL1_SW_OPEN, |
Marek Szyprowski | 4a4a8714 | 2017-10-18 11:56:22 +0200 | [diff] [blame] | 620 | attached, false); |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 621 | return -EINVAL; |
| 622 | } |
| 623 | |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | static void max77843_muic_irq_work(struct work_struct *work) |
| 628 | { |
| 629 | struct max77843_muic_info *info = container_of(work, |
| 630 | struct max77843_muic_info, irq_work); |
Krzysztof Kozlowski | bc1aadc | 2015-07-15 21:59:51 +0900 | [diff] [blame] | 631 | struct max77693_dev *max77843 = info->max77843; |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 632 | int ret = 0; |
| 633 | |
| 634 | mutex_lock(&info->mutex); |
| 635 | |
| 636 | ret = regmap_bulk_read(max77843->regmap_muic, |
| 637 | MAX77843_MUIC_REG_STATUS1, info->status, |
| 638 | MAX77843_MUIC_STATUS_NUM); |
| 639 | if (ret) { |
| 640 | dev_err(info->dev, "Cannot read STATUS registers\n"); |
| 641 | mutex_unlock(&info->mutex); |
| 642 | return; |
| 643 | } |
| 644 | |
| 645 | if (info->irq_adc) { |
| 646 | ret = max77843_muic_adc_handler(info); |
| 647 | if (ret) |
| 648 | dev_err(info->dev, "Unknown cable type\n"); |
| 649 | info->irq_adc = false; |
| 650 | } |
| 651 | |
| 652 | if (info->irq_chg) { |
| 653 | ret = max77843_muic_chg_handler(info); |
| 654 | if (ret) |
| 655 | dev_err(info->dev, "Unknown charger type\n"); |
| 656 | info->irq_chg = false; |
| 657 | } |
| 658 | |
| 659 | mutex_unlock(&info->mutex); |
| 660 | } |
| 661 | |
| 662 | static irqreturn_t max77843_muic_irq_handler(int irq, void *data) |
| 663 | { |
| 664 | struct max77843_muic_info *info = data; |
| 665 | int i, irq_type = -1; |
| 666 | |
| 667 | for (i = 0; i < ARRAY_SIZE(max77843_muic_irqs); i++) |
| 668 | if (irq == max77843_muic_irqs[i].virq) |
| 669 | irq_type = max77843_muic_irqs[i].irq; |
| 670 | |
| 671 | switch (irq_type) { |
| 672 | case MAX77843_MUIC_IRQ_INT1_ADC: |
| 673 | case MAX77843_MUIC_IRQ_INT1_ADCERROR: |
| 674 | case MAX77843_MUIC_IRQ_INT1_ADC1K: |
| 675 | info->irq_adc = true; |
| 676 | break; |
| 677 | case MAX77843_MUIC_IRQ_INT2_CHGTYP: |
| 678 | case MAX77843_MUIC_IRQ_INT2_CHGDETRUN: |
| 679 | case MAX77843_MUIC_IRQ_INT2_DCDTMR: |
| 680 | case MAX77843_MUIC_IRQ_INT2_DXOVP: |
| 681 | case MAX77843_MUIC_IRQ_INT2_VBVOLT: |
| 682 | info->irq_chg = true; |
| 683 | break; |
| 684 | case MAX77843_MUIC_IRQ_INT3_VBADC: |
| 685 | case MAX77843_MUIC_IRQ_INT3_VDNMON: |
| 686 | case MAX77843_MUIC_IRQ_INT3_DNRES: |
| 687 | case MAX77843_MUIC_IRQ_INT3_MPNACK: |
| 688 | case MAX77843_MUIC_IRQ_INT3_MRXBUFOW: |
| 689 | case MAX77843_MUIC_IRQ_INT3_MRXTRF: |
| 690 | case MAX77843_MUIC_IRQ_INT3_MRXPERR: |
| 691 | case MAX77843_MUIC_IRQ_INT3_MRXRDY: |
| 692 | break; |
| 693 | default: |
| 694 | dev_err(info->dev, "Cannot recognize IRQ(%d)\n", irq_type); |
| 695 | break; |
| 696 | } |
| 697 | |
| 698 | schedule_work(&info->irq_work); |
| 699 | |
| 700 | return IRQ_HANDLED; |
| 701 | } |
| 702 | |
| 703 | static void max77843_muic_detect_cable_wq(struct work_struct *work) |
| 704 | { |
| 705 | struct max77843_muic_info *info = container_of(to_delayed_work(work), |
| 706 | struct max77843_muic_info, wq_detcable); |
Krzysztof Kozlowski | bc1aadc | 2015-07-15 21:59:51 +0900 | [diff] [blame] | 707 | struct max77693_dev *max77843 = info->max77843; |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 708 | int chg_type, adc, ret; |
| 709 | bool attached; |
| 710 | |
| 711 | mutex_lock(&info->mutex); |
| 712 | |
| 713 | ret = regmap_bulk_read(max77843->regmap_muic, |
| 714 | MAX77843_MUIC_REG_STATUS1, info->status, |
| 715 | MAX77843_MUIC_STATUS_NUM); |
| 716 | if (ret) { |
| 717 | dev_err(info->dev, "Cannot read STATUS registers\n"); |
| 718 | goto err_cable_wq; |
| 719 | } |
| 720 | |
| 721 | adc = max77843_muic_get_cable_type(info, |
| 722 | MAX77843_CABLE_GROUP_ADC, &attached); |
| 723 | if (attached && adc != MAX77843_MUIC_ADC_OPEN) { |
| 724 | ret = max77843_muic_adc_handler(info); |
| 725 | if (ret < 0) { |
| 726 | dev_err(info->dev, "Cannot detect accessory\n"); |
| 727 | goto err_cable_wq; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | chg_type = max77843_muic_get_cable_type(info, |
| 732 | MAX77843_CABLE_GROUP_CHG, &attached); |
| 733 | if (attached && chg_type != MAX77843_MUIC_CHG_NONE) { |
| 734 | ret = max77843_muic_chg_handler(info); |
| 735 | if (ret < 0) { |
| 736 | dev_err(info->dev, "Cannot detect charger accessory\n"); |
| 737 | goto err_cable_wq; |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | err_cable_wq: |
| 742 | mutex_unlock(&info->mutex); |
| 743 | } |
| 744 | |
| 745 | static int max77843_muic_set_debounce_time(struct max77843_muic_info *info, |
| 746 | enum max77843_muic_adc_debounce_time time) |
| 747 | { |
Krzysztof Kozlowski | bc1aadc | 2015-07-15 21:59:51 +0900 | [diff] [blame] | 748 | struct max77693_dev *max77843 = info->max77843; |
Dan Carpenter | d927c59 | 2015-03-15 13:49:47 +0300 | [diff] [blame] | 749 | int ret; |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 750 | |
| 751 | switch (time) { |
| 752 | case MAX77843_DEBOUNCE_TIME_5MS: |
| 753 | case MAX77843_DEBOUNCE_TIME_10MS: |
| 754 | case MAX77843_DEBOUNCE_TIME_25MS: |
| 755 | case MAX77843_DEBOUNCE_TIME_38_62MS: |
| 756 | ret = regmap_update_bits(max77843->regmap_muic, |
| 757 | MAX77843_MUIC_REG_CONTROL4, |
| 758 | MAX77843_MUIC_CONTROL4_ADCDBSET_MASK, |
Krzysztof Kozlowski | 309a3e0 | 2015-07-15 21:59:53 +0900 | [diff] [blame] | 759 | time << MAX77843_MUIC_CONTROL4_ADCDBSET_SHIFT); |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 760 | if (ret < 0) { |
| 761 | dev_err(info->dev, "Cannot write MUIC regmap\n"); |
| 762 | return ret; |
| 763 | } |
| 764 | break; |
| 765 | default: |
| 766 | dev_err(info->dev, "Invalid ADC debounce time\n"); |
| 767 | return -EINVAL; |
| 768 | } |
| 769 | |
| 770 | return 0; |
| 771 | } |
| 772 | |
Krzysztof Kozlowski | bc1aadc | 2015-07-15 21:59:51 +0900 | [diff] [blame] | 773 | static int max77843_init_muic_regmap(struct max77693_dev *max77843) |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 774 | { |
| 775 | int ret; |
| 776 | |
Wolfram Sang | a6d9cfc | 2019-07-22 19:26:01 +0200 | [diff] [blame] | 777 | max77843->i2c_muic = i2c_new_dummy_device(max77843->i2c->adapter, |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 778 | I2C_ADDR_MUIC); |
Wolfram Sang | a6d9cfc | 2019-07-22 19:26:01 +0200 | [diff] [blame] | 779 | if (IS_ERR(max77843->i2c_muic)) { |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 780 | dev_err(&max77843->i2c->dev, |
| 781 | "Cannot allocate I2C device for MUIC\n"); |
Wolfram Sang | a6d9cfc | 2019-07-22 19:26:01 +0200 | [diff] [blame] | 782 | return PTR_ERR(max77843->i2c_muic); |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | i2c_set_clientdata(max77843->i2c_muic, max77843); |
| 786 | |
| 787 | max77843->regmap_muic = devm_regmap_init_i2c(max77843->i2c_muic, |
| 788 | &max77843_muic_regmap_config); |
| 789 | if (IS_ERR(max77843->regmap_muic)) { |
| 790 | ret = PTR_ERR(max77843->regmap_muic); |
| 791 | goto err_muic_i2c; |
| 792 | } |
| 793 | |
| 794 | ret = regmap_add_irq_chip(max77843->regmap_muic, max77843->irq, |
| 795 | IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED, |
| 796 | 0, &max77843_muic_irq_chip, &max77843->irq_data_muic); |
| 797 | if (ret < 0) { |
| 798 | dev_err(&max77843->i2c->dev, "Cannot add MUIC IRQ chip\n"); |
| 799 | goto err_muic_i2c; |
| 800 | } |
| 801 | |
| 802 | return 0; |
| 803 | |
| 804 | err_muic_i2c: |
| 805 | i2c_unregister_device(max77843->i2c_muic); |
| 806 | |
| 807 | return ret; |
| 808 | } |
| 809 | |
| 810 | static int max77843_muic_probe(struct platform_device *pdev) |
| 811 | { |
Krzysztof Kozlowski | bc1aadc | 2015-07-15 21:59:51 +0900 | [diff] [blame] | 812 | struct max77693_dev *max77843 = dev_get_drvdata(pdev->dev.parent); |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 813 | struct max77843_muic_info *info; |
| 814 | unsigned int id; |
Marek Szyprowski | d9204ac | 2018-11-08 14:45:45 +0100 | [diff] [blame] | 815 | int cable_type; |
| 816 | bool attached; |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 817 | int i, ret; |
| 818 | |
| 819 | info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); |
| 820 | if (!info) |
| 821 | return -ENOMEM; |
| 822 | |
| 823 | info->dev = &pdev->dev; |
| 824 | info->max77843 = max77843; |
| 825 | |
| 826 | platform_set_drvdata(pdev, info); |
| 827 | mutex_init(&info->mutex); |
| 828 | |
| 829 | /* Initialize i2c and regmap */ |
| 830 | ret = max77843_init_muic_regmap(max77843); |
| 831 | if (ret) { |
| 832 | dev_err(&pdev->dev, "Failed to init MUIC regmap\n"); |
| 833 | return ret; |
| 834 | } |
| 835 | |
| 836 | /* Turn off auto detection configuration */ |
| 837 | ret = regmap_update_bits(max77843->regmap_muic, |
| 838 | MAX77843_MUIC_REG_CONTROL4, |
| 839 | MAX77843_MUIC_CONTROL4_USBAUTO_MASK | |
| 840 | MAX77843_MUIC_CONTROL4_FCTAUTO_MASK, |
| 841 | CONTROL4_AUTO_DISABLE); |
| 842 | |
| 843 | /* Initialize extcon device */ |
| 844 | info->edev = devm_extcon_dev_allocate(&pdev->dev, |
| 845 | max77843_extcon_cable); |
| 846 | if (IS_ERR(info->edev)) { |
| 847 | dev_err(&pdev->dev, "Failed to allocate memory for extcon\n"); |
| 848 | ret = -ENODEV; |
| 849 | goto err_muic_irq; |
| 850 | } |
| 851 | |
| 852 | ret = devm_extcon_dev_register(&pdev->dev, info->edev); |
| 853 | if (ret) { |
| 854 | dev_err(&pdev->dev, "Failed to register extcon device\n"); |
| 855 | goto err_muic_irq; |
| 856 | } |
| 857 | |
| 858 | /* Set ADC debounce time */ |
| 859 | max77843_muic_set_debounce_time(info, MAX77843_DEBOUNCE_TIME_25MS); |
| 860 | |
Marek Szyprowski | d9204ac | 2018-11-08 14:45:45 +0100 | [diff] [blame] | 861 | /* Set initial path for UART when JIG is connected to get serial logs */ |
| 862 | ret = regmap_bulk_read(max77843->regmap_muic, |
| 863 | MAX77843_MUIC_REG_STATUS1, info->status, |
| 864 | MAX77843_MUIC_STATUS_NUM); |
| 865 | if (ret) { |
| 866 | dev_err(info->dev, "Cannot read STATUS registers\n"); |
| 867 | goto err_muic_irq; |
| 868 | } |
| 869 | cable_type = max77843_muic_get_cable_type(info, MAX77843_CABLE_GROUP_ADC, |
| 870 | &attached); |
| 871 | if (attached && cable_type == MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF) |
| 872 | max77843_muic_set_path(info, MAX77843_MUIC_CONTROL1_SW_UART, |
| 873 | true, false); |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 874 | |
| 875 | /* Check revision number of MUIC device */ |
| 876 | ret = regmap_read(max77843->regmap_muic, MAX77843_MUIC_REG_ID, &id); |
| 877 | if (ret < 0) { |
| 878 | dev_err(&pdev->dev, "Failed to read revision number\n"); |
| 879 | goto err_muic_irq; |
| 880 | } |
| 881 | dev_info(info->dev, "MUIC device ID : 0x%x\n", id); |
| 882 | |
| 883 | /* Support virtual irq domain for max77843 MUIC device */ |
| 884 | INIT_WORK(&info->irq_work, max77843_muic_irq_work); |
| 885 | |
Jaewon Kim | 135d9f7 | 2015-06-05 13:32:27 +0900 | [diff] [blame] | 886 | /* Clear IRQ bits before request IRQs */ |
| 887 | ret = regmap_bulk_read(max77843->regmap_muic, |
| 888 | MAX77843_MUIC_REG_INT1, info->status, |
Dan Carpenter | c4924e9 | 2016-02-04 14:36:09 +0300 | [diff] [blame] | 889 | MAX77843_MUIC_STATUS_NUM); |
Jaewon Kim | 135d9f7 | 2015-06-05 13:32:27 +0900 | [diff] [blame] | 890 | if (ret) { |
| 891 | dev_err(&pdev->dev, "Failed to Clear IRQ bits\n"); |
| 892 | goto err_muic_irq; |
| 893 | } |
| 894 | |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 895 | for (i = 0; i < ARRAY_SIZE(max77843_muic_irqs); i++) { |
| 896 | struct max77843_muic_irq *muic_irq = &max77843_muic_irqs[i]; |
Andrzej Hajda | c05c0d5 | 2015-12-14 11:06:03 +0100 | [diff] [blame] | 897 | int virq = 0; |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 898 | |
| 899 | virq = regmap_irq_get_virq(max77843->irq_data_muic, |
| 900 | muic_irq->irq); |
| 901 | if (virq <= 0) { |
| 902 | ret = -EINVAL; |
| 903 | goto err_muic_irq; |
| 904 | } |
| 905 | muic_irq->virq = virq; |
| 906 | |
| 907 | ret = devm_request_threaded_irq(&pdev->dev, virq, NULL, |
| 908 | max77843_muic_irq_handler, IRQF_NO_SUSPEND, |
| 909 | muic_irq->name, info); |
| 910 | if (ret) { |
| 911 | dev_err(&pdev->dev, |
| 912 | "Failed to request irq (IRQ: %d, error: %d)\n", |
| 913 | muic_irq->irq, ret); |
| 914 | goto err_muic_irq; |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | /* Detect accessory after completing the initialization of platform */ |
| 919 | INIT_DELAYED_WORK(&info->wq_detcable, max77843_muic_detect_cable_wq); |
| 920 | queue_delayed_work(system_power_efficient_wq, |
| 921 | &info->wq_detcable, msecs_to_jiffies(DELAY_MS_DEFAULT)); |
| 922 | |
| 923 | return 0; |
| 924 | |
| 925 | err_muic_irq: |
| 926 | regmap_del_irq_chip(max77843->irq, max77843->irq_data_muic); |
| 927 | i2c_unregister_device(max77843->i2c_muic); |
| 928 | |
| 929 | return ret; |
| 930 | } |
| 931 | |
| 932 | static int max77843_muic_remove(struct platform_device *pdev) |
| 933 | { |
| 934 | struct max77843_muic_info *info = platform_get_drvdata(pdev); |
Krzysztof Kozlowski | bc1aadc | 2015-07-15 21:59:51 +0900 | [diff] [blame] | 935 | struct max77693_dev *max77843 = info->max77843; |
Jaewon Kim | 27a28d3 | 2015-02-04 13:56:07 +0900 | [diff] [blame] | 936 | |
| 937 | cancel_work_sync(&info->irq_work); |
| 938 | regmap_del_irq_chip(max77843->irq, max77843->irq_data_muic); |
| 939 | i2c_unregister_device(max77843->i2c_muic); |
| 940 | |
| 941 | return 0; |
| 942 | } |
| 943 | |
| 944 | static const struct platform_device_id max77843_muic_id[] = { |
| 945 | { "max77843-muic", }, |
| 946 | { /* sentinel */ }, |
| 947 | }; |
| 948 | MODULE_DEVICE_TABLE(platform, max77843_muic_id); |
| 949 | |
| 950 | static struct platform_driver max77843_muic_driver = { |
| 951 | .driver = { |
| 952 | .name = "max77843-muic", |
| 953 | }, |
| 954 | .probe = max77843_muic_probe, |
| 955 | .remove = max77843_muic_remove, |
| 956 | .id_table = max77843_muic_id, |
| 957 | }; |
| 958 | |
| 959 | static int __init max77843_muic_init(void) |
| 960 | { |
| 961 | return platform_driver_register(&max77843_muic_driver); |
| 962 | } |
| 963 | subsys_initcall(max77843_muic_init); |
| 964 | |
| 965 | MODULE_DESCRIPTION("Maxim MAX77843 Extcon driver"); |
| 966 | MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>"); |
| 967 | MODULE_LICENSE("GPL"); |