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 drm_panel *panel;
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);
45 return drm_panel_get_modes(lvds->panel);
48 static struct drm_connector_helper_funcs sun4i_lvds_con_helper_funcs = {
49 .get_modes = sun4i_lvds_get_modes,
53 sun4i_lvds_connector_destroy(struct drm_connector *connector)
55 struct sun4i_lvds *lvds = drm_connector_to_sun4i_lvds(connector);
57 drm_panel_detach(lvds->panel);
58 drm_connector_cleanup(connector);
61 static const struct drm_connector_funcs sun4i_lvds_con_funcs = {
62 .fill_modes = drm_helper_probe_single_connector_modes,
63 .destroy = sun4i_lvds_connector_destroy,
64 .reset = drm_atomic_helper_connector_reset,
65 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
66 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
69 static void sun4i_lvds_encoder_enable(struct drm_encoder *encoder)
71 struct sun4i_lvds *lvds = drm_encoder_to_sun4i_lvds(encoder);
73 DRM_DEBUG_DRIVER("Enabling LVDS output\n");
76 drm_panel_prepare(lvds->panel);
77 drm_panel_enable(lvds->panel);
81 static void sun4i_lvds_encoder_disable(struct drm_encoder *encoder)
83 struct sun4i_lvds *lvds = drm_encoder_to_sun4i_lvds(encoder);
85 DRM_DEBUG_DRIVER("Disabling LVDS output\n");
88 drm_panel_disable(lvds->panel);
89 drm_panel_unprepare(lvds->panel);
93 static const struct drm_encoder_helper_funcs sun4i_lvds_enc_helper_funcs = {
94 .disable = sun4i_lvds_encoder_disable,
95 .enable = sun4i_lvds_encoder_enable,
98 static const struct drm_encoder_funcs sun4i_lvds_enc_funcs = {
99 .destroy = drm_encoder_cleanup,
102 int sun4i_lvds_init(struct drm_device *drm, struct sun4i_tcon *tcon)
104 struct drm_encoder *encoder;
105 struct drm_bridge *bridge;
106 struct sun4i_lvds *lvds;
109 lvds = devm_kzalloc(drm->dev, sizeof(*lvds), GFP_KERNEL);
112 encoder = &lvds->encoder;
114 ret = drm_of_find_panel_or_bridge(tcon->dev->of_node, 1, 0,
115 &lvds->panel, &bridge);
117 dev_info(drm->dev, "No panel or bridge found... LVDS output disabled\n");
121 drm_encoder_helper_add(&lvds->encoder,
122 &sun4i_lvds_enc_helper_funcs);
123 ret = drm_encoder_init(drm,
125 &sun4i_lvds_enc_funcs,
126 DRM_MODE_ENCODER_LVDS,
129 dev_err(drm->dev, "Couldn't initialise the lvds encoder\n");
133 /* The LVDS encoder can only work with the TCON channel 0 */
134 lvds->encoder.possible_crtcs = drm_crtc_mask(&tcon->crtc->crtc);
137 drm_connector_helper_add(&lvds->connector,
138 &sun4i_lvds_con_helper_funcs);
139 ret = drm_connector_init(drm, &lvds->connector,
140 &sun4i_lvds_con_funcs,
141 DRM_MODE_CONNECTOR_LVDS);
143 dev_err(drm->dev, "Couldn't initialise the lvds connector\n");
144 goto err_cleanup_connector;
147 drm_connector_attach_encoder(&lvds->connector,
150 ret = drm_panel_attach(lvds->panel, &lvds->connector);
152 dev_err(drm->dev, "Couldn't attach our panel\n");
153 goto err_cleanup_connector;
158 ret = drm_bridge_attach(encoder, bridge, NULL);
160 dev_err(drm->dev, "Couldn't attach our bridge\n");
161 goto err_cleanup_connector;
167 err_cleanup_connector:
168 drm_encoder_cleanup(&lvds->encoder);
172 EXPORT_SYMBOL(sun4i_lvds_init);