drm_panel.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * Copyright (C) 2013, NVIDIA Corporation. All rights reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sub license,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the
  12. * next paragraph) shall be included in all copies or substantial portions
  13. * of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <linux/backlight.h>
  24. #include <linux/err.h>
  25. #include <linux/module.h>
  26. #include <drm/drm_crtc.h>
  27. #include <drm/drm_panel.h>
  28. #include <drm/drm_print.h>
  29. static DEFINE_MUTEX(panel_lock);
  30. static LIST_HEAD(panel_list);
  31. /**
  32. * DOC: drm panel
  33. *
  34. * The DRM panel helpers allow drivers to register panel objects with a
  35. * central registry and provide functions to retrieve those panels in display
  36. * drivers.
  37. *
  38. * For easy integration into drivers using the &drm_bridge infrastructure please
  39. * take look at drm_panel_bridge_add() and devm_drm_panel_bridge_add().
  40. */
  41. /**
  42. * drm_panel_init - initialize a panel
  43. * @panel: DRM panel
  44. * @dev: parent device of the panel
  45. * @funcs: panel operations
  46. * @connector_type: the connector type (DRM_MODE_CONNECTOR_*) corresponding to
  47. * the panel interface
  48. *
  49. * Initialize the panel structure for subsequent registration with
  50. * drm_panel_add().
  51. */
  52. void drm_panel_init(struct drm_panel *panel, struct device *dev,
  53. const struct drm_panel_funcs *funcs, int connector_type)
  54. {
  55. INIT_LIST_HEAD(&panel->list);
  56. panel->dev = dev;
  57. panel->funcs = funcs;
  58. panel->connector_type = connector_type;
  59. }
  60. EXPORT_SYMBOL(drm_panel_init);
  61. /**
  62. * drm_panel_add - add a panel to the global registry
  63. * @panel: panel to add
  64. *
  65. * Add a panel to the global registry so that it can be looked up by display
  66. * drivers.
  67. */
  68. void drm_panel_add(struct drm_panel *panel)
  69. {
  70. mutex_lock(&panel_lock);
  71. list_add_tail(&panel->list, &panel_list);
  72. mutex_unlock(&panel_lock);
  73. }
  74. EXPORT_SYMBOL(drm_panel_add);
  75. /**
  76. * drm_panel_remove - remove a panel from the global registry
  77. * @panel: DRM panel
  78. *
  79. * Removes a panel from the global registry.
  80. */
  81. void drm_panel_remove(struct drm_panel *panel)
  82. {
  83. mutex_lock(&panel_lock);
  84. list_del_init(&panel->list);
  85. mutex_unlock(&panel_lock);
  86. }
  87. EXPORT_SYMBOL(drm_panel_remove);
  88. /**
  89. * drm_panel_prepare - power on a panel
  90. * @panel: DRM panel
  91. *
  92. * Calling this function will enable power and deassert any reset signals to
  93. * the panel. After this has completed it is possible to communicate with any
  94. * integrated circuitry via a command bus.
  95. *
  96. * Return: 0 on success or a negative error code on failure.
  97. */
  98. int drm_panel_prepare(struct drm_panel *panel)
  99. {
  100. if (!panel)
  101. return -EINVAL;
  102. if (panel->funcs && panel->funcs->prepare)
  103. return panel->funcs->prepare(panel);
  104. return 0;
  105. }
  106. EXPORT_SYMBOL(drm_panel_prepare);
  107. /**
  108. * drm_panel_unprepare - power off a panel
  109. * @panel: DRM panel
  110. *
  111. * Calling this function will completely power off a panel (assert the panel's
  112. * reset, turn off power supplies, ...). After this function has completed, it
  113. * is usually no longer possible to communicate with the panel until another
  114. * call to drm_panel_prepare().
  115. *
  116. * Return: 0 on success or a negative error code on failure.
  117. */
  118. int drm_panel_unprepare(struct drm_panel *panel)
  119. {
  120. if (!panel)
  121. return -EINVAL;
  122. if (panel->funcs && panel->funcs->unprepare)
  123. return panel->funcs->unprepare(panel);
  124. return 0;
  125. }
  126. EXPORT_SYMBOL(drm_panel_unprepare);
  127. /**
  128. * drm_panel_enable - enable a panel
  129. * @panel: DRM panel
  130. *
  131. * Calling this function will cause the panel display drivers to be turned on
  132. * and the backlight to be enabled. Content will be visible on screen after
  133. * this call completes.
  134. *
  135. * Return: 0 on success or a negative error code on failure.
  136. */
  137. int drm_panel_enable(struct drm_panel *panel)
  138. {
  139. int ret;
  140. if (!panel)
  141. return -EINVAL;
  142. if (panel->funcs && panel->funcs->enable) {
  143. ret = panel->funcs->enable(panel);
  144. if (ret < 0)
  145. return ret;
  146. }
  147. ret = backlight_enable(panel->backlight);
  148. if (ret < 0)
  149. DRM_DEV_INFO(panel->dev, "failed to enable backlight: %d\n",
  150. ret);
  151. return 0;
  152. }
  153. EXPORT_SYMBOL(drm_panel_enable);
  154. /**
  155. * drm_panel_disable - disable a panel
  156. * @panel: DRM panel
  157. *
  158. * This will typically turn off the panel's backlight or disable the display
  159. * drivers. For smart panels it should still be possible to communicate with
  160. * the integrated circuitry via any command bus after this call.
  161. *
  162. * Return: 0 on success or a negative error code on failure.
  163. */
  164. int drm_panel_disable(struct drm_panel *panel)
  165. {
  166. int ret;
  167. if (!panel)
  168. return -EINVAL;
  169. ret = backlight_disable(panel->backlight);
  170. if (ret < 0)
  171. DRM_DEV_INFO(panel->dev, "failed to disable backlight: %d\n",
  172. ret);
  173. if (panel->funcs && panel->funcs->disable)
  174. return panel->funcs->disable(panel);
  175. return 0;
  176. }
  177. EXPORT_SYMBOL(drm_panel_disable);
  178. /**
  179. * drm_panel_get_modes - probe the available display modes of a panel
  180. * @panel: DRM panel
  181. * @connector: DRM connector
  182. *
  183. * The modes probed from the panel are automatically added to the connector
  184. * that the panel is attached to.
  185. *
  186. * Return: The number of modes available from the panel on success or a
  187. * negative error code on failure.
  188. */
  189. int drm_panel_get_modes(struct drm_panel *panel,
  190. struct drm_connector *connector)
  191. {
  192. if (!panel)
  193. return -EINVAL;
  194. if (panel->funcs && panel->funcs->get_modes)
  195. return panel->funcs->get_modes(panel, connector);
  196. return -EOPNOTSUPP;
  197. }
  198. EXPORT_SYMBOL(drm_panel_get_modes);
  199. #ifdef CONFIG_OF
  200. /**
  201. * of_drm_find_panel - look up a panel using a device tree node
  202. * @np: device tree node of the panel
  203. *
  204. * Searches the set of registered panels for one that matches the given device
  205. * tree node. If a matching panel is found, return a pointer to it.
  206. *
  207. * Return: A pointer to the panel registered for the specified device tree
  208. * node or an ERR_PTR() if no panel matching the device tree node can be found.
  209. *
  210. * Possible error codes returned by this function:
  211. *
  212. * - EPROBE_DEFER: the panel device has not been probed yet, and the caller
  213. * should retry later
  214. * - ENODEV: the device is not available (status != "okay" or "ok")
  215. */
  216. struct drm_panel *of_drm_find_panel(const struct device_node *np)
  217. {
  218. struct drm_panel *panel;
  219. if (!of_device_is_available(np))
  220. return ERR_PTR(-ENODEV);
  221. mutex_lock(&panel_lock);
  222. list_for_each_entry(panel, &panel_list, list) {
  223. if (panel->dev->of_node == np) {
  224. mutex_unlock(&panel_lock);
  225. return panel;
  226. }
  227. }
  228. mutex_unlock(&panel_lock);
  229. return ERR_PTR(-EPROBE_DEFER);
  230. }
  231. EXPORT_SYMBOL(of_drm_find_panel);
  232. /**
  233. * of_drm_get_panel_orientation - look up the orientation of the panel through
  234. * the "rotation" binding from a device tree node
  235. * @np: device tree node of the panel
  236. * @orientation: orientation enum to be filled in
  237. *
  238. * Looks up the rotation of a panel in the device tree. The orientation of the
  239. * panel is expressed as a property name "rotation" in the device tree. The
  240. * rotation in the device tree is counter clockwise.
  241. *
  242. * Return: 0 when a valid rotation value (0, 90, 180, or 270) is read or the
  243. * rotation property doesn't exist. Return a negative error code on failure.
  244. */
  245. int of_drm_get_panel_orientation(const struct device_node *np,
  246. enum drm_panel_orientation *orientation)
  247. {
  248. int rotation, ret;
  249. ret = of_property_read_u32(np, "rotation", &rotation);
  250. if (ret == -EINVAL) {
  251. /* Don't return an error if there's no rotation property. */
  252. *orientation = DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
  253. return 0;
  254. }
  255. if (ret < 0)
  256. return ret;
  257. if (rotation == 0)
  258. *orientation = DRM_MODE_PANEL_ORIENTATION_NORMAL;
  259. else if (rotation == 90)
  260. *orientation = DRM_MODE_PANEL_ORIENTATION_RIGHT_UP;
  261. else if (rotation == 180)
  262. *orientation = DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP;
  263. else if (rotation == 270)
  264. *orientation = DRM_MODE_PANEL_ORIENTATION_LEFT_UP;
  265. else
  266. return -EINVAL;
  267. return 0;
  268. }
  269. EXPORT_SYMBOL(of_drm_get_panel_orientation);
  270. #endif
  271. #if IS_REACHABLE(CONFIG_BACKLIGHT_CLASS_DEVICE)
  272. /**
  273. * drm_panel_of_backlight - use backlight device node for backlight
  274. * @panel: DRM panel
  275. *
  276. * Use this function to enable backlight handling if your panel
  277. * uses device tree and has a backlight phandle.
  278. *
  279. * When the panel is enabled backlight will be enabled after a
  280. * successful call to &drm_panel_funcs.enable()
  281. *
  282. * When the panel is disabled backlight will be disabled before the
  283. * call to &drm_panel_funcs.disable().
  284. *
  285. * A typical implementation for a panel driver supporting device tree
  286. * will call this function at probe time. Backlight will then be handled
  287. * transparently without requiring any intervention from the driver.
  288. * drm_panel_of_backlight() must be called after the call to drm_panel_init().
  289. *
  290. * Return: 0 on success or a negative error code on failure.
  291. */
  292. int drm_panel_of_backlight(struct drm_panel *panel)
  293. {
  294. struct backlight_device *backlight;
  295. if (!panel || !panel->dev)
  296. return -EINVAL;
  297. backlight = devm_of_find_backlight(panel->dev);
  298. if (IS_ERR(backlight))
  299. return PTR_ERR(backlight);
  300. panel->backlight = backlight;
  301. return 0;
  302. }
  303. EXPORT_SYMBOL(drm_panel_of_backlight);
  304. #endif
  305. MODULE_AUTHOR("Thierry Reding <[email protected]>");
  306. MODULE_DESCRIPTION("DRM panel infrastructure");
  307. MODULE_LICENSE("GPL and additional rights");