1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2015 Texas Instruments
4 * Author: Jyri Sarha <jsarha@ti.com>
5 */
6
7 #include <linux/component.h>
8 #include <linux/of_graph.h>
9
10 #include <drm/drm_atomic_helper.h>
11 #include <drm/drm_bridge.h>
12 #include <drm/drm_of.h>
13 #include <drm/drm_simple_kms_helper.h>
14
15 #include "tilcdc_drv.h"
16 #include "tilcdc_external.h"
17
18 static const struct tilcdc_panel_info panel_info_tda998x = {
19 .ac_bias = 255,
20 .ac_bias_intrpt = 0,
21 .dma_burst_sz = 16,
22 .bpp = 16,
23 .fdd = 0x80,
24 .tft_alt_mode = 0,
25 .invert_pxl_clk = 1,
26 .sync_edge = 1,
27 .sync_ctrl = 1,
28 .raster_order = 0,
29 };
30
31 static const struct tilcdc_panel_info panel_info_default = {
32 .ac_bias = 255,
33 .ac_bias_intrpt = 0,
34 .dma_burst_sz = 16,
35 .bpp = 16,
36 .fdd = 0x80,
37 .tft_alt_mode = 0,
38 .sync_edge = 0,
39 .sync_ctrl = 1,
40 .raster_order = 0,
41 };
42
43 static
tilcdc_encoder_find_connector(struct drm_device * ddev,struct drm_encoder * encoder)44 struct drm_connector *tilcdc_encoder_find_connector(struct drm_device *ddev,
45 struct drm_encoder *encoder)
46 {
47 struct drm_connector *connector;
48
49 list_for_each_entry(connector, &ddev->mode_config.connector_list, head) {
50 if (drm_connector_has_possible_encoder(connector, encoder))
51 return connector;
52 }
53
54 dev_err(ddev->dev, "No connector found for %s encoder (id %d)\n",
55 encoder->name, encoder->base.id);
56
57 return NULL;
58 }
59
tilcdc_add_component_encoder(struct drm_device * ddev)60 int tilcdc_add_component_encoder(struct drm_device *ddev)
61 {
62 struct tilcdc_drm_private *priv = ddev->dev_private;
63 struct drm_encoder *encoder = NULL, *iter;
64
65 list_for_each_entry(iter, &ddev->mode_config.encoder_list, head)
66 if (iter->possible_crtcs & (1 << priv->crtc->index)) {
67 encoder = iter;
68 break;
69 }
70
71 if (!encoder) {
72 dev_err(ddev->dev, "%s: No suitable encoder found\n", __func__);
73 return -ENODEV;
74 }
75
76 priv->external_connector =
77 tilcdc_encoder_find_connector(ddev, encoder);
78
79 if (!priv->external_connector)
80 return -ENODEV;
81
82 /* Only tda998x is supported at the moment. */
83 tilcdc_crtc_set_simulate_vesa_sync(priv->crtc, true);
84 tilcdc_crtc_set_panel_info(priv->crtc, &panel_info_tda998x);
85
86 return 0;
87 }
88
89 static
tilcdc_attach_bridge(struct drm_device * ddev,struct drm_bridge * bridge)90 int tilcdc_attach_bridge(struct drm_device *ddev, struct drm_bridge *bridge)
91 {
92 struct tilcdc_drm_private *priv = ddev->dev_private;
93 int ret;
94
95 priv->external_encoder->possible_crtcs = BIT(0);
96
97 ret = drm_bridge_attach(priv->external_encoder, bridge, NULL, 0);
98 if (ret) {
99 dev_err(ddev->dev, "drm_bridge_attach() failed %d\n", ret);
100 return ret;
101 }
102
103 tilcdc_crtc_set_panel_info(priv->crtc, &panel_info_default);
104
105 priv->external_connector =
106 tilcdc_encoder_find_connector(ddev, priv->external_encoder);
107 if (!priv->external_connector)
108 return -ENODEV;
109
110 return 0;
111 }
112
tilcdc_attach_external_device(struct drm_device * ddev)113 int tilcdc_attach_external_device(struct drm_device *ddev)
114 {
115 struct tilcdc_drm_private *priv = ddev->dev_private;
116 struct drm_bridge *bridge;
117 struct drm_panel *panel;
118 int ret;
119
120 ret = drm_of_find_panel_or_bridge(ddev->dev->of_node, 0, 0,
121 &panel, &bridge);
122 if (ret == -ENODEV)
123 return 0;
124 else if (ret)
125 return ret;
126
127 priv->external_encoder = devm_kzalloc(ddev->dev,
128 sizeof(*priv->external_encoder),
129 GFP_KERNEL);
130 if (!priv->external_encoder)
131 return -ENOMEM;
132
133 ret = drm_simple_encoder_init(ddev, priv->external_encoder,
134 DRM_MODE_ENCODER_NONE);
135 if (ret) {
136 dev_err(ddev->dev, "drm_encoder_init() failed %d\n", ret);
137 return ret;
138 }
139
140 if (panel) {
141 bridge = devm_drm_panel_bridge_add_typed(ddev->dev, panel,
142 DRM_MODE_CONNECTOR_DPI);
143 if (IS_ERR(bridge)) {
144 ret = PTR_ERR(bridge);
145 goto err_encoder_cleanup;
146 }
147 }
148
149 ret = tilcdc_attach_bridge(ddev, bridge);
150 if (ret)
151 goto err_encoder_cleanup;
152
153 return 0;
154
155 err_encoder_cleanup:
156 drm_encoder_cleanup(priv->external_encoder);
157 return ret;
158 }
159
dev_match_of(struct device * dev,void * data)160 static int dev_match_of(struct device *dev, void *data)
161 {
162 return dev->of_node == data;
163 }
164
tilcdc_get_external_components(struct device * dev,struct component_match ** match)165 int tilcdc_get_external_components(struct device *dev,
166 struct component_match **match)
167 {
168 struct device_node *node;
169
170 node = of_graph_get_remote_node(dev->of_node, 0, 0);
171
172 if (!of_device_is_compatible(node, "nxp,tda998x")) {
173 of_node_put(node);
174 return 0;
175 }
176
177 if (match)
178 drm_of_component_match_add(dev, match, dev_match_of, node);
179 of_node_put(node);
180 return 1;
181 }
182