sti_hdmi.c 41 KB

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