mtk_hdmi_common.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2014 MediaTek Inc.
  4. * Copyright (c) 2024 Collabora Ltd.
  5. * AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
  6. */
  7. #include <drm/drm_modes.h>
  8. #include <linux/device.h>
  9. #include <linux/hdmi.h>
  10. #include <linux/i2c.h>
  11. #include <linux/math.h>
  12. #include <linux/of.h>
  13. #include <linux/of_platform.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/mfd/syscon.h>
  16. #include <sound/hdmi-codec.h>
  17. #include "mtk_hdmi_common.h"
  18. struct hdmi_acr_n {
  19. unsigned int clock;
  20. unsigned int n[3];
  21. };
  22. /* Recommended N values from HDMI specification, tables 7-1 to 7-3 */
  23. static const struct hdmi_acr_n hdmi_rec_n_table[] = {
  24. /* Clock, N: 32kHz 44.1kHz 48kHz */
  25. { 25175, { 4576, 7007, 6864 } },
  26. { 74176, { 11648, 17836, 11648 } },
  27. { 148352, { 11648, 8918, 5824 } },
  28. { 296703, { 5824, 4459, 5824 } },
  29. { 297000, { 3072, 4704, 5120 } },
  30. { 0, { 4096, 6272, 6144 } }, /* all other TMDS clocks */
  31. };
  32. /**
  33. * hdmi_recommended_n() - Return N value recommended by HDMI specification
  34. * @freq: audio sample rate in Hz
  35. * @clock: rounded TMDS clock in kHz
  36. */
  37. static unsigned int hdmi_recommended_n(unsigned int freq, unsigned int clock)
  38. {
  39. const struct hdmi_acr_n *recommended;
  40. unsigned int i;
  41. for (i = 0; i < ARRAY_SIZE(hdmi_rec_n_table) - 1; i++) {
  42. if (clock == hdmi_rec_n_table[i].clock)
  43. break;
  44. }
  45. recommended = hdmi_rec_n_table + i;
  46. switch (freq) {
  47. case 32000:
  48. return recommended->n[0];
  49. case 44100:
  50. return recommended->n[1];
  51. case 48000:
  52. return recommended->n[2];
  53. case 88200:
  54. return recommended->n[1] * 2;
  55. case 96000:
  56. return recommended->n[2] * 2;
  57. case 176400:
  58. return recommended->n[1] * 4;
  59. case 192000:
  60. return recommended->n[2] * 4;
  61. default:
  62. return (128 * freq) / 1000;
  63. }
  64. }
  65. static unsigned int hdmi_mode_clock_to_hz(unsigned int clock)
  66. {
  67. switch (clock) {
  68. case 25175:
  69. return 25174825; /* 25.2/1.001 MHz */
  70. case 74176:
  71. return 74175824; /* 74.25/1.001 MHz */
  72. case 148352:
  73. return 148351648; /* 148.5/1.001 MHz */
  74. case 296703:
  75. return 296703297; /* 297/1.001 MHz */
  76. default:
  77. return clock * 1000;
  78. }
  79. }
  80. static unsigned int hdmi_expected_cts(unsigned int audio_sample_rate,
  81. unsigned int tmds_clock, unsigned int n)
  82. {
  83. return DIV_ROUND_CLOSEST_ULL((u64)hdmi_mode_clock_to_hz(tmds_clock) * n,
  84. 128 * audio_sample_rate);
  85. }
  86. void mtk_hdmi_get_ncts(unsigned int sample_rate, unsigned int clock,
  87. unsigned int *n, unsigned int *cts)
  88. {
  89. *n = hdmi_recommended_n(sample_rate, clock);
  90. *cts = hdmi_expected_cts(sample_rate, clock, *n);
  91. }
  92. EXPORT_SYMBOL_NS_GPL(mtk_hdmi_get_ncts, "DRM_MTK_HDMI");
  93. int mtk_hdmi_audio_params(struct mtk_hdmi *hdmi,
  94. struct hdmi_codec_daifmt *daifmt,
  95. struct hdmi_codec_params *params)
  96. {
  97. struct hdmi_audio_param aud_params = { 0 };
  98. unsigned int chan = params->cea.channels;
  99. dev_dbg(hdmi->dev, "%s: %u Hz, %d bit, %d channels\n", __func__,
  100. params->sample_rate, params->sample_width, chan);
  101. if (!hdmi->bridge.encoder)
  102. return -ENODEV;
  103. switch (chan) {
  104. case 2:
  105. aud_params.aud_input_chan_type = HDMI_AUD_CHAN_TYPE_2_0;
  106. break;
  107. case 4:
  108. aud_params.aud_input_chan_type = HDMI_AUD_CHAN_TYPE_4_0;
  109. break;
  110. case 6:
  111. aud_params.aud_input_chan_type = HDMI_AUD_CHAN_TYPE_5_1;
  112. break;
  113. case 8:
  114. aud_params.aud_input_chan_type = HDMI_AUD_CHAN_TYPE_7_1;
  115. break;
  116. default:
  117. dev_err(hdmi->dev, "channel[%d] not supported!\n", chan);
  118. return -EINVAL;
  119. }
  120. switch (params->sample_rate) {
  121. case 32000:
  122. case 44100:
  123. case 48000:
  124. case 88200:
  125. case 96000:
  126. case 176400:
  127. case 192000:
  128. break;
  129. default:
  130. dev_err(hdmi->dev, "rate[%d] not supported!\n",
  131. params->sample_rate);
  132. return -EINVAL;
  133. }
  134. switch (daifmt->fmt) {
  135. case HDMI_I2S:
  136. aud_params.aud_codec = HDMI_AUDIO_CODING_TYPE_PCM;
  137. aud_params.aud_sample_size = HDMI_AUDIO_SAMPLE_SIZE_16;
  138. aud_params.aud_input_type = HDMI_AUD_INPUT_I2S;
  139. aud_params.aud_i2s_fmt = HDMI_I2S_MODE_I2S_24BIT;
  140. aud_params.aud_mclk = HDMI_AUD_MCLK_128FS;
  141. break;
  142. case HDMI_SPDIF:
  143. aud_params.aud_codec = HDMI_AUDIO_CODING_TYPE_PCM;
  144. aud_params.aud_sample_size = HDMI_AUDIO_SAMPLE_SIZE_16;
  145. aud_params.aud_input_type = HDMI_AUD_INPUT_SPDIF;
  146. break;
  147. default:
  148. dev_err(hdmi->dev, "%s: Invalid DAI format %d\n", __func__,
  149. daifmt->fmt);
  150. return -EINVAL;
  151. }
  152. memcpy(&aud_params.codec_params, params, sizeof(aud_params.codec_params));
  153. memcpy(&hdmi->aud_param, &aud_params, sizeof(aud_params));
  154. dev_dbg(hdmi->dev, "codec:%d, input:%d, channel:%d, fs:%d\n",
  155. aud_params.aud_codec, aud_params.aud_input_type,
  156. aud_params.aud_input_chan_type, aud_params.codec_params.sample_rate);
  157. return 0;
  158. }
  159. EXPORT_SYMBOL_NS_GPL(mtk_hdmi_audio_params, "DRM_MTK_HDMI");
  160. int mtk_hdmi_audio_get_eld(struct device *dev, void *data, uint8_t *buf, size_t len)
  161. {
  162. struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
  163. if (hdmi->enabled)
  164. memcpy(buf, hdmi->curr_conn->eld, min(sizeof(hdmi->curr_conn->eld), len));
  165. else
  166. memset(buf, 0, len);
  167. return 0;
  168. }
  169. EXPORT_SYMBOL_NS_GPL(mtk_hdmi_audio_get_eld, "DRM_MTK_HDMI");
  170. void mtk_hdmi_audio_set_plugged_cb(struct mtk_hdmi *hdmi, hdmi_codec_plugged_cb fn,
  171. struct device *codec_dev)
  172. {
  173. mutex_lock(&hdmi->update_plugged_status_lock);
  174. hdmi->plugged_cb = fn;
  175. hdmi->codec_dev = codec_dev;
  176. mutex_unlock(&hdmi->update_plugged_status_lock);
  177. }
  178. EXPORT_SYMBOL_NS_GPL(mtk_hdmi_audio_set_plugged_cb, "DRM_MTK_HDMI");
  179. static int mtk_hdmi_get_all_clk(struct mtk_hdmi *hdmi, struct device_node *np,
  180. const char * const *clock_names, size_t num_clocks)
  181. {
  182. int i;
  183. for (i = 0; i < num_clocks; i++) {
  184. hdmi->clk[i] = of_clk_get_by_name(np, clock_names[i]);
  185. if (IS_ERR(hdmi->clk[i]))
  186. return PTR_ERR(hdmi->clk[i]);
  187. }
  188. return 0;
  189. }
  190. bool mtk_hdmi_bridge_mode_fixup(struct drm_bridge *bridge,
  191. const struct drm_display_mode *mode,
  192. struct drm_display_mode *adjusted_mode)
  193. {
  194. return true;
  195. }
  196. EXPORT_SYMBOL_NS_GPL(mtk_hdmi_bridge_mode_fixup, "DRM_MTK_HDMI");
  197. void mtk_hdmi_bridge_mode_set(struct drm_bridge *bridge,
  198. const struct drm_display_mode *mode,
  199. const struct drm_display_mode *adjusted_mode)
  200. {
  201. struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge);
  202. dev_dbg(hdmi->dev, "cur info: name:%s, hdisplay:%d\n",
  203. adjusted_mode->name, adjusted_mode->hdisplay);
  204. dev_dbg(hdmi->dev, "hsync_start:%d,hsync_end:%d, htotal:%d",
  205. adjusted_mode->hsync_start, adjusted_mode->hsync_end,
  206. adjusted_mode->htotal);
  207. dev_dbg(hdmi->dev, "hskew:%d, vdisplay:%d\n",
  208. adjusted_mode->hskew, adjusted_mode->vdisplay);
  209. dev_dbg(hdmi->dev, "vsync_start:%d, vsync_end:%d, vtotal:%d",
  210. adjusted_mode->vsync_start, adjusted_mode->vsync_end,
  211. adjusted_mode->vtotal);
  212. dev_dbg(hdmi->dev, "vscan:%d, flag:%d\n",
  213. adjusted_mode->vscan, adjusted_mode->flags);
  214. drm_mode_copy(&hdmi->mode, adjusted_mode);
  215. }
  216. EXPORT_SYMBOL_NS_GPL(mtk_hdmi_bridge_mode_set, "DRM_MTK_HDMI");
  217. static void mtk_hdmi_put_device(void *_dev)
  218. {
  219. struct device *dev = _dev;
  220. put_device(dev);
  221. }
  222. static int mtk_hdmi_get_cec_dev(struct mtk_hdmi *hdmi, struct device *dev, struct device_node *np)
  223. {
  224. struct platform_device *cec_pdev;
  225. struct device_node *cec_np;
  226. int ret;
  227. /* The CEC module handles HDMI hotplug detection */
  228. cec_np = of_get_compatible_child(np->parent, "mediatek,mt8173-cec");
  229. if (!cec_np)
  230. return dev_err_probe(dev, -EOPNOTSUPP, "Failed to find CEC node\n");
  231. cec_pdev = of_find_device_by_node(cec_np);
  232. if (!cec_pdev) {
  233. dev_err(hdmi->dev, "Waiting for CEC device %pOF\n", cec_np);
  234. of_node_put(cec_np);
  235. return -EPROBE_DEFER;
  236. }
  237. of_node_put(cec_np);
  238. ret = devm_add_action_or_reset(dev, mtk_hdmi_put_device, &cec_pdev->dev);
  239. if (ret)
  240. return ret;
  241. /*
  242. * The mediatek,syscon-hdmi property contains a phandle link to the
  243. * MMSYS_CONFIG device and the register offset of the HDMI_SYS_CFG
  244. * registers it contains.
  245. */
  246. hdmi->sys_regmap = syscon_regmap_lookup_by_phandle_args(np, "mediatek,syscon-hdmi",
  247. 1, &hdmi->sys_offset);
  248. if (IS_ERR(hdmi->sys_regmap))
  249. return dev_err_probe(dev, PTR_ERR(hdmi->sys_regmap),
  250. "Failed to get system configuration registers\n");
  251. hdmi->cec_dev = &cec_pdev->dev;
  252. return 0;
  253. }
  254. static int mtk_hdmi_dt_parse_pdata(struct mtk_hdmi *hdmi, struct platform_device *pdev,
  255. const char * const *clk_names, size_t num_clocks)
  256. {
  257. struct device *dev = &pdev->dev;
  258. struct device_node *np = dev->of_node;
  259. struct device_node *remote, *i2c_np;
  260. int ret;
  261. ret = mtk_hdmi_get_all_clk(hdmi, np, clk_names, num_clocks);
  262. if (ret)
  263. return dev_err_probe(dev, ret, "Failed to get clocks\n");
  264. hdmi->irq = platform_get_irq(pdev, 0);
  265. if (hdmi->irq < 0)
  266. return hdmi->irq;
  267. hdmi->regs = device_node_to_regmap(dev->of_node);
  268. if (IS_ERR(hdmi->regs))
  269. return PTR_ERR(hdmi->regs);
  270. remote = of_graph_get_remote_node(np, 1, 0);
  271. if (!remote)
  272. return -EINVAL;
  273. if (!of_device_is_compatible(remote, "hdmi-connector")) {
  274. hdmi->bridge.next_bridge = of_drm_find_and_get_bridge(remote);
  275. if (!hdmi->bridge.next_bridge) {
  276. dev_err(dev, "Waiting for external bridge\n");
  277. of_node_put(remote);
  278. return -EPROBE_DEFER;
  279. }
  280. }
  281. i2c_np = of_parse_phandle(remote, "ddc-i2c-bus", 0);
  282. of_node_put(remote);
  283. if (!i2c_np)
  284. return dev_err_probe(dev, -EINVAL, "No ddc-i2c-bus in connector\n");
  285. hdmi->ddc_adpt = of_find_i2c_adapter_by_node(i2c_np);
  286. of_node_put(i2c_np);
  287. if (!hdmi->ddc_adpt)
  288. return dev_err_probe(dev, -EPROBE_DEFER, "Failed to get ddc i2c adapter by node\n");
  289. ret = devm_add_action_or_reset(dev, mtk_hdmi_put_device, &hdmi->ddc_adpt->dev);
  290. if (ret)
  291. return ret;
  292. ret = mtk_hdmi_get_cec_dev(hdmi, dev, np);
  293. if (ret == -EOPNOTSUPP)
  294. dev_info(dev, "CEC support unavailable: node not found\n");
  295. else if (ret)
  296. return ret;
  297. return 0;
  298. }
  299. static void mtk_hdmi_unregister_audio_driver(void *data)
  300. {
  301. platform_device_unregister(data);
  302. }
  303. static int mtk_hdmi_register_audio_driver(struct device *dev)
  304. {
  305. struct mtk_hdmi *hdmi = dev_get_drvdata(dev);
  306. struct hdmi_audio_param *aud_param = &hdmi->aud_param;
  307. struct hdmi_codec_pdata codec_data = {
  308. .ops = hdmi->conf->ver_conf->codec_ops,
  309. .max_i2s_channels = 2,
  310. .i2s = 1,
  311. .data = hdmi,
  312. .no_capture_mute = 1,
  313. };
  314. int ret;
  315. aud_param->aud_codec = HDMI_AUDIO_CODING_TYPE_PCM;
  316. aud_param->aud_sample_size = HDMI_AUDIO_SAMPLE_SIZE_16;
  317. aud_param->aud_input_type = HDMI_AUD_INPUT_I2S;
  318. aud_param->aud_i2s_fmt = HDMI_I2S_MODE_I2S_24BIT;
  319. aud_param->aud_mclk = HDMI_AUD_MCLK_128FS;
  320. aud_param->aud_input_chan_type = HDMI_AUD_CHAN_TYPE_2_0;
  321. hdmi->audio_pdev = platform_device_register_data(dev,
  322. HDMI_CODEC_DRV_NAME,
  323. PLATFORM_DEVID_AUTO,
  324. &codec_data,
  325. sizeof(codec_data));
  326. if (IS_ERR(hdmi->audio_pdev))
  327. return PTR_ERR(hdmi->audio_pdev);
  328. ret = devm_add_action_or_reset(dev, mtk_hdmi_unregister_audio_driver,
  329. hdmi->audio_pdev);
  330. if (ret)
  331. return ret;
  332. return 0;
  333. }
  334. struct mtk_hdmi *mtk_hdmi_common_probe(struct platform_device *pdev)
  335. {
  336. const struct mtk_hdmi_ver_conf *ver_conf;
  337. const struct mtk_hdmi_conf *hdmi_conf;
  338. struct device *dev = &pdev->dev;
  339. struct mtk_hdmi *hdmi;
  340. int ret;
  341. hdmi_conf = of_device_get_match_data(dev);
  342. if (!hdmi_conf)
  343. return ERR_PTR(-ENODEV);
  344. ver_conf = hdmi_conf->ver_conf;
  345. hdmi = devm_drm_bridge_alloc(dev, struct mtk_hdmi, bridge,
  346. ver_conf->bridge_funcs);
  347. if (IS_ERR(hdmi))
  348. return hdmi;
  349. hdmi->dev = dev;
  350. hdmi->conf = hdmi_conf;
  351. hdmi->clk = devm_kcalloc(dev, ver_conf->num_clocks, sizeof(*hdmi->clk), GFP_KERNEL);
  352. if (!hdmi->clk)
  353. return ERR_PTR(-ENOMEM);
  354. ret = mtk_hdmi_dt_parse_pdata(hdmi, pdev, ver_conf->mtk_hdmi_clock_names,
  355. ver_conf->num_clocks);
  356. if (ret)
  357. return ERR_PTR(ret);
  358. hdmi->phy = devm_phy_get(dev, "hdmi");
  359. if (IS_ERR(hdmi->phy))
  360. return dev_err_cast_probe(dev, hdmi->phy, "Failed to get HDMI PHY\n");
  361. mutex_init(&hdmi->update_plugged_status_lock);
  362. platform_set_drvdata(pdev, hdmi);
  363. ret = mtk_hdmi_register_audio_driver(dev);
  364. if (ret)
  365. return dev_err_ptr_probe(dev, ret, "Cannot register HDMI Audio driver\n");
  366. hdmi->bridge.of_node = pdev->dev.of_node;
  367. hdmi->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID
  368. | DRM_BRIDGE_OP_HPD;
  369. /* Only v2 support OP_HDMI now and it we know that it also support SPD */
  370. if (ver_conf->bridge_funcs->hdmi_write_avi_infoframe &&
  371. ver_conf->bridge_funcs->hdmi_clear_avi_infoframe)
  372. hdmi->bridge.ops |= DRM_BRIDGE_OP_HDMI |
  373. DRM_BRIDGE_OP_HDMI_SPD_INFOFRAME;
  374. hdmi->bridge.type = DRM_MODE_CONNECTOR_HDMIA;
  375. hdmi->bridge.ddc = hdmi->ddc_adpt;
  376. hdmi->bridge.vendor = "MediaTek";
  377. hdmi->bridge.product = "On-Chip HDMI";
  378. hdmi->bridge.interlace_allowed = ver_conf->interlace_allowed;
  379. ret = devm_drm_bridge_add(dev, &hdmi->bridge);
  380. if (ret)
  381. return dev_err_ptr_probe(dev, ret, "Failed to add bridge\n");
  382. return hdmi;
  383. }
  384. EXPORT_SYMBOL_NS_GPL(mtk_hdmi_common_probe, "DRM_MTK_HDMI");
  385. MODULE_AUTHOR("AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>");
  386. MODULE_DESCRIPTION("MediaTek HDMI Common Library");
  387. MODULE_LICENSE("GPL");