pwm.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. Generic PWM Device API
  2. February 1, 2010
  3. Bill Gatliff
  4. <[email protected]>
  5. The code in drivers/pwm and include/linux/pwm/ implements an API for
  6. applications involving pulse-width-modulation signals. This document
  7. describes how the API implementation facilitates both PWM-generating
  8. devices, and users of those devices.
  9. Motivation
  10. The primary goals for implementing the "generic PWM API" are to
  11. consolidate the various PWM implementations within a consistent and
  12. redundancy-reducing framework, and to facilitate the use of
  13. hotpluggable PWM devices.
  14. Previous PWM-related implementations within the Linux kernel achieved
  15. their consistency via cut-and-paste, but did not need to (and didn't)
  16. facilitate more than one PWM-generating device within the system---
  17. hotplug or otherwise. The Generic PWM Device API might be most
  18. appropriately viewed as an update to those implementations, rather
  19. than a complete rewrite.
  20. Challenges
  21. One of the difficulties in implementing a generic PWM framework is the
  22. fact that pulse-width-modulation applications involve real-world
  23. signals, which often must be carefully managed to prevent destruction
  24. of hardware that is linked to those signals. A DC motor that
  25. experiences a brief interruption in the PWM signal controlling it
  26. might destructively overheat; it could suddenly change speed, losing
  27. synchronization with a sensor; it could even suddenly change direction
  28. or torque, breaking the mechanical device connected to it.
  29. (A generic PWM device framework is not directly responsible for
  30. preventing the above scenarios: that responsibility lies with the
  31. hardware designer, and the application and driver authors. But it
  32. must to the greatest extent possible make it easy to avoid such
  33. problems).
  34. A generic PWM device framework must accommodate the substantial
  35. differences between available PWM-generating hardware devices, without
  36. becoming sub-optimal for any of them.
  37. Finally, a generic PWM device framework must be relatively
  38. lightweight, computationally speaking. Some PWM users demand
  39. high-speed outputs, plus the ability to regulate those outputs
  40. quickly. A device framework must be able to "keep up" with such
  41. hardware, while still leaving time to do real work.
  42. The Generic PWM Device API is an attempt to meet all of the above
  43. requirements. At its initial publication, the API was already in use
  44. managing small DC motors, sensors and solenoids through a
  45. custom-designed, optically-isolated H-bridge driver.
  46. Functional Overview
  47. The Generic PWM Device API framework is implemented in
  48. include/linux/pwm/pwm.h and drivers/pwm/pwm.c. The functions therein
  49. use information from pwm_device, pwm_channel and pwm_channel_config
  50. structures to invoke services in PWM peripheral device drivers.
  51. Consult drivers/pwm/atmel-pwm.c for an example driver.
  52. There are two classes of adopters of the PWM framework:
  53. "Users" -- those wishing to employ the API merely to produce PWM
  54. signals; once they have identified the appropriate physical output
  55. on the platform in question, they don't care about the details of
  56. the underlying hardware
  57. "Driver authors" -- those wishing to bind devices that can generate
  58. PWM signals to the Generic PWM Device API, so that the services of
  59. those devices become available to users. Assuming the hardware can
  60. support the needs of a user, driver authors don't care about the
  61. details of the user's application
  62. Generally speaking, users will first invoke pwm_request() to obtain a
  63. handle to a PWM device. They will then pass that handle to functions
  64. like pwm_duty_ns() and pwm_period_ns() to set the duty cycle and
  65. period of the PWM signal, respectively. They will also invoke
  66. pwm_start() and pwm_stop() to turn the signal on and off.
  67. The Generic PWM API framework also provides a sysfs interface to PWM
  68. devices, which is adequate for basic application needs and testing.
  69. Driver authors fill out a pwm_device structure, which describes the
  70. capabilities of the PWM hardware being constructed--- including the
  71. number of distinct output "channels" the peripheral offers. They then
  72. invoke pwm_register() (usually from within their device's probe()
  73. handler) to make the PWM API aware of their device. The framework
  74. will call back to the methods described in the pwm_device structure as
  75. users begin to configure and utilize the hardware.
  76. Note that PWM signals can be produced by a variety of peripherals,
  77. beyond the true "PWM hardware" offered by many system-on-chip devices.
  78. Other possibilities include timer/counters with compare-match
  79. capabilities, carefully-programmed synchronous serial ports
  80. (e.g. SPI), and GPIO pins driven by kernel interval timers. With a
  81. proper pwm_device structure, these devices and pseudo-devices can all
  82. be accommodated by the Generic PWM Device API framework.
  83. Using the API to Generate PWM Signals -- Basic Functions for Users
  84. pwm_request() -- Returns a pwm_channel pointer, which is subsequently
  85. passed to the other user-related PWM functions. Once requested, a PWM
  86. channel is marked as in-use and subsequent requests prior to
  87. pwm_free() will fail.
  88. The names used to refer to PWM devices are defined by driver authors.
  89. Typically they are platform device bus identifiers, and this
  90. convention is encouraged for consistency.
  91. pwm_free() -- Marks a PWM channel as no longer in use. The PWM device
  92. is stopped before it is released by the API.
  93. pwm_period_ns() -- Specifies the PWM signal's period, in nanoseconds.
  94. pwm_duty_ns() -- Specifies the PWM signal's active duration, in nanoseconds.
  95. pwm_duty_percent() -- Specifies the PWM signal's active duration, as a
  96. percentage of the current period of the signal. NOTE: this value is
  97. not recalculated if the period of the signal is subsequently changed.
  98. pwm_start(), pwm_stop() -- Turns the PWM signal on and off. Except
  99. where stated otherwise by a driver author, signals are stopped at the
  100. end of the current period, at which time the output is set to its
  101. inactive state.
  102. pwm_polarity() -- Defines whether the PWM signal output's active
  103. region is "1" or "0". A 10% duty-cycle, polarity=1 signal will
  104. conventionally be at 5V (or 3.3V, or 1000V, or whatever the platform
  105. hardware does) for 10% of the period. The same configuration of a
  106. polarity=0 signal will be at 5V (or 3.3V, or ...) for 90% of the
  107. period.
  108. Using the API to Generate PWM Signals -- Advanced Functions
  109. pwm_config() -- Passes a pwm_channel_config structure to the
  110. associated device driver. This function is invoked by pwm_start(),
  111. pwm_duty_ns(), etc. and is one of two main entry points to the PWM
  112. driver for the hardware being used. The configuration change is
  113. guaranteed atomic if multiple configuration changes are specified.
  114. This function might sleep, depending on what the device driver has to
  115. do to satisfy the request. All PWM device drivers must support this
  116. entry point.
  117. pwm_config_nosleep() -- Passes a pwm_channel_config structure to the
  118. associated device driver. If the driver must sleep in order to
  119. implement the requested configuration change, -EWOULDBLOCK is
  120. returned. Users may call this function from interrupt handlers, for
  121. example. This is the other main entry point into the PWM hardware
  122. driver, but not all device drivers support this entry point.
  123. pwm_synchronize(), pwm_unsynchronize() -- "Synchronizes" two or more
  124. PWM channels, if the underlying hardware permits. (If it doesn't, the
  125. framework facilitates emulating this capability but it is not yet
  126. implemented). Synchronized channels will start and stop
  127. simultaneously when any single channel in the group is started or
  128. stopped. Use pwm_unsynchronize(..., NULL) to completely detach a
  129. channel from any other synchronized channels. By default, all PWM
  130. channels are unsynchronized.
  131. pwm_set_handler() -- Defines an end-of-period callback. The indicated
  132. function will be invoked in a worker thread at the end of each PWM
  133. period, and can subsequently invoke pwm_config(), etc. Must be used
  134. with extreme care for high-speed PWM outputs. Set the handler
  135. function to NULL to un-set the handler.
  136. Implementing a PWM Device API Driver -- Functions for Driver Authors
  137. Fill out the appropriate fields in a pwm_device structure, and submit
  138. to pwm_register():
  139. bus_id -- the plain-text name of the device. Users will bind to a
  140. channel on the device using this name plus the channel number. For
  141. example, the Atmel PWMC's bus_id is "atmel_pwmc", the same as used by
  142. the platform device driver (recommended). The first device registered
  143. thereby receives bus_id "atmel_pwmc.0", which is what you put in
  144. pwm_device.bus_id. Channels are then named "atmel_pwmc.0:[0-3]".
  145. (Hint: just use pdev->dev.bus_id in your probe() method).
  146. nchan -- the number of distinct output channels provided by the device.
  147. request -- (optional) Invoked each time a user requests a channel.
  148. Use to turn on clocks, clean up register states, etc. The framework
  149. takes care of device locking/unlocking; you will see only successful
  150. requests.
  151. free -- (optional) Callback for each time a user relinquishes a
  152. channel. The framework will have already stopped, unsynchronized and
  153. un-handled the channel. Use to turn off clocks, etc. as necessary.
  154. synchronize, unsynchronize -- (optional) Callbacks to
  155. synchronize/unsynchronize channels. Some devices provide this
  156. capability in hardware; for others, it can be emulated (see
  157. atmel_pwmc.c's sync_mask for an example).
  158. set_callback -- (optional) Invoked when a user requests a handler. If
  159. the hardware supports an end-of-period interrupt, invoke the function
  160. indicated during your interrupt handler. The callback function itself
  161. is always internal to the API, and does not map directly to the user's
  162. callback function.
  163. config -- Invoked to change the device configuration, always from a
  164. sleep-capable context. All the changes indicated must be performed
  165. atomically, ideally synchronized to an end-of-period event (so that
  166. you avoid short or long output pulses). You may sleep, etc. as
  167. necessary within this function.
  168. config_nosleep -- (optional) Invoked to change device configuration
  169. from within a context that is not allowed to sleep. If you cannot
  170. perform the requested configuration changes without sleeping, return
  171. -EWOULDBLOCK.
  172. Acknowledgements
  173. The author expresses his gratitude to the countless developers who
  174. have reviewed and submitted feedback on the various versions of the
  175. Generic PWM Device API code, and those who have submitted drivers and
  176. applications that use the framework. You know who you are. ;)