aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Documentation/devicetree/bindings/clock/clock-bindings.txt117
-rw-r--r--Documentation/devicetree/bindings/clock/fixed-clock.txt21
-rw-r--r--drivers/clk/clk.c140
-rw-r--r--drivers/clk/clkdev.c77
-rw-r--r--include/linux/clk-provider.h14
-rw-r--r--include/linux/clk.h19
6 files changed, 388 insertions, 0 deletions
diff --git a/Documentation/devicetree/bindings/clock/clock-bindings.txt b/Documentation/devicetree/bindings/clock/clock-bindings.txt
new file mode 100644
index 000000000000..eb65d417f8c4
--- /dev/null
+++ b/Documentation/devicetree/bindings/clock/clock-bindings.txt
@@ -0,0 +1,117 @@
1This binding is a work-in-progress, and are based on some experimental
2work by benh[1].
3
4Sources of clock signal can be represented by any node in the device
5tree. Those nodes are designated as clock providers. Clock consumer
6nodes use a phandle and clock specifier pair to connect clock provider
7outputs to clock inputs. Similar to the gpio specifiers, a clock
8specifier is an array of one more more cells identifying the clock
9output on a device. The length of a clock specifier is defined by the
10value of a #clock-cells property in the clock provider node.
11
12[1] http://patchwork.ozlabs.org/patch/31551/
13
14==Clock providers==
15
16Required 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
21Optional properties:
22clock-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
35For 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
49Required properties:
50clocks: 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
55Optional properties:
56clock-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.
60clock-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
64For example:
65
66 device {
67 clocks = <&osc 1>, <&ref 0>;
68 clock-names = "baud", "register";
69 };
70
71
72This represents a device with two clock inputs, named "baud" and "register".
73The baud clock is connected to output 1 of the &osc device, and the register
74clock 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
108This DT fragment defines three devices: an external oscillator to provide a
109low-frequency reference clock, a PLL device to generate a higher frequency
110clock 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)
diff --git a/Documentation/devicetree/bindings/clock/fixed-clock.txt b/Documentation/devicetree/bindings/clock/fixed-clock.txt
new file mode 100644
index 000000000000..0b1fe7824093
--- /dev/null
+++ b/Documentation/devicetree/bindings/clock/fixed-clock.txt
@@ -0,0 +1,21 @@
1Binding for simple fixed-rate clock sources.
2
3This binding uses the common clock binding[1].
4
5[1] Documentation/devicetree/bindings/clock/clock-bindings.txt
6
7Required 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
12Optional properties:
13- gpios : From common gpio binding; gpio connection to clock enable pin.
14- clock-output-names : From common clock binding.
15
16Example:
17 clock {
18 compatible = "fixed-clock";
19 #clock-cells = <0>;
20 clock-frequency = <1000000000>;
21 };
diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
index 46317cbc088f..c87fdd710560 100644
--- a/drivers/clk/clk.c
+++ b/drivers/clk/clk.c
@@ -16,6 +16,7 @@
16#include <linux/err.h> 16#include <linux/err.h>
17#include <linux/list.h> 17#include <linux/list.h>
18#include <linux/slab.h> 18#include <linux/slab.h>
19#include <linux/of.h>
19 20
20static DEFINE_SPINLOCK(enable_lock); 21static DEFINE_SPINLOCK(enable_lock);
21static DEFINE_MUTEX(prepare_lock); 22static DEFINE_MUTEX(prepare_lock);
@@ -1550,3 +1551,142 @@ int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
1550 return ret; 1551 return ret;
1551} 1552}
1552EXPORT_SYMBOL_GPL(clk_notifier_unregister); 1553EXPORT_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 */
1564struct 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
1572static LIST_HEAD(of_clk_providers);
1573static DEFINE_MUTEX(of_clk_lock);
1574
1575struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
1576 void *data)
1577{
1578 return data;
1579}
1580EXPORT_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 */
1588int 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}
1610EXPORT_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 */
1616void 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}
1631EXPORT_SYMBOL_GPL(of_clk_del_provider);
1632
1633struct 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
1651const 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}
1673EXPORT_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 */
1682void __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
diff --git a/drivers/clk/clkdev.c b/drivers/clk/clkdev.c
index c535cf8c5770..20649b3c88fe 100644
--- a/drivers/clk/clkdev.c
+++ b/drivers/clk/clkdev.c
@@ -19,10 +19,80 @@
19#include <linux/mutex.h> 19#include <linux/mutex.h>
20#include <linux/clk.h> 20#include <linux/clk.h>
21#include <linux/clkdev.h> 21#include <linux/clkdev.h>
22#include <linux/of.h>
22 23
23static LIST_HEAD(clocks); 24static LIST_HEAD(clocks);
24static DEFINE_MUTEX(clocks_mutex); 25static DEFINE_MUTEX(clocks_mutex);
25 26
27#ifdef CONFIG_OF
28struct 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}
46EXPORT_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 */
57struct 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}
93EXPORT_SYMBOL(of_clk_get_by_name);
94#endif
95
26/* 96/*
27 * Find the correct struct clk for the device and connection ID. 97 * Find the correct struct clk for the device and connection ID.
28 * We do slightly fuzzy matching here: 98 * We do slightly fuzzy matching here:
@@ -83,6 +153,13 @@ EXPORT_SYMBOL(clk_get_sys);
83struct clk *clk_get(struct device *dev, const char *con_id) 153struct clk *clk_get(struct device *dev, const char *con_id)
84{ 154{
85 const char *dev_id = dev ? dev_name(dev) : NULL; 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 return clk_get_sys(dev_id, con_id); 164 return clk_get_sys(dev_id, con_id);
88} 165}
diff --git a/include/linux/clk-provider.h b/include/linux/clk-provider.h
index 06ad617664a2..7c9c691102b5 100644
--- a/include/linux/clk-provider.h
+++ b/include/linux/clk-provider.h
@@ -347,5 +347,19 @@ void __clk_unprepare(struct clk *clk);
347void __clk_reparent(struct clk *clk, struct clk *new_parent); 347void __clk_reparent(struct clk *clk, struct clk *new_parent);
348unsigned long __clk_round_rate(struct clk *clk, unsigned long rate); 348unsigned long __clk_round_rate(struct clk *clk, unsigned long rate);
349 349
350struct of_device_id;
351
352typedef void (*of_clk_init_cb_t)(struct device_node *);
353
354int 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);
358void of_clk_del_provider(struct device_node *np);
359struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
360 void *data);
361const char *of_clk_get_parent_name(struct device_node *np, int index);
362void of_clk_init(const struct of_device_id *matches);
363
350#endif /* CONFIG_COMMON_CLK */ 364#endif /* CONFIG_COMMON_CLK */
351#endif /* CLK_PROVIDER_H */ 365#endif /* CLK_PROVIDER_H */
diff --git a/include/linux/clk.h b/include/linux/clk.h
index ad5c43e8ae8a..8b70342e7e0b 100644
--- a/include/linux/clk.h
+++ b/include/linux/clk.h
@@ -310,4 +310,23 @@ struct clk *clk_get_sys(const char *dev_id, const char *con_id);
310int clk_add_alias(const char *alias, const char *alias_dev_name, char *id, 310int clk_add_alias(const char *alias, const char *alias_dev_name, char *id,
311 struct device *dev); 311 struct device *dev);
312 312
313struct device_node;
314struct of_phandle_args;
315
316#ifdef CONFIG_OF
317struct clk *of_clk_get(struct device_node *np, int index);
318struct clk *of_clk_get_by_name(struct device_node *np, const char *name);
319struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec);
320#else
321static inline struct clk *of_clk_get(struct device_node *np, int index)
322{
323 return NULL;
324}
325static 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#endif 332#endif