hdmi_connector.c 12 KB

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