blob: eec13cea3144b45d6720b119fc553f97fe5728f6 [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"
31#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);
54 struct sun4i_drv *drv = scrtc->drv;
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
59 sun4i_backend_commit(drv->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);
76 struct sun4i_drv *drv = scrtc->drv;
77
78 DRM_DEBUG_DRIVER("Disabling the CRTC\n");
79
80 sun4i_tcon_disable(drv->tcon);
Maxime Ripard2cd36832016-06-20 12:20:59 +020081
82 if (crtc->state->event && !crtc->state->active) {
83 spin_lock_irq(&crtc->dev->event_lock);
84 drm_crtc_send_vblank_event(crtc, crtc->state->event);
85 spin_unlock_irq(&crtc->dev->event_lock);
86
87 crtc->state->event = NULL;
88 }
Maxime Ripard9026e0d2015-10-29 09:36:23 +010089}
90
91static void sun4i_crtc_enable(struct drm_crtc *crtc)
92{
93 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
94 struct sun4i_drv *drv = scrtc->drv;
95
96 DRM_DEBUG_DRIVER("Enabling the CRTC\n");
97
98 sun4i_tcon_enable(drv->tcon);
99}
100
101static const struct drm_crtc_helper_funcs sun4i_crtc_helper_funcs = {
102 .atomic_begin = sun4i_crtc_atomic_begin,
103 .atomic_flush = sun4i_crtc_atomic_flush,
104 .disable = sun4i_crtc_disable,
105 .enable = sun4i_crtc_enable,
106};
107
Shawn Guo50480a72017-02-07 17:16:31 +0800108static int sun4i_crtc_enable_vblank(struct drm_crtc *crtc)
109{
110 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
111 struct sun4i_drv *drv = scrtc->drv;
112
113 DRM_DEBUG_DRIVER("Enabling VBLANK on crtc %p\n", crtc);
114
115 sun4i_tcon_enable_vblank(drv->tcon, true);
116
117 return 0;
118}
119
120static void sun4i_crtc_disable_vblank(struct drm_crtc *crtc)
121{
122 struct sun4i_crtc *scrtc = drm_crtc_to_sun4i_crtc(crtc);
123 struct sun4i_drv *drv = scrtc->drv;
124
125 DRM_DEBUG_DRIVER("Disabling VBLANK on crtc %p\n", crtc);
126
127 sun4i_tcon_enable_vblank(drv->tcon, false);
128}
129
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100130static const struct drm_crtc_funcs sun4i_crtc_funcs = {
131 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
132 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
133 .destroy = drm_crtc_cleanup,
134 .page_flip = drm_atomic_helper_page_flip,
135 .reset = drm_atomic_helper_crtc_reset,
136 .set_config = drm_atomic_helper_set_config,
Shawn Guo50480a72017-02-07 17:16:31 +0800137 .enable_vblank = sun4i_crtc_enable_vblank,
138 .disable_vblank = sun4i_crtc_disable_vblank,
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100139};
140
141struct sun4i_crtc *sun4i_crtc_init(struct drm_device *drm)
142{
143 struct sun4i_drv *drv = drm->dev_private;
144 struct sun4i_crtc *scrtc;
145 int ret;
146
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);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100150 scrtc->drv = drv;
151
152 ret = drm_crtc_init_with_planes(drm, &scrtc->crtc,
153 drv->primary,
154 NULL,
155 &sun4i_crtc_funcs,
156 NULL);
157 if (ret) {
158 dev_err(drm->dev, "Couldn't init DRM CRTC\n");
Chen-Yu Tsaiea411fd2017-02-17 11:13:30 +0800159 return ERR_PTR(ret);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100160 }
161
162 drm_crtc_helper_add(&scrtc->crtc, &sun4i_crtc_helper_funcs);
163
Chen-Yu Tsai75448602017-02-23 16:05:34 +0800164 /* Set crtc.port to output port node of the tcon */
165 scrtc->crtc.port = of_graph_get_port_by_id(drv->tcon->dev->of_node,
166 1);
167
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100168 return scrtc;
169}