lcd.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * LCD Lowlevel Control Abstraction
  3. *
  4. * Copyright (C) 2003,2004 Hewlett-Packard Company
  5. *
  6. */
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/device.h>
  10. #include <linux/lcd.h>
  11. #include <linux/notifier.h>
  12. #include <linux/ctype.h>
  13. #include <linux/err.h>
  14. #include <linux/fb.h>
  15. #if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \
  16. defined(CONFIG_LCD_CLASS_DEVICE_MODULE))
  17. /* This callback gets called when something important happens inside a
  18. * framebuffer driver. We're looking if that important event is blanking,
  19. * and if it is, we're switching lcd power as well ...
  20. */
  21. static int fb_notifier_callback(struct notifier_block *self,
  22. unsigned long event, void *data)
  23. {
  24. struct lcd_device *ld;
  25. struct fb_event *evdata = data;
  26. /* If we aren't interested in this event, skip it immediately ... */
  27. if (event != FB_EVENT_BLANK)
  28. return 0;
  29. ld = container_of(self, struct lcd_device, fb_notif);
  30. down(&ld->sem);
  31. if (ld->props)
  32. if (!ld->props->check_fb || ld->props->check_fb(evdata->info))
  33. ld->props->set_power(ld, *(int *)evdata->data);
  34. up(&ld->sem);
  35. return 0;
  36. }
  37. static int lcd_register_fb(struct lcd_device *ld)
  38. {
  39. memset(&ld->fb_notif, 0, sizeof(&ld->fb_notif));
  40. ld->fb_notif.notifier_call = fb_notifier_callback;
  41. return fb_register_client(&ld->fb_notif);
  42. }
  43. static void lcd_unregister_fb(struct lcd_device *ld)
  44. {
  45. fb_unregister_client(&ld->fb_notif);
  46. }
  47. #else
  48. static int lcd_register_fb(struct lcd_device *ld)
  49. {
  50. return 0;
  51. }
  52. static inline void lcd_unregister_fb(struct lcd_device *ld)
  53. {
  54. }
  55. #endif /* CONFIG_FB */
  56. static ssize_t lcd_show_power(struct class_device *cdev, char *buf)
  57. {
  58. int rc;
  59. struct lcd_device *ld = to_lcd_device(cdev);
  60. down(&ld->sem);
  61. if (likely(ld->props && ld->props->get_power))
  62. rc = sprintf(buf, "%d\n", ld->props->get_power(ld));
  63. else
  64. rc = -ENXIO;
  65. up(&ld->sem);
  66. return rc;
  67. }
  68. static ssize_t lcd_store_power(struct class_device *cdev, const char *buf, size_t count)
  69. {
  70. int rc = -ENXIO;
  71. char *endp;
  72. struct lcd_device *ld = to_lcd_device(cdev);
  73. int power = simple_strtoul(buf, &endp, 0);
  74. size_t size = endp - buf;
  75. if (*endp && isspace(*endp))
  76. size++;
  77. if (size != count)
  78. return -EINVAL;
  79. down(&ld->sem);
  80. if (likely(ld->props && ld->props->set_power)) {
  81. pr_debug("lcd: set power to %d\n", power);
  82. ld->props->set_power(ld, power);
  83. rc = count;
  84. }
  85. up(&ld->sem);
  86. return rc;
  87. }
  88. static ssize_t lcd_show_contrast(struct class_device *cdev, char *buf)
  89. {
  90. int rc = -ENXIO;
  91. struct lcd_device *ld = to_lcd_device(cdev);
  92. down(&ld->sem);
  93. if (likely(ld->props && ld->props->get_contrast))
  94. rc = sprintf(buf, "%d\n", ld->props->get_contrast(ld));
  95. up(&ld->sem);
  96. return rc;
  97. }
  98. static ssize_t lcd_store_contrast(struct class_device *cdev, const char *buf, size_t count)
  99. {
  100. int rc = -ENXIO;
  101. char *endp;
  102. struct lcd_device *ld = to_lcd_device(cdev);
  103. int contrast = simple_strtoul(buf, &endp, 0);
  104. size_t size = endp - buf;
  105. if (*endp && isspace(*endp))
  106. size++;
  107. if (size != count)
  108. return -EINVAL;
  109. down(&ld->sem);
  110. if (likely(ld->props && ld->props->set_contrast)) {
  111. pr_debug("lcd: set contrast to %d\n", contrast);
  112. ld->props->set_contrast(ld, contrast);
  113. rc = count;
  114. }
  115. up(&ld->sem);
  116. return rc;
  117. }
  118. static ssize_t lcd_show_max_contrast(struct class_device *cdev, char *buf)
  119. {
  120. int rc = -ENXIO;
  121. struct lcd_device *ld = to_lcd_device(cdev);
  122. down(&ld->sem);
  123. if (likely(ld->props))
  124. rc = sprintf(buf, "%d\n", ld->props->max_contrast);
  125. up(&ld->sem);
  126. return rc;
  127. }
  128. static void lcd_class_release(struct class_device *dev)
  129. {
  130. struct lcd_device *ld = to_lcd_device(dev);
  131. kfree(ld);
  132. }
  133. static struct class lcd_class = {
  134. .name = "lcd",
  135. .release = lcd_class_release,
  136. };
  137. #define DECLARE_ATTR(_name,_mode,_show,_store) \
  138. { \
  139. .attr = { .name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE }, \
  140. .show = _show, \
  141. .store = _store, \
  142. }
  143. static const struct class_device_attribute lcd_class_device_attributes[] = {
  144. DECLARE_ATTR(power, 0644, lcd_show_power, lcd_store_power),
  145. DECLARE_ATTR(contrast, 0644, lcd_show_contrast, lcd_store_contrast),
  146. DECLARE_ATTR(max_contrast, 0444, lcd_show_max_contrast, NULL),
  147. };
  148. /**
  149. * lcd_device_register - register a new object of lcd_device class.
  150. * @name: the name of the new object(must be the same as the name of the
  151. * respective framebuffer device).
  152. * @devdata: an optional pointer to be stored in the class_device. The
  153. * methods may retrieve it by using class_get_devdata(ld->class_dev).
  154. * @lp: the lcd properties structure.
  155. *
  156. * Creates and registers a new lcd class_device. Returns either an ERR_PTR()
  157. * or a pointer to the newly allocated device.
  158. */
  159. struct lcd_device *lcd_device_register(const char *name, void *devdata,
  160. struct lcd_properties *lp)
  161. {
  162. int i, rc;
  163. struct lcd_device *new_ld;
  164. pr_debug("lcd_device_register: name=%s\n", name);
  165. new_ld = kmalloc(sizeof(struct lcd_device), GFP_KERNEL);
  166. if (unlikely(!new_ld))
  167. return ERR_PTR(-ENOMEM);
  168. init_MUTEX(&new_ld->sem);
  169. new_ld->props = lp;
  170. memset(&new_ld->class_dev, 0, sizeof(new_ld->class_dev));
  171. new_ld->class_dev.class = &lcd_class;
  172. strlcpy(new_ld->class_dev.class_id, name, KOBJ_NAME_LEN);
  173. class_set_devdata(&new_ld->class_dev, devdata);
  174. rc = class_device_register(&new_ld->class_dev);
  175. if (unlikely(rc)) {
  176. error: kfree(new_ld);
  177. return ERR_PTR(rc);
  178. }
  179. rc = lcd_register_fb(new_ld);
  180. if (unlikely(rc))
  181. goto error;
  182. for (i = 0; i < ARRAY_SIZE(lcd_class_device_attributes); i++) {
  183. rc = class_device_create_file(&new_ld->class_dev,
  184. &lcd_class_device_attributes[i]);
  185. if (unlikely(rc)) {
  186. while (--i >= 0)
  187. class_device_remove_file(&new_ld->class_dev,
  188. &lcd_class_device_attributes[i]);
  189. class_device_unregister(&new_ld->class_dev);
  190. /* No need to kfree(new_ld) since release() method was called */
  191. return ERR_PTR(rc);
  192. }
  193. }
  194. return new_ld;
  195. }
  196. EXPORT_SYMBOL(lcd_device_register);
  197. /**
  198. * lcd_device_unregister - unregisters a object of lcd_device class.
  199. * @ld: the lcd device object to be unregistered and freed.
  200. *
  201. * Unregisters a previously registered via lcd_device_register object.
  202. */
  203. void lcd_device_unregister(struct lcd_device *ld)
  204. {
  205. int i;
  206. if (!ld)
  207. return;
  208. pr_debug("lcd_device_unregister: name=%s\n", ld->class_dev.class_id);
  209. for (i = 0; i < ARRAY_SIZE(lcd_class_device_attributes); i++)
  210. class_device_remove_file(&ld->class_dev,
  211. &lcd_class_device_attributes[i]);
  212. down(&ld->sem);
  213. ld->props = NULL;
  214. up(&ld->sem);
  215. lcd_unregister_fb(ld);
  216. class_device_unregister(&ld->class_dev);
  217. }
  218. EXPORT_SYMBOL(lcd_device_unregister);
  219. static void __exit lcd_class_exit(void)
  220. {
  221. class_unregister(&lcd_class);
  222. }
  223. static int __init lcd_class_init(void)
  224. {
  225. return class_register(&lcd_class);
  226. }
  227. /*
  228. * if this is compiled into the kernel, we need to ensure that the
  229. * class is registered before users of the class try to register lcd's
  230. */
  231. postcore_initcall(lcd_class_init);
  232. module_exit(lcd_class_exit);
  233. MODULE_LICENSE("GPL");
  234. MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
  235. MODULE_DESCRIPTION("LCD Lowlevel Control Abstraction");