led-class.c 7.4 KB
Newer Older
R
Richard Purdie 已提交
1 2 3 4
/*
 * LED Class Core
 *
 * Copyright (C) 2005 John Lenz <lenz@cs.wisc.edu>
5
 * Copyright (C) 2005-2007 Richard Purdie <rpurdie@openedhand.com>
R
Richard Purdie 已提交
6 7 8 9 10 11
 *
 * 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.
 */

12 13 14
#include <linux/ctype.h>
#include <linux/device.h>
#include <linux/err.h>
R
Richard Purdie 已提交
15
#include <linux/init.h>
16 17
#include <linux/kernel.h>
#include <linux/leds.h>
R
Richard Purdie 已提交
18
#include <linux/list.h>
19 20
#include <linux/module.h>
#include <linux/slab.h>
R
Richard Purdie 已提交
21
#include <linux/spinlock.h>
22
#include <linux/timer.h>
R
Richard Purdie 已提交
23 24 25 26
#include "leds.h"

static struct class *leds_class;

27
static ssize_t brightness_show(struct device *dev,
28
		struct device_attribute *attr, char *buf)
R
Richard Purdie 已提交
29
{
30
	struct led_classdev *led_cdev = dev_get_drvdata(dev);
R
Richard Purdie 已提交
31 32

	/* no lock needed for this */
33
	led_update_brightness(led_cdev);
R
Richard Purdie 已提交
34

S
Sven Wegener 已提交
35
	return sprintf(buf, "%u\n", led_cdev->brightness);
R
Richard Purdie 已提交
36 37
}

38
static ssize_t brightness_store(struct device *dev,
39
		struct device_attribute *attr, const char *buf, size_t size)
R
Richard Purdie 已提交
40
{
41
	struct led_classdev *led_cdev = dev_get_drvdata(dev);
S
Shuah Khan 已提交
42
	unsigned long state;
43 44 45 46 47 48 49 50
	ssize_t ret;

	mutex_lock(&led_cdev->led_access);

	if (led_sysfs_is_disabled(led_cdev)) {
		ret = -EBUSY;
		goto unlock;
	}
R
Richard Purdie 已提交
51

S
Shuah Khan 已提交
52 53
	ret = kstrtoul(buf, 10, &state);
	if (ret)
54
		goto unlock;
55

S
Shuah Khan 已提交
56 57
	if (state == LED_OFF)
		led_trigger_remove(led_cdev);
58
	led_set_brightness(led_cdev, state);
59

60 61 62 63
	ret = size;
unlock:
	mutex_unlock(&led_cdev->led_access);
	return ret;
R
Richard Purdie 已提交
64
}
65
static DEVICE_ATTR_RW(brightness);
R
Richard Purdie 已提交
66

67
static ssize_t max_brightness_show(struct device *dev,
68 69 70 71 72 73
		struct device_attribute *attr, char *buf)
{
	struct led_classdev *led_cdev = dev_get_drvdata(dev);

	return sprintf(buf, "%u\n", led_cdev->max_brightness);
}
74
static DEVICE_ATTR_RO(max_brightness);
75

76
#ifdef CONFIG_LEDS_TRIGGERS
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
static DEVICE_ATTR(trigger, 0644, led_trigger_show, led_trigger_store);
static struct attribute *led_trigger_attrs[] = {
	&dev_attr_trigger.attr,
	NULL,
};
static const struct attribute_group led_trigger_group = {
	.attrs = led_trigger_attrs,
};
#endif

static struct attribute *led_class_attrs[] = {
	&dev_attr_brightness.attr,
	&dev_attr_max_brightness.attr,
	NULL,
};

static const struct attribute_group led_group = {
	.attrs = led_class_attrs,
};

static const struct attribute_group *led_groups[] = {
	&led_group,
#ifdef CONFIG_LEDS_TRIGGERS
	&led_trigger_group,
101
#endif
102
	NULL,
103
};
R
Richard Purdie 已提交
104

105
static void led_timer_function(unsigned long data)
106
{
107
	struct led_classdev *led_cdev = (void *)data;
108 109 110 111
	unsigned long brightness;
	unsigned long delay;

	if (!led_cdev->blink_delay_on || !led_cdev->blink_delay_off) {
112
		led_set_brightness_async(led_cdev, LED_OFF);
113 114 115
		return;
	}

116 117 118 119 120
	if (led_cdev->flags & LED_BLINK_ONESHOT_STOP) {
		led_cdev->flags &= ~LED_BLINK_ONESHOT_STOP;
		return;
	}

121 122 123 124 125 126 127 128 129 130 131 132 133 134
	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.
		 */
		led_cdev->blink_brightness = brightness;
		brightness = LED_OFF;
		delay = led_cdev->blink_delay_off;
	}

135
	led_set_brightness_async(led_cdev, brightness);
136

137 138 139 140 141 142 143 144 145 146 147 148 149 150
	/* 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;
		}
	}

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

154 155 156 157 158 159 160
static void set_brightness_delayed(struct work_struct *ws)
{
	struct led_classdev *led_cdev =
		container_of(ws, struct led_classdev, set_brightness_work);

	led_stop_software_blink(led_cdev);

161
	led_set_brightness_async(led_cdev, led_cdev->delayed_set_value);
162 163
}

R
Richard Purdie 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
/**
 * led_classdev_suspend - suspend an led_classdev.
 * @led_cdev: the led_classdev to suspend.
 */
