hc
2023-12-06 08f87f769b595151be1afeff53e144f543faa614
kernel/drivers/gpu/drm/bridge/ti-tfp410.c
....@@ -1,26 +1,21 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Copyright (C) 2016 Texas Instruments
34 * 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
- *
95 */
106
11
-#include <linux/delay.h>
12
-#include <linux/fwnode.h>
137 #include <linux/gpio/consumer.h>
14
-#include <linux/irq.h>
8
+#include <linux/i2c.h>
159 #include <linux/module.h>
1610 #include <linux/of_graph.h>
1711 #include <linux/platform_device.h>
18
-#include <linux/i2c.h>
12
+#include <linux/workqueue.h>
1913
20
-#include <drm/drmP.h>
2114 #include <drm/drm_atomic_helper.h>
15
+#include <drm/drm_bridge.h>
2216 #include <drm/drm_crtc.h>
23
-#include <drm/drm_crtc_helper.h>
17
+#include <drm/drm_print.h>
18
+#include <drm/drm_probe_helper.h>
2419
2520 #define HOTPLUG_DEBOUNCE_MS 1100
2621
....@@ -28,9 +23,12 @@
2823 struct drm_bridge bridge;
2924 struct drm_connector connector;
3025
31
- struct i2c_adapter *ddc;
32
- struct gpio_desc *hpd;
26
+ u32 bus_format;
3327 struct delayed_work hpd_work;
28
+ struct gpio_desc *powerdown;
29
+
30
+ struct drm_bridge_timings timings;
31
+ struct drm_bridge *next_bridge;
3432
3533 struct device *dev;
3634 };
....@@ -53,13 +51,22 @@
5351 struct edid *edid;
5452 int ret;
5553
56
- if (!dvi->ddc)
57
- goto fallback;
54
+ if (dvi->next_bridge->ops & DRM_BRIDGE_OP_EDID) {
55
+ edid = drm_bridge_get_edid(dvi->next_bridge, connector);
56
+ if (!edid)
57
+ DRM_INFO("EDID read failed. Fallback to standard modes\n");
58
+ } else {
59
+ edid = NULL;
60
+ }
5861
59
- edid = drm_get_edid(connector, dvi->ddc);
6062 if (!edid) {
61
- DRM_INFO("EDID read failed. Fallback to standard modes\n");
62
- goto fallback;
63
+ /*
64
+ * No EDID, fallback on the XGA standard modes and prefer a mode
65
+ * pretty much anything can handle.
66
+ */
67
+ ret = drm_add_modes_noedid(connector, 1920, 1200);
68
+ drm_set_preferred_mode(connector, 1024, 768);
69
+ return ret;
6370 }
6471
6572 drm_connector_update_edid_property(connector, edid);
....@@ -67,15 +74,6 @@
6774 ret = drm_add_edid_modes(connector, edid);
6875
6976 kfree(edid);
70
-
71
- return ret;
72
-
73
-fallback:
74
- /* No EDID, fallback on the XGA standard modes */
75
- ret = drm_add_modes_noedid(connector, 1920, 1200);
76
-
77
- /* And prefer a mode pretty much anything can handle */
78
- drm_set_preferred_mode(connector, 1024, 768);
7977
8078 return ret;
8179 }
....@@ -89,21 +87,7 @@
8987 {
9088 struct tfp410 *dvi = drm_connector_to_tfp410(connector);
9189
92
- if (dvi->hpd) {
93
- if (gpiod_get_value_cansleep(dvi->hpd))
94
- return connector_status_connected;
95
- else
96
- return connector_status_disconnected;
97
- }
98
-
99
- if (dvi->ddc) {
100
- if (drm_probe_ddc(dvi->ddc))
101
- return connector_status_connected;
102
- else
103
- return connector_status_disconnected;
104
- }
105
-
106
- return connector_status_unknown;
90
+ return drm_bridge_detect(dvi->next_bridge);
10791 }
10892
10993 static const struct drm_connector_funcs tfp410_con_funcs = {
....@@ -113,38 +97,6 @@
11397 .reset = drm_atomic_helper_connector_reset,
11498 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
11599 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
116
-};
117
-
118
-static int tfp410_attach(struct drm_bridge *bridge)
119
-{
120
- struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
121
- int ret;
122
-
123
- if (!bridge->encoder) {
124
- dev_err(dvi->dev, "Missing encoder\n");
125
- return -ENODEV;
126
- }
127
-
128
- if (dvi->hpd)
129
- dvi->connector.polled = DRM_CONNECTOR_POLL_HPD;
130
-
131
- drm_connector_helper_add(&dvi->connector,
132
- &tfp410_con_helper_funcs);
133
- ret = drm_connector_init(bridge->dev, &dvi->connector,
134
- &tfp410_con_funcs, DRM_MODE_CONNECTOR_HDMIA);
135
- if (ret) {
136
- dev_err(dvi->dev, "drm_connector_init() failed: %d\n", ret);
137
- return ret;
138
- }
139
-
140
- drm_connector_attach_encoder(&dvi->connector,
141
- bridge->encoder);
142
-
143
- return 0;
144
-}
145
-
146
-static const struct drm_bridge_funcs tfp410_bridge_funcs = {
147
- .attach = tfp410_attach,
148100 };
149101
150102 static void tfp410_hpd_work_func(struct work_struct *work)
....@@ -157,56 +109,189 @@
157109 drm_helper_hpd_irq_event(dvi->bridge.dev);
158110 }
159111
160
-static irqreturn_t tfp410_hpd_irq_thread(int irq, void *arg)
112
+static void tfp410_hpd_callback(void *arg, enum drm_connector_status status)
161113 {
162114 struct tfp410 *dvi = arg;
163115
164116 mod_delayed_work(system_wq, &dvi->hpd_work,
165
- msecs_to_jiffies(HOTPLUG_DEBOUNCE_MS));
166
-
167
- return IRQ_HANDLED;
117
+ msecs_to_jiffies(HOTPLUG_DEBOUNCE_MS));
168118 }
169119
170
-static int tfp410_get_connector_properties(struct tfp410 *dvi)
120
+static int tfp410_attach(struct drm_bridge *bridge,
121
+ enum drm_bridge_attach_flags flags)
171122 {
172
- struct device_node *connector_node, *ddc_phandle;
173
- int ret = 0;
123
+ struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
124
+ int ret;
174125
175
- /* port@1 is the connector node */
176
- connector_node = of_graph_get_remote_node(dvi->dev->of_node, 1, -1);
177
- if (!connector_node)
126
+ ret = drm_bridge_attach(bridge->encoder, dvi->next_bridge, bridge,
127
+ DRM_BRIDGE_ATTACH_NO_CONNECTOR);
128
+ if (ret < 0)
129
+ return ret;
130
+
131
+ if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)
132
+ return 0;
133
+
134
+ if (!bridge->encoder) {
135
+ dev_err(dvi->dev, "Missing encoder\n");
178136 return -ENODEV;
179
-
180
- dvi->hpd = fwnode_get_named_gpiod(&connector_node->fwnode,
181
- "hpd-gpios", 0, GPIOD_IN, "hpd");
182
- if (IS_ERR(dvi->hpd)) {
183
- ret = PTR_ERR(dvi->hpd);
184
- dvi->hpd = NULL;
185
- if (ret == -ENOENT)
186
- ret = 0;
187
- else
188
- goto fail;
189137 }
190138
191
- ddc_phandle = of_parse_phandle(connector_node, "ddc-i2c-bus", 0);
192
- if (!ddc_phandle)
193
- goto fail;
194
-
195
- dvi->ddc = of_get_i2c_adapter_by_node(ddc_phandle);
196
- if (dvi->ddc)
197
- dev_info(dvi->dev, "Connector's ddc i2c bus found\n");
139
+ if (dvi->next_bridge->ops & DRM_BRIDGE_OP_DETECT)
140
+ dvi->connector.polled = DRM_CONNECTOR_POLL_HPD;
198141 else
199
- ret = -EPROBE_DEFER;
142
+ dvi->connector.polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
200143
201
- of_node_put(ddc_phandle);
144
+ if (dvi->next_bridge->ops & DRM_BRIDGE_OP_HPD) {
145
+ INIT_DELAYED_WORK(&dvi->hpd_work, tfp410_hpd_work_func);
146
+ drm_bridge_hpd_enable(dvi->next_bridge, tfp410_hpd_callback,
147
+ dvi);
148
+ }
202149
203
-fail:
204
- of_node_put(connector_node);
205
- return ret;
150
+ drm_connector_helper_add(&dvi->connector,
151
+ &tfp410_con_helper_funcs);
152
+ ret = drm_connector_init_with_ddc(bridge->dev, &dvi->connector,
153
+ &tfp410_con_funcs,
154
+ dvi->next_bridge->type,
155
+ dvi->next_bridge->ddc);
156
+ if (ret) {
157
+ dev_err(dvi->dev, "drm_connector_init_with_ddc() failed: %d\n",
158
+ ret);
159
+ return ret;
160
+ }
161
+
162
+ drm_display_info_set_bus_formats(&dvi->connector.display_info,
163
+ &dvi->bus_format, 1);
164
+
165
+ drm_connector_attach_encoder(&dvi->connector, bridge->encoder);
166
+
167
+ return 0;
206168 }
207169
208
-static int tfp410_init(struct device *dev)
170
+static void tfp410_detach(struct drm_bridge *bridge)
209171 {
172
+ struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
173
+
174
+ if (dvi->connector.dev && dvi->next_bridge->ops & DRM_BRIDGE_OP_HPD) {
175
+ drm_bridge_hpd_disable(dvi->next_bridge);
176
+ cancel_delayed_work_sync(&dvi->hpd_work);
177
+ }
178
+}
179
+
180
+static void tfp410_enable(struct drm_bridge *bridge)
181
+{
182
+ struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
183
+
184
+ gpiod_set_value_cansleep(dvi->powerdown, 0);
185
+}
186
+
187
+static void tfp410_disable(struct drm_bridge *bridge)
188
+{
189
+ struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
190
+
191
+ gpiod_set_value_cansleep(dvi->powerdown, 1);
192
+}
193
+
194
+static enum drm_mode_status tfp410_mode_valid(struct drm_bridge *bridge,
195
+ const struct drm_display_info *info,
196
+ const struct drm_display_mode *mode)
197
+{
198
+ if (mode->clock < 25000)
199
+ return MODE_CLOCK_LOW;
200
+
201
+ if (mode->clock > 165000)
202
+ return MODE_CLOCK_HIGH;
203
+
204
+ return MODE_OK;
205
+}
206
+
207
+static const struct drm_bridge_funcs tfp410_bridge_funcs = {
208
+ .attach = tfp410_attach,
209
+ .detach = tfp410_detach,
210
+ .enable = tfp410_enable,
211
+ .disable = tfp410_disable,
212
+ .mode_valid = tfp410_mode_valid,
213
+};
214
+
215
+static const struct drm_bridge_timings tfp410_default_timings = {
216
+ .input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE
217
+ | DRM_BUS_FLAG_DE_HIGH,
218
+ .setup_time_ps = 1200,
219
+ .hold_time_ps = 1300,
220
+};
221
+
222
+static int tfp410_parse_timings(struct tfp410 *dvi, bool i2c)
223
+{
224
+ struct drm_bridge_timings *timings = &dvi->timings;
225
+ struct device_node *ep;
226
+ u32 pclk_sample = 0;
227
+ u32 bus_width = 24;
228
+ u32 deskew = 0;
229
+
230
+ /* Start with defaults. */
231
+ *timings = tfp410_default_timings;
232
+
233
+ if (i2c)
234
+ /*
235
+ * In I2C mode timings are configured through the I2C interface.
236
+ * As the driver doesn't support I2C configuration yet, we just
237
+ * go with the defaults (BSEL=1, DSEL=1, DKEN=0, EDGE=1).
238
+ */
239
+ return 0;
240
+
241
+ /*
242
+ * In non-I2C mode, timings are configured through the BSEL, DSEL, DKEN
243
+ * and EDGE pins. They are specified in DT through endpoint properties
244
+ * and vendor-specific properties.
245
+ */
246
+ ep = of_graph_get_endpoint_by_regs(dvi->dev->of_node, 0, 0);
247
+ if (!ep)
248
+ return -EINVAL;
249
+
250
+ /* Get the sampling edge from the endpoint. */
251
+ of_property_read_u32(ep, "pclk-sample", &pclk_sample);
252
+ of_property_read_u32(ep, "bus-width", &bus_width);
253
+ of_node_put(ep);
254
+
255
+ timings->input_bus_flags = DRM_BUS_FLAG_DE_HIGH;
256
+
257
+ switch (pclk_sample) {
258
+ case 0:
259
+ timings->input_bus_flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_NEGEDGE
260
+ | DRM_BUS_FLAG_SYNC_SAMPLE_NEGEDGE;
261
+ break;
262
+ case 1:
263
+ timings->input_bus_flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE
264
+ | DRM_BUS_FLAG_SYNC_SAMPLE_POSEDGE;
265
+ break;
266
+ default:
267
+ return -EINVAL;
268
+ }
269
+
270
+ switch (bus_width) {
271
+ case 12:
272
+ dvi->bus_format = MEDIA_BUS_FMT_RGB888_2X12_LE;
273
+ break;
274
+ case 24:
275
+ dvi->bus_format = MEDIA_BUS_FMT_RGB888_1X24;
276
+ break;
277
+ default:
278
+ return -EINVAL;
279
+ }
280
+
281
+ /* Get the setup and hold time from vendor-specific properties. */
282
+ of_property_read_u32(dvi->dev->of_node, "ti,deskew", &deskew);
283
+ if (deskew > 7)
284
+ return -EINVAL;
285
+
286
+ timings->setup_time_ps = 1200 - 350 * ((s32)deskew - 4);
287
+ timings->hold_time_ps = max(0, 1300 + 350 * ((s32)deskew - 4));
288
+
289
+ return 0;
290
+}
291
+
292
+static int tfp410_init(struct device *dev, bool i2c)
293
+{
294
+ struct device_node *node;
210295 struct tfp410 *dvi;
211296 int ret;
212297
....@@ -218,58 +303,56 @@
218303 dvi = devm_kzalloc(dev, sizeof(*dvi), GFP_KERNEL);
219304 if (!dvi)
220305 return -ENOMEM;
306
+
307
+ dvi->dev = dev;
221308 dev_set_drvdata(dev, dvi);
222309
223310 dvi->bridge.funcs = &tfp410_bridge_funcs;
224311 dvi->bridge.of_node = dev->of_node;
225
- dvi->dev = dev;
312
+ dvi->bridge.timings = &dvi->timings;
313
+ dvi->bridge.type = DRM_MODE_CONNECTOR_DVID;
226314
227
- ret = tfp410_get_connector_properties(dvi);
315
+ ret = tfp410_parse_timings(dvi, i2c);
228316 if (ret)
229
- goto fail;
317
+ return ret;
230318
231
- if (dvi->hpd) {
232
- INIT_DELAYED_WORK(&dvi->hpd_work, tfp410_hpd_work_func);
319
+ /* Get the next bridge, connected to port@1. */
320
+ node = of_graph_get_remote_node(dev->of_node, 1, -1);
321
+ if (!node)
322
+ return -ENODEV;
233323
234
- ret = devm_request_threaded_irq(dev, gpiod_to_irq(dvi->hpd),
235
- NULL, tfp410_hpd_irq_thread, IRQF_TRIGGER_RISING |
236
- IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
237
- "hdmi-hpd", dvi);
238
- if (ret) {
239
- DRM_ERROR("failed to register hpd interrupt\n");
240
- goto fail;
241
- }
324
+ dvi->next_bridge = of_drm_find_bridge(node);
325
+ of_node_put(node);
326
+
327
+ if (!dvi->next_bridge)
328
+ return -EPROBE_DEFER;
329
+
330
+ /* Get the powerdown GPIO. */
331
+ dvi->powerdown = devm_gpiod_get_optional(dev, "powerdown",
332
+ GPIOD_OUT_HIGH);
333
+ if (IS_ERR(dvi->powerdown)) {
334
+ dev_err(dev, "failed to parse powerdown gpio\n");
335
+ return PTR_ERR(dvi->powerdown);
242336 }
243337
338
+ /* Register the DRM bridge. */
244339 drm_bridge_add(&dvi->bridge);
245340
246341 return 0;
247
-fail:
248
- i2c_put_adapter(dvi->ddc);
249
- if (dvi->hpd)
250
- gpiod_put(dvi->hpd);
251
- return ret;
252342 }
253343
254344 static int tfp410_fini(struct device *dev)
255345 {
256346 struct tfp410 *dvi = dev_get_drvdata(dev);
257347
258
- cancel_delayed_work_sync(&dvi->hpd_work);
259
-
260348 drm_bridge_remove(&dvi->bridge);
261
-
262
- if (dvi->ddc)
263
- i2c_put_adapter(dvi->ddc);
264
- if (dvi->hpd)
265
- gpiod_put(dvi->hpd);
266349
267350 return 0;
268351 }
269352
270353 static int tfp410_probe(struct platform_device *pdev)
271354 {
272
- return tfp410_init(&pdev->dev);
355
+ return tfp410_init(&pdev->dev, false);
273356 }
274357
275358 static int tfp410_remove(struct platform_device *pdev)
....@@ -306,7 +389,7 @@
306389 return -ENXIO;
307390 }
308391
309
- return tfp410_init(&client->dev);
392
+ return tfp410_init(&client->dev, true);
310393 }
311394
312395 static int tfp410_i2c_remove(struct i2c_client *client)