设备树中的spi设备以及内核对spi节点的处理流程(转)
dts文件中的spi节点
&ecspi2{ /* spi控制器节点 */
fsl,spi-num-chipselects= < 1 >;
cs-gpios = <&gpio5 13 0 > ;/* 片选的io口 */
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_ecspi2 >;
status = "okay"; /* status属性值为"okay" 表示该节点使能*/
spidev@0x00{
compatible = "spidev","rohm,dh2228fv";/* 此属性值用于与spi设备驱动匹配 */
reg = <0>; /*spi设备是没有设备地址的, 这里是指使用spi控制器的cs-gpios里的第几个片选io */
spi-max-frequency = <10000000>; /* 指定spi设备的最大工作时钟 */
/*以下为自定义属性 用于指定工作时序方式及其它功能设置等*/
...
buswidth = <8>; /* 传输以8位为单位 */
mode = <0>; /* 使用第几种工作时序(CPOL, CPHA) */
/*但在现用的内核源码里发现, spi设备的工作时序并不是用mode属性值来指定的*/
/* 如CPOL需要设1, 则只需在spi设备节点里加上"spi-cpol"属性即可; CPOL设0,则不写"spi-cpol"属性即可 */
/* CPHA设1时, 则在设备节点里加上"spi-cpha"属性即可 */
};
};
dtsi文件中的设备树节点
ecspi2: ecspi@30830000 {
compatible = "fsl,imx6ul-ecspi", "fsl,imx51-ecspi";
reg = <0x0 0x30830000 0x0 0x10000>;
interrupts = <GIC_SPI 32 IRQ_TYPE_LEVEL_HIGH>;
clocks = <&clk IMX8MQ_CLK_ECSPI2_ROOT>,
<&clk IMX8MQ_CLK_ECSPI2_ROOT>;
clock-names = "ipg", "per";
interrupt-parent = <&gpc>;
status = "disabled";
};
一般带spi名称的节点表示spi控制器, 它会先被转换为platform_device, 在内核中有对应的platform_driver;(根据compatible属性来匹配)一般为厂商所配套的platform_driver文件(freescale的处理文件为spi-imx.c),platform_driver的probe函数会调用spi_register_master(), 下面是spi节点在内核中的转化过程:
spi_imx_probe //drivers/spi/spi-imx.c
spi_bitbang_start
spi_register_master
of_register_spi_devices
for_each_available_child_of_node(ctlr->dev.of_node, nc) {
spi = of_register_spi_device(ctlr, nc); // 读取设备树中的spi子节点的属性
spi = spi_alloc_device(ctlr);
rc = spi_add_device(spi); //添加spi设备
}
下面来重点看一下of_register_spi_device()函数,该函数的主要作用是读取spi节点内的各种值
static struct spi_device *
of_register_spi_device(struct spi_master *master, struct device_node *nc)
{
struct spi_device *spi;
int rc;
u32 value;
/* Alloc an spi_device 分配一个spi设备*/
spi = spi_alloc_device(master);
if (!spi) {
dev_err(&master->dev, "spi_device alloc error for %s\n",
nc->full_name);
rc = -ENOMEM;
goto err_out;
}
/* Select device driver 获取 compatibel 属性 用于匹配spi driver*/
rc = of_modalias_node(nc, spi->modalias,
sizeof(spi->modalias));
if (rc < 0) {
dev_err(&master->dev, "cannot find modalias for %s\n",
nc->full_name);
goto err_out;
}
/* Device address 获取 reg 属性作为片选编号*/
rc = of_property_read_u32(nc, "reg", &value);
if (rc) {
dev_err(&master->dev, "%s has no valid 'reg' property (%d)\n",
nc->full_name, rc);
goto err_out;
}
spi->chip_select = value;
/* Mode (clock phase/polarity/etc.) spi mode选择*/
if (of_find_property(nc, "spi-cpha", NULL))
spi->mode |= SPI_CPHA;
if (of_find_property(nc, "spi-cpol", NULL))
spi->mode |= SPI_CPOL;
if (of_find_property(nc, "spi-cs-high", NULL))
spi->mode |= SPI_CS_HIGH;
if (of_find_property(nc, "spi-3wire", NULL))
spi->mode |= SPI_3WIRE;
if (of_find_property(nc, "spi-lsb-first", NULL))
spi->mode |= SPI_LSB_FIRST;
/* Device DUAL/QUAD mode */
if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
switch (value) {
case 1:
break;
case 2:
spi->mode |= SPI_TX_DUAL;
break;
case 4:
spi->mode |= SPI_TX_QUAD;
break;
default:
dev_warn(&master->dev,
"spi-tx-bus-width %d not supported\n",
value);
break;
}
}
if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
switch (value) {
case 1:
break;
case 2:
spi->mode |= SPI_RX_DUAL;
break;
case 4:
spi->mode |= SPI_RX_QUAD;
break;
default:
dev_warn(&master->dev,
"spi-rx-bus-width %d not supported\n",
value);
break;
}
}
/* Device speed spi速度设置*/
rc = of_property_read_u32(nc, "spi-max-frequency", &value);
if (rc) {
dev_err(&master->dev, "%s has no valid 'spi-max-frequency' property (%d)\n",
nc->full_name, rc);
goto err_out;
}
spi->max_speed_hz = value;
/* Store a pointer to the node in the device structure */
of_node_get(nc); //保存设备树节点
spi->dev.of_node = nc;
/* Register the new device 注册新的spi设备*/
rc = spi_add_device(spi);
if (rc) {
dev_err(&master->dev, "spi_device register error %s\n",
nc->full_name);
goto err_of_node_put;
}
return spi;
err_of_node_put:
of_node_put(nc);
err_out:
spi_dev_put(spi);
return ERR_PTR(rc);
}
生成了spi设备之后,会使用spi_match_device()匹配对应的spi driver。匹配成功之后生成对应的spi设备节点。
补充几点最近的发现:(针对于freescale im8mq 内核版本:linux-4.9.88)
1、spi节点下不能定义两个使用同一个片选信号的节点,如果有相同的节点会出现其中一个结点无效的情况。(未具体分析源码)
2、在spi节点下可声明除spi相关节点之外的节点,gpio的初始化需要放入spi根节点的pinctrl中。并且需要注意使用的io管脚不能在其他pinctrl节点中出现,不然会造成节点初始化失败的情况。
pinctrl_ecspi2: ecspi2grp {
fsl,pins = <
MX8MQ_IOMUXC_ECSPI2_SS0_GPIO5_IO13 0x16
MX8MQ_IOMUXC_ECSPI2_MOSI_ECSPI2_MOSI 0x16
MX8MQ_IOMUXC_ECSPI2_MISO_ECSPI2_MISO 0x16
MX8MQ_IOMUXC_ECSPI2_SCLK_ECSPI2_SCLK 0x1816
MX8MQ_IOMUXC_NAND_RE_B_GPIO3_IO15 0x16
>;
};
pinctrl_uart3: uart3grp {
fsl,pins = <
MX8MQ_IOMUXC_UART3_TXD_UART3_DCE_TX 0x49 /* UART3_TXD */
MX8MQ_IOMUXC_UART3_RXD_UART3_DCE_RX 0x49 /* UART3_RXD */
MX8MQ_IOMUXC_ECSPI1_MISO_UART3_DCE_CTS_B 0x49 /* ECSPI1_MISO UART3_CTS */
MX8MQ_IOMUXC_ECSPI1_SS0_UART3_DCE_RTS_B 0x49 /* ECSPI1_SS0 UART3_RTS */
//MX8MQ_IOMUXC_NAND_RE_B_GPIO3_IO15 0x56 /*需要屏蔽此处相同的管脚*/
MX8MQ_IOMUXC_GPIO1_IO00_ANAMIX_REF_CLK_32K 0x14 /* REF_CLK_32K */
>;
};
3、在spi设备匹配时会自动处理节点中的中断号,不需要自己处理了(下列函数位于driver/spi/spi.c)
static int spi_drv_probe(struct device *dev)
{
const struct spi_driver *sdrv = to_spi_driver(dev->driver);
struct spi_device *spi = to_spi_device(dev);
int ret;
ret = of_clk_set_defaults(dev->of_node, false);
if (ret)
return ret;
if (dev->of_node) {
spi->irq = of_irq_get(dev->of_node, 0); //获取中断号
if (spi->irq == -EPROBE_DEFER)
return -EPROBE_DEFER;
if (spi->irq < 0)
spi->irq = 0;
}
ret = dev_pm_domain_attach(dev, true);
if (ret != -EPROBE_DEFER) {
ret = sdrv->probe(spi);
if (ret)
dev_pm_domain_detach(dev, true);
}
return ret;
}
————————————————
版权声明:本文为CSDN博主「弋阳yoga」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_17270067/article/details/106993990