blob: d097c6f93ad0188ce82494c63e2d722253a05a98 [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
Maxime Ripard9026e0d2015-10-29 09:36:23 +010028#include "sun4i_crtc.h"
29#include "sun4i_drv.h"
Icenowy Zheng87969332017-05-17 22:47:17 +080030#include "sunxi_engine.h"
Maxime Ripard9026e0d2015-10-29 09:36:23 +010031#include "sun4i_tcon.h"
32
33static void sun4i_crtc_atomic_begin(struct drm_crtc *crtc,
34 struct drm_crtc_state *old_state)
35{
36 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
37 struct drm_device *dev = crtc->dev;
38 unsigned long flags;
39
40 if (crtc->state->event) {
41 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
42
43 spin_lock_irqsave(&dev->event_lock, flags);
44 scrtc->event = crtc->state->event;
45 spin_unlock_irqrestore(&dev->event_lock, flags);
46 crtc->state->event = NULL;
47 }
48}
49
50static void sun4i_crtc_atomic_flush(struct drm_crtc *crtc,
51 struct drm_crtc_state *old_state)
52{
53 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
Daniel Vettera33e93d2016-06-08 14:18:58 +020054 struct drm_pending_vblank_event *event = crtc->state->event;
Maxime Ripard9026e0d2015-10-29 09:36:23 +010055
56 DRM_DEBUG_DRIVER("Committing plane changes\n");
57
Icenowy Zheng87969332017-05-17 22:47:17 +080058 sunxi_engine_commit(scrtc->engine);
Daniel Vettera33e93d2016-06-08 14:18:58 +020059
60 if (event) {
61 crtc->state->event = NULL;
62
63 spin_lock_irq(&crtc->dev->event_lock);
64 if (drm_crtc_vblank_get(crtc) == 0)
65 drm_crtc_arm_vblank_event(crtc, event);
66 else
67 drm_crtc_send_vblank_event(crtc, event);
68 spin_unlock_irq(&crtc->dev->event_lock);
69 }
Maxime Ripard9026e0d2015-10-29 09:36:23 +010070}
71
Laurent Pinchart64581712017-06-30 12:36:45 +030072static void sun4i_crtc_atomic_disable(struct drm_crtc *crtc,
73 struct drm_crtc_state *old_state)
Maxime Ripard9026e0d2015-10-29 09:36:23 +010074{
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
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +030090static void sun4i_crtc_atomic_enable(struct drm_crtc *crtc,
91 struct drm_crtc_state *old_state)
Maxime Ripard9026e0d2015-10-29 09:36:23 +010092{
93 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
Maxime Ripard9026e0d2015-10-29 09:36:23 +010094
95 DRM_DEBUG_DRIVER("Enabling the CRTC\n");
96
Chen-Yu Tsai3c64fb32017-02-23 16:05:43 +080097 sun4i_tcon_enable(scrtc->tcon);
Maxime Ripard9026e0d2015-10-29 09:36:23 +010098}
99
100static const struct drm_crtc_helper_funcs sun4i_crtc_helper_funcs = {
101 .atomic_begin = sun4i_crtc_atomic_begin,
102 .atomic_flush = sun4i_crtc_atomic_flush,
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +0300103 .atomic_enable = sun4i_crtc_atomic_enable,
Laurent Pinchart64581712017-06-30 12:36:45 +0300104 .atomic_disable = sun4i_crtc_atomic_disable,
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100105};
106
Shawn Guo50480a72017-02-07 17:16:31 +0800107static int sun4i_crtc_enable_vblank(struct drm_crtc *crtc)
108{
109 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
Shawn Guo50480a72017-02-07 17:16:31 +0800110
111 DRM_DEBUG_DRIVER("Enabling VBLANK on crtc %p\n", crtc);
112
Chen-Yu Tsai3c64fb32017-02-23 16:05:43 +0800113 sun4i_tcon_enable_vblank(scrtc->tcon, true);
Shawn Guo50480a72017-02-07 17:16:31 +0800114
115 return 0;
116}
117
118static void sun4i_crtc_disable_vblank(struct drm_crtc *crtc)
119{
120 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
Shawn Guo50480a72017-02-07 17:16:31 +0800121
122 DRM_DEBUG_DRIVER("Disabling VBLANK on crtc %p\n", crtc);
123
Chen-Yu Tsai3c64fb32017-02-23 16:05:43 +0800124 sun4i_tcon_enable_vblank(scrtc->tcon, false);
Shawn Guo50480a72017-02-07 17:16:31 +0800125}
126
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100127static const struct drm_crtc_funcs sun4i_crtc_funcs = {
128 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
129 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
130 .destroy = drm_crtc_cleanup,
131 .page_flip = drm_atomic_helper_page_flip,
132 .reset = drm_atomic_helper_crtc_reset,
133 .set_config = drm_atomic_helper_set_config,
Shawn Guo50480a72017-02-07 17:16:31 +0800134 .enable_vblank = sun4i_crtc_enable_vblank,
135 .disable_vblank = sun4i_crtc_disable_vblank,
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100136};
137
Chen-Yu Tsai18c3b302017-03-09 18:05:28 +0800138struct sun4i_crtc *sun4i_crtc_init(struct drm_device *drm,
Icenowy Zheng87969332017-05-17 22:47:17 +0800139 struct sunxi_engine *engine,
Chen-Yu Tsai18c3b302017-03-09 18:05:28 +0800140 struct sun4i_tcon *tcon)
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100141{
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100142 struct sun4i_crtc *scrtc;
Icenowy Zheng7921e142017-05-15 00:30:36 +0800143 struct drm_plane **planes;
Chen-Yu Tsaidcd21582017-02-23 16:05:38 +0800144 struct drm_plane *primary = NULL, *cursor = NULL;
145 int ret, i;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100146
147 scrtc = devm_kzalloc(drm->dev, sizeof(*scrtc), GFP_KERNEL);
148 if (!scrtc)
Chen-Yu Tsaiea411fd2017-02-17 11:13:30 +0800149 return ERR_PTR(-ENOMEM);
Icenowy Zheng87969332017-05-17 22:47:17 +0800150 scrtc->engine = engine;
Chen-Yu Tsai18c3b302017-03-09 18:05:28 +0800151 scrtc->tcon = tcon;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100152
Chen-Yu Tsaib3f266e2017-02-23 16:05:36 +0800153 /* Create our layers */
Icenowy Zheng87969332017-05-17 22:47:17 +0800154 planes = sunxi_engine_layers_init(drm, engine);
Icenowy Zheng7921e142017-05-15 00:30:36 +0800155 if (IS_ERR(planes)) {
Chen-Yu Tsaib3f266e2017-02-23 16:05:36 +0800156 dev_err(drm->dev, "Couldn't create the planes\n");
Chen-Yu Tsaidcd21582017-02-23 16:05:38 +0800157 return NULL;
158 }
159
160 /* find primary and cursor planes for drm_crtc_init_with_planes */
Icenowy Zheng7921e142017-05-15 00:30:36 +0800161 for (i = 0; planes[i]; i++) {
162 struct drm_plane *plane = planes[i];
Chen-Yu Tsaidcd21582017-02-23 16:05:38 +0800163
Icenowy Zheng7921e142017-05-15 00:30:36 +0800164 switch (plane->type) {
Chen-Yu Tsaidcd21582017-02-23 16:05:38 +0800165 case DRM_PLANE_TYPE_PRIMARY:
Icenowy Zheng7921e142017-05-15 00:30:36 +0800166 primary = plane;
Chen-Yu Tsaidcd21582017-02-23 16:05:38 +0800167 break;
168 case DRM_PLANE_TYPE_CURSOR:
Icenowy Zheng7921e142017-05-15 00:30:36 +0800169 cursor = plane;
Chen-Yu Tsaidcd21582017-02-23 16:05:38 +0800170 break;
171 default:
172 break;
173 }
Chen-Yu Tsaib3f266e2017-02-23 16:05:36 +0800174 }
175
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100176 ret = drm_crtc_init_with_planes(drm, &scrtc->crtc,
Chen-Yu Tsaidcd21582017-02-23 16:05:38 +0800177 primary,
178 cursor,
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100179 &sun4i_crtc_funcs,
180 NULL);
181 if (ret) {
182 dev_err(drm->dev, "Couldn't init DRM CRTC\n");
Chen-Yu Tsaiea411fd2017-02-17 11:13:30 +0800183 return ERR_PTR(ret);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100184 }
185
186 drm_crtc_helper_add(&scrtc->crtc, &sun4i_crtc_helper_funcs);
187
Chen-Yu Tsai75448602017-02-23 16:05:34 +0800188 /* Set crtc.port to output port node of the tcon */
Chen-Yu Tsaie4cdcb72017-03-09 18:05:26 +0800189 scrtc->crtc.port = of_graph_get_port_by_id(scrtc->tcon->dev->of_node,
Chen-Yu Tsai75448602017-02-23 16:05:34 +0800190 1);
191
Chen-Yu Tsaia5154a42017-02-23 16:05:39 +0800192 /* Set possible_crtcs to this crtc for overlay planes */
Icenowy Zheng7921e142017-05-15 00:30:36 +0800193 for (i = 0; planes[i]; i++) {
Chen-Yu Tsaia5154a42017-02-23 16:05:39 +0800194 uint32_t possible_crtcs = BIT(drm_crtc_index(&scrtc->crtc));
Icenowy Zheng7921e142017-05-15 00:30:36 +0800195 struct drm_plane *plane = planes[i];
Chen-Yu Tsaia5154a42017-02-23 16:05:39 +0800196
Icenowy Zheng7921e142017-05-15 00:30:36 +0800197 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
198 plane->possible_crtcs = possible_crtcs;
Chen-Yu Tsaia5154a42017-02-23 16:05:39 +0800199 }
200
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100201 return scrtc;
202}