blob: bae0e2e81916def0ac5e7b9c0cb2454c2e9e0c4d [file] [log] [blame]
Sascha Hauerf326f792012-09-21 10:07:50 +02001/*
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 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 * MA 02110-1301, USA.
19 */
20#include <linux/module.h>
21#include <linux/export.h>
22#include <linux/device.h>
23#include <linux/platform_device.h>
24#include <drm/drmP.h>
Sascha Hauerf326f792012-09-21 10:07:50 +020025#include <drm/drm_crtc_helper.h>
26#include <linux/fb.h>
27#include <linux/clk.h>
28#include <drm/drm_gem_cma_helper.h>
29#include <drm/drm_fb_cma_helper.h>
30
31#include "ipu-v3/imx-ipu-v3.h"
32#include "imx-drm.h"
33
34#define DRIVER_DESC "i.MX IPUv3 Graphics"
35
36struct ipu_framebuffer {
37 struct drm_framebuffer base;
38 void *virt;
39 dma_addr_t phys;
40 size_t len;
41};
42
43struct ipu_crtc {
Sascha Hauerf326f792012-09-21 10:07:50 +020044 struct device *dev;
45 struct drm_crtc base;
46 struct imx_drm_crtc *imx_crtc;
47 struct ipuv3_channel *ipu_ch;
48 struct ipu_dc *dc;
49 struct ipu_dp *dp;
50 struct dmfc_channel *dmfc;
51 struct ipu_di *di;
52 int enabled;
Sascha Hauerf326f792012-09-21 10:07:50 +020053 struct drm_pending_vblank_event *page_flip_event;
54 struct drm_framebuffer *newfb;
55 int irq;
56 u32 interface_pix_fmt;
57 unsigned long di_clkflags;
Philipp Zabel2ea42602013-04-08 18:04:35 +020058 int di_hsync_pin;
59 int di_vsync_pin;
Sascha Hauerf326f792012-09-21 10:07:50 +020060};
61
62#define to_ipu_crtc(x) container_of(x, struct ipu_crtc, base)
63
64static int calc_vref(struct drm_display_mode *mode)
65{
66 unsigned long htotal, vtotal;
67
68 htotal = mode->htotal;
69 vtotal = mode->vtotal;
70
71 if (!htotal || !vtotal)
72 return 60;
73
74 return mode->clock * 1000 / vtotal / htotal;
75}
76
77static int calc_bandwidth(struct drm_display_mode *mode, unsigned int vref)
78{
79 return mode->hdisplay * mode->vdisplay * vref;
80}
81
82static void ipu_fb_enable(struct ipu_crtc *ipu_crtc)
83{
84 if (ipu_crtc->enabled)
85 return;
86
87 ipu_di_enable(ipu_crtc->di);
88 ipu_dmfc_enable_channel(ipu_crtc->dmfc);
89 ipu_idmac_enable_channel(ipu_crtc->ipu_ch);
90 ipu_dc_enable_channel(ipu_crtc->dc);
91 if (ipu_crtc->dp)
92 ipu_dp_enable_channel(ipu_crtc->dp);
93
94 ipu_crtc->enabled = 1;
95}
96
97static void ipu_fb_disable(struct ipu_crtc *ipu_crtc)
98{
99 if (!ipu_crtc->enabled)
100 return;
101
102 if (ipu_crtc->dp)
103 ipu_dp_disable_channel(ipu_crtc->dp);
104 ipu_dc_disable_channel(ipu_crtc->dc);
105 ipu_idmac_disable_channel(ipu_crtc->ipu_ch);
106 ipu_dmfc_disable_channel(ipu_crtc->dmfc);
107 ipu_di_disable(ipu_crtc->di);
108
109 ipu_crtc->enabled = 0;
110}
111
112static void ipu_crtc_dpms(struct drm_crtc *crtc, int mode)
113{
114 struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
115
Philipp Zabela8e4e232012-11-12 16:29:01 +0100116 dev_dbg(ipu_crtc->dev, "%s mode: %d\n", __func__, mode);
Sascha Hauerf326f792012-09-21 10:07:50 +0200117
118 switch (mode) {
119 case DRM_MODE_DPMS_ON:
120 ipu_fb_enable(ipu_crtc);
121 break;
122 case DRM_MODE_DPMS_STANDBY:
123 case DRM_MODE_DPMS_SUSPEND:
124 case DRM_MODE_DPMS_OFF:
125 ipu_fb_disable(ipu_crtc);
126 break;
127 }
128}
129
130static int ipu_page_flip(struct drm_crtc *crtc,
131 struct drm_framebuffer *fb,
132 struct drm_pending_vblank_event *event)
133{
134 struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
135 int ret;
136
137 if (ipu_crtc->newfb)
138 return -EBUSY;
139
140 ret = imx_drm_crtc_vblank_get(ipu_crtc->imx_crtc);
141 if (ret) {
142 dev_dbg(ipu_crtc->dev, "failed to acquire vblank counter\n");
143 list_del(&event->base.link);
144
145 return ret;
146 }
147
148 ipu_crtc->newfb = fb;
149 ipu_crtc->page_flip_event = event;
Philipp Zabel1f941ae2013-06-21 10:57:20 +0200150 crtc->fb = fb;
Sascha Hauerf326f792012-09-21 10:07:50 +0200151
152 return 0;
153}
154
155static const struct drm_crtc_funcs ipu_crtc_funcs = {
156 .set_config = drm_crtc_helper_set_config,
157 .destroy = drm_crtc_cleanup,
158 .page_flip = ipu_page_flip,
159};
160
161static int ipu_drm_set_base(struct drm_crtc *crtc, int x, int y)
162{
163 struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
164 struct drm_gem_cma_object *cma_obj;
165 struct drm_framebuffer *fb = crtc->fb;
166 unsigned long phys;
167
168 cma_obj = drm_fb_cma_get_gem_obj(fb, 0);
169 if (!cma_obj) {
170 DRM_LOG_KMS("entry is null.\n");
171 return -EFAULT;
172 }
173
174 phys = cma_obj->paddr;
175 phys += x * (fb->bits_per_pixel >> 3);
176 phys += y * fb->pitches[0];
177
178 dev_dbg(ipu_crtc->dev, "%s: phys: 0x%lx\n", __func__, phys);
179 dev_dbg(ipu_crtc->dev, "%s: xy: %dx%d\n", __func__, x, y);
180
181 ipu_cpmem_set_stride(ipu_get_cpmem(ipu_crtc->ipu_ch), fb->pitches[0]);
182 ipu_cpmem_set_buffer(ipu_get_cpmem(ipu_crtc->ipu_ch),
183 0, phys);
184
185 return 0;
186}
187
188static int ipu_crtc_mode_set(struct drm_crtc *crtc,
189 struct drm_display_mode *orig_mode,
190 struct drm_display_mode *mode,
191 int x, int y,
192 struct drm_framebuffer *old_fb)
193{
194 struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
195 struct drm_framebuffer *fb = ipu_crtc->base.fb;
196 int ret;
197 struct ipu_di_signal_cfg sig_cfg = {};
198 u32 out_pixel_fmt;
199 struct ipu_ch_param __iomem *cpmem = ipu_get_cpmem(ipu_crtc->ipu_ch);
200 int bpp;
201 u32 v4l2_fmt;
202
203 dev_dbg(ipu_crtc->dev, "%s: mode->hdisplay: %d\n", __func__,
204 mode->hdisplay);
205 dev_dbg(ipu_crtc->dev, "%s: mode->vdisplay: %d\n", __func__,
206 mode->vdisplay);
207
208 ipu_ch_param_zero(cpmem);
209
210 switch (fb->pixel_format) {
211 case DRM_FORMAT_XRGB8888:
212 case DRM_FORMAT_ARGB8888:
213 v4l2_fmt = V4L2_PIX_FMT_RGB32;
214 bpp = 32;
215 break;
216 case DRM_FORMAT_RGB565:
217 v4l2_fmt = V4L2_PIX_FMT_RGB565;
218 bpp = 16;
219 break;
220 case DRM_FORMAT_RGB888:
221 v4l2_fmt = V4L2_PIX_FMT_RGB24;
222 bpp = 24;
223 break;
224 default:
225 dev_err(ipu_crtc->dev, "unsupported pixel format 0x%08x\n",
226 fb->pixel_format);
227 return -EINVAL;
228 }
229
230 out_pixel_fmt = ipu_crtc->interface_pix_fmt;
231
232 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
233 sig_cfg.interlaced = 1;
234 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
235 sig_cfg.Hsync_pol = 1;
236 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
237 sig_cfg.Vsync_pol = 1;
238
239 sig_cfg.enable_pol = 1;
240 sig_cfg.clk_pol = 0;
241 sig_cfg.width = mode->hdisplay;
242 sig_cfg.height = mode->vdisplay;
243 sig_cfg.pixel_fmt = out_pixel_fmt;
244 sig_cfg.h_start_width = mode->htotal - mode->hsync_end;
245 sig_cfg.h_sync_width = mode->hsync_end - mode->hsync_start;
246 sig_cfg.h_end_width = mode->hsync_start - mode->hdisplay;
247
248 sig_cfg.v_start_width = mode->vtotal - mode->vsync_end;
249 sig_cfg.v_sync_width = mode->vsync_end - mode->vsync_start;
250 sig_cfg.v_end_width = mode->vsync_start - mode->vdisplay;
251 sig_cfg.pixelclock = mode->clock * 1000;
252 sig_cfg.clkflags = ipu_crtc->di_clkflags;
253
254 sig_cfg.v_to_h_sync = 0;
255
Philipp Zabel2ea42602013-04-08 18:04:35 +0200256 sig_cfg.hsync_pin = ipu_crtc->di_hsync_pin;
257 sig_cfg.vsync_pin = ipu_crtc->di_vsync_pin;
258
Sascha Hauerf326f792012-09-21 10:07:50 +0200259 if (ipu_crtc->dp) {
260 ret = ipu_dp_setup_channel(ipu_crtc->dp, IPUV3_COLORSPACE_RGB,
261 IPUV3_COLORSPACE_RGB);
262 if (ret) {
263 dev_err(ipu_crtc->dev,
264 "initializing display processor failed with %d\n",
265 ret);
266 return ret;
267 }
268 ipu_dp_set_global_alpha(ipu_crtc->dp, 1, 0, 1);
269 }
270
271 ret = ipu_dc_init_sync(ipu_crtc->dc, ipu_crtc->di, sig_cfg.interlaced,
272 out_pixel_fmt, mode->hdisplay);
273 if (ret) {
274 dev_err(ipu_crtc->dev,
275 "initializing display controller failed with %d\n",
276 ret);
277 return ret;
278 }
279
280 ret = ipu_di_init_sync_panel(ipu_crtc->di, &sig_cfg);
281 if (ret) {
282 dev_err(ipu_crtc->dev,
283 "initializing panel failed with %d\n", ret);
284 return ret;
285 }
286
287 ipu_cpmem_set_resolution(cpmem, mode->hdisplay, mode->vdisplay);
288 ipu_cpmem_set_fmt(cpmem, v4l2_fmt);
289 ipu_cpmem_set_high_priority(ipu_crtc->ipu_ch);
290
291 ret = ipu_dmfc_init_channel(ipu_crtc->dmfc, mode->hdisplay);
292 if (ret) {
293 dev_err(ipu_crtc->dev,
294 "initializing dmfc channel failed with %d\n",
295 ret);
296 return ret;
297 }
298
299 ret = ipu_dmfc_alloc_bandwidth(ipu_crtc->dmfc,
300 calc_bandwidth(mode, calc_vref(mode)), 64);
301 if (ret) {
302 dev_err(ipu_crtc->dev,
303 "allocating dmfc bandwidth failed with %d\n",
304 ret);
305 return ret;
306 }
307
308 ipu_drm_set_base(crtc, x, y);
309
310 return 0;
311}
312
313static void ipu_crtc_handle_pageflip(struct ipu_crtc *ipu_crtc)
314{
Sascha Hauerf326f792012-09-21 10:07:50 +0200315 unsigned long flags;
316 struct drm_device *drm = ipu_crtc->base.dev;
317
318 spin_lock_irqsave(&drm->event_lock, flags);
Rob Clark0eca56f2012-10-08 19:50:46 +0000319 if (ipu_crtc->page_flip_event)
320 drm_send_vblank_event(drm, -1, ipu_crtc->page_flip_event);
Sascha Hauerf326f792012-09-21 10:07:50 +0200321 ipu_crtc->page_flip_event = NULL;
Sascha Hauerf326f792012-09-21 10:07:50 +0200322 imx_drm_crtc_vblank_put(ipu_crtc->imx_crtc);
Sascha Hauerf326f792012-09-21 10:07:50 +0200323 spin_unlock_irqrestore(&drm->event_lock, flags);
324}
325
326static irqreturn_t ipu_irq_handler(int irq, void *dev_id)
327{
328 struct ipu_crtc *ipu_crtc = dev_id;
329
330 imx_drm_handle_vblank(ipu_crtc->imx_crtc);
331
332 if (ipu_crtc->newfb) {
Sascha Hauerf326f792012-09-21 10:07:50 +0200333 ipu_crtc->newfb = NULL;
334 ipu_drm_set_base(&ipu_crtc->base, 0, 0);
335 ipu_crtc_handle_pageflip(ipu_crtc);
336 }
337
338 return IRQ_HANDLED;
339}
340
341static bool ipu_crtc_mode_fixup(struct drm_crtc *crtc,
342 const struct drm_display_mode *mode,
343 struct drm_display_mode *adjusted_mode)
344{
345 return true;
346}
347
348static void ipu_crtc_prepare(struct drm_crtc *crtc)
349{
350 struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
351
352 ipu_fb_disable(ipu_crtc);
353}
354
355static void ipu_crtc_commit(struct drm_crtc *crtc)
356{
357 struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
358
359 ipu_fb_enable(ipu_crtc);
360}
361
Sascha Hauerf326f792012-09-21 10:07:50 +0200362static struct drm_crtc_helper_funcs ipu_helper_funcs = {
363 .dpms = ipu_crtc_dpms,
364 .mode_fixup = ipu_crtc_mode_fixup,
365 .mode_set = ipu_crtc_mode_set,
366 .prepare = ipu_crtc_prepare,
367 .commit = ipu_crtc_commit,
Sascha Hauerf326f792012-09-21 10:07:50 +0200368};
369
370static int ipu_enable_vblank(struct drm_crtc *crtc)
371{
372 struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
373
374 enable_irq(ipu_crtc->irq);
375
376 return 0;
377}
378
379static void ipu_disable_vblank(struct drm_crtc *crtc)
380{
381 struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
382
383 disable_irq(ipu_crtc->irq);
384}
385
386static int ipu_set_interface_pix_fmt(struct drm_crtc *crtc, u32 encoder_type,
Philipp Zabel2ea42602013-04-08 18:04:35 +0200387 u32 pixfmt, int hsync_pin, int vsync_pin)
Sascha Hauerf326f792012-09-21 10:07:50 +0200388{
389 struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
390
391 ipu_crtc->interface_pix_fmt = pixfmt;
Philipp Zabel2ea42602013-04-08 18:04:35 +0200392 ipu_crtc->di_hsync_pin = hsync_pin;
393 ipu_crtc->di_vsync_pin = vsync_pin;
Sascha Hauerf326f792012-09-21 10:07:50 +0200394
395 switch (encoder_type) {
Philipp Zabelb60b5bc2013-04-08 18:04:33 +0200396 case DRM_MODE_ENCODER_DAC:
397 case DRM_MODE_ENCODER_TVDAC:
Sascha Hauerf326f792012-09-21 10:07:50 +0200398 case DRM_MODE_ENCODER_LVDS:
399 ipu_crtc->di_clkflags = IPU_DI_CLKMODE_SYNC |
400 IPU_DI_CLKMODE_EXT;
401 break;
402 case DRM_MODE_ENCODER_NONE:
403 ipu_crtc->di_clkflags = 0;
404 break;
405 }
406
407 return 0;
408}
409
410static const struct imx_drm_crtc_helper_funcs ipu_crtc_helper_funcs = {
411 .enable_vblank = ipu_enable_vblank,
412 .disable_vblank = ipu_disable_vblank,
413 .set_interface_pix_fmt = ipu_set_interface_pix_fmt,
414 .crtc_funcs = &ipu_crtc_funcs,
415 .crtc_helper_funcs = &ipu_helper_funcs,
416};
417
418static void ipu_put_resources(struct ipu_crtc *ipu_crtc)
419{
420 if (!IS_ERR_OR_NULL(ipu_crtc->ipu_ch))
421 ipu_idmac_put(ipu_crtc->ipu_ch);
422 if (!IS_ERR_OR_NULL(ipu_crtc->dmfc))
423 ipu_dmfc_put(ipu_crtc->dmfc);
424 if (!IS_ERR_OR_NULL(ipu_crtc->dp))
425 ipu_dp_put(ipu_crtc->dp);
426 if (!IS_ERR_OR_NULL(ipu_crtc->di))
427 ipu_di_put(ipu_crtc->di);
428}
429
430static int ipu_get_resources(struct ipu_crtc *ipu_crtc,
431 struct ipu_client_platformdata *pdata)
432{
433 struct ipu_soc *ipu = dev_get_drvdata(ipu_crtc->dev->parent);
434 int ret;
435
436 ipu_crtc->ipu_ch = ipu_idmac_get(ipu, pdata->dma[0]);
Lothar Waßmann9a8f3f42012-12-25 15:58:38 +0100437 if (IS_ERR(ipu_crtc->ipu_ch)) {
Sascha Hauerf326f792012-09-21 10:07:50 +0200438 ret = PTR_ERR(ipu_crtc->ipu_ch);
439 goto err_out;
440 }
441
442 ipu_crtc->dc = ipu_dc_get(ipu, pdata->dc);
443 if (IS_ERR(ipu_crtc->dc)) {
444 ret = PTR_ERR(ipu_crtc->dc);
445 goto err_out;
446 }
447
448 ipu_crtc->dmfc = ipu_dmfc_get(ipu, pdata->dma[0]);
449 if (IS_ERR(ipu_crtc->dmfc)) {
450 ret = PTR_ERR(ipu_crtc->dmfc);
451 goto err_out;
452 }
453
454 if (pdata->dp >= 0) {
455 ipu_crtc->dp = ipu_dp_get(ipu, pdata->dp);
456 if (IS_ERR(ipu_crtc->dp)) {
Lothar Waßmann9a8f3f42012-12-25 15:58:38 +0100457 ret = PTR_ERR(ipu_crtc->dp);
Sascha Hauerf326f792012-09-21 10:07:50 +0200458 goto err_out;
459 }
460 }
461
462 ipu_crtc->di = ipu_di_get(ipu, pdata->di);
463 if (IS_ERR(ipu_crtc->di)) {
464 ret = PTR_ERR(ipu_crtc->di);
465 goto err_out;
466 }
467
Sascha Hauerf326f792012-09-21 10:07:50 +0200468 return 0;
469err_out:
470 ipu_put_resources(ipu_crtc);
471
472 return ret;
473}
474
475static int ipu_crtc_init(struct ipu_crtc *ipu_crtc,
476 struct ipu_client_platformdata *pdata)
477{
Philipp Zabel47b1be52013-02-20 10:57:01 +0800478 struct ipu_soc *ipu = dev_get_drvdata(ipu_crtc->dev->parent);
Sascha Hauerf326f792012-09-21 10:07:50 +0200479 int ret;
480
481 ret = ipu_get_resources(ipu_crtc, pdata);
482 if (ret) {
483 dev_err(ipu_crtc->dev, "getting resources failed with %d.\n",
484 ret);
485 return ret;
486 }
487
488 ret = imx_drm_add_crtc(&ipu_crtc->base,
489 &ipu_crtc->imx_crtc,
490 &ipu_crtc_helper_funcs, THIS_MODULE,
491 ipu_crtc->dev->parent->of_node, pdata->di);
492 if (ret) {
493 dev_err(ipu_crtc->dev, "adding crtc failed with %d.\n", ret);
494 goto err_put_resources;
495 }
496
Philipp Zabel47b1be52013-02-20 10:57:01 +0800497 ipu_crtc->irq = ipu_idmac_channel_irq(ipu, ipu_crtc->ipu_ch,
498 IPU_IRQ_EOF);
499 ret = devm_request_irq(ipu_crtc->dev, ipu_crtc->irq, ipu_irq_handler, 0,
500 "imx_drm", ipu_crtc);
501 if (ret < 0) {
502 dev_err(ipu_crtc->dev, "irq request failed with %d.\n", ret);
503 goto err_put_resources;
504 }
505
506 disable_irq(ipu_crtc->irq);
507
Sascha Hauerf326f792012-09-21 10:07:50 +0200508 return 0;
509
510err_put_resources:
511 ipu_put_resources(ipu_crtc);
512
513 return ret;
514}
515
Bill Pembertonc4aabf82012-11-19 13:22:11 -0500516static int ipu_drm_probe(struct platform_device *pdev)
Sascha Hauerf326f792012-09-21 10:07:50 +0200517{
518 struct ipu_client_platformdata *pdata = pdev->dev.platform_data;
519 struct ipu_crtc *ipu_crtc;
520 int ret;
521
522 if (!pdata)
523 return -EINVAL;
524
525 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
526
527 ipu_crtc = devm_kzalloc(&pdev->dev, sizeof(*ipu_crtc), GFP_KERNEL);
528 if (!ipu_crtc)
529 return -ENOMEM;
530
531 ipu_crtc->dev = &pdev->dev;
532
533 ret = ipu_crtc_init(ipu_crtc, pdata);
Lothar Waßmann9a8f3f42012-12-25 15:58:38 +0100534 if (ret)
535 return ret;
Sascha Hauerf326f792012-09-21 10:07:50 +0200536
537 platform_set_drvdata(pdev, ipu_crtc);
538
539 return 0;
540}
541
Bill Pemberton8aa1be42012-11-19 13:26:38 -0500542static int ipu_drm_remove(struct platform_device *pdev)
Sascha Hauerf326f792012-09-21 10:07:50 +0200543{
544 struct ipu_crtc *ipu_crtc = platform_get_drvdata(pdev);
545
546 imx_drm_remove_crtc(ipu_crtc->imx_crtc);
547
548 ipu_put_resources(ipu_crtc);
549
550 return 0;
551}
552
553static struct platform_driver ipu_drm_driver = {
554 .driver = {
555 .name = "imx-ipuv3-crtc",
556 },
557 .probe = ipu_drm_probe,
Bill Pemberton99c28f12012-11-19 13:20:51 -0500558 .remove = ipu_drm_remove,
Sascha Hauerf326f792012-09-21 10:07:50 +0200559};
560module_platform_driver(ipu_drm_driver);
561
562MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
563MODULE_DESCRIPTION(DRIVER_DESC);
564MODULE_LICENSE("GPL");
Fabio Estevamce9c1ce2013-08-18 21:40:06 -0300565MODULE_ALIAS("platform:imx-ipuv3-crtc");