• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

linux regmap and regmap_config

武飞扬头像
hhcs
帮助1

本文概述了regmap--它在剔除Linux子系统中的常见代码方面有多大作用,以及如何有效利用它在Linux中编写驱动程序。

Linux被划分为许多子系统,以便将不同部分的通用代码因子化,并简化驱动开发,这有助于代码维护。

Linux有一些子系统,如I2C和SPI,它们被用来连接停留在这些总线上的设备。这两种总线都有一个共同的功能,即从连接到它们的设备上读写寄存器。因此,读写这些寄存器、缓存寄存器的值等的代码必须存在于这两个子系统中。这导致冗余的代码存在于所有具有这种寄存器读写功能的子系统中。

为了避免这种情况的发生,并将普通的代码因素化,同时也为了方便驱动程序的维护和开发,Linux开发者从3.1版本开始引入了一个新的内核API,这个API被称为regmap。这个基础设施以前存在于Linux AsoC(ALSA)子系统中,但现在已经通过regmap API提供给整个Linux了。

早些时候,如果为驻留在SPI总线上的设备编写驱动程序,那么该驱动程序直接使用SPI子系统的SPI总线读写调用来与设备对话。现在,它使用regmap API来完成这一工作。regmap子系统负责调用SPI子系统的相关调用。因此,两个设备--一个驻留在I2C总线上,一个驻留在SPI总线上--将有相同的regmap读写调用来与总线上的各自设备对话。

这个子系统在Linux 3.1中首次引入,后来又引入了以下不同的功能。

  • 支持SPMI、MMIO
  • Spinlock和自定义锁机制
  • 缓存支持
  • 内联性转换
  • 寄存器范围检查
  • 支持IRQ
  • 只读和只写寄存器
  • 珍贵寄存器和易失性寄存器
  • 寄存器页

实现regmap
Linux中的regmap提供的API在include/linux/regmap.h中声明,在drivers/base/regmap/中实现。

在Linux的regmap中需要理解的重要数据结构

