Skip to contentSkip to conversation

extcon: arizona: Wait for any running HPDETs to complete on jack removal

PATCH In review Charles Keepax 2 participants 5 messages Jan 25, 2017 External connector subsystem (extcon)

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 extcon: arizona: Wait for any running HPDETs to complete on jack removal

As the HPDET can't be aborted mid way through we should not allow any new insertion to be processed until the previous HPDET has finished. It is very unlikely but with low enough debounce settings you could start a new HPDET before the old one has completed, which results in an erroneous reading.

---

Changes since v1:
- Added defines for the count and delay
- Added a comment to explain why we call arizona_hpdet_wait

Thanks,
Charles

 drivers/extcon/extcon-arizona.c | 45 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
drivers/extcon/extcon-arizona.c +45 -0
index ed78b7c..df98d84 100644
@@ -1049,6 +1049,42 @@ static void arizona_hpdet_work(struct work_struct *work)
 	mutex_unlock(&info->lock);
 }
 
+#define ARIZONA_HPDET_WAIT_COUNT 15
+#define ARIZONA_HPDET_WAIT_DELAY_MS 20
+
+static int arizona_hpdet_wait(struct arizona_extcon_info *info)
+{
+	struct arizona *arizona = info->arizona;
+	unsigned int val;
+	int i, ret;
+
+	for (i = 0; i < ARIZONA_HPDET_WAIT_COUNT; i++) {
+		ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2,
+				  &val);
+		if (ret) {
+			dev_err(arizona->dev,
+				"Failed to read HPDET state: %d\n", ret);
+			return ret;
+		}
+
+		switch (info->hpdet_ip_version) {
+		case 0:
+			if (val & ARIZONA_HP_DONE)
+				return 0;
+			break;
+		default:
+			if (val & ARIZONA_HP_DONE_B)
+				return 0;
+			break;
+		}
+
+		msleep(ARIZONA_HPDET_WAIT_DELAY_MS);
+	}
+
+	dev_err(arizona->dev, "HPDET did not appear to complete\n");
+	return -ETIMEDOUT;
+}
+
 static irqreturn_t arizona_jackdet(int irq, void *data)
 {
 	struct arizona_extcon_info *info = data;
@@ -1155,6 +1191,15 @@ static irqreturn_t arizona_jackdet(int irq, void *data)
 					"Removal report failed: %d\n", ret);
 		}
 
+		/*
+		 * If the jack was removed during a headphone detection we
+		 * need to wait for the headphone detection to finish, as
+		 * it can not be aborted. We don't want to be able to start
+		 * a new headphone detection from a fresh insert until this
+		 * one is finished.
+		 */
+		arizona_hpdet_wait(info);
+
 		regmap_update_bits(arizona->regmap,
 				   ARIZONA_JACK_DETECT_DEBOUNCE,
 				   ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB,
Signature · 1 line
2.1.4
maintainer
#1 lore raw
Quoted 67 lines · Charles Keepax

As the HPDET can't be aborted mid way through we should not allow any new insertion to be processed until the previous HPDET has finished. It is very unlikely but with low enough debounce settings you could start a new HPDET before the old one has completed, which results in an erroneous reading.

---

Changes since v1:
- Added defines for the count and delay
- Added a comment to explain why we call arizona_hpdet_wait

Thanks,
Charles

 drivers/extcon/extcon-arizona.c | 45 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
drivers/extcon/extcon-arizona.c +36 -0
index ed78b7c..df98d84 100644
@@ -1049,6 +1049,42 @@ static void arizona_hpdet_work(struct work_struct *work)
 	mutex_unlock(&info->lock);
 }
 
+#define ARIZONA_HPDET_WAIT_COUNT 15
+#define ARIZONA_HPDET_WAIT_DELAY_MS 20
+
+static int arizona_hpdet_wait(struct arizona_extcon_info *info)
+{
+	struct arizona *arizona = info->arizona;
+	unsigned int val;
+	int i, ret;
+
+	for (i = 0; i < ARIZONA_HPDET_WAIT_COUNT; i++) {
+		ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2,
+				  &val);
+		if (ret) {
+			dev_err(arizona->dev,
+				"Failed to read HPDET state: %d\n", ret);
+			return ret;
+		}
+
+		switch (info->hpdet_ip_version) {
+		case 0:
+			if (val & ARIZONA_HP_DONE)
+				return 0;
+			break;
+		default:
+			if (val & ARIZONA_HP_DONE_B)
+				return 0;
+			break;
+		}
+
+		msleep(ARIZONA_HPDET_WAIT_DELAY_MS);
+	}
+
+	dev_err(arizona->dev, "HPDET did not appear to complete\n");
+	return -ETIMEDOUT;
+}
+
 static irqreturn_t arizona_jackdet(int irq, void *data)
 {
 	struct arizona_extcon_info *info = data;
@@ -1155,6 +1191,15 @@ static irqreturn_t arizona_jackdet(int irq, void *data)
 					"Removal report failed: %d\n", ret);

