Skip to contentSkip to conversation

hwmon: Add StarFive JH7100 temperature sensor

PATCH Superseded Emil Renner Berthing 2 participants 5 messages Jun 16, 2021 – Jun 17, 2021

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Emil Renner Berthing

author maintainer
#0 lore raw

cover hwmon: Add StarFive JH7100 temperature sensor

This adds a driver for the temperature sensor on the JH7100, a RISC-V SoC by StarFive Technology Co. Ltd., and most likely also the upcoming JH7110 version of the chip.

The SoC is used on the BeagleV Starlight board:
https://github.com/beagleboard/beaglev-starlight

Support for this SoC is not yet upstreamed so feel free to not merge yet but I'd love some early feedback.

/Emil

Emil Renner Berthing (2):
dt-bindings: hwmon: add starfive,jh7100-temp bindings
hwmon: (sfctemp) Add StarFive JH7100 temperature sensor

 .../bindings/hwmon/starfive,jh7100-temp.yaml  |  43 +++
 drivers/hwmon/Kconfig                         |   9 +
 drivers/hwmon/Makefile                        |   1 +
 drivers/hwmon/sfctemp.c                       | 309 ++++++++++++++++++
 4 files changed, 362 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/hwmon/starfive,jh7100-temp.yaml
 create mode 100644 drivers/hwmon/sfctemp.c
Signature · 1 line
2.32.0
author maintainer
#1 lore raw

PATCH 1/2 dt-bindings: hwmon: add starfive,jh7100-temp bindings

Add bindings for the temperature sensor on the StarFive JH7100 SoC.

---
 .../bindings/hwmon/starfive,jh7100-temp.yaml  | 43 +++++++++++++++++++
 1 file changed, 43 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/hwmon/starfive,jh7100-temp.yaml
Documentation/devicetree/bindings/hwmon/starfive,jh7100-temp.yaml new file +43 -0
new file mode 100644
index 000000000000..5ca52c08d142
@@ -0,0 +1,43 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/hwmon/starfive,jh7100-temp.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: StarFive JH7100 Temperature Sensor
+
+maintainers:
+  - Emil Renner Berthing <[email protected]>
+
+description: |
+  StarFive Technology Co. JH7100 embedded temperature sensor
+
+properties:
+  compatible:
+    enum:
+      - starfive,jh7100-temp
+
+  reg:
+    maxItems: 1
+
+  '#thermal-sensor-cells':
+    const: 0
+
+  interrupts:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+  - interrupts
+
+additionalProperties: false
+
+examples:
+  - |
+    tmon: tmon@124a0000 {
+        compatible = "starfive,jh7100-temp";
+        reg = <0x124a0000 0x10000>;
+        #thermal-sensor-cells = <0>;
+        interrupts = <122>;
+    };
Signature · 1 line
2.32.0
author maintainer
#2 lore raw

PATCH 2/2 hwmon: (sfctemp) Add StarFive JH7100 temperature sensor

In reply to Emil Renner Berthing

Register definitions based on sfctemp driver in the StarFive 5.10 kernel by Samin Guo <[email protected]>.

---

@Samin: Only the register definitions and conversion constants are left from your driver, but still it would be really nice if you could reply to this mail with your Signed-off-by. Thanks!

 drivers/hwmon/Kconfig   |   9 ++
 drivers/hwmon/Makefile  |   1 +
 drivers/hwmon/sfctemp.c | 309 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 319 insertions(+)
 create mode 100644 drivers/hwmon/sfctemp.c
drivers/hwmon/Kconfig +9 -0
index 87624902ea80..fa7562920dfa 100644
@@ -1751,6 +1751,15 @@ config SENSORS_STTS751
 	  This driver can also be built as a module. If so, the module
 	  will be called stts751.
 
+config SENSORS_SFCTEMP
+	tristate "Starfive JH7100 temperature sensor"
+	help
+	  If you say yes here you get support for temperature sensor
+	  on the Starfive JH7100 SoC.
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called sfctemp.
+
 config SENSORS_SMM665
 	tristate "Summit Microelectronics SMM665"
 	depends on I2C
drivers/hwmon/Makefile +1 -0
index 59e78bc212cf..3723eb580bf3 100644
@@ -167,6 +167,7 @@ obj-$(CONFIG_SENSORS_SBTSI)	+= sbtsi_temp.o
 obj-$(CONFIG_SENSORS_SCH56XX_COMMON)+= sch56xx-common.o
 obj-$(CONFIG_SENSORS_SCH5627)	+= sch5627.o
 obj-$(CONFIG_SENSORS_SCH5636)	+= sch5636.o
