Skip to contentSkip to conversation

leds: max5970: Add support for max5970

PATCH RESEND Superseded Naresh Solanki 2 participants 12 messages Sep 14, 2023 – Nov 23, 2023 LED

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH RESEND v3 1/1 leds: max5970: Add support for max5970

From: Patrick Rudolph <[email protected]>

The MAX5970 is hot swap controller and has 4 indication LED.

---
Changes in V3:
- Drop array for ddata variable.
Changes in V2:
- Add of_node_put before return.
- Code cleanup
- Refactor code & remove max5970_setup_led function.

---
 drivers/leds/Kconfig        |  11 ++++
 drivers/leds/Makefile       |   1 +
 drivers/leds/leds-max5970.c | 110 ++++++++++++++++++++++++++++++++++++
 3 files changed, 122 insertions(+)
 create mode 100644 drivers/leds/leds-max5970.c
drivers/leds/Kconfig +11 -0
index b92208eccdea..03ef527cc545 100644
@@ -637,6 +637,17 @@ config LEDS_ADP5520
 	  To compile this driver as a module, choose M here: the module will
 	  be called leds-adp5520.
 
+config LEDS_MAX5970
+	tristate "LED Support for Maxim 5970"
+	depends on LEDS_CLASS
+	depends on MFD_MAX5970
+	help
+	  This option enables support for the Maxim MAX5970 & MAX5978 smart
+	  switch indication LEDs via the I2C bus.
+
+	  To compile this driver as a module, choose M here: the module will
+	  be called leds-max5970.
+
 config LEDS_MC13783
 	tristate "LED Support for MC13XXX PMIC"
 	depends on LEDS_CLASS
drivers/leds/Makefile +1 -0
index d7348e8bc019..6eaee0a753c6 100644
@@ -56,6 +56,7 @@ obj-$(CONFIG_LEDS_LP8501)		+= leds-lp8501.o
 obj-$(CONFIG_LEDS_LP8788)		+= leds-lp8788.o
 obj-$(CONFIG_LEDS_LP8860)		+= leds-lp8860.o
 obj-$(CONFIG_LEDS_LT3593)		+= leds-lt3593.o
+obj-$(CONFIG_LEDS_MAX5970)		+= leds-max5970.o
 obj-$(CONFIG_LEDS_MAX77650)		+= leds-max77650.o
 obj-$(CONFIG_LEDS_MAX8997)		+= leds-max8997.o
 obj-$(CONFIG_LEDS_MC13783)		+= leds-mc13783.o
drivers/leds/leds-max5970.c new file +110 -0

110 lines not on this page · raw message

base-commit: baca986e1f2c31f8e4b2a6d99d47c3bc844033e8

Signature · 1 line
2.41.0
maintainer Reviewed-by on v3 1/1
#1 lore raw
Quoted 13 lines · Naresh Solanki

From: Patrick Rudolph <[email protected]>

The MAX5970 is hot swap controller and has 4 indication LED.

---
Changes in V3:
- Drop array for ddata variable.
Changes in V2:
- Add of_node_put before return.
- Code cleanup
- Refactor code & remove max5970_setup_led function.

Quoted 6 lines
---
 drivers/leds/Kconfig        |  11 ++++
 drivers/leds/Makefile       |   1 +
 drivers/leds/leds-max5970.c | 110 ++++++++++++++++++++++++++++++++++++
 3 files changed, 122 insertions(+)
 create mode 100644 drivers/leds/leds-max5970.c

Couple of nits and you're good to go.

Once fixed please resubmit with my:

Quoted 77 lines
drivers/leds/Kconfig +11 -0
index b92208eccdea..03ef527cc545 100644
@@ -637,6 +637,17 @@ config LEDS_ADP5520
 	  To compile this driver as a module, choose M here: the module will
 	  be called leds-adp5520.
 
+config LEDS_MAX5970
+	tristate "LED Support for Maxim 5970"
+	depends on LEDS_CLASS
+	depends on MFD_MAX5970
+	help
+	  This option enables support for the Maxim MAX5970 & MAX5978 smart
+	  switch indication LEDs via the I2C bus.
+
+	  To compile this driver as a module, choose M here: the module will
+	  be called leds-max5970.
+
 config LEDS_MC13783
 	tristate "LED Support for MC13XXX PMIC"
 	depends on LEDS_CLASS
drivers/leds/Makefile +1 -0
index d7348e8bc019..6eaee0a753c6 100644
@@ -56,6 +56,7 @@ obj-$(CONFIG_LEDS_LP8501)		+= leds-lp8501.o
 obj-$(CONFIG_LEDS_LP8788)		+= leds-lp8788.o
 obj-$(CONFIG_LEDS_LP8860)		+= leds-lp8860.o
 obj-$(CONFIG_LEDS_LT3593)		+= leds-lt3593.o
+obj-$(CONFIG_LEDS_MAX5970)		+= leds-max5970.o
 obj-$(CONFIG_LEDS_MAX77650)		+= leds-max77650.o
 obj-$(CONFIG_LEDS_MAX8997)		+= leds-max8997.o
 obj-$(CONFIG_LEDS_MC13783)		+= leds-mc13783.o
drivers/leds/leds-max5970.c new file +37 -0
new file mode 100644
index 000000000000..c9685990e26e
@@ -0,0 +1,110 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Device driver for leds in MAX5970 and MAX5978 IC
+ *
+ * Copyright (c) 2022 9elements GmbH
+ *
+ * Author: Patrick Rudolph <[email protected]>
+ */
+
+#include <linux/leds.h>
+#include <linux/mfd/max5970.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+
+#define ldev_to_maxled(c)       container_of(c, struct max5970_led, cdev)
+
+struct max5970_led {
+	struct device *dev;
+	struct regmap *regmap;
+	struct led_classdev cdev;
+	unsigned int index;
+};
+
+static int max5970_led_set_brightness(struct led_classdev *cdev,
+				      enum led_brightness brightness)
+{
+	struct max5970_led *ddata = ldev_to_maxled(cdev);
+	int ret, val;
+
+	/* Set/clear corresponding bit for given led index */
+	val = !brightness ? BIT(ddata->index) : 0;
+
+	ret = regmap_update_bits(ddata->regmap, MAX5970_REG_LED_FLASH, BIT(ddata->index), val);
+	if (ret < 0)
+		dev_err(cdev->dev, "failed to set brightness %d", ret);
+

drivers/leds/leds-max5970.c line 47

Quoted 10 lines
drivers/leds/leds-max5970.c new file +10 -0
+	return ret;
+}
+
+static int max5970_led_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct device_node *np = dev_of_node(dev->parent);
+	struct regmap *regmap;
+	struct device_node *led_node;
+	struct device_node *child;

