1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * ledtrig-gio.c - LED Trigger Based on GPIO events 4 * 5 * Copyright 2009 Felipe Balbi <me@felipebalbi.com> 6 * Copyright 2023 Linus Walleij <linus.walleij@linaro.org> 7 */ 8 9 #include <linux/module.h> 10 #include <linux/kernel.h> 11 #include <linux/init.h> 12 #include <linux/gpio/consumer.h> 13 #include <linux/interrupt.h> 14 #include <linux/leds.h> 15 #include <linux/slab.h> 16 #include "../leds.h" 17 18 struct gpio_trig_data { 19 struct led_classdev *led; 20 unsigned desired_brightness; /* desired brightness when led is on */ 21 struct gpio_desc *gpiod; /* gpio that triggers the led */ 22 }; 23 24 static irqreturn_t gpio_trig_irq(int irq, void *_led) 25 { 26 struct led_classdev *led = _led; 27 struct gpio_trig_data *gpio_data = led_get_trigger_data(led); 28 int tmp; 29 30 tmp = gpiod_get_value_cansleep(gpio_data->gpiod); 31 if (tmp) { 32 if (gpio_data->desired_brightness) 33 led_set_brightness_nosleep(gpio_data->led, 34 gpio_data->desired_brightness); 35 else 36 led_set_brightness_nosleep(gpio_data->led, LED_FULL); 37 } else { 38 led_set_brightness_nosleep(gpio_data->led, LED_OFF); 39 } 40 41 return IRQ_HANDLED; 42 } 43 44 static ssize_t gpio_trig_brightness_show(struct device *dev, 45 struct device_attribute *attr, char *buf) 46 { 47 struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev); 48 49 return sprintf(buf, "%u\n", gpio_data->desired_brightness); 50 } 51 52 static ssize_t gpio_trig_brightness_store(struct device *dev, 53 struct device_attribute *attr, const char *buf, size_t n) 54 { 55 struct gpio_trig_data *gpio_data = led_trigger_get_drvdata(dev); 56 unsigned desired_brightness; 57 int ret; 58 59 ret = sscanf(buf, "%u", &desired_brightness); 60 if (ret < 1 || desired_brightness > 255) { 61 dev_err(dev, "invalid value\n"); 62 return -EINVAL; 63 } 64 65 gpio_data->desired_brightness = desired_brightness; 66 67 return n; 68 } 69 static DEVICE_ATTR(desired_brightness, 0644, gpio_trig_brightness_show, 70 gpio_trig_brightness_store); 71 72 static struct attribute *gpio_trig_attrs[] = { 73 &dev_attr_desired_brightness.attr, 74 NULL 75 }; 76 ATTRIBUTE_GROUPS(gpio_trig); 77 78 static int gpio_trig_activate(struct led_classdev *led) 79 { 80 struct gpio_trig_data *gpio_data; 81 struct device *dev = led->dev; 82 int ret; 83 84 gpio_data = kzalloc(sizeof(*gpio_data), GFP_KERNEL); 85 if (!gpio_data) 86 return -ENOMEM; 87 88 /* 89 * The generic property "trigger-sources" is followed, 90 * and we hope that this is a GPIO. 91 */ 92 gpio_data->gpiod = gpiod_get_optional(dev, "trigger-sources", GPIOD_IN); 93 if (IS_ERR(gpio_data->gpiod)) { 94 ret = PTR_ERR(gpio_data->gpiod); 95 kfree(gpio_data); 96 return ret; 97 } 98 if (!gpio_data->gpiod) { 99 dev_err(dev, "no valid GPIO for the trigger\n"); 100 kfree(gpio_data); 101 return -EINVAL; 102 } 103 104 gpiod_set_consumer_name(gpio_data->gpiod, "led-trigger"); 105 106 gpio_data->led = led; 107 led_set_trigger_data(led, gpio_data); 108 109 ret = request_threaded_irq(gpiod_to_irq(gpio_data->gpiod), NULL, gpio_trig_irq, 110 IRQF_ONESHOT | IRQF_SHARED | IRQF_TRIGGER_RISING 111 | IRQF_TRIGGER_FALLING, "ledtrig-gpio", led); 112 if (ret) { 113 dev_err(dev, "request_irq failed with error %d\n", ret); 114 gpiod_put(gpio_data->gpiod); 115 kfree(gpio_data); 116 return ret; 117 } 118 119 /* Finally update the LED to initial status */ 120 gpio_trig_irq(0, led); 121 122 return 0; 123 } 124 125 static void gpio_trig_deactivate(struct led_classdev *led) 126 { 127 struct gpio_trig_data *gpio_data = led_get_trigger_data(led); 128 129 free_irq(gpiod_to_irq(gpio_data->gpiod), led); 130 gpiod_put(gpio_data->gpiod); 131 kfree(gpio_data); 132 } 133 134 static struct led_trigger gpio_led_trigger = { 135 .name = "gpio", 136 .activate = gpio_trig_activate, 137 .deactivate = gpio_trig_deactivate, 138 .groups = gpio_trig_groups, 139 }; 140 module_led_trigger(gpio_led_trigger); 141 142 MODULE_AUTHOR("Felipe Balbi <me@felipebalbi.com>"); 143 MODULE_DESCRIPTION("GPIO LED trigger"); 144 MODULE_LICENSE("GPL v2"); 145