atomic: use <linux/atomic.h>
[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>
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
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 43DEFINE_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 */
55struct backlight_device *pmac_backlight;
14cf11af 56
5474c120 57int pmac_has_backlight_type(const char *type)
14cf11af 58{
30686ba6 59 struct device_node* bk_node = of_find_node_by_name(NULL, "backlight");
5474c120 60
14cf11af 61 if (bk_node) {
e2eb6392 62 const char *prop = of_get_property(bk_node,
018a3d1d 63 "backlight-control", NULL);
30686ba6
SR
64 if (prop && strncmp(prop, type, strlen(type)) == 0) {
65 of_node_put(bk_node);
5474c120 66 return 1;
30686ba6
SR
67 }
68 of_node_put(bk_node);
14cf11af
PM
69 }
70
5474c120 71 return 0;
14cf11af 72}
14cf11af 73
5474c120 74int pmac_backlight_curve_lookup(struct fb_info *info, int value)
14cf11af 75{
5474c120
MH
76 int level = (FB_BACKLIGHT_LEVELS - 1);
77
78 if (info && info->bl_dev) {
79 int i, max = 0;
80
81 /* Look for biggest value */
82 for (i = 0; i < FB_BACKLIGHT_LEVELS; i++)
83 max = max((int)info->bl_curve[i], max);
84
85 /* Look for nearest value */
86 for (i = 0; i < FB_BACKLIGHT_LEVELS; i++) {
87 int diff = abs(info->bl_curve[i] - value);
88 if (diff < max) {
89 max = diff;
90 level = i;
91 }
92 }
93
94 }
95
96 return level;
14cf11af 97}
14cf11af 98
6d5aefb8 99static void pmac_backlight_key_worker(struct work_struct *work)
14cf11af 100{
4b755999
MH
101 if (atomic_read(&kernel_backlight_disabled))
102 return;
103
5474c120
MH
104 mutex_lock(&pmac_backlight_mutex);
105 if (pmac_backlight) {
106 struct backlight_properties *props;
107 int brightness;
108
599a52d1 109 props = &pmac_backlight->props;
5474c120
MH
110
111 brightness = props->brightness +
e01af038
MH
112 ((pmac_backlight_key_queued?-1:1) *
113 (props->max_brightness / 15));
5474c120
MH
114
115 if (brightness < 0)
116 brightness = 0;
117 else if (brightness > props->max_brightness)
118 brightness = props->max_brightness;
119
120 props->brightness = brightness;
28ee086d 121 backlight_update_status(pmac_backlight);
5474c120
MH
122 }
123 mutex_unlock(&pmac_backlight_mutex);
14cf11af 124}
5474c120 125
4b755999 126/* This function is called in interrupt context */
e01af038 127void pmac_backlight_key(int direction)
14cf11af 128{
4b755999
MH
129 if (atomic_read(&kernel_backlight_disabled))
130 return;
131
e01af038
MH
132 /* we can receive multiple interrupts here, but the scheduled work
133 * will run only once, with the last value
134 */
135 pmac_backlight_key_queued = direction;
136 schedule_work(&pmac_backlight_key_work);
14cf11af
PM
137}
138
4b755999 139static int __pmac_backlight_set_legacy_brightness(int brightness)
14cf11af 140{
5474c120
MH
141 int error = -ENXIO;
142
143 mutex_lock(&pmac_backlight_mutex);
144 if (pmac_backlight) {
145 struct backlight_properties *props;
146
599a52d1 147 props = &pmac_backlight->props;
5474c120 148 props->brightness = brightness *
cab267c6
MH
149 (props->max_brightness + 1) /
150 (OLD_BACKLIGHT_MAX + 1);
151
152 if (props->brightness > props->max_brightness)
153 props->brightness = props->max_brightness;
154 else if (props->brightness < 0)
155 props->brightness = 0;
156
28ee086d 157 backlight_update_status(pmac_backlight);
5474c120
MH
158
159 error = 0;
14cf11af 160 }
5474c120
MH
161 mutex_unlock(&pmac_backlight_mutex);
162
163 return error;
14cf11af 164}
5474c120 165
6d5aefb8 166static void pmac_backlight_set_legacy_worker(struct work_struct *work)
4b755999
MH
167{
168 if (atomic_read(&kernel_backlight_disabled))
169 return;
170
171 __pmac_backlight_set_legacy_brightness(pmac_backlight_set_legacy_queued);
172}
173
174/* This function is called in interrupt context */
175void pmac_backlight_set_legacy_brightness_pmu(int brightness) {
176 if (atomic_read(&kernel_backlight_disabled))
177 return;
178
179 pmac_backlight_set_legacy_queued = brightness;
180 schedule_work(&pmac_backlight_set_legacy_work);
181}
182
183int pmac_backlight_set_legacy_brightness(int brightness)
184{
185 return __pmac_backlight_set_legacy_brightness(brightness);
186}
187
5474c120 188int pmac_backlight_get_legacy_brightness()
14cf11af 189{
5474c120 190 int result = -ENXIO;
14cf11af 191
5474c120
MH
192 mutex_lock(&pmac_backlight_mutex);
193 if (pmac_backlight) {
194 struct backlight_properties *props;
14cf11af 195
599a52d1 196 props = &pmac_backlight->props;
cab267c6 197
5474c120 198 result = props->brightness *
cab267c6
MH
199 (OLD_BACKLIGHT_MAX + 1) /
200 (props->max_brightness + 1);
5474c120
MH
201 }
202 mutex_unlock(&pmac_backlight_mutex);
14cf11af 203
5474c120 204 return result;
14cf11af 205}
e01af038 206
4b755999
MH
207void pmac_backlight_disable()
208{
209 atomic_inc(&kernel_backlight_disabled);
210}
211
212void pmac_backlight_enable()
213{
214 atomic_dec(&kernel_backlight_disabled);
215}
216
e01af038
MH
217EXPORT_SYMBOL_GPL(pmac_backlight);
218EXPORT_SYMBOL_GPL(pmac_backlight_mutex);
219EXPORT_SYMBOL_GPL(pmac_has_backlight_type);
This page took 0.485838 seconds and 5 git commands to generate.