blob: 21f6485c87cb15273b609fdab0e6565b38f889b5 [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
Rob Clark16ea9752013-01-08 15:04:28 -060057static void tfp410_encoder_dpms(struct drm_encoder *encoder, int mode)
58{
59 struct tfp410_encoder *tfp410_encoder = to_tfp410_encoder(encoder);
60
61 if (tfp410_encoder->dpms == mode)
62 return;
63
64 if (mode == DRM_MODE_DPMS_ON) {
65 DBG("Power on");
66 gpio_direction_output(tfp410_encoder->mod->gpio, 1);
67 } else {
68 DBG("Power off");
69 gpio_direction_output(tfp410_encoder->mod->gpio, 0);
70 }
71
72 tfp410_encoder->dpms = mode;
73}
74
Rob Clark16ea9752013-01-08 15:04:28 -060075static void tfp410_encoder_prepare(struct drm_encoder *encoder)
76{
77 tfp410_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
Rob Clark16ea9752013-01-08 15:04:28 -060078}
79
80static void tfp410_encoder_commit(struct drm_encoder *encoder)
81{
82 tfp410_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
83}
84
85static void tfp410_encoder_mode_set(struct drm_encoder *encoder,
86 struct drm_display_mode *mode,
87 struct drm_display_mode *adjusted_mode)
88{
89 /* nothing needed */
90}
91
92static const struct drm_encoder_funcs tfp410_encoder_funcs = {
Jyri Sarhad0ec32c2016-02-23 12:44:27 +020093 .destroy = drm_encoder_cleanup,
Rob Clark16ea9752013-01-08 15:04:28 -060094};
95
96static const struct drm_encoder_helper_funcs tfp410_encoder_helper_funcs = {
97 .dpms = tfp410_encoder_dpms,
Rob Clark16ea9752013-01-08 15:04:28 -060098 .prepare = tfp410_encoder_prepare,
99 .commit = tfp410_encoder_commit,
100 .mode_set = tfp410_encoder_mode_set,
101};
102
103static struct drm_encoder *tfp410_encoder_create(struct drm_device *dev,
104 struct tfp410_module *mod)
105{
106 struct tfp410_encoder *tfp410_encoder;
107 struct drm_encoder *encoder;
108 int ret;
109
Jyri Sarhad0ec32c2016-02-23 12:44:27 +0200110 tfp410_encoder = devm_kzalloc(dev->dev, sizeof(*tfp410_encoder),
111 GFP_KERNEL);
Rob Clark16ea9752013-01-08 15:04:28 -0600112 if (!tfp410_encoder) {
113 dev_err(dev->dev, "allocation failed\n");
114 return NULL;
115 }
116
117 tfp410_encoder->dpms = DRM_MODE_DPMS_OFF;
118 tfp410_encoder->mod = mod;
119
120 encoder = &tfp410_encoder->base;
121 encoder->possible_crtcs = 1;
122
123 ret = drm_encoder_init(dev, encoder, &tfp410_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +0200124 DRM_MODE_ENCODER_TMDS, NULL);
Rob Clark16ea9752013-01-08 15:04:28 -0600125 if (ret < 0)
126 goto fail;
127
128 drm_encoder_helper_add(encoder, &tfp410_encoder_helper_funcs);
129
130 return encoder;
131
132fail:
Jyri Sarhad0ec32c2016-02-23 12:44:27 +0200133 drm_encoder_cleanup(encoder);
Rob Clark16ea9752013-01-08 15:04:28 -0600134 return NULL;
135}
136
137/*
138 * Connector:
139 */
140
141struct tfp410_connector {
142 struct drm_connector base;
143
144 struct drm_encoder *encoder; /* our connected encoder */
145 struct tfp410_module *mod;
146};
147#define to_tfp410_connector(x) container_of(x, struct tfp410_connector, base)
148
149
150static void tfp410_connector_destroy(struct drm_connector *connector)
151{
Sachin Kamat62eb3e22014-07-09 17:12:57 +0530152 drm_connector_unregister(connector);
Rob Clark16ea9752013-01-08 15:04:28 -0600153 drm_connector_cleanup(connector);
Rob Clark16ea9752013-01-08 15:04:28 -0600154}
155
156static enum drm_connector_status tfp410_connector_detect(
157 struct drm_connector *connector,
158 bool force)
159{
160 struct tfp410_connector *tfp410_connector = to_tfp410_connector(connector);
161
162 if (drm_probe_ddc(tfp410_connector->mod->i2c))
163 return connector_status_connected;
164
165 return connector_status_unknown;
166}
167
168static int tfp410_connector_get_modes(struct drm_connector *connector)
169{
170 struct tfp410_connector *tfp410_connector = to_tfp410_connector(connector);
171 struct edid *edid;
172 int ret = 0;
173
174 edid = drm_get_edid(connector, tfp410_connector->mod->i2c);
175
176 drm_mode_connector_update_edid_property(connector, edid);
177
178 if (edid) {
179 ret = drm_add_edid_modes(connector, edid);
180 kfree(edid);
181 }
182
183 return ret;
184}
185
186static int tfp410_connector_mode_valid(struct drm_connector *connector,
187 struct drm_display_mode *mode)
188{
189 struct tilcdc_drm_private *priv = connector->dev->dev_private;
190 /* our only constraints are what the crtc can generate: */
191 return tilcdc_crtc_mode_valid(priv->crtc, mode);
192}
193
194static struct drm_encoder *tfp410_connector_best_encoder(
195 struct drm_connector *connector)
196{
197 struct tfp410_connector *tfp410_connector = to_tfp410_connector(connector);
198 return tfp410_connector->encoder;
199}
200
201static const struct drm_connector_funcs tfp410_connector_funcs = {
202 .destroy = tfp410_connector_destroy,
203 .dpms = drm_helper_connector_dpms,
204 .detect = tfp410_connector_detect,
205 .fill_modes = drm_helper_probe_single_connector_modes,
206};
207
208static const struct drm_connector_helper_funcs tfp410_connector_helper_funcs = {
209 .get_modes = tfp410_connector_get_modes,
210 .mode_valid = tfp410_connector_mode_valid,
211 .best_encoder = tfp410_connector_best_encoder,
212};
213
214static struct drm_connector *tfp410_connector_create(struct drm_device *dev,
215 struct tfp410_module *mod, struct drm_encoder *encoder)
216{
217 struct tfp410_connector *tfp410_connector;
218 struct drm_connector *connector;
219 int ret;
220
Jyri Sarhad0ec32c2016-02-23 12:44:27 +0200221 tfp410_connector = devm_kzalloc(dev->dev, sizeof(*tfp410_connector),
222 GFP_KERNEL);
Rob Clark16ea9752013-01-08 15:04:28 -0600223 if (!tfp410_connector) {
224 dev_err(dev->dev, "allocation failed\n");
225 return NULL;
226 }
227
228 tfp410_connector->encoder = encoder;
229 tfp410_connector->mod = mod;
230
231 connector = &tfp410_connector->base;
232
233 drm_connector_init(dev, connector, &tfp410_connector_funcs,
234 DRM_MODE_CONNECTOR_DVID);
235 drm_connector_helper_add(connector, &tfp410_connector_helper_funcs);
236
237 connector->polled = DRM_CONNECTOR_POLL_CONNECT |
238 DRM_CONNECTOR_POLL_DISCONNECT;
239
240 connector->interlace_allowed = 0;
241 connector->doublescan_allowed = 0;
242
243 ret = drm_mode_connector_attach_encoder(connector, encoder);
244 if (ret)
245 goto fail;
246
Thomas Wood34ea3d32014-05-29 16:57:41 +0100247 drm_connector_register(connector);
Rob Clark16ea9752013-01-08 15:04:28 -0600248
249 return connector;
250
251fail:
252 tfp410_connector_destroy(connector);
253 return NULL;
254}
255
256/*
257 * Module:
258 */
259
260static int tfp410_modeset_init(struct tilcdc_module *mod, struct drm_device *dev)
261{
262 struct tfp410_module *tfp410_mod = to_tfp410_module(mod);
263 struct tilcdc_drm_private *priv = dev->dev_private;
264 struct drm_encoder *encoder;
265 struct drm_connector *connector;
266
267 encoder = tfp410_encoder_create(dev, tfp410_mod);
268 if (!encoder)
269 return -ENOMEM;
270
271 connector = tfp410_connector_create(dev, tfp410_mod, encoder);
272 if (!connector)
273 return -ENOMEM;
274
275 priv->encoders[priv->num_encoders++] = encoder;
276 priv->connectors[priv->num_connectors++] = connector;
277
Jyri Sarha7c979b52016-04-13 18:59:16 +0300278 tilcdc_crtc_set_panel_info(priv->crtc, &dvi_info);
Rob Clark16ea9752013-01-08 15:04:28 -0600279 return 0;
280}
281
Rob Clark16ea9752013-01-08 15:04:28 -0600282static const struct tilcdc_module_ops tfp410_module_ops = {
283 .modeset_init = tfp410_modeset_init,
Rob Clark16ea9752013-01-08 15:04:28 -0600284};
285
286/*
287 * Device:
288 */
289
290static struct of_device_id tfp410_of_match[];
291
292static int tfp410_probe(struct platform_device *pdev)
293{
294 struct device_node *node = pdev->dev.of_node;
295 struct device_node *i2c_node;
296 struct tfp410_module *tfp410_mod;
297 struct tilcdc_module *mod;
298 struct pinctrl *pinctrl;
299 uint32_t i2c_phandle;
300 int ret = -EINVAL;
301
302 /* bail out early if no DT data: */
303 if (!node) {
304 dev_err(&pdev->dev, "device-tree data is missing\n");
305 return -ENXIO;
306 }
307
Jyri Sarhad0ec32c2016-02-23 12:44:27 +0200308 tfp410_mod = devm_kzalloc(&pdev->dev, sizeof(*tfp410_mod), GFP_KERNEL);
Rob Clark16ea9752013-01-08 15:04:28 -0600309 if (!tfp410_mod)
310 return -ENOMEM;
311
312 mod = &tfp410_mod->base;
Guido Martínez7cdcce92014-06-17 11:17:10 -0300313 pdev->dev.platform_data = mod;
Rob Clark16ea9752013-01-08 15:04:28 -0600314
315 tilcdc_module_init(mod, "tfp410", &tfp410_module_ops);
316
317 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
318 if (IS_ERR(pinctrl))
319 dev_warn(&pdev->dev, "pins are not configured\n");
320
321 if (of_property_read_u32(node, "i2c", &i2c_phandle)) {
322 dev_err(&pdev->dev, "could not get i2c bus phandle\n");
323 goto fail;
324 }
325
Benoit Parrotdc28aa02013-06-18 17:18:31 -0500326 mod->preferred_bpp = dvi_info.bpp;
327
Rob Clark16ea9752013-01-08 15:04:28 -0600328 i2c_node = of_find_node_by_phandle(i2c_phandle);
329 if (!i2c_node) {
330 dev_err(&pdev->dev, "could not get i2c bus node\n");
331 goto fail;
332 }
333
334 tfp410_mod->i2c = of_find_i2c_adapter_by_node(i2c_node);
335 if (!tfp410_mod->i2c) {
336 dev_err(&pdev->dev, "could not get i2c\n");
Guido Martínez7cdcce92014-06-17 11:17:10 -0300337 of_node_put(i2c_node);
Rob Clark16ea9752013-01-08 15:04:28 -0600338 goto fail;
339 }
340
341 of_node_put(i2c_node);
342
343 tfp410_mod->gpio = of_get_named_gpio_flags(node, "powerdn-gpio",
344 0, NULL);
Arnd Bergmann287980e2016-05-27 23:23:25 +0200345 if (tfp410_mod->gpio < 0) {
Rob Clark16ea9752013-01-08 15:04:28 -0600346 dev_warn(&pdev->dev, "No power down GPIO\n");
347 } else {
348 ret = gpio_request(tfp410_mod->gpio, "DVI_PDn");
349 if (ret) {
350 dev_err(&pdev->dev, "could not get DVI_PDn gpio\n");
Guido Martínez7cdcce92014-06-17 11:17:10 -0300351 goto fail_adapter;
Rob Clark16ea9752013-01-08 15:04:28 -0600352 }
353 }
354
355 return 0;
356
Guido Martínez7cdcce92014-06-17 11:17:10 -0300357fail_adapter:
358 i2c_put_adapter(tfp410_mod->i2c);
359
Rob Clark16ea9752013-01-08 15:04:28 -0600360fail:
Guido Martínez7cdcce92014-06-17 11:17:10 -0300361 tilcdc_module_cleanup(mod);
Rob Clark16ea9752013-01-08 15:04:28 -0600362 return ret;
363}
364
365static int tfp410_remove(struct platform_device *pdev)
366{
Guido Martínez7cdcce92014-06-17 11:17:10 -0300367 struct tilcdc_module *mod = dev_get_platdata(&pdev->dev);
368 struct tfp410_module *tfp410_mod = to_tfp410_module(mod);
369
370 i2c_put_adapter(tfp410_mod->i2c);
371 gpio_free(tfp410_mod->gpio);
372
373 tilcdc_module_cleanup(mod);
Guido Martínez7cdcce92014-06-17 11:17:10 -0300374
Rob Clark16ea9752013-01-08 15:04:28 -0600375 return 0;
376}
377
378static struct of_device_id tfp410_of_match[] = {
379 { .compatible = "ti,tilcdc,tfp410", },
380 { },
381};
Rob Clark16ea9752013-01-08 15:04:28 -0600382
383struct platform_driver tfp410_driver = {
384 .probe = tfp410_probe,
385 .remove = tfp410_remove,
386 .driver = {
387 .owner = THIS_MODULE,
388 .name = "tfp410",
389 .of_match_table = tfp410_of_match,
390 },
391};
392
393int __init tilcdc_tfp410_init(void)
394{
395 return platform_driver_register(&tfp410_driver);
396}
397
398void __exit tilcdc_tfp410_fini(void)
399{
400 platform_driver_unregister(&tfp410_driver);
401}