Nit: You can place these on the same line.

drivers/leds/leds-max5970.c line 53

Quoted 6 lines
drivers/leds/leds-max5970.c new file +6 -0
+	struct max5970_led *ddata;
+	int ret = -ENODEV, num_leds = 0;
+
+	regmap = dev_get_regmap(pdev->dev.parent, NULL);
+	if (!regmap)
+		return -EPROBE_DEFER;

Why are you deferring here?

Quoted 6 lines
drivers/leds/leds-max5970.c new file +6 -0
+	led_node = of_get_child_by_name(np, "leds");
+	if (!led_node)
+		return -ENODEV;
+
+	for_each_available_child_of_node(led_node, child) {
+		u32 reg;

drivers/leds/leds-max5970.c line 70

Quoted 10 lines
drivers/leds/leds-max5970.c new file +10 -0
+
+		if (of_property_read_u32(child, "reg", &reg))
+			continue;
+
+		if (reg >= MAX5970_NUM_LEDS) {
+			dev_err(dev, "invalid LED (%u >= %d)\n", reg, MAX5970_NUM_LEDS);
+			continue;
+		}
+
+		ddata = devm_kzalloc(dev, sizeof(struct max5970_led), GFP_KERNEL);

Nit: sizeof(*ddata)

Quoted 7 lines
drivers/leds/leds-max5970.c new file +7 -0
+		if (!ddata) {
+			of_node_put(child);
+			return -ENOMEM;
+		}
+
+		ddata->index = reg;
+		ddata->regmap = regmap;

drivers/leds/leds-max5970.c line 87

Quoted 10 lines
drivers/leds/leds-max5970.c new file +10 -0
+		ddata->dev = dev;
+
+		if (of_property_read_string(child, "label", &ddata->cdev.name))
+			ddata->cdev.name = child->name;
+
+		ddata->cdev.max_brightness = 1;
+		ddata->cdev.brightness_set_blocking = max5970_led_set_brightness;
+		ddata->cdev.default_trigger = "none";
+
+		ret = devm_led_classdev_register(ddata->dev, &ddata->cdev);

Nit: Use the shorter 'dev' version whilst it's available.

Quoted 28 lines
drivers/leds/leds-max5970.c new file +23 -0
+		if (ret < 0) {
+			of_node_put(child);
+			dev_err(dev, "Failed to initialize LED %u\n", reg);
+			return ret;
+		}
+		num_leds++;
+	}
+
+	return ret;
+}
+
+static struct platform_driver max5970_led_driver = {
+	.driver = {
+		.name = "max5970-led",
+	},
+	.probe = max5970_led_probe,
+};
+
+module_platform_driver(max5970_led_driver);
+MODULE_AUTHOR("Patrick Rudolph <[email protected]>");
+MODULE_AUTHOR("Naresh Solanki <[email protected]>");
+MODULE_DESCRIPTION("MAX5970_hot-swap controller LED driver");
+MODULE_LICENSE("GPL");

base-commit: baca986e1f2c31f8e4b2a6d99d47c3bc844033e8

Signature · 1 line
2.41.0
Signature · 1 line
Lee Jones [李琼斯]
#2 lore raw

Hi

Quoted 110 lines · Lee Jones
Quoted 13 lines · Naresh Solanki

From: Patrick Rudolph <[email protected]>

The MAX5970 is hot swap controller and has 4 indication LED.

---
Changes in V3:
- Drop array for ddata variable.
Changes in V2:
- Add of_node_put before return.
- Code cleanup
- Refactor code & remove max5970_setup_led function.

Quoted 6 lines
---
 drivers/leds/Kconfig        |  11 ++++
 drivers/leds/Makefile       |   1 +
 drivers/leds/leds-max5970.c | 110 ++++++++++++++++++++++++++++++++++++
 3 files changed, 122 insertions(+)
 create mode 100644 drivers/leds/leds-max5970.c

Couple of nits and you're good to go.

Once fixed please resubmit with my:

Quoted 81 lines
drivers/leds/Kconfig +11 -0
index b92208eccdea..03ef527cc545 100644
@@ -637,6 +637,17 @@ config LEDS_ADP5520
        To compile this driver as a module, choose M here: the module will
        be called leds-adp5520.
 
+config LEDS_MAX5970
+     tristate "LED Support for Maxim 5970"
+     depends on LEDS_CLASS
+     depends on MFD_MAX5970
+     help
+       This option enables support for the Maxim MAX5970 & MAX5978 smart
+       switch indication LEDs via the I2C bus.
+
+       To compile this driver as a module, choose M here: the module will
+       be called leds-max5970.
+
 config LEDS_MC13783
      tristate "LED Support for MC13XXX PMIC"
      depends on LEDS_CLASS
drivers/leds/Makefile +1 -0
index d7348e8bc019..6eaee0a753c6 100644
@@ -56,6 +56,7 @@ obj-$(CONFIG_LEDS_LP8501)           += leds-lp8501.o
 obj-$(CONFIG_LEDS_LP8788)            += leds-lp8788.o
 obj-$(CONFIG_LEDS_LP8860)            += leds-lp8860.o
 obj-$(CONFIG_LEDS_LT3593)            += leds-lt3593.o
+obj-$(CONFIG_LEDS_MAX5970)           += leds-max5970.o
 obj-$(CONFIG_LEDS_MAX77650)          += leds-max77650.o
 obj-$(CONFIG_LEDS_MAX8997)           += leds-max8997.o
 obj-$(CONFIG_LEDS_MC13783)           += leds-mc13783.o
