cdn-dp-core.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
  4. * Author: Chris Zhong <zyw@rock-chips.com>
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/component.h>
  8. #include <linux/extcon.h>
  9. #include <linux/firmware.h>
  10. #include <linux/mfd/syscon.h>
  11. #include <linux/phy/phy.h>
  12. #include <linux/regmap.h>
  13. #include <linux/reset.h>
  14. #include <sound/hdmi-codec.h>
  15. #include <drm/display/drm_dp_helper.h>
  16. #include <drm/drm_atomic_helper.h>
  17. #include <drm/drm_edid.h>
  18. #include <drm/drm_of.h>
  19. #include <drm/drm_probe_helper.h>
  20. #include <drm/drm_simple_kms_helper.h>
  21. #include "cdn-dp-core.h"
  22. #include "cdn-dp-reg.h"
  23. static inline struct cdn_dp_device *connector_to_dp(struct drm_connector *connector)
  24. {
  25. return container_of(connector, struct cdn_dp_device, connector);
  26. }
  27. static inline struct cdn_dp_device *encoder_to_dp(struct drm_encoder *encoder)
  28. {
  29. struct rockchip_encoder *rkencoder = to_rockchip_encoder(encoder);
  30. return container_of(rkencoder, struct cdn_dp_device, encoder);
  31. }
  32. #define GRF_SOC_CON9 0x6224
  33. #define DP_SEL_VOP_LIT BIT(12)
  34. #define GRF_SOC_CON26 0x6268
  35. #define DPTX_HPD_SEL (3 << 12)
  36. #define DPTX_HPD_DEL (2 << 12)
  37. #define DPTX_HPD_SEL_MASK (3 << 28)
  38. #define CDN_FW_TIMEOUT_MS (64 * 1000)
  39. #define CDN_DPCD_TIMEOUT_MS 5000
  40. #define CDN_DP_FIRMWARE "rockchip/dptx.bin"
  41. MODULE_FIRMWARE(CDN_DP_FIRMWARE);
  42. struct cdn_dp_data {
  43. u8 max_phy;
  44. };
  45. static struct cdn_dp_data rk3399_cdn_dp = {
  46. .max_phy = 2,
  47. };
  48. static const struct of_device_id cdn_dp_dt_ids[] = {
  49. { .compatible = "rockchip,rk3399-cdn-dp",
  50. .data = (void *)&rk3399_cdn_dp },
  51. {}
  52. };
  53. MODULE_DEVICE_TABLE(of, cdn_dp_dt_ids);
  54. static int cdn_dp_grf_write(struct cdn_dp_device *dp,
  55. unsigned int reg, unsigned int val)
  56. {
  57. int ret;
  58. ret = clk_prepare_enable(dp->grf_clk);
  59. if (ret) {
  60. DRM_DEV_ERROR(dp->dev, "Failed to prepare_enable grf clock\n");
  61. return ret;
  62. }
  63. ret = regmap_write(dp->grf, reg, val);
  64. if (ret) {
  65. DRM_DEV_ERROR(dp->dev, "Could not write to GRF: %d\n", ret);
  66. clk_disable_unprepare(dp->grf_clk);
  67. return ret;
  68. }
  69. clk_disable_unprepare(dp->grf_clk);
  70. return 0;
  71. }
  72. static int cdn_dp_clk_enable(struct cdn_dp_device *dp)
  73. {
  74. int ret;
  75. unsigned long rate;
  76. ret = clk_prepare_enable(dp->pclk);
  77. if (ret < 0) {
  78. DRM_DEV_ERROR(dp->dev, "cannot enable dp pclk %d\n", ret);
  79. goto err_pclk;
  80. }
  81. ret = clk_prepare_enable(dp->core_clk);
  82. if (ret < 0) {
  83. DRM_DEV_ERROR(dp->dev, "cannot enable core_clk %d\n", ret);
  84. goto err_core_clk;
  85. }
  86. ret = pm_runtime_get_sync(dp->dev);
  87. if (ret < 0) {
  88. DRM_DEV_ERROR(dp->dev, "cannot get pm runtime %d\n", ret);
  89. goto err_pm_runtime_get;
  90. }
  91. reset_control_assert(dp->core_rst);
  92. reset_control_assert(dp->dptx_rst);
  93. reset_control_assert(dp->apb_rst);
  94. reset_control_deassert(dp->core_rst);
  95. reset_control_deassert(dp->dptx_rst);
  96. reset_control_deassert(dp->apb_rst);
  97. rate = clk_get_rate(dp->core_clk);
  98. if (!rate) {
  99. DRM_DEV_ERROR(dp->dev, "get clk rate failed\n");
  100. ret = -EINVAL;
  101. goto err_set_rate;
  102. }
  103. cdn_dp_set_fw_clk(dp, rate);
  104. cdn_dp_clock_reset(dp);
  105. return 0;
  106. err_set_rate:
  107. pm_runtime_put(dp->dev);
  108. err_pm_runtime_get:
  109. clk_disable_unprepare(dp->core_clk);
  110. err_core_clk:
  111. clk_disable_unprepare(dp->pclk);
  112. err_pclk:
  113. return ret;
  114. }
  115. static void cdn_dp_clk_disable(struct cdn_dp_device *dp)
  116. {
  117. pm_runtime_put_sync(dp->dev);
  118. clk_disable_unprepare(dp->pclk);
  119. clk_disable_unprepare(dp->core_clk);
  120. }
  121. static int cdn_dp_get_port_lanes(struct cdn_dp_port *port)
  122. {
  123. struct extcon_dev *edev = port->extcon;
  124. union extcon_property_value property;
  125. int dptx;
  126. u8 lanes;
  127. dptx = extcon_get_state(edev, EXTCON_DISP_DP);
  128. if (dptx > 0) {
  129. extcon_get_property(edev, EXTCON_DISP_DP,
  130. EXTCON_PROP_USB_SS, &property);
  131. if (property.intval)
  132. lanes = 2;
  133. else
  134. lanes = 4;
  135. } else {
  136. lanes = 0;
  137. }
  138. return lanes;
  139. }
  140. static int cdn_dp_get_sink_count(struct cdn_dp_device *dp, u8 *sink_count)
  141. {
  142. int ret;
  143. u8 value;
  144. *sink_count = 0;
  145. ret = cdn_dp_dpcd_read(dp, DP_SINK_COUNT, &value, 1);
  146. if (ret)
  147. return ret;
  148. *sink_count = DP_GET_SINK_COUNT(value);
  149. return 0;
  150. }
  151. static struct cdn_dp_port *cdn_dp_connected_port(struct cdn_dp_device *dp)
  152. {
  153. struct cdn_dp_port *port;
  154. int i, lanes;
  155. for (i = 0; i < dp->ports; i++) {
  156. port = dp->port[i];
  157. lanes = cdn_dp_get_port_lanes(port);
  158. if (lanes)
  159. return port;
  160. }
  161. return NULL;
  162. }
  163. static bool cdn_dp_check_sink_connection(struct cdn_dp_device *dp)
  164. {
  165. unsigned long timeout = jiffies + msecs_to_jiffies(CDN_DPCD_TIMEOUT_MS);
  166. struct cdn_dp_port *port;
  167. u8 sink_count = 0;
  168. if (dp->active_port < 0 || dp->active_port >= dp->ports) {
  169. DRM_DEV_ERROR(dp->dev, "active_port is wrong!\n");
  170. return false;
  171. }
  172. port = dp->port[dp->active_port];
  173. /*
  174. * Attempt to read sink count, retry in case the sink may not be ready.
  175. *
  176. * Sinks are *supposed* to come up within 1ms from an off state, but
  177. * some docks need more time to power up.
  178. */
  179. while (time_before(jiffies, timeout)) {
  180. if (!extcon_get_state(port->extcon, EXTCON_DISP_DP))
  181. return false;
  182. if (!cdn_dp_get_sink_count(dp, &sink_count))
  183. return sink_count ? true : false;
  184. usleep_range(5000, 10000);
  185. }
  186. DRM_DEV_ERROR(dp->dev, "Get sink capability timed out\n");
  187. return false;
  188. }
  189. static enum drm_connector_status
  190. cdn_dp_connector_detect(struct drm_connector *connector, bool force)
  191. {
  192. struct cdn_dp_device *dp = connector_to_dp(connector);
  193. enum drm_connector_status status = connector_status_disconnected;
  194. mutex_lock(&dp->lock);
  195. if (dp->connected)
  196. status = connector_status_connected;
  197. mutex_unlock(&dp->lock);
  198. return status;
  199. }
  200. static void cdn_dp_connector_destroy(struct drm_connector *connector)
  201. {
  202. drm_connector_unregister(connector);
  203. drm_connector_cleanup(connector);
  204. }
  205. static const struct drm_connector_funcs cdn_dp_atomic_connector_funcs = {
  206. .detect = cdn_dp_connector_detect,
  207. .destroy = cdn_dp_connector_destroy,
  208. .fill_modes = drm_helper_probe_single_connector_modes,
  209. .reset = drm_atomic_helper_connector_reset,
  210. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  211. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  212. };
  213. static int cdn_dp_connector_get_modes(struct drm_connector *connector)
  214. {
  215. struct cdn_dp_device *dp = connector_to_dp(connector);
  216. int ret = 0;
  217. mutex_lock(&dp->lock);
  218. ret = drm_edid_connector_add_modes(connector);
  219. mutex_unlock(&dp->lock);
  220. return ret;
  221. }
  222. static enum drm_mode_status
  223. cdn_dp_connector_mode_valid(struct drm_connector *connector,
  224. struct drm_display_mode *mode)
  225. {
  226. struct cdn_dp_device *dp = connector_to_dp(connector);
  227. struct drm_display_info *display_info = &dp->connector.display_info;
  228. u32 requested, actual, rate, sink_max, source_max = 0;
  229. u8 lanes, bpc;
  230. /* If DP is disconnected, every mode is invalid */
  231. if (!dp->connected)
  232. return MODE_BAD;
  233. switch (display_info->bpc) {
  234. case 10:
  235. bpc = 10;
  236. break;
  237. case 6:
  238. bpc = 6;
  239. break;
  240. default:
  241. bpc = 8;
  242. break;
  243. }
  244. requested = mode->clock * bpc * 3 / 1000;
  245. source_max = dp->lanes;
  246. sink_max = drm_dp_max_lane_count(dp->dpcd);
  247. lanes = min(source_max, sink_max);
  248. source_max = drm_dp_bw_code_to_link_rate(CDN_DP_MAX_LINK_RATE);
  249. sink_max = drm_dp_max_link_rate(dp->dpcd);
  250. rate = min(source_max, sink_max);
  251. actual = rate * lanes / 100;
  252. /* efficiency is about 0.8 */
  253. actual = actual * 8 / 10;
  254. if (requested > actual) {
  255. DRM_DEV_DEBUG_KMS(dp->dev,
  256. "requested=%d, actual=%d, clock=%d\n",
  257. requested, actual, mode->clock);
  258. return MODE_CLOCK_HIGH;
  259. }
  260. return MODE_OK;
  261. }
  262. static struct drm_connector_helper_funcs cdn_dp_connector_helper_funcs = {
  263. .get_modes = cdn_dp_connector_get_modes,
  264. .mode_valid = cdn_dp_connector_mode_valid,
  265. };
  266. static int cdn_dp_firmware_init(struct cdn_dp_device *dp)
  267. {
  268. int ret;
  269. const u32 *iram_data, *dram_data;
  270. const struct firmware *fw = dp->fw;
  271. const struct cdn_firmware_header *hdr;
  272. hdr = (struct cdn_firmware_header *)fw->data;
  273. if (fw->size != le32_to_cpu(hdr->size_bytes)) {
  274. DRM_DEV_ERROR(dp->dev, "firmware is invalid\n");
  275. return -EINVAL;
  276. }
  277. iram_data = (const u32 *)(fw->data + hdr->header_size);
  278. dram_data = (const u32 *)(fw->data + hdr->header_size + hdr->iram_size);
  279. ret = cdn_dp_load_firmware(dp, iram_data, hdr->iram_size,
  280. dram_data, hdr->dram_size);
  281. if (ret)
  282. return ret;
  283. ret = cdn_dp_set_firmware_active(dp, true);
  284. if (ret) {
  285. DRM_DEV_ERROR(dp->dev, "active ucpu failed: %d\n", ret);
  286. return ret;
  287. }
  288. return cdn_dp_event_config(dp);
  289. }
  290. static int cdn_dp_get_sink_capability(struct cdn_dp_device *dp)
  291. {
  292. const struct drm_display_info *info = &dp->connector.display_info;
  293. int ret;
  294. if (!cdn_dp_check_sink_connection(dp))
  295. return -ENODEV;
  296. ret = cdn_dp_dpcd_read(dp, DP_DPCD_REV, dp->dpcd,
  297. DP_RECEIVER_CAP_SIZE);
  298. if (ret) {
  299. DRM_DEV_ERROR(dp->dev, "Failed to get caps %d\n", ret);
  300. return ret;
  301. }
  302. drm_edid_free(dp->drm_edid);
  303. dp->drm_edid = drm_edid_read_custom(&dp->connector,
  304. cdn_dp_get_edid_block, dp);
  305. drm_edid_connector_update(&dp->connector, dp->drm_edid);
  306. dp->sink_has_audio = info->has_audio;
  307. if (dp->drm_edid)
  308. DRM_DEV_DEBUG_KMS(dp->dev, "got edid: width[%d] x height[%d]\n",
  309. info->width_mm / 10, info->height_mm / 10);
  310. return 0;
  311. }
  312. static int cdn_dp_enable_phy(struct cdn_dp_device *dp, struct cdn_dp_port *port)
  313. {
  314. union extcon_property_value property;
  315. int ret;
  316. if (!port->phy_enabled) {
  317. ret = phy_power_on(port->phy);
  318. if (ret) {
  319. DRM_DEV_ERROR(dp->dev, "phy power on failed: %d\n",
  320. ret);
  321. goto err_phy;
  322. }
  323. port->phy_enabled = true;
  324. }
  325. ret = cdn_dp_grf_write(dp, GRF_SOC_CON26,
  326. DPTX_HPD_SEL_MASK | DPTX_HPD_SEL);
  327. if (ret) {
  328. DRM_DEV_ERROR(dp->dev, "Failed to write HPD_SEL %d\n", ret);
  329. goto err_power_on;
  330. }
  331. ret = cdn_dp_get_hpd_status(dp);
  332. if (ret <= 0) {
  333. if (!ret)
  334. DRM_DEV_ERROR(dp->dev, "hpd does not exist\n");
  335. goto err_power_on;
  336. }
  337. ret = extcon_get_property(port->extcon, EXTCON_DISP_DP,
  338. EXTCON_PROP_USB_TYPEC_POLARITY, &property);
  339. if (ret) {
  340. DRM_DEV_ERROR(dp->dev, "get property failed\n");
  341. goto err_power_on;
  342. }
  343. port->lanes = cdn_dp_get_port_lanes(port);
  344. ret = cdn_dp_set_host_cap(dp, port->lanes, property.intval);
  345. if (ret) {
  346. DRM_DEV_ERROR(dp->dev, "set host capabilities failed: %d\n",
  347. ret);
  348. goto err_power_on;
  349. }
  350. dp->active_port = port->id;
  351. return 0;
  352. err_power_on:
  353. if (phy_power_off(port->phy))
  354. DRM_DEV_ERROR(dp->dev, "phy power off failed: %d", ret);
  355. else
  356. port->phy_enabled = false;
  357. err_phy:
  358. cdn_dp_grf_write(dp, GRF_SOC_CON26,
  359. DPTX_HPD_SEL_MASK | DPTX_HPD_DEL);
  360. return ret;
  361. }
  362. static int cdn_dp_disable_phy(struct cdn_dp_device *dp,
  363. struct cdn_dp_port *port)
  364. {
  365. int ret;
  366. if (port->phy_enabled) {
  367. ret = phy_power_off(port->phy);
  368. if (ret) {
  369. DRM_DEV_ERROR(dp->dev, "phy power off failed: %d", ret);
  370. return ret;
  371. }
  372. }
  373. port->phy_enabled = false;
  374. port->lanes = 0;
  375. dp->active_port = -1;
  376. return 0;
  377. }
  378. static int cdn_dp_disable(struct cdn_dp_device *dp)
  379. {
  380. int ret, i;
  381. if (!dp->active)
  382. return 0;
  383. for (i = 0; i < dp->ports; i++)
  384. cdn_dp_disable_phy(dp, dp->port[i]);
  385. ret = cdn_dp_grf_write(dp, GRF_SOC_CON26,
  386. DPTX_HPD_SEL_MASK | DPTX_HPD_DEL);
  387. if (ret) {
  388. DRM_DEV_ERROR(dp->dev, "Failed to clear hpd sel %d\n",
  389. ret);
  390. return ret;
  391. }
  392. cdn_dp_set_firmware_active(dp, false);
  393. cdn_dp_clk_disable(dp);
  394. dp->active = false;
  395. dp->max_lanes = 0;
  396. dp->max_rate = 0;
  397. if (!dp->connected) {
  398. drm_edid_free(dp->drm_edid);
  399. dp->drm_edid = NULL;
  400. }
  401. return 0;
  402. }
  403. static int cdn_dp_enable(struct cdn_dp_device *dp)
  404. {
  405. int ret, i, lanes;
  406. struct cdn_dp_port *port;
  407. port = cdn_dp_connected_port(dp);
  408. if (!port) {
  409. DRM_DEV_ERROR(dp->dev,
  410. "Can't enable without connection\n");
  411. return -ENODEV;
  412. }
  413. if (dp->active)
  414. return 0;
  415. ret = cdn_dp_clk_enable(dp);
  416. if (ret)
  417. return ret;
  418. ret = cdn_dp_firmware_init(dp);
  419. if (ret) {
  420. DRM_DEV_ERROR(dp->dev, "firmware init failed: %d", ret);
  421. goto err_clk_disable;
  422. }
  423. /* only enable the port that connected with downstream device */
  424. for (i = port->id; i < dp->ports; i++) {
  425. port = dp->port[i];
  426. lanes = cdn_dp_get_port_lanes(port);
  427. if (lanes) {
  428. ret = cdn_dp_enable_phy(dp, port);
  429. if (ret)
  430. continue;
  431. ret = cdn_dp_get_sink_capability(dp);
  432. if (ret) {
  433. cdn_dp_disable_phy(dp, port);
  434. } else {
  435. dp->active = true;
  436. dp->lanes = port->lanes;
  437. return 0;
  438. }
  439. }
  440. }
  441. err_clk_disable:
  442. cdn_dp_clk_disable(dp);
  443. return ret;
  444. }
  445. static void cdn_dp_encoder_mode_set(struct drm_encoder *encoder,
  446. struct drm_display_mode *mode,
  447. struct drm_display_mode *adjusted)
  448. {
  449. struct cdn_dp_device *dp = encoder_to_dp(encoder);
  450. struct drm_display_info *display_info = &dp->connector.display_info;
  451. struct video_info *video = &dp->video_info;
  452. switch (display_info->bpc) {
  453. case 10:
  454. video->color_depth = 10;
  455. break;
  456. case 6:
  457. video->color_depth = 6;
  458. break;
  459. default:
  460. video->color_depth = 8;
  461. break;
  462. }
  463. video->color_fmt = PXL_RGB;
  464. video->v_sync_polarity = !!(mode->flags & DRM_MODE_FLAG_NVSYNC);
  465. video->h_sync_polarity = !!(mode->flags & DRM_MODE_FLAG_NHSYNC);
  466. drm_mode_copy(&dp->mode, adjusted);
  467. }
  468. static bool cdn_dp_check_link_status(struct cdn_dp_device *dp)
  469. {
  470. u8 link_status[DP_LINK_STATUS_SIZE];
  471. struct cdn_dp_port *port = cdn_dp_connected_port(dp);
  472. u8 sink_lanes = drm_dp_max_lane_count(dp->dpcd);
  473. if (!port || !dp->max_rate || !dp->max_lanes)
  474. return false;
  475. if (cdn_dp_dpcd_read(dp, DP_LANE0_1_STATUS, link_status,
  476. DP_LINK_STATUS_SIZE)) {
  477. DRM_ERROR("Failed to get link status\n");
  478. return false;
  479. }
  480. /* if link training is requested we should perform it always */
  481. return drm_dp_channel_eq_ok(link_status, min(port->lanes, sink_lanes));
  482. }
  483. static void cdn_dp_audio_handle_plugged_change(struct cdn_dp_device *dp,
  484. bool plugged)
  485. {
  486. if (dp->codec_dev)
  487. dp->plugged_cb(dp->codec_dev, plugged);
  488. }
  489. static void cdn_dp_encoder_enable(struct drm_encoder *encoder)
  490. {
  491. struct cdn_dp_device *dp = encoder_to_dp(encoder);
  492. int ret, val;
  493. ret = drm_of_encoder_active_endpoint_id(dp->dev->of_node, encoder);
  494. if (ret < 0) {
  495. DRM_DEV_ERROR(dp->dev, "Could not get vop id, %d", ret);
  496. return;
  497. }
  498. DRM_DEV_DEBUG_KMS(dp->dev, "vop %s output to cdn-dp\n",
  499. (ret) ? "LIT" : "BIG");
  500. if (ret)
  501. val = DP_SEL_VOP_LIT | (DP_SEL_VOP_LIT << 16);
  502. else
  503. val = DP_SEL_VOP_LIT << 16;
  504. ret = cdn_dp_grf_write(dp, GRF_SOC_CON9, val);
  505. if (ret)
  506. return;
  507. mutex_lock(&dp->lock);
  508. ret = cdn_dp_enable(dp);
  509. if (ret) {
  510. DRM_DEV_ERROR(dp->dev, "Failed to enable encoder %d\n",
  511. ret);
  512. goto out;
  513. }
  514. if (!cdn_dp_check_link_status(dp)) {
  515. ret = cdn_dp_train_link(dp);
  516. if (ret) {
  517. DRM_DEV_ERROR(dp->dev, "Failed link train %d\n", ret);
  518. goto out;
  519. }
  520. }
  521. ret = cdn_dp_set_video_status(dp, CONTROL_VIDEO_IDLE);
  522. if (ret) {
  523. DRM_DEV_ERROR(dp->dev, "Failed to idle video %d\n", ret);
  524. goto out;
  525. }
  526. ret = cdn_dp_config_video(dp);
  527. if (ret) {
  528. DRM_DEV_ERROR(dp->dev, "Failed to config video %d\n", ret);
  529. goto out;
  530. }
  531. ret = cdn_dp_set_video_status(dp, CONTROL_VIDEO_VALID);
  532. if (ret) {
  533. DRM_DEV_ERROR(dp->dev, "Failed to valid video %d\n", ret);
  534. goto out;
  535. }
  536. cdn_dp_audio_handle_plugged_change(dp, true);
  537. out:
  538. mutex_unlock(&dp->lock);
  539. }
  540. static void cdn_dp_encoder_disable(struct drm_encoder *encoder)
  541. {
  542. struct cdn_dp_device *dp = encoder_to_dp(encoder);
  543. int ret;
  544. mutex_lock(&dp->lock);
  545. cdn_dp_audio_handle_plugged_change(dp, false);
  546. if (dp->active) {
  547. ret = cdn_dp_disable(dp);
  548. if (ret) {
  549. DRM_DEV_ERROR(dp->dev, "Failed to disable encoder %d\n",
  550. ret);
  551. }
  552. }
  553. mutex_unlock(&dp->lock);
  554. /*
  555. * In the following 2 cases, we need to run the event_work to re-enable
  556. * the DP:
  557. * 1. If there is not just one port device is connected, and remove one
  558. * device from a port, the DP will be disabled here, at this case,
  559. * run the event_work to re-open DP for the other port.
  560. * 2. If re-training or re-config failed, the DP will be disabled here.
  561. * run the event_work to re-connect it.
  562. */
  563. if (!dp->connected && cdn_dp_connected_port(dp))
  564. schedule_work(&dp->event_work);
  565. }
  566. static int cdn_dp_encoder_atomic_check(struct drm_encoder *encoder,
  567. struct drm_crtc_state *crtc_state,
  568. struct drm_connector_state *conn_state)
  569. {
  570. struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
  571. s->output_mode = ROCKCHIP_OUT_MODE_AAAA;
  572. s->output_type = DRM_MODE_CONNECTOR_DisplayPort;
  573. return 0;
  574. }
  575. static const struct drm_encoder_helper_funcs cdn_dp_encoder_helper_funcs = {
  576. .mode_set = cdn_dp_encoder_mode_set,
  577. .enable = cdn_dp_encoder_enable,
  578. .disable = cdn_dp_encoder_disable,
  579. .atomic_check = cdn_dp_encoder_atomic_check,
  580. };
  581. static int cdn_dp_parse_dt(struct cdn_dp_device *dp)
  582. {
  583. struct device *dev = dp->dev;
  584. struct device_node *np = dev->of_node;
  585. struct platform_device *pdev = to_platform_device(dev);
  586. dp->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
  587. if (IS_ERR(dp->grf)) {
  588. DRM_DEV_ERROR(dev, "cdn-dp needs rockchip,grf property\n");
  589. return PTR_ERR(dp->grf);
  590. }
  591. dp->regs = devm_platform_ioremap_resource(pdev, 0);
  592. if (IS_ERR(dp->regs)) {
  593. DRM_DEV_ERROR(dev, "ioremap reg failed\n");
  594. return PTR_ERR(dp->regs);
  595. }
  596. dp->core_clk = devm_clk_get(dev, "core-clk");
  597. if (IS_ERR(dp->core_clk)) {
  598. DRM_DEV_ERROR(dev, "cannot get core_clk_dp\n");
  599. return PTR_ERR(dp->core_clk);
  600. }
  601. dp->pclk = devm_clk_get(dev, "pclk");
  602. if (IS_ERR(dp->pclk)) {
  603. DRM_DEV_ERROR(dev, "cannot get pclk\n");
  604. return PTR_ERR(dp->pclk);
  605. }
  606. dp->spdif_clk = devm_clk_get(dev, "spdif");
  607. if (IS_ERR(dp->spdif_clk)) {
  608. DRM_DEV_ERROR(dev, "cannot get spdif_clk\n");
  609. return PTR_ERR(dp->spdif_clk);
  610. }
  611. dp->grf_clk = devm_clk_get(dev, "grf");
  612. if (IS_ERR(dp->grf_clk)) {
  613. DRM_DEV_ERROR(dev, "cannot get grf clk\n");
  614. return PTR_ERR(dp->grf_clk);
  615. }
  616. dp->spdif_rst = devm_reset_control_get(dev, "spdif");
  617. if (IS_ERR(dp->spdif_rst)) {
  618. DRM_DEV_ERROR(dev, "no spdif reset control found\n");
  619. return PTR_ERR(dp->spdif_rst);
  620. }
  621. dp->dptx_rst = devm_reset_control_get(dev, "dptx");
  622. if (IS_ERR(dp->dptx_rst)) {
  623. DRM_DEV_ERROR(dev, "no uphy reset control found\n");
  624. return PTR_ERR(dp->dptx_rst);
  625. }
  626. dp->core_rst = devm_reset_control_get(dev, "core");
  627. if (IS_ERR(dp->core_rst)) {
  628. DRM_DEV_ERROR(dev, "no core reset control found\n");
  629. return PTR_ERR(dp->core_rst);
  630. }
  631. dp->apb_rst = devm_reset_control_get(dev, "apb");
  632. if (IS_ERR(dp->apb_rst)) {
  633. DRM_DEV_ERROR(dev, "no apb reset control found\n");
  634. return PTR_ERR(dp->apb_rst);
  635. }
  636. return 0;
  637. }
  638. static int cdn_dp_audio_hw_params(struct device *dev, void *data,
  639. struct hdmi_codec_daifmt *daifmt,
  640. struct hdmi_codec_params *params)
  641. {
  642. struct cdn_dp_device *dp = dev_get_drvdata(dev);
  643. struct audio_info audio = {
  644. .sample_width = params->sample_width,
  645. .sample_rate = params->sample_rate,
  646. .channels = params->channels,
  647. };
  648. int ret;
  649. mutex_lock(&dp->lock);
  650. if (!dp->active) {
  651. ret = -ENODEV;
  652. goto out;
  653. }
  654. switch (daifmt->fmt) {
  655. case HDMI_I2S:
  656. audio.format = AFMT_I2S;
  657. break;
  658. case HDMI_SPDIF:
  659. audio.format = AFMT_SPDIF;
  660. break;
  661. default:
  662. DRM_DEV_ERROR(dev, "Invalid format %d\n", daifmt->fmt);
  663. ret = -EINVAL;
  664. goto out;
  665. }
  666. ret = cdn_dp_audio_config(dp, &audio);
  667. if (!ret)
  668. dp->audio_info = audio;
  669. out:
  670. mutex_unlock(&dp->lock);
  671. return ret;
  672. }
  673. static void cdn_dp_audio_shutdown(struct device *dev, void *data)
  674. {
  675. struct cdn_dp_device *dp = dev_get_drvdata(dev);
  676. int ret;
  677. mutex_lock(&dp->lock);
  678. if (!dp->active)
  679. goto out;
  680. ret = cdn_dp_audio_stop(dp, &dp->audio_info);
  681. if (!ret)
  682. dp->audio_info.format = AFMT_UNUSED;
  683. out:
  684. mutex_unlock(&dp->lock);
  685. }
  686. static int cdn_dp_audio_mute_stream(struct device *dev, void *data,
  687. bool enable, int direction)
  688. {
  689. struct cdn_dp_device *dp = dev_get_drvdata(dev);
  690. int ret;
  691. mutex_lock(&dp->lock);
  692. if (!dp->active) {
  693. ret = -ENODEV;
  694. goto out;
  695. }
  696. ret = cdn_dp_audio_mute(dp, enable);
  697. out:
  698. mutex_unlock(&dp->lock);
  699. return ret;
  700. }
  701. static int cdn_dp_audio_get_eld(struct device *dev, void *data,
  702. u8 *buf, size_t len)
  703. {
  704. struct cdn_dp_device *dp = dev_get_drvdata(dev);
  705. memcpy(buf, dp->connector.eld, min(sizeof(dp->connector.eld), len));
  706. return 0;
  707. }
  708. static int cdn_dp_audio_hook_plugged_cb(struct device *dev, void *data,
  709. hdmi_codec_plugged_cb fn,
  710. struct device *codec_dev)
  711. {
  712. struct cdn_dp_device *dp = dev_get_drvdata(dev);
  713. mutex_lock(&dp->lock);
  714. dp->plugged_cb = fn;
  715. dp->codec_dev = codec_dev;
  716. cdn_dp_audio_handle_plugged_change(dp, dp->connected);
  717. mutex_unlock(&dp->lock);
  718. return 0;
  719. }
  720. static const struct hdmi_codec_ops audio_codec_ops = {
  721. .hw_params = cdn_dp_audio_hw_params,
  722. .audio_shutdown = cdn_dp_audio_shutdown,
  723. .mute_stream = cdn_dp_audio_mute_stream,
  724. .get_eld = cdn_dp_audio_get_eld,
  725. .hook_plugged_cb = cdn_dp_audio_hook_plugged_cb,
  726. .no_capture_mute = 1,
  727. };
  728. static int cdn_dp_audio_codec_init(struct cdn_dp_device *dp,
  729. struct device *dev)
  730. {
  731. struct hdmi_codec_pdata codec_data = {
  732. .i2s = 1,
  733. .spdif = 1,
  734. .ops = &audio_codec_ops,
  735. .max_i2s_channels = 8,
  736. };
  737. dp->audio_pdev = platform_device_register_data(
  738. dev, HDMI_CODEC_DRV_NAME, PLATFORM_DEVID_AUTO,
  739. &codec_data, sizeof(codec_data));
  740. return PTR_ERR_OR_ZERO(dp->audio_pdev);
  741. }
  742. static int cdn_dp_request_firmware(struct cdn_dp_device *dp)
  743. {
  744. int ret;
  745. unsigned long timeout = jiffies + msecs_to_jiffies(CDN_FW_TIMEOUT_MS);
  746. unsigned long sleep = 1000;
  747. WARN_ON(!mutex_is_locked(&dp->lock));
  748. if (dp->fw_loaded)
  749. return 0;
  750. /* Drop the lock before getting the firmware to avoid blocking boot */
  751. mutex_unlock(&dp->lock);
  752. while (time_before(jiffies, timeout)) {
  753. ret = request_firmware(&dp->fw, CDN_DP_FIRMWARE, dp->dev);
  754. if (ret == -ENOENT) {
  755. msleep(sleep);
  756. sleep *= 2;
  757. continue;
  758. } else if (ret) {
  759. DRM_DEV_ERROR(dp->dev,
  760. "failed to request firmware: %d\n", ret);
  761. goto out;
  762. }
  763. dp->fw_loaded = true;
  764. ret = 0;
  765. goto out;
  766. }
  767. DRM_DEV_ERROR(dp->dev, "Timed out trying to load firmware\n");
  768. ret = -ETIMEDOUT;
  769. out:
  770. mutex_lock(&dp->lock);
  771. return ret;
  772. }
  773. static void cdn_dp_pd_event_work(struct work_struct *work)
  774. {
  775. struct cdn_dp_device *dp = container_of(work, struct cdn_dp_device,
  776. event_work);
  777. int ret;
  778. mutex_lock(&dp->lock);
  779. if (dp->suspended)
  780. goto out;
  781. ret = cdn_dp_request_firmware(dp);
  782. if (ret)
  783. goto out;
  784. dp->connected = true;
  785. /* Not connected, notify userspace to disable the block */
  786. if (!cdn_dp_connected_port(dp)) {
  787. DRM_DEV_INFO(dp->dev, "Not connected; disabling cdn\n");
  788. dp->connected = false;
  789. /* Connected but not enabled, enable the block */
  790. } else if (!dp->active) {
  791. DRM_DEV_INFO(dp->dev, "Connected, not enabled; enabling cdn\n");
  792. ret = cdn_dp_enable(dp);
  793. if (ret) {
  794. DRM_DEV_ERROR(dp->dev, "Enabling dp failed: %d\n", ret);
  795. dp->connected = false;
  796. }
  797. /* Enabled and connected to a dongle without a sink, notify userspace */
  798. } else if (!cdn_dp_check_sink_connection(dp)) {
  799. DRM_DEV_INFO(dp->dev, "Connected without sink; assert hpd\n");
  800. dp->connected = false;
  801. /* Enabled and connected with a sink, re-train if requested */
  802. } else if (!cdn_dp_check_link_status(dp)) {
  803. unsigned int rate = dp->max_rate;
  804. unsigned int lanes = dp->max_lanes;
  805. struct drm_display_mode *mode = &dp->mode;
  806. DRM_DEV_INFO(dp->dev, "Connected with sink; re-train link\n");
  807. ret = cdn_dp_train_link(dp);
  808. if (ret) {
  809. dp->connected = false;
  810. DRM_DEV_ERROR(dp->dev, "Training link failed: %d\n", ret);
  811. goto out;
  812. }
  813. /* If training result is changed, update the video config */
  814. if (mode->clock &&
  815. (rate != dp->max_rate || lanes != dp->max_lanes)) {
  816. ret = cdn_dp_config_video(dp);
  817. if (ret) {
  818. dp->connected = false;
  819. DRM_DEV_ERROR(dp->dev, "Failed to configure video: %d\n", ret);
  820. }
  821. }
  822. }
  823. out:
  824. mutex_unlock(&dp->lock);
  825. drm_connector_helper_hpd_irq_event(&dp->connector);
  826. }
  827. static int cdn_dp_pd_event(struct notifier_block *nb,
  828. unsigned long event, void *priv)
  829. {
  830. struct cdn_dp_port *port = container_of(nb, struct cdn_dp_port,
  831. event_nb);
  832. struct cdn_dp_device *dp = port->dp;
  833. /*
  834. * It would be nice to be able to just do the work inline right here.
  835. * However, we need to make a bunch of calls that might sleep in order
  836. * to turn on the block/phy, so use a worker instead.
  837. */
  838. schedule_work(&dp->event_work);
  839. return NOTIFY_DONE;
  840. }
  841. static int cdn_dp_bind(struct device *dev, struct device *master, void *data)
  842. {
  843. struct cdn_dp_device *dp = dev_get_drvdata(dev);
  844. struct drm_encoder *encoder;
  845. struct drm_connector *connector;
  846. struct cdn_dp_port *port;
  847. struct drm_device *drm_dev = data;
  848. int ret, i;
  849. ret = cdn_dp_parse_dt(dp);
  850. if (ret < 0)
  851. return ret;
  852. dp->drm_dev = drm_dev;
  853. dp->connected = false;
  854. dp->active = false;
  855. dp->active_port = -1;
  856. dp->fw_loaded = false;
  857. INIT_WORK(&dp->event_work, cdn_dp_pd_event_work);
  858. encoder = &dp->encoder.encoder;
  859. encoder->possible_crtcs = drm_of_find_possible_crtcs(drm_dev,
  860. dev->of_node);
  861. DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs);
  862. ret = drm_simple_encoder_init(drm_dev, encoder,
  863. DRM_MODE_ENCODER_TMDS);
  864. if (ret) {
  865. DRM_ERROR("failed to initialize encoder with drm\n");
  866. return ret;
  867. }
  868. drm_encoder_helper_add(encoder, &cdn_dp_encoder_helper_funcs);
  869. connector = &dp->connector;
  870. connector->polled = DRM_CONNECTOR_POLL_HPD;
  871. connector->dpms = DRM_MODE_DPMS_OFF;
  872. ret = drm_connector_init(drm_dev, connector,
  873. &cdn_dp_atomic_connector_funcs,
  874. DRM_MODE_CONNECTOR_DisplayPort);
  875. if (ret) {
  876. DRM_ERROR("failed to initialize connector with drm\n");
  877. goto err_free_encoder;
  878. }
  879. drm_connector_helper_add(connector, &cdn_dp_connector_helper_funcs);
  880. ret = drm_connector_attach_encoder(connector, encoder);
  881. if (ret) {
  882. DRM_ERROR("failed to attach connector and encoder\n");
  883. goto err_free_connector;
  884. }
  885. for (i = 0; i < dp->ports; i++) {
  886. port = dp->port[i];
  887. port->event_nb.notifier_call = cdn_dp_pd_event;
  888. ret = devm_extcon_register_notifier(dp->dev, port->extcon,
  889. EXTCON_DISP_DP,
  890. &port->event_nb);
  891. if (ret) {
  892. DRM_DEV_ERROR(dev,
  893. "register EXTCON_DISP_DP notifier err\n");
  894. goto err_free_connector;
  895. }
  896. }
  897. pm_runtime_enable(dev);
  898. schedule_work(&dp->event_work);
  899. return 0;
  900. err_free_connector:
  901. drm_connector_cleanup(connector);
  902. err_free_encoder:
  903. drm_encoder_cleanup(encoder);
  904. return ret;
  905. }
  906. static void cdn_dp_unbind(struct device *dev, struct device *master, void *data)
  907. {
  908. struct cdn_dp_device *dp = dev_get_drvdata(dev);
  909. struct drm_encoder *encoder = &dp->encoder.encoder;
  910. struct drm_connector *connector = &dp->connector;
  911. cancel_work_sync(&dp->event_work);
  912. cdn_dp_encoder_disable(encoder);
  913. encoder->funcs->destroy(encoder);
  914. connector->funcs->destroy(connector);
  915. pm_runtime_disable(dev);
  916. if (dp->fw_loaded)
  917. release_firmware(dp->fw);
  918. drm_edid_free(dp->drm_edid);
  919. dp->drm_edid = NULL;
  920. }
  921. static const struct component_ops cdn_dp_component_ops = {
  922. .bind = cdn_dp_bind,
  923. .unbind = cdn_dp_unbind,
  924. };
  925. static int cdn_dp_suspend(struct device *dev)
  926. {
  927. struct cdn_dp_device *dp = dev_get_drvdata(dev);
  928. int ret = 0;
  929. mutex_lock(&dp->lock);
  930. if (dp->active)
  931. ret = cdn_dp_disable(dp);
  932. dp->suspended = true;
  933. mutex_unlock(&dp->lock);
  934. return ret;
  935. }
  936. static __maybe_unused int cdn_dp_resume(struct device *dev)
  937. {
  938. struct cdn_dp_device *dp = dev_get_drvdata(dev);
  939. mutex_lock(&dp->lock);
  940. dp->suspended = false;
  941. if (dp->fw_loaded)
  942. schedule_work(&dp->event_work);
  943. mutex_unlock(&dp->lock);
  944. return 0;
  945. }
  946. static int cdn_dp_probe(struct platform_device *pdev)
  947. {
  948. struct device *dev = &pdev->dev;
  949. const struct of_device_id *match;
  950. struct cdn_dp_data *dp_data;
  951. struct cdn_dp_port *port;
  952. struct cdn_dp_device *dp;
  953. struct extcon_dev *extcon;
  954. struct phy *phy;
  955. int ret;
  956. int i;
  957. dp = devm_kzalloc(dev, sizeof(*dp), GFP_KERNEL);
  958. if (!dp)
  959. return -ENOMEM;
  960. dp->dev = dev;
  961. match = of_match_node(cdn_dp_dt_ids, pdev->dev.of_node);
  962. dp_data = (struct cdn_dp_data *)match->data;
  963. for (i = 0; i < dp_data->max_phy; i++) {
  964. extcon = extcon_get_edev_by_phandle(dev, i);
  965. phy = devm_of_phy_get_by_index(dev, dev->of_node, i);
  966. if (PTR_ERR(extcon) == -EPROBE_DEFER ||
  967. PTR_ERR(phy) == -EPROBE_DEFER)
  968. return -EPROBE_DEFER;
  969. if (IS_ERR(extcon) || IS_ERR(phy))
  970. continue;
  971. port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
  972. if (!port)
  973. return -ENOMEM;
  974. port->extcon = extcon;
  975. port->phy = phy;
  976. port->dp = dp;
  977. port->id = i;
  978. dp->port[dp->ports++] = port;
  979. }
  980. if (!dp->ports) {
  981. DRM_DEV_ERROR(dev, "missing extcon or phy\n");
  982. return -EINVAL;
  983. }
  984. mutex_init(&dp->lock);
  985. dev_set_drvdata(dev, dp);
  986. ret = cdn_dp_audio_codec_init(dp, dev);
  987. if (ret)
  988. return ret;
  989. ret = component_add(dev, &cdn_dp_component_ops);
  990. if (ret)
  991. goto err_audio_deinit;
  992. return 0;
  993. err_audio_deinit:
  994. platform_device_unregister(dp->audio_pdev);
  995. return ret;
  996. }
  997. static void cdn_dp_remove(struct platform_device *pdev)
  998. {
  999. struct cdn_dp_device *dp = platform_get_drvdata(pdev);
  1000. platform_device_unregister(dp->audio_pdev);
  1001. cdn_dp_suspend(dp->dev);
  1002. component_del(&pdev->dev, &cdn_dp_component_ops);
  1003. }
  1004. static void cdn_dp_shutdown(struct platform_device *pdev)
  1005. {
  1006. struct cdn_dp_device *dp = platform_get_drvdata(pdev);
  1007. cdn_dp_suspend(dp->dev);
  1008. }
  1009. static const struct dev_pm_ops cdn_dp_pm_ops = {
  1010. SET_SYSTEM_SLEEP_PM_OPS(cdn_dp_suspend,
  1011. cdn_dp_resume)
  1012. };
  1013. struct platform_driver cdn_dp_driver = {
  1014. .probe = cdn_dp_probe,
  1015. .remove_new = cdn_dp_remove,
  1016. .shutdown = cdn_dp_shutdown,
  1017. .driver = {
  1018. .name = "cdn-dp",
  1019. .of_match_table = cdn_dp_dt_ids,
  1020. .pm = &cdn_dp_pm_ops,
  1021. },
  1022. };