结构regmap_config

  1.  
    /**
  2.  
    * Configuration for the register map of a device.
  3.  
    *
  4.  
    * @name: Optional name of the regmap. Useful when a device has multiple
  5.  
    * register regions.
  6.  
    *
  7.  
    * @reg_bits: Number of bits in a register address, mandatory.
  8.  
    * @reg_stride: The register address stride. Valid register addresses are a
  9.  
    * multiple of this value. If set to 0, a value of 1 will be
  10.  
    * used.
  11.  
    * @pad_bits: Number of bits of padding between register and value.
  12.  
    * @val_bits: Number of bits in a register value, mandatory.
  13.  
    *
  14.  
    * @writeable_reg: Optional callback returning true if the register
  15.  
    * can be written to. If this field is NULL but wr_table
  16.  
    * (see below) is not, the check is performed on such table
  17.  
    * (a register is writeable if it belongs to one of the ranges
  18.  
    * specified by wr_table).
  19.  
    * @readable_reg: Optional callback returning true if the register
  20.  
    * can be read from. If this field is NULL but rd_table
  21.  
    * (see below) is not, the check is performed on such table
  22.  
    * (a register is readable if it belongs to one of the ranges
  23.  
    * specified by rd_table).
  24.  
    * @volatile_reg: Optional callback returning true if the register
  25.  
    * value can't be cached. If this field is NULL but
  26.  
    * volatile_table (see below) is not, the check is performed on
  27.  
    * such table (a register is volatile if it belongs to one of
  28.  
    * the ranges specified by volatile_table).
  29.  
    * @precious_reg: Optional callback returning true if the register
  30.  
    * should not be read outside of a call from the driver
  31.  
    * (e.g., a clear on read interrupt status register). If this
  32.  
    * field is NULL but precious_table (see below) is not, the
  33.  
    * check is performed on such table (a register is precious if
  34.  
    * it belongs to one of the ranges specified by precious_table).
  35.  
    * @lock: Optional lock callback (overrides regmap's default lock
  36.  
    * function, based on spinlock or mutex).
  37.  
    * @unlock: As above for unlocking.
  38.  
    * @lock_arg: this field is passed as the only argument of lock/unlock
  39.  
    * functions (ignored in case regular lock/unlock functions
  40.  
    * are not overridden).
  41.  
    * @reg_read: Optional callback that if filled will be used to perform
  42.  
    * all the reads from the registers. Should only be provided for
  43.  
    * devices whose read operation cannot be represented as a simple
  44.  
    * read operation on a bus such as SPI, I2C, etc. Most of the
  45.  
    * devices do not need this.
  46.  
    * @reg_write: Same as above for writing.
  47.  
    * @fast_io: Register IO is fast. Use a spinlock instead of a mutex
  48.  
    * to perform locking. This field is ignored if custom lock/unlock
  49.  
    * functions are used (see fields lock/unlock of struct regmap_config).
  50.  
    * This field is a duplicate of a similar file in
  51.  
    * 'struct regmap_bus' and serves exact same purpose.
  52.  
    * Use it only for "no-bus" cases.
  53.  
    * @max_register: Optional, specifies the maximum valid register index.
  54.  
    * @wr_table: Optional, points to a struct regmap_access_table specifying
  55.  
    * valid ranges for write access.
  56.  
    * @rd_table: As above, for read access.
  57.  
    * @volatile_table: As above, for volatile registers.
  58.  
    * @precious_table: As above, for precious registers.
  59.  
    * @reg_defaults: Power on reset values for registers (for use with
  60.  
    * register cache support).
  61.  
    * @num_reg_defaults: Number of elements in reg_defaults.
  62.  
    *
  63.  
    * @read_flag_mask: Mask to be set in the top byte of the register when doing
  64.  
    * a read.
  65.  
    * @write_flag_mask: Mask to be set in the top byte of the register when doing
  66.  
    * a write. If both read_flag_mask and write_flag_mask are
  67.  
    * empty the regmap_bus default masks are used.
  68.  
    * @use_single_rw: If set, converts the bulk read and write operations into
  69.  
    * a series of single read and write operations. This is useful
  70.  
    * for device that does not support bulk read and write.
  71.  
    * @can_multi_write: If set, the device supports the multi write mode of bulk
  72.  
    * write operations, if clear multi write requests will be
  73.  
    * split into individual write operations
  74.  
    *
  75.  
    * @cache_type: The actual cache type.
  76.  
    * @reg_defaults_raw: Power on reset values for registers (for use with
  77.  
    * register cache support).
  78.  
    * @num_reg_defaults_raw: Number of elements in reg_defaults_raw.
  79.  
    * @reg_format_endian: Endianness for formatted register addresses. If this is
  80.  
    * DEFAULT, the @reg_format_endian_default value from the
  81.  
    * regmap bus is used.
  82.  
    * @val_format_endian: Endianness for formatted register values. If this is
  83.  
    * DEFAULT, the @reg_format_endian_default value from the
  84.  
    * regmap bus is used.
  85.  
    *
  86.  
    * @ranges: Array of configuration entries for virtual address ranges.
  87.  
    * @num_ranges: Number of range configuration entries.
  88.  
    */
  89.  
    struct regmap_config {
  90.  
    const char *name; 可选,寄存器名字
  91.  
     
  92.  
    int reg_bits; 寄存器地址位宽,必须填写
  93.  
    int reg_stride;// 寄存器操作宽度,比如为1时,所有寄存器可操作,为2时,只有2^n可操作
  94.  
    int pad_bits;
  95.  
    int val_bits;
  96.  
     
  97.  
    bool (*writeable_reg)(struct device *dev, unsigned int reg);
  98.  
    bool (*readable_reg)(struct device *dev, unsigned int reg);
  99.  
    bool (*volatile_reg)(struct device *dev, unsigned int reg);
  100.  
    bool (*precious_reg)(struct device *dev, unsigned int reg);
  101.  
    regmap_lock lock;
  102.  
    regmap_unlock unlock;
  103.  
    void *lock_arg;
  104.  
     
  105.  
    int (*reg_read)(void *context, unsigned int reg, unsigned int *val);
  106.  
    int (*reg_write)(void *context, unsigned int reg, unsigned int val);
  107.  
     
  108.  
    bool fast_io;
  109.  
     
  110.  
    unsigned int max_register;
  111.  
    const struct regmap_access_table *wr_table;
  112.  
    const struct regmap_access_table *rd_table;
  113.  
    const struct regmap_access_table *volatile_table;
  114.  
    const struct regmap_access_table *precious_table;
  115.  
    const struct reg_default *reg_defaults;
  116.  
    unsigned int num_reg_defaults;
  117.  
    enum regcache_type cache_type;
  118.  
    const void *reg_defaults_raw;
  119.  
    unsigned int num_reg_defaults_raw;
  120.  
     
  121.  
    u8 read_flag_mask;
  122.  
    u8 write_flag_mask;
  123.  
     
  124.  
    bool use_single_rw;
  125.  
    bool can_multi_write;
  126.  
     
  127.  
    enum regmap_endian reg_format_endian;
  128.  
    enum regmap_endian val_format_endian;
  129.  
     
  130.  
    const struct regmap_range_cfg *ranges;
  131.  
    unsigned int num_ranges;
  132.  
    };
