lcd.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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_power_show(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_power_store(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 DEVICE_ATTR_RW(lcd_power);
  115. static ssize_t contrast_show(struct device *dev,
  116. struct device_attribute *attr, char *buf)
  117. {
  118. int rc = -ENXIO;
  119. struct lcd_device *ld = to_lcd_device(dev);
  120. mutex_lock(&ld->ops_lock);
  121. if (ld->ops && ld->ops->get_contrast)
  122. rc = sprintf(buf, "%d\n", ld->ops->get_contrast(ld));
  123. mutex_unlock(&ld->ops_lock);
  124. return rc;
  125. }
  126. static ssize_t contrast_store(struct device *dev,
  127. struct device_attribute *attr, const char *buf, size_t count)
  128. {
  129. int rc;
  130. struct lcd_device *ld = to_lcd_device(dev);
  131. unsigned long contrast;
  132. rc = kstrtoul(buf, 0, &contrast);
  133. if (rc)
  134. return rc;
  135. rc = -ENXIO;
  136. mutex_lock(&ld->ops_lock);
  137. if (ld->ops && ld->ops->set_contrast) {
  138. pr_debug("set contrast to %lu\n", contrast);
  139. ld->ops->set_contrast(ld, contrast);
  140. rc = count;
  141. }
  142. mutex_unlock(&ld->ops_lock);
  143. return rc;
  144. }
  145. static DEVICE_ATTR_RW(contrast);
  146. static ssize_t max_contrast_show(struct device *dev,
  147. struct device_attribute *attr, char *buf)
  148. {
  149. struct lcd_device *ld = to_lcd_device(dev);
  150. return sprintf(buf, "%d\n", ld->props.max_contrast);
  151. }
  152. static DEVICE_ATTR_RO(max_contrast);
  153. static struct class *lcd_class;
  154. static void lcd_device_release(struct device *dev)
  155. {
  156. struct lcd_device *ld = to_lcd_device(dev);
  157. kfree(ld);
  158. }
  159. static struct attribute *lcd_device_attrs[] = {
  160. &dev_attr_lcd_power.attr,
  161. &dev_attr_contrast.attr,
  162. &dev_attr_max_contrast.attr,
  163. NULL,
  164. };
  165. ATTRIBUTE_GROUPS(lcd_device);
  166. /**
  167. * lcd_device_register - register a new object of lcd_device class.
  168. * @name: the name of the new object(must be the same as the name of the
  169. * respective framebuffer device).
  170. * @devdata: an optional pointer to be stored in the device. The
  171. * methods may retrieve it by using lcd_get_data(ld).
  172. * @ops: the lcd operations structure.
  173. *
  174. * Creates and registers a new lcd device. Returns either an ERR_PTR()
  175. * or a pointer to the newly allocated device.
  176. */
  177. struct lcd_device *lcd_device_register(const char *name, struct device *parent,
  178. void *devdata, struct lcd_ops *ops)
  179. {
  180. struct lcd_device *new_ld;
  181. int rc;
  182. pr_debug("lcd_device_register: name=%s\n", name);
  183. new_ld = kzalloc(sizeof(struct lcd_device), GFP_KERNEL);
  184. if (!new_ld)
  185. return ERR_PTR(-ENOMEM);
  186. mutex_init(&new_ld->ops_lock);
  187. mutex_init(&new_ld->update_lock);
  188. new_ld->dev.class = lcd_class;
  189. new_ld->dev.parent = parent;
  190. new_ld->dev.release = lcd_device_release;
  191. dev_set_name(&new_ld->dev, "%s", name);
  192. dev_set_drvdata(&new_ld->dev, devdata);
  193. new_ld->ops = ops;
  194. rc = device_register(&new_ld->dev);
  195. if (rc) {
  196. put_device(&new_ld->dev);
  197. return ERR_PTR(rc);
  198. }
  199. rc = lcd_register_fb(new_ld);
  200. if (rc) {
  201. device_unregister(&new_ld->dev);
  202. return ERR_PTR(rc);
  203. }
  204. return new_ld;
  205. }
  206. EXPORT_SYMBOL(lcd_device_register);
  207. /**
  208. * lcd_device_unregister - unregisters a object of lcd_device class.
  209. * @ld: the lcd device object to be unregistered and freed.
  210. *
  211. * Unregisters a previously registered via lcd_device_register object.
  212. */
  213. void lcd_device_unregister(struct lcd_device *ld)
  214. {
  215. if (!ld)
  216. return;
  217. mutex_lock(&ld->ops_lock);
  218. ld->ops = NULL;
  219. mutex_unlock(&ld->ops_lock);
  220. lcd_unregister_fb(ld);
  221. device_unregister(&ld->dev);
  222. }
  223. EXPORT_SYMBOL(lcd_device_unregister);
  224. static void devm_lcd_device_release(struct device *dev, void *res)
  225. {
  226. struct lcd_device *lcd = *(struct lcd_device **)res;
  227. lcd_device_unregister(lcd);
  228. }
  229. static int devm_lcd_device_match(struct device *dev, void *res, void *data)
  230. {
  231. struct lcd_device **r = res;
  232. return *r == data;
  233. }
  234. /**
  235. * devm_lcd_device_register - resource managed lcd_device_register()
  236. * @dev: the device to register
  237. * @name: the name of the device
  238. * @parent: a pointer to the parent device
  239. * @devdata: an optional pointer to be stored for private driver use
  240. * @ops: the lcd operations structure
  241. *
  242. * @return a struct lcd on success, or an ERR_PTR on error
  243. *
  244. * Managed lcd_device_register(). The lcd_device returned from this function
  245. * are automatically freed on driver detach. See lcd_device_register()
  246. * for more information.
  247. */
  248. struct lcd_device *devm_lcd_device_register(struct device *dev,
  249. const char *name, struct device *parent,
  250. void *devdata, struct lcd_ops *ops)
  251. {
  252. struct lcd_device **ptr, *lcd;
  253. ptr = devres_alloc(devm_lcd_device_release, sizeof(*ptr), GFP_KERNEL);
  254. if (!ptr)
  255. return ERR_PTR(-ENOMEM);
  256. lcd = lcd_device_register(name, parent, devdata, ops);
  257. if (!IS_ERR(lcd)) {
  258. *ptr = lcd;
  259. devres_add(dev, ptr);
  260. } else {
  261. devres_free(ptr);
  262. }
  263. return lcd;
  264. }
  265. EXPORT_SYMBOL(devm_lcd_device_register);
  266. /**
  267. * devm_lcd_device_unregister - resource managed lcd_device_unregister()
  268. * @dev: the device to unregister
  269. * @ld: the lcd device to unregister
  270. *
  271. * Deallocated a lcd allocated with devm_lcd_device_register(). Normally
  272. * this function will not need to be called and the resource management
  273. * code will ensure that the resource is freed.
  274. */
  275. void devm_lcd_device_unregister(struct device *dev, struct lcd_device *ld)
  276. {
  277. int rc;
  278. rc = devres_release(dev, devm_lcd_device_release,
  279. devm_lcd_device_match, ld);
  280. WARN_ON(rc);
  281. }
  282. EXPORT_SYMBOL(devm_lcd_device_unregister);
  283. static void __exit lcd_class_exit(void)
  284. {
  285. class_destroy(lcd_class);
  286. }
  287. static int __init lcd_class_init(void)
  288. {
  289. lcd_class = class_create(THIS_MODULE, "lcd");
  290. if (IS_ERR(lcd_class)) {
  291. pr_warn("Unable to create backlight class; errno = %ld\n",
  292. PTR_ERR(lcd_class));
  293. return PTR_ERR(lcd_class);
  294. }
  295. lcd_class->dev_groups = lcd_device_groups;
  296. return 0;
  297. }
  298. /*
  299. * if this is compiled into the kernel, we need to ensure that the
  300. * class is registered before users of the class try to register lcd's
  301. */
  302. postcore_initcall(lcd_class_init);
  303. module_exit(lcd_class_exit);
  304. MODULE_LICENSE("GPL");
  305. MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
  306. MODULE_DESCRIPTION("LCD Lowlevel Control Abstraction");