blob: 74eed6b74cd6dbf3a762328f9b5218b09e5f4a92 [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>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100013#include <asm/prom.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100014#include <asm/backlight.h>
15
Michael Hanselmann5474c122006-06-25 05:47:08 -070016#define OLD_BACKLIGHT_MAX 15
Paul Mackerras14cf11a2005-09-26 16:04:21 +100017
Michael Hanselmanne01af032006-07-10 04:44:45 -070018static void pmac_backlight_key_worker(void *data);
19static DECLARE_WORK(pmac_backlight_key_work, pmac_backlight_key_worker, NULL);
20
21/* Although this variable is used in interrupt context, it makes no sense to
22 * protect it. No user is able to produce enough key events per second and
23 * notice the errors that might happen.
24 */
25static int pmac_backlight_key_queued;
26
Michael Hanselmann5474c122006-06-25 05:47:08 -070027/* Protect the pmac_backlight variable */
28DEFINE_MUTEX(pmac_backlight_mutex);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100029
Michael Hanselmann5474c122006-06-25 05:47:08 -070030/* Main backlight storage
31 *
32 * Backlight drivers in this variable are required to have the "props"
33 * attribute set and to have an update_status function.
34 *
35 * We can only store one backlight here, but since Apple laptops have only one
36 * internal display, it doesn't matter. Other backlight drivers can be used
37 * independently.
38 *
39 * Lock ordering:
40 * pmac_backlight_mutex (global, main backlight)
41 * pmac_backlight->sem (backlight class)
42 */
43struct backlight_device *pmac_backlight;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100044
Michael Hanselmann5474c122006-06-25 05:47:08 -070045int pmac_has_backlight_type(const char *type)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100046{
Michael Hanselmann5474c122006-06-25 05:47:08 -070047 struct device_node* bk_node = find_devices("backlight");
Paul Mackerras14cf11a2005-09-26 16:04:21 +100048
Paul Mackerras14cf11a2005-09-26 16:04:21 +100049 if (bk_node) {
Michael Hanselmann5474c122006-06-25 05:47:08 -070050 char *prop = get_property(bk_node, "backlight-control", NULL);
51 if (prop && strncmp(prop, type, strlen(type)) == 0)
52 return 1;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100053 }
54
Paul Mackerras14cf11a2005-09-26 16:04:21 +100055 return 0;
56}
57
Michael Hanselmann5474c122006-06-25 05:47:08 -070058int pmac_backlight_curve_lookup(struct fb_info *info, int value)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100059{
Michael Hanselmann5474c122006-06-25 05:47:08 -070060 int level = (FB_BACKLIGHT_LEVELS - 1);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100061
Michael Hanselmann5474c122006-06-25 05:47:08 -070062 if (info && info->bl_dev) {
63 int i, max = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100064
Michael Hanselmann5474c122006-06-25 05:47:08 -070065 /* Look for biggest value */
66 for (i = 0; i < FB_BACKLIGHT_LEVELS; i++)
67 max = max((int)info->bl_curve[i], max);
68
69 /* Look for nearest value */
70 for (i = 0; i < FB_BACKLIGHT_LEVELS; i++) {
71 int diff = abs(info->bl_curve[i] - value);
72 if (diff < max) {
73 max = diff;
74 level = i;
75 }
76 }
77
Paul Mackerras14cf11a2005-09-26 16:04:21 +100078 }
Michael Hanselmann5474c122006-06-25 05:47:08 -070079
80 return level;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100081}
82
Michael Hanselmanne01af032006-07-10 04:44:45 -070083static void pmac_backlight_key_worker(void *data)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100084{
Michael Hanselmann5474c122006-06-25 05:47:08 -070085 mutex_lock(&pmac_backlight_mutex);
86 if (pmac_backlight) {
87 struct backlight_properties *props;
88 int brightness;
89
90 down(&pmac_backlight->sem);
91 props = pmac_backlight->props;
92
93 brightness = props->brightness +
Michael Hanselmanne01af032006-07-10 04:44:45 -070094 ((pmac_backlight_key_queued?-1:1) *
95 (props->max_brightness / 15));
Michael Hanselmann5474c122006-06-25 05:47:08 -070096
97 if (brightness < 0)
98 brightness = 0;
99 else if (brightness > props->max_brightness)
100 brightness = props->max_brightness;
101
102 props->brightness = brightness;
103 props->update_status(pmac_backlight);
104
105 up(&pmac_backlight->sem);
106 }
107 mutex_unlock(&pmac_backlight_mutex);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000108}
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000109
Michael Hanselmanne01af032006-07-10 04:44:45 -0700110void pmac_backlight_key(int direction)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000111{
Michael Hanselmanne01af032006-07-10 04:44:45 -0700112 /* we can receive multiple interrupts here, but the scheduled work
113 * will run only once, with the last value
114 */
115 pmac_backlight_key_queued = direction;
116 schedule_work(&pmac_backlight_key_work);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700117}
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000118
Michael Hanselmann5474c122006-06-25 05:47:08 -0700119int pmac_backlight_set_legacy_brightness(int brightness)
120{
121 int error = -ENXIO;
122
123 mutex_lock(&pmac_backlight_mutex);
124 if (pmac_backlight) {
125 struct backlight_properties *props;
126
127 down(&pmac_backlight->sem);
128 props = pmac_backlight->props;
129 props->brightness = brightness *
Michael Hanselmanncab267c2006-06-28 04:26:55 -0700130 (props->max_brightness + 1) /
131 (OLD_BACKLIGHT_MAX + 1);
132
133 if (props->brightness > props->max_brightness)
134 props->brightness = props->max_brightness;
135 else if (props->brightness < 0)
136 props->brightness = 0;
137
Michael Hanselmann5474c122006-06-25 05:47:08 -0700138 props->update_status(pmac_backlight);
139 up(&pmac_backlight->sem);
140
141 error = 0;
142 }
143 mutex_unlock(&pmac_backlight_mutex);
144
145 return error;
146}
147
148int pmac_backlight_get_legacy_brightness()
149{
150 int result = -ENXIO;
151
152 mutex_lock(&pmac_backlight_mutex);
153 if (pmac_backlight) {
154 struct backlight_properties *props;
155
156 down(&pmac_backlight->sem);
157 props = pmac_backlight->props;
Michael Hanselmanncab267c2006-06-28 04:26:55 -0700158
Michael Hanselmann5474c122006-06-25 05:47:08 -0700159 result = props->brightness *
Michael Hanselmanncab267c2006-06-28 04:26:55 -0700160 (OLD_BACKLIGHT_MAX + 1) /
161 (props->max_brightness + 1);
162
Michael Hanselmann5474c122006-06-25 05:47:08 -0700163 up(&pmac_backlight->sem);
164 }
165 mutex_unlock(&pmac_backlight_mutex);
166
167 return result;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000168}
Michael Hanselmanne01af032006-07-10 04:44:45 -0700169
170EXPORT_SYMBOL_GPL(pmac_backlight);
171EXPORT_SYMBOL_GPL(pmac_backlight_mutex);
172EXPORT_SYMBOL_GPL(pmac_has_backlight_type);