mdp4_lcdc_encoder.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. * Copyright (C) 2014 Red Hat
  3. * Author: Rob Clark <robdclark@gmail.com>
  4. * Author: Vinay Simha <vinaysimha@inforcecomputing.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "mdp4_kms.h"
  19. #include "drm_crtc.h"
  20. #include "drm_crtc_helper.h"
  21. struct mdp4_lcdc_encoder {
  22. struct drm_encoder base;
  23. struct drm_panel *panel;
  24. struct clk *lcdc_clk;
  25. unsigned long int pixclock;
  26. struct regulator *regs[3];
  27. bool enabled;
  28. uint32_t bsc;
  29. };
  30. #define to_mdp4_lcdc_encoder(x) container_of(x, struct mdp4_lcdc_encoder, base)
  31. static struct mdp4_kms *get_kms(struct drm_encoder *encoder)
  32. {
  33. struct msm_drm_private *priv = encoder->dev->dev_private;
  34. return to_mdp4_kms(to_mdp_kms(priv->kms));
  35. }
  36. #ifdef DOWNSTREAM_CONFIG_MSM_BUS_SCALING
  37. #include <mach/board.h>
  38. static void bs_init(struct mdp4_lcdc_encoder *mdp4_lcdc_encoder)
  39. {
  40. struct drm_device *dev = mdp4_lcdc_encoder->base.dev;
  41. struct lcdc_platform_data *lcdc_pdata = mdp4_find_pdata("lvds.0");
  42. if (!lcdc_pdata) {
  43. dev_err(dev->dev, "could not find lvds pdata\n");
  44. return;
  45. }
  46. if (lcdc_pdata->bus_scale_table) {
  47. mdp4_lcdc_encoder->bsc = msm_bus_scale_register_client(
  48. lcdc_pdata->bus_scale_table);
  49. DBG("lvds : bus scale client: %08x", mdp4_lcdc_encoder->bsc);
  50. }
  51. }
  52. static void bs_fini(struct mdp4_lcdc_encoder *mdp4_lcdc_encoder)
  53. {
  54. if (mdp4_lcdc_encoder->bsc) {
  55. msm_bus_scale_unregister_client(mdp4_lcdc_encoder->bsc);
  56. mdp4_lcdc_encoder->bsc = 0;
  57. }
  58. }
  59. static void bs_set(struct mdp4_lcdc_encoder *mdp4_lcdc_encoder, int idx)
  60. {
  61. if (mdp4_lcdc_encoder->bsc) {
  62. DBG("set bus scaling: %d", idx);
  63. msm_bus_scale_client_update_request(mdp4_lcdc_encoder->bsc, idx);
  64. }
  65. }
  66. #else
  67. static void bs_init(struct mdp4_lcdc_encoder *mdp4_lcdc_encoder) {}
  68. static void bs_fini(struct mdp4_lcdc_encoder *mdp4_lcdc_encoder) {}
  69. static void bs_set(struct mdp4_lcdc_encoder *mdp4_lcdc_encoder, int idx) {}
  70. #endif
  71. static void mdp4_lcdc_encoder_destroy(struct drm_encoder *encoder)
  72. {
  73. struct mdp4_lcdc_encoder *mdp4_lcdc_encoder =
  74. to_mdp4_lcdc_encoder(encoder);
  75. bs_fini(mdp4_lcdc_encoder);
  76. drm_encoder_cleanup(encoder);
  77. kfree(mdp4_lcdc_encoder);
  78. }
  79. static const struct drm_encoder_funcs mdp4_lcdc_encoder_funcs = {
  80. .destroy = mdp4_lcdc_encoder_destroy,
  81. };
  82. /* this should probably be a helper: */
  83. struct drm_connector *get_connector(struct drm_encoder *encoder)
  84. {
  85. struct drm_device *dev = encoder->dev;
  86. struct drm_connector *connector;
  87. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  88. if (connector->encoder == encoder)
  89. return connector;
  90. return NULL;
  91. }
  92. static void setup_phy(struct drm_encoder *encoder)
  93. {
  94. struct drm_device *dev = encoder->dev;
  95. struct drm_connector *connector = get_connector(encoder);
  96. struct mdp4_kms *mdp4_kms = get_kms(encoder);
  97. uint32_t lvds_intf = 0, lvds_phy_cfg0 = 0;
  98. int bpp, nchan, swap;
  99. if (!connector)
  100. return;
  101. bpp = 3 * connector->display_info.bpc;
  102. if (!bpp)
  103. bpp = 18;
  104. /* TODO, these should come from panel somehow: */
  105. nchan = 1;
  106. swap = 0;
  107. switch (bpp) {
  108. case 24:
  109. mdp4_write(mdp4_kms, REG_MDP4_LCDC_LVDS_MUX_CTL_3_TO_0(0),
  110. MDP4_LCDC_LVDS_MUX_CTL_3_TO_0_BIT0(0x08) |
  111. MDP4_LCDC_LVDS_MUX_CTL_3_TO_0_BIT1(0x05) |
  112. MDP4_LCDC_LVDS_MUX_CTL_3_TO_0_BIT2(0x04) |
  113. MDP4_LCDC_LVDS_MUX_CTL_3_TO_0_BIT3(0x03));
  114. mdp4_write(mdp4_kms, REG_MDP4_LCDC_LVDS_MUX_CTL_6_TO_4(0),
  115. MDP4_LCDC_LVDS_MUX_CTL_6_TO_4_BIT4(0x02) |
  116. MDP4_LCDC_LVDS_MUX_CTL_6_TO_4_BIT5(0x01) |
  117. MDP4_LCDC_LVDS_MUX_CTL_6_TO_4_BIT6(0x00));
  118. mdp4_write(mdp4_kms, REG_MDP4_LCDC_LVDS_MUX_CTL_3_TO_0(1),
  119. MDP4_LCDC_LVDS_MUX_CTL_3_TO_0_BIT0(0x11) |
  120. MDP4_LCDC_LVDS_MUX_CTL_3_TO_0_BIT1(0x10) |
  121. MDP4_LCDC_LVDS_MUX_CTL_3_TO_0_BIT2(0x0d) |
  122. MDP4_LCDC_LVDS_MUX_CTL_3_TO_0_BIT3(0x0c));
  123. mdp4_write(mdp4_kms, REG_MDP4_LCDC_LVDS_MUX_CTL_6_TO_4(1),
  124. MDP4_LCDC_LVDS_MUX_CTL_6_TO_4_BIT4(0x0b) |
  125. MDP4_LCDC_LVDS_MUX_CTL_6_TO_4_BIT5(0x0a) |
  126. MDP4_LCDC_LVDS_MUX_CTL_6_TO_4_BIT6(0x09));
  127. mdp4_write(mdp4_kms, REG_MDP4_LCDC_LVDS_MUX_CTL_3_TO_0(2),
  128. MDP4_LCDC_LVDS_MUX_CTL_3_TO_0_BIT0(0x1a) |
  129. MDP4_LCDC_LVDS_MUX_CTL_3_TO_0_BIT1(0x19) |
  130. MDP4_LCDC_LVDS_MUX_CTL_3_TO_0_BIT2(0x18) |
  131. MDP4_LCDC_LVDS_MUX_CTL_3_TO_0_BIT3(0x15));
  132. mdp4_write(mdp4_kms, REG_MDP4_LCDC_LVDS_MUX_CTL_6_TO_4(2),
  133. MDP4_LCDC_LVDS_MUX_CTL_6_TO_4_BIT4(0x14) |
  134. MDP4_LCDC_LVDS_MUX_CTL_6_TO_4_BIT5(0x13) |
  135. MDP4_LCDC_LVDS_MUX_CTL_6_TO_4_BIT6(0x12));
  136. mdp4_write(mdp4_kms, REG_MDP4_LCDC_LVDS_MUX_CTL_3_TO_0(3),
  137. MDP4_LCDC_LVDS_MUX_CTL_3_TO_0_BIT0(0x1b) |
  138. MDP4_LCDC_LVDS_MUX_CTL_3_TO_0_BIT1(0x17) |
  139. MDP4_LCDC_LVDS_MUX_CTL_3_TO_0_BIT2(0x16) |
  140. MDP4_LCDC_LVDS_MUX_CTL_3_TO_0_BIT3(0x0f));
  141. mdp4_write(mdp4_kms, REG_MDP4_LCDC_LVDS_MUX_CTL_6_TO_4(3),
  142. MDP4_LCDC_LVDS_MUX_CTL_6_TO_4_BIT4(0x0e) |
  143. MDP4_LCDC_LVDS_MUX_CTL_6_TO_4_BIT5(0x07) |
  144. MDP4_LCDC_LVDS_MUX_CTL_6_TO_4_BIT6(0x06));
  145. if (nchan == 2) {
  146. lvds_intf |= MDP4_LCDC_LVDS_INTF_CTL_CH2_DATA_LANE3_EN |
  147. MDP4_LCDC_LVDS_INTF_CTL_CH2_DATA_LANE2_EN |
  148. MDP4_LCDC_LVDS_INTF_CTL_CH2_DATA_LANE1_EN |
  149. MDP4_LCDC_LVDS_INTF_CTL_CH2_DATA_LANE0_EN |
  150. MDP4_LCDC_LVDS_INTF_CTL_CH1_DATA_LANE3_EN |
  151. MDP4_LCDC_LVDS_INTF_CTL_CH1_DATA_LANE2_EN |
  152. MDP4_LCDC_LVDS_INTF_CTL_CH1_DATA_LANE1_EN |
  153. MDP4_LCDC_LVDS_INTF_CTL_CH1_DATA_LANE0_EN;
  154. } else {
  155. lvds_intf |= MDP4_LCDC_LVDS_INTF_CTL_CH1_DATA_LANE3_EN |
  156. MDP4_LCDC_LVDS_INTF_CTL_CH1_DATA_LANE2_EN |
  157. MDP4_LCDC_LVDS_INTF_CTL_CH1_DATA_LANE1_EN |
  158. MDP4_LCDC_LVDS_INTF_CTL_CH1_DATA_LANE0_EN;
  159. }
  160. break;
  161. case 18:
  162. mdp4_write(mdp4_kms, REG_MDP4_LCDC_LVDS_MUX_CTL_3_TO_0(0),
  163. MDP4_LCDC_LVDS_MUX_CTL_3_TO_0_BIT0(0x0a) |
  164. MDP4_LCDC_LVDS_MUX_CTL_3_TO_0_BIT1(0x07) |
  165. MDP4_LCDC_LVDS_MUX_CTL_3_TO_0_BIT2(0x06) |
  166. MDP4_LCDC_LVDS_MUX_CTL_3_TO_0_BIT3(0x05));
  167. mdp4_write(mdp4_kms, REG_MDP4_LCDC_LVDS_MUX_CTL_6_TO_4(0),
  168. MDP4_LCDC_LVDS_MUX_CTL_6_TO_4_BIT4(0x04) |
  169. MDP4_LCDC_LVDS_MUX_CTL_6_TO_4_BIT5(0x03) |
  170. MDP4_LCDC_LVDS_MUX_CTL_6_TO_4_BIT6(0x02));
  171. mdp4_write(mdp4_kms, REG_MDP4_LCDC_LVDS_MUX_CTL_3_TO_0(1),
  172. MDP4_LCDC_LVDS_MUX_CTL_3_TO_0_BIT0(0x13) |
  173. MDP4_LCDC_LVDS_MUX_CTL_3_TO_0_BIT1(0x12) |
  174. MDP4_LCDC_LVDS_MUX_CTL_3_TO_0_BIT2(0x0f) |
  175. MDP4_LCDC_LVDS_MUX_CTL_3_TO_0_BIT3(0x0e));
  176. mdp4_write(mdp4_kms, REG_MDP4_LCDC_LVDS_MUX_CTL_6_TO_4(1),
  177. MDP4_LCDC_LVDS_MUX_CTL_6_TO_4_BIT4(0x0d) |
  178. MDP4_LCDC_LVDS_MUX_CTL_6_TO_4_BIT5(0x0c) |
  179. MDP4_LCDC_LVDS_MUX_CTL_6_TO_4_BIT6(0x0b));
  180. mdp4_write(mdp4_kms, REG_MDP4_LCDC_LVDS_MUX_CTL_3_TO_0(2),
  181. MDP4_LCDC_LVDS_MUX_CTL_3_TO_0_BIT0(0x1a) |
  182. MDP4_LCDC_LVDS_MUX_CTL_3_TO_0_BIT1(0x19) |
  183. MDP4_LCDC_LVDS_MUX_CTL_3_TO_0_BIT2(0x18) |
  184. MDP4_LCDC_LVDS_MUX_CTL_3_TO_0_BIT3(0x17));
  185. mdp4_write(mdp4_kms, REG_MDP4_LCDC_LVDS_MUX_CTL_6_TO_4(2),
  186. MDP4_LCDC_LVDS_MUX_CTL_6_TO_4_BIT4(0x16) |
  187. MDP4_LCDC_LVDS_MUX_CTL_6_TO_4_BIT5(0x15) |
  188. MDP4_LCDC_LVDS_MUX_CTL_6_TO_4_BIT6(0x14));
  189. if (nchan == 2) {
  190. lvds_intf |= MDP4_LCDC_LVDS_INTF_CTL_CH2_DATA_LANE2_EN |
  191. MDP4_LCDC_LVDS_INTF_CTL_CH2_DATA_LANE1_EN |
  192. MDP4_LCDC_LVDS_INTF_CTL_CH2_DATA_LANE0_EN |
  193. MDP4_LCDC_LVDS_INTF_CTL_CH1_DATA_LANE2_EN |
  194. MDP4_LCDC_LVDS_INTF_CTL_CH1_DATA_LANE1_EN |
  195. MDP4_LCDC_LVDS_INTF_CTL_CH1_DATA_LANE0_EN;
  196. } else {
  197. lvds_intf |= MDP4_LCDC_LVDS_INTF_CTL_CH1_DATA_LANE2_EN |
  198. MDP4_LCDC_LVDS_INTF_CTL_CH1_DATA_LANE1_EN |
  199. MDP4_LCDC_LVDS_INTF_CTL_CH1_DATA_LANE0_EN;
  200. }
  201. lvds_intf |= MDP4_LCDC_LVDS_INTF_CTL_RGB_OUT;
  202. break;
  203. default:
  204. dev_err(dev->dev, "unknown bpp: %d\n", bpp);
  205. return;
  206. }
  207. switch (nchan) {
  208. case 1:
  209. lvds_phy_cfg0 = MDP4_LVDS_PHY_CFG0_CHANNEL0;
  210. lvds_intf |= MDP4_LCDC_LVDS_INTF_CTL_CH1_CLK_LANE_EN |
  211. MDP4_LCDC_LVDS_INTF_CTL_MODE_SEL;
  212. break;
  213. case 2:
  214. lvds_phy_cfg0 = MDP4_LVDS_PHY_CFG0_CHANNEL0 |
  215. MDP4_LVDS_PHY_CFG0_CHANNEL1;
  216. lvds_intf |= MDP4_LCDC_LVDS_INTF_CTL_CH2_CLK_LANE_EN |
  217. MDP4_LCDC_LVDS_INTF_CTL_CH1_CLK_LANE_EN;
  218. break;
  219. default:
  220. dev_err(dev->dev, "unknown # of channels: %d\n", nchan);
  221. return;
  222. }
  223. if (swap)
  224. lvds_intf |= MDP4_LCDC_LVDS_INTF_CTL_CH_SWAP;
  225. lvds_intf |= MDP4_LCDC_LVDS_INTF_CTL_ENABLE;
  226. mdp4_write(mdp4_kms, REG_MDP4_LVDS_PHY_CFG0, lvds_phy_cfg0);
  227. mdp4_write(mdp4_kms, REG_MDP4_LCDC_LVDS_INTF_CTL, lvds_intf);
  228. mdp4_write(mdp4_kms, REG_MDP4_LVDS_PHY_CFG2, 0x30);
  229. mb();
  230. udelay(1);
  231. lvds_phy_cfg0 |= MDP4_LVDS_PHY_CFG0_SERIALIZATION_ENBLE;
  232. mdp4_write(mdp4_kms, REG_MDP4_LVDS_PHY_CFG0, lvds_phy_cfg0);
  233. }
  234. static bool mdp4_lcdc_encoder_mode_fixup(struct drm_encoder *encoder,
  235. const struct drm_display_mode *mode,
  236. struct drm_display_mode *adjusted_mode)
  237. {
  238. return true;
  239. }
  240. static void mdp4_lcdc_encoder_mode_set(struct drm_encoder *encoder,
  241. struct drm_display_mode *mode,
  242. struct drm_display_mode *adjusted_mode)
  243. {
  244. struct mdp4_lcdc_encoder *mdp4_lcdc_encoder =
  245. to_mdp4_lcdc_encoder(encoder);
  246. struct mdp4_kms *mdp4_kms = get_kms(encoder);
  247. uint32_t lcdc_hsync_skew, vsync_period, vsync_len, ctrl_pol;
  248. uint32_t display_v_start, display_v_end;
  249. uint32_t hsync_start_x, hsync_end_x;
  250. mode = adjusted_mode;
  251. DBG("set mode: %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
  252. mode->base.id, mode->name,
  253. mode->vrefresh, mode->clock,
  254. mode->hdisplay, mode->hsync_start,
  255. mode->hsync_end, mode->htotal,
  256. mode->vdisplay, mode->vsync_start,
  257. mode->vsync_end, mode->vtotal,
  258. mode->type, mode->flags);
  259. mdp4_lcdc_encoder->pixclock = mode->clock * 1000;
  260. DBG("pixclock=%lu", mdp4_lcdc_encoder->pixclock);
  261. ctrl_pol = 0;
  262. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  263. ctrl_pol |= MDP4_LCDC_CTRL_POLARITY_HSYNC_LOW;
  264. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  265. ctrl_pol |= MDP4_LCDC_CTRL_POLARITY_VSYNC_LOW;
  266. /* probably need to get DATA_EN polarity from panel.. */
  267. lcdc_hsync_skew = 0; /* get this from panel? */
  268. hsync_start_x = (mode->htotal - mode->hsync_start);
  269. hsync_end_x = mode->htotal - (mode->hsync_start - mode->hdisplay) - 1;
  270. vsync_period = mode->vtotal * mode->htotal;
  271. vsync_len = (mode->vsync_end - mode->vsync_start) * mode->htotal;
  272. display_v_start = (mode->vtotal - mode->vsync_start) * mode->htotal + lcdc_hsync_skew;
  273. display_v_end = vsync_period - ((mode->vsync_start - mode->vdisplay) * mode->htotal) + lcdc_hsync_skew - 1;
  274. mdp4_write(mdp4_kms, REG_MDP4_LCDC_HSYNC_CTRL,
  275. MDP4_LCDC_HSYNC_CTRL_PULSEW(mode->hsync_end - mode->hsync_start) |
  276. MDP4_LCDC_HSYNC_CTRL_PERIOD(mode->htotal));
  277. mdp4_write(mdp4_kms, REG_MDP4_LCDC_VSYNC_PERIOD, vsync_period);
  278. mdp4_write(mdp4_kms, REG_MDP4_LCDC_VSYNC_LEN, vsync_len);
  279. mdp4_write(mdp4_kms, REG_MDP4_LCDC_DISPLAY_HCTRL,
  280. MDP4_LCDC_DISPLAY_HCTRL_START(hsync_start_x) |
  281. MDP4_LCDC_DISPLAY_HCTRL_END(hsync_end_x));
  282. mdp4_write(mdp4_kms, REG_MDP4_LCDC_DISPLAY_VSTART, display_v_start);
  283. mdp4_write(mdp4_kms, REG_MDP4_LCDC_DISPLAY_VEND, display_v_end);
  284. mdp4_write(mdp4_kms, REG_MDP4_LCDC_BORDER_CLR, 0);
  285. mdp4_write(mdp4_kms, REG_MDP4_LCDC_UNDERFLOW_CLR,
  286. MDP4_LCDC_UNDERFLOW_CLR_ENABLE_RECOVERY |
  287. MDP4_LCDC_UNDERFLOW_CLR_COLOR(0xff));
  288. mdp4_write(mdp4_kms, REG_MDP4_LCDC_HSYNC_SKEW, lcdc_hsync_skew);
  289. mdp4_write(mdp4_kms, REG_MDP4_LCDC_CTRL_POLARITY, ctrl_pol);
  290. mdp4_write(mdp4_kms, REG_MDP4_LCDC_ACTIVE_HCTL,
  291. MDP4_LCDC_ACTIVE_HCTL_START(0) |
  292. MDP4_LCDC_ACTIVE_HCTL_END(0));
  293. mdp4_write(mdp4_kms, REG_MDP4_LCDC_ACTIVE_VSTART, 0);
  294. mdp4_write(mdp4_kms, REG_MDP4_LCDC_ACTIVE_VEND, 0);
  295. }
  296. static void mdp4_lcdc_encoder_disable(struct drm_encoder *encoder)
  297. {
  298. struct drm_device *dev = encoder->dev;
  299. struct mdp4_lcdc_encoder *mdp4_lcdc_encoder =
  300. to_mdp4_lcdc_encoder(encoder);
  301. struct mdp4_kms *mdp4_kms = get_kms(encoder);
  302. struct drm_panel *panel = mdp4_lcdc_encoder->panel;
  303. int i, ret;
  304. if (WARN_ON(!mdp4_lcdc_encoder->enabled))
  305. return;
  306. mdp4_write(mdp4_kms, REG_MDP4_LCDC_ENABLE, 0);
  307. if (panel) {
  308. drm_panel_disable(panel);
  309. drm_panel_unprepare(panel);
  310. }
  311. /*
  312. * Wait for a vsync so we know the ENABLE=0 latched before
  313. * the (connector) source of the vsync's gets disabled,
  314. * otherwise we end up in a funny state if we re-enable
  315. * before the disable latches, which results that some of
  316. * the settings changes for the new modeset (like new
  317. * scanout buffer) don't latch properly..
  318. */
  319. mdp_irq_wait(&mdp4_kms->base, MDP4_IRQ_PRIMARY_VSYNC);
  320. clk_disable_unprepare(mdp4_lcdc_encoder->lcdc_clk);
  321. for (i = 0; i < ARRAY_SIZE(mdp4_lcdc_encoder->regs); i++) {
  322. ret = regulator_disable(mdp4_lcdc_encoder->regs[i]);
  323. if (ret)
  324. dev_err(dev->dev, "failed to disable regulator: %d\n", ret);
  325. }
  326. bs_set(mdp4_lcdc_encoder, 0);
  327. mdp4_lcdc_encoder->enabled = false;
  328. }
  329. static void mdp4_lcdc_encoder_enable(struct drm_encoder *encoder)
  330. {
  331. struct drm_device *dev = encoder->dev;
  332. struct mdp4_lcdc_encoder *mdp4_lcdc_encoder =
  333. to_mdp4_lcdc_encoder(encoder);
  334. unsigned long pc = mdp4_lcdc_encoder->pixclock;
  335. struct mdp4_kms *mdp4_kms = get_kms(encoder);
  336. struct drm_panel *panel = mdp4_lcdc_encoder->panel;
  337. int i, ret;
  338. if (WARN_ON(mdp4_lcdc_encoder->enabled))
  339. return;
  340. /* TODO: hard-coded for 18bpp: */
  341. mdp4_crtc_set_config(encoder->crtc,
  342. MDP4_DMA_CONFIG_R_BPC(BPC6) |
  343. MDP4_DMA_CONFIG_G_BPC(BPC6) |
  344. MDP4_DMA_CONFIG_B_BPC(BPC6) |
  345. MDP4_DMA_CONFIG_PACK_ALIGN_MSB |
  346. MDP4_DMA_CONFIG_PACK(0x21) |
  347. MDP4_DMA_CONFIG_DEFLKR_EN |
  348. MDP4_DMA_CONFIG_DITHER_EN);
  349. mdp4_crtc_set_intf(encoder->crtc, INTF_LCDC_DTV, 0);
  350. bs_set(mdp4_lcdc_encoder, 1);
  351. for (i = 0; i < ARRAY_SIZE(mdp4_lcdc_encoder->regs); i++) {
  352. ret = regulator_enable(mdp4_lcdc_encoder->regs[i]);
  353. if (ret)
  354. dev_err(dev->dev, "failed to enable regulator: %d\n", ret);
  355. }
  356. DBG("setting lcdc_clk=%lu", pc);
  357. ret = clk_set_rate(mdp4_lcdc_encoder->lcdc_clk, pc);
  358. if (ret)
  359. dev_err(dev->dev, "failed to configure lcdc_clk: %d\n", ret);
  360. ret = clk_prepare_enable(mdp4_lcdc_encoder->lcdc_clk);
  361. if (ret)
  362. dev_err(dev->dev, "failed to enable lcdc_clk: %d\n", ret);
  363. if (panel) {
  364. drm_panel_prepare(panel);
  365. drm_panel_enable(panel);
  366. }
  367. setup_phy(encoder);
  368. mdp4_write(mdp4_kms, REG_MDP4_LCDC_ENABLE, 1);
  369. mdp4_lcdc_encoder->enabled = true;
  370. }
  371. static const struct drm_encoder_helper_funcs mdp4_lcdc_encoder_helper_funcs = {
  372. .mode_fixup = mdp4_lcdc_encoder_mode_fixup,
  373. .mode_set = mdp4_lcdc_encoder_mode_set,
  374. .disable = mdp4_lcdc_encoder_disable,
  375. .enable = mdp4_lcdc_encoder_enable,
  376. };
  377. long mdp4_lcdc_round_pixclk(struct drm_encoder *encoder, unsigned long rate)
  378. {
  379. struct mdp4_lcdc_encoder *mdp4_lcdc_encoder =
  380. to_mdp4_lcdc_encoder(encoder);
  381. return clk_round_rate(mdp4_lcdc_encoder->lcdc_clk, rate);
  382. }
  383. /* initialize encoder */
  384. struct drm_encoder *mdp4_lcdc_encoder_init(struct drm_device *dev,
  385. struct drm_panel *panel)
  386. {
  387. struct drm_encoder *encoder = NULL;
  388. struct mdp4_lcdc_encoder *mdp4_lcdc_encoder;
  389. struct regulator *reg;
  390. int ret;
  391. mdp4_lcdc_encoder = kzalloc(sizeof(*mdp4_lcdc_encoder), GFP_KERNEL);
  392. if (!mdp4_lcdc_encoder) {
  393. ret = -ENOMEM;
  394. goto fail;
  395. }
  396. mdp4_lcdc_encoder->panel = panel;
  397. encoder = &mdp4_lcdc_encoder->base;
  398. drm_encoder_init(dev, encoder, &mdp4_lcdc_encoder_funcs,
  399. DRM_MODE_ENCODER_LVDS);
  400. drm_encoder_helper_add(encoder, &mdp4_lcdc_encoder_helper_funcs);
  401. /* TODO: do we need different pll in other cases? */
  402. mdp4_lcdc_encoder->lcdc_clk = mpd4_lvds_pll_init(dev);
  403. if (IS_ERR(mdp4_lcdc_encoder->lcdc_clk)) {
  404. dev_err(dev->dev, "failed to get lvds_clk\n");
  405. ret = PTR_ERR(mdp4_lcdc_encoder->lcdc_clk);
  406. goto fail;
  407. }
  408. /* TODO: different regulators in other cases? */
  409. reg = devm_regulator_get(dev->dev, "lvds-vccs-3p3v");
  410. if (IS_ERR(reg)) {
  411. ret = PTR_ERR(reg);
  412. dev_err(dev->dev, "failed to get lvds-vccs-3p3v: %d\n", ret);
  413. goto fail;
  414. }
  415. mdp4_lcdc_encoder->regs[0] = reg;
  416. reg = devm_regulator_get(dev->dev, "lvds-pll-vdda");
  417. if (IS_ERR(reg)) {
  418. ret = PTR_ERR(reg);
  419. dev_err(dev->dev, "failed to get lvds-pll-vdda: %d\n", ret);
  420. goto fail;
  421. }
  422. mdp4_lcdc_encoder->regs[1] = reg;
  423. reg = devm_regulator_get(dev->dev, "lvds-vdda");
  424. if (IS_ERR(reg)) {
  425. ret = PTR_ERR(reg);
  426. dev_err(dev->dev, "failed to get lvds-vdda: %d\n", ret);
  427. goto fail;
  428. }
  429. mdp4_lcdc_encoder->regs[2] = reg;
  430. bs_init(mdp4_lcdc_encoder);
  431. return encoder;
  432. fail:
  433. if (encoder)
  434. mdp4_lcdc_encoder_destroy(encoder);
  435. return ERR_PTR(ret);
  436. }