mtk_hdmi.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843
  1. /*
  2. * Copyright (c) 2014 MediaTek Inc.
  3. * Author: Jie Qiu <jie.qiu@mediatek.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <drm/drmP.h>
  15. #include <drm/drm_atomic_helper.h>
  16. #include <drm/drm_crtc.h>
  17. #include <drm/drm_crtc_helper.h>
  18. #include <drm/drm_edid.h>
  19. #include <linux/arm-smccc.h>
  20. #include <linux/clk.h>
  21. #include <linux/delay.h>
  22. #include <linux/hdmi.h>
  23. #include <linux/i2c.h>
  24. #include <linux/io.h>
  25. #include <linux/kernel.h>
  26. #include <linux/mfd/syscon.h>
  27. #include <linux/of_platform.h>
  28. #include <linux/of.h>
  29. #include <linux/of_gpio.h>
  30. #include <linux/of_graph.h>
  31. #include <linux/phy/phy.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/regmap.h>
  34. #include <sound/hdmi-codec.h>
  35. #include "mtk_cec.h"
  36. #include "mtk_hdmi.h"
  37. #include "mtk_hdmi_regs.h"
  38. #define NCTS_BYTES 7
  39. enum mtk_hdmi_clk_id {
  40. MTK_HDMI_CLK_HDMI_PIXEL,
  41. MTK_HDMI_CLK_HDMI_PLL,
  42. MTK_HDMI_CLK_AUD_BCLK,
  43. MTK_HDMI_CLK_AUD_SPDIF,
  44. MTK_HDMI_CLK_COUNT
  45. };
  46. enum hdmi_aud_input_type {
  47. HDMI_AUD_INPUT_I2S = 0,
  48. HDMI_AUD_INPUT_SPDIF,
  49. };
  50. enum hdmi_aud_i2s_fmt {
  51. HDMI_I2S_MODE_RJT_24BIT = 0,
  52. HDMI_I2S_MODE_RJT_16BIT,
  53. HDMI_I2S_MODE_LJT_24BIT,
  54. HDMI_I2S_MODE_LJT_16BIT,
  55. HDMI_I2S_MODE_I2S_24BIT,
  56. HDMI_I2S_MODE_I2S_16BIT
  57. };
  58. enum hdmi_aud_mclk {
  59. HDMI_AUD_MCLK_128FS,
  60. HDMI_AUD_MCLK_192FS,
  61. HDMI_AUD_MCLK_256FS,
  62. HDMI_AUD_MCLK_384FS,
  63. HDMI_AUD_MCLK_512FS,
  64. HDMI_AUD_MCLK_768FS,
  65. HDMI_AUD_MCLK_1152FS,
  66. };
  67. enum hdmi_aud_channel_type {
  68. HDMI_AUD_CHAN_TYPE_1_0 = 0,
  69. HDMI_AUD_CHAN_TYPE_1_1,
  70. HDMI_AUD_CHAN_TYPE_2_0,
  71. HDMI_AUD_CHAN_TYPE_2_1,
  72. HDMI_AUD_CHAN_TYPE_3_0,
  73. HDMI_AUD_CHAN_TYPE_3_1,
  74. HDMI_AUD_CHAN_TYPE_4_0,
  75. HDMI_AUD_CHAN_TYPE_4_1,
  76. HDMI_AUD_CHAN_TYPE_5_0,
  77. HDMI_AUD_CHAN_TYPE_5_1,
  78. HDMI_AUD_CHAN_TYPE_6_0,
  79. HDMI_AUD_CHAN_TYPE_6_1,
  80. HDMI_AUD_CHAN_TYPE_7_0,
  81. HDMI_AUD_CHAN_TYPE_7_1,
  82. HDMI_AUD_CHAN_TYPE_3_0_LRS,
  83. HDMI_AUD_CHAN_TYPE_3_1_LRS,
  84. HDMI_AUD_CHAN_TYPE_4_0_CLRS,
  85. HDMI_AUD_CHAN_TYPE_4_1_CLRS,
  86. HDMI_AUD_CHAN_TYPE_6_1_CS,
  87. HDMI_AUD_CHAN_TYPE_6_1_CH,
  88. HDMI_AUD_CHAN_TYPE_6_1_OH,
  89. HDMI_AUD_CHAN_TYPE_6_1_CHR,
  90. HDMI_AUD_CHAN_TYPE_7_1_LH_RH,
  91. HDMI_AUD_CHAN_TYPE_7_1_LSR_RSR,
  92. HDMI_AUD_CHAN_TYPE_7_1_LC_RC,
  93. HDMI_AUD_CHAN_TYPE_7_1_LW_RW,
  94. HDMI_AUD_CHAN_TYPE_7_1_LSD_RSD,
  95. HDMI_AUD_CHAN_TYPE_7_1_LSS_RSS,
  96. HDMI_AUD_CHAN_TYPE_7_1_LHS_RHS,
  97. HDMI_AUD_CHAN_TYPE_7_1_CS_CH,
  98. HDMI_AUD_CHAN_TYPE_7_1_CS_OH,
  99. HDMI_AUD_CHAN_TYPE_7_1_CS_CHR,
  100. HDMI_AUD_CHAN_TYPE_7_1_CH_OH,
  101. HDMI_AUD_CHAN_TYPE_7_1_CH_CHR,
  102. HDMI_AUD_CHAN_TYPE_7_1_OH_CHR,
  103. HDMI_AUD_CHAN_TYPE_7_1_LSS_RSS_LSR_RSR,
  104. HDMI_AUD_CHAN_TYPE_6_0_CS,
  105. HDMI_AUD_CHAN_TYPE_6_0_CH,
  106. HDMI_AUD_CHAN_TYPE_6_0_OH,
  107. HDMI_AUD_CHAN_TYPE_6_0_CHR,
  108. HDMI_AUD_CHAN_TYPE_7_0_LH_RH,
  109. HDMI_AUD_CHAN_TYPE_7_0_LSR_RSR,
  110. HDMI_AUD_CHAN_TYPE_7_0_LC_RC,
  111. HDMI_AUD_CHAN_TYPE_7_0_LW_RW,
  112. HDMI_AUD_CHAN_TYPE_7_0_LSD_RSD,
  113. HDMI_AUD_CHAN_TYPE_7_0_LSS_RSS,
  114. HDMI_AUD_CHAN_TYPE_7_0_LHS_RHS,
  115. HDMI_AUD_CHAN_TYPE_7_0_CS_CH,
  116. HDMI_AUD_CHAN_TYPE_7_0_CS_OH,
  117. HDMI_AUD_CHAN_TYPE_7_0_CS_CHR,
  118. HDMI_AUD_CHAN_TYPE_7_0_CH_OH,
  119. HDMI_AUD_CHAN_TYPE_7_0_CH_CHR,
  120. HDMI_AUD_CHAN_TYPE_7_0_OH_CHR,
  121. HDMI_AUD_CHAN_TYPE_7_0_LSS_RSS_LSR_RSR,
  122. HDMI_AUD_CHAN_TYPE_8_0_LH_RH_CS,
  123. HDMI_AUD_CHAN_TYPE_UNKNOWN = 0xFF
  124. };
  125. enum hdmi_aud_channel_swap_type {
  126. HDMI_AUD_SWAP_LR,
  127. HDMI_AUD_SWAP_LFE_CC,
  128. HDMI_AUD_SWAP_LSRS,
  129. HDMI_AUD_SWAP_RLS_RRS,
  130. HDMI_AUD_SWAP_LR_STATUS,
  131. };
  132. struct hdmi_audio_param {
  133. enum hdmi_audio_coding_type aud_codec;
  134. enum hdmi_audio_sample_size aud_sampe_size;
  135. enum hdmi_aud_input_type aud_input_type;
  136. enum hdmi_aud_i2s_fmt aud_i2s_fmt;
  137. enum hdmi_aud_mclk aud_mclk;
  138. enum hdmi_aud_channel_type aud_input_chan_type;
  139. struct hdmi_codec_params codec_params;
  140. };
  141. struct mtk_hdmi {
  142. struct drm_bridge bridge;
  143. struct drm_connector conn;
  144. struct device *dev;
  145. struct phy *phy;
  146. struct device *cec_dev;
  147. struct i2c_adapter *ddc_adpt;
  148. struct clk *clk[MTK_HDMI_CLK_COUNT];
  149. struct drm_display_mode mode;
  150. bool dvi_mode;
  151. u32 min_clock;
  152. u32 max_clock;
  153. u32 max_hdisplay;
  154. u32 max_vdisplay;
  155. u32 ibias;
  156. u32 ibias_up;
  157. struct regmap *sys_regmap;
  158. unsigned int sys_offset;
  159. void __iomem *regs;
  160. enum hdmi_colorspace csp;
  161. struct hdmi_audio_param aud_param;
  162. bool audio_enable;
  163. bool powered;
  164. bool enabled;
  165. };
  166. static inline struct mtk_hdmi *hdmi_ctx_from_bridge(struct drm_bridge *b)
  167. {
  168. return container_of(b, struct mtk_hdmi, bridge);
  169. }
  170. static inline struct mtk_hdmi *hdmi_ctx_from_conn(struct drm_connector *c)
  171. {
  172. return container_of(c, struct mtk_hdmi, conn);
  173. }
  174. static u32 mtk_hdmi_read(struct mtk_hdmi *hdmi, u32 offset)
  175. {
  176. return readl(hdmi->regs + offset);
  177. }
  178. static void mtk_hdmi_write(struct mtk_hdmi *hdmi, u32 offset, u32 val)
  179. {
  180. writel(val, hdmi->regs + offset);
  181. }
  182. static void mtk_hdmi_clear_bits(struct mtk_hdmi *hdmi, u32 offset, u32 bits)
  183. {
  184. void __iomem *reg = hdmi->regs + offset;
  185. u32 tmp;
  186. tmp = readl(reg);
  187. tmp &= ~bits;
  188. writel(tmp, reg);
  189. }
  190. static void mtk_hdmi_set_bits(struct mtk_hdmi *hdmi, u32 offset, u32 bits)
  191. {
  192. void __iomem *reg = hdmi->regs + offset;
  193. u32 tmp;
  194. tmp = readl(reg);
  195. tmp |= bits;
  196. writel(tmp, reg);
  197. }
  198. static void mtk_hdmi_mask(struct mtk_hdmi *hdmi, u32 offset, u32 val, u32 mask)
  199. {
  200. void __iomem *reg = hdmi->regs + offset;
  201. u32 tmp;
  202. tmp = readl(reg);
  203. tmp = (tmp & ~mask) | (val & mask);
  204. writel(tmp, reg);
  205. }
  206. static void mtk_hdmi_hw_vid_black(struct mtk_hdmi *hdmi, bool black)
  207. {
  208. mtk_hdmi_mask(hdmi, VIDEO_CFG_4, black ? GEN_RGB : NORMAL_PATH,
  209. VIDEO_SOURCE_SEL);
  210. }
  211. static void mtk_hdmi_hw_make_reg_writable(struct mtk_hdmi *hdmi, bool enable)
  212. {
  213. struct arm_smccc_res res;
  214. /*
  215. * MT8173 HDMI hardware has an output control bit to enable/disable HDMI
  216. * output. This bit can only be controlled in ARM supervisor mode.
  217. * The ARM trusted firmware provides an API for the HDMI driver to set
  218. * this control bit to enable HDMI output in supervisor mode.
  219. */
  220. arm_smccc_smc(MTK_SIP_SET_AUTHORIZED_SECURE_REG, 0x14000904, 0x80000000,
  221. 0, 0, 0, 0, 0, &res);
  222. regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG20,
  223. HDMI_PCLK_FREE_RUN, enable ? HDMI_PCLK_FREE_RUN : 0);
  224. regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG1C,
  225. HDMI_ON | ANLG_ON, enable ? (HDMI_ON | ANLG_ON) : 0);
  226. }
  227. static void mtk_hdmi_hw_1p4_version_enable(struct mtk_hdmi *hdmi, bool enable)
  228. {
  229. regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG20,
  230. HDMI2P0_EN, enable ? 0 : HDMI2P0_EN);
  231. }
  232. static void mtk_hdmi_hw_aud_mute(struct mtk_hdmi *hdmi)
  233. {
  234. mtk_hdmi_set_bits(hdmi, GRL_AUDIO_CFG, AUDIO_ZERO);
  235. }
  236. static void mtk_hdmi_hw_aud_unmute(struct mtk_hdmi *hdmi)
  237. {
  238. mtk_hdmi_clear_bits(hdmi, GRL_AUDIO_CFG, AUDIO_ZERO);
  239. }
  240. static void mtk_hdmi_hw_reset(struct mtk_hdmi *hdmi)
  241. {
  242. regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG1C,
  243. HDMI_RST, HDMI_RST);
  244. regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG1C,
  245. HDMI_RST, 0);
  246. mtk_hdmi_clear_bits(hdmi, GRL_CFG3, CFG3_CONTROL_PACKET_DELAY);
  247. regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG1C,
  248. ANLG_ON, ANLG_ON);
  249. }
  250. static void mtk_hdmi_hw_enable_notice(struct mtk_hdmi *hdmi, bool enable_notice)
  251. {
  252. mtk_hdmi_mask(hdmi, GRL_CFG2, enable_notice ? CFG2_NOTICE_EN : 0,
  253. CFG2_NOTICE_EN);
  254. }
  255. static void mtk_hdmi_hw_write_int_mask(struct mtk_hdmi *hdmi, u32 int_mask)
  256. {
  257. mtk_hdmi_write(hdmi, GRL_INT_MASK, int_mask);
  258. }
  259. static void mtk_hdmi_hw_enable_dvi_mode(struct mtk_hdmi *hdmi, bool enable)
  260. {
  261. mtk_hdmi_mask(hdmi, GRL_CFG1, enable ? CFG1_DVI : 0, CFG1_DVI);
  262. }
  263. static void mtk_hdmi_hw_send_info_frame(struct mtk_hdmi *hdmi, u8 *buffer,
  264. u8 len)
  265. {
  266. u32 ctrl_reg = GRL_CTRL;
  267. int i;
  268. u8 *frame_data;
  269. enum hdmi_infoframe_type frame_type;
  270. u8 frame_ver;
  271. u8 frame_len;
  272. u8 checksum;
  273. int ctrl_frame_en = 0;
  274. frame_type = *buffer;
  275. buffer += 1;
  276. frame_ver = *buffer;
  277. buffer += 1;
  278. frame_len = *buffer;
  279. buffer += 1;
  280. checksum = *buffer;
  281. buffer += 1;
  282. frame_data = buffer;
  283. dev_dbg(hdmi->dev,
  284. "frame_type:0x%x,frame_ver:0x%x,frame_len:0x%x,checksum:0x%x\n",
  285. frame_type, frame_ver, frame_len, checksum);
  286. switch (frame_type) {
  287. case HDMI_INFOFRAME_TYPE_AVI:
  288. ctrl_frame_en = CTRL_AVI_EN;
  289. ctrl_reg = GRL_CTRL;
  290. break;
  291. case HDMI_INFOFRAME_TYPE_SPD:
  292. ctrl_frame_en = CTRL_SPD_EN;
  293. ctrl_reg = GRL_CTRL;
  294. break;
  295. case HDMI_INFOFRAME_TYPE_AUDIO:
  296. ctrl_frame_en = CTRL_AUDIO_EN;
  297. ctrl_reg = GRL_CTRL;
  298. break;
  299. case HDMI_INFOFRAME_TYPE_VENDOR:
  300. ctrl_frame_en = VS_EN;
  301. ctrl_reg = GRL_ACP_ISRC_CTRL;
  302. break;
  303. }
  304. mtk_hdmi_clear_bits(hdmi, ctrl_reg, ctrl_frame_en);
  305. mtk_hdmi_write(hdmi, GRL_INFOFRM_TYPE, frame_type);
  306. mtk_hdmi_write(hdmi, GRL_INFOFRM_VER, frame_ver);
  307. mtk_hdmi_write(hdmi, GRL_INFOFRM_LNG, frame_len);
  308. mtk_hdmi_write(hdmi, GRL_IFM_PORT, checksum);
  309. for (i = 0; i < frame_len; i++)
  310. mtk_hdmi_write(hdmi, GRL_IFM_PORT, frame_data[i]);
  311. mtk_hdmi_set_bits(hdmi, ctrl_reg, ctrl_frame_en);
  312. }
  313. static void mtk_hdmi_hw_send_aud_packet(struct mtk_hdmi *hdmi, bool enable)
  314. {
  315. mtk_hdmi_mask(hdmi, GRL_SHIFT_R2, enable ? 0 : AUDIO_PACKET_OFF,
  316. AUDIO_PACKET_OFF);
  317. }
  318. static void mtk_hdmi_hw_config_sys(struct mtk_hdmi *hdmi)
  319. {
  320. regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG20,
  321. HDMI_OUT_FIFO_EN | MHL_MODE_ON, 0);
  322. usleep_range(2000, 4000);
  323. regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG20,
  324. HDMI_OUT_FIFO_EN | MHL_MODE_ON, HDMI_OUT_FIFO_EN);
  325. }
  326. static void mtk_hdmi_hw_set_deep_color_mode(struct mtk_hdmi *hdmi)
  327. {
  328. regmap_update_bits(hdmi->sys_regmap, hdmi->sys_offset + HDMI_SYS_CFG20,
  329. DEEP_COLOR_MODE_MASK | DEEP_COLOR_EN,
  330. COLOR_8BIT_MODE);
  331. }
  332. static void mtk_hdmi_hw_send_av_mute(struct mtk_hdmi *hdmi)
  333. {
  334. mtk_hdmi_clear_bits(hdmi, GRL_CFG4, CTRL_AVMUTE);
  335. usleep_range(2000, 4000);
  336. mtk_hdmi_set_bits(hdmi, GRL_CFG4, CTRL_AVMUTE);
  337. }
  338. static void mtk_hdmi_hw_send_av_unmute(struct mtk_hdmi *hdmi)
  339. {
  340. mtk_hdmi_mask(hdmi, GRL_CFG4, CFG4_AV_UNMUTE_EN,
  341. CFG4_AV_UNMUTE_EN | CFG4_AV_UNMUTE_SET);
  342. usleep_range(2000, 4000);
  343. mtk_hdmi_mask(hdmi, GRL_CFG4, CFG4_AV_UNMUTE_SET,
  344. CFG4_AV_UNMUTE_EN | CFG4_AV_UNMUTE_SET);
  345. }
  346. static void mtk_hdmi_hw_ncts_enable(struct mtk_hdmi *hdmi, bool on)
  347. {
  348. mtk_hdmi_mask(hdmi, GRL_CTS_CTRL, on ? 0 : CTS_CTRL_SOFT,
  349. CTS_CTRL_SOFT);
  350. }
  351. static void mtk_hdmi_hw_ncts_auto_write_enable(struct mtk_hdmi *hdmi,
  352. bool enable)
  353. {
  354. mtk_hdmi_mask(hdmi, GRL_CTS_CTRL, enable ? NCTS_WRI_ANYTIME : 0,
  355. NCTS_WRI_ANYTIME);
  356. }
  357. static void mtk_hdmi_hw_msic_setting(struct mtk_hdmi *hdmi,
  358. struct drm_display_mode *mode)
  359. {
  360. mtk_hdmi_clear_bits(hdmi, GRL_CFG4, CFG4_MHL_MODE);
  361. if (mode->flags & DRM_MODE_FLAG_INTERLACE &&
  362. mode->clock == 74250 &&
  363. mode->vdisplay == 1080)
  364. mtk_hdmi_clear_bits(hdmi, GRL_CFG2, CFG2_MHL_DE_SEL);
  365. else
  366. mtk_hdmi_set_bits(hdmi, GRL_CFG2, CFG2_MHL_DE_SEL);
  367. }
  368. static void mtk_hdmi_hw_aud_set_channel_swap(struct mtk_hdmi *hdmi,
  369. enum hdmi_aud_channel_swap_type swap)
  370. {
  371. u8 swap_bit;
  372. switch (swap) {
  373. case HDMI_AUD_SWAP_LR:
  374. swap_bit = LR_SWAP;
  375. break;
  376. case HDMI_AUD_SWAP_LFE_CC:
  377. swap_bit = LFE_CC_SWAP;
  378. break;
  379. case HDMI_AUD_SWAP_LSRS:
  380. swap_bit = LSRS_SWAP;
  381. break;
  382. case HDMI_AUD_SWAP_RLS_RRS:
  383. swap_bit = RLS_RRS_SWAP;
  384. break;
  385. case HDMI_AUD_SWAP_LR_STATUS:
  386. swap_bit = LR_STATUS_SWAP;
  387. break;
  388. default:
  389. swap_bit = LFE_CC_SWAP;
  390. break;
  391. }
  392. mtk_hdmi_mask(hdmi, GRL_CH_SWAP, swap_bit, 0xff);
  393. }
  394. static void mtk_hdmi_hw_aud_set_bit_num(struct mtk_hdmi *hdmi,
  395. enum hdmi_audio_sample_size bit_num)
  396. {
  397. u32 val;
  398. switch (bit_num) {
  399. case HDMI_AUDIO_SAMPLE_SIZE_16:
  400. val = AOUT_16BIT;
  401. break;
  402. case HDMI_AUDIO_SAMPLE_SIZE_20:
  403. val = AOUT_20BIT;
  404. break;
  405. case HDMI_AUDIO_SAMPLE_SIZE_24:
  406. case HDMI_AUDIO_SAMPLE_SIZE_STREAM:
  407. val = AOUT_24BIT;
  408. break;
  409. }
  410. mtk_hdmi_mask(hdmi, GRL_AOUT_CFG, val, AOUT_BNUM_SEL_MASK);
  411. }
  412. static void mtk_hdmi_hw_aud_set_i2s_fmt(struct mtk_hdmi *hdmi,
  413. enum hdmi_aud_i2s_fmt i2s_fmt)
  414. {
  415. u32 val;
  416. val = mtk_hdmi_read(hdmi, GRL_CFG0);
  417. val &= ~(CFG0_W_LENGTH_MASK | CFG0_I2S_MODE_MASK);
  418. switch (i2s_fmt) {
  419. case HDMI_I2S_MODE_RJT_24BIT:
  420. val |= CFG0_I2S_MODE_RTJ | CFG0_W_LENGTH_24BIT;
  421. break;
  422. case HDMI_I2S_MODE_RJT_16BIT:
  423. val |= CFG0_I2S_MODE_RTJ | CFG0_W_LENGTH_16BIT;
  424. break;
  425. case HDMI_I2S_MODE_LJT_24BIT:
  426. default:
  427. val |= CFG0_I2S_MODE_LTJ | CFG0_W_LENGTH_24BIT;
  428. break;
  429. case HDMI_I2S_MODE_LJT_16BIT:
  430. val |= CFG0_I2S_MODE_LTJ | CFG0_W_LENGTH_16BIT;
  431. break;
  432. case HDMI_I2S_MODE_I2S_24BIT:
  433. val |= CFG0_I2S_MODE_I2S | CFG0_W_LENGTH_24BIT;
  434. break;
  435. case HDMI_I2S_MODE_I2S_16BIT:
  436. val |= CFG0_I2S_MODE_I2S | CFG0_W_LENGTH_16BIT;
  437. break;
  438. }
  439. mtk_hdmi_write(hdmi, GRL_CFG0, val);
  440. }
  441. static void mtk_hdmi_hw_audio_config(struct mtk_hdmi *hdmi, bool dst)
  442. {
  443. const u8 mask = HIGH_BIT_RATE | DST_NORMAL_DOUBLE | SACD_DST | DSD_SEL;
  444. u8 val;
  445. /* Disable high bitrate, set DST packet normal/double */
  446. mtk_hdmi_clear_bits(hdmi, GRL_AOUT_CFG, HIGH_BIT_RATE_PACKET_ALIGN);
  447. if (dst)
  448. val = DST_NORMAL_DOUBLE | SACD_DST;
  449. else
  450. val = 0;
  451. mtk_hdmi_mask(hdmi, GRL_AUDIO_CFG, val, mask);
  452. }
  453. static void mtk_hdmi_hw_aud_set_i2s_chan_num(struct mtk_hdmi *hdmi,
  454. enum hdmi_aud_channel_type channel_type,
  455. u8 channel_count)
  456. {
  457. unsigned int ch_switch;
  458. u8 i2s_uv;
  459. ch_switch = CH_SWITCH(7, 7) | CH_SWITCH(6, 6) |
  460. CH_SWITCH(5, 5) | CH_SWITCH(4, 4) |
  461. CH_SWITCH(3, 3) | CH_SWITCH(1, 2) |
  462. CH_SWITCH(2, 1) | CH_SWITCH(0, 0);
  463. if (channel_count == 2) {
  464. i2s_uv = I2S_UV_CH_EN(0);
  465. } else if (channel_count == 3 || channel_count == 4) {
  466. if (channel_count == 4 &&
  467. (channel_type == HDMI_AUD_CHAN_TYPE_3_0_LRS ||
  468. channel_type == HDMI_AUD_CHAN_TYPE_4_0))
  469. i2s_uv = I2S_UV_CH_EN(2) | I2S_UV_CH_EN(0);
  470. else
  471. i2s_uv = I2S_UV_CH_EN(3) | I2S_UV_CH_EN(2);
  472. } else if (channel_count == 6 || channel_count == 5) {
  473. if (channel_count == 6 &&
  474. channel_type != HDMI_AUD_CHAN_TYPE_5_1 &&
  475. channel_type != HDMI_AUD_CHAN_TYPE_4_1_CLRS) {
  476. i2s_uv = I2S_UV_CH_EN(3) | I2S_UV_CH_EN(2) |
  477. I2S_UV_CH_EN(1) | I2S_UV_CH_EN(0);
  478. } else {
  479. i2s_uv = I2S_UV_CH_EN(2) | I2S_UV_CH_EN(1) |
  480. I2S_UV_CH_EN(0);
  481. }
  482. } else if (channel_count == 8 || channel_count == 7) {
  483. i2s_uv = I2S_UV_CH_EN(3) | I2S_UV_CH_EN(2) |
  484. I2S_UV_CH_EN(1) | I2S_UV_CH_EN(0);
  485. } else {
  486. i2s_uv = I2S_UV_CH_EN(0);
  487. }
  488. mtk_hdmi_write(hdmi, GRL_CH_SW0, ch_switch & 0xff);
  489. mtk_hdmi_write(hdmi, GRL_CH_SW1, (ch_switch >> 8) & 0xff);
  490. mtk_hdmi_write(hdmi, GRL_CH_SW2, (ch_switch >> 16) & 0xff);
  491. mtk_hdmi_write(hdmi, GRL_I2S_UV, i2s_uv);
  492. }
  493. static void mtk_hdmi_hw_aud_set_input_type(struct mtk_hdmi *hdmi,
  494. enum hdmi_aud_input_type input_type)
  495. {
  496. u32 val;
  497. val = mtk_hdmi_read(hdmi, GRL_CFG1);
  498. if (input_type == HDMI_AUD_INPUT_I2S &&
  499. (val & CFG1_SPDIF) == CFG1_SPDIF) {
  500. val &= ~CFG1_SPDIF;
  501. } else if (input_type == HDMI_AUD_INPUT_SPDIF &&
  502. (val & CFG1_SPDIF) == 0) {
  503. val |= CFG1_SPDIF;
  504. }
  505. mtk_hdmi_write(hdmi, GRL_CFG1, val);
  506. }
  507. static void mtk_hdmi_hw_aud_set_channel_status(struct mtk_hdmi *hdmi,
  508. u8 *channel_status)
  509. {
  510. int i;
  511. for (i = 0; i < 5; i++) {
  512. mtk_hdmi_write(hdmi, GRL_I2S_C_STA0 + i * 4, channel_status[i]);
  513. mtk_hdmi_write(hdmi, GRL_L_STATUS_0 + i * 4, channel_status[i]);
  514. mtk_hdmi_write(hdmi, GRL_R_STATUS_0 + i * 4, channel_status[i]);
  515. }
  516. for (; i < 24; i++) {
  517. mtk_hdmi_write(hdmi, GRL_L_STATUS_0 + i * 4, 0);
  518. mtk_hdmi_write(hdmi, GRL_R_STATUS_0 + i * 4, 0);
  519. }
  520. }
  521. static void mtk_hdmi_hw_aud_src_reenable(struct mtk_hdmi *hdmi)
  522. {
  523. u32 val;
  524. val = mtk_hdmi_read(hdmi, GRL_MIX_CTRL);
  525. if (val & MIX_CTRL_SRC_EN) {
  526. val &= ~MIX_CTRL_SRC_EN;
  527. mtk_hdmi_write(hdmi, GRL_MIX_CTRL, val);
  528. usleep_range(255, 512);
  529. val |= MIX_CTRL_SRC_EN;
  530. mtk_hdmi_write(hdmi, GRL_MIX_CTRL, val);
  531. }
  532. }
  533. static void mtk_hdmi_hw_aud_src_disable(struct mtk_hdmi *hdmi)
  534. {
  535. u32 val;
  536. val = mtk_hdmi_read(hdmi, GRL_MIX_CTRL);
  537. val &= ~MIX_CTRL_SRC_EN;
  538. mtk_hdmi_write(hdmi, GRL_MIX_CTRL, val);
  539. mtk_hdmi_write(hdmi, GRL_SHIFT_L1, 0x00);
  540. }
  541. static void mtk_hdmi_hw_aud_set_mclk(struct mtk_hdmi *hdmi,
  542. enum hdmi_aud_mclk mclk)
  543. {
  544. u32 val;
  545. val = mtk_hdmi_read(hdmi, GRL_CFG5);
  546. val &= CFG5_CD_RATIO_MASK;
  547. switch (mclk) {
  548. case HDMI_AUD_MCLK_128FS:
  549. val |= CFG5_FS128;
  550. break;
  551. case HDMI_AUD_MCLK_256FS:
  552. val |= CFG5_FS256;
  553. break;
  554. case HDMI_AUD_MCLK_384FS:
  555. val |= CFG5_FS384;
  556. break;
  557. case HDMI_AUD_MCLK_512FS:
  558. val |= CFG5_FS512;
  559. break;
  560. case HDMI_AUD_MCLK_768FS:
  561. val |= CFG5_FS768;
  562. break;
  563. default:
  564. val |= CFG5_FS256;
  565. break;
  566. }
  567. mtk_hdmi_write(hdmi, GRL_CFG5, val);
  568. }
  569. struct hdmi_acr_n {
  570. unsigned int clock;
  571. unsigned int n[3];
  572. };
  573. /* Recommended N values from HDMI specification, tables 7-1 to 7-3 */
  574. static const struct hdmi_acr_n hdmi_rec_n_table[] = {
  575. /* Clock, N: 32kHz 44.1kHz 48kHz */
  576. { 25175, { 4576, 7007, 6864 } },
  577. { 74176, { 11648, 17836, 11648 } },
  578. { 148352, { 11648, 8918, 5824 } },
  579. { 296703, { 5824, 4459, 5824 } },
  580. { 297000, { 3072, 4704, 5120 } },
  581. { 0, { 4096, 6272, 6144 } }, /* all other TMDS clocks */
  582. };
  583. /**
  584. * hdmi_recommended_n() - Return N value recommended by HDMI specification
  585. * @freq: audio sample rate in Hz
  586. * @clock: rounded TMDS clock in kHz
  587. */
  588. static unsigned int hdmi_recommended_n(unsigned int freq, unsigned int clock)
  589. {
  590. const struct hdmi_acr_n *recommended;
  591. unsigned int i;
  592. for (i = 0; i < ARRAY_SIZE(hdmi_rec_n_table) - 1; i++) {
  593. if (clock == hdmi_rec_n_table[i].clock)
  594. break;
  595. }
  596. recommended = hdmi_rec_n_table + i;
  597. switch (freq) {
  598. case 32000:
  599. return recommended->n[0];
  600. case 44100:
  601. return recommended->n[1];
  602. case 48000:
  603. return recommended->n[2];
  604. case 88200:
  605. return recommended->n[1] * 2;
  606. case 96000:
  607. return recommended->n[2] * 2;
  608. case 176400:
  609. return recommended->n[1] * 4;
  610. case 192000:
  611. return recommended->n[2] * 4;
  612. default:
  613. return (128 * freq) / 1000;
  614. }
  615. }
  616. static unsigned int hdmi_mode_clock_to_hz(unsigned int clock)
  617. {
  618. switch (clock) {
  619. case 25175:
  620. return 25174825; /* 25.2/1.001 MHz */
  621. case 74176:
  622. return 74175824; /* 74.25/1.001 MHz */
  623. case 148352:
  624. return 148351648; /* 148.5/1.001 MHz */
  625. case 296703:
  626. return 296703297; /* 297/1.001 MHz */
  627. default:
  628. return clock * 1000;
  629. }
  630. }
  631. static unsigned int hdmi_expected_cts(unsigned int audio_sample_rate,
  632. unsigned int tmds_clock, unsigned int n)
  633. {
  634. return DIV_ROUND_CLOSEST_ULL((u64)hdmi_mode_clock_to_hz(tmds_clock) * n,
  635. 128 * audio_sample_rate);
  636. }
  637. static void do_hdmi_hw_aud_set_ncts(struct mtk_hdmi *hdmi, unsigned int n,
  638. unsigned int cts)
  639. {
  640. unsigned char val[NCTS_BYTES];
  641. int i;
  642. mtk_hdmi_write(hdmi, GRL_NCTS, 0);
  643. mtk_hdmi_write(hdmi, GRL_NCTS, 0);
  644. mtk_hdmi_write(hdmi, GRL_NCTS, 0);
  645. memset(val, 0, sizeof(val));
  646. val[0] = (cts >> 24) & 0xff;
  647. val[1] = (cts >> 16) & 0xff;
  648. val[2] = (cts >> 8) & 0xff;
  649. val[3] = cts & 0xff;
  650. val[4] = (n >> 16) & 0xff;
  651. val[5] = (n >> 8) & 0xff;
  652. val[6] = n & 0xff;
  653. for (i = 0; i < NCTS_BYTES; i++)
  654. mtk_hdmi_write(hdmi, GRL_NCTS, val[i]);
  655. }
  656. static void mtk_hdmi_hw_aud_set_ncts(struct mtk_hdmi *hdmi,
  657. unsigned int sample_rate,
  658. unsigned int clock)
  659. {
  660. unsigned int n, cts;
  661. n = hdmi_recommended_n(sample_rate, clock);
  662. cts = hdmi_expected_cts(sample_rate, clock, n);
  663. dev_dbg(hdmi->dev, "%s: sample_rate=%u, clock=%d, cts=%u, n=%u\n",
  664. __func__, sample_rate, clock, n, cts);
  665. mtk_hdmi_mask(hdmi, DUMMY_304, AUDIO_I2S_NCTS_SEL_64,
  666. AUDIO_I2S_NCTS_SEL);
  667. do_hdmi_hw_aud_set_ncts(hdmi, n, cts);
  668. }
  669. static u8 mtk_hdmi_aud_get_chnl_count(enum hdmi_aud_channel_type channel_type)
  670. {
  671. switch (channel_type) {
  672. case HDMI_AUD_CHAN_TYPE_1_0:
  673. case HDMI_AUD_CHAN_TYPE_1_1:
  674. case HDMI_AUD_CHAN_TYPE_2_0:
  675. return 2;
  676. case HDMI_AUD_CHAN_TYPE_2_1:
  677. case HDMI_AUD_CHAN_TYPE_3_0:
  678. return 3;
  679. case HDMI_AUD_CHAN_TYPE_3_1:
  680. case HDMI_AUD_CHAN_TYPE_4_0:
  681. case HDMI_AUD_CHAN_TYPE_3_0_LRS:
  682. return 4;
  683. case HDMI_AUD_CHAN_TYPE_4_1:
  684. case HDMI_AUD_CHAN_TYPE_5_0:
  685. case HDMI_AUD_CHAN_TYPE_3_1_LRS:
  686. case HDMI_AUD_CHAN_TYPE_4_0_CLRS:
  687. return 5;
  688. case HDMI_AUD_CHAN_TYPE_5_1:
  689. case HDMI_AUD_CHAN_TYPE_6_0:
  690. case HDMI_AUD_CHAN_TYPE_4_1_CLRS:
  691. case HDMI_AUD_CHAN_TYPE_6_0_CS:
  692. case HDMI_AUD_CHAN_TYPE_6_0_CH:
  693. case HDMI_AUD_CHAN_TYPE_6_0_OH:
  694. case HDMI_AUD_CHAN_TYPE_6_0_CHR:
  695. return 6;
  696. case HDMI_AUD_CHAN_TYPE_6_1:
  697. case HDMI_AUD_CHAN_TYPE_6_1_CS:
  698. case HDMI_AUD_CHAN_TYPE_6_1_CH:
  699. case HDMI_AUD_CHAN_TYPE_6_1_OH:
  700. case HDMI_AUD_CHAN_TYPE_6_1_CHR:
  701. case HDMI_AUD_CHAN_TYPE_7_0:
  702. case HDMI_AUD_CHAN_TYPE_7_0_LH_RH:
  703. case HDMI_AUD_CHAN_TYPE_7_0_LSR_RSR:
  704. case HDMI_AUD_CHAN_TYPE_7_0_LC_RC:
  705. case HDMI_AUD_CHAN_TYPE_7_0_LW_RW:
  706. case HDMI_AUD_CHAN_TYPE_7_0_LSD_RSD:
  707. case HDMI_AUD_CHAN_TYPE_7_0_LSS_RSS:
  708. case HDMI_AUD_CHAN_TYPE_7_0_LHS_RHS:
  709. case HDMI_AUD_CHAN_TYPE_7_0_CS_CH:
  710. case HDMI_AUD_CHAN_TYPE_7_0_CS_OH:
  711. case HDMI_AUD_CHAN_TYPE_7_0_CS_CHR:
  712. case HDMI_AUD_CHAN_TYPE_7_0_CH_OH:
  713. case HDMI_AUD_CHAN_TYPE_7_0_CH_CHR:
  714. case HDMI_AUD_CHAN_TYPE_7_0_OH_CHR:
  715. case HDMI_AUD_CHAN_TYPE_7_0_LSS_RSS_LSR_RSR:
  716. case HDMI_AUD_CHAN_TYPE_8_0_LH_RH_CS:
  717. return 7;
  718. case HDMI_AUD_CHAN_TYPE_7_1:
  719. case HDMI_AUD_CHAN_TYPE_7_1_LH_RH:
  720. case HDMI_AUD_CHAN_TYPE_7_1_LSR_RSR:
  721. case HDMI_AUD_CHAN_TYPE_7_1_LC_RC:
  722. case HDMI_AUD_CHAN_TYPE_7_1_LW_RW:
  723. case HDMI_AUD_CHAN_TYPE_7_1_LSD_RSD:
  724. case HDMI_AUD_CHAN_TYPE_7_1_LSS_RSS:
  725. case HDMI_AUD_CHAN_TYPE_7_1_LHS_RHS:
  726. case HDMI_AUD_CHAN_TYPE_7_1_CS_CH:
  727. case HDMI_AUD_CHAN_TYPE_7_1_CS_OH:
  728. case HDMI_AUD_CHAN_TYPE_7_1_CS_CHR:
  729. case HDMI_AUD_CHAN_TYPE_7_1_CH_OH:
  730. case HDMI_AUD_CHAN_TYPE_7_1_CH_CHR:
  731. case HDMI_AUD_CHAN_TYPE_7_1_OH_CHR:
  732. case HDMI_AUD_CHAN_TYPE_7_1_LSS_RSS_LSR_RSR:
  733. return 8;
  734. default:
  735. return 2;
  736. }
  737. }
  738. static int mtk_hdmi_video_change_vpll(struct mtk_hdmi *hdmi, u32 clock)
  739. {
  740. unsigned long rate;
  741. int ret;
  742. /* The DPI driver already should have set TVDPLL to the correct rate */
  743. ret = clk_set_rate(hdmi->clk[MTK_HDMI_CLK_HDMI_PLL], clock);
  744. if (ret) {
  745. dev_err(hdmi->dev, "Failed to set PLL to %u Hz: %d\n", clock,
  746. ret);
  747. return ret;
  748. }
  749. rate = clk_get_rate(hdmi->clk[MTK_HDMI_CLK_HDMI_PLL]);
  750. if (DIV_ROUND_CLOSEST(rate, 1000) != DIV_ROUND_CLOSEST(clock, 1000))
  751. dev_warn(hdmi->dev, "Want PLL %u Hz, got %lu Hz\n", clock,
  752. rate);
  753. else
  754. dev_dbg(hdmi->dev, "Want PLL %u Hz, got %lu Hz\n", clock, rate);
  755. mtk_hdmi_hw_config_sys(hdmi);
  756. mtk_hdmi_hw_set_deep_color_mode(hdmi);
  757. return 0;
  758. }
  759. static void mtk_hdmi_video_set_display_mode(struct mtk_hdmi *hdmi,
  760. struct drm_display_mode *mode)
  761. {
  762. mtk_hdmi_hw_reset(hdmi);
  763. mtk_hdmi_hw_enable_notice(hdmi, true);
  764. mtk_hdmi_hw_write_int_mask(hdmi, 0xff);
  765. mtk_hdmi_hw_enable_dvi_mode(hdmi, hdmi->dvi_mode);
  766. mtk_hdmi_hw_ncts_auto_write_enable(hdmi, true);
  767. mtk_hdmi_hw_msic_setting(hdmi, mode);
  768. }
  769. static int mtk_hdmi_aud_enable_packet(struct mtk_hdmi *hdmi, bool enable)
  770. {
  771. mtk_hdmi_hw_send_aud_packet(hdmi, enable);
  772. return 0;
  773. }
  774. static int mtk_hdmi_aud_on_off_hw_ncts(struct mtk_hdmi *hdmi, bool on)
  775. {
  776. mtk_hdmi_hw_ncts_enable(hdmi, on);
  777. return 0;
  778. }
  779. static int mtk_hdmi_aud_set_input(struct mtk_hdmi *hdmi)
  780. {
  781. enum hdmi_aud_channel_type chan_type;
  782. u8 chan_count;
  783. bool dst;
  784. mtk_hdmi_hw_aud_set_channel_swap(hdmi, HDMI_AUD_SWAP_LFE_CC);
  785. mtk_hdmi_set_bits(hdmi, GRL_MIX_CTRL, MIX_CTRL_FLAT);
  786. if (hdmi->aud_param.aud_input_type == HDMI_AUD_INPUT_SPDIF &&
  787. hdmi->aud_param.aud_codec == HDMI_AUDIO_CODING_TYPE_DST) {
  788. mtk_hdmi_hw_aud_set_bit_num(hdmi, HDMI_AUDIO_SAMPLE_SIZE_24);
  789. } else if (hdmi->aud_param.aud_i2s_fmt == HDMI_I2S_MODE_LJT_24BIT) {
  790. hdmi->aud_param.aud_i2s_fmt = HDMI_I2S_MODE_LJT_16BIT;
  791. }
  792. mtk_hdmi_hw_aud_set_i2s_fmt(hdmi, hdmi->aud_param.aud_i2s_fmt);
  793. mtk_hdmi_hw_aud_set_bit_num(hdmi, HDMI_AUDIO_SAMPLE_SIZE_24);
  794. dst = ((hdmi->aud_param.aud_input_type == HDMI_AUD_INPUT_SPDIF) &&
  795. (hdmi->aud_param.aud_codec == HDMI_AUDIO_CODING_TYPE_DST));
  796. mtk_hdmi_hw_audio_config(hdmi, dst);
  797. if (hdmi->aud_param.aud_input_type == HDMI_AUD_INPUT_SPDIF)
  798. chan_type = HDMI_AUD_CHAN_TYPE_2_0;
  799. else
  800. chan_type = hdmi->aud_param.aud_input_chan_type;
  801. chan_count = mtk_hdmi_aud_get_chnl_count(chan_type);
  802. mtk_hdmi_hw_aud_set_i2s_chan_num(hdmi, chan_type, chan_count);
  803. mtk_hdmi_hw_aud_set_input_type(hdmi, hdmi->aud_param.aud_input_type);
  804. return 0;
  805. }
  806. static int mtk_hdmi_aud_set_src(struct mtk_hdmi *hdmi,
  807. struct drm_display_mode *display_mode)
  808. {
  809. unsigned int sample_rate = hdmi->aud_param.codec_params.sample_rate;
  810. mtk_hdmi_aud_on_off_hw_ncts(hdmi, false);
  811. mtk_hdmi_hw_aud_src_disable(hdmi);
  812. mtk_hdmi_clear_bits(hdmi, GRL_CFG2, CFG2_ACLK_INV);
  813. if (hdmi->aud_param.aud_input_type == HDMI_AUD_INPUT_I2S) {
  814. switch (sample_rate) {
  815. case 32000:
  816. case 44100:
  817. case 48000:
  818. case 88200:
  819. case 96000:
  820. break;
  821. default:
  822. return -EINVAL;
  823. }
  824. mtk_hdmi_hw_aud_set_mclk(hdmi, hdmi->aud_param.aud_mclk);
  825. } else {
  826. switch (sample_rate) {
  827. case 32000:
  828. case 44100:
  829. case 48000:
  830. break;
  831. default:
  832. return -EINVAL;
  833. }
  834. mtk_hdmi_hw_aud_set_mclk(hdmi, HDMI_AUD_MCLK_128FS);
  835. }
  836. mtk_hdmi_hw_aud_set_ncts(hdmi, sample_rate, display_mode->clock);
  837. mtk_hdmi_hw_aud_src_reenable(hdmi);
  838. return 0;
  839. }
  840. static int mtk_hdmi_aud_output_config(struct mtk_hdmi *hdmi,
  841. struct drm_display_mode *display_mode)
  842. {
  843. mtk_hdmi_hw_aud_mute(hdmi);
  844. mtk_hdmi_aud_enable_packet(hdmi, false);
  845. mtk_hdmi_aud_set_input(hdmi);
  846. mtk_hdmi_aud_set_src(hdmi, display_mode);
  847. mtk_hdmi_hw_aud_set_channel_status(hdmi,
  848. hdmi->aud_param.codec_params.iec.status);
  849. usleep_range(50, 100);
  850. mtk_hdmi_aud_on_off_hw_ncts(hdmi, true);
  851. mtk_hdmi_aud_enable_packet(hdmi, true);
  852. mtk_hdmi_hw_aud_unmute(hdmi);
  853. return 0;
  854. }
  855. static int mtk_hdmi_setup_avi_infoframe(struct mtk_hdmi *hdmi,
  856. struct drm_display_mode *mode)
  857. {
  858. struct hdmi_avi_infoframe frame;
  859. u8 buffer[17];
  860. ssize_t err;
  861. err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode);
  862. if (err < 0) {
  863. dev_err(hdmi->dev,
  864. "Failed to get AVI infoframe from mode: %zd\n", err);
  865. return err;
  866. }
  867. err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
  868. if (err < 0) {
  869. dev_err(hdmi->dev, "Failed to pack AVI infoframe: %zd\n", err);
  870. return err;
  871. }
  872. mtk_hdmi_hw_send_info_frame(hdmi, buffer, sizeof(buffer));
  873. return 0;
  874. }
  875. static int mtk_hdmi_setup_spd_infoframe(struct mtk_hdmi *hdmi,
  876. const char *vendor,
  877. const char *product)
  878. {
  879. struct hdmi_spd_infoframe frame;
  880. u8 buffer[29];
  881. ssize_t err;
  882. err = hdmi_spd_infoframe_init(&frame, vendor, product);
  883. if (err < 0) {
  884. dev_err(hdmi->dev, "Failed to initialize SPD infoframe: %zd\n",
  885. err);
  886. return err;
  887. }
  888. err = hdmi_spd_infoframe_pack(&frame, buffer, sizeof(buffer));
  889. if (err < 0) {
  890. dev_err(hdmi->dev, "Failed to pack SDP infoframe: %zd\n", err);
  891. return err;
  892. }
  893. mtk_hdmi_hw_send_info_frame(hdmi, buffer, sizeof(buffer));
  894. return 0;
  895. }
  896. static int mtk_hdmi_setup_audio_infoframe(struct mtk_hdmi *hdmi)
  897. {
  898. struct hdmi_audio_infoframe frame;
  899. u8 buffer[14];
  900. ssize_t err;
  901. err = hdmi_audio_infoframe_init(&frame);
  902. if (err < 0) {
  903. dev_err(hdmi->dev, "Failed to setup audio infoframe: %zd\n",
  904. err);
  905. return err;
  906. }
  907. frame.coding_type = HDMI_AUDIO_CODING_TYPE_STREAM;
  908. frame.sample_frequency = HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM;
  909. frame.sample_size = HDMI_AUDIO_SAMPLE_SIZE_STREAM;
  910. frame.channels = mtk_hdmi_aud_get_chnl_count(
  911. hdmi->aud_param.aud_input_chan_type);
  912. err = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
  913. if (err < 0) {
  914. dev_err(hdmi->dev, "Failed to pack audio infoframe: %zd\n",
  915. err);
  916. return err;
  917. }
  918. mtk_hdmi_hw_send_info_frame(hdmi, buffer, sizeof(buffer));
  919. return 0;
  920. }
  921. static int mtk_hdmi_setup_vendor_specific_infoframe(struct mtk_hdmi *hdmi,
  922. struct drm_display_mode *mode)
  923. {
  924. struct hdmi_vendor_infoframe frame;
  925. u8 buffer[10];
  926. ssize_t err;
  927. err = drm_hdmi_vendor_infoframe_from_display_mode(&frame, mode);
  928. if (err) {
  929. dev_err(hdmi->dev,
  930. "Failed to get vendor infoframe from mode: %zd\n", err);
  931. return err;
  932. }
  933. err = hdmi_vendor_infoframe_pack(&frame, buffer, sizeof(buffer));
  934. if (err < 0) {
  935. dev_err(hdmi->dev, "Failed to pack vendor infoframe: %zd\n",
  936. err);
  937. return err;
  938. }
  939. mtk_hdmi_hw_send_info_frame(hdmi, buffer, sizeof(buffer));
  940. return 0;
  941. }
  942. static int mtk_hdmi_output_init(struct mtk_hdmi *hdmi)
  943. {
  944. struct hdmi_audio_param *aud_param = &hdmi->aud_param;
  945. hdmi->csp = HDMI_COLORSPACE_RGB;
  946. aud_param->aud_codec = HDMI_AUDIO_CODING_TYPE_PCM;
  947. aud_param->aud_sampe_size = HDMI_AUDIO_SAMPLE_SIZE_16;
  948. aud_param->aud_input_type = HDMI_AUD_INPUT_I2S;
  949. aud_param->aud_i2s_fmt = HDMI_I2S_MODE_I2S_24BIT;
  950. aud_param->aud_mclk = HDMI_AUD_MCLK_128FS;
  951. aud_param->aud_input_chan_type = HDMI_AUD_CHAN_TYPE_2_0;
  952. return 0;
  953. }
  954. static void mtk_hdmi_audio_enable(struct mtk_hdmi *hdmi)
  955. {
  956. mtk_hdmi_aud_enable_packet(hdmi, true);
  957. hdmi->audio_enable = true;
  958. }
  959. static void mtk_hdmi_audio_disable(struct mtk_hdmi *hdmi)
  960. {
  961. mtk_hdmi_aud_enable_packet(hdmi, false);
  962. hdmi->audio_enable = false;
  963. }
  964. static int mtk_hdmi_audio_set_param(struct mtk_hdmi *hdmi,
  965. struct hdmi_audio_param *param)
  966. {
  967. if (!hdmi->audio_enable) {
  968. dev_err(hdmi->dev, "hdmi audio is in disable state!\n");
  969. return -EINVAL;
  970. }
  971. dev_dbg(hdmi->dev, "codec:%d, input:%d, channel:%d, fs:%d\n",
  972. param->aud_codec, param->aud_input_type,
  973. param->aud_input_chan_type, param->codec_params.sample_rate);
  974. memcpy(&hdmi->aud_param, param, sizeof(*param));
  975. return mtk_hdmi_aud_output_config(hdmi, &hdmi->mode);
  976. }
  977. static int mtk_hdmi_output_set_display_mode(struct mtk_hdmi *hdmi,
  978. struct drm_display_mode *mode)
  979. {
  980. int ret;
  981. mtk_hdmi_hw_vid_black(hdmi, true);
  982. mtk_hdmi_hw_aud_mute(hdmi);
  983. mtk_hdmi_hw_send_av_mute(hdmi);
  984. phy_power_off(hdmi->phy);
  985. ret = mtk_hdmi_video_change_vpll(hdmi,
  986. mode->clock * 1000);
  987. if (ret) {
  988. dev_err(hdmi->dev, "Failed to set vpll: %d\n", ret);
  989. return ret;
  990. }
  991. mtk_hdmi_video_set_display_mode(hdmi, mode);
  992. phy_power_on(hdmi->phy);
  993. mtk_hdmi_aud_output_config(hdmi, mode);
  994. mtk_hdmi_hw_vid_black(hdmi, false);
  995. mtk_hdmi_hw_aud_unmute(hdmi);
  996. mtk_hdmi_hw_send_av_unmute(hdmi);
  997. return 0;
  998. }
  999. static const char * const mtk_hdmi_clk_names[MTK_HDMI_CLK_COUNT] = {
  1000. [MTK_HDMI_CLK_HDMI_PIXEL] = "pixel",
  1001. [MTK_HDMI_CLK_HDMI_PLL] = "pll",
  1002. [MTK_HDMI_CLK_AUD_BCLK] = "bclk",
  1003. [MTK_HDMI_CLK_AUD_SPDIF] = "spdif",
  1004. };
  1005. static int mtk_hdmi_get_all_clk(struct mtk_hdmi *hdmi,
  1006. struct device_node *np)
  1007. {
  1008. int i;
  1009. for (i = 0; i < ARRAY_SIZE(mtk_hdmi_clk_names); i++) {
  1010. hdmi->clk[i] = of_clk_get_by_name(np,
  1011. mtk_hdmi_clk_names[i]);
  1012. if (IS_ERR(hdmi->clk[i]))
  1013. return PTR_ERR(hdmi->clk[i]);
  1014. }
  1015. return 0;
  1016. }
  1017. static int mtk_hdmi_clk_enable_audio(struct mtk_hdmi *hdmi)
  1018. {
  1019. int ret;
  1020. ret = clk_prepare_enable(hdmi->clk[MTK_HDMI_CLK_AUD_BCLK]);
  1021. if (ret)
  1022. return ret;
  1023. ret = clk_prepare_enable(hdmi->clk[MTK_HDMI_CLK_AUD_SPDIF]);
  1024. if (ret)
  1025. goto err;
  1026. return 0;
  1027. err:
  1028. clk_disable_unprepare(hdmi->clk[MTK_HDMI_CLK_AUD_BCLK]);
  1029. return ret;
  1030. }
  1031. static void mtk_hdmi_clk_disable_audio(struct mtk_hdmi *hdmi)
  1032. {
  1033. clk_disable_unprepare(hdmi->clk[MTK_HDMI_CLK_AUD_BCLK]);
  1034. clk_disable_unprepare(hdmi->clk[MTK_HDMI_CLK_AUD_SPDIF]);
  1035. }
  1036. static enum drm_connector_status hdmi_conn_detect(struct drm_connector *conn,
  1037. bool force)
  1038. {
  1039. struct mtk_hdmi *hdmi = hdmi_ctx_from_conn(conn);
  1040. return mtk_cec_hpd_high(hdmi->cec_dev) ?
  1041. connector_status_connected : connector_status_disconnected;
  1042. }
  1043. static void hdmi_conn_destroy(struct drm_connector *conn)
  1044. {
  1045. struct mtk_hdmi *hdmi = hdmi_ctx_from_conn(conn);
  1046. mtk_cec_set_hpd_event(hdmi->cec_dev, NULL, NULL);
  1047. drm_connector_cleanup(conn);
  1048. }
  1049. static int mtk_hdmi_conn_get_modes(struct drm_connector *conn)
  1050. {
  1051. struct mtk_hdmi *hdmi = hdmi_ctx_from_conn(conn);
  1052. struct edid *edid;
  1053. int ret;
  1054. if (!hdmi->ddc_adpt)
  1055. return -ENODEV;
  1056. edid = drm_get_edid(conn, hdmi->ddc_adpt);
  1057. if (!edid)
  1058. return -ENODEV;
  1059. hdmi->dvi_mode = !drm_detect_monitor_audio(edid);
  1060. drm_mode_connector_update_edid_property(conn, edid);
  1061. ret = drm_add_edid_modes(conn, edid);
  1062. drm_edid_to_eld(conn, edid);
  1063. kfree(edid);
  1064. return ret;
  1065. }
  1066. static int mtk_hdmi_conn_mode_valid(struct drm_connector *conn,
  1067. struct drm_display_mode *mode)
  1068. {
  1069. struct mtk_hdmi *hdmi = hdmi_ctx_from_conn(conn);
  1070. dev_dbg(hdmi->dev, "xres=%d, yres=%d, refresh=%d, intl=%d clock=%d\n",
  1071. mode->hdisplay, mode->vdisplay, mode->vrefresh,
  1072. !!(mode->flags & DRM_MODE_FLAG_INTERLACE), mode->clock * 1000);
  1073. if (hdmi->bridge.next) {
  1074. struct drm_display_mode adjusted_mode;
  1075. drm_mode_copy(&adjusted_mode, mode);
  1076. if (!drm_bridge_mode_fixup(hdmi->bridge.next, mode,
  1077. &adjusted_mode))
  1078. return MODE_BAD;
  1079. }
  1080. if (mode->clock < 27000)
  1081. return MODE_CLOCK_LOW;
  1082. if (mode->clock > 297000)
  1083. return MODE_CLOCK_HIGH;
  1084. return drm_mode_validate_size(mode, 0x1fff, 0x1fff);
  1085. }
  1086. static struct drm_encoder *mtk_hdmi_conn_best_enc(struct drm_connector *conn)
  1087. {
  1088. struct mtk_hdmi *hdmi = hdmi_ctx_from_conn(conn);
  1089. return hdmi->bridge.encoder;
  1090. }
  1091. static const struct drm_connector_funcs mtk_hdmi_connector_funcs = {
  1092. /* .dpms = drm_atomic_helper_connector_dpms, */
  1093. .detect = hdmi_conn_detect,
  1094. .fill_modes = drm_helper_probe_single_connector_modes,
  1095. .destroy = hdmi_conn_destroy,
  1096. .reset = drm_atomic_helper_connector_reset,
  1097. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  1098. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  1099. };
  1100. static const struct drm_connector_helper_funcs
  1101. mtk_hdmi_connector_helper_funcs = {
  1102. .get_modes = mtk_hdmi_conn_get_modes,
  1103. .mode_valid = mtk_hdmi_conn_mode_valid,
  1104. .best_encoder = mtk_hdmi_conn_best_enc,
  1105. };
  1106. static void mtk_hdmi_hpd_event(bool hpd, struct device *dev)
  1107. {
  1108. struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
  1109. if (hdmi && hdmi->bridge.encoder && hdmi->bridge.encoder->dev)
  1110. drm_helper_hpd_irq_event(hdmi->bridge.encoder->dev);
  1111. }
  1112. /*
  1113. * Bridge callbacks
  1114. */
  1115. static int mtk_hdmi_bridge_attach(struct drm_bridge *bridge)
  1116. {
  1117. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  1118. int ret;
  1119. ret = drm_connector_init(bridge->encoder->dev, &hdmi->conn,
  1120. &mtk_hdmi_connector_funcs,
  1121. DRM_MODE_CONNECTOR_HDMIA);
  1122. if (ret) {
  1123. dev_err(hdmi->dev, "Failed to initialize connector: %d\n", ret);
  1124. return ret;
  1125. }
  1126. drm_connector_helper_add(&hdmi->conn, &mtk_hdmi_connector_helper_funcs);
  1127. hdmi->conn.polled = DRM_CONNECTOR_POLL_HPD;
  1128. hdmi->conn.interlace_allowed = true;
  1129. hdmi->conn.doublescan_allowed = false;
  1130. ret = drm_mode_connector_attach_encoder(&hdmi->conn,
  1131. bridge->encoder);
  1132. if (ret) {
  1133. dev_err(hdmi->dev,
  1134. "Failed to attach connector to encoder: %d\n", ret);
  1135. return ret;
  1136. }
  1137. if (bridge->next) {
  1138. bridge->next->encoder = bridge->encoder;
  1139. ret = drm_bridge_attach(bridge->encoder, bridge->next, NULL);
  1140. if (ret) {
  1141. dev_err(hdmi->dev,
  1142. "Failed to attach external bridge: %d\n", ret);
  1143. return ret;
  1144. }
  1145. }
  1146. mtk_cec_set_hpd_event(hdmi->cec_dev, mtk_hdmi_hpd_event, hdmi->dev);
  1147. return 0;
  1148. }
  1149. static bool mtk_hdmi_bridge_mode_fixup(struct drm_bridge *bridge,
  1150. const struct drm_display_mode *mode,
  1151. struct drm_display_mode *adjusted_mode)
  1152. {
  1153. return true;
  1154. }
  1155. static void mtk_hdmi_bridge_disable(struct drm_bridge *bridge)
  1156. {
  1157. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  1158. if (!hdmi->enabled)
  1159. return;
  1160. phy_power_off(hdmi->phy);
  1161. clk_disable_unprepare(hdmi->clk[MTK_HDMI_CLK_HDMI_PIXEL]);
  1162. clk_disable_unprepare(hdmi->clk[MTK_HDMI_CLK_HDMI_PLL]);
  1163. hdmi->enabled = false;
  1164. }
  1165. static void mtk_hdmi_bridge_post_disable(struct drm_bridge *bridge)
  1166. {
  1167. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  1168. if (!hdmi->powered)
  1169. return;
  1170. mtk_hdmi_hw_1p4_version_enable(hdmi, true);
  1171. mtk_hdmi_hw_make_reg_writable(hdmi, false);
  1172. hdmi->powered = false;
  1173. }
  1174. static void mtk_hdmi_bridge_mode_set(struct drm_bridge *bridge,
  1175. struct drm_display_mode *mode,
  1176. struct drm_display_mode *adjusted_mode)
  1177. {
  1178. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  1179. dev_dbg(hdmi->dev, "cur info: name:%s, hdisplay:%d\n",
  1180. adjusted_mode->name, adjusted_mode->hdisplay);
  1181. dev_dbg(hdmi->dev, "hsync_start:%d,hsync_end:%d, htotal:%d",
  1182. adjusted_mode->hsync_start, adjusted_mode->hsync_end,
  1183. adjusted_mode->htotal);
  1184. dev_dbg(hdmi->dev, "hskew:%d, vdisplay:%d\n",
  1185. adjusted_mode->hskew, adjusted_mode->vdisplay);
  1186. dev_dbg(hdmi->dev, "vsync_start:%d, vsync_end:%d, vtotal:%d",
  1187. adjusted_mode->vsync_start, adjusted_mode->vsync_end,
  1188. adjusted_mode->vtotal);
  1189. dev_dbg(hdmi->dev, "vscan:%d, flag:%d\n",
  1190. adjusted_mode->vscan, adjusted_mode->flags);
  1191. drm_mode_copy(&hdmi->mode, adjusted_mode);
  1192. }
  1193. static void mtk_hdmi_bridge_pre_enable(struct drm_bridge *bridge)
  1194. {
  1195. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  1196. mtk_hdmi_hw_make_reg_writable(hdmi, true);
  1197. mtk_hdmi_hw_1p4_version_enable(hdmi, true);
  1198. hdmi->powered = true;
  1199. }
  1200. static void mtk_hdmi_send_infoframe(struct mtk_hdmi *hdmi,
  1201. struct drm_display_mode *mode)
  1202. {
  1203. mtk_hdmi_setup_audio_infoframe(hdmi);
  1204. mtk_hdmi_setup_avi_infoframe(hdmi, mode);
  1205. mtk_hdmi_setup_spd_infoframe(hdmi, "mediatek", "On-chip HDMI");
  1206. if (mode->flags & DRM_MODE_FLAG_3D_MASK)
  1207. mtk_hdmi_setup_vendor_specific_infoframe(hdmi, mode);
  1208. }
  1209. static void mtk_hdmi_bridge_enable(struct drm_bridge *bridge)
  1210. {
  1211. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  1212. mtk_hdmi_output_set_display_mode(hdmi, &hdmi->mode);
  1213. clk_prepare_enable(hdmi->clk[MTK_HDMI_CLK_HDMI_PLL]);
  1214. clk_prepare_enable(hdmi->clk[MTK_HDMI_CLK_HDMI_PIXEL]);
  1215. phy_power_on(hdmi->phy);
  1216. mtk_hdmi_send_infoframe(hdmi, &hdmi->mode);
  1217. hdmi->enabled = true;
  1218. }
  1219. static const struct drm_bridge_funcs mtk_hdmi_bridge_funcs = {
  1220. .attach = mtk_hdmi_bridge_attach,
  1221. .mode_fixup = mtk_hdmi_bridge_mode_fixup,
  1222. .disable = mtk_hdmi_bridge_disable,
  1223. .post_disable = mtk_hdmi_bridge_post_disable,
  1224. .mode_set = mtk_hdmi_bridge_mode_set,
  1225. .pre_enable = mtk_hdmi_bridge_pre_enable,
  1226. .enable = mtk_hdmi_bridge_enable,
  1227. };
  1228. static int mtk_hdmi_dt_parse_pdata(struct mtk_hdmi *hdmi,
  1229. struct platform_device *pdev)
  1230. {
  1231. struct device *dev = &pdev->dev;
  1232. struct device_node *np = dev->of_node;
  1233. struct device_node *cec_np, *port, *ep, *remote, *i2c_np;
  1234. struct platform_device *cec_pdev;
  1235. struct regmap *regmap;
  1236. struct resource *mem;
  1237. int ret;
  1238. ret = mtk_hdmi_get_all_clk(hdmi, np);
  1239. if (ret) {
  1240. dev_err(dev, "Failed to get clocks: %d\n", ret);
  1241. return ret;
  1242. }
  1243. /* The CEC module handles HDMI hotplug detection */
  1244. cec_np = of_find_compatible_node(np->parent, NULL,
  1245. "mediatek,mt8173-cec");
  1246. if (!cec_np) {
  1247. dev_err(dev, "Failed to find CEC node\n");
  1248. return -EINVAL;
  1249. }
  1250. cec_pdev = of_find_device_by_node(cec_np);
  1251. if (!cec_pdev) {
  1252. dev_err(hdmi->dev, "Waiting for CEC device %s\n",
  1253. cec_np->full_name);
  1254. return -EPROBE_DEFER;
  1255. }
  1256. hdmi->cec_dev = &cec_pdev->dev;
  1257. /*
  1258. * The mediatek,syscon-hdmi property contains a phandle link to the
  1259. * MMSYS_CONFIG device and the register offset of the HDMI_SYS_CFG
  1260. * registers it contains.
  1261. */
  1262. regmap = syscon_regmap_lookup_by_phandle(np, "mediatek,syscon-hdmi");
  1263. ret = of_property_read_u32_index(np, "mediatek,syscon-hdmi", 1,
  1264. &hdmi->sys_offset);
  1265. if (IS_ERR(regmap))
  1266. ret = PTR_ERR(regmap);
  1267. if (ret) {
  1268. ret = PTR_ERR(regmap);
  1269. dev_err(dev,
  1270. "Failed to get system configuration registers: %d\n",
  1271. ret);
  1272. goto put_device;
  1273. }
  1274. hdmi->sys_regmap = regmap;
  1275. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1276. hdmi->regs = devm_ioremap_resource(dev, mem);
  1277. if (IS_ERR(hdmi->regs)) {
  1278. ret = PTR_ERR(hdmi->regs);
  1279. goto put_device;
  1280. }
  1281. remote = of_graph_get_remote_node(np, 1, 0);
  1282. if (!remote) {
  1283. ret = -EINVAL;
  1284. goto put_device;
  1285. }
  1286. ep = of_get_child_by_name(port, "endpoint");
  1287. if (!ep) {
  1288. dev_err(dev, "Missing endpoint node in port %s\n",
  1289. port->full_name);
  1290. of_node_put(port);
  1291. return -EINVAL;
  1292. }
  1293. of_node_put(port);
  1294. remote = of_graph_get_remote_port_parent(ep);
  1295. if (!remote) {
  1296. dev_err(dev, "Missing connector/bridge node for endpoint %s\n",
  1297. ep->full_name);
  1298. of_node_put(ep);
  1299. return -EINVAL;
  1300. }
  1301. of_node_put(ep);
  1302. if (!of_device_is_compatible(remote, "hdmi-connector")) {
  1303. hdmi->bridge.next = of_drm_find_bridge(remote);
  1304. if (!hdmi->bridge.next) {
  1305. dev_err(dev, "Waiting for external bridge\n");
  1306. of_node_put(remote);
  1307. ret = -EPROBE_DEFER;
  1308. goto put_device;
  1309. }
  1310. }
  1311. i2c_np = of_parse_phandle(remote, "ddc-i2c-bus", 0);
  1312. if (!i2c_np) {
  1313. dev_err(dev, "Failed to find ddc-i2c-bus node in %s\n",
  1314. remote->full_name);
  1315. of_node_put(remote);
  1316. ret = -EINVAL;
  1317. goto put_device;
  1318. }
  1319. of_node_put(remote);
  1320. hdmi->ddc_adpt = of_find_i2c_adapter_by_node(i2c_np);
  1321. if (!hdmi->ddc_adpt) {
  1322. dev_err(dev, "Failed to get ddc i2c adapter by node\n");
  1323. ret = -EINVAL;
  1324. goto put_device;
  1325. }
  1326. return 0;
  1327. put_device:
  1328. put_device(hdmi->cec_dev);
  1329. return ret;
  1330. }
  1331. /*
  1332. * HDMI audio codec callbacks
  1333. */
  1334. static int mtk_hdmi_audio_hw_params(struct device *dev, void *data,
  1335. struct hdmi_codec_daifmt *daifmt,
  1336. struct hdmi_codec_params *params)
  1337. {
  1338. struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
  1339. struct hdmi_audio_param hdmi_params;
  1340. unsigned int chan = params->cea.channels;
  1341. dev_dbg(hdmi->dev, "%s: %u Hz, %d bit, %d channels\n", __func__,
  1342. params->sample_rate, params->sample_width, chan);
  1343. if (!hdmi->bridge.encoder)
  1344. return -ENODEV;
  1345. switch (chan) {
  1346. case 2:
  1347. hdmi_params.aud_input_chan_type = HDMI_AUD_CHAN_TYPE_2_0;
  1348. break;
  1349. case 4:
  1350. hdmi_params.aud_input_chan_type = HDMI_AUD_CHAN_TYPE_4_0;
  1351. break;
  1352. case 6:
  1353. hdmi_params.aud_input_chan_type = HDMI_AUD_CHAN_TYPE_5_1;
  1354. break;
  1355. case 8:
  1356. hdmi_params.aud_input_chan_type = HDMI_AUD_CHAN_TYPE_7_1;
  1357. break;
  1358. default:
  1359. dev_err(hdmi->dev, "channel[%d] not supported!\n", chan);
  1360. return -EINVAL;
  1361. }
  1362. switch (params->sample_rate) {
  1363. case 32000:
  1364. case 44100:
  1365. case 48000:
  1366. case 88200:
  1367. case 96000:
  1368. case 176400:
  1369. case 192000:
  1370. break;
  1371. default:
  1372. dev_err(hdmi->dev, "rate[%d] not supported!\n",
  1373. params->sample_rate);
  1374. return -EINVAL;
  1375. }
  1376. switch (daifmt->fmt) {
  1377. case HDMI_I2S:
  1378. hdmi_params.aud_codec = HDMI_AUDIO_CODING_TYPE_PCM;
  1379. hdmi_params.aud_sampe_size = HDMI_AUDIO_SAMPLE_SIZE_16;
  1380. hdmi_params.aud_input_type = HDMI_AUD_INPUT_I2S;
  1381. hdmi_params.aud_i2s_fmt = HDMI_I2S_MODE_I2S_24BIT;
  1382. hdmi_params.aud_mclk = HDMI_AUD_MCLK_128FS;
  1383. break;
  1384. default:
  1385. dev_err(hdmi->dev, "%s: Invalid DAI format %d\n", __func__,
  1386. daifmt->fmt);
  1387. return -EINVAL;
  1388. }
  1389. memcpy(&hdmi_params.codec_params, params,
  1390. sizeof(hdmi_params.codec_params));
  1391. mtk_hdmi_audio_set_param(hdmi, &hdmi_params);
  1392. return 0;
  1393. }
  1394. static int mtk_hdmi_audio_startup(struct device *dev, void *data)
  1395. {
  1396. struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
  1397. dev_dbg(dev, "%s\n", __func__);
  1398. mtk_hdmi_audio_enable(hdmi);
  1399. return 0;
  1400. }
  1401. static void mtk_hdmi_audio_shutdown(struct device *dev, void *data)
  1402. {
  1403. struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
  1404. dev_dbg(dev, "%s\n", __func__);
  1405. mtk_hdmi_audio_disable(hdmi);
  1406. }
  1407. static int
  1408. mtk_hdmi_audio_digital_mute(struct device *dev, void *data, bool enable)
  1409. {
  1410. struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
  1411. dev_dbg(dev, "%s(%d)\n", __func__, enable);
  1412. if (enable)
  1413. mtk_hdmi_hw_aud_mute(hdmi);
  1414. else
  1415. mtk_hdmi_hw_aud_unmute(hdmi);
  1416. return 0;
  1417. }
  1418. static int mtk_hdmi_audio_get_eld(struct device *dev, void *data, uint8_t *buf, size_t len)
  1419. {
  1420. struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
  1421. dev_dbg(dev, "%s\n", __func__);
  1422. memcpy(buf, hdmi->conn.eld, min(sizeof(hdmi->conn.eld), len));
  1423. return 0;
  1424. }
  1425. static const struct hdmi_codec_ops mtk_hdmi_audio_codec_ops = {
  1426. .hw_params = mtk_hdmi_audio_hw_params,
  1427. .audio_startup = mtk_hdmi_audio_startup,
  1428. .audio_shutdown = mtk_hdmi_audio_shutdown,
  1429. .digital_mute = mtk_hdmi_audio_digital_mute,
  1430. .get_eld = mtk_hdmi_audio_get_eld,
  1431. };
  1432. static void mtk_hdmi_register_audio_driver(struct device *dev)
  1433. {
  1434. struct hdmi_codec_pdata codec_data = {
  1435. .ops = &mtk_hdmi_audio_codec_ops,
  1436. .max_i2s_channels = 2,
  1437. .i2s = 1,
  1438. };
  1439. struct platform_device *pdev;
  1440. pdev = platform_device_register_data(dev, HDMI_CODEC_DRV_NAME,
  1441. PLATFORM_DEVID_AUTO, &codec_data,
  1442. sizeof(codec_data));
  1443. if (IS_ERR(pdev))
  1444. return;
  1445. DDPINFO("%s driver bound to HDMI\n", HDMI_CODEC_DRV_NAME);
  1446. }
  1447. static int mtk_drm_hdmi_probe(struct platform_device *pdev)
  1448. {
  1449. struct mtk_hdmi *hdmi;
  1450. struct device *dev = &pdev->dev;
  1451. int ret;
  1452. hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
  1453. if (!hdmi)
  1454. return -ENOMEM;
  1455. hdmi->dev = dev;
  1456. ret = mtk_hdmi_dt_parse_pdata(hdmi, pdev);
  1457. if (ret)
  1458. return ret;
  1459. hdmi->phy = devm_phy_get(dev, "hdmi");
  1460. if (IS_ERR(hdmi->phy)) {
  1461. ret = PTR_ERR(hdmi->phy);
  1462. dev_err(dev, "Failed to get HDMI PHY: %d\n", ret);
  1463. return ret;
  1464. }
  1465. platform_set_drvdata(pdev, hdmi);
  1466. ret = mtk_hdmi_output_init(hdmi);
  1467. if (ret) {
  1468. dev_err(dev, "Failed to initialize hdmi output\n");
  1469. return ret;
  1470. }
  1471. mtk_hdmi_register_audio_driver(dev);
  1472. hdmi->bridge.funcs = &mtk_hdmi_bridge_funcs;
  1473. hdmi->bridge.of_node = pdev->dev.of_node;
  1474. ret = drm_bridge_add(&hdmi->bridge);
  1475. if (ret) {
  1476. dev_err(dev, "failed to add bridge, ret = %d\n", ret);
  1477. return ret;
  1478. }
  1479. ret = mtk_hdmi_clk_enable_audio(hdmi);
  1480. if (ret) {
  1481. dev_err(dev, "Failed to enable audio clocks: %d\n", ret);
  1482. goto err_bridge_remove;
  1483. }
  1484. dev_dbg(dev, "mediatek hdmi probe success\n");
  1485. return 0;
  1486. err_bridge_remove:
  1487. drm_bridge_remove(&hdmi->bridge);
  1488. return ret;
  1489. }
  1490. static int mtk_drm_hdmi_remove(struct platform_device *pdev)
  1491. {
  1492. struct mtk_hdmi *hdmi = platform_get_drvdata(pdev);
  1493. drm_bridge_remove(&hdmi->bridge);
  1494. mtk_hdmi_clk_disable_audio(hdmi);
  1495. return 0;
  1496. }
  1497. #ifdef CONFIG_PM_SLEEP
  1498. static int mtk_hdmi_suspend(struct device *dev)
  1499. {
  1500. struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
  1501. mtk_hdmi_clk_disable_audio(hdmi);
  1502. dev_dbg(dev, "hdmi suspend success!\n");
  1503. return 0;
  1504. }
  1505. static int mtk_hdmi_resume(struct device *dev)
  1506. {
  1507. struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
  1508. int ret = 0;
  1509. ret = mtk_hdmi_clk_enable_audio(hdmi);
  1510. if (ret) {
  1511. dev_err(dev, "hdmi resume failed!\n");
  1512. return ret;
  1513. }
  1514. dev_dbg(dev, "hdmi resume success!\n");
  1515. return 0;
  1516. }
  1517. #endif
  1518. static SIMPLE_DEV_PM_OPS(mtk_hdmi_pm_ops,
  1519. mtk_hdmi_suspend, mtk_hdmi_resume);
  1520. static const struct of_device_id mtk_drm_hdmi_of_ids[] = {
  1521. { .compatible = "mediatek,mt8173-hdmi", },
  1522. {}
  1523. };
  1524. static struct platform_driver mtk_hdmi_driver = {
  1525. .probe = mtk_drm_hdmi_probe,
  1526. .remove = mtk_drm_hdmi_remove,
  1527. .driver = {
  1528. .name = "mediatek-drm-hdmi",
  1529. .of_match_table = mtk_drm_hdmi_of_ids,
  1530. .pm = &mtk_hdmi_pm_ops,
  1531. },
  1532. };
  1533. static struct platform_driver * const mtk_hdmi_drivers[] = {
  1534. &mtk_hdmi_phy_driver,
  1535. &mtk_hdmi_ddc_driver,
  1536. &mtk_cec_driver,
  1537. &mtk_hdmi_driver,
  1538. };
  1539. static int __init mtk_hdmitx_init(void)
  1540. {
  1541. int ret;
  1542. int i;
  1543. for (i = 0; i < ARRAY_SIZE(mtk_hdmi_drivers); i++) {
  1544. ret = platform_driver_register(mtk_hdmi_drivers[i]);
  1545. if (ret < 0) {
  1546. DDPPR_ERR("Failed to register %s driver: %d\n",
  1547. mtk_hdmi_drivers[i]->driver.name, ret);
  1548. goto err;
  1549. }
  1550. }
  1551. return 0;
  1552. err:
  1553. while (--i >= 0)
  1554. platform_driver_unregister(mtk_hdmi_drivers[i]);
  1555. return ret;
  1556. }
  1557. static void __exit mtk_hdmitx_exit(void)
  1558. {
  1559. int i;
  1560. for (i = ARRAY_SIZE(mtk_hdmi_drivers) - 1; i >= 0; i--)
  1561. platform_driver_unregister(mtk_hdmi_drivers[i]);
  1562. }
  1563. module_init(mtk_hdmitx_init);
  1564. module_exit(mtk_hdmitx_exit);
  1565. MODULE_AUTHOR("Jie Qiu <jie.qiu@mediatek.com>");
  1566. MODULE_DESCRIPTION("MediaTek HDMI Driver");
  1567. MODULE_LICENSE("GPL v2");