内核代码如何知道正在使用哪个 spi 总线?

How does kernel code knows which spi bus is using?

我修改了设备树文件并使用4个GPIO引脚启用了spi,它支持pinmux并从gpio切换到spi功能。 但是在 Linux 内核代码中,代码如何知道使用了哪个 spi bus/pins 呢? 比如我找到一个Linux内核驱动:max1111.c,驱动一个spi ADC芯片。但是我检查了它的代码,并没有找到指定spi bus/pins的地方。

我在下面粘贴max1111.c。

/*
 * max1111.c - +2.7V, Low-Power, Multichannel, Serial 8-bit ADCs
 *
 * Based on arch/arm/mach-pxa/corgi_ssp.c
 *
 * Copyright (C) 2004-2005 Richard Purdie
 *
 * Copyright (C) 2008 Marvell International Ltd.
 *  Eric Miao <eric.miao@marvell.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  publishhed by the Free Software Foundation.
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/err.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/spi/spi.h>
#include <linux/slab.h>

enum chips { max1110, max1111, max1112, max1113 };

#define MAX1111_TX_BUF_SIZE 1
#define MAX1111_RX_BUF_SIZE 2

/* MAX1111 Commands */
#define MAX1111_CTRL_PD0      (1u << 0)
#define MAX1111_CTRL_PD1      (1u << 1)
#define MAX1111_CTRL_SGL      (1u << 2)
#define MAX1111_CTRL_UNI      (1u << 3)
#define MAX1110_CTRL_SEL_SH   (4)
#define MAX1111_CTRL_SEL_SH   (5)   /* NOTE: bit 4 is ignored */
#define MAX1111_CTRL_STR      (1u << 7)

struct max1111_data {
    struct spi_device   *spi;
    struct device       *hwmon_dev;
    struct spi_message  msg;
    struct spi_transfer xfer[2];
    uint8_t tx_buf[MAX1111_TX_BUF_SIZE];
    uint8_t rx_buf[MAX1111_RX_BUF_SIZE];
    struct mutex        drvdata_lock;
    /* protect msg, xfer and buffers from multiple access */
    int         sel_sh;
    int         lsb;
};

static int max1111_read(struct device *dev, int channel)
{
    struct max1111_data *data = dev_get_drvdata(dev);
    uint8_t v1, v2;
    int err;

    /* writing to drvdata struct is not thread safe, wait on mutex */
    mutex_lock(&data->drvdata_lock);

    data->tx_buf[0] = (channel << data->sel_sh) |
        MAX1111_CTRL_PD0 | MAX1111_CTRL_PD1 |
        MAX1111_CTRL_SGL | MAX1111_CTRL_UNI | MAX1111_CTRL_STR;

    err = spi_sync(data->spi, &data->msg);
    if (err < 0) {
        dev_err(dev, "spi_sync failed with %d\n", err);
        mutex_unlock(&data->drvdata_lock);
        return err;
    }

    v1 = data->rx_buf[0];
    v2 = data->rx_buf[1];

    mutex_unlock(&data->drvdata_lock);

    if ((v1 & 0xc0) || (v2 & 0x3f))
        return -EINVAL;

    return (v1 << 2) | (v2 >> 6);
}

#ifdef CONFIG_SHARPSL_PM
static struct max1111_data *the_max1111;

int max1111_read_channel(int channel)
{
    return max1111_read(&the_max1111->spi->dev, channel);
}
EXPORT_SYMBOL(max1111_read_channel);
#endif

/*
 * NOTE: SPI devices do not have a default 'name' attribute, which is
 * likely to be used by hwmon applications to distinguish between
 * different devices, explicitly add a name attribute here.
 */
static ssize_t show_name(struct device *dev,
             struct device_attribute *attr, char *buf)
{
    return sprintf(buf, "%s\n", to_spi_device(dev)->modalias);
}

static ssize_t show_adc(struct device *dev,
            struct device_attribute *attr, char *buf)
{
    struct max1111_data *data = dev_get_drvdata(dev);
    int channel = to_sensor_dev_attr(attr)->index;
    int ret;

    ret = max1111_read(dev, channel);
    if (ret < 0)
        return ret;

