From patchwork Fri Oct 8 08:11:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?TWljaGFsIFZva8OhxI0=?= X-Patchwork-Id: 12544549 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D0B5CC433EF for ; Fri, 8 Oct 2021 08:16:03 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 8D57560F90 for ; Fri, 8 Oct 2021 08:16:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 8D57560F90 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=ysoft.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=K5hzHKzeLsi5aSaw9w4zn+2BHuuuw6RF+GoIXYcHsB0=; b=2sR6vr4KtdmpEB mzk4Nnm8rUu8H1P+MDvpfwZ29qVFdIpJpiH7yQCfPAPAG07mEzSb+FeIWB0WJsFPyOZ3/CN/mMXF2 /QuKanQZiY8usnfCzLzhJeNpDW8bwyc3S4rTUckqGO36h50IQxZ3ARmivTUbZYVDMIG9WFGi5SEgn Em0ErWJpNLRlaaIPjgduvx6w8Qwf5M2IPXneKftgSVUZ9IarMnzdz2v5w+c/vxZHug4a/k8fRFJjP CaCqFiM/je/ILU8onJbqCAH8SJbzs1WAsn5lTHPnDuVaK5CIhGLJkkAL4rZAQb+m3J716E2JHWutc uxTYKQikdIIui/z0udgg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mYl1G-0020JJ-4A; Fri, 08 Oct 2021 08:14:18 +0000 Received: from uho.ysoft.cz ([81.19.3.130]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mYl1B-0020II-C5 for linux-arm-kernel@lists.infradead.org; Fri, 08 Oct 2021 08:14:16 +0000 Received: from vokac-Latitude-7410.ysoft.local (unknown [10.0.30.12]) by uho.ysoft.cz (Postfix) with ESMTP id ECF18A0476; Fri, 8 Oct 2021 10:14:06 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ysoft.com; s=20160406-ysoft-com; t=1633680847; bh=qw2VKvhxkkddHr4x1O/5STvAL5zyVtYEtbsNsc3vo8M=; h=From:To:Cc:Subject:Date:From; b=WtjqWVdmLRCsTAUKy7z1FzcyZs69BEz9VGC1gY5LhhCbkg4LcwUY1/wZ0JuLcWRuI RuBp1tAmcCGtCtK3eMFPgMNvGQuvC9qQHPD07GnXBWYvJVmAzle8+Irs2moQBeCO1a 5wIm6YfunyMhWSGMk5FUvktnBRyltNJinft6VCW4= From: =?utf-8?b?TWljaGFsIFZva8OhxI0=?= To: Andrzej Pietrasiewicz , linux-pm@vger.kernel.org, Daniel Lezcano , Shawn Guo Cc: Amit Kucheria , Sascha Hauer , Pengutronix Kernel Team , Fabio Estevam , NXP Linux Team , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, =?utf-8?q?Petr_Bene=C5=A1?= , petrben@gmail.com, stable@vger.kernel.org, =?utf-8?b?TWljaGFsIFZva8OhxI0=?= Subject: [PATCH] thermal: imx: Fix temperature measurements on i.MX6 after alarm Date: Fri, 8 Oct 2021 10:11:37 +0200 Message-Id: <20211008081137.1948848-1-michal.vokac@ysoft.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211008_011413_774984_B7D3129E X-CRM114-Status: GOOD ( 16.89 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Petr Beneš SoC temperature readout may not work after thermal alarm fires interrupt. This harms userspace as well as CPU cooling device. Two issues with the logic involved. First, there is no protection against concurent measurements, hence one can switch the sensor off while the other one tries to read temperature later. Second, the interrupt path usually fails. At the end the sensor is powered off and thermal IRQ is disabled. One has to reenable the thermal zone by the sysfs interface. Most of troubles come from commit d92ed2c9d3ff ("thermal: imx: Use driver's local data to decide whether to run a measurement") It uses data->irq_enabled as the "local data". Indeed, its value is related to the state of the sensor loosely under normal operation and, frankly, gets unleashed when the thermal interrupt arrives. Current patch adds the "local data" (new member sensor_on in imx_thermal_data) and sets its value in controlled manner. Fixes: d92ed2c9d3ff ("thermal: imx: Use driver's local data to decide whether to run a measurement") Cc: petrben@gmail.com Cc: stable@vger.kernel.org Signed-off-by: Petr Beneš Signed-off-by: Michal Vokáč --- drivers/thermal/imx_thermal.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/drivers/thermal/imx_thermal.c b/drivers/thermal/imx_thermal.c index 2c7473d86a59..df5658e21828 100644 --- a/drivers/thermal/imx_thermal.c +++ b/drivers/thermal/imx_thermal.c @@ -209,6 +209,8 @@ struct imx_thermal_data { struct clk *thermal_clk; const struct thermal_soc_data *socdata; const char *temp_grade; + struct mutex sensor_lock; + bool sensor_on; }; static void imx_set_panic_temp(struct imx_thermal_data *data, @@ -252,11 +254,12 @@ static int imx_get_temp(struct thermal_zone_device *tz, int *temp) const struct thermal_soc_data *soc_data = data->socdata; struct regmap *map = data->tempmon; unsigned int n_meas; - bool wait, run_measurement; + bool wait; u32 val; - run_measurement = !data->irq_enabled; - if (!run_measurement) { + mutex_lock(&data->sensor_lock); + + if (data->sensor_on) { /* Check if a measurement is currently in progress */ regmap_read(map, soc_data->temp_data, &val); wait = !(val & soc_data->temp_valid_mask); @@ -283,13 +286,15 @@ static int imx_get_temp(struct thermal_zone_device *tz, int *temp) regmap_read(map, soc_data->temp_data, &val); - if (run_measurement) { + if (!data->sensor_on) { regmap_write(map, soc_data->sensor_ctrl + REG_CLR, soc_data->measure_temp_mask); regmap_write(map, soc_data->sensor_ctrl + REG_SET, soc_data->power_down_mask); } + mutex_unlock(&data->sensor_lock); + if ((val & soc_data->temp_valid_mask) == 0) { dev_dbg(&tz->device, "temp measurement never finished\n"); return -EAGAIN; @@ -339,20 +344,26 @@ static int imx_change_mode(struct thermal_zone_device *tz, const struct thermal_soc_data *soc_data = data->socdata; if (mode == THERMAL_DEVICE_ENABLED) { + mutex_lock(&data->sensor_lock); regmap_write(map, soc_data->sensor_ctrl + REG_CLR, soc_data->power_down_mask); regmap_write(map, soc_data->sensor_ctrl + REG_SET, soc_data->measure_temp_mask); + data->sensor_on = true; + mutex_unlock(&data->sensor_lock); if (!data->irq_enabled) { data->irq_enabled = true; enable_irq(data->irq); } } else { + mutex_lock(&data->sensor_lock); regmap_write(map, soc_data->sensor_ctrl + REG_CLR, soc_data->measure_temp_mask); regmap_write(map, soc_data->sensor_ctrl + REG_SET, soc_data->power_down_mask); + data->sensor_on = false; + mutex_unlock(&data->sensor_lock); if (data->irq_enabled) { disable_irq(data->irq); @@ -728,6 +739,8 @@ static int imx_thermal_probe(struct platform_device *pdev) } /* Make sure sensor is in known good state for measurements */ + mutex_init(&data->sensor_lock); + mutex_lock(&data->sensor_lock); regmap_write(map, data->socdata->sensor_ctrl + REG_CLR, data->socdata->power_down_mask); regmap_write(map, data->socdata->sensor_ctrl + REG_CLR, @@ -739,6 +752,8 @@ static int imx_thermal_probe(struct platform_device *pdev) IMX6_MISC0_REFTOP_SELBIASOFF); regmap_write(map, data->socdata->sensor_ctrl + REG_SET, data->socdata->power_down_mask); + data->sensor_on = false; + mutex_unlock(&data->sensor_lock); ret = imx_thermal_register_legacy_cooling(data); if (ret) @@ -796,10 +811,13 @@ static int imx_thermal_probe(struct platform_device *pdev) if (data->socdata->version == TEMPMON_IMX6SX) imx_set_panic_temp(data, data->temp_critical); + mutex_lock(&data->sensor_lock); regmap_write(map, data->socdata->sensor_ctrl + REG_CLR, data->socdata->power_down_mask); regmap_write(map, data->socdata->sensor_ctrl + REG_SET, data->socdata->measure_temp_mask); + data->sensor_on = true; + mutex_unlock(&data->sensor_lock); data->irq_enabled = true; ret = thermal_zone_device_enable(data->tz); @@ -832,8 +850,12 @@ static int imx_thermal_remove(struct platform_device *pdev) struct regmap *map = data->tempmon; /* Disable measurements */ + mutex_lock(&data->sensor_lock); regmap_write(map, data->socdata->sensor_ctrl + REG_SET, data->socdata->power_down_mask); + data->sensor_on = false; + mutex_unlock(&data->sensor_lock); + if (!IS_ERR(data->thermal_clk)) clk_disable_unprepare(data->thermal_clk);