interconnect: qcom: icc-rpm: Fix peak rate calculation
[linux-2.6-microblaze.git] / drivers / gpu / drm / bridge / panel.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2016 Laurent Pinchart <laurent.pinchart@ideasonboard.com>
4  * Copyright (C) 2017 Broadcom
5  */
6
7 #include <linux/device.h>
8
9 #include <drm/drm_atomic_helper.h>
10 #include <drm/drm_bridge.h>
11 #include <drm/drm_connector.h>
12 #include <drm/drm_encoder.h>
13 #include <drm/drm_managed.h>
14 #include <drm/drm_modeset_helper_vtables.h>
15 #include <drm/drm_of.h>
16 #include <drm/drm_panel.h>
17 #include <drm/drm_print.h>
18 #include <drm/drm_probe_helper.h>
19
20 struct panel_bridge {
21         struct drm_bridge bridge;
22         struct drm_connector connector;
23         struct drm_panel *panel;
24         struct device_link *link;
25         u32 connector_type;
26 };
27
28 static inline struct panel_bridge *
29 drm_bridge_to_panel_bridge(struct drm_bridge *bridge)
30 {
31         return container_of(bridge, struct panel_bridge, bridge);
32 }
33
34 static inline struct panel_bridge *
35 drm_connector_to_panel_bridge(struct drm_connector *connector)
36 {
37         return container_of(connector, struct panel_bridge, connector);
38 }
39
40 static int panel_bridge_connector_get_modes(struct drm_connector *connector)
41 {
42         struct panel_bridge *panel_bridge =
43                 drm_connector_to_panel_bridge(connector);
44
45         return drm_panel_get_modes(panel_bridge->panel, connector);
46 }
47
48 static const struct drm_connector_helper_funcs
49 panel_bridge_connector_helper_funcs = {
50         .get_modes = panel_bridge_connector_get_modes,
51 };
52
53 static const struct drm_connector_funcs panel_bridge_connector_funcs = {
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,
59 };
60
61 static int panel_bridge_attach(struct drm_bridge *bridge,
62                                enum drm_bridge_attach_flags flags)
63 {
64         struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
65         struct drm_connector *connector = &panel_bridge->connector;
66         struct drm_panel *panel = panel_bridge->panel;
67         struct drm_device *drm_dev = bridge->dev;
68         int ret;
69
70         panel_bridge->link = device_link_add(drm_dev->dev, panel->dev,
71                                              DL_FLAG_STATELESS);
72         if (!panel_bridge->link) {
73                 DRM_ERROR("Failed to add device link between %s and %s\n",
74                           dev_name(drm_dev->dev), dev_name(panel->dev));
75                 return -EINVAL;
76         }
77
78         if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)
79                 return 0;
80
81         if (!bridge->encoder) {
82                 DRM_ERROR("Missing encoder\n");
83                 device_link_del(panel_bridge->link);
84                 return -ENODEV;
85         }
86
87         drm_connector_helper_add(connector,
88                                  &panel_bridge_connector_helper_funcs);
89
90         ret = drm_connector_init(bridge->dev, connector,
91                                  &panel_bridge_connector_funcs,
92                                  panel_bridge->connector_type);
93         if (ret) {
94                 DRM_ERROR("Failed to initialize connector\n");
95                 device_link_del(panel_bridge->link);
96                 return ret;
97         }
98
99         drm_panel_bridge_set_orientation(connector, bridge);
100
101         drm_connector_attach_encoder(&panel_bridge->connector,
102                                           bridge->encoder);
103
104         if (bridge->dev->registered) {
105                 if (connector->funcs->reset)
106                         connector->funcs->reset(connector);
107                 drm_connector_register(connector);
108         }
109
110         return 0;
111 }
112
113 static void panel_bridge_detach(struct drm_bridge *bridge)
114 {
115         struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
116         struct drm_connector *connector = &panel_bridge->connector;
117
118         device_link_del(panel_bridge->link);
119
120         /*
121          * Cleanup the connector if we know it was initialized.
122          *
123          * FIXME: This wouldn't be needed if the panel_bridge structure was
124          * allocated with drmm_kzalloc(). This might be tricky since the
125          * drm_device pointer can only be retrieved when the bridge is attached.
126          */
127         if (connector->dev)
128                 drm_connector_cleanup(connector);
129 }
130
131 static void panel_bridge_atomic_pre_enable(struct drm_bridge *bridge,
132                                 struct drm_bridge_state *old_bridge_state)
133 {
134         struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
135         struct drm_atomic_state *atomic_state = old_bridge_state->base.state;
136         struct drm_encoder *encoder = bridge->encoder;
137         struct drm_crtc *crtc;
138         struct drm_crtc_state *old_crtc_state;
139
140         crtc = drm_atomic_get_new_crtc_for_encoder(atomic_state, encoder);
141         if (!crtc)
142                 return;
143
144         old_crtc_state = drm_atomic_get_old_crtc_state(atomic_state, crtc);
145         if (old_crtc_state && old_crtc_state->self_refresh_active)
146                 return;
147
148         drm_panel_prepare(panel_bridge->panel);
149 }
150
151 static void panel_bridge_atomic_enable(struct drm_bridge *bridge,
152                                 struct drm_bridge_state *old_bridge_state)
153 {
154         struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
155         struct drm_atomic_state *atomic_state = old_bridge_state->base.state;
156         struct drm_encoder *encoder = bridge->encoder;
157         struct drm_crtc *crtc;
158         struct drm_crtc_state *old_crtc_state;
159
160         crtc = drm_atomic_get_new_crtc_for_encoder(atomic_state, encoder);
161         if (!crtc)
162                 return;
163
164         old_crtc_state = drm_atomic_get_old_crtc_state(atomic_state, crtc);
165         if (old_crtc_state && old_crtc_state->self_refresh_active)
166                 return;
167
168         drm_panel_enable(panel_bridge->panel);
169 }
170
171 static void panel_bridge_atomic_disable(struct drm_bridge *bridge,
172                                 struct drm_bridge_state *old_bridge_state)
173 {
174         struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
175         struct drm_atomic_state *atomic_state = old_bridge_state->base.state;
176         struct drm_encoder *encoder = bridge->encoder;
177         struct drm_crtc *crtc;
178         struct drm_crtc_state *new_crtc_state;
179
180         crtc = drm_atomic_get_old_crtc_for_encoder(atomic_state, encoder);
181         if (!crtc)
182                 return;
183
184         new_crtc_state = drm_atomic_get_new_crtc_state(atomic_state, crtc);
185         if (new_crtc_state && new_crtc_state->self_refresh_active)
186                 return;
187
188         drm_panel_disable(panel_bridge->panel);
189 }
190
191 static void panel_bridge_atomic_post_disable(struct drm_bridge *bridge,
192                                 struct drm_bridge_state *old_bridge_state)
193 {
194         struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
195         struct drm_atomic_state *atomic_state = old_bridge_state->base.state;
196         struct drm_encoder *encoder = bridge->encoder;
197         struct drm_crtc *crtc;
198         struct drm_crtc_state *new_crtc_state;
199
200         crtc = drm_atomic_get_old_crtc_for_encoder(atomic_state, encoder);
201         if (!crtc)
202                 return;
203
204         new_crtc_state = drm_atomic_get_new_crtc_state(atomic_state, crtc);
205         if (new_crtc_state && new_crtc_state->self_refresh_active)
206                 return;
207
208         drm_panel_unprepare(panel_bridge->panel);
209 }
210
211 static int panel_bridge_get_modes(struct drm_bridge *bridge,
212                                   struct drm_connector *connector)
213 {
214         struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
215
216         return drm_panel_get_modes(panel_bridge->panel, connector);
217 }
218
219 static void panel_bridge_debugfs_init(struct drm_bridge *bridge,
220                                       struct dentry *root)
221 {
222         struct panel_bridge *panel_bridge = drm_bridge_to_panel_bridge(bridge);
223         struct drm_panel *panel = panel_bridge->panel;
224
225         root = debugfs_create_dir("panel", root);
226         if (panel->funcs->debugfs_init)
227                 panel->funcs->debugfs_init(panel, root);
228 }
229
230 static const struct drm_bridge_funcs panel_bridge_bridge_funcs = {
231         .attach = panel_bridge_attach,
232         .detach = panel_bridge_detach,
233         .atomic_pre_enable = panel_bridge_atomic_pre_enable,
234         .atomic_enable = panel_bridge_atomic_enable,
235         .atomic_disable = panel_bridge_atomic_disable,
236         .atomic_post_disable = panel_bridge_atomic_post_disable,
237         .get_modes = panel_bridge_get_modes,
238         .atomic_reset = drm_atomic_helper_bridge_reset,
239         .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
240         .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
241         .atomic_get_input_bus_fmts = drm_atomic_helper_bridge_propagate_bus_fmt,
242         .debugfs_init = panel_bridge_debugfs_init,
243 };
244
245 /**
246  * drm_bridge_is_panel - Checks if a drm_bridge is a panel_bridge.
247  *
248  * @bridge: The drm_bridge to be checked.
249  *
250  * Returns true if the bridge is a panel bridge, or false otherwise.
251  */
252 bool drm_bridge_is_panel(const struct drm_bridge *bridge)
253 {
254         return bridge->funcs == &panel_bridge_bridge_funcs;
255 }
256 EXPORT_SYMBOL(drm_bridge_is_panel);
257
258 /**
259  * drm_panel_bridge_add - Creates a &drm_bridge and &drm_connector that
260  * just calls the appropriate functions from &drm_panel.
261  *
262  * @panel: The drm_panel being wrapped.  Must be non-NULL.
263  *
264  * For drivers converting from directly using drm_panel: The expected
265  * usage pattern is that during either encoder module probe or DSI
266  * host attach, a drm_panel will be looked up through
267  * drm_of_find_panel_or_bridge().  drm_panel_bridge_add() is used to
268  * wrap that panel in the new bridge, and the result can then be
269  * passed to drm_bridge_attach().  The drm_panel_prepare() and related
270  * functions can be dropped from the encoder driver (they're now
271  * called by the KMS helpers before calling into the encoder), along
272  * with connector creation.  When done with the bridge (after
273  * drm_mode_config_cleanup() if the bridge has already been attached), then
274  * drm_panel_bridge_remove() to free it.
275  *
276  * The connector type is set to @panel->connector_type, which must be set to a
277  * known type. Calling this function with a panel whose connector type is
278  * DRM_MODE_CONNECTOR_Unknown will return ERR_PTR(-EINVAL).
279  *
280  * See devm_drm_panel_bridge_add() for an automatically managed version of this
281  * function.
282  */
283 struct drm_bridge *drm_panel_bridge_add(struct drm_panel *panel)
284 {
285         if (WARN_ON(panel->connector_type == DRM_MODE_CONNECTOR_Unknown))
286                 return ERR_PTR(-EINVAL);
287
288         return drm_panel_bridge_add_typed(panel, panel->connector_type);
289 }
290 EXPORT_SYMBOL(drm_panel_bridge_add);
291
292 /**
293  * drm_panel_bridge_add_typed - Creates a &drm_bridge and &drm_connector with
294  * an explicit connector type.
295  * @panel: The drm_panel being wrapped.  Must be non-NULL.
296  * @connector_type: The connector type (DRM_MODE_CONNECTOR_*)
297  *
298  * This is just like drm_panel_bridge_add(), but forces the connector type to
299  * @connector_type instead of infering it from the panel.
300  *
301  * This function is deprecated and should not be used in new drivers. Use
302  * drm_panel_bridge_add() instead, and fix panel drivers as necessary if they
303  * don't report a connector type.
304  */
305 struct drm_bridge *drm_panel_bridge_add_typed(struct drm_panel *panel,
306                                               u32 connector_type)
307 {
308         struct panel_bridge *panel_bridge;
309
310         if (!panel)
311                 return ERR_PTR(-EINVAL);
312
313         panel_bridge = devm_kzalloc(panel->dev, sizeof(*panel_bridge),
314                                     GFP_KERNEL);
315         if (!panel_bridge)
316                 return ERR_PTR(-ENOMEM);
317
318         panel_bridge->connector_type = connector_type;
319         panel_bridge->panel = panel;
320
321         panel_bridge->bridge.funcs = &panel_bridge_bridge_funcs;
322         panel_bridge->bridge.of_node = panel->dev->of_node;
323         panel_bridge->bridge.ops = DRM_BRIDGE_OP_MODES;
324         panel_bridge->bridge.type = connector_type;
325
326         drm_bridge_add(&panel_bridge->bridge);
327
328         return &panel_bridge->bridge;
329 }
330 EXPORT_SYMBOL(drm_panel_bridge_add_typed);
331
332 /**
333  * drm_panel_bridge_remove - Unregisters and frees a drm_bridge
334  * created by drm_panel_bridge_add().
335  *
336  * @bridge: The drm_bridge being freed.
337  */
338 void drm_panel_bridge_remove(struct drm_bridge *bridge)
339 {
340         struct panel_bridge *panel_bridge;
341
342         if (!bridge)
343                 return;
344
345         if (bridge->funcs != &panel_bridge_bridge_funcs)
346                 return;
347
348         panel_bridge = drm_bridge_to_panel_bridge(bridge);
349
350         drm_bridge_remove(bridge);
351         devm_kfree(panel_bridge->panel->dev, bridge);
352 }
353 EXPORT_SYMBOL(drm_panel_bridge_remove);
354
355 /**
356  * drm_panel_bridge_set_orientation - Set the connector's panel orientation
357  * from the bridge that can be transformed to panel bridge.
358  *
359  * @connector: The connector to be set panel orientation.
360  * @bridge: The drm_bridge to be transformed to panel bridge.
361  *
362  * Returns 0 on success, negative errno on failure.
363  */
364 int drm_panel_bridge_set_orientation(struct drm_connector *connector,
365                                      struct drm_bridge *bridge)
366 {
367         struct panel_bridge *panel_bridge;
368
369         panel_bridge = drm_bridge_to_panel_bridge(bridge);
370
371         return drm_connector_set_orientation_from_panel(connector,
372                                                         panel_bridge->panel);
373 }
374 EXPORT_SYMBOL(drm_panel_bridge_set_orientation);
375
376 static void devm_drm_panel_bridge_release(struct device *dev, void *res)
377 {
378         struct drm_bridge **bridge = res;
379
380         drm_panel_bridge_remove(*bridge);
381 }
382
383 /**
384  * devm_drm_panel_bridge_add - Creates a managed &drm_bridge and &drm_connector
385  * that just calls the appropriate functions from &drm_panel.
386  * @dev: device to tie the bridge lifetime to
387  * @panel: The drm_panel being wrapped.  Must be non-NULL.
388  *
389  * This is the managed version of drm_panel_bridge_add() which automatically
390  * calls drm_panel_bridge_remove() when @dev is unbound.
391  */
392 struct drm_bridge *devm_drm_panel_bridge_add(struct device *dev,
393                                              struct drm_panel *panel)
394 {
395         if (WARN_ON(panel->connector_type == DRM_MODE_CONNECTOR_Unknown))
396                 return ERR_PTR(-EINVAL);
397
398         return devm_drm_panel_bridge_add_typed(dev, panel,
399                                                panel->connector_type);
400 }
401 EXPORT_SYMBOL(devm_drm_panel_bridge_add);
402
403 /**
404  * devm_drm_panel_bridge_add_typed - Creates a managed &drm_bridge and
405  * &drm_connector with an explicit connector type.
406  * @dev: device to tie the bridge lifetime to
407  * @panel: The drm_panel being wrapped.  Must be non-NULL.
408  * @connector_type: The connector type (DRM_MODE_CONNECTOR_*)
409  *
410  * This is just like devm_drm_panel_bridge_add(), but forces the connector type
411  * to @connector_type instead of infering it from the panel.
412  *
413  * This function is deprecated and should not be used in new drivers. Use
414  * devm_drm_panel_bridge_add() instead, and fix panel drivers as necessary if
415  * they don't report a connector type.
416  */
417 struct drm_bridge *devm_drm_panel_bridge_add_typed(struct device *dev,
418                                                    struct drm_panel *panel,
419                                                    u32 connector_type)
420 {
421         struct drm_bridge **ptr, *bridge;
422
423         ptr = devres_alloc(devm_drm_panel_bridge_release, sizeof(*ptr),
424                            GFP_KERNEL);
425         if (!ptr)
426                 return ERR_PTR(-ENOMEM);
427
428         bridge = drm_panel_bridge_add_typed(panel, connector_type);
429         if (IS_ERR(bridge)) {
430                 devres_free(ptr);
431                 return bridge;
432         }
433
434         bridge->pre_enable_prev_first = panel->prepare_prev_first;
435
436         *ptr = bridge;
437         devres_add(dev, ptr);
438
439         return bridge;
440 }
441 EXPORT_SYMBOL(devm_drm_panel_bridge_add_typed);
442
443 static void drmm_drm_panel_bridge_release(struct drm_device *drm, void *ptr)
444 {
445         struct drm_bridge *bridge = ptr;
446
447         drm_panel_bridge_remove(bridge);
448 }
449
450 /**
451  * drmm_panel_bridge_add - Creates a DRM-managed &drm_bridge and
452  *                         &drm_connector that just calls the
453  *                         appropriate functions from &drm_panel.
454  *
455  * @drm: DRM device to tie the bridge lifetime to
456  * @panel: The drm_panel being wrapped.  Must be non-NULL.
457  *
458  * This is the DRM-managed version of drm_panel_bridge_add() which
459  * automatically calls drm_panel_bridge_remove() when @dev is cleaned
460  * up.
461  */
462 struct drm_bridge *drmm_panel_bridge_add(struct drm_device *drm,
463                                          struct drm_panel *panel)
464 {
465         struct drm_bridge *bridge;
466         int ret;
467
468         bridge = drm_panel_bridge_add_typed(panel, panel->connector_type);
469         if (IS_ERR(bridge))
470                 return bridge;
471
472         ret = drmm_add_action_or_reset(drm, drmm_drm_panel_bridge_release,
473                                        bridge);
474         if (ret)
475                 return ERR_PTR(ret);
476
477         bridge->pre_enable_prev_first = panel->prepare_prev_first;
478
479         return bridge;
480 }
481 EXPORT_SYMBOL(drmm_panel_bridge_add);
482
483 /**
484  * drm_panel_bridge_connector - return the connector for the panel bridge
485  * @bridge: The drm_bridge.
486  *
487  * drm_panel_bridge creates the connector.
488  * This function gives external access to the connector.
489  *
490  * Returns: Pointer to drm_connector
491  */
492 struct drm_connector *drm_panel_bridge_connector(struct drm_bridge *bridge)
493 {
494         struct panel_bridge *panel_bridge;
495
496         panel_bridge = drm_bridge_to_panel_bridge(bridge);
497
498         return &panel_bridge->connector;
499 }
500 EXPORT_SYMBOL(drm_panel_bridge_connector);
501
502 #ifdef CONFIG_OF
503 /**
504  * devm_drm_of_get_bridge - Return next bridge in the chain
505  * @dev: device to tie the bridge lifetime to
506  * @np: device tree node containing encoder output ports
507  * @port: port in the device tree node
508  * @endpoint: endpoint in the device tree node
509  *
510  * Given a DT node's port and endpoint number, finds the connected node
511  * and returns the associated bridge if any, or creates and returns a
512  * drm panel bridge instance if a panel is connected.
513  *
514  * Returns a pointer to the bridge if successful, or an error pointer
515  * otherwise.
516  */
517 struct drm_bridge *devm_drm_of_get_bridge(struct device *dev,
518                                           struct device_node *np,
519                                           u32 port, u32 endpoint)
520 {
521         struct drm_bridge *bridge;
522         struct drm_panel *panel;
523         int ret;
524
525         ret = drm_of_find_panel_or_bridge(np, port, endpoint,
526                                           &panel, &bridge);
527         if (ret)
528                 return ERR_PTR(ret);
529
530         if (panel)
531                 bridge = devm_drm_panel_bridge_add(dev, panel);
532
533         return bridge;
534 }
535 EXPORT_SYMBOL(devm_drm_of_get_bridge);
536
537 /**
538  * drmm_of_get_bridge - Return next bridge in the chain
539  * @drm: device to tie the bridge lifetime to
540  * @np: device tree node containing encoder output ports
541  * @port: port in the device tree node
542  * @endpoint: endpoint in the device tree node
543  *
544  * Given a DT node's port and endpoint number, finds the connected node
545  * and returns the associated bridge if any, or creates and returns a
546  * drm panel bridge instance if a panel is connected.
547  *
548  * Returns a drmm managed pointer to the bridge if successful, or an error
549  * pointer otherwise.
550  */
551 struct drm_bridge *drmm_of_get_bridge(struct drm_device *drm,
552                                       struct device_node *np,
553                                       u32 port, u32 endpoint)
554 {
555         struct drm_bridge *bridge;
556         struct drm_panel *panel;
557         int ret;
558
559         ret = drm_of_find_panel_or_bridge(np, port, endpoint,
560                                           &panel, &bridge);
561         if (ret)
562                 return ERR_PTR(ret);
563
564         if (panel)
565                 bridge = drmm_panel_bridge_add(drm, panel);
566
567         return bridge;
568 }
569 EXPORT_SYMBOL(drmm_of_get_bridge);
570
571 #endif