linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Junhao Xie <bigfoot@classfun.cn>
To: devicetree@vger.kernel.org, linux-hwmon@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-leds@vger.kernel.org,
	linux-pm@vger.kernel.org, linux-rtc@vger.kernel.org,
	linux-watchdog@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org,
	linux-rockchip@lists.infradead.org
Cc: Jean Delvare <jdelvare@suse.com>,
	Guenter Roeck <linux@roeck-us.net>, Rob Herring <robh@kernel.org>,
	Krzysztof Kozlowski <krzk+dt@kernel.org>,
	Conor Dooley <conor+dt@kernel.org>, Pavel Machek <pavel@ucw.cz>,
	Lee Jones <lee@kernel.org>, Sebastian Reichel <sre@kernel.org>,
	Alexandre Belloni <alexandre.belloni@bootlin.com>,
	Wim Van Sebroeck <wim@linux-watchdog.org>,
	Heiko Stuebner <heiko@sntech.de>, Chukun Pan <amadeus@jmu.edu.cn>,
	Junhao Xie <bigfoot@classfun.cn>
Subject: [PATCH 6/9] hwmon: Add support for Photonicat PMU board temperature sensor
Date: Fri,  6 Sep 2024 17:36:27 +0800	[thread overview]
Message-ID: <20240906093630.2428329-7-bigfoot@classfun.cn> (raw)
In-Reply-To: <20240906093630.2428329-1-bigfoot@classfun.cn>

Photonicat PMU MCU will send status reports regularly,
including board temperature.

Signed-off-by: Junhao Xie <bigfoot@classfun.cn>
---
 drivers/hwmon/Kconfig            |  10 +++
 drivers/hwmon/Makefile           |   1 +
 drivers/hwmon/photonicat-hwmon.c | 129 +++++++++++++++++++++++++++++++
 3 files changed, 140 insertions(+)
 create mode 100644 drivers/hwmon/photonicat-hwmon.c

diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig
index b60fe2e58ad6..b345e4856bcf 100644
--- a/drivers/hwmon/Kconfig
+++ b/drivers/hwmon/Kconfig
@@ -1781,6 +1781,16 @@ config SENSORS_PT5161L
 	  This driver can also be built as a module. If so, the module
 	  will be called pt5161l.
 
+config SENSORS_PHOTONICAT_PMU_HWMON
+	tristate "Photonicat PMU temperature sensor"
+	depends on MFD_PHOTONICAT_PMU
+	help
+	  If you say yes here you get support for temperature sensor on the
+	  Photonicat PMU.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called photonicat-hwmon.
+
 config SENSORS_PWM_FAN
 	tristate "PWM fan"
 	depends on PWM || COMPILE_TEST
diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile
index b1c7056c37db..a30658acb093 100644
--- a/drivers/hwmon/Makefile
+++ b/drivers/hwmon/Makefile
@@ -183,6 +183,7 @@ obj-$(CONFIG_SENSORS_OXP) += oxp-sensors.o
 obj-$(CONFIG_SENSORS_PC87360)	+= pc87360.o
 obj-$(CONFIG_SENSORS_PC87427)	+= pc87427.o
 obj-$(CONFIG_SENSORS_PCF8591)	+= pcf8591.o
+obj-$(CONFIG_SENSORS_PHOTONICAT_PMU_HWMON)	+= photonicat-hwmon.o
 obj-$(CONFIG_SENSORS_POWERZ)	+= powerz.o
 obj-$(CONFIG_SENSORS_POWR1220)  += powr1220.o
 obj-$(CONFIG_SENSORS_PT5161L)	+= pt5161l.o
