1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * TFP410 DPI-to-DVI encoder driver
4 *
5 * Copyright (C) 2013 Texas Instruments
6 * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
7 */
8
9 #include <linux/err.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/slab.h>
14
15 #include <video/omapfb_dss.h>
16
17 struct panel_drv_data {
18 struct omap_dss_device dssdev;
19 struct omap_dss_device *in;
20
21 struct gpio_desc *pd_gpio;
22
23 int data_lines;
24
25 struct omap_video_timings timings;
26 };
27
28 #define to_panel_data(x) container_of(x, struct panel_drv_data, dssdev)
29
tfp410_connect(struct omap_dss_device * dssdev,struct omap_dss_device * dst)30 static int tfp410_connect(struct omap_dss_device *dssdev,
31 struct omap_dss_device *dst)
32 {
33 struct panel_drv_data *ddata = to_panel_data(dssdev);
34 struct omap_dss_device *in = ddata->in;
35 int r;
36
37 if (omapdss_device_is_connected(dssdev))
38 return -EBUSY;
39
40 r = in->ops.dpi->connect(in, dssdev);
41 if (r)
42 return r;
43
44 dst->src = dssdev;
45 dssdev->dst = dst;
46
47 return 0;
48 }
49
tfp410_disconnect(struct omap_dss_device * dssdev,struct omap_dss_device * dst)50 static void tfp410_disconnect(struct omap_dss_device *dssdev,
51 struct omap_dss_device *dst)
52 {
53 struct panel_drv_data *ddata = to_panel_data(dssdev);
54 struct omap_dss_device *in = ddata->in;
55
56 WARN_ON(!omapdss_device_is_connected(dssdev));
57 if (!omapdss_device_is_connected(dssdev))
58 return;
59
60 WARN_ON(dst != dssdev->dst);
61 if (dst != dssdev->dst)
62 return;
63
64 dst->src = NULL;
65 dssdev->dst = NULL;
66
67 in->ops.dpi->disconnect(in, &ddata->dssdev);
68 }
69
tfp410_enable(struct omap_dss_device * dssdev)70 static int tfp410_enable(struct omap_dss_device *dssdev)
71 {
72 struct panel_drv_data *ddata = to_panel_data(dssdev);
73 struct omap_dss_device *in = ddata->in;
74 int r;
75
76 if (!omapdss_device_is_connected(dssdev))
77 return -ENODEV;
78
79 if (omapdss_device_is_enabled(dssdev))
80 return 0;
81
82 in->ops.dpi->set_timings(in, &ddata->timings);
83 if (ddata->data_lines)
84 in->ops.dpi->set_data_lines(in, ddata->data_lines);
85
86 r = in->ops.dpi->enable(in);
87 if (r)
88 return r;
89
90 if (ddata->pd_gpio)
91 gpiod_set_value_cansleep(ddata->pd_gpio, 0);
92
93 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
94
95 return 0;
96 }
97
tfp410_disable(struct omap_dss_device * dssdev)98 static void tfp410_disable(struct omap_dss_device *dssdev)
99 {
100 struct panel_drv_data *ddata = to_panel_data(dssdev);
101 struct omap_dss_device *in = ddata->in;
102
103 if (!omapdss_device_is_enabled(dssdev))
104 return;
105
106 if (ddata->pd_gpio)
107 gpiod_set_value_cansleep(ddata->pd_gpio, 1);
108
109 in->ops.dpi->disable(in);
110
111 dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
112 }
113
tfp410_fix_timings(struct omap_video_timings * timings)114 static void tfp410_fix_timings(struct omap_video_timings *timings)
115 {
116 timings->data_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE;
117 timings->sync_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE;
118 timings->de_level = OMAPDSS_SIG_ACTIVE_HIGH;
119 }
120
tfp410_set_timings(struct omap_dss_device * dssdev,struct omap_video_timings * timings)121 static void tfp410_set_timings(struct omap_dss_device *dssdev,
122 struct omap_video_timings *timings)
123 {
124 struct panel_drv_data *ddata = to_panel_data(dssdev);
125 struct omap_dss_device *in = ddata->in;
126
127 tfp410_fix_timings(timings);
128
129 ddata->timings = *timings;
130 dssdev->panel.timings = *timings;
131
132 in->ops.dpi->set_timings(in, timings);
133 }
134
tfp410_get_timings(struct omap_dss_device * dssdev,struct omap_video_timings * timings)135 static void tfp410_get_timings(struct omap_dss_device *dssdev,
136 struct omap_video_timings *timings)
137 {
138 struct panel_drv_data *ddata = to_panel_data(dssdev);
139
140 *timings = ddata->timings;
141 }
142
tfp410_check_timings(struct omap_dss_device * dssdev,struct omap_video_timings * timings)143 static int tfp410_check_timings(struct omap_dss_device *dssdev,
144 struct omap_video_timings *timings)
145 {
146 struct panel_drv_data *ddata = to_panel_data(dssdev);
147 struct omap_dss_device *in = ddata->in;
148
149 tfp410_fix_timings(timings);
150
151 return in->ops.dpi->check_timings(in, timings);
152 }
153
154 static const struct omapdss_dvi_ops tfp410_dvi_ops = {
155 .connect = tfp410_connect,
156 .disconnect = tfp410_disconnect,
157
158 .enable = tfp410_enable,
159 .disable = tfp410_disable,
160
161 .check_timings = tfp410_check_timings,
162 .set_timings = tfp410_set_timings,
163 .get_timings = tfp410_get_timings,
164 };
165
tfp410_probe(struct platform_device * pdev)166 static int tfp410_probe(struct platform_device *pdev)
167 {
168 struct panel_drv_data *ddata;
169 struct omap_dss_device *dssdev;
170 int r;
171
172 if (!pdev->dev.of_node)
173 return -ENODEV;
174
175 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
176 if (!ddata)
177 return -ENOMEM;
178
179 platform_set_drvdata(pdev, ddata);
180
181 ddata->pd_gpio = devm_gpiod_get_optional(&pdev->dev, "powerdown",
182 GPIOD_OUT_HIGH);
183 r = PTR_ERR_OR_ZERO(ddata->pd_gpio);
184 if (r) {
185 dev_err(&pdev->dev, "Failed to request PD GPIO: %d\n", r);
186 return r;
187 }
188
189 gpiod_set_consumer_name(ddata->pd_gpio, "tfp410 PD");
190
191 ddata->in = omapdss_of_find_source_for_first_ep(pdev->dev.of_node);
192 r = PTR_ERR_OR_ZERO(ddata->in);
193 if (r) {
194 dev_err(&pdev->dev, "failed to find video source: %d\n", r);
195 return r;
196 }
197
198 dssdev = &ddata->dssdev;
199 dssdev->ops.dvi = &tfp410_dvi_ops;
200 dssdev->dev = &pdev->dev;
201 dssdev->type = OMAP_DISPLAY_TYPE_DPI;
202 dssdev->output_type = OMAP_DISPLAY_TYPE_DVI;
203 dssdev->owner = THIS_MODULE;
204 dssdev->phy.dpi.data_lines = ddata->data_lines;
205 dssdev->port_num = 1;
206
207 r = omapdss_register_output(dssdev);
208 if (r) {
209 dev_err(&pdev->dev, "Failed to register output\n");
210 goto err_reg;
211 }
212
213 return 0;
214 err_reg:
215 omap_dss_put_device(ddata->in);
216 return r;
217 }
218
tfp410_remove(struct platform_device * pdev)219 static void tfp410_remove(struct platform_device *pdev)
220 {
221 struct panel_drv_data *ddata = platform_get_drvdata(pdev);
222 struct omap_dss_device *dssdev = &ddata->dssdev;
223 struct omap_dss_device *in = ddata->in;
224
225 omapdss_unregister_output(&ddata->dssdev);
226
227 WARN_ON(omapdss_device_is_enabled(dssdev));
228 if (omapdss_device_is_enabled(dssdev))
229 tfp410_disable(dssdev);
230
231 WARN_ON(omapdss_device_is_connected(dssdev));
232 if (omapdss_device_is_connected(dssdev))
233 tfp410_disconnect(dssdev, dssdev->dst);
234
235 omap_dss_put_device(in);
236 }
237
238 static const struct of_device_id tfp410_of_match[] = {
239 { .compatible = "omapdss,ti,tfp410", },
240 {},
241 };
242
243 MODULE_DEVICE_TABLE(of, tfp410_of_match);
244
245 static struct platform_driver tfp410_driver = {
246 .probe = tfp410_probe,
247 .remove = tfp410_remove,
248 .driver = {
249 .name = "tfp410",
250 .of_match_table = tfp410_of_match,
251 },
252 };
253
254 module_platform_driver(tfp410_driver);
255
256 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
257 MODULE_DESCRIPTION("TFP410 DPI to DVI encoder driver");
258 MODULE_LICENSE("GPL");
259