vc4_hdmi.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  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_mode_connector_update_edid_property(connector, edid);
  253. ret = drm_add_edid_modes(connector, edid);
  254. drm_edid_to_eld(connector, edid);
  255. kfree(edid);
  256. return ret;
  257. }
  258. static const struct drm_connector_funcs vc4_hdmi_connector_funcs = {
  259. .detect = vc4_hdmi_connector_detect,
  260. .fill_modes = drm_helper_probe_single_connector_modes,
  261. .destroy = vc4_hdmi_connector_destroy,
  262. .reset = drm_atomic_helper_connector_reset,
  263. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  264. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  265. };
  266. static const struct drm_connector_helper_funcs vc4_hdmi_connector_helper_funcs = {
  267. .get_modes = vc4_hdmi_connector_get_modes,
  268. };
  269. static struct drm_connector *vc4_hdmi_connector_init(struct drm_device *dev,
  270. struct drm_encoder *encoder)
  271. {
  272. struct drm_connector *connector = NULL;
  273. struct vc4_hdmi_connector *hdmi_connector;
  274. int ret = 0;
  275. hdmi_connector = devm_kzalloc(dev->dev, sizeof(*hdmi_connector),
  276. GFP_KERNEL);
  277. if (!hdmi_connector) {
  278. ret = -ENOMEM;
  279. goto fail;
  280. }
  281. connector = &hdmi_connector->base;
  282. hdmi_connector->encoder = encoder;
  283. drm_connector_init(dev, connector, &vc4_hdmi_connector_funcs,
  284. DRM_MODE_CONNECTOR_HDMIA);
  285. drm_connector_helper_add(connector, &vc4_hdmi_connector_helper_funcs);
  286. connector->polled = (DRM_CONNECTOR_POLL_CONNECT |
  287. DRM_CONNECTOR_POLL_DISCONNECT);
  288. connector->interlace_allowed = 1;
  289. connector->doublescan_allowed = 0;
  290. drm_mode_connector_attach_encoder(connector, encoder);
  291. return connector;
  292. fail:
  293. if (connector)
  294. vc4_hdmi_connector_destroy(connector);
  295. return ERR_PTR(ret);
  296. }
  297. static void vc4_hdmi_encoder_destroy(struct drm_encoder *encoder)
  298. {
  299. drm_encoder_cleanup(encoder);
  300. }
  301. static const struct drm_encoder_funcs vc4_hdmi_encoder_funcs = {
  302. .destroy = vc4_hdmi_encoder_destroy,
  303. };
  304. static int vc4_hdmi_stop_packet(struct drm_encoder *encoder,
  305. enum hdmi_infoframe_type type)
  306. {
  307. struct drm_device *dev = encoder->dev;
  308. struct vc4_dev *vc4 = to_vc4_dev(dev);
  309. u32 packet_id = type - 0x80;
  310. HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
  311. HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) & ~BIT(packet_id));
  312. return wait_for(!(HDMI_READ(VC4_HDMI_RAM_PACKET_STATUS) &
  313. BIT(packet_id)), 100);
  314. }
  315. static void vc4_hdmi_write_infoframe(struct drm_encoder *encoder,
  316. union hdmi_infoframe *frame)
  317. {
  318. struct drm_device *dev = encoder->dev;
  319. struct vc4_dev *vc4 = to_vc4_dev(dev);
  320. u32 packet_id = frame->any.type - 0x80;
  321. u32 packet_reg = VC4_HDMI_RAM_PACKET(packet_id);
  322. uint8_t buffer[VC4_HDMI_PACKET_STRIDE];
  323. ssize_t len, i;
  324. int ret;
  325. WARN_ONCE(!(HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) &
  326. VC4_HDMI_RAM_PACKET_ENABLE),
  327. "Packet RAM has to be on to store the packet.");
  328. len = hdmi_infoframe_pack(frame, buffer, sizeof(buffer));
  329. if (len < 0)
  330. return;
  331. ret = vc4_hdmi_stop_packet(encoder, frame->any.type);
  332. if (ret) {
  333. DRM_ERROR("Failed to wait for infoframe to go idle: %d\n", ret);
  334. return;
  335. }
  336. for (i = 0; i < len; i += 7) {
  337. HDMI_WRITE(packet_reg,
  338. buffer[i + 0] << 0 |
  339. buffer[i + 1] << 8 |
  340. buffer[i + 2] << 16);
  341. packet_reg += 4;
  342. HDMI_WRITE(packet_reg,
  343. buffer[i + 3] << 0 |
  344. buffer[i + 4] << 8 |
  345. buffer[i + 5] << 16 |
  346. buffer[i + 6] << 24);
  347. packet_reg += 4;
  348. }
  349. HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
  350. HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) | BIT(packet_id));
  351. ret = wait_for((HDMI_READ(VC4_HDMI_RAM_PACKET_STATUS) &
  352. BIT(packet_id)), 100);
  353. if (ret)
  354. DRM_ERROR("Failed to wait for infoframe to start: %d\n", ret);
  355. }
  356. static void vc4_hdmi_set_avi_infoframe(struct drm_encoder *encoder)
  357. {
  358. struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
  359. struct drm_crtc *crtc = encoder->crtc;
  360. const struct drm_display_mode *mode = &crtc->state->adjusted_mode;
  361. union hdmi_infoframe frame;
  362. int ret;
  363. ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi, mode, false);
  364. if (ret < 0) {
  365. DRM_ERROR("couldn't fill AVI infoframe\n");
  366. return;
  367. }
  368. drm_hdmi_avi_infoframe_quant_range(&frame.avi, mode,
  369. vc4_encoder->limited_rgb_range ?
  370. HDMI_QUANTIZATION_RANGE_LIMITED :
  371. HDMI_QUANTIZATION_RANGE_FULL,
  372. vc4_encoder->rgb_range_selectable);
  373. vc4_hdmi_write_infoframe(encoder, &frame);
  374. }
  375. static void vc4_hdmi_set_spd_infoframe(struct drm_encoder *encoder)
  376. {
  377. union hdmi_infoframe frame;
  378. int ret;
  379. ret = hdmi_spd_infoframe_init(&frame.spd, "Broadcom", "Videocore");
  380. if (ret < 0) {
  381. DRM_ERROR("couldn't fill SPD infoframe\n");
  382. return;
  383. }
  384. frame.spd.sdi = HDMI_SPD_SDI_PC;
  385. vc4_hdmi_write_infoframe(encoder, &frame);
  386. }
  387. static void vc4_hdmi_set_audio_infoframe(struct drm_encoder *encoder)
  388. {
  389. struct drm_device *drm = encoder->dev;
  390. struct vc4_dev *vc4 = drm->dev_private;
  391. struct vc4_hdmi *hdmi = vc4->hdmi;
  392. union hdmi_infoframe frame;
  393. int ret;
  394. ret = hdmi_audio_infoframe_init(&frame.audio);
  395. frame.audio.coding_type = HDMI_AUDIO_CODING_TYPE_STREAM;
  396. frame.audio.sample_frequency = HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM;
  397. frame.audio.sample_size = HDMI_AUDIO_SAMPLE_SIZE_STREAM;
  398. frame.audio.channels = hdmi->audio.channels;
  399. vc4_hdmi_write_infoframe(encoder, &frame);
  400. }
  401. static void vc4_hdmi_set_infoframes(struct drm_encoder *encoder)
  402. {
  403. vc4_hdmi_set_avi_infoframe(encoder);
  404. vc4_hdmi_set_spd_infoframe(encoder);
  405. }
  406. static void vc4_hdmi_encoder_disable(struct drm_encoder *encoder)
  407. {
  408. struct drm_device *dev = encoder->dev;
  409. struct vc4_dev *vc4 = to_vc4_dev(dev);
  410. struct vc4_hdmi *hdmi = vc4->hdmi;
  411. int ret;
  412. HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG, 0);
  413. HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL, 0xf << 16);
  414. HD_WRITE(VC4_HD_VID_CTL,
  415. HD_READ(VC4_HD_VID_CTL) & ~VC4_HD_VID_CTL_ENABLE);
  416. clk_disable_unprepare(hdmi->pixel_clock);
  417. ret = pm_runtime_put(&hdmi->pdev->dev);
  418. if (ret < 0)
  419. DRM_ERROR("Failed to release power domain: %d\n", ret);
  420. }
  421. static void vc4_hdmi_encoder_enable(struct drm_encoder *encoder)
  422. {
  423. struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
  424. struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
  425. struct drm_device *dev = encoder->dev;
  426. struct vc4_dev *vc4 = to_vc4_dev(dev);
  427. struct vc4_hdmi *hdmi = vc4->hdmi;
  428. bool debug_dump_regs = false;
  429. bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
  430. bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
  431. bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
  432. u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
  433. u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
  434. VC4_HDMI_VERTA_VSP) |
  435. VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
  436. VC4_HDMI_VERTA_VFP) |
  437. VC4_SET_FIELD(mode->crtc_vdisplay, VC4_HDMI_VERTA_VAL));
  438. u32 vertb = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
  439. VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end,
  440. VC4_HDMI_VERTB_VBP));
  441. u32 vertb_even = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
  442. VC4_SET_FIELD(mode->crtc_vtotal -
  443. mode->crtc_vsync_end -
  444. interlaced,
  445. VC4_HDMI_VERTB_VBP));
  446. u32 csc_ctl;
  447. int ret;
  448. ret = pm_runtime_get_sync(&hdmi->pdev->dev);
  449. if (ret < 0) {
  450. DRM_ERROR("Failed to retain power domain: %d\n", ret);
  451. return;
  452. }
  453. ret = clk_set_rate(hdmi->pixel_clock,
  454. mode->clock * 1000 *
  455. ((mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1));
  456. if (ret) {
  457. DRM_ERROR("Failed to set pixel clock rate: %d\n", ret);
  458. return;
  459. }
  460. ret = clk_prepare_enable(hdmi->pixel_clock);
  461. if (ret) {
  462. DRM_ERROR("Failed to turn on pixel clock: %d\n", ret);
  463. return;
  464. }
  465. HDMI_WRITE(VC4_HDMI_SW_RESET_CONTROL,
  466. VC4_HDMI_SW_RESET_HDMI |
  467. VC4_HDMI_SW_RESET_FORMAT_DETECT);
  468. HDMI_WRITE(VC4_HDMI_SW_RESET_CONTROL, 0);
  469. /* PHY should be in reset, like
  470. * vc4_hdmi_encoder_disable() does.
  471. */
  472. HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL, 0xf << 16);
  473. HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL, 0);
  474. if (debug_dump_regs) {
  475. DRM_INFO("HDMI regs before:\n");
  476. vc4_hdmi_dump_regs(dev);
  477. }
  478. HD_WRITE(VC4_HD_VID_CTL, 0);
  479. HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
  480. HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
  481. VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT |
  482. VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS);
  483. HDMI_WRITE(VC4_HDMI_HORZA,
  484. (vsync_pos ? VC4_HDMI_HORZA_VPOS : 0) |
  485. (hsync_pos ? VC4_HDMI_HORZA_HPOS : 0) |
  486. VC4_SET_FIELD(mode->hdisplay * pixel_rep,
  487. VC4_HDMI_HORZA_HAP));
  488. HDMI_WRITE(VC4_HDMI_HORZB,
  489. VC4_SET_FIELD((mode->htotal -
  490. mode->hsync_end) * pixel_rep,
  491. VC4_HDMI_HORZB_HBP) |
  492. VC4_SET_FIELD((mode->hsync_end -
  493. mode->hsync_start) * pixel_rep,
  494. VC4_HDMI_HORZB_HSP) |
  495. VC4_SET_FIELD((mode->hsync_start -
  496. mode->hdisplay) * pixel_rep,
  497. VC4_HDMI_HORZB_HFP));
  498. HDMI_WRITE(VC4_HDMI_VERTA0, verta);
  499. HDMI_WRITE(VC4_HDMI_VERTA1, verta);
  500. HDMI_WRITE(VC4_HDMI_VERTB0, vertb_even);
  501. HDMI_WRITE(VC4_HDMI_VERTB1, vertb);
  502. HD_WRITE(VC4_HD_VID_CTL,
  503. (vsync_pos ? 0 : VC4_HD_VID_CTL_VSYNC_LOW) |
  504. (hsync_pos ? 0 : VC4_HD_VID_CTL_HSYNC_LOW));
  505. csc_ctl = VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR,
  506. VC4_HD_CSC_CTL_ORDER);
  507. if (vc4_encoder->hdmi_monitor &&
  508. drm_default_rgb_quant_range(mode) ==
  509. HDMI_QUANTIZATION_RANGE_LIMITED) {
  510. /* CEA VICs other than #1 requre limited range RGB
  511. * output unless overridden by an AVI infoframe.
  512. * Apply a colorspace conversion to squash 0-255 down
  513. * to 16-235. The matrix here is:
  514. *
  515. * [ 0 0 0.8594 16]
  516. * [ 0 0.8594 0 16]
  517. * [ 0.8594 0 0 16]
  518. * [ 0 0 0 1]
  519. */
  520. csc_ctl |= VC4_HD_CSC_CTL_ENABLE;
  521. csc_ctl |= VC4_HD_CSC_CTL_RGB2YCC;
  522. csc_ctl |= VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
  523. VC4_HD_CSC_CTL_MODE);
  524. HD_WRITE(VC4_HD_CSC_12_11, (0x000 << 16) | 0x000);
  525. HD_WRITE(VC4_HD_CSC_14_13, (0x100 << 16) | 0x6e0);
  526. HD_WRITE(VC4_HD_CSC_22_21, (0x6e0 << 16) | 0x000);
  527. HD_WRITE(VC4_HD_CSC_24_23, (0x100 << 16) | 0x000);
  528. HD_WRITE(VC4_HD_CSC_32_31, (0x000 << 16) | 0x6e0);
  529. HD_WRITE(VC4_HD_CSC_34_33, (0x100 << 16) | 0x000);
  530. vc4_encoder->limited_rgb_range = true;
  531. } else {
  532. vc4_encoder->limited_rgb_range = false;
  533. }
  534. /* The RGB order applies even when CSC is disabled. */
  535. HD_WRITE(VC4_HD_CSC_CTL, csc_ctl);
  536. HDMI_WRITE(VC4_HDMI_FIFO_CTL, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N);
  537. if (debug_dump_regs) {
  538. DRM_INFO("HDMI regs after:\n");
  539. vc4_hdmi_dump_regs(dev);
  540. }
  541. HD_WRITE(VC4_HD_VID_CTL,
  542. HD_READ(VC4_HD_VID_CTL) |
  543. VC4_HD_VID_CTL_ENABLE |
  544. VC4_HD_VID_CTL_UNDERFLOW_ENABLE |
  545. VC4_HD_VID_CTL_FRAME_COUNTER_RESET);
  546. if (vc4_encoder->hdmi_monitor) {
  547. HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
  548. HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
  549. VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
  550. ret = wait_for(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
  551. VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE, 1000);
  552. WARN_ONCE(ret, "Timeout waiting for "
  553. "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
  554. } else {
  555. HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
  556. HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) &
  557. ~(VC4_HDMI_RAM_PACKET_ENABLE));
  558. HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
  559. HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
  560. ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
  561. ret = wait_for(!(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
  562. VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE), 1000);
  563. WARN_ONCE(ret, "Timeout waiting for "
  564. "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
  565. }
  566. if (vc4_encoder->hdmi_monitor) {
  567. u32 drift;
  568. WARN_ON(!(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
  569. VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE));
  570. HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
  571. HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
  572. VC4_HDMI_SCHEDULER_CONTROL_VERT_ALWAYS_KEEPOUT);
  573. HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
  574. VC4_HDMI_RAM_PACKET_ENABLE);
  575. vc4_hdmi_set_infoframes(encoder);
  576. drift = HDMI_READ(VC4_HDMI_FIFO_CTL);
  577. drift &= VC4_HDMI_FIFO_VALID_WRITE_MASK;
  578. HDMI_WRITE(VC4_HDMI_FIFO_CTL,
  579. drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
  580. HDMI_WRITE(VC4_HDMI_FIFO_CTL,
  581. drift | VC4_HDMI_FIFO_CTL_RECENTER);
  582. udelay(1000);
  583. HDMI_WRITE(VC4_HDMI_FIFO_CTL,
  584. drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
  585. HDMI_WRITE(VC4_HDMI_FIFO_CTL,
  586. drift | VC4_HDMI_FIFO_CTL_RECENTER);
  587. ret = wait_for(HDMI_READ(VC4_HDMI_FIFO_CTL) &
  588. VC4_HDMI_FIFO_CTL_RECENTER_DONE, 1);
  589. WARN_ONCE(ret, "Timeout waiting for "
  590. "VC4_HDMI_FIFO_CTL_RECENTER_DONE");
  591. }
  592. }
  593. static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs = {
  594. .disable = vc4_hdmi_encoder_disable,
  595. .enable = vc4_hdmi_encoder_enable,
  596. };
  597. /* HDMI audio codec callbacks */
  598. static void vc4_hdmi_audio_set_mai_clock(struct vc4_hdmi *hdmi)
  599. {
  600. struct drm_device *drm = hdmi->encoder->dev;
  601. struct vc4_dev *vc4 = to_vc4_dev(drm);
  602. u32 hsm_clock = clk_get_rate(hdmi->hsm_clock);
  603. unsigned long n, m;
  604. rational_best_approximation(hsm_clock, hdmi->audio.samplerate,
  605. VC4_HD_MAI_SMP_N_MASK >>
  606. VC4_HD_MAI_SMP_N_SHIFT,
  607. (VC4_HD_MAI_SMP_M_MASK >>
  608. VC4_HD_MAI_SMP_M_SHIFT) + 1,
  609. &n, &m);
  610. HD_WRITE(VC4_HD_MAI_SMP,
  611. VC4_SET_FIELD(n, VC4_HD_MAI_SMP_N) |
  612. VC4_SET_FIELD(m - 1, VC4_HD_MAI_SMP_M));
  613. }
  614. static void vc4_hdmi_set_n_cts(struct vc4_hdmi *hdmi)
  615. {
  616. struct drm_encoder *encoder = hdmi->encoder;
  617. struct drm_crtc *crtc = encoder->crtc;
  618. struct drm_device *drm = encoder->dev;
  619. struct vc4_dev *vc4 = to_vc4_dev(drm);
  620. const struct drm_display_mode *mode = &crtc->state->adjusted_mode;
  621. u32 samplerate = hdmi->audio.samplerate;
  622. u32 n, cts;
  623. u64 tmp;
  624. n = 128 * samplerate / 1000;
  625. tmp = (u64)(mode->clock * 1000) * n;
  626. do_div(tmp, 128 * samplerate);
  627. cts = tmp;
  628. HDMI_WRITE(VC4_HDMI_CRP_CFG,
  629. VC4_HDMI_CRP_CFG_EXTERNAL_CTS_EN |
  630. VC4_SET_FIELD(n, VC4_HDMI_CRP_CFG_N));
  631. /*
  632. * We could get slightly more accurate clocks in some cases by
  633. * providing a CTS_1 value. The two CTS values are alternated
  634. * between based on the period fields
  635. */
  636. HDMI_WRITE(VC4_HDMI_CTS_0, cts);
  637. HDMI_WRITE(VC4_HDMI_CTS_1, cts);
  638. }
  639. static inline struct vc4_hdmi *dai_to_hdmi(struct snd_soc_dai *dai)
  640. {
  641. struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai);
  642. return snd_soc_card_get_drvdata(card);
  643. }
  644. static int vc4_hdmi_audio_startup(struct snd_pcm_substream *substream,
  645. struct snd_soc_dai *dai)
  646. {
  647. struct vc4_hdmi *hdmi = dai_to_hdmi(dai);
  648. struct drm_encoder *encoder = hdmi->encoder;
  649. struct vc4_dev *vc4 = to_vc4_dev(encoder->dev);
  650. int ret;
  651. if (hdmi->audio.substream && hdmi->audio.substream != substream)
  652. return -EINVAL;
  653. hdmi->audio.substream = substream;
  654. /*
  655. * If the HDMI encoder hasn't probed, or the encoder is
  656. * currently in DVI mode, treat the codec dai as missing.
  657. */
  658. if (!encoder->crtc || !(HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) &
  659. VC4_HDMI_RAM_PACKET_ENABLE))
  660. return -ENODEV;
  661. ret = snd_pcm_hw_constraint_eld(substream->runtime,
  662. hdmi->connector->eld);
  663. if (ret)
  664. return ret;
  665. return 0;
  666. }
  667. static int vc4_hdmi_audio_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  668. {
  669. return 0;
  670. }
  671. static void vc4_hdmi_audio_reset(struct vc4_hdmi *hdmi)
  672. {
  673. struct drm_encoder *encoder = hdmi->encoder;
  674. struct drm_device *drm = encoder->dev;
  675. struct device *dev = &hdmi->pdev->dev;
  676. struct vc4_dev *vc4 = to_vc4_dev(drm);
  677. int ret;
  678. ret = vc4_hdmi_stop_packet(encoder, HDMI_INFOFRAME_TYPE_AUDIO);
  679. if (ret)
  680. dev_err(dev, "Failed to stop audio infoframe: %d\n", ret);
  681. HD_WRITE(VC4_HD_MAI_CTL, VC4_HD_MAI_CTL_RESET);
  682. HD_WRITE(VC4_HD_MAI_CTL, VC4_HD_MAI_CTL_ERRORF);
  683. HD_WRITE(VC4_HD_MAI_CTL, VC4_HD_MAI_CTL_FLUSH);
  684. }
  685. static void vc4_hdmi_audio_shutdown(struct snd_pcm_substream *substream,
  686. struct snd_soc_dai *dai)
  687. {
  688. struct vc4_hdmi *hdmi = dai_to_hdmi(dai);
  689. if (substream != hdmi->audio.substream)
  690. return;
  691. vc4_hdmi_audio_reset(hdmi);
  692. hdmi->audio.substream = NULL;
  693. }
  694. /* HDMI audio codec callbacks */
  695. static int vc4_hdmi_audio_hw_params(struct snd_pcm_substream *substream,
  696. struct snd_pcm_hw_params *params,
  697. struct snd_soc_dai *dai)
  698. {
  699. struct vc4_hdmi *hdmi = dai_to_hdmi(dai);
  700. struct drm_encoder *encoder = hdmi->encoder;
  701. struct drm_device *drm = encoder->dev;
  702. struct device *dev = &hdmi->pdev->dev;
  703. struct vc4_dev *vc4 = to_vc4_dev(drm);
  704. u32 audio_packet_config, channel_mask;
  705. u32 channel_map, i;
  706. if (substream != hdmi->audio.substream)
  707. return -EINVAL;
  708. dev_dbg(dev, "%s: %u Hz, %d bit, %d channels\n", __func__,
  709. params_rate(params), params_width(params),
  710. params_channels(params));
  711. hdmi->audio.channels = params_channels(params);
  712. hdmi->audio.samplerate = params_rate(params);
  713. HD_WRITE(VC4_HD_MAI_CTL,
  714. VC4_HD_MAI_CTL_RESET |
  715. VC4_HD_MAI_CTL_FLUSH |
  716. VC4_HD_MAI_CTL_DLATE |
  717. VC4_HD_MAI_CTL_ERRORE |
  718. VC4_HD_MAI_CTL_ERRORF);
  719. vc4_hdmi_audio_set_mai_clock(hdmi);
  720. audio_packet_config =
  721. VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_SAMPLE_FLAT |
  722. VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_INACTIVE_CHANNELS |
  723. VC4_SET_FIELD(0xf, VC4_HDMI_AUDIO_PACKET_B_FRAME_IDENTIFIER);
  724. channel_mask = GENMASK(hdmi->audio.channels - 1, 0);
  725. audio_packet_config |= VC4_SET_FIELD(channel_mask,
  726. VC4_HDMI_AUDIO_PACKET_CEA_MASK);
  727. /* Set the MAI threshold. This logic mimics the firmware's. */
  728. if (hdmi->audio.samplerate > 96000) {
  729. HD_WRITE(VC4_HD_MAI_THR,
  730. VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQHIGH) |
  731. VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQLOW));
  732. } else if (hdmi->audio.samplerate > 48000) {
  733. HD_WRITE(VC4_HD_MAI_THR,
  734. VC4_SET_FIELD(0x14, VC4_HD_MAI_THR_DREQHIGH) |
  735. VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQLOW));
  736. } else {
  737. HD_WRITE(VC4_HD_MAI_THR,
  738. VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICHIGH) |
  739. VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICLOW) |
  740. VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_DREQHIGH) |
  741. VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_DREQLOW));
  742. }
  743. HDMI_WRITE(VC4_HDMI_MAI_CONFIG,
  744. VC4_HDMI_MAI_CONFIG_BIT_REVERSE |
  745. VC4_SET_FIELD(channel_mask, VC4_HDMI_MAI_CHANNEL_MASK));
  746. channel_map = 0;
  747. for (i = 0; i < 8; i++) {
  748. if (channel_mask & BIT(i))
  749. channel_map |= i << (3 * i);
  750. }
  751. HDMI_WRITE(VC4_HDMI_MAI_CHANNEL_MAP, channel_map);
  752. HDMI_WRITE(VC4_HDMI_AUDIO_PACKET_CONFIG, audio_packet_config);
  753. vc4_hdmi_set_n_cts(hdmi);
  754. return 0;
  755. }
  756. static int vc4_hdmi_audio_trigger(struct snd_pcm_substream *substream, int cmd,
  757. struct snd_soc_dai *dai)
  758. {
  759. struct vc4_hdmi *hdmi = dai_to_hdmi(dai);
  760. struct drm_encoder *encoder = hdmi->encoder;
  761. struct drm_device *drm = encoder->dev;
  762. struct vc4_dev *vc4 = to_vc4_dev(drm);
  763. switch (cmd) {
  764. case SNDRV_PCM_TRIGGER_START:
  765. vc4_hdmi_set_audio_infoframe(encoder);
  766. HDMI_WRITE(VC4_HDMI_TX_PHY_CTL0,
  767. HDMI_READ(VC4_HDMI_TX_PHY_CTL0) &
  768. ~VC4_HDMI_TX_PHY_RNG_PWRDN);
  769. HD_WRITE(VC4_HD_MAI_CTL,
  770. VC4_SET_FIELD(hdmi->audio.channels,
  771. VC4_HD_MAI_CTL_CHNUM) |
  772. VC4_HD_MAI_CTL_ENABLE);
  773. break;
  774. case SNDRV_PCM_TRIGGER_STOP:
  775. HD_WRITE(VC4_HD_MAI_CTL,
  776. VC4_HD_MAI_CTL_DLATE |
  777. VC4_HD_MAI_CTL_ERRORE |
  778. VC4_HD_MAI_CTL_ERRORF);
  779. HDMI_WRITE(VC4_HDMI_TX_PHY_CTL0,
  780. HDMI_READ(VC4_HDMI_TX_PHY_CTL0) |
  781. VC4_HDMI_TX_PHY_RNG_PWRDN);
  782. break;
  783. default:
  784. break;
  785. }
  786. return 0;
  787. }
  788. static inline struct vc4_hdmi *
  789. snd_component_to_hdmi(struct snd_soc_component *component)
  790. {
  791. struct snd_soc_card *card = snd_soc_component_get_drvdata(component);
  792. return snd_soc_card_get_drvdata(card);
  793. }
  794. static int vc4_hdmi_audio_eld_ctl_info(struct snd_kcontrol *kcontrol,
  795. struct snd_ctl_elem_info *uinfo)
  796. {
  797. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  798. struct vc4_hdmi *hdmi = snd_component_to_hdmi(component);
  799. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  800. uinfo->count = sizeof(hdmi->connector->eld);
  801. return 0;
  802. }
  803. static int vc4_hdmi_audio_eld_ctl_get(struct snd_kcontrol *kcontrol,
  804. struct snd_ctl_elem_value *ucontrol)
  805. {
  806. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  807. struct vc4_hdmi *hdmi = snd_component_to_hdmi(component);
  808. memcpy(ucontrol->value.bytes.data, hdmi->connector->eld,
  809. sizeof(hdmi->connector->eld));
  810. return 0;
  811. }
  812. static const struct snd_kcontrol_new vc4_hdmi_audio_controls[] = {
  813. {
  814. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  815. SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  816. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  817. .name = "ELD",
  818. .info = vc4_hdmi_audio_eld_ctl_info,
  819. .get = vc4_hdmi_audio_eld_ctl_get,
  820. },
  821. };
  822. static const struct snd_soc_dapm_widget vc4_hdmi_audio_widgets[] = {
  823. SND_SOC_DAPM_OUTPUT("TX"),
  824. };
  825. static const struct snd_soc_dapm_route vc4_hdmi_audio_routes[] = {
  826. { "TX", NULL, "Playback" },
  827. };
  828. static const struct snd_soc_codec_driver vc4_hdmi_audio_codec_drv = {
  829. .component_driver = {
  830. .controls = vc4_hdmi_audio_controls,
  831. .num_controls = ARRAY_SIZE(vc4_hdmi_audio_controls),
  832. .dapm_widgets = vc4_hdmi_audio_widgets,
  833. .num_dapm_widgets = ARRAY_SIZE(vc4_hdmi_audio_widgets),
  834. .dapm_routes = vc4_hdmi_audio_routes,
  835. .num_dapm_routes = ARRAY_SIZE(vc4_hdmi_audio_routes),
  836. },
  837. };
  838. static const struct snd_soc_dai_ops vc4_hdmi_audio_dai_ops = {
  839. .startup = vc4_hdmi_audio_startup,
  840. .shutdown = vc4_hdmi_audio_shutdown,
  841. .hw_params = vc4_hdmi_audio_hw_params,
  842. .set_fmt = vc4_hdmi_audio_set_fmt,
  843. .trigger = vc4_hdmi_audio_trigger,
  844. };
  845. static struct snd_soc_dai_driver vc4_hdmi_audio_codec_dai_drv = {
  846. .name = "vc4-hdmi-hifi",
  847. .playback = {
  848. .stream_name = "Playback",
  849. .channels_min = 2,
  850. .channels_max = 8,
  851. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
  852. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
  853. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
  854. SNDRV_PCM_RATE_192000,
  855. .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
  856. },
  857. };
  858. static const struct snd_soc_component_driver vc4_hdmi_audio_cpu_dai_comp = {
  859. .name = "vc4-hdmi-cpu-dai-component",
  860. };
  861. static int vc4_hdmi_audio_cpu_dai_probe(struct snd_soc_dai *dai)
  862. {
  863. struct vc4_hdmi *hdmi = dai_to_hdmi(dai);
  864. snd_soc_dai_init_dma_data(dai, &hdmi->audio.dma_data, NULL);
  865. return 0;
  866. }
  867. static struct snd_soc_dai_driver vc4_hdmi_audio_cpu_dai_drv = {
  868. .name = "vc4-hdmi-cpu-dai",
  869. .probe = vc4_hdmi_audio_cpu_dai_probe,
  870. .playback = {
  871. .stream_name = "Playback",
  872. .channels_min = 1,
  873. .channels_max = 8,
  874. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
  875. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
  876. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
  877. SNDRV_PCM_RATE_192000,
  878. .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
  879. },
  880. .ops = &vc4_hdmi_audio_dai_ops,
  881. };
  882. static const struct snd_dmaengine_pcm_config pcm_conf = {
  883. .chan_names[SNDRV_PCM_STREAM_PLAYBACK] = "audio-rx",
  884. .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
  885. };
  886. static int vc4_hdmi_audio_init(struct vc4_hdmi *hdmi)
  887. {
  888. struct snd_soc_dai_link *dai_link = &hdmi->audio.link;
  889. struct snd_soc_card *card = &hdmi->audio.card;
  890. struct device *dev = &hdmi->pdev->dev;
  891. const __be32 *addr;
  892. int ret;
  893. if (!of_find_property(dev->of_node, "dmas", NULL)) {
  894. dev_warn(dev,
  895. "'dmas' DT property is missing, no HDMI audio\n");
  896. return 0;
  897. }
  898. /*
  899. * Get the physical address of VC4_HD_MAI_DATA. We need to retrieve
  900. * the bus address specified in the DT, because the physical address
  901. * (the one returned by platform_get_resource()) is not appropriate
  902. * for DMA transfers.
  903. * This VC/MMU should probably be exposed to avoid this kind of hacks.
  904. */
  905. addr = of_get_address(dev->of_node, 1, NULL, NULL);
  906. hdmi->audio.dma_data.addr = be32_to_cpup(addr) + VC4_HD_MAI_DATA;
  907. hdmi->audio.dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  908. hdmi->audio.dma_data.maxburst = 2;
  909. ret = devm_snd_dmaengine_pcm_register(dev, &pcm_conf, 0);
  910. if (ret) {
  911. dev_err(dev, "Could not register PCM component: %d\n", ret);
  912. return ret;
  913. }
  914. ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_cpu_dai_comp,
  915. &vc4_hdmi_audio_cpu_dai_drv, 1);
  916. if (ret) {
  917. dev_err(dev, "Could not register CPU DAI: %d\n", ret);
  918. return ret;
  919. }
  920. /* register codec and codec dai */
  921. ret = snd_soc_register_codec(dev, &vc4_hdmi_audio_codec_drv,
  922. &vc4_hdmi_audio_codec_dai_drv, 1);
  923. if (ret) {
  924. dev_err(dev, "Could not register codec: %d\n", ret);
  925. return ret;
  926. }
  927. dai_link->name = "MAI";
  928. dai_link->stream_name = "MAI PCM";
  929. dai_link->codec_dai_name = vc4_hdmi_audio_codec_dai_drv.name;
  930. dai_link->cpu_dai_name = dev_name(dev);
  931. dai_link->codec_name = dev_name(dev);
  932. dai_link->platform_name = dev_name(dev);
  933. card->dai_link = dai_link;
  934. card->num_links = 1;
  935. card->name = "vc4-hdmi";
  936. card->dev = dev;
  937. /*
  938. * Be careful, snd_soc_register_card() calls dev_set_drvdata() and
  939. * stores a pointer to the snd card object in dev->driver_data. This
  940. * means we cannot use it for something else. The hdmi back-pointer is
  941. * now stored in card->drvdata and should be retrieved with
  942. * snd_soc_card_get_drvdata() if needed.
  943. */
  944. snd_soc_card_set_drvdata(card, hdmi);
  945. ret = devm_snd_soc_register_card(dev, card);
  946. if (ret) {
  947. dev_err(dev, "Could not register sound card: %d\n", ret);
  948. goto unregister_codec;
  949. }
  950. return 0;
  951. unregister_codec:
  952. snd_soc_unregister_codec(dev);
  953. return ret;
  954. }
  955. static void vc4_hdmi_audio_cleanup(struct vc4_hdmi *hdmi)
  956. {
  957. struct device *dev = &hdmi->pdev->dev;
  958. /*
  959. * If drvdata is not set this means the audio card was not
  960. * registered, just skip codec unregistration in this case.
  961. */
  962. if (dev_get_drvdata(dev))
  963. snd_soc_unregister_codec(dev);
  964. }
  965. #ifdef CONFIG_DRM_VC4_HDMI_CEC
  966. static irqreturn_t vc4_cec_irq_handler_thread(int irq, void *priv)
  967. {
  968. struct vc4_dev *vc4 = priv;
  969. struct vc4_hdmi *hdmi = vc4->hdmi;
  970. if (hdmi->cec_irq_was_rx) {
  971. if (hdmi->cec_rx_msg.len)
  972. cec_received_msg(hdmi->cec_adap, &hdmi->cec_rx_msg);
  973. } else if (hdmi->cec_tx_ok) {
  974. cec_transmit_done(hdmi->cec_adap, CEC_TX_STATUS_OK,
  975. 0, 0, 0, 0);
  976. } else {
  977. /*
  978. * This CEC implementation makes 1 retry, so if we
  979. * get a NACK, then that means it made 2 attempts.
  980. */
  981. cec_transmit_done(hdmi->cec_adap, CEC_TX_STATUS_NACK,
  982. 0, 2, 0, 0);
  983. }
  984. return IRQ_HANDLED;
  985. }
  986. static void vc4_cec_read_msg(struct vc4_dev *vc4, u32 cntrl1)
  987. {
  988. struct cec_msg *msg = &vc4->hdmi->cec_rx_msg;
  989. unsigned int i;
  990. msg->len = 1 + ((cntrl1 & VC4_HDMI_CEC_REC_WRD_CNT_MASK) >>
  991. VC4_HDMI_CEC_REC_WRD_CNT_SHIFT);
  992. for (i = 0; i < msg->len; i += 4) {
  993. u32 val = HDMI_READ(VC4_HDMI_CEC_RX_DATA_1 + i);
  994. msg->msg[i] = val & 0xff;
  995. msg->msg[i + 1] = (val >> 8) & 0xff;
  996. msg->msg[i + 2] = (val >> 16) & 0xff;
  997. msg->msg[i + 3] = (val >> 24) & 0xff;
  998. }
  999. }
  1000. static irqreturn_t vc4_cec_irq_handler(int irq, void *priv)
  1001. {
  1002. struct vc4_dev *vc4 = priv;
  1003. struct vc4_hdmi *hdmi = vc4->hdmi;
  1004. u32 stat = HDMI_READ(VC4_HDMI_CPU_STATUS);
  1005. u32 cntrl1, cntrl5;
  1006. if (!(stat & VC4_HDMI_CPU_CEC))
  1007. return IRQ_NONE;
  1008. hdmi->cec_rx_msg.len = 0;
  1009. cntrl1 = HDMI_READ(VC4_HDMI_CEC_CNTRL_1);
  1010. cntrl5 = HDMI_READ(VC4_HDMI_CEC_CNTRL_5);
  1011. hdmi->cec_irq_was_rx = cntrl5 & VC4_HDMI_CEC_RX_CEC_INT;
  1012. if (hdmi->cec_irq_was_rx) {
  1013. vc4_cec_read_msg(vc4, cntrl1);
  1014. cntrl1 |= VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
  1015. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1, cntrl1);
  1016. cntrl1 &= ~VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
  1017. } else {
  1018. hdmi->cec_tx_ok = cntrl1 & VC4_HDMI_CEC_TX_STATUS_GOOD;
  1019. cntrl1 &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
  1020. }
  1021. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1, cntrl1);
  1022. HDMI_WRITE(VC4_HDMI_CPU_CLEAR, VC4_HDMI_CPU_CEC);
  1023. return IRQ_WAKE_THREAD;
  1024. }
  1025. static int vc4_hdmi_cec_adap_enable(struct cec_adapter *adap, bool enable)
  1026. {
  1027. struct vc4_dev *vc4 = cec_get_drvdata(adap);
  1028. /* clock period in microseconds */
  1029. const u32 usecs = 1000000 / CEC_CLOCK_FREQ;
  1030. u32 val = HDMI_READ(VC4_HDMI_CEC_CNTRL_5);
  1031. val &= ~(VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET |
  1032. VC4_HDMI_CEC_CNT_TO_4700_US_MASK |
  1033. VC4_HDMI_CEC_CNT_TO_4500_US_MASK);
  1034. val |= ((4700 / usecs) << VC4_HDMI_CEC_CNT_TO_4700_US_SHIFT) |
  1035. ((4500 / usecs) << VC4_HDMI_CEC_CNT_TO_4500_US_SHIFT);
  1036. if (enable) {
  1037. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_5, val |
  1038. VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
  1039. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_5, val);
  1040. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_2,
  1041. ((1500 / usecs) << VC4_HDMI_CEC_CNT_TO_1500_US_SHIFT) |
  1042. ((1300 / usecs) << VC4_HDMI_CEC_CNT_TO_1300_US_SHIFT) |
  1043. ((800 / usecs) << VC4_HDMI_CEC_CNT_TO_800_US_SHIFT) |
  1044. ((600 / usecs) << VC4_HDMI_CEC_CNT_TO_600_US_SHIFT) |
  1045. ((400 / usecs) << VC4_HDMI_CEC_CNT_TO_400_US_SHIFT));
  1046. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_3,
  1047. ((2750 / usecs) << VC4_HDMI_CEC_CNT_TO_2750_US_SHIFT) |
  1048. ((2400 / usecs) << VC4_HDMI_CEC_CNT_TO_2400_US_SHIFT) |
  1049. ((2050 / usecs) << VC4_HDMI_CEC_CNT_TO_2050_US_SHIFT) |
  1050. ((1700 / usecs) << VC4_HDMI_CEC_CNT_TO_1700_US_SHIFT));
  1051. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_4,
  1052. ((4300 / usecs) << VC4_HDMI_CEC_CNT_TO_4300_US_SHIFT) |
  1053. ((3900 / usecs) << VC4_HDMI_CEC_CNT_TO_3900_US_SHIFT) |
  1054. ((3600 / usecs) << VC4_HDMI_CEC_CNT_TO_3600_US_SHIFT) |
  1055. ((3500 / usecs) << VC4_HDMI_CEC_CNT_TO_3500_US_SHIFT));
  1056. HDMI_WRITE(VC4_HDMI_CPU_MASK_CLEAR, VC4_HDMI_CPU_CEC);
  1057. } else {
  1058. HDMI_WRITE(VC4_HDMI_CPU_MASK_SET, VC4_HDMI_CPU_CEC);
  1059. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_5, val |
  1060. VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
  1061. }
  1062. return 0;
  1063. }
  1064. static int vc4_hdmi_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
  1065. {
  1066. struct vc4_dev *vc4 = cec_get_drvdata(adap);
  1067. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1,
  1068. (HDMI_READ(VC4_HDMI_CEC_CNTRL_1) & ~VC4_HDMI_CEC_ADDR_MASK) |
  1069. (log_addr & 0xf) << VC4_HDMI_CEC_ADDR_SHIFT);
  1070. return 0;
  1071. }
  1072. static int vc4_hdmi_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
  1073. u32 signal_free_time, struct cec_msg *msg)
  1074. {
  1075. struct vc4_dev *vc4 = cec_get_drvdata(adap);
  1076. u32 val;
  1077. unsigned int i;
  1078. for (i = 0; i < msg->len; i += 4)
  1079. HDMI_WRITE(VC4_HDMI_CEC_TX_DATA_1 + i,
  1080. (msg->msg[i]) |
  1081. (msg->msg[i + 1] << 8) |
  1082. (msg->msg[i + 2] << 16) |
  1083. (msg->msg[i + 3] << 24));
  1084. val = HDMI_READ(VC4_HDMI_CEC_CNTRL_1);
  1085. val &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
  1086. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1, val);
  1087. val &= ~VC4_HDMI_CEC_MESSAGE_LENGTH_MASK;
  1088. val |= (msg->len - 1) << VC4_HDMI_CEC_MESSAGE_LENGTH_SHIFT;
  1089. val |= VC4_HDMI_CEC_START_XMIT_BEGIN;
  1090. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1, val);
  1091. return 0;
  1092. }
  1093. static const struct cec_adap_ops vc4_hdmi_cec_adap_ops = {
  1094. .adap_enable = vc4_hdmi_cec_adap_enable,
  1095. .adap_log_addr = vc4_hdmi_cec_adap_log_addr,
  1096. .adap_transmit = vc4_hdmi_cec_adap_transmit,
  1097. };
  1098. #endif
  1099. static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data)
  1100. {
  1101. struct platform_device *pdev = to_platform_device(dev);
  1102. struct drm_device *drm = dev_get_drvdata(master);
  1103. struct vc4_dev *vc4 = drm->dev_private;
  1104. struct vc4_hdmi *hdmi;
  1105. struct vc4_hdmi_encoder *vc4_hdmi_encoder;
  1106. struct device_node *ddc_node;
  1107. u32 value;
  1108. int ret;
  1109. hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
  1110. if (!hdmi)
  1111. return -ENOMEM;
  1112. vc4_hdmi_encoder = devm_kzalloc(dev, sizeof(*vc4_hdmi_encoder),
  1113. GFP_KERNEL);
  1114. if (!vc4_hdmi_encoder)
  1115. return -ENOMEM;
  1116. vc4_hdmi_encoder->base.type = VC4_ENCODER_TYPE_HDMI;
  1117. hdmi->encoder = &vc4_hdmi_encoder->base.base;
  1118. hdmi->pdev = pdev;
  1119. hdmi->hdmicore_regs = vc4_ioremap_regs(pdev, 0);
  1120. if (IS_ERR(hdmi->hdmicore_regs))
  1121. return PTR_ERR(hdmi->hdmicore_regs);
  1122. hdmi->hd_regs = vc4_ioremap_regs(pdev, 1);
  1123. if (IS_ERR(hdmi->hd_regs))
  1124. return PTR_ERR(hdmi->hd_regs);
  1125. hdmi->pixel_clock = devm_clk_get(dev, "pixel");
  1126. if (IS_ERR(hdmi->pixel_clock)) {
  1127. DRM_ERROR("Failed to get pixel clock\n");
  1128. return PTR_ERR(hdmi->pixel_clock);
  1129. }
  1130. hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
  1131. if (IS_ERR(hdmi->hsm_clock)) {
  1132. DRM_ERROR("Failed to get HDMI state machine clock\n");
  1133. return PTR_ERR(hdmi->hsm_clock);
  1134. }
  1135. ddc_node = of_parse_phandle(dev->of_node, "ddc", 0);
  1136. if (!ddc_node) {
  1137. DRM_ERROR("Failed to find ddc node in device tree\n");
  1138. return -ENODEV;
  1139. }
  1140. hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node);
  1141. of_node_put(ddc_node);
  1142. if (!hdmi->ddc) {
  1143. DRM_DEBUG("Failed to get ddc i2c adapter by node\n");
  1144. return -EPROBE_DEFER;
  1145. }
  1146. /* This is the rate that is set by the firmware. The number
  1147. * needs to be a bit higher than the pixel clock rate
  1148. * (generally 148.5Mhz).
  1149. */
  1150. ret = clk_set_rate(hdmi->hsm_clock, HSM_CLOCK_FREQ);
  1151. if (ret) {
  1152. DRM_ERROR("Failed to set HSM clock rate: %d\n", ret);
  1153. goto err_put_i2c;
  1154. }
  1155. ret = clk_prepare_enable(hdmi->hsm_clock);
  1156. if (ret) {
  1157. DRM_ERROR("Failed to turn on HDMI state machine clock: %d\n",
  1158. ret);
  1159. goto err_put_i2c;
  1160. }
  1161. /* Only use the GPIO HPD pin if present in the DT, otherwise
  1162. * we'll use the HDMI core's register.
  1163. */
  1164. if (of_find_property(dev->of_node, "hpd-gpios", &value)) {
  1165. enum of_gpio_flags hpd_gpio_flags;
  1166. hdmi->hpd_gpio = of_get_named_gpio_flags(dev->of_node,
  1167. "hpd-gpios", 0,
  1168. &hpd_gpio_flags);
  1169. if (hdmi->hpd_gpio < 0) {
  1170. ret = hdmi->hpd_gpio;
  1171. goto err_unprepare_hsm;
  1172. }
  1173. hdmi->hpd_active_low = hpd_gpio_flags & OF_GPIO_ACTIVE_LOW;
  1174. }
  1175. vc4->hdmi = hdmi;
  1176. /* HDMI core must be enabled. */
  1177. if (!(HD_READ(VC4_HD_M_CTL) & VC4_HD_M_ENABLE)) {
  1178. HD_WRITE(VC4_HD_M_CTL, VC4_HD_M_SW_RST);
  1179. udelay(1);
  1180. HD_WRITE(VC4_HD_M_CTL, 0);
  1181. HD_WRITE(VC4_HD_M_CTL, VC4_HD_M_ENABLE);
  1182. }
  1183. pm_runtime_enable(dev);
  1184. drm_encoder_init(drm, hdmi->encoder, &vc4_hdmi_encoder_funcs,
  1185. DRM_MODE_ENCODER_TMDS, NULL);
  1186. drm_encoder_helper_add(hdmi->encoder, &vc4_hdmi_encoder_helper_funcs);
  1187. hdmi->connector = vc4_hdmi_connector_init(drm, hdmi->encoder);
  1188. if (IS_ERR(hdmi->connector)) {
  1189. ret = PTR_ERR(hdmi->connector);
  1190. goto err_destroy_encoder;
  1191. }
  1192. #ifdef CONFIG_DRM_VC4_HDMI_CEC
  1193. hdmi->cec_adap = cec_allocate_adapter(&vc4_hdmi_cec_adap_ops,
  1194. vc4, "vc4",
  1195. CEC_CAP_TRANSMIT |
  1196. CEC_CAP_LOG_ADDRS |
  1197. CEC_CAP_PASSTHROUGH |
  1198. CEC_CAP_RC, 1);
  1199. ret = PTR_ERR_OR_ZERO(hdmi->cec_adap);
  1200. if (ret < 0)
  1201. goto err_destroy_conn;
  1202. HDMI_WRITE(VC4_HDMI_CPU_MASK_SET, 0xffffffff);
  1203. value = HDMI_READ(VC4_HDMI_CEC_CNTRL_1);
  1204. value &= ~VC4_HDMI_CEC_DIV_CLK_CNT_MASK;
  1205. /*
  1206. * Set the logical address to Unregistered and set the clock
  1207. * divider: the hsm_clock rate and this divider setting will
  1208. * give a 40 kHz CEC clock.
  1209. */
  1210. value |= VC4_HDMI_CEC_ADDR_MASK |
  1211. (4091 << VC4_HDMI_CEC_DIV_CLK_CNT_SHIFT);
  1212. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1, value);
  1213. ret = devm_request_threaded_irq(dev, platform_get_irq(pdev, 0),
  1214. vc4_cec_irq_handler,
  1215. vc4_cec_irq_handler_thread, 0,
  1216. "vc4 hdmi cec", vc4);
  1217. if (ret)
  1218. goto err_delete_cec_adap;
  1219. ret = cec_register_adapter(hdmi->cec_adap, dev);
  1220. if (ret < 0)
  1221. goto err_delete_cec_adap;
  1222. #endif
  1223. ret = vc4_hdmi_audio_init(hdmi);
  1224. if (ret)
  1225. goto err_destroy_encoder;
  1226. return 0;
  1227. #ifdef CONFIG_DRM_VC4_HDMI_CEC
  1228. err_delete_cec_adap:
  1229. cec_delete_adapter(hdmi->cec_adap);
  1230. err_destroy_conn:
  1231. vc4_hdmi_connector_destroy(hdmi->connector);
  1232. #endif
  1233. err_destroy_encoder:
  1234. vc4_hdmi_encoder_destroy(hdmi->encoder);
  1235. err_unprepare_hsm:
  1236. clk_disable_unprepare(hdmi->hsm_clock);
  1237. pm_runtime_disable(dev);
  1238. err_put_i2c:
  1239. put_device(&hdmi->ddc->dev);
  1240. return ret;
  1241. }
  1242. static void vc4_hdmi_unbind(struct device *dev, struct device *master,
  1243. void *data)
  1244. {
  1245. struct drm_device *drm = dev_get_drvdata(master);
  1246. struct vc4_dev *vc4 = drm->dev_private;
  1247. struct vc4_hdmi *hdmi = vc4->hdmi;
  1248. vc4_hdmi_audio_cleanup(hdmi);
  1249. cec_unregister_adapter(hdmi->cec_adap);
  1250. vc4_hdmi_connector_destroy(hdmi->connector);
  1251. vc4_hdmi_encoder_destroy(hdmi->encoder);
  1252. clk_disable_unprepare(hdmi->hsm_clock);
  1253. pm_runtime_disable(dev);
  1254. put_device(&hdmi->ddc->dev);
  1255. vc4->hdmi = NULL;
  1256. }
  1257. static const struct component_ops vc4_hdmi_ops = {
  1258. .bind = vc4_hdmi_bind,
  1259. .unbind = vc4_hdmi_unbind,
  1260. };
  1261. static int vc4_hdmi_dev_probe(struct platform_device *pdev)
  1262. {
  1263. return component_add(&pdev->dev, &vc4_hdmi_ops);
  1264. }
  1265. static int vc4_hdmi_dev_remove(struct platform_device *pdev)
  1266. {
  1267. component_del(&pdev->dev, &vc4_hdmi_ops);
  1268. return 0;
  1269. }
  1270. static const struct of_device_id vc4_hdmi_dt_match[] = {
  1271. { .compatible = "brcm,bcm2835-hdmi" },
  1272. {}
  1273. };
  1274. struct platform_driver vc4_hdmi_driver = {
  1275. .probe = vc4_hdmi_dev_probe,
  1276. .remove = vc4_hdmi_dev_remove,
  1277. .driver = {
  1278. .name = "vc4_hdmi",
  1279. .of_match_table = vc4_hdmi_dt_match,
  1280. },
  1281. };