led-core.c 8.1 KB
Newer Older
R
Richard Purdie 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * LED Class Core
 *
 * Copyright 2005-2006 Openedhand Ltd.
 *
 * Author: Richard Purdie <rpurdie@openedhand.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */

#include <linux/kernel.h>
15
#include <linux/leds.h>
R
Richard Purdie 已提交
16 17
#include <linux/list.h>
#include <linux/module.h>
18
#include <linux/mutex.h>
19
#include <linux/rwsem.h>
R
Richard Purdie 已提交
20 21
#include "leds.h"

22
DECLARE_RWSEM(leds_list_lock);
23
EXPORT_SYMBOL_GPL(leds_list_lock);
R
Richard Purdie 已提交
24

25
LIST_HEAD(leds_list);
R
Richard Purdie 已提交
26
EXPORT_SYMBOL_GPL(leds_list);
27

28 29 30 31 32 33 34
static void led_timer_function(unsigned long data)
{
	struct led_classdev *led_cdev = (void *)data;
	unsigned long brightness;
	unsigned long delay;

	if (!led_cdev->blink_delay_on || !led_cdev->blink_delay_off) {
35
		led_set_brightness_nosleep(led_cdev, LED_OFF);
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
		return;
	}

	if (led_cdev->flags & LED_BLINK_ONESHOT_STOP) {
		led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP;
		return;
	}

	brightness = led_get_brightness(led_cdev);
	if (!brightness) {
		/* Time to switch the LED on. */
		brightness = led_cdev->blink_brightness;
		delay = led_cdev->blink_delay_on;
	} else {
		/* Store the current brightness value to be able
		 * to restore it when the delay_off period is over.
52 53
		 * Do it only if there is no pending blink brightness
		 * change, to avoid overwriting the new value.
54
		 */
55 56 57 58
		if (!(led_cdev->flags & LED_BLINK_BRIGHTNESS_CHANGE))
			led_cdev->blink_brightness = brightness;
		else
			led_cdev->flags &= ~LED_BLINK_BRIGHTNESS_CHANGE;
59 60 61 62
		brightness = LED_OFF;
		delay = led_cdev->blink_delay_off;
	}

63
	led_set_brightness_nosleep(led_cdev, brightness);
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

	/* Return in next iteration if led is in one-shot mode and we are in
	 * the final blink state so that the led is toggled each delay_on +
	 * delay_off milliseconds in worst case.
	 */
	if (led_cdev->flags & LED_BLINK_ONESHOT) {
		if (led_cdev->flags & LED_BLINK_INVERT) {
			if (brightness)
				led_cdev->flags |= LED_BLINK_ONESHOT_STOP;
		} else {
			if (!brightness)
				led_cdev->flags |= LED_BLINK_ONESHOT_STOP;
		}
	}

	mod_timer(&led_cdev->blink_timer, jiffies + msecs_to_jiffies(delay));
}

static void set_brightness_delayed(struct work_struct *ws)
{
	struct led_classdev *led_cdev =
		container_of(ws, struct led_classdev, set_brightness_work);
86
	int ret = 0;
87

88 89 90 91 92
	if (led_cdev->flags & LED_BLINK_DISABLE) {
		led_cdev->delayed_set_value = LED_OFF;
		led_stop_software_blink(led_cdev);
		led_cdev->flags &= ~LED_BLINK_DISABLE;
	}
93

94 95 96 97 98 99 100
	if (led_cdev->brightness_set)
		led_cdev->brightness_set(led_cdev, led_cdev->delayed_set_value);
	else if (led_cdev->brightness_set_blocking)
		ret = led_cdev->brightness_set_blocking(led_cdev,
						led_cdev->delayed_set_value);
	else
		ret = -ENOTSUPP;
101 102 103 104
	if (ret < 0 &&
	    /* LED HW might have been unplugged, therefore don't warn */
	    !(ret == -ENODEV && (led_cdev->flags & LED_UNREGISTERING) &&
	    (led_cdev->flags & LED_HW_PLUGGABLE)))
105 106
		dev_err(led_cdev->dev,
			"Setting an LED's brightness failed (%d)\n", ret);
107 108
}

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
static void led_set_software_blink(struct led_classdev *led_cdev,
				   unsigned long delay_on,
				   unsigned long delay_off)
{
	int current_brightness;

	current_brightness = led_get_brightness(led_cdev);
	if (current_brightness)
		led_cdev->blink_brightness = current_brightness;
	if (!led_cdev->blink_brightness)
		led_cdev->blink_brightness = led_cdev->max_brightness;

	led_cdev->blink_delay_on = delay_on;
	led_cdev->blink_delay_off = delay_off;

124 125
	/* never on - just set to off */
	if (!delay_on) {
126
		led_set_brightness_nosleep(led_cdev, LED_OFF);
127
		return;
128
	}
129 130 131

	/* never off - just set to brightness */
	if (!delay_off) {
132 133
		led_set_brightness_nosleep(led_cdev,
					   led_cdev->blink_brightness);
134 135 136
		return;
	}

137
	mod_timer(&led_cdev->blink_timer, jiffies + 1);
138 139 140
}


141
static void led_blink_setup(struct led_classdev *led_cdev,
142 143
		     unsigned long *delay_on,
		     unsigned long *delay_off)
144
{
145 146
	if (!(led_cdev->flags & LED_BLINK_ONESHOT) &&
	    led_cdev->blink_set &&
147 148 149 150 151 152 153 154 155
	    !led_cdev->blink_set(led_cdev, delay_on, delay_off))
		return;

	/* blink with 1 Hz as default if nothing specified */
	if (!*delay_on && !*delay_off)
		*delay_on = *delay_off = 500;

	led_set_software_blink(led_cdev, *delay_on, *delay_off);
}
156

