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, 19227a28d32SJaewon Kim .mask_base = MAX77843_MUIC_REG_INTMASK1, 19327a28d32SJaewon Kim .mask_invert = true, 19427a28d32SJaewon Kim .num_regs = 3, 19527a28d32SJaewon Kim .irqs = max77843_muic_irq, 19627a28d32SJaewon Kim .num_irqs = ARRAY_SIZE(max77843_muic_irq), 19727a28d32SJaewon Kim }; 19827a28d32SJaewon Kim 19927a28d32SJaewon Kim static int max77843_muic_set_path(struct max77843_muic_info *info, 2004a4a8714SMarek Szyprowski u8 val, bool attached, bool nobccomp) 20127a28d32SJaewon Kim { 202bc1aadc1SKrzysztof Kozlowski struct max77693_dev *max77843 = info->max77843; 20327a28d32SJaewon Kim int ret = 0; 20427a28d32SJaewon Kim unsigned int ctrl1, ctrl2; 20527a28d32SJaewon Kim 20627a28d32SJaewon Kim if (attached) 20727a28d32SJaewon Kim ctrl1 = val; 20827a28d32SJaewon Kim else 209309a3e00SKrzysztof Kozlowski ctrl1 = MAX77843_MUIC_CONTROL1_SW_OPEN; 2104a4a8714SMarek Szyprowski if (nobccomp) { 2114a4a8714SMarek Szyprowski /* Disable BC1.2 protocol and force manual switch control */ 2124a4a8714SMarek Szyprowski ctrl1 |= MAX77843_MUIC_CONTROL1_NOBCCOMP_MASK; 2134a4a8714SMarek Szyprowski } 21427a28d32SJaewon Kim 21527a28d32SJaewon Kim ret = regmap_update_bits(max77843->regmap_muic, 21627a28d32SJaewon Kim MAX77843_MUIC_REG_CONTROL1, 2174a4a8714SMarek Szyprowski MAX77843_MUIC_CONTROL1_COM_SW | 2184a4a8714SMarek Szyprowski MAX77843_MUIC_CONTROL1_NOBCCOMP_MASK, 2194a4a8714SMarek Szyprowski ctrl1); 22027a28d32SJaewon Kim if (ret < 0) { 22127a28d32SJaewon Kim dev_err(info->dev, "Cannot switch MUIC port\n"); 22227a28d32SJaewon Kim return ret; 22327a28d32SJaewon Kim } 22427a28d32SJaewon Kim 22527a28d32SJaewon Kim if (attached) 22627a28d32SJaewon Kim ctrl2 = MAX77843_MUIC_CONTROL2_CPEN_MASK; 22727a28d32SJaewon Kim else 22827a28d32SJaewon Kim ctrl2 = MAX77843_MUIC_CONTROL2_LOWPWR_MASK; 22927a28d32SJaewon Kim 23027a28d32SJaewon Kim ret = regmap_update_bits(max77843->regmap_muic, 23127a28d32SJaewon Kim MAX77843_MUIC_REG_CONTROL2, 23227a28d32SJaewon Kim MAX77843_MUIC_CONTROL2_LOWPWR_MASK | 23327a28d32SJaewon Kim MAX77843_MUIC_CONTROL2_CPEN_MASK, ctrl2); 23427a28d32SJaewon Kim if (ret < 0) { 23527a28d32SJaewon Kim dev_err(info->dev, "Cannot update lowpower mode\n"); 23627a28d32SJaewon Kim return ret; 23727a28d32SJaewon Kim } 23827a28d32SJaewon Kim 23927a28d32SJaewon Kim dev_dbg(info->dev, 24027a28d32SJaewon Kim "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n", 24127a28d32SJaewon Kim ctrl1, ctrl2, attached ? "attached" : "detached"); 24227a28d32SJaewon Kim 24327a28d32SJaewon Kim return 0; 24427a28d32SJaewon Kim } 24527a28d32SJaewon Kim 2467b965110SMarek Szyprowski static void max77843_charger_set_otg_vbus(struct max77843_muic_info *info, 2477b965110SMarek Szyprowski bool on) 2487b965110SMarek Szyprowski { 2497b965110SMarek Szyprowski struct max77693_dev *max77843 = info->max77843; 2507b965110SMarek Szyprowski unsigned int cnfg00; 2517b965110SMarek Szyprowski 2527b965110SMarek Szyprowski if (on) 2537b965110SMarek Szyprowski cnfg00 = MAX77843_CHG_OTG_MASK | MAX77843_CHG_BOOST_MASK; 2547b965110SMarek Szyprowski else 2557b965110SMarek Szyprowski cnfg00 = MAX77843_CHG_ENABLE | MAX77843_CHG_BUCK_MASK; 2567b965110SMarek Szyprowski 2577b965110SMarek Szyprowski regmap_update_bits(max77843->regmap_chg, MAX77843_CHG_REG_CHG_CNFG_00, 2587b965110SMarek Szyprowski MAX77843_CHG_MODE_MASK, cnfg00); 2597b965110SMarek Szyprowski } 2607b965110SMarek Szyprowski 26127a28d32SJaewon Kim static int max77843_muic_get_cable_type(struct max77843_muic_info *info, 26227a28d32SJaewon Kim enum max77843_muic_cable_group group, bool *attached) 26327a28d32SJaewon Kim { 26427a28d32SJaewon Kim int adc, chg_type, cable_type, gnd_type; 26527a28d32SJaewon Kim 26627a28d32SJaewon Kim adc = info->status[MAX77843_MUIC_STATUS1] & 26727a28d32SJaewon Kim MAX77843_MUIC_STATUS1_ADC_MASK; 268309a3e00SKrzysztof Kozlowski adc >>= MAX77843_MUIC_STATUS1_ADC_SHIFT; 26927a28d32SJaewon Kim 27027a28d32SJaewon Kim switch (group) { 27127a28d32SJaewon Kim case MAX77843_CABLE_GROUP_ADC: 27227a28d32SJaewon Kim if (adc == MAX77843_MUIC_ADC_OPEN) { 27327a28d32SJaewon Kim *attached = false; 27427a28d32SJaewon Kim cable_type = info->prev_cable_type; 27527a28d32SJaewon Kim info->prev_cable_type = MAX77843_MUIC_ADC_OPEN; 27627a28d32SJaewon Kim } else { 27727a28d32SJaewon Kim *attached = true; 27827a28d32SJaewon Kim cable_type = info->prev_cable_type = adc; 27927a28d32SJaewon Kim } 28027a28d32SJaewon Kim break; 28127a28d32SJaewon Kim case MAX77843_CABLE_GROUP_CHG: 28227a28d32SJaewon Kim chg_type = info->status[MAX77843_MUIC_STATUS2] & 28327a28d32SJaewon Kim MAX77843_MUIC_STATUS2_CHGTYP_MASK; 28427a28d32SJaewon Kim 28527a28d32SJaewon Kim /* Check GROUND accessory with charger cable */ 28627a28d32SJaewon Kim if (adc == MAX77843_MUIC_ADC_GROUND) { 28727a28d32SJaewon Kim if (chg_type == MAX77843_MUIC_CHG_NONE) { 288af57fa4dSSrikant Ritolia /* 289af57fa4dSSrikant Ritolia * The following state when charger cable is 29027a28d32SJaewon Kim * disconnected but the GROUND accessory still 291af57fa4dSSrikant Ritolia * connected. 292af57fa4dSSrikant Ritolia */ 29327a28d32SJaewon Kim *attached = false; 29427a28d32SJaewon Kim cable_type = info->prev_chg_type; 29527a28d32SJaewon Kim info->prev_chg_type = MAX77843_MUIC_CHG_NONE; 29627a28d32SJaewon Kim } else { 29727a28d32SJaewon Kim 298af57fa4dSSrikant Ritolia /* 299af57fa4dSSrikant Ritolia * The following state when charger cable is 300af57fa4dSSrikant Ritolia * connected on the GROUND accessory. 301af57fa4dSSrikant Ritolia */ 30227a28d32SJaewon Kim *attached = true; 30327a28d32SJaewon Kim cable_type = MAX77843_MUIC_CHG_GND; 30427a28d32SJaewon Kim info->prev_chg_type = MAX77843_MUIC_CHG_GND; 30527a28d32SJaewon Kim } 30627a28d32SJaewon Kim break; 30727a28d32SJaewon Kim } 30827a28d32SJaewon Kim 3094a4a8714SMarek Szyprowski if (adc == MAX77843_MUIC_ADC_RESERVED_ACC_3) { /* SmartDock */ 3104a4a8714SMarek Szyprowski if (chg_type == MAX77843_MUIC_CHG_NONE) { 3114a4a8714SMarek Szyprowski *attached = false; 3124a4a8714SMarek Szyprowski cable_type = info->prev_chg_type; 3134a4a8714SMarek Szyprowski info->prev_chg_type = MAX77843_MUIC_CHG_NONE; 3144a4a8714SMarek Szyprowski } else { 3154a4a8714SMarek Szyprowski *attached = true; 3164a4a8714SMarek Szyprowski cable_type = MAX77843_MUIC_CHG_DOCK; 3174a4a8714SMarek Szyprowski info->prev_chg_type = MAX77843_MUIC_CHG_DOCK; 3184a4a8714SMarek Szyprowski } 3194a4a8714SMarek Szyprowski break; 3204a4a8714SMarek Szyprowski } 3214a4a8714SMarek Szyprowski 32227a28d32SJaewon Kim if (chg_type == MAX77843_MUIC_CHG_NONE) { 32327a28d32SJaewon Kim *attached = false; 32427a28d32SJaewon Kim cable_type = info->prev_chg_type; 32527a28d32SJaewon Kim info->prev_chg_type = MAX77843_MUIC_CHG_NONE; 32627a28d32SJaewon Kim } else { 32727a28d32SJaewon Kim *attached = true; 32827a28d32SJaewon Kim cable_type = info->prev_chg_type = chg_type; 32927a28d32SJaewon Kim } 33027a28d32SJaewon Kim break; 33127a28d32SJaewon Kim case MAX77843_CABLE_GROUP_ADC_GND: 33227a28d32SJaewon Kim if (adc == MAX77843_MUIC_ADC_OPEN) { 33327a28d32SJaewon Kim *attached = false; 33427a28d32SJaewon Kim cable_type = info->prev_gnd_type; 33527a28d32SJaewon Kim info->prev_gnd_type = MAX77843_MUIC_ADC_OPEN; 33627a28d32SJaewon Kim } else { 33727a28d32SJaewon Kim *attached = true; 33827a28d32SJaewon Kim 339af57fa4dSSrikant Ritolia /* 340af57fa4dSSrikant Ritolia * Offset|ADC1K|VBVolt| 34127a28d32SJaewon Kim * 0x1| 0| 0| USB-HOST 34227a28d32SJaewon Kim * 0x1| 0| 1| USB-HOST with VB 34327a28d32SJaewon Kim * 0x1| 1| 0| MHL 344af57fa4dSSrikant Ritolia * 0x1| 1| 1| MHL with VB 345af57fa4dSSrikant Ritolia */ 34627a28d32SJaewon Kim /* Get ADC1K register bit */ 34727a28d32SJaewon Kim gnd_type = (info->status[MAX77843_MUIC_STATUS1] & 34827a28d32SJaewon Kim MAX77843_MUIC_STATUS1_ADC1K_MASK); 34927a28d32SJaewon Kim 35027a28d32SJaewon Kim /* Get VBVolt register bit */ 35127a28d32SJaewon Kim gnd_type |= (info->status[MAX77843_MUIC_STATUS2] & 35227a28d32SJaewon Kim MAX77843_MUIC_STATUS2_VBVOLT_MASK); 353309a3e00SKrzysztof Kozlowski gnd_type >>= MAX77843_MUIC_STATUS2_VBVOLT_SHIFT; 35427a28d32SJaewon Kim 35527a28d32SJaewon Kim /* Offset of GND cable */ 35627a28d32SJaewon Kim gnd_type |= MAX77843_MUIC_GND_USB_HOST; 35727a28d32SJaewon Kim cable_type = info->prev_gnd_type = gnd_type; 35827a28d32SJaewon Kim } 35927a28d32SJaewon Kim break; 36027a28d32SJaewon Kim default: 36127a28d32SJaewon Kim dev_err(info->dev, "Unknown cable group (%d)\n", group); 36227a28d32SJaewon Kim cable_type = -EINVAL; 36327a28d32SJaewon Kim break; 36427a28d32SJaewon Kim } 36527a28d32SJaewon Kim 36627a28d32SJaewon Kim return cable_type; 36727a28d32SJaewon Kim } 36827a28d32SJaewon Kim 36927a28d32SJaewon Kim static int max77843_muic_adc_gnd_handler(struct max77843_muic_info *info) 37027a28d32SJaewon Kim { 37127a28d32SJaewon Kim int ret, gnd_cable_type; 37227a28d32SJaewon Kim bool attached; 37327a28d32SJaewon Kim 37427a28d32SJaewon Kim gnd_cable_type = max77843_muic_get_cable_type(info, 37527a28d32SJaewon Kim MAX77843_CABLE_GROUP_ADC_GND, &attached); 37627a28d32SJaewon Kim dev_dbg(info->dev, "external connector is %s (gnd:0x%02x)\n", 37727a28d32SJaewon Kim attached ? "attached" : "detached", gnd_cable_type); 37827a28d32SJaewon Kim 37927a28d32SJaewon Kim switch (gnd_cable_type) { 38027a28d32SJaewon Kim case MAX77843_MUIC_GND_USB_HOST: 38127a28d32SJaewon Kim case MAX77843_MUIC_GND_USB_HOST_VB: 382309a3e00SKrzysztof Kozlowski ret = max77843_muic_set_path(info, 383309a3e00SKrzysztof Kozlowski MAX77843_MUIC_CONTROL1_SW_USB, 3844a4a8714SMarek Szyprowski attached, false); 38527a28d32SJaewon Kim if (ret < 0) 38627a28d32SJaewon Kim return ret; 38727a28d32SJaewon Kim 3888670b459SChanwoo Choi extcon_set_state_sync(info->edev, EXTCON_USB_HOST, attached); 3897b965110SMarek Szyprowski max77843_charger_set_otg_vbus(info, attached); 39027a28d32SJaewon Kim break; 39127a28d32SJaewon Kim case MAX77843_MUIC_GND_MHL_VB: 39227a28d32SJaewon Kim case MAX77843_MUIC_GND_MHL: 393309a3e00SKrzysztof Kozlowski ret = max77843_muic_set_path(info, 394309a3e00SKrzysztof Kozlowski MAX77843_MUIC_CONTROL1_SW_OPEN, 3954a4a8714SMarek Szyprowski attached, false); 39627a28d32SJaewon Kim if (ret < 0) 39727a28d32SJaewon Kim return ret; 39827a28d32SJaewon Kim 3998670b459SChanwoo Choi extcon_set_state_sync(info->edev, EXTCON_DISP_MHL, attached); 40027a28d32SJaewon Kim break; 40127a28d32SJaewon Kim default: 40227a28d32SJaewon Kim dev_err(info->dev, "failed to detect %s accessory(gnd:0x%x)\n", 40327a28d32SJaewon Kim attached ? "attached" : "detached", gnd_cable_type); 40427a28d32SJaewon Kim return -EINVAL; 40527a28d32SJaewon Kim } 40627a28d32SJaewon Kim 40727a28d32SJaewon Kim return 0; 40827a28d32SJaewon Kim } 40927a28d32SJaewon Kim 41027a28d32SJaewon Kim static int max77843_muic_jig_handler(struct max77843_muic_info *info, 41127a28d32SJaewon Kim int cable_type, bool attached) 41227a28d32SJaewon Kim { 41327a28d32SJaewon Kim int ret; 414309a3e00SKrzysztof Kozlowski u8 path = MAX77843_MUIC_CONTROL1_SW_OPEN; 41527a28d32SJaewon Kim 41627a28d32SJaewon Kim dev_dbg(info->dev, "external connector is %s (adc:0x%02x)\n", 41727a28d32SJaewon Kim attached ? "attached" : "detached", cable_type); 41827a28d32SJaewon Kim 41927a28d32SJaewon Kim switch (cable_type) { 42027a28d32SJaewon Kim case MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF: 42127a28d32SJaewon Kim case MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON: 422309a3e00SKrzysztof Kozlowski path = MAX77843_MUIC_CONTROL1_SW_USB; 42327a28d32SJaewon Kim break; 42427a28d32SJaewon Kim case MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF: 425309a3e00SKrzysztof Kozlowski path = MAX77843_MUIC_CONTROL1_SW_UART; 42627a28d32SJaewon Kim break; 42727a28d32SJaewon Kim default: 42841b3c015SChanwoo Choi return -EINVAL; 42941b3c015SChanwoo Choi } 43041b3c015SChanwoo Choi 4314a4a8714SMarek Szyprowski ret = max77843_muic_set_path(info, path, attached, false); 43227a28d32SJaewon Kim if (ret < 0) 43327a28d32SJaewon Kim return ret; 43441b3c015SChanwoo Choi 4358670b459SChanwoo Choi extcon_set_state_sync(info->edev, EXTCON_JIG, attached); 43627a28d32SJaewon Kim 43727a28d32SJaewon Kim return 0; 43827a28d32SJaewon Kim } 43927a28d32SJaewon Kim 4404a4a8714SMarek Szyprowski static int max77843_muic_dock_handler(struct max77843_muic_info *info, 4414a4a8714SMarek Szyprowski bool attached) 4424a4a8714SMarek Szyprowski { 4434a4a8714SMarek Szyprowski int ret; 4444a4a8714SMarek Szyprowski 4454a4a8714SMarek Szyprowski dev_dbg(info->dev, "external connector is %s (adc: 0x10)\n", 4464a4a8714SMarek Szyprowski attached ? "attached" : "detached"); 4474a4a8714SMarek Szyprowski 4484a4a8714SMarek Szyprowski ret = max77843_muic_set_path(info, MAX77843_MUIC_CONTROL1_SW_USB, 4494a4a8714SMarek Szyprowski attached, attached); 4504a4a8714SMarek Szyprowski if (ret < 0) 4514a4a8714SMarek Szyprowski return ret; 4524a4a8714SMarek Szyprowski 4534a4a8714SMarek Szyprowski extcon_set_state_sync(info->edev, EXTCON_DISP_MHL, attached); 4544a4a8714SMarek Szyprowski extcon_set_state_sync(info->edev, EXTCON_USB_HOST, attached); 4554a4a8714SMarek Szyprowski extcon_set_state_sync(info->edev, EXTCON_DOCK, attached); 4564a4a8714SMarek Szyprowski 4574a4a8714SMarek Szyprowski return 0; 4584a4a8714SMarek Szyprowski } 4594a4a8714SMarek Szyprowski 46027a28d32SJaewon Kim static int max77843_muic_adc_handler(struct max77843_muic_info *info) 46127a28d32SJaewon Kim { 46227a28d32SJaewon Kim int ret, cable_type; 46327a28d32SJaewon Kim bool attached; 46427a28d32SJaewon Kim 46527a28d32SJaewon Kim cable_type = max77843_muic_get_cable_type(info, 46627a28d32SJaewon Kim MAX77843_CABLE_GROUP_ADC, &attached); 46727a28d32SJaewon Kim 46827a28d32SJaewon Kim dev_dbg(info->dev, 46927a28d32SJaewon Kim "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n", 47027a28d32SJaewon Kim attached ? "attached" : "detached", cable_type, 47127a28d32SJaewon Kim info->prev_cable_type); 47227a28d32SJaewon Kim 47327a28d32SJaewon Kim switch (cable_type) { 4744a4a8714SMarek Szyprowski case MAX77843_MUIC_ADC_RESERVED_ACC_3: /* SmartDock */ 4754a4a8714SMarek Szyprowski ret = max77843_muic_dock_handler(info, attached); 4764a4a8714SMarek Szyprowski if (ret < 0) 4774a4a8714SMarek Szyprowski return ret; 4784a4a8714SMarek Szyprowski break; 47927a28d32SJaewon Kim case MAX77843_MUIC_ADC_GROUND: 48027a28d32SJaewon Kim ret = max77843_muic_adc_gnd_handler(info); 48127a28d32SJaewon Kim if (ret < 0) 48227a28d32SJaewon Kim return ret; 48327a28d32SJaewon Kim break; 48427a28d32SJaewon Kim case MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF: 48527a28d32SJaewon Kim case MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON: 48627a28d32SJaewon Kim case MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF: 48727a28d32SJaewon Kim ret = max77843_muic_jig_handler(info, cable_type, attached); 48827a28d32SJaewon Kim if (ret < 0) 48927a28d32SJaewon Kim return ret; 49027a28d32SJaewon Kim break; 49127a28d32SJaewon Kim case MAX77843_MUIC_ADC_SEND_END_BUTTON: 49227a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S1_BUTTON: 49327a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S2_BUTTON: 49427a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S3_BUTTON: 49527a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S4_BUTTON: 49627a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S5_BUTTON: 49727a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S6_BUTTON: 49827a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S7_BUTTON: 49927a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S8_BUTTON: 50027a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S9_BUTTON: 50127a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S10_BUTTON: 50227a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S11_BUTTON: 50327a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S12_BUTTON: 50427a28d32SJaewon Kim case MAX77843_MUIC_ADC_RESERVED_ACC_1: 50527a28d32SJaewon Kim case MAX77843_MUIC_ADC_RESERVED_ACC_2: 50627a28d32SJaewon Kim case MAX77843_MUIC_ADC_RESERVED_ACC_4: 50727a28d32SJaewon Kim case MAX77843_MUIC_ADC_RESERVED_ACC_5: 50827a28d32SJaewon Kim case MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE2: 50927a28d32SJaewon Kim case MAX77843_MUIC_ADC_PHONE_POWERED_DEV: 51027a28d32SJaewon Kim case MAX77843_MUIC_ADC_TTY_CONVERTER: 51127a28d32SJaewon Kim case MAX77843_MUIC_ADC_UART_CABLE: 51227a28d32SJaewon Kim case MAX77843_MUIC_ADC_CEA936A_TYPE1_CHG: 51327a28d32SJaewon Kim case MAX77843_MUIC_ADC_AV_CABLE_NOLOAD: 51427a28d32SJaewon Kim case MAX77843_MUIC_ADC_CEA936A_TYPE2_CHG: 51527a28d32SJaewon Kim case MAX77843_MUIC_ADC_FACTORY_MODE_UART_ON: 51627a28d32SJaewon Kim case MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE1: 51727a28d32SJaewon Kim case MAX77843_MUIC_ADC_OPEN: 51827a28d32SJaewon Kim dev_err(info->dev, 51927a28d32SJaewon Kim "accessory is %s but it isn't used (adc:0x%x)\n", 52027a28d32SJaewon Kim attached ? "attached" : "detached", cable_type); 52127a28d32SJaewon Kim return -EAGAIN; 52227a28d32SJaewon Kim default: 52327a28d32SJaewon Kim dev_err(info->dev, 52427a28d32SJaewon Kim "failed to detect %s accessory (adc:0x%x)\n", 52527a28d32SJaewon Kim attached ? "attached" : "detached", cable_type); 52627a28d32SJaewon Kim return -EINVAL; 52727a28d32SJaewon Kim } 52827a28d32SJaewon Kim 52927a28d32SJaewon Kim return 0; 53027a28d32SJaewon Kim } 53127a28d32SJaewon Kim 53227a28d32SJaewon Kim static int max77843_muic_chg_handler(struct max77843_muic_info *info) 53327a28d32SJaewon Kim { 53427a28d32SJaewon Kim int ret, chg_type, gnd_type; 53527a28d32SJaewon Kim bool attached; 53627a28d32SJaewon Kim 53727a28d32SJaewon Kim chg_type = max77843_muic_get_cable_type(info, 53827a28d32SJaewon Kim MAX77843_CABLE_GROUP_CHG, &attached); 53927a28d32SJaewon Kim 54027a28d32SJaewon Kim dev_dbg(info->dev, 54127a28d32SJaewon Kim "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n", 54227a28d32SJaewon Kim attached ? "attached" : "detached", 54327a28d32SJaewon Kim chg_type, info->prev_chg_type); 54427a28d32SJaewon Kim 54527a28d32SJaewon Kim switch (chg_type) { 54627a28d32SJaewon Kim case MAX77843_MUIC_CHG_USB: 547309a3e00SKrzysztof Kozlowski ret = max77843_muic_set_path(info, 548309a3e00SKrzysztof Kozlowski MAX77843_MUIC_CONTROL1_SW_USB, 5494a4a8714SMarek Szyprowski attached, false); 55027a28d32SJaewon Kim if (ret < 0) 55127a28d32SJaewon Kim return ret; 55227a28d32SJaewon Kim 5538670b459SChanwoo Choi extcon_set_state_sync(info->edev, EXTCON_USB, attached); 5548670b459SChanwoo Choi extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SDP, 5558b45b6a0SChanwoo Choi attached); 55627a28d32SJaewon Kim break; 55727a28d32SJaewon Kim case MAX77843_MUIC_CHG_DOWNSTREAM: 558309a3e00SKrzysztof Kozlowski ret = max77843_muic_set_path(info, 559309a3e00SKrzysztof Kozlowski MAX77843_MUIC_CONTROL1_SW_OPEN, 5604a4a8714SMarek Szyprowski attached, false); 56127a28d32SJaewon Kim if (ret < 0) 56227a28d32SJaewon Kim return ret; 56327a28d32SJaewon Kim 5648670b459SChanwoo Choi extcon_set_state_sync(info->edev, EXTCON_CHG_USB_CDP, 5652a9de9c0SChanwoo Choi attached); 56627a28d32SJaewon Kim break; 56727a28d32SJaewon Kim case MAX77843_MUIC_CHG_DEDICATED: 568309a3e00SKrzysztof Kozlowski ret = max77843_muic_set_path(info, 569309a3e00SKrzysztof Kozlowski MAX77843_MUIC_CONTROL1_SW_OPEN, 5704a4a8714SMarek Szyprowski attached, false); 57127a28d32SJaewon Kim if (ret < 0) 57227a28d32SJaewon Kim return ret; 57327a28d32SJaewon Kim 5748670b459SChanwoo Choi extcon_set_state_sync(info->edev, EXTCON_CHG_USB_DCP, 57511eecf91SChanwoo Choi attached); 57627a28d32SJaewon Kim break; 57727a28d32SJaewon Kim case MAX77843_MUIC_CHG_SPECIAL_500MA: 578309a3e00SKrzysztof Kozlowski ret = max77843_muic_set_path(info, 579309a3e00SKrzysztof Kozlowski MAX77843_MUIC_CONTROL1_SW_OPEN, 5804a4a8714SMarek Szyprowski attached, false); 58127a28d32SJaewon Kim if (ret < 0) 58227a28d32SJaewon Kim return ret; 58327a28d32SJaewon Kim 5848670b459SChanwoo Choi extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SLOW, 5852a9de9c0SChanwoo Choi attached); 58627a28d32SJaewon Kim break; 58727a28d32SJaewon Kim case MAX77843_MUIC_CHG_SPECIAL_1A: 588309a3e00SKrzysztof Kozlowski ret = max77843_muic_set_path(info, 589309a3e00SKrzysztof Kozlowski MAX77843_MUIC_CONTROL1_SW_OPEN, 5904a4a8714SMarek Szyprowski attached, false); 59127a28d32SJaewon Kim if (ret < 0) 59227a28d32SJaewon Kim return ret; 59327a28d32SJaewon Kim 5948670b459SChanwoo Choi extcon_set_state_sync(info->edev, EXTCON_CHG_USB_FAST, 5952a9de9c0SChanwoo Choi attached); 59627a28d32SJaewon Kim break; 59727a28d32SJaewon Kim case MAX77843_MUIC_CHG_GND: 59827a28d32SJaewon Kim gnd_type = max77843_muic_get_cable_type(info, 59927a28d32SJaewon Kim MAX77843_CABLE_GROUP_ADC_GND, &attached); 60027a28d32SJaewon Kim 60127a28d32SJaewon Kim /* Charger cable on MHL accessory is attach or detach */ 60227a28d32SJaewon Kim if (gnd_type == MAX77843_MUIC_GND_MHL_VB) 6038670b459SChanwoo Choi extcon_set_state_sync(info->edev, EXTCON_CHG_USB_DCP, 60411eecf91SChanwoo Choi true); 60527a28d32SJaewon Kim else if (gnd_type == MAX77843_MUIC_GND_MHL) 6068670b459SChanwoo Choi extcon_set_state_sync(info->edev, EXTCON_CHG_USB_DCP, 60711eecf91SChanwoo Choi false); 60827a28d32SJaewon Kim break; 6094a4a8714SMarek Szyprowski case MAX77843_MUIC_CHG_DOCK: 6104a4a8714SMarek Szyprowski extcon_set_state_sync(info->edev, EXTCON_CHG_USB_DCP, attached); 6114a4a8714SMarek Szyprowski break; 61227a28d32SJaewon Kim case MAX77843_MUIC_CHG_NONE: 61327a28d32SJaewon Kim break; 61427a28d32SJaewon Kim default: 61527a28d32SJaewon Kim dev_err(info->dev, 61627a28d32SJaewon Kim "failed to detect %s accessory (chg_type:0x%x)\n", 61727a28d32SJaewon Kim attached ? "attached" : "detached", chg_type); 61827a28d32SJaewon Kim 619309a3e00SKrzysztof Kozlowski max77843_muic_set_path(info, MAX77843_MUIC_CONTROL1_SW_OPEN, 6204a4a8714SMarek Szyprowski attached, false); 62127a28d32SJaewon Kim return -EINVAL; 62227a28d32SJaewon Kim } 62327a28d32SJaewon Kim 62427a28d32SJaewon Kim return 0; 62527a28d32SJaewon Kim } 62627a28d32SJaewon Kim 62727a28d32SJaewon Kim static void max77843_muic_irq_work(struct work_struct *work) 62827a28d32SJaewon Kim { 62927a28d32SJaewon Kim struct max77843_muic_info *info = container_of(work, 63027a28d32SJaewon Kim struct max77843_muic_info, irq_work); 631bc1aadc1SKrzysztof Kozlowski struct max77693_dev *max77843 = info->max77843; 63227a28d32SJaewon Kim int ret = 0; 63327a28d32SJaewon Kim 63427a28d32SJaewon Kim mutex_lock(&info->mutex); 63527a28d32SJaewon Kim 63627a28d32SJaewon Kim ret = regmap_bulk_read(max77843->regmap_muic, 63727a28d32SJaewon Kim MAX77843_MUIC_REG_STATUS1, info->status, 63827a28d32SJaewon Kim MAX77843_MUIC_STATUS_NUM); 63927a28d32SJaewon Kim if (ret) { 64027a28d32SJaewon Kim dev_err(info->dev, "Cannot read STATUS registers\n"); 64127a28d32SJaewon Kim mutex_unlock(&info->mutex); 64227a28d32SJaewon Kim return; 64327a28d32SJaewon Kim } 64427a28d32SJaewon Kim 64527a28d32SJaewon Kim if (info->irq_adc) { 64627a28d32SJaewon Kim ret = max77843_muic_adc_handler(info); 64727a28d32SJaewon Kim if (ret) 64827a28d32SJaewon Kim dev_err(info->dev, "Unknown cable type\n"); 64927a28d32SJaewon Kim info->irq_adc = false; 65027a28d32SJaewon Kim } 65127a28d32SJaewon Kim 65227a28d32SJaewon Kim if (info->irq_chg) { 65327a28d32SJaewon Kim ret = max77843_muic_chg_handler(info); 65427a28d32SJaewon Kim if (ret) 65527a28d32SJaewon Kim dev_err(info->dev, "Unknown charger type\n"); 65627a28d32SJaewon Kim info->irq_chg = false; 65727a28d32SJaewon Kim } 65827a28d32SJaewon Kim 65927a28d32SJaewon Kim mutex_unlock(&info->mutex); 66027a28d32SJaewon Kim } 66127a28d32SJaewon Kim 66227a28d32SJaewon Kim static irqreturn_t max77843_muic_irq_handler(int irq, void *data) 66327a28d32SJaewon Kim { 66427a28d32SJaewon Kim struct max77843_muic_info *info = data; 66527a28d32SJaewon Kim int i, irq_type = -1; 66627a28d32SJaewon Kim 66727a28d32SJaewon Kim for (i = 0; i < ARRAY_SIZE(max77843_muic_irqs); i++) 66827a28d32SJaewon Kim if (irq == max77843_muic_irqs[i].virq) 66927a28d32SJaewon Kim irq_type = max77843_muic_irqs[i].irq; 67027a28d32SJaewon Kim 67127a28d32SJaewon Kim switch (irq_type) { 67227a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT1_ADC: 67327a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT1_ADCERROR: 67427a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT1_ADC1K: 67527a28d32SJaewon Kim info->irq_adc = true; 67627a28d32SJaewon Kim break; 67727a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT2_CHGTYP: 67827a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT2_CHGDETRUN: 67927a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT2_DCDTMR: 68027a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT2_DXOVP: 68127a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT2_VBVOLT: 68227a28d32SJaewon Kim info->irq_chg = true; 68327a28d32SJaewon Kim break; 68427a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT3_VBADC: 68527a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT3_VDNMON: 68627a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT3_DNRES: 68727a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT3_MPNACK: 68827a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT3_MRXBUFOW: 68927a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT3_MRXTRF: 69027a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT3_MRXPERR: 69127a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT3_MRXRDY: 69227a28d32SJaewon Kim break; 69327a28d32SJaewon Kim default: 69427a28d32SJaewon Kim dev_err(info->dev, "Cannot recognize IRQ(%d)\n", irq_type); 69527a28d32SJaewon Kim break; 69627a28d32SJaewon Kim } 69727a28d32SJaewon Kim 69827a28d32SJaewon Kim schedule_work(&info->irq_work); 69927a28d32SJaewon Kim 70027a28d32SJaewon Kim return IRQ_HANDLED; 70127a28d32SJaewon Kim } 70227a28d32SJaewon Kim 70327a28d32SJaewon Kim static void max77843_muic_detect_cable_wq(struct work_struct *work) 70427a28d32SJaewon Kim { 70527a28d32SJaewon Kim struct max77843_muic_info *info = container_of(to_delayed_work(work), 70627a28d32SJaewon Kim struct max77843_muic_info, wq_detcable); 707bc1aadc1SKrzysztof Kozlowski struct max77693_dev *max77843 = info->max77843; 70827a28d32SJaewon Kim int chg_type, adc, ret; 70927a28d32SJaewon Kim bool attached; 71027a28d32SJaewon Kim 71127a28d32SJaewon Kim mutex_lock(&info->mutex); 71227a28d32SJaewon Kim 71327a28d32SJaewon Kim ret = regmap_bulk_read(max77843->regmap_muic, 71427a28d32SJaewon Kim MAX77843_MUIC_REG_STATUS1, info->status, 71527a28d32SJaewon Kim MAX77843_MUIC_STATUS_NUM); 71627a28d32SJaewon Kim if (ret) { 71727a28d32SJaewon Kim dev_err(info->dev, "Cannot read STATUS registers\n"); 71827a28d32SJaewon Kim goto err_cable_wq; 71927a28d32SJaewon Kim } 72027a28d32SJaewon Kim 72127a28d32SJaewon Kim adc = max77843_muic_get_cable_type(info, 72227a28d32SJaewon Kim MAX77843_CABLE_GROUP_ADC, &attached); 72327a28d32SJaewon Kim if (attached && adc != MAX77843_MUIC_ADC_OPEN) { 72427a28d32SJaewon Kim ret = max77843_muic_adc_handler(info); 72527a28d32SJaewon Kim if (ret < 0) { 72627a28d32SJaewon Kim dev_err(info->dev, "Cannot detect accessory\n"); 72727a28d32SJaewon Kim goto err_cable_wq; 72827a28d32SJaewon Kim } 72927a28d32SJaewon Kim } 73027a28d32SJaewon Kim 73127a28d32SJaewon Kim chg_type = max77843_muic_get_cable_type(info, 73227a28d32SJaewon Kim MAX77843_CABLE_GROUP_CHG, &attached); 73327a28d32SJaewon Kim if (attached && chg_type != MAX77843_MUIC_CHG_NONE) { 73427a28d32SJaewon Kim ret = max77843_muic_chg_handler(info); 73527a28d32SJaewon Kim if (ret < 0) { 73627a28d32SJaewon Kim dev_err(info->dev, "Cannot detect charger accessory\n"); 73727a28d32SJaewon Kim goto err_cable_wq; 73827a28d32SJaewon Kim } 73927a28d32SJaewon Kim } 74027a28d32SJaewon Kim 74127a28d32SJaewon Kim err_cable_wq: 74227a28d32SJaewon Kim mutex_unlock(&info->mutex); 74327a28d32SJaewon Kim } 74427a28d32SJaewon Kim 74527a28d32SJaewon Kim static int max77843_muic_set_debounce_time(struct max77843_muic_info *info, 74627a28d32SJaewon Kim enum max77843_muic_adc_debounce_time time) 74727a28d32SJaewon Kim { 748bc1aadc1SKrzysztof Kozlowski struct max77693_dev *max77843 = info->max77843; 749d927c590SDan Carpenter int ret; 75027a28d32SJaewon Kim 75127a28d32SJaewon Kim switch (time) { 75227a28d32SJaewon Kim case MAX77843_DEBOUNCE_TIME_5MS: 75327a28d32SJaewon Kim case MAX77843_DEBOUNCE_TIME_10MS: 75427a28d32SJaewon Kim case MAX77843_DEBOUNCE_TIME_25MS: 75527a28d32SJaewon Kim case MAX77843_DEBOUNCE_TIME_38_62MS: 75627a28d32SJaewon Kim ret = regmap_update_bits(max77843->regmap_muic, 75727a28d32SJaewon Kim MAX77843_MUIC_REG_CONTROL4, 75827a28d32SJaewon Kim MAX77843_MUIC_CONTROL4_ADCDBSET_MASK, 759309a3e00SKrzysztof Kozlowski time << MAX77843_MUIC_CONTROL4_ADCDBSET_SHIFT); 76027a28d32SJaewon Kim if (ret < 0) { 76127a28d32SJaewon Kim dev_err(info->dev, "Cannot write MUIC regmap\n"); 76227a28d32SJaewon Kim return ret; 76327a28d32SJaewon Kim } 76427a28d32SJaewon Kim break; 76527a28d32SJaewon Kim default: 76627a28d32SJaewon Kim dev_err(info->dev, "Invalid ADC debounce time\n"); 76727a28d32SJaewon Kim return -EINVAL; 76827a28d32SJaewon Kim } 76927a28d32SJaewon Kim 77027a28d32SJaewon Kim return 0; 77127a28d32SJaewon Kim } 77227a28d32SJaewon Kim 773bc1aadc1SKrzysztof Kozlowski static int max77843_init_muic_regmap(struct max77693_dev *max77843) 77427a28d32SJaewon Kim { 77527a28d32SJaewon Kim int ret; 77627a28d32SJaewon Kim 777a6d9cfcbSWolfram Sang max77843->i2c_muic = i2c_new_dummy_device(max77843->i2c->adapter, 77827a28d32SJaewon Kim I2C_ADDR_MUIC); 779a6d9cfcbSWolfram Sang if (IS_ERR(max77843->i2c_muic)) { 78027a28d32SJaewon Kim dev_err(&max77843->i2c->dev, 78127a28d32SJaewon Kim "Cannot allocate I2C device for MUIC\n"); 782a6d9cfcbSWolfram Sang return PTR_ERR(max77843->i2c_muic); 78327a28d32SJaewon Kim } 78427a28d32SJaewon Kim 78527a28d32SJaewon Kim i2c_set_clientdata(max77843->i2c_muic, max77843); 78627a28d32SJaewon Kim 78727a28d32SJaewon Kim max77843->regmap_muic = devm_regmap_init_i2c(max77843->i2c_muic, 78827a28d32SJaewon Kim &max77843_muic_regmap_config); 78927a28d32SJaewon Kim if (IS_ERR(max77843->regmap_muic)) { 79027a28d32SJaewon Kim ret = PTR_ERR(max77843->regmap_muic); 79127a28d32SJaewon Kim goto err_muic_i2c; 79227a28d32SJaewon Kim } 79327a28d32SJaewon Kim 79427a28d32SJaewon Kim ret = regmap_add_irq_chip(max77843->regmap_muic, max77843->irq, 79527a28d32SJaewon Kim IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED, 79627a28d32SJaewon Kim 0, &max77843_muic_irq_chip, &max77843->irq_data_muic); 79727a28d32SJaewon Kim if (ret < 0) { 79827a28d32SJaewon Kim dev_err(&max77843->i2c->dev, "Cannot add MUIC IRQ chip\n"); 79927a28d32SJaewon Kim goto err_muic_i2c; 80027a28d32SJaewon Kim } 80127a28d32SJaewon Kim 80227a28d32SJaewon Kim return 0; 80327a28d32SJaewon Kim 80427a28d32SJaewon Kim err_muic_i2c: 80527a28d32SJaewon Kim i2c_unregister_device(max77843->i2c_muic); 80627a28d32SJaewon Kim 80727a28d32SJaewon Kim return ret; 80827a28d32SJaewon Kim } 80927a28d32SJaewon Kim 81027a28d32SJaewon Kim static int max77843_muic_probe(struct platform_device *pdev) 81127a28d32SJaewon Kim { 812bc1aadc1SKrzysztof Kozlowski struct max77693_dev *max77843 = dev_get_drvdata(pdev->dev.parent); 81327a28d32SJaewon Kim struct max77843_muic_info *info; 81427a28d32SJaewon Kim unsigned int id; 815d9204acbSMarek Szyprowski int cable_type; 816d9204acbSMarek Szyprowski bool attached; 81727a28d32SJaewon Kim int i, ret; 81827a28d32SJaewon Kim 81927a28d32SJaewon Kim info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); 82027a28d32SJaewon Kim if (!info) 82127a28d32SJaewon Kim return -ENOMEM; 82227a28d32SJaewon Kim 82327a28d32SJaewon Kim info->dev = &pdev->dev; 82427a28d32SJaewon Kim info->max77843 = max77843; 82527a28d32SJaewon Kim 82627a28d32SJaewon Kim platform_set_drvdata(pdev, info); 82727a28d32SJaewon Kim mutex_init(&info->mutex); 82827a28d32SJaewon Kim 82927a28d32SJaewon Kim /* Initialize i2c and regmap */ 83027a28d32SJaewon Kim ret = max77843_init_muic_regmap(max77843); 83127a28d32SJaewon Kim if (ret) { 83227a28d32SJaewon Kim dev_err(&pdev->dev, "Failed to init MUIC regmap\n"); 83327a28d32SJaewon Kim return ret; 83427a28d32SJaewon Kim } 83527a28d32SJaewon Kim 83627a28d32SJaewon Kim /* Turn off auto detection configuration */ 83727a28d32SJaewon Kim ret = regmap_update_bits(max77843->regmap_muic, 83827a28d32SJaewon Kim MAX77843_MUIC_REG_CONTROL4, 83927a28d32SJaewon Kim MAX77843_MUIC_CONTROL4_USBAUTO_MASK | 84027a28d32SJaewon Kim MAX77843_MUIC_CONTROL4_FCTAUTO_MASK, 84127a28d32SJaewon Kim CONTROL4_AUTO_DISABLE); 84227a28d32SJaewon Kim 84327a28d32SJaewon Kim /* Initialize extcon device */ 84427a28d32SJaewon Kim info->edev = devm_extcon_dev_allocate(&pdev->dev, 84527a28d32SJaewon Kim max77843_extcon_cable); 84627a28d32SJaewon Kim if (IS_ERR(info->edev)) { 84727a28d32SJaewon Kim dev_err(&pdev->dev, "Failed to allocate memory for extcon\n"); 848*a4cf11faSKrzysztof Kozlowski ret = PTR_ERR(info->edev); 84927a28d32SJaewon Kim goto err_muic_irq; 85027a28d32SJaewon Kim } 85127a28d32SJaewon Kim 85227a28d32SJaewon Kim ret = devm_extcon_dev_register(&pdev->dev, info->edev); 85327a28d32SJaewon Kim if (ret) { 85427a28d32SJaewon Kim dev_err(&pdev->dev, "Failed to register extcon device\n"); 85527a28d32SJaewon Kim goto err_muic_irq; 85627a28d32SJaewon Kim } 85727a28d32SJaewon Kim 85827a28d32SJaewon Kim /* Set ADC debounce time */ 85927a28d32SJaewon Kim max77843_muic_set_debounce_time(info, MAX77843_DEBOUNCE_TIME_25MS); 86027a28d32SJaewon Kim 861d9204acbSMarek Szyprowski /* Set initial path for UART when JIG is connected to get serial logs */ 862d9204acbSMarek Szyprowski ret = regmap_bulk_read(max77843->regmap_muic, 863d9204acbSMarek Szyprowski MAX77843_MUIC_REG_STATUS1, info->status, 864d9204acbSMarek Szyprowski MAX77843_MUIC_STATUS_NUM); 865d9204acbSMarek Szyprowski if (ret) { 866d9204acbSMarek Szyprowski dev_err(info->dev, "Cannot read STATUS registers\n"); 867d9204acbSMarek Szyprowski goto err_muic_irq; 868d9204acbSMarek Szyprowski } 869d9204acbSMarek Szyprowski cable_type = max77843_muic_get_cable_type(info, MAX77843_CABLE_GROUP_ADC, 870d9204acbSMarek Szyprowski &attached); 871d9204acbSMarek Szyprowski if (attached && cable_type == MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF) 872d9204acbSMarek Szyprowski max77843_muic_set_path(info, MAX77843_MUIC_CONTROL1_SW_UART, 873d9204acbSMarek Szyprowski true, false); 87427a28d32SJaewon Kim 87527a28d32SJaewon Kim /* Check revision number of MUIC device */ 87627a28d32SJaewon Kim ret = regmap_read(max77843->regmap_muic, MAX77843_MUIC_REG_ID, &id); 87727a28d32SJaewon Kim if (ret < 0) { 87827a28d32SJaewon Kim dev_err(&pdev->dev, "Failed to read revision number\n"); 87927a28d32SJaewon Kim goto err_muic_irq; 88027a28d32SJaewon Kim } 88127a28d32SJaewon Kim dev_info(info->dev, "MUIC device ID : 0x%x\n", id); 88227a28d32SJaewon Kim 88327a28d32SJaewon Kim /* Support virtual irq domain for max77843 MUIC device */ 88427a28d32SJaewon Kim INIT_WORK(&info->irq_work, max77843_muic_irq_work); 88527a28d32SJaewon Kim 886135d9f7dSJaewon Kim /* Clear IRQ bits before request IRQs */ 887135d9f7dSJaewon Kim ret = regmap_bulk_read(max77843->regmap_muic, 888135d9f7dSJaewon Kim MAX77843_MUIC_REG_INT1, info->status, 889c4924e92SDan Carpenter MAX77843_MUIC_STATUS_NUM); 890135d9f7dSJaewon Kim if (ret) { 891135d9f7dSJaewon Kim dev_err(&pdev->dev, "Failed to Clear IRQ bits\n"); 892135d9f7dSJaewon Kim goto err_muic_irq; 893135d9f7dSJaewon Kim } 894135d9f7dSJaewon Kim 89527a28d32SJaewon Kim for (i = 0; i < ARRAY_SIZE(max77843_muic_irqs); i++) { 89627a28d32SJaewon Kim struct max77843_muic_irq *muic_irq = &max77843_muic_irqs[i]; 897c05c0d54SAndrzej Hajda int virq = 0; 89827a28d32SJaewon Kim 89927a28d32SJaewon Kim virq = regmap_irq_get_virq(max77843->irq_data_muic, 90027a28d32SJaewon Kim muic_irq->irq); 90127a28d32SJaewon Kim if (virq <= 0) { 90227a28d32SJaewon Kim ret = -EINVAL; 90327a28d32SJaewon Kim goto err_muic_irq; 90427a28d32SJaewon Kim } 90527a28d32SJaewon Kim muic_irq->virq = virq; 90627a28d32SJaewon Kim 90727a28d32SJaewon Kim ret = devm_request_threaded_irq(&pdev->dev, virq, NULL, 90827a28d32SJaewon Kim max77843_muic_irq_handler, IRQF_NO_SUSPEND, 90927a28d32SJaewon Kim muic_irq->name, info); 91027a28d32SJaewon Kim if (ret) { 91127a28d32SJaewon Kim dev_err(&pdev->dev, 91227a28d32SJaewon Kim "Failed to request irq (IRQ: %d, error: %d)\n", 91327a28d32SJaewon Kim muic_irq->irq, ret); 91427a28d32SJaewon Kim goto err_muic_irq; 91527a28d32SJaewon Kim } 91627a28d32SJaewon Kim } 91727a28d32SJaewon Kim 91827a28d32SJaewon Kim /* Detect accessory after completing the initialization of platform */ 91927a28d32SJaewon Kim INIT_DELAYED_WORK(&info->wq_detcable, max77843_muic_detect_cable_wq); 92027a28d32SJaewon Kim queue_delayed_work(system_power_efficient_wq, 92127a28d32SJaewon Kim &info->wq_detcable, msecs_to_jiffies(DELAY_MS_DEFAULT)); 92227a28d32SJaewon Kim 92327a28d32SJaewon Kim return 0; 92427a28d32SJaewon Kim 92527a28d32SJaewon Kim err_muic_irq: 92627a28d32SJaewon Kim regmap_del_irq_chip(max77843->irq, max77843->irq_data_muic); 92727a28d32SJaewon Kim i2c_unregister_device(max77843->i2c_muic); 92827a28d32SJaewon Kim 92927a28d32SJaewon Kim return ret; 93027a28d32SJaewon Kim } 93127a28d32SJaewon Kim 93227a28d32SJaewon Kim static int max77843_muic_remove(struct platform_device *pdev) 93327a28d32SJaewon Kim { 93427a28d32SJaewon Kim struct max77843_muic_info *info = platform_get_drvdata(pdev); 935bc1aadc1SKrzysztof Kozlowski struct max77693_dev *max77843 = info->max77843; 93627a28d32SJaewon Kim 93727a28d32SJaewon Kim cancel_work_sync(&info->irq_work); 93827a28d32SJaewon Kim regmap_del_irq_chip(max77843->irq, max77843->irq_data_muic); 93927a28d32SJaewon Kim i2c_unregister_device(max77843->i2c_muic); 94027a28d32SJaewon Kim 94127a28d32SJaewon Kim return 0; 94227a28d32SJaewon Kim } 94327a28d32SJaewon Kim 94427a28d32SJaewon Kim static const struct platform_device_id max77843_muic_id[] = { 94527a28d32SJaewon Kim { "max77843-muic", }, 94627a28d32SJaewon Kim { /* sentinel */ }, 94727a28d32SJaewon Kim }; 94827a28d32SJaewon Kim MODULE_DEVICE_TABLE(platform, max77843_muic_id); 94927a28d32SJaewon Kim 95027a28d32SJaewon Kim static struct platform_driver max77843_muic_driver = { 95127a28d32SJaewon Kim .driver = { 95227a28d32SJaewon Kim .name = "max77843-muic", 95327a28d32SJaewon Kim }, 95427a28d32SJaewon Kim .probe = max77843_muic_probe, 95527a28d32SJaewon Kim .remove = max77843_muic_remove, 95627a28d32SJaewon Kim .id_table = max77843_muic_id, 95727a28d32SJaewon Kim }; 95827a28d32SJaewon Kim 95927a28d32SJaewon Kim static int __init max77843_muic_init(void) 96027a28d32SJaewon Kim { 96127a28d32SJaewon Kim return platform_driver_register(&max77843_muic_driver); 96227a28d32SJaewon Kim } 96327a28d32SJaewon Kim subsys_initcall(max77843_muic_init); 96427a28d32SJaewon Kim 96527a28d32SJaewon Kim MODULE_DESCRIPTION("Maxim MAX77843 Extcon driver"); 96627a28d32SJaewon Kim MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>"); 96727a28d32SJaewon Kim MODULE_LICENSE("GPL"); 968