Merge branch 'linus' into x86/quirks
[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
22 static int corgibl_intensity;
23 static struct backlight_properties corgibl_data;
24 static struct backlight_device *corgi_backlight_device;
25 static struct generic_bl_info *bl_machinfo;
26
27 static unsigned long corgibl_flags;
28 #define CORGIBL_SUSPENDED     0x01
29 #define CORGIBL_BATTLOW       0x02
30
31 static int corgibl_send_intensity(struct backlight_device *bd)
32 {
33         int intensity = bd->props.brightness;
34
35         if (bd->props.power != FB_BLANK_UNBLANK)
36                 intensity = 0;
37         if (bd->props.fb_blank != FB_BLANK_UNBLANK)
38                 intensity = 0;
39         if (corgibl_flags & CORGIBL_SUSPENDED)
40                 intensity = 0;
41         if (corgibl_flags & CORGIBL_BATTLOW)
42                 intensity &= bl_machinfo->limit_mask;
43
44         bl_machinfo->set_bl_intensity(intensity);
45
46         corgibl_intensity = intensity;
47
48         if (bl_machinfo->kick_battery)
49                 bl_machinfo->kick_battery();
50
51         return 0;
52 }
53
54 #ifdef CONFIG_PM
55 static int corgibl_suspend(struct platform_device *pdev, pm_message_t state)
56 {
57         struct backlight_device *bd = platform_get_drvdata(pdev);
58
59         corgibl_flags |= CORGIBL_SUSPENDED;
60         backlight_update_status(bd);
61         return 0;
62 }
63
64 static int corgibl_resume(struct platform_device *pdev)
65 {
66         struct backlight_device *bd = platform_get_drvdata(pdev);
67
68         corgibl_flags &= ~CORGIBL_SUSPENDED;
69         backlight_update_status(bd);
70         return 0;
71 }
72 #else
73 #define corgibl_suspend NULL
74 #define corgibl_resume  NULL
75 #endif
76
77 static int corgibl_get_intensity(struct backlight_device *bd)
78 {
79         return corgibl_intensity;
80 }
81
82 /*
83  * Called when the battery is low to limit the backlight intensity.
84  * If limit==0 clear any limit, otherwise limit the intensity
85  */
86 void corgibl_limit_intensity(int limit)
87 {
88         if (limit)
89                 corgibl_flags |= CORGIBL_BATTLOW;
90         else
91                 corgibl_flags &= ~CORGIBL_BATTLOW;
92         backlight_update_status(corgi_backlight_device);
93 }
94 EXPORT_SYMBOL(corgibl_limit_intensity);
95
96
97 static struct backlight_ops corgibl_ops = {
98         .get_brightness = corgibl_get_intensity,
99         .update_status  = corgibl_send_intensity,
100 };
101
102 static int corgibl_probe(struct platform_device *pdev)
103 {
104         struct generic_bl_info *machinfo = pdev->dev.platform_data;
105         const char *name = "generic-bl";
106
107         bl_machinfo = machinfo;
108         if (!machinfo->limit_mask)
109                 machinfo->limit_mask = -1;
110
111         if (machinfo->name)
112                 name = machinfo->name;
113
114         corgi_backlight_device = backlight_device_register (name,
115                 &pdev->dev, NULL, &corgibl_ops);
116         if (IS_ERR (corgi_backlight_device))
117                 return PTR_ERR (corgi_backlight_device);
118
119         platform_set_drvdata(pdev, corgi_backlight_device);
120
121         corgi_backlight_device->props.max_brightness = machinfo->max_intensity;
122         corgi_backlight_device->props.power = FB_BLANK_UNBLANK;
123         corgi_backlight_device->props.brightness = machinfo->default_intensity;
124         backlight_update_status(corgi_backlight_device);
125
126         printk("Corgi Backlight Driver Initialized.\n");
127         return 0;
128 }
129
130 static int corgibl_remove(struct platform_device *pdev)
131 {
132         struct backlight_device *bd = platform_get_drvdata(pdev);
133
134         corgibl_data.power = 0;
135         corgibl_data.brightness = 0;
136         backlight_update_status(bd);
137
138         backlight_device_unregister(bd);
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   = "generic-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");