vc4_hdmi.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507
  1. /*
  2. * Copyright (C) 2015 Broadcom
  3. * Copyright (c) 2014 The Linux Foundation. All rights reserved.
  4. * Copyright (C) 2013 Red Hat
  5. * Author: Rob Clark <robdclark@gmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * DOC: VC4 Falcon HDMI module
  21. *
  22. * The HDMI core has a state machine and a PHY. On BCM2835, most of
  23. * the unit operates off of the HSM clock from CPRMAN. It also
  24. * internally uses the PLLH_PIX clock for the PHY.
  25. *
  26. * HDMI infoframes are kept within a small packet ram, where each
  27. * packet can be individually enabled for including in a frame.
  28. *
  29. * HDMI audio is implemented entirely within the HDMI IP block. A
  30. * register in the HDMI encoder takes SPDIF frames from the DMA engine
  31. * and transfers them over an internal MAI (multi-channel audio
  32. * interconnect) bus to the encoder side for insertion into the video
  33. * blank regions.
  34. *
  35. * The driver's HDMI encoder does not yet support power management.
  36. * The HDMI encoder's power domain and the HSM/pixel clocks are kept
  37. * continuously running, and only the HDMI logic and packet ram are
  38. * powered off/on at disable/enable time.
  39. *
  40. * The driver does not yet support CEC control, though the HDMI
  41. * encoder block has CEC support.
  42. */
  43. #include <drm/drm_atomic_helper.h>
  44. #include <drm/drm_crtc_helper.h>
  45. #include <drm/drm_edid.h>
  46. #include <linux/clk.h>
  47. #include <linux/component.h>
  48. #include <linux/i2c.h>
  49. #include <linux/of_address.h>
  50. #include <linux/of_gpio.h>
  51. #include <linux/of_platform.h>
  52. #include <linux/pm_runtime.h>
  53. #include <linux/rational.h>
  54. #include <sound/dmaengine_pcm.h>
  55. #include <sound/pcm_drm_eld.h>
  56. #include <sound/pcm_params.h>
  57. #include <sound/soc.h>
  58. #include "media/cec.h"
  59. #include "vc4_drv.h"
  60. #include "vc4_regs.h"
  61. #define HSM_CLOCK_FREQ 163682864
  62. #define CEC_CLOCK_FREQ 40000
  63. #define CEC_CLOCK_DIV (HSM_CLOCK_FREQ / CEC_CLOCK_FREQ)
  64. /* HDMI audio information */
  65. struct vc4_hdmi_audio {
  66. struct snd_soc_card card;
  67. struct snd_soc_dai_link link;
  68. int samplerate;
  69. int channels;
  70. struct snd_dmaengine_dai_dma_data dma_data;
  71. struct snd_pcm_substream *substream;
  72. };
  73. /* General HDMI hardware state. */
  74. struct vc4_hdmi {
  75. struct platform_device *pdev;
  76. struct drm_encoder *encoder;
  77. struct drm_connector *connector;
  78. struct vc4_hdmi_audio audio;
  79. struct i2c_adapter *ddc;
  80. void __iomem *hdmicore_regs;
  81. void __iomem *hd_regs;
  82. int hpd_gpio;
  83. bool hpd_active_low;
  84. struct cec_adapter *cec_adap;
  85. struct cec_msg cec_rx_msg;
  86. bool cec_tx_ok;
  87. bool cec_irq_was_rx;
  88. struct clk *pixel_clock;
  89. struct clk *hsm_clock;
  90. };
  91. #define HDMI_READ(offset) readl(vc4->hdmi->hdmicore_regs + offset)
  92. #define HDMI_WRITE(offset, val) writel(val, vc4->hdmi->hdmicore_regs + offset)
  93. #define HD_READ(offset) readl(vc4->hdmi->hd_regs + offset)
  94. #define HD_WRITE(offset, val) writel(val, vc4->hdmi->hd_regs + offset)
  95. /* VC4 HDMI encoder KMS struct */
  96. struct vc4_hdmi_encoder {
  97. struct vc4_encoder base;
  98. bool hdmi_monitor;
  99. bool limited_rgb_range;
  100. bool rgb_range_selectable;
  101. };
  102. static inline struct vc4_hdmi_encoder *
  103. to_vc4_hdmi_encoder(struct drm_encoder *encoder)
  104. {
  105. return container_of(encoder, struct vc4_hdmi_encoder, base.base);
  106. }
  107. /* VC4 HDMI connector KMS struct */
  108. struct vc4_hdmi_connector {
  109. struct drm_connector base;
  110. /* Since the connector is attached to just the one encoder,
  111. * this is the reference to it so we can do the best_encoder()
  112. * hook.
  113. */
  114. struct drm_encoder *encoder;
  115. };
  116. static inline struct vc4_hdmi_connector *
  117. to_vc4_hdmi_connector(struct drm_connector *connector)
  118. {
  119. return container_of(connector, struct vc4_hdmi_connector, base);
  120. }
  121. #define HDMI_REG(reg) { reg, #reg }
  122. static const struct {
  123. u32 reg;
  124. const char *name;
  125. } hdmi_regs[] = {
  126. HDMI_REG(VC4_HDMI_CORE_REV),
  127. HDMI_REG(VC4_HDMI_SW_RESET_CONTROL),
  128. HDMI_REG(VC4_HDMI_HOTPLUG_INT),
  129. HDMI_REG(VC4_HDMI_HOTPLUG),
  130. HDMI_REG(VC4_HDMI_MAI_CHANNEL_MAP),
  131. HDMI_REG(VC4_HDMI_MAI_CONFIG),
  132. HDMI_REG(VC4_HDMI_MAI_FORMAT),
  133. HDMI_REG(VC4_HDMI_AUDIO_PACKET_CONFIG),
  134. HDMI_REG(VC4_HDMI_RAM_PACKET_CONFIG),
  135. HDMI_REG(VC4_HDMI_HORZA),
  136. HDMI_REG(VC4_HDMI_HORZB),
  137. HDMI_REG(VC4_HDMI_FIFO_CTL),
  138. HDMI_REG(VC4_HDMI_SCHEDULER_CONTROL),
  139. HDMI_REG(VC4_HDMI_VERTA0),
  140. HDMI_REG(VC4_HDMI_VERTA1),
  141. HDMI_REG(VC4_HDMI_VERTB0),
  142. HDMI_REG(VC4_HDMI_VERTB1),
  143. HDMI_REG(VC4_HDMI_TX_PHY_RESET_CTL),
  144. HDMI_REG(VC4_HDMI_TX_PHY_CTL0),
  145. HDMI_REG(VC4_HDMI_CEC_CNTRL_1),
  146. HDMI_REG(VC4_HDMI_CEC_CNTRL_2),
  147. HDMI_REG(VC4_HDMI_CEC_CNTRL_3),
  148. HDMI_REG(VC4_HDMI_CEC_CNTRL_4),
  149. HDMI_REG(VC4_HDMI_CEC_CNTRL_5),
  150. HDMI_REG(VC4_HDMI_CPU_STATUS),
  151. HDMI_REG(VC4_HDMI_CPU_MASK_STATUS),
  152. HDMI_REG(VC4_HDMI_CEC_RX_DATA_1),
  153. HDMI_REG(VC4_HDMI_CEC_RX_DATA_2),
  154. HDMI_REG(VC4_HDMI_CEC_RX_DATA_3),
  155. HDMI_REG(VC4_HDMI_CEC_RX_DATA_4),
  156. HDMI_REG(VC4_HDMI_CEC_TX_DATA_1),
  157. HDMI_REG(VC4_HDMI_CEC_TX_DATA_2),
  158. HDMI_REG(VC4_HDMI_CEC_TX_DATA_3),
  159. HDMI_REG(VC4_HDMI_CEC_TX_DATA_4),
  160. };
  161. static const struct {
  162. u32 reg;
  163. const char *name;
  164. } hd_regs[] = {
  165. HDMI_REG(VC4_HD_M_CTL),
  166. HDMI_REG(VC4_HD_MAI_CTL),
  167. HDMI_REG(VC4_HD_MAI_THR),
  168. HDMI_REG(VC4_HD_MAI_FMT),
  169. HDMI_REG(VC4_HD_MAI_SMP),
  170. HDMI_REG(VC4_HD_VID_CTL),
  171. HDMI_REG(VC4_HD_CSC_CTL),
  172. HDMI_REG(VC4_HD_FRAME_COUNT),
  173. };
  174. #ifdef CONFIG_DEBUG_FS
  175. int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused)
  176. {
  177. struct drm_info_node *node = (struct drm_info_node *)m->private;
  178. struct drm_device *dev = node->minor->dev;
  179. struct vc4_dev *vc4 = to_vc4_dev(dev);
  180. int i;
  181. for (i = 0; i < ARRAY_SIZE(hdmi_regs); i++) {
  182. seq_printf(m, "%s (0x%04x): 0x%08x\n",
  183. hdmi_regs[i].name, hdmi_regs[i].reg,
  184. HDMI_READ(hdmi_regs[i].reg));
  185. }
  186. for (i = 0; i < ARRAY_SIZE(hd_regs); i++) {
  187. seq_printf(m, "%s (0x%04x): 0x%08x\n",
  188. hd_regs[i].name, hd_regs[i].reg,
  189. HD_READ(hd_regs[i].reg));
  190. }
  191. return 0;
  192. }
  193. #endif /* CONFIG_DEBUG_FS */
  194. static void vc4_hdmi_dump_regs(struct drm_device *dev)
  195. {
  196. struct vc4_dev *vc4 = to_vc4_dev(dev);
  197. int i;
  198. for (i = 0; i < ARRAY_SIZE(hdmi_regs); i++) {
  199. DRM_INFO("0x%04x (%s): 0x%08x\n",
  200. hdmi_regs[i].reg, hdmi_regs[i].name,
  201. HDMI_READ(hdmi_regs[i].reg));
  202. }
  203. for (i = 0; i < ARRAY_SIZE(hd_regs); i++) {
  204. DRM_INFO("0x%04x (%s): 0x%08x\n",
  205. hd_regs[i].reg, hd_regs[i].name,
  206. HD_READ(hd_regs[i].reg));
  207. }
  208. }
  209. static enum drm_connector_status
  210. vc4_hdmi_connector_detect(struct drm_connector *connector, bool force)
  211. {
  212. struct drm_device *dev = connector->dev;
  213. struct vc4_dev *vc4 = to_vc4_dev(dev);
  214. if (vc4->hdmi->hpd_gpio) {
  215. if (gpio_get_value_cansleep(vc4->hdmi->hpd_gpio) ^
  216. vc4->hdmi->hpd_active_low)
  217. return connector_status_connected;
  218. cec_phys_addr_invalidate(vc4->hdmi->cec_adap);
  219. return connector_status_disconnected;
  220. }
  221. if (drm_probe_ddc(vc4->hdmi->ddc))
  222. return connector_status_connected;
  223. if (HDMI_READ(VC4_HDMI_HOTPLUG) & VC4_HDMI_HOTPLUG_CONNECTED)
  224. return connector_status_connected;
  225. cec_phys_addr_invalidate(vc4->hdmi->cec_adap);
  226. return connector_status_disconnected;
  227. }
  228. static void vc4_hdmi_connector_destroy(struct drm_connector *connector)
  229. {
  230. drm_connector_unregister(connector);
  231. drm_connector_cleanup(connector);
  232. }
  233. static int vc4_hdmi_connector_get_modes(struct drm_connector *connector)
  234. {
  235. struct vc4_hdmi_connector *vc4_connector =
  236. to_vc4_hdmi_connector(connector);
  237. struct drm_encoder *encoder = vc4_connector->encoder;
  238. struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
  239. struct drm_device *dev = connector->dev;
  240. struct vc4_dev *vc4 = to_vc4_dev(dev);
  241. int ret = 0;
  242. struct edid *edid;
  243. edid = drm_get_edid(connector, vc4->hdmi->ddc);
  244. cec_s_phys_addr_from_edid(vc4->hdmi->cec_adap, edid);
  245. if (!edid)
  246. return -ENODEV;
  247. vc4_encoder->hdmi_monitor = drm_detect_hdmi_monitor(edid);
  248. if (edid && edid->input & DRM_EDID_INPUT_DIGITAL) {
  249. vc4_encoder->rgb_range_selectable =
  250. drm_rgb_quant_range_selectable(edid);
  251. }
  252. drm_connector_update_edid_property(connector, edid);
  253. ret = drm_add_edid_modes(connector, edid);
  254. kfree(edid);
  255. return ret;
  256. }
  257. static const struct drm_connector_funcs vc4_hdmi_connector_funcs = {
  258. .detect = vc4_hdmi_connector_detect,
  259. .fill_modes = drm_helper_probe_single_connector_modes,
  260. .destroy = vc4_hdmi_connector_destroy,
  261. .reset = drm_atomic_helper_connector_reset,
  262. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  263. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  264. };
  265. static const struct drm_connector_helper_funcs vc4_hdmi_connector_helper_funcs = {
  266. .get_modes = vc4_hdmi_connector_get_modes,
  267. };
  268. static struct drm_connector *vc4_hdmi_connector_init(struct drm_device *dev,
  269. struct drm_encoder *encoder)
  270. {
  271. struct drm_connector *connector;
  272. struct vc4_hdmi_connector *hdmi_connector;
  273. hdmi_connector = devm_kzalloc(dev->dev, sizeof(*hdmi_connector),
  274. GFP_KERNEL);
  275. if (!hdmi_connector)
  276. return ERR_PTR(-ENOMEM);
  277. connector = &hdmi_connector->base;
  278. hdmi_connector->encoder = encoder;
  279. drm_connector_init(dev, connector, &vc4_hdmi_connector_funcs,
  280. DRM_MODE_CONNECTOR_HDMIA);
  281. drm_connector_helper_add(connector, &vc4_hdmi_connector_helper_funcs);
  282. connector->polled = (DRM_CONNECTOR_POLL_CONNECT |
  283. DRM_CONNECTOR_POLL_DISCONNECT);
  284. connector->interlace_allowed = 1;
  285. connector->doublescan_allowed = 0;
  286. drm_connector_attach_encoder(connector, encoder);
  287. return connector;
  288. }
  289. static void vc4_hdmi_encoder_destroy(struct drm_encoder *encoder)
  290. {
  291. drm_encoder_cleanup(encoder);
  292. }
  293. static const struct drm_encoder_funcs vc4_hdmi_encoder_funcs = {
  294. .destroy = vc4_hdmi_encoder_destroy,
  295. };
  296. static int vc4_hdmi_stop_packet(struct drm_encoder *encoder,
  297. enum hdmi_infoframe_type type)
  298. {
  299. struct drm_device *dev = encoder->dev;
  300. struct vc4_dev *vc4 = to_vc4_dev(dev);
  301. u32 packet_id = type - 0x80;
  302. HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
  303. HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) & ~BIT(packet_id));
  304. return wait_for(!(HDMI_READ(VC4_HDMI_RAM_PACKET_STATUS) &
  305. BIT(packet_id)), 100);
  306. }
  307. static void vc4_hdmi_write_infoframe(struct drm_encoder *encoder,
  308. union hdmi_infoframe *frame)
  309. {
  310. struct drm_device *dev = encoder->dev;
  311. struct vc4_dev *vc4 = to_vc4_dev(dev);
  312. u32 packet_id = frame->any.type - 0x80;
  313. u32 packet_reg = VC4_HDMI_RAM_PACKET(packet_id);
  314. uint8_t buffer[VC4_HDMI_PACKET_STRIDE];
  315. ssize_t len, i;
  316. int ret;
  317. WARN_ONCE(!(HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) &
  318. VC4_HDMI_RAM_PACKET_ENABLE),
  319. "Packet RAM has to be on to store the packet.");
  320. len = hdmi_infoframe_pack(frame, buffer, sizeof(buffer));
  321. if (len < 0)
  322. return;
  323. ret = vc4_hdmi_stop_packet(encoder, frame->any.type);
  324. if (ret) {
  325. DRM_ERROR("Failed to wait for infoframe to go idle: %d\n", ret);
  326. return;
  327. }
  328. for (i = 0; i < len; i += 7) {
  329. HDMI_WRITE(packet_reg,
  330. buffer[i + 0] << 0 |
  331. buffer[i + 1] << 8 |
  332. buffer[i + 2] << 16);
  333. packet_reg += 4;
  334. HDMI_WRITE(packet_reg,
  335. buffer[i + 3] << 0 |
  336. buffer[i + 4] << 8 |
  337. buffer[i + 5] << 16 |
  338. buffer[i + 6] << 24);
  339. packet_reg += 4;
  340. }
  341. HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
  342. HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) | BIT(packet_id));
  343. ret = wait_for((HDMI_READ(VC4_HDMI_RAM_PACKET_STATUS) &
  344. BIT(packet_id)), 100);
  345. if (ret)
  346. DRM_ERROR("Failed to wait for infoframe to start: %d\n", ret);
  347. }
  348. static void vc4_hdmi_set_avi_infoframe(struct drm_encoder *encoder)
  349. {
  350. struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
  351. struct drm_crtc *crtc = encoder->crtc;
  352. const struct drm_display_mode *mode = &crtc->state->adjusted_mode;
  353. union hdmi_infoframe frame;
  354. int ret;
  355. ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi, mode, false);
  356. if (ret < 0) {
  357. DRM_ERROR("couldn't fill AVI infoframe\n");
  358. return;
  359. }
  360. drm_hdmi_avi_infoframe_quant_range(&frame.avi, mode,
  361. vc4_encoder->limited_rgb_range ?
  362. HDMI_QUANTIZATION_RANGE_LIMITED :
  363. HDMI_QUANTIZATION_RANGE_FULL,
  364. vc4_encoder->rgb_range_selectable,
  365. false);
  366. vc4_hdmi_write_infoframe(encoder, &frame);
  367. }
  368. static void vc4_hdmi_set_spd_infoframe(struct drm_encoder *encoder)
  369. {
  370. union hdmi_infoframe frame;
  371. int ret;
  372. ret = hdmi_spd_infoframe_init(&frame.spd, "Broadcom", "Videocore");
  373. if (ret < 0) {
  374. DRM_ERROR("couldn't fill SPD infoframe\n");
  375. return;
  376. }
  377. frame.spd.sdi = HDMI_SPD_SDI_PC;
  378. vc4_hdmi_write_infoframe(encoder, &frame);
  379. }
  380. static void vc4_hdmi_set_audio_infoframe(struct drm_encoder *encoder)
  381. {
  382. struct drm_device *drm = encoder->dev;
  383. struct vc4_dev *vc4 = drm->dev_private;
  384. struct vc4_hdmi *hdmi = vc4->hdmi;
  385. union hdmi_infoframe frame;
  386. int ret;
  387. ret = hdmi_audio_infoframe_init(&frame.audio);
  388. frame.audio.coding_type = HDMI_AUDIO_CODING_TYPE_STREAM;
  389. frame.audio.sample_frequency = HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM;
  390. frame.audio.sample_size = HDMI_AUDIO_SAMPLE_SIZE_STREAM;
  391. frame.audio.channels = hdmi->audio.channels;
  392. vc4_hdmi_write_infoframe(encoder, &frame);
  393. }
  394. static void vc4_hdmi_set_infoframes(struct drm_encoder *encoder)
  395. {
  396. vc4_hdmi_set_avi_infoframe(encoder);
  397. vc4_hdmi_set_spd_infoframe(encoder);
  398. }
  399. static void vc4_hdmi_encoder_disable(struct drm_encoder *encoder)
  400. {
  401. struct drm_device *dev = encoder->dev;
  402. struct vc4_dev *vc4 = to_vc4_dev(dev);
  403. struct vc4_hdmi *hdmi = vc4->hdmi;
  404. int ret;
  405. HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG, 0);
  406. HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL, 0xf << 16);
  407. HD_WRITE(VC4_HD_VID_CTL,
  408. HD_READ(VC4_HD_VID_CTL) & ~VC4_HD_VID_CTL_ENABLE);
  409. clk_disable_unprepare(hdmi->pixel_clock);
  410. ret = pm_runtime_put(&hdmi->pdev->dev);
  411. if (ret < 0)
  412. DRM_ERROR("Failed to release power domain: %d\n", ret);
  413. }
  414. static void vc4_hdmi_encoder_enable(struct drm_encoder *encoder)
  415. {
  416. struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
  417. struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
  418. struct drm_device *dev = encoder->dev;
  419. struct vc4_dev *vc4 = to_vc4_dev(dev);
  420. struct vc4_hdmi *hdmi = vc4->hdmi;
  421. bool debug_dump_regs = false;
  422. bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
  423. bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
  424. bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
  425. u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
  426. u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
  427. VC4_HDMI_VERTA_VSP) |
  428. VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
  429. VC4_HDMI_VERTA_VFP) |
  430. VC4_SET_FIELD(mode->crtc_vdisplay, VC4_HDMI_VERTA_VAL));
  431. u32 vertb = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
  432. VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end,
  433. VC4_HDMI_VERTB_VBP));
  434. u32 vertb_even = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
  435. VC4_SET_FIELD(mode->crtc_vtotal -
  436. mode->crtc_vsync_end -
  437. interlaced,
  438. VC4_HDMI_VERTB_VBP));
  439. u32 csc_ctl;
  440. int ret;
  441. ret = pm_runtime_get_sync(&hdmi->pdev->dev);
  442. if (ret < 0) {
  443. DRM_ERROR("Failed to retain power domain: %d\n", ret);
  444. return;
  445. }
  446. ret = clk_set_rate(hdmi->pixel_clock,
  447. mode->clock * 1000 *
  448. ((mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1));
  449. if (ret) {
  450. DRM_ERROR("Failed to set pixel clock rate: %d\n", ret);
  451. return;
  452. }
  453. ret = clk_prepare_enable(hdmi->pixel_clock);
  454. if (ret) {
  455. DRM_ERROR("Failed to turn on pixel clock: %d\n", ret);
  456. return;
  457. }
  458. HDMI_WRITE(VC4_HDMI_SW_RESET_CONTROL,
  459. VC4_HDMI_SW_RESET_HDMI |
  460. VC4_HDMI_SW_RESET_FORMAT_DETECT);
  461. HDMI_WRITE(VC4_HDMI_SW_RESET_CONTROL, 0);
  462. /* PHY should be in reset, like
  463. * vc4_hdmi_encoder_disable() does.
  464. */
  465. HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL, 0xf << 16);
  466. HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL, 0);
  467. if (debug_dump_regs) {
  468. DRM_INFO("HDMI regs before:\n");
  469. vc4_hdmi_dump_regs(dev);
  470. }
  471. HD_WRITE(VC4_HD_VID_CTL, 0);
  472. HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
  473. HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
  474. VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT |
  475. VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS);
  476. HDMI_WRITE(VC4_HDMI_HORZA,
  477. (vsync_pos ? VC4_HDMI_HORZA_VPOS : 0) |
  478. (hsync_pos ? VC4_HDMI_HORZA_HPOS : 0) |
  479. VC4_SET_FIELD(mode->hdisplay * pixel_rep,
  480. VC4_HDMI_HORZA_HAP));
  481. HDMI_WRITE(VC4_HDMI_HORZB,
  482. VC4_SET_FIELD((mode->htotal -
  483. mode->hsync_end) * pixel_rep,
  484. VC4_HDMI_HORZB_HBP) |
  485. VC4_SET_FIELD((mode->hsync_end -
  486. mode->hsync_start) * pixel_rep,
  487. VC4_HDMI_HORZB_HSP) |
  488. VC4_SET_FIELD((mode->hsync_start -
  489. mode->hdisplay) * pixel_rep,
  490. VC4_HDMI_HORZB_HFP));
  491. HDMI_WRITE(VC4_HDMI_VERTA0, verta);
  492. HDMI_WRITE(VC4_HDMI_VERTA1, verta);
  493. HDMI_WRITE(VC4_HDMI_VERTB0, vertb_even);
  494. HDMI_WRITE(VC4_HDMI_VERTB1, vertb);
  495. HD_WRITE(VC4_HD_VID_CTL,
  496. (vsync_pos ? 0 : VC4_HD_VID_CTL_VSYNC_LOW) |
  497. (hsync_pos ? 0 : VC4_HD_VID_CTL_HSYNC_LOW));
  498. csc_ctl = VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR,
  499. VC4_HD_CSC_CTL_ORDER);
  500. if (vc4_encoder->hdmi_monitor &&
  501. drm_default_rgb_quant_range(mode) ==
  502. HDMI_QUANTIZATION_RANGE_LIMITED) {
  503. /* CEA VICs other than #1 requre limited range RGB
  504. * output unless overridden by an AVI infoframe.
  505. * Apply a colorspace conversion to squash 0-255 down
  506. * to 16-235. The matrix here is:
  507. *
  508. * [ 0 0 0.8594 16]
  509. * [ 0 0.8594 0 16]
  510. * [ 0.8594 0 0 16]
  511. * [ 0 0 0 1]
  512. */
  513. csc_ctl |= VC4_HD_CSC_CTL_ENABLE;
  514. csc_ctl |= VC4_HD_CSC_CTL_RGB2YCC;
  515. csc_ctl |= VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
  516. VC4_HD_CSC_CTL_MODE);
  517. HD_WRITE(VC4_HD_CSC_12_11, (0x000 << 16) | 0x000);
  518. HD_WRITE(VC4_HD_CSC_14_13, (0x100 << 16) | 0x6e0);
  519. HD_WRITE(VC4_HD_CSC_22_21, (0x6e0 << 16) | 0x000);
  520. HD_WRITE(VC4_HD_CSC_24_23, (0x100 << 16) | 0x000);
  521. HD_WRITE(VC4_HD_CSC_32_31, (0x000 << 16) | 0x6e0);
  522. HD_WRITE(VC4_HD_CSC_34_33, (0x100 << 16) | 0x000);
  523. vc4_encoder->limited_rgb_range = true;
  524. } else {
  525. vc4_encoder->limited_rgb_range = false;
  526. }
  527. /* The RGB order applies even when CSC is disabled. */
  528. HD_WRITE(VC4_HD_CSC_CTL, csc_ctl);
  529. HDMI_WRITE(VC4_HDMI_FIFO_CTL, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N);
  530. if (debug_dump_regs) {
  531. DRM_INFO("HDMI regs after:\n");
  532. vc4_hdmi_dump_regs(dev);
  533. }
  534. HD_WRITE(VC4_HD_VID_CTL,
  535. HD_READ(VC4_HD_VID_CTL) |
  536. VC4_HD_VID_CTL_ENABLE |
  537. VC4_HD_VID_CTL_UNDERFLOW_ENABLE |
  538. VC4_HD_VID_CTL_FRAME_COUNTER_RESET);
  539. if (vc4_encoder->hdmi_monitor) {
  540. HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
  541. HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
  542. VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
  543. ret = wait_for(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
  544. VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE, 1000);
  545. WARN_ONCE(ret, "Timeout waiting for "
  546. "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
  547. } else {
  548. HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
  549. HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) &
  550. ~(VC4_HDMI_RAM_PACKET_ENABLE));
  551. HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
  552. HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
  553. ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
  554. ret = wait_for(!(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
  555. VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE), 1000);
  556. WARN_ONCE(ret, "Timeout waiting for "
  557. "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
  558. }
  559. if (vc4_encoder->hdmi_monitor) {
  560. u32 drift;
  561. WARN_ON(!(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
  562. VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE));
  563. HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
  564. HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
  565. VC4_HDMI_SCHEDULER_CONTROL_VERT_ALWAYS_KEEPOUT);
  566. HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
  567. VC4_HDMI_RAM_PACKET_ENABLE);
  568. vc4_hdmi_set_infoframes(encoder);
  569. drift = HDMI_READ(VC4_HDMI_FIFO_CTL);
  570. drift &= VC4_HDMI_FIFO_VALID_WRITE_MASK;
  571. HDMI_WRITE(VC4_HDMI_FIFO_CTL,
  572. drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
  573. HDMI_WRITE(VC4_HDMI_FIFO_CTL,
  574. drift | VC4_HDMI_FIFO_CTL_RECENTER);
  575. usleep_range(1000, 1100);
  576. HDMI_WRITE(VC4_HDMI_FIFO_CTL,
  577. drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
  578. HDMI_WRITE(VC4_HDMI_FIFO_CTL,
  579. drift | VC4_HDMI_FIFO_CTL_RECENTER);
  580. ret = wait_for(HDMI_READ(VC4_HDMI_FIFO_CTL) &
  581. VC4_HDMI_FIFO_CTL_RECENTER_DONE, 1);
  582. WARN_ONCE(ret, "Timeout waiting for "
  583. "VC4_HDMI_FIFO_CTL_RECENTER_DONE");
  584. }
  585. }
  586. static enum drm_mode_status
  587. vc4_hdmi_encoder_mode_valid(struct drm_encoder *crtc,
  588. const struct drm_display_mode *mode)
  589. {
  590. /* HSM clock must be 108% of the pixel clock. Additionally,
  591. * the AXI clock needs to be at least 25% of pixel clock, but
  592. * HSM ends up being the limiting factor.
  593. */
  594. if (mode->clock > HSM_CLOCK_FREQ / (1000 * 108 / 100))
  595. return MODE_CLOCK_HIGH;
  596. return MODE_OK;
  597. }
  598. static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs = {
  599. .mode_valid = vc4_hdmi_encoder_mode_valid,
  600. .disable = vc4_hdmi_encoder_disable,
  601. .enable = vc4_hdmi_encoder_enable,
  602. };
  603. /* HDMI audio codec callbacks */
  604. static void vc4_hdmi_audio_set_mai_clock(struct vc4_hdmi *hdmi)
  605. {
  606. struct drm_device *drm = hdmi->encoder->dev;
  607. struct vc4_dev *vc4 = to_vc4_dev(drm);
  608. u32 hsm_clock = clk_get_rate(hdmi->hsm_clock);
  609. unsigned long n, m;
  610. rational_best_approximation(hsm_clock, hdmi->audio.samplerate,
  611. VC4_HD_MAI_SMP_N_MASK >>
  612. VC4_HD_MAI_SMP_N_SHIFT,
  613. (VC4_HD_MAI_SMP_M_MASK >>
  614. VC4_HD_MAI_SMP_M_SHIFT) + 1,
  615. &n, &m);
  616. HD_WRITE(VC4_HD_MAI_SMP,
  617. VC4_SET_FIELD(n, VC4_HD_MAI_SMP_N) |
  618. VC4_SET_FIELD(m - 1, VC4_HD_MAI_SMP_M));
  619. }
  620. static void vc4_hdmi_set_n_cts(struct vc4_hdmi *hdmi)
  621. {
  622. struct drm_encoder *encoder = hdmi->encoder;
  623. struct drm_crtc *crtc = encoder->crtc;
  624. struct drm_device *drm = encoder->dev;
  625. struct vc4_dev *vc4 = to_vc4_dev(drm);
  626. const struct drm_display_mode *mode = &crtc->state->adjusted_mode;
  627. u32 samplerate = hdmi->audio.samplerate;
  628. u32 n, cts;
  629. u64 tmp;
  630. n = 128 * samplerate / 1000;
  631. tmp = (u64)(mode->clock * 1000) * n;
  632. do_div(tmp, 128 * samplerate);
  633. cts = tmp;
  634. HDMI_WRITE(VC4_HDMI_CRP_CFG,
  635. VC4_HDMI_CRP_CFG_EXTERNAL_CTS_EN |
  636. VC4_SET_FIELD(n, VC4_HDMI_CRP_CFG_N));
  637. /*
  638. * We could get slightly more accurate clocks in some cases by
  639. * providing a CTS_1 value. The two CTS values are alternated
  640. * between based on the period fields
  641. */
  642. HDMI_WRITE(VC4_HDMI_CTS_0, cts);
  643. HDMI_WRITE(VC4_HDMI_CTS_1, cts);
  644. }
  645. static inline struct vc4_hdmi *dai_to_hdmi(struct snd_soc_dai *dai)
  646. {
  647. struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai);
  648. return snd_soc_card_get_drvdata(card);
  649. }
  650. static int vc4_hdmi_audio_startup(struct snd_pcm_substream *substream,
  651. struct snd_soc_dai *dai)
  652. {
  653. struct vc4_hdmi *hdmi = dai_to_hdmi(dai);
  654. struct drm_encoder *encoder = hdmi->encoder;
  655. struct vc4_dev *vc4 = to_vc4_dev(encoder->dev);
  656. int ret;
  657. if (hdmi->audio.substream && hdmi->audio.substream != substream)
  658. return -EINVAL;
  659. hdmi->audio.substream = substream;
  660. /*
  661. * If the HDMI encoder hasn't probed, or the encoder is
  662. * currently in DVI mode, treat the codec dai as missing.
  663. */
  664. if (!encoder->crtc || !(HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) &
  665. VC4_HDMI_RAM_PACKET_ENABLE))
  666. return -ENODEV;
  667. ret = snd_pcm_hw_constraint_eld(substream->runtime,
  668. hdmi->connector->eld);
  669. if (ret)
  670. return ret;
  671. return 0;
  672. }
  673. static int vc4_hdmi_audio_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  674. {
  675. return 0;
  676. }
  677. static void vc4_hdmi_audio_reset(struct vc4_hdmi *hdmi)
  678. {
  679. struct drm_encoder *encoder = hdmi->encoder;
  680. struct drm_device *drm = encoder->dev;
  681. struct device *dev = &hdmi->pdev->dev;
  682. struct vc4_dev *vc4 = to_vc4_dev(drm);
  683. int ret;
  684. ret = vc4_hdmi_stop_packet(encoder, HDMI_INFOFRAME_TYPE_AUDIO);
  685. if (ret)
  686. dev_err(dev, "Failed to stop audio infoframe: %d\n", ret);
  687. HD_WRITE(VC4_HD_MAI_CTL, VC4_HD_MAI_CTL_RESET);
  688. HD_WRITE(VC4_HD_MAI_CTL, VC4_HD_MAI_CTL_ERRORF);
  689. HD_WRITE(VC4_HD_MAI_CTL, VC4_HD_MAI_CTL_FLUSH);
  690. }
  691. static void vc4_hdmi_audio_shutdown(struct snd_pcm_substream *substream,
  692. struct snd_soc_dai *dai)
  693. {
  694. struct vc4_hdmi *hdmi = dai_to_hdmi(dai);
  695. if (substream != hdmi->audio.substream)
  696. return;
  697. vc4_hdmi_audio_reset(hdmi);
  698. hdmi->audio.substream = NULL;
  699. }
  700. /* HDMI audio codec callbacks */
  701. static int vc4_hdmi_audio_hw_params(struct snd_pcm_substream *substream,
  702. struct snd_pcm_hw_params *params,
  703. struct snd_soc_dai *dai)
  704. {
  705. struct vc4_hdmi *hdmi = dai_to_hdmi(dai);
  706. struct drm_encoder *encoder = hdmi->encoder;
  707. struct drm_device *drm = encoder->dev;
  708. struct device *dev = &hdmi->pdev->dev;
  709. struct vc4_dev *vc4 = to_vc4_dev(drm);
  710. u32 audio_packet_config, channel_mask;
  711. u32 channel_map, i;
  712. if (substream != hdmi->audio.substream)
  713. return -EINVAL;
  714. dev_dbg(dev, "%s: %u Hz, %d bit, %d channels\n", __func__,
  715. params_rate(params), params_width(params),
  716. params_channels(params));
  717. hdmi->audio.channels = params_channels(params);
  718. hdmi->audio.samplerate = params_rate(params);
  719. HD_WRITE(VC4_HD_MAI_CTL,
  720. VC4_HD_MAI_CTL_RESET |
  721. VC4_HD_MAI_CTL_FLUSH |
  722. VC4_HD_MAI_CTL_DLATE |
  723. VC4_HD_MAI_CTL_ERRORE |
  724. VC4_HD_MAI_CTL_ERRORF);
  725. vc4_hdmi_audio_set_mai_clock(hdmi);
  726. audio_packet_config =
  727. VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_SAMPLE_FLAT |
  728. VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_INACTIVE_CHANNELS |
  729. VC4_SET_FIELD(0xf, VC4_HDMI_AUDIO_PACKET_B_FRAME_IDENTIFIER);
  730. channel_mask = GENMASK(hdmi->audio.channels - 1, 0);
  731. audio_packet_config |= VC4_SET_FIELD(channel_mask,
  732. VC4_HDMI_AUDIO_PACKET_CEA_MASK);
  733. /* Set the MAI threshold. This logic mimics the firmware's. */
  734. if (hdmi->audio.samplerate > 96000) {
  735. HD_WRITE(VC4_HD_MAI_THR,
  736. VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQHIGH) |
  737. VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQLOW));
  738. } else if (hdmi->audio.samplerate > 48000) {
  739. HD_WRITE(VC4_HD_MAI_THR,
  740. VC4_SET_FIELD(0x14, VC4_HD_MAI_THR_DREQHIGH) |
  741. VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQLOW));
  742. } else {
  743. HD_WRITE(VC4_HD_MAI_THR,
  744. VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICHIGH) |
  745. VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICLOW) |
  746. VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_DREQHIGH) |
  747. VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_DREQLOW));
  748. }
  749. HDMI_WRITE(VC4_HDMI_MAI_CONFIG,
  750. VC4_HDMI_MAI_CONFIG_BIT_REVERSE |
  751. VC4_SET_FIELD(channel_mask, VC4_HDMI_MAI_CHANNEL_MASK));
  752. channel_map = 0;
  753. for (i = 0; i < 8; i++) {
  754. if (channel_mask & BIT(i))
  755. channel_map |= i << (3 * i);
  756. }
  757. HDMI_WRITE(VC4_HDMI_MAI_CHANNEL_MAP, channel_map);
  758. HDMI_WRITE(VC4_HDMI_AUDIO_PACKET_CONFIG, audio_packet_config);
  759. vc4_hdmi_set_n_cts(hdmi);
  760. return 0;
  761. }
  762. static int vc4_hdmi_audio_trigger(struct snd_pcm_substream *substream, int cmd,
  763. struct snd_soc_dai *dai)
  764. {
  765. struct vc4_hdmi *hdmi = dai_to_hdmi(dai);
  766. struct drm_encoder *encoder = hdmi->encoder;
  767. struct drm_device *drm = encoder->dev;
  768. struct vc4_dev *vc4 = to_vc4_dev(drm);
  769. switch (cmd) {
  770. case SNDRV_PCM_TRIGGER_START:
  771. vc4_hdmi_set_audio_infoframe(encoder);
  772. HDMI_WRITE(VC4_HDMI_TX_PHY_CTL0,
  773. HDMI_READ(VC4_HDMI_TX_PHY_CTL0) &
  774. ~VC4_HDMI_TX_PHY_RNG_PWRDN);
  775. HD_WRITE(VC4_HD_MAI_CTL,
  776. VC4_SET_FIELD(hdmi->audio.channels,
  777. VC4_HD_MAI_CTL_CHNUM) |
  778. VC4_HD_MAI_CTL_ENABLE);
  779. break;
  780. case SNDRV_PCM_TRIGGER_STOP:
  781. HD_WRITE(VC4_HD_MAI_CTL,
  782. VC4_HD_MAI_CTL_DLATE |
  783. VC4_HD_MAI_CTL_ERRORE |
  784. VC4_HD_MAI_CTL_ERRORF);
  785. HDMI_WRITE(VC4_HDMI_TX_PHY_CTL0,
  786. HDMI_READ(VC4_HDMI_TX_PHY_CTL0) |
  787. VC4_HDMI_TX_PHY_RNG_PWRDN);
  788. break;
  789. default:
  790. break;
  791. }
  792. return 0;
  793. }
  794. static inline struct vc4_hdmi *
  795. snd_component_to_hdmi(struct snd_soc_component *component)
  796. {
  797. struct snd_soc_card *card = snd_soc_component_get_drvdata(component);
  798. return snd_soc_card_get_drvdata(card);
  799. }
  800. static int vc4_hdmi_audio_eld_ctl_info(struct snd_kcontrol *kcontrol,
  801. struct snd_ctl_elem_info *uinfo)
  802. {
  803. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  804. struct vc4_hdmi *hdmi = snd_component_to_hdmi(component);
  805. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  806. uinfo->count = sizeof(hdmi->connector->eld);
  807. return 0;
  808. }
  809. static int vc4_hdmi_audio_eld_ctl_get(struct snd_kcontrol *kcontrol,
  810. struct snd_ctl_elem_value *ucontrol)
  811. {
  812. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  813. struct vc4_hdmi *hdmi = snd_component_to_hdmi(component);
  814. memcpy(ucontrol->value.bytes.data, hdmi->connector->eld,
  815. sizeof(hdmi->connector->eld));
  816. return 0;
  817. }
  818. static const struct snd_kcontrol_new vc4_hdmi_audio_controls[] = {
  819. {
  820. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  821. SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  822. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  823. .name = "ELD",
  824. .info = vc4_hdmi_audio_eld_ctl_info,
  825. .get = vc4_hdmi_audio_eld_ctl_get,
  826. },
  827. };
  828. static const struct snd_soc_dapm_widget vc4_hdmi_audio_widgets[] = {
  829. SND_SOC_DAPM_OUTPUT("TX"),
  830. };
  831. static const struct snd_soc_dapm_route vc4_hdmi_audio_routes[] = {
  832. { "TX", NULL, "Playback" },
  833. };
  834. static const struct snd_soc_component_driver vc4_hdmi_audio_component_drv = {
  835. .controls = vc4_hdmi_audio_controls,
  836. .num_controls = ARRAY_SIZE(vc4_hdmi_audio_controls),
  837. .dapm_widgets = vc4_hdmi_audio_widgets,
  838. .num_dapm_widgets = ARRAY_SIZE(vc4_hdmi_audio_widgets),
  839. .dapm_routes = vc4_hdmi_audio_routes,
  840. .num_dapm_routes = ARRAY_SIZE(vc4_hdmi_audio_routes),
  841. .idle_bias_on = 1,
  842. .use_pmdown_time = 1,
  843. .endianness = 1,
  844. .non_legacy_dai_naming = 1,
  845. };
  846. static const struct snd_soc_dai_ops vc4_hdmi_audio_dai_ops = {
  847. .startup = vc4_hdmi_audio_startup,
  848. .shutdown = vc4_hdmi_audio_shutdown,
  849. .hw_params = vc4_hdmi_audio_hw_params,
  850. .set_fmt = vc4_hdmi_audio_set_fmt,
  851. .trigger = vc4_hdmi_audio_trigger,
  852. };
  853. static struct snd_soc_dai_driver vc4_hdmi_audio_codec_dai_drv = {
  854. .name = "vc4-hdmi-hifi",
  855. .playback = {
  856. .stream_name = "Playback",
  857. .channels_min = 2,
  858. .channels_max = 8,
  859. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
  860. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
  861. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
  862. SNDRV_PCM_RATE_192000,
  863. .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
  864. },
  865. };
  866. static const struct snd_soc_component_driver vc4_hdmi_audio_cpu_dai_comp = {
  867. .name = "vc4-hdmi-cpu-dai-component",
  868. };
  869. static int vc4_hdmi_audio_cpu_dai_probe(struct snd_soc_dai *dai)
  870. {
  871. struct vc4_hdmi *hdmi = dai_to_hdmi(dai);
  872. snd_soc_dai_init_dma_data(dai, &hdmi->audio.dma_data, NULL);
  873. return 0;
  874. }
  875. static struct snd_soc_dai_driver vc4_hdmi_audio_cpu_dai_drv = {
  876. .name = "vc4-hdmi-cpu-dai",
  877. .probe = vc4_hdmi_audio_cpu_dai_probe,
  878. .playback = {
  879. .stream_name = "Playback",
  880. .channels_min = 1,
  881. .channels_max = 8,
  882. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
  883. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
  884. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
  885. SNDRV_PCM_RATE_192000,
  886. .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
  887. },
  888. .ops = &vc4_hdmi_audio_dai_ops,
  889. };
  890. static const struct snd_dmaengine_pcm_config pcm_conf = {
  891. .chan_names[SNDRV_PCM_STREAM_PLAYBACK] = "audio-rx",
  892. .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
  893. };
  894. static int vc4_hdmi_audio_init(struct vc4_hdmi *hdmi)
  895. {
  896. struct snd_soc_dai_link *dai_link = &hdmi->audio.link;
  897. struct snd_soc_card *card = &hdmi->audio.card;
  898. struct device *dev = &hdmi->pdev->dev;
  899. const __be32 *addr;
  900. int ret;
  901. if (!of_find_property(dev->of_node, "dmas", NULL)) {
  902. dev_warn(dev,
  903. "'dmas' DT property is missing, no HDMI audio\n");
  904. return 0;
  905. }
  906. /*
  907. * Get the physical address of VC4_HD_MAI_DATA. We need to retrieve
  908. * the bus address specified in the DT, because the physical address
  909. * (the one returned by platform_get_resource()) is not appropriate
  910. * for DMA transfers.
  911. * This VC/MMU should probably be exposed to avoid this kind of hacks.
  912. */
  913. addr = of_get_address(dev->of_node, 1, NULL, NULL);
  914. hdmi->audio.dma_data.addr = be32_to_cpup(addr) + VC4_HD_MAI_DATA;
  915. hdmi->audio.dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  916. hdmi->audio.dma_data.maxburst = 2;
  917. ret = devm_snd_dmaengine_pcm_register(dev, &pcm_conf, 0);
  918. if (ret) {
  919. dev_err(dev, "Could not register PCM component: %d\n", ret);
  920. return ret;
  921. }
  922. ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_cpu_dai_comp,
  923. &vc4_hdmi_audio_cpu_dai_drv, 1);
  924. if (ret) {
  925. dev_err(dev, "Could not register CPU DAI: %d\n", ret);
  926. return ret;
  927. }
  928. /* register component and codec dai */
  929. ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_component_drv,
  930. &vc4_hdmi_audio_codec_dai_drv, 1);
  931. if (ret) {
  932. dev_err(dev, "Could not register component: %d\n", ret);
  933. return ret;
  934. }
  935. dai_link->name = "MAI";
  936. dai_link->stream_name = "MAI PCM";
  937. dai_link->codec_dai_name = vc4_hdmi_audio_codec_dai_drv.name;
  938. dai_link->cpu_dai_name = dev_name(dev);
  939. dai_link->codec_name = dev_name(dev);
  940. dai_link->platform_name = dev_name(dev);
  941. card->dai_link = dai_link;
  942. card->num_links = 1;
  943. card->name = "vc4-hdmi";
  944. card->dev = dev;
  945. /*
  946. * Be careful, snd_soc_register_card() calls dev_set_drvdata() and
  947. * stores a pointer to the snd card object in dev->driver_data. This
  948. * means we cannot use it for something else. The hdmi back-pointer is
  949. * now stored in card->drvdata and should be retrieved with
  950. * snd_soc_card_get_drvdata() if needed.
  951. */
  952. snd_soc_card_set_drvdata(card, hdmi);
  953. ret = devm_snd_soc_register_card(dev, card);
  954. if (ret)
  955. dev_err(dev, "Could not register sound card: %d\n", ret);
  956. return ret;
  957. }
  958. #ifdef CONFIG_DRM_VC4_HDMI_CEC
  959. static irqreturn_t vc4_cec_irq_handler_thread(int irq, void *priv)
  960. {
  961. struct vc4_dev *vc4 = priv;
  962. struct vc4_hdmi *hdmi = vc4->hdmi;
  963. if (hdmi->cec_irq_was_rx) {
  964. if (hdmi->cec_rx_msg.len)
  965. cec_received_msg(hdmi->cec_adap, &hdmi->cec_rx_msg);
  966. } else if (hdmi->cec_tx_ok) {
  967. cec_transmit_done(hdmi->cec_adap, CEC_TX_STATUS_OK,
  968. 0, 0, 0, 0);
  969. } else {
  970. /*
  971. * This CEC implementation makes 1 retry, so if we
  972. * get a NACK, then that means it made 2 attempts.
  973. */
  974. cec_transmit_done(hdmi->cec_adap, CEC_TX_STATUS_NACK,
  975. 0, 2, 0, 0);
  976. }
  977. return IRQ_HANDLED;
  978. }
  979. static void vc4_cec_read_msg(struct vc4_dev *vc4, u32 cntrl1)
  980. {
  981. struct cec_msg *msg = &vc4->hdmi->cec_rx_msg;
  982. unsigned int i;
  983. msg->len = 1 + ((cntrl1 & VC4_HDMI_CEC_REC_WRD_CNT_MASK) >>
  984. VC4_HDMI_CEC_REC_WRD_CNT_SHIFT);
  985. for (i = 0; i < msg->len; i += 4) {
  986. u32 val = HDMI_READ(VC4_HDMI_CEC_RX_DATA_1 + i);
  987. msg->msg[i] = val & 0xff;
  988. msg->msg[i + 1] = (val >> 8) & 0xff;
  989. msg->msg[i + 2] = (val >> 16) & 0xff;
  990. msg->msg[i + 3] = (val >> 24) & 0xff;
  991. }
  992. }
  993. static irqreturn_t vc4_cec_irq_handler(int irq, void *priv)
  994. {
  995. struct vc4_dev *vc4 = priv;
  996. struct vc4_hdmi *hdmi = vc4->hdmi;
  997. u32 stat = HDMI_READ(VC4_HDMI_CPU_STATUS);
  998. u32 cntrl1, cntrl5;
  999. if (!(stat & VC4_HDMI_CPU_CEC))
  1000. return IRQ_NONE;
  1001. hdmi->cec_rx_msg.len = 0;
  1002. cntrl1 = HDMI_READ(VC4_HDMI_CEC_CNTRL_1);
  1003. cntrl5 = HDMI_READ(VC4_HDMI_CEC_CNTRL_5);
  1004. hdmi->cec_irq_was_rx = cntrl5 & VC4_HDMI_CEC_RX_CEC_INT;
  1005. if (hdmi->cec_irq_was_rx) {
  1006. vc4_cec_read_msg(vc4, cntrl1);
  1007. cntrl1 |= VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
  1008. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1, cntrl1);
  1009. cntrl1 &= ~VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
  1010. } else {
  1011. hdmi->cec_tx_ok = cntrl1 & VC4_HDMI_CEC_TX_STATUS_GOOD;
  1012. cntrl1 &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
  1013. }
  1014. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1, cntrl1);
  1015. HDMI_WRITE(VC4_HDMI_CPU_CLEAR, VC4_HDMI_CPU_CEC);
  1016. return IRQ_WAKE_THREAD;
  1017. }
  1018. static int vc4_hdmi_cec_adap_enable(struct cec_adapter *adap, bool enable)
  1019. {
  1020. struct vc4_dev *vc4 = cec_get_drvdata(adap);
  1021. /* clock period in microseconds */
  1022. const u32 usecs = 1000000 / CEC_CLOCK_FREQ;
  1023. u32 val = HDMI_READ(VC4_HDMI_CEC_CNTRL_5);
  1024. val &= ~(VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET |
  1025. VC4_HDMI_CEC_CNT_TO_4700_US_MASK |
  1026. VC4_HDMI_CEC_CNT_TO_4500_US_MASK);
  1027. val |= ((4700 / usecs) << VC4_HDMI_CEC_CNT_TO_4700_US_SHIFT) |
  1028. ((4500 / usecs) << VC4_HDMI_CEC_CNT_TO_4500_US_SHIFT);
  1029. if (enable) {
  1030. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_5, val |
  1031. VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
  1032. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_5, val);
  1033. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_2,
  1034. ((1500 / usecs) << VC4_HDMI_CEC_CNT_TO_1500_US_SHIFT) |
  1035. ((1300 / usecs) << VC4_HDMI_CEC_CNT_TO_1300_US_SHIFT) |
  1036. ((800 / usecs) << VC4_HDMI_CEC_CNT_TO_800_US_SHIFT) |
  1037. ((600 / usecs) << VC4_HDMI_CEC_CNT_TO_600_US_SHIFT) |
  1038. ((400 / usecs) << VC4_HDMI_CEC_CNT_TO_400_US_SHIFT));
  1039. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_3,
  1040. ((2750 / usecs) << VC4_HDMI_CEC_CNT_TO_2750_US_SHIFT) |
  1041. ((2400 / usecs) << VC4_HDMI_CEC_CNT_TO_2400_US_SHIFT) |
  1042. ((2050 / usecs) << VC4_HDMI_CEC_CNT_TO_2050_US_SHIFT) |
  1043. ((1700 / usecs) << VC4_HDMI_CEC_CNT_TO_1700_US_SHIFT));
  1044. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_4,
  1045. ((4300 / usecs) << VC4_HDMI_CEC_CNT_TO_4300_US_SHIFT) |
  1046. ((3900 / usecs) << VC4_HDMI_CEC_CNT_TO_3900_US_SHIFT) |
  1047. ((3600 / usecs) << VC4_HDMI_CEC_CNT_TO_3600_US_SHIFT) |
  1048. ((3500 / usecs) << VC4_HDMI_CEC_CNT_TO_3500_US_SHIFT));
  1049. HDMI_WRITE(VC4_HDMI_CPU_MASK_CLEAR, VC4_HDMI_CPU_CEC);
  1050. } else {
  1051. HDMI_WRITE(VC4_HDMI_CPU_MASK_SET, VC4_HDMI_CPU_CEC);
  1052. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_5, val |
  1053. VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
  1054. }
  1055. return 0;
  1056. }
  1057. static int vc4_hdmi_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
  1058. {
  1059. struct vc4_dev *vc4 = cec_get_drvdata(adap);
  1060. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1,
  1061. (HDMI_READ(VC4_HDMI_CEC_CNTRL_1) & ~VC4_HDMI_CEC_ADDR_MASK) |
  1062. (log_addr & 0xf) << VC4_HDMI_CEC_ADDR_SHIFT);
  1063. return 0;
  1064. }
  1065. static int vc4_hdmi_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
  1066. u32 signal_free_time, struct cec_msg *msg)
  1067. {
  1068. struct vc4_dev *vc4 = cec_get_drvdata(adap);
  1069. u32 val;
  1070. unsigned int i;
  1071. for (i = 0; i < msg->len; i += 4)
  1072. HDMI_WRITE(VC4_HDMI_CEC_TX_DATA_1 + i,
  1073. (msg->msg[i]) |
  1074. (msg->msg[i + 1] << 8) |
  1075. (msg->msg[i + 2] << 16) |
  1076. (msg->msg[i + 3] << 24));
  1077. val = HDMI_READ(VC4_HDMI_CEC_CNTRL_1);
  1078. val &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
  1079. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1, val);
  1080. val &= ~VC4_HDMI_CEC_MESSAGE_LENGTH_MASK;
  1081. val |= (msg->len - 1) << VC4_HDMI_CEC_MESSAGE_LENGTH_SHIFT;
  1082. val |= VC4_HDMI_CEC_START_XMIT_BEGIN;
  1083. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1, val);
  1084. return 0;
  1085. }
  1086. static const struct cec_adap_ops vc4_hdmi_cec_adap_ops = {
  1087. .adap_enable = vc4_hdmi_cec_adap_enable,
  1088. .adap_log_addr = vc4_hdmi_cec_adap_log_addr,
  1089. .adap_transmit = vc4_hdmi_cec_adap_transmit,
  1090. };
  1091. #endif
  1092. static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data)
  1093. {
  1094. struct platform_device *pdev = to_platform_device(dev);
  1095. struct drm_device *drm = dev_get_drvdata(master);
  1096. struct vc4_dev *vc4 = drm->dev_private;
  1097. struct vc4_hdmi *hdmi;
  1098. struct vc4_hdmi_encoder *vc4_hdmi_encoder;
  1099. struct device_node *ddc_node;
  1100. u32 value;
  1101. int ret;
  1102. hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
  1103. if (!hdmi)
  1104. return -ENOMEM;
  1105. vc4_hdmi_encoder = devm_kzalloc(dev, sizeof(*vc4_hdmi_encoder),
  1106. GFP_KERNEL);
  1107. if (!vc4_hdmi_encoder)
  1108. return -ENOMEM;
  1109. vc4_hdmi_encoder->base.type = VC4_ENCODER_TYPE_HDMI;
  1110. hdmi->encoder = &vc4_hdmi_encoder->base.base;
  1111. hdmi->pdev = pdev;
  1112. hdmi->hdmicore_regs = vc4_ioremap_regs(pdev, 0);
  1113. if (IS_ERR(hdmi->hdmicore_regs))
  1114. return PTR_ERR(hdmi->hdmicore_regs);
  1115. hdmi->hd_regs = vc4_ioremap_regs(pdev, 1);
  1116. if (IS_ERR(hdmi->hd_regs))
  1117. return PTR_ERR(hdmi->hd_regs);
  1118. hdmi->pixel_clock = devm_clk_get(dev, "pixel");
  1119. if (IS_ERR(hdmi->pixel_clock)) {
  1120. DRM_ERROR("Failed to get pixel clock\n");
  1121. return PTR_ERR(hdmi->pixel_clock);
  1122. }
  1123. hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
  1124. if (IS_ERR(hdmi->hsm_clock)) {
  1125. DRM_ERROR("Failed to get HDMI state machine clock\n");
  1126. return PTR_ERR(hdmi->hsm_clock);
  1127. }
  1128. ddc_node = of_parse_phandle(dev->of_node, "ddc", 0);
  1129. if (!ddc_node) {
  1130. DRM_ERROR("Failed to find ddc node in device tree\n");
  1131. return -ENODEV;
  1132. }
  1133. hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node);
  1134. of_node_put(ddc_node);
  1135. if (!hdmi->ddc) {
  1136. DRM_DEBUG("Failed to get ddc i2c adapter by node\n");
  1137. return -EPROBE_DEFER;
  1138. }
  1139. /* This is the rate that is set by the firmware. The number
  1140. * needs to be a bit higher than the pixel clock rate
  1141. * (generally 148.5Mhz).
  1142. */
  1143. ret = clk_set_rate(hdmi->hsm_clock, HSM_CLOCK_FREQ);
  1144. if (ret) {
  1145. DRM_ERROR("Failed to set HSM clock rate: %d\n", ret);
  1146. goto err_put_i2c;
  1147. }
  1148. ret = clk_prepare_enable(hdmi->hsm_clock);
  1149. if (ret) {
  1150. DRM_ERROR("Failed to turn on HDMI state machine clock: %d\n",
  1151. ret);
  1152. goto err_put_i2c;
  1153. }
  1154. /* Only use the GPIO HPD pin if present in the DT, otherwise
  1155. * we'll use the HDMI core's register.
  1156. */
  1157. if (of_find_property(dev->of_node, "hpd-gpios", &value)) {
  1158. enum of_gpio_flags hpd_gpio_flags;
  1159. hdmi->hpd_gpio = of_get_named_gpio_flags(dev->of_node,
  1160. "hpd-gpios", 0,
  1161. &hpd_gpio_flags);
  1162. if (hdmi->hpd_gpio < 0) {
  1163. ret = hdmi->hpd_gpio;
  1164. goto err_unprepare_hsm;
  1165. }
  1166. hdmi->hpd_active_low = hpd_gpio_flags & OF_GPIO_ACTIVE_LOW;
  1167. }
  1168. vc4->hdmi = hdmi;
  1169. /* HDMI core must be enabled. */
  1170. if (!(HD_READ(VC4_HD_M_CTL) & VC4_HD_M_ENABLE)) {
  1171. HD_WRITE(VC4_HD_M_CTL, VC4_HD_M_SW_RST);
  1172. udelay(1);
  1173. HD_WRITE(VC4_HD_M_CTL, 0);
  1174. HD_WRITE(VC4_HD_M_CTL, VC4_HD_M_ENABLE);
  1175. }
  1176. pm_runtime_enable(dev);
  1177. drm_encoder_init(drm, hdmi->encoder, &vc4_hdmi_encoder_funcs,
  1178. DRM_MODE_ENCODER_TMDS, NULL);
  1179. drm_encoder_helper_add(hdmi->encoder, &vc4_hdmi_encoder_helper_funcs);
  1180. hdmi->connector = vc4_hdmi_connector_init(drm, hdmi->encoder);
  1181. if (IS_ERR(hdmi->connector)) {
  1182. ret = PTR_ERR(hdmi->connector);
  1183. goto err_destroy_encoder;
  1184. }
  1185. #ifdef CONFIG_DRM_VC4_HDMI_CEC
  1186. hdmi->cec_adap = cec_allocate_adapter(&vc4_hdmi_cec_adap_ops,
  1187. vc4, "vc4",
  1188. CEC_CAP_TRANSMIT |
  1189. CEC_CAP_LOG_ADDRS |
  1190. CEC_CAP_PASSTHROUGH |
  1191. CEC_CAP_RC, 1);
  1192. ret = PTR_ERR_OR_ZERO(hdmi->cec_adap);
  1193. if (ret < 0)
  1194. goto err_destroy_conn;
  1195. HDMI_WRITE(VC4_HDMI_CPU_MASK_SET, 0xffffffff);
  1196. value = HDMI_READ(VC4_HDMI_CEC_CNTRL_1);
  1197. value &= ~VC4_HDMI_CEC_DIV_CLK_CNT_MASK;
  1198. /*
  1199. * Set the logical address to Unregistered and set the clock
  1200. * divider: the hsm_clock rate and this divider setting will
  1201. * give a 40 kHz CEC clock.
  1202. */
  1203. value |= VC4_HDMI_CEC_ADDR_MASK |
  1204. (4091 << VC4_HDMI_CEC_DIV_CLK_CNT_SHIFT);
  1205. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1, value);
  1206. ret = devm_request_threaded_irq(dev, platform_get_irq(pdev, 0),
  1207. vc4_cec_irq_handler,
  1208. vc4_cec_irq_handler_thread, 0,
  1209. "vc4 hdmi cec", vc4);
  1210. if (ret)
  1211. goto err_delete_cec_adap;
  1212. ret = cec_register_adapter(hdmi->cec_adap, dev);
  1213. if (ret < 0)
  1214. goto err_delete_cec_adap;
  1215. #endif
  1216. ret = vc4_hdmi_audio_init(hdmi);
  1217. if (ret)
  1218. goto err_destroy_encoder;
  1219. return 0;
  1220. #ifdef CONFIG_DRM_VC4_HDMI_CEC
  1221. err_delete_cec_adap:
  1222. cec_delete_adapter(hdmi->cec_adap);
  1223. err_destroy_conn:
  1224. vc4_hdmi_connector_destroy(hdmi->connector);
  1225. #endif
  1226. err_destroy_encoder:
  1227. vc4_hdmi_encoder_destroy(hdmi->encoder);
  1228. err_unprepare_hsm:
  1229. clk_disable_unprepare(hdmi->hsm_clock);
  1230. pm_runtime_disable(dev);
  1231. err_put_i2c:
  1232. put_device(&hdmi->ddc->dev);
  1233. return ret;
  1234. }
  1235. static void vc4_hdmi_unbind(struct device *dev, struct device *master,
  1236. void *data)
  1237. {
  1238. struct drm_device *drm = dev_get_drvdata(master);
  1239. struct vc4_dev *vc4 = drm->dev_private;
  1240. struct vc4_hdmi *hdmi = vc4->hdmi;
  1241. cec_unregister_adapter(hdmi->cec_adap);
  1242. vc4_hdmi_connector_destroy(hdmi->connector);
  1243. vc4_hdmi_encoder_destroy(hdmi->encoder);
  1244. clk_disable_unprepare(hdmi->hsm_clock);
  1245. pm_runtime_disable(dev);
  1246. put_device(&hdmi->ddc->dev);
  1247. vc4->hdmi = NULL;
  1248. }
  1249. static const struct component_ops vc4_hdmi_ops = {
  1250. .bind = vc4_hdmi_bind,
  1251. .unbind = vc4_hdmi_unbind,
  1252. };
  1253. static int vc4_hdmi_dev_probe(struct platform_device *pdev)
  1254. {
  1255. return component_add(&pdev->dev, &vc4_hdmi_ops);
  1256. }
  1257. static int vc4_hdmi_dev_remove(struct platform_device *pdev)
  1258. {
  1259. component_del(&pdev->dev, &vc4_hdmi_ops);
  1260. return 0;
  1261. }
  1262. static const struct of_device_id vc4_hdmi_dt_match[] = {
  1263. { .compatible = "brcm,bcm2835-hdmi" },
  1264. {}
  1265. };
  1266. struct platform_driver vc4_hdmi_driver = {
  1267. .probe = vc4_hdmi_dev_probe,
  1268. .remove = vc4_hdmi_dev_remove,
  1269. .driver = {
  1270. .name = "vc4_hdmi",
  1271. .of_match_table = vc4_hdmi_dt_match,
  1272. },
  1273. };