1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2016 Broadcom Limited
4 */
5
6 /**
7 * DOC: VC4 DPI module
8 *
9 * The VC4 DPI hardware supports MIPI DPI type 4 and Nokia ViSSI
10 * signals. On BCM2835, these can be routed out to GPIO0-27 with the
11 * ALT2 function.
12 */
13
14 #include <drm/drm_atomic_helper.h>
15 #include <drm/drm_bridge.h>
16 #include <drm/drm_edid.h>
17 #include <drm/drm_of.h>
18 #include <drm/drm_panel.h>
19 #include <drm/drm_probe_helper.h>
20 #include <drm/drm_simple_kms_helper.h>
21 #include <linux/clk.h>
22 #include <linux/component.h>
23 #include <linux/of_graph.h>
24 #include <linux/of_platform.h>
25 #include "vc4_drv.h"
26 #include "vc4_regs.h"
27
28 #define DPI_C 0x00
29 # define DPI_OUTPUT_ENABLE_MODE BIT(16)
30
31 /* The order field takes the incoming 24 bit RGB from the pixel valve
32 * and shuffles the 3 channels.
33 */
34 # define DPI_ORDER_MASK VC4_MASK(15, 14)
35 # define DPI_ORDER_SHIFT 14
36 # define DPI_ORDER_RGB 0
37 # define DPI_ORDER_BGR 1
38 # define DPI_ORDER_GRB 2
39 # define DPI_ORDER_BRG 3
40
41 /* The format field takes the ORDER-shuffled pixel valve data and
42 * formats it onto the output lines.
43 */
44 # define DPI_FORMAT_MASK VC4_MASK(13, 11)
45 # define DPI_FORMAT_SHIFT 11
46 /* This define is named in the hardware, but actually just outputs 0. */
47 # define DPI_FORMAT_9BIT_666_RGB 0
48 /* Outputs 00000000rrrrrggggggbbbbb */
49 # define DPI_FORMAT_16BIT_565_RGB_1 1
50 /* Outputs 000rrrrr00gggggg000bbbbb */
51 # define DPI_FORMAT_16BIT_565_RGB_2 2
52 /* Outputs 00rrrrr000gggggg00bbbbb0 */
53 # define DPI_FORMAT_16BIT_565_RGB_3 3
54 /* Outputs 000000rrrrrrggggggbbbbbb */
55 # define DPI_FORMAT_18BIT_666_RGB_1 4
56 /* Outputs 00rrrrrr00gggggg00bbbbbb */
57 # define DPI_FORMAT_18BIT_666_RGB_2 5
58 /* Outputs rrrrrrrrggggggggbbbbbbbb */
59 # define DPI_FORMAT_24BIT_888_RGB 6
60
61 /* Reverses the polarity of the corresponding signal */
62 # define DPI_PIXEL_CLK_INVERT BIT(10)
63 # define DPI_HSYNC_INVERT BIT(9)
64 # define DPI_VSYNC_INVERT BIT(8)
65 # define DPI_OUTPUT_ENABLE_INVERT BIT(7)
66
67 /* Outputs the signal the falling clock edge instead of rising. */
68 # define DPI_HSYNC_NEGATE BIT(6)
69 # define DPI_VSYNC_NEGATE BIT(5)
70 # define DPI_OUTPUT_ENABLE_NEGATE BIT(4)
71
72 /* Disables the signal */
73 # define DPI_HSYNC_DISABLE BIT(3)
74 # define DPI_VSYNC_DISABLE BIT(2)
75 # define DPI_OUTPUT_ENABLE_DISABLE BIT(1)
76
77 /* Power gate to the device, full reset at 0 -> 1 transition */
78 # define DPI_ENABLE BIT(0)
79
80 /* All other registers besides DPI_C return the ID */
81 #define DPI_ID 0x04
82 # define DPI_ID_VALUE 0x00647069
83
84 /* General DPI hardware state. */
85 struct vc4_dpi {
86 struct platform_device *pdev;
87
88 struct drm_encoder *encoder;
89
90 void __iomem *regs;
91
92 struct clk *pixel_clock;
93 struct clk *core_clock;
94
95 struct debugfs_regset32 regset;
96 };
97
98 #define DPI_READ(offset) readl(dpi->regs + (offset))
99 #define DPI_WRITE(offset, val) writel(val, dpi->regs + (offset))
100
101 /* VC4 DPI encoder KMS struct */
102 struct vc4_dpi_encoder {
103 struct vc4_encoder base;
104 struct vc4_dpi *dpi;
105 };
106
107 static inline struct vc4_dpi_encoder *
to_vc4_dpi_encoder(struct drm_encoder * encoder)108 to_vc4_dpi_encoder(struct drm_encoder *encoder)
109 {
110 return container_of(encoder, struct vc4_dpi_encoder, base.base);
111 }
112
113 static const struct debugfs_reg32 dpi_regs[] = {
114 VC4_REG32(DPI_C),
115 VC4_REG32(DPI_ID),
116 };
117
vc4_dpi_encoder_disable(struct drm_encoder * encoder)118 static void vc4_dpi_encoder_disable(struct drm_encoder *encoder)
119 {
120 struct vc4_dpi_encoder *vc4_encoder = to_vc4_dpi_encoder(encoder);
121 struct vc4_dpi *dpi = vc4_encoder->dpi;
122
123 clk_disable_unprepare(dpi->pixel_clock);
124 }
125
vc4_dpi_encoder_enable(struct drm_encoder * encoder)126 static void vc4_dpi_encoder_enable(struct drm_encoder *encoder)
127 {
128 struct drm_device *dev = encoder->dev;
129 struct drm_display_mode *mode = &encoder->crtc->mode;
130 struct vc4_dpi_encoder *vc4_encoder = to_vc4_dpi_encoder(encoder);
131 struct vc4_dpi *dpi = vc4_encoder->dpi;
132 struct drm_connector_list_iter conn_iter;
133 struct drm_connector *connector = NULL, *connector_scan;
134 u32 dpi_c = DPI_ENABLE | DPI_OUTPUT_ENABLE_MODE;
135 int ret;
136
137 /* Look up the connector attached to DPI so we can get the
138 * bus_format. Ideally the bridge would tell us the
139 * bus_format we want, but it doesn't yet, so assume that it's
140 * uniform throughout the bridge chain.
141 */
142 drm_connector_list_iter_begin(dev, &conn_iter);
143 drm_for_each_connector_iter(connector_scan, &conn_iter) {
144 if (connector_scan->encoder == encoder) {
145 connector = connector_scan;
146 break;
147 }
148 }
149 drm_connector_list_iter_end(&conn_iter);
150
151 if (connector) {
152 if (connector->display_info.num_bus_formats) {
153 u32 bus_format = connector->display_info.bus_formats[0];
154
155 switch (bus_format) {
156 case MEDIA_BUS_FMT_RGB888_1X24:
157 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_24BIT_888_RGB,
158 DPI_FORMAT);
159 break;
160 case MEDIA_BUS_FMT_BGR888_1X24:
161 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_24BIT_888_RGB,
162 DPI_FORMAT);
163 dpi_c |= VC4_SET_FIELD(DPI_ORDER_BGR,
164 DPI_ORDER);
165 break;
166 case MEDIA_BUS_FMT_RGB666_1X24_CPADHI:
167 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_18BIT_666_RGB_2,
168 DPI_FORMAT);
169 break;
170 case MEDIA_BUS_FMT_RGB666_1X18:
171 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_18BIT_666_RGB_1,
172 DPI_FORMAT);
173 break;
174 case MEDIA_BUS_FMT_RGB565_1X16:
175 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_16BIT_565_RGB_1,
176 DPI_FORMAT);
177 break;
178 default:
179 DRM_ERROR("Unknown media bus format %d\n",
180 bus_format);
181 break;
182 }
183 }
184
185 if (connector->display_info.bus_flags & DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE)
186 dpi_c |= DPI_PIXEL_CLK_INVERT;
187
188 if (connector->display_info.bus_flags & DRM_BUS_FLAG_DE_LOW)
189 dpi_c |= DPI_OUTPUT_ENABLE_INVERT;
190 } else {
191 /* Default to 24bit if no connector found. */
192 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_24BIT_888_RGB, DPI_FORMAT);
193 }
194
195 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
196 dpi_c |= DPI_HSYNC_INVERT;
197 else if (!(mode->flags & DRM_MODE_FLAG_PHSYNC))
198 dpi_c |= DPI_HSYNC_DISABLE;
199
200 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
201 dpi_c |= DPI_VSYNC_INVERT;
202 else if (!(mode->flags & DRM_MODE_FLAG_PVSYNC))
203 dpi_c |= DPI_VSYNC_DISABLE;
204
205 DPI_WRITE(DPI_C, dpi_c);
206
207 ret = clk_set_rate(dpi->pixel_clock, mode->clock * 1000);
208 if (ret)
209 DRM_ERROR("Failed to set clock rate: %d\n", ret);
210
211 ret = clk_prepare_enable(dpi->pixel_clock);
212 if (ret)
213 DRM_ERROR("Failed to set clock rate: %d\n", ret);
214 }
215
vc4_dpi_encoder_mode_valid(struct drm_encoder * encoder,const struct drm_display_mode * mode)216 static enum drm_mode_status vc4_dpi_encoder_mode_valid(struct drm_encoder *encoder,
217 const struct drm_display_mode *mode)
218 {
219 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
220 return MODE_NO_INTERLACE;
221
222 return MODE_OK;
223 }
224
225 static const struct drm_encoder_helper_funcs vc4_dpi_encoder_helper_funcs = {
226 .disable = vc4_dpi_encoder_disable,
227 .enable = vc4_dpi_encoder_enable,
228 .mode_valid = vc4_dpi_encoder_mode_valid,
229 };
230
231 static const struct of_device_id vc4_dpi_dt_match[] = {
232 { .compatible = "brcm,bcm2835-dpi", .data = NULL },
233 {}
234 };
235
236 /* Sets up the next link in the display chain, whether it's a panel or
237 * a bridge.
238 */
vc4_dpi_init_bridge(struct vc4_dpi * dpi)239 static int vc4_dpi_init_bridge(struct vc4_dpi *dpi)
240 {
241 struct device *dev = &dpi->pdev->dev;
242 struct drm_panel *panel;
243 struct drm_bridge *bridge;
244 int ret;
245
246 ret = drm_of_find_panel_or_bridge(dev->of_node, 0, 0,
247 &panel, &bridge);
248 if (ret) {
249 /* If nothing was connected in the DT, that's not an
250 * error.
251 */
252 if (ret == -ENODEV)
253 return 0;
254 else
255 return ret;
256 }
257
258 if (panel)
259 bridge = drm_panel_bridge_add_typed(panel,
260 DRM_MODE_CONNECTOR_DPI);
261
262 return drm_bridge_attach(dpi->encoder, bridge, NULL, 0);
263 }
264
vc4_dpi_bind(struct device * dev,struct device * master,void * data)265 static int vc4_dpi_bind(struct device *dev, struct device *master, void *data)
266 {
267 struct platform_device *pdev = to_platform_device(dev);
268 struct drm_device *drm = dev_get_drvdata(master);
269 struct vc4_dev *vc4 = to_vc4_dev(drm);
270 struct vc4_dpi *dpi;
271 struct vc4_dpi_encoder *vc4_dpi_encoder;
272 int ret;
273
274 dpi = devm_kzalloc(dev, sizeof(*dpi), GFP_KERNEL);
275 if (!dpi)
276 return -ENOMEM;
277
278 vc4_dpi_encoder = devm_kzalloc(dev, sizeof(*vc4_dpi_encoder),
279 GFP_KERNEL);
280 if (!vc4_dpi_encoder)
281 return -ENOMEM;
282 vc4_dpi_encoder->base.type = VC4_ENCODER_TYPE_DPI;
283 vc4_dpi_encoder->dpi = dpi;
284 dpi->encoder = &vc4_dpi_encoder->base.base;
285
286 dpi->pdev = pdev;
287 dpi->regs = vc4_ioremap_regs(pdev, 0);
288 if (IS_ERR(dpi->regs))
289 return PTR_ERR(dpi->regs);
290 dpi->regset.base = dpi->regs;
291 dpi->regset.regs = dpi_regs;
292 dpi->regset.nregs = ARRAY_SIZE(dpi_regs);
293
294 if (DPI_READ(DPI_ID) != DPI_ID_VALUE) {
295 dev_err(dev, "Port returned 0x%08x for ID instead of 0x%08x\n",
296 DPI_READ(DPI_ID), DPI_ID_VALUE);
297 return -ENODEV;
298 }
299
300 dpi->core_clock = devm_clk_get(dev, "core");
301 if (IS_ERR(dpi->core_clock)) {
302 ret = PTR_ERR(dpi->core_clock);
303 if (ret != -EPROBE_DEFER)
304 DRM_ERROR("Failed to get core clock: %d\n", ret);
305 return ret;
306 }
307 dpi->pixel_clock = devm_clk_get(dev, "pixel");
308 if (IS_ERR(dpi->pixel_clock)) {
309 ret = PTR_ERR(dpi->pixel_clock);
310 if (ret != -EPROBE_DEFER)
311 DRM_ERROR("Failed to get pixel clock: %d\n", ret);
312 return ret;
313 }
314
315 ret = clk_prepare_enable(dpi->core_clock);
316 if (ret)
317 DRM_ERROR("Failed to turn on core clock: %d\n", ret);
318
319 drm_simple_encoder_init(drm, dpi->encoder, DRM_MODE_ENCODER_DPI);
320 drm_encoder_helper_add(dpi->encoder, &vc4_dpi_encoder_helper_funcs);
321
322 ret = vc4_dpi_init_bridge(dpi);
323 if (ret)
324 goto err_destroy_encoder;
325
326 dev_set_drvdata(dev, dpi);
327
328 vc4->dpi = dpi;
329
330 vc4_debugfs_add_regset32(drm, "dpi_regs", &dpi->regset);
331
332 return 0;
333
334 err_destroy_encoder:
335 drm_encoder_cleanup(dpi->encoder);
336 clk_disable_unprepare(dpi->core_clock);
337 return ret;
338 }
339
vc4_dpi_unbind(struct device * dev,struct device * master,void * data)340 static void vc4_dpi_unbind(struct device *dev, struct device *master,
341 void *data)
342 {
343 struct drm_device *drm = dev_get_drvdata(master);
344 struct vc4_dev *vc4 = to_vc4_dev(drm);
345 struct vc4_dpi *dpi = dev_get_drvdata(dev);
346
347 drm_of_panel_bridge_remove(dev->of_node, 0, 0);
348
349 drm_encoder_cleanup(dpi->encoder);
350
351 clk_disable_unprepare(dpi->core_clock);
352
353 vc4->dpi = NULL;
354 }
355
356 static const struct component_ops vc4_dpi_ops = {
357 .bind = vc4_dpi_bind,
358 .unbind = vc4_dpi_unbind,
359 };
360
vc4_dpi_dev_probe(struct platform_device * pdev)361 static int vc4_dpi_dev_probe(struct platform_device *pdev)
362 {
363 return component_add(&pdev->dev, &vc4_dpi_ops);
364 }
365
vc4_dpi_dev_remove(struct platform_device * pdev)366 static int vc4_dpi_dev_remove(struct platform_device *pdev)
367 {
368 component_del(&pdev->dev, &vc4_dpi_ops);
369 return 0;
370 }
371
372 struct platform_driver vc4_dpi_driver = {
373 .probe = vc4_dpi_dev_probe,
374 .remove = vc4_dpi_dev_remove,
375 .driver = {
376 .name = "vc4_dpi",
377 .of_match_table = vc4_dpi_dt_match,
378 },
379 };
380