Linux Audio

Check our new training course

Loading...
v5.4
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Copyright (C) 2016 Texas Instruments
  4 * Author: Jyri Sarha <jsarha@ti.com>
  5 */
  6
  7#include <linux/delay.h>
  8#include <linux/fwnode.h>
  9#include <linux/gpio/consumer.h>
 10#include <linux/i2c.h>
 11#include <linux/irq.h>
 12#include <linux/module.h>
 13#include <linux/of_graph.h>
 14#include <linux/platform_device.h>
 
 15
 16#include <drm/drm_atomic_helper.h>
 
 17#include <drm/drm_crtc.h>
 
 18#include <drm/drm_print.h>
 19#include <drm/drm_probe_helper.h>
 20
 21#define HOTPLUG_DEBOUNCE_MS		1100
 22
 23struct tfp410 {
 24	struct drm_bridge	bridge;
 25	struct drm_connector	connector;
 26	unsigned int		connector_type;
 27
 28	u32			bus_format;
 29	struct i2c_adapter	*ddc;
 30	struct gpio_desc	*hpd;
 31	int			hpd_irq;
 32	struct delayed_work	hpd_work;
 33	struct gpio_desc	*powerdown;
 34
 35	struct drm_bridge_timings timings;
 
 36
 37	struct device *dev;
 38};
 39
 40static inline struct tfp410 *
 41drm_bridge_to_tfp410(struct drm_bridge *bridge)
 42{
 43	return container_of(bridge, struct tfp410, bridge);
 44}
 45
 46static inline struct tfp410 *
 47drm_connector_to_tfp410(struct drm_connector *connector)
 48{
 49	return container_of(connector, struct tfp410, connector);
 50}
 51
 52static int tfp410_get_modes(struct drm_connector *connector)
 53{
 54	struct tfp410 *dvi = drm_connector_to_tfp410(connector);
 55	struct edid *edid;
 56	int ret;
 57
 58	if (!dvi->ddc)
 59		goto fallback;
 60
 61	edid = drm_get_edid(connector, dvi->ddc);
 62	if (!edid) {
 63		DRM_INFO("EDID read failed. Fallback to standard modes\n");
 64		goto fallback;
 65	}
 66
 67	drm_connector_update_edid_property(connector, edid);
 68
 69	ret = drm_add_edid_modes(connector, edid);
 70
 71	kfree(edid);
 72
 73	return ret;
 
 
 
 
 
 
 74
 75fallback:
 76	/* No EDID, fallback on the XGA standard modes */
 77	ret = drm_add_modes_noedid(connector, 1920, 1200);
 78
 79	/* And prefer a mode pretty much anything can handle */
 80	drm_set_preferred_mode(connector, 1024, 768);
 81
 82	return ret;
 83}
 84
 85static const struct drm_connector_helper_funcs tfp410_con_helper_funcs = {
 86	.get_modes	= tfp410_get_modes,
 87};
 88
 89static enum drm_connector_status
 90tfp410_connector_detect(struct drm_connector *connector, bool force)
 91{
 92	struct tfp410 *dvi = drm_connector_to_tfp410(connector);
 93
 94	if (dvi->hpd) {
 95		if (gpiod_get_value_cansleep(dvi->hpd))
 96			return connector_status_connected;
 97		else
 98			return connector_status_disconnected;
 99	}
100
101	if (dvi->ddc) {
102		if (drm_probe_ddc(dvi->ddc))
103			return connector_status_connected;
104		else
105			return connector_status_disconnected;
106	}
107
108	return connector_status_unknown;
109}
110
111static const struct drm_connector_funcs tfp410_con_funcs = {
112	.detect			= tfp410_connector_detect,
113	.fill_modes		= drm_helper_probe_single_connector_modes,
114	.destroy		= drm_connector_cleanup,
115	.reset			= drm_atomic_helper_connector_reset,
116	.atomic_duplicate_state	= drm_atomic_helper_connector_duplicate_state,
117	.atomic_destroy_state	= drm_atomic_helper_connector_destroy_state,
118};
119
120static int tfp410_attach(struct drm_bridge *bridge)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121{
122	struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
123	int ret;
124
125	if (!bridge->encoder) {
126		dev_err(dvi->dev, "Missing encoder\n");
127		return -ENODEV;
128	}
129
130	if (dvi->hpd_irq >= 0)
 
 
 
131		dvi->connector.polled = DRM_CONNECTOR_POLL_HPD;
132	else
133		dvi->connector.polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
134
 
 
 
 
 
 
135	drm_connector_helper_add(&dvi->connector,
136				 &tfp410_con_helper_funcs);
137	ret = drm_connector_init_with_ddc(bridge->dev, &dvi->connector,
138					  &tfp410_con_funcs,
139					  dvi->connector_type,
140					  dvi->ddc);
141	if (ret) {
142		dev_err(dvi->dev, "drm_connector_init() failed: %d\n", ret);
 
143		return ret;
144	}
145
146	drm_display_info_set_bus_formats(&dvi->connector.display_info,
147					 &dvi->bus_format, 1);
148
149	drm_connector_attach_encoder(&dvi->connector,
150					  bridge->encoder);
151
152	return 0;
153}
154
 
 
 
 
 
 
 
 
 
 
155static void tfp410_enable(struct drm_bridge *bridge)
156{
157	struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
158
159	gpiod_set_value_cansleep(dvi->powerdown, 0);
160}
161
162static void tfp410_disable(struct drm_bridge *bridge)
163{
164	struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
165
166	gpiod_set_value_cansleep(dvi->powerdown, 1);
167}
168
169static const struct drm_bridge_funcs tfp410_bridge_funcs = {
170	.attach		= tfp410_attach,
171	.enable		= tfp410_enable,
172	.disable	= tfp410_disable,
173};
 
