drm_panel.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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 (must NOT be DRM_MODE_CONNECTOR_Unknown)
  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. if (connector_type == DRM_MODE_CONNECTOR_Unknown)
  56. DRM_WARN("%s: %s: a valid connector type is required!\n", __func__, dev_name(dev));
  57. INIT_LIST_HEAD(&panel->list);
  58. INIT_LIST_HEAD(&panel->followers);
  59. mutex_init(&panel->follower_lock);
  60. panel->dev = dev;
  61. panel->funcs = funcs;
  62. panel->connector_type = connector_type;
  63. }
  64. EXPORT_SYMBOL(drm_panel_init);
  65. /**
  66. * drm_panel_add - add a panel to the global registry
  67. * @panel: panel to add
  68. *
  69. * Add a panel to the global registry so that it can be looked up by display
  70. * drivers.
  71. */
  72. void drm_panel_add(struct drm_panel *panel)
  73. {
  74. mutex_lock(&panel_lock);
  75. list_add_tail(&panel->list, &panel_list);
  76. mutex_unlock(&panel_lock);
  77. }
  78. EXPORT_SYMBOL(drm_panel_add);
  79. /**
  80. * drm_panel_remove - remove a panel from the global registry
  81. * @panel: DRM panel
  82. *
  83. * Removes a panel from the global registry.
  84. */
  85. void drm_panel_remove(struct drm_panel *panel)
  86. {
  87. mutex_lock(&panel_lock);
  88. list_del_init(&panel->list);
  89. mutex_unlock(&panel_lock);
  90. }
  91. EXPORT_SYMBOL(drm_panel_remove);
  92. /**
  93. * drm_panel_prepare - power on a panel
  94. * @panel: DRM panel
  95. *
  96. * Calling this function will enable power and deassert any reset signals to
  97. * the panel. After this has completed it is possible to communicate with any
  98. * integrated circuitry via a command bus.
  99. *
  100. * Return: 0 on success or a negative error code on failure.
  101. */
  102. int drm_panel_prepare(struct drm_panel *panel)
  103. {
  104. struct drm_panel_follower *follower;
  105. int ret;
  106. if (!panel)
  107. return -EINVAL;
  108. if (panel->prepared) {
  109. dev_warn(panel->dev, "Skipping prepare of already prepared panel\n");
  110. return 0;
  111. }
  112. mutex_lock(&panel->follower_lock);
  113. if (panel->funcs && panel->funcs->prepare) {
  114. ret = panel->funcs->prepare(panel);
  115. if (ret < 0)
  116. goto exit;
  117. }
  118. panel->prepared = true;
  119. list_for_each_entry(follower, &panel->followers, list) {
  120. ret = follower->funcs->panel_prepared(follower);
  121. if (ret < 0)
  122. dev_info(panel->dev, "%ps failed: %d\n",
  123. follower->funcs->panel_prepared, ret);
  124. }
  125. ret = 0;
  126. exit:
  127. mutex_unlock(&panel->follower_lock);
  128. return ret;
  129. }
  130. EXPORT_SYMBOL(drm_panel_prepare);
  131. /**
  132. * drm_panel_unprepare - power off a panel
  133. * @panel: DRM panel
  134. *
  135. * Calling this function will completely power off a panel (assert the panel's
  136. * reset, turn off power supplies, ...). After this function has completed, it
  137. * is usually no longer possible to communicate with the panel until another
  138. * call to drm_panel_prepare().
  139. *
  140. * Return: 0 on success or a negative error code on failure.
  141. */
  142. int drm_panel_unprepare(struct drm_panel *panel)
  143. {
  144. struct drm_panel_follower *follower;
  145. int ret;
  146. if (!panel)
  147. return -EINVAL;
  148. /*
  149. * If you are seeing the warning below it likely means one of two things:
  150. * - Your panel driver incorrectly calls drm_panel_unprepare() in its
  151. * shutdown routine. You should delete this.
  152. * - You are using panel-edp or panel-simple and your DRM modeset
  153. * driver's shutdown() callback happened after the panel's shutdown().
  154. * In this case the warning is harmless though ideally you should
  155. * figure out how to reverse the order of the shutdown() callbacks.
  156. */
  157. if (!panel->prepared) {
  158. dev_warn(panel->dev, "Skipping unprepare of already unprepared panel\n");
  159. return 0;
  160. }
  161. mutex_lock(&panel->follower_lock);
  162. list_for_each_entry(follower, &panel->followers, list) {
  163. ret = follower->funcs->panel_unpreparing(follower);
  164. if (ret < 0)
  165. dev_info(panel->dev, "%ps failed: %d\n",
  166. follower->funcs->panel_unpreparing, ret);
  167. }
  168. if (panel->funcs && panel->funcs->unprepare) {
  169. ret = panel->funcs->unprepare(panel);
  170. if (ret < 0)
  171. goto exit;
  172. }
  173. panel->prepared = false;
  174. ret = 0;
  175. exit:
  176. mutex_unlock(&panel->follower_lock);
  177. return ret;
  178. }
  179. EXPORT_SYMBOL(drm_panel_unprepare);
  180. /**
  181. * drm_panel_enable - enable a panel
  182. * @panel: DRM panel
  183. *
  184. * Calling this function will cause the panel display drivers to be turned on
  185. * and the backlight to be enabled. Content will be visible on screen after
  186. * this call completes.
  187. *
  188. * Return: 0 on success or a negative error code on failure.
  189. */
  190. int drm_panel_enable(struct drm_panel *panel)
  191. {
  192. int ret;
  193. if (!panel)
  194. return -EINVAL;
  195. if (panel->enabled) {
  196. dev_warn(panel->dev, "Skipping enable of already enabled panel\n");
  197. return 0;
  198. }
  199. if (panel->funcs && panel->funcs->enable) {
  200. ret = panel->funcs->enable(panel);
  201. if (ret < 0)
  202. return ret;
  203. }
  204. panel->enabled = true;
  205. ret = backlight_enable(panel->backlight);
  206. if (ret < 0)
  207. DRM_DEV_INFO(panel->dev, "failed to enable backlight: %d\n",
  208. ret);
  209. return 0;
  210. }
  211. EXPORT_SYMBOL(drm_panel_enable);
  212. /**
  213. * drm_panel_disable - disable a panel
  214. * @panel: DRM panel
  215. *
  216. * This will typically turn off the panel's backlight or disable the display
  217. * drivers. For smart panels it should still be possible to communicate with
  218. * the integrated circuitry via any command bus after this call.
  219. *
  220. * Return: 0 on success or a negative error code on failure.
  221. */
  222. int drm_panel_disable(struct drm_panel *panel)
  223. {
  224. int ret;
  225. if (!panel)
  226. return -EINVAL;
  227. /*
  228. * If you are seeing the warning below it likely means one of two things:
  229. * - Your panel driver incorrectly calls drm_panel_disable() in its
  230. * shutdown routine. You should delete this.
  231. * - You are using panel-edp or panel-simple and your DRM modeset
  232. * driver's shutdown() callback happened after the panel's shutdown().
  233. * In this case the warning is harmless though ideally you should
  234. * figure out how to reverse the order of the shutdown() callbacks.
  235. */
  236. if (!panel->enabled) {
  237. dev_warn(panel->dev, "Skipping disable of already disabled panel\n");
  238. return 0;
  239. }
  240. ret = backlight_disable(panel->backlight);
  241. if (ret < 0)
  242. DRM_DEV_INFO(panel->dev, "failed to disable backlight: %d\n",
  243. ret);
  244. if (panel->funcs && panel->funcs->disable) {
  245. ret = panel->funcs->disable(panel);
  246. if (ret < 0)
  247. return ret;
  248. }
  249. panel->enabled = false;
  250. return 0;
  251. }
  252. EXPORT_SYMBOL(drm_panel_disable);
  253. /**
  254. * drm_panel_get_modes - probe the available display modes of a panel
  255. * @panel: DRM panel
  256. * @connector: DRM connector
  257. *
  258. * The modes probed from the panel are automatically added to the connector
  259. * that the panel is attached to.
  260. *
  261. * Return: The number of modes available from the panel on success, or 0 on
  262. * failure (no modes).
  263. */
  264. int drm_panel_get_modes(struct drm_panel *panel,
  265. struct drm_connector *connector)
  266. {
  267. if (!panel)
  268. return 0;
  269. if (panel->funcs && panel->funcs->get_modes) {
  270. int num;
  271. num = panel->funcs->get_modes(panel, connector);
  272. if (num > 0)
  273. return num;
  274. }
  275. return 0;
  276. }
  277. EXPORT_SYMBOL(drm_panel_get_modes);
  278. #ifdef CONFIG_OF
  279. /**
  280. * of_drm_find_panel - look up a panel using a device tree node
  281. * @np: device tree node of the panel
  282. *
  283. * Searches the set of registered panels for one that matches the given device
  284. * tree node. If a matching panel is found, return a pointer to it.
  285. *
  286. * Return: A pointer to the panel registered for the specified device tree
  287. * node or an ERR_PTR() if no panel matching the device tree node can be found.
  288. *
  289. * Possible error codes returned by this function:
  290. *
  291. * - EPROBE_DEFER: the panel device has not been probed yet, and the caller
  292. * should retry later
  293. * - ENODEV: the device is not available (status != "okay" or "ok")
  294. */
  295. struct drm_panel *of_drm_find_panel(const struct device_node *np)
  296. {
  297. struct drm_panel *panel;
  298. if (!of_device_is_available(np))
  299. return ERR_PTR(-ENODEV);
  300. mutex_lock(&panel_lock);
  301. list_for_each_entry(panel, &panel_list, list) {
  302. if (panel->dev->of_node == np) {
  303. mutex_unlock(&panel_lock);
  304. return panel;
  305. }
  306. }
  307. mutex_unlock(&panel_lock);
  308. return ERR_PTR(-EPROBE_DEFER);
  309. }
  310. EXPORT_SYMBOL(of_drm_find_panel);
  311. /**
  312. * of_drm_get_panel_orientation - look up the orientation of the panel through
  313. * the "rotation" binding from a device tree node
  314. * @np: device tree node of the panel
  315. * @orientation: orientation enum to be filled in
  316. *
  317. * Looks up the rotation of a panel in the device tree. The orientation of the
  318. * panel is expressed as a property name "rotation" in the device tree. The
  319. * rotation in the device tree is counter clockwise.
  320. *
  321. * Return: 0 when a valid rotation value (0, 90, 180, or 270) is read or the
  322. * rotation property doesn't exist. Return a negative error code on failure.
  323. */
  324. int of_drm_get_panel_orientation(const struct device_node *np,
  325. enum drm_panel_orientation *orientation)
  326. {
  327. int rotation, ret;
  328. ret = of_property_read_u32(np, "rotation", &rotation);
  329. if (ret == -EINVAL) {
  330. /* Don't return an error if there's no rotation property. */
  331. *orientation = DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
  332. return 0;
  333. }
  334. if (ret < 0)
  335. return ret;
  336. if (rotation == 0)
  337. *orientation = DRM_MODE_PANEL_ORIENTATION_NORMAL;
  338. else if (rotation == 90)
  339. *orientation = DRM_MODE_PANEL_ORIENTATION_RIGHT_UP;
  340. else if (rotation == 180)
  341. *orientation = DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP;
  342. else if (rotation == 270)
  343. *orientation = DRM_MODE_PANEL_ORIENTATION_LEFT_UP;
  344. else
  345. return -EINVAL;
  346. return 0;
  347. }
  348. EXPORT_SYMBOL(of_drm_get_panel_orientation);
  349. #endif
  350. /**
  351. * drm_is_panel_follower() - Check if the device is a panel follower
  352. * @dev: The 'struct device' to check
  353. *
  354. * This checks to see if a device needs to be power sequenced together with
  355. * a panel using the panel follower API.
  356. * At the moment panels can only be followed on device tree enabled systems.
  357. * The "panel" property of the follower points to the panel to be followed.
  358. *
  359. * Return: true if we should be power sequenced with a panel; false otherwise.
  360. */
  361. bool drm_is_panel_follower(struct device *dev)
  362. {
  363. /*
  364. * The "panel" property is actually a phandle, but for simplicity we
  365. * don't bother trying to parse it here. We just need to know if the
  366. * property is there.
  367. */
  368. return of_property_read_bool(dev->of_node, "panel");
  369. }
  370. EXPORT_SYMBOL(drm_is_panel_follower);
  371. /**
  372. * drm_panel_add_follower() - Register something to follow panel state.
  373. * @follower_dev: The 'struct device' for the follower.
  374. * @follower: The panel follower descriptor for the follower.
  375. *
  376. * A panel follower is called right after preparing the panel and right before
  377. * unpreparing the panel. It's primary intention is to power on an associated
  378. * touchscreen, though it could be used for any similar devices. Multiple
  379. * devices are allowed the follow the same panel.
  380. *
  381. * If a follower is added to a panel that's already been turned on, the
  382. * follower's prepare callback is called right away.
  383. *
  384. * At the moment panels can only be followed on device tree enabled systems.
  385. * The "panel" property of the follower points to the panel to be followed.
  386. *
  387. * Return: 0 or an error code. Note that -ENODEV means that we detected that
  388. * follower_dev is not actually following a panel. The caller may
  389. * choose to ignore this return value if following a panel is optional.
  390. */
  391. int drm_panel_add_follower(struct device *follower_dev,
  392. struct drm_panel_follower *follower)
  393. {
  394. struct device_node *panel_np;
  395. struct drm_panel *panel;
  396. int ret;
  397. panel_np = of_parse_phandle(follower_dev->of_node, "panel", 0);
  398. if (!panel_np)
  399. return -ENODEV;
  400. panel = of_drm_find_panel(panel_np);
  401. of_node_put(panel_np);
  402. if (IS_ERR(panel))
  403. return PTR_ERR(panel);
  404. get_device(panel->dev);
  405. follower->panel = panel;
  406. mutex_lock(&panel->follower_lock);
  407. list_add_tail(&follower->list, &panel->followers);
  408. if (panel->prepared) {
  409. ret = follower->funcs->panel_prepared(follower);
  410. if (ret < 0)
  411. dev_info(panel->dev, "%ps failed: %d\n",
  412. follower->funcs->panel_prepared, ret);
  413. }
  414. mutex_unlock(&panel->follower_lock);
  415. return 0;
  416. }
  417. EXPORT_SYMBOL(drm_panel_add_follower);
  418. /**
  419. * drm_panel_remove_follower() - Reverse drm_panel_add_follower().
  420. * @follower: The panel follower descriptor for the follower.
  421. *
  422. * Undo drm_panel_add_follower(). This includes calling the follower's
  423. * unprepare function if we're removed from a panel that's currently prepared.
  424. *
  425. * Return: 0 or an error code.
  426. */
  427. void drm_panel_remove_follower(struct drm_panel_follower *follower)
  428. {
  429. struct drm_panel *panel = follower->panel;
  430. int ret;
  431. mutex_lock(&panel->follower_lock);
  432. if (panel->prepared) {
  433. ret = follower->funcs->panel_unpreparing(follower);
  434. if (ret < 0)
  435. dev_info(panel->dev, "%ps failed: %d\n",
  436. follower->funcs->panel_unpreparing, ret);
  437. }
  438. list_del_init(&follower->list);
  439. mutex_unlock(&panel->follower_lock);
  440. put_device(panel->dev);
  441. }
  442. EXPORT_SYMBOL(drm_panel_remove_follower);
  443. static void drm_panel_remove_follower_void(void *follower)
  444. {
  445. drm_panel_remove_follower(follower);
  446. }
  447. /**
  448. * devm_drm_panel_add_follower() - devm version of drm_panel_add_follower()
  449. * @follower_dev: The 'struct device' for the follower.
  450. * @follower: The panel follower descriptor for the follower.
  451. *
  452. * Handles calling drm_panel_remove_follower() using devm on the follower_dev.
  453. *
  454. * Return: 0 or an error code.
  455. */
  456. int devm_drm_panel_add_follower(struct device *follower_dev,
  457. struct drm_panel_follower *follower)
  458. {
  459. int ret;
  460. ret = drm_panel_add_follower(follower_dev, follower);
  461. if (ret)
  462. return ret;
  463. return devm_add_action_or_reset(follower_dev,
  464. drm_panel_remove_follower_void, follower);
  465. }
  466. EXPORT_SYMBOL(devm_drm_panel_add_follower);
  467. #if IS_REACHABLE(CONFIG_BACKLIGHT_CLASS_DEVICE)
  468. /**
  469. * drm_panel_of_backlight - use backlight device node for backlight
  470. * @panel: DRM panel
  471. *
  472. * Use this function to enable backlight handling if your panel
  473. * uses device tree and has a backlight phandle.
  474. *
  475. * When the panel is enabled backlight will be enabled after a
  476. * successful call to &drm_panel_funcs.enable()
  477. *
  478. * When the panel is disabled backlight will be disabled before the
  479. * call to &drm_panel_funcs.disable().
  480. *
  481. * A typical implementation for a panel driver supporting device tree
  482. * will call this function at probe time. Backlight will then be handled
  483. * transparently without requiring any intervention from the driver.
  484. * drm_panel_of_backlight() must be called after the call to drm_panel_init().
  485. *
  486. * Return: 0 on success or a negative error code on failure.
  487. */
  488. int drm_panel_of_backlight(struct drm_panel *panel)
  489. {
  490. struct backlight_device *backlight;
  491. if (!panel || !panel->dev)
  492. return -EINVAL;
  493. backlight = devm_of_find_backlight(panel->dev);
  494. if (IS_ERR(backlight))
  495. return PTR_ERR(backlight);
  496. panel->backlight = backlight;
  497. return 0;
  498. }
  499. EXPORT_SYMBOL(drm_panel_of_backlight);
  500. #endif
  501. MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
  502. MODULE_DESCRIPTION("DRM panel infrastructure");
  503. MODULE_LICENSE("GPL and additional rights");