2 * LCD Lowlevel Control Abstraction
4 * Copyright (C) 2003,2004 Hewlett-Packard Company
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>
17 static ssize_t
lcd_show_power(struct class_device
*cdev
, char *buf
)
20 struct lcd_device
*ld
= to_lcd_device(cdev
);
23 if (likely(ld
->props
&& ld
->props
->get_power
))
24 rc
= sprintf(buf
, "%d\n", ld
->props
->get_power(ld
));
32 static ssize_t
lcd_store_power(struct class_device
*cdev
, const char *buf
, size_t count
)
36 struct lcd_device
*ld
= to_lcd_device(cdev
);
37 int power
= simple_strtoul(buf
, &endp
, 0);
38 size_t size
= endp
- buf
;
40 if (*endp
&& isspace(*endp
))
46 if (likely(ld
->props
&& ld
->props
->set_power
)) {
47 pr_debug("lcd: set power to %d\n", power
);
48 ld
->props
->set_power(ld
, power
);
56 static ssize_t
lcd_show_contrast(struct class_device
*cdev
, char *buf
)
59 struct lcd_device
*ld
= to_lcd_device(cdev
);
62 if (likely(ld
->props
&& ld
->props
->get_contrast
))
63 rc
= sprintf(buf
, "%d\n", ld
->props
->get_contrast(ld
));
69 static ssize_t
lcd_store_contrast(struct class_device
*cdev
, const char *buf
, size_t count
)
73 struct lcd_device
*ld
= to_lcd_device(cdev
);
74 int contrast
= simple_strtoul(buf
, &endp
, 0);
75 size_t size
= endp
- buf
;
77 if (*endp
&& isspace(*endp
))
83 if (likely(ld
->props
&& ld
->props
->set_contrast
)) {
84 pr_debug("lcd: set contrast to %d\n", contrast
);
85 ld
->props
->set_contrast(ld
, contrast
);
93 static ssize_t
lcd_show_max_contrast(struct class_device
*cdev
, char *buf
)
96 struct lcd_device
*ld
= to_lcd_device(cdev
);
99 if (likely(ld
->props
))
100 rc
= sprintf(buf
, "%d\n", ld
->props
->max_contrast
);
106 static void lcd_class_release(struct class_device
*dev
)
108 struct lcd_device
*ld
= to_lcd_device(dev
);
112 static struct class lcd_class
= {
114 .release
= lcd_class_release
,
117 #define DECLARE_ATTR(_name,_mode,_show,_store) \
119 .attr = { .name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE }, \
124 static struct class_device_attribute lcd_class_device_attributes
[] = {
125 DECLARE_ATTR(power
, 0644, lcd_show_power
, lcd_store_power
),
126 DECLARE_ATTR(contrast
, 0644, lcd_show_contrast
, lcd_store_contrast
),
127 DECLARE_ATTR(max_contrast
, 0444, lcd_show_max_contrast
, NULL
),
130 /* This callback gets called when something important happens inside a
131 * framebuffer driver. We're looking if that important event is blanking,
132 * and if it is, we're switching lcd power as well ...
134 static int fb_notifier_callback(struct notifier_block
*self
,
135 unsigned long event
, void *data
)
137 struct lcd_device
*ld
;
138 struct fb_event
*evdata
=(struct fb_event
*)data
;
140 /* If we aren't interested in this event, skip it immediately ... */
141 if (event
!= FB_EVENT_BLANK
)
144 ld
= container_of(self
, struct lcd_device
, fb_notif
);
147 if (!ld
->props
->check_fb
|| ld
->props
->check_fb(evdata
->info
))
148 ld
->props
->set_power(ld
, *(int *)evdata
->data
);
154 * lcd_device_register - register a new object of lcd_device class.
155 * @name: the name of the new object(must be the same as the name of the
156 * respective framebuffer device).
157 * @devdata: an optional pointer to be stored in the class_device. The
158 * methods may retrieve it by using class_get_devdata(ld->class_dev).
159 * @lp: the lcd properties structure.
161 * Creates and registers a new lcd class_device. Returns either an ERR_PTR()
162 * or a pointer to the newly allocated device.
164 struct lcd_device
*lcd_device_register(const char *name
, void *devdata
,
165 struct lcd_properties
*lp
)
168 struct lcd_device
*new_ld
;
170 pr_debug("lcd_device_register: name=%s\n", name
);
172 new_ld
= kmalloc(sizeof(struct lcd_device
), GFP_KERNEL
);
173 if (unlikely(!new_ld
))
174 return ERR_PTR(-ENOMEM
);
176 init_MUTEX(&new_ld
->sem
);
178 memset(&new_ld
->class_dev
, 0, sizeof(new_ld
->class_dev
));
179 new_ld
->class_dev
.class = &lcd_class
;
180 strlcpy(new_ld
->class_dev
.class_id
, name
, KOBJ_NAME_LEN
);
181 class_set_devdata(&new_ld
->class_dev
, devdata
);
183 rc
= class_device_register(&new_ld
->class_dev
);
185 error
: kfree(new_ld
);
189 memset(&new_ld
->fb_notif
, 0, sizeof(new_ld
->fb_notif
));
190 new_ld
->fb_notif
.notifier_call
= fb_notifier_callback
;
192 rc
= fb_register_client(&new_ld
->fb_notif
);
196 for (i
= 0; i
< ARRAY_SIZE(lcd_class_device_attributes
); i
++) {
197 rc
= class_device_create_file(&new_ld
->class_dev
,
198 &lcd_class_device_attributes
[i
]);
201 class_device_remove_file(&new_ld
->class_dev
,
202 &lcd_class_device_attributes
[i
]);
203 class_device_unregister(&new_ld
->class_dev
);
204 /* No need to kfree(new_ld) since release() method was called */
211 EXPORT_SYMBOL(lcd_device_register
);
214 * lcd_device_unregister - unregisters a object of lcd_device class.
215 * @ld: the lcd device object to be unregistered and freed.
217 * Unregisters a previously registered via lcd_device_register object.
219 void lcd_device_unregister(struct lcd_device
*ld
)
226 pr_debug("lcd_device_unregister: name=%s\n", ld
->class_dev
.class_id
);
228 for (i
= 0; i
< ARRAY_SIZE(lcd_class_device_attributes
); i
++)
229 class_device_remove_file(&ld
->class_dev
,
230 &lcd_class_device_attributes
[i
]);
236 fb_unregister_client(&ld
->fb_notif
);
238 class_device_unregister(&ld
->class_dev
);
240 EXPORT_SYMBOL(lcd_device_unregister
);
242 static void __exit
lcd_class_exit(void)
244 class_unregister(&lcd_class
);
247 static int __init
lcd_class_init(void)
249 return class_register(&lcd_class
);
253 * if this is compiled into the kernel, we need to ensure that the
254 * class is registered before users of the class try to register lcd's
256 postcore_initcall(lcd_class_init
);
257 module_exit(lcd_class_exit
);
259 MODULE_LICENSE("GPL");
260 MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
261 MODULE_DESCRIPTION("LCD Lowlevel Control Abstraction");