lcd.c 6.6 KB

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