xref: /linux/drivers/gpu/drm/sun4i/sun4i_rgb.c (revision 06c4a9c2ae606a8c9fab303613234804b9c45a64)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
229e57fabSMaxime Ripard /*
329e57fabSMaxime Ripard  * Copyright (C) 2015 Free Electrons
429e57fabSMaxime Ripard  * Copyright (C) 2015 NextThing Co
529e57fabSMaxime Ripard  *
629e57fabSMaxime Ripard  * Maxime Ripard <maxime.ripard@free-electrons.com>
729e57fabSMaxime Ripard  */
829e57fabSMaxime Ripard 
929e57fabSMaxime Ripard #include <linux/clk.h>
1029e57fabSMaxime Ripard 
1129e57fabSMaxime Ripard #include <drm/drm_atomic_helper.h>
12ee68c743SBoris Brezillon #include <drm/drm_bridge.h>
13ebc94461SRob Herring #include <drm/drm_of.h>
1429e57fabSMaxime Ripard #include <drm/drm_panel.h>
159c25a297SSam Ravnborg #include <drm/drm_print.h>
16fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h>
1729e57fabSMaxime Ripard 
18a5154a4dSChen-Yu Tsai #include "sun4i_crtc.h"
1929e57fabSMaxime Ripard #include "sun4i_tcon.h"
200c3ff44cSBaoyou Xie #include "sun4i_rgb.h"
2129e57fabSMaxime Ripard 
2229e57fabSMaxime Ripard struct sun4i_rgb {
2329e57fabSMaxime Ripard 	struct drm_connector	connector;
2429e57fabSMaxime Ripard 	struct drm_encoder	encoder;
2529e57fabSMaxime Ripard 
26b9c8506cSChen-Yu Tsai 	struct sun4i_tcon	*tcon;
271ce6f91cSMaxime Ripard 	struct drm_panel	*panel;
2819d0ffe0SMaxime Ripard 	struct drm_bridge	*bridge;
2929e57fabSMaxime Ripard };
3029e57fabSMaxime Ripard 
3129e57fabSMaxime Ripard static inline struct sun4i_rgb *
3229e57fabSMaxime Ripard drm_connector_to_sun4i_rgb(struct drm_connector *connector)
3329e57fabSMaxime Ripard {
3429e57fabSMaxime Ripard 	return container_of(connector, struct sun4i_rgb,
3529e57fabSMaxime Ripard 			    connector);
3629e57fabSMaxime Ripard }
3729e57fabSMaxime Ripard 
3829e57fabSMaxime Ripard static inline struct sun4i_rgb *
3929e57fabSMaxime Ripard drm_encoder_to_sun4i_rgb(struct drm_encoder *encoder)
4029e57fabSMaxime Ripard {
4129e57fabSMaxime Ripard 	return container_of(encoder, struct sun4i_rgb,
4229e57fabSMaxime Ripard 			    encoder);
4329e57fabSMaxime Ripard }
4429e57fabSMaxime Ripard 
4529e57fabSMaxime Ripard static int sun4i_rgb_get_modes(struct drm_connector *connector)
4629e57fabSMaxime Ripard {
4729e57fabSMaxime Ripard 	struct sun4i_rgb *rgb =
4829e57fabSMaxime Ripard 		drm_connector_to_sun4i_rgb(connector);
4929e57fabSMaxime Ripard 
50*06c4a9c2SSam Ravnborg 	return drm_panel_get_modes(rgb->panel, connector);
5129e57fabSMaxime Ripard }
5229e57fabSMaxime Ripard 
53e2771debSMaxime Ripard /*
54e2771debSMaxime Ripard  * VESA DMT defines a tolerance of 0.5% on the pixel clock, while the
55e2771debSMaxime Ripard  * CVT spec reuses that tolerance in its examples, so it looks to be a
56e2771debSMaxime Ripard  * good default tolerance for the EDID-based modes. Define it to 5 per
57e2771debSMaxime Ripard  * mille to avoid floating point operations.
58e2771debSMaxime Ripard  */
59e2771debSMaxime Ripard #define SUN4I_RGB_DOTCLOCK_TOLERANCE_PER_MILLE	5
60e2771debSMaxime Ripard 
61cde8b754SGiulio Benetti static enum drm_mode_status sun4i_rgb_mode_valid(struct drm_encoder *crtc,
62cde8b754SGiulio Benetti 						 const struct drm_display_mode *mode)
6329e57fabSMaxime Ripard {
64cde8b754SGiulio Benetti 	struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(crtc);
65b9c8506cSChen-Yu Tsai 	struct sun4i_tcon *tcon = rgb->tcon;
6629e57fabSMaxime Ripard 	u32 hsync = mode->hsync_end - mode->hsync_start;
6729e57fabSMaxime Ripard 	u32 vsync = mode->vsync_end - mode->vsync_start;
689f7dfd0cSMaxime Ripard 	unsigned long long rate = mode->clock * 1000;
69e2771debSMaxime Ripard 	unsigned long long lowest, highest;
709f7dfd0cSMaxime Ripard 	unsigned long long rounded_rate;
7129e57fabSMaxime Ripard 
7229e57fabSMaxime Ripard 	DRM_DEBUG_DRIVER("Validating modes...\n");
7329e57fabSMaxime Ripard 
7429e57fabSMaxime Ripard 	if (hsync < 1)
7529e57fabSMaxime Ripard 		return MODE_HSYNC_NARROW;
7629e57fabSMaxime Ripard 
7729e57fabSMaxime Ripard 	if (hsync > 0x3ff)
7829e57fabSMaxime Ripard 		return MODE_HSYNC_WIDE;
7929e57fabSMaxime Ripard 
8029e57fabSMaxime Ripard 	if ((mode->hdisplay < 1) || (mode->htotal < 1))
8129e57fabSMaxime Ripard 		return MODE_H_ILLEGAL;
8229e57fabSMaxime Ripard 
8329e57fabSMaxime Ripard 	if ((mode->hdisplay > 0x7ff) || (mode->htotal > 0xfff))
8429e57fabSMaxime Ripard 		return MODE_BAD_HVALUE;
8529e57fabSMaxime Ripard 
8629e57fabSMaxime Ripard 	DRM_DEBUG_DRIVER("Horizontal parameters OK\n");
8729e57fabSMaxime Ripard 
8829e57fabSMaxime Ripard 	if (vsync < 1)
8929e57fabSMaxime Ripard 		return MODE_VSYNC_NARROW;
9029e57fabSMaxime Ripard 
9129e57fabSMaxime Ripard 	if (vsync > 0x3ff)
9229e57fabSMaxime Ripard 		return MODE_VSYNC_WIDE;
9329e57fabSMaxime Ripard 
9429e57fabSMaxime Ripard 	if ((mode->vdisplay < 1) || (mode->vtotal < 1))
9529e57fabSMaxime Ripard 		return MODE_V_ILLEGAL;
9629e57fabSMaxime Ripard 
9729e57fabSMaxime Ripard 	if ((mode->vdisplay > 0x7ff) || (mode->vtotal > 0xfff))
9829e57fabSMaxime Ripard 		return MODE_BAD_VVALUE;
9929e57fabSMaxime Ripard 
10029e57fabSMaxime Ripard 	DRM_DEBUG_DRIVER("Vertical parameters OK\n");
10129e57fabSMaxime Ripard 
102e2771debSMaxime Ripard 	/*
103e2771debSMaxime Ripard 	 * TODO: We should use the struct display_timing if available
104e2771debSMaxime Ripard 	 * and / or trying to stretch the timings within that
105e2771debSMaxime Ripard 	 * tolerancy to take care of panels that we wouldn't be able
106e2771debSMaxime Ripard 	 * to have a exact match for.
107e2771debSMaxime Ripard 	 */
108e2771debSMaxime Ripard 	if (rgb->panel) {
109e2771debSMaxime Ripard 		DRM_DEBUG_DRIVER("RGB panel used, skipping clock rate checks");
110e2771debSMaxime Ripard 		goto out;
111e2771debSMaxime Ripard 	}
112e2771debSMaxime Ripard 
113e2771debSMaxime Ripard 	/*
114e2771debSMaxime Ripard 	 * That shouldn't ever happen unless something is really wrong, but it
115e2771debSMaxime Ripard 	 * doesn't harm to check.
116e2771debSMaxime Ripard 	 */
117e2771debSMaxime Ripard 	if (!rgb->bridge)
118e2771debSMaxime Ripard 		goto out;
119e2771debSMaxime Ripard 
1205af894bdSMaxime Ripard 	tcon->dclk_min_div = 6;
1215af894bdSMaxime Ripard 	tcon->dclk_max_div = 127;
122bb43d40dSMaxime Ripard 	rounded_rate = clk_round_rate(tcon->dclk, rate);
123e2771debSMaxime Ripard 
124e2771debSMaxime Ripard 	lowest = rate * (1000 - SUN4I_RGB_DOTCLOCK_TOLERANCE_PER_MILLE);
125e2771debSMaxime Ripard 	do_div(lowest, 1000);
126e2771debSMaxime Ripard 	if (rounded_rate < lowest)
127bb43d40dSMaxime Ripard 		return MODE_CLOCK_LOW;
128bb43d40dSMaxime Ripard 
129e2771debSMaxime Ripard 	highest = rate * (1000 + SUN4I_RGB_DOTCLOCK_TOLERANCE_PER_MILLE);
130e2771debSMaxime Ripard 	do_div(highest, 1000);
131e2771debSMaxime Ripard 	if (rounded_rate > highest)
132bb43d40dSMaxime Ripard 		return MODE_CLOCK_HIGH;
133bb43d40dSMaxime Ripard 
134e2771debSMaxime Ripard out:
135bb43d40dSMaxime Ripard 	DRM_DEBUG_DRIVER("Clock rate OK\n");
136bb43d40dSMaxime Ripard 
13729e57fabSMaxime Ripard 	return MODE_OK;
13829e57fabSMaxime Ripard }
13929e57fabSMaxime Ripard 
14029e57fabSMaxime Ripard static struct drm_connector_helper_funcs sun4i_rgb_con_helper_funcs = {
14129e57fabSMaxime Ripard 	.get_modes	= sun4i_rgb_get_modes,
14229e57fabSMaxime Ripard };
14329e57fabSMaxime Ripard 
14429e57fabSMaxime Ripard static void
14529e57fabSMaxime Ripard sun4i_rgb_connector_destroy(struct drm_connector *connector)
14629e57fabSMaxime Ripard {
14729e57fabSMaxime Ripard 	struct sun4i_rgb *rgb = drm_connector_to_sun4i_rgb(connector);
14829e57fabSMaxime Ripard 
1491ce6f91cSMaxime Ripard 	drm_panel_detach(rgb->panel);
15029e57fabSMaxime Ripard 	drm_connector_cleanup(connector);
15129e57fabSMaxime Ripard }
15229e57fabSMaxime Ripard 
15332b4d575SBhumika Goyal static const struct drm_connector_funcs sun4i_rgb_con_funcs = {
15429e57fabSMaxime Ripard 	.fill_modes		= drm_helper_probe_single_connector_modes,
15529e57fabSMaxime Ripard 	.destroy		= sun4i_rgb_connector_destroy,
15629e57fabSMaxime Ripard 	.reset			= drm_atomic_helper_connector_reset,
15729e57fabSMaxime Ripard 	.atomic_duplicate_state	= drm_atomic_helper_connector_duplicate_state,
15829e57fabSMaxime Ripard 	.atomic_destroy_state	= drm_atomic_helper_connector_destroy_state,
15929e57fabSMaxime Ripard };
16029e57fabSMaxime Ripard 
16129e57fabSMaxime Ripard static void sun4i_rgb_encoder_enable(struct drm_encoder *encoder)
16229e57fabSMaxime Ripard {
16329e57fabSMaxime Ripard 	struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
16429e57fabSMaxime Ripard 
16529e57fabSMaxime Ripard 	DRM_DEBUG_DRIVER("Enabling RGB output\n");
16629e57fabSMaxime Ripard 
1671ce6f91cSMaxime Ripard 	if (rgb->panel) {
1681ce6f91cSMaxime Ripard 		drm_panel_prepare(rgb->panel);
1691ce6f91cSMaxime Ripard 		drm_panel_enable(rgb->panel);
17029e57fabSMaxime Ripard 	}
17145e88f99SMaxime Ripard }
17229e57fabSMaxime Ripard 
17329e57fabSMaxime Ripard static void sun4i_rgb_encoder_disable(struct drm_encoder *encoder)
17429e57fabSMaxime Ripard {
17529e57fabSMaxime Ripard 	struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
17629e57fabSMaxime Ripard 
17729e57fabSMaxime Ripard 	DRM_DEBUG_DRIVER("Disabling RGB output\n");
17829e57fabSMaxime Ripard 
1791ce6f91cSMaxime Ripard 	if (rgb->panel) {
1801ce6f91cSMaxime Ripard 		drm_panel_disable(rgb->panel);
1811ce6f91cSMaxime Ripard 		drm_panel_unprepare(rgb->panel);
1824b309502SJonathan Liu 	}
18345e88f99SMaxime Ripard }
18429e57fabSMaxime Ripard 
18529e57fabSMaxime Ripard static struct drm_encoder_helper_funcs sun4i_rgb_enc_helper_funcs = {
18629e57fabSMaxime Ripard 	.disable	= sun4i_rgb_encoder_disable,
18729e57fabSMaxime Ripard 	.enable		= sun4i_rgb_encoder_enable,
188cde8b754SGiulio Benetti 	.mode_valid	= sun4i_rgb_mode_valid,
18929e57fabSMaxime Ripard };
19029e57fabSMaxime Ripard 
19129e57fabSMaxime Ripard static void sun4i_rgb_enc_destroy(struct drm_encoder *encoder)
19229e57fabSMaxime Ripard {
19329e57fabSMaxime Ripard 	drm_encoder_cleanup(encoder);
19429e57fabSMaxime Ripard }
19529e57fabSMaxime Ripard 
19629e57fabSMaxime Ripard static struct drm_encoder_funcs sun4i_rgb_enc_funcs = {
19729e57fabSMaxime Ripard 	.destroy	= sun4i_rgb_enc_destroy,
19829e57fabSMaxime Ripard };
19929e57fabSMaxime Ripard 
200b9c8506cSChen-Yu Tsai int sun4i_rgb_init(struct drm_device *drm, struct sun4i_tcon *tcon)
20129e57fabSMaxime Ripard {
202894f5a9fSMaxime Ripard 	struct drm_encoder *encoder;
20329e57fabSMaxime Ripard 	struct sun4i_rgb *rgb;
20429e57fabSMaxime Ripard 	int ret;
20529e57fabSMaxime Ripard 
20629e57fabSMaxime Ripard 	rgb = devm_kzalloc(drm->dev, sizeof(*rgb), GFP_KERNEL);
20729e57fabSMaxime Ripard 	if (!rgb)
20829e57fabSMaxime Ripard 		return -ENOMEM;
209b9c8506cSChen-Yu Tsai 	rgb->tcon = tcon;
210894f5a9fSMaxime Ripard 	encoder = &rgb->encoder;
21129e57fabSMaxime Ripard 
212ebc94461SRob Herring 	ret = drm_of_find_panel_or_bridge(tcon->dev->of_node, 1, 0,
21319d0ffe0SMaxime Ripard 					  &rgb->panel, &rgb->bridge);
214ebc94461SRob Herring 	if (ret) {
215894f5a9fSMaxime Ripard 		dev_info(drm->dev, "No panel or bridge found... RGB output disabled\n");
216a8444c7eSMaxime Ripard 		return 0;
217a8444c7eSMaxime Ripard 	}
218a8444c7eSMaxime Ripard 
21929e57fabSMaxime Ripard 	drm_encoder_helper_add(&rgb->encoder,
22029e57fabSMaxime Ripard 			       &sun4i_rgb_enc_helper_funcs);
22129e57fabSMaxime Ripard 	ret = drm_encoder_init(drm,
22229e57fabSMaxime Ripard 			       &rgb->encoder,
22329e57fabSMaxime Ripard 			       &sun4i_rgb_enc_funcs,
22429e57fabSMaxime Ripard 			       DRM_MODE_ENCODER_NONE,
22529e57fabSMaxime Ripard 			       NULL);
22629e57fabSMaxime Ripard 	if (ret) {
22729e57fabSMaxime Ripard 		dev_err(drm->dev, "Couldn't initialise the rgb encoder\n");
22829e57fabSMaxime Ripard 		goto err_out;
22929e57fabSMaxime Ripard 	}
23029e57fabSMaxime Ripard 
23129e57fabSMaxime Ripard 	/* The RGB encoder can only work with the TCON channel 0 */
232dbf8f9e4SVille Syrjälä 	rgb->encoder.possible_crtcs = drm_crtc_mask(&tcon->crtc->crtc);
23329e57fabSMaxime Ripard 
2341ce6f91cSMaxime Ripard 	if (rgb->panel) {
23529e57fabSMaxime Ripard 		drm_connector_helper_add(&rgb->connector,
23629e57fabSMaxime Ripard 					 &sun4i_rgb_con_helper_funcs);
23729e57fabSMaxime Ripard 		ret = drm_connector_init(drm, &rgb->connector,
23829e57fabSMaxime Ripard 					 &sun4i_rgb_con_funcs,
23929e57fabSMaxime Ripard 					 DRM_MODE_CONNECTOR_Unknown);
24029e57fabSMaxime Ripard 		if (ret) {
24129e57fabSMaxime Ripard 			dev_err(drm->dev, "Couldn't initialise the rgb connector\n");
24229e57fabSMaxime Ripard 			goto err_cleanup_connector;
24329e57fabSMaxime Ripard 		}
24429e57fabSMaxime Ripard 
245cde4c44dSDaniel Vetter 		drm_connector_attach_encoder(&rgb->connector,
246894f5a9fSMaxime Ripard 						  &rgb->encoder);
24729e57fabSMaxime Ripard 
2481ce6f91cSMaxime Ripard 		ret = drm_panel_attach(rgb->panel, &rgb->connector);
249894f5a9fSMaxime Ripard 		if (ret) {
250894f5a9fSMaxime Ripard 			dev_err(drm->dev, "Couldn't attach our panel\n");
251894f5a9fSMaxime Ripard 			goto err_cleanup_connector;
252894f5a9fSMaxime Ripard 		}
253894f5a9fSMaxime Ripard 	}
254894f5a9fSMaxime Ripard 
25519d0ffe0SMaxime Ripard 	if (rgb->bridge) {
25619d0ffe0SMaxime Ripard 		ret = drm_bridge_attach(encoder, rgb->bridge, NULL);
257894f5a9fSMaxime Ripard 		if (ret) {
258894f5a9fSMaxime Ripard 			dev_err(drm->dev, "Couldn't attach our bridge\n");
259894f5a9fSMaxime Ripard 			goto err_cleanup_connector;
260894f5a9fSMaxime Ripard 		}
261894f5a9fSMaxime Ripard 	}
26229e57fabSMaxime Ripard 
26329e57fabSMaxime Ripard 	return 0;
26429e57fabSMaxime Ripard 
26529e57fabSMaxime Ripard err_cleanup_connector:
26629e57fabSMaxime Ripard 	drm_encoder_cleanup(&rgb->encoder);
26729e57fabSMaxime Ripard err_out:
26829e57fabSMaxime Ripard 	return ret;
26929e57fabSMaxime Ripard }
27029e57fabSMaxime Ripard EXPORT_SYMBOL(sun4i_rgb_init);
271