
在Linux驅動中,操作時鐘只需要簡單調用內核提供的通用接口即可,clock驅動通常是由芯片廠商開發的,在Linux啟動時clock驅動就已經初始化完成。
本篇介紹Linux clock子系統以及clock驅動的實現。
晶振:晶源振蕩器,提供時鐘。
(資料圖)
PLL:Phase lock loop,鎖相環。 用于提升頻率。
OSC:oscillator的簡寫,振蕩器。
Linux的時鐘子系統由CCF(common clock framework)框架管理, CCF向上給用戶提供了通用的時鐘接口,向下給驅動開發者提供硬件操作的接口。 各結構體關系如下:
CCF框架比較簡單,只有這幾個結構體。 CCF框架分為了consumer、ccf和provider三部分。
consumer:
時鐘的使用者,clock子系統向consumer的提供通用的時鐘API接口,使其可以屏蔽底層硬件差異。 提供給consumer操作的API如下:
struct clk *clk_get(struct device *dev, const char *id);struct clk *devm_clk_get(struct device *dev, const char *id);int clk_enable(struct clk *clk);//使能時鐘,不會睡眠void clk_disable(struct clk *clk);//使能時鐘,不會睡眠unsigned long clk_get_rate(struct clk *clk);void clk_put(struct clk *clk);long clk_round_rate(struct clk *clk, unsigned long rate);int clk_set_rate(struct clk *clk, unsigned long rate);int clk_set_parent(struct clk *clk, struct clk *parent);struct clk *clk_get_parent(struct clk *clk);int clk_prepare(struct clk *clk);void clk_unprepare(struct clk *clk);int clk_prepare_enable(struct clk *clk) //使能時鐘,可能會睡眠void clk_disable_unprepare(struct clk *clk) //禁止時鐘,可能會睡眠unsigned long clk_get_rate(struct clk *clk) //獲取時鐘頻率
consumer在使用這些API時,必須先調用devm_clk_get()
或clk_get()
獲取一個struct clk *
指針句柄,后續都通過傳入該句柄來操作,struct clk相當于實例化一個時鐘。
ccf:
clock子系統的核心,用一個struct clk_core
結構體表示,每個注冊設備都對應一個struct clk_core
。
provider(時鐘的提供者):
struct clk_hw
:表示一個具體的硬件時鐘。
struct clk_init_data
:struct clk_hw結構體成員,用于表示該時鐘下的初始化數據,如時鐘名字name、操作函數ops等。
// include/linux/clk-provider.hstruct clk_hw{ struct clk_core *core; struct clk *clk; const struct clk_init_data *init;< font >< /font >}< font >< /font >< font >< /font >struct clk_init_data{ const char *name; //時鐘名字 const struct clk_ops *ops; //時鐘硬件操作函數集合 const char *const *parent_names; //父時鐘名字 const struct clk_parent_data *parent_data; const struct clk_hw **parent_hws;< font >< /font > u8 num_parents;< font >< /font > unsigned long flags;< font >< /font >}< font >< /font >
struct clk_ops
:時鐘硬件操作的函數集合,定義了操作硬件的回調函數,consumer在調用clk_set_rate()
等API時會調用到struct clk_ops
具體指向的函數,這個需要芯片廠商開發clock驅動時去實現。
//include/linux/clk-provider.h< font >< /font >< font >< /font >struct clk_ops { int (*prepare)(struct clk_hw *hw); void (*unprepare)(struct clk_hw *hw); int (*is_prepared)(struct clk_hw *hw); void (*unprepare_unused)(struct clk_hw *hw); int (*enable)(struct clk_hw *hw); void (*disable)(struct clk_hw *hw); int (*is_enabled)(struct clk_hw *hw); void (*disable_unused)(struct clk_hw *hw); int (*save_context)(struct clk_hw *hw); void (*restore_context)(struct clk_hw *hw); unsigned long (*recalc_rate)(struct clk_hw *hw, unsigned long parent_rate); long (*round_rate)(struct clk_hw *hw, unsigned long rate, unsigned long *parent_rate); int (*determine_rate)(struct clk_hw *hw, struct clk_rate_request *req); int (*set_parent)(struct clk_hw *hw, u8 index);< font >< /font > u8 (*get_parent)(struct clk_hw *hw); int (*set_rate)(struct clk_hw *hw, unsigned long rate, unsigned long parent_rate); int (*set_rate_and_parent)(struct clk_hw *hw, unsigned long rate, unsigned long parent_rate, u8 index); unsigned long (*recalc_accuracy)(struct clk_hw *hw, unsigned long parent_accuracy); int (*get_phase)(struct clk_hw *hw); int (*set_phase)(struct clk_hw *hw, int degrees); int (*get_duty_cycle)(struct clk_hw *hw, struct clk_duty *duty); int (*set_duty_cycle)(struct clk_hw *hw, struct clk_duty *duty); int (*init)(struct clk_hw *hw); void (*terminate)(struct clk_hw *hw); void (*debug_init)(struct clk_hw *hw, struct dentry *dentry);< font >< /font >};< font >< /font >
struct clk_ops中每個函數功能在include/linux/clk-provider.h
都有具體的說明,在開發clock驅動時,這些函數并不需要全部實現。下面列舉幾個最常用,也是經常需要實現的函數。
函數 | 說明 |
---|---|
recalc_rate | 通過查詢硬件,重新計算此時鐘的速率。可選,但建議——如果未設置此操作,則時鐘速率初始化為0。 |
round_rate | 給定目標速率作為輸入,返回時鐘實際支持的最接近速率。 |
set_rate | 更改此時鐘的速率。請求的速率由第二個參數指定,該參數通常應該是調用.round_rate返回。第三個參數給出了父速率,這對大多數.set_rate實現有幫助。成功返回0,否則返回-EERROR |
enable | 時鐘enable |
disable | 時鐘disable |
對于一般的驅動開發(非clock驅動),我們只需要在dts中配置時鐘,然后在驅動調用通用的時鐘API接口即可。
1、設備樹中配置時鐘
mmc0:mmc0@0x12345678{< font >< /font > compatible = "xx,xx-mmc0";< font >< /font > ......< font >< /font > clocks = < &peri PERI_MCI0 >;//指定mmc0的時鐘來自PERI_MCI0,PERI_MCI0的父時鐘是peri< font >< /font > clocks-names = "mmc0"; //時鐘名,調用devm_clk_get獲取時鐘時,可以傳入該名字< font >< /font > ......< font >< /font > };< font >< /font >
以mmc的設備節點為例,上述mmc0指定了時鐘來自PERI_MCI0,PERI_MCI0的父時鐘是peri,并將所指定的時鐘給它命名為"mmc0"。
2、驅動中使用API接口
簡單的使用:
/* 1、獲取時鐘 */host- >clk = devm_clk_get(&pdev- >dev, NULL); //或者devm_clk_get(&pdev- >dev, "mmc0") if (IS_ERR(host- >clk)) {< font >< /font > dev_err(dev, "failed to find clock source\\n");< font >< /font > ret = PTR_ERR(host- >clk);< font >< /font > goto probe_out_free_dev;< font >< /font > }< font >< /font >< font >< /font >/* 2、使能時鐘 */< font >< /font >ret = clk_prepare_enable(host- >clk);< font >< /font >if (ret) {< font >< /font > dev_err(dev, "failed to enable clock source.\\n"); goto probe_out_free_dev;< font >< /font >}< font >< /font >< font >< /font >probe_out_free_dev:< font >< /font > kfree(host);< font >< /font >
在驅動中操作時鐘,第一步需要獲取struct clk
指針句柄,后續都通過該指針進行操作,例如:設置頻率:
ret = clk_set_rate(host- >clk, 300000);
獲得頻率:
ret = clk_get_rate(host- >clk);
注意:devm_clk_get()的兩個參數是二選一,可以都傳入,也可以只傳入一個參數。
像i2c、mmc等這些外設驅動,通常只需要使能門控即可,因為這些外設并不是時鐘源,它們只有開關。如果直接調用clk_ser_rate
函數設置頻率,clk_set_rate
會向上傳遞,即設置它的父時鐘頻率。例如在該例子中直接調用clk_set_rate
函數,最終設置的是時鐘源peri
的頻率。
clock驅動在時鐘子系統中屬于provider,provider是時鐘的提供者,即具體的clock驅動。
clock驅動在Linux剛啟動的時候就要完成,比initcall
都要早期,因此clock驅動是在內核中進行實現。在內核的drivers/clk
目錄下,可以看到各個芯片廠商對各自芯片clock驅動的實現:
下面以一個簡單的時鐘樹,舉例說明一個芯片的時鐘驅動的大致實現過程:
通常來說,一個芯片的時鐘樹是比較固定的,例如,以下時鐘樹:
時鐘樹的 根節點一般是晶振時鐘,上圖根節點為24M晶振時鐘。根節點下面是PLL,PLL用于提升頻率。PPL0下又分頻給PERI、DSP和ISP。PLL1分頻給DDR和ENC。
對于PLL來說,PLL的頻率可以通過寄存器設置,但通常是固定的,所以PLL屬于 固定時鐘。
對PERI、DSP等模塊來說,它們的頻率來自于PLL的分頻,因此這些模塊的時鐘屬于 分頻時鐘。
設備樹中表示一個時鐘源,應有如下屬性,例如24M晶振時鐘:
clocks{< font >< /font > osc24M:osc24M{< font >< /font > compatible = "fixed-clock";< font >< /font > #clock-cells = < 0 >; clock-output-name = "osc24M"; clock-frequency = < 24000000 >;< font >< /font > };< font >< /font >};< font >< /font >
屬性 | 說明 |
---|---|
compatible | 驅動匹配名字 |
#clock-cells | 提供輸出時鐘的路數。#clock-cells為0時,代表輸出一路時鐘#clock-cells為1時,代表輸出2路時鐘。 |
#clock-output-names | 輸出時鐘的名字 |
#clock-frequency | 輸出時鐘的頻率 |
clock驅動編寫的基本步驟:
實現struct clk_ops
相關成員函數定義分配struct clk_onecell_data
結構體,初始化相關數據定義分配struct clk_init_data
結構體,初始化相關數據調用clk_register
將時鐘注冊進框架調用clk_register_clkdev
注冊時鐘設備調用of_clk_add_provider
,將clk provider存放到of_clk_provider鏈表中管理調用CLK_OF_DECLARE
聲明驅動fixed_clk針對像PLL這種具有固定頻率的時鐘,對于PLL,我們只需要實現.recalc_rate
函數。
設備樹:
#define PLL0_CLK 0< font >< /font >< font >< /font >clocks{< font >< /font > osc24M:osc24M{< font >< /font > compatible = "fixed-clock";< font >< /font > #clock-cells = < 0 >; clock-output-names = "osc24M"; clock-frequency = < 24000000 >;< font >< /font > };< font >< /font > pll0:pll0{< font >< /font > compatible = "xx, choogle-fixed-clk";< font >< /font > #clock-cells = < 0 >; clock-id = < PLL0_CLK >; clock-frequency = < 1000000000 >; clock-output-names = "pll0";< font >< /font > clocks = < &osc24M >;< font >< /font > };< font >< /font >};< font >< /font >
驅動:
#include < linux/clk-provier.h >#include < linux/clkdev.h >#include < linux/clk.h >#include < linux/module.h >#include < linux/of.h >#include < linux/of_address.h >#include < linux/platform_device.h >#include < linux/slab.h >#include < linux/delay.h >< font >< /font >< font >< /font >#define CLOCK_BASE 0X12340000#define CLOCK_SIZE 0X1000< font >< /font >< font >< /font >struct xx_fixed_clk{ void __iomem *reg;//保存映射后寄存器基址 unsigned long fixed_rate;//頻率 int id;//clock id struct clk_hw*;< font >< /font >};< font >< /font >static unsigned long xx_pll0_fixed_clk_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)< font >< /font >{< font >< /font > unsigned long recalc_rate; //硬件操作:查詢寄存器,獲得分頻系數,計算頻率然后返回 return recalc_rate;< font >< /font >}< font >< /font >< font >< /font >static struct clk_ops xx_pll0_fixed_clk_ops = {< font >< /font > .recalc_rate = xx_pll0_fixed_clk_recalc_rate,< font >< /font >};< font >< /font >< font >< /font >struct clk_ops *xx_fixed_clk_ops[] = {< font >< /font > &xx_pll0_fixed_clk_ops,< font >< /font >};< font >< /font >< font >< /font >struct clk * __init xx_register_fixed_clk(const char *name, const char *parent_name, void __iomem *res_reg, u32 fixed_rate, int id, const struct clk_ops *ops)< font >< /font >{< font >< /font > struct xx_fixed_clk *fixed_clk; struct clk *clk; struct clk_init_data init = {};< font >< /font >< font >< /font > fixed_clk = kzalloc(sizeof(*fixed_clk), GFP_KERNEL); if (!fixed_clk) return ERR_PTR(-ENOMEM);< font >< /font >< font >< /font > //初始化struct clk_init_data數據< font >< /font > init.name = name;< font >< /font > init.flags = CLK_IS_BASIC;< font >< /font > init.parent_names = parent_name ? &parent_name : NULL;< font >< /font > init.num_parents = parent_name ? 1 : 0;< font >< /font >< font >< /font > fixed_clk- >reg = res_reg;//保存映射后的基址 fixed_clk- >fixed_rate = fixed_rate;//保存頻率 fixed_clk- >id = id;//保存clock id< font >< /font >< font >< /font > fixed_clk- >hw.init = &init;< font >< /font >< font >< /font > //時鐘注冊 clk = clk_register(NULL, &fixed_clk- >hw); if (IS_ERR(clk))< font >< /font > kfree(fixed_clk);< font >< /font >< font >< /font > return clk;< font >< /font >}< font >< /font >< font >< /font >static void __init of_xx_fixed_clk_init(struct device_node *np)< font >< /font >{< font >< /font > struct clk_onecell_data *clk_data; const char *clk_name = np- >name;< font >< /font > < font >< /font > const char *parent_name = of_clk_get_parent_name(np, 0); void __iomem *res_reg = ioremap(CLOCK_BASE, CLOCK_SIZE);//寄存器基址映射< font >< /font >< font >< /font > u32 rate = -1; int clock_id, index, number;< font >< /font >< font >< /font > clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL); if (!clk_data ) return;< font >< /font >< font >< /font > number = of_property_count_u32_elems(np, "clock-id");< font >< /font > clk_data- >clks = kcalloc(number, sizeof(struct clk*), GFP_KERNEL); if (!clk_data- >clks) goto err_free_data;< font >< /font >< font >< /font > of_property_read_u32(np, "clock-frequency", &rate);< font >< /font >< font >< /font >< font >< /font > /** * 操作寄存器:初始化PLL時鐘頻率 * ...... */< font >< /font >< font >< /font > for (index=0; index< number; index++) {< font >< /font > of_property_read_string_index(np, "clock-output-names", index, &clk_name);< font >< /font > of_property_read_u32_index(np, "clock-id", index, &clock_id);< font >< /font >< font >< /font > clk_data- >clks[index] = xx_register_fixed_clk(clk_name, parent_name, < font >< /font > res_reg, rate, clock_id, ak_fixed_clk_ops[pll_id]);< font >< /font > if (IS_ERR(clk_data- >clks[index])) {< font >< /font > pr_err("%s register fixed clk failed: clk_name:%s, index = %d\\n",< font >< /font > __func__, clk_name, index);< font >< /font > WARN_ON(true); continue;< font >< /font > }< font >< /font > clk_register_clkdev(clk_data- >clks[index], clk_name, NULL);//注冊時鐘設備< font >< /font > }< font >< /font >< font >< /font > clk_data- >clk_num = number;< font >< /font > if (number == 1) {< font >< /font > of_clk_add_provider(np, of_clk_src_simple_get, clk_data- >clks[0]);< font >< /font > } else {< font >< /font > of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);< font >< /font > }< font >< /font > return;< font >< /font >< font >< /font >err_free_data:< font >< /font > kfree(clk_data);< font >< /font >< font >< /font >}< font >< /font >< font >< /font >CLK_OF_DECLARE(xx_fixed_clk, "xx,xx-fixed-clk", of_xx_fixed_clk_init);
peri的時鐘來自于Pll的分頻,對于這類時鐘,需要實現.round_rate
、.set_rate
、.recalc_rate
。
設備樹:
#define PLL0_CLK 0#defeine PLL0_FACTOR_PERI 0< font >< /font >< font >< /font >clocks{< font >< /font > osc24M:osc24M{//晶振時鐘 compatible = "fixed-clock"; #clock-cells = < 0 >; clock-output-names = "osc24M";< font >< /font > clock-frequency = < 24000000 >;< font >< /font > };< font >< /font > pll0:pll0{//pll倍頻時鐘 compatible = "xx, xx-fixed-clk"; #clock-cells = < 0 >; clock-id = < PLL0_CLK >;< font >< /font > clock-frequency = < 1000000000 >;< font >< /font > clock-output-names = "pll0";< font >< /font > clocks = < &osc24M >;//pll的父時鐘為24M晶振< font >< /font > };< font >< /font >< font >< /font > factor_pll0_clk:factor_pll0_clk{//pll分頻時鐘 compatible = "xx,xx-pll0-factor-clk"; #clock-cells = < 1 >; clock-id = < PLL0_FACTOR_PERI >;< font >< /font > clock-output-names = "pll0_peri";< font >< /font > clocks = < &pll0 PLL0_CLK >;//PERI子系統的父時鐘為pll0< font >< /font > };< font >< /font >};< font >< /font >
驅動:
static long xx_factor_pll0_clk_round_rate(struct clk_hw *hw, unsigned long rate, unsigned long *parent_rate)< font >< /font >{< font >< /font > unsigned long round_rate; //返回時鐘實際支持的最接近速率 return round_rate;< font >< /font >}< font >< /font >static int xx_factor_pll0_clk_set_rate(struct clk_hw *hw, unsigned long rate, unsigned long parent_rate)< font >< /font >{< font >< /font > int ret = 0; //操作寄存器,設置頻率 return ret;< font >< /font >}< font >< /font >< font >< /font >static unsigned long xx_factor_pll0_clk_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)< font >< /font >{< font >< /font > unsigned long recalc_rate; //查詢寄存器,獲得分頻系數,計算頻率然后返回 return recalc_rate;< font >< /font >< font >< /font >}< font >< /font >< font >< /font >const struct clk_ops xx_factor_clk_ops = {< font >< /font > .round_rate = xx_factor_pll0_clk_round_rate,//給定目標速率作為輸入,返回時鐘< font >< /font > .set_rate = xx_factor_pll0_clk_set_rate,< font >< /font > .recalc_rate = xx_factor_pll0_clk_recalc_rate,< font >< /font >}< font >< /font >< font >< /font >static void __init of_xx_factor_clk_init(struct device_node *np)< font >< /font >{< font >< /font > //驅動入口 //參考上述pll的注冊,唯一不同的就是struct clk_ops的成員函數實現< font >< /font >}< font >< /font >< font >< /font >CLK_OF_DECLARE(xx_factor_clk, "xx,xx-factor-clk", of_xx_facotr_clk_init);
門控就是開關,對于門控而言,我們只需要實現struct clk_ops
的.enable
和.disable
設備樹:
#define PLL0_CLK 0#defeine PLL0_FACTOR_PERI 0#define PERI_MCI0 0< font >< /font >< font >< /font >mmc0:mmc0@0x12345678{< font >< /font > compatible = "xx,xx-mmc0";< font >< /font > ......< font >< /font > clocks = < &peri PERI_MCI0 >;< font >< /font > clocks-names = "mmc0";< font >< /font > ......< font >< /font > };< font >< /font >< font >< /font >clocks{< font >< /font > osc24M:osc24M{< font >< /font > compatible = "fixed-clock";< font >< /font > #clock-cells = < 0 >; clock-output-names = "osc24M"; clock-frequency = < 24000000 >;< font >< /font > };< font >< /font > pll0:pll0{< font >< /font > compatible = "xx, xx-fixed-clk";< font >< /font > #clock-cells = < 0 >; clock-id = < PLL0_CLK >; clock-frequency = < 1000000000 >; clock-output-names = "pll0";< font >< /font > clocks = < &osc24M >;< font >< /font > };< font >< /font >< font >< /font > factor_pll0_clk:factor_pll0_clk{< font >< /font > compatible = "xx,xx-pll0-factor-clk";< font >< /font > #clock-cells = < 1 >; clock-id = < PLL0_FACTOR_PERI >; clock-output-names = "pll0_peri";< font >< /font > clocks = < &pll0 PLL0_CLK >;< font >< /font > };< font >< /font > < font >< /font > peri:peri{< font >< /font > compatible = "xx,xx-gate-clk";< font >< /font > #clock-cells = < 1 >;< font >< /font > /*peri gate*/< font >< /font > clock-id = < PERI_MCI0 >; clock-output-names = "mci0_peri";< font >< /font > clocks = < &factor_pll0_clk PLL0_FACTOR_PERI >;< font >< /font > };< font >< /font >};< font >< /font >
驅動:
static int xx_gate_clk_enable(struct clk_hw *hw)< font >< /font >{< font >< /font > //寄存器操作,打開門控 return 0;< font >< /font >}< font >< /font >< font >< /font >static int xx_gate_clk_disable(struct clk_hw *hw)< font >< /font >{< font >< /font > //寄存器操作,門控關 return 0;< font >< /font >}< font >< /font >< font >< /font >const struct clk_ops ak_gate_clk_ops = {< font >< /font > .enable = xx_gate_clk_enable,< font >< /font > .disable = xx_gate_clk_disable,< font >< /font >}< font >< /font >< font >< /font >static void __init of_xx_gate_clk_init(struct device_node *np)< font >< /font >{< font >< /font > //參考上述fixed_clk的注冊,幾乎相同,只不過操作函數clk_ops的實現不一樣< font >< /font >}< font >< /font >< font >< /font >CLK_OF_DECLARE(xx_gate_clk, "xx,xx-gate-clk", of_xx_gate_clk_init);
每個芯片廠商在clock驅動的實現上都有很大的差異,上述只是對clock驅動實現的簡單舉例,作為參考。 對于一般的驅動,只需要會簡單的使用內核提供的時鐘API接口即可。
標簽: