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

clk: add DT clock binding support

Based on work 1st by Ben Herrenschmidt and Jeremy Kerr, then by Grant
Likely, this patch adds support to clk_get to allow drivers to retrieve
clock data from the device tree.

Platforms scan for clocks in DT with of_clk_init and a match table, and
the register a provider through of_clk_add_provider. The provider's
clk_src_get function will be called when a device references the
provider's OF node for a clock reference.

v6 (Rob Herring):
- Return error values instead of NULL to match clock framework
expectations

v5 (Rob Herring):
- Move from drivers/of into common clock subsystem
- Squashed "dt/clock: add a simple provider get function" and
"dt/clock: add function to get parent clock name"
- Rebase to 3.4-rc1
- Drop CONFIG_OF_CLOCK and just use CONFIG_OF
- Add missing EXPORT_SYMBOL to various functions
- s/clock-output-name/clock-output-names/
- Define that fixed-clock binding is a single output

v4 (Rob Herring):
- Rework for common clk subsystem
- Add of_clk_get_parent_name function

v3: - Clarified documentation

v2: - fixed errant ';' causing compile error
- Editorial fixes from Shawn Guo
- merged in adding lookup to clkdev
- changed property names to match established convention. After
working with the binding a bit it really made more sense to follow the
lead of 'reg', 'gpios' and 'interrupts' by making the input simply
'clocks' & 'clock-names' instead of 'clock-input-*', and to only use
clock-output* for the producer nodes. (Sorry Shawn, this will mean
you need to change some code, but it should be trivial)
- Add ability to inherit clocks from parent nodes by using an empty
'clock-ranges' property. Useful for busses. I could use some feedback
on the new property name, 'clock-ranges' doesn't feel right to me.

Signed-off-by: Grant Likely <grant.likely@secretlab.ca>
Signed-off-by: Rob Herring <rob.herring@calxeda.com>
Reviewed-by: Shawn Guo <shawn.guo@freescale.com>
Cc: Sascha Hauer <kernel@pengutronix.de>
Signed-off-by: Mike Turquette <mturquette@linaro.org>

authored by

Grant Likely and committed by
Mike Turquette
766e6a4e a613163d

