Linux driven Platform Driver Model summary (based on tiny210 platform LED driver)

Source: Internet
Author: User
Tags volatile

1. Overview

in general, the 2.6 kernel has been initialized and mounted with a platform bus in the Sysfs file system. So when we write the platform model driver, we need to do two jobs:1: Implement platform Drive 2: Implement PLATForm Device, however There are many other small tasks that need to be implemented in the process of implementing these two tasks, as described later. The core architecture of the platform model-driven implementation process is simple, as shown below.

Platform Drive model three objects: Platform bus, platform device, platform driver. platform Bus-corresponding kernel structure: struct bus_type--> It contains the most critical functions: match () Platform Device -corresponding kernel structure: struct Platform_device--registration:platform_device_register ( Unregiste) Platform Driver -corresponding kernel structure: struct platform_driver--Registration: Platform_driver_register (unregiste)

A brief introduction to the working process of the platform driver: when the device (or driver) registers, it will trigger the bus to call its own match function to findcurrentlyplatform whether the bus is mounted with thedevice (or drive) name-matching driver (or device) (http://blog.csdn.net/xy010902100449/article/details/45700523 We talked about the platform device-driven match auto-matching process), If there is a binding between the two parties, if the device is registered, and the driver is not registered, then the device will not match the driver with the same name when it is registered on the bus, and then when the driver is registered to the bus, the bus will immediately match the device and driver with the same name as the binding. re-call the probe function in the driver, and so on , if the driver is registered first, the same as the device driver will match the failure, the matching failure will cause its probe function is not called, but to wait until the device registration succeeds and bind itself to be called.

2. Code

/* * author:zp1015 * * Copyright scut. * * Platform Device for LED * * #include <linux/init.h> #include <linux/module.h> #include <linux/fs.h> #i Nclude <asm/io.h> #include <asm/uaccess.h> #include <linux/device.h> #include <linux/cdev.h># Include <linux/platform_device.h>static void myled_platform_device_release (struct device * dev) {return;} static struct resource myled_resource[] = {[0] = {. Start = 0xe0200280,. End = 0xe         0200280 +,. Flags = ioresource_mem},};static struct Platform_device myledplatform_device_led = { . Name = "Myled_platform_device_driver",. id =-1,. num_resources = Array_size (Myled_resource),. Resource = Myled_resource,. Dev = {. Release = MYLED_PLATF Orm_device_release,},};static int __init myled_platform_device_init (void) {PRINTK ("Myled_platform_deviceAdd ok!\n "); return Platform_device_register (&myledplatform_device_led);} static void __exit myled_platform_device_exit (void) {PRINTK ("Myled_platform_device remove ok!\n");p Latform_device_ Unregister (&myledplatform_device_led);} Module_author ("ZP1015"); Module_license ("GPL"); Module_init (Myled_platform_device_init); Module_exit (Myled_platform_device_exit);

/* * author:zp1015 * * Copyright scut. * * Platform Driver for LED * * #include <linux/init.h> #include <linux/module.h> #include <linux/fs.h> #i Nclude <asm/io.h> #include <asm/uaccess.h> #include <linux/device.h> #include <linux/cdev.h># Include <linux/platform_device.h> #define GLOBAL_LED_MAJOR 250static unsigned int global_led_major = Global_led_ MAJOR; static struct Cdev *led_cdev = NULL; static struct Class *led_class = null;volatile unsigned long *gpbcon = null;volatile unsigned long *gpbdat = NULL; Volatile unsigned long *gpbup = NULL; static int Myled_open (struct inode * inode,struct file * file) {return 0;} Static ssize_t myled_read (struct file * file,const Char __user * in,size_t size,loff_t * off) {return 0;} Static ssize_t myled_write (struct file * file,const Char __user * in,size_t size,loff_t * off) {return 0;} static void Myled_configure (void) {*gpbcon &= ~ ((0x3<< (0*4)) | (0x3<< (1*4)) | (0x3<< (2*4)) | (0x3<< (3*4)); *gpbcon |= (0x1<< (0*4)) | (0x1<< (1*4)) | (0x1<< (2*4)) | (0x1<< (3*4)));} static void myled_on (void) {*gpbdat &= ~ ((1<<0) | (1<<1) | (1&LT;&LT;2) | (1<<3)); /Illumination}static void Myled_off (void) {*gpbdat |= (1 << 0) | ( 1 << 1) | (1 << 2) | (1 << 3);//Extinguish light}struct file_operations led_fops = {. Owner = This_module,.open = Myled_open,.read = Myled_read,.writ e = myled_write,};static int __devinit myled_probe (struct platform_device *pdev) {int ret;int err;dev_t devno;struct Resou Rce *pioresource_mem;devno = MKDEV (global_led_major,0);p rintk (kern_alert "myled_probe!\n"); if (devno) {ret = Register_ Chrdev_region (devno,1, "Myled_platfor_driver");} else {ret = alloc_chrdev_region (&devno,0,1, "Myled_platfor_driver"); global_led_major = major (Devno);} if (Ret < 0) {return ret;} Led_cdev = Cdev_alloc (); Cdev_init (led_cdev,&led_fops); led_cdev->owner = This_module;err = Cdev_add (Led_cdev, devno,1); led_class = Class_create (this_modULE, "Myled_platfor_driver");d evice_create (Led_class,null,mkdev (global_led_major,0), NULL, "platfor_driver_for_ Myled ");p Ioresource_mem = Platform_get_resource (pdev,ioresource_mem,0); Gpbcon = Ioremap (Pioresource_mem->start, Pioresource_mem->end-pioresource_mem->start); gpbdat = Gpbcon + 1;gpbup = Gpbcon + 2;myled_configure (); myled_on () ; if (err) {PRINTK (kern_notice "Error%d adding Led_cdev", err); return-1;} else {PRINTK (kern_notice "Platform_driver_for_ myled init ok!\n "); return 0;}} static int __devexit myled_remove (struct platform_device *pdev) {PRINTK ("myled_remove!\n"); Cdev_del (Led_cdev); Iounmap (Gpbcon); Unregister_chrdev_region (MKDEV (global_led_major,0), 1);d Evice_destroy (Led_class, MKDEV (Global_ led_major,0)); Class_destroy (Led_class); Myled_off (); return 0;} static struct Platform_driver Myled_platform_driver = {. Probe = Myled_probe,.remove = __devexit_p (myled_remove),. Driver = {. Name = "Myled_platform_device_driver",. Owner = this_module,}};static int __init Myled_platform_driver_iniT (void) {PRINTK ("platform_driver_for_myled init\n"); return Platform_driver_register (&myled_platform_driver);} static void __exit myled_platform_driver_exit (void) {PRINTK ("platform_driver_for_myled exit\n");p Latform_driver_ Unregister (&myled_platform_driver);} Module_author ("ZP1015"); Module_license ("GPL"); Module_param (Global_led_major,int,s_irugo); Module_init (Myled_platform_driver_init); Module_exit (Myled_platform_driver_exit);

3. Achieve Resultsregister the device and then register the device driver
insmod Lights All bright, rmmod and so on all out.

Linux driven Platform Driver Model summary (based on tiny210 platform LED driver)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.