dw-hdmi.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658
  1. /*
  2. * DesignWare High-Definition Multimedia Interface (HDMI) driver
  3. *
  4. * Copyright (C) 2013-2015 Mentor Graphics Inc.
  5. * Copyright (C) 2011-2013 Freescale Semiconductor, Inc.
  6. * Copyright (C) 2010, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. */
  14. #include <linux/module.h>
  15. #include <linux/irq.h>
  16. #include <linux/delay.h>
  17. #include <linux/err.h>
  18. #include <linux/clk.h>
  19. #include <linux/hdmi.h>
  20. #include <linux/mutex.h>
  21. #include <linux/of_device.h>
  22. #include <linux/regmap.h>
  23. #include <linux/spinlock.h>
  24. #include <drm/drm_of.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_encoder_slave.h>
  30. #include <drm/bridge/dw_hdmi.h>
  31. #include <uapi/linux/media-bus-format.h>
  32. #include <uapi/linux/videodev2.h>
  33. #include "dw-hdmi.h"
  34. #include "dw-hdmi-audio.h"
  35. #include "dw-hdmi-cec.h"
  36. #include <media/cec-notifier.h>
  37. #define DDC_CI_ADDR 0x37
  38. #define DDC_SEGMENT_ADDR 0x30
  39. #define HDMI_EDID_LEN 512
  40. enum hdmi_datamap {
  41. RGB444_8B = 0x01,
  42. RGB444_10B = 0x03,
  43. RGB444_12B = 0x05,
  44. RGB444_16B = 0x07,
  45. YCbCr444_8B = 0x09,
  46. YCbCr444_10B = 0x0B,
  47. YCbCr444_12B = 0x0D,
  48. YCbCr444_16B = 0x0F,
  49. YCbCr422_8B = 0x16,
  50. YCbCr422_10B = 0x14,
  51. YCbCr422_12B = 0x12,
  52. };
  53. static const u16 csc_coeff_default[3][4] = {
  54. { 0x2000, 0x0000, 0x0000, 0x0000 },
  55. { 0x0000, 0x2000, 0x0000, 0x0000 },
  56. { 0x0000, 0x0000, 0x2000, 0x0000 }
  57. };
  58. static const u16 csc_coeff_rgb_out_eitu601[3][4] = {
  59. { 0x2000, 0x6926, 0x74fd, 0x010e },
  60. { 0x2000, 0x2cdd, 0x0000, 0x7e9a },
  61. { 0x2000, 0x0000, 0x38b4, 0x7e3b }
  62. };
  63. static const u16 csc_coeff_rgb_out_eitu709[3][4] = {
  64. { 0x2000, 0x7106, 0x7a02, 0x00a7 },
  65. { 0x2000, 0x3264, 0x0000, 0x7e6d },
  66. { 0x2000, 0x0000, 0x3b61, 0x7e25 }
  67. };
  68. static const u16 csc_coeff_rgb_in_eitu601[3][4] = {
  69. { 0x2591, 0x1322, 0x074b, 0x0000 },
  70. { 0x6535, 0x2000, 0x7acc, 0x0200 },
  71. { 0x6acd, 0x7534, 0x2000, 0x0200 }
  72. };
  73. static const u16 csc_coeff_rgb_in_eitu709[3][4] = {
  74. { 0x2dc5, 0x0d9b, 0x049e, 0x0000 },
  75. { 0x62f0, 0x2000, 0x7d11, 0x0200 },
  76. { 0x6756, 0x78ab, 0x2000, 0x0200 }
  77. };
  78. struct hdmi_vmode {
  79. bool mdataenablepolarity;
  80. unsigned int mpixelclock;
  81. unsigned int mpixelrepetitioninput;
  82. unsigned int mpixelrepetitionoutput;
  83. };
  84. struct hdmi_data_info {
  85. unsigned int enc_in_bus_format;
  86. unsigned int enc_out_bus_format;
  87. unsigned int enc_in_encoding;
  88. unsigned int enc_out_encoding;
  89. unsigned int pix_repet_factor;
  90. unsigned int hdcp_enable;
  91. struct hdmi_vmode video_mode;
  92. };
  93. struct dw_hdmi_i2c {
  94. struct i2c_adapter adap;
  95. struct mutex lock; /* used to serialize data transfers */
  96. struct completion cmp;
  97. u8 stat;
  98. u8 slave_reg;
  99. bool is_regaddr;
  100. bool is_segment;
  101. };
  102. struct dw_hdmi_phy_data {
  103. enum dw_hdmi_phy_type type;
  104. const char *name;
  105. unsigned int gen;
  106. bool has_svsret;
  107. int (*configure)(struct dw_hdmi *hdmi,
  108. const struct dw_hdmi_plat_data *pdata,
  109. unsigned long mpixelclock);
  110. };
  111. struct dw_hdmi {
  112. struct drm_connector connector;
  113. struct drm_bridge bridge;
  114. unsigned int version;
  115. struct platform_device *audio;
  116. struct platform_device *cec;
  117. struct device *dev;
  118. struct clk *isfr_clk;
  119. struct clk *iahb_clk;
  120. struct clk *cec_clk;
  121. struct dw_hdmi_i2c *i2c;
  122. struct hdmi_data_info hdmi_data;
  123. const struct dw_hdmi_plat_data *plat_data;
  124. int vic;
  125. u8 edid[HDMI_EDID_LEN];
  126. struct {
  127. const struct dw_hdmi_phy_ops *ops;
  128. const char *name;
  129. void *data;
  130. bool enabled;
  131. } phy;
  132. struct drm_display_mode previous_mode;
  133. struct i2c_adapter *ddc;
  134. void __iomem *regs;
  135. bool sink_is_hdmi;
  136. bool sink_has_audio;
  137. struct mutex mutex; /* for state below and previous_mode */
  138. enum drm_connector_force force; /* mutex-protected force state */
  139. bool disabled; /* DRM has disabled our bridge */
  140. bool bridge_is_on; /* indicates the bridge is on */
  141. bool rxsense; /* rxsense state */
  142. u8 phy_mask; /* desired phy int mask settings */
  143. u8 mc_clkdis; /* clock disable register */
  144. spinlock_t audio_lock;
  145. struct mutex audio_mutex;
  146. unsigned int sample_rate;
  147. unsigned int audio_cts;
  148. unsigned int audio_n;
  149. bool audio_enable;
  150. unsigned int reg_shift;
  151. struct regmap *regm;
  152. void (*enable_audio)(struct dw_hdmi *hdmi);
  153. void (*disable_audio)(struct dw_hdmi *hdmi);
  154. struct cec_notifier *cec_notifier;
  155. };
  156. #define HDMI_IH_PHY_STAT0_RX_SENSE \
  157. (HDMI_IH_PHY_STAT0_RX_SENSE0 | HDMI_IH_PHY_STAT0_RX_SENSE1 | \
  158. HDMI_IH_PHY_STAT0_RX_SENSE2 | HDMI_IH_PHY_STAT0_RX_SENSE3)
  159. #define HDMI_PHY_RX_SENSE \
  160. (HDMI_PHY_RX_SENSE0 | HDMI_PHY_RX_SENSE1 | \
  161. HDMI_PHY_RX_SENSE2 | HDMI_PHY_RX_SENSE3)
  162. static inline void hdmi_writeb(struct dw_hdmi *hdmi, u8 val, int offset)
  163. {
  164. regmap_write(hdmi->regm, offset << hdmi->reg_shift, val);
  165. }
  166. static inline u8 hdmi_readb(struct dw_hdmi *hdmi, int offset)
  167. {
  168. unsigned int val = 0;
  169. regmap_read(hdmi->regm, offset << hdmi->reg_shift, &val);
  170. return val;
  171. }
  172. static void hdmi_modb(struct dw_hdmi *hdmi, u8 data, u8 mask, unsigned reg)
  173. {
  174. regmap_update_bits(hdmi->regm, reg << hdmi->reg_shift, mask, data);
  175. }
  176. static void hdmi_mask_writeb(struct dw_hdmi *hdmi, u8 data, unsigned int reg,
  177. u8 shift, u8 mask)
  178. {
  179. hdmi_modb(hdmi, data << shift, mask, reg);
  180. }
  181. static void dw_hdmi_i2c_init(struct dw_hdmi *hdmi)
  182. {
  183. /* Software reset */
  184. hdmi_writeb(hdmi, 0x00, HDMI_I2CM_SOFTRSTZ);
  185. /* Set Standard Mode speed (determined to be 100KHz on iMX6) */
  186. hdmi_writeb(hdmi, 0x00, HDMI_I2CM_DIV);
  187. /* Set done, not acknowledged and arbitration interrupt polarities */
  188. hdmi_writeb(hdmi, HDMI_I2CM_INT_DONE_POL, HDMI_I2CM_INT);
  189. hdmi_writeb(hdmi, HDMI_I2CM_CTLINT_NAC_POL | HDMI_I2CM_CTLINT_ARB_POL,
  190. HDMI_I2CM_CTLINT);
  191. /* Clear DONE and ERROR interrupts */
  192. hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,
  193. HDMI_IH_I2CM_STAT0);
  194. /* Mute DONE and ERROR interrupts */
  195. hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,
  196. HDMI_IH_MUTE_I2CM_STAT0);
  197. }
  198. static int dw_hdmi_i2c_read(struct dw_hdmi *hdmi,
  199. unsigned char *buf, unsigned int length)
  200. {
  201. struct dw_hdmi_i2c *i2c = hdmi->i2c;
  202. int stat;
  203. if (!i2c->is_regaddr) {
  204. dev_dbg(hdmi->dev, "set read register address to 0\n");
  205. i2c->slave_reg = 0x00;
  206. i2c->is_regaddr = true;
  207. }
  208. while (length--) {
  209. reinit_completion(&i2c->cmp);
  210. hdmi_writeb(hdmi, i2c->slave_reg++, HDMI_I2CM_ADDRESS);
  211. if (i2c->is_segment)
  212. hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_READ_EXT,
  213. HDMI_I2CM_OPERATION);
  214. else
  215. hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_READ,
  216. HDMI_I2CM_OPERATION);
  217. stat = wait_for_completion_timeout(&i2c->cmp, HZ / 10);
  218. if (!stat)
  219. return -EAGAIN;
  220. /* Check for error condition on the bus */
  221. if (i2c->stat & HDMI_IH_I2CM_STAT0_ERROR)
  222. return -EIO;
  223. *buf++ = hdmi_readb(hdmi, HDMI_I2CM_DATAI);
  224. }
  225. i2c->is_segment = false;
  226. return 0;
  227. }
  228. static int dw_hdmi_i2c_write(struct dw_hdmi *hdmi,
  229. unsigned char *buf, unsigned int length)
  230. {
  231. struct dw_hdmi_i2c *i2c = hdmi->i2c;
  232. int stat;
  233. if (!i2c->is_regaddr) {
  234. /* Use the first write byte as register address */
  235. i2c->slave_reg = buf[0];
  236. length--;
  237. buf++;
  238. i2c->is_regaddr = true;
  239. }
  240. while (length--) {
  241. reinit_completion(&i2c->cmp);
  242. hdmi_writeb(hdmi, *buf++, HDMI_I2CM_DATAO);
  243. hdmi_writeb(hdmi, i2c->slave_reg++, HDMI_I2CM_ADDRESS);
  244. hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_WRITE,
  245. HDMI_I2CM_OPERATION);
  246. stat = wait_for_completion_timeout(&i2c->cmp, HZ / 10);
  247. if (!stat)
  248. return -EAGAIN;
  249. /* Check for error condition on the bus */
  250. if (i2c->stat & HDMI_IH_I2CM_STAT0_ERROR)
  251. return -EIO;
  252. }
  253. return 0;
  254. }
  255. static int dw_hdmi_i2c_xfer(struct i2c_adapter *adap,
  256. struct i2c_msg *msgs, int num)
  257. {
  258. struct dw_hdmi *hdmi = i2c_get_adapdata(adap);
  259. struct dw_hdmi_i2c *i2c = hdmi->i2c;
  260. u8 addr = msgs[0].addr;
  261. int i, ret = 0;
  262. if (addr == DDC_CI_ADDR)
  263. /*
  264. * The internal I2C controller does not support the multi-byte
  265. * read and write operations needed for DDC/CI.
  266. * TOFIX: Blacklist the DDC/CI address until we filter out
  267. * unsupported I2C operations.
  268. */
  269. return -EOPNOTSUPP;
  270. dev_dbg(hdmi->dev, "xfer: num: %d, addr: %#x\n", num, addr);
  271. for (i = 0; i < num; i++) {
  272. if (msgs[i].len == 0) {
  273. dev_dbg(hdmi->dev,
  274. "unsupported transfer %d/%d, no data\n",
  275. i + 1, num);
  276. return -EOPNOTSUPP;
  277. }
  278. }
  279. mutex_lock(&i2c->lock);
  280. /* Unmute DONE and ERROR interrupts */
  281. hdmi_writeb(hdmi, 0x00, HDMI_IH_MUTE_I2CM_STAT0);
  282. /* Set slave device address taken from the first I2C message */
  283. hdmi_writeb(hdmi, addr, HDMI_I2CM_SLAVE);
  284. /* Set slave device register address on transfer */
  285. i2c->is_regaddr = false;
  286. /* Set segment pointer for I2C extended read mode operation */
  287. i2c->is_segment = false;
  288. for (i = 0; i < num; i++) {
  289. dev_dbg(hdmi->dev, "xfer: num: %d/%d, len: %d, flags: %#x\n",
  290. i + 1, num, msgs[i].len, msgs[i].flags);
  291. if (msgs[i].addr == DDC_SEGMENT_ADDR && msgs[i].len == 1) {
  292. i2c->is_segment = true;
  293. hdmi_writeb(hdmi, DDC_SEGMENT_ADDR, HDMI_I2CM_SEGADDR);
  294. hdmi_writeb(hdmi, *msgs[i].buf, HDMI_I2CM_SEGPTR);
  295. } else {
  296. if (msgs[i].flags & I2C_M_RD)
  297. ret = dw_hdmi_i2c_read(hdmi, msgs[i].buf,
  298. msgs[i].len);
  299. else
  300. ret = dw_hdmi_i2c_write(hdmi, msgs[i].buf,
  301. msgs[i].len);
  302. }
  303. if (ret < 0)
  304. break;
  305. }
  306. if (!ret)
  307. ret = num;
  308. /* Mute DONE and ERROR interrupts */
  309. hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,
  310. HDMI_IH_MUTE_I2CM_STAT0);
  311. mutex_unlock(&i2c->lock);
  312. return ret;
  313. }
  314. static u32 dw_hdmi_i2c_func(struct i2c_adapter *adapter)
  315. {
  316. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  317. }
  318. static const struct i2c_algorithm dw_hdmi_algorithm = {
  319. .master_xfer = dw_hdmi_i2c_xfer,
  320. .functionality = dw_hdmi_i2c_func,
  321. };
  322. static struct i2c_adapter *dw_hdmi_i2c_adapter(struct dw_hdmi *hdmi)
  323. {
  324. struct i2c_adapter *adap;
  325. struct dw_hdmi_i2c *i2c;
  326. int ret;
  327. i2c = devm_kzalloc(hdmi->dev, sizeof(*i2c), GFP_KERNEL);
  328. if (!i2c)
  329. return ERR_PTR(-ENOMEM);
  330. mutex_init(&i2c->lock);
  331. init_completion(&i2c->cmp);
  332. adap = &i2c->adap;
  333. adap->class = I2C_CLASS_DDC;
  334. adap->owner = THIS_MODULE;
  335. adap->dev.parent = hdmi->dev;
  336. adap->algo = &dw_hdmi_algorithm;
  337. strlcpy(adap->name, "DesignWare HDMI", sizeof(adap->name));
  338. i2c_set_adapdata(adap, hdmi);
  339. ret = i2c_add_adapter(adap);
  340. if (ret) {
  341. dev_warn(hdmi->dev, "cannot add %s I2C adapter\n", adap->name);
  342. devm_kfree(hdmi->dev, i2c);
  343. return ERR_PTR(ret);
  344. }
  345. hdmi->i2c = i2c;
  346. dev_info(hdmi->dev, "registered %s I2C bus driver\n", adap->name);
  347. return adap;
  348. }
  349. static void hdmi_set_cts_n(struct dw_hdmi *hdmi, unsigned int cts,
  350. unsigned int n)
  351. {
  352. /* Must be set/cleared first */
  353. hdmi_modb(hdmi, 0, HDMI_AUD_CTS3_CTS_MANUAL, HDMI_AUD_CTS3);
  354. /* nshift factor = 0 */
  355. hdmi_modb(hdmi, 0, HDMI_AUD_CTS3_N_SHIFT_MASK, HDMI_AUD_CTS3);
  356. hdmi_writeb(hdmi, ((cts >> 16) & HDMI_AUD_CTS3_AUDCTS19_16_MASK) |
  357. HDMI_AUD_CTS3_CTS_MANUAL, HDMI_AUD_CTS3);
  358. hdmi_writeb(hdmi, (cts >> 8) & 0xff, HDMI_AUD_CTS2);
  359. hdmi_writeb(hdmi, cts & 0xff, HDMI_AUD_CTS1);
  360. hdmi_writeb(hdmi, (n >> 16) & 0x0f, HDMI_AUD_N3);
  361. hdmi_writeb(hdmi, (n >> 8) & 0xff, HDMI_AUD_N2);
  362. hdmi_writeb(hdmi, n & 0xff, HDMI_AUD_N1);
  363. }
  364. static unsigned int hdmi_compute_n(unsigned int freq, unsigned long pixel_clk)
  365. {
  366. unsigned int n = (128 * freq) / 1000;
  367. unsigned int mult = 1;
  368. while (freq > 48000) {
  369. mult *= 2;
  370. freq /= 2;
  371. }
  372. switch (freq) {
  373. case 32000:
  374. if (pixel_clk == 25175000)
  375. n = 4576;
  376. else if (pixel_clk == 27027000)
  377. n = 4096;
  378. else if (pixel_clk == 74176000 || pixel_clk == 148352000)
  379. n = 11648;
  380. else
  381. n = 4096;
  382. n *= mult;
  383. break;
  384. case 44100:
  385. if (pixel_clk == 25175000)
  386. n = 7007;
  387. else if (pixel_clk == 74176000)
  388. n = 17836;
  389. else if (pixel_clk == 148352000)
  390. n = 8918;
  391. else
  392. n = 6272;
  393. n *= mult;
  394. break;
  395. case 48000:
  396. if (pixel_clk == 25175000)
  397. n = 6864;
  398. else if (pixel_clk == 27027000)
  399. n = 6144;
  400. else if (pixel_clk == 74176000)
  401. n = 11648;
  402. else if (pixel_clk == 148352000)
  403. n = 5824;
  404. else
  405. n = 6144;
  406. n *= mult;
  407. break;
  408. default:
  409. break;
  410. }
  411. return n;
  412. }
  413. static void hdmi_set_clk_regenerator(struct dw_hdmi *hdmi,
  414. unsigned long pixel_clk, unsigned int sample_rate)
  415. {
  416. unsigned long ftdms = pixel_clk;
  417. unsigned int n, cts;
  418. u64 tmp;
  419. n = hdmi_compute_n(sample_rate, pixel_clk);
  420. /*
  421. * Compute the CTS value from the N value. Note that CTS and N
  422. * can be up to 20 bits in total, so we need 64-bit math. Also
  423. * note that our TDMS clock is not fully accurate; it is accurate
  424. * to kHz. This can introduce an unnecessary remainder in the
  425. * calculation below, so we don't try to warn about that.
  426. */
  427. tmp = (u64)ftdms * n;
  428. do_div(tmp, 128 * sample_rate);
  429. cts = tmp;
  430. dev_dbg(hdmi->dev, "%s: fs=%uHz ftdms=%lu.%03luMHz N=%d cts=%d\n",
  431. __func__, sample_rate, ftdms / 1000000, (ftdms / 1000) % 1000,
  432. n, cts);
  433. spin_lock_irq(&hdmi->audio_lock);
  434. hdmi->audio_n = n;
  435. hdmi->audio_cts = cts;
  436. hdmi_set_cts_n(hdmi, cts, hdmi->audio_enable ? n : 0);
  437. spin_unlock_irq(&hdmi->audio_lock);
  438. }
  439. static void hdmi_init_clk_regenerator(struct dw_hdmi *hdmi)
  440. {
  441. mutex_lock(&hdmi->audio_mutex);
  442. hdmi_set_clk_regenerator(hdmi, 74250000, hdmi->sample_rate);
  443. mutex_unlock(&hdmi->audio_mutex);
  444. }
  445. static void hdmi_clk_regenerator_update_pixel_clock(struct dw_hdmi *hdmi)
  446. {
  447. mutex_lock(&hdmi->audio_mutex);
  448. hdmi_set_clk_regenerator(hdmi, hdmi->hdmi_data.video_mode.mpixelclock,
  449. hdmi->sample_rate);
  450. mutex_unlock(&hdmi->audio_mutex);
  451. }
  452. void dw_hdmi_set_sample_rate(struct dw_hdmi *hdmi, unsigned int rate)
  453. {
  454. mutex_lock(&hdmi->audio_mutex);
  455. hdmi->sample_rate = rate;
  456. hdmi_set_clk_regenerator(hdmi, hdmi->hdmi_data.video_mode.mpixelclock,
  457. hdmi->sample_rate);
  458. mutex_unlock(&hdmi->audio_mutex);
  459. }
  460. EXPORT_SYMBOL_GPL(dw_hdmi_set_sample_rate);
  461. static void hdmi_enable_audio_clk(struct dw_hdmi *hdmi, bool enable)
  462. {
  463. if (enable)
  464. hdmi->mc_clkdis &= ~HDMI_MC_CLKDIS_AUDCLK_DISABLE;
  465. else
  466. hdmi->mc_clkdis |= HDMI_MC_CLKDIS_AUDCLK_DISABLE;
  467. hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);
  468. }
  469. static void dw_hdmi_ahb_audio_enable(struct dw_hdmi *hdmi)
  470. {
  471. hdmi_set_cts_n(hdmi, hdmi->audio_cts, hdmi->audio_n);
  472. }
  473. static void dw_hdmi_ahb_audio_disable(struct dw_hdmi *hdmi)
  474. {
  475. hdmi_set_cts_n(hdmi, hdmi->audio_cts, 0);
  476. }
  477. static void dw_hdmi_i2s_audio_enable(struct dw_hdmi *hdmi)
  478. {
  479. hdmi_set_cts_n(hdmi, hdmi->audio_cts, hdmi->audio_n);
  480. hdmi_enable_audio_clk(hdmi, true);
  481. }
  482. static void dw_hdmi_i2s_audio_disable(struct dw_hdmi *hdmi)
  483. {
  484. hdmi_enable_audio_clk(hdmi, false);
  485. }
  486. void dw_hdmi_audio_enable(struct dw_hdmi *hdmi)
  487. {
  488. unsigned long flags;
  489. spin_lock_irqsave(&hdmi->audio_lock, flags);
  490. hdmi->audio_enable = true;
  491. if (hdmi->enable_audio)
  492. hdmi->enable_audio(hdmi);
  493. spin_unlock_irqrestore(&hdmi->audio_lock, flags);
  494. }
  495. EXPORT_SYMBOL_GPL(dw_hdmi_audio_enable);
  496. void dw_hdmi_audio_disable(struct dw_hdmi *hdmi)
  497. {
  498. unsigned long flags;
  499. spin_lock_irqsave(&hdmi->audio_lock, flags);
  500. hdmi->audio_enable = false;
  501. if (hdmi->disable_audio)
  502. hdmi->disable_audio(hdmi);
  503. spin_unlock_irqrestore(&hdmi->audio_lock, flags);
  504. }
  505. EXPORT_SYMBOL_GPL(dw_hdmi_audio_disable);
  506. static bool hdmi_bus_fmt_is_rgb(unsigned int bus_format)
  507. {
  508. switch (bus_format) {
  509. case MEDIA_BUS_FMT_RGB888_1X24:
  510. case MEDIA_BUS_FMT_RGB101010_1X30:
  511. case MEDIA_BUS_FMT_RGB121212_1X36:
  512. case MEDIA_BUS_FMT_RGB161616_1X48:
  513. return true;
  514. default:
  515. return false;
  516. }
  517. }
  518. static bool hdmi_bus_fmt_is_yuv444(unsigned int bus_format)
  519. {
  520. switch (bus_format) {
  521. case MEDIA_BUS_FMT_YUV8_1X24:
  522. case MEDIA_BUS_FMT_YUV10_1X30:
  523. case MEDIA_BUS_FMT_YUV12_1X36:
  524. case MEDIA_BUS_FMT_YUV16_1X48:
  525. return true;
  526. default:
  527. return false;
  528. }
  529. }
  530. static bool hdmi_bus_fmt_is_yuv422(unsigned int bus_format)
  531. {
  532. switch (bus_format) {
  533. case MEDIA_BUS_FMT_UYVY8_1X16:
  534. case MEDIA_BUS_FMT_UYVY10_1X20:
  535. case MEDIA_BUS_FMT_UYVY12_1X24:
  536. return true;
  537. default:
  538. return false;
  539. }
  540. }
  541. static int hdmi_bus_fmt_color_depth(unsigned int bus_format)
  542. {
  543. switch (bus_format) {
  544. case MEDIA_BUS_FMT_RGB888_1X24:
  545. case MEDIA_BUS_FMT_YUV8_1X24:
  546. case MEDIA_BUS_FMT_UYVY8_1X16:
  547. case MEDIA_BUS_FMT_UYYVYY8_0_5X24:
  548. return 8;
  549. case MEDIA_BUS_FMT_RGB101010_1X30:
  550. case MEDIA_BUS_FMT_YUV10_1X30:
  551. case MEDIA_BUS_FMT_UYVY10_1X20:
  552. case MEDIA_BUS_FMT_UYYVYY10_0_5X30:
  553. return 10;
  554. case MEDIA_BUS_FMT_RGB121212_1X36:
  555. case MEDIA_BUS_FMT_YUV12_1X36:
  556. case MEDIA_BUS_FMT_UYVY12_1X24:
  557. case MEDIA_BUS_FMT_UYYVYY12_0_5X36:
  558. return 12;
  559. case MEDIA_BUS_FMT_RGB161616_1X48:
  560. case MEDIA_BUS_FMT_YUV16_1X48:
  561. case MEDIA_BUS_FMT_UYYVYY16_0_5X48:
  562. return 16;
  563. default:
  564. return 0;
  565. }
  566. }
  567. /*
  568. * this submodule is responsible for the video data synchronization.
  569. * for example, for RGB 4:4:4 input, the data map is defined as
  570. * pin{47~40} <==> R[7:0]
  571. * pin{31~24} <==> G[7:0]
  572. * pin{15~8} <==> B[7:0]
  573. */
  574. static void hdmi_video_sample(struct dw_hdmi *hdmi)
  575. {
  576. int color_format = 0;
  577. u8 val;
  578. switch (hdmi->hdmi_data.enc_in_bus_format) {
  579. case MEDIA_BUS_FMT_RGB888_1X24:
  580. color_format = 0x01;
  581. break;
  582. case MEDIA_BUS_FMT_RGB101010_1X30:
  583. color_format = 0x03;
  584. break;
  585. case MEDIA_BUS_FMT_RGB121212_1X36:
  586. color_format = 0x05;
  587. break;
  588. case MEDIA_BUS_FMT_RGB161616_1X48:
  589. color_format = 0x07;
  590. break;
  591. case MEDIA_BUS_FMT_YUV8_1X24:
  592. case MEDIA_BUS_FMT_UYYVYY8_0_5X24:
  593. color_format = 0x09;
  594. break;
  595. case MEDIA_BUS_FMT_YUV10_1X30:
  596. case MEDIA_BUS_FMT_UYYVYY10_0_5X30:
  597. color_format = 0x0B;
  598. break;
  599. case MEDIA_BUS_FMT_YUV12_1X36:
  600. case MEDIA_BUS_FMT_UYYVYY12_0_5X36:
  601. color_format = 0x0D;
  602. break;
  603. case MEDIA_BUS_FMT_YUV16_1X48:
  604. case MEDIA_BUS_FMT_UYYVYY16_0_5X48:
  605. color_format = 0x0F;
  606. break;
  607. case MEDIA_BUS_FMT_UYVY8_1X16:
  608. color_format = 0x16;
  609. break;
  610. case MEDIA_BUS_FMT_UYVY10_1X20:
  611. color_format = 0x14;
  612. break;
  613. case MEDIA_BUS_FMT_UYVY12_1X24:
  614. color_format = 0x12;
  615. break;
  616. default:
  617. return;
  618. }
  619. val = HDMI_TX_INVID0_INTERNAL_DE_GENERATOR_DISABLE |
  620. ((color_format << HDMI_TX_INVID0_VIDEO_MAPPING_OFFSET) &
  621. HDMI_TX_INVID0_VIDEO_MAPPING_MASK);
  622. hdmi_writeb(hdmi, val, HDMI_TX_INVID0);
  623. /* Enable TX stuffing: When DE is inactive, fix the output data to 0 */
  624. val = HDMI_TX_INSTUFFING_BDBDATA_STUFFING_ENABLE |
  625. HDMI_TX_INSTUFFING_RCRDATA_STUFFING_ENABLE |
  626. HDMI_TX_INSTUFFING_GYDATA_STUFFING_ENABLE;
  627. hdmi_writeb(hdmi, val, HDMI_TX_INSTUFFING);
  628. hdmi_writeb(hdmi, 0x0, HDMI_TX_GYDATA0);
  629. hdmi_writeb(hdmi, 0x0, HDMI_TX_GYDATA1);
  630. hdmi_writeb(hdmi, 0x0, HDMI_TX_RCRDATA0);
  631. hdmi_writeb(hdmi, 0x0, HDMI_TX_RCRDATA1);
  632. hdmi_writeb(hdmi, 0x0, HDMI_TX_BCBDATA0);
  633. hdmi_writeb(hdmi, 0x0, HDMI_TX_BCBDATA1);
  634. }
  635. static int is_color_space_conversion(struct dw_hdmi *hdmi)
  636. {
  637. return hdmi->hdmi_data.enc_in_bus_format != hdmi->hdmi_data.enc_out_bus_format;
  638. }
  639. static int is_color_space_decimation(struct dw_hdmi *hdmi)
  640. {
  641. if (!hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format))
  642. return 0;
  643. if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_in_bus_format) ||
  644. hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_in_bus_format))
  645. return 1;
  646. return 0;
  647. }
  648. static int is_color_space_interpolation(struct dw_hdmi *hdmi)
  649. {
  650. if (!hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_in_bus_format))
  651. return 0;
  652. if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format) ||
  653. hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_out_bus_format))
  654. return 1;
  655. return 0;
  656. }
  657. static void dw_hdmi_update_csc_coeffs(struct dw_hdmi *hdmi)
  658. {
  659. const u16 (*csc_coeff)[3][4] = &csc_coeff_default;
  660. unsigned i;
  661. u32 csc_scale = 1;
  662. if (is_color_space_conversion(hdmi)) {
  663. if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format)) {
  664. if (hdmi->hdmi_data.enc_out_encoding ==
  665. V4L2_YCBCR_ENC_601)
  666. csc_coeff = &csc_coeff_rgb_out_eitu601;
  667. else
  668. csc_coeff = &csc_coeff_rgb_out_eitu709;
  669. } else if (hdmi_bus_fmt_is_rgb(
  670. hdmi->hdmi_data.enc_in_bus_format)) {
  671. if (hdmi->hdmi_data.enc_out_encoding ==
  672. V4L2_YCBCR_ENC_601)
  673. csc_coeff = &csc_coeff_rgb_in_eitu601;
  674. else
  675. csc_coeff = &csc_coeff_rgb_in_eitu709;
  676. csc_scale = 0;
  677. }
  678. }
  679. /* The CSC registers are sequential, alternating MSB then LSB */
  680. for (i = 0; i < ARRAY_SIZE(csc_coeff_default[0]); i++) {
  681. u16 coeff_a = (*csc_coeff)[0][i];
  682. u16 coeff_b = (*csc_coeff)[1][i];
  683. u16 coeff_c = (*csc_coeff)[2][i];
  684. hdmi_writeb(hdmi, coeff_a & 0xff, HDMI_CSC_COEF_A1_LSB + i * 2);
  685. hdmi_writeb(hdmi, coeff_a >> 8, HDMI_CSC_COEF_A1_MSB + i * 2);
  686. hdmi_writeb(hdmi, coeff_b & 0xff, HDMI_CSC_COEF_B1_LSB + i * 2);
  687. hdmi_writeb(hdmi, coeff_b >> 8, HDMI_CSC_COEF_B1_MSB + i * 2);
  688. hdmi_writeb(hdmi, coeff_c & 0xff, HDMI_CSC_COEF_C1_LSB + i * 2);
  689. hdmi_writeb(hdmi, coeff_c >> 8, HDMI_CSC_COEF_C1_MSB + i * 2);
  690. }
  691. hdmi_modb(hdmi, csc_scale, HDMI_CSC_SCALE_CSCSCALE_MASK,
  692. HDMI_CSC_SCALE);
  693. }
  694. static void hdmi_video_csc(struct dw_hdmi *hdmi)
  695. {
  696. int color_depth = 0;
  697. int interpolation = HDMI_CSC_CFG_INTMODE_DISABLE;
  698. int decimation = 0;
  699. /* YCC422 interpolation to 444 mode */
  700. if (is_color_space_interpolation(hdmi))
  701. interpolation = HDMI_CSC_CFG_INTMODE_CHROMA_INT_FORMULA1;
  702. else if (is_color_space_decimation(hdmi))
  703. decimation = HDMI_CSC_CFG_DECMODE_CHROMA_INT_FORMULA3;
  704. switch (hdmi_bus_fmt_color_depth(hdmi->hdmi_data.enc_out_bus_format)) {
  705. case 8:
  706. color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_24BPP;
  707. break;
  708. case 10:
  709. color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_30BPP;
  710. break;
  711. case 12:
  712. color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_36BPP;
  713. break;
  714. case 16:
  715. color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_48BPP;
  716. break;
  717. default:
  718. return;
  719. }
  720. /* Configure the CSC registers */
  721. hdmi_writeb(hdmi, interpolation | decimation, HDMI_CSC_CFG);
  722. hdmi_modb(hdmi, color_depth, HDMI_CSC_SCALE_CSC_COLORDE_PTH_MASK,
  723. HDMI_CSC_SCALE);
  724. dw_hdmi_update_csc_coeffs(hdmi);
  725. }
  726. /*
  727. * HDMI video packetizer is used to packetize the data.
  728. * for example, if input is YCC422 mode or repeater is used,
  729. * data should be repacked this module can be bypassed.
  730. */
  731. static void hdmi_video_packetize(struct dw_hdmi *hdmi)
  732. {
  733. unsigned int color_depth = 0;
  734. unsigned int remap_size = HDMI_VP_REMAP_YCC422_16bit;
  735. unsigned int output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_PP;
  736. struct hdmi_data_info *hdmi_data = &hdmi->hdmi_data;
  737. u8 val, vp_conf;
  738. if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format) ||
  739. hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_out_bus_format)) {
  740. switch (hdmi_bus_fmt_color_depth(
  741. hdmi->hdmi_data.enc_out_bus_format)) {
  742. case 8:
  743. color_depth = 4;
  744. output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS;
  745. break;
  746. case 10:
  747. color_depth = 5;
  748. break;
  749. case 12:
  750. color_depth = 6;
  751. break;
  752. case 16:
  753. color_depth = 7;
  754. break;
  755. default:
  756. output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS;
  757. }
  758. } else if (hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format)) {
  759. switch (hdmi_bus_fmt_color_depth(
  760. hdmi->hdmi_data.enc_out_bus_format)) {
  761. case 0:
  762. case 8:
  763. remap_size = HDMI_VP_REMAP_YCC422_16bit;
  764. break;
  765. case 10:
  766. remap_size = HDMI_VP_REMAP_YCC422_20bit;
  767. break;
  768. case 12:
  769. remap_size = HDMI_VP_REMAP_YCC422_24bit;
  770. break;
  771. default:
  772. return;
  773. }
  774. output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_YCC422;
  775. } else {
  776. return;
  777. }
  778. /* set the packetizer registers */
  779. val = ((color_depth << HDMI_VP_PR_CD_COLOR_DEPTH_OFFSET) &
  780. HDMI_VP_PR_CD_COLOR_DEPTH_MASK) |
  781. ((hdmi_data->pix_repet_factor <<
  782. HDMI_VP_PR_CD_DESIRED_PR_FACTOR_OFFSET) &
  783. HDMI_VP_PR_CD_DESIRED_PR_FACTOR_MASK);
  784. hdmi_writeb(hdmi, val, HDMI_VP_PR_CD);
  785. hdmi_modb(hdmi, HDMI_VP_STUFF_PR_STUFFING_STUFFING_MODE,
  786. HDMI_VP_STUFF_PR_STUFFING_MASK, HDMI_VP_STUFF);
  787. /* Data from pixel repeater block */
  788. if (hdmi_data->pix_repet_factor > 1) {
  789. vp_conf = HDMI_VP_CONF_PR_EN_ENABLE |
  790. HDMI_VP_CONF_BYPASS_SELECT_PIX_REPEATER;
  791. } else { /* data from packetizer block */
  792. vp_conf = HDMI_VP_CONF_PR_EN_DISABLE |
  793. HDMI_VP_CONF_BYPASS_SELECT_VID_PACKETIZER;
  794. }
  795. hdmi_modb(hdmi, vp_conf,
  796. HDMI_VP_CONF_PR_EN_MASK |
  797. HDMI_VP_CONF_BYPASS_SELECT_MASK, HDMI_VP_CONF);
  798. hdmi_modb(hdmi, 1 << HDMI_VP_STUFF_IDEFAULT_PHASE_OFFSET,
  799. HDMI_VP_STUFF_IDEFAULT_PHASE_MASK, HDMI_VP_STUFF);
  800. hdmi_writeb(hdmi, remap_size, HDMI_VP_REMAP);
  801. if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_PP) {
  802. vp_conf = HDMI_VP_CONF_BYPASS_EN_DISABLE |
  803. HDMI_VP_CONF_PP_EN_ENABLE |
  804. HDMI_VP_CONF_YCC422_EN_DISABLE;
  805. } else if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_YCC422) {
  806. vp_conf = HDMI_VP_CONF_BYPASS_EN_DISABLE |
  807. HDMI_VP_CONF_PP_EN_DISABLE |
  808. HDMI_VP_CONF_YCC422_EN_ENABLE;
  809. } else if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS) {
  810. vp_conf = HDMI_VP_CONF_BYPASS_EN_ENABLE |
  811. HDMI_VP_CONF_PP_EN_DISABLE |
  812. HDMI_VP_CONF_YCC422_EN_DISABLE;
  813. } else {
  814. return;
  815. }
  816. hdmi_modb(hdmi, vp_conf,
  817. HDMI_VP_CONF_BYPASS_EN_MASK | HDMI_VP_CONF_PP_EN_ENMASK |
  818. HDMI_VP_CONF_YCC422_EN_MASK, HDMI_VP_CONF);
  819. hdmi_modb(hdmi, HDMI_VP_STUFF_PP_STUFFING_STUFFING_MODE |
  820. HDMI_VP_STUFF_YCC422_STUFFING_STUFFING_MODE,
  821. HDMI_VP_STUFF_PP_STUFFING_MASK |
  822. HDMI_VP_STUFF_YCC422_STUFFING_MASK, HDMI_VP_STUFF);
  823. hdmi_modb(hdmi, output_select, HDMI_VP_CONF_OUTPUT_SELECTOR_MASK,
  824. HDMI_VP_CONF);
  825. }
  826. /* -----------------------------------------------------------------------------
  827. * Synopsys PHY Handling
  828. */
  829. static inline void hdmi_phy_test_clear(struct dw_hdmi *hdmi,
  830. unsigned char bit)
  831. {
  832. hdmi_modb(hdmi, bit << HDMI_PHY_TST0_TSTCLR_OFFSET,
  833. HDMI_PHY_TST0_TSTCLR_MASK, HDMI_PHY_TST0);
  834. }
  835. static bool hdmi_phy_wait_i2c_done(struct dw_hdmi *hdmi, int msec)
  836. {
  837. u32 val;
  838. while ((val = hdmi_readb(hdmi, HDMI_IH_I2CMPHY_STAT0) & 0x3) == 0) {
  839. if (msec-- == 0)
  840. return false;
  841. udelay(1000);
  842. }
  843. hdmi_writeb(hdmi, val, HDMI_IH_I2CMPHY_STAT0);
  844. return true;
  845. }
  846. void dw_hdmi_phy_i2c_write(struct dw_hdmi *hdmi, unsigned short data,
  847. unsigned char addr)
  848. {
  849. hdmi_writeb(hdmi, 0xFF, HDMI_IH_I2CMPHY_STAT0);
  850. hdmi_writeb(hdmi, addr, HDMI_PHY_I2CM_ADDRESS_ADDR);
  851. hdmi_writeb(hdmi, (unsigned char)(data >> 8),
  852. HDMI_PHY_I2CM_DATAO_1_ADDR);
  853. hdmi_writeb(hdmi, (unsigned char)(data >> 0),
  854. HDMI_PHY_I2CM_DATAO_0_ADDR);
  855. hdmi_writeb(hdmi, HDMI_PHY_I2CM_OPERATION_ADDR_WRITE,
  856. HDMI_PHY_I2CM_OPERATION_ADDR);
  857. hdmi_phy_wait_i2c_done(hdmi, 1000);
  858. }
  859. EXPORT_SYMBOL_GPL(dw_hdmi_phy_i2c_write);
  860. static void dw_hdmi_phy_enable_powerdown(struct dw_hdmi *hdmi, bool enable)
  861. {
  862. hdmi_mask_writeb(hdmi, !enable, HDMI_PHY_CONF0,
  863. HDMI_PHY_CONF0_PDZ_OFFSET,
  864. HDMI_PHY_CONF0_PDZ_MASK);
  865. }
  866. static void dw_hdmi_phy_enable_tmds(struct dw_hdmi *hdmi, u8 enable)
  867. {
  868. hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
  869. HDMI_PHY_CONF0_ENTMDS_OFFSET,
  870. HDMI_PHY_CONF0_ENTMDS_MASK);
  871. }
  872. static void dw_hdmi_phy_enable_svsret(struct dw_hdmi *hdmi, u8 enable)
  873. {
  874. hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
  875. HDMI_PHY_CONF0_SVSRET_OFFSET,
  876. HDMI_PHY_CONF0_SVSRET_MASK);
  877. }
  878. void dw_hdmi_phy_gen2_pddq(struct dw_hdmi *hdmi, u8 enable)
  879. {
  880. hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
  881. HDMI_PHY_CONF0_GEN2_PDDQ_OFFSET,
  882. HDMI_PHY_CONF0_GEN2_PDDQ_MASK);
  883. }
  884. EXPORT_SYMBOL_GPL(dw_hdmi_phy_gen2_pddq);
  885. void dw_hdmi_phy_gen2_txpwron(struct dw_hdmi *hdmi, u8 enable)
  886. {
  887. hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
  888. HDMI_PHY_CONF0_GEN2_TXPWRON_OFFSET,
  889. HDMI_PHY_CONF0_GEN2_TXPWRON_MASK);
  890. }
  891. EXPORT_SYMBOL_GPL(dw_hdmi_phy_gen2_txpwron);
  892. static void dw_hdmi_phy_sel_data_en_pol(struct dw_hdmi *hdmi, u8 enable)
  893. {
  894. hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
  895. HDMI_PHY_CONF0_SELDATAENPOL_OFFSET,
  896. HDMI_PHY_CONF0_SELDATAENPOL_MASK);
  897. }
  898. static void dw_hdmi_phy_sel_interface_control(struct dw_hdmi *hdmi, u8 enable)
  899. {
  900. hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
  901. HDMI_PHY_CONF0_SELDIPIF_OFFSET,
  902. HDMI_PHY_CONF0_SELDIPIF_MASK);
  903. }
  904. void dw_hdmi_phy_reset(struct dw_hdmi *hdmi)
  905. {
  906. /* PHY reset. The reset signal is active high on Gen2 PHYs. */
  907. hdmi_writeb(hdmi, HDMI_MC_PHYRSTZ_PHYRSTZ, HDMI_MC_PHYRSTZ);
  908. hdmi_writeb(hdmi, 0, HDMI_MC_PHYRSTZ);
  909. }
  910. EXPORT_SYMBOL_GPL(dw_hdmi_phy_reset);
  911. void dw_hdmi_phy_i2c_set_addr(struct dw_hdmi *hdmi, u8 address)
  912. {
  913. hdmi_phy_test_clear(hdmi, 1);
  914. hdmi_writeb(hdmi, address, HDMI_PHY_I2CM_SLAVE_ADDR);
  915. hdmi_phy_test_clear(hdmi, 0);
  916. }
  917. EXPORT_SYMBOL_GPL(dw_hdmi_phy_i2c_set_addr);
  918. static void dw_hdmi_phy_power_off(struct dw_hdmi *hdmi)
  919. {
  920. const struct dw_hdmi_phy_data *phy = hdmi->phy.data;
  921. unsigned int i;
  922. u16 val;
  923. if (phy->gen == 1) {
  924. dw_hdmi_phy_enable_tmds(hdmi, 0);
  925. dw_hdmi_phy_enable_powerdown(hdmi, true);
  926. return;
  927. }
  928. dw_hdmi_phy_gen2_txpwron(hdmi, 0);
  929. /*
  930. * Wait for TX_PHY_LOCK to be deasserted to indicate that the PHY went
  931. * to low power mode.
  932. */
  933. for (i = 0; i < 5; ++i) {
  934. val = hdmi_readb(hdmi, HDMI_PHY_STAT0);
  935. if (!(val & HDMI_PHY_TX_PHY_LOCK))
  936. break;
  937. usleep_range(1000, 2000);
  938. }
  939. if (val & HDMI_PHY_TX_PHY_LOCK)
  940. dev_warn(hdmi->dev, "PHY failed to power down\n");
  941. else
  942. dev_dbg(hdmi->dev, "PHY powered down in %u iterations\n", i);
  943. dw_hdmi_phy_gen2_pddq(hdmi, 1);
  944. }
  945. static int dw_hdmi_phy_power_on(struct dw_hdmi *hdmi)
  946. {
  947. const struct dw_hdmi_phy_data *phy = hdmi->phy.data;
  948. unsigned int i;
  949. u8 val;
  950. if (phy->gen == 1) {
  951. dw_hdmi_phy_enable_powerdown(hdmi, false);
  952. /* Toggle TMDS enable. */
  953. dw_hdmi_phy_enable_tmds(hdmi, 0);
  954. dw_hdmi_phy_enable_tmds(hdmi, 1);
  955. return 0;
  956. }
  957. dw_hdmi_phy_gen2_txpwron(hdmi, 1);
  958. dw_hdmi_phy_gen2_pddq(hdmi, 0);
  959. /* Wait for PHY PLL lock */
  960. for (i = 0; i < 5; ++i) {
  961. val = hdmi_readb(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_TX_PHY_LOCK;
  962. if (val)
  963. break;
  964. usleep_range(1000, 2000);
  965. }
  966. if (!val) {
  967. dev_err(hdmi->dev, "PHY PLL failed to lock\n");
  968. return -ETIMEDOUT;
  969. }
  970. dev_dbg(hdmi->dev, "PHY PLL locked %u iterations\n", i);
  971. return 0;
  972. }
  973. /*
  974. * PHY configuration function for the DWC HDMI 3D TX PHY. Based on the available
  975. * information the DWC MHL PHY has the same register layout and is thus also
  976. * supported by this function.
  977. */
  978. static int hdmi_phy_configure_dwc_hdmi_3d_tx(struct dw_hdmi *hdmi,
  979. const struct dw_hdmi_plat_data *pdata,
  980. unsigned long mpixelclock)
  981. {
  982. const struct dw_hdmi_mpll_config *mpll_config = pdata->mpll_cfg;
  983. const struct dw_hdmi_curr_ctrl *curr_ctrl = pdata->cur_ctr;
  984. const struct dw_hdmi_phy_config *phy_config = pdata->phy_config;
  985. /* PLL/MPLL Cfg - always match on final entry */
  986. for (; mpll_config->mpixelclock != ~0UL; mpll_config++)
  987. if (mpixelclock <= mpll_config->mpixelclock)
  988. break;
  989. for (; curr_ctrl->mpixelclock != ~0UL; curr_ctrl++)
  990. if (mpixelclock <= curr_ctrl->mpixelclock)
  991. break;
  992. for (; phy_config->mpixelclock != ~0UL; phy_config++)
  993. if (mpixelclock <= phy_config->mpixelclock)
  994. break;
  995. if (mpll_config->mpixelclock == ~0UL ||
  996. curr_ctrl->mpixelclock == ~0UL ||
  997. phy_config->mpixelclock == ~0UL)
  998. return -EINVAL;
  999. dw_hdmi_phy_i2c_write(hdmi, mpll_config->res[0].cpce,
  1000. HDMI_3D_TX_PHY_CPCE_CTRL);
  1001. dw_hdmi_phy_i2c_write(hdmi, mpll_config->res[0].gmp,
  1002. HDMI_3D_TX_PHY_GMPCTRL);
  1003. dw_hdmi_phy_i2c_write(hdmi, curr_ctrl->curr[0],
  1004. HDMI_3D_TX_PHY_CURRCTRL);
  1005. dw_hdmi_phy_i2c_write(hdmi, 0, HDMI_3D_TX_PHY_PLLPHBYCTRL);
  1006. dw_hdmi_phy_i2c_write(hdmi, HDMI_3D_TX_PHY_MSM_CTRL_CKO_SEL_FB_CLK,
  1007. HDMI_3D_TX_PHY_MSM_CTRL);
  1008. dw_hdmi_phy_i2c_write(hdmi, phy_config->term, HDMI_3D_TX_PHY_TXTERM);
  1009. dw_hdmi_phy_i2c_write(hdmi, phy_config->sym_ctr,
  1010. HDMI_3D_TX_PHY_CKSYMTXCTRL);
  1011. dw_hdmi_phy_i2c_write(hdmi, phy_config->vlev_ctr,
  1012. HDMI_3D_TX_PHY_VLEVCTRL);
  1013. /* Override and disable clock termination. */
  1014. dw_hdmi_phy_i2c_write(hdmi, HDMI_3D_TX_PHY_CKCALCTRL_OVERRIDE,
  1015. HDMI_3D_TX_PHY_CKCALCTRL);
  1016. return 0;
  1017. }
  1018. static int hdmi_phy_configure(struct dw_hdmi *hdmi)
  1019. {
  1020. const struct dw_hdmi_phy_data *phy = hdmi->phy.data;
  1021. const struct dw_hdmi_plat_data *pdata = hdmi->plat_data;
  1022. unsigned long mpixelclock = hdmi->hdmi_data.video_mode.mpixelclock;
  1023. int ret;
  1024. dw_hdmi_phy_power_off(hdmi);
  1025. /* Leave low power consumption mode by asserting SVSRET. */
  1026. if (phy->has_svsret)
  1027. dw_hdmi_phy_enable_svsret(hdmi, 1);
  1028. dw_hdmi_phy_reset(hdmi);
  1029. hdmi_writeb(hdmi, HDMI_MC_HEACPHY_RST_ASSERT, HDMI_MC_HEACPHY_RST);
  1030. dw_hdmi_phy_i2c_set_addr(hdmi, HDMI_PHY_I2CM_SLAVE_ADDR_PHY_GEN2);
  1031. /* Write to the PHY as configured by the platform */
  1032. if (pdata->configure_phy)
  1033. ret = pdata->configure_phy(hdmi, pdata, mpixelclock);
  1034. else
  1035. ret = phy->configure(hdmi, pdata, mpixelclock);
  1036. if (ret) {
  1037. dev_err(hdmi->dev, "PHY configuration failed (clock %lu)\n",
  1038. mpixelclock);
  1039. return ret;
  1040. }
  1041. return dw_hdmi_phy_power_on(hdmi);
  1042. }
  1043. static int dw_hdmi_phy_init(struct dw_hdmi *hdmi, void *data,
  1044. struct drm_display_mode *mode)
  1045. {
  1046. int i, ret;
  1047. /* HDMI Phy spec says to do the phy initialization sequence twice */
  1048. for (i = 0; i < 2; i++) {
  1049. dw_hdmi_phy_sel_data_en_pol(hdmi, 1);
  1050. dw_hdmi_phy_sel_interface_control(hdmi, 0);
  1051. ret = hdmi_phy_configure(hdmi);
  1052. if (ret)
  1053. return ret;
  1054. }
  1055. return 0;
  1056. }
  1057. static void dw_hdmi_phy_disable(struct dw_hdmi *hdmi, void *data)
  1058. {
  1059. dw_hdmi_phy_power_off(hdmi);
  1060. }
  1061. enum drm_connector_status dw_hdmi_phy_read_hpd(struct dw_hdmi *hdmi,
  1062. void *data)
  1063. {
  1064. return hdmi_readb(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_HPD ?
  1065. connector_status_connected : connector_status_disconnected;
  1066. }
  1067. EXPORT_SYMBOL_GPL(dw_hdmi_phy_read_hpd);
  1068. void dw_hdmi_phy_update_hpd(struct dw_hdmi *hdmi, void *data,
  1069. bool force, bool disabled, bool rxsense)
  1070. {
  1071. u8 old_mask = hdmi->phy_mask;
  1072. if (force || disabled || !rxsense)
  1073. hdmi->phy_mask |= HDMI_PHY_RX_SENSE;
  1074. else
  1075. hdmi->phy_mask &= ~HDMI_PHY_RX_SENSE;
  1076. if (old_mask != hdmi->phy_mask)
  1077. hdmi_writeb(hdmi, hdmi->phy_mask, HDMI_PHY_MASK0);
  1078. }
  1079. EXPORT_SYMBOL_GPL(dw_hdmi_phy_update_hpd);
  1080. void dw_hdmi_phy_setup_hpd(struct dw_hdmi *hdmi, void *data)
  1081. {
  1082. /*
  1083. * Configure the PHY RX SENSE and HPD interrupts polarities and clear
  1084. * any pending interrupt.
  1085. */
  1086. hdmi_writeb(hdmi, HDMI_PHY_HPD | HDMI_PHY_RX_SENSE, HDMI_PHY_POL0);
  1087. hdmi_writeb(hdmi, HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE,
  1088. HDMI_IH_PHY_STAT0);
  1089. /* Enable cable hot plug irq. */
  1090. hdmi_writeb(hdmi, hdmi->phy_mask, HDMI_PHY_MASK0);
  1091. /* Clear and unmute interrupts. */
  1092. hdmi_writeb(hdmi, HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE,
  1093. HDMI_IH_PHY_STAT0);
  1094. hdmi_writeb(hdmi, ~(HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE),
  1095. HDMI_IH_MUTE_PHY_STAT0);
  1096. }
  1097. EXPORT_SYMBOL_GPL(dw_hdmi_phy_setup_hpd);
  1098. static const struct dw_hdmi_phy_ops dw_hdmi_synopsys_phy_ops = {
  1099. .init = dw_hdmi_phy_init,
  1100. .disable = dw_hdmi_phy_disable,
  1101. .read_hpd = dw_hdmi_phy_read_hpd,
  1102. .update_hpd = dw_hdmi_phy_update_hpd,
  1103. .setup_hpd = dw_hdmi_phy_setup_hpd,
  1104. };
  1105. /* -----------------------------------------------------------------------------
  1106. * HDMI TX Setup
  1107. */
  1108. static void hdmi_tx_hdcp_config(struct dw_hdmi *hdmi)
  1109. {
  1110. u8 de;
  1111. if (hdmi->hdmi_data.video_mode.mdataenablepolarity)
  1112. de = HDMI_A_VIDPOLCFG_DATAENPOL_ACTIVE_HIGH;
  1113. else
  1114. de = HDMI_A_VIDPOLCFG_DATAENPOL_ACTIVE_LOW;
  1115. /* disable rx detect */
  1116. hdmi_modb(hdmi, HDMI_A_HDCPCFG0_RXDETECT_DISABLE,
  1117. HDMI_A_HDCPCFG0_RXDETECT_MASK, HDMI_A_HDCPCFG0);
  1118. hdmi_modb(hdmi, de, HDMI_A_VIDPOLCFG_DATAENPOL_MASK, HDMI_A_VIDPOLCFG);
  1119. hdmi_modb(hdmi, HDMI_A_HDCPCFG1_ENCRYPTIONDISABLE_DISABLE,
  1120. HDMI_A_HDCPCFG1_ENCRYPTIONDISABLE_MASK, HDMI_A_HDCPCFG1);
  1121. }
  1122. static void hdmi_config_AVI(struct dw_hdmi *hdmi, struct drm_display_mode *mode)
  1123. {
  1124. struct hdmi_avi_infoframe frame;
  1125. u8 val;
  1126. /* Initialise info frame from DRM mode */
  1127. drm_hdmi_avi_infoframe_from_display_mode(&frame, mode, false);
  1128. if (hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_out_bus_format))
  1129. frame.colorspace = HDMI_COLORSPACE_YUV444;
  1130. else if (hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format))
  1131. frame.colorspace = HDMI_COLORSPACE_YUV422;
  1132. else
  1133. frame.colorspace = HDMI_COLORSPACE_RGB;
  1134. /* Set up colorimetry */
  1135. if (!hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format)) {
  1136. switch (hdmi->hdmi_data.enc_out_encoding) {
  1137. case V4L2_YCBCR_ENC_601:
  1138. if (hdmi->hdmi_data.enc_in_encoding == V4L2_YCBCR_ENC_XV601)
  1139. frame.colorimetry = HDMI_COLORIMETRY_EXTENDED;
  1140. else
  1141. frame.colorimetry = HDMI_COLORIMETRY_ITU_601;
  1142. frame.extended_colorimetry =
  1143. HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
  1144. break;
  1145. case V4L2_YCBCR_ENC_709:
  1146. if (hdmi->hdmi_data.enc_in_encoding == V4L2_YCBCR_ENC_XV709)
  1147. frame.colorimetry = HDMI_COLORIMETRY_EXTENDED;
  1148. else
  1149. frame.colorimetry = HDMI_COLORIMETRY_ITU_709;
  1150. frame.extended_colorimetry =
  1151. HDMI_EXTENDED_COLORIMETRY_XV_YCC_709;
  1152. break;
  1153. default: /* Carries no data */
  1154. frame.colorimetry = HDMI_COLORIMETRY_ITU_601;
  1155. frame.extended_colorimetry =
  1156. HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
  1157. break;
  1158. }
  1159. } else {
  1160. frame.colorimetry = HDMI_COLORIMETRY_NONE;
  1161. frame.extended_colorimetry =
  1162. HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
  1163. }
  1164. frame.scan_mode = HDMI_SCAN_MODE_NONE;
  1165. /*
  1166. * The Designware IP uses a different byte format from standard
  1167. * AVI info frames, though generally the bits are in the correct
  1168. * bytes.
  1169. */
  1170. /*
  1171. * AVI data byte 1 differences: Colorspace in bits 0,1 rather than 5,6,
  1172. * scan info in bits 4,5 rather than 0,1 and active aspect present in
  1173. * bit 6 rather than 4.
  1174. */
  1175. val = (frame.scan_mode & 3) << 4 | (frame.colorspace & 3);
  1176. if (frame.active_aspect & 15)
  1177. val |= HDMI_FC_AVICONF0_ACTIVE_FMT_INFO_PRESENT;
  1178. if (frame.top_bar || frame.bottom_bar)
  1179. val |= HDMI_FC_AVICONF0_BAR_DATA_HORIZ_BAR;
  1180. if (frame.left_bar || frame.right_bar)
  1181. val |= HDMI_FC_AVICONF0_BAR_DATA_VERT_BAR;
  1182. hdmi_writeb(hdmi, val, HDMI_FC_AVICONF0);
  1183. /* AVI data byte 2 differences: none */
  1184. val = ((frame.colorimetry & 0x3) << 6) |
  1185. ((frame.picture_aspect & 0x3) << 4) |
  1186. (frame.active_aspect & 0xf);
  1187. hdmi_writeb(hdmi, val, HDMI_FC_AVICONF1);
  1188. /* AVI data byte 3 differences: none */
  1189. val = ((frame.extended_colorimetry & 0x7) << 4) |
  1190. ((frame.quantization_range & 0x3) << 2) |
  1191. (frame.nups & 0x3);
  1192. if (frame.itc)
  1193. val |= HDMI_FC_AVICONF2_IT_CONTENT_VALID;
  1194. hdmi_writeb(hdmi, val, HDMI_FC_AVICONF2);
  1195. /* AVI data byte 4 differences: none */
  1196. val = frame.video_code & 0x7f;
  1197. hdmi_writeb(hdmi, val, HDMI_FC_AVIVID);
  1198. /* AVI Data Byte 5- set up input and output pixel repetition */
  1199. val = (((hdmi->hdmi_data.video_mode.mpixelrepetitioninput + 1) <<
  1200. HDMI_FC_PRCONF_INCOMING_PR_FACTOR_OFFSET) &
  1201. HDMI_FC_PRCONF_INCOMING_PR_FACTOR_MASK) |
  1202. ((hdmi->hdmi_data.video_mode.mpixelrepetitionoutput <<
  1203. HDMI_FC_PRCONF_OUTPUT_PR_FACTOR_OFFSET) &
  1204. HDMI_FC_PRCONF_OUTPUT_PR_FACTOR_MASK);
  1205. hdmi_writeb(hdmi, val, HDMI_FC_PRCONF);
  1206. /*
  1207. * AVI data byte 5 differences: content type in 0,1 rather than 4,5,
  1208. * ycc range in bits 2,3 rather than 6,7
  1209. */
  1210. val = ((frame.ycc_quantization_range & 0x3) << 2) |
  1211. (frame.content_type & 0x3);
  1212. hdmi_writeb(hdmi, val, HDMI_FC_AVICONF3);
  1213. /* AVI Data Bytes 6-13 */
  1214. hdmi_writeb(hdmi, frame.top_bar & 0xff, HDMI_FC_AVIETB0);
  1215. hdmi_writeb(hdmi, (frame.top_bar >> 8) & 0xff, HDMI_FC_AVIETB1);
  1216. hdmi_writeb(hdmi, frame.bottom_bar & 0xff, HDMI_FC_AVISBB0);
  1217. hdmi_writeb(hdmi, (frame.bottom_bar >> 8) & 0xff, HDMI_FC_AVISBB1);
  1218. hdmi_writeb(hdmi, frame.left_bar & 0xff, HDMI_FC_AVIELB0);
  1219. hdmi_writeb(hdmi, (frame.left_bar >> 8) & 0xff, HDMI_FC_AVIELB1);
  1220. hdmi_writeb(hdmi, frame.right_bar & 0xff, HDMI_FC_AVISRB0);
  1221. hdmi_writeb(hdmi, (frame.right_bar >> 8) & 0xff, HDMI_FC_AVISRB1);
  1222. }
  1223. static void hdmi_config_vendor_specific_infoframe(struct dw_hdmi *hdmi,
  1224. struct drm_display_mode *mode)
  1225. {
  1226. struct hdmi_vendor_infoframe frame;
  1227. u8 buffer[10];
  1228. ssize_t err;
  1229. err = drm_hdmi_vendor_infoframe_from_display_mode(&frame,
  1230. &hdmi->connector,
  1231. mode);
  1232. if (err < 0)
  1233. /*
  1234. * Going into that statement does not means vendor infoframe
  1235. * fails. It just informed us that vendor infoframe is not
  1236. * needed for the selected mode. Only 4k or stereoscopic 3D
  1237. * mode requires vendor infoframe. So just simply return.
  1238. */
  1239. return;
  1240. err = hdmi_vendor_infoframe_pack(&frame, buffer, sizeof(buffer));
  1241. if (err < 0) {
  1242. dev_err(hdmi->dev, "Failed to pack vendor infoframe: %zd\n",
  1243. err);
  1244. return;
  1245. }
  1246. hdmi_mask_writeb(hdmi, 0, HDMI_FC_DATAUTO0, HDMI_FC_DATAUTO0_VSD_OFFSET,
  1247. HDMI_FC_DATAUTO0_VSD_MASK);
  1248. /* Set the length of HDMI vendor specific InfoFrame payload */
  1249. hdmi_writeb(hdmi, buffer[2], HDMI_FC_VSDSIZE);
  1250. /* Set 24bit IEEE Registration Identifier */
  1251. hdmi_writeb(hdmi, buffer[4], HDMI_FC_VSDIEEEID0);
  1252. hdmi_writeb(hdmi, buffer[5], HDMI_FC_VSDIEEEID1);
  1253. hdmi_writeb(hdmi, buffer[6], HDMI_FC_VSDIEEEID2);
  1254. /* Set HDMI_Video_Format and HDMI_VIC/3D_Structure */
  1255. hdmi_writeb(hdmi, buffer[7], HDMI_FC_VSDPAYLOAD0);
  1256. hdmi_writeb(hdmi, buffer[8], HDMI_FC_VSDPAYLOAD1);
  1257. if (frame.s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF)
  1258. hdmi_writeb(hdmi, buffer[9], HDMI_FC_VSDPAYLOAD2);
  1259. /* Packet frame interpolation */
  1260. hdmi_writeb(hdmi, 1, HDMI_FC_DATAUTO1);
  1261. /* Auto packets per frame and line spacing */
  1262. hdmi_writeb(hdmi, 0x11, HDMI_FC_DATAUTO2);
  1263. /* Configures the Frame Composer On RDRB mode */
  1264. hdmi_mask_writeb(hdmi, 1, HDMI_FC_DATAUTO0, HDMI_FC_DATAUTO0_VSD_OFFSET,
  1265. HDMI_FC_DATAUTO0_VSD_MASK);
  1266. }
  1267. static void hdmi_av_composer(struct dw_hdmi *hdmi,
  1268. const struct drm_display_mode *mode)
  1269. {
  1270. u8 inv_val;
  1271. struct hdmi_vmode *vmode = &hdmi->hdmi_data.video_mode;
  1272. int hblank, vblank, h_de_hs, v_de_vs, hsync_len, vsync_len;
  1273. unsigned int vdisplay;
  1274. vmode->mpixelclock = mode->clock * 1000;
  1275. dev_dbg(hdmi->dev, "final pixclk = %d\n", vmode->mpixelclock);
  1276. /* Set up HDMI_FC_INVIDCONF */
  1277. inv_val = (hdmi->hdmi_data.hdcp_enable ?
  1278. HDMI_FC_INVIDCONF_HDCP_KEEPOUT_ACTIVE :
  1279. HDMI_FC_INVIDCONF_HDCP_KEEPOUT_INACTIVE);
  1280. inv_val |= mode->flags & DRM_MODE_FLAG_PVSYNC ?
  1281. HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_HIGH :
  1282. HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_LOW;
  1283. inv_val |= mode->flags & DRM_MODE_FLAG_PHSYNC ?
  1284. HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_HIGH :
  1285. HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_LOW;
  1286. inv_val |= (vmode->mdataenablepolarity ?
  1287. HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_HIGH :
  1288. HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_LOW);
  1289. if (hdmi->vic == 39)
  1290. inv_val |= HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_HIGH;
  1291. else
  1292. inv_val |= mode->flags & DRM_MODE_FLAG_INTERLACE ?
  1293. HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_HIGH :
  1294. HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_LOW;
  1295. inv_val |= mode->flags & DRM_MODE_FLAG_INTERLACE ?
  1296. HDMI_FC_INVIDCONF_IN_I_P_INTERLACED :
  1297. HDMI_FC_INVIDCONF_IN_I_P_PROGRESSIVE;
  1298. inv_val |= hdmi->sink_is_hdmi ?
  1299. HDMI_FC_INVIDCONF_DVI_MODEZ_HDMI_MODE :
  1300. HDMI_FC_INVIDCONF_DVI_MODEZ_DVI_MODE;
  1301. hdmi_writeb(hdmi, inv_val, HDMI_FC_INVIDCONF);
  1302. vdisplay = mode->vdisplay;
  1303. vblank = mode->vtotal - mode->vdisplay;
  1304. v_de_vs = mode->vsync_start - mode->vdisplay;
  1305. vsync_len = mode->vsync_end - mode->vsync_start;
  1306. /*
  1307. * When we're setting an interlaced mode, we need
  1308. * to adjust the vertical timing to suit.
  1309. */
  1310. if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
  1311. vdisplay /= 2;
  1312. vblank /= 2;
  1313. v_de_vs /= 2;
  1314. vsync_len /= 2;
  1315. }
  1316. /* Set up horizontal active pixel width */
  1317. hdmi_writeb(hdmi, mode->hdisplay >> 8, HDMI_FC_INHACTV1);
  1318. hdmi_writeb(hdmi, mode->hdisplay, HDMI_FC_INHACTV0);
  1319. /* Set up vertical active lines */
  1320. hdmi_writeb(hdmi, vdisplay >> 8, HDMI_FC_INVACTV1);
  1321. hdmi_writeb(hdmi, vdisplay, HDMI_FC_INVACTV0);
  1322. /* Set up horizontal blanking pixel region width */
  1323. hblank = mode->htotal - mode->hdisplay;
  1324. hdmi_writeb(hdmi, hblank >> 8, HDMI_FC_INHBLANK1);
  1325. hdmi_writeb(hdmi, hblank, HDMI_FC_INHBLANK0);
  1326. /* Set up vertical blanking pixel region width */
  1327. hdmi_writeb(hdmi, vblank, HDMI_FC_INVBLANK);
  1328. /* Set up HSYNC active edge delay width (in pixel clks) */
  1329. h_de_hs = mode->hsync_start - mode->hdisplay;
  1330. hdmi_writeb(hdmi, h_de_hs >> 8, HDMI_FC_HSYNCINDELAY1);
  1331. hdmi_writeb(hdmi, h_de_hs, HDMI_FC_HSYNCINDELAY0);
  1332. /* Set up VSYNC active edge delay (in lines) */
  1333. hdmi_writeb(hdmi, v_de_vs, HDMI_FC_VSYNCINDELAY);
  1334. /* Set up HSYNC active pulse width (in pixel clks) */
  1335. hsync_len = mode->hsync_end - mode->hsync_start;
  1336. hdmi_writeb(hdmi, hsync_len >> 8, HDMI_FC_HSYNCINWIDTH1);
  1337. hdmi_writeb(hdmi, hsync_len, HDMI_FC_HSYNCINWIDTH0);
  1338. /* Set up VSYNC active edge delay (in lines) */
  1339. hdmi_writeb(hdmi, vsync_len, HDMI_FC_VSYNCINWIDTH);
  1340. }
  1341. /* HDMI Initialization Step B.4 */
  1342. static void dw_hdmi_enable_video_path(struct dw_hdmi *hdmi)
  1343. {
  1344. /* control period minimum duration */
  1345. hdmi_writeb(hdmi, 12, HDMI_FC_CTRLDUR);
  1346. hdmi_writeb(hdmi, 32, HDMI_FC_EXCTRLDUR);
  1347. hdmi_writeb(hdmi, 1, HDMI_FC_EXCTRLSPAC);
  1348. /* Set to fill TMDS data channels */
  1349. hdmi_writeb(hdmi, 0x0B, HDMI_FC_CH0PREAM);
  1350. hdmi_writeb(hdmi, 0x16, HDMI_FC_CH1PREAM);
  1351. hdmi_writeb(hdmi, 0x21, HDMI_FC_CH2PREAM);
  1352. /* Enable pixel clock and tmds data path */
  1353. hdmi->mc_clkdis |= HDMI_MC_CLKDIS_HDCPCLK_DISABLE |
  1354. HDMI_MC_CLKDIS_CSCCLK_DISABLE |
  1355. HDMI_MC_CLKDIS_AUDCLK_DISABLE |
  1356. HDMI_MC_CLKDIS_PREPCLK_DISABLE |
  1357. HDMI_MC_CLKDIS_TMDSCLK_DISABLE;
  1358. hdmi->mc_clkdis &= ~HDMI_MC_CLKDIS_PIXELCLK_DISABLE;
  1359. hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);
  1360. hdmi->mc_clkdis &= ~HDMI_MC_CLKDIS_TMDSCLK_DISABLE;
  1361. hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);
  1362. /* Enable csc path */
  1363. if (is_color_space_conversion(hdmi)) {
  1364. hdmi->mc_clkdis &= ~HDMI_MC_CLKDIS_CSCCLK_DISABLE;
  1365. hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);
  1366. }
  1367. /* Enable color space conversion if needed */
  1368. if (is_color_space_conversion(hdmi))
  1369. hdmi_writeb(hdmi, HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_IN_PATH,
  1370. HDMI_MC_FLOWCTRL);
  1371. else
  1372. hdmi_writeb(hdmi, HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_BYPASS,
  1373. HDMI_MC_FLOWCTRL);
  1374. }
  1375. /* Workaround to clear the overflow condition */
  1376. static void dw_hdmi_clear_overflow(struct dw_hdmi *hdmi)
  1377. {
  1378. unsigned int count;
  1379. unsigned int i;
  1380. u8 val;
  1381. /*
  1382. * Under some circumstances the Frame Composer arithmetic unit can miss
  1383. * an FC register write due to being busy processing the previous one.
  1384. * The issue can be worked around by issuing a TMDS software reset and
  1385. * then write one of the FC registers several times.
  1386. *
  1387. * The number of iterations matters and depends on the HDMI TX revision
  1388. * (and possibly on the platform). So far i.MX6Q (v1.30a), i.MX6DL
  1389. * (v1.31a) and multiple Allwinner SoCs (v1.32a) have been identified
  1390. * as needing the workaround, with 4 iterations for v1.30a and 1
  1391. * iteration for others.
  1392. * The Amlogic Meson GX SoCs (v2.01a) have been identified as needing
  1393. * the workaround with a single iteration.
  1394. */
  1395. switch (hdmi->version) {
  1396. case 0x130a:
  1397. count = 4;
  1398. break;
  1399. case 0x131a:
  1400. case 0x132a:
  1401. case 0x201a:
  1402. count = 1;
  1403. break;
  1404. default:
  1405. return;
  1406. }
  1407. /* TMDS software reset */
  1408. hdmi_writeb(hdmi, (u8)~HDMI_MC_SWRSTZ_TMDSSWRST_REQ, HDMI_MC_SWRSTZ);
  1409. val = hdmi_readb(hdmi, HDMI_FC_INVIDCONF);
  1410. for (i = 0; i < count; i++)
  1411. hdmi_writeb(hdmi, val, HDMI_FC_INVIDCONF);
  1412. }
  1413. static void hdmi_disable_overflow_interrupts(struct dw_hdmi *hdmi)
  1414. {
  1415. hdmi_writeb(hdmi, HDMI_IH_MUTE_FC_STAT2_OVERFLOW_MASK,
  1416. HDMI_IH_MUTE_FC_STAT2);
  1417. }
  1418. static int dw_hdmi_setup(struct dw_hdmi *hdmi, struct drm_display_mode *mode)
  1419. {
  1420. int ret;
  1421. hdmi_disable_overflow_interrupts(hdmi);
  1422. hdmi->vic = drm_match_cea_mode(mode);
  1423. if (!hdmi->vic) {
  1424. dev_dbg(hdmi->dev, "Non-CEA mode used in HDMI\n");
  1425. } else {
  1426. dev_dbg(hdmi->dev, "CEA mode used vic=%d\n", hdmi->vic);
  1427. }
  1428. if ((hdmi->vic == 6) || (hdmi->vic == 7) ||
  1429. (hdmi->vic == 21) || (hdmi->vic == 22) ||
  1430. (hdmi->vic == 2) || (hdmi->vic == 3) ||
  1431. (hdmi->vic == 17) || (hdmi->vic == 18))
  1432. hdmi->hdmi_data.enc_out_encoding = V4L2_YCBCR_ENC_601;
  1433. else
  1434. hdmi->hdmi_data.enc_out_encoding = V4L2_YCBCR_ENC_709;
  1435. hdmi->hdmi_data.video_mode.mpixelrepetitionoutput = 0;
  1436. hdmi->hdmi_data.video_mode.mpixelrepetitioninput = 0;
  1437. /* TOFIX: Get input format from plat data or fallback to RGB888 */
  1438. if (hdmi->plat_data->input_bus_format)
  1439. hdmi->hdmi_data.enc_in_bus_format =
  1440. hdmi->plat_data->input_bus_format;
  1441. else
  1442. hdmi->hdmi_data.enc_in_bus_format = MEDIA_BUS_FMT_RGB888_1X24;
  1443. /* TOFIX: Get input encoding from plat data or fallback to none */
  1444. if (hdmi->plat_data->input_bus_encoding)
  1445. hdmi->hdmi_data.enc_in_encoding =
  1446. hdmi->plat_data->input_bus_encoding;
  1447. else
  1448. hdmi->hdmi_data.enc_in_encoding = V4L2_YCBCR_ENC_DEFAULT;
  1449. /* TOFIX: Default to RGB888 output format */
  1450. hdmi->hdmi_data.enc_out_bus_format = MEDIA_BUS_FMT_RGB888_1X24;
  1451. hdmi->hdmi_data.pix_repet_factor = 0;
  1452. hdmi->hdmi_data.hdcp_enable = 0;
  1453. hdmi->hdmi_data.video_mode.mdataenablepolarity = true;
  1454. /* HDMI Initialization Step B.1 */
  1455. hdmi_av_composer(hdmi, mode);
  1456. /* HDMI Initializateion Step B.2 */
  1457. ret = hdmi->phy.ops->init(hdmi, hdmi->phy.data, &hdmi->previous_mode);
  1458. if (ret)
  1459. return ret;
  1460. hdmi->phy.enabled = true;
  1461. /* HDMI Initialization Step B.3 */
  1462. dw_hdmi_enable_video_path(hdmi);
  1463. if (hdmi->sink_has_audio) {
  1464. dev_dbg(hdmi->dev, "sink has audio support\n");
  1465. /* HDMI Initialization Step E - Configure audio */
  1466. hdmi_clk_regenerator_update_pixel_clock(hdmi);
  1467. hdmi_enable_audio_clk(hdmi, hdmi->audio_enable);
  1468. }
  1469. /* not for DVI mode */
  1470. if (hdmi->sink_is_hdmi) {
  1471. dev_dbg(hdmi->dev, "%s HDMI mode\n", __func__);
  1472. /* HDMI Initialization Step F - Configure AVI InfoFrame */
  1473. hdmi_config_AVI(hdmi, mode);
  1474. hdmi_config_vendor_specific_infoframe(hdmi, mode);
  1475. } else {
  1476. dev_dbg(hdmi->dev, "%s DVI mode\n", __func__);
  1477. }
  1478. hdmi_video_packetize(hdmi);
  1479. hdmi_video_csc(hdmi);
  1480. hdmi_video_sample(hdmi);
  1481. hdmi_tx_hdcp_config(hdmi);
  1482. dw_hdmi_clear_overflow(hdmi);
  1483. return 0;
  1484. }
  1485. static void dw_hdmi_setup_i2c(struct dw_hdmi *hdmi)
  1486. {
  1487. hdmi_writeb(hdmi, HDMI_PHY_I2CM_INT_ADDR_DONE_POL,
  1488. HDMI_PHY_I2CM_INT_ADDR);
  1489. hdmi_writeb(hdmi, HDMI_PHY_I2CM_CTLINT_ADDR_NAC_POL |
  1490. HDMI_PHY_I2CM_CTLINT_ADDR_ARBITRATION_POL,
  1491. HDMI_PHY_I2CM_CTLINT_ADDR);
  1492. }
  1493. static void initialize_hdmi_ih_mutes(struct dw_hdmi *hdmi)
  1494. {
  1495. u8 ih_mute;
  1496. /*
  1497. * Boot up defaults are:
  1498. * HDMI_IH_MUTE = 0x03 (disabled)
  1499. * HDMI_IH_MUTE_* = 0x00 (enabled)
  1500. *
  1501. * Disable top level interrupt bits in HDMI block
  1502. */
  1503. ih_mute = hdmi_readb(hdmi, HDMI_IH_MUTE) |
  1504. HDMI_IH_MUTE_MUTE_WAKEUP_INTERRUPT |
  1505. HDMI_IH_MUTE_MUTE_ALL_INTERRUPT;
  1506. hdmi_writeb(hdmi, ih_mute, HDMI_IH_MUTE);
  1507. /* by default mask all interrupts */
  1508. hdmi_writeb(hdmi, 0xff, HDMI_VP_MASK);
  1509. hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK0);
  1510. hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK1);
  1511. hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK2);
  1512. hdmi_writeb(hdmi, 0xff, HDMI_PHY_MASK0);
  1513. hdmi_writeb(hdmi, 0xff, HDMI_PHY_I2CM_INT_ADDR);
  1514. hdmi_writeb(hdmi, 0xff, HDMI_PHY_I2CM_CTLINT_ADDR);
  1515. hdmi_writeb(hdmi, 0xff, HDMI_AUD_INT);
  1516. hdmi_writeb(hdmi, 0xff, HDMI_AUD_SPDIFINT);
  1517. hdmi_writeb(hdmi, 0xff, HDMI_AUD_HBR_MASK);
  1518. hdmi_writeb(hdmi, 0xff, HDMI_GP_MASK);
  1519. hdmi_writeb(hdmi, 0xff, HDMI_A_APIINTMSK);
  1520. hdmi_writeb(hdmi, 0xff, HDMI_I2CM_INT);
  1521. hdmi_writeb(hdmi, 0xff, HDMI_I2CM_CTLINT);
  1522. /* Disable interrupts in the IH_MUTE_* registers */
  1523. hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT0);
  1524. hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT1);
  1525. hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT2);
  1526. hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_AS_STAT0);
  1527. hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_PHY_STAT0);
  1528. hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_I2CM_STAT0);
  1529. hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_CEC_STAT0);
  1530. hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_VP_STAT0);
  1531. hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_I2CMPHY_STAT0);
  1532. hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_AHBDMAAUD_STAT0);
  1533. /* Enable top level interrupt bits in HDMI block */
  1534. ih_mute &= ~(HDMI_IH_MUTE_MUTE_WAKEUP_INTERRUPT |
  1535. HDMI_IH_MUTE_MUTE_ALL_INTERRUPT);
  1536. hdmi_writeb(hdmi, ih_mute, HDMI_IH_MUTE);
  1537. }
  1538. static void dw_hdmi_poweron(struct dw_hdmi *hdmi)
  1539. {
  1540. hdmi->bridge_is_on = true;
  1541. dw_hdmi_setup(hdmi, &hdmi->previous_mode);
  1542. }
  1543. static void dw_hdmi_poweroff(struct dw_hdmi *hdmi)
  1544. {
  1545. if (hdmi->phy.enabled) {
  1546. hdmi->phy.ops->disable(hdmi, hdmi->phy.data);
  1547. hdmi->phy.enabled = false;
  1548. }
  1549. hdmi->bridge_is_on = false;
  1550. }
  1551. static void dw_hdmi_update_power(struct dw_hdmi *hdmi)
  1552. {
  1553. int force = hdmi->force;
  1554. if (hdmi->disabled) {
  1555. force = DRM_FORCE_OFF;
  1556. } else if (force == DRM_FORCE_UNSPECIFIED) {
  1557. if (hdmi->rxsense)
  1558. force = DRM_FORCE_ON;
  1559. else
  1560. force = DRM_FORCE_OFF;
  1561. }
  1562. if (force == DRM_FORCE_OFF) {
  1563. if (hdmi->bridge_is_on)
  1564. dw_hdmi_poweroff(hdmi);
  1565. } else {
  1566. if (!hdmi->bridge_is_on)
  1567. dw_hdmi_poweron(hdmi);
  1568. }
  1569. }
  1570. /*
  1571. * Adjust the detection of RXSENSE according to whether we have a forced
  1572. * connection mode enabled, or whether we have been disabled. There is
  1573. * no point processing RXSENSE interrupts if we have a forced connection
  1574. * state, or DRM has us disabled.
  1575. *
  1576. * We also disable rxsense interrupts when we think we're disconnected
  1577. * to avoid floating TDMS signals giving false rxsense interrupts.
  1578. *
  1579. * Note: we still need to listen for HPD interrupts even when DRM has us
  1580. * disabled so that we can detect a connect event.
  1581. */
  1582. static void dw_hdmi_update_phy_mask(struct dw_hdmi *hdmi)
  1583. {
  1584. if (hdmi->phy.ops->update_hpd)
  1585. hdmi->phy.ops->update_hpd(hdmi, hdmi->phy.data,
  1586. hdmi->force, hdmi->disabled,
  1587. hdmi->rxsense);
  1588. }
  1589. static enum drm_connector_status
  1590. dw_hdmi_connector_detect(struct drm_connector *connector, bool force)
  1591. {
  1592. struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
  1593. connector);
  1594. mutex_lock(&hdmi->mutex);
  1595. hdmi->force = DRM_FORCE_UNSPECIFIED;
  1596. dw_hdmi_update_power(hdmi);
  1597. dw_hdmi_update_phy_mask(hdmi);
  1598. mutex_unlock(&hdmi->mutex);
  1599. return hdmi->phy.ops->read_hpd(hdmi, hdmi->phy.data);
  1600. }
  1601. static int dw_hdmi_connector_get_modes(struct drm_connector *connector)
  1602. {
  1603. struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
  1604. connector);
  1605. struct edid *edid;
  1606. int ret = 0;
  1607. if (!hdmi->ddc)
  1608. return 0;
  1609. edid = drm_get_edid(connector, hdmi->ddc);
  1610. if (edid) {
  1611. dev_dbg(hdmi->dev, "got edid: width[%d] x height[%d]\n",
  1612. edid->width_cm, edid->height_cm);
  1613. hdmi->sink_is_hdmi = drm_detect_hdmi_monitor(edid);
  1614. hdmi->sink_has_audio = drm_detect_monitor_audio(edid);
  1615. drm_connector_update_edid_property(connector, edid);
  1616. cec_notifier_set_phys_addr_from_edid(hdmi->cec_notifier, edid);
  1617. ret = drm_add_edid_modes(connector, edid);
  1618. kfree(edid);
  1619. } else {
  1620. dev_dbg(hdmi->dev, "failed to get edid\n");
  1621. }
  1622. return ret;
  1623. }
  1624. static void dw_hdmi_connector_force(struct drm_connector *connector)
  1625. {
  1626. struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
  1627. connector);
  1628. mutex_lock(&hdmi->mutex);
  1629. hdmi->force = connector->force;
  1630. dw_hdmi_update_power(hdmi);
  1631. dw_hdmi_update_phy_mask(hdmi);
  1632. mutex_unlock(&hdmi->mutex);
  1633. }
  1634. static const struct drm_connector_funcs dw_hdmi_connector_funcs = {
  1635. .fill_modes = drm_helper_probe_single_connector_modes,
  1636. .detect = dw_hdmi_connector_detect,
  1637. .destroy = drm_connector_cleanup,
  1638. .force = dw_hdmi_connector_force,
  1639. .reset = drm_atomic_helper_connector_reset,
  1640. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  1641. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  1642. };
  1643. static const struct drm_connector_helper_funcs dw_hdmi_connector_helper_funcs = {
  1644. .get_modes = dw_hdmi_connector_get_modes,
  1645. .best_encoder = drm_atomic_helper_best_encoder,
  1646. };
  1647. static int dw_hdmi_bridge_attach(struct drm_bridge *bridge)
  1648. {
  1649. struct dw_hdmi *hdmi = bridge->driver_private;
  1650. struct drm_encoder *encoder = bridge->encoder;
  1651. struct drm_connector *connector = &hdmi->connector;
  1652. connector->interlace_allowed = 1;
  1653. connector->polled = DRM_CONNECTOR_POLL_HPD;
  1654. drm_connector_helper_add(connector, &dw_hdmi_connector_helper_funcs);
  1655. drm_connector_init(bridge->dev, connector, &dw_hdmi_connector_funcs,
  1656. DRM_MODE_CONNECTOR_HDMIA);
  1657. drm_connector_attach_encoder(connector, encoder);
  1658. return 0;
  1659. }
  1660. static enum drm_mode_status
  1661. dw_hdmi_bridge_mode_valid(struct drm_bridge *bridge,
  1662. const struct drm_display_mode *mode)
  1663. {
  1664. struct dw_hdmi *hdmi = bridge->driver_private;
  1665. struct drm_connector *connector = &hdmi->connector;
  1666. enum drm_mode_status mode_status = MODE_OK;
  1667. /* We don't support double-clocked modes */
  1668. if (mode->flags & DRM_MODE_FLAG_DBLCLK)
  1669. return MODE_BAD;
  1670. if (hdmi->plat_data->mode_valid)
  1671. mode_status = hdmi->plat_data->mode_valid(connector, mode);
  1672. return mode_status;
  1673. }
  1674. static void dw_hdmi_bridge_mode_set(struct drm_bridge *bridge,
  1675. struct drm_display_mode *orig_mode,
  1676. struct drm_display_mode *mode)
  1677. {
  1678. struct dw_hdmi *hdmi = bridge->driver_private;
  1679. mutex_lock(&hdmi->mutex);
  1680. /* Store the display mode for plugin/DKMS poweron events */
  1681. memcpy(&hdmi->previous_mode, mode, sizeof(hdmi->previous_mode));
  1682. mutex_unlock(&hdmi->mutex);
  1683. }
  1684. static void dw_hdmi_bridge_disable(struct drm_bridge *bridge)
  1685. {
  1686. struct dw_hdmi *hdmi = bridge->driver_private;
  1687. mutex_lock(&hdmi->mutex);
  1688. hdmi->disabled = true;
  1689. dw_hdmi_update_power(hdmi);
  1690. dw_hdmi_update_phy_mask(hdmi);
  1691. mutex_unlock(&hdmi->mutex);
  1692. }
  1693. static void dw_hdmi_bridge_enable(struct drm_bridge *bridge)
  1694. {
  1695. struct dw_hdmi *hdmi = bridge->driver_private;
  1696. mutex_lock(&hdmi->mutex);
  1697. hdmi->disabled = false;
  1698. dw_hdmi_update_power(hdmi);
  1699. dw_hdmi_update_phy_mask(hdmi);
  1700. mutex_unlock(&hdmi->mutex);
  1701. }
  1702. static const struct drm_bridge_funcs dw_hdmi_bridge_funcs = {
  1703. .attach = dw_hdmi_bridge_attach,
  1704. .enable = dw_hdmi_bridge_enable,
  1705. .disable = dw_hdmi_bridge_disable,
  1706. .mode_set = dw_hdmi_bridge_mode_set,
  1707. .mode_valid = dw_hdmi_bridge_mode_valid,
  1708. };
  1709. static irqreturn_t dw_hdmi_i2c_irq(struct dw_hdmi *hdmi)
  1710. {
  1711. struct dw_hdmi_i2c *i2c = hdmi->i2c;
  1712. unsigned int stat;
  1713. stat = hdmi_readb(hdmi, HDMI_IH_I2CM_STAT0);
  1714. if (!stat)
  1715. return IRQ_NONE;
  1716. hdmi_writeb(hdmi, stat, HDMI_IH_I2CM_STAT0);
  1717. i2c->stat = stat;
  1718. complete(&i2c->cmp);
  1719. return IRQ_HANDLED;
  1720. }
  1721. static irqreturn_t dw_hdmi_hardirq(int irq, void *dev_id)
  1722. {
  1723. struct dw_hdmi *hdmi = dev_id;
  1724. u8 intr_stat;
  1725. irqreturn_t ret = IRQ_NONE;
  1726. if (hdmi->i2c)
  1727. ret = dw_hdmi_i2c_irq(hdmi);
  1728. intr_stat = hdmi_readb(hdmi, HDMI_IH_PHY_STAT0);
  1729. if (intr_stat) {
  1730. hdmi_writeb(hdmi, ~0, HDMI_IH_MUTE_PHY_STAT0);
  1731. return IRQ_WAKE_THREAD;
  1732. }
  1733. return ret;
  1734. }
  1735. void dw_hdmi_setup_rx_sense(struct dw_hdmi *hdmi, bool hpd, bool rx_sense)
  1736. {
  1737. mutex_lock(&hdmi->mutex);
  1738. if (!hdmi->force) {
  1739. /*
  1740. * If the RX sense status indicates we're disconnected,
  1741. * clear the software rxsense status.
  1742. */
  1743. if (!rx_sense)
  1744. hdmi->rxsense = false;
  1745. /*
  1746. * Only set the software rxsense status when both
  1747. * rxsense and hpd indicates we're connected.
  1748. * This avoids what seems to be bad behaviour in
  1749. * at least iMX6S versions of the phy.
  1750. */
  1751. if (hpd)
  1752. hdmi->rxsense = true;
  1753. dw_hdmi_update_power(hdmi);
  1754. dw_hdmi_update_phy_mask(hdmi);
  1755. }
  1756. mutex_unlock(&hdmi->mutex);
  1757. }
  1758. EXPORT_SYMBOL_GPL(dw_hdmi_setup_rx_sense);
  1759. static irqreturn_t dw_hdmi_irq(int irq, void *dev_id)
  1760. {
  1761. struct dw_hdmi *hdmi = dev_id;
  1762. u8 intr_stat, phy_int_pol, phy_pol_mask, phy_stat;
  1763. intr_stat = hdmi_readb(hdmi, HDMI_IH_PHY_STAT0);
  1764. phy_int_pol = hdmi_readb(hdmi, HDMI_PHY_POL0);
  1765. phy_stat = hdmi_readb(hdmi, HDMI_PHY_STAT0);
  1766. phy_pol_mask = 0;
  1767. if (intr_stat & HDMI_IH_PHY_STAT0_HPD)
  1768. phy_pol_mask |= HDMI_PHY_HPD;
  1769. if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE0)
  1770. phy_pol_mask |= HDMI_PHY_RX_SENSE0;
  1771. if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE1)
  1772. phy_pol_mask |= HDMI_PHY_RX_SENSE1;
  1773. if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE2)
  1774. phy_pol_mask |= HDMI_PHY_RX_SENSE2;
  1775. if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE3)
  1776. phy_pol_mask |= HDMI_PHY_RX_SENSE3;
  1777. if (phy_pol_mask)
  1778. hdmi_modb(hdmi, ~phy_int_pol, phy_pol_mask, HDMI_PHY_POL0);
  1779. /*
  1780. * RX sense tells us whether the TDMS transmitters are detecting
  1781. * load - in other words, there's something listening on the
  1782. * other end of the link. Use this to decide whether we should
  1783. * power on the phy as HPD may be toggled by the sink to merely
  1784. * ask the source to re-read the EDID.
  1785. */
  1786. if (intr_stat &
  1787. (HDMI_IH_PHY_STAT0_RX_SENSE | HDMI_IH_PHY_STAT0_HPD)) {
  1788. dw_hdmi_setup_rx_sense(hdmi,
  1789. phy_stat & HDMI_PHY_HPD,
  1790. phy_stat & HDMI_PHY_RX_SENSE);
  1791. if ((phy_stat & (HDMI_PHY_RX_SENSE | HDMI_PHY_HPD)) == 0)
  1792. cec_notifier_set_phys_addr(hdmi->cec_notifier,
  1793. CEC_PHYS_ADDR_INVALID);
  1794. }
  1795. if (intr_stat & HDMI_IH_PHY_STAT0_HPD) {
  1796. dev_dbg(hdmi->dev, "EVENT=%s\n",
  1797. phy_int_pol & HDMI_PHY_HPD ? "plugin" : "plugout");
  1798. if (hdmi->bridge.dev)
  1799. drm_helper_hpd_irq_event(hdmi->bridge.dev);
  1800. }
  1801. hdmi_writeb(hdmi, intr_stat, HDMI_IH_PHY_STAT0);
  1802. hdmi_writeb(hdmi, ~(HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE),
  1803. HDMI_IH_MUTE_PHY_STAT0);
  1804. return IRQ_HANDLED;
  1805. }
  1806. static const struct dw_hdmi_phy_data dw_hdmi_phys[] = {
  1807. {
  1808. .type = DW_HDMI_PHY_DWC_HDMI_TX_PHY,
  1809. .name = "DWC HDMI TX PHY",
  1810. .gen = 1,
  1811. }, {
  1812. .type = DW_HDMI_PHY_DWC_MHL_PHY_HEAC,
  1813. .name = "DWC MHL PHY + HEAC PHY",
  1814. .gen = 2,
  1815. .has_svsret = true,
  1816. .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
  1817. }, {
  1818. .type = DW_HDMI_PHY_DWC_MHL_PHY,
  1819. .name = "DWC MHL PHY",
  1820. .gen = 2,
  1821. .has_svsret = true,
  1822. .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
  1823. }, {
  1824. .type = DW_HDMI_PHY_DWC_HDMI_3D_TX_PHY_HEAC,
  1825. .name = "DWC HDMI 3D TX PHY + HEAC PHY",
  1826. .gen = 2,
  1827. .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
  1828. }, {
  1829. .type = DW_HDMI_PHY_DWC_HDMI_3D_TX_PHY,
  1830. .name = "DWC HDMI 3D TX PHY",
  1831. .gen = 2,
  1832. .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
  1833. }, {
  1834. .type = DW_HDMI_PHY_DWC_HDMI20_TX_PHY,
  1835. .name = "DWC HDMI 2.0 TX PHY",
  1836. .gen = 2,
  1837. .has_svsret = true,
  1838. .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
  1839. }, {
  1840. .type = DW_HDMI_PHY_VENDOR_PHY,
  1841. .name = "Vendor PHY",
  1842. }
  1843. };
  1844. static int dw_hdmi_detect_phy(struct dw_hdmi *hdmi)
  1845. {
  1846. unsigned int i;
  1847. u8 phy_type;
  1848. phy_type = hdmi_readb(hdmi, HDMI_CONFIG2_ID);
  1849. if (phy_type == DW_HDMI_PHY_VENDOR_PHY) {
  1850. /* Vendor PHYs require support from the glue layer. */
  1851. if (!hdmi->plat_data->phy_ops || !hdmi->plat_data->phy_name) {
  1852. dev_err(hdmi->dev,
  1853. "Vendor HDMI PHY not supported by glue layer\n");
  1854. return -ENODEV;
  1855. }
  1856. hdmi->phy.ops = hdmi->plat_data->phy_ops;
  1857. hdmi->phy.data = hdmi->plat_data->phy_data;
  1858. hdmi->phy.name = hdmi->plat_data->phy_name;
  1859. return 0;
  1860. }
  1861. /* Synopsys PHYs are handled internally. */
  1862. for (i = 0; i < ARRAY_SIZE(dw_hdmi_phys); ++i) {
  1863. if (dw_hdmi_phys[i].type == phy_type) {
  1864. hdmi->phy.ops = &dw_hdmi_synopsys_phy_ops;
  1865. hdmi->phy.name = dw_hdmi_phys[i].name;
  1866. hdmi->phy.data = (void *)&dw_hdmi_phys[i];
  1867. if (!dw_hdmi_phys[i].configure &&
  1868. !hdmi->plat_data->configure_phy) {
  1869. dev_err(hdmi->dev, "%s requires platform support\n",
  1870. hdmi->phy.name);
  1871. return -ENODEV;
  1872. }
  1873. return 0;
  1874. }
  1875. }
  1876. dev_err(hdmi->dev, "Unsupported HDMI PHY type (%02x)\n", phy_type);
  1877. return -ENODEV;
  1878. }
  1879. static void dw_hdmi_cec_enable(struct dw_hdmi *hdmi)
  1880. {
  1881. mutex_lock(&hdmi->mutex);
  1882. hdmi->mc_clkdis &= ~HDMI_MC_CLKDIS_CECCLK_DISABLE;
  1883. hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);
  1884. mutex_unlock(&hdmi->mutex);
  1885. }
  1886. static void dw_hdmi_cec_disable(struct dw_hdmi *hdmi)
  1887. {
  1888. mutex_lock(&hdmi->mutex);
  1889. hdmi->mc_clkdis |= HDMI_MC_CLKDIS_CECCLK_DISABLE;
  1890. hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);
  1891. mutex_unlock(&hdmi->mutex);
  1892. }
  1893. static const struct dw_hdmi_cec_ops dw_hdmi_cec_ops = {
  1894. .write = hdmi_writeb,
  1895. .read = hdmi_readb,
  1896. .enable = dw_hdmi_cec_enable,
  1897. .disable = dw_hdmi_cec_disable,
  1898. };
  1899. static const struct regmap_config hdmi_regmap_8bit_config = {
  1900. .reg_bits = 32,
  1901. .val_bits = 8,
  1902. .reg_stride = 1,
  1903. .max_register = HDMI_I2CM_FS_SCL_LCNT_0_ADDR,
  1904. };
  1905. static const struct regmap_config hdmi_regmap_32bit_config = {
  1906. .reg_bits = 32,
  1907. .val_bits = 32,
  1908. .reg_stride = 4,
  1909. .max_register = HDMI_I2CM_FS_SCL_LCNT_0_ADDR << 2,
  1910. };
  1911. static struct dw_hdmi *
  1912. __dw_hdmi_probe(struct platform_device *pdev,
  1913. const struct dw_hdmi_plat_data *plat_data)
  1914. {
  1915. struct device *dev = &pdev->dev;
  1916. struct device_node *np = dev->of_node;
  1917. struct platform_device_info pdevinfo;
  1918. struct device_node *ddc_node;
  1919. struct dw_hdmi_cec_data cec;
  1920. struct dw_hdmi *hdmi;
  1921. struct resource *iores = NULL;
  1922. int irq;
  1923. int ret;
  1924. u32 val = 1;
  1925. u8 prod_id0;
  1926. u8 prod_id1;
  1927. u8 config0;
  1928. u8 config3;
  1929. hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
  1930. if (!hdmi)
  1931. return ERR_PTR(-ENOMEM);
  1932. hdmi->plat_data = plat_data;
  1933. hdmi->dev = dev;
  1934. hdmi->sample_rate = 48000;
  1935. hdmi->disabled = true;
  1936. hdmi->rxsense = true;
  1937. hdmi->phy_mask = (u8)~(HDMI_PHY_HPD | HDMI_PHY_RX_SENSE);
  1938. hdmi->mc_clkdis = 0x7f;
  1939. mutex_init(&hdmi->mutex);
  1940. mutex_init(&hdmi->audio_mutex);
  1941. spin_lock_init(&hdmi->audio_lock);
  1942. ddc_node = of_parse_phandle(np, "ddc-i2c-bus", 0);
  1943. if (ddc_node) {
  1944. hdmi->ddc = of_get_i2c_adapter_by_node(ddc_node);
  1945. of_node_put(ddc_node);
  1946. if (!hdmi->ddc) {
  1947. dev_dbg(hdmi->dev, "failed to read ddc node\n");
  1948. return ERR_PTR(-EPROBE_DEFER);
  1949. }
  1950. } else {
  1951. dev_dbg(hdmi->dev, "no ddc property found\n");
  1952. }
  1953. if (!plat_data->regm) {
  1954. const struct regmap_config *reg_config;
  1955. of_property_read_u32(np, "reg-io-width", &val);
  1956. switch (val) {
  1957. case 4:
  1958. reg_config = &hdmi_regmap_32bit_config;
  1959. hdmi->reg_shift = 2;
  1960. break;
  1961. case 1:
  1962. reg_config = &hdmi_regmap_8bit_config;
  1963. break;
  1964. default:
  1965. dev_err(dev, "reg-io-width must be 1 or 4\n");
  1966. return ERR_PTR(-EINVAL);
  1967. }
  1968. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1969. hdmi->regs = devm_ioremap_resource(dev, iores);
  1970. if (IS_ERR(hdmi->regs)) {
  1971. ret = PTR_ERR(hdmi->regs);
  1972. goto err_res;
  1973. }
  1974. hdmi->regm = devm_regmap_init_mmio(dev, hdmi->regs, reg_config);
  1975. if (IS_ERR(hdmi->regm)) {
  1976. dev_err(dev, "Failed to configure regmap\n");
  1977. ret = PTR_ERR(hdmi->regm);
  1978. goto err_res;
  1979. }
  1980. } else {
  1981. hdmi->regm = plat_data->regm;
  1982. }
  1983. hdmi->isfr_clk = devm_clk_get(hdmi->dev, "isfr");
  1984. if (IS_ERR(hdmi->isfr_clk)) {
  1985. ret = PTR_ERR(hdmi->isfr_clk);
  1986. dev_err(hdmi->dev, "Unable to get HDMI isfr clk: %d\n", ret);
  1987. goto err_res;
  1988. }
  1989. ret = clk_prepare_enable(hdmi->isfr_clk);
  1990. if (ret) {
  1991. dev_err(hdmi->dev, "Cannot enable HDMI isfr clock: %d\n", ret);
  1992. goto err_res;
  1993. }
  1994. hdmi->iahb_clk = devm_clk_get(hdmi->dev, "iahb");
  1995. if (IS_ERR(hdmi->iahb_clk)) {
  1996. ret = PTR_ERR(hdmi->iahb_clk);
  1997. dev_err(hdmi->dev, "Unable to get HDMI iahb clk: %d\n", ret);
  1998. goto err_isfr;
  1999. }
  2000. ret = clk_prepare_enable(hdmi->iahb_clk);
  2001. if (ret) {
  2002. dev_err(hdmi->dev, "Cannot enable HDMI iahb clock: %d\n", ret);
  2003. goto err_isfr;
  2004. }
  2005. hdmi->cec_clk = devm_clk_get(hdmi->dev, "cec");
  2006. if (PTR_ERR(hdmi->cec_clk) == -ENOENT) {
  2007. hdmi->cec_clk = NULL;
  2008. } else if (IS_ERR(hdmi->cec_clk)) {
  2009. ret = PTR_ERR(hdmi->cec_clk);
  2010. if (ret != -EPROBE_DEFER)
  2011. dev_err(hdmi->dev, "Cannot get HDMI cec clock: %d\n",
  2012. ret);
  2013. hdmi->cec_clk = NULL;
  2014. goto err_iahb;
  2015. } else {
  2016. ret = clk_prepare_enable(hdmi->cec_clk);
  2017. if (ret) {
  2018. dev_err(hdmi->dev, "Cannot enable HDMI cec clock: %d\n",
  2019. ret);
  2020. goto err_iahb;
  2021. }
  2022. }
  2023. /* Product and revision IDs */
  2024. hdmi->version = (hdmi_readb(hdmi, HDMI_DESIGN_ID) << 8)
  2025. | (hdmi_readb(hdmi, HDMI_REVISION_ID) << 0);
  2026. prod_id0 = hdmi_readb(hdmi, HDMI_PRODUCT_ID0);
  2027. prod_id1 = hdmi_readb(hdmi, HDMI_PRODUCT_ID1);
  2028. if (prod_id0 != HDMI_PRODUCT_ID0_HDMI_TX ||
  2029. (prod_id1 & ~HDMI_PRODUCT_ID1_HDCP) != HDMI_PRODUCT_ID1_HDMI_TX) {
  2030. dev_err(dev, "Unsupported HDMI controller (%04x:%02x:%02x)\n",
  2031. hdmi->version, prod_id0, prod_id1);
  2032. ret = -ENODEV;
  2033. goto err_iahb;
  2034. }
  2035. ret = dw_hdmi_detect_phy(hdmi);
  2036. if (ret < 0)
  2037. goto err_iahb;
  2038. dev_info(dev, "Detected HDMI TX controller v%x.%03x %s HDCP (%s)\n",
  2039. hdmi->version >> 12, hdmi->version & 0xfff,
  2040. prod_id1 & HDMI_PRODUCT_ID1_HDCP ? "with" : "without",
  2041. hdmi->phy.name);
  2042. initialize_hdmi_ih_mutes(hdmi);
  2043. irq = platform_get_irq(pdev, 0);
  2044. if (irq < 0) {
  2045. ret = irq;
  2046. goto err_iahb;
  2047. }
  2048. ret = devm_request_threaded_irq(dev, irq, dw_hdmi_hardirq,
  2049. dw_hdmi_irq, IRQF_SHARED,
  2050. dev_name(dev), hdmi);
  2051. if (ret)
  2052. goto err_iahb;
  2053. hdmi->cec_notifier = cec_notifier_get(dev);
  2054. if (!hdmi->cec_notifier) {
  2055. ret = -ENOMEM;
  2056. goto err_iahb;
  2057. }
  2058. /*
  2059. * To prevent overflows in HDMI_IH_FC_STAT2, set the clk regenerator
  2060. * N and cts values before enabling phy
  2061. */
  2062. hdmi_init_clk_regenerator(hdmi);
  2063. /* If DDC bus is not specified, try to register HDMI I2C bus */
  2064. if (!hdmi->ddc) {
  2065. hdmi->ddc = dw_hdmi_i2c_adapter(hdmi);
  2066. if (IS_ERR(hdmi->ddc))
  2067. hdmi->ddc = NULL;
  2068. }
  2069. hdmi->bridge.driver_private = hdmi;
  2070. hdmi->bridge.funcs = &dw_hdmi_bridge_funcs;
  2071. #ifdef CONFIG_OF
  2072. hdmi->bridge.of_node = pdev->dev.of_node;
  2073. #endif
  2074. dw_hdmi_setup_i2c(hdmi);
  2075. if (hdmi->phy.ops->setup_hpd)
  2076. hdmi->phy.ops->setup_hpd(hdmi, hdmi->phy.data);
  2077. memset(&pdevinfo, 0, sizeof(pdevinfo));
  2078. pdevinfo.parent = dev;
  2079. pdevinfo.id = PLATFORM_DEVID_AUTO;
  2080. config0 = hdmi_readb(hdmi, HDMI_CONFIG0_ID);
  2081. config3 = hdmi_readb(hdmi, HDMI_CONFIG3_ID);
  2082. if (iores && config3 & HDMI_CONFIG3_AHBAUDDMA) {
  2083. struct dw_hdmi_audio_data audio;
  2084. audio.phys = iores->start;
  2085. audio.base = hdmi->regs;
  2086. audio.irq = irq;
  2087. audio.hdmi = hdmi;
  2088. audio.eld = hdmi->connector.eld;
  2089. hdmi->enable_audio = dw_hdmi_ahb_audio_enable;
  2090. hdmi->disable_audio = dw_hdmi_ahb_audio_disable;
  2091. pdevinfo.name = "dw-hdmi-ahb-audio";
  2092. pdevinfo.data = &audio;
  2093. pdevinfo.size_data = sizeof(audio);
  2094. pdevinfo.dma_mask = DMA_BIT_MASK(32);
  2095. hdmi->audio = platform_device_register_full(&pdevinfo);
  2096. } else if (config0 & HDMI_CONFIG0_I2S) {
  2097. struct dw_hdmi_i2s_audio_data audio;
  2098. audio.hdmi = hdmi;
  2099. audio.write = hdmi_writeb;
  2100. audio.read = hdmi_readb;
  2101. hdmi->enable_audio = dw_hdmi_i2s_audio_enable;
  2102. hdmi->disable_audio = dw_hdmi_i2s_audio_disable;
  2103. pdevinfo.name = "dw-hdmi-i2s-audio";
  2104. pdevinfo.data = &audio;
  2105. pdevinfo.size_data = sizeof(audio);
  2106. pdevinfo.dma_mask = DMA_BIT_MASK(32);
  2107. hdmi->audio = platform_device_register_full(&pdevinfo);
  2108. }
  2109. if (config0 & HDMI_CONFIG0_CEC) {
  2110. cec.hdmi = hdmi;
  2111. cec.ops = &dw_hdmi_cec_ops;
  2112. cec.irq = irq;
  2113. pdevinfo.name = "dw-hdmi-cec";
  2114. pdevinfo.data = &cec;
  2115. pdevinfo.size_data = sizeof(cec);
  2116. pdevinfo.dma_mask = 0;
  2117. hdmi->cec = platform_device_register_full(&pdevinfo);
  2118. }
  2119. /* Reset HDMI DDC I2C master controller and mute I2CM interrupts */
  2120. if (hdmi->i2c)
  2121. dw_hdmi_i2c_init(hdmi);
  2122. return hdmi;
  2123. err_iahb:
  2124. if (hdmi->i2c) {
  2125. i2c_del_adapter(&hdmi->i2c->adap);
  2126. hdmi->ddc = NULL;
  2127. }
  2128. if (hdmi->cec_notifier)
  2129. cec_notifier_put(hdmi->cec_notifier);
  2130. clk_disable_unprepare(hdmi->iahb_clk);
  2131. if (hdmi->cec_clk)
  2132. clk_disable_unprepare(hdmi->cec_clk);
  2133. err_isfr:
  2134. clk_disable_unprepare(hdmi->isfr_clk);
  2135. err_res:
  2136. i2c_put_adapter(hdmi->ddc);
  2137. return ERR_PTR(ret);
  2138. }
  2139. static void __dw_hdmi_remove(struct dw_hdmi *hdmi)
  2140. {
  2141. if (hdmi->audio && !IS_ERR(hdmi->audio))
  2142. platform_device_unregister(hdmi->audio);
  2143. if (!IS_ERR(hdmi->cec))
  2144. platform_device_unregister(hdmi->cec);
  2145. /* Disable all interrupts */
  2146. hdmi_writeb(hdmi, ~0, HDMI_IH_MUTE_PHY_STAT0);
  2147. if (hdmi->cec_notifier)
  2148. cec_notifier_put(hdmi->cec_notifier);
  2149. clk_disable_unprepare(hdmi->iahb_clk);
  2150. clk_disable_unprepare(hdmi->isfr_clk);
  2151. if (hdmi->cec_clk)
  2152. clk_disable_unprepare(hdmi->cec_clk);
  2153. if (hdmi->i2c)
  2154. i2c_del_adapter(&hdmi->i2c->adap);
  2155. else
  2156. i2c_put_adapter(hdmi->ddc);
  2157. }
  2158. /* -----------------------------------------------------------------------------
  2159. * Probe/remove API, used from platforms based on the DRM bridge API.
  2160. */
  2161. struct dw_hdmi *dw_hdmi_probe(struct platform_device *pdev,
  2162. const struct dw_hdmi_plat_data *plat_data)
  2163. {
  2164. struct dw_hdmi *hdmi;
  2165. hdmi = __dw_hdmi_probe(pdev, plat_data);
  2166. if (IS_ERR(hdmi))
  2167. return hdmi;
  2168. drm_bridge_add(&hdmi->bridge);
  2169. return hdmi;
  2170. }
  2171. EXPORT_SYMBOL_GPL(dw_hdmi_probe);
  2172. void dw_hdmi_remove(struct dw_hdmi *hdmi)
  2173. {
  2174. drm_bridge_remove(&hdmi->bridge);
  2175. __dw_hdmi_remove(hdmi);
  2176. }
  2177. EXPORT_SYMBOL_GPL(dw_hdmi_remove);
  2178. /* -----------------------------------------------------------------------------
  2179. * Bind/unbind API, used from platforms based on the component framework.
  2180. */
  2181. struct dw_hdmi *dw_hdmi_bind(struct platform_device *pdev,
  2182. struct drm_encoder *encoder,
  2183. const struct dw_hdmi_plat_data *plat_data)
  2184. {
  2185. struct dw_hdmi *hdmi;
  2186. int ret;
  2187. hdmi = __dw_hdmi_probe(pdev, plat_data);
  2188. if (IS_ERR(hdmi))
  2189. return hdmi;
  2190. ret = drm_bridge_attach(encoder, &hdmi->bridge, NULL);
  2191. if (ret) {
  2192. dw_hdmi_remove(hdmi);
  2193. DRM_ERROR("Failed to initialize bridge with drm\n");
  2194. return ERR_PTR(ret);
  2195. }
  2196. return hdmi;
  2197. }
  2198. EXPORT_SYMBOL_GPL(dw_hdmi_bind);
  2199. void dw_hdmi_unbind(struct dw_hdmi *hdmi)
  2200. {
  2201. __dw_hdmi_remove(hdmi);
  2202. }
  2203. EXPORT_SYMBOL_GPL(dw_hdmi_unbind);
  2204. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
  2205. MODULE_AUTHOR("Andy Yan <andy.yan@rock-chips.com>");
  2206. MODULE_AUTHOR("Yakir Yang <ykk@rock-chips.com>");
  2207. MODULE_AUTHOR("Vladimir Zapolskiy <vladimir_zapolskiy@mentor.com>");
  2208. MODULE_DESCRIPTION("DW HDMI transmitter driver");
  2209. MODULE_LICENSE("GPL");
  2210. MODULE_ALIAS("platform:dw-hdmi");