linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Daniel Lezcano <daniel.lezcano@linaro.org>
To: Anson Huang <Anson.Huang@nxp.com>,
	rui.zhang@intel.com, amit.kucheria@verdurent.com,
	robh+dt@kernel.org, mark.rutland@arm.com, shawnguo@kernel.org,
	s.hauer@pengutronix.de, kernel@pengutronix.de,
	festevam@gmail.com, catalin.marinas@arm.com, will@kernel.org,
	leonard.crestez@nxp.com, daniel.baluta@nxp.com,
	shengjiu.wang@nxp.com, peng.fan@nxp.com, ping.bai@nxp.com,
	jun.li@nxp.com, bjorn.andersson@linaro.org, olof@lixom.net,
	vkoul@kernel.org, dinguyen@kernel.org,
	marcin.juszkiewicz@linaro.org, linux-pm@vger.kernel.org,
	devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org
Cc: Linux-imx@nxp.com
Subject: Re: [PATCH RESEND 2/4] thermal: imx8mm: Add support for i.MX8MM thermal monitoring unit
Date: Thu, 27 Feb 2020 12:07:36 +0100	[thread overview]
Message-ID: <f8dfdb39-14e5-4ee2-927a-fecbcd66c71e@linaro.org> (raw)
In-Reply-To: <1582186646-22096-2-git-send-email-Anson.Huang@nxp.com>

On 20/02/2020 09:17, Anson Huang wrote:
> i.MX8MM has a thermal monitoring unit(TMU) inside, it ONLY has one
> sensor for CPU, add support for reading immediate temperature of
> this sensor.
> 
> Signed-off-by: Anson Huang <Anson.Huang@nxp.com>
> ---
> This patch is base on patch series: https://lkml.org/lkml/2020/2/19/1258
> ---
>  drivers/thermal/Kconfig          |  10 +++
>  drivers/thermal/Makefile         |   1 +
>  drivers/thermal/imx8mm_thermal.c | 134 +++++++++++++++++++++++++++++++++++++++
>  3 files changed, 145 insertions(+)
>  create mode 100644 drivers/thermal/imx8mm_thermal.c
> 
> diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig
> index d1cb8dc..972b169 100644
> --- a/drivers/thermal/Kconfig
> +++ b/drivers/thermal/Kconfig
> @@ -262,6 +262,16 @@ config IMX_SC_THERMAL
>  	  sensor. It supports one critical trip point and one
>  	  passive trip point for each thermal sensor.
>  
> +config IMX8MM_THERMAL
> +	tristate "Temperature sensor driver for Freescale i.MX8MM SoC"
> +	depends on ARCH_MXC
> +	depends on OF
> +	help
> +	  Support for Thermal Monitoring Unit (TMU) found on Freescale i.MX8MM SoC.
> +	  It supports one critical trip point and one passive trip point. The
> +	  cpufreq is used as the cooling device to throttle CPUs when the passive
> +	  trip is crossed.
> +
>  config MAX77620_THERMAL
>  	tristate "Temperature sensor driver for Maxim MAX77620 PMIC"
>  	depends on MFD_MAX77620
> diff --git a/drivers/thermal/Makefile b/drivers/thermal/Makefile
> index a11a6d8..120a05e 100644
> --- a/drivers/thermal/Makefile
> +++ b/drivers/thermal/Makefile
> @@ -44,6 +44,7 @@ obj-$(CONFIG_ARMADA_THERMAL)	+= armada_thermal.o
>  obj-$(CONFIG_TANGO_THERMAL)	+= tango_thermal.o
>  obj-$(CONFIG_IMX_THERMAL)	+= imx_thermal.o
>  obj-$(CONFIG_IMX_SC_THERMAL)	+= imx_sc_thermal.o
> +obj-$(CONFIG_IMX8MM_THERMAL)	+= imx8mm_thermal.o
>  obj-$(CONFIG_MAX77620_THERMAL)	+= max77620_thermal.o
>  obj-$(CONFIG_QORIQ_THERMAL)	+= qoriq_thermal.o
>  obj-$(CONFIG_DA9062_THERMAL)	+= da9062-thermal.o
> diff --git a/drivers/thermal/imx8mm_thermal.c b/drivers/thermal/imx8mm_thermal.c
> new file mode 100644
> index 0000000..04f8a8f
> --- /dev/null
> +++ b/drivers/thermal/imx8mm_thermal.c
> @@ -0,0 +1,134 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Copyright 2019 NXP.

