ipuv3-crtc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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 interface_pix_fmt;
  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. u32 out_pixel_fmt;
  124. int ret;
  125. dev_dbg(ipu_crtc->dev, "%s: mode->hdisplay: %d\n", __func__,
  126. mode->hdisplay);
  127. dev_dbg(ipu_crtc->dev, "%s: mode->vdisplay: %d\n", __func__,
  128. mode->vdisplay);
  129. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  130. if (encoder->crtc == crtc)
  131. encoder_types |= BIT(encoder->encoder_type);
  132. dev_dbg(ipu_crtc->dev, "%s: attached to encoder types 0x%lx\n",
  133. __func__, encoder_types);
  134. /*
  135. * If we have DAC, TVDAC or LDB, then we need the IPU DI clock
  136. * to be the same as the LDB DI clock.
  137. */
  138. if (encoder_types & (BIT(DRM_MODE_ENCODER_DAC) |
  139. BIT(DRM_MODE_ENCODER_TVDAC) |
  140. BIT(DRM_MODE_ENCODER_LVDS)))
  141. sig_cfg.clkflags = IPU_DI_CLKMODE_SYNC | IPU_DI_CLKMODE_EXT;
  142. else
  143. sig_cfg.clkflags = 0;
  144. out_pixel_fmt = ipu_crtc->interface_pix_fmt;
  145. sig_cfg.enable_pol = 1;
  146. sig_cfg.clk_pol = 0;
  147. sig_cfg.pixel_fmt = out_pixel_fmt;
  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. out_pixel_fmt, 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. }
  172. static void ipu_crtc_handle_pageflip(struct ipu_crtc *ipu_crtc)
  173. {
  174. unsigned long flags;
  175. struct drm_device *drm = ipu_crtc->base.dev;
  176. spin_lock_irqsave(&drm->event_lock, flags);
  177. if (ipu_crtc->page_flip_event)
  178. drm_send_vblank_event(drm, -1, ipu_crtc->page_flip_event);
  179. ipu_crtc->page_flip_event = NULL;
  180. imx_drm_crtc_vblank_put(ipu_crtc->imx_crtc);
  181. spin_unlock_irqrestore(&drm->event_lock, flags);
  182. }
  183. static irqreturn_t ipu_irq_handler(int irq, void *dev_id)
  184. {
  185. struct ipu_crtc *ipu_crtc = dev_id;
  186. imx_drm_handle_vblank(ipu_crtc->imx_crtc);
  187. if (ipu_crtc->newfb) {
  188. struct ipu_plane *plane = ipu_crtc->plane[0];
  189. ipu_crtc->newfb = NULL;
  190. ipu_plane_set_base(plane, ipu_crtc->base.primary->fb,
  191. plane->x, plane->y);
  192. ipu_crtc_handle_pageflip(ipu_crtc);
  193. }
  194. return IRQ_HANDLED;
  195. }
  196. static bool ipu_crtc_mode_fixup(struct drm_crtc *crtc,
  197. const struct drm_display_mode *mode,
  198. struct drm_display_mode *adjusted_mode)
  199. {
  200. struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
  201. struct videomode vm;
  202. int ret;
  203. drm_display_mode_to_videomode(adjusted_mode, &vm);
  204. ret = ipu_di_adjust_videomode(ipu_crtc->di, &vm);
  205. if (ret)
  206. return false;
  207. drm_display_mode_from_videomode(&vm, adjusted_mode);
  208. return true;
  209. }
  210. static void ipu_crtc_prepare(struct drm_crtc *crtc)
  211. {
  212. struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
  213. ipu_fb_disable(ipu_crtc);
  214. }
  215. static void ipu_crtc_commit(struct drm_crtc *crtc)
  216. {
  217. struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
  218. ipu_fb_enable(ipu_crtc);
  219. }
  220. static struct drm_crtc_helper_funcs ipu_helper_funcs = {
  221. .dpms = ipu_crtc_dpms,
  222. .mode_fixup = ipu_crtc_mode_fixup,
  223. .mode_set = ipu_crtc_mode_set,
  224. .prepare = ipu_crtc_prepare,
  225. .commit = ipu_crtc_commit,
  226. };
  227. static int ipu_enable_vblank(struct drm_crtc *crtc)
  228. {
  229. return 0;
  230. }
  231. static void ipu_disable_vblank(struct drm_crtc *crtc)
  232. {
  233. struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
  234. ipu_crtc->page_flip_event = NULL;
  235. ipu_crtc->newfb = NULL;
  236. }
  237. static int ipu_set_interface_pix_fmt(struct drm_crtc *crtc,
  238. u32 pixfmt, int hsync_pin, int vsync_pin)
  239. {
  240. struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
  241. ipu_crtc->interface_pix_fmt = pixfmt;
  242. ipu_crtc->di_hsync_pin = hsync_pin;
  243. ipu_crtc->di_vsync_pin = vsync_pin;
  244. return 0;
  245. }
  246. static const struct imx_drm_crtc_helper_funcs ipu_crtc_helper_funcs = {
  247. .enable_vblank = ipu_enable_vblank,
  248. .disable_vblank = ipu_disable_vblank,
  249. .set_interface_pix_fmt = ipu_set_interface_pix_fmt,
  250. .crtc_funcs = &ipu_crtc_funcs,
  251. .crtc_helper_funcs = &ipu_helper_funcs,
  252. };
  253. static void ipu_put_resources(struct ipu_crtc *ipu_crtc)
  254. {
  255. if (!IS_ERR_OR_NULL(ipu_crtc->dc))
  256. ipu_dc_put(ipu_crtc->dc);
  257. if (!IS_ERR_OR_NULL(ipu_crtc->di))
  258. ipu_di_put(ipu_crtc->di);
  259. }
  260. static int ipu_get_resources(struct ipu_crtc *ipu_crtc,
  261. struct ipu_client_platformdata *pdata)
  262. {
  263. struct ipu_soc *ipu = dev_get_drvdata(ipu_crtc->dev->parent);
  264. int ret;
  265. ipu_crtc->dc = ipu_dc_get(ipu, pdata->dc);
  266. if (IS_ERR(ipu_crtc->dc)) {
  267. ret = PTR_ERR(ipu_crtc->dc);
  268. goto err_out;
  269. }
  270. ipu_crtc->di = ipu_di_get(ipu, pdata->di);
  271. if (IS_ERR(ipu_crtc->di)) {
  272. ret = PTR_ERR(ipu_crtc->di);
  273. goto err_out;
  274. }
  275. return 0;
  276. err_out:
  277. ipu_put_resources(ipu_crtc);
  278. return ret;
  279. }
  280. static int ipu_crtc_init(struct ipu_crtc *ipu_crtc,
  281. struct ipu_client_platformdata *pdata, struct drm_device *drm)
  282. {
  283. struct ipu_soc *ipu = dev_get_drvdata(ipu_crtc->dev->parent);
  284. int dp = -EINVAL;
  285. int ret;
  286. int id;
  287. ret = ipu_get_resources(ipu_crtc, pdata);
  288. if (ret) {
  289. dev_err(ipu_crtc->dev, "getting resources failed with %d.\n",
  290. ret);
  291. return ret;
  292. }
  293. ret = imx_drm_add_crtc(drm, &ipu_crtc->base, &ipu_crtc->imx_crtc,
  294. &ipu_crtc_helper_funcs, ipu_crtc->dev->of_node);
  295. if (ret) {
  296. dev_err(ipu_crtc->dev, "adding crtc failed with %d.\n", ret);
  297. goto err_put_resources;
  298. }
  299. if (pdata->dp >= 0)
  300. dp = IPU_DP_FLOW_SYNC_BG;
  301. id = imx_drm_crtc_id(ipu_crtc->imx_crtc);
  302. ipu_crtc->plane[0] = ipu_plane_init(ipu_crtc->base.dev, ipu,
  303. pdata->dma[0], dp, BIT(id), true);
  304. ret = ipu_plane_get_resources(ipu_crtc->plane[0]);
  305. if (ret) {
  306. dev_err(ipu_crtc->dev, "getting plane 0 resources failed with %d.\n",
  307. ret);
  308. goto err_remove_crtc;
  309. }
  310. /* If this crtc is using the DP, add an overlay plane */
  311. if (pdata->dp >= 0 && pdata->dma[1] > 0) {
  312. ipu_crtc->plane[1] = ipu_plane_init(ipu_crtc->base.dev, ipu,
  313. pdata->dma[1],
  314. IPU_DP_FLOW_SYNC_FG,
  315. BIT(id), false);
  316. if (IS_ERR(ipu_crtc->plane[1]))
  317. ipu_crtc->plane[1] = NULL;
  318. }
  319. ipu_crtc->irq = ipu_plane_irq(ipu_crtc->plane[0]);
  320. ret = devm_request_irq(ipu_crtc->dev, ipu_crtc->irq, ipu_irq_handler, 0,
  321. "imx_drm", ipu_crtc);
  322. if (ret < 0) {
  323. dev_err(ipu_crtc->dev, "irq request failed with %d.\n", ret);
  324. goto err_put_plane_res;
  325. }
  326. return 0;
  327. err_put_plane_res:
  328. ipu_plane_put_resources(ipu_crtc->plane[0]);
  329. err_remove_crtc:
  330. imx_drm_remove_crtc(ipu_crtc->imx_crtc);
  331. err_put_resources:
  332. ipu_put_resources(ipu_crtc);
  333. return ret;
  334. }
  335. static struct device_node *ipu_drm_get_port_by_id(struct device_node *parent,
  336. int port_id)
  337. {
  338. struct device_node *port;
  339. int id, ret;
  340. port = of_get_child_by_name(parent, "port");
  341. while (port) {
  342. ret = of_property_read_u32(port, "reg", &id);
  343. if (!ret && id == port_id)
  344. return port;
  345. do {
  346. port = of_get_next_child(parent, port);
  347. if (!port)
  348. return NULL;
  349. } while (of_node_cmp(port->name, "port"));
  350. }
  351. return NULL;
  352. }
  353. static int ipu_drm_bind(struct device *dev, struct device *master, void *data)
  354. {
  355. struct ipu_client_platformdata *pdata = dev->platform_data;
  356. struct drm_device *drm = data;
  357. struct ipu_crtc *ipu_crtc;
  358. int ret;
  359. ipu_crtc = devm_kzalloc(dev, sizeof(*ipu_crtc), GFP_KERNEL);
  360. if (!ipu_crtc)
  361. return -ENOMEM;
  362. ipu_crtc->dev = dev;
  363. ret = ipu_crtc_init(ipu_crtc, pdata, drm);
  364. if (ret)
  365. return ret;
  366. dev_set_drvdata(dev, ipu_crtc);
  367. return 0;
  368. }
  369. static void ipu_drm_unbind(struct device *dev, struct device *master,
  370. void *data)
  371. {
  372. struct ipu_crtc *ipu_crtc = dev_get_drvdata(dev);
  373. imx_drm_remove_crtc(ipu_crtc->imx_crtc);
  374. ipu_plane_put_resources(ipu_crtc->plane[0]);
  375. ipu_put_resources(ipu_crtc);
  376. }
  377. static const struct component_ops ipu_crtc_ops = {
  378. .bind = ipu_drm_bind,
  379. .unbind = ipu_drm_unbind,
  380. };
  381. static int ipu_drm_probe(struct platform_device *pdev)
  382. {
  383. struct device *dev = &pdev->dev;
  384. struct ipu_client_platformdata *pdata = dev->platform_data;
  385. int ret;
  386. if (!dev->platform_data)
  387. return -EINVAL;
  388. if (!dev->of_node) {
  389. /* Associate crtc device with the corresponding DI port node */
  390. dev->of_node = ipu_drm_get_port_by_id(dev->parent->of_node,
  391. pdata->di + 2);
  392. if (!dev->of_node) {
  393. dev_err(dev, "missing port@%d node in %s\n",
  394. pdata->di + 2, dev->parent->of_node->full_name);
  395. return -ENODEV;
  396. }
  397. }
  398. ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
  399. if (ret)
  400. return ret;
  401. return component_add(dev, &ipu_crtc_ops);
  402. }
  403. static int ipu_drm_remove(struct platform_device *pdev)
  404. {
  405. component_del(&pdev->dev, &ipu_crtc_ops);
  406. return 0;
  407. }
  408. static struct platform_driver ipu_drm_driver = {
  409. .driver = {
  410. .name = "imx-ipuv3-crtc",
  411. },
  412. .probe = ipu_drm_probe,
  413. .remove = ipu_drm_remove,
  414. };
  415. module_platform_driver(ipu_drm_driver);
  416. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
  417. MODULE_DESCRIPTION(DRIVER_DESC);
  418. MODULE_LICENSE("GPL");
  419. MODULE_ALIAS("platform:imx-ipuv3-crtc");