blob: 549de7e24cfdf445f27180ab7c776da21af50a94 [file] [log] [blame]
Richard Purdiec72a1d62006-03-31 02:31:04 -08001/*
2 * LED Class Core
3 *
4 * Copyright 2005-2006 Openedhand Ltd.
5 *
6 * Author: Richard Purdie <rpurdie@openedhand.com>
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 */
13
14#include <linux/kernel.h>
Jacek Anaszewski04713302014-08-07 05:10:22 -070015#include <linux/leds.h>
Richard Purdiec72a1d62006-03-31 02:31:04 -080016#include <linux/list.h>
17#include <linux/module.h>
Jacek Anaszewski04713302014-08-07 05:10:22 -070018#include <linux/mutex.h>
Richard Purdie72f8da32007-12-31 23:09:44 +000019#include <linux/rwsem.h>
Richard Purdiec72a1d62006-03-31 02:31:04 -080020#include "leds.h"
21
Richard Purdie72f8da32007-12-31 23:09:44 +000022DECLARE_RWSEM(leds_list_lock);
Richard Purdiec72a1d62006-03-31 02:31:04 -080023EXPORT_SYMBOL_GPL(leds_list_lock);
Németh Márton4d404fd2008-03-09 20:59:57 +000024
25LIST_HEAD(leds_list);
26EXPORT_SYMBOL_GPL(leds_list);
Bryan Wua403d932012-03-23 15:02:14 -070027
Bryan Wua403d932012-03-23 15:02:14 -070028static void led_set_software_blink(struct led_classdev *led_cdev,
29 unsigned long delay_on,
30 unsigned long delay_off)
31{
32 int current_brightness;
33
34 current_brightness = led_get_brightness(led_cdev);
35 if (current_brightness)
36 led_cdev->blink_brightness = current_brightness;
37 if (!led_cdev->blink_brightness)
38 led_cdev->blink_brightness = led_cdev->max_brightness;
39
Bryan Wua403d932012-03-23 15:02:14 -070040 led_cdev->blink_delay_on = delay_on;
41 led_cdev->blink_delay_off = delay_off;
42
Stefan Sørensen8d82fef2014-02-04 00:11:42 -080043 /* never on - just set to off */
44 if (!delay_on) {
Jacek Anaszewski4d71a4a2014-11-14 02:50:18 -080045 led_set_brightness_async(led_cdev, LED_OFF);
Bryan Wua403d932012-03-23 15:02:14 -070046 return;
Stefan Sørensen8d82fef2014-02-04 00:11:42 -080047 }
Bryan Wua403d932012-03-23 15:02:14 -070048
49 /* never off - just set to brightness */
50 if (!delay_off) {
Jacek Anaszewski4d71a4a2014-11-14 02:50:18 -080051 led_set_brightness_async(led_cdev, led_cdev->blink_brightness);
Bryan Wua403d932012-03-23 15:02:14 -070052 return;
53 }
54
Jiri Kosina90673592014-09-02 02:03:12 -070055 mod_timer(&led_cdev->blink_timer, jiffies + 1);
Bryan Wua403d932012-03-23 15:02:14 -070056}
57
58
Bryan Wu20c0e6b2012-06-15 22:15:27 +080059static void led_blink_setup(struct led_classdev *led_cdev,
Fabio Baltieri5bb629c2012-05-27 07:19:22 +080060 unsigned long *delay_on,
61 unsigned long *delay_off)
Bryan Wua403d932012-03-23 15:02:14 -070062{
Fabio Baltieri5bb629c2012-05-27 07:19:22 +080063 if (!(led_cdev->flags & LED_BLINK_ONESHOT) &&
64 led_cdev->blink_set &&
Bryan Wua403d932012-03-23 15:02:14 -070065 !led_cdev->blink_set(led_cdev, delay_on, delay_off))
66 return;
67
68 /* blink with 1 Hz as default if nothing specified */
69 if (!*delay_on && !*delay_off)
70 *delay_on = *delay_off = 500;
71
72 led_set_software_blink(led_cdev, *delay_on, *delay_off);
73}
Fabio Baltieri5bb629c2012-05-27 07:19:22 +080074
75void led_blink_set(struct led_classdev *led_cdev,
76 unsigned long *delay_on,
77 unsigned long *delay_off)
78{
Jiri Kosina90673592014-09-02 02:03:12 -070079 del_timer_sync(&led_cdev->blink_timer);
Fabio Baltieri5bb629c2012-05-27 07:19:22 +080080
81 led_cdev->flags &= ~LED_BLINK_ONESHOT;
82 led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP;
83
84 led_blink_setup(led_cdev, delay_on, delay_off);
85}
Bryan Wua403d932012-03-23 15:02:14 -070086EXPORT_SYMBOL(led_blink_set);
87
Fabio Baltieri5bb629c2012-05-27 07:19:22 +080088void led_blink_set_oneshot(struct led_classdev *led_cdev,
89 unsigned long *delay_on,
90 unsigned long *delay_off,
91 int invert)
92{
93 if ((led_cdev->flags & LED_BLINK_ONESHOT) &&
Jiri Kosina90673592014-09-02 02:03:12 -070094 timer_pending(&led_cdev->blink_timer))
Fabio Baltieri5bb629c2012-05-27 07:19:22 +080095 return;
96
97 led_cdev->flags |= LED_BLINK_ONESHOT;
98 led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP;
99
100 if (invert)
101 led_cdev->flags |= LED_BLINK_INVERT;
102 else
103 led_cdev->flags &= ~LED_BLINK_INVERT;
104
105 led_blink_setup(led_cdev, delay_on, delay_off);
106}
107EXPORT_SYMBOL(led_blink_set_oneshot);
108
Fabio Baltierid23a22a2012-08-15 21:44:34 +0800109void led_stop_software_blink(struct led_classdev *led_cdev)
Bryan Wua403d932012-03-23 15:02:14 -0700110{
Jiri Kosina90673592014-09-02 02:03:12 -0700111 del_timer_sync(&led_cdev->blink_timer);
Fabio Baltieri43786482012-06-07 03:12:34 +0800112 led_cdev->blink_delay_on = 0;
113 led_cdev->blink_delay_off = 0;
Fabio Baltierid23a22a2012-08-15 21:44:34 +0800114}
115EXPORT_SYMBOL_GPL(led_stop_software_blink);
116
117void led_set_brightness(struct led_classdev *led_cdev,
118 enum led_brightness brightness)
119{
Jacek Anaszewski4d71a4a2014-11-14 02:50:18 -0800120 int ret = 0;
121
Stas Sergeev76931ed2015-05-14 08:24:02 -0700122 /* delay brightness if soft-blink is active */
Fabio Baltierid23a22a2012-08-15 21:44:34 +0800123 if (led_cdev->blink_delay_on || led_cdev->blink_delay_off) {
124 led_cdev->delayed_set_value = brightness;
Stas Sergeev76931ed2015-05-14 08:24:02 -0700125 if (brightness == LED_OFF)
126 schedule_work(&led_cdev->set_brightness_work);
Fabio Baltierid23a22a2012-08-15 21:44:34 +0800127 return;
128 }
Fabio Baltieri43786482012-06-07 03:12:34 +0800129
Jacek Anaszewski4d71a4a2014-11-14 02:50:18 -0800130 if (led_cdev->flags & SET_BRIGHTNESS_ASYNC) {
131 led_set_brightness_async(led_cdev, brightness);
132 return;
133 } else if (led_cdev->flags & SET_BRIGHTNESS_SYNC)
134 ret = led_set_brightness_sync(led_cdev, brightness);
135 else
136 ret = -EINVAL;
137
138 if (ret < 0)
139 dev_dbg(led_cdev->dev, "Setting LED brightness failed (%d)\n",
140 ret);
Bryan Wua403d932012-03-23 15:02:14 -0700141}
Shuah Khan19cd67e2012-06-14 04:34:30 +0800142EXPORT_SYMBOL(led_set_brightness);
Jacek Anaszewski3ef7de52014-08-20 06:41:55 -0700143
144int led_update_brightness(struct led_classdev *led_cdev)
145{
146 int ret = 0;
147
148 if (led_cdev->brightness_get) {
149 ret = led_cdev->brightness_get(led_cdev);
150 if (ret >= 0) {
151 led_cdev->brightness = ret;
152 return 0;
153 }
154 }
155
156 return ret;
157}
158EXPORT_SYMBOL(led_update_brightness);
Jacek Anaszewskiacd899e2014-09-22 08:21:04 -0700159
160/* Caller must ensure led_cdev->led_access held */
161void led_sysfs_disable(struct led_classdev *led_cdev)
162{
163 lockdep_assert_held(&led_cdev->led_access);
164
165 led_cdev->flags |= LED_SYSFS_DISABLE;
166}
167EXPORT_SYMBOL_GPL(led_sysfs_disable);
168
169/* Caller must ensure led_cdev->led_access held */
170void led_sysfs_enable(struct led_classdev *led_cdev)
171{
172 lockdep_assert_held(&led_cdev->led_access);
173
174 led_cdev->flags &= ~LED_SYSFS_DISABLE;
175}
176EXPORT_SYMBOL_GPL(led_sysfs_enable);