linux-hwmon.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Guenter Roeck <linux@roeck-us.net>
To: Neil Armstrong <narmstrong@baylibre.com>,
	lee.jones@linaro.org, jdelvare@suse.com,
	srinivas.kandagatla@linaro.org
Cc: nick@khadas.com, art@khadas.com,
	linux-amlogic@lists.infradead.org, linux-hwmon@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org
Subject: Re: [RFC 3/8] hwmon: add support for the MCU controlled FAN on Khadas boards
Date: Tue, 21 Apr 2020 02:24:30 -0700	[thread overview]
Message-ID: <37d7db98-cae8-4d97-8e39-88602756dd62@roeck-us.net> (raw)
In-Reply-To: <20200421080102.22796-4-narmstrong@baylibre.com>

On 4/21/20 1:00 AM, Neil Armstrong wrote:
> The new Khadas VIM2 and VIM3 boards controls the cooling fan via the
> on-board microcontroller.
> 
> This implements the FAN control as HWMON devices as cell of the Khadas
> MCU MFD driver.
> 
> Signed-off-by: Neil Armstrong <narmstrong@baylibre.com>

This should be a thermal driver, not a hwmon driver.
I don't see a single standard hwmon attribute, making
the driver quite pointless as hwmon device.

Guenter

> ---
>  drivers/hwmon/Kconfig          |   9 ++
>  drivers/hwmon/Makefile         |   1 +
>  drivers/hwmon/khadas-mcu-fan.c | 230 +++++++++++++++++++++++++++++++++
>  3 files changed, 240 insertions(+)
>  create mode 100644 drivers/hwmon/khadas-mcu-fan.c
> 
> diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig
> index 4c62f900bf7e..7f63ff7bb732 100644
> --- a/drivers/hwmon/Kconfig
> +++ b/drivers/hwmon/Kconfig
> @@ -1997,6 +1997,15 @@ config SENSORS_XGENE
>  	  If you say yes here you get support for the temperature
>  	  and power sensors for APM X-Gene SoC.
>  
> +config SENSORS_KHADAS_MCU_FAN
> +	tristate "Khadas MCU controller FAN driver"
> +	depends on OF || COMPILE_TEST
> +	select MFD_CORE
> +	select REGMAP
> +	help
> +	  If you say yes here you get support for the FAN controlled
> +	  by the Microcontroller found on the Khadas VIM boards.
> +
>  if ACPI
>  
>  comment "ACPI drivers"
> diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile
> index b0b9c8e57176..20db40f6d57f 100644
> --- a/drivers/hwmon/Makefile
> +++ b/drivers/hwmon/Makefile
> @@ -185,6 +185,7 @@ obj-$(CONFIG_SENSORS_W83L786NG)	+= w83l786ng.o
>  obj-$(CONFIG_SENSORS_WM831X)	+= wm831x-hwmon.o
>  obj-$(CONFIG_SENSORS_WM8350)	+= wm8350-hwmon.o
>  obj-$(CONFIG_SENSORS_XGENE)	+= xgene-hwmon.o
> +obj-$(CONFIG_SENSORS_KHADAS_MCU_FAN)	+= khadas-mcu-fan.o
>  
>  obj-$(CONFIG_SENSORS_OCC)	+= occ/
>  obj-$(CONFIG_PMBUS)		+= pmbus/
> diff --git a/drivers/hwmon/khadas-mcu-fan.c b/drivers/hwmon/khadas-mcu-fan.c
> new file mode 100644
> index 000000000000..dd92b2bcb7e3
> --- /dev/null
> +++ b/drivers/hwmon/khadas-mcu-fan.c
> @@ -0,0 +1,230 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Khadas MCU Controlled FAN driver
> + *
> + * Copyright (C) 2020 BayLibre SAS
> + * Author(s): Neil Armstrong <narmstrong@baylibre.com>
> + */
> +
> +#include <linux/hwmon.h>
> +#include <linux/hwmon-sysfs.h>
> +#include <linux/module.h>
> +#include <linux/of.h>
> +#include <linux/platform_device.h>
> +#include <linux/regulator/consumer.h>
> +#include <linux/mfd/khadas-mcu.h>
> +#include <linux/regmap.h>
> +#include <linux/sysfs.h>
> +#include <linux/thermal.h>
> +
> +#define MAX_LEVEL 3
> +
> +struct khadas_mcu_fan_ctx {
> +	struct khadas_mcu *mcu;
> +	unsigned int level;
> +	struct thermal_cooling_device *cdev;
> +};
> +
> +static int khadas_mcu_fan_set_level(struct khadas_mcu_fan_ctx *ctx,
> +				    unsigned int level)
> +{
> +	int ret;
> +
> +	ret = regmap_write(ctx->mcu->map, KHADAS_MCU_CMD_FAN_STATUS_CTRL_REG,
> +			   level);
> +	if (ret)
> +		return ret;
> +
> +	ctx->level = level;
> +
> +	return 0;
> +}
> +
> +static ssize_t level_store(struct device *dev, struct device_attribute *attr,
> +			   const char *buf, size_t count)
> +{
> +	struct khadas_mcu_fan_ctx *ctx = dev_get_drvdata(dev);
> +	unsigned long level;
> +	int ret;
> +
> +	if (kstrtoul(buf, 10, &level) || level > MAX_LEVEL)
> +		return -EINVAL;
> +
> +	ret = khadas_mcu_fan_set_level(ctx, level);
> +	if (ret < 0)
> +		return ret;
> +
> +	return count;
> +}
> +
> +static ssize_t level_show(struct device *dev, struct device_attribute *attr,
> +			  char *buf)
> +{
> +	struct khadas_mcu_fan_ctx *ctx = dev_get_drvdata(dev);
> +
> +	return sprintf(buf, "%u\n", ctx->level);
> +}
> +
> +static SENSOR_DEVICE_ATTR_RW(level1, level, 0);
> +
> +static struct attribute *khadas_mcu_fan_attrs[] = {
> +	&sensor_dev_attr_level1.dev_attr.attr,
> +	NULL,
> +};
> +
> +static const struct attribute_group khadas_mcu_fan_group = {
> +	.attrs = khadas_mcu_fan_attrs,
> +};
> +
> +static const struct attribute_group *khadas_mcu_fan_groups[] = {
> +	&khadas_mcu_fan_group,
> +	NULL,
> +};
> +
> +/* thermal cooling device callbacks */
> +static int khadas_mcu_fan_get_max_state(struct thermal_cooling_device *cdev,
> +					unsigned long *state)
> +{
> +	struct khadas_mcu_fan_ctx *ctx = cdev->devdata;
> +
> +	if (!ctx)
> +		return -EINVAL;
> +
> +	*state = MAX_LEVEL;
> +
> +	return 0;
> +}
> +
> +static int khadas_mcu_fan_get_cur_state(struct thermal_cooling_device *cdev,
> +					unsigned long *state)
> +{
> +	struct khadas_mcu_fan_ctx *ctx = cdev->devdata;
> +
> +	if (!ctx)
> +		return -EINVAL;
> +
> +	*state = ctx->level;
> +
> +	return 0;
> +}
> +
> +static int
> +khadas_mcu_fan_set_cur_state(struct thermal_cooling_device *cdev,
> +			     unsigned long state)
> +{
> +	struct khadas_mcu_fan_ctx *ctx = cdev->devdata;
> +
> +	if (!ctx || (state > MAX_LEVEL))
> +		return -EINVAL;
> +
> +	if (state == ctx->level)
> +		return 0;
> +
> +	return khadas_mcu_fan_set_level(ctx, state);
> +}
> +
> +static const struct thermal_cooling_device_ops khadas_mcu_fan_cooling_ops = {
> +	.get_max_state = khadas_mcu_fan_get_max_state,
> +	.get_cur_state = khadas_mcu_fan_get_cur_state,
> +	.set_cur_state = khadas_mcu_fan_set_cur_state,
> +};
> +
> +static int khadas_mcu_fan_probe(struct platform_device *pdev)
> +{
> +	struct khadas_mcu *mcu = dev_get_drvdata(pdev->dev.parent);
> +	struct thermal_cooling_device *cdev;
> +	struct device *dev = &pdev->dev;
> +	struct khadas_mcu_fan_ctx *ctx;
> +	struct device *hwmon;
> +	int ret;
> +
> +	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
> +	if (!ctx)
> +		return -ENOMEM;
> +	ctx->mcu = mcu;
> +	platform_set_drvdata(pdev, ctx);
> +
> +	hwmon = devm_hwmon_device_register_with_groups(dev, "khadas-mcu-fan",
> +						       ctx,
> +						       khadas_mcu_fan_groups);
> +	if (IS_ERR(hwmon)) {
> +		dev_err(dev, "Failed to register hwmon device\n");
> +		return PTR_ERR(hwmon);
> +	}
> +
> +	if (IS_ENABLED(CONFIG_THERMAL)) {
> +		cdev = devm_thermal_of_cooling_device_register(dev->parent,
> +			dev->parent->of_node, "khadas-mcu-fan", ctx,
> +			&khadas_mcu_fan_cooling_ops);
> +		if (IS_ERR(cdev)) {
> +			ret = PTR_ERR(cdev);
> +			dev_err(dev,
> +				"Failed to register khadas-mcu-fan as cooling device: %d\n",
> +				ret);
> +			return ret;
> +		}
> +		ctx->cdev = cdev;
> +		thermal_cdev_update(cdev);
> +	}
> +
> +	return 0;
> +}
> +
> +static int khadas_mcu_fan_disable(struct device *dev)
> +{
> +	struct khadas_mcu_fan_ctx *ctx = dev_get_drvdata(dev);
> +	unsigned int level_save = ctx->level;
> +	int ret;
> +
> +	ret = khadas_mcu_fan_set_level(ctx, 0);
> +	if (ret)
> +		return ret;
> +
> +	ctx->level = level_save;
> +
> +	return 0;
> +}
> +
> +static void khadas_mcu_fan_shutdown(struct platform_device *pdev)
> +{
> +	khadas_mcu_fan_disable(&pdev->dev);
> +}
> +
> +#ifdef CONFIG_PM_SLEEP
> +static int khadas_mcu_fan_suspend(struct device *dev)
> +{
> +	return khadas_mcu_fan_disable(dev);
> +}
> +
> +static int khadas_mcu_fan_resume(struct device *dev)
> +{
> +	struct khadas_mcu_fan_ctx *ctx = dev_get_drvdata(dev);
> +
> +	return khadas_mcu_fan_set_level(ctx, ctx->level);
> +}
> +#endif
> +
> +static SIMPLE_DEV_PM_OPS(khadas_mcu_fan_pm, khadas_mcu_fan_suspend,
> +			 khadas_mcu_fan_resume);
> +
> +static const struct platform_device_id khadas_mcu_fan_id_table[] = {
> +	{ .name = "khadas-mcu-fan-ctrl", },
> +	{},
> +};
> +MODULE_DEVICE_TABLE(platform, khadas_mcu_fan_id_table);
> +
> +static struct platform_driver khadas_mcu_fan_driver = {
> +	.probe		= khadas_mcu_fan_probe,
> +	.shutdown	= khadas_mcu_fan_shutdown,
> +	.driver	= {
> +		.name		= "khadas-mcu-fan-ctrl",
> +		.pm		= &khadas_mcu_fan_pm,
> +	},
> +	.id_table	= khadas_mcu_fan_id_table,
> +};
> +
> +module_platform_driver(khadas_mcu_fan_driver);
> +
> +MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
> +MODULE_DESCRIPTION("Khadas MCU FAN driver");
> +MODULE_LICENSE("GPL");
> 


  reply	other threads:[~2020-04-21  9:24 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-21  8:00 [RFC 0/8] mfd: Add support for Khadas Microcontroller Neil Armstrong
2020-04-21  8:00 ` [RFC 1/8] dt-bindings: mfd: add Khadas Microcontroller bindings Neil Armstrong
2020-05-06 20:12   ` Rob Herring
2020-04-21  8:00 ` [RFC 2/8] mfd: add support for the Khadas System control Microcontroller Neil Armstrong
2020-04-21  8:00 ` [RFC 3/8] hwmon: add support for the MCU controlled FAN on Khadas boards Neil Armstrong
2020-04-21  9:24   ` Guenter Roeck [this message]
2020-04-28 12:58     ` Neil Armstrong
2020-04-21  8:00 ` [RFC 4/8] nvmem: add support for the Khadas MCU Programmable User Memory Neil Armstrong
2020-04-21  8:00 ` [RFC 5/8] MAINTAINERS: add myself as maintainer for Khadas MCU drivers Neil Armstrong
2020-04-21  8:01 ` [RFC 6/8] arm64: dts: meson-g12b: move G12B thermal nodes to meson-g12b.dtsi Neil Armstrong
2020-04-21  8:01 ` [RFC 7/8] arm64: dts: meson-sm1: add cpu thermal nodes Neil Armstrong
2020-04-21  8:01 ` [RFC 8/8] arm64: dts: meson-khadas-vim3: add Khadas MCU nodes Neil Armstrong
2020-04-29 19:16 ` [RFC 0/8] mfd: Add support for Khadas Microcontroller Kevin Hilman
2020-05-07  7:44   ` Neil Armstrong

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=37d7db98-cae8-4d97-8e39-88602756dd62@roeck-us.net \
    --to=linux@roeck-us.net \
    --cc=art@khadas.com \
    --cc=jdelvare@suse.com \
    --cc=lee.jones@linaro.org \
    --cc=linux-amlogic@lists.infradead.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=narmstrong@baylibre.com \
    --cc=nick@khadas.com \
    --cc=srinivas.kandagatla@linaro.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).