Linux下spi驅動開發(1) linux
做者:劉洪濤,華清遠見嵌入式學院講師。 數組
1、概述 框架
基於子系統去開發驅動程序已是linux內核中廣泛的作法了。前面寫過基於I2C子系統的驅動開發。本文介紹另一種經常使用總線SPI的開發方法。SPI子系統的開發和I2C有不少的類似性,你們能夠對比學習。本主題分爲兩個部分敘述,第一部分介紹基於SPI子系統開發的理論框架;第二部分以華清遠見教學平臺FS_S5PC100上的M25P10芯片爲例(內核版本2.6.29),編寫一個SPI驅動程序實例。 異步
2、SPI總線協議簡介 async
介紹驅動開發前,須要先熟悉下SPI通信協議中的幾個關鍵的地方,後面在編寫驅動時,須要考慮相關因素。 函數
SPI總線由MISO(串行數據輸入)、MOSI(串行數據輸出)、SCK(串行移位時鐘)、CS(使能信號)4個信號線組成。如FS_S5PC100上的M25P10芯片接線爲: 學習
上圖中M25P10的D腳爲它的數據輸入腳,Q爲數據輸出腳,C爲時鐘腳。 this
SPI經常使用四種數據傳輸模式,主要差異在於:輸出串行同步時鐘極性(CPOL)和相位(CPHA)能夠進行配置。若是CPOL= 0,串行同步時鐘的空閒狀態爲低電平;若是CPOL= 1,串行同步時鐘的空閒狀態爲高電平。若是CPHA= 0,在串行同步時鐘的前沿(上升或降低)數據被採樣;若是CPHA = 1,在串行同步時鐘的後沿(上升或降低)數據被採樣。 spa
這四種模式中究竟選擇哪一種模式取決於設備。如M25P10的手冊中明確它能夠支持的兩種模式爲:CPOL=0 CPHA=0 和 CPOL=1 CPHA=1 指針
3、linux下SPI驅動開發
首先明確SPI驅動層次,以下圖:
咱們以上面的這個圖爲思路
一、 Platform bus
Platform bus對應的結構是platform_bus_type,這個內核開始就定義好的。咱們不須要定義。
二、Platform_device
SPI控制器對應platform_device的定義方式,一樣以S5PC100中的SPI控制器爲例,參看arch/arm/plat-s5pc1xx/dev-spi.c文件
struct platform_device s3c_device_spi0 = {
.name = "s3c64xx-spi", //名稱,要和Platform_driver匹配
.id = 0, //第0個控制器,S5PC100中有3個控制器
.num_resources = ARRAY_SIZE(s5pc1xx_spi0_resource), //佔用資源的種類
.resource = s5pc1xx_spi0_resource, //指向資源結構數組的指針
.dev = {
.dma_mask = &spi_dmamask, //dma尋址範圍
.coherent_dma_mask = DMA_BIT_MASK(32), //能夠經過關閉cache等措施保證一致性的dma尋址範圍
.platform_data = &s5pc1xx_spi0_pdata, //特殊的平臺數據,參看後文
},
};
static struct s3c64xx_spi_cntrlr_info s5pc1xx_spi0_pdata = {
.cfg_gpio = s5pc1xx_spi_cfg_gpio, //用於控制器管腳的IO配置
.fifo_lvl_mask = 0x7f,
.rx_lvl_offset = 13,
};
static int s5pc1xx_spi_cfg_gpio(struct platform_device *pdev)
{
switch (pdev->id) {
case 0:
s3c_gpio_cfgpin(S5PC1XX_GPB(0), S5PC1XX_GPB0_SPI_MISO0);
s3c_gpio_cfgpin(S5PC1XX_GPB(1), S5PC1XX_GPB1_SPI_CLK0);
s3c_gpio_cfgpin(S5PC1XX_GPB(2), S5PC1XX_GPB2_SPI_MOSI0);
s3c_gpio_setpull(S5PC1XX_GPB(0), S3C_GPIO_PULL_UP);
s3c_gpio_setpull(S5PC1XX_GPB(1), S3C_GPIO_PULL_UP);
s3c_gpio_setpull(S5PC1XX_GPB(2), S3C_GPIO_PULL_UP);
break;
case 1:
s3c_gpio_cfgpin(S5PC1XX_GPB(4), S5PC1XX_GPB4_SPI_MISO1);
s3c_gpio_cfgpin(S5PC1XX_GPB(5), S5PC1XX_GPB5_SPI_CLK1);
s3c_gpio_cfgpin(S5PC1XX_GPB(6), S5PC1XX_GPB6_SPI_MOSI1);
s3c_gpio_setpull(S5PC1XX_GPB(4), S3C_GPIO_PULL_UP);
s3c_gpio_setpull(S5PC1XX_GPB(5), S3C_GPIO_PULL_UP);
s3c_gpio_setpull(S5PC1XX_GPB(6), S3C_GPIO_PULL_UP);
break;
case 2:
s3c_gpio_cfgpin(S5PC1XX_GPG3(0), S5PC1XX_GPG3_0_SPI_CLK2);
s3c_gpio_cfgpin(S5PC1XX_GPG3(2), S5PC1XX_GPG3_2_SPI_MISO2);
s3c_gpio_cfgpin(S5PC1XX_GPG3(3), S5PC1XX_GPG3_3_SPI_MOSI2);
s3c_gpio_setpull(S5PC1XX_GPG3(0), S3C_GPIO_PULL_UP);
s3c_gpio_setpull(S5PC1XX_GPG3(2), S3C_GPIO_PULL_UP);
s3c_gpio_setpull(S5PC1XX_GPG3(3), S3C_GPIO_PULL_UP);
break;
default:
dev_err(&pdev->dev, "Invalid SPI Controller number!");
return -EINVAL;
}
三、Platform_driver
再看platform_driver,參看drivers/spi/spi_s3c64xx.c文件
static struct platform_driver s3c64xx_spi_driver = {
.driver = {
.name = "s3c64xx-spi", //名稱,和platform_device對應
.owner = THIS_MODULE,
},
.remove = s3c64xx_spi_remove,
.suspend = s3c64xx_spi_suspend,
.resume = s3c64xx_spi_resume,
};
platform_driver_probe(&s3c64xx_spi_driver, s3c64xx_spi_probe);//註冊s3c64xx_spi_driver
和平臺中註冊的platform_device匹配後,調用s3c64xx_spi_probe。而後根據傳入的platform_device參數,構建一個用於描述SPI控制器的結構體spi_master,並註冊。spi_register_master(master)。後續註冊的spi_device須要選定本身的spi_master,並利用spi_master提供的傳輸功能傳輸spi數據。
和I2C相似,SPI也有一個描述控制器的對象叫spi_master。其主要成員是主機控制器的序號(系統中可能存在多個SPI主機控制器)、片選數量、SPI模式和時鐘設置用到的函數、數據傳輸用到的函數等。
struct spi_master {
struct device dev;
s16 bus_num; //表示是SPI主機控制器的編號。由平臺代碼決定
u16 num_chipselect; //控制器支持的片選數量,即能支持多少個spi設備
int (*setup)(struct spi_device *spi); //針對設備設置SPI的工做時鐘及數據傳輸模式等。在spi_add_device函數中調用。
int (*transfer)(struct spi_device *spi,
struct spi_message *mesg); //實現數據的雙向傳輸,可能會睡眠
void (*cleanup)(struct spi_device *spi); //註銷時調用
};
四、Spi bus
Spi總線對應的總線類型爲spi_bus_type,在內核的drivers/spi/spi.c中定義
struct bus_type spi_bus_type = {
.name = "spi",
.dev_attrs = spi_dev_attrs,
.match = spi_match_device,
.uevent = spi_uevent,
.suspend = spi_suspend,
.resume = spi_resume,
};
對應的匹配規則是(高版本中的匹配規則會稍有變化,引入了id_table,能夠匹配多個spi設備名稱):
static int spi_match_device(struct device *dev, struct device_driver *drv)
{
const struct spi_device *spi = to_spi_device(dev);
return strcmp(spi->modalias, drv->name) == 0;
}
五、spi_device
下面該講到spi_device的構建與註冊了。spi_device對應的含義是掛接在spi總線上的一個設備,因此描述它的時候應該明確它自身的設備特性、傳輸要求、及掛接在哪一個總線上。
static struct spi_board_info s3c_spi_devs[] __initdata = {
{
.modalias = "m25p10",
.mode = SPI_MODE_0, //CPOL=0, CPHA=0 此處選擇具體數據傳輸模式
.max_speed_hz = 10000000, //最大的spi時鐘頻率
/* Connected to SPI-0 as 1st Slave */
.bus_num = 0, //設備鏈接在spi控制器0上
.chip_select = 0, //片選線號,在S5PC100的控制器驅動中沒有使用它做爲片選的依據,而是選擇了下文controller_data裏的方法。
.controller_data = &smdk_spi0_csi[0],
},
};
static struct s3c64xx_spi_csinfo smdk_spi0_csi[] = {
[0] = {
.set_level = smdk_m25p10_cs_set_level,
.fb_delay = 0x3,
},
};
static void smdk_m25p10_cs_set_level(int high) //spi控制器會用這個方法設置cs
{
u32 val;
val = readl(S5PC1XX_GPBDAT);
if (high)
val |= (1<<3);
else
val &= ~(1<<3);
writel(val, S5PC1XX_GPBDAT);
}
spi_register_board_info(s3c_spi_devs, ARRAY_SIZE(s3c_spi_devs));//註冊spi_board_info。這個代碼會把spi_board_info註冊要鏈表board_list上。
事實上上文提到的spi_master的註冊會在spi_register_board_info以後,spi_master註冊的過程當中會調用scan_boardinfo掃描board_list,找到掛接在它上面的spi設備,而後建立並註冊spi_device。
static void scan_boardinfo(struct spi_master *master)
{
struct boardinfo *bi;
mutex_lock(&board_lock);
list_for_each_entry(bi, &board_list, list) {
struct spi_board_info *chip = bi->board_info;
unsigned n;
for (n = bi->n_board_info; n > 0; n--, chip++) {
if (chip->bus_num != master->bus_num)
continue;
/* NOTE: this relies on spi_new_device to
* issue diagnostics when given bogus inputs
*/
(void) spi_new_device(master, chip); //建立並註冊了spi_device
}
}
mutex_unlock(&board_lock);
}
六、spi_driver
本文先以linux內核中的/driver/mtd/devices/m25p80.c驅動爲參考。
static struct spi_driver m25p80_driver = { //spi_driver的構建
.driver = {
.name = "m25p80",
.bus = &spi_bus_type,
.owner = THIS_MODULE,
},
.probe = m25p_probe,
.remove = __devexit_p(m25p_remove),
*/
};
spi_register_driver(&m25p80_driver);//spi driver的註冊
在有匹配的spi device時,會調用m25p_probe
static int __devinit m25p_probe(struct spi_device *spi)
{
……
}
根據傳入的spi_device參數,能夠找到對應的spi_master。接下來就能夠利用spi子系統爲咱們完成數據交互了。能夠參看m25p80_read函數。要完成傳輸,先理解下面幾個結構的含義:(這兩個結構的定義及詳細註釋參見include/linux/spi/spi.h)
spi_message:描述一次完整的傳輸,即cs信號從高->底->高的傳輸
spi_transfer:多個spi_transfer夠成一個spi_message
舉例說明:m25p80的讀過程以下圖
能夠分解爲兩個spi_ transfer一個是寫命令,另外一個是讀數據。具體實現參見m25p80.c中的m25p80_read函數。下面內容摘取之此函數。
struct spi_transfer t[2]; //定義了兩個spi_transfer
struct spi_message m; //定義了兩個spi_message
spi_message_init(&m); //初始化其transfers鏈表
t[0].tx_buf = flash->command;
t[0].len = CMD_SIZE + FAST_READ_DUMMY_BYTE; //定義第一個transfer的寫指針和長度
spi_message_add_tail(&t[0], &m); //添加到spi_message
t[1].rx_buf = buf;
t[1].len = len; //定義第二個transfer的讀指針和長度
spi_message_add_tail(&t[1], &m); //添加到spi_message
flash->command[0] = OPCODE_READ;
flash->command[1] = from >> 16;
flash->command[2] = from >> 8;
flash->command[3] = from; //初始化前面寫buf的內容
spi_sync(flash->spi, &m); //調用spi_master發送spi_message
// spi_sync爲同步方式發送,還能夠用spi_async異步方式,那樣的話,須要設置回調完成函數。
另外你也能夠選擇一些封裝好的更容易使用的函數,這些函數能夠在include/linux/spi/spi.h文件中找到,如:
extern int spi_write_then_read(struct spi_device *spi, const u8 *txbuf, unsigned n_tx, u8 *rxbuf, unsigned n_rx);