tda998x_drv.c 52 KB

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