platform.txt 11 KB

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