blob: 246711b76e5489e0b2ccdf9624b9388a702865ce [file] [log] [blame]
William Breathitt Gray0040a392019-04-02 15:30:36 +09001/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Counter interface
4 * Copyright (C) 2018 William Breathitt Gray
5 */
6#ifndef _COUNTER_H_
7#define _COUNTER_H_
8
William Breathitt Grayb6c50af2021-09-29 12:15:59 +09009#include <linux/cdev.h>
William Breathitt Gray0040a392019-04-02 15:30:36 +090010#include <linux/device.h>
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090011#include <linux/kernel.h>
William Breathitt Grayb6c50af2021-09-29 12:15:59 +090012#include <linux/kfifo.h>
13#include <linux/mutex.h>
14#include <linux/spinlock_types.h>
William Breathitt Gray0040a392019-04-02 15:30:36 +090015#include <linux/types.h>
William Breathitt Grayb6c50af2021-09-29 12:15:59 +090016#include <linux/wait.h>
William Breathitt Graye65c26f42021-09-29 12:15:58 +090017#include <uapi/linux/counter.h>
William Breathitt Gray0040a392019-04-02 15:30:36 +090018
William Breathitt Gray0040a392019-04-02 15:30:36 +090019struct counter_device;
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090020struct counter_count;
21struct counter_synapse;
William Breathitt Gray0040a392019-04-02 15:30:36 +090022struct counter_signal;
23
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090024enum counter_comp_type {
25 COUNTER_COMP_U8,
26 COUNTER_COMP_U64,
27 COUNTER_COMP_BOOL,
28 COUNTER_COMP_SIGNAL_LEVEL,
29 COUNTER_COMP_FUNCTION,
30 COUNTER_COMP_SYNAPSE_ACTION,
31 COUNTER_COMP_ENUM,
32 COUNTER_COMP_COUNT_DIRECTION,
33 COUNTER_COMP_COUNT_MODE,
William Breathitt Gray650ae672022-09-27 18:53:38 -040034 COUNTER_COMP_SIGNAL_POLARITY,
William Breathitt Grayd2011be12022-09-27 18:53:42 -040035 COUNTER_COMP_ARRAY,
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090036};
37
William Breathitt Gray0040a392019-04-02 15:30:36 +090038/**
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090039 * struct counter_comp - Counter component node
40 * @type: Counter component data type
41 * @name: device-specific component name
42 * @priv: component-relevant data
William Breathitt Graycca1fd42022-09-22 07:20:57 -040043 * @action_read: Synapse action mode read callback. The read value of the
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090044 * respective Synapse action mode should be passed back via
45 * the action parameter.
William Breathitt Graycca1fd42022-09-22 07:20:57 -040046 * @device_u8_read: Device u8 component read callback. The read value of the
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090047 * respective Device u8 component should be passed back via
48 * the val parameter.
William Breathitt Graycca1fd42022-09-22 07:20:57 -040049 * @count_u8_read: Count u8 component read callback. The read value of the
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090050 * respective Count u8 component should be passed back via
51 * the val parameter.
William Breathitt Graycca1fd42022-09-22 07:20:57 -040052 * @signal_u8_read: Signal u8 component read callback. The read value of the
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090053 * respective Signal u8 component should be passed back via
54 * the val parameter.
William Breathitt Graycca1fd42022-09-22 07:20:57 -040055 * @device_u32_read: Device u32 component read callback. The read value of
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090056 * the respective Device u32 component should be passed
57 * back via the val parameter.
William Breathitt Graycca1fd42022-09-22 07:20:57 -040058 * @count_u32_read: Count u32 component read callback. The read value of the
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090059 * respective Count u32 component should be passed back via
60 * the val parameter.
William Breathitt Graycca1fd42022-09-22 07:20:57 -040061 * @signal_u32_read: Signal u32 component read callback. The read value of
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090062 * the respective Signal u32 component should be passed
63 * back via the val parameter.
William Breathitt Graycca1fd42022-09-22 07:20:57 -040064 * @device_u64_read: Device u64 component read callback. The read value of
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090065 * the respective Device u64 component should be passed
66 * back via the val parameter.
William Breathitt Graycca1fd42022-09-22 07:20:57 -040067 * @count_u64_read: Count u64 component read callback. The read value of the
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090068 * respective Count u64 component should be passed back via
69 * the val parameter.
William Breathitt Graycca1fd42022-09-22 07:20:57 -040070 * @signal_u64_read: Signal u64 component read callback. The read value of
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090071 * the respective Signal u64 component should be passed
72 * back via the val parameter.
William Breathitt Grayd2011be12022-09-27 18:53:42 -040073 * @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.
William Breathitt Graycca1fd42022-09-22 07:20:57 -040097 * @action_write: Synapse action mode write callback. The write value of
William Breathitt Grayaaec1a02021-08-27 12:47:47 +090098 * the respective Synapse action mode is passed via the
99 * action parameter.
William Breathitt Graycca1fd42022-09-22 07:20:57 -0400100 * @device_u8_write: Device u8 component write callback. The write value of
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900101 * the respective Device u8 component is passed via the val
102 * parameter.
William Breathitt Graycca1fd42022-09-22 07:20:57 -0400103 * @count_u8_write: Count u8 component write callback. The write value of
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900104 * the respective Count u8 component is passed via the val
105 * parameter.
William Breathitt Graycca1fd42022-09-22 07:20:57 -0400106 * @signal_u8_write: Signal u8 component write callback. The write value of
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900107 * the respective Signal u8 component is passed via the val
108 * parameter.
William Breathitt Graycca1fd42022-09-22 07:20:57 -0400109 * @device_u32_write: Device u32 component write callback. The write value of
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900110 * the respective Device u32 component is passed via the
111 * val parameter.
William Breathitt Graycca1fd42022-09-22 07:20:57 -0400112 * @count_u32_write: Count u32 component write callback. The write value of
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900113 * the respective Count u32 component is passed via the val
114 * parameter.
William Breathitt Graycca1fd42022-09-22 07:20:57 -0400115 * @signal_u32_write: Signal u32 component write callback. The write value of
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900116 * the respective Signal u32 component is passed via the
117 * val parameter.
William Breathitt Graycca1fd42022-09-22 07:20:57 -0400118 * @device_u64_write: Device u64 component write callback. The write value of
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900119 * the respective Device u64 component is passed via the
120 * val parameter.
William Breathitt Graycca1fd42022-09-22 07:20:57 -0400121 * @count_u64_write: Count u64 component write callback. The write value of
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900122 * the respective Count u64 component is passed via the val
123 * parameter.
William Breathitt Graycca1fd42022-09-22 07:20:57 -0400124 * @signal_u64_write: Signal u64 component write callback. The write value of
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900125 * the respective Signal u64 component is passed via the
126 * val parameter.
William Breathitt Grayd2011be12022-09-27 18:53:42 -0400127 * @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
132 * the val parameter.
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
138 * the val parameter.
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
144 * the val parameter.
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
150 * the val parameter.
William Breathitt Gray0040a392019-04-02 15:30:36 +0900151 */
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900152struct counter_comp {
153 enum counter_comp_type type;
William Breathitt Gray0040a392019-04-02 15:30:36 +0900154 const char *name;
William Breathitt Gray0040a392019-04-02 15:30:36 +0900155 void *priv;
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900156 union {
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,
167 u32 *val);
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,
173 u64 *val);
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);
William Breathitt Grayd2011be12022-09-27 18:53:42 -0400178 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);
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900189 };
190 union {
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,
201 u32 val);
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,
207 u64 val);
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);
William Breathitt Grayd2011be12022-09-27 18:53:42 -0400212 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);
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900223 };
William Breathitt Gray0040a392019-04-02 15:30:36 +0900224};
225
226/**
227 * struct counter_signal - Counter Signal node
William Breathitt Gray712392f2021-08-27 12:47:48 +0900228 * @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
William Breathitt Gray0040a392019-04-02 15:30:36 +0900232 */
233struct counter_signal {
234 int id;
235 const char *name;
236
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900237 struct counter_comp *ext;
William Breathitt Gray0040a392019-04-02 15:30:36 +0900238 size_t num_ext;
William Breathitt Gray0040a392019-04-02 15:30:36 +0900239};
240
241/**
242 * struct counter_synapse - Counter Synapse node
William Breathitt Gray0040a392019-04-02 15:30:36 +0900243 * @actions_list: array of available action modes
244 * @num_actions: number of action modes specified in @actions_list
William Breathitt Gray712392f2021-08-27 12:47:48 +0900245 * @signal: pointer to the associated Signal
William Breathitt Gray0040a392019-04-02 15:30:36 +0900246 */
247struct counter_synapse {
William Breathitt Gray0040a392019-04-02 15:30:36 +0900248 const enum counter_synapse_action *actions_list;
249 size_t num_actions;
250
251 struct counter_signal *signal;
252};
253
William Breathitt Gray0040a392019-04-02 15:30:36 +0900254/**
255 * struct counter_count - Counter Count node
William Breathitt Gray712392f2021-08-27 12:47:48 +0900256 * @id: unique ID used to identify the Count
257 * @name: device-specific Count name
258 * @functions_list: array of available function modes
William Breathitt Gray0040a392019-04-02 15:30:36 +0900259 * @num_functions: number of function modes specified in @functions_list
William Breathitt Gray712392f2021-08-27 12:47:48 +0900260 * @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
William Breathitt Gray0040a392019-04-02 15:30:36 +0900264 */
265struct counter_count {
266 int id;
267 const char *name;
268
William Breathitt Gray394a0152021-08-03 21:06:15 +0900269 const enum counter_function *functions_list;
William Breathitt Gray0040a392019-04-02 15:30:36 +0900270 size_t num_functions;
271
272 struct counter_synapse *synapses;
273 size_t num_synapses;
274
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900275 struct counter_comp *ext;
William Breathitt Gray0040a392019-04-02 15:30:36 +0900276 size_t num_ext;
William Breathitt Gray0040a392019-04-02 15:30:36 +0900277};
278
279/**
William Breathitt Grayb6c50af2021-09-29 12:15:59 +0900280 * 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
285 */
286struct counter_event_node {
287 struct list_head l;
288 u8 event;
289 u8 channel;
290 struct list_head comp_list;
291};
292
293/**
William Breathitt Gray0040a392019-04-02 15:30:36 +0900294 * struct counter_ops - Callbacks from driver
William Breathitt Gray712392f2021-08-27 12:47:48 +0900295 * @signal_read: optional read callback for Signals. The read level of
296 * the respective Signal should be passed back via the
297 * level parameter.
298 * @count_read: read callback for Counts. The read value of the
299 * respective Count should be passed back via the value
300 * parameter.
301 * @count_write: optional write callback for Counts. The write value for
302 * the respective Count is passed in via the value
William Breathitt Grayd49e6ee2019-10-06 16:03:09 -0400303 * parameter.
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900304 * @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.
William Breathitt Gray712392f2021-08-27 12:47:48 +0900307 * @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.
William Breathitt Grayb6c50af2021-09-29 12:15:59 +0900316 * @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.
William Breathitt Gray0040a392019-04-02 15:30:36 +0900323 */
324struct counter_ops {
325 int (*signal_read)(struct counter_device *counter,
326 struct counter_signal *signal,
William Breathitt Gray493b9382021-08-03 21:06:14 +0900327 enum counter_signal_level *level);
William Breathitt Gray0040a392019-04-02 15:30:36 +0900328 int (*count_read)(struct counter_device *counter,
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900329 struct counter_count *count, u64 *value);
William Breathitt Gray0040a392019-04-02 15:30:36 +0900330 int (*count_write)(struct counter_device *counter,
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900331 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);
William Breathitt Grayb6c50af2021-09-29 12:15:59 +0900346 int (*events_configure)(struct counter_device *counter);
347 int (*watch_validate)(struct counter_device *counter,
348 const struct counter_watch *watch);
William Breathitt Gray0040a392019-04-02 15:30:36 +0900349};
350
351/**
William Breathitt Gray0040a392019-04-02 15:30:36 +0900352 * struct counter_device - Counter data structure
William Breathitt Gray712392f2021-08-27 12:47:48 +0900353 * @name: name of the device
William Breathitt Gray0040a392019-04-02 15:30:36 +0900354 * @parent: optional parent device providing the counters
William Breathitt Gray0040a392019-04-02 15:30:36 +0900355 * @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
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900362 * @dev: internal device structure
William Breathitt Grayb6c50af2021-09-29 12:15:59 +0900363 * @chrdev: internal character device structure
364 * @events_list: list of current watching Counter events
365 * @events_list_lock: lock to protect Counter events list operations
366 * @next_events_list: list of next watching Counter events
367 * @n_events_list_lock: lock to protect Counter next events list operations
368 * @events: queue of detected Counter events
369 * @events_wait: wait queue to allow blocking reads of Counter events
William Breathitt Gray8ac33b82021-10-21 19:35:40 +0900370 * @events_in_lock: lock to protect Counter events queue in operations
371 * @events_out_lock: lock to protect Counter events queue out operations
William Breathitt Grayb6c50af2021-09-29 12:15:59 +0900372 * @ops_exist_lock: lock to prevent use during removal
William Breathitt Gray0040a392019-04-02 15:30:36 +0900373 */
374struct counter_device {
375 const char *name;
376 struct device *parent;
William Breathitt Gray0040a392019-04-02 15:30:36 +0900377
378 const struct counter_ops *ops;
379
380 struct counter_signal *signals;
381 size_t num_signals;
382 struct counter_count *counts;
383 size_t num_counts;
384
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900385 struct counter_comp *ext;
William Breathitt Gray0040a392019-04-02 15:30:36 +0900386 size_t num_ext;
387
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900388 struct device dev;
William Breathitt Grayb6c50af2021-09-29 12:15:59 +0900389 struct cdev chrdev;
390 struct list_head events_list;
391 spinlock_t events_list_lock;
392 struct list_head next_events_list;
393 struct mutex n_events_list_lock;
394 DECLARE_KFIFO_PTR(events, struct counter_event);
395 wait_queue_head_t events_wait;
William Breathitt Gray8ac33b82021-10-21 19:35:40 +0900396 spinlock_t events_in_lock;
397 struct mutex events_out_lock;
William Breathitt Grayb6c50af2021-09-29 12:15:59 +0900398 struct mutex ops_exist_lock;
William Breathitt Gray0040a392019-04-02 15:30:36 +0900399};
400
Uwe Kleine-König5207fb22021-12-30 16:02:41 +0100401void *counter_priv(const struct counter_device *const counter);
402
Uwe Kleine-Königc18e2762021-12-30 16:02:50 +0100403struct counter_device *counter_alloc(size_t sizeof_priv);
404void counter_put(struct counter_device *const counter);
405int counter_add(struct counter_device *const counter);
406
William Breathitt Gray0040a392019-04-02 15:30:36 +0900407void counter_unregister(struct counter_device *const counter);
Uwe Kleine-Königc18e2762021-12-30 16:02:50 +0100408struct counter_device *devm_counter_alloc(struct device *dev,
409 size_t sizeof_priv);
410int devm_counter_add(struct device *dev,
411 struct counter_device *const counter);
William Breathitt Grayb6c50af2021-09-29 12:15:59 +0900412void counter_push_event(struct counter_device *const counter, const u8 event,
413 const u8 channel);
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900414
415#define COUNTER_COMP_DEVICE_U8(_name, _read, _write) \
416{ \
417 .type = COUNTER_COMP_U8, \
418 .name = (_name), \
419 .device_u8_read = (_read), \
420 .device_u8_write = (_write), \
421}
422#define COUNTER_COMP_COUNT_U8(_name, _read, _write) \
423{ \
424 .type = COUNTER_COMP_U8, \
425 .name = (_name), \
426 .count_u8_read = (_read), \
427 .count_u8_write = (_write), \
428}
429#define COUNTER_COMP_SIGNAL_U8(_name, _read, _write) \
430{ \
431 .type = COUNTER_COMP_U8, \
432 .name = (_name), \
433 .signal_u8_read = (_read), \
434 .signal_u8_write = (_write), \
435}
436
437#define COUNTER_COMP_DEVICE_U64(_name, _read, _write) \
438{ \
439 .type = COUNTER_COMP_U64, \
440 .name = (_name), \
441 .device_u64_read = (_read), \
442 .device_u64_write = (_write), \
443}
444#define COUNTER_COMP_COUNT_U64(_name, _read, _write) \
445{ \
446 .type = COUNTER_COMP_U64, \
447 .name = (_name), \
448 .count_u64_read = (_read), \
449 .count_u64_write = (_write), \
450}
451#define COUNTER_COMP_SIGNAL_U64(_name, _read, _write) \
452{ \
453 .type = COUNTER_COMP_U64, \
454 .name = (_name), \
455 .signal_u64_read = (_read), \
456 .signal_u64_write = (_write), \
457}
458
459#define COUNTER_COMP_DEVICE_BOOL(_name, _read, _write) \
460{ \
461 .type = COUNTER_COMP_BOOL, \
462 .name = (_name), \
463 .device_u8_read = (_read), \
464 .device_u8_write = (_write), \
465}
466#define COUNTER_COMP_COUNT_BOOL(_name, _read, _write) \
467{ \
468 .type = COUNTER_COMP_BOOL, \
469 .name = (_name), \
470 .count_u8_read = (_read), \
471 .count_u8_write = (_write), \
472}
473#define COUNTER_COMP_SIGNAL_BOOL(_name, _read, _write) \
474{ \
475 .type = COUNTER_COMP_BOOL, \
476 .name = (_name), \
477 .signal_u8_read = (_read), \
478 .signal_u8_write = (_write), \
479}
480
481struct counter_available {
482 union {
483 const u32 *enums;
484 const char *const *strs;
485 };
486 size_t num_items;
487};
488
489#define DEFINE_COUNTER_AVAILABLE(_name, _enums) \
490 struct counter_available _name = { \
491 .enums = (_enums), \
492 .num_items = ARRAY_SIZE(_enums), \
493 }
494
495#define DEFINE_COUNTER_ENUM(_name, _strs) \
496 struct counter_available _name = { \
497 .strs = (_strs), \
498 .num_items = ARRAY_SIZE(_strs), \
499 }
500
501#define COUNTER_COMP_DEVICE_ENUM(_name, _get, _set, _available) \
502{ \
503 .type = COUNTER_COMP_ENUM, \
504 .name = (_name), \
505 .device_u32_read = (_get), \
506 .device_u32_write = (_set), \
507 .priv = &(_available), \
508}
509#define COUNTER_COMP_COUNT_ENUM(_name, _get, _set, _available) \
510{ \
511 .type = COUNTER_COMP_ENUM, \
512 .name = (_name), \
513 .count_u32_read = (_get), \
514 .count_u32_write = (_set), \
515 .priv = &(_available), \
516}
517#define COUNTER_COMP_SIGNAL_ENUM(_name, _get, _set, _available) \
518{ \
519 .type = COUNTER_COMP_ENUM, \
520 .name = (_name), \
521 .signal_u32_read = (_get), \
522 .signal_u32_write = (_set), \
523 .priv = &(_available), \
524}
525
William Breathitt Grayd2011be12022-09-27 18:53:42 -0400526struct counter_array {
527 enum counter_comp_type type;
528 const struct counter_available *avail;
529 union {
530 size_t length;
531 size_t idx;
532 };
533};
534
535#define DEFINE_COUNTER_ARRAY_U64(_name, _length) \
536 struct counter_array _name = { \
537 .type = COUNTER_COMP_U64, \
538 .length = (_length), \
539 }
540
541#define DEFINE_COUNTER_ARRAY_CAPTURE(_name, _length) \
542 DEFINE_COUNTER_ARRAY_U64(_name, _length)
543
William Breathitt Gray472a1482022-10-02 08:04:19 -0400544#define DEFINE_COUNTER_ARRAY_POLARITY(_name, _available, _length) \
William Breathitt Grayd2011be12022-09-27 18:53:42 -0400545 struct counter_array _name = { \
546 .type = COUNTER_COMP_SIGNAL_POLARITY, \
William Breathitt Gray472a1482022-10-02 08:04:19 -0400547 .avail = &(_available), \
William Breathitt Grayd2011be12022-09-27 18:53:42 -0400548 .length = (_length), \
549 }
550
551#define COUNTER_COMP_DEVICE_ARRAY_U64(_name, _read, _write, _array) \
552{ \
553 .type = COUNTER_COMP_ARRAY, \
554 .name = (_name), \
555 .device_array_u64_read = (_read), \
556 .device_array_u64_write = (_write), \
557 .priv = &(_array), \
558}
559#define COUNTER_COMP_COUNT_ARRAY_U64(_name, _read, _write, _array) \
560{ \
561 .type = COUNTER_COMP_ARRAY, \
562 .name = (_name), \
563 .count_array_u64_read = (_read), \
564 .count_array_u64_write = (_write), \
565 .priv = &(_array), \
566}
567#define COUNTER_COMP_SIGNAL_ARRAY_U64(_name, _read, _write, _array) \
568{ \
569 .type = COUNTER_COMP_ARRAY, \
570 .name = (_name), \
571 .signal_array_u64_read = (_read), \
572 .signal_array_u64_write = (_write), \
573 .priv = &(_array), \
574}
575
William Breathitt Gray45d29182022-09-27 18:53:40 -0400576#define COUNTER_COMP_CAPTURE(_read, _write) \
577 COUNTER_COMP_COUNT_U64("capture", _read, _write)
578
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900579#define COUNTER_COMP_CEILING(_read, _write) \
580 COUNTER_COMP_COUNT_U64("ceiling", _read, _write)
581
582#define COUNTER_COMP_COUNT_MODE(_read, _write, _available) \
583{ \
584 .type = COUNTER_COMP_COUNT_MODE, \
585 .name = "count_mode", \
586 .count_u32_read = (_read), \
587 .count_u32_write = (_write), \
588 .priv = &(_available), \
589}
590
591#define COUNTER_COMP_DIRECTION(_read) \
592{ \
593 .type = COUNTER_COMP_COUNT_DIRECTION, \
594 .name = "direction", \
595 .count_u32_read = (_read), \
596}
597
598#define COUNTER_COMP_ENABLE(_read, _write) \
599 COUNTER_COMP_COUNT_BOOL("enable", _read, _write)
600
601#define COUNTER_COMP_FLOOR(_read, _write) \
602 COUNTER_COMP_COUNT_U64("floor", _read, _write)
603
William Breathitt Gray650ae672022-09-27 18:53:38 -0400604#define COUNTER_COMP_POLARITY(_read, _write, _available) \
605{ \
606 .type = COUNTER_COMP_SIGNAL_POLARITY, \
607 .name = "polarity", \
608 .signal_u32_read = (_read), \
609 .signal_u32_write = (_write), \
610 .priv = &(_available), \
611}
612
William Breathitt Grayaaec1a02021-08-27 12:47:47 +0900613#define COUNTER_COMP_PRESET(_read, _write) \
614 COUNTER_COMP_COUNT_U64("preset", _read, _write)
615
616#define COUNTER_COMP_PRESET_ENABLE(_read, _write) \
617 COUNTER_COMP_COUNT_BOOL("preset_enable", _read, _write)
William Breathitt Gray0040a392019-04-02 15:30:36 +0900618
William Breathitt Grayd2011be12022-09-27 18:53:42 -0400619#define COUNTER_COMP_ARRAY_CAPTURE(_read, _write, _array) \
620 COUNTER_COMP_COUNT_ARRAY_U64("capture", _read, _write, _array)
621
622#define COUNTER_COMP_ARRAY_POLARITY(_read, _write, _array) \
623{ \
624 .type = COUNTER_COMP_ARRAY, \
625 .name = "polarity", \
626 .signal_array_u32_read = (_read), \
627 .signal_array_u32_write = (_write), \
628 .priv = &(_array), \
629}
630
William Breathitt Gray0040a392019-04-02 15:30:36 +0900631#endif /* _COUNTER_H_ */