xref: /linux/drivers/gpu/drm/sun4i/sun4i_rgb.c (revision 9f7dfd0cb50f93662b182c05e59987ba9db9505e)
129e57fabSMaxime Ripard /*
229e57fabSMaxime Ripard  * Copyright (C) 2015 Free Electrons
329e57fabSMaxime Ripard  * Copyright (C) 2015 NextThing Co
429e57fabSMaxime Ripard  *
529e57fabSMaxime Ripard  * Maxime Ripard <maxime.ripard@free-electrons.com>
629e57fabSMaxime Ripard  *
729e57fabSMaxime Ripard  * This program is free software; you can redistribute it and/or
829e57fabSMaxime Ripard  * modify it under the terms of the GNU General Public License as
929e57fabSMaxime Ripard  * published by the Free Software Foundation; either version 2 of
1029e57fabSMaxime Ripard  * the License, or (at your option) any later version.
1129e57fabSMaxime Ripard  */
1229e57fabSMaxime Ripard 
1329e57fabSMaxime Ripard #include <linux/clk.h>
1429e57fabSMaxime Ripard 
1529e57fabSMaxime Ripard #include <drm/drmP.h>
1629e57fabSMaxime Ripard #include <drm/drm_atomic_helper.h>
17ebc94461SRob Herring #include <drm/drm_of.h>
1829e57fabSMaxime Ripard #include <drm/drm_panel.h>
19fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h>
2029e57fabSMaxime Ripard 
21a5154a4dSChen-Yu Tsai #include "sun4i_crtc.h"
2229e57fabSMaxime Ripard #include "sun4i_tcon.h"
230c3ff44cSBaoyou Xie #include "sun4i_rgb.h"
2429e57fabSMaxime Ripard 
2529e57fabSMaxime Ripard struct sun4i_rgb {
2629e57fabSMaxime Ripard 	struct drm_connector	connector;
2729e57fabSMaxime Ripard 	struct drm_encoder	encoder;
2829e57fabSMaxime Ripard 
29b9c8506cSChen-Yu Tsai 	struct sun4i_tcon	*tcon;
301ce6f91cSMaxime Ripard 	struct drm_panel	*panel;
3119d0ffe0SMaxime Ripard 	struct drm_bridge	*bridge;
3229e57fabSMaxime Ripard };
3329e57fabSMaxime Ripard 
3429e57fabSMaxime Ripard static inline struct sun4i_rgb *
3529e57fabSMaxime Ripard drm_connector_to_sun4i_rgb(struct drm_connector *connector)
3629e57fabSMaxime Ripard {
3729e57fabSMaxime Ripard 	return container_of(connector, struct sun4i_rgb,
3829e57fabSMaxime Ripard 			    connector);
3929e57fabSMaxime Ripard }
4029e57fabSMaxime Ripard 
4129e57fabSMaxime Ripard static inline struct sun4i_rgb *
4229e57fabSMaxime Ripard drm_encoder_to_sun4i_rgb(struct drm_encoder *encoder)
4329e57fabSMaxime Ripard {
4429e57fabSMaxime Ripard 	return container_of(encoder, struct sun4i_rgb,
4529e57fabSMaxime Ripard 			    encoder);
4629e57fabSMaxime Ripard }
4729e57fabSMaxime Ripard 
4829e57fabSMaxime Ripard static int sun4i_rgb_get_modes(struct drm_connector *connector)
4929e57fabSMaxime Ripard {
5029e57fabSMaxime Ripard 	struct sun4i_rgb *rgb =
5129e57fabSMaxime Ripard 		drm_connector_to_sun4i_rgb(connector);
5229e57fabSMaxime Ripard 
531ce6f91cSMaxime Ripard 	return drm_panel_get_modes(rgb->panel);
5429e57fabSMaxime Ripard }
5529e57fabSMaxime Ripard 
56cde8b754SGiulio Benetti static enum drm_mode_status sun4i_rgb_mode_valid(struct drm_encoder *crtc,
57cde8b754SGiulio Benetti 						 const struct drm_display_mode *mode)
5829e57fabSMaxime Ripard {
59cde8b754SGiulio Benetti 	struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(crtc);
60b9c8506cSChen-Yu Tsai 	struct sun4i_tcon *tcon = rgb->tcon;
6129e57fabSMaxime Ripard 	u32 hsync = mode->hsync_end - mode->hsync_start;
6229e57fabSMaxime Ripard 	u32 vsync = mode->vsync_end - mode->vsync_start;
63*9f7dfd0cSMaxime Ripard 	unsigned long long rate = mode->clock * 1000;
64*9f7dfd0cSMaxime Ripard 	unsigned long long rounded_rate;
6529e57fabSMaxime Ripard 
6629e57fabSMaxime Ripard 	DRM_DEBUG_DRIVER("Validating modes...\n");
6729e57fabSMaxime Ripard 
6829e57fabSMaxime Ripard 	if (hsync < 1)
6929e57fabSMaxime Ripard 		return MODE_HSYNC_NARROW;
7029e57fabSMaxime Ripard 
7129e57fabSMaxime Ripard 	if (hsync > 0x3ff)
7229e57fabSMaxime Ripard 		return MODE_HSYNC_WIDE;
7329e57fabSMaxime Ripard 
7429e57fabSMaxime Ripard 	if ((mode->hdisplay < 1) || (mode->htotal < 1))
7529e57fabSMaxime Ripard 		return MODE_H_ILLEGAL;
7629e57fabSMaxime Ripard 
7729e57fabSMaxime Ripard 	if ((mode->hdisplay > 0x7ff) || (mode->htotal > 0xfff))
7829e57fabSMaxime Ripard 		return MODE_BAD_HVALUE;
7929e57fabSMaxime Ripard 
8029e57fabSMaxime Ripard 	DRM_DEBUG_DRIVER("Horizontal parameters OK\n");
8129e57fabSMaxime Ripard 
8229e57fabSMaxime Ripard 	if (vsync < 1)
8329e57fabSMaxime Ripard 		return MODE_VSYNC_NARROW;
8429e57fabSMaxime Ripard 
8529e57fabSMaxime Ripard 	if (vsync > 0x3ff)
8629e57fabSMaxime Ripard 		return MODE_VSYNC_WIDE;
8729e57fabSMaxime Ripard 
8829e57fabSMaxime Ripard 	if ((mode->vdisplay < 1) || (mode->vtotal < 1))
8929e57fabSMaxime Ripard 		return MODE_V_ILLEGAL;
9029e57fabSMaxime Ripard 
9129e57fabSMaxime Ripard 	if ((mode->vdisplay > 0x7ff) || (mode->vtotal > 0xfff))
9229e57fabSMaxime Ripard 		return MODE_BAD_VVALUE;
9329e57fabSMaxime Ripard 
9429e57fabSMaxime Ripard 	DRM_DEBUG_DRIVER("Vertical parameters OK\n");
9529e57fabSMaxime Ripard 
965af894bdSMaxime Ripard 	tcon->dclk_min_div = 6;
975af894bdSMaxime Ripard 	tcon->dclk_max_div = 127;
98bb43d40dSMaxime Ripard 	rounded_rate = clk_round_rate(tcon->dclk, rate);
99bb43d40dSMaxime Ripard 	if (rounded_rate < rate)
100bb43d40dSMaxime Ripard 		return MODE_CLOCK_LOW;
101bb43d40dSMaxime Ripard 
102bb43d40dSMaxime Ripard 	if (rounded_rate > rate)
103bb43d40dSMaxime Ripard 		return MODE_CLOCK_HIGH;
104bb43d40dSMaxime Ripard 
105bb43d40dSMaxime Ripard 	DRM_DEBUG_DRIVER("Clock rate OK\n");
106bb43d40dSMaxime Ripard 
10729e57fabSMaxime Ripard 	return MODE_OK;
10829e57fabSMaxime Ripard }
10929e57fabSMaxime Ripard 
11029e57fabSMaxime Ripard static struct drm_connector_helper_funcs sun4i_rgb_con_helper_funcs = {
11129e57fabSMaxime Ripard 	.get_modes	= sun4i_rgb_get_modes,
11229e57fabSMaxime Ripard };
11329e57fabSMaxime Ripard 
11429e57fabSMaxime Ripard static void
11529e57fabSMaxime Ripard sun4i_rgb_connector_destroy(struct drm_connector *connector)
11629e57fabSMaxime Ripard {
11729e57fabSMaxime Ripard 	struct sun4i_rgb *rgb = drm_connector_to_sun4i_rgb(connector);
11829e57fabSMaxime Ripard 
1191ce6f91cSMaxime Ripard 	drm_panel_detach(rgb->panel);
12029e57fabSMaxime Ripard 	drm_connector_cleanup(connector);
12129e57fabSMaxime Ripard }
12229e57fabSMaxime Ripard 
12332b4d575SBhumika Goyal static const struct drm_connector_funcs sun4i_rgb_con_funcs = {
12429e57fabSMaxime Ripard 	.fill_modes		= drm_helper_probe_single_connector_modes,
12529e57fabSMaxime Ripard 	.destroy		= sun4i_rgb_connector_destroy,
12629e57fabSMaxime Ripard 	.reset			= drm_atomic_helper_connector_reset,
12729e57fabSMaxime Ripard 	.atomic_duplicate_state	= drm_atomic_helper_connector_duplicate_state,
12829e57fabSMaxime Ripard 	.atomic_destroy_state	= drm_atomic_helper_connector_destroy_state,
12929e57fabSMaxime Ripard };
13029e57fabSMaxime Ripard 
13129e57fabSMaxime Ripard static void sun4i_rgb_encoder_enable(struct drm_encoder *encoder)
13229e57fabSMaxime Ripard {
13329e57fabSMaxime Ripard 	struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
13429e57fabSMaxime Ripard 
13529e57fabSMaxime Ripard 	DRM_DEBUG_DRIVER("Enabling RGB output\n");
13629e57fabSMaxime Ripard 
1371ce6f91cSMaxime Ripard 	if (rgb->panel) {
1381ce6f91cSMaxime Ripard 		drm_panel_prepare(rgb->panel);
1391ce6f91cSMaxime Ripard 		drm_panel_enable(rgb->panel);
14029e57fabSMaxime Ripard 	}
14145e88f99SMaxime Ripard }
14229e57fabSMaxime Ripard 
14329e57fabSMaxime Ripard static void sun4i_rgb_encoder_disable(struct drm_encoder *encoder)
14429e57fabSMaxime Ripard {
14529e57fabSMaxime Ripard 	struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
14629e57fabSMaxime Ripard 
14729e57fabSMaxime Ripard 	DRM_DEBUG_DRIVER("Disabling RGB output\n");
14829e57fabSMaxime Ripard 
1491ce6f91cSMaxime Ripard 	if (rgb->panel) {
1501ce6f91cSMaxime Ripard 		drm_panel_disable(rgb->panel);
1511ce6f91cSMaxime Ripard 		drm_panel_unprepare(rgb->panel);
1524b309502SJonathan Liu 	}
15345e88f99SMaxime Ripard }
15429e57fabSMaxime Ripard 
15529e57fabSMaxime Ripard static struct drm_encoder_helper_funcs sun4i_rgb_enc_helper_funcs = {
15629e57fabSMaxime Ripard 	.disable	= sun4i_rgb_encoder_disable,
15729e57fabSMaxime Ripard 	.enable		= sun4i_rgb_encoder_enable,
158cde8b754SGiulio Benetti 	.mode_valid	= sun4i_rgb_mode_valid,
15929e57fabSMaxime Ripard };
16029e57fabSMaxime Ripard 
16129e57fabSMaxime Ripard static void sun4i_rgb_enc_destroy(struct drm_encoder *encoder)
16229e57fabSMaxime Ripard {
16329e57fabSMaxime Ripard 	drm_encoder_cleanup(encoder);
16429e57fabSMaxime Ripard }
16529e57fabSMaxime Ripard 
16629e57fabSMaxime Ripard static struct drm_encoder_funcs sun4i_rgb_enc_funcs = {
16729e57fabSMaxime Ripard 	.destroy	= sun4i_rgb_enc_destroy,
16829e57fabSMaxime Ripard };
16929e57fabSMaxime Ripard 
170b9c8506cSChen-Yu Tsai int sun4i_rgb_init(struct drm_device *drm, struct sun4i_tcon *tcon)
17129e57fabSMaxime Ripard {
172894f5a9fSMaxime Ripard 	struct drm_encoder *encoder;
17329e57fabSMaxime Ripard 	struct sun4i_rgb *rgb;
17429e57fabSMaxime Ripard 	int ret;
17529e57fabSMaxime Ripard 
17629e57fabSMaxime Ripard 	rgb = devm_kzalloc(drm->dev, sizeof(*rgb), GFP_KERNEL);
17729e57fabSMaxime Ripard 	if (!rgb)
17829e57fabSMaxime Ripard 		return -ENOMEM;
179b9c8506cSChen-Yu Tsai 	rgb->tcon = tcon;
180894f5a9fSMaxime Ripard 	encoder = &rgb->encoder;
18129e57fabSMaxime Ripard 
182ebc94461SRob Herring 	ret = drm_of_find_panel_or_bridge(tcon->dev->of_node, 1, 0,
18319d0ffe0SMaxime Ripard 					  &rgb->panel, &rgb->bridge);
184ebc94461SRob Herring 	if (ret) {
185894f5a9fSMaxime Ripard 		dev_info(drm->dev, "No panel or bridge found... RGB output disabled\n");
186a8444c7eSMaxime Ripard 		return 0;
187a8444c7eSMaxime Ripard 	}
188a8444c7eSMaxime Ripard 
18929e57fabSMaxime Ripard 	drm_encoder_helper_add(&rgb->encoder,
19029e57fabSMaxime Ripard 			       &sun4i_rgb_enc_helper_funcs);
19129e57fabSMaxime Ripard 	ret = drm_encoder_init(drm,
19229e57fabSMaxime Ripard 			       &rgb->encoder,
19329e57fabSMaxime Ripard 			       &sun4i_rgb_enc_funcs,
19429e57fabSMaxime Ripard 			       DRM_MODE_ENCODER_NONE,
19529e57fabSMaxime Ripard 			       NULL);
19629e57fabSMaxime Ripard 	if (ret) {
19729e57fabSMaxime Ripard 		dev_err(drm->dev, "Couldn't initialise the rgb encoder\n");
19829e57fabSMaxime Ripard 		goto err_out;
19929e57fabSMaxime Ripard 	}
20029e57fabSMaxime Ripard 
20129e57fabSMaxime Ripard 	/* The RGB encoder can only work with the TCON channel 0 */
202dbf8f9e4SVille Syrjälä 	rgb->encoder.possible_crtcs = drm_crtc_mask(&tcon->crtc->crtc);
20329e57fabSMaxime Ripard 
2041ce6f91cSMaxime Ripard 	if (rgb->panel) {
20529e57fabSMaxime Ripard 		drm_connector_helper_add(&rgb->connector,
20629e57fabSMaxime Ripard 					 &sun4i_rgb_con_helper_funcs);
20729e57fabSMaxime Ripard 		ret = drm_connector_init(drm, &rgb->connector,
20829e57fabSMaxime Ripard 					 &sun4i_rgb_con_funcs,
20929e57fabSMaxime Ripard 					 DRM_MODE_CONNECTOR_Unknown);
21029e57fabSMaxime Ripard 		if (ret) {
21129e57fabSMaxime Ripard 			dev_err(drm->dev, "Couldn't initialise the rgb connector\n");
21229e57fabSMaxime Ripard 			goto err_cleanup_connector;
21329e57fabSMaxime Ripard 		}
21429e57fabSMaxime Ripard 
215cde4c44dSDaniel Vetter 		drm_connector_attach_encoder(&rgb->connector,
216894f5a9fSMaxime Ripard 						  &rgb->encoder);
21729e57fabSMaxime Ripard 
2181ce6f91cSMaxime Ripard 		ret = drm_panel_attach(rgb->panel, &rgb->connector);
219894f5a9fSMaxime Ripard 		if (ret) {
220894f5a9fSMaxime Ripard 			dev_err(drm->dev, "Couldn't attach our panel\n");
221894f5a9fSMaxime Ripard 			goto err_cleanup_connector;
222894f5a9fSMaxime Ripard 		}
223894f5a9fSMaxime Ripard 	}
224894f5a9fSMaxime Ripard 
22519d0ffe0SMaxime Ripard 	if (rgb->bridge) {
22619d0ffe0SMaxime Ripard 		ret = drm_bridge_attach(encoder, rgb->bridge, NULL);
227894f5a9fSMaxime Ripard 		if (ret) {
228894f5a9fSMaxime Ripard 			dev_err(drm->dev, "Couldn't attach our bridge\n");
229894f5a9fSMaxime Ripard 			goto err_cleanup_connector;
230894f5a9fSMaxime Ripard 		}
231894f5a9fSMaxime Ripard 	}
23229e57fabSMaxime Ripard 
23329e57fabSMaxime Ripard 	return 0;
23429e57fabSMaxime Ripard 
23529e57fabSMaxime Ripard err_cleanup_connector:
23629e57fabSMaxime Ripard 	drm_encoder_cleanup(&rgb->encoder);
23729e57fabSMaxime Ripard err_out:
23829e57fabSMaxime Ripard 	return ret;
23929e57fabSMaxime Ripard }
24029e57fabSMaxime Ripard EXPORT_SYMBOL(sun4i_rgb_init);
241