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