vc4_hdmi.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524
  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. 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_mode_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. udelay(1000);
  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_codec_driver vc4_hdmi_audio_codec_drv = {
  835. .component_driver = {
  836. .controls = vc4_hdmi_audio_controls,
  837. .num_controls = ARRAY_SIZE(vc4_hdmi_audio_controls),
  838. .dapm_widgets = vc4_hdmi_audio_widgets,
  839. .num_dapm_widgets = ARRAY_SIZE(vc4_hdmi_audio_widgets),
  840. .dapm_routes = vc4_hdmi_audio_routes,
  841. .num_dapm_routes = ARRAY_SIZE(vc4_hdmi_audio_routes),
  842. },
  843. };
  844. static const struct snd_soc_dai_ops vc4_hdmi_audio_dai_ops = {
  845. .startup = vc4_hdmi_audio_startup,
  846. .shutdown = vc4_hdmi_audio_shutdown,
  847. .hw_params = vc4_hdmi_audio_hw_params,
  848. .set_fmt = vc4_hdmi_audio_set_fmt,
  849. .trigger = vc4_hdmi_audio_trigger,
  850. };
  851. static struct snd_soc_dai_driver vc4_hdmi_audio_codec_dai_drv = {
  852. .name = "vc4-hdmi-hifi",
  853. .playback = {
  854. .stream_name = "Playback",
  855. .channels_min = 2,
  856. .channels_max = 8,
  857. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
  858. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
  859. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
  860. SNDRV_PCM_RATE_192000,
  861. .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
  862. },
  863. };
  864. static const struct snd_soc_component_driver vc4_hdmi_audio_cpu_dai_comp = {
  865. .name = "vc4-hdmi-cpu-dai-component",
  866. };
  867. static int vc4_hdmi_audio_cpu_dai_probe(struct snd_soc_dai *dai)
  868. {
  869. struct vc4_hdmi *hdmi = dai_to_hdmi(dai);
  870. snd_soc_dai_init_dma_data(dai, &hdmi->audio.dma_data, NULL);
  871. return 0;
  872. }
  873. static struct snd_soc_dai_driver vc4_hdmi_audio_cpu_dai_drv = {
  874. .name = "vc4-hdmi-cpu-dai",
  875. .probe = vc4_hdmi_audio_cpu_dai_probe,
  876. .playback = {
  877. .stream_name = "Playback",
  878. .channels_min = 1,
  879. .channels_max = 8,
  880. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
  881. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
  882. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
  883. SNDRV_PCM_RATE_192000,
  884. .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
  885. },
  886. .ops = &vc4_hdmi_audio_dai_ops,
  887. };
  888. static const struct snd_dmaengine_pcm_config pcm_conf = {
  889. .chan_names[SNDRV_PCM_STREAM_PLAYBACK] = "audio-rx",
  890. .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
  891. };
  892. static int vc4_hdmi_audio_init(struct vc4_hdmi *hdmi)
  893. {
  894. struct snd_soc_dai_link *dai_link = &hdmi->audio.link;
  895. struct snd_soc_card *card = &hdmi->audio.card;
  896. struct device *dev = &hdmi->pdev->dev;
  897. const __be32 *addr;
  898. int ret;
  899. if (!of_find_property(dev->of_node, "dmas", NULL)) {
  900. dev_warn(dev,
  901. "'dmas' DT property is missing, no HDMI audio\n");
  902. return 0;
  903. }
  904. /*
  905. * Get the physical address of VC4_HD_MAI_DATA. We need to retrieve
  906. * the bus address specified in the DT, because the physical address
  907. * (the one returned by platform_get_resource()) is not appropriate
  908. * for DMA transfers.
  909. * This VC/MMU should probably be exposed to avoid this kind of hacks.
  910. */
  911. addr = of_get_address(dev->of_node, 1, NULL, NULL);
  912. hdmi->audio.dma_data.addr = be32_to_cpup(addr) + VC4_HD_MAI_DATA;
  913. hdmi->audio.dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  914. hdmi->audio.dma_data.maxburst = 2;
  915. ret = devm_snd_dmaengine_pcm_register(dev, &pcm_conf, 0);
  916. if (ret) {
  917. dev_err(dev, "Could not register PCM component: %d\n", ret);
  918. return ret;
  919. }
  920. ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_cpu_dai_comp,
  921. &vc4_hdmi_audio_cpu_dai_drv, 1);
  922. if (ret) {
  923. dev_err(dev, "Could not register CPU DAI: %d\n", ret);
  924. return ret;
  925. }
  926. /* register codec and codec dai */
  927. ret = snd_soc_register_codec(dev, &vc4_hdmi_audio_codec_drv,
  928. &vc4_hdmi_audio_codec_dai_drv, 1);
  929. if (ret) {
  930. dev_err(dev, "Could not register codec: %d\n", ret);
  931. return ret;
  932. }
  933. dai_link->name = "MAI";
  934. dai_link->stream_name = "MAI PCM";
  935. dai_link->codec_dai_name = vc4_hdmi_audio_codec_dai_drv.name;
  936. dai_link->cpu_dai_name = dev_name(dev);
  937. dai_link->codec_name = dev_name(dev);
  938. dai_link->platform_name = dev_name(dev);
  939. card->dai_link = dai_link;
  940. card->num_links = 1;
  941. card->name = "vc4-hdmi";
  942. card->dev = dev;
  943. /*
  944. * Be careful, snd_soc_register_card() calls dev_set_drvdata() and
  945. * stores a pointer to the snd card object in dev->driver_data. This
  946. * means we cannot use it for something else. The hdmi back-pointer is
  947. * now stored in card->drvdata and should be retrieved with
  948. * snd_soc_card_get_drvdata() if needed.
  949. */
  950. snd_soc_card_set_drvdata(card, hdmi);
  951. ret = devm_snd_soc_register_card(dev, card);
  952. if (ret) {
  953. dev_err(dev, "Could not register sound card: %d\n", ret);
  954. goto unregister_codec;
  955. }
  956. return 0;
  957. unregister_codec:
  958. snd_soc_unregister_codec(dev);
  959. return ret;
  960. }
  961. static void vc4_hdmi_audio_cleanup(struct vc4_hdmi *hdmi)
  962. {
  963. struct device *dev = &hdmi->pdev->dev;
  964. /*
  965. * If drvdata is not set this means the audio card was not
  966. * registered, just skip codec unregistration in this case.
  967. */
  968. if (dev_get_drvdata(dev))
  969. snd_soc_unregister_codec(dev);
  970. }
  971. #ifdef CONFIG_DRM_VC4_HDMI_CEC
  972. static irqreturn_t vc4_cec_irq_handler_thread(int irq, void *priv)
  973. {
  974. struct vc4_dev *vc4 = priv;
  975. struct vc4_hdmi *hdmi = vc4->hdmi;
  976. if (hdmi->cec_irq_was_rx) {
  977. if (hdmi->cec_rx_msg.len)
  978. cec_received_msg(hdmi->cec_adap, &hdmi->cec_rx_msg);
  979. } else if (hdmi->cec_tx_ok) {
  980. cec_transmit_done(hdmi->cec_adap, CEC_TX_STATUS_OK,
  981. 0, 0, 0, 0);
  982. } else {
  983. /*
  984. * This CEC implementation makes 1 retry, so if we
  985. * get a NACK, then that means it made 2 attempts.
  986. */
  987. cec_transmit_done(hdmi->cec_adap, CEC_TX_STATUS_NACK,
  988. 0, 2, 0, 0);
  989. }
  990. return IRQ_HANDLED;
  991. }
  992. static void vc4_cec_read_msg(struct vc4_dev *vc4, u32 cntrl1)
  993. {
  994. struct cec_msg *msg = &vc4->hdmi->cec_rx_msg;
  995. unsigned int i;
  996. msg->len = 1 + ((cntrl1 & VC4_HDMI_CEC_REC_WRD_CNT_MASK) >>
  997. VC4_HDMI_CEC_REC_WRD_CNT_SHIFT);
  998. for (i = 0; i < msg->len; i += 4) {
  999. u32 val = HDMI_READ(VC4_HDMI_CEC_RX_DATA_1 + i);
  1000. msg->msg[i] = val & 0xff;
  1001. msg->msg[i + 1] = (val >> 8) & 0xff;
  1002. msg->msg[i + 2] = (val >> 16) & 0xff;
  1003. msg->msg[i + 3] = (val >> 24) & 0xff;
  1004. }
  1005. }
  1006. static irqreturn_t vc4_cec_irq_handler(int irq, void *priv)
  1007. {
  1008. struct vc4_dev *vc4 = priv;
  1009. struct vc4_hdmi *hdmi = vc4->hdmi;
  1010. u32 stat = HDMI_READ(VC4_HDMI_CPU_STATUS);
  1011. u32 cntrl1, cntrl5;
  1012. if (!(stat & VC4_HDMI_CPU_CEC))
  1013. return IRQ_NONE;
  1014. hdmi->cec_rx_msg.len = 0;
  1015. cntrl1 = HDMI_READ(VC4_HDMI_CEC_CNTRL_1);
  1016. cntrl5 = HDMI_READ(VC4_HDMI_CEC_CNTRL_5);
  1017. hdmi->cec_irq_was_rx = cntrl5 & VC4_HDMI_CEC_RX_CEC_INT;
  1018. if (hdmi->cec_irq_was_rx) {
  1019. vc4_cec_read_msg(vc4, cntrl1);
  1020. cntrl1 |= VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
  1021. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1, cntrl1);
  1022. cntrl1 &= ~VC4_HDMI_CEC_CLEAR_RECEIVE_OFF;
  1023. } else {
  1024. hdmi->cec_tx_ok = cntrl1 & VC4_HDMI_CEC_TX_STATUS_GOOD;
  1025. cntrl1 &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
  1026. }
  1027. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1, cntrl1);
  1028. HDMI_WRITE(VC4_HDMI_CPU_CLEAR, VC4_HDMI_CPU_CEC);
  1029. return IRQ_WAKE_THREAD;
  1030. }
  1031. static int vc4_hdmi_cec_adap_enable(struct cec_adapter *adap, bool enable)
  1032. {
  1033. struct vc4_dev *vc4 = cec_get_drvdata(adap);
  1034. /* clock period in microseconds */
  1035. const u32 usecs = 1000000 / CEC_CLOCK_FREQ;
  1036. u32 val = HDMI_READ(VC4_HDMI_CEC_CNTRL_5);
  1037. val &= ~(VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET |
  1038. VC4_HDMI_CEC_CNT_TO_4700_US_MASK |
  1039. VC4_HDMI_CEC_CNT_TO_4500_US_MASK);
  1040. val |= ((4700 / usecs) << VC4_HDMI_CEC_CNT_TO_4700_US_SHIFT) |
  1041. ((4500 / usecs) << VC4_HDMI_CEC_CNT_TO_4500_US_SHIFT);
  1042. if (enable) {
  1043. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_5, val |
  1044. VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
  1045. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_5, val);
  1046. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_2,
  1047. ((1500 / usecs) << VC4_HDMI_CEC_CNT_TO_1500_US_SHIFT) |
  1048. ((1300 / usecs) << VC4_HDMI_CEC_CNT_TO_1300_US_SHIFT) |
  1049. ((800 / usecs) << VC4_HDMI_CEC_CNT_TO_800_US_SHIFT) |
  1050. ((600 / usecs) << VC4_HDMI_CEC_CNT_TO_600_US_SHIFT) |
  1051. ((400 / usecs) << VC4_HDMI_CEC_CNT_TO_400_US_SHIFT));
  1052. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_3,
  1053. ((2750 / usecs) << VC4_HDMI_CEC_CNT_TO_2750_US_SHIFT) |
  1054. ((2400 / usecs) << VC4_HDMI_CEC_CNT_TO_2400_US_SHIFT) |
  1055. ((2050 / usecs) << VC4_HDMI_CEC_CNT_TO_2050_US_SHIFT) |
  1056. ((1700 / usecs) << VC4_HDMI_CEC_CNT_TO_1700_US_SHIFT));
  1057. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_4,
  1058. ((4300 / usecs) << VC4_HDMI_CEC_CNT_TO_4300_US_SHIFT) |
  1059. ((3900 / usecs) << VC4_HDMI_CEC_CNT_TO_3900_US_SHIFT) |
  1060. ((3600 / usecs) << VC4_HDMI_CEC_CNT_TO_3600_US_SHIFT) |
  1061. ((3500 / usecs) << VC4_HDMI_CEC_CNT_TO_3500_US_SHIFT));
  1062. HDMI_WRITE(VC4_HDMI_CPU_MASK_CLEAR, VC4_HDMI_CPU_CEC);
  1063. } else {
  1064. HDMI_WRITE(VC4_HDMI_CPU_MASK_SET, VC4_HDMI_CPU_CEC);
  1065. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_5, val |
  1066. VC4_HDMI_CEC_TX_SW_RESET | VC4_HDMI_CEC_RX_SW_RESET);
  1067. }
  1068. return 0;
  1069. }
  1070. static int vc4_hdmi_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
  1071. {
  1072. struct vc4_dev *vc4 = cec_get_drvdata(adap);
  1073. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1,
  1074. (HDMI_READ(VC4_HDMI_CEC_CNTRL_1) & ~VC4_HDMI_CEC_ADDR_MASK) |
  1075. (log_addr & 0xf) << VC4_HDMI_CEC_ADDR_SHIFT);
  1076. return 0;
  1077. }
  1078. static int vc4_hdmi_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
  1079. u32 signal_free_time, struct cec_msg *msg)
  1080. {
  1081. struct vc4_dev *vc4 = cec_get_drvdata(adap);
  1082. u32 val;
  1083. unsigned int i;
  1084. for (i = 0; i < msg->len; i += 4)
  1085. HDMI_WRITE(VC4_HDMI_CEC_TX_DATA_1 + i,
  1086. (msg->msg[i]) |
  1087. (msg->msg[i + 1] << 8) |
  1088. (msg->msg[i + 2] << 16) |
  1089. (msg->msg[i + 3] << 24));
  1090. val = HDMI_READ(VC4_HDMI_CEC_CNTRL_1);
  1091. val &= ~VC4_HDMI_CEC_START_XMIT_BEGIN;
  1092. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1, val);
  1093. val &= ~VC4_HDMI_CEC_MESSAGE_LENGTH_MASK;
  1094. val |= (msg->len - 1) << VC4_HDMI_CEC_MESSAGE_LENGTH_SHIFT;
  1095. val |= VC4_HDMI_CEC_START_XMIT_BEGIN;
  1096. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1, val);
  1097. return 0;
  1098. }
  1099. static const struct cec_adap_ops vc4_hdmi_cec_adap_ops = {
  1100. .adap_enable = vc4_hdmi_cec_adap_enable,
  1101. .adap_log_addr = vc4_hdmi_cec_adap_log_addr,
  1102. .adap_transmit = vc4_hdmi_cec_adap_transmit,
  1103. };
  1104. #endif
  1105. static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data)
  1106. {
  1107. struct platform_device *pdev = to_platform_device(dev);
  1108. struct drm_device *drm = dev_get_drvdata(master);
  1109. struct vc4_dev *vc4 = drm->dev_private;
  1110. struct vc4_hdmi *hdmi;
  1111. struct vc4_hdmi_encoder *vc4_hdmi_encoder;
  1112. struct device_node *ddc_node;
  1113. u32 value;
  1114. int ret;
  1115. hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
  1116. if (!hdmi)
  1117. return -ENOMEM;
  1118. vc4_hdmi_encoder = devm_kzalloc(dev, sizeof(*vc4_hdmi_encoder),
  1119. GFP_KERNEL);
  1120. if (!vc4_hdmi_encoder)
  1121. return -ENOMEM;
  1122. vc4_hdmi_encoder->base.type = VC4_ENCODER_TYPE_HDMI;
  1123. hdmi->encoder = &vc4_hdmi_encoder->base.base;
  1124. hdmi->pdev = pdev;
  1125. hdmi->hdmicore_regs = vc4_ioremap_regs(pdev, 0);
  1126. if (IS_ERR(hdmi->hdmicore_regs))
  1127. return PTR_ERR(hdmi->hdmicore_regs);
  1128. hdmi->hd_regs = vc4_ioremap_regs(pdev, 1);
  1129. if (IS_ERR(hdmi->hd_regs))
  1130. return PTR_ERR(hdmi->hd_regs);
  1131. hdmi->pixel_clock = devm_clk_get(dev, "pixel");
  1132. if (IS_ERR(hdmi->pixel_clock)) {
  1133. DRM_ERROR("Failed to get pixel clock\n");
  1134. return PTR_ERR(hdmi->pixel_clock);
  1135. }
  1136. hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
  1137. if (IS_ERR(hdmi->hsm_clock)) {
  1138. DRM_ERROR("Failed to get HDMI state machine clock\n");
  1139. return PTR_ERR(hdmi->hsm_clock);
  1140. }
  1141. ddc_node = of_parse_phandle(dev->of_node, "ddc", 0);
  1142. if (!ddc_node) {
  1143. DRM_ERROR("Failed to find ddc node in device tree\n");
  1144. return -ENODEV;
  1145. }
  1146. hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node);
  1147. of_node_put(ddc_node);
  1148. if (!hdmi->ddc) {
  1149. DRM_DEBUG("Failed to get ddc i2c adapter by node\n");
  1150. return -EPROBE_DEFER;
  1151. }
  1152. /* This is the rate that is set by the firmware. The number
  1153. * needs to be a bit higher than the pixel clock rate
  1154. * (generally 148.5Mhz).
  1155. */
  1156. ret = clk_set_rate(hdmi->hsm_clock, HSM_CLOCK_FREQ);
  1157. if (ret) {
  1158. DRM_ERROR("Failed to set HSM clock rate: %d\n", ret);
  1159. goto err_put_i2c;
  1160. }
  1161. ret = clk_prepare_enable(hdmi->hsm_clock);
  1162. if (ret) {
  1163. DRM_ERROR("Failed to turn on HDMI state machine clock: %d\n",
  1164. ret);
  1165. goto err_put_i2c;
  1166. }
  1167. /* Only use the GPIO HPD pin if present in the DT, otherwise
  1168. * we'll use the HDMI core's register.
  1169. */
  1170. if (of_find_property(dev->of_node, "hpd-gpios", &value)) {
  1171. enum of_gpio_flags hpd_gpio_flags;
  1172. hdmi->hpd_gpio = of_get_named_gpio_flags(dev->of_node,
  1173. "hpd-gpios", 0,
  1174. &hpd_gpio_flags);
  1175. if (hdmi->hpd_gpio < 0) {
  1176. ret = hdmi->hpd_gpio;
  1177. goto err_unprepare_hsm;
  1178. }
  1179. hdmi->hpd_active_low = hpd_gpio_flags & OF_GPIO_ACTIVE_LOW;
  1180. }
  1181. vc4->hdmi = hdmi;
  1182. /* HDMI core must be enabled. */
  1183. if (!(HD_READ(VC4_HD_M_CTL) & VC4_HD_M_ENABLE)) {
  1184. HD_WRITE(VC4_HD_M_CTL, VC4_HD_M_SW_RST);
  1185. udelay(1);
  1186. HD_WRITE(VC4_HD_M_CTL, 0);
  1187. HD_WRITE(VC4_HD_M_CTL, VC4_HD_M_ENABLE);
  1188. }
  1189. pm_runtime_enable(dev);
  1190. drm_encoder_init(drm, hdmi->encoder, &vc4_hdmi_encoder_funcs,
  1191. DRM_MODE_ENCODER_TMDS, NULL);
  1192. drm_encoder_helper_add(hdmi->encoder, &vc4_hdmi_encoder_helper_funcs);
  1193. hdmi->connector = vc4_hdmi_connector_init(drm, hdmi->encoder);
  1194. if (IS_ERR(hdmi->connector)) {
  1195. ret = PTR_ERR(hdmi->connector);
  1196. goto err_destroy_encoder;
  1197. }
  1198. #ifdef CONFIG_DRM_VC4_HDMI_CEC
  1199. hdmi->cec_adap = cec_allocate_adapter(&vc4_hdmi_cec_adap_ops,
  1200. vc4, "vc4",
  1201. CEC_CAP_TRANSMIT |
  1202. CEC_CAP_LOG_ADDRS |
  1203. CEC_CAP_PASSTHROUGH |
  1204. CEC_CAP_RC, 1);
  1205. ret = PTR_ERR_OR_ZERO(hdmi->cec_adap);
  1206. if (ret < 0)
  1207. goto err_destroy_conn;
  1208. HDMI_WRITE(VC4_HDMI_CPU_MASK_SET, 0xffffffff);
  1209. value = HDMI_READ(VC4_HDMI_CEC_CNTRL_1);
  1210. value &= ~VC4_HDMI_CEC_DIV_CLK_CNT_MASK;
  1211. /*
  1212. * Set the logical address to Unregistered and set the clock
  1213. * divider: the hsm_clock rate and this divider setting will
  1214. * give a 40 kHz CEC clock.
  1215. */
  1216. value |= VC4_HDMI_CEC_ADDR_MASK |
  1217. (4091 << VC4_HDMI_CEC_DIV_CLK_CNT_SHIFT);
  1218. HDMI_WRITE(VC4_HDMI_CEC_CNTRL_1, value);
  1219. ret = devm_request_threaded_irq(dev, platform_get_irq(pdev, 0),
  1220. vc4_cec_irq_handler,
  1221. vc4_cec_irq_handler_thread, 0,
  1222. "vc4 hdmi cec", vc4);
  1223. if (ret)
  1224. goto err_delete_cec_adap;
  1225. ret = cec_register_adapter(hdmi->cec_adap, dev);
  1226. if (ret < 0)
  1227. goto err_delete_cec_adap;
  1228. #endif
  1229. ret = vc4_hdmi_audio_init(hdmi);
  1230. if (ret)
  1231. goto err_destroy_encoder;
  1232. return 0;
  1233. #ifdef CONFIG_DRM_VC4_HDMI_CEC
  1234. err_delete_cec_adap:
  1235. cec_delete_adapter(hdmi->cec_adap);
  1236. err_destroy_conn:
  1237. vc4_hdmi_connector_destroy(hdmi->connector);
  1238. #endif
  1239. err_destroy_encoder:
  1240. vc4_hdmi_encoder_destroy(hdmi->encoder);
  1241. err_unprepare_hsm:
  1242. clk_disable_unprepare(hdmi->hsm_clock);
  1243. pm_runtime_disable(dev);
  1244. err_put_i2c:
  1245. put_device(&hdmi->ddc->dev);
  1246. return ret;
  1247. }
  1248. static void vc4_hdmi_unbind(struct device *dev, struct device *master,
  1249. void *data)
  1250. {
  1251. struct drm_device *drm = dev_get_drvdata(master);
  1252. struct vc4_dev *vc4 = drm->dev_private;
  1253. struct vc4_hdmi *hdmi = vc4->hdmi;
  1254. vc4_hdmi_audio_cleanup(hdmi);
  1255. cec_unregister_adapter(hdmi->cec_adap);
  1256. vc4_hdmi_connector_destroy(hdmi->connector);
  1257. vc4_hdmi_encoder_destroy(hdmi->encoder);
  1258. clk_disable_unprepare(hdmi->hsm_clock);
  1259. pm_runtime_disable(dev);
  1260. put_device(&hdmi->ddc->dev);
  1261. vc4->hdmi = NULL;
  1262. }
  1263. static const struct component_ops vc4_hdmi_ops = {
  1264. .bind = vc4_hdmi_bind,
  1265. .unbind = vc4_hdmi_unbind,
  1266. };
  1267. static int vc4_hdmi_dev_probe(struct platform_device *pdev)
  1268. {
  1269. return component_add(&pdev->dev, &vc4_hdmi_ops);
  1270. }
  1271. static int vc4_hdmi_dev_remove(struct platform_device *pdev)
  1272. {
  1273. component_del(&pdev->dev, &vc4_hdmi_ops);
  1274. return 0;
  1275. }
  1276. static const struct of_device_id vc4_hdmi_dt_match[] = {
  1277. { .compatible = "brcm,bcm2835-hdmi" },
  1278. {}
  1279. };
  1280. struct platform_driver vc4_hdmi_driver = {
  1281. .probe = vc4_hdmi_dev_probe,
  1282. .remove = vc4_hdmi_dev_remove,
  1283. .driver = {
  1284. .name = "vc4_hdmi",
  1285. .of_match_table = vc4_hdmi_dt_match,
  1286. },
  1287. };