1 /* 2 * Copyright (C) 2015 Texas Instruments 3 * Author: Jyri Sarha <jsarha@ti.com> 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 as published by 7 * the Free Software Foundation. 8 * 9 */ 10 11 #include <linux/component.h> 12 #include <linux/of_graph.h> 13 #include <drm/drm_atomic_helper.h> 14 #include <drm/drm_of.h> 15 16 #include "tilcdc_drv.h" 17 #include "tilcdc_external.h" 18 19 static const struct tilcdc_panel_info panel_info_tda998x = { 20 .ac_bias = 255, 21 .ac_bias_intrpt = 0, 22 .dma_burst_sz = 16, 23 .bpp = 16, 24 .fdd = 0x80, 25 .tft_alt_mode = 0, 26 .invert_pxl_clk = 1, 27 .sync_edge = 1, 28 .sync_ctrl = 1, 29 .raster_order = 0, 30 }; 31 32 static const struct tilcdc_panel_info panel_info_default = { 33 .ac_bias = 255, 34 .ac_bias_intrpt = 0, 35 .dma_burst_sz = 16, 36 .bpp = 16, 37 .fdd = 0x80, 38 .tft_alt_mode = 0, 39 .sync_edge = 0, 40 .sync_ctrl = 1, 41 .raster_order = 0, 42 }; 43 44 static int tilcdc_external_mode_valid(struct drm_connector *connector, 45 struct drm_display_mode *mode) 46 { 47 struct tilcdc_drm_private *priv = connector->dev->dev_private; 48 int ret; 49 50 ret = tilcdc_crtc_mode_valid(priv->crtc, mode); 51 if (ret != MODE_OK) 52 return ret; 53 54 BUG_ON(priv->external_connector != connector); 55 BUG_ON(!priv->connector_funcs); 56 57 /* If the connector has its own mode_valid call it. */ 58 if (!IS_ERR(priv->connector_funcs) && 59 priv->connector_funcs->mode_valid) 60 return priv->connector_funcs->mode_valid(connector, mode); 61 62 return MODE_OK; 63 } 64 65 static int tilcdc_add_external_connector(struct drm_device *dev, 66 struct drm_connector *connector) 67 { 68 struct tilcdc_drm_private *priv = dev->dev_private; 69 struct drm_connector_helper_funcs *connector_funcs; 70 71 /* There should never be more than one connector */ 72 if (WARN_ON(priv->external_connector)) 73 return -EINVAL; 74 75 priv->external_connector = connector; 76 connector_funcs = devm_kzalloc(dev->dev, sizeof(*connector_funcs), 77 GFP_KERNEL); 78 if (!connector_funcs) 79 return -ENOMEM; 80 81 /* connector->helper_private contains always struct 82 * connector_helper_funcs pointer. For tilcdc crtc to have a 83 * say if a specific mode is Ok, we need to install our own 84 * helper functions. In our helper functions we copy 85 * everything else but use our own mode_valid() (above). 86 */ 87 if (connector->helper_private) { 88 priv->connector_funcs = connector->helper_private; 89 *connector_funcs = *priv->connector_funcs; 90 } else { 91 priv->connector_funcs = ERR_PTR(-ENOENT); 92 } 93 connector_funcs->mode_valid = tilcdc_external_mode_valid; 94 drm_connector_helper_add(connector, connector_funcs); 95 96 dev_dbg(dev->dev, "External connector '%s' connected\n", 97 connector->name); 98 99 return 0; 100 } 101 102 static 103 struct drm_connector *tilcdc_encoder_find_connector(struct drm_device *ddev, 104 struct drm_encoder *encoder) 105 { 106 struct drm_connector *connector; 107 108 list_for_each_entry(connector, &ddev->mode_config.connector_list, head) { 109 if (drm_connector_has_possible_encoder(connector, encoder)) 110 return connector; 111 } 112 113 dev_err(ddev->dev, "No connector found for %s encoder (id %d)\n", 114 encoder->name, encoder->base.id); 115 116 return NULL; 117 } 118 119 int tilcdc_add_component_encoder(struct drm_device *ddev) 120 { 121 struct tilcdc_drm_private *priv = ddev->dev_private; 122 struct drm_connector *connector; 123 struct drm_encoder *encoder; 124 125 list_for_each_entry(encoder, &ddev->mode_config.encoder_list, head) 126 if (encoder->possible_crtcs & (1 << priv->crtc->index)) 127 break; 128 129 if (!encoder) { 130 dev_err(ddev->dev, "%s: No suitable encoder found\n", __func__); 131 return -ENODEV; 132 } 133 134 connector = tilcdc_encoder_find_connector(ddev, encoder); 135 136 if (!connector) 137 return -ENODEV; 138 139 /* Only tda998x is supported at the moment. */ 140 tilcdc_crtc_set_simulate_vesa_sync(priv->crtc, true); 141 tilcdc_crtc_set_panel_info(priv->crtc, &panel_info_tda998x); 142 143 return tilcdc_add_external_connector(ddev, connector); 144 } 145 146 void tilcdc_remove_external_device(struct drm_device *dev) 147 { 148 struct tilcdc_drm_private *priv = dev->dev_private; 149 150 /* Restore the original helper functions, if any. */ 151 if (IS_ERR(priv->connector_funcs)) 152 drm_connector_helper_add(priv->external_connector, NULL); 153 else if (priv->connector_funcs) 154 drm_connector_helper_add(priv->external_connector, 155 priv->connector_funcs); 156 } 157 158 static const struct drm_encoder_funcs tilcdc_external_encoder_funcs = { 159 .destroy = drm_encoder_cleanup, 160 }; 161 162 static 163 int tilcdc_attach_bridge(struct drm_device *ddev, struct drm_bridge *bridge) 164 { 165 struct tilcdc_drm_private *priv = ddev->dev_private; 166 struct drm_connector *connector; 167 int ret; 168 169 priv->external_encoder->possible_crtcs = BIT(0); 170 171 ret = drm_bridge_attach(priv->external_encoder, bridge, NULL); 172 if (ret) { 173 dev_err(ddev->dev, "drm_bridge_attach() failed %d\n", ret); 174 return ret; 175 } 176 177 tilcdc_crtc_set_panel_info(priv->crtc, &panel_info_default); 178 179 connector = tilcdc_encoder_find_connector(ddev, priv->external_encoder); 180 if (!connector) 181 return -ENODEV; 182 183 ret = tilcdc_add_external_connector(ddev, connector); 184 185 return ret; 186 } 187 188 int tilcdc_attach_external_device(struct drm_device *ddev) 189 { 190 struct tilcdc_drm_private *priv = ddev->dev_private; 191 struct drm_bridge *bridge; 192 struct drm_panel *panel; 193 int ret; 194 195 ret = drm_of_find_panel_or_bridge(ddev->dev->of_node, 0, 0, 196 &panel, &bridge); 197 if (ret == -ENODEV) 198 return 0; 199 else if (ret) 200 return ret; 201 202 priv->external_encoder = devm_kzalloc(ddev->dev, 203 sizeof(*priv->external_encoder), 204 GFP_KERNEL); 205 if (!priv->external_encoder) 206 return -ENOMEM; 207 208 ret = drm_encoder_init(ddev, priv->external_encoder, 209 &tilcdc_external_encoder_funcs, 210 DRM_MODE_ENCODER_NONE, NULL); 211 if (ret) { 212 dev_err(ddev->dev, "drm_encoder_init() failed %d\n", ret); 213 return ret; 214 } 215 216 if (panel) { 217 bridge = devm_drm_panel_bridge_add(ddev->dev, panel, 218 DRM_MODE_CONNECTOR_DPI); 219 if (IS_ERR(bridge)) { 220 ret = PTR_ERR(bridge); 221 goto err_encoder_cleanup; 222 } 223 } 224 225 ret = tilcdc_attach_bridge(ddev, bridge); 226 if (ret) 227 goto err_encoder_cleanup; 228 229 return 0; 230 231 err_encoder_cleanup: 232 drm_encoder_cleanup(priv->external_encoder); 233 return ret; 234 } 235 236 static int dev_match_of(struct device *dev, void *data) 237 { 238 return dev->of_node == data; 239 } 240 241 int tilcdc_get_external_components(struct device *dev, 242 struct component_match **match) 243 { 244 struct device_node *node; 245 246 node = of_graph_get_remote_node(dev->of_node, 0, 0); 247 248 if (!of_device_is_compatible(node, "nxp,tda998x")) { 249 of_node_put(node); 250 return 0; 251 } 252 253 if (match) 254 drm_of_component_match_add(dev, match, dev_match_of, node); 255 of_node_put(node); 256 return 1; 257 } 258