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