sti_hdmi.c 41 KB

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