mtk_hdmi.c 46 KB

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