blob: 7a0315855e90130d5eaa2f00e76750e59a68853f [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>
Ezequiel Garciad898ce02014-09-02 09:51:22 -030021#include <linux/gpio/consumer.h>
Rob Clark0d4bbaf2012-12-18 17:34:16 -060022#include <video/display_timing.h>
23#include <video/of_display_timing.h>
24#include <video/videomode.h>
25
26#include "tilcdc_drv.h"
27
28struct panel_module {
29 struct tilcdc_module base;
30 struct tilcdc_panel_info *info;
31 struct display_timings *timings;
32 struct backlight_device *backlight;
Ezequiel Garciad898ce02014-09-02 09:51:22 -030033 struct gpio_desc *enable_gpio;
Rob Clark0d4bbaf2012-12-18 17:34:16 -060034};
35#define to_panel_module(x) container_of(x, struct panel_module, base)
36
37
38/*
39 * Encoder:
40 */
41
42struct panel_encoder {
43 struct drm_encoder base;
44 struct panel_module *mod;
45};
46#define to_panel_encoder(x) container_of(x, struct panel_encoder, base)
47
48
49static void panel_encoder_destroy(struct drm_encoder *encoder)
50{
51 struct panel_encoder *panel_encoder = to_panel_encoder(encoder);
52 drm_encoder_cleanup(encoder);
53 kfree(panel_encoder);
54}
55
56static void panel_encoder_dpms(struct drm_encoder *encoder, int mode)
57{
58 struct panel_encoder *panel_encoder = to_panel_encoder(encoder);
59 struct backlight_device *backlight = panel_encoder->mod->backlight;
Ezequiel Garciad898ce02014-09-02 09:51:22 -030060 struct gpio_desc *gpio = panel_encoder->mod->enable_gpio;
Rob Clark0d4bbaf2012-12-18 17:34:16 -060061
Ezequiel Garciad898ce02014-09-02 09:51:22 -030062 if (backlight) {
63 backlight->props.power = mode == DRM_MODE_DPMS_ON ?
64 FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
65 backlight_update_status(backlight);
66 }
Rob Clark0d4bbaf2012-12-18 17:34:16 -060067
Ezequiel Garciad898ce02014-09-02 09:51:22 -030068 if (gpio)
69 gpiod_set_value_cansleep(gpio,
70 mode == DRM_MODE_DPMS_ON ? 1 : 0);
Rob Clark0d4bbaf2012-12-18 17:34:16 -060071}
72
73static bool panel_encoder_mode_fixup(struct drm_encoder *encoder,
74 const struct drm_display_mode *mode,
75 struct drm_display_mode *adjusted_mode)
76{
77 /* nothing needed */
78 return true;
79}
80
81static void panel_encoder_prepare(struct drm_encoder *encoder)
82{
83 struct panel_encoder *panel_encoder = to_panel_encoder(encoder);
84 panel_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
85 tilcdc_crtc_set_panel_info(encoder->crtc, panel_encoder->mod->info);
86}
87
88static void panel_encoder_commit(struct drm_encoder *encoder)
89{
90 panel_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
91}
92
93static void panel_encoder_mode_set(struct drm_encoder *encoder,
94 struct drm_display_mode *mode,
95 struct drm_display_mode *adjusted_mode)
96{
97 /* nothing needed */
98}
99
100static const struct drm_encoder_funcs panel_encoder_funcs = {
101 .destroy = panel_encoder_destroy,
102};
103
104static const struct drm_encoder_helper_funcs panel_encoder_helper_funcs = {
105 .dpms = panel_encoder_dpms,
106 .mode_fixup = panel_encoder_mode_fixup,
107 .prepare = panel_encoder_prepare,
108 .commit = panel_encoder_commit,
109 .mode_set = panel_encoder_mode_set,
110};
111
112static struct drm_encoder *panel_encoder_create(struct drm_device *dev,
113 struct panel_module *mod)
114{
115 struct panel_encoder *panel_encoder;
116 struct drm_encoder *encoder;
117 int ret;
118
119 panel_encoder = kzalloc(sizeof(*panel_encoder), GFP_KERNEL);
120 if (!panel_encoder) {
121 dev_err(dev->dev, "allocation failed\n");
122 return NULL;
123 }
124
125 panel_encoder->mod = mod;
126
127 encoder = &panel_encoder->base;
128 encoder->possible_crtcs = 1;
129
130 ret = drm_encoder_init(dev, encoder, &panel_encoder_funcs,
131 DRM_MODE_ENCODER_LVDS);
132 if (ret < 0)
133 goto fail;
134
135 drm_encoder_helper_add(encoder, &panel_encoder_helper_funcs);
136
137 return encoder;
138
139fail:
140 panel_encoder_destroy(encoder);
141 return NULL;
142}
143
144/*
145 * Connector:
146 */
147
148struct panel_connector {
149 struct drm_connector base;
150
151 struct drm_encoder *encoder; /* our connected encoder */
152 struct panel_module *mod;
153};
154#define to_panel_connector(x) container_of(x, struct panel_connector, base)
155
156
157static void panel_connector_destroy(struct drm_connector *connector)
158{
159 struct panel_connector *panel_connector = to_panel_connector(connector);
Sachin Kamat62eb3e22014-07-09 17:12:57 +0530160 drm_connector_unregister(connector);
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600161 drm_connector_cleanup(connector);
162 kfree(panel_connector);
163}
164
165static enum drm_connector_status panel_connector_detect(
166 struct drm_connector *connector,
167 bool force)
168{
169 return connector_status_connected;
170}
171
172static int panel_connector_get_modes(struct drm_connector *connector)
173{
174 struct drm_device *dev = connector->dev;
175 struct panel_connector *panel_connector = to_panel_connector(connector);
176 struct display_timings *timings = panel_connector->mod->timings;
177 int i;
178
179 for (i = 0; i < timings->num_timings; i++) {
180 struct drm_display_mode *mode = drm_mode_create(dev);
181 struct videomode vm;
182
Tomi Valkeinen6cd2c7d2013-03-21 14:20:12 +0200183 if (videomode_from_timings(timings, &vm, i))
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600184 break;
185
186 drm_display_mode_from_videomode(&vm, mode);
187
188 mode->type = DRM_MODE_TYPE_DRIVER;
189
190 if (timings->native_mode == i)
191 mode->type |= DRM_MODE_TYPE_PREFERRED;
192
193 drm_mode_set_name(mode);
194 drm_mode_probed_add(connector, mode);
195 }
196
197 return i;
198}
199
200static int panel_connector_mode_valid(struct drm_connector *connector,
201 struct drm_display_mode *mode)
202{
203 struct tilcdc_drm_private *priv = connector->dev->dev_private;
204 /* our only constraints are what the crtc can generate: */
205 return tilcdc_crtc_mode_valid(priv->crtc, mode);
206}
207
208static struct drm_encoder *panel_connector_best_encoder(
209 struct drm_connector *connector)
210{
211 struct panel_connector *panel_connector = to_panel_connector(connector);
212 return panel_connector->encoder;
213}
214
215static const struct drm_connector_funcs panel_connector_funcs = {
216 .destroy = panel_connector_destroy,
217 .dpms = drm_helper_connector_dpms,
218 .detect = panel_connector_detect,
219 .fill_modes = drm_helper_probe_single_connector_modes,
220};
221
222static const struct drm_connector_helper_funcs panel_connector_helper_funcs = {
223 .get_modes = panel_connector_get_modes,
224 .mode_valid = panel_connector_mode_valid,
225 .best_encoder = panel_connector_best_encoder,
226};
227
228static struct drm_connector *panel_connector_create(struct drm_device *dev,
229 struct panel_module *mod, struct drm_encoder *encoder)
230{
231 struct panel_connector *panel_connector;
232 struct drm_connector *connector;
233 int ret;
234
235 panel_connector = kzalloc(sizeof(*panel_connector), GFP_KERNEL);
236 if (!panel_connector) {
237 dev_err(dev->dev, "allocation failed\n");
238 return NULL;
239 }
240
241 panel_connector->encoder = encoder;
242 panel_connector->mod = mod;
243
244 connector = &panel_connector->base;
245
246 drm_connector_init(dev, connector, &panel_connector_funcs,
247 DRM_MODE_CONNECTOR_LVDS);
248 drm_connector_helper_add(connector, &panel_connector_helper_funcs);
249
250 connector->interlace_allowed = 0;
251 connector->doublescan_allowed = 0;
252
253 ret = drm_mode_connector_attach_encoder(connector, encoder);
254 if (ret)
255 goto fail;
256
Thomas Wood34ea3d32014-05-29 16:57:41 +0100257 drm_connector_register(connector);
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600258
259 return connector;
260
261fail:
262 panel_connector_destroy(connector);
263 return NULL;
264}
265
266/*
267 * Module:
268 */
269
270static int panel_modeset_init(struct tilcdc_module *mod, struct drm_device *dev)
271{
272 struct panel_module *panel_mod = to_panel_module(mod);
273 struct tilcdc_drm_private *priv = dev->dev_private;
274 struct drm_encoder *encoder;
275 struct drm_connector *connector;
276
277 encoder = panel_encoder_create(dev, panel_mod);
278 if (!encoder)
279 return -ENOMEM;
280
281 connector = panel_connector_create(dev, panel_mod, encoder);
282 if (!connector)
283 return -ENOMEM;
284
285 priv->encoders[priv->num_encoders++] = encoder;
286 priv->connectors[priv->num_connectors++] = connector;
287
288 return 0;
289}
290
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600291static const struct tilcdc_module_ops panel_module_ops = {
292 .modeset_init = panel_modeset_init,
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600293};
294
295/*
296 * Device:
297 */
298
299/* maybe move this somewhere common if it is needed by other outputs? */
Sachin Kamat1733d012013-03-02 15:53:09 +0530300static struct tilcdc_panel_info *of_get_panel_info(struct device_node *np)
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600301{
302 struct device_node *info_np;
303 struct tilcdc_panel_info *info;
304 int ret = 0;
305
306 if (!np) {
307 pr_err("%s: no devicenode given\n", __func__);
308 return NULL;
309 }
310
311 info_np = of_get_child_by_name(np, "panel-info");
312 if (!info_np) {
313 pr_err("%s: could not find panel-info node\n", __func__);
314 return NULL;
315 }
316
317 info = kzalloc(sizeof(*info), GFP_KERNEL);
318 if (!info) {
319 pr_err("%s: allocation failed\n", __func__);
Ezequiel Garcia9430dfa2014-09-02 09:51:16 -0300320 of_node_put(info_np);
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600321 return NULL;
322 }
323
324 ret |= of_property_read_u32(info_np, "ac-bias", &info->ac_bias);
325 ret |= of_property_read_u32(info_np, "ac-bias-intrpt", &info->ac_bias_intrpt);
326 ret |= of_property_read_u32(info_np, "dma-burst-sz", &info->dma_burst_sz);
327 ret |= of_property_read_u32(info_np, "bpp", &info->bpp);
328 ret |= of_property_read_u32(info_np, "fdd", &info->fdd);
329 ret |= of_property_read_u32(info_np, "sync-edge", &info->sync_edge);
330 ret |= of_property_read_u32(info_np, "sync-ctrl", &info->sync_ctrl);
331 ret |= of_property_read_u32(info_np, "raster-order", &info->raster_order);
332 ret |= of_property_read_u32(info_np, "fifo-th", &info->fifo_th);
333
334 /* optional: */
335 info->tft_alt_mode = of_property_read_bool(info_np, "tft-alt-mode");
336 info->invert_pxl_clk = of_property_read_bool(info_np, "invert-pxl-clk");
337
338 if (ret) {
339 pr_err("%s: error reading panel-info properties\n", __func__);
340 kfree(info);
Ezequiel Garcia9430dfa2014-09-02 09:51:16 -0300341 of_node_put(info_np);
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600342 return NULL;
343 }
Ezequiel Garcia9430dfa2014-09-02 09:51:16 -0300344 of_node_put(info_np);
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600345
346 return info;
347}
348
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600349static int panel_probe(struct platform_device *pdev)
350{
Ezequiel Garcia18c44db2014-09-02 09:51:20 -0300351 struct device_node *bl_node, *node = pdev->dev.of_node;
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600352 struct panel_module *panel_mod;
353 struct tilcdc_module *mod;
354 struct pinctrl *pinctrl;
Ezequiel Garcia12778fc2014-09-02 09:51:21 -0300355 int ret;
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600356
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600357 /* bail out early if no DT data: */
358 if (!node) {
359 dev_err(&pdev->dev, "device-tree data is missing\n");
360 return -ENXIO;
361 }
362
Ezequiel Garcia971645d2014-09-02 09:51:19 -0300363 panel_mod = devm_kzalloc(&pdev->dev, sizeof(*panel_mod), GFP_KERNEL);
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600364 if (!panel_mod)
365 return -ENOMEM;
366
Ezequiel Garcia18c44db2014-09-02 09:51:20 -0300367 bl_node = of_parse_phandle(node, "backlight", 0);
368 if (bl_node) {
369 panel_mod->backlight = of_find_backlight_by_node(bl_node);
370 of_node_put(bl_node);
371
372 if (!panel_mod->backlight)
373 return -EPROBE_DEFER;
374
375 dev_info(&pdev->dev, "found backlight\n");
376 }
377
Ezequiel Garciad898ce02014-09-02 09:51:22 -0300378 panel_mod->enable_gpio = devm_gpiod_get(&pdev->dev, "enable");
379 if (IS_ERR(panel_mod->enable_gpio)) {
380 ret = PTR_ERR(panel_mod->enable_gpio);
381 if (ret != -ENOENT) {
382 dev_err(&pdev->dev, "failed to request enable GPIO\n");
383 goto fail_backlight;
384 }
385
386 /* Optional GPIO is not here, continue silently. */
387 panel_mod->enable_gpio = NULL;
388 } else {
389 ret = gpiod_direction_output(panel_mod->enable_gpio, 0);
390 if (ret < 0) {
391 dev_err(&pdev->dev, "failed to setup GPIO\n");
392 goto fail_backlight;
393 }
394 dev_info(&pdev->dev, "found enable GPIO\n");
395 }
396
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600397 mod = &panel_mod->base;
Guido Martínez7cdcce92014-06-17 11:17:10 -0300398 pdev->dev.platform_data = mod;
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600399
400 tilcdc_module_init(mod, "panel", &panel_module_ops);
401
402 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
403 if (IS_ERR(pinctrl))
404 dev_warn(&pdev->dev, "pins are not configured\n");
405
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600406 panel_mod->timings = of_get_display_timings(node);
407 if (!panel_mod->timings) {
408 dev_err(&pdev->dev, "could not get panel timings\n");
Ezequiel Garcia12778fc2014-09-02 09:51:21 -0300409 ret = -EINVAL;
Guido Martínez7cdcce92014-06-17 11:17:10 -0300410 goto fail_free;
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600411 }
412
413 panel_mod->info = of_get_panel_info(node);
414 if (!panel_mod->info) {
415 dev_err(&pdev->dev, "could not get panel info\n");
Ezequiel Garcia12778fc2014-09-02 09:51:21 -0300416 ret = -EINVAL;
Guido Martínez7cdcce92014-06-17 11:17:10 -0300417 goto fail_timings;
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600418 }
419
Benoit Parrotdc28aa02013-06-18 17:18:31 -0500420 mod->preferred_bpp = panel_mod->info->bpp;
421
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600422 return 0;
423
Guido Martínez7cdcce92014-06-17 11:17:10 -0300424fail_timings:
425 display_timings_release(panel_mod->timings);
426
427fail_free:
Guido Martínez7cdcce92014-06-17 11:17:10 -0300428 tilcdc_module_cleanup(mod);
Ezequiel Garciad898ce02014-09-02 09:51:22 -0300429
430fail_backlight:
Ezequiel Garcia18c44db2014-09-02 09:51:20 -0300431 if (panel_mod->backlight)
432 put_device(&panel_mod->backlight->dev);
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600433 return ret;
434}
435
436static int panel_remove(struct platform_device *pdev)
437{
Guido Martínez7cdcce92014-06-17 11:17:10 -0300438 struct tilcdc_module *mod = dev_get_platdata(&pdev->dev);
439 struct panel_module *panel_mod = to_panel_module(mod);
Ezequiel Garcia18c44db2014-09-02 09:51:20 -0300440 struct backlight_device *backlight = panel_mod->backlight;
441
442 if (backlight)
443 put_device(&backlight->dev);
Guido Martínez7cdcce92014-06-17 11:17:10 -0300444
445 display_timings_release(panel_mod->timings);
446
447 tilcdc_module_cleanup(mod);
448 kfree(panel_mod->info);
Guido Martínez7cdcce92014-06-17 11:17:10 -0300449
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600450 return 0;
451}
452
453static struct of_device_id panel_of_match[] = {
454 { .compatible = "ti,tilcdc,panel", },
455 { },
456};
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600457
458struct platform_driver panel_driver = {
459 .probe = panel_probe,
460 .remove = panel_remove,
461 .driver = {
462 .owner = THIS_MODULE,
463 .name = "panel",
464 .of_match_table = panel_of_match,
465 },
466};
467
468int __init tilcdc_panel_init(void)
469{
470 return platform_driver_register(&panel_driver);
471}
472
473void __exit tilcdc_panel_fini(void)
474{
475 platform_driver_unregister(&panel_driver);
476}