hdmi_connector.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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 void enable_hpd_clocks(struct hdmi *hdmi, bool enable)
  118. {
  119. const struct hdmi_platform_config *config = hdmi->config;
  120. struct device *dev = &hdmi->pdev->dev;
  121. int i, ret;
  122. if (enable) {
  123. for (i = 0; i < config->hpd_clk_cnt; i++) {
  124. if (config->hpd_freq && config->hpd_freq[i]) {
  125. ret = clk_set_rate(hdmi->hpd_clks[i],
  126. config->hpd_freq[i]);
  127. if (ret)
  128. dev_warn(dev,
  129. "failed to set clk %s (%d)\n",
  130. config->hpd_clk_names[i], ret);
  131. }
  132. ret = clk_prepare_enable(hdmi->hpd_clks[i]);
  133. if (ret) {
  134. dev_err(dev,
  135. "failed to enable hpd clk: %s (%d)\n",
  136. config->hpd_clk_names[i], ret);
  137. }
  138. }
  139. } else {
  140. for (i = config->hpd_clk_cnt - 1; i >= 0; i--)
  141. clk_disable_unprepare(hdmi->hpd_clks[i]);
  142. }
  143. }
  144. int msm_hdmi_hpd_enable(struct drm_connector *connector)
  145. {
  146. struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
  147. struct hdmi *hdmi = hdmi_connector->hdmi;
  148. const struct hdmi_platform_config *config = hdmi->config;
  149. struct device *dev = &hdmi->pdev->dev;
  150. uint32_t hpd_ctrl;
  151. int i, ret;
  152. unsigned long flags;
  153. for (i = 0; i < config->hpd_reg_cnt; i++) {
  154. ret = regulator_enable(hdmi->hpd_regs[i]);
  155. if (ret) {
  156. dev_err(dev, "failed to enable hpd regulator: %s (%d)\n",
  157. config->hpd_reg_names[i], ret);
  158. goto fail;
  159. }
  160. }
  161. ret = pinctrl_pm_select_default_state(dev);
  162. if (ret) {
  163. dev_err(dev, "pinctrl state chg failed: %d\n", ret);
  164. goto fail;
  165. }
  166. ret = gpio_config(hdmi, true);
  167. if (ret) {
  168. dev_err(dev, "failed to configure GPIOs: %d\n", ret);
  169. goto fail;
  170. }
  171. pm_runtime_get_sync(dev);
  172. enable_hpd_clocks(hdmi, true);
  173. msm_hdmi_set_mode(hdmi, false);
  174. msm_hdmi_phy_reset(hdmi);
  175. msm_hdmi_set_mode(hdmi, true);
  176. hdmi_write(hdmi, REG_HDMI_USEC_REFTIMER, 0x0001001b);
  177. /* enable HPD events: */
  178. hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL,
  179. HDMI_HPD_INT_CTRL_INT_CONNECT |
  180. HDMI_HPD_INT_CTRL_INT_EN);
  181. /* set timeout to 4.1ms (max) for hardware debounce */
  182. spin_lock_irqsave(&hdmi->reg_lock, flags);
  183. hpd_ctrl = hdmi_read(hdmi, REG_HDMI_HPD_CTRL);
  184. hpd_ctrl |= HDMI_HPD_CTRL_TIMEOUT(0x1fff);
  185. /* Toggle HPD circuit to trigger HPD sense */
  186. hdmi_write(hdmi, REG_HDMI_HPD_CTRL,
  187. ~HDMI_HPD_CTRL_ENABLE & hpd_ctrl);
  188. hdmi_write(hdmi, REG_HDMI_HPD_CTRL,
  189. HDMI_HPD_CTRL_ENABLE | hpd_ctrl);
  190. spin_unlock_irqrestore(&hdmi->reg_lock, flags);
  191. return 0;
  192. fail:
  193. return ret;
  194. }
  195. static void hdp_disable(struct hdmi_connector *hdmi_connector)
  196. {
  197. struct hdmi *hdmi = hdmi_connector->hdmi;
  198. const struct hdmi_platform_config *config = hdmi->config;
  199. struct device *dev = &hdmi->pdev->dev;
  200. int i, ret = 0;
  201. /* Disable HPD interrupt */
  202. hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL, 0);
  203. msm_hdmi_set_mode(hdmi, false);
  204. enable_hpd_clocks(hdmi, false);
  205. pm_runtime_put_autosuspend(dev);
  206. ret = gpio_config(hdmi, false);
  207. if (ret)
  208. dev_warn(dev, "failed to unconfigure GPIOs: %d\n", ret);
  209. ret = pinctrl_pm_select_sleep_state(dev);
  210. if (ret)
  211. dev_warn(dev, "pinctrl state chg failed: %d\n", ret);
  212. for (i = 0; i < config->hpd_reg_cnt; i++) {
  213. ret = regulator_disable(hdmi->hpd_regs[i]);
  214. if (ret)
  215. dev_warn(dev, "failed to disable hpd regulator: %s (%d)\n",
  216. config->hpd_reg_names[i], ret);
  217. }
  218. }
  219. static void
  220. msm_hdmi_hotplug_work(struct work_struct *work)
  221. {
  222. struct hdmi_connector *hdmi_connector =
  223. container_of(work, struct hdmi_connector, hpd_work);
  224. struct drm_connector *connector = &hdmi_connector->base;
  225. drm_helper_hpd_irq_event(connector->dev);
  226. }
  227. void msm_hdmi_connector_irq(struct drm_connector *connector)
  228. {
  229. struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
  230. struct hdmi *hdmi = hdmi_connector->hdmi;
  231. uint32_t hpd_int_status, hpd_int_ctrl;
  232. /* Process HPD: */
  233. hpd_int_status = hdmi_read(hdmi, REG_HDMI_HPD_INT_STATUS);
  234. hpd_int_ctrl = hdmi_read(hdmi, REG_HDMI_HPD_INT_CTRL);
  235. if ((hpd_int_ctrl & HDMI_HPD_INT_CTRL_INT_EN) &&
  236. (hpd_int_status & HDMI_HPD_INT_STATUS_INT)) {
  237. bool detected = !!(hpd_int_status & HDMI_HPD_INT_STATUS_CABLE_DETECTED);
  238. /* ack & disable (temporarily) HPD events: */
  239. hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL,
  240. HDMI_HPD_INT_CTRL_INT_ACK);
  241. DBG("status=%04x, ctrl=%04x", hpd_int_status, hpd_int_ctrl);
  242. /* detect disconnect if we are connected or visa versa: */
  243. hpd_int_ctrl = HDMI_HPD_INT_CTRL_INT_EN;
  244. if (!detected)
  245. hpd_int_ctrl |= HDMI_HPD_INT_CTRL_INT_CONNECT;
  246. hdmi_write(hdmi, REG_HDMI_HPD_INT_CTRL, hpd_int_ctrl);
  247. queue_work(hdmi->workq, &hdmi_connector->hpd_work);
  248. }
  249. }
  250. static enum drm_connector_status detect_reg(struct hdmi *hdmi)
  251. {
  252. uint32_t hpd_int_status;
  253. pm_runtime_get_sync(&hdmi->pdev->dev);
  254. enable_hpd_clocks(hdmi, true);
  255. hpd_int_status = hdmi_read(hdmi, REG_HDMI_HPD_INT_STATUS);
  256. enable_hpd_clocks(hdmi, false);
  257. pm_runtime_put_autosuspend(&hdmi->pdev->dev);
  258. return (hpd_int_status & HDMI_HPD_INT_STATUS_CABLE_DETECTED) ?
  259. connector_status_connected : connector_status_disconnected;
  260. }
  261. #define HPD_GPIO_INDEX 2
  262. static enum drm_connector_status detect_gpio(struct hdmi *hdmi)
  263. {
  264. const struct hdmi_platform_config *config = hdmi->config;
  265. struct hdmi_gpio_data hpd_gpio = config->gpios[HPD_GPIO_INDEX];
  266. return gpio_get_value(hpd_gpio.num) ?
  267. connector_status_connected :
  268. connector_status_disconnected;
  269. }
  270. static enum drm_connector_status hdmi_connector_detect(
  271. struct drm_connector *connector, bool force)
  272. {
  273. struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
  274. struct hdmi *hdmi = hdmi_connector->hdmi;
  275. const struct hdmi_platform_config *config = hdmi->config;
  276. struct hdmi_gpio_data hpd_gpio = config->gpios[HPD_GPIO_INDEX];
  277. enum drm_connector_status stat_gpio, stat_reg;
  278. int retry = 20;
  279. /*
  280. * some platforms may not have hpd gpio. Rely only on the status
  281. * provided by REG_HDMI_HPD_INT_STATUS in this case.
  282. */
  283. if (hpd_gpio.num == -1)
  284. return detect_reg(hdmi);
  285. do {
  286. stat_gpio = detect_gpio(hdmi);
  287. stat_reg = detect_reg(hdmi);
  288. if (stat_gpio == stat_reg)
  289. break;
  290. mdelay(10);
  291. } while (--retry);
  292. /* the status we get from reading gpio seems to be more reliable,
  293. * so trust that one the most if we didn't manage to get hdmi and
  294. * gpio status to agree:
  295. */
  296. if (stat_gpio != stat_reg) {
  297. DBG("HDMI_HPD_INT_STATUS tells us: %d", stat_reg);
  298. DBG("hpd gpio tells us: %d", stat_gpio);
  299. }
  300. return stat_gpio;
  301. }
  302. static void hdmi_connector_destroy(struct drm_connector *connector)
  303. {
  304. struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
  305. hdp_disable(hdmi_connector);
  306. drm_connector_cleanup(connector);
  307. kfree(hdmi_connector);
  308. }
  309. static int msm_hdmi_connector_get_modes(struct drm_connector *connector)
  310. {
  311. struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
  312. struct hdmi *hdmi = hdmi_connector->hdmi;
  313. struct edid *edid;
  314. uint32_t hdmi_ctrl;
  315. int ret = 0;
  316. hdmi_ctrl = hdmi_read(hdmi, REG_HDMI_CTRL);
  317. hdmi_write(hdmi, REG_HDMI_CTRL, hdmi_ctrl | HDMI_CTRL_ENABLE);
  318. edid = drm_get_edid(connector, hdmi->i2c);
  319. hdmi_write(hdmi, REG_HDMI_CTRL, hdmi_ctrl);
  320. hdmi->hdmi_mode = drm_detect_hdmi_monitor(edid);
  321. drm_connector_update_edid_property(connector, edid);
  322. if (edid) {
  323. ret = drm_add_edid_modes(connector, edid);
  324. kfree(edid);
  325. }
  326. return ret;
  327. }
  328. static int msm_hdmi_connector_mode_valid(struct drm_connector *connector,
  329. struct drm_display_mode *mode)
  330. {
  331. struct hdmi_connector *hdmi_connector = to_hdmi_connector(connector);
  332. struct hdmi *hdmi = hdmi_connector->hdmi;
  333. const struct hdmi_platform_config *config = hdmi->config;
  334. struct msm_drm_private *priv = connector->dev->dev_private;
  335. struct msm_kms *kms = priv->kms;
  336. long actual, requested;
  337. requested = 1000 * mode->clock;
  338. actual = kms->funcs->round_pixclk(kms,
  339. requested, hdmi_connector->hdmi->encoder);
  340. /* for mdp5/apq8074, we manage our own pixel clk (as opposed to
  341. * mdp4/dtv stuff where pixel clk is assigned to mdp/encoder
  342. * instead):
  343. */
  344. if (config->pwr_clk_cnt > 0)
  345. actual = clk_round_rate(hdmi->pwr_clks[0], actual);
  346. DBG("requested=%ld, actual=%ld", requested, actual);
  347. if (actual != requested)
  348. return MODE_CLOCK_RANGE;
  349. return 0;
  350. }
  351. static const struct drm_connector_funcs hdmi_connector_funcs = {
  352. .detect = hdmi_connector_detect,
  353. .fill_modes = drm_helper_probe_single_connector_modes,
  354. .destroy = hdmi_connector_destroy,
  355. .reset = drm_atomic_helper_connector_reset,
  356. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  357. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  358. };
  359. static const struct drm_connector_helper_funcs msm_hdmi_connector_helper_funcs = {
  360. .get_modes = msm_hdmi_connector_get_modes,
  361. .mode_valid = msm_hdmi_connector_mode_valid,
  362. };
  363. /* initialize connector */
  364. struct drm_connector *msm_hdmi_connector_init(struct hdmi *hdmi)
  365. {
  366. struct drm_connector *connector = NULL;
  367. struct hdmi_connector *hdmi_connector;
  368. hdmi_connector = kzalloc(sizeof(*hdmi_connector), GFP_KERNEL);
  369. if (!hdmi_connector)
  370. return ERR_PTR(-ENOMEM);
  371. hdmi_connector->hdmi = hdmi;
  372. INIT_WORK(&hdmi_connector->hpd_work, msm_hdmi_hotplug_work);
  373. connector = &hdmi_connector->base;
  374. drm_connector_init(hdmi->dev, connector, &hdmi_connector_funcs,
  375. DRM_MODE_CONNECTOR_HDMIA);
  376. drm_connector_helper_add(connector, &msm_hdmi_connector_helper_funcs);
  377. connector->polled = DRM_CONNECTOR_POLL_CONNECT |
  378. DRM_CONNECTOR_POLL_DISCONNECT;
  379. connector->interlace_allowed = 0;
  380. connector->doublescan_allowed = 0;
  381. drm_connector_attach_encoder(connector, hdmi->encoder);
  382. return connector;
  383. }