led-core.c 9.5 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/of.h>
20
#include <linux/rwsem.h>
21
#include <linux/slab.h>
R
Richard Purdie 已提交
22 23
#include "leds.h"

24
DECLARE_RWSEM(leds_list_lock);
25
EXPORT_SYMBOL_GPL(leds_list_lock);
R
Richard Purdie 已提交
26

27
LIST_HEAD(leds_list);
R
Richard Purdie 已提交
28
EXPORT_SYMBOL_GPL(leds_list);
29

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
static int __led_set_brightness(struct led_classdev *led_cdev,
				enum led_brightness value)
{
	if (!led_cdev->brightness_set)
		return -ENOTSUPP;

	led_cdev->brightness_set(led_cdev, value);

	return 0;
}

static int __led_set_brightness_blocking(struct led_classdev *led_cdev,
					 enum led_brightness value)
{
	if (!led_cdev->brightness_set_blocking)
		return -ENOTSUPP;

	return led_cdev->brightness_set_blocking(led_cdev, value);
}

50
static void led_timer_function(struct timer_list *t)
51
{
52
	struct led_classdev *led_cdev = from_timer(led_cdev, t, blink_timer);
53 54 55 56
	unsigned long brightness;
	unsigned long delay;

	if (!led_cdev->blink_delay_on || !led_cdev->blink_delay_off) {
57
		led_set_brightness_nosleep(led_cdev, LED_OFF);
58
		clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
59 60 61
		return;
	}

62 63 64
	if (test_and_clear_bit(LED_BLINK_ONESHOT_STOP,
			       &led_cdev->work_flags)) {
		clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
65 66 67 68 69 70
		return;
	}

	brightness = led_get_brightness(led_cdev);
	if (!brightness) {
		/* Time to switch the LED on. */
71 72 73 74 75
		if (test_and_clear_bit(LED_BLINK_BRIGHTNESS_CHANGE,
					&led_cdev->work_flags))
			brightness = led_cdev->new_blink_brightness;
		else
			brightness = led_cdev->blink_brightness;
76 77 78 79 80
		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.
		 */
81
		led_cdev->blink_brightness = brightness;
82 83 84 85
		brightness = LED_OFF;
		delay = led_cdev->blink_delay_off;
	}

86
	led_set_brightness_nosleep(led_cdev, brightness);
87 88 89 90 91

	/* 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.
	 */
92 93
	if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags)) {
		if (test_bit(LED_BLINK_INVERT, &led_cdev->work_flags)) {
94
			if (brightness)
95 96
				set_bit(LED_BLINK_ONESHOT_STOP,
					&led_cdev->work_flags);
97 98
		} else {
			if (!brightness)
99 100
				set_bit(LED_BLINK_ONESHOT_STOP,
					&led_cdev->work_flags);
101 102 103 104 105 106 107 108 109 110
		}
	}

	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);
111
	int ret = 0;
112

113
	if (test_and_clear_bit(LED_BLINK_DISABLE, &led_cdev->work_flags)) {
114 115 116
		led_cdev->delayed_set_value = LED_OFF;
		led_stop_software_blink(led_cdev);
	}
117

118 119 120 121
	ret = __led_set_brightness(led_cdev, led_cdev->delayed_set_value);
	if (ret == -ENOTSUPP)
		ret = __led_set_brightness_blocking(led_cdev,
					led_cdev->delayed_set_value);
122 123 124 125
	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)))
126 127
		dev_err(led_cdev->dev,
			"Setting an LED's brightness failed (%d)\n", ret);
128 129
}

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
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;

145 146
	/* never on - just set to off */
	if (!delay_on) {
147
		led_set_brightness_nosleep(led_cdev, LED_OFF);
148
		return;
149
	}
150 151 152

	/* never off - just set to brightness */
	if (!delay_off) {
153 154
		led_set_brightness_nosleep(led_cdev,
					   led_cdev->blink_brightness);
155 156 157
		return;
	}

158
	set_bit(LED_BLINK_SW, &led_cdev->work_flags);
159
	mod_timer(&led_cdev->blink_timer, jiffies + 1);
160 161 162
}


163
static void led_blink_setup(struct led_classdev *led_cdev,
164 165
		     unsigned long *delay_on,
		     unsigned long *delay_off)
