hdmi_connector.c 12 KB

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