174
175static void tfp410_hpd_work_func(struct work_struct *work)
 
 
 
 
 
 
 
 
 
 
 
176{
177	struct tfp410 *dvi;
 
178
179	dvi = container_of(work, struct tfp410, hpd_work.work);
180
181	if (dvi->bridge.dev)
182		drm_helper_hpd_irq_event(dvi->bridge.dev);
 
 
 
 
 
 
183}
184
185static irqreturn_t tfp410_hpd_irq_thread(int irq, void *arg)
 
 
 
186{
187	struct tfp410 *dvi = arg;
188
189	mod_delayed_work(system_wq, &dvi->hpd_work,
190			msecs_to_jiffies(HOTPLUG_DEBOUNCE_MS));
 
 
 
191
192	return IRQ_HANDLED;
193}
194
 
 
 
 
 
 
 
 
 
 
 
 
 
195static const struct drm_bridge_timings tfp410_default_timings = {
196	.input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE
197			 | DRM_BUS_FLAG_DE_HIGH,
198	.setup_time_ps = 1200,
199	.hold_time_ps = 1300,
200};
201
202static int tfp410_parse_timings(struct tfp410 *dvi, bool i2c)
203{
204	struct drm_bridge_timings *timings = &dvi->timings;
205	struct device_node *ep;
206	u32 pclk_sample = 0;
207	u32 bus_width = 24;
208	s32 deskew = 0;
209
210	/* Start with defaults. */
211	*timings = tfp410_default_timings;
212
213	if (i2c)
214		/*
215		 * In I2C mode timings are configured through the I2C interface.
216		 * As the driver doesn't support I2C configuration yet, we just
217		 * go with the defaults (BSEL=1, DSEL=1, DKEN=0, EDGE=1).
218		 */
219		return 0;
220
221	/*
222	 * In non-I2C mode, timings are configured through the BSEL, DSEL, DKEN
223	 * and EDGE pins. They are specified in DT through endpoint properties
224	 * and vendor-specific properties.
225	 */
226	ep = of_graph_get_endpoint_by_regs(dvi->dev->of_node, 0, 0);
227	if (!ep)
228		return -EINVAL;
229
230	/* Get the sampling edge from the endpoint. */
231	of_property_read_u32(ep, "pclk-sample", &pclk_sample);
232	of_property_read_u32(ep, "bus-width", &bus_width);
233	of_node_put(ep);
234
235	timings->input_bus_flags = DRM_BUS_FLAG_DE_HIGH;
236
237	switch (pclk_sample) {
238	case 0:
239		timings->input_bus_flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_NEGEDGE
240					 |  DRM_BUS_FLAG_SYNC_SAMPLE_NEGEDGE;
241		break;
242	case 1:
243		timings->input_bus_flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE
244					 |  DRM_BUS_FLAG_SYNC_SAMPLE_POSEDGE;
245		break;
246	default:
247		return -EINVAL;
248	}
249
250	switch (bus_width) {
251	case 12:
252		dvi->bus_format = MEDIA_BUS_FMT_RGB888_2X12_LE;
253		break;
254	case 24:
255		dvi->bus_format = MEDIA_BUS_FMT_RGB888_1X24;
256		break;
257	default:
258		return -EINVAL;
259	}
260
261	/* Get the setup and hold time from vendor-specific properties. */
262	of_property_read_u32(dvi->dev->of_node, "ti,deskew", (u32 *)&deskew);
263	if (deskew < -4 || deskew > 3)
264		return -EINVAL;
265
266	timings->setup_time_ps = min(0, 1200 - 350 * deskew);
267	timings->hold_time_ps = min(0, 1300 + 350 * deskew);
268
269	return 0;
270}
271
272static int tfp410_get_connector_properties(struct tfp410 *dvi)
273{
274	struct device_node *connector_node, *ddc_phandle;
275	int ret = 0;
276
277	/* port@1 is the connector node */
278	connector_node = of_graph_get_remote_node(dvi->dev->of_node, 1, -1);
279	if (!connector_node)
280		return -ENODEV;
281
282	if (of_device_is_compatible(connector_node, "hdmi-connector"))
283		dvi->connector_type = DRM_MODE_CONNECTOR_HDMIA;
284	else
285		dvi->connector_type = DRM_MODE_CONNECTOR_DVID;
286
287	dvi->hpd = fwnode_get_named_gpiod(&connector_node->fwnode,
288					"hpd-gpios", 0, GPIOD_IN, "hpd");
289	if (IS_ERR(dvi->hpd)) {
290		ret = PTR_ERR(dvi->hpd);
291		dvi->hpd = NULL;
292		if (ret == -ENOENT)
293			ret = 0;
294		else
295			goto fail;
296	}
297
298	ddc_phandle = of_parse_phandle(connector_node, "ddc-i2c-bus", 0);
299	if (!ddc_phandle)
300		goto fail;
301
302	dvi->ddc = of_get_i2c_adapter_by_node(ddc_phandle);
303	if (dvi->ddc)
304		dev_info(dvi->dev, "Connector's ddc i2c bus found\n");
305	else
306		ret = -EPROBE_DEFER;
307
308	of_node_put(ddc_phandle);
309
310fail:
311	of_node_put(connector_node);
312	return ret;
313}
314
315static int tfp410_init(struct device *dev, bool i2c)
316{
 
317	struct tfp410 *dvi;
318	int ret;
319
320	if (!dev->of_node) {
321		dev_err(dev, "device-tree data is missing\n");
322		return -ENXIO;
323	}
324
325	dvi = devm_kzalloc(dev, sizeof(*dvi), GFP_KERNEL);
326	if (!dvi)
327		return -ENOMEM;
 
 
328	dev_set_drvdata(dev, dvi);
329
330	dvi->bridge.funcs = &tfp410_bridge_funcs;
331	dvi->bridge.of_node = dev->of_node;
332	dvi->bridge.timings = &dvi->timings;
333	dvi->dev = dev;
334
335	ret = tfp410_parse_timings(dvi, i2c);
336	if (ret)
337		goto fail;
338
339	ret = tfp410_get_connector_properties(dvi);
340	if (ret)
341		goto fail;
 
 
 
 
342
 
 
 
 
343	dvi->powerdown = devm_gpiod_get_optional(dev, "powerdown",
344						 GPIOD_OUT_HIGH);
345	if (IS_ERR(dvi->powerdown)) {
346		dev_err(dev, "failed to parse powerdown gpio\n");
347		return PTR_ERR(dvi->powerdown);
348	}
349
350	if (dvi->hpd)
351		dvi->hpd_irq = gpiod_to_irq(dvi->hpd);
352	else
353		dvi->hpd_irq = -ENXIO;
354
355	if (dvi->hpd_irq >= 0) {
356		INIT_DELAYED_WORK(&dvi->hpd_work, tfp410_hpd_work_func);
357
358		ret = devm_request_threaded_irq(dev, dvi->hpd_irq,
359			NULL, tfp410_hpd_irq_thread, IRQF_TRIGGER_RISING |
360			IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
361			"hdmi-hpd", dvi);
362		if (ret) {
363			DRM_ERROR("failed to register hpd interrupt\n");
364			goto fail;
365		}
366	}
367
368	drm_bridge_add(&dvi->bridge);
369
370	return 0;
371fail:
372	i2c_put_adapter(dvi->ddc);
373	if (dvi->hpd)
374		gpiod_put(dvi->hpd);
375	return ret;
376}
377
378static int tfp410_fini(struct device *dev)
379{
380	struct tfp410 *dvi = dev_get_drvdata(dev);
381
382	if (dvi->hpd_irq >= 0)
383		cancel_delayed_work_sync(&dvi->hpd_work);
384
385	drm_bridge_remove(&dvi->bridge);
386
387	if (dvi->ddc)
388		i2c_put_adapter(dvi->ddc);
389	if (dvi->hpd)
390		gpiod_put(dvi->hpd);
391
392	return 0;
393}
394
395static int tfp410_probe(struct platform_device *pdev)
396{
397	return tfp410_init(&pdev->dev, false);
398}
399
400static int tfp410_remove(struct platform_device *pdev)
401{
402	return tfp410_fini(&pdev->dev);
403}
404
405static const struct of_device_id tfp410_match[] = {
406	{ .compatible = "ti,tfp410" },
407	{},
408};
409MODULE_DEVICE_TABLE(of, tfp410_match);
410
411static struct platform_driver tfp410_platform_driver = {
412	.probe	= tfp410_probe,
413	.remove	= tfp410_remove,
414	.driver	= {
415		.name		= "tfp410-bridge",
416		.of_match_table	= tfp410_match,
417	},
418};
419
420#if IS_ENABLED(CONFIG_I2C)
421/* There is currently no i2c functionality. */
422static int tfp410_i2c_probe(struct i2c_client *client,
423			    const struct i2c_device_id *id)
424{
425	int reg;
426
427	if (!client->dev.of_node ||
428	    of_property_read_u32(client->dev.of_node, "reg", &reg)) {
429		dev_err(&client->dev,
430			"Can't get i2c reg property from device-tree\n");
431		return -ENXIO;
432	}
433
434	return tfp410_init(&client->dev, true);
435}
436
437static int tfp410_i2c_remove(struct i2c_client *client)
438{
439	return tfp410_fini(&client->dev);
440}
441
442static const struct i2c_device_id tfp410_i2c_ids[] = {
443	{ "tfp410", 0 },
444	{ }
445};
446MODULE_DEVICE_TABLE(i2c, tfp410_i2c_ids);
447
448static struct i2c_driver tfp410_i2c_driver = {
449	.driver = {
450		.name	= "tfp410",
451		.of_match_table = of_match_ptr(tfp410_match),
452	},
453	.id_table	= tfp410_i2c_ids,
454	.probe		= tfp410_i2c_probe,
455	.remove		= tfp410_i2c_remove,
456};
457#endif /* IS_ENABLED(CONFIG_I2C) */
458
459static struct {
460	uint i2c:1;
461	uint platform:1;
462}  tfp410_registered_driver;
463
464static int __init tfp410_module_init(void)
465{
466	int ret;
467
468#if IS_ENABLED(CONFIG_I2C)
469	ret = i2c_add_driver(&tfp410_i2c_driver);
470	if (ret)
471		pr_err("%s: registering i2c driver failed: %d",
472		       __func__, ret);
473	else
474		tfp410_registered_driver.i2c = 1;
475#endif
476
477	ret = platform_driver_register(&tfp410_platform_driver);
478	if (ret)
479		pr_err("%s: registering platform driver failed: %d",
480		       __func__, ret);
481	else
482		tfp410_registered_driver.platform = 1;
483
484	if (tfp410_registered_driver.i2c ||
485	    tfp410_registered_driver.platform)
486		return 0;
487
488	return ret;
489}
490module_init(tfp410_module_init);
491
492static void __exit tfp410_module_exit(void)
493{
494#if IS_ENABLED(CONFIG_I2C)
495	if (tfp410_registered_driver.i2c)
496		i2c_del_driver(&tfp410_i2c_driver);
497#endif
498	if (tfp410_registered_driver.platform)
499		platform_driver_unregister(&tfp410_platform_driver);
500}
501module_exit(tfp410_module_exit);
502
503MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>");
504MODULE_DESCRIPTION("TI TFP410 DVI bridge driver");
505MODULE_LICENSE("GPL");
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Copyright (C) 2016 Texas Instruments
  4 * Author: Jyri Sarha <jsarha@ti.com>
  5 */
  6
 
 
  7#include <linux/gpio/consumer.h>
  8#include <linux/i2c.h>
  9#include <linux/media-bus-format.h>
 10#include <linux/module.h>
 11#include <linux/of_graph.h>
 12#include <linux/platform_device.h>
 13#include <linux/workqueue.h>
 14
 15#include <drm/drm_atomic_helper.h>
 16#include <drm/drm_bridge.h>
 17#include <drm/drm_crtc.h>
 18#include <drm/drm_edid.h>
 19#include <drm/drm_print.h>
 20#include <drm/drm_probe_helper.h>
 21
 22#define HOTPLUG_DEBOUNCE_MS		1100
 23
 24struct tfp410 {
 25	struct drm_bridge	bridge;
 26	struct drm_connector	connector;
 
 27
 28	u32			bus_format;
 
 
 
 29	struct delayed_work	hpd_work;
 30	struct gpio_desc	*powerdown;
 31
 32	struct drm_bridge_timings timings;
 33	struct drm_bridge	*next_bridge;
 34
 35	struct device *dev;
 36};
 37
 38static inline struct tfp410 *
 39drm_bridge_to_tfp410(struct drm_bridge *bridge)
 40{
 41	return container_of(bridge, struct tfp410, bridge);
 42}
 43
 44static inline struct tfp410 *
 45drm_connector_to_tfp410(struct drm_connector *connector)
 46{
 47	return container_of(connector, struct tfp410, connector);
 48}
 49
 50static int tfp410_get_modes(struct drm_connector *connector)
 51{
 52	struct tfp410 *dvi = drm_connector_to_tfp410(connector);
 53	const struct drm_edid *drm_edid;
 54	int ret;
 55
 56	if (dvi->next_bridge->ops & DRM_BRIDGE_OP_EDID) {
 57		drm_edid = drm_bridge_edid_read(dvi->next_bridge, connector);
 58		if (!drm_edid)
 59			DRM_INFO("EDID read failed. Fallback to standard modes\n");
 60	} else {
 61		drm_edid = NULL;
 
 62	}
 63
 64	drm_edid_connector_update(connector, drm_edid);
 
 
 65
 66	if (!drm_edid) {
 67		/*
 68		 * No EDID, fallback on the XGA standard modes and prefer a mode
 69		 * pretty much anything can handle.
 70		 */
 71		ret = drm_add_modes_noedid(connector, 1920, 1200);
 72		drm_set_preferred_mode(connector, 1024, 768);
 73		return ret;
 74	}
 75
 76	ret = drm_edid_connector_add_modes(connector);
 
 
 77
 78	drm_edid_free(drm_edid);
 
 79
 80	return ret;
 81}
 82
 83static const struct drm_connector_helper_funcs tfp410_con_helper_funcs = {
 84	.get_modes	= tfp410_get_modes,
 85};
 86
 87static enum drm_connector_status
 88tfp410_connector_detect(struct drm_connector *connector, bool force)
 89{
 90	struct tfp410 *dvi = drm_connector_to_tfp410(connector);
 91
 92	return drm_bridge_detect(dvi->next_bridge);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 93}
 94
 95static const struct drm_connector_funcs tfp410_con_funcs = {
 96	.detect			= tfp410_connector_detect,
 97	.fill_modes		= drm_helper_probe_single_connector_modes,
 98	.destroy		= drm_connector_cleanup,
 99	.reset			= drm_atomic_helper_connector_reset,
100	.atomic_duplicate_state	= drm_atomic_helper_connector_duplicate_state,
101	.atomic_destroy_state	= drm_atomic_helper_connector_destroy_state,
102};
103
104static void tfp410_hpd_work_func(struct work_struct *work)
105{
106	struct tfp410 *dvi;
107
108	dvi = container_of(work, struct tfp410, hpd_work.work);
109
110	if (dvi->bridge.dev)
111		drm_helper_hpd_irq_event(dvi->bridge.dev);
112}
113
114static void tfp410_hpd_callback(void *arg, enum drm_connector_status status)
115{
116	struct tfp410 *dvi = arg;
117
118	mod_delayed_work(system_wq, &dvi->hpd_work,
119			 msecs_to_jiffies(HOTPLUG_DEBOUNCE_MS));
120}
121
122static int tfp410_attach(struct drm_bridge *bridge,
123			 enum drm_bridge_attach_flags flags)
124{
125	struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
126	int ret;
127
128	ret = drm_bridge_attach(bridge->encoder, dvi->next_bridge, bridge,
129				DRM_BRIDGE_ATTACH_NO_CONNECTOR);
130	if (ret < 0)
131		return ret;
132
133	if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)
134		return 0;
135
136	if (dvi->next_bridge->ops & DRM_BRIDGE_OP_DETECT)
137		dvi->connector.polled = DRM_CONNECTOR_POLL_HPD;
138	else
139		dvi->connector.polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
140
141	if (dvi->next_bridge->ops & DRM_BRIDGE_OP_HPD) {
142		INIT_DELAYED_WORK(&dvi->hpd_work, tfp410_hpd_work_func);
143		drm_bridge_hpd_enable(dvi->next_bridge, tfp410_hpd_callback,
144				      dvi);
145	}
146
147	drm_connector_helper_add(&dvi->connector,
148				 &tfp410_con_helper_funcs);
149	ret = drm_connector_init_with_ddc(bridge->dev, &dvi->connector,
150					  &tfp410_con_funcs,
151					  dvi->next_bridge->type,
152					  dvi->next_bridge->ddc);
153	if (ret) {
154		dev_err(dvi->dev, "drm_connector_init_with_ddc() failed: %d\n",
155			ret);
156		return ret;
157	}
158
159	drm_display_info_set_bus_formats(&dvi->connector.display_info,
160					 &dvi->bus_format, 1);
161
162	drm_connector_attach_encoder(&dvi->connector, bridge->encoder);
 