drivers/leds/leds-max5970.c new file +41 -0
new file mode 100644
index 000000000000..c9685990e26e
@@ -0,0 +1,110 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Device driver for leds in MAX5970 and MAX5978 IC
+ *
+ * Copyright (c) 2022 9elements GmbH
+ *
+ * Author: Patrick Rudolph <[email protected]>
+ */
+
+#include <linux/leds.h>
+#include <linux/mfd/max5970.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+
+#define ldev_to_maxled(c)       container_of(c, struct max5970_led, cdev)
+
+struct max5970_led {
+     struct device *dev;
+     struct regmap *regmap;
+     struct led_classdev cdev;
+     unsigned int index;
+};
+
+static int max5970_led_set_brightness(struct led_classdev *cdev,
+                                   enum led_brightness brightness)
+{
+     struct max5970_led *ddata = ldev_to_maxled(cdev);
+     int ret, val;
+
+     /* Set/clear corresponding bit for given led index */
+     val = !brightness ? BIT(ddata->index) : 0;
+
+     ret = regmap_update_bits(ddata->regmap, MAX5970_REG_LED_FLASH, BIT(ddata->index), val);
+     if (ret < 0)
+             dev_err(cdev->dev, "failed to set brightness %d", ret);
+
+     return ret;
+}
+
+static int max5970_led_probe(struct platform_device *pdev)
Quoted 8 lines

drivers/leds/leds-max5970.c line 47

Quoted 6 lines
drivers/leds/leds-max5970.c new file +6 -0
+{
+     struct device *dev = &pdev->dev;
+     struct device_node *np = dev_of_node(dev->parent);
+     struct regmap *regmap;
+     struct device_node *led_node;
+     struct device_node *child;

Nit: You can place these on the same line.

Ack

Quoted 9 lines

drivers/leds/leds-max5970.c line 53

Quoted 7 lines
drivers/leds/leds-max5970.c new file +6 -0
+     struct max5970_led *ddata;
+     int ret = -ENODEV, num_leds = 0;
+
+     regmap = dev_get_regmap(pdev->dev.parent, NULL);
+     if (!regmap)
+             return -EPROBE_DEFER;

Why are you deferring here?

This is a Leaf driver. Making sure the parent driver has initialized regmap.

Quoted 11 lines
Quoted 11 lines
drivers/leds/leds-max5970.c new file +10 -0
+     led_node = of_get_child_by_name(np, "leds");
+     if (!led_node)
+             return -ENODEV;
+
+     for_each_available_child_of_node(led_node, child) {
+             u32 reg;
+
+             if (of_property_read_u32(child, "reg", &reg))
+                     continue;
+
Quoted 8 lines

drivers/leds/leds-max5970.c line 70

Quoted 6 lines
drivers/leds/leds-max5970.c new file +6 -0
+             if (reg >= MAX5970_NUM_LEDS) {
+                     dev_err(dev, "invalid LED (%u >= %d)\n", reg, MAX5970_NUM_LEDS);
+                     continue;
+             }
+
+             ddata = devm_kzalloc(dev, sizeof(struct max5970_led), GFP_KERNEL);

Nit: sizeof(*ddata)

Ack

Quoted 12 lines
Quoted 12 lines
drivers/leds/leds-max5970.c new file +11 -0
+             if (!ddata) {
+                     of_node_put(child);
+                     return -ENOMEM;
+             }
+
+             ddata->index = reg;
+             ddata->regmap = regmap;
+             ddata->dev = dev;
+
+             if (of_property_read_string(child, "label", &ddata->cdev.name))
+                     ddata->cdev.name = child->name;
Quoted 8 lines

drivers/leds/leds-max5970.c line 87

Quoted 6 lines
drivers/leds/leds-max5970.c new file +6 -0
+
+             ddata->cdev.max_brightness = 1;
+             ddata->cdev.brightness_set_blocking = max5970_led_set_brightness;
+             ddata->cdev.default_trigger = "none";
+
+             ret = devm_led_classdev_register(ddata->dev, &ddata->cdev);

Nit: Use the shorter 'dev' version whilst it's available.

Ack

Quoted 32 lines
Quoted 25 lines
drivers/leds/leds-max5970.c new file +23 -0
+             if (ret < 0) {
+                     of_node_put(child);
+                     dev_err(dev, "Failed to initialize LED %u\n", reg);
+                     return ret;
+             }
+             num_leds++;
+     }
+
+     return ret;
+}
+
+static struct platform_driver max5970_led_driver = {
+     .driver = {
+             .name = "max5970-led",
+     },
+     .probe = max5970_led_probe,
+};
+
+module_platform_driver(max5970_led_driver);
+MODULE_AUTHOR("Patrick Rudolph <[email protected]>");
+MODULE_AUTHOR("Naresh Solanki <[email protected]>");
+MODULE_DESCRIPTION("MAX5970_hot-swap controller LED driver");
+MODULE_LICENSE("GPL");
Quoted 4 lines

base-commit: baca986e1f2c31f8e4b2a6d99d47c3bc844033e8 --
2.41.0

--
Lee Jones [李琼斯]

maintainer
#3 lore raw
Quoted 131 lines · Naresh Solanki

Hi

Quoted 110 lines · Lee Jones
Quoted 13 lines · Naresh Solanki

From: Patrick Rudolph <[email protected]>

The MAX5970 is hot swap controller and has 4 indication LED.

---
Changes in V3:
- Drop array for ddata variable.
Changes in V2:
- Add of_node_put before return.
- Code cleanup
- Refactor code & remove max5970_setup_led function.

Quoted 6 lines
---
 drivers/leds/Kconfig        |  11 ++++
 drivers/leds/Makefile       |   1 +
 drivers/leds/leds-max5970.c | 110 ++++++++++++++++++++++++++++++++++++
 3 files changed, 122 insertions(+)
 create mode 100644 drivers/leds/leds-max5970.c

Couple of nits and you're good to go.

Once fixed please resubmit with my:

Quoted 81 lines
drivers/leds/Kconfig +11 -0
index b92208eccdea..03ef527cc545 100644
@@ -637,6 +637,17 @@ config LEDS_ADP5520
        To compile this driver as a module, choose M here: the module will
        be called leds-adp5520.
 
