当前位置:   article > 正文

Linux I2C(二) - I2C软硬件架构

Linux I2C(二) - I2C软硬件架构

1,I2C的总线拓扑

2,I2C S/W topology

linux kernel I2C framework使用如下的软件拓扑抽象I2C硬件(我们可以一起领会一下其中的“设备模型”思想):

1)platform bus(/sys/bus/platform)是驱动工程师常见的bus,用于挂载和CPU通过系统总线连接的各类外设。在I2C framework中,I2C控制器直接从属于platform bus,我们在linux kernel中常说的I2C driver,都是指I2C controller driver,都是以platform driver的形式存在,当然,对应的控制器是platform device。

2)与此同时,kernel抽象出I2C bus(/sys/bus/i2c),用于挂载和I2C controller通过I2C总线连接的各个I2C slave device。

3)比较特殊的地方是,I2C core使用一个虚拟实体----I2C adapter,抽象I2C controller有关的功能(主要是数据的收发),I2C adapter也挂载在I2C bus上。

4)I2C adapter和I2C slave device都挂载在I2C bus上,就可以方便的进行Master(I2C adapter)和Slave之间的匹配操作,并通过I2C core提供的统一接口,访问I2C salve device,进行数据的收发。

5)以上各实体在sysfs中的位置,已经在“图片2”中通过红色字体标注,大家可自行理解。

3,软件框架

1)I2C framework的最终目标,是提供一种“访问I2C slave devices”的方法。由于这些slave devices由I2C controller控制,因而主要由I2C controller驱动实现这一目标。

2)经过I2C framework的抽象,consumer可以不用关心I2C总线的技术细节,只需要通过简单的API,就可以与slave devices进行数据交互。正常情况下,consumer是位于内核态的其它driver(如HDMI driver、touch screen driver等等)。与此同时,I2C framework也通过字符设备向用户空间提供类似的接口,用户空间程序可以通过该接口访问slave devices。

3)在I2C framework内部,有I2C core、I2C busses、I2C algos和I2C muxes四个模块。

4)I2C core使用I2C adapter和I2C algorithm两个子模块抽象I2C controller的功能,使用I2C client和I2C driver抽象I2C slave device的功能(对应设备模型中的device和device driver)。另外,基于I2C协议,通过smbus模块实现SMBus(System Management Bus,系统管理总线)的功能。

5)I2C busses是各个I2C controller drivers的集合,位于drivers/i2c/busses/目录下,驱动工程师常说的“I2C driver”就是指它们。

6)I2C algos包含了一些通用的I2C algorithm,所谓的algorithm,是指I2C协议包的生成方法,进而组合成I2C的read/write指令,一般情况下,都是由硬件实现,不需要特别关注该目录。

7)I2C muxes用于实现I2C bus的多路复用功能,属于奇葩的冷门功能,暂不过多介绍。

4,源码目录结构

在Linux内核源代码中的drivers目录下有一个i2c目录,而在i2c目录下又包含如下文件和文件夹。

(1)i2c-core.c

    这个文件实现了i2c核心的功能以及/proc/bus/i2c*接口。

(2)i2c-dev.c

    实现了i2c适配器设备文件的功能,每一个i2c适配器都被分配一个设备。通过适配器访问设备时的主设备号都为89,次设备号为0 ~ 255。应用程序通过“i2c-%d”(i2c-0,i2c-1,... ,i2c-10,...)文件名并使用文件操作接口open(),write(),read(), ioctl()和close()等来访问这个设备。

    i2c-dev.c并不是针对特定的设备而设计的,只是提供了通用的read()、write()和ioctl()等接口,应用层可以借用这些接访问挂接在适配器上的i2c设备的存储空间或寄存器,并控制i2c设备的工作方式。

(3)busses文件夹

    这个文件夹包含了一些i2c主机控制器驱动,如i2c-tegra.c、i2c-omap.c、i2c-versatile.c、i2c-s3c2410.c等。

(4)algos文件夹

    实现了一些i2c总线适配器的通信方法。

5,i2c_adapter/i2c_algorithm/i2c_driver/i2c_client结构体之间的关系

    内核中的i2c.h头文件中对i2c_adapter、i2c_algorithm、i2c_driver和i2c_client这4个数据结构进行了定义。他们的定义位于include/linux/i2c.h文件中。

