hdmi_connector.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. if (config->hpd_freq && config->hpd_freq[i]) {
  115. ret = clk_set_rate(hdmi->hpd_clks[i],
  116. config->hpd_freq[i]);
  117. if (ret)
  118. dev_warn(dev->dev, "failed to set clk %s (%d)\n",
  119. config->hpd_clk_names[i], ret);
  120. }
  121. ret = clk_prepare_enable(hdmi->hpd_clks[i]);
  122. if (ret) {
  123. dev_err(dev->dev, "failed to enable hpd clk: %s (%d)\n",
  124. config->hpd_clk_names[i], ret);
  125. goto fail;
  126. }
  127. }
  128. for (i = 0; i < config->hpd_reg_cnt; i++) {
  129. ret = regulator_enable(hdmi->hpd_regs[i]);
  130. if (ret) {
  131. dev_err(dev->dev, "failed to enable hpd regulator: %s (%d)\n",
  132. config->hpd_reg_names[i], ret);
  133. goto fail;
  134. }
  135. }
  136. hdmi_set_mode(hdmi, false);
  137. phy->funcs->reset(phy);
  138. hdmi_set_mode(hdmi, true);
  139. hdmi_write(hdmi, REG_HDMI_USEC_REFTIMER, 0x0001001b);
  140. /* enable HPD events: */
  141. hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL,
  142. HDMI_HPD_INT_CTRL_INT_CONNECT |
  143. HDMI_HPD_INT_CTRL_INT_EN);
  144. /* set timeout to 4.1ms (max) for hardware debounce */
  145. hpd_ctrl = hdmi_read(hdmi, REG_HDMI_HPD_CTRL);
  146. hpd_ctrl |= HDMI_HPD_CTRL_TIMEOUT(0x1fff);
  147. /* Toggle HPD circuit to trigger HPD sense */
  148. hdmi_write(hdmi, REG_HDMI_HPD_CTRL,
  149. ~HDMI_HPD_CTRL_ENABLE & hpd_ctrl);
  150. hdmi_write(hdmi, REG_HDMI_HPD_CTRL,
  151. HDMI_HPD_CTRL_ENABLE | hpd_ctrl);
  152. return 0;
  153. fail:
  154. return ret;
  155. }
  156. static int hdp_disable(struct hdmi_connector *hdmi_connector)
  157. {
  158. struct hdmi *hdmi = hdmi_connector->hdmi;
  159. const struct hdmi_platform_config *config = hdmi->config;
  160. struct drm_device *dev = hdmi_connector->base.dev;
  161. int i, ret = 0;
  162. /* Disable HPD interrupt */
  163. hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL, 0);
  164. hdmi_set_mode(hdmi, false);
  165. for (i = 0; i < config->hpd_reg_cnt; i++) {
  166. ret = regulator_disable(hdmi->hpd_regs[i]);
  167. if (ret) {
  168. dev_err(dev->dev, "failed to disable hpd regulator: %s (%d)\n",
  169. config->hpd_reg_names[i], ret);
  170. goto fail;
  171. }
  172. }
  173. for (i = 0; i < config->hpd_clk_cnt; i++)
  174. clk_disable_unprepare(hdmi->hpd_clks[i]);
  175. ret = gpio_config(hdmi, false);
  176. if (ret) {
  177. dev_err(dev->dev, "failed to unconfigure GPIOs: %d\n", ret);
  178. goto fail;
  179. }
  180. return 0;
  181. fail:
  182. return ret;
  183. }
  184. static void
  185. hotplug_work(struct work_struct *work)
  186. {
  187. struct hdmi_connector *hdmi_connector =
  188. container_of(work, struct hdmi_connector, hpd_work);
  189. struct drm_connector *connector = &hdmi_connector->base;
  190. drm_helper_hpd_irq_event(connector->dev);
  191. }
  192. void hdmi_connector_irq(struct drm_connector *connector)
  193. {
  194. struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
  195. struct msm_drm_private *priv = connector->dev->dev_private;
  196. struct hdmi *hdmi = hdmi_connector->hdmi;
  197. uint32_t hpd_int_status, hpd_int_ctrl;
  198. /* Process HPD: */
  199. hpd_int_status = hdmi_read(hdmi, REG_HDMI_HPD_INT_STATUS);
  200. hpd_int_ctrl = hdmi_read(hdmi, REG_HDMI_HPD_INT_CTRL);
  201. if ((hpd_int_ctrl & HDMI_HPD_INT_CTRL_INT_EN) &&
  202. (hpd_int_status & HDMI_HPD_INT_STATUS_INT)) {
  203. bool detected = !!(hpd_int_status & HDMI_HPD_INT_STATUS_CABLE_DETECTED);
  204. DBG("status=%04x, ctrl=%04x", hpd_int_status, hpd_int_ctrl);
  205. /* ack the irq: */
  206. hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL,
  207. hpd_int_ctrl | HDMI_HPD_INT_CTRL_INT_ACK);
  208. /* detect disconnect if we are connected or visa versa: */
  209. hpd_int_ctrl = HDMI_HPD_INT_CTRL_INT_EN;
  210. if (!detected)
  211. hpd_int_ctrl |= HDMI_HPD_INT_CTRL_INT_CONNECT;
  212. hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL, hpd_int_ctrl);
  213. queue_work(priv->wq, &hdmi_connector->hpd_work);
  214. }
  215. }
  216. static enum drm_connector_status detect_reg(struct hdmi *hdmi)
  217. {
  218. uint32_t hpd_int_status = hdmi_read(hdmi, REG_HDMI_HPD_INT_STATUS);
  219. return (hpd_int_status & HDMI_HPD_INT_STATUS_CABLE_DETECTED) ?
  220. connector_status_connected : connector_status_disconnected;
  221. }
  222. static enum drm_connector_status detect_gpio(struct hdmi *hdmi)
  223. {
  224. const struct hdmi_platform_config *config = hdmi->config;
  225. return gpio_get_value(config->hpd_gpio) ?
  226. connector_status_connected :
  227. connector_status_disconnected;
  228. }
  229. static enum drm_connector_status hdmi_connector_detect(
  230. struct drm_connector *connector, bool force)
  231. {
  232. struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
  233. struct hdmi *hdmi = hdmi_connector->hdmi;
  234. enum drm_connector_status stat_gpio, stat_reg;
  235. int retry = 20;
  236. do {
  237. stat_gpio = detect_gpio(hdmi);
  238. stat_reg = detect_reg(hdmi);
  239. if (stat_gpio == stat_reg)
  240. break;
  241. mdelay(10);
  242. } while (--retry);
  243. /* the status we get from reading gpio seems to be more reliable,
  244. * so trust that one the most if we didn't manage to get hdmi and
  245. * gpio status to agree:
  246. */
  247. if (stat_gpio != stat_reg) {
  248. DBG("HDMI_HPD_INT_STATUS tells us: %d", stat_reg);
  249. DBG("hpd gpio tells us: %d", stat_gpio);
  250. }
  251. return stat_gpio;
  252. }
  253. static void hdmi_connector_destroy(struct drm_connector *connector)
  254. {
  255. struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
  256. hdp_disable(hdmi_connector);
  257. drm_sysfs_connector_remove(connector);
  258. drm_connector_cleanup(connector);
  259. hdmi_unreference(hdmi_connector->hdmi);
  260. kfree(hdmi_connector);
  261. }
  262. static int hdmi_connector_get_modes(struct drm_connector *connector)
  263. {
  264. struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
  265. struct hdmi *hdmi = hdmi_connector->hdmi;
  266. struct edid *edid;
  267. uint32_t hdmi_ctrl;
  268. int ret = 0;
  269. hdmi_ctrl = hdmi_read(hdmi, REG_HDMI_CTRL);
  270. hdmi_write(hdmi, REG_HDMI_CTRL, hdmi_ctrl | HDMI_CTRL_ENABLE);
  271. edid = drm_get_edid(connector, hdmi->i2c);
  272. hdmi_write(hdmi, REG_HDMI_CTRL, hdmi_ctrl);
  273. drm_mode_connector_update_edid_property(connector, edid);
  274. if (edid) {
  275. ret = drm_add_edid_modes(connector, edid);
  276. kfree(edid);
  277. }
  278. return ret;
  279. }
  280. static int hdmi_connector_mode_valid(struct drm_connector *connector,
  281. struct drm_display_mode *mode)
  282. {
  283. struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
  284. struct hdmi *hdmi = hdmi_connector->hdmi;
  285. const struct hdmi_platform_config *config = hdmi->config;
  286. struct msm_drm_private *priv = connector->dev->dev_private;
  287. struct msm_kms *kms = priv->kms;
  288. long actual, requested;
  289. requested = 1000 * mode->clock;
  290. actual = kms->funcs->round_pixclk(kms,
  291. requested, hdmi_connector->hdmi->encoder);
  292. /* for mdp5/apq8074, we manage our own pixel clk (as opposed to
  293. * mdp4/dtv stuff where pixel clk is assigned to mdp/encoder
  294. * instead):
  295. */
  296. if (config->pwr_clk_cnt > 0)
  297. actual = clk_round_rate(hdmi->pwr_clks[0], actual);
  298. DBG("requested=%ld, actual=%ld", requested, actual);
  299. if (actual != requested)
  300. return MODE_CLOCK_RANGE;
  301. return 0;
  302. }
  303. static struct drm_encoder *
  304. hdmi_connector_best_encoder(struct drm_connector *connector)
  305. {
  306. struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
  307. return hdmi_connector->hdmi->encoder;
  308. }
  309. static const struct drm_connector_funcs hdmi_connector_funcs = {
  310. .dpms = drm_helper_connector_dpms,
  311. .detect = hdmi_connector_detect,
  312. .fill_modes = drm_helper_probe_single_connector_modes,
  313. .destroy = hdmi_connector_destroy,
  314. };
  315. static const struct drm_connector_helper_funcs hdmi_connector_helper_funcs = {
  316. .get_modes = hdmi_connector_get_modes,
  317. .mode_valid = hdmi_connector_mode_valid,
  318. .best_encoder = hdmi_connector_best_encoder,
  319. };
  320. /* initialize connector */
  321. struct drm_connector *hdmi_connector_init(struct hdmi *hdmi)
  322. {
  323. struct drm_connector *connector = NULL;
  324. struct hdmi_connector *hdmi_connector;
  325. int ret;
  326. hdmi_connector = kzalloc(sizeof(*hdmi_connector), GFP_KERNEL);
  327. if (!hdmi_connector) {
  328. ret = -ENOMEM;
  329. goto fail;
  330. }
  331. hdmi_connector->hdmi = hdmi_reference(hdmi);
  332. INIT_WORK(&hdmi_connector->hpd_work, hotplug_work);
  333. connector = &hdmi_connector->base;
  334. drm_connector_init(hdmi->dev, connector, &hdmi_connector_funcs,
  335. DRM_MODE_CONNECTOR_HDMIA);
  336. drm_connector_helper_add(connector, &hdmi_connector_helper_funcs);
  337. connector->polled = DRM_CONNECTOR_POLL_CONNECT |
  338. DRM_CONNECTOR_POLL_DISCONNECT;
  339. connector->interlace_allowed = 1;
  340. connector->doublescan_allowed = 0;
  341. drm_sysfs_connector_add(connector);
  342. ret = hpd_enable(hdmi_connector);
  343. if (ret) {
  344. dev_err(hdmi->dev->dev, "failed to enable HPD: %d\n", ret);
  345. goto fail;
  346. }
  347. drm_mode_connector_attach_encoder(connector, hdmi->encoder);
  348. return connector;
  349. fail:
  350. if (connector)
  351. hdmi_connector_destroy(connector);
  352. return ERR_PTR(ret);
  353. }