]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/imx-drm/ipuv3-crtc.c
imx-drm: ipuv3-crtc: change display enable/disable order
[karo-tx-linux.git] / drivers / staging / imx-drm / ipuv3-crtc.c
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  * 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/component.h>
21 #include <linux/module.h>
22 #include <linux/export.h>
23 #include <linux/device.h>
24 #include <linux/platform_device.h>
25 #include <drm/drmP.h>
26 #include <drm/drm_crtc_helper.h>
27 #include <linux/fb.h>
28 #include <linux/clk.h>
29 #include <linux/errno.h>
30 #include <drm/drm_gem_cma_helper.h>
31 #include <drm/drm_fb_cma_helper.h>
32
33 #include "ipu-v3/imx-ipu-v3.h"
34 #include "imx-drm.h"
35 #include "ipuv3-plane.h"
36
37 #define DRIVER_DESC             "i.MX IPUv3 Graphics"
38
39 struct ipu_crtc {
40         struct device           *dev;
41         struct drm_crtc         base;
42         struct imx_drm_crtc     *imx_crtc;
43
44         /* plane[0] is the full plane, plane[1] is the partial plane */
45         struct ipu_plane        *plane[2];
46
47         struct ipu_dc           *dc;
48         struct ipu_di           *di;
49         int                     enabled;
50         struct drm_pending_vblank_event *page_flip_event;
51         struct drm_framebuffer  *newfb;
52         int                     irq;
53         u32                     interface_pix_fmt;
54         unsigned long           di_clkflags;
55         int                     di_hsync_pin;
56         int                     di_vsync_pin;
57 };
58
59 #define to_ipu_crtc(x) container_of(x, struct ipu_crtc, base)
60
61 static void ipu_fb_enable(struct ipu_crtc *ipu_crtc)
62 {
63         if (ipu_crtc->enabled)
64                 return;
65
66         /* TODO: Enable DC module here, right now it is never disabled */
67         ipu_plane_enable(ipu_crtc->plane[0]);
68         /* Start DC channel and DI after IDMAC */
69         ipu_dc_enable_channel(ipu_crtc->dc);
70         ipu_di_enable(ipu_crtc->di);
71
72         ipu_crtc->enabled = 1;
73 }
74
75 static void ipu_fb_disable(struct ipu_crtc *ipu_crtc)
76 {
77         if (!ipu_crtc->enabled)
78                 return;
79
80         /* Stop DC channel and DI before IDMAC */
81         ipu_dc_disable_channel(ipu_crtc->dc);
82         ipu_di_disable(ipu_crtc->di);
83         ipu_plane_disable(ipu_crtc->plane[0]);
84         /* TODO: Disable DC module here */
85
86         ipu_crtc->enabled = 0;
87 }
88
89 static void ipu_crtc_dpms(struct drm_crtc *crtc, int mode)
90 {
91         struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
92
93         dev_dbg(ipu_crtc->dev, "%s mode: %d\n", __func__, mode);
94
95         switch (mode) {
96         case DRM_MODE_DPMS_ON:
97                 ipu_fb_enable(ipu_crtc);
98                 break;
99         case DRM_MODE_DPMS_STANDBY:
100         case DRM_MODE_DPMS_SUSPEND:
101         case DRM_MODE_DPMS_OFF:
102                 ipu_fb_disable(ipu_crtc);
103                 break;
104         }
105 }
106
107 static int ipu_page_flip(struct drm_crtc *crtc,
108                 struct drm_framebuffer *fb,
109                 struct drm_pending_vblank_event *event,
110                 uint32_t page_flip_flags)
111 {
112         struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
113         int ret;
114
115         if (ipu_crtc->newfb)
116                 return -EBUSY;
117
118         ret = imx_drm_crtc_vblank_get(ipu_crtc->imx_crtc);
119         if (ret) {
120                 dev_dbg(ipu_crtc->dev, "failed to acquire vblank counter\n");
121                 list_del(&event->base.link);
122
123                 return ret;
124         }
125
126         ipu_crtc->newfb = fb;
127         ipu_crtc->page_flip_event = event;
128         crtc->primary->fb = fb;
129
130         return 0;
131 }
132
133 static const struct drm_crtc_funcs ipu_crtc_funcs = {
134         .set_config = drm_crtc_helper_set_config,
135         .destroy = drm_crtc_cleanup,
136         .page_flip = ipu_page_flip,
137 };
138
139 static int ipu_crtc_mode_set(struct drm_crtc *crtc,
140                                struct drm_display_mode *orig_mode,
141                                struct drm_display_mode *mode,
142                                int x, int y,
143                                struct drm_framebuffer *old_fb)
144 {
145         struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
146         int ret;
147         struct ipu_di_signal_cfg sig_cfg = {};
148         u32 out_pixel_fmt;
149
150         dev_dbg(ipu_crtc->dev, "%s: mode->hdisplay: %d\n", __func__,
151                         mode->hdisplay);
152         dev_dbg(ipu_crtc->dev, "%s: mode->vdisplay: %d\n", __func__,
153                         mode->vdisplay);
154
155         out_pixel_fmt = ipu_crtc->interface_pix_fmt;
156
157         if (mode->flags & DRM_MODE_FLAG_INTERLACE)
158                 sig_cfg.interlaced = 1;
159         if (mode->flags & DRM_MODE_FLAG_PHSYNC)
160                 sig_cfg.Hsync_pol = 1;
161         if (mode->flags & DRM_MODE_FLAG_PVSYNC)
162                 sig_cfg.Vsync_pol = 1;
163
164         sig_cfg.enable_pol = 1;
165         sig_cfg.clk_pol = 0;
166         sig_cfg.width = mode->hdisplay;
167         sig_cfg.height = mode->vdisplay;
168         sig_cfg.pixel_fmt = out_pixel_fmt;
169         sig_cfg.h_start_width = mode->htotal - mode->hsync_end;
170         sig_cfg.h_sync_width = mode->hsync_end - mode->hsync_start;
171         sig_cfg.h_end_width = mode->hsync_start - mode->hdisplay;
172
173         sig_cfg.v_start_width = mode->vtotal - mode->vsync_end;
174         sig_cfg.v_sync_width = mode->vsync_end - mode->vsync_start;
175         sig_cfg.v_end_width = mode->vsync_start - mode->vdisplay;
176         sig_cfg.pixelclock = mode->clock * 1000;
177         sig_cfg.clkflags = ipu_crtc->di_clkflags;
178
179         sig_cfg.v_to_h_sync = 0;
180
181         sig_cfg.hsync_pin = ipu_crtc->di_hsync_pin;
182         sig_cfg.vsync_pin = ipu_crtc->di_vsync_pin;
183
184         ret = ipu_dc_init_sync(ipu_crtc->dc, ipu_crtc->di, sig_cfg.interlaced,
185                         out_pixel_fmt, mode->hdisplay);
186         if (ret) {
187                 dev_err(ipu_crtc->dev,
188                                 "initializing display controller failed with %d\n",
189                                 ret);
190                 return ret;
191         }
192
193         ret = ipu_di_init_sync_panel(ipu_crtc->di, &sig_cfg);
194         if (ret) {
195                 dev_err(ipu_crtc->dev,
196                                 "initializing panel failed with %d\n", ret);
197                 return ret;
198         }
199
200         return ipu_plane_mode_set(ipu_crtc->plane[0], crtc, mode, crtc->primary->fb,
201                                   0, 0, mode->hdisplay, mode->vdisplay,
202                                   x, y, mode->hdisplay, mode->vdisplay);
203 }
204
205 static void ipu_crtc_handle_pageflip(struct ipu_crtc *ipu_crtc)
206 {
207         unsigned long flags;
208         struct drm_device *drm = ipu_crtc->base.dev;
209
210         spin_lock_irqsave(&drm->event_lock, flags);
211         if (ipu_crtc->page_flip_event)
212                 drm_send_vblank_event(drm, -1, ipu_crtc->page_flip_event);
213         ipu_crtc->page_flip_event = NULL;
214         imx_drm_crtc_vblank_put(ipu_crtc->imx_crtc);
215         spin_unlock_irqrestore(&drm->event_lock, flags);
216 }
217
218 static irqreturn_t ipu_irq_handler(int irq, void *dev_id)
219 {
220         struct ipu_crtc *ipu_crtc = dev_id;
221
222         imx_drm_handle_vblank(ipu_crtc->imx_crtc);
223
224         if (ipu_crtc->newfb) {
225                 ipu_crtc->newfb = NULL;
226                 ipu_plane_set_base(ipu_crtc->plane[0], ipu_crtc->base.primary->fb,
227                                 ipu_crtc->plane[0]->x, ipu_crtc->plane[0]->y);
228                 ipu_crtc_handle_pageflip(ipu_crtc);
229         }
230
231         return IRQ_HANDLED;
232 }
233
234 static bool ipu_crtc_mode_fixup(struct drm_crtc *crtc,
235                                   const struct drm_display_mode *mode,
236                                   struct drm_display_mode *adjusted_mode)
237 {
238         return true;
239 }
240
241 static void ipu_crtc_prepare(struct drm_crtc *crtc)
242 {
243         struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
244
245         ipu_fb_disable(ipu_crtc);
246 }
247
248 static void ipu_crtc_commit(struct drm_crtc *crtc)
249 {
250         struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
251
252         ipu_fb_enable(ipu_crtc);
253 }
254
255 static struct drm_crtc_helper_funcs ipu_helper_funcs = {
256         .dpms = ipu_crtc_dpms,
257         .mode_fixup = ipu_crtc_mode_fixup,
258         .mode_set = ipu_crtc_mode_set,
259         .prepare = ipu_crtc_prepare,
260         .commit = ipu_crtc_commit,
261 };
262
263 static int ipu_enable_vblank(struct drm_crtc *crtc)
264 {
265         return 0;
266 }
267
268 static void ipu_disable_vblank(struct drm_crtc *crtc)
269 {
270         struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
271
272         ipu_crtc->page_flip_event = NULL;
273         ipu_crtc->newfb = NULL;
274 }
275
276 static int ipu_set_interface_pix_fmt(struct drm_crtc *crtc, u32 encoder_type,
277                 u32 pixfmt, int hsync_pin, int vsync_pin)
278 {
279         struct ipu_crtc *ipu_crtc = to_ipu_crtc(crtc);
280
281         ipu_crtc->interface_pix_fmt = pixfmt;
282         ipu_crtc->di_hsync_pin = hsync_pin;
283         ipu_crtc->di_vsync_pin = vsync_pin;
284
285         switch (encoder_type) {
286         case DRM_MODE_ENCODER_DAC:
287         case DRM_MODE_ENCODER_TVDAC:
288         case DRM_MODE_ENCODER_LVDS:
289                 ipu_crtc->di_clkflags = IPU_DI_CLKMODE_SYNC |
290                         IPU_DI_CLKMODE_EXT;
291                 break;
292         case DRM_MODE_ENCODER_TMDS:
293         case DRM_MODE_ENCODER_NONE:
294                 ipu_crtc->di_clkflags = 0;
295                 break;
296         }
297
298         return 0;
299 }
300
301 static const struct imx_drm_crtc_helper_funcs ipu_crtc_helper_funcs = {
302         .enable_vblank = ipu_enable_vblank,
303         .disable_vblank = ipu_disable_vblank,
304         .set_interface_pix_fmt = ipu_set_interface_pix_fmt,
305         .crtc_funcs = &ipu_crtc_funcs,
306         .crtc_helper_funcs = &ipu_helper_funcs,
307 };
308
309 static void ipu_put_resources(struct ipu_crtc *ipu_crtc)
310 {
311         if (!IS_ERR_OR_NULL(ipu_crtc->dc))
312                 ipu_dc_put(ipu_crtc->dc);
313         if (!IS_ERR_OR_NULL(ipu_crtc->di))
314                 ipu_di_put(ipu_crtc->di);
315 }
316
317 static int ipu_get_resources(struct ipu_crtc *ipu_crtc,
318                 struct ipu_client_platformdata *pdata)
319 {
320         struct ipu_soc *ipu = dev_get_drvdata(ipu_crtc->dev->parent);
321         int ret;
322
323         ipu_crtc->dc = ipu_dc_get(ipu, pdata->dc);
324         if (IS_ERR(ipu_crtc->dc)) {
325                 ret = PTR_ERR(ipu_crtc->dc);
326                 goto err_out;
327         }
328
329         ipu_crtc->di = ipu_di_get(ipu, pdata->di);
330         if (IS_ERR(ipu_crtc->di)) {
331                 ret = PTR_ERR(ipu_crtc->di);
332                 goto err_out;
333         }
334
335         return 0;
336 err_out:
337         ipu_put_resources(ipu_crtc);
338
339         return ret;
340 }
341
342 static int ipu_crtc_init(struct ipu_crtc *ipu_crtc,
343         struct ipu_client_platformdata *pdata, struct drm_device *drm)
344 {
345         struct ipu_soc *ipu = dev_get_drvdata(ipu_crtc->dev->parent);
346         int dp = -EINVAL;
347         int ret;
348         int id;
349
350         ret = ipu_get_resources(ipu_crtc, pdata);
351         if (ret) {
352                 dev_err(ipu_crtc->dev, "getting resources failed with %d.\n",
353                                 ret);
354                 return ret;
355         }
356
357         ret = imx_drm_add_crtc(drm, &ipu_crtc->base, &ipu_crtc->imx_crtc,
358                         &ipu_crtc_helper_funcs, ipu_crtc->dev->of_node);
359         if (ret) {
360                 dev_err(ipu_crtc->dev, "adding crtc failed with %d.\n", ret);
361                 goto err_put_resources;
362         }
363
364         if (pdata->dp >= 0)
365                 dp = IPU_DP_FLOW_SYNC_BG;
366         id = imx_drm_crtc_id(ipu_crtc->imx_crtc);
367         ipu_crtc->plane[0] = ipu_plane_init(ipu_crtc->base.dev, ipu,
368                                             pdata->dma[0], dp, BIT(id), true);
369         ret = ipu_plane_get_resources(ipu_crtc->plane[0]);
370         if (ret) {
371                 dev_err(ipu_crtc->dev, "getting plane 0 resources failed with %d.\n",
372                         ret);
373                 goto err_remove_crtc;
374         }
375
376         /* If this crtc is using the DP, add an overlay plane */
377         if (pdata->dp >= 0 && pdata->dma[1] > 0) {
378                 ipu_crtc->plane[1] = ipu_plane_init(ipu_crtc->base.dev, ipu,
379                                                     pdata->dma[1],
380                                                     IPU_DP_FLOW_SYNC_FG,
381                                                     BIT(id), false);
382                 if (IS_ERR(ipu_crtc->plane[1]))
383                         ipu_crtc->plane[1] = NULL;
384         }
385
386         ipu_crtc->irq = ipu_plane_irq(ipu_crtc->plane[0]);
387         ret = devm_request_irq(ipu_crtc->dev, ipu_crtc->irq, ipu_irq_handler, 0,
388                         "imx_drm", ipu_crtc);
389         if (ret < 0) {
390                 dev_err(ipu_crtc->dev, "irq request failed with %d.\n", ret);
391                 goto err_put_plane_res;
392         }
393
394         return 0;
395
396 err_put_plane_res:
397         ipu_plane_put_resources(ipu_crtc->plane[0]);
398 err_remove_crtc:
399         imx_drm_remove_crtc(ipu_crtc->imx_crtc);
400 err_put_resources:
401         ipu_put_resources(ipu_crtc);
402
403         return ret;
404 }
405
406 static struct device_node *ipu_drm_get_port_by_id(struct device_node *parent,
407                                                   int port_id)
408 {
409         struct device_node *port;
410         int id, ret;
411
412         port = of_get_child_by_name(parent, "port");
413         while (port) {
414                 ret = of_property_read_u32(port, "reg", &id);
415                 if (!ret && id == port_id)
416                         return port;
417
418                 do {
419                         port = of_get_next_child(parent, port);
420                         if (!port)
421                                 return NULL;
422                 } while (of_node_cmp(port->name, "port"));
423         }
424
425         return NULL;
426 }
427
428 static int ipu_drm_bind(struct device *dev, struct device *master, void *data)
429 {
430         struct ipu_client_platformdata *pdata = dev->platform_data;
431         struct drm_device *drm = data;
432         struct ipu_crtc *ipu_crtc;
433         int ret;
434
435         ipu_crtc = devm_kzalloc(dev, sizeof(*ipu_crtc), GFP_KERNEL);
436         if (!ipu_crtc)
437                 return -ENOMEM;
438
439         ipu_crtc->dev = dev;
440
441         ret = ipu_crtc_init(ipu_crtc, pdata, drm);
442         if (ret)
443                 return ret;
444
445         dev_set_drvdata(dev, ipu_crtc);
446
447         return 0;
448 }
449
450 static void ipu_drm_unbind(struct device *dev, struct device *master,
451         void *data)
452 {
453         struct ipu_crtc *ipu_crtc = dev_get_drvdata(dev);
454
455         imx_drm_remove_crtc(ipu_crtc->imx_crtc);
456
457         ipu_plane_put_resources(ipu_crtc->plane[0]);
458         ipu_put_resources(ipu_crtc);
459 }
460
461 static const struct component_ops ipu_crtc_ops = {
462         .bind = ipu_drm_bind,
463         .unbind = ipu_drm_unbind,
464 };
465
466 static int ipu_drm_probe(struct platform_device *pdev)
467 {
468         struct device *dev = &pdev->dev;
469         struct ipu_client_platformdata *pdata = dev->platform_data;
470         int ret;
471
472         if (!dev->platform_data)
473                 return -EINVAL;
474
475         if (!dev->of_node) {
476                 /* Associate crtc device with the corresponding DI port node */
477                 dev->of_node = ipu_drm_get_port_by_id(dev->parent->of_node,
478                                                       pdata->di + 2);
479                 if (!dev->of_node) {
480                         dev_err(dev, "missing port@%d node in %s\n",
481                                 pdata->di + 2, dev->parent->of_node->full_name);
482                         return -ENODEV;
483                 }
484         }
485
486         ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
487         if (ret)
488                 return ret;
489
490         return component_add(dev, &ipu_crtc_ops);
491 }
492
493 static int ipu_drm_remove(struct platform_device *pdev)
494 {
495         component_del(&pdev->dev, &ipu_crtc_ops);
496         return 0;
497 }
498
499 static struct platform_driver ipu_drm_driver = {
500         .driver = {
501                 .name = "imx-ipuv3-crtc",
502         },
503         .probe = ipu_drm_probe,
504         .remove = ipu_drm_remove,
505 };
506 module_platform_driver(ipu_drm_driver);
507
508 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
509 MODULE_DESCRIPTION(DRIVER_DESC);
510 MODULE_LICENSE("GPL");
511 MODULE_ALIAS("platform:imx-ipuv3-crtc");