i2c_adapter结构体:

  1. /*
  2. * i2c_adapter is the structure used to identify a physical i2c bus along
  3. * with the access algorithms necessary to access it.
  4. */
  5. struct i2c_adapter {
  6. struct module *owner;
  7. unsigned int class; /* classes to allow probing for */
  8. const struct i2c_algorithm *algo; /* the algorithm to access the bus */
  9. void *algo_data;
  10. /* data fields that are valid for all devices */
  11. const struct i2c_lock_operations *lock_ops;
  12. struct rt_mutex bus_lock;
  13. struct rt_mutex mux_lock;
  14. int timeout; /* in jiffies */
  15. int retries;
  16. struct device dev; /* the adapter device */
  17. unsigned long locked_flags; /* owned by the I2C core */
  18. #define I2C_ALF_IS_SUSPENDED 0
  19. #define I2C_ALF_SUSPEND_REPORTED 1
  20. int nr;
  21. char name[48];
  22. struct completion dev_released;
  23. struct mutex userspace_clients_lock;
  24. struct list_head userspace_clients;
  25. struct i2c_bus_recovery_info *bus_recovery_info;
  26. const struct i2c_adapter_quirks *quirks;
  27. struct irq_domain *host_notify_domain;
  28. };
  29. #define to_i2c_adapter(d) container_of(d, struct i2c_adapter, dev)

i2c_algorithm结构体:

  1. /**
  2. * struct i2c_algorithm - represent I2C transfer method
  3. * @master_xfer: Issue a set of i2c transactions to the given I2C adapter
  4. * defined by the msgs array, with num messages available to transfer via
  5. * the adapter specified by adap.
  6. * @master_xfer_atomic: same as @master_xfer. Yet, only using atomic context
  7. * so e.g. PMICs can be accessed very late before shutdown. Optional.
  8. * @smbus_xfer: Issue smbus transactions to the given I2C adapter. If this
  9. * is not present, then the bus layer will try and convert the SMBus calls
  10. * into I2C transfers instead.
  11. * @smbus_xfer_atomic: same as @smbus_xfer. Yet, only using atomic context
  12. * so e.g. PMICs can be accessed very late before shutdown. Optional.
  13. * @functionality: Return the flags that this algorithm/adapter pair supports
  14. * from the ``I2C_FUNC_*`` flags.
  15. * @reg_slave: Register given client to I2C slave mode of this adapter
  16. * @unreg_slave: Unregister given client from I2C slave mode of this adapter
  17. *
  18. * The following structs are for those who like to implement new bus drivers:
  19. * i2c_algorithm is the interface to a class of hardware solutions which can
  20. * be addressed using the same bus algorithms - i.e. bit-banging or the PCF8584
  21. * to name two of the most common.
  22. *
  23. * The return codes from the ``master_xfer{_atomic}`` fields should indicate the
  24. * type of error code that occurred during the transfer, as documented in the
  25. * Kernel Documentation file Documentation/i2c/fault-codes.rst.
  26. */
  27. struct i2c_algorithm {
  28. /*
  29. * If an adapter algorithm can't do I2C-level access, set master_xfer
  30. * to NULL. If an adapter algorithm can do SMBus access, set
  31. * smbus_xfer. If set to NULL, the SMBus protocol is simulated
  32. * using common I2C messages.
  33. *
  34. * master_xfer should return the number of messages successfully
  35. * processed, or a negative value on error
  36. */
  37. int (*master_xfer)(struct i2c_adapter *adap, struct i2c_msg *msgs,
  38. int num);
  39. int (*master_xfer_atomic)(struct i2c_adapter *adap,
  40. struct i2c_msg *msgs, int num);
  41. int (*smbus_xfer)(struct i2c_adapter *adap, u16 addr,
  42. unsigned short flags, char read_write,
  43. u8 command, int size, union i2c_smbus_data *data);
  44. int (*smbus_xfer_atomic)(struct i2c_adapter *adap, u16 addr,
  45. unsigned short flags, char read_write,
  46. u8 command, int size, union i2c_smbus_data *data);
  47. /* To determine what the adapter supports */
  48. u32 (*functionality)(struct i2c_adapter *adap);
  49. #if IS_ENABLED(CONFIG_I2C_SLAVE)
  50. int (*reg_slave)(struct i2c_client *client);
  51. int (*unreg_slave)(struct i2c_client *client);
  52. #endif
  53. };

