1914b881fSChanwoo Choi /* 2914b881fSChanwoo Choi * extcon-sm5502.c - Silicon Mitus SM5502 extcon drvier to support USB switches 3914b881fSChanwoo Choi * 4914b881fSChanwoo Choi * Copyright (c) 2014 Samsung Electronics Co., Ltd 5914b881fSChanwoo Choi * Author: Chanwoo Choi <cw00.choi@samsung.com> 6914b881fSChanwoo Choi * 7914b881fSChanwoo Choi * This program is free software; you can redistribute it and/or modify it 8914b881fSChanwoo Choi * under the terms of the GNU General Public License as published by the 9914b881fSChanwoo Choi * Free Software Foundation; either version 2 of the License, or (at your 10914b881fSChanwoo Choi * option) any later version. 11914b881fSChanwoo Choi * 12914b881fSChanwoo Choi * This program is distributed in the hope that it will be useful, 13914b881fSChanwoo Choi * but WITHOUT ANY WARRANTY; without even the implied warranty of 14914b881fSChanwoo Choi * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15914b881fSChanwoo Choi * GNU General Public License for more details. 16914b881fSChanwoo Choi */ 17914b881fSChanwoo Choi 18914b881fSChanwoo Choi #include <linux/err.h> 19914b881fSChanwoo Choi #include <linux/i2c.h> 20914b881fSChanwoo Choi #include <linux/input.h> 21914b881fSChanwoo Choi #include <linux/interrupt.h> 22914b881fSChanwoo Choi #include <linux/irqdomain.h> 23914b881fSChanwoo Choi #include <linux/kernel.h> 24914b881fSChanwoo Choi #include <linux/module.h> 25914b881fSChanwoo Choi #include <linux/platform_device.h> 26914b881fSChanwoo Choi #include <linux/regmap.h> 27914b881fSChanwoo Choi #include <linux/slab.h> 28914b881fSChanwoo Choi #include <linux/extcon.h> 29914b881fSChanwoo Choi #include <linux/extcon/sm5502.h> 30914b881fSChanwoo Choi 31e1954452SChanwoo Choi #define DELAY_MS_DEFAULT 17000 /* unit: millisecond */ 32e1954452SChanwoo Choi 33914b881fSChanwoo Choi struct muic_irq { 34914b881fSChanwoo Choi unsigned int irq; 35914b881fSChanwoo Choi const char *name; 36914b881fSChanwoo Choi unsigned int virq; 37914b881fSChanwoo Choi }; 38914b881fSChanwoo Choi 39914b881fSChanwoo Choi struct reg_data { 40914b881fSChanwoo Choi u8 reg; 41914b881fSChanwoo Choi unsigned int val; 42914b881fSChanwoo Choi bool invert; 43914b881fSChanwoo Choi }; 44914b881fSChanwoo Choi 45914b881fSChanwoo Choi struct sm5502_muic_info { 46914b881fSChanwoo Choi struct device *dev; 47914b881fSChanwoo Choi struct extcon_dev *edev; 48914b881fSChanwoo Choi 49914b881fSChanwoo Choi struct i2c_client *i2c; 50914b881fSChanwoo Choi struct regmap *regmap; 51914b881fSChanwoo Choi 52914b881fSChanwoo Choi struct regmap_irq_chip_data *irq_data; 53914b881fSChanwoo Choi struct muic_irq *muic_irqs; 54914b881fSChanwoo Choi unsigned int num_muic_irqs; 55914b881fSChanwoo Choi int irq; 56914b881fSChanwoo Choi bool irq_attach; 57914b881fSChanwoo Choi bool irq_detach; 58914b881fSChanwoo Choi struct work_struct irq_work; 59914b881fSChanwoo Choi 60914b881fSChanwoo Choi struct reg_data *reg_data; 61914b881fSChanwoo Choi unsigned int num_reg_data; 62914b881fSChanwoo Choi 63914b881fSChanwoo Choi struct mutex mutex; 64e1954452SChanwoo Choi 65e1954452SChanwoo Choi /* 66e1954452SChanwoo Choi * Use delayed workqueue to detect cable state and then 67e1954452SChanwoo Choi * notify cable state to notifiee/platform through uevent. 68e1954452SChanwoo Choi * After completing the booting of platform, the extcon provider 69e1954452SChanwoo Choi * driver should notify cable state to upper layer. 70e1954452SChanwoo Choi */ 71e1954452SChanwoo Choi struct delayed_work wq_detcable; 72914b881fSChanwoo Choi }; 73914b881fSChanwoo Choi 74914b881fSChanwoo Choi /* Default value of SM5502 register to bring up MUIC device. */ 75914b881fSChanwoo Choi static struct reg_data sm5502_reg_data[] = { 76914b881fSChanwoo Choi { 77914b881fSChanwoo Choi .reg = SM5502_REG_CONTROL, 78914b881fSChanwoo Choi .val = SM5502_REG_CONTROL_MASK_INT_MASK, 79914b881fSChanwoo Choi .invert = false, 80914b881fSChanwoo Choi }, { 81914b881fSChanwoo Choi .reg = SM5502_REG_INTMASK1, 82914b881fSChanwoo Choi .val = SM5502_REG_INTM1_KP_MASK 83914b881fSChanwoo Choi | SM5502_REG_INTM1_LKP_MASK 84914b881fSChanwoo Choi | SM5502_REG_INTM1_LKR_MASK, 85914b881fSChanwoo Choi .invert = true, 86914b881fSChanwoo Choi }, { 87914b881fSChanwoo Choi .reg = SM5502_REG_INTMASK2, 88914b881fSChanwoo Choi .val = SM5502_REG_INTM2_VBUS_DET_MASK 89914b881fSChanwoo Choi | SM5502_REG_INTM2_REV_ACCE_MASK 90914b881fSChanwoo Choi | SM5502_REG_INTM2_ADC_CHG_MASK 91914b881fSChanwoo Choi | SM5502_REG_INTM2_STUCK_KEY_MASK 92914b881fSChanwoo Choi | SM5502_REG_INTM2_STUCK_KEY_RCV_MASK 93914b881fSChanwoo Choi | SM5502_REG_INTM2_MHL_MASK, 94914b881fSChanwoo Choi .invert = true, 95914b881fSChanwoo Choi }, 96914b881fSChanwoo Choi { } 97914b881fSChanwoo Choi }; 98914b881fSChanwoo Choi 99914b881fSChanwoo Choi /* List of detectable cables */ 100914b881fSChanwoo Choi enum { 101914b881fSChanwoo Choi EXTCON_CABLE_USB = 0, 102914b881fSChanwoo Choi EXTCON_CABLE_USB_HOST, 103914b881fSChanwoo Choi EXTCON_CABLE_TA, 104914b881fSChanwoo Choi 105914b881fSChanwoo Choi EXTCON_CABLE_END, 106914b881fSChanwoo Choi }; 107914b881fSChanwoo Choi 108914b881fSChanwoo Choi static const char *sm5502_extcon_cable[] = { 109914b881fSChanwoo Choi [EXTCON_CABLE_USB] = "USB", 110914b881fSChanwoo Choi [EXTCON_CABLE_USB_HOST] = "USB-Host", 111914b881fSChanwoo Choi [EXTCON_CABLE_TA] = "TA", 112914b881fSChanwoo Choi NULL, 113914b881fSChanwoo Choi }; 114914b881fSChanwoo Choi 115914b881fSChanwoo Choi /* Define supported accessory type */ 116914b881fSChanwoo Choi enum sm5502_muic_acc_type { 117914b881fSChanwoo Choi SM5502_MUIC_ADC_GROUND = 0x0, 118914b881fSChanwoo Choi SM5502_MUIC_ADC_SEND_END_BUTTON, 119914b881fSChanwoo Choi SM5502_MUIC_ADC_REMOTE_S1_BUTTON, 120914b881fSChanwoo Choi SM5502_MUIC_ADC_REMOTE_S2_BUTTON, 121914b881fSChanwoo Choi SM5502_MUIC_ADC_REMOTE_S3_BUTTON, 122914b881fSChanwoo Choi SM5502_MUIC_ADC_REMOTE_S4_BUTTON, 123914b881fSChanwoo Choi SM5502_MUIC_ADC_REMOTE_S5_BUTTON, 124914b881fSChanwoo Choi SM5502_MUIC_ADC_REMOTE_S6_BUTTON, 125914b881fSChanwoo Choi SM5502_MUIC_ADC_REMOTE_S7_BUTTON, 126914b881fSChanwoo Choi SM5502_MUIC_ADC_REMOTE_S8_BUTTON, 127914b881fSChanwoo Choi SM5502_MUIC_ADC_REMOTE_S9_BUTTON, 128914b881fSChanwoo Choi SM5502_MUIC_ADC_REMOTE_S10_BUTTON, 129914b881fSChanwoo Choi SM5502_MUIC_ADC_REMOTE_S11_BUTTON, 130914b881fSChanwoo Choi SM5502_MUIC_ADC_REMOTE_S12_BUTTON, 131914b881fSChanwoo Choi SM5502_MUIC_ADC_RESERVED_ACC_1, 132914b881fSChanwoo Choi SM5502_MUIC_ADC_RESERVED_ACC_2, 133914b881fSChanwoo Choi SM5502_MUIC_ADC_RESERVED_ACC_3, 134914b881fSChanwoo Choi SM5502_MUIC_ADC_RESERVED_ACC_4, 135914b881fSChanwoo Choi SM5502_MUIC_ADC_RESERVED_ACC_5, 136914b881fSChanwoo Choi SM5502_MUIC_ADC_AUDIO_TYPE2, 137914b881fSChanwoo Choi SM5502_MUIC_ADC_PHONE_POWERED_DEV, 138914b881fSChanwoo Choi SM5502_MUIC_ADC_TTY_CONVERTER, 139914b881fSChanwoo Choi SM5502_MUIC_ADC_UART_CABLE, 140914b881fSChanwoo Choi SM5502_MUIC_ADC_TYPE1_CHARGER, 141914b881fSChanwoo Choi SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB, 142914b881fSChanwoo Choi SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB, 143914b881fSChanwoo Choi SM5502_MUIC_ADC_AUDIO_VIDEO_CABLE, 144914b881fSChanwoo Choi SM5502_MUIC_ADC_TYPE2_CHARGER, 145914b881fSChanwoo Choi SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART, 146914b881fSChanwoo Choi SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART, 147914b881fSChanwoo Choi SM5502_MUIC_ADC_AUDIO_TYPE1, 148914b881fSChanwoo Choi SM5502_MUIC_ADC_OPEN = 0x1f, 149914b881fSChanwoo Choi 150914b881fSChanwoo Choi /* The below accessories have same ADC value (0x1f or 0x1e). 151914b881fSChanwoo Choi So, Device type1 is used to separate specific accessory. */ 152914b881fSChanwoo Choi /* |---------|--ADC| */ 153914b881fSChanwoo Choi /* | [7:5]|[4:0]| */ 154914b881fSChanwoo Choi SM5502_MUIC_ADC_AUDIO_TYPE1_FULL_REMOTE = 0x3e, /* | 001|11110| */ 155914b881fSChanwoo Choi SM5502_MUIC_ADC_AUDIO_TYPE1_SEND_END = 0x5e, /* | 010|11110| */ 156914b881fSChanwoo Choi /* |Dev Type1|--ADC| */ 157914b881fSChanwoo Choi SM5502_MUIC_ADC_OPEN_USB = 0x5f, /* | 010|11111| */ 158914b881fSChanwoo Choi SM5502_MUIC_ADC_OPEN_TA = 0xdf, /* | 110|11111| */ 159914b881fSChanwoo Choi SM5502_MUIC_ADC_OPEN_USB_OTG = 0xff, /* | 111|11111| */ 160914b881fSChanwoo Choi }; 161914b881fSChanwoo Choi 162914b881fSChanwoo Choi /* List of supported interrupt for SM5502 */ 163914b881fSChanwoo Choi static struct muic_irq sm5502_muic_irqs[] = { 164914b881fSChanwoo Choi { SM5502_IRQ_INT1_ATTACH, "muic-attach" }, 165914b881fSChanwoo Choi { SM5502_IRQ_INT1_DETACH, "muic-detach" }, 166914b881fSChanwoo Choi { SM5502_IRQ_INT1_KP, "muic-kp" }, 167914b881fSChanwoo Choi { SM5502_IRQ_INT1_LKP, "muic-lkp" }, 168914b881fSChanwoo Choi { SM5502_IRQ_INT1_LKR, "muic-lkr" }, 169914b881fSChanwoo Choi { SM5502_IRQ_INT1_OVP_EVENT, "muic-ovp-event" }, 170914b881fSChanwoo Choi { SM5502_IRQ_INT1_OCP_EVENT, "muic-ocp-event" }, 171914b881fSChanwoo Choi { SM5502_IRQ_INT1_OVP_OCP_DIS, "muic-ovp-ocp-dis" }, 172914b881fSChanwoo Choi { SM5502_IRQ_INT2_VBUS_DET, "muic-vbus-det" }, 173914b881fSChanwoo Choi { SM5502_IRQ_INT2_REV_ACCE, "muic-rev-acce" }, 174914b881fSChanwoo Choi { SM5502_IRQ_INT2_ADC_CHG, "muic-adc-chg" }, 175914b881fSChanwoo Choi { SM5502_IRQ_INT2_STUCK_KEY, "muic-stuck-key" }, 176914b881fSChanwoo Choi { SM5502_IRQ_INT2_STUCK_KEY_RCV, "muic-stuck-key-rcv" }, 177914b881fSChanwoo Choi { SM5502_IRQ_INT2_MHL, "muic-mhl" }, 178914b881fSChanwoo Choi }; 179914b881fSChanwoo Choi 180914b881fSChanwoo Choi /* Define interrupt list of SM5502 to register regmap_irq */ 181914b881fSChanwoo Choi static const struct regmap_irq sm5502_irqs[] = { 182914b881fSChanwoo Choi /* INT1 interrupts */ 183914b881fSChanwoo Choi { .reg_offset = 0, .mask = SM5502_IRQ_INT1_ATTACH_MASK, }, 184914b881fSChanwoo Choi { .reg_offset = 0, .mask = SM5502_IRQ_INT1_DETACH_MASK, }, 185914b881fSChanwoo Choi { .reg_offset = 0, .mask = SM5502_IRQ_INT1_KP_MASK, }, 186914b881fSChanwoo Choi { .reg_offset = 0, .mask = SM5502_IRQ_INT1_LKP_MASK, }, 187914b881fSChanwoo Choi { .reg_offset = 0, .mask = SM5502_IRQ_INT1_LKR_MASK, }, 188914b881fSChanwoo Choi { .reg_offset = 0, .mask = SM5502_IRQ_INT1_OVP_EVENT_MASK, }, 189914b881fSChanwoo Choi { .reg_offset = 0, .mask = SM5502_IRQ_INT1_OCP_EVENT_MASK, }, 190914b881fSChanwoo Choi { .reg_offset = 0, .mask = SM5502_IRQ_INT1_OVP_OCP_DIS_MASK, }, 191914b881fSChanwoo Choi 192914b881fSChanwoo Choi /* INT2 interrupts */ 193914b881fSChanwoo Choi { .reg_offset = 1, .mask = SM5502_IRQ_INT2_VBUS_DET_MASK,}, 194914b881fSChanwoo Choi { .reg_offset = 1, .mask = SM5502_IRQ_INT2_REV_ACCE_MASK, }, 195914b881fSChanwoo Choi { .reg_offset = 1, .mask = SM5502_IRQ_INT2_ADC_CHG_MASK, }, 196914b881fSChanwoo Choi { .reg_offset = 1, .mask = SM5502_IRQ_INT2_STUCK_KEY_MASK, }, 197914b881fSChanwoo Choi { .reg_offset = 1, .mask = SM5502_IRQ_INT2_STUCK_KEY_RCV_MASK, }, 198914b881fSChanwoo Choi { .reg_offset = 1, .mask = SM5502_IRQ_INT2_MHL_MASK, }, 199914b881fSChanwoo Choi }; 200914b881fSChanwoo Choi 201914b881fSChanwoo Choi static const struct regmap_irq_chip sm5502_muic_irq_chip = { 202914b881fSChanwoo Choi .name = "sm5502", 203914b881fSChanwoo Choi .status_base = SM5502_REG_INT1, 204914b881fSChanwoo Choi .mask_base = SM5502_REG_INTMASK1, 205914b881fSChanwoo Choi .mask_invert = false, 206914b881fSChanwoo Choi .num_regs = 2, 207914b881fSChanwoo Choi .irqs = sm5502_irqs, 208914b881fSChanwoo Choi .num_irqs = ARRAY_SIZE(sm5502_irqs), 209914b881fSChanwoo Choi }; 210914b881fSChanwoo Choi 211914b881fSChanwoo Choi /* Define regmap configuration of SM5502 for I2C communication */ 212914b881fSChanwoo Choi static bool sm5502_muic_volatile_reg(struct device *dev, unsigned int reg) 213914b881fSChanwoo Choi { 214914b881fSChanwoo Choi switch (reg) { 215914b881fSChanwoo Choi case SM5502_REG_INTMASK1: 216914b881fSChanwoo Choi case SM5502_REG_INTMASK2: 217914b881fSChanwoo Choi return true; 218914b881fSChanwoo Choi default: 219914b881fSChanwoo Choi break; 220914b881fSChanwoo Choi } 221914b881fSChanwoo Choi return false; 222914b881fSChanwoo Choi } 223914b881fSChanwoo Choi 224914b881fSChanwoo Choi static const struct regmap_config sm5502_muic_regmap_config = { 225914b881fSChanwoo Choi .reg_bits = 8, 226914b881fSChanwoo Choi .val_bits = 8, 227914b881fSChanwoo Choi .volatile_reg = sm5502_muic_volatile_reg, 228914b881fSChanwoo Choi .max_register = SM5502_REG_END, 229914b881fSChanwoo Choi }; 230914b881fSChanwoo Choi 231914b881fSChanwoo Choi /* Return cable type of attached or detached accessories */ 232914b881fSChanwoo Choi static unsigned int sm5502_muic_get_cable_type(struct sm5502_muic_info *info) 233914b881fSChanwoo Choi { 234914b881fSChanwoo Choi unsigned int cable_type = -1, adc, dev_type1; 235914b881fSChanwoo Choi int ret; 236914b881fSChanwoo Choi 237914b881fSChanwoo Choi /* Read ADC value according to external cable or button */ 238914b881fSChanwoo Choi ret = regmap_read(info->regmap, SM5502_REG_ADC, &adc); 239914b881fSChanwoo Choi if (ret) { 240914b881fSChanwoo Choi dev_err(info->dev, "failed to read ADC register\n"); 241914b881fSChanwoo Choi return ret; 242914b881fSChanwoo Choi } 243914b881fSChanwoo Choi 244914b881fSChanwoo Choi /* 245914b881fSChanwoo Choi * If ADC is SM5502_MUIC_ADC_GROUND(0x0), external cable hasn't 246914b881fSChanwoo Choi * connected with to MUIC device. 247914b881fSChanwoo Choi */ 248914b881fSChanwoo Choi cable_type &= SM5502_REG_ADC_MASK; 249914b881fSChanwoo Choi if (cable_type == SM5502_MUIC_ADC_GROUND) 250914b881fSChanwoo Choi return SM5502_MUIC_ADC_GROUND; 251914b881fSChanwoo Choi 252914b881fSChanwoo Choi switch (cable_type) { 253914b881fSChanwoo Choi case SM5502_MUIC_ADC_GROUND: 254914b881fSChanwoo Choi case SM5502_MUIC_ADC_SEND_END_BUTTON: 255914b881fSChanwoo Choi case SM5502_MUIC_ADC_REMOTE_S1_BUTTON: 256914b881fSChanwoo Choi case SM5502_MUIC_ADC_REMOTE_S2_BUTTON: 257914b881fSChanwoo Choi case SM5502_MUIC_ADC_REMOTE_S3_BUTTON: 258914b881fSChanwoo Choi case SM5502_MUIC_ADC_REMOTE_S4_BUTTON: 259914b881fSChanwoo Choi case SM5502_MUIC_ADC_REMOTE_S5_BUTTON: 260914b881fSChanwoo Choi case SM5502_MUIC_ADC_REMOTE_S6_BUTTON: 261914b881fSChanwoo Choi case SM5502_MUIC_ADC_REMOTE_S7_BUTTON: 262914b881fSChanwoo Choi case SM5502_MUIC_ADC_REMOTE_S8_BUTTON: 263914b881fSChanwoo Choi case SM5502_MUIC_ADC_REMOTE_S9_BUTTON: 264914b881fSChanwoo Choi case SM5502_MUIC_ADC_REMOTE_S10_BUTTON: 265914b881fSChanwoo Choi case SM5502_MUIC_ADC_REMOTE_S11_BUTTON: 266914b881fSChanwoo Choi case SM5502_MUIC_ADC_REMOTE_S12_BUTTON: 267914b881fSChanwoo Choi case SM5502_MUIC_ADC_RESERVED_ACC_1: 268914b881fSChanwoo Choi case SM5502_MUIC_ADC_RESERVED_ACC_2: 269914b881fSChanwoo Choi case SM5502_MUIC_ADC_RESERVED_ACC_3: 270914b881fSChanwoo Choi case SM5502_MUIC_ADC_RESERVED_ACC_4: 271914b881fSChanwoo Choi case SM5502_MUIC_ADC_RESERVED_ACC_5: 272914b881fSChanwoo Choi case SM5502_MUIC_ADC_AUDIO_TYPE2: 273914b881fSChanwoo Choi case SM5502_MUIC_ADC_PHONE_POWERED_DEV: 274914b881fSChanwoo Choi case SM5502_MUIC_ADC_TTY_CONVERTER: 275914b881fSChanwoo Choi case SM5502_MUIC_ADC_UART_CABLE: 276914b881fSChanwoo Choi case SM5502_MUIC_ADC_TYPE1_CHARGER: 277914b881fSChanwoo Choi case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB: 278914b881fSChanwoo Choi case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB: 279914b881fSChanwoo Choi case SM5502_MUIC_ADC_AUDIO_VIDEO_CABLE: 280914b881fSChanwoo Choi case SM5502_MUIC_ADC_TYPE2_CHARGER: 281914b881fSChanwoo Choi case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART: 282914b881fSChanwoo Choi case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART: 283914b881fSChanwoo Choi break; 284914b881fSChanwoo Choi case SM5502_MUIC_ADC_AUDIO_TYPE1: 285914b881fSChanwoo Choi /* 286914b881fSChanwoo Choi * Check whether cable type is 287914b881fSChanwoo Choi * SM5502_MUIC_ADC_AUDIO_TYPE1_FULL_REMOTE 288914b881fSChanwoo Choi * or SM5502_MUIC_ADC_AUDIO_TYPE1_SEND_END 289914b881fSChanwoo Choi * by using Button event. 290914b881fSChanwoo Choi */ 291914b881fSChanwoo Choi break; 292914b881fSChanwoo Choi case SM5502_MUIC_ADC_OPEN: 293914b881fSChanwoo Choi ret = regmap_read(info->regmap, SM5502_REG_DEV_TYPE1, 294914b881fSChanwoo Choi &dev_type1); 295914b881fSChanwoo Choi if (ret) { 296914b881fSChanwoo Choi dev_err(info->dev, "failed to read DEV_TYPE1 reg\n"); 297914b881fSChanwoo Choi return ret; 298914b881fSChanwoo Choi } 299914b881fSChanwoo Choi 300914b881fSChanwoo Choi switch (dev_type1) { 301914b881fSChanwoo Choi case SM5502_REG_DEV_TYPE1_USB_SDP_MASK: 302914b881fSChanwoo Choi cable_type = SM5502_MUIC_ADC_OPEN_USB; 303914b881fSChanwoo Choi break; 304914b881fSChanwoo Choi case SM5502_REG_DEV_TYPE1_DEDICATED_CHG_MASK: 305914b881fSChanwoo Choi cable_type = SM5502_MUIC_ADC_OPEN_TA; 306914b881fSChanwoo Choi break; 307914b881fSChanwoo Choi case SM5502_REG_DEV_TYPE1_USB_OTG_MASK: 308914b881fSChanwoo Choi cable_type = SM5502_MUIC_ADC_OPEN_USB_OTG; 309914b881fSChanwoo Choi break; 310914b881fSChanwoo Choi default: 311914b881fSChanwoo Choi dev_dbg(info->dev, 312914b881fSChanwoo Choi "cannot identify the cable type: adc(0x%x) " 313914b881fSChanwoo Choi "dev_type1(0x%x)\n", adc, dev_type1); 314914b881fSChanwoo Choi return -EINVAL; 315914b881fSChanwoo Choi }; 316914b881fSChanwoo Choi break; 317914b881fSChanwoo Choi default: 318914b881fSChanwoo Choi dev_err(info->dev, 319914b881fSChanwoo Choi "failed to identify the cable type: adc(0x%x)\n", adc); 320914b881fSChanwoo Choi return -EINVAL; 321914b881fSChanwoo Choi }; 322914b881fSChanwoo Choi 323914b881fSChanwoo Choi return cable_type; 324914b881fSChanwoo Choi } 325914b881fSChanwoo Choi 326914b881fSChanwoo Choi static int sm5502_muic_cable_handler(struct sm5502_muic_info *info, 327914b881fSChanwoo Choi bool attached) 328914b881fSChanwoo Choi { 329914b881fSChanwoo Choi static unsigned int prev_cable_type = SM5502_MUIC_ADC_GROUND; 330914b881fSChanwoo Choi const char **cable_names = info->edev->supported_cable; 331914b881fSChanwoo Choi unsigned int cable_type = SM5502_MUIC_ADC_GROUND; 332914b881fSChanwoo Choi unsigned int idx = 0; 333914b881fSChanwoo Choi 334914b881fSChanwoo Choi if (!cable_names) 335914b881fSChanwoo Choi return 0; 336914b881fSChanwoo Choi 337914b881fSChanwoo Choi /* Get the type of attached or detached cable */ 338914b881fSChanwoo Choi if (attached) 339914b881fSChanwoo Choi cable_type = sm5502_muic_get_cable_type(info); 340914b881fSChanwoo Choi else if (!attached) 341914b881fSChanwoo Choi cable_type = prev_cable_type; 342914b881fSChanwoo Choi prev_cable_type = cable_type; 343914b881fSChanwoo Choi 344914b881fSChanwoo Choi switch (cable_type) { 345914b881fSChanwoo Choi case SM5502_MUIC_ADC_OPEN_USB: 346914b881fSChanwoo Choi idx = EXTCON_CABLE_USB; 347914b881fSChanwoo Choi break; 348914b881fSChanwoo Choi case SM5502_MUIC_ADC_OPEN_TA: 349914b881fSChanwoo Choi idx = EXTCON_CABLE_TA; 350914b881fSChanwoo Choi break; 351914b881fSChanwoo Choi case SM5502_MUIC_ADC_OPEN_USB_OTG: 352914b881fSChanwoo Choi idx = EXTCON_CABLE_USB_HOST; 353914b881fSChanwoo Choi break; 354e1954452SChanwoo Choi case SM5502_MUIC_ADC_GROUND: 355e1954452SChanwoo Choi break; 356914b881fSChanwoo Choi default: 357914b881fSChanwoo Choi dev_dbg(info->dev, 358914b881fSChanwoo Choi "cannot handle this cable_type (0x%x)\n", cable_type); 359914b881fSChanwoo Choi return 0; 360914b881fSChanwoo Choi }; 361914b881fSChanwoo Choi 362914b881fSChanwoo Choi extcon_set_cable_state(info->edev, cable_names[idx], attached); 363914b881fSChanwoo Choi 364914b881fSChanwoo Choi return 0; 365914b881fSChanwoo Choi } 366914b881fSChanwoo Choi 367914b881fSChanwoo Choi static void sm5502_muic_irq_work(struct work_struct *work) 368914b881fSChanwoo Choi { 369914b881fSChanwoo Choi struct sm5502_muic_info *info = container_of(work, 370914b881fSChanwoo Choi struct sm5502_muic_info, irq_work); 371914b881fSChanwoo Choi int ret = 0; 372914b881fSChanwoo Choi 373914b881fSChanwoo Choi if (!info->edev) 374914b881fSChanwoo Choi return; 375914b881fSChanwoo Choi 376914b881fSChanwoo Choi mutex_lock(&info->mutex); 377914b881fSChanwoo Choi 378914b881fSChanwoo Choi /* Detect attached or detached cables */ 379914b881fSChanwoo Choi if (info->irq_attach) { 380914b881fSChanwoo Choi ret = sm5502_muic_cable_handler(info, true); 381914b881fSChanwoo Choi info->irq_attach = false; 382914b881fSChanwoo Choi } 383914b881fSChanwoo Choi if (info->irq_detach) { 384914b881fSChanwoo Choi ret = sm5502_muic_cable_handler(info, false); 385914b881fSChanwoo Choi info->irq_detach = false; 386914b881fSChanwoo Choi } 387914b881fSChanwoo Choi 388914b881fSChanwoo Choi if (ret < 0) 389914b881fSChanwoo Choi dev_err(info->dev, "failed to handle MUIC interrupt\n"); 390914b881fSChanwoo Choi 391914b881fSChanwoo Choi mutex_unlock(&info->mutex); 392914b881fSChanwoo Choi 393914b881fSChanwoo Choi return; 394914b881fSChanwoo Choi } 395914b881fSChanwoo Choi 396914b881fSChanwoo Choi /* 397914b881fSChanwoo Choi * Sets irq_attach or irq_detach in sm5502_muic_info and returns 0. 398914b881fSChanwoo Choi * Returns -ESRCH if irq_type does not match registered IRQ for this dev type. 399914b881fSChanwoo Choi */ 400914b881fSChanwoo Choi static int sm5502_parse_irq(struct sm5502_muic_info *info, int irq_type) 401914b881fSChanwoo Choi { 402914b881fSChanwoo Choi switch (irq_type) { 403914b881fSChanwoo Choi case SM5502_IRQ_INT1_ATTACH: 404914b881fSChanwoo Choi info->irq_attach = true; 405914b881fSChanwoo Choi break; 406914b881fSChanwoo Choi case SM5502_IRQ_INT1_DETACH: 407914b881fSChanwoo Choi info->irq_detach = true; 408914b881fSChanwoo Choi break; 409914b881fSChanwoo Choi case SM5502_IRQ_INT1_KP: 410914b881fSChanwoo Choi case SM5502_IRQ_INT1_LKP: 411914b881fSChanwoo Choi case SM5502_IRQ_INT1_LKR: 412914b881fSChanwoo Choi case SM5502_IRQ_INT1_OVP_EVENT: 413914b881fSChanwoo Choi case SM5502_IRQ_INT1_OCP_EVENT: 414914b881fSChanwoo Choi case SM5502_IRQ_INT1_OVP_OCP_DIS: 415914b881fSChanwoo Choi case SM5502_IRQ_INT2_VBUS_DET: 416914b881fSChanwoo Choi case SM5502_IRQ_INT2_REV_ACCE: 417914b881fSChanwoo Choi case SM5502_IRQ_INT2_ADC_CHG: 418914b881fSChanwoo Choi case SM5502_IRQ_INT2_STUCK_KEY: 419914b881fSChanwoo Choi case SM5502_IRQ_INT2_STUCK_KEY_RCV: 420914b881fSChanwoo Choi case SM5502_IRQ_INT2_MHL: 421914b881fSChanwoo Choi default: 422914b881fSChanwoo Choi break; 423914b881fSChanwoo Choi } 424914b881fSChanwoo Choi 425914b881fSChanwoo Choi return 0; 426914b881fSChanwoo Choi } 427914b881fSChanwoo Choi 428914b881fSChanwoo Choi static irqreturn_t sm5502_muic_irq_handler(int irq, void *data) 429914b881fSChanwoo Choi { 430914b881fSChanwoo Choi struct sm5502_muic_info *info = data; 431914b881fSChanwoo Choi int i, irq_type = -1, ret; 432914b881fSChanwoo Choi 433914b881fSChanwoo Choi for (i = 0; i < info->num_muic_irqs; i++) 434914b881fSChanwoo Choi if (irq == info->muic_irqs[i].virq) 435914b881fSChanwoo Choi irq_type = info->muic_irqs[i].irq; 436914b881fSChanwoo Choi 437914b881fSChanwoo Choi ret = sm5502_parse_irq(info, irq_type); 438914b881fSChanwoo Choi if (ret < 0) { 439914b881fSChanwoo Choi dev_warn(info->dev, "cannot handle is interrupt:%d\n", 440914b881fSChanwoo Choi irq_type); 441914b881fSChanwoo Choi return IRQ_HANDLED; 442914b881fSChanwoo Choi } 443914b881fSChanwoo Choi schedule_work(&info->irq_work); 444914b881fSChanwoo Choi 445914b881fSChanwoo Choi return IRQ_HANDLED; 446914b881fSChanwoo Choi } 447914b881fSChanwoo Choi 448e1954452SChanwoo Choi static void sm5502_muic_detect_cable_wq(struct work_struct *work) 449e1954452SChanwoo Choi { 450e1954452SChanwoo Choi struct sm5502_muic_info *info = container_of(to_delayed_work(work), 451e1954452SChanwoo Choi struct sm5502_muic_info, wq_detcable); 452e1954452SChanwoo Choi int ret; 453e1954452SChanwoo Choi 454e1954452SChanwoo Choi /* Notify the state of connector cable or not */ 455e1954452SChanwoo Choi ret = sm5502_muic_cable_handler(info, true); 456e1954452SChanwoo Choi if (ret < 0) 457e1954452SChanwoo Choi dev_warn(info->dev, "failed to detect cable state\n"); 458e1954452SChanwoo Choi } 459e1954452SChanwoo Choi 460914b881fSChanwoo Choi static void sm5502_init_dev_type(struct sm5502_muic_info *info) 461914b881fSChanwoo Choi { 462914b881fSChanwoo Choi unsigned int reg_data, vendor_id, version_id; 463914b881fSChanwoo Choi int i, ret; 464914b881fSChanwoo Choi 465914b881fSChanwoo Choi /* To test I2C, Print version_id and vendor_id of SM5502 */ 466914b881fSChanwoo Choi ret = regmap_read(info->regmap, SM5502_REG_DEVICE_ID, ®_data); 467914b881fSChanwoo Choi if (ret) { 468914b881fSChanwoo Choi dev_err(info->dev, 469914b881fSChanwoo Choi "failed to read DEVICE_ID register: %d\n", ret); 470914b881fSChanwoo Choi return; 471914b881fSChanwoo Choi } 472914b881fSChanwoo Choi 473914b881fSChanwoo Choi vendor_id = ((reg_data & SM5502_REG_DEVICE_ID_VENDOR_MASK) >> 474914b881fSChanwoo Choi SM5502_REG_DEVICE_ID_VENDOR_SHIFT); 475914b881fSChanwoo Choi version_id = ((reg_data & SM5502_REG_DEVICE_ID_VERSION_MASK) >> 476914b881fSChanwoo Choi SM5502_REG_DEVICE_ID_VERSION_SHIFT); 477914b881fSChanwoo Choi 478914b881fSChanwoo Choi dev_info(info->dev, "Device type: version: 0x%x, vendor: 0x%x\n", 479914b881fSChanwoo Choi version_id, vendor_id); 480914b881fSChanwoo Choi 481914b881fSChanwoo Choi /* Initiazle the register of SM5502 device to bring-up */ 482914b881fSChanwoo Choi for (i = 0; i < info->num_reg_data; i++) { 483914b881fSChanwoo Choi unsigned int val = 0; 484914b881fSChanwoo Choi 485914b881fSChanwoo Choi if (!info->reg_data[i].invert) 486914b881fSChanwoo Choi val |= ~info->reg_data[i].val; 487914b881fSChanwoo Choi else 488914b881fSChanwoo Choi val = info->reg_data[i].val; 489914b881fSChanwoo Choi regmap_write(info->regmap, info->reg_data[i].reg, val); 490914b881fSChanwoo Choi } 491914b881fSChanwoo Choi } 492914b881fSChanwoo Choi 493914b881fSChanwoo Choi static int sm5022_muic_i2c_probe(struct i2c_client *i2c, 494914b881fSChanwoo Choi const struct i2c_device_id *id) 495914b881fSChanwoo Choi { 496914b881fSChanwoo Choi struct device_node *np = i2c->dev.of_node; 497914b881fSChanwoo Choi struct sm5502_muic_info *info; 498914b881fSChanwoo Choi int i, ret, irq_flags; 499914b881fSChanwoo Choi 500914b881fSChanwoo Choi if (!np) 501914b881fSChanwoo Choi return -EINVAL; 502914b881fSChanwoo Choi 503914b881fSChanwoo Choi info = devm_kzalloc(&i2c->dev, sizeof(*info), GFP_KERNEL); 504914b881fSChanwoo Choi if (!info) 505914b881fSChanwoo Choi return -ENOMEM; 506914b881fSChanwoo Choi i2c_set_clientdata(i2c, info); 507914b881fSChanwoo Choi 508914b881fSChanwoo Choi info->dev = &i2c->dev; 509914b881fSChanwoo Choi info->i2c = i2c; 510914b881fSChanwoo Choi info->irq = i2c->irq; 511914b881fSChanwoo Choi info->muic_irqs = sm5502_muic_irqs; 512914b881fSChanwoo Choi info->num_muic_irqs = ARRAY_SIZE(sm5502_muic_irqs); 513914b881fSChanwoo Choi info->reg_data = sm5502_reg_data; 514914b881fSChanwoo Choi info->num_reg_data = ARRAY_SIZE(sm5502_reg_data); 515914b881fSChanwoo Choi 516914b881fSChanwoo Choi mutex_init(&info->mutex); 517914b881fSChanwoo Choi 518914b881fSChanwoo Choi INIT_WORK(&info->irq_work, sm5502_muic_irq_work); 519914b881fSChanwoo Choi 520914b881fSChanwoo Choi info->regmap = devm_regmap_init_i2c(i2c, &sm5502_muic_regmap_config); 521914b881fSChanwoo Choi if (IS_ERR(info->regmap)) { 522914b881fSChanwoo Choi ret = PTR_ERR(info->regmap); 523914b881fSChanwoo Choi dev_err(info->dev, "failed to allocate register map: %d\n", 524914b881fSChanwoo Choi ret); 525914b881fSChanwoo Choi return ret; 526914b881fSChanwoo Choi } 527914b881fSChanwoo Choi 528914b881fSChanwoo Choi /* Support irq domain for SM5502 MUIC device */ 529914b881fSChanwoo Choi irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_SHARED; 530914b881fSChanwoo Choi ret = regmap_add_irq_chip(info->regmap, info->irq, irq_flags, 0, 531914b881fSChanwoo Choi &sm5502_muic_irq_chip, &info->irq_data); 532914b881fSChanwoo Choi if (ret != 0) { 533914b881fSChanwoo Choi dev_err(info->dev, "failed to request IRQ %d: %d\n", 534914b881fSChanwoo Choi info->irq, ret); 535914b881fSChanwoo Choi return ret; 536914b881fSChanwoo Choi } 537914b881fSChanwoo Choi 538914b881fSChanwoo Choi for (i = 0; i < info->num_muic_irqs; i++) { 539914b881fSChanwoo Choi struct muic_irq *muic_irq = &info->muic_irqs[i]; 540914b881fSChanwoo Choi unsigned int virq = 0; 541914b881fSChanwoo Choi 542914b881fSChanwoo Choi virq = regmap_irq_get_virq(info->irq_data, muic_irq->irq); 543914b881fSChanwoo Choi if (virq <= 0) 544914b881fSChanwoo Choi return -EINVAL; 545914b881fSChanwoo Choi muic_irq->virq = virq; 546914b881fSChanwoo Choi 547914b881fSChanwoo Choi ret = devm_request_threaded_irq(info->dev, virq, NULL, 548914b881fSChanwoo Choi sm5502_muic_irq_handler, 549914b881fSChanwoo Choi IRQF_NO_SUSPEND, 550914b881fSChanwoo Choi muic_irq->name, info); 551914b881fSChanwoo Choi if (ret) { 552914b881fSChanwoo Choi dev_err(info->dev, "failed: irq request (IRQ: %d," 553914b881fSChanwoo Choi " error :%d)\n", muic_irq->irq, ret); 554914b881fSChanwoo Choi return ret; 555914b881fSChanwoo Choi } 556914b881fSChanwoo Choi } 557914b881fSChanwoo Choi 558914b881fSChanwoo Choi /* Allocate extcon device */ 559914b881fSChanwoo Choi info->edev = devm_extcon_dev_allocate(info->dev, sm5502_extcon_cable); 560914b881fSChanwoo Choi if (IS_ERR(info->edev)) { 561914b881fSChanwoo Choi dev_err(info->dev, "failed to allocate memory for extcon\n"); 562914b881fSChanwoo Choi return -ENOMEM; 563914b881fSChanwoo Choi } 564914b881fSChanwoo Choi info->edev->name = np->name; 565914b881fSChanwoo Choi 566914b881fSChanwoo Choi /* Register extcon device */ 567914b881fSChanwoo Choi ret = devm_extcon_dev_register(info->dev, info->edev); 568914b881fSChanwoo Choi if (ret) { 569914b881fSChanwoo Choi dev_err(info->dev, "failed to register extcon device\n"); 570914b881fSChanwoo Choi return ret; 571914b881fSChanwoo Choi } 572914b881fSChanwoo Choi 573e1954452SChanwoo Choi /* 574e1954452SChanwoo Choi * Detect accessory after completing the initialization of platform 575e1954452SChanwoo Choi * 576e1954452SChanwoo Choi * - Use delayed workqueue to detect cable state and then 577e1954452SChanwoo Choi * notify cable state to notifiee/platform through uevent. 578e1954452SChanwoo Choi * After completing the booting of platform, the extcon provider 579e1954452SChanwoo Choi * driver should notify cable state to upper layer. 580e1954452SChanwoo Choi */ 581e1954452SChanwoo Choi INIT_DELAYED_WORK(&info->wq_detcable, sm5502_muic_detect_cable_wq); 582e1954452SChanwoo Choi queue_delayed_work(system_power_efficient_wq, &info->wq_detcable, 583e1954452SChanwoo Choi msecs_to_jiffies(DELAY_MS_DEFAULT)); 584e1954452SChanwoo Choi 585914b881fSChanwoo Choi /* Initialize SM5502 device and print vendor id and version id */ 586914b881fSChanwoo Choi sm5502_init_dev_type(info); 587914b881fSChanwoo Choi 588914b881fSChanwoo Choi return 0; 589914b881fSChanwoo Choi } 590914b881fSChanwoo Choi 591914b881fSChanwoo Choi static int sm5502_muic_i2c_remove(struct i2c_client *i2c) 592914b881fSChanwoo Choi { 593914b881fSChanwoo Choi struct sm5502_muic_info *info = i2c_get_clientdata(i2c); 594914b881fSChanwoo Choi 595914b881fSChanwoo Choi regmap_del_irq_chip(info->irq, info->irq_data); 596914b881fSChanwoo Choi 597914b881fSChanwoo Choi return 0; 598914b881fSChanwoo Choi } 599914b881fSChanwoo Choi 600914b881fSChanwoo Choi static struct of_device_id sm5502_dt_match[] = { 601914b881fSChanwoo Choi { .compatible = "siliconmitus,sm5502-muic" }, 602914b881fSChanwoo Choi { }, 603914b881fSChanwoo Choi }; 604914b881fSChanwoo Choi 605914b881fSChanwoo Choi #ifdef CONFIG_PM_SLEEP 606914b881fSChanwoo Choi static int sm5502_muic_suspend(struct device *dev) 607914b881fSChanwoo Choi { 608914b881fSChanwoo Choi struct i2c_client *i2c = container_of(dev, struct i2c_client, dev); 609914b881fSChanwoo Choi struct sm5502_muic_info *info = i2c_get_clientdata(i2c); 610914b881fSChanwoo Choi 611914b881fSChanwoo Choi enable_irq_wake(info->irq); 612914b881fSChanwoo Choi 613914b881fSChanwoo Choi return 0; 614914b881fSChanwoo Choi } 615914b881fSChanwoo Choi 616914b881fSChanwoo Choi static int sm5502_muic_resume(struct device *dev) 617914b881fSChanwoo Choi { 618914b881fSChanwoo Choi struct i2c_client *i2c = container_of(dev, struct i2c_client, dev); 619914b881fSChanwoo Choi struct sm5502_muic_info *info = i2c_get_clientdata(i2c); 620914b881fSChanwoo Choi 621914b881fSChanwoo Choi disable_irq_wake(info->irq); 622914b881fSChanwoo Choi 623914b881fSChanwoo Choi return 0; 624914b881fSChanwoo Choi } 625914b881fSChanwoo Choi #endif 626914b881fSChanwoo Choi 627914b881fSChanwoo Choi static SIMPLE_DEV_PM_OPS(sm5502_muic_pm_ops, 628914b881fSChanwoo Choi sm5502_muic_suspend, sm5502_muic_resume); 629914b881fSChanwoo Choi 630914b881fSChanwoo Choi static const struct i2c_device_id sm5502_i2c_id[] = { 631914b881fSChanwoo Choi { "sm5502", TYPE_SM5502 }, 632914b881fSChanwoo Choi { } 633914b881fSChanwoo Choi }; 634914b881fSChanwoo Choi MODULE_DEVICE_TABLE(i2c, sm5502_i2c_id); 635914b881fSChanwoo Choi 636914b881fSChanwoo Choi static struct i2c_driver sm5502_muic_i2c_driver = { 637914b881fSChanwoo Choi .driver = { 638914b881fSChanwoo Choi .name = "sm5502", 639914b881fSChanwoo Choi .owner = THIS_MODULE, 640914b881fSChanwoo Choi .pm = &sm5502_muic_pm_ops, 641914b881fSChanwoo Choi .of_match_table = sm5502_dt_match, 642914b881fSChanwoo Choi }, 643914b881fSChanwoo Choi .probe = sm5022_muic_i2c_probe, 644914b881fSChanwoo Choi .remove = sm5502_muic_i2c_remove, 645914b881fSChanwoo Choi .id_table = sm5502_i2c_id, 646914b881fSChanwoo Choi }; 647914b881fSChanwoo Choi 648914b881fSChanwoo Choi static int __init sm5502_muic_i2c_init(void) 649914b881fSChanwoo Choi { 650914b881fSChanwoo Choi return i2c_add_driver(&sm5502_muic_i2c_driver); 651914b881fSChanwoo Choi } 652914b881fSChanwoo Choi subsys_initcall(sm5502_muic_i2c_init); 653914b881fSChanwoo Choi 654914b881fSChanwoo Choi MODULE_DESCRIPTION("Silicon Mitus SM5502 Extcon driver"); 655914b881fSChanwoo Choi MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>"); 656914b881fSChanwoo Choi MODULE_LICENSE("GPL"); 657