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