163
164	return 0;
165}
166
167static void tfp410_detach(struct drm_bridge *bridge)
168{
169	struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
170
171	if (dvi->connector.dev && dvi->next_bridge->ops & DRM_BRIDGE_OP_HPD) {
172		drm_bridge_hpd_disable(dvi->next_bridge);
173		cancel_delayed_work_sync(&dvi->hpd_work);
174	}
175}
176
177static void tfp410_enable(struct drm_bridge *bridge)
178{
179	struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
180
181	gpiod_set_value_cansleep(dvi->powerdown, 0);
182}
183
184static void tfp410_disable(struct drm_bridge *bridge)
185{
186	struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
187
188	gpiod_set_value_cansleep(dvi->powerdown, 1);
189}
190
191static enum drm_mode_status tfp410_mode_valid(struct drm_bridge *bridge,
192					      const struct drm_display_info *info,
193					      const struct drm_display_mode *mode)
194{
195	if (mode->clock < 25000)
196		return MODE_CLOCK_LOW;
197
198	if (mode->clock > 165000)
199		return MODE_CLOCK_HIGH;
200
201	return MODE_OK;
202}
203
204static u32 *tfp410_get_input_bus_fmts(struct drm_bridge *bridge,
205				      struct drm_bridge_state *bridge_state,
206				      struct drm_crtc_state *crtc_state,
207				      struct drm_connector_state *conn_state,
208				      u32 output_fmt,
209				      unsigned int *num_input_fmts)
210{
211	struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
212	u32 *input_fmts;
213
214	*num_input_fmts = 0;
215
216	input_fmts = kzalloc(sizeof(*input_fmts), GFP_KERNEL);
217	if (!input_fmts)
218		return NULL;
219
220	*num_input_fmts = 1;
221	input_fmts[0] = dvi->bus_format;
222
223	return input_fmts;
224}
225
226static int tfp410_atomic_check(struct drm_bridge *bridge,
227			       struct drm_bridge_state *bridge_state,
228			       struct drm_crtc_state *crtc_state,
229			       struct drm_connector_state *conn_state)
230{
231	struct tfp410 *dvi = drm_bridge_to_tfp410(bridge);
232
233	/*
234	 * There might be flags negotiation supported in future.
235	 * Set the bus flags in atomic_check statically for now.
236	 */
237	bridge_state->input_bus_cfg.flags = dvi->timings.input_bus_flags;
238
239	return 0;
240}
241
242static const struct drm_bridge_funcs tfp410_bridge_funcs = {
243	.attach		= tfp410_attach,
244	.detach		= tfp410_detach,
245	.enable		= tfp410_enable,
246	.disable	= tfp410_disable,
247	.mode_valid	= tfp410_mode_valid,
248	.atomic_reset = drm_atomic_helper_bridge_reset,
249	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
250	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
251	.atomic_get_input_bus_fmts = tfp410_get_input_bus_fmts,
252	.atomic_check = tfp410_atomic_check,
253};
254
255static const struct drm_bridge_timings tfp410_default_timings = {
256	.input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE
257			 | DRM_BUS_FLAG_DE_HIGH,
258	.setup_time_ps = 1200,
259	.hold_time_ps = 1300,
260};
261
262static int tfp410_parse_timings(struct tfp410 *dvi, bool i2c)
263{
264	struct drm_bridge_timings *timings = &dvi->timings;
265	struct device_node *ep;
266	u32 pclk_sample = 0;
267	u32 bus_width = 24;
268	u32 deskew = 0;
269
270	/* Start with defaults. */
271	*timings = tfp410_default_timings;
272
273	if (i2c)
274		/*
275		 * In I2C mode timings are configured through the I2C interface.
276		 * As the driver doesn't support I2C configuration yet, we just
277		 * go with the defaults (BSEL=1, DSEL=1, DKEN=0, EDGE=1).
278		 */
279		return 0;
280
281	/*
282	 * In non-I2C mode, timings are configured through the BSEL, DSEL, DKEN
283	 * and EDGE pins. They are specified in DT through endpoint properties
284	 * and vendor-specific properties.
285	 */
286	ep = of_graph_get_endpoint_by_regs(dvi->dev->of_node, 0, 0);
287	if (!ep)
288		return -EINVAL;
289
290	/* Get the sampling edge from the endpoint. */
291	of_property_read_u32(ep, "pclk-sample", &pclk_sample);
292	of_property_read_u32(ep, "bus-width", &bus_width);
293	of_node_put(ep);
294
295	timings->input_bus_flags = DRM_BUS_FLAG_DE_HIGH;
296
297	switch (pclk_sample) {
298	case 0:
299		timings->input_bus_flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_NEGEDGE
300					 |  DRM_BUS_FLAG_SYNC_SAMPLE_NEGEDGE;
301		break;
302	case 1:
303		timings->input_bus_flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE
304					 |  DRM_BUS_FLAG_SYNC_SAMPLE_POSEDGE;
305		break;
306	default:
307		return -EINVAL;
308	}
309
310	switch (bus_width) {
311	case 12:
312		dvi->bus_format = MEDIA_BUS_FMT_RGB888_2X12_LE;
313		break;
314	case 24:
315		dvi->bus_format = MEDIA_BUS_FMT_RGB888_1X24;
316		break;
317	default:
318		return -EINVAL;
319	}
320
321	/* Get the setup and hold time from vendor-specific properties. */
322	of_property_read_u32(dvi->dev->of_node, "ti,deskew", &deskew);
323	if (deskew > 7)
324		return -EINVAL;
325
326	timings->setup_time_ps = 1200 - 350 * ((s32)deskew - 4);
327	timings->hold_time_ps = max(0, 1300 + 350 * ((s32)deskew - 4));
328
329	return 0;
330}
331
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
332static int tfp410_init(struct device *dev, bool i2c)
333{
334	struct device_node *node;
335	struct tfp410 *dvi;
336	int ret;
337
338	if (!dev->of_node) {
339		dev_err(dev, "device-tree data is missing\n");
340		return -ENXIO;
341	}
342
343	dvi = devm_kzalloc(dev, sizeof(*dvi), GFP_KERNEL);
344	if (!dvi)
345		return -ENOMEM;
346
347	dvi->dev = dev;
348	dev_set_drvdata(dev, dvi);
349
350	dvi->bridge.funcs = &tfp410_bridge_funcs;
351	dvi->bridge.of_node = dev->of_node;
352	dvi->bridge.timings = &dvi->timings;
353	dvi->bridge.type = DRM_MODE_CONNECTOR_DVID;
354
355	ret = tfp410_parse_timings(dvi, i2c);
356	if (ret)
357		return ret;
358
359	/* Get the next bridge, connected to port@1. */
360	node = of_graph_get_remote_node(dev->of_node, 1, -1);
361	if (!node)
362		return -ENODEV;
363
364	dvi->next_bridge = of_drm_find_bridge(node);
365	of_node_put(node);
366
367	if (!dvi->next_bridge)
368		return -EPROBE_DEFER;
369
370	/* Get the powerdown GPIO. */
371	dvi->powerdown = devm_gpiod_get_optional(dev, "powerdown",
372						 GPIOD_OUT_HIGH);
373	if (IS_ERR(dvi->powerdown)) {
374		dev_err(dev, "failed to parse powerdown gpio\n");
375		return PTR_ERR(dvi->powerdown);
376	}
377
378	/*  Register the DRM bridge. */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
379	drm_bridge_add(&dvi->bridge);
380
381	return 0;
 
 
 
 
 
382}
383
384static void tfp410_fini(struct device *dev)
385{
386	struct tfp410 *dvi = dev_get_drvdata(dev);
387
 
 
 
388	drm_bridge_remove(&dvi->bridge);
 
 
 
 
 
 
 
389}
390
391static int tfp410_probe(struct platform_device *pdev)
392{
393	return tfp410_init(&pdev->dev, false);
394}
395
396static void tfp410_remove(struct platform_device *pdev)
397{
398	tfp410_fini(&pdev->dev);
399}
400
401static const struct of_device_id tfp410_match[] = {
402	{ .compatible = "ti,tfp410" },
403	{},
404};
405MODULE_DEVICE_TABLE(of, tfp410_match);
406
407static struct platform_driver tfp410_platform_driver = {
408	.probe	= tfp410_probe,
409	.remove = tfp410_remove,
410	.driver	= {
411		.name		= "tfp410-bridge",
412		.of_match_table	= tfp410_match,
413	},
414};
415
416#if IS_ENABLED(CONFIG_I2C)
417/* There is currently no i2c functionality. */
418static int tfp410_i2c_probe(struct i2c_client *client)
 
