vc4_hdmi.c 36 KB

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