i2c_driver结构体:

  1. /**
  2. * struct i2c_driver - represent an I2C device driver
  3. * @class: What kind of i2c device we instantiate (for detect)
  4. * @probe: Callback for device binding - soon to be deprecated
  5. * @probe_new: New callback for device binding
  6. * @remove: Callback for device unbinding
  7. * @shutdown: Callback for device shutdown
  8. * @alert: Alert callback, for example for the SMBus alert protocol
  9. * @command: Callback for bus-wide signaling (optional)
  10. * @driver: Device driver model driver
  11. * @id_table: List of I2C devices supported by this driver
  12. * @detect: Callback for device detection
  13. * @address_list: The I2C addresses to probe (for detect)
  14. * @clients: List of detected clients we created (for i2c-core use only)
  15. *
  16. * The driver.owner field should be set to the module owner of this driver.
  17. * The driver.name field should be set to the name of this driver.
  18. *
  19. * For automatic device detection, both @detect and @address_list must
  20. * be defined. @class should also be set, otherwise only devices forced
  21. * with module parameters will be created. The detect function must
  22. * fill at least the name field of the i2c_board_info structure it is
  23. * handed upon successful detection, and possibly also the flags field.
  24. *
  25. * If @detect is missing, the driver will still work fine for enumerated
  26. * devices. Detected devices simply won't be supported. This is expected
  27. * for the many I2C/SMBus devices which can't be detected reliably, and
  28. * the ones which can always be enumerated in practice.
  29. *
  30. * The i2c_client structure which is handed to the @detect callback is
  31. * not a real i2c_client. It is initialized just enough so that you can
  32. * call i2c_smbus_read_byte_data and friends on it. Don't do anything
  33. * else with it. In particular, calling dev_dbg and friends on it is
  34. * not allowed.
  35. */
  36. struct i2c_driver {
  37. unsigned int class;
  38. /* Standard driver model interfaces */
  39. int (*probe)(struct i2c_client *client, const struct i2c_device_id *id);
  40. int (*remove)(struct i2c_client *client);
  41. /* New driver model interface to aid the seamless removal of the
  42. * current probe()'s, more commonly unused than used second parameter.
  43. */
  44. int (*probe_new)(struct i2c_client *client);
  45. /* driver model interfaces that don't relate to enumeration */
  46. void (*shutdown)(struct i2c_client *client);
  47. /* Alert callback, for example for the SMBus alert protocol.
  48. * The format and meaning of the data value depends on the protocol.
  49. * For the SMBus alert protocol, there is a single bit of data passed
  50. * as the alert response's low bit ("event flag").
  51. * For the SMBus Host Notify protocol, the data corresponds to the
  52. * 16-bit payload data reported by the slave device acting as master.
  53. */
  54. void (*alert)(struct i2c_client *client, enum i2c_alert_protocol protocol,
  55. unsigned int data);
  56. /* a ioctl like command that can be used to perform specific functions
  57. * with the device.
  58. */
  59. int (*command)(struct i2c_client *client, unsigned int cmd, void *arg);
  60. struct device_driver driver;
  61. const struct i2c_device_id *id_table;
  62. /* Device detection callback for automatic device creation */
  63. int (*detect)(struct i2c_client *client, struct i2c_board_info *info);
  64. const unsigned short *address_list;
  65. struct list_head clients;
  66. };
  67. #define to_i2c_driver(d) container_of(d, struct i2c_driver, driver)

