Merge tag 'tilcdc-4.9-fixes' of https://github.com/jsarha/linux into drm-next
[cascardo/linux.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
27 struct tfp410_module {
28         struct tilcdc_module base;
29         struct i2c_adapter *i2c;
30         int gpio;
31 };
32 #define to_tfp410_module(x) container_of(x, struct tfp410_module, base)
33
34
35 static const struct tilcdc_panel_info dvi_info = {
36                 .ac_bias                = 255,
37                 .ac_bias_intrpt         = 0,
38                 .dma_burst_sz           = 16,
39                 .bpp                    = 16,
40                 .fdd                    = 0x80,
41                 .tft_alt_mode           = 0,
42                 .sync_edge              = 0,
43                 .sync_ctrl              = 1,
44                 .raster_order           = 0,
45 };
46
47 /*
48  * Encoder:
49  */
50
51 struct tfp410_encoder {
52         struct drm_encoder base;
53         struct tfp410_module *mod;
54         int dpms;
55 };
56 #define to_tfp410_encoder(x) container_of(x, struct tfp410_encoder, base)
57
58 static void tfp410_encoder_dpms(struct drm_encoder *encoder, int mode)
59 {
60         struct tfp410_encoder *tfp410_encoder = to_tfp410_encoder(encoder);
61
62         if (tfp410_encoder->dpms == mode)
63                 return;
64
65         if (mode == DRM_MODE_DPMS_ON) {
66                 DBG("Power on");
67                 gpio_direction_output(tfp410_encoder->mod->gpio, 1);
68         } else {
69                 DBG("Power off");
70                 gpio_direction_output(tfp410_encoder->mod->gpio, 0);
71         }
72
73         tfp410_encoder->dpms = mode;
74 }
75
76 static void tfp410_encoder_prepare(struct drm_encoder *encoder)
77 {
78         tfp410_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
79 }
80
81 static void tfp410_encoder_commit(struct drm_encoder *encoder)
82 {
83         tfp410_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
84 }
85
86 static void tfp410_encoder_mode_set(struct drm_encoder *encoder,
87                 struct drm_display_mode *mode,
88                 struct drm_display_mode *adjusted_mode)
89 {
90         /* nothing needed */
91 }
92
93 static const struct drm_encoder_funcs tfp410_encoder_funcs = {
94                 .destroy        = drm_encoder_cleanup,
95 };
96
97 static const struct drm_encoder_helper_funcs tfp410_encoder_helper_funcs = {
98                 .dpms           = tfp410_encoder_dpms,
99                 .prepare        = tfp410_encoder_prepare,
100                 .commit         = tfp410_encoder_commit,
101                 .mode_set       = tfp410_encoder_mode_set,
102 };
103
104 static struct drm_encoder *tfp410_encoder_create(struct drm_device *dev,
105                 struct tfp410_module *mod)
106 {
107         struct tfp410_encoder *tfp410_encoder;
108         struct drm_encoder *encoder;
109         int ret;
110
111         tfp410_encoder = devm_kzalloc(dev->dev, sizeof(*tfp410_encoder),
112                                       GFP_KERNEL);
113         if (!tfp410_encoder) {
114                 dev_err(dev->dev, "allocation failed\n");
115                 return NULL;
116         }
117
118         tfp410_encoder->dpms = DRM_MODE_DPMS_OFF;
119         tfp410_encoder->mod = mod;
120
121         encoder = &tfp410_encoder->base;
122         encoder->possible_crtcs = 1;
123
124         ret = drm_encoder_init(dev, encoder, &tfp410_encoder_funcs,
125                         DRM_MODE_ENCODER_TMDS, NULL);
126         if (ret < 0)
127                 goto fail;
128
129         drm_encoder_helper_add(encoder, &tfp410_encoder_helper_funcs);
130
131         return encoder;
132
133 fail:
134         drm_encoder_cleanup(encoder);
135         return NULL;
136 }
137
138 /*
139  * Connector:
140  */
141
142 struct tfp410_connector {
143         struct drm_connector base;
144
145         struct drm_encoder *encoder;  /* our connected encoder */
146         struct tfp410_module *mod;
147 };
148 #define to_tfp410_connector(x) container_of(x, struct tfp410_connector, base)
149
150
151 static void tfp410_connector_destroy(struct drm_connector *connector)
152 {
153         drm_connector_unregister(connector);
154         drm_connector_cleanup(connector);
155 }
156
157 static enum drm_connector_status tfp410_connector_detect(
158                 struct drm_connector *connector,
159                 bool force)
160 {
161         struct tfp410_connector *tfp410_connector = to_tfp410_connector(connector);
162
163         if (drm_probe_ddc(tfp410_connector->mod->i2c))
164                 return connector_status_connected;
165
166         return connector_status_unknown;
167 }
168
169 static int tfp410_connector_get_modes(struct drm_connector *connector)
170 {
171         struct tfp410_connector *tfp410_connector = to_tfp410_connector(connector);
172         struct edid *edid;
173         int ret = 0;
174
175         edid = drm_get_edid(connector, tfp410_connector->mod->i2c);
176
177         drm_mode_connector_update_edid_property(connector, edid);
178
179         if (edid) {
180                 ret = drm_add_edid_modes(connector, edid);
181                 kfree(edid);
182         }
183
184         return ret;
185 }
186
187 static int tfp410_connector_mode_valid(struct drm_connector *connector,
188                   struct drm_display_mode *mode)
189 {
190         struct tilcdc_drm_private *priv = connector->dev->dev_private;
191         /* our only constraints are what the crtc can generate: */
192         return tilcdc_crtc_mode_valid(priv->crtc, mode);
193 }
194
195 static struct drm_encoder *tfp410_connector_best_encoder(
196                 struct drm_connector *connector)
197 {
198         struct tfp410_connector *tfp410_connector = to_tfp410_connector(connector);
199         return tfp410_connector->encoder;
200 }
201
202 static const struct drm_connector_funcs tfp410_connector_funcs = {
203         .destroy            = tfp410_connector_destroy,
204         .dpms               = drm_atomic_helper_connector_dpms,
205         .detect             = tfp410_connector_detect,
206         .fill_modes         = drm_helper_probe_single_connector_modes,
207         .reset              = drm_atomic_helper_connector_reset,
208         .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
209         .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
210 };
211
212 static const struct drm_connector_helper_funcs tfp410_connector_helper_funcs = {
213         .get_modes          = tfp410_connector_get_modes,
214         .mode_valid         = tfp410_connector_mode_valid,
215         .best_encoder       = tfp410_connector_best_encoder,
216 };
217
218 static struct drm_connector *tfp410_connector_create(struct drm_device *dev,
219                 struct tfp410_module *mod, struct drm_encoder *encoder)
220 {
221         struct tfp410_connector *tfp410_connector;
222         struct drm_connector *connector;
223         int ret;
224
225         tfp410_connector = devm_kzalloc(dev->dev, sizeof(*tfp410_connector),
226                                         GFP_KERNEL);
227         if (!tfp410_connector) {
228                 dev_err(dev->dev, "allocation failed\n");
229                 return NULL;
230         }
231
232         tfp410_connector->encoder = encoder;
233         tfp410_connector->mod = mod;
234
235         connector = &tfp410_connector->base;
236
237         drm_connector_init(dev, connector, &tfp410_connector_funcs,
238                         DRM_MODE_CONNECTOR_DVID);
239         drm_connector_helper_add(connector, &tfp410_connector_helper_funcs);
240
241         connector->polled = DRM_CONNECTOR_POLL_CONNECT |
242                         DRM_CONNECTOR_POLL_DISCONNECT;
243
244         connector->interlace_allowed = 0;
245         connector->doublescan_allowed = 0;
246
247         ret = drm_mode_connector_attach_encoder(connector, encoder);
248         if (ret)
249                 goto fail;
250
251         drm_connector_register(connector);
252
253         return connector;
254
255 fail:
256         tfp410_connector_destroy(connector);
257         return NULL;
258 }
259
260 /*
261  * Module:
262  */
263
264 static int tfp410_modeset_init(struct tilcdc_module *mod, struct drm_device *dev)
265 {
266         struct tfp410_module *tfp410_mod = to_tfp410_module(mod);
267         struct tilcdc_drm_private *priv = dev->dev_private;
268         struct drm_encoder *encoder;
269         struct drm_connector *connector;
270
271         encoder = tfp410_encoder_create(dev, tfp410_mod);
272         if (!encoder)
273                 return -ENOMEM;
274
275         connector = tfp410_connector_create(dev, tfp410_mod, encoder);
276         if (!connector)
277                 return -ENOMEM;
278
279         priv->encoders[priv->num_encoders++] = encoder;
280         priv->connectors[priv->num_connectors++] = connector;
281
282         tilcdc_crtc_set_panel_info(priv->crtc, &dvi_info);
283         return 0;
284 }
285
286 static const struct tilcdc_module_ops tfp410_module_ops = {
287                 .modeset_init = tfp410_modeset_init,
288 };
289
290 /*
291  * Device:
292  */
293
294 static struct of_device_id tfp410_of_match[];
295
296 static int tfp410_probe(struct platform_device *pdev)
297 {
298         struct device_node *node = pdev->dev.of_node;
299         struct device_node *i2c_node;
300         struct tfp410_module *tfp410_mod;
301         struct tilcdc_module *mod;
302         struct pinctrl *pinctrl;
303         uint32_t i2c_phandle;
304         int ret = -EINVAL;
305
306         /* bail out early if no DT data: */
307         if (!node) {
308                 dev_err(&pdev->dev, "device-tree data is missing\n");
309                 return -ENXIO;
310         }
311
312         tfp410_mod = devm_kzalloc(&pdev->dev, sizeof(*tfp410_mod), GFP_KERNEL);
313         if (!tfp410_mod)
314                 return -ENOMEM;
315
316         mod = &tfp410_mod->base;
317         pdev->dev.platform_data = mod;
318
319         tilcdc_module_init(mod, "tfp410", &tfp410_module_ops);
320
321         pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
322         if (IS_ERR(pinctrl))
323                 dev_warn(&pdev->dev, "pins are not configured\n");
324
325         if (of_property_read_u32(node, "i2c", &i2c_phandle)) {
326                 dev_err(&pdev->dev, "could not get i2c bus phandle\n");
327                 goto fail;
328         }
329
330         i2c_node = of_find_node_by_phandle(i2c_phandle);
331         if (!i2c_node) {
332                 dev_err(&pdev->dev, "could not get i2c bus node\n");
333                 goto fail;
334         }
335
336         tfp410_mod->i2c = of_find_i2c_adapter_by_node(i2c_node);
337         if (!tfp410_mod->i2c) {
338                 dev_err(&pdev->dev, "could not get i2c\n");
339                 of_node_put(i2c_node);
340                 goto fail;
341         }
342
343         of_node_put(i2c_node);
344
345         tfp410_mod->gpio = of_get_named_gpio_flags(node, "powerdn-gpio",
346                         0, NULL);
347         if (tfp410_mod->gpio < 0) {
348                 dev_warn(&pdev->dev, "No power down GPIO\n");
349         } else {
350                 ret = gpio_request(tfp410_mod->gpio, "DVI_PDn");
351                 if (ret) {
352                         dev_err(&pdev->dev, "could not get DVI_PDn gpio\n");
353                         goto fail_adapter;
354                 }
355         }
356
357         return 0;
358
359 fail_adapter:
360         i2c_put_adapter(tfp410_mod->i2c);
361
362 fail:
363         tilcdc_module_cleanup(mod);
364         return ret;
365 }
366
367 static int tfp410_remove(struct platform_device *pdev)
368 {
369         struct tilcdc_module *mod = dev_get_platdata(&pdev->dev);
370         struct tfp410_module *tfp410_mod = to_tfp410_module(mod);
371
372         i2c_put_adapter(tfp410_mod->i2c);
373         gpio_free(tfp410_mod->gpio);
374
375         tilcdc_module_cleanup(mod);
376
377         return 0;
378 }
379
380 static struct of_device_id tfp410_of_match[] = {
381                 { .compatible = "ti,tilcdc,tfp410", },
382                 { },
383 };
384
385 struct platform_driver tfp410_driver = {
386         .probe = tfp410_probe,
387         .remove = tfp410_remove,
388         .driver = {
389                 .owner = THIS_MODULE,
390                 .name = "tfp410",
391                 .of_match_table = tfp410_of_match,
392         },
393 };
394
395 int __init tilcdc_tfp410_init(void)
396 {
397         return platform_driver_register(&tfp410_driver);
398 }
399
400 void __exit tilcdc_tfp410_fini(void)
401 {
402         platform_driver_unregister(&tfp410_driver);
403 }