1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Free Electrons
4 * Maxime Ripard <maxime.ripard@free-electrons.com>
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>
16 #include "sun4i_crtc.h"
17 #include "sun4i_tcon.h"
18 #include "sun4i_lvds.h"
21 struct drm_connector connector;
22 struct drm_encoder encoder;
24 struct drm_panel *panel;
27 static inline struct sun4i_lvds *
28 drm_connector_to_sun4i_lvds(struct drm_connector *connector)
30 return container_of(connector, struct sun4i_lvds,
34 static inline struct sun4i_lvds *
35 drm_encoder_to_sun4i_lvds(struct drm_encoder *encoder)
37 return container_of(encoder, struct sun4i_lvds,
41 static int sun4i_lvds_get_modes(struct drm_connector *connector)
43 struct sun4i_lvds *lvds =
44 drm_connector_to_sun4i_lvds(connector);
46 return drm_panel_get_modes(lvds->panel, connector);
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);
58 drm_panel_detach(lvds->panel);
59 drm_connector_cleanup(connector);
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,
70 static void sun4i_lvds_encoder_enable(struct drm_encoder *encoder)
72 struct sun4i_lvds *lvds = drm_encoder_to_sun4i_lvds(encoder);
74 DRM_DEBUG_DRIVER("Enabling LVDS output\n");
77 drm_panel_prepare(lvds->panel);
78 drm_panel_enable(lvds->panel);
82 static void sun4i_lvds_encoder_disable(struct drm_encoder *encoder)
84 struct sun4i_lvds *lvds = drm_encoder_to_sun4i_lvds(encoder);
86 DRM_DEBUG_DRIVER("Disabling LVDS output\n");
89 drm_panel_disable(lvds->panel);
90 drm_panel_unprepare(lvds->panel);
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,
99 static const struct drm_encoder_funcs sun4i_lvds_enc_funcs = {
100 .destroy = drm_encoder_cleanup,
103 int sun4i_lvds_init(struct drm_device *drm, struct sun4i_tcon *tcon)
105 struct drm_encoder *encoder;
106 struct drm_bridge *bridge;
107 struct sun4i_lvds *lvds;
110 lvds = devm_kzalloc(drm->dev, sizeof(*lvds), GFP_KERNEL);
113 encoder = &lvds->encoder;
115 ret = drm_of_find_panel_or_bridge(tcon->dev->of_node, 1, 0,
116 &lvds->panel, &bridge);
118 dev_info(drm->dev, "No panel or bridge found... LVDS output disabled\n");
122 drm_encoder_helper_add(&lvds->encoder,
123 &sun4i_lvds_enc_helper_funcs);
124 ret = drm_encoder_init(drm,
126 &sun4i_lvds_enc_funcs,
127 DRM_MODE_ENCODER_LVDS,
130 dev_err(drm->dev, "Couldn't initialise the lvds encoder\n");
134 /* The LVDS encoder can only work with the TCON channel 0 */
135 lvds->encoder.possible_crtcs = drm_crtc_mask(&tcon->crtc->crtc);
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);
144 dev_err(drm->dev, "Couldn't initialise the lvds connector\n");
145 goto err_cleanup_connector;
148 drm_connector_attach_encoder(&lvds->connector,
151 ret = drm_panel_attach(lvds->panel, &lvds->connector);
153 dev_err(drm->dev, "Couldn't attach our panel\n");
154 goto err_cleanup_connector;
159 ret = drm_bridge_attach(encoder, bridge, NULL);
161 dev_err(drm->dev, "Couldn't attach our bridge\n");
162 goto err_cleanup_connector;
168 err_cleanup_connector:
169 drm_encoder_cleanup(&lvds->encoder);
173 EXPORT_SYMBOL(sun4i_lvds_init);