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