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_properties pmu_backlight_data
;
19 static spinlock_t pmu_backlight_lock
;
22 static int pmu_backlight_get_level_brightness(struct fb_info
*info
,
27 /* Get and convert the value */
28 mutex_lock(&info
->bl_mutex
);
29 pmulevel
= info
->bl_curve
[level
] * FB_BACKLIGHT_MAX
/ MAX_PMU_LEVEL
;
30 mutex_unlock(&info
->bl_mutex
);
34 else if (pmulevel
> MAX_PMU_LEVEL
)
35 pmulevel
= MAX_PMU_LEVEL
;
40 static int pmu_backlight_update_status(struct backlight_device
*bd
)
42 struct fb_info
*info
= class_get_devdata(&bd
->class_dev
);
43 struct adb_request req
;
45 int level
= bd
->props
->brightness
;
47 spin_lock_irqsave(&pmu_backlight_lock
, flags
);
49 /* Don't update brightness when sleeping */
53 if (bd
->props
->power
!= FB_BLANK_UNBLANK
||
54 bd
->props
->fb_blank
!= FB_BLANK_UNBLANK
)
58 int pmulevel
= pmu_backlight_get_level_brightness(info
, level
);
60 pmu_request(&req
, NULL
, 2, PMU_BACKLIGHT_BRIGHT
, pmulevel
);
61 pmu_wait_complete(&req
);
63 pmu_request(&req
, NULL
, 2, PMU_POWER_CTRL
,
64 PMU_POW_BACKLIGHT
| PMU_POW_ON
);
65 pmu_wait_complete(&req
);
67 pmu_request(&req
, NULL
, 2, PMU_POWER_CTRL
,
68 PMU_POW_BACKLIGHT
| PMU_POW_OFF
);
69 pmu_wait_complete(&req
);
73 spin_unlock_irqrestore(&pmu_backlight_lock
, flags
);
78 static int pmu_backlight_get_brightness(struct backlight_device
*bd
)
80 return bd
->props
->brightness
;
83 static struct backlight_properties pmu_backlight_data
= {
85 .get_brightness
= pmu_backlight_get_brightness
,
86 .update_status
= pmu_backlight_update_status
,
87 .max_brightness
= (FB_BACKLIGHT_LEVELS
- 1),
91 static int pmu_backlight_sleep_call(struct pmu_sleep_notifier
*self
, int when
)
95 spin_lock_irqsave(&pmu_backlight_lock
, flags
);
98 case PBOOK_SLEEP_REQUEST
:
106 spin_unlock_irqrestore(&pmu_backlight_lock
, flags
);
108 return PBOOK_SLEEP_OK
;
111 static struct pmu_sleep_notifier pmu_backlight_sleep_notif
= {
112 .notifier_call
= pmu_backlight_sleep_call
,
116 void __init
pmu_backlight_init()
118 struct backlight_device
*bd
;
119 struct fb_info
*info
;
123 /* Special case for the old PowerBook since I can't test on it */
125 machine_is_compatible("AAPL,3400/2400") ||
126 machine_is_compatible("AAPL,3500");
129 !pmac_has_backlight_type("pmu") &&
130 !machine_is_compatible("AAPL,PowerBook1998") &&
131 !machine_is_compatible("PowerBook1,1"))
134 /* Actually, this is a hack, but I don't know of a better way
135 * to get the first framebuffer device.
137 info
= registered_fb
[0];
139 printk("pmubl: No framebuffer found\n");
143 snprintf(name
, sizeof(name
), "pmubl%d", info
->node
);
145 bd
= backlight_device_register(name
, info
, &pmu_backlight_data
);
147 printk("pmubl: Backlight registration failed\n");
151 mutex_lock(&info
->bl_mutex
);
153 fb_bl_default_curve(info
, 0x7F, 0x46, 0x0E);
154 mutex_unlock(&info
->bl_mutex
);
156 level
= pmu_backlight_data
.max_brightness
;
159 /* read autosaved value if available */
160 struct adb_request req
;
161 pmu_request(&req
, NULL
, 2, 0xd9, 0);
162 pmu_wait_complete(&req
);
164 mutex_lock(&info
->bl_mutex
);
165 level
= pmac_backlight_curve_lookup(info
,
166 (req
.reply
[0] >> 4) *
167 pmu_backlight_data
.max_brightness
/ 15);
168 mutex_unlock(&info
->bl_mutex
);
172 bd
->props
->brightness
= level
;
173 bd
->props
->power
= FB_BLANK_UNBLANK
;
174 bd
->props
->update_status(bd
);
177 mutex_lock(&pmac_backlight_mutex
);
180 mutex_unlock(&pmac_backlight_mutex
);
183 pmu_register_sleep_notifier(&pmu_backlight_sleep_notif
);
186 printk("pmubl: Backlight initialized (%s)\n", name
);