sti_hdmi.c 41 KB

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