void led_classdev_suspend(struct led_classdev *led_cdev)
{
	led_cdev->flags |= LED_SUSPENDED;
	led_cdev->brightness_set(led_cdev, 0);
}
EXPORT_SYMBOL_GPL(led_classdev_suspend);

/**
 * led_classdev_resume - resume an led_classdev.
 * @led_cdev: the led_classdev to resume.
 */
void led_classdev_resume(struct led_classdev *led_cdev)
{
	led_cdev->brightness_set(led_cdev, led_cdev->brightness);
	led_cdev->flags &= ~LED_SUSPENDED;
}
EXPORT_SYMBOL_GPL(led_classdev_resume);

186
static int led_suspend(struct device *dev)
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
{
	struct led_classdev *led_cdev = dev_get_drvdata(dev);

	if (led_cdev->flags & LED_CORE_SUSPENDRESUME)
		led_classdev_suspend(led_cdev);

	return 0;
}

static int led_resume(struct device *dev)
{
	struct led_classdev *led_cdev = dev_get_drvdata(dev);

	if (led_cdev->flags & LED_CORE_SUSPENDRESUME)
		led_classdev_resume(led_cdev);

	return 0;
}

206 207 208 209 210
static const struct dev_pm_ops leds_class_dev_pm_ops = {
	.suspend        = led_suspend,
	.resume         = led_resume,
};

R
Richard Purdie 已提交
211 212
/**
 * led_classdev_register - register a new object of led_classdev class.
213
 * @parent: The device to register.
R
Richard Purdie 已提交
214 215 216 217
 * @led_cdev: the led_classdev structure for this device.
 */
int led_classdev_register(struct device *parent, struct led_classdev *led_cdev)
{
218 219 220
	led_cdev->dev = device_create_with_groups(leds_class, parent, 0,
					led_cdev, led_cdev->groups,
					"%s", led_cdev->name);
221
	if (IS_ERR(led_cdev->dev))
222
		return PTR_ERR(led_cdev->dev);
R
Richard Purdie 已提交
223

224 225 226
#ifdef CONFIG_LEDS_TRIGGERS
	init_rwsem(&led_cdev->trigger_lock);
#endif
227
	mutex_init(&led_cdev->led_access);
R
Richard Purdie 已提交
228
	/* add to the list of leds */
229
	down_write(&leds_list_lock);
R
Richard Purdie 已提交
230
	list_add_tail(&led_cdev->node, &leds_list);
231
	up_write(&leds_list_lock);
R
Richard Purdie 已提交
232

233 234 235
	if (!led_cdev->max_brightness)
		led_cdev->max_brightness = LED_FULL;

236 237
	led_cdev->flags |= SET_BRIGHTNESS_ASYNC;

238 239
	led_update_brightness(led_cdev);

240 241
	INIT_WORK(&led_cdev->set_brightness_work, set_brightness_delayed);

242 243 244
	init_timer(&led_cdev->blink_timer);
	led_cdev->blink_timer.function = led_timer_function;
	led_cdev->blink_timer.data = (unsigned long)led_cdev;
245

246
#ifdef CONFIG_LEDS_TRIGGERS
247
	led_trigger_set_default(led_cdev);
248 249
#endif

250
	dev_dbg(parent, "Registered led device: %s\n",
251
			led_cdev->name);
R
Richard Purdie 已提交
252 253 254 255 256 257

	return 0;
}
EXPORT_SYMBOL_GPL(led_classdev_register);

/**
Q
Qinghuang Feng 已提交
258
 * led_classdev_unregister - unregisters a object of led_properties class.
H
Henrik Kretzschmar 已提交
259
 * @led_cdev: the led device to unregister
R
Richard Purdie 已提交
260 261 262
 *
 * Unregisters a previously registered via led_classdev_register object.
 */
263
void led_classdev_unregister(struct led_classdev *led_cdev)
R
Richard Purdie 已提交
264
{
265
#ifdef CONFIG_LEDS_TRIGGERS
266
	down_write(&led_cdev->trigger_lock);
267 268
	if (led_cdev->trigger)
		led_trigger_set(led_cdev, NULL);
269
	up_write(&led_cdev->trigger_lock);
270
#endif
R
Richard Purdie 已提交
271

272 273
	cancel_work_sync(&led_cdev->set_brightness_work);

274
	/* Stop blinking */
275
	led_stop_software_blink(led_cdev);
276
	led_set_brightness(led_cdev, LED_OFF);
277

278
	device_unregister(led_cdev->dev);
R
Richard Purdie 已提交
279

280
	down_write(&leds_list_lock);
R
Richard Purdie 已提交
281
	list_del(&led_cdev->node);
282
	up_write(&leds_list_lock);
283 284

	mutex_destroy(&led_cdev->led_access);
R
Richard Purdie 已提交
285
}
286
EXPORT_SYMBOL_GPL(led_classdev_unregister);
R
Richard Purdie 已提交
287 288 289 290 291 292

static int __init leds_init(void)
{
	leds_class = class_create(THIS_MODULE, "leds");
	if (IS_ERR(leds_class))
		return PTR_ERR(leds_class);
293
	leds_class->pm = &leds_class_dev_pm_ops;
294
	leds_class->dev_groups = led_groups;
R
Richard Purdie 已提交
295 296 297 298 299 300 301 302 303 304 305 306 307 308
	return 0;
}

static void __exit leds_exit(void)
{
	class_destroy(leds_class);
}

subsys_initcall(leds_init);
module_exit(leds_exit);

MODULE_AUTHOR("John Lenz, Richard Purdie");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("LED Class Interface");