OSDN Git Service

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