lcd.c 6.5 KB

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