学新通


这是一个由regmap子系统使用的每个设备配置结构,用于与设备对话。它由驱动代码定义,包含所有与设备的寄存器相关的信息。它的重要字段的描述列在下面。

这是该设备寄存器中的位数,例如,如果是1字节的寄存器,它将被设置为8的值。

val_bits: 这是将在设备寄存器中设置的值的位数。
writeable_reg:这是一个写在驱动程序代码中的用户定义的函数,每当要写一个寄存器时,就会调用这个函数。每当驱动调用regmap子系统写一个寄存器时,这个驱动函数就会被调用;如果这个寄存器不可写,它将返回 "false",写操作将向驱动返回一个错误。这是一个 "每个寄存器 "的写操作回调,是可选的。
wr_table。如果驱动没有提供writeable_reg回调,那么在进行写操作之前,regmap会检查wr_table。如果寄存器地址在wr_table提供的范围内,那么就执行写操作。这也是可选的,驱动可以省略它的定义,也可以将它设置为NULL。

readable_reg: 这是一个用户定义的函数,写在驱动代码中,每当要读一个寄存器时就会调用。每当驱动调用regmap子系统读取一个寄存器时,这个驱动函数就会被调用以确保该寄存器是可读的。如果这个寄存器不可读,驱动函数将返回 "false",读操作将向驱动返回一个错误。这是一个 "每个寄存器 "的读操作回调,是可选的。

rd_table: 如果一个驱动没有提供readable_reg回调,那么在进行读操作之前,regmap会检查rd_table。如果寄存器地址在rd_table提供的范围内,那么读操作就被执行。这也是可选的,驱动程序可以省略它的定义,可以将它设置为NULL。
volatile_reg: 这是一个写在驱动代码中的用户定义的函数,每当通过缓存写入或读出一个寄存器时就会调用这个函数。每当驱动通过regmap缓存读取或写入一个寄存器时,这个函数首先被调用,如果它返回 "false",才会使用缓存方法;否则,寄存器被直接写入或读取,因为寄存器是易失性的,不需要使用缓存。这是一个 "每个寄存器 "的操作回调,是可选的。

volatile_table: 如果一个驱动没有提供volatile_reg回调,那么volatile_table就会被regmap检查,看寄存器是否是volatile的。如果寄存器地址在volatile_table提供的范围内,则不使用缓存操作。这也是可选的,驱动程序可以省略它的定义,也可以将它设置为NULL。

锁定。这是一个用户定义的回调函数,写在驱动代码中,在开始任何读或写操作之前被调用。该函数应该接受一个锁,并返回它。这是一个可选的函数--如果不提供,regmap将提供自己的锁定机制。

解锁。这是用户定义的回调,写在驱动代码中,用于解锁,它是由锁例程创建的。这是可选的,如果不提供,将由regmap的内部锁定机制取代。

lock_arg: 这是传递给锁定和解锁回调例程的参数。
fast_io。如果没有提供自定义的锁和解锁机制,regmap内部使用mutex来锁和解锁。如果驱动希望regmap使用自旋锁,那么fast_io应该设置为 "true";否则,regmap将使用基于mutex的锁。

max_register。每当要执行任何读或写操作时,regmap首先检查寄存器地址是否小于max_register,只有当它是,才执行操作。 max_register如果被设置为0,则被忽略。

read_flag_mask。通常,在SPI或I2C中,一个写或读将在最高字节中设置最高位,以区分写和读操作。这个掩码被设置在寄存器值的较高字节中。
write_flag_mask。这个掩码也被设置在寄存器值的较高字节中。

这些是结构regmap_config的字段,这个配置被传递给regmap_init,它创建结构regmap并返回可以在读写操作中使用的内容。
wr_table, rd_table和volatile_table是可选的表(仅在没有提供相应的回调时使用),在写和读操作时被regmap用来进行范围检查。这些被实现为regmap_access_table结构。以下是它的字段。
yes_ranges:这些是被认为是有效范围的地址范围。
n_yes_ranges:这是yes_ranges中的条目数。
no_ranges:这些是被认为是无效范围的地址范围。
n_no_ranges:这是no_ranges中的条目数。