419{
420	int reg;
421
422	if (!client->dev.of_node ||
423	    of_property_read_u32(client->dev.of_node, "reg", &reg)) {
424		dev_err(&client->dev,
425			"Can't get i2c reg property from device-tree\n");
426		return -ENXIO;
427	}
428
429	return tfp410_init(&client->dev, true);
430}
431
432static void tfp410_i2c_remove(struct i2c_client *client)
433{
434	tfp410_fini(&client->dev);
435}
436
437static const struct i2c_device_id tfp410_i2c_ids[] = {
438	{ "tfp410", 0 },
439	{ }
440};
441MODULE_DEVICE_TABLE(i2c, tfp410_i2c_ids);
442
443static struct i2c_driver tfp410_i2c_driver = {
444	.driver = {
445		.name	= "tfp410",
446		.of_match_table = tfp410_match,
447	},
448	.id_table	= tfp410_i2c_ids,
449	.probe		= tfp410_i2c_probe,
450	.remove		= tfp410_i2c_remove,
451};
452#endif /* IS_ENABLED(CONFIG_I2C) */
453
454static struct {
455	uint i2c:1;
456	uint platform:1;
457}  tfp410_registered_driver;
458
459static int __init tfp410_module_init(void)
460{
461	int ret;
462
463#if IS_ENABLED(CONFIG_I2C)
464	ret = i2c_add_driver(&tfp410_i2c_driver);
465	if (ret)
466		pr_err("%s: registering i2c driver failed: %d",
467		       __func__, ret);
468	else
469		tfp410_registered_driver.i2c = 1;
470#endif
471
472	ret = platform_driver_register(&tfp410_platform_driver);
473	if (ret)
474		pr_err("%s: registering platform driver failed: %d",
475		       __func__, ret);
476	else
477		tfp410_registered_driver.platform = 1;
478
479	if (tfp410_registered_driver.i2c ||
480	    tfp410_registered_driver.platform)
481		return 0;
482
483	return ret;
484}
485module_init(tfp410_module_init);
486
487static void __exit tfp410_module_exit(void)
488{
489#if IS_ENABLED(CONFIG_I2C)
490	if (tfp410_registered_driver.i2c)
491		i2c_del_driver(&tfp410_i2c_driver);
492#endif
493	if (tfp410_registered_driver.platform)
494		platform_driver_unregister(&tfp410_platform_driver);
495}
496module_exit(tfp410_module_exit);
497
498MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>");
499MODULE_DESCRIPTION("TI TFP410 DVI bridge driver");
500MODULE_LICENSE("GPL");