aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/macintosh/via-pmu-led.c
blob: af8375ed0f5ebdf72e150fac3a45e70e573c6919 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * via-pmu LED class device
 *
 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
 * NON INFRINGEMENT.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 */
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/leds.h>
#include <linux/adb.h>
#include <linux/pmu.h>
#include <asm/prom.h>

static spinlock_t pmu_blink_lock;
static struct adb_request pmu_blink_req;
/* -1: no change, 0: request off, 1: request on */
static int requested_change;
static int sleeping;

static void pmu_req_done(struct adb_request * req)
{
	unsigned long flags;

	spin_lock_irqsave(&pmu_blink_lock, flags);
	/* if someone requested a change in the meantime
	 * (we only see the last one which is fine)
	 * then apply it now */
	if (requested_change != -1 && !sleeping)
		pmu_request(&pmu_blink_req, NULL, 4, 0xee, 4, 0, requested_change);
	/* reset requested change */
	requested_change = -1;
	spin_unlock_irqrestore(&pmu_blink_lock, flags);
}

static void pmu_led_set(struct led_classdev *led_cdev,
			enum led_brightness brightness)
{
	unsigned long flags;

	spin_lock_irqsave(&pmu_blink_lock, flags);
	switch (brightness) {
	case LED_OFF:
		requested_change = 0;
		break;
	case LED_FULL:
		requested_change = 1;
		break;
	default:
		goto out;
		break;
	}
	/* if request isn't done, then don't do anything */
	if (pmu_blink_req.complete && !sleeping)
		pmu_request(&pmu_blink_req, NULL, 4, 0xee, 4, 0, requested_change);
 out:
 	spin_unlock_irqrestore(&pmu_blink_lock, flags);
}

static struct led_classdev pmu_led = {
	.name = "pmu-front-led",
#ifdef CONFIG_BLK_DEV_IDE_PMAC_BLINK
	.default_trigger = "ide-disk",
#endif
	.brightness_set = pmu_led_set,
};

#ifdef CONFIG_PM
static int pmu_led_sleep_call(struct pmu_sleep_notifier *self, int when)
{
	unsigned long flags;

	spin_lock_irqsave(&pmu_blink_lock, flags);

	switch (when) {
	case PBOOK_SLEEP_REQUEST:
		sleeping = 1;
		break;
	case PBOOK_WAKE:
		sleeping = 0;
		break;
	default:
		/* do nothing */
		break;
	}
	spin_unlock_irqrestore(&pmu_blink_lock, flags);

	return PBOOK_SLEEP_OK;
}

static struct pmu_sleep_notifier via_pmu_led_sleep_notif = {
	.notifier_call = pmu_led_sleep_call,
};
#endif

static int __init via_pmu_led_init(void)
{
	struct device_node *dt;
	const char *model;

	/* only do this on keylargo based models */
	if (pmu_get_model() != PMU_KEYLARGO_BASED)
		return -ENODEV;

	dt = of_find_node_by_path("/");
	if (dt == NULL)
		return -ENODEV;
	model = (const char *)get_property(dt, "model", NULL);
	if (model == NULL)
		return -ENODEV;
	if (strncmp(model, "PowerBook", strlen("PowerBook")) != 0 &&
	    strncmp(model, "iBook", strlen("iBook")) != 0) {
		of_node_put(dt);
		/* ignore */
		return -ENODEV;
	}
	of_node_put(dt);

	spin_lock_init(&pmu_blink_lock);
	/* no outstanding req */
	pmu_blink_req.complete = 1;
	pmu_blink_req.done = pmu_req_done;
#ifdef CONFIG_PM
	pmu_register_sleep_notifier(&via_pmu_led_sleep_notif);
#endif
	return led_classdev_register(NULL, &pmu_led);
}

late_initcall(via_pmu_led_init);