xref: /linux/drivers/gpu/drm/bridge/ti-tpd12s015.c (revision 3cdbe59868ef5228b561bb30bde13cc1021ee8a0)
1cff5e6f7SLaurent Pinchart // SPDX-License-Identifier: GPL-2.0
2cff5e6f7SLaurent Pinchart /*
3cff5e6f7SLaurent Pinchart  * TPD12S015 HDMI ESD protection & level shifter chip driver
4cff5e6f7SLaurent Pinchart  *
5cff5e6f7SLaurent Pinchart  * Copyright (C) 2019 Texas Instruments Incorporated
6cff5e6f7SLaurent Pinchart  *
7cff5e6f7SLaurent Pinchart  * Based on the omapdrm-specific encoder-opa362 driver
8cff5e6f7SLaurent Pinchart  *
9cff5e6f7SLaurent Pinchart  * Copyright (C) 2013 Texas Instruments Incorporated
10cff5e6f7SLaurent Pinchart  * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
11cff5e6f7SLaurent Pinchart  */
12cff5e6f7SLaurent Pinchart 
13cff5e6f7SLaurent Pinchart #include <linux/delay.h>
14cff5e6f7SLaurent Pinchart #include <linux/gpio/consumer.h>
15cff5e6f7SLaurent Pinchart #include <linux/interrupt.h>
16cff5e6f7SLaurent Pinchart #include <linux/module.h>
17cff5e6f7SLaurent Pinchart #include <linux/mutex.h>
18cff5e6f7SLaurent Pinchart #include <linux/of.h>
19cff5e6f7SLaurent Pinchart #include <linux/of_graph.h>
20cff5e6f7SLaurent Pinchart #include <linux/platform_device.h>
21cff5e6f7SLaurent Pinchart 
22cff5e6f7SLaurent Pinchart #include <drm/drm_bridge.h>
23cff5e6f7SLaurent Pinchart 
24cff5e6f7SLaurent Pinchart struct tpd12s015_device {
25cff5e6f7SLaurent Pinchart 	struct drm_bridge bridge;
26cff5e6f7SLaurent Pinchart 
27cff5e6f7SLaurent Pinchart 	struct gpio_desc *ct_cp_hpd_gpio;
28cff5e6f7SLaurent Pinchart 	struct gpio_desc *ls_oe_gpio;
29cff5e6f7SLaurent Pinchart 	struct gpio_desc *hpd_gpio;
30cff5e6f7SLaurent Pinchart 	int hpd_irq;
31cff5e6f7SLaurent Pinchart 
32cff5e6f7SLaurent Pinchart 	struct drm_bridge *next_bridge;
33cff5e6f7SLaurent Pinchart };
34cff5e6f7SLaurent Pinchart 
35cff5e6f7SLaurent Pinchart static inline struct tpd12s015_device *to_tpd12s015(struct drm_bridge *bridge)
36cff5e6f7SLaurent Pinchart {
37cff5e6f7SLaurent Pinchart 	return container_of(bridge, struct tpd12s015_device, bridge);
38cff5e6f7SLaurent Pinchart }
39cff5e6f7SLaurent Pinchart 
40cff5e6f7SLaurent Pinchart static int tpd12s015_attach(struct drm_bridge *bridge,
41cff5e6f7SLaurent Pinchart 			    enum drm_bridge_attach_flags flags)
42cff5e6f7SLaurent Pinchart {
43cff5e6f7SLaurent Pinchart 	struct tpd12s015_device *tpd = to_tpd12s015(bridge);
44cff5e6f7SLaurent Pinchart 	int ret;
45cff5e6f7SLaurent Pinchart 
46cff5e6f7SLaurent Pinchart 	if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR))
47cff5e6f7SLaurent Pinchart 		return -EINVAL;
48cff5e6f7SLaurent Pinchart 
49cff5e6f7SLaurent Pinchart 	ret = drm_bridge_attach(bridge->encoder, tpd->next_bridge,
50cff5e6f7SLaurent Pinchart 				bridge, flags);
51cff5e6f7SLaurent Pinchart 	if (ret < 0)
52cff5e6f7SLaurent Pinchart 		return ret;
53cff5e6f7SLaurent Pinchart 
54cff5e6f7SLaurent Pinchart 	gpiod_set_value_cansleep(tpd->ls_oe_gpio, 1);
55cff5e6f7SLaurent Pinchart 
56cff5e6f7SLaurent Pinchart 	/* DC-DC converter needs at max 300us to get to 90% of 5V. */
57cff5e6f7SLaurent Pinchart 	usleep_range(300, 1000);
58cff5e6f7SLaurent Pinchart 
59cff5e6f7SLaurent Pinchart 	return 0;
60cff5e6f7SLaurent Pinchart }
61cff5e6f7SLaurent Pinchart 
62cff5e6f7SLaurent Pinchart static void tpd12s015_detach(struct drm_bridge *bridge)
63cff5e6f7SLaurent Pinchart {
64cff5e6f7SLaurent Pinchart 	struct tpd12s015_device *tpd = to_tpd12s015(bridge);
65cff5e6f7SLaurent Pinchart 
66cff5e6f7SLaurent Pinchart 	gpiod_set_value_cansleep(tpd->ls_oe_gpio, 0);
67cff5e6f7SLaurent Pinchart }
68cff5e6f7SLaurent Pinchart 
69cff5e6f7SLaurent Pinchart static enum drm_connector_status tpd12s015_detect(struct drm_bridge *bridge)
70cff5e6f7SLaurent Pinchart {
71cff5e6f7SLaurent Pinchart 	struct tpd12s015_device *tpd = to_tpd12s015(bridge);
72cff5e6f7SLaurent Pinchart 
73cff5e6f7SLaurent Pinchart 	if (gpiod_get_value_cansleep(tpd->hpd_gpio))
74cff5e6f7SLaurent Pinchart 		return connector_status_connected;
75cff5e6f7SLaurent Pinchart 	else
76cff5e6f7SLaurent Pinchart 		return connector_status_disconnected;
77cff5e6f7SLaurent Pinchart }
78cff5e6f7SLaurent Pinchart 
79cff5e6f7SLaurent Pinchart static void tpd12s015_hpd_enable(struct drm_bridge *bridge)
80cff5e6f7SLaurent Pinchart {
81cff5e6f7SLaurent Pinchart 	struct tpd12s015_device *tpd = to_tpd12s015(bridge);
82cff5e6f7SLaurent Pinchart 
83cff5e6f7SLaurent Pinchart 	gpiod_set_value_cansleep(tpd->ct_cp_hpd_gpio, 1);
84cff5e6f7SLaurent Pinchart }
85cff5e6f7SLaurent Pinchart 
86cff5e6f7SLaurent Pinchart static void tpd12s015_hpd_disable(struct drm_bridge *bridge)
87cff5e6f7SLaurent Pinchart {
88cff5e6f7SLaurent Pinchart 	struct tpd12s015_device *tpd = to_tpd12s015(bridge);
89cff5e6f7SLaurent Pinchart 
90cff5e6f7SLaurent Pinchart 	gpiod_set_value_cansleep(tpd->ct_cp_hpd_gpio, 0);
91cff5e6f7SLaurent Pinchart }
92cff5e6f7SLaurent Pinchart 
93cff5e6f7SLaurent Pinchart static const struct drm_bridge_funcs tpd12s015_bridge_funcs = {
94cff5e6f7SLaurent Pinchart 	.attach			= tpd12s015_attach,
95cff5e6f7SLaurent Pinchart 	.detach			= tpd12s015_detach,
96cff5e6f7SLaurent Pinchart 	.detect			= tpd12s015_detect,
97cff5e6f7SLaurent Pinchart 	.hpd_enable		= tpd12s015_hpd_enable,
98cff5e6f7SLaurent Pinchart 	.hpd_disable		= tpd12s015_hpd_disable,
99cff5e6f7SLaurent Pinchart };
100cff5e6f7SLaurent Pinchart 
101cff5e6f7SLaurent Pinchart static irqreturn_t tpd12s015_hpd_isr(int irq, void *data)
102cff5e6f7SLaurent Pinchart {
103cff5e6f7SLaurent Pinchart 	struct tpd12s015_device *tpd = data;
104cff5e6f7SLaurent Pinchart 	struct drm_bridge *bridge = &tpd->bridge;
105cff5e6f7SLaurent Pinchart 
106cff5e6f7SLaurent Pinchart 	drm_bridge_hpd_notify(bridge, tpd12s015_detect(bridge));
107cff5e6f7SLaurent Pinchart 
108cff5e6f7SLaurent Pinchart 	return IRQ_HANDLED;
109cff5e6f7SLaurent Pinchart }
110cff5e6f7SLaurent Pinchart 
111cff5e6f7SLaurent Pinchart static int tpd12s015_probe(struct platform_device *pdev)
112cff5e6f7SLaurent Pinchart {
113cff5e6f7SLaurent Pinchart 	struct tpd12s015_device *tpd;
114cff5e6f7SLaurent Pinchart 	struct device_node *node;
115cff5e6f7SLaurent Pinchart 	struct gpio_desc *gpio;
116cff5e6f7SLaurent Pinchart 	int ret;
117cff5e6f7SLaurent Pinchart 
118cff5e6f7SLaurent Pinchart 	tpd = devm_kzalloc(&pdev->dev, sizeof(*tpd), GFP_KERNEL);
119cff5e6f7SLaurent Pinchart 	if (!tpd)
120cff5e6f7SLaurent Pinchart 		return -ENOMEM;
121cff5e6f7SLaurent Pinchart 
122cff5e6f7SLaurent Pinchart 	platform_set_drvdata(pdev, tpd);
123cff5e6f7SLaurent Pinchart 
124cff5e6f7SLaurent Pinchart 	tpd->bridge.funcs = &tpd12s015_bridge_funcs;
125cff5e6f7SLaurent Pinchart 	tpd->bridge.of_node = pdev->dev.of_node;
126cff5e6f7SLaurent Pinchart 	tpd->bridge.type = DRM_MODE_CONNECTOR_HDMIA;
127cff5e6f7SLaurent Pinchart 	tpd->bridge.ops = DRM_BRIDGE_OP_DETECT;
128cff5e6f7SLaurent Pinchart 
129cff5e6f7SLaurent Pinchart 	/* Get the next bridge, connected to port@1. */
130cff5e6f7SLaurent Pinchart 	node = of_graph_get_remote_node(pdev->dev.of_node, 1, -1);
131cff5e6f7SLaurent Pinchart 	if (!node)
132cff5e6f7SLaurent Pinchart 		return -ENODEV;
133cff5e6f7SLaurent Pinchart 
134cff5e6f7SLaurent Pinchart 	tpd->next_bridge = of_drm_find_bridge(node);
135cff5e6f7SLaurent Pinchart 	of_node_put(node);
136cff5e6f7SLaurent Pinchart 
137cff5e6f7SLaurent Pinchart 	if (!tpd->next_bridge)
138cff5e6f7SLaurent Pinchart 		return -EPROBE_DEFER;
139cff5e6f7SLaurent Pinchart 
140cff5e6f7SLaurent Pinchart 	/* Get the control and HPD GPIOs. */
141cff5e6f7SLaurent Pinchart 	gpio = devm_gpiod_get_index_optional(&pdev->dev, NULL, 0,
142cff5e6f7SLaurent Pinchart 					     GPIOD_OUT_LOW);
143cff5e6f7SLaurent Pinchart 	if (IS_ERR(gpio))
144cff5e6f7SLaurent Pinchart 		return PTR_ERR(gpio);
145cff5e6f7SLaurent Pinchart 
146cff5e6f7SLaurent Pinchart 	tpd->ct_cp_hpd_gpio = gpio;
147cff5e6f7SLaurent Pinchart 
148cff5e6f7SLaurent Pinchart 	gpio = devm_gpiod_get_index_optional(&pdev->dev, NULL, 1,
149cff5e6f7SLaurent Pinchart 					     GPIOD_OUT_LOW);
150cff5e6f7SLaurent Pinchart 	if (IS_ERR(gpio))
151cff5e6f7SLaurent Pinchart 		return PTR_ERR(gpio);
152cff5e6f7SLaurent Pinchart 
153cff5e6f7SLaurent Pinchart 	tpd->ls_oe_gpio = gpio;
154cff5e6f7SLaurent Pinchart 
155cff5e6f7SLaurent Pinchart 	gpio = devm_gpiod_get_index(&pdev->dev, NULL, 2, GPIOD_IN);
156cff5e6f7SLaurent Pinchart 	if (IS_ERR(gpio))
157cff5e6f7SLaurent Pinchart 		return PTR_ERR(gpio);
158cff5e6f7SLaurent Pinchart 
159cff5e6f7SLaurent Pinchart 	tpd->hpd_gpio = gpio;
160cff5e6f7SLaurent Pinchart 
161cff5e6f7SLaurent Pinchart 	/* Register the IRQ if the HPD GPIO is IRQ-capable. */
162cff5e6f7SLaurent Pinchart 	tpd->hpd_irq = gpiod_to_irq(tpd->hpd_gpio);
163c2530cc9SYueHaibing 	if (tpd->hpd_irq >= 0) {
164cff5e6f7SLaurent Pinchart 		ret = devm_request_threaded_irq(&pdev->dev, tpd->hpd_irq, NULL,
165cff5e6f7SLaurent Pinchart 						tpd12s015_hpd_isr,
166cff5e6f7SLaurent Pinchart 						IRQF_TRIGGER_RISING |
167cff5e6f7SLaurent Pinchart 						IRQF_TRIGGER_FALLING |
168cff5e6f7SLaurent Pinchart 						IRQF_ONESHOT,
169cff5e6f7SLaurent Pinchart 						"tpd12s015 hpd", tpd);
170cff5e6f7SLaurent Pinchart 		if (ret)
171cff5e6f7SLaurent Pinchart 			return ret;
172cff5e6f7SLaurent Pinchart 
173cff5e6f7SLaurent Pinchart 		tpd->bridge.ops |= DRM_BRIDGE_OP_HPD;
174cff5e6f7SLaurent Pinchart 	}
175cff5e6f7SLaurent Pinchart 
176cff5e6f7SLaurent Pinchart 	/* Register the DRM bridge. */
177cff5e6f7SLaurent Pinchart 	drm_bridge_add(&tpd->bridge);
178cff5e6f7SLaurent Pinchart 
179cff5e6f7SLaurent Pinchart 	return 0;
180cff5e6f7SLaurent Pinchart }
181cff5e6f7SLaurent Pinchart 
182*3cdbe598SUwe Kleine-König static void tpd12s015_remove(struct platform_device *pdev)
183cff5e6f7SLaurent Pinchart {
184cff5e6f7SLaurent Pinchart 	struct tpd12s015_device *tpd = platform_get_drvdata(pdev);
185cff5e6f7SLaurent Pinchart 
186cff5e6f7SLaurent Pinchart 	drm_bridge_remove(&tpd->bridge);
187cff5e6f7SLaurent Pinchart }
188cff5e6f7SLaurent Pinchart 
189cff5e6f7SLaurent Pinchart static const struct of_device_id tpd12s015_of_match[] = {
190cff5e6f7SLaurent Pinchart 	{ .compatible = "ti,tpd12s015", },
191cff5e6f7SLaurent Pinchart 	{},
192cff5e6f7SLaurent Pinchart };
193cff5e6f7SLaurent Pinchart 
194cff5e6f7SLaurent Pinchart MODULE_DEVICE_TABLE(of, tpd12s015_of_match);
195cff5e6f7SLaurent Pinchart 
196cff5e6f7SLaurent Pinchart static struct platform_driver tpd12s015_driver = {
197cff5e6f7SLaurent Pinchart 	.probe	= tpd12s015_probe,
198*3cdbe598SUwe Kleine-König 	.remove_new = tpd12s015_remove,
199cff5e6f7SLaurent Pinchart 	.driver	= {
200cff5e6f7SLaurent Pinchart 		.name	= "tpd12s015",
201cff5e6f7SLaurent Pinchart 		.of_match_table = tpd12s015_of_match,
202cff5e6f7SLaurent Pinchart 	},
203cff5e6f7SLaurent Pinchart };
204cff5e6f7SLaurent Pinchart 
205cff5e6f7SLaurent Pinchart module_platform_driver(tpd12s015_driver);
206cff5e6f7SLaurent Pinchart 
207cff5e6f7SLaurent Pinchart MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
208cff5e6f7SLaurent Pinchart MODULE_DESCRIPTION("TPD12S015 HDMI level shifter and ESD protection driver");
209cff5e6f7SLaurent Pinchart MODULE_LICENSE("GPL");
210