11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2bb5cdf8dSAndrew F. Davis /* 3bb5cdf8dSAndrew F. Davis * OMAP Display Subsystem Base 4bb5cdf8dSAndrew F. Davis * 51b409fdaSAlexander A. Klimov * Copyright (C) 2015-2017 Texas Instruments Incorporated - https://www.ti.com/ 6bb5cdf8dSAndrew F. Davis */ 7bb5cdf8dSAndrew F. Davis 8a99ac0d9STomi Valkeinen #include <linux/kernel.h> 96a7c5a22SLaurent Pinchart #include <linux/list.h> 10a99ac0d9STomi Valkeinen #include <linux/module.h> 116a7c5a22SLaurent Pinchart #include <linux/mutex.h> 121e08c822SPeter Ujfalusi #include <linux/of.h> 131e08c822SPeter Ujfalusi #include <linux/of_graph.h> 1479107f27SLaurent Pinchart #include <linux/platform_device.h> 15d3541ca8SLaurent Pinchart 16d3541ca8SLaurent Pinchart #include "dss.h" 171e08c822SPeter Ujfalusi #include "omapdss.h" 18a99ac0d9STomi Valkeinen 1950638ae5SLaurent Pinchart struct dispc_device *dispc_get_dispc(struct dss_device *dss) 2050638ae5SLaurent Pinchart { 2150638ae5SLaurent Pinchart return dss->dispc; 2250638ae5SLaurent Pinchart } 2350638ae5SLaurent Pinchart 24d3541ca8SLaurent Pinchart const struct dispc_ops *dispc_get_ops(struct dss_device *dss) 258a13398cSTomi Valkeinen { 26d3541ca8SLaurent Pinchart return dss->dispc_ops; 278a13398cSTomi Valkeinen } 286a7c5a22SLaurent Pinchart 296a7c5a22SLaurent Pinchart /* ----------------------------------------------------------------------------- 306a7c5a22SLaurent Pinchart * OMAP DSS Devices Handling 316a7c5a22SLaurent Pinchart */ 326a7c5a22SLaurent Pinchart 336a7c5a22SLaurent Pinchart static LIST_HEAD(omapdss_devices_list); 346a7c5a22SLaurent Pinchart static DEFINE_MUTEX(omapdss_devices_lock); 356a7c5a22SLaurent Pinchart 366a7c5a22SLaurent Pinchart void omapdss_device_register(struct omap_dss_device *dssdev) 376a7c5a22SLaurent Pinchart { 386a7c5a22SLaurent Pinchart mutex_lock(&omapdss_devices_lock); 396a7c5a22SLaurent Pinchart list_add_tail(&dssdev->list, &omapdss_devices_list); 406a7c5a22SLaurent Pinchart mutex_unlock(&omapdss_devices_lock); 416a7c5a22SLaurent Pinchart } 426a7c5a22SLaurent Pinchart 436a7c5a22SLaurent Pinchart void omapdss_device_unregister(struct omap_dss_device *dssdev) 446a7c5a22SLaurent Pinchart { 456a7c5a22SLaurent Pinchart mutex_lock(&omapdss_devices_lock); 466a7c5a22SLaurent Pinchart list_del(&dssdev->list); 476a7c5a22SLaurent Pinchart mutex_unlock(&omapdss_devices_lock); 486a7c5a22SLaurent Pinchart } 496a7c5a22SLaurent Pinchart 509184f8d9SLaurent Pinchart static bool omapdss_device_is_registered(struct device_node *node) 519184f8d9SLaurent Pinchart { 529184f8d9SLaurent Pinchart struct omap_dss_device *dssdev; 539184f8d9SLaurent Pinchart bool found = false; 549184f8d9SLaurent Pinchart 559184f8d9SLaurent Pinchart mutex_lock(&omapdss_devices_lock); 569184f8d9SLaurent Pinchart 579184f8d9SLaurent Pinchart list_for_each_entry(dssdev, &omapdss_devices_list, list) { 589184f8d9SLaurent Pinchart if (dssdev->dev->of_node == node) { 599184f8d9SLaurent Pinchart found = true; 609184f8d9SLaurent Pinchart break; 619184f8d9SLaurent Pinchart } 629184f8d9SLaurent Pinchart } 639184f8d9SLaurent Pinchart 649184f8d9SLaurent Pinchart mutex_unlock(&omapdss_devices_lock); 659184f8d9SLaurent Pinchart return found; 669184f8d9SLaurent Pinchart } 679184f8d9SLaurent Pinchart 68c1dfe721SLaurent Pinchart struct omap_dss_device *omapdss_device_get(struct omap_dss_device *dssdev) 69c1dfe721SLaurent Pinchart { 70*55b68fb8STomi Valkeinen if (get_device(dssdev->dev) == NULL) 71c1dfe721SLaurent Pinchart return NULL; 72c1dfe721SLaurent Pinchart 73c1dfe721SLaurent Pinchart return dssdev; 74c1dfe721SLaurent Pinchart } 75c1dfe721SLaurent Pinchart 76c1dfe721SLaurent Pinchart void omapdss_device_put(struct omap_dss_device *dssdev) 77c1dfe721SLaurent Pinchart { 78c1dfe721SLaurent Pinchart put_device(dssdev->dev); 79c1dfe721SLaurent Pinchart } 80c1dfe721SLaurent Pinchart 81ce69aac8SLaurent Pinchart struct omap_dss_device *omapdss_find_device_by_node(struct device_node *node) 82e10bd354SLaurent Pinchart { 83e10bd354SLaurent Pinchart struct omap_dss_device *dssdev; 84e10bd354SLaurent Pinchart 85e10bd354SLaurent Pinchart list_for_each_entry(dssdev, &omapdss_devices_list, list) { 86ce69aac8SLaurent Pinchart if (dssdev->dev->of_node == node) 87c1dfe721SLaurent Pinchart return omapdss_device_get(dssdev); 88e10bd354SLaurent Pinchart } 89e10bd354SLaurent Pinchart 90e10bd354SLaurent Pinchart return NULL; 91e10bd354SLaurent Pinchart } 92e10bd354SLaurent Pinchart 93b9f4d2ebSLaurent Pinchart /* 9419b4200dSLaurent Pinchart * Search for the next output device starting at @from. Release the reference to 9519b4200dSLaurent Pinchart * the @from device, and acquire a reference to the returned device if found. 96b9f4d2ebSLaurent Pinchart */ 9719b4200dSLaurent Pinchart struct omap_dss_device *omapdss_device_next_output(struct omap_dss_device *from) 98b9f4d2ebSLaurent Pinchart { 99b9f4d2ebSLaurent Pinchart struct omap_dss_device *dssdev; 100b9f4d2ebSLaurent Pinchart struct list_head *list; 101b9f4d2ebSLaurent Pinchart 102b9f4d2ebSLaurent Pinchart mutex_lock(&omapdss_devices_lock); 103b9f4d2ebSLaurent Pinchart 104b9f4d2ebSLaurent Pinchart if (list_empty(&omapdss_devices_list)) { 105b9f4d2ebSLaurent Pinchart dssdev = NULL; 106b9f4d2ebSLaurent Pinchart goto done; 107b9f4d2ebSLaurent Pinchart } 108b9f4d2ebSLaurent Pinchart 109b9f4d2ebSLaurent Pinchart /* 110b9f4d2ebSLaurent Pinchart * Start from the from entry if given or from omapdss_devices_list 111b9f4d2ebSLaurent Pinchart * otherwise. 112b9f4d2ebSLaurent Pinchart */ 113b9f4d2ebSLaurent Pinchart list = from ? &from->list : &omapdss_devices_list; 114b9f4d2ebSLaurent Pinchart 115b9f4d2ebSLaurent Pinchart list_for_each_entry(dssdev, list, list) { 116b9f4d2ebSLaurent Pinchart /* 117b9f4d2ebSLaurent Pinchart * Stop if we reach the omapdss_devices_list, that's the end of 118b9f4d2ebSLaurent Pinchart * the list. 119b9f4d2ebSLaurent Pinchart */ 120b9f4d2ebSLaurent Pinchart if (&dssdev->list == &omapdss_devices_list) { 121b9f4d2ebSLaurent Pinchart dssdev = NULL; 122b9f4d2ebSLaurent Pinchart goto done; 123b9f4d2ebSLaurent Pinchart } 124b9f4d2ebSLaurent Pinchart 125811860ddSSebastian Reichel if (dssdev->id && dssdev->bridge) 126b9f4d2ebSLaurent Pinchart goto done; 127b9f4d2ebSLaurent Pinchart } 128b9f4d2ebSLaurent Pinchart 129b9f4d2ebSLaurent Pinchart dssdev = NULL; 130b9f4d2ebSLaurent Pinchart 131b9f4d2ebSLaurent Pinchart done: 132b9f4d2ebSLaurent Pinchart if (from) 133c1dfe721SLaurent Pinchart omapdss_device_put(from); 134b9f4d2ebSLaurent Pinchart if (dssdev) 135c1dfe721SLaurent Pinchart omapdss_device_get(dssdev); 136b9f4d2ebSLaurent Pinchart 137b9f4d2ebSLaurent Pinchart mutex_unlock(&omapdss_devices_lock); 138b9f4d2ebSLaurent Pinchart return dssdev; 139b9f4d2ebSLaurent Pinchart } 140b9f4d2ebSLaurent Pinchart 141b49a2139SLaurent Pinchart static bool omapdss_device_is_connected(struct omap_dss_device *dssdev) 142b49a2139SLaurent Pinchart { 143df6682b4SLaurent Pinchart return dssdev->dss; 144b49a2139SLaurent Pinchart } 145b49a2139SLaurent Pinchart 146f324b279SLaurent Pinchart int omapdss_device_connect(struct dss_device *dss, 147f324b279SLaurent Pinchart struct omap_dss_device *src, 148ec727e3fSLaurent Pinchart struct omap_dss_device *dst) 149ec727e3fSLaurent Pinchart { 15079107f27SLaurent Pinchart dev_dbg(&dss->pdev->dev, "connect(%s, %s)\n", 15179107f27SLaurent Pinchart src ? dev_name(src->dev) : "NULL", 15279107f27SLaurent Pinchart dst ? dev_name(dst->dev) : "NULL"); 15379107f27SLaurent Pinchart 15479107f27SLaurent Pinchart if (!dst) { 15579107f27SLaurent Pinchart /* 15679107f27SLaurent Pinchart * The destination is NULL when the source is connected to a 157a779618bSLaurent Pinchart * bridge instead of a DSS device. Stop here, we will attach 158a779618bSLaurent Pinchart * the bridge later when we will have a DRM encoder. 15979107f27SLaurent Pinchart */ 160a779618bSLaurent Pinchart return src && src->bridge ? 0 : -EINVAL; 16179107f27SLaurent Pinchart } 1621f507968SLaurent Pinchart 163511afb44SLaurent Pinchart if (omapdss_device_is_connected(dst)) 1641f507968SLaurent Pinchart return -EBUSY; 1651f507968SLaurent Pinchart 166511afb44SLaurent Pinchart dst->dss = dss; 167f324b279SLaurent Pinchart 168fb557171SLaurent Pinchart return 0; 169ec727e3fSLaurent Pinchart } 170ec727e3fSLaurent Pinchart 171ec727e3fSLaurent Pinchart void omapdss_device_disconnect(struct omap_dss_device *src, 172ec727e3fSLaurent Pinchart struct omap_dss_device *dst) 173ec727e3fSLaurent Pinchart { 17479107f27SLaurent Pinchart struct dss_device *dss = src ? src->dss : dst->dss; 17579107f27SLaurent Pinchart 17679107f27SLaurent Pinchart dev_dbg(&dss->pdev->dev, "disconnect(%s, %s)\n", 17779107f27SLaurent Pinchart src ? dev_name(src->dev) : "NULL", 17879107f27SLaurent Pinchart dst ? dev_name(dst->dev) : "NULL"); 17979107f27SLaurent Pinchart 18079107f27SLaurent Pinchart if (!dst) { 181a779618bSLaurent Pinchart WARN_ON(!src->bridge); 18279107f27SLaurent Pinchart return; 18379107f27SLaurent Pinchart } 1841f507968SLaurent Pinchart 185511afb44SLaurent Pinchart if (!dst->id && !omapdss_device_is_connected(dst)) { 1862390fadbSSebastian Reichel WARN_ON(1); 1871f507968SLaurent Pinchart return; 1881f507968SLaurent Pinchart } 1891f507968SLaurent Pinchart 190511afb44SLaurent Pinchart dst->dss = NULL; 191ec727e3fSLaurent Pinchart } 192ec727e3fSLaurent Pinchart 1936a7c5a22SLaurent Pinchart /* ----------------------------------------------------------------------------- 1946a7c5a22SLaurent Pinchart * Components Handling 1956a7c5a22SLaurent Pinchart */ 1966a7c5a22SLaurent Pinchart 1976a7c5a22SLaurent Pinchart static struct list_head omapdss_comp_list; 1986a7c5a22SLaurent Pinchart 1996a7c5a22SLaurent Pinchart struct omapdss_comp_node { 2006a7c5a22SLaurent Pinchart struct list_head list; 2016a7c5a22SLaurent Pinchart struct device_node *node; 2026a7c5a22SLaurent Pinchart bool dss_core_component; 2034e17763cSLaurent Pinchart const char *compat; 2046a7c5a22SLaurent Pinchart }; 2056a7c5a22SLaurent Pinchart 2061e08c822SPeter Ujfalusi static bool omapdss_list_contains(const struct device_node *node) 2071e08c822SPeter Ujfalusi { 2081e08c822SPeter Ujfalusi struct omapdss_comp_node *comp; 2091e08c822SPeter Ujfalusi 2101e08c822SPeter Ujfalusi list_for_each_entry(comp, &omapdss_comp_list, list) { 2111e08c822SPeter Ujfalusi if (comp->node == node) 2121e08c822SPeter Ujfalusi return true; 2131e08c822SPeter Ujfalusi } 2141e08c822SPeter Ujfalusi 2151e08c822SPeter Ujfalusi return false; 2161e08c822SPeter Ujfalusi } 2171e08c822SPeter Ujfalusi 2181e08c822SPeter Ujfalusi static void omapdss_walk_device(struct device *dev, struct device_node *node, 2191e08c822SPeter Ujfalusi bool dss_core) 2201e08c822SPeter Ujfalusi { 2214e17763cSLaurent Pinchart struct omapdss_comp_node *comp; 2221e08c822SPeter Ujfalusi struct device_node *n; 2234e17763cSLaurent Pinchart const char *compat; 2244e17763cSLaurent Pinchart int ret; 2251e08c822SPeter Ujfalusi 2264e17763cSLaurent Pinchart ret = of_property_read_string(node, "compatible", &compat); 2274e17763cSLaurent Pinchart if (ret < 0) 2284e17763cSLaurent Pinchart return; 2294e17763cSLaurent Pinchart 2304e17763cSLaurent Pinchart comp = devm_kzalloc(dev, sizeof(*comp), GFP_KERNEL); 2311e08c822SPeter Ujfalusi if (comp) { 2321e08c822SPeter Ujfalusi comp->node = node; 2331e08c822SPeter Ujfalusi comp->dss_core_component = dss_core; 2344e17763cSLaurent Pinchart comp->compat = compat; 2351e08c822SPeter Ujfalusi list_add(&comp->list, &omapdss_comp_list); 2361e08c822SPeter Ujfalusi } 2371e08c822SPeter Ujfalusi 2381e08c822SPeter Ujfalusi /* 2391e08c822SPeter Ujfalusi * of_graph_get_remote_port_parent() prints an error if there is no 2401e08c822SPeter Ujfalusi * port/ports node. To avoid that, check first that there's the node. 2411e08c822SPeter Ujfalusi */ 2421e08c822SPeter Ujfalusi n = of_get_child_by_name(node, "ports"); 2431e08c822SPeter Ujfalusi if (!n) 2441e08c822SPeter Ujfalusi n = of_get_child_by_name(node, "port"); 2451e08c822SPeter Ujfalusi if (!n) 2461e08c822SPeter Ujfalusi return; 2471e08c822SPeter Ujfalusi 2481e08c822SPeter Ujfalusi of_node_put(n); 2491e08c822SPeter Ujfalusi 2501e08c822SPeter Ujfalusi n = NULL; 2511e08c822SPeter Ujfalusi while ((n = of_graph_get_next_endpoint(node, n)) != NULL) { 2521e08c822SPeter Ujfalusi struct device_node *pn = of_graph_get_remote_port_parent(n); 2531e08c822SPeter Ujfalusi 2541e08c822SPeter Ujfalusi if (!pn) 2551e08c822SPeter Ujfalusi continue; 2561e08c822SPeter Ujfalusi 2571e08c822SPeter Ujfalusi if (!of_device_is_available(pn) || omapdss_list_contains(pn)) { 2581e08c822SPeter Ujfalusi of_node_put(pn); 2591e08c822SPeter Ujfalusi continue; 2601e08c822SPeter Ujfalusi } 2611e08c822SPeter Ujfalusi 2621e08c822SPeter Ujfalusi omapdss_walk_device(dev, pn, false); 2631e08c822SPeter Ujfalusi } 2641e08c822SPeter Ujfalusi } 2651e08c822SPeter Ujfalusi 2661e08c822SPeter Ujfalusi void omapdss_gather_components(struct device *dev) 2671e08c822SPeter Ujfalusi { 2681e08c822SPeter Ujfalusi struct device_node *child; 2691e08c822SPeter Ujfalusi 2701e08c822SPeter Ujfalusi INIT_LIST_HEAD(&omapdss_comp_list); 2711e08c822SPeter Ujfalusi 2721e08c822SPeter Ujfalusi omapdss_walk_device(dev, dev->of_node, true); 2731e08c822SPeter Ujfalusi 2744e17763cSLaurent Pinchart for_each_available_child_of_node(dev->of_node, child) 2751e08c822SPeter Ujfalusi omapdss_walk_device(dev, child, true); 2761e08c822SPeter Ujfalusi } 2771e08c822SPeter Ujfalusi 2781e08c822SPeter Ujfalusi static bool omapdss_component_is_loaded(struct omapdss_comp_node *comp) 2791e08c822SPeter Ujfalusi { 2801e08c822SPeter Ujfalusi if (comp->dss_core_component) 2811e08c822SPeter Ujfalusi return true; 2824e17763cSLaurent Pinchart if (!strstarts(comp->compat, "omapdss,")) 2834e17763cSLaurent Pinchart return true; 2849184f8d9SLaurent Pinchart if (omapdss_device_is_registered(comp->node)) 2851e08c822SPeter Ujfalusi return true; 2861e08c822SPeter Ujfalusi 2871e08c822SPeter Ujfalusi return false; 2881e08c822SPeter Ujfalusi } 2891e08c822SPeter Ujfalusi 2901e08c822SPeter Ujfalusi bool omapdss_stack_is_ready(void) 2911e08c822SPeter Ujfalusi { 2921e08c822SPeter Ujfalusi struct omapdss_comp_node *comp; 2931e08c822SPeter Ujfalusi 2941e08c822SPeter Ujfalusi list_for_each_entry(comp, &omapdss_comp_list, list) { 2951e08c822SPeter Ujfalusi if (!omapdss_component_is_loaded(comp)) 2961e08c822SPeter Ujfalusi return false; 2971e08c822SPeter Ujfalusi } 2981e08c822SPeter Ujfalusi 2991e08c822SPeter Ujfalusi return true; 3001e08c822SPeter Ujfalusi } 301