i2c_client结构体:

  1. /**
  2. * struct i2c_client - represent an I2C slave device
  3. * @flags: see I2C_CLIENT_* for possible flags
  4. * @addr: Address used on the I2C bus connected to the parent adapter.
  5. * @name: Indicates the type of the device, usually a chip name that's
  6. * generic enough to hide second-sourcing and compatible revisions.
  7. * @adapter: manages the bus segment hosting this I2C device
  8. * @dev: Driver model device node for the slave.
  9. * @init_irq: IRQ that was set at initialization
  10. * @irq: indicates the IRQ generated by this device (if any)
  11. * @detected: member of an i2c_driver.clients list or i2c-core's
  12. * userspace_devices list
  13. * @slave_cb: Callback when I2C slave mode of an adapter is used. The adapter
  14. * calls it to pass on slave events to the slave driver.
  15. *
  16. * An i2c_client identifies a single device (i.e. chip) connected to an
  17. * i2c bus. The behaviour exposed to Linux is defined by the driver
  18. * managing the device.
  19. */
  20. struct i2c_client {
  21. unsigned short flags; /* div., see below */
  22. #define I2C_CLIENT_PEC 0x04 /* Use Packet Error Checking */
  23. #define I2C_CLIENT_TEN 0x10 /* we have a ten bit chip address */
  24. /* Must equal I2C_M_TEN below */
  25. #define I2C_CLIENT_SLAVE 0x20 /* we are the slave */
  26. #define I2C_CLIENT_HOST_NOTIFY 0x40 /* We want to use I2C host notify */
  27. #define I2C_CLIENT_WAKE 0x80 /* for board_info; true iff can wake */
  28. #define I2C_CLIENT_SCCB 0x9000 /* Use Omnivision SCCB protocol */
  29. /* Must match I2C_M_STOP|IGNORE_NAK */
  30. unsigned short addr; /* chip address - NOTE: 7bit */
  31. /* addresses are stored in the */
  32. /* _LOWER_ 7 bits */
  33. char name[I2C_NAME_SIZE];
  34. struct i2c_adapter *adapter; /* the adapter we sit on */
  35. struct device dev; /* the device structure */
  36. int init_irq; /* irq set at initialization */
  37. int irq; /* irq issued by device */
  38. struct list_head detected;
  39. #if IS_ENABLED(CONFIG_I2C_SLAVE)
  40. i2c_slave_cb_t slave_cb; /* callback for slave mode */
  41. #endif
  42. };
  43. #define to_i2c_client(d) container_of(d, struct i2c_client, dev)

下面分析i2c_adapter、i2c_algorithm、i2c_driver、i2c_client这4个数据结构的作用及其关系。

(1)i2c_adapter 与 i2c_algorithm

    i2c_adapter对应于物理上的一个适配器,而i2c_algorithm对应一套通信方法。一个i2c适配器需要i2c_algorithm提供的通信函数来控制适配器产生特定的访问周期。缺少i2c_algorithm的i2c_adapter什么也做不了,因此i2c_adapter中包含所使用的i2c_algorithm的指针。

    i2c_algorithm中的关键函数是master_xfer()用于产生i2c访问周期需要的信号,以i2c_msg(即i2c消息)为单位。i2c_msg结构体也非常重要,它定义于include/uapi/linux/i2c.h(在uapi目录下,证明用户空间也可能使用这个结构体)中该结构体的成员表明了i2c的传输地址、方向、缓冲区、缓冲区长度等信息。

