tda998x_drv.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769
  1. /*
  2. * Copyright (C) 2012 Texas Instruments
  3. * Author: Rob Clark <robdclark@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/component.h>
  18. #include <linux/hdmi.h>
  19. #include <linux/module.h>
  20. #include <linux/irq.h>
  21. #include <sound/asoundef.h>
  22. #include <sound/hdmi-codec.h>
  23. #include <drm/drmP.h>
  24. #include <drm/drm_atomic_helper.h>
  25. #include <drm/drm_crtc_helper.h>
  26. #include <drm/drm_edid.h>
  27. #include <drm/drm_of.h>
  28. #include <drm/i2c/tda998x.h>
  29. #define DBG(fmt, ...) DRM_DEBUG(fmt"\n", ##__VA_ARGS__)
  30. struct tda998x_audio_port {
  31. u8 format; /* AFMT_xxx */
  32. u8 config; /* AP value */
  33. };
  34. struct tda998x_priv {
  35. struct i2c_client *cec;
  36. struct i2c_client *hdmi;
  37. struct mutex mutex;
  38. u16 rev;
  39. u8 cec_addr;
  40. u8 current_page;
  41. bool is_on;
  42. bool supports_infoframes;
  43. bool sink_has_audio;
  44. u8 vip_cntrl_0;
  45. u8 vip_cntrl_1;
  46. u8 vip_cntrl_2;
  47. unsigned long tmds_clock;
  48. struct tda998x_audio_params audio_params;
  49. struct platform_device *audio_pdev;
  50. struct mutex audio_mutex;
  51. wait_queue_head_t wq_edid;
  52. volatile int wq_edid_wait;
  53. struct work_struct detect_work;
  54. struct timer_list edid_delay_timer;
  55. wait_queue_head_t edid_delay_waitq;
  56. bool edid_delay_active;
  57. struct drm_encoder encoder;
  58. struct drm_connector connector;
  59. struct tda998x_audio_port audio_port[2];
  60. };
  61. #define conn_to_tda998x_priv(x) \
  62. container_of(x, struct tda998x_priv, connector)
  63. #define enc_to_tda998x_priv(x) \
  64. container_of(x, struct tda998x_priv, encoder)
  65. /* The TDA9988 series of devices use a paged register scheme.. to simplify
  66. * things we encode the page # in upper bits of the register #. To read/
  67. * write a given register, we need to make sure CURPAGE register is set
  68. * appropriately. Which implies reads/writes are not atomic. Fun!
  69. */
  70. #define REG(page, addr) (((page) << 8) | (addr))
  71. #define REG2ADDR(reg) ((reg) & 0xff)
  72. #define REG2PAGE(reg) (((reg) >> 8) & 0xff)
  73. #define REG_CURPAGE 0xff /* write */
  74. /* Page 00h: General Control */
  75. #define REG_VERSION_LSB REG(0x00, 0x00) /* read */
  76. #define REG_MAIN_CNTRL0 REG(0x00, 0x01) /* read/write */
  77. # define MAIN_CNTRL0_SR (1 << 0)
  78. # define MAIN_CNTRL0_DECS (1 << 1)
  79. # define MAIN_CNTRL0_DEHS (1 << 2)
  80. # define MAIN_CNTRL0_CECS (1 << 3)
  81. # define MAIN_CNTRL0_CEHS (1 << 4)
  82. # define MAIN_CNTRL0_SCALER (1 << 7)
  83. #define REG_VERSION_MSB REG(0x00, 0x02) /* read */
  84. #define REG_SOFTRESET REG(0x00, 0x0a) /* write */
  85. # define SOFTRESET_AUDIO (1 << 0)
  86. # define SOFTRESET_I2C_MASTER (1 << 1)
  87. #define REG_DDC_DISABLE REG(0x00, 0x0b) /* read/write */
  88. #define REG_CCLK_ON REG(0x00, 0x0c) /* read/write */
  89. #define REG_I2C_MASTER REG(0x00, 0x0d) /* read/write */
  90. # define I2C_MASTER_DIS_MM (1 << 0)
  91. # define I2C_MASTER_DIS_FILT (1 << 1)
  92. # define I2C_MASTER_APP_STRT_LAT (1 << 2)
  93. #define REG_FEAT_POWERDOWN REG(0x00, 0x0e) /* read/write */
  94. # define FEAT_POWERDOWN_PREFILT BIT(0)
  95. # define FEAT_POWERDOWN_CSC BIT(1)
  96. # define FEAT_POWERDOWN_SPDIF (1 << 3)
  97. #define REG_INT_FLAGS_0 REG(0x00, 0x0f) /* read/write */
  98. #define REG_INT_FLAGS_1 REG(0x00, 0x10) /* read/write */
  99. #define REG_INT_FLAGS_2 REG(0x00, 0x11) /* read/write */
  100. # define INT_FLAGS_2_EDID_BLK_RD (1 << 1)
  101. #define REG_ENA_ACLK REG(0x00, 0x16) /* read/write */
  102. #define REG_ENA_VP_0 REG(0x00, 0x18) /* read/write */
  103. #define REG_ENA_VP_1 REG(0x00, 0x19) /* read/write */
  104. #define REG_ENA_VP_2 REG(0x00, 0x1a) /* read/write */
  105. #define REG_ENA_AP REG(0x00, 0x1e) /* read/write */
  106. #define REG_VIP_CNTRL_0 REG(0x00, 0x20) /* write */
  107. # define VIP_CNTRL_0_MIRR_A (1 << 7)
  108. # define VIP_CNTRL_0_SWAP_A(x) (((x) & 7) << 4)
  109. # define VIP_CNTRL_0_MIRR_B (1 << 3)
  110. # define VIP_CNTRL_0_SWAP_B(x) (((x) & 7) << 0)
  111. #define REG_VIP_CNTRL_1 REG(0x00, 0x21) /* write */
  112. # define VIP_CNTRL_1_MIRR_C (1 << 7)
  113. # define VIP_CNTRL_1_SWAP_C(x) (((x) & 7) << 4)
  114. # define VIP_CNTRL_1_MIRR_D (1 << 3)
  115. # define VIP_CNTRL_1_SWAP_D(x) (((x) & 7) << 0)
  116. #define REG_VIP_CNTRL_2 REG(0x00, 0x22) /* write */
  117. # define VIP_CNTRL_2_MIRR_E (1 << 7)
  118. # define VIP_CNTRL_2_SWAP_E(x) (((x) & 7) << 4)
  119. # define VIP_CNTRL_2_MIRR_F (1 << 3)
  120. # define VIP_CNTRL_2_SWAP_F(x) (((x) & 7) << 0)
  121. #define REG_VIP_CNTRL_3 REG(0x00, 0x23) /* write */
  122. # define VIP_CNTRL_3_X_TGL (1 << 0)
  123. # define VIP_CNTRL_3_H_TGL (1 << 1)
  124. # define VIP_CNTRL_3_V_TGL (1 << 2)
  125. # define VIP_CNTRL_3_EMB (1 << 3)
  126. # define VIP_CNTRL_3_SYNC_DE (1 << 4)
  127. # define VIP_CNTRL_3_SYNC_HS (1 << 5)
  128. # define VIP_CNTRL_3_DE_INT (1 << 6)
  129. # define VIP_CNTRL_3_EDGE (1 << 7)
  130. #define REG_VIP_CNTRL_4 REG(0x00, 0x24) /* write */
  131. # define VIP_CNTRL_4_BLC(x) (((x) & 3) << 0)
  132. # define VIP_CNTRL_4_BLANKIT(x) (((x) & 3) << 2)
  133. # define VIP_CNTRL_4_CCIR656 (1 << 4)
  134. # define VIP_CNTRL_4_656_ALT (1 << 5)
  135. # define VIP_CNTRL_4_TST_656 (1 << 6)
  136. # define VIP_CNTRL_4_TST_PAT (1 << 7)
  137. #define REG_VIP_CNTRL_5 REG(0x00, 0x25) /* write */
  138. # define VIP_CNTRL_5_CKCASE (1 << 0)
  139. # define VIP_CNTRL_5_SP_CNT(x) (((x) & 3) << 1)
  140. #define REG_MUX_AP REG(0x00, 0x26) /* read/write */
  141. # define MUX_AP_SELECT_I2S 0x64
  142. # define MUX_AP_SELECT_SPDIF 0x40
  143. #define REG_MUX_VP_VIP_OUT REG(0x00, 0x27) /* read/write */
  144. #define REG_MAT_CONTRL REG(0x00, 0x80) /* write */
  145. # define MAT_CONTRL_MAT_SC(x) (((x) & 3) << 0)
  146. # define MAT_CONTRL_MAT_BP (1 << 2)
  147. #define REG_VIDFORMAT REG(0x00, 0xa0) /* write */
  148. #define REG_REFPIX_MSB REG(0x00, 0xa1) /* write */
  149. #define REG_REFPIX_LSB REG(0x00, 0xa2) /* write */
  150. #define REG_REFLINE_MSB REG(0x00, 0xa3) /* write */
  151. #define REG_REFLINE_LSB REG(0x00, 0xa4) /* write */
  152. #define REG_NPIX_MSB REG(0x00, 0xa5) /* write */
  153. #define REG_NPIX_LSB REG(0x00, 0xa6) /* write */
  154. #define REG_NLINE_MSB REG(0x00, 0xa7) /* write */
  155. #define REG_NLINE_LSB REG(0x00, 0xa8) /* write */
  156. #define REG_VS_LINE_STRT_1_MSB REG(0x00, 0xa9) /* write */
  157. #define REG_VS_LINE_STRT_1_LSB REG(0x00, 0xaa) /* write */
  158. #define REG_VS_PIX_STRT_1_MSB REG(0x00, 0xab) /* write */
  159. #define REG_VS_PIX_STRT_1_LSB REG(0x00, 0xac) /* write */
  160. #define REG_VS_LINE_END_1_MSB REG(0x00, 0xad) /* write */
  161. #define REG_VS_LINE_END_1_LSB REG(0x00, 0xae) /* write */
  162. #define REG_VS_PIX_END_1_MSB REG(0x00, 0xaf) /* write */
  163. #define REG_VS_PIX_END_1_LSB REG(0x00, 0xb0) /* write */
  164. #define REG_VS_LINE_STRT_2_MSB REG(0x00, 0xb1) /* write */
  165. #define REG_VS_LINE_STRT_2_LSB REG(0x00, 0xb2) /* write */
  166. #define REG_VS_PIX_STRT_2_MSB REG(0x00, 0xb3) /* write */
  167. #define REG_VS_PIX_STRT_2_LSB REG(0x00, 0xb4) /* write */
  168. #define REG_VS_LINE_END_2_MSB REG(0x00, 0xb5) /* write */
  169. #define REG_VS_LINE_END_2_LSB REG(0x00, 0xb6) /* write */
  170. #define REG_VS_PIX_END_2_MSB REG(0x00, 0xb7) /* write */
  171. #define REG_VS_PIX_END_2_LSB REG(0x00, 0xb8) /* write */
  172. #define REG_HS_PIX_START_MSB REG(0x00, 0xb9) /* write */
  173. #define REG_HS_PIX_START_LSB REG(0x00, 0xba) /* write */
  174. #define REG_HS_PIX_STOP_MSB REG(0x00, 0xbb) /* write */
  175. #define REG_HS_PIX_STOP_LSB REG(0x00, 0xbc) /* write */
  176. #define REG_VWIN_START_1_MSB REG(0x00, 0xbd) /* write */
  177. #define REG_VWIN_START_1_LSB REG(0x00, 0xbe) /* write */
  178. #define REG_VWIN_END_1_MSB REG(0x00, 0xbf) /* write */
  179. #define REG_VWIN_END_1_LSB REG(0x00, 0xc0) /* write */
  180. #define REG_VWIN_START_2_MSB REG(0x00, 0xc1) /* write */
  181. #define REG_VWIN_START_2_LSB REG(0x00, 0xc2) /* write */
  182. #define REG_VWIN_END_2_MSB REG(0x00, 0xc3) /* write */
  183. #define REG_VWIN_END_2_LSB REG(0x00, 0xc4) /* write */
  184. #define REG_DE_START_MSB REG(0x00, 0xc5) /* write */
  185. #define REG_DE_START_LSB REG(0x00, 0xc6) /* write */
  186. #define REG_DE_STOP_MSB REG(0x00, 0xc7) /* write */
  187. #define REG_DE_STOP_LSB REG(0x00, 0xc8) /* write */
  188. #define REG_TBG_CNTRL_0 REG(0x00, 0xca) /* write */
  189. # define TBG_CNTRL_0_TOP_TGL (1 << 0)
  190. # define TBG_CNTRL_0_TOP_SEL (1 << 1)
  191. # define TBG_CNTRL_0_DE_EXT (1 << 2)
  192. # define TBG_CNTRL_0_TOP_EXT (1 << 3)
  193. # define TBG_CNTRL_0_FRAME_DIS (1 << 5)
  194. # define TBG_CNTRL_0_SYNC_MTHD (1 << 6)
  195. # define TBG_CNTRL_0_SYNC_ONCE (1 << 7)
  196. #define REG_TBG_CNTRL_1 REG(0x00, 0xcb) /* write */
  197. # define TBG_CNTRL_1_H_TGL (1 << 0)
  198. # define TBG_CNTRL_1_V_TGL (1 << 1)
  199. # define TBG_CNTRL_1_TGL_EN (1 << 2)
  200. # define TBG_CNTRL_1_X_EXT (1 << 3)
  201. # define TBG_CNTRL_1_H_EXT (1 << 4)
  202. # define TBG_CNTRL_1_V_EXT (1 << 5)
  203. # define TBG_CNTRL_1_DWIN_DIS (1 << 6)
  204. #define REG_ENABLE_SPACE REG(0x00, 0xd6) /* write */
  205. #define REG_HVF_CNTRL_0 REG(0x00, 0xe4) /* write */
  206. # define HVF_CNTRL_0_SM (1 << 7)
  207. # define HVF_CNTRL_0_RWB (1 << 6)
  208. # define HVF_CNTRL_0_PREFIL(x) (((x) & 3) << 2)
  209. # define HVF_CNTRL_0_INTPOL(x) (((x) & 3) << 0)
  210. #define REG_HVF_CNTRL_1 REG(0x00, 0xe5) /* write */
  211. # define HVF_CNTRL_1_FOR (1 << 0)
  212. # define HVF_CNTRL_1_YUVBLK (1 << 1)
  213. # define HVF_CNTRL_1_VQR(x) (((x) & 3) << 2)
  214. # define HVF_CNTRL_1_PAD(x) (((x) & 3) << 4)
  215. # define HVF_CNTRL_1_SEMI_PLANAR (1 << 6)
  216. #define REG_RPT_CNTRL REG(0x00, 0xf0) /* write */
  217. #define REG_I2S_FORMAT REG(0x00, 0xfc) /* read/write */
  218. # define I2S_FORMAT(x) (((x) & 3) << 0)
  219. #define REG_AIP_CLKSEL REG(0x00, 0xfd) /* write */
  220. # define AIP_CLKSEL_AIP_SPDIF (0 << 3)
  221. # define AIP_CLKSEL_AIP_I2S (1 << 3)
  222. # define AIP_CLKSEL_FS_ACLK (0 << 0)
  223. # define AIP_CLKSEL_FS_MCLK (1 << 0)
  224. # define AIP_CLKSEL_FS_FS64SPDIF (2 << 0)
  225. /* Page 02h: PLL settings */
  226. #define REG_PLL_SERIAL_1 REG(0x02, 0x00) /* read/write */
  227. # define PLL_SERIAL_1_SRL_FDN (1 << 0)
  228. # define PLL_SERIAL_1_SRL_IZ(x) (((x) & 3) << 1)
  229. # define PLL_SERIAL_1_SRL_MAN_IZ (1 << 6)
  230. #define REG_PLL_SERIAL_2 REG(0x02, 0x01) /* read/write */
  231. # define PLL_SERIAL_2_SRL_NOSC(x) ((x) << 0)
  232. # define PLL_SERIAL_2_SRL_PR(x) (((x) & 0xf) << 4)
  233. #define REG_PLL_SERIAL_3 REG(0x02, 0x02) /* read/write */
  234. # define PLL_SERIAL_3_SRL_CCIR (1 << 0)
  235. # define PLL_SERIAL_3_SRL_DE (1 << 2)
  236. # define PLL_SERIAL_3_SRL_PXIN_SEL (1 << 4)
  237. #define REG_SERIALIZER REG(0x02, 0x03) /* read/write */
  238. #define REG_BUFFER_OUT REG(0x02, 0x04) /* read/write */
  239. #define REG_PLL_SCG1 REG(0x02, 0x05) /* read/write */
  240. #define REG_PLL_SCG2 REG(0x02, 0x06) /* read/write */
  241. #define REG_PLL_SCGN1 REG(0x02, 0x07) /* read/write */
  242. #define REG_PLL_SCGN2 REG(0x02, 0x08) /* read/write */
  243. #define REG_PLL_SCGR1 REG(0x02, 0x09) /* read/write */
  244. #define REG_PLL_SCGR2 REG(0x02, 0x0a) /* read/write */
  245. #define REG_AUDIO_DIV REG(0x02, 0x0e) /* read/write */
  246. # define AUDIO_DIV_SERCLK_1 0
  247. # define AUDIO_DIV_SERCLK_2 1
  248. # define AUDIO_DIV_SERCLK_4 2
  249. # define AUDIO_DIV_SERCLK_8 3
  250. # define AUDIO_DIV_SERCLK_16 4
  251. # define AUDIO_DIV_SERCLK_32 5
  252. #define REG_SEL_CLK REG(0x02, 0x11) /* read/write */
  253. # define SEL_CLK_SEL_CLK1 (1 << 0)
  254. # define SEL_CLK_SEL_VRF_CLK(x) (((x) & 3) << 1)
  255. # define SEL_CLK_ENA_SC_CLK (1 << 3)
  256. #define REG_ANA_GENERAL REG(0x02, 0x12) /* read/write */
  257. /* Page 09h: EDID Control */
  258. #define REG_EDID_DATA_0 REG(0x09, 0x00) /* read */
  259. /* next 127 successive registers are the EDID block */
  260. #define REG_EDID_CTRL REG(0x09, 0xfa) /* read/write */
  261. #define REG_DDC_ADDR REG(0x09, 0xfb) /* read/write */
  262. #define REG_DDC_OFFS REG(0x09, 0xfc) /* read/write */
  263. #define REG_DDC_SEGM_ADDR REG(0x09, 0xfd) /* read/write */
  264. #define REG_DDC_SEGM REG(0x09, 0xfe) /* read/write */
  265. /* Page 10h: information frames and packets */
  266. #define REG_IF1_HB0 REG(0x10, 0x20) /* read/write */
  267. #define REG_IF2_HB0 REG(0x10, 0x40) /* read/write */
  268. #define REG_IF3_HB0 REG(0x10, 0x60) /* read/write */
  269. #define REG_IF4_HB0 REG(0x10, 0x80) /* read/write */
  270. #define REG_IF5_HB0 REG(0x10, 0xa0) /* read/write */
  271. /* Page 11h: audio settings and content info packets */
  272. #define REG_AIP_CNTRL_0 REG(0x11, 0x00) /* read/write */
  273. # define AIP_CNTRL_0_RST_FIFO (1 << 0)
  274. # define AIP_CNTRL_0_SWAP (1 << 1)
  275. # define AIP_CNTRL_0_LAYOUT (1 << 2)
  276. # define AIP_CNTRL_0_ACR_MAN (1 << 5)
  277. # define AIP_CNTRL_0_RST_CTS (1 << 6)
  278. #define REG_CA_I2S REG(0x11, 0x01) /* read/write */
  279. # define CA_I2S_CA_I2S(x) (((x) & 31) << 0)
  280. # define CA_I2S_HBR_CHSTAT (1 << 6)
  281. #define REG_LATENCY_RD REG(0x11, 0x04) /* read/write */
  282. #define REG_ACR_CTS_0 REG(0x11, 0x05) /* read/write */
  283. #define REG_ACR_CTS_1 REG(0x11, 0x06) /* read/write */
  284. #define REG_ACR_CTS_2 REG(0x11, 0x07) /* read/write */
  285. #define REG_ACR_N_0 REG(0x11, 0x08) /* read/write */
  286. #define REG_ACR_N_1 REG(0x11, 0x09) /* read/write */
  287. #define REG_ACR_N_2 REG(0x11, 0x0a) /* read/write */
  288. #define REG_CTS_N REG(0x11, 0x0c) /* read/write */
  289. # define CTS_N_K(x) (((x) & 7) << 0)
  290. # define CTS_N_M(x) (((x) & 3) << 4)
  291. #define REG_ENC_CNTRL REG(0x11, 0x0d) /* read/write */
  292. # define ENC_CNTRL_RST_ENC (1 << 0)
  293. # define ENC_CNTRL_RST_SEL (1 << 1)
  294. # define ENC_CNTRL_CTL_CODE(x) (((x) & 3) << 2)
  295. #define REG_DIP_FLAGS REG(0x11, 0x0e) /* read/write */
  296. # define DIP_FLAGS_ACR (1 << 0)
  297. # define DIP_FLAGS_GC (1 << 1)
  298. #define REG_DIP_IF_FLAGS REG(0x11, 0x0f) /* read/write */
  299. # define DIP_IF_FLAGS_IF1 (1 << 1)
  300. # define DIP_IF_FLAGS_IF2 (1 << 2)
  301. # define DIP_IF_FLAGS_IF3 (1 << 3)
  302. # define DIP_IF_FLAGS_IF4 (1 << 4)
  303. # define DIP_IF_FLAGS_IF5 (1 << 5)
  304. #define REG_CH_STAT_B(x) REG(0x11, 0x14 + (x)) /* read/write */
  305. /* Page 12h: HDCP and OTP */
  306. #define REG_TX3 REG(0x12, 0x9a) /* read/write */
  307. #define REG_TX4 REG(0x12, 0x9b) /* read/write */
  308. # define TX4_PD_RAM (1 << 1)
  309. #define REG_TX33 REG(0x12, 0xb8) /* read/write */
  310. # define TX33_HDMI (1 << 1)
  311. /* Page 13h: Gamut related metadata packets */
  312. /* CEC registers: (not paged)
  313. */
  314. #define REG_CEC_INTSTATUS 0xee /* read */
  315. # define CEC_INTSTATUS_CEC (1 << 0)
  316. # define CEC_INTSTATUS_HDMI (1 << 1)
  317. #define REG_CEC_FRO_IM_CLK_CTRL 0xfb /* read/write */
  318. # define CEC_FRO_IM_CLK_CTRL_GHOST_DIS (1 << 7)
  319. # define CEC_FRO_IM_CLK_CTRL_ENA_OTP (1 << 6)
  320. # define CEC_FRO_IM_CLK_CTRL_IMCLK_SEL (1 << 1)
  321. # define CEC_FRO_IM_CLK_CTRL_FRO_DIV (1 << 0)
  322. #define REG_CEC_RXSHPDINTENA 0xfc /* read/write */
  323. #define REG_CEC_RXSHPDINT 0xfd /* read */
  324. # define CEC_RXSHPDINT_RXSENS BIT(0)
  325. # define CEC_RXSHPDINT_HPD BIT(1)
  326. #define REG_CEC_RXSHPDLEV 0xfe /* read */
  327. # define CEC_RXSHPDLEV_RXSENS (1 << 0)
  328. # define CEC_RXSHPDLEV_HPD (1 << 1)
  329. #define REG_CEC_ENAMODS 0xff /* read/write */
  330. # define CEC_ENAMODS_DIS_FRO (1 << 6)
  331. # define CEC_ENAMODS_DIS_CCLK (1 << 5)
  332. # define CEC_ENAMODS_EN_RXSENS (1 << 2)
  333. # define CEC_ENAMODS_EN_HDMI (1 << 1)
  334. # define CEC_ENAMODS_EN_CEC (1 << 0)
  335. /* Device versions: */
  336. #define TDA9989N2 0x0101
  337. #define TDA19989 0x0201
  338. #define TDA19989N2 0x0202
  339. #define TDA19988 0x0301
  340. static void
  341. cec_write(struct tda998x_priv *priv, u16 addr, u8 val)
  342. {
  343. u8 buf[] = {addr, val};
  344. struct i2c_msg msg = {
  345. .addr = priv->cec_addr,
  346. .len = 2,
  347. .buf = buf,
  348. };
  349. int ret;
  350. ret = i2c_transfer(priv->hdmi->adapter, &msg, 1);
  351. if (ret < 0)
  352. dev_err(&priv->hdmi->dev, "Error %d writing to cec:0x%x\n",
  353. ret, addr);
  354. }
  355. static u8
  356. cec_read(struct tda998x_priv *priv, u8 addr)
  357. {
  358. u8 val;
  359. struct i2c_msg msg[2] = {
  360. {
  361. .addr = priv->cec_addr,
  362. .len = 1,
  363. .buf = &addr,
  364. }, {
  365. .addr = priv->cec_addr,
  366. .flags = I2C_M_RD,
  367. .len = 1,
  368. .buf = &val,
  369. },
  370. };
  371. int ret;
  372. ret = i2c_transfer(priv->hdmi->adapter, msg, ARRAY_SIZE(msg));
  373. if (ret < 0) {
  374. dev_err(&priv->hdmi->dev, "Error %d reading from cec:0x%x\n",
  375. ret, addr);
  376. val = 0;
  377. }
  378. return val;
  379. }
  380. static int
  381. set_page(struct tda998x_priv *priv, u16 reg)
  382. {
  383. if (REG2PAGE(reg) != priv->current_page) {
  384. struct i2c_client *client = priv->hdmi;
  385. u8 buf[] = {
  386. REG_CURPAGE, REG2PAGE(reg)
  387. };
  388. int ret = i2c_master_send(client, buf, sizeof(buf));
  389. if (ret < 0) {
  390. dev_err(&client->dev, "%s %04x err %d\n", __func__,
  391. reg, ret);
  392. return ret;
  393. }
  394. priv->current_page = REG2PAGE(reg);
  395. }
  396. return 0;
  397. }
  398. static int
  399. reg_read_range(struct tda998x_priv *priv, u16 reg, char *buf, int cnt)
  400. {
  401. struct i2c_client *client = priv->hdmi;
  402. u8 addr = REG2ADDR(reg);
  403. int ret;
  404. mutex_lock(&priv->mutex);
  405. ret = set_page(priv, reg);
  406. if (ret < 0)
  407. goto out;
  408. ret = i2c_master_send(client, &addr, sizeof(addr));
  409. if (ret < 0)
  410. goto fail;
  411. ret = i2c_master_recv(client, buf, cnt);
  412. if (ret < 0)
  413. goto fail;
  414. goto out;
  415. fail:
  416. dev_err(&client->dev, "Error %d reading from 0x%x\n", ret, reg);
  417. out:
  418. mutex_unlock(&priv->mutex);
  419. return ret;
  420. }
  421. static void
  422. reg_write_range(struct tda998x_priv *priv, u16 reg, u8 *p, int cnt)
  423. {
  424. struct i2c_client *client = priv->hdmi;
  425. u8 buf[cnt+1];
  426. int ret;
  427. buf[0] = REG2ADDR(reg);
  428. memcpy(&buf[1], p, cnt);
  429. mutex_lock(&priv->mutex);
  430. ret = set_page(priv, reg);
  431. if (ret < 0)
  432. goto out;
  433. ret = i2c_master_send(client, buf, cnt + 1);
  434. if (ret < 0)
  435. dev_err(&client->dev, "Error %d writing to 0x%x\n", ret, reg);
  436. out:
  437. mutex_unlock(&priv->mutex);
  438. }
  439. static int
  440. reg_read(struct tda998x_priv *priv, u16 reg)
  441. {
  442. u8 val = 0;
  443. int ret;
  444. ret = reg_read_range(priv, reg, &val, sizeof(val));
  445. if (ret < 0)
  446. return ret;
  447. return val;
  448. }
  449. static void
  450. reg_write(struct tda998x_priv *priv, u16 reg, u8 val)
  451. {
  452. struct i2c_client *client = priv->hdmi;
  453. u8 buf[] = {REG2ADDR(reg), val};
  454. int ret;
  455. mutex_lock(&priv->mutex);
  456. ret = set_page(priv, reg);
  457. if (ret < 0)
  458. goto out;
  459. ret = i2c_master_send(client, buf, sizeof(buf));
  460. if (ret < 0)
  461. dev_err(&client->dev, "Error %d writing to 0x%x\n", ret, reg);
  462. out:
  463. mutex_unlock(&priv->mutex);
  464. }
  465. static void
  466. reg_write16(struct tda998x_priv *priv, u16 reg, u16 val)
  467. {
  468. struct i2c_client *client = priv->hdmi;
  469. u8 buf[] = {REG2ADDR(reg), val >> 8, val};
  470. int ret;
  471. mutex_lock(&priv->mutex);
  472. ret = set_page(priv, reg);
  473. if (ret < 0)
  474. goto out;
  475. ret = i2c_master_send(client, buf, sizeof(buf));
  476. if (ret < 0)
  477. dev_err(&client->dev, "Error %d writing to 0x%x\n", ret, reg);
  478. out:
  479. mutex_unlock(&priv->mutex);
  480. }
  481. static void
  482. reg_set(struct tda998x_priv *priv, u16 reg, u8 val)
  483. {
  484. int old_val;
  485. old_val = reg_read(priv, reg);
  486. if (old_val >= 0)
  487. reg_write(priv, reg, old_val | val);
  488. }
  489. static void
  490. reg_clear(struct tda998x_priv *priv, u16 reg, u8 val)
  491. {
  492. int old_val;
  493. old_val = reg_read(priv, reg);
  494. if (old_val >= 0)
  495. reg_write(priv, reg, old_val & ~val);
  496. }
  497. static void
  498. tda998x_reset(struct tda998x_priv *priv)
  499. {
  500. /* reset audio and i2c master: */
  501. reg_write(priv, REG_SOFTRESET, SOFTRESET_AUDIO | SOFTRESET_I2C_MASTER);
  502. msleep(50);
  503. reg_write(priv, REG_SOFTRESET, 0);
  504. msleep(50);
  505. /* reset transmitter: */
  506. reg_set(priv, REG_MAIN_CNTRL0, MAIN_CNTRL0_SR);
  507. reg_clear(priv, REG_MAIN_CNTRL0, MAIN_CNTRL0_SR);
  508. /* PLL registers common configuration */
  509. reg_write(priv, REG_PLL_SERIAL_1, 0x00);
  510. reg_write(priv, REG_PLL_SERIAL_2, PLL_SERIAL_2_SRL_NOSC(1));
  511. reg_write(priv, REG_PLL_SERIAL_3, 0x00);
  512. reg_write(priv, REG_SERIALIZER, 0x00);
  513. reg_write(priv, REG_BUFFER_OUT, 0x00);
  514. reg_write(priv, REG_PLL_SCG1, 0x00);
  515. reg_write(priv, REG_AUDIO_DIV, AUDIO_DIV_SERCLK_8);
  516. reg_write(priv, REG_SEL_CLK, SEL_CLK_SEL_CLK1 | SEL_CLK_ENA_SC_CLK);
  517. reg_write(priv, REG_PLL_SCGN1, 0xfa);
  518. reg_write(priv, REG_PLL_SCGN2, 0x00);
  519. reg_write(priv, REG_PLL_SCGR1, 0x5b);
  520. reg_write(priv, REG_PLL_SCGR2, 0x00);
  521. reg_write(priv, REG_PLL_SCG2, 0x10);
  522. /* Write the default value MUX register */
  523. reg_write(priv, REG_MUX_VP_VIP_OUT, 0x24);
  524. }
  525. /*
  526. * The TDA998x has a problem when trying to read the EDID close to a
  527. * HPD assertion: it needs a delay of 100ms to avoid timing out while
  528. * trying to read EDID data.
  529. *
  530. * However, tda998x_connector_get_modes() may be called at any moment
  531. * after tda998x_connector_detect() indicates that we are connected, so
  532. * we need to delay probing modes in tda998x_connector_get_modes() after
  533. * we have seen a HPD inactive->active transition. This code implements
  534. * that delay.
  535. */
  536. static void tda998x_edid_delay_done(unsigned long data)
  537. {
  538. struct tda998x_priv *priv = (struct tda998x_priv *)data;
  539. priv->edid_delay_active = false;
  540. wake_up(&priv->edid_delay_waitq);
  541. schedule_work(&priv->detect_work);
  542. }
  543. static void tda998x_edid_delay_start(struct tda998x_priv *priv)
  544. {
  545. priv->edid_delay_active = true;
  546. mod_timer(&priv->edid_delay_timer, jiffies + HZ/10);
  547. }
  548. static int tda998x_edid_delay_wait(struct tda998x_priv *priv)
  549. {
  550. return wait_event_killable(priv->edid_delay_waitq, !priv->edid_delay_active);
  551. }
  552. /*
  553. * We need to run the KMS hotplug event helper outside of our threaded
  554. * interrupt routine as this can call back into our get_modes method,
  555. * which will want to make use of interrupts.
  556. */
  557. static void tda998x_detect_work(struct work_struct *work)
  558. {
  559. struct tda998x_priv *priv =
  560. container_of(work, struct tda998x_priv, detect_work);
  561. struct drm_device *dev = priv->encoder.dev;
  562. if (dev)
  563. drm_kms_helper_hotplug_event(dev);
  564. }
  565. /*
  566. * only 2 interrupts may occur: screen plug/unplug and EDID read
  567. */
  568. static irqreturn_t tda998x_irq_thread(int irq, void *data)
  569. {
  570. struct tda998x_priv *priv = data;
  571. u8 sta, cec, lvl, flag0, flag1, flag2;
  572. bool handled = false;
  573. sta = cec_read(priv, REG_CEC_INTSTATUS);
  574. if (sta & CEC_INTSTATUS_HDMI) {
  575. cec = cec_read(priv, REG_CEC_RXSHPDINT);
  576. lvl = cec_read(priv, REG_CEC_RXSHPDLEV);
  577. flag0 = reg_read(priv, REG_INT_FLAGS_0);
  578. flag1 = reg_read(priv, REG_INT_FLAGS_1);
  579. flag2 = reg_read(priv, REG_INT_FLAGS_2);
  580. DRM_DEBUG_DRIVER(
  581. "tda irq sta %02x cec %02x lvl %02x f0 %02x f1 %02x f2 %02x\n",
  582. sta, cec, lvl, flag0, flag1, flag2);
  583. if (cec & CEC_RXSHPDINT_HPD) {
  584. if (lvl & CEC_RXSHPDLEV_HPD)
  585. tda998x_edid_delay_start(priv);
  586. else
  587. schedule_work(&priv->detect_work);
  588. handled = true;
  589. }
  590. if ((flag2 & INT_FLAGS_2_EDID_BLK_RD) && priv->wq_edid_wait) {
  591. priv->wq_edid_wait = 0;
  592. wake_up(&priv->wq_edid);
  593. handled = true;
  594. }
  595. }
  596. return IRQ_RETVAL(handled);
  597. }
  598. static void
  599. tda998x_write_if(struct tda998x_priv *priv, u8 bit, u16 addr,
  600. union hdmi_infoframe *frame)
  601. {
  602. u8 buf[32];
  603. ssize_t len;
  604. len = hdmi_infoframe_pack(frame, buf, sizeof(buf));
  605. if (len < 0) {
  606. dev_err(&priv->hdmi->dev,
  607. "hdmi_infoframe_pack() type=0x%02x failed: %zd\n",
  608. frame->any.type, len);
  609. return;
  610. }
  611. reg_clear(priv, REG_DIP_IF_FLAGS, bit);
  612. reg_write_range(priv, addr, buf, len);
  613. reg_set(priv, REG_DIP_IF_FLAGS, bit);
  614. }
  615. static int tda998x_write_aif(struct tda998x_priv *priv,
  616. struct hdmi_audio_infoframe *cea)
  617. {
  618. union hdmi_infoframe frame;
  619. frame.audio = *cea;
  620. tda998x_write_if(priv, DIP_IF_FLAGS_IF4, REG_IF4_HB0, &frame);
  621. return 0;
  622. }
  623. static void
  624. tda998x_write_avi(struct tda998x_priv *priv, struct drm_display_mode *mode)
  625. {
  626. union hdmi_infoframe frame;
  627. drm_hdmi_avi_infoframe_from_display_mode(&frame.avi, mode, false);
  628. frame.avi.quantization_range = HDMI_QUANTIZATION_RANGE_FULL;
  629. tda998x_write_if(priv, DIP_IF_FLAGS_IF2, REG_IF2_HB0, &frame);
  630. }
  631. /* Audio support */
  632. static void tda998x_audio_mute(struct tda998x_priv *priv, bool on)
  633. {
  634. if (on) {
  635. reg_set(priv, REG_SOFTRESET, SOFTRESET_AUDIO);
  636. reg_clear(priv, REG_SOFTRESET, SOFTRESET_AUDIO);
  637. reg_set(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_FIFO);
  638. } else {
  639. reg_clear(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_FIFO);
  640. }
  641. }
  642. static int
  643. tda998x_configure_audio(struct tda998x_priv *priv,
  644. struct tda998x_audio_params *params)
  645. {
  646. u8 buf[6], clksel_aip, clksel_fs, cts_n, adiv;
  647. u32 n;
  648. /* Enable audio ports */
  649. reg_write(priv, REG_ENA_AP, params->config);
  650. /* Set audio input source */
  651. switch (params->format) {
  652. case AFMT_SPDIF:
  653. reg_write(priv, REG_ENA_ACLK, 0);
  654. reg_write(priv, REG_MUX_AP, MUX_AP_SELECT_SPDIF);
  655. clksel_aip = AIP_CLKSEL_AIP_SPDIF;
  656. clksel_fs = AIP_CLKSEL_FS_FS64SPDIF;
  657. cts_n = CTS_N_M(3) | CTS_N_K(3);
  658. break;
  659. case AFMT_I2S:
  660. reg_write(priv, REG_ENA_ACLK, 1);
  661. reg_write(priv, REG_MUX_AP, MUX_AP_SELECT_I2S);
  662. clksel_aip = AIP_CLKSEL_AIP_I2S;
  663. clksel_fs = AIP_CLKSEL_FS_ACLK;
  664. switch (params->sample_width) {
  665. case 16:
  666. cts_n = CTS_N_M(3) | CTS_N_K(1);
  667. break;
  668. case 18:
  669. case 20:
  670. case 24:
  671. cts_n = CTS_N_M(3) | CTS_N_K(2);
  672. break;
  673. default:
  674. case 32:
  675. cts_n = CTS_N_M(3) | CTS_N_K(3);
  676. break;
  677. }
  678. break;
  679. default:
  680. dev_err(&priv->hdmi->dev, "Unsupported I2S format\n");
  681. return -EINVAL;
  682. }
  683. reg_write(priv, REG_AIP_CLKSEL, clksel_aip);
  684. reg_clear(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_LAYOUT |
  685. AIP_CNTRL_0_ACR_MAN); /* auto CTS */
  686. reg_write(priv, REG_CTS_N, cts_n);
  687. /*
  688. * Audio input somehow depends on HDMI line rate which is
  689. * related to pixclk. Testing showed that modes with pixclk
  690. * >100MHz need a larger divider while <40MHz need the default.
  691. * There is no detailed info in the datasheet, so we just
  692. * assume 100MHz requires larger divider.
  693. */
  694. adiv = AUDIO_DIV_SERCLK_8;
  695. if (priv->tmds_clock > 100000)
  696. adiv++; /* AUDIO_DIV_SERCLK_16 */
  697. /* S/PDIF asks for a larger divider */
  698. if (params->format == AFMT_SPDIF)
  699. adiv++; /* AUDIO_DIV_SERCLK_16 or _32 */
  700. reg_write(priv, REG_AUDIO_DIV, adiv);
  701. /*
  702. * This is the approximate value of N, which happens to be
  703. * the recommended values for non-coherent clocks.
  704. */
  705. n = 128 * params->sample_rate / 1000;
  706. /* Write the CTS and N values */
  707. buf[0] = 0x44;
  708. buf[1] = 0x42;
  709. buf[2] = 0x01;
  710. buf[3] = n;
  711. buf[4] = n >> 8;
  712. buf[5] = n >> 16;
  713. reg_write_range(priv, REG_ACR_CTS_0, buf, 6);
  714. /* Set CTS clock reference */
  715. reg_write(priv, REG_AIP_CLKSEL, clksel_aip | clksel_fs);
  716. /* Reset CTS generator */
  717. reg_set(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_CTS);
  718. reg_clear(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_CTS);
  719. /* Write the channel status
  720. * The REG_CH_STAT_B-registers skip IEC958 AES2 byte, because
  721. * there is a separate register for each I2S wire.
  722. */
  723. buf[0] = params->status[0];
  724. buf[1] = params->status[1];
  725. buf[2] = params->status[3];
  726. buf[3] = params->status[4];
  727. reg_write_range(priv, REG_CH_STAT_B(0), buf, 4);
  728. tda998x_audio_mute(priv, true);
  729. msleep(20);
  730. tda998x_audio_mute(priv, false);
  731. return tda998x_write_aif(priv, &params->cea);
  732. }
  733. static int tda998x_audio_hw_params(struct device *dev, void *data,
  734. struct hdmi_codec_daifmt *daifmt,
  735. struct hdmi_codec_params *params)
  736. {
  737. struct tda998x_priv *priv = dev_get_drvdata(dev);
  738. int i, ret;
  739. struct tda998x_audio_params audio = {
  740. .sample_width = params->sample_width,
  741. .sample_rate = params->sample_rate,
  742. .cea = params->cea,
  743. };
  744. memcpy(audio.status, params->iec.status,
  745. min(sizeof(audio.status), sizeof(params->iec.status)));
  746. switch (daifmt->fmt) {
  747. case HDMI_I2S:
  748. if (daifmt->bit_clk_inv || daifmt->frame_clk_inv ||
  749. daifmt->bit_clk_master || daifmt->frame_clk_master) {
  750. dev_err(dev, "%s: Bad flags %d %d %d %d\n", __func__,
  751. daifmt->bit_clk_inv, daifmt->frame_clk_inv,
  752. daifmt->bit_clk_master,
  753. daifmt->frame_clk_master);
  754. return -EINVAL;
  755. }
  756. for (i = 0; i < ARRAY_SIZE(priv->audio_port); i++)
  757. if (priv->audio_port[i].format == AFMT_I2S)
  758. audio.config = priv->audio_port[i].config;
  759. audio.format = AFMT_I2S;
  760. break;
  761. case HDMI_SPDIF:
  762. for (i = 0; i < ARRAY_SIZE(priv->audio_port); i++)
  763. if (priv->audio_port[i].format == AFMT_SPDIF)
  764. audio.config = priv->audio_port[i].config;
  765. audio.format = AFMT_SPDIF;
  766. break;
  767. default:
  768. dev_err(dev, "%s: Invalid format %d\n", __func__, daifmt->fmt);
  769. return -EINVAL;
  770. }
  771. if (audio.config == 0) {
  772. dev_err(dev, "%s: No audio configuration found\n", __func__);
  773. return -EINVAL;
  774. }
  775. mutex_lock(&priv->audio_mutex);
  776. if (priv->supports_infoframes && priv->sink_has_audio)
  777. ret = tda998x_configure_audio(priv, &audio);
  778. else
  779. ret = 0;
  780. if (ret == 0)
  781. priv->audio_params = audio;
  782. mutex_unlock(&priv->audio_mutex);
  783. return ret;
  784. }
  785. static void tda998x_audio_shutdown(struct device *dev, void *data)
  786. {
  787. struct tda998x_priv *priv = dev_get_drvdata(dev);
  788. mutex_lock(&priv->audio_mutex);
  789. reg_write(priv, REG_ENA_AP, 0);
  790. priv->audio_params.format = AFMT_UNUSED;
  791. mutex_unlock(&priv->audio_mutex);
  792. }
  793. int tda998x_audio_digital_mute(struct device *dev, void *data, bool enable)
  794. {
  795. struct tda998x_priv *priv = dev_get_drvdata(dev);
  796. mutex_lock(&priv->audio_mutex);
  797. tda998x_audio_mute(priv, enable);
  798. mutex_unlock(&priv->audio_mutex);
  799. return 0;
  800. }
  801. static int tda998x_audio_get_eld(struct device *dev, void *data,
  802. uint8_t *buf, size_t len)
  803. {
  804. struct tda998x_priv *priv = dev_get_drvdata(dev);
  805. mutex_lock(&priv->audio_mutex);
  806. memcpy(buf, priv->connector.eld,
  807. min(sizeof(priv->connector.eld), len));
  808. mutex_unlock(&priv->audio_mutex);
  809. return 0;
  810. }
  811. static const struct hdmi_codec_ops audio_codec_ops = {
  812. .hw_params = tda998x_audio_hw_params,
  813. .audio_shutdown = tda998x_audio_shutdown,
  814. .digital_mute = tda998x_audio_digital_mute,
  815. .get_eld = tda998x_audio_get_eld,
  816. };
  817. static int tda998x_audio_codec_init(struct tda998x_priv *priv,
  818. struct device *dev)
  819. {
  820. struct hdmi_codec_pdata codec_data = {
  821. .ops = &audio_codec_ops,
  822. .max_i2s_channels = 2,
  823. };
  824. int i;
  825. for (i = 0; i < ARRAY_SIZE(priv->audio_port); i++) {
  826. if (priv->audio_port[i].format == AFMT_I2S &&
  827. priv->audio_port[i].config != 0)
  828. codec_data.i2s = 1;
  829. if (priv->audio_port[i].format == AFMT_SPDIF &&
  830. priv->audio_port[i].config != 0)
  831. codec_data.spdif = 1;
  832. }
  833. priv->audio_pdev = platform_device_register_data(
  834. dev, HDMI_CODEC_DRV_NAME, PLATFORM_DEVID_AUTO,
  835. &codec_data, sizeof(codec_data));
  836. return PTR_ERR_OR_ZERO(priv->audio_pdev);
  837. }
  838. /* DRM connector functions */
  839. static int tda998x_connector_fill_modes(struct drm_connector *connector,
  840. uint32_t maxX, uint32_t maxY)
  841. {
  842. struct tda998x_priv *priv = conn_to_tda998x_priv(connector);
  843. int ret;
  844. mutex_lock(&priv->audio_mutex);
  845. ret = drm_helper_probe_single_connector_modes(connector, maxX, maxY);
  846. if (connector->edid_blob_ptr) {
  847. struct edid *edid = (void *)connector->edid_blob_ptr->data;
  848. priv->sink_has_audio = drm_detect_monitor_audio(edid);
  849. } else {
  850. priv->sink_has_audio = false;
  851. }
  852. mutex_unlock(&priv->audio_mutex);
  853. return ret;
  854. }
  855. static enum drm_connector_status
  856. tda998x_connector_detect(struct drm_connector *connector, bool force)
  857. {
  858. struct tda998x_priv *priv = conn_to_tda998x_priv(connector);
  859. u8 val = cec_read(priv, REG_CEC_RXSHPDLEV);
  860. return (val & CEC_RXSHPDLEV_HPD) ? connector_status_connected :
  861. connector_status_disconnected;
  862. }
  863. static void tda998x_connector_destroy(struct drm_connector *connector)
  864. {
  865. drm_connector_cleanup(connector);
  866. }
  867. static const struct drm_connector_funcs tda998x_connector_funcs = {
  868. .dpms = drm_helper_connector_dpms,
  869. .reset = drm_atomic_helper_connector_reset,
  870. .fill_modes = tda998x_connector_fill_modes,
  871. .detect = tda998x_connector_detect,
  872. .destroy = tda998x_connector_destroy,
  873. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  874. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  875. };
  876. static int read_edid_block(void *data, u8 *buf, unsigned int blk, size_t length)
  877. {
  878. struct tda998x_priv *priv = data;
  879. u8 offset, segptr;
  880. int ret, i;
  881. offset = (blk & 1) ? 128 : 0;
  882. segptr = blk / 2;
  883. reg_write(priv, REG_DDC_ADDR, 0xa0);
  884. reg_write(priv, REG_DDC_OFFS, offset);
  885. reg_write(priv, REG_DDC_SEGM_ADDR, 0x60);
  886. reg_write(priv, REG_DDC_SEGM, segptr);
  887. /* enable reading EDID: */
  888. priv->wq_edid_wait = 1;
  889. reg_write(priv, REG_EDID_CTRL, 0x1);
  890. /* flag must be cleared by sw: */
  891. reg_write(priv, REG_EDID_CTRL, 0x0);
  892. /* wait for block read to complete: */
  893. if (priv->hdmi->irq) {
  894. i = wait_event_timeout(priv->wq_edid,
  895. !priv->wq_edid_wait,
  896. msecs_to_jiffies(100));
  897. if (i < 0) {
  898. dev_err(&priv->hdmi->dev, "read edid wait err %d\n", i);
  899. return i;
  900. }
  901. } else {
  902. for (i = 100; i > 0; i--) {
  903. msleep(1);
  904. ret = reg_read(priv, REG_INT_FLAGS_2);
  905. if (ret < 0)
  906. return ret;
  907. if (ret & INT_FLAGS_2_EDID_BLK_RD)
  908. break;
  909. }
  910. }
  911. if (i == 0) {
  912. dev_err(&priv->hdmi->dev, "read edid timeout\n");
  913. return -ETIMEDOUT;
  914. }
  915. ret = reg_read_range(priv, REG_EDID_DATA_0, buf, length);
  916. if (ret != length) {
  917. dev_err(&priv->hdmi->dev, "failed to read edid block %d: %d\n",
  918. blk, ret);
  919. return ret;
  920. }
  921. return 0;
  922. }
  923. static int tda998x_connector_get_modes(struct drm_connector *connector)
  924. {
  925. struct tda998x_priv *priv = conn_to_tda998x_priv(connector);
  926. struct edid *edid;
  927. int n;
  928. /*
  929. * If we get killed while waiting for the HPD timeout, return
  930. * no modes found: we are not in a restartable path, so we
  931. * can't handle signals gracefully.
  932. */
  933. if (tda998x_edid_delay_wait(priv))
  934. return 0;
  935. if (priv->rev == TDA19988)
  936. reg_clear(priv, REG_TX4, TX4_PD_RAM);
  937. edid = drm_do_get_edid(connector, read_edid_block, priv);
  938. if (priv->rev == TDA19988)
  939. reg_set(priv, REG_TX4, TX4_PD_RAM);
  940. if (!edid) {
  941. dev_warn(&priv->hdmi->dev, "failed to read EDID\n");
  942. return 0;
  943. }
  944. drm_mode_connector_update_edid_property(connector, edid);
  945. n = drm_add_edid_modes(connector, edid);
  946. drm_edid_to_eld(connector, edid);
  947. kfree(edid);
  948. return n;
  949. }
  950. static int tda998x_connector_mode_valid(struct drm_connector *connector,
  951. struct drm_display_mode *mode)
  952. {
  953. /* TDA19988 dotclock can go up to 165MHz */
  954. struct tda998x_priv *priv = conn_to_tda998x_priv(connector);
  955. if (mode->clock > ((priv->rev == TDA19988) ? 165000 : 150000))
  956. return MODE_CLOCK_HIGH;
  957. if (mode->htotal >= BIT(13))
  958. return MODE_BAD_HVALUE;
  959. if (mode->vtotal >= BIT(11))
  960. return MODE_BAD_VVALUE;
  961. return MODE_OK;
  962. }
  963. static struct drm_encoder *
  964. tda998x_connector_best_encoder(struct drm_connector *connector)
  965. {
  966. struct tda998x_priv *priv = conn_to_tda998x_priv(connector);
  967. return &priv->encoder;
  968. }
  969. static
  970. const struct drm_connector_helper_funcs tda998x_connector_helper_funcs = {
  971. .get_modes = tda998x_connector_get_modes,
  972. .mode_valid = tda998x_connector_mode_valid,
  973. .best_encoder = tda998x_connector_best_encoder,
  974. };
  975. static int tda998x_connector_init(struct tda998x_priv *priv,
  976. struct drm_device *drm)
  977. {
  978. struct drm_connector *connector = &priv->connector;
  979. int ret;
  980. connector->interlace_allowed = 1;
  981. if (priv->hdmi->irq)
  982. connector->polled = DRM_CONNECTOR_POLL_HPD;
  983. else
  984. connector->polled = DRM_CONNECTOR_POLL_CONNECT |
  985. DRM_CONNECTOR_POLL_DISCONNECT;
  986. drm_connector_helper_add(connector, &tda998x_connector_helper_funcs);
  987. ret = drm_connector_init(drm, connector, &tda998x_connector_funcs,
  988. DRM_MODE_CONNECTOR_HDMIA);
  989. if (ret)
  990. return ret;
  991. drm_mode_connector_attach_encoder(&priv->connector, &priv->encoder);
  992. return 0;
  993. }
  994. /* DRM encoder functions */
  995. static void tda998x_encoder_dpms(struct drm_encoder *encoder, int mode)
  996. {
  997. struct tda998x_priv *priv = enc_to_tda998x_priv(encoder);
  998. bool on;
  999. /* we only care about on or off: */
  1000. on = mode == DRM_MODE_DPMS_ON;
  1001. if (on == priv->is_on)
  1002. return;
  1003. if (on) {
  1004. /* enable video ports, audio will be enabled later */
  1005. reg_write(priv, REG_ENA_VP_0, 0xff);
  1006. reg_write(priv, REG_ENA_VP_1, 0xff);
  1007. reg_write(priv, REG_ENA_VP_2, 0xff);
  1008. /* set muxing after enabling ports: */
  1009. reg_write(priv, REG_VIP_CNTRL_0, priv->vip_cntrl_0);
  1010. reg_write(priv, REG_VIP_CNTRL_1, priv->vip_cntrl_1);
  1011. reg_write(priv, REG_VIP_CNTRL_2, priv->vip_cntrl_2);
  1012. priv->is_on = true;
  1013. } else {
  1014. /* disable video ports */
  1015. reg_write(priv, REG_ENA_VP_0, 0x00);
  1016. reg_write(priv, REG_ENA_VP_1, 0x00);
  1017. reg_write(priv, REG_ENA_VP_2, 0x00);
  1018. priv->is_on = false;
  1019. }
  1020. }
  1021. static void
  1022. tda998x_encoder_mode_set(struct drm_encoder *encoder,
  1023. struct drm_display_mode *mode,
  1024. struct drm_display_mode *adjusted_mode)
  1025. {
  1026. struct tda998x_priv *priv = enc_to_tda998x_priv(encoder);
  1027. u16 ref_pix, ref_line, n_pix, n_line;
  1028. u16 hs_pix_s, hs_pix_e;
  1029. u16 vs1_pix_s, vs1_pix_e, vs1_line_s, vs1_line_e;
  1030. u16 vs2_pix_s, vs2_pix_e, vs2_line_s, vs2_line_e;
  1031. u16 vwin1_line_s, vwin1_line_e;
  1032. u16 vwin2_line_s, vwin2_line_e;
  1033. u16 de_pix_s, de_pix_e;
  1034. u8 reg, div, rep;
  1035. /*
  1036. * Internally TDA998x is using ITU-R BT.656 style sync but
  1037. * we get VESA style sync. TDA998x is using a reference pixel
  1038. * relative to ITU to sync to the input frame and for output
  1039. * sync generation. Currently, we are using reference detection
  1040. * from HS/VS, i.e. REFPIX/REFLINE denote frame start sync point
  1041. * which is position of rising VS with coincident rising HS.
  1042. *
  1043. * Now there is some issues to take care of:
  1044. * - HDMI data islands require sync-before-active
  1045. * - TDA998x register values must be > 0 to be enabled
  1046. * - REFLINE needs an additional offset of +1
  1047. * - REFPIX needs an addtional offset of +1 for UYUV and +3 for RGB
  1048. *
  1049. * So we add +1 to all horizontal and vertical register values,
  1050. * plus an additional +3 for REFPIX as we are using RGB input only.
  1051. */
  1052. n_pix = mode->htotal;
  1053. n_line = mode->vtotal;
  1054. hs_pix_e = mode->hsync_end - mode->hdisplay;
  1055. hs_pix_s = mode->hsync_start - mode->hdisplay;
  1056. de_pix_e = mode->htotal;
  1057. de_pix_s = mode->htotal - mode->hdisplay;
  1058. ref_pix = 3 + hs_pix_s;
  1059. /*
  1060. * Attached LCD controllers may generate broken sync. Allow
  1061. * those to adjust the position of the rising VS edge by adding
  1062. * HSKEW to ref_pix.
  1063. */
  1064. if (adjusted_mode->flags & DRM_MODE_FLAG_HSKEW)
  1065. ref_pix += adjusted_mode->hskew;
  1066. if ((mode->flags & DRM_MODE_FLAG_INTERLACE) == 0) {
  1067. ref_line = 1 + mode->vsync_start - mode->vdisplay;
  1068. vwin1_line_s = mode->vtotal - mode->vdisplay - 1;
  1069. vwin1_line_e = vwin1_line_s + mode->vdisplay;
  1070. vs1_pix_s = vs1_pix_e = hs_pix_s;
  1071. vs1_line_s = mode->vsync_start - mode->vdisplay;
  1072. vs1_line_e = vs1_line_s +
  1073. mode->vsync_end - mode->vsync_start;
  1074. vwin2_line_s = vwin2_line_e = 0;
  1075. vs2_pix_s = vs2_pix_e = 0;
  1076. vs2_line_s = vs2_line_e = 0;
  1077. } else {
  1078. ref_line = 1 + (mode->vsync_start - mode->vdisplay)/2;
  1079. vwin1_line_s = (mode->vtotal - mode->vdisplay)/2;
  1080. vwin1_line_e = vwin1_line_s + mode->vdisplay/2;
  1081. vs1_pix_s = vs1_pix_e = hs_pix_s;
  1082. vs1_line_s = (mode->vsync_start - mode->vdisplay)/2;
  1083. vs1_line_e = vs1_line_s +
  1084. (mode->vsync_end - mode->vsync_start)/2;
  1085. vwin2_line_s = vwin1_line_s + mode->vtotal/2;
  1086. vwin2_line_e = vwin2_line_s + mode->vdisplay/2;
  1087. vs2_pix_s = vs2_pix_e = hs_pix_s + mode->htotal/2;
  1088. vs2_line_s = vs1_line_s + mode->vtotal/2 ;
  1089. vs2_line_e = vs2_line_s +
  1090. (mode->vsync_end - mode->vsync_start)/2;
  1091. }
  1092. div = 148500 / mode->clock;
  1093. if (div != 0) {
  1094. div--;
  1095. if (div > 3)
  1096. div = 3;
  1097. }
  1098. mutex_lock(&priv->audio_mutex);
  1099. /* mute the audio FIFO: */
  1100. reg_set(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_FIFO);
  1101. /* set HDMI HDCP mode off: */
  1102. reg_write(priv, REG_TBG_CNTRL_1, TBG_CNTRL_1_DWIN_DIS);
  1103. reg_clear(priv, REG_TX33, TX33_HDMI);
  1104. reg_write(priv, REG_ENC_CNTRL, ENC_CNTRL_CTL_CODE(0));
  1105. /* no pre-filter or interpolator: */
  1106. reg_write(priv, REG_HVF_CNTRL_0, HVF_CNTRL_0_PREFIL(0) |
  1107. HVF_CNTRL_0_INTPOL(0));
  1108. reg_set(priv, REG_FEAT_POWERDOWN, FEAT_POWERDOWN_PREFILT);
  1109. reg_write(priv, REG_VIP_CNTRL_5, VIP_CNTRL_5_SP_CNT(0));
  1110. reg_write(priv, REG_VIP_CNTRL_4, VIP_CNTRL_4_BLANKIT(0) |
  1111. VIP_CNTRL_4_BLC(0));
  1112. reg_clear(priv, REG_PLL_SERIAL_1, PLL_SERIAL_1_SRL_MAN_IZ);
  1113. reg_clear(priv, REG_PLL_SERIAL_3, PLL_SERIAL_3_SRL_CCIR |
  1114. PLL_SERIAL_3_SRL_DE);
  1115. reg_write(priv, REG_SERIALIZER, 0);
  1116. reg_write(priv, REG_HVF_CNTRL_1, HVF_CNTRL_1_VQR(0));
  1117. /* TODO enable pixel repeat for pixel rates less than 25Msamp/s */
  1118. rep = 0;
  1119. reg_write(priv, REG_RPT_CNTRL, 0);
  1120. reg_write(priv, REG_SEL_CLK, SEL_CLK_SEL_VRF_CLK(0) |
  1121. SEL_CLK_SEL_CLK1 | SEL_CLK_ENA_SC_CLK);
  1122. reg_write(priv, REG_PLL_SERIAL_2, PLL_SERIAL_2_SRL_NOSC(div) |
  1123. PLL_SERIAL_2_SRL_PR(rep));
  1124. /* set color matrix bypass flag: */
  1125. reg_write(priv, REG_MAT_CONTRL, MAT_CONTRL_MAT_BP |
  1126. MAT_CONTRL_MAT_SC(1));
  1127. reg_set(priv, REG_FEAT_POWERDOWN, FEAT_POWERDOWN_CSC);
  1128. /* set BIAS tmds value: */
  1129. reg_write(priv, REG_ANA_GENERAL, 0x09);
  1130. /*
  1131. * Sync on rising HSYNC/VSYNC
  1132. */
  1133. reg = VIP_CNTRL_3_SYNC_HS;
  1134. /*
  1135. * TDA19988 requires high-active sync at input stage,
  1136. * so invert low-active sync provided by master encoder here
  1137. */
  1138. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  1139. reg |= VIP_CNTRL_3_H_TGL;
  1140. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  1141. reg |= VIP_CNTRL_3_V_TGL;
  1142. reg_write(priv, REG_VIP_CNTRL_3, reg);
  1143. reg_write(priv, REG_VIDFORMAT, 0x00);
  1144. reg_write16(priv, REG_REFPIX_MSB, ref_pix);
  1145. reg_write16(priv, REG_REFLINE_MSB, ref_line);
  1146. reg_write16(priv, REG_NPIX_MSB, n_pix);
  1147. reg_write16(priv, REG_NLINE_MSB, n_line);
  1148. reg_write16(priv, REG_VS_LINE_STRT_1_MSB, vs1_line_s);
  1149. reg_write16(priv, REG_VS_PIX_STRT_1_MSB, vs1_pix_s);
  1150. reg_write16(priv, REG_VS_LINE_END_1_MSB, vs1_line_e);
  1151. reg_write16(priv, REG_VS_PIX_END_1_MSB, vs1_pix_e);
  1152. reg_write16(priv, REG_VS_LINE_STRT_2_MSB, vs2_line_s);
  1153. reg_write16(priv, REG_VS_PIX_STRT_2_MSB, vs2_pix_s);
  1154. reg_write16(priv, REG_VS_LINE_END_2_MSB, vs2_line_e);
  1155. reg_write16(priv, REG_VS_PIX_END_2_MSB, vs2_pix_e);
  1156. reg_write16(priv, REG_HS_PIX_START_MSB, hs_pix_s);
  1157. reg_write16(priv, REG_HS_PIX_STOP_MSB, hs_pix_e);
  1158. reg_write16(priv, REG_VWIN_START_1_MSB, vwin1_line_s);
  1159. reg_write16(priv, REG_VWIN_END_1_MSB, vwin1_line_e);
  1160. reg_write16(priv, REG_VWIN_START_2_MSB, vwin2_line_s);
  1161. reg_write16(priv, REG_VWIN_END_2_MSB, vwin2_line_e);
  1162. reg_write16(priv, REG_DE_START_MSB, de_pix_s);
  1163. reg_write16(priv, REG_DE_STOP_MSB, de_pix_e);
  1164. if (priv->rev == TDA19988) {
  1165. /* let incoming pixels fill the active space (if any) */
  1166. reg_write(priv, REG_ENABLE_SPACE, 0x00);
  1167. }
  1168. /*
  1169. * Always generate sync polarity relative to input sync and
  1170. * revert input stage toggled sync at output stage
  1171. */
  1172. reg = TBG_CNTRL_1_DWIN_DIS | TBG_CNTRL_1_TGL_EN;
  1173. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  1174. reg |= TBG_CNTRL_1_H_TGL;
  1175. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  1176. reg |= TBG_CNTRL_1_V_TGL;
  1177. reg_write(priv, REG_TBG_CNTRL_1, reg);
  1178. /* must be last register set: */
  1179. reg_write(priv, REG_TBG_CNTRL_0, 0);
  1180. priv->tmds_clock = adjusted_mode->clock;
  1181. /* CEA-861B section 6 says that:
  1182. * CEA version 1 (CEA-861) has no support for infoframes.
  1183. * CEA version 2 (CEA-861A) supports version 1 AVI infoframes,
  1184. * and optional basic audio.
  1185. * CEA version 3 (CEA-861B) supports version 1 and 2 AVI infoframes,
  1186. * and optional digital audio, with audio infoframes.
  1187. *
  1188. * Since we only support generation of version 2 AVI infoframes,
  1189. * ignore CEA version 2 and below (iow, behave as if we're a
  1190. * CEA-861 source.)
  1191. */
  1192. priv->supports_infoframes = priv->connector.display_info.cea_rev >= 3;
  1193. if (priv->supports_infoframes) {
  1194. /* We need to turn HDMI HDCP stuff on to get audio through */
  1195. reg &= ~TBG_CNTRL_1_DWIN_DIS;
  1196. reg_write(priv, REG_TBG_CNTRL_1, reg);
  1197. reg_write(priv, REG_ENC_CNTRL, ENC_CNTRL_CTL_CODE(1));
  1198. reg_set(priv, REG_TX33, TX33_HDMI);
  1199. tda998x_write_avi(priv, adjusted_mode);
  1200. if (priv->audio_params.format != AFMT_UNUSED &&
  1201. priv->sink_has_audio)
  1202. tda998x_configure_audio(priv, &priv->audio_params);
  1203. }
  1204. mutex_unlock(&priv->audio_mutex);
  1205. }
  1206. static void tda998x_destroy(struct tda998x_priv *priv)
  1207. {
  1208. /* disable all IRQs and free the IRQ handler */
  1209. cec_write(priv, REG_CEC_RXSHPDINTENA, 0);
  1210. reg_clear(priv, REG_INT_FLAGS_2, INT_FLAGS_2_EDID_BLK_RD);
  1211. if (priv->audio_pdev)
  1212. platform_device_unregister(priv->audio_pdev);
  1213. if (priv->hdmi->irq)
  1214. free_irq(priv->hdmi->irq, priv);
  1215. del_timer_sync(&priv->edid_delay_timer);
  1216. cancel_work_sync(&priv->detect_work);
  1217. i2c_unregister_device(priv->cec);
  1218. }
  1219. /* I2C driver functions */
  1220. static int tda998x_get_audio_ports(struct tda998x_priv *priv,
  1221. struct device_node *np)
  1222. {
  1223. const u32 *port_data;
  1224. u32 size;
  1225. int i;
  1226. port_data = of_get_property(np, "audio-ports", &size);
  1227. if (!port_data)
  1228. return 0;
  1229. size /= sizeof(u32);
  1230. if (size > 2 * ARRAY_SIZE(priv->audio_port) || size % 2 != 0) {
  1231. dev_err(&priv->hdmi->dev,
  1232. "Bad number of elements in audio-ports dt-property\n");
  1233. return -EINVAL;
  1234. }
  1235. size /= 2;
  1236. for (i = 0; i < size; i++) {
  1237. u8 afmt = be32_to_cpup(&port_data[2*i]);
  1238. u8 ena_ap = be32_to_cpup(&port_data[2*i+1]);
  1239. if (afmt != AFMT_SPDIF && afmt != AFMT_I2S) {
  1240. dev_err(&priv->hdmi->dev,
  1241. "Bad audio format %u\n", afmt);
  1242. return -EINVAL;
  1243. }
  1244. priv->audio_port[i].format = afmt;
  1245. priv->audio_port[i].config = ena_ap;
  1246. }
  1247. if (priv->audio_port[0].format == priv->audio_port[1].format) {
  1248. dev_err(&priv->hdmi->dev,
  1249. "There can only be on I2S port and one SPDIF port\n");
  1250. return -EINVAL;
  1251. }
  1252. return 0;
  1253. }
  1254. static int tda998x_create(struct i2c_client *client, struct tda998x_priv *priv)
  1255. {
  1256. struct device_node *np = client->dev.of_node;
  1257. u32 video;
  1258. int rev_lo, rev_hi, ret;
  1259. mutex_init(&priv->audio_mutex); /* Protect access from audio thread */
  1260. priv->vip_cntrl_0 = VIP_CNTRL_0_SWAP_A(2) | VIP_CNTRL_0_SWAP_B(3);
  1261. priv->vip_cntrl_1 = VIP_CNTRL_1_SWAP_C(0) | VIP_CNTRL_1_SWAP_D(1);
  1262. priv->vip_cntrl_2 = VIP_CNTRL_2_SWAP_E(4) | VIP_CNTRL_2_SWAP_F(5);
  1263. /* CEC I2C address bound to TDA998x I2C addr by configuration pins */
  1264. priv->cec_addr = 0x34 + (client->addr & 0x03);
  1265. priv->current_page = 0xff;
  1266. priv->hdmi = client;
  1267. priv->cec = i2c_new_dummy(client->adapter, priv->cec_addr);
  1268. if (!priv->cec)
  1269. return -ENODEV;
  1270. mutex_init(&priv->mutex); /* protect the page access */
  1271. init_waitqueue_head(&priv->edid_delay_waitq);
  1272. setup_timer(&priv->edid_delay_timer, tda998x_edid_delay_done,
  1273. (unsigned long)priv);
  1274. INIT_WORK(&priv->detect_work, tda998x_detect_work);
  1275. /* wake up the device: */
  1276. cec_write(priv, REG_CEC_ENAMODS,
  1277. CEC_ENAMODS_EN_RXSENS | CEC_ENAMODS_EN_HDMI);
  1278. tda998x_reset(priv);
  1279. /* read version: */
  1280. rev_lo = reg_read(priv, REG_VERSION_LSB);
  1281. rev_hi = reg_read(priv, REG_VERSION_MSB);
  1282. if (rev_lo < 0 || rev_hi < 0) {
  1283. ret = rev_lo < 0 ? rev_lo : rev_hi;
  1284. goto fail;
  1285. }
  1286. priv->rev = rev_lo | rev_hi << 8;
  1287. /* mask off feature bits: */
  1288. priv->rev &= ~0x30; /* not-hdcp and not-scalar bit */
  1289. switch (priv->rev) {
  1290. case TDA9989N2:
  1291. dev_info(&client->dev, "found TDA9989 n2");
  1292. break;
  1293. case TDA19989:
  1294. dev_info(&client->dev, "found TDA19989");
  1295. break;
  1296. case TDA19989N2:
  1297. dev_info(&client->dev, "found TDA19989 n2");
  1298. break;
  1299. case TDA19988:
  1300. dev_info(&client->dev, "found TDA19988");
  1301. break;
  1302. default:
  1303. dev_err(&client->dev, "found unsupported device: %04x\n",
  1304. priv->rev);
  1305. goto fail;
  1306. }
  1307. /* after reset, enable DDC: */
  1308. reg_write(priv, REG_DDC_DISABLE, 0x00);
  1309. /* set clock on DDC channel: */
  1310. reg_write(priv, REG_TX3, 39);
  1311. /* if necessary, disable multi-master: */
  1312. if (priv->rev == TDA19989)
  1313. reg_set(priv, REG_I2C_MASTER, I2C_MASTER_DIS_MM);
  1314. cec_write(priv, REG_CEC_FRO_IM_CLK_CTRL,
  1315. CEC_FRO_IM_CLK_CTRL_GHOST_DIS | CEC_FRO_IM_CLK_CTRL_IMCLK_SEL);
  1316. /* initialize the optional IRQ */
  1317. if (client->irq) {
  1318. unsigned long irq_flags;
  1319. /* init read EDID waitqueue and HDP work */
  1320. init_waitqueue_head(&priv->wq_edid);
  1321. /* clear pending interrupts */
  1322. reg_read(priv, REG_INT_FLAGS_0);
  1323. reg_read(priv, REG_INT_FLAGS_1);
  1324. reg_read(priv, REG_INT_FLAGS_2);
  1325. irq_flags =
  1326. irqd_get_trigger_type(irq_get_irq_data(client->irq));
  1327. irq_flags |= IRQF_SHARED | IRQF_ONESHOT;
  1328. ret = request_threaded_irq(client->irq, NULL,
  1329. tda998x_irq_thread, irq_flags,
  1330. "tda998x", priv);
  1331. if (ret) {
  1332. dev_err(&client->dev,
  1333. "failed to request IRQ#%u: %d\n",
  1334. client->irq, ret);
  1335. goto fail;
  1336. }
  1337. /* enable HPD irq */
  1338. cec_write(priv, REG_CEC_RXSHPDINTENA, CEC_RXSHPDLEV_HPD);
  1339. }
  1340. /* enable EDID read irq: */
  1341. reg_set(priv, REG_INT_FLAGS_2, INT_FLAGS_2_EDID_BLK_RD);
  1342. if (!np)
  1343. return 0; /* non-DT */
  1344. /* get the device tree parameters */
  1345. ret = of_property_read_u32(np, "video-ports", &video);
  1346. if (ret == 0) {
  1347. priv->vip_cntrl_0 = video >> 16;
  1348. priv->vip_cntrl_1 = video >> 8;
  1349. priv->vip_cntrl_2 = video;
  1350. }
  1351. ret = tda998x_get_audio_ports(priv, np);
  1352. if (ret)
  1353. goto fail;
  1354. if (priv->audio_port[0].format != AFMT_UNUSED)
  1355. tda998x_audio_codec_init(priv, &client->dev);
  1356. return 0;
  1357. fail:
  1358. /* if encoder_init fails, the encoder slave is never registered,
  1359. * so cleanup here:
  1360. */
  1361. if (priv->cec)
  1362. i2c_unregister_device(priv->cec);
  1363. return -ENXIO;
  1364. }
  1365. static void tda998x_encoder_prepare(struct drm_encoder *encoder)
  1366. {
  1367. tda998x_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
  1368. }
  1369. static void tda998x_encoder_commit(struct drm_encoder *encoder)
  1370. {
  1371. tda998x_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
  1372. }
  1373. static const struct drm_encoder_helper_funcs tda998x_encoder_helper_funcs = {
  1374. .dpms = tda998x_encoder_dpms,
  1375. .prepare = tda998x_encoder_prepare,
  1376. .commit = tda998x_encoder_commit,
  1377. .mode_set = tda998x_encoder_mode_set,
  1378. };
  1379. static void tda998x_encoder_destroy(struct drm_encoder *encoder)
  1380. {
  1381. struct tda998x_priv *priv = enc_to_tda998x_priv(encoder);
  1382. tda998x_destroy(priv);
  1383. drm_encoder_cleanup(encoder);
  1384. }
  1385. static const struct drm_encoder_funcs tda998x_encoder_funcs = {
  1386. .destroy = tda998x_encoder_destroy,
  1387. };
  1388. static void tda998x_set_config(struct tda998x_priv *priv,
  1389. const struct tda998x_encoder_params *p)
  1390. {
  1391. priv->vip_cntrl_0 = VIP_CNTRL_0_SWAP_A(p->swap_a) |
  1392. (p->mirr_a ? VIP_CNTRL_0_MIRR_A : 0) |
  1393. VIP_CNTRL_0_SWAP_B(p->swap_b) |
  1394. (p->mirr_b ? VIP_CNTRL_0_MIRR_B : 0);
  1395. priv->vip_cntrl_1 = VIP_CNTRL_1_SWAP_C(p->swap_c) |
  1396. (p->mirr_c ? VIP_CNTRL_1_MIRR_C : 0) |
  1397. VIP_CNTRL_1_SWAP_D(p->swap_d) |
  1398. (p->mirr_d ? VIP_CNTRL_1_MIRR_D : 0);
  1399. priv->vip_cntrl_2 = VIP_CNTRL_2_SWAP_E(p->swap_e) |
  1400. (p->mirr_e ? VIP_CNTRL_2_MIRR_E : 0) |
  1401. VIP_CNTRL_2_SWAP_F(p->swap_f) |
  1402. (p->mirr_f ? VIP_CNTRL_2_MIRR_F : 0);
  1403. priv->audio_params = p->audio_params;
  1404. }
  1405. static int tda998x_bind(struct device *dev, struct device *master, void *data)
  1406. {
  1407. struct tda998x_encoder_params *params = dev->platform_data;
  1408. struct i2c_client *client = to_i2c_client(dev);
  1409. struct drm_device *drm = data;
  1410. struct tda998x_priv *priv;
  1411. u32 crtcs = 0;
  1412. int ret;
  1413. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  1414. if (!priv)
  1415. return -ENOMEM;
  1416. dev_set_drvdata(dev, priv);
  1417. if (dev->of_node)
  1418. crtcs = drm_of_find_possible_crtcs(drm, dev->of_node);
  1419. /* If no CRTCs were found, fall back to our old behaviour */
  1420. if (crtcs == 0) {
  1421. dev_warn(dev, "Falling back to first CRTC\n");
  1422. crtcs = 1 << 0;
  1423. }
  1424. priv->encoder.possible_crtcs = crtcs;
  1425. ret = tda998x_create(client, priv);
  1426. if (ret)
  1427. return ret;
  1428. if (!dev->of_node && params)
  1429. tda998x_set_config(priv, params);
  1430. drm_encoder_helper_add(&priv->encoder, &tda998x_encoder_helper_funcs);
  1431. ret = drm_encoder_init(drm, &priv->encoder, &tda998x_encoder_funcs,
  1432. DRM_MODE_ENCODER_TMDS, NULL);
  1433. if (ret)
  1434. goto err_encoder;
  1435. ret = tda998x_connector_init(priv, drm);
  1436. if (ret)
  1437. goto err_connector;
  1438. return 0;
  1439. err_connector:
  1440. drm_encoder_cleanup(&priv->encoder);
  1441. err_encoder:
  1442. tda998x_destroy(priv);
  1443. return ret;
  1444. }
  1445. static void tda998x_unbind(struct device *dev, struct device *master,
  1446. void *data)
  1447. {
  1448. struct tda998x_priv *priv = dev_get_drvdata(dev);
  1449. drm_connector_cleanup(&priv->connector);
  1450. drm_encoder_cleanup(&priv->encoder);
  1451. tda998x_destroy(priv);
  1452. }
  1453. static const struct component_ops tda998x_ops = {
  1454. .bind = tda998x_bind,
  1455. .unbind = tda998x_unbind,
  1456. };
  1457. static int
  1458. tda998x_probe(struct i2c_client *client, const struct i2c_device_id *id)
  1459. {
  1460. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  1461. dev_warn(&client->dev, "adapter does not support I2C\n");
  1462. return -EIO;
  1463. }
  1464. return component_add(&client->dev, &tda998x_ops);
  1465. }
  1466. static int tda998x_remove(struct i2c_client *client)
  1467. {
  1468. component_del(&client->dev, &tda998x_ops);
  1469. return 0;
  1470. }
  1471. #ifdef CONFIG_OF
  1472. static const struct of_device_id tda998x_dt_ids[] = {
  1473. { .compatible = "nxp,tda998x", },
  1474. { }
  1475. };
  1476. MODULE_DEVICE_TABLE(of, tda998x_dt_ids);
  1477. #endif
  1478. static struct i2c_device_id tda998x_ids[] = {
  1479. { "tda998x", 0 },
  1480. { }
  1481. };
  1482. MODULE_DEVICE_TABLE(i2c, tda998x_ids);
  1483. static struct i2c_driver tda998x_driver = {
  1484. .probe = tda998x_probe,
  1485. .remove = tda998x_remove,
  1486. .driver = {
  1487. .name = "tda998x",
  1488. .of_match_table = of_match_ptr(tda998x_dt_ids),
  1489. },
  1490. .id_table = tda998x_ids,
  1491. };
  1492. module_i2c_driver(tda998x_driver);
  1493. MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
  1494. MODULE_DESCRIPTION("NXP Semiconductors TDA998X HDMI Encoder");
  1495. MODULE_LICENSE("GPL");