Linux 4.2.1
[linux/fpc-iii.git] / drivers / gpu / drm / tilcdc / tilcdc_tfp410.c
blob354c47ca637407550a9fee0490d537c47e827209
1 /*
2 * Copyright (C) 2012 Texas Instruments
3 * Author: Rob Clark <robdclark@gmail.com>
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.
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.
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/>.
18 #include <linux/i2c.h>
19 #include <linux/gpio.h>
20 #include <linux/of_gpio.h>
21 #include <linux/pinctrl/pinmux.h>
22 #include <linux/pinctrl/consumer.h>
24 #include "tilcdc_drv.h"
26 struct tfp410_module {
27 struct tilcdc_module base;
28 struct i2c_adapter *i2c;
29 int gpio;
31 #define to_tfp410_module(x) container_of(x, struct tfp410_module, base)
34 static 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,
47 * Encoder:
50 struct tfp410_encoder {
51 struct drm_encoder base;
52 struct tfp410_module *mod;
53 int dpms;
55 #define to_tfp410_encoder(x) container_of(x, struct tfp410_encoder, base)
58 static void tfp410_encoder_destroy(struct drm_encoder *encoder)
60 struct tfp410_encoder *tfp410_encoder = to_tfp410_encoder(encoder);
61 drm_encoder_cleanup(encoder);
62 kfree(tfp410_encoder);
65 static void tfp410_encoder_dpms(struct drm_encoder *encoder, int mode)
67 struct tfp410_encoder *tfp410_encoder = to_tfp410_encoder(encoder);
69 if (tfp410_encoder->dpms == mode)
70 return;
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);
80 tfp410_encoder->dpms = mode;
83 static bool tfp410_encoder_mode_fixup(struct drm_encoder *encoder,
84 const struct drm_display_mode *mode,
85 struct drm_display_mode *adjusted_mode)
87 /* nothing needed */
88 return true;
91 static void tfp410_encoder_prepare(struct drm_encoder *encoder)
93 tfp410_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
94 tilcdc_crtc_set_panel_info(encoder->crtc, &dvi_info);
97 static void tfp410_encoder_commit(struct drm_encoder *encoder)
99 tfp410_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
102 static void tfp410_encoder_mode_set(struct drm_encoder *encoder,
103 struct drm_display_mode *mode,
104 struct drm_display_mode *adjusted_mode)
106 /* nothing needed */
109 static const struct drm_encoder_funcs tfp410_encoder_funcs = {
110 .destroy = tfp410_encoder_destroy,
113 static const struct drm_encoder_helper_funcs tfp410_encoder_helper_funcs = {
114 .dpms = tfp410_encoder_dpms,
115 .mode_fixup = tfp410_encoder_mode_fixup,
116 .prepare = tfp410_encoder_prepare,
117 .commit = tfp410_encoder_commit,
118 .mode_set = tfp410_encoder_mode_set,
121 static struct drm_encoder *tfp410_encoder_create(struct drm_device *dev,
122 struct tfp410_module *mod)
124 struct tfp410_encoder *tfp410_encoder;
125 struct drm_encoder *encoder;
126 int ret;
128 tfp410_encoder = kzalloc(sizeof(*tfp410_encoder), GFP_KERNEL);
129 if (!tfp410_encoder) {
130 dev_err(dev->dev, "allocation failed\n");
131 return NULL;
134 tfp410_encoder->dpms = DRM_MODE_DPMS_OFF;
135 tfp410_encoder->mod = mod;
137 encoder = &tfp410_encoder->base;
138 encoder->possible_crtcs = 1;
140 ret = drm_encoder_init(dev, encoder, &tfp410_encoder_funcs,
141 DRM_MODE_ENCODER_TMDS);
142 if (ret < 0)
143 goto fail;
145 drm_encoder_helper_add(encoder, &tfp410_encoder_helper_funcs);
147 return encoder;
149 fail:
150 tfp410_encoder_destroy(encoder);
151 return NULL;
155 * Connector:
158 struct tfp410_connector {
159 struct drm_connector base;
161 struct drm_encoder *encoder; /* our connected encoder */
162 struct tfp410_module *mod;
164 #define to_tfp410_connector(x) container_of(x, struct tfp410_connector, base)
167 static void tfp410_connector_destroy(struct drm_connector *connector)
169 struct tfp410_connector *tfp410_connector = to_tfp410_connector(connector);
170 drm_connector_unregister(connector);
171 drm_connector_cleanup(connector);
172 kfree(tfp410_connector);
175 static enum drm_connector_status tfp410_connector_detect(
176 struct drm_connector *connector,
177 bool force)
179 struct tfp410_connector *tfp410_connector = to_tfp410_connector(connector);
181 if (drm_probe_ddc(tfp410_connector->mod->i2c))
182 return connector_status_connected;
184 return connector_status_unknown;
187 static int tfp410_connector_get_modes(struct drm_connector *connector)
189 struct tfp410_connector *tfp410_connector = to_tfp410_connector(connector);
190 struct edid *edid;
191 int ret = 0;
193 edid = drm_get_edid(connector, tfp410_connector->mod->i2c);
195 drm_mode_connector_update_edid_property(connector, edid);
197 if (edid) {
198 ret = drm_add_edid_modes(connector, edid);
199 kfree(edid);
202 return ret;
205 static int tfp410_connector_mode_valid(struct drm_connector *connector,
206 struct drm_display_mode *mode)
208 struct tilcdc_drm_private *priv = connector->dev->dev_private;
209 /* our only constraints are what the crtc can generate: */
210 return tilcdc_crtc_mode_valid(priv->crtc, mode);
213 static struct drm_encoder *tfp410_connector_best_encoder(
214 struct drm_connector *connector)
216 struct tfp410_connector *tfp410_connector = to_tfp410_connector(connector);
217 return tfp410_connector->encoder;
220 static const struct drm_connector_funcs tfp410_connector_funcs = {
221 .destroy = tfp410_connector_destroy,
222 .dpms = drm_helper_connector_dpms,
223 .detect = tfp410_connector_detect,
224 .fill_modes = drm_helper_probe_single_connector_modes,
227 static const struct drm_connector_helper_funcs tfp410_connector_helper_funcs = {
228 .get_modes = tfp410_connector_get_modes,
229 .mode_valid = tfp410_connector_mode_valid,
230 .best_encoder = tfp410_connector_best_encoder,
233 static struct drm_connector *tfp410_connector_create(struct drm_device *dev,
234 struct tfp410_module *mod, struct drm_encoder *encoder)
236 struct tfp410_connector *tfp410_connector;
237 struct drm_connector *connector;
238 int ret;
240 tfp410_connector = kzalloc(sizeof(*tfp410_connector), GFP_KERNEL);
241 if (!tfp410_connector) {
242 dev_err(dev->dev, "allocation failed\n");
243 return NULL;
246 tfp410_connector->encoder = encoder;
247 tfp410_connector->mod = mod;
249 connector = &tfp410_connector->base;
251 drm_connector_init(dev, connector, &tfp410_connector_funcs,
252 DRM_MODE_CONNECTOR_DVID);
253 drm_connector_helper_add(connector, &tfp410_connector_helper_funcs);
255 connector->polled = DRM_CONNECTOR_POLL_CONNECT |
256 DRM_CONNECTOR_POLL_DISCONNECT;
258 connector->interlace_allowed = 0;
259 connector->doublescan_allowed = 0;
261 ret = drm_mode_connector_attach_encoder(connector, encoder);
262 if (ret)
263 goto fail;
265 drm_connector_register(connector);
267 return connector;
269 fail:
270 tfp410_connector_destroy(connector);
271 return NULL;
275 * Module:
278 static int tfp410_modeset_init(struct tilcdc_module *mod, struct drm_device *dev)
280 struct tfp410_module *tfp410_mod = to_tfp410_module(mod);
281 struct tilcdc_drm_private *priv = dev->dev_private;
282 struct drm_encoder *encoder;
283 struct drm_connector *connector;
285 encoder = tfp410_encoder_create(dev, tfp410_mod);
286 if (!encoder)
287 return -ENOMEM;
289 connector = tfp410_connector_create(dev, tfp410_mod, encoder);
290 if (!connector)
291 return -ENOMEM;
293 priv->encoders[priv->num_encoders++] = encoder;
294 priv->connectors[priv->num_connectors++] = connector;
296 return 0;
299 static const struct tilcdc_module_ops tfp410_module_ops = {
300 .modeset_init = tfp410_modeset_init,
304 * Device:
307 static struct of_device_id tfp410_of_match[];
309 static int tfp410_probe(struct platform_device *pdev)
311 struct device_node *node = pdev->dev.of_node;
312 struct device_node *i2c_node;
313 struct tfp410_module *tfp410_mod;
314 struct tilcdc_module *mod;
315 struct pinctrl *pinctrl;
316 uint32_t i2c_phandle;
317 int ret = -EINVAL;
319 /* bail out early if no DT data: */
320 if (!node) {
321 dev_err(&pdev->dev, "device-tree data is missing\n");
322 return -ENXIO;
325 tfp410_mod = kzalloc(sizeof(*tfp410_mod), GFP_KERNEL);
326 if (!tfp410_mod)
327 return -ENOMEM;
329 mod = &tfp410_mod->base;
330 pdev->dev.platform_data = mod;
332 tilcdc_module_init(mod, "tfp410", &tfp410_module_ops);
334 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
335 if (IS_ERR(pinctrl))
336 dev_warn(&pdev->dev, "pins are not configured\n");
338 if (of_property_read_u32(node, "i2c", &i2c_phandle)) {
339 dev_err(&pdev->dev, "could not get i2c bus phandle\n");
340 goto fail;
343 mod->preferred_bpp = dvi_info.bpp;
345 i2c_node = of_find_node_by_phandle(i2c_phandle);
346 if (!i2c_node) {
347 dev_err(&pdev->dev, "could not get i2c bus node\n");
348 goto fail;
351 tfp410_mod->i2c = of_find_i2c_adapter_by_node(i2c_node);
352 if (!tfp410_mod->i2c) {
353 dev_err(&pdev->dev, "could not get i2c\n");
354 of_node_put(i2c_node);
355 goto fail;
358 of_node_put(i2c_node);
360 tfp410_mod->gpio = of_get_named_gpio_flags(node, "powerdn-gpio",
361 0, NULL);
362 if (IS_ERR_VALUE(tfp410_mod->gpio)) {
363 dev_warn(&pdev->dev, "No power down GPIO\n");
364 } else {
365 ret = gpio_request(tfp410_mod->gpio, "DVI_PDn");
366 if (ret) {
367 dev_err(&pdev->dev, "could not get DVI_PDn gpio\n");
368 goto fail_adapter;
372 return 0;
374 fail_adapter:
375 i2c_put_adapter(tfp410_mod->i2c);
377 fail:
378 kfree(tfp410_mod);
379 tilcdc_module_cleanup(mod);
380 return ret;
383 static int tfp410_remove(struct platform_device *pdev)
385 struct tilcdc_module *mod = dev_get_platdata(&pdev->dev);
386 struct tfp410_module *tfp410_mod = to_tfp410_module(mod);
388 i2c_put_adapter(tfp410_mod->i2c);
389 gpio_free(tfp410_mod->gpio);
391 tilcdc_module_cleanup(mod);
392 kfree(tfp410_mod);
394 return 0;
397 static struct of_device_id tfp410_of_match[] = {
398 { .compatible = "ti,tilcdc,tfp410", },
399 { },
402 struct platform_driver tfp410_driver = {
403 .probe = tfp410_probe,
404 .remove = tfp410_remove,
405 .driver = {
406 .owner = THIS_MODULE,
407 .name = "tfp410",
408 .of_match_table = tfp410_of_match,
412 int __init tilcdc_tfp410_init(void)
414 return platform_driver_register(&tfp410_driver);
417 void __exit tilcdc_tfp410_fini(void)
419 platform_driver_unregister(&tfp410_driver);