treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / gpu / drm / exynos / exynos_drm_dpi.c
blob43fa0f26c052e97bc87248660d827e48c0ac14d8
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Exynos DRM Parallel output support.
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd
7 * Contacts: Andrzej Hajda <a.hajda@samsung.com>
8 */
10 #include <linux/of_graph.h>
11 #include <linux/regulator/consumer.h>
13 #include <drm/drm_atomic_helper.h>
14 #include <drm/drm_panel.h>
15 #include <drm/drm_print.h>
16 #include <drm/drm_probe_helper.h>
18 #include <video/of_videomode.h>
19 #include <video/videomode.h>
21 #include "exynos_drm_crtc.h"
23 struct exynos_dpi {
24 struct drm_encoder encoder;
25 struct device *dev;
26 struct device_node *panel_node;
28 struct drm_panel *panel;
29 struct drm_connector connector;
31 struct videomode *vm;
34 #define connector_to_dpi(c) container_of(c, struct exynos_dpi, connector)
36 static inline struct exynos_dpi *encoder_to_dpi(struct drm_encoder *e)
38 return container_of(e, struct exynos_dpi, encoder);
41 static enum drm_connector_status
42 exynos_dpi_detect(struct drm_connector *connector, bool force)
44 struct exynos_dpi *ctx = connector_to_dpi(connector);
46 if (ctx->panel)
47 drm_panel_attach(ctx->panel, &ctx->connector);
49 return connector_status_connected;
52 static void exynos_dpi_connector_destroy(struct drm_connector *connector)
54 drm_connector_unregister(connector);
55 drm_connector_cleanup(connector);
58 static const struct drm_connector_funcs exynos_dpi_connector_funcs = {
59 .detect = exynos_dpi_detect,
60 .fill_modes = drm_helper_probe_single_connector_modes,
61 .destroy = exynos_dpi_connector_destroy,
62 .reset = drm_atomic_helper_connector_reset,
63 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
64 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
67 static int exynos_dpi_get_modes(struct drm_connector *connector)
69 struct exynos_dpi *ctx = connector_to_dpi(connector);
71 /* fimd timings gets precedence over panel modes */
72 if (ctx->vm) {
73 struct drm_display_mode *mode;
75 mode = drm_mode_create(connector->dev);
76 if (!mode) {
77 DRM_DEV_ERROR(ctx->dev,
78 "failed to create a new display mode\n");
79 return 0;
81 drm_display_mode_from_videomode(ctx->vm, mode);
82 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
83 drm_mode_probed_add(connector, mode);
84 return 1;
87 if (ctx->panel)
88 return drm_panel_get_modes(ctx->panel, connector);
90 return 0;
93 static const struct drm_connector_helper_funcs exynos_dpi_connector_helper_funcs = {
94 .get_modes = exynos_dpi_get_modes,
97 static int exynos_dpi_create_connector(struct drm_encoder *encoder)
99 struct exynos_dpi *ctx = encoder_to_dpi(encoder);
100 struct drm_connector *connector = &ctx->connector;
101 int ret;
103 connector->polled = DRM_CONNECTOR_POLL_HPD;
105 ret = drm_connector_init(encoder->dev, connector,
106 &exynos_dpi_connector_funcs,
107 DRM_MODE_CONNECTOR_VGA);
108 if (ret) {
109 DRM_DEV_ERROR(ctx->dev,
110 "failed to initialize connector with drm\n");
111 return ret;
114 drm_connector_helper_add(connector, &exynos_dpi_connector_helper_funcs);
115 drm_connector_attach_encoder(connector, encoder);
117 return 0;
120 static void exynos_dpi_mode_set(struct drm_encoder *encoder,
121 struct drm_display_mode *mode,
122 struct drm_display_mode *adjusted_mode)
126 static void exynos_dpi_enable(struct drm_encoder *encoder)
128 struct exynos_dpi *ctx = encoder_to_dpi(encoder);
130 if (ctx->panel) {
131 drm_panel_prepare(ctx->panel);
132 drm_panel_enable(ctx->panel);
136 static void exynos_dpi_disable(struct drm_encoder *encoder)
138 struct exynos_dpi *ctx = encoder_to_dpi(encoder);
140 if (ctx->panel) {
141 drm_panel_disable(ctx->panel);
142 drm_panel_unprepare(ctx->panel);
146 static const struct drm_encoder_helper_funcs exynos_dpi_encoder_helper_funcs = {
147 .mode_set = exynos_dpi_mode_set,
148 .enable = exynos_dpi_enable,
149 .disable = exynos_dpi_disable,
152 static const struct drm_encoder_funcs exynos_dpi_encoder_funcs = {
153 .destroy = drm_encoder_cleanup,
156 enum {
157 FIMD_PORT_IN0,
158 FIMD_PORT_IN1,
159 FIMD_PORT_IN2,
160 FIMD_PORT_RGB,
161 FIMD_PORT_WRB,
164 static int exynos_dpi_parse_dt(struct exynos_dpi *ctx)
166 struct device *dev = ctx->dev;
167 struct device_node *dn = dev->of_node;
168 struct device_node *np;
170 ctx->panel_node = of_graph_get_remote_node(dn, FIMD_PORT_RGB, 0);
172 np = of_get_child_by_name(dn, "display-timings");
173 if (np) {
174 struct videomode *vm;
175 int ret;
177 of_node_put(np);
179 vm = devm_kzalloc(dev, sizeof(*ctx->vm), GFP_KERNEL);
180 if (!vm)
181 return -ENOMEM;
183 ret = of_get_videomode(dn, vm, 0);
184 if (ret < 0) {
185 devm_kfree(dev, vm);
186 return ret;
189 ctx->vm = vm;
191 return 0;
194 if (!ctx->panel_node)
195 return -EINVAL;
197 return 0;
200 int exynos_dpi_bind(struct drm_device *dev, struct drm_encoder *encoder)
202 int ret;
204 drm_encoder_init(dev, encoder, &exynos_dpi_encoder_funcs,
205 DRM_MODE_ENCODER_TMDS, NULL);
207 drm_encoder_helper_add(encoder, &exynos_dpi_encoder_helper_funcs);
209 ret = exynos_drm_set_possible_crtcs(encoder, EXYNOS_DISPLAY_TYPE_LCD);
210 if (ret < 0)
211 return ret;
213 ret = exynos_dpi_create_connector(encoder);
214 if (ret) {
215 DRM_DEV_ERROR(encoder_to_dpi(encoder)->dev,
216 "failed to create connector ret = %d\n", ret);
217 drm_encoder_cleanup(encoder);
218 return ret;
221 return 0;
224 struct drm_encoder *exynos_dpi_probe(struct device *dev)
226 struct exynos_dpi *ctx;
227 int ret;
229 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
230 if (!ctx)
231 return ERR_PTR(-ENOMEM);
233 ctx->dev = dev;
235 ret = exynos_dpi_parse_dt(ctx);
236 if (ret < 0) {
237 devm_kfree(dev, ctx);
238 return NULL;
241 if (ctx->panel_node) {
242 ctx->panel = of_drm_find_panel(ctx->panel_node);
243 if (IS_ERR(ctx->panel))
244 return ERR_CAST(ctx->panel);
247 return &ctx->encoder;
250 int exynos_dpi_remove(struct drm_encoder *encoder)
252 struct exynos_dpi *ctx = encoder_to_dpi(encoder);
254 exynos_dpi_disable(&ctx->encoder);
256 if (ctx->panel)
257 drm_panel_detach(ctx->panel);
259 return 0;