inno_hdmi.c 25 KB

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