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