+388
+117
Documentation/devicetree/bindings/clock/clock-bindings.txt
··· 1 + This binding is a work-in-progress, and are based on some experimental 2 + work by benh[1]. 3 + 4 + Sources of clock signal can be represented by any node in the device 5 + tree. Those nodes are designated as clock providers. Clock consumer 6 + nodes use a phandle and clock specifier pair to connect clock provider 7 + outputs to clock inputs. Similar to the gpio specifiers, a clock 8 + specifier is an array of one more more cells identifying the clock 9 + output on a device. The length of a clock specifier is defined by the 10 + value of a #clock-cells property in the clock provider node. 11 + 12 + [1] http://patchwork.ozlabs.org/patch/31551/ 13 + 14 + ==Clock providers== 15 + 16 + Required properties: 17 + #clock-cells: Number of cells in a clock specifier; Typically 0 for nodes 18 + with a single clock output and 1 for nodes with multiple 19 + clock outputs. 20 + 21 + Optional properties: 22 + clock-output-names: Recommended to be a list of strings of clock output signal 23 + names indexed by the first cell in the clock specifier. 24 + However, the meaning of clock-output-names is domain 25 + specific to the clock provider, and is only provided to 26 + encourage using the same meaning for the majority of clock 27 + providers. This format may not work for clock providers 28 + using a complex clock specifier format. In those cases it 29 + is recommended to omit this property and create a binding 30 + specific names property. 31 + 32 + Clock consumer nodes must never directly reference 33 + the provider's clock-output-names property. 34 + 35 + For example: 36 + 37 + oscillator { 38 + #clock-cells = <1>; 39 + clock-output-names = "ckil", "ckih"; 40 + }; 41 + 42 + - this node defines a device with two clock outputs, the first named 43 + "ckil" and the second named "ckih". Consumer nodes always reference 44 + clocks by index. The names should reflect the clock output signal 45 + names for the device. 46 + 47 + ==Clock consumers== 48 + 49 + Required properties: 50 + clocks: List of phandle and clock specifier pairs, one pair 51 + for each clock input to the device. Note: if the 52 + clock provider specifies '0' for #clock-cells, then 53 + only the phandle portion of the pair will appear. 54 + 55 + Optional properties: 56 + clock-names: List of clock input name strings sorted in the same 57 + order as the clocks property. Consumers drivers 58 + will use clock-names to match clock input names 59 + with clocks specifiers. 60 + clock-ranges: Empty property indicating that child nodes can inherit named 61 + clocks from this node. Useful for bus nodes to provide a 62 + clock to their children. 63 + 64 + For example: 65 + 66 + device { 67 + clocks = <&osc 1>, <&ref 0>; 68 + clock-names = "baud", "register"; 69 + }; 70 + 71 + 72 + This represents a device with two clock inputs, named "baud" and "register". 73 + The baud clock is connected to output 1 of the &osc device, and the register 74 + clock is connected to output 0 of the &ref. 75 + 76 + ==Example== 77 + 78 + /* external oscillator */ 79 + osc: oscillator { 80 + compatible = "fixed-clock"; 81 + #clock-cells = <1>; 82 + clock-frequency = <32678>; 83 + clock-output-names = "osc"; 84 + }; 85 + 86 + /* phase-locked-loop device, generates a higher frequency clock 87 + * from the external oscillator reference */ 88 + pll: pll@4c000 { 89 + compatible = "vendor,some-pll-interface" 90 + #clock-cells = <1>; 91 + clocks = <&osc 0>; 92 + clock-names = "ref"; 93 + reg = <0x4c000 0x1000>; 94 + clock-output-names = "pll", "pll-switched"; 95 + }; 96 + 97 + /* UART, using the low frequency oscillator for the baud clock, 98 + * and the high frequency switched PLL output for register 99 + * clocking */ 100 + uart@a000 { 101 + compatible = "fsl,imx-uart"; 102 + reg = <0xa000 0x1000>; 103 + interrupts = <33>; 104 + clocks = <&osc 0>, <&pll 1>; 105 + clock-names = "baud", "register"; 106 + }; 107 + 108 + This DT fragment defines three devices: an external oscillator to provide a 109 + low-frequency reference clock, a PLL device to generate a higher frequency 110 + clock signal, and a UART. 111 + 112 + * The oscillator is fixed-frequency, and provides one clock output, named "osc". 113 + * The PLL is both a clock provider and a clock consumer. It uses the clock 114 + signal generated by the external oscillator, and provides two output signals 115 + ("pll" and "pll-switched"). 116 + * The UART has its baud clock connected the external oscillator and its 117 + register clock connected to the PLL clock (the "pll-switched" signal)
+21
Documentation/devicetree/bindings/clock/fixed-clock.txt
··· 1 + Binding for simple fixed-rate clock sources. 2 + 3 + This binding uses the common clock binding[1]. 4 + 5 + [1] Documentation/devicetree/bindings/clock/clock-bindings.txt 6 + 7 + Required properties: 8 + - compatible : shall be "fixed-clock". 9 + - #clock-cells : from common clock binding; shall be set to 0. 10 + - clock-frequency : frequency of clock in Hz. Should be a single cell. 11 + 12 + Optional properties: 13 + - gpios : From common gpio binding; gpio connection to clock enable pin. 14 + - clock-output-names : From common clock binding. 15 + 16 + Example: 17 + clock { 18 + compatible = "fixed-clock"; 19 + #clock-cells = <0>; 20 + clock-frequency = <1000000000>; 21 + };
+140
drivers/clk/clk.c
··· 16 16 #include <linux/err.h> 17 17 #include <linux/list.h> 18 18 #include <linux/slab.h> 19 + #include <linux/of.h> 19 20 20 21 static DEFINE_SPINLOCK(enable_lock); 21 22 static DEFINE_MUTEX(prepare_lock); ··· 1551 1550 return ret; 1552 1551 } 1553 1552 EXPORT_SYMBOL_GPL(clk_notifier_unregister); 1553 + 1554 + #ifdef CONFIG_OF 1555 + /** 1556 + * struct of_clk_provider - Clock provider registration structure 1557 + * @link: Entry in global list of clock providers 1558 + * @node: Pointer to device tree node of clock provider 1559 + * @get: Get clock callback. Returns NULL or a struct clk for the 1560 + * given clock specifier 1561 + * @data: context pointer to be passed into @get callback 1562 + */ 1563 + struct of_clk_provider { 1564 + struct list_head link; 1565 + 1566 + struct device_node *node; 1567 + struct clk *(*get)(struct of_phandle_args *clkspec, void *data); 1568 + void *data; 1569 + }; 1570 + 1571 + static LIST_HEAD(of_clk_providers); 1572 + static DEFINE_MUTEX(of_clk_lock); 1573 + 1574 + struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec, 1575 + void *data) 1576 + { 1577 + return data; 1578 + } 1579 + EXPORT_SYMBOL_GPL(of_clk_src_simple_get); 1580 + 1581 + /** 1582 + * of_clk_add_provider() - Register a clock provider for a node 1583 + * @np: Device node pointer associated with clock provider 1584 + * @clk_src_get: callback for decoding clock 1585 + * @data: context pointer for @clk_src_get callback. 1586 + */ 1587 + int of_clk_add_provider(struct device_node *np, 1588 + struct clk *(*clk_src_get)(struct of_phandle_args *clkspec, 1589 + void *data), 1590 + void *data) 1591 + { 1592 + struct of_clk_provider *cp; 1593 + 1594 + cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL); 1595 + if (!cp) 1596 + return -ENOMEM; 1597 + 1598 + cp->node = of_node_get(np); 1599 + cp->data = data; 1600 + cp->get = clk_src_get; 1601 + 1602 + mutex_lock(&of_clk_lock); 1603 + list_add(&cp->link, &of_clk_providers); 1604 + mutex_unlock(&of_clk_lock); 1605 + pr_debug("Added clock from %s\n", np->full_name); 1606 + 1607 + return 0; 1608 + } 1609 + EXPORT_SYMBOL_GPL(of_clk_add_provider); 1610 + 1611 + /** 1612 + * of_clk_del_provider() - Remove a previously registered clock provider 1613 + * @np: Device node pointer associated with clock provider 1614 + */ 1615 + void of_clk_del_provider(struct device_node *np) 1616 + { 1617 + struct of_clk_provider *cp; 1618 + 1619 + mutex_lock(&of_clk_lock); 1620 + list_for_each_entry(cp, &of_clk_providers, link) { 1621 + if (cp->node == np) { 1622 + list_del(&cp->link); 1623 + of_node_put(cp->node); 1624 + kfree(cp); 1625 + break; 1626 + } 1627 + } 1628 + mutex_unlock(&of_clk_lock); 1629 + } 1630 + EXPORT_SYMBOL_GPL(of_clk_del_provider); 1631 + 1632 + struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec) 1633 + { 1634 + struct of_clk_provider *provider; 1635 + struct clk *clk = ERR_PTR(-ENOENT); 1636 + 1637 + /* Check if we have such a provider in our array */ 1638 + mutex_lock(&of_clk_lock); 1639 + list_for_each_entry(provider, &of_clk_providers, link) { 1640 + if (provider->node == clkspec->np) 1641 + clk = provider->get(clkspec, provider->data); 1642 + if (!IS_ERR(clk)) 1643 + break; 1644 + } 1645 + mutex_unlock(&of_clk_lock); 1646 + 1647 + return clk; 1648 + } 1649 + 1650 + const char *of_clk_get_parent_name(struct device_node *np, int index) 1651 + { 1652 + struct of_phandle_args clkspec; 1653 + const char *clk_name; 1654 + int rc; 1655 + 1656 + if (index < 0) 1657 + return NULL; 1658 + 1659 + rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index, 1660 + &clkspec); 1661 + if (rc) 1662 + return NULL; 1663 + 1664 + if (of_property_read_string_index(clkspec.np, "clock-output-names", 1665 + clkspec.args_count ? clkspec.args[0] : 0, 1666 + &clk_name) < 0) 1667 + clk_name = clkspec.np->name; 1668 + 1669 + of_node_put(clkspec.np); 1670 + return clk_name; 1671 + } 1672 + EXPORT_SYMBOL_GPL(of_clk_get_parent_name); 1673 + 1674 + /** 1675 + * of_clk_init() - Scan and init clock providers from the DT 1676 + * @matches: array of compatible values and init functions for providers. 1677 + * 1678 + * This function scans the device tree for matching clock providers and 1679 + * calls their initialization functions 1680 + */ 1681 + void __init of_clk_init(const struct of_device_id *matches) 1682 + { 1683 + struct device_node *np; 1684 + 1685 + for_each_matching_node(np, matches) { 1686 + const struct of_device_id *match = of_match_node(matches, np); 1687 + of_clk_init_cb_t clk_init_cb = match->data; 1688 + clk_init_cb(np); 1689 + } 1690 + } 1691 + #endif
+77
drivers/clk/clkdev.c
··· 19 19 #include <linux/mutex.h> 20 20 #include <linux/clk.h> 21 21 #include <linux/clkdev.h> 22 + #include <linux/of.h> 22 23 23 24 static LIST_HEAD(clocks); 24 25 static DEFINE_MUTEX(clocks_mutex); 26 + 27 + #ifdef CONFIG_OF 28 + struct clk *of_clk_get(struct device_node *np, int index) 29 + { 30 + struct of_phandle_args clkspec; 31 + struct clk *clk; 32 + int rc; 33 + 34 + if (index < 0) 35 + return ERR_PTR(-EINVAL); 36 + 37 + rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index, 38 + &clkspec); 39 + if (rc) 40 + return ERR_PTR(rc); 41 + 42 + clk = of_clk_get_from_provider(&clkspec); 43 + of_node_put(clkspec.np); 44 + return clk; 45 + } 46 + EXPORT_SYMBOL(of_clk_get); 47 + 48 + /** 49 + * of_clk_get_by_name() - Parse and lookup a clock referenced by a device node 50 + * @np: pointer to clock consumer node 51 + * @name: name of consumer's clock input, or NULL for the first clock reference 52 + * 53 + * This function parses the clocks and clock-names properties, 54 + * and uses them to look up the struct clk from the registered list of clock 55 + * providers. 56 + */ 57 + struct clk *of_clk_get_by_name(struct device_node *np, const char *name) 58 + { 59 + struct clk *clk = ERR_PTR(-ENOENT); 60 + 61 + /* Walk up the tree of devices looking for a clock that matches */ 62 + while (np) { 63 + int index = 0; 64 + 65 + /* 66 + * For named clocks, first look up the name in the 67 + * "clock-names" property. If it cannot be found, then 68 + * index will be an error code, and of_clk_get() will fail. 69 + */ 70 + if (name) 71 + index = of_property_match_string(np, "clock-names", name); 72 + clk = of_clk_get(np, index); 73 + if (!IS_ERR(clk)) 74 + break; 75 + else if (name && index >= 0) { 76 + pr_err("ERROR: could not get clock %s:%s(%i)\n", 77 + np->full_name, name ? name : "", index); 78 + return clk; 79 + } 80 + 81 + /* 82 + * No matching clock found on this node. If the parent node 83 + * has a "clock-ranges" property, then we can try one of its 84 + * clocks. 85 + */ 86 + np = np->parent; 87 + if (np && !of_get_property(np, "clock-ranges", NULL)) 88 + break; 89 + } 90 + 91 + return clk; 92 + } 93 + EXPORT_SYMBOL(of_clk_get_by_name); 94 + #endif 25 95 26 96 /* 27 97 * Find the correct struct clk for the device and connection ID. ··· 153 83 struct clk *clk_get(struct device *dev, const char *con_id) 154 84 { 155 85 const char *dev_id = dev ? dev_name(dev) : NULL; 86 + struct clk *clk; 87 + 88 + if (dev) { 89 + clk = of_clk_get_by_name(dev->of_node, con_id); 90 + if (clk && __clk_get(clk)) 91 + return clk; 92 + } 156 93 157 94 return clk_get_sys(dev_id, con_id); 158 95 }
+14
include/linux/clk-provider.h
··· 347 347 void __clk_reparent(struct clk *clk, struct clk *new_parent); 348 348 unsigned long __clk_round_rate(struct clk *clk, unsigned long rate); 349 349 350 + struct of_device_id; 351 + 352 + typedef void (*of_clk_init_cb_t)(struct device_node *); 353 + 354 + int of_clk_add_provider(struct device_node *np, 355 + struct clk *(*clk_src_get)(struct of_phandle_args *args, 356 + void *data), 357 + void *data); 358 + void of_clk_del_provider(struct device_node *np); 359 + struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec, 360 + void *data); 361 + const char *of_clk_get_parent_name(struct device_node *np, int index); 362 + void of_clk_init(const struct of_device_id *matches); 363 + 350 364 #endif /* CONFIG_COMMON_CLK */ 351 365 #endif /* CLK_PROVIDER_H */
+19
include/linux/clk.h
··· 310 310 int clk_add_alias(const char *alias, const char *alias_dev_name, char *id, 311 311 struct device *dev); 312 312 313 + struct device_node; 314 + struct of_phandle_args; 315 + 316 + #ifdef CONFIG_OF 317 + struct clk *of_clk_get(struct device_node *np, int index); 318 + struct clk *of_clk_get_by_name(struct device_node *np, const char *name); 319 + struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec); 320 + #else 321 + static inline struct clk *of_clk_get(struct device_node *np, int index) 322 + { 323 + return NULL; 324 + } 325 + static inline struct clk *of_clk_get_by_name(struct device_node *np, 326 + const char *name) 327 + { 328 + return NULL; 329 + } 330 + #endif 331 + 313 332 #endif