+obj-$(CONFIG_SENSORS_SFCTEMP)	+= sfctemp.o
 obj-$(CONFIG_SENSORS_SL28CPLD)	+= sl28cpld-hwmon.o
 obj-$(CONFIG_SENSORS_SHT15)	+= sht15.o
 obj-$(CONFIG_SENSORS_SHT21)	+= sht21.o
drivers/hwmon/sfctemp.c new file +309 -0

309 lines not on this page · raw message

Signature · 1 line
2.32.0
maintainer
#3 lore raw

Re: [PATCH v1 2/2] hwmon: (sfctemp) Add StarFive JH7100 temperature sensor

Quoted 6 lines · Emil Renner Berthing

Register definitions based on sfctemp driver in the StarFive 5.10 kernel by Samin Guo <[email protected]>.

---

Quoted 8 lines

@Samin: Only the register definitions and conversion constants are left from your driver, but still it would be really nice if you could reply to this mail with your Signed-off-by. Thanks!

 drivers/hwmon/Kconfig   |   9 ++
 drivers/hwmon/Makefile  |   1 +
 drivers/hwmon/sfctemp.c | 309 ++++++++++++++++++++++++++++++++++++++++

Documentation/hwmon/sfctemp is missing.

Quoted 44 lines
 3 files changed, 319 insertions(+)
 create mode 100644 drivers/hwmon/sfctemp.c
drivers/hwmon/Kconfig +9 -0
index 87624902ea80..fa7562920dfa 100644
@@ -1751,6 +1751,15 @@ config SENSORS_STTS751
 	  This driver can also be built as a module. If so, the module
 	  will be called stts751.
 
+config SENSORS_SFCTEMP
+	tristate "Starfive JH7100 temperature sensor"
+	help
+	  If you say yes here you get support for temperature sensor
+	  on the Starfive JH7100 SoC.
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called sfctemp.
+
 config SENSORS_SMM665
 	tristate "Summit Microelectronics SMM665"
 	depends on I2C
drivers/hwmon/Makefile +1 -0
index 59e78bc212cf..3723eb580bf3 100644
@@ -167,6 +167,7 @@ obj-$(CONFIG_SENSORS_SBTSI)	+= sbtsi_temp.o
 obj-$(CONFIG_SENSORS_SCH56XX_COMMON)+= sch56xx-common.o
 obj-$(CONFIG_SENSORS_SCH5627)	+= sch5627.o
 obj-$(CONFIG_SENSORS_SCH5636)	+= sch5636.o
+obj-$(CONFIG_SENSORS_SFCTEMP)	+= sfctemp.o
 obj-$(CONFIG_SENSORS_SL28CPLD)	+= sl28cpld-hwmon.o
 obj-$(CONFIG_SENSORS_SHT15)	+= sht15.o
 obj-$(CONFIG_SENSORS_SHT21)	+= sht21.o
drivers/hwmon/sfctemp.c new file +3 -0
new file mode 100644
index 000000000000..62a838063e4e
@@ -0,0 +1,309 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2021 Emil Renner Berthing <[email protected]>

drivers/hwmon/sfctemp.c line 13

Quoted 10 lines
drivers/hwmon/sfctemp.c new file +10 -0
+ * Copyright (C) 2021 Samin Guo <[email protected]>
+ */
+#include <linux/completion.h>
+#include <linux/delay.h>
+#include <linux/hwmon.h>
+#include <linux/interrupt.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>

This may work on some systems, but on others it will result in:

drivers/hwmon/sfctemp.c: In function ‘sfctemp_power_up’:
drivers/hwmon/sfctemp.c:76:2: error: implicit declaration of function ‘writel’

drivers/hwmon/sfctemp.c: In function ‘sfctemp_convert’:
drivers/hwmon/sfctemp.c:144:17: error: implicit declaration of function ‘readl’

It is necessary to include <linux/io.h>.

drivers/hwmon/sfctemp.c line 17

Quoted 4 lines
drivers/hwmon/sfctemp.c new file +4 -0
+
+/* TempSensor reset. The RSTN can be de-asserted once the analog core has
+ * powered up. Trst(min 100ns)
+ * 0:reset  1:de-assert */

/*
* Please use standard multi-line comments.
* checkpatch would tell you, plus it has other complaints.
* Please run checkpatch --strict and fix what it reports. */

