2 * Copyright (C) 2015 Free Electrons
3 * Copyright (C) 2015 NextThing Co
5 * Maxime Ripard <maxime.ripard@free-electrons.com>
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.
13 #include <linux/clk.h>
16 #include <drm/drm_atomic_helper.h>
17 #include <drm/drm_crtc_helper.h>
18 #include <drm/drm_of.h>
19 #include <drm/drm_panel.h>
21 #include "sun4i_crtc.h"
22 #include "sun4i_tcon.h"
23 #include "sun4i_rgb.h"
26 struct drm_connector connector;
27 struct drm_encoder encoder;
29 struct sun4i_tcon *tcon;
32 static inline struct sun4i_rgb *
33 drm_connector_to_sun4i_rgb(struct drm_connector *connector)
35 return container_of(connector, struct sun4i_rgb,
39 static inline struct sun4i_rgb *
40 drm_encoder_to_sun4i_rgb(struct drm_encoder *encoder)
42 return container_of(encoder, struct sun4i_rgb,
46 static int sun4i_rgb_get_modes(struct drm_connector *connector)
48 struct sun4i_rgb *rgb =
49 drm_connector_to_sun4i_rgb(connector);
50 struct sun4i_tcon *tcon = rgb->tcon;
52 return drm_panel_get_modes(tcon->panel);
55 static int sun4i_rgb_mode_valid(struct drm_connector *connector,
56 struct drm_display_mode *mode)
58 struct sun4i_rgb *rgb = drm_connector_to_sun4i_rgb(connector);
59 struct sun4i_tcon *tcon = rgb->tcon;
60 u32 hsync = mode->hsync_end - mode->hsync_start;
61 u32 vsync = mode->vsync_end - mode->vsync_start;
62 unsigned long rate = mode->clock * 1000;
65 DRM_DEBUG_DRIVER("Validating modes...\n");
68 return MODE_HSYNC_NARROW;
71 return MODE_HSYNC_WIDE;
73 if ((mode->hdisplay < 1) || (mode->htotal < 1))
74 return MODE_H_ILLEGAL;
76 if ((mode->hdisplay > 0x7ff) || (mode->htotal > 0xfff))
77 return MODE_BAD_HVALUE;
79 DRM_DEBUG_DRIVER("Horizontal parameters OK\n");
82 return MODE_VSYNC_NARROW;
85 return MODE_VSYNC_WIDE;
87 if ((mode->vdisplay < 1) || (mode->vtotal < 1))
88 return MODE_V_ILLEGAL;
90 if ((mode->vdisplay > 0x7ff) || (mode->vtotal > 0xfff))
91 return MODE_BAD_VVALUE;
93 DRM_DEBUG_DRIVER("Vertical parameters OK\n");
95 rounded_rate = clk_round_rate(tcon->dclk, rate);
96 if (rounded_rate < rate)
97 return MODE_CLOCK_LOW;
99 if (rounded_rate > rate)
100 return MODE_CLOCK_HIGH;
102 DRM_DEBUG_DRIVER("Clock rate OK\n");
107 static struct drm_connector_helper_funcs sun4i_rgb_con_helper_funcs = {
108 .get_modes = sun4i_rgb_get_modes,
109 .mode_valid = sun4i_rgb_mode_valid,
113 sun4i_rgb_connector_destroy(struct drm_connector *connector)
115 struct sun4i_rgb *rgb = drm_connector_to_sun4i_rgb(connector);
116 struct sun4i_tcon *tcon = rgb->tcon;
118 drm_panel_detach(tcon->panel);
119 drm_connector_cleanup(connector);
122 static const struct drm_connector_funcs sun4i_rgb_con_funcs = {
123 .fill_modes = drm_helper_probe_single_connector_modes,
124 .destroy = sun4i_rgb_connector_destroy,
125 .reset = drm_atomic_helper_connector_reset,
126 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
127 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
130 static void sun4i_rgb_encoder_enable(struct drm_encoder *encoder)
132 struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
133 struct sun4i_tcon *tcon = rgb->tcon;
135 DRM_DEBUG_DRIVER("Enabling RGB output\n");
137 if (!IS_ERR(tcon->panel))
138 drm_panel_prepare(tcon->panel);
140 sun4i_tcon_channel_enable(tcon, 0);
142 if (!IS_ERR(tcon->panel))
143 drm_panel_enable(tcon->panel);
146 static void sun4i_rgb_encoder_disable(struct drm_encoder *encoder)
148 struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
149 struct sun4i_tcon *tcon = rgb->tcon;
151 DRM_DEBUG_DRIVER("Disabling RGB output\n");
153 if (!IS_ERR(tcon->panel))
154 drm_panel_disable(tcon->panel);
156 sun4i_tcon_channel_disable(tcon, 0);
158 if (!IS_ERR(tcon->panel))
159 drm_panel_unprepare(tcon->panel);
162 static void sun4i_rgb_encoder_mode_set(struct drm_encoder *encoder,
163 struct drm_display_mode *mode,
164 struct drm_display_mode *adjusted_mode)
166 struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
167 struct sun4i_tcon *tcon = rgb->tcon;
169 sun4i_tcon0_mode_set(tcon, mode);
170 sun4i_tcon_set_mux(tcon, 0, encoder);
172 /* FIXME: This seems to be board specific */
173 clk_set_phase(tcon->dclk, 120);
176 static struct drm_encoder_helper_funcs sun4i_rgb_enc_helper_funcs = {
177 .mode_set = sun4i_rgb_encoder_mode_set,
178 .disable = sun4i_rgb_encoder_disable,
179 .enable = sun4i_rgb_encoder_enable,
182 static void sun4i_rgb_enc_destroy(struct drm_encoder *encoder)
184 drm_encoder_cleanup(encoder);
187 static struct drm_encoder_funcs sun4i_rgb_enc_funcs = {
188 .destroy = sun4i_rgb_enc_destroy,
191 int sun4i_rgb_init(struct drm_device *drm, struct sun4i_tcon *tcon)
193 struct drm_encoder *encoder;
194 struct drm_bridge *bridge;
195 struct sun4i_rgb *rgb;
198 rgb = devm_kzalloc(drm->dev, sizeof(*rgb), GFP_KERNEL);
202 encoder = &rgb->encoder;
204 ret = drm_of_find_panel_or_bridge(tcon->dev->of_node, 1, 0,
205 &tcon->panel, &bridge);
207 dev_info(drm->dev, "No panel or bridge found... RGB output disabled\n");
211 drm_encoder_helper_add(&rgb->encoder,
212 &sun4i_rgb_enc_helper_funcs);
213 ret = drm_encoder_init(drm,
215 &sun4i_rgb_enc_funcs,
216 DRM_MODE_ENCODER_NONE,
219 dev_err(drm->dev, "Couldn't initialise the rgb encoder\n");
223 /* The RGB encoder can only work with the TCON channel 0 */
224 rgb->encoder.possible_crtcs = BIT(drm_crtc_index(&tcon->crtc->crtc));
227 drm_connector_helper_add(&rgb->connector,
228 &sun4i_rgb_con_helper_funcs);
229 ret = drm_connector_init(drm, &rgb->connector,
230 &sun4i_rgb_con_funcs,
231 DRM_MODE_CONNECTOR_Unknown);
233 dev_err(drm->dev, "Couldn't initialise the rgb connector\n");
234 goto err_cleanup_connector;
237 drm_mode_connector_attach_encoder(&rgb->connector,
240 ret = drm_panel_attach(tcon->panel, &rgb->connector);
242 dev_err(drm->dev, "Couldn't attach our panel\n");
243 goto err_cleanup_connector;
248 ret = drm_bridge_attach(encoder, bridge, NULL);
250 dev_err(drm->dev, "Couldn't attach our bridge\n");
251 goto err_cleanup_connector;
257 err_cleanup_connector:
258 drm_encoder_cleanup(&rgb->encoder);
262 EXPORT_SYMBOL(sun4i_rgb_init);