Merge branch 'x86-cpu-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[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>
60063497 15#include <linux/atomic.h>
66b15db6 16#include <linux/export.h>
14cf11af 17#include <asm/prom.h>
14cf11af
PM
18#include <asm/backlight.h>
19
5474c120 20#define OLD_BACKLIGHT_MAX 15
14cf11af 21
6d5aefb8
DH
22static void pmac_backlight_key_worker(struct work_struct *work);
23static void pmac_backlight_set_legacy_worker(struct work_struct *work);
4b755999 24
6d5aefb8
DH
25static DECLARE_WORK(pmac_backlight_key_work, pmac_backlight_key_worker);
26static DECLARE_WORK(pmac_backlight_set_legacy_work, pmac_backlight_set_legacy_worker);
e01af038 27
4b755999
MH
28/* Although these variables are used in interrupt context, it makes no sense to
29 * protect them. No user is able to produce enough key events per second and
e01af038
MH
30 * notice the errors that might happen.
31 */
32static int pmac_backlight_key_queued;
4b755999
MH
33static int pmac_backlight_set_legacy_queued;
34
35/* The via-pmu code allows the backlight to be grabbed, in which case the
36 * in-kernel control of the brightness needs to be disabled. This should
37 * only be used by really old PowerBooks.
38 */
39static atomic_t kernel_backlight_disabled = ATOMIC_INIT(0);
e01af038 40
28ee086d
RP
41/* Protect the pmac_backlight variable below.
42 You should hold this lock when using the pmac_backlight pointer to
43 prevent its potential removal. */
5474c120 44DEFINE_MUTEX(pmac_backlight_mutex);
14cf11af 45
5474c120
MH
46/* Main backlight storage
47 *
599a52d1 48 * Backlight drivers in this variable are required to have the "ops"
5474c120
MH
49 * attribute set and to have an update_status function.
50 *
51 * We can only store one backlight here, but since Apple laptops have only one
52 * internal display, it doesn't matter. Other backlight drivers can be used
53 * independently.
54 *
5474c120
MH
55 */
56struct backlight_device *pmac_backlight;
14cf11af 57
5474c120 58int pmac_has_backlight_type(const char *type)
14cf11af 59{
30686ba6 60 struct device_node* bk_node = of_find_node_by_name(NULL, "backlight");
5474c120 61
14cf11af 62 if (bk_node) {
e2eb6392 63 const char *prop = of_get_property(bk_node,
018a3d1d 64 "backlight-control", NULL);
30686ba6
SR
65 if (prop && strncmp(prop, type, strlen(type)) == 0) {
66 of_node_put(bk_node);
5474c120 67 return 1;
30686ba6
SR
68 }
69 of_node_put(bk_node);
14cf11af
PM
70 }
71
5474c120 72 return 0;
14cf11af 73}
14cf11af 74
5474c120 75int pmac_backlight_curve_lookup(struct fb_info *info, int value)
14cf11af 76{
5474c120
MH
77 int level = (FB_BACKLIGHT_LEVELS - 1);
78
79 if (info && info->bl_dev) {
80 int i, max = 0;
81
82 /* Look for biggest value */
83 for (i = 0; i < FB_BACKLIGHT_LEVELS; i++)
84 max = max((int)info->bl_curve[i], max);
85
86 /* Look for nearest value */
87 for (i = 0; i < FB_BACKLIGHT_LEVELS; i++) {
88 int diff = abs(info->bl_curve[i] - value);
89 if (diff < max) {
90 max = diff;
91 level = i;
92 }
93 }
94
95 }
96
97 return level;
14cf11af 98}
14cf11af 99
6d5aefb8 100static void pmac_backlight_key_worker(struct work_struct *work)
14cf11af 101{
4b755999
MH
102 if (atomic_read(&kernel_backlight_disabled))
103 return;
104
5474c120
MH
105 mutex_lock(&pmac_backlight_mutex);
106 if (pmac_backlight) {
107 struct backlight_properties *props;
108 int brightness;
109
599a52d1 110 props = &pmac_backlight->props;
5474c120
MH
111
112 brightness = props->brightness +
e01af038
MH
113 ((pmac_backlight_key_queued?-1:1) *
114 (props->max_brightness / 15));
5474c120
MH
115
116 if (brightness < 0)
117 brightness = 0;
118 else if (brightness > props->max_brightness)
119 brightness = props->max_brightness;
120
121 props->brightness = brightness;
28ee086d 122 backlight_update_status(pmac_backlight);
5474c120
MH
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
599a52d1 148 props = &pmac_backlight->props;
5474c120 149 props->brightness = brightness *
cab267c6
MH
150 (props->max_brightness + 1) /
151 (OLD_BACKLIGHT_MAX + 1);
152
153 if (props->brightness > props->max_brightness)
154 props->brightness = props->max_brightness;
155 else if (props->brightness < 0)
156 props->brightness = 0;
157
28ee086d 158 backlight_update_status(pmac_backlight);
5474c120
MH
159
160 error = 0;
14cf11af 161 }
5474c120
MH
162 mutex_unlock(&pmac_backlight_mutex);
163
164 return error;
14cf11af 165}
5474c120 166
6d5aefb8 167static void pmac_backlight_set_legacy_worker(struct work_struct *work)
4b755999
MH
168{
169 if (atomic_read(&kernel_backlight_disabled))
170 return;
171
172 __pmac_backlight_set_legacy_brightness(pmac_backlight_set_legacy_queued);
173}
174
175/* This function is called in interrupt context */
176void pmac_backlight_set_legacy_brightness_pmu(int brightness) {
177 if (atomic_read(&kernel_backlight_disabled))
178 return;
179
180 pmac_backlight_set_legacy_queued = brightness;
181 schedule_work(&pmac_backlight_set_legacy_work);
182}
183
184int pmac_backlight_set_legacy_brightness(int brightness)
185{
186 return __pmac_backlight_set_legacy_brightness(brightness);
187}
188
5474c120 189int pmac_backlight_get_legacy_brightness()
14cf11af 190{
5474c120 191 int result = -ENXIO;
14cf11af 192
5474c120
MH
193 mutex_lock(&pmac_backlight_mutex);
194 if (pmac_backlight) {
195 struct backlight_properties *props;
14cf11af 196
599a52d1 197 props = &pmac_backlight->props;
cab267c6 198
5474c120 199 result = props->brightness *
cab267c6
MH
200 (OLD_BACKLIGHT_MAX + 1) /
201 (props->max_brightness + 1);
5474c120
MH
202 }
203 mutex_unlock(&pmac_backlight_mutex);
14cf11af 204
5474c120 205 return result;
14cf11af 206}
e01af038 207
4b755999
MH
208void pmac_backlight_disable()
209{
210 atomic_inc(&kernel_backlight_disabled);
211}
212
213void pmac_backlight_enable()
214{
215 atomic_dec(&kernel_backlight_disabled);
216}
217
e01af038
MH
218EXPORT_SYMBOL_GPL(pmac_backlight);
219EXPORT_SYMBOL_GPL(pmac_backlight_mutex);
220EXPORT_SYMBOL_GPL(pmac_has_backlight_type);
This page took 0.889793 seconds and 5 git commands to generate.