Quoted 16 lines
drivers/hwmon/sfctemp.c new file +16 -0
+#define SFCTEMP_RSTN     BIT(0)
+
+/* TempSensor analog core power down. The analog core will be powered up
+ * Tpu(min 50us) after PD is de-asserted. RSTN should be held low until the
+ * analog core is powered up.
+ * 0:power up  1:power down */
+#define SFCTEMP_PD       BIT(1)
+
+/* TempSensor start conversion enable.
+ * 0:disable  1:enable */
+#define SFCTEMP_RUN      BIT(2)
+
+/* TempSensor calibration mode enable.
+ * 0:disable  1:enable */
+#define SFCTEMP_CAL      BIT(4)
+

drivers/hwmon/sfctemp.c line 43

Quoted 10 lines
drivers/hwmon/sfctemp.c new file +10 -0
+/* TempSensor signature enable. Generate a toggle value outputting on DOUT for
+ * test purpose.
+ * 0:disable  1:enable */
+#define SFCTEMP_SGN      BIT(5)
+
+/* TempSensor test access control.
+ * 0000:normal 0001:Test1  0010:Test2  0011:Test3
+ * 0100:Test4  1000:Test8  1001:Test9 */
+#define SFCTEMP_TM_Pos   12
+#define SFCTEMP_TM_Msk   GENMASK(15, 12)

Defines should upper case. Besides, the above defines are not used. Please remove unused defines, and please rename the others to be upper case only.

drivers/hwmon/sfctemp.c line 55

Quoted 12 lines
drivers/hwmon/sfctemp.c new file +12 -0
+
+/* TempSensor conversion value output.
+ * Temp(c)=DOUT*Y/4094 - K */
+#define SFCTEMP_DOUT_Pos 16
+#define SFCTEMP_DOUT_Msk GENMASK(27, 16)
+
+/* TempSensor digital test output. */
+#define SFCTEMP_DIGO     BIT(31)
+
+/* DOUT to Celcius conversion constants */
+#define SFCTEMP_Y1000 237500L
+#define SFCTEMP_Z       4094L

Please use

#define<space><definition><tab><value>, eg

#define SFCTEMP_Y1000 237500L
#define SFCTEMP_Z 4094L

for all defines.