157 158 159 160 161 162 163 164 165
void led_init_core(struct led_classdev *led_cdev)
{
	INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed);

	setup_timer(&led_cdev->blink_timer, led_timer_function,
		    (unsigned long)led_cdev);
}
EXPORT_SYMBOL_GPL(led_init_core);

166 167 168 169
void led_blink_set(struct led_classdev *led_cdev,
		   unsigned long *delay_on,
		   unsigned long *delay_off)
{
170
	del_timer_sync(&led_cdev->blink_timer);
171 172 173 174 175 176

	led_cdev->flags &= ~LED_BLINK_ONESHOT;
	led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP;

	led_blink_setup(led_cdev, delay_on, delay_off);
}
177
EXPORT_SYMBOL_GPL(led_blink_set);
178

179 180 181 182 183 184
void led_blink_set_oneshot(struct led_classdev *led_cdev,
			   unsigned long *delay_on,
			   unsigned long *delay_off,
			   int invert)
{
	if ((led_cdev->flags & LED_BLINK_ONESHOT) &&
185
	     timer_pending(&led_cdev->blink_timer))
186 187 188 189 190 191 192 193 194 195 196 197
		return;

	led_cdev->flags |= LED_BLINK_ONESHOT;
	led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP;

	if (invert)
		led_cdev->flags |= LED_BLINK_INVERT;
	else
		led_cdev->flags &= ~LED_BLINK_INVERT;

	led_blink_setup(led_cdev, delay_on, delay_off);
}
198
EXPORT_SYMBOL_GPL(led_blink_set_oneshot);
199

200
void led_stop_software_blink(struct led_classdev *led_cdev)
201
{
202
	del_timer_sync(&led_cdev->blink_timer);
203 204
	led_cdev->blink_delay_on = 0;
	led_cdev->blink_delay_off = 0;
205 206 207 208 209 210
}
EXPORT_SYMBOL_GPL(led_stop_software_blink);

void led_set_brightness(struct led_classdev *led_cdev,
			enum led_brightness brightness)
{
211 212 213 214
	/*
	 * In case blinking is on delay brightness setting
	 * until the next timer tick.
	 */
215
	if (led_cdev->blink_delay_on || led_cdev->blink_delay_off) {
216 217 218 219 220 221 222
		/*
		 * If we need to disable soft blinking delegate this to the
		 * work queue task to avoid problems in case we are called
		 * from hard irq context.
		 */
		if (brightness == LED_OFF) {
			led_cdev->flags |= LED_BLINK_DISABLE;
223
			schedule_work(&led_cdev->set_brightness_work);
224 225 226 227
		} else {
			led_cdev->flags |= LED_BLINK_BRIGHTNESS_CHANGE;
			led_cdev->blink_brightness = brightness;
		}
228 229
		return;
	}
230

231
	led_set_brightness_nosleep(led_cdev, brightness);
232
}
233
EXPORT_SYMBOL_GPL(led_set_brightness);
234

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
void led_set_brightness_nopm(struct led_classdev *led_cdev,
			      enum led_brightness value)
{
	/* Use brightness_set op if available, it is guaranteed not to sleep */
	if (led_cdev->brightness_set) {
		led_cdev->brightness_set(led_cdev, value);
		return;
	}

	/* If brightness setting can sleep, delegate it to a work queue task */
	led_cdev->delayed_set_value = value;
	schedule_work(&led_cdev->set_brightness_work);
}
EXPORT_SYMBOL_GPL(led_set_brightness_nopm);

void led_set_brightness_nosleep(struct led_classdev *led_cdev,
				enum led_brightness value)
{
	led_cdev->brightness = min(value, led_cdev->max_brightness);

	if (led_cdev->flags & LED_SUSPENDED)
		return;

	led_set_brightness_nopm(led_cdev, led_cdev->brightness);
}
EXPORT_SYMBOL_GPL(led_set_brightness_nosleep);

262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
int led_set_brightness_sync(struct led_classdev *led_cdev,
			    enum led_brightness value)
{
	if (led_cdev->blink_delay_on || led_cdev->blink_delay_off)
		return -EBUSY;

	led_cdev->brightness = min(value, led_cdev->max_brightness);

	if (led_cdev->flags & LED_SUSPENDED)
		return 0;

	if (led_cdev->brightness_set_blocking)
		return led_cdev->brightness_set_blocking(led_cdev,
							 led_cdev->brightness);
	return -ENOTSUPP;
}
EXPORT_SYMBOL_GPL(led_set_brightness_sync);

280 281 282 283 284 285 286 287 288 289 290 291 292 293
int led_update_brightness(struct led_classdev *led_cdev)
{
	int ret = 0;

	if (led_cdev->brightness_get) {
		ret = led_cdev->brightness_get(led_cdev);
		if (ret >= 0) {
			led_cdev->brightness = ret;
			return 0;
		}
	}

	return ret;
}
294
EXPORT_SYMBOL_GPL(led_update_brightness);
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312

/* Caller must ensure led_cdev->led_access held */
void led_sysfs_disable(struct led_classdev *led_cdev)
{
	lockdep_assert_held(&led_cdev->led_access);

	led_cdev->flags |= LED_SYSFS_DISABLE;
}
EXPORT_SYMBOL_GPL(led_sysfs_disable);

/* Caller must ensure led_cdev->led_access held */
void led_sysfs_enable(struct led_classdev *led_cdev)
{
	lockdep_assert_held(&led_cdev->led_access);

	led_cdev->flags &= ~LED_SYSFS_DISABLE;
}
EXPORT_SYMBOL_GPL(led_sysfs_enable);