sti_hdmi.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) STMicroelectronics SA 2014
  4. * Author: Vincent Abriou <vincent.abriou@st.com> for STMicroelectronics.
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/component.h>
  8. #include <linux/debugfs.h>
  9. #include <linux/hdmi.h>
  10. #include <linux/i2c.h>
  11. #include <linux/module.h>
  12. #include <linux/io.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/reset.h>
  15. #include <drm/drm_atomic_helper.h>
  16. #include <drm/drm_bridge.h>
  17. #include <drm/drm_debugfs.h>
  18. #include <drm/drm_drv.h>
  19. #include <drm/drm_edid.h>
  20. #include <drm/drm_file.h>
  21. #include <drm/drm_print.h>
  22. #include <drm/drm_probe_helper.h>
  23. #include <sound/hdmi-codec.h>
  24. #include "sti_hdmi.h"
  25. #include "sti_hdmi_tx3g4c28phy.h"
  26. #include "sti_vtg.h"
  27. #define HDMI_CFG 0x0000
  28. #define HDMI_INT_EN 0x0004
  29. #define HDMI_INT_STA 0x0008
  30. #define HDMI_INT_CLR 0x000C
  31. #define HDMI_STA 0x0010
  32. #define HDMI_ACTIVE_VID_XMIN 0x0100
  33. #define HDMI_ACTIVE_VID_XMAX 0x0104
  34. #define HDMI_ACTIVE_VID_YMIN 0x0108
  35. #define HDMI_ACTIVE_VID_YMAX 0x010C
  36. #define HDMI_DFLT_CHL0_DAT 0x0110
  37. #define HDMI_DFLT_CHL1_DAT 0x0114
  38. #define HDMI_DFLT_CHL2_DAT 0x0118
  39. #define HDMI_AUDIO_CFG 0x0200
  40. #define HDMI_SPDIF_FIFO_STATUS 0x0204
  41. #define HDMI_SW_DI_1_HEAD_WORD 0x0210
  42. #define HDMI_SW_DI_1_PKT_WORD0 0x0214
  43. #define HDMI_SW_DI_1_PKT_WORD1 0x0218
  44. #define HDMI_SW_DI_1_PKT_WORD2 0x021C
  45. #define HDMI_SW_DI_1_PKT_WORD3 0x0220
  46. #define HDMI_SW_DI_1_PKT_WORD4 0x0224
  47. #define HDMI_SW_DI_1_PKT_WORD5 0x0228
  48. #define HDMI_SW_DI_1_PKT_WORD6 0x022C
  49. #define HDMI_SW_DI_CFG 0x0230
  50. #define HDMI_SAMPLE_FLAT_MASK 0x0244
  51. #define HDMI_AUDN 0x0400
  52. #define HDMI_AUD_CTS 0x0404
  53. #define HDMI_SW_DI_2_HEAD_WORD 0x0600
  54. #define HDMI_SW_DI_2_PKT_WORD0 0x0604
  55. #define HDMI_SW_DI_2_PKT_WORD1 0x0608
  56. #define HDMI_SW_DI_2_PKT_WORD2 0x060C
  57. #define HDMI_SW_DI_2_PKT_WORD3 0x0610
  58. #define HDMI_SW_DI_2_PKT_WORD4 0x0614
  59. #define HDMI_SW_DI_2_PKT_WORD5 0x0618
  60. #define HDMI_SW_DI_2_PKT_WORD6 0x061C
  61. #define HDMI_SW_DI_3_HEAD_WORD 0x0620
  62. #define HDMI_SW_DI_3_PKT_WORD0 0x0624
  63. #define HDMI_SW_DI_3_PKT_WORD1 0x0628
  64. #define HDMI_SW_DI_3_PKT_WORD2 0x062C
  65. #define HDMI_SW_DI_3_PKT_WORD3 0x0630
  66. #define HDMI_SW_DI_3_PKT_WORD4 0x0634
  67. #define HDMI_SW_DI_3_PKT_WORD5 0x0638
  68. #define HDMI_SW_DI_3_PKT_WORD6 0x063C
  69. #define HDMI_IFRAME_SLOT_AVI 1
  70. #define HDMI_IFRAME_SLOT_AUDIO 2
  71. #define HDMI_IFRAME_SLOT_VENDOR 3
  72. #define XCAT(prefix, x, suffix) prefix ## x ## suffix
  73. #define HDMI_SW_DI_N_HEAD_WORD(x) XCAT(HDMI_SW_DI_, x, _HEAD_WORD)
  74. #define HDMI_SW_DI_N_PKT_WORD0(x) XCAT(HDMI_SW_DI_, x, _PKT_WORD0)
  75. #define HDMI_SW_DI_N_PKT_WORD1(x) XCAT(HDMI_SW_DI_, x, _PKT_WORD1)
  76. #define HDMI_SW_DI_N_PKT_WORD2(x) XCAT(HDMI_SW_DI_, x, _PKT_WORD2)
  77. #define HDMI_SW_DI_N_PKT_WORD3(x) XCAT(HDMI_SW_DI_, x, _PKT_WORD3)
  78. #define HDMI_SW_DI_N_PKT_WORD4(x) XCAT(HDMI_SW_DI_, x, _PKT_WORD4)
  79. #define HDMI_SW_DI_N_PKT_WORD5(x) XCAT(HDMI_SW_DI_, x, _PKT_WORD5)
  80. #define HDMI_SW_DI_N_PKT_WORD6(x) XCAT(HDMI_SW_DI_, x, _PKT_WORD6)
  81. #define HDMI_SW_DI_MAX_WORD 7
  82. #define HDMI_IFRAME_DISABLED 0x0
  83. #define HDMI_IFRAME_SINGLE_SHOT 0x1
  84. #define HDMI_IFRAME_FIELD 0x2
  85. #define HDMI_IFRAME_FRAME 0x3
  86. #define HDMI_IFRAME_MASK 0x3
  87. #define HDMI_IFRAME_CFG_DI_N(x, n) ((x) << ((n-1)*4)) /* n from 1 to 6 */
  88. #define HDMI_CFG_DEVICE_EN BIT(0)
  89. #define HDMI_CFG_HDMI_NOT_DVI BIT(1)
  90. #define HDMI_CFG_HDCP_EN BIT(2)
  91. #define HDMI_CFG_ESS_NOT_OESS BIT(3)
  92. #define HDMI_CFG_H_SYNC_POL_NEG BIT(4)
  93. #define HDMI_CFG_V_SYNC_POL_NEG BIT(6)
  94. #define HDMI_CFG_422_EN BIT(8)
  95. #define HDMI_CFG_FIFO_OVERRUN_CLR BIT(12)
  96. #define HDMI_CFG_FIFO_UNDERRUN_CLR BIT(13)
  97. #define HDMI_CFG_SW_RST_EN BIT(31)
  98. #define HDMI_INT_GLOBAL BIT(0)
  99. #define HDMI_INT_SW_RST BIT(1)
  100. #define HDMI_INT_PIX_CAP BIT(3)
  101. #define HDMI_INT_HOT_PLUG BIT(4)
  102. #define HDMI_INT_DLL_LCK BIT(5)
  103. #define HDMI_INT_NEW_FRAME BIT(6)
  104. #define HDMI_INT_GENCTRL_PKT BIT(7)
  105. #define HDMI_INT_AUDIO_FIFO_XRUN BIT(8)
  106. #define HDMI_INT_SINK_TERM_PRESENT BIT(11)
  107. #define HDMI_DEFAULT_INT (HDMI_INT_SINK_TERM_PRESENT \
  108. | HDMI_INT_DLL_LCK \
  109. | HDMI_INT_HOT_PLUG \
  110. | HDMI_INT_GLOBAL)
  111. #define HDMI_WORKING_INT (HDMI_INT_SINK_TERM_PRESENT \
  112. | HDMI_INT_AUDIO_FIFO_XRUN \
  113. | HDMI_INT_GENCTRL_PKT \
  114. | HDMI_INT_NEW_FRAME \
  115. | HDMI_INT_DLL_LCK \
  116. | HDMI_INT_HOT_PLUG \
  117. | HDMI_INT_PIX_CAP \
  118. | HDMI_INT_SW_RST \
  119. | HDMI_INT_GLOBAL)
  120. #define HDMI_STA_SW_RST BIT(1)
  121. #define HDMI_AUD_CFG_8CH BIT(0)
  122. #define HDMI_AUD_CFG_SPDIF_DIV_2 BIT(1)
  123. #define HDMI_AUD_CFG_SPDIF_DIV_3 BIT(2)
  124. #define HDMI_AUD_CFG_SPDIF_CLK_DIV_4 (BIT(1) | BIT(2))
  125. #define HDMI_AUD_CFG_CTS_CLK_256FS BIT(12)
  126. #define HDMI_AUD_CFG_DTS_INVALID BIT(16)
  127. #define HDMI_AUD_CFG_ONE_BIT_INVALID (BIT(18) | BIT(19) | BIT(20) | BIT(21))
  128. #define HDMI_AUD_CFG_CH12_VALID BIT(28)
  129. #define HDMI_AUD_CFG_CH34_VALID BIT(29)
  130. #define HDMI_AUD_CFG_CH56_VALID BIT(30)
  131. #define HDMI_AUD_CFG_CH78_VALID BIT(31)
  132. /* sample flat mask */
  133. #define HDMI_SAMPLE_FLAT_NO 0
  134. #define HDMI_SAMPLE_FLAT_SP0 BIT(0)
  135. #define HDMI_SAMPLE_FLAT_SP1 BIT(1)
  136. #define HDMI_SAMPLE_FLAT_SP2 BIT(2)
  137. #define HDMI_SAMPLE_FLAT_SP3 BIT(3)
  138. #define HDMI_SAMPLE_FLAT_ALL (HDMI_SAMPLE_FLAT_SP0 | HDMI_SAMPLE_FLAT_SP1 |\
  139. HDMI_SAMPLE_FLAT_SP2 | HDMI_SAMPLE_FLAT_SP3)
  140. #define HDMI_INFOFRAME_HEADER_TYPE(x) (((x) & 0xff) << 0)
  141. #define HDMI_INFOFRAME_HEADER_VERSION(x) (((x) & 0xff) << 8)
  142. #define HDMI_INFOFRAME_HEADER_LEN(x) (((x) & 0x0f) << 16)
  143. struct sti_hdmi_connector {
  144. struct drm_connector drm_connector;
  145. struct drm_encoder *encoder;
  146. struct sti_hdmi *hdmi;
  147. struct drm_property *colorspace_property;
  148. };
  149. #define to_sti_hdmi_connector(x) \
  150. container_of(x, struct sti_hdmi_connector, drm_connector)
  151. static const struct drm_prop_enum_list colorspace_mode_names[] = {
  152. { HDMI_COLORSPACE_RGB, "rgb" },
  153. { HDMI_COLORSPACE_YUV422, "yuv422" },
  154. { HDMI_COLORSPACE_YUV444, "yuv444" },
  155. };
  156. u32 hdmi_read(struct sti_hdmi *hdmi, int offset)
  157. {
  158. return readl(hdmi->regs + offset);
  159. }
  160. void hdmi_write(struct sti_hdmi *hdmi, u32 val, int offset)
  161. {
  162. writel(val, hdmi->regs + offset);
  163. }
  164. /*
  165. * HDMI interrupt handler threaded
  166. *
  167. * @irq: irq number
  168. * @arg: connector structure
  169. */
  170. static irqreturn_t hdmi_irq_thread(int irq, void *arg)
  171. {
  172. struct sti_hdmi *hdmi = arg;
  173. /* Hot plug/unplug IRQ */
  174. if (hdmi->irq_status & HDMI_INT_HOT_PLUG) {
  175. hdmi->hpd = readl(hdmi->regs + HDMI_STA) & HDMI_STA_HOT_PLUG;
  176. if (hdmi->drm_dev)
  177. drm_helper_hpd_irq_event(hdmi->drm_dev);
  178. }
  179. /* Sw reset and PLL lock are exclusive so we can use the same
  180. * event to signal them
  181. */
  182. if (hdmi->irq_status & (HDMI_INT_SW_RST | HDMI_INT_DLL_LCK)) {
  183. hdmi->event_received = true;
  184. wake_up_interruptible(&hdmi->wait_event);
  185. }
  186. /* Audio FIFO underrun IRQ */
  187. if (hdmi->irq_status & HDMI_INT_AUDIO_FIFO_XRUN)
  188. DRM_INFO("Warning: audio FIFO underrun occurs!\n");
  189. return IRQ_HANDLED;
  190. }
  191. /*
  192. * HDMI interrupt handler
  193. *
  194. * @irq: irq number
  195. * @arg: connector structure
  196. */
  197. static irqreturn_t hdmi_irq(int irq, void *arg)
  198. {
  199. struct sti_hdmi *hdmi = arg;
  200. /* read interrupt status */
  201. hdmi->irq_status = hdmi_read(hdmi, HDMI_INT_STA);
  202. /* clear interrupt status */
  203. hdmi_write(hdmi, hdmi->irq_status, HDMI_INT_CLR);
  204. /* force sync bus write */
  205. hdmi_read(hdmi, HDMI_INT_STA);
  206. return IRQ_WAKE_THREAD;
  207. }
  208. /*
  209. * Set hdmi active area depending on the drm display mode selected
  210. *
  211. * @hdmi: pointer on the hdmi internal structure
  212. */
  213. static void hdmi_active_area(struct sti_hdmi *hdmi)
  214. {
  215. u32 xmin, xmax;
  216. u32 ymin, ymax;
  217. xmin = sti_vtg_get_pixel_number(hdmi->mode, 1);
  218. xmax = sti_vtg_get_pixel_number(hdmi->mode, hdmi->mode.hdisplay);
  219. ymin = sti_vtg_get_line_number(hdmi->mode, 0);
  220. ymax = sti_vtg_get_line_number(hdmi->mode, hdmi->mode.vdisplay - 1);
  221. hdmi_write(hdmi, xmin, HDMI_ACTIVE_VID_XMIN);
  222. hdmi_write(hdmi, xmax, HDMI_ACTIVE_VID_XMAX);
  223. hdmi_write(hdmi, ymin, HDMI_ACTIVE_VID_YMIN);
  224. hdmi_write(hdmi, ymax, HDMI_ACTIVE_VID_YMAX);
  225. }
  226. /*
  227. * Overall hdmi configuration
  228. *
  229. * @hdmi: pointer on the hdmi internal structure
  230. */
  231. static void hdmi_config(struct sti_hdmi *hdmi)
  232. {
  233. struct drm_connector *connector = hdmi->drm_connector;
  234. u32 conf;
  235. DRM_DEBUG_DRIVER("\n");
  236. /* Clear overrun and underrun fifo */
  237. conf = HDMI_CFG_FIFO_OVERRUN_CLR | HDMI_CFG_FIFO_UNDERRUN_CLR;
  238. /* Select encryption type and the framing mode */
  239. conf |= HDMI_CFG_ESS_NOT_OESS;
  240. if (connector->display_info.is_hdmi)
  241. conf |= HDMI_CFG_HDMI_NOT_DVI;
  242. /* Set Hsync polarity */
  243. if (hdmi->mode.flags & DRM_MODE_FLAG_NHSYNC) {
  244. DRM_DEBUG_DRIVER("H Sync Negative\n");
  245. conf |= HDMI_CFG_H_SYNC_POL_NEG;
  246. }
  247. /* Set Vsync polarity */
  248. if (hdmi->mode.flags & DRM_MODE_FLAG_NVSYNC) {
  249. DRM_DEBUG_DRIVER("V Sync Negative\n");
  250. conf |= HDMI_CFG_V_SYNC_POL_NEG;
  251. }
  252. /* Enable HDMI */
  253. conf |= HDMI_CFG_DEVICE_EN;
  254. hdmi_write(hdmi, conf, HDMI_CFG);
  255. }
  256. /*
  257. * Helper to reset info frame
  258. *
  259. * @hdmi: pointer on the hdmi internal structure
  260. * @slot: infoframe to reset
  261. */
  262. static void hdmi_infoframe_reset(struct sti_hdmi *hdmi,
  263. u32 slot)
  264. {
  265. u32 val, i;
  266. u32 head_offset, pack_offset;
  267. switch (slot) {
  268. case HDMI_IFRAME_SLOT_AVI:
  269. head_offset = HDMI_SW_DI_N_HEAD_WORD(HDMI_IFRAME_SLOT_AVI);
  270. pack_offset = HDMI_SW_DI_N_PKT_WORD0(HDMI_IFRAME_SLOT_AVI);
  271. break;
  272. case HDMI_IFRAME_SLOT_AUDIO:
  273. head_offset = HDMI_SW_DI_N_HEAD_WORD(HDMI_IFRAME_SLOT_AUDIO);
  274. pack_offset = HDMI_SW_DI_N_PKT_WORD0(HDMI_IFRAME_SLOT_AUDIO);
  275. break;
  276. case HDMI_IFRAME_SLOT_VENDOR:
  277. head_offset = HDMI_SW_DI_N_HEAD_WORD(HDMI_IFRAME_SLOT_VENDOR);
  278. pack_offset = HDMI_SW_DI_N_PKT_WORD0(HDMI_IFRAME_SLOT_VENDOR);
  279. break;
  280. default:
  281. DRM_ERROR("unsupported infoframe slot: %#x\n", slot);
  282. return;
  283. }
  284. /* Disable transmission for the selected slot */
  285. val = hdmi_read(hdmi, HDMI_SW_DI_CFG);
  286. val &= ~HDMI_IFRAME_CFG_DI_N(HDMI_IFRAME_MASK, slot);
  287. hdmi_write(hdmi, val, HDMI_SW_DI_CFG);
  288. /* Reset info frame registers */
  289. hdmi_write(hdmi, 0x0, head_offset);
  290. for (i = 0; i < HDMI_SW_DI_MAX_WORD; i += sizeof(u32))
  291. hdmi_write(hdmi, 0x0, pack_offset + i);
  292. }
  293. /*
  294. * Helper to concatenate infoframe in 32 bits word
  295. *
  296. * @ptr: pointer on the hdmi internal structure
  297. * @size: size to write
  298. */
  299. static inline unsigned int hdmi_infoframe_subpack(const u8 *ptr, size_t size)
  300. {
  301. unsigned long value = 0;
  302. size_t i;
  303. for (i = size; i > 0; i--)
  304. value = (value << 8) | ptr[i - 1];
  305. return value;
  306. }
  307. /*
  308. * Helper to write info frame
  309. *
  310. * @hdmi: pointer on the hdmi internal structure
  311. * @data: infoframe to write
  312. * @size: size to write
  313. */
  314. static void hdmi_infoframe_write_infopack(struct sti_hdmi *hdmi,
  315. const u8 *data,
  316. size_t size)
  317. {
  318. const u8 *ptr = data;
  319. u32 val, slot, mode, i;
  320. u32 head_offset, pack_offset;
  321. switch (*ptr) {
  322. case HDMI_INFOFRAME_TYPE_AVI:
  323. slot = HDMI_IFRAME_SLOT_AVI;
  324. mode = HDMI_IFRAME_FIELD;
  325. head_offset = HDMI_SW_DI_N_HEAD_WORD(HDMI_IFRAME_SLOT_AVI);
  326. pack_offset = HDMI_SW_DI_N_PKT_WORD0(HDMI_IFRAME_SLOT_AVI);
  327. break;
  328. case HDMI_INFOFRAME_TYPE_AUDIO:
  329. slot = HDMI_IFRAME_SLOT_AUDIO;
  330. mode = HDMI_IFRAME_FRAME;
  331. head_offset = HDMI_SW_DI_N_HEAD_WORD(HDMI_IFRAME_SLOT_AUDIO);
  332. pack_offset = HDMI_SW_DI_N_PKT_WORD0(HDMI_IFRAME_SLOT_AUDIO);
  333. break;
  334. case HDMI_INFOFRAME_TYPE_VENDOR:
  335. slot = HDMI_IFRAME_SLOT_VENDOR;
  336. mode = HDMI_IFRAME_FRAME;
  337. head_offset = HDMI_SW_DI_N_HEAD_WORD(HDMI_IFRAME_SLOT_VENDOR);
  338. pack_offset = HDMI_SW_DI_N_PKT_WORD0(HDMI_IFRAME_SLOT_VENDOR);
  339. break;
  340. default:
  341. DRM_ERROR("unsupported infoframe type: %#x\n", *ptr);
  342. return;
  343. }
  344. /* Disable transmission slot for updated infoframe */
  345. val = hdmi_read(hdmi, HDMI_SW_DI_CFG);
  346. val &= ~HDMI_IFRAME_CFG_DI_N(HDMI_IFRAME_MASK, slot);
  347. hdmi_write(hdmi, val, HDMI_SW_DI_CFG);
  348. val = HDMI_INFOFRAME_HEADER_TYPE(*ptr++);
  349. val |= HDMI_INFOFRAME_HEADER_VERSION(*ptr++);
  350. val |= HDMI_INFOFRAME_HEADER_LEN(*ptr++);
  351. writel(val, hdmi->regs + head_offset);
  352. /*
  353. * Each subpack contains 4 bytes
  354. * The First Bytes of the first subpacket must contain the checksum
  355. * Packet size is increase by one.
  356. */
  357. size = size - HDMI_INFOFRAME_HEADER_SIZE + 1;
  358. for (i = 0; i < size; i += sizeof(u32)) {
  359. size_t num;
  360. num = min_t(size_t, size - i, sizeof(u32));
  361. val = hdmi_infoframe_subpack(ptr, num);
  362. ptr += sizeof(u32);
  363. writel(val, hdmi->regs + pack_offset + i);
  364. }
  365. /* Enable transmission slot for updated infoframe */
  366. val = hdmi_read(hdmi, HDMI_SW_DI_CFG);
  367. val |= HDMI_IFRAME_CFG_DI_N(mode, slot);
  368. hdmi_write(hdmi, val, HDMI_SW_DI_CFG);
  369. }
  370. /*
  371. * Prepare and configure the AVI infoframe
  372. *
  373. * AVI infoframe are transmitted at least once per two video field and
  374. * contains information about HDMI transmission mode such as color space,
  375. * colorimetry, ...
  376. *
  377. * @hdmi: pointer on the hdmi internal structure
  378. *
  379. * Return negative value if error occurs
  380. */
  381. static int hdmi_avi_infoframe_config(struct sti_hdmi *hdmi)
  382. {
  383. struct drm_display_mode *mode = &hdmi->mode;
  384. struct hdmi_avi_infoframe infoframe;
  385. u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
  386. int ret;
  387. DRM_DEBUG_DRIVER("\n");
  388. ret = drm_hdmi_avi_infoframe_from_display_mode(&infoframe,
  389. hdmi->drm_connector, mode);
  390. if (ret < 0) {
  391. DRM_ERROR("failed to setup AVI infoframe: %d\n", ret);
  392. return ret;
  393. }
  394. /* fixed infoframe configuration not linked to the mode */
  395. infoframe.colorspace = hdmi->colorspace;
  396. infoframe.quantization_range = HDMI_QUANTIZATION_RANGE_DEFAULT;
  397. infoframe.colorimetry = HDMI_COLORIMETRY_NONE;
  398. ret = hdmi_avi_infoframe_pack(&infoframe, buffer, sizeof(buffer));
  399. if (ret < 0) {
  400. DRM_ERROR("failed to pack AVI infoframe: %d\n", ret);
  401. return ret;
  402. }
  403. hdmi_infoframe_write_infopack(hdmi, buffer, ret);
  404. return 0;
  405. }
  406. /*
  407. * Prepare and configure the AUDIO infoframe
  408. *
  409. * AUDIO infoframe are transmitted once per frame and
  410. * contains information about HDMI transmission mode such as audio codec,
  411. * sample size, ...
  412. *
  413. * @hdmi: pointer on the hdmi internal structure
  414. *
  415. * Return negative value if error occurs
  416. */
  417. static int hdmi_audio_infoframe_config(struct sti_hdmi *hdmi)
  418. {
  419. struct hdmi_audio_params *audio = &hdmi->audio;
  420. u8 buffer[HDMI_INFOFRAME_SIZE(AUDIO)];
  421. int ret, val;
  422. DRM_DEBUG_DRIVER("enter %s, AIF %s\n", __func__,
  423. audio->enabled ? "enable" : "disable");
  424. if (audio->enabled) {
  425. /* set audio parameters stored*/
  426. ret = hdmi_audio_infoframe_pack(&audio->cea, buffer,
  427. sizeof(buffer));
  428. if (ret < 0) {
  429. DRM_ERROR("failed to pack audio infoframe: %d\n", ret);
  430. return ret;
  431. }
  432. hdmi_infoframe_write_infopack(hdmi, buffer, ret);
  433. } else {
  434. /*disable audio info frame transmission */
  435. val = hdmi_read(hdmi, HDMI_SW_DI_CFG);
  436. val &= ~HDMI_IFRAME_CFG_DI_N(HDMI_IFRAME_MASK,
  437. HDMI_IFRAME_SLOT_AUDIO);
  438. hdmi_write(hdmi, val, HDMI_SW_DI_CFG);
  439. }
  440. return 0;
  441. }
  442. /*
  443. * Prepare and configure the VS infoframe
  444. *
  445. * Vendor Specific infoframe are transmitted once per frame and
  446. * contains vendor specific information.
  447. *
  448. * @hdmi: pointer on the hdmi internal structure
  449. *
  450. * Return negative value if error occurs
  451. */
  452. #define HDMI_VENDOR_INFOFRAME_MAX_SIZE 6
  453. static int hdmi_vendor_infoframe_config(struct sti_hdmi *hdmi)
  454. {
  455. struct drm_display_mode *mode = &hdmi->mode;
  456. struct hdmi_vendor_infoframe infoframe;
  457. u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_VENDOR_INFOFRAME_MAX_SIZE];
  458. int ret;
  459. DRM_DEBUG_DRIVER("\n");
  460. ret = drm_hdmi_vendor_infoframe_from_display_mode(&infoframe,
  461. hdmi->drm_connector,
  462. mode);
  463. if (ret < 0) {
  464. /*
  465. * Going into that statement does not means vendor infoframe
  466. * fails. It just informed us that vendor infoframe is not
  467. * needed for the selected mode. Only 4k or stereoscopic 3D
  468. * mode requires vendor infoframe. So just simply return 0.
  469. */
  470. return 0;
  471. }
  472. ret = hdmi_vendor_infoframe_pack(&infoframe, buffer, sizeof(buffer));
  473. if (ret < 0) {
  474. DRM_ERROR("failed to pack VS infoframe: %d\n", ret);
  475. return ret;
  476. }
  477. hdmi_infoframe_write_infopack(hdmi, buffer, ret);
  478. return 0;
  479. }
  480. #define HDMI_TIMEOUT_SWRESET 100 /*milliseconds */
  481. /*
  482. * Software reset of the hdmi subsystem
  483. *
  484. * @hdmi: pointer on the hdmi internal structure
  485. *
  486. */
  487. static void hdmi_swreset(struct sti_hdmi *hdmi)
  488. {
  489. u32 val;
  490. DRM_DEBUG_DRIVER("\n");
  491. /* Enable hdmi_audio clock only during hdmi reset */
  492. if (clk_prepare_enable(hdmi->clk_audio))
  493. DRM_INFO("Failed to prepare/enable hdmi_audio clk\n");
  494. /* Sw reset */
  495. hdmi->event_received = false;
  496. val = hdmi_read(hdmi, HDMI_CFG);
  497. val |= HDMI_CFG_SW_RST_EN;
  498. hdmi_write(hdmi, val, HDMI_CFG);
  499. /* Wait reset completed */
  500. wait_event_interruptible_timeout(hdmi->wait_event,
  501. hdmi->event_received,
  502. msecs_to_jiffies
  503. (HDMI_TIMEOUT_SWRESET));
  504. /*
  505. * HDMI_STA_SW_RST bit is set to '1' when SW_RST bit in HDMI_CFG is
  506. * set to '1' and clk_audio is running.
  507. */
  508. if ((hdmi_read(hdmi, HDMI_STA) & HDMI_STA_SW_RST) == 0)
  509. DRM_DEBUG_DRIVER("Warning: HDMI sw reset timeout occurs\n");
  510. val = hdmi_read(hdmi, HDMI_CFG);
  511. val &= ~HDMI_CFG_SW_RST_EN;
  512. hdmi_write(hdmi, val, HDMI_CFG);
  513. /* Disable hdmi_audio clock. Not used anymore for drm purpose */
  514. clk_disable_unprepare(hdmi->clk_audio);
  515. }
  516. #define DBGFS_PRINT_STR(str1, str2) seq_printf(s, "%-24s %s\n", str1, str2)
  517. #define DBGFS_PRINT_INT(str1, int2) seq_printf(s, "%-24s %d\n", str1, int2)
  518. #define DBGFS_DUMP(str, reg) seq_printf(s, "%s %-25s 0x%08X", str, #reg, \
  519. hdmi_read(hdmi, reg))
  520. #define DBGFS_DUMP_DI(reg, slot) DBGFS_DUMP("\n", reg(slot))
  521. static void hdmi_dbg_cfg(struct seq_file *s, int val)
  522. {
  523. int tmp;
  524. seq_putc(s, '\t');
  525. tmp = val & HDMI_CFG_HDMI_NOT_DVI;
  526. DBGFS_PRINT_STR("mode:", tmp ? "HDMI" : "DVI");
  527. seq_puts(s, "\t\t\t\t\t");
  528. tmp = val & HDMI_CFG_HDCP_EN;
  529. DBGFS_PRINT_STR("HDCP:", tmp ? "enable" : "disable");
  530. seq_puts(s, "\t\t\t\t\t");
  531. tmp = val & HDMI_CFG_ESS_NOT_OESS;
  532. DBGFS_PRINT_STR("HDCP mode:", tmp ? "ESS enable" : "OESS enable");
  533. seq_puts(s, "\t\t\t\t\t");
  534. tmp = val & HDMI_CFG_H_SYNC_POL_NEG;
  535. DBGFS_PRINT_STR("Hsync polarity:", tmp ? "inverted" : "normal");
  536. seq_puts(s, "\t\t\t\t\t");
  537. tmp = val & HDMI_CFG_V_SYNC_POL_NEG;
  538. DBGFS_PRINT_STR("Vsync polarity:", tmp ? "inverted" : "normal");
  539. seq_puts(s, "\t\t\t\t\t");
  540. tmp = val & HDMI_CFG_422_EN;
  541. DBGFS_PRINT_STR("YUV422 format:", tmp ? "enable" : "disable");
  542. }
  543. static void hdmi_dbg_sta(struct seq_file *s, int val)
  544. {
  545. int tmp;
  546. seq_putc(s, '\t');
  547. tmp = (val & HDMI_STA_DLL_LCK);
  548. DBGFS_PRINT_STR("pll:", tmp ? "locked" : "not locked");
  549. seq_puts(s, "\t\t\t\t\t");
  550. tmp = (val & HDMI_STA_HOT_PLUG);
  551. DBGFS_PRINT_STR("hdmi cable:", tmp ? "connected" : "not connected");
  552. }
  553. static void hdmi_dbg_sw_di_cfg(struct seq_file *s, int val)
  554. {
  555. int tmp;
  556. char *const en_di[] = {"no transmission",
  557. "single transmission",
  558. "once every field",
  559. "once every frame"};
  560. seq_putc(s, '\t');
  561. tmp = (val & HDMI_IFRAME_CFG_DI_N(HDMI_IFRAME_MASK, 1));
  562. DBGFS_PRINT_STR("Data island 1:", en_di[tmp]);
  563. seq_puts(s, "\t\t\t\t\t");
  564. tmp = (val & HDMI_IFRAME_CFG_DI_N(HDMI_IFRAME_MASK, 2)) >> 4;
  565. DBGFS_PRINT_STR("Data island 2:", en_di[tmp]);
  566. seq_puts(s, "\t\t\t\t\t");
  567. tmp = (val & HDMI_IFRAME_CFG_DI_N(HDMI_IFRAME_MASK, 3)) >> 8;
  568. DBGFS_PRINT_STR("Data island 3:", en_di[tmp]);
  569. seq_puts(s, "\t\t\t\t\t");
  570. tmp = (val & HDMI_IFRAME_CFG_DI_N(HDMI_IFRAME_MASK, 4)) >> 12;
  571. DBGFS_PRINT_STR("Data island 4:", en_di[tmp]);
  572. seq_puts(s, "\t\t\t\t\t");
  573. tmp = (val & HDMI_IFRAME_CFG_DI_N(HDMI_IFRAME_MASK, 5)) >> 16;
  574. DBGFS_PRINT_STR("Data island 5:", en_di[tmp]);
  575. seq_puts(s, "\t\t\t\t\t");
  576. tmp = (val & HDMI_IFRAME_CFG_DI_N(HDMI_IFRAME_MASK, 6)) >> 20;
  577. DBGFS_PRINT_STR("Data island 6:", en_di[tmp]);
  578. }
  579. static int hdmi_dbg_show(struct seq_file *s, void *data)
  580. {
  581. struct drm_info_node *node = s->private;
  582. struct sti_hdmi *hdmi = (struct sti_hdmi *)node->info_ent->data;
  583. seq_printf(s, "HDMI: (vaddr = 0x%p)", hdmi->regs);
  584. DBGFS_DUMP("\n", HDMI_CFG);
  585. hdmi_dbg_cfg(s, hdmi_read(hdmi, HDMI_CFG));
  586. DBGFS_DUMP("", HDMI_INT_EN);
  587. DBGFS_DUMP("\n", HDMI_STA);
  588. hdmi_dbg_sta(s, hdmi_read(hdmi, HDMI_STA));
  589. DBGFS_DUMP("", HDMI_ACTIVE_VID_XMIN);
  590. seq_putc(s, '\t');
  591. DBGFS_PRINT_INT("Xmin:", hdmi_read(hdmi, HDMI_ACTIVE_VID_XMIN));
  592. DBGFS_DUMP("", HDMI_ACTIVE_VID_XMAX);
  593. seq_putc(s, '\t');
  594. DBGFS_PRINT_INT("Xmax:", hdmi_read(hdmi, HDMI_ACTIVE_VID_XMAX));
  595. DBGFS_DUMP("", HDMI_ACTIVE_VID_YMIN);
  596. seq_putc(s, '\t');
  597. DBGFS_PRINT_INT("Ymin:", hdmi_read(hdmi, HDMI_ACTIVE_VID_YMIN));
  598. DBGFS_DUMP("", HDMI_ACTIVE_VID_YMAX);
  599. seq_putc(s, '\t');
  600. DBGFS_PRINT_INT("Ymax:", hdmi_read(hdmi, HDMI_ACTIVE_VID_YMAX));
  601. DBGFS_DUMP("", HDMI_SW_DI_CFG);
  602. hdmi_dbg_sw_di_cfg(s, hdmi_read(hdmi, HDMI_SW_DI_CFG));
  603. DBGFS_DUMP("\n", HDMI_AUDIO_CFG);
  604. DBGFS_DUMP("\n", HDMI_SPDIF_FIFO_STATUS);
  605. DBGFS_DUMP("\n", HDMI_AUDN);
  606. seq_printf(s, "\n AVI Infoframe (Data Island slot N=%d):",
  607. HDMI_IFRAME_SLOT_AVI);
  608. DBGFS_DUMP_DI(HDMI_SW_DI_N_HEAD_WORD, HDMI_IFRAME_SLOT_AVI);
  609. DBGFS_DUMP_DI(HDMI_SW_DI_N_PKT_WORD0, HDMI_IFRAME_SLOT_AVI);
  610. DBGFS_DUMP_DI(HDMI_SW_DI_N_PKT_WORD1, HDMI_IFRAME_SLOT_AVI);
  611. DBGFS_DUMP_DI(HDMI_SW_DI_N_PKT_WORD2, HDMI_IFRAME_SLOT_AVI);
  612. DBGFS_DUMP_DI(HDMI_SW_DI_N_PKT_WORD3, HDMI_IFRAME_SLOT_AVI);
  613. DBGFS_DUMP_DI(HDMI_SW_DI_N_PKT_WORD4, HDMI_IFRAME_SLOT_AVI);
  614. DBGFS_DUMP_DI(HDMI_SW_DI_N_PKT_WORD5, HDMI_IFRAME_SLOT_AVI);
  615. DBGFS_DUMP_DI(HDMI_SW_DI_N_PKT_WORD6, HDMI_IFRAME_SLOT_AVI);
  616. seq_printf(s, "\n\n AUDIO Infoframe (Data Island slot N=%d):",
  617. HDMI_IFRAME_SLOT_AUDIO);
  618. DBGFS_DUMP_DI(HDMI_SW_DI_N_HEAD_WORD, HDMI_IFRAME_SLOT_AUDIO);
  619. DBGFS_DUMP_DI(HDMI_SW_DI_N_PKT_WORD0, HDMI_IFRAME_SLOT_AUDIO);
  620. DBGFS_DUMP_DI(HDMI_SW_DI_N_PKT_WORD1, HDMI_IFRAME_SLOT_AUDIO);
  621. DBGFS_DUMP_DI(HDMI_SW_DI_N_PKT_WORD2, HDMI_IFRAME_SLOT_AUDIO);
  622. DBGFS_DUMP_DI(HDMI_SW_DI_N_PKT_WORD3, HDMI_IFRAME_SLOT_AUDIO);
  623. DBGFS_DUMP_DI(HDMI_SW_DI_N_PKT_WORD4, HDMI_IFRAME_SLOT_AUDIO);
  624. DBGFS_DUMP_DI(HDMI_SW_DI_N_PKT_WORD5, HDMI_IFRAME_SLOT_AUDIO);
  625. DBGFS_DUMP_DI(HDMI_SW_DI_N_PKT_WORD6, HDMI_IFRAME_SLOT_AUDIO);
  626. seq_printf(s, "\n\n VENDOR SPECIFIC Infoframe (Data Island slot N=%d):",
  627. HDMI_IFRAME_SLOT_VENDOR);
  628. DBGFS_DUMP_DI(HDMI_SW_DI_N_HEAD_WORD, HDMI_IFRAME_SLOT_VENDOR);
  629. DBGFS_DUMP_DI(HDMI_SW_DI_N_PKT_WORD0, HDMI_IFRAME_SLOT_VENDOR);
  630. DBGFS_DUMP_DI(HDMI_SW_DI_N_PKT_WORD1, HDMI_IFRAME_SLOT_VENDOR);
  631. DBGFS_DUMP_DI(HDMI_SW_DI_N_PKT_WORD2, HDMI_IFRAME_SLOT_VENDOR);
  632. DBGFS_DUMP_DI(HDMI_SW_DI_N_PKT_WORD3, HDMI_IFRAME_SLOT_VENDOR);
  633. DBGFS_DUMP_DI(HDMI_SW_DI_N_PKT_WORD4, HDMI_IFRAME_SLOT_VENDOR);
  634. DBGFS_DUMP_DI(HDMI_SW_DI_N_PKT_WORD5, HDMI_IFRAME_SLOT_VENDOR);
  635. DBGFS_DUMP_DI(HDMI_SW_DI_N_PKT_WORD6, HDMI_IFRAME_SLOT_VENDOR);
  636. seq_putc(s, '\n');
  637. return 0;
  638. }
  639. static struct drm_info_list hdmi_debugfs_files[] = {
  640. { "hdmi", hdmi_dbg_show, 0, NULL },
  641. };
  642. static void hdmi_debugfs_init(struct sti_hdmi *hdmi, struct drm_minor *minor)
  643. {
  644. unsigned int i;
  645. for (i = 0; i < ARRAY_SIZE(hdmi_debugfs_files); i++)
  646. hdmi_debugfs_files[i].data = hdmi;
  647. drm_debugfs_create_files(hdmi_debugfs_files,
  648. ARRAY_SIZE(hdmi_debugfs_files),
  649. minor->debugfs_root, minor);
  650. }
  651. static void sti_hdmi_disable(struct drm_bridge *bridge)
  652. {
  653. struct sti_hdmi *hdmi = bridge->driver_private;
  654. u32 val = hdmi_read(hdmi, HDMI_CFG);
  655. if (!hdmi->enabled)
  656. return;
  657. DRM_DEBUG_DRIVER("\n");
  658. /* Disable HDMI */
  659. val &= ~HDMI_CFG_DEVICE_EN;
  660. hdmi_write(hdmi, val, HDMI_CFG);
  661. hdmi_write(hdmi, 0xffffffff, HDMI_INT_CLR);
  662. /* Stop the phy */
  663. hdmi->phy_ops->stop(hdmi);
  664. /* Reset info frame transmission */
  665. hdmi_infoframe_reset(hdmi, HDMI_IFRAME_SLOT_AVI);
  666. hdmi_infoframe_reset(hdmi, HDMI_IFRAME_SLOT_AUDIO);
  667. hdmi_infoframe_reset(hdmi, HDMI_IFRAME_SLOT_VENDOR);
  668. /* Set the default channel data to be a dark red */
  669. hdmi_write(hdmi, 0x0000, HDMI_DFLT_CHL0_DAT);
  670. hdmi_write(hdmi, 0x0000, HDMI_DFLT_CHL1_DAT);
  671. hdmi_write(hdmi, 0x0060, HDMI_DFLT_CHL2_DAT);
  672. /* Disable/unprepare hdmi clock */
  673. clk_disable_unprepare(hdmi->clk_phy);
  674. clk_disable_unprepare(hdmi->clk_tmds);
  675. clk_disable_unprepare(hdmi->clk_pix);
  676. hdmi->enabled = false;
  677. cec_notifier_set_phys_addr(hdmi->notifier, CEC_PHYS_ADDR_INVALID);
  678. }
  679. /*
  680. * sti_hdmi_audio_get_non_coherent_n() - get N parameter for non-coherent
  681. * clocks. None-coherent clocks means that audio and TMDS clocks have not the
  682. * same source (drifts between clocks). In this case assumption is that CTS is
  683. * automatically calculated by hardware.
  684. *
  685. * @audio_fs: audio frame clock frequency in Hz
  686. *
  687. * Values computed are based on table described in HDMI specification 1.4b
  688. *
  689. * Returns n value.
  690. */
  691. static int sti_hdmi_audio_get_non_coherent_n(unsigned int audio_fs)
  692. {
  693. unsigned int n;
  694. switch (audio_fs) {
  695. case 32000:
  696. n = 4096;
  697. break;
  698. case 44100:
  699. n = 6272;
  700. break;
  701. case 48000:
  702. n = 6144;
  703. break;
  704. case 88200:
  705. n = 6272 * 2;
  706. break;
  707. case 96000:
  708. n = 6144 * 2;
  709. break;
  710. case 176400:
  711. n = 6272 * 4;
  712. break;
  713. case 192000:
  714. n = 6144 * 4;
  715. break;
  716. default:
  717. /* Not pre-defined, recommended value: 128 * fs / 1000 */
  718. n = (audio_fs * 128) / 1000;
  719. }
  720. return n;
  721. }
  722. static int hdmi_audio_configure(struct sti_hdmi *hdmi)
  723. {
  724. int audio_cfg, n;
  725. struct hdmi_audio_params *params = &hdmi->audio;
  726. struct hdmi_audio_infoframe *info = &params->cea;
  727. DRM_DEBUG_DRIVER("\n");
  728. if (!hdmi->enabled)
  729. return 0;
  730. /* update N parameter */
  731. n = sti_hdmi_audio_get_non_coherent_n(params->sample_rate);
  732. DRM_DEBUG_DRIVER("Audio rate = %d Hz, TMDS clock = %d Hz, n = %d\n",
  733. params->sample_rate, hdmi->mode.clock * 1000, n);
  734. hdmi_write(hdmi, n, HDMI_AUDN);
  735. /* update HDMI registers according to configuration */
  736. audio_cfg = HDMI_AUD_CFG_SPDIF_DIV_2 | HDMI_AUD_CFG_DTS_INVALID |
  737. HDMI_AUD_CFG_ONE_BIT_INVALID;
  738. switch (info->channels) {
  739. case 8:
  740. audio_cfg |= HDMI_AUD_CFG_CH78_VALID;
  741. fallthrough;
  742. case 6:
  743. audio_cfg |= HDMI_AUD_CFG_CH56_VALID;
  744. fallthrough;
  745. case 4:
  746. audio_cfg |= HDMI_AUD_CFG_CH34_VALID | HDMI_AUD_CFG_8CH;
  747. fallthrough;
  748. case 2:
  749. audio_cfg |= HDMI_AUD_CFG_CH12_VALID;
  750. break;
  751. default:
  752. DRM_ERROR("ERROR: Unsupported number of channels (%d)!\n",
  753. info->channels);
  754. return -EINVAL;
  755. }
  756. hdmi_write(hdmi, audio_cfg, HDMI_AUDIO_CFG);
  757. return hdmi_audio_infoframe_config(hdmi);
  758. }
  759. static void sti_hdmi_pre_enable(struct drm_bridge *bridge)
  760. {
  761. struct sti_hdmi *hdmi = bridge->driver_private;
  762. DRM_DEBUG_DRIVER("\n");
  763. if (hdmi->enabled)
  764. return;
  765. /* Prepare/enable clocks */
  766. if (clk_prepare_enable(hdmi->clk_pix))
  767. DRM_ERROR("Failed to prepare/enable hdmi_pix clk\n");
  768. if (clk_prepare_enable(hdmi->clk_tmds))
  769. DRM_ERROR("Failed to prepare/enable hdmi_tmds clk\n");
  770. if (clk_prepare_enable(hdmi->clk_phy))
  771. DRM_ERROR("Failed to prepare/enable hdmi_rejection_pll clk\n");
  772. hdmi->enabled = true;
  773. /* Program hdmi serializer and start phy */
  774. if (!hdmi->phy_ops->start(hdmi)) {
  775. DRM_ERROR("Unable to start hdmi phy\n");
  776. return;
  777. }
  778. /* Program hdmi active area */
  779. hdmi_active_area(hdmi);
  780. /* Enable working interrupts */
  781. hdmi_write(hdmi, HDMI_WORKING_INT, HDMI_INT_EN);
  782. /* Program hdmi config */
  783. hdmi_config(hdmi);
  784. /* Program AVI infoframe */
  785. if (hdmi_avi_infoframe_config(hdmi))
  786. DRM_ERROR("Unable to configure AVI infoframe\n");
  787. if (hdmi->audio.enabled) {
  788. if (hdmi_audio_configure(hdmi))
  789. DRM_ERROR("Unable to configure audio\n");
  790. } else {
  791. hdmi_audio_infoframe_config(hdmi);
  792. }
  793. /* Program VS infoframe */
  794. if (hdmi_vendor_infoframe_config(hdmi))
  795. DRM_ERROR("Unable to configure VS infoframe\n");
  796. /* Sw reset */
  797. hdmi_swreset(hdmi);
  798. }
  799. static void sti_hdmi_set_mode(struct drm_bridge *bridge,
  800. const struct drm_display_mode *mode,
  801. const struct drm_display_mode *adjusted_mode)
  802. {
  803. struct sti_hdmi *hdmi = bridge->driver_private;
  804. int ret;
  805. DRM_DEBUG_DRIVER("\n");
  806. /* Copy the drm display mode in the connector local structure */
  807. drm_mode_copy(&hdmi->mode, mode);
  808. /* Update clock framerate according to the selected mode */
  809. ret = clk_set_rate(hdmi->clk_pix, mode->clock * 1000);
  810. if (ret < 0) {
  811. DRM_ERROR("Cannot set rate (%dHz) for hdmi_pix clk\n",
  812. mode->clock * 1000);
  813. return;
  814. }
  815. ret = clk_set_rate(hdmi->clk_phy, mode->clock * 1000);
  816. if (ret < 0) {
  817. DRM_ERROR("Cannot set rate (%dHz) for hdmi_rejection_pll clk\n",
  818. mode->clock * 1000);
  819. return;
  820. }
  821. }
  822. static void sti_hdmi_bridge_nope(struct drm_bridge *bridge)
  823. {
  824. /* do nothing */
  825. }
  826. static const struct drm_bridge_funcs sti_hdmi_bridge_funcs = {
  827. .pre_enable = sti_hdmi_pre_enable,
  828. .enable = sti_hdmi_bridge_nope,
  829. .disable = sti_hdmi_disable,
  830. .post_disable = sti_hdmi_bridge_nope,
  831. .mode_set = sti_hdmi_set_mode,
  832. };
  833. static int sti_hdmi_connector_get_modes(struct drm_connector *connector)
  834. {
  835. const struct drm_display_info *info = &connector->display_info;
  836. struct sti_hdmi_connector *hdmi_connector
  837. = to_sti_hdmi_connector(connector);
  838. struct sti_hdmi *hdmi = hdmi_connector->hdmi;
  839. const struct drm_edid *drm_edid;
  840. int count;
  841. DRM_DEBUG_DRIVER("\n");
  842. drm_edid = drm_edid_read(connector);
  843. drm_edid_connector_update(connector, drm_edid);
  844. cec_notifier_set_phys_addr(hdmi->notifier,
  845. connector->display_info.source_physical_address);
  846. if (!drm_edid)
  847. goto fail;
  848. count = drm_edid_connector_add_modes(connector);
  849. DRM_DEBUG_KMS("%s : %dx%d cm\n",
  850. info->is_hdmi ? "hdmi monitor" : "dvi monitor",
  851. info->width_mm / 10, info->height_mm / 10);
  852. drm_edid_free(drm_edid);
  853. return count;
  854. fail:
  855. DRM_ERROR("Can't read HDMI EDID\n");
  856. return 0;
  857. }
  858. #define CLK_TOLERANCE_HZ 50
  859. static enum drm_mode_status
  860. sti_hdmi_connector_mode_valid(struct drm_connector *connector,
  861. struct drm_display_mode *mode)
  862. {
  863. int target = mode->clock * 1000;
  864. int target_min = target - CLK_TOLERANCE_HZ;
  865. int target_max = target + CLK_TOLERANCE_HZ;
  866. int result;
  867. struct sti_hdmi_connector *hdmi_connector
  868. = to_sti_hdmi_connector(connector);
  869. struct sti_hdmi *hdmi = hdmi_connector->hdmi;
  870. result = clk_round_rate(hdmi->clk_pix, target);
  871. DRM_DEBUG_DRIVER("target rate = %d => available rate = %d\n",
  872. target, result);
  873. if ((result < target_min) || (result > target_max)) {
  874. DRM_DEBUG_DRIVER("hdmi pixclk=%d not supported\n", target);
  875. return MODE_BAD;
  876. }
  877. return MODE_OK;
  878. }
  879. static const
  880. struct drm_connector_helper_funcs sti_hdmi_connector_helper_funcs = {
  881. .get_modes = sti_hdmi_connector_get_modes,
  882. .mode_valid = sti_hdmi_connector_mode_valid,
  883. };
  884. /* get detection status of display device */
  885. static enum drm_connector_status
  886. sti_hdmi_connector_detect(struct drm_connector *connector, bool force)
  887. {
  888. struct sti_hdmi_connector *hdmi_connector
  889. = to_sti_hdmi_connector(connector);
  890. struct sti_hdmi *hdmi = hdmi_connector->hdmi;
  891. DRM_DEBUG_DRIVER("\n");
  892. if (hdmi->hpd) {
  893. DRM_DEBUG_DRIVER("hdmi cable connected\n");
  894. return connector_status_connected;
  895. }
  896. DRM_DEBUG_DRIVER("hdmi cable disconnected\n");
  897. cec_notifier_set_phys_addr(hdmi->notifier, CEC_PHYS_ADDR_INVALID);
  898. return connector_status_disconnected;
  899. }
  900. static void sti_hdmi_connector_init_property(struct drm_device *drm_dev,
  901. struct drm_connector *connector)
  902. {
  903. struct sti_hdmi_connector *hdmi_connector
  904. = to_sti_hdmi_connector(connector);
  905. struct sti_hdmi *hdmi = hdmi_connector->hdmi;
  906. struct drm_property *prop;
  907. /* colorspace property */
  908. hdmi->colorspace = DEFAULT_COLORSPACE_MODE;
  909. prop = drm_property_create_enum(drm_dev, 0, "colorspace",
  910. colorspace_mode_names,
  911. ARRAY_SIZE(colorspace_mode_names));
  912. if (!prop) {
  913. DRM_ERROR("fails to create colorspace property\n");
  914. return;
  915. }
  916. hdmi_connector->colorspace_property = prop;
  917. drm_object_attach_property(&connector->base, prop, hdmi->colorspace);
  918. }
  919. static int
  920. sti_hdmi_connector_set_property(struct drm_connector *connector,
  921. struct drm_connector_state *state,
  922. struct drm_property *property,
  923. uint64_t val)
  924. {
  925. struct sti_hdmi_connector *hdmi_connector
  926. = to_sti_hdmi_connector(connector);
  927. struct sti_hdmi *hdmi = hdmi_connector->hdmi;
  928. if (property == hdmi_connector->colorspace_property) {
  929. hdmi->colorspace = val;
  930. return 0;
  931. }
  932. DRM_ERROR("failed to set hdmi connector property\n");
  933. return -EINVAL;
  934. }
  935. static int
  936. sti_hdmi_connector_get_property(struct drm_connector *connector,
  937. const struct drm_connector_state *state,
  938. struct drm_property *property,
  939. uint64_t *val)
  940. {
  941. struct sti_hdmi_connector *hdmi_connector
  942. = to_sti_hdmi_connector(connector);
  943. struct sti_hdmi *hdmi = hdmi_connector->hdmi;
  944. if (property == hdmi_connector->colorspace_property) {
  945. *val = hdmi->colorspace;
  946. return 0;
  947. }
  948. DRM_ERROR("failed to get hdmi connector property\n");
  949. return -EINVAL;
  950. }
  951. static int sti_hdmi_late_register(struct drm_connector *connector)
  952. {
  953. struct sti_hdmi_connector *hdmi_connector
  954. = to_sti_hdmi_connector(connector);
  955. struct sti_hdmi *hdmi = hdmi_connector->hdmi;
  956. hdmi_debugfs_init(hdmi, hdmi->drm_dev->primary);
  957. return 0;
  958. }
  959. static const struct drm_connector_funcs sti_hdmi_connector_funcs = {
  960. .fill_modes = drm_helper_probe_single_connector_modes,
  961. .detect = sti_hdmi_connector_detect,
  962. .destroy = drm_connector_cleanup,
  963. .reset = drm_atomic_helper_connector_reset,
  964. .atomic_set_property = sti_hdmi_connector_set_property,
  965. .atomic_get_property = sti_hdmi_connector_get_property,
  966. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  967. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  968. .late_register = sti_hdmi_late_register,
  969. };
  970. static struct drm_encoder *sti_hdmi_find_encoder(struct drm_device *dev)
  971. {
  972. struct drm_encoder *encoder;
  973. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  974. if (encoder->encoder_type == DRM_MODE_ENCODER_TMDS)
  975. return encoder;
  976. }
  977. return NULL;
  978. }
  979. static void hdmi_audio_shutdown(struct device *dev, void *data)
  980. {
  981. struct sti_hdmi *hdmi = dev_get_drvdata(dev);
  982. int audio_cfg;
  983. DRM_DEBUG_DRIVER("\n");
  984. /* disable audio */
  985. audio_cfg = HDMI_AUD_CFG_SPDIF_DIV_2 | HDMI_AUD_CFG_DTS_INVALID |
  986. HDMI_AUD_CFG_ONE_BIT_INVALID;
  987. hdmi_write(hdmi, audio_cfg, HDMI_AUDIO_CFG);
  988. hdmi->audio.enabled = false;
  989. hdmi_audio_infoframe_config(hdmi);
  990. }
  991. static int hdmi_audio_hw_params(struct device *dev,
  992. void *data,
  993. struct hdmi_codec_daifmt *daifmt,
  994. struct hdmi_codec_params *params)
  995. {
  996. struct sti_hdmi *hdmi = dev_get_drvdata(dev);
  997. int ret;
  998. DRM_DEBUG_DRIVER("\n");
  999. if ((daifmt->fmt != HDMI_I2S) || daifmt->bit_clk_inv ||
  1000. daifmt->frame_clk_inv || daifmt->bit_clk_provider ||
  1001. daifmt->frame_clk_provider) {
  1002. dev_err(dev, "%s: Bad flags %d %d %d %d\n", __func__,
  1003. daifmt->bit_clk_inv, daifmt->frame_clk_inv,
  1004. daifmt->bit_clk_provider,
  1005. daifmt->frame_clk_provider);
  1006. return -EINVAL;
  1007. }
  1008. hdmi->audio.sample_width = params->sample_width;
  1009. hdmi->audio.sample_rate = params->sample_rate;
  1010. hdmi->audio.cea = params->cea;
  1011. hdmi->audio.enabled = true;
  1012. ret = hdmi_audio_configure(hdmi);
  1013. if (ret < 0)
  1014. return ret;
  1015. return 0;
  1016. }
  1017. static int hdmi_audio_mute(struct device *dev, void *data,
  1018. bool enable, int direction)
  1019. {
  1020. struct sti_hdmi *hdmi = dev_get_drvdata(dev);
  1021. DRM_DEBUG_DRIVER("%s\n", enable ? "enable" : "disable");
  1022. if (enable)
  1023. hdmi_write(hdmi, HDMI_SAMPLE_FLAT_ALL, HDMI_SAMPLE_FLAT_MASK);
  1024. else
  1025. hdmi_write(hdmi, HDMI_SAMPLE_FLAT_NO, HDMI_SAMPLE_FLAT_MASK);
  1026. return 0;
  1027. }
  1028. static int hdmi_audio_get_eld(struct device *dev, void *data, uint8_t *buf, size_t len)
  1029. {
  1030. struct sti_hdmi *hdmi = dev_get_drvdata(dev);
  1031. struct drm_connector *connector = hdmi->drm_connector;
  1032. DRM_DEBUG_DRIVER("\n");
  1033. mutex_lock(&connector->eld_mutex);
  1034. memcpy(buf, connector->eld, min(sizeof(connector->eld), len));
  1035. mutex_unlock(&connector->eld_mutex);
  1036. return 0;
  1037. }
  1038. static const struct hdmi_codec_ops audio_codec_ops = {
  1039. .hw_params = hdmi_audio_hw_params,
  1040. .audio_shutdown = hdmi_audio_shutdown,
  1041. .mute_stream = hdmi_audio_mute,
  1042. .get_eld = hdmi_audio_get_eld,
  1043. .no_capture_mute = 1,
  1044. };
  1045. static int sti_hdmi_register_audio_driver(struct device *dev,
  1046. struct sti_hdmi *hdmi)
  1047. {
  1048. struct hdmi_codec_pdata codec_data = {
  1049. .ops = &audio_codec_ops,
  1050. .max_i2s_channels = 8,
  1051. .i2s = 1,
  1052. };
  1053. DRM_DEBUG_DRIVER("\n");
  1054. hdmi->audio.enabled = false;
  1055. hdmi->audio_pdev = platform_device_register_data(
  1056. dev, HDMI_CODEC_DRV_NAME, PLATFORM_DEVID_AUTO,
  1057. &codec_data, sizeof(codec_data));
  1058. if (IS_ERR(hdmi->audio_pdev))
  1059. return PTR_ERR(hdmi->audio_pdev);
  1060. DRM_INFO("%s Driver bound %s\n", HDMI_CODEC_DRV_NAME, dev_name(dev));
  1061. return 0;
  1062. }
  1063. static int sti_hdmi_bind(struct device *dev, struct device *master, void *data)
  1064. {
  1065. struct sti_hdmi *hdmi = dev_get_drvdata(dev);
  1066. struct drm_device *drm_dev = data;
  1067. struct drm_encoder *encoder;
  1068. struct sti_hdmi_connector *connector;
  1069. struct cec_connector_info conn_info;
  1070. struct drm_connector *drm_connector;
  1071. struct drm_bridge *bridge;
  1072. int err;
  1073. /* Set the drm device handle */
  1074. hdmi->drm_dev = drm_dev;
  1075. encoder = sti_hdmi_find_encoder(drm_dev);
  1076. if (!encoder)
  1077. return -EINVAL;
  1078. connector = devm_kzalloc(dev, sizeof(*connector), GFP_KERNEL);
  1079. if (!connector)
  1080. return -EINVAL;
  1081. connector->hdmi = hdmi;
  1082. bridge = devm_kzalloc(dev, sizeof(*bridge), GFP_KERNEL);
  1083. if (!bridge)
  1084. return -EINVAL;
  1085. bridge->driver_private = hdmi;
  1086. bridge->funcs = &sti_hdmi_bridge_funcs;
  1087. drm_bridge_attach(encoder, bridge, NULL, 0);
  1088. connector->encoder = encoder;
  1089. drm_connector = (struct drm_connector *)connector;
  1090. drm_connector->polled = DRM_CONNECTOR_POLL_HPD;
  1091. drm_connector_init_with_ddc(drm_dev, drm_connector,
  1092. &sti_hdmi_connector_funcs,
  1093. DRM_MODE_CONNECTOR_HDMIA,
  1094. hdmi->ddc_adapt);
  1095. drm_connector_helper_add(drm_connector,
  1096. &sti_hdmi_connector_helper_funcs);
  1097. /* initialise property */
  1098. sti_hdmi_connector_init_property(drm_dev, drm_connector);
  1099. hdmi->drm_connector = drm_connector;
  1100. err = drm_connector_attach_encoder(drm_connector, encoder);
  1101. if (err) {
  1102. DRM_ERROR("Failed to attach a connector to a encoder\n");
  1103. goto err_sysfs;
  1104. }
  1105. err = sti_hdmi_register_audio_driver(dev, hdmi);
  1106. if (err) {
  1107. DRM_ERROR("Failed to attach an audio codec\n");
  1108. goto err_sysfs;
  1109. }
  1110. /* Initialize audio infoframe */
  1111. err = hdmi_audio_infoframe_init(&hdmi->audio.cea);
  1112. if (err) {
  1113. DRM_ERROR("Failed to init audio infoframe\n");
  1114. goto err_sysfs;
  1115. }
  1116. cec_fill_conn_info_from_drm(&conn_info, drm_connector);
  1117. hdmi->notifier = cec_notifier_conn_register(&hdmi->dev, NULL,
  1118. &conn_info);
  1119. if (!hdmi->notifier) {
  1120. hdmi->drm_connector = NULL;
  1121. return -ENOMEM;
  1122. }
  1123. /* Enable default interrupts */
  1124. hdmi_write(hdmi, HDMI_DEFAULT_INT, HDMI_INT_EN);
  1125. return 0;
  1126. err_sysfs:
  1127. hdmi->drm_connector = NULL;
  1128. return -EINVAL;
  1129. }
  1130. static void sti_hdmi_unbind(struct device *dev,
  1131. struct device *master, void *data)
  1132. {
  1133. struct sti_hdmi *hdmi = dev_get_drvdata(dev);
  1134. cec_notifier_conn_unregister(hdmi->notifier);
  1135. }
  1136. static const struct component_ops sti_hdmi_ops = {
  1137. .bind = sti_hdmi_bind,
  1138. .unbind = sti_hdmi_unbind,
  1139. };
  1140. static const struct of_device_id hdmi_of_match[] = {
  1141. {
  1142. .compatible = "st,stih407-hdmi",
  1143. .data = &tx3g4c28phy_ops,
  1144. }, {
  1145. /* end node */
  1146. }
  1147. };
  1148. MODULE_DEVICE_TABLE(of, hdmi_of_match);
  1149. static int sti_hdmi_probe(struct platform_device *pdev)
  1150. {
  1151. struct device *dev = &pdev->dev;
  1152. struct sti_hdmi *hdmi;
  1153. struct device_node *np = dev->of_node;
  1154. struct resource *res;
  1155. struct device_node *ddc;
  1156. int ret;
  1157. DRM_INFO("%s\n", __func__);
  1158. hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
  1159. if (!hdmi)
  1160. return -ENOMEM;
  1161. ddc = of_parse_phandle(pdev->dev.of_node, "ddc", 0);
  1162. if (ddc) {
  1163. hdmi->ddc_adapt = of_get_i2c_adapter_by_node(ddc);
  1164. of_node_put(ddc);
  1165. if (!hdmi->ddc_adapt)
  1166. return -EPROBE_DEFER;
  1167. }
  1168. hdmi->dev = pdev->dev;
  1169. /* Get resources */
  1170. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hdmi-reg");
  1171. if (!res) {
  1172. DRM_ERROR("Invalid hdmi resource\n");
  1173. ret = -ENOMEM;
  1174. goto release_adapter;
  1175. }
  1176. hdmi->regs = devm_ioremap(dev, res->start, resource_size(res));
  1177. if (!hdmi->regs) {
  1178. ret = -ENOMEM;
  1179. goto release_adapter;
  1180. }
  1181. hdmi->phy_ops = (struct hdmi_phy_ops *)
  1182. of_match_node(hdmi_of_match, np)->data;
  1183. /* Get clock resources */
  1184. hdmi->clk_pix = devm_clk_get(dev, "pix");
  1185. if (IS_ERR(hdmi->clk_pix)) {
  1186. DRM_ERROR("Cannot get hdmi_pix clock\n");
  1187. ret = PTR_ERR(hdmi->clk_pix);
  1188. goto release_adapter;
  1189. }
  1190. hdmi->clk_tmds = devm_clk_get(dev, "tmds");
  1191. if (IS_ERR(hdmi->clk_tmds)) {
  1192. DRM_ERROR("Cannot get hdmi_tmds clock\n");
  1193. ret = PTR_ERR(hdmi->clk_tmds);
  1194. goto release_adapter;
  1195. }
  1196. hdmi->clk_phy = devm_clk_get(dev, "phy");
  1197. if (IS_ERR(hdmi->clk_phy)) {
  1198. DRM_ERROR("Cannot get hdmi_phy clock\n");
  1199. ret = PTR_ERR(hdmi->clk_phy);
  1200. goto release_adapter;
  1201. }
  1202. hdmi->clk_audio = devm_clk_get(dev, "audio");
  1203. if (IS_ERR(hdmi->clk_audio)) {
  1204. DRM_ERROR("Cannot get hdmi_audio clock\n");
  1205. ret = PTR_ERR(hdmi->clk_audio);
  1206. goto release_adapter;
  1207. }
  1208. hdmi->hpd = readl(hdmi->regs + HDMI_STA) & HDMI_STA_HOT_PLUG;
  1209. init_waitqueue_head(&hdmi->wait_event);
  1210. hdmi->irq = platform_get_irq_byname(pdev, "irq");
  1211. if (hdmi->irq < 0) {
  1212. DRM_ERROR("Cannot get HDMI irq\n");
  1213. ret = hdmi->irq;
  1214. goto release_adapter;
  1215. }
  1216. ret = devm_request_threaded_irq(dev, hdmi->irq, hdmi_irq,
  1217. hdmi_irq_thread, IRQF_ONESHOT, dev_name(dev), hdmi);
  1218. if (ret) {
  1219. DRM_ERROR("Failed to register HDMI interrupt\n");
  1220. goto release_adapter;
  1221. }
  1222. hdmi->reset = devm_reset_control_get(dev, "hdmi");
  1223. /* Take hdmi out of reset */
  1224. if (!IS_ERR(hdmi->reset))
  1225. reset_control_deassert(hdmi->reset);
  1226. platform_set_drvdata(pdev, hdmi);
  1227. return component_add(&pdev->dev, &sti_hdmi_ops);
  1228. release_adapter:
  1229. i2c_put_adapter(hdmi->ddc_adapt);
  1230. return ret;
  1231. }
  1232. static void sti_hdmi_remove(struct platform_device *pdev)
  1233. {
  1234. struct sti_hdmi *hdmi = dev_get_drvdata(&pdev->dev);
  1235. i2c_put_adapter(hdmi->ddc_adapt);
  1236. if (hdmi->audio_pdev)
  1237. platform_device_unregister(hdmi->audio_pdev);
  1238. component_del(&pdev->dev, &sti_hdmi_ops);
  1239. }
  1240. struct platform_driver sti_hdmi_driver = {
  1241. .driver = {
  1242. .name = "sti-hdmi",
  1243. .of_match_table = hdmi_of_match,
  1244. },
  1245. .probe = sti_hdmi_probe,
  1246. .remove_new = sti_hdmi_remove,
  1247. };
  1248. MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
  1249. MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
  1250. MODULE_LICENSE("GPL");