mtk_hdmi.c 47 KB

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