xref: /linux/drivers/gpu/drm/sun4i/sun4i_lvds.c (revision fb8d617f)
1a0c1214eSMaxime Ripard // SPDX-License-Identifier: GPL-2.0+
2a0c1214eSMaxime Ripard /*
3a0c1214eSMaxime Ripard  * Copyright (C) 2017 Free Electrons
4a0c1214eSMaxime Ripard  * Maxime Ripard <maxime.ripard@free-electrons.com>
5a0c1214eSMaxime Ripard  */
6a0c1214eSMaxime Ripard 
7a0c1214eSMaxime Ripard #include <linux/clk.h>
8a0c1214eSMaxime Ripard 
9a0c1214eSMaxime Ripard #include <drm/drm_atomic_helper.h>
10ee68c743SBoris Brezillon #include <drm/drm_bridge.h>
11a0c1214eSMaxime Ripard #include <drm/drm_of.h>
12a0c1214eSMaxime Ripard #include <drm/drm_panel.h>
139c25a297SSam Ravnborg #include <drm/drm_print.h>
14fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h>
15f9f3a38dSThomas Zimmermann #include <drm/drm_simple_kms_helper.h>
16a0c1214eSMaxime Ripard 
17a0c1214eSMaxime Ripard #include "sun4i_crtc.h"
18a0c1214eSMaxime Ripard #include "sun4i_tcon.h"
19a0c1214eSMaxime Ripard #include "sun4i_lvds.h"
20a0c1214eSMaxime Ripard 
21a0c1214eSMaxime Ripard struct sun4i_lvds {
22a0c1214eSMaxime Ripard 	struct drm_connector	connector;
23a0c1214eSMaxime Ripard 	struct drm_encoder	encoder;
24a0c1214eSMaxime Ripard 
251ce6f91cSMaxime Ripard 	struct drm_panel	*panel;
26a0c1214eSMaxime Ripard };
27a0c1214eSMaxime Ripard 
28a0c1214eSMaxime Ripard static inline struct sun4i_lvds *
drm_connector_to_sun4i_lvds(struct drm_connector * connector)29a0c1214eSMaxime Ripard drm_connector_to_sun4i_lvds(struct drm_connector *connector)
30a0c1214eSMaxime Ripard {
31a0c1214eSMaxime Ripard 	return container_of(connector, struct sun4i_lvds,
32a0c1214eSMaxime Ripard 			    connector);
33a0c1214eSMaxime Ripard }
34a0c1214eSMaxime Ripard 
35a0c1214eSMaxime Ripard static inline struct sun4i_lvds *
drm_encoder_to_sun4i_lvds(struct drm_encoder * encoder)36a0c1214eSMaxime Ripard drm_encoder_to_sun4i_lvds(struct drm_encoder *encoder)
37a0c1214eSMaxime Ripard {
38a0c1214eSMaxime Ripard 	return container_of(encoder, struct sun4i_lvds,
39a0c1214eSMaxime Ripard 			    encoder);
40a0c1214eSMaxime Ripard }
41a0c1214eSMaxime Ripard 
sun4i_lvds_get_modes(struct drm_connector * connector)42a0c1214eSMaxime Ripard static int sun4i_lvds_get_modes(struct drm_connector *connector)
43a0c1214eSMaxime Ripard {
44a0c1214eSMaxime Ripard 	struct sun4i_lvds *lvds =
45a0c1214eSMaxime Ripard 		drm_connector_to_sun4i_lvds(connector);
46a0c1214eSMaxime Ripard 
4706c4a9c2SSam Ravnborg 	return drm_panel_get_modes(lvds->panel, connector);
48a0c1214eSMaxime Ripard }
49a0c1214eSMaxime Ripard 
50f13478c9SRikard Falkeborn static const struct drm_connector_helper_funcs sun4i_lvds_con_helper_funcs = {
51a0c1214eSMaxime Ripard 	.get_modes	= sun4i_lvds_get_modes,
52a0c1214eSMaxime Ripard };
53a0c1214eSMaxime Ripard 
54a0c1214eSMaxime Ripard static void
sun4i_lvds_connector_destroy(struct drm_connector * connector)55a0c1214eSMaxime Ripard sun4i_lvds_connector_destroy(struct drm_connector *connector)
56a0c1214eSMaxime Ripard {
57a0c1214eSMaxime Ripard 	drm_connector_cleanup(connector);
58a0c1214eSMaxime Ripard }
59a0c1214eSMaxime Ripard 
60a0c1214eSMaxime Ripard static const struct drm_connector_funcs sun4i_lvds_con_funcs = {
61a0c1214eSMaxime Ripard 	.fill_modes		= drm_helper_probe_single_connector_modes,
62a0c1214eSMaxime Ripard 	.destroy		= sun4i_lvds_connector_destroy,
63a0c1214eSMaxime Ripard 	.reset			= drm_atomic_helper_connector_reset,
64a0c1214eSMaxime Ripard 	.atomic_duplicate_state	= drm_atomic_helper_connector_duplicate_state,
65a0c1214eSMaxime Ripard 	.atomic_destroy_state	= drm_atomic_helper_connector_destroy_state,
66a0c1214eSMaxime Ripard };
67a0c1214eSMaxime Ripard 
sun4i_lvds_encoder_enable(struct drm_encoder * encoder)68a0c1214eSMaxime Ripard static void sun4i_lvds_encoder_enable(struct drm_encoder *encoder)
69a0c1214eSMaxime Ripard {
70a0c1214eSMaxime Ripard 	struct sun4i_lvds *lvds = drm_encoder_to_sun4i_lvds(encoder);
71a0c1214eSMaxime Ripard 
72a0c1214eSMaxime Ripard 	DRM_DEBUG_DRIVER("Enabling LVDS output\n");
73a0c1214eSMaxime Ripard 
741ce6f91cSMaxime Ripard 	if (lvds->panel) {
751ce6f91cSMaxime Ripard 		drm_panel_prepare(lvds->panel);
761ce6f91cSMaxime Ripard 		drm_panel_enable(lvds->panel);
77a0c1214eSMaxime Ripard 	}
78a0c1214eSMaxime Ripard }
79a0c1214eSMaxime Ripard 
sun4i_lvds_encoder_disable(struct drm_encoder * encoder)80a0c1214eSMaxime Ripard static void sun4i_lvds_encoder_disable(struct drm_encoder *encoder)
81a0c1214eSMaxime Ripard {
82a0c1214eSMaxime Ripard 	struct sun4i_lvds *lvds = drm_encoder_to_sun4i_lvds(encoder);
83a0c1214eSMaxime Ripard 
84a0c1214eSMaxime Ripard 	DRM_DEBUG_DRIVER("Disabling LVDS output\n");
85a0c1214eSMaxime Ripard 
861ce6f91cSMaxime Ripard 	if (lvds->panel) {
871ce6f91cSMaxime Ripard 		drm_panel_disable(lvds->panel);
881ce6f91cSMaxime Ripard 		drm_panel_unprepare(lvds->panel);
89a0c1214eSMaxime Ripard 	}
90a0c1214eSMaxime Ripard }
91a0c1214eSMaxime Ripard 
92a0c1214eSMaxime Ripard static const struct drm_encoder_helper_funcs sun4i_lvds_enc_helper_funcs = {
93a0c1214eSMaxime Ripard 	.disable	= sun4i_lvds_encoder_disable,
94a0c1214eSMaxime Ripard 	.enable		= sun4i_lvds_encoder_enable,
95a0c1214eSMaxime Ripard };
96a0c1214eSMaxime Ripard 
sun4i_lvds_init(struct drm_device * drm,struct sun4i_tcon * tcon)97a0c1214eSMaxime Ripard int sun4i_lvds_init(struct drm_device *drm, struct sun4i_tcon *tcon)
98a0c1214eSMaxime Ripard {
99a0c1214eSMaxime Ripard 	struct drm_encoder *encoder;
100a0c1214eSMaxime Ripard 	struct drm_bridge *bridge;
101a0c1214eSMaxime Ripard 	struct sun4i_lvds *lvds;
102a0c1214eSMaxime Ripard 	int ret;
103a0c1214eSMaxime Ripard 
104a0c1214eSMaxime Ripard 	lvds = devm_kzalloc(drm->dev, sizeof(*lvds), GFP_KERNEL);
105a0c1214eSMaxime Ripard 	if (!lvds)
106a0c1214eSMaxime Ripard 		return -ENOMEM;
107a0c1214eSMaxime Ripard 	encoder = &lvds->encoder;
108a0c1214eSMaxime Ripard 
109a0c1214eSMaxime Ripard 	ret = drm_of_find_panel_or_bridge(tcon->dev->of_node, 1, 0,
1101ce6f91cSMaxime Ripard 					  &lvds->panel, &bridge);
111a0c1214eSMaxime Ripard 	if (ret) {
112a0c1214eSMaxime Ripard 		dev_info(drm->dev, "No panel or bridge found... LVDS output disabled\n");
113a0c1214eSMaxime Ripard 		return 0;
114a0c1214eSMaxime Ripard 	}
115a0c1214eSMaxime Ripard 
116a0c1214eSMaxime Ripard 	drm_encoder_helper_add(&lvds->encoder,
117a0c1214eSMaxime Ripard 			       &sun4i_lvds_enc_helper_funcs);
118f9f3a38dSThomas Zimmermann 	ret = drm_simple_encoder_init(drm, &lvds->encoder,
119f9f3a38dSThomas Zimmermann 				      DRM_MODE_ENCODER_LVDS);
120a0c1214eSMaxime Ripard 	if (ret) {
121a0c1214eSMaxime Ripard 		dev_err(drm->dev, "Couldn't initialise the lvds encoder\n");
122a0c1214eSMaxime Ripard 		goto err_out;
123a0c1214eSMaxime Ripard 	}
124a0c1214eSMaxime Ripard 
125a0c1214eSMaxime Ripard 	/* The LVDS encoder can only work with the TCON channel 0 */
126dbf8f9e4SVille Syrjälä 	lvds->encoder.possible_crtcs = drm_crtc_mask(&tcon->crtc->crtc);
127a0c1214eSMaxime Ripard 
1281ce6f91cSMaxime Ripard 	if (lvds->panel) {
129a0c1214eSMaxime Ripard 		drm_connector_helper_add(&lvds->connector,
130a0c1214eSMaxime Ripard 					 &sun4i_lvds_con_helper_funcs);
131a0c1214eSMaxime Ripard 		ret = drm_connector_init(drm, &lvds->connector,
132a0c1214eSMaxime Ripard 					 &sun4i_lvds_con_funcs,
133a0c1214eSMaxime Ripard 					 DRM_MODE_CONNECTOR_LVDS);
134a0c1214eSMaxime Ripard 		if (ret) {
135a0c1214eSMaxime Ripard 			dev_err(drm->dev, "Couldn't initialise the lvds connector\n");
136a0c1214eSMaxime Ripard 			goto err_cleanup_connector;
137a0c1214eSMaxime Ripard 		}
138a0c1214eSMaxime Ripard 
139cde4c44dSDaniel Vetter 		drm_connector_attach_encoder(&lvds->connector,
140a0c1214eSMaxime Ripard 						  &lvds->encoder);
141a0c1214eSMaxime Ripard 	}
142a0c1214eSMaxime Ripard 
143a0c1214eSMaxime Ripard 	if (bridge) {
144a25b988fSLaurent Pinchart 		ret = drm_bridge_attach(encoder, bridge, NULL, 0);
145*fb8d617fSLaurent Pinchart 		if (ret)
146a0c1214eSMaxime Ripard 			goto err_cleanup_connector;
147a0c1214eSMaxime Ripard 	}
148a0c1214eSMaxime Ripard 
149a0c1214eSMaxime Ripard 	return 0;
150a0c1214eSMaxime Ripard 
151a0c1214eSMaxime Ripard err_cleanup_connector:
152a0c1214eSMaxime Ripard 	drm_encoder_cleanup(&lvds->encoder);
153a0c1214eSMaxime Ripard err_out:
154a0c1214eSMaxime Ripard 	return ret;
155a0c1214eSMaxime Ripard }
156a0c1214eSMaxime Ripard EXPORT_SYMBOL(sun4i_lvds_init);
157