1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2015 Texas Instruments
4 * Author: Jyri Sarha <jsarha@ti.com>
7 #include <linux/component.h>
8 #include <linux/of_graph.h>
10 #include <drm/drm_atomic_helper.h>
11 #include <drm/drm_bridge.h>
12 #include <drm/drm_of.h>
14 #include "tilcdc_drv.h"
15 #include "tilcdc_external.h"
17 static const struct tilcdc_panel_info panel_info_tda998x
= {
30 static const struct tilcdc_panel_info panel_info_default
= {
43 struct drm_connector
*tilcdc_encoder_find_connector(struct drm_device
*ddev
,
44 struct drm_encoder
*encoder
)
46 struct drm_connector
*connector
;
48 list_for_each_entry(connector
, &ddev
->mode_config
.connector_list
, head
) {
49 if (drm_connector_has_possible_encoder(connector
, encoder
))
53 dev_err(ddev
->dev
, "No connector found for %s encoder (id %d)\n",
54 encoder
->name
, encoder
->base
.id
);
59 int tilcdc_add_component_encoder(struct drm_device
*ddev
)
61 struct tilcdc_drm_private
*priv
= ddev
->dev_private
;
62 struct drm_encoder
*encoder
;
64 list_for_each_entry(encoder
, &ddev
->mode_config
.encoder_list
, head
)
65 if (encoder
->possible_crtcs
& (1 << priv
->crtc
->index
))
69 dev_err(ddev
->dev
, "%s: No suitable encoder found\n", __func__
);
73 priv
->external_connector
=
74 tilcdc_encoder_find_connector(ddev
, encoder
);
76 if (!priv
->external_connector
)
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
);
86 static const struct drm_encoder_funcs tilcdc_external_encoder_funcs
= {
87 .destroy
= drm_encoder_cleanup
,
91 int tilcdc_attach_bridge(struct drm_device
*ddev
, struct drm_bridge
*bridge
)
93 struct tilcdc_drm_private
*priv
= ddev
->dev_private
;
96 priv
->external_encoder
->possible_crtcs
= BIT(0);
98 ret
= drm_bridge_attach(priv
->external_encoder
, bridge
, NULL
);
100 dev_err(ddev
->dev
, "drm_bridge_attach() failed %d\n", ret
);
104 tilcdc_crtc_set_panel_info(priv
->crtc
, &panel_info_default
);
106 priv
->external_connector
=
107 tilcdc_encoder_find_connector(ddev
, priv
->external_encoder
);
108 if (!priv
->external_connector
)
114 int tilcdc_attach_external_device(struct drm_device
*ddev
)
116 struct tilcdc_drm_private
*priv
= ddev
->dev_private
;
117 struct drm_bridge
*bridge
;
118 struct drm_panel
*panel
;
121 ret
= drm_of_find_panel_or_bridge(ddev
->dev
->of_node
, 0, 0,
128 priv
->external_encoder
= devm_kzalloc(ddev
->dev
,
129 sizeof(*priv
->external_encoder
),
131 if (!priv
->external_encoder
)
134 ret
= drm_encoder_init(ddev
, priv
->external_encoder
,
135 &tilcdc_external_encoder_funcs
,
136 DRM_MODE_ENCODER_NONE
, NULL
);
138 dev_err(ddev
->dev
, "drm_encoder_init() failed %d\n", ret
);
143 bridge
= devm_drm_panel_bridge_add_typed(ddev
->dev
, panel
,
144 DRM_MODE_CONNECTOR_DPI
);
145 if (IS_ERR(bridge
)) {
146 ret
= PTR_ERR(bridge
);
147 goto err_encoder_cleanup
;
151 ret
= tilcdc_attach_bridge(ddev
, bridge
);
153 goto err_encoder_cleanup
;
158 drm_encoder_cleanup(priv
->external_encoder
);
162 static int dev_match_of(struct device
*dev
, void *data
)
164 return dev
->of_node
== data
;
167 int tilcdc_get_external_components(struct device
*dev
,
168 struct component_match
**match
)
170 struct device_node
*node
;
172 node
= of_graph_get_remote_node(dev
->of_node
, 0, 0);
174 if (!of_device_is_compatible(node
, "nxp,tda998x")) {
180 drm_of_component_match_add(dev
, match
, dev_match_of
, node
);