Copyright (c) 2020, NXP Semiconductors

Authors: Anson Huang <Anson.Huang@nxp.com>

??

> + *
> + */
> +
> +#include <linux/clk.h>
> +#include <linux/delay.h>
> +#include <linux/err.h>
> +#include <linux/io.h>
> +#include <linux/module.h>
> +#include <linux/of.h>
> +#include <linux/of_address.h>
> +#include <linux/platform_device.h>
> +#include <linux/thermal.h>
> +
> +#include "thermal_core.h"
> +
> +#define TER			0x0	/* TMU enable */
> +#define TRITSR			0x20	/* TMU immediate temp */
> +
> +#define TER_EN			BIT(31)
> +#define TRITSR_VAL_MASK		0xff
> +
> +#define TEMP_LOW_LIMIT		10
> +
> +struct imx8mm_tmu {
> +	struct thermal_zone_device *tzd;
> +	void __iomem *base;
> +	struct clk *clk;
> +};
> +
> +static int tmu_get_temp(void *data, int *temp)
> +{
> +	struct imx8mm_tmu *tmu = data;
> +	u32 val;
> +
> +	/* the temp sensor need about 1ms to finish the measurement */
> +	usleep_range(1000, 2000);

Why do yo need to force a delay here? If the sensor can not be read more
than one time every 1ms, then specify that in the DT switching the
polling to the right value, no?

> +	val = readl_relaxed(tmu->base + TRITSR) & TRITSR_VAL_MASK;
> +	if (val < TEMP_LOW_LIMIT)
> +		return -EAGAIN;>
> +	*temp = val * 1000;
> +	return 0;
> +}
> +
> +static struct thermal_zone_of_device_ops tmu_tz_ops = {
> +	.get_temp = tmu_get_temp,
> +};
> +
> +static int imx8mm_tmu_probe(struct platform_device *pdev)
> +{
> +	struct imx8mm_tmu *tmu;
> +	u32 val;
> +	int ret;
> +
> +	tmu = devm_kzalloc(&pdev->dev, sizeof(struct imx8mm_tmu), GFP_KERNEL);
> +	if (!tmu)
> +		return -ENOMEM;
> +
> +	tmu->base = devm_platform_ioremap_resource(pdev, 0);
> +	if (IS_ERR(tmu->base))
> +		return PTR_ERR(tmu->base);
> +
> +	tmu->clk = devm_clk_get(&pdev->dev, NULL);
> +	if (IS_ERR(tmu->clk)) {
> +		ret = PTR_ERR(tmu->clk);
> +		if (ret != -EPROBE_DEFER)
> +			dev_err(&pdev->dev,
> +				"failed to get tmu clock: %d\n", ret);
> +		return ret;
> +	}
> +
> +	ret = clk_prepare_enable(tmu->clk);
> +	if (ret) {
> +		dev_err(&pdev->dev, "failed to enable tmu clock: %d\n", ret);
> +		return ret;
> +	}
> +
> +	tmu->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev, 0,
> +							tmu, &tmu_tz_ops);
> +	if (IS_ERR(tmu->tzd)) {
> +		dev_err(&pdev->dev,
> +			"failed to register thermal zone sensor: %d\n", ret);
> +		return PTR_ERR(tmu->tzd);
> +	}
> +
> +	platform_set_drvdata(pdev, tmu);
> +
> +	/* enable the monitor */
> +	val = readl_relaxed(tmu->base + TER);
> +	val |= TER_EN;
> +	writel_relaxed(val, tmu->base + TER);

A side note perhaps you can wrap:

static int imx8mm_start(struct imx8mm_tmu *tmu)
{
	clk_prepare_enable()
	val = readl_relaxed(tmu->base + TER);
	val |= TER_EN;
	writel_relaxed(val, tmu->base + TER);
}

and