Quoted 63 lines
drivers/hwmon/sfctemp.c new file +63 -0
+#define SFCTEMP_K1000  81100L
+
+struct sfctemp {
+	struct mutex lock;
+	struct completion conversion_done;
+	void __iomem *regs;
+	bool enabled;
+};
+
+static irqreturn_t sfctemp_isr(int irq, void *data)
+{
+	struct sfctemp *sfctemp = data;
+
+	complete(&sfctemp->conversion_done);
+	return IRQ_HANDLED;
+}
+
+static void sfctemp_power_up(struct sfctemp *sfctemp)
+{
+	/* make sure we're powered down first */
+	writel(SFCTEMP_PD, sfctemp->regs);
+	udelay(1);
+
+	writel(0, sfctemp->regs);
+	/* wait t_pu(50us) + t_rst(100ns) */
+	usleep_range(60, 200);
+
+	/* de-assert reset */
+	writel(SFCTEMP_RSTN, sfctemp->regs);
+	udelay(1); /* wait t_su(500ps) */
+}
+
+static void sfctemp_power_down(struct sfctemp *sfctemp)
+{
+	writel(SFCTEMP_PD, sfctemp->regs);
+}
+
+static void sfctemp_run_single(struct sfctemp *sfctemp)
+{
+	writel(SFCTEMP_RSTN | SFCTEMP_RUN, sfctemp->regs);
+	udelay(1);
+	writel(SFCTEMP_RSTN, sfctemp->regs);
+}
+
+static int sfctemp_enable(struct sfctemp *sfctemp)
+{
+	mutex_lock(&sfctemp->lock);
+	if (sfctemp->enabled)
+		goto done;
+
+	sfctemp_power_up(sfctemp);
+	sfctemp->enabled = true;
+done:
+	mutex_unlock(&sfctemp->lock);
+	return 0;
+}
+
+static int sfctemp_disable(struct sfctemp *sfctemp)
+{
+	mutex_lock(&sfctemp->lock);
+	if (!sfctemp->enabled)
+		goto done;
+

drivers/hwmon/sfctemp.c line 128

Quoted 10 lines
drivers/hwmon/sfctemp.c new file +10 -0
+	sfctemp_power_down(sfctemp);
+	sfctemp->enabled = false;
+done:
+	mutex_unlock(&sfctemp->lock);
+	return 0;
+}
+
+static int sfctemp_convert(struct sfctemp *sfctemp, long *val)
+{
+	long ret;

Why long ? Sure, wait_for_completion_interruptible_timeout() returns a long, but it will never return a value large enough to warrant a long here.

Quoted 125 lines
drivers/hwmon/sfctemp.c new file +125 -0
+
+	mutex_lock(&sfctemp->lock);
+	if (!sfctemp->enabled) {
+		ret = -ENODATA;
+		goto out;
+	}
+
+	sfctemp_run_single(sfctemp);
+
+	ret = wait_for_completion_interruptible_timeout(&sfctemp->conversion_done,
+			                                msecs_to_jiffies(10));
+	if (ret < 0)
+		goto out;
+
+	/* calculate temperature in milli Celcius */
+	*val = (long)((readl(sfctemp->regs) & SFCTEMP_DOUT_Msk) >> SFCTEMP_DOUT_Pos)
+		* SFCTEMP_Y1000 / SFCTEMP_Z - SFCTEMP_K1000;
+
+	ret = 0;
+out:
+	mutex_unlock(&sfctemp->lock);
+	return ret;
+}
+
+static umode_t sfctemp_is_visible(const void *data, enum hwmon_sensor_types type,
+				  u32 attr, int channel)
+{
+	switch (type) {
+	case hwmon_temp:
+		switch (attr) {
+		case hwmon_temp_enable:
+			return 0644;
+		case hwmon_temp_input:
+			return 0444;
+		}
+		return 0;
+	default:
+		return 0;
+	}
+}
+
+static int sfctemp_read(struct device *dev, enum hwmon_sensor_types type,
+			u32 attr, int channel, long *val)
+{
+	struct sfctemp *sfctemp = dev_get_drvdata(dev);
+
+	switch (type) {
+	case hwmon_temp:
+		switch (attr) {
+		case hwmon_temp_enable:
+			*val = sfctemp->enabled;
+			return 0;
+		case hwmon_temp_input:
+			return sfctemp_convert(sfctemp, val);
+		}
+		return -EINVAL;
+	default:
+		return -EINVAL;
+	}
+}
+
+static int sfctemp_write(struct device *dev, enum hwmon_sensor_types type,
+			 u32 attr, int channel, long val)
+{
+	struct sfctemp *sfctemp = dev_get_drvdata(dev);
+
+	switch (type) {
+	case hwmon_temp:
+		switch (attr) {
+		case hwmon_temp_enable:
+			if (val == 0)
+				return sfctemp_disable(sfctemp);
+			if (val == 1)
+				return sfctemp_enable(sfctemp);
+			break;
+		}
+		return -EINVAL;
+	default:
+		return -EINVAL;
+	}
+}
+
+static const struct hwmon_channel_info *sfctemp_info[] = {
+	HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ),
+	HWMON_CHANNEL_INFO(temp, HWMON_T_ENABLE | HWMON_T_INPUT),
+	NULL
+};
+
+static const struct hwmon_ops sfctemp_hwmon_ops = {
+	.is_visible = sfctemp_is_visible,
+	.read = sfctemp_read,
+	.write = sfctemp_write,
+};
+
+static const struct hwmon_chip_info sfctemp_chip_info = {
+	.ops = &sfctemp_hwmon_ops,
+	.info = sfctemp_info,
+};
+
+static int sfctemp_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct device *hwmon_dev;
+	struct resource *mem;
+	struct sfctemp *sfctemp;
+	long val;
+	int ret;
+
+	sfctemp = devm_kzalloc(dev, sizeof(*sfctemp), GFP_KERNEL);
+	if (!sfctemp)
+		return -ENOMEM;
+
+	dev_set_drvdata(dev, sfctemp);
+	mutex_init(&sfctemp->lock);
+	init_completion(&sfctemp->conversion_done);
+
+	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	sfctemp->regs = devm_ioremap_resource(dev, mem);
+	if (IS_ERR(sfctemp->regs))
+		return PTR_ERR(sfctemp->regs);
+
+	ret = platform_get_irq(pdev, 0);
+	if (ret < 0)
+		return ret;
+

drivers/hwmon/sfctemp.c line 263

Quoted 10 lines
drivers/hwmon/sfctemp.c new file +10 -0
+	ret = devm_request_irq(dev, ret, sfctemp_isr,
+			       IRQF_SHARED, pdev->name, sfctemp);
+	if (ret) {
+		dev_err(dev, "request irq failed: %d\n", ret);
+		return ret;
+	}
+
+	ret = sfctemp_enable(sfctemp);
+	if (ret)
+		return ret;

Please consider adding devm_add_action_or_reset() to disable the sensor. Then you can use devm_hwmon_device_register_with_info(), and there is no need for a remove function.

