dp_display.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2020, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/slab.h>
  7. #include <linux/uaccess.h>
  8. #include <linux/debugfs.h>
  9. #include <linux/component.h>
  10. #include <linux/of_irq.h>
  11. #include <linux/phy/phy.h>
  12. #include <linux/delay.h>
  13. #include <drm/display/drm_dp_aux_bus.h>
  14. #include <drm/drm_edid.h>
  15. #include "msm_drv.h"
  16. #include "msm_kms.h"
  17. #include "dp_ctrl.h"
  18. #include "dp_catalog.h"
  19. #include "dp_aux.h"
  20. #include "dp_reg.h"
  21. #include "dp_link.h"
  22. #include "dp_panel.h"
  23. #include "dp_display.h"
  24. #include "dp_drm.h"
  25. #include "dp_audio.h"
  26. #include "dp_debug.h"
  27. static bool psr_enabled = false;
  28. module_param(psr_enabled, bool, 0);
  29. MODULE_PARM_DESC(psr_enabled, "enable PSR for eDP and DP displays");
  30. #define HPD_STRING_SIZE 30
  31. enum {
  32. ISR_DISCONNECTED,
  33. ISR_CONNECT_PENDING,
  34. ISR_CONNECTED,
  35. ISR_HPD_REPLUG_COUNT,
  36. ISR_IRQ_HPD_PULSE_COUNT,
  37. ISR_HPD_LO_GLITH_COUNT,
  38. };
  39. /* event thread connection state */
  40. enum {
  41. ST_DISCONNECTED,
  42. ST_MAINLINK_READY,
  43. ST_CONNECTED,
  44. ST_DISCONNECT_PENDING,
  45. ST_DISPLAY_OFF,
  46. };
  47. enum {
  48. EV_NO_EVENT,
  49. /* hpd events */
  50. EV_HPD_PLUG_INT,
  51. EV_IRQ_HPD_INT,
  52. EV_HPD_UNPLUG_INT,
  53. EV_USER_NOTIFICATION,
  54. };
  55. #define EVENT_TIMEOUT (HZ/10) /* 100ms */
  56. #define DP_EVENT_Q_MAX 8
  57. #define DP_TIMEOUT_NONE 0
  58. #define WAIT_FOR_RESUME_TIMEOUT_JIFFIES (HZ / 2)
  59. struct dp_event {
  60. u32 event_id;
  61. u32 data;
  62. u32 delay;
  63. };
  64. struct dp_display_private {
  65. int irq;
  66. unsigned int id;
  67. /* state variables */
  68. bool core_initialized;
  69. bool phy_initialized;
  70. bool audio_supported;
  71. struct drm_device *drm_dev;
  72. struct dp_catalog *catalog;
  73. struct drm_dp_aux *aux;
  74. struct dp_link *link;
  75. struct dp_panel *panel;
  76. struct dp_ctrl *ctrl;
  77. struct dp_display_mode dp_mode;
  78. struct msm_dp dp_display;
  79. /* wait for audio signaling */
  80. struct completion audio_comp;
  81. /* event related only access by event thread */
  82. struct mutex event_mutex;
  83. wait_queue_head_t event_q;
  84. u32 hpd_state;
  85. u32 event_pndx;
  86. u32 event_gndx;
  87. struct task_struct *ev_tsk;
  88. struct dp_event event_list[DP_EVENT_Q_MAX];
  89. spinlock_t event_lock;
  90. bool wide_bus_supported;
  91. struct dp_audio *audio;
  92. };
  93. struct msm_dp_desc {
  94. phys_addr_t io_start;
  95. unsigned int id;
  96. bool wide_bus_supported;
  97. };
  98. static const struct msm_dp_desc sc7180_dp_descs[] = {
  99. { .io_start = 0x0ae90000, .id = MSM_DP_CONTROLLER_0, .wide_bus_supported = true },
  100. {}
  101. };
  102. static const struct msm_dp_desc sc7280_dp_descs[] = {
  103. { .io_start = 0x0ae90000, .id = MSM_DP_CONTROLLER_0, .wide_bus_supported = true },
  104. { .io_start = 0x0aea0000, .id = MSM_DP_CONTROLLER_1, .wide_bus_supported = true },
  105. {}
  106. };
  107. static const struct msm_dp_desc sc8180x_dp_descs[] = {
  108. { .io_start = 0x0ae90000, .id = MSM_DP_CONTROLLER_0, .wide_bus_supported = true },
  109. { .io_start = 0x0ae98000, .id = MSM_DP_CONTROLLER_1, .wide_bus_supported = true },
  110. { .io_start = 0x0ae9a000, .id = MSM_DP_CONTROLLER_2, .wide_bus_supported = true },
  111. {}
  112. };
  113. static const struct msm_dp_desc sc8280xp_dp_descs[] = {
  114. { .io_start = 0x0ae90000, .id = MSM_DP_CONTROLLER_0, .wide_bus_supported = true },
  115. { .io_start = 0x0ae98000, .id = MSM_DP_CONTROLLER_1, .wide_bus_supported = true },
  116. { .io_start = 0x0ae9a000, .id = MSM_DP_CONTROLLER_2, .wide_bus_supported = true },
  117. { .io_start = 0x0aea0000, .id = MSM_DP_CONTROLLER_3, .wide_bus_supported = true },
  118. { .io_start = 0x22090000, .id = MSM_DP_CONTROLLER_0, .wide_bus_supported = true },
  119. { .io_start = 0x22098000, .id = MSM_DP_CONTROLLER_1, .wide_bus_supported = true },
  120. { .io_start = 0x2209a000, .id = MSM_DP_CONTROLLER_2, .wide_bus_supported = true },
  121. { .io_start = 0x220a0000, .id = MSM_DP_CONTROLLER_3, .wide_bus_supported = true },
  122. {}
  123. };
  124. static const struct msm_dp_desc sm8650_dp_descs[] = {
  125. { .io_start = 0x0af54000, .id = MSM_DP_CONTROLLER_0, .wide_bus_supported = true },
  126. {}
  127. };
  128. static const struct msm_dp_desc x1e80100_dp_descs[] = {
  129. { .io_start = 0x0ae90000, .id = MSM_DP_CONTROLLER_0, .wide_bus_supported = true },
  130. { .io_start = 0x0ae98000, .id = MSM_DP_CONTROLLER_1, .wide_bus_supported = true },
  131. { .io_start = 0x0ae9a000, .id = MSM_DP_CONTROLLER_2, .wide_bus_supported = true },
  132. { .io_start = 0x0aea0000, .id = MSM_DP_CONTROLLER_3, .wide_bus_supported = true },
  133. {}
  134. };
  135. static const struct of_device_id dp_dt_match[] = {
  136. { .compatible = "qcom,sc7180-dp", .data = &sc7180_dp_descs },
  137. { .compatible = "qcom,sc7280-dp", .data = &sc7280_dp_descs },
  138. { .compatible = "qcom,sc7280-edp", .data = &sc7280_dp_descs },
  139. { .compatible = "qcom,sc8180x-dp", .data = &sc8180x_dp_descs },
  140. { .compatible = "qcom,sc8180x-edp", .data = &sc8180x_dp_descs },
  141. { .compatible = "qcom,sc8280xp-dp", .data = &sc8280xp_dp_descs },
  142. { .compatible = "qcom,sc8280xp-edp", .data = &sc8280xp_dp_descs },
  143. { .compatible = "qcom,sdm845-dp", .data = &sc7180_dp_descs },
  144. { .compatible = "qcom,sm8350-dp", .data = &sc7180_dp_descs },
  145. { .compatible = "qcom,sm8650-dp", .data = &sm8650_dp_descs },
  146. { .compatible = "qcom,x1e80100-dp", .data = &x1e80100_dp_descs },
  147. {}
  148. };
  149. static struct dp_display_private *dev_get_dp_display_private(struct device *dev)
  150. {
  151. struct msm_dp *dp = dev_get_drvdata(dev);
  152. return container_of(dp, struct dp_display_private, dp_display);
  153. }
  154. static int dp_add_event(struct dp_display_private *dp_priv, u32 event,
  155. u32 data, u32 delay)
  156. {
  157. unsigned long flag;
  158. struct dp_event *todo;
  159. int pndx;
  160. spin_lock_irqsave(&dp_priv->event_lock, flag);
  161. pndx = dp_priv->event_pndx + 1;
  162. pndx %= DP_EVENT_Q_MAX;
  163. if (pndx == dp_priv->event_gndx) {
  164. pr_err("event_q is full: pndx=%d gndx=%d\n",
  165. dp_priv->event_pndx, dp_priv->event_gndx);
  166. spin_unlock_irqrestore(&dp_priv->event_lock, flag);
  167. return -EPERM;
  168. }
  169. todo = &dp_priv->event_list[dp_priv->event_pndx++];
  170. dp_priv->event_pndx %= DP_EVENT_Q_MAX;
  171. todo->event_id = event;
  172. todo->data = data;
  173. todo->delay = delay;
  174. wake_up(&dp_priv->event_q);
  175. spin_unlock_irqrestore(&dp_priv->event_lock, flag);
  176. return 0;
  177. }
  178. static int dp_del_event(struct dp_display_private *dp_priv, u32 event)
  179. {
  180. unsigned long flag;
  181. struct dp_event *todo;
  182. u32 gndx;
  183. spin_lock_irqsave(&dp_priv->event_lock, flag);
  184. if (dp_priv->event_pndx == dp_priv->event_gndx) {
  185. spin_unlock_irqrestore(&dp_priv->event_lock, flag);
  186. return -ENOENT;
  187. }
  188. gndx = dp_priv->event_gndx;
  189. while (dp_priv->event_pndx != gndx) {
  190. todo = &dp_priv->event_list[gndx];
  191. if (todo->event_id == event) {
  192. todo->event_id = EV_NO_EVENT; /* deleted */
  193. todo->delay = 0;
  194. }
  195. gndx++;
  196. gndx %= DP_EVENT_Q_MAX;
  197. }
  198. spin_unlock_irqrestore(&dp_priv->event_lock, flag);
  199. return 0;
  200. }
  201. void dp_display_signal_audio_start(struct msm_dp *dp_display)
  202. {
  203. struct dp_display_private *dp;
  204. dp = container_of(dp_display, struct dp_display_private, dp_display);
  205. reinit_completion(&dp->audio_comp);
  206. }
  207. void dp_display_signal_audio_complete(struct msm_dp *dp_display)
  208. {
  209. struct dp_display_private *dp;
  210. dp = container_of(dp_display, struct dp_display_private, dp_display);
  211. complete_all(&dp->audio_comp);
  212. }
  213. static int dp_hpd_event_thread_start(struct dp_display_private *dp_priv);
  214. static int dp_display_bind(struct device *dev, struct device *master,
  215. void *data)
  216. {
  217. int rc = 0;
  218. struct dp_display_private *dp = dev_get_dp_display_private(dev);
  219. struct msm_drm_private *priv = dev_get_drvdata(master);
  220. struct drm_device *drm = priv->dev;
  221. dp->dp_display.drm_dev = drm;
  222. priv->dp[dp->id] = &dp->dp_display;
  223. dp->drm_dev = drm;
  224. dp->aux->drm_dev = drm;
  225. rc = dp_aux_register(dp->aux);
  226. if (rc) {
  227. DRM_ERROR("DRM DP AUX register failed\n");
  228. goto end;
  229. }
  230. rc = dp_register_audio_driver(dev, dp->audio);
  231. if (rc) {
  232. DRM_ERROR("Audio registration Dp failed\n");
  233. goto end;
  234. }
  235. rc = dp_hpd_event_thread_start(dp);
  236. if (rc) {
  237. DRM_ERROR("Event thread create failed\n");
  238. goto end;
  239. }
  240. return 0;
  241. end:
  242. return rc;
  243. }
  244. static void dp_display_unbind(struct device *dev, struct device *master,
  245. void *data)
  246. {
  247. struct dp_display_private *dp = dev_get_dp_display_private(dev);
  248. struct msm_drm_private *priv = dev_get_drvdata(master);
  249. kthread_stop(dp->ev_tsk);
  250. of_dp_aux_depopulate_bus(dp->aux);
  251. dp_unregister_audio_driver(dev, dp->audio);
  252. dp_aux_unregister(dp->aux);
  253. dp->drm_dev = NULL;
  254. dp->aux->drm_dev = NULL;
  255. priv->dp[dp->id] = NULL;
  256. }
  257. static const struct component_ops dp_display_comp_ops = {
  258. .bind = dp_display_bind,
  259. .unbind = dp_display_unbind,
  260. };
  261. static void dp_display_send_hpd_event(struct msm_dp *dp_display)
  262. {
  263. struct dp_display_private *dp;
  264. struct drm_connector *connector;
  265. dp = container_of(dp_display, struct dp_display_private, dp_display);
  266. connector = dp->dp_display.connector;
  267. drm_helper_hpd_irq_event(connector->dev);
  268. }
  269. static int dp_display_send_hpd_notification(struct dp_display_private *dp,
  270. bool hpd)
  271. {
  272. if ((hpd && dp->dp_display.link_ready) ||
  273. (!hpd && !dp->dp_display.link_ready)) {
  274. drm_dbg_dp(dp->drm_dev, "HPD already %s\n",
  275. (hpd ? "on" : "off"));
  276. return 0;
  277. }
  278. /* reset video pattern flag on disconnect */
  279. if (!hpd) {
  280. dp->panel->video_test = false;
  281. if (!dp->dp_display.is_edp)
  282. drm_dp_set_subconnector_property(dp->dp_display.connector,
  283. connector_status_disconnected,
  284. dp->panel->dpcd,
  285. dp->panel->downstream_ports);
  286. }
  287. dp->dp_display.link_ready = hpd;
  288. drm_dbg_dp(dp->drm_dev, "type=%d hpd=%d\n",
  289. dp->dp_display.connector_type, hpd);
  290. dp_display_send_hpd_event(&dp->dp_display);
  291. return 0;
  292. }
  293. static int dp_display_process_hpd_high(struct dp_display_private *dp)
  294. {
  295. struct drm_connector *connector = dp->dp_display.connector;
  296. const struct drm_display_info *info = &connector->display_info;
  297. int rc = 0;
  298. rc = dp_panel_read_sink_caps(dp->panel, connector);
  299. if (rc)
  300. goto end;
  301. dp_link_process_request(dp->link);
  302. if (!dp->dp_display.is_edp)
  303. drm_dp_set_subconnector_property(connector,
  304. connector_status_connected,
  305. dp->panel->dpcd,
  306. dp->panel->downstream_ports);
  307. dp->dp_display.psr_supported = dp->panel->psr_cap.version && psr_enabled;
  308. dp->audio_supported = info->has_audio;
  309. dp_panel_handle_sink_request(dp->panel);
  310. /*
  311. * set sink to normal operation mode -- D0
  312. * before dpcd read
  313. */
  314. dp_link_psm_config(dp->link, &dp->panel->link_info, false);
  315. dp_link_reset_phy_params_vx_px(dp->link);
  316. rc = dp_ctrl_on_link(dp->ctrl);
  317. if (rc) {
  318. DRM_ERROR("failed to complete DP link training\n");
  319. goto end;
  320. }
  321. dp_add_event(dp, EV_USER_NOTIFICATION, true, 0);
  322. end:
  323. return rc;
  324. }
  325. static void dp_display_host_phy_init(struct dp_display_private *dp)
  326. {
  327. drm_dbg_dp(dp->drm_dev, "type=%d core_init=%d phy_init=%d\n",
  328. dp->dp_display.connector_type, dp->core_initialized,
  329. dp->phy_initialized);
  330. if (!dp->phy_initialized) {
  331. dp_ctrl_phy_init(dp->ctrl);
  332. dp->phy_initialized = true;
  333. }
  334. }
  335. static void dp_display_host_phy_exit(struct dp_display_private *dp)
  336. {
  337. drm_dbg_dp(dp->drm_dev, "type=%d core_init=%d phy_init=%d\n",
  338. dp->dp_display.connector_type, dp->core_initialized,
  339. dp->phy_initialized);
  340. if (dp->phy_initialized) {
  341. dp_ctrl_phy_exit(dp->ctrl);
  342. dp->phy_initialized = false;
  343. }
  344. }
  345. static void dp_display_host_init(struct dp_display_private *dp)
  346. {
  347. drm_dbg_dp(dp->drm_dev, "type=%d core_init=%d phy_init=%d\n",
  348. dp->dp_display.connector_type, dp->core_initialized,
  349. dp->phy_initialized);
  350. dp_ctrl_core_clk_enable(dp->ctrl);
  351. dp_ctrl_reset_irq_ctrl(dp->ctrl, true);
  352. dp_aux_init(dp->aux);
  353. dp->core_initialized = true;
  354. }
  355. static void dp_display_host_deinit(struct dp_display_private *dp)
  356. {
  357. drm_dbg_dp(dp->drm_dev, "type=%d core_init=%d phy_init=%d\n",
  358. dp->dp_display.connector_type, dp->core_initialized,
  359. dp->phy_initialized);
  360. dp_ctrl_reset_irq_ctrl(dp->ctrl, false);
  361. dp_aux_deinit(dp->aux);
  362. dp_ctrl_core_clk_disable(dp->ctrl);
  363. dp->core_initialized = false;
  364. }
  365. static int dp_display_usbpd_configure_cb(struct device *dev)
  366. {
  367. struct dp_display_private *dp = dev_get_dp_display_private(dev);
  368. dp_display_host_phy_init(dp);
  369. return dp_display_process_hpd_high(dp);
  370. }
  371. static int dp_display_notify_disconnect(struct device *dev)
  372. {
  373. struct dp_display_private *dp = dev_get_dp_display_private(dev);
  374. dp_add_event(dp, EV_USER_NOTIFICATION, false, 0);
  375. return 0;
  376. }
  377. static void dp_display_handle_video_request(struct dp_display_private *dp)
  378. {
  379. if (dp->link->sink_request & DP_TEST_LINK_VIDEO_PATTERN) {
  380. dp->panel->video_test = true;
  381. dp_link_send_test_response(dp->link);
  382. }
  383. }
  384. static int dp_display_handle_port_status_changed(struct dp_display_private *dp)
  385. {
  386. int rc = 0;
  387. if (drm_dp_is_branch(dp->panel->dpcd) && dp->link->sink_count == 0) {
  388. drm_dbg_dp(dp->drm_dev, "sink count is zero, nothing to do\n");
  389. if (dp->hpd_state != ST_DISCONNECTED) {
  390. dp->hpd_state = ST_DISCONNECT_PENDING;
  391. dp_add_event(dp, EV_USER_NOTIFICATION, false, 0);
  392. }
  393. } else {
  394. if (dp->hpd_state == ST_DISCONNECTED) {
  395. dp->hpd_state = ST_MAINLINK_READY;
  396. rc = dp_display_process_hpd_high(dp);
  397. if (rc)
  398. dp->hpd_state = ST_DISCONNECTED;
  399. }
  400. }
  401. return rc;
  402. }
  403. static int dp_display_handle_irq_hpd(struct dp_display_private *dp)
  404. {
  405. u32 sink_request = dp->link->sink_request;
  406. drm_dbg_dp(dp->drm_dev, "%d\n", sink_request);
  407. if (dp->hpd_state == ST_DISCONNECTED) {
  408. if (sink_request & DP_LINK_STATUS_UPDATED) {
  409. drm_dbg_dp(dp->drm_dev, "Disconnected sink_request: %d\n",
  410. sink_request);
  411. DRM_ERROR("Disconnected, no DP_LINK_STATUS_UPDATED\n");
  412. return -EINVAL;
  413. }
  414. }
  415. dp_ctrl_handle_sink_request(dp->ctrl);
  416. if (sink_request & DP_TEST_LINK_VIDEO_PATTERN)
  417. dp_display_handle_video_request(dp);
  418. return 0;
  419. }
  420. static int dp_display_usbpd_attention_cb(struct device *dev)
  421. {
  422. int rc = 0;
  423. u32 sink_request;
  424. struct dp_display_private *dp = dev_get_dp_display_private(dev);
  425. /* check for any test request issued by sink */
  426. rc = dp_link_process_request(dp->link);
  427. if (!rc) {
  428. sink_request = dp->link->sink_request;
  429. drm_dbg_dp(dp->drm_dev, "hpd_state=%d sink_request=%d\n",
  430. dp->hpd_state, sink_request);
  431. if (sink_request & DS_PORT_STATUS_CHANGED)
  432. rc = dp_display_handle_port_status_changed(dp);
  433. else
  434. rc = dp_display_handle_irq_hpd(dp);
  435. }
  436. return rc;
  437. }
  438. static int dp_hpd_plug_handle(struct dp_display_private *dp, u32 data)
  439. {
  440. u32 state;
  441. int ret;
  442. struct platform_device *pdev = dp->dp_display.pdev;
  443. dp_aux_enable_xfers(dp->aux, true);
  444. mutex_lock(&dp->event_mutex);
  445. state = dp->hpd_state;
  446. drm_dbg_dp(dp->drm_dev, "Before, type=%d hpd_state=%d\n",
  447. dp->dp_display.connector_type, state);
  448. if (state == ST_DISPLAY_OFF) {
  449. mutex_unlock(&dp->event_mutex);
  450. return 0;
  451. }
  452. if (state == ST_MAINLINK_READY || state == ST_CONNECTED) {
  453. mutex_unlock(&dp->event_mutex);
  454. return 0;
  455. }
  456. if (state == ST_DISCONNECT_PENDING) {
  457. /* wait until ST_DISCONNECTED */
  458. dp_add_event(dp, EV_HPD_PLUG_INT, 0, 1); /* delay = 1 */
  459. mutex_unlock(&dp->event_mutex);
  460. return 0;
  461. }
  462. ret = pm_runtime_resume_and_get(&pdev->dev);
  463. if (ret) {
  464. DRM_ERROR("failed to pm_runtime_resume\n");
  465. mutex_unlock(&dp->event_mutex);
  466. return ret;
  467. }
  468. ret = dp_display_usbpd_configure_cb(&pdev->dev);
  469. if (ret) { /* link train failed */
  470. dp->hpd_state = ST_DISCONNECTED;
  471. pm_runtime_put_sync(&pdev->dev);
  472. } else {
  473. dp->hpd_state = ST_MAINLINK_READY;
  474. }
  475. drm_dbg_dp(dp->drm_dev, "After, type=%d hpd_state=%d\n",
  476. dp->dp_display.connector_type, state);
  477. mutex_unlock(&dp->event_mutex);
  478. /* uevent will complete connection part */
  479. return 0;
  480. };
  481. static void dp_display_handle_plugged_change(struct msm_dp *dp_display,
  482. bool plugged)
  483. {
  484. struct dp_display_private *dp;
  485. dp = container_of(dp_display,
  486. struct dp_display_private, dp_display);
  487. /* notify audio subsystem only if sink supports audio */
  488. if (dp_display->plugged_cb && dp_display->codec_dev &&
  489. dp->audio_supported)
  490. dp_display->plugged_cb(dp_display->codec_dev, plugged);
  491. }
  492. static int dp_hpd_unplug_handle(struct dp_display_private *dp, u32 data)
  493. {
  494. u32 state;
  495. struct platform_device *pdev = dp->dp_display.pdev;
  496. dp_aux_enable_xfers(dp->aux, false);
  497. mutex_lock(&dp->event_mutex);
  498. state = dp->hpd_state;
  499. drm_dbg_dp(dp->drm_dev, "Before, type=%d hpd_state=%d\n",
  500. dp->dp_display.connector_type, state);
  501. /* unplugged, no more irq_hpd handle */
  502. dp_del_event(dp, EV_IRQ_HPD_INT);
  503. if (state == ST_DISCONNECTED) {
  504. /* triggered by irq_hdp with sink_count = 0 */
  505. if (dp->link->sink_count == 0) {
  506. dp_display_host_phy_exit(dp);
  507. }
  508. dp_display_notify_disconnect(&dp->dp_display.pdev->dev);
  509. mutex_unlock(&dp->event_mutex);
  510. return 0;
  511. } else if (state == ST_DISCONNECT_PENDING) {
  512. mutex_unlock(&dp->event_mutex);
  513. return 0;
  514. } else if (state == ST_MAINLINK_READY) {
  515. dp_ctrl_off_link(dp->ctrl);
  516. dp_display_host_phy_exit(dp);
  517. dp->hpd_state = ST_DISCONNECTED;
  518. dp_display_notify_disconnect(&dp->dp_display.pdev->dev);
  519. pm_runtime_put_sync(&pdev->dev);
  520. mutex_unlock(&dp->event_mutex);
  521. return 0;
  522. }
  523. /*
  524. * We don't need separate work for disconnect as
  525. * connect/attention interrupts are disabled
  526. */
  527. dp_display_notify_disconnect(&dp->dp_display.pdev->dev);
  528. if (state == ST_DISPLAY_OFF) {
  529. dp->hpd_state = ST_DISCONNECTED;
  530. } else {
  531. dp->hpd_state = ST_DISCONNECT_PENDING;
  532. }
  533. /* signal the disconnect event early to ensure proper teardown */
  534. dp_display_handle_plugged_change(&dp->dp_display, false);
  535. drm_dbg_dp(dp->drm_dev, "After, type=%d hpd_state=%d\n",
  536. dp->dp_display.connector_type, state);
  537. /* uevent will complete disconnection part */
  538. pm_runtime_put_sync(&pdev->dev);
  539. mutex_unlock(&dp->event_mutex);
  540. return 0;
  541. }
  542. static int dp_irq_hpd_handle(struct dp_display_private *dp, u32 data)
  543. {
  544. u32 state;
  545. mutex_lock(&dp->event_mutex);
  546. /* irq_hpd can happen at either connected or disconnected state */
  547. state = dp->hpd_state;
  548. drm_dbg_dp(dp->drm_dev, "Before, type=%d hpd_state=%d\n",
  549. dp->dp_display.connector_type, state);
  550. if (state == ST_DISPLAY_OFF) {
  551. mutex_unlock(&dp->event_mutex);
  552. return 0;
  553. }
  554. if (state == ST_MAINLINK_READY || state == ST_DISCONNECT_PENDING) {
  555. /* wait until ST_CONNECTED */
  556. dp_add_event(dp, EV_IRQ_HPD_INT, 0, 1); /* delay = 1 */
  557. mutex_unlock(&dp->event_mutex);
  558. return 0;
  559. }
  560. dp_display_usbpd_attention_cb(&dp->dp_display.pdev->dev);
  561. drm_dbg_dp(dp->drm_dev, "After, type=%d hpd_state=%d\n",
  562. dp->dp_display.connector_type, state);
  563. mutex_unlock(&dp->event_mutex);
  564. return 0;
  565. }
  566. static void dp_display_deinit_sub_modules(struct dp_display_private *dp)
  567. {
  568. dp_audio_put(dp->audio);
  569. dp_panel_put(dp->panel);
  570. dp_aux_put(dp->aux);
  571. }
  572. static int dp_init_sub_modules(struct dp_display_private *dp)
  573. {
  574. int rc = 0;
  575. struct device *dev = &dp->dp_display.pdev->dev;
  576. struct dp_panel_in panel_in = {
  577. .dev = dev,
  578. };
  579. struct phy *phy;
  580. phy = devm_phy_get(dev, "dp");
  581. if (IS_ERR(phy))
  582. return PTR_ERR(phy);
  583. rc = phy_set_mode_ext(phy, PHY_MODE_DP,
  584. dp->dp_display.is_edp ? PHY_SUBMODE_EDP : PHY_SUBMODE_DP);
  585. if (rc) {
  586. DRM_ERROR("failed to set phy submode, rc = %d\n", rc);
  587. dp->catalog = NULL;
  588. goto error;
  589. }
  590. dp->catalog = dp_catalog_get(dev);
  591. if (IS_ERR(dp->catalog)) {
  592. rc = PTR_ERR(dp->catalog);
  593. DRM_ERROR("failed to initialize catalog, rc = %d\n", rc);
  594. dp->catalog = NULL;
  595. goto error;
  596. }
  597. dp->aux = dp_aux_get(dev, dp->catalog,
  598. phy,
  599. dp->dp_display.is_edp);
  600. if (IS_ERR(dp->aux)) {
  601. rc = PTR_ERR(dp->aux);
  602. DRM_ERROR("failed to initialize aux, rc = %d\n", rc);
  603. dp->aux = NULL;
  604. goto error;
  605. }
  606. dp->link = dp_link_get(dev, dp->aux);
  607. if (IS_ERR(dp->link)) {
  608. rc = PTR_ERR(dp->link);
  609. DRM_ERROR("failed to initialize link, rc = %d\n", rc);
  610. dp->link = NULL;
  611. goto error_link;
  612. }
  613. panel_in.aux = dp->aux;
  614. panel_in.catalog = dp->catalog;
  615. panel_in.link = dp->link;
  616. dp->panel = dp_panel_get(&panel_in);
  617. if (IS_ERR(dp->panel)) {
  618. rc = PTR_ERR(dp->panel);
  619. DRM_ERROR("failed to initialize panel, rc = %d\n", rc);
  620. dp->panel = NULL;
  621. goto error_link;
  622. }
  623. dp->ctrl = dp_ctrl_get(dev, dp->link, dp->panel, dp->aux,
  624. dp->catalog,
  625. phy);
  626. if (IS_ERR(dp->ctrl)) {
  627. rc = PTR_ERR(dp->ctrl);
  628. DRM_ERROR("failed to initialize ctrl, rc = %d\n", rc);
  629. dp->ctrl = NULL;
  630. goto error_ctrl;
  631. }
  632. dp->audio = dp_audio_get(dp->dp_display.pdev, dp->panel, dp->catalog);
  633. if (IS_ERR(dp->audio)) {
  634. rc = PTR_ERR(dp->audio);
  635. pr_err("failed to initialize audio, rc = %d\n", rc);
  636. dp->audio = NULL;
  637. goto error_ctrl;
  638. }
  639. return rc;
  640. error_ctrl:
  641. dp_panel_put(dp->panel);
  642. error_link:
  643. dp_aux_put(dp->aux);
  644. error:
  645. return rc;
  646. }
  647. static int dp_display_set_mode(struct msm_dp *dp_display,
  648. struct dp_display_mode *mode)
  649. {
  650. struct dp_display_private *dp;
  651. dp = container_of(dp_display, struct dp_display_private, dp_display);
  652. drm_mode_copy(&dp->panel->dp_mode.drm_mode, &mode->drm_mode);
  653. dp->panel->dp_mode.bpp = mode->bpp;
  654. dp->panel->dp_mode.out_fmt_is_yuv_420 = mode->out_fmt_is_yuv_420;
  655. dp_panel_init_panel_info(dp->panel);
  656. return 0;
  657. }
  658. static int dp_display_enable(struct dp_display_private *dp, bool force_link_train)
  659. {
  660. int rc = 0;
  661. struct msm_dp *dp_display = &dp->dp_display;
  662. drm_dbg_dp(dp->drm_dev, "sink_count=%d\n", dp->link->sink_count);
  663. if (dp_display->power_on) {
  664. drm_dbg_dp(dp->drm_dev, "Link already setup, return\n");
  665. return 0;
  666. }
  667. rc = dp_ctrl_on_stream(dp->ctrl, force_link_train);
  668. if (!rc)
  669. dp_display->power_on = true;
  670. return rc;
  671. }
  672. static int dp_display_post_enable(struct msm_dp *dp_display)
  673. {
  674. struct dp_display_private *dp;
  675. u32 rate;
  676. dp = container_of(dp_display, struct dp_display_private, dp_display);
  677. rate = dp->link->link_params.rate;
  678. if (dp->audio_supported) {
  679. dp->audio->bw_code = drm_dp_link_rate_to_bw_code(rate);
  680. dp->audio->lane_count = dp->link->link_params.num_lanes;
  681. }
  682. /* signal the connect event late to synchronize video and display */
  683. dp_display_handle_plugged_change(dp_display, true);
  684. if (dp_display->psr_supported)
  685. dp_ctrl_config_psr(dp->ctrl);
  686. return 0;
  687. }
  688. static int dp_display_disable(struct dp_display_private *dp)
  689. {
  690. struct msm_dp *dp_display = &dp->dp_display;
  691. if (!dp_display->power_on)
  692. return 0;
  693. /* wait only if audio was enabled */
  694. if (dp_display->audio_enabled) {
  695. /* signal the disconnect event */
  696. dp_display_handle_plugged_change(dp_display, false);
  697. if (!wait_for_completion_timeout(&dp->audio_comp,
  698. HZ * 5))
  699. DRM_ERROR("audio comp timeout\n");
  700. }
  701. dp_display->audio_enabled = false;
  702. if (dp->link->sink_count == 0) {
  703. /*
  704. * irq_hpd with sink_count = 0
  705. * hdmi unplugged out of dongle
  706. */
  707. dp_ctrl_off_link_stream(dp->ctrl);
  708. } else {
  709. /*
  710. * unplugged interrupt
  711. * dongle unplugged out of DUT
  712. */
  713. dp_ctrl_off(dp->ctrl);
  714. dp_display_host_phy_exit(dp);
  715. }
  716. dp_display->power_on = false;
  717. drm_dbg_dp(dp->drm_dev, "sink count: %d\n", dp->link->sink_count);
  718. return 0;
  719. }
  720. int dp_display_set_plugged_cb(struct msm_dp *dp_display,
  721. hdmi_codec_plugged_cb fn, struct device *codec_dev)
  722. {
  723. bool plugged;
  724. dp_display->plugged_cb = fn;
  725. dp_display->codec_dev = codec_dev;
  726. plugged = dp_display->link_ready;
  727. dp_display_handle_plugged_change(dp_display, plugged);
  728. return 0;
  729. }
  730. /**
  731. * dp_bridge_mode_valid - callback to determine if specified mode is valid
  732. * @bridge: Pointer to drm bridge structure
  733. * @info: display info
  734. * @mode: Pointer to drm mode structure
  735. * Returns: Validity status for specified mode
  736. */
  737. enum drm_mode_status dp_bridge_mode_valid(struct drm_bridge *bridge,
  738. const struct drm_display_info *info,
  739. const struct drm_display_mode *mode)
  740. {
  741. const u32 num_components = 3, default_bpp = 24;
  742. struct dp_display_private *dp_display;
  743. struct dp_link_info *link_info;
  744. u32 mode_rate_khz = 0, supported_rate_khz = 0, mode_bpp = 0;
  745. struct msm_dp *dp;
  746. int mode_pclk_khz = mode->clock;
  747. dp = to_dp_bridge(bridge)->dp_display;
  748. if (!dp || !mode_pclk_khz || !dp->connector) {
  749. DRM_ERROR("invalid params\n");
  750. return -EINVAL;
  751. }
  752. dp_display = container_of(dp, struct dp_display_private, dp_display);
  753. link_info = &dp_display->panel->link_info;
  754. if ((drm_mode_is_420_only(&dp->connector->display_info, mode) &&
  755. dp_display->panel->vsc_sdp_supported) ||
  756. msm_dp_wide_bus_available(dp))
  757. mode_pclk_khz /= 2;
  758. if (mode_pclk_khz > DP_MAX_PIXEL_CLK_KHZ)
  759. return MODE_CLOCK_HIGH;
  760. mode_bpp = dp->connector->display_info.bpc * num_components;
  761. if (!mode_bpp)
  762. mode_bpp = default_bpp;
  763. mode_bpp = dp_panel_get_mode_bpp(dp_display->panel,
  764. mode_bpp, mode_pclk_khz);
  765. mode_rate_khz = mode_pclk_khz * mode_bpp;
  766. supported_rate_khz = link_info->num_lanes * link_info->rate * 8;
  767. if (mode_rate_khz > supported_rate_khz)
  768. return MODE_BAD;
  769. return MODE_OK;
  770. }
  771. int dp_display_get_modes(struct msm_dp *dp)
  772. {
  773. struct dp_display_private *dp_display;
  774. if (!dp) {
  775. DRM_ERROR("invalid params\n");
  776. return 0;
  777. }
  778. dp_display = container_of(dp, struct dp_display_private, dp_display);
  779. return dp_panel_get_modes(dp_display->panel,
  780. dp->connector);
  781. }
  782. bool dp_display_check_video_test(struct msm_dp *dp)
  783. {
  784. struct dp_display_private *dp_display;
  785. dp_display = container_of(dp, struct dp_display_private, dp_display);
  786. return dp_display->panel->video_test;
  787. }
  788. int dp_display_get_test_bpp(struct msm_dp *dp)
  789. {
  790. struct dp_display_private *dp_display;
  791. if (!dp) {
  792. DRM_ERROR("invalid params\n");
  793. return 0;
  794. }
  795. dp_display = container_of(dp, struct dp_display_private, dp_display);
  796. return dp_link_bit_depth_to_bpp(
  797. dp_display->link->test_video.test_bit_depth);
  798. }
  799. void msm_dp_snapshot(struct msm_disp_state *disp_state, struct msm_dp *dp)
  800. {
  801. struct dp_display_private *dp_display;
  802. dp_display = container_of(dp, struct dp_display_private, dp_display);
  803. /*
  804. * if we are reading registers we need the link clocks to be on
  805. * however till DP cable is connected this will not happen as we
  806. * do not know the resolution to power up with. Hence check the
  807. * power_on status before dumping DP registers to avoid crash due
  808. * to unclocked access
  809. */
  810. mutex_lock(&dp_display->event_mutex);
  811. if (!dp->power_on) {
  812. mutex_unlock(&dp_display->event_mutex);
  813. return;
  814. }
  815. dp_catalog_snapshot(dp_display->catalog, disp_state);
  816. mutex_unlock(&dp_display->event_mutex);
  817. }
  818. void dp_display_set_psr(struct msm_dp *dp_display, bool enter)
  819. {
  820. struct dp_display_private *dp;
  821. if (!dp_display) {
  822. DRM_ERROR("invalid params\n");
  823. return;
  824. }
  825. dp = container_of(dp_display, struct dp_display_private, dp_display);
  826. dp_ctrl_set_psr(dp->ctrl, enter);
  827. }
  828. static int hpd_event_thread(void *data)
  829. {
  830. struct dp_display_private *dp_priv;
  831. unsigned long flag;
  832. struct dp_event *todo;
  833. int timeout_mode = 0;
  834. dp_priv = (struct dp_display_private *)data;
  835. while (1) {
  836. if (timeout_mode) {
  837. wait_event_timeout(dp_priv->event_q,
  838. (dp_priv->event_pndx == dp_priv->event_gndx) ||
  839. kthread_should_stop(), EVENT_TIMEOUT);
  840. } else {
  841. wait_event_interruptible(dp_priv->event_q,
  842. (dp_priv->event_pndx != dp_priv->event_gndx) ||
  843. kthread_should_stop());
  844. }
  845. if (kthread_should_stop())
  846. break;
  847. spin_lock_irqsave(&dp_priv->event_lock, flag);
  848. todo = &dp_priv->event_list[dp_priv->event_gndx];
  849. if (todo->delay) {
  850. struct dp_event *todo_next;
  851. dp_priv->event_gndx++;
  852. dp_priv->event_gndx %= DP_EVENT_Q_MAX;
  853. /* re enter delay event into q */
  854. todo_next = &dp_priv->event_list[dp_priv->event_pndx++];
  855. dp_priv->event_pndx %= DP_EVENT_Q_MAX;
  856. todo_next->event_id = todo->event_id;
  857. todo_next->data = todo->data;
  858. todo_next->delay = todo->delay - 1;
  859. /* clean up older event */
  860. todo->event_id = EV_NO_EVENT;
  861. todo->delay = 0;
  862. /* switch to timeout mode */
  863. timeout_mode = 1;
  864. spin_unlock_irqrestore(&dp_priv->event_lock, flag);
  865. continue;
  866. }
  867. /* timeout with no events in q */
  868. if (dp_priv->event_pndx == dp_priv->event_gndx) {
  869. spin_unlock_irqrestore(&dp_priv->event_lock, flag);
  870. continue;
  871. }
  872. dp_priv->event_gndx++;
  873. dp_priv->event_gndx %= DP_EVENT_Q_MAX;
  874. timeout_mode = 0;
  875. spin_unlock_irqrestore(&dp_priv->event_lock, flag);
  876. switch (todo->event_id) {
  877. case EV_HPD_PLUG_INT:
  878. dp_hpd_plug_handle(dp_priv, todo->data);
  879. break;
  880. case EV_HPD_UNPLUG_INT:
  881. dp_hpd_unplug_handle(dp_priv, todo->data);
  882. break;
  883. case EV_IRQ_HPD_INT:
  884. dp_irq_hpd_handle(dp_priv, todo->data);
  885. break;
  886. case EV_USER_NOTIFICATION:
  887. dp_display_send_hpd_notification(dp_priv,
  888. todo->data);
  889. break;
  890. default:
  891. break;
  892. }
  893. }
  894. return 0;
  895. }
  896. static int dp_hpd_event_thread_start(struct dp_display_private *dp_priv)
  897. {
  898. /* set event q to empty */
  899. dp_priv->event_gndx = 0;
  900. dp_priv->event_pndx = 0;
  901. dp_priv->ev_tsk = kthread_run(hpd_event_thread, dp_priv, "dp_hpd_handler");
  902. if (IS_ERR(dp_priv->ev_tsk))
  903. return PTR_ERR(dp_priv->ev_tsk);
  904. return 0;
  905. }
  906. static irqreturn_t dp_display_irq_handler(int irq, void *dev_id)
  907. {
  908. struct dp_display_private *dp = dev_id;
  909. irqreturn_t ret = IRQ_NONE;
  910. u32 hpd_isr_status;
  911. if (!dp) {
  912. DRM_ERROR("invalid data\n");
  913. return IRQ_NONE;
  914. }
  915. hpd_isr_status = dp_catalog_hpd_get_intr_status(dp->catalog);
  916. if (hpd_isr_status & 0x0F) {
  917. drm_dbg_dp(dp->drm_dev, "type=%d isr=0x%x\n",
  918. dp->dp_display.connector_type, hpd_isr_status);
  919. /* hpd related interrupts */
  920. if (hpd_isr_status & DP_DP_HPD_PLUG_INT_MASK)
  921. dp_add_event(dp, EV_HPD_PLUG_INT, 0, 0);
  922. if (hpd_isr_status & DP_DP_IRQ_HPD_INT_MASK) {
  923. dp_add_event(dp, EV_IRQ_HPD_INT, 0, 0);
  924. }
  925. if (hpd_isr_status & DP_DP_HPD_REPLUG_INT_MASK) {
  926. dp_add_event(dp, EV_HPD_UNPLUG_INT, 0, 0);
  927. dp_add_event(dp, EV_HPD_PLUG_INT, 0, 3);
  928. }
  929. if (hpd_isr_status & DP_DP_HPD_UNPLUG_INT_MASK)
  930. dp_add_event(dp, EV_HPD_UNPLUG_INT, 0, 0);
  931. ret = IRQ_HANDLED;
  932. }
  933. /* DP controller isr */
  934. ret |= dp_ctrl_isr(dp->ctrl);
  935. /* DP aux isr */
  936. ret |= dp_aux_isr(dp->aux);
  937. return ret;
  938. }
  939. static int dp_display_request_irq(struct dp_display_private *dp)
  940. {
  941. int rc = 0;
  942. struct platform_device *pdev = dp->dp_display.pdev;
  943. dp->irq = platform_get_irq(pdev, 0);
  944. if (dp->irq < 0) {
  945. DRM_ERROR("failed to get irq\n");
  946. return dp->irq;
  947. }
  948. rc = devm_request_irq(&pdev->dev, dp->irq, dp_display_irq_handler,
  949. IRQF_TRIGGER_HIGH|IRQF_NO_AUTOEN,
  950. "dp_display_isr", dp);
  951. if (rc < 0) {
  952. DRM_ERROR("failed to request IRQ%u: %d\n",
  953. dp->irq, rc);
  954. return rc;
  955. }
  956. return 0;
  957. }
  958. static const struct msm_dp_desc *dp_display_get_desc(struct platform_device *pdev)
  959. {
  960. const struct msm_dp_desc *descs = of_device_get_match_data(&pdev->dev);
  961. struct resource *res;
  962. int i;
  963. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  964. if (!res)
  965. return NULL;
  966. for (i = 0; i < descs[i].io_start; i++) {
  967. if (descs[i].io_start == res->start)
  968. return &descs[i];
  969. }
  970. dev_err(&pdev->dev, "unknown displayport instance\n");
  971. return NULL;
  972. }
  973. static int dp_display_probe_tail(struct device *dev)
  974. {
  975. struct msm_dp *dp = dev_get_drvdata(dev);
  976. int ret;
  977. /*
  978. * External bridges are mandatory for eDP interfaces: one has to
  979. * provide at least an eDP panel (which gets wrapped into panel-bridge).
  980. *
  981. * For DisplayPort interfaces external bridges are optional, so
  982. * silently ignore an error if one is not present (-ENODEV).
  983. */
  984. dp->next_bridge = devm_drm_of_get_bridge(&dp->pdev->dev, dp->pdev->dev.of_node, 1, 0);
  985. if (IS_ERR(dp->next_bridge)) {
  986. ret = PTR_ERR(dp->next_bridge);
  987. dp->next_bridge = NULL;
  988. if (dp->is_edp || ret != -ENODEV)
  989. return ret;
  990. }
  991. ret = component_add(dev, &dp_display_comp_ops);
  992. if (ret)
  993. DRM_ERROR("component add failed, rc=%d\n", ret);
  994. return ret;
  995. }
  996. static int dp_auxbus_done_probe(struct drm_dp_aux *aux)
  997. {
  998. return dp_display_probe_tail(aux->dev);
  999. }
  1000. static int dp_display_get_connector_type(struct platform_device *pdev,
  1001. const struct msm_dp_desc *desc)
  1002. {
  1003. struct device_node *node = pdev->dev.of_node;
  1004. struct device_node *aux_bus = of_get_child_by_name(node, "aux-bus");
  1005. struct device_node *panel = of_get_child_by_name(aux_bus, "panel");
  1006. int connector_type;
  1007. if (panel)
  1008. connector_type = DRM_MODE_CONNECTOR_eDP;
  1009. else
  1010. connector_type = DRM_MODE_SUBCONNECTOR_DisplayPort;
  1011. of_node_put(panel);
  1012. of_node_put(aux_bus);
  1013. return connector_type;
  1014. }
  1015. static int dp_display_probe(struct platform_device *pdev)
  1016. {
  1017. int rc = 0;
  1018. struct dp_display_private *dp;
  1019. const struct msm_dp_desc *desc;
  1020. if (!pdev || !pdev->dev.of_node) {
  1021. DRM_ERROR("pdev not found\n");
  1022. return -ENODEV;
  1023. }
  1024. dp = devm_kzalloc(&pdev->dev, sizeof(*dp), GFP_KERNEL);
  1025. if (!dp)
  1026. return -ENOMEM;
  1027. desc = dp_display_get_desc(pdev);
  1028. if (!desc)
  1029. return -EINVAL;
  1030. dp->dp_display.pdev = pdev;
  1031. dp->id = desc->id;
  1032. dp->dp_display.connector_type = dp_display_get_connector_type(pdev, desc);
  1033. dp->wide_bus_supported = desc->wide_bus_supported;
  1034. dp->dp_display.is_edp =
  1035. (dp->dp_display.connector_type == DRM_MODE_CONNECTOR_eDP);
  1036. rc = dp_init_sub_modules(dp);
  1037. if (rc) {
  1038. DRM_ERROR("init sub module failed\n");
  1039. return -EPROBE_DEFER;
  1040. }
  1041. /* setup event q */
  1042. mutex_init(&dp->event_mutex);
  1043. init_waitqueue_head(&dp->event_q);
  1044. spin_lock_init(&dp->event_lock);
  1045. /* Store DP audio handle inside DP display */
  1046. dp->dp_display.dp_audio = dp->audio;
  1047. init_completion(&dp->audio_comp);
  1048. platform_set_drvdata(pdev, &dp->dp_display);
  1049. rc = devm_pm_runtime_enable(&pdev->dev);
  1050. if (rc)
  1051. goto err;
  1052. rc = dp_display_request_irq(dp);
  1053. if (rc)
  1054. goto err;
  1055. if (dp->dp_display.is_edp) {
  1056. rc = devm_of_dp_aux_populate_bus(dp->aux, dp_auxbus_done_probe);
  1057. if (rc) {
  1058. DRM_ERROR("eDP auxbus population failed, rc=%d\n", rc);
  1059. goto err;
  1060. }
  1061. } else {
  1062. rc = dp_display_probe_tail(&pdev->dev);
  1063. if (rc)
  1064. goto err;
  1065. }
  1066. return rc;
  1067. err:
  1068. dp_display_deinit_sub_modules(dp);
  1069. return rc;
  1070. }
  1071. static void dp_display_remove(struct platform_device *pdev)
  1072. {
  1073. struct dp_display_private *dp = dev_get_dp_display_private(&pdev->dev);
  1074. component_del(&pdev->dev, &dp_display_comp_ops);
  1075. dp_display_deinit_sub_modules(dp);
  1076. platform_set_drvdata(pdev, NULL);
  1077. }
  1078. static int dp_pm_runtime_suspend(struct device *dev)
  1079. {
  1080. struct dp_display_private *dp = dev_get_dp_display_private(dev);
  1081. disable_irq(dp->irq);
  1082. if (dp->dp_display.is_edp) {
  1083. dp_display_host_phy_exit(dp);
  1084. dp_catalog_ctrl_hpd_disable(dp->catalog);
  1085. }
  1086. dp_display_host_deinit(dp);
  1087. return 0;
  1088. }
  1089. static int dp_pm_runtime_resume(struct device *dev)
  1090. {
  1091. struct dp_display_private *dp = dev_get_dp_display_private(dev);
  1092. /*
  1093. * for eDP, host cotroller, HPD block and PHY are enabled here
  1094. * but with HPD irq disabled
  1095. *
  1096. * for DP, only host controller is enabled here.
  1097. * HPD block is enabled at dp_bridge_hpd_enable()
  1098. * PHY will be enabled at plugin handler later
  1099. */
  1100. dp_display_host_init(dp);
  1101. if (dp->dp_display.is_edp) {
  1102. dp_catalog_ctrl_hpd_enable(dp->catalog);
  1103. dp_display_host_phy_init(dp);
  1104. }
  1105. enable_irq(dp->irq);
  1106. return 0;
  1107. }
  1108. static const struct dev_pm_ops dp_pm_ops = {
  1109. SET_RUNTIME_PM_OPS(dp_pm_runtime_suspend, dp_pm_runtime_resume, NULL)
  1110. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  1111. pm_runtime_force_resume)
  1112. };
  1113. static struct platform_driver dp_display_driver = {
  1114. .probe = dp_display_probe,
  1115. .remove_new = dp_display_remove,
  1116. .driver = {
  1117. .name = "msm-dp-display",
  1118. .of_match_table = dp_dt_match,
  1119. .suppress_bind_attrs = true,
  1120. .pm = &dp_pm_ops,
  1121. },
  1122. };
  1123. int __init msm_dp_register(void)
  1124. {
  1125. int ret;
  1126. ret = platform_driver_register(&dp_display_driver);
  1127. if (ret)
  1128. DRM_ERROR("Dp display driver register failed");
  1129. return ret;
  1130. }
  1131. void __exit msm_dp_unregister(void)
  1132. {
  1133. platform_driver_unregister(&dp_display_driver);
  1134. }
  1135. bool msm_dp_is_yuv_420_enabled(const struct msm_dp *dp_display,
  1136. const struct drm_display_mode *mode)
  1137. {
  1138. struct dp_display_private *dp;
  1139. const struct drm_display_info *info;
  1140. dp = container_of(dp_display, struct dp_display_private, dp_display);
  1141. info = &dp_display->connector->display_info;
  1142. return dp->panel->vsc_sdp_supported && drm_mode_is_420_only(info, mode);
  1143. }
  1144. bool msm_dp_needs_periph_flush(const struct msm_dp *dp_display,
  1145. const struct drm_display_mode *mode)
  1146. {
  1147. return msm_dp_is_yuv_420_enabled(dp_display, mode);
  1148. }
  1149. bool msm_dp_wide_bus_available(const struct msm_dp *dp_display)
  1150. {
  1151. struct dp_display_private *dp;
  1152. dp = container_of(dp_display, struct dp_display_private, dp_display);
  1153. if (dp->dp_mode.out_fmt_is_yuv_420)
  1154. return false;
  1155. return dp->wide_bus_supported;
  1156. }
  1157. void dp_display_debugfs_init(struct msm_dp *dp_display, struct dentry *root, bool is_edp)
  1158. {
  1159. struct dp_display_private *dp;
  1160. struct device *dev;
  1161. int rc;
  1162. dp = container_of(dp_display, struct dp_display_private, dp_display);
  1163. dev = &dp->dp_display.pdev->dev;
  1164. rc = dp_debug_init(dev, dp->panel, dp->link, dp->dp_display.connector, root, is_edp);
  1165. if (rc)
  1166. DRM_ERROR("failed to initialize debug, rc = %d\n", rc);
  1167. }
  1168. int msm_dp_modeset_init(struct msm_dp *dp_display, struct drm_device *dev,
  1169. struct drm_encoder *encoder, bool yuv_supported)
  1170. {
  1171. struct dp_display_private *dp_priv;
  1172. int ret;
  1173. dp_display->drm_dev = dev;
  1174. dp_priv = container_of(dp_display, struct dp_display_private, dp_display);
  1175. ret = dp_bridge_init(dp_display, dev, encoder);
  1176. if (ret) {
  1177. DRM_DEV_ERROR(dev->dev,
  1178. "failed to create dp bridge: %d\n", ret);
  1179. return ret;
  1180. }
  1181. dp_display->connector = dp_drm_connector_init(dp_display, encoder, yuv_supported);
  1182. if (IS_ERR(dp_display->connector)) {
  1183. ret = PTR_ERR(dp_display->connector);
  1184. DRM_DEV_ERROR(dev->dev,
  1185. "failed to create dp connector: %d\n", ret);
  1186. dp_display->connector = NULL;
  1187. return ret;
  1188. }
  1189. dp_priv->panel->connector = dp_display->connector;
  1190. return 0;
  1191. }
  1192. void dp_bridge_atomic_enable(struct drm_bridge *drm_bridge,
  1193. struct drm_bridge_state *old_bridge_state)
  1194. {
  1195. struct msm_dp_bridge *dp_bridge = to_dp_bridge(drm_bridge);
  1196. struct msm_dp *dp = dp_bridge->dp_display;
  1197. int rc = 0;
  1198. struct dp_display_private *dp_display;
  1199. u32 state;
  1200. bool force_link_train = false;
  1201. dp_display = container_of(dp, struct dp_display_private, dp_display);
  1202. if (!dp_display->dp_mode.drm_mode.clock) {
  1203. DRM_ERROR("invalid params\n");
  1204. return;
  1205. }
  1206. if (dp->is_edp)
  1207. dp_hpd_plug_handle(dp_display, 0);
  1208. mutex_lock(&dp_display->event_mutex);
  1209. if (pm_runtime_resume_and_get(&dp->pdev->dev)) {
  1210. DRM_ERROR("failed to pm_runtime_resume\n");
  1211. mutex_unlock(&dp_display->event_mutex);
  1212. return;
  1213. }
  1214. state = dp_display->hpd_state;
  1215. if (state != ST_DISPLAY_OFF && state != ST_MAINLINK_READY) {
  1216. mutex_unlock(&dp_display->event_mutex);
  1217. return;
  1218. }
  1219. rc = dp_display_set_mode(dp, &dp_display->dp_mode);
  1220. if (rc) {
  1221. DRM_ERROR("Failed to perform a mode set, rc=%d\n", rc);
  1222. mutex_unlock(&dp_display->event_mutex);
  1223. return;
  1224. }
  1225. state = dp_display->hpd_state;
  1226. if (state == ST_DISPLAY_OFF) {
  1227. dp_display_host_phy_init(dp_display);
  1228. force_link_train = true;
  1229. }
  1230. dp_display_enable(dp_display, force_link_train);
  1231. rc = dp_display_post_enable(dp);
  1232. if (rc) {
  1233. DRM_ERROR("DP display post enable failed, rc=%d\n", rc);
  1234. dp_display_disable(dp_display);
  1235. }
  1236. /* completed connection */
  1237. dp_display->hpd_state = ST_CONNECTED;
  1238. drm_dbg_dp(dp->drm_dev, "type=%d Done\n", dp->connector_type);
  1239. mutex_unlock(&dp_display->event_mutex);
  1240. }
  1241. void dp_bridge_atomic_disable(struct drm_bridge *drm_bridge,
  1242. struct drm_bridge_state *old_bridge_state)
  1243. {
  1244. struct msm_dp_bridge *dp_bridge = to_dp_bridge(drm_bridge);
  1245. struct msm_dp *dp = dp_bridge->dp_display;
  1246. struct dp_display_private *dp_display;
  1247. dp_display = container_of(dp, struct dp_display_private, dp_display);
  1248. dp_ctrl_push_idle(dp_display->ctrl);
  1249. }
  1250. void dp_bridge_atomic_post_disable(struct drm_bridge *drm_bridge,
  1251. struct drm_bridge_state *old_bridge_state)
  1252. {
  1253. struct msm_dp_bridge *dp_bridge = to_dp_bridge(drm_bridge);
  1254. struct msm_dp *dp = dp_bridge->dp_display;
  1255. u32 state;
  1256. struct dp_display_private *dp_display;
  1257. dp_display = container_of(dp, struct dp_display_private, dp_display);
  1258. if (dp->is_edp)
  1259. dp_hpd_unplug_handle(dp_display, 0);
  1260. mutex_lock(&dp_display->event_mutex);
  1261. state = dp_display->hpd_state;
  1262. if (state != ST_DISCONNECT_PENDING && state != ST_CONNECTED)
  1263. drm_dbg_dp(dp->drm_dev, "type=%d wrong hpd_state=%d\n",
  1264. dp->connector_type, state);
  1265. dp_display_disable(dp_display);
  1266. state = dp_display->hpd_state;
  1267. if (state == ST_DISCONNECT_PENDING) {
  1268. /* completed disconnection */
  1269. dp_display->hpd_state = ST_DISCONNECTED;
  1270. } else {
  1271. dp_display->hpd_state = ST_DISPLAY_OFF;
  1272. }
  1273. drm_dbg_dp(dp->drm_dev, "type=%d Done\n", dp->connector_type);
  1274. pm_runtime_put_sync(&dp->pdev->dev);
  1275. mutex_unlock(&dp_display->event_mutex);
  1276. }
  1277. void dp_bridge_mode_set(struct drm_bridge *drm_bridge,
  1278. const struct drm_display_mode *mode,
  1279. const struct drm_display_mode *adjusted_mode)
  1280. {
  1281. struct msm_dp_bridge *dp_bridge = to_dp_bridge(drm_bridge);
  1282. struct msm_dp *dp = dp_bridge->dp_display;
  1283. struct dp_display_private *dp_display;
  1284. struct dp_panel *dp_panel;
  1285. dp_display = container_of(dp, struct dp_display_private, dp_display);
  1286. dp_panel = dp_display->panel;
  1287. memset(&dp_display->dp_mode, 0x0, sizeof(struct dp_display_mode));
  1288. if (dp_display_check_video_test(dp))
  1289. dp_display->dp_mode.bpp = dp_display_get_test_bpp(dp);
  1290. else /* Default num_components per pixel = 3 */
  1291. dp_display->dp_mode.bpp = dp->connector->display_info.bpc * 3;
  1292. if (!dp_display->dp_mode.bpp)
  1293. dp_display->dp_mode.bpp = 24; /* Default bpp */
  1294. drm_mode_copy(&dp_display->dp_mode.drm_mode, adjusted_mode);
  1295. dp_display->dp_mode.v_active_low =
  1296. !!(dp_display->dp_mode.drm_mode.flags & DRM_MODE_FLAG_NVSYNC);
  1297. dp_display->dp_mode.h_active_low =
  1298. !!(dp_display->dp_mode.drm_mode.flags & DRM_MODE_FLAG_NHSYNC);
  1299. dp_display->dp_mode.out_fmt_is_yuv_420 =
  1300. drm_mode_is_420_only(&dp->connector->display_info, adjusted_mode) &&
  1301. dp_panel->vsc_sdp_supported;
  1302. /* populate wide_bus_support to different layers */
  1303. dp_display->ctrl->wide_bus_en =
  1304. dp_display->dp_mode.out_fmt_is_yuv_420 ? false : dp_display->wide_bus_supported;
  1305. dp_display->catalog->wide_bus_en =
  1306. dp_display->dp_mode.out_fmt_is_yuv_420 ? false : dp_display->wide_bus_supported;
  1307. }
  1308. void dp_bridge_hpd_enable(struct drm_bridge *bridge)
  1309. {
  1310. struct msm_dp_bridge *dp_bridge = to_dp_bridge(bridge);
  1311. struct msm_dp *dp_display = dp_bridge->dp_display;
  1312. struct dp_display_private *dp = container_of(dp_display, struct dp_display_private, dp_display);
  1313. /*
  1314. * this is for external DP with hpd irq enabled case,
  1315. * step-1: dp_pm_runtime_resume() enable dp host only
  1316. * step-2: enable hdp block and have hpd irq enabled here
  1317. * step-3: waiting for plugin irq while phy is not initialized
  1318. * step-4: DP PHY is initialized at plugin handler before link training
  1319. *
  1320. */
  1321. mutex_lock(&dp->event_mutex);
  1322. if (pm_runtime_resume_and_get(&dp_display->pdev->dev)) {
  1323. DRM_ERROR("failed to resume power\n");
  1324. mutex_unlock(&dp->event_mutex);
  1325. return;
  1326. }
  1327. dp_catalog_ctrl_hpd_enable(dp->catalog);
  1328. /* enable HDP interrupts */
  1329. dp_catalog_hpd_config_intr(dp->catalog, DP_DP_HPD_INT_MASK, true);
  1330. dp_display->internal_hpd = true;
  1331. mutex_unlock(&dp->event_mutex);
  1332. }
  1333. void dp_bridge_hpd_disable(struct drm_bridge *bridge)
  1334. {
  1335. struct msm_dp_bridge *dp_bridge = to_dp_bridge(bridge);
  1336. struct msm_dp *dp_display = dp_bridge->dp_display;
  1337. struct dp_display_private *dp = container_of(dp_display, struct dp_display_private, dp_display);
  1338. mutex_lock(&dp->event_mutex);
  1339. /* disable HDP interrupts */
  1340. dp_catalog_hpd_config_intr(dp->catalog, DP_DP_HPD_INT_MASK, false);
  1341. dp_catalog_ctrl_hpd_disable(dp->catalog);
  1342. dp_display->internal_hpd = false;
  1343. pm_runtime_put_sync(&dp_display->pdev->dev);
  1344. mutex_unlock(&dp->event_mutex);
  1345. }
  1346. void dp_bridge_hpd_notify(struct drm_bridge *bridge,
  1347. enum drm_connector_status status)
  1348. {
  1349. struct msm_dp_bridge *dp_bridge = to_dp_bridge(bridge);
  1350. struct msm_dp *dp_display = dp_bridge->dp_display;
  1351. struct dp_display_private *dp = container_of(dp_display, struct dp_display_private, dp_display);
  1352. /* Without next_bridge interrupts are handled by the DP core directly */
  1353. if (dp_display->internal_hpd)
  1354. return;
  1355. if (!dp_display->link_ready && status == connector_status_connected)
  1356. dp_add_event(dp, EV_HPD_PLUG_INT, 0, 0);
  1357. else if (dp_display->link_ready && status == connector_status_disconnected)
  1358. dp_add_event(dp, EV_HPD_UNPLUG_INT, 0, 0);
  1359. }