Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 1 | /* |
Chanwoo Choi | b76668b | 2012-05-09 12:31:58 +0900 | [diff] [blame] | 2 | * extcon-max8997.c - MAX8997 extcon driver to support MAX8997 MUIC |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 3 | * |
Sachin Kamat | 2ca36f4 | 2012-11-20 15:46:33 +0900 | [diff] [blame] | 4 | * Copyright (C) 2012 Samsung Electronics |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 5 | * Donggeun Kim <dg77.kim@samsung.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/i2c.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/interrupt.h> |
| 23 | #include <linux/err.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/kobject.h> |
| 26 | #include <linux/mfd/max8997.h> |
| 27 | #include <linux/mfd/max8997-private.h> |
Chanwoo Choi | b76668b | 2012-05-09 12:31:58 +0900 | [diff] [blame] | 28 | #include <linux/extcon.h> |
Chanwoo Choi | dca1a71 | 2012-07-02 09:03:00 +0900 | [diff] [blame] | 29 | #include <linux/irqdomain.h> |
Chanwoo Choi | b76668b | 2012-05-09 12:31:58 +0900 | [diff] [blame] | 30 | |
| 31 | #define DEV_NAME "max8997-muic" |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 32 | |
Chanwoo Choi | 027fcd5 | 2013-02-13 08:50:00 +0900 | [diff] [blame] | 33 | enum max8997_muic_adc_debounce_time { |
| 34 | ADC_DEBOUNCE_TIME_0_5MS = 0, /* 0.5ms */ |
| 35 | ADC_DEBOUNCE_TIME_10MS, /* 10ms */ |
| 36 | ADC_DEBOUNCE_TIME_25MS, /* 25ms */ |
| 37 | ADC_DEBOUNCE_TIME_38_62MS, /* 38.62ms */ |
| 38 | }; |
| 39 | |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 40 | struct max8997_muic_irq { |
| 41 | unsigned int irq; |
| 42 | const char *name; |
Chanwoo Choi | dca1a71 | 2012-07-02 09:03:00 +0900 | [diff] [blame] | 43 | unsigned int virq; |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | static struct max8997_muic_irq muic_irqs[] = { |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 47 | { MAX8997_MUICIRQ_ADCError, "muic-ADCERROR" }, |
| 48 | { MAX8997_MUICIRQ_ADCLow, "muic-ADCLOW" }, |
| 49 | { MAX8997_MUICIRQ_ADC, "muic-ADC" }, |
| 50 | { MAX8997_MUICIRQ_VBVolt, "muic-VBVOLT" }, |
| 51 | { MAX8997_MUICIRQ_DBChg, "muic-DBCHG" }, |
| 52 | { MAX8997_MUICIRQ_DCDTmr, "muic-DCDTMR" }, |
| 53 | { MAX8997_MUICIRQ_ChgDetRun, "muic-CHGDETRUN" }, |
| 54 | { MAX8997_MUICIRQ_ChgTyp, "muic-CHGTYP" }, |
| 55 | { MAX8997_MUICIRQ_OVP, "muic-OVP" }, |
| 56 | }; |
| 57 | |
| 58 | /* Define supported cable type */ |
| 59 | enum max8997_muic_acc_type { |
| 60 | MAX8997_MUIC_ADC_GROUND = 0x0, |
| 61 | MAX8997_MUIC_ADC_MHL, /* MHL*/ |
| 62 | MAX8997_MUIC_ADC_REMOTE_S1_BUTTON, |
| 63 | MAX8997_MUIC_ADC_REMOTE_S2_BUTTON, |
| 64 | MAX8997_MUIC_ADC_REMOTE_S3_BUTTON, |
| 65 | MAX8997_MUIC_ADC_REMOTE_S4_BUTTON, |
| 66 | MAX8997_MUIC_ADC_REMOTE_S5_BUTTON, |
| 67 | MAX8997_MUIC_ADC_REMOTE_S6_BUTTON, |
| 68 | MAX8997_MUIC_ADC_REMOTE_S7_BUTTON, |
| 69 | MAX8997_MUIC_ADC_REMOTE_S8_BUTTON, |
| 70 | MAX8997_MUIC_ADC_REMOTE_S9_BUTTON, |
| 71 | MAX8997_MUIC_ADC_REMOTE_S10_BUTTON, |
| 72 | MAX8997_MUIC_ADC_REMOTE_S11_BUTTON, |
| 73 | MAX8997_MUIC_ADC_REMOTE_S12_BUTTON, |
| 74 | MAX8997_MUIC_ADC_RESERVED_ACC_1, |
| 75 | MAX8997_MUIC_ADC_RESERVED_ACC_2, |
| 76 | MAX8997_MUIC_ADC_RESERVED_ACC_3, |
| 77 | MAX8997_MUIC_ADC_RESERVED_ACC_4, |
| 78 | MAX8997_MUIC_ADC_RESERVED_ACC_5, |
| 79 | MAX8997_MUIC_ADC_CEA936_AUDIO, |
| 80 | MAX8997_MUIC_ADC_PHONE_POWERED_DEV, |
| 81 | MAX8997_MUIC_ADC_TTY_CONVERTER, |
| 82 | MAX8997_MUIC_ADC_UART_CABLE, |
| 83 | MAX8997_MUIC_ADC_CEA936A_TYPE1_CHG, |
| 84 | MAX8997_MUIC_ADC_FACTORY_MODE_USB_OFF, /* JIG-USB-OFF */ |
| 85 | MAX8997_MUIC_ADC_FACTORY_MODE_USB_ON, /* JIG-USB-ON */ |
| 86 | MAX8997_MUIC_ADC_AV_CABLE_NOLOAD, /* DESKDOCK */ |
| 87 | MAX8997_MUIC_ADC_CEA936A_TYPE2_CHG, |
| 88 | MAX8997_MUIC_ADC_FACTORY_MODE_UART_OFF, /* JIG-UART */ |
| 89 | MAX8997_MUIC_ADC_FACTORY_MODE_UART_ON, /* CARDOCK */ |
| 90 | MAX8997_MUIC_ADC_AUDIO_MODE_REMOTE, |
| 91 | MAX8997_MUIC_ADC_OPEN, /* OPEN */ |
| 92 | }; |
| 93 | |
| 94 | enum max8997_muic_cable_group { |
| 95 | MAX8997_CABLE_GROUP_ADC = 0, |
| 96 | MAX8997_CABLE_GROUP_ADC_GND, |
| 97 | MAX8997_CABLE_GROUP_CHG, |
| 98 | MAX8997_CABLE_GROUP_VBVOLT, |
| 99 | }; |
| 100 | |
| 101 | enum max8997_muic_usb_type { |
| 102 | MAX8997_USB_HOST, |
| 103 | MAX8997_USB_DEVICE, |
| 104 | }; |
| 105 | |
| 106 | enum max8997_muic_charger_type { |
| 107 | MAX8997_CHARGER_TYPE_NONE = 0, |
| 108 | MAX8997_CHARGER_TYPE_USB, |
| 109 | MAX8997_CHARGER_TYPE_DOWNSTREAM_PORT, |
| 110 | MAX8997_CHARGER_TYPE_DEDICATED_CHG, |
| 111 | MAX8997_CHARGER_TYPE_500MA, |
| 112 | MAX8997_CHARGER_TYPE_1A, |
| 113 | MAX8997_CHARGER_TYPE_DEAD_BATTERY = 7, |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | struct max8997_muic_info { |
| 117 | struct device *dev; |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 118 | struct i2c_client *muic; |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 119 | struct extcon_dev *edev; |
| 120 | int prev_cable_type; |
| 121 | int prev_chg_type; |
| 122 | u8 status[2]; |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 123 | |
| 124 | int irq; |
| 125 | struct work_struct irq_work; |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 126 | struct mutex mutex; |
Chanwoo Choi | b76668b | 2012-05-09 12:31:58 +0900 | [diff] [blame] | 127 | |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 128 | struct max8997_muic_platform_data *muic_pdata; |
| 129 | enum max8997_muic_charger_type pre_charger_type; |
Chanwoo Choi | 685dc9a | 2013-02-13 15:04:15 +0900 | [diff] [blame^] | 130 | |
| 131 | /* |
| 132 | * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB |
| 133 | * h/w path of COMP2/COMN1 on CONTROL1 register. |
| 134 | */ |
| 135 | int path_usb; |
| 136 | int path_uart; |
Chanwoo Choi | b76668b | 2012-05-09 12:31:58 +0900 | [diff] [blame] | 137 | }; |
| 138 | |
Chanwoo Choi | e3e5bc0 | 2013-02-12 20:44:19 +0900 | [diff] [blame] | 139 | enum { |
| 140 | EXTCON_CABLE_USB = 0, |
| 141 | EXTCON_CABLE_USB_HOST, |
| 142 | EXTCON_CABLE_TA, |
| 143 | EXTCON_CABLE_FAST_CHARGER, |
| 144 | EXTCON_CABLE_SLOW_CHARGER, |
| 145 | EXTCON_CABLE_CHARGE_DOWNSTREAM, |
| 146 | EXTCON_CABLE_MHL, |
| 147 | EXTCON_CABLE_DOCK_DESK, |
| 148 | EXTCON_CABLE_DOCK_CARD, |
| 149 | EXTCON_CABLE_JIG, |
| 150 | |
| 151 | _EXTCON_CABLE_NUM, |
| 152 | }; |
| 153 | |
Sachin Kamat | cae93db | 2013-01-31 09:29:29 +0900 | [diff] [blame] | 154 | static const char *max8997_extcon_cable[] = { |
Chanwoo Choi | e3e5bc0 | 2013-02-12 20:44:19 +0900 | [diff] [blame] | 155 | [EXTCON_CABLE_USB] = "USB", |
| 156 | [EXTCON_CABLE_USB_HOST] = "USB-Host", |
| 157 | [EXTCON_CABLE_TA] = "TA", |
| 158 | [EXTCON_CABLE_FAST_CHARGER] = "Fast-charger", |
| 159 | [EXTCON_CABLE_SLOW_CHARGER] = "Slow-charger", |
| 160 | [EXTCON_CABLE_CHARGE_DOWNSTREAM] = "Charge-downstream", |
| 161 | [EXTCON_CABLE_MHL] = "MHL", |
| 162 | [EXTCON_CABLE_DOCK_DESK] = "Dock-Desk", |
| 163 | [EXTCON_CABLE_DOCK_CARD] = "Dock-Card", |
| 164 | [EXTCON_CABLE_JIG] = "JIG", |
Chanwoo Choi | b76668b | 2012-05-09 12:31:58 +0900 | [diff] [blame] | 165 | |
| 166 | NULL, |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 167 | }; |
| 168 | |
Chanwoo Choi | 07c7050 | 2013-02-13 08:42:37 +0900 | [diff] [blame] | 169 | /* |
Chanwoo Choi | 027fcd5 | 2013-02-13 08:50:00 +0900 | [diff] [blame] | 170 | * max8997_muic_set_debounce_time - Set the debounce time of ADC |
| 171 | * @info: the instance including private data of max8997 MUIC |
| 172 | * @time: the debounce time of ADC |
| 173 | */ |
| 174 | static int max8997_muic_set_debounce_time(struct max8997_muic_info *info, |
| 175 | enum max8997_muic_adc_debounce_time time) |
| 176 | { |
| 177 | int ret; |
| 178 | |
| 179 | switch (time) { |
| 180 | case ADC_DEBOUNCE_TIME_0_5MS: |
| 181 | case ADC_DEBOUNCE_TIME_10MS: |
| 182 | case ADC_DEBOUNCE_TIME_25MS: |
| 183 | case ADC_DEBOUNCE_TIME_38_62MS: |
| 184 | ret = max8997_update_reg(info->muic, |
| 185 | MAX8997_MUIC_REG_CONTROL3, |
| 186 | time << CONTROL3_ADCDBSET_SHIFT, |
| 187 | CONTROL3_ADCDBSET_MASK); |
| 188 | if (ret) { |
| 189 | dev_err(info->dev, "failed to set ADC debounce time\n"); |
| 190 | return -EAGAIN; |
| 191 | } |
| 192 | break; |
| 193 | default: |
| 194 | dev_err(info->dev, "invalid ADC debounce time\n"); |
| 195 | return -EINVAL; |
| 196 | } |
| 197 | |
| 198 | return 0; |
| 199 | }; |
| 200 | |
| 201 | /* |
Chanwoo Choi | 07c7050 | 2013-02-13 08:42:37 +0900 | [diff] [blame] | 202 | * max8997_muic_set_path - Set hardware line according to attached cable |
| 203 | * @info: the instance including private data of max8997 MUIC |
| 204 | * @value: the path according to attached cable |
| 205 | * @attached: the state of cable (true:attached, false:detached) |
| 206 | * |
| 207 | * The max8997 MUIC device share outside H/W line among a varity of cables, |
| 208 | * so this function set internal path of H/W line according to the type of |
| 209 | * attached cable. |
| 210 | */ |
| 211 | static int max8997_muic_set_path(struct max8997_muic_info *info, |
| 212 | u8 val, bool attached) |
| 213 | { |
| 214 | int ret = 0; |
| 215 | u8 ctrl1, ctrl2 = 0; |
| 216 | |
| 217 | if (attached) |
| 218 | ctrl1 = val; |
| 219 | else |
| 220 | ctrl1 = CONTROL1_SW_OPEN; |
| 221 | |
| 222 | ret = max8997_update_reg(info->muic, |
| 223 | MAX8997_MUIC_REG_CONTROL1, ctrl1, COMP_SW_MASK); |
| 224 | if (ret < 0) { |
| 225 | dev_err(info->dev, "failed to update MUIC register\n"); |
| 226 | return -EAGAIN; |
| 227 | } |
| 228 | |
| 229 | if (attached) |
| 230 | ctrl2 |= CONTROL2_CPEN_MASK; /* LowPwr=0, CPEn=1 */ |
| 231 | else |
| 232 | ctrl2 |= CONTROL2_LOWPWR_MASK; /* LowPwr=1, CPEn=0 */ |
| 233 | |
| 234 | ret = max8997_update_reg(info->muic, |
| 235 | MAX8997_MUIC_REG_CONTROL2, ctrl2, |
| 236 | CONTROL2_LOWPWR_MASK | CONTROL2_CPEN_MASK); |
| 237 | if (ret < 0) { |
| 238 | dev_err(info->dev, "failed to update MUIC register\n"); |
| 239 | return -EAGAIN; |
| 240 | } |
| 241 | |
| 242 | dev_info(info->dev, |
| 243 | "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n", |
| 244 | ctrl1, ctrl2, attached ? "attached" : "detached"); |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 249 | /* |
| 250 | * max8997_muic_get_cable_type - Return cable type and check cable state |
| 251 | * @info: the instance including private data of max8997 MUIC |
| 252 | * @group: the path according to attached cable |
| 253 | * @attached: store cable state and return |
| 254 | * |
| 255 | * This function check the cable state either attached or detached, |
| 256 | * and then divide precise type of cable according to cable group. |
| 257 | * - MAX8997_CABLE_GROUP_ADC |
| 258 | * - MAX8997_CABLE_GROUP_CHG |
| 259 | */ |
| 260 | static int max8997_muic_get_cable_type(struct max8997_muic_info *info, |
| 261 | enum max8997_muic_cable_group group, bool *attached) |
| 262 | { |
| 263 | int cable_type = 0; |
| 264 | int adc; |
| 265 | int chg_type; |
| 266 | |
| 267 | switch (group) { |
| 268 | case MAX8997_CABLE_GROUP_ADC: |
| 269 | /* |
| 270 | * Read ADC value to check cable type and decide cable state |
| 271 | * according to cable type |
| 272 | */ |
| 273 | adc = info->status[0] & STATUS1_ADC_MASK; |
| 274 | adc >>= STATUS1_ADC_SHIFT; |
| 275 | |
| 276 | /* |
| 277 | * Check current cable state/cable type and store cable type |
| 278 | * (info->prev_cable_type) for handling cable when cable is |
| 279 | * detached. |
| 280 | */ |
| 281 | if (adc == MAX8997_MUIC_ADC_OPEN) { |
| 282 | *attached = false; |
| 283 | |
| 284 | cable_type = info->prev_cable_type; |
| 285 | info->prev_cable_type = MAX8997_MUIC_ADC_OPEN; |
| 286 | } else { |
| 287 | *attached = true; |
| 288 | |
| 289 | cable_type = info->prev_cable_type = adc; |
| 290 | } |
| 291 | break; |
| 292 | case MAX8997_CABLE_GROUP_CHG: |
| 293 | /* |
| 294 | * Read charger type to check cable type and decide cable state |
| 295 | * according to type of charger cable. |
| 296 | */ |
| 297 | chg_type = info->status[1] & STATUS2_CHGTYP_MASK; |
| 298 | chg_type >>= STATUS2_CHGTYP_SHIFT; |
| 299 | |
| 300 | if (chg_type == MAX8997_CHARGER_TYPE_NONE) { |
| 301 | *attached = false; |
| 302 | |
| 303 | cable_type = info->prev_chg_type; |
| 304 | info->prev_chg_type = MAX8997_CHARGER_TYPE_NONE; |
| 305 | } else { |
| 306 | *attached = true; |
| 307 | |
| 308 | /* |
| 309 | * Check current cable state/cable type and store cable |
| 310 | * type(info->prev_chg_type) for handling cable when |
| 311 | * charger cable is detached. |
| 312 | */ |
| 313 | cable_type = info->prev_chg_type = chg_type; |
| 314 | } |
| 315 | |
| 316 | break; |
| 317 | default: |
| 318 | dev_err(info->dev, "Unknown cable group (%d)\n", group); |
| 319 | cable_type = -EINVAL; |
| 320 | break; |
| 321 | } |
| 322 | |
| 323 | return cable_type; |
| 324 | } |
| 325 | |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 326 | static int max8997_muic_handle_usb(struct max8997_muic_info *info, |
| 327 | enum max8997_muic_usb_type usb_type, bool attached) |
| 328 | { |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 329 | int ret = 0; |
| 330 | |
| 331 | if (usb_type == MAX8997_USB_HOST) { |
Chanwoo Choi | 685dc9a | 2013-02-13 15:04:15 +0900 | [diff] [blame^] | 332 | ret = max8997_muic_set_path(info, info->path_usb, attached); |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 333 | if (ret < 0) { |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 334 | dev_err(info->dev, "failed to update muic register\n"); |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 335 | return ret; |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
Chanwoo Choi | b76668b | 2012-05-09 12:31:58 +0900 | [diff] [blame] | 339 | switch (usb_type) { |
| 340 | case MAX8997_USB_HOST: |
| 341 | extcon_set_cable_state(info->edev, "USB-Host", attached); |
| 342 | break; |
| 343 | case MAX8997_USB_DEVICE: |
| 344 | extcon_set_cable_state(info->edev, "USB", attached); |
| 345 | break; |
| 346 | default: |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 347 | dev_err(info->dev, "failed to detect %s usb cable\n", |
| 348 | attached ? "attached" : "detached"); |
| 349 | return -EINVAL; |
Chanwoo Choi | b76668b | 2012-05-09 12:31:58 +0900 | [diff] [blame] | 350 | } |
| 351 | |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 352 | return 0; |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 353 | } |
| 354 | |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 355 | static int max8997_muic_handle_dock(struct max8997_muic_info *info, |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 356 | int cable_type, bool attached) |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 357 | { |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 358 | int ret = 0; |
| 359 | |
Chanwoo Choi | 07c7050 | 2013-02-13 08:42:37 +0900 | [diff] [blame] | 360 | ret = max8997_muic_set_path(info, CONTROL1_SW_AUDIO, attached); |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 361 | if (ret) { |
| 362 | dev_err(info->dev, "failed to update muic register\n"); |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 363 | return ret; |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 364 | } |
| 365 | |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 366 | switch (cable_type) { |
| 367 | case MAX8997_MUIC_ADC_AV_CABLE_NOLOAD: |
Chanwoo Choi | b76668b | 2012-05-09 12:31:58 +0900 | [diff] [blame] | 368 | extcon_set_cable_state(info->edev, "Dock-desk", attached); |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 369 | break; |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 370 | case MAX8997_MUIC_ADC_FACTORY_MODE_UART_ON: |
Chanwoo Choi | b76668b | 2012-05-09 12:31:58 +0900 | [diff] [blame] | 371 | extcon_set_cable_state(info->edev, "Dock-card", attached); |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 372 | break; |
| 373 | default: |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 374 | dev_err(info->dev, "failed to detect %s dock device\n", |
| 375 | attached ? "attached" : "detached"); |
| 376 | return -EINVAL; |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 377 | } |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 378 | |
| 379 | return 0; |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | static int max8997_muic_handle_jig_uart(struct max8997_muic_info *info, |
| 383 | bool attached) |
| 384 | { |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 385 | int ret = 0; |
| 386 | |
| 387 | /* switch to UART */ |
Chanwoo Choi | 685dc9a | 2013-02-13 15:04:15 +0900 | [diff] [blame^] | 388 | ret = max8997_muic_set_path(info, info->path_uart, attached); |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 389 | if (ret) { |
| 390 | dev_err(info->dev, "failed to update muic register\n"); |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 391 | return -EINVAL; |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 392 | } |
| 393 | |
Chanwoo Choi | b76668b | 2012-05-09 12:31:58 +0900 | [diff] [blame] | 394 | extcon_set_cable_state(info->edev, "JIG", attached); |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 395 | |
| 396 | return 0; |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 397 | } |
| 398 | |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 399 | static int max8997_muic_adc_handler(struct max8997_muic_info *info) |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 400 | { |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 401 | int cable_type; |
| 402 | bool attached; |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 403 | int ret = 0; |
| 404 | |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 405 | /* Check cable state which is either detached or attached */ |
| 406 | cable_type = max8997_muic_get_cable_type(info, |
| 407 | MAX8997_CABLE_GROUP_ADC, &attached); |
| 408 | |
| 409 | switch (cable_type) { |
| 410 | case MAX8997_MUIC_ADC_GROUND: |
| 411 | ret = max8997_muic_handle_usb(info, MAX8997_USB_HOST, attached); |
| 412 | if (ret < 0) |
| 413 | return ret; |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 414 | break; |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 415 | case MAX8997_MUIC_ADC_MHL: |
| 416 | extcon_set_cable_state(info->edev, "MHL", attached); |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 417 | break; |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 418 | case MAX8997_MUIC_ADC_FACTORY_MODE_USB_OFF: |
| 419 | case MAX8997_MUIC_ADC_FACTORY_MODE_USB_ON: |
| 420 | ret = max8997_muic_handle_usb(info, MAX8997_USB_DEVICE, attached); |
| 421 | if (ret < 0) |
| 422 | return ret; |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 423 | break; |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 424 | case MAX8997_MUIC_ADC_AV_CABLE_NOLOAD: |
| 425 | case MAX8997_MUIC_ADC_FACTORY_MODE_UART_ON: |
| 426 | ret = max8997_muic_handle_dock(info, cable_type, attached); |
| 427 | if (ret < 0) |
| 428 | return ret; |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 429 | break; |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 430 | case MAX8997_MUIC_ADC_FACTORY_MODE_UART_OFF: |
| 431 | ret = max8997_muic_handle_jig_uart(info, attached); |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 432 | break; |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 433 | case MAX8997_MUIC_ADC_REMOTE_S1_BUTTON: |
| 434 | case MAX8997_MUIC_ADC_REMOTE_S2_BUTTON: |
| 435 | case MAX8997_MUIC_ADC_REMOTE_S3_BUTTON: |
| 436 | case MAX8997_MUIC_ADC_REMOTE_S4_BUTTON: |
| 437 | case MAX8997_MUIC_ADC_REMOTE_S5_BUTTON: |
| 438 | case MAX8997_MUIC_ADC_REMOTE_S6_BUTTON: |
| 439 | case MAX8997_MUIC_ADC_REMOTE_S7_BUTTON: |
| 440 | case MAX8997_MUIC_ADC_REMOTE_S8_BUTTON: |
| 441 | case MAX8997_MUIC_ADC_REMOTE_S9_BUTTON: |
| 442 | case MAX8997_MUIC_ADC_REMOTE_S10_BUTTON: |
| 443 | case MAX8997_MUIC_ADC_REMOTE_S11_BUTTON: |
| 444 | case MAX8997_MUIC_ADC_REMOTE_S12_BUTTON: |
| 445 | case MAX8997_MUIC_ADC_RESERVED_ACC_1: |
| 446 | case MAX8997_MUIC_ADC_RESERVED_ACC_2: |
| 447 | case MAX8997_MUIC_ADC_RESERVED_ACC_3: |
| 448 | case MAX8997_MUIC_ADC_RESERVED_ACC_4: |
| 449 | case MAX8997_MUIC_ADC_RESERVED_ACC_5: |
| 450 | case MAX8997_MUIC_ADC_CEA936_AUDIO: |
| 451 | case MAX8997_MUIC_ADC_PHONE_POWERED_DEV: |
| 452 | case MAX8997_MUIC_ADC_TTY_CONVERTER: |
| 453 | case MAX8997_MUIC_ADC_UART_CABLE: |
| 454 | case MAX8997_MUIC_ADC_CEA936A_TYPE1_CHG: |
| 455 | case MAX8997_MUIC_ADC_CEA936A_TYPE2_CHG: |
| 456 | case MAX8997_MUIC_ADC_AUDIO_MODE_REMOTE: |
| 457 | /* |
| 458 | * This cable isn't used in general case if it is specially |
| 459 | * needed to detect additional cable, should implement |
| 460 | * proper operation when this cable is attached/detached. |
| 461 | */ |
| 462 | dev_info(info->dev, |
| 463 | "cable is %s but it isn't used (type:0x%x)\n", |
| 464 | attached ? "attached" : "detached", cable_type); |
| 465 | return -EAGAIN; |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 466 | default: |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 467 | dev_err(info->dev, |
| 468 | "failed to detect %s unknown cable (type:0x%x)\n", |
| 469 | attached ? "attached" : "detached", cable_type); |
Devendra Naga | 3cafbd4 | 2012-08-31 14:00:19 +0530 | [diff] [blame] | 470 | return -EINVAL; |
Chanwoo Choi | b76668b | 2012-05-09 12:31:58 +0900 | [diff] [blame] | 471 | } |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 472 | |
Devendra Naga | 3cafbd4 | 2012-08-31 14:00:19 +0530 | [diff] [blame] | 473 | return 0; |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 474 | } |
| 475 | |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 476 | static int max8997_muic_chg_handler(struct max8997_muic_info *info) |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 477 | { |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 478 | int chg_type; |
| 479 | bool attached; |
| 480 | int adc; |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 481 | |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 482 | chg_type = max8997_muic_get_cable_type(info, |
| 483 | MAX8997_CABLE_GROUP_CHG, &attached); |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 484 | |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 485 | switch (chg_type) { |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 486 | case MAX8997_CHARGER_TYPE_NONE: |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 487 | break; |
| 488 | case MAX8997_CHARGER_TYPE_USB: |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 489 | adc = info->status[0] & STATUS1_ADC_MASK; |
| 490 | adc >>= STATUS1_ADC_SHIFT; |
| 491 | |
| 492 | if ((adc & STATUS1_ADC_MASK) == MAX8997_MUIC_ADC_OPEN) { |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 493 | max8997_muic_handle_usb(info, |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 494 | MAX8997_USB_DEVICE, attached); |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 495 | } |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 496 | break; |
| 497 | case MAX8997_CHARGER_TYPE_DOWNSTREAM_PORT: |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 498 | extcon_set_cable_state(info->edev, "Charge-downstream", attached); |
Chanwoo Choi | b76668b | 2012-05-09 12:31:58 +0900 | [diff] [blame] | 499 | break; |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 500 | case MAX8997_CHARGER_TYPE_DEDICATED_CHG: |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 501 | extcon_set_cable_state(info->edev, "TA", attached); |
Chanwoo Choi | b76668b | 2012-05-09 12:31:58 +0900 | [diff] [blame] | 502 | break; |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 503 | case MAX8997_CHARGER_TYPE_500MA: |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 504 | extcon_set_cable_state(info->edev, "Slow-charger", attached); |
Chanwoo Choi | b76668b | 2012-05-09 12:31:58 +0900 | [diff] [blame] | 505 | break; |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 506 | case MAX8997_CHARGER_TYPE_1A: |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 507 | extcon_set_cable_state(info->edev, "Fast-charger", attached); |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 508 | break; |
| 509 | default: |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 510 | dev_err(info->dev, |
| 511 | "failed to detect %s unknown chg cable (type:0x%x)\n", |
| 512 | attached ? "attached" : "detached", chg_type); |
| 513 | return -EINVAL; |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 514 | } |
| 515 | |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 516 | return 0; |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | static void max8997_muic_irq_work(struct work_struct *work) |
| 520 | { |
| 521 | struct max8997_muic_info *info = container_of(work, |
| 522 | struct max8997_muic_info, irq_work); |
Chanwoo Choi | dca1a71 | 2012-07-02 09:03:00 +0900 | [diff] [blame] | 523 | int irq_type = 0; |
| 524 | int i, ret; |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 525 | |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 526 | if (!info->edev) |
| 527 | return; |
| 528 | |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 529 | mutex_lock(&info->mutex); |
| 530 | |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 531 | for (i = 0 ; i < ARRAY_SIZE(muic_irqs) ; i++) |
| 532 | if (info->irq == muic_irqs[i].virq) |
| 533 | irq_type = muic_irqs[i].irq; |
| 534 | |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 535 | ret = max8997_bulk_read(info->muic, MAX8997_MUIC_REG_STATUS1, |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 536 | 2, info->status); |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 537 | if (ret) { |
| 538 | dev_err(info->dev, "failed to read muic register\n"); |
| 539 | mutex_unlock(&info->mutex); |
| 540 | return; |
| 541 | } |
| 542 | |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 543 | switch (irq_type) { |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 544 | case MAX8997_MUICIRQ_ADCError: |
| 545 | case MAX8997_MUICIRQ_ADCLow: |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 546 | case MAX8997_MUICIRQ_ADC: |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 547 | /* Handle all of cable except for charger cable */ |
| 548 | ret = max8997_muic_adc_handler(info); |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 549 | break; |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 550 | case MAX8997_MUICIRQ_VBVolt: |
| 551 | case MAX8997_MUICIRQ_DBChg: |
| 552 | case MAX8997_MUICIRQ_DCDTmr: |
| 553 | case MAX8997_MUICIRQ_ChgDetRun: |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 554 | case MAX8997_MUICIRQ_ChgTyp: |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 555 | /* Handle charger cable */ |
| 556 | ret = max8997_muic_chg_handler(info); |
| 557 | break; |
| 558 | case MAX8997_MUICIRQ_OVP: |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 559 | break; |
| 560 | default: |
Chanwoo Choi | b76668b | 2012-05-09 12:31:58 +0900 | [diff] [blame] | 561 | dev_info(info->dev, "misc interrupt: irq %d occurred\n", |
| 562 | irq_type); |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 563 | mutex_unlock(&info->mutex); |
| 564 | return; |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 565 | } |
| 566 | |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 567 | if (ret < 0) |
| 568 | dev_err(info->dev, "failed to handle MUIC interrupt\n"); |
| 569 | |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 570 | mutex_unlock(&info->mutex); |
| 571 | |
| 572 | return; |
| 573 | } |
| 574 | |
| 575 | static irqreturn_t max8997_muic_irq_handler(int irq, void *data) |
| 576 | { |
| 577 | struct max8997_muic_info *info = data; |
| 578 | |
| 579 | dev_dbg(info->dev, "irq:%d\n", irq); |
| 580 | info->irq = irq; |
| 581 | |
| 582 | schedule_work(&info->irq_work); |
| 583 | |
| 584 | return IRQ_HANDLED; |
| 585 | } |
| 586 | |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 587 | static int max8997_muic_detect_dev(struct max8997_muic_info *info) |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 588 | { |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 589 | int ret = 0; |
| 590 | int adc; |
| 591 | int chg_type; |
| 592 | bool attached; |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 593 | |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 594 | mutex_lock(&info->mutex); |
| 595 | |
| 596 | /* Read STATUSx register to detect accessory */ |
| 597 | ret = max8997_bulk_read(info->muic, |
| 598 | MAX8997_MUIC_REG_STATUS1, 2, info->status); |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 599 | if (ret) { |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 600 | dev_err(info->dev, "failed to read MUIC register\n"); |
| 601 | mutex_unlock(&info->mutex); |
| 602 | return -EINVAL; |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 603 | } |
| 604 | |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 605 | adc = max8997_muic_get_cable_type(info, MAX8997_CABLE_GROUP_ADC, |
| 606 | &attached); |
| 607 | if (attached && adc != MAX8997_MUIC_ADC_OPEN) { |
| 608 | ret = max8997_muic_adc_handler(info); |
| 609 | if (ret < 0) { |
| 610 | dev_err(info->dev, "Cannot detect ADC cable\n"); |
| 611 | mutex_unlock(&info->mutex); |
| 612 | return ret; |
| 613 | } |
| 614 | } |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 615 | |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 616 | chg_type = max8997_muic_get_cable_type(info, MAX8997_CABLE_GROUP_CHG, |
| 617 | &attached); |
| 618 | if (attached && chg_type != MAX8997_CHARGER_TYPE_NONE) { |
| 619 | ret = max8997_muic_chg_handler(info); |
| 620 | if (ret < 0) { |
| 621 | dev_err(info->dev, "Cannot detect charger cable\n"); |
| 622 | mutex_unlock(&info->mutex); |
| 623 | return ret; |
| 624 | } |
| 625 | } |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 626 | |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 627 | mutex_unlock(&info->mutex); |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 628 | |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 629 | return 0; |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 630 | } |
| 631 | |
Bill Pemberton | 44f34fd | 2012-11-19 13:23:21 -0500 | [diff] [blame] | 632 | static int max8997_muic_probe(struct platform_device *pdev) |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 633 | { |
Chanwoo Choi | b76668b | 2012-05-09 12:31:58 +0900 | [diff] [blame] | 634 | struct max8997_dev *max8997 = dev_get_drvdata(pdev->dev.parent); |
| 635 | struct max8997_platform_data *pdata = dev_get_platdata(max8997->dev); |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 636 | struct max8997_muic_info *info; |
| 637 | int ret, i; |
| 638 | |
Sachin Kamat | 0b672e9 | 2012-11-20 15:46:47 +0900 | [diff] [blame] | 639 | info = devm_kzalloc(&pdev->dev, sizeof(struct max8997_muic_info), |
| 640 | GFP_KERNEL); |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 641 | if (!info) { |
| 642 | dev_err(&pdev->dev, "failed to allocate memory\n"); |
Sachin Kamat | 0b672e9 | 2012-11-20 15:46:47 +0900 | [diff] [blame] | 643 | return -ENOMEM; |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 644 | } |
| 645 | |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 646 | info->dev = &pdev->dev; |
Chanwoo Choi | b76668b | 2012-05-09 12:31:58 +0900 | [diff] [blame] | 647 | info->muic = max8997->muic; |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 648 | |
| 649 | platform_set_drvdata(pdev, info); |
| 650 | mutex_init(&info->mutex); |
| 651 | |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 652 | INIT_WORK(&info->irq_work, max8997_muic_irq_work); |
| 653 | |
| 654 | for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) { |
| 655 | struct max8997_muic_irq *muic_irq = &muic_irqs[i]; |
Sachin Kamat | 68c9274 | 2012-11-20 15:46:41 +0900 | [diff] [blame] | 656 | unsigned int virq = 0; |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 657 | |
Chanwoo Choi | dca1a71 | 2012-07-02 09:03:00 +0900 | [diff] [blame] | 658 | virq = irq_create_mapping(max8997->irq_domain, muic_irq->irq); |
Sachin Kamat | 68c9274 | 2012-11-20 15:46:41 +0900 | [diff] [blame] | 659 | if (!virq) { |
| 660 | ret = -EINVAL; |
Chanwoo Choi | dca1a71 | 2012-07-02 09:03:00 +0900 | [diff] [blame] | 661 | goto err_irq; |
Sachin Kamat | 68c9274 | 2012-11-20 15:46:41 +0900 | [diff] [blame] | 662 | } |
Chanwoo Choi | dca1a71 | 2012-07-02 09:03:00 +0900 | [diff] [blame] | 663 | muic_irq->virq = virq; |
| 664 | |
Chanwoo Choi | ae3b321 | 2012-11-28 12:39:01 +0900 | [diff] [blame] | 665 | ret = request_threaded_irq(virq, NULL, |
| 666 | max8997_muic_irq_handler, |
| 667 | IRQF_NO_SUSPEND, |
| 668 | muic_irq->name, info); |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 669 | if (ret) { |
| 670 | dev_err(&pdev->dev, |
| 671 | "failed: irq request (IRQ: %d," |
| 672 | " error :%d)\n", |
| 673 | muic_irq->irq, ret); |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 674 | goto err_irq; |
| 675 | } |
| 676 | } |
| 677 | |
Chanwoo Choi | b76668b | 2012-05-09 12:31:58 +0900 | [diff] [blame] | 678 | /* External connector */ |
Sachin Kamat | 0b672e9 | 2012-11-20 15:46:47 +0900 | [diff] [blame] | 679 | info->edev = devm_kzalloc(&pdev->dev, sizeof(struct extcon_dev), |
| 680 | GFP_KERNEL); |
Chanwoo Choi | b76668b | 2012-05-09 12:31:58 +0900 | [diff] [blame] | 681 | if (!info->edev) { |
| 682 | dev_err(&pdev->dev, "failed to allocate memory for extcon\n"); |
| 683 | ret = -ENOMEM; |
| 684 | goto err_irq; |
| 685 | } |
| 686 | info->edev->name = DEV_NAME; |
| 687 | info->edev->supported_cable = max8997_extcon_cable; |
| 688 | ret = extcon_dev_register(info->edev, NULL); |
| 689 | if (ret) { |
| 690 | dev_err(&pdev->dev, "failed to register extcon device\n"); |
Sachin Kamat | 0b672e9 | 2012-11-20 15:46:47 +0900 | [diff] [blame] | 691 | goto err_irq; |
Chanwoo Choi | b76668b | 2012-05-09 12:31:58 +0900 | [diff] [blame] | 692 | } |
| 693 | |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 694 | /* Initialize registers according to platform data */ |
Chanwoo Choi | b76668b | 2012-05-09 12:31:58 +0900 | [diff] [blame] | 695 | if (pdata->muic_pdata) { |
| 696 | struct max8997_muic_platform_data *mdata = info->muic_pdata; |
| 697 | |
| 698 | for (i = 0; i < mdata->num_init_data; i++) { |
| 699 | max8997_write_reg(info->muic, mdata->init_data[i].addr, |
| 700 | mdata->init_data[i].data); |
| 701 | } |
| 702 | } |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 703 | |
Chanwoo Choi | 685dc9a | 2013-02-13 15:04:15 +0900 | [diff] [blame^] | 704 | /* |
| 705 | * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB |
| 706 | * h/w path of COMP2/COMN1 on CONTROL1 register. |
| 707 | */ |
| 708 | if (pdata->muic_pdata->path_uart) |
| 709 | info->path_uart = pdata->muic_pdata->path_uart; |
| 710 | else |
| 711 | info->path_uart = CONTROL1_SW_UART; |
| 712 | |
| 713 | if (pdata->muic_pdata->path_usb) |
| 714 | info->path_usb = pdata->muic_pdata->path_usb; |
| 715 | else |
| 716 | info->path_usb = CONTROL1_SW_USB; |
| 717 | |
| 718 | /* Set initial path for UART */ |
| 719 | max8997_muic_set_path(info, info->path_uart, true); |
| 720 | |
Chanwoo Choi | 027fcd5 | 2013-02-13 08:50:00 +0900 | [diff] [blame] | 721 | /* Set ADC debounce time */ |
| 722 | max8997_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS); |
| 723 | |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 724 | /* Initial device detection */ |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 725 | ret = max8997_muic_detect_dev(info); |
| 726 | if (ret < 0) { |
| 727 | dev_err(&pdev->dev, "failed to detect cable type\n"); |
| 728 | goto err_extcon; |
| 729 | } |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 730 | |
| 731 | return ret; |
| 732 | |
Chanwoo Choi | f73f623 | 2013-02-13 12:05:42 +0900 | [diff] [blame] | 733 | err_extcon: |
| 734 | extcon_dev_unregister(info->edev); |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 735 | err_irq: |
Axel Lin | 3241d56 | 2012-03-26 09:57:08 +0800 | [diff] [blame] | 736 | while (--i >= 0) |
Chanwoo Choi | dca1a71 | 2012-07-02 09:03:00 +0900 | [diff] [blame] | 737 | free_irq(muic_irqs[i].virq, info); |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 738 | return ret; |
| 739 | } |
| 740 | |
Bill Pemberton | 93ed032 | 2012-11-19 13:25:49 -0500 | [diff] [blame] | 741 | static int max8997_muic_remove(struct platform_device *pdev) |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 742 | { |
| 743 | struct max8997_muic_info *info = platform_get_drvdata(pdev); |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 744 | int i; |
| 745 | |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 746 | for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) |
Chanwoo Choi | dca1a71 | 2012-07-02 09:03:00 +0900 | [diff] [blame] | 747 | free_irq(muic_irqs[i].virq, info); |
Donggeun Kim | 71e5878 | 2011-12-15 18:20:47 +0900 | [diff] [blame] | 748 | cancel_work_sync(&info->irq_work); |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 749 | |
Chanwoo Choi | b76668b | 2012-05-09 12:31:58 +0900 | [diff] [blame] | 750 | extcon_dev_unregister(info->edev); |
| 751 | |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 752 | return 0; |
| 753 | } |
| 754 | |
| 755 | static struct platform_driver max8997_muic_driver = { |
| 756 | .driver = { |
Chanwoo Choi | b76668b | 2012-05-09 12:31:58 +0900 | [diff] [blame] | 757 | .name = DEV_NAME, |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 758 | .owner = THIS_MODULE, |
| 759 | }, |
| 760 | .probe = max8997_muic_probe, |
Bill Pemberton | 5f7e222 | 2012-11-19 13:20:06 -0500 | [diff] [blame] | 761 | .remove = max8997_muic_remove, |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 762 | }; |
| 763 | |
Axel Lin | b00e126 | 2012-01-22 15:33:49 +0800 | [diff] [blame] | 764 | module_platform_driver(max8997_muic_driver); |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 765 | |
Chanwoo Choi | b76668b | 2012-05-09 12:31:58 +0900 | [diff] [blame] | 766 | MODULE_DESCRIPTION("Maxim MAX8997 Extcon driver"); |
Donggeun Kim | 99f09be | 2011-11-24 18:12:18 +0900 | [diff] [blame] | 767 | MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>"); |
| 768 | MODULE_LICENSE("GPL"); |