^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * LEDs driver for GPIOs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2007 8D Technologies inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Raphael Assenat <raph@8d.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 2008 Freescale Semiconductor, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/leds.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/property.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct gpio_led_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct led_classdev cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct gpio_desc *gpiod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) u8 can_sleep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) u8 blinking;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) gpio_blink_set_t platform_gpio_blink_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static inline struct gpio_led_data *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) cdev_to_gpio_led_data(struct led_classdev *led_cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return container_of(led_cdev, struct gpio_led_data, cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static void gpio_led_set(struct led_classdev *led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) enum led_brightness value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct gpio_led_data *led_dat = cdev_to_gpio_led_data(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) int level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (value == LED_OFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) level = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) level = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (led_dat->blinking) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) led_dat->platform_gpio_blink_set(led_dat->gpiod, level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) led_dat->blinking = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (led_dat->can_sleep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) gpiod_set_value_cansleep(led_dat->gpiod, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) gpiod_set_value(led_dat->gpiod, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static int gpio_led_set_blocking(struct led_classdev *led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) enum led_brightness value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) gpio_led_set(led_cdev, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static int gpio_blink_set(struct led_classdev *led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) unsigned long *delay_on, unsigned long *delay_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct gpio_led_data *led_dat = cdev_to_gpio_led_data(led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) led_dat->blinking = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return led_dat->platform_gpio_blink_set(led_dat->gpiod, GPIO_LED_BLINK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) delay_on, delay_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) static int create_gpio_led(const struct gpio_led *template,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct gpio_led_data *led_dat, struct device *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct fwnode_handle *fwnode, gpio_blink_set_t blink_set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct led_init_data init_data = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) int ret, state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) led_dat->cdev.default_trigger = template->default_trigger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) led_dat->can_sleep = gpiod_cansleep(led_dat->gpiod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (!led_dat->can_sleep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) led_dat->cdev.brightness_set = gpio_led_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) led_dat->cdev.brightness_set_blocking = gpio_led_set_blocking;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) led_dat->blinking = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (blink_set) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) led_dat->platform_gpio_blink_set = blink_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) led_dat->cdev.blink_set = gpio_blink_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (template->default_state == LEDS_GPIO_DEFSTATE_KEEP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) state = gpiod_get_value_cansleep(led_dat->gpiod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (state < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) state = (template->default_state == LEDS_GPIO_DEFSTATE_ON);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) led_dat->cdev.brightness = state ? LED_FULL : LED_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (!template->retain_state_suspended)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (template->panic_indicator)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) led_dat->cdev.flags |= LED_PANIC_INDICATOR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (template->retain_state_shutdown)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) led_dat->cdev.flags |= LED_RETAIN_AT_SHUTDOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) ret = gpiod_direction_output(led_dat->gpiod, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (template->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) led_dat->cdev.name = template->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) ret = devm_led_classdev_register(parent, &led_dat->cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) init_data.fwnode = fwnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) ret = devm_led_classdev_register_ext(parent, &led_dat->cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) &init_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct gpio_leds_priv {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) int num_leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct gpio_led_data leds[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) struct fwnode_handle *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) struct gpio_leds_priv *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) int count, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) count = device_get_child_node_count(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (!count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) priv = devm_kzalloc(dev, struct_size(priv, leds, count), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (!priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) device_for_each_child_node(dev, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) struct gpio_led_data *led_dat = &priv->leds[priv->num_leds];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) struct gpio_led led = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) const char *state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * Acquire gpiod from DT with uninitialized label, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * will be updated after LED class device is registered,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * Only then the final LED name is known.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) led.gpiod = devm_fwnode_get_gpiod_from_child(dev, NULL, child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) GPIOD_ASIS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (IS_ERR(led.gpiod)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) fwnode_handle_put(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) return ERR_CAST(led.gpiod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) led_dat->gpiod = led.gpiod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (!fwnode_property_read_string(child, "default-state",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) &state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (!strcmp(state, "keep"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) led.default_state = LEDS_GPIO_DEFSTATE_KEEP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) else if (!strcmp(state, "on"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) led.default_state = LEDS_GPIO_DEFSTATE_ON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) led.default_state = LEDS_GPIO_DEFSTATE_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (fwnode_property_present(child, "retain-state-suspended"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) led.retain_state_suspended = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (fwnode_property_present(child, "retain-state-shutdown"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) led.retain_state_shutdown = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (fwnode_property_present(child, "panic-indicator"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) led.panic_indicator = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) ret = create_gpio_led(&led, led_dat, dev, child, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) fwnode_handle_put(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) /* Set gpiod label to match the corresponding LED name. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) gpiod_set_consumer_name(led_dat->gpiod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) led_dat->cdev.dev->kobj.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) priv->num_leds++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static const struct of_device_id of_gpio_leds_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) { .compatible = "gpio-leds", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) MODULE_DEVICE_TABLE(of, of_gpio_leds_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static struct gpio_desc *gpio_led_get_gpiod(struct device *dev, int idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) const struct gpio_led *template)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) struct gpio_desc *gpiod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) unsigned long flags = GPIOF_OUT_INIT_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * This means the LED does not come from the device tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * or ACPI, so let's try just getting it by index from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * device, this will hit the board file, if any and get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * the GPIO from there.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) gpiod = devm_gpiod_get_index(dev, NULL, idx, GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (!IS_ERR(gpiod)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) gpiod_set_consumer_name(gpiod, template->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) return gpiod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (PTR_ERR(gpiod) != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) return gpiod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) * This is the legacy code path for platform code that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * still uses GPIO numbers. Ultimately we would like to get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * rid of this block completely.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) /* skip leds that aren't available */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (!gpio_is_valid(template->gpio))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) return ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) if (template->active_low)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) flags |= GPIOF_ACTIVE_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) ret = devm_gpio_request_one(dev, template->gpio, flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) template->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) gpiod = gpio_to_desc(template->gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) if (!gpiod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) return gpiod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) static int gpio_led_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) struct gpio_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) struct gpio_leds_priv *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) int i, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) if (pdata && pdata->num_leds) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) priv = devm_kzalloc(&pdev->dev, struct_size(priv, leds, pdata->num_leds),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (!priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) priv->num_leds = pdata->num_leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) for (i = 0; i < priv->num_leds; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) const struct gpio_led *template = &pdata->leds[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) struct gpio_led_data *led_dat = &priv->leds[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (template->gpiod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) led_dat->gpiod = template->gpiod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) led_dat->gpiod =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) gpio_led_get_gpiod(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) i, template);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (IS_ERR(led_dat->gpiod)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) dev_info(&pdev->dev, "Skipping unavailable LED gpio %d (%s)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) template->gpio, template->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) ret = create_gpio_led(template, led_dat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) &pdev->dev, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) pdata->gpio_blink_set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) priv = gpio_leds_create(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) if (IS_ERR(priv))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) return PTR_ERR(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) platform_set_drvdata(pdev, priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) static void gpio_led_shutdown(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) struct gpio_leds_priv *priv = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) for (i = 0; i < priv->num_leds; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) struct gpio_led_data *led = &priv->leds[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if (!(led->cdev.flags & LED_RETAIN_AT_SHUTDOWN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) gpio_led_set(&led->cdev, LED_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) static struct platform_driver gpio_led_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) .probe = gpio_led_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) .shutdown = gpio_led_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) .name = "leds-gpio",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) .of_match_table = of_gpio_leds_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) module_platform_driver(gpio_led_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) MODULE_AUTHOR("Raphael Assenat <raph@8d.com>, Trent Piepho <tpiepho@freescale.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) MODULE_DESCRIPTION("GPIO LED driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) MODULE_ALIAS("platform:leds-gpio");