hdmi.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. static struct platform_device *hdmi_pdev;
  19. void hdmi_set_mode(struct hdmi *hdmi, bool power_on)
  20. {
  21. uint32_t ctrl = 0;
  22. if (power_on) {
  23. ctrl |= HDMI_CTRL_ENABLE;
  24. if (!hdmi->hdmi_mode) {
  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. DBG("HDMI Core: %s, HDMI_CTRL=0x%08x",
  36. power_on ? "Enable" : "Disable", ctrl);
  37. }
  38. irqreturn_t hdmi_irq(int irq, void *dev_id)
  39. {
  40. struct hdmi *hdmi = dev_id;
  41. /* Process HPD: */
  42. hdmi_connector_irq(hdmi->connector);
  43. /* Process DDC: */
  44. hdmi_i2c_irq(hdmi->i2c);
  45. /* TODO audio.. */
  46. return IRQ_HANDLED;
  47. }
  48. void hdmi_destroy(struct kref *kref)
  49. {
  50. struct hdmi *hdmi = container_of(kref, struct hdmi, refcount);
  51. struct hdmi_phy *phy = hdmi->phy;
  52. if (phy)
  53. phy->funcs->destroy(phy);
  54. if (hdmi->i2c)
  55. hdmi_i2c_destroy(hdmi->i2c);
  56. put_device(&hdmi->pdev->dev);
  57. }
  58. /* initialize connector */
  59. struct hdmi *hdmi_init(struct drm_device *dev, struct drm_encoder *encoder)
  60. {
  61. struct hdmi *hdmi = NULL;
  62. struct msm_drm_private *priv = dev->dev_private;
  63. struct platform_device *pdev = hdmi_pdev;
  64. struct hdmi_platform_config *config;
  65. int i, ret;
  66. if (!pdev) {
  67. dev_err(dev->dev, "no hdmi device\n");
  68. ret = -ENXIO;
  69. goto fail;
  70. }
  71. config = pdev->dev.platform_data;
  72. hdmi = kzalloc(sizeof(*hdmi), GFP_KERNEL);
  73. if (!hdmi) {
  74. ret = -ENOMEM;
  75. goto fail;
  76. }
  77. kref_init(&hdmi->refcount);
  78. get_device(&pdev->dev);
  79. hdmi->dev = dev;
  80. hdmi->pdev = pdev;
  81. hdmi->config = config;
  82. hdmi->encoder = encoder;
  83. /* not sure about which phy maps to which msm.. probably I miss some */
  84. if (config->phy_init)
  85. hdmi->phy = config->phy_init(hdmi);
  86. else
  87. hdmi->phy = ERR_PTR(-ENXIO);
  88. if (IS_ERR(hdmi->phy)) {
  89. ret = PTR_ERR(hdmi->phy);
  90. dev_err(dev->dev, "failed to load phy: %d\n", ret);
  91. hdmi->phy = NULL;
  92. goto fail;
  93. }
  94. hdmi->mmio = msm_ioremap(pdev, config->mmio_name, "HDMI");
  95. if (IS_ERR(hdmi->mmio)) {
  96. ret = PTR_ERR(hdmi->mmio);
  97. goto fail;
  98. }
  99. BUG_ON(config->hpd_reg_cnt > ARRAY_SIZE(hdmi->hpd_regs));
  100. for (i = 0; i < config->hpd_reg_cnt; i++) {
  101. struct regulator *reg;
  102. reg = devm_regulator_get(&pdev->dev, config->hpd_reg_names[i]);
  103. if (IS_ERR(reg)) {
  104. ret = PTR_ERR(reg);
  105. dev_err(dev->dev, "failed to get hpd regulator: %s (%d)\n",
  106. config->hpd_reg_names[i], ret);
  107. goto fail;
  108. }
  109. hdmi->hpd_regs[i] = reg;
  110. }
  111. BUG_ON(config->pwr_reg_cnt > ARRAY_SIZE(hdmi->pwr_regs));
  112. for (i = 0; i < config->pwr_reg_cnt; i++) {
  113. struct regulator *reg;
  114. reg = devm_regulator_get(&pdev->dev, config->pwr_reg_names[i]);
  115. if (IS_ERR(reg)) {
  116. ret = PTR_ERR(reg);
  117. dev_err(dev->dev, "failed to get pwr regulator: %s (%d)\n",
  118. config->pwr_reg_names[i], ret);
  119. goto fail;
  120. }
  121. hdmi->pwr_regs[i] = reg;
  122. }
  123. BUG_ON(config->hpd_clk_cnt > ARRAY_SIZE(hdmi->hpd_clks));
  124. for (i = 0; i < config->hpd_clk_cnt; i++) {
  125. struct clk *clk;
  126. clk = devm_clk_get(&pdev->dev, config->hpd_clk_names[i]);
  127. if (IS_ERR(clk)) {
  128. ret = PTR_ERR(clk);
  129. dev_err(dev->dev, "failed to get hpd clk: %s (%d)\n",
  130. config->hpd_clk_names[i], ret);
  131. goto fail;
  132. }
  133. hdmi->hpd_clks[i] = clk;
  134. }
  135. BUG_ON(config->pwr_clk_cnt > ARRAY_SIZE(hdmi->pwr_clks));
  136. for (i = 0; i < config->pwr_clk_cnt; i++) {
  137. struct clk *clk;
  138. clk = devm_clk_get(&pdev->dev, config->pwr_clk_names[i]);
  139. if (IS_ERR(clk)) {
  140. ret = PTR_ERR(clk);
  141. dev_err(dev->dev, "failed to get pwr clk: %s (%d)\n",
  142. config->pwr_clk_names[i], ret);
  143. goto fail;
  144. }
  145. hdmi->pwr_clks[i] = clk;
  146. }
  147. hdmi->i2c = hdmi_i2c_init(hdmi);
  148. if (IS_ERR(hdmi->i2c)) {
  149. ret = PTR_ERR(hdmi->i2c);
  150. dev_err(dev->dev, "failed to get i2c: %d\n", ret);
  151. hdmi->i2c = NULL;
  152. goto fail;
  153. }
  154. hdmi->bridge = hdmi_bridge_init(hdmi);
  155. if (IS_ERR(hdmi->bridge)) {
  156. ret = PTR_ERR(hdmi->bridge);
  157. dev_err(dev->dev, "failed to create HDMI bridge: %d\n", ret);
  158. hdmi->bridge = NULL;
  159. goto fail;
  160. }
  161. hdmi->connector = hdmi_connector_init(hdmi);
  162. if (IS_ERR(hdmi->connector)) {
  163. ret = PTR_ERR(hdmi->connector);
  164. dev_err(dev->dev, "failed to create HDMI connector: %d\n", ret);
  165. hdmi->connector = NULL;
  166. goto fail;
  167. }
  168. if (!config->shared_irq) {
  169. hdmi->irq = platform_get_irq(pdev, 0);
  170. if (hdmi->irq < 0) {
  171. ret = hdmi->irq;
  172. dev_err(dev->dev, "failed to get irq: %d\n", ret);
  173. goto fail;
  174. }
  175. ret = devm_request_threaded_irq(&pdev->dev, hdmi->irq,
  176. NULL, hdmi_irq, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  177. "hdmi_isr", hdmi);
  178. if (ret < 0) {
  179. dev_err(dev->dev, "failed to request IRQ%u: %d\n",
  180. hdmi->irq, ret);
  181. goto fail;
  182. }
  183. }
  184. encoder->bridge = hdmi->bridge;
  185. priv->bridges[priv->num_bridges++] = hdmi->bridge;
  186. priv->connectors[priv->num_connectors++] = hdmi->connector;
  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 int hdmi_dev_probe(struct platform_device *pdev)
  204. {
  205. static struct hdmi_platform_config config = {};
  206. #ifdef CONFIG_OF
  207. struct device_node *of_node = pdev->dev.of_node;
  208. int get_gpio(const char *name)
  209. {
  210. int gpio = of_get_named_gpio(of_node, name, 0);
  211. if (gpio < 0) {
  212. dev_err(&pdev->dev, "failed to get gpio: %s (%d)\n",
  213. name, gpio);
  214. gpio = -1;
  215. }
  216. return gpio;
  217. }
  218. /* TODO actually use DT.. */
  219. static const char *hpd_reg_names[] = {"hpd-gdsc", "hpd-5v"};
  220. static const char *pwr_reg_names[] = {"core-vdda", "core-vcc"};
  221. static const char *hpd_clk_names[] = {"iface_clk", "core_clk", "mdp_core_clk"};
  222. static const char *pwr_clk_names[] = {"extp_clk", "alt_iface_clk"};
  223. config.phy_init = hdmi_phy_8x74_init;
  224. config.mmio_name = "core_physical";
  225. config.hpd_reg_names = hpd_reg_names;
  226. config.hpd_reg_cnt = ARRAY_SIZE(hpd_reg_names);
  227. config.pwr_reg_names = pwr_reg_names;
  228. config.pwr_reg_cnt = ARRAY_SIZE(pwr_reg_names);
  229. config.hpd_clk_names = hpd_clk_names;
  230. config.hpd_clk_cnt = ARRAY_SIZE(hpd_clk_names);
  231. config.pwr_clk_names = pwr_clk_names;
  232. config.pwr_clk_cnt = ARRAY_SIZE(pwr_clk_names);
  233. config.ddc_clk_gpio = get_gpio("qcom,hdmi-tx-ddc-clk");
  234. config.ddc_data_gpio = get_gpio("qcom,hdmi-tx-ddc-data");
  235. config.hpd_gpio = get_gpio("qcom,hdmi-tx-hpd");
  236. config.mux_en_gpio = get_gpio("qcom,hdmi-tx-mux-en");
  237. config.mux_sel_gpio = get_gpio("qcom,hdmi-tx-mux-sel");
  238. config.shared_irq = true;
  239. #else
  240. static const char *hpd_clk_names[] = {
  241. "core_clk", "master_iface_clk", "slave_iface_clk",
  242. };
  243. if (cpu_is_apq8064()) {
  244. static const char *hpd_reg_names[] = {"8921_hdmi_mvs"};
  245. config.phy_init = hdmi_phy_8960_init;
  246. config.mmio_name = "hdmi_msm_hdmi_addr";
  247. config.hpd_reg_names = hpd_reg_names;
  248. config.hpd_reg_cnt = ARRAY_SIZE(hpd_reg_names);
  249. config.hpd_clk_names = hpd_clk_names;
  250. config.hpd_clk_cnt = ARRAY_SIZE(hpd_clk_names);
  251. config.ddc_clk_gpio = 70;
  252. config.ddc_data_gpio = 71;
  253. config.hpd_gpio = 72;
  254. config.mux_en_gpio = -1;
  255. config.mux_sel_gpio = 13 + NR_GPIO_IRQS;
  256. } else if (cpu_is_msm8960() || cpu_is_msm8960ab()) {
  257. static const char *hpd_reg_names[] = {"8921_hdmi_mvs"};
  258. config.phy_init = hdmi_phy_8960_init;
  259. config.mmio_name = "hdmi_msm_hdmi_addr";
  260. config.hpd_reg_names = hpd_reg_names;
  261. config.hpd_reg_cnt = ARRAY_SIZE(hpd_reg_names);
  262. config.hpd_clk_names = hpd_clk_names;
  263. config.hpd_clk_cnt = ARRAY_SIZE(hpd_clk_names);
  264. config.ddc_clk_gpio = 100;
  265. config.ddc_data_gpio = 101;
  266. config.hpd_gpio = 102;
  267. config.mux_en_gpio = -1;
  268. config.mux_sel_gpio = -1;
  269. } else if (cpu_is_msm8x60()) {
  270. static const char *hpd_reg_names[] = {
  271. "8901_hdmi_mvs", "8901_mpp0"
  272. };
  273. config.phy_init = hdmi_phy_8x60_init;
  274. config.mmio_name = "hdmi_msm_hdmi_addr";
  275. config.hpd_reg_names = hpd_reg_names;
  276. config.hpd_reg_cnt = ARRAY_SIZE(hpd_reg_names);
  277. config.hpd_clk_names = hpd_clk_names;
  278. config.hpd_clk_cnt = ARRAY_SIZE(hpd_clk_names);
  279. config.ddc_clk_gpio = 170;
  280. config.ddc_data_gpio = 171;
  281. config.hpd_gpio = 172;
  282. config.mux_en_gpio = -1;
  283. config.mux_sel_gpio = -1;
  284. }
  285. #endif
  286. pdev->dev.platform_data = &config;
  287. hdmi_pdev = pdev;
  288. return 0;
  289. }
  290. static int hdmi_dev_remove(struct platform_device *pdev)
  291. {
  292. hdmi_pdev = NULL;
  293. return 0;
  294. }
  295. static const struct of_device_id dt_match[] = {
  296. { .compatible = "qcom,hdmi-tx" },
  297. {}
  298. };
  299. MODULE_DEVICE_TABLE(of, dt_match);
  300. static struct platform_driver hdmi_driver = {
  301. .probe = hdmi_dev_probe,
  302. .remove = hdmi_dev_remove,
  303. .driver = {
  304. .name = "hdmi_msm",
  305. .of_match_table = dt_match,
  306. },
  307. };
  308. void __init hdmi_register(void)
  309. {
  310. platform_driver_register(&hdmi_driver);
  311. }
  312. void __exit hdmi_unregister(void)
  313. {
  314. platform_driver_unregister(&hdmi_driver);
  315. }