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