[PATCH v5 1/2] pwm: mc33xs2410: add hwmon support

Dimitri Fedrau via B4 Relay posted 2 patches 2 months, 2 weeks ago
[PATCH v5 1/2] pwm: mc33xs2410: add hwmon support
Posted by Dimitri Fedrau via B4 Relay 2 months, 2 weeks ago
From: Dimitri Fedrau <dimitri.fedrau@liebherr.com>

Support for hwmon is provided by a separate driver residing in hwmon
subsystem which is implemented as auxiliary device. Add handling of this
device.

Signed-off-by: Dimitri Fedrau <dimitri.fedrau@liebherr.com>
---
 drivers/pwm/Kconfig          |  1 +
 drivers/pwm/pwm-mc33xs2410.c | 20 ++++++++++++++++++--
 include/linux/mc33xs2410.h   | 16 ++++++++++++++++
 3 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig
index 3ef1757502ebd92b30584cd10611311a0fbfc03b..64f1c86340fdc7d0ef41bf14be5f6f0623a2bd31 100644
--- a/drivers/pwm/Kconfig
+++ b/drivers/pwm/Kconfig
@@ -436,6 +436,7 @@ config PWM_MC33XS2410
 	tristate "MC33XS2410 PWM support"
 	depends on OF
 	depends on SPI
+	select AUXILIARY_BUS
 	help
 	  NXP MC33XS2410 high-side switch driver. The MC33XS2410 is a four
 	  channel high-side switch. The device is operational from 3.0 V
diff --git a/drivers/pwm/pwm-mc33xs2410.c b/drivers/pwm/pwm-mc33xs2410.c
index a1ac3445ccdb4709d92e0075d424a8abc1416eee..6d99e3ff7239891d87711d98aa463ac158af70e1 100644
--- a/drivers/pwm/pwm-mc33xs2410.c
+++ b/drivers/pwm/pwm-mc33xs2410.c
@@ -17,11 +17,14 @@
  *   behavior of the output pin that is neither the old nor the new state,
  *   rather something in between.
  */
+#define DEFAULT_SYMBOL_NAMESPACE		"PWM_MC33XS2410"
 
+#include <linux/auxiliary_bus.h>
 #include <linux/bitfield.h>
 #include <linux/delay.h>
 #include <linux/err.h>
 #include <linux/math64.h>
+#include <linux/mc33xs2410.h>
 #include <linux/minmax.h>
 #include <linux/module.h>
 #include <linux/of.h>
@@ -120,12 +123,19 @@ static int mc33xs2410_read_reg(struct spi_device *spi, u8 reg, u16 *val, u8 flag
 	return mc33xs2410_read_regs(spi, &reg, flag, val, 1);
 }
 
-static int mc33xs2410_read_reg_ctrl(struct spi_device *spi, u8 reg, u16 *val)
+int mc33xs2410_read_reg_ctrl(struct spi_device *spi, u8 reg, u16 *val)
 {
 	return mc33xs2410_read_reg(spi, reg, val, MC33XS2410_FRAME_IN_DATA_RD);
 }
+EXPORT_SYMBOL_GPL(mc33xs2410_read_reg_ctrl);
 