drivers/extcon/extcon-arizona.c static irqreturn_t arizona_jackdet(int irq, void *data) · line 1201

Quoted 10 lines
drivers/extcon/extcon-arizona.c +8 -0
 		}
 
+		/*
+		 * If the jack was removed during a headphone detection we
+		 * need to wait for the headphone detection to finish, as
+		 * it can not be aborted. We don't want to be able to start
+		 * a new headphone detection from a fresh insert until this
+		 * one is finished.
+		 */
+		arizona_hpdet_wait(info);

If there is no necessary to handle the error return value,
I recommend that you better to use the dev_warn() instead of dev_err().

	dev_warn(arizona->dev, "HPDET did not appear to complete\n");

How about changing the debug level with dev_warn()?

Quoted 5 lines
drivers/extcon/extcon-arizona.c +1 -0
+
 		regmap_update_bits(arizona->regmap,
 				   ARIZONA_JACK_DETECT_DEBOUNCE,
 				   ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB,
Signature · 3 lines
Best Regards,
Chanwoo Choi
Samsung Electronics
#2 lore raw
Quoted 20 lines · Chanwoo Choi
Quoted 7 lines · Charles Keepax

As the HPDET can't be aborted mid way through we should not allow any new insertion to be processed until the previous HPDET has finished. It is very unlikely but with low enough debounce settings you could start a new HPDET before the old one has completed, which results in an erroneous reading.

---

drivers/extcon/extcon-arizona.c static irqreturn_t arizona_jackdet(int irq, void *data) · line 1201

Quoted 8 lines
drivers/extcon/extcon-arizona.c +8 -0
+		/*
+		 * If the jack was removed during a headphone detection we
+		 * need to wait for the headphone detection to finish, as
+		 * it can not be aborted. We don't want to be able to start
+		 * a new headphone detection from a fresh insert until this
+		 * one is finished.
+		 */
+		arizona_hpdet_wait(info);

If there is no necessary to handle the error return value,
I recommend that you better to use the dev_warn() instead of dev_err().

Quoted 5 lines
	dev_warn(arizona->dev, "HPDET did not appear to complete\n");

How about changing the debug level with dev_warn()?

Yeah can do.

Thanks,
Charles

maintainer
#3 lore raw
In reply to Charles Keepax

Hi,

I modified some minor issue and added my comment on below. After modified them by myself, Applied it.

Quoted 23 lines · Charles Keepax

As the HPDET can't be aborted mid way through we should not allow any new insertion to be processed until the previous HPDET has finished. It is very unlikely but with low enough debounce settings you could start a new HPDET before the old one has completed, which results in an erroneous reading.

---

Changes since v1:
- Added defines for the count and delay
- Added a comment to explain why we call arizona_hpdet_wait

Thanks,
Charles

 drivers/extcon/extcon-arizona.c | 45 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
drivers/extcon/extcon-arizona.c +0 -0
index ed78b7c..df98d84 100644

drivers/extcon/extcon-arizona.c static void arizona_hpdet_work(struct work_struct *work) · line 1053

Quoted 5 lines
drivers/extcon/extcon-arizona.c +2 -0
@@ -1049,6 +1049,42 @@ static void arizona_hpdet_work(struct work_struct *work)
 	mutex_unlock(&info->lock);
 }
 
+#define ARIZONA_HPDET_WAIT_COUNT 15
+#define ARIZONA_HPDET_WAIT_DELAY_MS 20

Move these definitions on the top.

drivers/extcon/extcon-arizona.c static void arizona_hpdet_work(struct work_struct *work) · line 1063