i2c_msg结构体的定义:

  1. /**
  2. * struct i2c_msg - an I2C transaction segment beginning with START
  3. * @addr: Slave address, either seven or ten bits. When this is a ten
  4. * bit address, I2C_M_TEN must be set in @flags and the adapter
  5. * must support I2C_FUNC_10BIT_ADDR.
  6. * @flags: I2C_M_RD is handled by all adapters. No other flags may be
  7. * provided unless the adapter exported the relevant I2C_FUNC_*
  8. * flags through i2c_check_functionality().
  9. * @len: Number of data bytes in @buf being read from or written to the
  10. * I2C slave address. For read transactions where I2C_M_RECV_LEN
  11. * is set, the caller guarantees that this buffer can hold up to
  12. * 32 bytes in addition to the initial length byte sent by the
  13. * slave (plus, if used, the SMBus PEC); and this value will be
  14. * incremented by the number of block data bytes received.
  15. * @buf: The buffer into which data is read, or from which it's written.
  16. *
  17. * An i2c_msg is the low level representation of one segment of an I2C
  18. * transaction. It is visible to drivers in the @i2c_transfer() procedure,
  19. * to userspace from i2c-dev, and to I2C adapter drivers through the
  20. * @i2c_adapter.@master_xfer() method.
  21. *
  22. * Except when I2C "protocol mangling" is used, all I2C adapters implement
  23. * the standard rules for I2C transactions. Each transaction begins with a
  24. * START. That is followed by the slave address, and a bit encoding read
  25. * versus write. Then follow all the data bytes, possibly including a byte
  26. * with SMBus PEC. The transfer terminates with a NAK, or when all those
  27. * bytes have been transferred and ACKed. If this is the last message in a
  28. * group, it is followed by a STOP. Otherwise it is followed by the next
  29. * @i2c_msg transaction segment, beginning with a (repeated) START.
  30. *
  31. * Alternatively, when the adapter supports I2C_FUNC_PROTOCOL_MANGLING then
  32. * passing certain @flags may have changed those standard protocol behaviors.
  33. * Those flags are only for use with broken/nonconforming slaves, and with
  34. * adapters which are known to support the specific mangling options they
  35. * need (one or more of IGNORE_NAK, NO_RD_ACK, NOSTART, and REV_DIR_ADDR).
  36. */
  37. struct i2c_msg {
  38. __u16 addr; /* slave address */
  39. __u16 flags;
  40. #define I2C_M_RD 0x0001 /* read data, from slave to master */
  41. /* I2C_M_RD is guaranteed to be 0x0001! */
  42. #define I2C_M_TEN 0x0010 /* this is a ten bit chip address */
  43. #define I2C_M_DMA_SAFE 0x0200 /* the buffer of this message is DMA safe */
  44. /* makes only sense in kernelspace */
  45. /* userspace buffers are copied anyway */
  46. #define I2C_M_RECV_LEN 0x0400 /* length will be first received byte */
  47. #define I2C_M_NO_RD_ACK 0x0800 /* if I2C_FUNC_PROTOCOL_MANGLING */
  48. #define I2C_M_IGNORE_NAK 0x1000 /* if I2C_FUNC_PROTOCOL_MANGLING */
  49. #define I2C_M_REV_DIR_ADDR 0x2000 /* if I2C_FUNC_PROTOCOL_MANGLING */
  50. #define I2C_M_NOSTART 0x4000 /* if I2C_FUNC_NOSTART */
  51. #define I2C_M_STOP 0x8000 /* if I2C_FUNC_PROTOCOL_MANGLING */
  52. __u16 len; /* msg length */
  53. __u8 *buf; /* pointer to msg data */
  54. };

(2)i2c_driver与i2c_client

    i2c_driver对应于一套驱动方法,其主要成员函数是probe()、remove()、suspend()、resume()等,另外,struct i2c_device_id形式的id_table是该驱动所支持的i2c设备的ID表。i2c_client对应于真实的物理设备,每个i2c设备都需要一个i2c_client来描述。i2c_driver与i2c_client的关系是一对多,一个i2c_driver可以支持多个同类型的i2c_client。

(3)i2c_adapter 与 i2c_client

    i2c_adapter 与 i2c_client的关系与i2c硬件体系中适配器和设备的关系一致,即i2c_client依附于i2c_adapter。由于一个适配器可以连接多个i2c设备,所以一个i2c_adapter也可以被多个i2c_client依附,i2c_adapter中包括依附于它的i2c_client的链表。

I2C驱动的各种数据结构的关系:

6,编写I2C驱动需要完成的工作

    一方面,适配器驱动可能是Linux内核还不包含的;另一方面,挂接在适配器上的具体的设备驱动可能也是Linux内核还不包含的。因此,工程师需要实现的主要工作如下:

(1)提供I2C适配器的硬件驱动,探测、初始化I2C适配器(如申请I2C的I/O地址和中断号)、驱动CPU控制的I2C适配器从硬件上产生各种信号以及处理I2C中断等。

(2)提供I2C适配器的algorithm,用具体适配器的xxx_xfer()函数填充i2c_algorithm的master_xfer指针,并把i2c_algorithm指针赋值给i2c_adapter的algo指针。

(3)实现I2C设备驱动中的i2c_driver接口,用具体设备yyy的yyy_probe()、yyy_remove()、yyy_suspend()、yyy_resume()函数指针和i2c_device_id设备ID表赋值给i2c_driver的probe、remove、suspend、resume和id_table指针。

(4)实现i2c设备所对应类型的具体驱动,i2c_driver只是实现设备与总线的挂接,而挂接在总线上的设备则千差万别。例如,如果是字符设备,就实现文件操作接口,即实现具体设备的yyy_read()、yyy_write()和yyy_ioctl()函数等;如果是声卡,就实现ALSA驱动。

上述前两个属于I2C总线驱动,后两个属于I2C设备驱动。

参考链接:

Linux I2C framework(1)_概述

<<Linux设备驱动开发详解>>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/472860
推荐阅读
相关标签
  

闽ICP备14008679号