hdmi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. * Copyright (c) 2014 The Linux Foundation. All rights reserved.
  3. * Copyright (C) 2013 Red Hat
  4. * Author: Rob Clark <robdclark@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/of_irq.h>
  19. #include <linux/of_gpio.h>
  20. #include "hdmi.h"
  21. void hdmi_set_mode(struct hdmi *hdmi, bool power_on)
  22. {
  23. uint32_t ctrl = 0;
  24. unsigned long flags;
  25. spin_lock_irqsave(&hdmi->reg_lock, flags);
  26. if (power_on) {
  27. ctrl |= HDMI_CTRL_ENABLE;
  28. if (!hdmi->hdmi_mode) {
  29. ctrl |= HDMI_CTRL_HDMI;
  30. hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
  31. ctrl &= ~HDMI_CTRL_HDMI;
  32. } else {
  33. ctrl |= HDMI_CTRL_HDMI;
  34. }
  35. } else {
  36. ctrl = HDMI_CTRL_HDMI;
  37. }
  38. hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
  39. spin_unlock_irqrestore(&hdmi->reg_lock, flags);
  40. DBG("HDMI Core: %s, HDMI_CTRL=0x%08x",
  41. power_on ? "Enable" : "Disable", ctrl);
  42. }
  43. static irqreturn_t hdmi_irq(int irq, void *dev_id)
  44. {
  45. struct hdmi *hdmi = dev_id;
  46. /* Process HPD: */
  47. hdmi_connector_irq(hdmi->connector);
  48. /* Process DDC: */
  49. hdmi_i2c_irq(hdmi->i2c);
  50. /* Process HDCP: */
  51. if (hdmi->hdcp_ctrl)
  52. hdmi_hdcp_irq(hdmi->hdcp_ctrl);
  53. /* TODO audio.. */
  54. return IRQ_HANDLED;
  55. }
  56. static void hdmi_destroy(struct hdmi *hdmi)
  57. {
  58. struct hdmi_phy *phy = hdmi->phy;
  59. /*
  60. * at this point, hpd has been disabled,
  61. * after flush workq, it's safe to deinit hdcp
  62. */
  63. if (hdmi->workq) {
  64. flush_workqueue(hdmi->workq);
  65. destroy_workqueue(hdmi->workq);
  66. }
  67. hdmi_hdcp_destroy(hdmi);
  68. if (phy)
  69. phy->funcs->destroy(phy);
  70. if (hdmi->i2c)
  71. hdmi_i2c_destroy(hdmi->i2c);
  72. platform_set_drvdata(hdmi->pdev, NULL);
  73. }
  74. /* construct hdmi at bind/probe time, grab all the resources. If
  75. * we are to EPROBE_DEFER we want to do it here, rather than later
  76. * at modeset_init() time
  77. */
  78. static struct hdmi *hdmi_init(struct platform_device *pdev)
  79. {
  80. struct hdmi_platform_config *config = pdev->dev.platform_data;
  81. struct hdmi *hdmi = NULL;
  82. struct resource *res;
  83. int i, ret;
  84. hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
  85. if (!hdmi) {
  86. ret = -ENOMEM;
  87. goto fail;
  88. }
  89. hdmi->pdev = pdev;
  90. hdmi->config = config;
  91. spin_lock_init(&hdmi->reg_lock);
  92. /* not sure about which phy maps to which msm.. probably I miss some */
  93. if (config->phy_init) {
  94. hdmi->phy = config->phy_init(hdmi);
  95. if (IS_ERR(hdmi->phy)) {
  96. ret = PTR_ERR(hdmi->phy);
  97. dev_err(&pdev->dev, "failed to load phy: %d\n", ret);
  98. hdmi->phy = NULL;
  99. goto fail;
  100. }
  101. }
  102. hdmi->mmio = msm_ioremap(pdev, config->mmio_name, "HDMI");
  103. if (IS_ERR(hdmi->mmio)) {
  104. ret = PTR_ERR(hdmi->mmio);
  105. goto fail;
  106. }
  107. /* HDCP needs physical address of hdmi register */
  108. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  109. config->mmio_name);
  110. hdmi->mmio_phy_addr = res->start;
  111. hdmi->qfprom_mmio = msm_ioremap(pdev,
  112. config->qfprom_mmio_name, "HDMI_QFPROM");
  113. if (IS_ERR(hdmi->qfprom_mmio)) {
  114. dev_info(&pdev->dev, "can't find qfprom resource\n");
  115. hdmi->qfprom_mmio = NULL;
  116. }
  117. hdmi->hpd_regs = devm_kzalloc(&pdev->dev, sizeof(hdmi->hpd_regs[0]) *
  118. config->hpd_reg_cnt, GFP_KERNEL);
  119. if (!hdmi->hpd_regs) {
  120. ret = -ENOMEM;
  121. goto fail;
  122. }
  123. for (i = 0; i < config->hpd_reg_cnt; i++) {
  124. struct regulator *reg;
  125. reg = devm_regulator_get(&pdev->dev,
  126. config->hpd_reg_names[i]);
  127. if (IS_ERR(reg)) {
  128. ret = PTR_ERR(reg);
  129. dev_err(&pdev->dev, "failed to get hpd regulator: %s (%d)\n",
  130. config->hpd_reg_names[i], ret);
  131. goto fail;
  132. }
  133. hdmi->hpd_regs[i] = reg;
  134. }
  135. hdmi->pwr_regs = devm_kzalloc(&pdev->dev, sizeof(hdmi->pwr_regs[0]) *
  136. config->pwr_reg_cnt, GFP_KERNEL);
  137. if (!hdmi->pwr_regs) {
  138. ret = -ENOMEM;
  139. goto fail;
  140. }
  141. for (i = 0; i < config->pwr_reg_cnt; i++) {
  142. struct regulator *reg;
  143. reg = devm_regulator_get(&pdev->dev,
  144. config->pwr_reg_names[i]);
  145. if (IS_ERR(reg)) {
  146. ret = PTR_ERR(reg);
  147. dev_err(&pdev->dev, "failed to get pwr regulator: %s (%d)\n",
  148. config->pwr_reg_names[i], ret);
  149. goto fail;
  150. }
  151. hdmi->pwr_regs[i] = reg;
  152. }
  153. hdmi->hpd_clks = devm_kzalloc(&pdev->dev, sizeof(hdmi->hpd_clks[0]) *
  154. config->hpd_clk_cnt, GFP_KERNEL);
  155. if (!hdmi->hpd_clks) {
  156. ret = -ENOMEM;
  157. goto fail;
  158. }
  159. for (i = 0; i < config->hpd_clk_cnt; i++) {
  160. struct clk *clk;
  161. clk = devm_clk_get(&pdev->dev, config->hpd_clk_names[i]);
  162. if (IS_ERR(clk)) {
  163. ret = PTR_ERR(clk);
  164. dev_err(&pdev->dev, "failed to get hpd clk: %s (%d)\n",
  165. config->hpd_clk_names[i], ret);
  166. goto fail;
  167. }
  168. hdmi->hpd_clks[i] = clk;
  169. }
  170. hdmi->pwr_clks = devm_kzalloc(&pdev->dev, sizeof(hdmi->pwr_clks[0]) *
  171. config->pwr_clk_cnt, GFP_KERNEL);
  172. if (!hdmi->pwr_clks) {
  173. ret = -ENOMEM;
  174. goto fail;
  175. }
  176. for (i = 0; i < config->pwr_clk_cnt; i++) {
  177. struct clk *clk;
  178. clk = devm_clk_get(&pdev->dev, config->pwr_clk_names[i]);
  179. if (IS_ERR(clk)) {
  180. ret = PTR_ERR(clk);
  181. dev_err(&pdev->dev, "failed to get pwr clk: %s (%d)\n",
  182. config->pwr_clk_names[i], ret);
  183. goto fail;
  184. }
  185. hdmi->pwr_clks[i] = clk;
  186. }
  187. hdmi->workq = alloc_ordered_workqueue("msm_hdmi", 0);
  188. hdmi->i2c = hdmi_i2c_init(hdmi);
  189. if (IS_ERR(hdmi->i2c)) {
  190. ret = PTR_ERR(hdmi->i2c);
  191. dev_err(&pdev->dev, "failed to get i2c: %d\n", ret);
  192. hdmi->i2c = NULL;
  193. goto fail;
  194. }
  195. hdmi->hdcp_ctrl = hdmi_hdcp_init(hdmi);
  196. if (IS_ERR(hdmi->hdcp_ctrl)) {
  197. dev_warn(&pdev->dev, "failed to init hdcp: disabled\n");
  198. hdmi->hdcp_ctrl = NULL;
  199. }
  200. return hdmi;
  201. fail:
  202. if (hdmi)
  203. hdmi_destroy(hdmi);
  204. return ERR_PTR(ret);
  205. }
  206. /* Second part of initialization, the drm/kms level modeset_init,
  207. * constructs/initializes mode objects, etc, is called from master
  208. * driver (not hdmi sub-device's probe/bind!)
  209. *
  210. * Any resource (regulator/clk/etc) which could be missing at boot
  211. * should be handled in hdmi_init() so that failure happens from
  212. * hdmi sub-device's probe.
  213. */
  214. int hdmi_modeset_init(struct hdmi *hdmi,
  215. struct drm_device *dev, struct drm_encoder *encoder)
  216. {
  217. struct msm_drm_private *priv = dev->dev_private;
  218. struct platform_device *pdev = hdmi->pdev;
  219. int ret;
  220. hdmi->dev = dev;
  221. hdmi->encoder = encoder;
  222. hdmi_audio_infoframe_init(&hdmi->audio.infoframe);
  223. hdmi->bridge = hdmi_bridge_init(hdmi);
  224. if (IS_ERR(hdmi->bridge)) {
  225. ret = PTR_ERR(hdmi->bridge);
  226. dev_err(dev->dev, "failed to create HDMI bridge: %d\n", ret);
  227. hdmi->bridge = NULL;
  228. goto fail;
  229. }
  230. hdmi->connector = hdmi_connector_init(hdmi);
  231. if (IS_ERR(hdmi->connector)) {
  232. ret = PTR_ERR(hdmi->connector);
  233. dev_err(dev->dev, "failed to create HDMI connector: %d\n", ret);
  234. hdmi->connector = NULL;
  235. goto fail;
  236. }
  237. hdmi->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
  238. if (hdmi->irq < 0) {
  239. ret = hdmi->irq;
  240. dev_err(dev->dev, "failed to get irq: %d\n", ret);
  241. goto fail;
  242. }
  243. ret = devm_request_irq(&pdev->dev, hdmi->irq,
  244. hdmi_irq, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  245. "hdmi_isr", hdmi);
  246. if (ret < 0) {
  247. dev_err(dev->dev, "failed to request IRQ%u: %d\n",
  248. hdmi->irq, ret);
  249. goto fail;
  250. }
  251. encoder->bridge = hdmi->bridge;
  252. priv->bridges[priv->num_bridges++] = hdmi->bridge;
  253. priv->connectors[priv->num_connectors++] = hdmi->connector;
  254. platform_set_drvdata(pdev, hdmi);
  255. return 0;
  256. fail:
  257. /* bridge is normally destroyed by drm: */
  258. if (hdmi->bridge) {
  259. hdmi_bridge_destroy(hdmi->bridge);
  260. hdmi->bridge = NULL;
  261. }
  262. if (hdmi->connector) {
  263. hdmi->connector->funcs->destroy(hdmi->connector);
  264. hdmi->connector = NULL;
  265. }
  266. return ret;
  267. }
  268. /*
  269. * The hdmi device:
  270. */
  271. #define HDMI_CFG(item, entry) \
  272. .item ## _names = item ##_names_ ## entry, \
  273. .item ## _cnt = ARRAY_SIZE(item ## _names_ ## entry)
  274. static const char *pwr_reg_names_none[] = {};
  275. static const char *hpd_reg_names_none[] = {};
  276. static struct hdmi_platform_config hdmi_tx_8660_config = {
  277. .phy_init = hdmi_phy_8x60_init,
  278. };
  279. static const char *hpd_reg_names_8960[] = {"core-vdda", "hdmi-mux"};
  280. static const char *hpd_clk_names_8960[] = {"core_clk", "master_iface_clk", "slave_iface_clk"};
  281. static struct hdmi_platform_config hdmi_tx_8960_config = {
  282. .phy_init = hdmi_phy_8960_init,
  283. HDMI_CFG(hpd_reg, 8960),
  284. HDMI_CFG(hpd_clk, 8960),
  285. };
  286. static const char *pwr_reg_names_8x74[] = {"core-vdda", "core-vcc"};
  287. static const char *hpd_reg_names_8x74[] = {"hpd-gdsc", "hpd-5v"};
  288. static const char *pwr_clk_names_8x74[] = {"extp_clk", "alt_iface_clk"};
  289. static const char *hpd_clk_names_8x74[] = {"iface_clk", "core_clk", "mdp_core_clk"};
  290. static unsigned long hpd_clk_freq_8x74[] = {0, 19200000, 0};
  291. static struct hdmi_platform_config hdmi_tx_8974_config = {
  292. .phy_init = hdmi_phy_8x74_init,
  293. HDMI_CFG(pwr_reg, 8x74),
  294. HDMI_CFG(hpd_reg, 8x74),
  295. HDMI_CFG(pwr_clk, 8x74),
  296. HDMI_CFG(hpd_clk, 8x74),
  297. .hpd_freq = hpd_clk_freq_8x74,
  298. };
  299. static const char *hpd_reg_names_8084[] = {"hpd-gdsc", "hpd-5v", "hpd-5v-en"};
  300. static struct hdmi_platform_config hdmi_tx_8084_config = {
  301. .phy_init = hdmi_phy_8x74_init,
  302. HDMI_CFG(pwr_reg, 8x74),
  303. HDMI_CFG(hpd_reg, 8084),
  304. HDMI_CFG(pwr_clk, 8x74),
  305. HDMI_CFG(hpd_clk, 8x74),
  306. .hpd_freq = hpd_clk_freq_8x74,
  307. };
  308. static struct hdmi_platform_config hdmi_tx_8994_config = {
  309. .phy_init = NULL, /* nothing to do for this HDMI PHY 20nm */
  310. HDMI_CFG(pwr_reg, 8x74),
  311. HDMI_CFG(hpd_reg, none),
  312. HDMI_CFG(pwr_clk, 8x74),
  313. HDMI_CFG(hpd_clk, 8x74),
  314. .hpd_freq = hpd_clk_freq_8x74,
  315. };
  316. static struct hdmi_platform_config hdmi_tx_8996_config = {
  317. .phy_init = NULL,
  318. HDMI_CFG(pwr_reg, none),
  319. HDMI_CFG(hpd_reg, none),
  320. HDMI_CFG(pwr_clk, 8x74),
  321. HDMI_CFG(hpd_clk, 8x74),
  322. .hpd_freq = hpd_clk_freq_8x74,
  323. };
  324. static int get_gpio(struct device *dev, struct device_node *of_node, const char *name)
  325. {
  326. int gpio = of_get_named_gpio(of_node, name, 0);
  327. if (gpio < 0) {
  328. char name2[32];
  329. snprintf(name2, sizeof(name2), "%s-gpio", name);
  330. gpio = of_get_named_gpio(of_node, name2, 0);
  331. if (gpio < 0) {
  332. DBG("failed to get gpio: %s (%d)", name, gpio);
  333. gpio = -1;
  334. }
  335. }
  336. return gpio;
  337. }
  338. static int hdmi_bind(struct device *dev, struct device *master, void *data)
  339. {
  340. struct drm_device *drm = dev_get_drvdata(master);
  341. struct msm_drm_private *priv = drm->dev_private;
  342. static struct hdmi_platform_config *hdmi_cfg;
  343. struct hdmi *hdmi;
  344. struct device_node *of_node = dev->of_node;
  345. hdmi_cfg = (struct hdmi_platform_config *)
  346. of_device_get_match_data(dev);
  347. if (!hdmi_cfg) {
  348. dev_err(dev, "unknown hdmi_cfg: %s\n", of_node->name);
  349. return -ENXIO;
  350. }
  351. hdmi_cfg->mmio_name = "core_physical";
  352. hdmi_cfg->qfprom_mmio_name = "qfprom_physical";
  353. hdmi_cfg->ddc_clk_gpio = get_gpio(dev, of_node, "qcom,hdmi-tx-ddc-clk");
  354. hdmi_cfg->ddc_data_gpio = get_gpio(dev, of_node, "qcom,hdmi-tx-ddc-data");
  355. hdmi_cfg->hpd_gpio = get_gpio(dev, of_node, "qcom,hdmi-tx-hpd");
  356. hdmi_cfg->mux_en_gpio = get_gpio(dev, of_node, "qcom,hdmi-tx-mux-en");
  357. hdmi_cfg->mux_sel_gpio = get_gpio(dev, of_node, "qcom,hdmi-tx-mux-sel");
  358. hdmi_cfg->mux_lpm_gpio = get_gpio(dev, of_node, "qcom,hdmi-tx-mux-lpm");
  359. dev->platform_data = hdmi_cfg;
  360. hdmi = hdmi_init(to_platform_device(dev));
  361. if (IS_ERR(hdmi))
  362. return PTR_ERR(hdmi);
  363. priv->hdmi = hdmi;
  364. return 0;
  365. }
  366. static void hdmi_unbind(struct device *dev, struct device *master,
  367. void *data)
  368. {
  369. struct drm_device *drm = dev_get_drvdata(master);
  370. struct msm_drm_private *priv = drm->dev_private;
  371. if (priv->hdmi) {
  372. hdmi_destroy(priv->hdmi);
  373. priv->hdmi = NULL;
  374. }
  375. }
  376. static const struct component_ops hdmi_ops = {
  377. .bind = hdmi_bind,
  378. .unbind = hdmi_unbind,
  379. };
  380. static int hdmi_dev_probe(struct platform_device *pdev)
  381. {
  382. return component_add(&pdev->dev, &hdmi_ops);
  383. }
  384. static int hdmi_dev_remove(struct platform_device *pdev)
  385. {
  386. component_del(&pdev->dev, &hdmi_ops);
  387. return 0;
  388. }
  389. static const struct of_device_id dt_match[] = {
  390. { .compatible = "qcom,hdmi-tx-8996", .data = &hdmi_tx_8996_config },
  391. { .compatible = "qcom,hdmi-tx-8994", .data = &hdmi_tx_8994_config },
  392. { .compatible = "qcom,hdmi-tx-8084", .data = &hdmi_tx_8084_config },
  393. { .compatible = "qcom,hdmi-tx-8974", .data = &hdmi_tx_8974_config },
  394. { .compatible = "qcom,hdmi-tx-8960", .data = &hdmi_tx_8960_config },
  395. { .compatible = "qcom,hdmi-tx-8660", .data = &hdmi_tx_8660_config },
  396. {}
  397. };
  398. static struct platform_driver hdmi_driver = {
  399. .probe = hdmi_dev_probe,
  400. .remove = hdmi_dev_remove,
  401. .driver = {
  402. .name = "hdmi_msm",
  403. .of_match_table = dt_match,
  404. },
  405. };
  406. void __init hdmi_register(void)
  407. {
  408. platform_driver_register(&hdmi_driver);
  409. }
  410. void __exit hdmi_unregister(void)
  411. {
  412. platform_driver_unregister(&hdmi_driver);
  413. }