1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef SOUND_FIREWIRE_AMDTP_H_INCLUDED
3*4882a593Smuzhiyun #define SOUND_FIREWIRE_AMDTP_H_INCLUDED
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun #include <linux/err.h>
6*4882a593Smuzhiyun #include <linux/interrupt.h>
7*4882a593Smuzhiyun #include <linux/mutex.h>
8*4882a593Smuzhiyun #include <linux/sched.h>
9*4882a593Smuzhiyun #include <sound/asound.h>
10*4882a593Smuzhiyun #include "packets-buffer.h"
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun /**
13*4882a593Smuzhiyun * enum cip_flags - describes details of the streaming protocol
14*4882a593Smuzhiyun * @CIP_NONBLOCKING: In non-blocking mode, each packet contains
15*4882a593Smuzhiyun * sample_rate/8000 samples, with rounding up or down to adjust
16*4882a593Smuzhiyun * for clock skew and left-over fractional samples. This should
17*4882a593Smuzhiyun * be used if supported by the device.
18*4882a593Smuzhiyun * @CIP_BLOCKING: In blocking mode, each packet contains either zero or
19*4882a593Smuzhiyun * SYT_INTERVAL samples, with these two types alternating so that
20*4882a593Smuzhiyun * the overall sample rate comes out right.
21*4882a593Smuzhiyun * @CIP_EMPTY_WITH_TAG0: Only for in-stream. Empty in-packets have TAG0.
22*4882a593Smuzhiyun * @CIP_DBC_IS_END_EVENT: The value of dbc in an packet corresponds to the end
23*4882a593Smuzhiyun * of event in the packet. Out of IEC 61883.
24*4882a593Smuzhiyun * @CIP_WRONG_DBS: Only for in-stream. The value of dbs is wrong in in-packets.
25*4882a593Smuzhiyun * The value of data_block_quadlets is used instead of reported value.
26*4882a593Smuzhiyun * @CIP_SKIP_DBC_ZERO_CHECK: Only for in-stream. Packets with zero in dbc is
27*4882a593Smuzhiyun * skipped for detecting discontinuity.
28*4882a593Smuzhiyun * @CIP_EMPTY_HAS_WRONG_DBC: Only for in-stream. The value of dbc in empty
29*4882a593Smuzhiyun * packet is wrong but the others are correct.
30*4882a593Smuzhiyun * @CIP_JUMBO_PAYLOAD: Only for in-stream. The number of data blocks in an
31*4882a593Smuzhiyun * packet is larger than IEC 61883-6 defines. Current implementation
32*4882a593Smuzhiyun * allows 5 times as large as IEC 61883-6 defines.
33*4882a593Smuzhiyun * @CIP_HEADER_WITHOUT_EOH: Only for in-stream. CIP Header doesn't include
34*4882a593Smuzhiyun * valid EOH.
35*4882a593Smuzhiyun * @CIP_NO_HEADERS: a lack of headers in packets
36*4882a593Smuzhiyun * @CIP_UNALIGHED_DBC: Only for in-stream. The value of dbc is not alighed to
37*4882a593Smuzhiyun * the value of current SYT_INTERVAL; e.g. initial value is not zero.
38*4882a593Smuzhiyun */
39*4882a593Smuzhiyun enum cip_flags {
40*4882a593Smuzhiyun CIP_NONBLOCKING = 0x00,
41*4882a593Smuzhiyun CIP_BLOCKING = 0x01,
42*4882a593Smuzhiyun CIP_EMPTY_WITH_TAG0 = 0x02,
43*4882a593Smuzhiyun CIP_DBC_IS_END_EVENT = 0x04,
44*4882a593Smuzhiyun CIP_WRONG_DBS = 0x08,
45*4882a593Smuzhiyun CIP_SKIP_DBC_ZERO_CHECK = 0x10,
46*4882a593Smuzhiyun CIP_EMPTY_HAS_WRONG_DBC = 0x20,
47*4882a593Smuzhiyun CIP_JUMBO_PAYLOAD = 0x40,
48*4882a593Smuzhiyun CIP_HEADER_WITHOUT_EOH = 0x80,
49*4882a593Smuzhiyun CIP_NO_HEADER = 0x100,
50*4882a593Smuzhiyun CIP_UNALIGHED_DBC = 0x200,
51*4882a593Smuzhiyun };
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun /**
54*4882a593Smuzhiyun * enum cip_sfc - supported Sampling Frequency Codes (SFCs)
55*4882a593Smuzhiyun * @CIP_SFC_32000: 32,000 data blocks
56*4882a593Smuzhiyun * @CIP_SFC_44100: 44,100 data blocks
57*4882a593Smuzhiyun * @CIP_SFC_48000: 48,000 data blocks
58*4882a593Smuzhiyun * @CIP_SFC_88200: 88,200 data blocks
59*4882a593Smuzhiyun * @CIP_SFC_96000: 96,000 data blocks
60*4882a593Smuzhiyun * @CIP_SFC_176400: 176,400 data blocks
61*4882a593Smuzhiyun * @CIP_SFC_192000: 192,000 data blocks
62*4882a593Smuzhiyun * @CIP_SFC_COUNT: the number of supported SFCs
63*4882a593Smuzhiyun *
64*4882a593Smuzhiyun * These values are used to show nominal Sampling Frequency Code in
65*4882a593Smuzhiyun * Format Dependent Field (FDF) of AMDTP packet header. In IEC 61883-6:2002,
66*4882a593Smuzhiyun * this code means the number of events per second. Actually the code
67*4882a593Smuzhiyun * represents the number of data blocks transferred per second in an AMDTP
68*4882a593Smuzhiyun * stream.
69*4882a593Smuzhiyun *
70*4882a593Smuzhiyun * In IEC 61883-6:2005, some extensions were added to support more types of
71*4882a593Smuzhiyun * data such as 'One Bit LInear Audio', therefore the meaning of SFC became
72*4882a593Smuzhiyun * different depending on the types.
73*4882a593Smuzhiyun *
74*4882a593Smuzhiyun * Currently our implementation is compatible with IEC 61883-6:2002.
75*4882a593Smuzhiyun */
76*4882a593Smuzhiyun enum cip_sfc {
77*4882a593Smuzhiyun CIP_SFC_32000 = 0,
78*4882a593Smuzhiyun CIP_SFC_44100 = 1,
79*4882a593Smuzhiyun CIP_SFC_48000 = 2,
80*4882a593Smuzhiyun CIP_SFC_88200 = 3,
81*4882a593Smuzhiyun CIP_SFC_96000 = 4,
82*4882a593Smuzhiyun CIP_SFC_176400 = 5,
83*4882a593Smuzhiyun CIP_SFC_192000 = 6,
84*4882a593Smuzhiyun CIP_SFC_COUNT
85*4882a593Smuzhiyun };
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun struct fw_unit;
88*4882a593Smuzhiyun struct fw_iso_context;
89*4882a593Smuzhiyun struct snd_pcm_substream;
90*4882a593Smuzhiyun struct snd_pcm_runtime;
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun enum amdtp_stream_direction {
93*4882a593Smuzhiyun AMDTP_OUT_STREAM = 0,
94*4882a593Smuzhiyun AMDTP_IN_STREAM
95*4882a593Smuzhiyun };
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun struct pkt_desc {
98*4882a593Smuzhiyun u32 cycle;
99*4882a593Smuzhiyun u32 syt;
100*4882a593Smuzhiyun unsigned int data_blocks;
101*4882a593Smuzhiyun unsigned int data_block_counter;
102*4882a593Smuzhiyun __be32 *ctx_payload;
103*4882a593Smuzhiyun };
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun struct amdtp_stream;
106*4882a593Smuzhiyun typedef unsigned int (*amdtp_stream_process_ctx_payloads_t)(
107*4882a593Smuzhiyun struct amdtp_stream *s,
108*4882a593Smuzhiyun const struct pkt_desc *desc,
109*4882a593Smuzhiyun unsigned int packets,
110*4882a593Smuzhiyun struct snd_pcm_substream *pcm);
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun struct amdtp_domain;
113*4882a593Smuzhiyun struct amdtp_stream {
114*4882a593Smuzhiyun struct fw_unit *unit;
115*4882a593Smuzhiyun enum cip_flags flags;
116*4882a593Smuzhiyun enum amdtp_stream_direction direction;
117*4882a593Smuzhiyun struct mutex mutex;
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun /* For packet processing. */
120*4882a593Smuzhiyun struct fw_iso_context *context;
121*4882a593Smuzhiyun struct iso_packets_buffer buffer;
122*4882a593Smuzhiyun unsigned int queue_size;
123*4882a593Smuzhiyun int packet_index;
124*4882a593Smuzhiyun struct pkt_desc *pkt_descs;
125*4882a593Smuzhiyun int tag;
126*4882a593Smuzhiyun union {
127*4882a593Smuzhiyun struct {
128*4882a593Smuzhiyun unsigned int ctx_header_size;
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun // limit for payload of iso packet.
131*4882a593Smuzhiyun unsigned int max_ctx_payload_length;
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun // For quirks of CIP headers.
134*4882a593Smuzhiyun // Fixed interval of dbc between previos/current
135*4882a593Smuzhiyun // packets.
136*4882a593Smuzhiyun unsigned int dbc_interval;
137*4882a593Smuzhiyun } tx;
138*4882a593Smuzhiyun struct {
139*4882a593Smuzhiyun // To calculate CIP data blocks and tstamp.
140*4882a593Smuzhiyun unsigned int transfer_delay;
141*4882a593Smuzhiyun unsigned int seq_index;
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun // To generate CIP header.
144*4882a593Smuzhiyun unsigned int fdf;
145*4882a593Smuzhiyun int syt_override;
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun // To generate constant hardware IRQ.
148*4882a593Smuzhiyun unsigned int event_count;
149*4882a593Smuzhiyun unsigned int events_per_period;
150*4882a593Smuzhiyun } rx;
151*4882a593Smuzhiyun } ctx_data;
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun /* For CIP headers. */
154*4882a593Smuzhiyun unsigned int source_node_id_field;
155*4882a593Smuzhiyun unsigned int data_block_quadlets;
156*4882a593Smuzhiyun unsigned int data_block_counter;
157*4882a593Smuzhiyun unsigned int sph;
158*4882a593Smuzhiyun unsigned int fmt;
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun /* Internal flags. */
161*4882a593Smuzhiyun enum cip_sfc sfc;
162*4882a593Smuzhiyun unsigned int syt_interval;
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun /* For a PCM substream processing. */
165*4882a593Smuzhiyun struct snd_pcm_substream *pcm;
166*4882a593Smuzhiyun struct work_struct period_work;
167*4882a593Smuzhiyun snd_pcm_uframes_t pcm_buffer_pointer;
168*4882a593Smuzhiyun unsigned int pcm_period_pointer;
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun /* To wait for first packet. */
171*4882a593Smuzhiyun bool callbacked;
172*4882a593Smuzhiyun wait_queue_head_t callback_wait;
173*4882a593Smuzhiyun u32 start_cycle;
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun /* For backends to process data blocks. */
176*4882a593Smuzhiyun void *protocol;
177*4882a593Smuzhiyun amdtp_stream_process_ctx_payloads_t process_ctx_payloads;
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun // For domain.
180*4882a593Smuzhiyun int channel;
181*4882a593Smuzhiyun int speed;
182*4882a593Smuzhiyun struct list_head list;
183*4882a593Smuzhiyun struct amdtp_domain *domain;
184*4882a593Smuzhiyun };
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
187*4882a593Smuzhiyun enum amdtp_stream_direction dir, enum cip_flags flags,
188*4882a593Smuzhiyun unsigned int fmt,
189*4882a593Smuzhiyun amdtp_stream_process_ctx_payloads_t process_ctx_payloads,
190*4882a593Smuzhiyun unsigned int protocol_size);
191*4882a593Smuzhiyun void amdtp_stream_destroy(struct amdtp_stream *s);
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate,
194*4882a593Smuzhiyun unsigned int data_block_quadlets);
195*4882a593Smuzhiyun unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s);
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun void amdtp_stream_update(struct amdtp_stream *s);
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
200*4882a593Smuzhiyun struct snd_pcm_runtime *runtime);
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun void amdtp_stream_pcm_prepare(struct amdtp_stream *s);
203*4882a593Smuzhiyun void amdtp_stream_pcm_abort(struct amdtp_stream *s);
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun extern const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT];
206*4882a593Smuzhiyun extern const unsigned int amdtp_rate_table[CIP_SFC_COUNT];
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun /**
209*4882a593Smuzhiyun * amdtp_stream_running - check stream is running or not
210*4882a593Smuzhiyun * @s: the AMDTP stream
211*4882a593Smuzhiyun *
212*4882a593Smuzhiyun * If this function returns true, the stream is running.
213*4882a593Smuzhiyun */
amdtp_stream_running(struct amdtp_stream * s)214*4882a593Smuzhiyun static inline bool amdtp_stream_running(struct amdtp_stream *s)
215*4882a593Smuzhiyun {
216*4882a593Smuzhiyun return !IS_ERR(s->context);
217*4882a593Smuzhiyun }
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun /**
220*4882a593Smuzhiyun * amdtp_streaming_error - check for streaming error
221*4882a593Smuzhiyun * @s: the AMDTP stream
222*4882a593Smuzhiyun *
223*4882a593Smuzhiyun * If this function returns true, the stream's packet queue has stopped due to
224*4882a593Smuzhiyun * an asynchronous error.
225*4882a593Smuzhiyun */
amdtp_streaming_error(struct amdtp_stream * s)226*4882a593Smuzhiyun static inline bool amdtp_streaming_error(struct amdtp_stream *s)
227*4882a593Smuzhiyun {
228*4882a593Smuzhiyun return s->packet_index < 0;
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun /**
232*4882a593Smuzhiyun * amdtp_stream_pcm_running - check PCM substream is running or not
233*4882a593Smuzhiyun * @s: the AMDTP stream
234*4882a593Smuzhiyun *
235*4882a593Smuzhiyun * If this function returns true, PCM substream in the AMDTP stream is running.
236*4882a593Smuzhiyun */
amdtp_stream_pcm_running(struct amdtp_stream * s)237*4882a593Smuzhiyun static inline bool amdtp_stream_pcm_running(struct amdtp_stream *s)
238*4882a593Smuzhiyun {
239*4882a593Smuzhiyun return !!s->pcm;
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun /**
243*4882a593Smuzhiyun * amdtp_stream_pcm_trigger - start/stop playback from a PCM device
244*4882a593Smuzhiyun * @s: the AMDTP stream
245*4882a593Smuzhiyun * @pcm: the PCM device to be started, or %NULL to stop the current device
246*4882a593Smuzhiyun *
247*4882a593Smuzhiyun * Call this function on a running isochronous stream to enable the actual
248*4882a593Smuzhiyun * transmission of PCM data. This function should be called from the PCM
249*4882a593Smuzhiyun * device's .trigger callback.
250*4882a593Smuzhiyun */
amdtp_stream_pcm_trigger(struct amdtp_stream * s,struct snd_pcm_substream * pcm)251*4882a593Smuzhiyun static inline void amdtp_stream_pcm_trigger(struct amdtp_stream *s,
252*4882a593Smuzhiyun struct snd_pcm_substream *pcm)
253*4882a593Smuzhiyun {
254*4882a593Smuzhiyun WRITE_ONCE(s->pcm, pcm);
255*4882a593Smuzhiyun }
256*4882a593Smuzhiyun
cip_sfc_is_base_44100(enum cip_sfc sfc)257*4882a593Smuzhiyun static inline bool cip_sfc_is_base_44100(enum cip_sfc sfc)
258*4882a593Smuzhiyun {
259*4882a593Smuzhiyun return sfc & 1;
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun /**
263*4882a593Smuzhiyun * amdtp_stream_wait_callback - sleep till callbacked or timeout
264*4882a593Smuzhiyun * @s: the AMDTP stream
265*4882a593Smuzhiyun * @timeout: msec till timeout
266*4882a593Smuzhiyun *
267*4882a593Smuzhiyun * If this function return false, the AMDTP stream should be stopped.
268*4882a593Smuzhiyun */
amdtp_stream_wait_callback(struct amdtp_stream * s,unsigned int timeout)269*4882a593Smuzhiyun static inline bool amdtp_stream_wait_callback(struct amdtp_stream *s,
270*4882a593Smuzhiyun unsigned int timeout)
271*4882a593Smuzhiyun {
272*4882a593Smuzhiyun return wait_event_timeout(s->callback_wait,
273*4882a593Smuzhiyun s->callbacked == true,
274*4882a593Smuzhiyun msecs_to_jiffies(timeout)) > 0;
275*4882a593Smuzhiyun }
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun struct seq_desc {
278*4882a593Smuzhiyun unsigned int syt_offset;
279*4882a593Smuzhiyun unsigned int data_blocks;
280*4882a593Smuzhiyun };
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun struct amdtp_domain {
283*4882a593Smuzhiyun struct list_head streams;
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun unsigned int events_per_period;
286*4882a593Smuzhiyun unsigned int events_per_buffer;
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun struct amdtp_stream *irq_target;
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun struct seq_desc *seq_descs;
291*4882a593Smuzhiyun unsigned int seq_size;
292*4882a593Smuzhiyun unsigned int seq_tail;
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun unsigned int data_block_state;
295*4882a593Smuzhiyun unsigned int syt_offset_state;
296*4882a593Smuzhiyun unsigned int last_syt_offset;
297*4882a593Smuzhiyun };
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun int amdtp_domain_init(struct amdtp_domain *d);
300*4882a593Smuzhiyun void amdtp_domain_destroy(struct amdtp_domain *d);
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun int amdtp_domain_add_stream(struct amdtp_domain *d, struct amdtp_stream *s,
303*4882a593Smuzhiyun int channel, int speed);
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun int amdtp_domain_start(struct amdtp_domain *d, unsigned int ir_delay_cycle);
306*4882a593Smuzhiyun void amdtp_domain_stop(struct amdtp_domain *d);
307*4882a593Smuzhiyun
amdtp_domain_set_events_per_period(struct amdtp_domain * d,unsigned int events_per_period,unsigned int events_per_buffer)308*4882a593Smuzhiyun static inline int amdtp_domain_set_events_per_period(struct amdtp_domain *d,
309*4882a593Smuzhiyun unsigned int events_per_period,
310*4882a593Smuzhiyun unsigned int events_per_buffer)
311*4882a593Smuzhiyun {
312*4882a593Smuzhiyun d->events_per_period = events_per_period;
313*4882a593Smuzhiyun d->events_per_buffer = events_per_buffer;
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun return 0;
316*4882a593Smuzhiyun }
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun unsigned long amdtp_domain_stream_pcm_pointer(struct amdtp_domain *d,
319*4882a593Smuzhiyun struct amdtp_stream *s);
320*4882a593Smuzhiyun int amdtp_domain_stream_pcm_ack(struct amdtp_domain *d, struct amdtp_stream *s);
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun #endif
323