leds.c 2.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 * DIGITAL Shark LED control routines.
 *
4 5
 * Driver for the 3 user LEDs found on the Shark
 * Based on Versatile and RealView machine LED code
L
Linus Torvalds 已提交
6
 *
7 8
 * License terms: GNU General Public License (GPL) version 2
 * Author: Bryan Wu <bryan.wu@canonical.com>
L
Linus Torvalds 已提交
9 10 11
 */
#include <linux/kernel.h>
#include <linux/init.h>
12
#include <linux/io.h>
13 14 15
#include <linux/ioport.h>
#include <linux/slab.h>
#include <linux/leds.h>
L
Linus Torvalds 已提交
16

17
#include <asm/mach-types.h>
L
Linus Torvalds 已提交
18

19 20 21 22 23
#if defined(CONFIG_NEW_LEDS) && defined(CONFIG_LEDS_CLASS)
struct shark_led {
	struct led_classdev cdev;
	u8 mask;
};
24

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/*
 * The triggers lines up below will only be used if the
 * LED triggers are compiled in.
 */
static const struct {
	const char *name;
	const char *trigger;
} shark_leds[] = {
	{ "shark:amber0", "default-on", },	/* Bit 5 */
	{ "shark:green", "heartbeat", },	/* Bit 6 */
	{ "shark:amber1", "cpu0" },		/* Bit 7 */
};

static u16 led_reg_read(void)
{
	outw(0x09, 0x24);
	return inw(0x26);
}
43

44 45 46 47 48
static void led_reg_write(u16 value)
{
	outw(0x09, 0x24);
	outw(value, 0x26);
}
L
Linus Torvalds 已提交
49

50 51 52 53 54 55
static void shark_led_set(struct led_classdev *cdev,
			      enum led_brightness b)
{
	struct shark_led *led = container_of(cdev,
						 struct shark_led, cdev);
	u16 reg = led_reg_read();
L
Linus Torvalds 已提交
56

57 58 59 60
	if (b != LED_OFF)
		reg |= led->mask;
	else
		reg &= ~led->mask;
L
Linus Torvalds 已提交
61

62
	led_reg_write(reg);
L
Linus Torvalds 已提交
63 64
}

65
static enum led_brightness shark_led_get(struct led_classdev *cdev)
L
Linus Torvalds 已提交
66
{
67 68 69
	struct shark_led *led = container_of(cdev,
						 struct shark_led, cdev);
	u16 reg = led_reg_read();
L
Linus Torvalds 已提交
70

71 72
	return (reg & led->mask) ? LED_FULL : LED_OFF;
}
L
Linus Torvalds 已提交
73

74 75 76 77
static int __init shark_leds_init(void)
{
	int i;
	u16 reg;
L
Linus Torvalds 已提交
78

79 80
	if (!machine_is_shark())
		return -ENODEV;
L
Linus Torvalds 已提交
81

82 83
	for (i = 0; i < ARRAY_SIZE(shark_leds); i++) {
		struct shark_led *led;
L
Linus Torvalds 已提交
84

85 86 87
		led = kzalloc(sizeof(*led), GFP_KERNEL);
		if (!led)
			break;
L
Linus Torvalds 已提交
88

89 90 91 92
		led->cdev.name = shark_leds[i].name;
		led->cdev.brightness_set = shark_led_set;
		led->cdev.brightness_get = shark_led_get;
		led->cdev.default_trigger = shark_leds[i].trigger;
L
Linus Torvalds 已提交
93

94 95
		/* Count in 5 bits offset */
		led->mask = BIT(i + 5);
L
Linus Torvalds 已提交
96

97 98 99 100 101
		if (led_classdev_register(NULL, &led->cdev) < 0) {
			kfree(led);
			break;
		}
	}
L
Linus Torvalds 已提交
102 103

	/* Make LEDs independent of power-state */
104 105 106 107 108
	request_region(0x24, 4, "led_reg");
	reg = led_reg_read();
	reg |= 1 << 10;
	led_reg_write(reg);

L
Linus Torvalds 已提交
109 110 111
	return 0;
}

112 113 114 115 116 117
/*
 * Since we may have triggers on any subsystem, defer registration
 * until after subsystem_init.
 */
fs_initcall(shark_leds_init);
#endif