xref: /linux/drivers/gpu/drm/drm_panel.c (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2013, NVIDIA Corporation.  All rights reserved.
4  * Copyright (C) 2016 Laurent Pinchart <laurent.pinchart@ideasonboard.com>
5  * Copyright (C) 2017 Broadcom
6  */
7 
8 #include <linux/backlight.h>
9 #include <linux/debugfs.h>
10 #include <linux/err.h>
11 #include <linux/export.h>
12 #include <linux/module.h>
13 #include <linux/of.h>
14 
15 #include <drm/drm_atomic_helper.h>
16 #include <drm/drm_bridge.h>
17 #include <drm/drm_connector.h>
18 #include <drm/drm_crtc.h>
19 #include <drm/drm_encoder.h>
20 #include <drm/drm_managed.h>
21 #include <drm/drm_modeset_helper_vtables.h>
22 #include <drm/drm_of.h>
23 #include <drm/drm_panel.h>
24 #include <drm/drm_print.h>
25 #include <drm/drm_probe_helper.h>
26 
27 static DEFINE_MUTEX(panel_lock);
28 static LIST_HEAD(panel_list);
29 
30 /**
31  * DOC: drm panel
32  *
33  * The DRM panel helpers allow drivers to register panel objects with a
34  * central registry and provide functions to retrieve those panels in display
35  * drivers.
36  *
37  * For easy integration into drivers using the &drm_bridge infrastructure please
38  * take look at drm_panel_bridge_add() and devm_drm_panel_bridge_add().
39  */
40 
41 static inline struct drm_panel *
42 drm_bridge_to_panel(const struct drm_bridge *bridge)
43 {
44 	return container_of(bridge, struct drm_panel, bridge);
45 }
46 
47 static inline struct drm_panel *
48 drm_connector_to_panel(const struct drm_connector *connector)
49 {
50 	return container_of(connector, struct drm_panel, connector);
51 }
52 
53 struct panel_bridge {
54 	struct drm_bridge bridge;
55 	struct drm_connector connector;
56 	struct drm_panel *panel;
57 	u32 connector_type;
58 };
59 
60 static int panel_bridge_connector_get_modes(struct drm_connector *connector)
61 {
62 	struct drm_panel *panel = drm_connector_to_panel(connector);
63 
64 	return drm_panel_get_modes(panel, connector);
65 }
66 
67 static const struct drm_connector_helper_funcs
68 panel_bridge_connector_helper_funcs = {
69 	.get_modes = panel_bridge_connector_get_modes,
70 };
71 
72 static const struct drm_connector_funcs panel_bridge_connector_funcs = {
73 	.reset = drm_atomic_helper_connector_reset,
74 	.fill_modes = drm_helper_probe_single_connector_modes,
75 	.destroy = drm_connector_cleanup,
76 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
77 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
78 };
79 
80 static int panel_bridge_attach(struct drm_bridge *bridge,
81 			       struct drm_encoder *encoder,
82 			       enum drm_bridge_attach_flags flags)
83 {
84 	struct drm_panel *panel = drm_bridge_to_panel(bridge);
85 	struct drm_connector *connector = &panel->connector;
86 	int ret;
87 
88 	if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)
89 		return 0;
90 
91 	drm_connector_helper_add(connector,
92 				 &panel_bridge_connector_helper_funcs);
93 
94 	ret = drm_connector_init(bridge->dev, connector,
95 				 &panel_bridge_connector_funcs,
96 				 panel->connector_type);
97 	if (ret) {
98 		DRM_ERROR("Failed to initialize connector\n");
99 		return ret;
100 	}
101 
102 	drm_panel_bridge_set_orientation(connector, bridge);
103 
104 	drm_connector_attach_encoder(connector, encoder);
105 
106 	if (bridge->dev->registered) {
107 		if (connector->funcs->reset)
108 			connector->funcs->reset(connector);
109 		drm_connector_register(connector);
110 	}
111 
112 	return 0;
113 }
114 
115 static void panel_bridge_detach(struct drm_bridge *bridge)
116 {
117 	struct drm_panel *panel = drm_bridge_to_panel(bridge);
118 	struct drm_connector *connector = &panel->connector;
119 
120 	/* Cleanup the connector if we know it was initialized */
121 	if (connector->dev)
122 		drm_connector_cleanup(connector);
123 }
124 
125 static void panel_bridge_atomic_pre_enable(struct drm_bridge *bridge,
126 					   struct drm_atomic_commit *atomic_state)
127 {
128 	struct drm_panel *panel = drm_bridge_to_panel(bridge);
129 	struct drm_encoder *encoder = bridge->encoder;
130 	struct drm_crtc *crtc;
131 	struct drm_crtc_state *old_crtc_state;
132 
133 	crtc = drm_atomic_get_new_crtc_for_encoder(atomic_state, encoder);
134 	if (!crtc)
135 		return;
136 
137 	old_crtc_state = drm_atomic_get_old_crtc_state(atomic_state, crtc);
138 	if (old_crtc_state && old_crtc_state->self_refresh_active)
139 		return;
140 
141 	drm_panel_prepare(panel);
142 }
143 
144 static void panel_bridge_atomic_enable(struct drm_bridge *bridge,
145 				       struct drm_atomic_commit *atomic_state)
146 {
147 	struct drm_panel *panel = drm_bridge_to_panel(bridge);
148 	struct drm_encoder *encoder = bridge->encoder;
149 	struct drm_crtc *crtc;
150 	struct drm_crtc_state *old_crtc_state;
151 
152 	crtc = drm_atomic_get_new_crtc_for_encoder(atomic_state, encoder);
153 	if (!crtc)
154 		return;
155 
156 	old_crtc_state = drm_atomic_get_old_crtc_state(atomic_state, crtc);
157 	if (old_crtc_state && old_crtc_state->self_refresh_active)
158 		return;
159 
160 	drm_panel_enable(panel);
161 }
162 
163 static void panel_bridge_atomic_disable(struct drm_bridge *bridge,
164 					struct drm_atomic_commit *atomic_state)
165 {
166 	struct drm_panel *panel = drm_bridge_to_panel(bridge);
167 	struct drm_encoder *encoder = bridge->encoder;
168 	struct drm_crtc *crtc;
169 	struct drm_crtc_state *new_crtc_state;
170 
171 	crtc = drm_atomic_get_old_crtc_for_encoder(atomic_state, encoder);
172 	if (!crtc)
173 		return;
174 
175 	new_crtc_state = drm_atomic_get_new_crtc_state(atomic_state, crtc);
176 	if (new_crtc_state && new_crtc_state->self_refresh_active)
177 		return;
178 
179 	drm_panel_disable(panel);
180 }
181 
182 static void panel_bridge_atomic_post_disable(struct drm_bridge *bridge,
183 					     struct drm_atomic_commit *atomic_state)
184 {
185 	struct drm_panel *panel = drm_bridge_to_panel(bridge);
186 	struct drm_encoder *encoder = bridge->encoder;
187 	struct drm_crtc *crtc;
188 	struct drm_crtc_state *new_crtc_state;
189 
190 	crtc = drm_atomic_get_old_crtc_for_encoder(atomic_state, encoder);
191 	if (!crtc)
192 		return;
193 
194 	new_crtc_state = drm_atomic_get_new_crtc_state(atomic_state, crtc);
195 	if (new_crtc_state && new_crtc_state->self_refresh_active)
196 		return;
197 
198 	drm_panel_unprepare(panel);
199 }
200 
201 static int panel_bridge_get_modes(struct drm_bridge *bridge,
202 				  struct drm_connector *connector)
203 {
204 	struct drm_panel *panel = drm_bridge_to_panel(bridge);
205 
206 	return drm_panel_get_modes(panel, connector);
207 }
208 
209 static void panel_bridge_debugfs_init(struct drm_bridge *bridge,
210 				      struct dentry *root)
211 {
212 	struct drm_panel *panel = drm_bridge_to_panel(bridge);
213 
214 	root = debugfs_create_dir("panel", root);
215 	if (panel->funcs->debugfs_init)
216 		panel->funcs->debugfs_init(panel, root);
217 }
218 
219 static const struct drm_bridge_funcs panel_bridge_bridge_funcs = {
220 	.attach = panel_bridge_attach,
221 	.detach = panel_bridge_detach,
222 	.atomic_pre_enable = panel_bridge_atomic_pre_enable,
223 	.atomic_enable = panel_bridge_atomic_enable,
224 	.atomic_disable = panel_bridge_atomic_disable,
225 	.atomic_post_disable = panel_bridge_atomic_post_disable,
226 	.get_modes = panel_bridge_get_modes,
227 	.atomic_create_state = drm_atomic_helper_bridge_create_state,
228 	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
229 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
230 	.atomic_get_input_bus_fmts = drm_atomic_helper_bridge_propagate_bus_fmt,
231 	.debugfs_init = panel_bridge_debugfs_init,
232 };
233 
234 /**
235  * drm_bridge_is_panel - Checks if a drm_bridge is a panel_bridge.
236  *
237  * @bridge: The drm_bridge to be checked.
238  *
239  * Returns true if the bridge is a panel bridge, or false otherwise.
240  */
241 bool drm_bridge_is_panel(const struct drm_bridge *bridge)
242 {
243 	return bridge->funcs == &panel_bridge_bridge_funcs;
244 }
245 EXPORT_SYMBOL(drm_bridge_is_panel);
246 
247 /**
248  * drm_panel_bridge_add - Creates a &drm_bridge and &drm_connector that
249  * just calls the appropriate functions from &drm_panel.
250  *
251  * @panel: The drm_panel being wrapped.  Must be non-NULL.
252  *
253  * This function is deprecated. A drm_bridge is embedded into every panel.
254  * Don't use it in new drivers and update your driver to stop using it.
255  *
256  * For drivers converting from directly using drm_panel: The expected
257  * usage pattern is that during either encoder module probe or DSI
258  * host attach, a drm_panel will be looked up through
259  * drm_of_find_panel_or_bridge().  drm_panel_bridge_add() is used to
260  * wrap that panel in the new bridge, and the result can then be
261  * passed to drm_bridge_attach().  The drm_panel_prepare() and related
262  * functions can be dropped from the encoder driver (they're now
263  * called by the KMS helpers before calling into the encoder), along
264  * with connector creation.  When done with the bridge (after
265  * drm_mode_config_cleanup() if the bridge has already been attached), then
266  * drm_panel_bridge_remove() to free it.
267  *
268  * The connector type is set to @panel->connector_type, which must be set to a
269  * known type. Calling this function with a panel whose connector type is
270  * DRM_MODE_CONNECTOR_Unknown will return ERR_PTR(-EINVAL).
271  *
272  * See devm_drm_panel_bridge_add() for an automatically managed version of this
273  * function.
274  */
275 struct drm_bridge *drm_panel_bridge_add(struct drm_panel *panel)
276 {
277 	if (WARN_ON(panel->connector_type == DRM_MODE_CONNECTOR_Unknown))
278 		return ERR_PTR(-EINVAL);
279 
280 	return drm_panel_bridge_add_typed(panel, panel->connector_type);
281 }
282 EXPORT_SYMBOL(drm_panel_bridge_add);
283 
284 /**
285  * drm_panel_bridge_add_typed - Pretend to create a &drm_bridge and &drm_connector with
286  * an explicit connector type.
287  * @panel: The drm_panel being wrapped.  Must be non-NULL.
288  * @connector_type: The connector type (DRM_MODE_CONNECTOR_*)
289  *
290  * This is just like drm_panel_bridge_add(), but forces the connector type to
291  * @connector_type instead of infering it from the panel.
292  *
293  * This function is deprecated. A drm_bridge is embedded into every panel.
294  * Don't use it in new drivers and update your driver to stop using it.
295  */
296 struct drm_bridge *drm_panel_bridge_add_typed(struct drm_panel *panel,
297 					      u32 connector_type)
298 {
299 	if (!panel)
300 		return ERR_PTR(-EINVAL);
301 
302 	return drm_bridge_get(&panel->bridge);
303 }
304 EXPORT_SYMBOL(drm_panel_bridge_add_typed);
305 
306 /**
307  * drm_panel_bridge_remove - Unregisters and frees a drm_bridge
308  * created by drm_panel_bridge_add().
309  *
310  * @bridge: The drm_bridge being freed.
311  *
312  * This function is deprecated. A drm_bridge is embedded into every panel
313  * and removed when the panel is removed.  Don't use it in new drivers and
314  * update your driver to stop using it.
315  */
316 void drm_panel_bridge_remove(struct drm_bridge *bridge)
317 {
318 	if (!bridge)
319 		return;
320 
321 	if (!drm_bridge_is_panel(bridge)) {
322 		drm_warn(bridge->dev, "%s: called on non-panel bridge!\n", __func__);
323 		return;
324 	}
325 
326 	drm_bridge_put(bridge);
327 }
328 EXPORT_SYMBOL(drm_panel_bridge_remove);
329 
330 /**
331  * drm_panel_bridge_set_orientation - Set the connector's panel orientation
332  * from the bridge that can be transformed to panel bridge.
333  *
334  * @connector: The connector to be set panel orientation.
335  * @bridge: The drm_bridge whose orientation should be set.
336  *
337  * Returns 0 on success, negative errno on failure.
338  */
339 int drm_panel_bridge_set_orientation(struct drm_connector *connector,
340 				     struct drm_bridge *bridge)
341 {
342 	struct drm_panel *panel = drm_bridge_to_panel(bridge);
343 
344 	return drm_connector_set_orientation_from_panel(connector, panel);
345 }
346 EXPORT_SYMBOL(drm_panel_bridge_set_orientation);
347 
348 static void devm_drm_panel_bridge_release(struct device *dev, void *res)
349 {
350 	struct drm_bridge *bridge = *(struct drm_bridge **)res;
351 
352 	if (!bridge)
353 		return;
354 
355 	drm_bridge_put(bridge);
356 }
357 
358 /**
359  * devm_drm_panel_bridge_add - Creates a managed &drm_bridge and &drm_connector
360  * that just calls the appropriate functions from &drm_panel.
361  * @dev: device to tie the bridge lifetime to
362  * @panel: The drm_panel being wrapped.  Must be non-NULL.
363  *
364  * This function is deprecated. A drm_bridge is embedded into every panel.
365  * Don't use it in new drivers and update your driver to stop using it.
366  *
367  * This is the managed version of drm_panel_bridge_add() which automatically
368  * calls drm_panel_bridge_remove() when @dev is unbound.
369  */
370 struct drm_bridge *devm_drm_panel_bridge_add(struct device *dev,
371 					     struct drm_panel *panel)
372 {
373 	if (WARN_ON(panel->connector_type == DRM_MODE_CONNECTOR_Unknown))
374 		return ERR_PTR(-EINVAL);
375 
376 	return devm_drm_panel_bridge_add_typed(dev, panel,
377 					       panel->connector_type);
378 }
379 EXPORT_SYMBOL(devm_drm_panel_bridge_add);
380 
381 /**
382  * devm_drm_panel_bridge_add_typed - Creates a managed &drm_bridge and
383  * &drm_connector with an explicit connector type.
384  * @dev: device to tie the bridge lifetime to
385  * @panel: The drm_panel being wrapped.  Must be non-NULL.
386  * @connector_type: The connector type (DRM_MODE_CONNECTOR_*)
387  *
388  * This is just like devm_drm_panel_bridge_add(), but forces the connector type
389  * to @connector_type instead of infering it from the panel.
390  *
391  * This function is deprecated. A drm_bridge is embedded into every panel.
392  * Don't use it in new drivers and update your driver to stop using it.
393  */
394 struct drm_bridge *devm_drm_panel_bridge_add_typed(struct device *dev,
395 						   struct drm_panel *panel,
396 						   u32 connector_type)
397 {
398 	struct drm_bridge **ptr, *bridge;
399 
400 	ptr = devres_alloc(devm_drm_panel_bridge_release, sizeof(*ptr),
401 			   GFP_KERNEL);
402 	if (!ptr)
403 		return ERR_PTR(-ENOMEM);
404 
405 	bridge = drm_panel_bridge_add_typed(panel, connector_type);
406 	if (IS_ERR(bridge)) {
407 		devres_free(ptr);
408 		return bridge;
409 	}
410 
411 	*ptr = bridge;
412 	devres_add(dev, ptr);
413 
414 	return &panel->bridge;
415 }
416 EXPORT_SYMBOL(devm_drm_panel_bridge_add_typed);
417 
418 static void drmm_drm_panel_bridge_release(struct drm_device *drm, void *ptr)
419 {
420 	struct drm_bridge *bridge = ptr;
421 
422 	drm_panel_bridge_remove(bridge);
423 }
424 
425 /**
426  * drmm_panel_bridge_add - Creates a DRM-managed &drm_bridge and
427  *                         &drm_connector that just calls the
428  *                         appropriate functions from &drm_panel.
429  *
430  * @drm: DRM device to tie the bridge lifetime to
431  * @panel: The drm_panel being wrapped.  Must be non-NULL.
432  *
433  * This function is deprecated. A drm_bridge is embedded into every panel.
434  * Don't use it in new drivers and update your driver to stop using it.
435  *
436  * This is the DRM-managed version of drm_panel_bridge_add() which
437  * automatically calls drm_panel_bridge_remove() when @dev is cleaned
438  * up.
439  */
440 struct drm_bridge *drmm_panel_bridge_add(struct drm_device *drm,
441 					 struct drm_panel *panel)
442 {
443 	struct drm_bridge *bridge;
444 	int ret;
445 
446 	bridge = drm_panel_bridge_add_typed(panel, panel->connector_type);
447 	if (IS_ERR(bridge))
448 		return bridge;
449 
450 	ret = drmm_add_action_or_reset(drm, drmm_drm_panel_bridge_release,
451 				       bridge);
452 	if (ret)
453 		return ERR_PTR(ret);
454 
455 	return bridge;
456 }
457 EXPORT_SYMBOL(drmm_panel_bridge_add);
458 
459 /**
460  * drm_panel_bridge_connector - return the connector for the panel (for
461  * legacy drivers not using DRM_BRIDGE_ATTACH_NO_CONNECTOR)
462  * @bridge: The drm_bridge.
463  *
464  * This function gives external access to the connector.
465  *
466  * Returns: Pointer to drm_connector
467  */
468 struct drm_connector *drm_panel_bridge_connector(struct drm_bridge *bridge)
469 {
470 	struct drm_panel *panel;
471 
472 	panel = drm_bridge_to_panel(bridge);
473 
474 	return &panel->connector;
475 }
476 EXPORT_SYMBOL(drm_panel_bridge_connector);
477 
478 #ifdef CONFIG_OF
479 /**
480  * devm_drm_of_get_bridge - Return next bridge in the chain
481  * @dev: device to tie the bridge lifetime to
482  * @np: device tree node containing encoder output ports
483  * @port: port in the device tree node
484  * @endpoint: endpoint in the device tree node
485  *
486  * This function is deprecated. A drm_bridge is embedded into every panel.
487  * Don't use it in new drivers and convert your driver to
488  * of_drm_get_bridge_by_endpoint().
489  *
490  * Given a DT node's port and endpoint number, finds the connected node
491  * and returns the associated bridge if any, or creates and returns a
492  * drm panel bridge instance if a panel is connected.
493  *
494  * Returns a pointer to the bridge if successful, or an error pointer
495  * otherwise.
496  */
497 struct drm_bridge *devm_drm_of_get_bridge(struct device *dev,
498 					  struct device_node *np,
499 					  u32 port, u32 endpoint)
500 {
501 	struct drm_bridge *bridge;
502 	struct drm_panel *panel;
503 	int ret;
504 
505 	ret = drm_of_find_panel_or_bridge(np, port, endpoint,
506 					  &panel, &bridge);
507 	if (ret)
508 		return ERR_PTR(ret);
509 
510 	if (panel) {
511 		bridge = devm_drm_panel_bridge_add(dev, panel);
512 		drm_panel_put(panel);
513 	}
514 
515 	return bridge;
516 }
517 EXPORT_SYMBOL(devm_drm_of_get_bridge);
518 
519 /**
520  * drmm_of_get_bridge - Return next bridge in the chain
521  * @drm: device to tie the bridge lifetime to
522  * @np: device tree node containing encoder output ports
523  * @port: port in the device tree node
524  * @endpoint: endpoint in the device tree node
525  *
526  * This function is deprecated. A drm_bridge is embedded into every panel.
527  * Don't use it in new drivers and convert your driver to
528  * of_drm_get_bridge_by_endpoint().
529  *
530  * Given a DT node's port and endpoint number, finds the connected node
531  * and returns the associated bridge if any, or creates and returns a
532  * drm panel bridge instance if a panel is connected.
533  *
534  * Returns a drmm managed pointer to the bridge if successful, or an error
535  * pointer otherwise.
536  */
537 struct drm_bridge *drmm_of_get_bridge(struct drm_device *drm,
538 				      struct device_node *np,
539 				      u32 port, u32 endpoint)
540 {
541 	struct drm_bridge *bridge;
542 	struct drm_panel *panel;
543 	int ret;
544 
545 	ret = drm_of_find_panel_or_bridge(np, port, endpoint,
546 					  &panel, &bridge);
547 	if (ret)
548 		return ERR_PTR(ret);
549 
550 	if (panel) {
551 		bridge = drmm_panel_bridge_add(drm, panel);
552 		drm_panel_put(panel);
553 	}
554 
555 	return bridge;
556 }
557 EXPORT_SYMBOL(drmm_of_get_bridge);
558 
559 #endif
560 
561 /**
562  * drm_panel_init - initialize a panel
563  * @panel: DRM panel
564  * @dev: parent device of the panel
565  * @funcs: panel operations
566  * @connector_type: the connector type (DRM_MODE_CONNECTOR_*) corresponding to
567  *	the panel interface (must NOT be DRM_MODE_CONNECTOR_Unknown)
568  *
569  * Initialize the panel structure for subsequent registration with
570  * drm_panel_add().
571  */
572 static void drm_panel_init(struct drm_panel *panel, struct device *dev,
573 			   const struct drm_panel_funcs *funcs,
574 			   int connector_type)
575 {
576 	if (connector_type == DRM_MODE_CONNECTOR_Unknown)
577 		DRM_WARN("%s: %s: a valid connector type is required!\n", __func__, dev_name(dev));
578 
579 	INIT_LIST_HEAD(&panel->list);
580 	INIT_LIST_HEAD(&panel->followers);
581 	mutex_init(&panel->follower_lock);
582 	panel->dev = dev;
583 	panel->funcs = funcs;
584 	panel->connector_type = connector_type;
585 }
586 
587 /**
588  * drm_panel_add - add a panel to the global registry
589  * @panel: panel to add
590  *
591  * Add a panel to the global registry so that it can be looked
592  * up by display drivers. The panel to be added must have been
593  * allocated by devm_drm_panel_alloc().
594  */
595 void drm_panel_add(struct drm_panel *panel)
596 {
597 	drm_panel_get(panel);
598 	mutex_lock(&panel_lock);
599 	list_add_tail(&panel->list, &panel_list);
600 	mutex_unlock(&panel_lock);
601 
602 	panel->bridge.of_node = panel->dev->of_node;
603 	panel->bridge.ops = DRM_BRIDGE_OP_MODES;
604 	panel->bridge.type = panel->connector_type;
605 	panel->bridge.pre_enable_prev_first = panel->prepare_prev_first;
606 
607 	drm_bridge_add(&panel->bridge);
608 }
609 EXPORT_SYMBOL(drm_panel_add);
610 
611 /**
612  * drm_panel_remove - remove a panel from the global registry
613  * @panel: DRM panel
614  *
615  * Removes a panel from the global registry.
616  */
617 void drm_panel_remove(struct drm_panel *panel)
618 {
619 	drm_bridge_remove(&panel->bridge);
620 	mutex_lock(&panel_lock);
621 	list_del_init(&panel->list);
622 	mutex_unlock(&panel_lock);
623 	drm_panel_put(panel);
624 }
625 EXPORT_SYMBOL(drm_panel_remove);
626 
627 static void drm_panel_add_release(void *data)
628 {
629 	drm_panel_remove(data);
630 }
631 
632 /**
633  * devm_drm_panel_add - add a panel to the global registry using devres
634  * @dev: device to which the panel is attached
635  * @panel: panel to add
636  *
637  * Add a panel to the global registry so that it can be looked
638  * up by display drivers. The panel to be added must have been
639  * allocated by devm_drm_panel_alloc(). Unlike drm_panel_add() with this
640  * function there is no need to call drm_panel_remove(), it will be called
641  * automatically.
642  */
643 int devm_drm_panel_add(struct device *dev, struct drm_panel *panel)
644 {
645 	drm_panel_add(panel);
646 
647 	return devm_add_action_or_reset(dev, drm_panel_add_release, panel);
648 }
649 EXPORT_SYMBOL(devm_drm_panel_add);
650 
651 /**
652  * drm_panel_prepare - power on a panel
653  * @panel: DRM panel
654  *
655  * Calling this function will enable power and deassert any reset signals to
656  * the panel. After this has completed it is possible to communicate with any
657  * integrated circuitry via a command bus. This function cannot fail (as it is
658  * called from the pre_enable call chain). There will always be a call to
659  * drm_panel_disable() afterwards.
660  */
661 void drm_panel_prepare(struct drm_panel *panel)
662 {
663 	struct drm_panel_follower *follower;
664 	int ret;
665 
666 	if (!panel)
667 		return;
668 
669 	if (panel->prepared) {
670 		dev_warn(panel->dev, "Skipping prepare of already prepared panel\n");
671 		return;
672 	}
673 
674 	mutex_lock(&panel->follower_lock);
675 
676 	if (panel->funcs && panel->funcs->prepare) {
677 		ret = panel->funcs->prepare(panel);
678 		if (ret < 0)
679 			goto exit;
680 	}
681 	panel->prepared = true;
682 
683 	list_for_each_entry(follower, &panel->followers, list) {
684 		if (!follower->funcs->panel_prepared)
685 			continue;
686 
687 		ret = follower->funcs->panel_prepared(follower);
688 		if (ret < 0)
689 			dev_info(panel->dev, "%ps failed: %d\n",
690 				 follower->funcs->panel_prepared, ret);
691 	}
692 
693 exit:
694 	mutex_unlock(&panel->follower_lock);
695 }
696 EXPORT_SYMBOL(drm_panel_prepare);
697 
698 /**
699  * drm_panel_unprepare - power off a panel
700  * @panel: DRM panel
701  *
702  * Calling this function will completely power off a panel (assert the panel's
703  * reset, turn off power supplies, ...). After this function has completed, it
704  * is usually no longer possible to communicate with the panel until another
705  * call to drm_panel_prepare().
706  */
707 void drm_panel_unprepare(struct drm_panel *panel)
708 {
709 	struct drm_panel_follower *follower;
710 	int ret;
711 
712 	if (!panel)
713 		return;
714 
715 	/*
716 	 * If you are seeing the warning below it likely means one of two things:
717 	 * - Your panel driver incorrectly calls drm_panel_unprepare() in its
718 	 *   shutdown routine. You should delete this.
719 	 * - You are using panel-edp or panel-simple and your DRM modeset
720 	 *   driver's shutdown() callback happened after the panel's shutdown().
721 	 *   In this case the warning is harmless though ideally you should
722 	 *   figure out how to reverse the order of the shutdown() callbacks.
723 	 */
724 	if (!panel->prepared) {
725 		dev_warn(panel->dev, "Skipping unprepare of already unprepared panel\n");
726 		return;
727 	}
728 
729 	mutex_lock(&panel->follower_lock);
730 
731 	list_for_each_entry(follower, &panel->followers, list) {
732 		if (!follower->funcs->panel_unpreparing)
733 			continue;
734 
735 		ret = follower->funcs->panel_unpreparing(follower);
736 		if (ret < 0)
737 			dev_info(panel->dev, "%ps failed: %d\n",
738 				 follower->funcs->panel_unpreparing, ret);
739 	}
740 
741 	if (panel->funcs && panel->funcs->unprepare) {
742 		ret = panel->funcs->unprepare(panel);
743 		if (ret < 0)
744 			goto exit;
745 	}
746 	panel->prepared = false;
747 
748 exit:
749 	mutex_unlock(&panel->follower_lock);
750 }
751 EXPORT_SYMBOL(drm_panel_unprepare);
752 
753 /**
754  * drm_panel_enable - enable a panel
755  * @panel: DRM panel
756  *
757  * Calling this function will cause the panel display drivers to be turned on
758  * and the backlight to be enabled. Content will be visible on screen after
759  * this call completes. This function cannot fail (as it is called from the
760  * enable call chain). There will always be a call to drm_panel_disable()
761  * afterwards.
762  */
763 void drm_panel_enable(struct drm_panel *panel)
764 {
765 	struct drm_panel_follower *follower;
766 	int ret;
767 
768 	if (!panel)
769 		return;
770 
771 	if (panel->enabled) {
772 		dev_warn(panel->dev, "Skipping enable of already enabled panel\n");
773 		return;
774 	}
775 
776 	mutex_lock(&panel->follower_lock);
777 
778 	if (panel->funcs && panel->funcs->enable) {
779 		ret = panel->funcs->enable(panel);
780 		if (ret < 0)
781 			goto exit;
782 	}
783 	panel->enabled = true;
784 
785 	ret = backlight_enable(panel->backlight);
786 	if (ret < 0)
787 		DRM_DEV_INFO(panel->dev, "failed to enable backlight: %d\n",
788 			     ret);
789 
790 	list_for_each_entry(follower, &panel->followers, list) {
791 		if (!follower->funcs->panel_enabled)
792 			continue;
793 
794 		ret = follower->funcs->panel_enabled(follower);
795 		if (ret < 0)
796 			dev_info(panel->dev, "%ps failed: %d\n",
797 				 follower->funcs->panel_enabled, ret);
798 	}
799 
800 exit:
801 	mutex_unlock(&panel->follower_lock);
802 }
803 EXPORT_SYMBOL(drm_panel_enable);
804 
805 /**
806  * drm_panel_disable - disable a panel
807  * @panel: DRM panel
808  *
809  * This will typically turn off the panel's backlight or disable the display
810  * drivers. For smart panels it should still be possible to communicate with
811  * the integrated circuitry via any command bus after this call.
812  */
813 void drm_panel_disable(struct drm_panel *panel)
814 {
815 	struct drm_panel_follower *follower;
816 	int ret;
817 
818 	if (!panel)
819 		return;
820 
821 	/*
822 	 * If you are seeing the warning below it likely means one of two things:
823 	 * - Your panel driver incorrectly calls drm_panel_disable() in its
824 	 *   shutdown routine. You should delete this.
825 	 * - You are using panel-edp or panel-simple and your DRM modeset
826 	 *   driver's shutdown() callback happened after the panel's shutdown().
827 	 *   In this case the warning is harmless though ideally you should
828 	 *   figure out how to reverse the order of the shutdown() callbacks.
829 	 */
830 	if (!panel->enabled) {
831 		dev_warn(panel->dev, "Skipping disable of already disabled panel\n");
832 		return;
833 	}
834 
835 	mutex_lock(&panel->follower_lock);
836 
837 	list_for_each_entry(follower, &panel->followers, list) {
838 		if (!follower->funcs->panel_disabling)
839 			continue;
840 
841 		ret = follower->funcs->panel_disabling(follower);
842 		if (ret < 0)
843 			dev_info(panel->dev, "%ps failed: %d\n",
844 				 follower->funcs->panel_disabling, ret);
845 	}
846 
847 	ret = backlight_disable(panel->backlight);
848 	if (ret < 0)
849 		DRM_DEV_INFO(panel->dev, "failed to disable backlight: %d\n",
850 			     ret);
851 
852 	if (panel->funcs && panel->funcs->disable) {
853 		ret = panel->funcs->disable(panel);
854 		if (ret < 0)
855 			goto exit;
856 	}
857 	panel->enabled = false;
858 
859 exit:
860 	mutex_unlock(&panel->follower_lock);
861 }
862 EXPORT_SYMBOL(drm_panel_disable);
863 
864 /**
865  * drm_panel_get_modes - probe the available display modes of a panel
866  * @panel: DRM panel
867  * @connector: DRM connector
868  *
869  * The modes probed from the panel are automatically added to the connector
870  * that the panel is attached to.
871  *
872  * Return: The number of modes available from the panel on success, or 0 on
873  * failure (no modes).
874  */
875 int drm_panel_get_modes(struct drm_panel *panel,
876 			struct drm_connector *connector)
877 {
878 	if (!panel)
879 		return 0;
880 
881 	if (panel->funcs && panel->funcs->get_modes) {
882 		int num;
883 
884 		num = panel->funcs->get_modes(panel, connector);
885 		if (num > 0)
886 			return num;
887 	}
888 
889 	return 0;
890 }
891 EXPORT_SYMBOL(drm_panel_get_modes);
892 
893 /**
894  * drm_panel_get - Acquire a panel reference
895  * @panel: DRM panel
896  *
897  * This function increments the panel's refcount.
898  * Returns:
899  * Pointer to @panel
900  */
901 struct drm_panel *drm_panel_get(struct drm_panel *panel)
902 {
903 	if (panel)
904 		drm_bridge_get(&panel->bridge);
905 
906 	return panel;
907 }
908 EXPORT_SYMBOL(drm_panel_get);
909 
910 /**
911  * drm_panel_put - Release a panel reference
912  * @panel: DRM panel
913  *
914  * This function decrements the panel's reference count and frees the
915  * object if the reference count drops to zero.
916  */
917 void drm_panel_put(struct drm_panel *panel)
918 {
919 	if (panel)
920 		drm_bridge_put(&panel->bridge);
921 }
922 EXPORT_SYMBOL(drm_panel_put);
923 
924 /**
925  * drm_panel_put_void - wrapper to drm_panel_put() taking a void pointer
926  *
927  * @data: pointer to @struct drm_panel, cast to a void pointer
928  *
929  * Wrapper of drm_panel_put() to be used when a function taking a void
930  * pointer is needed, for example as a devm action.
931  */
932 static void drm_panel_put_void(void *data)
933 {
934 	struct drm_panel *panel = (struct drm_panel *)data;
935 
936 	drm_panel_put(panel);
937 }
938 
939 void *__devm_drm_panel_alloc(struct device *dev, size_t size, size_t offset,
940 			     const struct drm_panel_funcs *funcs,
941 			     int connector_type)
942 {
943 	/*
944 	 * Struct embedding and offsets:
945 	 *
946 	 *    |--------------- user container struct ------------|
947 	 *    :    |---------- struct drm_panel ------------|
948 	 *    :    :    |----- struct drm_bridge ------|
949 	 *    A    B    C
950 	 *
951 	 * B - A = offset (passed as argument)
952 	 * C - B = panel_bridge_offset
953 	 * C - A = alloc_bridge_offset
954 	 */
955 	const size_t panel_bridge_offset = offsetof(struct drm_panel, bridge);
956 	const size_t alloc_bridge_offset = offset + panel_bridge_offset;
957 	struct drm_panel *panel;
958 	void *container;
959 	int err;
960 
961 	if (!funcs) {
962 		dev_warn(dev, "Missing funcs pointer\n");
963 		return ERR_PTR(-EINVAL);
964 	}
965 
966 	container = __devm_drm_bridge_alloc(dev, size, alloc_bridge_offset,
967 					    &panel_bridge_bridge_funcs);
968 	if (IS_ERR(container))
969 		return container;
970 
971 	panel = container + offset;
972 	panel->funcs = funcs;
973 	panel->bridge.of_node = dev->of_node;
974 
975 	drm_panel_get(panel);
976 
977 	err = devm_add_action_or_reset(dev, drm_panel_put_void, panel);
978 	if (err)
979 		return ERR_PTR(err);
980 
981 	drm_panel_init(panel, dev, funcs, connector_type);
982 
983 	return container;
984 }
985 EXPORT_SYMBOL(__devm_drm_panel_alloc);
986 
987 #ifdef CONFIG_OF
988 /**
989  * of_drm_find_panel - look up and reference a panel by device tree node
990  * @np: device tree node of the panel
991  *
992  * Searches the set of registered panels for one that matches the given device
993  * tree node. If a matching panel is found, the panel's reference count is
994  * incremented before returning a pointer to it. The caller must call
995  * drm_panel_put() when it no longer needs the panel pointer.
996  *
997  * Return: A reference-counted pointer to the panel registered for the specified
998  * device tree node or an ERR_PTR() if no panel matching the device tree node
999  * can be found.
1000  *
1001  * Possible error codes returned by this function:
1002  *
1003  * - EPROBE_DEFER: the panel device has not been probed yet, and the caller
1004  *   should retry later
1005  * - ENODEV: the device is not available (status != "okay" or "ok")
1006  */
1007 struct drm_panel *of_drm_find_panel(const struct device_node *np)
1008 {
1009 	struct drm_panel *panel;
1010 
1011 	if (!of_device_is_available(np))
1012 		return ERR_PTR(-ENODEV);
1013 
1014 	mutex_lock(&panel_lock);
1015 
1016 	list_for_each_entry(panel, &panel_list, list) {
1017 		if (panel->dev->of_node == np) {
1018 			drm_panel_get(panel);
1019 			mutex_unlock(&panel_lock);
1020 			return panel;
1021 		}
1022 	}
1023 
1024 	mutex_unlock(&panel_lock);
1025 	return ERR_PTR(-EPROBE_DEFER);
1026 }
1027 EXPORT_SYMBOL(of_drm_find_panel);
1028 
1029 /**
1030  * drm_of_find_panel_or_bridge - return connected panel or bridge device
1031  * @np: device tree node containing encoder output ports
1032  * @port: port in the device tree node
1033  * @endpoint: endpoint in the device tree node
1034  * @panel: pointer to hold returned drm_panel, must not be NULL. On success
1035  *         the caller must call drm_panel_put() when done with the panel
1036  * @bridge: pointer to hold returned drm_bridge
1037  *
1038  * Given a DT node's port and endpoint number, find the connected node and
1039  * return either the associated struct drm_panel or drm_bridge device.
1040  *
1041  * This function is deprecated and should not be used in new drivers. Use
1042  * of_drm_get_bridge_by_endpoint() instead when not looking for a panel, or
1043  * devm_drm_of_get_bridge() otherwise.
1044  *
1045  * Returns zero if successful, or one of the standard error codes if it fails.
1046  */
1047 int drm_of_find_panel_or_bridge(const struct device_node *np,
1048 				int port, int endpoint,
1049 				struct drm_panel **panel,
1050 				struct drm_bridge **bridge)
1051 {
1052 	if (WARN_ON(!panel))
1053 		return -EINVAL;
1054 
1055 	*panel = NULL;
1056 	if (bridge)
1057 		*bridge = NULL;
1058 
1059 	/*
1060 	 * of_graph_get_remote_node() produces a noisy error message if port
1061 	 * node isn't found and the absence of the port is a legit case here,
1062 	 * so at first we silently check whether a graph is present in the
1063 	 * device-tree node.
1064 	 */
1065 	if (!of_graph_is_present(np))
1066 		return -ENODEV;
1067 
1068 	struct device_node *remote __free(device_node) =
1069 		of_graph_get_remote_node(np, port, endpoint);
1070 	if (!remote)
1071 		return -ENODEV;
1072 
1073 	*panel = of_drm_find_panel(remote);
1074 	if (!IS_ERR(*panel))
1075 		return 0;
1076 
1077 	*panel = NULL;
1078 
1079 	if (bridge) {
1080 		/* No panel found yet, check for a bridge next. */
1081 		*bridge = of_drm_find_bridge(remote);
1082 		if (*bridge)
1083 			return 0;
1084 
1085 		*bridge = NULL;
1086 	}
1087 
1088 	return -EPROBE_DEFER;
1089 }
1090 EXPORT_SYMBOL_GPL(drm_of_find_panel_or_bridge);
1091 #endif
1092 
1093 /*
1094  * Find panel by fwnode, returning a counted reference.
1095  *
1096  * Behaves identically to of_drm_find_panel(). On success the returned
1097  * pointer has been passed through drm_panel_get(); the caller must call
1098  * drm_panel_put() when done with it.
1099  */
1100 static struct drm_panel *find_panel_by_fwnode(const struct fwnode_handle *fwnode)
1101 {
1102 	struct drm_panel *panel;
1103 
1104 	if (!fwnode_device_is_available(fwnode))
1105 		return ERR_PTR(-ENODEV);
1106 
1107 	mutex_lock(&panel_lock);
1108 
1109 	list_for_each_entry(panel, &panel_list, list) {
1110 		if (dev_fwnode(panel->dev) == fwnode) {
1111 			drm_panel_get(panel);
1112 			mutex_unlock(&panel_lock);
1113 			return panel;
1114 		}
1115 	}
1116 
1117 	mutex_unlock(&panel_lock);
1118 
1119 	return ERR_PTR(-EPROBE_DEFER);
1120 }
1121 
1122 /* Find panel by follower device */
1123 static struct drm_panel *find_panel_by_dev(struct device *follower_dev)
1124 {
1125 	struct fwnode_handle *fwnode;
1126 	struct drm_panel *panel;
1127 
1128 	fwnode = fwnode_find_reference(dev_fwnode(follower_dev), "panel", 0);
1129 	if (IS_ERR(fwnode))
1130 		return ERR_PTR(-ENODEV);
1131 
1132 	panel = find_panel_by_fwnode(fwnode);
1133 	fwnode_handle_put(fwnode);
1134 
1135 	return panel;
1136 }
1137 
1138 /**
1139  * drm_is_panel_follower() - Check if the device is a panel follower
1140  * @dev: The 'struct device' to check
1141  *
1142  * This checks to see if a device needs to be power sequenced together with
1143  * a panel using the panel follower API.
1144  *
1145  * The "panel" property of the follower points to the panel to be followed.
1146  *
1147  * Return: true if we should be power sequenced with a panel; false otherwise.
1148  */
1149 bool drm_is_panel_follower(struct device *dev)
1150 {
1151 	/*
1152 	 * The "panel" property is actually a phandle, but for simplicity we
1153 	 * don't bother trying to parse it here. We just need to know if the
1154 	 * property is there.
1155 	 */
1156 	return device_property_present(dev, "panel");
1157 }
1158 EXPORT_SYMBOL(drm_is_panel_follower);
1159 
1160 /**
1161  * drm_panel_add_follower() - Register something to follow panel state.
1162  * @follower_dev: The 'struct device' for the follower.
1163  * @follower:     The panel follower descriptor for the follower.
1164  *
1165  * A panel follower is called right after preparing/enabling the panel and right
1166  * before unpreparing/disabling the panel. It's primary intention is to power on
1167  * an associated touchscreen, though it could be used for any similar devices.
1168  * Multiple devices are allowed the follow the same panel.
1169  *
1170  * If a follower is added to a panel that's already been prepared/enabled, the
1171  * follower's prepared/enabled callback is called right away.
1172  *
1173  * The "panel" property of the follower points to the panel to be followed.
1174  *
1175  * Return: 0 or an error code. Note that -ENODEV means that we detected that
1176  *         follower_dev is not actually following a panel. The caller may
1177  *         choose to ignore this return value if following a panel is optional.
1178  */
1179 int drm_panel_add_follower(struct device *follower_dev,
1180 			   struct drm_panel_follower *follower)
1181 {
1182 	struct drm_panel *panel;
1183 	int ret;
1184 
1185 	panel = find_panel_by_dev(follower_dev);
1186 	if (IS_ERR(panel))
1187 		return PTR_ERR(panel);
1188 
1189 	get_device(panel->dev);
1190 	follower->panel = panel;
1191 
1192 	mutex_lock(&panel->follower_lock);
1193 
1194 	list_add_tail(&follower->list, &panel->followers);
1195 	if (panel->prepared && follower->funcs->panel_prepared) {
1196 		ret = follower->funcs->panel_prepared(follower);
1197 		if (ret < 0)
1198 			dev_info(panel->dev, "%ps failed: %d\n",
1199 				 follower->funcs->panel_prepared, ret);
1200 	}
1201 	if (panel->enabled && follower->funcs->panel_enabled) {
1202 		ret = follower->funcs->panel_enabled(follower);
1203 		if (ret < 0)
1204 			dev_info(panel->dev, "%ps failed: %d\n",
1205 				 follower->funcs->panel_enabled, ret);
1206 	}
1207 
1208 	mutex_unlock(&panel->follower_lock);
1209 
1210 	return 0;
1211 }
1212 EXPORT_SYMBOL(drm_panel_add_follower);
1213 
1214 /**
1215  * drm_panel_remove_follower() - Reverse drm_panel_add_follower().
1216  * @follower:     The panel follower descriptor for the follower.
1217  *
1218  * Undo drm_panel_add_follower(). This includes calling the follower's
1219  * unpreparing/disabling function if we're removed from a panel that's currently
1220  * prepared/enabled.
1221  *
1222  * Return: 0 or an error code.
1223  */
1224 void drm_panel_remove_follower(struct drm_panel_follower *follower)
1225 {
1226 	struct drm_panel *panel = follower->panel;
1227 	int ret;
1228 
1229 	mutex_lock(&panel->follower_lock);
1230 
1231 	if (panel->enabled && follower->funcs->panel_disabling) {
1232 		ret = follower->funcs->panel_disabling(follower);
1233 		if (ret < 0)
1234 			dev_info(panel->dev, "%ps failed: %d\n",
1235 				 follower->funcs->panel_disabling, ret);
1236 	}
1237 	if (panel->prepared && follower->funcs->panel_unpreparing) {
1238 		ret = follower->funcs->panel_unpreparing(follower);
1239 		if (ret < 0)
1240 			dev_info(panel->dev, "%ps failed: %d\n",
1241 				 follower->funcs->panel_unpreparing, ret);
1242 	}
1243 	list_del_init(&follower->list);
1244 
1245 	mutex_unlock(&panel->follower_lock);
1246 
1247 	put_device(panel->dev);
1248 	drm_panel_put(panel);
1249 }
1250 EXPORT_SYMBOL(drm_panel_remove_follower);
1251 
1252 static void drm_panel_remove_follower_void(void *follower)
1253 {
1254 	drm_panel_remove_follower(follower);
1255 }
1256 
1257 /**
1258  * devm_drm_panel_add_follower() - devm version of drm_panel_add_follower()
1259  * @follower_dev: The 'struct device' for the follower.
1260  * @follower:     The panel follower descriptor for the follower.
1261  *
1262  * Handles calling drm_panel_remove_follower() using devm on the follower_dev.
1263  *
1264  * Return: 0 or an error code.
1265  */
1266 int devm_drm_panel_add_follower(struct device *follower_dev,
1267 				struct drm_panel_follower *follower)
1268 {
1269 	int ret;
1270 
1271 	ret = drm_panel_add_follower(follower_dev, follower);
1272 	if (ret)
1273 		return ret;
1274 
1275 	return devm_add_action_or_reset(follower_dev,
1276 					drm_panel_remove_follower_void, follower);
1277 }
1278 EXPORT_SYMBOL(devm_drm_panel_add_follower);
1279 
1280 #if IS_REACHABLE(CONFIG_BACKLIGHT_CLASS_DEVICE)
1281 /**
1282  * drm_panel_of_backlight - use backlight device node for backlight
1283  * @panel: DRM panel
1284  *
1285  * Use this function to enable backlight handling if your panel
1286  * uses device tree and has a backlight phandle.
1287  *
1288  * When the panel is enabled backlight will be enabled after a
1289  * successful call to &drm_panel_funcs.enable()
1290  *
1291  * When the panel is disabled backlight will be disabled before the
1292  * call to &drm_panel_funcs.disable().
1293  *
1294  * A typical implementation for a panel driver supporting device tree
1295  * will call this function at probe time. Backlight will then be handled
1296  * transparently without requiring any intervention from the driver.
1297  *
1298  * Return: 0 on success or a negative error code on failure.
1299  */
1300 int drm_panel_of_backlight(struct drm_panel *panel)
1301 {
1302 	struct backlight_device *backlight;
1303 
1304 	if (!panel || !panel->dev)
1305 		return -EINVAL;
1306 
1307 	backlight = devm_of_find_backlight(panel->dev);
1308 
1309 	if (IS_ERR(backlight))
1310 		return PTR_ERR(backlight);
1311 
1312 	panel->backlight = backlight;
1313 	return 0;
1314 }
1315 EXPORT_SYMBOL(drm_panel_of_backlight);
1316 #endif
1317 
1318 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
1319 MODULE_DESCRIPTION("DRM panel infrastructure");
1320 MODULE_LICENSE("GPL and additional rights");
1321