1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * THC63LVD1024 LVDS to parallel data DRM bridge driver.
4  *
5  * Copyright (C) 2018 Jacopo Mondi <jacopo+renesas@jmondi.org>
6  */
7 
8 #include <drm/drmP.h>
9 #include <drm/drm_bridge.h>
10 #include <drm/drm_panel.h>
11 
12 #include <linux/gpio/consumer.h>
13 #include <linux/of_graph.h>
14 #include <linux/regulator/consumer.h>
15 #include <linux/slab.h>
16 
17 enum thc63_ports {
18 	THC63_LVDS_IN0,
19 	THC63_LVDS_IN1,
20 	THC63_RGB_OUT0,
21 	THC63_RGB_OUT1,
22 };
23 
24 struct thc63_dev {
25 	struct device *dev;
26 
27 	struct regulator *vcc;
28 
29 	struct gpio_desc *pdwn;
30 	struct gpio_desc *oe;
31 
32 	struct drm_bridge bridge;
33 	struct drm_bridge *next;
34 
35 	struct drm_bridge_timings timings;
36 };
37 
38 static inline struct thc63_dev *to_thc63(struct drm_bridge *bridge)
39 {
40 	return container_of(bridge, struct thc63_dev, bridge);
41 }
42 
43 static int thc63_attach(struct drm_bridge *bridge)
44 {
45 	struct thc63_dev *thc63 = to_thc63(bridge);
46 
47 	return drm_bridge_attach(bridge->encoder, thc63->next, bridge);
48 }
49 
50 static enum drm_mode_status thc63_mode_valid(struct drm_bridge *bridge,
51 					const struct drm_display_mode *mode)
52 {
53 	struct thc63_dev *thc63 = to_thc63(bridge);
54 	unsigned int min_freq;
55 	unsigned int max_freq;
56 
57 	/*
58 	 * The THC63LVD1024 pixel rate range is 8 to 135 MHz in all modes but
59 	 * dual-in, single-out where it is 40 to 150 MHz. As dual-in, dual-out
60 	 * isn't supported by the driver yet, simply derive the limits from the
61 	 * input mode.
62 	 */
63 	if (thc63->timings.dual_link) {
64 		min_freq = 40000;
65 		max_freq = 150000;
66 	} else {
67 		min_freq = 8000;
68 		max_freq = 135000;
69 	}
70 
71 	if (mode->clock < min_freq)
72 		return MODE_CLOCK_LOW;
73 
74 	if (mode->clock > max_freq)
75 		return MODE_CLOCK_HIGH;
76 
77 	return MODE_OK;
78 }
79 
80 static void thc63_enable(struct drm_bridge *bridge)
81 {
82 	struct thc63_dev *thc63 = to_thc63(bridge);
83 	int ret;
84 
85 	ret = regulator_enable(thc63->vcc);
86 	if (ret) {
87 		dev_err(thc63->dev,
88 			"Failed to enable regulator \"vcc\": %d\n", ret);
89 		return;
90 	}
91 
92 	gpiod_set_value(thc63->pdwn, 0);
93 	gpiod_set_value(thc63->oe, 1);
94 }
95 
96 static void thc63_disable(struct drm_bridge *bridge)
97 {
98 	struct thc63_dev *thc63 = to_thc63(bridge);
99 	int ret;
100 
101 	gpiod_set_value(thc63->oe, 0);
102 	gpiod_set_value(thc63->pdwn, 1);
103 
104 	ret = regulator_disable(thc63->vcc);
105 	if (ret)
106 		dev_err(thc63->dev,
107 			"Failed to disable regulator \"vcc\": %d\n", ret);
108 }
109 
110 static const struct drm_bridge_funcs thc63_bridge_func = {
111 	.attach	= thc63_attach,
112 	.mode_valid = thc63_mode_valid,
113 	.enable = thc63_enable,
114 	.disable = thc63_disable,
115 };
116 
117 static int thc63_parse_dt(struct thc63_dev *thc63)
118 {
119 	struct device_node *endpoint;
120 	struct device_node *remote;
121 
122 	endpoint = of_graph_get_endpoint_by_regs(thc63->dev->of_node,
123 						 THC63_RGB_OUT0, -1);
124 	if (!endpoint) {
125 		dev_err(thc63->dev, "Missing endpoint in port@%u\n",
126 			THC63_RGB_OUT0);
127 		return -ENODEV;
128 	}
129 
130 	remote = of_graph_get_remote_port_parent(endpoint);
131 	of_node_put(endpoint);
132 	if (!remote) {
133 		dev_err(thc63->dev, "Endpoint in port@%u unconnected\n",
134 			THC63_RGB_OUT0);
135 		return -ENODEV;
136 	}
137 
138 	if (!of_device_is_available(remote)) {
139 		dev_err(thc63->dev, "port@%u remote endpoint is disabled\n",
140 			THC63_RGB_OUT0);
141 		of_node_put(remote);
142 		return -ENODEV;
143 	}
144 
145 	thc63->next = of_drm_find_bridge(remote);
146 	of_node_put(remote);
147 	if (!thc63->next)
148 		return -EPROBE_DEFER;
149 
150 	endpoint = of_graph_get_endpoint_by_regs(thc63->dev->of_node,
151 						 THC63_LVDS_IN1, -1);
152 	if (endpoint) {
153 		remote = of_graph_get_remote_port_parent(endpoint);
154 		of_node_put(endpoint);
155 
156 		if (remote) {
157 			if (of_device_is_available(remote))
158 				thc63->timings.dual_link = true;
159 			of_node_put(remote);
160 		}
161 	}
162 
163 	dev_dbg(thc63->dev, "operating in %s-link mode\n",
164 		thc63->timings.dual_link ? "dual" : "single");
165 
166 	return 0;
167 }
168 
169 static int thc63_gpio_init(struct thc63_dev *thc63)
170 {
171 	thc63->oe = devm_gpiod_get_optional(thc63->dev, "oe", GPIOD_OUT_LOW);
172 	if (IS_ERR(thc63->oe)) {
173 		dev_err(thc63->dev, "Unable to get \"oe-gpios\": %ld\n",
174 			PTR_ERR(thc63->oe));
175 		return PTR_ERR(thc63->oe);
176 	}
177 
178 	thc63->pdwn = devm_gpiod_get_optional(thc63->dev, "powerdown",
179 					      GPIOD_OUT_HIGH);
180 	if (IS_ERR(thc63->pdwn)) {
181 		dev_err(thc63->dev, "Unable to get \"powerdown-gpios\": %ld\n",
182 			PTR_ERR(thc63->pdwn));
183 		return PTR_ERR(thc63->pdwn);
184 	}
185 
186 	return 0;
187 }
188 
189 static int thc63_probe(struct platform_device *pdev)
190 {
191 	struct thc63_dev *thc63;
192 	int ret;
193 
194 	thc63 = devm_kzalloc(&pdev->dev, sizeof(*thc63), GFP_KERNEL);
195 	if (!thc63)
196 		return -ENOMEM;
197 
198 	thc63->dev = &pdev->dev;
199 	platform_set_drvdata(pdev, thc63);
200 
201 	thc63->vcc = devm_regulator_get_optional(thc63->dev, "vcc");
202 	if (IS_ERR(thc63->vcc)) {
203 		if (PTR_ERR(thc63->vcc) == -EPROBE_DEFER)
204 			return -EPROBE_DEFER;
205 
206 		dev_err(thc63->dev, "Unable to get \"vcc\" supply: %ld\n",
207 			PTR_ERR(thc63->vcc));
208 		return PTR_ERR(thc63->vcc);
209 	}
210 
211 	ret = thc63_gpio_init(thc63);
212 	if (ret)
213 		return ret;
214 
215 	ret = thc63_parse_dt(thc63);
216 	if (ret)
217 		return ret;
218 
219 	thc63->bridge.driver_private = thc63;
220 	thc63->bridge.of_node = pdev->dev.of_node;
221 	thc63->bridge.funcs = &thc63_bridge_func;
222 	thc63->bridge.timings = &thc63->timings;
223 
224 	drm_bridge_add(&thc63->bridge);
225 
226 	return 0;
227 }
228 
229 static int thc63_remove(struct platform_device *pdev)
230 {
231 	struct thc63_dev *thc63 = platform_get_drvdata(pdev);
232 
233 	drm_bridge_remove(&thc63->bridge);
234 
235 	return 0;
236 }
237 
238 static const struct of_device_id thc63_match[] = {
239 	{ .compatible = "thine,thc63lvd1024", },
240 	{ },
241 };
242 MODULE_DEVICE_TABLE(of, thc63_match);
243 
244 static struct platform_driver thc63_driver = {
245 	.probe	= thc63_probe,
246 	.remove	= thc63_remove,
247 	.driver	= {
248 		.name		= "thc63lvd1024",
249 		.of_match_table	= thc63_match,
250 	},
251 };
252 module_platform_driver(thc63_driver);
253 
254 MODULE_AUTHOR("Jacopo Mondi <jacopo@jmondi.org>");
255 MODULE_DESCRIPTION("Thine THC63LVD1024 LVDS decoder DRM bridge driver");
256 MODULE_LICENSE("GPL v2");
257