LINUX platform devices are divided
1. device layer (mainly used to describe device resources)
2 driver layer (what we need to implement the write driver)
Device layer: mainly defines the resources of a device.
Use the platform_device struct to describe a platform device. Defined in (/include/LINUX/platform_device.h)
Struct platform_device {
Const char * name; // device name
Int id;
Struct device dev;
U32 num_resources; // number of resources used by devices
Struct resource * resource; // resource used
};
Example:/* RTC */
/*************************************** ***********************/
Arch/arm/plat-s3c24xx/devs. c defines many platform devices, intercepting the RTC device structure
Static struct resource initi_rtc_resource [] = {
[0] = {// IO port resource range
. Start = S3C24XX_PA_RTC,
. End = S3C24XX_PA_RTC + 0xff,
. Flags = IORESOURCE_MEM, // defines the type of this resource (the common point is the type of memory ing)/include/linux/ioport. h
},
[1] = {// RTC alarm resource interruption
. Start = IRQ_RTC,
. End = IRQ_RTC,
. Flags = IORESOURCE_IRQ, // The resource type is interrupted.
},
[2] = {// TICK cycle time resource interruption
. Start = IRQ_TICK,
. End = IRQ_TICK,
. Flags = IORESOURCE_IRQ,
}
};
Struct platform_device initi_device_rtc = {
. Name = "s3c2410-rtc", // RTC name (same as the name in the driver)
. Id =-1,
. Num_resources = ARRAY_SIZE (cloud_rtc_resource), (evaluate the value of the above-mentioned cloud_rtc_resource and define several resources)
. Resource = maid resource, // corresponding to the above struct resource maid []
};
EXPORT_SYMBOL (initi_device_rtc );
/*************************************** ***********************/
All the device resources of RTC are defined above! Then we need to add these resources to the kernel!
Open the arch/arm/mach-s3c2440/mach-smdk2440.c System Portal file for this ARM 2440 platform. There are two!
Static struct platform_device * smdk2440_devices [] _ initdata = {
& Initi_device_usb,
& Amp; cloud_device_ LCD,
& Amp; cloud_device_wdt,
& Amp; cloud_device_i2c0,
& Amp; cloud_device_iis,
& Initi_device_rtc, // because we want to add the RTC device to the system, add it here
};
Static void _ init smdk2440_machine_init (void)
{
S3c24xx_fb_set_platdata (& smdk2440_fb_info );
Initi_i2c0_set_platdata (NULL );
Platform_add_devices (smdk2440_devices, ARRAY_SIZE (smdk2440_devices); // here is the registration function for adding a device! The above smdk2440_devices is called.
Smdk_machine_init ();
}
Add the device resources to the kernel!
The following is our driver!
For detailed steps and Implementation of the RTC driver, refer to the blog of Huang gang's predecessors! Http://blog.chinaunix.net/u3/101649/showart_2142248.html
I am also a cainiao. I just want to write down my understanding and remember it!
An error occurred. Please click it and correct it! Thank you!
"Driver 《《《《《《《《《《《《《《《《《《《《《《《《《《《《《
The driver layer defines the driver using the struct platform_driver struct.
Static struct platform_driver rtc_driver = // defines the structure of platform_driver.
{
. Probe = rtc_probe,/* RTC detection function,
. Remove = _ devexit_p (rtc_remove),/* RTC remove function ,*/
. Suspend = rtc_suspend,/* RTC suspension function,
. Resume = rtc_resume,/* RTC restoration function,
. Driver =
{
/* Note that the name must be the same as that of the platform device defined in the system, so that the platform device can be associated with the driver of the platform device */
. Name = "s3c2410-rtc ",
. Owner = THIS_MODULE,
},
};
Static int _ init rtc_init (void)
{
/* Register RTC as a platform device driver */
Return platform_driver_register (& rtc_driver );
}
Static void _ exit rtc_exit (void)
{
/* Cancel the RTC platform device driver */
Platform_driver_unregister (& rtc_driver );
}
Module_init (rtc_init );
Module_exit (rtc_exit );
MODULE_LICENSE ("GPL ");
MODULE_AUTHOR ("Huang Gang ");
MODULE_DESCRIPTION ("My2440 RTC driver ");
This place is also the same as we used to write the character module driver, using module_init (), but we put everything like interrupt registration and IO ing in rtc_probe for implementation! Therefore, call return platform_driver_register (& rtc_driver) to register RTC as the platform device driver.
In fact, I don't know much about it in many places. I understand it like this: to maintain low power consumption, we just turn it on when using a certain device, when you do not need it, we can suspend it. Because ARM is generally used in consumer electronics, there is a high requirement on power consumption! This is also the purpose of using platform devices!
At first, I thought this was the driver implementation function (similar to open, read, and so on )! I found it later. No! The interface function is behind!
After the device is registered, the system only knows the device, but does not know what resources to use! So the rtc_probe detection function came out! It is mainly responsible for resource probe and initialization (in fact, device initialization can be put in the _ init rtc_init () function ). We register the interrupt (RTC has two interrupts) in the probe function to perform memory ing (IOMAP (), just like the initialization of the write character driver! However, Wang Gang's registration is interrupted in the subsequent open, where iomap is. I think it can be put anywhere, for example, interrupted. If shared interruptions are involved, you still need to register it in open.
For the probe function, see the blog of Huang gang!
After initialization, We need to register an RTC device class.
|/* Register RTC as an RTC device. The RTC device class is defined by the system in the core part of the RTC driver,
| Note that the rtcops parameter is a struct. The role of this struct and the interface functions are implemented in step 3.
| The rtc_device_register function is defined in rtc. h and implemented in drivers/rtc/class. c */
| Rtc = rtc_device_register ("my2440", & pdev-> dev, & rtcops, THIS_MODULE );
| If (IS_ERR (rtc ))
| {
|/* Handle errors */
| Dev_err (& pdev-> dev, "cannot attach rtc \ n ");
| Ret = PTR_ERR (rtc );
| Goto err_nortc;
|}
|
Static const struct rtc_class_ops rtcops = {// file_operation function of the RTC class)
. Open = rtc_open,
. Release = rtc_release,
. Irq_set_freq = rtc_setfreq,
. Irq_set_state = rtc_setpie,
. Read_time = rtc_gettime,
. Set_time = rtc_settime,
. Read_alarm = rtc_getalarm,
. Set_alarm = rtc_setalarm,
};
// This is the prototype of the RTC class registration function!
Struct rtc_device * rtc_device_register (const char * name,
Struct device * dev,
Const struct rtc_class_ops * ops,
Struct module * owner );
I am not quite clear about the role of this class. Do I understand the purpose of unified management ??? Not clear! Let me know!
There is a struct rtc_class_ops * ops in the class registration function. This struct is very important. It is the ones we WRITE the driver (READ, WRITE, etc.). We mainly implement this rtc_class_ops.
Struct rtc_class_ops {
Int (* open) (struct device *); // open
Void (* release) (struct device *); // close
Int (* ioctl) (struct device *, unsigned int, unsigned long); // IOCTL
Int (* read_time) (struct device *, struct rtc_time *); // read time
Int (* set_time) (struct device *, struct rtc_time *); // set the time
Int (* read_alarm) (struct device *, struct rtc_wkalrm *);
Int (* set_alarm) (struct device *, struct rtc_wkalrm *);
Int (* proc) (struct device *, struct seq_file *);
Int (* set_mmss) (struct device *, unsigned long secs );
Int (* irq_set_state) (struct device *, int enabled );
Int (* irq_set_freq) (struct device *, int freq );
Int (* read_callback) (struct device *, int data );
};
Let's see how similar it is to our file_operation! It can be said that it is exactly the same! We implement these functions to operate our RTC. I understand it now!
That's almost the case! I am too fond of food! Not very clear!
In what cases can we use the platform driver mechanism to write a driver? // The following is a conversion !! Haha!
My understanding is that as long as the peripheral device is not dependent on the kernel itself (in other words, as long as it is not within the minimum system required for the kernel operation), it is relatively independent, with their own resources (addresses and IRQs), you can use platform_driver. For example, LCD, usb, uart, and so on can all be written using platfrom_driver, while it is better to use the platfrom_driver mechanism for devices in the smallest system such as timer and irq. In fact, the kernel implementation is also like this.
As of Huang gang's blog! Http://blog.chinaunix.net/u3/101649/showart_2142248.html
The overall structure of RTC in Linux:
In my personal understanding, the overall structure of RTC in Linux is divided into two parts. The first part is that the platform device is mounted to the system bus. Here I call it the device layer (which may not be accurate ); the second part is the driver part, which is called the driver layer. In Linux, it seems impossible for a driver to be used on different platforms. Therefore, we should first look at the RTC part of the 2.6.30.4 kernel driver as a separate folder, the folder contains many RTC drivers of different architectures. Of course, there are also S3C2440 RTC drivers. However, these drivers all use methods in a group of files, this set of files is the core of RTC (note that the core here is not the operation on the RTC hardware, it refers to the RTC operation method. The operation on the hardware register is still in the specific driver ). Okay, let's use a diagram to illustrate this relationship !!
Source: LINUX platform device (linux platform_device and platform_driver) zhuan_lala19861008 Space _ Baidu Space