Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp246338pxf; Wed, 24 Mar 2021 04:16:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyelXxCjqj8HWmp09xC2nNrFIRzOQw4IfqRvlOR9HOMplHId8pTLkfSEOY0L9rd2fZBuWQd X-Received: by 2002:a17:906:b80c:: with SMTP id dv12mr3128616ejb.110.1616584588598; Wed, 24 Mar 2021 04:16:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616584588; cv=none; d=google.com; s=arc-20160816; b=CdxukW1Lq6GgeBf7e7CvULv4u8TKIMDCEwyZN9K6ANBj8SWqLbCHwy+Y32nMuAOQ5G i7WbXcTa+20X0RehG3LoiDctvhfq8l7I2Cb40woqMfUWcu1E055q8FYVidImiOaKBlx+ 2DaCqBzpBczTK2rQuzTR8kaBT/b3uNpm4evuLq5nI/IHR/oolR+j3C/UIuyVcHCqXxLI D3c7jj7Q7IfiVxdncPO9+ZSsixuaGAPLdbqixH6XXXetuIpywgQRsuL6aWtI6DFb1jMn Kn9V6PD6LyRl7cweMN1Xz+DraGGSaY387PkosapLU8mmmkSQxMQtPF8jbjCocoS8CmIK dyew== 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:subject:cc:to:from:date; bh=+Rsxa1K/vCAGRDqB3sgAxqxc6flHvAMO9oZuI+MSqqI=; b=RrIh3UDn9n/vp4+kaeSR6hBFEAROyAl+yjQI9QKqZvyjy9rVQGS3UVgyG35ew77eps sdNLvh9ClkTws3MSJRFF4pwLGTc66TPwHgjPy21Is09GoEWt3uyUnAhY46ZkZXr7JS3o L25w9EsgCL6p52f2+nkDH+Wt0pajaXmrRUDoMoZ2QSRI8tBXhbWjeuwkycXyqDqbRmbs Xr7x/zd0d5uTnY6q2KHRkBquFr5BVas/4rUR/RrKIkwqYxJrD15HzgeRWgw1iPIjylI3 aPfsj8bT6fFj78YdlFhaZamrCn/cuYpG4b6L7Vlw+2xhRuZ6fvAlHtgWNdYnW5PnX7Ai QvrQ== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (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 i6si1505797edu.313.2021.03.24.04.16.05; Wed, 24 Mar 2021 04:16:28 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=nic.cz Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232387AbhCXJeg (ORCPT + 99 others); Wed, 24 Mar 2021 05:34:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42158 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232548AbhCXJef (ORCPT ); Wed, 24 Mar 2021 05:34:35 -0400 Received: from mail.nic.cz (mail.nic.cz [IPv6:2001:1488:800:400::400]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 486BFC061763; Wed, 24 Mar 2021 02:34:35 -0700 (PDT) Received: from thinkpad (unknown [IPv6:2a0e:b107:ae1:0:3e97:eff:fe61:c680]) by mail.nic.cz (Postfix) with ESMTPSA id A0E62140A26; Wed, 24 Mar 2021 10:34:32 +0100 (CET) Date: Wed, 24 Mar 2021 10:34:31 +0000 From: Marek Behun To: Hermes Zhang Cc: , , , , , , , , Subject: Re: [PATCH 1/2] leds: leds-multi-gpio: Add multiple GPIOs LED driver Message-ID: <20210324103431.4b945915@thinkpad> In-Reply-To: <20210324075631.5004-2-chenhui.zhang@axis.com> References: <20210324075631.5004-1-chenhui.zhang@axis.com> <20210324075631.5004-2-chenhui.zhang@axis.com> X-Mailer: Claws Mail 3.17.8 (GTK+ 2.24.32; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-100.0 required=5.9 tests=SHORTCIRCUIT, USER_IN_WELCOMELIST,USER_IN_WHITELIST shortcircuit=ham autolearn=disabled version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mail.nic.cz 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 On Wed, 24 Mar 2021 15:56:30 +0800 Hermes Zhang wrote: > From: Hermes Zhang > > Introduce a new multiple GPIOs LED driver. This LED will made of > multiple GPIOs (up to 8) and will map different brightness to different > GPIOs states which defined in dts file. I wonder how many boards have such LEDs. Also if it wouldn't be better to expand the original leds-gpio driver. Probably depends on how much larger would such expansion make the leds-gpio driver. > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include Why do you include slab.h? > + > +#define MAX_GPIO_NUM 8 > + > +struct multi_gpio_led_priv { > + struct led_classdev cdev; > + > + struct gpio_descs *gpios; > + > + u8 *states; > + int nr_states; > +}; Use flexible array members. Allocate with devm_kzalloc(dev, struct_size(priv, states, priv->nr_states), GFP_KERNEL) > + > + > +static void multi_gpio_led_set(struct led_classdev *led_cdev, > + enum led_brightness value) > +{ > + struct multi_gpio_led_priv *priv; > + int idx; > + > + DECLARE_BITMAP(values, MAX_GPIO_NUM); > + > + priv = container_of(led_cdev, struct multi_gpio_led_priv, cdev); > + > + idx = (value - LED_OFF) * priv->nr_states / (LED_FULL + 1); LED_FULL / LED_OFF are deprecated, don't use them. > + > + values[0] = priv->states[idx]; > + > + gpiod_set_array_value(priv->gpios->ndescs, priv->gpios->desc, > + priv->gpios->info, values); > +} > + > +static int multi_gpio_led_probe(struct platform_device *pdev) > +{ > + struct device *dev = &pdev->dev; > + struct device_node *node = dev->of_node; > + struct multi_gpio_led_priv *priv = NULL; > + int ret; > + const char *state = NULL; > + struct led_init_data init_data = {}; > + > + priv = devm_kzalloc(dev, sizeof(struct multi_gpio_led_priv), GFP_KERNEL); > + if (!priv) > + return -ENOMEM; > + > + priv->gpios = devm_gpiod_get_array(dev, "led", GPIOD_OUT_LOW); > + if (IS_ERR(priv->gpios)) > + return PTR_ERR(priv->gpios); > + > + if (priv->gpios->ndescs >= MAX_GPIO_NUM) { > + dev_err(dev, "Too many GPIOs\n"); > + return -EINVAL; > + } > + > + ret = of_property_count_u8_elems(node, "led-states"); > + if (ret < 0) > + return ret; > + > + priv->nr_states = ret; > + priv->states = devm_kzalloc(dev, sizeof(*priv->states) * priv->nr_states, GFP_KERNEL); > + if (!priv->states) > + return -ENOMEM; > + > + ret = of_property_read_u8_array(node, "led-states", priv->states, priv->nr_states); > + if (ret) > + return ret; > + > + priv->cdev.max_brightness = LED_FULL; ???? max_brightness is not 255 (= LED_FULL). max_brightness must be derived from the led-states property. > + priv->cdev.default_trigger = of_get_property(node, "linux,default-trigger", NULL); > + priv->cdev.brightness_set = multi_gpio_led_set; > + > + init_data.fwnode = of_fwnode_handle(node); > + > + ret = devm_led_classdev_register_ext(dev, &priv->cdev, &init_data); > + if (ret < 0) > + return ret; > + > + of_property_read_string(node, "default-state", &state); > + if (!strcmp(state, "on")) > + multi_gpio_led_set(&priv->cdev, LED_FULL); > + else > + multi_gpio_led_set(&priv->cdev, LED_OFF); Again LED_FULL and LED_OFF... What about default-state = "keep" ? Hermes, do you actually have a device that controls LEDs this way? How many brightness options do they have? Also I think this functionality could be easily incorporated into the existing leds-gpio driver, instead of creating new driver. Moreover your driver can control only one LED, so it needs to be probed multiple times for multiple LEDs. Meanwhile the leds-gpio driver can register multiple LEDs in one probe... Marek