2 * Backlight code for via-pmu
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>
10 #include <asm/ptrace.h>
11 #include <linux/adb.h>
12 #include <linux/pmu.h>
13 #include <asm/backlight.h>
16 #define MAX_PMU_LEVEL 0xFF
18 static struct backlight_ops pmu_backlight_data
;
19 static DEFINE_SPINLOCK(pmu_backlight_lock
);
20 <<<<<<< HEAD
:drivers
/macintosh
/via
-pmu
-backlight
.c
23 static int sleeping
, uses_pmu_bl
;
24 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/macintosh
/via
-pmu
-backlight
.c
25 static u8 bl_curve
[FB_BACKLIGHT_LEVELS
];
27 static void pmu_backlight_init_curve(u8 off
, u8 min
, u8 max
)
29 int i
, flat
, count
, range
= (max
- min
);
33 for (flat
= 1; flat
< (FB_BACKLIGHT_LEVELS
/ 16); ++flat
)
36 count
= FB_BACKLIGHT_LEVELS
* 15 / 16;
37 for (i
= 0; i
< count
; ++i
)
38 bl_curve
[flat
+ i
] = min
+ (range
* (i
+ 1) / count
);
41 static int pmu_backlight_curve_lookup(int value
)
43 int level
= (FB_BACKLIGHT_LEVELS
- 1);
46 /* Look for biggest value */
47 for (i
= 0; i
< FB_BACKLIGHT_LEVELS
; i
++)
48 max
= max((int)bl_curve
[i
], max
);
50 /* Look for nearest value */
51 for (i
= 0; i
< FB_BACKLIGHT_LEVELS
; i
++) {
52 int diff
= abs(bl_curve
[i
] - value
);
61 static int pmu_backlight_get_level_brightness(int level
)
65 /* Get and convert the value */
66 pmulevel
= bl_curve
[level
] * FB_BACKLIGHT_MAX
/ MAX_PMU_LEVEL
;
69 else if (pmulevel
> MAX_PMU_LEVEL
)
70 pmulevel
= MAX_PMU_LEVEL
;
75 static int __pmu_backlight_update_status(struct backlight_device
*bd
)
77 struct adb_request req
;
78 int level
= bd
->props
.brightness
;
81 if (bd
->props
.power
!= FB_BLANK_UNBLANK
||
82 bd
->props
.fb_blank
!= FB_BLANK_UNBLANK
)
86 int pmulevel
= pmu_backlight_get_level_brightness(level
);
88 pmu_request(&req
, NULL
, 2, PMU_BACKLIGHT_BRIGHT
, pmulevel
);
89 pmu_wait_complete(&req
);
91 pmu_request(&req
, NULL
, 2, PMU_POWER_CTRL
,
92 PMU_POW_BACKLIGHT
| PMU_POW_ON
);
93 pmu_wait_complete(&req
);
95 pmu_request(&req
, NULL
, 2, PMU_POWER_CTRL
,
96 PMU_POW_BACKLIGHT
| PMU_POW_OFF
);
97 pmu_wait_complete(&req
);
103 static int pmu_backlight_update_status(struct backlight_device
*bd
)
108 spin_lock_irqsave(&pmu_backlight_lock
, flags
);
109 /* Don't update brightness when sleeping */
111 rc
= __pmu_backlight_update_status(bd
);
112 spin_unlock_irqrestore(&pmu_backlight_lock
, flags
);
117 static int pmu_backlight_get_brightness(struct backlight_device
*bd
)
119 return bd
->props
.brightness
;
122 static struct backlight_ops pmu_backlight_data
= {
123 .get_brightness
= pmu_backlight_get_brightness
,
124 .update_status
= pmu_backlight_update_status
,
129 void pmu_backlight_set_sleep(int sleep
)
133 spin_lock_irqsave(&pmu_backlight_lock
, flags
);
135 <<<<<<< HEAD
:drivers
/macintosh
/via
-pmu
-backlight
.c
136 if (pmac_backlight
) {
138 if (pmac_backlight
&& uses_pmu_bl
) {
139 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/macintosh
/via
-pmu
-backlight
.c
141 struct adb_request req
;
143 pmu_request(&req
, NULL
, 2, PMU_POWER_CTRL
,
144 PMU_POW_BACKLIGHT
| PMU_POW_OFF
);
145 pmu_wait_complete(&req
);
147 __pmu_backlight_update_status(pmac_backlight
);
149 spin_unlock_irqrestore(&pmu_backlight_lock
, flags
);
151 #endif /* CONFIG_PM */
153 void __init
pmu_backlight_init()
155 struct backlight_device
*bd
;
159 /* Special case for the old PowerBook since I can't test on it */
161 machine_is_compatible("AAPL,3400/2400") ||
162 machine_is_compatible("AAPL,3500");
165 !pmac_has_backlight_type("pmu") &&
166 !machine_is_compatible("AAPL,PowerBook1998") &&
167 !machine_is_compatible("PowerBook1,1"))
170 snprintf(name
, sizeof(name
), "pmubl");
172 bd
= backlight_device_register(name
, NULL
, NULL
, &pmu_backlight_data
);
174 printk(KERN_ERR
"PMU Backlight registration failed\n");
177 <<<<<<< HEAD
:drivers
/macintosh
/via
-pmu
-backlight
.c
180 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/macintosh
/via
-pmu
-backlight
.c
181 bd
->props
.max_brightness
= FB_BACKLIGHT_LEVELS
- 1;
182 pmu_backlight_init_curve(0x7F, 0x46, 0x0E);
184 level
= bd
->props
.max_brightness
;
187 /* read autosaved value if available */
188 struct adb_request req
;
189 pmu_request(&req
, NULL
, 2, 0xd9, 0);
190 pmu_wait_complete(&req
);
192 level
= pmu_backlight_curve_lookup(
193 (req
.reply
[0] >> 4) *
194 bd
->props
.max_brightness
/ 15);
197 bd
->props
.brightness
= level
;
198 bd
->props
.power
= FB_BLANK_UNBLANK
;
199 backlight_update_status(bd
);
201 printk(KERN_INFO
"PMU Backlight initialized (%s)\n", name
);