Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gpu / drm / bridge / display-connector.c
blob4d278573cdb99817bab4e58705fedcd9f43f8949
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2019 Laurent Pinchart <laurent.pinchart@ideasonboard.com>
4 */
6 #include <linux/gpio/consumer.h>
7 #include <linux/i2c.h>
8 #include <linux/interrupt.h>
9 #include <linux/module.h>
10 #include <linux/mutex.h>
11 #include <linux/of.h>
12 #include <linux/of_device.h>
13 #include <linux/platform_device.h>
15 #include <drm/drm_bridge.h>
16 #include <drm/drm_edid.h>
18 struct display_connector {
19 struct drm_bridge bridge;
21 struct gpio_desc *hpd_gpio;
22 int hpd_irq;
25 static inline struct display_connector *
26 to_display_connector(struct drm_bridge *bridge)
28 return container_of(bridge, struct display_connector, bridge);
31 static int display_connector_attach(struct drm_bridge *bridge,
32 enum drm_bridge_attach_flags flags)
34 return flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR ? 0 : -EINVAL;
37 static enum drm_connector_status
38 display_connector_detect(struct drm_bridge *bridge)
40 struct display_connector *conn = to_display_connector(bridge);
42 if (conn->hpd_gpio) {
43 if (gpiod_get_value_cansleep(conn->hpd_gpio))
44 return connector_status_connected;
45 else
46 return connector_status_disconnected;
49 if (conn->bridge.ddc && drm_probe_ddc(conn->bridge.ddc))
50 return connector_status_connected;
52 switch (conn->bridge.type) {
53 case DRM_MODE_CONNECTOR_DVIA:
54 case DRM_MODE_CONNECTOR_DVID:
55 case DRM_MODE_CONNECTOR_DVII:
56 case DRM_MODE_CONNECTOR_HDMIA:
57 case DRM_MODE_CONNECTOR_HDMIB:
59 * For DVI and HDMI connectors a DDC probe failure indicates
60 * that no cable is connected.
62 return connector_status_disconnected;
64 case DRM_MODE_CONNECTOR_Composite:
65 case DRM_MODE_CONNECTOR_SVIDEO:
66 case DRM_MODE_CONNECTOR_VGA:
67 default:
69 * Composite and S-Video connectors have no other detection
70 * mean than the HPD GPIO. For VGA connectors, even if we have
71 * an I2C bus, we can't assume that the cable is disconnected
72 * if drm_probe_ddc fails, as some cables don't wire the DDC
73 * pins.
75 return connector_status_unknown;
79 static struct edid *display_connector_get_edid(struct drm_bridge *bridge,
80 struct drm_connector *connector)
82 struct display_connector *conn = to_display_connector(bridge);
84 return drm_get_edid(connector, conn->bridge.ddc);
87 static const struct drm_bridge_funcs display_connector_bridge_funcs = {
88 .attach = display_connector_attach,
89 .detect = display_connector_detect,
90 .get_edid = display_connector_get_edid,
93 static irqreturn_t display_connector_hpd_irq(int irq, void *arg)
95 struct display_connector *conn = arg;
96 struct drm_bridge *bridge = &conn->bridge;
98 drm_bridge_hpd_notify(bridge, display_connector_detect(bridge));
100 return IRQ_HANDLED;
103 static int display_connector_probe(struct platform_device *pdev)
105 struct display_connector *conn;
106 unsigned int type;
107 const char *label;
108 int ret;
110 conn = devm_kzalloc(&pdev->dev, sizeof(*conn), GFP_KERNEL);
111 if (!conn)
112 return -ENOMEM;
114 platform_set_drvdata(pdev, conn);
116 type = (uintptr_t)of_device_get_match_data(&pdev->dev);
118 /* Get the exact connector type. */
119 switch (type) {
120 case DRM_MODE_CONNECTOR_DVII: {
121 bool analog, digital;
123 analog = of_property_read_bool(pdev->dev.of_node, "analog");
124 digital = of_property_read_bool(pdev->dev.of_node, "digital");
125 if (analog && !digital) {
126 conn->bridge.type = DRM_MODE_CONNECTOR_DVIA;
127 } else if (!analog && digital) {
128 conn->bridge.type = DRM_MODE_CONNECTOR_DVID;
129 } else if (analog && digital) {
130 conn->bridge.type = DRM_MODE_CONNECTOR_DVII;
131 } else {
132 dev_err(&pdev->dev, "DVI connector with no type\n");
133 return -EINVAL;
135 break;
138 case DRM_MODE_CONNECTOR_HDMIA: {
139 const char *hdmi_type;
141 ret = of_property_read_string(pdev->dev.of_node, "type",
142 &hdmi_type);
143 if (ret < 0) {
144 dev_err(&pdev->dev, "HDMI connector with no type\n");
145 return -EINVAL;
148 if (!strcmp(hdmi_type, "a") || !strcmp(hdmi_type, "c") ||
149 !strcmp(hdmi_type, "d") || !strcmp(hdmi_type, "e")) {
150 conn->bridge.type = DRM_MODE_CONNECTOR_HDMIA;
151 } else if (!strcmp(hdmi_type, "b")) {
152 conn->bridge.type = DRM_MODE_CONNECTOR_HDMIB;
153 } else {
154 dev_err(&pdev->dev,
155 "Unsupported HDMI connector type '%s'\n",
156 hdmi_type);
157 return -EINVAL;
160 break;
163 default:
164 conn->bridge.type = type;
165 break;
168 /* All the supported connector types support interlaced modes. */
169 conn->bridge.interlace_allowed = true;
171 /* Get the optional connector label. */
172 of_property_read_string(pdev->dev.of_node, "label", &label);
175 * Get the HPD GPIO for DVI and HDMI connectors. If the GPIO can provide
176 * edge interrupts, register an interrupt handler.
178 if (type == DRM_MODE_CONNECTOR_DVII ||
179 type == DRM_MODE_CONNECTOR_HDMIA) {
180 conn->hpd_gpio = devm_gpiod_get_optional(&pdev->dev, "hpd",
181 GPIOD_IN);
182 if (IS_ERR(conn->hpd_gpio)) {
183 if (PTR_ERR(conn->hpd_gpio) != -EPROBE_DEFER)
184 dev_err(&pdev->dev,
185 "Unable to retrieve HPD GPIO\n");
186 return PTR_ERR(conn->hpd_gpio);
189 conn->hpd_irq = gpiod_to_irq(conn->hpd_gpio);
190 } else {
191 conn->hpd_irq = -EINVAL;
194 if (conn->hpd_irq >= 0) {
195 ret = devm_request_threaded_irq(&pdev->dev, conn->hpd_irq,
196 NULL, display_connector_hpd_irq,
197 IRQF_TRIGGER_RISING |
198 IRQF_TRIGGER_FALLING |
199 IRQF_ONESHOT,
200 "HPD", conn);
201 if (ret) {
202 dev_info(&pdev->dev,
203 "Failed to request HPD edge interrupt, falling back to polling\n");
204 conn->hpd_irq = -EINVAL;
208 /* Retrieve the DDC I2C adapter for DVI, HDMI and VGA connectors. */
209 if (type == DRM_MODE_CONNECTOR_DVII ||
210 type == DRM_MODE_CONNECTOR_HDMIA ||
211 type == DRM_MODE_CONNECTOR_VGA) {
212 struct device_node *phandle;
214 phandle = of_parse_phandle(pdev->dev.of_node, "ddc-i2c-bus", 0);
215 if (phandle) {
216 conn->bridge.ddc = of_get_i2c_adapter_by_node(phandle);
217 of_node_put(phandle);
218 if (!conn->bridge.ddc)
219 return -EPROBE_DEFER;
220 } else {
221 dev_dbg(&pdev->dev,
222 "No I2C bus specified, disabling EDID readout\n");
226 conn->bridge.funcs = &display_connector_bridge_funcs;
227 conn->bridge.of_node = pdev->dev.of_node;
229 if (conn->bridge.ddc)
230 conn->bridge.ops |= DRM_BRIDGE_OP_EDID
231 | DRM_BRIDGE_OP_DETECT;
232 if (conn->hpd_gpio)
233 conn->bridge.ops |= DRM_BRIDGE_OP_DETECT;
234 if (conn->hpd_irq >= 0)
235 conn->bridge.ops |= DRM_BRIDGE_OP_HPD;
237 dev_dbg(&pdev->dev,
238 "Found %s display connector '%s' %s DDC bus and %s HPD GPIO (ops 0x%x)\n",
239 drm_get_connector_type_name(conn->bridge.type),
240 label ? label : "<unlabelled>",
241 conn->bridge.ddc ? "with" : "without",
242 conn->hpd_gpio ? "with" : "without",
243 conn->bridge.ops);
245 drm_bridge_add(&conn->bridge);
247 return 0;
250 static int display_connector_remove(struct platform_device *pdev)
252 struct display_connector *conn = platform_get_drvdata(pdev);
254 drm_bridge_remove(&conn->bridge);
256 if (!IS_ERR(conn->bridge.ddc))
257 i2c_put_adapter(conn->bridge.ddc);
259 return 0;
262 static const struct of_device_id display_connector_match[] = {
264 .compatible = "composite-video-connector",
265 .data = (void *)DRM_MODE_CONNECTOR_Composite,
266 }, {
267 .compatible = "dvi-connector",
268 .data = (void *)DRM_MODE_CONNECTOR_DVII,
269 }, {
270 .compatible = "hdmi-connector",
271 .data = (void *)DRM_MODE_CONNECTOR_HDMIA,
272 }, {
273 .compatible = "svideo-connector",
274 .data = (void *)DRM_MODE_CONNECTOR_SVIDEO,
275 }, {
276 .compatible = "vga-connector",
277 .data = (void *)DRM_MODE_CONNECTOR_VGA,
281 MODULE_DEVICE_TABLE(of, display_connector_match);
283 static struct platform_driver display_connector_driver = {
284 .probe = display_connector_probe,
285 .remove = display_connector_remove,
286 .driver = {
287 .name = "display-connector",
288 .of_match_table = display_connector_match,
291 module_platform_driver(display_connector_driver);
293 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
294 MODULE_DESCRIPTION("Display connector driver");
295 MODULE_LICENSE("GPL");