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