blob: 0dc8a45c408abd48e7a892ea19ba2ce235f5fcea [file] [log] [blame]
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001/*
2 * Miscellaneous procedures for dealing with the PowerMac hardware.
3 * Contains support for the backlight.
4 *
5 * Copyright (C) 2000 Benjamin Herrenschmidt
Michael Hanselmann5474c122006-06-25 05:47:08 -07006 * Copyright (C) 2006 Michael Hanselmann <linux-kernel@hansmi.ch>
Paul Mackerras14cf11a2005-09-26 16:04:21 +10007 *
8 */
9
Paul Mackerras14cf11a2005-09-26 16:04:21 +100010#include <linux/kernel.h>
Michael Hanselmann5474c122006-06-25 05:47:08 -070011#include <linux/fb.h>
12#include <linux/backlight.h>
Michael Hanselmann4b755992006-07-30 03:04:19 -070013#include <linux/adb.h>
14#include <linux/pmu.h>
15#include <asm/atomic.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100016#include <asm/prom.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100017#include <asm/backlight.h>
18
Michael Hanselmann5474c122006-06-25 05:47:08 -070019#define OLD_BACKLIGHT_MAX 15
Paul Mackerras14cf11a2005-09-26 16:04:21 +100020
David Howells6d5aefb2006-12-05 19:36:26 +000021static void pmac_backlight_key_worker(struct work_struct *work);
22static void pmac_backlight_set_legacy_worker(struct work_struct *work);
Michael Hanselmanne01af032006-07-10 04:44:45 -070023
David Howells6d5aefb2006-12-05 19:36:26 +000024static DECLARE_WORK(pmac_backlight_key_work, pmac_backlight_key_worker);
25static DECLARE_WORK(pmac_backlight_set_legacy_work, pmac_backlight_set_legacy_worker);
Michael Hanselmann4b755992006-07-30 03:04:19 -070026
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 Hanselmanne01af032006-07-10 04:44:45 -070029 * notice the errors that might happen.
30 */
31static int pmac_backlight_key_queued;
Michael Hanselmann4b755992006-07-30 03:04:19 -070032static 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 */
38static atomic_t kernel_backlight_disabled = ATOMIC_INIT(0);
Michael Hanselmanne01af032006-07-10 04:44:45 -070039
Richard Purdie28ee0862007-02-08 22:25:09 +000040/* 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 Hanselmann5474c122006-06-25 05:47:08 -070043DEFINE_MUTEX(pmac_backlight_mutex);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100044
Michael Hanselmann5474c122006-06-25 05:47:08 -070045/* Main backlight storage
46 *
Richard Purdie599a52d2007-02-10 23:07:48 +000047 * Backlight drivers in this variable are required to have the "ops"
Michael Hanselmann5474c122006-06-25 05:47:08 -070048 * 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 Hanselmann5474c122006-06-25 05:47:08 -070054 */
55struct backlight_device *pmac_backlight;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100056
Michael Hanselmann5474c122006-06-25 05:47:08 -070057int pmac_has_backlight_type(const char *type)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100058{
Michael Hanselmann5474c122006-06-25 05:47:08 -070059 struct device_node* bk_node = find_devices("backlight");
Paul Mackerras14cf11a2005-09-26 16:04:21 +100060
Paul Mackerras14cf11a2005-09-26 16:04:21 +100061 if (bk_node) {
Stephen Rothwelle2eb6392007-04-03 22:26:41 +100062 const char *prop = of_get_property(bk_node,
Jeremy Kerr018a3d12006-07-12 15:40:29 +100063 "backlight-control", NULL);
Michael Hanselmann5474c122006-06-25 05:47:08 -070064 if (prop && strncmp(prop, type, strlen(type)) == 0)
65 return 1;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100066 }
67
Paul Mackerras14cf11a2005-09-26 16:04:21 +100068 return 0;
69}
70
Michael Hanselmann5474c122006-06-25 05:47:08 -070071int pmac_backlight_curve_lookup(struct fb_info *info, int value)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100072{
Michael Hanselmann5474c122006-06-25 05:47:08 -070073 int level = (FB_BACKLIGHT_LEVELS - 1);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100074
Michael Hanselmann5474c122006-06-25 05:47:08 -070075 if (info && info->bl_dev) {
76 int i, max = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100077
Michael Hanselmann5474c122006-06-25 05:47:08 -070078 /* 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 Mackerras14cf11a2005-09-26 16:04:21 +100091 }
Michael Hanselmann5474c122006-06-25 05:47:08 -070092
93 return level;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100094}
95
David Howells6d5aefb2006-12-05 19:36:26 +000096static void pmac_backlight_key_worker(struct work_struct *work)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100097{
Michael Hanselmann4b755992006-07-30 03:04:19 -070098 if (atomic_read(&kernel_backlight_disabled))
99 return;
100
Michael Hanselmann5474c122006-06-25 05:47:08 -0700101 mutex_lock(&pmac_backlight_mutex);
102 if (pmac_backlight) {
103 struct backlight_properties *props;
104 int brightness;
105
Richard Purdie599a52d2007-02-10 23:07:48 +0000106 props = &pmac_backlight->props;
Michael Hanselmann5474c122006-06-25 05:47:08 -0700107
108 brightness = props->brightness +
Michael Hanselmanne01af032006-07-10 04:44:45 -0700109 ((pmac_backlight_key_queued?-1:1) *
110 (props->max_brightness / 15));
Michael Hanselmann5474c122006-06-25 05:47:08 -0700111
112 if (brightness < 0)
113 brightness = 0;
114 else if (brightness > props->max_brightness)
115 brightness = props->max_brightness;
116
117 props->brightness = brightness;
Richard Purdie28ee0862007-02-08 22:25:09 +0000118 backlight_update_status(pmac_backlight);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700119 }
120 mutex_unlock(&pmac_backlight_mutex);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000121}
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000122
Michael Hanselmann4b755992006-07-30 03:04:19 -0700123/* This function is called in interrupt context */
Michael Hanselmanne01af032006-07-10 04:44:45 -0700124void pmac_backlight_key(int direction)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000125{
Michael Hanselmann4b755992006-07-30 03:04:19 -0700126 if (atomic_read(&kernel_backlight_disabled))
127 return;
128
Michael Hanselmanne01af032006-07-10 04:44:45 -0700129 /* we can receive multiple interrupts here, but the scheduled work
130 * will run only once, with the last value
131 */
132 pmac_backlight_key_queued = direction;
133 schedule_work(&pmac_backlight_key_work);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700134}
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000135
Michael Hanselmann4b755992006-07-30 03:04:19 -0700136static int __pmac_backlight_set_legacy_brightness(int brightness)
Michael Hanselmann5474c122006-06-25 05:47:08 -0700137{
138 int error = -ENXIO;
139
140 mutex_lock(&pmac_backlight_mutex);
141 if (pmac_backlight) {
142 struct backlight_properties *props;
143
Richard Purdie599a52d2007-02-10 23:07:48 +0000144 props = &pmac_backlight->props;
Michael Hanselmann5474c122006-06-25 05:47:08 -0700145 props->brightness = brightness *
Michael Hanselmanncab267c2006-06-28 04:26:55 -0700146 (props->max_brightness + 1) /
147 (OLD_BACKLIGHT_MAX + 1);
148
149 if (props->brightness > props->max_brightness)
150 props->brightness = props->max_brightness;
151 else if (props->brightness < 0)
152 props->brightness = 0;
153
Richard Purdie28ee0862007-02-08 22:25:09 +0000154 backlight_update_status(pmac_backlight);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700155
156 error = 0;
157 }
158 mutex_unlock(&pmac_backlight_mutex);
159
160 return error;
161}
162
David Howells6d5aefb2006-12-05 19:36:26 +0000163static void pmac_backlight_set_legacy_worker(struct work_struct *work)
Michael Hanselmann4b755992006-07-30 03:04:19 -0700164{
165 if (atomic_read(&kernel_backlight_disabled))
166 return;
167
168 __pmac_backlight_set_legacy_brightness(pmac_backlight_set_legacy_queued);
169}
170
171/* This function is called in interrupt context */
172void pmac_backlight_set_legacy_brightness_pmu(int brightness) {
173 if (atomic_read(&kernel_backlight_disabled))
174 return;
175
176 pmac_backlight_set_legacy_queued = brightness;
177 schedule_work(&pmac_backlight_set_legacy_work);
178}
179
180int pmac_backlight_set_legacy_brightness(int brightness)
181{
182 return __pmac_backlight_set_legacy_brightness(brightness);
183}
184
Michael Hanselmann5474c122006-06-25 05:47:08 -0700185int pmac_backlight_get_legacy_brightness()
186{
187 int result = -ENXIO;
188
189 mutex_lock(&pmac_backlight_mutex);
190 if (pmac_backlight) {
191 struct backlight_properties *props;
192
Richard Purdie599a52d2007-02-10 23:07:48 +0000193 props = &pmac_backlight->props;
Michael Hanselmanncab267c2006-06-28 04:26:55 -0700194
Michael Hanselmann5474c122006-06-25 05:47:08 -0700195 result = props->brightness *
Michael Hanselmanncab267c2006-06-28 04:26:55 -0700196 (OLD_BACKLIGHT_MAX + 1) /
197 (props->max_brightness + 1);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700198 }
199 mutex_unlock(&pmac_backlight_mutex);
200
201 return result;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000202}
Michael Hanselmanne01af032006-07-10 04:44:45 -0700203
Michael Hanselmann4b755992006-07-30 03:04:19 -0700204void pmac_backlight_disable()
205{
206 atomic_inc(&kernel_backlight_disabled);
207}
208
209void pmac_backlight_enable()
210{
211 atomic_dec(&kernel_backlight_disabled);
212}
213
Michael Hanselmanne01af032006-07-10 04:44:45 -0700214EXPORT_SYMBOL_GPL(pmac_backlight);
215EXPORT_SYMBOL_GPL(pmac_backlight_mutex);
216EXPORT_SYMBOL_GPL(pmac_has_backlight_type);