[PATCH v1 6/7] hwmon: sl28cpld: add SMARC-sAM67 support

Michael Walle posted 7 patches 1 month, 1 week ago
There is a newer version of this series
[PATCH v1 6/7] hwmon: sl28cpld: add SMARC-sAM67 support
Posted by Michael Walle 1 month, 1 week ago
The on-board uC on the SMARC-sAM67 board is compatible with the older
CPLD implementation on the SMARC-sAL28 board, but has different sensors,
namely two voltage sensors and one temperature sensor. Add support for it.

Signed-off-by: Michael Walle <mwalle@kernel.org>
---
 drivers/hwmon/sl28cpld-hwmon.c | 76 ++++++++++++++++++++++++++++++++--
 1 file changed, 73 insertions(+), 3 deletions(-)

diff --git a/drivers/hwmon/sl28cpld-hwmon.c b/drivers/hwmon/sl28cpld-hwmon.c
index 454cc844fb9d..670308d9b72f 100644
--- a/drivers/hwmon/sl28cpld-hwmon.c
+++ b/drivers/hwmon/sl28cpld-hwmon.c
@@ -18,6 +18,9 @@
 #define   FAN_SCALE_X8		BIT(7)
 #define   FAN_VALUE_MASK	GENMASK(6, 0)
 
+#define SA67MCU_VOLTAGE(n)	(0x00 + ((n) * 2))
+#define SA67MCU_TEMP(n)		(0x04 + ((n) * 2))
+
 struct sl28cpld_hwmon {
 	struct regmap *regmap;
 	u32 offset;
@@ -75,8 +78,71 @@ static const struct hwmon_chip_info sl28cpld_hwmon_chip_info = {
 	.info = sl28cpld_hwmon_info,
 };
 
+static int sa67mcu_hwmon_read(struct device *dev,
+			      enum hwmon_sensor_types type, u32 attr,
+			      int channel, long *input)
+{
+	struct sl28cpld_hwmon *hwmon = dev_get_drvdata(dev);
+	unsigned int offset;
+	u8 reg[2];
+	int ret;
+
+	switch (type) {
+	case hwmon_in:
+		switch (attr) {
+		case hwmon_in_input:
+			offset = hwmon->offset + SA67MCU_VOLTAGE(channel);
+			break;
+		default:
+			return -EOPNOTSUPP;
+		}
+		break;
+	case hwmon_temp:
+		switch (attr) {
+		case hwmon_temp_input:
+			offset = hwmon->offset + SA67MCU_TEMP(channel);
+			break;
+		default:
+			return -EOPNOTSUPP;
+		}
+		break;
+	default:
+		return -EOPNOTSUPP;
+	}
+
+	/* Reading the low byte will capture the value */
+	ret = regmap_bulk_read(hwmon->regmap, offset, reg, ARRAY_SIZE(reg));
+	if (ret)
+		return ret;
+
+	*input = reg[1] << 8 | reg[0];
+
+	/* Temperatures are s16 and in 0.1degC steps. */
+	if (type == hwmon_temp)
+		*input = sign_extend32(*input, 15) * 100;
+
+	return 0;
+}
+
+static const struct hwmon_channel_info * const sa67mcu_hwmon_info[] = {
+	HWMON_CHANNEL_INFO(in, HWMON_I_INPUT, HWMON_I_INPUT),
+	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
+	NULL
+};
+
+static const struct hwmon_ops sa67mcu_hwmon_ops = {
+	.visible = 0444,
+	.read = sa67mcu_hwmon_read,
+};
+
+static const struct hwmon_chip_info sa67mcu_hwmon_chip_info = {
+	.ops = &sa67mcu_hwmon_ops,
+	.info = sa67mcu_hwmon_info,
+};
+
 static int sl28cpld_hwmon_probe(struct platform_device *pdev)
 {
+	const struct hwmon_chip_info *chip_info;
 	struct sl28cpld_hwmon *hwmon;
 	struct device *hwmon_dev;
 	int ret;
@@ -84,6 +150,10 @@ static int sl28cpld_hwmon_probe(struct platform_device *pdev)
 	if (!pdev->dev.parent)
 		return -ENODEV;
 
+	chip_info = device_get_match_data(&pdev->dev);
+	if (!chip_info)
+		return -EINVAL;
+
 	hwmon = devm_kzalloc(&pdev->dev, sizeof(*hwmon), GFP_KERNEL);
 	if (!hwmon)
 		return -ENOMEM;
@@ -97,8 +167,7 @@ static int sl28cpld_hwmon_probe(struct platform_device *pdev)
 		return -EINVAL;
 
 	hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev,
-				"sl28cpld_hwmon", hwmon,
-				&sl28cpld_hwmon_chip_info, NULL);
+				"sl28cpld_hwmon", hwmon, chip_info, NULL);
 	if (IS_ERR(hwmon_dev))
 		dev_err(&pdev->dev, "failed to register as hwmon device");
 