    /*
     * Assume the reference voltage to be 2.048V or 4.096V, with an 8-bit
     * sample. The LSB weight is 8mV or 16mV depending on the chip type.
     */
    return sprintf(buf, "%d\n", ret * data->lsb);
}

#define MAX1111_ADC_ATTR(_id)       \
    SENSOR_DEVICE_ATTR(in##_id##_input, S_IRUGO, show_adc, NULL, _id)

static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
static MAX1111_ADC_ATTR(0);
static MAX1111_ADC_ATTR(1);
static MAX1111_ADC_ATTR(2);
static MAX1111_ADC_ATTR(3);
static MAX1111_ADC_ATTR(4);
static MAX1111_ADC_ATTR(5);
static MAX1111_ADC_ATTR(6);
static MAX1111_ADC_ATTR(7);

static struct attribute *max1111_attributes[] = {
    &dev_attr_name.attr,
    &sensor_dev_attr_in0_input.dev_attr.attr,
    &sensor_dev_attr_in1_input.dev_attr.attr,
    &sensor_dev_attr_in2_input.dev_attr.attr,
    &sensor_dev_attr_in3_input.dev_attr.attr,
    NULL,
};

static const struct attribute_group max1111_attr_group = {
    .attrs  = max1111_attributes,
};

static struct attribute *max1110_attributes[] = {
    &sensor_dev_attr_in4_input.dev_attr.attr,
    &sensor_dev_attr_in5_input.dev_attr.attr,
    &sensor_dev_attr_in6_input.dev_attr.attr,
    &sensor_dev_attr_in7_input.dev_attr.attr,
    NULL,
};

static const struct attribute_group max1110_attr_group = {
    .attrs  = max1110_attributes,
};

static int setup_transfer(struct max1111_data *data)
{
    struct spi_message *m;
    struct spi_transfer *x;

    m = &data->msg;
    x = &data->xfer[0];

    spi_message_init(m);

    x->tx_buf = &data->tx_buf[0];
    x->len = MAX1111_TX_BUF_SIZE;
    spi_message_add_tail(x, m);

    x++;
    x->rx_buf = &data->rx_buf[0];
    x->len = MAX1111_RX_BUF_SIZE;
    spi_message_add_tail(x, m);

    return 0;
}

static int max1111_probe(struct spi_device *spi)
{
    enum chips chip = spi_get_device_id(spi)->driver_data;
    struct max1111_data *data;
    int err;

    spi->bits_per_word = 8;
    spi->mode = SPI_MODE_0;
    err = spi_setup(spi);
    if (err < 0)
        return err;

    data = devm_kzalloc(&spi->dev, sizeof(struct max1111_data), GFP_KERNEL);
    if (data == NULL) {
        dev_err(&spi->dev, "failed to allocate memory\n");
        return -ENOMEM;
    }

    switch (chip) {
    case max1110:
        data->lsb = 8;
        data->sel_sh = MAX1110_CTRL_SEL_SH;
        break;
    case max1111:
        data->lsb = 8;
        data->sel_sh = MAX1111_CTRL_SEL_SH;
        break;
    case max1112:
        data->lsb = 16;
        data->sel_sh = MAX1110_CTRL_SEL_SH;
        break;
    case max1113:
        data->lsb = 16;
        data->sel_sh = MAX1111_CTRL_SEL_SH;
        break;
    }
    err = setup_transfer(data);
    if (err)
        return err;

    mutex_init(&data->drvdata_lock);

    data->spi = spi;
    spi_set_drvdata(spi, data);

    err = sysfs_create_group(&spi->dev.kobj, &max1111_attr_group);
    if (err) {
        dev_err(&spi->dev, "failed to create attribute group\n");
        return err;
    }
    if (chip == max1110 || chip == max1112) {
        err = sysfs_create_group(&spi->dev.kobj, &max1110_attr_group);
        if (err) {
            dev_err(&spi->dev,
                "failed to create extended attribute group\n");
            goto err_remove;
        }
    }

    data->hwmon_dev = hwmon_device_register(&spi->dev);
    if (IS_ERR(data->hwmon_dev)) {
        dev_err(&spi->dev, "failed to create hwmon device\n");
        err = PTR_ERR(data->hwmon_dev);
        goto err_remove;
    }

#ifdef CONFIG_SHARPSL_PM
    the_max1111 = data;
#endif
    return 0;

err_remove:
    sysfs_remove_group(&spi->dev.kobj, &max1110_attr_group);
    sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group);
    return err;
}

