tda998x_drv.c 52 KB

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