Merge tag 'csky-for-linus-4.21' of git://github.com/c-sky/csky-linux
[linux-2.6-microblaze.git] / drivers / gpu / drm / rcar-du / rcar_du_encoder.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * rcar_du_encoder.c  --  R-Car Display Unit Encoder
4  *
5  * Copyright (C) 2013-2014 Renesas Electronics Corporation
6  *
7  * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
8  */
9
10 #include <linux/export.h>
11
12 #include <drm/drmP.h>
13 #include <drm/drm_crtc.h>
14 #include <drm/drm_crtc_helper.h>
15 #include <drm/drm_panel.h>
16
17 #include "rcar_du_drv.h"
18 #include "rcar_du_encoder.h"
19 #include "rcar_du_kms.h"
20
21 /* -----------------------------------------------------------------------------
22  * Encoder
23  */
24
25 static void rcar_du_encoder_mode_set(struct drm_encoder *encoder,
26                                      struct drm_crtc_state *crtc_state,
27                                      struct drm_connector_state *conn_state)
28 {
29         struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
30
31         rcar_du_crtc_route_output(crtc_state->crtc, renc->output);
32 }
33
34 static const struct drm_encoder_helper_funcs encoder_helper_funcs = {
35         .atomic_mode_set = rcar_du_encoder_mode_set,
36 };
37
38 static const struct drm_encoder_funcs encoder_funcs = {
39         .destroy = drm_encoder_cleanup,
40 };
41
42 int rcar_du_encoder_init(struct rcar_du_device *rcdu,
43                          enum rcar_du_output output,
44                          struct device_node *enc_node,
45                          struct device_node *con_node)
46 {
47         struct rcar_du_encoder *renc;
48         struct drm_encoder *encoder;
49         struct drm_bridge *bridge = NULL;
50         int ret;
51
52         renc = devm_kzalloc(rcdu->dev, sizeof(*renc), GFP_KERNEL);
53         if (renc == NULL)
54                 return -ENOMEM;
55
56         renc->output = output;
57         encoder = rcar_encoder_to_drm_encoder(renc);
58
59         dev_dbg(rcdu->dev, "initializing encoder %pOF for output %u\n",
60                 enc_node, output);
61
62         /* Locate the DRM bridge from the encoder DT node. */
63         bridge = of_drm_find_bridge(enc_node);
64         if (!bridge) {
65                 ret = -EPROBE_DEFER;
66                 goto done;
67         }
68
69         ret = drm_encoder_init(rcdu->ddev, encoder, &encoder_funcs,
70                                DRM_MODE_ENCODER_NONE, NULL);
71         if (ret < 0)
72                 goto done;
73
74         drm_encoder_helper_add(encoder, &encoder_helper_funcs);
75
76         /*
77          * Attach the bridge to the encoder. The bridge will create the
78          * connector.
79          */
80         ret = drm_bridge_attach(encoder, bridge, NULL);
81         if (ret) {
82                 drm_encoder_cleanup(encoder);
83                 return ret;
84         }
85
86 done:
87         if (ret < 0) {
88                 if (encoder->name)
89                         encoder->funcs->destroy(encoder);
90                 devm_kfree(rcdu->dev, renc);
91         }
92
93         return ret;
94 }