drivers/hwmon/sfctemp.c line 271

Quoted 8 lines
drivers/hwmon/sfctemp.c new file +8 -0
+
+	hwmon_dev = hwmon_device_register_with_info(dev, pdev->name, sfctemp,
+						    &sfctemp_chip_info, NULL);
+	if (IS_ERR(hwmon_dev))
+		return PTR_ERR(hwmon_dev);
+
+	/* do a conversion to check everything works */
+	ret = sfctemp_convert(sfctemp, &val);

Please drop this check; it will result in substantial boot delays. The assumption is the the hardware works; the system should not be optimized (slowed down) to handle a potential and hopefully rare failure case.

drivers/hwmon/sfctemp.c line 277

Quoted 6 lines
drivers/hwmon/sfctemp.c new file +6 -0
+	if (ret) {
+		hwmon_device_unregister(hwmon_dev);
+		return ret;
+	}
+
+	dev_info(dev, "%ld.%03ld C\n", val / 1000, val % 1000);

Please drop this message. Anyone interested in boot-time temperature readings can add a script and get it from sysfs attributes as needed.

Quoted 35 lines
drivers/hwmon/sfctemp.c new file +32 -0
+	return 0;
+}
+
+static int sfctemp_remove(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct sfctemp *sfctemp = dev_get_drvdata(dev);
+
+	hwmon_device_unregister(dev);
+	return sfctemp_disable(sfctemp);
+}
+
+static const struct of_device_id sfctemp_of_match[] = {
+	{ .compatible = "starfive,jh7100-temp" },
+	{ /* sentinel */ }
+};
+
+MODULE_DEVICE_TABLE(of, sfctemp_of_match);
+
+static struct platform_driver sfctemp_driver = {
+	.driver = {
+		.name = "sfctemp",
+		.of_match_table = of_match_ptr(sfctemp_of_match),
+	},
+	.probe  = sfctemp_probe,
+	.remove = sfctemp_remove,
+};
+module_platform_driver(sfctemp_driver);
+
+MODULE_AUTHOR("Emil Renner Berthing");
+MODULE_DESCRIPTION("StarFive JH7100 temperature sensor driver");
+MODULE_LICENSE("GPL");
Signature · 1 line
2.32.0
author maintainer
#4 lore raw

Re: [PATCH v1 2/2] hwmon: (sfctemp) Add StarFive JH7100 temperature sensor

Quoted 411 lines · Guenter Roeck
Quoted 6 lines · Emil Renner Berthing

Register definitions based on sfctemp driver in the StarFive 5.10 kernel by Samin Guo <[email protected]>.

---

Quoted 8 lines

@Samin: Only the register definitions and conversion constants are left from your driver, but still it would be really nice if you could reply to this mail with your Signed-off-by. Thanks!

 drivers/hwmon/Kconfig   |   9 ++
 drivers/hwmon/Makefile  |   1 +
 drivers/hwmon/sfctemp.c | 309 ++++++++++++++++++++++++++++++++++++++++

Documentation/hwmon/sfctemp is missing.

Quoted 44 lines
 3 files changed, 319 insertions(+)
 create mode 100644 drivers/hwmon/sfctemp.c
drivers/hwmon/Kconfig +9 -0
index 87624902ea80..fa7562920dfa 100644
@@ -1751,6 +1751,15 @@ config SENSORS_STTS751
        This driver can also be built as a module. If so, the module
        will be called stts751.
 
+config SENSORS_SFCTEMP
+     tristate "Starfive JH7100 temperature sensor"
+     help
+       If you say yes here you get support for temperature sensor
+       on the Starfive JH7100 SoC.
+
+       This driver can also be built as a module.  If so, the module
+       will be called sfctemp.
+
 config SENSORS_SMM665
      tristate "Summit Microelectronics SMM665"
      depends on I2C
drivers/hwmon/Makefile +1 -0
index 59e78bc212cf..3723eb580bf3 100644
@@ -167,6 +167,7 @@ obj-$(CONFIG_SENSORS_SBTSI)       += sbtsi_temp.o
 obj-$(CONFIG_SENSORS_SCH56XX_COMMON)+= sch56xx-common.o
 obj-$(CONFIG_SENSORS_SCH5627)        += sch5627.o
 obj-$(CONFIG_SENSORS_SCH5636)        += sch5636.o
