1df785aa8SLiviu Dudau #include <linux/component.h> 27e435aadSRussell King #include <linux/export.h> 37e435aadSRussell King #include <linux/list.h> 47e435aadSRussell King #include <linux/of_graph.h> 57e435aadSRussell King #include <drm/drmP.h> 61f2db303SRob Herring #include <drm/drm_bridge.h> 77e435aadSRussell King #include <drm/drm_crtc.h> 89338203cSLaurent Pinchart #include <drm/drm_encoder.h> 91f2db303SRob Herring #include <drm/drm_panel.h> 107e435aadSRussell King #include <drm/drm_of.h> 117e435aadSRussell King 1297ac0e47SRussell King static void drm_release_of(struct device *dev, void *data) 1397ac0e47SRussell King { 1497ac0e47SRussell King of_node_put(data); 1597ac0e47SRussell King } 1697ac0e47SRussell King 177e435aadSRussell King /** 187e435aadSRussell King * drm_crtc_port_mask - find the mask of a registered CRTC by port OF node 197e435aadSRussell King * @dev: DRM device 207e435aadSRussell King * @port: port OF node 217e435aadSRussell King * 227e435aadSRussell King * Given a port OF node, return the possible mask of the corresponding 237e435aadSRussell King * CRTC within a device's list of CRTCs. Returns zero if not found. 247e435aadSRussell King */ 257e435aadSRussell King static uint32_t drm_crtc_port_mask(struct drm_device *dev, 267e435aadSRussell King struct device_node *port) 277e435aadSRussell King { 287e435aadSRussell King unsigned int index = 0; 297e435aadSRussell King struct drm_crtc *tmp; 307e435aadSRussell King 316295d607SDaniel Vetter drm_for_each_crtc(tmp, dev) { 327e435aadSRussell King if (tmp->port == port) 337e435aadSRussell King return 1 << index; 347e435aadSRussell King 357e435aadSRussell King index++; 367e435aadSRussell King } 377e435aadSRussell King 387e435aadSRussell King return 0; 397e435aadSRussell King } 407e435aadSRussell King 417e435aadSRussell King /** 427e435aadSRussell King * drm_of_find_possible_crtcs - find the possible CRTCs for an encoder port 437e435aadSRussell King * @dev: DRM device 447e435aadSRussell King * @port: encoder port to scan for endpoints 457e435aadSRussell King * 467e435aadSRussell King * Scan all endpoints attached to a port, locate their attached CRTCs, 477e435aadSRussell King * and generate the DRM mask of CRTCs which may be attached to this 487e435aadSRussell King * encoder. 497e435aadSRussell King * 507e435aadSRussell King * See Documentation/devicetree/bindings/graph.txt for the bindings. 517e435aadSRussell King */ 527e435aadSRussell King uint32_t drm_of_find_possible_crtcs(struct drm_device *dev, 537e435aadSRussell King struct device_node *port) 547e435aadSRussell King { 557416f4e3SPhilipp Zabel struct device_node *remote_port, *ep; 567e435aadSRussell King uint32_t possible_crtcs = 0; 577e435aadSRussell King 587416f4e3SPhilipp Zabel for_each_endpoint_of_node(port, ep) { 597e435aadSRussell King remote_port = of_graph_get_remote_port(ep); 607e435aadSRussell King if (!remote_port) { 617e435aadSRussell King of_node_put(ep); 627e435aadSRussell King return 0; 637e435aadSRussell King } 647e435aadSRussell King 657e435aadSRussell King possible_crtcs |= drm_crtc_port_mask(dev, remote_port); 667e435aadSRussell King 677e435aadSRussell King of_node_put(remote_port); 687416f4e3SPhilipp Zabel } 697e435aadSRussell King 707e435aadSRussell King return possible_crtcs; 717e435aadSRussell King } 727e435aadSRussell King EXPORT_SYMBOL(drm_of_find_possible_crtcs); 73df785aa8SLiviu Dudau 74df785aa8SLiviu Dudau /** 7597ac0e47SRussell King * drm_of_component_match_add - Add a component helper OF node match rule 7697ac0e47SRussell King * @master: master device 7797ac0e47SRussell King * @matchptr: component match pointer 7897ac0e47SRussell King * @compare: compare function used for matching component 7997ac0e47SRussell King * @node: of_node 8097ac0e47SRussell King */ 8197ac0e47SRussell King void drm_of_component_match_add(struct device *master, 8297ac0e47SRussell King struct component_match **matchptr, 8397ac0e47SRussell King int (*compare)(struct device *, void *), 8497ac0e47SRussell King struct device_node *node) 8597ac0e47SRussell King { 8697ac0e47SRussell King of_node_get(node); 8797ac0e47SRussell King component_match_add_release(master, matchptr, drm_release_of, 8897ac0e47SRussell King compare, node); 8997ac0e47SRussell King } 9097ac0e47SRussell King EXPORT_SYMBOL_GPL(drm_of_component_match_add); 9197ac0e47SRussell King 9297ac0e47SRussell King /** 93df785aa8SLiviu Dudau * drm_of_component_probe - Generic probe function for a component based master 94df785aa8SLiviu Dudau * @dev: master device containing the OF node 95df785aa8SLiviu Dudau * @compare_of: compare function used for matching components 96df785aa8SLiviu Dudau * @master_ops: component master ops to be used 97df785aa8SLiviu Dudau * 98df785aa8SLiviu Dudau * Parse the platform device OF node and bind all the components associated 99df785aa8SLiviu Dudau * with the master. Interface ports are added before the encoders in order to 100df785aa8SLiviu Dudau * satisfy their .bind requirements 101df785aa8SLiviu Dudau * See Documentation/devicetree/bindings/graph.txt for the bindings. 102df785aa8SLiviu Dudau * 103df785aa8SLiviu Dudau * Returns zero if successful, or one of the standard error codes if it fails. 104df785aa8SLiviu Dudau */ 105df785aa8SLiviu Dudau int drm_of_component_probe(struct device *dev, 106df785aa8SLiviu Dudau int (*compare_of)(struct device *, void *), 107df785aa8SLiviu Dudau const struct component_master_ops *m_ops) 108df785aa8SLiviu Dudau { 109df785aa8SLiviu Dudau struct device_node *ep, *port, *remote; 110df785aa8SLiviu Dudau struct component_match *match = NULL; 111df785aa8SLiviu Dudau int i; 112df785aa8SLiviu Dudau 113df785aa8SLiviu Dudau if (!dev->of_node) 114df785aa8SLiviu Dudau return -EINVAL; 115df785aa8SLiviu Dudau 116df785aa8SLiviu Dudau /* 117df785aa8SLiviu Dudau * Bind the crtc's ports first, so that drm_of_find_possible_crtcs() 118df785aa8SLiviu Dudau * called from encoder's .bind callbacks works as expected 119df785aa8SLiviu Dudau */ 120df785aa8SLiviu Dudau for (i = 0; ; i++) { 121df785aa8SLiviu Dudau port = of_parse_phandle(dev->of_node, "ports", i); 122df785aa8SLiviu Dudau if (!port) 123df785aa8SLiviu Dudau break; 124df785aa8SLiviu Dudau 125df785aa8SLiviu Dudau if (!of_device_is_available(port->parent)) { 126df785aa8SLiviu Dudau of_node_put(port); 127df785aa8SLiviu Dudau continue; 128df785aa8SLiviu Dudau } 129df785aa8SLiviu Dudau 13097ac0e47SRussell King drm_of_component_match_add(dev, &match, compare_of, port); 131df785aa8SLiviu Dudau of_node_put(port); 132df785aa8SLiviu Dudau } 133df785aa8SLiviu Dudau 134df785aa8SLiviu Dudau if (i == 0) { 135df785aa8SLiviu Dudau dev_err(dev, "missing 'ports' property\n"); 136df785aa8SLiviu Dudau return -ENODEV; 137df785aa8SLiviu Dudau } 138df785aa8SLiviu Dudau 139df785aa8SLiviu Dudau if (!match) { 140df785aa8SLiviu Dudau dev_err(dev, "no available port\n"); 141df785aa8SLiviu Dudau return -ENODEV; 142df785aa8SLiviu Dudau } 143df785aa8SLiviu Dudau 144df785aa8SLiviu Dudau /* 145df785aa8SLiviu Dudau * For bound crtcs, bind the encoders attached to their remote endpoint 146df785aa8SLiviu Dudau */ 147df785aa8SLiviu Dudau for (i = 0; ; i++) { 148df785aa8SLiviu Dudau port = of_parse_phandle(dev->of_node, "ports", i); 149df785aa8SLiviu Dudau if (!port) 150df785aa8SLiviu Dudau break; 151df785aa8SLiviu Dudau 152df785aa8SLiviu Dudau if (!of_device_is_available(port->parent)) { 153df785aa8SLiviu Dudau of_node_put(port); 154df785aa8SLiviu Dudau continue; 155df785aa8SLiviu Dudau } 156df785aa8SLiviu Dudau 157df785aa8SLiviu Dudau for_each_child_of_node(port, ep) { 158df785aa8SLiviu Dudau remote = of_graph_get_remote_port_parent(ep); 159df785aa8SLiviu Dudau if (!remote || !of_device_is_available(remote)) { 160df785aa8SLiviu Dudau of_node_put(remote); 161df785aa8SLiviu Dudau continue; 162df785aa8SLiviu Dudau } else if (!of_device_is_available(remote->parent)) { 1634bf99144SRob Herring dev_warn(dev, "parent device of %pOF is not available\n", 1644bf99144SRob Herring remote); 165df785aa8SLiviu Dudau of_node_put(remote); 166df785aa8SLiviu Dudau continue; 167df785aa8SLiviu Dudau } 168df785aa8SLiviu Dudau 16997ac0e47SRussell King drm_of_component_match_add(dev, &match, compare_of, 17097ac0e47SRussell King remote); 171df785aa8SLiviu Dudau of_node_put(remote); 172df785aa8SLiviu Dudau } 173df785aa8SLiviu Dudau of_node_put(port); 174df785aa8SLiviu Dudau } 175df785aa8SLiviu Dudau 176df785aa8SLiviu Dudau return component_master_add_with_match(dev, m_ops, match); 177df785aa8SLiviu Dudau } 178df785aa8SLiviu Dudau EXPORT_SYMBOL(drm_of_component_probe); 1794cacf91fSPhilipp Zabel 1804cacf91fSPhilipp Zabel /* 1814cacf91fSPhilipp Zabel * drm_of_encoder_active_endpoint - return the active encoder endpoint 1824cacf91fSPhilipp Zabel * @node: device tree node containing encoder input ports 1834cacf91fSPhilipp Zabel * @encoder: drm_encoder 1844cacf91fSPhilipp Zabel * 1854cacf91fSPhilipp Zabel * Given an encoder device node and a drm_encoder with a connected crtc, 1864cacf91fSPhilipp Zabel * parse the encoder endpoint connecting to the crtc port. 1874cacf91fSPhilipp Zabel */ 1884cacf91fSPhilipp Zabel int drm_of_encoder_active_endpoint(struct device_node *node, 1894cacf91fSPhilipp Zabel struct drm_encoder *encoder, 1904cacf91fSPhilipp Zabel struct of_endpoint *endpoint) 1914cacf91fSPhilipp Zabel { 1924cacf91fSPhilipp Zabel struct device_node *ep; 1934cacf91fSPhilipp Zabel struct drm_crtc *crtc = encoder->crtc; 1944cacf91fSPhilipp Zabel struct device_node *port; 1954cacf91fSPhilipp Zabel int ret; 1964cacf91fSPhilipp Zabel 1974cacf91fSPhilipp Zabel if (!node || !crtc) 1984cacf91fSPhilipp Zabel return -EINVAL; 1994cacf91fSPhilipp Zabel 2004cacf91fSPhilipp Zabel for_each_endpoint_of_node(node, ep) { 2014cacf91fSPhilipp Zabel port = of_graph_get_remote_port(ep); 2024cacf91fSPhilipp Zabel of_node_put(port); 2034cacf91fSPhilipp Zabel if (port == crtc->port) { 2044cacf91fSPhilipp Zabel ret = of_graph_parse_endpoint(ep, endpoint); 2054cacf91fSPhilipp Zabel of_node_put(ep); 2064cacf91fSPhilipp Zabel return ret; 2074cacf91fSPhilipp Zabel } 2084cacf91fSPhilipp Zabel } 2094cacf91fSPhilipp Zabel 2104cacf91fSPhilipp Zabel return -EINVAL; 2114cacf91fSPhilipp Zabel } 2124cacf91fSPhilipp Zabel EXPORT_SYMBOL_GPL(drm_of_encoder_active_endpoint); 2131f2db303SRob Herring 2141f2db303SRob Herring /* 2151f2db303SRob Herring * drm_of_find_panel_or_bridge - return connected panel or bridge device 2161f2db303SRob Herring * @np: device tree node containing encoder output ports 2171f2db303SRob Herring * @panel: pointer to hold returned drm_panel 2181f2db303SRob Herring * @bridge: pointer to hold returned drm_bridge 2191f2db303SRob Herring * 2201f2db303SRob Herring * Given a DT node's port and endpoint number, find the connected node and 2211f2db303SRob Herring * return either the associated struct drm_panel or drm_bridge device. Either 2221f2db303SRob Herring * @panel or @bridge must not be NULL. 2231f2db303SRob Herring * 2241f2db303SRob Herring * Returns zero if successful, or one of the standard error codes if it fails. 2251f2db303SRob Herring */ 2261f2db303SRob Herring int drm_of_find_panel_or_bridge(const struct device_node *np, 2271f2db303SRob Herring int port, int endpoint, 2281f2db303SRob Herring struct drm_panel **panel, 2291f2db303SRob Herring struct drm_bridge **bridge) 2301f2db303SRob Herring { 2311f2db303SRob Herring int ret = -EPROBE_DEFER; 2321f2db303SRob Herring struct device_node *remote; 2331f2db303SRob Herring 2341f2db303SRob Herring if (!panel && !bridge) 2351f2db303SRob Herring return -EINVAL; 236320e421eSDan Carpenter if (panel) 237320e421eSDan Carpenter *panel = NULL; 2381f2db303SRob Herring 2391f2db303SRob Herring remote = of_graph_get_remote_node(np, port, endpoint); 2401f2db303SRob Herring if (!remote) 2411f2db303SRob Herring return -ENODEV; 2421f2db303SRob Herring 2431f2db303SRob Herring if (panel) { 2441f2db303SRob Herring *panel = of_drm_find_panel(remote); 2451f2db303SRob Herring if (*panel) 2461f2db303SRob Herring ret = 0; 2471f2db303SRob Herring } 2481f2db303SRob Herring 2491f2db303SRob Herring /* No panel found yet, check for a bridge next. */ 2501f2db303SRob Herring if (bridge) { 2511f2db303SRob Herring if (ret) { 2521f2db303SRob Herring *bridge = of_drm_find_bridge(remote); 2531f2db303SRob Herring if (*bridge) 2541f2db303SRob Herring ret = 0; 2551f2db303SRob Herring } else { 2561f2db303SRob Herring *bridge = NULL; 2571f2db303SRob Herring } 2581f2db303SRob Herring 2591f2db303SRob Herring } 2601f2db303SRob Herring 2611f2db303SRob Herring of_node_put(remote); 2621f2db303SRob Herring return ret; 2631f2db303SRob Herring } 2641f2db303SRob Herring EXPORT_SYMBOL_GPL(drm_of_find_panel_or_bridge); 265