vc4_hdmi.c 43 KB

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