]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
5474c120 MH |
2 | /* |
3 | * Backlight code for via-pmu | |
4 | * | |
5 | * Copyright (C) 1998 Paul Mackerras and Fabio Riccardi. | |
6 | * Copyright (C) 2001-2002 Benjamin Herrenschmidt | |
7 | * Copyright (C) 2006 Michael Hanselmann <[email protected]> | |
8 | * | |
9 | */ | |
10 | ||
11 | #include <asm/ptrace.h> | |
12 | #include <linux/adb.h> | |
074d363a | 13 | #include <linux/backlight.h> |
5474c120 MH |
14 | #include <linux/pmu.h> |
15 | #include <asm/backlight.h> | |
5474c120 MH |
16 | |
17 | #define MAX_PMU_LEVEL 0xFF | |
18 | ||
acc2472e | 19 | static const struct backlight_ops pmu_backlight_data; |
4efd587b | 20 | static DEFINE_SPINLOCK(pmu_backlight_lock); |
fa19d634 | 21 | static int sleeping, uses_pmu_bl; |
d565dd3b | 22 | static u8 bl_curve[FB_BACKLIGHT_LEVELS]; |
5474c120 | 23 | |
d565dd3b BH |
24 | static void pmu_backlight_init_curve(u8 off, u8 min, u8 max) |
25 | { | |
0094f2cd | 26 | int i, flat, count, range = (max - min); |
d565dd3b BH |
27 | |
28 | bl_curve[0] = off; | |
29 | ||
30 | for (flat = 1; flat < (FB_BACKLIGHT_LEVELS / 16); ++flat) | |
31 | bl_curve[flat] = min; | |
32 | ||
33 | count = FB_BACKLIGHT_LEVELS * 15 / 16; | |
34 | for (i = 0; i < count; ++i) | |
35 | bl_curve[flat + i] = min + (range * (i + 1) / count); | |
36 | } | |
37 | ||
38 | static int pmu_backlight_curve_lookup(int value) | |
39 | { | |
40 | int level = (FB_BACKLIGHT_LEVELS - 1); | |
41 | int i, max = 0; | |
42 | ||
43 | /* Look for biggest value */ | |
44 | for (i = 0; i < FB_BACKLIGHT_LEVELS; i++) | |
45 | max = max((int)bl_curve[i], max); | |
46 | ||
47 | /* Look for nearest value */ | |
48 | for (i = 0; i < FB_BACKLIGHT_LEVELS; i++) { | |
49 | int diff = abs(bl_curve[i] - value); | |
50 | if (diff < max) { | |
51 | max = diff; | |
52 | level = i; | |
53 | } | |
54 | } | |
55 | return level; | |
56 | } | |
57 | ||
58 | static int pmu_backlight_get_level_brightness(int level) | |
5474c120 MH |
59 | { |
60 | int pmulevel; | |
61 | ||
62 | /* Get and convert the value */ | |
d565dd3b | 63 | pmulevel = bl_curve[level] * FB_BACKLIGHT_MAX / MAX_PMU_LEVEL; |
5474c120 MH |
64 | if (pmulevel < 0) |
65 | pmulevel = 0; | |
66 | else if (pmulevel > MAX_PMU_LEVEL) | |
67 | pmulevel = MAX_PMU_LEVEL; | |
68 | ||
69 | return pmulevel; | |
70 | } | |
71 | ||
0094f2cd | 72 | static int __pmu_backlight_update_status(struct backlight_device *bd) |
5474c120 | 73 | { |
5474c120 | 74 | struct adb_request req; |
a0542d2c | 75 | int level = backlight_get_brightness(bd); |
5474c120 | 76 | |
4b755999 | 77 | if (level > 0) { |
d565dd3b | 78 | int pmulevel = pmu_backlight_get_level_brightness(level); |
5474c120 | 79 | |
4b755999 MH |
80 | pmu_request(&req, NULL, 2, PMU_BACKLIGHT_BRIGHT, pmulevel); |
81 | pmu_wait_complete(&req); | |
5474c120 | 82 | |
4b755999 MH |
83 | pmu_request(&req, NULL, 2, PMU_POWER_CTRL, |
84 | PMU_POW_BACKLIGHT | PMU_POW_ON); | |
85 | pmu_wait_complete(&req); | |
86 | } else { | |
87 | pmu_request(&req, NULL, 2, PMU_POWER_CTRL, | |
88 | PMU_POW_BACKLIGHT | PMU_POW_OFF); | |
89 | pmu_wait_complete(&req); | |
90 | } | |
91 | ||
5474c120 MH |
92 | return 0; |
93 | } | |
94 | ||
0094f2cd BH |
95 | static int pmu_backlight_update_status(struct backlight_device *bd) |
96 | { | |
97 | unsigned long flags; | |
98 | int rc = 0; | |
99 | ||
100 | spin_lock_irqsave(&pmu_backlight_lock, flags); | |
101 | /* Don't update brightness when sleeping */ | |
102 | if (!sleeping) | |
103 | rc = __pmu_backlight_update_status(bd); | |
104 | spin_unlock_irqrestore(&pmu_backlight_lock, flags); | |
105 | return rc; | |
106 | } | |
107 | ||
108 | ||
acc2472e | 109 | static const struct backlight_ops pmu_backlight_data = { |
5474c120 | 110 | .update_status = pmu_backlight_update_status, |
599a52d1 | 111 | |
5474c120 MH |
112 | }; |
113 | ||
4b755999 | 114 | #ifdef CONFIG_PM |
d565dd3b | 115 | void pmu_backlight_set_sleep(int sleep) |
4b755999 MH |
116 | { |
117 | unsigned long flags; | |
118 | ||
119 | spin_lock_irqsave(&pmu_backlight_lock, flags); | |
d565dd3b | 120 | sleeping = sleep; |
fa19d634 | 121 | if (pmac_backlight && uses_pmu_bl) { |
0094f2cd BH |
122 | if (sleep) { |
123 | struct adb_request req; | |
124 | ||
125 | pmu_request(&req, NULL, 2, PMU_POWER_CTRL, | |
126 | PMU_POW_BACKLIGHT | PMU_POW_OFF); | |
127 | pmu_wait_complete(&req); | |
128 | } else | |
129 | __pmu_backlight_update_status(pmac_backlight); | |
130 | } | |
4b755999 | 131 | spin_unlock_irqrestore(&pmu_backlight_lock, flags); |
4b755999 | 132 | } |
d565dd3b | 133 | #endif /* CONFIG_PM */ |
4b755999 | 134 | |
00f7b29f | 135 | void __init pmu_backlight_init(void) |
5474c120 | 136 | { |
a19a6ee6 | 137 | struct backlight_properties props; |
5474c120 | 138 | struct backlight_device *bd; |
5474c120 MH |
139 | char name[10]; |
140 | int level, autosave; | |
141 | ||
5474c120 MH |
142 | /* Special case for the old PowerBook since I can't test on it */ |
143 | autosave = | |
71a157e8 GL |
144 | of_machine_is_compatible("AAPL,3400/2400") || |
145 | of_machine_is_compatible("AAPL,3500"); | |
5474c120 MH |
146 | |
147 | if (!autosave && | |
148 | !pmac_has_backlight_type("pmu") && | |
71a157e8 GL |
149 | !of_machine_is_compatible("AAPL,PowerBook1998") && |
150 | !of_machine_is_compatible("PowerBook1,1")) | |
5474c120 MH |
151 | return; |
152 | ||
d565dd3b | 153 | snprintf(name, sizeof(name), "pmubl"); |
5474c120 | 154 | |
a19a6ee6 | 155 | memset(&props, 0, sizeof(struct backlight_properties)); |
bb7ca747 | 156 | props.type = BACKLIGHT_PLATFORM; |
a19a6ee6 MG |
157 | props.max_brightness = FB_BACKLIGHT_LEVELS - 1; |
158 | bd = backlight_device_register(name, NULL, NULL, &pmu_backlight_data, | |
159 | &props); | |
5474c120 | 160 | if (IS_ERR(bd)) { |
0094f2cd BH |
161 | printk(KERN_ERR "PMU Backlight registration failed\n"); |
162 | return; | |
5474c120 | 163 | } |
fa19d634 | 164 | uses_pmu_bl = 1; |
d565dd3b | 165 | pmu_backlight_init_curve(0x7F, 0x46, 0x0E); |
5474c120 | 166 | |
599a52d1 | 167 | level = bd->props.max_brightness; |
5474c120 MH |
168 | |
169 | if (autosave) { | |
170 | /* read autosaved value if available */ | |
171 | struct adb_request req; | |
172 | pmu_request(&req, NULL, 2, 0xd9, 0); | |
173 | pmu_wait_complete(&req); | |
174 | ||
d565dd3b | 175 | level = pmu_backlight_curve_lookup( |
5474c120 | 176 | (req.reply[0] >> 4) * |
599a52d1 | 177 | bd->props.max_brightness / 15); |
5474c120 MH |
178 | } |
179 | ||
599a52d1 | 180 | bd->props.brightness = level; |
c7907a47 | 181 | bd->props.power = BACKLIGHT_POWER_ON; |
28ee086d | 182 | backlight_update_status(bd); |
5474c120 | 183 | |
0094f2cd | 184 | printk(KERN_INFO "PMU Backlight initialized (%s)\n", name); |
5474c120 | 185 | } |