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
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/of_i2c.h>
20 #include <linux/gpio.h>
21 #include <linux/of_gpio.h>
22 #include <linux/pinctrl/pinmux.h>
23 #include <linux/pinctrl/consumer.h>
25 #include "tilcdc_drv.h"
27 struct tfp410_module {
28 struct tilcdc_module base;
29 struct i2c_adapter *i2c;
32 #define to_tfp410_module(x) container_of(x, struct tfp410_module, base)
35 static const struct tilcdc_panel_info dvi_info = {
51 struct tfp410_encoder {
52 struct drm_encoder base;
53 struct tfp410_module *mod;
56 #define to_tfp410_encoder(x) container_of(x, struct tfp410_encoder, base)
59 static void tfp410_encoder_destroy(struct drm_encoder *encoder)
61 struct tfp410_encoder *tfp410_encoder = to_tfp410_encoder(encoder);
62 drm_encoder_cleanup(encoder);
63 kfree(tfp410_encoder);
66 static void tfp410_encoder_dpms(struct drm_encoder *encoder, int mode)
68 struct tfp410_encoder *tfp410_encoder = to_tfp410_encoder(encoder);
70 if (tfp410_encoder->dpms == mode)
73 if (mode == DRM_MODE_DPMS_ON) {
75 gpio_direction_output(tfp410_encoder->mod->gpio, 1);
78 gpio_direction_output(tfp410_encoder->mod->gpio, 0);
81 tfp410_encoder->dpms = mode;
84 static bool tfp410_encoder_mode_fixup(struct drm_encoder *encoder,
85 const struct drm_display_mode *mode,
86 struct drm_display_mode *adjusted_mode)
92 static void tfp410_encoder_prepare(struct drm_encoder *encoder)
94 tfp410_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
95 tilcdc_crtc_set_panel_info(encoder->crtc, &dvi_info);
98 static void tfp410_encoder_commit(struct drm_encoder *encoder)
100 tfp410_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
103 static void tfp410_encoder_mode_set(struct drm_encoder *encoder,
104 struct drm_display_mode *mode,
105 struct drm_display_mode *adjusted_mode)
110 static const struct drm_encoder_funcs tfp410_encoder_funcs = {
111 .destroy = tfp410_encoder_destroy,
114 static const struct drm_encoder_helper_funcs tfp410_encoder_helper_funcs = {
115 .dpms = tfp410_encoder_dpms,
116 .mode_fixup = tfp410_encoder_mode_fixup,
117 .prepare = tfp410_encoder_prepare,
118 .commit = tfp410_encoder_commit,
119 .mode_set = tfp410_encoder_mode_set,
122 static struct drm_encoder *tfp410_encoder_create(struct drm_device *dev,
123 struct tfp410_module *mod)
125 struct tfp410_encoder *tfp410_encoder;
126 struct drm_encoder *encoder;
129 tfp410_encoder = kzalloc(sizeof(*tfp410_encoder), GFP_KERNEL);
130 if (!tfp410_encoder) {
131 dev_err(dev->dev, "allocation failed\n");
135 tfp410_encoder->dpms = DRM_MODE_DPMS_OFF;
136 tfp410_encoder->mod = mod;
138 encoder = &tfp410_encoder->base;
139 encoder->possible_crtcs = 1;
141 ret = drm_encoder_init(dev, encoder, &tfp410_encoder_funcs,
142 DRM_MODE_ENCODER_TMDS);
146 drm_encoder_helper_add(encoder, &tfp410_encoder_helper_funcs);
151 tfp410_encoder_destroy(encoder);
159 struct tfp410_connector {
160 struct drm_connector base;
162 struct drm_encoder *encoder; /* our connected encoder */
163 struct tfp410_module *mod;
165 #define to_tfp410_connector(x) container_of(x, struct tfp410_connector, base)
168 static void tfp410_connector_destroy(struct drm_connector *connector)
170 struct tfp410_connector *tfp410_connector = to_tfp410_connector(connector);
171 drm_sysfs_connector_remove(connector);
172 drm_connector_cleanup(connector);
173 kfree(tfp410_connector);
176 static enum drm_connector_status tfp410_connector_detect(
177 struct drm_connector *connector,
180 struct tfp410_connector *tfp410_connector = to_tfp410_connector(connector);
182 if (drm_probe_ddc(tfp410_connector->mod->i2c))
183 return connector_status_connected;
185 return connector_status_unknown;
188 static int tfp410_connector_get_modes(struct drm_connector *connector)
190 struct tfp410_connector *tfp410_connector = to_tfp410_connector(connector);
194 edid = drm_get_edid(connector, tfp410_connector->mod->i2c);
196 drm_mode_connector_update_edid_property(connector, edid);
199 ret = drm_add_edid_modes(connector, edid);
206 static int tfp410_connector_mode_valid(struct drm_connector *connector,
207 struct drm_display_mode *mode)
209 struct tilcdc_drm_private *priv = connector->dev->dev_private;
210 /* our only constraints are what the crtc can generate: */
211 return tilcdc_crtc_mode_valid(priv->crtc, mode);
214 static struct drm_encoder *tfp410_connector_best_encoder(
215 struct drm_connector *connector)
217 struct tfp410_connector *tfp410_connector = to_tfp410_connector(connector);
218 return tfp410_connector->encoder;
221 static const struct drm_connector_funcs tfp410_connector_funcs = {
222 .destroy = tfp410_connector_destroy,
223 .dpms = drm_helper_connector_dpms,
224 .detect = tfp410_connector_detect,
225 .fill_modes = drm_helper_probe_single_connector_modes,
228 static const struct drm_connector_helper_funcs tfp410_connector_helper_funcs = {
229 .get_modes = tfp410_connector_get_modes,
230 .mode_valid = tfp410_connector_mode_valid,
231 .best_encoder = tfp410_connector_best_encoder,
234 static struct drm_connector *tfp410_connector_create(struct drm_device *dev,
235 struct tfp410_module *mod, struct drm_encoder *encoder)
237 struct tfp410_connector *tfp410_connector;
238 struct drm_connector *connector;
241 tfp410_connector = kzalloc(sizeof(*tfp410_connector), GFP_KERNEL);
242 if (!tfp410_connector) {
243 dev_err(dev->dev, "allocation failed\n");
247 tfp410_connector->encoder = encoder;
248 tfp410_connector->mod = mod;
250 connector = &tfp410_connector->base;
252 drm_connector_init(dev, connector, &tfp410_connector_funcs,
253 DRM_MODE_CONNECTOR_DVID);
254 drm_connector_helper_add(connector, &tfp410_connector_helper_funcs);
256 connector->polled = DRM_CONNECTOR_POLL_CONNECT |
257 DRM_CONNECTOR_POLL_DISCONNECT;
259 connector->interlace_allowed = 0;
260 connector->doublescan_allowed = 0;
262 ret = drm_mode_connector_attach_encoder(connector, encoder);
266 drm_sysfs_connector_add(connector);
271 tfp410_connector_destroy(connector);
279 static int tfp410_modeset_init(struct tilcdc_module *mod, struct drm_device *dev)
281 struct tfp410_module *tfp410_mod = to_tfp410_module(mod);
282 struct tilcdc_drm_private *priv = dev->dev_private;
283 struct drm_encoder *encoder;
284 struct drm_connector *connector;
286 encoder = tfp410_encoder_create(dev, tfp410_mod);
290 connector = tfp410_connector_create(dev, tfp410_mod, encoder);
294 priv->encoders[priv->num_encoders++] = encoder;
295 priv->connectors[priv->num_connectors++] = connector;
300 static void tfp410_destroy(struct tilcdc_module *mod)
302 struct tfp410_module *tfp410_mod = to_tfp410_module(mod);
305 i2c_put_adapter(tfp410_mod->i2c);
307 if (!IS_ERR_VALUE(tfp410_mod->gpio))
308 gpio_free(tfp410_mod->gpio);
310 tilcdc_module_cleanup(mod);
314 static const struct tilcdc_module_ops tfp410_module_ops = {
315 .modeset_init = tfp410_modeset_init,
316 .destroy = tfp410_destroy,
323 static struct of_device_id tfp410_of_match[];
325 static int tfp410_probe(struct platform_device *pdev)
327 struct device_node *node = pdev->dev.of_node;
328 struct device_node *i2c_node;
329 struct tfp410_module *tfp410_mod;
330 struct tilcdc_module *mod;
331 struct pinctrl *pinctrl;
332 uint32_t i2c_phandle;
335 /* bail out early if no DT data: */
337 dev_err(&pdev->dev, "device-tree data is missing\n");
341 tfp410_mod = kzalloc(sizeof(*tfp410_mod), GFP_KERNEL);
345 mod = &tfp410_mod->base;
347 tilcdc_module_init(mod, "tfp410", &tfp410_module_ops);
349 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
351 dev_warn(&pdev->dev, "pins are not configured\n");
353 if (of_property_read_u32(node, "i2c", &i2c_phandle)) {
354 dev_err(&pdev->dev, "could not get i2c bus phandle\n");
358 i2c_node = of_find_node_by_phandle(i2c_phandle);
360 dev_err(&pdev->dev, "could not get i2c bus node\n");
364 tfp410_mod->i2c = of_find_i2c_adapter_by_node(i2c_node);
365 if (!tfp410_mod->i2c) {
366 dev_err(&pdev->dev, "could not get i2c\n");
370 of_node_put(i2c_node);
372 tfp410_mod->gpio = of_get_named_gpio_flags(node, "powerdn-gpio",
374 if (IS_ERR_VALUE(tfp410_mod->gpio)) {
375 dev_warn(&pdev->dev, "No power down GPIO\n");
377 ret = gpio_request(tfp410_mod->gpio, "DVI_PDn");
379 dev_err(&pdev->dev, "could not get DVI_PDn gpio\n");
391 static int tfp410_remove(struct platform_device *pdev)
396 static struct of_device_id tfp410_of_match[] = {
397 { .compatible = "ti,tilcdc,tfp410", },
401 struct platform_driver tfp410_driver = {
402 .probe = tfp410_probe,
403 .remove = tfp410_remove,
405 .owner = THIS_MODULE,
407 .of_match_table = tfp410_of_match,
411 int __init tilcdc_tfp410_init(void)
413 return platform_driver_register(&tfp410_driver);
416 void __exit tilcdc_tfp410_fini(void)
418 platform_driver_unregister(&tfp410_driver);