lcd.c 6.6 KB

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