shmob_drm_crtc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /*
  2. * shmob_drm_crtc.c -- SH Mobile DRM CRTCs
  3. *
  4. * Copyright (C) 2012 Renesas Electronics Corporation
  5. *
  6. * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/backlight.h>
  14. #include <linux/clk.h>
  15. #include <drm/drmP.h>
  16. #include <drm/drm_crtc.h>
  17. #include <drm/drm_crtc_helper.h>
  18. #include <drm/drm_fb_cma_helper.h>
  19. #include <drm/drm_gem_cma_helper.h>
  20. #include <drm/drm_plane_helper.h>
  21. #include <video/sh_mobile_meram.h>
  22. #include "shmob_drm_backlight.h"
  23. #include "shmob_drm_crtc.h"
  24. #include "shmob_drm_drv.h"
  25. #include "shmob_drm_kms.h"
  26. #include "shmob_drm_plane.h"
  27. #include "shmob_drm_regs.h"
  28. /*
  29. * TODO: panel support
  30. */
  31. /* -----------------------------------------------------------------------------
  32. * Clock management
  33. */
  34. static int shmob_drm_clk_on(struct shmob_drm_device *sdev)
  35. {
  36. int ret;
  37. if (sdev->clock) {
  38. ret = clk_prepare_enable(sdev->clock);
  39. if (ret < 0)
  40. return ret;
  41. }
  42. #if 0
  43. if (sdev->meram_dev && sdev->meram_dev->pdev)
  44. pm_runtime_get_sync(&sdev->meram_dev->pdev->dev);
  45. #endif
  46. return 0;
  47. }
  48. static void shmob_drm_clk_off(struct shmob_drm_device *sdev)
  49. {
  50. #if 0
  51. if (sdev->meram_dev && sdev->meram_dev->pdev)
  52. pm_runtime_put_sync(&sdev->meram_dev->pdev->dev);
  53. #endif
  54. if (sdev->clock)
  55. clk_disable_unprepare(sdev->clock);
  56. }
  57. /* -----------------------------------------------------------------------------
  58. * CRTC
  59. */
  60. static void shmob_drm_crtc_setup_geometry(struct shmob_drm_crtc *scrtc)
  61. {
  62. struct drm_crtc *crtc = &scrtc->crtc;
  63. struct shmob_drm_device *sdev = crtc->dev->dev_private;
  64. const struct shmob_drm_interface_data *idata = &sdev->pdata->iface;
  65. const struct drm_display_mode *mode = &crtc->mode;
  66. u32 value;
  67. value = sdev->ldmt1r
  68. | ((mode->flags & DRM_MODE_FLAG_PVSYNC) ? 0 : LDMT1R_VPOL)
  69. | ((mode->flags & DRM_MODE_FLAG_PHSYNC) ? 0 : LDMT1R_HPOL)
  70. | ((idata->flags & SHMOB_DRM_IFACE_FL_DWPOL) ? LDMT1R_DWPOL : 0)
  71. | ((idata->flags & SHMOB_DRM_IFACE_FL_DIPOL) ? LDMT1R_DIPOL : 0)
  72. | ((idata->flags & SHMOB_DRM_IFACE_FL_DAPOL) ? LDMT1R_DAPOL : 0)
  73. | ((idata->flags & SHMOB_DRM_IFACE_FL_HSCNT) ? LDMT1R_HSCNT : 0)
  74. | ((idata->flags & SHMOB_DRM_IFACE_FL_DWCNT) ? LDMT1R_DWCNT : 0);
  75. lcdc_write(sdev, LDMT1R, value);
  76. if (idata->interface >= SHMOB_DRM_IFACE_SYS8A &&
  77. idata->interface <= SHMOB_DRM_IFACE_SYS24) {
  78. /* Setup SYS bus. */
  79. value = (idata->sys.cs_setup << LDMT2R_CSUP_SHIFT)
  80. | (idata->sys.vsync_active_high ? LDMT2R_RSV : 0)
  81. | (idata->sys.vsync_dir_input ? LDMT2R_VSEL : 0)
  82. | (idata->sys.write_setup << LDMT2R_WCSC_SHIFT)
  83. | (idata->sys.write_cycle << LDMT2R_WCEC_SHIFT)
  84. | (idata->sys.write_strobe << LDMT2R_WCLW_SHIFT);
  85. lcdc_write(sdev, LDMT2R, value);
  86. value = (idata->sys.read_latch << LDMT3R_RDLC_SHIFT)
  87. | (idata->sys.read_setup << LDMT3R_RCSC_SHIFT)
  88. | (idata->sys.read_cycle << LDMT3R_RCEC_SHIFT)
  89. | (idata->sys.read_strobe << LDMT3R_RCLW_SHIFT);
  90. lcdc_write(sdev, LDMT3R, value);
  91. }
  92. value = ((mode->hdisplay / 8) << 16) /* HDCN */
  93. | (mode->htotal / 8); /* HTCN */
  94. lcdc_write(sdev, LDHCNR, value);
  95. value = (((mode->hsync_end - mode->hsync_start) / 8) << 16) /* HSYNW */
  96. | (mode->hsync_start / 8); /* HSYNP */
  97. lcdc_write(sdev, LDHSYNR, value);
  98. value = ((mode->hdisplay & 7) << 24) | ((mode->htotal & 7) << 16)
  99. | (((mode->hsync_end - mode->hsync_start) & 7) << 8)
  100. | (mode->hsync_start & 7);
  101. lcdc_write(sdev, LDHAJR, value);
  102. value = ((mode->vdisplay) << 16) /* VDLN */
  103. | mode->vtotal; /* VTLN */
  104. lcdc_write(sdev, LDVLNR, value);
  105. value = ((mode->vsync_end - mode->vsync_start) << 16) /* VSYNW */
  106. | mode->vsync_start; /* VSYNP */
  107. lcdc_write(sdev, LDVSYNR, value);
  108. }
  109. static void shmob_drm_crtc_start_stop(struct shmob_drm_crtc *scrtc, bool start)
  110. {
  111. struct shmob_drm_device *sdev = scrtc->crtc.dev->dev_private;
  112. u32 value;
  113. value = lcdc_read(sdev, LDCNT2R);
  114. if (start)
  115. lcdc_write(sdev, LDCNT2R, value | LDCNT2R_DO);
  116. else
  117. lcdc_write(sdev, LDCNT2R, value & ~LDCNT2R_DO);
  118. /* Wait until power is applied/stopped. */
  119. while (1) {
  120. value = lcdc_read(sdev, LDPMR) & LDPMR_LPS;
  121. if ((start && value) || (!start && !value))
  122. break;
  123. cpu_relax();
  124. }
  125. if (!start) {
  126. /* Stop the dot clock. */
  127. lcdc_write(sdev, LDDCKSTPR, LDDCKSTPR_DCKSTP);
  128. }
  129. }
  130. /*
  131. * shmob_drm_crtc_start - Configure and start the LCDC
  132. * @scrtc: the SH Mobile CRTC
  133. *
  134. * Configure and start the LCDC device. External devices (clocks, MERAM, panels,
  135. * ...) are not touched by this function.
  136. */
  137. static void shmob_drm_crtc_start(struct shmob_drm_crtc *scrtc)
  138. {
  139. struct drm_crtc *crtc = &scrtc->crtc;
  140. struct shmob_drm_device *sdev = crtc->dev->dev_private;
  141. const struct shmob_drm_interface_data *idata = &sdev->pdata->iface;
  142. const struct shmob_drm_format_info *format;
  143. struct drm_device *dev = sdev->ddev;
  144. struct drm_plane *plane;
  145. u32 value;
  146. int ret;
  147. if (scrtc->started)
  148. return;
  149. format = shmob_drm_format_info(crtc->primary->fb->format->format);
  150. if (WARN_ON(format == NULL))
  151. return;
  152. /* Enable clocks before accessing the hardware. */
  153. ret = shmob_drm_clk_on(sdev);
  154. if (ret < 0)
  155. return;
  156. /* Reset and enable the LCDC. */
  157. lcdc_write(sdev, LDCNT2R, lcdc_read(sdev, LDCNT2R) | LDCNT2R_BR);
  158. lcdc_wait_bit(sdev, LDCNT2R, LDCNT2R_BR, 0);
  159. lcdc_write(sdev, LDCNT2R, LDCNT2R_ME);
  160. /* Stop the LCDC first and disable all interrupts. */
  161. shmob_drm_crtc_start_stop(scrtc, false);
  162. lcdc_write(sdev, LDINTR, 0);
  163. /* Configure power supply, dot clocks and start them. */
  164. lcdc_write(sdev, LDPMR, 0);
  165. value = sdev->lddckr;
  166. if (idata->clk_div) {
  167. /* FIXME: sh7724 can only use 42, 48, 54 and 60 for the divider
  168. * denominator.
  169. */
  170. lcdc_write(sdev, LDDCKPAT1R, 0);
  171. lcdc_write(sdev, LDDCKPAT2R, (1 << (idata->clk_div / 2)) - 1);
  172. if (idata->clk_div == 1)
  173. value |= LDDCKR_MOSEL;
  174. else
  175. value |= idata->clk_div;
  176. }
  177. lcdc_write(sdev, LDDCKR, value);
  178. lcdc_write(sdev, LDDCKSTPR, 0);
  179. lcdc_wait_bit(sdev, LDDCKSTPR, ~0, 0);
  180. /* TODO: Setup SYS panel */
  181. /* Setup geometry, format, frame buffer memory and operation mode. */
  182. shmob_drm_crtc_setup_geometry(scrtc);
  183. /* TODO: Handle YUV colorspaces. Hardcode REC709 for now. */
  184. lcdc_write(sdev, LDDFR, format->lddfr | LDDFR_CF1);
  185. lcdc_write(sdev, LDMLSR, scrtc->line_size);
  186. lcdc_write(sdev, LDSA1R, scrtc->dma[0]);
  187. if (format->yuv)
  188. lcdc_write(sdev, LDSA2R, scrtc->dma[1]);
  189. lcdc_write(sdev, LDSM1R, 0);
  190. /* Word and long word swap. */
  191. switch (format->fourcc) {
  192. case DRM_FORMAT_RGB565:
  193. case DRM_FORMAT_NV21:
  194. case DRM_FORMAT_NV61:
  195. case DRM_FORMAT_NV42:
  196. value = LDDDSR_LS | LDDDSR_WS;
  197. break;
  198. case DRM_FORMAT_RGB888:
  199. case DRM_FORMAT_NV12:
  200. case DRM_FORMAT_NV16:
  201. case DRM_FORMAT_NV24:
  202. value = LDDDSR_LS | LDDDSR_WS | LDDDSR_BS;
  203. break;
  204. case DRM_FORMAT_ARGB8888:
  205. default:
  206. value = LDDDSR_LS;
  207. break;
  208. }
  209. lcdc_write(sdev, LDDDSR, value);
  210. /* Setup planes. */
  211. drm_for_each_legacy_plane(plane, dev) {
  212. if (plane->crtc == crtc)
  213. shmob_drm_plane_setup(plane);
  214. }
  215. /* Enable the display output. */
  216. lcdc_write(sdev, LDCNT1R, LDCNT1R_DE);
  217. shmob_drm_crtc_start_stop(scrtc, true);
  218. scrtc->started = true;
  219. }
  220. static void shmob_drm_crtc_stop(struct shmob_drm_crtc *scrtc)
  221. {
  222. struct drm_crtc *crtc = &scrtc->crtc;
  223. struct shmob_drm_device *sdev = crtc->dev->dev_private;
  224. if (!scrtc->started)
  225. return;
  226. /* Disable the MERAM cache. */
  227. if (scrtc->cache) {
  228. sh_mobile_meram_cache_free(sdev->meram, scrtc->cache);
  229. scrtc->cache = NULL;
  230. }
  231. /* Stop the LCDC. */
  232. shmob_drm_crtc_start_stop(scrtc, false);
  233. /* Disable the display output. */
  234. lcdc_write(sdev, LDCNT1R, 0);
  235. /* Stop clocks. */
  236. shmob_drm_clk_off(sdev);
  237. scrtc->started = false;
  238. }
  239. void shmob_drm_crtc_suspend(struct shmob_drm_crtc *scrtc)
  240. {
  241. shmob_drm_crtc_stop(scrtc);
  242. }
  243. void shmob_drm_crtc_resume(struct shmob_drm_crtc *scrtc)
  244. {
  245. if (scrtc->dpms != DRM_MODE_DPMS_ON)
  246. return;
  247. shmob_drm_crtc_start(scrtc);
  248. }
  249. static void shmob_drm_crtc_compute_base(struct shmob_drm_crtc *scrtc,
  250. int x, int y)
  251. {
  252. struct drm_crtc *crtc = &scrtc->crtc;
  253. struct drm_framebuffer *fb = crtc->primary->fb;
  254. struct shmob_drm_device *sdev = crtc->dev->dev_private;
  255. struct drm_gem_cma_object *gem;
  256. unsigned int bpp;
  257. bpp = scrtc->format->yuv ? 8 : scrtc->format->bpp;
  258. gem = drm_fb_cma_get_gem_obj(fb, 0);
  259. scrtc->dma[0] = gem->paddr + fb->offsets[0]
  260. + y * fb->pitches[0] + x * bpp / 8;
  261. if (scrtc->format->yuv) {
  262. bpp = scrtc->format->bpp - 8;
  263. gem = drm_fb_cma_get_gem_obj(fb, 1);
  264. scrtc->dma[1] = gem->paddr + fb->offsets[1]
  265. + y / (bpp == 4 ? 2 : 1) * fb->pitches[1]
  266. + x * (bpp == 16 ? 2 : 1);
  267. }
  268. if (scrtc->cache)
  269. sh_mobile_meram_cache_update(sdev->meram, scrtc->cache,
  270. scrtc->dma[0], scrtc->dma[1],
  271. &scrtc->dma[0], &scrtc->dma[1]);
  272. }
  273. static void shmob_drm_crtc_update_base(struct shmob_drm_crtc *scrtc)
  274. {
  275. struct drm_crtc *crtc = &scrtc->crtc;
  276. struct shmob_drm_device *sdev = crtc->dev->dev_private;
  277. shmob_drm_crtc_compute_base(scrtc, crtc->x, crtc->y);
  278. lcdc_write_mirror(sdev, LDSA1R, scrtc->dma[0]);
  279. if (scrtc->format->yuv)
  280. lcdc_write_mirror(sdev, LDSA2R, scrtc->dma[1]);
  281. lcdc_write(sdev, LDRCNTR, lcdc_read(sdev, LDRCNTR) ^ LDRCNTR_MRS);
  282. }
  283. #define to_shmob_crtc(c) container_of(c, struct shmob_drm_crtc, crtc)
  284. static void shmob_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
  285. {
  286. struct shmob_drm_crtc *scrtc = to_shmob_crtc(crtc);
  287. if (scrtc->dpms == mode)
  288. return;
  289. if (mode == DRM_MODE_DPMS_ON)
  290. shmob_drm_crtc_start(scrtc);
  291. else
  292. shmob_drm_crtc_stop(scrtc);
  293. scrtc->dpms = mode;
  294. }
  295. static void shmob_drm_crtc_mode_prepare(struct drm_crtc *crtc)
  296. {
  297. shmob_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  298. }
  299. static int shmob_drm_crtc_mode_set(struct drm_crtc *crtc,
  300. struct drm_display_mode *mode,
  301. struct drm_display_mode *adjusted_mode,
  302. int x, int y,
  303. struct drm_framebuffer *old_fb)
  304. {
  305. struct shmob_drm_crtc *scrtc = to_shmob_crtc(crtc);
  306. struct shmob_drm_device *sdev = crtc->dev->dev_private;
  307. const struct sh_mobile_meram_cfg *mdata = sdev->pdata->meram;
  308. const struct shmob_drm_format_info *format;
  309. void *cache;
  310. format = shmob_drm_format_info(crtc->primary->fb->format->format);
  311. if (format == NULL) {
  312. dev_dbg(sdev->dev, "mode_set: unsupported format %08x\n",
  313. crtc->primary->fb->format->format);
  314. return -EINVAL;
  315. }
  316. scrtc->format = format;
  317. scrtc->line_size = crtc->primary->fb->pitches[0];
  318. if (sdev->meram) {
  319. /* Enable MERAM cache if configured. We need to de-init
  320. * configured ICBs before we can re-initialize them.
  321. */
  322. if (scrtc->cache) {
  323. sh_mobile_meram_cache_free(sdev->meram, scrtc->cache);
  324. scrtc->cache = NULL;
  325. }
  326. cache = sh_mobile_meram_cache_alloc(sdev->meram, mdata,
  327. crtc->primary->fb->pitches[0],
  328. adjusted_mode->vdisplay,
  329. format->meram,
  330. &scrtc->line_size);
  331. if (!IS_ERR(cache))
  332. scrtc->cache = cache;
  333. }
  334. shmob_drm_crtc_compute_base(scrtc, x, y);
  335. return 0;
  336. }
  337. static void shmob_drm_crtc_mode_commit(struct drm_crtc *crtc)
  338. {
  339. shmob_drm_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
  340. }
  341. static int shmob_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  342. struct drm_framebuffer *old_fb)
  343. {
  344. shmob_drm_crtc_update_base(to_shmob_crtc(crtc));
  345. return 0;
  346. }
  347. static const struct drm_crtc_helper_funcs crtc_helper_funcs = {
  348. .dpms = shmob_drm_crtc_dpms,
  349. .prepare = shmob_drm_crtc_mode_prepare,
  350. .commit = shmob_drm_crtc_mode_commit,
  351. .mode_set = shmob_drm_crtc_mode_set,
  352. .mode_set_base = shmob_drm_crtc_mode_set_base,
  353. };
  354. void shmob_drm_crtc_finish_page_flip(struct shmob_drm_crtc *scrtc)
  355. {
  356. struct drm_pending_vblank_event *event;
  357. struct drm_device *dev = scrtc->crtc.dev;
  358. unsigned long flags;
  359. spin_lock_irqsave(&dev->event_lock, flags);
  360. event = scrtc->event;
  361. scrtc->event = NULL;
  362. if (event) {
  363. drm_crtc_send_vblank_event(&scrtc->crtc, event);
  364. drm_crtc_vblank_put(&scrtc->crtc);
  365. }
  366. spin_unlock_irqrestore(&dev->event_lock, flags);
  367. }
  368. static int shmob_drm_crtc_page_flip(struct drm_crtc *crtc,
  369. struct drm_framebuffer *fb,
  370. struct drm_pending_vblank_event *event,
  371. uint32_t page_flip_flags,
  372. struct drm_modeset_acquire_ctx *ctx)
  373. {
  374. struct shmob_drm_crtc *scrtc = to_shmob_crtc(crtc);
  375. struct drm_device *dev = scrtc->crtc.dev;
  376. unsigned long flags;
  377. spin_lock_irqsave(&dev->event_lock, flags);
  378. if (scrtc->event != NULL) {
  379. spin_unlock_irqrestore(&dev->event_lock, flags);
  380. return -EBUSY;
  381. }
  382. spin_unlock_irqrestore(&dev->event_lock, flags);
  383. crtc->primary->fb = fb;
  384. shmob_drm_crtc_update_base(scrtc);
  385. if (event) {
  386. event->pipe = 0;
  387. drm_crtc_vblank_get(&scrtc->crtc);
  388. spin_lock_irqsave(&dev->event_lock, flags);
  389. scrtc->event = event;
  390. spin_unlock_irqrestore(&dev->event_lock, flags);
  391. }
  392. return 0;
  393. }
  394. static void shmob_drm_crtc_enable_vblank(struct shmob_drm_device *sdev,
  395. bool enable)
  396. {
  397. unsigned long flags;
  398. u32 ldintr;
  399. /* Be careful not to acknowledge any pending interrupt. */
  400. spin_lock_irqsave(&sdev->irq_lock, flags);
  401. ldintr = lcdc_read(sdev, LDINTR) | LDINTR_STATUS_MASK;
  402. if (enable)
  403. ldintr |= LDINTR_VEE;
  404. else
  405. ldintr &= ~LDINTR_VEE;
  406. lcdc_write(sdev, LDINTR, ldintr);
  407. spin_unlock_irqrestore(&sdev->irq_lock, flags);
  408. }
  409. static int shmob_drm_enable_vblank(struct drm_crtc *crtc)
  410. {
  411. struct shmob_drm_device *sdev = crtc->dev->dev_private;
  412. shmob_drm_crtc_enable_vblank(sdev, true);
  413. return 0;
  414. }
  415. static void shmob_drm_disable_vblank(struct drm_crtc *crtc)
  416. {
  417. struct shmob_drm_device *sdev = crtc->dev->dev_private;
  418. shmob_drm_crtc_enable_vblank(sdev, false);
  419. }
  420. static const struct drm_crtc_funcs crtc_funcs = {
  421. .destroy = drm_crtc_cleanup,
  422. .set_config = drm_crtc_helper_set_config,
  423. .page_flip = shmob_drm_crtc_page_flip,
  424. .enable_vblank = shmob_drm_enable_vblank,
  425. .disable_vblank = shmob_drm_disable_vblank,
  426. };
  427. int shmob_drm_crtc_create(struct shmob_drm_device *sdev)
  428. {
  429. struct drm_crtc *crtc = &sdev->crtc.crtc;
  430. int ret;
  431. sdev->crtc.dpms = DRM_MODE_DPMS_OFF;
  432. ret = drm_crtc_init(sdev->ddev, crtc, &crtc_funcs);
  433. if (ret < 0)
  434. return ret;
  435. drm_crtc_helper_add(crtc, &crtc_helper_funcs);
  436. return 0;
  437. }
  438. /* -----------------------------------------------------------------------------
  439. * Encoder
  440. */
  441. #define to_shmob_encoder(e) \
  442. container_of(e, struct shmob_drm_encoder, encoder)
  443. static void shmob_drm_encoder_dpms(struct drm_encoder *encoder, int mode)
  444. {
  445. struct shmob_drm_encoder *senc = to_shmob_encoder(encoder);
  446. struct shmob_drm_device *sdev = encoder->dev->dev_private;
  447. struct shmob_drm_connector *scon = &sdev->connector;
  448. if (senc->dpms == mode)
  449. return;
  450. shmob_drm_backlight_dpms(scon, mode);
  451. senc->dpms = mode;
  452. }
  453. static bool shmob_drm_encoder_mode_fixup(struct drm_encoder *encoder,
  454. const struct drm_display_mode *mode,
  455. struct drm_display_mode *adjusted_mode)
  456. {
  457. struct drm_device *dev = encoder->dev;
  458. struct shmob_drm_device *sdev = dev->dev_private;
  459. struct drm_connector *connector = &sdev->connector.connector;
  460. const struct drm_display_mode *panel_mode;
  461. if (list_empty(&connector->modes)) {
  462. dev_dbg(dev->dev, "mode_fixup: empty modes list\n");
  463. return false;
  464. }
  465. /* The flat panel mode is fixed, just copy it to the adjusted mode. */
  466. panel_mode = list_first_entry(&connector->modes,
  467. struct drm_display_mode, head);
  468. drm_mode_copy(adjusted_mode, panel_mode);
  469. return true;
  470. }
  471. static void shmob_drm_encoder_mode_prepare(struct drm_encoder *encoder)
  472. {
  473. /* No-op, everything is handled in the CRTC code. */
  474. }
  475. static void shmob_drm_encoder_mode_set(struct drm_encoder *encoder,
  476. struct drm_display_mode *mode,
  477. struct drm_display_mode *adjusted_mode)
  478. {
  479. /* No-op, everything is handled in the CRTC code. */
  480. }
  481. static void shmob_drm_encoder_mode_commit(struct drm_encoder *encoder)
  482. {
  483. /* No-op, everything is handled in the CRTC code. */
  484. }
  485. static const struct drm_encoder_helper_funcs encoder_helper_funcs = {
  486. .dpms = shmob_drm_encoder_dpms,
  487. .mode_fixup = shmob_drm_encoder_mode_fixup,
  488. .prepare = shmob_drm_encoder_mode_prepare,
  489. .commit = shmob_drm_encoder_mode_commit,
  490. .mode_set = shmob_drm_encoder_mode_set,
  491. };
  492. static void shmob_drm_encoder_destroy(struct drm_encoder *encoder)
  493. {
  494. drm_encoder_cleanup(encoder);
  495. }
  496. static const struct drm_encoder_funcs encoder_funcs = {
  497. .destroy = shmob_drm_encoder_destroy,
  498. };
  499. int shmob_drm_encoder_create(struct shmob_drm_device *sdev)
  500. {
  501. struct drm_encoder *encoder = &sdev->encoder.encoder;
  502. int ret;
  503. sdev->encoder.dpms = DRM_MODE_DPMS_OFF;
  504. encoder->possible_crtcs = 1;
  505. ret = drm_encoder_init(sdev->ddev, encoder, &encoder_funcs,
  506. DRM_MODE_ENCODER_LVDS, NULL);
  507. if (ret < 0)
  508. return ret;
  509. drm_encoder_helper_add(encoder, &encoder_helper_funcs);
  510. return 0;
  511. }
  512. /* -----------------------------------------------------------------------------
  513. * Connector
  514. */
  515. #define to_shmob_connector(c) \
  516. container_of(c, struct shmob_drm_connector, connector)
  517. static int shmob_drm_connector_get_modes(struct drm_connector *connector)
  518. {
  519. struct shmob_drm_device *sdev = connector->dev->dev_private;
  520. struct drm_display_mode *mode;
  521. mode = drm_mode_create(connector->dev);
  522. if (mode == NULL)
  523. return 0;
  524. mode->type = DRM_MODE_TYPE_PREFERRED | DRM_MODE_TYPE_DRIVER;
  525. mode->clock = sdev->pdata->panel.mode.clock;
  526. mode->hdisplay = sdev->pdata->panel.mode.hdisplay;
  527. mode->hsync_start = sdev->pdata->panel.mode.hsync_start;
  528. mode->hsync_end = sdev->pdata->panel.mode.hsync_end;
  529. mode->htotal = sdev->pdata->panel.mode.htotal;
  530. mode->vdisplay = sdev->pdata->panel.mode.vdisplay;
  531. mode->vsync_start = sdev->pdata->panel.mode.vsync_start;
  532. mode->vsync_end = sdev->pdata->panel.mode.vsync_end;
  533. mode->vtotal = sdev->pdata->panel.mode.vtotal;
  534. mode->flags = sdev->pdata->panel.mode.flags;
  535. drm_mode_set_name(mode);
  536. drm_mode_probed_add(connector, mode);
  537. connector->display_info.width_mm = sdev->pdata->panel.width_mm;
  538. connector->display_info.height_mm = sdev->pdata->panel.height_mm;
  539. return 1;
  540. }
  541. static struct drm_encoder *
  542. shmob_drm_connector_best_encoder(struct drm_connector *connector)
  543. {
  544. struct shmob_drm_connector *scon = to_shmob_connector(connector);
  545. return scon->encoder;
  546. }
  547. static const struct drm_connector_helper_funcs connector_helper_funcs = {
  548. .get_modes = shmob_drm_connector_get_modes,
  549. .best_encoder = shmob_drm_connector_best_encoder,
  550. };
  551. static void shmob_drm_connector_destroy(struct drm_connector *connector)
  552. {
  553. struct shmob_drm_connector *scon = to_shmob_connector(connector);
  554. shmob_drm_backlight_exit(scon);
  555. drm_connector_unregister(connector);
  556. drm_connector_cleanup(connector);
  557. }
  558. static const struct drm_connector_funcs connector_funcs = {
  559. .dpms = drm_helper_connector_dpms,
  560. .fill_modes = drm_helper_probe_single_connector_modes,
  561. .destroy = shmob_drm_connector_destroy,
  562. };
  563. int shmob_drm_connector_create(struct shmob_drm_device *sdev,
  564. struct drm_encoder *encoder)
  565. {
  566. struct drm_connector *connector = &sdev->connector.connector;
  567. int ret;
  568. sdev->connector.encoder = encoder;
  569. connector->display_info.width_mm = sdev->pdata->panel.width_mm;
  570. connector->display_info.height_mm = sdev->pdata->panel.height_mm;
  571. ret = drm_connector_init(sdev->ddev, connector, &connector_funcs,
  572. DRM_MODE_CONNECTOR_LVDS);
  573. if (ret < 0)
  574. return ret;
  575. drm_connector_helper_add(connector, &connector_helper_funcs);
  576. ret = shmob_drm_backlight_init(&sdev->connector);
  577. if (ret < 0)
  578. goto err_cleanup;
  579. ret = drm_mode_connector_attach_encoder(connector, encoder);
  580. if (ret < 0)
  581. goto err_backlight;
  582. drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
  583. drm_object_property_set_value(&connector->base,
  584. sdev->ddev->mode_config.dpms_property, DRM_MODE_DPMS_OFF);
  585. return 0;
  586. err_backlight:
  587. shmob_drm_backlight_exit(&sdev->connector);
  588. err_cleanup:
  589. drm_connector_cleanup(connector);
  590. return ret;
  591. }