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"表示spidev0設備使能, "disabled"表示設備沒有使用*/
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函數會調用xx_spi_probe, 下面是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設備節點。