sti_hdmi.c 42 KB

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