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/gpio.h>
20 #include <linux/of_gpio.h>
21 #include <linux/pinctrl/pinmux.h>
22 #include <linux/pinctrl/consumer.h>
23 #include <drm/drm_atomic_helper.h>
25 #include "tilcdc_drv.h"
26 #include "tilcdc_tfp410.h"
28 struct tfp410_module {
29 struct tilcdc_module base;
30 struct i2c_adapter *i2c;
33 #define to_tfp410_module(x) container_of(x, struct tfp410_module, base)
36 static const struct tilcdc_panel_info dvi_info = {
52 struct tfp410_encoder {
53 struct drm_encoder base;
54 struct tfp410_module *mod;
57 #define to_tfp410_encoder(x) container_of(x, struct tfp410_encoder, base)
59 static void tfp410_encoder_dpms(struct drm_encoder *encoder, int mode)
61 struct tfp410_encoder *tfp410_encoder = to_tfp410_encoder(encoder);
63 if (tfp410_encoder->dpms == mode)
66 if (mode == DRM_MODE_DPMS_ON) {
68 gpio_direction_output(tfp410_encoder->mod->gpio, 1);
71 gpio_direction_output(tfp410_encoder->mod->gpio, 0);
74 tfp410_encoder->dpms = mode;
77 static void tfp410_encoder_prepare(struct drm_encoder *encoder)
79 tfp410_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
82 static void tfp410_encoder_commit(struct drm_encoder *encoder)
84 tfp410_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
87 static void tfp410_encoder_mode_set(struct drm_encoder *encoder,
88 struct drm_display_mode *mode,
89 struct drm_display_mode *adjusted_mode)
94 static const struct drm_encoder_funcs tfp410_encoder_funcs = {
95 .destroy = drm_encoder_cleanup,
98 static const struct drm_encoder_helper_funcs tfp410_encoder_helper_funcs = {
99 .dpms = tfp410_encoder_dpms,
100 .prepare = tfp410_encoder_prepare,
101 .commit = tfp410_encoder_commit,
102 .mode_set = tfp410_encoder_mode_set,
105 static struct drm_encoder *tfp410_encoder_create(struct drm_device *dev,
106 struct tfp410_module *mod)
108 struct tfp410_encoder *tfp410_encoder;
109 struct drm_encoder *encoder;
112 tfp410_encoder = devm_kzalloc(dev->dev, sizeof(*tfp410_encoder),
114 if (!tfp410_encoder) {
115 dev_err(dev->dev, "allocation failed\n");
119 tfp410_encoder->dpms = DRM_MODE_DPMS_OFF;
120 tfp410_encoder->mod = mod;
122 encoder = &tfp410_encoder->base;
123 encoder->possible_crtcs = 1;
125 ret = drm_encoder_init(dev, encoder, &tfp410_encoder_funcs,
126 DRM_MODE_ENCODER_TMDS, NULL);
130 drm_encoder_helper_add(encoder, &tfp410_encoder_helper_funcs);
135 drm_encoder_cleanup(encoder);
143 struct tfp410_connector {
144 struct drm_connector base;
146 struct drm_encoder *encoder; /* our connected encoder */
147 struct tfp410_module *mod;
149 #define to_tfp410_connector(x) container_of(x, struct tfp410_connector, base)
152 static void tfp410_connector_destroy(struct drm_connector *connector)
154 drm_connector_unregister(connector);
155 drm_connector_cleanup(connector);
158 static enum drm_connector_status tfp410_connector_detect(
159 struct drm_connector *connector,
162 struct tfp410_connector *tfp410_connector = to_tfp410_connector(connector);
164 if (drm_probe_ddc(tfp410_connector->mod->i2c))
165 return connector_status_connected;
167 return connector_status_unknown;
170 static int tfp410_connector_get_modes(struct drm_connector *connector)
172 struct tfp410_connector *tfp410_connector = to_tfp410_connector(connector);
176 edid = drm_get_edid(connector, tfp410_connector->mod->i2c);
178 drm_mode_connector_update_edid_property(connector, edid);
181 ret = drm_add_edid_modes(connector, edid);
188 static struct drm_encoder *tfp410_connector_best_encoder(
189 struct drm_connector *connector)
191 struct tfp410_connector *tfp410_connector = to_tfp410_connector(connector);
192 return tfp410_connector->encoder;
195 static const struct drm_connector_funcs tfp410_connector_funcs = {
196 .destroy = tfp410_connector_destroy,
197 .detect = tfp410_connector_detect,
198 .fill_modes = drm_helper_probe_single_connector_modes,
199 .reset = drm_atomic_helper_connector_reset,
200 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
201 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
204 static const struct drm_connector_helper_funcs tfp410_connector_helper_funcs = {
205 .get_modes = tfp410_connector_get_modes,
206 .best_encoder = tfp410_connector_best_encoder,
209 static struct drm_connector *tfp410_connector_create(struct drm_device *dev,
210 struct tfp410_module *mod, struct drm_encoder *encoder)
212 struct tfp410_connector *tfp410_connector;
213 struct drm_connector *connector;
216 tfp410_connector = devm_kzalloc(dev->dev, sizeof(*tfp410_connector),
218 if (!tfp410_connector) {
219 dev_err(dev->dev, "allocation failed\n");
223 tfp410_connector->encoder = encoder;
224 tfp410_connector->mod = mod;
226 connector = &tfp410_connector->base;
228 drm_connector_init(dev, connector, &tfp410_connector_funcs,
229 DRM_MODE_CONNECTOR_DVID);
230 drm_connector_helper_add(connector, &tfp410_connector_helper_funcs);
232 connector->polled = DRM_CONNECTOR_POLL_CONNECT |
233 DRM_CONNECTOR_POLL_DISCONNECT;
235 connector->interlace_allowed = 0;
236 connector->doublescan_allowed = 0;
238 ret = drm_mode_connector_attach_encoder(connector, encoder);
245 tfp410_connector_destroy(connector);
253 static int tfp410_modeset_init(struct tilcdc_module *mod, struct drm_device *dev)
255 struct tfp410_module *tfp410_mod = to_tfp410_module(mod);
256 struct tilcdc_drm_private *priv = dev->dev_private;
257 struct drm_encoder *encoder;
258 struct drm_connector *connector;
260 encoder = tfp410_encoder_create(dev, tfp410_mod);
264 connector = tfp410_connector_create(dev, tfp410_mod, encoder);
268 priv->encoders[priv->num_encoders++] = encoder;
269 priv->connectors[priv->num_connectors++] = connector;
271 tilcdc_crtc_set_panel_info(priv->crtc, &dvi_info);
275 static const struct tilcdc_module_ops tfp410_module_ops = {
276 .modeset_init = tfp410_modeset_init,
283 static struct of_device_id tfp410_of_match[];
285 static int tfp410_probe(struct platform_device *pdev)
287 struct device_node *node = pdev->dev.of_node;
288 struct device_node *i2c_node;
289 struct tfp410_module *tfp410_mod;
290 struct tilcdc_module *mod;
291 struct pinctrl *pinctrl;
292 uint32_t i2c_phandle;
295 /* bail out early if no DT data: */
297 dev_err(&pdev->dev, "device-tree data is missing\n");
301 tfp410_mod = devm_kzalloc(&pdev->dev, sizeof(*tfp410_mod), GFP_KERNEL);
305 mod = &tfp410_mod->base;
306 pdev->dev.platform_data = mod;
308 tilcdc_module_init(mod, "tfp410", &tfp410_module_ops);
310 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
312 dev_warn(&pdev->dev, "pins are not configured\n");
314 if (of_property_read_u32(node, "i2c", &i2c_phandle)) {
315 dev_err(&pdev->dev, "could not get i2c bus phandle\n");
319 i2c_node = of_find_node_by_phandle(i2c_phandle);
321 dev_err(&pdev->dev, "could not get i2c bus node\n");
325 tfp410_mod->i2c = of_find_i2c_adapter_by_node(i2c_node);
326 if (!tfp410_mod->i2c) {
327 dev_err(&pdev->dev, "could not get i2c\n");
328 of_node_put(i2c_node);
332 of_node_put(i2c_node);
334 tfp410_mod->gpio = of_get_named_gpio_flags(node, "powerdn-gpio",
336 if (tfp410_mod->gpio < 0) {
337 dev_warn(&pdev->dev, "No power down GPIO\n");
339 ret = gpio_request(tfp410_mod->gpio, "DVI_PDn");
341 dev_err(&pdev->dev, "could not get DVI_PDn gpio\n");
349 i2c_put_adapter(tfp410_mod->i2c);
352 tilcdc_module_cleanup(mod);
356 static int tfp410_remove(struct platform_device *pdev)
358 struct tilcdc_module *mod = dev_get_platdata(&pdev->dev);
359 struct tfp410_module *tfp410_mod = to_tfp410_module(mod);
361 i2c_put_adapter(tfp410_mod->i2c);
362 gpio_free(tfp410_mod->gpio);
364 tilcdc_module_cleanup(mod);
369 static struct of_device_id tfp410_of_match[] = {
370 { .compatible = "ti,tilcdc,tfp410", },
374 struct platform_driver tfp410_driver = {
375 .probe = tfp410_probe,
376 .remove = tfp410_remove,
378 .owner = THIS_MODULE,
380 .of_match_table = tfp410_of_match,
384 int __init tilcdc_tfp410_init(void)
386 return platform_driver_register(&tfp410_driver);
389 void __exit tilcdc_tfp410_fini(void)
391 platform_driver_unregister(&tfp410_driver);