xref: /openbmc/linux/drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_rgb.c (revision 4da722ca19f30f7db250db808d1ab1703607a932)
1 /*
2  * Copyright 2015 Freescale Semiconductor, Inc.
3  *
4  * Freescale DCU drm device driver
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  */
11 
12 #include <linux/backlight.h>
13 #include <linux/of_graph.h>
14 
15 #include <drm/drmP.h>
16 #include <drm/drm_atomic_helper.h>
17 #include <drm/drm_crtc_helper.h>
18 #include <drm/drm_of.h>
19 #include <drm/drm_panel.h>
20 
21 #include "fsl_dcu_drm_drv.h"
22 #include "fsl_tcon.h"
23 
24 static void fsl_dcu_drm_encoder_destroy(struct drm_encoder *encoder)
25 {
26 	drm_encoder_cleanup(encoder);
27 }
28 
29 static const struct drm_encoder_funcs encoder_funcs = {
30 	.destroy = fsl_dcu_drm_encoder_destroy,
31 };
32 
33 int fsl_dcu_drm_encoder_create(struct fsl_dcu_drm_device *fsl_dev,
34 			       struct drm_crtc *crtc)
35 {
36 	struct drm_encoder *encoder = &fsl_dev->encoder;
37 	int ret;
38 
39 	encoder->possible_crtcs = 1;
40 
41 	/* Use bypass mode for parallel RGB/LVDS encoder */
42 	if (fsl_dev->tcon)
43 		fsl_tcon_bypass_enable(fsl_dev->tcon);
44 
45 	ret = drm_encoder_init(fsl_dev->drm, encoder, &encoder_funcs,
46 			       DRM_MODE_ENCODER_LVDS, NULL);
47 	if (ret < 0)
48 		return ret;
49 
50 	return 0;
51 }
52 
53 static void fsl_dcu_drm_connector_destroy(struct drm_connector *connector)
54 {
55 	struct fsl_dcu_drm_connector *fsl_con = to_fsl_dcu_connector(connector);
56 
57 	drm_connector_unregister(connector);
58 	drm_panel_detach(fsl_con->panel);
59 	drm_connector_cleanup(connector);
60 }
61 
62 static const struct drm_connector_funcs fsl_dcu_drm_connector_funcs = {
63 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
64 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
65 	.destroy = fsl_dcu_drm_connector_destroy,
66 	.dpms = drm_atomic_helper_connector_dpms,
67 	.fill_modes = drm_helper_probe_single_connector_modes,
68 	.reset = drm_atomic_helper_connector_reset,
69 };
70 
71 static int fsl_dcu_drm_connector_get_modes(struct drm_connector *connector)
72 {
73 	struct fsl_dcu_drm_connector *fsl_connector;
74 	int (*get_modes)(struct drm_panel *panel);
75 	int num_modes = 0;
76 
77 	fsl_connector = to_fsl_dcu_connector(connector);
78 	if (fsl_connector->panel && fsl_connector->panel->funcs &&
79 	    fsl_connector->panel->funcs->get_modes) {
80 		get_modes = fsl_connector->panel->funcs->get_modes;
81 		num_modes = get_modes(fsl_connector->panel);
82 	}
83 
84 	return num_modes;
85 }
86 
87 static int fsl_dcu_drm_connector_mode_valid(struct drm_connector *connector,
88 					    struct drm_display_mode *mode)
89 {
90 	if (mode->hdisplay & 0xf)
91 		return MODE_ERROR;
92 
93 	return MODE_OK;
94 }
95 
96 static const struct drm_connector_helper_funcs connector_helper_funcs = {
97 	.get_modes = fsl_dcu_drm_connector_get_modes,
98 	.mode_valid = fsl_dcu_drm_connector_mode_valid,
99 };
100 
101 static int fsl_dcu_attach_panel(struct fsl_dcu_drm_device *fsl_dev,
102 				 struct drm_panel *panel)
103 {
104 	struct drm_encoder *encoder = &fsl_dev->encoder;
105 	struct drm_connector *connector = &fsl_dev->connector.base;
106 	struct drm_mode_config *mode_config = &fsl_dev->drm->mode_config;
107 	int ret;
108 
109 	fsl_dev->connector.encoder = encoder;
110 
111 	ret = drm_connector_init(fsl_dev->drm, connector,
112 				 &fsl_dcu_drm_connector_funcs,
113 				 DRM_MODE_CONNECTOR_LVDS);
114 	if (ret < 0)
115 		return ret;
116 
117 	drm_connector_helper_add(connector, &connector_helper_funcs);
118 	ret = drm_connector_register(connector);
119 	if (ret < 0)
120 		goto err_cleanup;
121 
122 	ret = drm_mode_connector_attach_encoder(connector, encoder);
123 	if (ret < 0)
124 		goto err_sysfs;
125 
126 	drm_object_property_set_value(&connector->base,
127 				      mode_config->dpms_property,
128 				      DRM_MODE_DPMS_OFF);
129 
130 	ret = drm_panel_attach(panel, connector);
131 	if (ret) {
132 		dev_err(fsl_dev->dev, "failed to attach panel\n");
133 		goto err_sysfs;
134 	}
135 
136 	return 0;
137 
138 err_sysfs:
139 	drm_connector_unregister(connector);
140 err_cleanup:
141 	drm_connector_cleanup(connector);
142 	return ret;
143 }
144 
145 int fsl_dcu_create_outputs(struct fsl_dcu_drm_device *fsl_dev)
146 {
147 	struct device_node *panel_node;
148 	struct drm_panel *panel;
149 	struct drm_bridge *bridge;
150 	int ret;
151 
152 	/* This is for backward compatibility */
153 	panel_node = of_parse_phandle(fsl_dev->np, "fsl,panel", 0);
154 	if (panel_node) {
155 		fsl_dev->connector.panel = of_drm_find_panel(panel_node);
156 		of_node_put(panel_node);
157 		if (!fsl_dev->connector.panel)
158 			return -EPROBE_DEFER;
159 		return fsl_dcu_attach_panel(fsl_dev, fsl_dev->connector.panel);
160 	}
161 
162 	ret = drm_of_find_panel_or_bridge(fsl_dev->np, 0, 0, &panel, &bridge);
163 	if (ret)
164 		return ret;
165 
166 	if (panel) {
167 		fsl_dev->connector.panel = panel;
168 		return fsl_dcu_attach_panel(fsl_dev, panel);
169 	}
170 
171 	return drm_bridge_attach(&fsl_dev->encoder, bridge, NULL);
172 }
173