tda998x_drv.c 62 KB

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