platform.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. ============================
  2. Platform Devices and Drivers
  3. ============================
  4. See <linux/platform_device.h> for the driver model interface to the
  5. platform bus: platform_device, and platform_driver. This pseudo-bus
  6. is used to connect devices on busses with minimal infrastructure,
  7. like those used to integrate peripherals on many system-on-chip
  8. processors, or some "legacy" PC interconnects; as opposed to large
  9. formally specified ones like PCI or USB.
  10. Platform devices
  11. ~~~~~~~~~~~~~~~~
  12. Platform devices are devices that typically appear as autonomous
  13. entities in the system. This includes legacy port-based devices and
  14. host bridges to peripheral buses, and most controllers integrated
  15. into system-on-chip platforms. What they usually have in common
  16. is direct addressing from a CPU bus. Rarely, a platform_device will
  17. be connected through a segment of some other kind of bus; but its
  18. registers will still be directly addressable.
  19. Platform devices are given a name, used in driver binding, and a
  20. list of resources such as addresses and IRQs::
  21. struct platform_device {
  22. const char *name;
  23. u32 id;
  24. struct device dev;
  25. u32 num_resources;
  26. struct resource *resource;
  27. };
  28. Platform drivers
  29. ~~~~~~~~~~~~~~~~
  30. Platform drivers follow the standard driver model convention, where
  31. discovery/enumeration is handled outside the drivers, and drivers
  32. provide probe() and remove() methods. They support power management
  33. and shutdown notifications using the standard conventions::
  34. struct platform_driver {
  35. int (*probe)(struct platform_device *);
  36. int (*remove)(struct platform_device *);
  37. void (*shutdown)(struct platform_device *);
  38. int (*suspend)(struct platform_device *, pm_message_t state);
  39. int (*suspend_late)(struct platform_device *, pm_message_t state);
  40. int (*resume_early)(struct platform_device *);
  41. int (*resume)(struct platform_device *);
  42. struct device_driver driver;
  43. };
  44. Note that probe() should in general verify that the specified device hardware
  45. actually exists; sometimes platform setup code can't be sure. The probing
  46. can use device resources, including clocks, and device platform_data.
  47. Platform drivers register themselves the normal way::
  48. int platform_driver_register(struct platform_driver *drv);
  49. Or, in common situations where the device is known not to be hot-pluggable,
  50. the probe() routine can live in an init section to reduce the driver's
  51. runtime memory footprint::
  52. int platform_driver_probe(struct platform_driver *drv,
  53. int (*probe)(struct platform_device *))
  54. Kernel modules can be composed of several platform drivers. The platform core
  55. provides helpers to register and unregister an array of drivers::
  56. int __platform_register_drivers(struct platform_driver * const *drivers,
  57. unsigned int count, struct module *owner);
  58. void platform_unregister_drivers(struct platform_driver * const *drivers,
  59. unsigned int count);
  60. If one of the drivers fails to register, all drivers registered up to that
  61. point will be unregistered in reverse order. Note that there is a convenience
  62. macro that passes THIS_MODULE as owner parameter::
  63. #define platform_register_drivers(drivers, count)
  64. Device Enumeration
  65. ~~~~~~~~~~~~~~~~~~
  66. As a rule, platform specific (and often board-specific) setup code will
  67. register platform devices::
  68. int platform_device_register(struct platform_device *pdev);
  69. int platform_add_devices(struct platform_device **pdevs, int ndev);
  70. The general rule is to register only those devices that actually exist,
  71. but in some cases extra devices might be registered. For example, a kernel
  72. might be configured to work with an external network adapter that might not
  73. be populated on all boards, or likewise to work with an integrated controller
  74. that some boards might not hook up to any peripherals.
  75. In some cases, boot firmware will export tables describing the devices
  76. that are populated on a given board. Without such tables, often the
  77. only way for system setup code to set up the correct devices is to build
  78. a kernel for a specific target board. Such board-specific kernels are
  79. common with embedded and custom systems development.
  80. In many cases, the memory and IRQ resources associated with the platform
  81. device are not enough to let the device's driver work. Board setup code
  82. will often provide additional information using the device's platform_data
  83. field to hold additional information.
  84. Embedded systems frequently need one or more clocks for platform devices,
  85. which are normally kept off until they're actively needed (to save power).
  86. System setup also associates those clocks with the device, so that
  87. calls to clk_get(&pdev->dev, clock_name) return them as needed.
  88. Legacy Drivers: Device Probing
  89. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  90. Some drivers are not fully converted to the driver model, because they take
  91. on a non-driver role: the driver registers its platform device, rather than
  92. leaving that for system infrastructure. Such drivers can't be hotplugged
  93. or coldplugged, since those mechanisms require device creation to be in a
  94. different system component than the driver.
  95. The only "good" reason for this is to handle older system designs which, like
  96. original IBM PCs, rely on error-prone "probe-the-hardware" models for hardware
  97. configuration. Newer systems have largely abandoned that model, in favor of
  98. bus-level support for dynamic configuration (PCI, USB), or device tables
  99. provided by the boot firmware (e.g. PNPACPI on x86). There are too many
  100. conflicting options about what might be where, and even educated guesses by
  101. an operating system will be wrong often enough to make trouble.
  102. This style of driver is discouraged. If you're updating such a driver,
  103. please try to move the device enumeration to a more appropriate location,
  104. outside the driver. This will usually be cleanup, since such drivers
  105. tend to already have "normal" modes, such as ones using device nodes that
  106. were created by PNP or by platform device setup.
  107. None the less, there are some APIs to support such legacy drivers. Avoid
  108. using these calls except with such hotplug-deficient drivers::
  109. struct platform_device *platform_device_alloc(
  110. const char *name, int id);
  111. You can use platform_device_alloc() to dynamically allocate a device, which
  112. you will then initialize with resources and platform_device_register().
  113. A better solution is usually::
  114. struct platform_device *platform_device_register_simple(
  115. const char *name, int id,
  116. struct resource *res, unsigned int nres);
  117. You can use platform_device_register_simple() as a one-step call to allocate
  118. and register a device.
  119. Device Naming and Driver Binding
  120. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  121. The platform_device.dev.bus_id is the canonical name for the devices.
  122. It's built from two components:
  123. * platform_device.name ... which is also used to for driver matching.
  124. * platform_device.id ... the device instance number, or else "-1"
  125. to indicate there's only one.
  126. These are concatenated, so name/id "serial"/0 indicates bus_id "serial.0", and
  127. "serial/3" indicates bus_id "serial.3"; both would use the platform_driver
  128. named "serial". While "my_rtc"/-1 would be bus_id "my_rtc" (no instance id)
  129. and use the platform_driver called "my_rtc".
  130. Driver binding is performed automatically by the driver core, invoking
  131. driver probe() after finding a match between device and driver. If the
  132. probe() succeeds, the driver and device are bound as usual. There are
  133. three different ways to find such a match:
  134. - Whenever a device is registered, the drivers for that bus are
  135. checked for matches. Platform devices should be registered very
  136. early during system boot.
  137. - When a driver is registered using platform_driver_register(), all
  138. unbound devices on that bus are checked for matches. Drivers
  139. usually register later during booting, or by module loading.
  140. - Registering a driver using platform_driver_probe() works just like
  141. using platform_driver_register(), except that the driver won't
  142. be probed later if another device registers. (Which is OK, since
  143. this interface is only for use with non-hotpluggable devices.)
  144. Early Platform Devices and Drivers
  145. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  146. The early platform interfaces provide platform data to platform device
  147. drivers early on during the system boot. The code is built on top of the
  148. early_param() command line parsing and can be executed very early on.
  149. Example: "earlyprintk" class early serial console in 6 steps
  150. 1. Registering early platform device data
  151. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  152. The architecture code registers platform device data using the function
  153. early_platform_add_devices(). In the case of early serial console this
  154. should be hardware configuration for the serial port. Devices registered
  155. at this point will later on be matched against early platform drivers.
  156. 2. Parsing kernel command line
  157. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  158. The architecture code calls parse_early_param() to parse the kernel
  159. command line. This will execute all matching early_param() callbacks.
  160. User specified early platform devices will be registered at this point.
  161. For the early serial console case the user can specify port on the
  162. kernel command line as "earlyprintk=serial.0" where "earlyprintk" is
  163. the class string, "serial" is the name of the platform driver and
  164. 0 is the platform device id. If the id is -1 then the dot and the
  165. id can be omitted.
  166. 3. Installing early platform drivers belonging to a certain class
  167. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  168. The architecture code may optionally force registration of all early
  169. platform drivers belonging to a certain class using the function
  170. early_platform_driver_register_all(). User specified devices from
  171. step 2 have priority over these. This step is omitted by the serial
  172. driver example since the early serial driver code should be disabled
  173. unless the user has specified port on the kernel command line.
  174. 4. Early platform driver registration
  175. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  176. Compiled-in platform drivers making use of early_platform_init() are
  177. automatically registered during step 2 or 3. The serial driver example
  178. should use early_platform_init("earlyprintk", &platform_driver).
  179. 5. Probing of early platform drivers belonging to a certain class
  180. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  181. The architecture code calls early_platform_driver_probe() to match
  182. registered early platform devices associated with a certain class with
  183. registered early platform drivers. Matched devices will get probed().
  184. This step can be executed at any point during the early boot. As soon
  185. as possible may be good for the serial port case.
  186. 6. Inside the early platform driver probe()
  187. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  188. The driver code needs to take special care during early boot, especially
  189. when it comes to memory allocation and interrupt registration. The code
  190. in the probe() function can use is_early_platform_device() to check if
  191. it is called at early platform device or at the regular platform device
  192. time. The early serial driver performs register_console() at this point.
  193. For further information, see <linux/platform_device.h>.