WIP FPC-III support
[linux/fpc-iii.git] / arch / powerpc / platforms / powermac / backlight.c
blob32224cb489d757dda98eb733f6ad7edbbf3a578e
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Miscellaneous procedures for dealing with the PowerMac hardware.
4 * Contains support for the backlight.
6 * Copyright (C) 2000 Benjamin Herrenschmidt
7 * Copyright (C) 2006 Michael Hanselmann <linux-kernel@hansmi.ch>
9 */
11 #include <linux/kernel.h>
12 #include <linux/fb.h>
13 #include <linux/backlight.h>
14 #include <linux/adb.h>
15 #include <linux/pmu.h>
16 #include <linux/atomic.h>
17 #include <linux/export.h>
18 #include <asm/prom.h>
19 #include <asm/backlight.h>
21 #define OLD_BACKLIGHT_MAX 15
23 static void pmac_backlight_key_worker(struct work_struct *work);
24 static void pmac_backlight_set_legacy_worker(struct work_struct *work);
26 static DECLARE_WORK(pmac_backlight_key_work, pmac_backlight_key_worker);
27 static DECLARE_WORK(pmac_backlight_set_legacy_work, pmac_backlight_set_legacy_worker);
29 /* Although these variables are used in interrupt context, it makes no sense to
30 * protect them. No user is able to produce enough key events per second and
31 * notice the errors that might happen.
33 static int pmac_backlight_key_queued;
34 static int pmac_backlight_set_legacy_queued;
36 /* The via-pmu code allows the backlight to be grabbed, in which case the
37 * in-kernel control of the brightness needs to be disabled. This should
38 * only be used by really old PowerBooks.
40 static atomic_t kernel_backlight_disabled = ATOMIC_INIT(0);
42 /* Protect the pmac_backlight variable below.
43 You should hold this lock when using the pmac_backlight pointer to
44 prevent its potential removal. */
45 DEFINE_MUTEX(pmac_backlight_mutex);
47 /* Main backlight storage
49 * Backlight drivers in this variable are required to have the "ops"
50 * attribute set and to have an update_status function.
52 * We can only store one backlight here, but since Apple laptops have only one
53 * internal display, it doesn't matter. Other backlight drivers can be used
54 * independently.
57 struct backlight_device *pmac_backlight;
59 int pmac_has_backlight_type(const char *type)
61 struct device_node* bk_node = of_find_node_by_name(NULL, "backlight");
63 if (bk_node) {
64 const char *prop = of_get_property(bk_node,
65 "backlight-control", NULL);
66 if (prop && strncmp(prop, type, strlen(type)) == 0) {
67 of_node_put(bk_node);
68 return 1;
70 of_node_put(bk_node);
73 return 0;
76 int pmac_backlight_curve_lookup(struct fb_info *info, int value)
78 int level = (FB_BACKLIGHT_LEVELS - 1);
80 if (info && info->bl_dev) {
81 int i, max = 0;
83 /* Look for biggest value */
84 for (i = 0; i < FB_BACKLIGHT_LEVELS; i++)
85 max = max((int)info->bl_curve[i], max);
87 /* Look for nearest value */
88 for (i = 0; i < FB_BACKLIGHT_LEVELS; i++) {
89 int diff = abs(info->bl_curve[i] - value);
90 if (diff < max) {
91 max = diff;
92 level = i;
98 return level;
101 static void pmac_backlight_key_worker(struct work_struct *work)
103 if (atomic_read(&kernel_backlight_disabled))
104 return;
106 mutex_lock(&pmac_backlight_mutex);
107 if (pmac_backlight) {
108 struct backlight_properties *props;
109 int brightness;
111 props = &pmac_backlight->props;
113 brightness = props->brightness +
114 ((pmac_backlight_key_queued?-1:1) *
115 (props->max_brightness / 15));
117 if (brightness < 0)
118 brightness = 0;
119 else if (brightness > props->max_brightness)
120 brightness = props->max_brightness;
122 props->brightness = brightness;
123 backlight_update_status(pmac_backlight);
125 mutex_unlock(&pmac_backlight_mutex);
128 /* This function is called in interrupt context */
129 void pmac_backlight_key(int direction)
131 if (atomic_read(&kernel_backlight_disabled))
132 return;
134 /* we can receive multiple interrupts here, but the scheduled work
135 * will run only once, with the last value
137 pmac_backlight_key_queued = direction;
138 schedule_work(&pmac_backlight_key_work);
141 static int __pmac_backlight_set_legacy_brightness(int brightness)
143 int error = -ENXIO;
145 mutex_lock(&pmac_backlight_mutex);
146 if (pmac_backlight) {
147 struct backlight_properties *props;
149 props = &pmac_backlight->props;
150 props->brightness = brightness *
151 (props->max_brightness + 1) /
152 (OLD_BACKLIGHT_MAX + 1);
154 if (props->brightness > props->max_brightness)
155 props->brightness = props->max_brightness;
156 else if (props->brightness < 0)
157 props->brightness = 0;
159 backlight_update_status(pmac_backlight);
161 error = 0;
163 mutex_unlock(&pmac_backlight_mutex);
165 return error;
168 static void pmac_backlight_set_legacy_worker(struct work_struct *work)
170 if (atomic_read(&kernel_backlight_disabled))
171 return;
173 __pmac_backlight_set_legacy_brightness(pmac_backlight_set_legacy_queued);
176 /* This function is called in interrupt context */
177 void pmac_backlight_set_legacy_brightness_pmu(int brightness) {
178 if (atomic_read(&kernel_backlight_disabled))
179 return;
181 pmac_backlight_set_legacy_queued = brightness;
182 schedule_work(&pmac_backlight_set_legacy_work);
185 int pmac_backlight_set_legacy_brightness(int brightness)
187 return __pmac_backlight_set_legacy_brightness(brightness);
190 int pmac_backlight_get_legacy_brightness(void)
192 int result = -ENXIO;
194 mutex_lock(&pmac_backlight_mutex);
195 if (pmac_backlight) {
196 struct backlight_properties *props;
198 props = &pmac_backlight->props;
200 result = props->brightness *
201 (OLD_BACKLIGHT_MAX + 1) /
202 (props->max_brightness + 1);
204 mutex_unlock(&pmac_backlight_mutex);
206 return result;
209 void pmac_backlight_disable(void)
211 atomic_inc(&kernel_backlight_disabled);
214 void pmac_backlight_enable(void)
216 atomic_dec(&kernel_backlight_disabled);
219 EXPORT_SYMBOL_GPL(pmac_backlight);
220 EXPORT_SYMBOL_GPL(pmac_backlight_mutex);
221 EXPORT_SYMBOL_GPL(pmac_has_backlight_type);