GNU Linux-libre 4.19.263-gnu1
[releases.git] / drivers / gpu / drm / tilcdc / tilcdc_tfp410.c
1 /*
2  * Copyright (C) 2012 Texas Instruments
3  * Author: Rob Clark <robdclark@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <linux/i2c.h>
19 #include <linux/gpio.h>
20 #include <linux/of_gpio.h>
21 #include <linux/pinctrl/pinmux.h>
22 #include <linux/pinctrl/consumer.h>
23 #include <drm/drm_atomic_helper.h>
24
25 #include "tilcdc_drv.h"
26 #include "tilcdc_tfp410.h"
27
28 struct tfp410_module {
29         struct tilcdc_module base;
30         struct i2c_adapter *i2c;
31         int gpio;
32 };
33 #define to_tfp410_module(x) container_of(x, struct tfp410_module, base)
34
35
36 static const struct tilcdc_panel_info dvi_info = {
37                 .ac_bias                = 255,
38                 .ac_bias_intrpt         = 0,
39                 .dma_burst_sz           = 16,
40                 .bpp                    = 16,
41                 .fdd                    = 0x80,
42                 .tft_alt_mode           = 0,
43                 .sync_edge              = 0,
44                 .sync_ctrl              = 1,
45                 .raster_order           = 0,
46 };
47
48 /*
49  * Encoder:
50  */
51
52 struct tfp410_encoder {
53         struct drm_encoder base;
54         struct tfp410_module *mod;
55         int dpms;
56 };
57 #define to_tfp410_encoder(x) container_of(x, struct tfp410_encoder, base)
58
59 static void tfp410_encoder_dpms(struct drm_encoder *encoder, int mode)
60 {
61         struct tfp410_encoder *tfp410_encoder = to_tfp410_encoder(encoder);
62
63         if (tfp410_encoder->dpms == mode)
64                 return;
65
66         if (mode == DRM_MODE_DPMS_ON) {
67                 DBG("Power on");
68                 gpio_direction_output(tfp410_encoder->mod->gpio, 1);
69         } else {
70                 DBG("Power off");
71                 gpio_direction_output(tfp410_encoder->mod->gpio, 0);
72         }
73
74         tfp410_encoder->dpms = mode;
75 }
76
77 static void tfp410_encoder_prepare(struct drm_encoder *encoder)
78 {
79         tfp410_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
80 }
81
82 static void tfp410_encoder_commit(struct drm_encoder *encoder)
83 {
84         tfp410_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
85 }
86
87 static void tfp410_encoder_mode_set(struct drm_encoder *encoder,
88                 struct drm_display_mode *mode,
89                 struct drm_display_mode *adjusted_mode)
90 {
91         /* nothing needed */
92 }
93
94 static const struct drm_encoder_funcs tfp410_encoder_funcs = {
95                 .destroy        = drm_encoder_cleanup,
96 };
97
98 static const struct drm_encoder_helper_funcs tfp410_encoder_helper_funcs = {
99                 .dpms           = tfp410_encoder_dpms,
100                 .prepare        = tfp410_encoder_prepare,
101                 .commit         = tfp410_encoder_commit,
102                 .mode_set       = tfp410_encoder_mode_set,
103 };
104
105 static struct drm_encoder *tfp410_encoder_create(struct drm_device *dev,
106                 struct tfp410_module *mod)
107 {
108         struct tfp410_encoder *tfp410_encoder;
109         struct drm_encoder *encoder;
110         int ret;
111
112         tfp410_encoder = devm_kzalloc(dev->dev, sizeof(*tfp410_encoder),
113                                       GFP_KERNEL);
114         if (!tfp410_encoder)
115                 return NULL;
116
117         tfp410_encoder->dpms = DRM_MODE_DPMS_OFF;
118         tfp410_encoder->mod = mod;
119
120         encoder = &tfp410_encoder->base;
121         encoder->possible_crtcs = 1;
122
123         ret = drm_encoder_init(dev, encoder, &tfp410_encoder_funcs,
124                         DRM_MODE_ENCODER_TMDS, NULL);
125         if (ret < 0)
126                 goto fail;
127
128         drm_encoder_helper_add(encoder, &tfp410_encoder_helper_funcs);
129
130         return encoder;
131
132 fail:
133         drm_encoder_cleanup(encoder);
134         return NULL;
135 }
136
137 /*
138  * Connector:
139  */
140
141 struct tfp410_connector {
142         struct drm_connector base;
143
144         struct drm_encoder *encoder;  /* our connected encoder */
145         struct tfp410_module *mod;
146 };
147 #define to_tfp410_connector(x) container_of(x, struct tfp410_connector, base)
148
149
150 static void tfp410_connector_destroy(struct drm_connector *connector)
151 {
152         drm_connector_unregister(connector);
153         drm_connector_cleanup(connector);
154 }
155
156 static enum drm_connector_status tfp410_connector_detect(
157                 struct drm_connector *connector,
158                 bool force)
159 {
160         struct tfp410_connector *tfp410_connector = to_tfp410_connector(connector);
161
162         if (drm_probe_ddc(tfp410_connector->mod->i2c))
163                 return connector_status_connected;
164
165         return connector_status_unknown;
166 }
167
168 static int tfp410_connector_get_modes(struct drm_connector *connector)
169 {
170         struct tfp410_connector *tfp410_connector = to_tfp410_connector(connector);
171         struct edid *edid;
172         int ret = 0;
173
174         edid = drm_get_edid(connector, tfp410_connector->mod->i2c);
175
176         drm_connector_update_edid_property(connector, edid);
177
178         if (edid) {
179                 ret = drm_add_edid_modes(connector, edid);
180                 kfree(edid);
181         }
182
183         return ret;
184 }
185
186 static struct drm_encoder *tfp410_connector_best_encoder(
187                 struct drm_connector *connector)
188 {
189         struct tfp410_connector *tfp410_connector = to_tfp410_connector(connector);
190         return tfp410_connector->encoder;
191 }
192
193 static const struct drm_connector_funcs tfp410_connector_funcs = {
194         .destroy            = tfp410_connector_destroy,
195         .detect             = tfp410_connector_detect,
196         .fill_modes         = drm_helper_probe_single_connector_modes,
197         .reset              = drm_atomic_helper_connector_reset,
198         .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
199         .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
200 };
201
202 static const struct drm_connector_helper_funcs tfp410_connector_helper_funcs = {
203         .get_modes          = tfp410_connector_get_modes,
204         .best_encoder       = tfp410_connector_best_encoder,
205 };
206
207 static struct drm_connector *tfp410_connector_create(struct drm_device *dev,
208                 struct tfp410_module *mod, struct drm_encoder *encoder)
209 {
210         struct tfp410_connector *tfp410_connector;
211         struct drm_connector *connector;
212         int ret;
213
214         tfp410_connector = devm_kzalloc(dev->dev, sizeof(*tfp410_connector),
215                                         GFP_KERNEL);
216         if (!tfp410_connector)
217                 return NULL;
218
219         tfp410_connector->encoder = encoder;
220         tfp410_connector->mod = mod;
221
222         connector = &tfp410_connector->base;
223
224         drm_connector_init(dev, connector, &tfp410_connector_funcs,
225                         DRM_MODE_CONNECTOR_DVID);
226         drm_connector_helper_add(connector, &tfp410_connector_helper_funcs);
227
228         connector->polled = DRM_CONNECTOR_POLL_CONNECT |
229                         DRM_CONNECTOR_POLL_DISCONNECT;
230
231         connector->interlace_allowed = 0;
232         connector->doublescan_allowed = 0;
233
234         ret = drm_connector_attach_encoder(connector, encoder);
235         if (ret)
236                 goto fail;
237
238         return connector;
239
240 fail:
241         tfp410_connector_destroy(connector);
242         return NULL;
243 }
244
245 /*
246  * Module:
247  */
248
249 static int tfp410_modeset_init(struct tilcdc_module *mod, struct drm_device *dev)
250 {
251         struct tfp410_module *tfp410_mod = to_tfp410_module(mod);
252         struct tilcdc_drm_private *priv = dev->dev_private;
253         struct drm_encoder *encoder;
254         struct drm_connector *connector;
255
256         encoder = tfp410_encoder_create(dev, tfp410_mod);
257         if (!encoder)
258                 return -ENOMEM;
259
260         connector = tfp410_connector_create(dev, tfp410_mod, encoder);
261         if (!connector)
262                 return -ENOMEM;
263
264         priv->encoders[priv->num_encoders++] = encoder;
265         priv->connectors[priv->num_connectors++] = connector;
266
267         tilcdc_crtc_set_panel_info(priv->crtc, &dvi_info);
268         return 0;
269 }
270
271 static const struct tilcdc_module_ops tfp410_module_ops = {
272                 .modeset_init = tfp410_modeset_init,
273 };
274
275 /*
276  * Device:
277  */
278
279 static int tfp410_probe(struct platform_device *pdev)
280 {
281         struct device_node *node = pdev->dev.of_node;
282         struct device_node *i2c_node;
283         struct tfp410_module *tfp410_mod;
284         struct tilcdc_module *mod;
285         struct pinctrl *pinctrl;
286         uint32_t i2c_phandle;
287         int ret = -EINVAL;
288
289         /* bail out early if no DT data: */
290         if (!node) {
291                 dev_err(&pdev->dev, "device-tree data is missing\n");
292                 return -ENXIO;
293         }
294
295         tfp410_mod = devm_kzalloc(&pdev->dev, sizeof(*tfp410_mod), GFP_KERNEL);
296         if (!tfp410_mod)
297                 return -ENOMEM;
298
299         mod = &tfp410_mod->base;
300         pdev->dev.platform_data = mod;
301
302         tilcdc_module_init(mod, "tfp410", &tfp410_module_ops);
303
304         pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
305         if (IS_ERR(pinctrl))
306                 dev_warn(&pdev->dev, "pins are not configured\n");
307
308         if (of_property_read_u32(node, "i2c", &i2c_phandle)) {
309                 dev_err(&pdev->dev, "could not get i2c bus phandle\n");
310                 goto fail;
311         }
312
313         i2c_node = of_find_node_by_phandle(i2c_phandle);
314         if (!i2c_node) {
315                 dev_err(&pdev->dev, "could not get i2c bus node\n");
316                 goto fail;
317         }
318
319         tfp410_mod->i2c = of_find_i2c_adapter_by_node(i2c_node);
320         if (!tfp410_mod->i2c) {
321                 dev_err(&pdev->dev, "could not get i2c\n");
322                 of_node_put(i2c_node);
323                 goto fail;
324         }
325
326         of_node_put(i2c_node);
327
328         tfp410_mod->gpio = of_get_named_gpio_flags(node, "powerdn-gpio",
329                         0, NULL);
330         if (tfp410_mod->gpio < 0) {
331                 dev_warn(&pdev->dev, "No power down GPIO\n");
332         } else {
333                 ret = gpio_request(tfp410_mod->gpio, "DVI_PDn");
334                 if (ret) {
335                         dev_err(&pdev->dev, "could not get DVI_PDn gpio\n");
336                         goto fail_adapter;
337                 }
338         }
339
340         return 0;
341
342 fail_adapter:
343         i2c_put_adapter(tfp410_mod->i2c);
344
345 fail:
346         tilcdc_module_cleanup(mod);
347         return ret;
348 }
349
350 static int tfp410_remove(struct platform_device *pdev)
351 {
352         struct tilcdc_module *mod = dev_get_platdata(&pdev->dev);
353         struct tfp410_module *tfp410_mod = to_tfp410_module(mod);
354
355         i2c_put_adapter(tfp410_mod->i2c);
356         gpio_free(tfp410_mod->gpio);
357
358         tilcdc_module_cleanup(mod);
359
360         return 0;
361 }
362
363 static const struct of_device_id tfp410_of_match[] = {
364                 { .compatible = "ti,tilcdc,tfp410", },
365                 { },
366 };
367
368 struct platform_driver tfp410_driver = {
369         .probe = tfp410_probe,
370         .remove = tfp410_remove,
371         .driver = {
372                 .owner = THIS_MODULE,
373                 .name = "tfp410",
374                 .of_match_table = tfp410_of_match,
375         },
376 };
377
378 int __init tilcdc_tfp410_init(void)
379 {
380         return platform_driver_register(&tfp410_driver);
381 }
382
383 void __exit tilcdc_tfp410_fini(void)
384 {
385         platform_driver_unregister(&tfp410_driver);
386 }