设备控制器的pin管脚何时进行绑定?
本文需要读者对linux设备驱动模型以及pinctrl子系统有一定的了解,同时对设备树中配置的特定控制器功能时的pin管脚状态配置了解。在设备和驱动匹配成功后的probe()函数中没有对于该控制器(uart,spi,mac等)的pin管脚绑定操作,那么绑定操作在什么地方进行的呢?
1、设备控制器pin管脚在何时被绑定?
如下所示为imx8 lpuart1的设备树节点配置信息,
lpuart1: serial@5a070000 {
compatible = "fsl,imx8qm-lpuart";
reg = <0x0 0x5a070000 0x0 0x1000>;
interrupts = <GIC_SPI 346 IRQ_TYPE_LEVEL_HIGH>;
interrupt-parent = <&wu>;
clocks = <&clk IMX8QXP_UART1_CLK>,<&clk IMX8QXP_UART1_IPG_CLK>;
clock-names = "per", "ipg";
assigned-clocks = <&clk IMX8QXP_UART1_CLK>;
assigned-clock-rates = <80000000>;
power-domains = <&pd_dma_lpuart1>;
dma-names = "tx","rx";
dmas = <&edma2 11 0 0>,<&edma2 10 0 1>;
status = "disabled";
};
&lpuart1 {
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_lpuart1>;
status = "okay";
};
&iomuxc {
pinctrl-names = "default"; //"default"状态
pinctrl-0 = <&pinctrl_hog>;//"default"状态对应的pinctrl配置
imx8qxp-mek {
...
pinctrl_lpuart1: lpuart1grp {
fsl,pins = <
SC_P_UART1_TX_ADMA_UART1_TX 0x06000020
SC_P_UART1_RX_ADMA_UART1_RX 0x06000020
>;
};
...
};
};
那么lpuart1使用的pin管脚的绑定设置在何时被调用呢?
答案就是,在linux设备驱动模型中设备和驱动匹配成功后调用really_probe(...)中进行控制器pin管脚的设置,具体如下:
源码:/drivers/base.dd.c
static int really_probe(struct device *dev, struct device_driver *drv)
{
...
re_probe:
dev->driver = drv; /*绑定设备和设备驱动*/
/* If using pinctrl, bind pins now before probing */
ret = pinctrl_bind_pins**(dev); /*pinctrl,绑定控制器的pinn管脚*/
if (ret)
goto pinctrl_bind_failed;
ret = dma_configure(dev); /*dma_configure,DMA配置信息*/
if (ret)
goto dma_failed;
if (driver_sysfs_add(dev)) {
printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n",__func__, dev_name(dev));
goto probe_failed;
}
if (dev->pm_domain && dev->pm_domain->activate) {
ret = dev->pm_domain->activate(dev);
if (ret)
goto probe_failed;
}
if (dev->bus->probe) { /*总线probe函数不存在才会调用drv->probe函数*/
ret = dev->bus->probe(dev);
if (ret)
goto probe_failed;
} else if (drv->probe) {
ret = drv->probe(dev); /*调用大名鼎鼎的设备驱动probe函数*/
if (ret)
goto probe_failed;
}
pinctrl_init_done**(dev); //pinctrl 初始化完成
...
}
下面看下pinctrl_bind_pins(...)函数的具体实现
源码:/drivers.base.pinctrl.c
/**
* struct dev_pin_info - pin state container for devices
* @p: pinctrl handle for the containing device
* @default_state: the default state for the handle, if found
* @init_state: the state at probe time, if found
* @sleep_state: the state at suspend time, if found
* @idle_state: the state at idle (runtime suspend) time, if found
*/
struct dev_pin_info {
struct pinctrl *p;
struct pinctrl_state *default_state;
struct pinctrl_state *init_state;
#ifdef CONFIG_PM
struct pinctrl_state *sleep_state;
struct pinctrl_state *idle_state;
#endif
};
/**
* pinctrl_bind_pins() - called by the device core before probe
* @dev: the device that is just about to probe
*/
int pinctrl_bind_pins(struct device *dev)
{
int ret;
if (dev->of_node_reused)
return 0;
//分配dev_pin_info 内存空间
dev->pins = devm_kzalloc(dev, sizeof(*(dev->pins)), GFP_KERNEL);
if (!dev->pins)
return -ENOMEM;
dev->pins->p = devm_pinctrl_get(dev);
if (IS_ERR(dev->pins->p)) {
dev_dbg(dev, "no pinctrl handle\n");
ret = PTR_ERR(dev->pins->p);
goto cleanup_alloc;
}
//查找"default"状态
dev->pins->default_state = pinctrl_lookup_state(dev->pins->p,PINCTRL_STATE_DEFAULT);
if (IS_ERR(dev->pins->default_state)) {
dev_dbg(dev, "no default pinctrl state\n");
ret = 0;
goto cleanup_get; //必须存在"default"状态
}
//查找"init"状态
dev->pins->init_state = pinctrl_lookup_state(dev->pins->p,PINCTRL_STATE_INIT);
if (IS_ERR(dev->pins->init_state)) {
/* Not supplying this state is perfectly legal */
dev_dbg(dev, "no init pinctrl state\n");
ret = pinctrl_select_state(dev->pins->p, dev->pins->default_state); //无"init"状态则设置为"default"状态
} else {
ret = pinctrl_select_state(dev->pins->p, dev->pins->init_state); //设置"init"状态
}
if (ret) {
dev_dbg(dev, "failed to activate initial pinctrl state\n");
goto cleanup_get;
}
#ifdef CONFIG_PM
/*
* If power management is enabled, we also look for the optional
* sleep and idle pin states, with semantics as defined in
* <linux/pinctrl/pinctrl-state.h>
*/
//查找"sleep"状态
dev->pins->sleep_state = pinctrl_lookup_state(dev->pins->p,PINCTRL_STATE_SLEEP);
if (IS_ERR(dev->pins->sleep_state))
/* Not supplying this state is perfectly legal */
dev_dbg(dev, "no sleep pinctrl state\n");
//查找"idle"状态
dev->pins->idle_state = pinctrl_lookup_state(dev->pins->p,PINCTRL_STATE_IDLE);
if (IS_ERR(dev->pins->idle_state))
/* Not supplying this state is perfectly legal */
dev_dbg(dev, "no idle pinctrl state\n");
#endif
return 0;
/*
* If no pinctrl handle or default state was found for this device,
* let's explicitly free the pin container in the device, there is
* no point in keeping it around.
*/
cleanup_get:
devm_pinctrl_put(dev->pins->p);
cleanup_alloc:
devm_kfree(dev, dev->pins);
dev->pins = NULL;
/* Return deferrals */
if (ret == -EPROBE_DEFER)
return ret;
/* Return serious errors */
if (ret == -EINVAL)
return ret;
/* We ignore errors like -ENOENT meaning no pinctrl state */
return 0;
}
/**
* pinctrl_init_done() - tell pinctrl probe is done
* We'll use this time to switch the pins from "init" to "default" unless the driver selected some other state.
* @dev: device to that's done probing
*/
int pinctrl_init_done(struct device *dev)
{
struct dev_pin_info *pins = dev->pins;
int ret;
if (!pins)
return 0;
if (IS_ERR(pins->init_state))
return 0; /* No such state */
if (pins->p->state != pins->init_state)
return 0; /* Not at init anyway */
if (IS_ERR(pins->default_state))
return 0; /* No default state */
ret = pinctrl_select_state(pins->p, pins->default_state); //切换为"default"状态
if (ret)
dev_err(dev, "failed to activate default pinctrl state\n");
return ret;
}