struct regmap  

  1.  
    struct regmap {
  2.  
    union {
  3.  
    struct mutex mutex;
  4.  
    struct {
  5.  
    spinlock_t spinlock;
  6.  
    unsigned long spinlock_flags;
  7.  
    };
  8.  
    };
  9.  
    regmap_lock lock;
  10.  
    regmap_unlock unlock;
  11.  
    void *lock_arg; /* This is passed to lock/unlock functions */
  12.  
     
  13.  
    struct device *dev; /* Device we do I/O on */
  14.  
    void *work_buf; /* Scratch buffer used to format I/O */
  15.  
    struct regmap_format format; /* Buffer format */
  16.  
    const struct regmap_bus *bus;
  17.  
    void *bus_context;
  18.  
    const char *name;
  19.  
     
  20.  
    bool async;
  21.  
    spinlock_t async_lock;
  22.  
    wait_queue_head_t async_waitq;
  23.  
    struct list_head async_list;
  24.  
    struct list_head async_free;
  25.  
    int async_ret;
  26.  
     
  27.  
    #ifdef CONFIG_DEBUG_FS
  28.  
    struct dentry *debugfs;
  29.  
    const char *debugfs_name;
  30.  
     
  31.  
    unsigned int debugfs_reg_len;
  32.  
    unsigned int debugfs_val_len;
  33.  
    unsigned int debugfs_tot_len;
  34.  
     
  35.  
    struct list_head debugfs_off_cache;
  36.  
    struct mutex cache_lock;
  37.  
    #endif
  38.  
     
  39.  
    unsigned int max_register;
  40.  
    bool (*writeable_reg)(struct device *dev, unsigned int reg);
  41.  
    bool (*readable_reg)(struct device *dev, unsigned int reg);
  42.  
    bool (*volatile_reg)(struct device *dev, unsigned int reg);
  43.  
    bool (*precious_reg)(struct device *dev, unsigned int reg);
  44.  
    const struct regmap_access_table *wr_table;
  45.  
    const struct regmap_access_table *rd_table;
  46.  
    const struct regmap_access_table *volatile_table;
  47.  
    const struct regmap_access_table *precious_table;
  48.  
     
  49.  
    int (*reg_read)(void *context, unsigned int reg, unsigned int *val);
  50.  
    int (*reg_write)(void *context, unsigned int reg, unsigned int val);
  51.  
     
  52.  
    bool defer_caching;
  53.  
     
  54.  
    u8 read_flag_mask;
  55.  
    u8 write_flag_mask;
  56.  
     
  57.  
    /* number of bits to (left) shift the reg value when formatting*/
  58.  
    int reg_shift;
  59.  
    int reg_stride;
  60.  
     
  61.  
    /* regcache specific members */
  62.  
    const struct regcache_ops *cache_ops;
  63.  
    enum regcache_type cache_type;
  64.  
     
  65.  
    /* number of bytes in reg_defaults_raw */
  66.  
    unsigned int cache_size_raw;
  67.  
    /* number of bytes per word in reg_defaults_raw */
  68.  
    unsigned int cache_word_size;
  69.  
    /* number of entries in reg_defaults */
  70.  
    unsigned int num_reg_defaults;
  71.  
    /* number of entries in reg_defaults_raw */
  72.  
    unsigned int num_reg_defaults_raw;
  73.  
     
  74.  
    /* if set, only the cache is modified not the HW */
  75.  
    u32 cache_only;
  76.  
    /* if set, only the HW is modified not the cache */
  77.  
    u32 cache_bypass;
  78.  
    /* if set, remember to free reg_defaults_raw */
  79.  
    bool cache_free;
  80.  
     
  81.  
    struct reg_default *reg_defaults;
  82.  
    const void *reg_defaults_raw;
  83.  
    void *cache;
  84.  
    u32 cache_dirty;
  85.  
     
  86.  
    struct reg_default *patch;
  87.  
    int patch_regs;
  88.  
     
  89.  
    /* if set, converts bulk rw to single rw */
  90.  
    bool use_single_rw;
  91.  
    /* if set, the device supports multi write mode */
  92.  
    bool can_multi_write;
  93.  
     
  94.  
    struct rb_root range_tree;
  95.  
    void *selector_work_buf; /* Scratch buffer used for selector */
  96.  
    };
学新通

