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/*
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_of.h>
19#include <drm/drm_panel.h>
20
21#include "sun4i_crtc.h"
22#include "sun4i_tcon.h"
23#include "sun4i_rgb.h"
24
25struct sun4i_rgb {
26 struct drm_connector connector;
27 struct drm_encoder encoder;
28
29 struct sun4i_tcon *tcon;
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 struct sun4i_tcon *tcon = rgb->tcon;
51
52 return drm_panel_get_modes(tcon->panel);
53}
54
55static enum drm_mode_status sun4i_rgb_mode_valid(struct drm_encoder *crtc,
56 const struct drm_display_mode *mode)
57{
58 struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(crtc);
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;
63 long rounded_rate;
64
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
95 tcon->dclk_min_div = 6;
96 tcon->dclk_max_div = 127;
97 rounded_rate = clk_round_rate(tcon->dclk, rate);
98 if (rounded_rate < rate)
99 return MODE_CLOCK_LOW;
100
101 if (rounded_rate > rate)
102 return MODE_CLOCK_HIGH;
103
104 DRM_DEBUG_DRIVER("Clock rate OK\n");
105
106 return MODE_OK;
107}
108
109static struct drm_connector_helper_funcs sun4i_rgb_con_helper_funcs = {
110 .get_modes = sun4i_rgb_get_modes,
111};
112
113static void
114sun4i_rgb_connector_destroy(struct drm_connector *connector)
115{
116 struct sun4i_rgb *rgb = drm_connector_to_sun4i_rgb(connector);
117 struct sun4i_tcon *tcon = rgb->tcon;
118
119 drm_panel_detach(tcon->panel);
120 drm_connector_cleanup(connector);
121}
122
123static const struct drm_connector_funcs sun4i_rgb_con_funcs = {
124 .fill_modes = drm_helper_probe_single_connector_modes,
125 .destroy = sun4i_rgb_connector_destroy,
126 .reset = drm_atomic_helper_connector_reset,
127 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
128 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
129};
130
131static void sun4i_rgb_encoder_enable(struct drm_encoder *encoder)
132{
133 struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
134 struct sun4i_tcon *tcon = rgb->tcon;
135
136 DRM_DEBUG_DRIVER("Enabling RGB output\n");
137
138 if (!IS_ERR(tcon->panel)) {
139 drm_panel_prepare(tcon->panel);
140 drm_panel_enable(tcon->panel);
141 }
142}
143
144static void sun4i_rgb_encoder_disable(struct drm_encoder *encoder)
145{
146 struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
147 struct sun4i_tcon *tcon = rgb->tcon;
148
149 DRM_DEBUG_DRIVER("Disabling RGB output\n");
150
151 if (!IS_ERR(tcon->panel)) {
152 drm_panel_disable(tcon->panel);
153 drm_panel_unprepare(tcon->panel);
154 }
155}
156
157static struct drm_encoder_helper_funcs sun4i_rgb_enc_helper_funcs = {
158 .disable = sun4i_rgb_encoder_disable,
159 .enable = sun4i_rgb_encoder_enable,
160 .mode_valid = sun4i_rgb_mode_valid,
161};
162
163static void sun4i_rgb_enc_destroy(struct drm_encoder *encoder)
164{
165 drm_encoder_cleanup(encoder);
166}
167
168static struct drm_encoder_funcs sun4i_rgb_enc_funcs = {
169 .destroy = sun4i_rgb_enc_destroy,
170};
171
172int sun4i_rgb_init(struct drm_device *drm, struct sun4i_tcon *tcon)
173{
174 struct drm_encoder *encoder;
175 struct drm_bridge *bridge;
176 struct sun4i_rgb *rgb;
177 int ret;
178
179 rgb = devm_kzalloc(drm->dev, sizeof(*rgb), GFP_KERNEL);
180 if (!rgb)
181 return -ENOMEM;
182 rgb->tcon = tcon;
183 encoder = &rgb->encoder;
184
185 ret = drm_of_find_panel_or_bridge(tcon->dev->of_node, 1, 0,
186 &tcon->panel, &bridge);
187 if (ret) {
188 dev_info(drm->dev, "No panel or bridge found... RGB output disabled\n");
189 return 0;
190 }
191
192 drm_encoder_helper_add(&rgb->encoder,
193 &sun4i_rgb_enc_helper_funcs);
194 ret = drm_encoder_init(drm,
195 &rgb->encoder,
196 &sun4i_rgb_enc_funcs,
197 DRM_MODE_ENCODER_NONE,
198 NULL);
199 if (ret) {
200 dev_err(drm->dev, "Couldn't initialise the rgb encoder\n");
201 goto err_out;
202 }
203
204 /* The RGB encoder can only work with the TCON channel 0 */
205 rgb->encoder.possible_crtcs = BIT(drm_crtc_index(&tcon->crtc->crtc));
206
207 if (tcon->panel) {
208 drm_connector_helper_add(&rgb->connector,
209 &sun4i_rgb_con_helper_funcs);
210 ret = drm_connector_init(drm, &rgb->connector,
211 &sun4i_rgb_con_funcs,
212 DRM_MODE_CONNECTOR_Unknown);
213 if (ret) {
214 dev_err(drm->dev, "Couldn't initialise the rgb connector\n");
215 goto err_cleanup_connector;
216 }
217
218 drm_mode_connector_attach_encoder(&rgb->connector,
219 &rgb->encoder);
220
221 ret = drm_panel_attach(tcon->panel, &rgb->connector);
222 if (ret) {
223 dev_err(drm->dev, "Couldn't attach our panel\n");
224 goto err_cleanup_connector;
225 }
226 }
227
228 if (bridge) {
229 ret = drm_bridge_attach(encoder, bridge, NULL);
230 if (ret) {
231 dev_err(drm->dev, "Couldn't attach our bridge\n");
232 goto err_cleanup_connector;
233 }
234 }
235
236 return 0;
237
238err_cleanup_connector:
239 drm_encoder_cleanup(&rgb->encoder);
240err_out:
241 return ret;
242}
243EXPORT_SYMBOL(sun4i_rgb_init);