lcd.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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;
  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. rc = -ENXIO;
  105. mutex_lock(&ld->ops_lock);
  106. if (ld->ops && ld->ops->set_power) {
  107. pr_debug("set power to %lu\n", power);
  108. ld->ops->set_power(ld, power);
  109. rc = count;
  110. }
  111. mutex_unlock(&ld->ops_lock);
  112. return rc;
  113. }
  114. static ssize_t lcd_show_contrast(struct device *dev,
  115. struct device_attribute *attr, char *buf)
  116. {
  117. int rc = -ENXIO;
  118. struct lcd_device *ld = to_lcd_device(dev);
  119. mutex_lock(&ld->ops_lock);
  120. if (ld->ops && ld->ops->get_contrast)
  121. rc = sprintf(buf, "%d\n", ld->ops->get_contrast(ld));
  122. mutex_unlock(&ld->ops_lock);
  123. return rc;
  124. }
  125. static ssize_t lcd_store_contrast(struct device *dev,
  126. struct device_attribute *attr, const char *buf, size_t count)
  127. {
  128. int rc;
  129. struct lcd_device *ld = to_lcd_device(dev);
  130. unsigned long contrast;
  131. rc = kstrtoul(buf, 0, &contrast);
  132. if (rc)
  133. return rc;
  134. rc = -ENXIO;
  135. mutex_lock(&ld->ops_lock);
  136. if (ld->ops && ld->ops->set_contrast) {
  137. pr_debug("set contrast to %lu\n", contrast);
  138. ld->ops->set_contrast(ld, contrast);
  139. rc = count;
  140. }
  141. mutex_unlock(&ld->ops_lock);
  142. return rc;
  143. }
  144. static ssize_t lcd_show_max_contrast(struct device *dev,
  145. struct device_attribute *attr, char *buf)
  146. {
  147. struct lcd_device *ld = to_lcd_device(dev);
  148. return sprintf(buf, "%d\n", ld->props.max_contrast);
  149. }
  150. static struct class *lcd_class;
  151. static void lcd_device_release(struct device *dev)
  152. {
  153. struct lcd_device *ld = to_lcd_device(dev);
  154. kfree(ld);
  155. }
  156. static struct device_attribute lcd_device_attributes[] = {
  157. __ATTR(lcd_power, 0644, lcd_show_power, lcd_store_power),
  158. __ATTR(contrast, 0644, lcd_show_contrast, lcd_store_contrast),
  159. __ATTR(max_contrast, 0444, lcd_show_max_contrast, NULL),
  160. __ATTR_NULL,
  161. };
  162. /**
  163. * lcd_device_register - register a new object of lcd_device class.
  164. * @name: the name of the new object(must be the same as the name of the
  165. * respective framebuffer device).
  166. * @devdata: an optional pointer to be stored in the device. The
  167. * methods may retrieve it by using lcd_get_data(ld).
  168. * @ops: the lcd operations structure.
  169. *
  170. * Creates and registers a new lcd device. Returns either an ERR_PTR()
  171. * or a pointer to the newly allocated device.
  172. */
  173. struct lcd_device *lcd_device_register(const char *name, struct device *parent,
  174. void *devdata, struct lcd_ops *ops)
  175. {
  176. struct lcd_device *new_ld;
  177. int rc;
  178. pr_debug("lcd_device_register: name=%s\n", name);
  179. new_ld = kzalloc(sizeof(struct lcd_device), GFP_KERNEL);
  180. if (!new_ld)
  181. return ERR_PTR(-ENOMEM);
  182. mutex_init(&new_ld->ops_lock);
  183. mutex_init(&new_ld->update_lock);
  184. new_ld->dev.class = lcd_class;
  185. new_ld->dev.parent = parent;
  186. new_ld->dev.release = lcd_device_release;
  187. dev_set_name(&new_ld->dev, name);
  188. dev_set_drvdata(&new_ld->dev, devdata);
  189. rc = device_register(&new_ld->dev);
  190. if (rc) {
  191. kfree(new_ld);
  192. return ERR_PTR(rc);
  193. }
  194. rc = lcd_register_fb(new_ld);
  195. if (rc) {
  196. device_unregister(&new_ld->dev);
  197. return ERR_PTR(rc);
  198. }
  199. new_ld->ops = ops;
  200. return new_ld;
  201. }
  202. EXPORT_SYMBOL(lcd_device_register);
  203. /**
  204. * lcd_device_unregister - unregisters a object of lcd_device class.
  205. * @ld: the lcd device object to be unregistered and freed.
  206. *
  207. * Unregisters a previously registered via lcd_device_register object.
  208. */
  209. void lcd_device_unregister(struct lcd_device *ld)
  210. {
  211. if (!ld)
  212. return;
  213. mutex_lock(&ld->ops_lock);
  214. ld->ops = NULL;
  215. mutex_unlock(&ld->ops_lock);
  216. lcd_unregister_fb(ld);
  217. device_unregister(&ld->dev);
  218. }
  219. EXPORT_SYMBOL(lcd_device_unregister);
  220. static void __exit lcd_class_exit(void)
  221. {
  222. class_destroy(lcd_class);
  223. }
  224. static int __init lcd_class_init(void)
  225. {
  226. lcd_class = class_create(THIS_MODULE, "lcd");
  227. if (IS_ERR(lcd_class)) {
  228. pr_warn("Unable to create backlight class; errno = %ld\n",
  229. PTR_ERR(lcd_class));
  230. return PTR_ERR(lcd_class);
  231. }
  232. lcd_class->dev_attrs = lcd_device_attributes;
  233. return 0;
  234. }
  235. /*
  236. * if this is compiled into the kernel, we need to ensure that the
  237. * class is registered before users of the class try to register lcd's
  238. */
  239. postcore_initcall(lcd_class_init);
  240. module_exit(lcd_class_exit);
  241. MODULE_LICENSE("GPL");
  242. MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
  243. MODULE_DESCRIPTION("LCD Lowlevel Control Abstraction");