regmap_init主要是把config和描述i2c设备的bus设置到regmap中.

  1.  
    struct regmap *regmap_init(struct device *dev,
  2.  
    const struct regmap_bus *bus,
  3.  
    void *bus_context,
  4.  
    const struct regmap_config *config)
  5.  
    {
  6.  
    struct regmap *map;
  7.  
    int ret = -EINVAL;
  8.  
    enum regmap_endian reg_endian, val_endian;
  9.  
     
  10.  
    map = kzalloc(sizeof(*map), GFP_KERNEL);
  11.  
    // 将regmap_config定义的参数赋值到regmap中
  12.  
    map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
  13.  
    map->format.pad_bytes = config->pad_bits / 8;
  14.  
    map->format.val_bytes = DIV_ROUND_UP(config->val_bits, 8);
  15.  
    map->format.buf_size = DIV_ROUND_UP(config->reg_bits
  16.  
    config->val_bits config->pad_bits, 8);
  17.  
    map->reg_shift = config->pad_bits % 8;
  18.  
    if (config->reg_stride)
  19.  
    map->reg_stride = config->reg_stride;
  20.  
    else
  21.  
    map->reg_stride = 1;
  22.  
    map->use_single_rw = config->use_single_rw;
  23.  
    map->can_multi_write = config->can_multi_write;
  24.  
    map->dev = dev;
  25.  
    map->bus = bus;
  26.  
    map->bus_context = bus_context;
  27.  
    map->max_register = config->max_register;
  28.  
    map->wr_table = config->wr_table;
  29.  
    map->rd_table = config->rd_table;
  30.  
    map->volatile_table = config->volatile_table;
  31.  
    map->precious_table = config->precious_table;
  32.  
    map->writeable_reg = config->writeable_reg;
  33.  
    map->readable_reg = config->readable_reg;
  34.  
    map->volatile_reg = config->volatile_reg;
  35.  
    map->precious_reg = config->precious_reg;
  36.  
    map->cache_type = config->cache_type;
  37.  
    map->name = config->name;
  38.  
     
  39.  
    /* regmap中有reg_read操作方法,通过bus是否为空赋值 */
  40.  
    if (!bus) {
  41.  
    // 对于该驱动,此处只会走这里
  42.  
    map->reg_read = config->reg_read;
  43.  
    map->reg_write = config->reg_write;
  44.  
     
  45.  
    map->defer_caching = false;
  46.  
    goto skip_format_initialization;
  47.  
    } else if (!bus->read || !bus->write) {
  48.  
    map->reg_read = _regmap_bus_reg_read;
  49.  
    map->reg_write = _regmap_bus_reg_write;
  50.  
     
  51.  
    map->defer_caching = false;
  52.  
    goto skip_format_initialization;
  53.  
    } else {
  54.  
    map->reg_read = _regmap_bus_read;
  55.  
    }
  56.  
    // 设置地址和寄存器值的大小端
  57.  
    reg_endian = regmap_get_reg_endian(bus, config);
  58.  
    val_endian = regmap_get_val_endian(dev, bus, config);
  59.  
    // 根据寄存器地址位宽和大小端解析寄存器地址
  60.  
    switch (config->reg_bits map->reg_shift) {
  61.  
    case 32:
  62.  
    switch (reg_endian) {
  63.  
    case REGMAP_ENDIAN_BIG:
  64.  
    map->format.format_reg = regmap_format_32_be;
  65.  
    break;
  66.  
    case REGMAP_ENDIAN_NATIVE:
  67.  
    map->format.format_reg = regmap_format_32_native;
  68.  
    break;
  69.  
    default:
  70.  
    goto err_map;
  71.  
    }
  72.  
    break;
  73.  
    }
  74.  
    // 根据寄存器值位宽和大小端解析寄存器值
  75.  
    switch (config->val_bits) {
  76.  
    case 16:
  77.  
    switch (val_endian) {
  78.  
    case REGMAP_ENDIAN_BIG:
  79.  
    map->format.format_val = regmap_format_16_be;
  80.  
    map->format.parse_val = regmap_parse_16_be;
  81.  
    map->format.parse_inplace = regmap_parse_16_be_inplace;
  82.  
    break;
  83.  
    case REGMAP_ENDIAN_LITTLE:
  84.  
    map->format.format_val = regmap_format_16_le;
  85.  
    map->format.parse_val = regmap_parse_16_le;
  86.  
    map->format.parse_inplace = regmap_parse_16_le_inplace;
  87.  
    break;
  88.  
    case REGMAP_ENDIAN_NATIVE:
  89.  
    map->format.format_val = regmap_format_16_native;
  90.  
    map->format.parse_val = regmap_parse_16_native;
  91.  
    break;
  92.  
    default:
  93.  
    goto err_map;
  94.  
    }
  95.  
    break;
  96.  
    }
  97.  
     
  98.  
    /* 对于val_bits = 16,reg_bits=16,regmap写函数选择_regmap_bus_raw_write */
  99.  
    if (map->format.format_write) {
  100.  
    map->defer_caching = false;
  101.  
    map->reg_write = _regmap_bus_formatted_write;
  102.  
    } else if (map->format.format_val) {
  103.  
    map->defer_caching = true;
  104.  
    map->reg_write = _regmap_bus_raw_write;
  105.  
    }
  106.  
    // 缓存初始化
  107.  
    ret = regcache_init(map, config);
  108.  
    }
