GNU Linux-libre 4.9.301-gnu1
[releases.git] / drivers / gpu / drm / omapdrm / displays / encoder-opa362.c
1 /*
2  * OPA362 analog video amplifier with output/power control
3  *
4  * Copyright (C) 2014 Golden Delicious Computers
5  * Author: H. Nikolaus Schaller <hns@goldelico.com>
6  *
7  * based on encoder-tfp410
8  *
9  * Copyright (C) 2013 Texas Instruments
10  * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
11  *
12  * This program is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU General Public License version 2 as published by
14  * the Free Software Foundation.
15  */
16
17 #include <linux/gpio/consumer.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/slab.h>
21
22 #include "../dss/omapdss.h"
23
24 struct panel_drv_data {
25         struct omap_dss_device dssdev;
26         struct omap_dss_device *in;
27
28         struct gpio_desc *enable_gpio;
29
30         struct omap_video_timings timings;
31 };
32
33 #define to_panel_data(x) container_of(x, struct panel_drv_data, dssdev)
34
35 static int opa362_connect(struct omap_dss_device *dssdev,
36                 struct omap_dss_device *dst)
37 {
38         struct panel_drv_data *ddata = to_panel_data(dssdev);
39         struct omap_dss_device *in = ddata->in;
40         int r;
41
42         dev_dbg(dssdev->dev, "connect\n");
43
44         if (omapdss_device_is_connected(dssdev))
45                 return -EBUSY;
46
47         r = in->ops.atv->connect(in, dssdev);
48         if (r)
49                 return r;
50
51         dst->src = dssdev;
52         dssdev->dst = dst;
53
54         return 0;
55 }
56
57 static void opa362_disconnect(struct omap_dss_device *dssdev,
58                 struct omap_dss_device *dst)
59 {
60         struct panel_drv_data *ddata = to_panel_data(dssdev);
61         struct omap_dss_device *in = ddata->in;
62
63         dev_dbg(dssdev->dev, "disconnect\n");
64
65         WARN_ON(!omapdss_device_is_connected(dssdev));
66         if (!omapdss_device_is_connected(dssdev))
67                 return;
68
69         WARN_ON(dst != dssdev->dst);
70         if (dst != dssdev->dst)
71                 return;
72
73         dst->src = NULL;
74         dssdev->dst = NULL;
75
76         in->ops.atv->disconnect(in, &ddata->dssdev);
77 }
78
79 static int opa362_enable(struct omap_dss_device *dssdev)
80 {
81         struct panel_drv_data *ddata = to_panel_data(dssdev);
82         struct omap_dss_device *in = ddata->in;
83         int r;
84
85         dev_dbg(dssdev->dev, "enable\n");
86
87         if (!omapdss_device_is_connected(dssdev))
88                 return -ENODEV;
89
90         if (omapdss_device_is_enabled(dssdev))
91                 return 0;
92
93         in->ops.atv->set_timings(in, &ddata->timings);
94
95         r = in->ops.atv->enable(in);
96         if (r)
97                 return r;
98
99         if (ddata->enable_gpio)
100                 gpiod_set_value_cansleep(ddata->enable_gpio, 1);
101
102         dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
103
104         return 0;
105 }
106
107 static void opa362_disable(struct omap_dss_device *dssdev)
108 {
109         struct panel_drv_data *ddata = to_panel_data(dssdev);
110         struct omap_dss_device *in = ddata->in;
111
112         dev_dbg(dssdev->dev, "disable\n");
113
114         if (!omapdss_device_is_enabled(dssdev))
115                 return;
116
117         if (ddata->enable_gpio)
118                 gpiod_set_value_cansleep(ddata->enable_gpio, 0);
119
120         in->ops.atv->disable(in);
121
122         dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
123 }
124
125 static void opa362_set_timings(struct omap_dss_device *dssdev,
126                 struct omap_video_timings *timings)
127 {
128         struct panel_drv_data *ddata = to_panel_data(dssdev);
129         struct omap_dss_device *in = ddata->in;
130
131         dev_dbg(dssdev->dev, "set_timings\n");
132
133         ddata->timings = *timings;
134         dssdev->panel.timings = *timings;
135
136         in->ops.atv->set_timings(in, timings);
137 }
138
139 static void opa362_get_timings(struct omap_dss_device *dssdev,
140                 struct omap_video_timings *timings)
141 {
142         struct panel_drv_data *ddata = to_panel_data(dssdev);
143
144         dev_dbg(dssdev->dev, "get_timings\n");
145
146         *timings = ddata->timings;
147 }
148
149 static int opa362_check_timings(struct omap_dss_device *dssdev,
150                 struct omap_video_timings *timings)
151 {
152         struct panel_drv_data *ddata = to_panel_data(dssdev);
153         struct omap_dss_device *in = ddata->in;
154
155         dev_dbg(dssdev->dev, "check_timings\n");
156
157         return in->ops.atv->check_timings(in, timings);
158 }
159
160 static void opa362_set_type(struct omap_dss_device *dssdev,
161                 enum omap_dss_venc_type type)
162 {
163         /* we can only drive a COMPOSITE output */
164         WARN_ON(type != OMAP_DSS_VENC_TYPE_COMPOSITE);
165
166 }
167
168 static const struct omapdss_atv_ops opa362_atv_ops = {
169         .connect        = opa362_connect,
170         .disconnect     = opa362_disconnect,
171
172         .enable         = opa362_enable,
173         .disable        = opa362_disable,
174
175         .check_timings  = opa362_check_timings,
176         .set_timings    = opa362_set_timings,
177         .get_timings    = opa362_get_timings,
178
179         .set_type       = opa362_set_type,
180 };
181
182 static int opa362_probe(struct platform_device *pdev)
183 {
184         struct device_node *node = pdev->dev.of_node;
185         struct panel_drv_data *ddata;
186         struct omap_dss_device *dssdev, *in;
187         struct gpio_desc *gpio;
188         int r;
189
190         dev_dbg(&pdev->dev, "probe\n");
191
192         if (node == NULL) {
193                 dev_err(&pdev->dev, "Unable to find device tree\n");
194                 return -EINVAL;
195         }
196
197         ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
198         if (!ddata)
199                 return -ENOMEM;
200
201         platform_set_drvdata(pdev, ddata);
202
203         gpio = devm_gpiod_get_optional(&pdev->dev, "enable", GPIOD_OUT_LOW);
204         if (IS_ERR(gpio))
205                 return PTR_ERR(gpio);
206
207         ddata->enable_gpio = gpio;
208
209         in = omapdss_of_find_source_for_first_ep(node);
210         if (IS_ERR(in)) {
211                 dev_err(&pdev->dev, "failed to find video source\n");
212                 return PTR_ERR(in);
213         }
214
215         ddata->in = in;
216
217         dssdev = &ddata->dssdev;
218         dssdev->ops.atv = &opa362_atv_ops;
219         dssdev->dev = &pdev->dev;
220         dssdev->type = OMAP_DISPLAY_TYPE_VENC;
221         dssdev->output_type = OMAP_DISPLAY_TYPE_VENC;
222         dssdev->owner = THIS_MODULE;
223
224         r = omapdss_register_output(dssdev);
225         if (r) {
226                 dev_err(&pdev->dev, "Failed to register output\n");
227                 goto err_reg;
228         }
229
230         return 0;
231 err_reg:
232         omap_dss_put_device(ddata->in);
233         return r;
234 }
235
236 static int __exit opa362_remove(struct platform_device *pdev)
237 {
238         struct panel_drv_data *ddata = platform_get_drvdata(pdev);
239         struct omap_dss_device *dssdev = &ddata->dssdev;
240         struct omap_dss_device *in = ddata->in;
241
242         omapdss_unregister_output(&ddata->dssdev);
243
244         WARN_ON(omapdss_device_is_enabled(dssdev));
245         if (omapdss_device_is_enabled(dssdev))
246                 opa362_disable(dssdev);
247
248         WARN_ON(omapdss_device_is_connected(dssdev));
249         if (omapdss_device_is_connected(dssdev))
250                 opa362_disconnect(dssdev, dssdev->dst);
251
252         omap_dss_put_device(in);
253
254         return 0;
255 }
256
257 static const struct of_device_id opa362_of_match[] = {
258         { .compatible = "omapdss,ti,opa362", },
259         {},
260 };
261 MODULE_DEVICE_TABLE(of, opa362_of_match);
262
263 static struct platform_driver opa362_driver = {
264         .probe  = opa362_probe,
265         .remove = __exit_p(opa362_remove),
266         .driver = {
267                 .name   = "amplifier-opa362",
268                 .of_match_table = opa362_of_match,
269                 .suppress_bind_attrs = true,
270         },
271 };
272
273 module_platform_driver(opa362_driver);
274
275 MODULE_AUTHOR("H. Nikolaus Schaller <hns@goldelico.com>");
276 MODULE_DESCRIPTION("OPA362 analog video amplifier with output/power control");
277 MODULE_LICENSE("GPL v2");