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