2 * LCD Lowlevel Control Abstraction
4 * Copyright (C) 2003,2004 Hewlett-Packard Company
8 #include <linux/version.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/device.h>
12 #include <linux/lcd.h>
13 #include <linux/notifier.h>
14 #include <linux/ctype.h>
15 #include <linux/err.h>
19 static ssize_t
lcd_show_power(struct class_device
*cdev
, char *buf
)
22 struct lcd_device
*ld
= to_lcd_device(cdev
);
25 if (likely(ld
->props
&& ld
->props
->get_power
))
26 rc
= sprintf(buf
, "%d\n", ld
->props
->get_power(ld
));
34 static ssize_t
lcd_store_power(struct class_device
*cdev
, const char *buf
, size_t count
)
38 struct lcd_device
*ld
= to_lcd_device(cdev
);
40 power
= simple_strtoul(buf
, &endp
, 0);
41 if (*endp
&& !isspace(*endp
))
45 if (likely(ld
->props
&& ld
->props
->set_power
)) {
46 pr_debug("lcd: set power to %d\n", power
);
47 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
));
71 static ssize_t
lcd_store_contrast(struct class_device
*cdev
, const char *buf
, size_t count
)
75 struct lcd_device
*ld
= to_lcd_device(cdev
);
77 contrast
= simple_strtoul(buf
, &endp
, 0);
78 if (*endp
&& !isspace(*endp
))
82 if (likely(ld
->props
&& ld
->props
->set_contrast
)) {
83 pr_debug("lcd: set contrast to %d\n", contrast
);
84 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
);
108 static void lcd_class_release(struct class_device
*dev
)
110 struct lcd_device
*ld
= to_lcd_device(dev
);
114 static struct class lcd_class
= {
116 .release
= lcd_class_release
,
119 #define DECLARE_ATTR(_name,_mode,_show,_store) \
121 .attr = { .name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE }, \
126 static struct class_device_attribute lcd_class_device_attributes
[] = {
127 DECLARE_ATTR(power
, 0644, lcd_show_power
, lcd_store_power
),
128 DECLARE_ATTR(contrast
, 0644, lcd_show_contrast
, lcd_store_contrast
),
129 DECLARE_ATTR(max_contrast
, 0444, lcd_show_max_contrast
, NULL
),
132 /* This callback gets called when something important happens inside a
133 * framebuffer driver. We're looking if that important event is blanking,
134 * and if it is, we're switching lcd power as well ...
136 static int fb_notifier_callback(struct notifier_block
*self
,
137 unsigned long event
, void *data
)
139 struct lcd_device
*ld
;
140 struct fb_event
*evdata
=(struct fb_event
*)data
;
142 /* If we aren't interested in this event, skip it immediately ... */
143 if (event
!= FB_EVENT_BLANK
)
146 ld
= container_of(self
, struct lcd_device
, fb_notif
);
149 if (!ld
->props
->check_fb
|| ld
->props
->check_fb(evdata
->info
))
150 ld
->props
->set_power(ld
, *(int *)evdata
->data
);
156 * lcd_device_register - register a new object of lcd_device class.
157 * @name: the name of the new object(must be the same as the name of the
158 * respective framebuffer device).
159 * @devdata: an optional pointer to be stored in the class_device. The
160 * methods may retrieve it by using class_get_devdata(ld->class_dev).
161 * @lp: the lcd properties structure.
163 * Creates and registers a new lcd class_device. Returns either an ERR_PTR()
164 * or a pointer to the newly allocated device.
166 struct lcd_device
*lcd_device_register(const char *name
, void *devdata
,
167 struct lcd_properties
*lp
)
170 struct lcd_device
*new_ld
;
172 pr_debug("lcd_device_register: name=%s\n", name
);
174 new_ld
= kmalloc(sizeof(struct lcd_device
), GFP_KERNEL
);
175 if (unlikely(!new_ld
))
176 return ERR_PTR(ENOMEM
);
178 init_MUTEX(&new_ld
->sem
);
180 memset(&new_ld
->class_dev
, 0, sizeof(new_ld
->class_dev
));
181 new_ld
->class_dev
.class = &lcd_class
;
182 strlcpy(new_ld
->class_dev
.class_id
, name
, KOBJ_NAME_LEN
);
183 class_set_devdata(&new_ld
->class_dev
, devdata
);
185 rc
= class_device_register(&new_ld
->class_dev
);
187 error
: kfree(new_ld
);
191 memset(&new_ld
->fb_notif
, 0, sizeof(new_ld
->fb_notif
));
192 new_ld
->fb_notif
.notifier_call
= fb_notifier_callback
;
194 rc
= fb_register_client(&new_ld
->fb_notif
);
198 for (i
= 0; i
< ARRAY_SIZE(lcd_class_device_attributes
); i
++) {
199 rc
= class_device_create_file(&new_ld
->class_dev
,
200 &lcd_class_device_attributes
[i
]);
203 class_device_remove_file(&new_ld
->class_dev
,
204 &lcd_class_device_attributes
[i
]);
205 class_device_unregister(&new_ld
->class_dev
);
206 /* No need to kfree(new_ld) since release() method was called */
213 EXPORT_SYMBOL(lcd_device_register
);
216 * lcd_device_unregister - unregisters a object of lcd_device class.
217 * @ld: the lcd device object to be unregistered and freed.
219 * Unregisters a previously registered via lcd_device_register object.
221 void lcd_device_unregister(struct lcd_device
*ld
)
228 pr_debug("lcd_device_unregister: name=%s\n", ld
->class_dev
.class_id
);
230 for (i
= 0; i
< ARRAY_SIZE(lcd_class_device_attributes
); i
++)
231 class_device_remove_file(&ld
->class_dev
,
232 &lcd_class_device_attributes
[i
]);
238 fb_unregister_client(&ld
->fb_notif
);
240 class_device_unregister(&ld
->class_dev
);
242 EXPORT_SYMBOL(lcd_device_unregister
);
244 static void __exit
lcd_class_exit(void)
246 class_unregister(&lcd_class
);
249 static int __init
lcd_class_init(void)
251 return class_register(&lcd_class
);
255 * if this is compiled into the kernel, we need to ensure that the
256 * class is registered before users of the class try to register lcd's
258 postcore_initcall(lcd_class_init
);
259 module_exit(lcd_class_exit
);
261 MODULE_LICENSE("GPL");
262 MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
263 MODULE_DESCRIPTION("LCD Lowlevel Control Abstraction");