Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2015 Free Electrons
4 * Copyright (C) 2015 NextThing Co
5 *
6 * Maxime Ripard <maxime.ripard@free-electrons.com>
7 */
8
9#include <linux/clk.h>
10
11#include <drm/drm_atomic_helper.h>
12#include <drm/drm_bridge.h>
13#include <drm/drm_of.h>
14#include <drm/drm_panel.h>
15#include <drm/drm_print.h>
16#include <drm/drm_probe_helper.h>
17#include <drm/drm_simple_kms_helper.h>
18
19#include "sun4i_crtc.h"
20#include "sun4i_tcon.h"
21#include "sun4i_rgb.h"
22
23struct sun4i_rgb {
24 struct drm_connector connector;
25 struct drm_encoder encoder;
26
27 struct sun4i_tcon *tcon;
28 struct drm_panel *panel;
29 struct drm_bridge *bridge;
30};
31
32static inline struct sun4i_rgb *
33drm_connector_to_sun4i_rgb(struct drm_connector *connector)
34{
35 return container_of(connector, struct sun4i_rgb,
36 connector);
37}
38
39static inline struct sun4i_rgb *
40drm_encoder_to_sun4i_rgb(struct drm_encoder *encoder)
41{
42 return container_of(encoder, struct sun4i_rgb,
43 encoder);
44}
45
46static int sun4i_rgb_get_modes(struct drm_connector *connector)
47{
48 struct sun4i_rgb *rgb =
49 drm_connector_to_sun4i_rgb(connector);
50
51 return drm_panel_get_modes(rgb->panel, connector);
52}
53
54/*
55 * VESA DMT defines a tolerance of 0.5% on the pixel clock, while the
56 * CVT spec reuses that tolerance in its examples, so it looks to be a
57 * good default tolerance for the EDID-based modes. Define it to 5 per
58 * mille to avoid floating point operations.
59 */
60#define SUN4I_RGB_DOTCLOCK_TOLERANCE_PER_MILLE 5
61
62static enum drm_mode_status sun4i_rgb_mode_valid(struct drm_encoder *crtc,
63 const struct drm_display_mode *mode)
64{
65 struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(crtc);
66 struct sun4i_tcon *tcon = rgb->tcon;
67 u32 hsync = mode->hsync_end - mode->hsync_start;
68 u32 vsync = mode->vsync_end - mode->vsync_start;
69 unsigned long long rate = mode->clock * 1000;
70 unsigned long long lowest, highest;
71 unsigned long long rounded_rate;
72
73 DRM_DEBUG_DRIVER("Validating modes...\n");
74
75 if (hsync < 1)
76 return MODE_HSYNC_NARROW;
77
78 if (hsync > 0x3ff)
79 return MODE_HSYNC_WIDE;
80
81 if ((mode->hdisplay < 1) || (mode->htotal < 1))
82 return MODE_H_ILLEGAL;
83
84 if ((mode->hdisplay > 0x7ff) || (mode->htotal > 0xfff))
85 return MODE_BAD_HVALUE;
86
87 DRM_DEBUG_DRIVER("Horizontal parameters OK\n");
88
89 if (vsync < 1)
90 return MODE_VSYNC_NARROW;
91
92 if (vsync > 0x3ff)
93 return MODE_VSYNC_WIDE;
94
95 if ((mode->vdisplay < 1) || (mode->vtotal < 1))
96 return MODE_V_ILLEGAL;
97
98 if ((mode->vdisplay > 0x7ff) || (mode->vtotal > 0xfff))
99 return MODE_BAD_VVALUE;
100
101 DRM_DEBUG_DRIVER("Vertical parameters OK\n");
102
103 /*
104 * TODO: We should use the struct display_timing if available
105 * and / or trying to stretch the timings within that
106 * tolerancy to take care of panels that we wouldn't be able
107 * to have a exact match for.
108 */
109 if (rgb->panel) {
110 DRM_DEBUG_DRIVER("RGB panel used, skipping clock rate checks");
111 goto out;
112 }
113
114 /*
115 * That shouldn't ever happen unless something is really wrong, but it
116 * doesn't harm to check.
117 */
118 if (!rgb->bridge)
119 goto out;
120
121 tcon->dclk_min_div = 6;
122 tcon->dclk_max_div = 127;
123 rounded_rate = clk_round_rate(tcon->dclk, rate);
124
125 lowest = rate * (1000 - SUN4I_RGB_DOTCLOCK_TOLERANCE_PER_MILLE);
126 do_div(lowest, 1000);
127 if (rounded_rate < lowest)
128 return MODE_CLOCK_LOW;
129
130 highest = rate * (1000 + SUN4I_RGB_DOTCLOCK_TOLERANCE_PER_MILLE);
131 do_div(highest, 1000);
132 if (rounded_rate > highest)
133 return MODE_CLOCK_HIGH;
134
135out:
136 DRM_DEBUG_DRIVER("Clock rate OK\n");
137
138 return MODE_OK;
139}
140
141static const struct drm_connector_helper_funcs sun4i_rgb_con_helper_funcs = {
142 .get_modes = sun4i_rgb_get_modes,
143};
144
145static void
146sun4i_rgb_connector_destroy(struct drm_connector *connector)
147{
148 drm_connector_cleanup(connector);
149}
150
151static const struct drm_connector_funcs sun4i_rgb_con_funcs = {
152 .fill_modes = drm_helper_probe_single_connector_modes,
153 .destroy = sun4i_rgb_connector_destroy,
154 .reset = drm_atomic_helper_connector_reset,
155 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
156 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
157};
158
159static void sun4i_rgb_encoder_enable(struct drm_encoder *encoder)
160{
161 struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
162
163 DRM_DEBUG_DRIVER("Enabling RGB output\n");
164
165 if (rgb->panel) {
166 drm_panel_prepare(rgb->panel);
167 drm_panel_enable(rgb->panel);
168 }
169}
170
171static void sun4i_rgb_encoder_disable(struct drm_encoder *encoder)
172{
173 struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
174
175 DRM_DEBUG_DRIVER("Disabling RGB output\n");
176
177 if (rgb->panel) {
178 drm_panel_disable(rgb->panel);
179 drm_panel_unprepare(rgb->panel);
180 }
181}
182
183static const struct drm_encoder_helper_funcs sun4i_rgb_enc_helper_funcs = {
184 .disable = sun4i_rgb_encoder_disable,
185 .enable = sun4i_rgb_encoder_enable,
186 .mode_valid = sun4i_rgb_mode_valid,
187};
188
189int sun4i_rgb_init(struct drm_device *drm, struct sun4i_tcon *tcon)
190{
191 struct drm_encoder *encoder;
192 struct sun4i_rgb *rgb;
193 int ret;
194
195 rgb = devm_kzalloc(drm->dev, sizeof(*rgb), GFP_KERNEL);
196 if (!rgb)
197 return -ENOMEM;
198 rgb->tcon = tcon;
199 encoder = &rgb->encoder;
200
201 ret = drm_of_find_panel_or_bridge(tcon->dev->of_node, 1, 0,
202 &rgb->panel, &rgb->bridge);
203 if (ret) {
204 dev_info(drm->dev, "No panel or bridge found... RGB output disabled\n");
205 return 0;
206 }
207
208 drm_encoder_helper_add(&rgb->encoder,
209 &sun4i_rgb_enc_helper_funcs);
210 ret = drm_simple_encoder_init(drm, &rgb->encoder,
211 DRM_MODE_ENCODER_NONE);
212 if (ret) {
213 dev_err(drm->dev, "Couldn't initialise the rgb encoder\n");
214 goto err_out;
215 }
216
217 /* The RGB encoder can only work with the TCON channel 0 */
218 rgb->encoder.possible_crtcs = drm_crtc_mask(&tcon->crtc->crtc);
219
220 if (rgb->panel) {
221 drm_connector_helper_add(&rgb->connector,
222 &sun4i_rgb_con_helper_funcs);
223 ret = drm_connector_init(drm, &rgb->connector,
224 &sun4i_rgb_con_funcs,
225 DRM_MODE_CONNECTOR_Unknown);
226 if (ret) {
227 dev_err(drm->dev, "Couldn't initialise the rgb connector\n");
228 goto err_cleanup_connector;
229 }
230
231 drm_connector_attach_encoder(&rgb->connector,
232 &rgb->encoder);
233 }
234
235 if (rgb->bridge) {
236 ret = drm_bridge_attach(encoder, rgb->bridge, NULL, 0);
237 if (ret)
238 goto err_cleanup_connector;
239 }
240
241 return 0;
242
243err_cleanup_connector:
244 drm_encoder_cleanup(&rgb->encoder);
245err_out:
246 return ret;
247}
248EXPORT_SYMBOL(sun4i_rgb_init);
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2015 Free Electrons
4 * Copyright (C) 2015 NextThing Co
5 *
6 * Maxime Ripard <maxime.ripard@free-electrons.com>
7 */
8
9#include <linux/clk.h>
10
11#include <drm/drm_atomic_helper.h>
12#include <drm/drm_of.h>
13#include <drm/drm_panel.h>
14#include <drm/drm_print.h>
15#include <drm/drm_probe_helper.h>
16
17#include "sun4i_crtc.h"
18#include "sun4i_tcon.h"
19#include "sun4i_rgb.h"
20
21struct sun4i_rgb {
22 struct drm_connector connector;
23 struct drm_encoder encoder;
24
25 struct sun4i_tcon *tcon;
26 struct drm_panel *panel;
27 struct drm_bridge *bridge;
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
49 return drm_panel_get_modes(rgb->panel);
50}
51
52/*
53 * VESA DMT defines a tolerance of 0.5% on the pixel clock, while the
54 * CVT spec reuses that tolerance in its examples, so it looks to be a
55 * good default tolerance for the EDID-based modes. Define it to 5 per
56 * mille to avoid floating point operations.
57 */
58#define SUN4I_RGB_DOTCLOCK_TOLERANCE_PER_MILLE 5
59
60static enum drm_mode_status sun4i_rgb_mode_valid(struct drm_encoder *crtc,
61 const struct drm_display_mode *mode)
62{
63 struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(crtc);
64 struct sun4i_tcon *tcon = rgb->tcon;
65 u32 hsync = mode->hsync_end - mode->hsync_start;
66 u32 vsync = mode->vsync_end - mode->vsync_start;
67 unsigned long long rate = mode->clock * 1000;
68 unsigned long long lowest, highest;
69 unsigned long long rounded_rate;
70
71 DRM_DEBUG_DRIVER("Validating modes...\n");
72
73 if (hsync < 1)
74 return MODE_HSYNC_NARROW;
75
76 if (hsync > 0x3ff)
77 return MODE_HSYNC_WIDE;
78
79 if ((mode->hdisplay < 1) || (mode->htotal < 1))
80 return MODE_H_ILLEGAL;
81
82 if ((mode->hdisplay > 0x7ff) || (mode->htotal > 0xfff))
83 return MODE_BAD_HVALUE;
84
85 DRM_DEBUG_DRIVER("Horizontal parameters OK\n");
86
87 if (vsync < 1)
88 return MODE_VSYNC_NARROW;
89
90 if (vsync > 0x3ff)
91 return MODE_VSYNC_WIDE;
92
93 if ((mode->vdisplay < 1) || (mode->vtotal < 1))
94 return MODE_V_ILLEGAL;
95
96 if ((mode->vdisplay > 0x7ff) || (mode->vtotal > 0xfff))
97 return MODE_BAD_VVALUE;
98
99 DRM_DEBUG_DRIVER("Vertical parameters OK\n");
100
101 /*
102 * TODO: We should use the struct display_timing if available
103 * and / or trying to stretch the timings within that
104 * tolerancy to take care of panels that we wouldn't be able
105 * to have a exact match for.
106 */
107 if (rgb->panel) {
108 DRM_DEBUG_DRIVER("RGB panel used, skipping clock rate checks");
109 goto out;
110 }
111
112 /*
113 * That shouldn't ever happen unless something is really wrong, but it
114 * doesn't harm to check.
115 */
116 if (!rgb->bridge)
117 goto out;
118
119 tcon->dclk_min_div = 6;
120 tcon->dclk_max_div = 127;
121 rounded_rate = clk_round_rate(tcon->dclk, rate);
122
123 lowest = rate * (1000 - SUN4I_RGB_DOTCLOCK_TOLERANCE_PER_MILLE);
124 do_div(lowest, 1000);
125 if (rounded_rate < lowest)
126 return MODE_CLOCK_LOW;
127
128 highest = rate * (1000 + SUN4I_RGB_DOTCLOCK_TOLERANCE_PER_MILLE);
129 do_div(highest, 1000);
130 if (rounded_rate > highest)
131 return MODE_CLOCK_HIGH;
132
133out:
134 DRM_DEBUG_DRIVER("Clock rate OK\n");
135
136 return MODE_OK;
137}
138
139static struct drm_connector_helper_funcs sun4i_rgb_con_helper_funcs = {
140 .get_modes = sun4i_rgb_get_modes,
141};
142
143static void
144sun4i_rgb_connector_destroy(struct drm_connector *connector)
145{
146 struct sun4i_rgb *rgb = drm_connector_to_sun4i_rgb(connector);
147
148 drm_panel_detach(rgb->panel);
149 drm_connector_cleanup(connector);
150}
151
152static const struct drm_connector_funcs sun4i_rgb_con_funcs = {
153 .fill_modes = drm_helper_probe_single_connector_modes,
154 .destroy = sun4i_rgb_connector_destroy,
155 .reset = drm_atomic_helper_connector_reset,
156 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
157 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
158};
159
160static void sun4i_rgb_encoder_enable(struct drm_encoder *encoder)
161{
162 struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
163
164 DRM_DEBUG_DRIVER("Enabling RGB output\n");
165
166 if (rgb->panel) {
167 drm_panel_prepare(rgb->panel);
168 drm_panel_enable(rgb->panel);
169 }
170}
171
172static void sun4i_rgb_encoder_disable(struct drm_encoder *encoder)
173{
174 struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
175
176 DRM_DEBUG_DRIVER("Disabling RGB output\n");
177
178 if (rgb->panel) {
179 drm_panel_disable(rgb->panel);
180 drm_panel_unprepare(rgb->panel);
181 }
182}
183
184static struct drm_encoder_helper_funcs sun4i_rgb_enc_helper_funcs = {
185 .disable = sun4i_rgb_encoder_disable,
186 .enable = sun4i_rgb_encoder_enable,
187 .mode_valid = sun4i_rgb_mode_valid,
188};
189
190static void sun4i_rgb_enc_destroy(struct drm_encoder *encoder)
191{
192 drm_encoder_cleanup(encoder);
193}
194
195static struct drm_encoder_funcs sun4i_rgb_enc_funcs = {
196 .destroy = sun4i_rgb_enc_destroy,
197};
198
199int sun4i_rgb_init(struct drm_device *drm, struct sun4i_tcon *tcon)
200{
201 struct drm_encoder *encoder;
202 struct sun4i_rgb *rgb;
203 int ret;
204
205 rgb = devm_kzalloc(drm->dev, sizeof(*rgb), GFP_KERNEL);
206 if (!rgb)
207 return -ENOMEM;
208 rgb->tcon = tcon;
209 encoder = &rgb->encoder;
210
211 ret = drm_of_find_panel_or_bridge(tcon->dev->of_node, 1, 0,
212 &rgb->panel, &rgb->bridge);
213 if (ret) {
214 dev_info(drm->dev, "No panel or bridge found... RGB output disabled\n");
215 return 0;
216 }
217
218 drm_encoder_helper_add(&rgb->encoder,
219 &sun4i_rgb_enc_helper_funcs);
220 ret = drm_encoder_init(drm,
221 &rgb->encoder,
222 &sun4i_rgb_enc_funcs,
223 DRM_MODE_ENCODER_NONE,
224 NULL);
225 if (ret) {
226 dev_err(drm->dev, "Couldn't initialise the rgb encoder\n");
227 goto err_out;
228 }
229
230 /* The RGB encoder can only work with the TCON channel 0 */
231 rgb->encoder.possible_crtcs = drm_crtc_mask(&tcon->crtc->crtc);
232
233 if (rgb->panel) {
234 drm_connector_helper_add(&rgb->connector,
235 &sun4i_rgb_con_helper_funcs);
236 ret = drm_connector_init(drm, &rgb->connector,
237 &sun4i_rgb_con_funcs,
238 DRM_MODE_CONNECTOR_Unknown);
239 if (ret) {
240 dev_err(drm->dev, "Couldn't initialise the rgb connector\n");
241 goto err_cleanup_connector;
242 }
243
244 drm_connector_attach_encoder(&rgb->connector,
245 &rgb->encoder);
246
247 ret = drm_panel_attach(rgb->panel, &rgb->connector);
248 if (ret) {
249 dev_err(drm->dev, "Couldn't attach our panel\n");
250 goto err_cleanup_connector;
251 }
252 }
253
254 if (rgb->bridge) {
255 ret = drm_bridge_attach(encoder, rgb->bridge, NULL);
256 if (ret) {
257 dev_err(drm->dev, "Couldn't attach our bridge\n");
258 goto err_cleanup_connector;
259 }
260 }
261
262 return 0;
263
264err_cleanup_connector:
265 drm_encoder_cleanup(&rgb->encoder);
266err_out:
267 return ret;
268}
269EXPORT_SYMBOL(sun4i_rgb_init);