1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright 2015 Freescale Semiconductor, Inc.
4  *
5  * Freescale DCU drm device driver
6  */
7 
8 #include <linux/backlight.h>
9 #include <linux/of_graph.h>
10 
11 #include <drm/drm_atomic_helper.h>
12 #include <drm/drm_bridge.h>
13 #include <drm/drm_of.h>
14 #include <drm/drm_panel.h>
15 #include <drm/drm_probe_helper.h>
16 #include <drm/drm_simple_kms_helper.h>
17 
18 #include "fsl_dcu_drm_drv.h"
19 #include "fsl_tcon.h"
20 
21 int fsl_dcu_drm_encoder_create(struct fsl_dcu_drm_device *fsl_dev,
22 			       struct drm_crtc *crtc)
23 {
24 	struct drm_encoder *encoder = &fsl_dev->encoder;
25 	int ret;
26 
27 	encoder->possible_crtcs = 1;
28 
29 	/* Use bypass mode for parallel RGB/LVDS encoder */
30 	if (fsl_dev->tcon)
31 		fsl_tcon_bypass_enable(fsl_dev->tcon);
32 
33 	ret = drm_simple_encoder_init(fsl_dev->drm, encoder,
34 				      DRM_MODE_ENCODER_LVDS);
35 	if (ret < 0)
36 		return ret;
37 
38 	return 0;
39 }
40 
41 static void fsl_dcu_drm_connector_destroy(struct drm_connector *connector)
42 {
43 	drm_connector_unregister(connector);
44 	drm_connector_cleanup(connector);
45 }
46 
47 static const struct drm_connector_funcs fsl_dcu_drm_connector_funcs = {
48 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
49 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
50 	.destroy = fsl_dcu_drm_connector_destroy,
51 	.fill_modes = drm_helper_probe_single_connector_modes,
52 	.reset = drm_atomic_helper_connector_reset,
53 };
54 
55 static int fsl_dcu_drm_connector_get_modes(struct drm_connector *connector)
56 {
57 	struct fsl_dcu_drm_connector *fsl_connector;
58 
59 	fsl_connector = to_fsl_dcu_connector(connector);
60 	return drm_panel_get_modes(fsl_connector->panel, connector);
61 }
62 
63 static int fsl_dcu_drm_connector_mode_valid(struct drm_connector *connector,
64 					    struct drm_display_mode *mode)
65 {
66 	if (mode->hdisplay & 0xf)
67 		return MODE_ERROR;
68 
69 	return MODE_OK;
70 }
71 
72 static const struct drm_connector_helper_funcs connector_helper_funcs = {
73 	.get_modes = fsl_dcu_drm_connector_get_modes,
74 	.mode_valid = fsl_dcu_drm_connector_mode_valid,
75 };
76 
77 static int fsl_dcu_attach_panel(struct fsl_dcu_drm_device *fsl_dev,
78 				 struct drm_panel *panel)
79 {
80 	struct drm_encoder *encoder = &fsl_dev->encoder;
81 	struct drm_connector *connector = &fsl_dev->connector.base;
82 	int ret;
83 
84 	fsl_dev->connector.encoder = encoder;
85 
86 	ret = drm_connector_init(fsl_dev->drm, connector,
87 				 &fsl_dcu_drm_connector_funcs,
88 				 DRM_MODE_CONNECTOR_LVDS);
89 	if (ret < 0)
90 		return ret;
91 
92 	drm_connector_helper_add(connector, &connector_helper_funcs);
93 	ret = drm_connector_register(connector);
94 	if (ret < 0)
95 		goto err_cleanup;
96 
97 	ret = drm_connector_attach_encoder(connector, encoder);
98 	if (ret < 0)
99 		goto err_sysfs;
100 
101 	return 0;
102 
103 err_sysfs:
104 	drm_connector_unregister(connector);
105 err_cleanup:
106 	drm_connector_cleanup(connector);
107 	return ret;
108 }
109 
110 int fsl_dcu_create_outputs(struct fsl_dcu_drm_device *fsl_dev)
111 {
112 	struct device_node *panel_node;
113 	struct drm_panel *panel;
114 	struct drm_bridge *bridge;
115 	int ret;
116 
117 	/* This is for backward compatibility */
118 	panel_node = of_parse_phandle(fsl_dev->np, "fsl,panel", 0);
119 	if (panel_node) {
120 		fsl_dev->connector.panel = of_drm_find_panel(panel_node);
121 		of_node_put(panel_node);
122 		if (IS_ERR(fsl_dev->connector.panel))
123 			return PTR_ERR(fsl_dev->connector.panel);
124 
125 		return fsl_dcu_attach_panel(fsl_dev, fsl_dev->connector.panel);
126 	}
127 
128 	ret = drm_of_find_panel_or_bridge(fsl_dev->np, 0, 0, &panel, &bridge);
129 	if (ret)
130 		return ret;
131 
132 	if (panel) {
133 		fsl_dev->connector.panel = panel;
134 		return fsl_dcu_attach_panel(fsl_dev, panel);
135 	}
136 
137 	return drm_bridge_attach(&fsl_dev->encoder, bridge, NULL, 0);
138 }
139