Skip to content

Commit 4b28b25

Browse files
charleskeepaxchanwoochoi
authored andcommitted
extcon: arizona: Factor out microphone and button detection
Continue refactoring the microphone detect handling by factoring out the handling for microphone detection and button detection into separate functions. This both makes the code a little clearer and prepares for some planned future refactoring to make the state handling in the driver more explicit. Signed-off-by: Charles Keepax <[email protected]> Signed-off-by: Chanwoo Choi <[email protected]>
1 parent 7e14fc4 commit 4b28b25

File tree

1 file changed

+71
-44
lines changed

1 file changed

+71
-44
lines changed

drivers/extcon/extcon-arizona.c

Lines changed: 71 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -872,38 +872,18 @@ static int arizona_micd_read(struct arizona_extcon_info *info)
872872
return val;
873873
}
874874

875-
static void arizona_micd_detect(struct work_struct *work)
875+
static int arizona_micdet_reading(void *priv)
876876
{
877-
struct arizona_extcon_info *info = container_of(work,
878-
struct arizona_extcon_info,
879-
micd_detect_work.work);
877+
struct arizona_extcon_info *info = priv;
880878
struct arizona *arizona = info->arizona;
881-
unsigned int val = 0, lvl;
882-
int ret, i, key;
883-
884-
cancel_delayed_work_sync(&info->micd_timeout_work);
885-
886-
mutex_lock(&info->lock);
887-
888-
/* If the cable was removed while measuring ignore the result */
889-
ret = extcon_get_state(info->edev, EXTCON_MECHANICAL);
890-
if (ret < 0) {
891-
dev_err(arizona->dev, "Failed to check cable state: %d\n",
892-
ret);
893-
mutex_unlock(&info->lock);
894-
return;
895-
} else if (!ret) {
896-
dev_dbg(arizona->dev, "Ignoring MICDET for removed cable\n");
897-
mutex_unlock(&info->lock);
898-
return;
899-
}
879+
int ret, val;
900880

901881
if (info->detecting && arizona->pdata.micd_software_compare)
902882
ret = arizona_micd_adc_read(info);
903883
else
904884
ret = arizona_micd_read(info);
905885
if (ret < 0)
906-
goto handled;
886+
return ret;
907887

908888
val = ret;
909889

@@ -913,11 +893,11 @@ static void arizona_micd_detect(struct work_struct *work)
913893
info->mic = false;
914894
info->detecting = false;
915895
arizona_identify_headphone(info);
916-
goto handled;
896+
return 0;
917897
}
918898

919899
/* If we got a high impedence we should have a headset, report it. */
920-
if (info->detecting && (val & ARIZONA_MICD_LVL_8)) {
900+
if (val & ARIZONA_MICD_LVL_8) {
921901
info->mic = true;
922902
info->detecting = false;
923903

@@ -936,7 +916,7 @@ static void arizona_micd_detect(struct work_struct *work)
936916
ret);
937917
}
938918

939-
goto handled;
919+
return 0;
940920
}
941921

942922
/* If we detected a lower impedence during initial startup
@@ -945,7 +925,7 @@ static void arizona_micd_detect(struct work_struct *work)
945925
* plain headphones. If both polarities report a low
946926
* impedence then give up and report headphones.
947927
*/
948-
if (info->detecting && (val & MICD_LVL_1_TO_7)) {
928+
if (val & MICD_LVL_1_TO_7) {
949929
if (info->jack_flips >= info->micd_num_modes * 10) {
950930
dev_dbg(arizona->dev, "Detected HP/line\n");
951931

@@ -959,11 +939,43 @@ static void arizona_micd_detect(struct work_struct *work)
959939
arizona_extcon_set_mode(info, info->micd_mode);
960940

961941
info->jack_flips++;
942+
943+
if (arizona->pdata.micd_software_compare)
944+
regmap_update_bits(arizona->regmap,
945+
ARIZONA_MIC_DETECT_1,
946+
ARIZONA_MICD_ENA,
947+
ARIZONA_MICD_ENA);
948+
949+
queue_delayed_work(system_power_efficient_wq,
950+
&info->micd_timeout_work,
951+
msecs_to_jiffies(arizona->pdata.micd_timeout));
962952
}
963953

964-
goto handled;
954+
return 0;
965955
}
966956

957+
/*
958+
* If we're still detecting and we detect a short then we've
959+
* got a headphone.
960+
*/
961+
dev_dbg(arizona->dev, "Headphone detected\n");
962+
info->detecting = false;
963+
964+
arizona_identify_headphone(info);
965+
966+
return 0;
967+
}
968+
969+
static int arizona_button_reading(void *priv)
970+
{
971+
struct arizona_extcon_info *info = priv;
972+
struct arizona *arizona = info->arizona;
973+
int val, key, lvl, i;
974+
975+
val = arizona_micd_read(info);
976+
if (val < 0)
977+
return val;
978+
967979
/*
968980
* If we're still detecting and we detect a short then we've
969981
* got a headphone. Otherwise it's a button press.
@@ -986,11 +998,6 @@ static void arizona_micd_detect(struct work_struct *work)
986998
} else {
987999
dev_err(arizona->dev, "Button out of range\n");
9881000
}
989-
} else if (info->detecting) {
990-
dev_dbg(arizona->dev, "Headphone detected\n");
991-
info->detecting = false;
992-
993-
arizona_identify_headphone(info);
9941001
} else {
9951002
dev_warn(arizona->dev, "Button with no mic: %x\n",
9961003
val);
@@ -1004,19 +1011,39 @@ static void arizona_micd_detect(struct work_struct *work)
10041011
arizona_extcon_pulse_micbias(info);
10051012
}
10061013

1007-
handled:
1008-
if (info->detecting) {
1009-
if (arizona->pdata.micd_software_compare)
1010-
regmap_update_bits(arizona->regmap,
1011-
ARIZONA_MIC_DETECT_1,
1012-
ARIZONA_MICD_ENA,
1013-
ARIZONA_MICD_ENA);
1014+
return 0;
1015+
}
10141016

1015-
queue_delayed_work(system_power_efficient_wq,
1016-
&info->micd_timeout_work,
1017-
msecs_to_jiffies(arizona->pdata.micd_timeout));
1017+
static void arizona_micd_detect(struct work_struct *work)
1018+
{
1019+
struct arizona_extcon_info *info = container_of(work,
1020+
struct arizona_extcon_info,
1021+
micd_detect_work.work);
1022+
struct arizona *arizona = info->arizona;
1023+
int ret;
1024+
1025+
cancel_delayed_work_sync(&info->micd_timeout_work);
1026+
1027+
mutex_lock(&info->lock);
1028+
1029+
/* If the cable was removed while measuring ignore the result */
1030+
ret = extcon_get_state(info->edev, EXTCON_MECHANICAL);
1031+
if (ret < 0) {
1032+
dev_err(arizona->dev, "Failed to check cable state: %d\n",
1033+
ret);
1034+
mutex_unlock(&info->lock);
1035+
return;
1036+
} else if (!ret) {
1037+
dev_dbg(arizona->dev, "Ignoring MICDET for removed cable\n");
1038+
mutex_unlock(&info->lock);
1039+
return;
10181040
}
10191041

1042+
if (info->detecting)
1043+
arizona_micdet_reading(info);
1044+
else
1045+
arizona_button_reading(info);
1046+
10201047
pm_runtime_mark_last_busy(info->dev);
10211048
mutex_unlock(&info->lock);
10221049
}

0 commit comments

Comments
 (0)