inno_hdmi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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. dev_err(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. 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, mode);
  247. if (hdmi->hdmi_data.enc_out_format == HDMI_COLORSPACE_YUV444)
  248. frame.avi.colorspace = HDMI_COLORSPACE_YUV444;
  249. else if (hdmi->hdmi_data.enc_out_format == HDMI_COLORSPACE_YUV422)
  250. frame.avi.colorspace = HDMI_COLORSPACE_YUV422;
  251. else
  252. frame.avi.colorspace = HDMI_COLORSPACE_RGB;
  253. return inno_hdmi_upload_frame(hdmi, rc, &frame, INFOFRAME_AVI, 0, 0, 0);
  254. }
  255. static int inno_hdmi_config_video_csc(struct inno_hdmi *hdmi)
  256. {
  257. struct hdmi_data_info *data = &hdmi->hdmi_data;
  258. int c0_c2_change = 0;
  259. int csc_enable = 0;
  260. int csc_mode = 0;
  261. int auto_csc = 0;
  262. int value;
  263. int i;
  264. /* Input video mode is SDR RGB24bit, data enable signal from external */
  265. hdmi_writeb(hdmi, HDMI_VIDEO_CONTRL1, v_DE_EXTERNAL |
  266. v_VIDEO_INPUT_FORMAT(VIDEO_INPUT_SDR_RGB444));
  267. /* Input color hardcode to RGB, and output color hardcode to RGB888 */
  268. value = v_VIDEO_INPUT_BITS(VIDEO_INPUT_8BITS) |
  269. v_VIDEO_OUTPUT_COLOR(0) |
  270. v_VIDEO_INPUT_CSP(0);
  271. hdmi_writeb(hdmi, HDMI_VIDEO_CONTRL2, value);
  272. if (data->enc_in_format == data->enc_out_format) {
  273. if ((data->enc_in_format == HDMI_COLORSPACE_RGB) ||
  274. (data->enc_in_format >= HDMI_COLORSPACE_YUV444)) {
  275. value = v_SOF_DISABLE | v_COLOR_DEPTH_NOT_INDICATED(1);
  276. hdmi_writeb(hdmi, HDMI_VIDEO_CONTRL3, value);
  277. hdmi_modb(hdmi, HDMI_VIDEO_CONTRL,
  278. m_VIDEO_AUTO_CSC | m_VIDEO_C0_C2_SWAP,
  279. v_VIDEO_AUTO_CSC(AUTO_CSC_DISABLE) |
  280. v_VIDEO_C0_C2_SWAP(C0_C2_CHANGE_DISABLE));
  281. return 0;
  282. }
  283. }
  284. if (data->colorimetry == HDMI_COLORIMETRY_ITU_601) {
  285. if ((data->enc_in_format == HDMI_COLORSPACE_RGB) &&
  286. (data->enc_out_format == HDMI_COLORSPACE_YUV444)) {
  287. csc_mode = CSC_RGB_0_255_TO_ITU601_16_235_8BIT;
  288. auto_csc = AUTO_CSC_DISABLE;
  289. c0_c2_change = C0_C2_CHANGE_DISABLE;
  290. csc_enable = v_CSC_ENABLE;
  291. } else if ((data->enc_in_format == HDMI_COLORSPACE_YUV444) &&
  292. (data->enc_out_format == HDMI_COLORSPACE_RGB)) {
  293. csc_mode = CSC_ITU601_16_235_TO_RGB_0_255_8BIT;
  294. auto_csc = AUTO_CSC_ENABLE;
  295. c0_c2_change = C0_C2_CHANGE_DISABLE;
  296. csc_enable = v_CSC_DISABLE;
  297. }
  298. } else {
  299. if ((data->enc_in_format == HDMI_COLORSPACE_RGB) &&
  300. (data->enc_out_format == HDMI_COLORSPACE_YUV444)) {
  301. csc_mode = CSC_RGB_0_255_TO_ITU709_16_235_8BIT;
  302. auto_csc = AUTO_CSC_DISABLE;
  303. c0_c2_change = C0_C2_CHANGE_DISABLE;
  304. csc_enable = v_CSC_ENABLE;
  305. } else if ((data->enc_in_format == HDMI_COLORSPACE_YUV444) &&
  306. (data->enc_out_format == HDMI_COLORSPACE_RGB)) {
  307. csc_mode = CSC_ITU709_16_235_TO_RGB_0_255_8BIT;
  308. auto_csc = AUTO_CSC_ENABLE;
  309. c0_c2_change = C0_C2_CHANGE_DISABLE;
  310. csc_enable = v_CSC_DISABLE;
  311. }
  312. }
  313. for (i = 0; i < 24; i++)
  314. hdmi_writeb(hdmi, HDMI_VIDEO_CSC_COEF + i,
  315. coeff_csc[csc_mode][i]);
  316. value = v_SOF_DISABLE | csc_enable | v_COLOR_DEPTH_NOT_INDICATED(1);
  317. hdmi_writeb(hdmi, HDMI_VIDEO_CONTRL3, value);
  318. hdmi_modb(hdmi, HDMI_VIDEO_CONTRL, m_VIDEO_AUTO_CSC |
  319. m_VIDEO_C0_C2_SWAP, v_VIDEO_AUTO_CSC(auto_csc) |
  320. v_VIDEO_C0_C2_SWAP(c0_c2_change));
  321. return 0;
  322. }
  323. static int inno_hdmi_config_video_timing(struct inno_hdmi *hdmi,
  324. struct drm_display_mode *mode)
  325. {
  326. int value;
  327. /* Set detail external video timing polarity and interlace mode */
  328. value = v_EXTERANL_VIDEO(1);
  329. value |= mode->flags & DRM_MODE_FLAG_PHSYNC ?
  330. v_HSYNC_POLARITY(1) : v_HSYNC_POLARITY(0);
  331. value |= mode->flags & DRM_MODE_FLAG_PVSYNC ?
  332. v_VSYNC_POLARITY(1) : v_VSYNC_POLARITY(0);
  333. value |= mode->flags & DRM_MODE_FLAG_INTERLACE ?
  334. v_INETLACE(1) : v_INETLACE(0);
  335. hdmi_writeb(hdmi, HDMI_VIDEO_TIMING_CTL, value);
  336. /* Set detail external video timing */
  337. value = mode->htotal;
  338. hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HTOTAL_L, value & 0xFF);
  339. hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HTOTAL_H, (value >> 8) & 0xFF);
  340. value = mode->htotal - mode->hdisplay;
  341. hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HBLANK_L, value & 0xFF);
  342. hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HBLANK_H, (value >> 8) & 0xFF);
  343. value = mode->hsync_start - mode->hdisplay;
  344. hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HDELAY_L, value & 0xFF);
  345. hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HDELAY_H, (value >> 8) & 0xFF);
  346. value = mode->hsync_end - mode->hsync_start;
  347. hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HDURATION_L, value & 0xFF);
  348. hdmi_writeb(hdmi, HDMI_VIDEO_EXT_HDURATION_H, (value >> 8) & 0xFF);
  349. value = mode->vtotal;
  350. hdmi_writeb(hdmi, HDMI_VIDEO_EXT_VTOTAL_L, value & 0xFF);
  351. hdmi_writeb(hdmi, HDMI_VIDEO_EXT_VTOTAL_H, (value >> 8) & 0xFF);
  352. value = mode->vtotal - mode->vdisplay;
  353. hdmi_writeb(hdmi, HDMI_VIDEO_EXT_VBLANK, value & 0xFF);
  354. value = mode->vsync_start - mode->vdisplay;
  355. hdmi_writeb(hdmi, HDMI_VIDEO_EXT_VDELAY, value & 0xFF);
  356. value = mode->vsync_end - mode->vsync_start;
  357. hdmi_writeb(hdmi, HDMI_VIDEO_EXT_VDURATION, value & 0xFF);
  358. hdmi_writeb(hdmi, HDMI_PHY_PRE_DIV_RATIO, 0x1e);
  359. hdmi_writeb(hdmi, HDMI_PHY_FEEDBACK_DIV_RATIO_LOW, 0x2c);
  360. hdmi_writeb(hdmi, HDMI_PHY_FEEDBACK_DIV_RATIO_HIGH, 0x01);
  361. return 0;
  362. }
  363. static int inno_hdmi_setup(struct inno_hdmi *hdmi,
  364. struct drm_display_mode *mode)
  365. {
  366. hdmi->hdmi_data.vic = drm_match_cea_mode(mode);
  367. hdmi->hdmi_data.enc_in_format = HDMI_COLORSPACE_RGB;
  368. hdmi->hdmi_data.enc_out_format = HDMI_COLORSPACE_RGB;
  369. if ((hdmi->hdmi_data.vic == 6) || (hdmi->hdmi_data.vic == 7) ||
  370. (hdmi->hdmi_data.vic == 21) || (hdmi->hdmi_data.vic == 22) ||
  371. (hdmi->hdmi_data.vic == 2) || (hdmi->hdmi_data.vic == 3) ||
  372. (hdmi->hdmi_data.vic == 17) || (hdmi->hdmi_data.vic == 18))
  373. hdmi->hdmi_data.colorimetry = HDMI_COLORIMETRY_ITU_601;
  374. else
  375. hdmi->hdmi_data.colorimetry = HDMI_COLORIMETRY_ITU_709;
  376. /* Mute video and audio output */
  377. hdmi_modb(hdmi, HDMI_AV_MUTE, m_AUDIO_MUTE | m_VIDEO_BLACK,
  378. v_AUDIO_MUTE(1) | v_VIDEO_MUTE(1));
  379. /* Set HDMI Mode */
  380. hdmi_writeb(hdmi, HDMI_HDCP_CTRL,
  381. v_HDMI_DVI(hdmi->hdmi_data.sink_is_hdmi));
  382. inno_hdmi_config_video_timing(hdmi, mode);
  383. inno_hdmi_config_video_csc(hdmi);
  384. if (hdmi->hdmi_data.sink_is_hdmi) {
  385. inno_hdmi_config_video_avi(hdmi, mode);
  386. inno_hdmi_config_video_vsi(hdmi, mode);
  387. }
  388. /*
  389. * When IP controller have configured to an accurate video
  390. * timing, then the TMDS clock source would be switched to
  391. * DCLK_LCDC, so we need to init the TMDS rate to mode pixel
  392. * clock rate, and reconfigure the DDC clock.
  393. */
  394. hdmi->tmds_rate = mode->clock * 1000;
  395. inno_hdmi_i2c_init(hdmi);
  396. /* Unmute video and audio output */
  397. hdmi_modb(hdmi, HDMI_AV_MUTE, m_AUDIO_MUTE | m_VIDEO_BLACK,
  398. v_AUDIO_MUTE(0) | v_VIDEO_MUTE(0));
  399. return 0;
  400. }
  401. static void inno_hdmi_encoder_mode_set(struct drm_encoder *encoder,
  402. struct drm_display_mode *mode,
  403. struct drm_display_mode *adj_mode)
  404. {
  405. struct inno_hdmi *hdmi = to_inno_hdmi(encoder);
  406. inno_hdmi_setup(hdmi, adj_mode);
  407. /* Store the display mode for plugin/DPMS poweron events */
  408. memcpy(&hdmi->previous_mode, adj_mode, sizeof(hdmi->previous_mode));
  409. }
  410. static void inno_hdmi_encoder_enable(struct drm_encoder *encoder)
  411. {
  412. struct inno_hdmi *hdmi = to_inno_hdmi(encoder);
  413. rockchip_drm_crtc_mode_config(encoder->crtc, DRM_MODE_CONNECTOR_HDMIA,
  414. ROCKCHIP_OUT_MODE_P888);
  415. inno_hdmi_set_pwr_mode(hdmi, NORMAL);
  416. }
  417. static void inno_hdmi_encoder_disable(struct drm_encoder *encoder)
  418. {
  419. struct inno_hdmi *hdmi = to_inno_hdmi(encoder);
  420. inno_hdmi_set_pwr_mode(hdmi, LOWER_PWR);
  421. }
  422. static bool inno_hdmi_encoder_mode_fixup(struct drm_encoder *encoder,
  423. const struct drm_display_mode *mode,
  424. struct drm_display_mode *adj_mode)
  425. {
  426. return true;
  427. }
  428. static struct drm_encoder_helper_funcs inno_hdmi_encoder_helper_funcs = {
  429. .enable = inno_hdmi_encoder_enable,
  430. .disable = inno_hdmi_encoder_disable,
  431. .mode_fixup = inno_hdmi_encoder_mode_fixup,
  432. .mode_set = inno_hdmi_encoder_mode_set,
  433. };
  434. static struct drm_encoder_funcs inno_hdmi_encoder_funcs = {
  435. .destroy = drm_encoder_cleanup,
  436. };
  437. static enum drm_connector_status
  438. inno_hdmi_connector_detect(struct drm_connector *connector, bool force)
  439. {
  440. struct inno_hdmi *hdmi = to_inno_hdmi(connector);
  441. return (hdmi_readb(hdmi, HDMI_STATUS) & m_HOTPLUG) ?
  442. connector_status_connected : connector_status_disconnected;
  443. }
  444. static int inno_hdmi_connector_get_modes(struct drm_connector *connector)
  445. {
  446. struct inno_hdmi *hdmi = to_inno_hdmi(connector);
  447. struct edid *edid;
  448. int ret = 0;
  449. if (!hdmi->ddc)
  450. return 0;
  451. edid = drm_get_edid(connector, hdmi->ddc);
  452. if (edid) {
  453. hdmi->hdmi_data.sink_is_hdmi = drm_detect_hdmi_monitor(edid);
  454. hdmi->hdmi_data.sink_has_audio = drm_detect_monitor_audio(edid);
  455. drm_mode_connector_update_edid_property(connector, edid);
  456. ret = drm_add_edid_modes(connector, edid);
  457. kfree(edid);
  458. }
  459. return ret;
  460. }
  461. static enum drm_mode_status
  462. inno_hdmi_connector_mode_valid(struct drm_connector *connector,
  463. struct drm_display_mode *mode)
  464. {
  465. return MODE_OK;
  466. }
  467. static struct drm_encoder *
  468. inno_hdmi_connector_best_encoder(struct drm_connector *connector)
  469. {
  470. struct inno_hdmi *hdmi = to_inno_hdmi(connector);
  471. return &hdmi->encoder;
  472. }
  473. static int
  474. inno_hdmi_probe_single_connector_modes(struct drm_connector *connector,
  475. uint32_t maxX, uint32_t maxY)
  476. {
  477. return drm_helper_probe_single_connector_modes(connector, 1920, 1080);
  478. }
  479. static void inno_hdmi_connector_destroy(struct drm_connector *connector)
  480. {
  481. drm_connector_unregister(connector);
  482. drm_connector_cleanup(connector);
  483. }
  484. static struct drm_connector_funcs inno_hdmi_connector_funcs = {
  485. .dpms = drm_atomic_helper_connector_dpms,
  486. .fill_modes = inno_hdmi_probe_single_connector_modes,
  487. .detect = inno_hdmi_connector_detect,
  488. .destroy = inno_hdmi_connector_destroy,
  489. .reset = drm_atomic_helper_connector_reset,
  490. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  491. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  492. };
  493. static struct drm_connector_helper_funcs inno_hdmi_connector_helper_funcs = {
  494. .get_modes = inno_hdmi_connector_get_modes,
  495. .mode_valid = inno_hdmi_connector_mode_valid,
  496. .best_encoder = inno_hdmi_connector_best_encoder,
  497. };
  498. static int inno_hdmi_register(struct drm_device *drm, struct inno_hdmi *hdmi)
  499. {
  500. struct drm_encoder *encoder = &hdmi->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_encoder_init(drm, encoder, &inno_hdmi_encoder_funcs,
  513. DRM_MODE_ENCODER_TMDS, NULL);
  514. hdmi->connector.polled = DRM_CONNECTOR_POLL_HPD;
  515. drm_connector_helper_add(&hdmi->connector,
  516. &inno_hdmi_connector_helper_funcs);
  517. drm_connector_init(drm, &hdmi->connector, &inno_hdmi_connector_funcs,
  518. DRM_MODE_CONNECTOR_HDMIA);
  519. drm_mode_connector_attach_encoder(&hdmi->connector, encoder);
  520. return 0;
  521. }
  522. static irqreturn_t inno_hdmi_i2c_irq(struct inno_hdmi *hdmi)
  523. {
  524. struct inno_hdmi_i2c *i2c = hdmi->i2c;
  525. u8 stat;
  526. stat = hdmi_readb(hdmi, HDMI_INTERRUPT_STATUS1);
  527. if (!(stat & m_INT_EDID_READY))
  528. return IRQ_NONE;
  529. /* Clear HDMI EDID interrupt flag */
  530. hdmi_writeb(hdmi, HDMI_INTERRUPT_STATUS1, m_INT_EDID_READY);
  531. complete(&i2c->cmp);
  532. return IRQ_HANDLED;
  533. }
  534. static irqreturn_t inno_hdmi_hardirq(int irq, void *dev_id)
  535. {
  536. struct inno_hdmi *hdmi = dev_id;
  537. irqreturn_t ret = IRQ_NONE;
  538. u8 interrupt;
  539. if (hdmi->i2c)
  540. ret = inno_hdmi_i2c_irq(hdmi);
  541. interrupt = hdmi_readb(hdmi, HDMI_STATUS);
  542. if (interrupt & m_INT_HOTPLUG) {
  543. hdmi_modb(hdmi, HDMI_STATUS, m_INT_HOTPLUG, m_INT_HOTPLUG);
  544. ret = IRQ_WAKE_THREAD;
  545. }
  546. return ret;
  547. }
  548. static irqreturn_t inno_hdmi_irq(int irq, void *dev_id)
  549. {
  550. struct inno_hdmi *hdmi = dev_id;
  551. drm_helper_hpd_irq_event(hdmi->connector.dev);
  552. return IRQ_HANDLED;
  553. }
  554. static int inno_hdmi_i2c_read(struct inno_hdmi *hdmi, struct i2c_msg *msgs)
  555. {
  556. int length = msgs->len;
  557. u8 *buf = msgs->buf;
  558. int ret;
  559. ret = wait_for_completion_timeout(&hdmi->i2c->cmp, HZ / 10);
  560. if (!ret)
  561. return -EAGAIN;
  562. while (length--)
  563. *buf++ = hdmi_readb(hdmi, HDMI_EDID_FIFO_ADDR);
  564. return 0;
  565. }
  566. static int inno_hdmi_i2c_write(struct inno_hdmi *hdmi, struct i2c_msg *msgs)
  567. {
  568. /*
  569. * The DDC module only support read EDID message, so
  570. * we assume that each word write to this i2c adapter
  571. * should be the offset of EDID word address.
  572. */
  573. if ((msgs->len != 1) ||
  574. ((msgs->addr != DDC_ADDR) && (msgs->addr != DDC_SEGMENT_ADDR)))
  575. return -EINVAL;
  576. reinit_completion(&hdmi->i2c->cmp);
  577. if (msgs->addr == DDC_SEGMENT_ADDR)
  578. hdmi->i2c->segment_addr = msgs->buf[0];
  579. if (msgs->addr == DDC_ADDR)
  580. hdmi->i2c->ddc_addr = msgs->buf[0];
  581. /* Set edid fifo first addr */
  582. hdmi_writeb(hdmi, HDMI_EDID_FIFO_OFFSET, 0x00);
  583. /* Set edid word address 0x00/0x80 */
  584. hdmi_writeb(hdmi, HDMI_EDID_WORD_ADDR, hdmi->i2c->ddc_addr);
  585. /* Set edid segment pointer */
  586. hdmi_writeb(hdmi, HDMI_EDID_SEGMENT_POINTER, hdmi->i2c->segment_addr);
  587. return 0;
  588. }
  589. static int inno_hdmi_i2c_xfer(struct i2c_adapter *adap,
  590. struct i2c_msg *msgs, int num)
  591. {
  592. struct inno_hdmi *hdmi = i2c_get_adapdata(adap);
  593. struct inno_hdmi_i2c *i2c = hdmi->i2c;
  594. int i, ret = 0;
  595. mutex_lock(&i2c->lock);
  596. /* Clear the EDID interrupt flag and unmute the interrupt */
  597. hdmi_writeb(hdmi, HDMI_INTERRUPT_MASK1, m_INT_EDID_READY);
  598. hdmi_writeb(hdmi, HDMI_INTERRUPT_STATUS1, m_INT_EDID_READY);
  599. for (i = 0; i < num; i++) {
  600. dev_dbg(hdmi->dev, "xfer: num: %d/%d, len: %d, flags: %#x\n",
  601. i + 1, num, msgs[i].len, msgs[i].flags);
  602. if (msgs[i].flags & I2C_M_RD)
  603. ret = inno_hdmi_i2c_read(hdmi, &msgs[i]);
  604. else
  605. ret = inno_hdmi_i2c_write(hdmi, &msgs[i]);
  606. if (ret < 0)
  607. break;
  608. }
  609. if (!ret)
  610. ret = num;
  611. /* Mute HDMI EDID interrupt */
  612. hdmi_writeb(hdmi, HDMI_INTERRUPT_MASK1, 0);
  613. mutex_unlock(&i2c->lock);
  614. return ret;
  615. }
  616. static u32 inno_hdmi_i2c_func(struct i2c_adapter *adapter)
  617. {
  618. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  619. }
  620. static const struct i2c_algorithm inno_hdmi_algorithm = {
  621. .master_xfer = inno_hdmi_i2c_xfer,
  622. .functionality = inno_hdmi_i2c_func,
  623. };
  624. static struct i2c_adapter *inno_hdmi_i2c_adapter(struct inno_hdmi *hdmi)
  625. {
  626. struct i2c_adapter *adap;
  627. struct inno_hdmi_i2c *i2c;
  628. int ret;
  629. i2c = devm_kzalloc(hdmi->dev, sizeof(*i2c), GFP_KERNEL);
  630. if (!i2c)
  631. return ERR_PTR(-ENOMEM);
  632. mutex_init(&i2c->lock);
  633. init_completion(&i2c->cmp);
  634. adap = &i2c->adap;
  635. adap->class = I2C_CLASS_DDC;
  636. adap->owner = THIS_MODULE;
  637. adap->dev.parent = hdmi->dev;
  638. adap->dev.of_node = hdmi->dev->of_node;
  639. adap->algo = &inno_hdmi_algorithm;
  640. strlcpy(adap->name, "Inno HDMI", sizeof(adap->name));
  641. i2c_set_adapdata(adap, hdmi);
  642. ret = i2c_add_adapter(adap);
  643. if (ret) {
  644. dev_warn(hdmi->dev, "cannot add %s I2C adapter\n", adap->name);
  645. devm_kfree(hdmi->dev, i2c);
  646. return ERR_PTR(ret);
  647. }
  648. hdmi->i2c = i2c;
  649. dev_info(hdmi->dev, "registered %s I2C bus driver\n", adap->name);
  650. return adap;
  651. }
  652. static int inno_hdmi_bind(struct device *dev, struct device *master,
  653. void *data)
  654. {
  655. struct platform_device *pdev = to_platform_device(dev);
  656. struct drm_device *drm = data;
  657. struct inno_hdmi *hdmi;
  658. struct resource *iores;
  659. int irq;
  660. int ret;
  661. hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
  662. if (!hdmi)
  663. return -ENOMEM;
  664. hdmi->dev = dev;
  665. hdmi->drm_dev = drm;
  666. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  667. if (!iores)
  668. return -ENXIO;
  669. hdmi->regs = devm_ioremap_resource(dev, iores);
  670. if (IS_ERR(hdmi->regs))
  671. return PTR_ERR(hdmi->regs);
  672. hdmi->pclk = devm_clk_get(hdmi->dev, "pclk");
  673. if (IS_ERR(hdmi->pclk)) {
  674. dev_err(hdmi->dev, "Unable to get HDMI pclk clk\n");
  675. return PTR_ERR(hdmi->pclk);
  676. }
  677. ret = clk_prepare_enable(hdmi->pclk);
  678. if (ret) {
  679. dev_err(hdmi->dev, "Cannot enable HDMI pclk clock: %d\n", ret);
  680. return ret;
  681. }
  682. irq = platform_get_irq(pdev, 0);
  683. if (irq < 0)
  684. return irq;
  685. inno_hdmi_reset(hdmi);
  686. hdmi->ddc = inno_hdmi_i2c_adapter(hdmi);
  687. if (IS_ERR(hdmi->ddc)) {
  688. hdmi->ddc = NULL;
  689. return PTR_ERR(hdmi->ddc);
  690. }
  691. /*
  692. * When IP controller haven't configured to an accurate video
  693. * timing, then the TMDS clock source would be switched to
  694. * PCLK_HDMI, so we need to init the TMDS rate to PCLK rate,
  695. * and reconfigure the DDC clock.
  696. */
  697. hdmi->tmds_rate = clk_get_rate(hdmi->pclk);
  698. inno_hdmi_i2c_init(hdmi);
  699. ret = inno_hdmi_register(drm, hdmi);
  700. if (ret)
  701. return ret;
  702. dev_set_drvdata(dev, hdmi);
  703. /* Unmute hotplug interrupt */
  704. hdmi_modb(hdmi, HDMI_STATUS, m_MASK_INT_HOTPLUG, v_MASK_INT_HOTPLUG(1));
  705. ret = devm_request_threaded_irq(dev, irq, inno_hdmi_hardirq,
  706. inno_hdmi_irq, IRQF_SHARED,
  707. dev_name(dev), hdmi);
  708. return ret;
  709. }
  710. static void inno_hdmi_unbind(struct device *dev, struct device *master,
  711. void *data)
  712. {
  713. struct inno_hdmi *hdmi = dev_get_drvdata(dev);
  714. hdmi->connector.funcs->destroy(&hdmi->connector);
  715. hdmi->encoder.funcs->destroy(&hdmi->encoder);
  716. clk_disable_unprepare(hdmi->pclk);
  717. i2c_put_adapter(hdmi->ddc);
  718. }
  719. static const struct component_ops inno_hdmi_ops = {
  720. .bind = inno_hdmi_bind,
  721. .unbind = inno_hdmi_unbind,
  722. };
  723. static int inno_hdmi_probe(struct platform_device *pdev)
  724. {
  725. return component_add(&pdev->dev, &inno_hdmi_ops);
  726. }
  727. static int inno_hdmi_remove(struct platform_device *pdev)
  728. {
  729. component_del(&pdev->dev, &inno_hdmi_ops);
  730. return 0;
  731. }
  732. static const struct of_device_id inno_hdmi_dt_ids[] = {
  733. { .compatible = "rockchip,rk3036-inno-hdmi",
  734. },
  735. {},
  736. };
  737. MODULE_DEVICE_TABLE(of, inno_hdmi_dt_ids);
  738. static struct platform_driver inno_hdmi_driver = {
  739. .probe = inno_hdmi_probe,
  740. .remove = inno_hdmi_remove,
  741. .driver = {
  742. .name = "innohdmi-rockchip",
  743. .of_match_table = inno_hdmi_dt_ids,
  744. },
  745. };
  746. module_platform_driver(inno_hdmi_driver);
  747. MODULE_AUTHOR("Zheng Yang <zhengyang@rock-chips.com>");
  748. MODULE_AUTHOR("Yakir Yang <ykk@rock-chips.com>");
  749. MODULE_DESCRIPTION("Rockchip Specific INNO-HDMI Driver");
  750. MODULE_LICENSE("GPL v2");
  751. MODULE_ALIAS("platform:innohdmi-rockchip");