学新通

regmap_init_i2c

当设备驱动配置好config以后,调用对应的regmap_init_xx,例如,这里是regmap_init_i2c

  1.  
    // drivers/base/regmap/regmap-i2c.c
  2.  
     
  3.  
    struct regmap *regmap_init_i2c(struct i2c_client *i2c,
  4.  
    const struct regmap_config *config)
  5.  
    {
  6.  
    // regmap_bus 定位了对应总线的读写函数。
  7.  
    const struct regmap_bus *bus = regmap_get_i2c_bus(i2c, config);
  8.  
     
  9.  
    if (IS_ERR(bus))
  10.  
    return ERR_CAST(bus);
  11.  
     
  12.  
    return regmap_init(&i2c->dev, bus, &i2c->dev, config);
  13.  
    }

注意到2个新的数据结构:

  • struct regmap_bus *bus
  • struct regmap *regmap_init_i2c(..

regmap_get_i2c_bus

对于普通I2C设备,regmap_bus为:

  1.  
    // drivers/base/regmap/regmap-i2c.c
  2.  
    static const struct regmap_bus *regmap_get_i2c_bus(struct i2c_client *i2c,
  3.  
    const struct regmap_config *config)
  4.  
    {
  5.  
    if (i2c_check_functionality(i2c->adapter, I2C_FUNC_I2C))
  6.  
    return &regmap_i2c;
  7.  
    // ...
  8.  
     
  9.  
    return ERR_PTR(-ENOTSUPP);
  10.  
    }
  11.  
     
  12.  
    static struct regmap_bus regmap_i2c = {
  13.  
    .write = regmap_i2c_write,
  14.  
    .gather_write = regmap_i2c_gather_write,
  15.  
    .read = regmap_i2c_read,
  16.  
    .reg_format_endian_default = REGMAP_ENDIAN_BIG,
  17.  
    .val_format_endian_default = REGMAP_ENDIAN_BIG,
  18.  
    };
学新通

regmap_bus结构定义了读写函数和默认的寄存器地址和寄存器值的大小端。

regmap_bus原型

  1.  
    /**
  2.  
    * struct regmap_bus - Description of a hardware bus for the register map
  3.  
    * infrastructure.
  4.  
    *
  5.  
    * @fast_io: Register IO is fast. Use a spinlock instead of a mutex
  6.  
    * to perform locking. This field is ignored if custom lock/unlock
  7.  
    * functions are used (see fields lock/unlock of
  8.  
    * struct regmap_config).
  9.  
    * @write: Write operation.
  10.  
    * @gather_write: Write operation with split register/value, return -ENOTSUPP
  11.  
    * if not implemented on a given device.
  12.  
    * @async_write: Write operation which completes asynchronously, optional and
  13.  
    * must serialise with respect to non-async I/O.
  14.  
    * @reg_write: Write a single register value to the given register address. This
  15.  
    * write operation has to complete when returning from the function.
  16.  
    * @reg_update_bits: Update bits operation to be used against volatile
  17.  
    * registers, intended for devices supporting some mechanism
  18.  
    * for setting clearing bits without having to
  19.  
    * read/modify/write.
  20.  
    * @read: Read operation. Data is returned in the buffer used to transmit
  21.  
    * data.
  22.  
    * @reg_read: Read a single register value from a given register address.
  23.  
    * @free_context: Free context.
  24.  
    * @async_alloc: Allocate a regmap_async() structure.
  25.  
    * @read_flag_mask: Mask to be set in the top byte of the register when doing
  26.  
    * a read.
  27.  
    * @reg_format_endian_default: Default endianness for formatted register
  28.  
    * addresses. Used when the regmap_config specifies DEFAULT. If this is
  29.  
    * DEFAULT, BIG is assumed.
  30.  
    * @val_format_endian_default: Default endianness for formatted register
  31.  
    * values. Used when the regmap_config specifies DEFAULT. If this is
  32.  
    * DEFAULT, BIG is assumed.
  33.  
    * @max_raw_read: Max raw read size that can be used on the bus.
  34.  
    * @max_raw_write: Max raw write size that can be used on the bus.
  35.  
    */
  36.  
    struct regmap_bus {
  37.  
    bool fast_io;
  38.  
    regmap_hw_write write;
  39.  
    regmap_hw_gather_write gather_write;
  40.  
    regmap_hw_async_write async_write;
  41.  
    regmap_hw_reg_write reg_write;
  42.  
    regmap_hw_reg_update_bits reg_update_bits;
  43.  
    regmap_hw_read read;
  44.  
    regmap_hw_reg_read reg_read;
  45.  
    regmap_hw_free_context free_context;
  46.  
    regmap_hw_async_alloc async_alloc;
  47.  
    u8 read_flag_mask;
  48.  
    enum regmap_endian reg_format_endian_default;
  49.  
    enum regmap_endian val_format_endian_default;
  50.  
    size_t max_raw_read;
  51.  
    size_t max_raw_write;
  52.  
    };
学新通

regmap APIs
regmap的API在include/linux/regmap.h中声明,以下是重要API的细节。
初始化例程。下面的例程根据SPI配置初始化regmap数据结构。

struct regmap * devm_regmap_init_spi(struct spi_device *spi, const struct regmap_config)。

下面的例程根据I2C配置初始化regmap数据结构。

struct regmap * devm_regmap_init_i2c(struct i2c_client *i2c, const struct regmap_config)。

在regmap初始化例程中,摄取regmap_config配置;然后分配regmap结构并将配置复制到其中。各个总线的读/写功能也被复制到regmap结构中。例如,在SPI总线的情况下,regmap的读写功能指针将指向SPI的读写功能。
regmap初始化后,驱动程序可以使用以下例程与设备对话。

int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)。

