zx_hdmi.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. /*
  2. * Copyright 2016 Linaro Ltd.
  3. * Copyright 2016 ZTE Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/component.h>
  12. #include <linux/delay.h>
  13. #include <linux/err.h>
  14. #include <linux/hdmi.h>
  15. #include <linux/irq.h>
  16. #include <linux/mfd/syscon.h>
  17. #include <linux/module.h>
  18. #include <linux/mutex.h>
  19. #include <linux/of_device.h>
  20. #include <drm/drm_atomic_helper.h>
  21. #include <drm/drm_crtc_helper.h>
  22. #include <drm/drm_edid.h>
  23. #include <drm/drm_of.h>
  24. #include <drm/drmP.h>
  25. #include <sound/hdmi-codec.h>
  26. #include "zx_hdmi_regs.h"
  27. #include "zx_vou.h"
  28. #define ZX_HDMI_INFOFRAME_SIZE 31
  29. #define DDC_SEGMENT_ADDR 0x30
  30. struct zx_hdmi_i2c {
  31. struct i2c_adapter adap;
  32. struct mutex lock;
  33. };
  34. struct zx_hdmi {
  35. struct drm_connector connector;
  36. struct drm_encoder encoder;
  37. struct zx_hdmi_i2c *ddc;
  38. struct device *dev;
  39. struct drm_device *drm;
  40. void __iomem *mmio;
  41. struct clk *cec_clk;
  42. struct clk *osc_clk;
  43. struct clk *xclk;
  44. bool sink_is_hdmi;
  45. bool sink_has_audio;
  46. const struct vou_inf *inf;
  47. struct platform_device *audio_pdev;
  48. };
  49. #define to_zx_hdmi(x) container_of(x, struct zx_hdmi, x)
  50. static inline u8 hdmi_readb(struct zx_hdmi *hdmi, u16 offset)
  51. {
  52. return readl_relaxed(hdmi->mmio + offset * 4);
  53. }
  54. static inline void hdmi_writeb(struct zx_hdmi *hdmi, u16 offset, u8 val)
  55. {
  56. writel_relaxed(val, hdmi->mmio + offset * 4);
  57. }
  58. static inline void hdmi_writeb_mask(struct zx_hdmi *hdmi, u16 offset,
  59. u8 mask, u8 val)
  60. {
  61. u8 tmp;
  62. tmp = hdmi_readb(hdmi, offset);
  63. tmp = (tmp & ~mask) | (val & mask);
  64. hdmi_writeb(hdmi, offset, tmp);
  65. }
  66. static int zx_hdmi_infoframe_trans(struct zx_hdmi *hdmi,
  67. union hdmi_infoframe *frame, u8 fsel)
  68. {
  69. u8 buffer[ZX_HDMI_INFOFRAME_SIZE];
  70. int num;
  71. int i;
  72. hdmi_writeb(hdmi, TPI_INFO_FSEL, fsel);
  73. num = hdmi_infoframe_pack(frame, buffer, ZX_HDMI_INFOFRAME_SIZE);
  74. if (num < 0) {
  75. DRM_DEV_ERROR(hdmi->dev, "failed to pack infoframe: %d\n", num);
  76. return num;
  77. }
  78. for (i = 0; i < num; i++)
  79. hdmi_writeb(hdmi, TPI_INFO_B0 + i, buffer[i]);
  80. hdmi_writeb_mask(hdmi, TPI_INFO_EN, TPI_INFO_TRANS_RPT,
  81. TPI_INFO_TRANS_RPT);
  82. hdmi_writeb_mask(hdmi, TPI_INFO_EN, TPI_INFO_TRANS_EN,
  83. TPI_INFO_TRANS_EN);
  84. return num;
  85. }
  86. static int zx_hdmi_config_video_vsi(struct zx_hdmi *hdmi,
  87. struct drm_display_mode *mode)
  88. {
  89. union hdmi_infoframe frame;
  90. int ret;
  91. ret = drm_hdmi_vendor_infoframe_from_display_mode(&frame.vendor.hdmi,
  92. mode);
  93. if (ret) {
  94. DRM_DEV_ERROR(hdmi->dev, "failed to get vendor infoframe: %d\n",
  95. ret);
  96. return ret;
  97. }
  98. return zx_hdmi_infoframe_trans(hdmi, &frame, FSEL_VSIF);
  99. }
  100. static int zx_hdmi_config_video_avi(struct zx_hdmi *hdmi,
  101. struct drm_display_mode *mode)
  102. {
  103. union hdmi_infoframe frame;
  104. int ret;
  105. ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi, mode);
  106. if (ret) {
  107. DRM_DEV_ERROR(hdmi->dev, "failed to get avi infoframe: %d\n",
  108. ret);
  109. return ret;
  110. }
  111. /* We always use YUV444 for HDMI output. */
  112. frame.avi.colorspace = HDMI_COLORSPACE_YUV444;
  113. return zx_hdmi_infoframe_trans(hdmi, &frame, FSEL_AVI);
  114. }
  115. static void zx_hdmi_encoder_mode_set(struct drm_encoder *encoder,
  116. struct drm_display_mode *mode,
  117. struct drm_display_mode *adj_mode)
  118. {
  119. struct zx_hdmi *hdmi = to_zx_hdmi(encoder);
  120. if (hdmi->sink_is_hdmi) {
  121. zx_hdmi_config_video_avi(hdmi, mode);
  122. zx_hdmi_config_video_vsi(hdmi, mode);
  123. }
  124. }
  125. static void zx_hdmi_phy_start(struct zx_hdmi *hdmi)
  126. {
  127. /* Copy from ZTE BSP code */
  128. hdmi_writeb(hdmi, 0x222, 0x0);
  129. hdmi_writeb(hdmi, 0x224, 0x4);
  130. hdmi_writeb(hdmi, 0x909, 0x0);
  131. hdmi_writeb(hdmi, 0x7b0, 0x90);
  132. hdmi_writeb(hdmi, 0x7b1, 0x00);
  133. hdmi_writeb(hdmi, 0x7b2, 0xa7);
  134. hdmi_writeb(hdmi, 0x7b8, 0xaa);
  135. hdmi_writeb(hdmi, 0x7b2, 0xa7);
  136. hdmi_writeb(hdmi, 0x7b3, 0x0f);
  137. hdmi_writeb(hdmi, 0x7b4, 0x0f);
  138. hdmi_writeb(hdmi, 0x7b5, 0x55);
  139. hdmi_writeb(hdmi, 0x7b7, 0x03);
  140. hdmi_writeb(hdmi, 0x7b9, 0x12);
  141. hdmi_writeb(hdmi, 0x7ba, 0x32);
  142. hdmi_writeb(hdmi, 0x7bc, 0x68);
  143. hdmi_writeb(hdmi, 0x7be, 0x40);
  144. hdmi_writeb(hdmi, 0x7bf, 0x84);
  145. hdmi_writeb(hdmi, 0x7c1, 0x0f);
  146. hdmi_writeb(hdmi, 0x7c8, 0x02);
  147. hdmi_writeb(hdmi, 0x7c9, 0x03);
  148. hdmi_writeb(hdmi, 0x7ca, 0x40);
  149. hdmi_writeb(hdmi, 0x7dc, 0x31);
  150. hdmi_writeb(hdmi, 0x7e2, 0x04);
  151. hdmi_writeb(hdmi, 0x7e0, 0x06);
  152. hdmi_writeb(hdmi, 0x7cb, 0x68);
  153. hdmi_writeb(hdmi, 0x7f9, 0x02);
  154. hdmi_writeb(hdmi, 0x7b6, 0x02);
  155. hdmi_writeb(hdmi, 0x7f3, 0x0);
  156. }
  157. static void zx_hdmi_hw_enable(struct zx_hdmi *hdmi)
  158. {
  159. /* Enable pclk */
  160. hdmi_writeb_mask(hdmi, CLKPWD, CLKPWD_PDIDCK, CLKPWD_PDIDCK);
  161. /* Enable HDMI for TX */
  162. hdmi_writeb_mask(hdmi, FUNC_SEL, FUNC_HDMI_EN, FUNC_HDMI_EN);
  163. /* Enable deep color packet */
  164. hdmi_writeb_mask(hdmi, P2T_CTRL, P2T_DC_PKT_EN, P2T_DC_PKT_EN);
  165. /* Enable HDMI/MHL mode for output */
  166. hdmi_writeb_mask(hdmi, TEST_TXCTRL, TEST_TXCTRL_HDMI_MODE,
  167. TEST_TXCTRL_HDMI_MODE);
  168. /* Configure reg_qc_sel */
  169. hdmi_writeb(hdmi, HDMICTL4, 0x3);
  170. /* Enable interrupt */
  171. hdmi_writeb_mask(hdmi, INTR1_MASK, INTR1_MONITOR_DETECT,
  172. INTR1_MONITOR_DETECT);
  173. /* Start up phy */
  174. zx_hdmi_phy_start(hdmi);
  175. }
  176. static void zx_hdmi_hw_disable(struct zx_hdmi *hdmi)
  177. {
  178. /* Disable interrupt */
  179. hdmi_writeb_mask(hdmi, INTR1_MASK, INTR1_MONITOR_DETECT, 0);
  180. /* Disable deep color packet */
  181. hdmi_writeb_mask(hdmi, P2T_CTRL, P2T_DC_PKT_EN, P2T_DC_PKT_EN);
  182. /* Disable HDMI for TX */
  183. hdmi_writeb_mask(hdmi, FUNC_SEL, FUNC_HDMI_EN, 0);
  184. /* Disable pclk */
  185. hdmi_writeb_mask(hdmi, CLKPWD, CLKPWD_PDIDCK, 0);
  186. }
  187. static void zx_hdmi_encoder_enable(struct drm_encoder *encoder)
  188. {
  189. struct zx_hdmi *hdmi = to_zx_hdmi(encoder);
  190. clk_prepare_enable(hdmi->cec_clk);
  191. clk_prepare_enable(hdmi->osc_clk);
  192. clk_prepare_enable(hdmi->xclk);
  193. zx_hdmi_hw_enable(hdmi);
  194. vou_inf_enable(VOU_HDMI, encoder->crtc);
  195. }
  196. static void zx_hdmi_encoder_disable(struct drm_encoder *encoder)
  197. {
  198. struct zx_hdmi *hdmi = to_zx_hdmi(encoder);
  199. vou_inf_disable(VOU_HDMI, encoder->crtc);
  200. zx_hdmi_hw_disable(hdmi);
  201. clk_disable_unprepare(hdmi->xclk);
  202. clk_disable_unprepare(hdmi->osc_clk);
  203. clk_disable_unprepare(hdmi->cec_clk);
  204. }
  205. static const struct drm_encoder_helper_funcs zx_hdmi_encoder_helper_funcs = {
  206. .enable = zx_hdmi_encoder_enable,
  207. .disable = zx_hdmi_encoder_disable,
  208. .mode_set = zx_hdmi_encoder_mode_set,
  209. };
  210. static const struct drm_encoder_funcs zx_hdmi_encoder_funcs = {
  211. .destroy = drm_encoder_cleanup,
  212. };
  213. static int zx_hdmi_connector_get_modes(struct drm_connector *connector)
  214. {
  215. struct zx_hdmi *hdmi = to_zx_hdmi(connector);
  216. struct edid *edid;
  217. int ret;
  218. edid = drm_get_edid(connector, &hdmi->ddc->adap);
  219. if (!edid)
  220. return 0;
  221. hdmi->sink_is_hdmi = drm_detect_hdmi_monitor(edid);
  222. hdmi->sink_has_audio = drm_detect_monitor_audio(edid);
  223. drm_mode_connector_update_edid_property(connector, edid);
  224. ret = drm_add_edid_modes(connector, edid);
  225. kfree(edid);
  226. return ret;
  227. }
  228. static enum drm_mode_status
  229. zx_hdmi_connector_mode_valid(struct drm_connector *connector,
  230. struct drm_display_mode *mode)
  231. {
  232. return MODE_OK;
  233. }
  234. static struct drm_connector_helper_funcs zx_hdmi_connector_helper_funcs = {
  235. .get_modes = zx_hdmi_connector_get_modes,
  236. .mode_valid = zx_hdmi_connector_mode_valid,
  237. };
  238. static enum drm_connector_status
  239. zx_hdmi_connector_detect(struct drm_connector *connector, bool force)
  240. {
  241. struct zx_hdmi *hdmi = to_zx_hdmi(connector);
  242. return (hdmi_readb(hdmi, TPI_HPD_RSEN) & TPI_HPD_CONNECTION) ?
  243. connector_status_connected : connector_status_disconnected;
  244. }
  245. static const struct drm_connector_funcs zx_hdmi_connector_funcs = {
  246. .dpms = drm_atomic_helper_connector_dpms,
  247. .fill_modes = drm_helper_probe_single_connector_modes,
  248. .detect = zx_hdmi_connector_detect,
  249. .destroy = drm_connector_cleanup,
  250. .reset = drm_atomic_helper_connector_reset,
  251. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  252. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  253. };
  254. static int zx_hdmi_register(struct drm_device *drm, struct zx_hdmi *hdmi)
  255. {
  256. struct drm_encoder *encoder = &hdmi->encoder;
  257. encoder->possible_crtcs = VOU_CRTC_MASK;
  258. drm_encoder_init(drm, encoder, &zx_hdmi_encoder_funcs,
  259. DRM_MODE_ENCODER_TMDS, NULL);
  260. drm_encoder_helper_add(encoder, &zx_hdmi_encoder_helper_funcs);
  261. hdmi->connector.polled = DRM_CONNECTOR_POLL_HPD;
  262. drm_connector_init(drm, &hdmi->connector, &zx_hdmi_connector_funcs,
  263. DRM_MODE_CONNECTOR_HDMIA);
  264. drm_connector_helper_add(&hdmi->connector,
  265. &zx_hdmi_connector_helper_funcs);
  266. drm_mode_connector_attach_encoder(&hdmi->connector, encoder);
  267. return 0;
  268. }
  269. static irqreturn_t zx_hdmi_irq_thread(int irq, void *dev_id)
  270. {
  271. struct zx_hdmi *hdmi = dev_id;
  272. drm_helper_hpd_irq_event(hdmi->connector.dev);
  273. return IRQ_HANDLED;
  274. }
  275. static irqreturn_t zx_hdmi_irq_handler(int irq, void *dev_id)
  276. {
  277. struct zx_hdmi *hdmi = dev_id;
  278. u8 lstat;
  279. lstat = hdmi_readb(hdmi, L1_INTR_STAT);
  280. /* Monitor detect/HPD interrupt */
  281. if (lstat & L1_INTR_STAT_INTR1) {
  282. u8 stat;
  283. stat = hdmi_readb(hdmi, INTR1_STAT);
  284. hdmi_writeb(hdmi, INTR1_STAT, stat);
  285. if (stat & INTR1_MONITOR_DETECT)
  286. return IRQ_WAKE_THREAD;
  287. }
  288. return IRQ_NONE;
  289. }
  290. static int zx_hdmi_audio_startup(struct device *dev, void *data)
  291. {
  292. struct zx_hdmi *hdmi = dev_get_drvdata(dev);
  293. struct drm_encoder *encoder = &hdmi->encoder;
  294. vou_inf_hdmi_audio_sel(encoder->crtc, VOU_HDMI_AUD_SPDIF);
  295. return 0;
  296. }
  297. static void zx_hdmi_audio_shutdown(struct device *dev, void *data)
  298. {
  299. struct zx_hdmi *hdmi = dev_get_drvdata(dev);
  300. /* Disable audio input */
  301. hdmi_writeb_mask(hdmi, AUD_EN, AUD_IN_EN, 0);
  302. }
  303. static inline int zx_hdmi_audio_get_n(unsigned int fs)
  304. {
  305. unsigned int n;
  306. if (fs && (fs % 44100) == 0)
  307. n = 6272 * (fs / 44100);
  308. else
  309. n = fs * 128 / 1000;
  310. return n;
  311. }
  312. static int zx_hdmi_audio_hw_params(struct device *dev,
  313. void *data,
  314. struct hdmi_codec_daifmt *daifmt,
  315. struct hdmi_codec_params *params)
  316. {
  317. struct zx_hdmi *hdmi = dev_get_drvdata(dev);
  318. struct hdmi_audio_infoframe *cea = &params->cea;
  319. union hdmi_infoframe frame;
  320. int n;
  321. /* We only support spdif for now */
  322. if (daifmt->fmt != HDMI_SPDIF) {
  323. DRM_DEV_ERROR(hdmi->dev, "invalid daifmt %d\n", daifmt->fmt);
  324. return -EINVAL;
  325. }
  326. switch (params->sample_width) {
  327. case 16:
  328. hdmi_writeb_mask(hdmi, TPI_AUD_CONFIG, SPDIF_SAMPLE_SIZE_MASK,
  329. SPDIF_SAMPLE_SIZE_16BIT);
  330. break;
  331. case 20:
  332. hdmi_writeb_mask(hdmi, TPI_AUD_CONFIG, SPDIF_SAMPLE_SIZE_MASK,
  333. SPDIF_SAMPLE_SIZE_20BIT);
  334. break;
  335. case 24:
  336. hdmi_writeb_mask(hdmi, TPI_AUD_CONFIG, SPDIF_SAMPLE_SIZE_MASK,
  337. SPDIF_SAMPLE_SIZE_24BIT);
  338. break;
  339. default:
  340. DRM_DEV_ERROR(hdmi->dev, "invalid sample width %d\n",
  341. params->sample_width);
  342. return -EINVAL;
  343. }
  344. /* CTS is calculated by hardware, and we only need to take care of N */
  345. n = zx_hdmi_audio_get_n(params->sample_rate);
  346. hdmi_writeb(hdmi, N_SVAL1, n & 0xff);
  347. hdmi_writeb(hdmi, N_SVAL2, (n >> 8) & 0xff);
  348. hdmi_writeb(hdmi, N_SVAL3, (n >> 16) & 0xf);
  349. /* Enable spdif mode */
  350. hdmi_writeb_mask(hdmi, AUD_MODE, SPDIF_EN, SPDIF_EN);
  351. /* Enable audio input */
  352. hdmi_writeb_mask(hdmi, AUD_EN, AUD_IN_EN, AUD_IN_EN);
  353. memcpy(&frame.audio, cea, sizeof(*cea));
  354. return zx_hdmi_infoframe_trans(hdmi, &frame, FSEL_AUDIO);
  355. }
  356. static int zx_hdmi_audio_digital_mute(struct device *dev, void *data,
  357. bool enable)
  358. {
  359. struct zx_hdmi *hdmi = dev_get_drvdata(dev);
  360. if (enable)
  361. hdmi_writeb_mask(hdmi, TPI_AUD_CONFIG, TPI_AUD_MUTE,
  362. TPI_AUD_MUTE);
  363. else
  364. hdmi_writeb_mask(hdmi, TPI_AUD_CONFIG, TPI_AUD_MUTE, 0);
  365. return 0;
  366. }
  367. static int zx_hdmi_audio_get_eld(struct device *dev, void *data,
  368. uint8_t *buf, size_t len)
  369. {
  370. struct zx_hdmi *hdmi = dev_get_drvdata(dev);
  371. struct drm_connector *connector = &hdmi->connector;
  372. memcpy(buf, connector->eld, min(sizeof(connector->eld), len));
  373. return 0;
  374. }
  375. static const struct hdmi_codec_ops zx_hdmi_codec_ops = {
  376. .audio_startup = zx_hdmi_audio_startup,
  377. .hw_params = zx_hdmi_audio_hw_params,
  378. .audio_shutdown = zx_hdmi_audio_shutdown,
  379. .digital_mute = zx_hdmi_audio_digital_mute,
  380. .get_eld = zx_hdmi_audio_get_eld,
  381. };
  382. static struct hdmi_codec_pdata zx_hdmi_codec_pdata = {
  383. .ops = &zx_hdmi_codec_ops,
  384. .spdif = 1,
  385. };
  386. static int zx_hdmi_audio_register(struct zx_hdmi *hdmi)
  387. {
  388. struct platform_device *pdev;
  389. pdev = platform_device_register_data(hdmi->dev, HDMI_CODEC_DRV_NAME,
  390. PLATFORM_DEVID_AUTO,
  391. &zx_hdmi_codec_pdata,
  392. sizeof(zx_hdmi_codec_pdata));
  393. if (IS_ERR(pdev))
  394. return PTR_ERR(pdev);
  395. hdmi->audio_pdev = pdev;
  396. return 0;
  397. }
  398. static int zx_hdmi_i2c_read(struct zx_hdmi *hdmi, struct i2c_msg *msg)
  399. {
  400. int len = msg->len;
  401. u8 *buf = msg->buf;
  402. int retry = 0;
  403. int ret = 0;
  404. /* Bits [9:8] of bytes */
  405. hdmi_writeb(hdmi, ZX_DDC_DIN_CNT2, (len >> 8) & 0xff);
  406. /* Bits [7:0] of bytes */
  407. hdmi_writeb(hdmi, ZX_DDC_DIN_CNT1, len & 0xff);
  408. /* Clear FIFO */
  409. hdmi_writeb_mask(hdmi, ZX_DDC_CMD, DDC_CMD_MASK, DDC_CMD_CLEAR_FIFO);
  410. /* Kick off the read */
  411. hdmi_writeb_mask(hdmi, ZX_DDC_CMD, DDC_CMD_MASK,
  412. DDC_CMD_SEQUENTIAL_READ);
  413. while (len > 0) {
  414. int cnt, i;
  415. /* FIFO needs some time to get ready */
  416. usleep_range(500, 1000);
  417. cnt = hdmi_readb(hdmi, ZX_DDC_DOUT_CNT) & DDC_DOUT_CNT_MASK;
  418. if (cnt == 0) {
  419. if (++retry > 5) {
  420. DRM_DEV_ERROR(hdmi->dev,
  421. "DDC FIFO read timed out!");
  422. return -ETIMEDOUT;
  423. }
  424. continue;
  425. }
  426. for (i = 0; i < cnt; i++)
  427. *buf++ = hdmi_readb(hdmi, ZX_DDC_DATA);
  428. len -= cnt;
  429. }
  430. return ret;
  431. }
  432. static int zx_hdmi_i2c_write(struct zx_hdmi *hdmi, struct i2c_msg *msg)
  433. {
  434. /*
  435. * The DDC I2C adapter is only for reading EDID data, so we assume
  436. * that the write to this adapter must be the EDID data offset.
  437. */
  438. if ((msg->len != 1) ||
  439. ((msg->addr != DDC_ADDR) && (msg->addr != DDC_SEGMENT_ADDR)))
  440. return -EINVAL;
  441. if (msg->addr == DDC_SEGMENT_ADDR)
  442. hdmi_writeb(hdmi, ZX_DDC_SEGM, msg->addr << 1);
  443. else if (msg->addr == DDC_ADDR)
  444. hdmi_writeb(hdmi, ZX_DDC_ADDR, msg->addr << 1);
  445. hdmi_writeb(hdmi, ZX_DDC_OFFSET, msg->buf[0]);
  446. return 0;
  447. }
  448. static int zx_hdmi_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
  449. int num)
  450. {
  451. struct zx_hdmi *hdmi = i2c_get_adapdata(adap);
  452. struct zx_hdmi_i2c *ddc = hdmi->ddc;
  453. int i, ret = 0;
  454. mutex_lock(&ddc->lock);
  455. /* Enable DDC master access */
  456. hdmi_writeb_mask(hdmi, TPI_DDC_MASTER_EN, HW_DDC_MASTER, HW_DDC_MASTER);
  457. for (i = 0; i < num; i++) {
  458. DRM_DEV_DEBUG(hdmi->dev,
  459. "xfer: num: %d/%d, len: %d, flags: %#x\n",
  460. i + 1, num, msgs[i].len, msgs[i].flags);
  461. if (msgs[i].flags & I2C_M_RD)
  462. ret = zx_hdmi_i2c_read(hdmi, &msgs[i]);
  463. else
  464. ret = zx_hdmi_i2c_write(hdmi, &msgs[i]);
  465. if (ret < 0)
  466. break;
  467. }
  468. if (!ret)
  469. ret = num;
  470. /* Disable DDC master access */
  471. hdmi_writeb_mask(hdmi, TPI_DDC_MASTER_EN, HW_DDC_MASTER, 0);
  472. mutex_unlock(&ddc->lock);
  473. return ret;
  474. }
  475. static u32 zx_hdmi_i2c_func(struct i2c_adapter *adapter)
  476. {
  477. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  478. }
  479. static const struct i2c_algorithm zx_hdmi_algorithm = {
  480. .master_xfer = zx_hdmi_i2c_xfer,
  481. .functionality = zx_hdmi_i2c_func,
  482. };
  483. static int zx_hdmi_ddc_register(struct zx_hdmi *hdmi)
  484. {
  485. struct i2c_adapter *adap;
  486. struct zx_hdmi_i2c *ddc;
  487. int ret;
  488. ddc = devm_kzalloc(hdmi->dev, sizeof(*ddc), GFP_KERNEL);
  489. if (!ddc)
  490. return -ENOMEM;
  491. hdmi->ddc = ddc;
  492. mutex_init(&ddc->lock);
  493. adap = &ddc->adap;
  494. adap->owner = THIS_MODULE;
  495. adap->class = I2C_CLASS_DDC;
  496. adap->dev.parent = hdmi->dev;
  497. adap->algo = &zx_hdmi_algorithm;
  498. snprintf(adap->name, sizeof(adap->name), "zx hdmi i2c");
  499. ret = i2c_add_adapter(adap);
  500. if (ret) {
  501. DRM_DEV_ERROR(hdmi->dev, "failed to add I2C adapter: %d\n",
  502. ret);
  503. return ret;
  504. }
  505. i2c_set_adapdata(adap, hdmi);
  506. return 0;
  507. }
  508. static int zx_hdmi_bind(struct device *dev, struct device *master, void *data)
  509. {
  510. struct platform_device *pdev = to_platform_device(dev);
  511. struct drm_device *drm = data;
  512. struct resource *res;
  513. struct zx_hdmi *hdmi;
  514. int irq;
  515. int ret;
  516. hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
  517. if (!hdmi)
  518. return -ENOMEM;
  519. hdmi->dev = dev;
  520. hdmi->drm = drm;
  521. dev_set_drvdata(dev, hdmi);
  522. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  523. hdmi->mmio = devm_ioremap_resource(dev, res);
  524. if (IS_ERR(hdmi->mmio)) {
  525. ret = PTR_ERR(hdmi->mmio);
  526. DRM_DEV_ERROR(dev, "failed to remap hdmi region: %d\n", ret);
  527. return ret;
  528. }
  529. irq = platform_get_irq(pdev, 0);
  530. if (irq < 0)
  531. return irq;
  532. hdmi->cec_clk = devm_clk_get(hdmi->dev, "osc_cec");
  533. if (IS_ERR(hdmi->cec_clk)) {
  534. ret = PTR_ERR(hdmi->cec_clk);
  535. DRM_DEV_ERROR(dev, "failed to get cec_clk: %d\n", ret);
  536. return ret;
  537. }
  538. hdmi->osc_clk = devm_clk_get(hdmi->dev, "osc_clk");
  539. if (IS_ERR(hdmi->osc_clk)) {
  540. ret = PTR_ERR(hdmi->osc_clk);
  541. DRM_DEV_ERROR(dev, "failed to get osc_clk: %d\n", ret);
  542. return ret;
  543. }
  544. hdmi->xclk = devm_clk_get(hdmi->dev, "xclk");
  545. if (IS_ERR(hdmi->xclk)) {
  546. ret = PTR_ERR(hdmi->xclk);
  547. DRM_DEV_ERROR(dev, "failed to get xclk: %d\n", ret);
  548. return ret;
  549. }
  550. ret = zx_hdmi_ddc_register(hdmi);
  551. if (ret) {
  552. DRM_DEV_ERROR(dev, "failed to register ddc: %d\n", ret);
  553. return ret;
  554. }
  555. ret = zx_hdmi_audio_register(hdmi);
  556. if (ret) {
  557. DRM_DEV_ERROR(dev, "failed to register audio: %d\n", ret);
  558. return ret;
  559. }
  560. ret = zx_hdmi_register(drm, hdmi);
  561. if (ret) {
  562. DRM_DEV_ERROR(dev, "failed to register hdmi: %d\n", ret);
  563. return ret;
  564. }
  565. ret = devm_request_threaded_irq(dev, irq, zx_hdmi_irq_handler,
  566. zx_hdmi_irq_thread, IRQF_SHARED,
  567. dev_name(dev), hdmi);
  568. if (ret) {
  569. DRM_DEV_ERROR(dev, "failed to request threaded irq: %d\n", ret);
  570. return ret;
  571. }
  572. return 0;
  573. }
  574. static void zx_hdmi_unbind(struct device *dev, struct device *master,
  575. void *data)
  576. {
  577. struct zx_hdmi *hdmi = dev_get_drvdata(dev);
  578. hdmi->connector.funcs->destroy(&hdmi->connector);
  579. hdmi->encoder.funcs->destroy(&hdmi->encoder);
  580. if (hdmi->audio_pdev)
  581. platform_device_unregister(hdmi->audio_pdev);
  582. }
  583. static const struct component_ops zx_hdmi_component_ops = {
  584. .bind = zx_hdmi_bind,
  585. .unbind = zx_hdmi_unbind,
  586. };
  587. static int zx_hdmi_probe(struct platform_device *pdev)
  588. {
  589. return component_add(&pdev->dev, &zx_hdmi_component_ops);
  590. }
  591. static int zx_hdmi_remove(struct platform_device *pdev)
  592. {
  593. component_del(&pdev->dev, &zx_hdmi_component_ops);
  594. return 0;
  595. }
  596. static const struct of_device_id zx_hdmi_of_match[] = {
  597. { .compatible = "zte,zx296718-hdmi", },
  598. { /* end */ },
  599. };
  600. MODULE_DEVICE_TABLE(of, zx_hdmi_of_match);
  601. struct platform_driver zx_hdmi_driver = {
  602. .probe = zx_hdmi_probe,
  603. .remove = zx_hdmi_remove,
  604. .driver = {
  605. .name = "zx-hdmi",
  606. .of_match_table = zx_hdmi_of_match,
  607. },
  608. };