static void imx8mm_stop(struct imx8mm_tmu *tmu)
{
	val = readl_relaxed(tmu->base + TER);
	val &= ~TER_EN;
	writel_relaxed(val, tmu->base + TER);
	clk_disable_unprepare(tmu->clk);
}

So the suspend/resume callbacks can call them directly if you decide to
add them.

> +	return 0;
> +}
> +
> +static int imx8mm_tmu_remove(struct platform_device *pdev)
> +{
> +	struct imx8mm_tmu *tmu = platform_get_drvdata(pdev);
> +	u32 val;
> +
> +	/* disable TMU */
> +	val = readl_relaxed(tmu->base + TER);
> +	val &= ~TER_EN;
> +	writel_relaxed(val, tmu->base + TER);
> +
> +	clk_disable_unprepare(tmu->clk);
> +	platform_set_drvdata(pdev, NULL);
> +
> +	return 0;
> +}
> +
> +static const struct of_device_id imx8mm_tmu_table[] = {
> +	{ .compatible = "fsl,imx8mm-tmu", },
> +	{ },
> +};
> +
> +static struct platform_driver imx8mm_tmu = {
> +	.driver = {
> +		.name	= "i.mx8mm_thermal",
> +		.of_match_table = imx8mm_tmu_table,
> +	},
> +	.probe = imx8mm_tmu_probe,
> +	.remove = imx8mm_tmu_remove,
> +};
> +module_platform_driver(imx8mm_tmu);
> +
> +MODULE_AUTHOR("Anson Huang <Anson.Huang@nxp.com>");
> +MODULE_DESCRIPTION("i.MX8MM Thermal Monitor Unit driver");
> +MODULE_LICENSE("GPL v2");
> 


-- 
 <http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog


  reply	other threads:[~2020-02-27 11:07 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-20  8:17 [PATCH RESEND 1/4] dt-bindings: thermal: imx8mm-thermal: Add binding doc for i.MX8MM Anson Huang
2020-02-20  8:17 ` [PATCH RESEND 2/4] thermal: imx8mm: Add support for i.MX8MM thermal monitoring unit Anson Huang
2020-02-27 11:07   ` Daniel Lezcano [this message]
2020-02-28  1:12     ` Anson Huang
2020-02-28  9:31       ` Daniel Lezcano
2020-02-29  3:37         ` Anson Huang
2020-02-20  8:17 ` [PATCH RESEND 3/4] arm64: defconfig: Enable CONFIG_IMX8MM_THERMAL as module Anson Huang
2020-02-20  8:17 ` [PATCH RESEND 4/4] arm64: dts: imx8mm: Add thermal zone support Anson Huang
  -- strict thread matches above, loose matches on Subject: below --
2019-10-28  6:03 [PATCH RESEND 1/4] dt-bindings: thermal: imx8mm-thermal: Add binding doc for i.MX8MM Anson Huang
2019-10-28  6:03 ` [PATCH RESEND 2/4] thermal: imx8mm: Add support for i.MX8MM thermal monitoring unit Anson Huang
2019-10-29 10:26   ` Fabio Estevam

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f8dfdb39-14e5-4ee2-927a-fecbcd66c71e@linaro.org \
    --to=daniel.lezcano@linaro.org \
    --cc=Anson.Huang@nxp.com \
    --cc=Linux-imx@nxp.com \
    --cc=amit.kucheria@verdurent.com \
    --cc=bjorn.andersson@linaro.org \
    --cc=catalin.marinas@arm.com \
    --cc=daniel.baluta@nxp.com \
    --cc=devicetree@vger.kernel.org \
    --cc=dinguyen@kernel.org \
    --cc=festevam@gmail.com \
    --cc=jun.li@nxp.com \
    --cc=kernel@pengutronix.de \
    --cc=leonard.crestez@nxp.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=marcin.juszkiewicz@linaro.org \
    --cc=mark.rutland@arm.com \
    --cc=olof@lixom.net \
    --cc=peng.fan@nxp.com \
    --cc=ping.bai@nxp.com \
    --cc=robh+dt@kernel.org \
    --cc=rui.zhang@intel.com \
    --cc=s.hauer@pengutronix.de \
    --cc=shawnguo@kernel.org \
    --cc=shengjiu.wang@nxp.com \
    --cc=vkoul@kernel.org \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).