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/pinctrl/pinmux.h>
19 #include <linux/pinctrl/consumer.h>
20 #include <linux/backlight.h>
21 #include <linux/gpio/consumer.h>
22 #include <video/display_timing.h>
23 #include <video/of_display_timing.h>
24 #include <video/videomode.h>
26 #include "tilcdc_drv.h"
29 struct tilcdc_module base
;
30 struct tilcdc_panel_info
*info
;
31 struct display_timings
*timings
;
32 struct backlight_device
*backlight
;
33 struct gpio_desc
*enable_gpio
;
35 #define to_panel_module(x) container_of(x, struct panel_module, base)
42 struct panel_encoder
{
43 struct drm_encoder base
;
44 struct panel_module
*mod
;
46 #define to_panel_encoder(x) container_of(x, struct panel_encoder, base)
49 static void panel_encoder_destroy(struct drm_encoder
*encoder
)
51 struct panel_encoder
*panel_encoder
= to_panel_encoder(encoder
);
52 drm_encoder_cleanup(encoder
);
56 static void panel_encoder_dpms(struct drm_encoder
*encoder
, int mode
)
58 struct panel_encoder
*panel_encoder
= to_panel_encoder(encoder
);
59 struct backlight_device
*backlight
= panel_encoder
->mod
->backlight
;
60 struct gpio_desc
*gpio
= panel_encoder
->mod
->enable_gpio
;
63 backlight
->props
.power
= mode
== DRM_MODE_DPMS_ON
?
64 FB_BLANK_UNBLANK
: FB_BLANK_POWERDOWN
;
65 backlight_update_status(backlight
);
69 gpiod_set_value_cansleep(gpio
,
70 mode
== DRM_MODE_DPMS_ON
? 1 : 0);
73 static bool panel_encoder_mode_fixup(struct drm_encoder
*encoder
,
74 const struct drm_display_mode
*mode
,
75 struct drm_display_mode
*adjusted_mode
)
81 static void panel_encoder_prepare(struct drm_encoder
*encoder
)
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
);
88 static void panel_encoder_commit(struct drm_encoder
*encoder
)
90 panel_encoder_dpms(encoder
, DRM_MODE_DPMS_ON
);
93 static void panel_encoder_mode_set(struct drm_encoder
*encoder
,
94 struct drm_display_mode
*mode
,
95 struct drm_display_mode
*adjusted_mode
)
100 static const struct drm_encoder_funcs panel_encoder_funcs
= {
101 .destroy
= panel_encoder_destroy
,
104 static 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
,
112 static struct drm_encoder
*panel_encoder_create(struct drm_device
*dev
,
113 struct panel_module
*mod
)
115 struct panel_encoder
*panel_encoder
;
116 struct drm_encoder
*encoder
;
119 panel_encoder
= kzalloc(sizeof(*panel_encoder
), GFP_KERNEL
);
120 if (!panel_encoder
) {
121 dev_err(dev
->dev
, "allocation failed\n");
125 panel_encoder
->mod
= mod
;
127 encoder
= &panel_encoder
->base
;
128 encoder
->possible_crtcs
= 1;
130 ret
= drm_encoder_init(dev
, encoder
, &panel_encoder_funcs
,
131 DRM_MODE_ENCODER_LVDS
);
135 drm_encoder_helper_add(encoder
, &panel_encoder_helper_funcs
);
140 panel_encoder_destroy(encoder
);
148 struct panel_connector
{
149 struct drm_connector base
;
151 struct drm_encoder
*encoder
; /* our connected encoder */
152 struct panel_module
*mod
;
154 #define to_panel_connector(x) container_of(x, struct panel_connector, base)
157 static void panel_connector_destroy(struct drm_connector
*connector
)
159 struct panel_connector
*panel_connector
= to_panel_connector(connector
);
160 drm_connector_unregister(connector
);
161 drm_connector_cleanup(connector
);
162 kfree(panel_connector
);
165 static enum drm_connector_status
panel_connector_detect(
166 struct drm_connector
*connector
,
169 return connector_status_connected
;
172 static int panel_connector_get_modes(struct drm_connector
*connector
)
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
;
179 for (i
= 0; i
< timings
->num_timings
; i
++) {
180 struct drm_display_mode
*mode
= drm_mode_create(dev
);
183 if (videomode_from_timings(timings
, &vm
, i
))
186 drm_display_mode_from_videomode(&vm
, mode
);
188 mode
->type
= DRM_MODE_TYPE_DRIVER
;
190 if (timings
->native_mode
== i
)
191 mode
->type
|= DRM_MODE_TYPE_PREFERRED
;
193 drm_mode_set_name(mode
);
194 drm_mode_probed_add(connector
, mode
);
200 static int panel_connector_mode_valid(struct drm_connector
*connector
,
201 struct drm_display_mode
*mode
)
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
);
208 static struct drm_encoder
*panel_connector_best_encoder(
209 struct drm_connector
*connector
)
211 struct panel_connector
*panel_connector
= to_panel_connector(connector
);
212 return panel_connector
->encoder
;
215 static 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
,
222 static 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
,
228 static struct drm_connector
*panel_connector_create(struct drm_device
*dev
,
229 struct panel_module
*mod
, struct drm_encoder
*encoder
)
231 struct panel_connector
*panel_connector
;
232 struct drm_connector
*connector
;
235 panel_connector
= kzalloc(sizeof(*panel_connector
), GFP_KERNEL
);
236 if (!panel_connector
) {
237 dev_err(dev
->dev
, "allocation failed\n");
241 panel_connector
->encoder
= encoder
;
242 panel_connector
->mod
= mod
;
244 connector
= &panel_connector
->base
;
246 drm_connector_init(dev
, connector
, &panel_connector_funcs
,
247 DRM_MODE_CONNECTOR_LVDS
);
248 drm_connector_helper_add(connector
, &panel_connector_helper_funcs
);
250 connector
->interlace_allowed
= 0;
251 connector
->doublescan_allowed
= 0;
253 ret
= drm_mode_connector_attach_encoder(connector
, encoder
);
257 drm_connector_register(connector
);
262 panel_connector_destroy(connector
);
270 static int panel_modeset_init(struct tilcdc_module
*mod
, struct drm_device
*dev
)
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
;
277 encoder
= panel_encoder_create(dev
, panel_mod
);
281 connector
= panel_connector_create(dev
, panel_mod
, encoder
);
285 priv
->encoders
[priv
->num_encoders
++] = encoder
;
286 priv
->connectors
[priv
->num_connectors
++] = connector
;
291 static const struct tilcdc_module_ops panel_module_ops
= {
292 .modeset_init
= panel_modeset_init
,
299 /* maybe move this somewhere common if it is needed by other outputs? */
300 static struct tilcdc_panel_info
*of_get_panel_info(struct device_node
*np
)
302 struct device_node
*info_np
;
303 struct tilcdc_panel_info
*info
;
307 pr_err("%s: no devicenode given\n", __func__
);
311 info_np
= of_get_child_by_name(np
, "panel-info");
313 pr_err("%s: could not find panel-info node\n", __func__
);
317 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
319 pr_err("%s: allocation failed\n", __func__
);
320 of_node_put(info_np
);
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
);
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");
339 pr_err("%s: error reading panel-info properties\n", __func__
);
341 of_node_put(info_np
);
344 of_node_put(info_np
);
349 static int panel_probe(struct platform_device
*pdev
)
351 struct device_node
*bl_node
, *node
= pdev
->dev
.of_node
;
352 struct panel_module
*panel_mod
;
353 struct tilcdc_module
*mod
;
354 struct pinctrl
*pinctrl
;
357 /* bail out early if no DT data: */
359 dev_err(&pdev
->dev
, "device-tree data is missing\n");
363 panel_mod
= devm_kzalloc(&pdev
->dev
, sizeof(*panel_mod
), GFP_KERNEL
);
367 bl_node
= of_parse_phandle(node
, "backlight", 0);
369 panel_mod
->backlight
= of_find_backlight_by_node(bl_node
);
370 of_node_put(bl_node
);
372 if (!panel_mod
->backlight
)
373 return -EPROBE_DEFER
;
375 dev_info(&pdev
->dev
, "found backlight\n");
378 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");
386 /* Optional GPIO is not here, continue silently. */
387 panel_mod
->enable_gpio
= NULL
;
389 ret
= gpiod_direction_output(panel_mod
->enable_gpio
, 0);
391 dev_err(&pdev
->dev
, "failed to setup GPIO\n");
394 dev_info(&pdev
->dev
, "found enable GPIO\n");
397 mod
= &panel_mod
->base
;
398 pdev
->dev
.platform_data
= mod
;
400 tilcdc_module_init(mod
, "panel", &panel_module_ops
);
402 pinctrl
= devm_pinctrl_get_select_default(&pdev
->dev
);
404 dev_warn(&pdev
->dev
, "pins are not configured\n");
406 panel_mod
->timings
= of_get_display_timings(node
);
407 if (!panel_mod
->timings
) {
408 dev_err(&pdev
->dev
, "could not get panel timings\n");
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");
420 mod
->preferred_bpp
= panel_mod
->info
->bpp
;
425 display_timings_release(panel_mod
->timings
);
428 tilcdc_module_cleanup(mod
);
431 if (panel_mod
->backlight
)
432 put_device(&panel_mod
->backlight
->dev
);
436 static int panel_remove(struct platform_device
*pdev
)
438 struct tilcdc_module
*mod
= dev_get_platdata(&pdev
->dev
);
439 struct panel_module
*panel_mod
= to_panel_module(mod
);
440 struct backlight_device
*backlight
= panel_mod
->backlight
;
443 put_device(&backlight
->dev
);
445 display_timings_release(panel_mod
->timings
);
447 tilcdc_module_cleanup(mod
);
448 kfree(panel_mod
->info
);
453 static struct of_device_id panel_of_match
[] = {
454 { .compatible
= "ti,tilcdc,panel", },
458 struct platform_driver panel_driver
= {
459 .probe
= panel_probe
,
460 .remove
= panel_remove
,
462 .owner
= THIS_MODULE
,
464 .of_match_table
= panel_of_match
,
468 int __init
tilcdc_panel_init(void)
470 return platform_driver_register(&panel_driver
);
473 void __exit
tilcdc_panel_fini(void)
475 platform_driver_unregister(&panel_driver
);