## 参考 - linux_5.10/Documentation/spi/spi-summary.rst ## 概念 ### 工作原理 SPI 请求将提交至IO对列。这些请求按照“先进先出(FIFO)”原则执行,在完成后将通过回调方式告知。 当然,也有一些简单的异步封装回调方法,包含发送命令并接收应答的一类接口。 ### 两种SPI驱动 #### Controller drivers ... 此类驱动的对象为SOC片上的SPI控制器,这些控制器通常支持“Master”与“Slave”角色切换。 此类驱动常直接访问硬件寄存器并使用DMA来提高数据传递效率,或许也可以通过CPU控制数据的每一位来控制可编程GPIO管脚。 #### Protocol drivers ... 此类驱动的对象为SOC片外接的SPI设备,这些设备以“Master”或“Slave”角色接入SPI控制器,进而完成数据的传输。 在SPI驱动之上,可能被具象化为MTD、Audio等设备接入更上一级的子系统。 ### 数据结构 `struct spi_device` 结构体是两种驱动的基类。 ```c #---- linux_5.10/include/linux/spi/spi.h struct spi_device { struct device dev; struct spi_controller *controller; struct spi_controller *master; /* compatibility layer */ ... ``` ### 框架 input layer, ALSA, networking, MTD, the character device framework, or other Linux subsystems. ### Messages If you like, spi_message_alloc() and spi_message_free() convenience ### 内存管理 SPI设备必须管理以下两种类型内存: - I/O 数据; - spi_message\spi_transfer数据; 可以使用spi_message_alloc()、spi_message_free()代为管理。 ## 示例 ### 设备 #### 板级静态定义 ##### Master - `struct platform_device`, 基于平台设备开发; 路径:`arch/.../mach-*/board-*.c`: ```c #include /* for mysoc_spi_data */ /* if your mach-* infrastructure doesn't support kernels that can * run on multiple boards, pdata wouldn't benefit from "__init". */ static struct mysoc_spi_data pdata __initdata = { ... }; static __init board_init(void) { ... /* this board only uses SPI controller #2 */ mysoc_register_spi(2, &pdata); ... } ``` ```c #include static struct platform_device spi2 = { ... }; void mysoc_register_spi(unsigned n, struct mysoc_spi_data *pdata) { struct mysoc_spi_data *pdata2; pdata2 = kmalloc(sizeof *pdata2, GFP_KERNEL); *pdata2 = pdata; ... if (n == 2) { spi2->dev.platform_data = pdata2; register_platform_device(&spi2); /* also: set up pin modes so the spi2 signals are * visible on the relevant pins ... bootloaders on * production boards may already have done this, but * developer boards will often need Linux to do it. */ } ... } ``` ##### Slave 文件:`arch/.../mach-*/board-*.c`: ```c static struct ads7846_platform_data ads_info = { .vref_delay_usecs = 100, .x_plate_ohms = 580, .y_plate_ohms = 410, }; static struct spi_board_info spi_board_info[] __initdata = { { .modalias = "ads7846", .platform_data = &ads_info, .mode = SPI_MODE_0, .irq = GPIO_IRQ(31), .max_speed_hz = 120000 /* max sample rate at 3V */ * 16, .bus_num = 1, .chip_select = 0, }, }; ``` ```c spi_register_board_info(spi_board_info, ARRAY_SIZE(spi_board_info)); ``` #### DTS动态定义 ##### Master ```dts #---- build/boards/default/dts/cv181x/cv181x_base.dtsi / { spi3:spi3@041B0000 { interrupts = <57 IRQ_TYPE_LEVEL_HIGH>; interrupt-parent = <&plic0>; }; ... ``` ##### Slave - 独占总线 ```dts #---- build/boards/default/dts/cv181x/cv181x_asic_bga.dtsi &spi3 { status = "okay"; num-cs = <1>; spidev@0 { compatible = "rohm,dh2228fv"; spi-max-frequency = <1000000>; reg = <0>; }; }; #---- build/boards/cv181x/cv1813h_milkv_duos_sd/dts_riscv/cv1813h_milkv_duos_sd.dts &spi3 { status = "okay"; spidev@0 { status = "okay"; }; }; ``` - 多设备 参考内核文档: 1. linux_5.10/Documentation/devicetree/bindings/spi/spi-bus.txt; 2. linux_5.10/arch/arm/boot/dts/imx6ul-imx6ull-opos6uldev.dtsi ```dts #---- linux_5.10/arch/arm/boot/dts/imx6ull-opos6uldev.dts /dts-v1/; #include "imx6ull-opos6ul.dtsi" # 1 # include "imx6ul.dtsi" # 1.1 #include "imx6ul-imx6ull-opos6uldev.dtsi" # 2 / { model = "Armadeus Systems OPOS6UL SoM (i.MX6ULL) on OPOS6ULDev board"; compatible = "armadeus,imx6ull-opos6uldev", "armadeus,imx6ull-opos6ul", "fsl,imx6ull"; }; #1.1- linux_5.10/arch/arm/boot/dts/imx6ul.dtsi / { ... soc { ... aips1: bus@2000000 { ... spba-bus@2000000 { ... ecspi4: spi@2014000 { #address-cells = <1>; #size-cells = <0>; compatible = "fsl,imx6ul-ecspi", "fsl,imx51-ecspi"; reg = <0x02014000 0x4000>; interrupts = ; clocks = <&clks IMX6UL_CLK_ECSPI4>, <&clks IMX6UL_CLK_ECSPI4>; clock-names = "ipg", "per"; dmas = <&sdma 9 7 1>, <&sdma 10 7 2>; dma-names = "rx", "tx"; status = "disabled"; }; #2--- linux_5.10/arch/arm/boot/dts/imx6ul-imx6ull-opos6uldev.dtsi &ecspi4 { pinctrl-names = "default"; pinctrl-0 = <&pinctrl_ecspi4>; num-cs = 2; cs-gpios = <&gpio4 9 GPIO_ACTIVE_LOW>, <&gpio4 3 GPIO_ACTIVE_LOW>; status = "okay"; spidev0: spi@0 { compatible = "spidev"; reg = <0>; spi-max-frequency = <5000000>; }; spidev1: spi@1 { compatible = "spidev"; reg = <1>; spi-max-frequency = <5000000>; }; }; ``` #### 总结 1. `spi-max-frequency`,多通道时,需要定义设备允许的最大速率;---- 待确认; ### 驱动 - `spi_busnum_to_master()`, 查找目标SPI控制器; - `spi_new_device()`, 创建; - `spi_unregister_device()`销毁; #### SPI Controller Driver ```c struct spi_master *master; struct CONTROLLER *c; master = spi_alloc_master(dev, sizeof *c); if (!master) return -ENODEV; c = spi_master_get_devdata(master); ``` - `spi_[un]register_master()`注册至系统; master操作集 - `master->setup(struct spi_device *spi)`, clock rate, SPI mode, and word sizes. - `master->cleanup(struct spi_device *spi)`, 清除之前由`spi_device.controller_state`锁定的状态; ... - `master->transfer_one_message(struct spi_master *master, struct spi_message *mesg)`, 发送一个mesg; #### SPI Protocol Driver ```c static int CHIP_probe(struct spi_device *spi) { struct CHIP *chip; struct CHIP_platform_data *pdata; /* assuming the driver requires board-specific data: */ pdata = &spi->dev.platform_data; if (!pdata) return -ENODEV; /* get memory for driver's per-chip state */ chip = kzalloc(sizeof *chip, GFP_KERNEL); if (!chip) return -ENOMEM; spi_set_drvdata(spi, chip); ... etc return 0; } static struct spi_driver CHIP_driver = { .driver = { .name = "CHIP", .owner = THIS_MODULE, .pm = &CHIP_pm_ops, }, .probe = CHIP_probe, .remove = CHIP_remove, }; ``` spi_setup(), ## 属性 - `spi_message.is_dma_mapped`, ## 调试 #### sys节点 - `/sys/bus/spi/devices/spiB.C` 总线B,设备C, 都是从0开始编号; - `/sys/class/spi_master/spiB` spi_master类,总线B; - `/sys/class/spi_slave/spiB` ## 备注 - `master->transfer(struct spi_device *spi, struct spi_message *message)`, 1. 已经被弃用; 2. 可以打断IO对列,提前发送;