drm/bridge: Add ti-tfp410 DVI transmitter driver
[linux-2.6-block.git] / drivers / gpu / drm / tilcdc / tilcdc_external.c
CommitLineData
103cd8bc
JS
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>
97ac0e47 13#include <drm/drm_of.h>
103cd8bc
JS
14
15#include "tilcdc_drv.h"
16#include "tilcdc_external.h"
17
18static 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
31static int tilcdc_external_mode_valid(struct drm_connector *connector,
32 struct drm_display_mode *mode)
33{
34 struct tilcdc_drm_private *priv = connector->dev->dev_private;
35 int ret, i;
36
37 ret = tilcdc_crtc_mode_valid(priv->crtc, mode);
38 if (ret != MODE_OK)
39 return ret;
40
41 for (i = 0; i < priv->num_connectors &&
42 priv->connectors[i] != connector; i++)
43 ;
44
45 BUG_ON(priv->connectors[i] != connector);
46 BUG_ON(!priv->connector_funcs[i]);
47
48 /* If the connector has its own mode_valid call it. */
49 if (!IS_ERR(priv->connector_funcs[i]) &&
50 priv->connector_funcs[i]->mode_valid)
51 return priv->connector_funcs[i]->mode_valid(connector, mode);
52
53 return MODE_OK;
54}
55
c5665385 56static int tilcdc_add_external_encoder(struct drm_device *dev,
103cd8bc
JS
57 struct drm_connector *connector)
58{
59 struct tilcdc_drm_private *priv = dev->dev_private;
60 struct drm_connector_helper_funcs *connector_funcs;
61
62 priv->connectors[priv->num_connectors] = connector;
63 priv->encoders[priv->num_encoders++] = connector->encoder;
64
65 /* Only tda998x is supported at the moment. */
66 tilcdc_crtc_set_simulate_vesa_sync(priv->crtc, true);
67 tilcdc_crtc_set_panel_info(priv->crtc, &panel_info_tda998x);
103cd8bc
JS
68
69 connector_funcs = devm_kzalloc(dev->dev, sizeof(*connector_funcs),
70 GFP_KERNEL);
71 if (!connector_funcs)
72 return -ENOMEM;
73
74 /* connector->helper_private contains always struct
75 * connector_helper_funcs pointer. For tilcdc crtc to have a
76 * say if a specific mode is Ok, we need to install our own
77 * helper functions. In our helper functions we copy
78 * everything else but use our own mode_valid() (above).
79 */
80 if (connector->helper_private) {
81 priv->connector_funcs[priv->num_connectors] =
82 connector->helper_private;
83 *connector_funcs = *priv->connector_funcs[priv->num_connectors];
84 } else {
85 priv->connector_funcs[priv->num_connectors] = ERR_PTR(-ENOENT);
86 }
87 connector_funcs->mode_valid = tilcdc_external_mode_valid;
88 drm_connector_helper_add(connector, connector_funcs);
89 priv->num_connectors++;
90
91 dev_dbg(dev->dev, "External encoder '%s' connected\n",
92 connector->encoder->name);
93
94 return 0;
95}
96
c5665385 97int tilcdc_add_external_encoders(struct drm_device *dev)
103cd8bc
JS
98{
99 struct tilcdc_drm_private *priv = dev->dev_private;
100 struct drm_connector *connector;
101 int num_internal_connectors = priv->num_connectors;
102
103 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
104 bool found = false;
105 int i, ret;
106
107 for (i = 0; i < num_internal_connectors; i++)
108 if (connector == priv->connectors[i])
109 found = true;
110 if (!found) {
c5665385 111 ret = tilcdc_add_external_encoder(dev, connector);
103cd8bc
JS
112 if (ret)
113 return ret;
114 }
115 }
116 return 0;
117}
118
119void tilcdc_remove_external_encoders(struct drm_device *dev)
120{
121 struct tilcdc_drm_private *priv = dev->dev_private;
122 int i;
123
124 /* Restore the original helper functions, if any. */
125 for (i = 0; i < priv->num_connectors; i++)
126 if (IS_ERR(priv->connector_funcs[i]))
127 drm_connector_helper_add(priv->connectors[i], NULL);
128 else if (priv->connector_funcs[i])
129 drm_connector_helper_add(priv->connectors[i],
130 priv->connector_funcs[i]);
131}
132
133static int dev_match_of(struct device *dev, void *data)
134{
135 return dev->of_node == data;
136}
137
138int tilcdc_get_external_components(struct device *dev,
139 struct component_match **match)
140{
10a55a18 141 struct device_node *node;
103cd8bc
JS
142 struct device_node *ep = NULL;
143 int count = 0;
144
10a55a18
JS
145 /* Avoid error print by of_graph_get_next_endpoint() if there
146 * is no ports present.
147 */
148 node = of_get_child_by_name(dev->of_node, "ports");
149 if (!node)
150 node = of_get_child_by_name(dev->of_node, "port");
151 if (!node)
152 return 0;
153 of_node_put(node);
103cd8bc 154
10a55a18 155 while ((ep = of_graph_get_next_endpoint(dev->of_node, ep))) {
103cd8bc 156 node = of_graph_get_remote_port_parent(ep);
a3479c4f 157 if (!node || !of_device_is_available(node)) {
103cd8bc
JS
158 of_node_put(node);
159 continue;
160 }
161
162 dev_dbg(dev, "Subdevice node '%s' found\n", node->name);
163 if (match)
97ac0e47
RK
164 drm_of_component_match_add(dev, match, dev_match_of,
165 node);
103cd8bc
JS
166 of_node_put(node);
167 count++;
168 }
169
170 if (count > 1) {
171 dev_err(dev, "Only one external encoder is supported\n");
172 return -EINVAL;
173 }
174
175 return count;
176}