xref: /OK3568_Linux_fs/kernel/Documentation/driver-api/driver-model/platform.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun============================
2*4882a593SmuzhiyunPlatform Devices and Drivers
3*4882a593Smuzhiyun============================
4*4882a593Smuzhiyun
5*4882a593SmuzhiyunSee <linux/platform_device.h> for the driver model interface to the
6*4882a593Smuzhiyunplatform bus:  platform_device, and platform_driver.  This pseudo-bus
7*4882a593Smuzhiyunis used to connect devices on busses with minimal infrastructure,
8*4882a593Smuzhiyunlike those used to integrate peripherals on many system-on-chip
9*4882a593Smuzhiyunprocessors, or some "legacy" PC interconnects; as opposed to large
10*4882a593Smuzhiyunformally specified ones like PCI or USB.
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun
13*4882a593SmuzhiyunPlatform devices
14*4882a593Smuzhiyun~~~~~~~~~~~~~~~~
15*4882a593SmuzhiyunPlatform devices are devices that typically appear as autonomous
16*4882a593Smuzhiyunentities in the system. This includes legacy port-based devices and
17*4882a593Smuzhiyunhost bridges to peripheral buses, and most controllers integrated
18*4882a593Smuzhiyuninto system-on-chip platforms.  What they usually have in common
19*4882a593Smuzhiyunis direct addressing from a CPU bus.  Rarely, a platform_device will
20*4882a593Smuzhiyunbe connected through a segment of some other kind of bus; but its
21*4882a593Smuzhiyunregisters will still be directly addressable.
22*4882a593Smuzhiyun
23*4882a593SmuzhiyunPlatform devices are given a name, used in driver binding, and a
24*4882a593Smuzhiyunlist of resources such as addresses and IRQs::
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun  struct platform_device {
27*4882a593Smuzhiyun	const char	*name;
28*4882a593Smuzhiyun	u32		id;
29*4882a593Smuzhiyun	struct device	dev;
30*4882a593Smuzhiyun	u32		num_resources;
31*4882a593Smuzhiyun	struct resource	*resource;
32*4882a593Smuzhiyun  };
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun
35*4882a593SmuzhiyunPlatform drivers
36*4882a593Smuzhiyun~~~~~~~~~~~~~~~~
37*4882a593SmuzhiyunPlatform drivers follow the standard driver model convention, where
38*4882a593Smuzhiyundiscovery/enumeration is handled outside the drivers, and drivers
39*4882a593Smuzhiyunprovide probe() and remove() methods.  They support power management
40*4882a593Smuzhiyunand shutdown notifications using the standard conventions::
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun  struct platform_driver {
43*4882a593Smuzhiyun	int (*probe)(struct platform_device *);
44*4882a593Smuzhiyun	int (*remove)(struct platform_device *);
45*4882a593Smuzhiyun	void (*shutdown)(struct platform_device *);
46*4882a593Smuzhiyun	int (*suspend)(struct platform_device *, pm_message_t state);
47*4882a593Smuzhiyun	int (*suspend_late)(struct platform_device *, pm_message_t state);
48*4882a593Smuzhiyun	int (*resume_early)(struct platform_device *);
49*4882a593Smuzhiyun	int (*resume)(struct platform_device *);
50*4882a593Smuzhiyun	struct device_driver driver;
51*4882a593Smuzhiyun  };
52*4882a593Smuzhiyun
53*4882a593SmuzhiyunNote that probe() should in general verify that the specified device hardware
54*4882a593Smuzhiyunactually exists; sometimes platform setup code can't be sure.  The probing
55*4882a593Smuzhiyuncan use device resources, including clocks, and device platform_data.
56*4882a593Smuzhiyun
57*4882a593SmuzhiyunPlatform drivers register themselves the normal way::
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun	int platform_driver_register(struct platform_driver *drv);
60*4882a593Smuzhiyun
61*4882a593SmuzhiyunOr, in common situations where the device is known not to be hot-pluggable,
62*4882a593Smuzhiyunthe probe() routine can live in an init section to reduce the driver's
63*4882a593Smuzhiyunruntime memory footprint::
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun	int platform_driver_probe(struct platform_driver *drv,
66*4882a593Smuzhiyun			  int (*probe)(struct platform_device *))
67*4882a593Smuzhiyun
68*4882a593SmuzhiyunKernel modules can be composed of several platform drivers. The platform core
69*4882a593Smuzhiyunprovides helpers to register and unregister an array of drivers::
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun	int __platform_register_drivers(struct platform_driver * const *drivers,
72*4882a593Smuzhiyun				      unsigned int count, struct module *owner);
73*4882a593Smuzhiyun	void platform_unregister_drivers(struct platform_driver * const *drivers,
74*4882a593Smuzhiyun					 unsigned int count);
75*4882a593Smuzhiyun
76*4882a593SmuzhiyunIf one of the drivers fails to register, all drivers registered up to that
77*4882a593Smuzhiyunpoint will be unregistered in reverse order. Note that there is a convenience
78*4882a593Smuzhiyunmacro that passes THIS_MODULE as owner parameter::
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun	#define platform_register_drivers(drivers, count)
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun
83*4882a593SmuzhiyunDevice Enumeration
84*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~
85*4882a593SmuzhiyunAs a rule, platform specific (and often board-specific) setup code will
86*4882a593Smuzhiyunregister platform devices::
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun	int platform_device_register(struct platform_device *pdev);
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun	int platform_add_devices(struct platform_device **pdevs, int ndev);
91*4882a593Smuzhiyun
92*4882a593SmuzhiyunThe general rule is to register only those devices that actually exist,
93*4882a593Smuzhiyunbut in some cases extra devices might be registered.  For example, a kernel
94*4882a593Smuzhiyunmight be configured to work with an external network adapter that might not
95*4882a593Smuzhiyunbe populated on all boards, or likewise to work with an integrated controller
96*4882a593Smuzhiyunthat some boards might not hook up to any peripherals.
97*4882a593Smuzhiyun
98*4882a593SmuzhiyunIn some cases, boot firmware will export tables describing the devices
99*4882a593Smuzhiyunthat are populated on a given board.   Without such tables, often the
100*4882a593Smuzhiyunonly way for system setup code to set up the correct devices is to build
101*4882a593Smuzhiyuna kernel for a specific target board.  Such board-specific kernels are
102*4882a593Smuzhiyuncommon with embedded and custom systems development.
103*4882a593Smuzhiyun
104*4882a593SmuzhiyunIn many cases, the memory and IRQ resources associated with the platform
105*4882a593Smuzhiyundevice are not enough to let the device's driver work.  Board setup code
106*4882a593Smuzhiyunwill often provide additional information using the device's platform_data
107*4882a593Smuzhiyunfield to hold additional information.
108*4882a593Smuzhiyun
109*4882a593SmuzhiyunEmbedded systems frequently need one or more clocks for platform devices,
110*4882a593Smuzhiyunwhich are normally kept off until they're actively needed (to save power).
111*4882a593SmuzhiyunSystem setup also associates those clocks with the device, so that
112*4882a593Smuzhiyuncalls to clk_get(&pdev->dev, clock_name) return them as needed.
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun
115*4882a593SmuzhiyunLegacy Drivers:  Device Probing
116*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
117*4882a593SmuzhiyunSome drivers are not fully converted to the driver model, because they take
118*4882a593Smuzhiyunon a non-driver role:  the driver registers its platform device, rather than
119*4882a593Smuzhiyunleaving that for system infrastructure.  Such drivers can't be hotplugged
120*4882a593Smuzhiyunor coldplugged, since those mechanisms require device creation to be in a
121*4882a593Smuzhiyundifferent system component than the driver.
122*4882a593Smuzhiyun
123*4882a593SmuzhiyunThe only "good" reason for this is to handle older system designs which, like
124*4882a593Smuzhiyunoriginal IBM PCs, rely on error-prone "probe-the-hardware" models for hardware
125*4882a593Smuzhiyunconfiguration.  Newer systems have largely abandoned that model, in favor of
126*4882a593Smuzhiyunbus-level support for dynamic configuration (PCI, USB), or device tables
127*4882a593Smuzhiyunprovided by the boot firmware (e.g. PNPACPI on x86).  There are too many
128*4882a593Smuzhiyunconflicting options about what might be where, and even educated guesses by
129*4882a593Smuzhiyunan operating system will be wrong often enough to make trouble.
130*4882a593Smuzhiyun
131*4882a593SmuzhiyunThis style of driver is discouraged.  If you're updating such a driver,
132*4882a593Smuzhiyunplease try to move the device enumeration to a more appropriate location,
133*4882a593Smuzhiyunoutside the driver.  This will usually be cleanup, since such drivers
134*4882a593Smuzhiyuntend to already have "normal" modes, such as ones using device nodes that
135*4882a593Smuzhiyunwere created by PNP or by platform device setup.
136*4882a593Smuzhiyun
137*4882a593SmuzhiyunNone the less, there are some APIs to support such legacy drivers.  Avoid
138*4882a593Smuzhiyunusing these calls except with such hotplug-deficient drivers::
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun	struct platform_device *platform_device_alloc(
141*4882a593Smuzhiyun			const char *name, int id);
142*4882a593Smuzhiyun
143*4882a593SmuzhiyunYou can use platform_device_alloc() to dynamically allocate a device, which
144*4882a593Smuzhiyunyou will then initialize with resources and platform_device_register().
145*4882a593SmuzhiyunA better solution is usually::
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun	struct platform_device *platform_device_register_simple(
148*4882a593Smuzhiyun			const char *name, int id,
149*4882a593Smuzhiyun			struct resource *res, unsigned int nres);
150*4882a593Smuzhiyun
151*4882a593SmuzhiyunYou can use platform_device_register_simple() as a one-step call to allocate
152*4882a593Smuzhiyunand register a device.
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun
155*4882a593SmuzhiyunDevice Naming and Driver Binding
156*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
157*4882a593SmuzhiyunThe platform_device.dev.bus_id is the canonical name for the devices.
158*4882a593SmuzhiyunIt's built from two components:
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun    * platform_device.name ... which is also used to for driver matching.
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun    * platform_device.id ... the device instance number, or else "-1"
163*4882a593Smuzhiyun      to indicate there's only one.
164*4882a593Smuzhiyun
165*4882a593SmuzhiyunThese are concatenated, so name/id "serial"/0 indicates bus_id "serial.0", and
166*4882a593Smuzhiyun"serial/3" indicates bus_id "serial.3"; both would use the platform_driver
167*4882a593Smuzhiyunnamed "serial".  While "my_rtc"/-1 would be bus_id "my_rtc" (no instance id)
168*4882a593Smuzhiyunand use the platform_driver called "my_rtc".
169*4882a593Smuzhiyun
170*4882a593SmuzhiyunDriver binding is performed automatically by the driver core, invoking
171*4882a593Smuzhiyundriver probe() after finding a match between device and driver.  If the
172*4882a593Smuzhiyunprobe() succeeds, the driver and device are bound as usual.  There are
173*4882a593Smuzhiyunthree different ways to find such a match:
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun    - Whenever a device is registered, the drivers for that bus are
176*4882a593Smuzhiyun      checked for matches.  Platform devices should be registered very
177*4882a593Smuzhiyun      early during system boot.
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun    - When a driver is registered using platform_driver_register(), all
180*4882a593Smuzhiyun      unbound devices on that bus are checked for matches.  Drivers
181*4882a593Smuzhiyun      usually register later during booting, or by module loading.
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun    - Registering a driver using platform_driver_probe() works just like
184*4882a593Smuzhiyun      using platform_driver_register(), except that the driver won't
185*4882a593Smuzhiyun      be probed later if another device registers.  (Which is OK, since
186*4882a593Smuzhiyun      this interface is only for use with non-hotpluggable devices.)
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun
189*4882a593SmuzhiyunEarly Platform Devices and Drivers
190*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
191*4882a593SmuzhiyunThe early platform interfaces provide platform data to platform device
192*4882a593Smuzhiyundrivers early on during the system boot. The code is built on top of the
193*4882a593Smuzhiyunearly_param() command line parsing and can be executed very early on.
194*4882a593Smuzhiyun
195*4882a593SmuzhiyunExample: "earlyprintk" class early serial console in 6 steps
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun1. Registering early platform device data
198*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
199*4882a593SmuzhiyunThe architecture code registers platform device data using the function
200*4882a593Smuzhiyunearly_platform_add_devices(). In the case of early serial console this
201*4882a593Smuzhiyunshould be hardware configuration for the serial port. Devices registered
202*4882a593Smuzhiyunat this point will later on be matched against early platform drivers.
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun2. Parsing kernel command line
205*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
206*4882a593SmuzhiyunThe architecture code calls parse_early_param() to parse the kernel
207*4882a593Smuzhiyuncommand line. This will execute all matching early_param() callbacks.
208*4882a593SmuzhiyunUser specified early platform devices will be registered at this point.
209*4882a593SmuzhiyunFor the early serial console case the user can specify port on the
210*4882a593Smuzhiyunkernel command line as "earlyprintk=serial.0" where "earlyprintk" is
211*4882a593Smuzhiyunthe class string, "serial" is the name of the platform driver and
212*4882a593Smuzhiyun0 is the platform device id. If the id is -1 then the dot and the
213*4882a593Smuzhiyunid can be omitted.
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun3. Installing early platform drivers belonging to a certain class
216*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
217*4882a593SmuzhiyunThe architecture code may optionally force registration of all early
218*4882a593Smuzhiyunplatform drivers belonging to a certain class using the function
219*4882a593Smuzhiyunearly_platform_driver_register_all(). User specified devices from
220*4882a593Smuzhiyunstep 2 have priority over these. This step is omitted by the serial
221*4882a593Smuzhiyundriver example since the early serial driver code should be disabled
222*4882a593Smuzhiyununless the user has specified port on the kernel command line.
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun4. Early platform driver registration
225*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
226*4882a593SmuzhiyunCompiled-in platform drivers making use of early_platform_init() are
227*4882a593Smuzhiyunautomatically registered during step 2 or 3. The serial driver example
228*4882a593Smuzhiyunshould use early_platform_init("earlyprintk", &platform_driver).
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun5. Probing of early platform drivers belonging to a certain class
231*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
232*4882a593SmuzhiyunThe architecture code calls early_platform_driver_probe() to match
233*4882a593Smuzhiyunregistered early platform devices associated with a certain class with
234*4882a593Smuzhiyunregistered early platform drivers. Matched devices will get probed().
235*4882a593SmuzhiyunThis step can be executed at any point during the early boot. As soon
236*4882a593Smuzhiyunas possible may be good for the serial port case.
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun6. Inside the early platform driver probe()
239*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
240*4882a593SmuzhiyunThe driver code needs to take special care during early boot, especially
241*4882a593Smuzhiyunwhen it comes to memory allocation and interrupt registration. The code
242*4882a593Smuzhiyunin the probe() function can use is_early_platform_device() to check if
243*4882a593Smuzhiyunit is called at early platform device or at the regular platform device
244*4882a593Smuzhiyuntime. The early serial driver performs register_console() at this point.
245*4882a593Smuzhiyun
246*4882a593SmuzhiyunFor further information, see <linux/platform_device.h>.
247