+obj-$(CONFIG_SENSORS_SFCTEMP)        += sfctemp.o
 obj-$(CONFIG_SENSORS_SL28CPLD)       += sl28cpld-hwmon.o
 obj-$(CONFIG_SENSORS_SHT15)  += sht15.o
 obj-$(CONFIG_SENSORS_SHT21)  += sht21.o
drivers/hwmon/sfctemp.c new file +3 -0
new file mode 100644
index 000000000000..62a838063e4e
@@ -0,0 +1,309 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2021 Emil Renner Berthing <[email protected]>

drivers/hwmon/sfctemp.c line 13

Quoted 10 lines
drivers/hwmon/sfctemp.c new file +10 -0
+ * Copyright (C) 2021 Samin Guo <[email protected]>
+ */
+#include <linux/completion.h>
+#include <linux/delay.h>
+#include <linux/hwmon.h>
+#include <linux/interrupt.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>

This may work on some systems, but on others it will result in:

drivers/hwmon/sfctemp.c: In function ‘sfctemp_power_up’:
drivers/hwmon/sfctemp.c:76:2: error: implicit declaration of function ‘writel’

drivers/hwmon/sfctemp.c: In function ‘sfctemp_convert’:
drivers/hwmon/sfctemp.c:144:17: error: implicit declaration of function ‘readl’

It is necessary to include <linux/io.h>.

drivers/hwmon/sfctemp.c line 17

Quoted 4 lines
drivers/hwmon/sfctemp.c new file +4 -0
+
+/* TempSensor reset. The RSTN can be de-asserted once the analog core has
+ * powered up. Trst(min 100ns)
+ * 0:reset  1:de-assert */

/*
* Please use standard multi-line comments.
* checkpatch would tell you, plus it has other complaints.
* Please run checkpatch --strict and fix what it reports. */

Quoted 16 lines
drivers/hwmon/sfctemp.c new file +16 -0
+#define SFCTEMP_RSTN     BIT(0)
+
+/* TempSensor analog core power down. The analog core will be powered up
+ * Tpu(min 50us) after PD is de-asserted. RSTN should be held low until the
+ * analog core is powered up.
+ * 0:power up  1:power down */
+#define SFCTEMP_PD       BIT(1)
+
+/* TempSensor start conversion enable.
+ * 0:disable  1:enable */
+#define SFCTEMP_RUN      BIT(2)
+
+/* TempSensor calibration mode enable.
+ * 0:disable  1:enable */
+#define SFCTEMP_CAL      BIT(4)
+

drivers/hwmon/sfctemp.c line 43

Quoted 10 lines
drivers/hwmon/sfctemp.c new file +10 -0
+/* TempSensor signature enable. Generate a toggle value outputting on DOUT for
+ * test purpose.
+ * 0:disable  1:enable */
+#define SFCTEMP_SGN      BIT(5)
+
+/* TempSensor test access control.
+ * 0000:normal 0001:Test1  0010:Test2  0011:Test3
+ * 0100:Test4  1000:Test8  1001:Test9 */
+#define SFCTEMP_TM_Pos   12
+#define SFCTEMP_TM_Msk   GENMASK(15, 12)

Defines should upper case. Besides, the above defines are not used. Please remove unused defines, and please rename the others to be upper case only.

drivers/hwmon/sfctemp.c line 55

Quoted 12 lines
drivers/hwmon/sfctemp.c new file +12 -0
+
+/* TempSensor conversion value output.
+ * Temp(c)=DOUT*Y/4094 - K */
+#define SFCTEMP_DOUT_Pos 16
+#define SFCTEMP_DOUT_Msk GENMASK(27, 16)
+
+/* TempSensor digital test output. */
+#define SFCTEMP_DIGO     BIT(31)
+
+/* DOUT to Celcius conversion constants */
+#define SFCTEMP_Y1000 237500L
+#define SFCTEMP_Z       4094L

Please use

#define<space><definition><tab><value>, eg

#define SFCTEMP_Y1000 237500L
#define SFCTEMP_Z 4094L

for all defines.

