該系列文章將分為四個部分:
第一部分,將對SPI子系統整體進行描述,同時給出SPI的相關數據結構,最後描述SPI總線的注冊。基於S3C2440的嵌入式Linux驅動——SPI子系統解讀(一)http://www.linuxidc.com/Linux/2012-08/68402.htm
第二部分,該文將對SPI的主控制器(master)驅動進行描述。基於S3C2440的嵌入式Linux驅動——SPI子系統解讀(二)http://www.linuxidc.com/Linux/2012-08/68404.htm
第三部分,即本篇文章,該文將對SPI設備驅動,也稱protocol 驅動,進行講解。
第四部分,通過SPI設備驅動留給用戶層的API,我們將從上到下描述數據是如何通過SPI的protocol 驅動,由bitbang中轉,最後由master驅動將數據傳輸出去。 基於S3C2440的嵌入式Linux驅動——SPI子系統解讀(四)http://www.linuxidc.com/Linux/2012-08/68406.htm
本文屬於第三部分。
5. SPI設備驅動
在主控制器驅動中,spi_device已經注冊了,在設備驅動中,首先要做的就是注冊spi_driver,並提供用戶層相應的API。
5.1 SPI設備驅動的注冊
下列數據結構及函數位於drivers/spi/spidev.c。
- static struct file_operations spidev_fops = {
- .owner = THIS_MODULE,
- /* REVISIT switch to aio primitives, so that userspace
- * gets more complete API coverage. It'll simplify things
- * too, except for the locking.
- */
- .write = spidev_write,
- .read = spidev_read,
- .unlocked_ioctl = spidev_ioctl,
- .open = spidev_open,
- .release = spidev_release,
- };
-
- /* The main reason to have this class is to make mdev/udev create the
- * /dev/spidevB.C character device nodes exposing our userspace API.
- * It also simplifies memory management.
- */
-
- static struct class *spidev_class;
-
- static struct spi_driver spidev_spi = {
- .driver = {
- .name = "spidev",
- .owner = THIS_MODULE,
- },
- .probe = spidev_probe,
- .remove = __devexit_p(spidev_remove),
-
- /* NOTE: suspend/resume methods are not necessary here.
- * We don't do anything except pass the requests to/from
- * the underlying controller. The refrigerator handles
- * most issues; the controller driver handles the rest.
- */
- };
-
- static int __init spidev_init(void)
- {
- int status;
-
- /* Claim our 256 reserved device numbers. Then register a class
- * that will key udev/mdev to add/remove /dev nodes. Last, register
- * the driver which manages those device numbers.
- */
- BUILD_BUG_ON(N_SPI_MINORS > 256); /*檢查次設備號*/
- status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops); /*注冊字符設備,major=153*/
- if (status < 0)
- return status;
-
- spidev_class = class_create(THIS_MODULE, "spidev"); /*創建spidev類*/
- if (IS_ERR(spidev_class)) {
- unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);
- return PTR_ERR(spidev_class);
- }
-
- status = spi_register_driver(&spidev_spi); /*注冊spi_driver,並調用probe方法*/
- if (status < 0) {
- class_destroy(spidev_class);
- unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);
- }
- return status;
- }
- module_init(spidev_init);
-
- static void __exit spidev_exit(void)
- {
- spi_unregister_driver(&spidev_spi); /*注銷spi_driver*/
- class_destroy(spidev_class); /*注銷類*/
- unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);/*注銷字符設備*/
- }
- module_exit(spidev_exit);
該函數中,創建了一個字符設備以提供API給用戶層,同時創建了一個spidev類,最後注冊spi_driver到內核中。
在這裡我們看到了SPI設備驅動是如何提供API給用戶層的,那就是通過再熟悉不過的字符設備。通過字符設備,給用戶層提供了5個API:open,release,write,read和ioctl。本文在後面將介紹open和close,剩余3個將在本系列的第四篇文章中介紹。
接著看下spi_register_driver函數, 該函數位於drivers/spi/spidev.c。
- /**
- * spi_register_driver - register a SPI driver
- * @sdrv: the driver to register
- * Context: can sleep
- */
- int spi_register_driver(struct spi_driver *sdrv)
- {
- sdrv->driver.bus = &spi_bus_type;
- if (sdrv->probe)
- sdrv->driver.probe = spi_drv_probe;
- if (sdrv->remove)
- sdrv->driver.remove = spi_drv_remove;
- if (sdrv->shutdown)
- sdrv->driver.shutdown = spi_drv_shutdown;
- return driver_register(&sdrv->driver);
- }
- EXPORT_SYMBOL_GPL(spi_register_driver);
在調用driver_register的過程中,將用driver.name和spi_device的modalias字段進行比較,兩者相等則將該spi_driver和spi_device進行綁定。
當spi_driver注冊成功以後,將調用probe方法:spidev_probe函數。