vc4_hdmi.c 44 KB

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