int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)。

regmap_write。这个函数用来向设备写数据。它接收初始化期间返回的regmap结构,注册地址和要设置的值。以下是regmap_write例程执行的步骤。

首先,regmap_write接收锁,如果regmap_config中的fast_io被设置,它将是spinlock;否则,它将是mutex。

接下来,如果在regmap_config中设置了max_register,那么它将检查传递的寄存器地址是否小于max_register。如果小于max_register,那么只执行写操作;否则,返回-EIO(无效的I/O)。

之后,如果在regmap_config中设置了writeable_reg回调,那么将调用该回调。如果该回调返回 "true",则进行进一步操作;如果它返回 "false",则返回错误-EIO。这个步骤只有在设置了writeable_reg的情况下才会执行。

如果没有设置writeable_reg,但是设置了wr_table,那么会检查寄存器地址是否位于no_ranges,在这种情况下会返回-EIO错误;否则,会检查它是否位于yes_ranges。如果它不在那里,那么就会返回-EIO错误,并且操作被终止。如果它在yes_ranges中,那么将执行进一步的操作。这一步只有在wr_table被设置的情况下才会执行,否则就会跳过这一步。

现在检查是否允许缓存。如果允许,那么寄存器的值将被缓存,而不是直接写入硬件,并且操作在这一步完成。如果不允许缓存,则进入下一个步骤。缓存将在后面讨论。

在调用这个硬件写入例程将值写入硬件寄存器后,这个函数将写入_flag_mask到值的第一个字节,值被写入设备。
完成写操作后,写之前的锁被释放,函数返回。

regmap_read。这个函数用于从设备上读取数据。它接收初始化期间返回的regmap结构,并注册要读取数据的地址和值指针。在寄存器读取过程中,将执行以下步骤。
首先,读函数在执行读操作之前会被锁定。如果在regmap_config中设置了fast_io,这将是一个自旋锁;否则,regmap将使用mutex。
接下来,它将检查传递的寄存器地址是否小于max_register;如果不是,那么将返回-EIO。这一步只在max_register被设置为大于0时进行。
然后,它将检查readable_reg回调是否被设置。如果是,则调用该回调,如果该回调返回'false',则读取操作被终止,返回-EIO错误。如果这个回调返回 "true",那么进一步的操作将被执行。

接下来要检查的是寄存器地址是否在配置中rd_table的no_ranges范围内。如果是,那么将返回一个-EIO错误。如果它既不在no_ranges也不在yes_ranges中,那么也会返回-EIO错误。只有当它位于yes_ranges中时,才能进行进一步的操作。这个步骤只有在设置了rd_table的情况下才会执行。
现在,如果允许缓存,那么就从缓存中读取寄存器的值,函数返回被读取的值。如果缓存被设置为旁路,那么将执行下一步。
在采取上述步骤后,调用硬件读取操作来读取寄存器的值,并且用返回的值来更新被传递的变量的值。
在开始这个操作之前的锁现在被释放,函数返回。

