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 | |
David Howells | 6d5aefb | 2006-12-05 19:36:26 +0000 | [diff] [blame] | 21 | static void pmac_backlight_key_worker(struct work_struct *work); |
| 22 | static void pmac_backlight_set_legacy_worker(struct work_struct *work); |
Michael Hanselmann | e01af03 | 2006-07-10 04:44:45 -0700 | [diff] [blame] | 23 | |
David Howells | 6d5aefb | 2006-12-05 19:36:26 +0000 | [diff] [blame] | 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); |
Michael Hanselmann | 4b75599 | 2006-07-30 03:04:19 -0700 | [diff] [blame] | 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 | |
Richard Purdie | 28ee086 | 2007-02-08 22:25:09 +0000 | [diff] [blame] | 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. */ |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 43 | DEFINE_MUTEX(pmac_backlight_mutex); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 44 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 45 | /* Main backlight storage |
| 46 | * |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 47 | * Backlight drivers in this variable are required to have the "ops" |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 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 | * |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 54 | */ |
| 55 | struct backlight_device *pmac_backlight; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 56 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 57 | int pmac_has_backlight_type(const char *type) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 58 | { |
Stephen Rothwell | 30686ba | 2007-04-24 13:53:04 +1000 | [diff] [blame] | 59 | struct device_node* bk_node = of_find_node_by_name(NULL, "backlight"); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 60 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 61 | if (bk_node) { |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 62 | const char *prop = of_get_property(bk_node, |
Jeremy Kerr | 018a3d1 | 2006-07-12 15:40:29 +1000 | [diff] [blame] | 63 | "backlight-control", NULL); |
Stephen Rothwell | 30686ba | 2007-04-24 13:53:04 +1000 | [diff] [blame] | 64 | if (prop && strncmp(prop, type, strlen(type)) == 0) { |
| 65 | of_node_put(bk_node); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 66 | return 1; |
Stephen Rothwell | 30686ba | 2007-04-24 13:53:04 +1000 | [diff] [blame] | 67 | } |
| 68 | of_node_put(bk_node); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 69 | } |
| 70 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 71 | return 0; |
| 72 | } |
| 73 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 74 | int pmac_backlight_curve_lookup(struct fb_info *info, int value) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 75 | { |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 76 | int level = (FB_BACKLIGHT_LEVELS - 1); |
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 | if (info && info->bl_dev) { |
| 79 | int i, max = 0; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 80 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 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 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 94 | } |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 95 | |
| 96 | return level; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 97 | } |
| 98 | |
David Howells | 6d5aefb | 2006-12-05 19:36:26 +0000 | [diff] [blame] | 99 | static void pmac_backlight_key_worker(struct work_struct *work) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 100 | { |
Michael Hanselmann | 4b75599 | 2006-07-30 03:04:19 -0700 | [diff] [blame] | 101 | if (atomic_read(&kernel_backlight_disabled)) |
| 102 | return; |
| 103 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 104 | mutex_lock(&pmac_backlight_mutex); |
| 105 | if (pmac_backlight) { |
| 106 | struct backlight_properties *props; |
| 107 | int brightness; |
| 108 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 109 | props = &pmac_backlight->props; |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 110 | |
| 111 | brightness = props->brightness + |
Michael Hanselmann | e01af03 | 2006-07-10 04:44:45 -0700 | [diff] [blame] | 112 | ((pmac_backlight_key_queued?-1:1) * |
| 113 | (props->max_brightness / 15)); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 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; |
Richard Purdie | 28ee086 | 2007-02-08 22:25:09 +0000 | [diff] [blame] | 121 | backlight_update_status(pmac_backlight); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 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 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 147 | props = &pmac_backlight->props; |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 148 | props->brightness = brightness * |
Michael Hanselmann | cab267c | 2006-06-28 04:26:55 -0700 | [diff] [blame] | 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 | |
Richard Purdie | 28ee086 | 2007-02-08 22:25:09 +0000 | [diff] [blame] | 157 | backlight_update_status(pmac_backlight); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 158 | |
| 159 | error = 0; |
| 160 | } |
| 161 | mutex_unlock(&pmac_backlight_mutex); |
| 162 | |
| 163 | return error; |
| 164 | } |
| 165 | |
David Howells | 6d5aefb | 2006-12-05 19:36:26 +0000 | [diff] [blame] | 166 | static void pmac_backlight_set_legacy_worker(struct work_struct *work) |
Michael Hanselmann | 4b75599 | 2006-07-30 03:04:19 -0700 | [diff] [blame] | 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 */ |
| 175 | void 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 | |
| 183 | int pmac_backlight_set_legacy_brightness(int brightness) |
| 184 | { |
| 185 | return __pmac_backlight_set_legacy_brightness(brightness); |
| 186 | } |
| 187 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 188 | int pmac_backlight_get_legacy_brightness() |
| 189 | { |
| 190 | int result = -ENXIO; |
| 191 | |
| 192 | mutex_lock(&pmac_backlight_mutex); |
| 193 | if (pmac_backlight) { |
| 194 | struct backlight_properties *props; |
| 195 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame] | 196 | props = &pmac_backlight->props; |
Michael Hanselmann | cab267c | 2006-06-28 04:26:55 -0700 | [diff] [blame] | 197 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 198 | result = props->brightness * |
Michael Hanselmann | cab267c | 2006-06-28 04:26:55 -0700 | [diff] [blame] | 199 | (OLD_BACKLIGHT_MAX + 1) / |
| 200 | (props->max_brightness + 1); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 201 | } |
| 202 | mutex_unlock(&pmac_backlight_mutex); |
| 203 | |
| 204 | return result; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 205 | } |
Michael Hanselmann | e01af03 | 2006-07-10 04:44:45 -0700 | [diff] [blame] | 206 | |
Michael Hanselmann | 4b75599 | 2006-07-30 03:04:19 -0700 | [diff] [blame] | 207 | void pmac_backlight_disable() |
| 208 | { |
| 209 | atomic_inc(&kernel_backlight_disabled); |
| 210 | } |
| 211 | |
| 212 | void pmac_backlight_enable() |
| 213 | { |
| 214 | atomic_dec(&kernel_backlight_disabled); |
| 215 | } |
| 216 | |
Michael Hanselmann | e01af03 | 2006-07-10 04:44:45 -0700 | [diff] [blame] | 217 | EXPORT_SYMBOL_GPL(pmac_backlight); |
| 218 | EXPORT_SYMBOL_GPL(pmac_backlight_mutex); |
| 219 | EXPORT_SYMBOL_GPL(pmac_has_backlight_type); |