tda998x_drv.c 58 KB

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