手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

基于S3C2440的嵌入式Linux驱动——SPI子系统解读(一)

[日期:2012-08-16] 来源:Linux社区  作者:yj4231 [字体:大 中 小]

2.2 spi_device

该结构用于描述SPI设备,也就是从设备的相关信息。

NOTE:SPI子系统只支持主模式,也就是说S3C2440的SPI只能工作在master模式,外围设备只能为slave模式。

  1. /** 
  2.  * struct spi_device - Master side proxy for an SPI slave device 
  3.  * @dev: Driver model representation of the device. 
  4.  * @master: SPI controller used with the device. 
  5.  * @max_speed_hz: Maximum clock rate to be used with this chip 
  6.  *  (on this board); may be changed by the device's driver. 
  7.  *  The spi_transfer.speed_hz can override this for each transfer. 
  8.  * @chip_select: Chipselect, distinguishing chips handled by @master. 
  9.  * @mode: The spi mode defines how data is clocked out and in. 
  10.  *  This may be changed by the device's driver. 
  11.  *  The "active low" default for chipselect mode can be overridden 
  12.  *  (by specifying SPI_CS_HIGH) as can the "MSB first" default for 
  13.  *  each word in a transfer (by specifying SPI_LSB_FIRST). 
  14.  * @bits_per_word: Data transfers involve one or more words; word sizes 
  15.  *  like eight or 12 bits are common.  In-memory wordsizes are 
  16.  *  powers of two bytes (e.g. 20 bit samples use 32 bits). 
  17.  *  This may be changed by the device's driver, or left at the 
  18.  *  default (0) indicating protocol words are eight bit bytes. 
  19.  *  The spi_transfer.bits_per_word can override this for each transfer. 
  20.  * @irq: Negative, or the number passed to request_irq() to receive 
  21.  *  interrupts from this device. 
  22.  * @controller_state: Controller's runtime state 
  23.  * @controller_data: Board-specific definitions for controller, such as 
  24.  *  FIFO initialization parameters; from board_info.controller_data 
  25.  * @modalias: Name of the driver to use with this device, or an alias 
  26.  *  for that name.  This appears in the sysfs "modalias" attribute 
  27.  *  for driver coldplugging, and in uevents used for hotplugging 
  28.  * 
  29.  * A @spi_device is used to interchange data between an SPI slave 
  30.  * (usually a discrete chip) and CPU memory. 
  31.  * 
  32.  * In @dev, the platform_data is used to hold information about this 
  33.  * device that's meaningful to the device's protocol driver, but not 
  34.  * to its controller.  One example might be an identifier for a chip 
  35.  * variant with slightly different functionality; another might be 
  36.  * information about how this particular board wires the chip's pins. 
  37.  */  
  38. struct spi_device {  
  39.     struct device       dev;  
  40.     struct spi_master   *master;  
  41.     u32         max_speed_hz;  
  42.     u8          chip_select;  
  43.     u8          mode;  
  44. #define SPI_CPHA    0x01            /* clock phase */   
  45. #define SPI_CPOL    0x02            /* clock polarity */   
  46. #define SPI_MODE_0  (0|0)           /* (original MicroWire) */   
  47. #define SPI_MODE_1  (0|SPI_CPHA)   
  48. #define SPI_MODE_2  (SPI_CPOL|0)   
  49. #define SPI_MODE_3  (SPI_CPOL|SPI_CPHA)   
  50. #define SPI_CS_HIGH 0x04            /* chipselect active high? */   
  51. #define SPI_LSB_FIRST   0x08            /* per-word bits-on-wire */   
  52. #define SPI_3WIRE   0x10            /* SI/SO signals shared */   
  53. #define SPI_LOOP    0x20            /* loopback mode */   
  54.     u8          bits_per_word;  
  55.     int         irq;  
  56.     void            *controller_state;  
  57.     void            *controller_data;  
  58.     char            modalias[32];  
  59.   
  60.     /* 
  61.      * likely need more hooks for more protocol options affecting how 
  62.      * the controller talks to each chip, like: 
  63.      *  - memory packing (12 bit samples into low bits, others zeroed) 
  64.      *  - priority 
  65.      *  - drop chipselect after each word 
  66.      *  - chipselect delays 
  67.      *  - ... 
  68.      */  
  69. };  

2.3 spi_board_info