+config LEDS_MAX5970
+     tristate "LED Support for Maxim 5970"
+     depends on LEDS_CLASS
+     depends on MFD_MAX5970
+     help
+       This option enables support for the Maxim MAX5970 & MAX5978 smart
+       switch indication LEDs via the I2C bus.
+
+       To compile this driver as a module, choose M here: the module will
+       be called leds-max5970.
+
 config LEDS_MC13783
      tristate "LED Support for MC13XXX PMIC"
      depends on LEDS_CLASS
drivers/leds/Makefile +1 -0
index d7348e8bc019..6eaee0a753c6 100644
@@ -56,6 +56,7 @@ obj-$(CONFIG_LEDS_LP8501)           += leds-lp8501.o
 obj-$(CONFIG_LEDS_LP8788)            += leds-lp8788.o
 obj-$(CONFIG_LEDS_LP8860)            += leds-lp8860.o
 obj-$(CONFIG_LEDS_LT3593)            += leds-lt3593.o
+obj-$(CONFIG_LEDS_MAX5970)           += leds-max5970.o
 obj-$(CONFIG_LEDS_MAX77650)          += leds-max77650.o
 obj-$(CONFIG_LEDS_MAX8997)           += leds-max8997.o
 obj-$(CONFIG_LEDS_MC13783)           += leds-mc13783.o
drivers/leds/leds-max5970.c new file +41 -0
new file mode 100644
index 000000000000..c9685990e26e
@@ -0,0 +1,110 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Device driver for leds in MAX5970 and MAX5978 IC
+ *
+ * Copyright (c) 2022 9elements GmbH
+ *
+ * Author: Patrick Rudolph <[email protected]>
+ */
+
+#include <linux/leds.h>
+#include <linux/mfd/max5970.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+
+#define ldev_to_maxled(c)       container_of(c, struct max5970_led, cdev)
+
+struct max5970_led {
+     struct device *dev;
+     struct regmap *regmap;
+     struct led_classdev cdev;
+     unsigned int index;
+};
+
+static int max5970_led_set_brightness(struct led_classdev *cdev,
+                                   enum led_brightness brightness)
+{
+     struct max5970_led *ddata = ldev_to_maxled(cdev);
+     int ret, val;
+
+     /* Set/clear corresponding bit for given led index */
+     val = !brightness ? BIT(ddata->index) : 0;
+
+     ret = regmap_update_bits(ddata->regmap, MAX5970_REG_LED_FLASH, BIT(ddata->index), val);
+     if (ret < 0)
+             dev_err(cdev->dev, "failed to set brightness %d", ret);
+
+     return ret;
+}
+
+static int max5970_led_probe(struct platform_device *pdev)
Quoted 8 lines

drivers/leds/leds-max5970.c line 47

