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>
26 #include <drm/drm_probe_helper.h>
28 #include "tilcdc_drv.h"
29 #include "tilcdc_panel.h"
32 struct tilcdc_module base
;
33 struct tilcdc_panel_info
*info
;
34 struct display_timings
*timings
;
35 struct backlight_device
*backlight
;
36 struct gpio_desc
*enable_gpio
;
38 #define to_panel_module(x) container_of(x, struct panel_module, base)
45 struct panel_encoder
{
46 struct drm_encoder base
;
47 struct panel_module
*mod
;
49 #define to_panel_encoder(x) container_of(x, struct panel_encoder, base)
51 static void panel_encoder_dpms(struct drm_encoder
*encoder
, int mode
)
53 struct panel_encoder
*panel_encoder
= to_panel_encoder(encoder
);
54 struct backlight_device
*backlight
= panel_encoder
->mod
->backlight
;
55 struct gpio_desc
*gpio
= panel_encoder
->mod
->enable_gpio
;
58 backlight
->props
.power
= mode
== DRM_MODE_DPMS_ON
?
59 FB_BLANK_UNBLANK
: FB_BLANK_POWERDOWN
;
60 backlight_update_status(backlight
);
64 gpiod_set_value_cansleep(gpio
,
65 mode
== DRM_MODE_DPMS_ON
? 1 : 0);
68 static void panel_encoder_prepare(struct drm_encoder
*encoder
)
70 panel_encoder_dpms(encoder
, DRM_MODE_DPMS_OFF
);
73 static void panel_encoder_commit(struct drm_encoder
*encoder
)
75 panel_encoder_dpms(encoder
, DRM_MODE_DPMS_ON
);
78 static void panel_encoder_mode_set(struct drm_encoder
*encoder
,
79 struct drm_display_mode
*mode
,
80 struct drm_display_mode
*adjusted_mode
)
85 static const struct drm_encoder_funcs panel_encoder_funcs
= {
86 .destroy
= drm_encoder_cleanup
,
89 static const struct drm_encoder_helper_funcs panel_encoder_helper_funcs
= {
90 .dpms
= panel_encoder_dpms
,
91 .prepare
= panel_encoder_prepare
,
92 .commit
= panel_encoder_commit
,
93 .mode_set
= panel_encoder_mode_set
,
96 static struct drm_encoder
*panel_encoder_create(struct drm_device
*dev
,
97 struct panel_module
*mod
)
99 struct panel_encoder
*panel_encoder
;
100 struct drm_encoder
*encoder
;
103 panel_encoder
= devm_kzalloc(dev
->dev
, sizeof(*panel_encoder
),
108 panel_encoder
->mod
= mod
;
110 encoder
= &panel_encoder
->base
;
111 encoder
->possible_crtcs
= 1;
113 ret
= drm_encoder_init(dev
, encoder
, &panel_encoder_funcs
,
114 DRM_MODE_ENCODER_LVDS
, NULL
);
118 drm_encoder_helper_add(encoder
, &panel_encoder_helper_funcs
);
123 drm_encoder_cleanup(encoder
);
131 struct panel_connector
{
132 struct drm_connector base
;
134 struct drm_encoder
*encoder
; /* our connected encoder */
135 struct panel_module
*mod
;
137 #define to_panel_connector(x) container_of(x, struct panel_connector, base)
140 static void panel_connector_destroy(struct drm_connector
*connector
)
142 drm_connector_unregister(connector
);
143 drm_connector_cleanup(connector
);
146 static int panel_connector_get_modes(struct drm_connector
*connector
)
148 struct drm_device
*dev
= connector
->dev
;
149 struct panel_connector
*panel_connector
= to_panel_connector(connector
);
150 struct display_timings
*timings
= panel_connector
->mod
->timings
;
153 for (i
= 0; i
< timings
->num_timings
; i
++) {
154 struct drm_display_mode
*mode
= drm_mode_create(dev
);
157 if (videomode_from_timings(timings
, &vm
, i
))
160 drm_display_mode_from_videomode(&vm
, mode
);
162 mode
->type
= DRM_MODE_TYPE_DRIVER
;
164 if (timings
->native_mode
== i
)
165 mode
->type
|= DRM_MODE_TYPE_PREFERRED
;
167 drm_mode_set_name(mode
);
168 drm_mode_probed_add(connector
, mode
);
174 static int panel_connector_mode_valid(struct drm_connector
*connector
,
175 struct drm_display_mode
*mode
)
177 struct tilcdc_drm_private
*priv
= connector
->dev
->dev_private
;
178 /* our only constraints are what the crtc can generate: */
179 return tilcdc_crtc_mode_valid(priv
->crtc
, mode
);
182 static struct drm_encoder
*panel_connector_best_encoder(
183 struct drm_connector
*connector
)
185 struct panel_connector
*panel_connector
= to_panel_connector(connector
);
186 return panel_connector
->encoder
;
189 static const struct drm_connector_funcs panel_connector_funcs
= {
190 .destroy
= panel_connector_destroy
,
191 .fill_modes
= drm_helper_probe_single_connector_modes
,
192 .reset
= drm_atomic_helper_connector_reset
,
193 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
194 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
197 static const struct drm_connector_helper_funcs panel_connector_helper_funcs
= {
198 .get_modes
= panel_connector_get_modes
,
199 .mode_valid
= panel_connector_mode_valid
,
200 .best_encoder
= panel_connector_best_encoder
,
203 static struct drm_connector
*panel_connector_create(struct drm_device
*dev
,
204 struct panel_module
*mod
, struct drm_encoder
*encoder
)
206 struct panel_connector
*panel_connector
;
207 struct drm_connector
*connector
;
210 panel_connector
= devm_kzalloc(dev
->dev
, sizeof(*panel_connector
),
212 if (!panel_connector
)
215 panel_connector
->encoder
= encoder
;
216 panel_connector
->mod
= mod
;
218 connector
= &panel_connector
->base
;
220 drm_connector_init(dev
, connector
, &panel_connector_funcs
,
221 DRM_MODE_CONNECTOR_LVDS
);
222 drm_connector_helper_add(connector
, &panel_connector_helper_funcs
);
224 connector
->interlace_allowed
= 0;
225 connector
->doublescan_allowed
= 0;
227 ret
= drm_connector_attach_encoder(connector
, encoder
);
234 panel_connector_destroy(connector
);
242 static int panel_modeset_init(struct tilcdc_module
*mod
, struct drm_device
*dev
)
244 struct panel_module
*panel_mod
= to_panel_module(mod
);
245 struct tilcdc_drm_private
*priv
= dev
->dev_private
;
246 struct drm_encoder
*encoder
;
247 struct drm_connector
*connector
;
249 encoder
= panel_encoder_create(dev
, panel_mod
);
253 connector
= panel_connector_create(dev
, panel_mod
, encoder
);
257 priv
->encoders
[priv
->num_encoders
++] = encoder
;
258 priv
->connectors
[priv
->num_connectors
++] = connector
;
260 tilcdc_crtc_set_panel_info(priv
->crtc
,
261 to_panel_encoder(encoder
)->mod
->info
);
266 static const struct tilcdc_module_ops panel_module_ops
= {
267 .modeset_init
= panel_modeset_init
,
274 /* maybe move this somewhere common if it is needed by other outputs? */
275 static struct tilcdc_panel_info
*of_get_panel_info(struct device_node
*np
)
277 struct device_node
*info_np
;
278 struct tilcdc_panel_info
*info
;
282 pr_err("%s: no devicenode given\n", __func__
);
286 info_np
= of_get_child_by_name(np
, "panel-info");
288 pr_err("%s: could not find panel-info node\n", __func__
);
292 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
296 ret
|= of_property_read_u32(info_np
, "ac-bias", &info
->ac_bias
);
297 ret
|= of_property_read_u32(info_np
, "ac-bias-intrpt", &info
->ac_bias_intrpt
);
298 ret
|= of_property_read_u32(info_np
, "dma-burst-sz", &info
->dma_burst_sz
);
299 ret
|= of_property_read_u32(info_np
, "bpp", &info
->bpp
);
300 ret
|= of_property_read_u32(info_np
, "fdd", &info
->fdd
);
301 ret
|= of_property_read_u32(info_np
, "sync-edge", &info
->sync_edge
);
302 ret
|= of_property_read_u32(info_np
, "sync-ctrl", &info
->sync_ctrl
);
303 ret
|= of_property_read_u32(info_np
, "raster-order", &info
->raster_order
);
304 ret
|= of_property_read_u32(info_np
, "fifo-th", &info
->fifo_th
);
307 info
->tft_alt_mode
= of_property_read_bool(info_np
, "tft-alt-mode");
308 info
->invert_pxl_clk
= of_property_read_bool(info_np
, "invert-pxl-clk");
311 pr_err("%s: error reading panel-info properties\n", __func__
);
317 of_node_put(info_np
);
321 static int panel_probe(struct platform_device
*pdev
)
323 struct device_node
*bl_node
, *node
= pdev
->dev
.of_node
;
324 struct panel_module
*panel_mod
;
325 struct tilcdc_module
*mod
;
326 struct pinctrl
*pinctrl
;
329 /* bail out early if no DT data: */
331 dev_err(&pdev
->dev
, "device-tree data is missing\n");
335 panel_mod
= devm_kzalloc(&pdev
->dev
, sizeof(*panel_mod
), GFP_KERNEL
);
339 bl_node
= of_parse_phandle(node
, "backlight", 0);
341 panel_mod
->backlight
= of_find_backlight_by_node(bl_node
);
342 of_node_put(bl_node
);
344 if (!panel_mod
->backlight
)
345 return -EPROBE_DEFER
;
347 dev_info(&pdev
->dev
, "found backlight\n");
350 panel_mod
->enable_gpio
= devm_gpiod_get_optional(&pdev
->dev
, "enable",
352 if (IS_ERR(panel_mod
->enable_gpio
)) {
353 ret
= PTR_ERR(panel_mod
->enable_gpio
);
354 dev_err(&pdev
->dev
, "failed to request enable GPIO\n");
358 if (panel_mod
->enable_gpio
)
359 dev_info(&pdev
->dev
, "found enable GPIO\n");
361 mod
= &panel_mod
->base
;
362 pdev
->dev
.platform_data
= mod
;
364 tilcdc_module_init(mod
, "panel", &panel_module_ops
);
366 pinctrl
= devm_pinctrl_get_select_default(&pdev
->dev
);
368 dev_warn(&pdev
->dev
, "pins are not configured\n");
370 panel_mod
->timings
= of_get_display_timings(node
);
371 if (!panel_mod
->timings
) {
372 dev_err(&pdev
->dev
, "could not get panel timings\n");
377 panel_mod
->info
= of_get_panel_info(node
);
378 if (!panel_mod
->info
) {
379 dev_err(&pdev
->dev
, "could not get panel info\n");
387 display_timings_release(panel_mod
->timings
);
390 tilcdc_module_cleanup(mod
);
393 if (panel_mod
->backlight
)
394 put_device(&panel_mod
->backlight
->dev
);
398 static int panel_remove(struct platform_device
*pdev
)
400 struct tilcdc_module
*mod
= dev_get_platdata(&pdev
->dev
);
401 struct panel_module
*panel_mod
= to_panel_module(mod
);
402 struct backlight_device
*backlight
= panel_mod
->backlight
;
405 put_device(&backlight
->dev
);
407 display_timings_release(panel_mod
->timings
);
409 tilcdc_module_cleanup(mod
);
410 kfree(panel_mod
->info
);
415 static const struct of_device_id panel_of_match
[] = {
416 { .compatible
= "ti,tilcdc,panel", },
420 struct platform_driver panel_driver
= {
421 .probe
= panel_probe
,
422 .remove
= panel_remove
,
424 .owner
= THIS_MODULE
,
425 .name
= "tilcdc-panel",
426 .of_match_table
= panel_of_match
,
430 int __init
tilcdc_panel_init(void)
432 return platform_driver_register(&panel_driver
);
435 void __exit
tilcdc_panel_fini(void)
437 platform_driver_unregister(&panel_driver
);