xref: /linux/drivers/gpu/drm/sun4i/sun4i_rgb.c (revision 762f99f4f3cb41a775b5157dd761217beba65873)
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>
17f9f3a38dSThomas Zimmermann #include <drm/drm_simple_kms_helper.h>
1829e57fabSMaxime Ripard 
19a5154a4dSChen-Yu Tsai #include "sun4i_crtc.h"
2029e57fabSMaxime Ripard #include "sun4i_tcon.h"
210c3ff44cSBaoyou Xie #include "sun4i_rgb.h"
2229e57fabSMaxime Ripard 
2329e57fabSMaxime Ripard struct sun4i_rgb {
2429e57fabSMaxime Ripard 	struct drm_connector	connector;
2529e57fabSMaxime Ripard 	struct drm_encoder	encoder;
2629e57fabSMaxime Ripard 
27b9c8506cSChen-Yu Tsai 	struct sun4i_tcon	*tcon;
281ce6f91cSMaxime Ripard 	struct drm_panel	*panel;
2919d0ffe0SMaxime Ripard 	struct drm_bridge	*bridge;
3029e57fabSMaxime Ripard };
3129e57fabSMaxime Ripard 
3229e57fabSMaxime Ripard static inline struct sun4i_rgb *
drm_connector_to_sun4i_rgb(struct drm_connector * connector)3329e57fabSMaxime Ripard drm_connector_to_sun4i_rgb(struct drm_connector *connector)
3429e57fabSMaxime Ripard {
3529e57fabSMaxime Ripard 	return container_of(connector, struct sun4i_rgb,
3629e57fabSMaxime Ripard 			    connector);
3729e57fabSMaxime Ripard }
3829e57fabSMaxime Ripard 
3929e57fabSMaxime Ripard static inline struct sun4i_rgb *
drm_encoder_to_sun4i_rgb(struct drm_encoder * encoder)4029e57fabSMaxime Ripard drm_encoder_to_sun4i_rgb(struct drm_encoder *encoder)
4129e57fabSMaxime Ripard {
4229e57fabSMaxime Ripard 	return container_of(encoder, struct sun4i_rgb,
4329e57fabSMaxime Ripard 			    encoder);
4429e57fabSMaxime Ripard }
4529e57fabSMaxime Ripard 
sun4i_rgb_get_modes(struct drm_connector * connector)4629e57fabSMaxime Ripard static int sun4i_rgb_get_modes(struct drm_connector *connector)
4729e57fabSMaxime Ripard {
4829e57fabSMaxime Ripard 	struct sun4i_rgb *rgb =
4929e57fabSMaxime Ripard 		drm_connector_to_sun4i_rgb(connector);
5029e57fabSMaxime Ripard 
5106c4a9c2SSam Ravnborg 	return drm_panel_get_modes(rgb->panel, connector);
5229e57fabSMaxime Ripard }
5329e57fabSMaxime Ripard 
54e2771debSMaxime Ripard /*
55e2771debSMaxime Ripard  * VESA DMT defines a tolerance of 0.5% on the pixel clock, while the
56e2771debSMaxime Ripard  * CVT spec reuses that tolerance in its examples, so it looks to be a
57e2771debSMaxime Ripard  * good default tolerance for the EDID-based modes. Define it to 5 per
58e2771debSMaxime Ripard  * mille to avoid floating point operations.
59e2771debSMaxime Ripard  */
60e2771debSMaxime Ripard #define SUN4I_RGB_DOTCLOCK_TOLERANCE_PER_MILLE	5
61e2771debSMaxime Ripard 
sun4i_rgb_mode_valid(struct drm_encoder * crtc,const struct drm_display_mode * mode)62cde8b754SGiulio Benetti static enum drm_mode_status sun4i_rgb_mode_valid(struct drm_encoder *crtc,
63cde8b754SGiulio Benetti 						 const struct drm_display_mode *mode)
6429e57fabSMaxime Ripard {
65cde8b754SGiulio Benetti 	struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(crtc);
66b9c8506cSChen-Yu Tsai 	struct sun4i_tcon *tcon = rgb->tcon;
6729e57fabSMaxime Ripard 	u32 hsync = mode->hsync_end - mode->hsync_start;
6829e57fabSMaxime Ripard 	u32 vsync = mode->vsync_end - mode->vsync_start;
699f7dfd0cSMaxime Ripard 	unsigned long long rate = mode->clock * 1000;
70e2771debSMaxime Ripard 	unsigned long long lowest, highest;
719f7dfd0cSMaxime Ripard 	unsigned long long rounded_rate;
7229e57fabSMaxime Ripard 
7329e57fabSMaxime Ripard 	DRM_DEBUG_DRIVER("Validating modes...\n");
7429e57fabSMaxime Ripard 
7529e57fabSMaxime Ripard 	if (hsync < 1)
7629e57fabSMaxime Ripard 		return MODE_HSYNC_NARROW;
7729e57fabSMaxime Ripard 
7829e57fabSMaxime Ripard 	if (hsync > 0x3ff)
7929e57fabSMaxime Ripard 		return MODE_HSYNC_WIDE;
8029e57fabSMaxime Ripard 
8129e57fabSMaxime Ripard 	if ((mode->hdisplay < 1) || (mode->htotal < 1))
8229e57fabSMaxime Ripard 		return MODE_H_ILLEGAL;
8329e57fabSMaxime Ripard 
8429e57fabSMaxime Ripard 	if ((mode->hdisplay > 0x7ff) || (mode->htotal > 0xfff))
8529e57fabSMaxime Ripard 		return MODE_BAD_HVALUE;
8629e57fabSMaxime Ripard 
8729e57fabSMaxime Ripard 	DRM_DEBUG_DRIVER("Horizontal parameters OK\n");
8829e57fabSMaxime Ripard 
8929e57fabSMaxime Ripard 	if (vsync < 1)
9029e57fabSMaxime Ripard 		return MODE_VSYNC_NARROW;
9129e57fabSMaxime Ripard 
9229e57fabSMaxime Ripard 	if (vsync > 0x3ff)
9329e57fabSMaxime Ripard 		return MODE_VSYNC_WIDE;
9429e57fabSMaxime Ripard 
9529e57fabSMaxime Ripard 	if ((mode->vdisplay < 1) || (mode->vtotal < 1))
9629e57fabSMaxime Ripard 		return MODE_V_ILLEGAL;
9729e57fabSMaxime Ripard 
9829e57fabSMaxime Ripard 	if ((mode->vdisplay > 0x7ff) || (mode->vtotal > 0xfff))
9929e57fabSMaxime Ripard 		return MODE_BAD_VVALUE;
10029e57fabSMaxime Ripard 
10129e57fabSMaxime Ripard 	DRM_DEBUG_DRIVER("Vertical parameters OK\n");
10229e57fabSMaxime Ripard 
103e2771debSMaxime Ripard 	/*
104e2771debSMaxime Ripard 	 * TODO: We should use the struct display_timing if available
105e2771debSMaxime Ripard 	 * and / or trying to stretch the timings within that
106e2771debSMaxime Ripard 	 * tolerancy to take care of panels that we wouldn't be able
107e2771debSMaxime Ripard 	 * to have a exact match for.
108e2771debSMaxime Ripard 	 */
109e2771debSMaxime Ripard 	if (rgb->panel) {
110e2771debSMaxime Ripard 		DRM_DEBUG_DRIVER("RGB panel used, skipping clock rate checks");
111e2771debSMaxime Ripard 		goto out;
112e2771debSMaxime Ripard 	}
113e2771debSMaxime Ripard 
114e2771debSMaxime Ripard 	/*
115e2771debSMaxime Ripard 	 * That shouldn't ever happen unless something is really wrong, but it
116e2771debSMaxime Ripard 	 * doesn't harm to check.
117e2771debSMaxime Ripard 	 */
118e2771debSMaxime Ripard 	if (!rgb->bridge)
119e2771debSMaxime Ripard 		goto out;
120e2771debSMaxime Ripard 
1215af894bdSMaxime Ripard 	tcon->dclk_min_div = 6;
1225af894bdSMaxime Ripard 	tcon->dclk_max_div = 127;
123bb43d40dSMaxime Ripard 	rounded_rate = clk_round_rate(tcon->dclk, rate);
124e2771debSMaxime Ripard 
125e2771debSMaxime Ripard 	lowest = rate * (1000 - SUN4I_RGB_DOTCLOCK_TOLERANCE_PER_MILLE);
126e2771debSMaxime Ripard 	do_div(lowest, 1000);
127e2771debSMaxime Ripard 	if (rounded_rate < lowest)
128bb43d40dSMaxime Ripard 		return MODE_CLOCK_LOW;
129bb43d40dSMaxime Ripard 
130e2771debSMaxime Ripard 	highest = rate * (1000 + SUN4I_RGB_DOTCLOCK_TOLERANCE_PER_MILLE);
131e2771debSMaxime Ripard 	do_div(highest, 1000);
132e2771debSMaxime Ripard 	if (rounded_rate > highest)
133bb43d40dSMaxime Ripard 		return MODE_CLOCK_HIGH;
134bb43d40dSMaxime Ripard 
135e2771debSMaxime Ripard out:
136bb43d40dSMaxime Ripard 	DRM_DEBUG_DRIVER("Clock rate OK\n");
137bb43d40dSMaxime Ripard 
13829e57fabSMaxime Ripard 	return MODE_OK;
13929e57fabSMaxime Ripard }
14029e57fabSMaxime Ripard 
141f13478c9SRikard Falkeborn static const struct drm_connector_helper_funcs sun4i_rgb_con_helper_funcs = {
14229e57fabSMaxime Ripard 	.get_modes	= sun4i_rgb_get_modes,
14329e57fabSMaxime Ripard };
14429e57fabSMaxime Ripard 
14529e57fabSMaxime Ripard static void
sun4i_rgb_connector_destroy(struct drm_connector * connector)14629e57fabSMaxime Ripard sun4i_rgb_connector_destroy(struct drm_connector *connector)
14729e57fabSMaxime Ripard {
14829e57fabSMaxime Ripard 	drm_connector_cleanup(connector);
14929e57fabSMaxime Ripard }
15029e57fabSMaxime Ripard 
15132b4d575SBhumika Goyal static const struct drm_connector_funcs sun4i_rgb_con_funcs = {
15229e57fabSMaxime Ripard 	.fill_modes		= drm_helper_probe_single_connector_modes,
15329e57fabSMaxime Ripard 	.destroy		= sun4i_rgb_connector_destroy,
15429e57fabSMaxime Ripard 	.reset			= drm_atomic_helper_connector_reset,
15529e57fabSMaxime Ripard 	.atomic_duplicate_state	= drm_atomic_helper_connector_duplicate_state,
15629e57fabSMaxime Ripard 	.atomic_destroy_state	= drm_atomic_helper_connector_destroy_state,
15729e57fabSMaxime Ripard };
15829e57fabSMaxime Ripard 
sun4i_rgb_encoder_enable(struct drm_encoder * encoder)15929e57fabSMaxime Ripard static void sun4i_rgb_encoder_enable(struct drm_encoder *encoder)
16029e57fabSMaxime Ripard {
16129e57fabSMaxime Ripard 	struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
16229e57fabSMaxime Ripard 
16329e57fabSMaxime Ripard 	DRM_DEBUG_DRIVER("Enabling RGB output\n");
16429e57fabSMaxime Ripard 
1651ce6f91cSMaxime Ripard 	if (rgb->panel) {
1661ce6f91cSMaxime Ripard 		drm_panel_prepare(rgb->panel);
1671ce6f91cSMaxime Ripard 		drm_panel_enable(rgb->panel);
16829e57fabSMaxime Ripard 	}
16945e88f99SMaxime Ripard }
17029e57fabSMaxime Ripard 
sun4i_rgb_encoder_disable(struct drm_encoder * encoder)17129e57fabSMaxime Ripard static void sun4i_rgb_encoder_disable(struct drm_encoder *encoder)
17229e57fabSMaxime Ripard {
17329e57fabSMaxime Ripard 	struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
17429e57fabSMaxime Ripard 
17529e57fabSMaxime Ripard 	DRM_DEBUG_DRIVER("Disabling RGB output\n");
17629e57fabSMaxime Ripard 
1771ce6f91cSMaxime Ripard 	if (rgb->panel) {
1781ce6f91cSMaxime Ripard 		drm_panel_disable(rgb->panel);
1791ce6f91cSMaxime Ripard 		drm_panel_unprepare(rgb->panel);
1804b309502SJonathan Liu 	}
18145e88f99SMaxime Ripard }
18229e57fabSMaxime Ripard 
183f13478c9SRikard Falkeborn static const struct drm_encoder_helper_funcs sun4i_rgb_enc_helper_funcs = {
18429e57fabSMaxime Ripard 	.disable	= sun4i_rgb_encoder_disable,
18529e57fabSMaxime Ripard 	.enable		= sun4i_rgb_encoder_enable,
186cde8b754SGiulio Benetti 	.mode_valid	= sun4i_rgb_mode_valid,
18729e57fabSMaxime Ripard };
18829e57fabSMaxime Ripard 
sun4i_rgb_init(struct drm_device * drm,struct sun4i_tcon * tcon)189b9c8506cSChen-Yu Tsai int sun4i_rgb_init(struct drm_device *drm, struct sun4i_tcon *tcon)
19029e57fabSMaxime Ripard {
191894f5a9fSMaxime Ripard 	struct drm_encoder *encoder;
19229e57fabSMaxime Ripard 	struct sun4i_rgb *rgb;
19329e57fabSMaxime Ripard 	int ret;
19429e57fabSMaxime Ripard 
19529e57fabSMaxime Ripard 	rgb = devm_kzalloc(drm->dev, sizeof(*rgb), GFP_KERNEL);
19629e57fabSMaxime Ripard 	if (!rgb)
19729e57fabSMaxime Ripard 		return -ENOMEM;
198b9c8506cSChen-Yu Tsai 	rgb->tcon = tcon;
199894f5a9fSMaxime Ripard 	encoder = &rgb->encoder;
20029e57fabSMaxime Ripard 
201ebc94461SRob Herring 	ret = drm_of_find_panel_or_bridge(tcon->dev->of_node, 1, 0,
20219d0ffe0SMaxime Ripard 					  &rgb->panel, &rgb->bridge);
203ebc94461SRob Herring 	if (ret) {
204894f5a9fSMaxime Ripard 		dev_info(drm->dev, "No panel or bridge found... RGB output disabled\n");
205a8444c7eSMaxime Ripard 		return 0;
206a8444c7eSMaxime Ripard 	}
207a8444c7eSMaxime Ripard 
20829e57fabSMaxime Ripard 	drm_encoder_helper_add(&rgb->encoder,
20929e57fabSMaxime Ripard 			       &sun4i_rgb_enc_helper_funcs);
210f9f3a38dSThomas Zimmermann 	ret = drm_simple_encoder_init(drm, &rgb->encoder,
211f9f3a38dSThomas Zimmermann 				      DRM_MODE_ENCODER_NONE);
21229e57fabSMaxime Ripard 	if (ret) {
21329e57fabSMaxime Ripard 		dev_err(drm->dev, "Couldn't initialise the rgb encoder\n");
21429e57fabSMaxime Ripard 		goto err_out;
21529e57fabSMaxime Ripard 	}
21629e57fabSMaxime Ripard 
21729e57fabSMaxime Ripard 	/* The RGB encoder can only work with the TCON channel 0 */
218dbf8f9e4SVille Syrjälä 	rgb->encoder.possible_crtcs = drm_crtc_mask(&tcon->crtc->crtc);
21929e57fabSMaxime Ripard 
2201ce6f91cSMaxime Ripard 	if (rgb->panel) {
22129e57fabSMaxime Ripard 		drm_connector_helper_add(&rgb->connector,
22229e57fabSMaxime Ripard 					 &sun4i_rgb_con_helper_funcs);
22329e57fabSMaxime Ripard 		ret = drm_connector_init(drm, &rgb->connector,
22429e57fabSMaxime Ripard 					 &sun4i_rgb_con_funcs,
22529e57fabSMaxime Ripard 					 DRM_MODE_CONNECTOR_Unknown);
22629e57fabSMaxime Ripard 		if (ret) {
22729e57fabSMaxime Ripard 			dev_err(drm->dev, "Couldn't initialise the rgb connector\n");
22829e57fabSMaxime Ripard 			goto err_cleanup_connector;
22929e57fabSMaxime Ripard 		}
23029e57fabSMaxime Ripard 
231cde4c44dSDaniel Vetter 		drm_connector_attach_encoder(&rgb->connector,
232894f5a9fSMaxime Ripard 						  &rgb->encoder);
233894f5a9fSMaxime Ripard 	}
234894f5a9fSMaxime Ripard 
23519d0ffe0SMaxime Ripard 	if (rgb->bridge) {
236a25b988fSLaurent Pinchart 		ret = drm_bridge_attach(encoder, rgb->bridge, NULL, 0);
237*fb8d617fSLaurent Pinchart 		if (ret)
238894f5a9fSMaxime Ripard 			goto err_cleanup_connector;
239894f5a9fSMaxime Ripard 	}
24029e57fabSMaxime Ripard 
24129e57fabSMaxime Ripard 	return 0;
24229e57fabSMaxime Ripard 
24329e57fabSMaxime Ripard err_cleanup_connector:
24429e57fabSMaxime Ripard 	drm_encoder_cleanup(&rgb->encoder);
24529e57fabSMaxime Ripard err_out:
24629e57fabSMaxime Ripard 	return ret;
24729e57fabSMaxime Ripard }
24829e57fabSMaxime Ripard EXPORT_SYMBOL(sun4i_rgb_init);
249