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