@@ -106,7 +175,8 @@ static int sl28cpld_hwmon_probe(struct platform_device *pdev)
 }
 
 static const struct of_device_id sl28cpld_hwmon_of_match[] = {
-	{ .compatible = "kontron,sl28cpld-fan" },
+	{ .compatible = "kontron,sl28cpld-fan", .data = &sl28cpld_hwmon_chip_info },
+	{ .compatible = "kontron,sa67mcu-hwmon", .data = &sa67mcu_hwmon_chip_info },
 	{}
 };
 MODULE_DEVICE_TABLE(of, sl28cpld_hwmon_of_match);
-- 
2.39.5
Re: [PATCH v1 6/7] hwmon: sl28cpld: add SMARC-sAM67 support
Posted by Guenter Roeck 3 weeks, 5 days ago
On 8/22/25 06:15, Michael Walle wrote:
> The on-board uC on the SMARC-sAM67 board is compatible with the older
> CPLD implementation on the SMARC-sAL28 board, but has different sensors,
> namely two voltage sensors and one temperature sensor. Add support for it.
> 
> Signed-off-by: Michael Walle <mwalle@kernel.org>
> ---
>   drivers/hwmon/sl28cpld-hwmon.c | 76 ++++++++++++++++++++++++++++++++--

Documentation/hwmon/sl28cpld.rst would have to be updated as well.

...

>   static const struct of_device_id sl28cpld_hwmon_of_match[] = {
> -	{ .compatible = "kontron,sl28cpld-fan" },
> +	{ .compatible = "kontron,sl28cpld-fan", .data = &sl28cpld_hwmon_chip_info },
> +	{ .compatible = "kontron,sa67mcu-hwmon", .data = &sa67mcu_hwmon_chip_info },

Effectively this means that the two chips have completely different functionality.
One reports fan speeds, the other reports voltages and current.
This should be a separate driver.

Guenter
Re: [PATCH v1 6/7] hwmon: sl28cpld: add SMARC-sAM67 support
Posted by Michael Walle 3 weeks, 4 days ago
Hi Guenter,

> >   static const struct of_device_id sl28cpld_hwmon_of_match[] = {
> > -	{ .compatible = "kontron,sl28cpld-fan" },
> > +	{ .compatible = "kontron,sl28cpld-fan", .data = &sl28cpld_hwmon_chip_info },
> > +	{ .compatible = "kontron,sa67mcu-hwmon", .data = &sa67mcu_hwmon_chip_info },
>
> Effectively this means that the two chips have completely different functionality.
> One reports fan speeds, the other reports voltages and current.
> This should be a separate driver.

Fair enough. I wasn't sure, the reason why I've chosen to add it to
the sl28cpld driver was that I don't want to clutter the directory
with many small board specific drivers. They all have the simple-mfd
parent driver in common. In the end it's up to you of course, so
separate driver?

-michael
Re: [PATCH v1 6/7] hwmon: sl28cpld: add SMARC-sAM67 support
Posted by Guenter Roeck 3 weeks, 4 days ago
On 9/8/25 00:04, Michael Walle wrote:
> Hi Guenter,
> 
>>>    static const struct of_device_id sl28cpld_hwmon_of_match[] = {
>>> -	{ .compatible = "kontron,sl28cpld-fan" },
>>> +	{ .compatible = "kontron,sl28cpld-fan", .data = &sl28cpld_hwmon_chip_info },
>>> +	{ .compatible = "kontron,sa67mcu-hwmon", .data = &sa67mcu_hwmon_chip_info },
>>
>> Effectively this means that the two chips have completely different functionality.
>> One reports fan speeds, the other reports voltages and current.
>> This should be a separate driver.
> 
> Fair enough. I wasn't sure, the reason why I've chosen to add it to
> the sl28cpld driver was that I don't want to clutter the directory
> with many small board specific drivers. They all have the simple-mfd
> parent driver in common. In the end it's up to you of course, so
> separate driver?
> 

Yes, please. The parent is irrelevant. Functionality isn't.
Thanks,
Guenter