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