该结构也是对从设备的描述,只不过它是板级信息,最终该结构的所有信息将复制给spi_device。

  1. * INTERFACE between board init code and SPI infrastructure.  
  2.  *  
  3.  * No SPI driver ever sees these SPI device table segments, but  
  4.  * it's how the SPI core (or adapters that get hotplugged) grows  
  5.  * the driver model tree.  
  6.  *  
  7.  * As a rule, SPI devices can't be probed.  Instead, board init code  
  8.  * provides a table listing the devices which are present, with enough  
  9.  * information to bind and set up the device's driver.  There's basic  
  10.  * support for nonstatic configurations too; enough to handle adding  
  11.  * parport adapters, or microcontrollers acting as USB-to-SPI bridges.  
  12.  */  
  13.   
  14. /** 
  15.  * struct spi_board_info - board-specific template for a SPI device 
  16.  * @modalias: Initializes spi_device.modalias; identifies the driver. 
  17.  * @platform_data: Initializes spi_device.platform_data; the particular 
  18.  *  data stored there is driver-specific. 
  19.  * @controller_data: Initializes spi_device.controller_data; some 
  20.  *  controllers need hints about hardware setup, e.g. for DMA. 
  21.  * @irq: Initializes spi_device.irq; depends on how the board is wired. 
  22.  * @max_speed_hz: Initializes spi_device.max_speed_hz; based on limits 
  23.  *  from the chip datasheet and board-specific signal quality issues. 
  24.  * @bus_num: Identifies which spi_master parents the spi_device; unused 
  25.  *  by spi_new_device(), and otherwise depends on board wiring. 
  26.  * @chip_select: Initializes spi_device.chip_select; depends on how 
  27.  *  the board is wired. 
  28.  * @mode: Initializes spi_device.mode; based on the chip datasheet, board 
  29.  *  wiring (some devices support both 3WIRE and standard modes), and 
  30.  *  possibly presence of an inverter in the chipselect path. 
  31.  * 
  32.  * When adding new SPI devices to the device tree, these structures serve 
  33.  * as a partial device template.  They hold information which can't always 
  34.  * be determined by drivers.  Information that probe() can establish (such 
  35.  * as the default transfer wordsize) is not included here. 
  36.  * 
  37.  * These structures are used in two places.  Their primary role is to 
  38.  * be stored in tables of board-specific device descriptors, which are 
  39.  * declared early in board initialization and then used (much later) to 
  40.  * populate a controller's device tree after the that controller's driver 
  41.  * initializes.  A secondary (and atypical) role is as a parameter to 
  42.  * spi_new_device() call, which happens after those controller drivers 
  43.  * are active in some dynamic board configuration models. 
  44.  */  
  45. struct spi_board_info {  
  46.     /* the device name and module name are coupled, like platform_bus; 
  47.      * "modalias" is normally the driver name. 
  48.      * 
  49.      * platform_data goes to spi_device.dev.platform_data, 
  50.      * controller_data goes to spi_device.controller_data, 
  51.      * irq is copied too 
  52.      */  
  53.     char        modalias[32];  
  54.     const void  *platform_data;  
  55.     void        *controller_data;  
  56.     int     irq;  
  57.   
  58.     /* slower signaling on noisy or low voltage boards */  
  59.     u32     max_speed_hz;  
  60.   
  61.   
  62.     /* bus_num is board specific and matches the bus_num of some 
  63.      * spi_master that will probably be registered later. 
  64.      * 
  65.      * chip_select reflects how this chip is wired to that master; 
  66.      * it's less than num_chipselect. 
  67.      */  
  68.     u16     bus_num;  
  69.     u16     chip_select;  
  70.   
  71.     /* mode becomes spi_device.mode, and is essential for chips 
  72.      * where the default of SPI_CS_HIGH = 0 is wrong. 
  73.      */  
  74.     u8      mode;  
  75.   
  76.     /* ... may need additional spi_device chip config data here. 
  77.      * avoid stuff protocol drivers can set; but include stuff 
  78.      * needed to behave without being bound to a driver: 
  79.      *  - quirks like clock rate mattering when not selected 
  80.      */  
  81. };  

2.4 spi_driver

该结构用于描述SPI设备驱动。驱动核心将根据driver.name和spi_board_info 的modalias进行匹配,如过modalia和name相等,则绑定驱动程序和SPI设备。  

  1. /** 
  2.  * struct spi_driver - Host side "protocol" driver 
  3.  * @probe: Binds this driver to the spi device.  Drivers can verify 
  4.  *  that the device is actually present, and may need to configure 
  5.  *  characteristics (such as bits_per_word) which weren't needed for 
  6.  *  the initial configuration done during system setup. 
  7.  * @remove: Unbinds this driver from the spi device 
  8.  * @shutdown: Standard shutdown callback used during system state 
  9.  *  transitions such as powerdown/halt and kexec 
  10.  * @suspend: Standard suspend callback used during system state transitions 
  11.  * @resume: Standard resume callback used during system state transitions 
  12.  * @driver: SPI device drivers should initialize the name and owner 
  13.  *  field of this structure. 
  14.  * 
  15.  * This represents the kind of device driver that uses SPI messages to 
  16.  * interact with the hardware at the other end of a SPI link.  It's called 
  17.  * a "protocol" driver because it works through messages rather than talking 
  18.  * directly to SPI hardware (which is what the underlying SPI controller 
  19.  * driver does to pass those messages).  These protocols are defined in the 
  20.  * specification for the device(s) supported by the driver. 
  21.  * 
  22.  * As a rule, those device protocols represent the lowest level interface 
  23.  * supported by a driver, and it will support upper level interfaces too. 
  24.  * Examples of such upper levels include frameworks like MTD, networking, 
  25.  * MMC, RTC, filesystem character device nodes, and hardware monitoring. 
  26.  */  
  27. struct spi_driver {  
  28.     int         (*probe)(struct spi_device *spi);  
  29.     int         (*remove)(struct spi_device *spi);  
  30.     void            (*shutdown)(struct spi_device *spi);  
  31.     int         (*suspend)(struct spi_device *spi, pm_message_t mesg);  
  32.     int         (*resume)(struct spi_device *spi);  
  33.     struct device_driver     driver;  
  34. };  
linux
相关资讯       嵌入式Linux  S3C2440  SPI 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款