hdmi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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_exclusive(&pdev->dev,
  102. 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_exclusive(&pdev->dev,
  115. config->pwr_reg_names[i]);
  116. if (IS_ERR(reg)) {
  117. ret = PTR_ERR(reg);
  118. dev_err(dev->dev, "failed to get pwr regulator: %s (%d)\n",
  119. config->pwr_reg_names[i], ret);
  120. goto fail;
  121. }
  122. hdmi->pwr_regs[i] = reg;
  123. }
  124. BUG_ON(config->hpd_clk_cnt > ARRAY_SIZE(hdmi->hpd_clks));
  125. for (i = 0; i < config->hpd_clk_cnt; i++) {
  126. struct clk *clk;
  127. clk = devm_clk_get(&pdev->dev, config->hpd_clk_names[i]);
  128. if (IS_ERR(clk)) {
  129. ret = PTR_ERR(clk);
  130. dev_err(dev->dev, "failed to get hpd clk: %s (%d)\n",
  131. config->hpd_clk_names[i], ret);
  132. goto fail;
  133. }
  134. hdmi->hpd_clks[i] = clk;
  135. }
  136. BUG_ON(config->pwr_clk_cnt > ARRAY_SIZE(hdmi->pwr_clks));
  137. for (i = 0; i < config->pwr_clk_cnt; i++) {
  138. struct clk *clk;
  139. clk = devm_clk_get(&pdev->dev, config->pwr_clk_names[i]);
  140. if (IS_ERR(clk)) {
  141. ret = PTR_ERR(clk);
  142. dev_err(dev->dev, "failed to get pwr clk: %s (%d)\n",
  143. config->pwr_clk_names[i], ret);
  144. goto fail;
  145. }
  146. hdmi->pwr_clks[i] = clk;
  147. }
  148. hdmi->i2c = hdmi_i2c_init(hdmi);
  149. if (IS_ERR(hdmi->i2c)) {
  150. ret = PTR_ERR(hdmi->i2c);
  151. dev_err(dev->dev, "failed to get i2c: %d\n", ret);
  152. hdmi->i2c = NULL;
  153. goto fail;
  154. }
  155. hdmi->bridge = hdmi_bridge_init(hdmi);
  156. if (IS_ERR(hdmi->bridge)) {
  157. ret = PTR_ERR(hdmi->bridge);
  158. dev_err(dev->dev, "failed to create HDMI bridge: %d\n", ret);
  159. hdmi->bridge = NULL;
  160. goto fail;
  161. }
  162. hdmi->connector = hdmi_connector_init(hdmi);
  163. if (IS_ERR(hdmi->connector)) {
  164. ret = PTR_ERR(hdmi->connector);
  165. dev_err(dev->dev, "failed to create HDMI connector: %d\n", ret);
  166. hdmi->connector = NULL;
  167. goto fail;
  168. }
  169. if (!config->shared_irq) {
  170. hdmi->irq = platform_get_irq(pdev, 0);
  171. if (hdmi->irq < 0) {
  172. ret = hdmi->irq;
  173. dev_err(dev->dev, "failed to get irq: %d\n", ret);
  174. goto fail;
  175. }
  176. ret = devm_request_threaded_irq(&pdev->dev, hdmi->irq,
  177. NULL, hdmi_irq, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  178. "hdmi_isr", hdmi);
  179. if (ret < 0) {
  180. dev_err(dev->dev, "failed to request IRQ%u: %d\n",
  181. hdmi->irq, ret);
  182. goto fail;
  183. }
  184. }
  185. encoder->bridge = hdmi->bridge;
  186. priv->bridges[priv->num_bridges++] = hdmi->bridge;
  187. priv->connectors[priv->num_connectors++] = hdmi->connector;
  188. platform_set_drvdata(pdev, hdmi);
  189. return hdmi;
  190. fail:
  191. if (hdmi) {
  192. /* bridge/connector are normally destroyed by drm: */
  193. if (hdmi->bridge)
  194. hdmi->bridge->funcs->destroy(hdmi->bridge);
  195. if (hdmi->connector)
  196. hdmi->connector->funcs->destroy(hdmi->connector);
  197. hdmi_destroy(&hdmi->refcount);
  198. }
  199. return ERR_PTR(ret);
  200. }
  201. /*
  202. * The hdmi device:
  203. */
  204. #include <linux/of_gpio.h>
  205. static void set_hdmi_pdev(struct drm_device *dev,
  206. struct platform_device *pdev)
  207. {
  208. struct msm_drm_private *priv = dev->dev_private;
  209. priv->hdmi_pdev = pdev;
  210. }
  211. #ifdef CONFIG_OF
  212. static int get_gpio(struct device *dev, struct device_node *of_node, const char *name)
  213. {
  214. int gpio = of_get_named_gpio(of_node, name, 0);
  215. if (gpio < 0) {
  216. char name2[32];
  217. snprintf(name2, sizeof(name2), "%s-gpio", name);
  218. gpio = of_get_named_gpio(of_node, name2, 0);
  219. if (gpio < 0) {
  220. dev_err(dev, "failed to get gpio: %s (%d)\n",
  221. name, gpio);
  222. gpio = -1;
  223. }
  224. }
  225. return gpio;
  226. }
  227. #endif
  228. static int hdmi_bind(struct device *dev, struct device *master, void *data)
  229. {
  230. static struct hdmi_platform_config config = {};
  231. #ifdef CONFIG_OF
  232. struct device_node *of_node = dev->of_node;
  233. if (of_device_is_compatible(of_node, "qcom,hdmi-tx-8074")) {
  234. static const char *hpd_reg_names[] = {"hpd-gdsc", "hpd-5v"};
  235. static const char *pwr_reg_names[] = {"core-vdda", "core-vcc"};
  236. static const char *hpd_clk_names[] = {"iface_clk", "core_clk", "mdp_core_clk"};
  237. static unsigned long hpd_clk_freq[] = {0, 19200000, 0};
  238. static const char *pwr_clk_names[] = {"extp_clk", "alt_iface_clk"};
  239. config.phy_init = hdmi_phy_8x74_init;
  240. config.hpd_reg_names = hpd_reg_names;
  241. config.hpd_reg_cnt = ARRAY_SIZE(hpd_reg_names);
  242. config.pwr_reg_names = pwr_reg_names;
  243. config.pwr_reg_cnt = ARRAY_SIZE(pwr_reg_names);
  244. config.hpd_clk_names = hpd_clk_names;
  245. config.hpd_freq = hpd_clk_freq;
  246. config.hpd_clk_cnt = ARRAY_SIZE(hpd_clk_names);
  247. config.pwr_clk_names = pwr_clk_names;
  248. config.pwr_clk_cnt = ARRAY_SIZE(pwr_clk_names);
  249. config.shared_irq = true;
  250. } else if (of_device_is_compatible(of_node, "qcom,hdmi-tx-8960")) {
  251. static const char *hpd_clk_names[] = {"core_clk", "master_iface_clk", "slave_iface_clk"};
  252. static const char *hpd_reg_names[] = {"core-vdda", "hdmi-mux"};
  253. config.phy_init = hdmi_phy_8960_init;
  254. config.hpd_reg_names = hpd_reg_names;
  255. config.hpd_reg_cnt = ARRAY_SIZE(hpd_reg_names);
  256. config.hpd_clk_names = hpd_clk_names;
  257. config.hpd_clk_cnt = ARRAY_SIZE(hpd_clk_names);
  258. } else if (of_device_is_compatible(of_node, "qcom,hdmi-tx-8660")) {
  259. config.phy_init = hdmi_phy_8x60_init;
  260. } else {
  261. dev_err(dev, "unknown phy: %s\n", of_node->name);
  262. }
  263. config.mmio_name = "core_physical";
  264. config.ddc_clk_gpio = get_gpio(dev, of_node, "qcom,hdmi-tx-ddc-clk");
  265. config.ddc_data_gpio = get_gpio(dev, of_node, "qcom,hdmi-tx-ddc-data");
  266. config.hpd_gpio = get_gpio(dev, of_node, "qcom,hdmi-tx-hpd");
  267. config.mux_en_gpio = get_gpio(dev, of_node, "qcom,hdmi-tx-mux-en");
  268. config.mux_sel_gpio = get_gpio(dev, of_node, "qcom,hdmi-tx-mux-sel");
  269. config.mux_lpm_gpio = get_gpio(dev, of_node, "qcom,hdmi-tx-mux-lpm");
  270. #else
  271. static const char *hpd_clk_names[] = {
  272. "core_clk", "master_iface_clk", "slave_iface_clk",
  273. };
  274. if (cpu_is_apq8064()) {
  275. static const char *hpd_reg_names[] = {"8921_hdmi_mvs"};
  276. config.phy_init = hdmi_phy_8960_init;
  277. config.mmio_name = "hdmi_msm_hdmi_addr";
  278. config.hpd_reg_names = hpd_reg_names;
  279. config.hpd_reg_cnt = ARRAY_SIZE(hpd_reg_names);
  280. config.hpd_clk_names = hpd_clk_names;
  281. config.hpd_clk_cnt = ARRAY_SIZE(hpd_clk_names);
  282. config.ddc_clk_gpio = 70;
  283. config.ddc_data_gpio = 71;
  284. config.hpd_gpio = 72;
  285. config.mux_en_gpio = -1;
  286. config.mux_sel_gpio = -1;
  287. } else if (cpu_is_msm8960() || cpu_is_msm8960ab()) {
  288. static const char *hpd_reg_names[] = {"8921_hdmi_mvs"};
  289. config.phy_init = hdmi_phy_8960_init;
  290. config.mmio_name = "hdmi_msm_hdmi_addr";
  291. config.hpd_reg_names = hpd_reg_names;
  292. config.hpd_reg_cnt = ARRAY_SIZE(hpd_reg_names);
  293. config.hpd_clk_names = hpd_clk_names;
  294. config.hpd_clk_cnt = ARRAY_SIZE(hpd_clk_names);
  295. config.ddc_clk_gpio = 100;
  296. config.ddc_data_gpio = 101;
  297. config.hpd_gpio = 102;
  298. config.mux_en_gpio = -1;
  299. config.mux_sel_gpio = -1;
  300. } else if (cpu_is_msm8x60()) {
  301. static const char *hpd_reg_names[] = {
  302. "8901_hdmi_mvs", "8901_mpp0"
  303. };
  304. config.phy_init = hdmi_phy_8x60_init;
  305. config.mmio_name = "hdmi_msm_hdmi_addr";
  306. config.hpd_reg_names = hpd_reg_names;
  307. config.hpd_reg_cnt = ARRAY_SIZE(hpd_reg_names);
  308. config.hpd_clk_names = hpd_clk_names;
  309. config.hpd_clk_cnt = ARRAY_SIZE(hpd_clk_names);
  310. config.ddc_clk_gpio = 170;
  311. config.ddc_data_gpio = 171;
  312. config.hpd_gpio = 172;
  313. config.mux_en_gpio = -1;
  314. config.mux_sel_gpio = -1;
  315. }
  316. #endif
  317. dev->platform_data = &config;
  318. set_hdmi_pdev(dev_get_drvdata(master), to_platform_device(dev));
  319. return 0;
  320. }
  321. static void hdmi_unbind(struct device *dev, struct device *master,
  322. void *data)
  323. {
  324. set_hdmi_pdev(dev_get_drvdata(master), NULL);
  325. }
  326. static const struct component_ops hdmi_ops = {
  327. .bind = hdmi_bind,
  328. .unbind = hdmi_unbind,
  329. };
  330. static int hdmi_dev_probe(struct platform_device *pdev)
  331. {
  332. return component_add(&pdev->dev, &hdmi_ops);
  333. }
  334. static int hdmi_dev_remove(struct platform_device *pdev)
  335. {
  336. component_del(&pdev->dev, &hdmi_ops);
  337. return 0;
  338. }
  339. static const struct of_device_id dt_match[] = {
  340. { .compatible = "qcom,hdmi-tx-8074" },
  341. { .compatible = "qcom,hdmi-tx-8960" },
  342. { .compatible = "qcom,hdmi-tx-8660" },
  343. {}
  344. };
  345. static struct platform_driver hdmi_driver = {
  346. .probe = hdmi_dev_probe,
  347. .remove = hdmi_dev_remove,
  348. .driver = {
  349. .name = "hdmi_msm",
  350. .of_match_table = dt_match,
  351. },
  352. };
  353. void __init hdmi_register(void)
  354. {
  355. platform_driver_register(&hdmi_driver);
  356. }
  357. void __exit hdmi_unregister(void)
  358. {
  359. platform_driver_unregister(&hdmi_driver);
  360. }