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");
60 int i = of_property_match_string(bk_node, "backlight-control", type);
66 static void pmac_backlight_key_worker(struct work_struct *work)
68 if (atomic_read(&kernel_backlight_disabled))
71 mutex_lock(&pmac_backlight_mutex);
73 struct backlight_properties *props;
76 props = &pmac_backlight->props;
78 brightness = props->brightness +
79 ((pmac_backlight_key_queued?-1:1) *
80 (props->max_brightness / 15));
84 else if (brightness > props->max_brightness)
85 brightness = props->max_brightness;
87 props->brightness = brightness;
88 backlight_update_status(pmac_backlight);
90 mutex_unlock(&pmac_backlight_mutex);
93 /* This function is called in interrupt context */
94 void pmac_backlight_key(int direction)
96 if (atomic_read(&kernel_backlight_disabled))
99 /* we can receive multiple interrupts here, but the scheduled work
100 * will run only once, with the last value
102 pmac_backlight_key_queued = direction;
103 schedule_work(&pmac_backlight_key_work);
106 static int __pmac_backlight_set_legacy_brightness(int brightness)
110 mutex_lock(&pmac_backlight_mutex);
111 if (pmac_backlight) {
112 struct backlight_properties *props;
114 props = &pmac_backlight->props;
115 props->brightness = brightness *
116 (props->max_brightness + 1) /
117 (OLD_BACKLIGHT_MAX + 1);
119 if (props->brightness > props->max_brightness)
120 props->brightness = props->max_brightness;
121 else if (props->brightness < 0)
122 props->brightness = 0;
124 backlight_update_status(pmac_backlight);
128 mutex_unlock(&pmac_backlight_mutex);
133 static void pmac_backlight_set_legacy_worker(struct work_struct *work)
135 if (atomic_read(&kernel_backlight_disabled))
138 __pmac_backlight_set_legacy_brightness(pmac_backlight_set_legacy_queued);
141 /* This function is called in interrupt context */
142 void pmac_backlight_set_legacy_brightness_pmu(int brightness) {
143 if (atomic_read(&kernel_backlight_disabled))
146 pmac_backlight_set_legacy_queued = brightness;
147 schedule_work(&pmac_backlight_set_legacy_work);
150 int pmac_backlight_set_legacy_brightness(int brightness)
152 return __pmac_backlight_set_legacy_brightness(brightness);
155 int pmac_backlight_get_legacy_brightness(void)
159 mutex_lock(&pmac_backlight_mutex);
160 if (pmac_backlight) {
161 struct backlight_properties *props;
163 props = &pmac_backlight->props;
165 result = props->brightness *
166 (OLD_BACKLIGHT_MAX + 1) /
167 (props->max_brightness + 1);
169 mutex_unlock(&pmac_backlight_mutex);
174 void pmac_backlight_disable(void)
176 atomic_inc(&kernel_backlight_disabled);
179 void pmac_backlight_enable(void)
181 atomic_dec(&kernel_backlight_disabled);
184 EXPORT_SYMBOL_GPL(pmac_backlight);
185 EXPORT_SYMBOL_GPL(pmac_backlight_mutex);
186 EXPORT_SYMBOL_GPL(pmac_has_backlight_type);