generic-counter.rst 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. .. SPDX-License-Identifier: GPL-2.0
  2. =========================
  3. Generic Counter Interface
  4. =========================
  5. Introduction
  6. ============
  7. Counter devices are prevalent among a diverse spectrum of industries.
  8. The ubiquitous presence of these devices necessitates a common interface
  9. and standard of interaction and exposure. This driver API attempts to
  10. resolve the issue of duplicate code found among existing counter device
  11. drivers by introducing a generic counter interface for consumption. The
  12. Generic Counter interface enables drivers to support and expose a common
  13. set of components and functionality present in counter devices.
  14. Theory
  15. ======
  16. Counter devices can vary greatly in design, but regardless of whether
  17. some devices are quadrature encoder counters or tally counters, all
  18. counter devices consist of a core set of components. This core set of
  19. components, shared by all counter devices, is what forms the essence of
  20. the Generic Counter interface.
  21. There are three core components to a counter:
  22. * Signal:
  23. Stream of data to be evaluated by the counter.
  24. * Synapse:
  25. Association of a Signal, and evaluation trigger, with a Count.
  26. * Count:
  27. Accumulation of the effects of connected Synapses.
  28. SIGNAL
  29. ------
  30. A Signal represents a stream of data. This is the input data that is
  31. evaluated by the counter to determine the count data; e.g. a quadrature
  32. signal output line of a rotary encoder. Not all counter devices provide
  33. user access to the Signal data, so exposure is optional for drivers.
  34. When the Signal data is available for user access, the Generic Counter
  35. interface provides the following available signal values:
  36. * SIGNAL_LOW:
  37. Signal line is in a low state.
  38. * SIGNAL_HIGH:
  39. Signal line is in a high state.
  40. A Signal may be associated with one or more Counts.
  41. SYNAPSE
  42. -------
  43. A Synapse represents the association of a Signal with a Count. Signal
  44. data affects respective Count data, and the Synapse represents this
  45. relationship.
  46. The Synapse action mode specifies the Signal data condition that
  47. triggers the respective Count's count function evaluation to update the
  48. count data. The Generic Counter interface provides the following
  49. available action modes:
  50. * None:
  51. Signal does not trigger the count function. In Pulse-Direction count
  52. function mode, this Signal is evaluated as Direction.
  53. * Rising Edge:
  54. Low state transitions to high state.
  55. * Falling Edge:
  56. High state transitions to low state.
  57. * Both Edges:
  58. Any state transition.
  59. A counter is defined as a set of input signals associated with count
  60. data that are generated by the evaluation of the state of the associated
  61. input signals as defined by the respective count functions. Within the
  62. context of the Generic Counter interface, a counter consists of Counts
  63. each associated with a set of Signals, whose respective Synapse
  64. instances represent the count function update conditions for the
  65. associated Counts.
  66. A Synapse associates one Signal with one Count.
  67. COUNT
  68. -----
  69. A Count represents the accumulation of the effects of connected
  70. Synapses; i.e. the count data for a set of Signals. The Generic
  71. Counter interface represents the count data as a natural number.
  72. A Count has a count function mode which represents the update behavior
  73. for the count data. The Generic Counter interface provides the following
  74. available count function modes:
  75. * Increase:
  76. Accumulated count is incremented.
  77. * Decrease:
  78. Accumulated count is decremented.
  79. * Pulse-Direction:
  80. Rising edges on signal A updates the respective count. The input level
  81. of signal B determines direction.
  82. * Quadrature:
  83. A pair of quadrature encoding signals are evaluated to determine
  84. position and direction. The following Quadrature modes are available:
  85. - x1 A:
  86. If direction is forward, rising edges on quadrature pair signal A
  87. updates the respective count; if the direction is backward, falling
  88. edges on quadrature pair signal A updates the respective count.
  89. Quadrature encoding determines the direction.
  90. - x1 B:
  91. If direction is forward, rising edges on quadrature pair signal B
  92. updates the respective count; if the direction is backward, falling
  93. edges on quadrature pair signal B updates the respective count.
  94. Quadrature encoding determines the direction.
  95. - x2 A:
  96. Any state transition on quadrature pair signal A updates the
  97. respective count. Quadrature encoding determines the direction.
  98. - x2 B:
  99. Any state transition on quadrature pair signal B updates the
  100. respective count. Quadrature encoding determines the direction.
  101. - x4:
  102. Any state transition on either quadrature pair signals updates the
  103. respective count. Quadrature encoding determines the direction.
  104. A Count has a set of one or more associated Synapses.
  105. Paradigm
  106. ========
  107. The most basic counter device may be expressed as a single Count
  108. associated with a single Signal via a single Synapse. Take for example
  109. a counter device which simply accumulates a count of rising edges on a
  110. source input line::
  111. Count Synapse Signal
  112. ----- ------- ------
  113. +---------------------+
  114. | Data: Count | Rising Edge ________
  115. | Function: Increase | <------------- / Source \
  116. | | ____________
  117. +---------------------+
  118. In this example, the Signal is a source input line with a pulsing
  119. voltage, while the Count is a persistent count value which is repeatedly
  120. incremented. The Signal is associated with the respective Count via a
  121. Synapse. The increase function is triggered by the Signal data condition
  122. specified by the Synapse -- in this case a rising edge condition on the
  123. voltage input line. In summary, the counter device existence and
  124. behavior is aptly represented by respective Count, Signal, and Synapse
  125. components: a rising edge condition triggers an increase function on an
  126. accumulating count datum.
  127. A counter device is not limited to a single Signal; in fact, in theory
  128. many Signals may be associated with even a single Count. For example, a
  129. quadrature encoder counter device can keep track of position based on
  130. the states of two input lines::
  131. Count Synapse Signal
  132. ----- ------- ------
  133. +-------------------------+
  134. | Data: Position | Both Edges ___
  135. | Function: Quadrature x4 | <------------ / A \
  136. | | _______
  137. | |
  138. | | Both Edges ___
  139. | | <------------ / B \
  140. | | _______
  141. +-------------------------+
  142. In this example, two Signals (quadrature encoder lines A and B) are
  143. associated with a single Count: a rising or falling edge on either A or
  144. B triggers the "Quadrature x4" function which determines the direction
  145. of movement and updates the respective position data. The "Quadrature
  146. x4" function is likely implemented in the hardware of the quadrature
  147. encoder counter device; the Count, Signals, and Synapses simply
  148. represent this hardware behavior and functionality.
  149. Signals associated with the same Count can have differing Synapse action
  150. mode conditions. For example, a quadrature encoder counter device
  151. operating in a non-quadrature Pulse-Direction mode could have one input
  152. line dedicated for movement and a second input line dedicated for
  153. direction::
  154. Count Synapse Signal
  155. ----- ------- ------
  156. +---------------------------+
  157. | Data: Position | Rising Edge ___
  158. | Function: Pulse-Direction | <------------- / A \ (Movement)
  159. | | _______
  160. | |
  161. | | None ___
  162. | | <------------- / B \ (Direction)
  163. | | _______
  164. +---------------------------+
  165. Only Signal A triggers the "Pulse-Direction" update function, but the
  166. instantaneous state of Signal B is still required in order to know the
  167. direction so that the position data may be properly updated. Ultimately,
  168. both Signals are associated with the same Count via two respective
  169. Synapses, but only one Synapse has an active action mode condition which
  170. triggers the respective count function while the other is left with a
  171. "None" condition action mode to indicate its respective Signal's
  172. availability for state evaluation despite its non-triggering mode.
  173. Keep in mind that the Signal, Synapse, and Count are abstract
  174. representations which do not need to be closely married to their
  175. respective physical sources. This allows the user of a counter to
  176. divorce themselves from the nuances of physical components (such as
  177. whether an input line is differential or single-ended) and instead focus
  178. on the core idea of what the data and process represent (e.g. position
  179. as interpreted from quadrature encoding data).
  180. Driver API
  181. ==========
  182. Driver authors may utilize the Generic Counter interface in their code
  183. by including the include/linux/counter.h header file. This header file
  184. provides several core data structures, function prototypes, and macros
  185. for defining a counter device.
  186. .. kernel-doc:: include/linux/counter.h
  187. :internal:
  188. .. kernel-doc:: drivers/counter/counter-core.c
  189. :export:
  190. .. kernel-doc:: drivers/counter/counter-chrdev.c
  191. :export:
  192. Driver Implementation
  193. =====================
  194. To support a counter device, a driver must first allocate the available
  195. Counter Signals via counter_signal structures. These Signals should
  196. be stored as an array and set to the signals array member of an
  197. allocated counter_device structure before the Counter is registered to
  198. the system.
  199. Counter Counts may be allocated via counter_count structures, and
  200. respective Counter Signal associations (Synapses) made via
  201. counter_synapse structures. Associated counter_synapse structures are
  202. stored as an array and set to the synapses array member of the
  203. respective counter_count structure. These counter_count structures are
  204. set to the counts array member of an allocated counter_device structure
  205. before the Counter is registered to the system.
  206. Driver callbacks must be provided to the counter_device structure in
  207. order to communicate with the device: to read and write various Signals
  208. and Counts, and to set and get the "action mode" and "function mode" for
  209. various Synapses and Counts respectively.
  210. A counter_device structure is allocated using counter_alloc() and then
  211. registered to the system by passing it to the counter_add() function, and
  212. unregistered by passing it to the counter_unregister function. There are
  213. device managed variants of these functions: devm_counter_alloc() and
  214. devm_counter_add().
  215. The struct counter_comp structure is used to define counter extensions
  216. for Signals, Synapses, and Counts.
  217. The "type" member specifies the type of high-level data (e.g. BOOL,
  218. COUNT_DIRECTION, etc.) handled by this extension. The "``*_read``" and
  219. "``*_write``" members can then be set by the counter device driver with
  220. callbacks to handle that data using native C data types (i.e. u8, u64,
  221. etc.).
  222. Convenience macros such as ``COUNTER_COMP_COUNT_U64`` are provided for
  223. use by driver authors. In particular, driver authors are expected to use
  224. the provided macros for standard Counter subsystem attributes in order
  225. to maintain a consistent interface for userspace. For example, a counter
  226. device driver may define several standard attributes like so::
  227. struct counter_comp count_ext[] = {
  228. COUNTER_COMP_DIRECTION(count_direction_read),
  229. COUNTER_COMP_ENABLE(count_enable_read, count_enable_write),
  230. COUNTER_COMP_CEILING(count_ceiling_read, count_ceiling_write),
  231. };
  232. This makes it simple to see, add, and modify the attributes that are
  233. supported by this driver ("direction", "enable", and "ceiling") and to
  234. maintain this code without getting lost in a web of struct braces.
  235. Callbacks must match the function type expected for the respective
  236. component or extension. These function types are defined in the struct
  237. counter_comp structure as the "``*_read``" and "``*_write``" union
  238. members.
  239. The corresponding callback prototypes for the extensions mentioned in
  240. the previous example above would be::
  241. int count_direction_read(struct counter_device *counter,
  242. struct counter_count *count,
  243. enum counter_count_direction *direction);
  244. int count_enable_read(struct counter_device *counter,
  245. struct counter_count *count, u8 *enable);
  246. int count_enable_write(struct counter_device *counter,
  247. struct counter_count *count, u8 enable);
  248. int count_ceiling_read(struct counter_device *counter,
  249. struct counter_count *count, u64 *ceiling);
  250. int count_ceiling_write(struct counter_device *counter,
  251. struct counter_count *count, u64 ceiling);
  252. Determining the type of extension to create is a matter of scope.
  253. * Signal extensions are attributes that expose information/control
  254. specific to a Signal. These types of attributes will exist under a
  255. Signal's directory in sysfs.
  256. For example, if you have an invert feature for a Signal, you can have
  257. a Signal extension called "invert" that toggles that feature:
  258. /sys/bus/counter/devices/counterX/signalY/invert
  259. * Count extensions are attributes that expose information/control
  260. specific to a Count. These type of attributes will exist under a
  261. Count's directory in sysfs.
  262. For example, if you want to pause/unpause a Count from updating, you
  263. can have a Count extension called "enable" that toggles such:
  264. /sys/bus/counter/devices/counterX/countY/enable
  265. * Device extensions are attributes that expose information/control
  266. non-specific to a particular Count or Signal. This is where you would
  267. put your global features or other miscellaneous functionality.
  268. For example, if your device has an overtemp sensor, you can report the
  269. chip overheated via a device extension called "error_overtemp":
  270. /sys/bus/counter/devices/counterX/error_overtemp
  271. Subsystem Architecture
  272. ======================
  273. Counter drivers pass and take data natively (i.e. ``u8``, ``u64``, etc.)
  274. and the shared counter module handles the translation between the sysfs
  275. interface. This guarantees a standard userspace interface for all
  276. counter drivers, and enables a Generic Counter chrdev interface via a
  277. generalized device driver ABI.
  278. A high-level view of how a count value is passed down from a counter
  279. driver is exemplified by the following. The driver callbacks are first
  280. registered to the Counter core component for use by the Counter
  281. userspace interface components::
  282. Driver callbacks registration:
  283. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  284. +----------------------------+
  285. | Counter device driver |
  286. +----------------------------+
  287. | Processes data from device |
  288. +----------------------------+
  289. |
  290. -------------------
  291. / driver callbacks /
  292. -------------------
  293. |
  294. V
  295. +----------------------+
  296. | Counter core |
  297. +----------------------+
  298. | Routes device driver |
  299. | callbacks to the |
  300. | userspace interfaces |
  301. +----------------------+
  302. |
  303. -------------------
  304. / driver callbacks /
  305. -------------------
  306. |
  307. +---------------+---------------+
  308. | |
  309. V V
  310. +--------------------+ +---------------------+
  311. | Counter sysfs | | Counter chrdev |
  312. +--------------------+ +---------------------+
  313. | Translates to the | | Translates to the |
  314. | standard Counter | | standard Counter |
  315. | sysfs output | | character device |
  316. +--------------------+ +---------------------+
  317. Thereafter, data can be transferred directly between the Counter device
  318. driver and Counter userspace interface::
  319. Count data request:
  320. ~~~~~~~~~~~~~~~~~~~
  321. ----------------------
  322. / Counter device \
  323. +----------------------+
  324. | Count register: 0x28 |
  325. +----------------------+
  326. |
  327. -----------------
  328. / raw count data /
  329. -----------------
  330. |
  331. V
  332. +----------------------------+
  333. | Counter device driver |
  334. +----------------------------+
  335. | Processes data from device |
  336. |----------------------------|
  337. | Type: u64 |
  338. | Value: 42 |
  339. +----------------------------+
  340. |
  341. ----------
  342. / u64 /
  343. ----------
  344. |
  345. +---------------+---------------+
  346. | |
  347. V V
  348. +--------------------+ +---------------------+
  349. | Counter sysfs | | Counter chrdev |
  350. +--------------------+ +---------------------+
  351. | Translates to the | | Translates to the |
  352. | standard Counter | | standard Counter |
  353. | sysfs output | | character device |
  354. |--------------------| |---------------------|
  355. | Type: const char * | | Type: u64 |
  356. | Value: "42" | | Value: 42 |
  357. +--------------------+ +---------------------+
  358. | |
  359. --------------- -----------------------
  360. / const char * / / struct counter_event /
  361. --------------- -----------------------
  362. | |
  363. | V
  364. | +-----------+
  365. | | read |
  366. | +-----------+
  367. | \ Count: 42 /
  368. | -----------
  369. |
  370. V
  371. +--------------------------------------------------+
  372. | `/sys/bus/counter/devices/counterX/countY/count` |
  373. +--------------------------------------------------+
  374. \ Count: "42" /
  375. --------------------------------------------------
  376. There are four primary components involved:
  377. Counter device driver
  378. ---------------------
  379. Communicates with the hardware device to read/write data; e.g. counter
  380. drivers for quadrature encoders, timers, etc.
  381. Counter core
  382. ------------
  383. Registers the counter device driver to the system so that the respective
  384. callbacks are called during userspace interaction.
  385. Counter sysfs
  386. -------------
  387. Translates counter data to the standard Counter sysfs interface format
  388. and vice versa.
  389. Please refer to the ``Documentation/ABI/testing/sysfs-bus-counter`` file
  390. for a detailed breakdown of the available Generic Counter interface
  391. sysfs attributes.
  392. Counter chrdev
  393. --------------
  394. Translates Counter events to the standard Counter character device; data
  395. is transferred via standard character device read calls, while Counter
  396. events are configured via ioctl calls.
  397. Sysfs Interface
  398. ===============
  399. Several sysfs attributes are generated by the Generic Counter interface,
  400. and reside under the ``/sys/bus/counter/devices/counterX`` directory,
  401. where ``X`` is to the respective counter device id. Please see
  402. ``Documentation/ABI/testing/sysfs-bus-counter`` for detailed information
  403. on each Generic Counter interface sysfs attribute.
  404. Through these sysfs attributes, programs and scripts may interact with
  405. the Generic Counter paradigm Counts, Signals, and Synapses of respective
  406. counter devices.
  407. Counter Character Device
  408. ========================
  409. Counter character device nodes are created under the ``/dev`` directory
  410. as ``counterX``, where ``X`` is the respective counter device id.
  411. Defines for the standard Counter data types are exposed via the
  412. userspace ``include/uapi/linux/counter.h`` file.
  413. Counter events
  414. --------------
  415. Counter device drivers can support Counter events by utilizing the
  416. ``counter_push_event`` function::
  417. void counter_push_event(struct counter_device *const counter, const u8 event,
  418. const u8 channel);
  419. The event id is specified by the ``event`` parameter; the event channel
  420. id is specified by the ``channel`` parameter. When this function is
  421. called, the Counter data associated with the respective event is
  422. gathered, and a ``struct counter_event`` is generated for each datum and
  423. pushed to userspace.
  424. Counter events can be configured by users to report various Counter
  425. data of interest. This can be conceptualized as a list of Counter
  426. component read calls to perform. For example:
  427. +------------------------+------------------------+
  428. | COUNTER_EVENT_OVERFLOW | COUNTER_EVENT_INDEX |
  429. +========================+========================+
  430. | Channel 0 | Channel 0 |
  431. +------------------------+------------------------+
  432. | * Count 0 | * Signal 0 |
  433. | * Count 1 | * Signal 0 Extension 0 |
  434. | * Signal 3 | * Extension 4 |
  435. | * Count 4 Extension 2 +------------------------+
  436. | * Signal 5 Extension 0 | Channel 1 |
  437. | +------------------------+
  438. | | * Signal 4 |
  439. | | * Signal 4 Extension 0 |
  440. | | * Count 7 |
  441. +------------------------+------------------------+
  442. When ``counter_push_event(counter, COUNTER_EVENT_INDEX, 1)`` is called
  443. for example, it will go down the list for the ``COUNTER_EVENT_INDEX``
  444. event channel 1 and execute the read callbacks for Signal 4, Signal 4
  445. Extension 0, and Count 7 -- the data returned for each is pushed to a
  446. kfifo as a ``struct counter_event``, which userspace can retrieve via a
  447. standard read operation on the respective character device node.
  448. Userspace
  449. ---------
  450. Userspace applications can configure Counter events via ioctl operations
  451. on the Counter character device node. There following ioctl codes are
  452. supported and provided by the ``linux/counter.h`` userspace header file:
  453. * :c:macro:`COUNTER_ADD_WATCH_IOCTL`
  454. * :c:macro:`COUNTER_ENABLE_EVENTS_IOCTL`
  455. * :c:macro:`COUNTER_DISABLE_EVENTS_IOCTL`
  456. To configure events to gather Counter data, users first populate a
  457. ``struct counter_watch`` with the relevant event id, event channel id,
  458. and the information for the desired Counter component from which to
  459. read, and then pass it via the ``COUNTER_ADD_WATCH_IOCTL`` ioctl
  460. command.
  461. Note that an event can be watched without gathering Counter data by
  462. setting the ``component.type`` member equal to
  463. ``COUNTER_COMPONENT_NONE``. With this configuration the Counter
  464. character device will simply populate the event timestamps for those
  465. respective ``struct counter_event`` elements and ignore the component
  466. value.
  467. The ``COUNTER_ADD_WATCH_IOCTL`` command will buffer these Counter
  468. watches. When ready, the ``COUNTER_ENABLE_EVENTS_IOCTL`` ioctl command
  469. may be used to activate these Counter watches.
  470. Userspace applications can then execute a ``read`` operation (optionally
  471. calling ``poll`` first) on the Counter character device node to retrieve
  472. ``struct counter_event`` elements with the desired data.