[PATCH 6/6] Input: mc13783-pwrbutton: add OF support

Alexander Kurz posted 6 patches 1 month, 2 weeks ago
[PATCH 6/6] Input: mc13783-pwrbutton: add OF support
Posted by Alexander Kurz 1 month, 2 weeks ago
Add OF support for the mc13783-pwrbutton so that it can be used with
modern DT based systems.

Signed-off-by: Alexander Kurz <akurz@blala.de>
---
 drivers/input/misc/mc13783-pwrbutton.c | 78 +++++++++++++++++++++++++-
 1 file changed, 75 insertions(+), 3 deletions(-)

diff --git a/drivers/input/misc/mc13783-pwrbutton.c b/drivers/input/misc/mc13783-pwrbutton.c
index 49bc5d25f098..11a97ce070a5 100644
--- a/drivers/input/misc/mc13783-pwrbutton.c
+++ b/drivers/input/misc/mc13783-pwrbutton.c
@@ -29,6 +29,7 @@
 #include <linux/mfd/mc13783.h>
 #include <linux/sched.h>
 #include <linux/slab.h>
+#include <linux/of.h>
 
 struct mc13783_pwrb {
 	struct input_dev *pwr;
@@ -105,8 +106,75 @@ static irqreturn_t button3_irq(int irq, void *_priv)
 	return button_irq(MC13783_IRQ_ONOFD3, _priv);
 }
 
