From patchwork Thu Sep 6 22:09:40 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 146141 Delivered-To: patch@linaro.org Received: by 2002:a2e:1648:0:0:0:0:0 with SMTP id 8-v6csp410321ljw; Thu, 6 Sep 2018 15:09:55 -0700 (PDT) X-Google-Smtp-Source: ANB0VdYAk4SopeTndKHNFm1K9wpoZSL8VWA4DVqha379fyV36Hy/VRcfIy8JhWIoV03NYPzhBc3B X-Received: by 2002:a62:1157:: with SMTP id z84-v6mr5291898pfi.66.1536271795419; Thu, 06 Sep 2018 15:09:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536271795; cv=none; d=google.com; s=arc-20160816; b=z2r1Y5qFr5QJzCd9sgkh+VSuvIPHmAsvO6p5TkVjK2FaF59vjDvKRjB57HR9PkgxWC mpK8bvEj+cJ0a+W2cQFa9jRZ/sCwDrF5iP7ZukYEIenxmlb8kZccbKzSB1QaYTM1La/R lpetrLHAPxgmDHUAk5W07yfh61/sG7pNxRIvdn00GO8M2JTi+9t5u/sDkOsVoB9F21JU 3e5vf/ul9jLUSEwk6jU0bOA/X0rA9jl49XnS0NKwZozqfG82HeG+cN1fJQUMsAH8hJN7 qU5FRV2zLH9GY0xUPJQAgf9XBynedcwff9t844utaHgHmeZsnLalT21Olra6R/56sO0+ lQQw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature; bh=agAZ/bT6OKTuwgLxMovh/jQBE7U5cMHQLfCHRTT2GTU=; b=Yo9cygdPTJCjk2zyVjnJ0M8lMrlOx55ZDXWl8kqp14Te5QJJX5NN73GlV5uGPsrKw3 knXNZeO8rEsm8qHoCWHxNQxh50U5vap73n9Qhb/VzOx2MRU1Maz8G/ktl6E+Xkru1xUs Aop1//BYG9QeJELQx3cCOxa5Gc/vit2Gw1lo70IqIoNAkrj3tn0SDP9ip4r6dresUyqf SyTHRnYqD5RPFKUXn61wmQGdlBts21XjiYNpq8RwOBC2Y+BornEyNk9zs4TEmEzzq1hs G+0Jk3wUIMnAqPsTnZerWRJiK/23W3QvO15SpJjrJwYtCMvL2wi0+lhIVD6brfOHL7Px ItHA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=apO+OJHG; spf=pass (google.com: best guess record for domain of linux-leds-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-leds-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d11-v6si6412323plo.91.2018.09.06.15.09.55; Thu, 06 Sep 2018 15:09:55 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-leds-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=apO+OJHG; spf=pass (google.com: best guess record for domain of linux-leds-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-leds-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727730AbeIGCr1 (ORCPT + 1 other); Thu, 6 Sep 2018 22:47:27 -0400 Received: from mail-lf1-f66.google.com ([209.85.167.66]:39133 "EHLO mail-lf1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726173AbeIGCr1 (ORCPT ); Thu, 6 Sep 2018 22:47:27 -0400 Received: by mail-lf1-f66.google.com with SMTP id v77-v6so10405278lfa.6 for ; Thu, 06 Sep 2018 15:09:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=agAZ/bT6OKTuwgLxMovh/jQBE7U5cMHQLfCHRTT2GTU=; b=apO+OJHGCa7vyznOV9YI+fF0TZvA8tk/zZ0yebA70l0wYPGp6M2Twv19IKKa1i9dEF tMlKEtzBx0LBY2wJo0b26bxQR9BhWlpN4aLM6C6iORiJPWdW6CMA7/qqqpMr6Z0C/hO+ i09WPmiS7XqLfkxufzW8p0H2NZZKRTtLgFIaE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=agAZ/bT6OKTuwgLxMovh/jQBE7U5cMHQLfCHRTT2GTU=; b=icwYjWqx0ebINqBa+Qjpo+ApNE3EKQiwtecNT3B53EEIBMBlRSvLw36C4oBf3jhVoc G78jaOJ0bXpMrmJ7njxssxLcioJvzWLVp4+JmdhRrZX8GXvl4qVWq8nE9mfYdeODHerT tBd97RtwX/xaDnNJRoZGowxV219juruQAFYs6ZnhZGWjMBJPnzKjZFbO/3t50Sh9tq22 oWqdTVhrpraQ3Hrm6yoTfAe7p6SnjqzyPmlJRxRmzarHSkdPQQdeQQ0gXo52FvGwNmKn jClez3k8uhOJnIuTazd5ZbuFiHIgVDTDPNUnDJ25bp52WPejY5ESxe66y2NnDLW3KfyI DeFQ== X-Gm-Message-State: APzg51Cx9pzAjGJZFrvDEUCh0eae6ZIbeSFAkRNwl/kG8U47OWuehzJI AKTPuXZ6Up79+a5VpUXmUU49R1vLcMHqTw== X-Received: by 2002:a19:5353:: with SMTP id h80-v6mr3333688lfb.9.1536271792548; Thu, 06 Sep 2018 15:09:52 -0700 (PDT) Received: from localhost.bredbandsbolaget (c-ae7b71d5.014-348-6c756e10.bbcust.telenor.se. [213.113.123.174]) by smtp.gmail.com with ESMTPSA id g63-v6sm1015390lji.79.2018.09.06.15.09.50 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 06 Sep 2018 15:09:50 -0700 (PDT) From: Linus Walleij To: Jacek Anaszewski , Pavel Machek Cc: linux-leds@vger.kernel.org, Linus Walleij Subject: [PATCH] leds: gpio: Try to lookup gpiod from device Date: Fri, 7 Sep 2018 00:09:40 +0200 Message-Id: <20180906220940.12053-1-linus.walleij@linaro.org> X-Mailer: git-send-email 2.17.1 Sender: linux-leds-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org This augments the GPIO lookup code in the GPIO LEDs to attempt to look up a GPIO descriptor from the device with index. This makes it possible to use GPIO machine look-up tables and stop passing global GPIO numbers through platform data. Using this we can stepwise convert existing board files to use machine descriptor tables and then eventually drop the legacy GPIO support and only include and use descriptors exclusively. Signed-off-by: Linus Walleij --- drivers/leds/leds-gpio.c | 93 +++++++++++++++++++++++++++------------- 1 file changed, 63 insertions(+), 30 deletions(-) -- 2.17.1 diff --git a/drivers/leds/leds-gpio.c b/drivers/leds/leds-gpio.c index 764c31301f90..fbc623148595 100644 --- a/drivers/leds/leds-gpio.c +++ b/drivers/leds/leds-gpio.c @@ -81,35 +81,6 @@ static int create_gpio_led(const struct gpio_led *template, { int ret, state; - led_dat->gpiod = template->gpiod; - if (!led_dat->gpiod) { - /* - * This is the legacy code path for platform code that - * still uses GPIO numbers. Ultimately we would like to get - * rid of this block completely. - */ - unsigned long flags = GPIOF_OUT_INIT_LOW; - - /* skip leds that aren't available */ - if (!gpio_is_valid(template->gpio)) { - dev_info(parent, "Skipping unavailable LED gpio %d (%s)\n", - template->gpio, template->name); - return 0; - } - - if (template->active_low) - flags |= GPIOF_ACTIVE_LOW; - - ret = devm_gpio_request_one(parent, template->gpio, flags, - template->name); - if (ret < 0) - return ret; - - led_dat->gpiod = gpio_to_desc(template->gpio); - if (!led_dat->gpiod) - return -EINVAL; - } - led_dat->cdev.name = template->name; led_dat->cdev.default_trigger = template->default_trigger; led_dat->can_sleep = gpiod_cansleep(led_dat->gpiod); @@ -231,6 +202,53 @@ static const struct of_device_id of_gpio_leds_match[] = { MODULE_DEVICE_TABLE(of, of_gpio_leds_match); +static struct gpio_desc *gpio_led_get_gpiod(struct device *dev, int idx, + const struct gpio_led *template) +{ + struct gpio_desc *gpiod; + unsigned long flags = GPIOF_OUT_INIT_LOW; + int ret; + + /* + * This means the LED does not come from the device tree + * or ACPI, so let's try just getting it by index from the + * device, this will hit the board file, if any and get + * the GPIO from there. + */ + gpiod = devm_gpiod_get_index(dev, NULL, idx, flags); + if (!IS_ERR(gpiod)) { + gpiod_set_consumer_name(gpiod, template->name); + return gpiod; + } + if (PTR_ERR(gpiod) != -ENOENT) + return gpiod; + + /* + * This is the legacy code path for platform code that + * still uses GPIO numbers. Ultimately we would like to get + * rid of this block completely. + */ + + /* skip leds that aren't available */ + if (!gpio_is_valid(template->gpio)) { + return ERR_PTR(-ENOENT); + } + + if (template->active_low) + flags |= GPIOF_ACTIVE_LOW; + + ret = devm_gpio_request_one(dev, template->gpio, flags, + template->name); + if (ret < 0) + return ERR_PTR(ret); + + gpiod = gpio_to_desc(template->gpio); + if (!gpiod) + return ERR_PTR(-EINVAL); + + return gpiod; +} + static int gpio_led_probe(struct platform_device *pdev) { struct gpio_led_platform_data *pdata = dev_get_platdata(&pdev->dev); @@ -246,7 +264,22 @@ static int gpio_led_probe(struct platform_device *pdev) priv->num_leds = pdata->num_leds; for (i = 0; i < priv->num_leds; i++) { - ret = create_gpio_led(&pdata->leds[i], &priv->leds[i], + const struct gpio_led *template = &pdata->leds[i]; + struct gpio_led_data *led_dat = &priv->leds[i]; + + if (template->gpiod) + led_dat->gpiod = template->gpiod; + else + led_dat->gpiod = + gpio_led_get_gpiod(&pdev->dev, + i, template); + if (IS_ERR(led_dat->gpiod)) { + dev_info(&pdev->dev, "Skipping unavailable LED gpio %d (%s)\n", + template->gpio, template->name); + continue; + } + + ret = create_gpio_led(template, led_dat, &pdev->dev, NULL, pdata->gpio_blink_set); if (ret < 0)