mtk_hdmi.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828
  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) {
  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. 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. 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. 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_setup_audio_infoframe(hdmi);
  995. mtk_hdmi_setup_avi_infoframe(hdmi, mode);
  996. mtk_hdmi_setup_spd_infoframe(hdmi, "mediatek", "On-chip HDMI");
  997. if (mode->flags & DRM_MODE_FLAG_3D_MASK)
  998. mtk_hdmi_setup_vendor_specific_infoframe(hdmi, mode);
  999. mtk_hdmi_hw_vid_black(hdmi, false);
  1000. mtk_hdmi_hw_aud_unmute(hdmi);
  1001. mtk_hdmi_hw_send_av_unmute(hdmi);
  1002. return 0;
  1003. }
  1004. static const char * const mtk_hdmi_clk_names[MTK_HDMI_CLK_COUNT] = {
  1005. [MTK_HDMI_CLK_HDMI_PIXEL] = "pixel",
  1006. [MTK_HDMI_CLK_HDMI_PLL] = "pll",
  1007. [MTK_HDMI_CLK_AUD_BCLK] = "bclk",
  1008. [MTK_HDMI_CLK_AUD_SPDIF] = "spdif",
  1009. };
  1010. static int mtk_hdmi_get_all_clk(struct mtk_hdmi *hdmi,
  1011. struct device_node *np)
  1012. {
  1013. int i;
  1014. for (i = 0; i < ARRAY_SIZE(mtk_hdmi_clk_names); i++) {
  1015. hdmi->clk[i] = of_clk_get_by_name(np,
  1016. mtk_hdmi_clk_names[i]);
  1017. if (IS_ERR(hdmi->clk[i]))
  1018. return PTR_ERR(hdmi->clk[i]);
  1019. }
  1020. return 0;
  1021. }
  1022. static int mtk_hdmi_clk_enable_audio(struct mtk_hdmi *hdmi)
  1023. {
  1024. int ret;
  1025. ret = clk_prepare_enable(hdmi->clk[MTK_HDMI_CLK_AUD_BCLK]);
  1026. if (ret)
  1027. return ret;
  1028. ret = clk_prepare_enable(hdmi->clk[MTK_HDMI_CLK_AUD_SPDIF]);
  1029. if (ret)
  1030. goto err;
  1031. return 0;
  1032. err:
  1033. clk_disable_unprepare(hdmi->clk[MTK_HDMI_CLK_AUD_BCLK]);
  1034. return ret;
  1035. }
  1036. static void mtk_hdmi_clk_disable_audio(struct mtk_hdmi *hdmi)
  1037. {
  1038. clk_disable_unprepare(hdmi->clk[MTK_HDMI_CLK_AUD_BCLK]);
  1039. clk_disable_unprepare(hdmi->clk[MTK_HDMI_CLK_AUD_SPDIF]);
  1040. }
  1041. static enum drm_connector_status hdmi_conn_detect(struct drm_connector *conn,
  1042. bool force)
  1043. {
  1044. struct mtk_hdmi *hdmi = hdmi_ctx_from_conn(conn);
  1045. return mtk_cec_hpd_high(hdmi->cec_dev) ?
  1046. connector_status_connected : connector_status_disconnected;
  1047. }
  1048. static void hdmi_conn_destroy(struct drm_connector *conn)
  1049. {
  1050. struct mtk_hdmi *hdmi = hdmi_ctx_from_conn(conn);
  1051. mtk_cec_set_hpd_event(hdmi->cec_dev, NULL, NULL);
  1052. drm_connector_cleanup(conn);
  1053. }
  1054. static int mtk_hdmi_conn_get_modes(struct drm_connector *conn)
  1055. {
  1056. struct mtk_hdmi *hdmi = hdmi_ctx_from_conn(conn);
  1057. struct edid *edid;
  1058. int ret;
  1059. if (!hdmi->ddc_adpt)
  1060. return -ENODEV;
  1061. edid = drm_get_edid(conn, hdmi->ddc_adpt);
  1062. if (!edid)
  1063. return -ENODEV;
  1064. hdmi->dvi_mode = !drm_detect_monitor_audio(edid);
  1065. drm_mode_connector_update_edid_property(conn, edid);
  1066. ret = drm_add_edid_modes(conn, edid);
  1067. drm_edid_to_eld(conn, edid);
  1068. kfree(edid);
  1069. return ret;
  1070. }
  1071. static int mtk_hdmi_conn_mode_valid(struct drm_connector *conn,
  1072. struct drm_display_mode *mode)
  1073. {
  1074. struct mtk_hdmi *hdmi = hdmi_ctx_from_conn(conn);
  1075. dev_dbg(hdmi->dev, "xres=%d, yres=%d, refresh=%d, intl=%d clock=%d\n",
  1076. mode->hdisplay, mode->vdisplay, mode->vrefresh,
  1077. !!(mode->flags & DRM_MODE_FLAG_INTERLACE), mode->clock * 1000);
  1078. if (hdmi->bridge.next) {
  1079. struct drm_display_mode adjusted_mode;
  1080. drm_mode_copy(&adjusted_mode, mode);
  1081. if (!drm_bridge_mode_fixup(hdmi->bridge.next, mode,
  1082. &adjusted_mode))
  1083. return MODE_BAD;
  1084. }
  1085. if (mode->clock < 27000)
  1086. return MODE_CLOCK_LOW;
  1087. if (mode->clock > 297000)
  1088. return MODE_CLOCK_HIGH;
  1089. return drm_mode_validate_size(mode, 0x1fff, 0x1fff);
  1090. }
  1091. static struct drm_encoder *mtk_hdmi_conn_best_enc(struct drm_connector *conn)
  1092. {
  1093. struct mtk_hdmi *hdmi = hdmi_ctx_from_conn(conn);
  1094. return hdmi->bridge.encoder;
  1095. }
  1096. static const struct drm_connector_funcs mtk_hdmi_connector_funcs = {
  1097. .dpms = drm_atomic_helper_connector_dpms,
  1098. .detect = hdmi_conn_detect,
  1099. .fill_modes = drm_helper_probe_single_connector_modes,
  1100. .destroy = hdmi_conn_destroy,
  1101. .reset = drm_atomic_helper_connector_reset,
  1102. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  1103. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  1104. };
  1105. static const struct drm_connector_helper_funcs
  1106. mtk_hdmi_connector_helper_funcs = {
  1107. .get_modes = mtk_hdmi_conn_get_modes,
  1108. .mode_valid = mtk_hdmi_conn_mode_valid,
  1109. .best_encoder = mtk_hdmi_conn_best_enc,
  1110. };
  1111. static void mtk_hdmi_hpd_event(bool hpd, struct device *dev)
  1112. {
  1113. struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
  1114. if (hdmi && hdmi->bridge.encoder && hdmi->bridge.encoder->dev)
  1115. drm_helper_hpd_irq_event(hdmi->bridge.encoder->dev);
  1116. }
  1117. /*
  1118. * Bridge callbacks
  1119. */
  1120. static int mtk_hdmi_bridge_attach(struct drm_bridge *bridge)
  1121. {
  1122. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  1123. int ret;
  1124. ret = drm_connector_init(bridge->encoder->dev, &hdmi->conn,
  1125. &mtk_hdmi_connector_funcs,
  1126. DRM_MODE_CONNECTOR_HDMIA);
  1127. if (ret) {
  1128. dev_err(hdmi->dev, "Failed to initialize connector: %d\n", ret);
  1129. return ret;
  1130. }
  1131. drm_connector_helper_add(&hdmi->conn, &mtk_hdmi_connector_helper_funcs);
  1132. hdmi->conn.polled = DRM_CONNECTOR_POLL_HPD;
  1133. hdmi->conn.interlace_allowed = true;
  1134. hdmi->conn.doublescan_allowed = false;
  1135. ret = drm_mode_connector_attach_encoder(&hdmi->conn,
  1136. bridge->encoder);
  1137. if (ret) {
  1138. dev_err(hdmi->dev,
  1139. "Failed to attach connector to encoder: %d\n", ret);
  1140. return ret;
  1141. }
  1142. if (bridge->next) {
  1143. bridge->next->encoder = bridge->encoder;
  1144. ret = drm_bridge_attach(bridge->encoder->dev, bridge->next);
  1145. if (ret) {
  1146. dev_err(hdmi->dev,
  1147. "Failed to attach external bridge: %d\n", ret);
  1148. return ret;
  1149. }
  1150. }
  1151. mtk_cec_set_hpd_event(hdmi->cec_dev, mtk_hdmi_hpd_event, hdmi->dev);
  1152. return 0;
  1153. }
  1154. static bool mtk_hdmi_bridge_mode_fixup(struct drm_bridge *bridge,
  1155. const struct drm_display_mode *mode,
  1156. struct drm_display_mode *adjusted_mode)
  1157. {
  1158. return true;
  1159. }
  1160. static void mtk_hdmi_bridge_disable(struct drm_bridge *bridge)
  1161. {
  1162. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  1163. if (!hdmi->enabled)
  1164. return;
  1165. phy_power_off(hdmi->phy);
  1166. clk_disable_unprepare(hdmi->clk[MTK_HDMI_CLK_HDMI_PIXEL]);
  1167. clk_disable_unprepare(hdmi->clk[MTK_HDMI_CLK_HDMI_PLL]);
  1168. hdmi->enabled = false;
  1169. }
  1170. static void mtk_hdmi_bridge_post_disable(struct drm_bridge *bridge)
  1171. {
  1172. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  1173. if (!hdmi->powered)
  1174. return;
  1175. mtk_hdmi_hw_1p4_version_enable(hdmi, true);
  1176. mtk_hdmi_hw_make_reg_writable(hdmi, false);
  1177. hdmi->powered = false;
  1178. }
  1179. static void mtk_hdmi_bridge_mode_set(struct drm_bridge *bridge,
  1180. struct drm_display_mode *mode,
  1181. struct drm_display_mode *adjusted_mode)
  1182. {
  1183. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  1184. dev_dbg(hdmi->dev, "cur info: name:%s, hdisplay:%d\n",
  1185. adjusted_mode->name, adjusted_mode->hdisplay);
  1186. dev_dbg(hdmi->dev, "hsync_start:%d,hsync_end:%d, htotal:%d",
  1187. adjusted_mode->hsync_start, adjusted_mode->hsync_end,
  1188. adjusted_mode->htotal);
  1189. dev_dbg(hdmi->dev, "hskew:%d, vdisplay:%d\n",
  1190. adjusted_mode->hskew, adjusted_mode->vdisplay);
  1191. dev_dbg(hdmi->dev, "vsync_start:%d, vsync_end:%d, vtotal:%d",
  1192. adjusted_mode->vsync_start, adjusted_mode->vsync_end,
  1193. adjusted_mode->vtotal);
  1194. dev_dbg(hdmi->dev, "vscan:%d, flag:%d\n",
  1195. adjusted_mode->vscan, adjusted_mode->flags);
  1196. drm_mode_copy(&hdmi->mode, adjusted_mode);
  1197. }
  1198. static void mtk_hdmi_bridge_pre_enable(struct drm_bridge *bridge)
  1199. {
  1200. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  1201. mtk_hdmi_hw_make_reg_writable(hdmi, true);
  1202. mtk_hdmi_hw_1p4_version_enable(hdmi, true);
  1203. hdmi->powered = true;
  1204. }
  1205. static void mtk_hdmi_bridge_enable(struct drm_bridge *bridge)
  1206. {
  1207. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  1208. mtk_hdmi_output_set_display_mode(hdmi, &hdmi->mode);
  1209. clk_prepare_enable(hdmi->clk[MTK_HDMI_CLK_HDMI_PLL]);
  1210. clk_prepare_enable(hdmi->clk[MTK_HDMI_CLK_HDMI_PIXEL]);
  1211. phy_power_on(hdmi->phy);
  1212. hdmi->enabled = true;
  1213. }
  1214. static const struct drm_bridge_funcs mtk_hdmi_bridge_funcs = {
  1215. .attach = mtk_hdmi_bridge_attach,
  1216. .mode_fixup = mtk_hdmi_bridge_mode_fixup,
  1217. .disable = mtk_hdmi_bridge_disable,
  1218. .post_disable = mtk_hdmi_bridge_post_disable,
  1219. .mode_set = mtk_hdmi_bridge_mode_set,
  1220. .pre_enable = mtk_hdmi_bridge_pre_enable,
  1221. .enable = mtk_hdmi_bridge_enable,
  1222. };
  1223. static int mtk_hdmi_dt_parse_pdata(struct mtk_hdmi *hdmi,
  1224. struct platform_device *pdev)
  1225. {
  1226. struct device *dev = &pdev->dev;
  1227. struct device_node *np = dev->of_node;
  1228. struct device_node *cec_np, *port, *ep, *remote, *i2c_np;
  1229. struct platform_device *cec_pdev;
  1230. struct regmap *regmap;
  1231. struct resource *mem;
  1232. int ret;
  1233. ret = mtk_hdmi_get_all_clk(hdmi, np);
  1234. if (ret) {
  1235. dev_err(dev, "Failed to get clocks: %d\n", ret);
  1236. return ret;
  1237. }
  1238. /* The CEC module handles HDMI hotplug detection */
  1239. cec_np = of_find_compatible_node(np->parent, NULL,
  1240. "mediatek,mt8173-cec");
  1241. if (!cec_np) {
  1242. dev_err(dev, "Failed to find CEC node\n");
  1243. return -EINVAL;
  1244. }
  1245. cec_pdev = of_find_device_by_node(cec_np);
  1246. if (!cec_pdev) {
  1247. dev_err(hdmi->dev, "Waiting for CEC device %s\n",
  1248. cec_np->full_name);
  1249. return -EPROBE_DEFER;
  1250. }
  1251. hdmi->cec_dev = &cec_pdev->dev;
  1252. /*
  1253. * The mediatek,syscon-hdmi property contains a phandle link to the
  1254. * MMSYS_CONFIG device and the register offset of the HDMI_SYS_CFG
  1255. * registers it contains.
  1256. */
  1257. regmap = syscon_regmap_lookup_by_phandle(np, "mediatek,syscon-hdmi");
  1258. ret = of_property_read_u32_index(np, "mediatek,syscon-hdmi", 1,
  1259. &hdmi->sys_offset);
  1260. if (IS_ERR(regmap))
  1261. ret = PTR_ERR(regmap);
  1262. if (ret) {
  1263. ret = PTR_ERR(regmap);
  1264. dev_err(dev,
  1265. "Failed to get system configuration registers: %d\n",
  1266. ret);
  1267. return ret;
  1268. }
  1269. hdmi->sys_regmap = regmap;
  1270. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1271. hdmi->regs = devm_ioremap_resource(dev, mem);
  1272. if (IS_ERR(hdmi->regs))
  1273. return PTR_ERR(hdmi->regs);
  1274. port = of_graph_get_port_by_id(np, 1);
  1275. if (!port) {
  1276. dev_err(dev, "Missing output port node\n");
  1277. return -EINVAL;
  1278. }
  1279. ep = of_get_child_by_name(port, "endpoint");
  1280. if (!ep) {
  1281. dev_err(dev, "Missing endpoint node in port %s\n",
  1282. port->full_name);
  1283. of_node_put(port);
  1284. return -EINVAL;
  1285. }
  1286. of_node_put(port);
  1287. remote = of_graph_get_remote_port_parent(ep);
  1288. if (!remote) {
  1289. dev_err(dev, "Missing connector/bridge node for endpoint %s\n",
  1290. ep->full_name);
  1291. of_node_put(ep);
  1292. return -EINVAL;
  1293. }
  1294. of_node_put(ep);
  1295. if (!of_device_is_compatible(remote, "hdmi-connector")) {
  1296. hdmi->bridge.next = of_drm_find_bridge(remote);
  1297. if (!hdmi->bridge.next) {
  1298. dev_err(dev, "Waiting for external bridge\n");
  1299. of_node_put(remote);
  1300. return -EPROBE_DEFER;
  1301. }
  1302. }
  1303. i2c_np = of_parse_phandle(remote, "ddc-i2c-bus", 0);
  1304. if (!i2c_np) {
  1305. dev_err(dev, "Failed to find ddc-i2c-bus node in %s\n",
  1306. remote->full_name);
  1307. of_node_put(remote);
  1308. return -EINVAL;
  1309. }
  1310. of_node_put(remote);
  1311. hdmi->ddc_adpt = of_find_i2c_adapter_by_node(i2c_np);
  1312. if (!hdmi->ddc_adpt) {
  1313. dev_err(dev, "Failed to get ddc i2c adapter by node\n");
  1314. return -EINVAL;
  1315. }
  1316. return 0;
  1317. }
  1318. /*
  1319. * HDMI audio codec callbacks
  1320. */
  1321. static int mtk_hdmi_audio_hw_params(struct device *dev,
  1322. struct hdmi_codec_daifmt *daifmt,
  1323. struct hdmi_codec_params *params)
  1324. {
  1325. struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
  1326. struct hdmi_audio_param hdmi_params;
  1327. unsigned int chan = params->cea.channels;
  1328. dev_dbg(hdmi->dev, "%s: %u Hz, %d bit, %d channels\n", __func__,
  1329. params->sample_rate, params->sample_width, chan);
  1330. if (!hdmi->bridge.encoder)
  1331. return -ENODEV;
  1332. switch (chan) {
  1333. case 2:
  1334. hdmi_params.aud_input_chan_type = HDMI_AUD_CHAN_TYPE_2_0;
  1335. break;
  1336. case 4:
  1337. hdmi_params.aud_input_chan_type = HDMI_AUD_CHAN_TYPE_4_0;
  1338. break;
  1339. case 6:
  1340. hdmi_params.aud_input_chan_type = HDMI_AUD_CHAN_TYPE_5_1;
  1341. break;
  1342. case 8:
  1343. hdmi_params.aud_input_chan_type = HDMI_AUD_CHAN_TYPE_7_1;
  1344. break;
  1345. default:
  1346. dev_err(hdmi->dev, "channel[%d] not supported!\n", chan);
  1347. return -EINVAL;
  1348. }
  1349. switch (params->sample_rate) {
  1350. case 32000:
  1351. case 44100:
  1352. case 48000:
  1353. case 88200:
  1354. case 96000:
  1355. case 176400:
  1356. case 192000:
  1357. break;
  1358. default:
  1359. dev_err(hdmi->dev, "rate[%d] not supported!\n",
  1360. params->sample_rate);
  1361. return -EINVAL;
  1362. }
  1363. switch (daifmt->fmt) {
  1364. case HDMI_I2S:
  1365. hdmi_params.aud_codec = HDMI_AUDIO_CODING_TYPE_PCM;
  1366. hdmi_params.aud_sampe_size = HDMI_AUDIO_SAMPLE_SIZE_16;
  1367. hdmi_params.aud_input_type = HDMI_AUD_INPUT_I2S;
  1368. hdmi_params.aud_i2s_fmt = HDMI_I2S_MODE_I2S_24BIT;
  1369. hdmi_params.aud_mclk = HDMI_AUD_MCLK_128FS;
  1370. break;
  1371. default:
  1372. dev_err(hdmi->dev, "%s: Invalid DAI format %d\n", __func__,
  1373. daifmt->fmt);
  1374. return -EINVAL;
  1375. }
  1376. memcpy(&hdmi_params.codec_params, params,
  1377. sizeof(hdmi_params.codec_params));
  1378. mtk_hdmi_audio_set_param(hdmi, &hdmi_params);
  1379. return 0;
  1380. }
  1381. static int mtk_hdmi_audio_startup(struct device *dev)
  1382. {
  1383. struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
  1384. dev_dbg(dev, "%s\n", __func__);
  1385. mtk_hdmi_audio_enable(hdmi);
  1386. return 0;
  1387. }
  1388. static void mtk_hdmi_audio_shutdown(struct device *dev)
  1389. {
  1390. struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
  1391. dev_dbg(dev, "%s\n", __func__);
  1392. mtk_hdmi_audio_disable(hdmi);
  1393. }
  1394. int mtk_hdmi_audio_digital_mute(struct device *dev, bool enable)
  1395. {
  1396. struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
  1397. dev_dbg(dev, "%s(%d)\n", __func__, enable);
  1398. if (enable)
  1399. mtk_hdmi_hw_aud_mute(hdmi);
  1400. else
  1401. mtk_hdmi_hw_aud_unmute(hdmi);
  1402. return 0;
  1403. }
  1404. static int mtk_hdmi_audio_get_eld(struct device *dev, uint8_t *buf, size_t len)
  1405. {
  1406. struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
  1407. dev_dbg(dev, "%s\n", __func__);
  1408. memcpy(buf, hdmi->conn.eld, min(sizeof(hdmi->conn.eld), len));
  1409. return 0;
  1410. }
  1411. static const struct hdmi_codec_ops mtk_hdmi_audio_codec_ops = {
  1412. .hw_params = mtk_hdmi_audio_hw_params,
  1413. .audio_startup = mtk_hdmi_audio_startup,
  1414. .audio_shutdown = mtk_hdmi_audio_shutdown,
  1415. .digital_mute = mtk_hdmi_audio_digital_mute,
  1416. .get_eld = mtk_hdmi_audio_get_eld,
  1417. };
  1418. static void mtk_hdmi_register_audio_driver(struct device *dev)
  1419. {
  1420. struct hdmi_codec_pdata codec_data = {
  1421. .ops = &mtk_hdmi_audio_codec_ops,
  1422. .max_i2s_channels = 2,
  1423. .i2s = 1,
  1424. };
  1425. struct platform_device *pdev;
  1426. pdev = platform_device_register_data(dev, HDMI_CODEC_DRV_NAME,
  1427. PLATFORM_DEVID_AUTO, &codec_data,
  1428. sizeof(codec_data));
  1429. if (IS_ERR(pdev))
  1430. return;
  1431. DRM_INFO("%s driver bound to HDMI\n", HDMI_CODEC_DRV_NAME);
  1432. }
  1433. static int mtk_drm_hdmi_probe(struct platform_device *pdev)
  1434. {
  1435. struct mtk_hdmi *hdmi;
  1436. struct device *dev = &pdev->dev;
  1437. int ret;
  1438. hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
  1439. if (!hdmi)
  1440. return -ENOMEM;
  1441. hdmi->dev = dev;
  1442. ret = mtk_hdmi_dt_parse_pdata(hdmi, pdev);
  1443. if (ret)
  1444. return ret;
  1445. hdmi->phy = devm_phy_get(dev, "hdmi");
  1446. if (IS_ERR(hdmi->phy)) {
  1447. ret = PTR_ERR(hdmi->phy);
  1448. dev_err(dev, "Failed to get HDMI PHY: %d\n", ret);
  1449. return ret;
  1450. }
  1451. platform_set_drvdata(pdev, hdmi);
  1452. ret = mtk_hdmi_output_init(hdmi);
  1453. if (ret) {
  1454. dev_err(dev, "Failed to initialize hdmi output\n");
  1455. return ret;
  1456. }
  1457. mtk_hdmi_register_audio_driver(dev);
  1458. hdmi->bridge.funcs = &mtk_hdmi_bridge_funcs;
  1459. hdmi->bridge.of_node = pdev->dev.of_node;
  1460. ret = drm_bridge_add(&hdmi->bridge);
  1461. if (ret) {
  1462. dev_err(dev, "failed to add bridge, ret = %d\n", ret);
  1463. return ret;
  1464. }
  1465. ret = mtk_hdmi_clk_enable_audio(hdmi);
  1466. if (ret) {
  1467. dev_err(dev, "Failed to enable audio clocks: %d\n", ret);
  1468. goto err_bridge_remove;
  1469. }
  1470. dev_dbg(dev, "mediatek hdmi probe success\n");
  1471. return 0;
  1472. err_bridge_remove:
  1473. drm_bridge_remove(&hdmi->bridge);
  1474. return ret;
  1475. }
  1476. static int mtk_drm_hdmi_remove(struct platform_device *pdev)
  1477. {
  1478. struct mtk_hdmi *hdmi = platform_get_drvdata(pdev);
  1479. drm_bridge_remove(&hdmi->bridge);
  1480. mtk_hdmi_clk_disable_audio(hdmi);
  1481. return 0;
  1482. }
  1483. #ifdef CONFIG_PM_SLEEP
  1484. static int mtk_hdmi_suspend(struct device *dev)
  1485. {
  1486. struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
  1487. mtk_hdmi_clk_disable_audio(hdmi);
  1488. dev_dbg(dev, "hdmi suspend success!\n");
  1489. return 0;
  1490. }
  1491. static int mtk_hdmi_resume(struct device *dev)
  1492. {
  1493. struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
  1494. int ret = 0;
  1495. ret = mtk_hdmi_clk_enable_audio(hdmi);
  1496. if (ret) {
  1497. dev_err(dev, "hdmi resume failed!\n");
  1498. return ret;
  1499. }
  1500. dev_dbg(dev, "hdmi resume success!\n");
  1501. return 0;
  1502. }
  1503. #endif
  1504. static SIMPLE_DEV_PM_OPS(mtk_hdmi_pm_ops,
  1505. mtk_hdmi_suspend, mtk_hdmi_resume);
  1506. static const struct of_device_id mtk_drm_hdmi_of_ids[] = {
  1507. { .compatible = "mediatek,mt8173-hdmi", },
  1508. {}
  1509. };
  1510. static struct platform_driver mtk_hdmi_driver = {
  1511. .probe = mtk_drm_hdmi_probe,
  1512. .remove = mtk_drm_hdmi_remove,
  1513. .driver = {
  1514. .name = "mediatek-drm-hdmi",
  1515. .of_match_table = mtk_drm_hdmi_of_ids,
  1516. .pm = &mtk_hdmi_pm_ops,
  1517. },
  1518. };
  1519. static struct platform_driver * const mtk_hdmi_drivers[] = {
  1520. &mtk_hdmi_phy_driver,
  1521. &mtk_hdmi_ddc_driver,
  1522. &mtk_cec_driver,
  1523. &mtk_hdmi_driver,
  1524. };
  1525. static int __init mtk_hdmitx_init(void)
  1526. {
  1527. int ret;
  1528. int i;
  1529. for (i = 0; i < ARRAY_SIZE(mtk_hdmi_drivers); i++) {
  1530. ret = platform_driver_register(mtk_hdmi_drivers[i]);
  1531. if (ret < 0) {
  1532. pr_err("Failed to register %s driver: %d\n",
  1533. mtk_hdmi_drivers[i]->driver.name, ret);
  1534. goto err;
  1535. }
  1536. }
  1537. return 0;
  1538. err:
  1539. while (--i >= 0)
  1540. platform_driver_unregister(mtk_hdmi_drivers[i]);
  1541. return ret;
  1542. }
  1543. static void __exit mtk_hdmitx_exit(void)
  1544. {
  1545. int i;
  1546. for (i = ARRAY_SIZE(mtk_hdmi_drivers) - 1; i >= 0; i--)
  1547. platform_driver_unregister(mtk_hdmi_drivers[i]);
  1548. }
  1549. module_init(mtk_hdmitx_init);
  1550. module_exit(mtk_hdmitx_exit);
  1551. MODULE_AUTHOR("Jie Qiu <jie.qiu@mediatek.com>");
  1552. MODULE_DESCRIPTION("MediaTek HDMI Driver");
  1553. MODULE_LICENSE("GPL v2");