2 * Copyright (C) 2016 Laurent Pinchart <laurent.pinchart@ideasonboard.com>
3 * Copyright (C) 2017 Broadcom
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
12 #include <drm/drm_panel.h>
13 #include <drm/drm_atomic_helper.h>
14 #include <drm/drm_connector.h>
15 #include <drm/drm_crtc_helper.h>
16 #include <drm/drm_encoder.h>
17 #include <drm/drm_modeset_helper_vtables.h>
18 #include <drm/drm_panel.h>
21 struct drm_bridge bridge
;
22 struct drm_connector connector
;
23 struct drm_panel
*panel
;
27 static inline struct panel_bridge
*
28 drm_bridge_to_panel_bridge(struct drm_bridge
*bridge
)
30 return container_of(bridge
, struct panel_bridge
, bridge
);
33 static inline struct panel_bridge
*
34 drm_connector_to_panel_bridge(struct drm_connector
*connector
)
36 return container_of(connector
, struct panel_bridge
, connector
);
39 static int panel_bridge_connector_get_modes(struct drm_connector
*connector
)
41 struct panel_bridge
*panel_bridge
=
42 drm_connector_to_panel_bridge(connector
);
44 return drm_panel_get_modes(panel_bridge
->panel
);
47 static const struct drm_connector_helper_funcs
48 panel_bridge_connector_helper_funcs
= {
49 .get_modes
= panel_bridge_connector_get_modes
,
52 static const struct drm_connector_funcs panel_bridge_connector_funcs
= {
53 .dpms
= drm_atomic_helper_connector_dpms
,
54 .reset
= drm_atomic_helper_connector_reset
,
55 .fill_modes
= drm_helper_probe_single_connector_modes
,
56 .destroy
= drm_connector_cleanup
,
57 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
58 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
61 static int panel_bridge_attach(struct drm_bridge
*bridge
)
63 struct panel_bridge
*panel_bridge
= drm_bridge_to_panel_bridge(bridge
);
64 struct drm_connector
*connector
= &panel_bridge
->connector
;
67 if (!bridge
->encoder
) {
68 DRM_ERROR("Missing encoder\n");
72 drm_connector_helper_add(connector
,
73 &panel_bridge_connector_helper_funcs
);
75 ret
= drm_connector_init(bridge
->dev
, connector
,
76 &panel_bridge_connector_funcs
,
77 panel_bridge
->connector_type
);
79 DRM_ERROR("Failed to initialize connector\n");
83 drm_mode_connector_attach_encoder(&panel_bridge
->connector
,
86 ret
= drm_panel_attach(panel_bridge
->panel
, &panel_bridge
->connector
);
93 static void panel_bridge_detach(struct drm_bridge
*bridge
)
95 struct panel_bridge
*panel_bridge
= drm_bridge_to_panel_bridge(bridge
);
97 drm_panel_detach(panel_bridge
->panel
);
100 static void panel_bridge_pre_enable(struct drm_bridge
*bridge
)
102 struct panel_bridge
*panel_bridge
= drm_bridge_to_panel_bridge(bridge
);
104 drm_panel_prepare(panel_bridge
->panel
);
107 static void panel_bridge_enable(struct drm_bridge
*bridge
)
109 struct panel_bridge
*panel_bridge
= drm_bridge_to_panel_bridge(bridge
);
111 drm_panel_enable(panel_bridge
->panel
);
114 static void panel_bridge_disable(struct drm_bridge
*bridge
)
116 struct panel_bridge
*panel_bridge
= drm_bridge_to_panel_bridge(bridge
);
118 drm_panel_disable(panel_bridge
->panel
);
121 static void panel_bridge_post_disable(struct drm_bridge
*bridge
)
123 struct panel_bridge
*panel_bridge
= drm_bridge_to_panel_bridge(bridge
);
125 drm_panel_unprepare(panel_bridge
->panel
);
128 static const struct drm_bridge_funcs panel_bridge_bridge_funcs
= {
129 .attach
= panel_bridge_attach
,
130 .detach
= panel_bridge_detach
,
131 .pre_enable
= panel_bridge_pre_enable
,
132 .enable
= panel_bridge_enable
,
133 .disable
= panel_bridge_disable
,
134 .post_disable
= panel_bridge_post_disable
,
138 * drm_panel_bridge_add - Creates a drm_bridge and drm_connector that
139 * just calls the appropriate functions from drm_panel.
141 * @panel: The drm_panel being wrapped. Must be non-NULL.
142 * @connector_type: The DRM_MODE_CONNECTOR_* for the connector to be
145 * For drivers converting from directly using drm_panel: The expected
146 * usage pattern is that during either encoder module probe or DSI
147 * host attach, a drm_panel will be looked up through
148 * drm_of_find_panel_or_bridge(). drm_panel_bridge_add() is used to
149 * wrap that panel in the new bridge, and the result can then be
150 * passed to drm_bridge_attach(). The drm_panel_prepare() and related
151 * functions can be dropped from the encoder driver (they're now
152 * called by the KMS helpers before calling into the encoder), along
153 * with connector creation. When done with the bridge,
154 * drm_bridge_detach() should be called as normal, then
155 * drm_panel_bridge_remove() to free it.
157 struct drm_bridge
*drm_panel_bridge_add(struct drm_panel
*panel
,
160 struct panel_bridge
*panel_bridge
;
164 return ERR_PTR(-EINVAL
);
166 panel_bridge
= devm_kzalloc(panel
->dev
, sizeof(*panel_bridge
),
169 return ERR_PTR(-ENOMEM
);
171 panel_bridge
->connector_type
= connector_type
;
172 panel_bridge
->panel
= panel
;
174 panel_bridge
->bridge
.funcs
= &panel_bridge_bridge_funcs
;
176 panel_bridge
->bridge
.of_node
= panel
->dev
->of_node
;
179 ret
= drm_bridge_add(&panel_bridge
->bridge
);
183 return &panel_bridge
->bridge
;
185 EXPORT_SYMBOL(drm_panel_bridge_add
);
188 * drm_panel_bridge_remove - Unregisters and frees a drm_bridge
189 * created by drm_panel_bridge_add().
191 * @bridge: The drm_bridge being freed.
193 void drm_panel_bridge_remove(struct drm_bridge
*bridge
)
195 struct panel_bridge
*panel_bridge
= drm_bridge_to_panel_bridge(bridge
);
197 drm_bridge_remove(bridge
);
198 devm_kfree(panel_bridge
->panel
->dev
, bridge
);
200 EXPORT_SYMBOL(drm_panel_bridge_remove
);