Chanwoo Choi | 962e56b | 2013-11-22 16:51:06 +0100 | [diff] [blame] | 1 | /* |
Krzysztof Kozlowski | 4706a52 | 2014-04-14 11:17:19 +0200 | [diff] [blame^] | 2 | * extcon-max14577.c - MAX14577/77836 extcon driver to support MUIC |
Chanwoo Choi | 962e56b | 2013-11-22 16:51:06 +0100 | [diff] [blame] | 3 | * |
Krzysztof Kozlowski | 4706a52 | 2014-04-14 11:17:19 +0200 | [diff] [blame^] | 4 | * Copyright (C) 2013,2014 Samsung Electrnoics |
Chanwoo Choi | 962e56b | 2013-11-22 16:51:06 +0100 | [diff] [blame] | 5 | * Chanwoo Choi <cw00.choi@samsung.com> |
Krzysztof Kozlowski | 4706a52 | 2014-04-14 11:17:19 +0200 | [diff] [blame^] | 6 | * Krzysztof Kozlowski <k.kozlowski@samsung.com> |
Chanwoo Choi | 962e56b | 2013-11-22 16:51:06 +0100 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/i2c.h> |
| 22 | #include <linux/interrupt.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/mfd/max14577.h> |
| 25 | #include <linux/mfd/max14577-private.h> |
| 26 | #include <linux/extcon.h> |
| 27 | |
Chanwoo Choi | 962e56b | 2013-11-22 16:51:06 +0100 | [diff] [blame] | 28 | #define DELAY_MS_DEFAULT 17000 /* unit: millisecond */ |
| 29 | |
| 30 | enum max14577_muic_adc_debounce_time { |
| 31 | ADC_DEBOUNCE_TIME_5MS = 0, |
| 32 | ADC_DEBOUNCE_TIME_10MS, |
| 33 | ADC_DEBOUNCE_TIME_25MS, |
| 34 | ADC_DEBOUNCE_TIME_38_62MS, |
| 35 | }; |
| 36 | |
| 37 | enum max14577_muic_status { |
| 38 | MAX14577_MUIC_STATUS1 = 0, |
| 39 | MAX14577_MUIC_STATUS2 = 1, |
| 40 | MAX14577_MUIC_STATUS_END, |
| 41 | }; |
| 42 | |
Krzysztof Kozlowski | 0ca852b | 2014-04-14 11:17:16 +0200 | [diff] [blame] | 43 | /** |
| 44 | * struct max14577_muic_irq |
| 45 | * @irq: the index of irq list of MUIC device. |
| 46 | * @name: the name of irq. |
| 47 | * @virq: the virtual irq to use irq domain |
| 48 | */ |
| 49 | struct max14577_muic_irq { |
| 50 | unsigned int irq; |
| 51 | const char *name; |
| 52 | unsigned int virq; |
| 53 | }; |
| 54 | |
| 55 | static struct max14577_muic_irq max14577_muic_irqs[] = { |
| 56 | { MAX14577_IRQ_INT1_ADC, "muic-ADC" }, |
| 57 | { MAX14577_IRQ_INT1_ADCLOW, "muic-ADCLOW" }, |
| 58 | { MAX14577_IRQ_INT1_ADCERR, "muic-ADCError" }, |
| 59 | { MAX14577_IRQ_INT2_CHGTYP, "muic-CHGTYP" }, |
| 60 | { MAX14577_IRQ_INT2_CHGDETRUN, "muic-CHGDETRUN" }, |
| 61 | { MAX14577_IRQ_INT2_DCDTMR, "muic-DCDTMR" }, |
| 62 | { MAX14577_IRQ_INT2_DBCHG, "muic-DBCHG" }, |
| 63 | { MAX14577_IRQ_INT2_VBVOLT, "muic-VBVOLT" }, |
| 64 | }; |
| 65 | |
Krzysztof Kozlowski | 4706a52 | 2014-04-14 11:17:19 +0200 | [diff] [blame^] | 66 | static struct max14577_muic_irq max77836_muic_irqs[] = { |
| 67 | { MAX14577_IRQ_INT1_ADC, "muic-ADC" }, |
| 68 | { MAX14577_IRQ_INT1_ADCLOW, "muic-ADCLOW" }, |
| 69 | { MAX14577_IRQ_INT1_ADCERR, "muic-ADCError" }, |
| 70 | { MAX77836_IRQ_INT1_ADC1K, "muic-ADC1K" }, |
| 71 | { MAX14577_IRQ_INT2_CHGTYP, "muic-CHGTYP" }, |
| 72 | { MAX14577_IRQ_INT2_CHGDETRUN, "muic-CHGDETRUN" }, |
| 73 | { MAX14577_IRQ_INT2_DCDTMR, "muic-DCDTMR" }, |
| 74 | { MAX14577_IRQ_INT2_DBCHG, "muic-DBCHG" }, |
| 75 | { MAX14577_IRQ_INT2_VBVOLT, "muic-VBVOLT" }, |
| 76 | { MAX77836_IRQ_INT2_VIDRM, "muic-VIDRM" }, |
| 77 | }; |
| 78 | |
Chanwoo Choi | 962e56b | 2013-11-22 16:51:06 +0100 | [diff] [blame] | 79 | struct max14577_muic_info { |
| 80 | struct device *dev; |
| 81 | struct max14577 *max14577; |
| 82 | struct extcon_dev *edev; |
| 83 | int prev_cable_type; |
| 84 | int prev_chg_type; |
| 85 | u8 status[MAX14577_MUIC_STATUS_END]; |
| 86 | |
Krzysztof Kozlowski | 0ca852b | 2014-04-14 11:17:16 +0200 | [diff] [blame] | 87 | struct max14577_muic_irq *muic_irqs; |
| 88 | unsigned int muic_irqs_num; |
Chanwoo Choi | 962e56b | 2013-11-22 16:51:06 +0100 | [diff] [blame] | 89 | bool irq_adc; |
| 90 | bool irq_chg; |
| 91 | struct work_struct irq_work; |
| 92 | struct mutex mutex; |
| 93 | |
| 94 | /* |
| 95 | * Use delayed workqueue to detect cable state and then |
| 96 | * notify cable state to notifiee/platform through uevent. |
| 97 | * After completing the booting of platform, the extcon provider |
| 98 | * driver should notify cable state to upper layer. |
| 99 | */ |
| 100 | struct delayed_work wq_detcable; |
| 101 | |
| 102 | /* |
| 103 | * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB |
| 104 | * h/w path of COMP2/COMN1 on CONTROL1 register. |
| 105 | */ |
| 106 | int path_usb; |
| 107 | int path_uart; |
| 108 | }; |
| 109 | |
| 110 | enum max14577_muic_cable_group { |
| 111 | MAX14577_CABLE_GROUP_ADC = 0, |
| 112 | MAX14577_CABLE_GROUP_CHG, |
| 113 | }; |
| 114 | |
Chanwoo Choi | 962e56b | 2013-11-22 16:51:06 +0100 | [diff] [blame] | 115 | /* Define supported accessory type */ |
| 116 | enum max14577_muic_acc_type { |
| 117 | MAX14577_MUIC_ADC_GROUND = 0x0, |
| 118 | MAX14577_MUIC_ADC_SEND_END_BUTTON, |
| 119 | MAX14577_MUIC_ADC_REMOTE_S1_BUTTON, |
| 120 | MAX14577_MUIC_ADC_REMOTE_S2_BUTTON, |
| 121 | MAX14577_MUIC_ADC_REMOTE_S3_BUTTON, |
| 122 | MAX14577_MUIC_ADC_REMOTE_S4_BUTTON, |
| 123 | MAX14577_MUIC_ADC_REMOTE_S5_BUTTON, |
| 124 | MAX14577_MUIC_ADC_REMOTE_S6_BUTTON, |
| 125 | MAX14577_MUIC_ADC_REMOTE_S7_BUTTON, |
| 126 | MAX14577_MUIC_ADC_REMOTE_S8_BUTTON, |
| 127 | MAX14577_MUIC_ADC_REMOTE_S9_BUTTON, |
| 128 | MAX14577_MUIC_ADC_REMOTE_S10_BUTTON, |
| 129 | MAX14577_MUIC_ADC_REMOTE_S11_BUTTON, |
| 130 | MAX14577_MUIC_ADC_REMOTE_S12_BUTTON, |
| 131 | MAX14577_MUIC_ADC_RESERVED_ACC_1, |
| 132 | MAX14577_MUIC_ADC_RESERVED_ACC_2, |
| 133 | MAX14577_MUIC_ADC_RESERVED_ACC_3, |
| 134 | MAX14577_MUIC_ADC_RESERVED_ACC_4, |
| 135 | MAX14577_MUIC_ADC_RESERVED_ACC_5, |
| 136 | MAX14577_MUIC_ADC_AUDIO_DEVICE_TYPE2, |
| 137 | MAX14577_MUIC_ADC_PHONE_POWERED_DEV, |
| 138 | MAX14577_MUIC_ADC_TTY_CONVERTER, |
| 139 | MAX14577_MUIC_ADC_UART_CABLE, |
| 140 | MAX14577_MUIC_ADC_CEA936A_TYPE1_CHG, |
| 141 | MAX14577_MUIC_ADC_FACTORY_MODE_USB_OFF, |
| 142 | MAX14577_MUIC_ADC_FACTORY_MODE_USB_ON, |
| 143 | MAX14577_MUIC_ADC_AV_CABLE_NOLOAD, |
| 144 | MAX14577_MUIC_ADC_CEA936A_TYPE2_CHG, |
| 145 | MAX14577_MUIC_ADC_FACTORY_MODE_UART_OFF, |
| 146 | MAX14577_MUIC_ADC_FACTORY_MODE_UART_ON, |
| 147 | MAX14577_MUIC_ADC_AUDIO_DEVICE_TYPE1, /* with Remote and Simple Ctrl */ |
| 148 | MAX14577_MUIC_ADC_OPEN, |
| 149 | }; |
| 150 | |
| 151 | /* max14577 MUIC device support below list of accessories(external connector) */ |
| 152 | enum { |
| 153 | EXTCON_CABLE_USB = 0, |
| 154 | EXTCON_CABLE_TA, |
| 155 | EXTCON_CABLE_FAST_CHARGER, |
| 156 | EXTCON_CABLE_SLOW_CHARGER, |
| 157 | EXTCON_CABLE_CHARGE_DOWNSTREAM, |
| 158 | EXTCON_CABLE_JIG_USB_ON, |
| 159 | EXTCON_CABLE_JIG_USB_OFF, |
| 160 | EXTCON_CABLE_JIG_UART_OFF, |
| 161 | EXTCON_CABLE_JIG_UART_ON, |
| 162 | |
| 163 | _EXTCON_CABLE_NUM, |
| 164 | }; |
| 165 | |
| 166 | static const char *max14577_extcon_cable[] = { |
| 167 | [EXTCON_CABLE_USB] = "USB", |
| 168 | [EXTCON_CABLE_TA] = "TA", |
| 169 | [EXTCON_CABLE_FAST_CHARGER] = "Fast-charger", |
| 170 | [EXTCON_CABLE_SLOW_CHARGER] = "Slow-charger", |
| 171 | [EXTCON_CABLE_CHARGE_DOWNSTREAM] = "Charge-downstream", |
| 172 | [EXTCON_CABLE_JIG_USB_ON] = "JIG-USB-ON", |
| 173 | [EXTCON_CABLE_JIG_USB_OFF] = "JIG-USB-OFF", |
| 174 | [EXTCON_CABLE_JIG_UART_OFF] = "JIG-UART-OFF", |
| 175 | [EXTCON_CABLE_JIG_UART_ON] = "JIG-UART-ON", |
| 176 | |
| 177 | NULL, |
| 178 | }; |
| 179 | |
| 180 | /* |
| 181 | * max14577_muic_set_debounce_time - Set the debounce time of ADC |
| 182 | * @info: the instance including private data of max14577 MUIC |
| 183 | * @time: the debounce time of ADC |
| 184 | */ |
| 185 | static int max14577_muic_set_debounce_time(struct max14577_muic_info *info, |
| 186 | enum max14577_muic_adc_debounce_time time) |
| 187 | { |
| 188 | u8 ret; |
| 189 | |
| 190 | switch (time) { |
| 191 | case ADC_DEBOUNCE_TIME_5MS: |
| 192 | case ADC_DEBOUNCE_TIME_10MS: |
| 193 | case ADC_DEBOUNCE_TIME_25MS: |
| 194 | case ADC_DEBOUNCE_TIME_38_62MS: |
| 195 | ret = max14577_update_reg(info->max14577->regmap, |
| 196 | MAX14577_MUIC_REG_CONTROL3, |
| 197 | CTRL3_ADCDBSET_MASK, |
| 198 | time << CTRL3_ADCDBSET_SHIFT); |
| 199 | if (ret) { |
| 200 | dev_err(info->dev, "failed to set ADC debounce time\n"); |
| 201 | return ret; |
| 202 | } |
| 203 | break; |
| 204 | default: |
| 205 | dev_err(info->dev, "invalid ADC debounce time\n"); |
| 206 | return -EINVAL; |
| 207 | } |
| 208 | |
| 209 | return 0; |
| 210 | }; |
| 211 | |
| 212 | /* |
| 213 | * max14577_muic_set_path - Set hardware line according to attached cable |
| 214 | * @info: the instance including private data of max14577 MUIC |
| 215 | * @value: the path according to attached cable |
| 216 | * @attached: the state of cable (true:attached, false:detached) |
| 217 | * |
| 218 | * The max14577 MUIC device share outside H/W line among a varity of cables |
| 219 | * so, this function set internal path of H/W line according to the type of |
| 220 | * attached cable. |
| 221 | */ |
| 222 | static int max14577_muic_set_path(struct max14577_muic_info *info, |
| 223 | u8 val, bool attached) |
| 224 | { |
| 225 | int ret = 0; |
| 226 | u8 ctrl1, ctrl2 = 0; |
| 227 | |
| 228 | /* Set open state to path before changing hw path */ |
| 229 | ret = max14577_update_reg(info->max14577->regmap, |
| 230 | MAX14577_MUIC_REG_CONTROL1, |
| 231 | CLEAR_IDBEN_MICEN_MASK, CTRL1_SW_OPEN); |
| 232 | if (ret < 0) { |
| 233 | dev_err(info->dev, "failed to update MUIC register\n"); |
| 234 | return ret; |
| 235 | } |
| 236 | |
| 237 | if (attached) |
| 238 | ctrl1 = val; |
| 239 | else |
| 240 | ctrl1 = CTRL1_SW_OPEN; |
| 241 | |
| 242 | ret = max14577_update_reg(info->max14577->regmap, |
| 243 | MAX14577_MUIC_REG_CONTROL1, |
| 244 | CLEAR_IDBEN_MICEN_MASK, ctrl1); |
| 245 | if (ret < 0) { |
| 246 | dev_err(info->dev, "failed to update MUIC register\n"); |
| 247 | return ret; |
| 248 | } |
| 249 | |
| 250 | if (attached) |
| 251 | ctrl2 |= CTRL2_CPEN_MASK; /* LowPwr=0, CPEn=1 */ |
| 252 | else |
| 253 | ctrl2 |= CTRL2_LOWPWR_MASK; /* LowPwr=1, CPEn=0 */ |
| 254 | |
| 255 | ret = max14577_update_reg(info->max14577->regmap, |
| 256 | MAX14577_REG_CONTROL2, |
| 257 | CTRL2_LOWPWR_MASK | CTRL2_CPEN_MASK, ctrl2); |
| 258 | if (ret < 0) { |
| 259 | dev_err(info->dev, "failed to update MUIC register\n"); |
| 260 | return ret; |
| 261 | } |
| 262 | |
| 263 | dev_dbg(info->dev, |
| 264 | "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n", |
| 265 | ctrl1, ctrl2, attached ? "attached" : "detached"); |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | /* |
| 271 | * max14577_muic_get_cable_type - Return cable type and check cable state |
| 272 | * @info: the instance including private data of max14577 MUIC |
| 273 | * @group: the path according to attached cable |
| 274 | * @attached: store cable state and return |
| 275 | * |
| 276 | * This function check the cable state either attached or detached, |
| 277 | * and then divide precise type of cable according to cable group. |
| 278 | * - max14577_CABLE_GROUP_ADC |
| 279 | * - max14577_CABLE_GROUP_CHG |
| 280 | */ |
| 281 | static int max14577_muic_get_cable_type(struct max14577_muic_info *info, |
| 282 | enum max14577_muic_cable_group group, bool *attached) |
| 283 | { |
| 284 | int cable_type = 0; |
| 285 | int adc; |
| 286 | int chg_type; |
| 287 | |
| 288 | switch (group) { |
| 289 | case MAX14577_CABLE_GROUP_ADC: |
| 290 | /* |
| 291 | * Read ADC value to check cable type and decide cable state |
| 292 | * according to cable type |
| 293 | */ |
| 294 | adc = info->status[MAX14577_MUIC_STATUS1] & STATUS1_ADC_MASK; |
| 295 | adc >>= STATUS1_ADC_SHIFT; |
| 296 | |
| 297 | /* |
| 298 | * Check current cable state/cable type and store cable type |
| 299 | * (info->prev_cable_type) for handling cable when cable is |
| 300 | * detached. |
| 301 | */ |
| 302 | if (adc == MAX14577_MUIC_ADC_OPEN) { |
| 303 | *attached = false; |
| 304 | |
| 305 | cable_type = info->prev_cable_type; |
| 306 | info->prev_cable_type = MAX14577_MUIC_ADC_OPEN; |
| 307 | } else { |
| 308 | *attached = true; |
| 309 | |
| 310 | cable_type = info->prev_cable_type = adc; |
| 311 | } |
| 312 | break; |
| 313 | case MAX14577_CABLE_GROUP_CHG: |
| 314 | /* |
| 315 | * Read charger type to check cable type and decide cable state |
| 316 | * according to type of charger cable. |
| 317 | */ |
| 318 | chg_type = info->status[MAX14577_MUIC_STATUS2] & |
| 319 | STATUS2_CHGTYP_MASK; |
| 320 | chg_type >>= STATUS2_CHGTYP_SHIFT; |
| 321 | |
| 322 | if (chg_type == MAX14577_CHARGER_TYPE_NONE) { |
| 323 | *attached = false; |
| 324 | |
| 325 | cable_type = info->prev_chg_type; |
| 326 | info->prev_chg_type = MAX14577_CHARGER_TYPE_NONE; |
| 327 | } else { |
| 328 | *attached = true; |
| 329 | |
| 330 | /* |
| 331 | * Check current cable state/cable type and store cable |
| 332 | * type(info->prev_chg_type) for handling cable when |
| 333 | * charger cable is detached. |
| 334 | */ |
| 335 | cable_type = info->prev_chg_type = chg_type; |
| 336 | } |
| 337 | |
| 338 | break; |
| 339 | default: |
| 340 | dev_err(info->dev, "Unknown cable group (%d)\n", group); |
| 341 | cable_type = -EINVAL; |
| 342 | break; |
| 343 | } |
| 344 | |
| 345 | return cable_type; |
| 346 | } |
| 347 | |
| 348 | static int max14577_muic_jig_handler(struct max14577_muic_info *info, |
| 349 | int cable_type, bool attached) |
| 350 | { |
| 351 | char cable_name[32]; |
| 352 | int ret = 0; |
| 353 | u8 path = CTRL1_SW_OPEN; |
| 354 | |
| 355 | dev_dbg(info->dev, |
| 356 | "external connector is %s (adc:0x%02x)\n", |
| 357 | attached ? "attached" : "detached", cable_type); |
| 358 | |
| 359 | switch (cable_type) { |
| 360 | case MAX14577_MUIC_ADC_FACTORY_MODE_USB_OFF: /* ADC_JIG_USB_OFF */ |
| 361 | /* PATH:AP_USB */ |
| 362 | strcpy(cable_name, "JIG-USB-OFF"); |
| 363 | path = CTRL1_SW_USB; |
| 364 | break; |
| 365 | case MAX14577_MUIC_ADC_FACTORY_MODE_USB_ON: /* ADC_JIG_USB_ON */ |
| 366 | /* PATH:AP_USB */ |
| 367 | strcpy(cable_name, "JIG-USB-ON"); |
| 368 | path = CTRL1_SW_USB; |
| 369 | break; |
| 370 | case MAX14577_MUIC_ADC_FACTORY_MODE_UART_OFF: /* ADC_JIG_UART_OFF */ |
| 371 | /* PATH:AP_UART */ |
| 372 | strcpy(cable_name, "JIG-UART-OFF"); |
| 373 | path = CTRL1_SW_UART; |
| 374 | break; |
| 375 | default: |
| 376 | dev_err(info->dev, "failed to detect %s jig cable\n", |
| 377 | attached ? "attached" : "detached"); |
| 378 | return -EINVAL; |
| 379 | } |
| 380 | |
| 381 | ret = max14577_muic_set_path(info, path, attached); |
| 382 | if (ret < 0) |
| 383 | return ret; |
| 384 | |
| 385 | extcon_set_cable_state(info->edev, cable_name, attached); |
| 386 | |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | static int max14577_muic_adc_handler(struct max14577_muic_info *info) |
| 391 | { |
| 392 | int cable_type; |
| 393 | bool attached; |
| 394 | int ret = 0; |
| 395 | |
| 396 | /* Check accessory state which is either detached or attached */ |
| 397 | cable_type = max14577_muic_get_cable_type(info, |
| 398 | MAX14577_CABLE_GROUP_ADC, &attached); |
| 399 | |
| 400 | dev_dbg(info->dev, |
| 401 | "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n", |
| 402 | attached ? "attached" : "detached", cable_type, |
| 403 | info->prev_cable_type); |
| 404 | |
| 405 | switch (cable_type) { |
| 406 | case MAX14577_MUIC_ADC_FACTORY_MODE_USB_OFF: |
| 407 | case MAX14577_MUIC_ADC_FACTORY_MODE_USB_ON: |
| 408 | case MAX14577_MUIC_ADC_FACTORY_MODE_UART_OFF: |
| 409 | /* JIG */ |
| 410 | ret = max14577_muic_jig_handler(info, cable_type, attached); |
| 411 | if (ret < 0) |
| 412 | return ret; |
| 413 | break; |
| 414 | case MAX14577_MUIC_ADC_GROUND: |
| 415 | case MAX14577_MUIC_ADC_SEND_END_BUTTON: |
| 416 | case MAX14577_MUIC_ADC_REMOTE_S1_BUTTON: |
| 417 | case MAX14577_MUIC_ADC_REMOTE_S2_BUTTON: |
| 418 | case MAX14577_MUIC_ADC_REMOTE_S3_BUTTON: |
| 419 | case MAX14577_MUIC_ADC_REMOTE_S4_BUTTON: |
| 420 | case MAX14577_MUIC_ADC_REMOTE_S5_BUTTON: |
| 421 | case MAX14577_MUIC_ADC_REMOTE_S6_BUTTON: |
| 422 | case MAX14577_MUIC_ADC_REMOTE_S7_BUTTON: |
| 423 | case MAX14577_MUIC_ADC_REMOTE_S8_BUTTON: |
| 424 | case MAX14577_MUIC_ADC_REMOTE_S9_BUTTON: |
| 425 | case MAX14577_MUIC_ADC_REMOTE_S10_BUTTON: |
| 426 | case MAX14577_MUIC_ADC_REMOTE_S11_BUTTON: |
| 427 | case MAX14577_MUIC_ADC_REMOTE_S12_BUTTON: |
| 428 | case MAX14577_MUIC_ADC_RESERVED_ACC_1: |
| 429 | case MAX14577_MUIC_ADC_RESERVED_ACC_2: |
| 430 | case MAX14577_MUIC_ADC_RESERVED_ACC_3: |
| 431 | case MAX14577_MUIC_ADC_RESERVED_ACC_4: |
| 432 | case MAX14577_MUIC_ADC_RESERVED_ACC_5: |
| 433 | case MAX14577_MUIC_ADC_AUDIO_DEVICE_TYPE2: |
| 434 | case MAX14577_MUIC_ADC_PHONE_POWERED_DEV: |
| 435 | case MAX14577_MUIC_ADC_TTY_CONVERTER: |
| 436 | case MAX14577_MUIC_ADC_UART_CABLE: |
| 437 | case MAX14577_MUIC_ADC_CEA936A_TYPE1_CHG: |
| 438 | case MAX14577_MUIC_ADC_AV_CABLE_NOLOAD: |
| 439 | case MAX14577_MUIC_ADC_CEA936A_TYPE2_CHG: |
| 440 | case MAX14577_MUIC_ADC_FACTORY_MODE_UART_ON: |
| 441 | case MAX14577_MUIC_ADC_AUDIO_DEVICE_TYPE1: |
| 442 | /* |
| 443 | * This accessory isn't used in general case if it is specially |
| 444 | * needed to detect additional accessory, should implement |
| 445 | * proper operation when this accessory is attached/detached. |
| 446 | */ |
| 447 | dev_info(info->dev, |
| 448 | "accessory is %s but it isn't used (adc:0x%x)\n", |
| 449 | attached ? "attached" : "detached", cable_type); |
| 450 | return -EAGAIN; |
| 451 | default: |
| 452 | dev_err(info->dev, |
| 453 | "failed to detect %s accessory (adc:0x%x)\n", |
| 454 | attached ? "attached" : "detached", cable_type); |
| 455 | return -EINVAL; |
| 456 | } |
| 457 | |
| 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | static int max14577_muic_chg_handler(struct max14577_muic_info *info) |
| 462 | { |
| 463 | int chg_type; |
| 464 | bool attached; |
| 465 | int ret = 0; |
| 466 | |
| 467 | chg_type = max14577_muic_get_cable_type(info, |
| 468 | MAX14577_CABLE_GROUP_CHG, &attached); |
| 469 | |
| 470 | dev_dbg(info->dev, |
| 471 | "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n", |
| 472 | attached ? "attached" : "detached", |
| 473 | chg_type, info->prev_chg_type); |
| 474 | |
| 475 | switch (chg_type) { |
| 476 | case MAX14577_CHARGER_TYPE_USB: |
| 477 | /* PATH:AP_USB */ |
| 478 | ret = max14577_muic_set_path(info, info->path_usb, attached); |
| 479 | if (ret < 0) |
| 480 | return ret; |
| 481 | |
| 482 | extcon_set_cable_state(info->edev, "USB", attached); |
| 483 | break; |
| 484 | case MAX14577_CHARGER_TYPE_DEDICATED_CHG: |
| 485 | extcon_set_cable_state(info->edev, "TA", attached); |
| 486 | break; |
| 487 | case MAX14577_CHARGER_TYPE_DOWNSTREAM_PORT: |
| 488 | extcon_set_cable_state(info->edev, |
| 489 | "Charge-downstream", attached); |
| 490 | break; |
| 491 | case MAX14577_CHARGER_TYPE_SPECIAL_500MA: |
| 492 | extcon_set_cable_state(info->edev, "Slow-charger", attached); |
| 493 | break; |
| 494 | case MAX14577_CHARGER_TYPE_SPECIAL_1A: |
| 495 | extcon_set_cable_state(info->edev, "Fast-charger", attached); |
| 496 | break; |
| 497 | case MAX14577_CHARGER_TYPE_NONE: |
| 498 | case MAX14577_CHARGER_TYPE_DEAD_BATTERY: |
| 499 | break; |
| 500 | default: |
| 501 | dev_err(info->dev, |
| 502 | "failed to detect %s accessory (chg_type:0x%x)\n", |
| 503 | attached ? "attached" : "detached", chg_type); |
| 504 | return -EINVAL; |
| 505 | } |
| 506 | |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | static void max14577_muic_irq_work(struct work_struct *work) |
| 511 | { |
| 512 | struct max14577_muic_info *info = container_of(work, |
| 513 | struct max14577_muic_info, irq_work); |
| 514 | int ret = 0; |
| 515 | |
| 516 | if (!info->edev) |
| 517 | return; |
| 518 | |
| 519 | mutex_lock(&info->mutex); |
| 520 | |
| 521 | ret = max14577_bulk_read(info->max14577->regmap, |
| 522 | MAX14577_MUIC_REG_STATUS1, info->status, 2); |
| 523 | if (ret) { |
| 524 | dev_err(info->dev, "failed to read MUIC register\n"); |
| 525 | mutex_unlock(&info->mutex); |
| 526 | return; |
| 527 | } |
| 528 | |
| 529 | if (info->irq_adc) { |
| 530 | ret = max14577_muic_adc_handler(info); |
| 531 | info->irq_adc = false; |
| 532 | } |
| 533 | if (info->irq_chg) { |
| 534 | ret = max14577_muic_chg_handler(info); |
| 535 | info->irq_chg = false; |
| 536 | } |
| 537 | |
| 538 | if (ret < 0) |
| 539 | dev_err(info->dev, "failed to handle MUIC interrupt\n"); |
| 540 | |
| 541 | mutex_unlock(&info->mutex); |
| 542 | |
| 543 | return; |
| 544 | } |
| 545 | |
Krzysztof Kozlowski | 4706a52 | 2014-04-14 11:17:19 +0200 | [diff] [blame^] | 546 | /* |
| 547 | * Sets irq_adc or irq_chg in max14577_muic_info and returns 1. |
| 548 | * Returns 0 if irq_type does not match registered IRQ for this device type. |
| 549 | */ |
| 550 | static int max14577_parse_irq(struct max14577_muic_info *info, int irq_type) |
| 551 | { |
| 552 | switch (irq_type) { |
| 553 | case MAX14577_IRQ_INT1_ADC: |
| 554 | case MAX14577_IRQ_INT1_ADCLOW: |
| 555 | case MAX14577_IRQ_INT1_ADCERR: |
| 556 | /* Handle all of accessory except for |
| 557 | type of charger accessory */ |
| 558 | info->irq_adc = true; |
| 559 | return 1; |
| 560 | case MAX14577_IRQ_INT2_CHGTYP: |
| 561 | case MAX14577_IRQ_INT2_CHGDETRUN: |
| 562 | case MAX14577_IRQ_INT2_DCDTMR: |
| 563 | case MAX14577_IRQ_INT2_DBCHG: |
| 564 | case MAX14577_IRQ_INT2_VBVOLT: |
| 565 | /* Handle charger accessory */ |
| 566 | info->irq_chg = true; |
| 567 | return 1; |
| 568 | default: |
| 569 | return 0; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | /* |
| 574 | * Sets irq_adc or irq_chg in max14577_muic_info and returns 1. |
| 575 | * Returns 0 if irq_type does not match registered IRQ for this device type. |
| 576 | */ |
| 577 | static int max77836_parse_irq(struct max14577_muic_info *info, int irq_type) |
| 578 | { |
| 579 | /* First check common max14577 interrupts */ |
| 580 | if (max14577_parse_irq(info, irq_type)) |
| 581 | return 1; |
| 582 | |
| 583 | switch (irq_type) { |
| 584 | case MAX77836_IRQ_INT1_ADC1K: |
| 585 | info->irq_adc = true; |
| 586 | return 1; |
| 587 | case MAX77836_IRQ_INT2_VIDRM: |
| 588 | /* Handle charger accessory */ |
| 589 | info->irq_chg = true; |
| 590 | return 1; |
| 591 | default: |
| 592 | return 0; |
| 593 | } |
| 594 | } |
| 595 | |
Chanwoo Choi | 962e56b | 2013-11-22 16:51:06 +0100 | [diff] [blame] | 596 | static irqreturn_t max14577_muic_irq_handler(int irq, void *data) |
| 597 | { |
| 598 | struct max14577_muic_info *info = data; |
| 599 | int i, irq_type = -1; |
Krzysztof Kozlowski | 4706a52 | 2014-04-14 11:17:19 +0200 | [diff] [blame^] | 600 | bool irq_parsed; |
Chanwoo Choi | 962e56b | 2013-11-22 16:51:06 +0100 | [diff] [blame] | 601 | |
| 602 | /* |
| 603 | * We may be called multiple times for different nested IRQ-s. |
| 604 | * Including changes in INT1_ADC and INT2_CGHTYP at once. |
| 605 | * However we only need to know whether it was ADC, charger |
| 606 | * or both interrupts so decode IRQ and turn on proper flags. |
| 607 | */ |
Krzysztof Kozlowski | 0ca852b | 2014-04-14 11:17:16 +0200 | [diff] [blame] | 608 | for (i = 0; i < info->muic_irqs_num; i++) |
| 609 | if (irq == info->muic_irqs[i].virq) |
| 610 | irq_type = info->muic_irqs[i].irq; |
Chanwoo Choi | 962e56b | 2013-11-22 16:51:06 +0100 | [diff] [blame] | 611 | |
Krzysztof Kozlowski | 4706a52 | 2014-04-14 11:17:19 +0200 | [diff] [blame^] | 612 | switch (info->max14577->dev_type) { |
| 613 | case MAXIM_DEVICE_TYPE_MAX77836: |
| 614 | irq_parsed = max77836_parse_irq(info, irq_type); |
Chanwoo Choi | 962e56b | 2013-11-22 16:51:06 +0100 | [diff] [blame] | 615 | break; |
Krzysztof Kozlowski | 4706a52 | 2014-04-14 11:17:19 +0200 | [diff] [blame^] | 616 | case MAXIM_DEVICE_TYPE_MAX14577: |
Chanwoo Choi | 962e56b | 2013-11-22 16:51:06 +0100 | [diff] [blame] | 617 | default: |
Krzysztof Kozlowski | 4706a52 | 2014-04-14 11:17:19 +0200 | [diff] [blame^] | 618 | irq_parsed = max14577_parse_irq(info, irq_type); |
| 619 | break; |
| 620 | } |
| 621 | |
| 622 | if (!irq_parsed) { |
Chanwoo Choi | 962e56b | 2013-11-22 16:51:06 +0100 | [diff] [blame] | 623 | dev_err(info->dev, "muic interrupt: irq %d occurred, skipped\n", |
| 624 | irq_type); |
| 625 | return IRQ_HANDLED; |
| 626 | } |
| 627 | schedule_work(&info->irq_work); |
| 628 | |
| 629 | return IRQ_HANDLED; |
| 630 | } |
| 631 | |
| 632 | static int max14577_muic_detect_accessory(struct max14577_muic_info *info) |
| 633 | { |
| 634 | int ret = 0; |
| 635 | int adc; |
| 636 | int chg_type; |
| 637 | bool attached; |
| 638 | |
| 639 | mutex_lock(&info->mutex); |
| 640 | |
| 641 | /* Read STATUSx register to detect accessory */ |
| 642 | ret = max14577_bulk_read(info->max14577->regmap, |
| 643 | MAX14577_MUIC_REG_STATUS1, info->status, 2); |
| 644 | if (ret) { |
| 645 | dev_err(info->dev, "failed to read MUIC register\n"); |
| 646 | mutex_unlock(&info->mutex); |
| 647 | return ret; |
| 648 | } |
| 649 | |
| 650 | adc = max14577_muic_get_cable_type(info, MAX14577_CABLE_GROUP_ADC, |
| 651 | &attached); |
| 652 | if (attached && adc != MAX14577_MUIC_ADC_OPEN) { |
| 653 | ret = max14577_muic_adc_handler(info); |
| 654 | if (ret < 0) { |
| 655 | dev_err(info->dev, "Cannot detect accessory\n"); |
| 656 | mutex_unlock(&info->mutex); |
| 657 | return ret; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | chg_type = max14577_muic_get_cable_type(info, MAX14577_CABLE_GROUP_CHG, |
| 662 | &attached); |
| 663 | if (attached && chg_type != MAX14577_CHARGER_TYPE_NONE) { |
| 664 | ret = max14577_muic_chg_handler(info); |
| 665 | if (ret < 0) { |
| 666 | dev_err(info->dev, "Cannot detect charger accessory\n"); |
| 667 | mutex_unlock(&info->mutex); |
| 668 | return ret; |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | mutex_unlock(&info->mutex); |
| 673 | |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | static void max14577_muic_detect_cable_wq(struct work_struct *work) |
| 678 | { |
| 679 | struct max14577_muic_info *info = container_of(to_delayed_work(work), |
| 680 | struct max14577_muic_info, wq_detcable); |
| 681 | |
| 682 | max14577_muic_detect_accessory(info); |
| 683 | } |
| 684 | |
| 685 | static int max14577_muic_probe(struct platform_device *pdev) |
| 686 | { |
| 687 | struct max14577 *max14577 = dev_get_drvdata(pdev->dev.parent); |
| 688 | struct max14577_muic_info *info; |
| 689 | int delay_jiffies; |
| 690 | int ret; |
| 691 | int i; |
| 692 | u8 id; |
| 693 | |
| 694 | info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); |
| 695 | if (!info) { |
| 696 | dev_err(&pdev->dev, "failed to allocate memory\n"); |
| 697 | return -ENOMEM; |
| 698 | } |
| 699 | info->dev = &pdev->dev; |
| 700 | info->max14577 = max14577; |
| 701 | |
| 702 | platform_set_drvdata(pdev, info); |
| 703 | mutex_init(&info->mutex); |
| 704 | |
| 705 | INIT_WORK(&info->irq_work, max14577_muic_irq_work); |
| 706 | |
Krzysztof Kozlowski | 0ca852b | 2014-04-14 11:17:16 +0200 | [diff] [blame] | 707 | switch (max14577->dev_type) { |
Krzysztof Kozlowski | 4706a52 | 2014-04-14 11:17:19 +0200 | [diff] [blame^] | 708 | case MAXIM_DEVICE_TYPE_MAX77836: |
| 709 | info->muic_irqs = max77836_muic_irqs; |
| 710 | info->muic_irqs_num = ARRAY_SIZE(max77836_muic_irqs); |
| 711 | break; |
Krzysztof Kozlowski | 0ca852b | 2014-04-14 11:17:16 +0200 | [diff] [blame] | 712 | case MAXIM_DEVICE_TYPE_MAX14577: |
| 713 | default: |
| 714 | info->muic_irqs = max14577_muic_irqs; |
| 715 | info->muic_irqs_num = ARRAY_SIZE(max14577_muic_irqs); |
| 716 | } |
| 717 | |
Chanwoo Choi | 962e56b | 2013-11-22 16:51:06 +0100 | [diff] [blame] | 718 | /* Support irq domain for max14577 MUIC device */ |
Krzysztof Kozlowski | 0ca852b | 2014-04-14 11:17:16 +0200 | [diff] [blame] | 719 | for (i = 0; i < info->muic_irqs_num; i++) { |
| 720 | struct max14577_muic_irq *muic_irq = &info->muic_irqs[i]; |
Chanwoo Choi | 962e56b | 2013-11-22 16:51:06 +0100 | [diff] [blame] | 721 | unsigned int virq = 0; |
| 722 | |
| 723 | virq = regmap_irq_get_virq(max14577->irq_data, muic_irq->irq); |
| 724 | if (!virq) |
| 725 | return -EINVAL; |
| 726 | muic_irq->virq = virq; |
| 727 | |
| 728 | ret = devm_request_threaded_irq(&pdev->dev, virq, NULL, |
| 729 | max14577_muic_irq_handler, |
| 730 | IRQF_NO_SUSPEND, |
| 731 | muic_irq->name, info); |
| 732 | if (ret) { |
| 733 | dev_err(&pdev->dev, |
| 734 | "failed: irq request (IRQ: %d," |
| 735 | " error :%d)\n", |
| 736 | muic_irq->irq, ret); |
| 737 | return ret; |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | /* Initialize extcon device */ |
| 742 | info->edev = devm_kzalloc(&pdev->dev, sizeof(*info->edev), GFP_KERNEL); |
| 743 | if (!info->edev) { |
| 744 | dev_err(&pdev->dev, "failed to allocate memory for extcon\n"); |
| 745 | return -ENOMEM; |
| 746 | } |
Chanwoo Choi | 4005da5 | 2014-04-14 11:17:12 +0200 | [diff] [blame] | 747 | |
| 748 | info->edev->name = dev_name(&pdev->dev); |
Chanwoo Choi | 962e56b | 2013-11-22 16:51:06 +0100 | [diff] [blame] | 749 | info->edev->supported_cable = max14577_extcon_cable; |
| 750 | ret = extcon_dev_register(info->edev); |
| 751 | if (ret) { |
| 752 | dev_err(&pdev->dev, "failed to register extcon device\n"); |
| 753 | return ret; |
| 754 | } |
| 755 | |
| 756 | /* Default h/w line path */ |
| 757 | info->path_usb = CTRL1_SW_USB; |
| 758 | info->path_uart = CTRL1_SW_UART; |
| 759 | delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT); |
| 760 | |
| 761 | /* Set initial path for UART */ |
| 762 | max14577_muic_set_path(info, info->path_uart, true); |
| 763 | |
| 764 | /* Check revision number of MUIC device*/ |
| 765 | ret = max14577_read_reg(info->max14577->regmap, |
| 766 | MAX14577_REG_DEVICEID, &id); |
| 767 | if (ret < 0) { |
| 768 | dev_err(&pdev->dev, "failed to read revision number\n"); |
| 769 | goto err_extcon; |
| 770 | } |
| 771 | dev_info(info->dev, "device ID : 0x%x\n", id); |
| 772 | |
| 773 | /* Set ADC debounce time */ |
| 774 | max14577_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS); |
| 775 | |
| 776 | /* |
| 777 | * Detect accessory after completing the initialization of platform |
| 778 | * |
| 779 | * - Use delayed workqueue to detect cable state and then |
| 780 | * notify cable state to notifiee/platform through uevent. |
| 781 | * After completing the booting of platform, the extcon provider |
| 782 | * driver should notify cable state to upper layer. |
| 783 | */ |
| 784 | INIT_DELAYED_WORK(&info->wq_detcable, max14577_muic_detect_cable_wq); |
| 785 | ret = queue_delayed_work(system_power_efficient_wq, &info->wq_detcable, |
| 786 | delay_jiffies); |
| 787 | if (ret < 0) { |
| 788 | dev_err(&pdev->dev, |
| 789 | "failed to schedule delayed work for cable detect\n"); |
| 790 | goto err_extcon; |
| 791 | } |
| 792 | |
| 793 | return ret; |
| 794 | |
| 795 | err_extcon: |
| 796 | extcon_dev_unregister(info->edev); |
| 797 | return ret; |
| 798 | } |
| 799 | |
| 800 | static int max14577_muic_remove(struct platform_device *pdev) |
| 801 | { |
| 802 | struct max14577_muic_info *info = platform_get_drvdata(pdev); |
| 803 | |
| 804 | cancel_work_sync(&info->irq_work); |
| 805 | extcon_dev_unregister(info->edev); |
| 806 | |
| 807 | return 0; |
| 808 | } |
| 809 | |
Krzysztof Kozlowski | 4706a52 | 2014-04-14 11:17:19 +0200 | [diff] [blame^] | 810 | static const struct platform_device_id max14577_muic_id[] = { |
| 811 | { "max14577-muic", MAXIM_DEVICE_TYPE_MAX14577, }, |
| 812 | { "max77836-muic", MAXIM_DEVICE_TYPE_MAX77836, }, |
| 813 | { } |
| 814 | }; |
| 815 | MODULE_DEVICE_TABLE(platform, max14577_muic_id); |
| 816 | |
Chanwoo Choi | 962e56b | 2013-11-22 16:51:06 +0100 | [diff] [blame] | 817 | static struct platform_driver max14577_muic_driver = { |
| 818 | .driver = { |
Chanwoo Choi | 4005da5 | 2014-04-14 11:17:12 +0200 | [diff] [blame] | 819 | .name = "max14577-muic", |
Chanwoo Choi | 962e56b | 2013-11-22 16:51:06 +0100 | [diff] [blame] | 820 | .owner = THIS_MODULE, |
| 821 | }, |
| 822 | .probe = max14577_muic_probe, |
| 823 | .remove = max14577_muic_remove, |
Krzysztof Kozlowski | 4706a52 | 2014-04-14 11:17:19 +0200 | [diff] [blame^] | 824 | .id_table = max14577_muic_id, |
Chanwoo Choi | 962e56b | 2013-11-22 16:51:06 +0100 | [diff] [blame] | 825 | }; |
| 826 | |
| 827 | module_platform_driver(max14577_muic_driver); |
| 828 | |
Krzysztof Kozlowski | 4706a52 | 2014-04-14 11:17:19 +0200 | [diff] [blame^] | 829 | MODULE_DESCRIPTION("Maxim 14577/77836 Extcon driver"); |
| 830 | MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>, Krzysztof Kozlowski <k.kozlowski@samsung.com>"); |
Chanwoo Choi | 962e56b | 2013-11-22 16:51:06 +0100 | [diff] [blame] | 831 | MODULE_LICENSE("GPL"); |
| 832 | MODULE_ALIAS("platform:extcon-max14577"); |