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 
17 #include "fsl_dcu_drm_drv.h"
18 #include "fsl_tcon.h"
19 
20 static void fsl_dcu_drm_encoder_destroy(struct drm_encoder *encoder)
21 {
22 	drm_encoder_cleanup(encoder);
23 }
24 
25 static const struct drm_encoder_funcs encoder_funcs = {
26 	.destroy = fsl_dcu_drm_encoder_destroy,
27 };
28 
29 int fsl_dcu_drm_encoder_create(struct fsl_dcu_drm_device *fsl_dev,
30 			       struct drm_crtc *crtc)
31 {
32 	struct drm_encoder *encoder = &fsl_dev->encoder;
33 	int ret;
34 
35 	encoder->possible_crtcs = 1;
36 
37 	/* Use bypass mode for parallel RGB/LVDS encoder */
38 	if (fsl_dev->tcon)
39 		fsl_tcon_bypass_enable(fsl_dev->tcon);
40 
41 	ret = drm_encoder_init(fsl_dev->drm, encoder, &encoder_funcs,
42 			       DRM_MODE_ENCODER_LVDS, NULL);
43 	if (ret < 0)
44 		return ret;
45 
46 	return 0;
47 }
48 
49 static void fsl_dcu_drm_connector_destroy(struct drm_connector *connector)
50 {
51 	struct fsl_dcu_drm_connector *fsl_con = to_fsl_dcu_connector(connector);
52 
53 	drm_connector_unregister(connector);
54 	drm_panel_detach(fsl_con->panel);
55 	drm_connector_cleanup(connector);
56 }
57 
58 static const struct drm_connector_funcs fsl_dcu_drm_connector_funcs = {
59 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
60 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
61 	.destroy = fsl_dcu_drm_connector_destroy,
62 	.fill_modes = drm_helper_probe_single_connector_modes,
63 	.reset = drm_atomic_helper_connector_reset,
64 };
65 
66 static int fsl_dcu_drm_connector_get_modes(struct drm_connector *connector)
67 {
68 	struct fsl_dcu_drm_connector *fsl_connector;
69 
70 	fsl_connector = to_fsl_dcu_connector(connector);
71 	return drm_panel_get_modes(fsl_connector->panel, connector);
72 }
73 
74 static int fsl_dcu_drm_connector_mode_valid(struct drm_connector *connector,
75 					    struct drm_display_mode *mode)
76 {
77 	if (mode->hdisplay & 0xf)
78 		return MODE_ERROR;
79 
80 	return MODE_OK;
81 }
82 
83 static const struct drm_connector_helper_funcs connector_helper_funcs = {
84 	.get_modes = fsl_dcu_drm_connector_get_modes,
85 	.mode_valid = fsl_dcu_drm_connector_mode_valid,
86 };
87 
88 static int fsl_dcu_attach_panel(struct fsl_dcu_drm_device *fsl_dev,
89 				 struct drm_panel *panel)
90 {
91 	struct drm_encoder *encoder = &fsl_dev->encoder;
92 	struct drm_connector *connector = &fsl_dev->connector.base;
93 	int ret;
94 
95 	fsl_dev->connector.encoder = encoder;
96 
97 	ret = drm_connector_init(fsl_dev->drm, connector,
98 				 &fsl_dcu_drm_connector_funcs,
99 				 DRM_MODE_CONNECTOR_LVDS);
100 	if (ret < 0)
101 		return ret;
102 
103 	drm_connector_helper_add(connector, &connector_helper_funcs);
104 	ret = drm_connector_register(connector);
105 	if (ret < 0)
106 		goto err_cleanup;
107 
108 	ret = drm_connector_attach_encoder(connector, encoder);
109 	if (ret < 0)
110 		goto err_sysfs;
111 
112 	ret = drm_panel_attach(panel, connector);
113 	if (ret) {
114 		dev_err(fsl_dev->dev, "failed to attach panel\n");
115 		goto err_sysfs;
116 	}
117 
118 	return 0;
119 
120 err_sysfs:
121 	drm_connector_unregister(connector);
122 err_cleanup:
123 	drm_connector_cleanup(connector);
124 	return ret;
125 }
126 
127 int fsl_dcu_create_outputs(struct fsl_dcu_drm_device *fsl_dev)
128 {
129 	struct device_node *panel_node;
130 	struct drm_panel *panel;
131 	struct drm_bridge *bridge;
132 	int ret;
133 
134 	/* This is for backward compatibility */
135 	panel_node = of_parse_phandle(fsl_dev->np, "fsl,panel", 0);
136 	if (panel_node) {
137 		fsl_dev->connector.panel = of_drm_find_panel(panel_node);
138 		of_node_put(panel_node);
139 		if (IS_ERR(fsl_dev->connector.panel))
140 			return PTR_ERR(fsl_dev->connector.panel);
141 
142 		return fsl_dcu_attach_panel(fsl_dev, fsl_dev->connector.panel);
143 	}
144 
145 	ret = drm_of_find_panel_or_bridge(fsl_dev->np, 0, 0, &panel, &bridge);
146 	if (ret)
147 		return ret;
148 
149 	if (panel) {
150 		fsl_dev->connector.panel = panel;
151 		return fsl_dcu_attach_panel(fsl_dev, panel);
152 	}
153 
154 	return drm_bridge_attach(&fsl_dev->encoder, bridge, NULL, 0);
155 }
156