led-core.c 4.2 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 35 36 37 38 39 40 41 42

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;

43 44
	/* never on - just set to off */
	if (!delay_on) {
45
		led_set_brightness_async(led_cdev, LED_OFF);
46
		return;
47
	}
48 49 50

	/* never off - just set to brightness */
	if (!delay_off) {
51
		led_set_brightness_async(led_cdev, led_cdev->blink_brightness);
52 53 54
		return;
	}

55
	mod_timer(&led_cdev->blink_timer, jiffies + 1);
56 57 58
}


59
static void led_blink_setup(struct led_classdev *led_cdev,
60 61
		     unsigned long *delay_on,
		     unsigned long *delay_off)
62
{
63 64
	if (!(led_cdev->flags & LED_BLINK_ONESHOT) &&
	    led_cdev->blink_set &&
65 66 67 68 69 70 71 72 73
	    !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);
}
74 75 76 77 78

void led_blink_set(struct led_classdev *led_cdev,
		   unsigned long *delay_on,
		   unsigned long *delay_off)
{
79
	del_timer_sync(&led_cdev->blink_timer);
80 81 82 83 84 85

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

	led_blink_setup(led_cdev, delay_on, delay_off);
}
86 87
EXPORT_SYMBOL(led_blink_set);

88 89 90 91 92 93
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) &&
94
	     timer_pending(&led_cdev->blink_timer))
95 96 97 98 99 100 101 102 103 104 105 106 107 108
		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);
}
EXPORT_SYMBOL(led_blink_set_oneshot);

109
void led_stop_software_blink(struct led_classdev *led_cdev)
110
{
111
	del_timer_sync(&led_cdev->blink_timer);
112 113
	led_cdev->blink_delay_on = 0;
	led_cdev->blink_delay_off = 0;
114 115 116 117 118 119
}
EXPORT_SYMBOL_GPL(led_stop_software_blink);

void led_set_brightness(struct led_classdev *led_cdev,
			enum led_brightness brightness)
{
120 121
	int ret = 0;

122
	/* delay brightness if soft-blink is active */
123 124
	if (led_cdev->blink_delay_on || led_cdev->blink_delay_off) {
		led_cdev->delayed_set_value = brightness;
125 126
		if (brightness == LED_OFF)
			schedule_work(&led_cdev->set_brightness_work);
127 128
		return;
	}
129

130 131 132 133 134 135 136 137 138 139 140
	if (led_cdev->flags & SET_BRIGHTNESS_ASYNC) {
		led_set_brightness_async(led_cdev, brightness);
		return;
	} else if (led_cdev->flags & SET_BRIGHTNESS_SYNC)
		ret = led_set_brightness_sync(led_cdev, brightness);
	else
		ret = -EINVAL;

	if (ret < 0)
		dev_dbg(led_cdev->dev, "Setting LED brightness failed (%d)\n",
			ret);
141
}
142
EXPORT_SYMBOL(led_set_brightness);
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

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;
}
EXPORT_SYMBOL(led_update_brightness);
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

/* 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);