spi驱动模型和i2c的类似,都按照主机外设分离来设计的。但我觉得比i2c的要简明好多。
上文配置的spi驱动最外层是platform总线然后是spi总线然后是字符设备。
spi驱动模型分为
spi主控制器驱动,对应结构体spi_master-spi_s3c24xx.c。控制怎么发。
spi外设驱动,对应结构体spi_driver-----spidev.c。实现与用户的接口。
***************************************************************************************
对于te6410,linux2.6.36.2 2012-6-10
在板子文件mach-smdk6410.c中注册平台设备,
在spi_s3c64xx.c中使用platform_driver_probe(platform_driver_register)注册平台驱动,
在平台驱动的probe函数中,注册使用spi_register_master注册spi主机控制器驱动,实现操作spi寄存器。
在 spidev.c中使用spi_register_driver注册spi外设驱动。并注册字符设备实现与用户空间的接口。或者不使用字符设备也行,比如mcp2515没有使用字符设备,而是按照net_device的思路来实现用户接口的。
由于平台设备写进了板子文件,所以在系统启动时会自动注册这个平台设备及将其挂在平台总线。由于平台驱动也编译进了内核,所以也会自动注册即也挂在了平台总线。平台核心会匹配两者,
成功后,调用平台驱动的probe函数来注册spi主机控制器驱动此时会将主机控制器驱动挂在spi总线。在spi外设驱动insmod进内核时即也挂上了spi总线,spi核心会匹配两者,
成功后,调用spi外设驱动的probe函数实现真正的用户接口比如cdev,net_device。
而在spi外设驱动实现的read,write等函数,最终调用的是匹配的spi主机控制器驱动的transfer()
***************************************************************************************
关于spi_driver和spi_device的匹配:
spi_driver中name字段
static struct spi_driver spidev_spi_driver = {
.driver = {
.name = "spidev",
.owner = THIS_MODULE,
},
...
}
和mach-smdk6410.c中的modalias字段
static struct spi_board_info s3c2410_spi0_board[] = {
[0] = {
.modalias = "spidev",
.bus_num = 0,
.chip_select = 0,
.max_speed_hz = 500*1000,
},
};
要一致才行。
如果使用id_table来匹配则id_table中的项目要与spi_board_info中的modalias 匹配( 假如.modalias = "mcp2515", ),则在mcp251x.c中
static const struct spi_device_id mcp251x_id_table[] = {
{ "mcp2510", CAN_MCP251X_MCP2510 },
{ "mcp2515", CAN_MCP251X_MCP2515 },//必须的
{ },
};
MODULE_DEVICE_TABLE(spi, mcp251x_id_table);
static struct spi_driver mcp251x_can_driver = {
.driver = {
.name = "mcp2515",//无关,可以不是mcp2515
.bus = &spi_bus_type,
.owner = THIS_MODULE,
},
.id_table = mcp251x_id_table,
}
匹配原理
spi_master注册过程中会扫描arch/.../mach-*/board-*.c 中调用spi_register_board_info注册的信息,为每一个与本总线编号相同的信息建立一个spi_device。
根据Linux内核的驱动模型,注册在同一总线下的驱动和设备会进行匹配。spi_bus_type总线匹配的依据是名字。这样当自己编写的spi_driver和spi_device同名的时候,
spi_driver的probe方法就会被调用。spi_driver就能看到与自己匹配的spi_device了。
http://blog.csdn.net/yuanlulu/article/details/6318165
如果有idtable的话,就匹配idtable里各个项目的name,这样就可以支持多个name了,如源码
spi.c
***************************************************************************************
在spidev.c实现了spi的字符设备驱动
http://blog.csdn.net/songqqnew/article/details/7037583
上文配置的spi驱动最外层是platform总线然后是spi总线然后是字符设备。
spi驱动模型分为
spi主控制器驱动,对应结构体spi_master-spi_s3c24xx.c。控制怎么发。
spi外设驱动,对应结构体spi_driver-----spidev.c。实现与用户的接口。
***************************************************************************************
对于te6410,linux2.6.36.2 2012-6-10
在板子文件mach-smdk6410.c中注册平台设备,
在spi_s3c64xx.c中使用platform_driver_probe(platform_driver_register)注册平台驱动,
在平台驱动的probe函数中,注册使用spi_register_master注册spi主机控制器驱动,实现操作spi寄存器。
在 spidev.c中使用spi_register_driver注册spi外设驱动。并注册字符设备实现与用户空间的接口。或者不使用字符设备也行,比如mcp2515没有使用字符设备,而是按照net_device的思路来实现用户接口的。
由于平台设备写进了板子文件,所以在系统启动时会自动注册这个平台设备及将其挂在平台总线。由于平台驱动也编译进了内核,所以也会自动注册即也挂在了平台总线。平台核心会匹配两者,
成功后,调用平台驱动的probe函数来注册spi主机控制器驱动此时会将主机控制器驱动挂在spi总线。在spi外设驱动insmod进内核时即也挂上了spi总线,spi核心会匹配两者,
成功后,调用spi外设驱动的probe函数实现真正的用户接口比如cdev,net_device。
而在spi外设驱动实现的read,write等函数,最终调用的是匹配的spi主机控制器驱动的transfer()
***************************************************************************************
关于spi_driver和spi_device的匹配:
spi_driver中name字段
static struct spi_driver spidev_spi_driver = {
.driver = {
.name = "spidev",
.owner = THIS_MODULE,
},
...
}
和mach-smdk6410.c中的modalias字段
static struct spi_board_info s3c2410_spi0_board[] = {
[0] = {
.modalias = "spidev",
.bus_num = 0,
.chip_select = 0,
.max_speed_hz = 500*1000,
},
};
要一致才行。
如果使用id_table来匹配则id_table中的项目要与spi_board_info中的modalias 匹配( 假如.modalias = "mcp2515", ),则在mcp251x.c中
static const struct spi_device_id mcp251x_id_table[] = {
{ "mcp2510", CAN_MCP251X_MCP2510 },
{ "mcp2515", CAN_MCP251X_MCP2515 },//必须的
{ },
};
MODULE_DEVICE_TABLE(spi, mcp251x_id_table);
static struct spi_driver mcp251x_can_driver = {
.driver = {
.name = "mcp2515",//无关,可以不是mcp2515
.bus = &spi_bus_type,
.owner = THIS_MODULE,
},
.id_table = mcp251x_id_table,
}
匹配原理
spi_master注册过程中会扫描arch/.../mach-*/board-*.c 中调用spi_register_board_info注册的信息,为每一个与本总线编号相同的信息建立一个spi_device。
根据Linux内核的驱动模型,注册在同一总线下的驱动和设备会进行匹配。spi_bus_type总线匹配的依据是名字。这样当自己编写的spi_driver和spi_device同名的时候,
spi_driver的probe方法就会被调用。spi_driver就能看到与自己匹配的spi_device了。
http://blog.csdn.net/yuanlulu/article/details/6318165
如果有idtable的话,就匹配idtable里各个项目的name,这样就可以支持多个name了,如源码
spi.c
static int spi_match_device(struct device *dev, struct device_driver *drv)
{
const struct spi_device *spi = to_spi_device(dev);
const struct spi_driver *sdrv = to_spi_driver(drv);
/* Attempt an OF style match */
if (of_driver_match_device(dev, drv))
return 1;
if (sdrv->id_table)
return !!spi_match_id(sdrv->id_table, spi);
return strcmp(spi->modalias, drv->name) == 0;
}
static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,
const struct spi_device *sdev)
{
while (id->name[0]) {
if (!strcmp(sdev->modalias, id->name))
return id;
id++;
}
return NULL;
}
***************************************************************************************
在spidev.c实现了spi的字符设备驱动
static struct spi_driver spidev_spi_driver = {
.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);//注册字符设备,这个才是真的用户接口
if (status < 0)
return status;
spidev_class = class_create(THIS_MODULE, "spidev");
if (IS_ERR(spidev_class)) {
unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
return PTR_ERR(spidev_class);
}
status = spi_register_driver(&spidev_spi_driver);//注册spi_driver
if (status < 0) {
class_destroy(spidev_class);
unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
}
return status;
}
static const 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,
};
static int __devinit spidev_probe(struct spi_device *spi)
{
struct spidev_data *spidev;
int status;
unsigned long minor;
/* Allocate driver data */
spidev = kzalloc(sizeof(*spidev), GFP_KERNEL);
if (!spidev)
return -ENOMEM;
/* Initialize the driver data */
spidev->spi = spi;
spin_lock_init(&spidev->spi_lock);
mutex_init(&spidev->buf_lock);
INIT_LIST_HEAD(&spidev->device_entry);
/* If we can allocate a minor number, hook up this device.
* Reusing minors is fine so long as udev or mdev is working.
*/
mutex_lock(&device_list_lock);
minor = find_first_zero_bit(minors, N_SPI_MINORS);
if (minor < N_SPI_MINORS) {
struct device *dev;
spidev->devt = MKDEV(SPIDEV_MAJOR, minor);
dev = device_create(spidev_class, &spi->dev, spidev->devt,
spidev, "spidev%d.%d",
spi->master->bus_num, spi->chip_select);//创建设备文件
status = IS_ERR(dev) ? PTR_ERR(dev) : 0;
} else {
dev_dbg(&spi->dev, "no minor number available!\n");
status = -ENODEV;
}
if (status == 0) {
set_bit(minor, minors);
list_add(&spidev->device_entry, &device_list);
}
mutex_unlock(&device_list_lock);
if (status == 0)
spi_set_drvdata(spi, spidev);
else
kfree(spidev);
return status;
}
//spi读,调用spidev_sync_read--spidev_sync--spi_async,最后这个函数是spi核心提供的,这个函数最终会调用master的transfer函数直接操作硬件来传输数据。
/* Read-only message with current device setup */
static ssize_t
spidev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
{
struct spidev_data *spidev;
ssize_t status = 0;
/* chipselect only toggles at start or end of operation */
if (count > bufsiz)
return -EMSGSIZE;
spidev = filp->private_data;
mutex_lock(&spidev->buf_lock);
status = spidev_sync_read(spidev, count);
if (status > 0) {
unsigned long missing;
missing = copy_to_user(buf, spidev->buffer, status);
if (missing == status)
status = -EFAULT;
else
status = status - missing;
}
mutex_unlock(&spidev->buf_lock);
return status;
}
static inline ssize_t
spidev_sync_read(struct spidev_data *spidev, size_t len)
{
struct spi_transfer t = {
.rx_buf = spidev->buffer,
.len = len,
};
struct spi_message m;
spi_message_init(&m);//初始化spi_message,
spi_message_add_tail(&t, &m);
return spidev_sync(spidev, &m);
}
static ssize_t
spidev_sync(struct spidev_data *spidev, struct spi_message *message)
{
DECLARE_COMPLETION_ONSTACK(done);
int status;
message->complete = spidev_complete;
message->context = &done;
spin_lock_irq(&spidev->spi_lock);
if (spidev->spi == NULL)
status = -ESHUTDOWN;
else
status = spi_async(spidev->spi, message);//异步传输,如果是同步传输,则会阻塞一直到这个消息被处理完。
spin_unlock_irq(&spidev->spi_lock);
if (status == 0) {
wait_for_completion(&done);
status = message->status;
if (status == 0)
status = message->actual_length;
}
return status;
}
//spi写,调用spidev_sync_write--spidev_sync--spi_async,最后这个函数是spi核心提供的,这个函数最终会调用master的transfer函数直接操作硬件来传输数据。
/* Write-only message with current device setup */
static ssize_t
spidev_write(struct file *filp, const char __user *buf,
size_t count, loff_t *f_pos)
{
struct spidev_data *spidev;
ssize_t status = 0;
unsigned long missing;
/* chipselect only toggles at start or end of operation */
if (count > bufsiz)
return -EMSGSIZE;
spidev = filp->private_data;
mutex_lock(&spidev->buf_lock);
missing = copy_from_user(spidev->buffer, buf, count);
if (missing == 0) {
status = spidev_sync_write(spidev, count);
} else
status = -EFAULT;
mutex_unlock(&spidev->buf_lock);
return status;
}
http://blog.csdn.net/songqqnew/article/details/7037583