hdmi_connector.c 14 KB

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