Quoted 10 lines
drivers/extcon/extcon-arizona.c +10 -0
+
+static int arizona_hpdet_wait(struct arizona_extcon_info *info)
+{
+	struct arizona *arizona = info->arizona;
+	unsigned int val;
+	int i, ret;
+
+	for (i = 0; i < ARIZONA_HPDET_WAIT_COUNT; i++) {
+		ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2,
+				  &val);

Remove the space indentation. I prefer to use only tab indentation.

Quoted 11 lines
drivers/extcon/extcon-arizona.c +11 -0
+		if (ret) {
+			dev_err(arizona->dev,
+				"Failed to read HPDET state: %d\n", ret);
+			return ret;
+		}
+
+		switch (info->hpdet_ip_version) {
+		case 0:
+			if (val & ARIZONA_HP_DONE)
+				return 0;
+			break;

drivers/extcon/extcon-arizona.c static void arizona_hpdet_work(struct work_struct *work) · line 1084

Quoted 10 lines
drivers/extcon/extcon-arizona.c +10 -0
+		default:
+			if (val & ARIZONA_HP_DONE_B)
+				return 0;
+			break;
+		}
+
+		msleep(ARIZONA_HPDET_WAIT_DELAY_MS);
+	}
+
+	dev_err(arizona->dev, "HPDET did not appear to complete\n");

Use dev_warn() instead of dev_err() because this message just warn the current status.

Quoted 23 lines
drivers/extcon/extcon-arizona.c +12 -0
+	return -ETIMEDOUT;
+}
+
 static irqreturn_t arizona_jackdet(int irq, void *data)
 {
 	struct arizona_extcon_info *info = data;
@@ -1155,6 +1191,15 @@ static irqreturn_t arizona_jackdet(int irq, void *data)
 					"Removal report failed: %d\n", ret);
 		}
 
+		/*
+		 * If the jack was removed during a headphone detection we
+		 * need to wait for the headphone detection to finish, as
+		 * it can not be aborted. We don't want to be able to start
+		 * a new headphone detection from a fresh insert until this
+		 * one is finished.
+		 */
+		arizona_hpdet_wait(info);
+
 		regmap_update_bits(arizona->regmap,
 				   ARIZONA_JACK_DETECT_DEBOUNCE,
 				   ARIZONA_MICD_CLAMP_DB | ARIZONA_JD1_DB,
Signature · 3 lines
Best Regards,
Chanwoo Choi
Samsung Electronics
#4 lore raw
Quoted 11 lines · Chanwoo Choi

Hi,

I modified some minor issue and added my comment on below. After modified them by myself, Applied it.

Quoted 5 lines · Charles Keepax

As the HPDET can't be aborted mid way through we should not allow any new insertion to be processed until the previous HPDET has finished. It is very unlikely but with low enough debounce settings you could start a new HPDET before the old one has completed, which results in an erroneous reading.

Quoted 7 lines

drivers/extcon/extcon-arizona.c static void arizona_hpdet_work(struct work_struct *work) · line 1053

Quoted 4 lines

---

drivers/extcon/extcon-arizona.c +2 -0
+#define ARIZONA_HPDET_WAIT_COUNT 15
+#define ARIZONA_HPDET_WAIT_DELAY_MS 20

Move these definitions on the top.

You can make this change if it is your preference, but generally I feel this makes the code less clear. Previously you could look at this bit of the code locally and clearly see what was happening now the reader will need to spin all the way up to the top of the file probably twice.

Quoted 4 lines
Quoted 4 lines
drivers/extcon/extcon-arizona.c +4 -0
+
+static int arizona_hpdet_wait(struct arizona_extcon_info *info)
+{
+	struct arizona *arizona = info->arizona;
Quoted 9 lines

drivers/extcon/extcon-arizona.c static void arizona_hpdet_work(struct work_struct *work) · line 1063

Quoted 6 lines
drivers/extcon/extcon-arizona.c +6 -0
+	unsigned int val;
+	int i, ret;
+
+	for (i = 0; i < ARIZONA_HPDET_WAIT_COUNT; i++) {
+		ret = regmap_read(arizona->regmap, ARIZONA_HEADPHONE_DETECT_2,
+				  &val);

Remove the space indentation. I prefer to use only tab indentation.

Again I don't mind, but my understanding was this was the preferred style in the kernel and checkpatch --strict will warn if you remove the spaces. It does also match the style of the rest of the file itself.

Thanks,
Charles