Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp3978808ybl; Mon, 9 Dec 2019 03:24:22 -0800 (PST) X-Google-Smtp-Source: APXvYqxjCWzx59fMgsDzDQTSvA1NbsVLEdOiS2Ethk6a8Kn5LMIRonPOwFB6yqIj4Di3AxM4w0ou X-Received: by 2002:a05:6830:18ce:: with SMTP id v14mr19446047ote.254.1575890662066; Mon, 09 Dec 2019 03:24:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1575890662; cv=none; d=google.com; s=arc-20160816; b=Q1BTo6l8QaJmSg/wjA0dePHxELpTOVl89tSb3tQaA/fu7u6dmznwFP3iOB9k0ARfXz K7BNXgO3S7/grBtC7p3saKCIZMchlk0ETSHWsXnFtWBeyMp4LElJV9K3Q8e4lpYTKcSz O2ZqjhxFJgpw0fx5Hyo+BQ3D2iCzXfIB/xfj7XGU6Q0D2cq2w/eks9EXozZbL7/90HPL bDcPrPXqIdc7oQQ11kwEkJd2zdUUrDXVheNLDma8zJwrt0u1kbRk7oq44WfLAAWLGcbI Quqv6yXbfSYBLXAPxUJ9cGjvvluMTqd4o+nyR+yc+47ZVEpYOYdtDIxi5vpL8ECRxBc+ Agpw== 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=ytfwSFfXRj6bDTvmt8cvHWOAoK0OCuFICJ+RmAJfG3A=; b=riV6+xE8jckYuXsgC1D3wHf875hQN9BDLE4C86KCeSt3SuteqUCJpdVJdheOUo1MZC h3nO2Vjejg8euD82kHP85560zkktwJ6b6DrYYVx9CDTUiwlJ6sjU/9wmNc4urYAyLvzf hpMPKrrhbAz2ZoQ7IIaE1CmoVMVwDu6YeYx1XaBSn6XYbJzNJrvb8sCmgrWGtvOGsRV4 JZxVW7DmBd476ny9/0NiYBrj0tT58UA+3bG4uNVtx6NKXfXLt+1vSLoNh84Ed5tyV5yW UFbxnMLXGYgXPB51JF231IUSJ3c2nrz0HrV54W+0P84kp6/qk1KX54vWBO76xDXzqKUT zr7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cirrus.com header.s=PODMain02222019 header.b=VPLU7VDx; 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 t185si10463997oib.39.2019.12.09.03.24.09; Mon, 09 Dec 2019 03:24:22 -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=VPLU7VDx; 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 S1726377AbfLILJf (ORCPT + 99 others); Mon, 9 Dec 2019 06:09:35 -0500 Received: from mx0a-001ae601.pphosted.com ([67.231.149.25]:20226 "EHLO mx0b-001ae601.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727731AbfLILJa (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 xB9B9QlU029232; Mon, 9 Dec 2019 05:09:27 -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=ytfwSFfXRj6bDTvmt8cvHWOAoK0OCuFICJ+RmAJfG3A=; b=VPLU7VDxE4xf0fcuFHaeZtrIQoetjjKCEUz+Qxo7BVMhBUR60CksHdp32PAUixK1dIHO h4b7F1gFTk9rYU12HKbeWoj8Ryn6H74sjQr6YjHMpTJL0YtNM4HlZ68ItLEdTEkQt5fN 2Au1QpxloE6umj2Bc1b7DKR9WcnXe3sT83dgbrATEg2TSY8iuuG5F7+rRGZbe/a5HNAN cUCG+kJaHkLIl85apPiaKl3dRNVVoRYwV5O7/4B933ttop6GsUDybM6Arip8HFkMusHi 1djah/bpqCjleMyAaDp/01iBHDRyvlWhMWGHOAv/qjQgr7UteQW4mIHWbIjqMjEDEVX5 WA== 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-5 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT); Mon, 09 Dec 2019 05:09:27 -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 93BD02A1; Mon, 9 Dec 2019 11:09:16 +0000 (UTC) From: Charles Keepax To: CC: , , Subject: [PATCH 09/10] extcon: arizona: Factor out microphone impedance into a function Date: Mon, 9 Dec 2019 11:09:15 +0000 Message-ID: <20191209110916.29524-9-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=537 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 The microphone detection handler is very long, start breaking it up by factoring out the actual reading of the impedance value into a separate functions. Additionally, this also fixes a minor bug and ensures that the microphone timeout will be rescheduled in all error cases. Signed-off-by: Charles Keepax --- drivers/extcon/extcon-arizona.c | 125 +++++++++++++++++++++++----------------- 1 file changed, 73 insertions(+), 52 deletions(-) diff --git a/drivers/extcon/extcon-arizona.c b/drivers/extcon/extcon-arizona.c index a0135f44cba1e..b09a9a8ce98bc 100644 --- a/drivers/extcon/extcon-arizona.c +++ b/drivers/extcon/extcon-arizona.c @@ -804,70 +804,55 @@ static void arizona_micd_timeout_work(struct work_struct *work) mutex_unlock(&info->lock); } -static void arizona_micd_detect(struct work_struct *work) +static int arizona_micd_adc_read(struct arizona_extcon_info *info) { - struct arizona_extcon_info *info = container_of(work, - struct arizona_extcon_info, - micd_detect_work.work); struct arizona *arizona = info->arizona; - unsigned int val = 0, lvl; - int ret, i, key; - - cancel_delayed_work_sync(&info->micd_timeout_work); + unsigned int val; + int ret; - mutex_lock(&info->lock); + /* Must disable MICD before we read the ADCVAL */ + regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1, + ARIZONA_MICD_ENA, 0); - /* 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; + ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_4, &val); + if (ret != 0) { + dev_err(arizona->dev, + "Failed to read MICDET_ADCVAL: %d\n", ret); + return ret; } - if (info->detecting && arizona->pdata.micd_software_compare) { - /* Must disable MICD before we read the ADCVAL */ - regmap_update_bits(arizona->regmap, ARIZONA_MIC_DETECT_1, - ARIZONA_MICD_ENA, 0); - ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_4, &val); - if (ret != 0) { - dev_err(arizona->dev, - "Failed to read MICDET_ADCVAL: %d\n", - ret); - mutex_unlock(&info->lock); - return; - } + dev_dbg(arizona->dev, "MICDET_ADCVAL: %x\n", val); - dev_dbg(arizona->dev, "MICDET_ADCVAL: %x\n", val); + val &= ARIZONA_MICDET_ADCVAL_MASK; + if (val < ARRAY_SIZE(arizona_micd_levels)) + val = arizona_micd_levels[val]; + else + val = INT_MAX; + + if (val <= QUICK_HEADPHONE_MAX_OHM) + val = ARIZONA_MICD_STS | ARIZONA_MICD_LVL_0; + else if (val <= MICROPHONE_MIN_OHM) + val = ARIZONA_MICD_STS | ARIZONA_MICD_LVL_1; + else if (val <= MICROPHONE_MAX_OHM) + val = ARIZONA_MICD_STS | ARIZONA_MICD_LVL_8; + else + val = ARIZONA_MICD_LVL_8; - val &= ARIZONA_MICDET_ADCVAL_MASK; - if (val < ARRAY_SIZE(arizona_micd_levels)) - val = arizona_micd_levels[val]; - else - val = INT_MAX; - - if (val <= QUICK_HEADPHONE_MAX_OHM) - val = ARIZONA_MICD_STS | ARIZONA_MICD_LVL_0; - else if (val <= MICROPHONE_MIN_OHM) - val = ARIZONA_MICD_STS | ARIZONA_MICD_LVL_1; - else if (val <= MICROPHONE_MAX_OHM) - val = ARIZONA_MICD_STS | ARIZONA_MICD_LVL_8; - else - val = ARIZONA_MICD_LVL_8; - } + return val; +} + +static int arizona_micd_read(struct arizona_extcon_info *info) +{ + struct arizona *arizona = info->arizona; + unsigned int val = 0; + int ret, i; for (i = 0; i < 10 && !(val & MICD_LVL_0_TO_8); i++) { ret = regmap_read(arizona->regmap, ARIZONA_MIC_DETECT_3, &val); if (ret != 0) { dev_err(arizona->dev, "Failed to read MICDET: %d\n", ret); - mutex_unlock(&info->lock); - return; + return ret; } dev_dbg(arizona->dev, "MICDET: %x\n", val); @@ -875,17 +860,53 @@ static void arizona_micd_detect(struct work_struct *work) if (!(val & ARIZONA_MICD_VALID)) { dev_warn(arizona->dev, "Microphone detection state invalid\n"); - mutex_unlock(&info->lock); - return; + return -EINVAL; } } if (i == 10 && !(val & MICD_LVL_0_TO_8)) { dev_err(arizona->dev, "Failed to get valid MICDET value\n"); + return -EINVAL; + } + + return val; +} + +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; + 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; } + 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; + + val = ret; + /* Due to jack detect this should never happen */ if (!(val & ARIZONA_MICD_STS)) { dev_warn(arizona->dev, "Detected open circuit\n"); -- 2.11.0