深圳网站建设服务公司,公司官网怎么建立,营业推广促销方式有哪些,网站建设费属于广告费吗文章目录基本概念CLK子系统时钟API的使用clock驱动实例1、时钟树2、设备树3、驱动实现fixed_clk固定时钟实现factor_clk分频时钟实现gate_clk门控时钟实现基本概念 晶振#xff1a;晶源振荡器 PLL#xff1a;Phase lock loop#xff0c;锁相环。用于提升频率 OSC#xff1a…
文章目录基本概念CLK子系统时钟API的使用clock驱动实例1、时钟树2、设备树3、驱动实现fixed_clk固定时钟实现factor_clk分频时钟实现gate_clk门控时钟实现基本概念 晶振晶源振荡器 PLLPhase lock loop锁相环。用于提升频率 OSCoscillator的简写振荡器
CLK子系统 Linux的时钟子系统由CCFcommon 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_datastruct clk_hw结构体成员用于表示该时钟下的初始化数据如时钟名字name、操作函数ops等。
// include/linux/clk-provider.h
struct clk_hw{struct clk_core *core;struct clk *clk;const struct clk_init_data *init;
}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;u8 num_parents;unsigned long flags;
}struct clk_ops时钟硬件操作的函数集合定义了操作硬件的回调函数consumer在调用clk_set_rate()等API时会调用到struct clk_ops具体指向的函数这个需要芯片厂商开发clock驱动时去实现。
//include/linux/clk-provider.hstruct 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);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);
};struct clk_ops中每个函数功能在include/linux/clk-provider.h都有具体的说明在开发clock驱动时这些函数并不需要全部实现。下面列举几个最常用也是经常需要实现的函数。
函数说明recalc_rate通过查询硬件重新计算此时钟的速率。可选但建议——如果未设置此操作则时钟速率初始化为0。round_rate给定目标速率作为输入返回时钟实际支持的最接近速率。set_rate更改此时钟的速率。请求的速率由第二个参数指定该参数通常应该是调用.round_rate返回。第三个参数给出了父速率这对大多数.set_rate实现有帮助。成功返回0否则返回-EERRORenable时钟enabledisable时钟disable
时钟API的使用 对于一般的驱动开发非clock驱动我们只需要在dts中配置时钟然后在驱动调用通用的时钟API接口即可。 1、设备树中配置时钟 mmc0:mmc00x12345678{compatible xx,xx-mmc0;......clocks peri PERI_MCI0;//指定mmc0的时钟来自PERI_MCI0PERI_MCI0的父时钟是periclocks-names mmc0; //时钟名调用devm_clk_get获取时钟时可以传入该名字......};以mmc的设备节点为例上述mmc0指定了时钟来自PERI_MCI0PERI_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)) {dev_err(dev, failed to find clock source\n);ret PTR_ERR(host-clk);goto probe_out_free_dev;}/* 2、使能时钟 */
ret clk_prepare_enable(host-clk);
if (ret) {dev_err(dev, failed to enable clock source.\n);goto probe_out_free_dev;
}probe_out_free_dev:kfree(host);在驱动中操作时钟第一步需要获取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驱动实例 clock驱动在时钟子系统中属于providerprovider是时钟的提供者即具体的clock驱动。clock驱动在Linux刚启动的时候就要完成比initcall都要早期因此clock驱动是在内核中进行实现。在内核的drivers/clk目录下可以看到各个芯片厂商对各自芯片clock驱动的实现 下面以一个简单的时钟树举例说明一个芯片的时钟驱动的大致实现过程
1、时钟树
通常来说一个芯片的时钟树是比较固定的例如以下时钟树 时钟树的根节点一般是晶振时钟上图根节点为24M晶振时钟。根节点下面是PLLPLL用于提升频率。PPL0下又分频给PERI、DSP和ISP。PLL1分频给DDR和ENC。
对于PLL来说PLL的频率可以通过寄存器设置但通常是固定的所以PLL属于固定时钟。
对PERI、DSP等模块来说它们的频率来自于PLL的分频因此这些模块的时钟属于分频时钟。
2、设备树
设备树中表示一个时钟源应有如下属性例如24Mosc
clocks{osc24M:osc24M{compatible fixed-clock;#clock-cells 0;clock-output-name osc24M;clock-frequency 24000000;};
};属性说明compatible驱动匹配名字#clock-cells提供输出时钟的路数。#clock-cells为0时代表输出一路时钟#clock-cells为1时代表输出2路时钟。#clock-output-names输出时钟的名字#clock-frequency输出时钟的频率
3、驱动实现
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固定时钟实现
fixed_clk针对像PLL这种具有固定频率的时钟对于PLL我们只需要实现.recalc_rate函数 设备树
#define PLL0_CLK 0clocks{osc24M:osc24M{compatible fixed-clock;#clock-cells 0;clock-output-names osc24M;clock-frequency 24000000;};pll0:pll0{compatible xx, choogle-fixed-clk;#clock-cells 0;clock-id PLL0_CLK;clock-frequency 1000000000;clock-output-names pll0;clocks osc24M;};
};驱动
#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#define CLOCK_BASE 0X12340000
#define CLOCK_SIZE 0X1000struct xx_fixed_clk{void __iomem *reg;//保存映射后寄存器基址unsigned long fixed_rate;//频率int id;//clock idstruct clk_hw*;
}
static unsigned long xx_pll0_fixed_clk_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
{unsigned long recalc_rate;//硬件操作查询寄存器获得分频系数计算频率然后返回return recalc_rate;
}static struct clk_ops xx_pll0_fixed_clk_ops {.recalc_rate xx_pll0_fixed_clk_recalc_rate,
};struct clk_ops *xx_fixed_clk_ops[] {xx_pll0_fixed_clk_ops,
};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)
{struct xx_fixed_clk *fixed_clk;struct clk *clk;struct clk_init_data init {};fixed_clk kzalloc(sizeof(*fixed_clk), GFP_KERNEL);if (!fixed_clk)return ERR_PTR(-ENOMEM);//初始化struct clk_init_data数据init.name name;init.flags CLK_IS_BASIC;init.parent_names parent_name ? parent_name : NULL;init.num_parents parent_name ? 1 : 0;fixed_clk-reg res_reg;//保存映射后的基址fixed_clk-fixed_rate fixed_rate;//保存频率fixed_clk-id id;//保存clock idfixed_clk-hw.init init;//时钟注册clk clk_register(NULL, fixed_clk-hw);if (IS_ERR(clk))kfree(fixed_clk);return clk;
}static void __init of_xx_fixed_clk_init(struct device_node *np)
{struct clk_onecell_data *clk_data;const char *clk_name np-name;const char *parent_name of_clk_get_parent_name(np, 0);void __iomem *res_reg ioremap(CLOCK_BASE, CLOCK_SIZE);//寄存器基址映射u32 rate -1;int clock_id, index, number;clk_data kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);if (!clk_data )return;number of_property_count_u32_elems(np, clock-id);clk_data-clks kcalloc(number, sizeof(struct clk*), GFP_KERNEL);if (!clk_data-clks)goto err_free_data;of_property_read_u32(np, clock-frequency, rate);/*** 操作寄存器初始化PLL时钟频率* ......*/for (index0; indexnumber; index) {of_property_read_string_index(np, clock-output-names, index, clk_name);of_property_read_u32_index(np, clock-id, index, clock_id);clk_data-clks[index] xx_register_fixed_clk(clk_name, parent_name, res_reg, rate, clock_id, ak_fixed_clk_ops[pll_id]);if (IS_ERR(clk_data-clks[index])) {pr_err(%s register fixed clk failed: clk_name:%s, index %d\n,__func__, clk_name, index);WARN_ON(true);continue;}clk_register_clkdev(clk_data-clks[index], clk_name, NULL);//注册时钟设备}clk_data-clk_num number;if (number 1) {of_clk_add_provider(np, of_clk_src_simple_get, clk_data-clks[0]);} else {of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);}return;err_free_data:kfree(clk_data);}CLK_OF_DECLARE(xx_fixed_clk, xx,xx-fixed-clk, of_xx_fixed_clk_init);factor_clk分频时钟实现
peri的时钟来自于Pll的分频对于这类时钟需要实现.round_rate、.set_rate、.recalc_rate。
设备树
#define PLL0_CLK 0
#defeine PLL0_FACTOR_PERI 0clocks{osc24M:osc24M{//晶振时钟compatible fixed-clock;#clock-cells 0;clock-output-names osc24M;clock-frequency 24000000;};pll0:pll0{//pll倍频时钟compatible xx, xx-fixed-clk;#clock-cells 0;clock-id PLL0_CLK;clock-frequency 1000000000;clock-output-names pll0;clocks osc24M;//pll的父时钟为24M晶振};factor_pll0_clk:factor_pll0_clk{//pll分频时钟compatible xx,xx-pll0-factor-clk;#clock-cells 1;clock-id PLL0_FACTOR_PERI;clock-output-names pll0_peri;clocks pll0 PLL0_CLK;//PERI子系统的父时钟为pll0};
};驱动
static long xx_factor_pll0_clk_round_rate(struct clk_hw *hw, unsigned long rate,unsigned long *parent_rate)
{unsigned long round_rate;//返回时钟实际支持的最接近速率return round_rate;
}
static int xx_factor_pll0_clk_set_rate(struct clk_hw *hw, unsigned long rate, unsigned long parent_rate)
{int ret 0;//操作寄存器设置频率return ret;
}static unsigned long xx_factor_pll0_clk_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
{unsigned long recalc_rate;//查询寄存器获得分频系数计算频率然后返回return recalc_rate;}const struct clk_ops xx_factor_clk_ops {.round_rate xx_factor_pll0_clk_round_rate,//给定目标速率作为输入返回时钟.set_rate xx_factor_pll0_clk_set_rate,.recalc_rate xx_factor_pll0_clk_recalc_rate,
}static void __init of_xx_factor_clk_init(struct device_node *np)
{//驱动入口//参考上述pll的注册唯一不同的就是struct clk_ops的成员函数实现
}CLK_OF_DECLARE(xx_factor_clk, xx,xx-factor-clk, of_xx_facotr_clk_init);gate_clk门控时钟实现
门控就是开关对于门控而言我们只需要实现struct clk_ops的.enable和.disable 设备树
#define PLL0_CLK 0
#defeine PLL0_FACTOR_PERI 0
#define PERI_MCI0 0mmc0:mmc00x12345678{compatible xx,xx-mmc0;......clocks peri PERI_MCI0;clocks-names mmc0;......};clocks{osc24M:osc24M{compatible fixed-clock;#clock-cells 0;clock-output-names osc24M;clock-frequency 24000000;};pll0:pll0{compatible xx, xx-fixed-clk;#clock-cells 0;clock-id PLL0_CLK;clock-frequency 1000000000;clock-output-names pll0;clocks osc24M;};factor_pll0_clk:factor_pll0_clk{compatible xx,xx-pll0-factor-clk;#clock-cells 1;clock-id PLL0_FACTOR_PERI;clock-output-names pll0_peri;clocks pll0 PLL0_CLK;};peri:peri{compatible xx,xx-gate-clk;#clock-cells 1;/*peri gate*/clock-id PERI_MCI0;clock-output-names mci0_peri;clocks factor_pll0_clk PLL0_FACTOR_PERI;};
};
驱动
static int xx_gate_clk_enable(struct clk_hw *hw)
{//寄存器操作打开门控return 0;
}static int xx_gate_clk_disable(struct clk_hw *hw)
{//寄存器操作门控关return 0;
}const struct clk_ops ak_gate_clk_ops {.enable xx_gate_clk_enable,.disable xx_gate_clk_disable,
}static void __init of_xx_gate_clk_init(struct device_node *np)
{//参考上述fixed_clk的注册几乎相同只不过操作函数clk_ops的实现不一样
}CLK_OF_DECLARE(xx_gate_clk, xx,xx-gate-clk, of_xx_gate_clk_init);上述只是对clock驱动实现的简单举例每个芯片厂商在clock驱动的实现上都有很大的差异。对于一般的驱动只需要会简单的使用内核提供的时钟API接口即可。