WorkQueue: Fix up arch-specific work items where possible
[deliverable/linux.git] / arch / powerpc / platforms / powermac / backlight.c
CommitLineData
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
21static void pmac_backlight_key_worker(struct work_struct *work);
22static void pmac_backlight_set_legacy_worker(struct work_struct *work);
4b755999 23
6d5aefb8
DH
24static DECLARE_WORK(pmac_backlight_key_work, pmac_backlight_key_worker);
25static 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 */
31static int pmac_backlight_key_queued;
4b755999
MH
32static 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 */
38static atomic_t kernel_backlight_disabled = ATOMIC_INIT(0);
e01af038 39
5474c120
MH
40/* Protect the pmac_backlight variable */
41DEFINE_MUTEX(pmac_backlight_mutex);
14cf11af 42
5474c120
MH
43/* Main backlight storage
44 *
45 * Backlight drivers in this variable are required to have the "props"
46 * attribute set and to have an update_status function.
47 *
48 * We can only store one backlight here, but since Apple laptops have only one
49 * internal display, it doesn't matter. Other backlight drivers can be used
50 * independently.
51 *
52 * Lock ordering:
53 * pmac_backlight_mutex (global, main backlight)
54 * pmac_backlight->sem (backlight class)
55 */
56struct backlight_device *pmac_backlight;
14cf11af 57
5474c120 58int pmac_has_backlight_type(const char *type)
14cf11af 59{
5474c120
MH
60 struct device_node* bk_node = find_devices("backlight");
61
14cf11af 62 if (bk_node) {
018a3d1d
JK
63 const char *prop = get_property(bk_node,
64 "backlight-control", NULL);
5474c120
MH
65 if (prop && strncmp(prop, type, strlen(type)) == 0)
66 return 1;
14cf11af
PM
67 }
68
5474c120 69 return 0;
14cf11af 70}
14cf11af 71
5474c120 72int pmac_backlight_curve_lookup(struct fb_info *info, int value)
14cf11af 73{
5474c120
MH
74 int level = (FB_BACKLIGHT_LEVELS - 1);
75
76 if (info && info->bl_dev) {
77 int i, max = 0;
78
79 /* Look for biggest value */
80 for (i = 0; i < FB_BACKLIGHT_LEVELS; i++)
81 max = max((int)info->bl_curve[i], max);
82
83 /* Look for nearest value */
84 for (i = 0; i < FB_BACKLIGHT_LEVELS; i++) {
85 int diff = abs(info->bl_curve[i] - value);
86 if (diff < max) {
87 max = diff;
88 level = i;
89 }
90 }
91
92 }
93
94 return level;
14cf11af 95}
14cf11af 96
6d5aefb8 97static void pmac_backlight_key_worker(struct work_struct *work)
14cf11af 98{
4b755999
MH
99 if (atomic_read(&kernel_backlight_disabled))
100 return;
101
5474c120
MH
102 mutex_lock(&pmac_backlight_mutex);
103 if (pmac_backlight) {
104 struct backlight_properties *props;
105 int brightness;
106
107 down(&pmac_backlight->sem);
108 props = pmac_backlight->props;
109
110 brightness = props->brightness +
e01af038
MH
111 ((pmac_backlight_key_queued?-1:1) *
112 (props->max_brightness / 15));
5474c120
MH
113
114 if (brightness < 0)
115 brightness = 0;
116 else if (brightness > props->max_brightness)
117 brightness = props->max_brightness;
118
119 props->brightness = brightness;
120 props->update_status(pmac_backlight);
121
122 up(&pmac_backlight->sem);
123 }
124 mutex_unlock(&pmac_backlight_mutex);
14cf11af 125}
5474c120 126
4b755999 127/* This function is called in interrupt context */
e01af038 128void pmac_backlight_key(int direction)
14cf11af 129{
4b755999
MH
130 if (atomic_read(&kernel_backlight_disabled))
131 return;
132
e01af038
MH
133 /* we can receive multiple interrupts here, but the scheduled work
134 * will run only once, with the last value
135 */
136 pmac_backlight_key_queued = direction;
137 schedule_work(&pmac_backlight_key_work);
14cf11af
PM
138}
139
4b755999 140static int __pmac_backlight_set_legacy_brightness(int brightness)
14cf11af 141{
5474c120
MH
142 int error = -ENXIO;
143
144 mutex_lock(&pmac_backlight_mutex);
145 if (pmac_backlight) {
146 struct backlight_properties *props;
147
148 down(&pmac_backlight->sem);
149 props = pmac_backlight->props;
150 props->brightness = brightness *
cab267c6
MH
151 (props->max_brightness + 1) /
152 (OLD_BACKLIGHT_MAX + 1);
153
154 if (props->brightness > props->max_brightness)
155 props->brightness = props->max_brightness;
156 else if (props->brightness < 0)
157 props->brightness = 0;
158
5474c120
MH
159 props->update_status(pmac_backlight);
160 up(&pmac_backlight->sem);
161
162 error = 0;
14cf11af 163 }
5474c120
MH
164 mutex_unlock(&pmac_backlight_mutex);
165
166 return error;
14cf11af 167}
5474c120 168
6d5aefb8 169static void pmac_backlight_set_legacy_worker(struct work_struct *work)
4b755999
MH
170{
171 if (atomic_read(&kernel_backlight_disabled))
172 return;
173
174 __pmac_backlight_set_legacy_brightness(pmac_backlight_set_legacy_queued);
175}
176
177/* This function is called in interrupt context */
178void pmac_backlight_set_legacy_brightness_pmu(int brightness) {
179 if (atomic_read(&kernel_backlight_disabled))
180 return;
181
182 pmac_backlight_set_legacy_queued = brightness;
183 schedule_work(&pmac_backlight_set_legacy_work);
184}
185
186int pmac_backlight_set_legacy_brightness(int brightness)
187{
188 return __pmac_backlight_set_legacy_brightness(brightness);
189}
190
5474c120 191int pmac_backlight_get_legacy_brightness()
14cf11af 192{
5474c120 193 int result = -ENXIO;
14cf11af 194
5474c120
MH
195 mutex_lock(&pmac_backlight_mutex);
196 if (pmac_backlight) {
197 struct backlight_properties *props;
14cf11af 198
5474c120
MH
199 down(&pmac_backlight->sem);
200 props = pmac_backlight->props;
cab267c6 201
5474c120 202 result = props->brightness *
cab267c6
MH
203 (OLD_BACKLIGHT_MAX + 1) /
204 (props->max_brightness + 1);
205
5474c120
MH
206 up(&pmac_backlight->sem);
207 }
208 mutex_unlock(&pmac_backlight_mutex);
14cf11af 209
5474c120 210 return result;
14cf11af 211}
e01af038 212
4b755999
MH
213void pmac_backlight_disable()
214{
215 atomic_inc(&kernel_backlight_disabled);
216}
217
218void pmac_backlight_enable()
219{
220 atomic_dec(&kernel_backlight_disabled);
221}
222
e01af038
MH
223EXPORT_SYMBOL_GPL(pmac_backlight);
224EXPORT_SYMBOL_GPL(pmac_backlight_mutex);
225EXPORT_SYMBOL_GPL(pmac_has_backlight_type);
This page took 0.144252 seconds and 5 git commands to generate.