hdmi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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 "hdmi.h"
  20. void hdmi_set_mode(struct hdmi *hdmi, bool power_on)
  21. {
  22. uint32_t ctrl = 0;
  23. unsigned long flags;
  24. spin_lock_irqsave(&hdmi->reg_lock, flags);
  25. if (power_on) {
  26. ctrl |= HDMI_CTRL_ENABLE;
  27. if (!hdmi->hdmi_mode) {
  28. ctrl |= HDMI_CTRL_HDMI;
  29. hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
  30. ctrl &= ~HDMI_CTRL_HDMI;
  31. } else {
  32. ctrl |= HDMI_CTRL_HDMI;
  33. }
  34. } else {
  35. ctrl = HDMI_CTRL_HDMI;
  36. }
  37. hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
  38. spin_unlock_irqrestore(&hdmi->reg_lock, flags);
  39. DBG("HDMI Core: %s, HDMI_CTRL=0x%08x",
  40. power_on ? "Enable" : "Disable", ctrl);
  41. }
  42. static irqreturn_t hdmi_irq(int irq, void *dev_id)
  43. {
  44. struct hdmi *hdmi = dev_id;
  45. /* Process HPD: */
  46. hdmi_connector_irq(hdmi->connector);
  47. /* Process DDC: */
  48. hdmi_i2c_irq(hdmi->i2c);
  49. /* Process HDCP: */
  50. if (hdmi->hdcp_ctrl)
  51. hdmi_hdcp_irq(hdmi->hdcp_ctrl);
  52. /* TODO audio.. */
  53. return IRQ_HANDLED;
  54. }
  55. static void hdmi_destroy(struct hdmi *hdmi)
  56. {
  57. struct hdmi_phy *phy = hdmi->phy;
  58. /*
  59. * at this point, hpd has been disabled,
  60. * after flush workq, it's safe to deinit hdcp
  61. */
  62. if (hdmi->workq) {
  63. flush_workqueue(hdmi->workq);
  64. destroy_workqueue(hdmi->workq);
  65. }
  66. hdmi_hdcp_destroy(hdmi);
  67. if (phy)
  68. phy->funcs->destroy(phy);
  69. if (hdmi->i2c)
  70. hdmi_i2c_destroy(hdmi->i2c);
  71. platform_set_drvdata(hdmi->pdev, NULL);
  72. }
  73. /* construct hdmi at bind/probe time, grab all the resources. If
  74. * we are to EPROBE_DEFER we want to do it here, rather than later
  75. * at modeset_init() time
  76. */
  77. static struct hdmi *hdmi_init(struct platform_device *pdev)
  78. {
  79. struct hdmi_platform_config *config = pdev->dev.platform_data;
  80. struct hdmi *hdmi = NULL;
  81. struct resource *res;
  82. int i, ret;
  83. hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
  84. if (!hdmi) {
  85. ret = -ENOMEM;
  86. goto fail;
  87. }
  88. hdmi->pdev = pdev;
  89. hdmi->config = config;
  90. spin_lock_init(&hdmi->reg_lock);
  91. /* not sure about which phy maps to which msm.. probably I miss some */
  92. if (config->phy_init) {
  93. hdmi->phy = config->phy_init(hdmi);
  94. if (IS_ERR(hdmi->phy)) {
  95. ret = PTR_ERR(hdmi->phy);
  96. dev_err(&pdev->dev, "failed to load phy: %d\n", ret);
  97. hdmi->phy = NULL;
  98. goto fail;
  99. }
  100. }
  101. hdmi->mmio = msm_ioremap(pdev, config->mmio_name, "HDMI");
  102. if (IS_ERR(hdmi->mmio)) {
  103. ret = PTR_ERR(hdmi->mmio);
  104. goto fail;
  105. }
  106. /* HDCP needs physical address of hdmi register */
  107. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  108. config->mmio_name);
  109. hdmi->mmio_phy_addr = res->start;
  110. hdmi->qfprom_mmio = msm_ioremap(pdev,
  111. config->qfprom_mmio_name, "HDMI_QFPROM");
  112. if (IS_ERR(hdmi->qfprom_mmio)) {
  113. dev_info(&pdev->dev, "can't find qfprom resource\n");
  114. hdmi->qfprom_mmio = NULL;
  115. }
  116. hdmi->hpd_regs = devm_kzalloc(&pdev->dev, sizeof(hdmi->hpd_regs[0]) *
  117. config->hpd_reg_cnt, GFP_KERNEL);
  118. if (!hdmi->hpd_regs) {
  119. ret = -ENOMEM;
  120. goto fail;
  121. }
  122. for (i = 0; i < config->hpd_reg_cnt; i++) {
  123. struct regulator *reg;
  124. reg = devm_regulator_get(&pdev->dev,
  125. config->hpd_reg_names[i]);
  126. if (IS_ERR(reg)) {
  127. ret = PTR_ERR(reg);
  128. dev_err(&pdev->dev, "failed to get hpd regulator: %s (%d)\n",
  129. config->hpd_reg_names[i], ret);
  130. goto fail;
  131. }
  132. hdmi->hpd_regs[i] = reg;
  133. }
  134. hdmi->pwr_regs = devm_kzalloc(&pdev->dev, sizeof(hdmi->pwr_regs[0]) *
  135. config->pwr_reg_cnt, GFP_KERNEL);
  136. if (!hdmi->pwr_regs) {
  137. ret = -ENOMEM;
  138. goto fail;
  139. }
  140. for (i = 0; i < config->pwr_reg_cnt; i++) {
  141. struct regulator *reg;
  142. reg = devm_regulator_get(&pdev->dev,
  143. config->pwr_reg_names[i]);
  144. if (IS_ERR(reg)) {
  145. ret = PTR_ERR(reg);
  146. dev_err(&pdev->dev, "failed to get pwr regulator: %s (%d)\n",
  147. config->pwr_reg_names[i], ret);
  148. goto fail;
  149. }
  150. hdmi->pwr_regs[i] = reg;
  151. }
  152. hdmi->hpd_clks = devm_kzalloc(&pdev->dev, sizeof(hdmi->hpd_clks[0]) *
  153. config->hpd_clk_cnt, GFP_KERNEL);
  154. if (!hdmi->hpd_clks) {
  155. ret = -ENOMEM;
  156. goto fail;
  157. }
  158. for (i = 0; i < config->hpd_clk_cnt; i++) {
  159. struct clk *clk;
  160. clk = devm_clk_get(&pdev->dev, config->hpd_clk_names[i]);
  161. if (IS_ERR(clk)) {
  162. ret = PTR_ERR(clk);
  163. dev_err(&pdev->dev, "failed to get hpd clk: %s (%d)\n",
  164. config->hpd_clk_names[i], ret);
  165. goto fail;
  166. }
  167. hdmi->hpd_clks[i] = clk;
  168. }
  169. hdmi->pwr_clks = devm_kzalloc(&pdev->dev, sizeof(hdmi->pwr_clks[0]) *
  170. config->pwr_clk_cnt, GFP_KERNEL);
  171. if (!hdmi->pwr_clks) {
  172. ret = -ENOMEM;
  173. goto fail;
  174. }
  175. for (i = 0; i < config->pwr_clk_cnt; i++) {
  176. struct clk *clk;
  177. clk = devm_clk_get(&pdev->dev, config->pwr_clk_names[i]);
  178. if (IS_ERR(clk)) {
  179. ret = PTR_ERR(clk);
  180. dev_err(&pdev->dev, "failed to get pwr clk: %s (%d)\n",
  181. config->pwr_clk_names[i], ret);
  182. goto fail;
  183. }
  184. hdmi->pwr_clks[i] = clk;
  185. }
  186. hdmi->workq = alloc_ordered_workqueue("msm_hdmi", 0);
  187. hdmi->i2c = hdmi_i2c_init(hdmi);
  188. if (IS_ERR(hdmi->i2c)) {
  189. ret = PTR_ERR(hdmi->i2c);
  190. dev_err(&pdev->dev, "failed to get i2c: %d\n", ret);
  191. hdmi->i2c = NULL;
  192. goto fail;
  193. }
  194. hdmi->hdcp_ctrl = hdmi_hdcp_init(hdmi);
  195. if (IS_ERR(hdmi->hdcp_ctrl)) {
  196. dev_warn(&pdev->dev, "failed to init hdcp: disabled\n");
  197. hdmi->hdcp_ctrl = NULL;
  198. }
  199. return hdmi;
  200. fail:
  201. if (hdmi)
  202. hdmi_destroy(hdmi);
  203. return ERR_PTR(ret);
  204. }
  205. /* Second part of initialization, the drm/kms level modeset_init,
  206. * constructs/initializes mode objects, etc, is called from master
  207. * driver (not hdmi sub-device's probe/bind!)
  208. *
  209. * Any resource (regulator/clk/etc) which could be missing at boot
  210. * should be handled in hdmi_init() so that failure happens from
  211. * hdmi sub-device's probe.
  212. */
  213. int hdmi_modeset_init(struct hdmi *hdmi,
  214. struct drm_device *dev, struct drm_encoder *encoder)
  215. {
  216. struct msm_drm_private *priv = dev->dev_private;
  217. struct platform_device *pdev = hdmi->pdev;
  218. int ret;
  219. hdmi->dev = dev;
  220. hdmi->encoder = encoder;
  221. hdmi_audio_infoframe_init(&hdmi->audio.infoframe);
  222. hdmi->bridge = hdmi_bridge_init(hdmi);
  223. if (IS_ERR(hdmi->bridge)) {
  224. ret = PTR_ERR(hdmi->bridge);
  225. dev_err(dev->dev, "failed to create HDMI bridge: %d\n", ret);
  226. hdmi->bridge = NULL;
  227. goto fail;
  228. }
  229. hdmi->connector = hdmi_connector_init(hdmi);
  230. if (IS_ERR(hdmi->connector)) {
  231. ret = PTR_ERR(hdmi->connector);
  232. dev_err(dev->dev, "failed to create HDMI connector: %d\n", ret);
  233. hdmi->connector = NULL;
  234. goto fail;
  235. }
  236. hdmi->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
  237. if (hdmi->irq < 0) {
  238. ret = hdmi->irq;
  239. dev_err(dev->dev, "failed to get irq: %d\n", ret);
  240. goto fail;
  241. }
  242. ret = devm_request_irq(&pdev->dev, hdmi->irq,
  243. hdmi_irq, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  244. "hdmi_isr", hdmi);
  245. if (ret < 0) {
  246. dev_err(dev->dev, "failed to request IRQ%u: %d\n",
  247. hdmi->irq, ret);
  248. goto fail;
  249. }
  250. encoder->bridge = hdmi->bridge;
  251. priv->bridges[priv->num_bridges++] = hdmi->bridge;
  252. priv->connectors[priv->num_connectors++] = hdmi->connector;
  253. platform_set_drvdata(pdev, hdmi);
  254. return 0;
  255. fail:
  256. /* bridge is normally destroyed by drm: */
  257. if (hdmi->bridge) {
  258. hdmi_bridge_destroy(hdmi->bridge);
  259. hdmi->bridge = NULL;
  260. }
  261. if (hdmi->connector) {
  262. hdmi->connector->funcs->destroy(hdmi->connector);
  263. hdmi->connector = NULL;
  264. }
  265. return ret;
  266. }
  267. /*
  268. * The hdmi device:
  269. */
  270. #include <linux/of_gpio.h>
  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 const struct of_device_id dt_match[] = {
  325. { .compatible = "qcom,hdmi-tx-8996", .data = &hdmi_tx_8996_config },
  326. { .compatible = "qcom,hdmi-tx-8994", .data = &hdmi_tx_8994_config },
  327. { .compatible = "qcom,hdmi-tx-8084", .data = &hdmi_tx_8084_config },
  328. { .compatible = "qcom,hdmi-tx-8974", .data = &hdmi_tx_8974_config },
  329. { .compatible = "qcom,hdmi-tx-8960", .data = &hdmi_tx_8960_config },
  330. { .compatible = "qcom,hdmi-tx-8660", .data = &hdmi_tx_8660_config },
  331. {}
  332. };
  333. #ifdef CONFIG_OF
  334. static int get_gpio(struct device *dev, struct device_node *of_node, const char *name)
  335. {
  336. int gpio = of_get_named_gpio(of_node, name, 0);
  337. if (gpio < 0) {
  338. char name2[32];
  339. snprintf(name2, sizeof(name2), "%s-gpio", name);
  340. gpio = of_get_named_gpio(of_node, name2, 0);
  341. if (gpio < 0) {
  342. DBG("failed to get gpio: %s (%d)", name, gpio);
  343. gpio = -1;
  344. }
  345. }
  346. return gpio;
  347. }
  348. #endif
  349. static int hdmi_bind(struct device *dev, struct device *master, void *data)
  350. {
  351. struct drm_device *drm = dev_get_drvdata(master);
  352. struct msm_drm_private *priv = drm->dev_private;
  353. static struct hdmi_platform_config *hdmi_cfg;
  354. struct hdmi *hdmi;
  355. #ifdef CONFIG_OF
  356. struct device_node *of_node = dev->of_node;
  357. const struct of_device_id *match;
  358. match = of_match_node(dt_match, of_node);
  359. if (match && match->data) {
  360. hdmi_cfg = (struct hdmi_platform_config *)match->data;
  361. DBG("hdmi phy: %s", match->compatible);
  362. } else {
  363. dev_err(dev, "unknown phy: %s\n", of_node->name);
  364. return -ENXIO;
  365. }
  366. hdmi_cfg->mmio_name = "core_physical";
  367. hdmi_cfg->qfprom_mmio_name = "qfprom_physical";
  368. hdmi_cfg->ddc_clk_gpio = get_gpio(dev, of_node, "qcom,hdmi-tx-ddc-clk");
  369. hdmi_cfg->ddc_data_gpio = get_gpio(dev, of_node, "qcom,hdmi-tx-ddc-data");
  370. hdmi_cfg->hpd_gpio = get_gpio(dev, of_node, "qcom,hdmi-tx-hpd");
  371. hdmi_cfg->mux_en_gpio = get_gpio(dev, of_node, "qcom,hdmi-tx-mux-en");
  372. hdmi_cfg->mux_sel_gpio = get_gpio(dev, of_node, "qcom,hdmi-tx-mux-sel");
  373. hdmi_cfg->mux_lpm_gpio = get_gpio(dev, of_node, "qcom,hdmi-tx-mux-lpm");
  374. #else
  375. static struct hdmi_platform_config config = {};
  376. static const char *hpd_clk_names[] = {
  377. "core_clk", "master_iface_clk", "slave_iface_clk",
  378. };
  379. if (cpu_is_apq8064()) {
  380. static const char *hpd_reg_names[] = {"8921_hdmi_mvs"};
  381. config.phy_init = hdmi_phy_8960_init;
  382. config.hpd_reg_names = hpd_reg_names;
  383. config.hpd_reg_cnt = ARRAY_SIZE(hpd_reg_names);
  384. config.hpd_clk_names = hpd_clk_names;
  385. config.hpd_clk_cnt = ARRAY_SIZE(hpd_clk_names);
  386. config.ddc_clk_gpio = 70;
  387. config.ddc_data_gpio = 71;
  388. config.hpd_gpio = 72;
  389. config.mux_en_gpio = -1;
  390. config.mux_sel_gpio = -1;
  391. } else if (cpu_is_msm8960() || cpu_is_msm8960ab()) {
  392. static const char *hpd_reg_names[] = {"8921_hdmi_mvs"};
  393. config.phy_init = hdmi_phy_8960_init;
  394. config.hpd_reg_names = hpd_reg_names;
  395. config.hpd_reg_cnt = ARRAY_SIZE(hpd_reg_names);
  396. config.hpd_clk_names = hpd_clk_names;
  397. config.hpd_clk_cnt = ARRAY_SIZE(hpd_clk_names);
  398. config.ddc_clk_gpio = 100;
  399. config.ddc_data_gpio = 101;
  400. config.hpd_gpio = 102;
  401. config.mux_en_gpio = -1;
  402. config.mux_sel_gpio = -1;
  403. } else if (cpu_is_msm8x60()) {
  404. static const char *hpd_reg_names[] = {
  405. "8901_hdmi_mvs", "8901_mpp0"
  406. };
  407. config.phy_init = hdmi_phy_8x60_init;
  408. config.hpd_reg_names = hpd_reg_names;
  409. config.hpd_reg_cnt = ARRAY_SIZE(hpd_reg_names);
  410. config.hpd_clk_names = hpd_clk_names;
  411. config.hpd_clk_cnt = ARRAY_SIZE(hpd_clk_names);
  412. config.ddc_clk_gpio = 170;
  413. config.ddc_data_gpio = 171;
  414. config.hpd_gpio = 172;
  415. config.mux_en_gpio = -1;
  416. config.mux_sel_gpio = -1;
  417. }
  418. config.mmio_name = "hdmi_msm_hdmi_addr";
  419. config.qfprom_mmio_name = "hdmi_msm_qfprom_addr";
  420. hdmi_cfg = &config;
  421. #endif
  422. dev->platform_data = hdmi_cfg;
  423. hdmi = hdmi_init(to_platform_device(dev));
  424. if (IS_ERR(hdmi))
  425. return PTR_ERR(hdmi);
  426. priv->hdmi = hdmi;
  427. return 0;
  428. }
  429. static void hdmi_unbind(struct device *dev, struct device *master,
  430. void *data)
  431. {
  432. struct drm_device *drm = dev_get_drvdata(master);
  433. struct msm_drm_private *priv = drm->dev_private;
  434. if (priv->hdmi) {
  435. hdmi_destroy(priv->hdmi);
  436. priv->hdmi = NULL;
  437. }
  438. }
  439. static const struct component_ops hdmi_ops = {
  440. .bind = hdmi_bind,
  441. .unbind = hdmi_unbind,
  442. };
  443. static int hdmi_dev_probe(struct platform_device *pdev)
  444. {
  445. return component_add(&pdev->dev, &hdmi_ops);
  446. }
  447. static int hdmi_dev_remove(struct platform_device *pdev)
  448. {
  449. component_del(&pdev->dev, &hdmi_ops);
  450. return 0;
  451. }
  452. static struct platform_driver hdmi_driver = {
  453. .probe = hdmi_dev_probe,
  454. .remove = hdmi_dev_remove,
  455. .driver = {
  456. .name = "hdmi_msm",
  457. .of_match_table = dt_match,
  458. },
  459. };
  460. void __init hdmi_register(void)
  461. {
  462. platform_driver_register(&hdmi_driver);
  463. }
  464. void __exit hdmi_unregister(void)
  465. {
  466. platform_driver_unregister(&hdmi_driver);
  467. }