ipuv3-crtc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /*
  2. * i.MX IPUv3 Graphics driver
  3. *
  4. * Copyright (C) 2011 Sascha Hauer, Pengutronix
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/component.h>
  16. #include <linux/module.h>
  17. #include <linux/export.h>
  18. #include <linux/device.h>
  19. #include <linux/platform_device.h>
  20. #include <drm/drmP.h>
  21. #include <drm/drm_crtc_helper.h>
  22. #include <linux/fb.h>
  23. #include <linux/clk.h>
  24. #include <linux/errno.h>
  25. #include <drm/drm_gem_cma_helper.h>
  26. #include <drm/drm_fb_cma_helper.h>
  27. #include <video/imx-ipu-v3.h>
  28. #include "imx-drm.h"
  29. #include "ipuv3-plane.h"
  30. #define DRIVER_DESC "i.MX IPUv3 Graphics"
  31. struct ipu_crtc {
  32. struct device *dev;
  33. struct drm_crtc base;
  34. struct imx_drm_crtc *imx_crtc;
  35. /* plane[0] is the full plane, plane[1] is the partial plane */
  36. struct ipu_plane *plane[2];
  37. struct ipu_dc *dc;
  38. struct ipu_di *di;
  39. int enabled;
  40. struct drm_pending_vblank_event *page_flip_event;
  41. struct drm_framebuffer *newfb;
  42. int irq;
  43. u32 bus_format;
  44. int di_hsync_pin;
  45. int di_vsync_pin;
  46. };
  47. #define to_ipu_crtc(x) container_of(x, struct ipu_crtc, base)
  48. static void ipu_fb_enable(struct ipu_crtc *ipu_crtc)
  49. {
  50. struct ipu_soc *ipu = dev_get_drvdata(ipu_crtc->dev->parent);
  51. if (ipu_crtc->enabled)
  52. return;
  53. ipu_dc_enable(ipu);
  54. ipu_plane_enable(ipu_crtc->plane[0]);
  55. /* Start DC channel and DI after IDMAC */
  56. ipu_dc_enable_channel(ipu_crtc->dc);
  57. ipu_di_enable(ipu_crtc->di);
  58. ipu_crtc->enabled = 1;
  59. }
  60. static void ipu_fb_disable(struct ipu_crtc *ipu_crtc)
  61. {
  62. struct ipu_soc *ipu = dev_get_drvdata(ipu_crtc->dev->parent);
  63. if (!ipu_crtc->enabled)
  64. return;
  65. /* Stop DC channel and DI before IDMAC */
  66. ipu_dc_disable_channel(ipu_crtc->dc);
  67. ipu_di_disable(ipu_crtc->di);
  68. ipu_plane_disable(ipu_crtc->plane[0]);
  69. ipu_dc_disable(ipu);
  70. ipu_crtc->enabled = 0;
  71. }
  72. static void ipu_crtc_dpms(struct drm_crtc *crtc, int mode)
  73. {
  74. struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
  75. dev_dbg(ipu_crtc->dev, "%s mode: %d\n", __func__, mode);
  76. switch (mode) {
  77. case DRM_MODE_DPMS_ON:
  78. ipu_fb_enable(ipu_crtc);
  79. break;
  80. case DRM_MODE_DPMS_STANDBY:
  81. case DRM_MODE_DPMS_SUSPEND:
  82. case DRM_MODE_DPMS_OFF:
  83. ipu_fb_disable(ipu_crtc);
  84. break;
  85. }
  86. }
  87. static int ipu_page_flip(struct drm_crtc *crtc,
  88. struct drm_framebuffer *fb,
  89. struct drm_pending_vblank_event *event,
  90. uint32_t page_flip_flags)
  91. {
  92. struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
  93. int ret;
  94. if (ipu_crtc->newfb)
  95. return -EBUSY;
  96. ret = imx_drm_crtc_vblank_get(ipu_crtc->imx_crtc);
  97. if (ret) {
  98. dev_dbg(ipu_crtc->dev, "failed to acquire vblank counter\n");
  99. list_del(&event->base.link);
  100. return ret;
  101. }
  102. ipu_crtc->newfb = fb;
  103. ipu_crtc->page_flip_event = event;
  104. crtc->primary->fb = fb;
  105. return 0;
  106. }
  107. static const struct drm_crtc_funcs ipu_crtc_funcs = {
  108. .set_config = drm_crtc_helper_set_config,
  109. .destroy = drm_crtc_cleanup,
  110. .page_flip = ipu_page_flip,
  111. };
  112. static int ipu_crtc_mode_set(struct drm_crtc *crtc,
  113. struct drm_display_mode *orig_mode,
  114. struct drm_display_mode *mode,
  115. int x, int y,
  116. struct drm_framebuffer *old_fb)
  117. {
  118. struct drm_device *dev = crtc->dev;
  119. struct drm_encoder *encoder;
  120. struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
  121. struct ipu_di_signal_cfg sig_cfg = {};
  122. unsigned long encoder_types = 0;
  123. int ret;
  124. dev_dbg(ipu_crtc->dev, "%s: mode->hdisplay: %d\n", __func__,
  125. mode->hdisplay);
  126. dev_dbg(ipu_crtc->dev, "%s: mode->vdisplay: %d\n", __func__,
  127. mode->vdisplay);
  128. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  129. if (encoder->crtc == crtc)
  130. encoder_types |= BIT(encoder->encoder_type);
  131. dev_dbg(ipu_crtc->dev, "%s: attached to encoder types 0x%lx\n",
  132. __func__, encoder_types);
  133. /*
  134. * If we have DAC or LDB, then we need the IPU DI clock to be
  135. * the same as the LDB DI clock. For TVDAC, derive the IPU DI
  136. * clock from 27 MHz TVE_DI clock, but allow to divide it.
  137. */
  138. if (encoder_types & (BIT(DRM_MODE_ENCODER_DAC) |
  139. BIT(DRM_MODE_ENCODER_LVDS)))
  140. sig_cfg.clkflags = IPU_DI_CLKMODE_SYNC | IPU_DI_CLKMODE_EXT;
  141. else if (encoder_types & BIT(DRM_MODE_ENCODER_TVDAC))
  142. sig_cfg.clkflags = IPU_DI_CLKMODE_EXT;
  143. else
  144. sig_cfg.clkflags = 0;
  145. sig_cfg.enable_pol = 1;
  146. sig_cfg.clk_pol = 0;
  147. sig_cfg.bus_format = ipu_crtc->bus_format;
  148. sig_cfg.v_to_h_sync = 0;
  149. sig_cfg.hsync_pin = ipu_crtc->di_hsync_pin;
  150. sig_cfg.vsync_pin = ipu_crtc->di_vsync_pin;
  151. drm_display_mode_to_videomode(mode, &sig_cfg.mode);
  152. ret = ipu_dc_init_sync(ipu_crtc->dc, ipu_crtc->di,
  153. mode->flags & DRM_MODE_FLAG_INTERLACE,
  154. ipu_crtc->bus_format, mode->hdisplay);
  155. if (ret) {
  156. dev_err(ipu_crtc->dev,
  157. "initializing display controller failed with %d\n",
  158. ret);
  159. return ret;
  160. }
  161. ret = ipu_di_init_sync_panel(ipu_crtc->di, &sig_cfg);
  162. if (ret) {
  163. dev_err(ipu_crtc->dev,
  164. "initializing panel failed with %d\n", ret);
  165. return ret;
  166. }
  167. return ipu_plane_mode_set(ipu_crtc->plane[0], crtc, mode,
  168. crtc->primary->fb,
  169. 0, 0, mode->hdisplay, mode->vdisplay,
  170. x, y, mode->hdisplay, mode->vdisplay,
  171. mode->flags & DRM_MODE_FLAG_INTERLACE);
  172. }
  173. static void ipu_crtc_handle_pageflip(struct ipu_crtc *ipu_crtc)
  174. {
  175. unsigned long flags;
  176. struct drm_device *drm = ipu_crtc->base.dev;
  177. spin_lock_irqsave(&drm->event_lock, flags);
  178. if (ipu_crtc->page_flip_event)
  179. drm_crtc_send_vblank_event(&ipu_crtc->base,
  180. ipu_crtc->page_flip_event);
  181. ipu_crtc->page_flip_event = NULL;
  182. imx_drm_crtc_vblank_put(ipu_crtc->imx_crtc);
  183. spin_unlock_irqrestore(&drm->event_lock, flags);
  184. }
  185. static irqreturn_t ipu_irq_handler(int irq, void *dev_id)
  186. {
  187. struct ipu_crtc *ipu_crtc = dev_id;
  188. imx_drm_handle_vblank(ipu_crtc->imx_crtc);
  189. if (ipu_crtc->newfb) {
  190. struct ipu_plane *plane = ipu_crtc->plane[0];
  191. ipu_crtc->newfb = NULL;
  192. ipu_plane_set_base(plane, ipu_crtc->base.primary->fb,
  193. plane->x, plane->y);
  194. ipu_crtc_handle_pageflip(ipu_crtc);
  195. }
  196. return IRQ_HANDLED;
  197. }
  198. static bool ipu_crtc_mode_fixup(struct drm_crtc *crtc,
  199. const struct drm_display_mode *mode,
  200. struct drm_display_mode *adjusted_mode)
  201. {
  202. struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
  203. struct videomode vm;
  204. int ret;
  205. drm_display_mode_to_videomode(adjusted_mode, &vm);
  206. ret = ipu_di_adjust_videomode(ipu_crtc->di, &vm);
  207. if (ret)
  208. return false;
  209. drm_display_mode_from_videomode(&vm, adjusted_mode);
  210. return true;
  211. }
  212. static void ipu_crtc_prepare(struct drm_crtc *crtc)
  213. {
  214. struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
  215. ipu_fb_disable(ipu_crtc);
  216. }
  217. static void ipu_crtc_commit(struct drm_crtc *crtc)
  218. {
  219. struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
  220. ipu_fb_enable(ipu_crtc);
  221. }
  222. static struct drm_crtc_helper_funcs ipu_helper_funcs = {
  223. .dpms = ipu_crtc_dpms,
  224. .mode_fixup = ipu_crtc_mode_fixup,
  225. .mode_set = ipu_crtc_mode_set,
  226. .prepare = ipu_crtc_prepare,
  227. .commit = ipu_crtc_commit,
  228. };
  229. static int ipu_enable_vblank(struct drm_crtc *crtc)
  230. {
  231. return 0;
  232. }
  233. static void ipu_disable_vblank(struct drm_crtc *crtc)
  234. {
  235. struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
  236. ipu_crtc->page_flip_event = NULL;
  237. ipu_crtc->newfb = NULL;
  238. }
  239. static int ipu_set_interface_pix_fmt(struct drm_crtc *crtc,
  240. u32 bus_format, int hsync_pin, int vsync_pin)
  241. {
  242. struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
  243. ipu_crtc->bus_format = bus_format;
  244. ipu_crtc->di_hsync_pin = hsync_pin;
  245. ipu_crtc->di_vsync_pin = vsync_pin;
  246. return 0;
  247. }
  248. static const struct imx_drm_crtc_helper_funcs ipu_crtc_helper_funcs = {
  249. .enable_vblank = ipu_enable_vblank,
  250. .disable_vblank = ipu_disable_vblank,
  251. .set_interface_pix_fmt = ipu_set_interface_pix_fmt,
  252. .crtc_funcs = &ipu_crtc_funcs,
  253. .crtc_helper_funcs = &ipu_helper_funcs,
  254. };
  255. static void ipu_put_resources(struct ipu_crtc *ipu_crtc)
  256. {
  257. if (!IS_ERR_OR_NULL(ipu_crtc->dc))
  258. ipu_dc_put(ipu_crtc->dc);
  259. if (!IS_ERR_OR_NULL(ipu_crtc->di))
  260. ipu_di_put(ipu_crtc->di);
  261. }
  262. static int ipu_get_resources(struct ipu_crtc *ipu_crtc,
  263. struct ipu_client_platformdata *pdata)
  264. {
  265. struct ipu_soc *ipu = dev_get_drvdata(ipu_crtc->dev->parent);
  266. int ret;
  267. ipu_crtc->dc = ipu_dc_get(ipu, pdata->dc);
  268. if (IS_ERR(ipu_crtc->dc)) {
  269. ret = PTR_ERR(ipu_crtc->dc);
  270. goto err_out;
  271. }
  272. ipu_crtc->di = ipu_di_get(ipu, pdata->di);
  273. if (IS_ERR(ipu_crtc->di)) {
  274. ret = PTR_ERR(ipu_crtc->di);
  275. goto err_out;
  276. }
  277. return 0;
  278. err_out:
  279. ipu_put_resources(ipu_crtc);
  280. return ret;
  281. }
  282. static int ipu_crtc_init(struct ipu_crtc *ipu_crtc,
  283. struct ipu_client_platformdata *pdata, struct drm_device *drm)
  284. {
  285. struct ipu_soc *ipu = dev_get_drvdata(ipu_crtc->dev->parent);
  286. int dp = -EINVAL;
  287. int ret;
  288. ret = ipu_get_resources(ipu_crtc, pdata);
  289. if (ret) {
  290. dev_err(ipu_crtc->dev, "getting resources failed with %d.\n",
  291. ret);
  292. return ret;
  293. }
  294. if (pdata->dp >= 0)
  295. dp = IPU_DP_FLOW_SYNC_BG;
  296. ipu_crtc->plane[0] = ipu_plane_init(drm, ipu, pdata->dma[0], dp, 0,
  297. DRM_PLANE_TYPE_PRIMARY);
  298. if (IS_ERR(ipu_crtc->plane[0])) {
  299. ret = PTR_ERR(ipu_crtc->plane[0]);
  300. goto err_put_resources;
  301. }
  302. ret = imx_drm_add_crtc(drm, &ipu_crtc->base, &ipu_crtc->imx_crtc,
  303. &ipu_crtc->plane[0]->base, &ipu_crtc_helper_funcs,
  304. pdata->of_node);
  305. if (ret) {
  306. dev_err(ipu_crtc->dev, "adding crtc failed with %d.\n", ret);
  307. goto err_put_resources;
  308. }
  309. ret = ipu_plane_get_resources(ipu_crtc->plane[0]);
  310. if (ret) {
  311. dev_err(ipu_crtc->dev, "getting plane 0 resources failed with %d.\n",
  312. ret);
  313. goto err_remove_crtc;
  314. }
  315. /* If this crtc is using the DP, add an overlay plane */
  316. if (pdata->dp >= 0 && pdata->dma[1] > 0) {
  317. ipu_crtc->plane[1] = ipu_plane_init(drm, ipu, pdata->dma[1],
  318. IPU_DP_FLOW_SYNC_FG,
  319. drm_crtc_mask(&ipu_crtc->base),
  320. DRM_PLANE_TYPE_OVERLAY);
  321. if (IS_ERR(ipu_crtc->plane[1]))
  322. ipu_crtc->plane[1] = NULL;
  323. }
  324. ipu_crtc->irq = ipu_plane_irq(ipu_crtc->plane[0]);
  325. ret = devm_request_irq(ipu_crtc->dev, ipu_crtc->irq, ipu_irq_handler, 0,
  326. "imx_drm", ipu_crtc);
  327. if (ret < 0) {
  328. dev_err(ipu_crtc->dev, "irq request failed with %d.\n", ret);
  329. goto err_put_plane_res;
  330. }
  331. return 0;
  332. err_put_plane_res:
  333. ipu_plane_put_resources(ipu_crtc->plane[0]);
  334. err_remove_crtc:
  335. imx_drm_remove_crtc(ipu_crtc->imx_crtc);
  336. err_put_resources:
  337. ipu_put_resources(ipu_crtc);
  338. return ret;
  339. }
  340. static int ipu_drm_bind(struct device *dev, struct device *master, void *data)
  341. {
  342. struct ipu_client_platformdata *pdata = dev->platform_data;
  343. struct drm_device *drm = data;
  344. struct ipu_crtc *ipu_crtc;
  345. int ret;
  346. ipu_crtc = devm_kzalloc(dev, sizeof(*ipu_crtc), GFP_KERNEL);
  347. if (!ipu_crtc)
  348. return -ENOMEM;
  349. ipu_crtc->dev = dev;
  350. ret = ipu_crtc_init(ipu_crtc, pdata, drm);
  351. if (ret)
  352. return ret;
  353. dev_set_drvdata(dev, ipu_crtc);
  354. return 0;
  355. }
  356. static void ipu_drm_unbind(struct device *dev, struct device *master,
  357. void *data)
  358. {
  359. struct ipu_crtc *ipu_crtc = dev_get_drvdata(dev);
  360. imx_drm_remove_crtc(ipu_crtc->imx_crtc);
  361. ipu_plane_put_resources(ipu_crtc->plane[0]);
  362. ipu_put_resources(ipu_crtc);
  363. }
  364. static const struct component_ops ipu_crtc_ops = {
  365. .bind = ipu_drm_bind,
  366. .unbind = ipu_drm_unbind,
  367. };
  368. static int ipu_drm_probe(struct platform_device *pdev)
  369. {
  370. struct device *dev = &pdev->dev;
  371. int ret;
  372. if (!dev->platform_data)
  373. return -EINVAL;
  374. ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
  375. if (ret)
  376. return ret;
  377. return component_add(dev, &ipu_crtc_ops);
  378. }
  379. static int ipu_drm_remove(struct platform_device *pdev)
  380. {
  381. component_del(&pdev->dev, &ipu_crtc_ops);
  382. return 0;
  383. }
  384. static struct platform_driver ipu_drm_driver = {
  385. .driver = {
  386. .name = "imx-ipuv3-crtc",
  387. },
  388. .probe = ipu_drm_probe,
  389. .remove = ipu_drm_remove,
  390. };
  391. module_platform_driver(ipu_drm_driver);
  392. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
  393. MODULE_DESCRIPTION(DRIVER_DESC);
  394. MODULE_LICENSE("GPL");
  395. MODULE_ALIAS("platform:imx-ipuv3-crtc");