When we write or modify driving, often encounter problems related to the clock, I do not know where to start. Herein, in the version 3.0.35 kernel i.MX6 example how to obtain and use the clock.
The most common way to get the clock
❶ acquisition by name
, for example: Get clock clko
clko = clk_get(NULL, "clko_clk");
❷ acquired by the apparatus
, for example, the driving clock is eligible ssi's audio interface,
ssi-> clk = clk_get (& pdev-> dev, NULL); // device name is "imx-ssi"
We can always check how is obtained by looking at the function prototype of clk_get
struct clk clk_get(struct device dev, const char *con_id)
{
const char *dev_id = dev ? dev_name(dev) : NULL;
return clk_get_sys(dev_id, con_id);
}
We continue to view the function clk_get_sys, this function to get the clock by equipment name or the name of the clock
struct clk clk_get_sys(const char dev_id, const char *con_id)
{
struct clk_lookup *cl;
mutex_lock(&clocks_mutex);
cl = clk_find(dev_id, con_id);
if (cl && !__clk_get(cl->clk))
cl = NULL;
mutex_unlock(&clocks_mutex);
return cl ? cl->clk : ERR_PTR(-ENOENT);
}
This function calls clk_find get the clock to find according to the device name and the name of the clock in the clock list, and return to get the clock.
static struct clk_lookup clk_find(const char dev_id, const char *con_id)
{
struct clk_lookup *p, *cl = NULL;
int match, best = 0;
list_for_each_entry(p, &clocks, node) {
match = 0;
if (p->dev_id) {
if (!dev_id || strcmp(p->dev_id,dev_id))
continue;
match += 2;
} // 先找设备名
if (p->con_id) {//再找时钟名
if (!con_id || strcmp(p->con_id,con_id))
continue;
match += 1;
}
if (match > best) {
cl = p;
if (match != 3)
best=match;
else
break;
}
}
return cl;}
Registration clock
mentioned the acquisition clock, the clock from the list within a list according to the clock or clock device name is how generated.
We call the clock initialization function in i.Mx6 board-level initialization file inside.
static void __init mx6_sabresd_timer_init(void)
{
struct clk *uart_clk;
#ifdef CONFIG_LOCAL_TIMERS
twd_base = ioremap(LOCAL_TWD_ADDR,SZ_256);
BUG_ON(!twd_base);
#endif
mx6_clocks_init(32768, 24000000, 0, 0);
uart_clk = clk_get_sys("imx-uart.0", NULL);
early_console_setup(UART1_BASE_ADDR, uart_clk);
}
We see mx6_clocks_init function
int __init mx6_clocks_init(unsigned long ckil, unsigned long osc,
unsigned long ckih1, unsigned long ckih2)
{
…
for (i = 0; i < ARRAY_SIZE(lookups); i++){
clkdev_add(&lookups[i]);
clk_debug_register(lookups[i].clk);
}
….
}
Inside look at the array of function lookups decided how many clock
static struct clk_lookup lookups[] = {
……
_REGISTER_CLOCK("imx-ssi.0", NULL, ssi1_clk),
_REGISTER_CLOCK("imx-ssi.1", NULL, ssi2_clk),
_REGISTER_CLOCK("imx-ssi.2", NULL, ssi3_clk),
_REGISTER_CLOCK(NULL, "clko_clk", clko_clk),
……
}
clk_lookup definition
struct clk_lookup {
struct list_head node;
const char *dev_id;
const char *con_id;
struct clk *clk;
};
clk_lookup assignment
#define _REGISTER_CLOCK(d, n, c) \
{ \
.dev_id = d, \
.con_id = n, \
.clk = &c, \
}
Look function clkdev_add
void clkdev_add(struct clk_lookup *cl){
mutex_lock(&clocks_mutex);
list_add_tail(&cl->node, &clocks);
mutex_unlock(&clocks_mutex);
}
Thus, we can find mx6_clocks_init clock function in the array lookups, all added to the list within clocks. We then get the clock defined by the first step in the lookups method.