-static int mc33xs2410_modify_reg(struct spi_device *spi, u8 reg, u8 mask, u8 val)
+int mc33xs2410_read_reg_diag(struct spi_device *spi, u8 reg, u16 *val)
+{
+	return mc33xs2410_read_reg(spi, reg, val, 0);
+}
+EXPORT_SYMBOL_GPL(mc33xs2410_read_reg_diag);
+
+int mc33xs2410_modify_reg(struct spi_device *spi, u8 reg, u8 mask, u8 val)
 {
 	u16 tmp;
 	int ret;
@@ -139,6 +149,7 @@ static int mc33xs2410_modify_reg(struct spi_device *spi, u8 reg, u8 mask, u8 val
 
 	return mc33xs2410_write_reg(spi, reg, tmp);
 }
+EXPORT_SYMBOL_GPL(mc33xs2410_modify_reg);
 
 static u8 mc33xs2410_pwm_get_freq(u64 period)
 {
@@ -314,6 +325,7 @@ static int mc33xs2410_reset(struct device *dev)
 static int mc33xs2410_probe(struct spi_device *spi)
 {
 	struct device *dev = &spi->dev;
+	struct auxiliary_device *adev;
 	struct pwm_chip *chip;
 	int ret;
 
@@ -361,6 +373,10 @@ static int mc33xs2410_probe(struct spi_device *spi)
 	if (ret < 0)
 		return dev_err_probe(dev, ret, "Failed to add pwm chip\n");
 
+	adev = devm_auxiliary_device_create(dev, "hwmon", NULL);
+	if (!adev)
+		return dev_err_probe(dev, -ENODEV, "Failed to register hwmon device\n");
+
 	return 0;
 }
 
diff --git a/include/linux/mc33xs2410.h b/include/linux/mc33xs2410.h
new file mode 100644
index 0000000000000000000000000000000000000000..31c0edf10dd7370a0c3e603947256577d4d40854
--- /dev/null
+++ b/include/linux/mc33xs2410.h
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2024 Liebherr-Electronics and Drives GmbH
+ */
+#ifndef _MC33XS2410_H
+#define _MC33XS2410_H
+
+#include <linux/spi/spi.h>
+
+MODULE_IMPORT_NS("PWM_MC33XS2410");
+
+int mc33xs2410_read_reg_ctrl(struct spi_device *spi, u8 reg, u16 *val);
+int mc33xs2410_read_reg_diag(struct spi_device *spi, u8 reg, u16 *val);
+int mc33xs2410_modify_reg(struct spi_device *spi, u8 reg, u8 mask, u8 val);
+
+#endif /* _MC33XS2410_H */

-- 
2.39.5
Re: [PATCH v5 1/2] pwm: mc33xs2410: add hwmon support
Posted by Uwe Kleine-König 2 months, 1 week ago
Hello Dimitri,

On Wed, Jul 23, 2025 at 07:34:56PM +0200, Dimitri Fedrau via B4 Relay wrote:
> @@ -361,6 +373,10 @@ static int mc33xs2410_probe(struct spi_device *spi)
>  	if (ret < 0)
>  		return dev_err_probe(dev, ret, "Failed to add pwm chip\n");
>  
> +	adev = devm_auxiliary_device_create(dev, "hwmon", NULL);
> +	if (!adev)
> +		return dev_err_probe(dev, -ENODEV, "Failed to register hwmon device\n");

Not so nice that you have to make up an error code here. But that's not
your fault but devm_auxiliary_device_create()'s which returns NULL on
error instead of an error pointer.

> +
>  	return 0;
>  }

I applied the patch for the upcoming merge window to

https://git.kernel.org/pub/scm/linux/kernel/git/ukleinek/linux.git pwm/for-next

. Expecting the merge window to open on Sunday that's a bit tight
because I like drivers to be in next for a bit before they enter the
mainline. The major code change is in drivers/hwmon and I assume Guenter
is ok with me taking it for 6.17-rc1. If not, please object, then I'll
not send it to Linus and wait for the 6.18-rc1 PR.

Best regards
Uwe
Re: [PATCH v5 1/2] pwm: mc33xs2410: add hwmon support
Posted by Guenter Roeck 2 months, 1 week ago
On 7/24/25 00:30, Uwe Kleine-König wrote:
> Hello Dimitri,
> 
> On Wed, Jul 23, 2025 at 07:34:56PM +0200, Dimitri Fedrau via B4 Relay wrote:
>> @@ -361,6 +373,10 @@ static int mc33xs2410_probe(struct spi_device *spi)
>>   	if (ret < 0)
>>   		return dev_err_probe(dev, ret, "Failed to add pwm chip\n");
>>   
>> +	adev = devm_auxiliary_device_create(dev, "hwmon", NULL);
>> +	if (!adev)
>> +		return dev_err_probe(dev, -ENODEV, "Failed to register hwmon device\n");
> 
> Not so nice that you have to make up an error code here. But that's not
> your fault but devm_auxiliary_device_create()'s which returns NULL on
> error instead of an error pointer.
> 
>> +
>>   	return 0;
>>   }
> 
> I applied the patch for the upcoming merge window to
> 
> https://git.kernel.org/pub/scm/linux/kernel/git/ukleinek/linux.git pwm/for-next
> 
> . Expecting the merge window to open on Sunday that's a bit tight
> because I like drivers to be in next for a bit before they enter the
> mainline. The major code change is in drivers/hwmon and I assume Guenter
> is ok with me taking it for 6.17-rc1. If not, please object, then I'll
> not send it to Linus and wait for the 6.18-rc1 PR.
> 

Please go ahead and apply both patches.

Thanks,
Guenter