vc4_hdmi.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  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_atomic_helper.h"
  44. #include "drm_crtc_helper.h"
  45. #include "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/rational.h"
  53. #include "sound/dmaengine_pcm.h"
  54. #include "sound/pcm_drm_eld.h"
  55. #include "sound/pcm_params.h"
  56. #include "sound/soc.h"
  57. #include "vc4_drv.h"
  58. #include "vc4_regs.h"
  59. /* HDMI audio information */
  60. struct vc4_hdmi_audio {
  61. struct snd_soc_card card;
  62. struct snd_soc_dai_link link;
  63. int samplerate;
  64. int channels;
  65. struct snd_dmaengine_dai_dma_data dma_data;
  66. struct snd_pcm_substream *substream;
  67. };
  68. /* General HDMI hardware state. */
  69. struct vc4_hdmi {
  70. struct platform_device *pdev;
  71. struct drm_encoder *encoder;
  72. struct drm_connector *connector;
  73. struct vc4_hdmi_audio audio;
  74. struct i2c_adapter *ddc;
  75. void __iomem *hdmicore_regs;
  76. void __iomem *hd_regs;
  77. int hpd_gpio;
  78. bool hpd_active_low;
  79. struct clk *pixel_clock;
  80. struct clk *hsm_clock;
  81. };
  82. #define HDMI_READ(offset) readl(vc4->hdmi->hdmicore_regs + offset)
  83. #define HDMI_WRITE(offset, val) writel(val, vc4->hdmi->hdmicore_regs + offset)
  84. #define HD_READ(offset) readl(vc4->hdmi->hd_regs + offset)
  85. #define HD_WRITE(offset, val) writel(val, vc4->hdmi->hd_regs + offset)
  86. /* VC4 HDMI encoder KMS struct */
  87. struct vc4_hdmi_encoder {
  88. struct vc4_encoder base;
  89. bool hdmi_monitor;
  90. bool limited_rgb_range;
  91. bool rgb_range_selectable;
  92. };
  93. static inline struct vc4_hdmi_encoder *
  94. to_vc4_hdmi_encoder(struct drm_encoder *encoder)
  95. {
  96. return container_of(encoder, struct vc4_hdmi_encoder, base.base);
  97. }
  98. /* VC4 HDMI connector KMS struct */
  99. struct vc4_hdmi_connector {
  100. struct drm_connector base;
  101. /* Since the connector is attached to just the one encoder,
  102. * this is the reference to it so we can do the best_encoder()
  103. * hook.
  104. */
  105. struct drm_encoder *encoder;
  106. };
  107. static inline struct vc4_hdmi_connector *
  108. to_vc4_hdmi_connector(struct drm_connector *connector)
  109. {
  110. return container_of(connector, struct vc4_hdmi_connector, base);
  111. }
  112. #define HDMI_REG(reg) { reg, #reg }
  113. static const struct {
  114. u32 reg;
  115. const char *name;
  116. } hdmi_regs[] = {
  117. HDMI_REG(VC4_HDMI_CORE_REV),
  118. HDMI_REG(VC4_HDMI_SW_RESET_CONTROL),
  119. HDMI_REG(VC4_HDMI_HOTPLUG_INT),
  120. HDMI_REG(VC4_HDMI_HOTPLUG),
  121. HDMI_REG(VC4_HDMI_MAI_CHANNEL_MAP),
  122. HDMI_REG(VC4_HDMI_MAI_CONFIG),
  123. HDMI_REG(VC4_HDMI_MAI_FORMAT),
  124. HDMI_REG(VC4_HDMI_AUDIO_PACKET_CONFIG),
  125. HDMI_REG(VC4_HDMI_RAM_PACKET_CONFIG),
  126. HDMI_REG(VC4_HDMI_HORZA),
  127. HDMI_REG(VC4_HDMI_HORZB),
  128. HDMI_REG(VC4_HDMI_FIFO_CTL),
  129. HDMI_REG(VC4_HDMI_SCHEDULER_CONTROL),
  130. HDMI_REG(VC4_HDMI_VERTA0),
  131. HDMI_REG(VC4_HDMI_VERTA1),
  132. HDMI_REG(VC4_HDMI_VERTB0),
  133. HDMI_REG(VC4_HDMI_VERTB1),
  134. HDMI_REG(VC4_HDMI_TX_PHY_RESET_CTL),
  135. HDMI_REG(VC4_HDMI_TX_PHY_CTL0),
  136. };
  137. static const struct {
  138. u32 reg;
  139. const char *name;
  140. } hd_regs[] = {
  141. HDMI_REG(VC4_HD_M_CTL),
  142. HDMI_REG(VC4_HD_MAI_CTL),
  143. HDMI_REG(VC4_HD_MAI_THR),
  144. HDMI_REG(VC4_HD_MAI_FMT),
  145. HDMI_REG(VC4_HD_MAI_SMP),
  146. HDMI_REG(VC4_HD_VID_CTL),
  147. HDMI_REG(VC4_HD_CSC_CTL),
  148. HDMI_REG(VC4_HD_FRAME_COUNT),
  149. };
  150. #ifdef CONFIG_DEBUG_FS
  151. int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused)
  152. {
  153. struct drm_info_node *node = (struct drm_info_node *)m->private;
  154. struct drm_device *dev = node->minor->dev;
  155. struct vc4_dev *vc4 = to_vc4_dev(dev);
  156. int i;
  157. for (i = 0; i < ARRAY_SIZE(hdmi_regs); i++) {
  158. seq_printf(m, "%s (0x%04x): 0x%08x\n",
  159. hdmi_regs[i].name, hdmi_regs[i].reg,
  160. HDMI_READ(hdmi_regs[i].reg));
  161. }
  162. for (i = 0; i < ARRAY_SIZE(hd_regs); i++) {
  163. seq_printf(m, "%s (0x%04x): 0x%08x\n",
  164. hd_regs[i].name, hd_regs[i].reg,
  165. HD_READ(hd_regs[i].reg));
  166. }
  167. return 0;
  168. }
  169. #endif /* CONFIG_DEBUG_FS */
  170. static void vc4_hdmi_dump_regs(struct drm_device *dev)
  171. {
  172. struct vc4_dev *vc4 = to_vc4_dev(dev);
  173. int i;
  174. for (i = 0; i < ARRAY_SIZE(hdmi_regs); i++) {
  175. DRM_INFO("0x%04x (%s): 0x%08x\n",
  176. hdmi_regs[i].reg, hdmi_regs[i].name,
  177. HDMI_READ(hdmi_regs[i].reg));
  178. }
  179. for (i = 0; i < ARRAY_SIZE(hd_regs); i++) {
  180. DRM_INFO("0x%04x (%s): 0x%08x\n",
  181. hd_regs[i].reg, hd_regs[i].name,
  182. HD_READ(hd_regs[i].reg));
  183. }
  184. }
  185. static enum drm_connector_status
  186. vc4_hdmi_connector_detect(struct drm_connector *connector, bool force)
  187. {
  188. struct drm_device *dev = connector->dev;
  189. struct vc4_dev *vc4 = to_vc4_dev(dev);
  190. if (vc4->hdmi->hpd_gpio) {
  191. if (gpio_get_value_cansleep(vc4->hdmi->hpd_gpio) ^
  192. vc4->hdmi->hpd_active_low)
  193. return connector_status_connected;
  194. else
  195. return connector_status_disconnected;
  196. }
  197. if (drm_probe_ddc(vc4->hdmi->ddc))
  198. return connector_status_connected;
  199. if (HDMI_READ(VC4_HDMI_HOTPLUG) & VC4_HDMI_HOTPLUG_CONNECTED)
  200. return connector_status_connected;
  201. else
  202. return connector_status_disconnected;
  203. }
  204. static void vc4_hdmi_connector_destroy(struct drm_connector *connector)
  205. {
  206. drm_connector_unregister(connector);
  207. drm_connector_cleanup(connector);
  208. }
  209. static int vc4_hdmi_connector_get_modes(struct drm_connector *connector)
  210. {
  211. struct vc4_hdmi_connector *vc4_connector =
  212. to_vc4_hdmi_connector(connector);
  213. struct drm_encoder *encoder = vc4_connector->encoder;
  214. struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
  215. struct drm_device *dev = connector->dev;
  216. struct vc4_dev *vc4 = to_vc4_dev(dev);
  217. int ret = 0;
  218. struct edid *edid;
  219. edid = drm_get_edid(connector, vc4->hdmi->ddc);
  220. if (!edid)
  221. return -ENODEV;
  222. vc4_encoder->hdmi_monitor = drm_detect_hdmi_monitor(edid);
  223. if (edid && edid->input & DRM_EDID_INPUT_DIGITAL) {
  224. vc4_encoder->rgb_range_selectable =
  225. drm_rgb_quant_range_selectable(edid);
  226. }
  227. drm_mode_connector_update_edid_property(connector, edid);
  228. ret = drm_add_edid_modes(connector, edid);
  229. drm_edid_to_eld(connector, edid);
  230. return ret;
  231. }
  232. static const struct drm_connector_funcs vc4_hdmi_connector_funcs = {
  233. .dpms = drm_atomic_helper_connector_dpms,
  234. .detect = vc4_hdmi_connector_detect,
  235. .fill_modes = drm_helper_probe_single_connector_modes,
  236. .destroy = vc4_hdmi_connector_destroy,
  237. .reset = drm_atomic_helper_connector_reset,
  238. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  239. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  240. };
  241. static const struct drm_connector_helper_funcs vc4_hdmi_connector_helper_funcs = {
  242. .get_modes = vc4_hdmi_connector_get_modes,
  243. };
  244. static struct drm_connector *vc4_hdmi_connector_init(struct drm_device *dev,
  245. struct drm_encoder *encoder)
  246. {
  247. struct drm_connector *connector = NULL;
  248. struct vc4_hdmi_connector *hdmi_connector;
  249. int ret = 0;
  250. hdmi_connector = devm_kzalloc(dev->dev, sizeof(*hdmi_connector),
  251. GFP_KERNEL);
  252. if (!hdmi_connector) {
  253. ret = -ENOMEM;
  254. goto fail;
  255. }
  256. connector = &hdmi_connector->base;
  257. hdmi_connector->encoder = encoder;
  258. drm_connector_init(dev, connector, &vc4_hdmi_connector_funcs,
  259. DRM_MODE_CONNECTOR_HDMIA);
  260. drm_connector_helper_add(connector, &vc4_hdmi_connector_helper_funcs);
  261. connector->polled = (DRM_CONNECTOR_POLL_CONNECT |
  262. DRM_CONNECTOR_POLL_DISCONNECT);
  263. connector->interlace_allowed = 1;
  264. connector->doublescan_allowed = 0;
  265. drm_mode_connector_attach_encoder(connector, encoder);
  266. return connector;
  267. fail:
  268. if (connector)
  269. vc4_hdmi_connector_destroy(connector);
  270. return ERR_PTR(ret);
  271. }
  272. static void vc4_hdmi_encoder_destroy(struct drm_encoder *encoder)
  273. {
  274. drm_encoder_cleanup(encoder);
  275. }
  276. static const struct drm_encoder_funcs vc4_hdmi_encoder_funcs = {
  277. .destroy = vc4_hdmi_encoder_destroy,
  278. };
  279. static int vc4_hdmi_stop_packet(struct drm_encoder *encoder,
  280. enum hdmi_infoframe_type type)
  281. {
  282. struct drm_device *dev = encoder->dev;
  283. struct vc4_dev *vc4 = to_vc4_dev(dev);
  284. u32 packet_id = type - 0x80;
  285. HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
  286. HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) & ~BIT(packet_id));
  287. return wait_for(!(HDMI_READ(VC4_HDMI_RAM_PACKET_STATUS) &
  288. BIT(packet_id)), 100);
  289. }
  290. static void vc4_hdmi_write_infoframe(struct drm_encoder *encoder,
  291. union hdmi_infoframe *frame)
  292. {
  293. struct drm_device *dev = encoder->dev;
  294. struct vc4_dev *vc4 = to_vc4_dev(dev);
  295. u32 packet_id = frame->any.type - 0x80;
  296. u32 packet_reg = VC4_HDMI_RAM_PACKET(packet_id);
  297. uint8_t buffer[VC4_HDMI_PACKET_STRIDE];
  298. ssize_t len, i;
  299. int ret;
  300. WARN_ONCE(!(HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) &
  301. VC4_HDMI_RAM_PACKET_ENABLE),
  302. "Packet RAM has to be on to store the packet.");
  303. len = hdmi_infoframe_pack(frame, buffer, sizeof(buffer));
  304. if (len < 0)
  305. return;
  306. ret = vc4_hdmi_stop_packet(encoder, frame->any.type);
  307. if (ret) {
  308. DRM_ERROR("Failed to wait for infoframe to go idle: %d\n", ret);
  309. return;
  310. }
  311. for (i = 0; i < len; i += 7) {
  312. HDMI_WRITE(packet_reg,
  313. buffer[i + 0] << 0 |
  314. buffer[i + 1] << 8 |
  315. buffer[i + 2] << 16);
  316. packet_reg += 4;
  317. HDMI_WRITE(packet_reg,
  318. buffer[i + 3] << 0 |
  319. buffer[i + 4] << 8 |
  320. buffer[i + 5] << 16 |
  321. buffer[i + 6] << 24);
  322. packet_reg += 4;
  323. }
  324. HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
  325. HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) | BIT(packet_id));
  326. ret = wait_for((HDMI_READ(VC4_HDMI_RAM_PACKET_STATUS) &
  327. BIT(packet_id)), 100);
  328. if (ret)
  329. DRM_ERROR("Failed to wait for infoframe to start: %d\n", ret);
  330. }
  331. static void vc4_hdmi_set_avi_infoframe(struct drm_encoder *encoder)
  332. {
  333. struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
  334. struct drm_crtc *crtc = encoder->crtc;
  335. const struct drm_display_mode *mode = &crtc->state->adjusted_mode;
  336. union hdmi_infoframe frame;
  337. int ret;
  338. ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi, mode);
  339. if (ret < 0) {
  340. DRM_ERROR("couldn't fill AVI infoframe\n");
  341. return;
  342. }
  343. drm_hdmi_avi_infoframe_quant_range(&frame.avi, mode,
  344. vc4_encoder->limited_rgb_range ?
  345. HDMI_QUANTIZATION_RANGE_LIMITED :
  346. HDMI_QUANTIZATION_RANGE_FULL,
  347. vc4_encoder->rgb_range_selectable);
  348. vc4_hdmi_write_infoframe(encoder, &frame);
  349. }
  350. static void vc4_hdmi_set_spd_infoframe(struct drm_encoder *encoder)
  351. {
  352. union hdmi_infoframe frame;
  353. int ret;
  354. ret = hdmi_spd_infoframe_init(&frame.spd, "Broadcom", "Videocore");
  355. if (ret < 0) {
  356. DRM_ERROR("couldn't fill SPD infoframe\n");
  357. return;
  358. }
  359. frame.spd.sdi = HDMI_SPD_SDI_PC;
  360. vc4_hdmi_write_infoframe(encoder, &frame);
  361. }
  362. static void vc4_hdmi_set_audio_infoframe(struct drm_encoder *encoder)
  363. {
  364. struct drm_device *drm = encoder->dev;
  365. struct vc4_dev *vc4 = drm->dev_private;
  366. struct vc4_hdmi *hdmi = vc4->hdmi;
  367. union hdmi_infoframe frame;
  368. int ret;
  369. ret = hdmi_audio_infoframe_init(&frame.audio);
  370. frame.audio.coding_type = HDMI_AUDIO_CODING_TYPE_STREAM;
  371. frame.audio.sample_frequency = HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM;
  372. frame.audio.sample_size = HDMI_AUDIO_SAMPLE_SIZE_STREAM;
  373. frame.audio.channels = hdmi->audio.channels;
  374. vc4_hdmi_write_infoframe(encoder, &frame);
  375. }
  376. static void vc4_hdmi_set_infoframes(struct drm_encoder *encoder)
  377. {
  378. vc4_hdmi_set_avi_infoframe(encoder);
  379. vc4_hdmi_set_spd_infoframe(encoder);
  380. }
  381. static void vc4_hdmi_encoder_mode_set(struct drm_encoder *encoder,
  382. struct drm_display_mode *unadjusted_mode,
  383. struct drm_display_mode *mode)
  384. {
  385. struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
  386. struct drm_device *dev = encoder->dev;
  387. struct vc4_dev *vc4 = to_vc4_dev(dev);
  388. bool debug_dump_regs = false;
  389. bool hsync_pos = mode->flags & DRM_MODE_FLAG_PHSYNC;
  390. bool vsync_pos = mode->flags & DRM_MODE_FLAG_PVSYNC;
  391. bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
  392. u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
  393. u32 verta = (VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
  394. VC4_HDMI_VERTA_VSP) |
  395. VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
  396. VC4_HDMI_VERTA_VFP) |
  397. VC4_SET_FIELD(mode->crtc_vdisplay, VC4_HDMI_VERTA_VAL));
  398. u32 vertb = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
  399. VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end,
  400. VC4_HDMI_VERTB_VBP));
  401. u32 vertb_even = (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO) |
  402. VC4_SET_FIELD(mode->crtc_vtotal -
  403. mode->crtc_vsync_end -
  404. interlaced,
  405. VC4_HDMI_VERTB_VBP));
  406. u32 csc_ctl;
  407. if (debug_dump_regs) {
  408. DRM_INFO("HDMI regs before:\n");
  409. vc4_hdmi_dump_regs(dev);
  410. }
  411. HD_WRITE(VC4_HD_VID_CTL, 0);
  412. clk_set_rate(vc4->hdmi->pixel_clock, mode->clock * 1000 *
  413. ((mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1));
  414. HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
  415. HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
  416. VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT |
  417. VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS);
  418. HDMI_WRITE(VC4_HDMI_HORZA,
  419. (vsync_pos ? VC4_HDMI_HORZA_VPOS : 0) |
  420. (hsync_pos ? VC4_HDMI_HORZA_HPOS : 0) |
  421. VC4_SET_FIELD(mode->hdisplay * pixel_rep,
  422. VC4_HDMI_HORZA_HAP));
  423. HDMI_WRITE(VC4_HDMI_HORZB,
  424. VC4_SET_FIELD((mode->htotal -
  425. mode->hsync_end) * pixel_rep,
  426. VC4_HDMI_HORZB_HBP) |
  427. VC4_SET_FIELD((mode->hsync_end -
  428. mode->hsync_start) * pixel_rep,
  429. VC4_HDMI_HORZB_HSP) |
  430. VC4_SET_FIELD((mode->hsync_start -
  431. mode->hdisplay) * pixel_rep,
  432. VC4_HDMI_HORZB_HFP));
  433. HDMI_WRITE(VC4_HDMI_VERTA0, verta);
  434. HDMI_WRITE(VC4_HDMI_VERTA1, verta);
  435. HDMI_WRITE(VC4_HDMI_VERTB0, vertb_even);
  436. HDMI_WRITE(VC4_HDMI_VERTB1, vertb);
  437. HD_WRITE(VC4_HD_VID_CTL,
  438. (vsync_pos ? 0 : VC4_HD_VID_CTL_VSYNC_LOW) |
  439. (hsync_pos ? 0 : VC4_HD_VID_CTL_HSYNC_LOW));
  440. csc_ctl = VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR,
  441. VC4_HD_CSC_CTL_ORDER);
  442. if (vc4_encoder->hdmi_monitor &&
  443. drm_default_rgb_quant_range(mode) ==
  444. HDMI_QUANTIZATION_RANGE_LIMITED) {
  445. /* CEA VICs other than #1 requre limited range RGB
  446. * output unless overridden by an AVI infoframe.
  447. * Apply a colorspace conversion to squash 0-255 down
  448. * to 16-235. The matrix here is:
  449. *
  450. * [ 0 0 0.8594 16]
  451. * [ 0 0.8594 0 16]
  452. * [ 0.8594 0 0 16]
  453. * [ 0 0 0 1]
  454. */
  455. csc_ctl |= VC4_HD_CSC_CTL_ENABLE;
  456. csc_ctl |= VC4_HD_CSC_CTL_RGB2YCC;
  457. csc_ctl |= VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM,
  458. VC4_HD_CSC_CTL_MODE);
  459. HD_WRITE(VC4_HD_CSC_12_11, (0x000 << 16) | 0x000);
  460. HD_WRITE(VC4_HD_CSC_14_13, (0x100 << 16) | 0x6e0);
  461. HD_WRITE(VC4_HD_CSC_22_21, (0x6e0 << 16) | 0x000);
  462. HD_WRITE(VC4_HD_CSC_24_23, (0x100 << 16) | 0x000);
  463. HD_WRITE(VC4_HD_CSC_32_31, (0x000 << 16) | 0x6e0);
  464. HD_WRITE(VC4_HD_CSC_34_33, (0x100 << 16) | 0x000);
  465. vc4_encoder->limited_rgb_range = true;
  466. } else {
  467. vc4_encoder->limited_rgb_range = false;
  468. }
  469. /* The RGB order applies even when CSC is disabled. */
  470. HD_WRITE(VC4_HD_CSC_CTL, csc_ctl);
  471. HDMI_WRITE(VC4_HDMI_FIFO_CTL, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N);
  472. if (debug_dump_regs) {
  473. DRM_INFO("HDMI regs after:\n");
  474. vc4_hdmi_dump_regs(dev);
  475. }
  476. }
  477. static void vc4_hdmi_encoder_disable(struct drm_encoder *encoder)
  478. {
  479. struct drm_device *dev = encoder->dev;
  480. struct vc4_dev *vc4 = to_vc4_dev(dev);
  481. HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG, 0);
  482. HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL, 0xf << 16);
  483. HD_WRITE(VC4_HD_VID_CTL,
  484. HD_READ(VC4_HD_VID_CTL) & ~VC4_HD_VID_CTL_ENABLE);
  485. }
  486. static void vc4_hdmi_encoder_enable(struct drm_encoder *encoder)
  487. {
  488. struct vc4_hdmi_encoder *vc4_encoder = to_vc4_hdmi_encoder(encoder);
  489. struct drm_device *dev = encoder->dev;
  490. struct vc4_dev *vc4 = to_vc4_dev(dev);
  491. int ret;
  492. HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL, 0);
  493. HD_WRITE(VC4_HD_VID_CTL,
  494. HD_READ(VC4_HD_VID_CTL) |
  495. VC4_HD_VID_CTL_ENABLE |
  496. VC4_HD_VID_CTL_UNDERFLOW_ENABLE |
  497. VC4_HD_VID_CTL_FRAME_COUNTER_RESET);
  498. if (vc4_encoder->hdmi_monitor) {
  499. HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
  500. HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
  501. VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
  502. ret = wait_for(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
  503. VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE, 1000);
  504. WARN_ONCE(ret, "Timeout waiting for "
  505. "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
  506. } else {
  507. HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
  508. HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) &
  509. ~(VC4_HDMI_RAM_PACKET_ENABLE));
  510. HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
  511. HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
  512. ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI);
  513. ret = wait_for(!(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
  514. VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE), 1000);
  515. WARN_ONCE(ret, "Timeout waiting for "
  516. "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
  517. }
  518. if (vc4_encoder->hdmi_monitor) {
  519. u32 drift;
  520. WARN_ON(!(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) &
  521. VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE));
  522. HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL,
  523. HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL) |
  524. VC4_HDMI_SCHEDULER_CONTROL_VERT_ALWAYS_KEEPOUT);
  525. HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG,
  526. VC4_HDMI_RAM_PACKET_ENABLE);
  527. vc4_hdmi_set_infoframes(encoder);
  528. drift = HDMI_READ(VC4_HDMI_FIFO_CTL);
  529. drift &= VC4_HDMI_FIFO_VALID_WRITE_MASK;
  530. HDMI_WRITE(VC4_HDMI_FIFO_CTL,
  531. drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
  532. HDMI_WRITE(VC4_HDMI_FIFO_CTL,
  533. drift | VC4_HDMI_FIFO_CTL_RECENTER);
  534. udelay(1000);
  535. HDMI_WRITE(VC4_HDMI_FIFO_CTL,
  536. drift & ~VC4_HDMI_FIFO_CTL_RECENTER);
  537. HDMI_WRITE(VC4_HDMI_FIFO_CTL,
  538. drift | VC4_HDMI_FIFO_CTL_RECENTER);
  539. ret = wait_for(HDMI_READ(VC4_HDMI_FIFO_CTL) &
  540. VC4_HDMI_FIFO_CTL_RECENTER_DONE, 1);
  541. WARN_ONCE(ret, "Timeout waiting for "
  542. "VC4_HDMI_FIFO_CTL_RECENTER_DONE");
  543. }
  544. }
  545. static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs = {
  546. .mode_set = vc4_hdmi_encoder_mode_set,
  547. .disable = vc4_hdmi_encoder_disable,
  548. .enable = vc4_hdmi_encoder_enable,
  549. };
  550. /* HDMI audio codec callbacks */
  551. static void vc4_hdmi_audio_set_mai_clock(struct vc4_hdmi *hdmi)
  552. {
  553. struct drm_device *drm = hdmi->encoder->dev;
  554. struct vc4_dev *vc4 = to_vc4_dev(drm);
  555. u32 hsm_clock = clk_get_rate(hdmi->hsm_clock);
  556. unsigned long n, m;
  557. rational_best_approximation(hsm_clock, hdmi->audio.samplerate,
  558. VC4_HD_MAI_SMP_N_MASK >>
  559. VC4_HD_MAI_SMP_N_SHIFT,
  560. (VC4_HD_MAI_SMP_M_MASK >>
  561. VC4_HD_MAI_SMP_M_SHIFT) + 1,
  562. &n, &m);
  563. HD_WRITE(VC4_HD_MAI_SMP,
  564. VC4_SET_FIELD(n, VC4_HD_MAI_SMP_N) |
  565. VC4_SET_FIELD(m - 1, VC4_HD_MAI_SMP_M));
  566. }
  567. static void vc4_hdmi_set_n_cts(struct vc4_hdmi *hdmi)
  568. {
  569. struct drm_encoder *encoder = hdmi->encoder;
  570. struct drm_crtc *crtc = encoder->crtc;
  571. struct drm_device *drm = encoder->dev;
  572. struct vc4_dev *vc4 = to_vc4_dev(drm);
  573. const struct drm_display_mode *mode = &crtc->state->adjusted_mode;
  574. u32 samplerate = hdmi->audio.samplerate;
  575. u32 n, cts;
  576. u64 tmp;
  577. n = 128 * samplerate / 1000;
  578. tmp = (u64)(mode->clock * 1000) * n;
  579. do_div(tmp, 128 * samplerate);
  580. cts = tmp;
  581. HDMI_WRITE(VC4_HDMI_CRP_CFG,
  582. VC4_HDMI_CRP_CFG_EXTERNAL_CTS_EN |
  583. VC4_SET_FIELD(n, VC4_HDMI_CRP_CFG_N));
  584. /*
  585. * We could get slightly more accurate clocks in some cases by
  586. * providing a CTS_1 value. The two CTS values are alternated
  587. * between based on the period fields
  588. */
  589. HDMI_WRITE(VC4_HDMI_CTS_0, cts);
  590. HDMI_WRITE(VC4_HDMI_CTS_1, cts);
  591. }
  592. static inline struct vc4_hdmi *dai_to_hdmi(struct snd_soc_dai *dai)
  593. {
  594. struct snd_soc_card *card = snd_soc_dai_get_drvdata(dai);
  595. return snd_soc_card_get_drvdata(card);
  596. }
  597. static int vc4_hdmi_audio_startup(struct snd_pcm_substream *substream,
  598. struct snd_soc_dai *dai)
  599. {
  600. struct vc4_hdmi *hdmi = dai_to_hdmi(dai);
  601. struct drm_encoder *encoder = hdmi->encoder;
  602. struct vc4_dev *vc4 = to_vc4_dev(encoder->dev);
  603. int ret;
  604. if (hdmi->audio.substream && hdmi->audio.substream != substream)
  605. return -EINVAL;
  606. hdmi->audio.substream = substream;
  607. /*
  608. * If the HDMI encoder hasn't probed, or the encoder is
  609. * currently in DVI mode, treat the codec dai as missing.
  610. */
  611. if (!encoder->crtc || !(HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG) &
  612. VC4_HDMI_RAM_PACKET_ENABLE))
  613. return -ENODEV;
  614. ret = snd_pcm_hw_constraint_eld(substream->runtime,
  615. hdmi->connector->eld);
  616. if (ret)
  617. return ret;
  618. return 0;
  619. }
  620. static int vc4_hdmi_audio_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  621. {
  622. return 0;
  623. }
  624. static void vc4_hdmi_audio_reset(struct vc4_hdmi *hdmi)
  625. {
  626. struct drm_encoder *encoder = hdmi->encoder;
  627. struct drm_device *drm = encoder->dev;
  628. struct device *dev = &hdmi->pdev->dev;
  629. struct vc4_dev *vc4 = to_vc4_dev(drm);
  630. int ret;
  631. ret = vc4_hdmi_stop_packet(encoder, HDMI_INFOFRAME_TYPE_AUDIO);
  632. if (ret)
  633. dev_err(dev, "Failed to stop audio infoframe: %d\n", ret);
  634. HD_WRITE(VC4_HD_MAI_CTL, VC4_HD_MAI_CTL_RESET);
  635. HD_WRITE(VC4_HD_MAI_CTL, VC4_HD_MAI_CTL_ERRORF);
  636. HD_WRITE(VC4_HD_MAI_CTL, VC4_HD_MAI_CTL_FLUSH);
  637. }
  638. static void vc4_hdmi_audio_shutdown(struct snd_pcm_substream *substream,
  639. struct snd_soc_dai *dai)
  640. {
  641. struct vc4_hdmi *hdmi = dai_to_hdmi(dai);
  642. if (substream != hdmi->audio.substream)
  643. return;
  644. vc4_hdmi_audio_reset(hdmi);
  645. hdmi->audio.substream = NULL;
  646. }
  647. /* HDMI audio codec callbacks */
  648. static int vc4_hdmi_audio_hw_params(struct snd_pcm_substream *substream,
  649. struct snd_pcm_hw_params *params,
  650. struct snd_soc_dai *dai)
  651. {
  652. struct vc4_hdmi *hdmi = dai_to_hdmi(dai);
  653. struct drm_encoder *encoder = hdmi->encoder;
  654. struct drm_device *drm = encoder->dev;
  655. struct device *dev = &hdmi->pdev->dev;
  656. struct vc4_dev *vc4 = to_vc4_dev(drm);
  657. u32 audio_packet_config, channel_mask;
  658. u32 channel_map, i;
  659. if (substream != hdmi->audio.substream)
  660. return -EINVAL;
  661. dev_dbg(dev, "%s: %u Hz, %d bit, %d channels\n", __func__,
  662. params_rate(params), params_width(params),
  663. params_channels(params));
  664. hdmi->audio.channels = params_channels(params);
  665. hdmi->audio.samplerate = params_rate(params);
  666. HD_WRITE(VC4_HD_MAI_CTL,
  667. VC4_HD_MAI_CTL_RESET |
  668. VC4_HD_MAI_CTL_FLUSH |
  669. VC4_HD_MAI_CTL_DLATE |
  670. VC4_HD_MAI_CTL_ERRORE |
  671. VC4_HD_MAI_CTL_ERRORF);
  672. vc4_hdmi_audio_set_mai_clock(hdmi);
  673. audio_packet_config =
  674. VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_SAMPLE_FLAT |
  675. VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_INACTIVE_CHANNELS |
  676. VC4_SET_FIELD(0xf, VC4_HDMI_AUDIO_PACKET_B_FRAME_IDENTIFIER);
  677. channel_mask = GENMASK(hdmi->audio.channels - 1, 0);
  678. audio_packet_config |= VC4_SET_FIELD(channel_mask,
  679. VC4_HDMI_AUDIO_PACKET_CEA_MASK);
  680. /* Set the MAI threshold. This logic mimics the firmware's. */
  681. if (hdmi->audio.samplerate > 96000) {
  682. HD_WRITE(VC4_HD_MAI_THR,
  683. VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQHIGH) |
  684. VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQLOW));
  685. } else if (hdmi->audio.samplerate > 48000) {
  686. HD_WRITE(VC4_HD_MAI_THR,
  687. VC4_SET_FIELD(0x14, VC4_HD_MAI_THR_DREQHIGH) |
  688. VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQLOW));
  689. } else {
  690. HD_WRITE(VC4_HD_MAI_THR,
  691. VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICHIGH) |
  692. VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICLOW) |
  693. VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_DREQHIGH) |
  694. VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_DREQLOW));
  695. }
  696. HDMI_WRITE(VC4_HDMI_MAI_CONFIG,
  697. VC4_HDMI_MAI_CONFIG_BIT_REVERSE |
  698. VC4_SET_FIELD(channel_mask, VC4_HDMI_MAI_CHANNEL_MASK));
  699. channel_map = 0;
  700. for (i = 0; i < 8; i++) {
  701. if (channel_mask & BIT(i))
  702. channel_map |= i << (3 * i);
  703. }
  704. HDMI_WRITE(VC4_HDMI_MAI_CHANNEL_MAP, channel_map);
  705. HDMI_WRITE(VC4_HDMI_AUDIO_PACKET_CONFIG, audio_packet_config);
  706. vc4_hdmi_set_n_cts(hdmi);
  707. return 0;
  708. }
  709. static int vc4_hdmi_audio_trigger(struct snd_pcm_substream *substream, int cmd,
  710. struct snd_soc_dai *dai)
  711. {
  712. struct vc4_hdmi *hdmi = dai_to_hdmi(dai);
  713. struct drm_encoder *encoder = hdmi->encoder;
  714. struct drm_device *drm = encoder->dev;
  715. struct vc4_dev *vc4 = to_vc4_dev(drm);
  716. switch (cmd) {
  717. case SNDRV_PCM_TRIGGER_START:
  718. vc4_hdmi_set_audio_infoframe(encoder);
  719. HDMI_WRITE(VC4_HDMI_TX_PHY_CTL0,
  720. HDMI_READ(VC4_HDMI_TX_PHY_CTL0) &
  721. ~VC4_HDMI_TX_PHY_RNG_PWRDN);
  722. HD_WRITE(VC4_HD_MAI_CTL,
  723. VC4_SET_FIELD(hdmi->audio.channels,
  724. VC4_HD_MAI_CTL_CHNUM) |
  725. VC4_HD_MAI_CTL_ENABLE);
  726. break;
  727. case SNDRV_PCM_TRIGGER_STOP:
  728. HD_WRITE(VC4_HD_MAI_CTL,
  729. VC4_HD_MAI_CTL_DLATE |
  730. VC4_HD_MAI_CTL_ERRORE |
  731. VC4_HD_MAI_CTL_ERRORF);
  732. HDMI_WRITE(VC4_HDMI_TX_PHY_CTL0,
  733. HDMI_READ(VC4_HDMI_TX_PHY_CTL0) |
  734. VC4_HDMI_TX_PHY_RNG_PWRDN);
  735. break;
  736. default:
  737. break;
  738. }
  739. return 0;
  740. }
  741. static inline struct vc4_hdmi *
  742. snd_component_to_hdmi(struct snd_soc_component *component)
  743. {
  744. struct snd_soc_card *card = snd_soc_component_get_drvdata(component);
  745. return snd_soc_card_get_drvdata(card);
  746. }
  747. static int vc4_hdmi_audio_eld_ctl_info(struct snd_kcontrol *kcontrol,
  748. struct snd_ctl_elem_info *uinfo)
  749. {
  750. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  751. struct vc4_hdmi *hdmi = snd_component_to_hdmi(component);
  752. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  753. uinfo->count = sizeof(hdmi->connector->eld);
  754. return 0;
  755. }
  756. static int vc4_hdmi_audio_eld_ctl_get(struct snd_kcontrol *kcontrol,
  757. struct snd_ctl_elem_value *ucontrol)
  758. {
  759. struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
  760. struct vc4_hdmi *hdmi = snd_component_to_hdmi(component);
  761. memcpy(ucontrol->value.bytes.data, hdmi->connector->eld,
  762. sizeof(hdmi->connector->eld));
  763. return 0;
  764. }
  765. static const struct snd_kcontrol_new vc4_hdmi_audio_controls[] = {
  766. {
  767. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  768. SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  769. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  770. .name = "ELD",
  771. .info = vc4_hdmi_audio_eld_ctl_info,
  772. .get = vc4_hdmi_audio_eld_ctl_get,
  773. },
  774. };
  775. static const struct snd_soc_dapm_widget vc4_hdmi_audio_widgets[] = {
  776. SND_SOC_DAPM_OUTPUT("TX"),
  777. };
  778. static const struct snd_soc_dapm_route vc4_hdmi_audio_routes[] = {
  779. { "TX", NULL, "Playback" },
  780. };
  781. static const struct snd_soc_codec_driver vc4_hdmi_audio_codec_drv = {
  782. .component_driver = {
  783. .controls = vc4_hdmi_audio_controls,
  784. .num_controls = ARRAY_SIZE(vc4_hdmi_audio_controls),
  785. .dapm_widgets = vc4_hdmi_audio_widgets,
  786. .num_dapm_widgets = ARRAY_SIZE(vc4_hdmi_audio_widgets),
  787. .dapm_routes = vc4_hdmi_audio_routes,
  788. .num_dapm_routes = ARRAY_SIZE(vc4_hdmi_audio_routes),
  789. },
  790. };
  791. static const struct snd_soc_dai_ops vc4_hdmi_audio_dai_ops = {
  792. .startup = vc4_hdmi_audio_startup,
  793. .shutdown = vc4_hdmi_audio_shutdown,
  794. .hw_params = vc4_hdmi_audio_hw_params,
  795. .set_fmt = vc4_hdmi_audio_set_fmt,
  796. .trigger = vc4_hdmi_audio_trigger,
  797. };
  798. static struct snd_soc_dai_driver vc4_hdmi_audio_codec_dai_drv = {
  799. .name = "vc4-hdmi-hifi",
  800. .playback = {
  801. .stream_name = "Playback",
  802. .channels_min = 2,
  803. .channels_max = 8,
  804. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
  805. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
  806. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
  807. SNDRV_PCM_RATE_192000,
  808. .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
  809. },
  810. };
  811. static const struct snd_soc_component_driver vc4_hdmi_audio_cpu_dai_comp = {
  812. .name = "vc4-hdmi-cpu-dai-component",
  813. };
  814. static int vc4_hdmi_audio_cpu_dai_probe(struct snd_soc_dai *dai)
  815. {
  816. struct vc4_hdmi *hdmi = dai_to_hdmi(dai);
  817. snd_soc_dai_init_dma_data(dai, &hdmi->audio.dma_data, NULL);
  818. return 0;
  819. }
  820. static struct snd_soc_dai_driver vc4_hdmi_audio_cpu_dai_drv = {
  821. .name = "vc4-hdmi-cpu-dai",
  822. .probe = vc4_hdmi_audio_cpu_dai_probe,
  823. .playback = {
  824. .stream_name = "Playback",
  825. .channels_min = 1,
  826. .channels_max = 8,
  827. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
  828. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
  829. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
  830. SNDRV_PCM_RATE_192000,
  831. .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
  832. },
  833. .ops = &vc4_hdmi_audio_dai_ops,
  834. };
  835. static const struct snd_dmaengine_pcm_config pcm_conf = {
  836. .chan_names[SNDRV_PCM_STREAM_PLAYBACK] = "audio-rx",
  837. .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
  838. };
  839. static int vc4_hdmi_audio_init(struct vc4_hdmi *hdmi)
  840. {
  841. struct snd_soc_dai_link *dai_link = &hdmi->audio.link;
  842. struct snd_soc_card *card = &hdmi->audio.card;
  843. struct device *dev = &hdmi->pdev->dev;
  844. const __be32 *addr;
  845. int ret;
  846. if (!of_find_property(dev->of_node, "dmas", NULL)) {
  847. dev_warn(dev,
  848. "'dmas' DT property is missing, no HDMI audio\n");
  849. return 0;
  850. }
  851. /*
  852. * Get the physical address of VC4_HD_MAI_DATA. We need to retrieve
  853. * the bus address specified in the DT, because the physical address
  854. * (the one returned by platform_get_resource()) is not appropriate
  855. * for DMA transfers.
  856. * This VC/MMU should probably be exposed to avoid this kind of hacks.
  857. */
  858. addr = of_get_address(dev->of_node, 1, NULL, NULL);
  859. hdmi->audio.dma_data.addr = be32_to_cpup(addr) + VC4_HD_MAI_DATA;
  860. hdmi->audio.dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  861. hdmi->audio.dma_data.maxburst = 2;
  862. ret = devm_snd_dmaengine_pcm_register(dev, &pcm_conf, 0);
  863. if (ret) {
  864. dev_err(dev, "Could not register PCM component: %d\n", ret);
  865. return ret;
  866. }
  867. ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_cpu_dai_comp,
  868. &vc4_hdmi_audio_cpu_dai_drv, 1);
  869. if (ret) {
  870. dev_err(dev, "Could not register CPU DAI: %d\n", ret);
  871. return ret;
  872. }
  873. /* register codec and codec dai */
  874. ret = snd_soc_register_codec(dev, &vc4_hdmi_audio_codec_drv,
  875. &vc4_hdmi_audio_codec_dai_drv, 1);
  876. if (ret) {
  877. dev_err(dev, "Could not register codec: %d\n", ret);
  878. return ret;
  879. }
  880. dai_link->name = "MAI";
  881. dai_link->stream_name = "MAI PCM";
  882. dai_link->codec_dai_name = vc4_hdmi_audio_codec_dai_drv.name;
  883. dai_link->cpu_dai_name = dev_name(dev);
  884. dai_link->codec_name = dev_name(dev);
  885. dai_link->platform_name = dev_name(dev);
  886. card->dai_link = dai_link;
  887. card->num_links = 1;
  888. card->name = "vc4-hdmi";
  889. card->dev = dev;
  890. /*
  891. * Be careful, snd_soc_register_card() calls dev_set_drvdata() and
  892. * stores a pointer to the snd card object in dev->driver_data. This
  893. * means we cannot use it for something else. The hdmi back-pointer is
  894. * now stored in card->drvdata and should be retrieved with
  895. * snd_soc_card_get_drvdata() if needed.
  896. */
  897. snd_soc_card_set_drvdata(card, hdmi);
  898. ret = devm_snd_soc_register_card(dev, card);
  899. if (ret) {
  900. dev_err(dev, "Could not register sound card: %d\n", ret);
  901. goto unregister_codec;
  902. }
  903. return 0;
  904. unregister_codec:
  905. snd_soc_unregister_codec(dev);
  906. return ret;
  907. }
  908. static void vc4_hdmi_audio_cleanup(struct vc4_hdmi *hdmi)
  909. {
  910. struct device *dev = &hdmi->pdev->dev;
  911. /*
  912. * If drvdata is not set this means the audio card was not
  913. * registered, just skip codec unregistration in this case.
  914. */
  915. if (dev_get_drvdata(dev))
  916. snd_soc_unregister_codec(dev);
  917. }
  918. static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data)
  919. {
  920. struct platform_device *pdev = to_platform_device(dev);
  921. struct drm_device *drm = dev_get_drvdata(master);
  922. struct vc4_dev *vc4 = drm->dev_private;
  923. struct vc4_hdmi *hdmi;
  924. struct vc4_hdmi_encoder *vc4_hdmi_encoder;
  925. struct device_node *ddc_node;
  926. u32 value;
  927. int ret;
  928. hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
  929. if (!hdmi)
  930. return -ENOMEM;
  931. vc4_hdmi_encoder = devm_kzalloc(dev, sizeof(*vc4_hdmi_encoder),
  932. GFP_KERNEL);
  933. if (!vc4_hdmi_encoder)
  934. return -ENOMEM;
  935. vc4_hdmi_encoder->base.type = VC4_ENCODER_TYPE_HDMI;
  936. hdmi->encoder = &vc4_hdmi_encoder->base.base;
  937. hdmi->pdev = pdev;
  938. hdmi->hdmicore_regs = vc4_ioremap_regs(pdev, 0);
  939. if (IS_ERR(hdmi->hdmicore_regs))
  940. return PTR_ERR(hdmi->hdmicore_regs);
  941. hdmi->hd_regs = vc4_ioremap_regs(pdev, 1);
  942. if (IS_ERR(hdmi->hd_regs))
  943. return PTR_ERR(hdmi->hd_regs);
  944. hdmi->pixel_clock = devm_clk_get(dev, "pixel");
  945. if (IS_ERR(hdmi->pixel_clock)) {
  946. DRM_ERROR("Failed to get pixel clock\n");
  947. return PTR_ERR(hdmi->pixel_clock);
  948. }
  949. hdmi->hsm_clock = devm_clk_get(dev, "hdmi");
  950. if (IS_ERR(hdmi->hsm_clock)) {
  951. DRM_ERROR("Failed to get HDMI state machine clock\n");
  952. return PTR_ERR(hdmi->hsm_clock);
  953. }
  954. ddc_node = of_parse_phandle(dev->of_node, "ddc", 0);
  955. if (!ddc_node) {
  956. DRM_ERROR("Failed to find ddc node in device tree\n");
  957. return -ENODEV;
  958. }
  959. hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node);
  960. of_node_put(ddc_node);
  961. if (!hdmi->ddc) {
  962. DRM_DEBUG("Failed to get ddc i2c adapter by node\n");
  963. return -EPROBE_DEFER;
  964. }
  965. /* Enable the clocks at startup. We can't quite recover from
  966. * turning off the pixel clock during disable/enables yet, so
  967. * it's always running.
  968. */
  969. ret = clk_prepare_enable(hdmi->pixel_clock);
  970. if (ret) {
  971. DRM_ERROR("Failed to turn on pixel clock: %d\n", ret);
  972. goto err_put_i2c;
  973. }
  974. /* This is the rate that is set by the firmware. The number
  975. * needs to be a bit higher than the pixel clock rate
  976. * (generally 148.5Mhz).
  977. */
  978. ret = clk_set_rate(hdmi->hsm_clock, 163682864);
  979. if (ret) {
  980. DRM_ERROR("Failed to set HSM clock rate: %d\n", ret);
  981. goto err_unprepare_pix;
  982. }
  983. ret = clk_prepare_enable(hdmi->hsm_clock);
  984. if (ret) {
  985. DRM_ERROR("Failed to turn on HDMI state machine clock: %d\n",
  986. ret);
  987. goto err_unprepare_pix;
  988. }
  989. /* Only use the GPIO HPD pin if present in the DT, otherwise
  990. * we'll use the HDMI core's register.
  991. */
  992. if (of_find_property(dev->of_node, "hpd-gpios", &value)) {
  993. enum of_gpio_flags hpd_gpio_flags;
  994. hdmi->hpd_gpio = of_get_named_gpio_flags(dev->of_node,
  995. "hpd-gpios", 0,
  996. &hpd_gpio_flags);
  997. if (hdmi->hpd_gpio < 0) {
  998. ret = hdmi->hpd_gpio;
  999. goto err_unprepare_hsm;
  1000. }
  1001. hdmi->hpd_active_low = hpd_gpio_flags & OF_GPIO_ACTIVE_LOW;
  1002. }
  1003. vc4->hdmi = hdmi;
  1004. /* HDMI core must be enabled. */
  1005. if (!(HD_READ(VC4_HD_M_CTL) & VC4_HD_M_ENABLE)) {
  1006. HD_WRITE(VC4_HD_M_CTL, VC4_HD_M_SW_RST);
  1007. udelay(1);
  1008. HD_WRITE(VC4_HD_M_CTL, 0);
  1009. HD_WRITE(VC4_HD_M_CTL, VC4_HD_M_ENABLE);
  1010. HDMI_WRITE(VC4_HDMI_SW_RESET_CONTROL,
  1011. VC4_HDMI_SW_RESET_HDMI |
  1012. VC4_HDMI_SW_RESET_FORMAT_DETECT);
  1013. HDMI_WRITE(VC4_HDMI_SW_RESET_CONTROL, 0);
  1014. /* PHY should be in reset, like
  1015. * vc4_hdmi_encoder_disable() does.
  1016. */
  1017. HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL, 0xf << 16);
  1018. }
  1019. drm_encoder_init(drm, hdmi->encoder, &vc4_hdmi_encoder_funcs,
  1020. DRM_MODE_ENCODER_TMDS, NULL);
  1021. drm_encoder_helper_add(hdmi->encoder, &vc4_hdmi_encoder_helper_funcs);
  1022. hdmi->connector = vc4_hdmi_connector_init(drm, hdmi->encoder);
  1023. if (IS_ERR(hdmi->connector)) {
  1024. ret = PTR_ERR(hdmi->connector);
  1025. goto err_destroy_encoder;
  1026. }
  1027. ret = vc4_hdmi_audio_init(hdmi);
  1028. if (ret)
  1029. goto err_destroy_encoder;
  1030. return 0;
  1031. err_destroy_encoder:
  1032. vc4_hdmi_encoder_destroy(hdmi->encoder);
  1033. err_unprepare_hsm:
  1034. clk_disable_unprepare(hdmi->hsm_clock);
  1035. err_unprepare_pix:
  1036. clk_disable_unprepare(hdmi->pixel_clock);
  1037. err_put_i2c:
  1038. put_device(&hdmi->ddc->dev);
  1039. return ret;
  1040. }
  1041. static void vc4_hdmi_unbind(struct device *dev, struct device *master,
  1042. void *data)
  1043. {
  1044. struct drm_device *drm = dev_get_drvdata(master);
  1045. struct vc4_dev *vc4 = drm->dev_private;
  1046. struct vc4_hdmi *hdmi = vc4->hdmi;
  1047. vc4_hdmi_audio_cleanup(hdmi);
  1048. vc4_hdmi_connector_destroy(hdmi->connector);
  1049. vc4_hdmi_encoder_destroy(hdmi->encoder);
  1050. clk_disable_unprepare(hdmi->pixel_clock);
  1051. clk_disable_unprepare(hdmi->hsm_clock);
  1052. put_device(&hdmi->ddc->dev);
  1053. vc4->hdmi = NULL;
  1054. }
  1055. static const struct component_ops vc4_hdmi_ops = {
  1056. .bind = vc4_hdmi_bind,
  1057. .unbind = vc4_hdmi_unbind,
  1058. };
  1059. static int vc4_hdmi_dev_probe(struct platform_device *pdev)
  1060. {
  1061. return component_add(&pdev->dev, &vc4_hdmi_ops);
  1062. }
  1063. static int vc4_hdmi_dev_remove(struct platform_device *pdev)
  1064. {
  1065. component_del(&pdev->dev, &vc4_hdmi_ops);
  1066. return 0;
  1067. }
  1068. static const struct of_device_id vc4_hdmi_dt_match[] = {
  1069. { .compatible = "brcm,bcm2835-hdmi" },
  1070. {}
  1071. };
  1072. struct platform_driver vc4_hdmi_driver = {
  1073. .probe = vc4_hdmi_dev_probe,
  1074. .remove = vc4_hdmi_dev_remove,
  1075. .driver = {
  1076. .name = "vc4_hdmi",
  1077. .of_match_table = vc4_hdmi_dt_match,
  1078. },
  1079. };