hdmi_connector.c 12 KB

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