blob: 275fedbdbd9eae420939627e7fcdf9236cfce4fa [file] [log] [blame]
Eric Anholt08302c32016-02-10 11:42:32 -08001/*
2 * Copyright (C) 2016 Broadcom Limited
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published by
6 * the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17/**
18 * DOC: VC4 DPI module
19 *
20 * The VC4 DPI hardware supports MIPI DPI type 4 and Nokia ViSSI
21 * signals, which are routed out to GPIO0-27 with the ALT2 function.
22 */
23
24#include "drm_atomic_helper.h"
25#include "drm_crtc_helper.h"
26#include "drm_edid.h"
27#include "drm_panel.h"
28#include "linux/clk.h"
29#include "linux/component.h"
30#include "linux/of_graph.h"
31#include "linux/of_platform.h"
32#include "vc4_drv.h"
33#include "vc4_regs.h"
34
35#define DPI_C 0x00
36# define DPI_OUTPUT_ENABLE_MODE BIT(16)
37
38/* The order field takes the incoming 24 bit RGB from the pixel valve
39 * and shuffles the 3 channels.
40 */
41# define DPI_ORDER_MASK VC4_MASK(15, 14)
42# define DPI_ORDER_SHIFT 14
43# define DPI_ORDER_RGB 0
44# define DPI_ORDER_BGR 1
45# define DPI_ORDER_GRB 2
46# define DPI_ORDER_BRG 3
47
48/* The format field takes the ORDER-shuffled pixel valve data and
49 * formats it onto the output lines.
50 */
51# define DPI_FORMAT_MASK VC4_MASK(13, 11)
52# define DPI_FORMAT_SHIFT 11
53/* This define is named in the hardware, but actually just outputs 0. */
54# define DPI_FORMAT_9BIT_666_RGB 0
55/* Outputs 00000000rrrrrggggggbbbbb */
56# define DPI_FORMAT_16BIT_565_RGB_1 1
57/* Outputs 000rrrrr00gggggg000bbbbb */
58# define DPI_FORMAT_16BIT_565_RGB_2 2
59/* Outputs 00rrrrr000gggggg00bbbbb0 */
60# define DPI_FORMAT_16BIT_565_RGB_3 3
61/* Outputs 000000rrrrrrggggggbbbbbb */
62# define DPI_FORMAT_18BIT_666_RGB_1 4
63/* Outputs 00rrrrrr00gggggg00bbbbbb */
64# define DPI_FORMAT_18BIT_666_RGB_2 5
65/* Outputs rrrrrrrrggggggggbbbbbbbb */
66# define DPI_FORMAT_24BIT_888_RGB 6
67
68/* Reverses the polarity of the corresponding signal */
69# define DPI_PIXEL_CLK_INVERT BIT(10)
70# define DPI_HSYNC_INVERT BIT(9)
71# define DPI_VSYNC_INVERT BIT(8)
72# define DPI_OUTPUT_ENABLE_INVERT BIT(7)
73
74/* Outputs the signal the falling clock edge instead of rising. */
75# define DPI_HSYNC_NEGATE BIT(6)
76# define DPI_VSYNC_NEGATE BIT(5)
77# define DPI_OUTPUT_ENABLE_NEGATE BIT(4)
78
79/* Disables the signal */
80# define DPI_HSYNC_DISABLE BIT(3)
81# define DPI_VSYNC_DISABLE BIT(2)
82# define DPI_OUTPUT_ENABLE_DISABLE BIT(1)
83
84/* Power gate to the device, full reset at 0 -> 1 transition */
85# define DPI_ENABLE BIT(0)
86
87/* All other registers besides DPI_C return the ID */
88#define DPI_ID 0x04
89# define DPI_ID_VALUE 0x00647069
90
91/* General DPI hardware state. */
92struct vc4_dpi {
93 struct platform_device *pdev;
94
95 struct drm_encoder *encoder;
96 struct drm_connector *connector;
97 struct drm_panel *panel;
98
99 void __iomem *regs;
100
101 struct clk *pixel_clock;
102 struct clk *core_clock;
103};
104
105#define DPI_READ(offset) readl(dpi->regs + (offset))
106#define DPI_WRITE(offset, val) writel(val, dpi->regs + (offset))
107
108/* VC4 DPI encoder KMS struct */
109struct vc4_dpi_encoder {
110 struct vc4_encoder base;
111 struct vc4_dpi *dpi;
112};
113
114static inline struct vc4_dpi_encoder *
115to_vc4_dpi_encoder(struct drm_encoder *encoder)
116{
117 return container_of(encoder, struct vc4_dpi_encoder, base.base);
118}
119
120/* VC4 DPI connector KMS struct */
121struct vc4_dpi_connector {
122 struct drm_connector base;
123 struct vc4_dpi *dpi;
124
125 /* Since the connector is attached to just the one encoder,
126 * this is the reference to it so we can do the best_encoder()
127 * hook.
128 */
129 struct drm_encoder *encoder;
130};
131
132static inline struct vc4_dpi_connector *
133to_vc4_dpi_connector(struct drm_connector *connector)
134{
135 return container_of(connector, struct vc4_dpi_connector, base);
136}
137
138#define DPI_REG(reg) { reg, #reg }
139static const struct {
140 u32 reg;
141 const char *name;
142} dpi_regs[] = {
143 DPI_REG(DPI_C),
144 DPI_REG(DPI_ID),
145};
146
147static void vc4_dpi_dump_regs(struct vc4_dpi *dpi)
148{
149 int i;
150
151 for (i = 0; i < ARRAY_SIZE(dpi_regs); i++) {
152 DRM_INFO("0x%04x (%s): 0x%08x\n",
153 dpi_regs[i].reg, dpi_regs[i].name,
154 DPI_READ(dpi_regs[i].reg));
155 }
156}
157
158#ifdef CONFIG_DEBUG_FS
159int vc4_dpi_debugfs_regs(struct seq_file *m, void *unused)
160{
161 struct drm_info_node *node = (struct drm_info_node *)m->private;
162 struct drm_device *dev = node->minor->dev;
163 struct vc4_dev *vc4 = to_vc4_dev(dev);
164 struct vc4_dpi *dpi = vc4->dpi;
165 int i;
166
167 if (!dpi)
168 return 0;
169
170 for (i = 0; i < ARRAY_SIZE(dpi_regs); i++) {
171 seq_printf(m, "%s (0x%04x): 0x%08x\n",
172 dpi_regs[i].name, dpi_regs[i].reg,
173 DPI_READ(dpi_regs[i].reg));
174 }
175
176 return 0;
177}
178#endif
179
180static enum drm_connector_status
181vc4_dpi_connector_detect(struct drm_connector *connector, bool force)
182{
183 struct vc4_dpi_connector *vc4_connector =
184 to_vc4_dpi_connector(connector);
185 struct vc4_dpi *dpi = vc4_connector->dpi;
186
187 if (dpi->panel)
188 return connector_status_connected;
189 else
190 return connector_status_disconnected;
191}
192
193static void vc4_dpi_connector_destroy(struct drm_connector *connector)
194{
195 drm_connector_unregister(connector);
196 drm_connector_cleanup(connector);
197}
198
199static int vc4_dpi_connector_get_modes(struct drm_connector *connector)
200{
201 struct vc4_dpi_connector *vc4_connector =
202 to_vc4_dpi_connector(connector);
203 struct vc4_dpi *dpi = vc4_connector->dpi;
204
205 if (dpi->panel)
206 return drm_panel_get_modes(dpi->panel);
207
208 return 0;
209}
210
Eric Anholt08302c32016-02-10 11:42:32 -0800211static const struct drm_connector_funcs vc4_dpi_connector_funcs = {
212 .dpms = drm_atomic_helper_connector_dpms,
213 .detect = vc4_dpi_connector_detect,
214 .fill_modes = drm_helper_probe_single_connector_modes,
215 .destroy = vc4_dpi_connector_destroy,
216 .reset = drm_atomic_helper_connector_reset,
217 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
218 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
219};
220
221static const struct drm_connector_helper_funcs vc4_dpi_connector_helper_funcs = {
222 .get_modes = vc4_dpi_connector_get_modes,
Eric Anholt08302c32016-02-10 11:42:32 -0800223};
224
225static struct drm_connector *vc4_dpi_connector_init(struct drm_device *dev,
226 struct vc4_dpi *dpi)
227{
228 struct drm_connector *connector = NULL;
229 struct vc4_dpi_connector *dpi_connector;
Eric Anholt08302c32016-02-10 11:42:32 -0800230
231 dpi_connector = devm_kzalloc(dev->dev, sizeof(*dpi_connector),
232 GFP_KERNEL);
Colin Ian Kinga9402df2016-06-02 10:38:29 +0100233 if (!dpi_connector)
234 return ERR_PTR(-ENOMEM);
235
Eric Anholt08302c32016-02-10 11:42:32 -0800236 connector = &dpi_connector->base;
237
238 dpi_connector->encoder = dpi->encoder;
239 dpi_connector->dpi = dpi;
240
241 drm_connector_init(dev, connector, &vc4_dpi_connector_funcs,
242 DRM_MODE_CONNECTOR_DPI);
243 drm_connector_helper_add(connector, &vc4_dpi_connector_helper_funcs);
244
245 connector->polled = 0;
246 connector->interlace_allowed = 0;
247 connector->doublescan_allowed = 0;
248
249 drm_mode_connector_attach_encoder(connector, dpi->encoder);
250
251 return connector;
Eric Anholt08302c32016-02-10 11:42:32 -0800252}
253
254static const struct drm_encoder_funcs vc4_dpi_encoder_funcs = {
255 .destroy = drm_encoder_cleanup,
256};
257
258static void vc4_dpi_encoder_disable(struct drm_encoder *encoder)
259{
260 struct vc4_dpi_encoder *vc4_encoder = to_vc4_dpi_encoder(encoder);
261 struct vc4_dpi *dpi = vc4_encoder->dpi;
262
263 drm_panel_disable(dpi->panel);
264
265 clk_disable_unprepare(dpi->pixel_clock);
266
267 drm_panel_unprepare(dpi->panel);
268}
269
270static void vc4_dpi_encoder_enable(struct drm_encoder *encoder)
271{
272 struct drm_display_mode *mode = &encoder->crtc->mode;
273 struct vc4_dpi_encoder *vc4_encoder = to_vc4_dpi_encoder(encoder);
274 struct vc4_dpi *dpi = vc4_encoder->dpi;
275 u32 dpi_c = DPI_ENABLE | DPI_OUTPUT_ENABLE_MODE;
276 int ret;
277
278 ret = drm_panel_prepare(dpi->panel);
279 if (ret) {
280 DRM_ERROR("Panel failed to prepare\n");
281 return;
282 }
283
284 if (dpi->connector->display_info.num_bus_formats) {
285 u32 bus_format = dpi->connector->display_info.bus_formats[0];
286
287 switch (bus_format) {
288 case MEDIA_BUS_FMT_RGB888_1X24:
289 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_24BIT_888_RGB,
290 DPI_FORMAT);
291 break;
292 case MEDIA_BUS_FMT_BGR888_1X24:
293 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_24BIT_888_RGB,
294 DPI_FORMAT);
295 dpi_c |= VC4_SET_FIELD(DPI_ORDER_BGR, DPI_ORDER);
296 break;
297 case MEDIA_BUS_FMT_RGB666_1X24_CPADHI:
298 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_18BIT_666_RGB_2,
299 DPI_FORMAT);
300 break;
301 case MEDIA_BUS_FMT_RGB666_1X18:
302 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_18BIT_666_RGB_1,
303 DPI_FORMAT);
304 break;
305 case MEDIA_BUS_FMT_RGB565_1X16:
306 dpi_c |= VC4_SET_FIELD(DPI_FORMAT_16BIT_565_RGB_3,
307 DPI_FORMAT);
308 break;
309 default:
310 DRM_ERROR("Unknown media bus format %d\n", bus_format);
311 break;
312 }
313 }
314
315 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
316 dpi_c |= DPI_HSYNC_INVERT;
317 else if (!(mode->flags & DRM_MODE_FLAG_PHSYNC))
318 dpi_c |= DPI_HSYNC_DISABLE;
319
320 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
321 dpi_c |= DPI_VSYNC_INVERT;
322 else if (!(mode->flags & DRM_MODE_FLAG_PVSYNC))
323 dpi_c |= DPI_VSYNC_DISABLE;
324
325 DPI_WRITE(DPI_C, dpi_c);
326
327 ret = clk_set_rate(dpi->pixel_clock, mode->clock * 1000);
328 if (ret)
329 DRM_ERROR("Failed to set clock rate: %d\n", ret);
330
331 ret = clk_prepare_enable(dpi->pixel_clock);
332 if (ret)
333 DRM_ERROR("Failed to set clock rate: %d\n", ret);
334
335 ret = drm_panel_enable(dpi->panel);
336 if (ret) {
337 DRM_ERROR("Panel failed to enable\n");
338 drm_panel_unprepare(dpi->panel);
339 return;
340 }
341}
342
343static const struct drm_encoder_helper_funcs vc4_dpi_encoder_helper_funcs = {
344 .disable = vc4_dpi_encoder_disable,
345 .enable = vc4_dpi_encoder_enable,
346};
347
348static const struct of_device_id vc4_dpi_dt_match[] = {
349 { .compatible = "brcm,bcm2835-dpi", .data = NULL },
350 {}
351};
352
353/* Walks the OF graph to find the panel node and then asks DRM to look
354 * up the panel.
355 */
356static struct drm_panel *vc4_dpi_get_panel(struct device *dev)
357{
358 struct device_node *endpoint, *panel_node;
359 struct device_node *np = dev->of_node;
360 struct drm_panel *panel;
361
362 endpoint = of_graph_get_next_endpoint(np, NULL);
363 if (!endpoint) {
364 dev_err(dev, "no endpoint to fetch DPI panel\n");
365 return NULL;
366 }
367
368 /* don't proceed if we have an endpoint but no panel_node tied to it */
369 panel_node = of_graph_get_remote_port_parent(endpoint);
370 of_node_put(endpoint);
371 if (!panel_node) {
372 dev_err(dev, "no valid panel node\n");
373 return NULL;
374 }
375
376 panel = of_drm_find_panel(panel_node);
377 of_node_put(panel_node);
378
379 return panel;
380}
381
382static int vc4_dpi_bind(struct device *dev, struct device *master, void *data)
383{
384 struct platform_device *pdev = to_platform_device(dev);
385 struct drm_device *drm = dev_get_drvdata(master);
386 struct vc4_dev *vc4 = to_vc4_dev(drm);
387 struct vc4_dpi *dpi;
388 struct vc4_dpi_encoder *vc4_dpi_encoder;
389 int ret;
390
391 dpi = devm_kzalloc(dev, sizeof(*dpi), GFP_KERNEL);
392 if (!dpi)
393 return -ENOMEM;
394
395 vc4_dpi_encoder = devm_kzalloc(dev, sizeof(*vc4_dpi_encoder),
396 GFP_KERNEL);
397 if (!vc4_dpi_encoder)
398 return -ENOMEM;
399 vc4_dpi_encoder->base.type = VC4_ENCODER_TYPE_DPI;
400 vc4_dpi_encoder->dpi = dpi;
401 dpi->encoder = &vc4_dpi_encoder->base.base;
402
403 dpi->pdev = pdev;
404 dpi->regs = vc4_ioremap_regs(pdev, 0);
405 if (IS_ERR(dpi->regs))
406 return PTR_ERR(dpi->regs);
407
408 vc4_dpi_dump_regs(dpi);
409
410 if (DPI_READ(DPI_ID) != DPI_ID_VALUE) {
411 dev_err(dev, "Port returned 0x%08x for ID instead of 0x%08x\n",
412 DPI_READ(DPI_ID), DPI_ID_VALUE);
413 return -ENODEV;
414 }
415
416 dpi->core_clock = devm_clk_get(dev, "core");
417 if (IS_ERR(dpi->core_clock)) {
418 ret = PTR_ERR(dpi->core_clock);
419 if (ret != -EPROBE_DEFER)
420 DRM_ERROR("Failed to get core clock: %d\n", ret);
421 return ret;
422 }
423 dpi->pixel_clock = devm_clk_get(dev, "pixel");
424 if (IS_ERR(dpi->pixel_clock)) {
425 ret = PTR_ERR(dpi->pixel_clock);
426 if (ret != -EPROBE_DEFER)
427 DRM_ERROR("Failed to get pixel clock: %d\n", ret);
428 return ret;
429 }
430
431 ret = clk_prepare_enable(dpi->core_clock);
432 if (ret)
433 DRM_ERROR("Failed to turn on core clock: %d\n", ret);
434
435 dpi->panel = vc4_dpi_get_panel(dev);
436
437 drm_encoder_init(drm, dpi->encoder, &vc4_dpi_encoder_funcs,
438 DRM_MODE_ENCODER_DPI, NULL);
439 drm_encoder_helper_add(dpi->encoder, &vc4_dpi_encoder_helper_funcs);
440
441 dpi->connector = vc4_dpi_connector_init(drm, dpi);
442 if (IS_ERR(dpi->connector)) {
443 ret = PTR_ERR(dpi->connector);
444 goto err_destroy_encoder;
445 }
446
447 if (dpi->panel)
448 drm_panel_attach(dpi->panel, dpi->connector);
449
450 dev_set_drvdata(dev, dpi);
451
452 vc4->dpi = dpi;
453
454 return 0;
455
456err_destroy_encoder:
457 drm_encoder_cleanup(dpi->encoder);
458 clk_disable_unprepare(dpi->core_clock);
459 return ret;
460}
461
462static void vc4_dpi_unbind(struct device *dev, struct device *master,
463 void *data)
464{
465 struct drm_device *drm = dev_get_drvdata(master);
466 struct vc4_dev *vc4 = to_vc4_dev(drm);
467 struct vc4_dpi *dpi = dev_get_drvdata(dev);
468
469 if (dpi->panel)
470 drm_panel_detach(dpi->panel);
471
472 vc4_dpi_connector_destroy(dpi->connector);
473 drm_encoder_cleanup(dpi->encoder);
474
475 clk_disable_unprepare(dpi->core_clock);
476
477 vc4->dpi = NULL;
478}
479
480static const struct component_ops vc4_dpi_ops = {
481 .bind = vc4_dpi_bind,
482 .unbind = vc4_dpi_unbind,
483};
484
485static int vc4_dpi_dev_probe(struct platform_device *pdev)
486{
487 return component_add(&pdev->dev, &vc4_dpi_ops);
488}
489
490static int vc4_dpi_dev_remove(struct platform_device *pdev)
491{
492 component_del(&pdev->dev, &vc4_dpi_ops);
493 return 0;
494}
495
496struct platform_driver vc4_dpi_driver = {
497 .probe = vc4_dpi_dev_probe,
498 .remove = vc4_dpi_dev_remove,
499 .driver = {
500 .name = "vc4_dpi",
501 .of_match_table = vc4_dpi_dt_match,
502 },
503};