ipuv3-crtc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 const 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. }
  236. static int ipu_set_interface_pix_fmt(struct drm_crtc *crtc,
  237. u32 bus_format, int hsync_pin, int vsync_pin)
  238. {
  239. struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
  240. ipu_crtc->bus_format = bus_format;
  241. ipu_crtc->di_hsync_pin = hsync_pin;
  242. ipu_crtc->di_vsync_pin = vsync_pin;
  243. return 0;
  244. }
  245. static const struct imx_drm_crtc_helper_funcs ipu_crtc_helper_funcs = {
  246. .enable_vblank = ipu_enable_vblank,
  247. .disable_vblank = ipu_disable_vblank,
  248. .set_interface_pix_fmt = ipu_set_interface_pix_fmt,
  249. .crtc_funcs = &ipu_crtc_funcs,
  250. .crtc_helper_funcs = &ipu_helper_funcs,
  251. };
  252. static void ipu_put_resources(struct ipu_crtc *ipu_crtc)
  253. {
  254. if (!IS_ERR_OR_NULL(ipu_crtc->dc))
  255. ipu_dc_put(ipu_crtc->dc);
  256. if (!IS_ERR_OR_NULL(ipu_crtc->di))
  257. ipu_di_put(ipu_crtc->di);
  258. }
  259. static int ipu_get_resources(struct ipu_crtc *ipu_crtc,
  260. struct ipu_client_platformdata *pdata)
  261. {
  262. struct ipu_soc *ipu = dev_get_drvdata(ipu_crtc->dev->parent);
  263. int ret;
  264. ipu_crtc->dc = ipu_dc_get(ipu, pdata->dc);
  265. if (IS_ERR(ipu_crtc->dc)) {
  266. ret = PTR_ERR(ipu_crtc->dc);
  267. goto err_out;
  268. }
  269. ipu_crtc->di = ipu_di_get(ipu, pdata->di);
  270. if (IS_ERR(ipu_crtc->di)) {
  271. ret = PTR_ERR(ipu_crtc->di);
  272. goto err_out;
  273. }
  274. return 0;
  275. err_out:
  276. ipu_put_resources(ipu_crtc);
  277. return ret;
  278. }
  279. static int ipu_crtc_init(struct ipu_crtc *ipu_crtc,
  280. struct ipu_client_platformdata *pdata, struct drm_device *drm)
  281. {
  282. struct ipu_soc *ipu = dev_get_drvdata(ipu_crtc->dev->parent);
  283. int dp = -EINVAL;
  284. int ret;
  285. ret = ipu_get_resources(ipu_crtc, pdata);
  286. if (ret) {
  287. dev_err(ipu_crtc->dev, "getting resources failed with %d.\n",
  288. ret);
  289. return ret;
  290. }
  291. if (pdata->dp >= 0)
  292. dp = IPU_DP_FLOW_SYNC_BG;
  293. ipu_crtc->plane[0] = ipu_plane_init(drm, ipu, pdata->dma[0], dp, 0,
  294. DRM_PLANE_TYPE_PRIMARY);
  295. if (IS_ERR(ipu_crtc->plane[0])) {
  296. ret = PTR_ERR(ipu_crtc->plane[0]);
  297. goto err_put_resources;
  298. }
  299. ret = imx_drm_add_crtc(drm, &ipu_crtc->base, &ipu_crtc->imx_crtc,
  300. &ipu_crtc->plane[0]->base, &ipu_crtc_helper_funcs,
  301. ipu_crtc->dev->of_node);
  302. if (ret) {
  303. dev_err(ipu_crtc->dev, "adding crtc failed with %d.\n", ret);
  304. goto err_put_resources;
  305. }
  306. ret = ipu_plane_get_resources(ipu_crtc->plane[0]);
  307. if (ret) {
  308. dev_err(ipu_crtc->dev, "getting plane 0 resources failed with %d.\n",
  309. ret);
  310. goto err_remove_crtc;
  311. }
  312. /* If this crtc is using the DP, add an overlay plane */
  313. if (pdata->dp >= 0 && pdata->dma[1] > 0) {
  314. ipu_crtc->plane[1] = ipu_plane_init(drm, ipu, pdata->dma[1],
  315. IPU_DP_FLOW_SYNC_FG,
  316. drm_crtc_mask(&ipu_crtc->base),
  317. DRM_PLANE_TYPE_OVERLAY);
  318. if (IS_ERR(ipu_crtc->plane[1]))
  319. ipu_crtc->plane[1] = NULL;
  320. }
  321. ipu_crtc->irq = ipu_plane_irq(ipu_crtc->plane[0]);
  322. ret = devm_request_irq(ipu_crtc->dev, ipu_crtc->irq, ipu_irq_handler, 0,
  323. "imx_drm", ipu_crtc);
  324. if (ret < 0) {
  325. dev_err(ipu_crtc->dev, "irq request failed with %d.\n", ret);
  326. goto err_put_plane_res;
  327. }
  328. return 0;
  329. err_put_plane_res:
  330. ipu_plane_put_resources(ipu_crtc->plane[0]);
  331. err_remove_crtc:
  332. imx_drm_remove_crtc(ipu_crtc->imx_crtc);
  333. err_put_resources:
  334. ipu_put_resources(ipu_crtc);
  335. return ret;
  336. }
  337. static int ipu_drm_bind(struct device *dev, struct device *master, void *data)
  338. {
  339. struct ipu_client_platformdata *pdata = dev->platform_data;
  340. struct drm_device *drm = data;
  341. struct ipu_crtc *ipu_crtc;
  342. int ret;
  343. ipu_crtc = devm_kzalloc(dev, sizeof(*ipu_crtc), GFP_KERNEL);
  344. if (!ipu_crtc)
  345. return -ENOMEM;
  346. ipu_crtc->dev = dev;
  347. ret = ipu_crtc_init(ipu_crtc, pdata, drm);
  348. if (ret)
  349. return ret;
  350. dev_set_drvdata(dev, ipu_crtc);
  351. return 0;
  352. }
  353. static void ipu_drm_unbind(struct device *dev, struct device *master,
  354. void *data)
  355. {
  356. struct ipu_crtc *ipu_crtc = dev_get_drvdata(dev);
  357. imx_drm_remove_crtc(ipu_crtc->imx_crtc);
  358. ipu_plane_put_resources(ipu_crtc->plane[0]);
  359. ipu_put_resources(ipu_crtc);
  360. }
  361. static const struct component_ops ipu_crtc_ops = {
  362. .bind = ipu_drm_bind,
  363. .unbind = ipu_drm_unbind,
  364. };
  365. static int ipu_drm_probe(struct platform_device *pdev)
  366. {
  367. struct device *dev = &pdev->dev;
  368. int ret;
  369. if (!dev->platform_data)
  370. return -EINVAL;
  371. ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
  372. if (ret)
  373. return ret;
  374. return component_add(dev, &ipu_crtc_ops);
  375. }
  376. static int ipu_drm_remove(struct platform_device *pdev)
  377. {
  378. component_del(&pdev->dev, &ipu_crtc_ops);
  379. return 0;
  380. }
  381. static struct platform_driver ipu_drm_driver = {
  382. .driver = {
  383. .name = "imx-ipuv3-crtc",
  384. },
  385. .probe = ipu_drm_probe,
  386. .remove = ipu_drm_remove,
  387. };
  388. module_platform_driver(ipu_drm_driver);
  389. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
  390. MODULE_DESCRIPTION(DRIVER_DESC);
  391. MODULE_LICENSE("GPL");
  392. MODULE_ALIAS("platform:imx-ipuv3-crtc");