hdmi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 <linux/of_irq.h>
  18. #include "hdmi.h"
  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. static 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. static void hdmi_destroy(struct hdmi *hdmi)
  49. {
  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. /* construct hdmi at bind/probe time, grab all the resources. If
  58. * we are to EPROBE_DEFER we want to do it here, rather than later
  59. * at modeset_init() time
  60. */
  61. static struct hdmi *hdmi_init(struct platform_device *pdev)
  62. {
  63. struct hdmi_platform_config *config = pdev->dev.platform_data;
  64. struct hdmi *hdmi = NULL;
  65. int i, ret;
  66. hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
  67. if (!hdmi) {
  68. ret = -ENOMEM;
  69. goto fail;
  70. }
  71. hdmi->pdev = pdev;
  72. hdmi->config = config;
  73. /* not sure about which phy maps to which msm.. probably I miss some */
  74. if (config->phy_init)
  75. hdmi->phy = config->phy_init(hdmi);
  76. else
  77. hdmi->phy = ERR_PTR(-ENXIO);
  78. if (IS_ERR(hdmi->phy)) {
  79. ret = PTR_ERR(hdmi->phy);
  80. dev_err(&pdev->dev, "failed to load phy: %d\n", ret);
  81. hdmi->phy = NULL;
  82. goto fail;
  83. }
  84. hdmi->mmio = msm_ioremap(pdev, config->mmio_name, "HDMI");
  85. if (IS_ERR(hdmi->mmio)) {
  86. ret = PTR_ERR(hdmi->mmio);
  87. goto fail;
  88. }
  89. BUG_ON(config->hpd_reg_cnt > ARRAY_SIZE(hdmi->hpd_regs));
  90. for (i = 0; i < config->hpd_reg_cnt; i++) {
  91. struct regulator *reg;
  92. reg = devm_regulator_get(&pdev->dev,
  93. config->hpd_reg_names[i]);
  94. if (IS_ERR(reg)) {
  95. ret = PTR_ERR(reg);
  96. dev_err(&pdev->dev, "failed to get hpd regulator: %s (%d)\n",
  97. config->hpd_reg_names[i], ret);
  98. goto fail;
  99. }
  100. hdmi->hpd_regs[i] = reg;
  101. }
  102. BUG_ON(config->pwr_reg_cnt > ARRAY_SIZE(hdmi->pwr_regs));
  103. for (i = 0; i < config->pwr_reg_cnt; i++) {
  104. struct regulator *reg;
  105. reg = devm_regulator_get(&pdev->dev,
  106. config->pwr_reg_names[i]);
  107. if (IS_ERR(reg)) {
  108. ret = PTR_ERR(reg);
  109. dev_err(&pdev->dev, "failed to get pwr regulator: %s (%d)\n",
  110. config->pwr_reg_names[i], ret);
  111. goto fail;
  112. }
  113. hdmi->pwr_regs[i] = reg;
  114. }
  115. BUG_ON(config->hpd_clk_cnt > ARRAY_SIZE(hdmi->hpd_clks));
  116. for (i = 0; i < config->hpd_clk_cnt; i++) {
  117. struct clk *clk;
  118. clk = devm_clk_get(&pdev->dev, config->hpd_clk_names[i]);
  119. if (IS_ERR(clk)) {
  120. ret = PTR_ERR(clk);
  121. dev_err(&pdev->dev, "failed to get hpd clk: %s (%d)\n",
  122. config->hpd_clk_names[i], ret);
  123. goto fail;
  124. }
  125. hdmi->hpd_clks[i] = clk;
  126. }
  127. BUG_ON(config->pwr_clk_cnt > ARRAY_SIZE(hdmi->pwr_clks));
  128. for (i = 0; i < config->pwr_clk_cnt; i++) {
  129. struct clk *clk;
  130. clk = devm_clk_get(&pdev->dev, config->pwr_clk_names[i]);
  131. if (IS_ERR(clk)) {
  132. ret = PTR_ERR(clk);
  133. dev_err(&pdev->dev, "failed to get pwr clk: %s (%d)\n",
  134. config->pwr_clk_names[i], ret);
  135. goto fail;
  136. }
  137. hdmi->pwr_clks[i] = clk;
  138. }
  139. hdmi->i2c = hdmi_i2c_init(hdmi);
  140. if (IS_ERR(hdmi->i2c)) {
  141. ret = PTR_ERR(hdmi->i2c);
  142. dev_err(&pdev->dev, "failed to get i2c: %d\n", ret);
  143. hdmi->i2c = NULL;
  144. goto fail;
  145. }
  146. return hdmi;
  147. fail:
  148. if (hdmi)
  149. hdmi_destroy(hdmi);
  150. return ERR_PTR(ret);
  151. }
  152. /* Second part of initialization, the drm/kms level modeset_init,
  153. * constructs/initializes mode objects, etc, is called from master
  154. * driver (not hdmi sub-device's probe/bind!)
  155. *
  156. * Any resource (regulator/clk/etc) which could be missing at boot
  157. * should be handled in hdmi_init() so that failure happens from
  158. * hdmi sub-device's probe.
  159. */
  160. int hdmi_modeset_init(struct hdmi *hdmi,
  161. struct drm_device *dev, struct drm_encoder *encoder)
  162. {
  163. struct msm_drm_private *priv = dev->dev_private;
  164. struct platform_device *pdev = hdmi->pdev;
  165. int ret;
  166. hdmi->dev = dev;
  167. hdmi->encoder = encoder;
  168. hdmi_audio_infoframe_init(&hdmi->audio.infoframe);
  169. hdmi->bridge = hdmi_bridge_init(hdmi);
  170. if (IS_ERR(hdmi->bridge)) {
  171. ret = PTR_ERR(hdmi->bridge);
  172. dev_err(dev->dev, "failed to create HDMI bridge: %d\n", ret);
  173. hdmi->bridge = NULL;
  174. goto fail;
  175. }
  176. hdmi->connector = hdmi_connector_init(hdmi);
  177. if (IS_ERR(hdmi->connector)) {
  178. ret = PTR_ERR(hdmi->connector);
  179. dev_err(dev->dev, "failed to create HDMI connector: %d\n", ret);
  180. hdmi->connector = NULL;
  181. goto fail;
  182. }
  183. hdmi->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
  184. if (hdmi->irq < 0) {
  185. ret = hdmi->irq;
  186. dev_err(dev->dev, "failed to get irq: %d\n", ret);
  187. goto fail;
  188. }
  189. ret = devm_request_irq(&pdev->dev, hdmi->irq,
  190. hdmi_irq, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  191. "hdmi_isr", hdmi);
  192. if (ret < 0) {
  193. dev_err(dev->dev, "failed to request IRQ%u: %d\n",
  194. hdmi->irq, ret);
  195. goto fail;
  196. }
  197. encoder->bridge = hdmi->bridge;
  198. priv->bridges[priv->num_bridges++] = hdmi->bridge;
  199. priv->connectors[priv->num_connectors++] = hdmi->connector;
  200. platform_set_drvdata(pdev, hdmi);
  201. return 0;
  202. fail:
  203. /* bridge/connector are normally destroyed by drm: */
  204. if (hdmi->bridge) {
  205. hdmi->bridge->funcs->destroy(hdmi->bridge);
  206. hdmi->bridge = NULL;
  207. }
  208. if (hdmi->connector) {
  209. hdmi->connector->funcs->destroy(hdmi->connector);
  210. hdmi->connector = NULL;
  211. }
  212. return ret;
  213. }
  214. /*
  215. * The hdmi device:
  216. */
  217. #include <linux/of_gpio.h>
  218. #ifdef CONFIG_OF
  219. static int get_gpio(struct device *dev, struct device_node *of_node, const char *name)
  220. {
  221. int gpio = of_get_named_gpio(of_node, name, 0);
  222. if (gpio < 0) {
  223. char name2[32];
  224. snprintf(name2, sizeof(name2), "%s-gpio", name);
  225. gpio = of_get_named_gpio(of_node, name2, 0);
  226. if (gpio < 0) {
  227. dev_err(dev, "failed to get gpio: %s (%d)\n",
  228. name, gpio);
  229. gpio = -1;
  230. }
  231. }
  232. return gpio;
  233. }
  234. #endif
  235. static int hdmi_bind(struct device *dev, struct device *master, void *data)
  236. {
  237. struct drm_device *drm = dev_get_drvdata(master);
  238. struct msm_drm_private *priv = drm->dev_private;
  239. static struct hdmi_platform_config config = {};
  240. struct hdmi *hdmi;
  241. #ifdef CONFIG_OF
  242. struct device_node *of_node = dev->of_node;
  243. if (of_device_is_compatible(of_node, "qcom,hdmi-tx-8074")) {
  244. static const char *hpd_reg_names[] = {"hpd-gdsc", "hpd-5v"};
  245. static const char *pwr_reg_names[] = {"core-vdda", "core-vcc"};
  246. static const char *hpd_clk_names[] = {"iface_clk", "core_clk", "mdp_core_clk"};
  247. static unsigned long hpd_clk_freq[] = {0, 19200000, 0};
  248. static const char *pwr_clk_names[] = {"extp_clk", "alt_iface_clk"};
  249. config.phy_init = hdmi_phy_8x74_init;
  250. config.hpd_reg_names = hpd_reg_names;
  251. config.hpd_reg_cnt = ARRAY_SIZE(hpd_reg_names);
  252. config.pwr_reg_names = pwr_reg_names;
  253. config.pwr_reg_cnt = ARRAY_SIZE(pwr_reg_names);
  254. config.hpd_clk_names = hpd_clk_names;
  255. config.hpd_freq = hpd_clk_freq;
  256. config.hpd_clk_cnt = ARRAY_SIZE(hpd_clk_names);
  257. config.pwr_clk_names = pwr_clk_names;
  258. config.pwr_clk_cnt = ARRAY_SIZE(pwr_clk_names);
  259. } else if (of_device_is_compatible(of_node, "qcom,hdmi-tx-8960")) {
  260. static const char *hpd_clk_names[] = {"core_clk", "master_iface_clk", "slave_iface_clk"};
  261. static const char *hpd_reg_names[] = {"core-vdda", "hdmi-mux"};
  262. config.phy_init = hdmi_phy_8960_init;
  263. config.hpd_reg_names = hpd_reg_names;
  264. config.hpd_reg_cnt = ARRAY_SIZE(hpd_reg_names);
  265. config.hpd_clk_names = hpd_clk_names;
  266. config.hpd_clk_cnt = ARRAY_SIZE(hpd_clk_names);
  267. } else if (of_device_is_compatible(of_node, "qcom,hdmi-tx-8660")) {
  268. config.phy_init = hdmi_phy_8x60_init;
  269. } else {
  270. dev_err(dev, "unknown phy: %s\n", of_node->name);
  271. }
  272. config.mmio_name = "core_physical";
  273. config.ddc_clk_gpio = get_gpio(dev, of_node, "qcom,hdmi-tx-ddc-clk");
  274. config.ddc_data_gpio = get_gpio(dev, of_node, "qcom,hdmi-tx-ddc-data");
  275. config.hpd_gpio = get_gpio(dev, of_node, "qcom,hdmi-tx-hpd");
  276. config.mux_en_gpio = get_gpio(dev, of_node, "qcom,hdmi-tx-mux-en");
  277. config.mux_sel_gpio = get_gpio(dev, of_node, "qcom,hdmi-tx-mux-sel");
  278. config.mux_lpm_gpio = get_gpio(dev, of_node, "qcom,hdmi-tx-mux-lpm");
  279. #else
  280. static const char *hpd_clk_names[] = {
  281. "core_clk", "master_iface_clk", "slave_iface_clk",
  282. };
  283. if (cpu_is_apq8064()) {
  284. static const char *hpd_reg_names[] = {"8921_hdmi_mvs"};
  285. config.phy_init = hdmi_phy_8960_init;
  286. config.mmio_name = "hdmi_msm_hdmi_addr";
  287. config.hpd_reg_names = hpd_reg_names;
  288. config.hpd_reg_cnt = ARRAY_SIZE(hpd_reg_names);
  289. config.hpd_clk_names = hpd_clk_names;
  290. config.hpd_clk_cnt = ARRAY_SIZE(hpd_clk_names);
  291. config.ddc_clk_gpio = 70;
  292. config.ddc_data_gpio = 71;
  293. config.hpd_gpio = 72;
  294. config.mux_en_gpio = -1;
  295. config.mux_sel_gpio = -1;
  296. } else if (cpu_is_msm8960() || cpu_is_msm8960ab()) {
  297. static const char *hpd_reg_names[] = {"8921_hdmi_mvs"};
  298. config.phy_init = hdmi_phy_8960_init;
  299. config.mmio_name = "hdmi_msm_hdmi_addr";
  300. config.hpd_reg_names = hpd_reg_names;
  301. config.hpd_reg_cnt = ARRAY_SIZE(hpd_reg_names);
  302. config.hpd_clk_names = hpd_clk_names;
  303. config.hpd_clk_cnt = ARRAY_SIZE(hpd_clk_names);
  304. config.ddc_clk_gpio = 100;
  305. config.ddc_data_gpio = 101;
  306. config.hpd_gpio = 102;
  307. config.mux_en_gpio = -1;
  308. config.mux_sel_gpio = -1;
  309. } else if (cpu_is_msm8x60()) {
  310. static const char *hpd_reg_names[] = {
  311. "8901_hdmi_mvs", "8901_mpp0"
  312. };
  313. config.phy_init = hdmi_phy_8x60_init;
  314. config.mmio_name = "hdmi_msm_hdmi_addr";
  315. config.hpd_reg_names = hpd_reg_names;
  316. config.hpd_reg_cnt = ARRAY_SIZE(hpd_reg_names);
  317. config.hpd_clk_names = hpd_clk_names;
  318. config.hpd_clk_cnt = ARRAY_SIZE(hpd_clk_names);
  319. config.ddc_clk_gpio = 170;
  320. config.ddc_data_gpio = 171;
  321. config.hpd_gpio = 172;
  322. config.mux_en_gpio = -1;
  323. config.mux_sel_gpio = -1;
  324. }
  325. #endif
  326. dev->platform_data = &config;
  327. hdmi = hdmi_init(to_platform_device(dev));
  328. if (IS_ERR(hdmi))
  329. return PTR_ERR(hdmi);
  330. priv->hdmi = hdmi;
  331. return 0;
  332. }
  333. static void hdmi_unbind(struct device *dev, struct device *master,
  334. void *data)
  335. {
  336. struct drm_device *drm = dev_get_drvdata(master);
  337. struct msm_drm_private *priv = drm->dev_private;
  338. if (priv->hdmi) {
  339. hdmi_destroy(priv->hdmi);
  340. priv->hdmi = NULL;
  341. }
  342. }
  343. static const struct component_ops hdmi_ops = {
  344. .bind = hdmi_bind,
  345. .unbind = hdmi_unbind,
  346. };
  347. static int hdmi_dev_probe(struct platform_device *pdev)
  348. {
  349. return component_add(&pdev->dev, &hdmi_ops);
  350. }
  351. static int hdmi_dev_remove(struct platform_device *pdev)
  352. {
  353. component_del(&pdev->dev, &hdmi_ops);
  354. return 0;
  355. }
  356. static const struct of_device_id dt_match[] = {
  357. { .compatible = "qcom,hdmi-tx-8074" },
  358. { .compatible = "qcom,hdmi-tx-8960" },
  359. { .compatible = "qcom,hdmi-tx-8660" },
  360. {}
  361. };
  362. static struct platform_driver hdmi_driver = {
  363. .probe = hdmi_dev_probe,
  364. .remove = hdmi_dev_remove,
  365. .driver = {
  366. .name = "hdmi_msm",
  367. .of_match_table = dt_match,
  368. },
  369. };
  370. void __init hdmi_register(void)
  371. {
  372. platform_driver_register(&hdmi_driver);
  373. }
  374. void __exit hdmi_unregister(void)
  375. {
  376. platform_driver_unregister(&hdmi_driver);
  377. }