blob: 757208f5173115a553653b2f93f4c1de116cca7f [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"
Baoyou Xie0c3ff442016-09-08 18:59:22 +080022#include "sun4i_rgb.h"
Maxime Ripard29e57fa2015-10-29 09:37:32 +010023
24struct sun4i_rgb {
25 struct drm_connector connector;
26 struct drm_encoder encoder;
27
28 struct sun4i_drv *drv;
29};
30
31static inline struct sun4i_rgb *
32drm_connector_to_sun4i_rgb(struct drm_connector *connector)
33{
34 return container_of(connector, struct sun4i_rgb,
35 connector);
36}
37
38static inline struct sun4i_rgb *
39drm_encoder_to_sun4i_rgb(struct drm_encoder *encoder)
40{
41 return container_of(encoder, struct sun4i_rgb,
42 encoder);
43}
44
45static int sun4i_rgb_get_modes(struct drm_connector *connector)
46{
47 struct sun4i_rgb *rgb =
48 drm_connector_to_sun4i_rgb(connector);
49 struct sun4i_drv *drv = rgb->drv;
50 struct sun4i_tcon *tcon = drv->tcon;
51
52 return drm_panel_get_modes(tcon->panel);
53}
54
55static int sun4i_rgb_mode_valid(struct drm_connector *connector,
56 struct drm_display_mode *mode)
57{
Maxime Ripardbb43d402016-04-21 11:25:26 +020058 struct sun4i_rgb *rgb = drm_connector_to_sun4i_rgb(connector);
59 struct sun4i_drv *drv = rgb->drv;
60 struct sun4i_tcon *tcon = drv->tcon;
Maxime Ripard29e57fa2015-10-29 09:37:32 +010061 u32 hsync = mode->hsync_end - mode->hsync_start;
62 u32 vsync = mode->vsync_end - mode->vsync_start;
Maxime Ripardbb43d402016-04-21 11:25:26 +020063 unsigned long rate = mode->clock * 1000;
64 long rounded_rate;
Maxime Ripard29e57fa2015-10-29 09:37:32 +010065
66 DRM_DEBUG_DRIVER("Validating modes...\n");
67
68 if (hsync < 1)
69 return MODE_HSYNC_NARROW;
70
71 if (hsync > 0x3ff)
72 return MODE_HSYNC_WIDE;
73
74 if ((mode->hdisplay < 1) || (mode->htotal < 1))
75 return MODE_H_ILLEGAL;
76
77 if ((mode->hdisplay > 0x7ff) || (mode->htotal > 0xfff))
78 return MODE_BAD_HVALUE;
79
80 DRM_DEBUG_DRIVER("Horizontal parameters OK\n");
81
82 if (vsync < 1)
83 return MODE_VSYNC_NARROW;
84
85 if (vsync > 0x3ff)
86 return MODE_VSYNC_WIDE;
87
88 if ((mode->vdisplay < 1) || (mode->vtotal < 1))
89 return MODE_V_ILLEGAL;
90
91 if ((mode->vdisplay > 0x7ff) || (mode->vtotal > 0xfff))
92 return MODE_BAD_VVALUE;
93
94 DRM_DEBUG_DRIVER("Vertical parameters OK\n");
95
Maxime Ripardbb43d402016-04-21 11:25:26 +020096 rounded_rate = clk_round_rate(tcon->dclk, rate);
97 if (rounded_rate < rate)
98 return MODE_CLOCK_LOW;
99
100 if (rounded_rate > rate)
101 return MODE_CLOCK_HIGH;
102
103 DRM_DEBUG_DRIVER("Clock rate OK\n");
104
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100105 return MODE_OK;
106}
107
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100108static struct drm_connector_helper_funcs sun4i_rgb_con_helper_funcs = {
109 .get_modes = sun4i_rgb_get_modes,
110 .mode_valid = sun4i_rgb_mode_valid,
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100111};
112
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100113static void
114sun4i_rgb_connector_destroy(struct drm_connector *connector)
115{
116 struct sun4i_rgb *rgb = drm_connector_to_sun4i_rgb(connector);
117 struct sun4i_drv *drv = rgb->drv;
118 struct sun4i_tcon *tcon = drv->tcon;
119
120 drm_panel_detach(tcon->panel);
121 drm_connector_cleanup(connector);
122}
123
124static struct drm_connector_funcs sun4i_rgb_con_funcs = {
125 .dpms = drm_atomic_helper_connector_dpms,
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100126 .fill_modes = drm_helper_probe_single_connector_modes,
127 .destroy = sun4i_rgb_connector_destroy,
128 .reset = drm_atomic_helper_connector_reset,
129 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
130 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
131};
132
133static int sun4i_rgb_atomic_check(struct drm_encoder *encoder,
134 struct drm_crtc_state *crtc_state,
135 struct drm_connector_state *conn_state)
136{
137 return 0;
138}
139
140static void sun4i_rgb_encoder_enable(struct drm_encoder *encoder)
141{
142 struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
143 struct sun4i_drv *drv = rgb->drv;
144 struct sun4i_tcon *tcon = drv->tcon;
145
146 DRM_DEBUG_DRIVER("Enabling RGB output\n");
147
Jonathan Liu5647b252016-09-26 20:21:45 +1000148 if (!IS_ERR(tcon->panel))
Jonathan Liu4b309502016-08-30 16:55:00 +1000149 drm_panel_prepare(tcon->panel);
Maxime Ripard894f5a92016-04-11 12:16:33 +0200150
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100151 sun4i_tcon_channel_enable(tcon, 0);
Jonathan Liu5647b252016-09-26 20:21:45 +1000152
153 if (!IS_ERR(tcon->panel))
154 drm_panel_enable(tcon->panel);
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100155}
156
157static void sun4i_rgb_encoder_disable(struct drm_encoder *encoder)
158{
159 struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
160 struct sun4i_drv *drv = rgb->drv;
161 struct sun4i_tcon *tcon = drv->tcon;
162
163 DRM_DEBUG_DRIVER("Disabling RGB output\n");
164
Jonathan Liu5647b252016-09-26 20:21:45 +1000165 if (!IS_ERR(tcon->panel))
166 drm_panel_disable(tcon->panel);
167
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100168 sun4i_tcon_channel_disable(tcon, 0);
Maxime Ripard894f5a92016-04-11 12:16:33 +0200169
Jonathan Liu5647b252016-09-26 20:21:45 +1000170 if (!IS_ERR(tcon->panel))
Jonathan Liu4b309502016-08-30 16:55:00 +1000171 drm_panel_unprepare(tcon->panel);
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100172}
173
174static void sun4i_rgb_encoder_mode_set(struct drm_encoder *encoder,
175 struct drm_display_mode *mode,
176 struct drm_display_mode *adjusted_mode)
177{
178 struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
179 struct sun4i_drv *drv = rgb->drv;
180 struct sun4i_tcon *tcon = drv->tcon;
181
182 sun4i_tcon0_mode_set(tcon, mode);
183
184 clk_set_rate(tcon->dclk, mode->crtc_clock * 1000);
185
186 /* FIXME: This seems to be board specific */
187 clk_set_phase(tcon->dclk, 120);
188}
189
190static struct drm_encoder_helper_funcs sun4i_rgb_enc_helper_funcs = {
191 .atomic_check = sun4i_rgb_atomic_check,
192 .mode_set = sun4i_rgb_encoder_mode_set,
193 .disable = sun4i_rgb_encoder_disable,
194 .enable = sun4i_rgb_encoder_enable,
195};
196
197static void sun4i_rgb_enc_destroy(struct drm_encoder *encoder)
198{
199 drm_encoder_cleanup(encoder);
200}
201
202static struct drm_encoder_funcs sun4i_rgb_enc_funcs = {
203 .destroy = sun4i_rgb_enc_destroy,
204};
205
206int sun4i_rgb_init(struct drm_device *drm)
207{
208 struct sun4i_drv *drv = drm->dev_private;
209 struct sun4i_tcon *tcon = drv->tcon;
Maxime Ripard894f5a92016-04-11 12:16:33 +0200210 struct drm_encoder *encoder;
Laurent Pinchart3bb80f22016-11-28 17:59:08 +0200211 struct drm_bridge *bridge;
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100212 struct sun4i_rgb *rgb;
213 int ret;
214
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100215 rgb = devm_kzalloc(drm->dev, sizeof(*rgb), GFP_KERNEL);
216 if (!rgb)
217 return -ENOMEM;
218 rgb->drv = drv;
Maxime Ripard894f5a92016-04-11 12:16:33 +0200219 encoder = &rgb->encoder;
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100220
Maxime Riparda8444c72016-07-20 10:35:06 +0200221 tcon->panel = sun4i_tcon_find_panel(tcon->dev->of_node);
Laurent Pinchart3bb80f22016-11-28 17:59:08 +0200222 bridge = sun4i_tcon_find_bridge(tcon->dev->of_node);
223 if (IS_ERR(tcon->panel) && IS_ERR(bridge)) {
Maxime Ripard894f5a92016-04-11 12:16:33 +0200224 dev_info(drm->dev, "No panel or bridge found... RGB output disabled\n");
Maxime Riparda8444c72016-07-20 10:35:06 +0200225 return 0;
226 }
227
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100228 drm_encoder_helper_add(&rgb->encoder,
229 &sun4i_rgb_enc_helper_funcs);
230 ret = drm_encoder_init(drm,
231 &rgb->encoder,
232 &sun4i_rgb_enc_funcs,
233 DRM_MODE_ENCODER_NONE,
234 NULL);
235 if (ret) {
236 dev_err(drm->dev, "Couldn't initialise the rgb encoder\n");
237 goto err_out;
238 }
239
240 /* The RGB encoder can only work with the TCON channel 0 */
241 rgb->encoder.possible_crtcs = BIT(0);
242
Maxime Ripard894f5a92016-04-11 12:16:33 +0200243 if (!IS_ERR(tcon->panel)) {
244 drm_connector_helper_add(&rgb->connector,
245 &sun4i_rgb_con_helper_funcs);
246 ret = drm_connector_init(drm, &rgb->connector,
247 &sun4i_rgb_con_funcs,
248 DRM_MODE_CONNECTOR_Unknown);
249 if (ret) {
250 dev_err(drm->dev, "Couldn't initialise the rgb connector\n");
251 goto err_cleanup_connector;
252 }
253
254 drm_mode_connector_attach_encoder(&rgb->connector,
255 &rgb->encoder);
256
257 ret = drm_panel_attach(tcon->panel, &rgb->connector);
258 if (ret) {
259 dev_err(drm->dev, "Couldn't attach our panel\n");
260 goto err_cleanup_connector;
261 }
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100262 }
263
Laurent Pinchart3bb80f22016-11-28 17:59:08 +0200264 if (!IS_ERR(bridge)) {
265 ret = drm_bridge_attach(encoder, bridge, NULL);
Maxime Ripard894f5a92016-04-11 12:16:33 +0200266 if (ret) {
267 dev_err(drm->dev, "Couldn't attach our bridge\n");
268 goto err_cleanup_connector;
269 }
270 }
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100271
272 return 0;
273
274err_cleanup_connector:
275 drm_encoder_cleanup(&rgb->encoder);
276err_out:
277 return ret;
278}
279EXPORT_SYMBOL(sun4i_rgb_init);