Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 //
0003 // extcon-max77693.c - MAX77693 extcon driver to support MAX77693 MUIC
0004 //
0005 // Copyright (C) 2012 Samsung Electrnoics
0006 // Chanwoo Choi <cw00.choi@samsung.com>
0007 
0008 #include <linux/devm-helpers.h>
0009 #include <linux/kernel.h>
0010 #include <linux/module.h>
0011 #include <linux/i2c.h>
0012 #include <linux/slab.h>
0013 #include <linux/input.h>
0014 #include <linux/interrupt.h>
0015 #include <linux/err.h>
0016 #include <linux/platform_device.h>
0017 #include <linux/mfd/max77693.h>
0018 #include <linux/mfd/max77693-common.h>
0019 #include <linux/mfd/max77693-private.h>
0020 #include <linux/extcon-provider.h>
0021 #include <linux/regmap.h>
0022 #include <linux/irqdomain.h>
0023 
0024 #define DEV_NAME            "max77693-muic"
0025 #define DELAY_MS_DEFAULT        20000       /* unit: millisecond */
0026 
0027 /*
0028  * Default value of MAX77693 register to bring up MUIC device.
0029  * If user don't set some initial value for MUIC device through platform data,
0030  * extcon-max77693 driver use 'default_init_data' to bring up base operation
0031  * of MAX77693 MUIC device.
0032  */
0033 static struct max77693_reg_data default_init_data[] = {
0034     {
0035         /* STATUS2 - [3]ChgDetRun */
0036         .addr = MAX77693_MUIC_REG_STATUS2,
0037         .data = MAX77693_STATUS2_CHGDETRUN_MASK,
0038     }, {
0039         /* INTMASK1 - Unmask [3]ADC1KM,[0]ADCM */
0040         .addr = MAX77693_MUIC_REG_INTMASK1,
0041         .data = INTMASK1_ADC1K_MASK
0042             | INTMASK1_ADC_MASK,
0043     }, {
0044         /* INTMASK2 - Unmask [0]ChgTypM */
0045         .addr = MAX77693_MUIC_REG_INTMASK2,
0046         .data = INTMASK2_CHGTYP_MASK,
0047     }, {
0048         /* INTMASK3 - Mask all of interrupts */
0049         .addr = MAX77693_MUIC_REG_INTMASK3,
0050         .data = 0x0,
0051     }, {
0052         /* CDETCTRL2 */
0053         .addr = MAX77693_MUIC_REG_CDETCTRL2,
0054         .data = CDETCTRL2_VIDRMEN_MASK
0055             | CDETCTRL2_DXOVPEN_MASK,
0056     },
0057 };
0058 
0059 enum max77693_muic_adc_debounce_time {
0060     ADC_DEBOUNCE_TIME_5MS = 0,
0061     ADC_DEBOUNCE_TIME_10MS,
0062     ADC_DEBOUNCE_TIME_25MS,
0063     ADC_DEBOUNCE_TIME_38_62MS,
0064 };
0065 
0066 struct max77693_muic_info {
0067     struct device *dev;
0068     struct max77693_dev *max77693;
0069     struct extcon_dev *edev;
0070     int prev_cable_type;
0071     int prev_cable_type_gnd;
0072     int prev_chg_type;
0073     int prev_button_type;
0074     u8 status[2];
0075 
0076     int irq;
0077     struct work_struct irq_work;
0078     struct mutex mutex;
0079 
0080     /*
0081      * Use delayed workqueue to detect cable state and then
0082      * notify cable state to notifiee/platform through uevent.
0083      * After completing the booting of platform, the extcon provider
0084      * driver should notify cable state to upper layer.
0085      */
0086     struct delayed_work wq_detcable;
0087 
0088     /* Button of dock device */
0089     struct input_dev *dock;
0090 
0091     /*
0092      * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
0093      * h/w path of COMP2/COMN1 on CONTROL1 register.
0094      */
0095     int path_usb;
0096     int path_uart;
0097 };
0098 
0099 enum max77693_muic_cable_group {
0100     MAX77693_CABLE_GROUP_ADC = 0,
0101     MAX77693_CABLE_GROUP_ADC_GND,
0102     MAX77693_CABLE_GROUP_CHG,
0103     MAX77693_CABLE_GROUP_VBVOLT,
0104 };
0105 
0106 enum max77693_muic_charger_type {
0107     MAX77693_CHARGER_TYPE_NONE = 0,
0108     MAX77693_CHARGER_TYPE_USB,
0109     MAX77693_CHARGER_TYPE_DOWNSTREAM_PORT,
0110     MAX77693_CHARGER_TYPE_DEDICATED_CHG,
0111     MAX77693_CHARGER_TYPE_APPLE_500MA,
0112     MAX77693_CHARGER_TYPE_APPLE_1A_2A,
0113     MAX77693_CHARGER_TYPE_DEAD_BATTERY = 7,
0114 };
0115 
0116 /**
0117  * struct max77693_muic_irq
0118  * @irq: the index of irq list of MUIC device.
0119  * @name: the name of irq.
0120  * @virq: the virtual irq to use irq domain
0121  */
0122 struct max77693_muic_irq {
0123     unsigned int irq;
0124     const char *name;
0125     unsigned int virq;
0126 };
0127 
0128 static struct max77693_muic_irq muic_irqs[] = {
0129     { MAX77693_MUIC_IRQ_INT1_ADC,       "muic-ADC" },
0130     { MAX77693_MUIC_IRQ_INT1_ADC_LOW,   "muic-ADCLOW" },
0131     { MAX77693_MUIC_IRQ_INT1_ADC_ERR,   "muic-ADCError" },
0132     { MAX77693_MUIC_IRQ_INT1_ADC1K,     "muic-ADC1K" },
0133     { MAX77693_MUIC_IRQ_INT2_CHGTYP,    "muic-CHGTYP" },
0134     { MAX77693_MUIC_IRQ_INT2_CHGDETREUN,    "muic-CHGDETREUN" },
0135     { MAX77693_MUIC_IRQ_INT2_DCDTMR,    "muic-DCDTMR" },
0136     { MAX77693_MUIC_IRQ_INT2_DXOVP,     "muic-DXOVP" },
0137     { MAX77693_MUIC_IRQ_INT2_VBVOLT,    "muic-VBVOLT" },
0138     { MAX77693_MUIC_IRQ_INT2_VIDRM,     "muic-VIDRM" },
0139     { MAX77693_MUIC_IRQ_INT3_EOC,       "muic-EOC" },
0140     { MAX77693_MUIC_IRQ_INT3_CGMBC,     "muic-CGMBC" },
0141     { MAX77693_MUIC_IRQ_INT3_OVP,       "muic-OVP" },
0142     { MAX77693_MUIC_IRQ_INT3_MBCCHG_ERR,    "muic-MBCCHG_ERR" },
0143     { MAX77693_MUIC_IRQ_INT3_CHG_ENABLED,   "muic-CHG_ENABLED" },
0144     { MAX77693_MUIC_IRQ_INT3_BAT_DET,   "muic-BAT_DET" },
0145 };
0146 
0147 /* Define supported accessory type */
0148 enum max77693_muic_acc_type {
0149     MAX77693_MUIC_ADC_GROUND = 0x0,
0150     MAX77693_MUIC_ADC_SEND_END_BUTTON,
0151     MAX77693_MUIC_ADC_REMOTE_S1_BUTTON,
0152     MAX77693_MUIC_ADC_REMOTE_S2_BUTTON,
0153     MAX77693_MUIC_ADC_REMOTE_S3_BUTTON,
0154     MAX77693_MUIC_ADC_REMOTE_S4_BUTTON,
0155     MAX77693_MUIC_ADC_REMOTE_S5_BUTTON,
0156     MAX77693_MUIC_ADC_REMOTE_S6_BUTTON,
0157     MAX77693_MUIC_ADC_REMOTE_S7_BUTTON,
0158     MAX77693_MUIC_ADC_REMOTE_S8_BUTTON,
0159     MAX77693_MUIC_ADC_REMOTE_S9_BUTTON,
0160     MAX77693_MUIC_ADC_REMOTE_S10_BUTTON,
0161     MAX77693_MUIC_ADC_REMOTE_S11_BUTTON,
0162     MAX77693_MUIC_ADC_REMOTE_S12_BUTTON,
0163     MAX77693_MUIC_ADC_RESERVED_ACC_1,
0164     MAX77693_MUIC_ADC_RESERVED_ACC_2,
0165     MAX77693_MUIC_ADC_RESERVED_ACC_3,
0166     MAX77693_MUIC_ADC_RESERVED_ACC_4,
0167     MAX77693_MUIC_ADC_RESERVED_ACC_5,
0168     MAX77693_MUIC_ADC_CEA936_AUDIO,
0169     MAX77693_MUIC_ADC_PHONE_POWERED_DEV,
0170     MAX77693_MUIC_ADC_TTY_CONVERTER,
0171     MAX77693_MUIC_ADC_UART_CABLE,
0172     MAX77693_MUIC_ADC_CEA936A_TYPE1_CHG,
0173     MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF,
0174     MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON,
0175     MAX77693_MUIC_ADC_AV_CABLE_NOLOAD,
0176     MAX77693_MUIC_ADC_CEA936A_TYPE2_CHG,
0177     MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF,
0178     MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON,
0179     MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE,
0180     MAX77693_MUIC_ADC_OPEN,
0181 
0182     /*
0183      * The below accessories have same ADC value so ADCLow and
0184      * ADC1K bit is used to separate specific accessory.
0185      */
0186                         /* ADC|VBVolot|ADCLow|ADC1K| */
0187     MAX77693_MUIC_GND_USB_HOST = 0x100, /* 0x0|      0|     0|    0| */
0188     MAX77693_MUIC_GND_USB_HOST_VB = 0x104,  /* 0x0|      1|     0|    0| */
0189     MAX77693_MUIC_GND_AV_CABLE_LOAD = 0x102,/* 0x0|      0|     1|    0| */
0190     MAX77693_MUIC_GND_MHL = 0x103,      /* 0x0|      0|     1|    1| */
0191     MAX77693_MUIC_GND_MHL_VB = 0x107,   /* 0x0|      1|     1|    1| */
0192 };
0193 
0194 /*
0195  * MAX77693 MUIC device support below list of accessories(external connector)
0196  */
0197 static const unsigned int max77693_extcon_cable[] = {
0198     EXTCON_USB,
0199     EXTCON_USB_HOST,
0200     EXTCON_CHG_USB_SDP,
0201     EXTCON_CHG_USB_DCP,
0202     EXTCON_CHG_USB_FAST,
0203     EXTCON_CHG_USB_SLOW,
0204     EXTCON_CHG_USB_CDP,
0205     EXTCON_DISP_MHL,
0206     EXTCON_JIG,
0207     EXTCON_DOCK,
0208     EXTCON_NONE,
0209 };
0210 
0211 /*
0212  * max77693_muic_set_debounce_time - Set the debounce time of ADC
0213  * @info: the instance including private data of max77693 MUIC
0214  * @time: the debounce time of ADC
0215  */
0216 static int max77693_muic_set_debounce_time(struct max77693_muic_info *info,
0217         enum max77693_muic_adc_debounce_time time)
0218 {
0219     int ret;
0220 
0221     switch (time) {
0222     case ADC_DEBOUNCE_TIME_5MS:
0223     case ADC_DEBOUNCE_TIME_10MS:
0224     case ADC_DEBOUNCE_TIME_25MS:
0225     case ADC_DEBOUNCE_TIME_38_62MS:
0226         /*
0227          * Don't touch BTLDset, JIGset when you want to change adc
0228          * debounce time. If it writes other than 0 to BTLDset, JIGset
0229          * muic device will be reset and loose current state.
0230          */
0231         ret = regmap_write(info->max77693->regmap_muic,
0232                   MAX77693_MUIC_REG_CTRL3,
0233                   time << MAX77693_CONTROL3_ADCDBSET_SHIFT);
0234         if (ret) {
0235             dev_err(info->dev, "failed to set ADC debounce time\n");
0236             return ret;
0237         }
0238         break;
0239     default:
0240         dev_err(info->dev, "invalid ADC debounce time\n");
0241         return -EINVAL;
0242     }
0243 
0244     return 0;
0245 };
0246 
0247 /*
0248  * max77693_muic_set_path - Set hardware line according to attached cable
0249  * @info: the instance including private data of max77693 MUIC
0250  * @value: the path according to attached cable
0251  * @attached: the state of cable (true:attached, false:detached)
0252  *
0253  * The max77693 MUIC device share outside H/W line among a varity of cables
0254  * so, this function set internal path of H/W line according to the type of
0255  * attached cable.
0256  */
0257 static int max77693_muic_set_path(struct max77693_muic_info *info,
0258         u8 val, bool attached)
0259 {
0260     int ret;
0261     unsigned int ctrl1, ctrl2 = 0;
0262 
0263     if (attached)
0264         ctrl1 = val;
0265     else
0266         ctrl1 = MAX77693_CONTROL1_SW_OPEN;
0267 
0268     ret = regmap_update_bits(info->max77693->regmap_muic,
0269             MAX77693_MUIC_REG_CTRL1, COMP_SW_MASK, ctrl1);
0270     if (ret < 0) {
0271         dev_err(info->dev, "failed to update MUIC register\n");
0272         return ret;
0273     }
0274 
0275     if (attached)
0276         ctrl2 |= MAX77693_CONTROL2_CPEN_MASK;   /* LowPwr=0, CPEn=1 */
0277     else
0278         ctrl2 |= MAX77693_CONTROL2_LOWPWR_MASK; /* LowPwr=1, CPEn=0 */
0279 
0280     ret = regmap_update_bits(info->max77693->regmap_muic,
0281             MAX77693_MUIC_REG_CTRL2,
0282             MAX77693_CONTROL2_LOWPWR_MASK | MAX77693_CONTROL2_CPEN_MASK,
0283             ctrl2);
0284     if (ret < 0) {
0285         dev_err(info->dev, "failed to update MUIC register\n");
0286         return ret;
0287     }
0288 
0289     dev_info(info->dev,
0290         "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
0291         ctrl1, ctrl2, attached ? "attached" : "detached");
0292 
0293     return 0;
0294 }
0295 
0296 /*
0297  * max77693_muic_get_cable_type - Return cable type and check cable state
0298  * @info: the instance including private data of max77693 MUIC
0299  * @group: the path according to attached cable
0300  * @attached: store cable state and return
0301  *
0302  * This function check the cable state either attached or detached,
0303  * and then divide precise type of cable according to cable group.
0304  *  - MAX77693_CABLE_GROUP_ADC
0305  *  - MAX77693_CABLE_GROUP_ADC_GND
0306  *  - MAX77693_CABLE_GROUP_CHG
0307  *  - MAX77693_CABLE_GROUP_VBVOLT
0308  */
0309 static int max77693_muic_get_cable_type(struct max77693_muic_info *info,
0310         enum max77693_muic_cable_group group, bool *attached)
0311 {
0312     int cable_type = 0;
0313     int adc;
0314     int adc1k;
0315     int adclow;
0316     int vbvolt;
0317     int chg_type;
0318 
0319     switch (group) {
0320     case MAX77693_CABLE_GROUP_ADC:
0321         /*
0322          * Read ADC value to check cable type and decide cable state
0323          * according to cable type
0324          */
0325         adc = info->status[0] & MAX77693_STATUS1_ADC_MASK;
0326         adc >>= MAX77693_STATUS1_ADC_SHIFT;
0327 
0328         /*
0329          * Check current cable state/cable type and store cable type
0330          * (info->prev_cable_type) for handling cable when cable is
0331          * detached.
0332          */
0333         if (adc == MAX77693_MUIC_ADC_OPEN) {
0334             *attached = false;
0335 
0336             cable_type = info->prev_cable_type;
0337             info->prev_cable_type = MAX77693_MUIC_ADC_OPEN;
0338         } else {
0339             *attached = true;
0340 
0341             cable_type = info->prev_cable_type = adc;
0342         }
0343         break;
0344     case MAX77693_CABLE_GROUP_ADC_GND:
0345         /*
0346          * Read ADC value to check cable type and decide cable state
0347          * according to cable type
0348          */
0349         adc = info->status[0] & MAX77693_STATUS1_ADC_MASK;
0350         adc >>= MAX77693_STATUS1_ADC_SHIFT;
0351 
0352         /*
0353          * Check current cable state/cable type and store cable type
0354          * (info->prev_cable_type/_gnd) for handling cable when cable
0355          * is detached.
0356          */
0357         if (adc == MAX77693_MUIC_ADC_OPEN) {
0358             *attached = false;
0359 
0360             cable_type = info->prev_cable_type_gnd;
0361             info->prev_cable_type_gnd = MAX77693_MUIC_ADC_OPEN;
0362         } else {
0363             *attached = true;
0364 
0365             adclow = info->status[0] & MAX77693_STATUS1_ADCLOW_MASK;
0366             adclow >>= MAX77693_STATUS1_ADCLOW_SHIFT;
0367             adc1k = info->status[0] & MAX77693_STATUS1_ADC1K_MASK;
0368             adc1k >>= MAX77693_STATUS1_ADC1K_SHIFT;
0369 
0370             vbvolt = info->status[1] & MAX77693_STATUS2_VBVOLT_MASK;
0371             vbvolt >>= MAX77693_STATUS2_VBVOLT_SHIFT;
0372 
0373             /**
0374              * [0x1|VBVolt|ADCLow|ADC1K]
0375              * [0x1|     0|     0|    0] USB_HOST
0376              * [0x1|     1|     0|    0] USB_HSOT_VB
0377              * [0x1|     0|     1|    0] Audio Video cable with load
0378              * [0x1|     0|     1|    1] MHL without charging cable
0379              * [0x1|     1|     1|    1] MHL with charging cable
0380              */
0381             cable_type = ((0x1 << 8)
0382                     | (vbvolt << 2)
0383                     | (adclow << 1)
0384                     | adc1k);
0385 
0386             info->prev_cable_type = adc;
0387             info->prev_cable_type_gnd = cable_type;
0388         }
0389 
0390         break;
0391     case MAX77693_CABLE_GROUP_CHG:
0392         /*
0393          * Read charger type to check cable type and decide cable state
0394          * according to type of charger cable.
0395          */
0396         chg_type = info->status[1] & MAX77693_STATUS2_CHGTYP_MASK;
0397         chg_type >>= MAX77693_STATUS2_CHGTYP_SHIFT;
0398 
0399         if (chg_type == MAX77693_CHARGER_TYPE_NONE) {
0400             *attached = false;
0401 
0402             cable_type = info->prev_chg_type;
0403             info->prev_chg_type = MAX77693_CHARGER_TYPE_NONE;
0404         } else {
0405             *attached = true;
0406 
0407             /*
0408              * Check current cable state/cable type and store cable
0409              * type(info->prev_chg_type) for handling cable when
0410              * charger cable is detached.
0411              */
0412             cable_type = info->prev_chg_type = chg_type;
0413         }
0414 
0415         break;
0416     case MAX77693_CABLE_GROUP_VBVOLT:
0417         /*
0418          * Read ADC value to check cable type and decide cable state
0419          * according to cable type
0420          */
0421         adc = info->status[0] & MAX77693_STATUS1_ADC_MASK;
0422         adc >>= MAX77693_STATUS1_ADC_SHIFT;
0423         chg_type = info->status[1] & MAX77693_STATUS2_CHGTYP_MASK;
0424         chg_type >>= MAX77693_STATUS2_CHGTYP_SHIFT;
0425 
0426         if (adc == MAX77693_MUIC_ADC_OPEN
0427                 && chg_type == MAX77693_CHARGER_TYPE_NONE)
0428             *attached = false;
0429         else
0430             *attached = true;
0431 
0432         /*
0433          * Read vbvolt field, if vbvolt is 1,
0434          * this cable is used for charging.
0435          */
0436         vbvolt = info->status[1] & MAX77693_STATUS2_VBVOLT_MASK;
0437         vbvolt >>= MAX77693_STATUS2_VBVOLT_SHIFT;
0438 
0439         cable_type = vbvolt;
0440         break;
0441     default:
0442         dev_err(info->dev, "Unknown cable group (%d)\n", group);
0443         cable_type = -EINVAL;
0444         break;
0445     }
0446 
0447     return cable_type;
0448 }
0449 
0450 static int max77693_muic_dock_handler(struct max77693_muic_info *info,
0451         int cable_type, bool attached)
0452 {
0453     int ret = 0;
0454     int vbvolt;
0455     bool cable_attached;
0456     unsigned int dock_id;
0457 
0458     dev_info(info->dev,
0459         "external connector is %s (adc:0x%02x)\n",
0460         attached ? "attached" : "detached", cable_type);
0461 
0462     switch (cable_type) {
0463     case MAX77693_MUIC_ADC_RESERVED_ACC_3:      /* Dock-Smart */
0464         /*
0465          * Check power cable whether attached or detached state.
0466          * The Dock-Smart device need surely external power supply.
0467          * If power cable(USB/TA) isn't connected to Dock device,
0468          * user can't use Dock-Smart for desktop mode.
0469          */
0470         vbvolt = max77693_muic_get_cable_type(info,
0471                 MAX77693_CABLE_GROUP_VBVOLT, &cable_attached);
0472         if (attached && !vbvolt) {
0473             dev_warn(info->dev,
0474                 "Cannot detect external power supply\n");
0475             return 0;
0476         }
0477 
0478         /*
0479          * Notify Dock/MHL state.
0480          * - Dock device include three type of cable which
0481          * are HDMI, USB for mouse/keyboard and micro-usb port
0482          * for USB/TA cable. Dock device need always exteranl
0483          * power supply(USB/TA cable through micro-usb cable). Dock
0484          * device support screen output of target to separate
0485          * monitor and mouse/keyboard for desktop mode.
0486          *
0487          * Features of 'USB/TA cable with Dock device'
0488          * - Support MHL
0489          * - Support external output feature of audio
0490          * - Support charging through micro-usb port without data
0491          *       connection if TA cable is connected to target.
0492          * - Support charging and data connection through micro-usb port
0493          *           if USB cable is connected between target and host
0494          *       device.
0495          * - Support OTG(On-The-Go) device (Ex: Mouse/Keyboard)
0496          */
0497         ret = max77693_muic_set_path(info, info->path_usb, attached);
0498         if (ret < 0)
0499             return ret;
0500 
0501         extcon_set_state_sync(info->edev, EXTCON_DOCK, attached);
0502         extcon_set_state_sync(info->edev, EXTCON_DISP_MHL, attached);
0503         goto out;
0504     case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE:   /* Dock-Desk */
0505         dock_id = EXTCON_DOCK;
0506         break;
0507     case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD:     /* Dock-Audio */
0508         dock_id = EXTCON_DOCK;
0509         if (!attached) {
0510             extcon_set_state_sync(info->edev, EXTCON_USB, false);
0511             extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SDP,
0512                         false);
0513         }
0514         break;
0515     default:
0516         dev_err(info->dev, "failed to detect %s dock device\n",
0517             attached ? "attached" : "detached");
0518         return -EINVAL;
0519     }
0520 
0521     /* Dock-Car/Desk/Audio, PATH:AUDIO */
0522     ret = max77693_muic_set_path(info, MAX77693_CONTROL1_SW_AUDIO,
0523                     attached);
0524     if (ret < 0)
0525         return ret;
0526     extcon_set_state_sync(info->edev, dock_id, attached);
0527 
0528 out:
0529     return 0;
0530 }
0531 
0532 static int max77693_muic_dock_button_handler(struct max77693_muic_info *info,
0533         int button_type, bool attached)
0534 {
0535     struct input_dev *dock = info->dock;
0536     unsigned int code;
0537 
0538     switch (button_type) {
0539     case MAX77693_MUIC_ADC_REMOTE_S3_BUTTON-1
0540         ... MAX77693_MUIC_ADC_REMOTE_S3_BUTTON+1:
0541         /* DOCK_KEY_PREV */
0542         code = KEY_PREVIOUSSONG;
0543         break;
0544     case MAX77693_MUIC_ADC_REMOTE_S7_BUTTON-1
0545         ... MAX77693_MUIC_ADC_REMOTE_S7_BUTTON+1:
0546         /* DOCK_KEY_NEXT */
0547         code = KEY_NEXTSONG;
0548         break;
0549     case MAX77693_MUIC_ADC_REMOTE_S9_BUTTON:
0550         /* DOCK_VOL_DOWN */
0551         code = KEY_VOLUMEDOWN;
0552         break;
0553     case MAX77693_MUIC_ADC_REMOTE_S10_BUTTON:
0554         /* DOCK_VOL_UP */
0555         code = KEY_VOLUMEUP;
0556         break;
0557     case MAX77693_MUIC_ADC_REMOTE_S12_BUTTON-1
0558         ... MAX77693_MUIC_ADC_REMOTE_S12_BUTTON+1:
0559         /* DOCK_KEY_PLAY_PAUSE */
0560         code = KEY_PLAYPAUSE;
0561         break;
0562     default:
0563         dev_err(info->dev,
0564             "failed to detect %s key (adc:0x%x)\n",
0565             attached ? "pressed" : "released", button_type);
0566         return -EINVAL;
0567     }
0568 
0569     input_event(dock, EV_KEY, code, attached);
0570     input_sync(dock);
0571 
0572     return 0;
0573 }
0574 
0575 static int max77693_muic_adc_ground_handler(struct max77693_muic_info *info)
0576 {
0577     int cable_type_gnd;
0578     int ret = 0;
0579     bool attached;
0580 
0581     cable_type_gnd = max77693_muic_get_cable_type(info,
0582                 MAX77693_CABLE_GROUP_ADC_GND, &attached);
0583 
0584     switch (cable_type_gnd) {
0585     case MAX77693_MUIC_GND_USB_HOST:
0586     case MAX77693_MUIC_GND_USB_HOST_VB:
0587         /* USB_HOST, PATH: AP_USB */
0588         ret = max77693_muic_set_path(info, MAX77693_CONTROL1_SW_USB,
0589                         attached);
0590         if (ret < 0)
0591             return ret;
0592         extcon_set_state_sync(info->edev, EXTCON_USB_HOST, attached);
0593         break;
0594     case MAX77693_MUIC_GND_AV_CABLE_LOAD:
0595         /* Audio Video Cable with load, PATH:AUDIO */
0596         ret = max77693_muic_set_path(info, MAX77693_CONTROL1_SW_AUDIO,
0597                         attached);
0598         if (ret < 0)
0599             return ret;
0600         extcon_set_state_sync(info->edev, EXTCON_USB, attached);
0601         extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SDP,
0602                     attached);
0603         break;
0604     case MAX77693_MUIC_GND_MHL:
0605     case MAX77693_MUIC_GND_MHL_VB:
0606         /* MHL or MHL with USB/TA cable */
0607         extcon_set_state_sync(info->edev, EXTCON_DISP_MHL, attached);
0608         break;
0609     default:
0610         dev_err(info->dev, "failed to detect %s cable of gnd type\n",
0611             attached ? "attached" : "detached");
0612         return -EINVAL;
0613     }
0614 
0615     return 0;
0616 }
0617 
0618 static int max77693_muic_jig_handler(struct max77693_muic_info *info,
0619         int cable_type, bool attached)
0620 {
0621     int ret = 0;
0622     u8 path = MAX77693_CONTROL1_SW_OPEN;
0623 
0624     dev_info(info->dev,
0625         "external connector is %s (adc:0x%02x)\n",
0626         attached ? "attached" : "detached", cable_type);
0627 
0628     switch (cable_type) {
0629     case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF:    /* ADC_JIG_USB_OFF */
0630     case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON: /* ADC_JIG_USB_ON */
0631         /* PATH:AP_USB */
0632         path = MAX77693_CONTROL1_SW_USB;
0633         break;
0634     case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF:   /* ADC_JIG_UART_OFF */
0635     case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON:    /* ADC_JIG_UART_ON */
0636         /* PATH:AP_UART */
0637         path = MAX77693_CONTROL1_SW_UART;
0638         break;
0639     default:
0640         dev_err(info->dev, "failed to detect %s jig cable\n",
0641             attached ? "attached" : "detached");
0642         return -EINVAL;
0643     }
0644 
0645     ret = max77693_muic_set_path(info, path, attached);
0646     if (ret < 0)
0647         return ret;
0648 
0649     extcon_set_state_sync(info->edev, EXTCON_JIG, attached);
0650 
0651     return 0;
0652 }
0653 
0654 static int max77693_muic_adc_handler(struct max77693_muic_info *info)
0655 {
0656     int cable_type;
0657     int button_type;
0658     bool attached;
0659     int ret = 0;
0660 
0661     /* Check accessory state which is either detached or attached */
0662     cable_type = max77693_muic_get_cable_type(info,
0663                 MAX77693_CABLE_GROUP_ADC, &attached);
0664 
0665     dev_info(info->dev,
0666         "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
0667         attached ? "attached" : "detached", cable_type,
0668         info->prev_cable_type);
0669 
0670     switch (cable_type) {
0671     case MAX77693_MUIC_ADC_GROUND:
0672         /* USB_HOST/MHL/Audio */
0673         max77693_muic_adc_ground_handler(info);
0674         break;
0675     case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF:
0676     case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON:
0677     case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF:
0678     case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON:
0679         /* JIG */
0680         ret = max77693_muic_jig_handler(info, cable_type, attached);
0681         if (ret < 0)
0682             return ret;
0683         break;
0684     case MAX77693_MUIC_ADC_RESERVED_ACC_3:      /* Dock-Smart */
0685     case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE:   /* Dock-Desk */
0686     case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD:     /* Dock-Audio */
0687         /*
0688          * DOCK device
0689          *
0690          * The MAX77693 MUIC device can detect total 34 cable type
0691          * except of charger cable and MUIC device didn't define
0692          * specfic role of cable in the range of from 0x01 to 0x12
0693          * of ADC value. So, can use/define cable with no role according
0694          * to schema of hardware board.
0695          */
0696         ret = max77693_muic_dock_handler(info, cable_type, attached);
0697         if (ret < 0)
0698             return ret;
0699         break;
0700     case MAX77693_MUIC_ADC_REMOTE_S3_BUTTON:      /* DOCK_KEY_PREV */
0701     case MAX77693_MUIC_ADC_REMOTE_S7_BUTTON:      /* DOCK_KEY_NEXT */
0702     case MAX77693_MUIC_ADC_REMOTE_S9_BUTTON:      /* DOCK_VOL_DOWN */
0703     case MAX77693_MUIC_ADC_REMOTE_S10_BUTTON:     /* DOCK_VOL_UP */
0704     case MAX77693_MUIC_ADC_REMOTE_S12_BUTTON:     /* DOCK_KEY_PLAY_PAUSE */
0705         /*
0706          * Button of DOCK device
0707          * - the Prev/Next/Volume Up/Volume Down/Play-Pause button
0708          *
0709          * The MAX77693 MUIC device can detect total 34 cable type
0710          * except of charger cable and MUIC device didn't define
0711          * specfic role of cable in the range of from 0x01 to 0x12
0712          * of ADC value. So, can use/define cable with no role according
0713          * to schema of hardware board.
0714          */
0715         if (attached)
0716             button_type = info->prev_button_type = cable_type;
0717         else
0718             button_type = info->prev_button_type;
0719 
0720         ret = max77693_muic_dock_button_handler(info, button_type,
0721                             attached);
0722         if (ret < 0)
0723             return ret;
0724         break;
0725     case MAX77693_MUIC_ADC_SEND_END_BUTTON:
0726     case MAX77693_MUIC_ADC_REMOTE_S1_BUTTON:
0727     case MAX77693_MUIC_ADC_REMOTE_S2_BUTTON:
0728     case MAX77693_MUIC_ADC_REMOTE_S4_BUTTON:
0729     case MAX77693_MUIC_ADC_REMOTE_S5_BUTTON:
0730     case MAX77693_MUIC_ADC_REMOTE_S6_BUTTON:
0731     case MAX77693_MUIC_ADC_REMOTE_S8_BUTTON:
0732     case MAX77693_MUIC_ADC_REMOTE_S11_BUTTON:
0733     case MAX77693_MUIC_ADC_RESERVED_ACC_1:
0734     case MAX77693_MUIC_ADC_RESERVED_ACC_2:
0735     case MAX77693_MUIC_ADC_RESERVED_ACC_4:
0736     case MAX77693_MUIC_ADC_RESERVED_ACC_5:
0737     case MAX77693_MUIC_ADC_CEA936_AUDIO:
0738     case MAX77693_MUIC_ADC_PHONE_POWERED_DEV:
0739     case MAX77693_MUIC_ADC_TTY_CONVERTER:
0740     case MAX77693_MUIC_ADC_UART_CABLE:
0741     case MAX77693_MUIC_ADC_CEA936A_TYPE1_CHG:
0742     case MAX77693_MUIC_ADC_CEA936A_TYPE2_CHG:
0743         /*
0744          * This accessory isn't used in general case if it is specially
0745          * needed to detect additional accessory, should implement
0746          * proper operation when this accessory is attached/detached.
0747          */
0748         dev_info(info->dev,
0749             "accessory is %s but it isn't used (adc:0x%x)\n",
0750             attached ? "attached" : "detached", cable_type);
0751         return -EAGAIN;
0752     default:
0753         dev_err(info->dev,
0754             "failed to detect %s accessory (adc:0x%x)\n",
0755             attached ? "attached" : "detached", cable_type);
0756         return -EINVAL;
0757     }
0758 
0759     return 0;
0760 }
0761 
0762 static int max77693_muic_chg_handler(struct max77693_muic_info *info)
0763 {
0764     int chg_type;
0765     int cable_type_gnd;
0766     int cable_type;
0767     bool attached;
0768     bool cable_attached;
0769     int ret = 0;
0770 
0771     chg_type = max77693_muic_get_cable_type(info,
0772                 MAX77693_CABLE_GROUP_CHG, &attached);
0773 
0774     dev_info(info->dev,
0775         "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n",
0776             attached ? "attached" : "detached",
0777             chg_type, info->prev_chg_type);
0778 
0779     switch (chg_type) {
0780     case MAX77693_CHARGER_TYPE_USB:
0781     case MAX77693_CHARGER_TYPE_DEDICATED_CHG:
0782     case MAX77693_CHARGER_TYPE_NONE:
0783         /* Check MAX77693_CABLE_GROUP_ADC_GND type */
0784         cable_type_gnd = max77693_muic_get_cable_type(info,
0785                     MAX77693_CABLE_GROUP_ADC_GND,
0786                     &cable_attached);
0787         switch (cable_type_gnd) {
0788         case MAX77693_MUIC_GND_MHL:
0789         case MAX77693_MUIC_GND_MHL_VB:
0790             /*
0791              * MHL cable with USB/TA cable
0792              * - MHL cable include two port(HDMI line and separate
0793              * micro-usb port. When the target connect MHL cable,
0794              * extcon driver check whether USB/TA cable is
0795              * connected. If USB/TA cable is connected, extcon
0796              * driver notify state to notifiee for charging battery.
0797              *
0798              * Features of 'USB/TA with MHL cable'
0799              * - Support MHL
0800              * - Support charging through micro-usb port without
0801              *   data connection
0802              */
0803             extcon_set_state_sync(info->edev, EXTCON_CHG_USB_DCP,
0804                         attached);
0805             extcon_set_state_sync(info->edev, EXTCON_DISP_MHL,
0806                         cable_attached);
0807             break;
0808         }
0809 
0810         /* Check MAX77693_CABLE_GROUP_ADC type */
0811         cable_type = max77693_muic_get_cable_type(info,
0812                     MAX77693_CABLE_GROUP_ADC,
0813                     &cable_attached);
0814         switch (cable_type) {
0815         case MAX77693_MUIC_ADC_AV_CABLE_NOLOAD:     /* Dock-Audio */
0816             /*
0817              * Dock-Audio device with USB/TA cable
0818              * - Dock device include two port(Dock-Audio and micro-
0819              * usb port). When the target connect Dock-Audio device,
0820              * extcon driver check whether USB/TA cable is connected
0821              * or not. If USB/TA cable is connected, extcon driver
0822              * notify state to notifiee for charging battery.
0823              *
0824              * Features of 'USB/TA cable with Dock-Audio device'
0825              * - Support external output feature of audio.
0826              * - Support charging through micro-usb port without
0827              *   data connection.
0828              */
0829             extcon_set_state_sync(info->edev, EXTCON_USB,
0830                         attached);
0831             extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SDP,
0832                         attached);
0833 
0834             if (!cable_attached)
0835                 extcon_set_state_sync(info->edev, EXTCON_DOCK,
0836                             cable_attached);
0837             break;
0838         case MAX77693_MUIC_ADC_RESERVED_ACC_3:      /* Dock-Smart */
0839             /*
0840              * Dock-Smart device with USB/TA cable
0841              * - Dock-Desk device include three type of cable which
0842              * are HDMI, USB for mouse/keyboard and micro-usb port
0843              * for USB/TA cable. Dock-Smart device need always
0844              * exteranl power supply(USB/TA cable through micro-usb
0845              * cable). Dock-Smart device support screen output of
0846              * target to separate monitor and mouse/keyboard for
0847              * desktop mode.
0848              *
0849              * Features of 'USB/TA cable with Dock-Smart device'
0850              * - Support MHL
0851              * - Support external output feature of audio
0852              * - Support charging through micro-usb port without
0853              *   data connection if TA cable is connected to target.
0854              * - Support charging and data connection through micro-
0855              *   usb port if USB cable is connected between target
0856              *   and host device
0857              * - Support OTG(On-The-Go) device (Ex: Mouse/Keyboard)
0858              */
0859             ret = max77693_muic_set_path(info, info->path_usb,
0860                             attached);
0861             if (ret < 0)
0862                 return ret;
0863 
0864             extcon_set_state_sync(info->edev, EXTCON_DOCK,
0865                         attached);
0866             extcon_set_state_sync(info->edev, EXTCON_DISP_MHL,
0867                         attached);
0868             break;
0869         }
0870 
0871         /* Check MAX77693_CABLE_GROUP_CHG type */
0872         switch (chg_type) {
0873         case MAX77693_CHARGER_TYPE_NONE:
0874             /*
0875              * When MHL(with USB/TA cable) or Dock-Audio with USB/TA
0876              * cable is attached, muic device happen below two irq.
0877              * - 'MAX77693_MUIC_IRQ_INT1_ADC' for detecting
0878              *    MHL/Dock-Audio.
0879              * - 'MAX77693_MUIC_IRQ_INT2_CHGTYP' for detecting
0880              *    USB/TA cable connected to MHL or Dock-Audio.
0881              * Always, happen eariler MAX77693_MUIC_IRQ_INT1_ADC
0882              * irq than MAX77693_MUIC_IRQ_INT2_CHGTYP irq.
0883              *
0884              * If user attach MHL (with USB/TA cable and immediately
0885              * detach MHL with USB/TA cable before MAX77693_MUIC_IRQ
0886              * _INT2_CHGTYP irq is happened, USB/TA cable remain
0887              * connected state to target. But USB/TA cable isn't
0888              * connected to target. The user be face with unusual
0889              * action. So, driver should check this situation in
0890              * spite of, that previous charger type is N/A.
0891              */
0892             break;
0893         case MAX77693_CHARGER_TYPE_USB:
0894             /* Only USB cable, PATH:AP_USB */
0895             ret = max77693_muic_set_path(info, info->path_usb,
0896                             attached);
0897             if (ret < 0)
0898                 return ret;
0899 
0900             extcon_set_state_sync(info->edev, EXTCON_USB,
0901                         attached);
0902             extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SDP,
0903                         attached);
0904             break;
0905         case MAX77693_CHARGER_TYPE_DEDICATED_CHG:
0906             /* Only TA cable */
0907             extcon_set_state_sync(info->edev, EXTCON_CHG_USB_DCP,
0908                         attached);
0909             break;
0910         }
0911         break;
0912     case MAX77693_CHARGER_TYPE_DOWNSTREAM_PORT:
0913         extcon_set_state_sync(info->edev, EXTCON_CHG_USB_CDP,
0914                     attached);
0915         break;
0916     case MAX77693_CHARGER_TYPE_APPLE_500MA:
0917         extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SLOW,
0918                     attached);
0919         break;
0920     case MAX77693_CHARGER_TYPE_APPLE_1A_2A:
0921         extcon_set_state_sync(info->edev, EXTCON_CHG_USB_FAST,
0922                     attached);
0923         break;
0924     case MAX77693_CHARGER_TYPE_DEAD_BATTERY:
0925         break;
0926     default:
0927         dev_err(info->dev,
0928             "failed to detect %s accessory (chg_type:0x%x)\n",
0929             attached ? "attached" : "detached", chg_type);
0930         return -EINVAL;
0931     }
0932 
0933     return 0;
0934 }
0935 
0936 static void max77693_muic_irq_work(struct work_struct *work)
0937 {
0938     struct max77693_muic_info *info = container_of(work,
0939             struct max77693_muic_info, irq_work);
0940     int irq_type = -1;
0941     int i, ret = 0;
0942 
0943     if (!info->edev)
0944         return;
0945 
0946     mutex_lock(&info->mutex);
0947 
0948     for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
0949         if (info->irq == muic_irqs[i].virq)
0950             irq_type = muic_irqs[i].irq;
0951 
0952     ret = regmap_bulk_read(info->max77693->regmap_muic,
0953             MAX77693_MUIC_REG_STATUS1, info->status, 2);
0954     if (ret) {
0955         dev_err(info->dev, "failed to read MUIC register\n");
0956         mutex_unlock(&info->mutex);
0957         return;
0958     }
0959 
0960     switch (irq_type) {
0961     case MAX77693_MUIC_IRQ_INT1_ADC:
0962     case MAX77693_MUIC_IRQ_INT1_ADC_LOW:
0963     case MAX77693_MUIC_IRQ_INT1_ADC_ERR:
0964     case MAX77693_MUIC_IRQ_INT1_ADC1K:
0965         /*
0966          * Handle all of accessory except for
0967          * type of charger accessory.
0968          */
0969         ret = max77693_muic_adc_handler(info);
0970         break;
0971     case MAX77693_MUIC_IRQ_INT2_CHGTYP:
0972     case MAX77693_MUIC_IRQ_INT2_CHGDETREUN:
0973     case MAX77693_MUIC_IRQ_INT2_DCDTMR:
0974     case MAX77693_MUIC_IRQ_INT2_DXOVP:
0975     case MAX77693_MUIC_IRQ_INT2_VBVOLT:
0976     case MAX77693_MUIC_IRQ_INT2_VIDRM:
0977         /* Handle charger accessory */
0978         ret = max77693_muic_chg_handler(info);
0979         break;
0980     case MAX77693_MUIC_IRQ_INT3_EOC:
0981     case MAX77693_MUIC_IRQ_INT3_CGMBC:
0982     case MAX77693_MUIC_IRQ_INT3_OVP:
0983     case MAX77693_MUIC_IRQ_INT3_MBCCHG_ERR:
0984     case MAX77693_MUIC_IRQ_INT3_CHG_ENABLED:
0985     case MAX77693_MUIC_IRQ_INT3_BAT_DET:
0986         break;
0987     default:
0988         dev_err(info->dev, "muic interrupt: irq %d occurred\n",
0989                 irq_type);
0990         mutex_unlock(&info->mutex);
0991         return;
0992     }
0993 
0994     if (ret < 0)
0995         dev_err(info->dev, "failed to handle MUIC interrupt\n");
0996 
0997     mutex_unlock(&info->mutex);
0998 }
0999 
1000 static irqreturn_t max77693_muic_irq_handler(int irq, void *data)
1001 {
1002     struct max77693_muic_info *info = data;
1003 
1004     info->irq = irq;
1005     schedule_work(&info->irq_work);
1006 
1007     return IRQ_HANDLED;
1008 }
1009 
1010 static const struct regmap_config max77693_muic_regmap_config = {
1011     .reg_bits = 8,
1012     .val_bits = 8,
1013 };
1014 
1015 static int max77693_muic_detect_accessory(struct max77693_muic_info *info)
1016 {
1017     int ret = 0;
1018     int adc;
1019     int chg_type;
1020     bool attached;
1021 
1022     mutex_lock(&info->mutex);
1023 
1024     /* Read STATUSx register to detect accessory */
1025     ret = regmap_bulk_read(info->max77693->regmap_muic,
1026             MAX77693_MUIC_REG_STATUS1, info->status, 2);
1027     if (ret) {
1028         dev_err(info->dev, "failed to read MUIC register\n");
1029         mutex_unlock(&info->mutex);
1030         return ret;
1031     }
1032 
1033     adc = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_ADC,
1034                     &attached);
1035     if (attached && adc != MAX77693_MUIC_ADC_OPEN) {
1036         ret = max77693_muic_adc_handler(info);
1037         if (ret < 0) {
1038             dev_err(info->dev, "Cannot detect accessory\n");
1039             mutex_unlock(&info->mutex);
1040             return ret;
1041         }
1042     }
1043 
1044     chg_type = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_CHG,
1045                     &attached);
1046     if (attached && chg_type != MAX77693_CHARGER_TYPE_NONE) {
1047         ret = max77693_muic_chg_handler(info);
1048         if (ret < 0) {
1049             dev_err(info->dev, "Cannot detect charger accessory\n");
1050             mutex_unlock(&info->mutex);
1051             return ret;
1052         }
1053     }
1054 
1055     mutex_unlock(&info->mutex);
1056 
1057     return 0;
1058 }
1059 
1060 static void max77693_muic_detect_cable_wq(struct work_struct *work)
1061 {
1062     struct max77693_muic_info *info = container_of(to_delayed_work(work),
1063                 struct max77693_muic_info, wq_detcable);
1064 
1065     max77693_muic_detect_accessory(info);
1066 }
1067 
1068 static int max77693_muic_probe(struct platform_device *pdev)
1069 {
1070     struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent);
1071     struct max77693_platform_data *pdata = dev_get_platdata(max77693->dev);
1072     struct max77693_muic_info *info;
1073     struct max77693_reg_data *init_data;
1074     int num_init_data;
1075     int delay_jiffies;
1076     int cable_type;
1077     bool attached;
1078     int ret;
1079     int i;
1080     unsigned int id;
1081 
1082     info = devm_kzalloc(&pdev->dev, sizeof(struct max77693_muic_info),
1083                    GFP_KERNEL);
1084     if (!info)
1085         return -ENOMEM;
1086 
1087     info->dev = &pdev->dev;
1088     info->max77693 = max77693;
1089     if (info->max77693->regmap_muic) {
1090         dev_dbg(&pdev->dev, "allocate register map\n");
1091     } else {
1092         info->max77693->regmap_muic = devm_regmap_init_i2c(
1093                         info->max77693->i2c_muic,
1094                         &max77693_muic_regmap_config);
1095         if (IS_ERR(info->max77693->regmap_muic)) {
1096             ret = PTR_ERR(info->max77693->regmap_muic);
1097             dev_err(max77693->dev,
1098                 "failed to allocate register map: %d\n", ret);
1099             return ret;
1100         }
1101     }
1102 
1103     /* Register input device for button of dock device */
1104     info->dock = devm_input_allocate_device(&pdev->dev);
1105     if (!info->dock) {
1106         dev_err(&pdev->dev, "%s: failed to allocate input\n", __func__);
1107         return -ENOMEM;
1108     }
1109     info->dock->name = "max77693-muic/dock";
1110     info->dock->phys = "max77693-muic/extcon";
1111     info->dock->dev.parent = &pdev->dev;
1112 
1113     __set_bit(EV_REP, info->dock->evbit);
1114 
1115     input_set_capability(info->dock, EV_KEY, KEY_VOLUMEUP);
1116     input_set_capability(info->dock, EV_KEY, KEY_VOLUMEDOWN);
1117     input_set_capability(info->dock, EV_KEY, KEY_PLAYPAUSE);
1118     input_set_capability(info->dock, EV_KEY, KEY_PREVIOUSSONG);
1119     input_set_capability(info->dock, EV_KEY, KEY_NEXTSONG);
1120 
1121     ret = input_register_device(info->dock);
1122     if (ret < 0) {
1123         dev_err(&pdev->dev, "Cannot register input device error(%d)\n",
1124                 ret);
1125         return ret;
1126     }
1127 
1128     platform_set_drvdata(pdev, info);
1129     mutex_init(&info->mutex);
1130 
1131     ret = devm_work_autocancel(&pdev->dev, &info->irq_work,
1132                    max77693_muic_irq_work);
1133     if (ret)
1134         return ret;
1135 
1136     /* Support irq domain for MAX77693 MUIC device */
1137     for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) {
1138         struct max77693_muic_irq *muic_irq = &muic_irqs[i];
1139         int virq;
1140 
1141         virq = regmap_irq_get_virq(max77693->irq_data_muic,
1142                     muic_irq->irq);
1143         if (virq <= 0)
1144             return -EINVAL;
1145         muic_irq->virq = virq;
1146 
1147         ret = devm_request_threaded_irq(&pdev->dev, virq, NULL,
1148                 max77693_muic_irq_handler,
1149                 IRQF_NO_SUSPEND,
1150                 muic_irq->name, info);
1151         if (ret) {
1152             dev_err(&pdev->dev,
1153                 "failed: irq request (IRQ: %d, error :%d)\n",
1154                 muic_irq->irq, ret);
1155             return ret;
1156         }
1157     }
1158 
1159     /* Initialize extcon device */
1160     info->edev = devm_extcon_dev_allocate(&pdev->dev,
1161                           max77693_extcon_cable);
1162     if (IS_ERR(info->edev)) {
1163         dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
1164         return PTR_ERR(info->edev);
1165     }
1166 
1167     ret = devm_extcon_dev_register(&pdev->dev, info->edev);
1168     if (ret) {
1169         dev_err(&pdev->dev, "failed to register extcon device\n");
1170         return ret;
1171     }
1172 
1173     /* Initialize MUIC register by using platform data or default data */
1174     if (pdata && pdata->muic_data) {
1175         init_data = pdata->muic_data->init_data;
1176         num_init_data = pdata->muic_data->num_init_data;
1177     } else {
1178         init_data = default_init_data;
1179         num_init_data = ARRAY_SIZE(default_init_data);
1180     }
1181 
1182     for (i = 0; i < num_init_data; i++) {
1183         regmap_write(info->max77693->regmap_muic,
1184                 init_data[i].addr,
1185                 init_data[i].data);
1186     }
1187 
1188     if (pdata && pdata->muic_data) {
1189         struct max77693_muic_platform_data *muic_pdata
1190                            = pdata->muic_data;
1191 
1192         /*
1193          * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
1194          * h/w path of COMP2/COMN1 on CONTROL1 register.
1195          */
1196         if (muic_pdata->path_uart)
1197             info->path_uart = muic_pdata->path_uart;
1198         else
1199             info->path_uart = MAX77693_CONTROL1_SW_UART;
1200 
1201         if (muic_pdata->path_usb)
1202             info->path_usb = muic_pdata->path_usb;
1203         else
1204             info->path_usb = MAX77693_CONTROL1_SW_USB;
1205 
1206         /*
1207          * Default delay time for detecting cable state
1208          * after certain time.
1209          */
1210         if (muic_pdata->detcable_delay_ms)
1211             delay_jiffies =
1212                 msecs_to_jiffies(muic_pdata->detcable_delay_ms);
1213         else
1214             delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
1215     } else {
1216         info->path_usb = MAX77693_CONTROL1_SW_USB;
1217         info->path_uart = MAX77693_CONTROL1_SW_UART;
1218         delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
1219     }
1220 
1221     /* Set initial path for UART when JIG is connected to get serial logs */
1222     ret = regmap_bulk_read(info->max77693->regmap_muic,
1223             MAX77693_MUIC_REG_STATUS1, info->status, 2);
1224     if (ret) {
1225         dev_err(info->dev, "failed to read MUIC register\n");
1226         return ret;
1227     }
1228     cable_type = max77693_muic_get_cable_type(info,
1229                        MAX77693_CABLE_GROUP_ADC, &attached);
1230     if (attached && (cable_type == MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON ||
1231              cable_type == MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF))
1232         max77693_muic_set_path(info, info->path_uart, true);
1233 
1234     /* Check revision number of MUIC device*/
1235     ret = regmap_read(info->max77693->regmap_muic,
1236             MAX77693_MUIC_REG_ID, &id);
1237     if (ret < 0) {
1238         dev_err(&pdev->dev, "failed to read revision number\n");
1239         return ret;
1240     }
1241     dev_info(info->dev, "device ID : 0x%x\n", id);
1242 
1243     /* Set ADC debounce time */
1244     max77693_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS);
1245 
1246     /*
1247      * Detect accessory after completing the initialization of platform
1248      *
1249      * - Use delayed workqueue to detect cable state and then
1250      * notify cable state to notifiee/platform through uevent.
1251      * After completing the booting of platform, the extcon provider
1252      * driver should notify cable state to upper layer.
1253      */
1254     INIT_DELAYED_WORK(&info->wq_detcable, max77693_muic_detect_cable_wq);
1255     queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
1256             delay_jiffies);
1257 
1258     return ret;
1259 }
1260 
1261 static struct platform_driver max77693_muic_driver = {
1262     .driver     = {
1263         .name   = DEV_NAME,
1264     },
1265     .probe      = max77693_muic_probe,
1266 };
1267 
1268 module_platform_driver(max77693_muic_driver);
1269 
1270 MODULE_DESCRIPTION("Maxim MAX77693 Extcon driver");
1271 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
1272 MODULE_LICENSE("GPL");
1273 MODULE_ALIAS("platform:max77693-muic");