Commit | Line | Data |
---|---|---|
14cf11af PM |
1 | /* |
2 | * Miscellaneous procedures for dealing with the PowerMac hardware. | |
3 | * Contains support for the backlight. | |
4 | * | |
5 | * Copyright (C) 2000 Benjamin Herrenschmidt | |
5474c120 | 6 | * Copyright (C) 2006 Michael Hanselmann <linux-kernel@hansmi.ch> |
14cf11af PM |
7 | * |
8 | */ | |
9 | ||
14cf11af | 10 | #include <linux/kernel.h> |
5474c120 MH |
11 | #include <linux/fb.h> |
12 | #include <linux/backlight.h> | |
14cf11af | 13 | #include <asm/prom.h> |
14cf11af PM |
14 | #include <asm/backlight.h> |
15 | ||
5474c120 | 16 | #define OLD_BACKLIGHT_MAX 15 |
14cf11af | 17 | |
5474c120 MH |
18 | /* Protect the pmac_backlight variable */ |
19 | DEFINE_MUTEX(pmac_backlight_mutex); | |
14cf11af | 20 | |
5474c120 MH |
21 | /* 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 | */ | |
34 | struct backlight_device *pmac_backlight; | |
14cf11af | 35 | |
5474c120 | 36 | int pmac_has_backlight_type(const char *type) |
14cf11af | 37 | { |
5474c120 MH |
38 | struct device_node* bk_node = find_devices("backlight"); |
39 | ||
14cf11af | 40 | if (bk_node) { |
018a3d1d JK |
41 | const char *prop = get_property(bk_node, |
42 | "backlight-control", NULL); | |
5474c120 MH |
43 | if (prop && strncmp(prop, type, strlen(type)) == 0) |
44 | return 1; | |
14cf11af PM |
45 | } |
46 | ||
5474c120 | 47 | return 0; |
14cf11af | 48 | } |
14cf11af | 49 | |
5474c120 | 50 | int pmac_backlight_curve_lookup(struct fb_info *info, int value) |
14cf11af | 51 | { |
5474c120 MH |
52 | int level = (FB_BACKLIGHT_LEVELS - 1); |
53 | ||
54 | if (info && info->bl_dev) { | |
55 | int i, max = 0; | |
56 | ||
57 | /* 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 | ||
70 | } | |
71 | ||
72 | return level; | |
14cf11af | 73 | } |
14cf11af | 74 | |
5474c120 | 75 | static void pmac_backlight_key(int direction) |
14cf11af | 76 | { |
5474c120 MH |
77 | 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); | |
14cf11af | 99 | } |
5474c120 MH |
100 | |
101 | void pmac_backlight_key_up() | |
14cf11af | 102 | { |
5474c120 | 103 | pmac_backlight_key(0); |
14cf11af PM |
104 | } |
105 | ||
5474c120 | 106 | void pmac_backlight_key_down() |
14cf11af | 107 | { |
5474c120 | 108 | pmac_backlight_key(1); |
14cf11af | 109 | } |
14cf11af | 110 | |
5474c120 | 111 | int pmac_backlight_set_legacy_brightness(int brightness) |
14cf11af | 112 | { |
5474c120 MH |
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 * | |
cab267c6 MH |
122 | (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 | ||
5474c120 MH |
130 | props->update_status(pmac_backlight); |
131 | up(&pmac_backlight->sem); | |
132 | ||
133 | error = 0; | |
14cf11af | 134 | } |
5474c120 MH |
135 | mutex_unlock(&pmac_backlight_mutex); |
136 | ||
137 | return error; | |
14cf11af | 138 | } |
5474c120 MH |
139 | |
140 | int pmac_backlight_get_legacy_brightness() | |
14cf11af | 141 | { |
5474c120 | 142 | int result = -ENXIO; |
14cf11af | 143 | |
5474c120 MH |
144 | mutex_lock(&pmac_backlight_mutex); |
145 | if (pmac_backlight) { | |
146 | struct backlight_properties *props; | |
14cf11af | 147 | |
5474c120 MH |
148 | down(&pmac_backlight->sem); |
149 | props = pmac_backlight->props; | |
cab267c6 | 150 | |
5474c120 | 151 | result = props->brightness * |
cab267c6 MH |
152 | (OLD_BACKLIGHT_MAX + 1) / |
153 | (props->max_brightness + 1); | |
154 | ||
5474c120 MH |
155 | up(&pmac_backlight->sem); |
156 | } | |
157 | mutex_unlock(&pmac_backlight_mutex); | |
14cf11af | 158 | |
5474c120 | 159 | return result; |
14cf11af | 160 | } |