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