mtk_hdmi.c 45 KB

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