diff --git a/drivers/hwmon/photonicat-hwmon.c b/drivers/hwmon/photonicat-hwmon.c
new file mode 100644
index 000000000000..f1e3ee5f5781
--- /dev/null
+++ b/drivers/hwmon/photonicat-hwmon.c
@@ -0,0 +1,129 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2024 Junhao Xie <bigfoot@classfun.cn>
+ */
+
+#include <linux/completion.h>
+#include <linux/hwmon.h>
+#include <linux/mfd/photonicat-pmu.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+
+struct pcat_hwmon {
+	struct device *dev;
+	struct pcat_pmu *pmu;
+	struct notifier_block nb;
+	struct device *hwmon;
+	int temperature;
+	struct completion initial_report;
+};
+
+static ssize_t temp1_input_show(struct device *dev,
+				struct device_attribute *devattr, char *buf)
+{
+	struct pcat_hwmon *hwmon = dev_get_drvdata(dev);
+
+	return sprintf(buf, "%d\n", hwmon->temperature * 1000);
+}
+
+static DEVICE_ATTR_RO(temp1_input);
+
+static struct attribute *pcat_pmu_temp_attrs[] = {
+	&dev_attr_temp1_input.attr,
+	NULL
+};
+
+ATTRIBUTE_GROUPS(pcat_pmu_temp);
+
+static int pcat_hwmon_notify(struct notifier_block *nb, unsigned long action,
+			     void *data)
+{
+	struct pcat_hwmon *hwmon = container_of(nb, struct pcat_hwmon, nb);
+	struct pcat_data_cmd_status *status = pcat_data_get_data(data);
+
+	if (action != PCAT_CMD_STATUS_REPORT)
+		return NOTIFY_DONE;
+
+	hwmon->temperature = status->temp - 40;
+	complete(&hwmon->initial_report);
+
+	return NOTIFY_DONE;
+}
+
+static int pcat_hwmon_probe(struct platform_device *pdev)
+{
+	int ret;
+	struct device *dev = &pdev->dev;
+	struct pcat_hwmon *hwmon;
+	const char *label;
+
+	hwmon = devm_kzalloc(dev, sizeof(*hwmon), GFP_KERNEL);
+	if (!hwmon)
+		return -ENOMEM;
+
+	hwmon->dev = dev;
+	hwmon->pmu = dev_get_drvdata(dev->parent);
+	hwmon->nb.notifier_call = pcat_hwmon_notify;
+	init_completion(&hwmon->initial_report);
+	platform_set_drvdata(pdev, hwmon);
+
+	ret = of_property_read_string(dev->of_node, "label", &label);
+	if (ret)
+		return dev_err_probe(dev, ret, "No label property\n");
+
+	ret = pcat_pmu_register_notify(hwmon->pmu, &hwmon->nb);
+	if (ret)
+		return ret;
+
+	if (!wait_for_completion_timeout(&hwmon->initial_report,
+					 msecs_to_jiffies(3000))) {
+		ret = dev_err_probe(dev, -ETIMEDOUT,
+				    "timeout waiting for initial report\n");
+		goto error;
+	}
+
+	dev_info(dev, "Board Temprature: %d degress C\n", hwmon->temperature);
+
+	hwmon->hwmon = devm_hwmon_device_register_with_groups(
+		dev, label, hwmon, pcat_pmu_temp_groups);
+
+	if (IS_ERR(hwmon->hwmon)) {
+		ret = PTR_ERR(hwmon->hwmon);
+		dev_err_probe(&pdev->dev, ret,
+			      "Failed to register hwmon device\n");
+		goto error;
+	}
+
+	return 0;
+error:
+	pcat_pmu_unregister_notify(hwmon->pmu, &hwmon->nb);
+	return ret;
+}
+
+static void pcat_hwmon_remove(struct platform_device *pdev)
+{
+	struct pcat_hwmon *hwmon = platform_get_drvdata(pdev);
+
+	pcat_pmu_unregister_notify(hwmon->pmu, &hwmon->nb);
+}
+
+static const struct of_device_id pcat_hwmon_dt_ids[] = {
+	{ .compatible = "ariaboard,photonicat-pmu-hwmon", },
+	{ /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(of, pcat_hwmon_dt_ids);
+
+static struct platform_driver pcat_hwmon_driver = {
+	.driver = {
+		.name = "photonicat-hwmon",
+		.of_match_table = pcat_hwmon_dt_ids,
+	},
+	.probe = pcat_hwmon_probe,
+	.remove = pcat_hwmon_remove,
+};
+module_platform_driver(pcat_hwmon_driver);
+
+MODULE_AUTHOR("Junhao Xie <bigfoot@classfun.cn>");
+MODULE_DESCRIPTION("Photonicat PMU Hardware Monitor");
+MODULE_LICENSE("GPL");
-- 
2.46.0



  parent reply	other threads:[~2024-09-06  9:52 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-06  9:36 [PATCH 0/9] Introduce Photonicat power management MCU driver Junhao Xie
2024-09-06  9:36 ` [PATCH 1/9] mfd: Add driver for Photonicat power management MCU Junhao Xie
2024-09-06  9:43   ` Krzysztof Kozlowski
2024-09-06 10:40     ` Junhao Xie
2024-09-07  8:10   ` Markus Elfring
2024-09-07 14:46     ` Junhao Xie
2024-09-07  8:44   ` Markus Elfring
2024-09-07 14:33     ` Junhao Xie
2024-09-08  8:14       ` Krzysztof Kozlowski
2024-09-12  7:55         ` Lee Jones
2024-09-06  9:36 ` [PATCH 2/9] power: reset: add Photonicat PMU poweroff driver Junhao Xie
2024-09-06  9:44   ` Krzysztof Kozlowski
2024-09-06 10:05     ` Junhao Xie
2024-09-06  9:36 ` [PATCH 3/9] watchdog: Add Photonicat PMU watchdog driver Junhao Xie
2024-09-06 11:52   ` Guenter Roeck
2024-09-06 13:41     ` Junhao Xie
2024-09-06  9:36 ` [PATCH 4/9] power: supply: photonicat-supply: Add Photonicat PMU battery and charger Junhao Xie
2024-09-06  9:36 ` [PATCH 5/9] rtc: Add Photonicat PMU real-time clock Junhao Xie
2024-09-06  9:36 ` Junhao Xie [this message]
2024-09-06 11:41   ` [PATCH 6/9] hwmon: Add support for Photonicat PMU board temperature sensor Guenter Roeck
2024-09-06 13:49     ` Junhao Xie
2024-09-06 14:33       ` Guenter Roeck
2024-09-06  9:36 ` [PATCH 7/9] leds: add Photonicat PMU LED driver Junhao Xie
2024-10-02 15:35   ` Lee Jones
2024-11-08  3:48     ` Junhao Xie
2024-09-06  9:36 ` [PATCH 8/9] dt-bindings: Add documentation for Photonicat PMU Junhao Xie
2024-09-06  9:51   ` Krzysztof Kozlowski
2024-09-07 14:27     ` Junhao Xie
2024-09-08  8:13       ` Krzysztof Kozlowski
2024-09-06  9:36 ` [PATCH 9/9] arm64: dts: rockchip: add Photonicat PMU support for Ariaboard Photonicat Junhao Xie
2024-09-06  9:53   ` Krzysztof Kozlowski
2024-09-06 13:56     ` Junhao Xie
2024-09-06  9:45 ` [PATCH 0/9] Introduce Photonicat power management MCU driver Krzysztof Kozlowski
2024-09-06 10:20   ` Junhao Xie
2024-09-08  9:30 ` Chukun Pan
2024-09-11  6:23   ` Junhao Xie

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=20240906093630.2428329-7-bigfoot@classfun.cn \
    --to=bigfoot@classfun.cn \
    --cc=alexandre.belloni@bootlin.com \
    --cc=amadeus@jmu.edu.cn \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=heiko@sntech.de \
    --cc=jdelvare@suse.com \
    --cc=krzk+dt@kernel.org \
    --cc=lee@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-leds@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=linux-rockchip@lists.infradead.org \
    --cc=linux-rtc@vger.kernel.org \
    --cc=linux-watchdog@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=pavel@ucw.cz \
    --cc=robh@kernel.org \
    --cc=sre@kernel.org \
    --cc=wim@linux-watchdog.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).