lcd.c 6.4 KB

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