blob: a00096b1c713429be3cbe4a512e83cd66fcf6ee9 [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>
Arun Sharma600634972011-07-26 16:09:06 -070015#include <linux/atomic.h>
Paul Gortmaker66b15db2011-05-27 10:46:24 -040016#include <linux/export.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100017#include <asm/prom.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100018#include <asm/backlight.h>
19
Michael Hanselmann5474c122006-06-25 05:47:08 -070020#define OLD_BACKLIGHT_MAX 15
Paul Mackerras14cf11a2005-09-26 16:04:21 +100021
David Howells6d5aefb2006-12-05 19:36:26 +000022static void pmac_backlight_key_worker(struct work_struct *work);
23static void pmac_backlight_set_legacy_worker(struct work_struct *work);
Michael Hanselmanne01af032006-07-10 04:44:45 -070024
David Howells6d5aefb2006-12-05 19:36:26 +000025static DECLARE_WORK(pmac_backlight_key_work, pmac_backlight_key_worker);
26static DECLARE_WORK(pmac_backlight_set_legacy_work, pmac_backlight_set_legacy_worker);
Michael Hanselmann4b755992006-07-30 03:04:19 -070027
28/* Although these variables are used in interrupt context, it makes no sense to
29 * protect them. No user is able to produce enough key events per second and
Michael Hanselmanne01af032006-07-10 04:44:45 -070030 * notice the errors that might happen.
31 */
32static int pmac_backlight_key_queued;
Michael Hanselmann4b755992006-07-30 03:04:19 -070033static int pmac_backlight_set_legacy_queued;
34
35/* The via-pmu code allows the backlight to be grabbed, in which case the
36 * in-kernel control of the brightness needs to be disabled. This should
37 * only be used by really old PowerBooks.
38 */
39static atomic_t kernel_backlight_disabled = ATOMIC_INIT(0);
Michael Hanselmanne01af032006-07-10 04:44:45 -070040
Richard Purdie28ee0862007-02-08 22:25:09 +000041/* Protect the pmac_backlight variable below.
42 You should hold this lock when using the pmac_backlight pointer to
43 prevent its potential removal. */
Michael Hanselmann5474c122006-06-25 05:47:08 -070044DEFINE_MUTEX(pmac_backlight_mutex);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100045
Michael Hanselmann5474c122006-06-25 05:47:08 -070046/* Main backlight storage
47 *
Richard Purdie599a52d2007-02-10 23:07:48 +000048 * Backlight drivers in this variable are required to have the "ops"
Michael Hanselmann5474c122006-06-25 05:47:08 -070049 * attribute set and to have an update_status function.
50 *
51 * We can only store one backlight here, but since Apple laptops have only one
52 * internal display, it doesn't matter. Other backlight drivers can be used
53 * independently.
54 *
Michael Hanselmann5474c122006-06-25 05:47:08 -070055 */
56struct backlight_device *pmac_backlight;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100057
Michael Hanselmann5474c122006-06-25 05:47:08 -070058int pmac_has_backlight_type(const char *type)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100059{
Stephen Rothwell30686ba2007-04-24 13:53:04 +100060 struct device_node* bk_node = of_find_node_by_name(NULL, "backlight");
Paul Mackerras14cf11a2005-09-26 16:04:21 +100061
Paul Mackerras14cf11a2005-09-26 16:04:21 +100062 if (bk_node) {
Stephen Rothwelle2eb6392007-04-03 22:26:41 +100063 const char *prop = of_get_property(bk_node,
Jeremy Kerr018a3d12006-07-12 15:40:29 +100064 "backlight-control", NULL);
Stephen Rothwell30686ba2007-04-24 13:53:04 +100065 if (prop && strncmp(prop, type, strlen(type)) == 0) {
66 of_node_put(bk_node);
Michael Hanselmann5474c122006-06-25 05:47:08 -070067 return 1;
Stephen Rothwell30686ba2007-04-24 13:53:04 +100068 }
69 of_node_put(bk_node);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100070 }
71
Paul Mackerras14cf11a2005-09-26 16:04:21 +100072 return 0;
73}
74
Michael Hanselmann5474c122006-06-25 05:47:08 -070075int pmac_backlight_curve_lookup(struct fb_info *info, int value)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100076{
Michael Hanselmann5474c122006-06-25 05:47:08 -070077 int level = (FB_BACKLIGHT_LEVELS - 1);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100078
Michael Hanselmann5474c122006-06-25 05:47:08 -070079 if (info && info->bl_dev) {
80 int i, max = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100081
Michael Hanselmann5474c122006-06-25 05:47:08 -070082 /* Look for biggest value */
83 for (i = 0; i < FB_BACKLIGHT_LEVELS; i++)
84 max = max((int)info->bl_curve[i], max);
85
86 /* Look for nearest value */
87 for (i = 0; i < FB_BACKLIGHT_LEVELS; i++) {
88 int diff = abs(info->bl_curve[i] - value);
89 if (diff < max) {
90 max = diff;
91 level = i;
92 }
93 }
94
Paul Mackerras14cf11a2005-09-26 16:04:21 +100095 }
Michael Hanselmann5474c122006-06-25 05:47:08 -070096
97 return level;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100098}
99
David Howells6d5aefb2006-12-05 19:36:26 +0000100static void pmac_backlight_key_worker(struct work_struct *work)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000101{
Michael Hanselmann4b755992006-07-30 03:04:19 -0700102 if (atomic_read(&kernel_backlight_disabled))
103 return;
104
Michael Hanselmann5474c122006-06-25 05:47:08 -0700105 mutex_lock(&pmac_backlight_mutex);
106 if (pmac_backlight) {
107 struct backlight_properties *props;
108 int brightness;
109
Richard Purdie599a52d2007-02-10 23:07:48 +0000110 props = &pmac_backlight->props;
Michael Hanselmann5474c122006-06-25 05:47:08 -0700111
112 brightness = props->brightness +
Michael Hanselmanne01af032006-07-10 04:44:45 -0700113 ((pmac_backlight_key_queued?-1:1) *
114 (props->max_brightness / 15));
Michael Hanselmann5474c122006-06-25 05:47:08 -0700115
116 if (brightness < 0)
117 brightness = 0;
118 else if (brightness > props->max_brightness)
119 brightness = props->max_brightness;
120
121 props->brightness = brightness;
Richard Purdie28ee0862007-02-08 22:25:09 +0000122 backlight_update_status(pmac_backlight);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700123 }
124 mutex_unlock(&pmac_backlight_mutex);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000125}
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000126
Michael Hanselmann4b755992006-07-30 03:04:19 -0700127/* This function is called in interrupt context */
Michael Hanselmanne01af032006-07-10 04:44:45 -0700128void pmac_backlight_key(int direction)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000129{
Michael Hanselmann4b755992006-07-30 03:04:19 -0700130 if (atomic_read(&kernel_backlight_disabled))
131 return;
132
Michael Hanselmanne01af032006-07-10 04:44:45 -0700133 /* we can receive multiple interrupts here, but the scheduled work
134 * will run only once, with the last value
135 */
136 pmac_backlight_key_queued = direction;
137 schedule_work(&pmac_backlight_key_work);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700138}
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000139
Michael Hanselmann4b755992006-07-30 03:04:19 -0700140static int __pmac_backlight_set_legacy_brightness(int brightness)
Michael Hanselmann5474c122006-06-25 05:47:08 -0700141{
142 int error = -ENXIO;
143
144 mutex_lock(&pmac_backlight_mutex);
145 if (pmac_backlight) {
146 struct backlight_properties *props;
147
Richard Purdie599a52d2007-02-10 23:07:48 +0000148 props = &pmac_backlight->props;
Michael Hanselmann5474c122006-06-25 05:47:08 -0700149 props->brightness = brightness *
Michael Hanselmanncab267c2006-06-28 04:26:55 -0700150 (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
Richard Purdie28ee0862007-02-08 22:25:09 +0000158 backlight_update_status(pmac_backlight);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700159
160 error = 0;
161 }
162 mutex_unlock(&pmac_backlight_mutex);
163
164 return error;
165}
166
David Howells6d5aefb2006-12-05 19:36:26 +0000167static void pmac_backlight_set_legacy_worker(struct work_struct *work)
Michael Hanselmann4b755992006-07-30 03:04:19 -0700168{
169 if (atomic_read(&kernel_backlight_disabled))
170 return;
171
172 __pmac_backlight_set_legacy_brightness(pmac_backlight_set_legacy_queued);
173}
174
175/* This function is called in interrupt context */
176void pmac_backlight_set_legacy_brightness_pmu(int brightness) {
177 if (atomic_read(&kernel_backlight_disabled))
178 return;
179
180 pmac_backlight_set_legacy_queued = brightness;
181 schedule_work(&pmac_backlight_set_legacy_work);
182}
183
184int pmac_backlight_set_legacy_brightness(int brightness)
185{
186 return __pmac_backlight_set_legacy_brightness(brightness);
187}
188
Michael Hanselmann5474c122006-06-25 05:47:08 -0700189int pmac_backlight_get_legacy_brightness()
190{
191 int result = -ENXIO;
192
193 mutex_lock(&pmac_backlight_mutex);
194 if (pmac_backlight) {
195 struct backlight_properties *props;
196
Richard Purdie599a52d2007-02-10 23:07:48 +0000197 props = &pmac_backlight->props;
Michael Hanselmanncab267c2006-06-28 04:26:55 -0700198
Michael Hanselmann5474c122006-06-25 05:47:08 -0700199 result = props->brightness *
Michael Hanselmanncab267c2006-06-28 04:26:55 -0700200 (OLD_BACKLIGHT_MAX + 1) /
201 (props->max_brightness + 1);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700202 }
203 mutex_unlock(&pmac_backlight_mutex);
204
205 return result;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000206}
Michael Hanselmanne01af032006-07-10 04:44:45 -0700207
Michael Hanselmann4b755992006-07-30 03:04:19 -0700208void pmac_backlight_disable()
209{
210 atomic_inc(&kernel_backlight_disabled);
211}
212
213void pmac_backlight_enable()
214{
215 atomic_dec(&kernel_backlight_disabled);
216}
217
Michael Hanselmanne01af032006-07-10 04:44:45 -0700218EXPORT_SYMBOL_GPL(pmac_backlight);
219EXPORT_SYMBOL_GPL(pmac_backlight_mutex);
220EXPORT_SYMBOL_GPL(pmac_has_backlight_type);