Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp754735pxk; Thu, 17 Sep 2020 15:38:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzsnI69os4+s/TM36ph4EJ6skiB1Z1PeUj2vsLyjXJkTvZR7Gzy4n0zBkS9AiUiyVqlo3YD X-Received: by 2002:a17:906:8401:: with SMTP id n1mr32103599ejx.215.1600382318436; Thu, 17 Sep 2020 15:38:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600382318; cv=none; d=google.com; s=arc-20160816; b=FPK2qQH41e2/iI3+nCYdx5ic6tR/UldoVfohejgtRJ2wRr2lrxdxMtdxZdbw2hrgam XXFUiveP8PgBAoHNUcJddOshzT2v/yZqGTfD4ODkcUAJX0g4+8zYyn/CjoLb+avJUTgV I5mHuEGy+cqv362MdD+KdYrjn2eTnuDfEvbkrc9zwWEwNjhHY86OqYFb6b1DDlB/eneF 8flEHLOFTNjFXJvFExSiSUe11RDuMox9o34nYLJVPLpjqV+/eiUHWBV914rwU4AqHf6/ a8Ml+5e6jHFVgjUy4Pw8FMDhlgRO4EvlKSKMX8mREhRYG/7ojuRGjaK8czihVMCkr7Hx nh+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=eOPpLpg91vOSTX6WBEGwfN0v83ouLRFlvZmR7dA1Mj0=; b=k/MdCVD/GD1aWBNUNDlyCX1xYULA3fXmV5P4moTB1J5jFkXwApERLczxEKnbg8PBW4 4mCZCp+cMU8WxX+ACmvzOrKBzR6nUnzVmz6Pm9aZ9e/fLmpv3krbpuDMihNr9eQrzUR6 yS/ZW0NoebLq3y9wD/VKwEjyAkxKQKVHY3nPVpAl8N0fRClbaixI1w/3m6l7Wzzn2P2m 3yzQ0XQ9dioOqniNbaxnZ2AfxoWDmDbI7wXMsTYYsCU4oJTiAlKlTDlbnzDXcLcrmo+N VotJoSywvZFzcp1OG/kmTiY89gRfHcKrnRZOWCAxZT86ZZdjS2C1zquN81JLbUWlwYv4 954Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nic.cz header.s=default header.b=LrvDsyzI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=nic.cz Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u14si849899ejt.556.2020.09.17.15.38.15; Thu, 17 Sep 2020 15:38:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@nic.cz header.s=default header.b=LrvDsyzI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=nic.cz Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726803AbgIQWgE (ORCPT + 99 others); Thu, 17 Sep 2020 18:36:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49448 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726291AbgIQWeL (ORCPT ); Thu, 17 Sep 2020 18:34:11 -0400 Received: from mail.nic.cz (lists.nic.cz [IPv6:2001:1488:800:400::400]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 10697C061353; Thu, 17 Sep 2020 15:34:00 -0700 (PDT) Received: from dellmb.labs.office.nic.cz (unknown [IPv6:2001:1488:fffe:6:cac7:3539:7f1f:463]) by mail.nic.cz (Postfix) with ESMTP id 5E9ED142047; Fri, 18 Sep 2020 00:33:58 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=nic.cz; s=default; t=1600382038; bh=kO27Fg+MT9zBd6Q8cGBkhOm5D9hO6342LpulPUFWOrU=; h=From:To:Date; b=LrvDsyzICRwXELJouQWHAIZXygws0xakvJ1Ff2KVswHb5exzPdpycx3OmUhD+/vJI 7qJXYNHa+QQ3B4/FsaEaaWANsB63UiYYh7q1F3JfiyIFib3H8UonUPMcUfsjDetWJ+ hAf+sbokYYuv72YfaQ4+DNWQOj8Xwa4F/Bx2aANA= From: =?UTF-8?q?Marek=20Beh=C3=BAn?= To: linux-leds@vger.kernel.org Cc: Pavel Machek , Dan Murphy , =?UTF-8?q?Ond=C5=99ej=20Jirman?= , linux-kernel@vger.kernel.org, Rob Herring , devicetree@vger.kernel.org, =?UTF-8?q?Marek=20Beh=C3=BAn?= , "H . Nikolaus Schaller" , Grant Feng Subject: [PATCH leds v2 28/50] leds: is31fl319x: refactor to register LEDs while parsing DT Date: Fri, 18 Sep 2020 00:33:16 +0200 Message-Id: <20200917223338.14164-29-marek.behun@nic.cz> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200917223338.14164-1-marek.behun@nic.cz> References: <20200917223338.14164-1-marek.behun@nic.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mail.nic.cz X-Spam-Status: No, score=0.00 X-Spamd-Bar: / X-Virus-Scanned: clamav-milter 0.102.2 at mail X-Virus-Status: Clean Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Refactor the code so that LEDs are registered while device tree is being parsed. Signed-off-by: Marek BehĂșn Cc: H. Nikolaus Schaller Cc: Grant Feng --- 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