Merge tag 'nds32-for-linus-4.18' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-microblaze.git] / drivers / gpu / drm / bridge / thc63lvd1024.c
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
36 static inline struct thc63_dev *to_thc63(struct drm_bridge *bridge)
37 {
38         return container_of(bridge, struct thc63_dev, bridge);
39 }
40
41 static int thc63_attach(struct drm_bridge *bridge)
42 {
43         struct thc63_dev *thc63 = to_thc63(bridge);
44
45         return drm_bridge_attach(bridge->encoder, thc63->next, bridge);
46 }
47
48 static void thc63_enable(struct drm_bridge *bridge)
49 {
50         struct thc63_dev *thc63 = to_thc63(bridge);
51         int ret;
52
53         ret = regulator_enable(thc63->vcc);
54         if (ret) {
55                 dev_err(thc63->dev,
56                         "Failed to enable regulator \"vcc\": %d\n", ret);
57                 return;
58         }
59
60         gpiod_set_value(thc63->pdwn, 0);
61         gpiod_set_value(thc63->oe, 1);
62 }
63
64 static void thc63_disable(struct drm_bridge *bridge)
65 {
66         struct thc63_dev *thc63 = to_thc63(bridge);
67         int ret;
68
69         gpiod_set_value(thc63->oe, 0);
70         gpiod_set_value(thc63->pdwn, 1);
71
72         ret = regulator_disable(thc63->vcc);
73         if (ret)
74                 dev_err(thc63->dev,
75                         "Failed to disable regulator \"vcc\": %d\n", ret);
76 }
77
78 static const struct drm_bridge_funcs thc63_bridge_func = {
79         .attach = thc63_attach,
80         .enable = thc63_enable,
81         .disable = thc63_disable,
82 };
83
84 static int thc63_parse_dt(struct thc63_dev *thc63)
85 {
86         struct device_node *thc63_out;
87         struct device_node *remote;
88
89         thc63_out = of_graph_get_endpoint_by_regs(thc63->dev->of_node,
90                                                   THC63_RGB_OUT0, -1);
91         if (!thc63_out) {
92                 dev_err(thc63->dev, "Missing endpoint in port@%u\n",
93                         THC63_RGB_OUT0);
94                 return -ENODEV;
95         }
96
97         remote = of_graph_get_remote_port_parent(thc63_out);
98         of_node_put(thc63_out);
99         if (!remote) {
100                 dev_err(thc63->dev, "Endpoint in port@%u unconnected\n",
101                         THC63_RGB_OUT0);
102                 return -ENODEV;
103         }
104
105         if (!of_device_is_available(remote)) {
106                 dev_err(thc63->dev, "port@%u remote endpoint is disabled\n",
107                         THC63_RGB_OUT0);
108                 of_node_put(remote);
109                 return -ENODEV;
110         }
111
112         thc63->next = of_drm_find_bridge(remote);
113         of_node_put(remote);
114         if (!thc63->next)
115                 return -EPROBE_DEFER;
116
117         return 0;
118 }
119
120 static int thc63_gpio_init(struct thc63_dev *thc63)
121 {
122         thc63->oe = devm_gpiod_get_optional(thc63->dev, "oe", GPIOD_OUT_LOW);
123         if (IS_ERR(thc63->oe)) {
124                 dev_err(thc63->dev, "Unable to get \"oe-gpios\": %ld\n",
125                         PTR_ERR(thc63->oe));
126                 return PTR_ERR(thc63->oe);
127         }
128
129         thc63->pdwn = devm_gpiod_get_optional(thc63->dev, "powerdown",
130                                               GPIOD_OUT_HIGH);
131         if (IS_ERR(thc63->pdwn)) {
132                 dev_err(thc63->dev, "Unable to get \"powerdown-gpios\": %ld\n",
133                         PTR_ERR(thc63->pdwn));
134                 return PTR_ERR(thc63->pdwn);
135         }
136
137         return 0;
138 }
139
140 static int thc63_probe(struct platform_device *pdev)
141 {
142         struct thc63_dev *thc63;
143         int ret;
144
145         thc63 = devm_kzalloc(&pdev->dev, sizeof(*thc63), GFP_KERNEL);
146         if (!thc63)
147                 return -ENOMEM;
148
149         thc63->dev = &pdev->dev;
150         platform_set_drvdata(pdev, thc63);
151
152         thc63->vcc = devm_regulator_get_optional(thc63->dev, "vcc");
153         if (IS_ERR(thc63->vcc)) {
154                 if (PTR_ERR(thc63->vcc) == -EPROBE_DEFER)
155                         return -EPROBE_DEFER;
156
157                 dev_err(thc63->dev, "Unable to get \"vcc\" supply: %ld\n",
158                         PTR_ERR(thc63->vcc));
159                 return PTR_ERR(thc63->vcc);
160         }
161
162         ret = thc63_gpio_init(thc63);
163         if (ret)
164                 return ret;
165
166         ret = thc63_parse_dt(thc63);
167         if (ret)
168                 return ret;
169
170         thc63->bridge.driver_private = thc63;
171         thc63->bridge.of_node = pdev->dev.of_node;
172         thc63->bridge.funcs = &thc63_bridge_func;
173
174         drm_bridge_add(&thc63->bridge);
175
176         return 0;
177 }
178
179 static int thc63_remove(struct platform_device *pdev)
180 {
181         struct thc63_dev *thc63 = platform_get_drvdata(pdev);
182
183         drm_bridge_remove(&thc63->bridge);
184
185         return 0;
186 }
187
188 static const struct of_device_id thc63_match[] = {
189         { .compatible = "thine,thc63lvd1024", },
190         { },
191 };
192 MODULE_DEVICE_TABLE(of, thc63_match);
193
194 static struct platform_driver thc63_driver = {
195         .probe  = thc63_probe,
196         .remove = thc63_remove,
197         .driver = {
198                 .name           = "thc63lvd1024",
199                 .of_match_table = thc63_match,
200         },
201 };
202 module_platform_driver(thc63_driver);
203
204 MODULE_AUTHOR("Jacopo Mondi <jacopo@jmondi.org>");
205 MODULE_DESCRIPTION("Thine THC63LVD1024 LVDS decoder DRM bridge driver");
206 MODULE_LICENSE("GPL v2");