aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/extcon
diff options
context:
space:
mode:
authorChanwoo Choi <cw00.choi@samsung.com>2012-11-26 19:40:32 -0500
committerChanwoo Choi <cw00.choi@samsung.com>2013-01-15 01:42:14 -0500
commit154f757fd315270e42bd17f4a68d84bd070e5758 (patch)
treec7b107c638428d0d959672fe00be356f1cdff8ed /drivers/extcon
parent9931faca02c604c22335f5a935a501bb2ace6e20 (diff)
extcon: max77693: Remove duplicate code by making function
This patch make max77693-muic_get_cable_type() function to remove duplicate code because almost internal function need to read adc/adc1k/adclow/chg_type value of MUIC register. Also, this patch add description of internal function move field constant of muic device from extcon-max77693 driver to max77693 header file because of it is needed for masking some interrupt through platform data. Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com> Signed-off-by: Myungjoo Ham <myungjoo.ham@samsung.com>
Diffstat (limited to 'drivers/extcon')
-rw-r--r--drivers/extcon/extcon-max77693.c440
1 files changed, 241 insertions, 199 deletions
diff --git a/drivers/extcon/extcon-max77693.c b/drivers/extcon/extcon-max77693.c
index 8c17b65eb74d..e84d5dc06798 100644
--- a/drivers/extcon/extcon-max77693.c
+++ b/drivers/extcon/extcon-max77693.c
@@ -30,92 +30,6 @@
30 30
31#define DEV_NAME "max77693-muic" 31#define DEV_NAME "max77693-muic"
32 32
33/* MAX77693 MUIC - STATUS1~3 Register */
34#define STATUS1_ADC_SHIFT (0)
35#define STATUS1_ADCLOW_SHIFT (5)
36#define STATUS1_ADCERR_SHIFT (6)
37#define STATUS1_ADC1K_SHIFT (7)
38#define STATUS1_ADC_MASK (0x1f << STATUS1_ADC_SHIFT)
39#define STATUS1_ADCLOW_MASK (0x1 << STATUS1_ADCLOW_SHIFT)
40#define STATUS1_ADCERR_MASK (0x1 << STATUS1_ADCERR_SHIFT)
41#define STATUS1_ADC1K_MASK (0x1 << STATUS1_ADC1K_SHIFT)
42
43#define STATUS2_CHGTYP_SHIFT (0)
44#define STATUS2_CHGDETRUN_SHIFT (3)
45#define STATUS2_DCDTMR_SHIFT (4)
46#define STATUS2_DXOVP_SHIFT (5)
47#define STATUS2_VBVOLT_SHIFT (6)
48#define STATUS2_VIDRM_SHIFT (7)
49#define STATUS2_CHGTYP_MASK (0x7 << STATUS2_CHGTYP_SHIFT)
50#define STATUS2_CHGDETRUN_MASK (0x1 << STATUS2_CHGDETRUN_SHIFT)
51#define STATUS2_DCDTMR_MASK (0x1 << STATUS2_DCDTMR_SHIFT)
52#define STATUS2_DXOVP_MASK (0x1 << STATUS2_DXOVP_SHIFT)
53#define STATUS2_VBVOLT_MASK (0x1 << STATUS2_VBVOLT_SHIFT)
54#define STATUS2_VIDRM_MASK (0x1 << STATUS2_VIDRM_SHIFT)
55
56#define STATUS3_OVP_SHIFT (2)
57#define STATUS3_OVP_MASK (0x1 << STATUS3_OVP_SHIFT)
58
59/* MAX77693 CDETCTRL1~2 register */
60#define CDETCTRL1_CHGDETEN_SHIFT (0)
61#define CDETCTRL1_CHGTYPMAN_SHIFT (1)
62#define CDETCTRL1_DCDEN_SHIFT (2)
63#define CDETCTRL1_DCD2SCT_SHIFT (3)
64#define CDETCTRL1_CDDELAY_SHIFT (4)
65#define CDETCTRL1_DCDCPL_SHIFT (5)
66#define CDETCTRL1_CDPDET_SHIFT (7)
67#define CDETCTRL1_CHGDETEN_MASK (0x1 << CDETCTRL1_CHGDETEN_SHIFT)
68#define CDETCTRL1_CHGTYPMAN_MASK (0x1 << CDETCTRL1_CHGTYPMAN_SHIFT)
69#define CDETCTRL1_DCDEN_MASK (0x1 << CDETCTRL1_DCDEN_SHIFT)
70#define CDETCTRL1_DCD2SCT_MASK (0x1 << CDETCTRL1_DCD2SCT_SHIFT)
71#define CDETCTRL1_CDDELAY_MASK (0x1 << CDETCTRL1_CDDELAY_SHIFT)
72#define CDETCTRL1_DCDCPL_MASK (0x1 << CDETCTRL1_DCDCPL_SHIFT)
73#define CDETCTRL1_CDPDET_MASK (0x1 << CDETCTRL1_CDPDET_SHIFT)
74
75#define CDETCTRL2_VIDRMEN_SHIFT (1)
76#define CDETCTRL2_DXOVPEN_SHIFT (3)
77#define CDETCTRL2_VIDRMEN_MASK (0x1 << CDETCTRL2_VIDRMEN_SHIFT)
78#define CDETCTRL2_DXOVPEN_MASK (0x1 << CDETCTRL2_DXOVPEN_SHIFT)
79
80/* MAX77693 MUIC - CONTROL1~3 register */
81#define COMN1SW_SHIFT (0)
82#define COMP2SW_SHIFT (3)
83#define COMN1SW_MASK (0x7 << COMN1SW_SHIFT)
84#define COMP2SW_MASK (0x7 << COMP2SW_SHIFT)
85#define COMP_SW_MASK (COMP2SW_MASK | COMN1SW_MASK)
86#define CONTROL1_SW_USB ((1 << COMP2SW_SHIFT) \
87 | (1 << COMN1SW_SHIFT))
88#define CONTROL1_SW_AUDIO ((2 << COMP2SW_SHIFT) \
89 | (2 << COMN1SW_SHIFT))
90#define CONTROL1_SW_UART ((3 << COMP2SW_SHIFT) \
91 | (3 << COMN1SW_SHIFT))
92#define CONTROL1_SW_OPEN ((0 << COMP2SW_SHIFT) \
93 | (0 << COMN1SW_SHIFT))
94
95#define CONTROL2_LOWPWR_SHIFT (0)
96#define CONTROL2_ADCEN_SHIFT (1)
97#define CONTROL2_CPEN_SHIFT (2)
98#define CONTROL2_SFOUTASRT_SHIFT (3)
99#define CONTROL2_SFOUTORD_SHIFT (4)
100#define CONTROL2_ACCDET_SHIFT (5)
101#define CONTROL2_USBCPINT_SHIFT (6)
102#define CONTROL2_RCPS_SHIFT (7)
103#define CONTROL2_LOWPWR_MASK (0x1 << CONTROL2_LOWPWR_SHIFT)
104#define CONTROL2_ADCEN_MASK (0x1 << CONTROL2_ADCEN_SHIFT)
105#define CONTROL2_CPEN_MASK (0x1 << CONTROL2_CPEN_SHIFT)
106#define CONTROL2_SFOUTASRT_MASK (0x1 << CONTROL2_SFOUTASRT_SHIFT)
107#define CONTROL2_SFOUTORD_MASK (0x1 << CONTROL2_SFOUTORD_SHIFT)
108#define CONTROL2_ACCDET_MASK (0x1 << CONTROL2_ACCDET_SHIFT)
109#define CONTROL2_USBCPINT_MASK (0x1 << CONTROL2_USBCPINT_SHIFT)
110#define CONTROL2_RCPS_MASK (0x1 << CONTROL2_RCPS_SHIFT)
111
112#define CONTROL3_JIGSET_SHIFT (0)
113#define CONTROL3_BTLDSET_SHIFT (2)
114#define CONTROL3_ADCDBSET_SHIFT (4)
115#define CONTROL3_JIGSET_MASK (0x3 << CONTROL3_JIGSET_SHIFT)
116#define CONTROL3_BTLDSET_MASK (0x3 << CONTROL3_BTLDSET_SHIFT)
117#define CONTROL3_ADCDBSET_MASK (0x3 << CONTROL3_ADCDBSET_SHIFT)
118
119enum max77693_muic_adc_debounce_time { 33enum max77693_muic_adc_debounce_time {
120 ADC_DEBOUNCE_TIME_5MS = 0, 34 ADC_DEBOUNCE_TIME_5MS = 0,
121 ADC_DEBOUNCE_TIME_10MS, 35 ADC_DEBOUNCE_TIME_10MS,
@@ -127,8 +41,8 @@ struct max77693_muic_info {
127 struct device *dev; 41 struct device *dev;
128 struct max77693_dev *max77693; 42 struct max77693_dev *max77693;
129 struct extcon_dev *edev; 43 struct extcon_dev *edev;
130 int prev_adc; 44 int prev_cable_type;
131 int prev_adc_gnd; 45 int prev_cable_type_gnd;
132 int prev_chg_type; 46 int prev_chg_type;
133 u8 status[2]; 47 u8 status[2];
134 48
@@ -137,6 +51,13 @@ struct max77693_muic_info {
137 struct mutex mutex; 51 struct mutex mutex;
138}; 52};
139 53
54enum max77693_muic_cable_group {
55 MAX77693_CABLE_GROUP_ADC = 0,
56 MAX77693_CABLE_GROUP_ADC_GND,
57 MAX77693_CABLE_GROUP_CHG,
58 MAX77693_CABLE_GROUP_VBVOLT,
59};
60
140enum max77693_muic_charger_type { 61enum max77693_muic_charger_type {
141 MAX77693_CHARGER_TYPE_NONE = 0, 62 MAX77693_CHARGER_TYPE_NONE = 0,
142 MAX77693_CHARGER_TYPE_USB, 63 MAX77693_CHARGER_TYPE_USB,
@@ -221,21 +142,40 @@ enum max77693_muic_acc_type {
221}; 142};
222 143
223/* MAX77693 MUIC device support below list of accessories(external connector) */ 144/* MAX77693 MUIC device support below list of accessories(external connector) */
224const char *max77693_extcon_cable[] = { 145enum {
225 [0] = "USB", 146 EXTCON_CABLE_USB = 0,
226 [1] = "USB-Host", 147 EXTCON_CABLE_USB_HOST,
227 [2] = "TA", 148 EXTCON_CABLE_TA,
228 [3] = "Fast-charger", 149 EXTCON_CABLE_FAST_CHARGER,
229 [4] = "Slow-charger", 150 EXTCON_CABLE_SLOW_CHARGER,
230 [5] = "Charge-downstream", 151 EXTCON_CABLE_CHARGE_DOWNSTREAM,
231 [6] = "MHL", 152 EXTCON_CABLE_MHL,
232 [7] = "Audio-video-load", 153 EXTCON_CABLE_AUDIO_VIDEO_LOAD,
233 [8] = "Audio-video-noload", 154 EXTCON_CABLE_AUDIO_VIDEO_NOLOAD,
234 [9] = "JIG", 155 EXTCON_CABLE_JIG,
156
157 _EXTCON_CABLE_NUM,
158};
235 159
160const char *max77693_extcon_cable[] = {
161 [EXTCON_CABLE_USB] = "USB",
162 [EXTCON_CABLE_USB_HOST] = "USB-Host",
163 [EXTCON_CABLE_TA] = "TA",
164 [EXTCON_CABLE_FAST_CHARGER] = "Fast-charger",
165 [EXTCON_CABLE_SLOW_CHARGER] = "Slow-charger",
166 [EXTCON_CABLE_CHARGE_DOWNSTREAM] = "Charge-downstream",
167 [EXTCON_CABLE_MHL] = "MHL",
168 [EXTCON_CABLE_AUDIO_VIDEO_LOAD] = "Audio-video-load",
169 [EXTCON_CABLE_AUDIO_VIDEO_NOLOAD] = "Audio-video-noload",
170 [EXTCON_CABLE_JIG] = "JIG",
236 NULL, 171 NULL,
237}; 172};
238 173
174/*
175 * max77693_muic_set_debounce_time - Set the debounce time of ADC
176 * @info: the instance including private data of max77693 MUIC
177 * @time: the debounce time of ADC
178 */
239static int max77693_muic_set_debounce_time(struct max77693_muic_info *info, 179static int max77693_muic_set_debounce_time(struct max77693_muic_info *info,
240 enum max77693_muic_adc_debounce_time time) 180 enum max77693_muic_adc_debounce_time time)
241{ 181{
@@ -262,6 +202,16 @@ static int max77693_muic_set_debounce_time(struct max77693_muic_info *info,
262 return ret; 202 return ret;
263}; 203};
264 204
205/*
206 * max77693_muic_set_path - Set hardware line according to attached cable
207 * @info: the instance including private data of max77693 MUIC
208 * @value: the path according to attached cable
209 * @attached: the state of cable (true:attached, false:detached)
210 *
211 * The max77693 MUIC device share outside H/W line among a varity of cables
212 * so, this function set internal path of H/W line according to the type of
213 * attached cable.
214 */
265static int max77693_muic_set_path(struct max77693_muic_info *info, 215static int max77693_muic_set_path(struct max77693_muic_info *info,
266 u8 val, bool attached) 216 u8 val, bool attached)
267{ 217{
@@ -300,36 +250,169 @@ out:
300 return ret; 250 return ret;
301} 251}
302 252
303static int max77693_muic_adc_ground_handler(struct max77693_muic_info *info, 253/*
304 bool attached) 254 * max77693_muic_get_cable_type - Return cable type and check cable state
255 * @info: the instance including private data of max77693 MUIC
256 * @group: the path according to attached cable
257 * @attached: store cable state and return
258 *
259 * This function check the cable state either attached or detached,
260 * and then divide precise type of cable according to cable group.
261 * - MAX77693_CABLE_GROUP_ADC
262 * - MAX77693_CABLE_GROUP_ADC_GND
263 * - MAX77693_CABLE_GROUP_CHG
264 * - MAX77693_CABLE_GROUP_VBVOLT
265 */
266static int max77693_muic_get_cable_type(struct max77693_muic_info *info,
267 enum max77693_muic_cable_group group, bool *attached)
305{ 268{
306 int ret = 0; 269 int cable_type = 0;
307 int type; 270 int adc;
308 int adc, adc1k, adclow; 271 int adc1k;
272 int adclow;
273 int vbvolt;
274 int chg_type;
275
276 switch (group) {
277 case MAX77693_CABLE_GROUP_ADC:
278 /*
279 * Read ADC value to check cable type and decide cable state
280 * according to cable type
281 */
282 adc = info->status[0] & STATUS1_ADC_MASK;
283 adc >>= STATUS1_ADC_SHIFT;
284
285 /*
286 * Check current cable state/cable type and store cable type
287 * (info->prev_cable_type) for handling cable when cable is
288 * detached.
289 */
290 if (adc == MAX77693_MUIC_ADC_OPEN) {
291 *attached = false;
292
293 cable_type = info->prev_cable_type;
294 info->prev_cable_type = MAX77693_MUIC_ADC_OPEN;
295 } else {
296 *attached = true;
297
298 cable_type = info->prev_cable_type = adc;
299 }
300 break;
301 case MAX77693_CABLE_GROUP_ADC_GND:
302 /*
303 * Read ADC value to check cable type and decide cable state
304 * according to cable type
305 */
306 adc = info->status[0] & STATUS1_ADC_MASK;
307 adc >>= STATUS1_ADC_SHIFT;
309 308
310 if (attached) { 309 /*
310 * Check current cable state/cable type and store cable type
311 * (info->prev_cable_type/_gnd) for handling cable when cable
312 * is detached.
313 */
314 if (adc == MAX77693_MUIC_ADC_OPEN) {
315 *attached = false;
316
317 cable_type = info->prev_cable_type_gnd;
318 info->prev_cable_type_gnd = MAX77693_MUIC_ADC_OPEN;
319 } else {
320 *attached = true;
321
322 adclow = info->status[0] & STATUS1_ADCLOW_MASK;
323 adclow >>= STATUS1_ADCLOW_SHIFT;
324 adc1k = info->status[0] & STATUS1_ADC1K_MASK;
325 adc1k >>= STATUS1_ADC1K_SHIFT;
326
327 vbvolt = info->status[1] & STATUS2_VBVOLT_MASK;
328 vbvolt >>= STATUS2_VBVOLT_SHIFT;
329
330 /**
331 * [0x1][VBVolt][ADCLow][ADC1K]
332 * [0x1 0 0 0 ] : USB_OTG
333 * [0x1 0 1 0 ] : Audio Video Cable with load
334 * [0x1 0 1 1 ] : MHL without charging connector
335 * [0x1 1 1 1 ] : MHL with charging connector
336 */
337 cable_type = ((0x1 << 8)
338 | (vbvolt << 2)
339 | (adclow << 1)
340 | adc1k);
341
342 info->prev_cable_type = adc;
343 info->prev_cable_type_gnd = cable_type;
344 }
345
346 break;
347 case MAX77693_CABLE_GROUP_CHG:
348 /*
349 * Read charger type to check cable type and decide cable state
350 * according to type of charger cable.
351 */
352 chg_type = info->status[1] & STATUS2_CHGTYP_MASK;
353 chg_type >>= STATUS2_CHGTYP_SHIFT;
354
355 if (chg_type == MAX77693_CHARGER_TYPE_NONE) {
356 *attached = false;
357
358 cable_type = info->prev_chg_type;
359 info->prev_chg_type = MAX77693_CHARGER_TYPE_NONE;
360 } else {
361 *attached = true;
362
363 /*
364 * Check current cable state/cable type and store cable
365 * type(info->prev_chg_type) for handling cable when
366 * charger cable is detached.
367 */
368 cable_type = info->prev_chg_type = chg_type;
369 }
370
371 break;
372 case MAX77693_CABLE_GROUP_VBVOLT:
373 /*
374 * Read ADC value to check cable type and decide cable state
375 * according to cable type
376 */
311 adc = info->status[0] & STATUS1_ADC_MASK; 377 adc = info->status[0] & STATUS1_ADC_MASK;
312 adclow = info->status[0] & STATUS1_ADCLOW_MASK; 378 adc >>= STATUS1_ADC_SHIFT;
313 adclow >>= STATUS1_ADCLOW_SHIFT; 379 chg_type = info->status[1] & STATUS2_CHGTYP_MASK;
314 adc1k = info->status[0] & STATUS1_ADC1K_MASK; 380 chg_type >>= STATUS2_CHGTYP_SHIFT;
315 adc1k >>= STATUS1_ADC1K_SHIFT; 381
316 382 if (adc == MAX77693_MUIC_ADC_OPEN
317 /** 383 && chg_type == MAX77693_CHARGER_TYPE_NONE)
318 * [0x1][ADCLow][ADC1K] 384 *attached = false;
319 * [0x1 0 0 ] : USB_OTG 385 else
320 * [0x1 1 0 ] : Audio Video Cable with load 386 *attached = true;
321 * [0x1 1 1 ] : MHL 387
388 /*
389 * Read vbvolt field, if vbvolt is 1,
390 * this cable is used for charging.
322 */ 391 */
323 type = ((0x1 << 8) | (adclow << 1) | adc1k); 392 vbvolt = info->status[1] & STATUS2_VBVOLT_MASK;
393 vbvolt >>= STATUS2_VBVOLT_SHIFT;
394
395 cable_type = vbvolt;
396 break;
397 default:
398 dev_err(info->dev, "Unknown cable group (%d)\n", group);
399 cable_type = -EINVAL;
400 break;
401 }
402
403 return cable_type;
404}
405
406static int max77693_muic_adc_ground_handler(struct max77693_muic_info *info)
407{
408 int cable_type_gnd;
409 int ret = 0;
410 bool attached;
324 411
325 /* Store previous ADC value to handle accessory 412 cable_type_gnd = max77693_muic_get_cable_type(info,
326 when accessory will be detached */ 413 MAX77693_CABLE_GROUP_ADC_GND, &attached);
327 info->prev_adc = adc;
328 info->prev_adc_gnd = type;
329 } else
330 type = info->prev_adc_gnd;
331 414
332 switch (type) { 415 switch (cable_type_gnd) {
333 case MAX77693_MUIC_GND_USB_OTG: 416 case MAX77693_MUIC_GND_USB_OTG:
334 /* USB_OTG */ 417 /* USB_OTG */
335 ret = max77693_muic_set_path(info, CONTROL1_SW_USB, attached); 418 ret = max77693_muic_set_path(info, CONTROL1_SW_USB, attached);
@@ -352,8 +435,6 @@ static int max77693_muic_adc_ground_handler(struct max77693_muic_info *info,
352 default: 435 default:
353 dev_err(info->dev, "failed to detect %s accessory\n", 436 dev_err(info->dev, "failed to detect %s accessory\n",
354 attached ? "attached" : "detached"); 437 attached ? "attached" : "detached");
355 dev_err(info->dev, "- adc:0x%x, adclow:0x%x, adc1k:0x%x\n",
356 adc, adclow, adc1k);
357 ret = -EINVAL; 438 ret = -EINVAL;
358 break; 439 break;
359 } 440 }
@@ -362,45 +443,36 @@ out:
362 return ret; 443 return ret;
363} 444}
364 445
365static int max77693_muic_adc_handler(struct max77693_muic_info *info, 446static int max77693_muic_adc_handler(struct max77693_muic_info *info)
366 int curr_adc, bool attached)
367{ 447{
448 int cable_type;
449 bool attached;
368 int ret = 0; 450 int ret = 0;
369 int adc;
370 451
371 if (attached) { 452 /* Check accessory state which is either detached or attached */
372 /* Store ADC value to handle accessory 453 cable_type = max77693_muic_get_cable_type(info,
373 when accessory will be detached */ 454 MAX77693_CABLE_GROUP_ADC, &attached);
374 info->prev_adc = curr_adc;
375 adc = curr_adc;
376 } else
377 adc = info->prev_adc;
378 455
379 dev_info(info->dev, 456 dev_info(info->dev,
380 "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n", 457 "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
381 attached ? "attached" : "detached", curr_adc, info->prev_adc); 458 attached ? "attached" : "detached", cable_type,
459 info->prev_cable_type);
382 460
383 switch (adc) { 461 switch (cable_type) {
384 case MAX77693_MUIC_ADC_GROUND: 462 case MAX77693_MUIC_ADC_GROUND:
385 /* USB_OTG/MHL/Audio */ 463 /* USB_OTG/MHL/Audio */
386 max77693_muic_adc_ground_handler(info, attached); 464 max77693_muic_adc_ground_handler(info);
387 break; 465 break;
388 case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF: 466 case MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF:
389 case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON: 467 case MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON:
390 /* USB */
391 ret = max77693_muic_set_path(info, CONTROL1_SW_USB, attached);
392 if (ret < 0)
393 goto out;
394 extcon_set_cable_state(info->edev, "USB", attached);
395 break;
396 case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF: 468 case MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF:
397 case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON:
398 /* JIG */ 469 /* JIG */
399 ret = max77693_muic_set_path(info, CONTROL1_SW_UART, attached); 470 ret = max77693_muic_set_path(info, CONTROL1_SW_UART, attached);
400 if (ret < 0) 471 if (ret < 0)
401 goto out; 472 goto out;
402 extcon_set_cable_state(info->edev, "JIG", attached); 473 extcon_set_cable_state(info->edev, "JIG", attached);
403 break; 474 break;
475 case MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON:
404 case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE: 476 case MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE:
405 /* Audio Video cable with no-load */ 477 /* Audio Video cable with no-load */
406 ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached); 478 ret = max77693_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
@@ -439,12 +511,12 @@ static int max77693_muic_adc_handler(struct max77693_muic_info *info,
439 proper operation when this accessory is attached/detached. */ 511 proper operation when this accessory is attached/detached. */
440 dev_info(info->dev, 512 dev_info(info->dev,
441 "accessory is %s but it isn't used (adc:0x%x)\n", 513 "accessory is %s but it isn't used (adc:0x%x)\n",
442 attached ? "attached" : "detached", adc); 514 attached ? "attached" : "detached", cable_type);
443 goto out; 515 goto out;
444 default: 516 default:
445 dev_err(info->dev, 517 dev_err(info->dev,
446 "failed to detect %s accessory (adc:0x%x)\n", 518 "failed to detect %s accessory (adc:0x%x)\n",
447 attached ? "attached" : "detached", adc); 519 attached ? "attached" : "detached", cable_type);
448 ret = -EINVAL; 520 ret = -EINVAL;
449 goto out; 521 goto out;
450 } 522 }
@@ -453,24 +525,19 @@ out:
453 return ret; 525 return ret;
454} 526}
455 527
456static int max77693_muic_chg_handler(struct max77693_muic_info *info, 528static int max77693_muic_chg_handler(struct max77693_muic_info *info)
457 int curr_chg_type, bool attached)
458{ 529{
459 int ret = 0;
460 int chg_type; 530 int chg_type;
531 bool attached;
532 int ret = 0;
461 533
462 if (attached) { 534 chg_type = max77693_muic_get_cable_type(info,
463 /* Store previous charger type to control 535 MAX77693_CABLE_GROUP_CHG, &attached);
464 when charger accessory will be detached */
465 info->prev_chg_type = curr_chg_type;
466 chg_type = curr_chg_type;
467 } else
468 chg_type = info->prev_chg_type;
469 536
470 dev_info(info->dev, 537 dev_info(info->dev,
471 "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n", 538 "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n",
472 attached ? "attached" : "detached", 539 attached ? "attached" : "detached",
473 curr_chg_type, info->prev_chg_type); 540 chg_type, info->prev_chg_type);
474 541
475 switch (chg_type) { 542 switch (chg_type) {
476 case MAX77693_CHARGER_TYPE_USB: 543 case MAX77693_CHARGER_TYPE_USB:
@@ -510,10 +577,8 @@ static void max77693_muic_irq_work(struct work_struct *work)
510{ 577{
511 struct max77693_muic_info *info = container_of(work, 578 struct max77693_muic_info *info = container_of(work,
512 struct max77693_muic_info, irq_work); 579 struct max77693_muic_info, irq_work);
513 int curr_adc, curr_chg_type;
514 int irq_type = -1; 580 int irq_type = -1;
515 int i, ret = 0; 581 int i, ret = 0;
516 bool attached = true;
517 582
518 if (!info->edev) 583 if (!info->edev)
519 return; 584 return;
@@ -539,14 +604,7 @@ static void max77693_muic_irq_work(struct work_struct *work)
539 case MAX77693_MUIC_IRQ_INT1_ADC1K: 604 case MAX77693_MUIC_IRQ_INT1_ADC1K:
540 /* Handle all of accessory except for 605 /* Handle all of accessory except for
541 type of charger accessory */ 606 type of charger accessory */
542 curr_adc = info->status[0] & STATUS1_ADC_MASK; 607 ret = max77693_muic_adc_handler(info);
543 curr_adc >>= STATUS1_ADC_SHIFT;
544
545 /* Check accessory state which is either detached or attached */
546 if (curr_adc == MAX77693_MUIC_ADC_OPEN)
547 attached = false;
548
549 ret = max77693_muic_adc_handler(info, curr_adc, attached);
550 break; 608 break;
551 case MAX77693_MUIC_IRQ_INT2_CHGTYP: 609 case MAX77693_MUIC_IRQ_INT2_CHGTYP:
552 case MAX77693_MUIC_IRQ_INT2_CHGDETREUN: 610 case MAX77693_MUIC_IRQ_INT2_CHGDETREUN:
@@ -555,15 +613,7 @@ static void max77693_muic_irq_work(struct work_struct *work)
555 case MAX77693_MUIC_IRQ_INT2_VBVOLT: 613 case MAX77693_MUIC_IRQ_INT2_VBVOLT:
556 case MAX77693_MUIC_IRQ_INT2_VIDRM: 614 case MAX77693_MUIC_IRQ_INT2_VIDRM:
557 /* Handle charger accessory */ 615 /* Handle charger accessory */
558 curr_chg_type = info->status[1] & STATUS2_CHGTYP_MASK; 616 ret = max77693_muic_chg_handler(info);
559 curr_chg_type >>= STATUS2_CHGTYP_SHIFT;
560
561 /* Check charger accessory state which
562 is either detached or attached */
563 if (curr_chg_type == MAX77693_CHARGER_TYPE_NONE)
564 attached = false;
565
566 ret = max77693_muic_chg_handler(info, curr_chg_type, attached);
567 break; 617 break;
568 case MAX77693_MUIC_IRQ_INT3_EOC: 618 case MAX77693_MUIC_IRQ_INT3_EOC:
569 case MAX77693_MUIC_IRQ_INT3_CGMBC: 619 case MAX77693_MUIC_IRQ_INT3_CGMBC:
@@ -604,7 +654,9 @@ static struct regmap_config max77693_muic_regmap_config = {
604static int max77693_muic_detect_accessory(struct max77693_muic_info *info) 654static int max77693_muic_detect_accessory(struct max77693_muic_info *info)
605{ 655{
606 int ret = 0; 656 int ret = 0;
607 int adc, chg_type; 657 int adc;
658 int chg_type;
659 bool attached;
608 660
609 mutex_lock(&info->mutex); 661 mutex_lock(&info->mutex);
610 662
@@ -617,34 +669,24 @@ static int max77693_muic_detect_accessory(struct max77693_muic_info *info)
617 return -EINVAL; 669 return -EINVAL;
618 } 670 }
619 671
620 adc = info->status[0] & STATUS1_ADC_MASK; 672 adc = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_ADC,
621 adc >>= STATUS1_ADC_SHIFT; 673 &attached);
622 674 if (attached && adc != MAX77693_MUIC_ADC_OPEN) {
623 if (adc != MAX77693_MUIC_ADC_OPEN) { 675 ret = max77693_muic_adc_handler(info);
624 dev_info(info->dev,
625 "external connector is attached (adc:0x%02x)\n", adc);
626
627 ret = max77693_muic_adc_handler(info, adc, true);
628 if (ret < 0) 676 if (ret < 0)
629 dev_err(info->dev, "failed to detect accessory\n"); 677 dev_err(info->dev, "Cannot detect accessory\n");
630 goto out;
631 } 678 }
632 679
633 chg_type = info->status[1] & STATUS2_CHGTYP_MASK; 680 chg_type = max77693_muic_get_cable_type(info, MAX77693_CABLE_GROUP_CHG,
634 chg_type >>= STATUS2_CHGTYP_SHIFT; 681 &attached);
635 682 if (attached && chg_type != MAX77693_CHARGER_TYPE_NONE) {
636 if (chg_type != MAX77693_CHARGER_TYPE_NONE) { 683 ret = max77693_muic_chg_handler(info);
637 dev_info(info->dev,
638 "external connector is attached (chg_type:0x%x)\n",
639 chg_type);
640
641 max77693_muic_chg_handler(info, chg_type, true);
642 if (ret < 0) 684 if (ret < 0)
643 dev_err(info->dev, "failed to detect charger accessory\n"); 685 dev_err(info->dev, "Cannot detect charger accessory\n");
644 } 686 }
645 687
646out:
647 mutex_unlock(&info->mutex); 688 mutex_unlock(&info->mutex);
689
648 return ret; 690 return ret;
649} 691}
650 692