+#ifdef CONFIG_OF
+static inline struct mc13xxx_buttons_platform_data __init *mc13xxx_pwrbutton_probe_dt(
+	struct platform_device *pdev)
+{
+	struct mc13xxx_buttons_platform_data *pdata;
+	struct device_node *parent, *child;
+	struct device *dev = &pdev->dev;
+	enum mc13xxx_chip_type chip = platform_get_device_id(pdev)->driver_data;
+	int ret = -ENODATA;
+
+	/* ONOFD3 is only supported for MC13783. */
+	int max_idx = chip != MC13XXX_CHIP_TYPE_MC13783 ? 2 : 1;
+
+	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
+	if (!pdata)
+		return ERR_PTR(-ENOMEM);
+
+	parent = of_get_child_by_name(dev->parent->of_node, "pwrbuttons");
+	if (!parent)
+		goto out_node_put;
+
+	for_each_child_of_node(parent, child) {
+		u32 idx;
+		u8 dbnc = MC13783_BUTTON_DBNC_30MS;
+
+		if (of_property_read_u32(child, "reg", &idx))
+			continue;
+
+		if (idx > max_idx) {
+			dev_warn(dev, "reg out of range\n");
+			continue;
+		}
+
+		of_property_read_u8(child, "debounce-delay-value", &dbnc);
+		if (dbnc > MC13783_BUTTON_DBNC_750MS) {
+			dev_warn(dev, "debounce-delay-value out of range\n");
+			continue;
+		}
+
+		if (of_property_read_u32(child, "linux,code", &pdata->b_on_key[idx]))
+			continue;
+
+		if (of_property_read_bool(child, "active-low"))
+			pdata->b_on_flags[idx] |= MC13783_BUTTON_POL_INVERT;
+
+		if (of_property_read_bool(child, "enable-reset"))
+			pdata->b_on_flags[idx] |= MC13783_BUTTON_RESET_EN;
+
+		pdata->b_on_flags[idx] |= MC13783_BUTTON_ENABLE | dbnc;
+	}
+
+	ret = 0;
+
+out_node_put:
+	of_node_put(parent);
+
+	return ret ? ERR_PTR(ret) : pdata;
+}
+#else
+static inline struct mc13xxx_buttons_platform_data __init *mc13xxx_pwrbutton_probe_dt(
+	struct platform_device *pdev)
+{
+	return ERR_PTR(-ENODEV);
+}
+#endif
+
 static int mc13783_pwrbutton_probe(struct platform_device *pdev)
 {
+	struct device *dev = &pdev->dev;
 	const struct mc13xxx_buttons_platform_data *pdata;
 	struct mc13xxx *mc13783 = dev_get_drvdata(pdev->dev.parent);
 	enum mc13xxx_chip_type chip = platform_get_device_id(pdev)->driver_data;
@@ -116,9 +184,13 @@ static int mc13783_pwrbutton_probe(struct platform_device *pdev)
 	int reg = 0;
 
 	pdata = dev_get_platdata(&pdev->dev);
-	if (!pdata) {
-		dev_err(&pdev->dev, "missing platform data\n");
-		return -ENODEV;
+	if (dev->parent->of_node) {
+		pdata = mc13xxx_pwrbutton_probe_dt(pdev);
+		if (IS_ERR(pdata))
+			return PTR_ERR(pdata);
+	} else if (!pdata) {
+		dev_err(dev, "missing platform data\n");
+		return -ENODATA;
 	}
 
 	pwr = devm_input_allocate_device(&pdev->dev);
-- 
2.39.5
Re: [PATCH 6/6] Input: mc13783-pwrbutton: add OF support
Posted by Dmitry Torokhov 1 month, 2 weeks ago
Hi Alexander,

On Sun, Aug 17, 2025 at 10:27:50AM +0000, Alexander Kurz wrote:
> Add OF support for the mc13783-pwrbutton so that it can be used with
> modern DT based systems.
> 
> Signed-off-by: Alexander Kurz <akurz@blala.de>
> ---
>  drivers/input/misc/mc13783-pwrbutton.c | 78 +++++++++++++++++++++++++-
>  1 file changed, 75 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/input/misc/mc13783-pwrbutton.c b/drivers/input/misc/mc13783-pwrbutton.c
> index 49bc5d25f098..11a97ce070a5 100644
> --- a/drivers/input/misc/mc13783-pwrbutton.c
> +++ b/drivers/input/misc/mc13783-pwrbutton.c
> @@ -29,6 +29,7 @@
>  #include <linux/mfd/mc13783.h>
>  #include <linux/sched.h>
>  #include <linux/slab.h>
> +#include <linux/of.h>

Please use generic device properties API (device_property_read_XX()).

Thanks.

-- 
Dmitry
Re: [PATCH 6/6] Input: mc13783-pwrbutton: add OF support
Posted by kernel test robot 1 month, 2 weeks ago
Hi Alexander,

kernel test robot noticed the following build warnings:

[auto build test WARNING on dtor-input/next]
[also build test WARNING on dtor-input/for-linus lee-mfd/for-mfd-next lee-mfd/for-mfd-fixes robh/for-next linus/master v6.17-rc1 next-20250815]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Alexander-Kurz/Input-mc13783-pwrbutton-fix-irq-mixup/20250817-182649
base:   https://git.kernel.org/pub/scm/linux/kernel/git/dtor/input.git next
patch link:    https://lore.kernel.org/r/20250817102751.29709-7-akurz%40blala.de
patch subject: [PATCH 6/6] Input: mc13783-pwrbutton: add OF support
config: arm-randconfig-001-20250817 (https://download.01.org/0day-ci/archive/20250818/202508180010.aF8jhqrQ-lkp@intel.com/config)
compiler: arm-linux-gnueabi-gcc (GCC) 14.3.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250818/202508180010.aF8jhqrQ-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202508180010.aF8jhqrQ-lkp@intel.com/

All warnings (new ones prefixed by >>, old ones prefixed by <<):

>> WARNING: modpost: vmlinux: section mismatch in reference: mc13783_pwrbutton_probe+0x243 (section: .text.mc13783_pwrbutton_probe) -> mc13xxx_pwrbutton_probe_dt (section: .init.text)

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH 6/6] Input: mc13783-pwrbutton: add OF support
Posted by Krzysztof Kozlowski 1 month, 2 weeks ago
On 17/08/2025 12:27, Alexander Kurz wrote:
> Add OF support for the mc13783-pwrbutton so that it can be used with
> modern DT based systems.
> 
> Signed-off-by: Alexander Kurz <akurz@blala.de>
> ---
>  drivers/input/misc/mc13783-pwrbutton.c | 78 +++++++++++++++++++++++++-
>  1 file changed, 75 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/input/misc/mc13783-pwrbutton.c b/drivers/input/misc/mc13783-pwrbutton.c
> index 49bc5d25f098..11a97ce070a5 100644
> --- a/drivers/input/misc/mc13783-pwrbutton.c
> +++ b/drivers/input/misc/mc13783-pwrbutton.c
> @@ -29,6 +29,7 @@
>  #include <linux/mfd/mc13783.h>
>  #include <linux/sched.h>
>  #include <linux/slab.h>
> +#include <linux/of.h>
>  
>  struct mc13783_pwrb {
>  	struct input_dev *pwr;
> @@ -105,8 +106,75 @@ static irqreturn_t button3_irq(int irq, void *_priv)
>  	return button_irq(MC13783_IRQ_ONOFD3, _priv);
>  }
>  
> +#ifdef CONFIG_OF
> +static inline struct mc13xxx_buttons_platform_data __init *mc13xxx_pwrbutton_probe_dt(
> +	struct platform_device *pdev)
> +{
> +	struct mc13xxx_buttons_platform_data *pdata;
> +	struct device_node *parent, *child;
> +	struct device *dev = &pdev->dev;
> +	enum mc13xxx_chip_type chip = platform_get_device_id(pdev)->driver_data;
> +	int ret = -ENODATA;
> +

No blank lines between declarations.

> +	/* ONOFD3 is only supported for MC13783. */
> +	int max_idx = chip != MC13XXX_CHIP_TYPE_MC13783 ? 2 : 1;

Ternary operator is hardly readable. Just store the number of buttons in
device match data


> +
> +	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
> +	if (!pdata)
> +		return ERR_PTR(-ENOMEM);
> +
> +	parent = of_get_child_by_name(dev->parent->of_node, "pwrbuttons");

Node name: buttons or keys instead

> +	if (!parent)
> +		goto out_node_put;
> +
> +	for_each_child_of_node(parent, child) {
> +		u32 idx;
> +		u8 dbnc = MC13783_BUTTON_DBNC_30MS;
> +
> +		if (of_property_read_u32(child, "reg", &idx))
> +			continue;
> +
> +		if (idx > max_idx) {
> +			dev_warn(dev, "reg out of range\n");
> +			continue;
> +		}
> +
> +		of_property_read_u8(child, "debounce-delay-value", &dbnc);
> +		if (dbnc > MC13783_BUTTON_DBNC_750MS) {
> +			dev_warn(dev, "debounce-delay-value out of range\n");
> +			continue;
> +		}
> +
> +		if (of_property_read_u32(child, "linux,code", &pdata->b_on_key[idx]))
> +			continue;
> +
> +		if (of_property_read_bool(child, "active-low"))
> +			pdata->b_on_flags[idx] |= MC13783_BUTTON_POL_INVERT;
> +
> +		if (of_property_read_bool(child, "enable-reset"))
> +			pdata->b_on_flags[idx] |= MC13783_BUTTON_RESET_EN;
> +
> +		pdata->b_on_flags[idx] |= MC13783_BUTTON_ENABLE | dbnc;
> +	}
> +
> +	ret = 0;
> +
> +out_node_put:
> +	of_node_put(parent);
> +
> +	return ret ? ERR_PTR(ret) : pdata;
> +}
> +#else
> +static inline struct mc13xxx_buttons_platform_data __init *mc13xxx_pwrbutton_probe_dt(


Section mismatch. Build your code with proper DEBUG options for section
mismatch check.

> +	struct platform_device *pdev)
> +{
> +	return ERR_PTR(-ENODEV);
> +}
> +#endif
Best regards,
Krzysztof