blob: 458043a539959ec08a867aaf98ea4274f78b6693 [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>
Jyri Sarha018cfbd2016-04-13 19:00:51 +030023#include <drm/drm_atomic_helper.h>
Rob Clark16ea9752013-01-08 15:04:28 -060024
25#include "tilcdc_drv.h"
Baoyou Xie0fd86772016-09-08 19:08:56 +080026#include "tilcdc_tfp410.h"
Rob Clark16ea9752013-01-08 15:04:28 -060027
28struct 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
36static 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
52struct 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
Rob Clark16ea9752013-01-08 15:04:28 -060059static 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
Rob Clark16ea9752013-01-08 15:04:28 -060077static void tfp410_encoder_prepare(struct drm_encoder *encoder)
78{
79 tfp410_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
Rob Clark16ea9752013-01-08 15:04:28 -060080}
81
82static void tfp410_encoder_commit(struct drm_encoder *encoder)
83{
84 tfp410_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
85}
86
87static 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
94static const struct drm_encoder_funcs tfp410_encoder_funcs = {
Jyri Sarhad0ec32c2016-02-23 12:44:27 +020095 .destroy = drm_encoder_cleanup,
Rob Clark16ea9752013-01-08 15:04:28 -060096};
97
98static const struct drm_encoder_helper_funcs tfp410_encoder_helper_funcs = {
99 .dpms = tfp410_encoder_dpms,
Rob Clark16ea9752013-01-08 15:04:28 -0600100 .prepare = tfp410_encoder_prepare,
101 .commit = tfp410_encoder_commit,
102 .mode_set = tfp410_encoder_mode_set,
103};
104
105static 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
Jyri Sarhad0ec32c2016-02-23 12:44:27 +0200112 tfp410_encoder = devm_kzalloc(dev->dev, sizeof(*tfp410_encoder),
113 GFP_KERNEL);
Rob Clark16ea9752013-01-08 15:04:28 -0600114 if (!tfp410_encoder) {
115 dev_err(dev->dev, "allocation failed\n");
116 return NULL;
117 }
118
119 tfp410_encoder->dpms = DRM_MODE_DPMS_OFF;
120 tfp410_encoder->mod = mod;
121
122 encoder = &tfp410_encoder->base;
123 encoder->possible_crtcs = 1;
124
125 ret = drm_encoder_init(dev, encoder, &tfp410_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +0200126 DRM_MODE_ENCODER_TMDS, NULL);
Rob Clark16ea9752013-01-08 15:04:28 -0600127 if (ret < 0)
128 goto fail;
129
130 drm_encoder_helper_add(encoder, &tfp410_encoder_helper_funcs);
131
132 return encoder;
133
134fail:
Jyri Sarhad0ec32c2016-02-23 12:44:27 +0200135 drm_encoder_cleanup(encoder);
Rob Clark16ea9752013-01-08 15:04:28 -0600136 return NULL;
137}
138
139/*
140 * Connector:
141 */
142
143struct tfp410_connector {
144 struct drm_connector base;
145
146 struct drm_encoder *encoder; /* our connected encoder */
147 struct tfp410_module *mod;
148};
149#define to_tfp410_connector(x) container_of(x, struct tfp410_connector, base)
150
151
152static void tfp410_connector_destroy(struct drm_connector *connector)
153{
Sachin Kamat62eb3e22014-07-09 17:12:57 +0530154 drm_connector_unregister(connector);
Rob Clark16ea9752013-01-08 15:04:28 -0600155 drm_connector_cleanup(connector);
Rob Clark16ea9752013-01-08 15:04:28 -0600156}
157
158static enum drm_connector_status tfp410_connector_detect(
159 struct drm_connector *connector,
160 bool force)
161{
162 struct tfp410_connector *tfp410_connector = to_tfp410_connector(connector);
163
164 if (drm_probe_ddc(tfp410_connector->mod->i2c))
165 return connector_status_connected;
166
167 return connector_status_unknown;
168}
169
170static int tfp410_connector_get_modes(struct drm_connector *connector)
171{
172 struct tfp410_connector *tfp410_connector = to_tfp410_connector(connector);
173 struct edid *edid;
174 int ret = 0;
175
176 edid = drm_get_edid(connector, tfp410_connector->mod->i2c);
177
178 drm_mode_connector_update_edid_property(connector, edid);
179
180 if (edid) {
181 ret = drm_add_edid_modes(connector, edid);
182 kfree(edid);
183 }
184
185 return ret;
186}
187
188static int tfp410_connector_mode_valid(struct drm_connector *connector,
189 struct drm_display_mode *mode)
190{
191 struct tilcdc_drm_private *priv = connector->dev->dev_private;
192 /* our only constraints are what the crtc can generate: */
193 return tilcdc_crtc_mode_valid(priv->crtc, mode);
194}
195
196static struct drm_encoder *tfp410_connector_best_encoder(
197 struct drm_connector *connector)
198{
199 struct tfp410_connector *tfp410_connector = to_tfp410_connector(connector);
200 return tfp410_connector->encoder;
201}
202
203static const struct drm_connector_funcs tfp410_connector_funcs = {
204 .destroy = tfp410_connector_destroy,
Jyri Sarha018cfbd2016-04-13 19:00:51 +0300205 .dpms = drm_atomic_helper_connector_dpms,
Rob Clark16ea9752013-01-08 15:04:28 -0600206 .detect = tfp410_connector_detect,
207 .fill_modes = drm_helper_probe_single_connector_modes,
Jyri Sarha018cfbd2016-04-13 19:00:51 +0300208 .reset = drm_atomic_helper_connector_reset,
209 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
210 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Rob Clark16ea9752013-01-08 15:04:28 -0600211};
212
213static const struct drm_connector_helper_funcs tfp410_connector_helper_funcs = {
214 .get_modes = tfp410_connector_get_modes,
215 .mode_valid = tfp410_connector_mode_valid,
216 .best_encoder = tfp410_connector_best_encoder,
217};
218
219static struct drm_connector *tfp410_connector_create(struct drm_device *dev,
220 struct tfp410_module *mod, struct drm_encoder *encoder)
221{
222 struct tfp410_connector *tfp410_connector;
223 struct drm_connector *connector;
224 int ret;
225
Jyri Sarhad0ec32c2016-02-23 12:44:27 +0200226 tfp410_connector = devm_kzalloc(dev->dev, sizeof(*tfp410_connector),
227 GFP_KERNEL);
Rob Clark16ea9752013-01-08 15:04:28 -0600228 if (!tfp410_connector) {
229 dev_err(dev->dev, "allocation failed\n");
230 return NULL;
231 }
232
233 tfp410_connector->encoder = encoder;
234 tfp410_connector->mod = mod;
235
236 connector = &tfp410_connector->base;
237
238 drm_connector_init(dev, connector, &tfp410_connector_funcs,
239 DRM_MODE_CONNECTOR_DVID);
240 drm_connector_helper_add(connector, &tfp410_connector_helper_funcs);
241
242 connector->polled = DRM_CONNECTOR_POLL_CONNECT |
243 DRM_CONNECTOR_POLL_DISCONNECT;
244
245 connector->interlace_allowed = 0;
246 connector->doublescan_allowed = 0;
247
248 ret = drm_mode_connector_attach_encoder(connector, encoder);
249 if (ret)
250 goto fail;
251
Thomas Wood34ea3d32014-05-29 16:57:41 +0100252 drm_connector_register(connector);
Rob Clark16ea9752013-01-08 15:04:28 -0600253
254 return connector;
255
256fail:
257 tfp410_connector_destroy(connector);
258 return NULL;
259}
260
261/*
262 * Module:
263 */
264
265static int tfp410_modeset_init(struct tilcdc_module *mod, struct drm_device *dev)
266{
267 struct tfp410_module *tfp410_mod = to_tfp410_module(mod);
268 struct tilcdc_drm_private *priv = dev->dev_private;
269 struct drm_encoder *encoder;
270 struct drm_connector *connector;
271
272 encoder = tfp410_encoder_create(dev, tfp410_mod);
273 if (!encoder)
274 return -ENOMEM;
275
276 connector = tfp410_connector_create(dev, tfp410_mod, encoder);
277 if (!connector)
278 return -ENOMEM;
279
280 priv->encoders[priv->num_encoders++] = encoder;
281 priv->connectors[priv->num_connectors++] = connector;
282
Jyri Sarha7c979b52016-04-13 18:59:16 +0300283 tilcdc_crtc_set_panel_info(priv->crtc, &dvi_info);
Rob Clark16ea9752013-01-08 15:04:28 -0600284 return 0;
285}
286
Rob Clark16ea9752013-01-08 15:04:28 -0600287static const struct tilcdc_module_ops tfp410_module_ops = {
288 .modeset_init = tfp410_modeset_init,
Rob Clark16ea9752013-01-08 15:04:28 -0600289};
290
291/*
292 * Device:
293 */
294
295static struct of_device_id tfp410_of_match[];
296
297static int tfp410_probe(struct platform_device *pdev)
298{
299 struct device_node *node = pdev->dev.of_node;
300 struct device_node *i2c_node;
301 struct tfp410_module *tfp410_mod;
302 struct tilcdc_module *mod;
303 struct pinctrl *pinctrl;
304 uint32_t i2c_phandle;
305 int ret = -EINVAL;
306
307 /* bail out early if no DT data: */
308 if (!node) {
309 dev_err(&pdev->dev, "device-tree data is missing\n");
310 return -ENXIO;
311 }
312
Jyri Sarhad0ec32c2016-02-23 12:44:27 +0200313 tfp410_mod = devm_kzalloc(&pdev->dev, sizeof(*tfp410_mod), GFP_KERNEL);
Rob Clark16ea9752013-01-08 15:04:28 -0600314 if (!tfp410_mod)
315 return -ENOMEM;
316
317 mod = &tfp410_mod->base;
Guido Martínez7cdcce92014-06-17 11:17:10 -0300318 pdev->dev.platform_data = mod;
Rob Clark16ea9752013-01-08 15:04:28 -0600319
320 tilcdc_module_init(mod, "tfp410", &tfp410_module_ops);
321
322 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
323 if (IS_ERR(pinctrl))
324 dev_warn(&pdev->dev, "pins are not configured\n");
325
326 if (of_property_read_u32(node, "i2c", &i2c_phandle)) {
327 dev_err(&pdev->dev, "could not get i2c bus phandle\n");
328 goto fail;
329 }
330
331 i2c_node = of_find_node_by_phandle(i2c_phandle);
332 if (!i2c_node) {
333 dev_err(&pdev->dev, "could not get i2c bus node\n");
334 goto fail;
335 }
336
337 tfp410_mod->i2c = of_find_i2c_adapter_by_node(i2c_node);
338 if (!tfp410_mod->i2c) {
339 dev_err(&pdev->dev, "could not get i2c\n");
Guido Martínez7cdcce92014-06-17 11:17:10 -0300340 of_node_put(i2c_node);
Rob Clark16ea9752013-01-08 15:04:28 -0600341 goto fail;
342 }
343
344 of_node_put(i2c_node);
345
346 tfp410_mod->gpio = of_get_named_gpio_flags(node, "powerdn-gpio",
347 0, NULL);
Arnd Bergmann287980e2016-05-27 23:23:25 +0200348 if (tfp410_mod->gpio < 0) {
Rob Clark16ea9752013-01-08 15:04:28 -0600349 dev_warn(&pdev->dev, "No power down GPIO\n");
350 } else {
351 ret = gpio_request(tfp410_mod->gpio, "DVI_PDn");
352 if (ret) {
353 dev_err(&pdev->dev, "could not get DVI_PDn gpio\n");
Guido Martínez7cdcce92014-06-17 11:17:10 -0300354 goto fail_adapter;
Rob Clark16ea9752013-01-08 15:04:28 -0600355 }
356 }
357
358 return 0;
359
Guido Martínez7cdcce92014-06-17 11:17:10 -0300360fail_adapter:
361 i2c_put_adapter(tfp410_mod->i2c);
362
Rob Clark16ea9752013-01-08 15:04:28 -0600363fail:
Guido Martínez7cdcce92014-06-17 11:17:10 -0300364 tilcdc_module_cleanup(mod);
Rob Clark16ea9752013-01-08 15:04:28 -0600365 return ret;
366}
367
368static int tfp410_remove(struct platform_device *pdev)
369{
Guido Martínez7cdcce92014-06-17 11:17:10 -0300370 struct tilcdc_module *mod = dev_get_platdata(&pdev->dev);
371 struct tfp410_module *tfp410_mod = to_tfp410_module(mod);
372
373 i2c_put_adapter(tfp410_mod->i2c);
374 gpio_free(tfp410_mod->gpio);
375
376 tilcdc_module_cleanup(mod);
Guido Martínez7cdcce92014-06-17 11:17:10 -0300377
Rob Clark16ea9752013-01-08 15:04:28 -0600378 return 0;
379}
380
381static struct of_device_id tfp410_of_match[] = {
382 { .compatible = "ti,tilcdc,tfp410", },
383 { },
384};
Rob Clark16ea9752013-01-08 15:04:28 -0600385
386struct platform_driver tfp410_driver = {
387 .probe = tfp410_probe,
388 .remove = tfp410_remove,
389 .driver = {
390 .owner = THIS_MODULE,
391 .name = "tfp410",
392 .of_match_table = tfp410_of_match,
393 },
394};
395
396int __init tilcdc_tfp410_init(void)
397{
398 return platform_driver_register(&tfp410_driver);
399}
400
401void __exit tilcdc_tfp410_fini(void)
402{
403 platform_driver_unregister(&tfp410_driver);
404}