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>
25 #include <drm/drm_atomic_helper.h>
27 #include "tilcdc_drv.h"
28 #include "tilcdc_panel.h"
31 struct tilcdc_module base
;
32 struct tilcdc_panel_info
*info
;
33 struct display_timings
*timings
;
34 struct backlight_device
*backlight
;
35 struct gpio_desc
*enable_gpio
;
37 #define to_panel_module(x) container_of(x, struct panel_module, base)
44 struct panel_encoder
{
45 struct drm_encoder base
;
46 struct panel_module
*mod
;
48 #define to_panel_encoder(x) container_of(x, struct panel_encoder, base)
50 static void panel_encoder_dpms(struct drm_encoder
*encoder
, int mode
)
52 struct panel_encoder
*panel_encoder
= to_panel_encoder(encoder
);
53 struct backlight_device
*backlight
= panel_encoder
->mod
->backlight
;
54 struct gpio_desc
*gpio
= panel_encoder
->mod
->enable_gpio
;
57 backlight
->props
.power
= mode
== DRM_MODE_DPMS_ON
?
58 FB_BLANK_UNBLANK
: FB_BLANK_POWERDOWN
;
59 backlight_update_status(backlight
);
63 gpiod_set_value_cansleep(gpio
,
64 mode
== DRM_MODE_DPMS_ON
? 1 : 0);
67 static void panel_encoder_prepare(struct drm_encoder
*encoder
)
69 panel_encoder_dpms(encoder
, DRM_MODE_DPMS_OFF
);
72 static void panel_encoder_commit(struct drm_encoder
*encoder
)
74 panel_encoder_dpms(encoder
, DRM_MODE_DPMS_ON
);
77 static void panel_encoder_mode_set(struct drm_encoder
*encoder
,
78 struct drm_display_mode
*mode
,
79 struct drm_display_mode
*adjusted_mode
)
84 static const struct drm_encoder_funcs panel_encoder_funcs
= {
85 .destroy
= drm_encoder_cleanup
,
88 static const struct drm_encoder_helper_funcs panel_encoder_helper_funcs
= {
89 .dpms
= panel_encoder_dpms
,
90 .prepare
= panel_encoder_prepare
,
91 .commit
= panel_encoder_commit
,
92 .mode_set
= panel_encoder_mode_set
,
95 static struct drm_encoder
*panel_encoder_create(struct drm_device
*dev
,
96 struct panel_module
*mod
)
98 struct panel_encoder
*panel_encoder
;
99 struct drm_encoder
*encoder
;
102 panel_encoder
= devm_kzalloc(dev
->dev
, sizeof(*panel_encoder
),
104 if (!panel_encoder
) {
105 dev_err(dev
->dev
, "allocation failed\n");
109 panel_encoder
->mod
= mod
;
111 encoder
= &panel_encoder
->base
;
112 encoder
->possible_crtcs
= 1;
114 ret
= drm_encoder_init(dev
, encoder
, &panel_encoder_funcs
,
115 DRM_MODE_ENCODER_LVDS
, NULL
);
119 drm_encoder_helper_add(encoder
, &panel_encoder_helper_funcs
);
124 drm_encoder_cleanup(encoder
);
132 struct panel_connector
{
133 struct drm_connector base
;
135 struct drm_encoder
*encoder
; /* our connected encoder */
136 struct panel_module
*mod
;
138 #define to_panel_connector(x) container_of(x, struct panel_connector, base)
141 static void panel_connector_destroy(struct drm_connector
*connector
)
143 drm_connector_unregister(connector
);
144 drm_connector_cleanup(connector
);
147 static enum drm_connector_status
panel_connector_detect(
148 struct drm_connector
*connector
,
151 return connector_status_connected
;
154 static int panel_connector_get_modes(struct drm_connector
*connector
)
156 struct drm_device
*dev
= connector
->dev
;
157 struct panel_connector
*panel_connector
= to_panel_connector(connector
);
158 struct display_timings
*timings
= panel_connector
->mod
->timings
;
161 for (i
= 0; i
< timings
->num_timings
; i
++) {
162 struct drm_display_mode
*mode
;
165 if (videomode_from_timings(timings
, &vm
, i
))
168 mode
= drm_mode_create(dev
);
172 drm_display_mode_from_videomode(&vm
, mode
);
174 mode
->type
= DRM_MODE_TYPE_DRIVER
;
176 if (timings
->native_mode
== i
)
177 mode
->type
|= DRM_MODE_TYPE_PREFERRED
;
179 drm_mode_set_name(mode
);
180 drm_mode_probed_add(connector
, mode
);
186 static int panel_connector_mode_valid(struct drm_connector
*connector
,
187 struct drm_display_mode
*mode
)
189 struct tilcdc_drm_private
*priv
= connector
->dev
->dev_private
;
190 /* our only constraints are what the crtc can generate: */
191 return tilcdc_crtc_mode_valid(priv
->crtc
, mode
);
194 static struct drm_encoder
*panel_connector_best_encoder(
195 struct drm_connector
*connector
)
197 struct panel_connector
*panel_connector
= to_panel_connector(connector
);
198 return panel_connector
->encoder
;
201 static const struct drm_connector_funcs panel_connector_funcs
= {
202 .destroy
= panel_connector_destroy
,
203 .dpms
= drm_atomic_helper_connector_dpms
,
204 .detect
= panel_connector_detect
,
205 .fill_modes
= drm_helper_probe_single_connector_modes
,
206 .reset
= drm_atomic_helper_connector_reset
,
207 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
208 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
211 static const struct drm_connector_helper_funcs panel_connector_helper_funcs
= {
212 .get_modes
= panel_connector_get_modes
,
213 .mode_valid
= panel_connector_mode_valid
,
214 .best_encoder
= panel_connector_best_encoder
,
217 static struct drm_connector
*panel_connector_create(struct drm_device
*dev
,
218 struct panel_module
*mod
, struct drm_encoder
*encoder
)
220 struct panel_connector
*panel_connector
;
221 struct drm_connector
*connector
;
224 panel_connector
= devm_kzalloc(dev
->dev
, sizeof(*panel_connector
),
226 if (!panel_connector
) {
227 dev_err(dev
->dev
, "allocation failed\n");
231 panel_connector
->encoder
= encoder
;
232 panel_connector
->mod
= mod
;
234 connector
= &panel_connector
->base
;
236 drm_connector_init(dev
, connector
, &panel_connector_funcs
,
237 DRM_MODE_CONNECTOR_LVDS
);
238 drm_connector_helper_add(connector
, &panel_connector_helper_funcs
);
240 connector
->interlace_allowed
= 0;
241 connector
->doublescan_allowed
= 0;
243 ret
= drm_mode_connector_attach_encoder(connector
, encoder
);
247 drm_connector_register(connector
);
252 panel_connector_destroy(connector
);
260 static int panel_modeset_init(struct tilcdc_module
*mod
, struct drm_device
*dev
)
262 struct panel_module
*panel_mod
= to_panel_module(mod
);
263 struct tilcdc_drm_private
*priv
= dev
->dev_private
;
264 struct drm_encoder
*encoder
;
265 struct drm_connector
*connector
;
267 encoder
= panel_encoder_create(dev
, panel_mod
);
271 connector
= panel_connector_create(dev
, panel_mod
, encoder
);
275 priv
->encoders
[priv
->num_encoders
++] = encoder
;
276 priv
->connectors
[priv
->num_connectors
++] = connector
;
278 tilcdc_crtc_set_panel_info(priv
->crtc
,
279 to_panel_encoder(encoder
)->mod
->info
);
284 static const struct tilcdc_module_ops panel_module_ops
= {
285 .modeset_init
= panel_modeset_init
,
292 /* maybe move this somewhere common if it is needed by other outputs? */
293 static struct tilcdc_panel_info
*of_get_panel_info(struct device_node
*np
)
295 struct device_node
*info_np
;
296 struct tilcdc_panel_info
*info
;
300 pr_err("%s: no devicenode given\n", __func__
);
304 info_np
= of_get_child_by_name(np
, "panel-info");
306 pr_err("%s: could not find panel-info node\n", __func__
);
310 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
312 pr_err("%s: allocation failed\n", __func__
);
313 of_node_put(info_np
);
317 ret
|= of_property_read_u32(info_np
, "ac-bias", &info
->ac_bias
);
318 ret
|= of_property_read_u32(info_np
, "ac-bias-intrpt", &info
->ac_bias_intrpt
);
319 ret
|= of_property_read_u32(info_np
, "dma-burst-sz", &info
->dma_burst_sz
);
320 ret
|= of_property_read_u32(info_np
, "bpp", &info
->bpp
);
321 ret
|= of_property_read_u32(info_np
, "fdd", &info
->fdd
);
322 ret
|= of_property_read_u32(info_np
, "sync-edge", &info
->sync_edge
);
323 ret
|= of_property_read_u32(info_np
, "sync-ctrl", &info
->sync_ctrl
);
324 ret
|= of_property_read_u32(info_np
, "raster-order", &info
->raster_order
);
325 ret
|= of_property_read_u32(info_np
, "fifo-th", &info
->fifo_th
);
328 info
->tft_alt_mode
= of_property_read_bool(info_np
, "tft-alt-mode");
329 info
->invert_pxl_clk
= of_property_read_bool(info_np
, "invert-pxl-clk");
332 pr_err("%s: error reading panel-info properties\n", __func__
);
334 of_node_put(info_np
);
337 of_node_put(info_np
);
342 static int panel_probe(struct platform_device
*pdev
)
344 struct device_node
*bl_node
, *node
= pdev
->dev
.of_node
;
345 struct panel_module
*panel_mod
;
346 struct tilcdc_module
*mod
;
347 struct pinctrl
*pinctrl
;
350 /* bail out early if no DT data: */
352 dev_err(&pdev
->dev
, "device-tree data is missing\n");
356 panel_mod
= devm_kzalloc(&pdev
->dev
, sizeof(*panel_mod
), GFP_KERNEL
);
360 bl_node
= of_parse_phandle(node
, "backlight", 0);
362 panel_mod
->backlight
= of_find_backlight_by_node(bl_node
);
363 of_node_put(bl_node
);
365 if (!panel_mod
->backlight
)
366 return -EPROBE_DEFER
;
368 dev_info(&pdev
->dev
, "found backlight\n");
371 panel_mod
->enable_gpio
= devm_gpiod_get_optional(&pdev
->dev
, "enable",
373 if (IS_ERR(panel_mod
->enable_gpio
)) {
374 ret
= PTR_ERR(panel_mod
->enable_gpio
);
375 dev_err(&pdev
->dev
, "failed to request enable GPIO\n");
379 if (panel_mod
->enable_gpio
)
380 dev_info(&pdev
->dev
, "found enable GPIO\n");
382 mod
= &panel_mod
->base
;
383 pdev
->dev
.platform_data
= mod
;
385 tilcdc_module_init(mod
, "panel", &panel_module_ops
);
387 pinctrl
= devm_pinctrl_get_select_default(&pdev
->dev
);
389 dev_warn(&pdev
->dev
, "pins are not configured\n");
391 panel_mod
->timings
= of_get_display_timings(node
);
392 if (!panel_mod
->timings
) {
393 dev_err(&pdev
->dev
, "could not get panel timings\n");
398 panel_mod
->info
= of_get_panel_info(node
);
399 if (!panel_mod
->info
) {
400 dev_err(&pdev
->dev
, "could not get panel info\n");
408 display_timings_release(panel_mod
->timings
);
411 tilcdc_module_cleanup(mod
);
414 if (panel_mod
->backlight
)
415 put_device(&panel_mod
->backlight
->dev
);
419 static int panel_remove(struct platform_device
*pdev
)
421 struct tilcdc_module
*mod
= dev_get_platdata(&pdev
->dev
);
422 struct panel_module
*panel_mod
= to_panel_module(mod
);
423 struct backlight_device
*backlight
= panel_mod
->backlight
;
426 put_device(&backlight
->dev
);
428 display_timings_release(panel_mod
->timings
);
430 tilcdc_module_cleanup(mod
);
431 kfree(panel_mod
->info
);
436 static struct of_device_id panel_of_match
[] = {
437 { .compatible
= "ti,tilcdc,panel", },
441 struct platform_driver panel_driver
= {
442 .probe
= panel_probe
,
443 .remove
= panel_remove
,
445 .owner
= THIS_MODULE
,
447 .of_match_table
= panel_of_match
,
451 int __init
tilcdc_panel_init(void)
453 return platform_driver_register(&panel_driver
);
456 void __exit
tilcdc_panel_fini(void)
458 platform_driver_unregister(&panel_driver
);