mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
To: Yinbo Zhu <zhuyinbo@loongson.cn>,
	"Rafael J . Wysocki" <rafael@kernel.org>,
	Daniel Lezcano <daniel.lezcano@linaro.org>,
	Amit Kucheria <amitk@kernel.org>, Zhang Rui <rui.zhang@intel.com>,
	Rob Herring <robh+dt@kernel.org>,
	Krzysztof Kozlowski <krzysztof.kozlowski+dt@linaro.org>,
	linux-pm@vger.kernel.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org
Cc: zhanghongchen <zhanghongchen@loongson.cn>
Subject: Re: [PATCH v1 3/3] thermal: ls2k: add thermal management support
Date: Mon, 19 Sep 2022 09:11:18 +0200	[thread overview]
Message-ID: <b5578e0f-ac41-9889-a6bb-4b1d74197ec0@linaro.org> (raw)
In-Reply-To: <20220919034915.26912-3-zhuyinbo@loongson.cn>

On 19/09/2022 05:49, Yinbo Zhu wrote:
> This patch adds the support for loongson2k thermal sensor controller,
> which can support maximum 3 sensors.
> 
> It's based on thermal of framework:
>  - Trip points defined in device tree.
>  - Cpufreq as cooling device registered in loongson2k cpufreq driver.
>  - Pwm fan as cooling device registered in hwmon pwm-fan driver.
> 
> Signed-off-by: zhanghongchen <zhanghongchen@loongson.cn>
> Signed-off-by: Yinbo Zhu <zhuyinbo@loongson.cn>
> ---
>  drivers/thermal/Kconfig        |  10 ++
>  drivers/thermal/Makefile       |   1 +
>  drivers/thermal/ls2k_thermal.c | 244 +++++++++++++++++++++++++++++++++
>  3 files changed, 255 insertions(+)
>  create mode 100644 drivers/thermal/ls2k_thermal.c
> 
> diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig
> index e052dae614eb..04f5c044bc94 100644
> --- a/drivers/thermal/Kconfig
> +++ b/drivers/thermal/Kconfig
> @@ -504,4 +504,14 @@ config KHADAS_MCU_FAN_THERMAL
>  	  If you say yes here you get support for the FAN controlled
>  	  by the Microcontroller found on the Khadas VIM boards.
>  
> +config LOONGSON2K_THERMAL
> +	tristate "Loongson 2K SOC series thermal driver"
> +	depends on OF
> +	default m

Why should it be module by default on x86 or ARM? See how other drivers
do it.

> +	help
> +	  Support for Thermal driver found on Loongson 2K SOC series platforms.
> +	  It supports one critical trip point and one passive trip point. The
> +	  cpufreq and the pwm fan is used as the cooling device to throttle CPUs
> +	  when the passive trip is crossed.
> +
>  endif

(...)