166
{
P
Pavel Machek 已提交
167 168 169 170 171
	/*
	 * If "set brightness to 0" is pending in workqueue, we don't
	 * want that to be reordered after blink_set()
	 */
	flush_work(&led_cdev->set_brightness_work);
172
	if (!test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) &&
173
	    led_cdev->blink_set &&
174 175 176 177 178 179 180 181 182
	    !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);
}
183

184 185 186 187
void led_init_core(struct led_classdev *led_cdev)
{
	INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed);

188
	timer_setup(&led_cdev->blink_timer, led_timer_function, 0);
189 190 191
}
EXPORT_SYMBOL_GPL(led_init_core);

192 193 194 195
void led_blink_set(struct led_classdev *led_cdev,
		   unsigned long *delay_on,
		   unsigned long *delay_off)
{
196
	del_timer_sync(&led_cdev->blink_timer);
197

198
	clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
199 200
	clear_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags);
	clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags);
201 202 203

	led_blink_setup(led_cdev, delay_on, delay_off);
}
204
EXPORT_SYMBOL_GPL(led_blink_set);
205

206 207 208 209 210
void led_blink_set_oneshot(struct led_classdev *led_cdev,
			   unsigned long *delay_on,
			   unsigned long *delay_off,
			   int invert)
{
211
	if (test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) &&
212
	     timer_pending(&led_cdev->blink_timer))
213 214
		return;

215 216
	set_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags);
	clear_bit(LED_BLINK_ONESHOT_STOP, &led_cdev->work_flags);
217 218

	if (invert)
219
		set_bit(LED_BLINK_INVERT, &led_cdev->work_flags);
220
	else
221
		clear_bit(LED_BLINK_INVERT, &led_cdev->work_flags);
222 223 224

	led_blink_setup(led_cdev, delay_on, delay_off);
}
225
EXPORT_SYMBOL_GPL(led_blink_set_oneshot);
226

227
void led_stop_software_blink(struct led_classdev *led_cdev)
228
{
229
	del_timer_sync(&led_cdev->blink_timer);
230 231
	led_cdev->blink_delay_on = 0;
	led_cdev->blink_delay_off = 0;
232
	clear_bit(LED_BLINK_SW, &led_cdev->work_flags);
233 234 235 236 237 238
}
EXPORT_SYMBOL_GPL(led_stop_software_blink);

void led_set_brightness(struct led_classdev *led_cdev,
			enum led_brightness brightness)
{
239
	/*
240
	 * If software blink is active, delay brightness setting
241 242
	 * until the next timer tick.
	 */
243
	if (test_bit(LED_BLINK_SW, &led_cdev->work_flags)) {
244 245 246 247 248 249
		/*
		 * 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) {
250
			set_bit(LED_BLINK_DISABLE, &led_cdev->work_flags);
251
			schedule_work(&led_cdev->set_brightness_work);
252
		} else {
253 254
			set_bit(LED_BLINK_BRIGHTNESS_CHANGE,
				&led_cdev->work_flags);
255
			led_cdev->new_blink_brightness = brightness;
256
		}
257 258
		return;
	}
259

260
	led_set_brightness_nosleep(led_cdev, brightness);
261
}
262
EXPORT_SYMBOL_GPL(led_set_brightness);
263

264 265 266 267
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 */
268
	if (!__led_set_brightness(led_cdev, value))
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
		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);

289 290 291 292 293 294 295 296 297 298 299
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;

300
	return __led_set_brightness_blocking(led_cdev, led_cdev->brightness);
301 302 303
}
EXPORT_SYMBOL_GPL(led_set_brightness_sync);

304 305 306 307 308 309 310 311 312 313 314 315 316 317
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;
}
318
EXPORT_SYMBOL_GPL(led_update_brightness);
319

320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
u32 *led_get_default_pattern(struct led_classdev *led_cdev, unsigned int *size)
{
	struct device_node *np = dev_of_node(led_cdev->dev);
	u32 *pattern;
	int count;

	if (!np)
		return NULL;

	count = of_property_count_u32_elems(np, "led-pattern");
	if (count < 0)
		return NULL;

	pattern = kcalloc(count, sizeof(*pattern), GFP_KERNEL);
	if (!pattern)
		return NULL;

	if (of_property_read_u32_array(np, "led-pattern", pattern, count)) {
		kfree(pattern);
		return NULL;
	}

	*size = count;

	return pattern;
}
EXPORT_SYMBOL_GPL(led_get_default_pattern);

348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
/* 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);