mtk_hdmi.c 46 KB

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