127a28d32SJaewon Kim /* 227a28d32SJaewon Kim * extcon-max77843.c - Maxim MAX77843 extcon driver to support 327a28d32SJaewon Kim * MUIC(Micro USB Interface Controller) 427a28d32SJaewon Kim * 527a28d32SJaewon Kim * Copyright (C) 2015 Samsung Electronics 627a28d32SJaewon Kim * Author: Jaewon Kim <jaewon02.kim@samsung.com> 727a28d32SJaewon Kim * 827a28d32SJaewon Kim * This program is free software; you can redistribute it and/or modify 927a28d32SJaewon Kim * it under the terms of the GNU General Public License as published by 1027a28d32SJaewon Kim * the Free Software Foundation; either version 2 of the License, or 1127a28d32SJaewon Kim * (at your option) any later version. 1227a28d32SJaewon Kim */ 1327a28d32SJaewon Kim 1427a28d32SJaewon Kim #include <linux/extcon.h> 1527a28d32SJaewon Kim #include <linux/i2c.h> 1627a28d32SJaewon Kim #include <linux/interrupt.h> 1727a28d32SJaewon Kim #include <linux/kernel.h> 18bc1aadc1SKrzysztof Kozlowski #include <linux/mfd/max77693-common.h> 1927a28d32SJaewon Kim #include <linux/mfd/max77843-private.h> 2027a28d32SJaewon Kim #include <linux/module.h> 2127a28d32SJaewon Kim #include <linux/platform_device.h> 2227a28d32SJaewon Kim #include <linux/workqueue.h> 2327a28d32SJaewon Kim 2427a28d32SJaewon Kim #define DELAY_MS_DEFAULT 15000 /* unit: millisecond */ 2527a28d32SJaewon Kim 2627a28d32SJaewon Kim enum max77843_muic_status { 2727a28d32SJaewon Kim MAX77843_MUIC_STATUS1 = 0, 2827a28d32SJaewon Kim MAX77843_MUIC_STATUS2, 2927a28d32SJaewon Kim MAX77843_MUIC_STATUS3, 3027a28d32SJaewon Kim 3127a28d32SJaewon Kim MAX77843_MUIC_STATUS_NUM, 3227a28d32SJaewon Kim }; 3327a28d32SJaewon Kim 3427a28d32SJaewon Kim struct max77843_muic_info { 3527a28d32SJaewon Kim struct device *dev; 36bc1aadc1SKrzysztof Kozlowski struct max77693_dev *max77843; 3727a28d32SJaewon Kim struct extcon_dev *edev; 3827a28d32SJaewon Kim 3927a28d32SJaewon Kim struct mutex mutex; 4027a28d32SJaewon Kim struct work_struct irq_work; 4127a28d32SJaewon Kim struct delayed_work wq_detcable; 4227a28d32SJaewon Kim 4327a28d32SJaewon Kim u8 status[MAX77843_MUIC_STATUS_NUM]; 4427a28d32SJaewon Kim int prev_cable_type; 4527a28d32SJaewon Kim int prev_chg_type; 4627a28d32SJaewon Kim int prev_gnd_type; 4727a28d32SJaewon Kim 4827a28d32SJaewon Kim bool irq_adc; 4927a28d32SJaewon Kim bool irq_chg; 5027a28d32SJaewon Kim }; 5127a28d32SJaewon Kim 5227a28d32SJaewon Kim enum max77843_muic_cable_group { 5327a28d32SJaewon Kim MAX77843_CABLE_GROUP_ADC = 0, 5427a28d32SJaewon Kim MAX77843_CABLE_GROUP_ADC_GND, 5527a28d32SJaewon Kim MAX77843_CABLE_GROUP_CHG, 5627a28d32SJaewon Kim }; 5727a28d32SJaewon Kim 5827a28d32SJaewon Kim enum max77843_muic_adc_debounce_time { 5927a28d32SJaewon Kim MAX77843_DEBOUNCE_TIME_5MS = 0, 6027a28d32SJaewon Kim MAX77843_DEBOUNCE_TIME_10MS, 6127a28d32SJaewon Kim MAX77843_DEBOUNCE_TIME_25MS, 6227a28d32SJaewon Kim MAX77843_DEBOUNCE_TIME_38_62MS, 6327a28d32SJaewon Kim }; 6427a28d32SJaewon Kim 6527a28d32SJaewon Kim /* Define accessory cable type */ 6627a28d32SJaewon Kim enum max77843_muic_accessory_type { 6727a28d32SJaewon Kim MAX77843_MUIC_ADC_GROUND = 0, 6827a28d32SJaewon Kim MAX77843_MUIC_ADC_SEND_END_BUTTON, 6927a28d32SJaewon Kim MAX77843_MUIC_ADC_REMOTE_S1_BUTTON, 7027a28d32SJaewon Kim MAX77843_MUIC_ADC_REMOTE_S2_BUTTON, 7127a28d32SJaewon Kim MAX77843_MUIC_ADC_REMOTE_S3_BUTTON, 7227a28d32SJaewon Kim MAX77843_MUIC_ADC_REMOTE_S4_BUTTON, 7327a28d32SJaewon Kim MAX77843_MUIC_ADC_REMOTE_S5_BUTTON, 7427a28d32SJaewon Kim MAX77843_MUIC_ADC_REMOTE_S6_BUTTON, 7527a28d32SJaewon Kim MAX77843_MUIC_ADC_REMOTE_S7_BUTTON, 7627a28d32SJaewon Kim MAX77843_MUIC_ADC_REMOTE_S8_BUTTON, 7727a28d32SJaewon Kim MAX77843_MUIC_ADC_REMOTE_S9_BUTTON, 7827a28d32SJaewon Kim MAX77843_MUIC_ADC_REMOTE_S10_BUTTON, 7927a28d32SJaewon Kim MAX77843_MUIC_ADC_REMOTE_S11_BUTTON, 8027a28d32SJaewon Kim MAX77843_MUIC_ADC_REMOTE_S12_BUTTON, 8127a28d32SJaewon Kim MAX77843_MUIC_ADC_RESERVED_ACC_1, 8227a28d32SJaewon Kim MAX77843_MUIC_ADC_RESERVED_ACC_2, 8327a28d32SJaewon Kim MAX77843_MUIC_ADC_RESERVED_ACC_3, 8427a28d32SJaewon Kim MAX77843_MUIC_ADC_RESERVED_ACC_4, 8527a28d32SJaewon Kim MAX77843_MUIC_ADC_RESERVED_ACC_5, 8627a28d32SJaewon Kim MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE2, 8727a28d32SJaewon Kim MAX77843_MUIC_ADC_PHONE_POWERED_DEV, 8827a28d32SJaewon Kim MAX77843_MUIC_ADC_TTY_CONVERTER, 8927a28d32SJaewon Kim MAX77843_MUIC_ADC_UART_CABLE, 9027a28d32SJaewon Kim MAX77843_MUIC_ADC_CEA936A_TYPE1_CHG, 9127a28d32SJaewon Kim MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF, 9227a28d32SJaewon Kim MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON, 9327a28d32SJaewon Kim MAX77843_MUIC_ADC_AV_CABLE_NOLOAD, 9427a28d32SJaewon Kim MAX77843_MUIC_ADC_CEA936A_TYPE2_CHG, 9527a28d32SJaewon Kim MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF, 9627a28d32SJaewon Kim MAX77843_MUIC_ADC_FACTORY_MODE_UART_ON, 9727a28d32SJaewon Kim MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE1, 9827a28d32SJaewon Kim MAX77843_MUIC_ADC_OPEN, 9927a28d32SJaewon Kim 10027a28d32SJaewon Kim /* The blow accessories should check 10127a28d32SJaewon Kim not only ADC value but also ADC1K and VBVolt value. */ 10227a28d32SJaewon Kim /* Offset|ADC1K|VBVolt| */ 10327a28d32SJaewon Kim MAX77843_MUIC_GND_USB_HOST = 0x100, /* 0x1| 0| 0| */ 10427a28d32SJaewon Kim MAX77843_MUIC_GND_USB_HOST_VB = 0x101, /* 0x1| 0| 1| */ 10527a28d32SJaewon Kim MAX77843_MUIC_GND_MHL = 0x102, /* 0x1| 1| 0| */ 10627a28d32SJaewon Kim MAX77843_MUIC_GND_MHL_VB = 0x103, /* 0x1| 1| 1| */ 10727a28d32SJaewon Kim }; 10827a28d32SJaewon Kim 10927a28d32SJaewon Kim /* Define charger cable type */ 11027a28d32SJaewon Kim enum max77843_muic_charger_type { 11127a28d32SJaewon Kim MAX77843_MUIC_CHG_NONE = 0, 11227a28d32SJaewon Kim MAX77843_MUIC_CHG_USB, 11327a28d32SJaewon Kim MAX77843_MUIC_CHG_DOWNSTREAM, 11427a28d32SJaewon Kim MAX77843_MUIC_CHG_DEDICATED, 11527a28d32SJaewon Kim MAX77843_MUIC_CHG_SPECIAL_500MA, 11627a28d32SJaewon Kim MAX77843_MUIC_CHG_SPECIAL_1A, 11727a28d32SJaewon Kim MAX77843_MUIC_CHG_SPECIAL_BIAS, 11827a28d32SJaewon Kim MAX77843_MUIC_CHG_RESERVED, 11927a28d32SJaewon Kim MAX77843_MUIC_CHG_GND, 12027a28d32SJaewon Kim }; 12127a28d32SJaewon Kim 12273b6ecdbSChanwoo Choi static const unsigned int max77843_extcon_cable[] = { 1232a9de9c0SChanwoo Choi EXTCON_USB, 1242a9de9c0SChanwoo Choi EXTCON_USB_HOST, 1252a9de9c0SChanwoo Choi EXTCON_TA, 1262a9de9c0SChanwoo Choi EXTCON_CHARGE_DOWNSTREAM, 1272a9de9c0SChanwoo Choi EXTCON_FAST_CHARGER, 1282a9de9c0SChanwoo Choi EXTCON_SLOW_CHARGER, 1292a9de9c0SChanwoo Choi EXTCON_MHL, 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, 20027a28d32SJaewon Kim u8 val, bool attached) 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 209*309a3e00SKrzysztof Kozlowski ctrl1 = MAX77843_MUIC_CONTROL1_SW_OPEN; 21027a28d32SJaewon Kim 21127a28d32SJaewon Kim ret = regmap_update_bits(max77843->regmap_muic, 21227a28d32SJaewon Kim MAX77843_MUIC_REG_CONTROL1, 213*309a3e00SKrzysztof Kozlowski MAX77843_MUIC_CONTROL1_COM_SW, ctrl1); 21427a28d32SJaewon Kim if (ret < 0) { 21527a28d32SJaewon Kim dev_err(info->dev, "Cannot switch MUIC port\n"); 21627a28d32SJaewon Kim return ret; 21727a28d32SJaewon Kim } 21827a28d32SJaewon Kim 21927a28d32SJaewon Kim if (attached) 22027a28d32SJaewon Kim ctrl2 = MAX77843_MUIC_CONTROL2_CPEN_MASK; 22127a28d32SJaewon Kim else 22227a28d32SJaewon Kim ctrl2 = MAX77843_MUIC_CONTROL2_LOWPWR_MASK; 22327a28d32SJaewon Kim 22427a28d32SJaewon Kim ret = regmap_update_bits(max77843->regmap_muic, 22527a28d32SJaewon Kim MAX77843_MUIC_REG_CONTROL2, 22627a28d32SJaewon Kim MAX77843_MUIC_CONTROL2_LOWPWR_MASK | 22727a28d32SJaewon Kim MAX77843_MUIC_CONTROL2_CPEN_MASK, ctrl2); 22827a28d32SJaewon Kim if (ret < 0) { 22927a28d32SJaewon Kim dev_err(info->dev, "Cannot update lowpower mode\n"); 23027a28d32SJaewon Kim return ret; 23127a28d32SJaewon Kim } 23227a28d32SJaewon Kim 23327a28d32SJaewon Kim dev_dbg(info->dev, 23427a28d32SJaewon Kim "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n", 23527a28d32SJaewon Kim ctrl1, ctrl2, attached ? "attached" : "detached"); 23627a28d32SJaewon Kim 23727a28d32SJaewon Kim return 0; 23827a28d32SJaewon Kim } 23927a28d32SJaewon Kim 24027a28d32SJaewon Kim static int max77843_muic_get_cable_type(struct max77843_muic_info *info, 24127a28d32SJaewon Kim enum max77843_muic_cable_group group, bool *attached) 24227a28d32SJaewon Kim { 24327a28d32SJaewon Kim int adc, chg_type, cable_type, gnd_type; 24427a28d32SJaewon Kim 24527a28d32SJaewon Kim adc = info->status[MAX77843_MUIC_STATUS1] & 24627a28d32SJaewon Kim MAX77843_MUIC_STATUS1_ADC_MASK; 247*309a3e00SKrzysztof Kozlowski adc >>= MAX77843_MUIC_STATUS1_ADC_SHIFT; 24827a28d32SJaewon Kim 24927a28d32SJaewon Kim switch (group) { 25027a28d32SJaewon Kim case MAX77843_CABLE_GROUP_ADC: 25127a28d32SJaewon Kim if (adc == MAX77843_MUIC_ADC_OPEN) { 25227a28d32SJaewon Kim *attached = false; 25327a28d32SJaewon Kim cable_type = info->prev_cable_type; 25427a28d32SJaewon Kim info->prev_cable_type = MAX77843_MUIC_ADC_OPEN; 25527a28d32SJaewon Kim } else { 25627a28d32SJaewon Kim *attached = true; 25727a28d32SJaewon Kim cable_type = info->prev_cable_type = adc; 25827a28d32SJaewon Kim } 25927a28d32SJaewon Kim break; 26027a28d32SJaewon Kim case MAX77843_CABLE_GROUP_CHG: 26127a28d32SJaewon Kim chg_type = info->status[MAX77843_MUIC_STATUS2] & 26227a28d32SJaewon Kim MAX77843_MUIC_STATUS2_CHGTYP_MASK; 26327a28d32SJaewon Kim 26427a28d32SJaewon Kim /* Check GROUND accessory with charger cable */ 26527a28d32SJaewon Kim if (adc == MAX77843_MUIC_ADC_GROUND) { 26627a28d32SJaewon Kim if (chg_type == MAX77843_MUIC_CHG_NONE) { 26727a28d32SJaewon Kim /* The following state when charger cable is 26827a28d32SJaewon Kim * disconnected but the GROUND accessory still 26927a28d32SJaewon Kim * connected */ 27027a28d32SJaewon Kim *attached = false; 27127a28d32SJaewon Kim cable_type = info->prev_chg_type; 27227a28d32SJaewon Kim info->prev_chg_type = MAX77843_MUIC_CHG_NONE; 27327a28d32SJaewon Kim } else { 27427a28d32SJaewon Kim 27527a28d32SJaewon Kim /* The following state when charger cable is 27627a28d32SJaewon Kim * connected on the GROUND accessory */ 27727a28d32SJaewon Kim *attached = true; 27827a28d32SJaewon Kim cable_type = MAX77843_MUIC_CHG_GND; 27927a28d32SJaewon Kim info->prev_chg_type = MAX77843_MUIC_CHG_GND; 28027a28d32SJaewon Kim } 28127a28d32SJaewon Kim break; 28227a28d32SJaewon Kim } 28327a28d32SJaewon Kim 28427a28d32SJaewon Kim if (chg_type == MAX77843_MUIC_CHG_NONE) { 28527a28d32SJaewon Kim *attached = false; 28627a28d32SJaewon Kim cable_type = info->prev_chg_type; 28727a28d32SJaewon Kim info->prev_chg_type = MAX77843_MUIC_CHG_NONE; 28827a28d32SJaewon Kim } else { 28927a28d32SJaewon Kim *attached = true; 29027a28d32SJaewon Kim cable_type = info->prev_chg_type = chg_type; 29127a28d32SJaewon Kim } 29227a28d32SJaewon Kim break; 29327a28d32SJaewon Kim case MAX77843_CABLE_GROUP_ADC_GND: 29427a28d32SJaewon Kim if (adc == MAX77843_MUIC_ADC_OPEN) { 29527a28d32SJaewon Kim *attached = false; 29627a28d32SJaewon Kim cable_type = info->prev_gnd_type; 29727a28d32SJaewon Kim info->prev_gnd_type = MAX77843_MUIC_ADC_OPEN; 29827a28d32SJaewon Kim } else { 29927a28d32SJaewon Kim *attached = true; 30027a28d32SJaewon Kim 30127a28d32SJaewon Kim /* Offset|ADC1K|VBVolt| 30227a28d32SJaewon Kim * 0x1| 0| 0| USB-HOST 30327a28d32SJaewon Kim * 0x1| 0| 1| USB-HOST with VB 30427a28d32SJaewon Kim * 0x1| 1| 0| MHL 30527a28d32SJaewon Kim * 0x1| 1| 1| MHL with VB */ 30627a28d32SJaewon Kim /* Get ADC1K register bit */ 30727a28d32SJaewon Kim gnd_type = (info->status[MAX77843_MUIC_STATUS1] & 30827a28d32SJaewon Kim MAX77843_MUIC_STATUS1_ADC1K_MASK); 30927a28d32SJaewon Kim 31027a28d32SJaewon Kim /* Get VBVolt register bit */ 31127a28d32SJaewon Kim gnd_type |= (info->status[MAX77843_MUIC_STATUS2] & 31227a28d32SJaewon Kim MAX77843_MUIC_STATUS2_VBVOLT_MASK); 313*309a3e00SKrzysztof Kozlowski gnd_type >>= MAX77843_MUIC_STATUS2_VBVOLT_SHIFT; 31427a28d32SJaewon Kim 31527a28d32SJaewon Kim /* Offset of GND cable */ 31627a28d32SJaewon Kim gnd_type |= MAX77843_MUIC_GND_USB_HOST; 31727a28d32SJaewon Kim cable_type = info->prev_gnd_type = gnd_type; 31827a28d32SJaewon Kim } 31927a28d32SJaewon Kim break; 32027a28d32SJaewon Kim default: 32127a28d32SJaewon Kim dev_err(info->dev, "Unknown cable group (%d)\n", group); 32227a28d32SJaewon Kim cable_type = -EINVAL; 32327a28d32SJaewon Kim break; 32427a28d32SJaewon Kim } 32527a28d32SJaewon Kim 32627a28d32SJaewon Kim return cable_type; 32727a28d32SJaewon Kim } 32827a28d32SJaewon Kim 32927a28d32SJaewon Kim static int max77843_muic_adc_gnd_handler(struct max77843_muic_info *info) 33027a28d32SJaewon Kim { 33127a28d32SJaewon Kim int ret, gnd_cable_type; 33227a28d32SJaewon Kim bool attached; 33327a28d32SJaewon Kim 33427a28d32SJaewon Kim gnd_cable_type = max77843_muic_get_cable_type(info, 33527a28d32SJaewon Kim MAX77843_CABLE_GROUP_ADC_GND, &attached); 33627a28d32SJaewon Kim dev_dbg(info->dev, "external connector is %s (gnd:0x%02x)\n", 33727a28d32SJaewon Kim attached ? "attached" : "detached", gnd_cable_type); 33827a28d32SJaewon Kim 33927a28d32SJaewon Kim switch (gnd_cable_type) { 34027a28d32SJaewon Kim case MAX77843_MUIC_GND_USB_HOST: 34127a28d32SJaewon Kim case MAX77843_MUIC_GND_USB_HOST_VB: 342*309a3e00SKrzysztof Kozlowski ret = max77843_muic_set_path(info, 343*309a3e00SKrzysztof Kozlowski MAX77843_MUIC_CONTROL1_SW_USB, 344*309a3e00SKrzysztof Kozlowski attached); 34527a28d32SJaewon Kim if (ret < 0) 34627a28d32SJaewon Kim return ret; 34727a28d32SJaewon Kim 3482a9de9c0SChanwoo Choi extcon_set_cable_state_(info->edev, EXTCON_USB_HOST, attached); 34927a28d32SJaewon Kim break; 35027a28d32SJaewon Kim case MAX77843_MUIC_GND_MHL_VB: 35127a28d32SJaewon Kim case MAX77843_MUIC_GND_MHL: 352*309a3e00SKrzysztof Kozlowski ret = max77843_muic_set_path(info, 353*309a3e00SKrzysztof Kozlowski MAX77843_MUIC_CONTROL1_SW_OPEN, 354*309a3e00SKrzysztof Kozlowski attached); 35527a28d32SJaewon Kim if (ret < 0) 35627a28d32SJaewon Kim return ret; 35727a28d32SJaewon Kim 3582a9de9c0SChanwoo Choi extcon_set_cable_state_(info->edev, EXTCON_MHL, attached); 35927a28d32SJaewon Kim break; 36027a28d32SJaewon Kim default: 36127a28d32SJaewon Kim dev_err(info->dev, "failed to detect %s accessory(gnd:0x%x)\n", 36227a28d32SJaewon Kim attached ? "attached" : "detached", gnd_cable_type); 36327a28d32SJaewon Kim return -EINVAL; 36427a28d32SJaewon Kim } 36527a28d32SJaewon Kim 36627a28d32SJaewon Kim return 0; 36727a28d32SJaewon Kim } 36827a28d32SJaewon Kim 36927a28d32SJaewon Kim static int max77843_muic_jig_handler(struct max77843_muic_info *info, 37027a28d32SJaewon Kim int cable_type, bool attached) 37127a28d32SJaewon Kim { 37227a28d32SJaewon Kim int ret; 373*309a3e00SKrzysztof Kozlowski u8 path = MAX77843_MUIC_CONTROL1_SW_OPEN; 37427a28d32SJaewon Kim 37527a28d32SJaewon Kim dev_dbg(info->dev, "external connector is %s (adc:0x%02x)\n", 37627a28d32SJaewon Kim attached ? "attached" : "detached", cable_type); 37727a28d32SJaewon Kim 37827a28d32SJaewon Kim switch (cable_type) { 37927a28d32SJaewon Kim case MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF: 38027a28d32SJaewon Kim case MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON: 381*309a3e00SKrzysztof Kozlowski path = MAX77843_MUIC_CONTROL1_SW_USB; 38227a28d32SJaewon Kim break; 38327a28d32SJaewon Kim case MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF: 384*309a3e00SKrzysztof Kozlowski path = MAX77843_MUIC_CONTROL1_SW_UART; 38527a28d32SJaewon Kim break; 38627a28d32SJaewon Kim default: 38741b3c015SChanwoo Choi return -EINVAL; 38841b3c015SChanwoo Choi } 38941b3c015SChanwoo Choi 39041b3c015SChanwoo Choi ret = max77843_muic_set_path(info, path, attached); 39127a28d32SJaewon Kim if (ret < 0) 39227a28d32SJaewon Kim return ret; 39341b3c015SChanwoo Choi 3942a9de9c0SChanwoo Choi extcon_set_cable_state_(info->edev, EXTCON_JIG, attached); 39527a28d32SJaewon Kim 39627a28d32SJaewon Kim return 0; 39727a28d32SJaewon Kim } 39827a28d32SJaewon Kim 39927a28d32SJaewon Kim static int max77843_muic_adc_handler(struct max77843_muic_info *info) 40027a28d32SJaewon Kim { 40127a28d32SJaewon Kim int ret, cable_type; 40227a28d32SJaewon Kim bool attached; 40327a28d32SJaewon Kim 40427a28d32SJaewon Kim cable_type = max77843_muic_get_cable_type(info, 40527a28d32SJaewon Kim MAX77843_CABLE_GROUP_ADC, &attached); 40627a28d32SJaewon Kim 40727a28d32SJaewon Kim dev_dbg(info->dev, 40827a28d32SJaewon Kim "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n", 40927a28d32SJaewon Kim attached ? "attached" : "detached", cable_type, 41027a28d32SJaewon Kim info->prev_cable_type); 41127a28d32SJaewon Kim 41227a28d32SJaewon Kim switch (cable_type) { 41327a28d32SJaewon Kim case MAX77843_MUIC_ADC_GROUND: 41427a28d32SJaewon Kim ret = max77843_muic_adc_gnd_handler(info); 41527a28d32SJaewon Kim if (ret < 0) 41627a28d32SJaewon Kim return ret; 41727a28d32SJaewon Kim break; 41827a28d32SJaewon Kim case MAX77843_MUIC_ADC_FACTORY_MODE_USB_OFF: 41927a28d32SJaewon Kim case MAX77843_MUIC_ADC_FACTORY_MODE_USB_ON: 42027a28d32SJaewon Kim case MAX77843_MUIC_ADC_FACTORY_MODE_UART_OFF: 42127a28d32SJaewon Kim ret = max77843_muic_jig_handler(info, cable_type, attached); 42227a28d32SJaewon Kim if (ret < 0) 42327a28d32SJaewon Kim return ret; 42427a28d32SJaewon Kim break; 42527a28d32SJaewon Kim case MAX77843_MUIC_ADC_SEND_END_BUTTON: 42627a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S1_BUTTON: 42727a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S2_BUTTON: 42827a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S3_BUTTON: 42927a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S4_BUTTON: 43027a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S5_BUTTON: 43127a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S6_BUTTON: 43227a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S7_BUTTON: 43327a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S8_BUTTON: 43427a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S9_BUTTON: 43527a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S10_BUTTON: 43627a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S11_BUTTON: 43727a28d32SJaewon Kim case MAX77843_MUIC_ADC_REMOTE_S12_BUTTON: 43827a28d32SJaewon Kim case MAX77843_MUIC_ADC_RESERVED_ACC_1: 43927a28d32SJaewon Kim case MAX77843_MUIC_ADC_RESERVED_ACC_2: 44027a28d32SJaewon Kim case MAX77843_MUIC_ADC_RESERVED_ACC_3: 44127a28d32SJaewon Kim case MAX77843_MUIC_ADC_RESERVED_ACC_4: 44227a28d32SJaewon Kim case MAX77843_MUIC_ADC_RESERVED_ACC_5: 44327a28d32SJaewon Kim case MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE2: 44427a28d32SJaewon Kim case MAX77843_MUIC_ADC_PHONE_POWERED_DEV: 44527a28d32SJaewon Kim case MAX77843_MUIC_ADC_TTY_CONVERTER: 44627a28d32SJaewon Kim case MAX77843_MUIC_ADC_UART_CABLE: 44727a28d32SJaewon Kim case MAX77843_MUIC_ADC_CEA936A_TYPE1_CHG: 44827a28d32SJaewon Kim case MAX77843_MUIC_ADC_AV_CABLE_NOLOAD: 44927a28d32SJaewon Kim case MAX77843_MUIC_ADC_CEA936A_TYPE2_CHG: 45027a28d32SJaewon Kim case MAX77843_MUIC_ADC_FACTORY_MODE_UART_ON: 45127a28d32SJaewon Kim case MAX77843_MUIC_ADC_AUDIO_DEVICE_TYPE1: 45227a28d32SJaewon Kim case MAX77843_MUIC_ADC_OPEN: 45327a28d32SJaewon Kim dev_err(info->dev, 45427a28d32SJaewon Kim "accessory is %s but it isn't used (adc:0x%x)\n", 45527a28d32SJaewon Kim attached ? "attached" : "detached", cable_type); 45627a28d32SJaewon Kim return -EAGAIN; 45727a28d32SJaewon Kim default: 45827a28d32SJaewon Kim dev_err(info->dev, 45927a28d32SJaewon Kim "failed to detect %s accessory (adc:0x%x)\n", 46027a28d32SJaewon Kim attached ? "attached" : "detached", cable_type); 46127a28d32SJaewon Kim return -EINVAL; 46227a28d32SJaewon Kim } 46327a28d32SJaewon Kim 46427a28d32SJaewon Kim return 0; 46527a28d32SJaewon Kim } 46627a28d32SJaewon Kim 46727a28d32SJaewon Kim static int max77843_muic_chg_handler(struct max77843_muic_info *info) 46827a28d32SJaewon Kim { 46927a28d32SJaewon Kim int ret, chg_type, gnd_type; 47027a28d32SJaewon Kim bool attached; 47127a28d32SJaewon Kim 47227a28d32SJaewon Kim chg_type = max77843_muic_get_cable_type(info, 47327a28d32SJaewon Kim MAX77843_CABLE_GROUP_CHG, &attached); 47427a28d32SJaewon Kim 47527a28d32SJaewon Kim dev_dbg(info->dev, 47627a28d32SJaewon Kim "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n", 47727a28d32SJaewon Kim attached ? "attached" : "detached", 47827a28d32SJaewon Kim chg_type, info->prev_chg_type); 47927a28d32SJaewon Kim 48027a28d32SJaewon Kim switch (chg_type) { 48127a28d32SJaewon Kim case MAX77843_MUIC_CHG_USB: 482*309a3e00SKrzysztof Kozlowski ret = max77843_muic_set_path(info, 483*309a3e00SKrzysztof Kozlowski MAX77843_MUIC_CONTROL1_SW_USB, 484*309a3e00SKrzysztof Kozlowski attached); 48527a28d32SJaewon Kim if (ret < 0) 48627a28d32SJaewon Kim return ret; 48727a28d32SJaewon Kim 4882a9de9c0SChanwoo Choi extcon_set_cable_state_(info->edev, EXTCON_USB, attached); 48927a28d32SJaewon Kim break; 49027a28d32SJaewon Kim case MAX77843_MUIC_CHG_DOWNSTREAM: 491*309a3e00SKrzysztof Kozlowski ret = max77843_muic_set_path(info, 492*309a3e00SKrzysztof Kozlowski MAX77843_MUIC_CONTROL1_SW_OPEN, 493*309a3e00SKrzysztof Kozlowski attached); 49427a28d32SJaewon Kim if (ret < 0) 49527a28d32SJaewon Kim return ret; 49627a28d32SJaewon Kim 4972a9de9c0SChanwoo Choi extcon_set_cable_state_(info->edev, EXTCON_CHARGE_DOWNSTREAM, 4982a9de9c0SChanwoo Choi attached); 49927a28d32SJaewon Kim break; 50027a28d32SJaewon Kim case MAX77843_MUIC_CHG_DEDICATED: 501*309a3e00SKrzysztof Kozlowski ret = max77843_muic_set_path(info, 502*309a3e00SKrzysztof Kozlowski MAX77843_MUIC_CONTROL1_SW_OPEN, 503*309a3e00SKrzysztof Kozlowski attached); 50427a28d32SJaewon Kim if (ret < 0) 50527a28d32SJaewon Kim return ret; 50627a28d32SJaewon Kim 5072a9de9c0SChanwoo Choi extcon_set_cable_state_(info->edev, EXTCON_TA, attached); 50827a28d32SJaewon Kim break; 50927a28d32SJaewon Kim case MAX77843_MUIC_CHG_SPECIAL_500MA: 510*309a3e00SKrzysztof Kozlowski ret = max77843_muic_set_path(info, 511*309a3e00SKrzysztof Kozlowski MAX77843_MUIC_CONTROL1_SW_OPEN, 512*309a3e00SKrzysztof Kozlowski attached); 51327a28d32SJaewon Kim if (ret < 0) 51427a28d32SJaewon Kim return ret; 51527a28d32SJaewon Kim 5162a9de9c0SChanwoo Choi extcon_set_cable_state_(info->edev, EXTCON_SLOW_CHARGER, 5172a9de9c0SChanwoo Choi attached); 51827a28d32SJaewon Kim break; 51927a28d32SJaewon Kim case MAX77843_MUIC_CHG_SPECIAL_1A: 520*309a3e00SKrzysztof Kozlowski ret = max77843_muic_set_path(info, 521*309a3e00SKrzysztof Kozlowski MAX77843_MUIC_CONTROL1_SW_OPEN, 522*309a3e00SKrzysztof Kozlowski attached); 52327a28d32SJaewon Kim if (ret < 0) 52427a28d32SJaewon Kim return ret; 52527a28d32SJaewon Kim 5262a9de9c0SChanwoo Choi extcon_set_cable_state_(info->edev, EXTCON_FAST_CHARGER, 5272a9de9c0SChanwoo Choi attached); 52827a28d32SJaewon Kim break; 52927a28d32SJaewon Kim case MAX77843_MUIC_CHG_GND: 53027a28d32SJaewon Kim gnd_type = max77843_muic_get_cable_type(info, 53127a28d32SJaewon Kim MAX77843_CABLE_GROUP_ADC_GND, &attached); 53227a28d32SJaewon Kim 53327a28d32SJaewon Kim /* Charger cable on MHL accessory is attach or detach */ 53427a28d32SJaewon Kim if (gnd_type == MAX77843_MUIC_GND_MHL_VB) 5352a9de9c0SChanwoo Choi extcon_set_cable_state_(info->edev, EXTCON_TA, true); 53627a28d32SJaewon Kim else if (gnd_type == MAX77843_MUIC_GND_MHL) 5372a9de9c0SChanwoo Choi extcon_set_cable_state_(info->edev, EXTCON_TA, false); 53827a28d32SJaewon Kim break; 53927a28d32SJaewon Kim case MAX77843_MUIC_CHG_NONE: 54027a28d32SJaewon Kim break; 54127a28d32SJaewon Kim default: 54227a28d32SJaewon Kim dev_err(info->dev, 54327a28d32SJaewon Kim "failed to detect %s accessory (chg_type:0x%x)\n", 54427a28d32SJaewon Kim attached ? "attached" : "detached", chg_type); 54527a28d32SJaewon Kim 546*309a3e00SKrzysztof Kozlowski max77843_muic_set_path(info, MAX77843_MUIC_CONTROL1_SW_OPEN, 547*309a3e00SKrzysztof Kozlowski attached); 54827a28d32SJaewon Kim return -EINVAL; 54927a28d32SJaewon Kim } 55027a28d32SJaewon Kim 55127a28d32SJaewon Kim return 0; 55227a28d32SJaewon Kim } 55327a28d32SJaewon Kim 55427a28d32SJaewon Kim static void max77843_muic_irq_work(struct work_struct *work) 55527a28d32SJaewon Kim { 55627a28d32SJaewon Kim struct max77843_muic_info *info = container_of(work, 55727a28d32SJaewon Kim struct max77843_muic_info, irq_work); 558bc1aadc1SKrzysztof Kozlowski struct max77693_dev *max77843 = info->max77843; 55927a28d32SJaewon Kim int ret = 0; 56027a28d32SJaewon Kim 56127a28d32SJaewon Kim mutex_lock(&info->mutex); 56227a28d32SJaewon Kim 56327a28d32SJaewon Kim ret = regmap_bulk_read(max77843->regmap_muic, 56427a28d32SJaewon Kim MAX77843_MUIC_REG_STATUS1, info->status, 56527a28d32SJaewon Kim MAX77843_MUIC_STATUS_NUM); 56627a28d32SJaewon Kim if (ret) { 56727a28d32SJaewon Kim dev_err(info->dev, "Cannot read STATUS registers\n"); 56827a28d32SJaewon Kim mutex_unlock(&info->mutex); 56927a28d32SJaewon Kim return; 57027a28d32SJaewon Kim } 57127a28d32SJaewon Kim 57227a28d32SJaewon Kim if (info->irq_adc) { 57327a28d32SJaewon Kim ret = max77843_muic_adc_handler(info); 57427a28d32SJaewon Kim if (ret) 57527a28d32SJaewon Kim dev_err(info->dev, "Unknown cable type\n"); 57627a28d32SJaewon Kim info->irq_adc = false; 57727a28d32SJaewon Kim } 57827a28d32SJaewon Kim 57927a28d32SJaewon Kim if (info->irq_chg) { 58027a28d32SJaewon Kim ret = max77843_muic_chg_handler(info); 58127a28d32SJaewon Kim if (ret) 58227a28d32SJaewon Kim dev_err(info->dev, "Unknown charger type\n"); 58327a28d32SJaewon Kim info->irq_chg = false; 58427a28d32SJaewon Kim } 58527a28d32SJaewon Kim 58627a28d32SJaewon Kim mutex_unlock(&info->mutex); 58727a28d32SJaewon Kim } 58827a28d32SJaewon Kim 58927a28d32SJaewon Kim static irqreturn_t max77843_muic_irq_handler(int irq, void *data) 59027a28d32SJaewon Kim { 59127a28d32SJaewon Kim struct max77843_muic_info *info = data; 59227a28d32SJaewon Kim int i, irq_type = -1; 59327a28d32SJaewon Kim 59427a28d32SJaewon Kim for (i = 0; i < ARRAY_SIZE(max77843_muic_irqs); i++) 59527a28d32SJaewon Kim if (irq == max77843_muic_irqs[i].virq) 59627a28d32SJaewon Kim irq_type = max77843_muic_irqs[i].irq; 59727a28d32SJaewon Kim 59827a28d32SJaewon Kim switch (irq_type) { 59927a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT1_ADC: 60027a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT1_ADCERROR: 60127a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT1_ADC1K: 60227a28d32SJaewon Kim info->irq_adc = true; 60327a28d32SJaewon Kim break; 60427a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT2_CHGTYP: 60527a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT2_CHGDETRUN: 60627a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT2_DCDTMR: 60727a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT2_DXOVP: 60827a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT2_VBVOLT: 60927a28d32SJaewon Kim info->irq_chg = true; 61027a28d32SJaewon Kim break; 61127a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT3_VBADC: 61227a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT3_VDNMON: 61327a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT3_DNRES: 61427a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT3_MPNACK: 61527a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT3_MRXBUFOW: 61627a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT3_MRXTRF: 61727a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT3_MRXPERR: 61827a28d32SJaewon Kim case MAX77843_MUIC_IRQ_INT3_MRXRDY: 61927a28d32SJaewon Kim break; 62027a28d32SJaewon Kim default: 62127a28d32SJaewon Kim dev_err(info->dev, "Cannot recognize IRQ(%d)\n", irq_type); 62227a28d32SJaewon Kim break; 62327a28d32SJaewon Kim } 62427a28d32SJaewon Kim 62527a28d32SJaewon Kim schedule_work(&info->irq_work); 62627a28d32SJaewon Kim 62727a28d32SJaewon Kim return IRQ_HANDLED; 62827a28d32SJaewon Kim } 62927a28d32SJaewon Kim 63027a28d32SJaewon Kim static void max77843_muic_detect_cable_wq(struct work_struct *work) 63127a28d32SJaewon Kim { 63227a28d32SJaewon Kim struct max77843_muic_info *info = container_of(to_delayed_work(work), 63327a28d32SJaewon Kim struct max77843_muic_info, wq_detcable); 634bc1aadc1SKrzysztof Kozlowski struct max77693_dev *max77843 = info->max77843; 63527a28d32SJaewon Kim int chg_type, adc, ret; 63627a28d32SJaewon Kim bool attached; 63727a28d32SJaewon Kim 63827a28d32SJaewon Kim mutex_lock(&info->mutex); 63927a28d32SJaewon Kim 64027a28d32SJaewon Kim ret = regmap_bulk_read(max77843->regmap_muic, 64127a28d32SJaewon Kim MAX77843_MUIC_REG_STATUS1, info->status, 64227a28d32SJaewon Kim MAX77843_MUIC_STATUS_NUM); 64327a28d32SJaewon Kim if (ret) { 64427a28d32SJaewon Kim dev_err(info->dev, "Cannot read STATUS registers\n"); 64527a28d32SJaewon Kim goto err_cable_wq; 64627a28d32SJaewon Kim } 64727a28d32SJaewon Kim 64827a28d32SJaewon Kim adc = max77843_muic_get_cable_type(info, 64927a28d32SJaewon Kim MAX77843_CABLE_GROUP_ADC, &attached); 65027a28d32SJaewon Kim if (attached && adc != MAX77843_MUIC_ADC_OPEN) { 65127a28d32SJaewon Kim ret = max77843_muic_adc_handler(info); 65227a28d32SJaewon Kim if (ret < 0) { 65327a28d32SJaewon Kim dev_err(info->dev, "Cannot detect accessory\n"); 65427a28d32SJaewon Kim goto err_cable_wq; 65527a28d32SJaewon Kim } 65627a28d32SJaewon Kim } 65727a28d32SJaewon Kim 65827a28d32SJaewon Kim chg_type = max77843_muic_get_cable_type(info, 65927a28d32SJaewon Kim MAX77843_CABLE_GROUP_CHG, &attached); 66027a28d32SJaewon Kim if (attached && chg_type != MAX77843_MUIC_CHG_NONE) { 66127a28d32SJaewon Kim ret = max77843_muic_chg_handler(info); 66227a28d32SJaewon Kim if (ret < 0) { 66327a28d32SJaewon Kim dev_err(info->dev, "Cannot detect charger accessory\n"); 66427a28d32SJaewon Kim goto err_cable_wq; 66527a28d32SJaewon Kim } 66627a28d32SJaewon Kim } 66727a28d32SJaewon Kim 66827a28d32SJaewon Kim err_cable_wq: 66927a28d32SJaewon Kim mutex_unlock(&info->mutex); 67027a28d32SJaewon Kim } 67127a28d32SJaewon Kim 67227a28d32SJaewon Kim static int max77843_muic_set_debounce_time(struct max77843_muic_info *info, 67327a28d32SJaewon Kim enum max77843_muic_adc_debounce_time time) 67427a28d32SJaewon Kim { 675bc1aadc1SKrzysztof Kozlowski struct max77693_dev *max77843 = info->max77843; 676d927c590SDan Carpenter int ret; 67727a28d32SJaewon Kim 67827a28d32SJaewon Kim switch (time) { 67927a28d32SJaewon Kim case MAX77843_DEBOUNCE_TIME_5MS: 68027a28d32SJaewon Kim case MAX77843_DEBOUNCE_TIME_10MS: 68127a28d32SJaewon Kim case MAX77843_DEBOUNCE_TIME_25MS: 68227a28d32SJaewon Kim case MAX77843_DEBOUNCE_TIME_38_62MS: 68327a28d32SJaewon Kim ret = regmap_update_bits(max77843->regmap_muic, 68427a28d32SJaewon Kim MAX77843_MUIC_REG_CONTROL4, 68527a28d32SJaewon Kim MAX77843_MUIC_CONTROL4_ADCDBSET_MASK, 686*309a3e00SKrzysztof Kozlowski time << MAX77843_MUIC_CONTROL4_ADCDBSET_SHIFT); 68727a28d32SJaewon Kim if (ret < 0) { 68827a28d32SJaewon Kim dev_err(info->dev, "Cannot write MUIC regmap\n"); 68927a28d32SJaewon Kim return ret; 69027a28d32SJaewon Kim } 69127a28d32SJaewon Kim break; 69227a28d32SJaewon Kim default: 69327a28d32SJaewon Kim dev_err(info->dev, "Invalid ADC debounce time\n"); 69427a28d32SJaewon Kim return -EINVAL; 69527a28d32SJaewon Kim } 69627a28d32SJaewon Kim 69727a28d32SJaewon Kim return 0; 69827a28d32SJaewon Kim } 69927a28d32SJaewon Kim 700bc1aadc1SKrzysztof Kozlowski static int max77843_init_muic_regmap(struct max77693_dev *max77843) 70127a28d32SJaewon Kim { 70227a28d32SJaewon Kim int ret; 70327a28d32SJaewon Kim 70427a28d32SJaewon Kim max77843->i2c_muic = i2c_new_dummy(max77843->i2c->adapter, 70527a28d32SJaewon Kim I2C_ADDR_MUIC); 70627a28d32SJaewon Kim if (!max77843->i2c_muic) { 70727a28d32SJaewon Kim dev_err(&max77843->i2c->dev, 70827a28d32SJaewon Kim "Cannot allocate I2C device for MUIC\n"); 709b9b518f5SDan Carpenter return -ENOMEM; 71027a28d32SJaewon Kim } 71127a28d32SJaewon Kim 71227a28d32SJaewon Kim i2c_set_clientdata(max77843->i2c_muic, max77843); 71327a28d32SJaewon Kim 71427a28d32SJaewon Kim max77843->regmap_muic = devm_regmap_init_i2c(max77843->i2c_muic, 71527a28d32SJaewon Kim &max77843_muic_regmap_config); 71627a28d32SJaewon Kim if (IS_ERR(max77843->regmap_muic)) { 71727a28d32SJaewon Kim ret = PTR_ERR(max77843->regmap_muic); 71827a28d32SJaewon Kim goto err_muic_i2c; 71927a28d32SJaewon Kim } 72027a28d32SJaewon Kim 72127a28d32SJaewon Kim ret = regmap_add_irq_chip(max77843->regmap_muic, max77843->irq, 72227a28d32SJaewon Kim IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED, 72327a28d32SJaewon Kim 0, &max77843_muic_irq_chip, &max77843->irq_data_muic); 72427a28d32SJaewon Kim if (ret < 0) { 72527a28d32SJaewon Kim dev_err(&max77843->i2c->dev, "Cannot add MUIC IRQ chip\n"); 72627a28d32SJaewon Kim goto err_muic_i2c; 72727a28d32SJaewon Kim } 72827a28d32SJaewon Kim 72927a28d32SJaewon Kim return 0; 73027a28d32SJaewon Kim 73127a28d32SJaewon Kim err_muic_i2c: 73227a28d32SJaewon Kim i2c_unregister_device(max77843->i2c_muic); 73327a28d32SJaewon Kim 73427a28d32SJaewon Kim return ret; 73527a28d32SJaewon Kim } 73627a28d32SJaewon Kim 73727a28d32SJaewon Kim static int max77843_muic_probe(struct platform_device *pdev) 73827a28d32SJaewon Kim { 739bc1aadc1SKrzysztof Kozlowski struct max77693_dev *max77843 = dev_get_drvdata(pdev->dev.parent); 74027a28d32SJaewon Kim struct max77843_muic_info *info; 74127a28d32SJaewon Kim unsigned int id; 74227a28d32SJaewon Kim int i, ret; 74327a28d32SJaewon Kim 74427a28d32SJaewon Kim info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); 74527a28d32SJaewon Kim if (!info) 74627a28d32SJaewon Kim return -ENOMEM; 74727a28d32SJaewon Kim 74827a28d32SJaewon Kim info->dev = &pdev->dev; 74927a28d32SJaewon Kim info->max77843 = max77843; 75027a28d32SJaewon Kim 75127a28d32SJaewon Kim platform_set_drvdata(pdev, info); 75227a28d32SJaewon Kim mutex_init(&info->mutex); 75327a28d32SJaewon Kim 75427a28d32SJaewon Kim /* Initialize i2c and regmap */ 75527a28d32SJaewon Kim ret = max77843_init_muic_regmap(max77843); 75627a28d32SJaewon Kim if (ret) { 75727a28d32SJaewon Kim dev_err(&pdev->dev, "Failed to init MUIC regmap\n"); 75827a28d32SJaewon Kim return ret; 75927a28d32SJaewon Kim } 76027a28d32SJaewon Kim 76127a28d32SJaewon Kim /* Turn off auto detection configuration */ 76227a28d32SJaewon Kim ret = regmap_update_bits(max77843->regmap_muic, 76327a28d32SJaewon Kim MAX77843_MUIC_REG_CONTROL4, 76427a28d32SJaewon Kim MAX77843_MUIC_CONTROL4_USBAUTO_MASK | 76527a28d32SJaewon Kim MAX77843_MUIC_CONTROL4_FCTAUTO_MASK, 76627a28d32SJaewon Kim CONTROL4_AUTO_DISABLE); 76727a28d32SJaewon Kim 76827a28d32SJaewon Kim /* Initialize extcon device */ 76927a28d32SJaewon Kim info->edev = devm_extcon_dev_allocate(&pdev->dev, 77027a28d32SJaewon Kim max77843_extcon_cable); 77127a28d32SJaewon Kim if (IS_ERR(info->edev)) { 77227a28d32SJaewon Kim dev_err(&pdev->dev, "Failed to allocate memory for extcon\n"); 77327a28d32SJaewon Kim ret = -ENODEV; 77427a28d32SJaewon Kim goto err_muic_irq; 77527a28d32SJaewon Kim } 77627a28d32SJaewon Kim 77727a28d32SJaewon Kim ret = devm_extcon_dev_register(&pdev->dev, info->edev); 77827a28d32SJaewon Kim if (ret) { 77927a28d32SJaewon Kim dev_err(&pdev->dev, "Failed to register extcon device\n"); 78027a28d32SJaewon Kim goto err_muic_irq; 78127a28d32SJaewon Kim } 78227a28d32SJaewon Kim 78327a28d32SJaewon Kim /* Set ADC debounce time */ 78427a28d32SJaewon Kim max77843_muic_set_debounce_time(info, MAX77843_DEBOUNCE_TIME_25MS); 78527a28d32SJaewon Kim 78627a28d32SJaewon Kim /* Set initial path for UART */ 787*309a3e00SKrzysztof Kozlowski max77843_muic_set_path(info, MAX77843_MUIC_CONTROL1_SW_UART, true); 78827a28d32SJaewon Kim 78927a28d32SJaewon Kim /* Check revision number of MUIC device */ 79027a28d32SJaewon Kim ret = regmap_read(max77843->regmap_muic, MAX77843_MUIC_REG_ID, &id); 79127a28d32SJaewon Kim if (ret < 0) { 79227a28d32SJaewon Kim dev_err(&pdev->dev, "Failed to read revision number\n"); 79327a28d32SJaewon Kim goto err_muic_irq; 79427a28d32SJaewon Kim } 79527a28d32SJaewon Kim dev_info(info->dev, "MUIC device ID : 0x%x\n", id); 79627a28d32SJaewon Kim 79727a28d32SJaewon Kim /* Support virtual irq domain for max77843 MUIC device */ 79827a28d32SJaewon Kim INIT_WORK(&info->irq_work, max77843_muic_irq_work); 79927a28d32SJaewon Kim 80027a28d32SJaewon Kim for (i = 0; i < ARRAY_SIZE(max77843_muic_irqs); i++) { 80127a28d32SJaewon Kim struct max77843_muic_irq *muic_irq = &max77843_muic_irqs[i]; 80227a28d32SJaewon Kim unsigned int virq = 0; 80327a28d32SJaewon Kim 80427a28d32SJaewon Kim virq = regmap_irq_get_virq(max77843->irq_data_muic, 80527a28d32SJaewon Kim muic_irq->irq); 80627a28d32SJaewon Kim if (virq <= 0) { 80727a28d32SJaewon Kim ret = -EINVAL; 80827a28d32SJaewon Kim goto err_muic_irq; 80927a28d32SJaewon Kim } 81027a28d32SJaewon Kim muic_irq->virq = virq; 81127a28d32SJaewon Kim 81227a28d32SJaewon Kim ret = devm_request_threaded_irq(&pdev->dev, virq, NULL, 81327a28d32SJaewon Kim max77843_muic_irq_handler, IRQF_NO_SUSPEND, 81427a28d32SJaewon Kim muic_irq->name, info); 81527a28d32SJaewon Kim if (ret) { 81627a28d32SJaewon Kim dev_err(&pdev->dev, 81727a28d32SJaewon Kim "Failed to request irq (IRQ: %d, error: %d)\n", 81827a28d32SJaewon Kim muic_irq->irq, ret); 81927a28d32SJaewon Kim goto err_muic_irq; 82027a28d32SJaewon Kim } 82127a28d32SJaewon Kim } 82227a28d32SJaewon Kim 82327a28d32SJaewon Kim /* Detect accessory after completing the initialization of platform */ 82427a28d32SJaewon Kim INIT_DELAYED_WORK(&info->wq_detcable, max77843_muic_detect_cable_wq); 82527a28d32SJaewon Kim queue_delayed_work(system_power_efficient_wq, 82627a28d32SJaewon Kim &info->wq_detcable, msecs_to_jiffies(DELAY_MS_DEFAULT)); 82727a28d32SJaewon Kim 82827a28d32SJaewon Kim return 0; 82927a28d32SJaewon Kim 83027a28d32SJaewon Kim err_muic_irq: 83127a28d32SJaewon Kim regmap_del_irq_chip(max77843->irq, max77843->irq_data_muic); 83227a28d32SJaewon Kim i2c_unregister_device(max77843->i2c_muic); 83327a28d32SJaewon Kim 83427a28d32SJaewon Kim return ret; 83527a28d32SJaewon Kim } 83627a28d32SJaewon Kim 83727a28d32SJaewon Kim static int max77843_muic_remove(struct platform_device *pdev) 83827a28d32SJaewon Kim { 83927a28d32SJaewon Kim struct max77843_muic_info *info = platform_get_drvdata(pdev); 840bc1aadc1SKrzysztof Kozlowski struct max77693_dev *max77843 = info->max77843; 84127a28d32SJaewon Kim 84227a28d32SJaewon Kim cancel_work_sync(&info->irq_work); 84327a28d32SJaewon Kim regmap_del_irq_chip(max77843->irq, max77843->irq_data_muic); 84427a28d32SJaewon Kim i2c_unregister_device(max77843->i2c_muic); 84527a28d32SJaewon Kim 84627a28d32SJaewon Kim return 0; 84727a28d32SJaewon Kim } 84827a28d32SJaewon Kim 84927a28d32SJaewon Kim static const struct platform_device_id max77843_muic_id[] = { 85027a28d32SJaewon Kim { "max77843-muic", }, 85127a28d32SJaewon Kim { /* sentinel */ }, 85227a28d32SJaewon Kim }; 85327a28d32SJaewon Kim MODULE_DEVICE_TABLE(platform, max77843_muic_id); 85427a28d32SJaewon Kim 85527a28d32SJaewon Kim static struct platform_driver max77843_muic_driver = { 85627a28d32SJaewon Kim .driver = { 85727a28d32SJaewon Kim .name = "max77843-muic", 85827a28d32SJaewon Kim }, 85927a28d32SJaewon Kim .probe = max77843_muic_probe, 86027a28d32SJaewon Kim .remove = max77843_muic_remove, 86127a28d32SJaewon Kim .id_table = max77843_muic_id, 86227a28d32SJaewon Kim }; 86327a28d32SJaewon Kim 86427a28d32SJaewon Kim static int __init max77843_muic_init(void) 86527a28d32SJaewon Kim { 86627a28d32SJaewon Kim return platform_driver_register(&max77843_muic_driver); 86727a28d32SJaewon Kim } 86827a28d32SJaewon Kim subsys_initcall(max77843_muic_init); 86927a28d32SJaewon Kim 87027a28d32SJaewon Kim MODULE_DESCRIPTION("Maxim MAX77843 Extcon driver"); 87127a28d32SJaewon Kim MODULE_AUTHOR("Jaewon Kim <jaewon02.kim@samsung.com>"); 87227a28d32SJaewon Kim MODULE_LICENSE("GPL"); 873