再看一下对于spi设备结构更高层次的表述结构:
- struct spidev_data {
- dev_t devt;
- spinlock_t spi_lock;
- struct spi_device *spi; //指向spi设备结构
- struct list_head device_entry; //spi设备链表device_list挂接点
- /* buffer is NULL unless this device is open (users > 0) */
- struct mutex buf_lock;
- unsigned users;
- u8 *buffer;
- };
而driver端的表述结构struct spi_driver,具体如下:
- struct spi_driver {
- const struct spi_device_id *id_table; //匹配的设备表
- int (*probe)(struct spi_device *spi);
- int (*remove)(struct spi_device *spi);
- void (*shutdown)(struct spi_device *spi);
- int (*suspend)(struct spi_device *spi, pm_message_t mesg);
- int (*resume)(struct spi_device *spi);
- struct device_driver driver;
- };
是不是很标准?哈,好了,关于相关的数据结构就先介绍这么多。
二、spi核心代码的初始化分析
首先看一下spi总线的注册代码很简单,位于driver/spi/spi.c下:
- static int __init spi_init(void)
- {
- int status;
- //其中buf为static u8 *buf,
- //SPI_BUFSIZ为#define SPI_BUFSIZ max(32,SMP_CACHE_BYTES)
- buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
- if (!buf) {
- status = -ENOMEM;
- goto err0;
- }
- //注册spi_bus
- status = bus_register(&spi_bus_type);
- if (status < 0)
- goto err1;
- //在sys/class下产生spi_master这个节点,用于自动产生设备节点
- //下面挂接的是控制器的节点
- status = class_register(&spi_master_class);
- if (status < 0)
- goto err2;
- return 0;
- err2:
- bus_unregister(&spi_bus_type);
- err1:
- kfree(buf);
- buf = NULL;
- err0:
- return status;
- }
- //其中注册bus结构为
- 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,
- };
- //类函数为
- static struct class spi_master_class = {
- .name = "spi_master",
- .owner = THIS_MODULE,
- .dev_release = spi_master_release,
- };
- //顺便把driver与device的匹配函数也一起分析了吧,很简单
- 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 */
- //利用of表进行匹配
- if (of_driver_match_device(dev, drv))
- return 1;
- //利用id表进行匹配
- if (sdrv->id_table)
- return !!spi_match_id(sdrv->id_table, spi);
- //利用名字进行匹配
- return strcmp(spi->modalias, drv->name) == 0;
- }
再看一下driver的注册,spi_register_driver函数:
- int spi_register_driver(struct spi_driver *sdrv)
- {
- //很类似于platfor的注册,很简单,
- //就不具体介绍了
- 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;
- //注册标准的driver,此时会去匹配bus设备链表上
- //支持的device,找到会调用相应函数
- return driver_register(&sdrv->driver);
- }
还有个板级信息的添加函数:
- int __init
- spi_register_board_info(struct spi_board_info const *info, unsigned n)
- {
- struct boardinfo *bi;
- bi = kmalloc(sizeof(*bi) + n * sizeof *info, GFP_KERNEL);
- if (!bi)
- return -ENOMEM;
- bi->n_board_info = n;
- memcpy(bi->board_info, info, n * sizeof *info);
- mutex_lock(&board_lock);
- list_add_tail(&bi->list, &board_list);
- mutex_unlock(&board_lock);
- return 0;
- }
函数很简单,利用定义的spi_board_info信息,填充了boardinfo结构,并挂到board_list链表。
最后来看一下一个重量级函数,master的注册函数:
- int spi_register_master(struct spi_master *master)
- {
- static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
- struct device *dev = master->dev.parent;
- int status = -ENODEV;
- int dynamic = 0;
- //内嵌的标准设备结构必须初始化好
- if (!dev)
- return -ENODEV;
- /* even if it's just one always-selected device, there must
- * be at least one chipselect
- */
- //支持的设备数量为0,出错返回
- if (master->num_chipselect == 0)
- return -EINVAL;
- /* convention: dynamically assigned bus IDs count down from the max */
- if (master->bus_num < 0) {
- /* FIXME switch to an IDR based scheme, something like
- * I2C now uses, so we can't run out of "dynamic" IDs
- */
- //控制器编号(总线编号)不能小于0
- master->bus_num = atomic_dec_return(&dyn_bus_id);
- dynamic = 1;
- }
- spin_lock_init(&master->bus_lock_spinlock);
- mutex_init(&master->bus_lock_mutex);
- master->bus_lock_flag = 0;
- /* register the device, then userspace will see it.
- * registration fails if the bus ID is in use.
- */
- //设置master->dev的名字,形式为spi+bus_num,如:spi0
- dev_set_name(&master->dev, "spi%u", master->bus_num);
- //注册master内嵌的标准device结构
- status = device_add(&master->dev);
- if (status < 0)
- goto done;
- dev_dbg(dev, "registered master %s%s/n", dev_name(&master->dev),
- dynamic ? " (dynamic)" : "");
- /* populate children from any spi device tables */
- //重点哦,扫描并实例化spi设备
- scan_boardinfo(master);
- status = 0;
- /* Register devices from the device tree */
- of_register_spi_devices(master);
- done:
- return status;
- }
- //先来看scan_boardinfo这个分支
- static void scan_boardinfo(struct spi_master *master)
- {
- struct boardinfo *bi;
- mutex_lock(&board_lock);
- //找到我们注册的spi相关的板级信息结构体
- //还记得board_list吧
- list_for_each_entry(bi, &board_list, list) {
- struct spi_board_info *chip = bi->board_info;
- unsigned n;
- //因为可能存在多个spi总线,因此spi信息结构也会有
- //多个,找到bus号匹配的就对了
- 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);
- }
- }
- mutex_unlock(&board_lock);
- }
- //跟进spi_new_device函数
- struct spi_device *spi_new_device(struct spi_master *master,
- struct spi_board_info *chip)
- {
- struct spi_device *proxy;
- int status;
- /* NOTE: caller did any chip->bus_num checks necessary.
- *
- * Also, unless we change the return value convention to use
- * error-or-pointer (not NULL-or-pointer), troubleshootability
- * suggests syslogged diagnostics are best here (ugh).
- */
- //为需要实例化的设备分配内存
- //同时会将bus成员制定为spi_bus_type,parent
- //指定为该master,层次关系
- proxy = spi_alloc_device(master);
- if (!proxy)
- return NULL;
- WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
- //各个成员赋值,都是用我们注册的板级信息
- proxy->chip_select = chip->chip_select;
- proxy->max_speed_hz = chip->max_speed_hz;
- proxy->mode = chip->mode;
- proxy->irq = chip->irq;
- strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
- proxy->dev.platform_data = (void *) chip->platform_data;
- proxy->controller_data = chip->controller_data;
- proxy->controller_state = NULL;
- //把该设备添加到spi总线
- status = spi_add_device(proxy);
- if (status < 0) {
- spi_dev_put(proxy);
- return NULL;
- }
- return proxy;
- }
- //继续spi_add_device函数
- int spi_add_device(struct spi_device *spi)
- {
- static DEFINE_MUTEX(spi_add_lock);
- struct device *dev = spi->master->dev.parent;
- struct device *d;
- int status;
- /* Chipselects are numbered 0..max; validate. */
- //spi设备的编号不能大于master定义的最大数目
- if (spi->chip_select >= spi->master->num_chipselect) {
- dev_err(dev, "cs%d >= max %d/n",
- spi->chip_select,
- spi->master->num_chipselect);
- return -EINVAL;
- }
- /* Set the bus ID string */
- //设备节点名字,形式:spi0.0
- dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),
- spi->chip_select);
- /* We need to make sure there's no other device with this
- * chipselect **BEFORE** we call setup(), else we'll trash
- * its configuration. Lock against concurrent add() calls.
- */
- mutex_lock(&spi_add_lock);
- //确保设备不会重复注册
- d = bus_find_device_by_name(&spi_bus_type, NULL, dev_name(&spi->dev));
- if (d != NULL) {
- dev_err(dev, "chipselect %d already in use/n",
- spi->chip_select);
- put_device(d);
- status = -EBUSY;
- goto done;
- }
- /* Drivers may modify this initial i/o setup, but will
- * normally rely on the device being setup. Devices
- * using SPI_CS_HIGH can't coexist well otherwise...
- */
- //设置spi模式和时钟速率
- //会调用spi->master->setup函数设置
- status = spi_setup(spi);
- if (status < 0) {
- dev_err(dev, "can't %s %s, status %d/n",
- "setup", dev_name(&spi->dev), status);
- goto done;
- }
- /* Device may be bound to an active driver when this returns */
- //将该实例化的设备添加到总线
- status = device_add(&spi->dev);
- if (status < 0)
- dev_err(dev, "can't %s %s, status %d/n",
- "add", dev_name(&spi->dev), status);
- else
- dev_dbg(dev, "registered child %s/n", dev_name(&spi->dev));
- done:
- mutex_unlock(&spi_add_lock);
- return status;
- }
- //最后看下spi_setup函数
- int spi_setup(struct spi_device *spi)
- {
- unsigned bad_bits;
- int status;
- /* help drivers fail *cleanly* when they need options
- * that aren't supported with their current master
- */
- //必须和当前的master设置的模式匹配
- bad_bits = spi->mode & ~spi->master->mode_bits;
- if (bad_bits) {
- dev_dbg(&spi->dev, "setup: unsupported mode bits %x/n",
- bad_bits);
- return -EINVAL;
- }
- if (!spi->bits_per_word)
- spi->bits_per_word = 8;
- //最后调用控制器平台相关的setup成员设置该spi设备
- status = spi->master->setup(spi);
- dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s"
- "%u bits/w, %u Hz max --> %d/n",
- (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
- (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
- (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
- (spi->mode & SPI_3WIRE) ? "3wire, " : "",
- (spi->mode & SPI_LOOP) ? "loopback, " : "",
- spi->bits_per_word, spi->max_speed_hz,
- status);
- return status;
- }
- //回头看下一开始下面的那个分支of_register_spi_devices
- void of_register_spi_devices(struct spi_master *master)
- {
- struct spi_device *spi;
- struct device_node *nc;
- const __be32 *prop;
- int rc;
- int len;
- //如果定义了of_node成员才会走该分支
- if (!master->dev.of_node)
- return;
- //从控制器master->dev.of_node中去获取注册SPI设备spi_device的信息,然
- //后分配结构体spi_device并注册
- for_each_child_of_node(master->dev.of_node, nc) {
- /* Alloc an spi_device */
- spi = spi_alloc_device(master);
- if (!spi) {
- dev_err(&master->dev, "spi_device alloc error for %s/n",
- nc->full_name);
- spi_dev_put(spi);
- continue;
- }
- /* Select device driver */
- if (of_modalias_node(nc, spi->modalias,
- sizeof(spi->modalias)) < 0) {
- dev_err(&master->dev, "cannot find modalias for %s/n",
- nc->full_name);
- spi_dev_put(spi);
- continue;
- }
- /* Device address */
- prop = of_get_property(nc, "reg", &len);
- if (!prop || len < sizeof(*prop)) {
- dev_err(&master->dev, "%s has no 'reg' property/n",
- nc->full_name);
- spi_dev_put(spi);
- continue;
- }
- spi->chip_select = be32_to_cpup(prop);
- /* Mode (clock phase/polarity/etc.) */
- 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;
- /* Device speed */
- prop = of_get_property(nc, "spi-max-frequency", &len);
- if (!prop || len < sizeof(*prop)) {
- dev_err(&master->dev, "%s has no 'spi-max-frequency' property/n",
- nc->full_name);
- spi_dev_put(spi);
- continue;
- }
- spi->max_speed_hz = be32_to_cpup(prop);
- /* IRQ */
- spi->irq = irq_of_parse_and_map(nc, 0);
- /* Store a pointer to the node in the device structure */
- of_node_get(nc);
- spi->dev.of_node = nc;
- /* Register the new device */
- request_module(spi->modalias);
- rc = spi_add_device(spi);
- if (rc) {
- dev_err(&master->dev, "spi_device register error %s/n",
- nc->full_name);
- spi_dev_put(spi);
- }
- }
- }
