platform总线驱动学习

platform总线工作过程:

A-- platform bus总线先被kenrel注册。

B -- 系统初始化过程中调用platform_add_devices或者platform_device_register,将平台设备(platform devices)注册到平台总线中(platform bus)

     int platform_add_devices(struct platform_device **devs, int num);

该函数的第一个参数为平台设备数组的指针,第二个参数为平台设备的数量,它内部调用了platform_device_register()函 数用于注册单个的平台设备。

    int platform_device_register(struct platform_device *pdev)

注册单个platform设备

 

C -- 平台驱动(platform driver)与平台设备(platform device)的关联是在platform_driver_register或者driver_register中实现,一般这个函数在驱动的初始化过程调用。

      通过这三步,就将平台总线,设备,驱动关联起来。

所谓的platform_device并不是与字符设备、块设备和网络设备并列的概念,而是Linux系统提供的一种附加手段,例如,在 S3C6410处理器中,把内部集成的I2C、RTC、SPI、LCD、看门狗等控制器都归纳为platform_device,而它们本身就是字符设备。 

           platform 驱动只是在字符设备驱动外套一层platform_driver 的外壳。

          引入platform模型符合Linux 设备模型 —— 总线、设备、驱动,设备模型中配套的sysfs节点都可以用,方便我们的开发;当然你也可以选择不用,不过就失去了一些platform带来的便利;       设备驱动中引入platform 概念,隔离BSP和驱动。在BSP中定义platform设备和设备使用的资源、设备的具体匹配信息,而在驱动中,只需要通过API去获取资源和数据,做到了板相关代码和驱动代码的分离,使得驱动具有更好的可扩展性和跨平台性。


  1. #include 
    #include 
    #include 
    #include 
     
    static struct resource beep_resource[] =
    {
    	[0] ={
    		.start = 0x7F0080A0,
    		.end =  0x7F0080A0 + 0x4,
    		.flags = IORESOURCE_MEM,
    	},
     
    	[1] ={
    		.start = 0x7F006000,
    		.end =  0x7F006000 + 0x4,
    		.flags = IORESOURCE_MEM,
    	}
    };
     
    static void hello_release(struct device *dev)
    {
    	printk("hello_release\n");
    	return ;
    }
     
     
     
    static struct platform_device hello_device=
    {
        .name = "bigbang",
        .id = -1,
        .dev.release = hello_release,
        .num_resources = ARRAY_SIZE(beep_resource),
        .resource = beep_resource,
    };
     
    static int hello_init(void)
    {
    	printk("hello_init");
    	return platform_device_register(&hello_device);
    }
     
    static void hello_exit(void)
    {
    	printk("hello_exit");
    	platform_device_unregister(&hello_device);
    	return;
    }
     
    MODULE_LICENSE("GPL");
    module_init(hello_init);
    module_exit(hello_exit);
    
    
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    #include 
    #include 
    #include 
    
    #include 
    #include 
    #include 
    #include 
    
    
    
    
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    
    
    
    
    #define DEVICE_NAME     "beep_pwm"
    
    #define PWM_IOCTL_SET_FREQ      1
    #define PWM_IOCTL_STOP          0
    
    static struct semaphore lock; //信号量
    
    
    static int major = 250;
    static int minor=0;
    static dev_t devno;
    static struct class *cls;
    static struct device *test_device;
             
            
     
    static void		 *Define_S3C64XX_GPFCON;
    
    static void 		*Define_S3C_TCFG0;
    static void 		*Define_S3C_TCFG1;
    static void 		*Define_S3C_TCON;
    static void 		*Define_S3C_TCNTB0;
    static void 		*Define_S3C_TCMPB0;
    
    
    /* freq:  pclk/50/16/65536 ~ pclk/50/16
      * if pclk = 50MHz, freq is 1Hz to 62500Hz
      * human ear : 20Hz~ 20000Hz
      */
    static void PWM_Set_Freq( unsigned long freq )
    {
        unsigned long tcon;
        unsigned long tcnt;
        unsigned long tcfg1;
        unsigned long tcfg0;
        struct clk *clk_p;
        unsigned long pclk;
        unsigned tmp;
        tmp = readl(Define_S3C64XX_GPFCON);
        tmp &=~(0x3U << 30);  //GPF15  [31:30]    10 = PWM TOUT[1]
        tmp |=  (0x2U << 30);
        writel(tmp, Define_S3C64XX_GPFCON);// GPF15 设置为PWM TOUT 1
        
        tcon = __raw_readl(Define_S3C_TCON);  //定时器控制寄存器
        tcfg1 = __raw_readl(Define_S3C_TCFG1); //时钟多路复用器和 DMA模式的选择
        tcfg0 = __raw_readl(Define_S3C_TCFG0); //时钟预定标器和死区结构
        //prescaler = 50   Prescaler 0 [7:0] R/W Prescaler 0 value for timer 0 & 1
        tcfg0 &= ~S3C_TCFG_PRESCALER0_MASK;  //{prescaler value} = 1~255
        tcfg0 |= (50 - 1);  //prescaler value=50
        //mux = 1/16  TCFG1  Divider MUX0 [3:0] R/W Select Mux input for PWM Timer 0
        tcfg1 &= ~S3C_TCFG1_MUX0_MASK;
        tcfg1 |= S3C_TCFG1_MUX0_DIV16; // 0100: 1/16
        __raw_writel(tcfg1, Define_S3C_TCFG1);
        __raw_writel(tcfg0, Define_S3C_TCFG0);
        /* clk_get获取一个名为id的时针
         * 输入参数dev:   可以为NULL
         * 输入参数id:    时针名称,如fclk、hclk、pclk等
         * 返回值:        返回该时钟的clk结构体
         *
         *再将clk_get返回的clk结构体传递给clk_get_rate,获取该时钟的频率
         */
        /*PCLK is used for APB bus, which is used by the peripherals
         *such as WDT, IIS, I2C, PWM timer, MMC */
        clk_p = clk_get(NULL, "pclk");  //获取一个名为id的时针
        pclk  = clk_get_rate(clk_p);  //获取该时钟的频率
        //定时器输入时钟频率
        //Timer input clock Frequency = PCLK / ( {prescaler value + 1} ) / {divider value}
        tcnt  = (pclk/50/16)/freq;
        __raw_writel(tcnt,/*S3C_TCNTB(0)*/Define_S3C_TCNTB0); //TCNTB0:定时器0计数缓冲器。
        __raw_writel(tcnt/2,/*S3C_TCMPB(0)*/Define_S3C_TCMPB0);//TCMPB0:定时器0比较缓冲寄存器。
        tcon &= ~0x1f;
        tcon |= 0xb;        //disable deadzone, auto-reload, inv-off, update TCNTB0&TCMPB0, start timer 0
        __raw_writel(tcon, Define_S3C_TCON);
        /*Note: Manual update bit must be 1’b0 before Start/Stop bit is 1’b1.
           If Manual update bit is 1’b1 and Start/Stop bit is 1’b1,
           timer counter is not update by new value.
           Timer counter value is last value. */
        tcon &= ~2;         //clear manual update bit
        __raw_writel(tcon, Define_S3C_TCON);
    }
    void PWM_Stop( void )
    {
        unsigned tmp;
        tmp = readl(Define_S3C64XX_GPFCON);
        //tmp &= ~(0x3U << 28);
        tmp &= ~(0x3U << 30);
        writel(tmp, Define_S3C64XX_GPFCON);
    }
    /* 该函数尝试获得信号量sem,如果能够立刻获得,
    它就获得该信号量并返回0,否则,返回非0值。
    它不会导致调用者睡眠,可以在中断上下文使用。
    */
    static int s3c64xx_pwm_open(struct inode *inode, struct file *file)
    {
        if (!down_trylock(&lock))
        {
            return 0;
        }
        else
        {
            return -EBUSY;
        }
    }
    static int s3c64xx_pwm_close(struct inode *inode, struct file *file)
    {
        up(&lock);  //该函数释放信号量sem,唤醒等待者
        return 0;
    }
    static long s3c64xx_pwm_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
    {
        switch (cmd)
        {
        case PWM_IOCTL_SET_FREQ:
            if (arg == 0)
            {
                return -EINVAL;
            }
            PWM_Set_Freq(arg);
            break;
        case PWM_IOCTL_STOP:
        default:
            PWM_Stop();
            break;
        }
        return 0;
    }
    static struct file_operations dev_fops =
    {
        .owner          = THIS_MODULE,
        .open           = s3c64xx_pwm_open,
        .release        = s3c64xx_pwm_close,
        .unlocked_ioctl = s3c64xx_pwm_ioctl,
    };
    
    
    static void beep_unmap(void)
    {
    	iounmap(Define_S3C64XX_GPFCON);		
    	iounmap(Define_S3C_TCFG0);
    }
    static int beep_probe(struct platform_device *pdev)
    {
    	int ret;	
    	printk("match ok!");
    	
    	Define_S3C64XX_GPFCON = ioremap(pdev->resource[0].start,pdev->resource[0].end - pdev->resource[0].start);
    	Define_S3C_TCFG0 = ioremap(pdev->resource[1].start, pdev->resource[1].end - pdev->resource[1].start);
     	Define_S3C_TCFG1=Define_S3C_TCFG0+4;
     	Define_S3C_TCON=Define_S3C_TCFG1+4;
     	Define_S3C_TCNTB0=Define_S3C_TCON+4;
     	Define_S3C_TCMPB0=Define_S3C_TCNTB0+4;
     	
    	devno = MKDEV(major,minor);
    	ret = register_chrdev(major,"beep_pwm",&dev_fops);
     
    	cls = class_create(THIS_MODULE, "myclass");
    	if(IS_ERR(cls))
    	{
    		unregister_chrdev(major,"beep_pwm");
    		return -EBUSY;
    	}
     
    	test_device = device_create(cls,NULL,devno,NULL,"beep_pwm");//mknod /dev/hello
    	if(IS_ERR(test_device))
    	{
    		class_destroy(cls);
    		unregister_chrdev(major,"beep_pwm");
    		return -EBUSY;
    	}
    
    
    	PWM_Set_Freq(1000);
    	
    	return 0;
    }
     
    static int beep_remove(struct platform_device *pdev)
    {
    	beep_unmap();
    	device_destroy(cls,devno);
    	class_destroy(cls);	
    	unregister_chrdev(major,"beep_pwm");
     
    	return 0;
    }
     
    static struct platform_driver beep_driver=
    {
        .driver.name = DEVICE_NAME,
        .probe = beep_probe,
        .remove = beep_remove,
    };
    
    static int __init dev_init(void)
    {
        int ret;
        /* 该函数用于初始化一个互斥锁,即它把信号量sem的值设置为1,
        等同于sema_init (struct semaphore *sem, 1)*/
        init_MUTEX(&lock);
        
    	ret = platform_driver_register(&beep_driver);
        
        printk (DEVICE_NAME"\tinitialized\n");
        return ret;
    }
    static void __exit dev_exit(void)
    {
    
    	platform_driver_unregister(&beep_driver);
    
    }
    module_init(dev_init);
    module_exit(dev_exit);
    MODULE_LICENSE("GPL");
    MODULE_AUTHOR("FORLINX Inc.");
    MODULE_DESCRIPTION("S3C6410 PWM Driver");
    
    #include 
    #include 
    #include 
    #include 
    #include 
    
    #define PWM_IOCTL_SET_FREQ      1
    #define PWM_IOCTL_STOP          0
    main()
    {
    	int fd;
     
    	fd = open("/dev/pwm",O_RDWR);
    	if(fd<0)
    	{
    		perror("open fail \n");
    		return ;
    	}
    
    	ioctl(fd,PWM_IOCTL_SET_FREQ,100);
    	sleep(10);
    	ioctl(fd,PWM_IOCTL_STOP);	
     
    	close(fd);
    }
    

     

你可能感兴趣的:(Linux驱动)