blob: c716c128ded26c94e5e55344468047cc84afc884 [file] [log] [blame]
Rob Clark0d4bbaf2012-12-18 17:34:16 -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/pinctrl/pinmux.h>
19#include <linux/pinctrl/consumer.h>
20#include <linux/backlight.h>
21#include <video/display_timing.h>
22#include <video/of_display_timing.h>
23#include <video/videomode.h>
24
25#include "tilcdc_drv.h"
26
27struct panel_module {
28 struct tilcdc_module base;
29 struct tilcdc_panel_info *info;
30 struct display_timings *timings;
31 struct backlight_device *backlight;
32};
33#define to_panel_module(x) container_of(x, struct panel_module, base)
34
35
36/*
37 * Encoder:
38 */
39
40struct panel_encoder {
41 struct drm_encoder base;
42 struct panel_module *mod;
43};
44#define to_panel_encoder(x) container_of(x, struct panel_encoder, base)
45
46
47static void panel_encoder_destroy(struct drm_encoder *encoder)
48{
49 struct panel_encoder *panel_encoder = to_panel_encoder(encoder);
50 drm_encoder_cleanup(encoder);
51 kfree(panel_encoder);
52}
53
54static void panel_encoder_dpms(struct drm_encoder *encoder, int mode)
55{
56 struct panel_encoder *panel_encoder = to_panel_encoder(encoder);
57 struct backlight_device *backlight = panel_encoder->mod->backlight;
58
59 if (!backlight)
60 return;
61
62 backlight->props.power = mode == DRM_MODE_DPMS_ON
63 ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
64 backlight_update_status(backlight);
65}
66
67static bool panel_encoder_mode_fixup(struct drm_encoder *encoder,
68 const struct drm_display_mode *mode,
69 struct drm_display_mode *adjusted_mode)
70{
71 /* nothing needed */
72 return true;
73}
74
75static void panel_encoder_prepare(struct drm_encoder *encoder)
76{
77 struct panel_encoder *panel_encoder = to_panel_encoder(encoder);
78 panel_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
79 tilcdc_crtc_set_panel_info(encoder->crtc, panel_encoder->mod->info);
80}
81
82static void panel_encoder_commit(struct drm_encoder *encoder)
83{
84 panel_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
85}
86
87static void panel_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
94static const struct drm_encoder_funcs panel_encoder_funcs = {
95 .destroy = panel_encoder_destroy,
96};
97
98static const struct drm_encoder_helper_funcs panel_encoder_helper_funcs = {
99 .dpms = panel_encoder_dpms,
100 .mode_fixup = panel_encoder_mode_fixup,
101 .prepare = panel_encoder_prepare,
102 .commit = panel_encoder_commit,
103 .mode_set = panel_encoder_mode_set,
104};
105
106static struct drm_encoder *panel_encoder_create(struct drm_device *dev,
107 struct panel_module *mod)
108{
109 struct panel_encoder *panel_encoder;
110 struct drm_encoder *encoder;
111 int ret;
112
113 panel_encoder = kzalloc(sizeof(*panel_encoder), GFP_KERNEL);
114 if (!panel_encoder) {
115 dev_err(dev->dev, "allocation failed\n");
116 return NULL;
117 }
118
119 panel_encoder->mod = mod;
120
121 encoder = &panel_encoder->base;
122 encoder->possible_crtcs = 1;
123
124 ret = drm_encoder_init(dev, encoder, &panel_encoder_funcs,
125 DRM_MODE_ENCODER_LVDS);
126 if (ret < 0)
127 goto fail;
128
129 drm_encoder_helper_add(encoder, &panel_encoder_helper_funcs);
130
131 return encoder;
132
133fail:
134 panel_encoder_destroy(encoder);
135 return NULL;
136}
137
138/*
139 * Connector:
140 */
141
142struct panel_connector {
143 struct drm_connector base;
144
145 struct drm_encoder *encoder; /* our connected encoder */
146 struct panel_module *mod;
147};
148#define to_panel_connector(x) container_of(x, struct panel_connector, base)
149
150
151static void panel_connector_destroy(struct drm_connector *connector)
152{
153 struct panel_connector *panel_connector = to_panel_connector(connector);
Sachin Kamat62eb3e22014-07-09 17:12:57 +0530154 drm_connector_unregister(connector);
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600155 drm_connector_cleanup(connector);
156 kfree(panel_connector);
157}
158
159static enum drm_connector_status panel_connector_detect(
160 struct drm_connector *connector,
161 bool force)
162{
163 return connector_status_connected;
164}
165
166static int panel_connector_get_modes(struct drm_connector *connector)
167{
168 struct drm_device *dev = connector->dev;
169 struct panel_connector *panel_connector = to_panel_connector(connector);
170 struct display_timings *timings = panel_connector->mod->timings;
171 int i;
172
173 for (i = 0; i < timings->num_timings; i++) {
174 struct drm_display_mode *mode = drm_mode_create(dev);
175 struct videomode vm;
176
Tomi Valkeinen6cd2c7d2013-03-21 14:20:12 +0200177 if (videomode_from_timings(timings, &vm, i))
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600178 break;
179
180 drm_display_mode_from_videomode(&vm, mode);
181
182 mode->type = DRM_MODE_TYPE_DRIVER;
183
184 if (timings->native_mode == i)
185 mode->type |= DRM_MODE_TYPE_PREFERRED;
186
187 drm_mode_set_name(mode);
188 drm_mode_probed_add(connector, mode);
189 }
190
191 return i;
192}
193
194static int panel_connector_mode_valid(struct drm_connector *connector,
195 struct drm_display_mode *mode)
196{
197 struct tilcdc_drm_private *priv = connector->dev->dev_private;
198 /* our only constraints are what the crtc can generate: */
199 return tilcdc_crtc_mode_valid(priv->crtc, mode);
200}
201
202static struct drm_encoder *panel_connector_best_encoder(
203 struct drm_connector *connector)
204{
205 struct panel_connector *panel_connector = to_panel_connector(connector);
206 return panel_connector->encoder;
207}
208
209static const struct drm_connector_funcs panel_connector_funcs = {
210 .destroy = panel_connector_destroy,
211 .dpms = drm_helper_connector_dpms,
212 .detect = panel_connector_detect,
213 .fill_modes = drm_helper_probe_single_connector_modes,
214};
215
216static const struct drm_connector_helper_funcs panel_connector_helper_funcs = {
217 .get_modes = panel_connector_get_modes,
218 .mode_valid = panel_connector_mode_valid,
219 .best_encoder = panel_connector_best_encoder,
220};
221
222static struct drm_connector *panel_connector_create(struct drm_device *dev,
223 struct panel_module *mod, struct drm_encoder *encoder)
224{
225 struct panel_connector *panel_connector;
226 struct drm_connector *connector;
227 int ret;
228
229 panel_connector = kzalloc(sizeof(*panel_connector), GFP_KERNEL);
230 if (!panel_connector) {
231 dev_err(dev->dev, "allocation failed\n");
232 return NULL;
233 }
234
235 panel_connector->encoder = encoder;
236 panel_connector->mod = mod;
237
238 connector = &panel_connector->base;
239
240 drm_connector_init(dev, connector, &panel_connector_funcs,
241 DRM_MODE_CONNECTOR_LVDS);
242 drm_connector_helper_add(connector, &panel_connector_helper_funcs);
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
Thomas Wood34ea3d32014-05-29 16:57:41 +0100251 drm_connector_register(connector);
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600252
253 return connector;
254
255fail:
256 panel_connector_destroy(connector);
257 return NULL;
258}
259
260/*
261 * Module:
262 */
263
264static int panel_modeset_init(struct tilcdc_module *mod, struct drm_device *dev)
265{
266 struct panel_module *panel_mod = to_panel_module(mod);
267 struct tilcdc_drm_private *priv = dev->dev_private;
268 struct drm_encoder *encoder;
269 struct drm_connector *connector;
270
271 encoder = panel_encoder_create(dev, panel_mod);
272 if (!encoder)
273 return -ENOMEM;
274
275 connector = panel_connector_create(dev, panel_mod, encoder);
276 if (!connector)
277 return -ENOMEM;
278
279 priv->encoders[priv->num_encoders++] = encoder;
280 priv->connectors[priv->num_connectors++] = connector;
281
282 return 0;
283}
284
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600285static const struct tilcdc_module_ops panel_module_ops = {
286 .modeset_init = panel_modeset_init,
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600287};
288
289/*
290 * Device:
291 */
292
293/* maybe move this somewhere common if it is needed by other outputs? */
Sachin Kamat1733d012013-03-02 15:53:09 +0530294static struct tilcdc_panel_info *of_get_panel_info(struct device_node *np)
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600295{
296 struct device_node *info_np;
297 struct tilcdc_panel_info *info;
298 int ret = 0;
299
300 if (!np) {
301 pr_err("%s: no devicenode given\n", __func__);
302 return NULL;
303 }
304
305 info_np = of_get_child_by_name(np, "panel-info");
306 if (!info_np) {
307 pr_err("%s: could not find panel-info node\n", __func__);
308 return NULL;
309 }
310
311 info = kzalloc(sizeof(*info), GFP_KERNEL);
312 if (!info) {
313 pr_err("%s: allocation failed\n", __func__);
Ezequiel Garcia9430dfa2014-09-02 09:51:16 -0300314 of_node_put(info_np);
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600315 return NULL;
316 }
317
318 ret |= of_property_read_u32(info_np, "ac-bias", &info->ac_bias);
319 ret |= of_property_read_u32(info_np, "ac-bias-intrpt", &info->ac_bias_intrpt);
320 ret |= of_property_read_u32(info_np, "dma-burst-sz", &info->dma_burst_sz);
321 ret |= of_property_read_u32(info_np, "bpp", &info->bpp);
322 ret |= of_property_read_u32(info_np, "fdd", &info->fdd);
323 ret |= of_property_read_u32(info_np, "sync-edge", &info->sync_edge);
324 ret |= of_property_read_u32(info_np, "sync-ctrl", &info->sync_ctrl);
325 ret |= of_property_read_u32(info_np, "raster-order", &info->raster_order);
326 ret |= of_property_read_u32(info_np, "fifo-th", &info->fifo_th);
327
328 /* optional: */
329 info->tft_alt_mode = of_property_read_bool(info_np, "tft-alt-mode");
330 info->invert_pxl_clk = of_property_read_bool(info_np, "invert-pxl-clk");
331
332 if (ret) {
333 pr_err("%s: error reading panel-info properties\n", __func__);
334 kfree(info);
Ezequiel Garcia9430dfa2014-09-02 09:51:16 -0300335 of_node_put(info_np);
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600336 return NULL;
337 }
Ezequiel Garcia9430dfa2014-09-02 09:51:16 -0300338 of_node_put(info_np);
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600339
340 return info;
341}
342
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600343static int panel_probe(struct platform_device *pdev)
344{
345 struct device_node *node = pdev->dev.of_node;
346 struct panel_module *panel_mod;
347 struct tilcdc_module *mod;
348 struct pinctrl *pinctrl;
349 int ret = -EINVAL;
350
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600351 /* bail out early if no DT data: */
352 if (!node) {
353 dev_err(&pdev->dev, "device-tree data is missing\n");
354 return -ENXIO;
355 }
356
Ezequiel Garcia971645d2014-09-02 09:51:19 -0300357 panel_mod = devm_kzalloc(&pdev->dev, sizeof(*panel_mod), GFP_KERNEL);
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600358 if (!panel_mod)
359 return -ENOMEM;
360
361 mod = &panel_mod->base;
Guido Martínez7cdcce92014-06-17 11:17:10 -0300362 pdev->dev.platform_data = mod;
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600363
364 tilcdc_module_init(mod, "panel", &panel_module_ops);
365
366 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
367 if (IS_ERR(pinctrl))
368 dev_warn(&pdev->dev, "pins are not configured\n");
369
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600370 panel_mod->timings = of_get_display_timings(node);
371 if (!panel_mod->timings) {
372 dev_err(&pdev->dev, "could not get panel timings\n");
Guido Martínez7cdcce92014-06-17 11:17:10 -0300373 goto fail_free;
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600374 }
375
376 panel_mod->info = of_get_panel_info(node);
377 if (!panel_mod->info) {
378 dev_err(&pdev->dev, "could not get panel info\n");
Guido Martínez7cdcce92014-06-17 11:17:10 -0300379 goto fail_timings;
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600380 }
381
Benoit Parrotdc28aa02013-06-18 17:18:31 -0500382 mod->preferred_bpp = panel_mod->info->bpp;
383
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600384 panel_mod->backlight = of_find_backlight_by_node(node);
385 if (panel_mod->backlight)
386 dev_info(&pdev->dev, "found backlight\n");
387
388 return 0;
389
Guido Martínez7cdcce92014-06-17 11:17:10 -0300390fail_timings:
391 display_timings_release(panel_mod->timings);
392
393fail_free:
Guido Martínez7cdcce92014-06-17 11:17:10 -0300394 tilcdc_module_cleanup(mod);
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600395 return ret;
396}
397
398static int panel_remove(struct platform_device *pdev)
399{
Guido Martínez7cdcce92014-06-17 11:17:10 -0300400 struct tilcdc_module *mod = dev_get_platdata(&pdev->dev);
401 struct panel_module *panel_mod = to_panel_module(mod);
402
403 display_timings_release(panel_mod->timings);
404
405 tilcdc_module_cleanup(mod);
406 kfree(panel_mod->info);
Guido Martínez7cdcce92014-06-17 11:17:10 -0300407
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600408 return 0;
409}
410
411static struct of_device_id panel_of_match[] = {
412 { .compatible = "ti,tilcdc,panel", },
413 { },
414};
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600415
416struct platform_driver panel_driver = {
417 .probe = panel_probe,
418 .remove = panel_remove,
419 .driver = {
420 .owner = THIS_MODULE,
421 .name = "panel",
422 .of_match_table = panel_of_match,
423 },
424};
425
426int __init tilcdc_panel_init(void)
427{
428 return platform_driver_register(&panel_driver);
429}
430
431void __exit tilcdc_panel_fini(void)
432{
433 platform_driver_unregister(&panel_driver);
434}