| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * LEDs driver for GPIOs |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (C) 2007 8D Technologies inc. |
|---|
| 5 | 6 | * Raphael Assenat <raph@8d.com> |
|---|
| 6 | 7 | * Copyright (C) 2008 Freescale Semiconductor, Inc. |
|---|
| 7 | | - * |
|---|
| 8 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 9 | | - * it under the terms of the GNU General Public License version 2 as |
|---|
| 10 | | - * published by the Free Software Foundation. |
|---|
| 11 | | - * |
|---|
| 12 | 8 | */ |
|---|
| 13 | 9 | #include <linux/err.h> |
|---|
| 14 | 10 | #include <linux/gpio.h> |
|---|
| .. | .. |
|---|
| 77 | 73 | |
|---|
| 78 | 74 | static int create_gpio_led(const struct gpio_led *template, |
|---|
| 79 | 75 | struct gpio_led_data *led_dat, struct device *parent, |
|---|
| 80 | | - struct device_node *np, gpio_blink_set_t blink_set) |
|---|
| 76 | + struct fwnode_handle *fwnode, gpio_blink_set_t blink_set) |
|---|
| 81 | 77 | { |
|---|
| 78 | + struct led_init_data init_data = {}; |
|---|
| 82 | 79 | int ret, state; |
|---|
| 83 | 80 | |
|---|
| 84 | | - led_dat->gpiod = template->gpiod; |
|---|
| 85 | | - if (!led_dat->gpiod) { |
|---|
| 86 | | - /* |
|---|
| 87 | | - * This is the legacy code path for platform code that |
|---|
| 88 | | - * still uses GPIO numbers. Ultimately we would like to get |
|---|
| 89 | | - * rid of this block completely. |
|---|
| 90 | | - */ |
|---|
| 91 | | - unsigned long flags = GPIOF_OUT_INIT_LOW; |
|---|
| 92 | | - |
|---|
| 93 | | - /* skip leds that aren't available */ |
|---|
| 94 | | - if (!gpio_is_valid(template->gpio)) { |
|---|
| 95 | | - dev_info(parent, "Skipping unavailable LED gpio %d (%s)\n", |
|---|
| 96 | | - template->gpio, template->name); |
|---|
| 97 | | - return 0; |
|---|
| 98 | | - } |
|---|
| 99 | | - |
|---|
| 100 | | - if (template->active_low) |
|---|
| 101 | | - flags |= GPIOF_ACTIVE_LOW; |
|---|
| 102 | | - |
|---|
| 103 | | - ret = devm_gpio_request_one(parent, template->gpio, flags, |
|---|
| 104 | | - template->name); |
|---|
| 105 | | - if (ret < 0) |
|---|
| 106 | | - return ret; |
|---|
| 107 | | - |
|---|
| 108 | | - led_dat->gpiod = gpio_to_desc(template->gpio); |
|---|
| 109 | | - if (!led_dat->gpiod) |
|---|
| 110 | | - return -EINVAL; |
|---|
| 111 | | - } |
|---|
| 112 | | - |
|---|
| 113 | | - led_dat->cdev.name = template->name; |
|---|
| 114 | 81 | led_dat->cdev.default_trigger = template->default_trigger; |
|---|
| 115 | 82 | led_dat->can_sleep = gpiod_cansleep(led_dat->gpiod); |
|---|
| 116 | 83 | if (!led_dat->can_sleep) |
|---|
| .. | .. |
|---|
| 141 | 108 | if (ret < 0) |
|---|
| 142 | 109 | return ret; |
|---|
| 143 | 110 | |
|---|
| 144 | | - return devm_of_led_classdev_register(parent, np, &led_dat->cdev); |
|---|
| 111 | + if (template->name) { |
|---|
| 112 | + led_dat->cdev.name = template->name; |
|---|
| 113 | + ret = devm_led_classdev_register(parent, &led_dat->cdev); |
|---|
| 114 | + } else { |
|---|
| 115 | + init_data.fwnode = fwnode; |
|---|
| 116 | + ret = devm_led_classdev_register_ext(parent, &led_dat->cdev, |
|---|
| 117 | + &init_data); |
|---|
| 118 | + } |
|---|
| 119 | + |
|---|
| 120 | + return ret; |
|---|
| 145 | 121 | } |
|---|
| 146 | 122 | |
|---|
| 147 | 123 | struct gpio_leds_priv { |
|---|
| 148 | 124 | int num_leds; |
|---|
| 149 | 125 | struct gpio_led_data leds[]; |
|---|
| 150 | 126 | }; |
|---|
| 151 | | - |
|---|
| 152 | | -static inline int sizeof_gpio_leds_priv(int num_leds) |
|---|
| 153 | | -{ |
|---|
| 154 | | - return sizeof(struct gpio_leds_priv) + |
|---|
| 155 | | - (sizeof(struct gpio_led_data) * num_leds); |
|---|
| 156 | | -} |
|---|
| 157 | 127 | |
|---|
| 158 | 128 | static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev) |
|---|
| 159 | 129 | { |
|---|
| .. | .. |
|---|
| 166 | 136 | if (!count) |
|---|
| 167 | 137 | return ERR_PTR(-ENODEV); |
|---|
| 168 | 138 | |
|---|
| 169 | | - priv = devm_kzalloc(dev, sizeof_gpio_leds_priv(count), GFP_KERNEL); |
|---|
| 139 | + priv = devm_kzalloc(dev, struct_size(priv, leds, count), GFP_KERNEL); |
|---|
| 170 | 140 | if (!priv) |
|---|
| 171 | 141 | return ERR_PTR(-ENOMEM); |
|---|
| 172 | 142 | |
|---|
| .. | .. |
|---|
| 174 | 144 | struct gpio_led_data *led_dat = &priv->leds[priv->num_leds]; |
|---|
| 175 | 145 | struct gpio_led led = {}; |
|---|
| 176 | 146 | const char *state = NULL; |
|---|
| 177 | | - struct device_node *np = to_of_node(child); |
|---|
| 178 | 147 | |
|---|
| 179 | | - ret = fwnode_property_read_string(child, "label", &led.name); |
|---|
| 180 | | - if (ret && IS_ENABLED(CONFIG_OF) && np) |
|---|
| 181 | | - led.name = np->name; |
|---|
| 182 | | - if (!led.name) { |
|---|
| 183 | | - fwnode_handle_put(child); |
|---|
| 184 | | - return ERR_PTR(-EINVAL); |
|---|
| 185 | | - } |
|---|
| 186 | | - |
|---|
| 148 | + /* |
|---|
| 149 | + * Acquire gpiod from DT with uninitialized label, which |
|---|
| 150 | + * will be updated after LED class device is registered, |
|---|
| 151 | + * Only then the final LED name is known. |
|---|
| 152 | + */ |
|---|
| 187 | 153 | led.gpiod = devm_fwnode_get_gpiod_from_child(dev, NULL, child, |
|---|
| 188 | 154 | GPIOD_ASIS, |
|---|
| 189 | | - led.name); |
|---|
| 155 | + NULL); |
|---|
| 190 | 156 | if (IS_ERR(led.gpiod)) { |
|---|
| 191 | 157 | fwnode_handle_put(child); |
|---|
| 192 | 158 | return ERR_CAST(led.gpiod); |
|---|
| 193 | 159 | } |
|---|
| 194 | 160 | |
|---|
| 195 | | - fwnode_property_read_string(child, "linux,default-trigger", |
|---|
| 196 | | - &led.default_trigger); |
|---|
| 161 | + led_dat->gpiod = led.gpiod; |
|---|
| 197 | 162 | |
|---|
| 198 | 163 | if (!fwnode_property_read_string(child, "default-state", |
|---|
| 199 | 164 | &state)) { |
|---|
| .. | .. |
|---|
| 212 | 177 | if (fwnode_property_present(child, "panic-indicator")) |
|---|
| 213 | 178 | led.panic_indicator = 1; |
|---|
| 214 | 179 | |
|---|
| 215 | | - ret = create_gpio_led(&led, led_dat, dev, np, NULL); |
|---|
| 180 | + ret = create_gpio_led(&led, led_dat, dev, child, NULL); |
|---|
| 216 | 181 | if (ret < 0) { |
|---|
| 217 | 182 | fwnode_handle_put(child); |
|---|
| 218 | 183 | return ERR_PTR(ret); |
|---|
| 219 | 184 | } |
|---|
| 220 | | - led_dat->cdev.dev->of_node = np; |
|---|
| 185 | + /* Set gpiod label to match the corresponding LED name. */ |
|---|
| 186 | + gpiod_set_consumer_name(led_dat->gpiod, |
|---|
| 187 | + led_dat->cdev.dev->kobj.name); |
|---|
| 221 | 188 | priv->num_leds++; |
|---|
| 222 | 189 | } |
|---|
| 223 | 190 | |
|---|
| .. | .. |
|---|
| 231 | 198 | |
|---|
| 232 | 199 | MODULE_DEVICE_TABLE(of, of_gpio_leds_match); |
|---|
| 233 | 200 | |
|---|
| 201 | +static struct gpio_desc *gpio_led_get_gpiod(struct device *dev, int idx, |
|---|
| 202 | + const struct gpio_led *template) |
|---|
| 203 | +{ |
|---|
| 204 | + struct gpio_desc *gpiod; |
|---|
| 205 | + unsigned long flags = GPIOF_OUT_INIT_LOW; |
|---|
| 206 | + int ret; |
|---|
| 207 | + |
|---|
| 208 | + /* |
|---|
| 209 | + * This means the LED does not come from the device tree |
|---|
| 210 | + * or ACPI, so let's try just getting it by index from the |
|---|
| 211 | + * device, this will hit the board file, if any and get |
|---|
| 212 | + * the GPIO from there. |
|---|
| 213 | + */ |
|---|
| 214 | + gpiod = devm_gpiod_get_index(dev, NULL, idx, GPIOD_OUT_LOW); |
|---|
| 215 | + if (!IS_ERR(gpiod)) { |
|---|
| 216 | + gpiod_set_consumer_name(gpiod, template->name); |
|---|
| 217 | + return gpiod; |
|---|
| 218 | + } |
|---|
| 219 | + if (PTR_ERR(gpiod) != -ENOENT) |
|---|
| 220 | + return gpiod; |
|---|
| 221 | + |
|---|
| 222 | + /* |
|---|
| 223 | + * This is the legacy code path for platform code that |
|---|
| 224 | + * still uses GPIO numbers. Ultimately we would like to get |
|---|
| 225 | + * rid of this block completely. |
|---|
| 226 | + */ |
|---|
| 227 | + |
|---|
| 228 | + /* skip leds that aren't available */ |
|---|
| 229 | + if (!gpio_is_valid(template->gpio)) |
|---|
| 230 | + return ERR_PTR(-ENOENT); |
|---|
| 231 | + |
|---|
| 232 | + if (template->active_low) |
|---|
| 233 | + flags |= GPIOF_ACTIVE_LOW; |
|---|
| 234 | + |
|---|
| 235 | + ret = devm_gpio_request_one(dev, template->gpio, flags, |
|---|
| 236 | + template->name); |
|---|
| 237 | + if (ret < 0) |
|---|
| 238 | + return ERR_PTR(ret); |
|---|
| 239 | + |
|---|
| 240 | + gpiod = gpio_to_desc(template->gpio); |
|---|
| 241 | + if (!gpiod) |
|---|
| 242 | + return ERR_PTR(-EINVAL); |
|---|
| 243 | + |
|---|
| 244 | + return gpiod; |
|---|
| 245 | +} |
|---|
| 246 | + |
|---|
| 234 | 247 | static int gpio_led_probe(struct platform_device *pdev) |
|---|
| 235 | 248 | { |
|---|
| 236 | 249 | struct gpio_led_platform_data *pdata = dev_get_platdata(&pdev->dev); |
|---|
| .. | .. |
|---|
| 238 | 251 | int i, ret = 0; |
|---|
| 239 | 252 | |
|---|
| 240 | 253 | if (pdata && pdata->num_leds) { |
|---|
| 241 | | - priv = devm_kzalloc(&pdev->dev, |
|---|
| 242 | | - sizeof_gpio_leds_priv(pdata->num_leds), |
|---|
| 243 | | - GFP_KERNEL); |
|---|
| 254 | + priv = devm_kzalloc(&pdev->dev, struct_size(priv, leds, pdata->num_leds), |
|---|
| 255 | + GFP_KERNEL); |
|---|
| 244 | 256 | if (!priv) |
|---|
| 245 | 257 | return -ENOMEM; |
|---|
| 246 | 258 | |
|---|
| 247 | 259 | priv->num_leds = pdata->num_leds; |
|---|
| 248 | 260 | for (i = 0; i < priv->num_leds; i++) { |
|---|
| 249 | | - ret = create_gpio_led(&pdata->leds[i], &priv->leds[i], |
|---|
| 261 | + const struct gpio_led *template = &pdata->leds[i]; |
|---|
| 262 | + struct gpio_led_data *led_dat = &priv->leds[i]; |
|---|
| 263 | + |
|---|
| 264 | + if (template->gpiod) |
|---|
| 265 | + led_dat->gpiod = template->gpiod; |
|---|
| 266 | + else |
|---|
| 267 | + led_dat->gpiod = |
|---|
| 268 | + gpio_led_get_gpiod(&pdev->dev, |
|---|
| 269 | + i, template); |
|---|
| 270 | + if (IS_ERR(led_dat->gpiod)) { |
|---|
| 271 | + dev_info(&pdev->dev, "Skipping unavailable LED gpio %d (%s)\n", |
|---|
| 272 | + template->gpio, template->name); |
|---|
| 273 | + continue; |
|---|
| 274 | + } |
|---|
| 275 | + |
|---|
| 276 | + ret = create_gpio_led(template, led_dat, |
|---|
| 250 | 277 | &pdev->dev, NULL, |
|---|
| 251 | 278 | pdata->gpio_blink_set); |
|---|
| 252 | 279 | if (ret < 0) |
|---|