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 
14 #include <drm/drmP.h>
15 #include <drm/drm_atomic_helper.h>
16 #include <drm/drm_crtc_helper.h>
17 #include <drm/drm_panel.h>
18 
19 #include "fsl_dcu_drm_drv.h"
20 #include "fsl_tcon.h"
21 
22 static int
23 fsl_dcu_drm_encoder_atomic_check(struct drm_encoder *encoder,
24 				 struct drm_crtc_state *crtc_state,
25 				 struct drm_connector_state *conn_state)
26 {
27 	return 0;
28 }
29 
30 static void fsl_dcu_drm_encoder_disable(struct drm_encoder *encoder)
31 {
32 	struct drm_device *dev = encoder->dev;
33 	struct fsl_dcu_drm_device *fsl_dev = dev->dev_private;
34 
35 	if (fsl_dev->tcon)
36 		fsl_tcon_bypass_disable(fsl_dev->tcon);
37 }
38 
39 static void fsl_dcu_drm_encoder_enable(struct drm_encoder *encoder)
40 {
41 	struct drm_device *dev = encoder->dev;
42 	struct fsl_dcu_drm_device *fsl_dev = dev->dev_private;
43 
44 	if (fsl_dev->tcon)
45 		fsl_tcon_bypass_enable(fsl_dev->tcon);
46 }
47 
48 static const struct drm_encoder_helper_funcs encoder_helper_funcs = {
49 	.atomic_check = fsl_dcu_drm_encoder_atomic_check,
50 	.disable = fsl_dcu_drm_encoder_disable,
51 	.enable = fsl_dcu_drm_encoder_enable,
52 };
53 
54 static void fsl_dcu_drm_encoder_destroy(struct drm_encoder *encoder)
55 {
56 	drm_encoder_cleanup(encoder);
57 }
58 
59 static const struct drm_encoder_funcs encoder_funcs = {
60 	.destroy = fsl_dcu_drm_encoder_destroy,
61 };
62 
63 int fsl_dcu_drm_encoder_create(struct fsl_dcu_drm_device *fsl_dev,
64 			       struct drm_crtc *crtc)
65 {
66 	struct drm_encoder *encoder = &fsl_dev->encoder;
67 	int ret;
68 
69 	encoder->possible_crtcs = 1;
70 	ret = drm_encoder_init(fsl_dev->drm, encoder, &encoder_funcs,
71 			       DRM_MODE_ENCODER_LVDS, NULL);
72 	if (ret < 0)
73 		return ret;
74 
75 	drm_encoder_helper_add(encoder, &encoder_helper_funcs);
76 
77 	return 0;
78 }
79 
80 static void fsl_dcu_drm_connector_destroy(struct drm_connector *connector)
81 {
82 	drm_connector_unregister(connector);
83 	drm_connector_cleanup(connector);
84 }
85 
86 static enum drm_connector_status
87 fsl_dcu_drm_connector_detect(struct drm_connector *connector, bool force)
88 {
89 	return connector_status_connected;
90 }
91 
92 static const struct drm_connector_funcs fsl_dcu_drm_connector_funcs = {
93 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
94 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
95 	.destroy = fsl_dcu_drm_connector_destroy,
96 	.detect = fsl_dcu_drm_connector_detect,
97 	.dpms = drm_atomic_helper_connector_dpms,
98 	.fill_modes = drm_helper_probe_single_connector_modes,
99 	.reset = drm_atomic_helper_connector_reset,
100 };
101 
102 static struct drm_encoder *
103 fsl_dcu_drm_connector_best_encoder(struct drm_connector *connector)
104 {
105 	struct fsl_dcu_drm_connector *fsl_con = to_fsl_dcu_connector(connector);
106 
107 	return fsl_con->encoder;
108 }
109 
110 static int fsl_dcu_drm_connector_get_modes(struct drm_connector *connector)
111 {
112 	struct fsl_dcu_drm_connector *fsl_connector;
113 	int (*get_modes)(struct drm_panel *panel);
114 	int num_modes = 0;
115 
116 	fsl_connector = to_fsl_dcu_connector(connector);
117 	if (fsl_connector->panel && fsl_connector->panel->funcs &&
118 	    fsl_connector->panel->funcs->get_modes) {
119 		get_modes = fsl_connector->panel->funcs->get_modes;
120 		num_modes = get_modes(fsl_connector->panel);
121 	}
122 
123 	return num_modes;
124 }
125 
126 static int fsl_dcu_drm_connector_mode_valid(struct drm_connector *connector,
127 					    struct drm_display_mode *mode)
128 {
129 	if (mode->hdisplay & 0xf)
130 		return MODE_ERROR;
131 
132 	return MODE_OK;
133 }
134 
135 static const struct drm_connector_helper_funcs connector_helper_funcs = {
136 	.best_encoder = fsl_dcu_drm_connector_best_encoder,
137 	.get_modes = fsl_dcu_drm_connector_get_modes,
138 	.mode_valid = fsl_dcu_drm_connector_mode_valid,
139 };
140 
141 int fsl_dcu_drm_connector_create(struct fsl_dcu_drm_device *fsl_dev,
142 				 struct drm_encoder *encoder)
143 {
144 	struct drm_connector *connector = &fsl_dev->connector.base;
145 	struct drm_mode_config mode_config = fsl_dev->drm->mode_config;
146 	struct device_node *panel_node;
147 	int ret;
148 
149 	fsl_dev->connector.encoder = encoder;
150 
151 	ret = drm_connector_init(fsl_dev->drm, connector,
152 				 &fsl_dcu_drm_connector_funcs,
153 				 DRM_MODE_CONNECTOR_LVDS);
154 	if (ret < 0)
155 		return ret;
156 
157 	drm_connector_helper_add(connector, &connector_helper_funcs);
158 	ret = drm_connector_register(connector);
159 	if (ret < 0)
160 		goto err_cleanup;
161 
162 	ret = drm_mode_connector_attach_encoder(connector, encoder);
163 	if (ret < 0)
164 		goto err_sysfs;
165 
166 	drm_object_property_set_value(&connector->base,
167 				      mode_config.dpms_property,
168 				      DRM_MODE_DPMS_OFF);
169 
170 	panel_node = of_parse_phandle(fsl_dev->np, "fsl,panel", 0);
171 	if (panel_node) {
172 		fsl_dev->connector.panel = of_drm_find_panel(panel_node);
173 		if (!fsl_dev->connector.panel) {
174 			ret = -EPROBE_DEFER;
175 			goto err_sysfs;
176 		}
177 	of_node_put(panel_node);
178 	}
179 
180 	ret = drm_panel_attach(fsl_dev->connector.panel, connector);
181 	if (ret) {
182 		dev_err(fsl_dev->dev, "failed to attach panel\n");
183 		goto err_sysfs;
184 	}
185 
186 	return 0;
187 
188 err_sysfs:
189 	drm_connector_unregister(connector);
190 err_cleanup:
191 	drm_connector_cleanup(connector);
192 	return ret;
193 }
194