From: "Marek Behún" <marek.behun@nic.cz>
To: linux-leds@vger.kernel.org
Cc: "Pavel Machek" <pavel@ucw.cz>, "Dan Murphy" <dmurphy@ti.com>,
"Ondřej Jirman" <megous@megous.com>,
linux-kernel@vger.kernel.org, "Rob Herring" <robh+dt@kernel.org>,
devicetree@vger.kernel.org, "Marek Behún" <marek.behun@nic.cz>,
"H . Nikolaus Schaller" <hns@goldelico.com>,
"Grant Feng" <von81@163.com>
Subject: [PATCH leds v2 28/50] leds: is31fl319x: refactor to register LEDs while parsing DT
Date: Fri, 18 Sep 2020 00:33:16 +0200 [thread overview]
Message-ID: <20200917223338.14164-29-marek.behun@nic.cz> (raw)
In-Reply-To: <20200917223338.14164-1-marek.behun@nic.cz>
Refactor the code so that LEDs are registered while device tree is being
parsed.
Signed-off-by: Marek Behún <marek.behun@nic.cz>
Cc: H. Nikolaus Schaller <hns@goldelico.com>
Cc: Grant Feng <von81@163.com>
---
drivers/leds/leds-is31fl319x.c | 174 ++++++++++++++-------------------
1 file changed, 76 insertions(+), 98 deletions(-)
diff --git a/drivers/leds/leds-is31fl319x.c b/drivers/leds/leds-is31fl319x.c
index 8e3e02d959989..1dae319b03d60 100644
--- a/drivers/leds/leds-is31fl319x.c
+++ b/drivers/leds/leds-is31fl319x.c
@@ -209,86 +209,6 @@ static u32 is31fl319x_parse_audio_gain(struct device *dev)
return result;;
}
-static int is31fl319x_parse_child_dt(const struct device *dev,
- const struct device_node *child,
- struct is31fl319x_led *led)
-{
- struct led_classdev *cdev = &led->cdev;
- int ret;
-
- if (of_property_read_string(child, "label", &cdev->name))
- cdev->name = child->name;
-
- ret = of_property_read_string(child, "linux,default-trigger",
- &cdev->default_trigger);
- if (ret < 0 && ret != -EINVAL) /* is optional */
- return ret;
-
- return 0;
-}
-
-static int is31fl319x_parse_dt(struct device *dev,
- struct is31fl319x_chip *is31)
-{
- struct device_node *np = dev_of_node(dev), *child;
- int count;
- int ret;
-
- if (!np)
- return -ENODEV;
-
- is31->cdef = device_get_match_data(dev);
-
- count = of_get_available_child_count(np);
-
- dev_dbg(dev, "probing with %d leds defined in DT\n", count);
-
- if (!count || count > is31->cdef->num_leds) {
- dev_err(dev, "Number of leds defined must be between 1 and %u\n",
- is31->cdef->num_leds);
- return -ENODEV;
- }
-
- for_each_available_child_of_node(np, child) {
- struct is31fl319x_led *led;
- u32 reg;
-
- ret = of_property_read_u32(child, "reg", ®);
- if (ret) {
- dev_err(dev, "Failed to read led 'reg' property\n");
- goto put_child_node;
- }
-
- if (reg < 1 || reg > is31->cdef->num_leds) {
- dev_err(dev, "invalid led reg %u\n", reg);
- ret = -EINVAL;
- goto put_child_node;
- }
-
- led = &is31->leds[reg - 1];
-
- if (led->configured) {
- dev_err(dev, "led %u is already configured\n", reg);
- ret = -EINVAL;
- goto put_child_node;
- }
-
- ret = is31fl319x_parse_child_dt(dev, child, led);
- if (ret) {
- dev_err(dev, "led %u DT parsing failed\n", reg);
- goto put_child_node;
- }
-
- led->configured = true;
- }
-
- return 0;
-
-put_child_node:
- of_node_put(child);
- return ret;
-}
-
static bool is31fl319x_readable_reg(struct device *dev, unsigned int reg)
{ /* we have no readable registers */
return false;
@@ -345,6 +265,78 @@ static inline int is31fl319x_db_to_gain(u32 dezibel)
return dezibel / IS31FL319X_AUDIO_GAIN_DB_STEP;
}
+static int is31fl319x_led_register(struct device *dev,
+ struct is31fl319x_chip *is31,
+ struct device_node *np)
+{
+ struct is31fl319x_led *led;
+ u32 reg;
+ int ret;
+
+ ret = of_property_read_u32(np, "reg", ®);
+ if (ret) {
+ dev_err(dev, "Cannot read reg property of %pOF\n", np);
+ return ret;
+ }
+
+ if (reg < 1 || reg > is31->cdef->num_leds) {
+ dev_err(dev, "Node %pOF has invalid reg property\n", np);
+ return -EINVAL;
+ }
+
+ led = &is31->leds[reg - 1];
+
+ if (led->configured) {
+ dev_err(dev, "Node %pOF 'reg' conflicts with another LED\n",
+ np);
+ return -EEXIST;
+ }
+
+ if (of_property_read_string(np, "label", &led->cdev.name))
+ led->cdev.name = np->name;
+
+ ret = of_property_read_string(np, "linux,default-trigger",
+ &led->cdev.default_trigger);
+ if (ret < 0 && ret != -EINVAL) /* is optional */
+ return ret;
+
+ led->configured = true;
+ led->chip = is31;
+ led->cdev.brightness_set_blocking = is31fl319x_brightness_set;
+
+ ret = devm_led_classdev_register(dev, &led->cdev);
+ if (ret)
+ dev_err(dev, "Failed to register LED for node %pOF\n", np);
+
+ return ret;
+}
+
+static int is31fl319x_parse_and_register(struct device *dev,
+ struct is31fl319x_chip *is31)
+{
+ struct device_node *np = dev_of_node(dev), *child;
+ int ret, count;
+
+ count = of_get_available_child_count(np);
+ if (count < 1 || count > is31->cdef->num_leds) {
+ dev_err(dev, "Between 1 and %u LEDs have to be defined\n",
+ is31->cdef->num_leds);
+ return -ENODEV;
+ }
+
+ dev_dbg(dev, "probing with %d leds defined in DT\n", count);
+
+ for_each_available_child_of_node(np, child) {
+ ret = is31fl319x_led_register(dev, is31, child);
+ if (ret) {
+ of_node_put(child);
+ return ret;
+ }
+ }
+
+ return 0;
+}
+
static int is31fl319x_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
@@ -353,7 +345,6 @@ static int is31fl319x_probe(struct i2c_client *client,
struct gpio_desc *shutdown_gpio;
u32 audio_gain_db;
int err;
- int i = 0;
u32 aggregated_led_microamp;
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
@@ -388,11 +379,8 @@ static int is31fl319x_probe(struct i2c_client *client,
mutex_init(&is31->lock);
- err = is31fl319x_parse_dt(&client->dev, is31);
- if (err)
- goto free_mutex;
-
is31->client = client;
+ is31->cdef = device_get_match_data(dev);
is31->regmap = devm_regmap_init_i2c(client, ®map_config);
if (IS_ERR(is31->regmap)) {
dev_err(&client->dev, "failed to allocate register map\n");
@@ -417,19 +405,9 @@ static int is31fl319x_probe(struct i2c_client *client,
is31fl319x_microamp_to_cs(dev, aggregated_led_microamp) |
is31fl319x_db_to_gain(audio_gain_db));
- for (i = 0; i < is31->cdef->num_leds; i++) {
- struct is31fl319x_led *led = &is31->leds[i];
-
- if (!led->configured)
- continue;
-
- led->chip = is31;
- led->cdev.brightness_set_blocking = is31fl319x_brightness_set;
-
- err = devm_led_classdev_register(&client->dev, &led->cdev);
- if (err < 0)
- goto free_mutex;
- }
+ err = is31fl319x_parse_and_register(dev, is31);
+ if (err)
+ goto free_mutex;
return 0;
--
2.26.2
next prev parent reply other threads:[~2020-09-17 22:36 UTC|newest]
Thread overview: 70+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-09-17 22:32 [PATCH leds v2 00/50] Start moving parsing of `linux,default-trigger` to LED core (a cleanup of LED drivers) Marek Behún
2020-09-17 22:32 ` [PATCH leds v2 01/50] leds: various: compile if COMPILE_TEST=y Marek Behún
2020-09-17 22:32 ` [PATCH leds v2 02/50] leds: ip30: " Marek Behún
2020-09-17 22:32 ` [PATCH leds v2 03/50] leds: fsg: " Marek Behún
2020-09-19 9:56 ` Pavel Machek
2020-09-19 15:54 ` Marek Behun
2020-09-17 22:32 ` [PATCH leds v2 04/50] leds: various: use device_get_match_data Marek Behún
2020-09-17 22:32 ` [PATCH leds v2 05/50] leds: various: guard of_match_table member value with of_match_ptr Marek Behún
2020-09-18 6:15 ` Sakari Ailus
2020-09-18 9:20 ` Marek Behun
2020-09-18 9:57 ` Sakari Ailus
2020-09-18 10:10 ` Marek Behun
2020-09-28 8:03 ` Lee Jones
2020-09-28 8:11 ` Lee Jones
2020-09-28 9:37 ` Sakari Ailus
2020-09-17 22:32 ` [PATCH leds v2 06/50] leds: various: use dev_of_node(dev) instead of dev->of_node Marek Behún
2020-09-17 22:32 ` [PATCH leds v2 07/50] leds: lt3593: do not rewrite .of_node of new LED device to wrong value Marek Behún
2020-09-17 22:32 ` [PATCH leds v2 08/50] leds: various: use only available OF children Marek Behún
2020-09-17 22:32 ` [PATCH leds v2 09/50] leds: various: fix OF node leaks Marek Behún
2020-09-17 22:32 ` [PATCH leds v2 10/50] leds: bcm6328, bcm6358: use devres LED registering function Marek Behún
2020-09-17 22:32 ` [PATCH leds v2 11/50] leds: bcm6328, bcm6358: use struct led_init_data when registering Marek Behún
2020-09-17 22:33 ` [PATCH leds v2 12/50] leds: bcm6328: cosmetic: use reverse christmas tree Marek Behún
2020-09-19 9:18 ` Pavel Machek
2020-09-17 22:33 ` [PATCH leds v2 13/50] leds: bcm6358: " Marek Behún
2020-09-17 22:33 ` [PATCH leds v2 14/50] leds: lm3697: use struct led_init_data when registering Marek Behún
2020-09-17 22:33 ` [PATCH leds v2 15/50] leds: lm3697: cosmetic change: use helper variable, reverse christmas tree Marek Behún
2020-09-18 11:47 ` Dan Murphy
2020-09-18 17:22 ` Marek Behun
2020-09-17 22:33 ` [PATCH leds v2 16/50] leds: max77650: use struct led_init_data when registering Marek Behún
2020-09-17 22:33 ` [PATCH leds v2 17/50] leds: max77650: use reverse christmas tree Marek Behún
2020-09-17 22:33 ` [PATCH leds v2 18/50] leds: mt6323: use struct led_init_data when registering Marek Behún
2020-09-17 22:33 ` [PATCH leds v2 19/50] leds: mt6323: cosmetic change: use helper variable Marek Behún
2020-09-17 22:33 ` [PATCH leds v2 20/50] leds: pm8058: use struct led_init_data when registering Marek Behún
2020-09-29 13:14 ` Linus Walleij
2020-09-17 22:33 ` [PATCH leds v2 21/50] leds: pm8058: cosmetic change: use helper variable Marek Behún
2020-09-17 22:33 ` [PATCH leds v2 22/50] leds: pm8058: cosmetic change: no need to return in if guard Marek Behún
2020-09-17 22:33 ` [PATCH leds v2 23/50] leds: pm8058: cosmetic: use reverse christmas tree Marek Behún
2020-09-17 22:33 ` [PATCH leds v2 24/50] leds: is31fl32xx: use struct led_init_data when registering Marek Behún
2020-09-17 22:33 ` [PATCH leds v2 25/50] leds: is31fl319x: compute aggregated max current separately Marek Behún
2020-09-19 10:20 ` Pavel Machek
2020-09-17 22:33 ` [PATCH leds v2 26/50] leds: is31fl319x: don't store shutdown gpio descriptor Marek Behún
2020-09-17 22:33 ` [PATCH leds v2 27/50] leds: is31fl319x: don't store audio gain value, simply set it Marek Behún
2020-09-17 22:33 ` Marek Behún [this message]
2020-09-17 22:33 ` [PATCH leds v2 29/50] leds: is31fl319x: cosmetic: use reverse christmas tree Marek Behún
2020-09-17 22:33 ` [PATCH leds v2 30/50] leds: is31fl319x: use struct led_init_data when registering Marek Behún
2020-09-17 22:33 ` [PATCH leds v2 31/50] leds: lm36274: use devres LED registering function Marek Behún
2020-09-19 10:04 ` Pavel Machek
2020-09-17 22:33 ` [PATCH leds v2 32/50] leds: lm36274: cosmetic: rename lm36274_data to chip Marek Behún
2020-09-17 22:33 ` [PATCH leds v2 33/50] leds: lm36274: don't iterate through children since there is only one Marek Behún
2020-09-17 22:33 ` [PATCH leds v2 34/50] leds: lm36274: use struct led_init_data when registering Marek Behún
2020-09-17 22:33 ` [PATCH leds v2 35/50] leds: lm36274: do not set chip settings in DT parsing function Marek Behún
2020-09-17 22:33 ` [PATCH leds v2 36/50] leds: ns2: use devres LED registering function Marek Behún
2020-09-17 22:33 ` [PATCH leds v2 37/50] leds: ns2: alloc simple array instead of struct ns2_led_priv Marek Behún
2020-09-17 22:33 ` [PATCH leds v2 38/50] leds: ns2: support OF probing only, forget platdata Marek Behún
2020-09-17 22:33 ` [PATCH leds v2 39/50] leds: ns2: move parsing of one LED into separate function Marek Behún
2020-09-17 22:33 ` [PATCH leds v2 40/50] leds: ns2: use devres API for getting GPIO descriptors Marek Behún
2020-09-17 22:33 ` [PATCH leds v2 41/50] leds: ns2: cosmetic structure rename Marek Behún
2020-09-17 22:33 ` [PATCH leds v2 42/50] leds: ns2: cosmetic variable rename Marek Behún
2020-09-17 22:33 ` [PATCH leds v2 43/50] leds: ns2: cosmetic change Marek Behún
2020-09-17 22:33 ` [PATCH leds v2 44/50] leds: ns2: cosmetic change: use helper variable Marek Behún
2020-09-17 22:33 ` [PATCH leds v2 45/50] leds: ns2: register LED immediately after parsing DT properties Marek Behún
2020-09-17 22:33 ` [PATCH leds v2 46/50] leds: ns2: remove unneeded variable Marek Behún
2020-09-17 22:33 ` [PATCH leds v2 47/50] leds: ns2: cosmetic: use reverse christmas tree Marek Behún
2020-09-17 22:33 ` [PATCH leds v2 48/50] leds: ns2: reorder headers alphabetically Marek Behún
2020-09-19 10:12 ` Pavel Machek
2020-09-17 22:33 ` [PATCH leds v2 49/50] leds: ns2: use struct led_init_data when registering Marek Behún
2020-09-17 22:33 ` [PATCH leds v2 50/50] leds: parse linux,default-trigger DT property in LED core Marek Behún
2020-09-19 10:19 ` Pavel Machek
2020-09-22 17:32 ` [PATCH leds v2 00/50] Start moving parsing of `linux,default-trigger` to LED core (a cleanup of LED drivers) Simon Guinot
2020-09-26 13:47 ` Simon Guinot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200917223338.14164-29-marek.behun@nic.cz \
--to=marek.behun@nic.cz \
--cc=devicetree@vger.kernel.org \
--cc=dmurphy@ti.com \
--cc=hns@goldelico.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-leds@vger.kernel.org \
--cc=megous@megous.com \
--cc=pavel@ucw.cz \
--cc=robh+dt@kernel.org \
--cc=von81@163.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).