hdmi_connector.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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 msm_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, i;
  74. if (on) {
  75. for (i = 0; i < HDMI_MAX_NUM_GPIO; i++) {
  76. struct hdmi_gpio_data gpio = config->gpios[i];
  77. if (gpio.num != -1) {
  78. ret = gpio_request(gpio.num, gpio.label);
  79. if (ret) {
  80. dev_err(dev,
  81. "'%s'(%d) gpio_request failed: %d\n",
  82. gpio.label, gpio.num, ret);
  83. goto err;
  84. }
  85. if (gpio.output) {
  86. gpio_direction_output(gpio.num,
  87. gpio.value);
  88. } else {
  89. gpio_direction_input(gpio.num);
  90. gpio_set_value_cansleep(gpio.num,
  91. gpio.value);
  92. }
  93. }
  94. }
  95. DBG("gpio on");
  96. } else {
  97. for (i = 0; i < HDMI_MAX_NUM_GPIO; i++) {
  98. struct hdmi_gpio_data gpio = config->gpios[i];
  99. if (gpio.num == -1)
  100. continue;
  101. if (gpio.output) {
  102. int value = gpio.value ? 0 : 1;
  103. gpio_set_value_cansleep(gpio.num, value);
  104. }
  105. gpio_free(gpio.num);
  106. };
  107. DBG("gpio off");
  108. }
  109. return 0;
  110. err:
  111. while (i--) {
  112. if (config->gpios[i].num != -1)
  113. gpio_free(config->gpios[i].num);
  114. }
  115. return ret;
  116. }
  117. static int hpd_enable(struct hdmi_connector *hdmi_connector)
  118. {
  119. struct hdmi *hdmi = hdmi_connector->hdmi;
  120. const struct hdmi_platform_config *config = hdmi->config;
  121. struct device *dev = &hdmi->pdev->dev;
  122. uint32_t hpd_ctrl;
  123. int i, ret;
  124. unsigned long flags;
  125. for (i = 0; i < config->hpd_reg_cnt; i++) {
  126. ret = regulator_enable(hdmi->hpd_regs[i]);
  127. if (ret) {
  128. dev_err(dev, "failed to enable hpd regulator: %s (%d)\n",
  129. config->hpd_reg_names[i], ret);
  130. goto fail;
  131. }
  132. }
  133. ret = pinctrl_pm_select_default_state(dev);
  134. if (ret) {
  135. dev_err(dev, "pinctrl state chg failed: %d\n", ret);
  136. goto fail;
  137. }
  138. ret = gpio_config(hdmi, true);
  139. if (ret) {
  140. dev_err(dev, "failed to configure GPIOs: %d\n", ret);
  141. goto fail;
  142. }
  143. for (i = 0; i < config->hpd_clk_cnt; i++) {
  144. if (config->hpd_freq && config->hpd_freq[i]) {
  145. ret = clk_set_rate(hdmi->hpd_clks[i],
  146. config->hpd_freq[i]);
  147. if (ret)
  148. dev_warn(dev, "failed to set clk %s (%d)\n",
  149. config->hpd_clk_names[i], ret);
  150. }
  151. ret = clk_prepare_enable(hdmi->hpd_clks[i]);
  152. if (ret) {
  153. dev_err(dev, "failed to enable hpd clk: %s (%d)\n",
  154. config->hpd_clk_names[i], ret);
  155. goto fail;
  156. }
  157. }
  158. msm_hdmi_set_mode(hdmi, false);
  159. msm_hdmi_phy_reset(hdmi);
  160. msm_hdmi_set_mode(hdmi, true);
  161. hdmi_write(hdmi, REG_HDMI_USEC_REFTIMER, 0x0001001b);
  162. /* enable HPD events: */
  163. hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL,
  164. HDMI_HPD_INT_CTRL_INT_CONNECT |
  165. HDMI_HPD_INT_CTRL_INT_EN);
  166. /* set timeout to 4.1ms (max) for hardware debounce */
  167. spin_lock_irqsave(&hdmi->reg_lock, flags);
  168. hpd_ctrl = hdmi_read(hdmi, REG_HDMI_HPD_CTRL);
  169. hpd_ctrl |= HDMI_HPD_CTRL_TIMEOUT(0x1fff);
  170. /* Toggle HPD circuit to trigger HPD sense */
  171. hdmi_write(hdmi, REG_HDMI_HPD_CTRL,
  172. ~HDMI_HPD_CTRL_ENABLE & hpd_ctrl);
  173. hdmi_write(hdmi, REG_HDMI_HPD_CTRL,
  174. HDMI_HPD_CTRL_ENABLE | hpd_ctrl);
  175. spin_unlock_irqrestore(&hdmi->reg_lock, flags);
  176. return 0;
  177. fail:
  178. return ret;
  179. }
  180. static void hdp_disable(struct hdmi_connector *hdmi_connector)
  181. {
  182. struct hdmi *hdmi = hdmi_connector->hdmi;
  183. const struct hdmi_platform_config *config = hdmi->config;
  184. struct device *dev = &hdmi->pdev->dev;
  185. int i, ret = 0;
  186. /* Disable HPD interrupt */
  187. hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL, 0);
  188. msm_hdmi_set_mode(hdmi, false);
  189. for (i = 0; i < config->hpd_clk_cnt; i++)
  190. clk_disable_unprepare(hdmi->hpd_clks[i]);
  191. ret = gpio_config(hdmi, false);
  192. if (ret)
  193. dev_warn(dev, "failed to unconfigure GPIOs: %d\n", ret);
  194. ret = pinctrl_pm_select_sleep_state(dev);
  195. if (ret)
  196. dev_warn(dev, "pinctrl state chg failed: %d\n", ret);
  197. for (i = 0; i < config->hpd_reg_cnt; i++) {
  198. ret = regulator_disable(hdmi->hpd_regs[i]);
  199. if (ret)
  200. dev_warn(dev, "failed to disable hpd regulator: %s (%d)\n",
  201. config->hpd_reg_names[i], ret);
  202. }
  203. }
  204. static void
  205. msm_hdmi_hotplug_work(struct work_struct *work)
  206. {
  207. struct hdmi_connector *hdmi_connector =
  208. container_of(work, struct hdmi_connector, hpd_work);
  209. struct drm_connector *connector = &hdmi_connector->base;
  210. drm_helper_hpd_irq_event(connector->dev);
  211. }
  212. void msm_hdmi_connector_irq(struct drm_connector *connector)
  213. {
  214. struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
  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. /* ack & disable (temporarily) HPD events: */
  224. hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL,
  225. HDMI_HPD_INT_CTRL_INT_ACK);
  226. DBG("status=%04x, ctrl=%04x", hpd_int_status, hpd_int_ctrl);
  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(hdmi->workq, &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. #define HPD_GPIO_INDEX 2
  242. static enum drm_connector_status detect_gpio(struct hdmi *hdmi)
  243. {
  244. const struct hdmi_platform_config *config = hdmi->config;
  245. struct hdmi_gpio_data hpd_gpio = config->gpios[HPD_GPIO_INDEX];
  246. return gpio_get_value(hpd_gpio.num) ?
  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. const struct hdmi_platform_config *config = hdmi->config;
  256. struct hdmi_gpio_data hpd_gpio = config->gpios[HPD_GPIO_INDEX];
  257. enum drm_connector_status stat_gpio, stat_reg;
  258. int retry = 20;
  259. /*
  260. * some platforms may not have hpd gpio. Rely only on the status
  261. * provided by REG_HDMI_HPD_INT_STATUS in this case.
  262. */
  263. if (hpd_gpio.num == -1)
  264. return detect_reg(hdmi);
  265. do {
  266. stat_gpio = detect_gpio(hdmi);
  267. stat_reg = detect_reg(hdmi);
  268. if (stat_gpio == stat_reg)
  269. break;
  270. mdelay(10);
  271. } while (--retry);
  272. /* the status we get from reading gpio seems to be more reliable,
  273. * so trust that one the most if we didn't manage to get hdmi and
  274. * gpio status to agree:
  275. */
  276. if (stat_gpio != stat_reg) {
  277. DBG("HDMI_HPD_INT_STATUS tells us: %d", stat_reg);
  278. DBG("hpd gpio tells us: %d", stat_gpio);
  279. }
  280. return stat_gpio;
  281. }
  282. static void hdmi_connector_destroy(struct drm_connector *connector)
  283. {
  284. struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
  285. hdp_disable(hdmi_connector);
  286. drm_connector_cleanup(connector);
  287. kfree(hdmi_connector);
  288. }
  289. static int msm_hdmi_connector_get_modes(struct drm_connector *connector)
  290. {
  291. struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
  292. struct hdmi *hdmi = hdmi_connector->hdmi;
  293. struct edid *edid;
  294. uint32_t hdmi_ctrl;
  295. int ret = 0;
  296. hdmi_ctrl = hdmi_read(hdmi, REG_HDMI_CTRL);
  297. hdmi_write(hdmi, REG_HDMI_CTRL, hdmi_ctrl | HDMI_CTRL_ENABLE);
  298. edid = drm_get_edid(connector, hdmi->i2c);
  299. hdmi_write(hdmi, REG_HDMI_CTRL, hdmi_ctrl);
  300. hdmi->hdmi_mode = drm_detect_hdmi_monitor(edid);
  301. drm_mode_connector_update_edid_property(connector, edid);
  302. if (edid) {
  303. ret = drm_add_edid_modes(connector, edid);
  304. kfree(edid);
  305. }
  306. return ret;
  307. }
  308. static int msm_hdmi_connector_mode_valid(struct drm_connector *connector,
  309. struct drm_display_mode *mode)
  310. {
  311. struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
  312. struct hdmi *hdmi = hdmi_connector->hdmi;
  313. const struct hdmi_platform_config *config = hdmi->config;
  314. struct msm_drm_private *priv = connector->dev->dev_private;
  315. struct msm_kms *kms = priv->kms;
  316. long actual, requested;
  317. requested = 1000 * mode->clock;
  318. actual = kms->funcs->round_pixclk(kms,
  319. requested, hdmi_connector->hdmi->encoder);
  320. /* for mdp5/apq8074, we manage our own pixel clk (as opposed to
  321. * mdp4/dtv stuff where pixel clk is assigned to mdp/encoder
  322. * instead):
  323. */
  324. if (config->pwr_clk_cnt > 0)
  325. actual = clk_round_rate(hdmi->pwr_clks[0], actual);
  326. DBG("requested=%ld, actual=%ld", requested, actual);
  327. if (actual != requested)
  328. return MODE_CLOCK_RANGE;
  329. return 0;
  330. }
  331. static const struct drm_connector_funcs hdmi_connector_funcs = {
  332. .dpms = drm_atomic_helper_connector_dpms,
  333. .detect = hdmi_connector_detect,
  334. .fill_modes = drm_helper_probe_single_connector_modes,
  335. .destroy = hdmi_connector_destroy,
  336. .reset = drm_atomic_helper_connector_reset,
  337. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  338. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  339. };
  340. static const struct drm_connector_helper_funcs msm_hdmi_connector_helper_funcs = {
  341. .get_modes = msm_hdmi_connector_get_modes,
  342. .mode_valid = msm_hdmi_connector_mode_valid,
  343. };
  344. /* initialize connector */
  345. struct drm_connector *msm_hdmi_connector_init(struct hdmi *hdmi)
  346. {
  347. struct drm_connector *connector = NULL;
  348. struct hdmi_connector *hdmi_connector;
  349. int ret;
  350. hdmi_connector = kzalloc(sizeof(*hdmi_connector), GFP_KERNEL);
  351. if (!hdmi_connector)
  352. return ERR_PTR(-ENOMEM);
  353. hdmi_connector->hdmi = hdmi;
  354. INIT_WORK(&hdmi_connector->hpd_work, msm_hdmi_hotplug_work);
  355. connector = &hdmi_connector->base;
  356. drm_connector_init(hdmi->dev, connector, &hdmi_connector_funcs,
  357. DRM_MODE_CONNECTOR_HDMIA);
  358. drm_connector_helper_add(connector, &msm_hdmi_connector_helper_funcs);
  359. connector->polled = DRM_CONNECTOR_POLL_CONNECT |
  360. DRM_CONNECTOR_POLL_DISCONNECT;
  361. connector->interlace_allowed = 0;
  362. connector->doublescan_allowed = 0;
  363. ret = hpd_enable(hdmi_connector);
  364. if (ret) {
  365. dev_err(&hdmi->pdev->dev, "failed to enable HPD: %d\n", ret);
  366. return ERR_PTR(ret);
  367. }
  368. drm_mode_connector_attach_encoder(connector, hdmi->encoder);
  369. return connector;
  370. }