panel-samsung-atna33xc20.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2021 Google Inc.
  4. *
  5. * Panel driver for the Samsung ATNA33XC20 panel. This panel can't be handled
  6. * by the DRM_PANEL_SIMPLE driver because its power sequencing is non-standard.
  7. */
  8. #include <linux/backlight.h>
  9. #include <linux/delay.h>
  10. #include <linux/gpio/consumer.h>
  11. #include <linux/iopoll.h>
  12. #include <linux/module.h>
  13. #include <linux/pm_runtime.h>
  14. #include <linux/regulator/consumer.h>
  15. #include <drm/display/drm_dp_aux_bus.h>
  16. #include <drm/display/drm_dp_helper.h>
  17. #include <drm/drm_edid.h>
  18. #include <drm/drm_panel.h>
  19. /* T3 VCC to HPD high is max 200 ms */
  20. #define HPD_MAX_MS 200
  21. #define HPD_MAX_US (HPD_MAX_MS * 1000)
  22. struct atana33xc20_panel {
  23. struct drm_panel base;
  24. bool prepared;
  25. bool enabled;
  26. bool el3_was_on;
  27. bool no_hpd;
  28. struct gpio_desc *hpd_gpio;
  29. struct regulator *supply;
  30. struct gpio_desc *el_on3_gpio;
  31. struct drm_dp_aux *aux;
  32. struct edid *edid;
  33. ktime_t powered_off_time;
  34. ktime_t powered_on_time;
  35. ktime_t el_on3_off_time;
  36. };
  37. static inline struct atana33xc20_panel *to_atana33xc20(struct drm_panel *panel)
  38. {
  39. return container_of(panel, struct atana33xc20_panel, base);
  40. }
  41. static void atana33xc20_wait(ktime_t start_ktime, unsigned int min_ms)
  42. {
  43. ktime_t now_ktime, min_ktime;
  44. min_ktime = ktime_add(start_ktime, ms_to_ktime(min_ms));
  45. now_ktime = ktime_get();
  46. if (ktime_before(now_ktime, min_ktime))
  47. msleep(ktime_to_ms(ktime_sub(min_ktime, now_ktime)) + 1);
  48. }
  49. static int atana33xc20_suspend(struct device *dev)
  50. {
  51. struct atana33xc20_panel *p = dev_get_drvdata(dev);
  52. int ret;
  53. /*
  54. * Note 3 (Example of power off sequence in detail) in spec
  55. * specifies to wait 150 ms after deasserting EL3_ON before
  56. * powering off.
  57. */
  58. if (p->el3_was_on)
  59. atana33xc20_wait(p->el_on3_off_time, 150);
  60. ret = regulator_disable(p->supply);
  61. if (ret)
  62. return ret;
  63. p->powered_off_time = ktime_get();
  64. p->el3_was_on = false;
  65. return 0;
  66. }
  67. static int atana33xc20_resume(struct device *dev)
  68. {
  69. struct atana33xc20_panel *p = dev_get_drvdata(dev);
  70. int hpd_asserted;
  71. int ret;
  72. /* T12 (Power off time) is min 500 ms */
  73. atana33xc20_wait(p->powered_off_time, 500);
  74. ret = regulator_enable(p->supply);
  75. if (ret)
  76. return ret;
  77. p->powered_on_time = ktime_get();
  78. if (p->no_hpd) {
  79. msleep(HPD_MAX_MS);
  80. return 0;
  81. }
  82. if (p->hpd_gpio) {
  83. ret = readx_poll_timeout(gpiod_get_value_cansleep, p->hpd_gpio,
  84. hpd_asserted, hpd_asserted,
  85. 1000, HPD_MAX_US);
  86. if (hpd_asserted < 0)
  87. ret = hpd_asserted;
  88. if (ret)
  89. dev_warn(dev, "Error waiting for HPD GPIO: %d\n", ret);
  90. return ret;
  91. }
  92. if (p->aux->wait_hpd_asserted) {
  93. ret = p->aux->wait_hpd_asserted(p->aux, HPD_MAX_US);
  94. if (ret)
  95. dev_warn(dev, "Controller error waiting for HPD: %d\n", ret);
  96. return ret;
  97. }
  98. /*
  99. * Note that it's possible that no_hpd is false, hpd_gpio is
  100. * NULL, and wait_hpd_asserted is NULL. This is because
  101. * wait_hpd_asserted() is optional even if HPD is hooked up to
  102. * a dedicated pin on the eDP controller. In this case we just
  103. * assume that the controller driver will wait for HPD at the
  104. * right times.
  105. */
  106. return 0;
  107. }
  108. static int atana33xc20_disable(struct drm_panel *panel)
  109. {
  110. struct atana33xc20_panel *p = to_atana33xc20(panel);
  111. /* Disabling when already disabled is a no-op */
  112. if (!p->enabled)
  113. return 0;
  114. gpiod_set_value_cansleep(p->el_on3_gpio, 0);
  115. p->el_on3_off_time = ktime_get();
  116. p->enabled = false;
  117. /*
  118. * Keep track of the fact that EL_ON3 was on but we haven't power
  119. * cycled yet. This lets us know that "el_on3_off_time" is recent (we
  120. * don't need to worry about ktime wraparounds) and also makes it
  121. * obvious if we try to enable again without a power cycle (see the
  122. * warning in atana33xc20_enable()).
  123. */
  124. p->el3_was_on = true;
  125. /*
  126. * Sleeping 20 ms here (after setting the GPIO) avoids a glitch when
  127. * powering off.
  128. */
  129. msleep(20);
  130. return 0;
  131. }
  132. static int atana33xc20_enable(struct drm_panel *panel)
  133. {
  134. struct atana33xc20_panel *p = to_atana33xc20(panel);
  135. /* Enabling when already enabled is a no-op */
  136. if (p->enabled)
  137. return 0;
  138. /*
  139. * Once EL_ON3 drops we absolutely need a power cycle before the next
  140. * enable or the backlight will never come on again. The code ensures
  141. * this because disable() is _always_ followed by unprepare() and
  142. * unprepare() forces a suspend with pm_runtime_put_sync_suspend(),
  143. * but let's track just to make sure since the requirement is so
  144. * non-obvious.
  145. */
  146. if (WARN_ON(p->el3_was_on))
  147. return -EIO;
  148. /*
  149. * Note 2 (Example of power on sequence in detail) in spec specifies
  150. * to wait 400 ms after powering on before asserting EL3_on.
  151. */
  152. atana33xc20_wait(p->powered_on_time, 400);
  153. gpiod_set_value_cansleep(p->el_on3_gpio, 1);
  154. p->enabled = true;
  155. return 0;
  156. }
  157. static int atana33xc20_unprepare(struct drm_panel *panel)
  158. {
  159. struct atana33xc20_panel *p = to_atana33xc20(panel);
  160. int ret;
  161. /* Unpreparing when already unprepared is a no-op */
  162. if (!p->prepared)
  163. return 0;
  164. /*
  165. * Purposely do a put_sync, don't use autosuspend. The panel's tcon
  166. * seems to sometimes crash when you stop giving it data and this is
  167. * the best way to ensure it will come back.
  168. *
  169. * NOTE: we still want autosuspend for cases where we only turn on
  170. * to get the EDID or otherwise send DP AUX commands to the panel.
  171. */
  172. ret = pm_runtime_put_sync_suspend(panel->dev);
  173. if (ret < 0)
  174. return ret;
  175. p->prepared = false;
  176. return 0;
  177. }
  178. static int atana33xc20_prepare(struct drm_panel *panel)
  179. {
  180. struct atana33xc20_panel *p = to_atana33xc20(panel);
  181. int ret;
  182. /* Preparing when already prepared is a no-op */
  183. if (p->prepared)
  184. return 0;
  185. ret = pm_runtime_get_sync(panel->dev);
  186. if (ret < 0) {
  187. pm_runtime_put_autosuspend(panel->dev);
  188. return ret;
  189. }
  190. p->prepared = true;
  191. return 0;
  192. }
  193. static int atana33xc20_get_modes(struct drm_panel *panel,
  194. struct drm_connector *connector)
  195. {
  196. struct atana33xc20_panel *p = to_atana33xc20(panel);
  197. struct dp_aux_ep_device *aux_ep = to_dp_aux_ep_dev(panel->dev);
  198. int num = 0;
  199. pm_runtime_get_sync(panel->dev);
  200. if (!p->edid)
  201. p->edid = drm_get_edid(connector, &aux_ep->aux->ddc);
  202. num = drm_add_edid_modes(connector, p->edid);
  203. pm_runtime_mark_last_busy(panel->dev);
  204. pm_runtime_put_autosuspend(panel->dev);
  205. return num;
  206. }
  207. static const struct drm_panel_funcs atana33xc20_funcs = {
  208. .disable = atana33xc20_disable,
  209. .enable = atana33xc20_enable,
  210. .unprepare = atana33xc20_unprepare,
  211. .prepare = atana33xc20_prepare,
  212. .get_modes = atana33xc20_get_modes,
  213. };
  214. static void atana33xc20_runtime_disable(void *data)
  215. {
  216. pm_runtime_disable(data);
  217. }
  218. static void atana33xc20_dont_use_autosuspend(void *data)
  219. {
  220. pm_runtime_dont_use_autosuspend(data);
  221. }
  222. static int atana33xc20_probe(struct dp_aux_ep_device *aux_ep)
  223. {
  224. struct atana33xc20_panel *panel;
  225. struct device *dev = &aux_ep->dev;
  226. int ret;
  227. panel = devm_kzalloc(dev, sizeof(*panel), GFP_KERNEL);
  228. if (!panel)
  229. return -ENOMEM;
  230. dev_set_drvdata(dev, panel);
  231. panel->aux = aux_ep->aux;
  232. panel->supply = devm_regulator_get(dev, "power");
  233. if (IS_ERR(panel->supply))
  234. return dev_err_probe(dev, PTR_ERR(panel->supply),
  235. "Failed to get power supply\n");
  236. panel->el_on3_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
  237. if (IS_ERR(panel->el_on3_gpio))
  238. return dev_err_probe(dev, PTR_ERR(panel->el_on3_gpio),
  239. "Failed to get enable GPIO\n");
  240. panel->no_hpd = of_property_read_bool(dev->of_node, "no-hpd");
  241. if (!panel->no_hpd) {
  242. panel->hpd_gpio = devm_gpiod_get_optional(dev, "hpd", GPIOD_IN);
  243. if (IS_ERR(panel->hpd_gpio))
  244. return dev_err_probe(dev, PTR_ERR(panel->hpd_gpio),
  245. "Failed to get HPD GPIO\n");
  246. }
  247. pm_runtime_enable(dev);
  248. ret = devm_add_action_or_reset(dev, atana33xc20_runtime_disable, dev);
  249. if (ret)
  250. return ret;
  251. pm_runtime_set_autosuspend_delay(dev, 1000);
  252. pm_runtime_use_autosuspend(dev);
  253. ret = devm_add_action_or_reset(dev, atana33xc20_dont_use_autosuspend, dev);
  254. if (ret)
  255. return ret;
  256. drm_panel_init(&panel->base, dev, &atana33xc20_funcs, DRM_MODE_CONNECTOR_eDP);
  257. pm_runtime_get_sync(dev);
  258. ret = drm_panel_dp_aux_backlight(&panel->base, aux_ep->aux);
  259. pm_runtime_mark_last_busy(dev);
  260. pm_runtime_put_autosuspend(dev);
  261. if (ret)
  262. return dev_err_probe(dev, ret,
  263. "failed to register dp aux backlight\n");
  264. drm_panel_add(&panel->base);
  265. return 0;
  266. }
  267. static void atana33xc20_remove(struct dp_aux_ep_device *aux_ep)
  268. {
  269. struct device *dev = &aux_ep->dev;
  270. struct atana33xc20_panel *panel = dev_get_drvdata(dev);
  271. drm_panel_remove(&panel->base);
  272. drm_panel_disable(&panel->base);
  273. drm_panel_unprepare(&panel->base);
  274. kfree(panel->edid);
  275. }
  276. static void atana33xc20_shutdown(struct dp_aux_ep_device *aux_ep)
  277. {
  278. struct device *dev = &aux_ep->dev;
  279. struct atana33xc20_panel *panel = dev_get_drvdata(dev);
  280. drm_panel_disable(&panel->base);
  281. drm_panel_unprepare(&panel->base);
  282. }
  283. static const struct of_device_id atana33xc20_dt_match[] = {
  284. { .compatible = "samsung,atna33xc20", },
  285. { /* sentinal */ }
  286. };
  287. MODULE_DEVICE_TABLE(of, atana33xc20_dt_match);
  288. static const struct dev_pm_ops atana33xc20_pm_ops = {
  289. SET_RUNTIME_PM_OPS(atana33xc20_suspend, atana33xc20_resume, NULL)
  290. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  291. pm_runtime_force_resume)
  292. };
  293. static struct dp_aux_ep_driver atana33xc20_driver = {
  294. .driver = {
  295. .name = "samsung_atana33xc20",
  296. .of_match_table = atana33xc20_dt_match,
  297. .pm = &atana33xc20_pm_ops,
  298. },
  299. .probe = atana33xc20_probe,
  300. .remove = atana33xc20_remove,
  301. .shutdown = atana33xc20_shutdown,
  302. };
  303. static int __init atana33xc20_init(void)
  304. {
  305. return dp_aux_dp_driver_register(&atana33xc20_driver);
  306. }
  307. module_init(atana33xc20_init);
  308. static void __exit atana33xc20_exit(void)
  309. {
  310. dp_aux_dp_driver_unregister(&atana33xc20_driver);
  311. }
  312. module_exit(atana33xc20_exit);
  313. MODULE_DESCRIPTION("Samsung ATANA33XC20 Panel Driver");
  314. MODULE_LICENSE("GPL v2");