lcd.c 8.2 KB

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