hdmi.c 13 KB

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