Liviu Dudau | df785aa | 2015-10-20 10:23:12 +0100 | [diff] [blame] | 1 | #include <linux/component.h> |
Russell King | 7e435aa | 2014-06-15 11:07:12 +0100 | [diff] [blame] | 2 | #include <linux/export.h> |
| 3 | #include <linux/list.h> |
| 4 | #include <linux/of_graph.h> |
| 5 | #include <drm/drmP.h> |
Rob Herring | 1f2db30 | 2017-03-22 08:26:05 -0500 | [diff] [blame] | 6 | #include <drm/drm_bridge.h> |
Russell King | 7e435aa | 2014-06-15 11:07:12 +0100 | [diff] [blame] | 7 | #include <drm/drm_crtc.h> |
Laurent Pinchart | 9338203 | 2016-11-28 20:51:09 +0200 | [diff] [blame] | 8 | #include <drm/drm_encoder.h> |
Rob Herring | 1f2db30 | 2017-03-22 08:26:05 -0500 | [diff] [blame] | 9 | #include <drm/drm_panel.h> |
Russell King | 7e435aa | 2014-06-15 11:07:12 +0100 | [diff] [blame] | 10 | #include <drm/drm_of.h> |
| 11 | |
Russell King | 97ac0e4 | 2016-10-19 11:28:27 +0100 | [diff] [blame] | 12 | static void drm_release_of(struct device *dev, void *data) |
| 13 | { |
| 14 | of_node_put(data); |
| 15 | } |
| 16 | |
Russell King | 7e435aa | 2014-06-15 11:07:12 +0100 | [diff] [blame] | 17 | /** |
| 18 | * drm_crtc_port_mask - find the mask of a registered CRTC by port OF node |
| 19 | * @dev: DRM device |
| 20 | * @port: port OF node |
| 21 | * |
| 22 | * Given a port OF node, return the possible mask of the corresponding |
| 23 | * CRTC within a device's list of CRTCs. Returns zero if not found. |
| 24 | */ |
| 25 | static uint32_t drm_crtc_port_mask(struct drm_device *dev, |
| 26 | struct device_node *port) |
| 27 | { |
| 28 | unsigned int index = 0; |
| 29 | struct drm_crtc *tmp; |
| 30 | |
Daniel Vetter | 6295d60 | 2015-07-09 23:44:25 +0200 | [diff] [blame] | 31 | drm_for_each_crtc(tmp, dev) { |
Russell King | 7e435aa | 2014-06-15 11:07:12 +0100 | [diff] [blame] | 32 | if (tmp->port == port) |
| 33 | return 1 << index; |
| 34 | |
| 35 | index++; |
| 36 | } |
| 37 | |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * drm_of_find_possible_crtcs - find the possible CRTCs for an encoder port |
| 43 | * @dev: DRM device |
| 44 | * @port: encoder port to scan for endpoints |
| 45 | * |
| 46 | * Scan all endpoints attached to a port, locate their attached CRTCs, |
| 47 | * and generate the DRM mask of CRTCs which may be attached to this |
| 48 | * encoder. |
| 49 | * |
| 50 | * See Documentation/devicetree/bindings/graph.txt for the bindings. |
| 51 | */ |
| 52 | uint32_t drm_of_find_possible_crtcs(struct drm_device *dev, |
| 53 | struct device_node *port) |
| 54 | { |
Philipp Zabel | 7416f4e | 2014-07-03 22:59:51 +0200 | [diff] [blame] | 55 | struct device_node *remote_port, *ep; |
Russell King | 7e435aa | 2014-06-15 11:07:12 +0100 | [diff] [blame] | 56 | uint32_t possible_crtcs = 0; |
| 57 | |
Philipp Zabel | 7416f4e | 2014-07-03 22:59:51 +0200 | [diff] [blame] | 58 | for_each_endpoint_of_node(port, ep) { |
Russell King | 7e435aa | 2014-06-15 11:07:12 +0100 | [diff] [blame] | 59 | remote_port = of_graph_get_remote_port(ep); |
| 60 | if (!remote_port) { |
| 61 | of_node_put(ep); |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | possible_crtcs |= drm_crtc_port_mask(dev, remote_port); |
| 66 | |
| 67 | of_node_put(remote_port); |
Philipp Zabel | 7416f4e | 2014-07-03 22:59:51 +0200 | [diff] [blame] | 68 | } |
Russell King | 7e435aa | 2014-06-15 11:07:12 +0100 | [diff] [blame] | 69 | |
| 70 | return possible_crtcs; |
| 71 | } |
| 72 | EXPORT_SYMBOL(drm_of_find_possible_crtcs); |
Liviu Dudau | df785aa | 2015-10-20 10:23:12 +0100 | [diff] [blame] | 73 | |
| 74 | /** |
Russell King | 97ac0e4 | 2016-10-19 11:28:27 +0100 | [diff] [blame] | 75 | * drm_of_component_match_add - Add a component helper OF node match rule |
| 76 | * @master: master device |
| 77 | * @matchptr: component match pointer |
| 78 | * @compare: compare function used for matching component |
| 79 | * @node: of_node |
| 80 | */ |
| 81 | void drm_of_component_match_add(struct device *master, |
| 82 | struct component_match **matchptr, |
| 83 | int (*compare)(struct device *, void *), |
| 84 | struct device_node *node) |
| 85 | { |
| 86 | of_node_get(node); |
| 87 | component_match_add_release(master, matchptr, drm_release_of, |
| 88 | compare, node); |
| 89 | } |
| 90 | EXPORT_SYMBOL_GPL(drm_of_component_match_add); |
| 91 | |
| 92 | /** |
Liviu Dudau | df785aa | 2015-10-20 10:23:12 +0100 | [diff] [blame] | 93 | * drm_of_component_probe - Generic probe function for a component based master |
| 94 | * @dev: master device containing the OF node |
| 95 | * @compare_of: compare function used for matching components |
| 96 | * @master_ops: component master ops to be used |
| 97 | * |
| 98 | * Parse the platform device OF node and bind all the components associated |
| 99 | * with the master. Interface ports are added before the encoders in order to |
| 100 | * satisfy their .bind requirements |
| 101 | * See Documentation/devicetree/bindings/graph.txt for the bindings. |
| 102 | * |
| 103 | * Returns zero if successful, or one of the standard error codes if it fails. |
| 104 | */ |
| 105 | int drm_of_component_probe(struct device *dev, |
| 106 | int (*compare_of)(struct device *, void *), |
| 107 | const struct component_master_ops *m_ops) |
| 108 | { |
| 109 | struct device_node *ep, *port, *remote; |
| 110 | struct component_match *match = NULL; |
| 111 | int i; |
| 112 | |
| 113 | if (!dev->of_node) |
| 114 | return -EINVAL; |
| 115 | |
| 116 | /* |
| 117 | * Bind the crtc's ports first, so that drm_of_find_possible_crtcs() |
| 118 | * called from encoder's .bind callbacks works as expected |
| 119 | */ |
| 120 | for (i = 0; ; i++) { |
| 121 | port = of_parse_phandle(dev->of_node, "ports", i); |
| 122 | if (!port) |
| 123 | break; |
| 124 | |
| 125 | if (!of_device_is_available(port->parent)) { |
| 126 | of_node_put(port); |
| 127 | continue; |
| 128 | } |
| 129 | |
Russell King | 97ac0e4 | 2016-10-19 11:28:27 +0100 | [diff] [blame] | 130 | drm_of_component_match_add(dev, &match, compare_of, port); |
Liviu Dudau | df785aa | 2015-10-20 10:23:12 +0100 | [diff] [blame] | 131 | of_node_put(port); |
| 132 | } |
| 133 | |
| 134 | if (i == 0) { |
| 135 | dev_err(dev, "missing 'ports' property\n"); |
| 136 | return -ENODEV; |
| 137 | } |
| 138 | |
| 139 | if (!match) { |
| 140 | dev_err(dev, "no available port\n"); |
| 141 | return -ENODEV; |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * For bound crtcs, bind the encoders attached to their remote endpoint |
| 146 | */ |
| 147 | for (i = 0; ; i++) { |
| 148 | port = of_parse_phandle(dev->of_node, "ports", i); |
| 149 | if (!port) |
| 150 | break; |
| 151 | |
| 152 | if (!of_device_is_available(port->parent)) { |
| 153 | of_node_put(port); |
| 154 | continue; |
| 155 | } |
| 156 | |
| 157 | for_each_child_of_node(port, ep) { |
| 158 | remote = of_graph_get_remote_port_parent(ep); |
| 159 | if (!remote || !of_device_is_available(remote)) { |
| 160 | of_node_put(remote); |
| 161 | continue; |
| 162 | } else if (!of_device_is_available(remote->parent)) { |
| 163 | dev_warn(dev, "parent device of %s is not available\n", |
| 164 | remote->full_name); |
| 165 | of_node_put(remote); |
| 166 | continue; |
| 167 | } |
| 168 | |
Russell King | 97ac0e4 | 2016-10-19 11:28:27 +0100 | [diff] [blame] | 169 | drm_of_component_match_add(dev, &match, compare_of, |
| 170 | remote); |
Liviu Dudau | df785aa | 2015-10-20 10:23:12 +0100 | [diff] [blame] | 171 | of_node_put(remote); |
| 172 | } |
| 173 | of_node_put(port); |
| 174 | } |
| 175 | |
| 176 | return component_master_add_with_match(dev, m_ops, match); |
| 177 | } |
| 178 | EXPORT_SYMBOL(drm_of_component_probe); |
Philipp Zabel | 4cacf91 | 2015-02-24 11:34:01 +0100 | [diff] [blame] | 179 | |
| 180 | /* |
| 181 | * drm_of_encoder_active_endpoint - return the active encoder endpoint |
| 182 | * @node: device tree node containing encoder input ports |
| 183 | * @encoder: drm_encoder |
| 184 | * |
| 185 | * Given an encoder device node and a drm_encoder with a connected crtc, |
| 186 | * parse the encoder endpoint connecting to the crtc port. |
| 187 | */ |
| 188 | int drm_of_encoder_active_endpoint(struct device_node *node, |
| 189 | struct drm_encoder *encoder, |
| 190 | struct of_endpoint *endpoint) |
| 191 | { |
| 192 | struct device_node *ep; |
| 193 | struct drm_crtc *crtc = encoder->crtc; |
| 194 | struct device_node *port; |
| 195 | int ret; |
| 196 | |
| 197 | if (!node || !crtc) |
| 198 | return -EINVAL; |
| 199 | |
| 200 | for_each_endpoint_of_node(node, ep) { |
| 201 | port = of_graph_get_remote_port(ep); |
| 202 | of_node_put(port); |
| 203 | if (port == crtc->port) { |
| 204 | ret = of_graph_parse_endpoint(ep, endpoint); |
| 205 | of_node_put(ep); |
| 206 | return ret; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | return -EINVAL; |
| 211 | } |
| 212 | EXPORT_SYMBOL_GPL(drm_of_encoder_active_endpoint); |
Rob Herring | 1f2db30 | 2017-03-22 08:26:05 -0500 | [diff] [blame] | 213 | |
| 214 | /* |
| 215 | * drm_of_find_panel_or_bridge - return connected panel or bridge device |
| 216 | * @np: device tree node containing encoder output ports |
| 217 | * @panel: pointer to hold returned drm_panel |
| 218 | * @bridge: pointer to hold returned drm_bridge |
| 219 | * |
| 220 | * Given a DT node's port and endpoint number, find the connected node and |
| 221 | * return either the associated struct drm_panel or drm_bridge device. Either |
| 222 | * @panel or @bridge must not be NULL. |
| 223 | * |
| 224 | * Returns zero if successful, or one of the standard error codes if it fails. |
| 225 | */ |
| 226 | int drm_of_find_panel_or_bridge(const struct device_node *np, |
| 227 | int port, int endpoint, |
| 228 | struct drm_panel **panel, |
| 229 | struct drm_bridge **bridge) |
| 230 | { |
| 231 | int ret = -EPROBE_DEFER; |
| 232 | struct device_node *remote; |
| 233 | |
| 234 | if (!panel && !bridge) |
| 235 | return -EINVAL; |
| 236 | |
| 237 | remote = of_graph_get_remote_node(np, port, endpoint); |
| 238 | if (!remote) |
| 239 | return -ENODEV; |
| 240 | |
| 241 | if (panel) { |
| 242 | *panel = of_drm_find_panel(remote); |
| 243 | if (*panel) |
| 244 | ret = 0; |
| 245 | } |
| 246 | |
| 247 | /* No panel found yet, check for a bridge next. */ |
| 248 | if (bridge) { |
| 249 | if (ret) { |
| 250 | *bridge = of_drm_find_bridge(remote); |
| 251 | if (*bridge) |
| 252 | ret = 0; |
| 253 | } else { |
| 254 | *bridge = NULL; |
| 255 | } |
| 256 | |
| 257 | } |
| 258 | |
| 259 | of_node_put(remote); |
| 260 | return ret; |
| 261 | } |
| 262 | EXPORT_SYMBOL_GPL(drm_of_find_panel_or_bridge); |