11213a366SKrzysztof Kozlowski // SPDX-License-Identifier: GPL-2.0+ 21213a366SKrzysztof Kozlowski // 31213a366SKrzysztof Kozlowski // extcon-max77843.c - Maxim MAX77843 extcon driver to support 41213a366SKrzysztof Kozlowski // MUIC(Micro USB Interface Controller) 51213a366SKrzysztof Kozlowski // 61213a366SKrzysztof Kozlowski // Copyright (C) 2015 Samsung Electronics 71213a366SKrzysztof Kozlowski // Author: Jaewon Kim <jaewon02.kim@samsung.com> 827a28d32SJaewon Kim 9176aa360SChanwoo Choi #include <linux/extcon-provider.h> 1027a28d32SJaewon Kim #include <linux/i2c.h> 1127a28d32SJaewon Kim #include <linux/interrupt.h> 1227a28d32SJaewon Kim #include <linux/kernel.h> 13bc1aadc1SKrzysztof Kozlowski #include <linux/mfd/max77693-common.h> 1427a28d32SJaewon Kim #include <linux/mfd/max77843-private.h> 1527a28d32SJaewon Kim #include <linux/module.h> 1627a28d32SJaewon Kim #include <linux/platform_device.h> 1727a28d32SJaewon Kim #include <linux/workqueue.h> 1827a28d32SJaewon Kim 1927a28d32SJaewon Kim #define DELAY_MS_DEFAULT 15000 /* unit: millisecond */ 2027a28d32SJaewon Kim 2127a28d32SJaewon Kim enum max77843_muic_status { 2227a28d32SJaewon Kim MAX77843_MUIC_STATUS1 = 0, 2327a28d32SJaewon Kim MAX77843_MUIC_STATUS2, 2427a28d32SJaewon Kim MAX77843_MUIC_STATUS3, 2527a28d32SJaewon Kim 2627a28d32SJaewon Kim MAX77843_MUIC_STATUS_NUM, 2727a28d32SJaewon Kim }; 2827a28d32SJaewon Kim 2927a28d32SJaewon Kim struct max77843_muic_info { 3027a28d32SJaewon Kim struct device *dev; 31bc1aadc1SKrzysztof Kozlowski struct max77693_dev *max77843; 3227a28d32SJaewon Kim struct extcon_dev *edev; 3327a28d32SJaewon Kim 3427a28d32SJaewon Kim struct mutex mutex; 3527a28d32SJaewon Kim struct work_struct irq_work; 3627a28d32SJaewon Kim struct delayed_work wq_detcable; 3727a28d32SJaewon Kim 3827a28d32SJaewon Kim u8 status[MAX77843_MUIC_STATUS_NUM]; 3927a28d32SJaewon Kim int prev_cable_type; 4027a28d32SJaewon Kim int prev_chg_type; 4127a28d32SJaewon Kim int prev_gnd_type; 4227a28d32SJaewon Kim 4327a28d32SJaewon Kim bool irq_adc; 4427a28d32SJaewon Kim bool irq_chg; 4527a28d32SJaewon Kim }; 4627a28d32SJaewon Kim 4727a28d32SJaewon Kim enum max77843_muic_cable_group { 4827a28d32SJaewon Kim MAX77843_CABLE_GROUP_ADC = 0, 4927a28d32SJaewon Kim MAX77843_CABLE_GROUP_ADC_GND, 5027a28d32SJaewon Kim MAX77843_CABLE_GROUP_CHG, 5127a28d32SJaewon Kim }; 5227a28d32SJaewon Kim 5327a28d32SJaewon Kim enum max77843_muic_adc_debounce_time { 5427a28d32SJaewon Kim MAX77843_DEBOUNCE_TIME_5MS = 0, 5527a28d32SJaewon Kim MAX77843_DEBOUNCE_TIME_10MS, 5627a28d32SJaewon Kim MAX77843_DEBOUNCE_TIME_25MS, 5727a28d32SJaewon Kim MAX77843_DEBOUNCE_TIME_38_62MS, 5827a28d32SJaewon Kim }; 5927a28d32SJaewon Kim 6027a28d32SJaewon Kim /* Define accessory cable type */ 6127a28d32SJaewon Kim enum max77843_muic_accessory_type { 6227a28d32SJaewon Kim MAX77843_MUIC_ADC_GROUND = 0, 6327a28d32SJaewon Kim MAX77843_MUIC_ADC_SEND_END_BUTTON, 6427a28d32SJaewon Kim MAX77843_MUIC_ADC_REMOTE_S1_BUTTON, 6527a28d32SJaewon Kim MAX77843_MUIC_ADC_REMOTE_S2_BUTTON, 6627a28d32SJaewon Kim MAX77843_MUIC_ADC_REMOTE_S3_BUTTON, 6727a28d32SJaewon Kim MAX77843_MUIC_ADC_REMOTE_S4_BUTTON, 6827a28d32SJaewon Kim MAX77843_MUIC_ADC_REMOTE_S5_BUTTON, 6927a28d32SJaewon Kim MAX77843_MUIC_ADC_REMOTE_S6_BUTTON, 7027a28d32SJaewon Kim MAX77843_MUIC_ADC_REMOTE_S7_BUTTON, 7127a28d32SJaewon Kim MAX77843_MUIC_ADC_REMOTE_S8_BUTTON, 7227a28d32SJaewon Kim MAX77843_MUIC_ADC_REMOTE_S9_BUTTON, 7327a28d32SJaewon Kim MAX77843_MUIC_ADC_REMOTE_S10_BUTTON, 7427a28d32SJaewon Kim MAX77843_MUIC_ADC_REMOTE_S11_BUTTON, 7527a28d32SJaewon Kim MAX77843_MUIC_ADC_REMOTE_S12_BUTTON, 7627a28d32SJaewon Kim MAX77843_MUIC_ADC_RESERVED_ACC_1, 7727a28d32SJaewon Kim MAX77843_MUIC_ADC_RESERVED_ACC_2, 784a4a8714SMarek Szyprowski MAX77843_MUIC_ADC_RESERVED_ACC_3, /* SmartDock */ 7927a28d32SJaewon Kim MAX77843_MUIC_ADC_RESERVED_ACC_4, 8027a28d32SJaewon Kim MAX77843_MUIC_ADC_RESERVED_ACC_5, 8127a28d32SJaewon Kim MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE2, 8227a28d32SJaewon Kim MAX77843_MUIC_ADC_PHONE_POWERED_DEV, 8327a28d32SJaewon Kim MAX77843_MUIC_ADC_TTY_CONVERTER, 8427a28d32SJaewon Kim MAX77843_MUIC_ADC_UART_CABLE, 8527a28d32SJaewon Kim MAX77843_MUIC_ADC_CEA936A_TYPE1_CHG, 8627a28d32SJaewon Kim MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF, 8727a28d32SJaewon Kim MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON, 8827a28d32SJaewon Kim MAX77843_MUIC_ADC_AV_CABLE_NOLOAD, 8927a28d32SJaewon Kim MAX77843_MUIC_ADC_CEA936A_TYPE2_CHG, 9027a28d32SJaewon Kim MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF, 9127a28d32SJaewon Kim MAX77843_MUIC_ADC_FACTORY_MODE_UART_ON, 9227a28d32SJaewon Kim MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE1, 9327a28d32SJaewon Kim MAX77843_MUIC_ADC_OPEN, 9427a28d32SJaewon Kim 95af57fa4dSSrikant Ritolia /* 96af57fa4dSSrikant Ritolia * The below accessories should check 97af57fa4dSSrikant Ritolia * not only ADC value but also ADC1K and VBVolt value. 98af57fa4dSSrikant Ritolia */ 9927a28d32SJaewon Kim /* Offset|ADC1K|VBVolt| */ 10027a28d32SJaewon Kim MAX77843_MUIC_GND_USB_HOST = 0x100, /* 0x1| 0| 0| */ 10127a28d32SJaewon Kim MAX77843_MUIC_GND_USB_HOST_VB = 0x101, /* 0x1| 0| 1| */ 10227a28d32SJaewon Kim MAX77843_MUIC_GND_MHL = 0x102, /* 0x1| 1| 0| */ 10327a28d32SJaewon Kim MAX77843_MUIC_GND_MHL_VB = 0x103, /* 0x1| 1| 1| */ 10427a28d32SJaewon Kim }; 10527a28d32SJaewon Kim 10627a28d32SJaewon Kim /* Define charger cable type */ 10727a28d32SJaewon Kim enum max77843_muic_charger_type { 10827a28d32SJaewon Kim MAX77843_MUIC_CHG_NONE = 0, 10927a28d32SJaewon Kim MAX77843_MUIC_CHG_USB, 11027a28d32SJaewon Kim MAX77843_MUIC_CHG_DOWNSTREAM, 11127a28d32SJaewon Kim MAX77843_MUIC_CHG_DEDICATED, 11227a28d32SJaewon Kim MAX77843_MUIC_CHG_SPECIAL_500MA, 11327a28d32SJaewon Kim MAX77843_MUIC_CHG_SPECIAL_1A, 11427a28d32SJaewon Kim MAX77843_MUIC_CHG_SPECIAL_BIAS, 11527a28d32SJaewon Kim MAX77843_MUIC_CHG_RESERVED, 11627a28d32SJaewon Kim MAX77843_MUIC_CHG_GND, 1174a4a8714SMarek Szyprowski MAX77843_MUIC_CHG_DOCK, 11827a28d32SJaewon Kim }; 11927a28d32SJaewon Kim 12073b6ecdbSChanwoo Choi static const unsigned int max77843_extcon_cable[] = { 1212a9de9c0SChanwoo Choi EXTCON_USB, 1222a9de9c0SChanwoo Choi EXTCON_USB_HOST, 1238b45b6a0SChanwoo Choi EXTCON_CHG_USB_SDP, 12411eecf91SChanwoo Choi EXTCON_CHG_USB_DCP, 12511eecf91SChanwoo Choi EXTCON_CHG_USB_CDP, 12611eecf91SChanwoo Choi EXTCON_CHG_USB_FAST, 12711eecf91SChanwoo Choi EXTCON_CHG_USB_SLOW, 12811eecf91SChanwoo Choi EXTCON_DISP_MHL, 1294a4a8714SMarek Szyprowski EXTCON_DOCK, 1302a9de9c0SChanwoo Choi EXTCON_JIG, 1312a9de9c0SChanwoo Choi EXTCON_NONE, 13227a28d32SJaewon Kim }; 13327a28d32SJaewon Kim 13427a28d32SJaewon Kim struct max77843_muic_irq { 13527a28d32SJaewon Kim unsigned int irq; 13627a28d32SJaewon Kim const char *name; 13727a28d32SJaewon Kim unsigned int virq; 13827a28d32SJaewon Kim }; 13927a28d32SJaewon Kim 14027a28d32SJaewon Kim static struct max77843_muic_irq max77843_muic_irqs[] = { 14127a28d32SJaewon Kim { MAX77843_MUIC_IRQ_INT1_ADC, "MUIC-ADC" }, 14227a28d32SJaewon Kim { MAX77843_MUIC_IRQ_INT1_ADCERROR, "MUIC-ADC_ERROR" }, 14327a28d32SJaewon Kim { MAX77843_MUIC_IRQ_INT1_ADC1K, "MUIC-ADC1K" }, 14427a28d32SJaewon Kim { MAX77843_MUIC_IRQ_INT2_CHGTYP, "MUIC-CHGTYP" }, 14527a28d32SJaewon Kim { MAX77843_MUIC_IRQ_INT2_CHGDETRUN, "MUIC-CHGDETRUN" }, 14627a28d32SJaewon Kim { MAX77843_MUIC_IRQ_INT2_DCDTMR, "MUIC-DCDTMR" }, 14727a28d32SJaewon Kim { MAX77843_MUIC_IRQ_INT2_DXOVP, "MUIC-DXOVP" }, 14827a28d32SJaewon Kim { MAX77843_MUIC_IRQ_INT2_VBVOLT, "MUIC-VBVOLT" }, 14927a28d32SJaewon Kim { MAX77843_MUIC_IRQ_INT3_VBADC, "MUIC-VBADC" }, 15027a28d32SJaewon Kim { MAX77843_MUIC_IRQ_INT3_VDNMON, "MUIC-VDNMON" }, 15127a28d32SJaewon Kim { MAX77843_MUIC_IRQ_INT3_DNRES, "MUIC-DNRES" }, 15227a28d32SJaewon Kim { MAX77843_MUIC_IRQ_INT3_MPNACK, "MUIC-MPNACK"}, 15327a28d32SJaewon Kim { MAX77843_MUIC_IRQ_INT3_MRXBUFOW, "MUIC-MRXBUFOW"}, 15427a28d32SJaewon Kim { MAX77843_MUIC_IRQ_INT3_MRXTRF, "MUIC-MRXTRF"}, 15527a28d32SJaewon Kim { MAX77843_MUIC_IRQ_INT3_MRXPERR, "MUIC-MRXPERR"}, 15627a28d32SJaewon Kim { MAX77843_MUIC_IRQ_INT3_MRXRDY, "MUIC-MRXRDY"}, 15727a28d32SJaewon Kim }; 15827a28d32SJaewon Kim 15927a28d32SJaewon Kim static const struct regmap_config max77843_muic_regmap_config = { 16027a28d32SJaewon Kim .reg_bits = 8, 16127a28d32SJaewon Kim .val_bits = 8, 16227a28d32SJaewon Kim .max_register = MAX77843_MUIC_REG_END, 16327a28d32SJaewon Kim }; 16427a28d32SJaewon Kim 16527a28d32SJaewon Kim static const struct regmap_irq max77843_muic_irq[] = { 16627a28d32SJaewon Kim /* INT1 interrupt */ 16727a28d32SJaewon Kim { .reg_offset = 0, .mask = MAX77843_MUIC_ADC, }, 16827a28d32SJaewon Kim { .reg_offset = 0, .mask = MAX77843_MUIC_ADCERROR, }, 16927a28d32SJaewon Kim { .reg_offset = 0, .mask = MAX77843_MUIC_ADC1K, }, 17027a28d32SJaewon Kim 17127a28d32SJaewon Kim /* INT2 interrupt */ 17227a28d32SJaewon Kim { .reg_offset = 1, .mask = MAX77843_MUIC_CHGTYP, }, 17327a28d32SJaewon Kim { .reg_offset = 1, .mask = MAX77843_MUIC_CHGDETRUN, }, 17427a28d32SJaewon Kim { .reg_offset = 1, .mask = MAX77843_MUIC_DCDTMR, }, 17527a28d32SJaewon Kim { .reg_offset = 1, .mask = MAX77843_MUIC_DXOVP, }, 17627a28d32SJaewon Kim { .reg_offset = 1, .mask = MAX77843_MUIC_VBVOLT, }, 17727a28d32SJaewon Kim 17827a28d32SJaewon Kim /* INT3 interrupt */ 17927a28d32SJaewon Kim { .reg_offset = 2, .mask = MAX77843_MUIC_VBADC, }, 18027a28d32SJaewon Kim { .reg_offset = 2, .mask = MAX77843_MUIC_VDNMON, }, 18127a28d32SJaewon Kim { .reg_offset = 2, .mask = MAX77843_MUIC_DNRES, }, 18227a28d32SJaewon Kim { .reg_offset = 2, .mask = MAX77843_MUIC_MPNACK, }, 18327a28d32SJaewon Kim { .reg_offset = 2, .mask = MAX77843_MUIC_MRXBUFOW, }, 18427a28d32SJaewon Kim { .reg_offset = 2, .mask = MAX77843_MUIC_MRXTRF, }, 18527a28d32SJaewon Kim { .reg_offset = 2, .mask = MAX77843_MUIC_MRXPERR, }, 18627a28d32SJaewon Kim { .reg_offset = 2, .mask = MAX77843_MUIC_MRXRDY, }, 18727a28d32SJaewon Kim }; 18827a28d32SJaewon Kim 18927a28d32SJaewon Kim static const struct regmap_irq_chip max77843_muic_irq_chip = { 19027a28d32SJaewon Kim .name = "max77843-muic", 19127a28d32SJaewon Kim .status_base = MAX77843_MUIC_REG_INT1, 1925de7cdd7SAidan MacDonald .unmask_base = MAX77843_MUIC_REG_INTMASK1, 19327a28d32SJaewon Kim .num_regs = 3, 19427a28d32SJaewon Kim .irqs = max77843_muic_irq, 19527a28d32SJaewon Kim .num_irqs = ARRAY_SIZE(max77843_muic_irq), 19627a28d32SJaewon Kim }; 19727a28d32SJaewon Kim 19827a28d32SJaewon Kim static int max77843_muic_set_path(struct max77843_muic_info *info, 1994a4a8714SMarek Szyprowski u8 val, bool attached, bool nobccomp) 20027a28d32SJaewon Kim { 201bc1aadc1SKrzysztof Kozlowski struct max77693_dev *max77843 = info->max77843; 20227a28d32SJaewon Kim int ret = 0; 20327a28d32SJaewon Kim unsigned int ctrl1, ctrl2; 20427a28d32SJaewon Kim 20527a28d32SJaewon Kim if (attached) 20627a28d32SJaewon Kim ctrl1 = val; 20727a28d32SJaewon Kim else 208309a3e00SKrzysztof Kozlowski ctrl1 = MAX77843_MUIC_CONTROL1_SW_OPEN; 2094a4a8714SMarek Szyprowski if (nobccomp) { 2104a4a8714SMarek Szyprowski /* Disable BC1.2 protocol and force manual switch control */ 2114a4a8714SMarek Szyprowski ctrl1 |= MAX77843_MUIC_CONTROL1_NOBCCOMP_MASK; 2124a4a8714SMarek Szyprowski } 21327a28d32SJaewon Kim 21427a28d32SJaewon Kim ret = regmap_update_bits(max77843->regmap_muic, 21527a28d32SJaewon Kim MAX77843_MUIC_REG_CONTROL1, 2164a4a8714SMarek Szyprowski MAX77843_MUIC_CONTROL1_COM_SW | 2174a4a8714SMarek Szyprowski MAX77843_MUIC_CONTROL1_NOBCCOMP_MASK, 2184a4a8714SMarek Szyprowski ctrl1); 21927a28d32SJaewon Kim if (ret < 0) { 22027a28d32SJaewon Kim dev_err(info->dev, "Cannot switch MUIC port\n"); 22127a28d32SJaewon Kim return ret; 22227a28d32SJaewon Kim } 22327a28d32SJaewon Kim 22427a28d32SJaewon Kim if (attached) 22527a28d32SJaewon Kim ctrl2 = MAX77843_MUIC_CONTROL2_CPEN_MASK; 22627a28d32SJaewon Kim else 22727a28d32SJaewon Kim ctrl2 = MAX77843_MUIC_CONTROL2_LOWPWR_MASK; 22827a28d32SJaewon Kim 22927a28d32SJaewon Kim ret = regmap_update_bits(max77843->regmap_muic, 23027a28d32SJaewon Kim MAX77843_MUIC_REG_CONTROL2, 23127a28d32SJaewon Kim MAX77843_MUIC_CONTROL2_LOWPWR_MASK | 23227a28d32SJaewon Kim MAX77843_MUIC_CONTROL2_CPEN_MASK, ctrl2); 23327a28d32SJaewon Kim if (ret < 0) { 23427a28d32SJaewon Kim dev_err(info->dev, "Cannot update lowpower mode\n"); 23527a28d32SJaewon Kim return ret; 23627a28d32SJaewon Kim } 23727a28d32SJaewon Kim 23827a28d32SJaewon Kim dev_dbg(info->dev, 23927a28d32SJaewon Kim "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n", 24027a28d32SJaewon Kim ctrl1, ctrl2, attached ? "attached" : "detached"); 24127a28d32SJaewon Kim 24227a28d32SJaewon Kim return 0; 24327a28d32SJaewon Kim } 24427a28d32SJaewon Kim 2457b965110SMarek Szyprowski static void max77843_charger_set_otg_vbus(struct max77843_muic_info *info, 2467b965110SMarek Szyprowski bool on) 2477b965110SMarek Szyprowski { 2487b965110SMarek Szyprowski struct max77693_dev *max77843 = info->max77843; 2497b965110SMarek Szyprowski unsigned int cnfg00; 2507b965110SMarek Szyprowski 2517b965110SMarek Szyprowski if (on) 2527b965110SMarek Szyprowski cnfg00 = MAX77843_CHG_OTG_MASK | MAX77843_CHG_BOOST_MASK; 2537b965110SMarek Szyprowski else 2547b965110SMarek Szyprowski cnfg00 = MAX77843_CHG_ENABLE | MAX77843_CHG_BUCK_MASK; 2557b965110SMarek Szyprowski 2567b965110SMarek Szyprowski regmap_update_bits(max77843->regmap_chg, MAX77843_CHG_REG_CHG_CNFG_00, 2577b965110SMarek Szyprowski MAX77843_CHG_MODE_MASK, cnfg00); 2587b965110SMarek Szyprowski } 2597b965110SMarek Szyprowski 26027a28d32SJaewon Kim static int max77843_muic_get_cable_type(struct max77843_muic_info *info, 26127a28d32SJaewon Kim enum max77843_muic_cable_group group, bool *attached) 26227a28d32SJaewon Kim { 26327a28d32SJaewon Kim int adc, chg_type, cable_type, gnd_type; 26427a28d32SJaewon Kim 26527a28d32SJaewon Kim adc = info->status[MAX77843_MUIC_STATUS1] & 26627a28d32SJaewon Kim MAX77843_MUIC_STATUS1_ADC_MASK; 267309a3e00SKrzysztof Kozlowski adc >>= MAX77843_MUIC_STATUS1_ADC_SHIFT; 26827a28d32SJaewon Kim 26927a28d32SJaewon Kim switch (group) { 27027a28d32SJaewon Kim case MAX77843_CABLE_GROUP_ADC: 27127a28d32SJaewon Kim if (adc == MAX77843_MUIC_ADC_OPEN) { 27227a28d32SJaewon Kim *attached = false; 27327a28d32SJaewon Kim cable_type = info->prev_cable_type; 27427a28d32SJaewon Kim info->prev_cable_type = MAX77843_MUIC_ADC_OPEN; 27527a28d32SJaewon Kim } else { 27627a28d32SJaewon Kim *attached = true; 27727a28d32SJaewon Kim cable_type = info->prev_cable_type = adc; 27827a28d32SJaewon Kim } 27927a28d32SJaewon Kim break; 28027a28d32SJaewon Kim case MAX77843_CABLE_GROUP_CHG: 28127a28d32SJaewon Kim chg_type = info->status[MAX77843_MUIC_STATUS2] & 28227a28d32SJaewon Kim MAX77843_MUIC_STATUS2_CHGTYP_MASK; 28327a28d32SJaewon Kim 28427a28d32SJaewon Kim /* Check GROUND accessory with charger cable */ 28527a28d32SJaewon Kim if (adc == MAX77843_MUIC_ADC_GROUND) { 28627a28d32SJaewon Kim if (chg_type == MAX77843_MUIC_CHG_NONE) { 287af57fa4dSSrikant Ritolia /* 288af57fa4dSSrikant Ritolia * The following state when charger cable is 28927a28d32SJaewon Kim * disconnected but the GROUND accessory still 290af57fa4dSSrikant Ritolia * connected. 291af57fa4dSSrikant Ritolia */ 29227a28d32SJaewon Kim *attached = false; 29327a28d32SJaewon Kim cable_type = info->prev_chg_type; 29427a28d32SJaewon Kim info->prev_chg_type = MAX77843_MUIC_CHG_NONE; 29527a28d32SJaewon Kim } else { 29627a28d32SJaewon Kim 297af57fa4dSSrikant Ritolia /* 298af57fa4dSSrikant Ritolia * The following state when charger cable is 299af57fa4dSSrikant Ritolia * connected on the GROUND accessory. 300af57fa4dSSrikant Ritolia */ 30127a28d32SJaewon Kim *attached = true; 30227a28d32SJaewon Kim cable_type = MAX77843_MUIC_CHG_GND; 30327a28d32SJaewon Kim info->prev_chg_type = MAX77843_MUIC_CHG_GND; 30427a28d32SJaewon Kim } 30527a28d32SJaewon Kim break; 30627a28d32SJaewon Kim } 30727a28d32SJaewon Kim 3084a4a8714SMarek Szyprowski if (adc == MAX77843_MUIC_ADC_RESERVED_ACC_3) { /* SmartDock */ 3094a4a8714SMarek Szyprowski if (chg_type == MAX77843_MUIC_CHG_NONE) { 3104a4a8714SMarek Szyprowski *attached = false; 3114a4a8714SMarek Szyprowski cable_type = info->prev_chg_type; 3124a4a8714SMarek Szyprowski info->prev_chg_type = MAX77843_MUIC_CHG_NONE; 3134a4a8714SMarek Szyprowski } else { 3144a4a8714SMarek Szyprowski *attached = true; 3154a4a8714SMarek Szyprowski cable_type = MAX77843_MUIC_CHG_DOCK; 3164a4a8714SMarek Szyprowski info->prev_chg_type = MAX77843_MUIC_CHG_DOCK; 3174a4a8714SMarek Szyprowski } 3184a4a8714SMarek Szyprowski break; 3194a4a8714SMarek Szyprowski } 3204a4a8714SMarek Szyprowski 32127a28d32SJaewon Kim if (chg_type == MAX77843_MUIC_CHG_NONE) { 32227a28d32SJaewon Kim *attached = false; 32327a28d32SJaewon Kim cable_type = info->prev_chg_type; 32427a28d32SJaewon Kim info->prev_chg_type = MAX77843_MUIC_CHG_NONE; 32527a28d32SJaewon Kim } else { 32627a28d32SJaewon Kim *attached = true; 32727a28d32SJaewon Kim cable_type = info->prev_chg_type = chg_type; 32827a28d32SJaewon Kim } 32927a28d32SJaewon Kim break; 33027a28d32SJaewon Kim case MAX77843_CABLE_GROUP_ADC_GND: 33127a28d32SJaewon Kim if (adc == MAX77843_MUIC_ADC_OPEN) { 33227a28d32SJaewon Kim *attached = false; 33327a28d32SJaewon Kim cable_type = info->prev_gnd_type; 33427a28d32SJaewon Kim info->prev_gnd_type = MAX77843_MUIC_ADC_OPEN; 33527a28d32SJaewon Kim } else { 33627a28d32SJaewon Kim *attached = true; 33727a28d32SJaewon Kim 338af57fa4dSSrikant Ritolia /* 339af57fa4dSSrikant Ritolia * Offset|ADC1K|VBVolt| 34027a28d32SJaewon Kim * 0x1| 0| 0| USB-HOST 34127a28d32SJaewon Kim * 0x1| 0| 1| USB-HOST with VB 34227a28d32SJaewon Kim * 0x1| 1| 0| MHL 343af57fa4dSSrikant Ritolia * 0x1| 1| 1| MHL with VB 344af57fa4dSSrikant Ritolia */ 34527a28d32SJaewon Kim /* Get ADC1K register bit */ 34627a28d32SJaewon Kim gnd_type = (info->status[MAX77843_MUIC_STATUS1] & 34727a28d32SJaewon Kim MAX77843_MUIC_STATUS1_ADC1K_MASK); 34827a28d32SJaewon Kim 34927a28d32SJaewon Kim /* Get VBVolt register bit */ 35027a28d32SJaewon Kim gnd_type |= (info->status[MAX77843_MUIC_STATUS2] & 35127a28d32SJaewon Kim MAX77843_MUIC_STATUS2_VBVOLT_MASK); 352309a3e00SKrzysztof Kozlowski gnd_type >>= MAX77843_MUIC_STATUS2_VBVOLT_SHIFT; 35327a28d32SJaewon Kim 35427a28d32SJaewon Kim /* Offset of GND cable */ 35527a28d32SJaewon Kim gnd_type |= MAX77843_MUIC_GND_USB_HOST; 35627a28d32SJaewon Kim cable_type = info->prev_gnd_type = gnd_type; 35727a28d32SJaewon Kim } 35827a28d32SJaewon Kim break; 35927a28d32SJaewon Kim default: 36027a28d32SJaewon Kim dev_err(info->dev, "Unknown cable group (%d)\n", group); 36127a28d32SJaewon Kim cable_type = -EINVAL; 36227a28d32SJaewon Kim break; 36327a28d32SJaewon Kim } 36427a28d32SJaewon Kim 36527a28d32SJaewon Kim return cable_type; 36627a28d32SJaewon Kim } 36727a28d32SJaewon Kim 36827a28d32SJaewon Kim static int max77843_muic_adc_gnd_handler(struct max77843_muic_info *info) 36927a28d32SJaewon Kim { 37027a28d32SJaewon Kim int ret, gnd_cable_type; 37127a28d32SJaewon Kim bool attached; 37227a28d32SJaewon Kim 37327a28d32SJaewon Kim gnd_cable_type = max77843_muic_get_cable_type(info, 37427a28d32SJaewon Kim MAX77843_CABLE_GROUP_ADC_GND, &attached); 37527a28d32SJaewon Kim dev_dbg(info->dev, "external connector is %s (gnd:0x%02x)\n", 37627a28d32SJaewon Kim attached ? "attached" : "detached", gnd_cable_type); 37727a28d32SJaewon Kim 37827a28d32SJaewon Kim switch (gnd_cable_type) { 37927a28d32SJaewon Kim case MAX77843_MUIC_GND_USB_HOST: 38027a28d32SJaewon Kim case MAX77843_MUIC_GND_USB_HOST_VB: 381309a3e00SKrzysztof Kozlowski ret = max77843_muic_set_path(info, 382309a3e00SKrzysztof Kozlowski MAX77843_MUIC_CONTROL1_SW_USB, 3834a4a8714SMarek Szyprowski attached, false); 38427a28d32SJaewon Kim if (ret < 0) 38527a28d32SJaewon Kim return ret; 38627a28d32SJaewon Kim 3878670b459SChanwoo Choi extcon_set_state_sync(info->edev, EXTCON_USB_HOST, attached); 3887b965110SMarek Szyprowski max77843_charger_set_otg_vbus(info, attached); 38927a28d32SJaewon Kim break; 39027a28d32SJaewon Kim case MAX77843_MUIC_GND_MHL_VB: 39127a28d32SJaewon Kim case MAX77843_MUIC_GND_MHL: 392309a3e00SKrzysztof Kozlowski ret = max77843_muic_set_path(info, 393309a3e00SKrzysztof Kozlowski MAX77843_MUIC_CONTROL1_SW_OPEN, 3944a4a8714SMarek Szyprowski attached, false); 39527a28d32SJaewon Kim if (ret < 0) 39627a28d32SJaewon Kim return ret; 39727a28d32SJaewon Kim 3988670b459SChanwoo Choi extcon_set_state_sync(info->edev, EXTCON_DISP_MHL, attached); 39927a28d32SJaewon Kim break; 40027a28d32SJaewon Kim default: 40127a28d32SJaewon Kim dev_err(info->dev, "failed to detect %s accessory(gnd:0x%x)\n", 40227a28d32SJaewon Kim attached ? "attached" : "detached", gnd_cable_type); 40327a28d32SJaewon Kim return -EINVAL; 40427a28d32SJaewon Kim } 40527a28d32SJaewon Kim 40627a28d32SJaewon Kim return 0; 40727a28d32SJaewon Kim } 40827a28d32SJaewon Kim 40927a28d32SJaewon Kim static int max77843_muic_jig_handler(struct max77843_muic_info *info, 41027a28d32SJaewon Kim int cable_type, bool attached) 41127a28d32SJaewon Kim { 41227a28d32SJaewon Kim int ret; 413309a3e00SKrzysztof Kozlowski u8 path = MAX77843_MUIC_CONTROL1_SW_OPEN; 41427a28d32SJaewon Kim 41527a28d32SJaewon Kim dev_dbg(info->dev, "external connector is %s (adc:0x%02x)\n", 41627a28d32SJaewon Kim attached ? "attached" : "detached", cable_type); 41727a28d32SJaewon Kim 41827a28d32SJaewon Kim switch (cable_type) { 41927a28d32SJaewon Kim case MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF: 42027a28d32SJaewon Kim case MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON: 421309a3e00SKrzysztof Kozlowski path = MAX77843_MUIC_CONTROL1_SW_USB; 42227a28d32SJaewon Kim break; 42327a28d32SJaewon Kim case MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF: 424309a3e00SKrzysztof Kozlowski path = MAX77843_MUIC_CONTROL1_SW_UART; 42527a28d32SJaewon Kim break; 42627a28d32SJaewon Kim default: 42741b3c015SChanwoo Choi return -EINVAL; 42841b3c015SChanwoo Choi } 42941b3c015SChanwoo Choi 4304a4a8714SMarek Szyprowski ret = max77843_muic_set_path(info, path, attached, false); 43127a28d32SJaewon Kim if (ret < 0) 43227a28d32SJaewon Kim return ret; 43341b3c015SChanwoo Choi 4348670b459SChanwoo Choi extcon_set_state_sync(info->edev, EXTCON_JIG, attached); 43527a28d32SJaewon Kim 43627a28d32SJaewon Kim return 0; 43727a28d32SJaewon Kim } 43827a28d32SJaewon Kim 4394a4a8714SMarek Szyprowski static int max77843_muic_dock_handler(struct max77843_muic_info *info, 4404a4a8714SMarek Szyprowski bool attached) 4414a4a8714SMarek Szyprowski { 4424a4a8714SMarek Szyprowski int ret; 4434a4a8714SMarek Szyprowski 4444a4a8714SMarek Szyprowski dev_dbg(info->dev, "external connector is %s (adc: 0x10)\n", 4454a4a8714SMarek Szyprowski attached ? "attached" : "detached"); 4464a4a8714SMarek Szyprowski 4474a4a8714SMarek Szyprowski ret = max77843_muic_set_path(info, MAX77843_MUIC_CONTROL1_SW_USB, 4484a4a8714SMarek Szyprowski attached, attached); 4494a4a8714SMarek Szyprowski if (ret < 0) 4504a4a8714SMarek Szyprowski return ret; 4514a4a8714SMarek Szyprowski 4524a4a8714SMarek Szyprowski extcon_set_state_sync(info->edev, EXTCON_DISP_MHL, attached); 4534a4a8714SMarek Szyprowski extcon_set_state_sync(info->edev, EXTCON_USB_HOST, attached); 4544a4a8714SMarek Szyprowski extcon_set_state_sync(info->edev, EXTCON_DOCK, attached); 4554a4a8714SMarek Szyprowski 4564a4a8714SMarek Szyprowski return 0; 4574a4a8714SMarek Szyprowski } 4584a4a8714SMarek Szyprowski 45927a28d32SJaewon Kim static int max77843_muic_adc_handler(struct max77843_muic_info *info) 46027a28d32SJaewon Kim { 46127a28d32SJaewon Kim int ret, cable_type; 46227a28d32SJaewon Kim bool attached; 46327a28d32SJaewon Kim 46427a28d32SJaewon Kim cable_type = max77843_muic_get_cable_type(info, 46527a28d32SJaewon Kim MAX77843_CABLE_GROUP_ADC, &attached); 46627a28d32SJaewon Kim 46727a28d32SJaewon Kim dev_dbg(info->dev, 46827a28d32SJaewon Kim "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n", 46927a28d32SJaewon Kim attached ? "attached" : "detached", cable_type, 47027a28d32SJaewon Kim info->prev_cable_type); 47127a28d32SJaewon Kim 47227a28d32SJaewon Kim switch (cable_type) { 4734a4a8714SMarek Szyprowski case MAX77843_MUIC_ADC_RESERVED_ACC_3: /* SmartDock */ 4744a4a8714SMarek Szyprowski ret = max77843_muic_dock_handler(info, attached); 4754a4a8714SMarek Szyprowski if (ret < 0) 4764a4a8714SMarek Szyprowski return ret; 4774a4a8714SMarek Szyprowski break; 47827a28d32SJaewon Kim case MAX77843_MUIC_ADC_GROUND: 47927a28d32SJaewon Kim ret = max77843_muic_adc_gnd_handler(info); 48027a28d32SJaewon Kim if (ret < 0) 48127a28d32SJaewon Kim return ret; 48227a28d32SJaewon Kim break; 48327a28d32SJaewon Kim case MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF: 48427a28d32SJaewon Kim case MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON: 48527a28d32SJaewon Kim case MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF: 48627a28d32SJaewon Kim ret = max77843_muic_jig_handler(info, cable_type, attached); 48727a28d32SJaewon Kim if (ret < 0) 48827a28d32SJaewon Kim return ret; 48927a28d32SJaewon Kim break; 49027a28d32SJaewon Kim case MAX77843_MUIC_ADC_SEND_END_BUTTON: 49127a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S1_BUTTON: 49227a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S2_BUTTON: 49327a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S3_BUTTON: 49427a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S4_BUTTON: 49527a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S5_BUTTON: 49627a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S6_BUTTON: 49727a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S7_BUTTON: 49827a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S8_BUTTON: 49927a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S9_BUTTON: 50027a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S10_BUTTON: 50127a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S11_BUTTON: 50227a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S12_BUTTON: 50327a28d32SJaewon Kim case MAX77843_MUIC_ADC_RESERVED_ACC_1: 50427a28d32SJaewon Kim case MAX77843_MUIC_ADC_RESERVED_ACC_2: 50527a28d32SJaewon Kim case MAX77843_MUIC_ADC_RESERVED_ACC_4: 50627a28d32SJaewon Kim case MAX77843_MUIC_ADC_RESERVED_ACC_5: 50727a28d32SJaewon Kim case MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE2: 50827a28d32SJaewon Kim case MAX77843_MUIC_ADC_PHONE_POWERED_DEV: 50927a28d32SJaewon Kim case MAX77843_MUIC_ADC_TTY_CONVERTER: 51027a28d32SJaewon Kim case MAX77843_MUIC_ADC_UART_CABLE: 51127a28d32SJaewon Kim case MAX77843_MUIC_ADC_CEA936A_TYPE1_CHG: 51227a28d32SJaewon Kim case MAX77843_MUIC_ADC_AV_CABLE_NOLOAD: 51327a28d32SJaewon Kim case MAX77843_MUIC_ADC_CEA936A_TYPE2_CHG: 51427a28d32SJaewon Kim case MAX77843_MUIC_ADC_FACTORY_MODE_UART_ON: 51527a28d32SJaewon Kim case MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE1: 51627a28d32SJaewon Kim case MAX77843_MUIC_ADC_OPEN: 51727a28d32SJaewon Kim dev_err(info->dev, 51827a28d32SJaewon Kim "accessory is %s but it isn't used (adc:0x%x)\n", 51927a28d32SJaewon Kim attached ? "attached" : "detached", cable_type); 52027a28d32SJaewon Kim return -EAGAIN; 52127a28d32SJaewon Kim default: 52227a28d32SJaewon Kim dev_err(info->dev, 52327a28d32SJaewon Kim "failed to detect %s accessory (adc:0x%x)\n", 52427a28d32SJaewon Kim attached ? "attached" : "detached", cable_type); 52527a28d32SJaewon Kim return -EINVAL; 52627a28d32SJaewon Kim } 52727a28d32SJaewon Kim 52827a28d32SJaewon Kim return 0; 52927a28d32SJaewon Kim } 53027a28d32SJaewon Kim 53127a28d32SJaewon Kim static int max77843_muic_chg_handler(struct max77843_muic_info *info) 53227a28d32SJaewon Kim { 53327a28d32SJaewon Kim int ret, chg_type, gnd_type; 53427a28d32SJaewon Kim bool attached; 53527a28d32SJaewon Kim 53627a28d32SJaewon Kim chg_type = max77843_muic_get_cable_type(info, 53727a28d32SJaewon Kim MAX77843_CABLE_GROUP_CHG, &attached); 53827a28d32SJaewon Kim 53927a28d32SJaewon Kim dev_dbg(info->dev, 54027a28d32SJaewon Kim "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n", 54127a28d32SJaewon Kim attached ? "attached" : "detached", 54227a28d32SJaewon Kim chg_type, info->prev_chg_type); 54327a28d32SJaewon Kim 54427a28d32SJaewon Kim switch (chg_type) { 54527a28d32SJaewon Kim case MAX77843_MUIC_CHG_USB: 546309a3e00SKrzysztof Kozlowski ret = max77843_muic_set_path(info, 547309a3e00SKrzysztof Kozlowski MAX77843_MUIC_CONTROL1_SW_USB, 5484a4a8714SMarek Szyprowski attached, false); 54927a28d32SJaewon Kim if (ret < 0) 55027a28d32SJaewon Kim return ret; 55127a28d32SJaewon Kim 5528670b459SChanwoo Choi extcon_set_state_sync(info->edev, EXTCON_USB, attached); 5538670b459SChanwoo Choi extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SDP, 5548b45b6a0SChanwoo Choi attached); 55527a28d32SJaewon Kim break; 55627a28d32SJaewon Kim case MAX77843_MUIC_CHG_DOWNSTREAM: 557309a3e00SKrzysztof Kozlowski ret = max77843_muic_set_path(info, 558309a3e00SKrzysztof Kozlowski MAX77843_MUIC_CONTROL1_SW_OPEN, 5594a4a8714SMarek Szyprowski attached, false); 56027a28d32SJaewon Kim if (ret < 0) 56127a28d32SJaewon Kim return ret; 56227a28d32SJaewon Kim 5638670b459SChanwoo Choi extcon_set_state_sync(info->edev, EXTCON_CHG_USB_CDP, 5642a9de9c0SChanwoo Choi attached); 56527a28d32SJaewon Kim break; 56627a28d32SJaewon Kim case MAX77843_MUIC_CHG_DEDICATED: 567309a3e00SKrzysztof Kozlowski ret = max77843_muic_set_path(info, 568309a3e00SKrzysztof Kozlowski MAX77843_MUIC_CONTROL1_SW_OPEN, 5694a4a8714SMarek Szyprowski attached, false); 57027a28d32SJaewon Kim if (ret < 0) 57127a28d32SJaewon Kim return ret; 57227a28d32SJaewon Kim 5738670b459SChanwoo Choi extcon_set_state_sync(info->edev, EXTCON_CHG_USB_DCP, 57411eecf91SChanwoo Choi attached); 57527a28d32SJaewon Kim break; 57627a28d32SJaewon Kim case MAX77843_MUIC_CHG_SPECIAL_500MA: 577309a3e00SKrzysztof Kozlowski ret = max77843_muic_set_path(info, 578309a3e00SKrzysztof Kozlowski MAX77843_MUIC_CONTROL1_SW_OPEN, 5794a4a8714SMarek Szyprowski attached, false); 58027a28d32SJaewon Kim if (ret < 0) 58127a28d32SJaewon Kim return ret; 58227a28d32SJaewon Kim 5838670b459SChanwoo Choi extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SLOW, 5842a9de9c0SChanwoo Choi attached); 58527a28d32SJaewon Kim break; 58627a28d32SJaewon Kim case MAX77843_MUIC_CHG_SPECIAL_1A: 587309a3e00SKrzysztof Kozlowski ret = max77843_muic_set_path(info, 588309a3e00SKrzysztof Kozlowski MAX77843_MUIC_CONTROL1_SW_OPEN, 5894a4a8714SMarek Szyprowski attached, false); 59027a28d32SJaewon Kim if (ret < 0) 59127a28d32SJaewon Kim return ret; 59227a28d32SJaewon Kim 5938670b459SChanwoo Choi extcon_set_state_sync(info->edev, EXTCON_CHG_USB_FAST, 5942a9de9c0SChanwoo Choi attached); 59527a28d32SJaewon Kim break; 59627a28d32SJaewon Kim case MAX77843_MUIC_CHG_GND: 59727a28d32SJaewon Kim gnd_type = max77843_muic_get_cable_type(info, 59827a28d32SJaewon Kim MAX77843_CABLE_GROUP_ADC_GND, &attached); 59927a28d32SJaewon Kim 60027a28d32SJaewon Kim /* Charger cable on MHL accessory is attach or detach */ 60127a28d32SJaewon Kim if (gnd_type == MAX77843_MUIC_GND_MHL_VB) 6028670b459SChanwoo Choi extcon_set_state_sync(info->edev, EXTCON_CHG_USB_DCP, 60311eecf91SChanwoo Choi true); 60427a28d32SJaewon Kim else if (gnd_type == MAX77843_MUIC_GND_MHL) 6058670b459SChanwoo Choi extcon_set_state_sync(info->edev, EXTCON_CHG_USB_DCP, 60611eecf91SChanwoo Choi false); 60727a28d32SJaewon Kim break; 6084a4a8714SMarek Szyprowski case MAX77843_MUIC_CHG_DOCK: 6094a4a8714SMarek Szyprowski extcon_set_state_sync(info->edev, EXTCON_CHG_USB_DCP, attached); 6104a4a8714SMarek Szyprowski break; 61127a28d32SJaewon Kim case MAX77843_MUIC_CHG_NONE: 61227a28d32SJaewon Kim break; 61327a28d32SJaewon Kim default: 61427a28d32SJaewon Kim dev_err(info->dev, 61527a28d32SJaewon Kim "failed to detect %s accessory (chg_type:0x%x)\n", 61627a28d32SJaewon Kim attached ? "attached" : "detached", chg_type); 61727a28d32SJaewon Kim 618309a3e00SKrzysztof Kozlowski max77843_muic_set_path(info, MAX77843_MUIC_CONTROL1_SW_OPEN, 6194a4a8714SMarek Szyprowski attached, false); 62027a28d32SJaewon Kim return -EINVAL; 62127a28d32SJaewon Kim } 62227a28d32SJaewon Kim 62327a28d32SJaewon Kim return 0; 62427a28d32SJaewon Kim } 62527a28d32SJaewon Kim 62627a28d32SJaewon Kim static void max77843_muic_irq_work(struct work_struct *work) 62727a28d32SJaewon Kim { 62827a28d32SJaewon Kim struct max77843_muic_info *info = container_of(work, 62927a28d32SJaewon Kim struct max77843_muic_info, irq_work); 630bc1aadc1SKrzysztof Kozlowski struct max77693_dev *max77843 = info->max77843; 63127a28d32SJaewon Kim int ret = 0; 63227a28d32SJaewon Kim 63327a28d32SJaewon Kim mutex_lock(&info->mutex); 63427a28d32SJaewon Kim 63527a28d32SJaewon Kim ret = regmap_bulk_read(max77843->regmap_muic, 63627a28d32SJaewon Kim MAX77843_MUIC_REG_STATUS1, info->status, 63727a28d32SJaewon Kim MAX77843_MUIC_STATUS_NUM); 63827a28d32SJaewon Kim if (ret) { 63927a28d32SJaewon Kim dev_err(info->dev, "Cannot read STATUS registers\n"); 64027a28d32SJaewon Kim mutex_unlock(&info->mutex); 64127a28d32SJaewon Kim return; 64227a28d32SJaewon Kim } 64327a28d32SJaewon Kim 64427a28d32SJaewon Kim if (info->irq_adc) { 64527a28d32SJaewon Kim ret = max77843_muic_adc_handler(info); 64627a28d32SJaewon Kim if (ret) 64727a28d32SJaewon Kim dev_err(info->dev, "Unknown cable type\n"); 64827a28d32SJaewon Kim info->irq_adc = false; 64927a28d32SJaewon Kim } 65027a28d32SJaewon Kim 65127a28d32SJaewon Kim if (info->irq_chg) { 65227a28d32SJaewon Kim ret = max77843_muic_chg_handler(info); 65327a28d32SJaewon Kim if (ret) 65427a28d32SJaewon Kim dev_err(info->dev, "Unknown charger type\n"); 65527a28d32SJaewon Kim info->irq_chg = false; 65627a28d32SJaewon Kim } 65727a28d32SJaewon Kim 65827a28d32SJaewon Kim mutex_unlock(&info->mutex); 65927a28d32SJaewon Kim } 66027a28d32SJaewon Kim 66127a28d32SJaewon Kim static irqreturn_t max77843_muic_irq_handler(int irq, void *data) 66227a28d32SJaewon Kim { 66327a28d32SJaewon Kim struct max77843_muic_info *info = data; 66427a28d32SJaewon Kim int i, irq_type = -1; 66527a28d32SJaewon Kim 66627a28d32SJaewon Kim for (i = 0; i < ARRAY_SIZE(max77843_muic_irqs); i++) 66727a28d32SJaewon Kim if (irq == max77843_muic_irqs[i].virq) 66827a28d32SJaewon Kim irq_type = max77843_muic_irqs[i].irq; 66927a28d32SJaewon Kim 67027a28d32SJaewon Kim switch (irq_type) { 67127a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT1_ADC: 67227a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT1_ADCERROR: 67327a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT1_ADC1K: 67427a28d32SJaewon Kim info->irq_adc = true; 67527a28d32SJaewon Kim break; 67627a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT2_CHGTYP: 67727a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT2_CHGDETRUN: 67827a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT2_DCDTMR: 67927a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT2_DXOVP: 68027a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT2_VBVOLT: 68127a28d32SJaewon Kim info->irq_chg = true; 68227a28d32SJaewon Kim break; 68327a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT3_VBADC: 68427a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT3_VDNMON: 68527a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT3_DNRES: 68627a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT3_MPNACK: 68727a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT3_MRXBUFOW: 68827a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT3_MRXTRF: 68927a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT3_MRXPERR: 69027a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT3_MRXRDY: 69127a28d32SJaewon Kim break; 69227a28d32SJaewon Kim default: 69327a28d32SJaewon Kim dev_err(info->dev, "Cannot recognize IRQ(%d)\n", irq_type); 69427a28d32SJaewon Kim break; 69527a28d32SJaewon Kim } 69627a28d32SJaewon Kim 69727a28d32SJaewon Kim schedule_work(&info->irq_work); 69827a28d32SJaewon Kim 69927a28d32SJaewon Kim return IRQ_HANDLED; 70027a28d32SJaewon Kim } 70127a28d32SJaewon Kim 70227a28d32SJaewon Kim static void max77843_muic_detect_cable_wq(struct work_struct *work) 70327a28d32SJaewon Kim { 70427a28d32SJaewon Kim struct max77843_muic_info *info = container_of(to_delayed_work(work), 70527a28d32SJaewon Kim struct max77843_muic_info, wq_detcable); 706bc1aadc1SKrzysztof Kozlowski struct max77693_dev *max77843 = info->max77843; 70727a28d32SJaewon Kim int chg_type, adc, ret; 70827a28d32SJaewon Kim bool attached; 70927a28d32SJaewon Kim 71027a28d32SJaewon Kim mutex_lock(&info->mutex); 71127a28d32SJaewon Kim 71227a28d32SJaewon Kim ret = regmap_bulk_read(max77843->regmap_muic, 71327a28d32SJaewon Kim MAX77843_MUIC_REG_STATUS1, info->status, 71427a28d32SJaewon Kim MAX77843_MUIC_STATUS_NUM); 71527a28d32SJaewon Kim if (ret) { 71627a28d32SJaewon Kim dev_err(info->dev, "Cannot read STATUS registers\n"); 71727a28d32SJaewon Kim goto err_cable_wq; 71827a28d32SJaewon Kim } 71927a28d32SJaewon Kim 72027a28d32SJaewon Kim adc = max77843_muic_get_cable_type(info, 72127a28d32SJaewon Kim MAX77843_CABLE_GROUP_ADC, &attached); 72227a28d32SJaewon Kim if (attached && adc != MAX77843_MUIC_ADC_OPEN) { 72327a28d32SJaewon Kim ret = max77843_muic_adc_handler(info); 72427a28d32SJaewon Kim if (ret < 0) { 72527a28d32SJaewon Kim dev_err(info->dev, "Cannot detect accessory\n"); 72627a28d32SJaewon Kim goto err_cable_wq; 72727a28d32SJaewon Kim } 72827a28d32SJaewon Kim } 72927a28d32SJaewon Kim 73027a28d32SJaewon Kim chg_type = max77843_muic_get_cable_type(info, 73127a28d32SJaewon Kim MAX77843_CABLE_GROUP_CHG, &attached); 73227a28d32SJaewon Kim if (attached && chg_type != MAX77843_MUIC_CHG_NONE) { 73327a28d32SJaewon Kim ret = max77843_muic_chg_handler(info); 73427a28d32SJaewon Kim if (ret < 0) { 73527a28d32SJaewon Kim dev_err(info->dev, "Cannot detect charger accessory\n"); 73627a28d32SJaewon Kim goto err_cable_wq; 73727a28d32SJaewon Kim } 73827a28d32SJaewon Kim } 73927a28d32SJaewon Kim 74027a28d32SJaewon Kim err_cable_wq: 74127a28d32SJaewon Kim mutex_unlock(&info->mutex); 74227a28d32SJaewon Kim } 74327a28d32SJaewon Kim 74427a28d32SJaewon Kim static int max77843_muic_set_debounce_time(struct max77843_muic_info *info, 74527a28d32SJaewon Kim enum max77843_muic_adc_debounce_time time) 74627a28d32SJaewon Kim { 747bc1aadc1SKrzysztof Kozlowski struct max77693_dev *max77843 = info->max77843; 748d927c590SDan Carpenter int ret; 74927a28d32SJaewon Kim 75027a28d32SJaewon Kim switch (time) { 75127a28d32SJaewon Kim case MAX77843_DEBOUNCE_TIME_5MS: 75227a28d32SJaewon Kim case MAX77843_DEBOUNCE_TIME_10MS: 75327a28d32SJaewon Kim case MAX77843_DEBOUNCE_TIME_25MS: 75427a28d32SJaewon Kim case MAX77843_DEBOUNCE_TIME_38_62MS: 75527a28d32SJaewon Kim ret = regmap_update_bits(max77843->regmap_muic, 75627a28d32SJaewon Kim MAX77843_MUIC_REG_CONTROL4, 75727a28d32SJaewon Kim MAX77843_MUIC_CONTROL4_ADCDBSET_MASK, 758309a3e00SKrzysztof Kozlowski time << MAX77843_MUIC_CONTROL4_ADCDBSET_SHIFT); 75927a28d32SJaewon Kim if (ret < 0) { 76027a28d32SJaewon Kim dev_err(info->dev, "Cannot write MUIC regmap\n"); 76127a28d32SJaewon Kim return ret; 76227a28d32SJaewon Kim } 76327a28d32SJaewon Kim break; 76427a28d32SJaewon Kim default: 76527a28d32SJaewon Kim dev_err(info->dev, "Invalid ADC debounce time\n"); 76627a28d32SJaewon Kim return -EINVAL; 76727a28d32SJaewon Kim } 76827a28d32SJaewon Kim 76927a28d32SJaewon Kim return 0; 77027a28d32SJaewon Kim } 77127a28d32SJaewon Kim 772bc1aadc1SKrzysztof Kozlowski static int max77843_init_muic_regmap(struct max77693_dev *max77843) 77327a28d32SJaewon Kim { 77427a28d32SJaewon Kim int ret; 77527a28d32SJaewon Kim 776a6d9cfcbSWolfram Sang max77843->i2c_muic = i2c_new_dummy_device(max77843->i2c->adapter, 77727a28d32SJaewon Kim I2C_ADDR_MUIC); 778a6d9cfcbSWolfram Sang if (IS_ERR(max77843->i2c_muic)) { 77927a28d32SJaewon Kim dev_err(&max77843->i2c->dev, 78027a28d32SJaewon Kim "Cannot allocate I2C device for MUIC\n"); 781a6d9cfcbSWolfram Sang return PTR_ERR(max77843->i2c_muic); 78227a28d32SJaewon Kim } 78327a28d32SJaewon Kim 78427a28d32SJaewon Kim i2c_set_clientdata(max77843->i2c_muic, max77843); 78527a28d32SJaewon Kim 78627a28d32SJaewon Kim max77843->regmap_muic = devm_regmap_init_i2c(max77843->i2c_muic, 78727a28d32SJaewon Kim &max77843_muic_regmap_config); 78827a28d32SJaewon Kim if (IS_ERR(max77843->regmap_muic)) { 78927a28d32SJaewon Kim ret = PTR_ERR(max77843->regmap_muic); 79027a28d32SJaewon Kim goto err_muic_i2c; 79127a28d32SJaewon Kim } 79227a28d32SJaewon Kim 79327a28d32SJaewon Kim ret = regmap_add_irq_chip(max77843->regmap_muic, max77843->irq, 79427a28d32SJaewon Kim IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED, 79527a28d32SJaewon Kim 0, &max77843_muic_irq_chip, &max77843->irq_data_muic); 79627a28d32SJaewon Kim if (ret < 0) { 79727a28d32SJaewon Kim dev_err(&max77843->i2c->dev, "Cannot add MUIC IRQ chip\n"); 79827a28d32SJaewon Kim goto err_muic_i2c; 79927a28d32SJaewon Kim } 80027a28d32SJaewon Kim 80127a28d32SJaewon Kim return 0; 80227a28d32SJaewon Kim 80327a28d32SJaewon Kim err_muic_i2c: 80427a28d32SJaewon Kim i2c_unregister_device(max77843->i2c_muic); 80527a28d32SJaewon Kim 80627a28d32SJaewon Kim return ret; 80727a28d32SJaewon Kim } 80827a28d32SJaewon Kim 80927a28d32SJaewon Kim static int max77843_muic_probe(struct platform_device *pdev) 81027a28d32SJaewon Kim { 811bc1aadc1SKrzysztof Kozlowski struct max77693_dev *max77843 = dev_get_drvdata(pdev->dev.parent); 81227a28d32SJaewon Kim struct max77843_muic_info *info; 81327a28d32SJaewon Kim unsigned int id; 814d9204acbSMarek Szyprowski int cable_type; 815d9204acbSMarek Szyprowski bool attached; 81627a28d32SJaewon Kim int i, ret; 81727a28d32SJaewon Kim 81827a28d32SJaewon Kim info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); 81927a28d32SJaewon Kim if (!info) 82027a28d32SJaewon Kim return -ENOMEM; 82127a28d32SJaewon Kim 82227a28d32SJaewon Kim info->dev = &pdev->dev; 82327a28d32SJaewon Kim info->max77843 = max77843; 82427a28d32SJaewon Kim 82527a28d32SJaewon Kim platform_set_drvdata(pdev, info); 82627a28d32SJaewon Kim mutex_init(&info->mutex); 82727a28d32SJaewon Kim 82827a28d32SJaewon Kim /* Initialize i2c and regmap */ 82927a28d32SJaewon Kim ret = max77843_init_muic_regmap(max77843); 83027a28d32SJaewon Kim if (ret) { 83127a28d32SJaewon Kim dev_err(&pdev->dev, "Failed to init MUIC regmap\n"); 83227a28d32SJaewon Kim return ret; 83327a28d32SJaewon Kim } 83427a28d32SJaewon Kim 83527a28d32SJaewon Kim /* Turn off auto detection configuration */ 83627a28d32SJaewon Kim ret = regmap_update_bits(max77843->regmap_muic, 83727a28d32SJaewon Kim MAX77843_MUIC_REG_CONTROL4, 83827a28d32SJaewon Kim MAX77843_MUIC_CONTROL4_USBAUTO_MASK | 83927a28d32SJaewon Kim MAX77843_MUIC_CONTROL4_FCTAUTO_MASK, 84027a28d32SJaewon Kim CONTROL4_AUTO_DISABLE); 84127a28d32SJaewon Kim 84227a28d32SJaewon Kim /* Initialize extcon device */ 84327a28d32SJaewon Kim info->edev = devm_extcon_dev_allocate(&pdev->dev, 84427a28d32SJaewon Kim max77843_extcon_cable); 84527a28d32SJaewon Kim if (IS_ERR(info->edev)) { 84627a28d32SJaewon Kim dev_err(&pdev->dev, "Failed to allocate memory for extcon\n"); 847a4cf11faSKrzysztof Kozlowski ret = PTR_ERR(info->edev); 84827a28d32SJaewon Kim goto err_muic_irq; 84927a28d32SJaewon Kim } 85027a28d32SJaewon Kim 85127a28d32SJaewon Kim ret = devm_extcon_dev_register(&pdev->dev, info->edev); 85227a28d32SJaewon Kim if (ret) { 85327a28d32SJaewon Kim dev_err(&pdev->dev, "Failed to register extcon device\n"); 85427a28d32SJaewon Kim goto err_muic_irq; 85527a28d32SJaewon Kim } 85627a28d32SJaewon Kim 85727a28d32SJaewon Kim /* Set ADC debounce time */ 85827a28d32SJaewon Kim max77843_muic_set_debounce_time(info, MAX77843_DEBOUNCE_TIME_25MS); 85927a28d32SJaewon Kim 860d9204acbSMarek Szyprowski /* Set initial path for UART when JIG is connected to get serial logs */ 861d9204acbSMarek Szyprowski ret = regmap_bulk_read(max77843->regmap_muic, 862d9204acbSMarek Szyprowski MAX77843_MUIC_REG_STATUS1, info->status, 863d9204acbSMarek Szyprowski MAX77843_MUIC_STATUS_NUM); 864d9204acbSMarek Szyprowski if (ret) { 865d9204acbSMarek Szyprowski dev_err(info->dev, "Cannot read STATUS registers\n"); 866d9204acbSMarek Szyprowski goto err_muic_irq; 867d9204acbSMarek Szyprowski } 868d9204acbSMarek Szyprowski cable_type = max77843_muic_get_cable_type(info, MAX77843_CABLE_GROUP_ADC, 869d9204acbSMarek Szyprowski &attached); 870d9204acbSMarek Szyprowski if (attached && cable_type == MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF) 871d9204acbSMarek Szyprowski max77843_muic_set_path(info, MAX77843_MUIC_CONTROL1_SW_UART, 872d9204acbSMarek Szyprowski true, false); 87327a28d32SJaewon Kim 87427a28d32SJaewon Kim /* Check revision number of MUIC device */ 87527a28d32SJaewon Kim ret = regmap_read(max77843->regmap_muic, MAX77843_MUIC_REG_ID, &id); 87627a28d32SJaewon Kim if (ret < 0) { 87727a28d32SJaewon Kim dev_err(&pdev->dev, "Failed to read revision number\n"); 87827a28d32SJaewon Kim goto err_muic_irq; 87927a28d32SJaewon Kim } 88027a28d32SJaewon Kim dev_info(info->dev, "MUIC device ID : 0x%x\n", id); 88127a28d32SJaewon Kim 88227a28d32SJaewon Kim /* Support virtual irq domain for max77843 MUIC device */ 88327a28d32SJaewon Kim INIT_WORK(&info->irq_work, max77843_muic_irq_work); 88427a28d32SJaewon Kim 885135d9f7dSJaewon Kim /* Clear IRQ bits before request IRQs */ 886135d9f7dSJaewon Kim ret = regmap_bulk_read(max77843->regmap_muic, 887135d9f7dSJaewon Kim MAX77843_MUIC_REG_INT1, info->status, 888c4924e92SDan Carpenter MAX77843_MUIC_STATUS_NUM); 889135d9f7dSJaewon Kim if (ret) { 890135d9f7dSJaewon Kim dev_err(&pdev->dev, "Failed to Clear IRQ bits\n"); 891135d9f7dSJaewon Kim goto err_muic_irq; 892135d9f7dSJaewon Kim } 893135d9f7dSJaewon Kim 89427a28d32SJaewon Kim for (i = 0; i < ARRAY_SIZE(max77843_muic_irqs); i++) { 89527a28d32SJaewon Kim struct max77843_muic_irq *muic_irq = &max77843_muic_irqs[i]; 896c05c0d54SAndrzej Hajda int virq = 0; 89727a28d32SJaewon Kim 89827a28d32SJaewon Kim virq = regmap_irq_get_virq(max77843->irq_data_muic, 89927a28d32SJaewon Kim muic_irq->irq); 90027a28d32SJaewon Kim if (virq <= 0) { 90127a28d32SJaewon Kim ret = -EINVAL; 90227a28d32SJaewon Kim goto err_muic_irq; 90327a28d32SJaewon Kim } 90427a28d32SJaewon Kim muic_irq->virq = virq; 90527a28d32SJaewon Kim 90627a28d32SJaewon Kim ret = devm_request_threaded_irq(&pdev->dev, virq, NULL, 90727a28d32SJaewon Kim max77843_muic_irq_handler, IRQF_NO_SUSPEND, 90827a28d32SJaewon Kim muic_irq->name, info); 90927a28d32SJaewon Kim if (ret) { 91027a28d32SJaewon Kim dev_err(&pdev->dev, 91127a28d32SJaewon Kim "Failed to request irq (IRQ: %d, error: %d)\n", 91227a28d32SJaewon Kim muic_irq->irq, ret); 91327a28d32SJaewon Kim goto err_muic_irq; 91427a28d32SJaewon Kim } 91527a28d32SJaewon Kim } 91627a28d32SJaewon Kim 91727a28d32SJaewon Kim /* Detect accessory after completing the initialization of platform */ 91827a28d32SJaewon Kim INIT_DELAYED_WORK(&info->wq_detcable, max77843_muic_detect_cable_wq); 91927a28d32SJaewon Kim queue_delayed_work(system_power_efficient_wq, 92027a28d32SJaewon Kim &info->wq_detcable, msecs_to_jiffies(DELAY_MS_DEFAULT)); 92127a28d32SJaewon Kim 92227a28d32SJaewon Kim return 0; 92327a28d32SJaewon Kim 92427a28d32SJaewon Kim err_muic_irq: 92527a28d32SJaewon Kim regmap_del_irq_chip(max77843->irq, max77843->irq_data_muic); 92627a28d32SJaewon Kim i2c_unregister_device(max77843->i2c_muic); 92727a28d32SJaewon Kim 92827a28d32SJaewon Kim return ret; 92927a28d32SJaewon Kim } 93027a28d32SJaewon Kim 931*9688b1d0SUwe Kleine-König static void max77843_muic_remove(struct platform_device *pdev) 93227a28d32SJaewon Kim { 93327a28d32SJaewon Kim struct max77843_muic_info *info = platform_get_drvdata(pdev); 934bc1aadc1SKrzysztof Kozlowski struct max77693_dev *max77843 = info->max77843; 93527a28d32SJaewon Kim 93627a28d32SJaewon Kim cancel_work_sync(&info->irq_work); 93727a28d32SJaewon Kim regmap_del_irq_chip(max77843->irq, max77843->irq_data_muic); 93827a28d32SJaewon Kim i2c_unregister_device(max77843->i2c_muic); 93927a28d32SJaewon Kim } 94027a28d32SJaewon Kim 94127a28d32SJaewon Kim static const struct platform_device_id max77843_muic_id[] = { 94227a28d32SJaewon Kim { "max77843-muic", }, 94327a28d32SJaewon Kim { /* sentinel */ }, 94427a28d32SJaewon Kim }; 94527a28d32SJaewon Kim MODULE_DEVICE_TABLE(platform, max77843_muic_id); 94627a28d32SJaewon Kim 947da886ba8SMarek Szyprowski static const struct of_device_id of_max77843_muic_dt_match[] = { 948da886ba8SMarek Szyprowski { .compatible = "maxim,max77843-muic", }, 949da886ba8SMarek Szyprowski { /* sentinel */ }, 950da886ba8SMarek Szyprowski }; 951da886ba8SMarek Szyprowski MODULE_DEVICE_TABLE(of, of_max77843_muic_dt_match); 952da886ba8SMarek Szyprowski 95327a28d32SJaewon Kim static struct platform_driver max77843_muic_driver = { 95427a28d32SJaewon Kim .driver = { 95527a28d32SJaewon Kim .name = "max77843-muic", 956da886ba8SMarek Szyprowski .of_match_table = of_max77843_muic_dt_match, 95727a28d32SJaewon Kim }, 95827a28d32SJaewon Kim .probe = max77843_muic_probe, 959*9688b1d0SUwe Kleine-König .remove_new = max77843_muic_remove, 96027a28d32SJaewon Kim .id_table = max77843_muic_id, 96127a28d32SJaewon Kim }; 96227a28d32SJaewon Kim 96327a28d32SJaewon Kim static int __init max77843_muic_init(void) 96427a28d32SJaewon Kim { 96527a28d32SJaewon Kim return platform_driver_register(&max77843_muic_driver); 96627a28d32SJaewon Kim } 96727a28d32SJaewon Kim subsys_initcall(max77843_muic_init); 96827a28d32SJaewon Kim 96927a28d32SJaewon Kim MODULE_DESCRIPTION("Maxim MAX77843 Extcon driver"); 97027a28d32SJaewon Kim MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>"); 97127a28d32SJaewon Kim MODULE_LICENSE("GPL"); 972