hdmi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Copyright (C) 2013 Red Hat
  3. * Author: Rob Clark <robdclark@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "hdmi.h"
  18. void hdmi_set_mode(struct hdmi *hdmi, bool power_on)
  19. {
  20. uint32_t ctrl = 0;
  21. if (power_on) {
  22. ctrl |= HDMI_CTRL_ENABLE;
  23. if (!hdmi->hdmi_mode) {
  24. ctrl |= HDMI_CTRL_HDMI;
  25. hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
  26. ctrl &= ~HDMI_CTRL_HDMI;
  27. } else {
  28. ctrl |= HDMI_CTRL_HDMI;
  29. }
  30. } else {
  31. ctrl = HDMI_CTRL_HDMI;
  32. }
  33. hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
  34. DBG("HDMI Core: %s, HDMI_CTRL=0x%08x",
  35. power_on ? "Enable" : "Disable", ctrl);
  36. }
  37. irqreturn_t hdmi_irq(int irq, void *dev_id)
  38. {
  39. struct hdmi *hdmi = dev_id;
  40. /* Process HPD: */
  41. hdmi_connector_irq(hdmi->connector);
  42. /* Process DDC: */
  43. hdmi_i2c_irq(hdmi->i2c);
  44. /* TODO audio.. */
  45. return IRQ_HANDLED;
  46. }
  47. void hdmi_destroy(struct kref *kref)
  48. {
  49. struct hdmi *hdmi = container_of(kref, struct hdmi, refcount);
  50. struct hdmi_phy *phy = hdmi->phy;
  51. if (phy)
  52. phy->funcs->destroy(phy);
  53. if (hdmi->i2c)
  54. hdmi_i2c_destroy(hdmi->i2c);
  55. platform_set_drvdata(hdmi->pdev, NULL);
  56. }
  57. /* initialize connector */
  58. struct hdmi *hdmi_init(struct drm_device *dev, struct drm_encoder *encoder)
  59. {
  60. struct hdmi *hdmi = NULL;
  61. struct msm_drm_private *priv = dev->dev_private;
  62. struct platform_device *pdev = priv->hdmi_pdev;
  63. struct hdmi_platform_config *config;
  64. int i, ret;
  65. if (!pdev) {
  66. dev_err(dev->dev, "no hdmi device\n");
  67. ret = -ENXIO;
  68. goto fail;
  69. }
  70. config = pdev->dev.platform_data;
  71. hdmi = kzalloc(sizeof(*hdmi), GFP_KERNEL);
  72. if (!hdmi) {
  73. ret = -ENOMEM;
  74. goto fail;
  75. }
  76. kref_init(&hdmi->refcount);
  77. hdmi->dev = dev;
  78. hdmi->pdev = pdev;
  79. hdmi->config = config;
  80. hdmi->encoder = encoder;
  81. hdmi_audio_infoframe_init(&hdmi->audio.infoframe);
  82. /* not sure about which phy maps to which msm.. probably I miss some */
  83. if (config->phy_init)
  84. hdmi->phy = config->phy_init(hdmi);
  85. else
  86. hdmi->phy = ERR_PTR(-ENXIO);
  87. if (IS_ERR(hdmi->phy)) {
  88. ret = PTR_ERR(hdmi->phy);
  89. dev_err(dev->dev, "failed to load phy: %d\n", ret);
  90. hdmi->phy = NULL;
  91. goto fail;
  92. }
  93. hdmi->mmio = msm_ioremap(pdev, config->mmio_name, "HDMI");
  94. if (IS_ERR(hdmi->mmio)) {
  95. ret = PTR_ERR(hdmi->mmio);
  96. goto fail;
  97. }
  98. BUG_ON(config->hpd_reg_cnt > ARRAY_SIZE(hdmi->hpd_regs));
  99. for (i = 0; i < config->hpd_reg_cnt; i++) {
  100. struct regulator *reg;
  101. reg = devm_regulator_get(&pdev->dev, config->hpd_reg_names[i]);
  102. if (IS_ERR(reg)) {
  103. ret = PTR_ERR(reg);
  104. dev_err(dev->dev, "failed to get hpd regulator: %s (%d)\n",
  105. config->hpd_reg_names[i], ret);
  106. goto fail;
  107. }
  108. hdmi->hpd_regs[i] = reg;
  109. }
  110. BUG_ON(config->pwr_reg_cnt > ARRAY_SIZE(hdmi->pwr_regs));
  111. for (i = 0; i < config->pwr_reg_cnt; i++) {
  112. struct regulator *reg;
  113. reg = devm_regulator_get(&pdev->dev, config->pwr_reg_names[i]);
  114. if (IS_ERR(reg)) {
  115. ret = PTR_ERR(reg);
  116. dev_err(dev->dev, "failed to get pwr regulator: %s (%d)\n",
  117. config->pwr_reg_names[i], ret);
  118. goto fail;
  119. }
  120. hdmi->pwr_regs[i] = reg;
  121. }
  122. BUG_ON(config->hpd_clk_cnt > ARRAY_SIZE(hdmi->hpd_clks));
  123. for (i = 0; i < config->hpd_clk_cnt; i++) {
  124. struct clk *clk;
  125. clk = devm_clk_get(&pdev->dev, config->hpd_clk_names[i]);
  126. if (IS_ERR(clk)) {
  127. ret = PTR_ERR(clk);
  128. dev_err(dev->dev, "failed to get hpd clk: %s (%d)\n",
  129. config->hpd_clk_names[i], ret);
  130. goto fail;
  131. }
  132. hdmi->hpd_clks[i] = clk;
  133. }
  134. BUG_ON(config->pwr_clk_cnt > ARRAY_SIZE(hdmi->pwr_clks));
  135. for (i = 0; i < config->pwr_clk_cnt; i++) {
  136. struct clk *clk;
  137. clk = devm_clk_get(&pdev->dev, config->pwr_clk_names[i]);
  138. if (IS_ERR(clk)) {
  139. ret = PTR_ERR(clk);
  140. dev_err(dev->dev, "failed to get pwr clk: %s (%d)\n",
  141. config->pwr_clk_names[i], ret);
  142. goto fail;
  143. }
  144. hdmi->pwr_clks[i] = clk;
  145. }
  146. hdmi->i2c = hdmi_i2c_init(hdmi);
  147. if (IS_ERR(hdmi->i2c)) {
  148. ret = PTR_ERR(hdmi->i2c);
  149. dev_err(dev->dev, "failed to get i2c: %d\n", ret);
  150. hdmi->i2c = NULL;
  151. goto fail;
  152. }
  153. hdmi->bridge = hdmi_bridge_init(hdmi);
  154. if (IS_ERR(hdmi->bridge)) {
  155. ret = PTR_ERR(hdmi->bridge);
  156. dev_err(dev->dev, "failed to create HDMI bridge: %d\n", ret);
  157. hdmi->bridge = NULL;
  158. goto fail;
  159. }
  160. hdmi->connector = hdmi_connector_init(hdmi);
  161. if (IS_ERR(hdmi->connector)) {
  162. ret = PTR_ERR(hdmi->connector);
  163. dev_err(dev->dev, "failed to create HDMI connector: %d\n", ret);
  164. hdmi->connector = NULL;
  165. goto fail;
  166. }
  167. if (!config->shared_irq) {
  168. hdmi->irq = platform_get_irq(pdev, 0);
  169. if (hdmi->irq < 0) {
  170. ret = hdmi->irq;
  171. dev_err(dev->dev, "failed to get irq: %d\n", ret);
  172. goto fail;
  173. }
  174. ret = devm_request_threaded_irq(&pdev->dev, hdmi->irq,
  175. NULL, hdmi_irq, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  176. "hdmi_isr", hdmi);
  177. if (ret < 0) {
  178. dev_err(dev->dev, "failed to request IRQ%u: %d\n",
  179. hdmi->irq, ret);
  180. goto fail;
  181. }
  182. }
  183. encoder->bridge = hdmi->bridge;
  184. priv->bridges[priv->num_bridges++] = hdmi->bridge;
  185. priv->connectors[priv->num_connectors++] = hdmi->connector;
  186. platform_set_drvdata(pdev, hdmi);
  187. return hdmi;
  188. fail:
  189. if (hdmi) {
  190. /* bridge/connector are normally destroyed by drm: */
  191. if (hdmi->bridge)
  192. hdmi->bridge->funcs->destroy(hdmi->bridge);
  193. if (hdmi->connector)
  194. hdmi->connector->funcs->destroy(hdmi->connector);
  195. hdmi_destroy(&hdmi->refcount);
  196. }
  197. return ERR_PTR(ret);
  198. }
  199. /*
  200. * The hdmi device:
  201. */
  202. #include <linux/of_gpio.h>
  203. static void set_hdmi_pdev(struct drm_device *dev,
  204. struct platform_device *pdev)
  205. {
  206. struct msm_drm_private *priv = dev->dev_private;
  207. priv->hdmi_pdev = pdev;
  208. }
  209. static int hdmi_bind(struct device *dev, struct device *master, void *data)
  210. {
  211. static struct hdmi_platform_config config = {};
  212. #ifdef CONFIG_OF
  213. struct device_node *of_node = dev->of_node;
  214. int get_gpio(const char *name)
  215. {
  216. int gpio = of_get_named_gpio(of_node, name, 0);
  217. if (gpio < 0) {
  218. dev_err(dev, "failed to get gpio: %s (%d)\n",
  219. name, gpio);
  220. gpio = -1;
  221. }
  222. return gpio;
  223. }
  224. /* TODO actually use DT.. */
  225. static const char *hpd_reg_names[] = {"hpd-gdsc", "hpd-5v"};
  226. static const char *pwr_reg_names[] = {"core-vdda", "core-vcc"};
  227. static const char *hpd_clk_names[] = {"iface_clk", "core_clk", "mdp_core_clk"};
  228. static unsigned long hpd_clk_freq[] = {0, 19200000, 0};
  229. static const char *pwr_clk_names[] = {"extp_clk", "alt_iface_clk"};
  230. config.phy_init = hdmi_phy_8x74_init;
  231. config.mmio_name = "core_physical";
  232. config.hpd_reg_names = hpd_reg_names;
  233. config.hpd_reg_cnt = ARRAY_SIZE(hpd_reg_names);
  234. config.pwr_reg_names = pwr_reg_names;
  235. config.pwr_reg_cnt = ARRAY_SIZE(pwr_reg_names);
  236. config.hpd_clk_names = hpd_clk_names;
  237. config.hpd_freq = hpd_clk_freq;
  238. config.hpd_clk_cnt = ARRAY_SIZE(hpd_clk_names);
  239. config.pwr_clk_names = pwr_clk_names;
  240. config.pwr_clk_cnt = ARRAY_SIZE(pwr_clk_names);
  241. config.ddc_clk_gpio = get_gpio("qcom,hdmi-tx-ddc-clk");
  242. config.ddc_data_gpio = get_gpio("qcom,hdmi-tx-ddc-data");
  243. config.hpd_gpio = get_gpio("qcom,hdmi-tx-hpd");
  244. config.mux_en_gpio = get_gpio("qcom,hdmi-tx-mux-en");
  245. config.mux_sel_gpio = get_gpio("qcom,hdmi-tx-mux-sel");
  246. config.shared_irq = true;
  247. #else
  248. static const char *hpd_clk_names[] = {
  249. "core_clk", "master_iface_clk", "slave_iface_clk",
  250. };
  251. if (cpu_is_apq8064()) {
  252. static const char *hpd_reg_names[] = {"8921_hdmi_mvs"};
  253. config.phy_init = hdmi_phy_8960_init;
  254. config.mmio_name = "hdmi_msm_hdmi_addr";
  255. config.hpd_reg_names = hpd_reg_names;
  256. config.hpd_reg_cnt = ARRAY_SIZE(hpd_reg_names);
  257. config.hpd_clk_names = hpd_clk_names;
  258. config.hpd_clk_cnt = ARRAY_SIZE(hpd_clk_names);
  259. config.ddc_clk_gpio = 70;
  260. config.ddc_data_gpio = 71;
  261. config.hpd_gpio = 72;
  262. config.mux_en_gpio = -1;
  263. config.mux_sel_gpio = -1;
  264. } else if (cpu_is_msm8960() || cpu_is_msm8960ab()) {
  265. static const char *hpd_reg_names[] = {"8921_hdmi_mvs"};
  266. config.phy_init = hdmi_phy_8960_init;
  267. config.mmio_name = "hdmi_msm_hdmi_addr";
  268. config.hpd_reg_names = hpd_reg_names;
  269. config.hpd_reg_cnt = ARRAY_SIZE(hpd_reg_names);
  270. config.hpd_clk_names = hpd_clk_names;
  271. config.hpd_clk_cnt = ARRAY_SIZE(hpd_clk_names);
  272. config.ddc_clk_gpio = 100;
  273. config.ddc_data_gpio = 101;
  274. config.hpd_gpio = 102;
  275. config.mux_en_gpio = -1;
  276. config.mux_sel_gpio = -1;
  277. } else if (cpu_is_msm8x60()) {
  278. static const char *hpd_reg_names[] = {
  279. "8901_hdmi_mvs", "8901_mpp0"
  280. };
  281. config.phy_init = hdmi_phy_8x60_init;
  282. config.mmio_name = "hdmi_msm_hdmi_addr";
  283. config.hpd_reg_names = hpd_reg_names;
  284. config.hpd_reg_cnt = ARRAY_SIZE(hpd_reg_names);
  285. config.hpd_clk_names = hpd_clk_names;
  286. config.hpd_clk_cnt = ARRAY_SIZE(hpd_clk_names);
  287. config.ddc_clk_gpio = 170;
  288. config.ddc_data_gpio = 171;
  289. config.hpd_gpio = 172;
  290. config.mux_en_gpio = -1;
  291. config.mux_sel_gpio = -1;
  292. }
  293. #endif
  294. dev->platform_data = &config;
  295. set_hdmi_pdev(dev_get_drvdata(master), to_platform_device(dev));
  296. return 0;
  297. }
  298. static void hdmi_unbind(struct device *dev, struct device *master,
  299. void *data)
  300. {
  301. set_hdmi_pdev(dev_get_drvdata(master), NULL);
  302. }
  303. static const struct component_ops hdmi_ops = {
  304. .bind = hdmi_bind,
  305. .unbind = hdmi_unbind,
  306. };
  307. static int hdmi_dev_probe(struct platform_device *pdev)
  308. {
  309. return component_add(&pdev->dev, &hdmi_ops);
  310. }
  311. static int hdmi_dev_remove(struct platform_device *pdev)
  312. {
  313. component_del(&pdev->dev, &hdmi_ops);
  314. return 0;
  315. }
  316. static const struct of_device_id dt_match[] = {
  317. { .compatible = "qcom,hdmi-tx" },
  318. {}
  319. };
  320. static struct platform_driver hdmi_driver = {
  321. .probe = hdmi_dev_probe,
  322. .remove = hdmi_dev_remove,
  323. .driver = {
  324. .name = "hdmi_msm",
  325. .of_match_table = dt_match,
  326. },
  327. };
  328. void __init hdmi_register(void)
  329. {
  330. platform_driver_register(&hdmi_driver);
  331. }
  332. void __exit hdmi_unregister(void)
  333. {
  334. platform_driver_unregister(&hdmi_driver);
  335. }