xref: /linux/drivers/gpu/drm/bridge/ti-tfp410.c (revision d61f65159eeef5c10885b83a6d58530c44dca3d5)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2dc55ac3bSJyri Sarha /*
3dc55ac3bSJyri Sarha  * Copyright (C) 2016 Texas Instruments
4dc55ac3bSJyri Sarha  * Author: Jyri Sarha <jsarha@ti.com>
5dc55ac3bSJyri Sarha  */
6dc55ac3bSJyri Sarha 
7f56c9202SChristopher Spinrath #include <linux/gpio/consumer.h>
895b60804SSam Ravnborg #include <linux/i2c.h>
972bd9ea3SVille Syrjälä #include <linux/media-bus-format.h>
10dc55ac3bSJyri Sarha #include <linux/module.h>
11dc55ac3bSJyri Sarha #include <linux/of_graph.h>
12dc55ac3bSJyri Sarha #include <linux/platform_device.h>
135cafa0f1SLaurent Pinchart #include <linux/workqueue.h>
14dc55ac3bSJyri Sarha 
15dc55ac3bSJyri Sarha #include <drm/drm_atomic_helper.h>
16ee68c743SBoris Brezillon #include <drm/drm_bridge.h>
17dc55ac3bSJyri Sarha #include <drm/drm_crtc.h>
18255490f9SVille Syrjälä #include <drm/drm_edid.h>
1995b60804SSam Ravnborg #include <drm/drm_print.h>
20fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h>
21dc55ac3bSJyri Sarha 
22f56c9202SChristopher Spinrath #define HOTPLUG_DEBOUNCE_MS		1100
23f56c9202SChristopher Spinrath 
24dc55ac3bSJyri Sarha struct tfp410 {
25dc55ac3bSJyri Sarha 	struct drm_bridge	bridge;
26dc55ac3bSJyri Sarha 	struct drm_connector	connector;
27dc55ac3bSJyri Sarha 
280eb2766dSPeter Ujfalusi 	u32			bus_format;
29f56c9202SChristopher Spinrath 	struct delayed_work	hpd_work;
3038c02db7SLaurent Pinchart 	struct gpio_desc	*powerdown;
31dc55ac3bSJyri Sarha 
32897dae56SLaurent Pinchart 	struct drm_bridge_timings timings;
335cafa0f1SLaurent Pinchart 	struct drm_bridge	*next_bridge;
34897dae56SLaurent Pinchart 
35dc55ac3bSJyri Sarha 	struct device *dev;
36dc55ac3bSJyri Sarha };
37dc55ac3bSJyri Sarha 
38dc55ac3bSJyri Sarha static inline struct tfp410 *
39dc55ac3bSJyri Sarha drm_bridge_to_tfp410(struct drm_bridge *bridge)
40dc55ac3bSJyri Sarha {
41dc55ac3bSJyri Sarha 	return container_of(bridge, struct tfp410, bridge);
42dc55ac3bSJyri Sarha }
43dc55ac3bSJyri Sarha 
44dc55ac3bSJyri Sarha static inline struct tfp410 *
45dc55ac3bSJyri Sarha drm_connector_to_tfp410(struct drm_connector *connector)
46dc55ac3bSJyri Sarha {
47dc55ac3bSJyri Sarha 	return container_of(connector, struct tfp410, connector);
48dc55ac3bSJyri Sarha }
49dc55ac3bSJyri Sarha 
50dc55ac3bSJyri Sarha static int tfp410_get_modes(struct drm_connector *connector)
51dc55ac3bSJyri Sarha {
52dc55ac3bSJyri Sarha 	struct tfp410 *dvi = drm_connector_to_tfp410(connector);
537b90330fSJani Nikula 	const struct drm_edid *drm_edid;
54dc55ac3bSJyri Sarha 	int ret;
55dc55ac3bSJyri Sarha 
5611d3cf80SLaurent Pinchart 	if (dvi->next_bridge->ops & DRM_BRIDGE_OP_EDID) {
577b90330fSJani Nikula 		drm_edid = drm_bridge_edid_read(dvi->next_bridge, connector);
587b90330fSJani Nikula 		if (!drm_edid)
59dc55ac3bSJyri Sarha 			DRM_INFO("EDID read failed. Fallback to standard modes\n");
6011d3cf80SLaurent Pinchart 	} else {
617b90330fSJani Nikula 		drm_edid = NULL;
6211d3cf80SLaurent Pinchart 	}
635cafa0f1SLaurent Pinchart 
64*d61f6515SJani Nikula 	drm_edid_connector_update(connector, drm_edid);
65*d61f6515SJani Nikula 
667b90330fSJani Nikula 	if (!drm_edid) {
675cafa0f1SLaurent Pinchart 		/*
685cafa0f1SLaurent Pinchart 		 * No EDID, fallback on the XGA standard modes and prefer a mode
695cafa0f1SLaurent Pinchart 		 * pretty much anything can handle.
705cafa0f1SLaurent Pinchart 		 */
715cafa0f1SLaurent Pinchart 		ret = drm_add_modes_noedid(connector, 1920, 1200);
725cafa0f1SLaurent Pinchart 		drm_set_preferred_mode(connector, 1024, 768);
735cafa0f1SLaurent Pinchart 		return ret;
74dc55ac3bSJyri Sarha 	}
75dc55ac3bSJyri Sarha 
767b90330fSJani Nikula 	ret = drm_edid_connector_add_modes(connector);
77c08f99c3STomi Valkeinen 
787b90330fSJani Nikula 	drm_edid_free(drm_edid);
79c08f99c3STomi Valkeinen 
80c08f99c3STomi Valkeinen 	return ret;
81dc55ac3bSJyri Sarha }
82dc55ac3bSJyri Sarha 
83dc55ac3bSJyri Sarha static const struct drm_connector_helper_funcs tfp410_con_helper_funcs = {
84dc55ac3bSJyri Sarha 	.get_modes	= tfp410_get_modes,
85dc55ac3bSJyri Sarha };
86dc55ac3bSJyri Sarha 
87dc55ac3bSJyri Sarha static enum drm_connector_status
88dc55ac3bSJyri Sarha tfp410_connector_detect(struct drm_connector *connector, bool force)
89dc55ac3bSJyri Sarha {
90dc55ac3bSJyri Sarha 	struct tfp410 *dvi = drm_connector_to_tfp410(connector);
91dc55ac3bSJyri Sarha 
925cafa0f1SLaurent Pinchart 	return drm_bridge_detect(dvi->next_bridge);
93dc55ac3bSJyri Sarha }
94dc55ac3bSJyri Sarha 
95dc55ac3bSJyri Sarha static const struct drm_connector_funcs tfp410_con_funcs = {
96dc55ac3bSJyri Sarha 	.detect			= tfp410_connector_detect,
97dc55ac3bSJyri Sarha 	.fill_modes		= drm_helper_probe_single_connector_modes,
98dc55ac3bSJyri Sarha 	.destroy		= drm_connector_cleanup,
99dc55ac3bSJyri Sarha 	.reset			= drm_atomic_helper_connector_reset,
100dc55ac3bSJyri Sarha 	.atomic_duplicate_state	= drm_atomic_helper_connector_duplicate_state,
101dc55ac3bSJyri Sarha 	.atomic_destroy_state	= drm_atomic_helper_connector_destroy_state,
102dc55ac3bSJyri Sarha };
103dc55ac3bSJyri Sarha 
1045cafa0f1SLaurent Pinchart static void tfp410_hpd_work_func(struct work_struct *work)
1055cafa0f1SLaurent Pinchart {
1065cafa0f1SLaurent Pinchart 	struct tfp410 *dvi;
1075cafa0f1SLaurent Pinchart 
1085cafa0f1SLaurent Pinchart 	dvi = container_of(work, struct tfp410, hpd_work.work);
1095cafa0f1SLaurent Pinchart 
1105cafa0f1SLaurent Pinchart 	if (dvi->bridge.dev)
1115cafa0f1SLaurent Pinchart 		drm_helper_hpd_irq_event(dvi->bridge.dev);
1125cafa0f1SLaurent Pinchart }
1135cafa0f1SLaurent Pinchart 
1145cafa0f1SLaurent Pinchart static void tfp410_hpd_callback(void *arg, enum drm_connector_status status)
1155cafa0f1SLaurent Pinchart {
1165cafa0f1SLaurent Pinchart 	struct tfp410 *dvi = arg;
1175cafa0f1SLaurent Pinchart 
1185cafa0f1SLaurent Pinchart 	mod_delayed_work(system_wq, &dvi->hpd_work,
1195cafa0f1SLaurent Pinchart 			 msecs_to_jiffies(HOTPLUG_DEBOUNCE_MS));
1205cafa0f1SLaurent Pinchart }
1215cafa0f1SLaurent Pinchart 
122a25b988fSLaurent Pinchart static int tfp410_attach(struct drm_bridge *bridge,
123a25b988fSLaurent Pinchart 			 enum drm_bridge_attach_flags flags)
124dc55ac3bSJyri Sarha {
125dc55ac3bSJyri Sarha 	struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
126dc55ac3bSJyri Sarha 	int ret;
127dc55ac3bSJyri Sarha 
1285cafa0f1SLaurent Pinchart 	ret = drm_bridge_attach(bridge->encoder, dvi->next_bridge, bridge,
1295cafa0f1SLaurent Pinchart 				DRM_BRIDGE_ATTACH_NO_CONNECTOR);
1305cafa0f1SLaurent Pinchart 	if (ret < 0)
1315cafa0f1SLaurent Pinchart 		return ret;
1325cafa0f1SLaurent Pinchart 
133e00a5caaSLaurent Pinchart 	if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)
134e00a5caaSLaurent Pinchart 		return 0;
135a25b988fSLaurent Pinchart 
136dc55ac3bSJyri Sarha 	if (!bridge->encoder) {
137dc55ac3bSJyri Sarha 		dev_err(dvi->dev, "Missing encoder\n");
138dc55ac3bSJyri Sarha 		return -ENODEV;
139dc55ac3bSJyri Sarha 	}
140dc55ac3bSJyri Sarha 
1415cafa0f1SLaurent Pinchart 	if (dvi->next_bridge->ops & DRM_BRIDGE_OP_DETECT)
142f56c9202SChristopher Spinrath 		dvi->connector.polled = DRM_CONNECTOR_POLL_HPD;
1433d31e215SPeter Ujfalusi 	else
1443d31e215SPeter Ujfalusi 		dvi->connector.polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
145f56c9202SChristopher Spinrath 
1465cafa0f1SLaurent Pinchart 	if (dvi->next_bridge->ops & DRM_BRIDGE_OP_HPD) {
1475cafa0f1SLaurent Pinchart 		INIT_DELAYED_WORK(&dvi->hpd_work, tfp410_hpd_work_func);
1485cafa0f1SLaurent Pinchart 		drm_bridge_hpd_enable(dvi->next_bridge, tfp410_hpd_callback,
1495cafa0f1SLaurent Pinchart 				      dvi);
1505cafa0f1SLaurent Pinchart 	}
1515cafa0f1SLaurent Pinchart 
152dc55ac3bSJyri Sarha 	drm_connector_helper_add(&dvi->connector,
153dc55ac3bSJyri Sarha 				 &tfp410_con_helper_funcs);
154cfb44455SAndrzej Pietrasiewicz 	ret = drm_connector_init_with_ddc(bridge->dev, &dvi->connector,
155cfb44455SAndrzej Pietrasiewicz 					  &tfp410_con_funcs,
1565cafa0f1SLaurent Pinchart 					  dvi->next_bridge->type,
1575cafa0f1SLaurent Pinchart 					  dvi->next_bridge->ddc);
158dc55ac3bSJyri Sarha 	if (ret) {
15948bc281eSGeert Uytterhoeven 		dev_err(dvi->dev, "drm_connector_init_with_ddc() failed: %d\n",
16048bc281eSGeert Uytterhoeven 			ret);
161dc55ac3bSJyri Sarha 		return ret;
162dc55ac3bSJyri Sarha 	}
163dc55ac3bSJyri Sarha 
1640eb2766dSPeter Ujfalusi 	drm_display_info_set_bus_formats(&dvi->connector.display_info,
1650eb2766dSPeter Ujfalusi 					 &dvi->bus_format, 1);
1660eb2766dSPeter Ujfalusi 
1675cafa0f1SLaurent Pinchart 	drm_connector_attach_encoder(&dvi->connector, bridge->encoder);
168dc55ac3bSJyri Sarha 
169dc55ac3bSJyri Sarha 	return 0;
170dc55ac3bSJyri Sarha }
171dc55ac3bSJyri Sarha 
1725cafa0f1SLaurent Pinchart static void tfp410_detach(struct drm_bridge *bridge)
1735cafa0f1SLaurent Pinchart {
1745cafa0f1SLaurent Pinchart 	struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
1755cafa0f1SLaurent Pinchart 
1765cafa0f1SLaurent Pinchart 	if (dvi->connector.dev && dvi->next_bridge->ops & DRM_BRIDGE_OP_HPD) {
1775cafa0f1SLaurent Pinchart 		drm_bridge_hpd_disable(dvi->next_bridge);
1785cafa0f1SLaurent Pinchart 		cancel_delayed_work_sync(&dvi->hpd_work);
1795cafa0f1SLaurent Pinchart 	}
1805cafa0f1SLaurent Pinchart }
1815cafa0f1SLaurent Pinchart 
18238c02db7SLaurent Pinchart static void tfp410_enable(struct drm_bridge *bridge)
18338c02db7SLaurent Pinchart {
18438c02db7SLaurent Pinchart 	struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
18538c02db7SLaurent Pinchart 
18638c02db7SLaurent Pinchart 	gpiod_set_value_cansleep(dvi->powerdown, 0);
18738c02db7SLaurent Pinchart }
18838c02db7SLaurent Pinchart 
18938c02db7SLaurent Pinchart static void tfp410_disable(struct drm_bridge *bridge)
19038c02db7SLaurent Pinchart {
19138c02db7SLaurent Pinchart 	struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
19238c02db7SLaurent Pinchart 
19338c02db7SLaurent Pinchart 	gpiod_set_value_cansleep(dvi->powerdown, 1);
19438c02db7SLaurent Pinchart }
19538c02db7SLaurent Pinchart 
1969109594dSTomi Valkeinen static enum drm_mode_status tfp410_mode_valid(struct drm_bridge *bridge,
19712c683e1SLaurent Pinchart 					      const struct drm_display_info *info,
1989109594dSTomi Valkeinen 					      const struct drm_display_mode *mode)
1999109594dSTomi Valkeinen {
2009109594dSTomi Valkeinen 	if (mode->clock < 25000)
2019109594dSTomi Valkeinen 		return MODE_CLOCK_LOW;
2029109594dSTomi Valkeinen 
2039109594dSTomi Valkeinen 	if (mode->clock > 165000)
2049109594dSTomi Valkeinen 		return MODE_CLOCK_HIGH;
2059109594dSTomi Valkeinen 
2069109594dSTomi Valkeinen 	return MODE_OK;
2079109594dSTomi Valkeinen }
2089109594dSTomi Valkeinen 
209fdf1d8e2SNikhil Devshatwar static u32 *tfp410_get_input_bus_fmts(struct drm_bridge *bridge,
210fdf1d8e2SNikhil Devshatwar 				      struct drm_bridge_state *bridge_state,
211fdf1d8e2SNikhil Devshatwar 				      struct drm_crtc_state *crtc_state,
212fdf1d8e2SNikhil Devshatwar 				      struct drm_connector_state *conn_state,
213fdf1d8e2SNikhil Devshatwar 				      u32 output_fmt,
214fdf1d8e2SNikhil Devshatwar 				      unsigned int *num_input_fmts)
215fdf1d8e2SNikhil Devshatwar {
216fdf1d8e2SNikhil Devshatwar 	struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
217fdf1d8e2SNikhil Devshatwar 	u32 *input_fmts;
218fdf1d8e2SNikhil Devshatwar 
219fdf1d8e2SNikhil Devshatwar 	*num_input_fmts = 0;
220fdf1d8e2SNikhil Devshatwar 
221fdf1d8e2SNikhil Devshatwar 	input_fmts = kzalloc(sizeof(*input_fmts), GFP_KERNEL);
222fdf1d8e2SNikhil Devshatwar 	if (!input_fmts)
223fdf1d8e2SNikhil Devshatwar 		return NULL;
224fdf1d8e2SNikhil Devshatwar 
225fdf1d8e2SNikhil Devshatwar 	*num_input_fmts = 1;
226fdf1d8e2SNikhil Devshatwar 	input_fmts[0] = dvi->bus_format;
227fdf1d8e2SNikhil Devshatwar 
228fdf1d8e2SNikhil Devshatwar 	return input_fmts;
229fdf1d8e2SNikhil Devshatwar }
230fdf1d8e2SNikhil Devshatwar 
231584a3408SNikhil Devshatwar static int tfp410_atomic_check(struct drm_bridge *bridge,
232584a3408SNikhil Devshatwar 			       struct drm_bridge_state *bridge_state,
233584a3408SNikhil Devshatwar 			       struct drm_crtc_state *crtc_state,
234584a3408SNikhil Devshatwar 			       struct drm_connector_state *conn_state)
235584a3408SNikhil Devshatwar {
236584a3408SNikhil Devshatwar 	struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
237584a3408SNikhil Devshatwar 
238584a3408SNikhil Devshatwar 	/*
239584a3408SNikhil Devshatwar 	 * There might be flags negotiation supported in future.
240584a3408SNikhil Devshatwar 	 * Set the bus flags in atomic_check statically for now.
241584a3408SNikhil Devshatwar 	 */
242584a3408SNikhil Devshatwar 	bridge_state->input_bus_cfg.flags = dvi->timings.input_bus_flags;
243584a3408SNikhil Devshatwar 
244584a3408SNikhil Devshatwar 	return 0;
245584a3408SNikhil Devshatwar }
246584a3408SNikhil Devshatwar 
247dc55ac3bSJyri Sarha static const struct drm_bridge_funcs tfp410_bridge_funcs = {
248dc55ac3bSJyri Sarha 	.attach		= tfp410_attach,
2495cafa0f1SLaurent Pinchart 	.detach		= tfp410_detach,
25038c02db7SLaurent Pinchart 	.enable		= tfp410_enable,
25138c02db7SLaurent Pinchart 	.disable	= tfp410_disable,
2529109594dSTomi Valkeinen 	.mode_valid	= tfp410_mode_valid,
253fdf1d8e2SNikhil Devshatwar 	.atomic_reset = drm_atomic_helper_bridge_reset,
254fdf1d8e2SNikhil Devshatwar 	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
255fdf1d8e2SNikhil Devshatwar 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
256fdf1d8e2SNikhil Devshatwar 	.atomic_get_input_bus_fmts = tfp410_get_input_bus_fmts,
257584a3408SNikhil Devshatwar 	.atomic_check = tfp410_atomic_check,
258dc55ac3bSJyri Sarha };
259dc55ac3bSJyri Sarha 
260897dae56SLaurent Pinchart static const struct drm_bridge_timings tfp410_default_timings = {
261897dae56SLaurent Pinchart 	.input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE
262897dae56SLaurent Pinchart 			 | DRM_BUS_FLAG_DE_HIGH,
263897dae56SLaurent Pinchart 	.setup_time_ps = 1200,
264897dae56SLaurent Pinchart 	.hold_time_ps = 1300,
265897dae56SLaurent Pinchart };
266897dae56SLaurent Pinchart 
267897dae56SLaurent Pinchart static int tfp410_parse_timings(struct tfp410 *dvi, bool i2c)
268897dae56SLaurent Pinchart {
269897dae56SLaurent Pinchart 	struct drm_bridge_timings *timings = &dvi->timings;
270897dae56SLaurent Pinchart 	struct device_node *ep;
271897dae56SLaurent Pinchart 	u32 pclk_sample = 0;
2720eb2766dSPeter Ujfalusi 	u32 bus_width = 24;
273db8f92a5SRicardo Cañuelo 	u32 deskew = 0;
274897dae56SLaurent Pinchart 
275897dae56SLaurent Pinchart 	/* Start with defaults. */
276897dae56SLaurent Pinchart 	*timings = tfp410_default_timings;
277897dae56SLaurent Pinchart 
278897dae56SLaurent Pinchart 	if (i2c)
279897dae56SLaurent Pinchart 		/*
280897dae56SLaurent Pinchart 		 * In I2C mode timings are configured through the I2C interface.
281897dae56SLaurent Pinchart 		 * As the driver doesn't support I2C configuration yet, we just
282897dae56SLaurent Pinchart 		 * go with the defaults (BSEL=1, DSEL=1, DKEN=0, EDGE=1).
283897dae56SLaurent Pinchart 		 */
284897dae56SLaurent Pinchart 		return 0;
285897dae56SLaurent Pinchart 
286897dae56SLaurent Pinchart 	/*
287897dae56SLaurent Pinchart 	 * In non-I2C mode, timings are configured through the BSEL, DSEL, DKEN
288897dae56SLaurent Pinchart 	 * and EDGE pins. They are specified in DT through endpoint properties
289897dae56SLaurent Pinchart 	 * and vendor-specific properties.
290897dae56SLaurent Pinchart 	 */
291897dae56SLaurent Pinchart 	ep = of_graph_get_endpoint_by_regs(dvi->dev->of_node, 0, 0);
292897dae56SLaurent Pinchart 	if (!ep)
293897dae56SLaurent Pinchart 		return -EINVAL;
294897dae56SLaurent Pinchart 
295897dae56SLaurent Pinchart 	/* Get the sampling edge from the endpoint. */
296897dae56SLaurent Pinchart 	of_property_read_u32(ep, "pclk-sample", &pclk_sample);
2970eb2766dSPeter Ujfalusi 	of_property_read_u32(ep, "bus-width", &bus_width);
298897dae56SLaurent Pinchart 	of_node_put(ep);
299897dae56SLaurent Pinchart 
300897dae56SLaurent Pinchart 	timings->input_bus_flags = DRM_BUS_FLAG_DE_HIGH;
301897dae56SLaurent Pinchart 
302897dae56SLaurent Pinchart 	switch (pclk_sample) {
303897dae56SLaurent Pinchart 	case 0:
304897dae56SLaurent Pinchart 		timings->input_bus_flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_NEGEDGE
305897dae56SLaurent Pinchart 					 |  DRM_BUS_FLAG_SYNC_SAMPLE_NEGEDGE;
306897dae56SLaurent Pinchart 		break;
307897dae56SLaurent Pinchart 	case 1:
308897dae56SLaurent Pinchart 		timings->input_bus_flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE
309897dae56SLaurent Pinchart 					 |  DRM_BUS_FLAG_SYNC_SAMPLE_POSEDGE;
310897dae56SLaurent Pinchart 		break;
311897dae56SLaurent Pinchart 	default:
312897dae56SLaurent Pinchart 		return -EINVAL;
313897dae56SLaurent Pinchart 	}
314897dae56SLaurent Pinchart 
3150eb2766dSPeter Ujfalusi 	switch (bus_width) {
3160eb2766dSPeter Ujfalusi 	case 12:
3170eb2766dSPeter Ujfalusi 		dvi->bus_format = MEDIA_BUS_FMT_RGB888_2X12_LE;
3180eb2766dSPeter Ujfalusi 		break;
3190eb2766dSPeter Ujfalusi 	case 24:
3200eb2766dSPeter Ujfalusi 		dvi->bus_format = MEDIA_BUS_FMT_RGB888_1X24;
3210eb2766dSPeter Ujfalusi 		break;
3220eb2766dSPeter Ujfalusi 	default:
3230eb2766dSPeter Ujfalusi 		return -EINVAL;
3240eb2766dSPeter Ujfalusi 	}
3250eb2766dSPeter Ujfalusi 
326897dae56SLaurent Pinchart 	/* Get the setup and hold time from vendor-specific properties. */
327db8f92a5SRicardo Cañuelo 	of_property_read_u32(dvi->dev->of_node, "ti,deskew", &deskew);
328db8f92a5SRicardo Cañuelo 	if (deskew > 7)
329897dae56SLaurent Pinchart 		return -EINVAL;
330897dae56SLaurent Pinchart 
331b9fe8620SRicardo Cañuelo 	timings->setup_time_ps = 1200 - 350 * ((s32)deskew - 4);
332b9fe8620SRicardo Cañuelo 	timings->hold_time_ps = max(0, 1300 + 350 * ((s32)deskew - 4));
333897dae56SLaurent Pinchart 
334897dae56SLaurent Pinchart 	return 0;
335897dae56SLaurent Pinchart }
336897dae56SLaurent Pinchart 
337897dae56SLaurent Pinchart static int tfp410_init(struct device *dev, bool i2c)
338dc55ac3bSJyri Sarha {
3395cafa0f1SLaurent Pinchart 	struct device_node *node;
340dc55ac3bSJyri Sarha 	struct tfp410 *dvi;
341dc55ac3bSJyri Sarha 	int ret;
342dc55ac3bSJyri Sarha 
343dc55ac3bSJyri Sarha 	if (!dev->of_node) {
344dc55ac3bSJyri Sarha 		dev_err(dev, "device-tree data is missing\n");
345dc55ac3bSJyri Sarha 		return -ENXIO;
346dc55ac3bSJyri Sarha 	}
347dc55ac3bSJyri Sarha 
348dc55ac3bSJyri Sarha 	dvi = devm_kzalloc(dev, sizeof(*dvi), GFP_KERNEL);
349dc55ac3bSJyri Sarha 	if (!dvi)
350dc55ac3bSJyri Sarha 		return -ENOMEM;
3515cafa0f1SLaurent Pinchart 
3525cafa0f1SLaurent Pinchart 	dvi->dev = dev;
353dc55ac3bSJyri Sarha 	dev_set_drvdata(dev, dvi);
354dc55ac3bSJyri Sarha 
355dc55ac3bSJyri Sarha 	dvi->bridge.funcs = &tfp410_bridge_funcs;
356dc55ac3bSJyri Sarha 	dvi->bridge.of_node = dev->of_node;
357897dae56SLaurent Pinchart 	dvi->bridge.timings = &dvi->timings;
3585cafa0f1SLaurent Pinchart 	dvi->bridge.type = DRM_MODE_CONNECTOR_DVID;
359dc55ac3bSJyri Sarha 
360897dae56SLaurent Pinchart 	ret = tfp410_parse_timings(dvi, i2c);
361897dae56SLaurent Pinchart 	if (ret)
3625cafa0f1SLaurent Pinchart 		return ret;
363897dae56SLaurent Pinchart 
3645cafa0f1SLaurent Pinchart 	/* Get the next bridge, connected to port@1. */
3655cafa0f1SLaurent Pinchart 	node = of_graph_get_remote_node(dev->of_node, 1, -1);
3665cafa0f1SLaurent Pinchart 	if (!node)
3675cafa0f1SLaurent Pinchart 		return -ENODEV;
368dc55ac3bSJyri Sarha 
3695cafa0f1SLaurent Pinchart 	dvi->next_bridge = of_drm_find_bridge(node);
3705cafa0f1SLaurent Pinchart 	of_node_put(node);
3715cafa0f1SLaurent Pinchart 
3725cafa0f1SLaurent Pinchart 	if (!dvi->next_bridge)
3735cafa0f1SLaurent Pinchart 		return -EPROBE_DEFER;
3745cafa0f1SLaurent Pinchart 
3755cafa0f1SLaurent Pinchart 	/* Get the powerdown GPIO. */
37638c02db7SLaurent Pinchart 	dvi->powerdown = devm_gpiod_get_optional(dev, "powerdown",
37738c02db7SLaurent Pinchart 						 GPIOD_OUT_HIGH);
37838c02db7SLaurent Pinchart 	if (IS_ERR(dvi->powerdown)) {
37938c02db7SLaurent Pinchart 		dev_err(dev, "failed to parse powerdown gpio\n");
38038c02db7SLaurent Pinchart 		return PTR_ERR(dvi->powerdown);
38138c02db7SLaurent Pinchart 	}
38238c02db7SLaurent Pinchart 
3835cafa0f1SLaurent Pinchart 	/*  Register the DRM bridge. */
384f74c5275SInki Dae 	drm_bridge_add(&dvi->bridge);
385dc55ac3bSJyri Sarha 
386dc55ac3bSJyri Sarha 	return 0;
387dc55ac3bSJyri Sarha }
388dc55ac3bSJyri Sarha 
3896a47a16dSUwe Kleine-König static void tfp410_fini(struct device *dev)
390dc55ac3bSJyri Sarha {
391dc55ac3bSJyri Sarha 	struct tfp410 *dvi = dev_get_drvdata(dev);
392dc55ac3bSJyri Sarha 
393dc55ac3bSJyri Sarha 	drm_bridge_remove(&dvi->bridge);
394dc55ac3bSJyri Sarha }
395dc55ac3bSJyri Sarha 
396dc55ac3bSJyri Sarha static int tfp410_probe(struct platform_device *pdev)
397dc55ac3bSJyri Sarha {
398897dae56SLaurent Pinchart 	return tfp410_init(&pdev->dev, false);
399dc55ac3bSJyri Sarha }
400dc55ac3bSJyri Sarha 
40167fcd5d2SUwe Kleine-König static void tfp410_remove(struct platform_device *pdev)
402dc55ac3bSJyri Sarha {
4036a47a16dSUwe Kleine-König 	tfp410_fini(&pdev->dev);
404dc55ac3bSJyri Sarha }
405dc55ac3bSJyri Sarha 
406dc55ac3bSJyri Sarha static const struct of_device_id tfp410_match[] = {
407dc55ac3bSJyri Sarha 	{ .compatible = "ti,tfp410" },
408dc55ac3bSJyri Sarha 	{},
409dc55ac3bSJyri Sarha };
410dc55ac3bSJyri Sarha MODULE_DEVICE_TABLE(of, tfp410_match);
411dc55ac3bSJyri Sarha 
4125b47d089SWei Yongjun static struct platform_driver tfp410_platform_driver = {
413dc55ac3bSJyri Sarha 	.probe	= tfp410_probe,
41467fcd5d2SUwe Kleine-König 	.remove_new = tfp410_remove,
415dc55ac3bSJyri Sarha 	.driver	= {
416dc55ac3bSJyri Sarha 		.name		= "tfp410-bridge",
417dc55ac3bSJyri Sarha 		.of_match_table	= tfp410_match,
418dc55ac3bSJyri Sarha 	},
419dc55ac3bSJyri Sarha };
420dc55ac3bSJyri Sarha 
421dc55ac3bSJyri Sarha #if IS_ENABLED(CONFIG_I2C)
422dc55ac3bSJyri Sarha /* There is currently no i2c functionality. */
423612e241fSUwe Kleine-König static int tfp410_i2c_probe(struct i2c_client *client)
424dc55ac3bSJyri Sarha {
425dc55ac3bSJyri Sarha 	int reg;
426dc55ac3bSJyri Sarha 
427dc55ac3bSJyri Sarha 	if (!client->dev.of_node ||
428dc55ac3bSJyri Sarha 	    of_property_read_u32(client->dev.of_node, "reg", &reg)) {
429dc55ac3bSJyri Sarha 		dev_err(&client->dev,
430dc55ac3bSJyri Sarha 			"Can't get i2c reg property from device-tree\n");
431dc55ac3bSJyri Sarha 		return -ENXIO;
432dc55ac3bSJyri Sarha 	}
433dc55ac3bSJyri Sarha 
434897dae56SLaurent Pinchart 	return tfp410_init(&client->dev, true);
435dc55ac3bSJyri Sarha }
436dc55ac3bSJyri Sarha 
437ed5c2f5fSUwe Kleine-König static void tfp410_i2c_remove(struct i2c_client *client)
438dc55ac3bSJyri Sarha {
4396a47a16dSUwe Kleine-König 	tfp410_fini(&client->dev);
440dc55ac3bSJyri Sarha }
441dc55ac3bSJyri Sarha 
442dc55ac3bSJyri Sarha static const struct i2c_device_id tfp410_i2c_ids[] = {
443dc55ac3bSJyri Sarha 	{ "tfp410", 0 },
444dc55ac3bSJyri Sarha 	{ }
445dc55ac3bSJyri Sarha };
446dc55ac3bSJyri Sarha MODULE_DEVICE_TABLE(i2c, tfp410_i2c_ids);
447dc55ac3bSJyri Sarha 
448dc55ac3bSJyri Sarha static struct i2c_driver tfp410_i2c_driver = {
449dc55ac3bSJyri Sarha 	.driver = {
450dc55ac3bSJyri Sarha 		.name	= "tfp410",
451132b6512SZhu Wang 		.of_match_table = tfp410_match,
452dc55ac3bSJyri Sarha 	},
453dc55ac3bSJyri Sarha 	.id_table	= tfp410_i2c_ids,
454332af828SUwe Kleine-König 	.probe		= tfp410_i2c_probe,
455dc55ac3bSJyri Sarha 	.remove		= tfp410_i2c_remove,
456dc55ac3bSJyri Sarha };
457dc55ac3bSJyri Sarha #endif /* IS_ENABLED(CONFIG_I2C) */
458dc55ac3bSJyri Sarha 
459dc55ac3bSJyri Sarha static struct {
460dc55ac3bSJyri Sarha 	uint i2c:1;
461dc55ac3bSJyri Sarha 	uint platform:1;
462dc55ac3bSJyri Sarha }  tfp410_registered_driver;
463dc55ac3bSJyri Sarha 
464dc55ac3bSJyri Sarha static int __init tfp410_module_init(void)
465dc55ac3bSJyri Sarha {
466dc55ac3bSJyri Sarha 	int ret;
467dc55ac3bSJyri Sarha 
468dc55ac3bSJyri Sarha #if IS_ENABLED(CONFIG_I2C)
469dc55ac3bSJyri Sarha 	ret = i2c_add_driver(&tfp410_i2c_driver);
470dc55ac3bSJyri Sarha 	if (ret)
471dc55ac3bSJyri Sarha 		pr_err("%s: registering i2c driver failed: %d",
472dc55ac3bSJyri Sarha 		       __func__, ret);
473dc55ac3bSJyri Sarha 	else
474dc55ac3bSJyri Sarha 		tfp410_registered_driver.i2c = 1;
475dc55ac3bSJyri Sarha #endif
476dc55ac3bSJyri Sarha 
477dc55ac3bSJyri Sarha 	ret = platform_driver_register(&tfp410_platform_driver);
478dc55ac3bSJyri Sarha 	if (ret)
479dc55ac3bSJyri Sarha 		pr_err("%s: registering platform driver failed: %d",
480dc55ac3bSJyri Sarha 		       __func__, ret);
481dc55ac3bSJyri Sarha 	else
482dc55ac3bSJyri Sarha 		tfp410_registered_driver.platform = 1;
483dc55ac3bSJyri Sarha 
484dc55ac3bSJyri Sarha 	if (tfp410_registered_driver.i2c ||
485dc55ac3bSJyri Sarha 	    tfp410_registered_driver.platform)
486dc55ac3bSJyri Sarha 		return 0;
487dc55ac3bSJyri Sarha 
488dc55ac3bSJyri Sarha 	return ret;
489dc55ac3bSJyri Sarha }
490dc55ac3bSJyri Sarha module_init(tfp410_module_init);
491dc55ac3bSJyri Sarha 
492dc55ac3bSJyri Sarha static void __exit tfp410_module_exit(void)
493dc55ac3bSJyri Sarha {
494dc55ac3bSJyri Sarha #if IS_ENABLED(CONFIG_I2C)
495dc55ac3bSJyri Sarha 	if (tfp410_registered_driver.i2c)
496dc55ac3bSJyri Sarha 		i2c_del_driver(&tfp410_i2c_driver);
497dc55ac3bSJyri Sarha #endif
498dc55ac3bSJyri Sarha 	if (tfp410_registered_driver.platform)
499dc55ac3bSJyri Sarha 		platform_driver_unregister(&tfp410_platform_driver);
500dc55ac3bSJyri Sarha }
501dc55ac3bSJyri Sarha module_exit(tfp410_module_exit);
502dc55ac3bSJyri Sarha 
503dc55ac3bSJyri Sarha MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>");
504dc55ac3bSJyri Sarha MODULE_DESCRIPTION("TI TFP410 DVI bridge driver");
505dc55ac3bSJyri Sarha MODULE_LICENSE("GPL");
506