]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * LCD Lowlevel Control Abstraction | |
3 | * | |
4 | * Copyright (C) 2003,2004 Hewlett-Packard Company | |
5 | * | |
6 | */ | |
7 | ||
1da177e4 LT |
8 | #include <linux/module.h> |
9 | #include <linux/init.h> | |
10 | #include <linux/device.h> | |
11 | #include <linux/lcd.h> | |
12 | #include <linux/notifier.h> | |
13 | #include <linux/ctype.h> | |
14 | #include <linux/err.h> | |
15 | #include <linux/fb.h> | |
5a0e3ad6 | 16 | #include <linux/slab.h> |
1da177e4 | 17 | |
3d5eeadd JS |
18 | #if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \ |
19 | defined(CONFIG_LCD_CLASS_DEVICE_MODULE)) | |
20 | /* This callback gets called when something important happens inside a | |
21 | * framebuffer driver. We're looking if that important event is blanking, | |
22 | * and if it is, we're switching lcd power as well ... | |
23 | */ | |
24 | static int fb_notifier_callback(struct notifier_block *self, | |
25 | unsigned long event, void *data) | |
26 | { | |
27 | struct lcd_device *ld; | |
28 | struct fb_event *evdata = data; | |
29 | ||
30 | /* If we aren't interested in this event, skip it immediately ... */ | |
faa312da EM |
31 | switch (event) { |
32 | case FB_EVENT_BLANK: | |
33 | case FB_EVENT_MODE_CHANGE: | |
34 | case FB_EVENT_MODE_CHANGE_ALL: | |
d54ad83f ID |
35 | case FB_EARLY_EVENT_BLANK: |
36 | case FB_R_EARLY_EVENT_BLANK: | |
faa312da EM |
37 | break; |
38 | default: | |
3d5eeadd | 39 | return 0; |
faa312da | 40 | } |
3d5eeadd JS |
41 | |
42 | ld = container_of(self, struct lcd_device, fb_notif); | |
faa312da EM |
43 | if (!ld->ops) |
44 | return 0; | |
45 | ||
599a52d1 | 46 | mutex_lock(&ld->ops_lock); |
faa312da | 47 | if (!ld->ops->check_fb || ld->ops->check_fb(ld, evdata->info)) { |
b3b4dc88 BD |
48 | if (event == FB_EVENT_BLANK) { |
49 | if (ld->ops->set_power) | |
50 | ld->ops->set_power(ld, *(int *)evdata->data); | |
d54ad83f ID |
51 | } else if (event == FB_EARLY_EVENT_BLANK) { |
52 | if (ld->ops->early_set_power) | |
53 | ld->ops->early_set_power(ld, | |
54 | *(int *)evdata->data); | |
55 | } else if (event == FB_R_EARLY_EVENT_BLANK) { | |
56 | if (ld->ops->r_early_set_power) | |
57 | ld->ops->r_early_set_power(ld, | |
58 | *(int *)evdata->data); | |
b3b4dc88 BD |
59 | } else { |
60 | if (ld->ops->set_mode) | |
61 | ld->ops->set_mode(ld, evdata->data); | |
62 | } | |
faa312da | 63 | } |
599a52d1 | 64 | mutex_unlock(&ld->ops_lock); |
3d5eeadd JS |
65 | return 0; |
66 | } | |
67 | ||
68 | static int lcd_register_fb(struct lcd_device *ld) | |
69 | { | |
1e0fa6bd | 70 | memset(&ld->fb_notif, 0, sizeof(ld->fb_notif)); |
3d5eeadd JS |
71 | ld->fb_notif.notifier_call = fb_notifier_callback; |
72 | return fb_register_client(&ld->fb_notif); | |
73 | } | |
74 | ||
75 | static void lcd_unregister_fb(struct lcd_device *ld) | |
76 | { | |
77 | fb_unregister_client(&ld->fb_notif); | |
78 | } | |
79 | #else | |
80 | static int lcd_register_fb(struct lcd_device *ld) | |
81 | { | |
82 | return 0; | |
83 | } | |
84 | ||
85 | static inline void lcd_unregister_fb(struct lcd_device *ld) | |
86 | { | |
87 | } | |
88 | #endif /* CONFIG_FB */ | |
89 | ||
655bfd7a RP |
90 | static ssize_t lcd_show_power(struct device *dev, struct device_attribute *attr, |
91 | char *buf) | |
1da177e4 LT |
92 | { |
93 | int rc; | |
655bfd7a | 94 | struct lcd_device *ld = to_lcd_device(dev); |
1da177e4 | 95 | |
599a52d1 RP |
96 | mutex_lock(&ld->ops_lock); |
97 | if (ld->ops && ld->ops->get_power) | |
98 | rc = sprintf(buf, "%d\n", ld->ops->get_power(ld)); | |
1da177e4 LT |
99 | else |
100 | rc = -ENXIO; | |
599a52d1 | 101 | mutex_unlock(&ld->ops_lock); |
1da177e4 LT |
102 | |
103 | return rc; | |
104 | } | |
105 | ||
655bfd7a RP |
106 | static ssize_t lcd_store_power(struct device *dev, |
107 | struct device_attribute *attr, const char *buf, size_t count) | |
1da177e4 | 108 | { |
68673afd | 109 | int rc = -ENXIO; |
655bfd7a | 110 | struct lcd_device *ld = to_lcd_device(dev); |
66655760 | 111 | unsigned long power; |
1da177e4 | 112 | |
66655760 JH |
113 | rc = kstrtoul(buf, 0, &power); |
114 | if (rc) | |
115 | return rc; | |
1da177e4 | 116 | |
599a52d1 RP |
117 | mutex_lock(&ld->ops_lock); |
118 | if (ld->ops && ld->ops->set_power) { | |
66655760 | 119 | pr_debug("lcd: set power to %lu\n", power); |
599a52d1 | 120 | ld->ops->set_power(ld, power); |
1da177e4 | 121 | rc = count; |
68673afd | 122 | } |
599a52d1 | 123 | mutex_unlock(&ld->ops_lock); |
1da177e4 LT |
124 | |
125 | return rc; | |
126 | } | |
127 | ||
655bfd7a RP |
128 | static ssize_t lcd_show_contrast(struct device *dev, |
129 | struct device_attribute *attr, char *buf) | |
1da177e4 | 130 | { |
68673afd | 131 | int rc = -ENXIO; |
655bfd7a | 132 | struct lcd_device *ld = to_lcd_device(dev); |
1da177e4 | 133 | |
599a52d1 RP |
134 | mutex_lock(&ld->ops_lock); |
135 | if (ld->ops && ld->ops->get_contrast) | |
136 | rc = sprintf(buf, "%d\n", ld->ops->get_contrast(ld)); | |
137 | mutex_unlock(&ld->ops_lock); | |
1da177e4 LT |
138 | |
139 | return rc; | |
140 | } | |
141 | ||
655bfd7a RP |
142 | static ssize_t lcd_store_contrast(struct device *dev, |
143 | struct device_attribute *attr, const char *buf, size_t count) | |
1da177e4 | 144 | { |
68673afd | 145 | int rc = -ENXIO; |
655bfd7a | 146 | struct lcd_device *ld = to_lcd_device(dev); |
66655760 | 147 | unsigned long contrast; |
1da177e4 | 148 | |
66655760 JH |
149 | rc = kstrtoul(buf, 0, &contrast); |
150 | if (rc) | |
151 | return rc; | |
1da177e4 | 152 | |
599a52d1 RP |
153 | mutex_lock(&ld->ops_lock); |
154 | if (ld->ops && ld->ops->set_contrast) { | |
66655760 | 155 | pr_debug("lcd: set contrast to %lu\n", contrast); |
599a52d1 | 156 | ld->ops->set_contrast(ld, contrast); |
1da177e4 | 157 | rc = count; |
68673afd | 158 | } |
599a52d1 | 159 | mutex_unlock(&ld->ops_lock); |
1da177e4 LT |
160 | |
161 | return rc; | |
162 | } | |
163 | ||
655bfd7a RP |
164 | static ssize_t lcd_show_max_contrast(struct device *dev, |
165 | struct device_attribute *attr, char *buf) | |
1da177e4 | 166 | { |
655bfd7a | 167 | struct lcd_device *ld = to_lcd_device(dev); |
1da177e4 | 168 | |
599a52d1 | 169 | return sprintf(buf, "%d\n", ld->props.max_contrast); |
1da177e4 LT |
170 | } |
171 | ||
0ad90efd | 172 | static struct class *lcd_class; |
655bfd7a RP |
173 | |
174 | static void lcd_device_release(struct device *dev) | |
1da177e4 LT |
175 | { |
176 | struct lcd_device *ld = to_lcd_device(dev); | |
177 | kfree(ld); | |
178 | } | |
179 | ||
655bfd7a RP |
180 | static struct device_attribute lcd_device_attributes[] = { |
181 | __ATTR(lcd_power, 0644, lcd_show_power, lcd_store_power), | |
182 | __ATTR(contrast, 0644, lcd_show_contrast, lcd_store_contrast), | |
183 | __ATTR(max_contrast, 0444, lcd_show_max_contrast, NULL), | |
184 | __ATTR_NULL, | |
1da177e4 LT |
185 | }; |
186 | ||
1da177e4 LT |
187 | /** |
188 | * lcd_device_register - register a new object of lcd_device class. | |
189 | * @name: the name of the new object(must be the same as the name of the | |
190 | * respective framebuffer device). | |
655bfd7a RP |
191 | * @devdata: an optional pointer to be stored in the device. The |
192 | * methods may retrieve it by using lcd_get_data(ld). | |
599a52d1 | 193 | * @ops: the lcd operations structure. |
1da177e4 | 194 | * |
655bfd7a | 195 | * Creates and registers a new lcd device. Returns either an ERR_PTR() |
1da177e4 LT |
196 | * or a pointer to the newly allocated device. |
197 | */ | |
655bfd7a RP |
198 | struct lcd_device *lcd_device_register(const char *name, struct device *parent, |
199 | void *devdata, struct lcd_ops *ops) | |
1da177e4 | 200 | { |
1da177e4 | 201 | struct lcd_device *new_ld; |
655bfd7a | 202 | int rc; |
1da177e4 LT |
203 | |
204 | pr_debug("lcd_device_register: name=%s\n", name); | |
205 | ||
599a52d1 | 206 | new_ld = kzalloc(sizeof(struct lcd_device), GFP_KERNEL); |
90968e8e | 207 | if (!new_ld) |
10ad1b73 | 208 | return ERR_PTR(-ENOMEM); |
1da177e4 | 209 | |
599a52d1 | 210 | mutex_init(&new_ld->ops_lock); |
28ee086d | 211 | mutex_init(&new_ld->update_lock); |
1da177e4 | 212 | |
655bfd7a RP |
213 | new_ld->dev.class = lcd_class; |
214 | new_ld->dev.parent = parent; | |
215 | new_ld->dev.release = lcd_device_release; | |
64dba9a9 | 216 | dev_set_name(&new_ld->dev, name); |
655bfd7a RP |
217 | dev_set_drvdata(&new_ld->dev, devdata); |
218 | ||
219 | rc = device_register(&new_ld->dev); | |
90968e8e | 220 | if (rc) { |
2fd5a154 | 221 | kfree(new_ld); |
1da177e4 LT |
222 | return ERR_PTR(rc); |
223 | } | |
224 | ||
3d5eeadd | 225 | rc = lcd_register_fb(new_ld); |
2fd5a154 | 226 | if (rc) { |
655bfd7a | 227 | device_unregister(&new_ld->dev); |
2fd5a154 DT |
228 | return ERR_PTR(rc); |
229 | } | |
1da177e4 | 230 | |
655bfd7a | 231 | new_ld->ops = ops; |
1da177e4 LT |
232 | |
233 | return new_ld; | |
234 | } | |
235 | EXPORT_SYMBOL(lcd_device_register); | |
236 | ||
237 | /** | |
238 | * lcd_device_unregister - unregisters a object of lcd_device class. | |
239 | * @ld: the lcd device object to be unregistered and freed. | |
240 | * | |
241 | * Unregisters a previously registered via lcd_device_register object. | |
242 | */ | |
243 | void lcd_device_unregister(struct lcd_device *ld) | |
244 | { | |
1da177e4 LT |
245 | if (!ld) |
246 | return; | |
247 | ||
599a52d1 RP |
248 | mutex_lock(&ld->ops_lock); |
249 | ld->ops = NULL; | |
250 | mutex_unlock(&ld->ops_lock); | |
3d5eeadd | 251 | lcd_unregister_fb(ld); |
655bfd7a RP |
252 | |
253 | device_unregister(&ld->dev); | |
1da177e4 LT |
254 | } |
255 | EXPORT_SYMBOL(lcd_device_unregister); | |
256 | ||
257 | static void __exit lcd_class_exit(void) | |
258 | { | |
655bfd7a | 259 | class_destroy(lcd_class); |
1da177e4 LT |
260 | } |
261 | ||
262 | static int __init lcd_class_init(void) | |
263 | { | |
655bfd7a RP |
264 | lcd_class = class_create(THIS_MODULE, "lcd"); |
265 | if (IS_ERR(lcd_class)) { | |
266 | printk(KERN_WARNING "Unable to create backlight class; errno = %ld\n", | |
267 | PTR_ERR(lcd_class)); | |
268 | return PTR_ERR(lcd_class); | |
269 | } | |
270 | ||
271 | lcd_class->dev_attrs = lcd_device_attributes; | |
272 | return 0; | |
1da177e4 LT |
273 | } |
274 | ||
275 | /* | |
276 | * if this is compiled into the kernel, we need to ensure that the | |
277 | * class is registered before users of the class try to register lcd's | |
278 | */ | |
279 | postcore_initcall(lcd_class_init); | |
280 | module_exit(lcd_class_exit); | |
281 | ||
282 | MODULE_LICENSE("GPL"); | |
283 | MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>"); | |
284 | MODULE_DESCRIPTION("LCD Lowlevel Control Abstraction"); |