Quoted 63 lines
drivers/hwmon/sfctemp.c new file +63 -0
+#define SFCTEMP_K1000  81100L
+
+struct sfctemp {
+     struct mutex lock;
+     struct completion conversion_done;
+     void __iomem *regs;
+     bool enabled;
+};
+
+static irqreturn_t sfctemp_isr(int irq, void *data)
+{
+     struct sfctemp *sfctemp = data;
+
+     complete(&sfctemp->conversion_done);
+     return IRQ_HANDLED;
+}
+
+static void sfctemp_power_up(struct sfctemp *sfctemp)
+{
+     /* make sure we're powered down first */
+     writel(SFCTEMP_PD, sfctemp->regs);
+     udelay(1);
+
+     writel(0, sfctemp->regs);
+     /* wait t_pu(50us) + t_rst(100ns) */
+     usleep_range(60, 200);
+
+     /* de-assert reset */
+     writel(SFCTEMP_RSTN, sfctemp->regs);
+     udelay(1); /* wait t_su(500ps) */
+}
+
+static void sfctemp_power_down(struct sfctemp *sfctemp)
+{
+     writel(SFCTEMP_PD, sfctemp->regs);
+}
+
+static void sfctemp_run_single(struct sfctemp *sfctemp)
+{
+     writel(SFCTEMP_RSTN | SFCTEMP_RUN, sfctemp->regs);
+     udelay(1);
+     writel(SFCTEMP_RSTN, sfctemp->regs);
+}
+
+static int sfctemp_enable(struct sfctemp *sfctemp)
+{
+     mutex_lock(&sfctemp->lock);
+     if (sfctemp->enabled)
+             goto done;
+
+     sfctemp_power_up(sfctemp);
+     sfctemp->enabled = true;
+done:
+     mutex_unlock(&sfctemp->lock);
+     return 0;
+}
+
+static int sfctemp_disable(struct sfctemp *sfctemp)
+{
+     mutex_lock(&sfctemp->lock);
+     if (!sfctemp->enabled)
+             goto done;
+

drivers/hwmon/sfctemp.c line 128

Quoted 10 lines
drivers/hwmon/sfctemp.c new file +10 -0
+     sfctemp_power_down(sfctemp);
+     sfctemp->enabled = false;
+done:
+     mutex_unlock(&sfctemp->lock);
+     return 0;
+}
+
+static int sfctemp_convert(struct sfctemp *sfctemp, long *val)
+{
+     long ret;

Why long ? Sure, wait_for_completion_interruptible_timeout() returns a long, but it will never return a value large enough to warrant a long here.

Quoted 125 lines
drivers/hwmon/sfctemp.c new file +125 -0
+
+     mutex_lock(&sfctemp->lock);
+     if (!sfctemp->enabled) {
+             ret = -ENODATA;
+             goto out;
+     }
+
+     sfctemp_run_single(sfctemp);
+
+     ret = wait_for_completion_interruptible_timeout(&sfctemp->conversion_done,
+                                                     msecs_to_jiffies(10));
+     if (ret < 0)
+             goto out;
+
+     /* calculate temperature in milli Celcius */
+     *val = (long)((readl(sfctemp->regs) & SFCTEMP_DOUT_Msk) >> SFCTEMP_DOUT_Pos)
+             * SFCTEMP_Y1000 / SFCTEMP_Z - SFCTEMP_K1000;
+
+     ret = 0;
+out:
+     mutex_unlock(&sfctemp->lock);
+     return ret;
+}
+
+static umode_t sfctemp_is_visible(const void *data, enum hwmon_sensor_types type,
+                               u32 attr, int channel)
+{
+     switch (type) {
+     case hwmon_temp:
+             switch (attr) {
+             case hwmon_temp_enable:
+                     return 0644;
+             case hwmon_temp_input:
+                     return 0444;
+             }
+             return 0;
+     default:
+             return 0;
+     }
+}
+
+static int sfctemp_read(struct device *dev, enum hwmon_sensor_types type,
+                     u32 attr, int channel, long *val)
+{
+     struct sfctemp *sfctemp = dev_get_drvdata(dev);
+
+     switch (type) {
+     case hwmon_temp:
+             switch (attr) {
+             case hwmon_temp_enable:
+                     *val = sfctemp->enabled;
+                     return 0;
+             case hwmon_temp_input:
+                     return sfctemp_convert(sfctemp, val);
+             }
+             return -EINVAL;
+     default:
+             return -EINVAL;
+     }
+}
+
+static int sfctemp_write(struct device *dev, enum hwmon_sensor_types type,
+                      u32 attr, int channel, long val)
+{
+     struct sfctemp *sfctemp = dev_get_drvdata(dev);
+
+     switch (type) {
+     case hwmon_temp:
+             switch (attr) {
+             case hwmon_temp_enable:
+                     if (val == 0)
+                             return sfctemp_disable(sfctemp);
+                     if (val == 1)
+                             return sfctemp_enable(sfctemp);
+                     break;
+             }
+             return -EINVAL;
+     default:
+             return -EINVAL;
+     }
+}
+
+static const struct hwmon_channel_info *sfctemp_info[] = {
+     HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ),
+     HWMON_CHANNEL_INFO(temp, HWMON_T_ENABLE | HWMON_T_INPUT),
+     NULL
+};
+
+static const struct hwmon_ops sfctemp_hwmon_ops = {
+     .is_visible = sfctemp_is_visible,
+     .read = sfctemp_read,
+     .write = sfctemp_write,
+};
+
+static const struct hwmon_chip_info sfctemp_chip_info = {
+     .ops = &sfctemp_hwmon_ops,
+     .info = sfctemp_info,
+};
+
+static int sfctemp_probe(struct platform_device *pdev)
+{
+     struct device *dev = &pdev->dev;
+     struct device *hwmon_dev;
+     struct resource *mem;
+     struct sfctemp *sfctemp;
+     long val;
+     int ret;
+
+     sfctemp = devm_kzalloc(dev, sizeof(*sfctemp), GFP_KERNEL);
+     if (!sfctemp)
+             return -ENOMEM;
+
+     dev_set_drvdata(dev, sfctemp);
+     mutex_init(&sfctemp->lock);
+     init_completion(&sfctemp->conversion_done);
+
+     mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+     sfctemp->regs = devm_ioremap_resource(dev, mem);
+     if (IS_ERR(sfctemp->regs))
+             return PTR_ERR(sfctemp->regs);
+
+     ret = platform_get_irq(pdev, 0);
+     if (ret < 0)
+             return ret;
+

drivers/hwmon/sfctemp.c line 263

Quoted 10 lines
drivers/hwmon/sfctemp.c new file +10 -0
+     ret = devm_request_irq(dev, ret, sfctemp_isr,
+                            IRQF_SHARED, pdev->name, sfctemp);
+     if (ret) {
+             dev_err(dev, "request irq failed: %d\n", ret);
+             return ret;
+     }
+
+     ret = sfctemp_enable(sfctemp);
+     if (ret)
+             return ret;

Please consider adding devm_add_action_or_reset() to disable the sensor. Then you can use devm_hwmon_device_register_with_info(), and there is no need for a remove function.

drivers/hwmon/sfctemp.c line 271

Quoted 8 lines
drivers/hwmon/sfctemp.c new file +8 -0
+
+     hwmon_dev = hwmon_device_register_with_info(dev, pdev->name, sfctemp,
+                                                 &sfctemp_chip_info, NULL);
+     if (IS_ERR(hwmon_dev))
+             return PTR_ERR(hwmon_dev);
+
+     /* do a conversion to check everything works */
+     ret = sfctemp_convert(sfctemp, &val);

Please drop this check; it will result in substantial boot delays. The assumption is the the hardware works; the system should not be optimized (slowed down) to handle a potential and hopefully rare failure case.

drivers/hwmon/sfctemp.c line 277

Quoted 6 lines
drivers/hwmon/sfctemp.c new file +6 -0
+     if (ret) {
+             hwmon_device_unregister(hwmon_dev);
+             return ret;
+     }
+
+     dev_info(dev, "%ld.%03ld C\n", val / 1000, val % 1000);

Please drop this message. Anyone interested in boot-time temperature readings can add a script and get it from sysfs attributes as needed.

Quoted 24 lines
drivers/hwmon/sfctemp.c new file +24 -0
+     return 0;
+}
+
+static int sfctemp_remove(struct platform_device *pdev)
+{
+     struct device *dev = &pdev->dev;
+     struct sfctemp *sfctemp = dev_get_drvdata(dev);
+
+     hwmon_device_unregister(dev);
+     return sfctemp_disable(sfctemp);
+}
+
+static const struct of_device_id sfctemp_of_match[] = {
+     { .compatible = "starfive,jh7100-temp" },
+     { /* sentinel */ }
+};
+
+MODULE_DEVICE_TABLE(of, sfctemp_of_match);
+
+static struct platform_driver sfctemp_driver = {
+     .driver = {
+             .name = "sfctemp",
+             .of_match_table = of_match_ptr(sfctemp_of_match),
+     },
Quoted 8 lines

drivers/hwmon/sfctemp.c line 309

Quoted 8 lines
drivers/hwmon/sfctemp.c new file +8 -0
+     .probe  = sfctemp_probe,
+     .remove = sfctemp_remove,
+};
+module_platform_driver(sfctemp_driver);
+
+MODULE_AUTHOR("Emil Renner Berthing");
+MODULE_DESCRIPTION("StarFive JH7100 temperature sensor driver");
+MODULE_LICENSE("GPL");

Hi Guenter,

Thanks for the review. It all sounds reasonable. I'll address it all in v2.

/Emil