sti_hdmi.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  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. static void sti_hdmi_pre_enable(struct drm_bridge *bridge)
  681. {
  682. struct sti_hdmi *hdmi = bridge->driver_private;
  683. DRM_DEBUG_DRIVER("\n");
  684. if (hdmi->enabled)
  685. return;
  686. /* Prepare/enable clocks */
  687. if (clk_prepare_enable(hdmi->clk_pix))
  688. DRM_ERROR("Failed to prepare/enable hdmi_pix clk\n");
  689. if (clk_prepare_enable(hdmi->clk_tmds))
  690. DRM_ERROR("Failed to prepare/enable hdmi_tmds clk\n");
  691. if (clk_prepare_enable(hdmi->clk_phy))
  692. DRM_ERROR("Failed to prepare/enable hdmi_rejec_pll clk\n");
  693. hdmi->enabled = true;
  694. /* Program hdmi serializer and start phy */
  695. if (!hdmi->phy_ops->start(hdmi)) {
  696. DRM_ERROR("Unable to start hdmi phy\n");
  697. return;
  698. }
  699. /* Program hdmi active area */
  700. hdmi_active_area(hdmi);
  701. /* Enable working interrupts */
  702. hdmi_write(hdmi, HDMI_WORKING_INT, HDMI_INT_EN);
  703. /* Program hdmi config */
  704. hdmi_config(hdmi);
  705. /* Program AVI infoframe */
  706. if (hdmi_avi_infoframe_config(hdmi))
  707. DRM_ERROR("Unable to configure AVI infoframe\n");
  708. /* Program AUDIO infoframe */
  709. if (hdmi_audio_infoframe_config(hdmi))
  710. DRM_ERROR("Unable to configure AUDIO infoframe\n");
  711. /* Program VS infoframe */
  712. if (hdmi_vendor_infoframe_config(hdmi))
  713. DRM_ERROR("Unable to configure VS infoframe\n");
  714. /* Sw reset */
  715. hdmi_swreset(hdmi);
  716. }
  717. static void sti_hdmi_set_mode(struct drm_bridge *bridge,
  718. struct drm_display_mode *mode,
  719. struct drm_display_mode *adjusted_mode)
  720. {
  721. struct sti_hdmi *hdmi = bridge->driver_private;
  722. int ret;
  723. DRM_DEBUG_DRIVER("\n");
  724. /* Copy the drm display mode in the connector local structure */
  725. memcpy(&hdmi->mode, mode, sizeof(struct drm_display_mode));
  726. /* Update clock framerate according to the selected mode */
  727. ret = clk_set_rate(hdmi->clk_pix, mode->clock * 1000);
  728. if (ret < 0) {
  729. DRM_ERROR("Cannot set rate (%dHz) for hdmi_pix clk\n",
  730. mode->clock * 1000);
  731. return;
  732. }
  733. ret = clk_set_rate(hdmi->clk_phy, mode->clock * 1000);
  734. if (ret < 0) {
  735. DRM_ERROR("Cannot set rate (%dHz) for hdmi_rejection_pll clk\n",
  736. mode->clock * 1000);
  737. return;
  738. }
  739. }
  740. static void sti_hdmi_bridge_nope(struct drm_bridge *bridge)
  741. {
  742. /* do nothing */
  743. }
  744. static const struct drm_bridge_funcs sti_hdmi_bridge_funcs = {
  745. .pre_enable = sti_hdmi_pre_enable,
  746. .enable = sti_hdmi_bridge_nope,
  747. .disable = sti_hdmi_disable,
  748. .post_disable = sti_hdmi_bridge_nope,
  749. .mode_set = sti_hdmi_set_mode,
  750. };
  751. static int sti_hdmi_connector_get_modes(struct drm_connector *connector)
  752. {
  753. struct sti_hdmi_connector *hdmi_connector
  754. = to_sti_hdmi_connector(connector);
  755. struct sti_hdmi *hdmi = hdmi_connector->hdmi;
  756. struct edid *edid;
  757. int count;
  758. DRM_DEBUG_DRIVER("\n");
  759. edid = drm_get_edid(connector, hdmi->ddc_adapt);
  760. if (!edid)
  761. goto fail;
  762. count = drm_add_edid_modes(connector, edid);
  763. drm_mode_connector_update_edid_property(connector, edid);
  764. drm_edid_to_eld(connector, edid);
  765. kfree(edid);
  766. return count;
  767. fail:
  768. DRM_ERROR("Can't read HDMI EDID\n");
  769. return 0;
  770. }
  771. #define CLK_TOLERANCE_HZ 50
  772. static int sti_hdmi_connector_mode_valid(struct drm_connector *connector,
  773. struct drm_display_mode *mode)
  774. {
  775. int target = mode->clock * 1000;
  776. int target_min = target - CLK_TOLERANCE_HZ;
  777. int target_max = target + CLK_TOLERANCE_HZ;
  778. int result;
  779. struct sti_hdmi_connector *hdmi_connector
  780. = to_sti_hdmi_connector(connector);
  781. struct sti_hdmi *hdmi = hdmi_connector->hdmi;
  782. result = clk_round_rate(hdmi->clk_pix, target);
  783. DRM_DEBUG_DRIVER("target rate = %d => available rate = %d\n",
  784. target, result);
  785. if ((result < target_min) || (result > target_max)) {
  786. DRM_DEBUG_DRIVER("hdmi pixclk=%d not supported\n", target);
  787. return MODE_BAD;
  788. }
  789. return MODE_OK;
  790. }
  791. static const
  792. struct drm_connector_helper_funcs sti_hdmi_connector_helper_funcs = {
  793. .get_modes = sti_hdmi_connector_get_modes,
  794. .mode_valid = sti_hdmi_connector_mode_valid,
  795. };
  796. /* get detection status of display device */
  797. static enum drm_connector_status
  798. sti_hdmi_connector_detect(struct drm_connector *connector, bool force)
  799. {
  800. struct sti_hdmi_connector *hdmi_connector
  801. = to_sti_hdmi_connector(connector);
  802. struct sti_hdmi *hdmi = hdmi_connector->hdmi;
  803. DRM_DEBUG_DRIVER("\n");
  804. if (hdmi->hpd) {
  805. DRM_DEBUG_DRIVER("hdmi cable connected\n");
  806. return connector_status_connected;
  807. }
  808. DRM_DEBUG_DRIVER("hdmi cable disconnected\n");
  809. return connector_status_disconnected;
  810. }
  811. static void sti_hdmi_connector_init_property(struct drm_device *drm_dev,
  812. struct drm_connector *connector)
  813. {
  814. struct sti_hdmi_connector *hdmi_connector
  815. = to_sti_hdmi_connector(connector);
  816. struct sti_hdmi *hdmi = hdmi_connector->hdmi;
  817. struct drm_property *prop;
  818. /* colorspace property */
  819. hdmi->colorspace = DEFAULT_COLORSPACE_MODE;
  820. prop = drm_property_create_enum(drm_dev, 0, "colorspace",
  821. colorspace_mode_names,
  822. ARRAY_SIZE(colorspace_mode_names));
  823. if (!prop) {
  824. DRM_ERROR("fails to create colorspace property\n");
  825. return;
  826. }
  827. hdmi_connector->colorspace_property = prop;
  828. drm_object_attach_property(&connector->base, prop, hdmi->colorspace);
  829. /* hdmi_mode property */
  830. hdmi->hdmi_mode = DEFAULT_HDMI_MODE;
  831. prop = drm_property_create_enum(drm_dev, 0, "hdmi_mode",
  832. hdmi_mode_names,
  833. ARRAY_SIZE(hdmi_mode_names));
  834. if (!prop) {
  835. DRM_ERROR("fails to create colorspace property\n");
  836. return;
  837. }
  838. hdmi_connector->hdmi_mode_property = prop;
  839. drm_object_attach_property(&connector->base, prop, hdmi->hdmi_mode);
  840. }
  841. static int
  842. sti_hdmi_connector_set_property(struct drm_connector *connector,
  843. struct drm_connector_state *state,
  844. struct drm_property *property,
  845. uint64_t val)
  846. {
  847. struct sti_hdmi_connector *hdmi_connector
  848. = to_sti_hdmi_connector(connector);
  849. struct sti_hdmi *hdmi = hdmi_connector->hdmi;
  850. if (property == hdmi_connector->colorspace_property) {
  851. hdmi->colorspace = val;
  852. return 0;
  853. }
  854. if (property == hdmi_connector->hdmi_mode_property) {
  855. hdmi->hdmi_mode = val;
  856. return 0;
  857. }
  858. DRM_ERROR("failed to set hdmi connector property\n");
  859. return -EINVAL;
  860. }
  861. static int
  862. sti_hdmi_connector_get_property(struct drm_connector *connector,
  863. const struct drm_connector_state *state,
  864. struct drm_property *property,
  865. uint64_t *val)
  866. {
  867. struct sti_hdmi_connector *hdmi_connector
  868. = to_sti_hdmi_connector(connector);
  869. struct sti_hdmi *hdmi = hdmi_connector->hdmi;
  870. if (property == hdmi_connector->colorspace_property) {
  871. *val = hdmi->colorspace;
  872. return 0;
  873. }
  874. if (property == hdmi_connector->hdmi_mode_property) {
  875. *val = hdmi->hdmi_mode;
  876. return 0;
  877. }
  878. DRM_ERROR("failed to get hdmi connector property\n");
  879. return -EINVAL;
  880. }
  881. static int sti_hdmi_late_register(struct drm_connector *connector)
  882. {
  883. struct sti_hdmi_connector *hdmi_connector
  884. = to_sti_hdmi_connector(connector);
  885. struct sti_hdmi *hdmi = hdmi_connector->hdmi;
  886. if (hdmi_debugfs_init(hdmi, hdmi->drm_dev->primary)) {
  887. DRM_ERROR("HDMI debugfs setup failed\n");
  888. return -EINVAL;
  889. }
  890. return 0;
  891. }
  892. static const struct drm_connector_funcs sti_hdmi_connector_funcs = {
  893. .dpms = drm_atomic_helper_connector_dpms,
  894. .fill_modes = drm_helper_probe_single_connector_modes,
  895. .detect = sti_hdmi_connector_detect,
  896. .destroy = drm_connector_cleanup,
  897. .reset = drm_atomic_helper_connector_reset,
  898. .set_property = drm_atomic_helper_connector_set_property,
  899. .atomic_set_property = sti_hdmi_connector_set_property,
  900. .atomic_get_property = sti_hdmi_connector_get_property,
  901. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  902. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  903. .late_register = sti_hdmi_late_register,
  904. };
  905. static struct drm_encoder *sti_hdmi_find_encoder(struct drm_device *dev)
  906. {
  907. struct drm_encoder *encoder;
  908. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  909. if (encoder->encoder_type == DRM_MODE_ENCODER_TMDS)
  910. return encoder;
  911. }
  912. return NULL;
  913. }
  914. /**
  915. * sti_hdmi_audio_get_non_coherent_n() - get N parameter for non-coherent
  916. * clocks. None-coherent clocks means that audio and TMDS clocks have not the
  917. * same source (drifts between clocks). In this case assumption is that CTS is
  918. * automatically calculated by hardware.
  919. *
  920. * @audio_fs: audio frame clock frequency in Hz
  921. *
  922. * Values computed are based on table described in HDMI specification 1.4b
  923. *
  924. * Returns n value.
  925. */
  926. static int sti_hdmi_audio_get_non_coherent_n(unsigned int audio_fs)
  927. {
  928. unsigned int n;
  929. switch (audio_fs) {
  930. case 32000:
  931. n = 4096;
  932. break;
  933. case 44100:
  934. n = 6272;
  935. break;
  936. case 48000:
  937. n = 6144;
  938. break;
  939. case 88200:
  940. n = 6272 * 2;
  941. break;
  942. case 96000:
  943. n = 6144 * 2;
  944. break;
  945. case 176400:
  946. n = 6272 * 4;
  947. break;
  948. case 192000:
  949. n = 6144 * 4;
  950. break;
  951. default:
  952. /* Not pre-defined, recommended value: 128 * fs / 1000 */
  953. n = (audio_fs * 128) / 1000;
  954. }
  955. return n;
  956. }
  957. static int hdmi_audio_configure(struct sti_hdmi *hdmi,
  958. struct hdmi_audio_params *params)
  959. {
  960. int audio_cfg, n;
  961. struct hdmi_audio_infoframe *info = &params->cea;
  962. DRM_DEBUG_DRIVER("\n");
  963. if (!hdmi->enabled)
  964. return 0;
  965. /* update N parameter */
  966. n = sti_hdmi_audio_get_non_coherent_n(params->sample_rate);
  967. DRM_DEBUG_DRIVER("Audio rate = %d Hz, TMDS clock = %d Hz, n = %d\n",
  968. params->sample_rate, hdmi->mode.clock * 1000, n);
  969. hdmi_write(hdmi, n, HDMI_AUDN);
  970. /* update HDMI registers according to configuration */
  971. audio_cfg = HDMI_AUD_CFG_SPDIF_DIV_2 | HDMI_AUD_CFG_DTS_INVALID |
  972. HDMI_AUD_CFG_ONE_BIT_INVALID;
  973. switch (info->channels) {
  974. case 8:
  975. audio_cfg |= HDMI_AUD_CFG_CH78_VALID;
  976. case 6:
  977. audio_cfg |= HDMI_AUD_CFG_CH56_VALID;
  978. case 4:
  979. audio_cfg |= HDMI_AUD_CFG_CH34_VALID | HDMI_AUD_CFG_8CH;
  980. case 2:
  981. audio_cfg |= HDMI_AUD_CFG_CH12_VALID;
  982. break;
  983. default:
  984. DRM_ERROR("ERROR: Unsupported number of channels (%d)!\n",
  985. info->channels);
  986. return -EINVAL;
  987. }
  988. hdmi_write(hdmi, audio_cfg, HDMI_AUDIO_CFG);
  989. hdmi->audio = *params;
  990. return hdmi_audio_infoframe_config(hdmi);
  991. }
  992. static void hdmi_audio_shutdown(struct device *dev, void *data)
  993. {
  994. struct sti_hdmi *hdmi = dev_get_drvdata(dev);
  995. int audio_cfg;
  996. DRM_DEBUG_DRIVER("\n");
  997. /* disable audio */
  998. audio_cfg = HDMI_AUD_CFG_SPDIF_DIV_2 | HDMI_AUD_CFG_DTS_INVALID |
  999. HDMI_AUD_CFG_ONE_BIT_INVALID;
  1000. hdmi_write(hdmi, audio_cfg, HDMI_AUDIO_CFG);
  1001. hdmi->audio.enabled = false;
  1002. hdmi_audio_infoframe_config(hdmi);
  1003. }
  1004. static int hdmi_audio_hw_params(struct device *dev,
  1005. void *data,
  1006. struct hdmi_codec_daifmt *daifmt,
  1007. struct hdmi_codec_params *params)
  1008. {
  1009. struct sti_hdmi *hdmi = dev_get_drvdata(dev);
  1010. int ret;
  1011. struct hdmi_audio_params audio = {
  1012. .sample_width = params->sample_width,
  1013. .sample_rate = params->sample_rate,
  1014. .cea = params->cea,
  1015. };
  1016. DRM_DEBUG_DRIVER("\n");
  1017. if (!hdmi->enabled)
  1018. return 0;
  1019. if ((daifmt->fmt != HDMI_I2S) || daifmt->bit_clk_inv ||
  1020. daifmt->frame_clk_inv || daifmt->bit_clk_master ||
  1021. daifmt->frame_clk_master) {
  1022. dev_err(dev, "%s: Bad flags %d %d %d %d\n", __func__,
  1023. daifmt->bit_clk_inv, daifmt->frame_clk_inv,
  1024. daifmt->bit_clk_master,
  1025. daifmt->frame_clk_master);
  1026. return -EINVAL;
  1027. }
  1028. audio.enabled = true;
  1029. ret = hdmi_audio_configure(hdmi, &audio);
  1030. if (ret < 0)
  1031. return ret;
  1032. return 0;
  1033. }
  1034. static int hdmi_audio_digital_mute(struct device *dev, void *data, bool enable)
  1035. {
  1036. struct sti_hdmi *hdmi = dev_get_drvdata(dev);
  1037. DRM_DEBUG_DRIVER("%s\n", enable ? "enable" : "disable");
  1038. if (enable)
  1039. hdmi_write(hdmi, HDMI_SAMPLE_FLAT_ALL, HDMI_SAMPLE_FLAT_MASK);
  1040. else
  1041. hdmi_write(hdmi, HDMI_SAMPLE_FLAT_NO, HDMI_SAMPLE_FLAT_MASK);
  1042. return 0;
  1043. }
  1044. static int hdmi_audio_get_eld(struct device *dev, void *data, uint8_t *buf, size_t len)
  1045. {
  1046. struct sti_hdmi *hdmi = dev_get_drvdata(dev);
  1047. struct drm_connector *connector = hdmi->drm_connector;
  1048. DRM_DEBUG_DRIVER("\n");
  1049. memcpy(buf, connector->eld, min(sizeof(connector->eld), len));
  1050. return 0;
  1051. }
  1052. static const struct hdmi_codec_ops audio_codec_ops = {
  1053. .hw_params = hdmi_audio_hw_params,
  1054. .audio_shutdown = hdmi_audio_shutdown,
  1055. .digital_mute = hdmi_audio_digital_mute,
  1056. .get_eld = hdmi_audio_get_eld,
  1057. };
  1058. static int sti_hdmi_register_audio_driver(struct device *dev,
  1059. struct sti_hdmi *hdmi)
  1060. {
  1061. struct hdmi_codec_pdata codec_data = {
  1062. .ops = &audio_codec_ops,
  1063. .max_i2s_channels = 8,
  1064. .i2s = 1,
  1065. };
  1066. DRM_DEBUG_DRIVER("\n");
  1067. hdmi->audio.enabled = false;
  1068. hdmi->audio_pdev = platform_device_register_data(
  1069. dev, HDMI_CODEC_DRV_NAME, PLATFORM_DEVID_AUTO,
  1070. &codec_data, sizeof(codec_data));
  1071. if (IS_ERR(hdmi->audio_pdev))
  1072. return PTR_ERR(hdmi->audio_pdev);
  1073. DRM_INFO("%s Driver bound %s\n", HDMI_CODEC_DRV_NAME, dev_name(dev));
  1074. return 0;
  1075. }
  1076. static int sti_hdmi_bind(struct device *dev, struct device *master, void *data)
  1077. {
  1078. struct sti_hdmi *hdmi = dev_get_drvdata(dev);
  1079. struct drm_device *drm_dev = data;
  1080. struct drm_encoder *encoder;
  1081. struct sti_hdmi_connector *connector;
  1082. struct drm_connector *drm_connector;
  1083. struct drm_bridge *bridge;
  1084. int err;
  1085. /* Set the drm device handle */
  1086. hdmi->drm_dev = drm_dev;
  1087. encoder = sti_hdmi_find_encoder(drm_dev);
  1088. if (!encoder)
  1089. return -EINVAL;
  1090. connector = devm_kzalloc(dev, sizeof(*connector), GFP_KERNEL);
  1091. if (!connector)
  1092. return -EINVAL;
  1093. connector->hdmi = hdmi;
  1094. bridge = devm_kzalloc(dev, sizeof(*bridge), GFP_KERNEL);
  1095. if (!bridge)
  1096. return -EINVAL;
  1097. bridge->driver_private = hdmi;
  1098. bridge->funcs = &sti_hdmi_bridge_funcs;
  1099. drm_bridge_attach(drm_dev, bridge);
  1100. encoder->bridge = bridge;
  1101. connector->encoder = encoder;
  1102. drm_connector = (struct drm_connector *)connector;
  1103. drm_connector->polled = DRM_CONNECTOR_POLL_HPD;
  1104. drm_connector_init(drm_dev, drm_connector,
  1105. &sti_hdmi_connector_funcs, DRM_MODE_CONNECTOR_HDMIA);
  1106. drm_connector_helper_add(drm_connector,
  1107. &sti_hdmi_connector_helper_funcs);
  1108. /* initialise property */
  1109. sti_hdmi_connector_init_property(drm_dev, drm_connector);
  1110. hdmi->drm_connector = drm_connector;
  1111. err = drm_mode_connector_attach_encoder(drm_connector, encoder);
  1112. if (err) {
  1113. DRM_ERROR("Failed to attach a connector to a encoder\n");
  1114. goto err_sysfs;
  1115. }
  1116. err = sti_hdmi_register_audio_driver(dev, hdmi);
  1117. if (err) {
  1118. DRM_ERROR("Failed to attach an audio codec\n");
  1119. goto err_sysfs;
  1120. }
  1121. /* Initialize audio infoframe */
  1122. err = hdmi_audio_infoframe_init(&hdmi->audio.cea);
  1123. if (err) {
  1124. DRM_ERROR("Failed to init audio infoframe\n");
  1125. goto err_sysfs;
  1126. }
  1127. /* Enable default interrupts */
  1128. hdmi_write(hdmi, HDMI_DEFAULT_INT, HDMI_INT_EN);
  1129. return 0;
  1130. err_sysfs:
  1131. drm_bridge_remove(bridge);
  1132. hdmi->drm_connector = NULL;
  1133. return -EINVAL;
  1134. }
  1135. static void sti_hdmi_unbind(struct device *dev,
  1136. struct device *master, void *data)
  1137. {
  1138. struct sti_hdmi *hdmi = dev_get_drvdata(dev);
  1139. struct drm_device *drm_dev = data;
  1140. hdmi_debugfs_exit(hdmi, drm_dev->primary);
  1141. }
  1142. static const struct component_ops sti_hdmi_ops = {
  1143. .bind = sti_hdmi_bind,
  1144. .unbind = sti_hdmi_unbind,
  1145. };
  1146. static const struct of_device_id hdmi_of_match[] = {
  1147. {
  1148. .compatible = "st,stih407-hdmi",
  1149. .data = &tx3g4c28phy_ops,
  1150. }, {
  1151. /* end node */
  1152. }
  1153. };
  1154. MODULE_DEVICE_TABLE(of, hdmi_of_match);
  1155. static int sti_hdmi_probe(struct platform_device *pdev)
  1156. {
  1157. struct device *dev = &pdev->dev;
  1158. struct sti_hdmi *hdmi;
  1159. struct device_node *np = dev->of_node;
  1160. struct resource *res;
  1161. struct device_node *ddc;
  1162. int ret;
  1163. DRM_INFO("%s\n", __func__);
  1164. hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
  1165. if (!hdmi)
  1166. return -ENOMEM;
  1167. ddc = of_parse_phandle(pdev->dev.of_node, "ddc", 0);
  1168. if (ddc) {
  1169. hdmi->ddc_adapt = of_get_i2c_adapter_by_node(ddc);
  1170. of_node_put(ddc);
  1171. if (!hdmi->ddc_adapt)
  1172. return -EPROBE_DEFER;
  1173. }
  1174. hdmi->dev = pdev->dev;
  1175. /* Get resources */
  1176. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hdmi-reg");
  1177. if (!res) {
  1178. DRM_ERROR("Invalid hdmi resource\n");
  1179. ret = -ENOMEM;
  1180. goto release_adapter;
  1181. }
  1182. hdmi->regs = devm_ioremap_nocache(dev, res->start, resource_size(res));
  1183. if (!hdmi->regs) {
  1184. ret = -ENOMEM;
  1185. goto release_adapter;
  1186. }
  1187. hdmi->phy_ops = (struct hdmi_phy_ops *)
  1188. of_match_node(hdmi_of_match, np)->data;
  1189. /* Get clock resources */
  1190. hdmi->clk_pix = devm_clk_get(dev, "pix");
  1191. if (IS_ERR(hdmi->clk_pix)) {
  1192. DRM_ERROR("Cannot get hdmi_pix clock\n");
  1193. ret = PTR_ERR(hdmi->clk_pix);
  1194. goto release_adapter;
  1195. }
  1196. hdmi->clk_tmds = devm_clk_get(dev, "tmds");
  1197. if (IS_ERR(hdmi->clk_tmds)) {
  1198. DRM_ERROR("Cannot get hdmi_tmds clock\n");
  1199. ret = PTR_ERR(hdmi->clk_tmds);
  1200. goto release_adapter;
  1201. }
  1202. hdmi->clk_phy = devm_clk_get(dev, "phy");
  1203. if (IS_ERR(hdmi->clk_phy)) {
  1204. DRM_ERROR("Cannot get hdmi_phy clock\n");
  1205. ret = PTR_ERR(hdmi->clk_phy);
  1206. goto release_adapter;
  1207. }
  1208. hdmi->clk_audio = devm_clk_get(dev, "audio");
  1209. if (IS_ERR(hdmi->clk_audio)) {
  1210. DRM_ERROR("Cannot get hdmi_audio clock\n");
  1211. ret = PTR_ERR(hdmi->clk_audio);
  1212. goto release_adapter;
  1213. }
  1214. hdmi->hpd = readl(hdmi->regs + HDMI_STA) & HDMI_STA_HOT_PLUG;
  1215. init_waitqueue_head(&hdmi->wait_event);
  1216. hdmi->irq = platform_get_irq_byname(pdev, "irq");
  1217. ret = devm_request_threaded_irq(dev, hdmi->irq, hdmi_irq,
  1218. hdmi_irq_thread, IRQF_ONESHOT, dev_name(dev), hdmi);
  1219. if (ret) {
  1220. DRM_ERROR("Failed to register HDMI interrupt\n");
  1221. goto release_adapter;
  1222. }
  1223. hdmi->reset = devm_reset_control_get(dev, "hdmi");
  1224. /* Take hdmi out of reset */
  1225. if (!IS_ERR(hdmi->reset))
  1226. reset_control_deassert(hdmi->reset);
  1227. platform_set_drvdata(pdev, hdmi);
  1228. return component_add(&pdev->dev, &sti_hdmi_ops);
  1229. release_adapter:
  1230. i2c_put_adapter(hdmi->ddc_adapt);
  1231. return ret;
  1232. }
  1233. static int sti_hdmi_remove(struct platform_device *pdev)
  1234. {
  1235. struct sti_hdmi *hdmi = dev_get_drvdata(&pdev->dev);
  1236. i2c_put_adapter(hdmi->ddc_adapt);
  1237. if (hdmi->audio_pdev)
  1238. platform_device_unregister(hdmi->audio_pdev);
  1239. component_del(&pdev->dev, &sti_hdmi_ops);
  1240. return 0;
  1241. }
  1242. struct platform_driver sti_hdmi_driver = {
  1243. .driver = {
  1244. .name = "sti-hdmi",
  1245. .owner = THIS_MODULE,
  1246. .of_match_table = hdmi_of_match,
  1247. },
  1248. .probe = sti_hdmi_probe,
  1249. .remove = sti_hdmi_remove,
  1250. };
  1251. MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
  1252. MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
  1253. MODULE_LICENSE("GPL");