inno_hdmi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  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. inno_hdmi_set_pwr_mode(hdmi, NORMAL);
  414. }
  415. static void inno_hdmi_encoder_disable(struct drm_encoder *encoder)
  416. {
  417. struct inno_hdmi *hdmi = to_inno_hdmi(encoder);
  418. inno_hdmi_set_pwr_mode(hdmi, LOWER_PWR);
  419. }
  420. static bool inno_hdmi_encoder_mode_fixup(struct drm_encoder *encoder,
  421. const struct drm_display_mode *mode,
  422. struct drm_display_mode *adj_mode)
  423. {
  424. return true;
  425. }
  426. static int
  427. inno_hdmi_encoder_atomic_check(struct drm_encoder *encoder,
  428. struct drm_crtc_state *crtc_state,
  429. struct drm_connector_state *conn_state)
  430. {
  431. struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
  432. s->output_mode = ROCKCHIP_OUT_MODE_P888;
  433. s->output_type = DRM_MODE_CONNECTOR_HDMIA;
  434. return 0;
  435. }
  436. static struct drm_encoder_helper_funcs inno_hdmi_encoder_helper_funcs = {
  437. .enable = inno_hdmi_encoder_enable,
  438. .disable = inno_hdmi_encoder_disable,
  439. .mode_fixup = inno_hdmi_encoder_mode_fixup,
  440. .mode_set = inno_hdmi_encoder_mode_set,
  441. .atomic_check = inno_hdmi_encoder_atomic_check,
  442. };
  443. static struct drm_encoder_funcs inno_hdmi_encoder_funcs = {
  444. .destroy = drm_encoder_cleanup,
  445. };
  446. static enum drm_connector_status
  447. inno_hdmi_connector_detect(struct drm_connector *connector, bool force)
  448. {
  449. struct inno_hdmi *hdmi = 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 = 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_is_hdmi = drm_detect_hdmi_monitor(edid);
  463. hdmi->hdmi_data.sink_has_audio = drm_detect_monitor_audio(edid);
  464. drm_mode_connector_update_edid_property(connector, edid);
  465. ret = drm_add_edid_modes(connector, edid);
  466. kfree(edid);
  467. }
  468. return ret;
  469. }
  470. static enum drm_mode_status
  471. inno_hdmi_connector_mode_valid(struct drm_connector *connector,
  472. struct drm_display_mode *mode)
  473. {
  474. return MODE_OK;
  475. }
  476. static int
  477. inno_hdmi_probe_single_connector_modes(struct drm_connector *connector,
  478. uint32_t maxX, uint32_t maxY)
  479. {
  480. return drm_helper_probe_single_connector_modes(connector, 1920, 1080);
  481. }
  482. static void inno_hdmi_connector_destroy(struct drm_connector *connector)
  483. {
  484. drm_connector_unregister(connector);
  485. drm_connector_cleanup(connector);
  486. }
  487. static struct drm_connector_funcs inno_hdmi_connector_funcs = {
  488. .dpms = drm_atomic_helper_connector_dpms,
  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_mode_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. dev_dbg(hdmi->dev, "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. 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. struct resource *iores;
  661. int irq;
  662. int ret;
  663. hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
  664. if (!hdmi)
  665. return -ENOMEM;
  666. hdmi->dev = dev;
  667. hdmi->drm_dev = drm;
  668. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  669. if (!iores)
  670. return -ENXIO;
  671. hdmi->regs = devm_ioremap_resource(dev, iores);
  672. if (IS_ERR(hdmi->regs))
  673. return PTR_ERR(hdmi->regs);
  674. hdmi->pclk = devm_clk_get(hdmi->dev, "pclk");
  675. if (IS_ERR(hdmi->pclk)) {
  676. dev_err(hdmi->dev, "Unable to get HDMI pclk clk\n");
  677. return PTR_ERR(hdmi->pclk);
  678. }
  679. ret = clk_prepare_enable(hdmi->pclk);
  680. if (ret) {
  681. dev_err(hdmi->dev, "Cannot enable HDMI pclk clock: %d\n", ret);
  682. return ret;
  683. }
  684. irq = platform_get_irq(pdev, 0);
  685. if (irq < 0)
  686. return irq;
  687. inno_hdmi_reset(hdmi);
  688. hdmi->ddc = inno_hdmi_i2c_adapter(hdmi);
  689. if (IS_ERR(hdmi->ddc)) {
  690. ret = PTR_ERR(hdmi->ddc);
  691. hdmi->ddc = NULL;
  692. return ret;
  693. }
  694. /*
  695. * When IP controller haven't configured to an accurate video
  696. * timing, then the TMDS clock source would be switched to
  697. * PCLK_HDMI, so we need to init the TMDS rate to PCLK rate,
  698. * and reconfigure the DDC clock.
  699. */
  700. hdmi->tmds_rate = clk_get_rate(hdmi->pclk);
  701. inno_hdmi_i2c_init(hdmi);
  702. ret = inno_hdmi_register(drm, hdmi);
  703. if (ret)
  704. return ret;
  705. dev_set_drvdata(dev, hdmi);
  706. /* Unmute hotplug interrupt */
  707. hdmi_modb(hdmi, HDMI_STATUS, m_MASK_INT_HOTPLUG, v_MASK_INT_HOTPLUG(1));
  708. ret = devm_request_threaded_irq(dev, irq, inno_hdmi_hardirq,
  709. inno_hdmi_irq, IRQF_SHARED,
  710. dev_name(dev), hdmi);
  711. return ret;
  712. }
  713. static void inno_hdmi_unbind(struct device *dev, struct device *master,
  714. void *data)
  715. {
  716. struct inno_hdmi *hdmi = dev_get_drvdata(dev);
  717. hdmi->connector.funcs->destroy(&hdmi->connector);
  718. hdmi->encoder.funcs->destroy(&hdmi->encoder);
  719. clk_disable_unprepare(hdmi->pclk);
  720. i2c_put_adapter(hdmi->ddc);
  721. }
  722. static const struct component_ops inno_hdmi_ops = {
  723. .bind = inno_hdmi_bind,
  724. .unbind = inno_hdmi_unbind,
  725. };
  726. static int inno_hdmi_probe(struct platform_device *pdev)
  727. {
  728. return component_add(&pdev->dev, &inno_hdmi_ops);
  729. }
  730. static int inno_hdmi_remove(struct platform_device *pdev)
  731. {
  732. component_del(&pdev->dev, &inno_hdmi_ops);
  733. return 0;
  734. }
  735. static const struct of_device_id inno_hdmi_dt_ids[] = {
  736. { .compatible = "rockchip,rk3036-inno-hdmi",
  737. },
  738. {},
  739. };
  740. MODULE_DEVICE_TABLE(of, inno_hdmi_dt_ids);
  741. static struct platform_driver inno_hdmi_driver = {
  742. .probe = inno_hdmi_probe,
  743. .remove = inno_hdmi_remove,
  744. .driver = {
  745. .name = "innohdmi-rockchip",
  746. .of_match_table = inno_hdmi_dt_ids,
  747. },
  748. };
  749. module_platform_driver(inno_hdmi_driver);
  750. MODULE_AUTHOR("Zheng Yang <zhengyang@rock-chips.com>");
  751. MODULE_AUTHOR("Yakir Yang <ykk@rock-chips.com>");
  752. MODULE_DESCRIPTION("Rockchip Specific INNO-HDMI Driver");
  753. MODULE_LICENSE("GPL v2");
  754. MODULE_ALIAS("platform:innohdmi-rockchip");