1 /* SPDX-License-Identifier: GPL-2.0 */
4 * Copyright (C) 2018 William Breathitt Gray
9 #include <linux/counter_enum.h>
10 #include <linux/device.h>
11 #include <linux/types.h>
13 enum counter_count_direction
{
14 COUNTER_COUNT_DIRECTION_FORWARD
= 0,
15 COUNTER_COUNT_DIRECTION_BACKWARD
17 extern const char *const counter_count_direction_str
[2];
19 enum counter_count_mode
{
20 COUNTER_COUNT_MODE_NORMAL
= 0,
21 COUNTER_COUNT_MODE_RANGE_LIMIT
,
22 COUNTER_COUNT_MODE_NON_RECYCLE
,
23 COUNTER_COUNT_MODE_MODULO_N
25 extern const char *const counter_count_mode_str
[4];
27 struct counter_device
;
28 struct counter_signal
;
31 * struct counter_signal_ext - Counter Signal extensions
32 * @name: attribute name
33 * @read: read callback for this attribute; may be NULL
34 * @write: write callback for this attribute; may be NULL
35 * @priv: data private to the driver
37 struct counter_signal_ext
{
39 ssize_t (*read
)(struct counter_device
*counter
,
40 struct counter_signal
*signal
, void *priv
, char *buf
);
41 ssize_t (*write
)(struct counter_device
*counter
,
42 struct counter_signal
*signal
, void *priv
,
43 const char *buf
, size_t len
);
48 * struct counter_signal - Counter Signal node
49 * @id: unique ID used to identify signal
50 * @name: device-specific Signal name; ideally, this should match the name
51 * as it appears in the datasheet documentation
52 * @ext: optional array of Counter Signal extensions
53 * @num_ext: number of Counter Signal extensions specified in @ext
54 * @priv: optional private data supplied by driver
56 struct counter_signal
{
60 const struct counter_signal_ext
*ext
;
67 * struct counter_signal_enum_ext - Signal enum extension attribute
68 * @items: Array of strings
69 * @num_items: Number of items specified in @items
70 * @set: Set callback function; may be NULL
71 * @get: Get callback function; may be NULL
73 * The counter_signal_enum_ext structure can be used to implement enum style
74 * Signal extension attributes. Enum style attributes are those which have a set
75 * of strings that map to unsigned integer values. The Generic Counter Signal
76 * enum extension helper code takes care of mapping between value and string, as
77 * well as generating a "_available" file which contains a list of all available
78 * items. The get callback is used to query the currently active item; the index
79 * of the item within the respective items array is returned via the 'item'
80 * parameter. The set callback is called when the attribute is updated; the
81 * 'item' parameter contains the index of the newly activated item within the
82 * respective items array.
84 struct counter_signal_enum_ext
{
85 const char * const *items
;
87 int (*get
)(struct counter_device
*counter
,
88 struct counter_signal
*signal
, size_t *item
);
89 int (*set
)(struct counter_device
*counter
,
90 struct counter_signal
*signal
, size_t item
);
94 * COUNTER_SIGNAL_ENUM() - Initialize Signal enum extension
95 * @_name: Attribute name
96 * @_e: Pointer to a counter_signal_enum_ext structure
98 * This should usually be used together with COUNTER_SIGNAL_ENUM_AVAILABLE()
100 #define COUNTER_SIGNAL_ENUM(_name, _e) \
103 .read = counter_signal_enum_read, \
104 .write = counter_signal_enum_write, \
109 * COUNTER_SIGNAL_ENUM_AVAILABLE() - Initialize Signal enum available extension
110 * @_name: Attribute name ("_available" will be appended to the name)
111 * @_e: Pointer to a counter_signal_enum_ext structure
113 * Creates a read only attribute that lists all the available enum items in a
114 * newline separated list. This should usually be used together with
115 * COUNTER_SIGNAL_ENUM()
117 #define COUNTER_SIGNAL_ENUM_AVAILABLE(_name, _e) \
119 .name = (_name "_available"), \
120 .read = counter_signal_enum_available_read, \
124 enum counter_synapse_action
{
125 COUNTER_SYNAPSE_ACTION_NONE
= 0,
126 COUNTER_SYNAPSE_ACTION_RISING_EDGE
,
127 COUNTER_SYNAPSE_ACTION_FALLING_EDGE
,
128 COUNTER_SYNAPSE_ACTION_BOTH_EDGES
132 * struct counter_synapse - Counter Synapse node
133 * @action: index of current action mode
134 * @actions_list: array of available action modes
135 * @num_actions: number of action modes specified in @actions_list
136 * @signal: pointer to associated signal
138 struct counter_synapse
{
140 const enum counter_synapse_action
*actions_list
;
143 struct counter_signal
*signal
;
146 struct counter_count
;
149 * struct counter_count_ext - Counter Count extension
150 * @name: attribute name
151 * @read: read callback for this attribute; may be NULL
152 * @write: write callback for this attribute; may be NULL
153 * @priv: data private to the driver
155 struct counter_count_ext
{
157 ssize_t (*read
)(struct counter_device
*counter
,
158 struct counter_count
*count
, void *priv
, char *buf
);
159 ssize_t (*write
)(struct counter_device
*counter
,
160 struct counter_count
*count
, void *priv
,
161 const char *buf
, size_t len
);
165 enum counter_count_function
{
166 COUNTER_COUNT_FUNCTION_INCREASE
= 0,
167 COUNTER_COUNT_FUNCTION_DECREASE
,
168 COUNTER_COUNT_FUNCTION_PULSE_DIRECTION
,
169 COUNTER_COUNT_FUNCTION_QUADRATURE_X1_A
,
170 COUNTER_COUNT_FUNCTION_QUADRATURE_X1_B
,
171 COUNTER_COUNT_FUNCTION_QUADRATURE_X2_A
,
172 COUNTER_COUNT_FUNCTION_QUADRATURE_X2_B
,
173 COUNTER_COUNT_FUNCTION_QUADRATURE_X4
177 * struct counter_count - Counter Count node
178 * @id: unique ID used to identify Count
179 * @name: device-specific Count name; ideally, this should match
180 * the name as it appears in the datasheet documentation
181 * @function: index of current function mode
182 * @functions_list: array available function modes
183 * @num_functions: number of function modes specified in @functions_list
184 * @synapses: array of synapses for initialization
185 * @num_synapses: number of synapses specified in @synapses
186 * @ext: optional array of Counter Count extensions
187 * @num_ext: number of Counter Count extensions specified in @ext
188 * @priv: optional private data supplied by driver
190 struct counter_count
{
195 const enum counter_count_function
*functions_list
;
196 size_t num_functions
;
198 struct counter_synapse
*synapses
;
201 const struct counter_count_ext
*ext
;
208 * struct counter_count_enum_ext - Count enum extension attribute
209 * @items: Array of strings
210 * @num_items: Number of items specified in @items
211 * @set: Set callback function; may be NULL
212 * @get: Get callback function; may be NULL
214 * The counter_count_enum_ext structure can be used to implement enum style
215 * Count extension attributes. Enum style attributes are those which have a set
216 * of strings that map to unsigned integer values. The Generic Counter Count
217 * enum extension helper code takes care of mapping between value and string, as
218 * well as generating a "_available" file which contains a list of all available
219 * items. The get callback is used to query the currently active item; the index
220 * of the item within the respective items array is returned via the 'item'
221 * parameter. The set callback is called when the attribute is updated; the
222 * 'item' parameter contains the index of the newly activated item within the
223 * respective items array.
225 struct counter_count_enum_ext
{
226 const char * const *items
;
228 int (*get
)(struct counter_device
*counter
, struct counter_count
*count
,
230 int (*set
)(struct counter_device
*counter
, struct counter_count
*count
,
235 * COUNTER_COUNT_ENUM() - Initialize Count enum extension
236 * @_name: Attribute name
237 * @_e: Pointer to a counter_count_enum_ext structure
239 * This should usually be used together with COUNTER_COUNT_ENUM_AVAILABLE()
241 #define COUNTER_COUNT_ENUM(_name, _e) \
244 .read = counter_count_enum_read, \
245 .write = counter_count_enum_write, \
250 * COUNTER_COUNT_ENUM_AVAILABLE() - Initialize Count enum available extension
251 * @_name: Attribute name ("_available" will be appended to the name)
252 * @_e: Pointer to a counter_count_enum_ext structure
254 * Creates a read only attribute that lists all the available enum items in a
255 * newline separated list. This should usually be used together with
256 * COUNTER_COUNT_ENUM()
258 #define COUNTER_COUNT_ENUM_AVAILABLE(_name, _e) \
260 .name = (_name "_available"), \
261 .read = counter_count_enum_available_read, \
266 * struct counter_device_attr_group - internal container for attribute group
267 * @attr_group: Counter sysfs attributes group
268 * @attr_list: list to keep track of created Counter sysfs attributes
269 * @num_attr: number of Counter sysfs attributes
271 struct counter_device_attr_group
{
272 struct attribute_group attr_group
;
273 struct list_head attr_list
;
278 * struct counter_device_state - internal state container for a Counter device
279 * @id: unique ID used to identify the Counter
280 * @dev: internal device structure
281 * @groups_list: attribute groups list (for Signals, Counts, and ext)
282 * @num_groups: number of attribute groups containers
283 * @groups: Counter sysfs attribute groups (to populate @dev.groups)
285 struct counter_device_state
{
288 struct counter_device_attr_group
*groups_list
;
290 const struct attribute_group
**groups
;
293 enum counter_signal_value
{
294 COUNTER_SIGNAL_LOW
= 0,
299 * struct counter_ops - Callbacks from driver
300 * @signal_read: optional read callback for Signal attribute. The read
301 * value of the respective Signal should be passed back via
303 * @count_read: optional read callback for Count attribute. The read
304 * value of the respective Count should be passed back via
306 * @count_write: optional write callback for Count attribute. The write
307 * value for the respective Count is passed in via the val
309 * @function_get: function to get the current count function mode. Returns
310 * 0 on success and negative error code on error. The index
311 * of the respective Count's returned function mode should
312 * be passed back via the function parameter.
313 * @function_set: function to set the count function mode. function is the
314 * index of the requested function mode from the respective
315 * Count's functions_list array.
316 * @action_get: function to get the current action mode. Returns 0 on
317 * success and negative error code on error. The index of
318 * the respective Synapse's returned action mode should be
319 * passed back via the action parameter.
320 * @action_set: function to set the action mode. action is the index of
321 * the requested action mode from the respective Synapse's
322 * actions_list array.
325 int (*signal_read
)(struct counter_device
*counter
,
326 struct counter_signal
*signal
,
327 enum counter_signal_value
*val
);
328 int (*count_read
)(struct counter_device
*counter
,
329 struct counter_count
*count
, unsigned long *val
);
330 int (*count_write
)(struct counter_device
*counter
,
331 struct counter_count
*count
, unsigned long val
);
332 int (*function_get
)(struct counter_device
*counter
,
333 struct counter_count
*count
, size_t *function
);
334 int (*function_set
)(struct counter_device
*counter
,
335 struct counter_count
*count
, size_t function
);
336 int (*action_get
)(struct counter_device
*counter
,
337 struct counter_count
*count
,
338 struct counter_synapse
*synapse
, size_t *action
);
339 int (*action_set
)(struct counter_device
*counter
,
340 struct counter_count
*count
,
341 struct counter_synapse
*synapse
, size_t action
);
345 * struct counter_device_ext - Counter device extension
346 * @name: attribute name
347 * @read: read callback for this attribute; may be NULL
348 * @write: write callback for this attribute; may be NULL
349 * @priv: data private to the driver
351 struct counter_device_ext
{
353 ssize_t (*read
)(struct counter_device
*counter
, void *priv
, char *buf
);
354 ssize_t (*write
)(struct counter_device
*counter
, void *priv
,
355 const char *buf
, size_t len
);
360 * struct counter_device_enum_ext - Counter enum extension attribute
361 * @items: Array of strings
362 * @num_items: Number of items specified in @items
363 * @set: Set callback function; may be NULL
364 * @get: Get callback function; may be NULL
366 * The counter_device_enum_ext structure can be used to implement enum style
367 * Counter extension attributes. Enum style attributes are those which have a
368 * set of strings that map to unsigned integer values. The Generic Counter enum
369 * extension helper code takes care of mapping between value and string, as well
370 * as generating a "_available" file which contains a list of all available
371 * items. The get callback is used to query the currently active item; the index
372 * of the item within the respective items array is returned via the 'item'
373 * parameter. The set callback is called when the attribute is updated; the
374 * 'item' parameter contains the index of the newly activated item within the
375 * respective items array.
377 struct counter_device_enum_ext
{
378 const char * const *items
;
380 int (*get
)(struct counter_device
*counter
, size_t *item
);
381 int (*set
)(struct counter_device
*counter
, size_t item
);
385 * COUNTER_DEVICE_ENUM() - Initialize Counter enum extension
386 * @_name: Attribute name
387 * @_e: Pointer to a counter_device_enum_ext structure
389 * This should usually be used together with COUNTER_DEVICE_ENUM_AVAILABLE()
391 #define COUNTER_DEVICE_ENUM(_name, _e) \
394 .read = counter_device_enum_read, \
395 .write = counter_device_enum_write, \
400 * COUNTER_DEVICE_ENUM_AVAILABLE() - Initialize Counter enum available extension
401 * @_name: Attribute name ("_available" will be appended to the name)
402 * @_e: Pointer to a counter_device_enum_ext structure
404 * Creates a read only attribute that lists all the available enum items in a
405 * newline separated list. This should usually be used together with
406 * COUNTER_DEVICE_ENUM()
408 #define COUNTER_DEVICE_ENUM_AVAILABLE(_name, _e) \
410 .name = (_name "_available"), \
411 .read = counter_device_enum_available_read, \
416 * struct counter_device - Counter data structure
417 * @name: name of the device as it appears in the datasheet
418 * @parent: optional parent device providing the counters
419 * @device_state: internal device state container
420 * @ops: callbacks from driver
421 * @signals: array of Signals
422 * @num_signals: number of Signals specified in @signals
423 * @counts: array of Counts
424 * @num_counts: number of Counts specified in @counts
425 * @ext: optional array of Counter device extensions
426 * @num_ext: number of Counter device extensions specified in @ext
427 * @priv: optional private data supplied by driver
429 struct counter_device
{
431 struct device
*parent
;
432 struct counter_device_state
*device_state
;
434 const struct counter_ops
*ops
;
436 struct counter_signal
*signals
;
438 struct counter_count
*counts
;
441 const struct counter_device_ext
*ext
;
447 int counter_register(struct counter_device
*const counter
);
448 void counter_unregister(struct counter_device
*const counter
);
449 int devm_counter_register(struct device
*dev
,
450 struct counter_device
*const counter
);
451 void devm_counter_unregister(struct device
*dev
,
452 struct counter_device
*const counter
);
454 #endif /* _COUNTER_H_ */