• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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>
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>
24 
25 #include "tilcdc_drv.h"
26 #include "tilcdc_tfp410.h"
27 
28 struct 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 
36 static 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 
52 struct 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 
tfp410_encoder_dpms(struct drm_encoder * encoder,int mode)59 static 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 
tfp410_encoder_prepare(struct drm_encoder * encoder)77 static void tfp410_encoder_prepare(struct drm_encoder *encoder)
78 {
79 	tfp410_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
80 }
81 
tfp410_encoder_commit(struct drm_encoder * encoder)82 static void tfp410_encoder_commit(struct drm_encoder *encoder)
83 {
84 	tfp410_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
85 }
86 
tfp410_encoder_mode_set(struct drm_encoder * encoder,struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)87 static 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 
94 static const struct drm_encoder_funcs tfp410_encoder_funcs = {
95 		.destroy        = drm_encoder_cleanup,
96 };
97 
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,
103 };
104 
tfp410_encoder_create(struct drm_device * dev,struct tfp410_module * mod)105 static 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 
112 	tfp410_encoder = devm_kzalloc(dev->dev, sizeof(*tfp410_encoder),
113 				      GFP_KERNEL);
114 	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,
126 			DRM_MODE_ENCODER_TMDS, NULL);
127 	if (ret < 0)
128 		goto fail;
129 
130 	drm_encoder_helper_add(encoder, &tfp410_encoder_helper_funcs);
131 
132 	return encoder;
133 
134 fail:
135 	drm_encoder_cleanup(encoder);
136 	return NULL;
137 }
138 
139 /*
140  * Connector:
141  */
142 
143 struct 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 
tfp410_connector_destroy(struct drm_connector * connector)152 static void tfp410_connector_destroy(struct drm_connector *connector)
153 {
154 	drm_connector_unregister(connector);
155 	drm_connector_cleanup(connector);
156 }
157 
tfp410_connector_detect(struct drm_connector * connector,bool force)158 static 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 
tfp410_connector_get_modes(struct drm_connector * connector)170 static 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 
tfp410_connector_mode_valid(struct drm_connector * connector,struct drm_display_mode * mode)188 static 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 
tfp410_connector_best_encoder(struct drm_connector * connector)196 static 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 
203 static const struct drm_connector_funcs tfp410_connector_funcs = {
204 	.destroy            = tfp410_connector_destroy,
205 	.detect             = tfp410_connector_detect,
206 	.fill_modes         = drm_helper_probe_single_connector_modes,
207 	.reset              = drm_atomic_helper_connector_reset,
208 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
209 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
210 };
211 
212 static const struct drm_connector_helper_funcs tfp410_connector_helper_funcs = {
213 	.get_modes          = tfp410_connector_get_modes,
214 	.mode_valid         = tfp410_connector_mode_valid,
215 	.best_encoder       = tfp410_connector_best_encoder,
216 };
217 
tfp410_connector_create(struct drm_device * dev,struct tfp410_module * mod,struct drm_encoder * encoder)218 static struct drm_connector *tfp410_connector_create(struct drm_device *dev,
219 		struct tfp410_module *mod, struct drm_encoder *encoder)
220 {
221 	struct tfp410_connector *tfp410_connector;
222 	struct drm_connector *connector;
223 	int ret;
224 
225 	tfp410_connector = devm_kzalloc(dev->dev, sizeof(*tfp410_connector),
226 					GFP_KERNEL);
227 	if (!tfp410_connector) {
228 		dev_err(dev->dev, "allocation failed\n");
229 		return NULL;
230 	}
231 
232 	tfp410_connector->encoder = encoder;
233 	tfp410_connector->mod = mod;
234 
235 	connector = &tfp410_connector->base;
236 
237 	drm_connector_init(dev, connector, &tfp410_connector_funcs,
238 			DRM_MODE_CONNECTOR_DVID);
239 	drm_connector_helper_add(connector, &tfp410_connector_helper_funcs);
240 
241 	connector->polled = DRM_CONNECTOR_POLL_CONNECT |
242 			DRM_CONNECTOR_POLL_DISCONNECT;
243 
244 	connector->interlace_allowed = 0;
245 	connector->doublescan_allowed = 0;
246 
247 	ret = drm_mode_connector_attach_encoder(connector, encoder);
248 	if (ret)
249 		goto fail;
250 
251 	return connector;
252 
253 fail:
254 	tfp410_connector_destroy(connector);
255 	return NULL;
256 }
257 
258 /*
259  * Module:
260  */
261 
tfp410_modeset_init(struct tilcdc_module * mod,struct drm_device * dev)262 static int tfp410_modeset_init(struct tilcdc_module *mod, struct drm_device *dev)
263 {
264 	struct tfp410_module *tfp410_mod = to_tfp410_module(mod);
265 	struct tilcdc_drm_private *priv = dev->dev_private;
266 	struct drm_encoder *encoder;
267 	struct drm_connector *connector;
268 
269 	encoder = tfp410_encoder_create(dev, tfp410_mod);
270 	if (!encoder)
271 		return -ENOMEM;
272 
273 	connector = tfp410_connector_create(dev, tfp410_mod, encoder);
274 	if (!connector)
275 		return -ENOMEM;
276 
277 	priv->encoders[priv->num_encoders++] = encoder;
278 	priv->connectors[priv->num_connectors++] = connector;
279 
280 	tilcdc_crtc_set_panel_info(priv->crtc, &dvi_info);
281 	return 0;
282 }
283 
284 static const struct tilcdc_module_ops tfp410_module_ops = {
285 		.modeset_init = tfp410_modeset_init,
286 };
287 
288 /*
289  * Device:
290  */
291 
292 static struct of_device_id tfp410_of_match[];
293 
tfp410_probe(struct platform_device * pdev)294 static int tfp410_probe(struct platform_device *pdev)
295 {
296 	struct device_node *node = pdev->dev.of_node;
297 	struct device_node *i2c_node;
298 	struct tfp410_module *tfp410_mod;
299 	struct tilcdc_module *mod;
300 	struct pinctrl *pinctrl;
301 	uint32_t i2c_phandle;
302 	int ret = -EINVAL;
303 
304 	/* bail out early if no DT data: */
305 	if (!node) {
306 		dev_err(&pdev->dev, "device-tree data is missing\n");
307 		return -ENXIO;
308 	}
309 
310 	tfp410_mod = devm_kzalloc(&pdev->dev, sizeof(*tfp410_mod), GFP_KERNEL);
311 	if (!tfp410_mod)
312 		return -ENOMEM;
313 
314 	mod = &tfp410_mod->base;
315 	pdev->dev.platform_data = mod;
316 
317 	tilcdc_module_init(mod, "tfp410", &tfp410_module_ops);
318 
319 	pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
320 	if (IS_ERR(pinctrl))
321 		dev_warn(&pdev->dev, "pins are not configured\n");
322 
323 	if (of_property_read_u32(node, "i2c", &i2c_phandle)) {
324 		dev_err(&pdev->dev, "could not get i2c bus phandle\n");
325 		goto fail;
326 	}
327 
328 	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");
337 		of_node_put(i2c_node);
338 		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);
345 	if (tfp410_mod->gpio < 0) {
346 		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");
351 			goto fail_adapter;
352 		}
353 	}
354 
355 	return 0;
356 
357 fail_adapter:
358 	i2c_put_adapter(tfp410_mod->i2c);
359 
360 fail:
361 	tilcdc_module_cleanup(mod);
362 	return ret;
363 }
364 
tfp410_remove(struct platform_device * pdev)365 static int tfp410_remove(struct platform_device *pdev)
366 {
367 	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);
374 
375 	return 0;
376 }
377 
378 static struct of_device_id tfp410_of_match[] = {
379 		{ .compatible = "ti,tilcdc,tfp410", },
380 		{ },
381 };
382 
383 struct 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 
tilcdc_tfp410_init(void)393 int __init tilcdc_tfp410_init(void)
394 {
395 	return platform_driver_register(&tfp410_driver);
396 }
397 
tilcdc_tfp410_fini(void)398 void __exit tilcdc_tfp410_fini(void)
399 {
400 	platform_driver_unregister(&tfp410_driver);
401 }
402