hdmi_connector.c 12 KB

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