dw-hdmi.c 70 KB

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