Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp2916894pxb; Mon, 18 Oct 2021 04:46:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyxN8DljzdngEI+7z6XI/1HSsBDGh0x37Z/HZJinSvGeV3GySU8aAt3Ebk/L/QfrvcQI0Q5 X-Received: by 2002:a17:907:e8a:: with SMTP id ho10mr16112608ejc.440.1634557584814; Mon, 18 Oct 2021 04:46:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634557584; cv=none; d=google.com; s=arc-20160816; b=r/TjTTQmp8iTUcT6sbWQLC4J1PrU/Nxo2cgDohThYkTtmORHJbKqv0lYzoF8dIHDks 3WIfwk0ln04SFXrka5UQQKjYWNb6RWCeFm9basDLzn8ZKGYV7F5yi367/vJX65Ggbwu3 Cq6xtVTx/+bpoU214oLs4mcpFKBb+srQ92Z0rpT5jZRc0kgOkGPBNp4a4nhJLJ2PgqWf 1u30JDhFhKCrbcbiWsU6ON9CejRDOUpt8bw3kcAB3pUWk2ZGE90ZP6bz3Kygs6xg8Z5D XAj+7flqVE6hIx8tox/fhjHMajpuqR6jFQyk68DGBmjXwLbR8ud4nyJCb24K7TZdasha xEmg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=oxtTocfZDe8NeogbuLWnnR+icWeZEV7xYOyxnqTH3SM=; b=cpl774/eevFpffWyJ+6SsNbyi0ydeWSPOTYIoBpD8HgkbnK2/wleiUBGFiYn3kiaoh mFl4H4psYtjYvJcy8XwUFp2yFBtTl7+Q/3/V7EtaywdWzu/P8L6LvErDdt3lknCA8XHW nicvmLKRv0nPAQF5C7+OOgCsOU3F1G+VDpI4dhFjtswDS7tU7uf2nPbU/So82aR+9N6f s0BA0gjP5WyutlSJmh9Pwbl6TpT5Wju8E+gyWbQi2bmTWS2WWv2enRR1qhcyLYI3XOan cabwj2CqrMR7IMeB4sAAnRd516fctoTpJdImbtmStcgUT0hcW9fCJ/v7GnQ/Yzg0lgl1 MpFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=DHV9wmyF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j2si18826173ejx.208.2021.10.18.04.46.01; Mon, 18 Oct 2021 04:46:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=DHV9wmyF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231364AbhJRLoL (ORCPT + 99 others); Mon, 18 Oct 2021 07:44:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37518 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231167AbhJRLoK (ORCPT ); Mon, 18 Oct 2021 07:44:10 -0400 Received: from mail-pf1-x433.google.com (mail-pf1-x433.google.com [IPv6:2607:f8b0:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F1444C061714; Mon, 18 Oct 2021 04:41:59 -0700 (PDT) Received: by mail-pf1-x433.google.com with SMTP id f11so10620776pfc.12; Mon, 18 Oct 2021 04:41:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=oxtTocfZDe8NeogbuLWnnR+icWeZEV7xYOyxnqTH3SM=; b=DHV9wmyFndQgoNutJNXy8Mn3Thc4M+NvSKgdROTSPktZby8DuA8Y/PgUj17WwZCl0Q ASLHqB3NJ4+o+F0MgYOJ0QRk3/spslLQsQXE7aR7sVX/vlkv2W9/8kSfYj7WhdY2/1Bj fydbaP5X9fdMUOxtm21srDGnHY85VKX1zaQdBxghbl0fHgwYVRBY6NhZgJjHCkxYqLRZ Fk36NNHbBHjrxVNuPnahG6lVlUyhTT+nuUJ05Tom5hUUf36zS9CEUEhEKD7kBAKz9cjV Th+7pp1A4FpmsxqF3NPH9slvb3TSi6jXRq+XWXD+o+FHV+0lYE25pynVyEoTvdo4aHe2 jkWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=oxtTocfZDe8NeogbuLWnnR+icWeZEV7xYOyxnqTH3SM=; b=0VswrrphndwZu3kqumvZiC85jkaOjiJq/xLqlraj+y8KYB5ERxNsr9GbXiBrykz4wV 38H8V5kz5XB/B2uFXP5lDbcxRkkkhWyL6Rm8QKoTWHt6oOwdyQ1WPsezwpEcgX0x4mxn fggxigcDLSkZGkvQcFLnzz88lKMFdIxlVxLuyHxaYpXwVmSmP4QQH8biuHAbPQn3MI5j MabExs761LQBKZu6+Tv+b/6bbG5mwJ5T2ewifRGX/iX7i7QYKqlcUFZuqagnqnVoL8/G 2Han0OWtP/e4DyDC6C1lS5sO2RVmEDbg88QcJJqloaNP4XRpY7vzG/mtgsgV9QfshH7/ w2MQ== X-Gm-Message-State: AOAM531IbPIP1hJ1SlJhvCwhPBiKPvXkJ/BBh5SoCfjVQyRdJjMuIttw vH6ao1Y0wN/UK4PjpqQXTgUPiSkHYR7StddTGBk= X-Received: by 2002:a63:4754:: with SMTP id w20mr23146985pgk.98.1634557319344; Mon, 18 Oct 2021 04:41:59 -0700 (PDT) MIME-Version: 1.0 References: <20211008081137.1948848-1-michal.vokac@ysoft.com> <20211018112820.qkebjt2gk2w53lp5@pengutronix.de> In-Reply-To: <20211018112820.qkebjt2gk2w53lp5@pengutronix.de> From: Petr Benes Date: Mon, 18 Oct 2021 13:41:48 +0200 Message-ID: Subject: Re: [PATCH] thermal: imx: Fix temperature measurements on i.MX6 after alarm To: Oleksij Rempel Cc: =?UTF-8?B?TWljaGFsIFZva8OhxI0=?= , Andrzej Pietrasiewicz , linux-pm@vger.kernel.org, Daniel Lezcano , Shawn Guo , 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?= , stable@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 18 Oct 2021 at 13:28, Oleksij Rempel wrot= e: > > Hi Michal, > > I hope you have seen this patch: > https://lore.kernel.org/all/20210924115032.29684-1-o.rempel@pengutronix.d= e/ > > Are there any reason why this was ignored? Missed completely. It looks it addresses the problem more systematically. > > On Fri, Oct 08, 2021 at 10:11:37AM +0200, Michal Vok=C3=A1=C4=8D wrote: > > From: Petr Bene=C5=A1 > > > > SoC temperature readout may not work after thermal alarm fires interrup= t. > > This harms userspace as well as CPU cooling device. > > > > Two issues with the logic involved. First, there is no protection again= st > > concurent measurements, hence one can switch the sensor off while > > the other one tries to read temperature later. Second, the interrupt pa= th > > 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 w= hether to run a measurement") > > Cc: petrben@gmail.com > > Cc: stable@vger.kernel.org > > Signed-off-by: Petr Bene=C5=A1 > > Signed-off-by: Michal Vok=C3=A1=C4=8D > > --- > > 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_therma= l.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_devic= e *tz, int *temp) > > const struct thermal_soc_data *soc_data =3D data->socdata; > > struct regmap *map =3D data->tempmon; > > unsigned int n_meas; > > - bool wait, run_measurement; > > + bool wait; > > u32 val; > > > > - run_measurement =3D !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 =3D !(val & soc_data->temp_valid_mask); > > @@ -283,13 +286,15 @@ static int imx_get_temp(struct thermal_zone_devic= e *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) =3D=3D 0) { > > dev_dbg(&tz->device, "temp measurement never finished\n")= ; > > return -EAGAIN; > > @@ -339,20 +344,26 @@ static int imx_change_mode(struct thermal_zone_de= vice *tz, > > const struct thermal_soc_data *soc_data =3D data->socdata; > > > > if (mode =3D=3D 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 =3D true; > > + mutex_unlock(&data->sensor_lock); > > > > if (!data->irq_enabled) { > > data->irq_enabled =3D 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 =3D 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 =3D false; > > + mutex_unlock(&data->sensor_lock); > > > > ret =3D imx_thermal_register_legacy_cooling(data); > > if (ret) > > @@ -796,10 +811,13 @@ static int imx_thermal_probe(struct platform_devi= ce *pdev) > > if (data->socdata->version =3D=3D 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 =3D true; > > + mutex_unlock(&data->sensor_lock); > > > > data->irq_enabled =3D true; > > ret =3D thermal_zone_device_enable(data->tz); > > @@ -832,8 +850,12 @@ static int imx_thermal_remove(struct platform_devi= ce *pdev) > > struct regmap *map =3D 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 =3D false; > > + mutex_unlock(&data->sensor_lock); > > + > > if (!IS_ERR(data->thermal_clk)) > > clk_disable_unprepare(data->thermal_clk); > > > > -- > > 2.25.1 > >