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