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 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 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; 64 65 list_for_each_entry(encoder, &ddev->mode_config.encoder_list, head) 66 if (encoder->possible_crtcs & (1 << priv->crtc->index)) 67 break; 68 69 if (!encoder) { 70 dev_err(ddev->dev, "%s: No suitable encoder found\n", __func__); 71 return -ENODEV; 72 } 73 74 priv->external_connector = 75 tilcdc_encoder_find_connector(ddev, encoder); 76 77 if (!priv->external_connector) 78 return -ENODEV; 79 80 /* Only tda998x is supported at the moment. */ 81 tilcdc_crtc_set_simulate_vesa_sync(priv->crtc, true); 82 tilcdc_crtc_set_panel_info(priv->crtc, &panel_info_tda998x); 83 84 return 0; 85 } 86 87 static 88 int tilcdc_attach_bridge(struct drm_device *ddev, struct drm_bridge *bridge) 89 { 90 struct tilcdc_drm_private *priv = ddev->dev_private; 91 int ret; 92 93 priv->external_encoder->possible_crtcs = BIT(0); 94 95 ret = drm_bridge_attach(priv->external_encoder, bridge, NULL, 0); 96 if (ret) 97 return ret; 98 99 tilcdc_crtc_set_panel_info(priv->crtc, &panel_info_default); 100 101 priv->external_connector = 102 tilcdc_encoder_find_connector(ddev, priv->external_encoder); 103 if (!priv->external_connector) 104 return -ENODEV; 105 106 return 0; 107 } 108 109 int tilcdc_attach_external_device(struct drm_device *ddev) 110 { 111 struct tilcdc_drm_private *priv = ddev->dev_private; 112 struct drm_bridge *bridge; 113 struct drm_panel *panel; 114 int ret; 115 116 ret = drm_of_find_panel_or_bridge(ddev->dev->of_node, 0, 0, 117 &panel, &bridge); 118 if (ret == -ENODEV) 119 return 0; 120 else if (ret) 121 return ret; 122 123 priv->external_encoder = devm_kzalloc(ddev->dev, 124 sizeof(*priv->external_encoder), 125 GFP_KERNEL); 126 if (!priv->external_encoder) 127 return -ENOMEM; 128 129 ret = drm_simple_encoder_init(ddev, priv->external_encoder, 130 DRM_MODE_ENCODER_NONE); 131 if (ret) { 132 dev_err(ddev->dev, "drm_encoder_init() failed %d\n", ret); 133 return ret; 134 } 135 136 if (panel) { 137 bridge = devm_drm_panel_bridge_add_typed(ddev->dev, panel, 138 DRM_MODE_CONNECTOR_DPI); 139 if (IS_ERR(bridge)) { 140 ret = PTR_ERR(bridge); 141 goto err_encoder_cleanup; 142 } 143 } 144 145 ret = tilcdc_attach_bridge(ddev, bridge); 146 if (ret) 147 goto err_encoder_cleanup; 148 149 return 0; 150 151 err_encoder_cleanup: 152 drm_encoder_cleanup(priv->external_encoder); 153 return ret; 154 } 155 156 static int dev_match_of(struct device *dev, void *data) 157 { 158 return dev->of_node == data; 159 } 160 161 int tilcdc_get_external_components(struct device *dev, 162 struct component_match **match) 163 { 164 struct device_node *node; 165 166 node = of_graph_get_remote_node(dev->of_node, 0, 0); 167 168 if (!of_device_is_compatible(node, "nxp,tda998x")) { 169 of_node_put(node); 170 return 0; 171 } 172 173 if (match) 174 drm_of_component_match_add(dev, match, dev_match_of, node); 175 of_node_put(node); 176 return 1; 177 } 178