Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp3965428ybl; Mon, 9 Dec 2019 03:10:30 -0800 (PST) X-Google-Smtp-Source: APXvYqxJZaLCxd+Wmqxp/r1xH7MA8WfHHmaTosA3UEHj1XL9d80FkyO1OSY1Kk1sq4QHqJ6tFyeE X-Received: by 2002:a05:6808:191:: with SMTP id w17mr22617433oic.29.1575889830466; Mon, 09 Dec 2019 03:10:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1575889830; cv=none; d=google.com; s=arc-20160816; b=I1iCfaIKn0ONUpFTb27JcMeDtdKO2hT6Wh+kqqwvA7Zzvq12hqzxboHL2uxHHEuTcI +ouN5GjZL2vUgQ5iKH+/wou8mUB37AtLO1HBObqSjlzJLU80jV8yHASmsfAXhhKKVZg0 p/thYLmuQnIik0Tda1kA37MxbkA2n7wsmQhWCTUgPbIrFC331sLt22YfnXgjr1rnn5Xk u9QOpncMlH+WOAU+3to+d2VToFVG31WtCkMEOlvhVDYk5T8g7MS2IUqGRstbMBrzwOx6 PDfT7LKyab+OeIJWS9LnuMSXa8U4GBmINGPv7f/XrcdMc6Ym6QPT00t20PPa/ihAV8vr HKkg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=E8Gko3t1/vmDhbyLkG6oaY0YzKBD04P+NBZ0WSQsnZk=; b=ou7lXwN/l8AwHAKjk3eP/p4Grle7/4qHXTvtH6AfzZbchqDZeBcLuyVx0zkBTPPpBz 3tL+omnBUGiJyCZvNZBLwqzZ4C2wLyD3sEh69GVcQXAdfsjtKmKHTMc9F8vJwQXFcLIE hr4K8Bevhjsj2pId5eOOrtY/K1ZRdC6xechpd65L8tu2A0OopZCRobjyIdeTtm6EClzC T9tZfzvcRwBsxm3xFU8c3CFqt6p868cZia1w4x5HaT1J/6fz9TQFEt/y26G9pwyXSzOC TlSo8Wo206f/iIsHCB2BjYJwMRslpa0tSsDLu5af37/6y+/uszugTrBjgbefrPqJIojx RAqA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cirrus.com header.s=PODMain02222019 header.b=kVaxjB2m; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=opensource.cirrus.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r1si11962917otn.150.2019.12.09.03.10.19; Mon, 09 Dec 2019 03:10:30 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@cirrus.com header.s=PODMain02222019 header.b=kVaxjB2m; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=opensource.cirrus.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727777AbfLILJg (ORCPT + 99 others); Mon, 9 Dec 2019 06:09:36 -0500 Received: from mx0a-001ae601.pphosted.com ([67.231.149.25]:23858 "EHLO mx0b-001ae601.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727580AbfLILJa (ORCPT ); Mon, 9 Dec 2019 06:09:30 -0500 Received: from pps.filterd (m0077473.ppops.net [127.0.0.1]) by mx0a-001ae601.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id xB9B9QlV029232; Mon, 9 Dec 2019 05:09:28 -0600 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cirrus.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-type; s=PODMain02222019; bh=E8Gko3t1/vmDhbyLkG6oaY0YzKBD04P+NBZ0WSQsnZk=; b=kVaxjB2mSa5m1m9e/QApHdfxaCeDCVMllOTyPtvFofJFpIwwXtLVveAh1rcKcrs8+gy/ V1VzCA6FPL+z346wstGSnpNbVZPKyuVoHrOg8z/CR3TOyTBl7xDQOYo8a53roou23vV6 AqXTIPokhLQQZUbGPxO5UxKRZG6YdAYLnhh9KlM9JCe98rJEkfryW859Sjp8gm4CzYEZ rR+RJFwa5LdJgViRU9Wsgi6msUJAdL61H6tlzcX8oFb48/akVAcTlEXhJiO4SocoOD/2 x2Hb+5IOg/8aVT4Gfskr4Mm10I/jgylps3cNZ2JlZ3I9veUR4OmQIbAYeflrcsNgSq1q RQ== Authentication-Results: ppops.net; spf=fail smtp.mailfrom=ckeepax@opensource.cirrus.com Received: from ediex02.ad.cirrus.com ([5.172.152.52]) by mx0a-001ae601.pphosted.com with ESMTP id 2wrac6j0mt-6 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT); Mon, 09 Dec 2019 05:09:28 -0600 Received: from EDIEX01.ad.cirrus.com (198.61.84.80) by EDIEX02.ad.cirrus.com (198.61.84.81) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1591.10; Mon, 9 Dec 2019 11:09:16 +0000 Received: from ediswmail.ad.cirrus.com (198.61.86.93) by EDIEX01.ad.cirrus.com (198.61.84.80) with Microsoft SMTP Server id 15.1.1591.10 via Frontend Transport; Mon, 9 Dec 2019 11:09:16 +0000 Received: from algalon.ad.cirrus.com (algalon.ad.cirrus.com [198.90.251.122]) by ediswmail.ad.cirrus.com (Postfix) with ESMTP id A0E832D1; Mon, 9 Dec 2019 11:09:16 +0000 (UTC) From: Charles Keepax To: CC: , , Subject: [PATCH 10/10] extcon: arizona: Factor out microphone and button detection Date: Mon, 9 Dec 2019 11:09:16 +0000 Message-ID: <20191209110916.29524-10-ckeepax@opensource.cirrus.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20191209110916.29524-1-ckeepax@opensource.cirrus.com> References: <20191209110916.29524-1-ckeepax@opensource.cirrus.com> MIME-Version: 1.0 Content-Type: text/plain X-Proofpoint-SPF-Result: fail X-Proofpoint-SPF-Record: v=spf1 include:spf-001ae601.pphosted.com include:spf.protection.outlook.com -all X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 lowpriorityscore=0 mlxlogscore=999 spamscore=0 mlxscore=0 clxscore=1015 adultscore=0 impostorscore=0 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-1910280000 definitions=main-1912090095 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- drivers/extcon/extcon-arizona.c | 115 +++++++++++++++++++++++++--------------- 1 file changed, 71 insertions(+), 44 deletions(-) diff --git a/drivers/extcon/extcon-arizona.c b/drivers/extcon/extcon-arizona.c index b09a9a8ce98bc..7401733db08bb 100644 --- a/drivers/extcon/extcon-arizona.c +++ b/drivers/extcon/extcon-arizona.c @@ -872,38 +872,18 @@ static int arizona_micd_read(struct arizona_extcon_info *info) return val; } -static void arizona_micd_detect(struct work_struct *work) +static int arizona_micdet_reading(void *priv) { - struct arizona_extcon_info *info = container_of(work, - struct arizona_extcon_info, - micd_detect_work.work); + struct arizona_extcon_info *info = priv; struct arizona *arizona = info->arizona; - unsigned int val = 0, lvl; - int ret, i, key; - - cancel_delayed_work_sync(&info->micd_timeout_work); - - mutex_lock(&info->lock); - - /* If the cable was removed while measuring ignore the result */ - ret = extcon_get_state(info->edev, EXTCON_MECHANICAL); - if (ret < 0) { - dev_err(arizona->dev, "Failed to check cable state: %d\n", - ret); - mutex_unlock(&info->lock); - return; - } else if (!ret) { - dev_dbg(arizona->dev, "Ignoring MICDET for removed cable\n"); - mutex_unlock(&info->lock); - return; - } + int ret, val; if (info->detecting && arizona->pdata.micd_software_compare) ret = arizona_micd_adc_read(info); else ret = arizona_micd_read(info); if (ret < 0) - goto handled; + return ret; val = ret; @@ -913,11 +893,11 @@ static void arizona_micd_detect(struct work_struct *work) info->mic = false; info->detecting = false; arizona_identify_headphone(info); - goto handled; + return 0; } /* If we got a high impedence we should have a headset, report it. */ - if (info->detecting && (val & ARIZONA_MICD_LVL_8)) { + if (val & ARIZONA_MICD_LVL_8) { info->mic = true; info->detecting = false; @@ -936,7 +916,7 @@ static void arizona_micd_detect(struct work_struct *work) ret); } - goto handled; + return 0; } /* If we detected a lower impedence during initial startup @@ -945,7 +925,7 @@ static void arizona_micd_detect(struct work_struct *work) * plain headphones. If both polarities report a low * impedence then give up and report headphones. */ - if (info->detecting && (val & MICD_LVL_1_TO_7)) { + if (val & MICD_LVL_1_TO_7) { if (info->jack_flips >= info->micd_num_modes * 10) { dev_dbg(arizona->dev, "Detected HP/line\n"); @@ -959,13 +939,45 @@ static void arizona_micd_detect(struct work_struct *work) arizona_extcon_set_mode(info, info->micd_mode); info->jack_flips++; + + if (arizona->pdata.micd_software_compare) + regmap_update_bits(arizona->regmap, + ARIZONA_MIC_DETECT_1, + ARIZONA_MICD_ENA, + ARIZONA_MICD_ENA); + + queue_delayed_work(system_power_efficient_wq, + &info->micd_timeout_work, + msecs_to_jiffies(arizona->pdata.micd_timeout)); } - goto handled; + return 0; } /* * If we're still detecting and we detect a short then we've + * got a headphone. + */ + dev_dbg(arizona->dev, "Headphone detected\n"); + info->detecting = false; + + arizona_identify_headphone(info); + + return 0; +} + +static int arizona_button_reading(void *priv) +{ + struct arizona_extcon_info *info = priv; + struct arizona *arizona = info->arizona; + int val, key, lvl, i; + + val = arizona_micd_read(info); + if (val < 0) + return val; + + /* + * If we're still detecting and we detect a short then we've * got a headphone. Otherwise it's a button press. */ if (val & MICD_LVL_0_TO_7) { @@ -986,11 +998,6 @@ static void arizona_micd_detect(struct work_struct *work) } else { dev_err(arizona->dev, "Button out of range\n"); } - } else if (info->detecting) { - dev_dbg(arizona->dev, "Headphone detected\n"); - info->detecting = false; - - arizona_identify_headphone(info); } else { dev_warn(arizona->dev, "Button with no mic: %x\n", val); @@ -1004,19 +1011,39 @@ static void arizona_micd_detect(struct work_struct *work) arizona_extcon_pulse_micbias(info); } -handled: - if (info->detecting) { - if (arizona->pdata.micd_software_compare) - regmap_update_bits(arizona->regmap, - ARIZONA_MIC_DETECT_1, - ARIZONA_MICD_ENA, - ARIZONA_MICD_ENA); + return 0; +} - queue_delayed_work(system_power_efficient_wq, - &info->micd_timeout_work, - msecs_to_jiffies(arizona->pdata.micd_timeout)); +static void arizona_micd_detect(struct work_struct *work) +{ + struct arizona_extcon_info *info = container_of(work, + struct arizona_extcon_info, + micd_detect_work.work); + struct arizona *arizona = info->arizona; + int ret; + + cancel_delayed_work_sync(&info->micd_timeout_work); + + mutex_lock(&info->lock); + + /* If the cable was removed while measuring ignore the result */ + ret = extcon_get_state(info->edev, EXTCON_MECHANICAL); + if (ret < 0) { + dev_err(arizona->dev, "Failed to check cable state: %d\n", + ret); + mutex_unlock(&info->lock); + return; + } else if (!ret) { + dev_dbg(arizona->dev, "Ignoring MICDET for removed cable\n"); + mutex_unlock(&info->lock); + return; } + if (info->detecting) + arizona_micdet_reading(info); + else + arizona_button_reading(info); + pm_runtime_mark_last_busy(info->dev); mutex_unlock(&info->lock); } -- 2.11.0