inno_hdmi.c 27 KB

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