static int max1111_remove(struct spi_device *spi)
{
    struct max1111_data *data = spi_get_drvdata(spi);

    hwmon_device_unregister(data->hwmon_dev);
    sysfs_remove_group(&spi->dev.kobj, &max1110_attr_group);
    sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group);
    mutex_destroy(&data->drvdata_lock);
    return 0;
}

static const struct spi_device_id max1111_ids[] = {
    { "max1110", max1110 },
    { "max1111", max1111 },
    { "max1112", max1112 },
    { "max1113", max1113 },
    { },
};
MODULE_DEVICE_TABLE(spi, max1111_ids);

static struct spi_driver max1111_driver = {
    .driver     = {
        .name   = "max1111",
        .owner  = THIS_MODULE,
    },
    .id_table   = max1111_ids,
    .probe      = max1111_probe,
    .remove     = max1111_remove,
};

module_spi_driver(max1111_driver);

MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
MODULE_DESCRIPTION("MAX1110/MAX1111/MAX1112/MAX1113 ADC Driver");
MODULE_LICENSE("GPL");

SPI 设备驱动程序(max1111 在您的情况下)在探测阶段 (max1111_probe) 获取指向底层 SPI 控制器 (struct spi_device *spi) 的指针。驱动程序应该使用它向控制器发送请求(例如使用 spi_sync)。驱动程序不知道 SPI 控制器使用什么 PINS。

什么 SPI 控制器传递给 SPI 设备驱动程序? SPI设备,应该是 在 SPI 控制器节点内的 DTS 文件中声明。从 SPI 控制器节点初始化的控制器被传递到设备 probe.

SPI 控制器可以是硬件(特定于 SoC),或 SPI-GPIO。在硬件 SPI 的情况下,引脚通常在 SoC TRM 中专用和指定。在 SPI-GPIO 的情况下,GPIO 名称在 SPI-GPIO 的 DTS 属性中指定。属性名称是:gpio-sck、gpio-miso、gpio-mosi、num-chipselects 和 cs-gpios(列表)。

SPI设备(max1111)被列为子设备的设备树文件将具有SPI模块的相应基地址,如下例所示,该基地址与SPI总线相关而不是设备。

spix@ABCDXYZ {
         compatible = "busdriver,variant";  

    /*This naming convention depends on the device tree*/    
    mosi = <gpioA> /*replace gpioA/B/C/D with your gpios*/
    miso = <gpioB>
    gpio-clk  = <gpioC>
    gpio-cs0  = <gpioD>
    spi-max-frequency = <freq1>;
                ;
                ;
                ;

max1111@0 {
        compatible = "max1111";
        reg = <0>;
        spi-max-frequency = <freq2>;
               ;
               ;
               ;
    };
        ;
        ;
        ;
 }

在上面的设备树文件中,您可以看到设备节点 "max1111" 列为总线节点 "spix" 下的子节点,其寄存器地址范围从地址@ABCDXYZ 开始。您需要参考您正在使用的 MCU 的用户指南以了解该基址。 驱动程序文件 max1111.c 是位于 spix 总线上的 SPI 设备之一的设备驱动程序文件。 驱动程序 max1111.c 不知道它所在的 SPI 线路,只是享受 SPI 总线驱动程序提供的 API(例如:数据传输和芯片 select 处理) 与 "busdriver,variant" 关联。 SPI 总线驱动程序将负责 configuring/multiplexing MCU 引脚进入 SPI 模式(在 machine/board 特定初始化代码中明确处理)。

所以你所要做的就是,

  1. 确定 max1111 设备正在使用哪个 SPI 总线。
  2. 然后到你板子对应的设备树文件
  3. 将您的设备驱动程序详细信息(+ SPI 参数)作为子节点的一部分添加到该总线节点
  4. 将您的 max1111.c 包含到内核 biuld 目录(主要是 drivers/spi 文件夹)

就是这样。

注意:这里假设您的机器已经启用了特定的 SPI 总线驱动程序。