Chanwoo Choi | c03e017 | 2014-08-18 09:05:21 +0900 | [diff] [blame] | 1 | /* |
| 2 | * extcon-rt8973a.c - Richtek RT8973A extcon driver 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. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/err.h> |
| 14 | #include <linux/i2c.h> |
| 15 | #include <linux/input.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/irqdomain.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/regmap.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/extcon.h> |
| 24 | |
| 25 | #include "extcon-rt8973a.h" |
| 26 | |
| 27 | #define DELAY_MS_DEFAULT 20000 /* unit: millisecond */ |
| 28 | |
| 29 | struct muic_irq { |
| 30 | unsigned int irq; |
| 31 | const char *name; |
| 32 | unsigned int virq; |
| 33 | }; |
| 34 | |
| 35 | struct reg_data { |
| 36 | u8 reg; |
| 37 | u8 mask; |
| 38 | u8 val; |
| 39 | bool invert; |
| 40 | }; |
| 41 | |
| 42 | struct rt8973a_muic_info { |
| 43 | struct device *dev; |
| 44 | struct extcon_dev *edev; |
| 45 | |
| 46 | struct i2c_client *i2c; |
| 47 | struct regmap *regmap; |
| 48 | |
| 49 | struct regmap_irq_chip_data *irq_data; |
| 50 | struct muic_irq *muic_irqs; |
| 51 | unsigned int num_muic_irqs; |
| 52 | int irq; |
| 53 | bool irq_attach; |
| 54 | bool irq_detach; |
| 55 | bool irq_ovp; |
| 56 | bool irq_otp; |
| 57 | struct work_struct irq_work; |
| 58 | |
| 59 | struct reg_data *reg_data; |
| 60 | unsigned int num_reg_data; |
| 61 | bool auto_config; |
| 62 | |
| 63 | struct mutex mutex; |
| 64 | |
| 65 | /* |
| 66 | * Use delayed workqueue to detect cable state and then |
| 67 | * notify cable state to notifiee/platform through uevent. |
| 68 | * After completing the booting of platform, the extcon provider |
| 69 | * driver should notify cable state to upper layer. |
| 70 | */ |
| 71 | struct delayed_work wq_detcable; |
| 72 | }; |
| 73 | |
| 74 | /* Default value of RT8973A register to bring up MUIC device. */ |
| 75 | static struct reg_data rt8973a_reg_data[] = { |
| 76 | { |
| 77 | .reg = RT8973A_REG_CONTROL1, |
| 78 | .mask = RT8973A_REG_CONTROL1_ADC_EN_MASK |
| 79 | | RT8973A_REG_CONTROL1_USB_CHD_EN_MASK |
| 80 | | RT8973A_REG_CONTROL1_CHGTYP_MASK |
| 81 | | RT8973A_REG_CONTROL1_SWITCH_OPEN_MASK |
| 82 | | RT8973A_REG_CONTROL1_AUTO_CONFIG_MASK |
| 83 | | RT8973A_REG_CONTROL1_INTM_MASK, |
| 84 | .val = RT8973A_REG_CONTROL1_ADC_EN_MASK |
| 85 | | RT8973A_REG_CONTROL1_USB_CHD_EN_MASK |
| 86 | | RT8973A_REG_CONTROL1_CHGTYP_MASK, |
| 87 | .invert = false, |
| 88 | }, |
| 89 | { /* sentinel */ } |
| 90 | }; |
| 91 | |
| 92 | /* List of detectable cables */ |
Chanwoo Choi | 73b6ecd | 2015-06-12 11:10:06 +0900 | [diff] [blame] | 93 | static const unsigned int rt8973a_extcon_cable[] = { |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 94 | EXTCON_USB, |
| 95 | EXTCON_USB_HOST, |
Chanwoo Choi | 11eecf9 | 2015-10-03 14:15:13 +0900 | [diff] [blame] | 96 | EXTCON_CHG_USB_DCP, |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 97 | EXTCON_JIG, |
| 98 | EXTCON_NONE, |
Chanwoo Choi | c03e017 | 2014-08-18 09:05:21 +0900 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | /* Define OVP (Over Voltage Protection), OTP (Over Temperature Protection) */ |
| 102 | enum rt8973a_event_type { |
| 103 | RT8973A_EVENT_ATTACH = 1, |
| 104 | RT8973A_EVENT_DETACH, |
| 105 | RT8973A_EVENT_OVP, |
| 106 | RT8973A_EVENT_OTP, |
| 107 | }; |
| 108 | |
| 109 | /* Define supported accessory type */ |
| 110 | enum rt8973a_muic_acc_type { |
| 111 | RT8973A_MUIC_ADC_OTG = 0x0, |
| 112 | RT8973A_MUIC_ADC_AUDIO_SEND_END_BUTTON, |
| 113 | RT8973A_MUIC_ADC_AUDIO_REMOTE_S1_BUTTON, |
| 114 | RT8973A_MUIC_ADC_AUDIO_REMOTE_S2_BUTTON, |
| 115 | RT8973A_MUIC_ADC_AUDIO_REMOTE_S3_BUTTON, |
| 116 | RT8973A_MUIC_ADC_AUDIO_REMOTE_S4_BUTTON, |
| 117 | RT8973A_MUIC_ADC_AUDIO_REMOTE_S5_BUTTON, |
| 118 | RT8973A_MUIC_ADC_AUDIO_REMOTE_S6_BUTTON, |
| 119 | RT8973A_MUIC_ADC_AUDIO_REMOTE_S7_BUTTON, |
| 120 | RT8973A_MUIC_ADC_AUDIO_REMOTE_S8_BUTTON, |
| 121 | RT8973A_MUIC_ADC_AUDIO_REMOTE_S9_BUTTON, |
| 122 | RT8973A_MUIC_ADC_AUDIO_REMOTE_S10_BUTTON, |
| 123 | RT8973A_MUIC_ADC_AUDIO_REMOTE_S11_BUTTON, |
| 124 | RT8973A_MUIC_ADC_AUDIO_REMOTE_S12_BUTTON, |
| 125 | RT8973A_MUIC_ADC_RESERVED_ACC_1, |
| 126 | RT8973A_MUIC_ADC_RESERVED_ACC_2, |
| 127 | RT8973A_MUIC_ADC_RESERVED_ACC_3, |
| 128 | RT8973A_MUIC_ADC_RESERVED_ACC_4, |
| 129 | RT8973A_MUIC_ADC_RESERVED_ACC_5, |
| 130 | RT8973A_MUIC_ADC_AUDIO_TYPE2, |
| 131 | RT8973A_MUIC_ADC_PHONE_POWERED_DEV, |
| 132 | RT8973A_MUIC_ADC_UNKNOWN_ACC_1, |
| 133 | RT8973A_MUIC_ADC_UNKNOWN_ACC_2, |
| 134 | RT8973A_MUIC_ADC_TA, |
| 135 | RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB, |
| 136 | RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB, |
| 137 | RT8973A_MUIC_ADC_UNKNOWN_ACC_3, |
| 138 | RT8973A_MUIC_ADC_UNKNOWN_ACC_4, |
| 139 | RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART, |
| 140 | RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART, |
| 141 | RT8973A_MUIC_ADC_UNKNOWN_ACC_5, |
| 142 | RT8973A_MUIC_ADC_OPEN = 0x1f, |
| 143 | |
| 144 | /* The below accessories has same ADC value (0x1f). |
| 145 | So, Device type1 is used to separate specific accessory. */ |
| 146 | /* |---------|--ADC| */ |
| 147 | /* | [7:5]|[4:0]| */ |
| 148 | RT8973A_MUIC_ADC_USB = 0x3f, /* | 001|11111| */ |
| 149 | }; |
| 150 | |
| 151 | /* List of supported interrupt for RT8973A */ |
| 152 | static struct muic_irq rt8973a_muic_irqs[] = { |
| 153 | { RT8973A_INT1_ATTACH, "muic-attach" }, |
| 154 | { RT8973A_INT1_DETACH, "muic-detach" }, |
| 155 | { RT8973A_INT1_CHGDET, "muic-chgdet" }, |
| 156 | { RT8973A_INT1_DCD_T, "muic-dcd-t" }, |
| 157 | { RT8973A_INT1_OVP, "muic-ovp" }, |
| 158 | { RT8973A_INT1_CONNECT, "muic-connect" }, |
| 159 | { RT8973A_INT1_ADC_CHG, "muic-adc-chg" }, |
| 160 | { RT8973A_INT1_OTP, "muic-otp" }, |
| 161 | { RT8973A_INT2_UVLO, "muic-uvlo" }, |
| 162 | { RT8973A_INT2_POR, "muic-por" }, |
| 163 | { RT8973A_INT2_OTP_FET, "muic-otp-fet" }, |
| 164 | { RT8973A_INT2_OVP_FET, "muic-ovp-fet" }, |
| 165 | { RT8973A_INT2_OCP_LATCH, "muic-ocp-latch" }, |
| 166 | { RT8973A_INT2_OCP, "muic-ocp" }, |
| 167 | { RT8973A_INT2_OVP_OCP, "muic-ovp-ocp" }, |
| 168 | }; |
| 169 | |
| 170 | /* Define interrupt list of RT8973A to register regmap_irq */ |
| 171 | static const struct regmap_irq rt8973a_irqs[] = { |
| 172 | /* INT1 interrupts */ |
| 173 | { .reg_offset = 0, .mask = RT8973A_INT1_ATTACH_MASK, }, |
| 174 | { .reg_offset = 0, .mask = RT8973A_INT1_DETACH_MASK, }, |
| 175 | { .reg_offset = 0, .mask = RT8973A_INT1_CHGDET_MASK, }, |
| 176 | { .reg_offset = 0, .mask = RT8973A_INT1_DCD_T_MASK, }, |
| 177 | { .reg_offset = 0, .mask = RT8973A_INT1_OVP_MASK, }, |
| 178 | { .reg_offset = 0, .mask = RT8973A_INT1_CONNECT_MASK, }, |
| 179 | { .reg_offset = 0, .mask = RT8973A_INT1_ADC_CHG_MASK, }, |
| 180 | { .reg_offset = 0, .mask = RT8973A_INT1_OTP_MASK, }, |
| 181 | |
| 182 | /* INT2 interrupts */ |
| 183 | { .reg_offset = 1, .mask = RT8973A_INT2_UVLOT_MASK,}, |
| 184 | { .reg_offset = 1, .mask = RT8973A_INT2_POR_MASK, }, |
| 185 | { .reg_offset = 1, .mask = RT8973A_INT2_OTP_FET_MASK, }, |
| 186 | { .reg_offset = 1, .mask = RT8973A_INT2_OVP_FET_MASK, }, |
| 187 | { .reg_offset = 1, .mask = RT8973A_INT2_OCP_LATCH_MASK, }, |
| 188 | { .reg_offset = 1, .mask = RT8973A_INT2_OCP_MASK, }, |
| 189 | { .reg_offset = 1, .mask = RT8973A_INT2_OVP_OCP_MASK, }, |
| 190 | }; |
| 191 | |
| 192 | static const struct regmap_irq_chip rt8973a_muic_irq_chip = { |
| 193 | .name = "rt8973a", |
| 194 | .status_base = RT8973A_REG_INT1, |
| 195 | .mask_base = RT8973A_REG_INTM1, |
| 196 | .mask_invert = false, |
| 197 | .num_regs = 2, |
| 198 | .irqs = rt8973a_irqs, |
| 199 | .num_irqs = ARRAY_SIZE(rt8973a_irqs), |
| 200 | }; |
| 201 | |
| 202 | /* Define regmap configuration of RT8973A for I2C communication */ |
| 203 | static bool rt8973a_muic_volatile_reg(struct device *dev, unsigned int reg) |
| 204 | { |
| 205 | switch (reg) { |
| 206 | case RT8973A_REG_INTM1: |
| 207 | case RT8973A_REG_INTM2: |
| 208 | return true; |
| 209 | default: |
| 210 | break; |
| 211 | } |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | static const struct regmap_config rt8973a_muic_regmap_config = { |
| 216 | .reg_bits = 8, |
| 217 | .val_bits = 8, |
| 218 | .volatile_reg = rt8973a_muic_volatile_reg, |
| 219 | .max_register = RT8973A_REG_END, |
| 220 | }; |
| 221 | |
| 222 | /* Change DM_CON/DP_CON/VBUSIN switch according to cable type */ |
| 223 | static int rt8973a_muic_set_path(struct rt8973a_muic_info *info, |
| 224 | unsigned int con_sw, bool attached) |
| 225 | { |
| 226 | int ret; |
| 227 | |
| 228 | /* |
| 229 | * Don't need to set h/w path according to cable type |
| 230 | * if Auto-configuration mode of CONTROL1 register is true. |
| 231 | */ |
| 232 | if (info->auto_config) |
| 233 | return 0; |
| 234 | |
| 235 | if (!attached) |
| 236 | con_sw = DM_DP_SWITCH_UART; |
| 237 | |
| 238 | switch (con_sw) { |
| 239 | case DM_DP_SWITCH_OPEN: |
| 240 | case DM_DP_SWITCH_USB: |
| 241 | case DM_DP_SWITCH_UART: |
| 242 | ret = regmap_update_bits(info->regmap, RT8973A_REG_MANUAL_SW1, |
| 243 | RT8973A_REG_MANUAL_SW1_DP_MASK | |
| 244 | RT8973A_REG_MANUAL_SW1_DM_MASK, |
| 245 | con_sw); |
| 246 | if (ret < 0) { |
| 247 | dev_err(info->dev, |
| 248 | "cannot update DM_CON/DP_CON switch\n"); |
| 249 | return ret; |
| 250 | } |
| 251 | break; |
| 252 | default: |
| 253 | dev_err(info->dev, "Unknown DM_CON/DP_CON switch type (%d)\n", |
| 254 | con_sw); |
| 255 | return -EINVAL; |
| 256 | } |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | static int rt8973a_muic_get_cable_type(struct rt8973a_muic_info *info) |
| 262 | { |
| 263 | unsigned int adc, dev1; |
| 264 | int ret, cable_type; |
| 265 | |
| 266 | /* Read ADC value according to external cable or button */ |
| 267 | ret = regmap_read(info->regmap, RT8973A_REG_ADC, &adc); |
| 268 | if (ret) { |
| 269 | dev_err(info->dev, "failed to read ADC register\n"); |
| 270 | return ret; |
| 271 | } |
| 272 | cable_type = adc & RT8973A_REG_ADC_MASK; |
| 273 | |
| 274 | /* Read Device 1 reigster to identify correct cable type */ |
| 275 | ret = regmap_read(info->regmap, RT8973A_REG_DEV1, &dev1); |
| 276 | if (ret) { |
| 277 | dev_err(info->dev, "failed to read DEV1 register\n"); |
| 278 | return ret; |
| 279 | } |
| 280 | |
| 281 | switch (adc) { |
| 282 | case RT8973A_MUIC_ADC_OPEN: |
| 283 | if (dev1 & RT8973A_REG_DEV1_USB_MASK) |
| 284 | cable_type = RT8973A_MUIC_ADC_USB; |
| 285 | else if (dev1 & RT8973A_REG_DEV1_DCPORT_MASK) |
| 286 | cable_type = RT8973A_MUIC_ADC_TA; |
| 287 | else |
| 288 | cable_type = RT8973A_MUIC_ADC_OPEN; |
| 289 | break; |
| 290 | default: |
| 291 | break; |
| 292 | } |
| 293 | |
| 294 | return cable_type; |
| 295 | } |
| 296 | |
| 297 | static int rt8973a_muic_cable_handler(struct rt8973a_muic_info *info, |
| 298 | enum rt8973a_event_type event) |
| 299 | { |
| 300 | static unsigned int prev_cable_type; |
Chanwoo Choi | c03e017 | 2014-08-18 09:05:21 +0900 | [diff] [blame] | 301 | unsigned int con_sw = DM_DP_SWITCH_UART; |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 302 | int ret, cable_type; |
Chanwoo Choi | 73b6ecd | 2015-06-12 11:10:06 +0900 | [diff] [blame] | 303 | unsigned int id; |
Chanwoo Choi | c03e017 | 2014-08-18 09:05:21 +0900 | [diff] [blame] | 304 | bool attached = false; |
| 305 | |
Chanwoo Choi | c03e017 | 2014-08-18 09:05:21 +0900 | [diff] [blame] | 306 | switch (event) { |
| 307 | case RT8973A_EVENT_ATTACH: |
| 308 | cable_type = rt8973a_muic_get_cable_type(info); |
| 309 | attached = true; |
| 310 | break; |
| 311 | case RT8973A_EVENT_DETACH: |
| 312 | cable_type = prev_cable_type; |
| 313 | attached = false; |
| 314 | break; |
| 315 | case RT8973A_EVENT_OVP: |
| 316 | case RT8973A_EVENT_OTP: |
| 317 | dev_warn(info->dev, |
| 318 | "happen Over %s issue. Need to disconnect all cables\n", |
| 319 | event == RT8973A_EVENT_OVP ? "Voltage" : "Temperature"); |
| 320 | cable_type = prev_cable_type; |
| 321 | attached = false; |
| 322 | break; |
| 323 | default: |
| 324 | dev_err(info->dev, |
| 325 | "Cannot handle this event (event:%d)\n", event); |
| 326 | return -EINVAL; |
| 327 | } |
| 328 | prev_cable_type = cable_type; |
| 329 | |
| 330 | switch (cable_type) { |
| 331 | case RT8973A_MUIC_ADC_OTG: |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 332 | id = EXTCON_USB_HOST; |
Chanwoo Choi | c03e017 | 2014-08-18 09:05:21 +0900 | [diff] [blame] | 333 | con_sw = DM_DP_SWITCH_USB; |
| 334 | break; |
| 335 | case RT8973A_MUIC_ADC_TA: |
Chanwoo Choi | 11eecf9 | 2015-10-03 14:15:13 +0900 | [diff] [blame] | 336 | id = EXTCON_CHG_USB_DCP; |
Chanwoo Choi | c03e017 | 2014-08-18 09:05:21 +0900 | [diff] [blame] | 337 | con_sw = DM_DP_SWITCH_OPEN; |
| 338 | break; |
| 339 | case RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_OFF_USB: |
Chanwoo Choi | c03e017 | 2014-08-18 09:05:21 +0900 | [diff] [blame] | 340 | case RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_ON_USB: |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 341 | id = EXTCON_JIG; |
Chanwoo Choi | 41b3c01 | 2015-04-24 19:50:48 +0900 | [diff] [blame] | 342 | con_sw = DM_DP_SWITCH_USB; |
Chanwoo Choi | c03e017 | 2014-08-18 09:05:21 +0900 | [diff] [blame] | 343 | break; |
| 344 | case RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_OFF_UART: |
Chanwoo Choi | c03e017 | 2014-08-18 09:05:21 +0900 | [diff] [blame] | 345 | case RT8973A_MUIC_ADC_FACTORY_MODE_BOOT_ON_UART: |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 346 | id = EXTCON_JIG; |
Chanwoo Choi | c03e017 | 2014-08-18 09:05:21 +0900 | [diff] [blame] | 347 | con_sw = DM_DP_SWITCH_UART; |
| 348 | break; |
| 349 | case RT8973A_MUIC_ADC_USB: |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 350 | id = EXTCON_USB; |
Chanwoo Choi | c03e017 | 2014-08-18 09:05:21 +0900 | [diff] [blame] | 351 | con_sw = DM_DP_SWITCH_USB; |
| 352 | break; |
| 353 | case RT8973A_MUIC_ADC_OPEN: |
| 354 | return 0; |
| 355 | case RT8973A_MUIC_ADC_UNKNOWN_ACC_1: |
| 356 | case RT8973A_MUIC_ADC_UNKNOWN_ACC_2: |
| 357 | case RT8973A_MUIC_ADC_UNKNOWN_ACC_3: |
| 358 | case RT8973A_MUIC_ADC_UNKNOWN_ACC_4: |
| 359 | case RT8973A_MUIC_ADC_UNKNOWN_ACC_5: |
| 360 | dev_warn(info->dev, |
| 361 | "Unknown accessory type (adc:0x%x)\n", cable_type); |
| 362 | return 0; |
| 363 | case RT8973A_MUIC_ADC_AUDIO_SEND_END_BUTTON: |
| 364 | case RT8973A_MUIC_ADC_AUDIO_REMOTE_S1_BUTTON: |
| 365 | case RT8973A_MUIC_ADC_AUDIO_REMOTE_S2_BUTTON: |
| 366 | case RT8973A_MUIC_ADC_AUDIO_REMOTE_S3_BUTTON: |
| 367 | case RT8973A_MUIC_ADC_AUDIO_REMOTE_S4_BUTTON: |
| 368 | case RT8973A_MUIC_ADC_AUDIO_REMOTE_S5_BUTTON: |
| 369 | case RT8973A_MUIC_ADC_AUDIO_REMOTE_S6_BUTTON: |
| 370 | case RT8973A_MUIC_ADC_AUDIO_REMOTE_S7_BUTTON: |
| 371 | case RT8973A_MUIC_ADC_AUDIO_REMOTE_S8_BUTTON: |
| 372 | case RT8973A_MUIC_ADC_AUDIO_REMOTE_S9_BUTTON: |
| 373 | case RT8973A_MUIC_ADC_AUDIO_REMOTE_S10_BUTTON: |
| 374 | case RT8973A_MUIC_ADC_AUDIO_REMOTE_S11_BUTTON: |
| 375 | case RT8973A_MUIC_ADC_AUDIO_REMOTE_S12_BUTTON: |
| 376 | case RT8973A_MUIC_ADC_AUDIO_TYPE2: |
| 377 | dev_warn(info->dev, |
| 378 | "Audio device/button type (adc:0x%x)\n", cable_type); |
| 379 | return 0; |
| 380 | case RT8973A_MUIC_ADC_RESERVED_ACC_1: |
| 381 | case RT8973A_MUIC_ADC_RESERVED_ACC_2: |
| 382 | case RT8973A_MUIC_ADC_RESERVED_ACC_3: |
| 383 | case RT8973A_MUIC_ADC_RESERVED_ACC_4: |
| 384 | case RT8973A_MUIC_ADC_RESERVED_ACC_5: |
| 385 | case RT8973A_MUIC_ADC_PHONE_POWERED_DEV: |
| 386 | return 0; |
| 387 | default: |
| 388 | dev_err(info->dev, |
| 389 | "Cannot handle this cable_type (adc:0x%x)\n", |
| 390 | cable_type); |
| 391 | return -EINVAL; |
| 392 | } |
| 393 | |
| 394 | /* Change internal hardware path(DM_CON/DP_CON) */ |
| 395 | ret = rt8973a_muic_set_path(info, con_sw, attached); |
| 396 | if (ret < 0) |
| 397 | return ret; |
| 398 | |
| 399 | /* Change the state of external accessory */ |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 400 | extcon_set_cable_state_(info->edev, id, attached); |
Chanwoo Choi | c03e017 | 2014-08-18 09:05:21 +0900 | [diff] [blame] | 401 | |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | static void rt8973a_muic_irq_work(struct work_struct *work) |
| 406 | { |
| 407 | struct rt8973a_muic_info *info = container_of(work, |
| 408 | struct rt8973a_muic_info, irq_work); |
| 409 | int ret = 0; |
| 410 | |
| 411 | if (!info->edev) |
| 412 | return; |
| 413 | |
| 414 | mutex_lock(&info->mutex); |
| 415 | |
| 416 | /* Detect attached or detached cables */ |
| 417 | if (info->irq_attach) { |
| 418 | ret = rt8973a_muic_cable_handler(info, RT8973A_EVENT_ATTACH); |
| 419 | info->irq_attach = false; |
| 420 | } |
| 421 | |
| 422 | if (info->irq_detach) { |
| 423 | ret = rt8973a_muic_cable_handler(info, RT8973A_EVENT_DETACH); |
| 424 | info->irq_detach = false; |
| 425 | } |
| 426 | |
| 427 | if (info->irq_ovp) { |
| 428 | ret = rt8973a_muic_cable_handler(info, RT8973A_EVENT_OVP); |
| 429 | info->irq_ovp = false; |
| 430 | } |
| 431 | |
| 432 | if (info->irq_otp) { |
| 433 | ret = rt8973a_muic_cable_handler(info, RT8973A_EVENT_OTP); |
| 434 | info->irq_otp = false; |
| 435 | } |
| 436 | |
| 437 | if (ret < 0) |
| 438 | dev_err(info->dev, "failed to handle MUIC interrupt\n"); |
| 439 | |
| 440 | mutex_unlock(&info->mutex); |
| 441 | } |
| 442 | |
| 443 | static irqreturn_t rt8973a_muic_irq_handler(int irq, void *data) |
| 444 | { |
| 445 | struct rt8973a_muic_info *info = data; |
| 446 | int i, irq_type = -1; |
| 447 | |
| 448 | for (i = 0; i < info->num_muic_irqs; i++) |
| 449 | if (irq == info->muic_irqs[i].virq) |
| 450 | irq_type = info->muic_irqs[i].irq; |
| 451 | |
| 452 | switch (irq_type) { |
| 453 | case RT8973A_INT1_ATTACH: |
| 454 | info->irq_attach = true; |
| 455 | break; |
| 456 | case RT8973A_INT1_DETACH: |
| 457 | info->irq_detach = true; |
| 458 | break; |
| 459 | case RT8973A_INT1_OVP: |
| 460 | info->irq_ovp = true; |
| 461 | break; |
| 462 | case RT8973A_INT1_OTP: |
| 463 | info->irq_otp = true; |
| 464 | break; |
| 465 | case RT8973A_INT1_CHGDET: |
| 466 | case RT8973A_INT1_DCD_T: |
| 467 | case RT8973A_INT1_CONNECT: |
| 468 | case RT8973A_INT1_ADC_CHG: |
| 469 | case RT8973A_INT2_UVLO: |
| 470 | case RT8973A_INT2_POR: |
| 471 | case RT8973A_INT2_OTP_FET: |
| 472 | case RT8973A_INT2_OVP_FET: |
| 473 | case RT8973A_INT2_OCP_LATCH: |
| 474 | case RT8973A_INT2_OCP: |
| 475 | case RT8973A_INT2_OVP_OCP: |
| 476 | default: |
| 477 | dev_dbg(info->dev, |
| 478 | "Cannot handle this interrupt (%d)\n", irq_type); |
| 479 | break; |
| 480 | } |
| 481 | |
| 482 | schedule_work(&info->irq_work); |
| 483 | |
| 484 | return IRQ_HANDLED; |
| 485 | } |
| 486 | |
| 487 | static void rt8973a_muic_detect_cable_wq(struct work_struct *work) |
| 488 | { |
| 489 | struct rt8973a_muic_info *info = container_of(to_delayed_work(work), |
| 490 | struct rt8973a_muic_info, wq_detcable); |
| 491 | int ret; |
| 492 | |
| 493 | /* Notify the state of connector cable or not */ |
| 494 | ret = rt8973a_muic_cable_handler(info, RT8973A_EVENT_ATTACH); |
| 495 | if (ret < 0) |
| 496 | dev_warn(info->dev, "failed to detect cable state\n"); |
| 497 | } |
| 498 | |
| 499 | static void rt8973a_init_dev_type(struct rt8973a_muic_info *info) |
| 500 | { |
| 501 | unsigned int data, vendor_id, version_id; |
| 502 | int i, ret; |
| 503 | |
| 504 | /* To test I2C, Print version_id and vendor_id of RT8973A */ |
| 505 | ret = regmap_read(info->regmap, RT8973A_REG_DEVICE_ID, &data); |
| 506 | if (ret) { |
| 507 | dev_err(info->dev, |
| 508 | "failed to read DEVICE_ID register: %d\n", ret); |
| 509 | return; |
| 510 | } |
| 511 | |
| 512 | vendor_id = ((data & RT8973A_REG_DEVICE_ID_VENDOR_MASK) >> |
| 513 | RT8973A_REG_DEVICE_ID_VENDOR_SHIFT); |
| 514 | version_id = ((data & RT8973A_REG_DEVICE_ID_VERSION_MASK) >> |
| 515 | RT8973A_REG_DEVICE_ID_VERSION_SHIFT); |
| 516 | |
| 517 | dev_info(info->dev, "Device type: version: 0x%x, vendor: 0x%x\n", |
| 518 | version_id, vendor_id); |
| 519 | |
| 520 | /* Initiazle the register of RT8973A device to bring-up */ |
| 521 | for (i = 0; i < info->num_reg_data; i++) { |
| 522 | u8 reg = info->reg_data[i].reg; |
| 523 | u8 mask = info->reg_data[i].mask; |
| 524 | u8 val = 0; |
| 525 | |
| 526 | if (info->reg_data[i].invert) |
| 527 | val = ~info->reg_data[i].val; |
| 528 | else |
| 529 | val = info->reg_data[i].val; |
| 530 | |
| 531 | regmap_update_bits(info->regmap, reg, mask, val); |
| 532 | } |
| 533 | |
| 534 | /* Check whether RT8973A is auto swithcing mode or not */ |
| 535 | ret = regmap_read(info->regmap, RT8973A_REG_CONTROL1, &data); |
| 536 | if (ret) { |
| 537 | dev_err(info->dev, |
| 538 | "failed to read CONTROL1 register: %d\n", ret); |
| 539 | return; |
| 540 | } |
| 541 | |
| 542 | data &= RT8973A_REG_CONTROL1_AUTO_CONFIG_MASK; |
| 543 | if (data) { |
| 544 | info->auto_config = true; |
| 545 | dev_info(info->dev, |
| 546 | "Enable Auto-configuration for internal path\n"); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | static int rt8973a_muic_i2c_probe(struct i2c_client *i2c, |
| 551 | const struct i2c_device_id *id) |
| 552 | { |
| 553 | struct device_node *np = i2c->dev.of_node; |
| 554 | struct rt8973a_muic_info *info; |
| 555 | int i, ret, irq_flags; |
| 556 | |
| 557 | if (!np) |
| 558 | return -EINVAL; |
| 559 | |
| 560 | info = devm_kzalloc(&i2c->dev, sizeof(*info), GFP_KERNEL); |
Chanwoo Choi | 34825e5 | 2015-03-07 01:41:36 +0900 | [diff] [blame] | 561 | if (!info) |
Chanwoo Choi | c03e017 | 2014-08-18 09:05:21 +0900 | [diff] [blame] | 562 | return -ENOMEM; |
Chanwoo Choi | c03e017 | 2014-08-18 09:05:21 +0900 | [diff] [blame] | 563 | i2c_set_clientdata(i2c, info); |
| 564 | |
| 565 | info->dev = &i2c->dev; |
| 566 | info->i2c = i2c; |
| 567 | info->irq = i2c->irq; |
| 568 | info->muic_irqs = rt8973a_muic_irqs; |
| 569 | info->num_muic_irqs = ARRAY_SIZE(rt8973a_muic_irqs); |
| 570 | info->reg_data = rt8973a_reg_data; |
| 571 | info->num_reg_data = ARRAY_SIZE(rt8973a_reg_data); |
| 572 | |
| 573 | mutex_init(&info->mutex); |
| 574 | |
| 575 | INIT_WORK(&info->irq_work, rt8973a_muic_irq_work); |
| 576 | |
| 577 | info->regmap = devm_regmap_init_i2c(i2c, &rt8973a_muic_regmap_config); |
| 578 | if (IS_ERR(info->regmap)) { |
| 579 | ret = PTR_ERR(info->regmap); |
| 580 | dev_err(info->dev, "failed to allocate register map: %d\n", |
| 581 | ret); |
| 582 | return ret; |
| 583 | } |
| 584 | |
| 585 | /* Support irq domain for RT8973A MUIC device */ |
| 586 | irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_SHARED; |
| 587 | ret = regmap_add_irq_chip(info->regmap, info->irq, irq_flags, 0, |
| 588 | &rt8973a_muic_irq_chip, &info->irq_data); |
| 589 | if (ret != 0) { |
| 590 | dev_err(info->dev, "failed to add irq_chip (irq:%d, err:%d)\n", |
| 591 | info->irq, ret); |
| 592 | return ret; |
| 593 | } |
| 594 | |
| 595 | for (i = 0; i < info->num_muic_irqs; i++) { |
| 596 | struct muic_irq *muic_irq = &info->muic_irqs[i]; |
Andrzej Hajda | 238ffc2e6 | 2015-09-24 16:00:20 +0200 | [diff] [blame] | 597 | int virq = 0; |
Chanwoo Choi | c03e017 | 2014-08-18 09:05:21 +0900 | [diff] [blame] | 598 | |
| 599 | virq = regmap_irq_get_virq(info->irq_data, muic_irq->irq); |
| 600 | if (virq <= 0) |
| 601 | return -EINVAL; |
| 602 | muic_irq->virq = virq; |
| 603 | |
| 604 | ret = devm_request_threaded_irq(info->dev, virq, NULL, |
| 605 | rt8973a_muic_irq_handler, |
| 606 | IRQF_NO_SUSPEND, |
| 607 | muic_irq->name, info); |
| 608 | if (ret) { |
| 609 | dev_err(info->dev, |
| 610 | "failed: irq request (IRQ: %d, error :%d)\n", |
| 611 | muic_irq->irq, ret); |
| 612 | return ret; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | /* Allocate extcon device */ |
| 617 | info->edev = devm_extcon_dev_allocate(info->dev, rt8973a_extcon_cable); |
| 618 | if (IS_ERR(info->edev)) { |
| 619 | dev_err(info->dev, "failed to allocate memory for extcon\n"); |
| 620 | return -ENOMEM; |
| 621 | } |
Chanwoo Choi | c03e017 | 2014-08-18 09:05:21 +0900 | [diff] [blame] | 622 | |
| 623 | /* Register extcon device */ |
| 624 | ret = devm_extcon_dev_register(info->dev, info->edev); |
| 625 | if (ret) { |
| 626 | dev_err(info->dev, "failed to register extcon device\n"); |
| 627 | return ret; |
| 628 | } |
| 629 | |
| 630 | /* |
| 631 | * Detect accessory after completing the initialization of platform |
| 632 | * |
| 633 | * - Use delayed workqueue to detect cable state and then |
| 634 | * notify cable state to notifiee/platform through uevent. |
| 635 | * After completing the booting of platform, the extcon provider |
| 636 | * driver should notify cable state to upper layer. |
| 637 | */ |
| 638 | INIT_DELAYED_WORK(&info->wq_detcable, rt8973a_muic_detect_cable_wq); |
| 639 | queue_delayed_work(system_power_efficient_wq, &info->wq_detcable, |
| 640 | msecs_to_jiffies(DELAY_MS_DEFAULT)); |
| 641 | |
| 642 | /* Initialize RT8973A device and print vendor id and version id */ |
| 643 | rt8973a_init_dev_type(info); |
| 644 | |
| 645 | return 0; |
| 646 | } |
| 647 | |
| 648 | static int rt8973a_muic_i2c_remove(struct i2c_client *i2c) |
| 649 | { |
| 650 | struct rt8973a_muic_info *info = i2c_get_clientdata(i2c); |
| 651 | |
| 652 | regmap_del_irq_chip(info->irq, info->irq_data); |
| 653 | |
| 654 | return 0; |
| 655 | } |
| 656 | |
Chanwoo Choi | 34825e5 | 2015-03-07 01:41:36 +0900 | [diff] [blame] | 657 | static const struct of_device_id rt8973a_dt_match[] = { |
Chanwoo Choi | c03e017 | 2014-08-18 09:05:21 +0900 | [diff] [blame] | 658 | { .compatible = "richtek,rt8973a-muic" }, |
| 659 | { }, |
| 660 | }; |
Javier Martinez Canillas | ff612f9 | 2015-08-25 08:31:15 +0200 | [diff] [blame] | 661 | MODULE_DEVICE_TABLE(of, rt8973a_dt_match); |
Chanwoo Choi | c03e017 | 2014-08-18 09:05:21 +0900 | [diff] [blame] | 662 | |
| 663 | #ifdef CONFIG_PM_SLEEP |
| 664 | static int rt8973a_muic_suspend(struct device *dev) |
| 665 | { |
| 666 | struct i2c_client *i2c = container_of(dev, struct i2c_client, dev); |
| 667 | struct rt8973a_muic_info *info = i2c_get_clientdata(i2c); |
| 668 | |
| 669 | enable_irq_wake(info->irq); |
| 670 | |
| 671 | return 0; |
| 672 | } |
| 673 | |
| 674 | static int rt8973a_muic_resume(struct device *dev) |
| 675 | { |
| 676 | struct i2c_client *i2c = container_of(dev, struct i2c_client, dev); |
| 677 | struct rt8973a_muic_info *info = i2c_get_clientdata(i2c); |
| 678 | |
| 679 | disable_irq_wake(info->irq); |
| 680 | |
| 681 | return 0; |
| 682 | } |
| 683 | #endif |
| 684 | |
| 685 | static SIMPLE_DEV_PM_OPS(rt8973a_muic_pm_ops, |
| 686 | rt8973a_muic_suspend, rt8973a_muic_resume); |
| 687 | |
| 688 | static const struct i2c_device_id rt8973a_i2c_id[] = { |
| 689 | { "rt8973a", TYPE_RT8973A }, |
| 690 | { } |
| 691 | }; |
| 692 | MODULE_DEVICE_TABLE(i2c, rt8973a_i2c_id); |
| 693 | |
| 694 | static struct i2c_driver rt8973a_muic_i2c_driver = { |
| 695 | .driver = { |
| 696 | .name = "rt8973a", |
Chanwoo Choi | c03e017 | 2014-08-18 09:05:21 +0900 | [diff] [blame] | 697 | .pm = &rt8973a_muic_pm_ops, |
| 698 | .of_match_table = rt8973a_dt_match, |
| 699 | }, |
| 700 | .probe = rt8973a_muic_i2c_probe, |
| 701 | .remove = rt8973a_muic_i2c_remove, |
| 702 | .id_table = rt8973a_i2c_id, |
| 703 | }; |
| 704 | |
| 705 | static int __init rt8973a_muic_i2c_init(void) |
| 706 | { |
| 707 | return i2c_add_driver(&rt8973a_muic_i2c_driver); |
| 708 | } |
| 709 | subsys_initcall(rt8973a_muic_i2c_init); |
| 710 | |
| 711 | MODULE_DESCRIPTION("Richtek RT8973A Extcon driver"); |
| 712 | MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>"); |
| 713 | MODULE_LICENSE("GPL"); |