hdmi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  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. #include <linux/gpio/consumer.h>
  8. #include <linux/of_irq.h>
  9. #include <linux/of_platform.h>
  10. #include <linux/platform_device.h>
  11. #include <drm/drm_bridge_connector.h>
  12. #include <drm/drm_of.h>
  13. #include <sound/hdmi-codec.h>
  14. #include "hdmi.h"
  15. void msm_hdmi_set_mode(struct hdmi *hdmi, bool power_on)
  16. {
  17. uint32_t ctrl = 0;
  18. unsigned long flags;
  19. spin_lock_irqsave(&hdmi->reg_lock, flags);
  20. if (power_on) {
  21. ctrl |= HDMI_CTRL_ENABLE;
  22. if (!hdmi->hdmi_mode) {
  23. ctrl |= HDMI_CTRL_HDMI;
  24. hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
  25. ctrl &= ~HDMI_CTRL_HDMI;
  26. } else {
  27. ctrl |= HDMI_CTRL_HDMI;
  28. }
  29. } else {
  30. ctrl = HDMI_CTRL_HDMI;
  31. }
  32. hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
  33. spin_unlock_irqrestore(&hdmi->reg_lock, flags);
  34. DBG("HDMI Core: %s, HDMI_CTRL=0x%08x",
  35. power_on ? "Enable" : "Disable", ctrl);
  36. }
  37. static irqreturn_t msm_hdmi_irq(int irq, void *dev_id)
  38. {
  39. struct hdmi *hdmi = dev_id;
  40. /* Process HPD: */
  41. msm_hdmi_hpd_irq(hdmi->bridge);
  42. /* Process DDC: */
  43. msm_hdmi_i2c_irq(hdmi->i2c);
  44. /* Process HDCP: */
  45. if (hdmi->hdcp_ctrl)
  46. msm_hdmi_hdcp_irq(hdmi->hdcp_ctrl);
  47. /* TODO audio.. */
  48. return IRQ_HANDLED;
  49. }
  50. static void msm_hdmi_destroy(struct hdmi *hdmi)
  51. {
  52. /*
  53. * at this point, hpd has been disabled,
  54. * after flush workq, it's safe to deinit hdcp
  55. */
  56. if (hdmi->workq)
  57. destroy_workqueue(hdmi->workq);
  58. msm_hdmi_hdcp_destroy(hdmi);
  59. if (hdmi->i2c)
  60. msm_hdmi_i2c_destroy(hdmi->i2c);
  61. }
  62. static void msm_hdmi_put_phy(struct hdmi *hdmi)
  63. {
  64. if (hdmi->phy_dev) {
  65. put_device(hdmi->phy_dev);
  66. hdmi->phy = NULL;
  67. hdmi->phy_dev = NULL;
  68. }
  69. }
  70. static int msm_hdmi_get_phy(struct hdmi *hdmi)
  71. {
  72. struct platform_device *pdev = hdmi->pdev;
  73. struct platform_device *phy_pdev;
  74. struct device_node *phy_node;
  75. phy_node = of_parse_phandle(pdev->dev.of_node, "phys", 0);
  76. if (!phy_node) {
  77. DRM_DEV_ERROR(&pdev->dev, "cannot find phy device\n");
  78. return -ENXIO;
  79. }
  80. phy_pdev = of_find_device_by_node(phy_node);
  81. of_node_put(phy_node);
  82. if (!phy_pdev)
  83. return dev_err_probe(&pdev->dev, -EPROBE_DEFER, "phy driver is not ready\n");
  84. hdmi->phy = platform_get_drvdata(phy_pdev);
  85. if (!hdmi->phy) {
  86. put_device(&phy_pdev->dev);
  87. return dev_err_probe(&pdev->dev, -EPROBE_DEFER, "phy driver is not ready\n");
  88. }
  89. hdmi->phy_dev = &phy_pdev->dev;
  90. return 0;
  91. }
  92. /* construct hdmi at bind/probe time, grab all the resources. If
  93. * we are to EPROBE_DEFER we want to do it here, rather than later
  94. * at modeset_init() time
  95. */
  96. static int msm_hdmi_init(struct hdmi *hdmi)
  97. {
  98. struct platform_device *pdev = hdmi->pdev;
  99. int ret;
  100. hdmi->workq = alloc_ordered_workqueue("msm_hdmi", 0);
  101. if (!hdmi->workq) {
  102. ret = -ENOMEM;
  103. goto fail;
  104. }
  105. hdmi->i2c = msm_hdmi_i2c_init(hdmi);
  106. if (IS_ERR(hdmi->i2c)) {
  107. ret = PTR_ERR(hdmi->i2c);
  108. DRM_DEV_ERROR(&pdev->dev, "failed to get i2c: %d\n", ret);
  109. hdmi->i2c = NULL;
  110. goto fail;
  111. }
  112. hdmi->hdcp_ctrl = msm_hdmi_hdcp_init(hdmi);
  113. if (IS_ERR(hdmi->hdcp_ctrl)) {
  114. dev_warn(&pdev->dev, "failed to init hdcp: disabled\n");
  115. hdmi->hdcp_ctrl = NULL;
  116. }
  117. return 0;
  118. fail:
  119. msm_hdmi_destroy(hdmi);
  120. return ret;
  121. }
  122. /* Second part of initialization, the drm/kms level modeset_init,
  123. * constructs/initializes mode objects, etc, is called from master
  124. * driver (not hdmi sub-device's probe/bind!)
  125. *
  126. * Any resource (regulator/clk/etc) which could be missing at boot
  127. * should be handled in msm_hdmi_init() so that failure happens from
  128. * hdmi sub-device's probe.
  129. */
  130. int msm_hdmi_modeset_init(struct hdmi *hdmi,
  131. struct drm_device *dev, struct drm_encoder *encoder)
  132. {
  133. int ret;
  134. hdmi->dev = dev;
  135. hdmi->encoder = encoder;
  136. hdmi_audio_infoframe_init(&hdmi->audio.infoframe);
  137. ret = msm_hdmi_bridge_init(hdmi);
  138. if (ret) {
  139. DRM_DEV_ERROR(dev->dev, "failed to create HDMI bridge: %d\n", ret);
  140. goto fail;
  141. }
  142. if (hdmi->next_bridge) {
  143. ret = drm_bridge_attach(hdmi->encoder, hdmi->next_bridge, hdmi->bridge,
  144. DRM_BRIDGE_ATTACH_NO_CONNECTOR);
  145. if (ret) {
  146. DRM_DEV_ERROR(dev->dev, "failed to attach next HDMI bridge: %d\n", ret);
  147. goto fail;
  148. }
  149. }
  150. hdmi->connector = drm_bridge_connector_init(hdmi->dev, encoder);
  151. if (IS_ERR(hdmi->connector)) {
  152. ret = PTR_ERR(hdmi->connector);
  153. DRM_DEV_ERROR(dev->dev, "failed to create HDMI connector: %d\n", ret);
  154. hdmi->connector = NULL;
  155. goto fail;
  156. }
  157. drm_connector_attach_encoder(hdmi->connector, hdmi->encoder);
  158. ret = devm_request_irq(dev->dev, hdmi->irq,
  159. msm_hdmi_irq, IRQF_TRIGGER_HIGH,
  160. "hdmi_isr", hdmi);
  161. if (ret < 0) {
  162. DRM_DEV_ERROR(dev->dev, "failed to request IRQ%u: %d\n",
  163. hdmi->irq, ret);
  164. goto fail;
  165. }
  166. ret = msm_hdmi_hpd_enable(hdmi->bridge);
  167. if (ret < 0) {
  168. DRM_DEV_ERROR(&hdmi->pdev->dev, "failed to enable HPD: %d\n", ret);
  169. goto fail;
  170. }
  171. return 0;
  172. fail:
  173. if (hdmi->connector) {
  174. hdmi->connector->funcs->destroy(hdmi->connector);
  175. hdmi->connector = NULL;
  176. }
  177. return ret;
  178. }
  179. /*
  180. * The hdmi device:
  181. */
  182. #define HDMI_CFG(item, entry) \
  183. .item ## _names = item ##_names_ ## entry, \
  184. .item ## _cnt = ARRAY_SIZE(item ## _names_ ## entry)
  185. static const char *hpd_reg_names_8960[] = {"core-vdda"};
  186. static const char *hpd_clk_names_8960[] = {"core", "master_iface", "slave_iface"};
  187. static const struct hdmi_platform_config hdmi_tx_8960_config = {
  188. HDMI_CFG(hpd_reg, 8960),
  189. HDMI_CFG(hpd_clk, 8960),
  190. };
  191. static const char *pwr_reg_names_8x74[] = {"core-vdda", "core-vcc"};
  192. static const char *pwr_clk_names_8x74[] = {"extp", "alt_iface"};
  193. static const char *hpd_clk_names_8x74[] = {"iface", "core", "mdp_core"};
  194. static unsigned long hpd_clk_freq_8x74[] = {0, 19200000, 0};
  195. static const struct hdmi_platform_config hdmi_tx_8974_config = {
  196. HDMI_CFG(pwr_reg, 8x74),
  197. HDMI_CFG(pwr_clk, 8x74),
  198. HDMI_CFG(hpd_clk, 8x74),
  199. .hpd_freq = hpd_clk_freq_8x74,
  200. };
  201. /*
  202. * HDMI audio codec callbacks
  203. */
  204. static int msm_hdmi_audio_hw_params(struct device *dev, void *data,
  205. struct hdmi_codec_daifmt *daifmt,
  206. struct hdmi_codec_params *params)
  207. {
  208. struct hdmi *hdmi = dev_get_drvdata(dev);
  209. unsigned int chan;
  210. unsigned int channel_allocation = 0;
  211. unsigned int rate;
  212. unsigned int level_shift = 0; /* 0dB */
  213. bool down_mix = false;
  214. DRM_DEV_DEBUG(dev, "%u Hz, %d bit, %d channels\n", params->sample_rate,
  215. params->sample_width, params->cea.channels);
  216. switch (params->cea.channels) {
  217. case 2:
  218. /* FR and FL speakers */
  219. channel_allocation = 0;
  220. chan = MSM_HDMI_AUDIO_CHANNEL_2;
  221. break;
  222. case 4:
  223. /* FC, LFE, FR and FL speakers */
  224. channel_allocation = 0x3;
  225. chan = MSM_HDMI_AUDIO_CHANNEL_4;
  226. break;
  227. case 6:
  228. /* RR, RL, FC, LFE, FR and FL speakers */
  229. channel_allocation = 0x0B;
  230. chan = MSM_HDMI_AUDIO_CHANNEL_6;
  231. break;
  232. case 8:
  233. /* FRC, FLC, RR, RL, FC, LFE, FR and FL speakers */
  234. channel_allocation = 0x1F;
  235. chan = MSM_HDMI_AUDIO_CHANNEL_8;
  236. break;
  237. default:
  238. return -EINVAL;
  239. }
  240. switch (params->sample_rate) {
  241. case 32000:
  242. rate = HDMI_SAMPLE_RATE_32KHZ;
  243. break;
  244. case 44100:
  245. rate = HDMI_SAMPLE_RATE_44_1KHZ;
  246. break;
  247. case 48000:
  248. rate = HDMI_SAMPLE_RATE_48KHZ;
  249. break;
  250. case 88200:
  251. rate = HDMI_SAMPLE_RATE_88_2KHZ;
  252. break;
  253. case 96000:
  254. rate = HDMI_SAMPLE_RATE_96KHZ;
  255. break;
  256. case 176400:
  257. rate = HDMI_SAMPLE_RATE_176_4KHZ;
  258. break;
  259. case 192000:
  260. rate = HDMI_SAMPLE_RATE_192KHZ;
  261. break;
  262. default:
  263. DRM_DEV_ERROR(dev, "rate[%d] not supported!\n",
  264. params->sample_rate);
  265. return -EINVAL;
  266. }
  267. msm_hdmi_audio_set_sample_rate(hdmi, rate);
  268. msm_hdmi_audio_info_setup(hdmi, 1, chan, channel_allocation,
  269. level_shift, down_mix);
  270. return 0;
  271. }
  272. static void msm_hdmi_audio_shutdown(struct device *dev, void *data)
  273. {
  274. struct hdmi *hdmi = dev_get_drvdata(dev);
  275. msm_hdmi_audio_info_setup(hdmi, 0, 0, 0, 0, 0);
  276. }
  277. static const struct hdmi_codec_ops msm_hdmi_audio_codec_ops = {
  278. .hw_params = msm_hdmi_audio_hw_params,
  279. .audio_shutdown = msm_hdmi_audio_shutdown,
  280. };
  281. static struct hdmi_codec_pdata codec_data = {
  282. .ops = &msm_hdmi_audio_codec_ops,
  283. .max_i2s_channels = 8,
  284. .i2s = 1,
  285. };
  286. static int msm_hdmi_register_audio_driver(struct hdmi *hdmi, struct device *dev)
  287. {
  288. hdmi->audio_pdev = platform_device_register_data(dev,
  289. HDMI_CODEC_DRV_NAME,
  290. PLATFORM_DEVID_AUTO,
  291. &codec_data,
  292. sizeof(codec_data));
  293. return PTR_ERR_OR_ZERO(hdmi->audio_pdev);
  294. }
  295. static int msm_hdmi_bind(struct device *dev, struct device *master, void *data)
  296. {
  297. struct msm_drm_private *priv = dev_get_drvdata(master);
  298. struct hdmi *hdmi = dev_get_drvdata(dev);
  299. int err;
  300. err = msm_hdmi_init(hdmi);
  301. if (err)
  302. return err;
  303. priv->hdmi = hdmi;
  304. err = msm_hdmi_register_audio_driver(hdmi, dev);
  305. if (err) {
  306. DRM_ERROR("Failed to attach an audio codec %d\n", err);
  307. hdmi->audio_pdev = NULL;
  308. }
  309. return 0;
  310. }
  311. static void msm_hdmi_unbind(struct device *dev, struct device *master,
  312. void *data)
  313. {
  314. struct msm_drm_private *priv = dev_get_drvdata(master);
  315. if (priv->hdmi) {
  316. if (priv->hdmi->audio_pdev)
  317. platform_device_unregister(priv->hdmi->audio_pdev);
  318. if (priv->hdmi->bridge)
  319. msm_hdmi_hpd_disable(priv->hdmi);
  320. msm_hdmi_destroy(priv->hdmi);
  321. priv->hdmi = NULL;
  322. }
  323. }
  324. static const struct component_ops msm_hdmi_ops = {
  325. .bind = msm_hdmi_bind,
  326. .unbind = msm_hdmi_unbind,
  327. };
  328. static int msm_hdmi_dev_probe(struct platform_device *pdev)
  329. {
  330. const struct hdmi_platform_config *config;
  331. struct device *dev = &pdev->dev;
  332. struct hdmi *hdmi;
  333. struct resource *res;
  334. int i, ret;
  335. config = of_device_get_match_data(dev);
  336. if (!config)
  337. return -EINVAL;
  338. hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
  339. if (!hdmi)
  340. return -ENOMEM;
  341. hdmi->pdev = pdev;
  342. hdmi->config = config;
  343. spin_lock_init(&hdmi->reg_lock);
  344. ret = drm_of_find_panel_or_bridge(pdev->dev.of_node, 1, 0, NULL, &hdmi->next_bridge);
  345. if (ret && ret != -ENODEV)
  346. return ret;
  347. hdmi->mmio = msm_ioremap(pdev, "core_physical");
  348. if (IS_ERR(hdmi->mmio))
  349. return PTR_ERR(hdmi->mmio);
  350. /* HDCP needs physical address of hdmi register */
  351. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  352. "core_physical");
  353. if (!res)
  354. return -EINVAL;
  355. hdmi->mmio_phy_addr = res->start;
  356. hdmi->qfprom_mmio = msm_ioremap(pdev, "qfprom_physical");
  357. if (IS_ERR(hdmi->qfprom_mmio)) {
  358. DRM_DEV_INFO(&pdev->dev, "can't find qfprom resource\n");
  359. hdmi->qfprom_mmio = NULL;
  360. }
  361. hdmi->irq = platform_get_irq(pdev, 0);
  362. if (hdmi->irq < 0)
  363. return hdmi->irq;
  364. hdmi->hpd_regs = devm_kcalloc(&pdev->dev,
  365. config->hpd_reg_cnt,
  366. sizeof(hdmi->hpd_regs[0]),
  367. GFP_KERNEL);
  368. if (!hdmi->hpd_regs)
  369. return -ENOMEM;
  370. for (i = 0; i < config->hpd_reg_cnt; i++)
  371. hdmi->hpd_regs[i].supply = config->hpd_reg_names[i];
  372. ret = devm_regulator_bulk_get(&pdev->dev, config->hpd_reg_cnt, hdmi->hpd_regs);
  373. if (ret)
  374. return dev_err_probe(dev, ret, "failed to get hpd regulators\n");
  375. hdmi->pwr_regs = devm_kcalloc(&pdev->dev,
  376. config->pwr_reg_cnt,
  377. sizeof(hdmi->pwr_regs[0]),
  378. GFP_KERNEL);
  379. if (!hdmi->pwr_regs)
  380. return -ENOMEM;
  381. for (i = 0; i < config->pwr_reg_cnt; i++)
  382. hdmi->pwr_regs[i].supply = config->pwr_reg_names[i];
  383. ret = devm_regulator_bulk_get(&pdev->dev, config->pwr_reg_cnt, hdmi->pwr_regs);
  384. if (ret)
  385. return dev_err_probe(dev, ret, "failed to get pwr regulators\n");
  386. hdmi->hpd_clks = devm_kcalloc(&pdev->dev,
  387. config->hpd_clk_cnt,
  388. sizeof(hdmi->hpd_clks[0]),
  389. GFP_KERNEL);
  390. if (!hdmi->hpd_clks)
  391. return -ENOMEM;
  392. for (i = 0; i < config->hpd_clk_cnt; i++) {
  393. struct clk *clk;
  394. clk = msm_clk_get(pdev, config->hpd_clk_names[i]);
  395. if (IS_ERR(clk))
  396. return dev_err_probe(dev, PTR_ERR(clk),
  397. "failed to get hpd clk: %s\n",
  398. config->hpd_clk_names[i]);
  399. hdmi->hpd_clks[i] = clk;
  400. }
  401. hdmi->pwr_clks = devm_kcalloc(&pdev->dev,
  402. config->pwr_clk_cnt,
  403. sizeof(hdmi->pwr_clks[0]),
  404. GFP_KERNEL);
  405. if (!hdmi->pwr_clks)
  406. return -ENOMEM;
  407. for (i = 0; i < config->pwr_clk_cnt; i++) {
  408. struct clk *clk;
  409. clk = msm_clk_get(pdev, config->pwr_clk_names[i]);
  410. if (IS_ERR(clk))
  411. return dev_err_probe(dev, PTR_ERR(clk),
  412. "failed to get pwr clk: %s\n",
  413. config->pwr_clk_names[i]);
  414. hdmi->pwr_clks[i] = clk;
  415. }
  416. hdmi->hpd_gpiod = devm_gpiod_get_optional(&pdev->dev, "hpd", GPIOD_IN);
  417. /* This will catch e.g. -EPROBE_DEFER */
  418. if (IS_ERR(hdmi->hpd_gpiod))
  419. return dev_err_probe(dev, PTR_ERR(hdmi->hpd_gpiod),
  420. "failed to get hpd gpio\n");
  421. if (!hdmi->hpd_gpiod)
  422. DBG("failed to get HPD gpio");
  423. if (hdmi->hpd_gpiod)
  424. gpiod_set_consumer_name(hdmi->hpd_gpiod, "HDMI_HPD");
  425. ret = msm_hdmi_get_phy(hdmi);
  426. if (ret) {
  427. DRM_DEV_ERROR(&pdev->dev, "failed to get phy\n");
  428. return ret;
  429. }
  430. ret = devm_pm_runtime_enable(&pdev->dev);
  431. if (ret)
  432. goto err_put_phy;
  433. platform_set_drvdata(pdev, hdmi);
  434. ret = component_add(&pdev->dev, &msm_hdmi_ops);
  435. if (ret)
  436. goto err_put_phy;
  437. return 0;
  438. err_put_phy:
  439. msm_hdmi_put_phy(hdmi);
  440. return ret;
  441. }
  442. static void msm_hdmi_dev_remove(struct platform_device *pdev)
  443. {
  444. struct hdmi *hdmi = dev_get_drvdata(&pdev->dev);
  445. component_del(&pdev->dev, &msm_hdmi_ops);
  446. msm_hdmi_put_phy(hdmi);
  447. }
  448. static const struct of_device_id msm_hdmi_dt_match[] = {
  449. { .compatible = "qcom,hdmi-tx-8998", .data = &hdmi_tx_8974_config },
  450. { .compatible = "qcom,hdmi-tx-8996", .data = &hdmi_tx_8974_config },
  451. { .compatible = "qcom,hdmi-tx-8994", .data = &hdmi_tx_8974_config },
  452. { .compatible = "qcom,hdmi-tx-8084", .data = &hdmi_tx_8974_config },
  453. { .compatible = "qcom,hdmi-tx-8974", .data = &hdmi_tx_8974_config },
  454. { .compatible = "qcom,hdmi-tx-8960", .data = &hdmi_tx_8960_config },
  455. { .compatible = "qcom,hdmi-tx-8660", .data = &hdmi_tx_8960_config },
  456. {}
  457. };
  458. static struct platform_driver msm_hdmi_driver = {
  459. .probe = msm_hdmi_dev_probe,
  460. .remove_new = msm_hdmi_dev_remove,
  461. .driver = {
  462. .name = "hdmi_msm",
  463. .of_match_table = msm_hdmi_dt_match,
  464. },
  465. };
  466. void __init msm_hdmi_register(void)
  467. {
  468. msm_hdmi_phy_driver_register();
  469. platform_driver_register(&msm_hdmi_driver);
  470. }
  471. void __exit msm_hdmi_unregister(void)
  472. {
  473. platform_driver_unregister(&msm_hdmi_driver);
  474. msm_hdmi_phy_driver_unregister();
  475. }