> +
> +static int ls2k_thermal_probe(struct platform_device *pdev)
> +{
> +	struct device *dev = &pdev->dev;
> +	struct resource *res;
> +	struct ls2k_thermal_data *data;
> +	int ret;
> +
> +	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
> +	if (!data)
> +		return -ENOMEM;
> +
> +	data->pdev = pdev;
> +	platform_set_drvdata(pdev, data);
> +
> +	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> +	data->regs = devm_ioremap(dev, res->start, resource_size(res));

There is a helper combining these two.

> +	if (IS_ERR(data->regs)) {
> +		dev_err(dev, "failed to get io address\n");

I think error msg can be skipped in such case. Core should print it.

> +		return PTR_ERR(data->regs);
> +	}
> +
> +	/* get irq */
> +	data->irq = platform_get_irq(pdev, 0);
> +	if (data->irq < 0)
> +		return data->irq;
> +
> +	/* get id */
> +	if (of_property_read_u32(dev->of_node, "id", &data->id)) {
> +		dev_err(dev, "not found id property!\n");
> +		data->id = LS2K_SOC_DEFAULT_SENSOR;
> +	}
> +
> +	if (data->id > LS2K_SOC_MAX_SENSOR_NUM) {
> +		dev_err(dev, "sensor id error,must be in <0 ~ %d>\n",
> +				LS2K_SOC_MAX_SENSOR_NUM);
> +		return -EINVAL;
> +	}
> +
> +	writeb(0xff, data->regs + LS2K_TSENSOR_STATUS);
> +
> +	ls2k_tsensor_set(data, 0, 0, false);
> +
> +	data->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev,
> +							   data->id, data,
> +							   &ls2k_of_thermal_ops);
> +	if (IS_ERR(data->tzd)) {
> +		ret = PTR_ERR(data->tzd);
> +		data->tzd = NULL;
> +		dev_err(&pdev->dev, "failed to register %d\n", ret);
> +		return ret;
> +	}
> +
> +	ret = devm_request_threaded_irq(dev, data->irq,
> +			ls2k_thermal_alarm_irq, ls2k_thermal_irq_thread,
> +			IRQF_ONESHOT, "ls2k_thermal", data);
> +	if (ret < 0) {
> +		dev_err(dev, "failed to request alarm irq: %d\n", ret);
> +		return ret;
> +	}
> +
> +	/*
> +	 * Thermal_zone doesn't enable hwmon as default,
> +	 * enable it here
> +	 */
> +	data->tzd->tzp->no_hwmon = false;
> +	ret = thermal_add_hwmon_sysfs(data->tzd);
> +	if (ret) {
> +		dev_err(dev, "failed to add hwmon sysfs interface %d\n", ret);
> +		return ret;
> +	}
> +
> +	return 0;
> +}
> +
> +int ls2k_thermal_remove(struct platform_device *pdev)
> +{
> +	struct ls2k_thermal_data *data = platform_get_drvdata(pdev);
> +	int reg_off = data->id * 2;
> +
> +	/* disable interrupt */
> +	writew(0, data->regs + LS2K_TSENSOR_CTRL_LO + reg_off);
> +	writew(0, data->regs + LS2K_TSENSOR_CTRL_HI + reg_off);
> +
> +	return 0;
> +}
> +
> +static const struct of_device_id of_ls2k_thermal_match[] = {
> +	{ .compatible = "loongson,2k-tsensor",},
> +	{ /* end */ }
> +};
> +MODULE_DEVICE_TABLE(of, of_ls2k_thermal_match);
> +
> +#ifdef CONFIG_PM_SLEEP
> +static int ls2k_thermal_suspend(struct device *dev)
> +{
> +	struct ls2k_thermal_data *data = dev_get_drvdata(dev);
> +	int reg_off = data->id * 2;
> +
> +	data->ctrl_low_val = readw(data->regs + LS2K_TSENSOR_CTRL_LO + reg_off);
> +	data->ctrl_hi_val = readw(data->regs + LS2K_TSENSOR_CTRL_HI + reg_off);
> +
> +	writew(0, data->regs + LS2K_TSENSOR_CTRL_LO + reg_off);
> +	writew(0, data->regs + LS2K_TSENSOR_CTRL_HI + reg_off);
> +
> +	return 0;
> +}
> +
> +static int ls2k_thermal_resume(struct device *dev)
> +{
> +	struct ls2k_thermal_data *data = dev_get_drvdata(dev);
> +	int reg_off = data->id * 2;
> +
> +	writew(data->ctrl_low_val, data->regs + LS2K_TSENSOR_CTRL_LO + reg_off);
> +	writew(data->ctrl_hi_val, data->regs + LS2K_TSENSOR_CTRL_HI + reg_off);
> +
> +	return 0;
> +}
> +
> +static SIMPLE_DEV_PM_OPS(ls2k_thermal_pm_ops,
> +			 ls2k_thermal_suspend, ls2k_thermal_resume);
> +#endif
> +
> +static struct platform_driver ls2k_thermal_driver = {
> +	.driver = {
> +		.name		= "ls2k_thermal",
> +#ifdef CONFIG_PM_SLEEP

pm_ptr() (and use same approach as its users - no need for ifdefs,
DEFINE_SIMPLE_DEV_PM_OPS)


> +		.pm = &ls2k_thermal_pm_ops,
> +#endif
> +		.of_match_table = of_ls2k_thermal_match,
> +	},
> +	.probe	= ls2k_thermal_probe,
> +	.remove	= ls2k_thermal_remove,
> +};
> +module_platform_driver(ls2k_thermal_driver);


Best regards,
Krzysztof

  reply	other threads:[~2022-09-19  7:11 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-19  3:49 [PATCH v1 1/3] MAINTAINERS: add maintainer for thermal driver for ls2k SoCs Yinbo Zhu
2022-09-19  3:49 ` [PATCH v1 2/3] dt-bindings: thermal: Convert ls2k to json-schema Yinbo Zhu
2022-09-19  6:58   ` Krzysztof Kozlowski
2022-09-19  7:03   ` Krzysztof Kozlowski
2022-09-19  3:49 ` [PATCH v1 3/3] thermal: ls2k: add thermal management support Yinbo Zhu
2022-09-19  7:11   ` Krzysztof Kozlowski [this message]
2022-09-19  8:58   ` kernel test robot

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=b5578e0f-ac41-9889-a6bb-4b1d74197ec0@linaro.org \
    --to=krzysztof.kozlowski@linaro.org \
    --cc=amitk@kernel.org \
    --cc=daniel.lezcano@linaro.org \
    --cc=devicetree@vger.kernel.org \
    --cc=krzysztof.kozlowski+dt@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=rafael@kernel.org \
    --cc=robh+dt@kernel.org \
    --cc=rui.zhang@intel.com \
    --cc=zhanghongchen@loongson.cn \
    --cc=zhuyinbo@loongson.cn \
    /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

all inboxes | Powered by JetHome®