hdmi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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/pinctrl/consumer.h>
  11. #include <linux/platform_device.h>
  12. #include <drm/drm_bridge_connector.h>
  13. #include <drm/drm_of.h>
  14. #include <drm/display/drm_hdmi_state_helper.h>
  15. #include "msm_kms.h"
  16. #include "hdmi.h"
  17. void msm_hdmi_set_mode(struct hdmi *hdmi, bool power_on)
  18. {
  19. uint32_t ctrl = 0;
  20. unsigned long flags;
  21. spin_lock_irqsave(&hdmi->reg_lock, flags);
  22. if (power_on) {
  23. ctrl |= HDMI_CTRL_ENABLE;
  24. if (!hdmi->connector->display_info.is_hdmi) {
  25. ctrl |= HDMI_CTRL_HDMI;
  26. hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
  27. ctrl &= ~HDMI_CTRL_HDMI;
  28. } else {
  29. ctrl |= HDMI_CTRL_HDMI;
  30. }
  31. } else {
  32. ctrl = HDMI_CTRL_HDMI;
  33. }
  34. hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
  35. spin_unlock_irqrestore(&hdmi->reg_lock, flags);
  36. DBG("HDMI Core: %s, HDMI_CTRL=0x%08x",
  37. power_on ? "Enable" : "Disable", ctrl);
  38. }
  39. static irqreturn_t msm_hdmi_irq(int irq, void *dev_id)
  40. {
  41. struct hdmi *hdmi = dev_id;
  42. /* Process HPD: */
  43. msm_hdmi_hpd_irq(hdmi->bridge);
  44. /* Process DDC: */
  45. msm_hdmi_i2c_irq(hdmi->i2c);
  46. /* Process HDCP: */
  47. if (hdmi->hdcp_ctrl)
  48. msm_hdmi_hdcp_irq(hdmi->hdcp_ctrl);
  49. /* TODO audio.. */
  50. return IRQ_HANDLED;
  51. }
  52. static void msm_hdmi_destroy(struct hdmi *hdmi)
  53. {
  54. /*
  55. * at this point, hpd has been disabled,
  56. * after flush workq, it's safe to deinit hdcp
  57. */
  58. if (hdmi->workq)
  59. destroy_workqueue(hdmi->workq);
  60. msm_hdmi_hdcp_destroy(hdmi);
  61. if (hdmi->i2c)
  62. msm_hdmi_i2c_destroy(hdmi->i2c);
  63. }
  64. static void msm_hdmi_put_phy(struct hdmi *hdmi)
  65. {
  66. if (hdmi->phy_dev) {
  67. put_device(hdmi->phy_dev);
  68. hdmi->phy = NULL;
  69. hdmi->phy_dev = NULL;
  70. }
  71. }
  72. static int msm_hdmi_get_phy(struct hdmi *hdmi)
  73. {
  74. struct platform_device *pdev = hdmi->pdev;
  75. struct platform_device *phy_pdev;
  76. struct device_node *phy_node;
  77. phy_node = of_parse_phandle(pdev->dev.of_node, "phys", 0);
  78. if (!phy_node) {
  79. DRM_DEV_ERROR(&pdev->dev, "cannot find phy device\n");
  80. return -ENXIO;
  81. }
  82. phy_pdev = of_find_device_by_node(phy_node);
  83. of_node_put(phy_node);
  84. if (!phy_pdev)
  85. return dev_err_probe(&pdev->dev, -EPROBE_DEFER, "phy driver is not ready\n");
  86. hdmi->phy = platform_get_drvdata(phy_pdev);
  87. if (!hdmi->phy) {
  88. put_device(&phy_pdev->dev);
  89. return dev_err_probe(&pdev->dev, -EPROBE_DEFER, "phy driver is not ready\n");
  90. }
  91. hdmi->phy_dev = &phy_pdev->dev;
  92. return 0;
  93. }
  94. /* construct hdmi at bind/probe time, grab all the resources. If
  95. * we are to EPROBE_DEFER we want to do it here, rather than later
  96. * at modeset_init() time
  97. */
  98. static int msm_hdmi_init(struct hdmi *hdmi)
  99. {
  100. struct platform_device *pdev = hdmi->pdev;
  101. int ret;
  102. hdmi->workq = alloc_ordered_workqueue("msm_hdmi", 0);
  103. if (!hdmi->workq) {
  104. ret = -ENOMEM;
  105. goto fail;
  106. }
  107. hdmi->i2c = msm_hdmi_i2c_init(hdmi);
  108. if (IS_ERR(hdmi->i2c)) {
  109. ret = PTR_ERR(hdmi->i2c);
  110. DRM_DEV_ERROR(&pdev->dev, "failed to get i2c: %d\n", ret);
  111. hdmi->i2c = NULL;
  112. goto fail;
  113. }
  114. hdmi->hdcp_ctrl = msm_hdmi_hdcp_init(hdmi);
  115. if (IS_ERR(hdmi->hdcp_ctrl)) {
  116. dev_warn(&pdev->dev, "failed to init hdcp: disabled\n");
  117. hdmi->hdcp_ctrl = NULL;
  118. }
  119. return 0;
  120. fail:
  121. msm_hdmi_destroy(hdmi);
  122. return ret;
  123. }
  124. /* Second part of initialization, the drm/kms level modeset_init,
  125. * constructs/initializes mode objects, etc, is called from master
  126. * driver (not hdmi sub-device's probe/bind!)
  127. *
  128. * Any resource (regulator/clk/etc) which could be missing at boot
  129. * should be handled in msm_hdmi_init() so that failure happens from
  130. * hdmi sub-device's probe.
  131. */
  132. int msm_hdmi_modeset_init(struct hdmi *hdmi,
  133. struct drm_device *dev, struct drm_encoder *encoder)
  134. {
  135. int ret;
  136. hdmi->dev = dev;
  137. hdmi->encoder = encoder;
  138. ret = msm_hdmi_bridge_init(hdmi);
  139. if (ret) {
  140. DRM_DEV_ERROR(dev->dev, "failed to create HDMI bridge: %d\n", ret);
  141. goto fail;
  142. }
  143. if (hdmi->next_bridge) {
  144. ret = drm_bridge_attach(hdmi->encoder, hdmi->next_bridge, hdmi->bridge,
  145. DRM_BRIDGE_ATTACH_NO_CONNECTOR);
  146. if (ret) {
  147. DRM_DEV_ERROR(dev->dev, "failed to attach next HDMI bridge: %d\n", ret);
  148. goto fail;
  149. }
  150. }
  151. hdmi->connector = drm_bridge_connector_init(hdmi->dev, encoder);
  152. if (IS_ERR(hdmi->connector)) {
  153. ret = PTR_ERR(hdmi->connector);
  154. DRM_DEV_ERROR(dev->dev, "failed to create HDMI connector: %d\n", ret);
  155. hdmi->connector = NULL;
  156. goto fail;
  157. }
  158. drm_connector_attach_encoder(hdmi->connector, hdmi->encoder);
  159. ret = devm_request_irq(dev->dev, hdmi->irq,
  160. msm_hdmi_irq, IRQF_TRIGGER_HIGH,
  161. "hdmi_isr", hdmi);
  162. if (ret < 0) {
  163. DRM_DEV_ERROR(dev->dev, "failed to request IRQ%u: %d\n",
  164. hdmi->irq, ret);
  165. goto fail;
  166. }
  167. return 0;
  168. fail:
  169. if (hdmi->connector) {
  170. hdmi->connector->funcs->destroy(hdmi->connector);
  171. hdmi->connector = NULL;
  172. }
  173. return ret;
  174. }
  175. /*
  176. * The hdmi device:
  177. */
  178. static const char * const pwr_reg_names_8960[] = {"core-vdda"};
  179. static const char * const pwr_clk_names_8960[] = {"core", "master_iface", "slave_iface"};
  180. static const struct hdmi_platform_config hdmi_tx_8960_config = {
  181. .pwr_reg_names = pwr_reg_names_8960,
  182. .pwr_reg_cnt = ARRAY_SIZE(pwr_reg_names_8960),
  183. .pwr_clk_names = pwr_clk_names_8960,
  184. .pwr_clk_cnt = ARRAY_SIZE(pwr_clk_names_8960),
  185. };
  186. static const char * const pwr_reg_names_8x74[] = {"core-vdda", "core-vcc"};
  187. static const char * const pwr_clk_names_8x74[] = {"iface", "core", "mdp_core", "alt_iface"};
  188. static const struct hdmi_platform_config hdmi_tx_8974_config = {
  189. .pwr_reg_names = pwr_reg_names_8x74,
  190. .pwr_reg_cnt = ARRAY_SIZE(pwr_reg_names_8x74),
  191. .pwr_clk_names = pwr_clk_names_8x74,
  192. .pwr_clk_cnt = ARRAY_SIZE(pwr_clk_names_8x74),
  193. };
  194. static int msm_hdmi_bind(struct device *dev, struct device *master, void *data)
  195. {
  196. struct msm_drm_private *priv = dev_get_drvdata(master);
  197. struct hdmi *hdmi = dev_get_drvdata(dev);
  198. int err;
  199. err = msm_hdmi_init(hdmi);
  200. if (err)
  201. return err;
  202. priv->kms->hdmi = hdmi;
  203. return 0;
  204. }
  205. static void msm_hdmi_unbind(struct device *dev, struct device *master,
  206. void *data)
  207. {
  208. struct msm_drm_private *priv = dev_get_drvdata(master);
  209. if (priv->kms->hdmi) {
  210. msm_hdmi_destroy(priv->kms->hdmi);
  211. priv->kms->hdmi = NULL;
  212. }
  213. }
  214. static const struct component_ops msm_hdmi_ops = {
  215. .bind = msm_hdmi_bind,
  216. .unbind = msm_hdmi_unbind,
  217. };
  218. static int msm_hdmi_dev_probe(struct platform_device *pdev)
  219. {
  220. const struct hdmi_platform_config *config;
  221. struct device *dev = &pdev->dev;
  222. struct hdmi *hdmi;
  223. struct resource *res;
  224. int i, ret;
  225. config = of_device_get_match_data(dev);
  226. if (!config)
  227. return -EINVAL;
  228. hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
  229. if (!hdmi)
  230. return -ENOMEM;
  231. hdmi->pdev = pdev;
  232. hdmi->config = config;
  233. spin_lock_init(&hdmi->reg_lock);
  234. mutex_init(&hdmi->state_mutex);
  235. ret = drm_of_find_panel_or_bridge(pdev->dev.of_node, 1, 0, NULL, &hdmi->next_bridge);
  236. if (ret && ret != -ENODEV)
  237. return ret;
  238. hdmi->mmio = msm_ioremap(pdev, "core_physical");
  239. if (IS_ERR(hdmi->mmio))
  240. return PTR_ERR(hdmi->mmio);
  241. /* HDCP needs physical address of hdmi register */
  242. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  243. "core_physical");
  244. if (!res)
  245. return -EINVAL;
  246. hdmi->mmio_phy_addr = res->start;
  247. hdmi->qfprom_mmio = msm_ioremap(pdev, "qfprom_physical");
  248. if (IS_ERR(hdmi->qfprom_mmio)) {
  249. DRM_DEV_INFO(&pdev->dev, "can't find qfprom resource\n");
  250. hdmi->qfprom_mmio = NULL;
  251. }
  252. hdmi->irq = platform_get_irq(pdev, 0);
  253. if (hdmi->irq < 0)
  254. return hdmi->irq;
  255. hdmi->pwr_regs = devm_kcalloc(&pdev->dev,
  256. config->pwr_reg_cnt,
  257. sizeof(hdmi->pwr_regs[0]),
  258. GFP_KERNEL);
  259. if (!hdmi->pwr_regs)
  260. return -ENOMEM;
  261. for (i = 0; i < config->pwr_reg_cnt; i++)
  262. hdmi->pwr_regs[i].supply = config->pwr_reg_names[i];
  263. ret = devm_regulator_bulk_get(&pdev->dev, config->pwr_reg_cnt, hdmi->pwr_regs);
  264. if (ret)
  265. return dev_err_probe(dev, ret, "failed to get pwr regulators\n");
  266. hdmi->pwr_clks = devm_kcalloc(&pdev->dev,
  267. config->pwr_clk_cnt,
  268. sizeof(hdmi->pwr_clks[0]),
  269. GFP_KERNEL);
  270. if (!hdmi->pwr_clks)
  271. return -ENOMEM;
  272. for (i = 0; i < config->pwr_clk_cnt; i++)
  273. hdmi->pwr_clks[i].id = config->pwr_clk_names[i];
  274. ret = devm_clk_bulk_get(&pdev->dev, config->pwr_clk_cnt, hdmi->pwr_clks);
  275. if (ret)
  276. return ret;
  277. hdmi->extp_clk = devm_clk_get_optional(&pdev->dev, "extp");
  278. if (IS_ERR(hdmi->extp_clk))
  279. return dev_err_probe(dev, PTR_ERR(hdmi->extp_clk),
  280. "failed to get extp clock\n");
  281. hdmi->hpd_gpiod = devm_gpiod_get_optional(&pdev->dev, "hpd", GPIOD_IN);
  282. /* This will catch e.g. -EPROBE_DEFER */
  283. if (IS_ERR(hdmi->hpd_gpiod))
  284. return dev_err_probe(dev, PTR_ERR(hdmi->hpd_gpiod),
  285. "failed to get hpd gpio\n");
  286. if (!hdmi->hpd_gpiod)
  287. DBG("failed to get HPD gpio");
  288. if (hdmi->hpd_gpiod)
  289. gpiod_set_consumer_name(hdmi->hpd_gpiod, "HDMI_HPD");
  290. ret = msm_hdmi_get_phy(hdmi);
  291. if (ret) {
  292. DRM_DEV_ERROR(&pdev->dev, "failed to get phy\n");
  293. return ret;
  294. }
  295. ret = devm_pm_runtime_enable(&pdev->dev);
  296. if (ret)
  297. goto err_put_phy;
  298. platform_set_drvdata(pdev, hdmi);
  299. ret = component_add(&pdev->dev, &msm_hdmi_ops);
  300. if (ret)
  301. goto err_put_phy;
  302. return 0;
  303. err_put_phy:
  304. msm_hdmi_put_phy(hdmi);
  305. return ret;
  306. }
  307. static void msm_hdmi_dev_remove(struct platform_device *pdev)
  308. {
  309. struct hdmi *hdmi = dev_get_drvdata(&pdev->dev);
  310. component_del(&pdev->dev, &msm_hdmi_ops);
  311. msm_hdmi_put_phy(hdmi);
  312. }
  313. static int msm_hdmi_runtime_suspend(struct device *dev)
  314. {
  315. struct hdmi *hdmi = dev_get_drvdata(dev);
  316. const struct hdmi_platform_config *config = hdmi->config;
  317. clk_bulk_disable_unprepare(config->pwr_clk_cnt, hdmi->pwr_clks);
  318. pinctrl_pm_select_sleep_state(dev);
  319. regulator_bulk_disable(config->pwr_reg_cnt, hdmi->pwr_regs);
  320. return 0;
  321. }
  322. static int msm_hdmi_runtime_resume(struct device *dev)
  323. {
  324. struct hdmi *hdmi = dev_get_drvdata(dev);
  325. const struct hdmi_platform_config *config = hdmi->config;
  326. int ret;
  327. ret = regulator_bulk_enable(config->pwr_reg_cnt, hdmi->pwr_regs);
  328. if (ret)
  329. return ret;
  330. ret = pinctrl_pm_select_default_state(dev);
  331. if (ret)
  332. goto fail;
  333. ret = clk_bulk_prepare_enable(config->pwr_clk_cnt, hdmi->pwr_clks);
  334. if (ret)
  335. goto fail;
  336. return 0;
  337. fail:
  338. pinctrl_pm_select_sleep_state(dev);
  339. return ret;
  340. }
  341. DEFINE_RUNTIME_DEV_PM_OPS(msm_hdmi_pm_ops, msm_hdmi_runtime_suspend, msm_hdmi_runtime_resume, NULL);
  342. static const struct of_device_id msm_hdmi_dt_match[] = {
  343. { .compatible = "qcom,hdmi-tx-8998", .data = &hdmi_tx_8974_config },
  344. { .compatible = "qcom,hdmi-tx-8996", .data = &hdmi_tx_8974_config },
  345. { .compatible = "qcom,hdmi-tx-8994", .data = &hdmi_tx_8974_config },
  346. { .compatible = "qcom,hdmi-tx-8084", .data = &hdmi_tx_8974_config },
  347. { .compatible = "qcom,hdmi-tx-8974", .data = &hdmi_tx_8974_config },
  348. { .compatible = "qcom,hdmi-tx-8960", .data = &hdmi_tx_8960_config },
  349. { .compatible = "qcom,hdmi-tx-8660", .data = &hdmi_tx_8960_config },
  350. {}
  351. };
  352. static struct platform_driver msm_hdmi_driver = {
  353. .probe = msm_hdmi_dev_probe,
  354. .remove = msm_hdmi_dev_remove,
  355. .driver = {
  356. .name = "hdmi_msm",
  357. .of_match_table = msm_hdmi_dt_match,
  358. .pm = &msm_hdmi_pm_ops,
  359. },
  360. };
  361. void __init msm_hdmi_register(void)
  362. {
  363. msm_hdmi_phy_driver_register();
  364. platform_driver_register(&msm_hdmi_driver);
  365. }
  366. void __exit msm_hdmi_unregister(void)
  367. {
  368. platform_driver_unregister(&msm_hdmi_driver);
  369. msm_hdmi_phy_driver_unregister();
  370. }