总体框图
pinctrl子系统预先确定引脚的数量和名字
- 为每个引脚的配置信息分配内存(一旦pinctrl子系统驱动程序从设备树里iomux节点解析出芯片引脚的属性信息后就会存放到这里来)
- 管理每个引脚的使用状态
imx6ul_pinctrl_probe()函数
pinctrl子系统驱动入口
drivers/pinctrl/freescale/pinctrl-imx6ul.c
static int imx6ul_pinctrl_probe(struct platform_device *pdev)
{
const struct imx_pinctrl_soc_info *pinctrl_info;
const struct of_device_id *match;
pinctrl_info = of_device_get_match_data(&pdev->dev);
if (!pinctrl_info)
return -ENODEV;
match = of_match_device(imx6ul_pinctrl_of_match, &pdev->dev);
if (!match)
return -ENODEV;
pinctrl_info = (struct imx_pinctrl_soc_info *) match->data;
return imx_pinctrl_probe(pdev, pinctrl_info);
}
struct pinctrl_pin_desc {
unsigned number;
const char *name;
void *drv_data;
};
struct imx_pinctrl_soc_info {
const struct pinctrl_pin_desc *pins;
unsigned int npins;
unsigned int flags;
const char *gpr_compatible;
...
}
165~295 imx6ul_pinctrl_pads
of_device_get_match_data()函数
drivers/of/device.c
const void *of_device_get_match_data(const struct device *dev)
{
const struct of_device_id *match;
match = of_match_device(dev->driver->of_match_table, dev);
if (!match)
return NULL;
return match->data;
}
of_match_device()函数
const struct of_device_id *of_match_device(const struct of_device_id *matches,
const struct device *dev)
{
if ((!matches) || (!dev->of_node))
return NULL;
return of_match_node(matches, dev->of_node);
}
imx6ul_pinctrl_pads
- 引脚的编号和名字表
IMX_PINCTRL_PIN宏
drivers/pinctrl/freescale/pinctrl-imx.h
#define IMX_PINCTRL_PIN(pin) PINCTRL_PIN(pin, #pin)
PINCTRL_PIN宏
include/linux/pinctrl/pinctrl.h
#define PINCTRL_PIN(a, b) { .number = a, .name = b }
引脚编号与复用寄存器的关系
pin.num = mux/4
imx_pinctrl:存储引脚的名字和编号原始表
imx_pinctrl_probe()函数
int imx_pinctrl_probe(struct platform_device *pdev,
const struct imx_pinctrl_soc_info *info)
{
struct imx_pinctrl *ipctl;
struct pinctrl_desc *imx_pinctrl_desc;
...
ipctl = devm_kzalloc(&pdev->dev, sizeof(*ipctl), GFP_KERNEL);
...
//获取平台设备resources的平台资源 reg
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
//对资源里的寄存器组地址进行映射
//把寄存器组的起始地址进行虚拟地址的映射 并把映射得到的虚拟地址记录在>base里
ipctl->base = devm_ioremap_resource(&pdev->dev, res);
...
imx_pinctrl_desc = devm_kzalloc(&pdev->dev, sizeof(*imx_pinctrl_desc),GFP_KERNEL);
...
imx_pinctrl_desc->name = dev_name(&pdev->dev);
imx_pinctrl_desc->pins = info->pins;
imx_pinctrl_desc->npins = info->npins;
imx_pinctrl_desc->pctlops = &imx_pctrl_ops;
imx_pinctrl_desc->pmxops = &imx_pmx_ops;
imx_pinctrl_desc->confops = &imx_pinconf_ops;
imx_pinctrl_desc->owner = THIS_MODULE;
...
ipctl->info = info;
ipctl->dev = &pdev->dev;
//把imx_pinctrl上的资源数据保存到platform_device平台设备上来
platform_set_drvdata(pdev, ipctl);
...
//
ret = devm_pinctrl_register_and_init(&pdev->dev,
imx_pinctrl_desc, ipctl,
&ipctl->pctl);
...
ret = imx_pinctrl_probe_dt(pdev, ipctl);
...
return pinctrl_enable(ipctl->pctl);
...
return ret;
}
pinctrl_desc:以基数树的方式存储引脚的名字,记录引脚的使用状态
devm_pinctrl_register_and_init()函数
drivers/pinctrl/core.c
int devm_pinctrl_register_and_init(struct device *dev,
struct pinctrl_desc *pctldesc,
void *driver_data,
struct pinctrl_dev **pctldev)
{
struct pinctrl_dev **ptr;
int error;
//为 pinctrl_dev申请内存
ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
if (!ptr)
return -ENOMEM;
//返回值是pinctrl_dev类型的
error = pinctrl_register_and_init(pctldesc, dev, driver_data, pctldev);
if (error) {
devres_free(ptr);
return error;
}
*ptr = *pctldev;
devres_add(dev, ptr);
return 0;
}
pinctrl_register_and_init
int pinctrl_register_and_init(struct pinctrl_desc *pctldesc,
struct device *dev, void *driver_data,
struct pinctrl_dev **pctldev)
{
struct pinctrl_dev *p;
//返回值是pinctrl_dev类型的
p = pinctrl_init_controller(pctldesc, dev, driver_data);
if (IS_ERR(p))
return PTR_ERR(p);
//最后返回来的p指针的值会赋值给*pctldev &ipctl->pctl(ipctl里的pctl指针)
*pctldev = p;
return 0;
}
pinctrl_init_controller()函数
drivers/pinctrl/core.c
参数:pinctrl_init_controller(imx_pinctrl_desc , iomux对应的device ,imx_pinctrl结构体)
pinctrl_register_pins(pinctrl_dev,imx6ul_pinctrl_pads,芯片引脚数量)
static struct pinctrl_dev *
pinctrl_init_controller(struct pinctrl_desc *pctldesc, struct device *dev,
void *driver_data)
{
struct pinctrl_dev *pctldev;
...
pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL);
...
pctldev->owner = pctldesc->owner;
pctldev->desc = pctldesc;
pctldev->driver_data = driver_data;
/*初始化基数树 把pin_desc_tree作为基数树的根节点*/
INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL);
...
pctldev->dev = dev;
...
//pctldesc->pins相当于引脚信息-- 为每一个芯片引脚都生成了一个pin_desc结构体
ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins);
...
return pctldev;
...
}
pinctrl_register_pins()函数
drivers/pinctrl/core.c
参数:num_descs–芯片引脚数量 , imx6ul_pinctrl_pads数组-记录了每个引脚的命名和编号
static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
const struct pinctrl_pin_desc *pins,
unsigned num_descs)
{
unsigned i;
int ret = 0;
//num_descs--芯片引脚数量 为每一个芯片引脚都生成了一个pin_desc结构体
for (i = 0; i < num_descs; i++) {
ret = pinctrl_register_one_pin(pctldev, &pins[i]); //把芯片的引脚和命名传进去了
if (ret)
return ret;
}
return 0;
}
pinctrl_register_one_pin()函数
drivers/pinctrl/core.c
static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev,
const struct pinctrl_pin_desc *pin)
{
struct pin_desc *pindesc;
//去基数树上去查找内容
pindesc = pin_desc_get(pctldev, pin->number);
if (pindesc) {
dev_err(pctldev->dev, "pin %d already registered\n",
pin->number);
return -EINVAL;
}
//创建一个个pin_desc结构体
pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL);
...
//pin_desc结构体里的pctldev指针会存储到 pinctrl_dev 所挂载的pin_desc_tree基数树上去
pindesc->pctldev = pctldev;
...
if (pin->name) {
pindesc->name = pin->name;
} else {
pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", pin->number);
if (!pindesc->name) {
kfree(pindesc);
return -ENOMEM;
}
pindesc->dynamic_name = true;
}
...
//
radix_tree_insert(&pctldev->pin_desc_tree, pin->number, pindesc);
...
return 0;
}
static inline struct pin_desc *pin_desc_get(struct pinctrl_dev *pctldev,
unsigned int pin)
{
return radix_tree_lookup(&pctldev->pin_desc_tree, pin);
}