Merge branch 'drm-patches' of master.kernel.org:/pub/scm/linux/kernel/git/airlied...
[linux-2.6] / drivers / video / backlight / corgi_bl.c
1 /*
2  *  Backlight Driver for Sharp Zaurus Handhelds (various models)
3  *
4  *  Copyright (c) 2004-2006 Richard Purdie
5  *
6  *  Based on Sharp's 2.4 Backlight Driver
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License version 2 as
10  *  published by the Free Software Foundation.
11  *
12  */
13
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/platform_device.h>
18 #include <linux/mutex.h>
19 #include <linux/fb.h>
20 #include <linux/backlight.h>
21 #include <asm/arch/sharpsl.h>
22 #include <asm/hardware/sharpsl_pm.h>
23
24 static int corgibl_intensity;
25 static DEFINE_MUTEX(bl_mutex);
26 static struct backlight_properties corgibl_data;
27 static struct backlight_device *corgi_backlight_device;
28 static struct corgibl_machinfo *bl_machinfo;
29
30 static unsigned long corgibl_flags;
31 #define CORGIBL_SUSPENDED     0x01
32 #define CORGIBL_BATTLOW       0x02
33
34 static int corgibl_send_intensity(struct backlight_device *bd)
35 {
36         void (*corgi_kick_batt)(void);
37         int intensity = bd->props->brightness;
38
39         if (bd->props->power != FB_BLANK_UNBLANK)
40                 intensity = 0;
41         if (bd->props->fb_blank != FB_BLANK_UNBLANK)
42                 intensity = 0;
43         if (corgibl_flags & CORGIBL_SUSPENDED)
44                 intensity = 0;
45         if (corgibl_flags & CORGIBL_BATTLOW)
46                 intensity &= bl_machinfo->limit_mask;
47
48         mutex_lock(&bl_mutex);
49         bl_machinfo->set_bl_intensity(intensity);
50         mutex_unlock(&bl_mutex);
51
52         corgibl_intensity = intensity;
53
54         corgi_kick_batt = symbol_get(sharpsl_battery_kick);
55         if (corgi_kick_batt) {
56                 corgi_kick_batt();
57                 symbol_put(sharpsl_battery_kick);
58         }
59
60         return 0;
61 }
62
63 #ifdef CONFIG_PM
64 static int corgibl_suspend(struct platform_device *dev, pm_message_t state)
65 {
66         corgibl_flags |= CORGIBL_SUSPENDED;
67         corgibl_send_intensity(corgi_backlight_device);
68         return 0;
69 }
70
71 static int corgibl_resume(struct platform_device *dev)
72 {
73         corgibl_flags &= ~CORGIBL_SUSPENDED;
74         corgibl_send_intensity(corgi_backlight_device);
75         return 0;
76 }
77 #else
78 #define corgibl_suspend NULL
79 #define corgibl_resume  NULL
80 #endif
81
82 static int corgibl_get_intensity(struct backlight_device *bd)
83 {
84         return corgibl_intensity;
85 }
86
87 static int corgibl_set_intensity(struct backlight_device *bd)
88 {
89         corgibl_send_intensity(corgi_backlight_device);
90         return 0;
91 }
92
93 /*
94  * Called when the battery is low to limit the backlight intensity.
95  * If limit==0 clear any limit, otherwise limit the intensity
96  */
97 void corgibl_limit_intensity(int limit)
98 {
99         if (limit)
100                 corgibl_flags |= CORGIBL_BATTLOW;
101         else
102                 corgibl_flags &= ~CORGIBL_BATTLOW;
103         corgibl_send_intensity(corgi_backlight_device);
104 }
105 EXPORT_SYMBOL(corgibl_limit_intensity);
106
107
108 static struct backlight_properties corgibl_data = {
109         .owner          = THIS_MODULE,
110         .get_brightness = corgibl_get_intensity,
111         .update_status  = corgibl_set_intensity,
112 };
113
114 static int corgibl_probe(struct platform_device *pdev)
115 {
116         struct corgibl_machinfo *machinfo = pdev->dev.platform_data;
117
118         bl_machinfo = machinfo;
119         corgibl_data.max_brightness = machinfo->max_intensity;
120         if (!machinfo->limit_mask)
121                 machinfo->limit_mask = -1;
122
123         corgi_backlight_device = backlight_device_register ("corgi-bl",
124                 NULL, &corgibl_data);
125         if (IS_ERR (corgi_backlight_device))
126                 return PTR_ERR (corgi_backlight_device);
127
128         corgibl_data.power = FB_BLANK_UNBLANK;
129         corgibl_data.brightness = machinfo->default_intensity;
130         corgibl_send_intensity(corgi_backlight_device);
131
132         printk("Corgi Backlight Driver Initialized.\n");
133         return 0;
134 }
135
136 static int corgibl_remove(struct platform_device *dev)
137 {
138         backlight_device_unregister(corgi_backlight_device);
139
140         printk("Corgi Backlight Driver Unloaded\n");
141         return 0;
142 }
143
144 static struct platform_driver corgibl_driver = {
145         .probe          = corgibl_probe,
146         .remove         = corgibl_remove,
147         .suspend        = corgibl_suspend,
148         .resume         = corgibl_resume,
149         .driver         = {
150                 .name   = "corgi-bl",
151         },
152 };
153
154 static int __init corgibl_init(void)
155 {
156         return platform_driver_register(&corgibl_driver);
157 }
158
159 static void __exit corgibl_exit(void)
160 {
161         platform_driver_unregister(&corgibl_driver);
162 }
163
164 module_init(corgibl_init);
165 module_exit(corgibl_exit);
166
167 MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
168 MODULE_DESCRIPTION("Corgi Backlight Driver");
169 MODULE_LICENSE("GPL");