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 desired_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 sysfs_emit(buf, "%u\n", gpio_data->desired_brightness); 50 } 51 52 static ssize_t desired_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 u8 desired_brightness; 57 int ret; 58 59 ret = kstrtou8(buf, 10, &desired_brightness); 60 if (ret) 61 return ret; 62 63 gpio_data->desired_brightness = desired_brightness; 64 65 return n; 66 } 67 static DEVICE_ATTR_RW(desired_brightness); 68 69 static struct attribute *gpio_trig_attrs[] = { 70 &dev_attr_desired_brightness.attr, 71 NULL 72 }; 73 ATTRIBUTE_GROUPS(gpio_trig); 74 75 static int gpio_trig_activate(struct led_classdev *led) 76 { 77 struct gpio_trig_data *gpio_data; 78 struct device *dev = led->dev; 79 int ret; 80 81 gpio_data = kzalloc_obj(*gpio_data); 82 if (!gpio_data) 83 return -ENOMEM; 84 85 /* 86 * The generic property "trigger-sources" is followed, 87 * and we hope that this is a GPIO. 88 */ 89 gpio_data->gpiod = gpiod_get_optional(dev, "trigger-sources", 90 GPIOD_IN | GPIOD_FLAGS_BIT_NONEXCLUSIVE); 91 if (IS_ERR(gpio_data->gpiod)) { 92 ret = PTR_ERR(gpio_data->gpiod); 93 kfree(gpio_data); 94 return ret; 95 } 96 if (!gpio_data->gpiod) { 97 dev_err(dev, "no valid GPIO for the trigger\n"); 98 kfree(gpio_data); 99 return -EINVAL; 100 } 101 102 gpiod_set_consumer_name(gpio_data->gpiod, "led-trigger"); 103 104 gpio_data->led = led; 105 led_set_trigger_data(led, gpio_data); 106 107 ret = request_threaded_irq(gpiod_to_irq(gpio_data->gpiod), NULL, gpio_trig_irq, 108 IRQF_ONESHOT | IRQF_SHARED | IRQF_TRIGGER_RISING 109 | IRQF_TRIGGER_FALLING, "ledtrig-gpio", led); 110 if (ret) { 111 dev_err(dev, "request_irq failed with error %d\n", ret); 112 gpiod_put(gpio_data->gpiod); 113 kfree(gpio_data); 114 return ret; 115 } 116 117 /* Finally update the LED to initial status */ 118 gpio_trig_irq(0, led); 119 120 return 0; 121 } 122 123 static void gpio_trig_deactivate(struct led_classdev *led) 124 { 125 struct gpio_trig_data *gpio_data = led_get_trigger_data(led); 126 127 free_irq(gpiod_to_irq(gpio_data->gpiod), led); 128 gpiod_put(gpio_data->gpiod); 129 kfree(gpio_data); 130 } 131 132 static struct led_trigger gpio_led_trigger = { 133 .name = "gpio", 134 .activate = gpio_trig_activate, 135 .deactivate = gpio_trig_deactivate, 136 .groups = gpio_trig_groups, 137 }; 138 module_led_trigger(gpio_led_trigger); 139 140 MODULE_AUTHOR("Felipe Balbi <me@felipebalbi.com>"); 141 MODULE_DESCRIPTION("GPIO LED trigger"); 142 MODULE_LICENSE("GPL v2"); 143