inno_hdmi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
  4. * Zheng Yang <[email protected]>
  5. * Yakir Yang <[email protected]>
  6. */
  7. #include <linux/irq.h>
  8. #include <linux/clk.h>
  9. #include <linux/delay.h>
  10. #include <linux/err.h>
  11. #include <linux/hdmi.h>
  12. #include <linux/mfd/syscon.h>
  13. #include <linux/module.h>
  14. #include <linux/mutex.h>
  15. #include <linux/of_device.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 "rockchip_drm_drv.h"
  22. #include "rockchip_drm_vop.h"
  23. #include "inno_hdmi.h"
  24. struct hdmi_data_info {
  25. int vic;
  26. bool sink_has_audio;
  27. unsigned int enc_in_format;
  28. unsigned int enc_out_format;
  29. unsigned int colorimetry;
  30. };
  31. struct inno_hdmi_i2c {
  32. struct i2c_adapter adap;
  33. u8 ddc_addr;
  34. u8 segment_addr;
  35. struct mutex lock;
  36. struct completion cmp;
  37. };
  38. struct inno_hdmi {
  39. struct device *dev;
  40. struct drm_device *drm_dev;
  41. int irq;
  42. struct clk *pclk;
  43. void __iomem *regs;
  44. struct drm_connector connector;
  45. struct rockchip_encoder encoder;
  46. struct inno_hdmi_i2c *i2c;
  47. struct i2c_adapter *ddc;
  48. unsigned int tmds_rate;
  49. struct hdmi_data_info hdmi_data;
  50. struct drm_display_mode previous_mode;
  51. };
  52. static struct inno_hdmi *encoder_to_inno_hdmi(struct drm_encoder *encoder)
  53. {
  54. struct rockchip_encoder *rkencoder = to_rockchip_encoder(encoder);
  55. return container_of(rkencoder, struct inno_hdmi, encoder);
  56. }
  57. static struct inno_hdmi *connector_to_inno_hdmi(struct drm_connector *connector)
  58. {
  59. return container_of(connector, struct inno_hdmi, connector);
  60. }
  61. enum {
  62. CSC_ITU601_16_235_TO_RGB_0_255_8BIT,
  63. CSC_ITU601_0_255_TO_RGB_0_255_8BIT,
  64. CSC_ITU709_16_235_TO_RGB_0_255_8BIT,
  65. CSC_RGB_0_255_TO_ITU601_16_235_8BIT,
  66. CSC_RGB_0_255_TO_ITU709_16_235_8BIT,
  67. CSC_RGB_0_255_TO_RGB_16_235_8BIT,
  68. };
  69. static const char coeff_csc[][24] = {
  70. /*
  71. * YUV2RGB:601 SD mode(Y[16:235], UV[16:240], RGB[0:255]):
  72. * R = 1.164*Y + 1.596*V - 204
  73. * G = 1.164*Y - 0.391*U - 0.813*V + 154
  74. * B = 1.164*Y + 2.018*U - 258
  75. */
  76. {
  77. 0x04, 0xa7, 0x00, 0x00, 0x06, 0x62, 0x02, 0xcc,
  78. 0x04, 0xa7, 0x11, 0x90, 0x13, 0x40, 0x00, 0x9a,
  79. 0x04, 0xa7, 0x08, 0x12, 0x00, 0x00, 0x03, 0x02
  80. },
  81. /*
  82. * YUV2RGB:601 SD mode(YUV[0:255],RGB[0:255]):
  83. * R = Y + 1.402*V - 248
  84. * G = Y - 0.344*U - 0.714*V + 135
  85. * B = Y + 1.772*U - 227
  86. */
  87. {
  88. 0x04, 0x00, 0x00, 0x00, 0x05, 0x9b, 0x02, 0xf8,
  89. 0x04, 0x00, 0x11, 0x60, 0x12, 0xdb, 0x00, 0x87,
  90. 0x04, 0x00, 0x07, 0x16, 0x00, 0x00, 0x02, 0xe3
  91. },
  92. /*
  93. * YUV2RGB:709 HD mode(Y[16:235],UV[16:240],RGB[0:255]):
  94. * R = 1.164*Y + 1.793*V - 248
  95. * G = 1.164*Y - 0.213*U - 0.534*V + 77
  96. * B = 1.164*Y + 2.115*U - 289
  97. */
  98. {
  99. 0x04, 0xa7, 0x00, 0x00, 0x07, 0x2c, 0x02, 0xf8,
  100. 0x04, 0xa7, 0x10, 0xda, 0x12, 0x22, 0x00, 0x4d,
  101. 0x04, 0xa7, 0x08, 0x74, 0x00, 0x00, 0x03, 0x21
  102. },
  103. /*
  104. * RGB2YUV:601 SD mode:
  105. * Cb = -0.291G - 0.148R + 0.439B + 128
  106. * Y = 0.504G + 0.257R + 0.098B + 16
  107. * Cr = -0.368G + 0.439R - 0.071B + 128
  108. */
  109. {
  110. 0x11, 0x5f, 0x01, 0x82, 0x10, 0x23, 0x00, 0x80,
  111. 0x02, 0x1c, 0x00, 0xa1, 0x00, 0x36, 0x00, 0x1e,
  112. 0x11, 0x29, 0x10, 0x59, 0x01, 0x82, 0x00, 0x80
  113. },
  114. /*
  115. * RGB2YUV:709 HD mode:
  116. * Cb = - 0.338G - 0.101R + 0.439B + 128
  117. * Y = 0.614G + 0.183R + 0.062B + 16
  118. * Cr = - 0.399G + 0.439R - 0.040B + 128
  119. */
  120. {
  121. 0x11, 0x98, 0x01, 0xc1, 0x10, 0x28, 0x00, 0x80,
  122. 0x02, 0x74, 0x00, 0xbb, 0x00, 0x3f, 0x00, 0x10,
  123. 0x11, 0x5a, 0x10, 0x67, 0x01, 0xc1, 0x00, 0x80
  124. },
  125. /*
  126. * RGB[0:255]2RGB[16:235]:
  127. * R' = R x (235-16)/255 + 16;
  128. * G' = G x (235-16)/255 + 16;
  129. * B' = B x (235-16)/255 + 16;
  130. */
  131. {
  132. 0x00, 0x00, 0x03, 0x6F, 0x00, 0x00, 0x00, 0x10,
  133. 0x03, 0x6F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
  134. 0x00, 0x00, 0x00, 0x00, 0x03, 0x6F, 0x00, 0x10
  135. },
  136. };
  137. static inline u8 hdmi_readb(struct inno_hdmi *hdmi, u16 offset)
  138. {
  139. return readl_relaxed(hdmi->regs + (offset) * 0x04);
  140. }
  141. static inline void hdmi_writeb(struct inno_hdmi *hdmi, u16 offset, u32 val)
  142. {
  143. writel_relaxed(val, hdmi->regs + (offset) * 0x04);
  144. }
  145. static inline void hdmi_modb(struct inno_hdmi *hdmi, u16 offset,
  146. u32 msk, u32 val)
  147. {
  148. u8 temp = hdmi_readb(hdmi, offset) & ~msk;
  149. temp |= val & msk;
  150. hdmi_writeb(hdmi, offset, temp);
  151. }
  152. static void inno_hdmi_i2c_init(struct inno_hdmi *hdmi)
  153. {
  154. int ddc_bus_freq;
  155. ddc_bus_freq = (hdmi->tmds_rate >> 2) / HDMI_SCL_RATE;
  156. hdmi_writeb(hdmi, DDC_BUS_FREQ_L, ddc_bus_freq & 0xFF);
  157. hdmi_writeb(hdmi, DDC_BUS_FREQ_H, (ddc_bus_freq >> 8) & 0xFF);
  158. /* Clear the EDID interrupt flag and mute the interrupt */
  159. hdmi_writeb(hdmi, HDMI_INTERRUPT_MASK1, 0);
  160. hdmi_writeb(hdmi, HDMI_INTERRUPT_STATUS1, m_INT_EDID_READY);
  161. }
  162. static void inno_hdmi_sys_power(struct inno_hdmi *hdmi, bool enable)
  163. {
  164. if (enable)
  165. hdmi_modb(hdmi, HDMI_SYS_CTRL, m_POWER, v_PWR_ON);
  166. else
  167. hdmi_modb(hdmi, HDMI_SYS_CTRL, m_POWER, v_PWR_OFF);
  168. }
  169. static void inno_hdmi_set_pwr_mode(struct inno_hdmi *hdmi, int mode)
  170. {
  171. switch (mode) {
  172. case NORMAL:
  173. inno_hdmi_sys_power(hdmi, false);
  174. hdmi_writeb(hdmi, HDMI_PHY_PRE_EMPHASIS, 0x6f);
  175. hdmi_writeb(hdmi, HDMI_PHY_DRIVER, 0xbb);
  176. hdmi_writeb(hdmi, HDMI_PHY_SYS_CTL, 0x15);
  177. hdmi_writeb(hdmi, HDMI_PHY_SYS_CTL, 0x14);
  178. hdmi_writeb(hdmi, HDMI_PHY_SYS_CTL, 0x10);
  179. hdmi_writeb(hdmi, HDMI_PHY_CHG_PWR, 0x0f);
  180. hdmi_writeb(hdmi, HDMI_PHY_SYNC, 0x00);
  181. hdmi_writeb(hdmi, HDMI_PHY_SYNC, 0x01);
  182. inno_hdmi_sys_power(hdmi, true);
  183. break;
  184. case LOWER_PWR:
  185. inno_hdmi_sys_power(hdmi, false);
  186. hdmi_writeb(hdmi, HDMI_PHY_DRIVER, 0x00);
  187. hdmi_writeb(hdmi, HDMI_PHY_PRE_EMPHASIS, 0x00);
  188. hdmi_writeb(hdmi, HDMI_PHY_CHG_PWR, 0x00);
  189. hdmi_writeb(hdmi, HDMI_PHY_SYS_CTL, 0x15);
  190. break;
  191. default:
  192. DRM_DEV_ERROR(hdmi->dev, "Unknown power mode %d\n", mode);
  193. }
  194. }
  195. static void inno_hdmi_reset(struct inno_hdmi *hdmi)
  196. {
  197. u32 val;
  198. u32 msk;
  199. hdmi_modb(hdmi, HDMI_SYS_CTRL, m_RST_DIGITAL, v_NOT_RST_DIGITAL);
  200. udelay(100);
  201. hdmi_modb(hdmi, HDMI_SYS_CTRL, m_RST_ANALOG, v_NOT_RST_ANALOG);
  202. udelay(100);
  203. msk = m_REG_CLK_INV | m_REG_CLK_SOURCE | m_POWER | m_INT_POL;
  204. val = v_REG_CLK_INV | v_REG_CLK_SOURCE_SYS | v_PWR_ON | v_INT_POL_HIGH;
  205. hdmi_modb(hdmi, HDMI_SYS_CTRL, msk, val);
  206. inno_hdmi_set_pwr_mode(hdmi, NORMAL);
  207. }
  208. static int inno_hdmi_upload_frame(struct inno_hdmi *hdmi, int setup_rc,
  209. union hdmi_infoframe *frame, u32 frame_index,
  210. u32 mask, u32 disable, u32 enable)
  211. {
  212. if (mask)
  213. hdmi_modb(hdmi, HDMI_PACKET_SEND_AUTO, mask, disable);
  214. hdmi_writeb(hdmi, HDMI_CONTROL_PACKET_BUF_INDEX, frame_index);
  215. if (setup_rc >= 0) {
  216. u8 packed_frame[HDMI_MAXIMUM_INFO_FRAME_SIZE];
  217. ssize_t rc, i;
  218. rc = hdmi_infoframe_pack(frame, packed_frame,
  219. sizeof(packed_frame));
  220. if (rc < 0)
  221. return rc;
  222. for (i = 0; i < rc; i++)
  223. hdmi_writeb(hdmi, HDMI_CONTROL_PACKET_ADDR + i,
  224. packed_frame[i]);
  225. if (mask)
  226. hdmi_modb(hdmi, HDMI_PACKET_SEND_AUTO, mask, enable);
  227. }
  228. return setup_rc;
  229. }
  230. static int inno_hdmi_config_video_vsi(struct inno_hdmi *hdmi,
  231. struct drm_display_mode *mode)
  232. {
  233. union hdmi_infoframe frame;
  234. int rc;
  235. rc = drm_hdmi_vendor_infoframe_from_display_mode(&frame.vendor.hdmi,
  236. &hdmi->connector,
  237. mode);
  238. return inno_hdmi_upload_frame(hdmi, rc, &frame, INFOFRAME_VSI,
  239. m_PACKET_VSI_EN, v_PACKET_VSI_EN(0), v_PACKET_VSI_EN(1));
  240. }
  241. static int inno_hdmi_config_video_avi(struct inno_hdmi *hdmi,
  242. struct drm_display_mode *mode)
  243. {
  244. union hdmi_infoframe frame;
  245. int rc;
  246. rc = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi,
  247. &hdmi->connector,
  248. mode);
  249. if (hdmi->hdmi_data.enc_out_format == HDMI_COLORSPACE_YUV444)
  250. frame.avi.colorspace = HDMI_COLORSPACE_YUV444;
  251. else if (hdmi->hdmi_data.enc_out_format == HDMI_COLORSPACE_YUV422)
  252. frame.avi.colorspace = HDMI_COLORSPACE_YUV422;
  253. else
  254. frame.avi.colorspace = HDMI_COLORSPACE_RGB;
  255. return inno_hdmi_upload_frame(hdmi, rc, &frame, INFOFRAME_AVI, 0, 0, 0);
  256. }
  257. static int inno_hdmi_config_video_csc(struct inno_hdmi *hdmi)
  258. {
  259. struct hdmi_data_info *data = &hdmi->hdmi_data;
  260. int c0_c2_change = 0;
  261. int csc_enable = 0;
  262. int csc_mode = 0;
  263. int auto_csc = 0;
  264. int value;
  265. int i;
  266. /* Input video mode is SDR RGB24bit, data enable signal from external */
  267. hdmi_writeb(hdmi, HDMI_VIDEO_CONTRL1, v_DE_EXTERNAL |
  268. v_VIDEO_INPUT_FORMAT(VIDEO_INPUT_SDR_RGB444));
  269. /* Input color hardcode to RGB, and output color hardcode to RGB888 */
  270. value = v_VIDEO_INPUT_BITS(VIDEO_INPUT_8BITS) |
  271. v_VIDEO_OUTPUT_COLOR(0) |
  272. v_VIDEO_INPUT_CSP(0);
  273. hdmi_writeb(hdmi, HDMI_VIDEO_CONTRL2, value);
  274. if (data->enc_in_format == data->enc_out_format) {
  275. if ((data->enc_in_format == HDMI_COLORSPACE_RGB) ||
  276. (data->enc_in_format >= HDMI_COLORSPACE_YUV444)) {
  277. value = v_SOF_DISABLE | v_COLOR_DEPTH_NOT_INDICATED(1);
  278. hdmi_writeb(hdmi, HDMI_VIDEO_CONTRL3, value);
  279. hdmi_modb(hdmi, HDMI_VIDEO_CONTRL,
  280. m_VIDEO_AUTO_CSC | m_VIDEO_C0_C2_SWAP,
  281. v_VIDEO_AUTO_CSC(AUTO_CSC_DISABLE) |
  282. v_VIDEO_C0_C2_SWAP(C0_C2_CHANGE_DISABLE));
  283. return 0;
  284. }
  285. }
  286. if (data->colorimetry == HDMI_COLORIMETRY_ITU_601) {
  287. if ((data->enc_in_format == HDMI_COLORSPACE_RGB) &&
  288. (data->enc_out_format == HDMI_COLORSPACE_YUV444)) {
  289. csc_mode = CSC_RGB_0_255_TO_ITU601_16_235_8BIT;
  290. auto_csc = AUTO_CSC_DISABLE;
  291. c0_c2_change = C0_C2_CHANGE_DISABLE;
  292. csc_enable = v_CSC_ENABLE;
  293. } else if ((data->enc_in_format == HDMI_COLORSPACE_YUV444) &&
  294. (data->enc_out_format == HDMI_COLORSPACE_RGB)) {
  295. csc_mode = CSC_ITU601_16_235_TO_RGB_0_255_8BIT;
  296. auto_csc = AUTO_CSC_ENABLE;
  297. c0_c2_change = C0_C2_CHANGE_DISABLE;
  298. csc_enable = v_CSC_DISABLE;
  299. }
  300. } else {
  301. if ((data->enc_in_format == HDMI_COLORSPACE_RGB) &&
  302. (data->enc_out_format == HDMI_COLORSPACE_YUV444)) {
  303. csc_mode = CSC_RGB_0_255_TO_ITU709_16_235_8BIT;
  304. auto_csc = AUTO_CSC_DISABLE;
  305. c0_c2_change = C0_C2_CHANGE_DISABLE;
  306. csc_enable = v_CSC_ENABLE;
  307. } else if ((data->enc_in_format == HDMI_COLORSPACE_YUV444) &&
  308. (data->enc_out_format == HDMI_COLORSPACE_RGB)) {
  309. csc_mode = CSC_ITU709_16_235_TO_RGB_0_255_8BIT;
  310. auto_csc = AUTO_CSC_ENABLE;
  311. c0_c2_change = C0_C2_CHANGE_DISABLE;
  312. csc_enable = v_CSC_DISABLE;
  313. }
  314. }
  315. for (i = 0; i < 24; i++)
  316. hdmi_writeb(hdmi, HDMI_VIDEO_CSC_COEF + i,
  317. coeff_csc[csc_mode][i]);
  318. value = v_SOF_DISABLE | csc_enable | v_COLOR_DEPTH_NOT_INDICATED(1);
  319. hdmi_writeb(hdmi, HDMI_VIDEO_CONTRL3, value);
  320. hdmi_modb(hdmi, HDMI_VIDEO_CONTRL, m_VIDEO_AUTO_CSC |
  321. m_VIDEO_C0_C2_SWAP, v_VIDEO_AUTO_CSC(auto_csc) |
  322. v_VIDEO_C0_C2_SWAP(c0_c2_change));
  323. return 0;
  324. }
  325. static int inno_hdmi_config_video_timing(struct inno_hdmi *hdmi,
  326. struct drm_display_mode *mode)
  327. {
  328. int value;
  329. /* Set detail external video timing polarity and interlace mode */
  330. value = v_EXTERANL_VIDEO(1);
  331. value |= mode->flags & DRM_MODE_FLAG_PHSYNC ?
  332. v_HSYNC_POLARITY(1) : v_HSYNC_POLARITY(0);
  333. value |= mode->flags & DRM_MODE_FLAG_PVSYNC ?
  334. v_VSYNC_POLARITY(1) : v_VSYNC_POLARITY(0);
  335. value |= mode->flags & DRM_MODE_FLAG_INTERLACE ?
  336. v_INETLACE(1) : v_INETLACE(0);
  337. hdmi_writeb(hdmi, HDMI_VIDEO_TIMING_CTL, value);
  338. /* Set detail external video timing */
  339. value = mode->htotal;
  340. hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HTOTAL_L, value & 0xFF);
  341. hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HTOTAL_H, (value >> 8) & 0xFF);
  342. value = mode->htotal - mode->hdisplay;
  343. hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HBLANK_L, value & 0xFF);
  344. hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HBLANK_H, (value >> 8) & 0xFF);
  345. value = mode->hsync_start - mode->hdisplay;
  346. hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HDELAY_L, value & 0xFF);
  347. hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HDELAY_H, (value >> 8) & 0xFF);
  348. value = mode->hsync_end - mode->hsync_start;
  349. hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HDURATION_L, value & 0xFF);
  350. hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HDURATION_H, (value >> 8) & 0xFF);
  351. value = mode->vtotal;
  352. hdmi_writeb(hdmi, HDMI_VIDEO_EXT_VTOTAL_L, value & 0xFF);
  353. hdmi_writeb(hdmi, HDMI_VIDEO_EXT_VTOTAL_H, (value >> 8) & 0xFF);
  354. value = mode->vtotal - mode->vdisplay;
  355. hdmi_writeb(hdmi, HDMI_VIDEO_EXT_VBLANK, value & 0xFF);
  356. value = mode->vsync_start - mode->vdisplay;
  357. hdmi_writeb(hdmi, HDMI_VIDEO_EXT_VDELAY, value & 0xFF);
  358. value = mode->vsync_end - mode->vsync_start;
  359. hdmi_writeb(hdmi, HDMI_VIDEO_EXT_VDURATION, value & 0xFF);
  360. hdmi_writeb(hdmi, HDMI_PHY_PRE_DIV_RATIO, 0x1e);
  361. hdmi_writeb(hdmi, HDMI_PHY_FEEDBACK_DIV_RATIO_LOW, 0x2c);
  362. hdmi_writeb(hdmi, HDMI_PHY_FEEDBACK_DIV_RATIO_HIGH, 0x01);
  363. return 0;
  364. }
  365. static int inno_hdmi_setup(struct inno_hdmi *hdmi,
  366. struct drm_display_mode *mode)
  367. {
  368. struct drm_display_info *display = &hdmi->connector.display_info;
  369. hdmi->hdmi_data.vic = drm_match_cea_mode(mode);
  370. hdmi->hdmi_data.enc_in_format = HDMI_COLORSPACE_RGB;
  371. hdmi->hdmi_data.enc_out_format = HDMI_COLORSPACE_RGB;
  372. if ((hdmi->hdmi_data.vic == 6) || (hdmi->hdmi_data.vic == 7) ||
  373. (hdmi->hdmi_data.vic == 21) || (hdmi->hdmi_data.vic == 22) ||
  374. (hdmi->hdmi_data.vic == 2) || (hdmi->hdmi_data.vic == 3) ||
  375. (hdmi->hdmi_data.vic == 17) || (hdmi->hdmi_data.vic == 18))
  376. hdmi->hdmi_data.colorimetry = HDMI_COLORIMETRY_ITU_601;
  377. else
  378. hdmi->hdmi_data.colorimetry = HDMI_COLORIMETRY_ITU_709;
  379. /* Mute video and audio output */
  380. hdmi_modb(hdmi, HDMI_AV_MUTE, m_AUDIO_MUTE | m_VIDEO_BLACK,
  381. v_AUDIO_MUTE(1) | v_VIDEO_MUTE(1));
  382. /* Set HDMI Mode */
  383. hdmi_writeb(hdmi, HDMI_HDCP_CTRL,
  384. v_HDMI_DVI(display->is_hdmi));
  385. inno_hdmi_config_video_timing(hdmi, mode);
  386. inno_hdmi_config_video_csc(hdmi);
  387. if (display->is_hdmi) {
  388. inno_hdmi_config_video_avi(hdmi, mode);
  389. inno_hdmi_config_video_vsi(hdmi, mode);
  390. }
  391. /*
  392. * When IP controller have configured to an accurate video
  393. * timing, then the TMDS clock source would be switched to
  394. * DCLK_LCDC, so we need to init the TMDS rate to mode pixel
  395. * clock rate, and reconfigure the DDC clock.
  396. */
  397. hdmi->tmds_rate = mode->clock * 1000;
  398. inno_hdmi_i2c_init(hdmi);
  399. /* Unmute video and audio output */
  400. hdmi_modb(hdmi, HDMI_AV_MUTE, m_AUDIO_MUTE | m_VIDEO_BLACK,
  401. v_AUDIO_MUTE(0) | v_VIDEO_MUTE(0));
  402. return 0;
  403. }
  404. static void inno_hdmi_encoder_mode_set(struct drm_encoder *encoder,
  405. struct drm_display_mode *mode,
  406. struct drm_display_mode *adj_mode)
  407. {
  408. struct inno_hdmi *hdmi = encoder_to_inno_hdmi(encoder);
  409. inno_hdmi_setup(hdmi, adj_mode);
  410. /* Store the display mode for plugin/DPMS poweron events */
  411. drm_mode_copy(&hdmi->previous_mode, adj_mode);
  412. }
  413. static void inno_hdmi_encoder_enable(struct drm_encoder *encoder)
  414. {
  415. struct inno_hdmi *hdmi = encoder_to_inno_hdmi(encoder);
  416. inno_hdmi_set_pwr_mode(hdmi, NORMAL);
  417. }
  418. static void inno_hdmi_encoder_disable(struct drm_encoder *encoder)
  419. {
  420. struct inno_hdmi *hdmi = encoder_to_inno_hdmi(encoder);
  421. inno_hdmi_set_pwr_mode(hdmi, LOWER_PWR);
  422. }
  423. static bool inno_hdmi_encoder_mode_fixup(struct drm_encoder *encoder,
  424. const struct drm_display_mode *mode,
  425. struct drm_display_mode *adj_mode)
  426. {
  427. return true;
  428. }
  429. static int
  430. inno_hdmi_encoder_atomic_check(struct drm_encoder *encoder,
  431. struct drm_crtc_state *crtc_state,
  432. struct drm_connector_state *conn_state)
  433. {
  434. struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
  435. s->output_mode = ROCKCHIP_OUT_MODE_P888;
  436. s->output_type = DRM_MODE_CONNECTOR_HDMIA;
  437. return 0;
  438. }
  439. static struct drm_encoder_helper_funcs inno_hdmi_encoder_helper_funcs = {
  440. .enable = inno_hdmi_encoder_enable,
  441. .disable = inno_hdmi_encoder_disable,
  442. .mode_fixup = inno_hdmi_encoder_mode_fixup,
  443. .mode_set = inno_hdmi_encoder_mode_set,
  444. .atomic_check = inno_hdmi_encoder_atomic_check,
  445. };
  446. static enum drm_connector_status
  447. inno_hdmi_connector_detect(struct drm_connector *connector, bool force)
  448. {
  449. struct inno_hdmi *hdmi = connector_to_inno_hdmi(connector);
  450. return (hdmi_readb(hdmi, HDMI_STATUS) & m_HOTPLUG) ?
  451. connector_status_connected : connector_status_disconnected;
  452. }
  453. static int inno_hdmi_connector_get_modes(struct drm_connector *connector)
  454. {
  455. struct inno_hdmi *hdmi = connector_to_inno_hdmi(connector);
  456. struct edid *edid;
  457. int ret = 0;
  458. if (!hdmi->ddc)
  459. return 0;
  460. edid = drm_get_edid(connector, hdmi->ddc);
  461. if (edid) {
  462. hdmi->hdmi_data.sink_has_audio = drm_detect_monitor_audio(edid);
  463. drm_connector_update_edid_property(connector, edid);
  464. ret = drm_add_edid_modes(connector, edid);
  465. kfree(edid);
  466. }
  467. return ret;
  468. }
  469. static enum drm_mode_status
  470. inno_hdmi_connector_mode_valid(struct drm_connector *connector,
  471. struct drm_display_mode *mode)
  472. {
  473. return MODE_OK;
  474. }
  475. static int
  476. inno_hdmi_probe_single_connector_modes(struct drm_connector *connector,
  477. uint32_t maxX, uint32_t maxY)
  478. {
  479. return drm_helper_probe_single_connector_modes(connector, 1920, 1080);
  480. }
  481. static void inno_hdmi_connector_destroy(struct drm_connector *connector)
  482. {
  483. drm_connector_unregister(connector);
  484. drm_connector_cleanup(connector);
  485. }
  486. static const struct drm_connector_funcs inno_hdmi_connector_funcs = {
  487. .fill_modes = inno_hdmi_probe_single_connector_modes,
  488. .detect = inno_hdmi_connector_detect,
  489. .destroy = inno_hdmi_connector_destroy,
  490. .reset = drm_atomic_helper_connector_reset,
  491. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  492. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  493. };
  494. static struct drm_connector_helper_funcs inno_hdmi_connector_helper_funcs = {
  495. .get_modes = inno_hdmi_connector_get_modes,
  496. .mode_valid = inno_hdmi_connector_mode_valid,
  497. };
  498. static int inno_hdmi_register(struct drm_device *drm, struct inno_hdmi *hdmi)
  499. {
  500. struct drm_encoder *encoder = &hdmi->encoder.encoder;
  501. struct device *dev = hdmi->dev;
  502. encoder->possible_crtcs = drm_of_find_possible_crtcs(drm, dev->of_node);
  503. /*
  504. * If we failed to find the CRTC(s) which this encoder is
  505. * supposed to be connected to, it's because the CRTC has
  506. * not been registered yet. Defer probing, and hope that
  507. * the required CRTC is added later.
  508. */
  509. if (encoder->possible_crtcs == 0)
  510. return -EPROBE_DEFER;
  511. drm_encoder_helper_add(encoder, &inno_hdmi_encoder_helper_funcs);
  512. drm_simple_encoder_init(drm, encoder, DRM_MODE_ENCODER_TMDS);
  513. hdmi->connector.polled = DRM_CONNECTOR_POLL_HPD;
  514. drm_connector_helper_add(&hdmi->connector,
  515. &inno_hdmi_connector_helper_funcs);
  516. drm_connector_init_with_ddc(drm, &hdmi->connector,
  517. &inno_hdmi_connector_funcs,
  518. DRM_MODE_CONNECTOR_HDMIA,
  519. hdmi->ddc);
  520. drm_connector_attach_encoder(&hdmi->connector, encoder);
  521. return 0;
  522. }
  523. static irqreturn_t inno_hdmi_i2c_irq(struct inno_hdmi *hdmi)
  524. {
  525. struct inno_hdmi_i2c *i2c = hdmi->i2c;
  526. u8 stat;
  527. stat = hdmi_readb(hdmi, HDMI_INTERRUPT_STATUS1);
  528. if (!(stat & m_INT_EDID_READY))
  529. return IRQ_NONE;
  530. /* Clear HDMI EDID interrupt flag */
  531. hdmi_writeb(hdmi, HDMI_INTERRUPT_STATUS1, m_INT_EDID_READY);
  532. complete(&i2c->cmp);
  533. return IRQ_HANDLED;
  534. }
  535. static irqreturn_t inno_hdmi_hardirq(int irq, void *dev_id)
  536. {
  537. struct inno_hdmi *hdmi = dev_id;
  538. irqreturn_t ret = IRQ_NONE;
  539. u8 interrupt;
  540. if (hdmi->i2c)
  541. ret = inno_hdmi_i2c_irq(hdmi);
  542. interrupt = hdmi_readb(hdmi, HDMI_STATUS);
  543. if (interrupt & m_INT_HOTPLUG) {
  544. hdmi_modb(hdmi, HDMI_STATUS, m_INT_HOTPLUG, m_INT_HOTPLUG);
  545. ret = IRQ_WAKE_THREAD;
  546. }
  547. return ret;
  548. }
  549. static irqreturn_t inno_hdmi_irq(int irq, void *dev_id)
  550. {
  551. struct inno_hdmi *hdmi = dev_id;
  552. drm_helper_hpd_irq_event(hdmi->connector.dev);
  553. return IRQ_HANDLED;
  554. }
  555. static int inno_hdmi_i2c_read(struct inno_hdmi *hdmi, struct i2c_msg *msgs)
  556. {
  557. int length = msgs->len;
  558. u8 *buf = msgs->buf;
  559. int ret;
  560. ret = wait_for_completion_timeout(&hdmi->i2c->cmp, HZ / 10);
  561. if (!ret)
  562. return -EAGAIN;
  563. while (length--)
  564. *buf++ = hdmi_readb(hdmi, HDMI_EDID_FIFO_ADDR);
  565. return 0;
  566. }
  567. static int inno_hdmi_i2c_write(struct inno_hdmi *hdmi, struct i2c_msg *msgs)
  568. {
  569. /*
  570. * The DDC module only support read EDID message, so
  571. * we assume that each word write to this i2c adapter
  572. * should be the offset of EDID word address.
  573. */
  574. if ((msgs->len != 1) ||
  575. ((msgs->addr != DDC_ADDR) && (msgs->addr != DDC_SEGMENT_ADDR)))
  576. return -EINVAL;
  577. reinit_completion(&hdmi->i2c->cmp);
  578. if (msgs->addr == DDC_SEGMENT_ADDR)
  579. hdmi->i2c->segment_addr = msgs->buf[0];
  580. if (msgs->addr == DDC_ADDR)
  581. hdmi->i2c->ddc_addr = msgs->buf[0];
  582. /* Set edid fifo first addr */
  583. hdmi_writeb(hdmi, HDMI_EDID_FIFO_OFFSET, 0x00);
  584. /* Set edid word address 0x00/0x80 */
  585. hdmi_writeb(hdmi, HDMI_EDID_WORD_ADDR, hdmi->i2c->ddc_addr);
  586. /* Set edid segment pointer */
  587. hdmi_writeb(hdmi, HDMI_EDID_SEGMENT_POINTER, hdmi->i2c->segment_addr);
  588. return 0;
  589. }
  590. static int inno_hdmi_i2c_xfer(struct i2c_adapter *adap,
  591. struct i2c_msg *msgs, int num)
  592. {
  593. struct inno_hdmi *hdmi = i2c_get_adapdata(adap);
  594. struct inno_hdmi_i2c *i2c = hdmi->i2c;
  595. int i, ret = 0;
  596. mutex_lock(&i2c->lock);
  597. /* Clear the EDID interrupt flag and unmute the interrupt */
  598. hdmi_writeb(hdmi, HDMI_INTERRUPT_MASK1, m_INT_EDID_READY);
  599. hdmi_writeb(hdmi, HDMI_INTERRUPT_STATUS1, m_INT_EDID_READY);
  600. for (i = 0; i < num; i++) {
  601. DRM_DEV_DEBUG(hdmi->dev,
  602. "xfer: num: %d/%d, len: %d, flags: %#x\n",
  603. i + 1, num, msgs[i].len, msgs[i].flags);
  604. if (msgs[i].flags & I2C_M_RD)
  605. ret = inno_hdmi_i2c_read(hdmi, &msgs[i]);
  606. else
  607. ret = inno_hdmi_i2c_write(hdmi, &msgs[i]);
  608. if (ret < 0)
  609. break;
  610. }
  611. if (!ret)
  612. ret = num;
  613. /* Mute HDMI EDID interrupt */
  614. hdmi_writeb(hdmi, HDMI_INTERRUPT_MASK1, 0);
  615. mutex_unlock(&i2c->lock);
  616. return ret;
  617. }
  618. static u32 inno_hdmi_i2c_func(struct i2c_adapter *adapter)
  619. {
  620. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  621. }
  622. static const struct i2c_algorithm inno_hdmi_algorithm = {
  623. .master_xfer = inno_hdmi_i2c_xfer,
  624. .functionality = inno_hdmi_i2c_func,
  625. };
  626. static struct i2c_adapter *inno_hdmi_i2c_adapter(struct inno_hdmi *hdmi)
  627. {
  628. struct i2c_adapter *adap;
  629. struct inno_hdmi_i2c *i2c;
  630. int ret;
  631. i2c = devm_kzalloc(hdmi->dev, sizeof(*i2c), GFP_KERNEL);
  632. if (!i2c)
  633. return ERR_PTR(-ENOMEM);
  634. mutex_init(&i2c->lock);
  635. init_completion(&i2c->cmp);
  636. adap = &i2c->adap;
  637. adap->class = I2C_CLASS_DDC;
  638. adap->owner = THIS_MODULE;
  639. adap->dev.parent = hdmi->dev;
  640. adap->dev.of_node = hdmi->dev->of_node;
  641. adap->algo = &inno_hdmi_algorithm;
  642. strlcpy(adap->name, "Inno HDMI", sizeof(adap->name));
  643. i2c_set_adapdata(adap, hdmi);
  644. ret = i2c_add_adapter(adap);
  645. if (ret) {
  646. dev_warn(hdmi->dev, "cannot add %s I2C adapter\n", adap->name);
  647. devm_kfree(hdmi->dev, i2c);
  648. return ERR_PTR(ret);
  649. }
  650. hdmi->i2c = i2c;
  651. DRM_DEV_INFO(hdmi->dev, "registered %s I2C bus driver\n", adap->name);
  652. return adap;
  653. }
  654. static int inno_hdmi_bind(struct device *dev, struct device *master,
  655. void *data)
  656. {
  657. struct platform_device *pdev = to_platform_device(dev);
  658. struct drm_device *drm = data;
  659. struct inno_hdmi *hdmi;
  660. int irq;
  661. int ret;
  662. hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
  663. if (!hdmi)
  664. return -ENOMEM;
  665. hdmi->dev = dev;
  666. hdmi->drm_dev = drm;
  667. hdmi->regs = devm_platform_ioremap_resource(pdev, 0);
  668. if (IS_ERR(hdmi->regs))
  669. return PTR_ERR(hdmi->regs);
  670. hdmi->pclk = devm_clk_get(hdmi->dev, "pclk");
  671. if (IS_ERR(hdmi->pclk)) {
  672. DRM_DEV_ERROR(hdmi->dev, "Unable to get HDMI pclk clk\n");
  673. return PTR_ERR(hdmi->pclk);
  674. }
  675. ret = clk_prepare_enable(hdmi->pclk);
  676. if (ret) {
  677. DRM_DEV_ERROR(hdmi->dev,
  678. "Cannot enable HDMI pclk clock: %d\n", ret);
  679. return ret;
  680. }
  681. irq = platform_get_irq(pdev, 0);
  682. if (irq < 0) {
  683. ret = irq;
  684. goto err_disable_clk;
  685. }
  686. inno_hdmi_reset(hdmi);
  687. hdmi->ddc = inno_hdmi_i2c_adapter(hdmi);
  688. if (IS_ERR(hdmi->ddc)) {
  689. ret = PTR_ERR(hdmi->ddc);
  690. hdmi->ddc = NULL;
  691. goto err_disable_clk;
  692. }
  693. /*
  694. * When IP controller haven't configured to an accurate video
  695. * timing, then the TMDS clock source would be switched to
  696. * PCLK_HDMI, so we need to init the TMDS rate to PCLK rate,
  697. * and reconfigure the DDC clock.
  698. */
  699. hdmi->tmds_rate = clk_get_rate(hdmi->pclk);
  700. inno_hdmi_i2c_init(hdmi);
  701. ret = inno_hdmi_register(drm, hdmi);
  702. if (ret)
  703. goto err_put_adapter;
  704. dev_set_drvdata(dev, hdmi);
  705. /* Unmute hotplug interrupt */
  706. hdmi_modb(hdmi, HDMI_STATUS, m_MASK_INT_HOTPLUG, v_MASK_INT_HOTPLUG(1));
  707. ret = devm_request_threaded_irq(dev, irq, inno_hdmi_hardirq,
  708. inno_hdmi_irq, IRQF_SHARED,
  709. dev_name(dev), hdmi);
  710. if (ret < 0)
  711. goto err_cleanup_hdmi;
  712. return 0;
  713. err_cleanup_hdmi:
  714. hdmi->connector.funcs->destroy(&hdmi->connector);
  715. hdmi->encoder.encoder.funcs->destroy(&hdmi->encoder.encoder);
  716. err_put_adapter:
  717. i2c_put_adapter(hdmi->ddc);
  718. err_disable_clk:
  719. clk_disable_unprepare(hdmi->pclk);
  720. return ret;
  721. }
  722. static void inno_hdmi_unbind(struct device *dev, struct device *master,
  723. void *data)
  724. {
  725. struct inno_hdmi *hdmi = dev_get_drvdata(dev);
  726. hdmi->connector.funcs->destroy(&hdmi->connector);
  727. hdmi->encoder.encoder.funcs->destroy(&hdmi->encoder.encoder);
  728. i2c_put_adapter(hdmi->ddc);
  729. clk_disable_unprepare(hdmi->pclk);
  730. }
  731. static const struct component_ops inno_hdmi_ops = {
  732. .bind = inno_hdmi_bind,
  733. .unbind = inno_hdmi_unbind,
  734. };
  735. static int inno_hdmi_probe(struct platform_device *pdev)
  736. {
  737. return component_add(&pdev->dev, &inno_hdmi_ops);
  738. }
  739. static int inno_hdmi_remove(struct platform_device *pdev)
  740. {
  741. component_del(&pdev->dev, &inno_hdmi_ops);
  742. return 0;
  743. }
  744. static const struct of_device_id inno_hdmi_dt_ids[] = {
  745. { .compatible = "rockchip,rk3036-inno-hdmi",
  746. },
  747. {},
  748. };
  749. MODULE_DEVICE_TABLE(of, inno_hdmi_dt_ids);
  750. struct platform_driver inno_hdmi_driver = {
  751. .probe = inno_hdmi_probe,
  752. .remove = inno_hdmi_remove,
  753. .driver = {
  754. .name = "innohdmi-rockchip",
  755. .of_match_table = inno_hdmi_dt_ids,
  756. },
  757. };