lcd.c 6.5 KB

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