2017-05-14 14:15:12 +02:00
|
|
|
========================
|
|
|
|
The Common Clk Framework
|
|
|
|
========================
|
|
|
|
|
|
|
|
:Author: Mike Turquette <mturquette@ti.com>
|
2012-03-16 07:11:18 +01:00
|
|
|
|
|
|
|
This document endeavours to explain the common clk framework details,
|
|
|
|
and how to port a platform over to this framework. It is not yet a
|
|
|
|
detailed explanation of the clock api in include/linux/clk.h, but
|
|
|
|
perhaps someday it will include that information.
|
|
|
|
|
2017-05-14 14:15:12 +02:00
|
|
|
Introduction and interface split
|
|
|
|
================================
|
2012-03-16 07:11:18 +01:00
|
|
|
|
|
|
|
The common clk framework is an interface to control the clock nodes
|
|
|
|
available on various devices today. This may come in the form of clock
|
|
|
|
gating, rate adjustment, muxing or other operations. This framework is
|
|
|
|
enabled with the CONFIG_COMMON_CLK option.
|
|
|
|
|
|
|
|
The interface itself is divided into two halves, each shielded from the
|
|
|
|
details of its counterpart. First is the common definition of struct
|
|
|
|
clk which unifies the framework-level accounting and infrastructure that
|
|
|
|
has traditionally been duplicated across a variety of platforms. Second
|
|
|
|
is a common implementation of the clk.h api, defined in
|
|
|
|
drivers/clk/clk.c. Finally there is struct clk_ops, whose operations
|
|
|
|
are invoked by the clk api implementation.
|
|
|
|
|
|
|
|
The second half of the interface is comprised of the hardware-specific
|
|
|
|
callbacks registered with struct clk_ops and the corresponding
|
|
|
|
hardware-specific structures needed to model a particular clock. For
|
|
|
|
the remainder of this document any reference to a callback in struct
|
|
|
|
clk_ops, such as .enable or .set_rate, implies the hardware-specific
|
|
|
|
implementation of that code. Likewise, references to struct clk_foo
|
|
|
|
serve as a convenient shorthand for the implementation of the
|
|
|
|
hardware-specific bits for the hypothetical "foo" hardware.
|
|
|
|
|
|
|
|
Tying the two halves of this interface together is struct clk_hw, which
|
2016-08-12 14:41:25 +02:00
|
|
|
is defined in struct clk_foo and pointed to within struct clk_core. This
|
2013-06-10 06:32:39 +02:00
|
|
|
allows for easy navigation between the two discrete halves of the common
|
2012-03-16 07:11:18 +01:00
|
|
|
clock interface.
|
|
|
|
|
2017-05-14 14:15:12 +02:00
|
|
|
Common data structures and api
|
|
|
|
==============================
|
2012-03-16 07:11:18 +01:00
|
|
|
|
2016-08-12 14:41:25 +02:00
|
|
|
Below is the common struct clk_core definition from
|
2017-05-14 14:15:12 +02:00
|
|
|
drivers/clk/clk.c, modified for brevity::
|
2012-03-16 07:11:18 +01:00
|
|
|
|
2016-08-12 14:41:25 +02:00
|
|
|
struct clk_core {
|
2012-03-16 07:11:18 +01:00
|
|
|
const char *name;
|
|
|
|
const struct clk_ops *ops;
|
|
|
|
struct clk_hw *hw;
|
2016-08-12 14:41:25 +02:00
|
|
|
struct module *owner;
|
|
|
|
struct clk_core *parent;
|
|
|
|
const char **parent_names;
|
|
|
|
struct clk_core **parents;
|
|
|
|
u8 num_parents;
|
|
|
|
u8 new_parent_index;
|
2012-03-16 07:11:18 +01:00
|
|
|
...
|
|
|
|
};
|
|
|
|
|
|
|
|
The members above make up the core of the clk tree topology. The clk
|
|
|
|
api itself defines several driver-facing functions which operate on
|
|
|
|
struct clk. That api is documented in include/linux/clk.h.
|
|
|
|
|
2016-08-12 14:41:25 +02:00
|
|
|
Platforms and devices utilizing the common struct clk_core use the struct
|
|
|
|
clk_ops pointer in struct clk_core to perform the hardware-specific parts of
|
2017-05-14 14:15:12 +02:00
|
|
|
the operations defined in clk-provider.h::
|
2012-03-16 07:11:18 +01:00
|
|
|
|
|
|
|
struct clk_ops {
|
|
|
|
int (*prepare)(struct clk_hw *hw);
|
|
|
|
void (*unprepare)(struct clk_hw *hw);
|
2016-08-12 14:41:25 +02:00
|
|
|
int (*is_prepared)(struct clk_hw *hw);
|
|
|
|
void (*unprepare_unused)(struct clk_hw *hw);
|
2012-03-16 07:11:18 +01:00
|
|
|
int (*enable)(struct clk_hw *hw);
|
|
|
|
void (*disable)(struct clk_hw *hw);
|
|
|
|
int (*is_enabled)(struct clk_hw *hw);
|
2016-08-12 14:41:25 +02:00
|
|
|
void (*disable_unused)(struct clk_hw *hw);
|
2012-03-16 07:11:18 +01:00
|
|
|
unsigned long (*recalc_rate)(struct clk_hw *hw,
|
|
|
|
unsigned long parent_rate);
|
2014-04-22 15:11:42 +02:00
|
|
|
long (*round_rate)(struct clk_hw *hw,
|
|
|
|
unsigned long rate,
|
|
|
|
unsigned long *parent_rate);
|
2015-07-07 20:48:08 +02:00
|
|
|
int (*determine_rate)(struct clk_hw *hw,
|
|
|
|
struct clk_rate_request *req);
|
2012-03-16 07:11:18 +01:00
|
|
|
int (*set_parent)(struct clk_hw *hw, u8 index);
|
|
|
|
u8 (*get_parent)(struct clk_hw *hw);
|
2014-04-22 15:11:42 +02:00
|
|
|
int (*set_rate)(struct clk_hw *hw,
|
|
|
|
unsigned long rate,
|
|
|
|
unsigned long parent_rate);
|
2014-01-15 19:47:22 +01:00
|
|
|
int (*set_rate_and_parent)(struct clk_hw *hw,
|
|
|
|
unsigned long rate,
|
2014-04-22 15:11:42 +02:00
|
|
|
unsigned long parent_rate,
|
|
|
|
u8 index);
|
2013-12-21 10:34:47 +01:00
|
|
|
unsigned long (*recalc_accuracy)(struct clk_hw *hw,
|
2014-04-22 15:11:42 +02:00
|
|
|
unsigned long parent_accuracy);
|
2016-08-12 14:41:25 +02:00
|
|
|
int (*get_phase)(struct clk_hw *hw);
|
|
|
|
int (*set_phase)(struct clk_hw *hw, int degrees);
|
2012-03-16 07:11:18 +01:00
|
|
|
void (*init)(struct clk_hw *hw);
|
2014-04-22 15:11:42 +02:00
|
|
|
int (*debug_init)(struct clk_hw *hw,
|
|
|
|
struct dentry *dentry);
|
2012-03-16 07:11:18 +01:00
|
|
|
};
|
|
|
|
|
2017-05-14 14:15:12 +02:00
|
|
|
Hardware clk implementations
|
|
|
|
============================
|
2012-03-16 07:11:18 +01:00
|
|
|
|
2016-08-12 14:41:25 +02:00
|
|
|
The strength of the common struct clk_core comes from its .ops and .hw pointers
|
2012-03-16 07:11:18 +01:00
|
|
|
which abstract the details of struct clk from the hardware-specific bits, and
|
|
|
|
vice versa. To illustrate consider the simple gateable clk implementation in
|
2017-05-14 14:15:12 +02:00
|
|
|
drivers/clk/clk-gate.c::
|
2012-03-16 07:11:18 +01:00
|
|
|
|
2017-05-14 14:15:12 +02:00
|
|
|
struct clk_gate {
|
|
|
|
struct clk_hw hw;
|
|
|
|
void __iomem *reg;
|
|
|
|
u8 bit_idx;
|
|
|
|
...
|
|
|
|
};
|
2012-03-16 07:11:18 +01:00
|
|
|
|
|
|
|
struct clk_gate contains struct clk_hw hw as well as hardware-specific
|
|
|
|
knowledge about which register and bit controls this clk's gating.
|
|
|
|
Nothing about clock topology or accounting, such as enable_count or
|
|
|
|
notifier_count, is needed here. That is all handled by the common
|
2016-08-12 14:41:25 +02:00
|
|
|
framework code and struct clk_core.
|
2012-03-16 07:11:18 +01:00
|
|
|
|
2017-05-14 14:15:12 +02:00
|
|
|
Let's walk through enabling this clk from driver code::
|
2012-03-16 07:11:18 +01:00
|
|
|
|
|
|
|
struct clk *clk;
|
|
|
|
clk = clk_get(NULL, "my_gateable_clk");
|
|
|
|
|
|
|
|
clk_prepare(clk);
|
|
|
|
clk_enable(clk);
|
|
|
|
|
2017-05-14 14:15:12 +02:00
|
|
|
The call graph for clk_enable is very simple::
|
2012-03-16 07:11:18 +01:00
|
|
|
|
2017-05-14 14:15:12 +02:00
|
|
|
clk_enable(clk);
|
|
|
|
clk->ops->enable(clk->hw);
|
|
|
|
[resolves to...]
|
|
|
|
clk_gate_enable(hw);
|
|
|
|
[resolves struct clk gate with to_clk_gate(hw)]
|
|
|
|
clk_gate_set_bit(gate);
|
2012-03-16 07:11:18 +01:00
|
|
|
|
2017-05-14 14:15:12 +02:00
|
|
|
And the definition of clk_gate_set_bit::
|
2012-03-16 07:11:18 +01:00
|
|
|
|
2017-05-14 14:15:12 +02:00
|
|
|
static void clk_gate_set_bit(struct clk_gate *gate)
|
|
|
|
{
|
|
|
|
u32 reg;
|
2012-03-16 07:11:18 +01:00
|
|
|
|
2017-05-14 14:15:12 +02:00
|
|
|
reg = __raw_readl(gate->reg);
|
|
|
|
reg |= BIT(gate->bit_idx);
|
|
|
|
writel(reg, gate->reg);
|
|
|
|
}
|
2012-03-16 07:11:18 +01:00
|
|
|
|
2017-05-14 14:15:12 +02:00
|
|
|
Note that to_clk_gate is defined as::
|
2012-03-16 07:11:18 +01:00
|
|
|
|
2017-05-14 14:15:12 +02:00
|
|
|
#define to_clk_gate(_hw) container_of(_hw, struct clk_gate, hw)
|
2012-03-16 07:11:18 +01:00
|
|
|
|
|
|
|
This pattern of abstraction is used for every clock hardware
|
|
|
|
representation.
|
|
|
|
|
2017-05-14 14:15:12 +02:00
|
|
|
Supporting your own clk hardware
|
|
|
|
================================
|
2012-03-16 07:11:18 +01:00
|
|
|
|
2016-08-12 14:41:25 +02:00
|
|
|
When implementing support for a new type of clock it is only necessary to
|
2017-05-14 14:15:12 +02:00
|
|
|
include the following header::
|
2012-03-16 07:11:18 +01:00
|
|
|
|
2017-05-14 14:15:12 +02:00
|
|
|
#include <linux/clk-provider.h>
|
2012-03-16 07:11:18 +01:00
|
|
|
|
|
|
|
To construct a clk hardware structure for your platform you must define
|
2017-05-14 14:15:12 +02:00
|
|
|
the following::
|
2012-03-16 07:11:18 +01:00
|
|
|
|
2017-05-14 14:15:12 +02:00
|
|
|
struct clk_foo {
|
|
|
|
struct clk_hw hw;
|
|
|
|
... hardware specific data goes here ...
|
|
|
|
};
|
2012-03-16 07:11:18 +01:00
|
|
|
|
|
|
|
To take advantage of your data you'll need to support valid operations
|
2017-05-14 14:15:12 +02:00
|
|
|
for your clk::
|
2012-03-16 07:11:18 +01:00
|
|
|
|
2017-05-14 14:15:12 +02:00
|
|
|
struct clk_ops clk_foo_ops {
|
|
|
|
.enable = &clk_foo_enable;
|
|
|
|
.disable = &clk_foo_disable;
|
|
|
|
};
|
2012-03-16 07:11:18 +01:00
|
|
|
|
2017-05-14 14:15:12 +02:00
|
|
|
Implement the above functions using container_of::
|
2012-03-16 07:11:18 +01:00
|
|
|
|
2017-05-14 14:15:12 +02:00
|
|
|
#define to_clk_foo(_hw) container_of(_hw, struct clk_foo, hw)
|
2012-03-16 07:11:18 +01:00
|
|
|
|
2017-05-14 14:15:12 +02:00
|
|
|
int clk_foo_enable(struct clk_hw *hw)
|
|
|
|
{
|
|
|
|
struct clk_foo *foo;
|
2012-03-16 07:11:18 +01:00
|
|
|
|
2017-05-14 14:15:12 +02:00
|
|
|
foo = to_clk_foo(hw);
|
2012-03-16 07:11:18 +01:00
|
|
|
|
2017-05-14 14:15:12 +02:00
|
|
|
... perform magic on foo ...
|
2012-03-16 07:11:18 +01:00
|
|
|
|
2017-05-14 14:15:12 +02:00
|
|
|
return 0;
|
|
|
|
};
|
2012-03-16 07:11:18 +01:00
|
|
|
|
|
|
|
Below is a matrix detailing which clk_ops are mandatory based upon the
|
2013-02-28 14:59:07 +01:00
|
|
|
hardware capabilities of that clock. A cell marked as "y" means
|
2012-03-16 07:11:18 +01:00
|
|
|
mandatory, a cell marked as "n" implies that either including that
|
2013-02-28 14:59:07 +01:00
|
|
|
callback is invalid or otherwise unnecessary. Empty cells are either
|
2012-03-16 07:11:18 +01:00
|
|
|
optional or must be evaluated on a case-by-case basis.
|
|
|
|
|
2017-05-14 14:15:12 +02:00
|
|
|
.. table:: clock hardware characteristics
|
|
|
|
|
|
|
|
+----------------+------+-------------+---------------+-------------+------+
|
|
|
|
| | gate | change rate | single parent | multiplexer | root |
|
|
|
|
+================+======+=============+===============+=============+======+
|
|
|
|
|.prepare | | | | | |
|
|
|
|
+----------------+------+-------------+---------------+-------------+------+
|
|
|
|
|.unprepare | | | | | |
|
|
|
|
+----------------+------+-------------+---------------+-------------+------+
|
|
|
|
+----------------+------+-------------+---------------+-------------+------+
|
|
|
|
|.enable | y | | | | |
|
|
|
|
+----------------+------+-------------+---------------+-------------+------+
|
|
|
|
|.disable | y | | | | |
|
|
|
|
+----------------+------+-------------+---------------+-------------+------+
|
|
|
|
|.is_enabled | y | | | | |
|
|
|
|
+----------------+------+-------------+---------------+-------------+------+
|
|
|
|
+----------------+------+-------------+---------------+-------------+------+
|
|
|
|
|.recalc_rate | | y | | | |
|
|
|
|
+----------------+------+-------------+---------------+-------------+------+
|
|
|
|
|.round_rate | | y [1]_ | | | |
|
|
|
|
+----------------+------+-------------+---------------+-------------+------+
|
|
|
|
|.determine_rate | | y [1]_ | | | |
|
|
|
|
+----------------+------+-------------+---------------+-------------+------+
|
|
|
|
|.set_rate | | y | | | |
|
|
|
|
+----------------+------+-------------+---------------+-------------+------+
|
|
|
|
+----------------+------+-------------+---------------+-------------+------+
|
|
|
|
|.set_parent | | | n | y | n |
|
|
|
|
+----------------+------+-------------+---------------+-------------+------+
|
|
|
|
|.get_parent | | | n | y | n |
|
|
|
|
+----------------+------+-------------+---------------+-------------+------+
|
|
|
|
+----------------+------+-------------+---------------+-------------+------+
|
|
|
|
|.recalc_accuracy| | | | | |
|
|
|
|
+----------------+------+-------------+---------------+-------------+------+
|
|
|
|
+----------------+------+-------------+---------------+-------------+------+
|
|
|
|
|.init | | | | | |
|
|
|
|
+----------------+------+-------------+---------------+-------------+------+
|
|
|
|
|
|
|
|
.. [1] either one of round_rate or determine_rate is required.
|
2012-03-16 07:11:18 +01:00
|
|
|
|
|
|
|
Finally, register your clock at run-time with a hardware-specific
|
|
|
|
registration function. This function simply populates struct clk_foo's
|
|
|
|
data and then passes the common struct clk parameters to the framework
|
2017-05-14 14:15:12 +02:00
|
|
|
with a call to::
|
2012-03-16 07:11:18 +01:00
|
|
|
|
2017-05-14 14:15:12 +02:00
|
|
|
clk_register(...)
|
2012-03-16 07:11:18 +01:00
|
|
|
|
2017-05-14 14:15:12 +02:00
|
|
|
See the basic clock types in ``drivers/clk/clk-*.c`` for examples.
|
2012-03-16 07:11:18 +01:00
|
|
|
|
2017-05-14 14:15:12 +02:00
|
|
|
Disabling clock gating of unused clocks
|
|
|
|
=======================================
|
2013-04-27 23:10:18 +02:00
|
|
|
|
|
|
|
Sometimes during development it can be useful to be able to bypass the
|
|
|
|
default disabling of unused clocks. For example, if drivers aren't enabling
|
|
|
|
clocks properly but rely on them being on from the bootloader, bypassing
|
|
|
|
the disabling means that the driver will remain functional while the issues
|
|
|
|
are sorted out.
|
|
|
|
|
|
|
|
To bypass this disabling, include "clk_ignore_unused" in the bootargs to the
|
|
|
|
kernel.
|
2014-02-28 13:40:56 +01:00
|
|
|
|
2017-05-14 14:15:12 +02:00
|
|
|
Locking
|
|
|
|
=======
|
2014-02-28 13:40:56 +01:00
|
|
|
|
|
|
|
The common clock framework uses two global locks, the prepare lock and the
|
|
|
|
enable lock.
|
|
|
|
|
|
|
|
The enable lock is a spinlock and is held across calls to the .enable,
|
|
|
|
.disable and .is_enabled operations. Those operations are thus not allowed to
|
|
|
|
sleep, and calls to the clk_enable(), clk_disable() and clk_is_enabled() API
|
|
|
|
functions are allowed in atomic context.
|
|
|
|
|
|
|
|
The prepare lock is a mutex and is held across calls to all other operations.
|
|
|
|
All those operations are allowed to sleep, and calls to the corresponding API
|
|
|
|
functions are not allowed in atomic context.
|
|
|
|
|
|
|
|
This effectively divides operations in two groups from a locking perspective.
|
|
|
|
|
|
|
|
Drivers don't need to manually protect resources shared between the operations
|
|
|
|
of one group, regardless of whether those resources are shared by multiple
|
|
|
|
clocks or not. However, access to resources that are shared between operations
|
|
|
|
of the two groups needs to be protected by the drivers. An example of such a
|
|
|
|
resource would be a register that controls both the clock rate and the clock
|
|
|
|
enable/disable state.
|
|
|
|
|
|
|
|
The clock framework is reentrant, in that a driver is allowed to call clock
|
|
|
|
framework functions from within its implementation of clock operations. This
|
|
|
|
can for instance cause a .set_rate operation of one clock being called from
|
|
|
|
within the .set_rate operation of another clock. This case must be considered
|
|
|
|
in the driver implementations, but the code flow is usually controlled by the
|
|
|
|
driver in that case.
|
|
|
|
|
|
|
|
Note that locking must also be considered when code outside of the common
|
|
|
|
clock framework needs to access resources used by the clock operations. This
|
|
|
|
is considered out of scope of this document.
|