Quoted 6 lines
drivers/leds/leds-max5970.c new file +6 -0
+{
+     struct device *dev = &pdev->dev;
+     struct device_node *np = dev_of_node(dev->parent);
+     struct regmap *regmap;
+     struct device_node *led_node;
+     struct device_node *child;

Nit: You can place these on the same line.

Ack

Quoted 8 lines
Quoted 7 lines
drivers/leds/leds-max5970.c new file +6 -0
+     struct max5970_led *ddata;
+     int ret = -ENODEV, num_leds = 0;
+
+     regmap = dev_get_regmap(pdev->dev.parent, NULL);
+     if (!regmap)
+             return -EPROBE_DEFER;
Quoted 2 lines
Quoted 1 line

Why are you deferring here?

This is a Leaf driver. Making sure the parent driver has initialized regmap.

How can this driver initialise before the parent driver?

Signature · 1 line
Lee Jones [李琼斯]
#4 lore raw

Hi,

Quoted 134 lines · Lee Jones
Quoted 131 lines · Naresh Solanki

Hi

Quoted 110 lines · Lee Jones
Quoted 13 lines · Naresh Solanki

From: Patrick Rudolph <[email protected]>

The MAX5970 is hot swap controller and has 4 indication LED.

---
Changes in V3:
- Drop array for ddata variable.
Changes in V2:
- Add of_node_put before return.
- Code cleanup
- Refactor code & remove max5970_setup_led function.

Quoted 6 lines
---
 drivers/leds/Kconfig        |  11 ++++
 drivers/leds/Makefile       |   1 +
 drivers/leds/leds-max5970.c | 110 ++++++++++++++++++++++++++++++++++++
 3 files changed, 122 insertions(+)
 create mode 100644 drivers/leds/leds-max5970.c

Couple of nits and you're good to go.

Once fixed please resubmit with my:

Quoted 81 lines
drivers/leds/Kconfig +11 -0
index b92208eccdea..03ef527cc545 100644
@@ -637,6 +637,17 @@ config LEDS_ADP5520
        To compile this driver as a module, choose M here: the module will
        be called leds-adp5520.
 
+config LEDS_MAX5970
+     tristate "LED Support for Maxim 5970"
+     depends on LEDS_CLASS
+     depends on MFD_MAX5970
+     help
+       This option enables support for the Maxim MAX5970 & MAX5978 smart
+       switch indication LEDs via the I2C bus.
+
+       To compile this driver as a module, choose M here: the module will
+       be called leds-max5970.
+
 config LEDS_MC13783
      tristate "LED Support for MC13XXX PMIC"
      depends on LEDS_CLASS
drivers/leds/Makefile +1 -0
index d7348e8bc019..6eaee0a753c6 100644
@@ -56,6 +56,7 @@ obj-$(CONFIG_LEDS_LP8501)           += leds-lp8501.o
 obj-$(CONFIG_LEDS_LP8788)            += leds-lp8788.o
 obj-$(CONFIG_LEDS_LP8860)            += leds-lp8860.o
 obj-$(CONFIG_LEDS_LT3593)            += leds-lt3593.o
+obj-$(CONFIG_LEDS_MAX5970)           += leds-max5970.o
 obj-$(CONFIG_LEDS_MAX77650)          += leds-max77650.o
 obj-$(CONFIG_LEDS_MAX8997)           += leds-max8997.o
 obj-$(CONFIG_LEDS_MC13783)           += leds-mc13783.o
drivers/leds/leds-max5970.c new file +41 -0
new file mode 100644
index 000000000000..c9685990e26e
@@ -0,0 +1,110 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Device driver for leds in MAX5970 and MAX5978 IC
+ *
+ * Copyright (c) 2022 9elements GmbH
+ *
+ * Author: Patrick Rudolph <[email protected]>
+ */
+
+#include <linux/leds.h>
+#include <linux/mfd/max5970.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+
+#define ldev_to_maxled(c)       container_of(c, struct max5970_led, cdev)
+
+struct max5970_led {
+     struct device *dev;
+     struct regmap *regmap;
+     struct led_classdev cdev;
+     unsigned int index;
+};
+
+static int max5970_led_set_brightness(struct led_classdev *cdev,
+                                   enum led_brightness brightness)
+{
+     struct max5970_led *ddata = ldev_to_maxled(cdev);
+     int ret, val;
+
+     /* Set/clear corresponding bit for given led index */
+     val = !brightness ? BIT(ddata->index) : 0;
+
+     ret = regmap_update_bits(ddata->regmap, MAX5970_REG_LED_FLASH, BIT(ddata->index), val);
+     if (ret < 0)
+             dev_err(cdev->dev, "failed to set brightness %d", ret);
+
+     return ret;
+}
+
+static int max5970_led_probe(struct platform_device *pdev)
Quoted 8 lines

drivers/leds/leds-max5970.c line 47

Quoted 6 lines
drivers/leds/leds-max5970.c new file +6 -0
+{
+     struct device *dev = &pdev->dev;
+     struct device_node *np = dev_of_node(dev->parent);
+     struct regmap *regmap;
+     struct device_node *led_node;
+     struct device_node *child;

Nit: You can place these on the same line.

Ack

Quoted 8 lines
Quoted 7 lines
drivers/leds/leds-max5970.c new file +6 -0
+     struct max5970_led *ddata;
+     int ret = -ENODEV, num_leds = 0;
+
+     regmap = dev_get_regmap(pdev->dev.parent, NULL);
+     if (!regmap)
+             return -EPROBE_DEFER;
Quoted 4 lines
Quoted 2 lines
Quoted 1 line

Why are you deferring here?

This is a Leaf driver. Making sure the parent driver has initialized regmap.

How can this driver initialise before the parent driver?

The parent driver in this case is simple_i2c_mfd.
Based on reference from other similar implementations, the regmap check was adapted.
As you mentioned, your right that leaf driver will not start before parent driver is loaded successfully so probably the DEFER might not be needed here.

Thanks,
Naresh

Quoted 3 lines

--
Lee Jones [李琼斯]

#5 lore raw

Hey Lee,

Is there anything specific you'd suggest changing in the current patchset, or are we good to proceed?

Thanks,
Naresh Solanki

Quoted 154 lines · Naresh Solanki

Hi,

Quoted 134 lines · Lee Jones
Quoted 131 lines · Naresh Solanki

Hi

Quoted 110 lines · Lee Jones
Quoted 13 lines · Naresh Solanki

From: Patrick Rudolph <[email protected]>

The MAX5970 is hot swap controller and has 4 indication LED.

---
Changes in V3:
- Drop array for ddata variable.
Changes in V2:
- Add of_node_put before return.
- Code cleanup
- Refactor code & remove max5970_setup_led function.

Quoted 6 lines
---
 drivers/leds/Kconfig        |  11 ++++
 drivers/leds/Makefile       |   1 +
 drivers/leds/leds-max5970.c | 110 ++++++++++++++++++++++++++++++++++++
 3 files changed, 122 insertions(+)
 create mode 100644 drivers/leds/leds-max5970.c

Couple of nits and you're good to go.

Once fixed please resubmit with my:

Quoted 81 lines
drivers/leds/Kconfig +11 -0
index b92208eccdea..03ef527cc545 100644
@@ -637,6 +637,17 @@ config LEDS_ADP5520
        To compile this driver as a module, choose M here: the module will
        be called leds-adp5520.
 
+config LEDS_MAX5970
+     tristate "LED Support for Maxim 5970"
+     depends on LEDS_CLASS
+     depends on MFD_MAX5970
+     help
+       This option enables support for the Maxim MAX5970 & MAX5978 smart
+       switch indication LEDs via the I2C bus.
+
+       To compile this driver as a module, choose M here: the module will
+       be called leds-max5970.
+
 config LEDS_MC13783
      tristate "LED Support for MC13XXX PMIC"
      depends on LEDS_CLASS
drivers/leds/Makefile +1 -0
index d7348e8bc019..6eaee0a753c6 100644
@@ -56,6 +56,7 @@ obj-$(CONFIG_LEDS_LP8501)           += leds-lp8501.o
 obj-$(CONFIG_LEDS_LP8788)            += leds-lp8788.o
 obj-$(CONFIG_LEDS_LP8860)            += leds-lp8860.o
 obj-$(CONFIG_LEDS_LT3593)            += leds-lt3593.o
+obj-$(CONFIG_LEDS_MAX5970)           += leds-max5970.o
 obj-$(CONFIG_LEDS_MAX77650)          += leds-max77650.o
 obj-$(CONFIG_LEDS_MAX8997)           += leds-max8997.o
 obj-$(CONFIG_LEDS_MC13783)           += leds-mc13783.o
drivers/leds/leds-max5970.c new file +41 -0
new file mode 100644
index 000000000000..c9685990e26e
@@ -0,0 +1,110 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Device driver for leds in MAX5970 and MAX5978 IC
+ *
+ * Copyright (c) 2022 9elements GmbH
+ *
+ * Author: Patrick Rudolph <[email protected]>
+ */
+
+#include <linux/leds.h>
+#include <linux/mfd/max5970.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+
+#define ldev_to_maxled(c)       container_of(c, struct max5970_led, cdev)
+
+struct max5970_led {
+     struct device *dev;
+     struct regmap *regmap;
+     struct led_classdev cdev;
+     unsigned int index;
+};
+
+static int max5970_led_set_brightness(struct led_classdev *cdev,
+                                   enum led_brightness brightness)
+{
+     struct max5970_led *ddata = ldev_to_maxled(cdev);
+     int ret, val;
+
+     /* Set/clear corresponding bit for given led index */
+     val = !brightness ? BIT(ddata->index) : 0;
+
+     ret = regmap_update_bits(ddata->regmap, MAX5970_REG_LED_FLASH, BIT(ddata->index), val);
+     if (ret < 0)
+             dev_err(cdev->dev, "failed to set brightness %d", ret);
+
+     return ret;
+}
+
+static int max5970_led_probe(struct platform_device *pdev)
Quoted 8 lines

drivers/leds/leds-max5970.c line 47

Quoted 6 lines
drivers/leds/leds-max5970.c new file +6 -0
+{
+     struct device *dev = &pdev->dev;
+     struct device_node *np = dev_of_node(dev->parent);
+     struct regmap *regmap;
+     struct device_node *led_node;
+     struct device_node *child;

Nit: You can place these on the same line.

Ack

Quoted 8 lines
Quoted 7 lines
drivers/leds/leds-max5970.c new file +6 -0
+     struct max5970_led *ddata;
+     int ret = -ENODEV, num_leds = 0;
+
+     regmap = dev_get_regmap(pdev->dev.parent, NULL);
+     if (!regmap)
+             return -EPROBE_DEFER;
Quoted 4 lines
Quoted 2 lines
Quoted 1 line

Why are you deferring here?

This is a Leaf driver. Making sure the parent driver has initialized regmap.

How can this driver initialise before the parent driver?

The parent driver in this case is simple_i2c_mfd.
Based on reference from other similar implementations, the regmap check was adapted.
As you mentioned, your right that leaf driver will not start before parent driver is loaded successfully so probably the DEFER might not be needed here.

Thanks,
Naresh

Quoted 3 lines

--
Lee Jones [李琼斯]

maintainer
#6 lore raw
Quoted 4 lines · Naresh Solanki

Hey Lee,

Is there anything specific you'd suggest changing in the current patchset, or are we good to proceed?

What do you mean by proceed?

You are good to make changes and submit a subsequent version.

Not entirely sure what you're asking.

Quoted 156 lines
Quoted 154 lines · Naresh Solanki

Hi,

Quoted 134 lines · Lee Jones
Quoted 131 lines · Naresh Solanki

Hi

Quoted 110 lines · Lee Jones
Quoted 13 lines · Naresh Solanki

From: Patrick Rudolph <[email protected]>

The MAX5970 is hot swap controller and has 4 indication LED.

---
Changes in V3:
- Drop array for ddata variable.
Changes in V2:
- Add of_node_put before return.
- Code cleanup
- Refactor code & remove max5970_setup_led function.

Quoted 6 lines
---
 drivers/leds/Kconfig        |  11 ++++
 drivers/leds/Makefile       |   1 +
 drivers/leds/leds-max5970.c | 110 ++++++++++++++++++++++++++++++++++++
 3 files changed, 122 insertions(+)
 create mode 100644 drivers/leds/leds-max5970.c

Couple of nits and you're good to go.

Once fixed please resubmit with my:

Quoted 81 lines
drivers/leds/Kconfig +11 -0
index b92208eccdea..03ef527cc545 100644
@@ -637,6 +637,17 @@ config LEDS_ADP5520
        To compile this driver as a module, choose M here: the module will
        be called leds-adp5520.
 
+config LEDS_MAX5970
+     tristate "LED Support for Maxim 5970"
+     depends on LEDS_CLASS
+     depends on MFD_MAX5970
+     help
+       This option enables support for the Maxim MAX5970 & MAX5978 smart
+       switch indication LEDs via the I2C bus.
+
+       To compile this driver as a module, choose M here: the module will
+       be called leds-max5970.
+
 config LEDS_MC13783
      tristate "LED Support for MC13XXX PMIC"
      depends on LEDS_CLASS
drivers/leds/Makefile +1 -0
index d7348e8bc019..6eaee0a753c6 100644
@@ -56,6 +56,7 @@ obj-$(CONFIG_LEDS_LP8501)           += leds-lp8501.o
 obj-$(CONFIG_LEDS_LP8788)            += leds-lp8788.o
 obj-$(CONFIG_LEDS_LP8860)            += leds-lp8860.o
 obj-$(CONFIG_LEDS_LT3593)            += leds-lt3593.o
+obj-$(CONFIG_LEDS_MAX5970)           += leds-max5970.o
 obj-$(CONFIG_LEDS_MAX77650)          += leds-max77650.o
 obj-$(CONFIG_LEDS_MAX8997)           += leds-max8997.o
 obj-$(CONFIG_LEDS_MC13783)           += leds-mc13783.o
drivers/leds/leds-max5970.c new file +41 -0
new file mode 100644
index 000000000000..c9685990e26e
@@ -0,0 +1,110 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Device driver for leds in MAX5970 and MAX5978 IC
+ *
+ * Copyright (c) 2022 9elements GmbH
+ *
+ * Author: Patrick Rudolph <[email protected]>
+ */
+
+#include <linux/leds.h>
+#include <linux/mfd/max5970.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+
+#define ldev_to_maxled(c)       container_of(c, struct max5970_led, cdev)
+
+struct max5970_led {
+     struct device *dev;
+     struct regmap *regmap;
+     struct led_classdev cdev;
+     unsigned int index;
+};
+
+static int max5970_led_set_brightness(struct led_classdev *cdev,
+                                   enum led_brightness brightness)
+{
+     struct max5970_led *ddata = ldev_to_maxled(cdev);
+     int ret, val;
+
+     /* Set/clear corresponding bit for given led index */
+     val = !brightness ? BIT(ddata->index) : 0;
+
+     ret = regmap_update_bits(ddata->regmap, MAX5970_REG_LED_FLASH, BIT(ddata->index), val);
+     if (ret < 0)
+             dev_err(cdev->dev, "failed to set brightness %d", ret);
+
+     return ret;
+}
+
+static int max5970_led_probe(struct platform_device *pdev)
Quoted 8 lines

drivers/leds/leds-max5970.c line 47

Quoted 6 lines
drivers/leds/leds-max5970.c new file +6 -0
+{
+     struct device *dev = &pdev->dev;
+     struct device_node *np = dev_of_node(dev->parent);
+     struct regmap *regmap;
+     struct device_node *led_node;
+     struct device_node *child;

Nit: You can place these on the same line.

Ack

Quoted 8 lines
Quoted 7 lines
drivers/leds/leds-max5970.c new file +6 -0
+     struct max5970_led *ddata;
+     int ret = -ENODEV, num_leds = 0;
+
+     regmap = dev_get_regmap(pdev->dev.parent, NULL);
+     if (!regmap)
+             return -EPROBE_DEFER;
Quoted 4 lines
Quoted 2 lines
Quoted 1 line

Why are you deferring here?

This is a Leaf driver. Making sure the parent driver has initialized regmap.

How can this driver initialise before the parent driver?

The parent driver in this case is simple_i2c_mfd.
Based on reference from other similar implementations, the regmap check was adapted.
As you mentioned, your right that leaf driver will not start before parent driver is loaded successfully so probably the DEFER might not be needed here.

Thanks,
Naresh

Quoted 3 lines

--
Lee Jones [李琼斯]

Signature · 1 line
Lee Jones [李琼斯]
#7 lore raw

Hi

Quoted 5 lines · Lee Jones
Quoted 2 lines · Naresh Solanki

Hey Lee,

Quoted 8 lines
Quoted 2 lines

Is there anything specific you'd suggest changing in the current patchset, or are we good to proceed?

What do you mean by proceed?

You are good to make changes and submit a subsequent version.

Not entirely sure what you're asking.

As a follow up on previous discussion regarding use of DEFER on probe if regmap isn't initialized, the implementation was based on other similar drivers & hence it was retained although its not needed due to dependencies.

I'm not entirely sure to keep the regmap check or make another patch revision with regmap check removed ?

Regards,
Naresh

Quoted 160 lines
Quoted 152 lines
Quoted 149 lines · Naresh Solanki

Hi,

Quoted 134 lines · Lee Jones
Quoted 131 lines · Naresh Solanki

Hi

Quoted 110 lines · Lee Jones
Quoted 13 lines · Naresh Solanki

From: Patrick Rudolph <[email protected]>

The MAX5970 is hot swap controller and has 4 indication LED.

---
Changes in V3:
- Drop array for ddata variable.
Changes in V2:
- Add of_node_put before return.
- Code cleanup
- Refactor code & remove max5970_setup_led function.

Quoted 6 lines
---
 drivers/leds/Kconfig        |  11 ++++
 drivers/leds/Makefile       |   1 +
 drivers/leds/leds-max5970.c | 110 ++++++++++++++++++++++++++++++++++++
 3 files changed, 122 insertions(+)
 create mode 100644 drivers/leds/leds-max5970.c

Couple of nits and you're good to go.

Once fixed please resubmit with my:

Quoted 81 lines
drivers/leds/Kconfig +11 -0
index b92208eccdea..03ef527cc545 100644
@@ -637,6 +637,17 @@ config LEDS_ADP5520
        To compile this driver as a module, choose M here: the module will
        be called leds-adp5520.
 
+config LEDS_MAX5970
+     tristate "LED Support for Maxim 5970"
+     depends on LEDS_CLASS
+     depends on MFD_MAX5970
+     help
+       This option enables support for the Maxim MAX5970 & MAX5978 smart
+       switch indication LEDs via the I2C bus.
+
+       To compile this driver as a module, choose M here: the module will
+       be called leds-max5970.
+
 config LEDS_MC13783
      tristate "LED Support for MC13XXX PMIC"
      depends on LEDS_CLASS
drivers/leds/Makefile +1 -0
index d7348e8bc019..6eaee0a753c6 100644
@@ -56,6 +56,7 @@ obj-$(CONFIG_LEDS_LP8501)           += leds-lp8501.o
 obj-$(CONFIG_LEDS_LP8788)            += leds-lp8788.o
 obj-$(CONFIG_LEDS_LP8860)            += leds-lp8860.o
 obj-$(CONFIG_LEDS_LT3593)            += leds-lt3593.o
+obj-$(CONFIG_LEDS_MAX5970)           += leds-max5970.o
 obj-$(CONFIG_LEDS_MAX77650)          += leds-max77650.o
 obj-$(CONFIG_LEDS_MAX8997)           += leds-max8997.o
 obj-$(CONFIG_LEDS_MC13783)           += leds-mc13783.o
drivers/leds/leds-max5970.c new file +41 -0
new file mode 100644
index 000000000000..c9685990e26e
@@ -0,0 +1,110 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Device driver for leds in MAX5970 and MAX5978 IC
+ *
+ * Copyright (c) 2022 9elements GmbH
+ *
+ * Author: Patrick Rudolph <[email protected]>
+ */
+
+#include <linux/leds.h>
+#include <linux/mfd/max5970.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+
+#define ldev_to_maxled(c)       container_of(c, struct max5970_led, cdev)
+
+struct max5970_led {
+     struct device *dev;
+     struct regmap *regmap;
+     struct led_classdev cdev;
+     unsigned int index;
+};
+
+static int max5970_led_set_brightness(struct led_classdev *cdev,
+                                   enum led_brightness brightness)
+{
+     struct max5970_led *ddata = ldev_to_maxled(cdev);
+     int ret, val;
+
+     /* Set/clear corresponding bit for given led index */
+     val = !brightness ? BIT(ddata->index) : 0;
+
+     ret = regmap_update_bits(ddata->regmap, MAX5970_REG_LED_FLASH, BIT(ddata->index), val);
+     if (ret < 0)
+             dev_err(cdev->dev, "failed to set brightness %d", ret);
+
+     return ret;
+}
+
+static int max5970_led_probe(struct platform_device *pdev)
Quoted 8 lines

drivers/leds/leds-max5970.c line 47

Quoted 6 lines
drivers/leds/leds-max5970.c new file +6 -0
+{
+     struct device *dev = &pdev->dev;
+     struct device_node *np = dev_of_node(dev->parent);
+     struct regmap *regmap;
+     struct device_node *led_node;
+     struct device_node *child;

Nit: You can place these on the same line.

Ack

Quoted 8 lines
Quoted 7 lines
drivers/leds/leds-max5970.c new file +6 -0
+     struct max5970_led *ddata;
+     int ret = -ENODEV, num_leds = 0;
+
+     regmap = dev_get_regmap(pdev->dev.parent, NULL);
+     if (!regmap)
+             return -EPROBE_DEFER;
Quoted 4 lines
Quoted 2 lines
Quoted 1 line

Why are you deferring here?

This is a Leaf driver. Making sure the parent driver has initialized regmap.

How can this driver initialise before the parent driver?

The parent driver in this case is simple_i2c_mfd.
Based on reference from other similar implementations, the regmap check was adapted.
As you mentioned, your right that leaf driver will not start before parent driver is loaded successfully so probably the DEFER might not be needed here.

Quoted 5 lines
Quoted 5 lines

Thanks,
Naresh

Quoted 3 lines

--
Lee Jones [李琼斯]

--
Lee Jones [李琼斯]

maintainer
#8 lore raw
Quoted 15 lines · Naresh Solanki

Hi

Quoted 12 lines · Lee Jones
Quoted 4 lines · Naresh Solanki

Hey Lee,

Is there anything specific you'd suggest changing in the current patchset, or are we good to proceed?

What do you mean by proceed?

You are good to make changes and submit a subsequent version.

Quoted 8 lines
Quoted 1 line

Not entirely sure what you're asking.

As a follow up on previous discussion regarding use of DEFER on probe if regmap isn't initialized, the implementation was based on other similar drivers & hence it was retained although its not needed due to dependencies.

I'm not entirely sure to keep the regmap check or make another patch revision with regmap check removed ?

You tell me.

You should understand the device you're attempting to support along with the code you're authoring and its subsequent implications. If you don't know what a section of code does or whether/why it's required, why did you write it?

Signature · 1 line
Lee Jones [李琼斯]
#9 lore raw

Hi Lee,

Thank you for your insights. I appreciate your guidance on the matter. Yes will rewrite the change as below:

        regmap = dev_get_regmap(pdev->dev.parent, NULL);
        if (!regmap)
                return -ENODEV;

I believe this modification aligns with your suggestion. Please let me know if this meets the requirements or if you have any further suggestions or adjustments

Regards,
Naresh

Quoted 36 lines · Lee Jones
Quoted 15 lines · Naresh Solanki

Hi

Quoted 12 lines · Lee Jones
Quoted 4 lines · Naresh Solanki

Hey Lee,

Is there anything specific you'd suggest changing in the current patchset, or are we good to proceed?

What do you mean by proceed?

You are good to make changes and submit a subsequent version.

Quoted 8 lines
Quoted 1 line

Not entirely sure what you're asking.

As a follow up on previous discussion regarding use of DEFER on probe if regmap isn't initialized, the implementation was based on other similar drivers & hence it was retained although its not needed due to dependencies.

I'm not entirely sure to keep the regmap check or make another patch revision with regmap check removed ?

You tell me.

You should understand the device you're attempting to support along with the code you're authoring and its subsequent implications. If you don't know what a section of code does or whether/why it's required, why did you write it?

--
Lee Jones [李琼斯]

maintainer
#10 lore raw

Please read this:

https://subspace.kernel.org/etiquette.html#do-not-top-post-when-replying

Quoted 5 lines · Naresh Solanki

Hi Lee,

Thank you for your insights. I appreciate your guidance on the matter. Yes will rewrite the change as below:

Quoted 7 lines
        regmap = dev_get_regmap(pdev->dev.parent, NULL);
        if (!regmap)
                return -ENODEV;

I believe this modification aligns with your suggestion. Please let me know if this meets the requirements or if you have any further suggestions or adjustments

Please submit the next revision.

Quoted 37 lines · Lee Jones
Quoted 36 lines · Naresh Solanki
Quoted 16 lines

Hi

Quoted 12 lines · Lee Jones
Quoted 4 lines · Naresh Solanki

Hey Lee,

Is there anything specific you'd suggest changing in the current patchset, or are we good to proceed?

What do you mean by proceed?

You are good to make changes and submit a subsequent version.

Quoted 8 lines
Quoted 1 line

Not entirely sure what you're asking.

As a follow up on previous discussion regarding use of DEFER on probe if regmap isn't initialized, the implementation was based on other similar drivers & hence it was retained although its not needed due to dependencies.

I'm not entirely sure to keep the regmap check or make another patch revision with regmap check removed ?

You tell me.

You should understand the device you're attempting to support along with the code you're authoring and its subsequent implications. If you don't know what a section of code does or whether/why it's required, why did you write it?

--
Lee Jones [李琼斯]

Signature · 1 line
Lee Jones [李琼斯]
#11 lore raw

Hi Lee

Quoted 4 lines · Lee Jones

Please read this:

https://subspace.kernel.org/etiquette.html#do-not-top-post-when-replying

Ack

Quoted 12 lines · Naresh Solanki
Quoted 10 lines

Hi Lee,

Thank you for your insights. I appreciate your guidance on the matter. Yes will rewrite the change as below:

        regmap = dev_get_regmap(pdev->dev.parent, NULL);
        if (!regmap)
                return -ENODEV;
Quoted 5 lines
Quoted 3 lines

I believe this modification aligns with your suggestion. Please let me know if this meets the requirements or if you have any further suggestions or adjustments

Please submit the next revision.

Ack

Regards,
Naresh

Quoted 41 lines
Quoted 31 lines · Lee Jones
Quoted 29 lines · Naresh Solanki
Quoted 16 lines

Hi

Quoted 12 lines · Lee Jones
Quoted 4 lines · Naresh Solanki

Hey Lee,

Is there anything specific you'd suggest changing in the current patchset, or are we good to proceed?

What do you mean by proceed?

You are good to make changes and submit a subsequent version.

Quoted 8 lines
Quoted 1 line

Not entirely sure what you're asking.

As a follow up on previous discussion regarding use of DEFER on probe if regmap isn't initialized, the implementation was based on other similar drivers & hence it was retained although its not needed due to dependencies.

I'm not entirely sure to keep the regmap check or make another patch revision with regmap check removed ?

You tell me.

Quoted 7 lines
Quoted 7 lines

You should understand the device you're attempting to support along with the code you're authoring and its subsequent implications. If you don't know what a section of code does or whether/why it's required, why did you write it?

--
Lee Jones [李琼斯]

--
Lee Jones [李琼斯]