lcd.c 6.5 KB

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