Chanwoo Choi | 914b881 | 2014-05-22 14:06:33 +0900 | [diff] [blame] | 1 | /* |
| 2 | * extcon-sm5502.c - Silicon Mitus SM5502 extcon drvier to support USB switches |
| 3 | * |
| 4 | * Copyright (c) 2014 Samsung Electronics Co., Ltd |
| 5 | * Author: Chanwoo Choi <cw00.choi@samsung.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License as published by the |
| 9 | * Free Software Foundation; either version 2 of the License, or (at your |
| 10 | * option) any later version. |
Chanwoo Choi | 914b881 | 2014-05-22 14:06:33 +0900 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <linux/err.h> |
| 14 | #include <linux/i2c.h> |
Chanwoo Choi | 914b881 | 2014-05-22 14:06:33 +0900 | [diff] [blame] | 15 | #include <linux/interrupt.h> |
| 16 | #include <linux/irqdomain.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/regmap.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/extcon.h> |
Chanwoo Choi | ca2a07e | 2014-07-31 16:32:46 +0900 | [diff] [blame] | 23 | |
| 24 | #include "extcon-sm5502.h" |
Chanwoo Choi | 914b881 | 2014-05-22 14:06:33 +0900 | [diff] [blame] | 25 | |
Chanwoo Choi | e195445 | 2014-05-28 14:21:55 +0900 | [diff] [blame] | 26 | #define DELAY_MS_DEFAULT 17000 /* unit: millisecond */ |
| 27 | |
Chanwoo Choi | 914b881 | 2014-05-22 14:06:33 +0900 | [diff] [blame] | 28 | struct muic_irq { |
| 29 | unsigned int irq; |
| 30 | const char *name; |
| 31 | unsigned int virq; |
| 32 | }; |
| 33 | |
| 34 | struct reg_data { |
| 35 | u8 reg; |
| 36 | unsigned int val; |
| 37 | bool invert; |
| 38 | }; |
| 39 | |
| 40 | struct sm5502_muic_info { |
| 41 | struct device *dev; |
| 42 | struct extcon_dev *edev; |
| 43 | |
| 44 | struct i2c_client *i2c; |
| 45 | struct regmap *regmap; |
| 46 | |
| 47 | struct regmap_irq_chip_data *irq_data; |
| 48 | struct muic_irq *muic_irqs; |
| 49 | unsigned int num_muic_irqs; |
| 50 | int irq; |
| 51 | bool irq_attach; |
| 52 | bool irq_detach; |
| 53 | struct work_struct irq_work; |
| 54 | |
| 55 | struct reg_data *reg_data; |
| 56 | unsigned int num_reg_data; |
| 57 | |
| 58 | struct mutex mutex; |
Chanwoo Choi | e195445 | 2014-05-28 14:21:55 +0900 | [diff] [blame] | 59 | |
| 60 | /* |
| 61 | * Use delayed workqueue to detect cable state and then |
| 62 | * notify cable state to notifiee/platform through uevent. |
| 63 | * After completing the booting of platform, the extcon provider |
| 64 | * driver should notify cable state to upper layer. |
| 65 | */ |
| 66 | struct delayed_work wq_detcable; |
Chanwoo Choi | 914b881 | 2014-05-22 14:06:33 +0900 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | /* Default value of SM5502 register to bring up MUIC device. */ |
| 70 | static struct reg_data sm5502_reg_data[] = { |
| 71 | { |
| 72 | .reg = SM5502_REG_CONTROL, |
| 73 | .val = SM5502_REG_CONTROL_MASK_INT_MASK, |
| 74 | .invert = false, |
| 75 | }, { |
| 76 | .reg = SM5502_REG_INTMASK1, |
| 77 | .val = SM5502_REG_INTM1_KP_MASK |
| 78 | | SM5502_REG_INTM1_LKP_MASK |
| 79 | | SM5502_REG_INTM1_LKR_MASK, |
| 80 | .invert = true, |
| 81 | }, { |
| 82 | .reg = SM5502_REG_INTMASK2, |
| 83 | .val = SM5502_REG_INTM2_VBUS_DET_MASK |
| 84 | | SM5502_REG_INTM2_REV_ACCE_MASK |
| 85 | | SM5502_REG_INTM2_ADC_CHG_MASK |
| 86 | | SM5502_REG_INTM2_STUCK_KEY_MASK |
| 87 | | SM5502_REG_INTM2_STUCK_KEY_RCV_MASK |
| 88 | | SM5502_REG_INTM2_MHL_MASK, |
| 89 | .invert = true, |
| 90 | }, |
| 91 | { } |
| 92 | }; |
| 93 | |
| 94 | /* List of detectable cables */ |
Chanwoo Choi | 73b6ecd | 2015-06-12 11:10:06 +0900 | [diff] [blame] | 95 | static const unsigned int sm5502_extcon_cable[] = { |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 96 | EXTCON_USB, |
| 97 | EXTCON_USB_HOST, |
Chanwoo Choi | 11eecf9 | 2015-10-03 14:15:13 +0900 | [diff] [blame] | 98 | EXTCON_CHG_USB_DCP, |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 99 | EXTCON_NONE, |
Chanwoo Choi | 914b881 | 2014-05-22 14:06:33 +0900 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | /* Define supported accessory type */ |
| 103 | enum sm5502_muic_acc_type { |
| 104 | SM5502_MUIC_ADC_GROUND = 0x0, |
| 105 | SM5502_MUIC_ADC_SEND_END_BUTTON, |
| 106 | SM5502_MUIC_ADC_REMOTE_S1_BUTTON, |
| 107 | SM5502_MUIC_ADC_REMOTE_S2_BUTTON, |
| 108 | SM5502_MUIC_ADC_REMOTE_S3_BUTTON, |
| 109 | SM5502_MUIC_ADC_REMOTE_S4_BUTTON, |
| 110 | SM5502_MUIC_ADC_REMOTE_S5_BUTTON, |
| 111 | SM5502_MUIC_ADC_REMOTE_S6_BUTTON, |
| 112 | SM5502_MUIC_ADC_REMOTE_S7_BUTTON, |
| 113 | SM5502_MUIC_ADC_REMOTE_S8_BUTTON, |
| 114 | SM5502_MUIC_ADC_REMOTE_S9_BUTTON, |
| 115 | SM5502_MUIC_ADC_REMOTE_S10_BUTTON, |
| 116 | SM5502_MUIC_ADC_REMOTE_S11_BUTTON, |
| 117 | SM5502_MUIC_ADC_REMOTE_S12_BUTTON, |
| 118 | SM5502_MUIC_ADC_RESERVED_ACC_1, |
| 119 | SM5502_MUIC_ADC_RESERVED_ACC_2, |
| 120 | SM5502_MUIC_ADC_RESERVED_ACC_3, |
| 121 | SM5502_MUIC_ADC_RESERVED_ACC_4, |
| 122 | SM5502_MUIC_ADC_RESERVED_ACC_5, |
| 123 | SM5502_MUIC_ADC_AUDIO_TYPE2, |
| 124 | SM5502_MUIC_ADC_PHONE_POWERED_DEV, |
| 125 | SM5502_MUIC_ADC_TTY_CONVERTER, |
| 126 | SM5502_MUIC_ADC_UART_CABLE, |
| 127 | SM5502_MUIC_ADC_TYPE1_CHARGER, |
| 128 | SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB, |
| 129 | SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB, |
| 130 | SM5502_MUIC_ADC_AUDIO_VIDEO_CABLE, |
| 131 | SM5502_MUIC_ADC_TYPE2_CHARGER, |
| 132 | SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART, |
| 133 | SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART, |
| 134 | SM5502_MUIC_ADC_AUDIO_TYPE1, |
| 135 | SM5502_MUIC_ADC_OPEN = 0x1f, |
| 136 | |
| 137 | /* The below accessories have same ADC value (0x1f or 0x1e). |
| 138 | So, Device type1 is used to separate specific accessory. */ |
| 139 | /* |---------|--ADC| */ |
| 140 | /* | [7:5]|[4:0]| */ |
| 141 | SM5502_MUIC_ADC_AUDIO_TYPE1_FULL_REMOTE = 0x3e, /* | 001|11110| */ |
| 142 | SM5502_MUIC_ADC_AUDIO_TYPE1_SEND_END = 0x5e, /* | 010|11110| */ |
| 143 | /* |Dev Type1|--ADC| */ |
| 144 | SM5502_MUIC_ADC_OPEN_USB = 0x5f, /* | 010|11111| */ |
| 145 | SM5502_MUIC_ADC_OPEN_TA = 0xdf, /* | 110|11111| */ |
| 146 | SM5502_MUIC_ADC_OPEN_USB_OTG = 0xff, /* | 111|11111| */ |
| 147 | }; |
| 148 | |
| 149 | /* List of supported interrupt for SM5502 */ |
| 150 | static struct muic_irq sm5502_muic_irqs[] = { |
| 151 | { SM5502_IRQ_INT1_ATTACH, "muic-attach" }, |
| 152 | { SM5502_IRQ_INT1_DETACH, "muic-detach" }, |
| 153 | { SM5502_IRQ_INT1_KP, "muic-kp" }, |
| 154 | { SM5502_IRQ_INT1_LKP, "muic-lkp" }, |
| 155 | { SM5502_IRQ_INT1_LKR, "muic-lkr" }, |
| 156 | { SM5502_IRQ_INT1_OVP_EVENT, "muic-ovp-event" }, |
| 157 | { SM5502_IRQ_INT1_OCP_EVENT, "muic-ocp-event" }, |
| 158 | { SM5502_IRQ_INT1_OVP_OCP_DIS, "muic-ovp-ocp-dis" }, |
| 159 | { SM5502_IRQ_INT2_VBUS_DET, "muic-vbus-det" }, |
| 160 | { SM5502_IRQ_INT2_REV_ACCE, "muic-rev-acce" }, |
| 161 | { SM5502_IRQ_INT2_ADC_CHG, "muic-adc-chg" }, |
| 162 | { SM5502_IRQ_INT2_STUCK_KEY, "muic-stuck-key" }, |
| 163 | { SM5502_IRQ_INT2_STUCK_KEY_RCV, "muic-stuck-key-rcv" }, |
| 164 | { SM5502_IRQ_INT2_MHL, "muic-mhl" }, |
| 165 | }; |
| 166 | |
| 167 | /* Define interrupt list of SM5502 to register regmap_irq */ |
| 168 | static const struct regmap_irq sm5502_irqs[] = { |
| 169 | /* INT1 interrupts */ |
| 170 | { .reg_offset = 0, .mask = SM5502_IRQ_INT1_ATTACH_MASK, }, |
| 171 | { .reg_offset = 0, .mask = SM5502_IRQ_INT1_DETACH_MASK, }, |
| 172 | { .reg_offset = 0, .mask = SM5502_IRQ_INT1_KP_MASK, }, |
| 173 | { .reg_offset = 0, .mask = SM5502_IRQ_INT1_LKP_MASK, }, |
| 174 | { .reg_offset = 0, .mask = SM5502_IRQ_INT1_LKR_MASK, }, |
| 175 | { .reg_offset = 0, .mask = SM5502_IRQ_INT1_OVP_EVENT_MASK, }, |
| 176 | { .reg_offset = 0, .mask = SM5502_IRQ_INT1_OCP_EVENT_MASK, }, |
| 177 | { .reg_offset = 0, .mask = SM5502_IRQ_INT1_OVP_OCP_DIS_MASK, }, |
| 178 | |
| 179 | /* INT2 interrupts */ |
| 180 | { .reg_offset = 1, .mask = SM5502_IRQ_INT2_VBUS_DET_MASK,}, |
| 181 | { .reg_offset = 1, .mask = SM5502_IRQ_INT2_REV_ACCE_MASK, }, |
| 182 | { .reg_offset = 1, .mask = SM5502_IRQ_INT2_ADC_CHG_MASK, }, |
| 183 | { .reg_offset = 1, .mask = SM5502_IRQ_INT2_STUCK_KEY_MASK, }, |
| 184 | { .reg_offset = 1, .mask = SM5502_IRQ_INT2_STUCK_KEY_RCV_MASK, }, |
| 185 | { .reg_offset = 1, .mask = SM5502_IRQ_INT2_MHL_MASK, }, |
| 186 | }; |
| 187 | |
| 188 | static const struct regmap_irq_chip sm5502_muic_irq_chip = { |
| 189 | .name = "sm5502", |
| 190 | .status_base = SM5502_REG_INT1, |
| 191 | .mask_base = SM5502_REG_INTMASK1, |
| 192 | .mask_invert = false, |
| 193 | .num_regs = 2, |
| 194 | .irqs = sm5502_irqs, |
| 195 | .num_irqs = ARRAY_SIZE(sm5502_irqs), |
| 196 | }; |
| 197 | |
| 198 | /* Define regmap configuration of SM5502 for I2C communication */ |
| 199 | static bool sm5502_muic_volatile_reg(struct device *dev, unsigned int reg) |
| 200 | { |
| 201 | switch (reg) { |
| 202 | case SM5502_REG_INTMASK1: |
| 203 | case SM5502_REG_INTMASK2: |
| 204 | return true; |
| 205 | default: |
| 206 | break; |
| 207 | } |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | static const struct regmap_config sm5502_muic_regmap_config = { |
| 212 | .reg_bits = 8, |
| 213 | .val_bits = 8, |
| 214 | .volatile_reg = sm5502_muic_volatile_reg, |
| 215 | .max_register = SM5502_REG_END, |
| 216 | }; |
| 217 | |
Chanwoo Choi | a75fed2 | 2014-05-28 15:35:29 +0900 | [diff] [blame] | 218 | /* Change DM_CON/DP_CON/VBUSIN switch according to cable type */ |
| 219 | static int sm5502_muic_set_path(struct sm5502_muic_info *info, |
| 220 | unsigned int con_sw, unsigned int vbus_sw, |
| 221 | bool attached) |
| 222 | { |
| 223 | int ret; |
| 224 | |
| 225 | if (!attached) { |
| 226 | con_sw = DM_DP_SWITCH_OPEN; |
| 227 | vbus_sw = VBUSIN_SWITCH_OPEN; |
| 228 | } |
| 229 | |
| 230 | switch (con_sw) { |
| 231 | case DM_DP_SWITCH_OPEN: |
| 232 | case DM_DP_SWITCH_USB: |
| 233 | case DM_DP_SWITCH_AUDIO: |
| 234 | case DM_DP_SWITCH_UART: |
| 235 | ret = regmap_update_bits(info->regmap, SM5502_REG_MANUAL_SW1, |
| 236 | SM5502_REG_MANUAL_SW1_DP_MASK | |
| 237 | SM5502_REG_MANUAL_SW1_DM_MASK, |
| 238 | con_sw); |
| 239 | if (ret < 0) { |
| 240 | dev_err(info->dev, |
| 241 | "cannot update DM_CON/DP_CON switch\n"); |
| 242 | return ret; |
| 243 | } |
| 244 | break; |
| 245 | default: |
| 246 | dev_err(info->dev, "Unknown DM_CON/DP_CON switch type (%d)\n", |
| 247 | con_sw); |
| 248 | return -EINVAL; |
| 249 | }; |
| 250 | |
| 251 | switch (vbus_sw) { |
| 252 | case VBUSIN_SWITCH_OPEN: |
| 253 | case VBUSIN_SWITCH_VBUSOUT: |
| 254 | case VBUSIN_SWITCH_MIC: |
| 255 | case VBUSIN_SWITCH_VBUSOUT_WITH_USB: |
| 256 | ret = regmap_update_bits(info->regmap, SM5502_REG_MANUAL_SW1, |
| 257 | SM5502_REG_MANUAL_SW1_VBUSIN_MASK, |
| 258 | vbus_sw); |
| 259 | if (ret < 0) { |
| 260 | dev_err(info->dev, |
| 261 | "cannot update VBUSIN switch\n"); |
| 262 | return ret; |
| 263 | } |
| 264 | break; |
| 265 | default: |
| 266 | dev_err(info->dev, "Unknown VBUS switch type (%d)\n", vbus_sw); |
| 267 | return -EINVAL; |
| 268 | }; |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
Chanwoo Choi | 914b881 | 2014-05-22 14:06:33 +0900 | [diff] [blame] | 273 | /* Return cable type of attached or detached accessories */ |
| 274 | static unsigned int sm5502_muic_get_cable_type(struct sm5502_muic_info *info) |
| 275 | { |
| 276 | unsigned int cable_type = -1, adc, dev_type1; |
| 277 | int ret; |
| 278 | |
| 279 | /* Read ADC value according to external cable or button */ |
| 280 | ret = regmap_read(info->regmap, SM5502_REG_ADC, &adc); |
| 281 | if (ret) { |
| 282 | dev_err(info->dev, "failed to read ADC register\n"); |
| 283 | return ret; |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | * If ADC is SM5502_MUIC_ADC_GROUND(0x0), external cable hasn't |
| 288 | * connected with to MUIC device. |
| 289 | */ |
Chanwoo Choi | 0ccc795 | 2014-07-30 15:39:02 +0900 | [diff] [blame] | 290 | cable_type = adc & SM5502_REG_ADC_MASK; |
Chanwoo Choi | 914b881 | 2014-05-22 14:06:33 +0900 | [diff] [blame] | 291 | if (cable_type == SM5502_MUIC_ADC_GROUND) |
| 292 | return SM5502_MUIC_ADC_GROUND; |
| 293 | |
| 294 | switch (cable_type) { |
| 295 | case SM5502_MUIC_ADC_GROUND: |
| 296 | case SM5502_MUIC_ADC_SEND_END_BUTTON: |
| 297 | case SM5502_MUIC_ADC_REMOTE_S1_BUTTON: |
| 298 | case SM5502_MUIC_ADC_REMOTE_S2_BUTTON: |
| 299 | case SM5502_MUIC_ADC_REMOTE_S3_BUTTON: |
| 300 | case SM5502_MUIC_ADC_REMOTE_S4_BUTTON: |
| 301 | case SM5502_MUIC_ADC_REMOTE_S5_BUTTON: |
| 302 | case SM5502_MUIC_ADC_REMOTE_S6_BUTTON: |
| 303 | case SM5502_MUIC_ADC_REMOTE_S7_BUTTON: |
| 304 | case SM5502_MUIC_ADC_REMOTE_S8_BUTTON: |
| 305 | case SM5502_MUIC_ADC_REMOTE_S9_BUTTON: |
| 306 | case SM5502_MUIC_ADC_REMOTE_S10_BUTTON: |
| 307 | case SM5502_MUIC_ADC_REMOTE_S11_BUTTON: |
| 308 | case SM5502_MUIC_ADC_REMOTE_S12_BUTTON: |
| 309 | case SM5502_MUIC_ADC_RESERVED_ACC_1: |
| 310 | case SM5502_MUIC_ADC_RESERVED_ACC_2: |
| 311 | case SM5502_MUIC_ADC_RESERVED_ACC_3: |
| 312 | case SM5502_MUIC_ADC_RESERVED_ACC_4: |
| 313 | case SM5502_MUIC_ADC_RESERVED_ACC_5: |
| 314 | case SM5502_MUIC_ADC_AUDIO_TYPE2: |
| 315 | case SM5502_MUIC_ADC_PHONE_POWERED_DEV: |
| 316 | case SM5502_MUIC_ADC_TTY_CONVERTER: |
| 317 | case SM5502_MUIC_ADC_UART_CABLE: |
| 318 | case SM5502_MUIC_ADC_TYPE1_CHARGER: |
| 319 | case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB: |
| 320 | case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB: |
| 321 | case SM5502_MUIC_ADC_AUDIO_VIDEO_CABLE: |
| 322 | case SM5502_MUIC_ADC_TYPE2_CHARGER: |
| 323 | case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART: |
| 324 | case SM5502_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART: |
| 325 | break; |
| 326 | case SM5502_MUIC_ADC_AUDIO_TYPE1: |
| 327 | /* |
| 328 | * Check whether cable type is |
| 329 | * SM5502_MUIC_ADC_AUDIO_TYPE1_FULL_REMOTE |
| 330 | * or SM5502_MUIC_ADC_AUDIO_TYPE1_SEND_END |
| 331 | * by using Button event. |
| 332 | */ |
| 333 | break; |
| 334 | case SM5502_MUIC_ADC_OPEN: |
| 335 | ret = regmap_read(info->regmap, SM5502_REG_DEV_TYPE1, |
| 336 | &dev_type1); |
| 337 | if (ret) { |
| 338 | dev_err(info->dev, "failed to read DEV_TYPE1 reg\n"); |
| 339 | return ret; |
| 340 | } |
| 341 | |
| 342 | switch (dev_type1) { |
| 343 | case SM5502_REG_DEV_TYPE1_USB_SDP_MASK: |
| 344 | cable_type = SM5502_MUIC_ADC_OPEN_USB; |
| 345 | break; |
| 346 | case SM5502_REG_DEV_TYPE1_DEDICATED_CHG_MASK: |
| 347 | cable_type = SM5502_MUIC_ADC_OPEN_TA; |
| 348 | break; |
| 349 | case SM5502_REG_DEV_TYPE1_USB_OTG_MASK: |
| 350 | cable_type = SM5502_MUIC_ADC_OPEN_USB_OTG; |
| 351 | break; |
| 352 | default: |
| 353 | dev_dbg(info->dev, |
Chanwoo Choi | 34825e5 | 2015-03-07 01:41:36 +0900 | [diff] [blame] | 354 | "cannot identify the cable type: adc(0x%x)\n", |
| 355 | adc); |
Chanwoo Choi | 914b881 | 2014-05-22 14:06:33 +0900 | [diff] [blame] | 356 | return -EINVAL; |
| 357 | }; |
| 358 | break; |
| 359 | default: |
| 360 | dev_err(info->dev, |
| 361 | "failed to identify the cable type: adc(0x%x)\n", adc); |
| 362 | return -EINVAL; |
| 363 | }; |
| 364 | |
| 365 | return cable_type; |
| 366 | } |
| 367 | |
| 368 | static int sm5502_muic_cable_handler(struct sm5502_muic_info *info, |
| 369 | bool attached) |
| 370 | { |
| 371 | static unsigned int prev_cable_type = SM5502_MUIC_ADC_GROUND; |
Chanwoo Choi | 914b881 | 2014-05-22 14:06:33 +0900 | [diff] [blame] | 372 | unsigned int cable_type = SM5502_MUIC_ADC_GROUND; |
Chanwoo Choi | a75fed2 | 2014-05-28 15:35:29 +0900 | [diff] [blame] | 373 | unsigned int con_sw = DM_DP_SWITCH_OPEN; |
| 374 | unsigned int vbus_sw = VBUSIN_SWITCH_OPEN; |
Chanwoo Choi | 73b6ecd | 2015-06-12 11:10:06 +0900 | [diff] [blame] | 375 | unsigned int id; |
Chanwoo Choi | a75fed2 | 2014-05-28 15:35:29 +0900 | [diff] [blame] | 376 | int ret; |
Chanwoo Choi | 914b881 | 2014-05-22 14:06:33 +0900 | [diff] [blame] | 377 | |
Chanwoo Choi | 914b881 | 2014-05-22 14:06:33 +0900 | [diff] [blame] | 378 | /* Get the type of attached or detached cable */ |
| 379 | if (attached) |
| 380 | cable_type = sm5502_muic_get_cable_type(info); |
Chanwoo Choi | fbae30d | 2014-08-12 10:15:39 +0900 | [diff] [blame] | 381 | else |
Chanwoo Choi | 914b881 | 2014-05-22 14:06:33 +0900 | [diff] [blame] | 382 | cable_type = prev_cable_type; |
| 383 | prev_cable_type = cable_type; |
| 384 | |
| 385 | switch (cable_type) { |
| 386 | case SM5502_MUIC_ADC_OPEN_USB: |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 387 | id = EXTCON_USB; |
Chanwoo Choi | a75fed2 | 2014-05-28 15:35:29 +0900 | [diff] [blame] | 388 | con_sw = DM_DP_SWITCH_USB; |
| 389 | vbus_sw = VBUSIN_SWITCH_VBUSOUT_WITH_USB; |
Chanwoo Choi | 914b881 | 2014-05-22 14:06:33 +0900 | [diff] [blame] | 390 | break; |
| 391 | case SM5502_MUIC_ADC_OPEN_TA: |
Chanwoo Choi | 11eecf9 | 2015-10-03 14:15:13 +0900 | [diff] [blame] | 392 | id = EXTCON_CHG_USB_DCP; |
Chanwoo Choi | a75fed2 | 2014-05-28 15:35:29 +0900 | [diff] [blame] | 393 | con_sw = DM_DP_SWITCH_OPEN; |
| 394 | vbus_sw = VBUSIN_SWITCH_VBUSOUT; |
Chanwoo Choi | 914b881 | 2014-05-22 14:06:33 +0900 | [diff] [blame] | 395 | break; |
| 396 | case SM5502_MUIC_ADC_OPEN_USB_OTG: |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 397 | id = EXTCON_USB_HOST; |
Chanwoo Choi | a75fed2 | 2014-05-28 15:35:29 +0900 | [diff] [blame] | 398 | con_sw = DM_DP_SWITCH_USB; |
| 399 | vbus_sw = VBUSIN_SWITCH_OPEN; |
Chanwoo Choi | e195445 | 2014-05-28 14:21:55 +0900 | [diff] [blame] | 400 | break; |
Chanwoo Choi | 914b881 | 2014-05-22 14:06:33 +0900 | [diff] [blame] | 401 | default: |
| 402 | dev_dbg(info->dev, |
| 403 | "cannot handle this cable_type (0x%x)\n", cable_type); |
| 404 | return 0; |
| 405 | }; |
| 406 | |
Chanwoo Choi | a75fed2 | 2014-05-28 15:35:29 +0900 | [diff] [blame] | 407 | /* Change internal hardware path(DM_CON/DP_CON, VBUSIN) */ |
| 408 | ret = sm5502_muic_set_path(info, con_sw, vbus_sw, attached); |
| 409 | if (ret < 0) |
| 410 | return ret; |
| 411 | |
| 412 | /* Change the state of external accessory */ |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 413 | extcon_set_cable_state_(info->edev, id, attached); |
Chanwoo Choi | 914b881 | 2014-05-22 14:06:33 +0900 | [diff] [blame] | 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | static void sm5502_muic_irq_work(struct work_struct *work) |
| 419 | { |
| 420 | struct sm5502_muic_info *info = container_of(work, |
| 421 | struct sm5502_muic_info, irq_work); |
| 422 | int ret = 0; |
| 423 | |
| 424 | if (!info->edev) |
| 425 | return; |
| 426 | |
| 427 | mutex_lock(&info->mutex); |
| 428 | |
| 429 | /* Detect attached or detached cables */ |
| 430 | if (info->irq_attach) { |
| 431 | ret = sm5502_muic_cable_handler(info, true); |
| 432 | info->irq_attach = false; |
| 433 | } |
| 434 | if (info->irq_detach) { |
| 435 | ret = sm5502_muic_cable_handler(info, false); |
| 436 | info->irq_detach = false; |
| 437 | } |
| 438 | |
| 439 | if (ret < 0) |
| 440 | dev_err(info->dev, "failed to handle MUIC interrupt\n"); |
| 441 | |
| 442 | mutex_unlock(&info->mutex); |
Chanwoo Choi | 914b881 | 2014-05-22 14:06:33 +0900 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /* |
| 446 | * Sets irq_attach or irq_detach in sm5502_muic_info and returns 0. |
| 447 | * Returns -ESRCH if irq_type does not match registered IRQ for this dev type. |
| 448 | */ |
| 449 | static int sm5502_parse_irq(struct sm5502_muic_info *info, int irq_type) |
| 450 | { |
| 451 | switch (irq_type) { |
| 452 | case SM5502_IRQ_INT1_ATTACH: |
| 453 | info->irq_attach = true; |
| 454 | break; |
| 455 | case SM5502_IRQ_INT1_DETACH: |
| 456 | info->irq_detach = true; |
| 457 | break; |
| 458 | case SM5502_IRQ_INT1_KP: |
| 459 | case SM5502_IRQ_INT1_LKP: |
| 460 | case SM5502_IRQ_INT1_LKR: |
| 461 | case SM5502_IRQ_INT1_OVP_EVENT: |
| 462 | case SM5502_IRQ_INT1_OCP_EVENT: |
| 463 | case SM5502_IRQ_INT1_OVP_OCP_DIS: |
| 464 | case SM5502_IRQ_INT2_VBUS_DET: |
| 465 | case SM5502_IRQ_INT2_REV_ACCE: |
| 466 | case SM5502_IRQ_INT2_ADC_CHG: |
| 467 | case SM5502_IRQ_INT2_STUCK_KEY: |
| 468 | case SM5502_IRQ_INT2_STUCK_KEY_RCV: |
| 469 | case SM5502_IRQ_INT2_MHL: |
| 470 | default: |
| 471 | break; |
| 472 | } |
| 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | static irqreturn_t sm5502_muic_irq_handler(int irq, void *data) |
| 478 | { |
| 479 | struct sm5502_muic_info *info = data; |
| 480 | int i, irq_type = -1, ret; |
| 481 | |
| 482 | for (i = 0; i < info->num_muic_irqs; i++) |
| 483 | if (irq == info->muic_irqs[i].virq) |
| 484 | irq_type = info->muic_irqs[i].irq; |
| 485 | |
| 486 | ret = sm5502_parse_irq(info, irq_type); |
| 487 | if (ret < 0) { |
| 488 | dev_warn(info->dev, "cannot handle is interrupt:%d\n", |
| 489 | irq_type); |
| 490 | return IRQ_HANDLED; |
| 491 | } |
| 492 | schedule_work(&info->irq_work); |
| 493 | |
| 494 | return IRQ_HANDLED; |
| 495 | } |
| 496 | |
Chanwoo Choi | e195445 | 2014-05-28 14:21:55 +0900 | [diff] [blame] | 497 | static void sm5502_muic_detect_cable_wq(struct work_struct *work) |
| 498 | { |
| 499 | struct sm5502_muic_info *info = container_of(to_delayed_work(work), |
| 500 | struct sm5502_muic_info, wq_detcable); |
| 501 | int ret; |
| 502 | |
| 503 | /* Notify the state of connector cable or not */ |
| 504 | ret = sm5502_muic_cable_handler(info, true); |
| 505 | if (ret < 0) |
| 506 | dev_warn(info->dev, "failed to detect cable state\n"); |
| 507 | } |
| 508 | |
Chanwoo Choi | 914b881 | 2014-05-22 14:06:33 +0900 | [diff] [blame] | 509 | static void sm5502_init_dev_type(struct sm5502_muic_info *info) |
| 510 | { |
| 511 | unsigned int reg_data, vendor_id, version_id; |
| 512 | int i, ret; |
| 513 | |
| 514 | /* To test I2C, Print version_id and vendor_id of SM5502 */ |
| 515 | ret = regmap_read(info->regmap, SM5502_REG_DEVICE_ID, ®_data); |
| 516 | if (ret) { |
| 517 | dev_err(info->dev, |
| 518 | "failed to read DEVICE_ID register: %d\n", ret); |
| 519 | return; |
| 520 | } |
| 521 | |
| 522 | vendor_id = ((reg_data & SM5502_REG_DEVICE_ID_VENDOR_MASK) >> |
| 523 | SM5502_REG_DEVICE_ID_VENDOR_SHIFT); |
| 524 | version_id = ((reg_data & SM5502_REG_DEVICE_ID_VERSION_MASK) >> |
| 525 | SM5502_REG_DEVICE_ID_VERSION_SHIFT); |
| 526 | |
| 527 | dev_info(info->dev, "Device type: version: 0x%x, vendor: 0x%x\n", |
| 528 | version_id, vendor_id); |
| 529 | |
| 530 | /* Initiazle the register of SM5502 device to bring-up */ |
| 531 | for (i = 0; i < info->num_reg_data; i++) { |
| 532 | unsigned int val = 0; |
| 533 | |
| 534 | if (!info->reg_data[i].invert) |
| 535 | val |= ~info->reg_data[i].val; |
| 536 | else |
| 537 | val = info->reg_data[i].val; |
| 538 | regmap_write(info->regmap, info->reg_data[i].reg, val); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | static int sm5022_muic_i2c_probe(struct i2c_client *i2c, |
| 543 | const struct i2c_device_id *id) |
| 544 | { |
| 545 | struct device_node *np = i2c->dev.of_node; |
| 546 | struct sm5502_muic_info *info; |
| 547 | int i, ret, irq_flags; |
| 548 | |
| 549 | if (!np) |
| 550 | return -EINVAL; |
| 551 | |
| 552 | info = devm_kzalloc(&i2c->dev, sizeof(*info), GFP_KERNEL); |
| 553 | if (!info) |
| 554 | return -ENOMEM; |
| 555 | i2c_set_clientdata(i2c, info); |
| 556 | |
| 557 | info->dev = &i2c->dev; |
| 558 | info->i2c = i2c; |
| 559 | info->irq = i2c->irq; |
| 560 | info->muic_irqs = sm5502_muic_irqs; |
| 561 | info->num_muic_irqs = ARRAY_SIZE(sm5502_muic_irqs); |
| 562 | info->reg_data = sm5502_reg_data; |
| 563 | info->num_reg_data = ARRAY_SIZE(sm5502_reg_data); |
| 564 | |
| 565 | mutex_init(&info->mutex); |
| 566 | |
| 567 | INIT_WORK(&info->irq_work, sm5502_muic_irq_work); |
| 568 | |
| 569 | info->regmap = devm_regmap_init_i2c(i2c, &sm5502_muic_regmap_config); |
| 570 | if (IS_ERR(info->regmap)) { |
| 571 | ret = PTR_ERR(info->regmap); |
| 572 | dev_err(info->dev, "failed to allocate register map: %d\n", |
| 573 | ret); |
| 574 | return ret; |
| 575 | } |
| 576 | |
| 577 | /* Support irq domain for SM5502 MUIC device */ |
| 578 | irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_SHARED; |
| 579 | ret = regmap_add_irq_chip(info->regmap, info->irq, irq_flags, 0, |
| 580 | &sm5502_muic_irq_chip, &info->irq_data); |
| 581 | if (ret != 0) { |
| 582 | dev_err(info->dev, "failed to request IRQ %d: %d\n", |
| 583 | info->irq, ret); |
| 584 | return ret; |
| 585 | } |
| 586 | |
| 587 | for (i = 0; i < info->num_muic_irqs; i++) { |
| 588 | struct muic_irq *muic_irq = &info->muic_irqs[i]; |
Andrzej Hajda | 363b389 | 2015-09-24 16:00:21 +0200 | [diff] [blame] | 589 | int virq = 0; |
Chanwoo Choi | 914b881 | 2014-05-22 14:06:33 +0900 | [diff] [blame] | 590 | |
| 591 | virq = regmap_irq_get_virq(info->irq_data, muic_irq->irq); |
| 592 | if (virq <= 0) |
| 593 | return -EINVAL; |
| 594 | muic_irq->virq = virq; |
| 595 | |
| 596 | ret = devm_request_threaded_irq(info->dev, virq, NULL, |
| 597 | sm5502_muic_irq_handler, |
| 598 | IRQF_NO_SUSPEND, |
| 599 | muic_irq->name, info); |
| 600 | if (ret) { |
Chanwoo Choi | fbae30d | 2014-08-12 10:15:39 +0900 | [diff] [blame] | 601 | dev_err(info->dev, |
| 602 | "failed: irq request (IRQ: %d, error :%d)\n", |
| 603 | muic_irq->irq, ret); |
Chanwoo Choi | 914b881 | 2014-05-22 14:06:33 +0900 | [diff] [blame] | 604 | return ret; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | /* Allocate extcon device */ |
| 609 | info->edev = devm_extcon_dev_allocate(info->dev, sm5502_extcon_cable); |
| 610 | if (IS_ERR(info->edev)) { |
| 611 | dev_err(info->dev, "failed to allocate memory for extcon\n"); |
| 612 | return -ENOMEM; |
| 613 | } |
Chanwoo Choi | 914b881 | 2014-05-22 14:06:33 +0900 | [diff] [blame] | 614 | |
| 615 | /* Register extcon device */ |
| 616 | ret = devm_extcon_dev_register(info->dev, info->edev); |
| 617 | if (ret) { |
| 618 | dev_err(info->dev, "failed to register extcon device\n"); |
| 619 | return ret; |
| 620 | } |
| 621 | |
Chanwoo Choi | e195445 | 2014-05-28 14:21:55 +0900 | [diff] [blame] | 622 | /* |
| 623 | * Detect accessory after completing the initialization of platform |
| 624 | * |
| 625 | * - Use delayed workqueue to detect cable state and then |
| 626 | * notify cable state to notifiee/platform through uevent. |
| 627 | * After completing the booting of platform, the extcon provider |
| 628 | * driver should notify cable state to upper layer. |
| 629 | */ |
| 630 | INIT_DELAYED_WORK(&info->wq_detcable, sm5502_muic_detect_cable_wq); |
| 631 | queue_delayed_work(system_power_efficient_wq, &info->wq_detcable, |
| 632 | msecs_to_jiffies(DELAY_MS_DEFAULT)); |
| 633 | |
Chanwoo Choi | 914b881 | 2014-05-22 14:06:33 +0900 | [diff] [blame] | 634 | /* Initialize SM5502 device and print vendor id and version id */ |
| 635 | sm5502_init_dev_type(info); |
| 636 | |
| 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | static int sm5502_muic_i2c_remove(struct i2c_client *i2c) |
| 641 | { |
| 642 | struct sm5502_muic_info *info = i2c_get_clientdata(i2c); |
| 643 | |
| 644 | regmap_del_irq_chip(info->irq, info->irq_data); |
| 645 | |
| 646 | return 0; |
| 647 | } |
| 648 | |
Chanwoo Choi | 34825e5 | 2015-03-07 01:41:36 +0900 | [diff] [blame] | 649 | static const struct of_device_id sm5502_dt_match[] = { |
Chanwoo Choi | 914b881 | 2014-05-22 14:06:33 +0900 | [diff] [blame] | 650 | { .compatible = "siliconmitus,sm5502-muic" }, |
| 651 | { }, |
| 652 | }; |
Javier Martinez Canillas | ff612f9 | 2015-08-25 08:31:15 +0200 | [diff] [blame] | 653 | MODULE_DEVICE_TABLE(of, sm5502_dt_match); |
Chanwoo Choi | 914b881 | 2014-05-22 14:06:33 +0900 | [diff] [blame] | 654 | |
| 655 | #ifdef CONFIG_PM_SLEEP |
| 656 | static int sm5502_muic_suspend(struct device *dev) |
| 657 | { |
| 658 | struct i2c_client *i2c = container_of(dev, struct i2c_client, dev); |
| 659 | struct sm5502_muic_info *info = i2c_get_clientdata(i2c); |
| 660 | |
| 661 | enable_irq_wake(info->irq); |
| 662 | |
| 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | static int sm5502_muic_resume(struct device *dev) |
| 667 | { |
| 668 | struct i2c_client *i2c = container_of(dev, struct i2c_client, dev); |
| 669 | struct sm5502_muic_info *info = i2c_get_clientdata(i2c); |
| 670 | |
| 671 | disable_irq_wake(info->irq); |
| 672 | |
| 673 | return 0; |
| 674 | } |
| 675 | #endif |
| 676 | |
| 677 | static SIMPLE_DEV_PM_OPS(sm5502_muic_pm_ops, |
| 678 | sm5502_muic_suspend, sm5502_muic_resume); |
| 679 | |
| 680 | static const struct i2c_device_id sm5502_i2c_id[] = { |
| 681 | { "sm5502", TYPE_SM5502 }, |
| 682 | { } |
| 683 | }; |
| 684 | MODULE_DEVICE_TABLE(i2c, sm5502_i2c_id); |
| 685 | |
| 686 | static struct i2c_driver sm5502_muic_i2c_driver = { |
| 687 | .driver = { |
| 688 | .name = "sm5502", |
Chanwoo Choi | 914b881 | 2014-05-22 14:06:33 +0900 | [diff] [blame] | 689 | .pm = &sm5502_muic_pm_ops, |
| 690 | .of_match_table = sm5502_dt_match, |
| 691 | }, |
| 692 | .probe = sm5022_muic_i2c_probe, |
| 693 | .remove = sm5502_muic_i2c_remove, |
| 694 | .id_table = sm5502_i2c_id, |
| 695 | }; |
| 696 | |
| 697 | static int __init sm5502_muic_i2c_init(void) |
| 698 | { |
| 699 | return i2c_add_driver(&sm5502_muic_i2c_driver); |
| 700 | } |
| 701 | subsys_initcall(sm5502_muic_i2c_init); |
| 702 | |
| 703 | MODULE_DESCRIPTION("Silicon Mitus SM5502 Extcon driver"); |
| 704 | MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>"); |
| 705 | MODULE_LICENSE("GPL"); |