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 enum drm_mode_status
64 fsl_dcu_drm_connector_mode_valid(struct drm_connector *connector,
65 				 struct drm_display_mode *mode)
66 {
67 	if (mode->hdisplay & 0xf)
68 		return MODE_ERROR;
69 
70 	return MODE_OK;
71 }
72 
73 static const struct drm_connector_helper_funcs connector_helper_funcs = {
74 	.get_modes = fsl_dcu_drm_connector_get_modes,
75 	.mode_valid = fsl_dcu_drm_connector_mode_valid,
76 };
77 
78 static int fsl_dcu_attach_panel(struct fsl_dcu_drm_device *fsl_dev,
79 				 struct drm_panel *panel)
80 {
81 	struct drm_encoder *encoder = &fsl_dev->encoder;
82 	struct drm_connector *connector = &fsl_dev->connector.base;
83 	int ret;
84 
85 	fsl_dev->connector.encoder = encoder;
86 
87 	ret = drm_connector_init(fsl_dev->drm, connector,
88 				 &fsl_dcu_drm_connector_funcs,
89 				 DRM_MODE_CONNECTOR_LVDS);
90 	if (ret < 0)
91 		return ret;
92 
93 	drm_connector_helper_add(connector, &connector_helper_funcs);
94 	ret = drm_connector_register(connector);
95 	if (ret < 0)
96 		goto err_cleanup;
97 
98 	ret = drm_connector_attach_encoder(connector, encoder);
99 	if (ret < 0)
100 		goto err_sysfs;
101 
102 	return 0;
103 
104 err_sysfs:
105 	drm_connector_unregister(connector);
106 err_cleanup:
107 	drm_connector_cleanup(connector);
108 	return ret;
109 }
110 
111 int fsl_dcu_create_outputs(struct fsl_dcu_drm_device *fsl_dev)
112 {
113 	struct device_node *panel_node;
114 	struct drm_panel *panel;
115 	struct drm_bridge *bridge;
116 	int ret;
117 
118 	/* This is for backward compatibility */
119 	panel_node = of_parse_phandle(fsl_dev->np, "fsl,panel", 0);
120 	if (panel_node) {
121 		fsl_dev->connector.panel = of_drm_find_panel(panel_node);
122 		of_node_put(panel_node);
123 		if (IS_ERR(fsl_dev->connector.panel))
124 			return PTR_ERR(fsl_dev->connector.panel);
125 
126 		return fsl_dcu_attach_panel(fsl_dev, fsl_dev->connector.panel);
127 	}
128 
129 	ret = drm_of_find_panel_or_bridge(fsl_dev->np, 0, 0, &panel, &bridge);
130 	if (ret)
131 		return ret;
132 
133 	if (panel) {
134 		fsl_dev->connector.panel = panel;
135 		return fsl_dcu_attach_panel(fsl_dev, panel);
136 	}
137 
138 	return drm_bridge_attach(&fsl_dev->encoder, bridge, NULL, 0);
139 }
140