Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gpu / drm / sun4i / sun4i_lvds.c
blobac570437172e2190e9c6c1960db5d5e309e463f6
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2017 Free Electrons
4 * Maxime Ripard <maxime.ripard@free-electrons.com>
5 */
7 #include <linux/clk.h>
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 #include <drm/drm_simple_kms_helper.h>
17 #include "sun4i_crtc.h"
18 #include "sun4i_tcon.h"
19 #include "sun4i_lvds.h"
21 struct sun4i_lvds {
22 struct drm_connector connector;
23 struct drm_encoder encoder;
25 struct drm_panel *panel;
28 static inline struct sun4i_lvds *
29 drm_connector_to_sun4i_lvds(struct drm_connector *connector)
31 return container_of(connector, struct sun4i_lvds,
32 connector);
35 static inline struct sun4i_lvds *
36 drm_encoder_to_sun4i_lvds(struct drm_encoder *encoder)
38 return container_of(encoder, struct sun4i_lvds,
39 encoder);
42 static int sun4i_lvds_get_modes(struct drm_connector *connector)
44 struct sun4i_lvds *lvds =
45 drm_connector_to_sun4i_lvds(connector);
47 return drm_panel_get_modes(lvds->panel, connector);
50 static const struct drm_connector_helper_funcs sun4i_lvds_con_helper_funcs = {
51 .get_modes = sun4i_lvds_get_modes,
54 static void
55 sun4i_lvds_connector_destroy(struct drm_connector *connector)
57 drm_connector_cleanup(connector);
60 static const struct drm_connector_funcs sun4i_lvds_con_funcs = {
61 .fill_modes = drm_helper_probe_single_connector_modes,
62 .destroy = sun4i_lvds_connector_destroy,
63 .reset = drm_atomic_helper_connector_reset,
64 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
65 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
68 static void sun4i_lvds_encoder_enable(struct drm_encoder *encoder)
70 struct sun4i_lvds *lvds = drm_encoder_to_sun4i_lvds(encoder);
72 DRM_DEBUG_DRIVER("Enabling LVDS output\n");
74 if (lvds->panel) {
75 drm_panel_prepare(lvds->panel);
76 drm_panel_enable(lvds->panel);
80 static void sun4i_lvds_encoder_disable(struct drm_encoder *encoder)
82 struct sun4i_lvds *lvds = drm_encoder_to_sun4i_lvds(encoder);
84 DRM_DEBUG_DRIVER("Disabling LVDS output\n");
86 if (lvds->panel) {
87 drm_panel_disable(lvds->panel);
88 drm_panel_unprepare(lvds->panel);
92 static const struct drm_encoder_helper_funcs sun4i_lvds_enc_helper_funcs = {
93 .disable = sun4i_lvds_encoder_disable,
94 .enable = sun4i_lvds_encoder_enable,
97 int sun4i_lvds_init(struct drm_device *drm, struct sun4i_tcon *tcon)
99 struct drm_encoder *encoder;
100 struct drm_bridge *bridge;
101 struct sun4i_lvds *lvds;
102 int ret;
104 lvds = devm_kzalloc(drm->dev, sizeof(*lvds), GFP_KERNEL);
105 if (!lvds)
106 return -ENOMEM;
107 encoder = &lvds->encoder;
109 ret = drm_of_find_panel_or_bridge(tcon->dev->of_node, 1, 0,
110 &lvds->panel, &bridge);
111 if (ret) {
112 dev_info(drm->dev, "No panel or bridge found... LVDS output disabled\n");
113 return 0;
116 drm_encoder_helper_add(&lvds->encoder,
117 &sun4i_lvds_enc_helper_funcs);
118 ret = drm_simple_encoder_init(drm, &lvds->encoder,
119 DRM_MODE_ENCODER_LVDS);
120 if (ret) {
121 dev_err(drm->dev, "Couldn't initialise the lvds encoder\n");
122 goto err_out;
125 /* The LVDS encoder can only work with the TCON channel 0 */
126 lvds->encoder.possible_crtcs = drm_crtc_mask(&tcon->crtc->crtc);
128 if (lvds->panel) {
129 drm_connector_helper_add(&lvds->connector,
130 &sun4i_lvds_con_helper_funcs);
131 ret = drm_connector_init(drm, &lvds->connector,
132 &sun4i_lvds_con_funcs,
133 DRM_MODE_CONNECTOR_LVDS);
134 if (ret) {
135 dev_err(drm->dev, "Couldn't initialise the lvds connector\n");
136 goto err_cleanup_connector;
139 drm_connector_attach_encoder(&lvds->connector,
140 &lvds->encoder);
143 if (bridge) {
144 ret = drm_bridge_attach(encoder, bridge, NULL, 0);
145 if (ret) {
146 dev_err(drm->dev, "Couldn't attach our bridge\n");
147 goto err_cleanup_connector;
151 return 0;
153 err_cleanup_connector:
154 drm_encoder_cleanup(&lvds->encoder);
155 err_out:
156 return ret;
158 EXPORT_SYMBOL(sun4i_lvds_init);