blob: 708b3543d4e922d0a656d8bb9c82ee814c18ea3d [file] [log] [blame]
Maxime Ripard9026e0d2015-10-29 09:36:23 +01001/*
2 * Copyright (C) 2015 Free Electrons
3 * Copyright (C) 2015 NextThing Co
4 *
5 * Maxime Ripard <maxime.ripard@free-electrons.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
11 */
12
13#include <drm/drmP.h>
14#include <drm/drm_atomic_helper.h>
15#include <drm/drm_crtc.h>
16#include <drm/drm_crtc_helper.h>
17#include <drm/drm_modes.h>
18
19#include <linux/clk-provider.h>
20#include <linux/ioport.h>
21#include <linux/of_address.h>
Chen-Yu Tsai75448602017-02-23 16:05:34 +080022#include <linux/of_graph.h>
Maxime Ripard9026e0d2015-10-29 09:36:23 +010023#include <linux/of_irq.h>
24#include <linux/regmap.h>
25
26#include <video/videomode.h>
27
28#include "sun4i_backend.h"
29#include "sun4i_crtc.h"
30#include "sun4i_drv.h"
Chen-Yu Tsaib3f266e2017-02-23 16:05:36 +080031#include "sun4i_layer.h"
Maxime Ripard9026e0d2015-10-29 09:36:23 +010032#include "sun4i_tcon.h"
33
34static void sun4i_crtc_atomic_begin(struct drm_crtc *crtc,
35 struct drm_crtc_state *old_state)
36{
37 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
38 struct drm_device *dev = crtc->dev;
39 unsigned long flags;
40
41 if (crtc->state->event) {
42 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
43
44 spin_lock_irqsave(&dev->event_lock, flags);
45 scrtc->event = crtc->state->event;
46 spin_unlock_irqrestore(&dev->event_lock, flags);
47 crtc->state->event = NULL;
48 }
49}
50
51static void sun4i_crtc_atomic_flush(struct drm_crtc *crtc,
52 struct drm_crtc_state *old_state)
53{
54 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
Daniel Vettera33e93d2016-06-08 14:18:58 +020055 struct drm_pending_vblank_event *event = crtc->state->event;
Maxime Ripard9026e0d2015-10-29 09:36:23 +010056
57 DRM_DEBUG_DRIVER("Committing plane changes\n");
58
Chen-Yu Tsai3c64fb32017-02-23 16:05:43 +080059 sun4i_backend_commit(scrtc->backend);
Daniel Vettera33e93d2016-06-08 14:18:58 +020060
61 if (event) {
62 crtc->state->event = NULL;
63
64 spin_lock_irq(&crtc->dev->event_lock);
65 if (drm_crtc_vblank_get(crtc) == 0)
66 drm_crtc_arm_vblank_event(crtc, event);
67 else
68 drm_crtc_send_vblank_event(crtc, event);
69 spin_unlock_irq(&crtc->dev->event_lock);
70 }
Maxime Ripard9026e0d2015-10-29 09:36:23 +010071}
72
73static void sun4i_crtc_disable(struct drm_crtc *crtc)
74{
75 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
Maxime Ripard9026e0d2015-10-29 09:36:23 +010076
77 DRM_DEBUG_DRIVER("Disabling the CRTC\n");
78
Chen-Yu Tsai3c64fb32017-02-23 16:05:43 +080079 sun4i_tcon_disable(scrtc->tcon);
Maxime Ripard2cd36832016-06-20 12:20:59 +020080
81 if (crtc->state->event && !crtc->state->active) {
82 spin_lock_irq(&crtc->dev->event_lock);
83 drm_crtc_send_vblank_event(crtc, crtc->state->event);
84 spin_unlock_irq(&crtc->dev->event_lock);
85
86 crtc->state->event = NULL;
87 }
Maxime Ripard9026e0d2015-10-29 09:36:23 +010088}
89
90static void sun4i_crtc_enable(struct drm_crtc *crtc)
91{
92 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
Maxime Ripard9026e0d2015-10-29 09:36:23 +010093
94 DRM_DEBUG_DRIVER("Enabling the CRTC\n");
95
Chen-Yu Tsai3c64fb32017-02-23 16:05:43 +080096 sun4i_tcon_enable(scrtc->tcon);
Maxime Ripard9026e0d2015-10-29 09:36:23 +010097}
98
99static const struct drm_crtc_helper_funcs sun4i_crtc_helper_funcs = {
100 .atomic_begin = sun4i_crtc_atomic_begin,
101 .atomic_flush = sun4i_crtc_atomic_flush,
102 .disable = sun4i_crtc_disable,
103 .enable = sun4i_crtc_enable,
104};
105
Shawn Guo50480a72017-02-07 17:16:31 +0800106static int sun4i_crtc_enable_vblank(struct drm_crtc *crtc)
107{
108 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
Shawn Guo50480a72017-02-07 17:16:31 +0800109
110 DRM_DEBUG_DRIVER("Enabling VBLANK on crtc %p\n", crtc);
111
Chen-Yu Tsai3c64fb32017-02-23 16:05:43 +0800112 sun4i_tcon_enable_vblank(scrtc->tcon, true);
Shawn Guo50480a72017-02-07 17:16:31 +0800113
114 return 0;
115}
116
117static void sun4i_crtc_disable_vblank(struct drm_crtc *crtc)
118{
119 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
Shawn Guo50480a72017-02-07 17:16:31 +0800120
121 DRM_DEBUG_DRIVER("Disabling VBLANK on crtc %p\n", crtc);
122
Chen-Yu Tsai3c64fb32017-02-23 16:05:43 +0800123 sun4i_tcon_enable_vblank(scrtc->tcon, false);
Shawn Guo50480a72017-02-07 17:16:31 +0800124}
125
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100126static const struct drm_crtc_funcs sun4i_crtc_funcs = {
127 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
128 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
129 .destroy = drm_crtc_cleanup,
130 .page_flip = drm_atomic_helper_page_flip,
131 .reset = drm_atomic_helper_crtc_reset,
132 .set_config = drm_atomic_helper_set_config,
Shawn Guo50480a72017-02-07 17:16:31 +0800133 .enable_vblank = sun4i_crtc_enable_vblank,
134 .disable_vblank = sun4i_crtc_disable_vblank,
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100135};
136
Chen-Yu Tsai18c3b302017-03-09 18:05:28 +0800137struct sun4i_crtc *sun4i_crtc_init(struct drm_device *drm,
138 struct sun4i_backend *backend,
139 struct sun4i_tcon *tcon)
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100140{
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100141 struct sun4i_crtc *scrtc;
Icenowy Zheng7921e142017-05-15 00:30:36 +0800142 struct drm_plane **planes;
Chen-Yu Tsaidcd21582017-02-23 16:05:38 +0800143 struct drm_plane *primary = NULL, *cursor = NULL;
144 int ret, i;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100145
146 scrtc = devm_kzalloc(drm->dev, sizeof(*scrtc), GFP_KERNEL);
147 if (!scrtc)
Chen-Yu Tsaiea411fd2017-02-17 11:13:30 +0800148 return ERR_PTR(-ENOMEM);
Chen-Yu Tsai18c3b302017-03-09 18:05:28 +0800149 scrtc->backend = backend;
150 scrtc->tcon = tcon;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100151
Chen-Yu Tsaib3f266e2017-02-23 16:05:36 +0800152 /* Create our layers */
Icenowy Zheng7921e142017-05-15 00:30:36 +0800153 planes = sun4i_layers_init(drm, scrtc);
154 if (IS_ERR(planes)) {
Chen-Yu Tsaib3f266e2017-02-23 16:05:36 +0800155 dev_err(drm->dev, "Couldn't create the planes\n");
Chen-Yu Tsaidcd21582017-02-23 16:05:38 +0800156 return NULL;
157 }
158
159 /* find primary and cursor planes for drm_crtc_init_with_planes */
Icenowy Zheng7921e142017-05-15 00:30:36 +0800160 for (i = 0; planes[i]; i++) {
161 struct drm_plane *plane = planes[i];
Chen-Yu Tsaidcd21582017-02-23 16:05:38 +0800162
Icenowy Zheng7921e142017-05-15 00:30:36 +0800163 switch (plane->type) {
Chen-Yu Tsaidcd21582017-02-23 16:05:38 +0800164 case DRM_PLANE_TYPE_PRIMARY:
Icenowy Zheng7921e142017-05-15 00:30:36 +0800165 primary = plane;
Chen-Yu Tsaidcd21582017-02-23 16:05:38 +0800166 break;
167 case DRM_PLANE_TYPE_CURSOR:
Icenowy Zheng7921e142017-05-15 00:30:36 +0800168 cursor = plane;
Chen-Yu Tsaidcd21582017-02-23 16:05:38 +0800169 break;
170 default:
171 break;
172 }
Chen-Yu Tsaib3f266e2017-02-23 16:05:36 +0800173 }
174
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100175 ret = drm_crtc_init_with_planes(drm, &scrtc->crtc,
Chen-Yu Tsaidcd21582017-02-23 16:05:38 +0800176 primary,
177 cursor,
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100178 &sun4i_crtc_funcs,
179 NULL);
180 if (ret) {
181 dev_err(drm->dev, "Couldn't init DRM CRTC\n");
Chen-Yu Tsaiea411fd2017-02-17 11:13:30 +0800182 return ERR_PTR(ret);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100183 }
184
185 drm_crtc_helper_add(&scrtc->crtc, &sun4i_crtc_helper_funcs);
186
Chen-Yu Tsai75448602017-02-23 16:05:34 +0800187 /* Set crtc.port to output port node of the tcon */
Chen-Yu Tsaie4cdcb72017-03-09 18:05:26 +0800188 scrtc->crtc.port = of_graph_get_port_by_id(scrtc->tcon->dev->of_node,
Chen-Yu Tsai75448602017-02-23 16:05:34 +0800189 1);
190
Chen-Yu Tsaia5154a42017-02-23 16:05:39 +0800191 /* Set possible_crtcs to this crtc for overlay planes */
Icenowy Zheng7921e142017-05-15 00:30:36 +0800192 for (i = 0; planes[i]; i++) {
Chen-Yu Tsaia5154a42017-02-23 16:05:39 +0800193 uint32_t possible_crtcs = BIT(drm_crtc_index(&scrtc->crtc));
Icenowy Zheng7921e142017-05-15 00:30:36 +0800194 struct drm_plane *plane = planes[i];
Chen-Yu Tsaia5154a42017-02-23 16:05:39 +0800195
Icenowy Zheng7921e142017-05-15 00:30:36 +0800196 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
197 plane->possible_crtcs = possible_crtcs;
Chen-Yu Tsaia5154a42017-02-23 16:05:39 +0800198 }
199
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100200 return scrtc;
201}