编写一个基于regmap的驱动
让我们试着写一个基于regmap框架的驱动程序。
让我们假设有一个设备X连接在SPI总线上,它有以下属性。

  • 8位寄存器地址
  • 8位寄存器值
  • 0x80作为写入掩码
  • 它是一个快速I/O设备,所以应该使用自旋锁
  • 有效的地址范围。
    1. 0x20到0x4F
    2. 0x60到0x7F
    驱动程序可以按以下方式编写。
  1.  
    //include other include files
  2.  
    #include <linux/regmap.h>
  3.  
     
  4.  
    static struct custom_drv_private_struct
  5.  
    {
  6.  
    //other fields relevant to device
  7.  
    struct regmap *map;
  8.  
    };
  9.  
     
  10.  
    static const struct regmap_range wr_rd_range[] =
  11.  
    {
  12.  
    {
  13.  
    .range_min = 0x20,
  14.  
    .range_max = 0x4F,
  15.  
    }, {
  16.  
    .range_min = 0x60,
  17.  
    .range_max = 0x7F
  18.  
    },
  19.  
    };
  20.  
    struct regmap_access_table drv_wr_table =
  21.  
    {
  22.  
    .yes_ranges = wr_rd_range,
  23.  
    .n_yes_ranges = ARRAY_SIZE(wr_rd_range),
  24.  
    };
  25.  
     
  26.  
    struct regmap_access_table drv_rd_table =
  27.  
    {
  28.  
    .yes_ranges = wr_rd_range,
  29.  
    .n_yes_ranges = ARRAY_SIZE(wr_rd_range),
  30.  
    };
  31.  
     
  32.  
    static bool writeable_reg(struct device *dev, unsigned int reg)
  33.  
    {
  34.  
    if(reg >= 0x20 && reg <= 0x4F)
  35.  
    return true;
  36.  
     
  37.  
    if(reg >= 0x60 && reg <= 0x7F)
  38.  
    return true;
  39.  
     
  40.  
    return false;
  41.  
    }
  42.  
     
  43.  
    static bool readable_reg(struct device *dev, unsigned int reg)
  44.  
    {
  45.  
    if(reg >= 0x20 && reg <= 0x4F)
  46.  
    return true;
  47.  
     
  48.  
    if(reg >= 0x60 && reg <= 0x7F)
  49.  
    return true;
  50.  
     
  51.  
    return false;
  52.  
    }
  53.  
    static int custom_drv_probe(struct spi_device *dev)
  54.  
    {
  55.  
    struct regmap_config config;
  56.  
    struct custom_drv_private_struct *priv;
  57.  
    unsigned int data;
  58.  
    //configure the regmap configuration
  59.  
    memset(&config, 0, sizeof(config));
  60.  
    config.reg_bits = 8;
  61.  
    config.val_bits = 8;
  62.  
    config.write_flag_mask = 0x80;
  63.  
    config.max_register = 0x80;
  64.  
    config.fast_io = true;
  65.  
    config.writeable_reg = drv_writeable_reg;
  66.  
    config.readable_reg = drv_readable_reg;
  67.  
    //only set below two things if
  68.  
    //writeable_reg
  69.  
    //and readable_reg is not set
  70.  
    //config.wr_table = drv_wr_table;
  71.  
    //config.rd_table = drv_rd_table;
  72.  
     
  73.  
    //allocate the private data structures as
  74.  
    //priv = devm_kzalloc
  75.  
    //Init the regmap spi configuration
  76.  
    priv->map = devm_regmap_init_spi(dev, &config);
  77.  
    //devm_regmap_init_i2c in case of i2c bus
  78.  
    //following operation will remain same in
  79.  
    //case of both i2c and spi or other bus
  80.  
    //read from the device, data variable will //contain device data
  81.  
    regmap_read(priv->map, 0x23, &data);
  82.  
    data = 0x24;
  83.  
    //write to the device
  84.  
    regmap_write(priv->map, 0x23, data);
  85.  
    if(regmap_read(priv->map, 0x85, &data)
  86.  
    < 0)
  87.  
    {
  88.  
    ///error since address is out of range
  89.  
    }
  90.  
    return 0;
  91.  
    }
学新通

在上面的例子中,你可以看到基于不同总线子系统的驱动程序中的冗余代码如何变成类似的代码,这使得驱动程序的编写和维护更加容易。在当前的regmap子系统中也有缓存支持。

缓存避免了直接在设备上执行操作。相反,它缓存了在设备和驱动之间传输的值,并将其作为未来的参考。最初,缓存只使用平面数组,这对32位地址不利。后来,这个问题通过更好的缓存类型得到了解决。

  • rbtree将连续的寄存器块存储在一个红/黑树中
  • 压缩存储压缩的数据块
    两者都依赖于现有的内核库。

enum regcache_type cache_type。

对tracepoint的支持在regmap中可用。更多信息,见debugfs/trace/events/regmap。

regmap_reg_write 0-001b reg=3b val=1a

regmap_reg_read 0-001b reg=1d val=1d

你也可以定义LOG_DEVICE用于早期启动日志

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgjbaji
系列文章
更多 icon
同类精品
更多 icon
继续加载