Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

usb: ulpi: Support device discovery via DT

The qcom HSIC ULPI phy doesn't have any bits set in the vendor or
product ID registers. This makes it impossible to make a ULPI
driver match against the ID registers. Add support to discover
the ULPI phys via DT help alleviate this problem. In the DT case,
we'll look for a ULPI bus node underneath the device registering
the ULPI viewport (or the parent of that device to support
chipidea's device layout) and then match up the phy node
underneath that with the ULPI device that's created.

The side benefit of this is that we can use standard properties
in the phy node like clks, regulators, gpios, etc. because we
don't have firmware like ACPI to turn these things on for us. And
we can use the DT phy binding to point our phy consumer to the
phy provider.

The ULPI bus code supports native enumeration by reading the
vendor ID and product ID registers at device creation time, but
we can't be certain that those register reads will succeed if the
phy is not powered up. To avoid any problems with reading the ID
registers before the phy is powered we fallback to DT matching
when the ID reads fail.

If the ULPI spec had some generic power sequencing for these
registers we could put that into the ULPI bus layer and power up
the device before reading the ID registers. Unfortunately this
doesn't exist and the power sequence is usually device specific.
By having the device matched up with DT we can avoid this
problem.

Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Acked-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Cc: <devicetree@vger.kernel.org>
Acked-by: Rob Herring <robh@kernel.org>
Signed-off-by: Stephen Boyd <stephen.boyd@linaro.org>
Signed-off-by: Peter Chen <peter.chen@nxp.com>

authored by

Stephen Boyd and committed by
Peter Chen
ef6a7bcf 7a3b7cd3

+93 -6
+20
Documentation/devicetree/bindings/usb/ulpi.txt
··· 1 + ULPI bus binding 2 + ---------------- 3 + 4 + Phys that are behind a ULPI connection can be described with the following 5 + binding. The host controller shall have a "ulpi" named node as a child, and 6 + that node shall have one enabled node underneath it representing the ulpi 7 + device on the bus. 8 + 9 + EXAMPLE 10 + ------- 11 + 12 + usb { 13 + compatible = "vendor,usb-controller"; 14 + 15 + ulpi { 16 + phy { 17 + compatible = "vendor,phy"; 18 + }; 19 + }; 20 + };
+73 -6
drivers/usb/common/ulpi.c
··· 16 16 #include <linux/module.h> 17 17 #include <linux/slab.h> 18 18 #include <linux/acpi.h> 19 + #include <linux/of.h> 20 + #include <linux/of_device.h> 21 + #include <linux/clk/clk-conf.h> 19 22 20 23 /* -------------------------------------------------------------------------- */ 21 24 ··· 42 39 struct ulpi *ulpi = to_ulpi_dev(dev); 43 40 const struct ulpi_device_id *id; 44 41 42 + /* Some ULPI devices don't have a vendor id so rely on OF match */ 43 + if (ulpi->id.vendor == 0) 44 + return of_driver_match_device(dev, driver); 45 + 45 46 for (id = drv->id_table; id->vendor; id++) 46 47 if (id->vendor == ulpi->id.vendor && 47 48 id->product == ulpi->id.product) ··· 57 50 static int ulpi_uevent(struct device *dev, struct kobj_uevent_env *env) 58 51 { 59 52 struct ulpi *ulpi = to_ulpi_dev(dev); 53 + int ret; 54 + 55 + ret = of_device_uevent_modalias(dev, env); 56 + if (ret != -ENODEV) 57 + return ret; 60 58 61 59 if (add_uevent_var(env, "MODALIAS=ulpi:v%04xp%04x", 62 60 ulpi->id.vendor, ulpi->id.product)) ··· 72 60 static int ulpi_probe(struct device *dev) 73 61 { 74 62 struct ulpi_driver *drv = to_ulpi_driver(dev->driver); 63 + int ret; 64 + 65 + ret = of_clk_set_defaults(dev->of_node, false); 66 + if (ret < 0) 67 + return ret; 75 68 76 69 return drv->probe(to_ulpi_dev(dev)); 77 70 } ··· 104 87 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, 105 88 char *buf) 106 89 { 90 + int len; 107 91 struct ulpi *ulpi = to_ulpi_dev(dev); 92 + 93 + len = of_device_get_modalias(dev, buf, PAGE_SIZE - 1); 94 + if (len != -ENODEV) 95 + return len; 108 96 109 97 return sprintf(buf, "ulpi:v%04xp%04x\n", 110 98 ulpi->id.vendor, ulpi->id.product); ··· 175 153 176 154 /* -------------------------------------------------------------------------- */ 177 155 178 - static int ulpi_register(struct device *dev, struct ulpi *ulpi) 156 + static int ulpi_of_register(struct ulpi *ulpi) 157 + { 158 + struct device_node *np = NULL, *child; 159 + struct device *parent; 160 + 161 + /* Find a ulpi bus underneath the parent or the grandparent */ 162 + parent = ulpi->dev.parent; 163 + if (parent->of_node) 164 + np = of_find_node_by_name(parent->of_node, "ulpi"); 165 + else if (parent->parent && parent->parent->of_node) 166 + np = of_find_node_by_name(parent->parent->of_node, "ulpi"); 167 + if (!np) 168 + return 0; 169 + 170 + child = of_get_next_available_child(np, NULL); 171 + of_node_put(np); 172 + if (!child) 173 + return -EINVAL; 174 + 175 + ulpi->dev.of_node = child; 176 + 177 + return 0; 178 + } 179 + 180 + static int ulpi_read_id(struct ulpi *ulpi) 179 181 { 180 182 int ret; 181 - 182 - ulpi->dev.parent = dev; /* needed early for ops */ 183 183 184 184 /* Test the interface */ 185 185 ret = ulpi_write(ulpi, ULPI_SCRATCH, 0xaa); 186 186 if (ret < 0) 187 - return ret; 187 + goto err; 188 188 189 189 ret = ulpi_read(ulpi, ULPI_SCRATCH); 190 190 if (ret < 0) 191 191 return ret; 192 192 193 193 if (ret != 0xaa) 194 - return -ENODEV; 194 + goto err; 195 195 196 196 ulpi->id.vendor = ulpi_read(ulpi, ULPI_VENDOR_ID_LOW); 197 197 ulpi->id.vendor |= ulpi_read(ulpi, ULPI_VENDOR_ID_HIGH) << 8; ··· 221 177 ulpi->id.product = ulpi_read(ulpi, ULPI_PRODUCT_ID_LOW); 222 178 ulpi->id.product |= ulpi_read(ulpi, ULPI_PRODUCT_ID_HIGH) << 8; 223 179 180 + /* Some ULPI devices don't have a vendor id so rely on OF match */ 181 + if (ulpi->id.vendor == 0) 182 + goto err; 183 + 184 + request_module("ulpi:v%04xp%04x", ulpi->id.vendor, ulpi->id.product); 185 + return 0; 186 + err: 187 + of_device_request_module(&ulpi->dev); 188 + return 0; 189 + } 190 + 191 + static int ulpi_register(struct device *dev, struct ulpi *ulpi) 192 + { 193 + int ret; 194 + 195 + ulpi->dev.parent = dev; /* needed early for ops */ 224 196 ulpi->dev.bus = &ulpi_bus; 225 197 ulpi->dev.type = &ulpi_dev_type; 226 198 dev_set_name(&ulpi->dev, "%s.ulpi", dev_name(dev)); 227 199 228 200 ACPI_COMPANION_SET(&ulpi->dev, ACPI_COMPANION(dev)); 229 201 230 - request_module("ulpi:v%04xp%04x", ulpi->id.vendor, ulpi->id.product); 202 + ret = ulpi_of_register(ulpi); 203 + if (ret) 204 + return ret; 205 + 206 + ret = ulpi_read_id(ulpi); 207 + if (ret) 208 + return ret; 231 209 232 210 ret = device_register(&ulpi->dev); 233 211 if (ret) ··· 300 234 */ 301 235 void ulpi_unregister_interface(struct ulpi *ulpi) 302 236 { 237 + of_node_put(ulpi->dev.of_node); 303 238 device_unregister(&ulpi->dev); 304 239 } 305 240 EXPORT_SYMBOL_GPL(ulpi_unregister_interface);