blob: c07697902ab12247afa250f9facb1ed0ba81f641 [file] [log] [blame]
Maxime Ripard29e57fa2015-10-29 09:37:32 +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 <linux/clk.h>
14
15#include <drm/drmP.h>
16#include <drm/drm_atomic_helper.h>
17#include <drm/drm_crtc_helper.h>
18#include <drm/drm_panel.h>
19
20#include "sun4i_drv.h"
21#include "sun4i_tcon.h"
22
23struct sun4i_rgb {
24 struct drm_connector connector;
25 struct drm_encoder encoder;
26
27 struct sun4i_drv *drv;
28};
29
30static inline struct sun4i_rgb *
31drm_connector_to_sun4i_rgb(struct drm_connector *connector)
32{
33 return container_of(connector, struct sun4i_rgb,
34 connector);
35}
36
37static inline struct sun4i_rgb *
38drm_encoder_to_sun4i_rgb(struct drm_encoder *encoder)
39{
40 return container_of(encoder, struct sun4i_rgb,
41 encoder);
42}
43
44static int sun4i_rgb_get_modes(struct drm_connector *connector)
45{
46 struct sun4i_rgb *rgb =
47 drm_connector_to_sun4i_rgb(connector);
48 struct sun4i_drv *drv = rgb->drv;
49 struct sun4i_tcon *tcon = drv->tcon;
50
51 return drm_panel_get_modes(tcon->panel);
52}
53
54static int sun4i_rgb_mode_valid(struct drm_connector *connector,
55 struct drm_display_mode *mode)
56{
Maxime Ripardbb43d402016-04-21 11:25:26 +020057 struct sun4i_rgb *rgb = drm_connector_to_sun4i_rgb(connector);
58 struct sun4i_drv *drv = rgb->drv;
59 struct sun4i_tcon *tcon = drv->tcon;
Maxime Ripard29e57fa2015-10-29 09:37:32 +010060 u32 hsync = mode->hsync_end - mode->hsync_start;
61 u32 vsync = mode->vsync_end - mode->vsync_start;
Maxime Ripardbb43d402016-04-21 11:25:26 +020062 unsigned long rate = mode->clock * 1000;
63 long rounded_rate;
Maxime Ripard29e57fa2015-10-29 09:37:32 +010064
65 DRM_DEBUG_DRIVER("Validating modes...\n");
66
67 if (hsync < 1)
68 return MODE_HSYNC_NARROW;
69
70 if (hsync > 0x3ff)
71 return MODE_HSYNC_WIDE;
72
73 if ((mode->hdisplay < 1) || (mode->htotal < 1))
74 return MODE_H_ILLEGAL;
75
76 if ((mode->hdisplay > 0x7ff) || (mode->htotal > 0xfff))
77 return MODE_BAD_HVALUE;
78
79 DRM_DEBUG_DRIVER("Horizontal parameters OK\n");
80
81 if (vsync < 1)
82 return MODE_VSYNC_NARROW;
83
84 if (vsync > 0x3ff)
85 return MODE_VSYNC_WIDE;
86
87 if ((mode->vdisplay < 1) || (mode->vtotal < 1))
88 return MODE_V_ILLEGAL;
89
90 if ((mode->vdisplay > 0x7ff) || (mode->vtotal > 0xfff))
91 return MODE_BAD_VVALUE;
92
93 DRM_DEBUG_DRIVER("Vertical parameters OK\n");
94
Maxime Ripardbb43d402016-04-21 11:25:26 +020095 rounded_rate = clk_round_rate(tcon->dclk, rate);
96 if (rounded_rate < rate)
97 return MODE_CLOCK_LOW;
98
99 if (rounded_rate > rate)
100 return MODE_CLOCK_HIGH;
101
102 DRM_DEBUG_DRIVER("Clock rate OK\n");
103
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100104 return MODE_OK;
105}
106
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100107static struct drm_connector_helper_funcs sun4i_rgb_con_helper_funcs = {
108 .get_modes = sun4i_rgb_get_modes,
109 .mode_valid = sun4i_rgb_mode_valid,
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100110};
111
112static enum drm_connector_status
113sun4i_rgb_connector_detect(struct drm_connector *connector, bool force)
114{
115 return connector_status_connected;
116}
117
118static void
119sun4i_rgb_connector_destroy(struct drm_connector *connector)
120{
121 struct sun4i_rgb *rgb = drm_connector_to_sun4i_rgb(connector);
122 struct sun4i_drv *drv = rgb->drv;
123 struct sun4i_tcon *tcon = drv->tcon;
124
125 drm_panel_detach(tcon->panel);
126 drm_connector_cleanup(connector);
127}
128
129static struct drm_connector_funcs sun4i_rgb_con_funcs = {
130 .dpms = drm_atomic_helper_connector_dpms,
131 .detect = sun4i_rgb_connector_detect,
132 .fill_modes = drm_helper_probe_single_connector_modes,
133 .destroy = sun4i_rgb_connector_destroy,
134 .reset = drm_atomic_helper_connector_reset,
135 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
136 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
137};
138
139static int sun4i_rgb_atomic_check(struct drm_encoder *encoder,
140 struct drm_crtc_state *crtc_state,
141 struct drm_connector_state *conn_state)
142{
143 return 0;
144}
145
146static void sun4i_rgb_encoder_enable(struct drm_encoder *encoder)
147{
148 struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
149 struct sun4i_drv *drv = rgb->drv;
150 struct sun4i_tcon *tcon = drv->tcon;
151
152 DRM_DEBUG_DRIVER("Enabling RGB output\n");
153
Jonathan Liu4b309502016-08-30 16:55:00 +1000154 if (!IS_ERR(tcon->panel)) {
155 drm_panel_prepare(tcon->panel);
Maxime Ripard894f5a92016-04-11 12:16:33 +0200156 drm_panel_enable(tcon->panel);
Jonathan Liu4b309502016-08-30 16:55:00 +1000157 }
Maxime Ripard894f5a92016-04-11 12:16:33 +0200158
159 if (!IS_ERR(encoder->bridge))
160 drm_bridge_enable(encoder->bridge);
161
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100162 sun4i_tcon_channel_enable(tcon, 0);
163}
164
165static void sun4i_rgb_encoder_disable(struct drm_encoder *encoder)
166{
167 struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
168 struct sun4i_drv *drv = rgb->drv;
169 struct sun4i_tcon *tcon = drv->tcon;
170
171 DRM_DEBUG_DRIVER("Disabling RGB output\n");
172
173 sun4i_tcon_channel_disable(tcon, 0);
Maxime Ripard894f5a92016-04-11 12:16:33 +0200174
175 if (!IS_ERR(encoder->bridge))
176 drm_bridge_disable(encoder->bridge);
177
Jonathan Liu4b309502016-08-30 16:55:00 +1000178 if (!IS_ERR(tcon->panel)) {
Maxime Ripard894f5a92016-04-11 12:16:33 +0200179 drm_panel_disable(tcon->panel);
Jonathan Liu4b309502016-08-30 16:55:00 +1000180 drm_panel_unprepare(tcon->panel);
181 }
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100182}
183
184static void sun4i_rgb_encoder_mode_set(struct drm_encoder *encoder,
185 struct drm_display_mode *mode,
186 struct drm_display_mode *adjusted_mode)
187{
188 struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
189 struct sun4i_drv *drv = rgb->drv;
190 struct sun4i_tcon *tcon = drv->tcon;
191
192 sun4i_tcon0_mode_set(tcon, mode);
193
194 clk_set_rate(tcon->dclk, mode->crtc_clock * 1000);
195
196 /* FIXME: This seems to be board specific */
197 clk_set_phase(tcon->dclk, 120);
198}
199
200static struct drm_encoder_helper_funcs sun4i_rgb_enc_helper_funcs = {
201 .atomic_check = sun4i_rgb_atomic_check,
202 .mode_set = sun4i_rgb_encoder_mode_set,
203 .disable = sun4i_rgb_encoder_disable,
204 .enable = sun4i_rgb_encoder_enable,
205};
206
207static void sun4i_rgb_enc_destroy(struct drm_encoder *encoder)
208{
209 drm_encoder_cleanup(encoder);
210}
211
212static struct drm_encoder_funcs sun4i_rgb_enc_funcs = {
213 .destroy = sun4i_rgb_enc_destroy,
214};
215
216int sun4i_rgb_init(struct drm_device *drm)
217{
218 struct sun4i_drv *drv = drm->dev_private;
219 struct sun4i_tcon *tcon = drv->tcon;
Maxime Ripard894f5a92016-04-11 12:16:33 +0200220 struct drm_encoder *encoder;
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100221 struct sun4i_rgb *rgb;
222 int ret;
223
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100224 rgb = devm_kzalloc(drm->dev, sizeof(*rgb), GFP_KERNEL);
225 if (!rgb)
226 return -ENOMEM;
227 rgb->drv = drv;
Maxime Ripard894f5a92016-04-11 12:16:33 +0200228 encoder = &rgb->encoder;
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100229
Maxime Riparda8444c72016-07-20 10:35:06 +0200230 tcon->panel = sun4i_tcon_find_panel(tcon->dev->of_node);
Maxime Ripard894f5a92016-04-11 12:16:33 +0200231 encoder->bridge = sun4i_tcon_find_bridge(tcon->dev->of_node);
232 if (IS_ERR(tcon->panel) && IS_ERR(encoder->bridge)) {
233 dev_info(drm->dev, "No panel or bridge found... RGB output disabled\n");
Maxime Riparda8444c72016-07-20 10:35:06 +0200234 return 0;
235 }
236
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100237 drm_encoder_helper_add(&rgb->encoder,
238 &sun4i_rgb_enc_helper_funcs);
239 ret = drm_encoder_init(drm,
240 &rgb->encoder,
241 &sun4i_rgb_enc_funcs,
242 DRM_MODE_ENCODER_NONE,
243 NULL);
244 if (ret) {
245 dev_err(drm->dev, "Couldn't initialise the rgb encoder\n");
246 goto err_out;
247 }
248
249 /* The RGB encoder can only work with the TCON channel 0 */
250 rgb->encoder.possible_crtcs = BIT(0);
251
Maxime Ripard894f5a92016-04-11 12:16:33 +0200252 if (!IS_ERR(tcon->panel)) {
253 drm_connector_helper_add(&rgb->connector,
254 &sun4i_rgb_con_helper_funcs);
255 ret = drm_connector_init(drm, &rgb->connector,
256 &sun4i_rgb_con_funcs,
257 DRM_MODE_CONNECTOR_Unknown);
258 if (ret) {
259 dev_err(drm->dev, "Couldn't initialise the rgb connector\n");
260 goto err_cleanup_connector;
261 }
262
263 drm_mode_connector_attach_encoder(&rgb->connector,
264 &rgb->encoder);
265
266 ret = drm_panel_attach(tcon->panel, &rgb->connector);
267 if (ret) {
268 dev_err(drm->dev, "Couldn't attach our panel\n");
269 goto err_cleanup_connector;
270 }
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100271 }
272
Maxime Ripard894f5a92016-04-11 12:16:33 +0200273 if (!IS_ERR(encoder->bridge)) {
274 encoder->bridge->encoder = &rgb->encoder;
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100275
Maxime Ripard894f5a92016-04-11 12:16:33 +0200276 ret = drm_bridge_attach(drm, encoder->bridge);
277 if (ret) {
278 dev_err(drm->dev, "Couldn't attach our bridge\n");
279 goto err_cleanup_connector;
280 }
281 }
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100282
283 return 0;
284
285err_cleanup_connector:
286 drm_encoder_cleanup(&rgb->encoder);
287err_out:
288 return ret;
289}
290EXPORT_SYMBOL(sun4i_rgb_init);