lcd.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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/slab.h>
  17. static DEFINE_MUTEX(lcd_dev_list_mutex);
  18. static LIST_HEAD(lcd_dev_list);
  19. static void lcd_notify_blank(struct lcd_device *ld, struct device *display_dev,
  20. int power)
  21. {
  22. guard(mutex)(&ld->ops_lock);
  23. if (!ld->ops || !ld->ops->set_power)
  24. return;
  25. if (ld->ops->controls_device && !ld->ops->controls_device(ld, display_dev))
  26. return;
  27. ld->ops->set_power(ld, power);
  28. }
  29. void lcd_notify_blank_all(struct device *display_dev, int power)
  30. {
  31. struct lcd_device *ld;
  32. guard(mutex)(&lcd_dev_list_mutex);
  33. list_for_each_entry(ld, &lcd_dev_list, entry)
  34. lcd_notify_blank(ld, display_dev, power);
  35. }
  36. EXPORT_SYMBOL(lcd_notify_blank_all);
  37. static void lcd_notify_mode_change(struct lcd_device *ld, struct device *display_dev,
  38. unsigned int width, unsigned int height)
  39. {
  40. guard(mutex)(&ld->ops_lock);
  41. if (!ld->ops || !ld->ops->set_mode)
  42. return;
  43. if (ld->ops->controls_device && !ld->ops->controls_device(ld, display_dev))
  44. return;
  45. ld->ops->set_mode(ld, width, height);
  46. }
  47. void lcd_notify_mode_change_all(struct device *display_dev,
  48. unsigned int width, unsigned int height)
  49. {
  50. struct lcd_device *ld;
  51. guard(mutex)(&lcd_dev_list_mutex);
  52. list_for_each_entry(ld, &lcd_dev_list, entry)
  53. lcd_notify_mode_change(ld, display_dev, width, height);
  54. }
  55. EXPORT_SYMBOL(lcd_notify_mode_change_all);
  56. static ssize_t lcd_power_show(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_power_store(struct device *dev,
  70. struct device_attribute *attr, const char *buf, size_t count)
  71. {
  72. int rc;
  73. struct lcd_device *ld = to_lcd_device(dev);
  74. unsigned long power;
  75. rc = kstrtoul(buf, 0, &power);
  76. if (rc)
  77. return rc;
  78. rc = -ENXIO;
  79. mutex_lock(&ld->ops_lock);
  80. if (ld->ops && ld->ops->set_power) {
  81. pr_debug("set power to %lu\n", power);
  82. ld->ops->set_power(ld, power);
  83. rc = count;
  84. }
  85. mutex_unlock(&ld->ops_lock);
  86. return rc;
  87. }
  88. static DEVICE_ATTR_RW(lcd_power);
  89. static ssize_t contrast_show(struct device *dev,
  90. struct device_attribute *attr, char *buf)
  91. {
  92. int rc = -ENXIO;
  93. struct lcd_device *ld = to_lcd_device(dev);
  94. mutex_lock(&ld->ops_lock);
  95. if (ld->ops && ld->ops->get_contrast)
  96. rc = sprintf(buf, "%d\n", ld->ops->get_contrast(ld));
  97. mutex_unlock(&ld->ops_lock);
  98. return rc;
  99. }
  100. static ssize_t contrast_store(struct device *dev,
  101. struct device_attribute *attr, const char *buf, size_t count)
  102. {
  103. int rc;
  104. struct lcd_device *ld = to_lcd_device(dev);
  105. unsigned long contrast;
  106. rc = kstrtoul(buf, 0, &contrast);
  107. if (rc)
  108. return rc;
  109. rc = -ENXIO;
  110. mutex_lock(&ld->ops_lock);
  111. if (ld->ops && ld->ops->set_contrast) {
  112. pr_debug("set contrast to %lu\n", contrast);
  113. ld->ops->set_contrast(ld, contrast);
  114. rc = count;
  115. }
  116. mutex_unlock(&ld->ops_lock);
  117. return rc;
  118. }
  119. static DEVICE_ATTR_RW(contrast);
  120. static ssize_t max_contrast_show(struct device *dev,
  121. struct device_attribute *attr, char *buf)
  122. {
  123. struct lcd_device *ld = to_lcd_device(dev);
  124. return sprintf(buf, "%d\n", ld->props.max_contrast);
  125. }
  126. static DEVICE_ATTR_RO(max_contrast);
  127. static void lcd_device_release(struct device *dev)
  128. {
  129. struct lcd_device *ld = to_lcd_device(dev);
  130. kfree(ld);
  131. }
  132. static struct attribute *lcd_device_attrs[] = {
  133. &dev_attr_lcd_power.attr,
  134. &dev_attr_contrast.attr,
  135. &dev_attr_max_contrast.attr,
  136. NULL,
  137. };
  138. ATTRIBUTE_GROUPS(lcd_device);
  139. static const struct class lcd_class = {
  140. .name = "lcd",
  141. .dev_groups = lcd_device_groups,
  142. };
  143. /**
  144. * lcd_device_register - register a new object of lcd_device class.
  145. * @name: the name of the new object(must be the same as the name of the
  146. * respective framebuffer device).
  147. * @parent: pointer to the parent's struct device .
  148. * @devdata: an optional pointer to be stored in the device. The
  149. * methods may retrieve it by using lcd_get_data(ld).
  150. * @ops: the lcd operations structure.
  151. *
  152. * Creates and registers a new lcd device. Returns either an ERR_PTR()
  153. * or a pointer to the newly allocated device.
  154. */
  155. struct lcd_device *lcd_device_register(const char *name, struct device *parent,
  156. void *devdata, const struct lcd_ops *ops)
  157. {
  158. struct lcd_device *new_ld;
  159. int rc;
  160. pr_debug("lcd_device_register: name=%s\n", name);
  161. new_ld = kzalloc_obj(struct lcd_device);
  162. if (!new_ld)
  163. return ERR_PTR(-ENOMEM);
  164. mutex_init(&new_ld->ops_lock);
  165. mutex_init(&new_ld->update_lock);
  166. new_ld->dev.class = &lcd_class;
  167. new_ld->dev.parent = parent;
  168. new_ld->dev.release = lcd_device_release;
  169. dev_set_name(&new_ld->dev, "%s", name);
  170. dev_set_drvdata(&new_ld->dev, devdata);
  171. new_ld->ops = ops;
  172. rc = device_register(&new_ld->dev);
  173. if (rc) {
  174. put_device(&new_ld->dev);
  175. return ERR_PTR(rc);
  176. }
  177. guard(mutex)(&lcd_dev_list_mutex);
  178. list_add(&new_ld->entry, &lcd_dev_list);
  179. return new_ld;
  180. }
  181. EXPORT_SYMBOL(lcd_device_register);
  182. /**
  183. * lcd_device_unregister - unregisters a object of lcd_device class.
  184. * @ld: the lcd device object to be unregistered and freed.
  185. *
  186. * Unregisters a previously registered via lcd_device_register object.
  187. */
  188. void lcd_device_unregister(struct lcd_device *ld)
  189. {
  190. if (!ld)
  191. return;
  192. guard(mutex)(&lcd_dev_list_mutex);
  193. list_del(&ld->entry);
  194. mutex_lock(&ld->ops_lock);
  195. ld->ops = NULL;
  196. mutex_unlock(&ld->ops_lock);
  197. device_unregister(&ld->dev);
  198. }
  199. EXPORT_SYMBOL(lcd_device_unregister);
  200. static void devm_lcd_device_release(struct device *dev, void *res)
  201. {
  202. struct lcd_device *lcd = *(struct lcd_device **)res;
  203. lcd_device_unregister(lcd);
  204. }
  205. static int devm_lcd_device_match(struct device *dev, void *res, void *data)
  206. {
  207. struct lcd_device **r = res;
  208. return *r == data;
  209. }
  210. /**
  211. * devm_lcd_device_register - resource managed lcd_device_register()
  212. * @dev: the device to register
  213. * @name: the name of the device
  214. * @parent: a pointer to the parent device
  215. * @devdata: an optional pointer to be stored for private driver use
  216. * @ops: the lcd operations structure
  217. *
  218. * @return a struct lcd on success, or an ERR_PTR on error
  219. *
  220. * Managed lcd_device_register(). The lcd_device returned from this function
  221. * are automatically freed on driver detach. See lcd_device_register()
  222. * for more information.
  223. */
  224. struct lcd_device *devm_lcd_device_register(struct device *dev,
  225. const char *name, struct device *parent,
  226. void *devdata, const struct lcd_ops *ops)
  227. {
  228. struct lcd_device **ptr, *lcd;
  229. ptr = devres_alloc(devm_lcd_device_release, sizeof(*ptr), GFP_KERNEL);
  230. if (!ptr)
  231. return ERR_PTR(-ENOMEM);
  232. lcd = lcd_device_register(name, parent, devdata, ops);
  233. if (!IS_ERR(lcd)) {
  234. *ptr = lcd;
  235. devres_add(dev, ptr);
  236. } else {
  237. devres_free(ptr);
  238. }
  239. return lcd;
  240. }
  241. EXPORT_SYMBOL(devm_lcd_device_register);
  242. /**
  243. * devm_lcd_device_unregister - resource managed lcd_device_unregister()
  244. * @dev: the device to unregister
  245. * @ld: the lcd device to unregister
  246. *
  247. * Deallocated a lcd allocated with devm_lcd_device_register(). Normally
  248. * this function will not need to be called and the resource management
  249. * code will ensure that the resource is freed.
  250. */
  251. void devm_lcd_device_unregister(struct device *dev, struct lcd_device *ld)
  252. {
  253. int rc;
  254. rc = devres_release(dev, devm_lcd_device_release,
  255. devm_lcd_device_match, ld);
  256. WARN_ON(rc);
  257. }
  258. EXPORT_SYMBOL(devm_lcd_device_unregister);
  259. static void __exit lcd_class_exit(void)
  260. {
  261. class_unregister(&lcd_class);
  262. }
  263. static int __init lcd_class_init(void)
  264. {
  265. int ret;
  266. ret = class_register(&lcd_class);
  267. if (ret) {
  268. pr_warn("Unable to create backlight class; errno = %d\n", ret);
  269. return ret;
  270. }
  271. return 0;
  272. }
  273. /*
  274. * if this is compiled into the kernel, we need to ensure that the
  275. * class is registered before users of the class try to register lcd's
  276. */
  277. postcore_initcall(lcd_class_init);
  278. module_exit(lcd_class_exit);
  279. MODULE_LICENSE("GPL");
  280. MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
  281. MODULE_DESCRIPTION("LCD Lowlevel Control Abstraction");