sti_hdmi.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477
  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_mode_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_mode_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. drm_bridge_remove(bridge);
  1101. hdmi->drm_connector = NULL;
  1102. return -EINVAL;
  1103. }
  1104. static void sti_hdmi_unbind(struct device *dev,
  1105. struct device *master, void *data)
  1106. {
  1107. }
  1108. static const struct component_ops sti_hdmi_ops = {
  1109. .bind = sti_hdmi_bind,
  1110. .unbind = sti_hdmi_unbind,
  1111. };
  1112. static const struct of_device_id hdmi_of_match[] = {
  1113. {
  1114. .compatible = "st,stih407-hdmi",
  1115. .data = &tx3g4c28phy_ops,
  1116. }, {
  1117. /* end node */
  1118. }
  1119. };
  1120. MODULE_DEVICE_TABLE(of, hdmi_of_match);
  1121. static int sti_hdmi_probe(struct platform_device *pdev)
  1122. {
  1123. struct device *dev = &pdev->dev;
  1124. struct sti_hdmi *hdmi;
  1125. struct device_node *np = dev->of_node;
  1126. struct resource *res;
  1127. struct device_node *ddc;
  1128. int ret;
  1129. DRM_INFO("%s\n", __func__);
  1130. hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
  1131. if (!hdmi)
  1132. return -ENOMEM;
  1133. ddc = of_parse_phandle(pdev->dev.of_node, "ddc", 0);
  1134. if (ddc) {
  1135. hdmi->ddc_adapt = of_get_i2c_adapter_by_node(ddc);
  1136. of_node_put(ddc);
  1137. if (!hdmi->ddc_adapt)
  1138. return -EPROBE_DEFER;
  1139. }
  1140. hdmi->dev = pdev->dev;
  1141. /* Get resources */
  1142. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hdmi-reg");
  1143. if (!res) {
  1144. DRM_ERROR("Invalid hdmi resource\n");
  1145. ret = -ENOMEM;
  1146. goto release_adapter;
  1147. }
  1148. hdmi->regs = devm_ioremap_nocache(dev, res->start, resource_size(res));
  1149. if (!hdmi->regs) {
  1150. ret = -ENOMEM;
  1151. goto release_adapter;
  1152. }
  1153. hdmi->phy_ops = (struct hdmi_phy_ops *)
  1154. of_match_node(hdmi_of_match, np)->data;
  1155. /* Get clock resources */
  1156. hdmi->clk_pix = devm_clk_get(dev, "pix");
  1157. if (IS_ERR(hdmi->clk_pix)) {
  1158. DRM_ERROR("Cannot get hdmi_pix clock\n");
  1159. ret = PTR_ERR(hdmi->clk_pix);
  1160. goto release_adapter;
  1161. }
  1162. hdmi->clk_tmds = devm_clk_get(dev, "tmds");
  1163. if (IS_ERR(hdmi->clk_tmds)) {
  1164. DRM_ERROR("Cannot get hdmi_tmds clock\n");
  1165. ret = PTR_ERR(hdmi->clk_tmds);
  1166. goto release_adapter;
  1167. }
  1168. hdmi->clk_phy = devm_clk_get(dev, "phy");
  1169. if (IS_ERR(hdmi->clk_phy)) {
  1170. DRM_ERROR("Cannot get hdmi_phy clock\n");
  1171. ret = PTR_ERR(hdmi->clk_phy);
  1172. goto release_adapter;
  1173. }
  1174. hdmi->clk_audio = devm_clk_get(dev, "audio");
  1175. if (IS_ERR(hdmi->clk_audio)) {
  1176. DRM_ERROR("Cannot get hdmi_audio clock\n");
  1177. ret = PTR_ERR(hdmi->clk_audio);
  1178. goto release_adapter;
  1179. }
  1180. hdmi->hpd = readl(hdmi->regs + HDMI_STA) & HDMI_STA_HOT_PLUG;
  1181. init_waitqueue_head(&hdmi->wait_event);
  1182. hdmi->irq = platform_get_irq_byname(pdev, "irq");
  1183. if (hdmi->irq < 0) {
  1184. DRM_ERROR("Cannot get HDMI irq\n");
  1185. ret = hdmi->irq;
  1186. goto release_adapter;
  1187. }
  1188. ret = devm_request_threaded_irq(dev, hdmi->irq, hdmi_irq,
  1189. hdmi_irq_thread, IRQF_ONESHOT, dev_name(dev), hdmi);
  1190. if (ret) {
  1191. DRM_ERROR("Failed to register HDMI interrupt\n");
  1192. goto release_adapter;
  1193. }
  1194. hdmi->notifier = cec_notifier_get(&pdev->dev);
  1195. if (!hdmi->notifier)
  1196. goto release_adapter;
  1197. hdmi->reset = devm_reset_control_get(dev, "hdmi");
  1198. /* Take hdmi out of reset */
  1199. if (!IS_ERR(hdmi->reset))
  1200. reset_control_deassert(hdmi->reset);
  1201. platform_set_drvdata(pdev, hdmi);
  1202. return component_add(&pdev->dev, &sti_hdmi_ops);
  1203. release_adapter:
  1204. i2c_put_adapter(hdmi->ddc_adapt);
  1205. return ret;
  1206. }
  1207. static int sti_hdmi_remove(struct platform_device *pdev)
  1208. {
  1209. struct sti_hdmi *hdmi = dev_get_drvdata(&pdev->dev);
  1210. cec_notifier_set_phys_addr(hdmi->notifier, CEC_PHYS_ADDR_INVALID);
  1211. i2c_put_adapter(hdmi->ddc_adapt);
  1212. if (hdmi->audio_pdev)
  1213. platform_device_unregister(hdmi->audio_pdev);
  1214. component_del(&pdev->dev, &sti_hdmi_ops);
  1215. cec_notifier_put(hdmi->notifier);
  1216. return 0;
  1217. }
  1218. struct platform_driver sti_hdmi_driver = {
  1219. .driver = {
  1220. .name = "sti-hdmi",
  1221. .owner = THIS_MODULE,
  1222. .of_match_table = hdmi_of_match,
  1223. },
  1224. .probe = sti_hdmi_probe,
  1225. .remove = sti_hdmi_remove,
  1226. };
  1227. MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
  1228. MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
  1229. MODULE_LICENSE("GPL");