1 // SPDX-License-Identifier: GPL-2.0-only
3 * Miscellaneous procedures for dealing with the PowerMac hardware.
4 * Contains support for the backlight.
6 * Copyright (C) 2000 Benjamin Herrenschmidt
11 #include <linux/kernel.h>
12 #include <linux/backlight.h>
13 #include <linux/adb.h>
14 #include <linux/pmu.h>
15 #include <linux/atomic.h>
16 #include <linux/export.h>
17 #include <asm/backlight.h>
19 #define OLD_BACKLIGHT_MAX 15
21 static void pmac_backlight_key_worker(struct work_struct *work);
22 static void pmac_backlight_set_legacy_worker(struct work_struct *work);
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);
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
29 * notice the errors that might happen.
31 static int pmac_backlight_key_queued;
32 static int pmac_backlight_set_legacy_queued;
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.
38 static atomic_t kernel_backlight_disabled = ATOMIC_INIT(0);
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. */
43 DEFINE_MUTEX(pmac_backlight_mutex);
45 /* Main backlight storage
47 * Backlight drivers in this variable are required to have the "ops"
48 * attribute set and to have an update_status function.
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
55 struct backlight_device *pmac_backlight;
57 int pmac_has_backlight_type(const char *type)
59 struct device_node* bk_node = of_find_node_by_name(NULL, "backlight");
62 const char *prop = of_get_property(bk_node,
63 "backlight-control", NULL);
64 if (prop && strncmp(prop, type, strlen(type)) == 0) {
74 static void pmac_backlight_key_worker(struct work_struct *work)
76 if (atomic_read(&kernel_backlight_disabled))
79 mutex_lock(&pmac_backlight_mutex);
81 struct backlight_properties *props;
84 props = &pmac_backlight->props;
86 brightness = props->brightness +
87 ((pmac_backlight_key_queued?-1:1) *
88 (props->max_brightness / 15));
92 else if (brightness > props->max_brightness)
93 brightness = props->max_brightness;
95 props->brightness = brightness;
96 backlight_update_status(pmac_backlight);
98 mutex_unlock(&pmac_backlight_mutex);
101 /* This function is called in interrupt context */
102 void pmac_backlight_key(int direction)
104 if (atomic_read(&kernel_backlight_disabled))
107 /* we can receive multiple interrupts here, but the scheduled work
108 * will run only once, with the last value
110 pmac_backlight_key_queued = direction;
111 schedule_work(&pmac_backlight_key_work);
114 static int __pmac_backlight_set_legacy_brightness(int brightness)
118 mutex_lock(&pmac_backlight_mutex);
119 if (pmac_backlight) {
120 struct backlight_properties *props;
122 props = &pmac_backlight->props;
123 props->brightness = brightness *
124 (props->max_brightness + 1) /
125 (OLD_BACKLIGHT_MAX + 1);
127 if (props->brightness > props->max_brightness)
128 props->brightness = props->max_brightness;
129 else if (props->brightness < 0)
130 props->brightness = 0;
132 backlight_update_status(pmac_backlight);
136 mutex_unlock(&pmac_backlight_mutex);
141 static void pmac_backlight_set_legacy_worker(struct work_struct *work)
143 if (atomic_read(&kernel_backlight_disabled))
146 __pmac_backlight_set_legacy_brightness(pmac_backlight_set_legacy_queued);
149 /* This function is called in interrupt context */
150 void pmac_backlight_set_legacy_brightness_pmu(int brightness) {
151 if (atomic_read(&kernel_backlight_disabled))
154 pmac_backlight_set_legacy_queued = brightness;
155 schedule_work(&pmac_backlight_set_legacy_work);
158 int pmac_backlight_set_legacy_brightness(int brightness)
160 return __pmac_backlight_set_legacy_brightness(brightness);
163 int pmac_backlight_get_legacy_brightness(void)
167 mutex_lock(&pmac_backlight_mutex);
168 if (pmac_backlight) {
169 struct backlight_properties *props;
171 props = &pmac_backlight->props;
173 result = props->brightness *
174 (OLD_BACKLIGHT_MAX + 1) /
175 (props->max_brightness + 1);
177 mutex_unlock(&pmac_backlight_mutex);
182 void pmac_backlight_disable(void)
184 atomic_inc(&kernel_backlight_disabled);
187 void pmac_backlight_enable(void)
189 atomic_dec(&kernel_backlight_disabled);
192 EXPORT_SYMBOL_GPL(pmac_backlight);
193 EXPORT_SYMBOL_GPL(pmac_backlight_mutex);
194 EXPORT_SYMBOL_GPL(pmac_has_backlight_type);