Commit 766e6a4ec602d0c107553b91b3434fe9c03474f4
Committed by
Mike Turquette
1 parent
a613163dff
Exists in
smarc-l5.0.0_1.0.0-ga
and in
5 other branches
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>
Showing 6 changed files with 388 additions and 0 deletions Side-by-side Diff
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) |
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 | + }; |
drivers/clk/clk.c
... | ... | @@ -16,6 +16,7 @@ |
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); |
... | ... | @@ -1550,4 +1551,143 @@ |
1550 | 1551 | return ret; |
1551 | 1552 | } |
1552 | 1553 | EXPORT_SYMBOL_GPL(clk_notifier_unregister); |
1554 | + | |
1555 | +#ifdef CONFIG_OF | |
1556 | +/** | |
1557 | + * struct of_clk_provider - Clock provider registration structure | |
1558 | + * @link: Entry in global list of clock providers | |
1559 | + * @node: Pointer to device tree node of clock provider | |
1560 | + * @get: Get clock callback. Returns NULL or a struct clk for the | |
1561 | + * given clock specifier | |
1562 | + * @data: context pointer to be passed into @get callback | |
1563 | + */ | |
1564 | +struct of_clk_provider { | |
1565 | + struct list_head link; | |
1566 | + | |
1567 | + struct device_node *node; | |
1568 | + struct clk *(*get)(struct of_phandle_args *clkspec, void *data); | |
1569 | + void *data; | |
1570 | +}; | |
1571 | + | |
1572 | +static LIST_HEAD(of_clk_providers); | |
1573 | +static DEFINE_MUTEX(of_clk_lock); | |
1574 | + | |
1575 | +struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec, | |
1576 | + void *data) | |
1577 | +{ | |
1578 | + return data; | |
1579 | +} | |
1580 | +EXPORT_SYMBOL_GPL(of_clk_src_simple_get); | |
1581 | + | |
1582 | +/** | |
1583 | + * of_clk_add_provider() - Register a clock provider for a node | |
1584 | + * @np: Device node pointer associated with clock provider | |
1585 | + * @clk_src_get: callback for decoding clock | |
1586 | + * @data: context pointer for @clk_src_get callback. | |
1587 | + */ | |
1588 | +int of_clk_add_provider(struct device_node *np, | |
1589 | + struct clk *(*clk_src_get)(struct of_phandle_args *clkspec, | |
1590 | + void *data), | |
1591 | + void *data) | |
1592 | +{ | |
1593 | + struct of_clk_provider *cp; | |
1594 | + | |
1595 | + cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL); | |
1596 | + if (!cp) | |
1597 | + return -ENOMEM; | |
1598 | + | |
1599 | + cp->node = of_node_get(np); | |
1600 | + cp->data = data; | |
1601 | + cp->get = clk_src_get; | |
1602 | + | |
1603 | + mutex_lock(&of_clk_lock); | |
1604 | + list_add(&cp->link, &of_clk_providers); | |
1605 | + mutex_unlock(&of_clk_lock); | |
1606 | + pr_debug("Added clock from %s\n", np->full_name); | |
1607 | + | |
1608 | + return 0; | |
1609 | +} | |
1610 | +EXPORT_SYMBOL_GPL(of_clk_add_provider); | |
1611 | + | |
1612 | +/** | |
1613 | + * of_clk_del_provider() - Remove a previously registered clock provider | |
1614 | + * @np: Device node pointer associated with clock provider | |
1615 | + */ | |
1616 | +void of_clk_del_provider(struct device_node *np) | |
1617 | +{ | |
1618 | + struct of_clk_provider *cp; | |
1619 | + | |
1620 | + mutex_lock(&of_clk_lock); | |
1621 | + list_for_each_entry(cp, &of_clk_providers, link) { | |
1622 | + if (cp->node == np) { | |
1623 | + list_del(&cp->link); | |
1624 | + of_node_put(cp->node); | |
1625 | + kfree(cp); | |
1626 | + break; | |
1627 | + } | |
1628 | + } | |
1629 | + mutex_unlock(&of_clk_lock); | |
1630 | +} | |
1631 | +EXPORT_SYMBOL_GPL(of_clk_del_provider); | |
1632 | + | |
1633 | +struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec) | |
1634 | +{ | |
1635 | + struct of_clk_provider *provider; | |
1636 | + struct clk *clk = ERR_PTR(-ENOENT); | |
1637 | + | |
1638 | + /* Check if we have such a provider in our array */ | |
1639 | + mutex_lock(&of_clk_lock); | |
1640 | + list_for_each_entry(provider, &of_clk_providers, link) { | |
1641 | + if (provider->node == clkspec->np) | |
1642 | + clk = provider->get(clkspec, provider->data); | |
1643 | + if (!IS_ERR(clk)) | |
1644 | + break; | |
1645 | + } | |
1646 | + mutex_unlock(&of_clk_lock); | |
1647 | + | |
1648 | + return clk; | |
1649 | +} | |
1650 | + | |
1651 | +const char *of_clk_get_parent_name(struct device_node *np, int index) | |
1652 | +{ | |
1653 | + struct of_phandle_args clkspec; | |
1654 | + const char *clk_name; | |
1655 | + int rc; | |
1656 | + | |
1657 | + if (index < 0) | |
1658 | + return NULL; | |
1659 | + | |
1660 | + rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index, | |
1661 | + &clkspec); | |
1662 | + if (rc) | |
1663 | + return NULL; | |
1664 | + | |
1665 | + if (of_property_read_string_index(clkspec.np, "clock-output-names", | |
1666 | + clkspec.args_count ? clkspec.args[0] : 0, | |
1667 | + &clk_name) < 0) | |
1668 | + clk_name = clkspec.np->name; | |
1669 | + | |
1670 | + of_node_put(clkspec.np); | |
1671 | + return clk_name; | |
1672 | +} | |
1673 | +EXPORT_SYMBOL_GPL(of_clk_get_parent_name); | |
1674 | + | |
1675 | +/** | |
1676 | + * of_clk_init() - Scan and init clock providers from the DT | |
1677 | + * @matches: array of compatible values and init functions for providers. | |
1678 | + * | |
1679 | + * This function scans the device tree for matching clock providers and | |
1680 | + * calls their initialization functions | |
1681 | + */ | |
1682 | +void __init of_clk_init(const struct of_device_id *matches) | |
1683 | +{ | |
1684 | + struct device_node *np; | |
1685 | + | |
1686 | + for_each_matching_node(np, matches) { | |
1687 | + const struct of_device_id *match = of_match_node(matches, np); | |
1688 | + of_clk_init_cb_t clk_init_cb = match->data; | |
1689 | + clk_init_cb(np); | |
1690 | + } | |
1691 | +} | |
1692 | +#endif |
drivers/clk/clkdev.c
... | ... | @@ -19,10 +19,80 @@ |
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); |
25 | 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 | |
95 | + | |
26 | 96 | /* |
27 | 97 | * Find the correct struct clk for the device and connection ID. |
28 | 98 | * We do slightly fuzzy matching here: |
... | ... | @@ -83,6 +153,13 @@ |
83 | 153 | struct clk *clk_get(struct device *dev, const char *con_id) |
84 | 154 | { |
85 | 155 | const char *dev_id = dev ? dev_name(dev) : NULL; |
156 | + struct clk *clk; | |
157 | + | |
158 | + if (dev) { | |
159 | + clk = of_clk_get_by_name(dev->of_node, con_id); | |
160 | + if (clk && __clk_get(clk)) | |
161 | + return clk; | |
162 | + } | |
86 | 163 | |
87 | 164 | return clk_get_sys(dev_id, con_id); |
88 | 165 | } |
include/linux/clk-provider.h
... | ... | @@ -347,6 +347,20 @@ |
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 */ |
include/linux/clk.h
... | ... | @@ -310,5 +310,24 @@ |
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 |