Commit | Line | Data |
---|---|---|
14cf11af PM |
1 | /* |
2 | * Miscellaneous procedures for dealing with the PowerMac hardware. | |
3 | * Contains support for the backlight. | |
4 | * | |
5 | * Copyright (C) 2000 Benjamin Herrenschmidt | |
5474c120 | 6 | * Copyright (C) 2006 Michael Hanselmann <linux-kernel@hansmi.ch> |
14cf11af PM |
7 | * |
8 | */ | |
9 | ||
14cf11af | 10 | #include <linux/kernel.h> |
5474c120 MH |
11 | #include <linux/fb.h> |
12 | #include <linux/backlight.h> | |
4b755999 MH |
13 | #include <linux/adb.h> |
14 | #include <linux/pmu.h> | |
15 | #include <asm/atomic.h> | |
14cf11af | 16 | #include <asm/prom.h> |
14cf11af PM |
17 | #include <asm/backlight.h> |
18 | ||
5474c120 | 19 | #define OLD_BACKLIGHT_MAX 15 |
14cf11af | 20 | |
6d5aefb8 DH |
21 | static void pmac_backlight_key_worker(struct work_struct *work); |
22 | static void pmac_backlight_set_legacy_worker(struct work_struct *work); | |
4b755999 | 23 | |
6d5aefb8 DH |
24 | static DECLARE_WORK(pmac_backlight_key_work, pmac_backlight_key_worker); |
25 | static DECLARE_WORK(pmac_backlight_set_legacy_work, pmac_backlight_set_legacy_worker); | |
e01af038 | 26 | |
4b755999 MH |
27 | /* Although these variables are used in interrupt context, it makes no sense to |
28 | * protect them. No user is able to produce enough key events per second and | |
e01af038 MH |
29 | * notice the errors that might happen. |
30 | */ | |
31 | static int pmac_backlight_key_queued; | |
4b755999 MH |
32 | static int pmac_backlight_set_legacy_queued; |
33 | ||
34 | /* The via-pmu code allows the backlight to be grabbed, in which case the | |
35 | * in-kernel control of the brightness needs to be disabled. This should | |
36 | * only be used by really old PowerBooks. | |
37 | */ | |
38 | static atomic_t kernel_backlight_disabled = ATOMIC_INIT(0); | |
e01af038 | 39 | |
28ee086d RP |
40 | /* Protect the pmac_backlight variable below. |
41 | You should hold this lock when using the pmac_backlight pointer to | |
42 | prevent its potential removal. */ | |
5474c120 | 43 | DEFINE_MUTEX(pmac_backlight_mutex); |
14cf11af | 44 | |
5474c120 MH |
45 | /* Main backlight storage |
46 | * | |
599a52d1 | 47 | * Backlight drivers in this variable are required to have the "ops" |
5474c120 MH |
48 | * attribute set and to have an update_status function. |
49 | * | |
50 | * We can only store one backlight here, but since Apple laptops have only one | |
51 | * internal display, it doesn't matter. Other backlight drivers can be used | |
52 | * independently. | |
53 | * | |
5474c120 MH |
54 | */ |
55 | struct backlight_device *pmac_backlight; | |
14cf11af | 56 | |
5474c120 | 57 | int pmac_has_backlight_type(const char *type) |
14cf11af | 58 | { |
5474c120 MH |
59 | struct device_node* bk_node = find_devices("backlight"); |
60 | ||
14cf11af | 61 | if (bk_node) { |
018a3d1d JK |
62 | const char *prop = get_property(bk_node, |
63 | "backlight-control", NULL); | |
5474c120 MH |
64 | if (prop && strncmp(prop, type, strlen(type)) == 0) |
65 | return 1; | |
14cf11af PM |
66 | } |
67 | ||
5474c120 | 68 | return 0; |
14cf11af | 69 | } |
14cf11af | 70 | |
5474c120 | 71 | int pmac_backlight_curve_lookup(struct fb_info *info, int value) |
14cf11af | 72 | { |
5474c120 MH |
73 | int level = (FB_BACKLIGHT_LEVELS - 1); |
74 | ||
75 | if (info && info->bl_dev) { | |
76 | int i, max = 0; | |
77 | ||
78 | /* Look for biggest value */ | |
79 | for (i = 0; i < FB_BACKLIGHT_LEVELS; i++) | |
80 | max = max((int)info->bl_curve[i], max); | |
81 | ||
82 | /* Look for nearest value */ | |
83 | for (i = 0; i < FB_BACKLIGHT_LEVELS; i++) { | |
84 | int diff = abs(info->bl_curve[i] - value); | |
85 | if (diff < max) { | |
86 | max = diff; | |
87 | level = i; | |
88 | } | |
89 | } | |
90 | ||
91 | } | |
92 | ||
93 | return level; | |
14cf11af | 94 | } |
14cf11af | 95 | |
6d5aefb8 | 96 | static void pmac_backlight_key_worker(struct work_struct *work) |
14cf11af | 97 | { |
4b755999 MH |
98 | if (atomic_read(&kernel_backlight_disabled)) |
99 | return; | |
100 | ||
5474c120 MH |
101 | mutex_lock(&pmac_backlight_mutex); |
102 | if (pmac_backlight) { | |
103 | struct backlight_properties *props; | |
104 | int brightness; | |
105 | ||
599a52d1 | 106 | props = &pmac_backlight->props; |
5474c120 MH |
107 | |
108 | brightness = props->brightness + | |
e01af038 MH |
109 | ((pmac_backlight_key_queued?-1:1) * |
110 | (props->max_brightness / 15)); | |
5474c120 MH |
111 | |
112 | if (brightness < 0) | |
113 | brightness = 0; | |
114 | else if (brightness > props->max_brightness) | |
115 | brightness = props->max_brightness; | |
116 | ||
117 | props->brightness = brightness; | |
28ee086d | 118 | backlight_update_status(pmac_backlight); |
5474c120 MH |
119 | } |
120 | mutex_unlock(&pmac_backlight_mutex); | |
14cf11af | 121 | } |
5474c120 | 122 | |
4b755999 | 123 | /* This function is called in interrupt context */ |
e01af038 | 124 | void pmac_backlight_key(int direction) |
14cf11af | 125 | { |
4b755999 MH |
126 | if (atomic_read(&kernel_backlight_disabled)) |
127 | return; | |
128 | ||
e01af038 MH |
129 | /* we can receive multiple interrupts here, but the scheduled work |
130 | * will run only once, with the last value | |
131 | */ | |
132 | pmac_backlight_key_queued = direction; | |
133 | schedule_work(&pmac_backlight_key_work); | |
14cf11af PM |
134 | } |
135 | ||
4b755999 | 136 | static int __pmac_backlight_set_legacy_brightness(int brightness) |
14cf11af | 137 | { |
5474c120 MH |
138 | int error = -ENXIO; |
139 | ||
140 | mutex_lock(&pmac_backlight_mutex); | |
141 | if (pmac_backlight) { | |
142 | struct backlight_properties *props; | |
143 | ||
599a52d1 | 144 | props = &pmac_backlight->props; |
5474c120 | 145 | props->brightness = brightness * |
cab267c6 MH |
146 | (props->max_brightness + 1) / |
147 | (OLD_BACKLIGHT_MAX + 1); | |
148 | ||
149 | if (props->brightness > props->max_brightness) | |
150 | props->brightness = props->max_brightness; | |
151 | else if (props->brightness < 0) | |
152 | props->brightness = 0; | |
153 | ||
28ee086d | 154 | backlight_update_status(pmac_backlight); |
5474c120 MH |
155 | |
156 | error = 0; | |
14cf11af | 157 | } |
5474c120 MH |
158 | mutex_unlock(&pmac_backlight_mutex); |
159 | ||
160 | return error; | |
14cf11af | 161 | } |
5474c120 | 162 | |
6d5aefb8 | 163 | static void pmac_backlight_set_legacy_worker(struct work_struct *work) |
4b755999 MH |
164 | { |
165 | if (atomic_read(&kernel_backlight_disabled)) | |
166 | return; | |
167 | ||
168 | __pmac_backlight_set_legacy_brightness(pmac_backlight_set_legacy_queued); | |
169 | } | |
170 | ||
171 | /* This function is called in interrupt context */ | |
172 | void pmac_backlight_set_legacy_brightness_pmu(int brightness) { | |
173 | if (atomic_read(&kernel_backlight_disabled)) | |
174 | return; | |
175 | ||
176 | pmac_backlight_set_legacy_queued = brightness; | |
177 | schedule_work(&pmac_backlight_set_legacy_work); | |
178 | } | |
179 | ||
180 | int pmac_backlight_set_legacy_brightness(int brightness) | |
181 | { | |
182 | return __pmac_backlight_set_legacy_brightness(brightness); | |
183 | } | |
184 | ||
5474c120 | 185 | int pmac_backlight_get_legacy_brightness() |
14cf11af | 186 | { |
5474c120 | 187 | int result = -ENXIO; |
14cf11af | 188 | |
5474c120 MH |
189 | mutex_lock(&pmac_backlight_mutex); |
190 | if (pmac_backlight) { | |
191 | struct backlight_properties *props; | |
14cf11af | 192 | |
599a52d1 | 193 | props = &pmac_backlight->props; |
cab267c6 | 194 | |
5474c120 | 195 | result = props->brightness * |
cab267c6 MH |
196 | (OLD_BACKLIGHT_MAX + 1) / |
197 | (props->max_brightness + 1); | |
5474c120 MH |
198 | } |
199 | mutex_unlock(&pmac_backlight_mutex); | |
14cf11af | 200 | |
5474c120 | 201 | return result; |
14cf11af | 202 | } |
e01af038 | 203 | |
4b755999 MH |
204 | void pmac_backlight_disable() |
205 | { | |
206 | atomic_inc(&kernel_backlight_disabled); | |
207 | } | |
208 | ||
209 | void pmac_backlight_enable() | |
210 | { | |
211 | atomic_dec(&kernel_backlight_disabled); | |
212 | } | |
213 | ||
e01af038 MH |
214 | EXPORT_SYMBOL_GPL(pmac_backlight); |
215 | EXPORT_SYMBOL_GPL(pmac_backlight_mutex); | |
216 | EXPORT_SYMBOL_GPL(pmac_has_backlight_type); |