1 /* SPDX-License-Identifier: GPL-2.0 */
4 * Copyright (C) 2018 William Breathitt Gray
9 #include <linux/cdev.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/kfifo.h>
13 #include <linux/mutex.h>
14 #include <linux/spinlock_types.h>
15 #include <linux/types.h>
16 #include <linux/wait.h>
17 #include <uapi/linux/counter.h>
19 struct counter_device;
21 struct counter_synapse;
22 struct counter_signal;
24 enum counter_comp_type {
28 COUNTER_COMP_SIGNAL_LEVEL,
29 COUNTER_COMP_FUNCTION,
30 COUNTER_COMP_SYNAPSE_ACTION,
32 COUNTER_COMP_COUNT_DIRECTION,
33 COUNTER_COMP_COUNT_MODE,
34 COUNTER_COMP_SIGNAL_POLARITY,
39 * struct counter_comp - Counter component node
40 * @type: Counter component data type
41 * @name: device-specific component name
42 * @priv: component-relevant data
43 * @action_read: Synapse action mode read callback. The read value of the
44 * respective Synapse action mode should be passed back via
45 * the action parameter.
46 * @device_u8_read: Device u8 component read callback. The read value of the
47 * respective Device u8 component should be passed back via
49 * @count_u8_read: Count u8 component read callback. The read value of the
50 * respective Count u8 component should be passed back via
52 * @signal_u8_read: Signal u8 component read callback. The read value of the
53 * respective Signal u8 component should be passed back via
55 * @device_u32_read: Device u32 component read callback. The read value of
56 * the respective Device u32 component should be passed
57 * back via the val parameter.
58 * @count_u32_read: Count u32 component read callback. The read value of the
59 * respective Count u32 component should be passed back via
61 * @signal_u32_read: Signal u32 component read callback. The read value of
62 * the respective Signal u32 component should be passed
63 * back via the val parameter.
64 * @device_u64_read: Device u64 component read callback. The read value of
65 * the respective Device u64 component should be passed
66 * back via the val parameter.
67 * @count_u64_read: Count u64 component read callback. The read value of the
68 * respective Count u64 component should be passed back via
70 * @signal_u64_read: Signal u64 component read callback. The read value of
71 * the respective Signal u64 component should be passed
72 * back via the val parameter.
73 * @signal_array_u32_read: Signal u32 array component read callback. The
74 * index of the respective Count u32 array
75 * component element is passed via the idx
76 * parameter. The read value of the respective
77 * Count u32 array component element should be
78 * passed back via the val parameter.
79 * @device_array_u64_read: Device u64 array component read callback. The
80 * index of the respective Device u64 array
81 * component element is passed via the idx
82 * parameter. The read value of the respective
83 * Device u64 array component element should be
84 * passed back via the val parameter.
85 * @count_array_u64_read: Count u64 array component read callback. The
86 * index of the respective Count u64 array
87 * component element is passed via the idx
88 * parameter. The read value of the respective
89 * Count u64 array component element should be
90 * passed back via the val parameter.
91 * @signal_array_u64_read: Signal u64 array component read callback. The
92 * index of the respective Count u64 array
93 * component element is passed via the idx
94 * parameter. The read value of the respective
95 * Count u64 array component element should be
96 * passed back via the val parameter.
97 * @action_write: Synapse action mode write callback. The write value of
98 * the respective Synapse action mode is passed via the
100 * @device_u8_write: Device u8 component write callback. The write value of
101 * the respective Device u8 component is passed via the val
103 * @count_u8_write: Count u8 component write callback. The write value of
104 * the respective Count u8 component is passed via the val
106 * @signal_u8_write: Signal u8 component write callback. The write value of
107 * the respective Signal u8 component is passed via the val
109 * @device_u32_write: Device u32 component write callback. The write value of
110 * the respective Device u32 component is passed via the
112 * @count_u32_write: Count u32 component write callback. The write value of
113 * the respective Count u32 component is passed via the val
115 * @signal_u32_write: Signal u32 component write callback. The write value of
116 * the respective Signal u32 component is passed via the
118 * @device_u64_write: Device u64 component write callback. The write value of
119 * the respective Device u64 component is passed via the
121 * @count_u64_write: Count u64 component write callback. The write value of
122 * the respective Count u64 component is passed via the val
124 * @signal_u64_write: Signal u64 component write callback. The write value of
125 * the respective Signal u64 component is passed via the
127 * @signal_array_u32_write: Signal u32 array component write callback. The
128 * index of the respective Signal u32 array
129 * component element is passed via the idx
130 * parameter. The write value of the respective
131 * Signal u32 array component element is passed via
133 * @device_array_u64_write: Device u64 array component write callback. The
134 * index of the respective Device u64 array
135 * component element is passed via the idx
136 * parameter. The write value of the respective
137 * Device u64 array component element is passed via
139 * @count_array_u64_write: Count u64 array component write callback. The
140 * index of the respective Count u64 array
141 * component element is passed via the idx
142 * parameter. The write value of the respective
143 * Count u64 array component element is passed via
145 * @signal_array_u64_write: Signal u64 array component write callback. The
146 * index of the respective Signal u64 array
147 * component element is passed via the idx
148 * parameter. The write value of the respective
149 * Signal u64 array component element is passed via
152 struct counter_comp {
153 enum counter_comp_type type;
157 int (*action_read)(struct counter_device *counter,
158 struct counter_count *count,
159 struct counter_synapse *synapse,
160 enum counter_synapse_action *action);
161 int (*device_u8_read)(struct counter_device *counter, u8 *val);
162 int (*count_u8_read)(struct counter_device *counter,
163 struct counter_count *count, u8 *val);
164 int (*signal_u8_read)(struct counter_device *counter,
165 struct counter_signal *signal, u8 *val);
166 int (*device_u32_read)(struct counter_device *counter,
168 int (*count_u32_read)(struct counter_device *counter,
169 struct counter_count *count, u32 *val);
170 int (*signal_u32_read)(struct counter_device *counter,
171 struct counter_signal *signal, u32 *val);
172 int (*device_u64_read)(struct counter_device *counter,
174 int (*count_u64_read)(struct counter_device *counter,
175 struct counter_count *count, u64 *val);
176 int (*signal_u64_read)(struct counter_device *counter,
177 struct counter_signal *signal, u64 *val);
178 int (*signal_array_u32_read)(struct counter_device *counter,
179 struct counter_signal *signal,
180 size_t idx, u32 *val);
181 int (*device_array_u64_read)(struct counter_device *counter,
182 size_t idx, u64 *val);
183 int (*count_array_u64_read)(struct counter_device *counter,
184 struct counter_count *count,
185 size_t idx, u64 *val);
186 int (*signal_array_u64_read)(struct counter_device *counter,
187 struct counter_signal *signal,
188 size_t idx, u64 *val);
191 int (*action_write)(struct counter_device *counter,
192 struct counter_count *count,
193 struct counter_synapse *synapse,
194 enum counter_synapse_action action);
195 int (*device_u8_write)(struct counter_device *counter, u8 val);
196 int (*count_u8_write)(struct counter_device *counter,
197 struct counter_count *count, u8 val);
198 int (*signal_u8_write)(struct counter_device *counter,
199 struct counter_signal *signal, u8 val);
200 int (*device_u32_write)(struct counter_device *counter,
202 int (*count_u32_write)(struct counter_device *counter,
203 struct counter_count *count, u32 val);
204 int (*signal_u32_write)(struct counter_device *counter,
205 struct counter_signal *signal, u32 val);
206 int (*device_u64_write)(struct counter_device *counter,
208 int (*count_u64_write)(struct counter_device *counter,
209 struct counter_count *count, u64 val);
210 int (*signal_u64_write)(struct counter_device *counter,
211 struct counter_signal *signal, u64 val);
212 int (*signal_array_u32_write)(struct counter_device *counter,
213 struct counter_signal *signal,
214 size_t idx, u32 val);
215 int (*device_array_u64_write)(struct counter_device *counter,
216 size_t idx, u64 val);
217 int (*count_array_u64_write)(struct counter_device *counter,
218 struct counter_count *count,
219 size_t idx, u64 val);
220 int (*signal_array_u64_write)(struct counter_device *counter,
221 struct counter_signal *signal,
222 size_t idx, u64 val);
227 * struct counter_signal - Counter Signal node
228 * @id: unique ID used to identify the Signal
229 * @name: device-specific Signal name
230 * @ext: optional array of Signal extensions
231 * @num_ext: number of Signal extensions specified in @ext
233 struct counter_signal {
237 struct counter_comp *ext;
242 * struct counter_synapse - Counter Synapse node
243 * @actions_list: array of available action modes
244 * @num_actions: number of action modes specified in @actions_list
245 * @signal: pointer to the associated Signal
247 struct counter_synapse {
248 const enum counter_synapse_action *actions_list;
251 struct counter_signal *signal;
255 * struct counter_count - Counter Count node
256 * @id: unique ID used to identify the Count
257 * @name: device-specific Count name
258 * @functions_list: array of available function modes
259 * @num_functions: number of function modes specified in @functions_list
260 * @synapses: array of Synapses for initialization
261 * @num_synapses: number of Synapses specified in @synapses
262 * @ext: optional array of Count extensions
263 * @num_ext: number of Count extensions specified in @ext
265 struct counter_count {
269 const enum counter_function *functions_list;
270 size_t num_functions;
272 struct counter_synapse *synapses;
275 struct counter_comp *ext;
280 * struct counter_event_node - Counter Event node
281 * @l: list of current watching Counter events
282 * @event: event that triggers
283 * @channel: event channel
284 * @comp_list: list of components to watch when event triggers
286 struct counter_event_node {
290 struct list_head comp_list;
294 * struct counter_ops - Callbacks from driver
295 * @signal_read: optional read callback for Signals. The read level of
296 * the respective Signal should be passed back via the
298 * @count_read: read callback for Counts. The read value of the
299 * respective Count should be passed back via the value
301 * @count_write: optional write callback for Counts. The write value for
302 * the respective Count is passed in via the value
304 * @function_read: read callback the Count function modes. The read
305 * function mode of the respective Count should be passed
306 * back via the function parameter.
307 * @function_write: optional write callback for Count function modes. The
308 * function mode to write for the respective Count is
309 * passed in via the function parameter.
310 * @action_read: optional read callback the Synapse action modes. The
311 * read action mode of the respective Synapse should be
312 * passed back via the action parameter.
313 * @action_write: optional write callback for Synapse action modes. The
314 * action mode to write for the respective Synapse is
315 * passed in via the action parameter.
316 * @events_configure: optional write callback to configure events. The list of
317 * struct counter_event_node may be accessed via the
318 * events_list member of the counter parameter.
319 * @watch_validate: optional callback to validate a watch. The Counter
320 * component watch configuration is passed in via the watch
321 * parameter. A return value of 0 indicates a valid Counter
322 * component watch configuration.
325 int (*signal_read)(struct counter_device *counter,
326 struct counter_signal *signal,
327 enum counter_signal_level *level);
328 int (*count_read)(struct counter_device *counter,
329 struct counter_count *count, u64 *value);
330 int (*count_write)(struct counter_device *counter,
331 struct counter_count *count, u64 value);
332 int (*function_read)(struct counter_device *counter,
333 struct counter_count *count,
334 enum counter_function *function);
335 int (*function_write)(struct counter_device *counter,
336 struct counter_count *count,
337 enum counter_function function);
338 int (*action_read)(struct counter_device *counter,
339 struct counter_count *count,
340 struct counter_synapse *synapse,
341 enum counter_synapse_action *action);
342 int (*action_write)(struct counter_device *counter,
343 struct counter_count *count,
344 struct counter_synapse *synapse,
345 enum counter_synapse_action action);
346 int (*events_configure)(struct counter_device *counter);
347 int (*watch_validate)(struct counter_device *counter,
348 const struct counter_watch *watch);
352 * struct counter_device - Counter data structure
353 * @name: name of the device
354 * @parent: optional parent device providing the counters
355 * @ops: callbacks from driver
356 * @signals: array of Signals
357 * @num_signals: number of Signals specified in @signals
358 * @counts: array of Counts
359 * @num_counts: number of Counts specified in @counts
360 * @ext: optional array of Counter device extensions
361 * @num_ext: number of Counter device extensions specified in @ext
362 * @priv: optional private data supplied by driver
363 * @dev: internal device structure
364 * @chrdev: internal character device structure
365 * @events_list: list of current watching Counter events
366 * @events_list_lock: lock to protect Counter events list operations
367 * @next_events_list: list of next watching Counter events
368 * @n_events_list_lock: lock to protect Counter next events list operations
369 * @events: queue of detected Counter events
370 * @events_wait: wait queue to allow blocking reads of Counter events
371 * @events_in_lock: lock to protect Counter events queue in operations
372 * @events_out_lock: lock to protect Counter events queue out operations
373 * @ops_exist_lock: lock to prevent use during removal
375 struct counter_device {
377 struct device *parent;
379 const struct counter_ops *ops;
381 struct counter_signal *signals;
383 struct counter_count *counts;
386 struct counter_comp *ext;
391 struct list_head events_list;
392 spinlock_t events_list_lock;
393 struct list_head next_events_list;
394 struct mutex n_events_list_lock;
395 DECLARE_KFIFO_PTR(events, struct counter_event);
396 wait_queue_head_t events_wait;
397 spinlock_t events_in_lock;
398 struct mutex events_out_lock;
399 struct mutex ops_exist_lock;
402 void *counter_priv(const struct counter_device *const counter) __attribute_const__;
404 struct counter_device *counter_alloc(size_t sizeof_priv);
405 void counter_put(struct counter_device *const counter);
406 int counter_add(struct counter_device *const counter);
408 void counter_unregister(struct counter_device *const counter);
409 struct counter_device *devm_counter_alloc(struct device *dev,
411 int devm_counter_add(struct device *dev,
412 struct counter_device *const counter);
413 void counter_push_event(struct counter_device *const counter, const u8 event,
416 #define COUNTER_COMP_DEVICE_U8(_name, _read, _write) \
418 .type = COUNTER_COMP_U8, \
420 .device_u8_read = (_read), \
421 .device_u8_write = (_write), \
423 #define COUNTER_COMP_COUNT_U8(_name, _read, _write) \
425 .type = COUNTER_COMP_U8, \
427 .count_u8_read = (_read), \
428 .count_u8_write = (_write), \
430 #define COUNTER_COMP_SIGNAL_U8(_name, _read, _write) \
432 .type = COUNTER_COMP_U8, \
434 .signal_u8_read = (_read), \
435 .signal_u8_write = (_write), \
438 #define COUNTER_COMP_DEVICE_U64(_name, _read, _write) \
440 .type = COUNTER_COMP_U64, \
442 .device_u64_read = (_read), \
443 .device_u64_write = (_write), \
445 #define COUNTER_COMP_COUNT_U64(_name, _read, _write) \
447 .type = COUNTER_COMP_U64, \
449 .count_u64_read = (_read), \
450 .count_u64_write = (_write), \
452 #define COUNTER_COMP_SIGNAL_U64(_name, _read, _write) \
454 .type = COUNTER_COMP_U64, \
456 .signal_u64_read = (_read), \
457 .signal_u64_write = (_write), \
460 #define COUNTER_COMP_DEVICE_BOOL(_name, _read, _write) \
462 .type = COUNTER_COMP_BOOL, \
464 .device_u8_read = (_read), \
465 .device_u8_write = (_write), \
467 #define COUNTER_COMP_COUNT_BOOL(_name, _read, _write) \
469 .type = COUNTER_COMP_BOOL, \
471 .count_u8_read = (_read), \
472 .count_u8_write = (_write), \
474 #define COUNTER_COMP_SIGNAL_BOOL(_name, _read, _write) \
476 .type = COUNTER_COMP_BOOL, \
478 .signal_u8_read = (_read), \
479 .signal_u8_write = (_write), \
482 struct counter_available {
485 const char *const *strs;
490 #define DEFINE_COUNTER_AVAILABLE(_name, _enums) \
491 struct counter_available _name = { \
493 .num_items = ARRAY_SIZE(_enums), \
496 #define DEFINE_COUNTER_ENUM(_name, _strs) \
497 struct counter_available _name = { \
499 .num_items = ARRAY_SIZE(_strs), \
502 #define COUNTER_COMP_DEVICE_ENUM(_name, _get, _set, _available) \
504 .type = COUNTER_COMP_ENUM, \
506 .device_u32_read = (_get), \
507 .device_u32_write = (_set), \
508 .priv = &(_available), \
510 #define COUNTER_COMP_COUNT_ENUM(_name, _get, _set, _available) \
512 .type = COUNTER_COMP_ENUM, \
514 .count_u32_read = (_get), \
515 .count_u32_write = (_set), \
516 .priv = &(_available), \
518 #define COUNTER_COMP_SIGNAL_ENUM(_name, _get, _set, _available) \
520 .type = COUNTER_COMP_ENUM, \
522 .signal_u32_read = (_get), \
523 .signal_u32_write = (_set), \
524 .priv = &(_available), \
527 struct counter_array {
528 enum counter_comp_type type;
529 const struct counter_available *avail;
536 #define DEFINE_COUNTER_ARRAY_U64(_name, _length) \
537 struct counter_array _name = { \
538 .type = COUNTER_COMP_U64, \
539 .length = (_length), \
542 #define DEFINE_COUNTER_ARRAY_CAPTURE(_name, _length) \
543 DEFINE_COUNTER_ARRAY_U64(_name, _length)
545 #define DEFINE_COUNTER_ARRAY_POLARITY(_name, _available, _length) \
546 struct counter_array _name = { \
547 .type = COUNTER_COMP_SIGNAL_POLARITY, \
548 .avail = &(_available), \
549 .length = (_length), \
552 #define COUNTER_COMP_DEVICE_ARRAY_U64(_name, _read, _write, _array) \
554 .type = COUNTER_COMP_ARRAY, \
556 .device_array_u64_read = (_read), \
557 .device_array_u64_write = (_write), \
560 #define COUNTER_COMP_COUNT_ARRAY_U64(_name, _read, _write, _array) \
562 .type = COUNTER_COMP_ARRAY, \
564 .count_array_u64_read = (_read), \
565 .count_array_u64_write = (_write), \
568 #define COUNTER_COMP_SIGNAL_ARRAY_U64(_name, _read, _write, _array) \
570 .type = COUNTER_COMP_ARRAY, \
572 .signal_array_u64_read = (_read), \
573 .signal_array_u64_write = (_write), \
577 #define COUNTER_COMP_CAPTURE(_read, _write) \
578 COUNTER_COMP_COUNT_U64("capture", _read, _write)
580 #define COUNTER_COMP_CEILING(_read, _write) \
581 COUNTER_COMP_COUNT_U64("ceiling", _read, _write)
583 #define COUNTER_COMP_COUNT_MODE(_read, _write, _available) \
585 .type = COUNTER_COMP_COUNT_MODE, \
586 .name = "count_mode", \
587 .count_u32_read = (_read), \
588 .count_u32_write = (_write), \
589 .priv = &(_available), \
592 #define COUNTER_COMP_DIRECTION(_read) \
594 .type = COUNTER_COMP_COUNT_DIRECTION, \
595 .name = "direction", \
596 .count_u32_read = (_read), \
599 #define COUNTER_COMP_ENABLE(_read, _write) \
600 COUNTER_COMP_COUNT_BOOL("enable", _read, _write)
602 #define COUNTER_COMP_FLOOR(_read, _write) \
603 COUNTER_COMP_COUNT_U64("floor", _read, _write)
605 #define COUNTER_COMP_POLARITY(_read, _write, _available) \
607 .type = COUNTER_COMP_SIGNAL_POLARITY, \
608 .name = "polarity", \
609 .signal_u32_read = (_read), \
610 .signal_u32_write = (_write), \
611 .priv = &(_available), \
614 #define COUNTER_COMP_PRESET(_read, _write) \
615 COUNTER_COMP_COUNT_U64("preset", _read, _write)
617 #define COUNTER_COMP_PRESET_ENABLE(_read, _write) \
618 COUNTER_COMP_COUNT_BOOL("preset_enable", _read, _write)
620 #define COUNTER_COMP_ARRAY_CAPTURE(_read, _write, _array) \
621 COUNTER_COMP_COUNT_ARRAY_U64("capture", _read, _write, _array)
623 #define COUNTER_COMP_ARRAY_POLARITY(_read, _write, _array) \
625 .type = COUNTER_COMP_ARRAY, \
626 .name = "polarity", \
627 .signal_array_u32_read = (_read), \
628 .signal_array_u32_write = (_write), \
632 #endif /* _COUNTER_H_ */