hdmi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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 msm_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 msm_hdmi_irq(int irq, void *dev_id)
  44. {
  45. struct hdmi *hdmi = dev_id;
  46. /* Process HPD: */
  47. msm_hdmi_connector_irq(hdmi->connector);
  48. /* Process DDC: */
  49. msm_hdmi_i2c_irq(hdmi->i2c);
  50. /* Process HDCP: */
  51. if (hdmi->hdcp_ctrl)
  52. msm_hdmi_hdcp_irq(hdmi->hdcp_ctrl);
  53. /* TODO audio.. */
  54. return IRQ_HANDLED;
  55. }
  56. static void msm_hdmi_destroy(struct hdmi *hdmi)
  57. {
  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. msm_hdmi_hdcp_destroy(hdmi);
  67. if (hdmi->phy_dev) {
  68. put_device(hdmi->phy_dev);
  69. hdmi->phy = NULL;
  70. hdmi->phy_dev = NULL;
  71. }
  72. if (hdmi->i2c)
  73. msm_hdmi_i2c_destroy(hdmi->i2c);
  74. platform_set_drvdata(hdmi->pdev, NULL);
  75. }
  76. static int msm_hdmi_get_phy(struct hdmi *hdmi)
  77. {
  78. struct platform_device *pdev = hdmi->pdev;
  79. struct platform_device *phy_pdev;
  80. struct device_node *phy_node;
  81. phy_node = of_parse_phandle(pdev->dev.of_node, "phys", 0);
  82. if (!phy_node) {
  83. dev_err(&pdev->dev, "cannot find phy device\n");
  84. return -ENXIO;
  85. }
  86. phy_pdev = of_find_device_by_node(phy_node);
  87. if (phy_pdev)
  88. hdmi->phy = platform_get_drvdata(phy_pdev);
  89. of_node_put(phy_node);
  90. if (!phy_pdev || !hdmi->phy) {
  91. dev_err(&pdev->dev, "phy driver is not ready\n");
  92. return -EPROBE_DEFER;
  93. }
  94. hdmi->phy_dev = get_device(&phy_pdev->dev);
  95. return 0;
  96. }
  97. /* construct hdmi at bind/probe time, grab all the resources. If
  98. * we are to EPROBE_DEFER we want to do it here, rather than later
  99. * at modeset_init() time
  100. */
  101. static struct hdmi *msm_hdmi_init(struct platform_device *pdev)
  102. {
  103. struct hdmi_platform_config *config = pdev->dev.platform_data;
  104. struct hdmi *hdmi = NULL;
  105. struct resource *res;
  106. int i, ret;
  107. hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
  108. if (!hdmi) {
  109. ret = -ENOMEM;
  110. goto fail;
  111. }
  112. hdmi->pdev = pdev;
  113. hdmi->config = config;
  114. spin_lock_init(&hdmi->reg_lock);
  115. hdmi->mmio = msm_ioremap(pdev, config->mmio_name, "HDMI");
  116. if (IS_ERR(hdmi->mmio)) {
  117. ret = PTR_ERR(hdmi->mmio);
  118. goto fail;
  119. }
  120. /* HDCP needs physical address of hdmi register */
  121. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  122. config->mmio_name);
  123. hdmi->mmio_phy_addr = res->start;
  124. hdmi->qfprom_mmio = msm_ioremap(pdev,
  125. config->qfprom_mmio_name, "HDMI_QFPROM");
  126. if (IS_ERR(hdmi->qfprom_mmio)) {
  127. dev_info(&pdev->dev, "can't find qfprom resource\n");
  128. hdmi->qfprom_mmio = NULL;
  129. }
  130. hdmi->hpd_regs = devm_kzalloc(&pdev->dev, sizeof(hdmi->hpd_regs[0]) *
  131. config->hpd_reg_cnt, GFP_KERNEL);
  132. if (!hdmi->hpd_regs) {
  133. ret = -ENOMEM;
  134. goto fail;
  135. }
  136. for (i = 0; i < config->hpd_reg_cnt; i++) {
  137. struct regulator *reg;
  138. reg = devm_regulator_get(&pdev->dev,
  139. config->hpd_reg_names[i]);
  140. if (IS_ERR(reg)) {
  141. ret = PTR_ERR(reg);
  142. dev_err(&pdev->dev, "failed to get hpd regulator: %s (%d)\n",
  143. config->hpd_reg_names[i], ret);
  144. goto fail;
  145. }
  146. hdmi->hpd_regs[i] = reg;
  147. }
  148. hdmi->pwr_regs = devm_kzalloc(&pdev->dev, sizeof(hdmi->pwr_regs[0]) *
  149. config->pwr_reg_cnt, GFP_KERNEL);
  150. if (!hdmi->pwr_regs) {
  151. ret = -ENOMEM;
  152. goto fail;
  153. }
  154. for (i = 0; i < config->pwr_reg_cnt; i++) {
  155. struct regulator *reg;
  156. reg = devm_regulator_get(&pdev->dev,
  157. config->pwr_reg_names[i]);
  158. if (IS_ERR(reg)) {
  159. ret = PTR_ERR(reg);
  160. dev_err(&pdev->dev, "failed to get pwr regulator: %s (%d)\n",
  161. config->pwr_reg_names[i], ret);
  162. goto fail;
  163. }
  164. hdmi->pwr_regs[i] = reg;
  165. }
  166. hdmi->hpd_clks = devm_kzalloc(&pdev->dev, sizeof(hdmi->hpd_clks[0]) *
  167. config->hpd_clk_cnt, GFP_KERNEL);
  168. if (!hdmi->hpd_clks) {
  169. ret = -ENOMEM;
  170. goto fail;
  171. }
  172. for (i = 0; i < config->hpd_clk_cnt; i++) {
  173. struct clk *clk;
  174. clk = devm_clk_get(&pdev->dev, config->hpd_clk_names[i]);
  175. if (IS_ERR(clk)) {
  176. ret = PTR_ERR(clk);
  177. dev_err(&pdev->dev, "failed to get hpd clk: %s (%d)\n",
  178. config->hpd_clk_names[i], ret);
  179. goto fail;
  180. }
  181. hdmi->hpd_clks[i] = clk;
  182. }
  183. hdmi->pwr_clks = devm_kzalloc(&pdev->dev, sizeof(hdmi->pwr_clks[0]) *
  184. config->pwr_clk_cnt, GFP_KERNEL);
  185. if (!hdmi->pwr_clks) {
  186. ret = -ENOMEM;
  187. goto fail;
  188. }
  189. for (i = 0; i < config->pwr_clk_cnt; i++) {
  190. struct clk *clk;
  191. clk = devm_clk_get(&pdev->dev, config->pwr_clk_names[i]);
  192. if (IS_ERR(clk)) {
  193. ret = PTR_ERR(clk);
  194. dev_err(&pdev->dev, "failed to get pwr clk: %s (%d)\n",
  195. config->pwr_clk_names[i], ret);
  196. goto fail;
  197. }
  198. hdmi->pwr_clks[i] = clk;
  199. }
  200. hdmi->workq = alloc_ordered_workqueue("msm_hdmi", 0);
  201. hdmi->i2c = msm_hdmi_i2c_init(hdmi);
  202. if (IS_ERR(hdmi->i2c)) {
  203. ret = PTR_ERR(hdmi->i2c);
  204. dev_err(&pdev->dev, "failed to get i2c: %d\n", ret);
  205. hdmi->i2c = NULL;
  206. goto fail;
  207. }
  208. ret = msm_hdmi_get_phy(hdmi);
  209. if (ret) {
  210. dev_err(&pdev->dev, "failed to get phy\n");
  211. goto fail;
  212. }
  213. hdmi->hdcp_ctrl = msm_hdmi_hdcp_init(hdmi);
  214. if (IS_ERR(hdmi->hdcp_ctrl)) {
  215. dev_warn(&pdev->dev, "failed to init hdcp: disabled\n");
  216. hdmi->hdcp_ctrl = NULL;
  217. }
  218. return hdmi;
  219. fail:
  220. if (hdmi)
  221. msm_hdmi_destroy(hdmi);
  222. return ERR_PTR(ret);
  223. }
  224. /* Second part of initialization, the drm/kms level modeset_init,
  225. * constructs/initializes mode objects, etc, is called from master
  226. * driver (not hdmi sub-device's probe/bind!)
  227. *
  228. * Any resource (regulator/clk/etc) which could be missing at boot
  229. * should be handled in msm_hdmi_init() so that failure happens from
  230. * hdmi sub-device's probe.
  231. */
  232. int msm_hdmi_modeset_init(struct hdmi *hdmi,
  233. struct drm_device *dev, struct drm_encoder *encoder)
  234. {
  235. struct msm_drm_private *priv = dev->dev_private;
  236. struct platform_device *pdev = hdmi->pdev;
  237. int ret;
  238. hdmi->dev = dev;
  239. hdmi->encoder = encoder;
  240. hdmi_audio_infoframe_init(&hdmi->audio.infoframe);
  241. hdmi->bridge = msm_hdmi_bridge_init(hdmi);
  242. if (IS_ERR(hdmi->bridge)) {
  243. ret = PTR_ERR(hdmi->bridge);
  244. dev_err(dev->dev, "failed to create HDMI bridge: %d\n", ret);
  245. hdmi->bridge = NULL;
  246. goto fail;
  247. }
  248. hdmi->connector = msm_hdmi_connector_init(hdmi);
  249. if (IS_ERR(hdmi->connector)) {
  250. ret = PTR_ERR(hdmi->connector);
  251. dev_err(dev->dev, "failed to create HDMI connector: %d\n", ret);
  252. hdmi->connector = NULL;
  253. goto fail;
  254. }
  255. hdmi->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
  256. if (hdmi->irq < 0) {
  257. ret = hdmi->irq;
  258. dev_err(dev->dev, "failed to get irq: %d\n", ret);
  259. goto fail;
  260. }
  261. ret = devm_request_irq(&pdev->dev, hdmi->irq,
  262. msm_hdmi_irq, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  263. "hdmi_isr", hdmi);
  264. if (ret < 0) {
  265. dev_err(dev->dev, "failed to request IRQ%u: %d\n",
  266. hdmi->irq, ret);
  267. goto fail;
  268. }
  269. encoder->bridge = hdmi->bridge;
  270. priv->bridges[priv->num_bridges++] = hdmi->bridge;
  271. priv->connectors[priv->num_connectors++] = hdmi->connector;
  272. platform_set_drvdata(pdev, hdmi);
  273. return 0;
  274. fail:
  275. /* bridge is normally destroyed by drm: */
  276. if (hdmi->bridge) {
  277. msm_hdmi_bridge_destroy(hdmi->bridge);
  278. hdmi->bridge = NULL;
  279. }
  280. if (hdmi->connector) {
  281. hdmi->connector->funcs->destroy(hdmi->connector);
  282. hdmi->connector = NULL;
  283. }
  284. return ret;
  285. }
  286. /*
  287. * The hdmi device:
  288. */
  289. #define HDMI_CFG(item, entry) \
  290. .item ## _names = item ##_names_ ## entry, \
  291. .item ## _cnt = ARRAY_SIZE(item ## _names_ ## entry)
  292. static const char *pwr_reg_names_none[] = {};
  293. static const char *hpd_reg_names_none[] = {};
  294. static struct hdmi_platform_config hdmi_tx_8660_config;
  295. static const char *hpd_reg_names_8960[] = {"core-vdda", "hdmi-mux"};
  296. static const char *hpd_clk_names_8960[] = {"core_clk", "master_iface_clk", "slave_iface_clk"};
  297. static struct hdmi_platform_config hdmi_tx_8960_config = {
  298. HDMI_CFG(hpd_reg, 8960),
  299. HDMI_CFG(hpd_clk, 8960),
  300. };
  301. static const char *pwr_reg_names_8x74[] = {"core-vdda", "core-vcc"};
  302. static const char *hpd_reg_names_8x74[] = {"hpd-gdsc", "hpd-5v"};
  303. static const char *pwr_clk_names_8x74[] = {"extp_clk", "alt_iface_clk"};
  304. static const char *hpd_clk_names_8x74[] = {"iface_clk", "core_clk", "mdp_core_clk"};
  305. static unsigned long hpd_clk_freq_8x74[] = {0, 19200000, 0};
  306. static struct hdmi_platform_config hdmi_tx_8974_config = {
  307. HDMI_CFG(pwr_reg, 8x74),
  308. HDMI_CFG(hpd_reg, 8x74),
  309. HDMI_CFG(pwr_clk, 8x74),
  310. HDMI_CFG(hpd_clk, 8x74),
  311. .hpd_freq = hpd_clk_freq_8x74,
  312. };
  313. static const char *hpd_reg_names_8084[] = {"hpd-gdsc", "hpd-5v", "hpd-5v-en"};
  314. static struct hdmi_platform_config hdmi_tx_8084_config = {
  315. HDMI_CFG(pwr_reg, 8x74),
  316. HDMI_CFG(hpd_reg, 8084),
  317. HDMI_CFG(pwr_clk, 8x74),
  318. HDMI_CFG(hpd_clk, 8x74),
  319. .hpd_freq = hpd_clk_freq_8x74,
  320. };
  321. static struct hdmi_platform_config hdmi_tx_8994_config = {
  322. HDMI_CFG(pwr_reg, 8x74),
  323. HDMI_CFG(hpd_reg, none),
  324. HDMI_CFG(pwr_clk, 8x74),
  325. HDMI_CFG(hpd_clk, 8x74),
  326. .hpd_freq = hpd_clk_freq_8x74,
  327. };
  328. static struct hdmi_platform_config hdmi_tx_8996_config = {
  329. HDMI_CFG(pwr_reg, none),
  330. HDMI_CFG(hpd_reg, none),
  331. HDMI_CFG(pwr_clk, 8x74),
  332. HDMI_CFG(hpd_clk, 8x74),
  333. .hpd_freq = hpd_clk_freq_8x74,
  334. };
  335. static const struct {
  336. const char *name;
  337. const bool output;
  338. const int value;
  339. const char *label;
  340. } msm_hdmi_gpio_pdata[] = {
  341. { "qcom,hdmi-tx-ddc-clk", true, 1, "HDMI_DDC_CLK" },
  342. { "qcom,hdmi-tx-ddc-data", true, 1, "HDMI_DDC_DATA" },
  343. { "qcom,hdmi-tx-hpd", false, 1, "HDMI_HPD" },
  344. { "qcom,hdmi-tx-mux-en", true, 1, "HDMI_MUX_EN" },
  345. { "qcom,hdmi-tx-mux-sel", true, 0, "HDMI_MUX_SEL" },
  346. { "qcom,hdmi-tx-mux-lpm", true, 1, "HDMI_MUX_LPM" },
  347. };
  348. static int msm_hdmi_get_gpio(struct device_node *of_node, const char *name)
  349. {
  350. int gpio = of_get_named_gpio(of_node, name, 0);
  351. if (gpio < 0) {
  352. char name2[32];
  353. snprintf(name2, sizeof(name2), "%s-gpio", name);
  354. gpio = of_get_named_gpio(of_node, name2, 0);
  355. if (gpio < 0) {
  356. DBG("failed to get gpio: %s (%d)", name, gpio);
  357. gpio = -1;
  358. }
  359. }
  360. return gpio;
  361. }
  362. static int msm_hdmi_bind(struct device *dev, struct device *master, void *data)
  363. {
  364. struct drm_device *drm = dev_get_drvdata(master);
  365. struct msm_drm_private *priv = drm->dev_private;
  366. static struct hdmi_platform_config *hdmi_cfg;
  367. struct hdmi *hdmi;
  368. struct device_node *of_node = dev->of_node;
  369. int i;
  370. hdmi_cfg = (struct hdmi_platform_config *)
  371. of_device_get_match_data(dev);
  372. if (!hdmi_cfg) {
  373. dev_err(dev, "unknown hdmi_cfg: %s\n", of_node->name);
  374. return -ENXIO;
  375. }
  376. hdmi_cfg->mmio_name = "core_physical";
  377. hdmi_cfg->qfprom_mmio_name = "qfprom_physical";
  378. for (i = 0; i < HDMI_MAX_NUM_GPIO; i++) {
  379. hdmi_cfg->gpios[i].num = msm_hdmi_get_gpio(of_node,
  380. msm_hdmi_gpio_pdata[i].name);
  381. hdmi_cfg->gpios[i].output = msm_hdmi_gpio_pdata[i].output;
  382. hdmi_cfg->gpios[i].value = msm_hdmi_gpio_pdata[i].value;
  383. hdmi_cfg->gpios[i].label = msm_hdmi_gpio_pdata[i].label;
  384. }
  385. dev->platform_data = hdmi_cfg;
  386. hdmi = msm_hdmi_init(to_platform_device(dev));
  387. if (IS_ERR(hdmi))
  388. return PTR_ERR(hdmi);
  389. priv->hdmi = hdmi;
  390. return 0;
  391. }
  392. static void msm_hdmi_unbind(struct device *dev, struct device *master,
  393. void *data)
  394. {
  395. struct drm_device *drm = dev_get_drvdata(master);
  396. struct msm_drm_private *priv = drm->dev_private;
  397. if (priv->hdmi) {
  398. msm_hdmi_destroy(priv->hdmi);
  399. priv->hdmi = NULL;
  400. }
  401. }
  402. static const struct component_ops msm_hdmi_ops = {
  403. .bind = msm_hdmi_bind,
  404. .unbind = msm_hdmi_unbind,
  405. };
  406. static int msm_hdmi_dev_probe(struct platform_device *pdev)
  407. {
  408. return component_add(&pdev->dev, &msm_hdmi_ops);
  409. }
  410. static int msm_hdmi_dev_remove(struct platform_device *pdev)
  411. {
  412. component_del(&pdev->dev, &msm_hdmi_ops);
  413. return 0;
  414. }
  415. static const struct of_device_id msm_hdmi_dt_match[] = {
  416. { .compatible = "qcom,hdmi-tx-8996", .data = &hdmi_tx_8996_config },
  417. { .compatible = "qcom,hdmi-tx-8994", .data = &hdmi_tx_8994_config },
  418. { .compatible = "qcom,hdmi-tx-8084", .data = &hdmi_tx_8084_config },
  419. { .compatible = "qcom,hdmi-tx-8974", .data = &hdmi_tx_8974_config },
  420. { .compatible = "qcom,hdmi-tx-8960", .data = &hdmi_tx_8960_config },
  421. { .compatible = "qcom,hdmi-tx-8660", .data = &hdmi_tx_8660_config },
  422. {}
  423. };
  424. static struct platform_driver msm_hdmi_driver = {
  425. .probe = msm_hdmi_dev_probe,
  426. .remove = msm_hdmi_dev_remove,
  427. .driver = {
  428. .name = "hdmi_msm",
  429. .of_match_table = msm_hdmi_dt_match,
  430. },
  431. };
  432. void __init msm_hdmi_register(void)
  433. {
  434. msm_hdmi_phy_driver_register();
  435. platform_driver_register(&msm_hdmi_driver);
  436. }
  437. void __exit msm_hdmi_unregister(void)
  438. {
  439. platform_driver_unregister(&msm_hdmi_driver);
  440. msm_hdmi_phy_driver_unregister();
  441. }