hdmi_connector.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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/gpio.h>
  18. #include "msm_kms.h"
  19. #include "hdmi.h"
  20. struct hdmi_connector {
  21. struct drm_connector base;
  22. struct hdmi *hdmi;
  23. struct work_struct hpd_work;
  24. };
  25. #define to_hdmi_connector(x) container_of(x, struct hdmi_connector, base)
  26. static int gpio_config(struct hdmi *hdmi, bool on)
  27. {
  28. struct drm_device *dev = hdmi->dev;
  29. const struct hdmi_platform_config *config = hdmi->config;
  30. int ret;
  31. if (on) {
  32. ret = gpio_request(config->ddc_clk_gpio, "HDMI_DDC_CLK");
  33. if (ret) {
  34. dev_err(dev->dev, "'%s'(%d) gpio_request failed: %d\n",
  35. "HDMI_DDC_CLK", config->ddc_clk_gpio, ret);
  36. goto error1;
  37. }
  38. gpio_set_value_cansleep(config->ddc_clk_gpio, 1);
  39. ret = gpio_request(config->ddc_data_gpio, "HDMI_DDC_DATA");
  40. if (ret) {
  41. dev_err(dev->dev, "'%s'(%d) gpio_request failed: %d\n",
  42. "HDMI_DDC_DATA", config->ddc_data_gpio, ret);
  43. goto error2;
  44. }
  45. gpio_set_value_cansleep(config->ddc_data_gpio, 1);
  46. ret = gpio_request(config->hpd_gpio, "HDMI_HPD");
  47. if (ret) {
  48. dev_err(dev->dev, "'%s'(%d) gpio_request failed: %d\n",
  49. "HDMI_HPD", config->hpd_gpio, ret);
  50. goto error3;
  51. }
  52. gpio_direction_input(config->hpd_gpio);
  53. gpio_set_value_cansleep(config->hpd_gpio, 1);
  54. if (config->mux_en_gpio != -1) {
  55. ret = gpio_request(config->mux_en_gpio, "HDMI_MUX_EN");
  56. if (ret) {
  57. dev_err(dev->dev, "'%s'(%d) gpio_request failed: %d\n",
  58. "HDMI_MUX_SEL", config->mux_en_gpio, ret);
  59. goto error4;
  60. }
  61. gpio_set_value_cansleep(config->mux_en_gpio, 1);
  62. }
  63. if (config->mux_sel_gpio != -1) {
  64. ret = gpio_request(config->mux_sel_gpio, "HDMI_MUX_SEL");
  65. if (ret) {
  66. dev_err(dev->dev, "'%s'(%d) gpio_request failed: %d\n",
  67. "HDMI_MUX_SEL", config->mux_sel_gpio, ret);
  68. goto error5;
  69. }
  70. gpio_set_value_cansleep(config->mux_sel_gpio, 0);
  71. }
  72. DBG("gpio on");
  73. } else {
  74. gpio_free(config->ddc_clk_gpio);
  75. gpio_free(config->ddc_data_gpio);
  76. gpio_free(config->hpd_gpio);
  77. if (config->mux_en_gpio != -1) {
  78. gpio_set_value_cansleep(config->mux_en_gpio, 0);
  79. gpio_free(config->mux_en_gpio);
  80. }
  81. if (config->mux_sel_gpio != -1) {
  82. gpio_set_value_cansleep(config->mux_sel_gpio, 1);
  83. gpio_free(config->mux_sel_gpio);
  84. }
  85. DBG("gpio off");
  86. }
  87. return 0;
  88. error5:
  89. if (config->mux_en_gpio != -1)
  90. gpio_free(config->mux_en_gpio);
  91. error4:
  92. gpio_free(config->hpd_gpio);
  93. error3:
  94. gpio_free(config->ddc_data_gpio);
  95. error2:
  96. gpio_free(config->ddc_clk_gpio);
  97. error1:
  98. return ret;
  99. }
  100. static int hpd_enable(struct hdmi_connector *hdmi_connector)
  101. {
  102. struct hdmi *hdmi = hdmi_connector->hdmi;
  103. const struct hdmi_platform_config *config = hdmi->config;
  104. struct drm_device *dev = hdmi_connector->base.dev;
  105. struct hdmi_phy *phy = hdmi->phy;
  106. uint32_t hpd_ctrl;
  107. int i, ret;
  108. ret = gpio_config(hdmi, true);
  109. if (ret) {
  110. dev_err(dev->dev, "failed to configure GPIOs: %d\n", ret);
  111. goto fail;
  112. }
  113. for (i = 0; i < config->hpd_clk_cnt; i++) {
  114. ret = clk_prepare_enable(hdmi->hpd_clks[i]);
  115. if (ret) {
  116. dev_err(dev->dev, "failed to enable hpd clk: %s (%d)\n",
  117. config->hpd_clk_names[i], ret);
  118. goto fail;
  119. }
  120. }
  121. for (i = 0; i < config->hpd_reg_cnt; i++) {
  122. ret = regulator_enable(hdmi->hpd_regs[i]);
  123. if (ret) {
  124. dev_err(dev->dev, "failed to enable hpd regulator: %s (%d)\n",
  125. config->hpd_reg_names[i], ret);
  126. goto fail;
  127. }
  128. }
  129. hdmi_set_mode(hdmi, false);
  130. phy->funcs->reset(phy);
  131. hdmi_set_mode(hdmi, true);
  132. hdmi_write(hdmi, REG_HDMI_USEC_REFTIMER, 0x0001001b);
  133. /* enable HPD events: */
  134. hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL,
  135. HDMI_HPD_INT_CTRL_INT_CONNECT |
  136. HDMI_HPD_INT_CTRL_INT_EN);
  137. /* set timeout to 4.1ms (max) for hardware debounce */
  138. hpd_ctrl = hdmi_read(hdmi, REG_HDMI_HPD_CTRL);
  139. hpd_ctrl |= HDMI_HPD_CTRL_TIMEOUT(0x1fff);
  140. /* Toggle HPD circuit to trigger HPD sense */
  141. hdmi_write(hdmi, REG_HDMI_HPD_CTRL,
  142. ~HDMI_HPD_CTRL_ENABLE & hpd_ctrl);
  143. hdmi_write(hdmi, REG_HDMI_HPD_CTRL,
  144. HDMI_HPD_CTRL_ENABLE | hpd_ctrl);
  145. return 0;
  146. fail:
  147. return ret;
  148. }
  149. static int hdp_disable(struct hdmi_connector *hdmi_connector)
  150. {
  151. struct hdmi *hdmi = hdmi_connector->hdmi;
  152. const struct hdmi_platform_config *config = hdmi->config;
  153. struct drm_device *dev = hdmi_connector->base.dev;
  154. int i, ret = 0;
  155. /* Disable HPD interrupt */
  156. hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL, 0);
  157. hdmi_set_mode(hdmi, false);
  158. for (i = 0; i < config->hpd_reg_cnt; i++) {
  159. ret = regulator_disable(hdmi->hpd_regs[i]);
  160. if (ret) {
  161. dev_err(dev->dev, "failed to disable hpd regulator: %s (%d)\n",
  162. config->hpd_reg_names[i], ret);
  163. goto fail;
  164. }
  165. }
  166. for (i = 0; i < config->hpd_clk_cnt; i++)
  167. clk_disable_unprepare(hdmi->hpd_clks[i]);
  168. ret = gpio_config(hdmi, false);
  169. if (ret) {
  170. dev_err(dev->dev, "failed to unconfigure GPIOs: %d\n", ret);
  171. goto fail;
  172. }
  173. return 0;
  174. fail:
  175. return ret;
  176. }
  177. static void
  178. hotplug_work(struct work_struct *work)
  179. {
  180. struct hdmi_connector *hdmi_connector =
  181. container_of(work, struct hdmi_connector, hpd_work);
  182. struct drm_connector *connector = &hdmi_connector->base;
  183. drm_helper_hpd_irq_event(connector->dev);
  184. }
  185. void hdmi_connector_irq(struct drm_connector *connector)
  186. {
  187. struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
  188. struct msm_drm_private *priv = connector->dev->dev_private;
  189. struct hdmi *hdmi = hdmi_connector->hdmi;
  190. uint32_t hpd_int_status, hpd_int_ctrl;
  191. /* Process HPD: */
  192. hpd_int_status = hdmi_read(hdmi, REG_HDMI_HPD_INT_STATUS);
  193. hpd_int_ctrl = hdmi_read(hdmi, REG_HDMI_HPD_INT_CTRL);
  194. if ((hpd_int_ctrl & HDMI_HPD_INT_CTRL_INT_EN) &&
  195. (hpd_int_status & HDMI_HPD_INT_STATUS_INT)) {
  196. bool detected = !!(hpd_int_status & HDMI_HPD_INT_STATUS_CABLE_DETECTED);
  197. DBG("status=%04x, ctrl=%04x", hpd_int_status, hpd_int_ctrl);
  198. /* ack the irq: */
  199. hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL,
  200. hpd_int_ctrl | HDMI_HPD_INT_CTRL_INT_ACK);
  201. /* detect disconnect if we are connected or visa versa: */
  202. hpd_int_ctrl = HDMI_HPD_INT_CTRL_INT_EN;
  203. if (!detected)
  204. hpd_int_ctrl |= HDMI_HPD_INT_CTRL_INT_CONNECT;
  205. hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL, hpd_int_ctrl);
  206. queue_work(priv->wq, &hdmi_connector->hpd_work);
  207. }
  208. }
  209. static enum drm_connector_status hdmi_connector_detect(
  210. struct drm_connector *connector, bool force)
  211. {
  212. struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
  213. struct hdmi *hdmi = hdmi_connector->hdmi;
  214. const struct hdmi_platform_config *config = hdmi->config;
  215. uint32_t hpd_int_status;
  216. int retry = 20;
  217. hpd_int_status = hdmi_read(hdmi, REG_HDMI_HPD_INT_STATUS);
  218. /* sense seems to in some cases be momentarily de-asserted, don't
  219. * let that trick us into thinking the monitor is gone:
  220. */
  221. while (retry-- && !(hpd_int_status & HDMI_HPD_INT_STATUS_CABLE_DETECTED)) {
  222. /* hdmi debounce logic seems to get stuck sometimes,
  223. * read directly the gpio to get a second opinion:
  224. */
  225. if (gpio_get_value(config->hpd_gpio)) {
  226. DBG("gpio tells us we are connected!");
  227. hpd_int_status |= HDMI_HPD_INT_STATUS_CABLE_DETECTED;
  228. break;
  229. }
  230. mdelay(10);
  231. hpd_int_status = hdmi_read(hdmi, REG_HDMI_HPD_INT_STATUS);
  232. DBG("status=%08x", hpd_int_status);
  233. }
  234. return (hpd_int_status & HDMI_HPD_INT_STATUS_CABLE_DETECTED) ?
  235. connector_status_connected : connector_status_disconnected;
  236. }
  237. static void hdmi_connector_destroy(struct drm_connector *connector)
  238. {
  239. struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
  240. hdp_disable(hdmi_connector);
  241. drm_sysfs_connector_remove(connector);
  242. drm_connector_cleanup(connector);
  243. hdmi_unreference(hdmi_connector->hdmi);
  244. kfree(hdmi_connector);
  245. }
  246. static int hdmi_connector_get_modes(struct drm_connector *connector)
  247. {
  248. struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
  249. struct hdmi *hdmi = hdmi_connector->hdmi;
  250. struct edid *edid;
  251. uint32_t hdmi_ctrl;
  252. int ret = 0;
  253. hdmi_ctrl = hdmi_read(hdmi, REG_HDMI_CTRL);
  254. hdmi_write(hdmi, REG_HDMI_CTRL, hdmi_ctrl | HDMI_CTRL_ENABLE);
  255. edid = drm_get_edid(connector, hdmi->i2c);
  256. hdmi_write(hdmi, REG_HDMI_CTRL, hdmi_ctrl);
  257. drm_mode_connector_update_edid_property(connector, edid);
  258. if (edid) {
  259. ret = drm_add_edid_modes(connector, edid);
  260. kfree(edid);
  261. }
  262. return ret;
  263. }
  264. static int hdmi_connector_mode_valid(struct drm_connector *connector,
  265. struct drm_display_mode *mode)
  266. {
  267. struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
  268. struct hdmi *hdmi = hdmi_connector->hdmi;
  269. const struct hdmi_platform_config *config = hdmi->config;
  270. struct msm_drm_private *priv = connector->dev->dev_private;
  271. struct msm_kms *kms = priv->kms;
  272. long actual, requested;
  273. requested = 1000 * mode->clock;
  274. actual = kms->funcs->round_pixclk(kms,
  275. requested, hdmi_connector->hdmi->encoder);
  276. /* for mdp5/apq8074, we manage our own pixel clk (as opposed to
  277. * mdp4/dtv stuff where pixel clk is assigned to mdp/encoder
  278. * instead):
  279. */
  280. if (config->pwr_clk_cnt > 0)
  281. actual = clk_round_rate(hdmi->pwr_clks[0], actual);
  282. DBG("requested=%ld, actual=%ld", requested, actual);
  283. if (actual != requested)
  284. return MODE_CLOCK_RANGE;
  285. return 0;
  286. }
  287. static struct drm_encoder *
  288. hdmi_connector_best_encoder(struct drm_connector *connector)
  289. {
  290. struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
  291. return hdmi_connector->hdmi->encoder;
  292. }
  293. static const struct drm_connector_funcs hdmi_connector_funcs = {
  294. .dpms = drm_helper_connector_dpms,
  295. .detect = hdmi_connector_detect,
  296. .fill_modes = drm_helper_probe_single_connector_modes,
  297. .destroy = hdmi_connector_destroy,
  298. };
  299. static const struct drm_connector_helper_funcs hdmi_connector_helper_funcs = {
  300. .get_modes = hdmi_connector_get_modes,
  301. .mode_valid = hdmi_connector_mode_valid,
  302. .best_encoder = hdmi_connector_best_encoder,
  303. };
  304. /* initialize connector */
  305. struct drm_connector *hdmi_connector_init(struct hdmi *hdmi)
  306. {
  307. struct drm_connector *connector = NULL;
  308. struct hdmi_connector *hdmi_connector;
  309. int ret;
  310. hdmi_connector = kzalloc(sizeof(*hdmi_connector), GFP_KERNEL);
  311. if (!hdmi_connector) {
  312. ret = -ENOMEM;
  313. goto fail;
  314. }
  315. hdmi_connector->hdmi = hdmi_reference(hdmi);
  316. INIT_WORK(&hdmi_connector->hpd_work, hotplug_work);
  317. connector = &hdmi_connector->base;
  318. drm_connector_init(hdmi->dev, connector, &hdmi_connector_funcs,
  319. DRM_MODE_CONNECTOR_HDMIA);
  320. drm_connector_helper_add(connector, &hdmi_connector_helper_funcs);
  321. connector->polled = DRM_CONNECTOR_POLL_HPD;
  322. connector->interlace_allowed = 1;
  323. connector->doublescan_allowed = 0;
  324. drm_sysfs_connector_add(connector);
  325. ret = hpd_enable(hdmi_connector);
  326. if (ret) {
  327. dev_err(hdmi->dev->dev, "failed to enable HPD: %d\n", ret);
  328. goto fail;
  329. }
  330. drm_mode_connector_attach_encoder(connector, hdmi->encoder);
  331. return connector;
  332. fail:
  333. if (connector)
  334. hdmi_connector_destroy(connector);
  335. return ERR_PTR(ret);
  336. }