blob: 52bdd00180d8575244a5b045f07db9b0dfecfde6 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * LED Class Core
3 *
4 * Copyright (C) 2005 John Lenz <lenz@cs.wisc.edu>
5 * Copyright (C) 2005-2007 Richard Purdie <rpurdie@openedhand.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/module.h>
13#include <linux/kernel.h>
14#include <linux/init.h>
15#include <linux/list.h>
16#include <linux/spinlock.h>
17#include <linux/device.h>
18#include <linux/timer.h>
19#include <linux/err.h>
20#include <linux/ctype.h>
21#include <linux/leds.h>
22#include "leds.h"
23
24static struct class *leds_class;
25
26static void led_update_brightness(struct led_classdev *led_cdev)
27{
28 if (led_cdev->brightness_get)
29 led_cdev->brightness = led_cdev->brightness_get(led_cdev);
30}
31
32static ssize_t led_brightness_show(struct device *dev,
33 struct device_attribute *attr, char *buf)
34{
35 struct led_classdev *led_cdev = dev_get_drvdata(dev);
36
37 if(NULL==led_cdev)
38 return -EINVAL;
39
40 /* no lock needed for this */
41 led_update_brightness(led_cdev);
42
43 return sprintf(buf, "%u\n", led_cdev->brightness);
44}
45
46static ssize_t led_brightness_store(struct device *dev,
47 struct device_attribute *attr, const char *buf, size_t size)
48{
49 struct led_classdev *led_cdev = dev_get_drvdata(dev);
50 ssize_t ret = -EINVAL;
51 char *after;
52 unsigned long state = simple_strtoul(buf, &after, 10);
53 size_t count = after - buf;
54
55 if(NULL==led_cdev)
56 return -EINVAL;
57
58 if (isspace(*after))
59 count++;
60
61 if (count == size) {
62 ret = count;
63
64 if (state == LED_OFF)
65 led_trigger_remove(led_cdev);
66 led_set_brightness(led_cdev, state);
67 }
68
69 return ret;
70}
71
72static ssize_t led_max_brightness_show(struct device *dev,
73 struct device_attribute *attr, char *buf)
74{
75 struct led_classdev *led_cdev = dev_get_drvdata(dev);
76
77 if(NULL==led_cdev)
78 return -EINVAL;
79
80 return sprintf(buf, "%u\n", led_cdev->max_brightness);
81}
82
83static struct device_attribute led_class_attrs[] = {
84 __ATTR(brightness, 0644, led_brightness_show, led_brightness_store),
85 __ATTR(max_brightness, 0444, led_max_brightness_show, NULL),
86#ifdef CONFIG_LEDS_TRIGGERS
87 __ATTR(trigger, 0644, led_trigger_show, led_trigger_store),
88#endif
89 __ATTR_NULL,
90};
91
92static void led_timer_function(unsigned long data)
93{
94 struct led_classdev *led_cdev = (void *)data;
95 unsigned long brightness;
96 unsigned long delay;
97
98 if (!led_cdev->blink_delay_on || !led_cdev->blink_delay_off) {
99 led_set_brightness(led_cdev, LED_OFF);
100 return;
101 }
102
103 brightness = led_get_brightness(led_cdev);
104 if (!brightness) {
105 /* Time to switch the LED on. */
106 brightness = led_cdev->blink_brightness;
107 delay = led_cdev->blink_delay_on;
108 } else {
109 /* Store the current brightness value to be able
110 * to restore it when the delay_off period is over.
111 */
112 led_cdev->blink_brightness = brightness;
113 brightness = LED_OFF;
114 delay = led_cdev->blink_delay_off;
115 }
116
117 led_set_brightness(led_cdev, brightness);
118
119 mod_timer(&led_cdev->blink_timer, jiffies + msecs_to_jiffies(delay));
120}
121
122/**
123 * led_classdev_suspend - suspend an led_classdev.
124 * @led_cdev: the led_classdev to suspend.
125 */
126void led_classdev_suspend(struct led_classdev *led_cdev)
127{
128 led_cdev->flags |= LED_SUSPENDED;
129 led_cdev->brightness_set(led_cdev, 0);
130}
131EXPORT_SYMBOL_GPL(led_classdev_suspend);
132
133/**
134 * led_classdev_resume - resume an led_classdev.
135 * @led_cdev: the led_classdev to resume.
136 */
137void led_classdev_resume(struct led_classdev *led_cdev)
138{
139 led_cdev->brightness_set(led_cdev, led_cdev->brightness);
140 led_cdev->flags &= ~LED_SUSPENDED;
141}
142EXPORT_SYMBOL_GPL(led_classdev_resume);
143
144static int led_suspend(struct device *dev, pm_message_t state)
145{
146 struct led_classdev *led_cdev = dev_get_drvdata(dev);
147
148 if(NULL==led_cdev)
149 return 0;
150 if (led_cdev->flags & LED_CORE_SUSPENDRESUME)
151 led_classdev_suspend(led_cdev);
152
153 return 0;
154}
155
156static int led_resume(struct device *dev)
157{
158 struct led_classdev *led_cdev = dev_get_drvdata(dev);
159
160 if(NULL==led_cdev)
161 return 0;
162 if (led_cdev->flags & LED_CORE_SUSPENDRESUME)
163 led_classdev_resume(led_cdev);
164
165 return 0;
166}
167
168/**
169 * led_classdev_register - register a new object of led_classdev class.
170 * @parent: The device to register.
171 * @led_cdev: the led_classdev structure for this device.
172 */
173int led_classdev_register(struct device *parent, struct led_classdev *led_cdev)
174{
175 led_cdev->dev = device_create(leds_class, parent, 0, led_cdev,
176 "%s", led_cdev->name);
177 if (IS_ERR(led_cdev->dev))
178 return PTR_ERR(led_cdev->dev);
179
180#ifdef CONFIG_LEDS_TRIGGERS
181 init_rwsem(&led_cdev->trigger_lock);
182#endif
183 /* add to the list of leds */
184 down_write(&leds_list_lock);
185 list_add_tail(&led_cdev->node, &leds_list);
186 up_write(&leds_list_lock);
187
188 if (!led_cdev->max_brightness)
189 led_cdev->max_brightness = LED_FULL;
190
191 led_update_brightness(led_cdev);
192
193 init_timer(&led_cdev->blink_timer);
194 led_cdev->blink_timer.function = led_timer_function;
195 led_cdev->blink_timer.data = (unsigned long)led_cdev;
196
197#ifdef CONFIG_LEDS_TRIGGERS
198 led_trigger_set_default(led_cdev);
199#endif
200
201 printk(KERN_DEBUG "Registered led device: %s\n",
202 led_cdev->name);
203
204 return 0;
205}
206EXPORT_SYMBOL_GPL(led_classdev_register);
207
208/**
209 * led_classdev_unregister - unregisters a object of led_properties class.
210 * @led_cdev: the led device to unregister
211 *
212 * Unregisters a previously registered via led_classdev_register object.
213 */
214void led_classdev_unregister(struct led_classdev *led_cdev)
215{
216#ifdef CONFIG_LEDS_TRIGGERS
217 down_write(&led_cdev->trigger_lock);
218 if (led_cdev->trigger)
219 led_trigger_set(led_cdev, NULL);
220 up_write(&led_cdev->trigger_lock);
221#endif
222
223 /* Stop blinking */
224 led_brightness_set(led_cdev, LED_OFF);
225
226 device_unregister(led_cdev->dev);
227
228 down_write(&leds_list_lock);
229 list_del(&led_cdev->node);
230 up_write(&leds_list_lock);
231}
232EXPORT_SYMBOL_GPL(led_classdev_unregister);
233
234static int __init leds_init(void)
235{
236 leds_class = class_create(THIS_MODULE, "leds");
237 if (IS_ERR(leds_class))
238 return PTR_ERR(leds_class);
239 leds_class->suspend = led_suspend;
240 leds_class->resume = led_resume;
241 leds_class->dev_attrs = led_class_attrs;
242 return 0;
243}
244
245static void __exit leds_exit(void)
246{
247 class_destroy(leds_class);
248}
249
250subsys_initcall(leds_init);
251module_exit(leds_exit);
252
253MODULE_AUTHOR("John Lenz, Richard Purdie");
254MODULE_LICENSE("GPL");
255MODULE_DESCRIPTION("LED Class Interface");