sti_hdmi.c 35 KB

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