lcd.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * LCD Lowlevel Control Abstraction
  3. *
  4. * Copyright (C) 2003,2004 Hewlett-Packard Company
  5. *
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  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 <linux/slab.h>
  17. #if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \
  18. defined(CONFIG_LCD_CLASS_DEVICE_MODULE))
  19. /* This callback gets called when something important happens inside a
  20. * framebuffer driver. We're looking if that important event is blanking,
  21. * and if it is, we're switching lcd power as well ...
  22. */
  23. static int fb_notifier_callback(struct notifier_block *self,
  24. unsigned long event, void *data)
  25. {
  26. struct lcd_device *ld;
  27. struct fb_event *evdata = data;
  28. /* If we aren't interested in this event, skip it immediately ... */
  29. switch (event) {
  30. case FB_EVENT_BLANK:
  31. case FB_EVENT_MODE_CHANGE:
  32. case FB_EVENT_MODE_CHANGE_ALL:
  33. case FB_EARLY_EVENT_BLANK:
  34. case FB_R_EARLY_EVENT_BLANK:
  35. break;
  36. default:
  37. return 0;
  38. }
  39. ld = container_of(self, struct lcd_device, fb_notif);
  40. if (!ld->ops)
  41. return 0;
  42. mutex_lock(&ld->ops_lock);
  43. if (!ld->ops->check_fb || ld->ops->check_fb(ld, evdata->info)) {
  44. if (event == FB_EVENT_BLANK) {
  45. if (ld->ops->set_power)
  46. ld->ops->set_power(ld, *(int *)evdata->data);
  47. } else if (event == FB_EARLY_EVENT_BLANK) {
  48. if (ld->ops->early_set_power)
  49. ld->ops->early_set_power(ld,
  50. *(int *)evdata->data);
  51. } else if (event == FB_R_EARLY_EVENT_BLANK) {
  52. if (ld->ops->r_early_set_power)
  53. ld->ops->r_early_set_power(ld,
  54. *(int *)evdata->data);
  55. } else {
  56. if (ld->ops->set_mode)
  57. ld->ops->set_mode(ld, evdata->data);
  58. }
  59. }
  60. mutex_unlock(&ld->ops_lock);
  61. return 0;
  62. }
  63. static int lcd_register_fb(struct lcd_device *ld)
  64. {
  65. memset(&ld->fb_notif, 0, sizeof(ld->fb_notif));
  66. ld->fb_notif.notifier_call = fb_notifier_callback;
  67. return fb_register_client(&ld->fb_notif);
  68. }
  69. static void lcd_unregister_fb(struct lcd_device *ld)
  70. {
  71. fb_unregister_client(&ld->fb_notif);
  72. }
  73. #else
  74. static int lcd_register_fb(struct lcd_device *ld)
  75. {
  76. return 0;
  77. }
  78. static inline void lcd_unregister_fb(struct lcd_device *ld)
  79. {
  80. }
  81. #endif /* CONFIG_FB */
  82. static ssize_t lcd_show_power(struct device *dev, struct device_attribute *attr,
  83. char *buf)
  84. {
  85. int rc;
  86. struct lcd_device *ld = to_lcd_device(dev);
  87. mutex_lock(&ld->ops_lock);
  88. if (ld->ops && ld->ops->get_power)
  89. rc = sprintf(buf, "%d\n", ld->ops->get_power(ld));
  90. else
  91. rc = -ENXIO;
  92. mutex_unlock(&ld->ops_lock);
  93. return rc;
  94. }
  95. static ssize_t lcd_store_power(struct device *dev,
  96. struct device_attribute *attr, const char *buf, size_t count)
  97. {
  98. int rc = -ENXIO;
  99. struct lcd_device *ld = to_lcd_device(dev);
  100. unsigned long power;
  101. rc = kstrtoul(buf, 0, &power);
  102. if (rc)
  103. return rc;
  104. mutex_lock(&ld->ops_lock);
  105. if (ld->ops && ld->ops->set_power) {
  106. pr_debug("set power to %lu\n", power);
  107. ld->ops->set_power(ld, power);
  108. rc = count;
  109. }
  110. mutex_unlock(&ld->ops_lock);
  111. return rc;
  112. }
  113. static ssize_t lcd_show_contrast(struct device *dev,
  114. struct device_attribute *attr, char *buf)
  115. {
  116. int rc = -ENXIO;
  117. struct lcd_device *ld = to_lcd_device(dev);
  118. mutex_lock(&ld->ops_lock);
  119. if (ld->ops && ld->ops->get_contrast)
  120. rc = sprintf(buf, "%d\n", ld->ops->get_contrast(ld));
  121. mutex_unlock(&ld->ops_lock);
  122. return rc;
  123. }
  124. static ssize_t lcd_store_contrast(struct device *dev,
  125. struct device_attribute *attr, const char *buf, size_t count)
  126. {
  127. int rc = -ENXIO;
  128. struct lcd_device *ld = to_lcd_device(dev);
  129. unsigned long contrast;
  130. rc = kstrtoul(buf, 0, &contrast);
  131. if (rc)
  132. return rc;
  133. mutex_lock(&ld->ops_lock);
  134. if (ld->ops && ld->ops->set_contrast) {
  135. pr_debug("set contrast to %lu\n", contrast);
  136. ld->ops->set_contrast(ld, contrast);
  137. rc = count;
  138. }
  139. mutex_unlock(&ld->ops_lock);
  140. return rc;
  141. }
  142. static ssize_t lcd_show_max_contrast(struct device *dev,
  143. struct device_attribute *attr, char *buf)
  144. {
  145. struct lcd_device *ld = to_lcd_device(dev);
  146. return sprintf(buf, "%d\n", ld->props.max_contrast);
  147. }
  148. static struct class *lcd_class;
  149. static void lcd_device_release(struct device *dev)
  150. {
  151. struct lcd_device *ld = to_lcd_device(dev);
  152. kfree(ld);
  153. }
  154. static struct device_attribute lcd_device_attributes[] = {
  155. __ATTR(lcd_power, 0644, lcd_show_power, lcd_store_power),
  156. __ATTR(contrast, 0644, lcd_show_contrast, lcd_store_contrast),
  157. __ATTR(max_contrast, 0444, lcd_show_max_contrast, NULL),
  158. __ATTR_NULL,
  159. };
  160. /**
  161. * lcd_device_register - register a new object of lcd_device class.
  162. * @name: the name of the new object(must be the same as the name of the
  163. * respective framebuffer device).
  164. * @devdata: an optional pointer to be stored in the device. The
  165. * methods may retrieve it by using lcd_get_data(ld).
  166. * @ops: the lcd operations structure.
  167. *
  168. * Creates and registers a new lcd device. Returns either an ERR_PTR()
  169. * or a pointer to the newly allocated device.
  170. */
  171. struct lcd_device *lcd_device_register(const char *name, struct device *parent,
  172. void *devdata, struct lcd_ops *ops)
  173. {
  174. struct lcd_device *new_ld;
  175. int rc;
  176. pr_debug("lcd_device_register: name=%s\n", name);
  177. new_ld = kzalloc(sizeof(struct lcd_device), GFP_KERNEL);
  178. if (!new_ld)
  179. return ERR_PTR(-ENOMEM);
  180. mutex_init(&new_ld->ops_lock);
  181. mutex_init(&new_ld->update_lock);
  182. new_ld->dev.class = lcd_class;
  183. new_ld->dev.parent = parent;
  184. new_ld->dev.release = lcd_device_release;
  185. dev_set_name(&new_ld->dev, name);
  186. dev_set_drvdata(&new_ld->dev, devdata);
  187. rc = device_register(&new_ld->dev);
  188. if (rc) {
  189. kfree(new_ld);
  190. return ERR_PTR(rc);
  191. }
  192. rc = lcd_register_fb(new_ld);
  193. if (rc) {
  194. device_unregister(&new_ld->dev);
  195. return ERR_PTR(rc);
  196. }
  197. new_ld->ops = ops;
  198. return new_ld;
  199. }
  200. EXPORT_SYMBOL(lcd_device_register);
  201. /**
  202. * lcd_device_unregister - unregisters a object of lcd_device class.
  203. * @ld: the lcd device object to be unregistered and freed.
  204. *
  205. * Unregisters a previously registered via lcd_device_register object.
  206. */
  207. void lcd_device_unregister(struct lcd_device *ld)
  208. {
  209. if (!ld)
  210. return;
  211. mutex_lock(&ld->ops_lock);
  212. ld->ops = NULL;
  213. mutex_unlock(&ld->ops_lock);
  214. lcd_unregister_fb(ld);
  215. device_unregister(&ld->dev);
  216. }
  217. EXPORT_SYMBOL(lcd_device_unregister);
  218. static void __exit lcd_class_exit(void)
  219. {
  220. class_destroy(lcd_class);
  221. }
  222. static int __init lcd_class_init(void)
  223. {
  224. lcd_class = class_create(THIS_MODULE, "lcd");
  225. if (IS_ERR(lcd_class)) {
  226. pr_warn("Unable to create backlight class; errno = %ld\n",
  227. PTR_ERR(lcd_class));
  228. return PTR_ERR(lcd_class);
  229. }
  230. lcd_class->dev_attrs = lcd_device_attributes;
  231. return 0;
  232. }
  233. /*
  234. * if this is compiled into the kernel, we need to ensure that the
  235. * class is registered before users of the class try to register lcd's
  236. */
  237. postcore_initcall(lcd_class_init);
  238. module_exit(lcd_class_exit);
  239. MODULE_LICENSE("GPL");
  240. MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
  241. MODULE_DESCRIPTION("LCD Lowlevel Control Abstraction");