xref: /linux/drivers/gpu/drm/meson/meson_encoder_dsi.c (revision 0e2b2a76278153d1ac312b0691cb65dabb9aef3e)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2016 BayLibre, SAS
4  * Author: Neil Armstrong <narmstrong@baylibre.com>
5  * Copyright (C) 2015 Amlogic, Inc. All rights reserved.
6  */
7 
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/of_device.h>
11 #include <linux/of_graph.h>
12 
13 #include <drm/drm_atomic_helper.h>
14 #include <drm/drm_simple_kms_helper.h>
15 #include <drm/drm_bridge.h>
16 #include <drm/drm_bridge_connector.h>
17 #include <drm/drm_device.h>
18 #include <drm/drm_probe_helper.h>
19 
20 #include "meson_drv.h"
21 #include "meson_encoder_dsi.h"
22 #include "meson_registers.h"
23 #include "meson_venc.h"
24 #include "meson_vclk.h"
25 
26 struct meson_encoder_dsi {
27 	struct drm_encoder encoder;
28 	struct drm_bridge bridge;
29 	struct drm_bridge *next_bridge;
30 	struct meson_drm *priv;
31 };
32 
33 #define bridge_to_meson_encoder_dsi(x) \
34 	container_of(x, struct meson_encoder_dsi, bridge)
35 
36 static int meson_encoder_dsi_attach(struct drm_bridge *bridge,
37 				    enum drm_bridge_attach_flags flags)
38 {
39 	struct meson_encoder_dsi *encoder_dsi = bridge_to_meson_encoder_dsi(bridge);
40 
41 	return drm_bridge_attach(bridge->encoder, encoder_dsi->next_bridge,
42 				 &encoder_dsi->bridge, flags);
43 }
44 
45 static void meson_encoder_dsi_atomic_enable(struct drm_bridge *bridge,
46 					    struct drm_bridge_state *bridge_state)
47 {
48 	struct meson_encoder_dsi *encoder_dsi = bridge_to_meson_encoder_dsi(bridge);
49 	struct drm_atomic_state *state = bridge_state->base.state;
50 	struct meson_drm *priv = encoder_dsi->priv;
51 	struct drm_connector_state *conn_state;
52 	struct drm_crtc_state *crtc_state;
53 	struct drm_connector *connector;
54 
55 	connector = drm_atomic_get_new_connector_for_encoder(state, bridge->encoder);
56 	if (WARN_ON(!connector))
57 		return;
58 
59 	conn_state = drm_atomic_get_new_connector_state(state, connector);
60 	if (WARN_ON(!conn_state))
61 		return;
62 
63 	crtc_state = drm_atomic_get_new_crtc_state(state, conn_state->crtc);
64 	if (WARN_ON(!crtc_state))
65 		return;
66 
67 	/* ENCL clock setup is handled by CCF */
68 
69 	meson_venc_mipi_dsi_mode_set(priv, &crtc_state->adjusted_mode);
70 	meson_encl_load_gamma(priv);
71 
72 	writel_relaxed(0, priv->io_base + _REG(ENCL_VIDEO_EN));
73 
74 	writel_bits_relaxed(ENCL_VIDEO_MODE_ADV_VFIFO_EN, ENCL_VIDEO_MODE_ADV_VFIFO_EN,
75 			    priv->io_base + _REG(ENCL_VIDEO_MODE_ADV));
76 	writel_relaxed(0, priv->io_base + _REG(ENCL_TST_EN));
77 
78 	writel_bits_relaxed(BIT(0), 0, priv->io_base + _REG(VPP_WRAP_OSD1_MATRIX_EN_CTRL));
79 
80 	writel_relaxed(1, priv->io_base + _REG(ENCL_VIDEO_EN));
81 }
82 
83 static void meson_encoder_dsi_atomic_disable(struct drm_bridge *bridge,
84 					     struct drm_bridge_state *bridge_state)
85 {
86 	struct meson_encoder_dsi *meson_encoder_dsi =
87 					bridge_to_meson_encoder_dsi(bridge);
88 	struct meson_drm *priv = meson_encoder_dsi->priv;
89 
90 	writel_relaxed(0, priv->io_base + _REG(ENCL_VIDEO_EN));
91 
92 	writel_bits_relaxed(BIT(0), BIT(0), priv->io_base + _REG(VPP_WRAP_OSD1_MATRIX_EN_CTRL));
93 }
94 
95 static const struct drm_bridge_funcs meson_encoder_dsi_bridge_funcs = {
96 	.attach	= meson_encoder_dsi_attach,
97 	.atomic_enable = meson_encoder_dsi_atomic_enable,
98 	.atomic_disable	= meson_encoder_dsi_atomic_disable,
99 	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
100 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
101 	.atomic_reset = drm_atomic_helper_bridge_reset,
102 };
103 
104 int meson_encoder_dsi_init(struct meson_drm *priv)
105 {
106 	struct meson_encoder_dsi *meson_encoder_dsi;
107 	struct device_node *remote;
108 	int ret;
109 
110 	meson_encoder_dsi = devm_kzalloc(priv->dev, sizeof(*meson_encoder_dsi), GFP_KERNEL);
111 	if (!meson_encoder_dsi)
112 		return -ENOMEM;
113 
114 	/* DSI Transceiver Bridge */
115 	remote = of_graph_get_remote_node(priv->dev->of_node, 2, 0);
116 	if (!remote) {
117 		dev_err(priv->dev, "DSI transceiver device is disabled");
118 		return 0;
119 	}
120 
121 	meson_encoder_dsi->next_bridge = of_drm_find_bridge(remote);
122 	if (!meson_encoder_dsi->next_bridge) {
123 		dev_dbg(priv->dev, "Failed to find DSI transceiver bridge\n");
124 		return -EPROBE_DEFER;
125 	}
126 
127 	/* DSI Encoder Bridge */
128 	meson_encoder_dsi->bridge.funcs = &meson_encoder_dsi_bridge_funcs;
129 	meson_encoder_dsi->bridge.of_node = priv->dev->of_node;
130 	meson_encoder_dsi->bridge.type = DRM_MODE_CONNECTOR_DSI;
131 
132 	drm_bridge_add(&meson_encoder_dsi->bridge);
133 
134 	meson_encoder_dsi->priv = priv;
135 
136 	/* Encoder */
137 	ret = drm_simple_encoder_init(priv->drm, &meson_encoder_dsi->encoder,
138 				      DRM_MODE_ENCODER_DSI);
139 	if (ret) {
140 		dev_err(priv->dev, "Failed to init DSI encoder: %d\n", ret);
141 		return ret;
142 	}
143 
144 	meson_encoder_dsi->encoder.possible_crtcs = BIT(0);
145 
146 	/* Attach DSI Encoder Bridge to Encoder */
147 	ret = drm_bridge_attach(&meson_encoder_dsi->encoder, &meson_encoder_dsi->bridge, NULL, 0);
148 	if (ret) {
149 		dev_err(priv->dev, "Failed to attach bridge: %d\n", ret);
150 		return ret;
151 	}
152 
153 	/*
154 	 * We should have now in place:
155 	 * encoder->[dsi encoder bridge]->[dw-mipi-dsi bridge]->[panel bridge]->[panel]
156 	 */
157 
158 	priv->encoders[MESON_ENC_DSI] = meson_encoder_dsi;
159 
160 	dev_dbg(priv->dev, "DSI encoder initialized\n");
161 
162 	return 0;
163 }
164 
165 void meson_encoder_dsi_remove(struct meson_drm *priv)
166 {
167 	struct meson_encoder_dsi *meson_encoder_dsi;
168 
169 	if (priv->encoders[MESON_ENC_DSI]) {
170 		meson_encoder_dsi = priv->encoders[MESON_ENC_DSI];
171 		drm_bridge_remove(&meson_encoder_dsi->bridge);
172 		drm_bridge_remove(meson_encoder_dsi->next_bridge);
173 	}
174 }
175