lcd.c 6.5 KB

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