lcd.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * LCD Lowlevel Control Abstraction
  4. *
  5. * Copyright (C) 2003,2004 Hewlett-Packard Company
  6. *
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/device.h>
  12. #include <linux/lcd.h>
  13. #include <linux/notifier.h>
  14. #include <linux/ctype.h>
  15. #include <linux/err.h>
  16. #include <linux/fb.h>
  17. #include <linux/slab.h>
  18. #if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \
  19. defined(CONFIG_LCD_CLASS_DEVICE_MODULE))
  20. /* This callback gets called when something important happens inside a
  21. * framebuffer driver. We're looking if that important event is blanking,
  22. * and if it is, we're switching lcd power as well ...
  23. */
  24. static int fb_notifier_callback(struct notifier_block *self,
  25. unsigned long event, void *data)
  26. {
  27. struct lcd_device *ld;
  28. struct fb_event *evdata = data;
  29. ld = container_of(self, struct lcd_device, fb_notif);
  30. if (!ld->ops)
  31. return 0;
  32. mutex_lock(&ld->ops_lock);
  33. if (!ld->ops->check_fb || ld->ops->check_fb(ld, evdata->info)) {
  34. if (event == FB_EVENT_BLANK) {
  35. if (ld->ops->set_power)
  36. ld->ops->set_power(ld, *(int *)evdata->data);
  37. } else {
  38. if (ld->ops->set_mode)
  39. ld->ops->set_mode(ld, evdata->data);
  40. }
  41. }
  42. mutex_unlock(&ld->ops_lock);
  43. return 0;
  44. }
  45. static int lcd_register_fb(struct lcd_device *ld)
  46. {
  47. memset(&ld->fb_notif, 0, sizeof(ld->fb_notif));
  48. ld->fb_notif.notifier_call = fb_notifier_callback;
  49. return fb_register_client(&ld->fb_notif);
  50. }
  51. static void lcd_unregister_fb(struct lcd_device *ld)
  52. {
  53. fb_unregister_client(&ld->fb_notif);
  54. }
  55. #else
  56. static int lcd_register_fb(struct lcd_device *ld)
  57. {
  58. return 0;
  59. }
  60. static inline void lcd_unregister_fb(struct lcd_device *ld)
  61. {
  62. }
  63. #endif /* CONFIG_FB */
  64. static ssize_t lcd_power_show(struct device *dev, struct device_attribute *attr,
  65. char *buf)
  66. {
  67. int rc;
  68. struct lcd_device *ld = to_lcd_device(dev);
  69. mutex_lock(&ld->ops_lock);
  70. if (ld->ops && ld->ops->get_power)
  71. rc = sprintf(buf, "%d\n", ld->ops->get_power(ld));
  72. else
  73. rc = -ENXIO;
  74. mutex_unlock(&ld->ops_lock);
  75. return rc;
  76. }
  77. static ssize_t lcd_power_store(struct device *dev,
  78. struct device_attribute *attr, const char *buf, size_t count)
  79. {
  80. int rc;
  81. struct lcd_device *ld = to_lcd_device(dev);
  82. unsigned long power;
  83. rc = kstrtoul(buf, 0, &power);
  84. if (rc)
  85. return rc;
  86. rc = -ENXIO;
  87. mutex_lock(&ld->ops_lock);
  88. if (ld->ops && ld->ops->set_power) {
  89. pr_debug("set power to %lu\n", power);
  90. ld->ops->set_power(ld, power);
  91. rc = count;
  92. }
  93. mutex_unlock(&ld->ops_lock);
  94. return rc;
  95. }
  96. static DEVICE_ATTR_RW(lcd_power);
  97. static ssize_t contrast_show(struct device *dev,
  98. struct device_attribute *attr, char *buf)
  99. {
  100. int rc = -ENXIO;
  101. struct lcd_device *ld = to_lcd_device(dev);
  102. mutex_lock(&ld->ops_lock);
  103. if (ld->ops && ld->ops->get_contrast)
  104. rc = sprintf(buf, "%d\n", ld->ops->get_contrast(ld));
  105. mutex_unlock(&ld->ops_lock);
  106. return rc;
  107. }
  108. static ssize_t contrast_store(struct device *dev,
  109. struct device_attribute *attr, const char *buf, size_t count)
  110. {
  111. int rc;
  112. struct lcd_device *ld = to_lcd_device(dev);
  113. unsigned long contrast;
  114. rc = kstrtoul(buf, 0, &contrast);
  115. if (rc)
  116. return rc;
  117. rc = -ENXIO;
  118. mutex_lock(&ld->ops_lock);
  119. if (ld->ops && ld->ops->set_contrast) {
  120. pr_debug("set contrast to %lu\n", contrast);
  121. ld->ops->set_contrast(ld, contrast);
  122. rc = count;
  123. }
  124. mutex_unlock(&ld->ops_lock);
  125. return rc;
  126. }
  127. static DEVICE_ATTR_RW(contrast);
  128. static ssize_t max_contrast_show(struct device *dev,
  129. struct device_attribute *attr, char *buf)
  130. {
  131. struct lcd_device *ld = to_lcd_device(dev);
  132. return sprintf(buf, "%d\n", ld->props.max_contrast);
  133. }
  134. static DEVICE_ATTR_RO(max_contrast);
  135. static void lcd_device_release(struct device *dev)
  136. {
  137. struct lcd_device *ld = to_lcd_device(dev);
  138. kfree(ld);
  139. }
  140. static struct attribute *lcd_device_attrs[] = {
  141. &dev_attr_lcd_power.attr,
  142. &dev_attr_contrast.attr,
  143. &dev_attr_max_contrast.attr,
  144. NULL,
  145. };
  146. ATTRIBUTE_GROUPS(lcd_device);
  147. static const struct class lcd_class = {
  148. .name = "lcd",
  149. .dev_groups = lcd_device_groups,
  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. * @parent: pointer to the parent's struct device .
  156. * @devdata: an optional pointer to be stored in the device. The
  157. * methods may retrieve it by using lcd_get_data(ld).
  158. * @ops: the lcd operations structure.
  159. *
  160. * Creates and registers a new lcd device. Returns either an ERR_PTR()
  161. * or a pointer to the newly allocated device.
  162. */
  163. struct lcd_device *lcd_device_register(const char *name, struct device *parent,
  164. void *devdata, const struct lcd_ops *ops)
  165. {
  166. struct lcd_device *new_ld;
  167. int rc;
  168. pr_debug("lcd_device_register: name=%s\n", name);
  169. new_ld = kzalloc(sizeof(struct lcd_device), GFP_KERNEL);
  170. if (!new_ld)
  171. return ERR_PTR(-ENOMEM);
  172. mutex_init(&new_ld->ops_lock);
  173. mutex_init(&new_ld->update_lock);
  174. new_ld->dev.class = &lcd_class;
  175. new_ld->dev.parent = parent;
  176. new_ld->dev.release = lcd_device_release;
  177. dev_set_name(&new_ld->dev, "%s", name);
  178. dev_set_drvdata(&new_ld->dev, devdata);
  179. new_ld->ops = ops;
  180. rc = device_register(&new_ld->dev);
  181. if (rc) {
  182. put_device(&new_ld->dev);
  183. return ERR_PTR(rc);
  184. }
  185. rc = lcd_register_fb(new_ld);
  186. if (rc) {
  187. device_unregister(&new_ld->dev);
  188. return ERR_PTR(rc);
  189. }
  190. return new_ld;
  191. }
  192. EXPORT_SYMBOL(lcd_device_register);
  193. /**
  194. * lcd_device_unregister - unregisters a object of lcd_device class.
  195. * @ld: the lcd device object to be unregistered and freed.
  196. *
  197. * Unregisters a previously registered via lcd_device_register object.
  198. */
  199. void lcd_device_unregister(struct lcd_device *ld)
  200. {
  201. if (!ld)
  202. return;
  203. mutex_lock(&ld->ops_lock);
  204. ld->ops = NULL;
  205. mutex_unlock(&ld->ops_lock);
  206. lcd_unregister_fb(ld);
  207. device_unregister(&ld->dev);
  208. }
  209. EXPORT_SYMBOL(lcd_device_unregister);
  210. static void devm_lcd_device_release(struct device *dev, void *res)
  211. {
  212. struct lcd_device *lcd = *(struct lcd_device **)res;
  213. lcd_device_unregister(lcd);
  214. }
  215. static int devm_lcd_device_match(struct device *dev, void *res, void *data)
  216. {
  217. struct lcd_device **r = res;
  218. return *r == data;
  219. }
  220. /**
  221. * devm_lcd_device_register - resource managed lcd_device_register()
  222. * @dev: the device to register
  223. * @name: the name of the device
  224. * @parent: a pointer to the parent device
  225. * @devdata: an optional pointer to be stored for private driver use
  226. * @ops: the lcd operations structure
  227. *
  228. * @return a struct lcd on success, or an ERR_PTR on error
  229. *
  230. * Managed lcd_device_register(). The lcd_device returned from this function
  231. * are automatically freed on driver detach. See lcd_device_register()
  232. * for more information.
  233. */
  234. struct lcd_device *devm_lcd_device_register(struct device *dev,
  235. const char *name, struct device *parent,
  236. void *devdata, const struct lcd_ops *ops)
  237. {
  238. struct lcd_device **ptr, *lcd;
  239. ptr = devres_alloc(devm_lcd_device_release, sizeof(*ptr), GFP_KERNEL);
  240. if (!ptr)
  241. return ERR_PTR(-ENOMEM);
  242. lcd = lcd_device_register(name, parent, devdata, ops);
  243. if (!IS_ERR(lcd)) {
  244. *ptr = lcd;
  245. devres_add(dev, ptr);
  246. } else {
  247. devres_free(ptr);
  248. }
  249. return lcd;
  250. }
  251. EXPORT_SYMBOL(devm_lcd_device_register);
  252. /**
  253. * devm_lcd_device_unregister - resource managed lcd_device_unregister()
  254. * @dev: the device to unregister
  255. * @ld: the lcd device to unregister
  256. *
  257. * Deallocated a lcd allocated with devm_lcd_device_register(). Normally
  258. * this function will not need to be called and the resource management
  259. * code will ensure that the resource is freed.
  260. */
  261. void devm_lcd_device_unregister(struct device *dev, struct lcd_device *ld)
  262. {
  263. int rc;
  264. rc = devres_release(dev, devm_lcd_device_release,
  265. devm_lcd_device_match, ld);
  266. WARN_ON(rc);
  267. }
  268. EXPORT_SYMBOL(devm_lcd_device_unregister);
  269. static void __exit lcd_class_exit(void)
  270. {
  271. class_unregister(&lcd_class);
  272. }
  273. static int __init lcd_class_init(void)
  274. {
  275. int ret;
  276. ret = class_register(&lcd_class);
  277. if (ret) {
  278. pr_warn("Unable to create backlight class; errno = %d\n", ret);
  279. return ret;
  280. }
  281. return 0;
  282. }
  283. /*
  284. * if this is compiled into the kernel, we need to ensure that the
  285. * class is registered before users of the class try to register lcd's
  286. */
  287. postcore_initcall(lcd_class_init);
  288. module_exit(lcd_class_exit);
  289. MODULE_LICENSE("GPL");
  290. MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
  291. MODULE_DESCRIPTION("LCD Lowlevel Control Abstraction");