From nobody Tue Dec 16 16:39:08 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 954A2C4167B for ; Thu, 30 Nov 2023 16:50:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345726AbjK3Quc (ORCPT ); Thu, 30 Nov 2023 11:50:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50142 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232327AbjK3Qua (ORCPT ); Thu, 30 Nov 2023 11:50:30 -0500 Received: from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::228]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F276410DB; Thu, 30 Nov 2023 08:50:35 -0800 (PST) Received: by mail.gandi.net (Postfix) with ESMTPSA id C76ED1BF206; Thu, 30 Nov 2023 16:50:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1701363034; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=jMLITpTzeh8DNALLYB3xaXz0o3Zgf4CFfgP7CSSCYRg=; b=g33WAfc7EIx8vMc/7Ld2/3ipneCfG0NkAkdKQKPk9IGkTzfjI9CR5T6hkPuFQcLOEVEAK3 7bmgCa2cTKSUcpcRASfUEgbdioYFBJmxCiDduRTjaQoJKY0RLpD0YbfdsNlyQVA2RGEXqw /6o+A0P2fPoFMO9YRHqRDMWdczLYi1Pv133HmcQIflqIc+McZpBxiPQ5MbNuPxEGkFHX1o si3if3oO2zH0u/g5xxPOvt+lshhCEOV3kUI1wPHBOWZqSlSvEb0D3WoXT0+fgJtHVhSZ8n 09RDB8xZGoMH56StEa4+3DlezjN56T9kUQ0DznK7DM4nInKWY1s8QitCvaIVqw== From: Thomas Richard To: rafael@kernel.org, daniel.lezcano@linaro.org Cc: rui.zhang@intel.com, lukasz.luba@arm.com, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, thomas.petazzoni@bootlin.com, gregory.clement@bootlin.com, theo.lebrun@bootlin.com, u-kumar1@ti.com, Thomas Richard Subject: [PATCH v2] thermal: k3_j72xx_bandgap: implement suspend/resume support Date: Thu, 30 Nov 2023 17:49:53 +0100 Message-Id: <20231130164953.2043305-1-thomas.richard@bootlin.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-GND-Sasl: thomas.richard@bootlin.com Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Th=C3=A9o Lebrun This add suspend-to-ram support. The derived_table is kept-as is, so the resume is only about pm_runtime_* calls and restoring the same registers as the probe. Extract the hardware initialization procedure to a function called at both probe-time & resume-time. The probe-time loop is split in two to ensure doing the hardware initialization before registering thermal zones. That ensures our callbacks cannot be called while in bad state. Signed-off-by: Th=C3=A9o Lebrun Signed-off-by: Thomas Richard Acked-by: Keerthy --- v2: - Fix warnings/errors reported by kernel test robot drivers/thermal/k3_j72xx_bandgap.c | 112 ++++++++++++++++++++--------- 1 file changed, 79 insertions(+), 33 deletions(-) diff --git a/drivers/thermal/k3_j72xx_bandgap.c b/drivers/thermal/k3_j72xx_= bandgap.c index c74094a86982..4ad23c8bf73b 100644 --- a/drivers/thermal/k3_j72xx_bandgap.c +++ b/drivers/thermal/k3_j72xx_bandgap.c @@ -178,6 +178,7 @@ struct k3_j72xx_bandgap { void __iomem *base; void __iomem *cfg2_base; struct k3_thermal_data *ts_data[K3_VTM_MAX_NUM_TS]; + int cnt; }; =20 /* common data structures */ @@ -338,24 +339,53 @@ static void print_look_up_table(struct device *dev, i= nt *ref_table) dev_dbg(dev, "%d %d %d\n", i, derived_table[i], ref_table[i]); } =20 +static void k3_j72xx_bandgap_init_hw(struct k3_j72xx_bandgap *bgp) +{ + struct k3_thermal_data *data; + int id, high_max, low_temp; + u32 val; + + for (id =3D 0; id < bgp->cnt; id++) { + data =3D bgp->ts_data[id]; + val =3D readl(bgp->cfg2_base + data->ctrl_offset); + val |=3D (K3_VTM_TMPSENS_CTRL_MAXT_OUTRG_EN | + K3_VTM_TMPSENS_CTRL_SOC | + K3_VTM_TMPSENS_CTRL_CLRZ | BIT(4)); + writel(val, bgp->cfg2_base + data->ctrl_offset); + } + + /* + * Program TSHUT thresholds + * Step 1: set the thresholds to ~123C and 105C WKUP_VTM_MISC_CTRL2 + * Step 2: WKUP_VTM_TMPSENS_CTRL_j set the MAXT_OUTRG_EN bit + * This is already taken care as per of init + * Step 3: WKUP_VTM_MISC_CTRL set the ANYMAXT_OUTRG_ALERT_EN bit + */ + high_max =3D k3_j72xx_bandgap_temp_to_adc_code(MAX_TEMP); + low_temp =3D k3_j72xx_bandgap_temp_to_adc_code(COOL_DOWN_TEMP); + + writel((low_temp << 16) | high_max, bgp->cfg2_base + K3_VTM_MISC_CTRL2_OF= FSET); + mdelay(100); + writel(K3_VTM_ANYMAXT_OUTRG_ALERT_EN, bgp->cfg2_base + K3_VTM_MISC_CTRL_O= FFSET); +} + struct k3_j72xx_bandgap_data { const bool has_errata_i2128; }; =20 static int k3_j72xx_bandgap_probe(struct platform_device *pdev) { - int ret =3D 0, cnt, val, id; - int high_max, low_temp; - struct resource *res; + const struct k3_j72xx_bandgap_data *driver_data; + struct thermal_zone_device *ti_thermal; struct device *dev =3D &pdev->dev; + bool workaround_needed =3D false; struct k3_j72xx_bandgap *bgp; struct k3_thermal_data *data; - bool workaround_needed =3D false; - const struct k3_j72xx_bandgap_data *driver_data; - struct thermal_zone_device *ti_thermal; - int *ref_table; struct err_values err_vals; void __iomem *fuse_base; + int ret =3D 0, val, id; + struct resource *res; + int *ref_table; =20 const s64 golden_factors[] =3D { -490019999999999936, @@ -422,10 +452,10 @@ static int k3_j72xx_bandgap_probe(struct platform_dev= ice *pdev) =20 /* Get the sensor count in the VTM */ val =3D readl(bgp->base + K3_VTM_DEVINFO_PWR0_OFFSET); - cnt =3D val & K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK; - cnt >>=3D __ffs(K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK); + bgp->cnt =3D val & K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK; + bgp->cnt >>=3D __ffs(K3_VTM_DEVINFO_PWR0_TEMPSENS_CT_MASK); =20 - data =3D devm_kcalloc(bgp->dev, cnt, sizeof(*data), GFP_KERNEL); + data =3D devm_kcalloc(bgp->dev, bgp->cnt, sizeof(*data), GFP_KERNEL); if (!data) { ret =3D -ENOMEM; goto err_alloc; @@ -449,8 +479,8 @@ static int k3_j72xx_bandgap_probe(struct platform_devic= e *pdev) else init_table(3, ref_table, pvt_wa_factors); =20 - /* Register the thermal sensors */ - for (id =3D 0; id < cnt; id++) { + /* Precompute the derived table & fill each thermal sensor struct */ + for (id =3D 0; id < bgp->cnt; id++) { data[id].bgp =3D bgp; data[id].ctrl_offset =3D K3_VTM_TMPSENS0_CTRL_OFFSET + id * 0x20; data[id].stat_offset =3D data[id].ctrl_offset + @@ -470,13 +500,13 @@ static int k3_j72xx_bandgap_probe(struct platform_dev= ice *pdev) else if (id =3D=3D 0 && !workaround_needed) memcpy(derived_table, ref_table, TABLE_SIZE * 4); =20 - val =3D readl(data[id].bgp->cfg2_base + data[id].ctrl_offset); - val |=3D (K3_VTM_TMPSENS_CTRL_MAXT_OUTRG_EN | - K3_VTM_TMPSENS_CTRL_SOC | - K3_VTM_TMPSENS_CTRL_CLRZ | BIT(4)); - writel(val, data[id].bgp->cfg2_base + data[id].ctrl_offset); - bgp->ts_data[id] =3D &data[id]; + } + + k3_j72xx_bandgap_init_hw(bgp); + + /* Register the thermal sensors */ + for (id =3D 0; id < bgp->cnt; id++) { ti_thermal =3D devm_thermal_of_zone_register(bgp->dev, id, &data[id], &k3_of_thermal_ops); if (IS_ERR(ti_thermal)) { @@ -486,21 +516,7 @@ static int k3_j72xx_bandgap_probe(struct platform_devi= ce *pdev) } } =20 - /* - * Program TSHUT thresholds - * Step 1: set the thresholds to ~123C and 105C WKUP_VTM_MISC_CTRL2 - * Step 2: WKUP_VTM_TMPSENS_CTRL_j set the MAXT_OUTRG_EN bit - * This is already taken care as per of init - * Step 3: WKUP_VTM_MISC_CTRL set the ANYMAXT_OUTRG_ALERT_EN bit - */ - high_max =3D k3_j72xx_bandgap_temp_to_adc_code(MAX_TEMP); - low_temp =3D k3_j72xx_bandgap_temp_to_adc_code(COOL_DOWN_TEMP); - - writel((low_temp << 16) | high_max, data[0].bgp->cfg2_base + - K3_VTM_MISC_CTRL2_OFFSET); - mdelay(100); - writel(K3_VTM_ANYMAXT_OUTRG_ALERT_EN, data[0].bgp->cfg2_base + - K3_VTM_MISC_CTRL_OFFSET); + platform_set_drvdata(pdev, bgp); =20 print_look_up_table(dev, ref_table); /* @@ -527,6 +543,35 @@ static void k3_j72xx_bandgap_remove(struct platform_de= vice *pdev) pm_runtime_disable(&pdev->dev); } =20 +static int __maybe_unused k3_j72xx_bandgap_suspend(struct device *dev) +{ + pm_runtime_put_sync(dev); + pm_runtime_disable(dev); + return 0; +} + +static int __maybe_unused k3_j72xx_bandgap_resume(struct device *dev) +{ + struct k3_j72xx_bandgap *bgp =3D dev_get_drvdata(dev); + int ret; + + pm_runtime_enable(dev); + ret =3D pm_runtime_get_sync(dev); + if (ret < 0) { + pm_runtime_put_noidle(dev); + pm_runtime_disable(dev); + return ret; + } + + k3_j72xx_bandgap_init_hw(bgp); + + return 0; +} + +static const struct dev_pm_ops k3_j72xx_bandgap_pm_ops =3D { + SET_SYSTEM_SLEEP_PM_OPS(k3_j72xx_bandgap_suspend, k3_j72xx_bandgap_resume) +}; + static const struct k3_j72xx_bandgap_data k3_j72xx_bandgap_j721e_data =3D { .has_errata_i2128 =3D true, }; @@ -554,6 +599,7 @@ static struct platform_driver k3_j72xx_bandgap_sensor_d= river =3D { .driver =3D { .name =3D "k3-j72xx-soc-thermal", .of_match_table =3D of_k3_j72xx_bandgap_match, + .pm =3D &k3_j72xx_bandgap_pm_ops, }, }; =20 --=20 2.39.2