1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Free Electrons
4 * Maxime Ripard <maxime.ripard@free-electrons.com>
10 #include <drm/drm_atomic_helper.h>
11 #include <drm/drm_of.h>
12 #include <drm/drm_panel.h>
13 #include <drm/drm_probe_helper.h>
15 #include "sun4i_crtc.h"
16 #include "sun4i_tcon.h"
17 #include "sun4i_lvds.h"
20 struct drm_connector connector
;
21 struct drm_encoder encoder
;
23 struct sun4i_tcon
*tcon
;
26 static inline struct sun4i_lvds
*
27 drm_connector_to_sun4i_lvds(struct drm_connector
*connector
)
29 return container_of(connector
, struct sun4i_lvds
,
33 static inline struct sun4i_lvds
*
34 drm_encoder_to_sun4i_lvds(struct drm_encoder
*encoder
)
36 return container_of(encoder
, struct sun4i_lvds
,
40 static int sun4i_lvds_get_modes(struct drm_connector
*connector
)
42 struct sun4i_lvds
*lvds
=
43 drm_connector_to_sun4i_lvds(connector
);
44 struct sun4i_tcon
*tcon
= lvds
->tcon
;
46 return drm_panel_get_modes(tcon
->panel
);
49 static struct drm_connector_helper_funcs sun4i_lvds_con_helper_funcs
= {
50 .get_modes
= sun4i_lvds_get_modes
,
54 sun4i_lvds_connector_destroy(struct drm_connector
*connector
)
56 struct sun4i_lvds
*lvds
= drm_connector_to_sun4i_lvds(connector
);
57 struct sun4i_tcon
*tcon
= lvds
->tcon
;
59 drm_panel_detach(tcon
->panel
);
60 drm_connector_cleanup(connector
);
63 static const struct drm_connector_funcs sun4i_lvds_con_funcs
= {
64 .fill_modes
= drm_helper_probe_single_connector_modes
,
65 .destroy
= sun4i_lvds_connector_destroy
,
66 .reset
= drm_atomic_helper_connector_reset
,
67 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
68 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
71 static void sun4i_lvds_encoder_enable(struct drm_encoder
*encoder
)
73 struct sun4i_lvds
*lvds
= drm_encoder_to_sun4i_lvds(encoder
);
74 struct sun4i_tcon
*tcon
= lvds
->tcon
;
76 DRM_DEBUG_DRIVER("Enabling LVDS output\n");
79 drm_panel_prepare(tcon
->panel
);
80 drm_panel_enable(tcon
->panel
);
84 static void sun4i_lvds_encoder_disable(struct drm_encoder
*encoder
)
86 struct sun4i_lvds
*lvds
= drm_encoder_to_sun4i_lvds(encoder
);
87 struct sun4i_tcon
*tcon
= lvds
->tcon
;
89 DRM_DEBUG_DRIVER("Disabling LVDS output\n");
92 drm_panel_disable(tcon
->panel
);
93 drm_panel_unprepare(tcon
->panel
);
97 static const struct drm_encoder_helper_funcs sun4i_lvds_enc_helper_funcs
= {
98 .disable
= sun4i_lvds_encoder_disable
,
99 .enable
= sun4i_lvds_encoder_enable
,
102 static const struct drm_encoder_funcs sun4i_lvds_enc_funcs
= {
103 .destroy
= drm_encoder_cleanup
,
106 int sun4i_lvds_init(struct drm_device
*drm
, struct sun4i_tcon
*tcon
)
108 struct drm_encoder
*encoder
;
109 struct drm_bridge
*bridge
;
110 struct sun4i_lvds
*lvds
;
113 lvds
= devm_kzalloc(drm
->dev
, sizeof(*lvds
), GFP_KERNEL
);
117 encoder
= &lvds
->encoder
;
119 ret
= drm_of_find_panel_or_bridge(tcon
->dev
->of_node
, 1, 0,
120 &tcon
->panel
, &bridge
);
122 dev_info(drm
->dev
, "No panel or bridge found... LVDS output disabled\n");
126 drm_encoder_helper_add(&lvds
->encoder
,
127 &sun4i_lvds_enc_helper_funcs
);
128 ret
= drm_encoder_init(drm
,
130 &sun4i_lvds_enc_funcs
,
131 DRM_MODE_ENCODER_LVDS
,
134 dev_err(drm
->dev
, "Couldn't initialise the lvds encoder\n");
138 /* The LVDS encoder can only work with the TCON channel 0 */
139 lvds
->encoder
.possible_crtcs
= drm_crtc_mask(&tcon
->crtc
->crtc
);
142 drm_connector_helper_add(&lvds
->connector
,
143 &sun4i_lvds_con_helper_funcs
);
144 ret
= drm_connector_init(drm
, &lvds
->connector
,
145 &sun4i_lvds_con_funcs
,
146 DRM_MODE_CONNECTOR_LVDS
);
148 dev_err(drm
->dev
, "Couldn't initialise the lvds connector\n");
149 goto err_cleanup_connector
;
152 drm_connector_attach_encoder(&lvds
->connector
,
155 ret
= drm_panel_attach(tcon
->panel
, &lvds
->connector
);
157 dev_err(drm
->dev
, "Couldn't attach our panel\n");
158 goto err_cleanup_connector
;
163 ret
= drm_bridge_attach(encoder
, bridge
, NULL
);
165 dev_err(drm
->dev
, "Couldn't attach our bridge\n");
166 goto err_cleanup_connector
;
172 err_cleanup_connector
:
173 drm_encoder_cleanup(&lvds
->encoder
);
177 EXPORT_SYMBOL(sun4i_lvds_init
);