编写 Linux 标准 PWM 驱动


编写Linux标准的PWM驱动,需要定义一个结构体 struct pwm_device ,实现五个个PWM函数(include/linux/pwm.h),如下所示:

  1. struct pwm_device;  
  2.   
  3. /* 
  4.  * pwm_request - request a PWM device 
  5.  */  
  6. struct pwm_device *pwm_request(int pwm_id, const char *label);  
  7.   
  8. /* 
  9.  * pwm_free - free a PWM device 
  10.  */  
  11. void pwm_free(struct pwm_device *pwm);  
  12.   
  13. /* 
  14.  * pwm_config - change a PWM device configuration 
  15.  */  
  16. int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns);  
  17.   
  18. /* 
  19.  * pwm_enable - start a PWM output toggling 
  20.  */  
  21. int pwm_enable(struct pwm_device *pwm);  
  22.   
  23. /* 
  24.  * pwm_disable - stop a PWM output toggling 
  25.  */  
  26. void pwm_disable(struct pwm_device *pwm);  

其中, struct pwm_device 必须包含以下的内容:

  1. struct pwm_device {  
  2.     struct list_head     list;  
  3.     struct platform_device  *pdev;  
  4.   
  5.     const char      *label;  
  6.   
  7.     unsigned int         period_ns;  
  8.     unsigned int         duty_ns;  
  9.   
  10.     unsigned char        running;  
  11.     unsigned char        use_count;  
  12.     unsigned char        pwm_id;  
  13. };  

代码的实现可以参考下面的文件:

arch/arm/plat-s3c/pwm.c


总结(以arm为例):

 1. 编写文件 arch/arm/mach-xxxx/pwm.c 

2. 修改 arch/arm/mach-xxxx/Makefile ,在最后一行加入下面的内容

obj-y    += pwm.c

3. 修改arch/arm/Kconfig,找到 config ARCH_XXXX ,在下面添加

select HAVE_PWM

相关内容