blob: 205b4a392862209fc435df846250af96e5a0b5e0 [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 Hanselmann5474c122006-06-25 05:47:08 -070018/* Protect the pmac_backlight variable */
19DEFINE_MUTEX(pmac_backlight_mutex);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100020
Michael Hanselmann5474c122006-06-25 05:47:08 -070021/* Main backlight storage
22 *
23 * Backlight drivers in this variable are required to have the "props"
24 * attribute set and to have an update_status function.
25 *
26 * We can only store one backlight here, but since Apple laptops have only one
27 * internal display, it doesn't matter. Other backlight drivers can be used
28 * independently.
29 *
30 * Lock ordering:
31 * pmac_backlight_mutex (global, main backlight)
32 * pmac_backlight->sem (backlight class)
33 */
34struct backlight_device *pmac_backlight;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100035
Michael Hanselmann5474c122006-06-25 05:47:08 -070036int pmac_has_backlight_type(const char *type)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100037{
Michael Hanselmann5474c122006-06-25 05:47:08 -070038 struct device_node* bk_node = find_devices("backlight");
Paul Mackerras14cf11a2005-09-26 16:04:21 +100039
Paul Mackerras14cf11a2005-09-26 16:04:21 +100040 if (bk_node) {
Jeremy Kerr018a3d12006-07-12 15:40:29 +100041 const char *prop = get_property(bk_node,
42 "backlight-control", NULL);
Michael Hanselmann5474c122006-06-25 05:47:08 -070043 if (prop && strncmp(prop, type, strlen(type)) == 0)
44 return 1;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100045 }
46
Paul Mackerras14cf11a2005-09-26 16:04:21 +100047 return 0;
48}
49
Michael Hanselmann5474c122006-06-25 05:47:08 -070050int pmac_backlight_curve_lookup(struct fb_info *info, int value)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100051{
Michael Hanselmann5474c122006-06-25 05:47:08 -070052 int level = (FB_BACKLIGHT_LEVELS - 1);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100053
Michael Hanselmann5474c122006-06-25 05:47:08 -070054 if (info && info->bl_dev) {
55 int i, max = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100056
Michael Hanselmann5474c122006-06-25 05:47:08 -070057 /* Look for biggest value */
58 for (i = 0; i < FB_BACKLIGHT_LEVELS; i++)
59 max = max((int)info->bl_curve[i], max);
60
61 /* Look for nearest value */
62 for (i = 0; i < FB_BACKLIGHT_LEVELS; i++) {
63 int diff = abs(info->bl_curve[i] - value);
64 if (diff < max) {
65 max = diff;
66 level = i;
67 }
68 }
69
Paul Mackerras14cf11a2005-09-26 16:04:21 +100070 }
Michael Hanselmann5474c122006-06-25 05:47:08 -070071
72 return level;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100073}
74
Michael Hanselmann5474c122006-06-25 05:47:08 -070075static void pmac_backlight_key(int direction)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100076{
Michael Hanselmann5474c122006-06-25 05:47:08 -070077 mutex_lock(&pmac_backlight_mutex);
78 if (pmac_backlight) {
79 struct backlight_properties *props;
80 int brightness;
81
82 down(&pmac_backlight->sem);
83 props = pmac_backlight->props;
84
85 brightness = props->brightness +
86 ((direction?-1:1) * (props->max_brightness / 15));
87
88 if (brightness < 0)
89 brightness = 0;
90 else if (brightness > props->max_brightness)
91 brightness = props->max_brightness;
92
93 props->brightness = brightness;
94 props->update_status(pmac_backlight);
95
96 up(&pmac_backlight->sem);
97 }
98 mutex_unlock(&pmac_backlight_mutex);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100099}
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000100
Michael Hanselmann5474c122006-06-25 05:47:08 -0700101void pmac_backlight_key_up()
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000102{
Michael Hanselmann5474c122006-06-25 05:47:08 -0700103 pmac_backlight_key(0);
104}
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000105
Michael Hanselmann5474c122006-06-25 05:47:08 -0700106void pmac_backlight_key_down()
107{
108 pmac_backlight_key(1);
109}
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000110
Michael Hanselmann5474c122006-06-25 05:47:08 -0700111int pmac_backlight_set_legacy_brightness(int brightness)
112{
113 int error = -ENXIO;
114
115 mutex_lock(&pmac_backlight_mutex);
116 if (pmac_backlight) {
117 struct backlight_properties *props;
118
119 down(&pmac_backlight->sem);
120 props = pmac_backlight->props;
121 props->brightness = brightness *
Michael Hanselmanncab267c2006-06-28 04:26:55 -0700122 (props->max_brightness + 1) /
123 (OLD_BACKLIGHT_MAX + 1);
124
125 if (props->brightness > props->max_brightness)
126 props->brightness = props->max_brightness;
127 else if (props->brightness < 0)
128 props->brightness = 0;
129
Michael Hanselmann5474c122006-06-25 05:47:08 -0700130 props->update_status(pmac_backlight);
131 up(&pmac_backlight->sem);
132
133 error = 0;
134 }
135 mutex_unlock(&pmac_backlight_mutex);
136
137 return error;
138}
139
140int pmac_backlight_get_legacy_brightness()
141{
142 int result = -ENXIO;
143
144 mutex_lock(&pmac_backlight_mutex);
145 if (pmac_backlight) {
146 struct backlight_properties *props;
147
148 down(&pmac_backlight->sem);
149 props = pmac_backlight->props;
Michael Hanselmanncab267c2006-06-28 04:26:55 -0700150
Michael Hanselmann5474c122006-06-25 05:47:08 -0700151 result = props->brightness *
Michael Hanselmanncab267c2006-06-28 04:26:55 -0700152 (OLD_BACKLIGHT_MAX + 1) /
153 (props->max_brightness + 1);
154
Michael Hanselmann5474c122006-06-25 05:47:08 -0700155 up(&pmac_backlight->sem);
156 }
157 mutex_unlock(&pmac_backlight_mutex);
158
159 return result;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000160}