Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Miscellaneous procedures for dealing with the PowerMac hardware. |
| 3 | * Contains support for the backlight. |
| 4 | * |
| 5 | * Copyright (C) 2000 Benjamin Herrenschmidt |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 6 | * Copyright (C) 2006 Michael Hanselmann <linux-kernel@hansmi.ch> |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 7 | * |
| 8 | */ |
| 9 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 10 | #include <linux/kernel.h> |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 11 | #include <linux/fb.h> |
| 12 | #include <linux/backlight.h> |
Michael Hanselmann | 4b75599 | 2006-07-30 03:04:19 -0700 | [diff] [blame^] | 13 | #include <linux/adb.h> |
| 14 | #include <linux/pmu.h> |
| 15 | #include <asm/atomic.h> |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 16 | #include <asm/prom.h> |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 17 | #include <asm/backlight.h> |
| 18 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 19 | #define OLD_BACKLIGHT_MAX 15 |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 20 | |
Michael Hanselmann | e01af03 | 2006-07-10 04:44:45 -0700 | [diff] [blame] | 21 | static void pmac_backlight_key_worker(void *data); |
Michael Hanselmann | 4b75599 | 2006-07-30 03:04:19 -0700 | [diff] [blame^] | 22 | static void pmac_backlight_set_legacy_worker(void *data); |
Michael Hanselmann | e01af03 | 2006-07-10 04:44:45 -0700 | [diff] [blame] | 23 | |
Michael Hanselmann | 4b75599 | 2006-07-30 03:04:19 -0700 | [diff] [blame^] | 24 | static DECLARE_WORK(pmac_backlight_key_work, pmac_backlight_key_worker, NULL); |
| 25 | static DECLARE_WORK(pmac_backlight_set_legacy_work, pmac_backlight_set_legacy_worker, NULL); |
| 26 | |
| 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 |
Michael Hanselmann | e01af03 | 2006-07-10 04:44:45 -0700 | [diff] [blame] | 29 | * notice the errors that might happen. |
| 30 | */ |
| 31 | static int pmac_backlight_key_queued; |
Michael Hanselmann | 4b75599 | 2006-07-30 03:04:19 -0700 | [diff] [blame^] | 32 | static 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 | */ |
| 38 | static atomic_t kernel_backlight_disabled = ATOMIC_INIT(0); |
Michael Hanselmann | e01af03 | 2006-07-10 04:44:45 -0700 | [diff] [blame] | 39 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 40 | /* Protect the pmac_backlight variable */ |
| 41 | DEFINE_MUTEX(pmac_backlight_mutex); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 42 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 43 | /* Main backlight storage |
| 44 | * |
| 45 | * Backlight drivers in this variable are required to have the "props" |
| 46 | * attribute set and to have an update_status function. |
| 47 | * |
| 48 | * We can only store one backlight here, but since Apple laptops have only one |
| 49 | * internal display, it doesn't matter. Other backlight drivers can be used |
| 50 | * independently. |
| 51 | * |
| 52 | * Lock ordering: |
| 53 | * pmac_backlight_mutex (global, main backlight) |
| 54 | * pmac_backlight->sem (backlight class) |
| 55 | */ |
| 56 | struct backlight_device *pmac_backlight; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 57 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 58 | int pmac_has_backlight_type(const char *type) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 59 | { |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 60 | struct device_node* bk_node = find_devices("backlight"); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 61 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 62 | if (bk_node) { |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 63 | char *prop = get_property(bk_node, "backlight-control", NULL); |
| 64 | if (prop && strncmp(prop, type, strlen(type)) == 0) |
| 65 | return 1; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 66 | } |
| 67 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 68 | return 0; |
| 69 | } |
| 70 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 71 | int pmac_backlight_curve_lookup(struct fb_info *info, int value) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 72 | { |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 73 | int level = (FB_BACKLIGHT_LEVELS - 1); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 74 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 75 | if (info && info->bl_dev) { |
| 76 | int i, max = 0; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 77 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 78 | /* Look for biggest value */ |
| 79 | for (i = 0; i < FB_BACKLIGHT_LEVELS; i++) |
| 80 | max = max((int)info->bl_curve[i], max); |
| 81 | |
| 82 | /* Look for nearest value */ |
| 83 | for (i = 0; i < FB_BACKLIGHT_LEVELS; i++) { |
| 84 | int diff = abs(info->bl_curve[i] - value); |
| 85 | if (diff < max) { |
| 86 | max = diff; |
| 87 | level = i; |
| 88 | } |
| 89 | } |
| 90 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 91 | } |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 92 | |
| 93 | return level; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 94 | } |
| 95 | |
Michael Hanselmann | e01af03 | 2006-07-10 04:44:45 -0700 | [diff] [blame] | 96 | static void pmac_backlight_key_worker(void *data) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 97 | { |
Michael Hanselmann | 4b75599 | 2006-07-30 03:04:19 -0700 | [diff] [blame^] | 98 | if (atomic_read(&kernel_backlight_disabled)) |
| 99 | return; |
| 100 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 101 | mutex_lock(&pmac_backlight_mutex); |
| 102 | if (pmac_backlight) { |
| 103 | struct backlight_properties *props; |
| 104 | int brightness; |
| 105 | |
| 106 | down(&pmac_backlight->sem); |
| 107 | props = pmac_backlight->props; |
| 108 | |
| 109 | brightness = props->brightness + |
Michael Hanselmann | e01af03 | 2006-07-10 04:44:45 -0700 | [diff] [blame] | 110 | ((pmac_backlight_key_queued?-1:1) * |
| 111 | (props->max_brightness / 15)); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 112 | |
| 113 | if (brightness < 0) |
| 114 | brightness = 0; |
| 115 | else if (brightness > props->max_brightness) |
| 116 | brightness = props->max_brightness; |
| 117 | |
| 118 | props->brightness = brightness; |
| 119 | props->update_status(pmac_backlight); |
| 120 | |
| 121 | up(&pmac_backlight->sem); |
| 122 | } |
| 123 | mutex_unlock(&pmac_backlight_mutex); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 124 | } |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 125 | |
Michael Hanselmann | 4b75599 | 2006-07-30 03:04:19 -0700 | [diff] [blame^] | 126 | /* This function is called in interrupt context */ |
Michael Hanselmann | e01af03 | 2006-07-10 04:44:45 -0700 | [diff] [blame] | 127 | void pmac_backlight_key(int direction) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 128 | { |
Michael Hanselmann | 4b75599 | 2006-07-30 03:04:19 -0700 | [diff] [blame^] | 129 | if (atomic_read(&kernel_backlight_disabled)) |
| 130 | return; |
| 131 | |
Michael Hanselmann | e01af03 | 2006-07-10 04:44:45 -0700 | [diff] [blame] | 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); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 137 | } |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 138 | |
Michael Hanselmann | 4b75599 | 2006-07-30 03:04:19 -0700 | [diff] [blame^] | 139 | static int __pmac_backlight_set_legacy_brightness(int brightness) |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 140 | { |
| 141 | int error = -ENXIO; |
| 142 | |
| 143 | mutex_lock(&pmac_backlight_mutex); |
| 144 | if (pmac_backlight) { |
| 145 | struct backlight_properties *props; |
| 146 | |
| 147 | down(&pmac_backlight->sem); |
| 148 | props = pmac_backlight->props; |
| 149 | props->brightness = brightness * |
Michael Hanselmann | cab267c | 2006-06-28 04:26:55 -0700 | [diff] [blame] | 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 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 158 | props->update_status(pmac_backlight); |
| 159 | up(&pmac_backlight->sem); |
| 160 | |
| 161 | error = 0; |
| 162 | } |
| 163 | mutex_unlock(&pmac_backlight_mutex); |
| 164 | |
| 165 | return error; |
| 166 | } |
| 167 | |
Michael Hanselmann | 4b75599 | 2006-07-30 03:04:19 -0700 | [diff] [blame^] | 168 | static void pmac_backlight_set_legacy_worker(void *data) |
| 169 | { |
| 170 | if (atomic_read(&kernel_backlight_disabled)) |
| 171 | return; |
| 172 | |
| 173 | __pmac_backlight_set_legacy_brightness(pmac_backlight_set_legacy_queued); |
| 174 | } |
| 175 | |
| 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; |
| 180 | |
| 181 | pmac_backlight_set_legacy_queued = brightness; |
| 182 | schedule_work(&pmac_backlight_set_legacy_work); |
| 183 | } |
| 184 | |
| 185 | int pmac_backlight_set_legacy_brightness(int brightness) |
| 186 | { |
| 187 | return __pmac_backlight_set_legacy_brightness(brightness); |
| 188 | } |
| 189 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 190 | int pmac_backlight_get_legacy_brightness() |
| 191 | { |
| 192 | int result = -ENXIO; |
| 193 | |
| 194 | mutex_lock(&pmac_backlight_mutex); |
| 195 | if (pmac_backlight) { |
| 196 | struct backlight_properties *props; |
| 197 | |
| 198 | down(&pmac_backlight->sem); |
| 199 | props = pmac_backlight->props; |
Michael Hanselmann | cab267c | 2006-06-28 04:26:55 -0700 | [diff] [blame] | 200 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 201 | result = props->brightness * |
Michael Hanselmann | cab267c | 2006-06-28 04:26:55 -0700 | [diff] [blame] | 202 | (OLD_BACKLIGHT_MAX + 1) / |
| 203 | (props->max_brightness + 1); |
| 204 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 205 | up(&pmac_backlight->sem); |
| 206 | } |
| 207 | mutex_unlock(&pmac_backlight_mutex); |
| 208 | |
| 209 | return result; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 210 | } |
Michael Hanselmann | e01af03 | 2006-07-10 04:44:45 -0700 | [diff] [blame] | 211 | |
Michael Hanselmann | 4b75599 | 2006-07-30 03:04:19 -0700 | [diff] [blame^] | 212 | void pmac_backlight_disable() |
| 213 | { |
| 214 | atomic_inc(&kernel_backlight_disabled); |
| 215 | } |
| 216 | |
| 217 | void pmac_backlight_enable() |
| 218 | { |
| 219 | atomic_dec(&kernel_backlight_disabled); |
| 220 | } |
| 221 | |
Michael Hanselmann | e01af03 | 2006-07-10 04:44:45 -0700 | [diff] [blame] | 222 | EXPORT_SYMBOL_GPL(pmac_backlight); |
| 223 | EXPORT_SYMBOL_GPL(pmac_backlight_mutex); |
| 224 | EXPORT_SYMBOL_GPL(pmac_has_backlight_type); |