xref: /linux-6.15/include/linux/soundwire/sdw.h (revision d00c2359)
1 /* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */
2 /* Copyright(c) 2015-17 Intel Corporation. */
3 
4 #ifndef __SOUNDWIRE_H
5 #define __SOUNDWIRE_H
6 
7 #include <linux/bug.h>
8 #include <linux/lockdep_types.h>
9 #include <linux/irq.h>
10 #include <linux/irqdomain.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/bitfield.h>
13 #include <sound/sdca.h>
14 
15 struct sdw_bus;
16 struct sdw_slave;
17 
18 /* SDW spec defines and enums, as defined by MIPI 1.1. Spec */
19 
20 /* SDW Broadcast Device Number */
21 #define SDW_BROADCAST_DEV_NUM		15
22 
23 /* SDW Enumeration Device Number */
24 #define SDW_ENUM_DEV_NUM		0
25 
26 /* SDW Group Device Numbers */
27 #define SDW_GROUP12_DEV_NUM		12
28 #define SDW_GROUP13_DEV_NUM		13
29 
30 /* SDW Master Device Number, not supported yet */
31 #define SDW_MASTER_DEV_NUM		14
32 
33 #define SDW_NUM_DEV_ID_REGISTERS	6
34 /* frame shape defines */
35 
36 /*
37  * Note: The maximum row define in SoundWire spec 1.1 is 23. In order to
38  * fill hole with 0, one more dummy entry is added
39  */
40 #define SDW_FRAME_ROWS		24
41 #define SDW_FRAME_COLS		8
42 #define SDW_FRAME_ROW_COLS		(SDW_FRAME_ROWS * SDW_FRAME_COLS)
43 
44 #define SDW_FRAME_CTRL_BITS		48
45 #define SDW_MAX_DEVICES			11
46 
47 #define SDW_MAX_PORTS			15
48 #define SDW_VALID_PORT_RANGE(n)		((n) < SDW_MAX_PORTS && (n) >= 1)
49 
50 enum {
51 	SDW_PORT_DIRN_SINK = 0,
52 	SDW_PORT_DIRN_SOURCE,
53 	SDW_PORT_DIRN_MAX,
54 };
55 
56 /*
57  * constants for flow control, ports and transport
58  *
59  * these are bit masks as devices can have multiple capabilities
60  */
61 
62 /*
63  * flow modes for SDW port. These can be isochronous, tx controlled,
64  * rx controlled or async
65  */
66 #define SDW_PORT_FLOW_MODE_ISOCH	0
67 #define SDW_PORT_FLOW_MODE_TX_CNTRL	BIT(0)
68 #define SDW_PORT_FLOW_MODE_RX_CNTRL	BIT(1)
69 #define SDW_PORT_FLOW_MODE_ASYNC	GENMASK(1, 0)
70 
71 /* sample packaging for block. It can be per port or per channel */
72 #define SDW_BLOCK_PACKG_PER_PORT	BIT(0)
73 #define SDW_BLOCK_PACKG_PER_CH		BIT(1)
74 
75 /**
76  * enum sdw_slave_status - Slave status
77  * @SDW_SLAVE_UNATTACHED: Slave is not attached with the bus.
78  * @SDW_SLAVE_ATTACHED: Slave is attached with bus.
79  * @SDW_SLAVE_ALERT: Some alert condition on the Slave
80  * @SDW_SLAVE_RESERVED: Reserved for future use
81  */
82 enum sdw_slave_status {
83 	SDW_SLAVE_UNATTACHED = 0,
84 	SDW_SLAVE_ATTACHED = 1,
85 	SDW_SLAVE_ALERT = 2,
86 	SDW_SLAVE_RESERVED = 3,
87 };
88 
89 /**
90  * enum sdw_clk_stop_type: clock stop operations
91  *
92  * @SDW_CLK_PRE_PREPARE: pre clock stop prepare
93  * @SDW_CLK_POST_PREPARE: post clock stop prepare
94  * @SDW_CLK_PRE_DEPREPARE: pre clock stop de-prepare
95  * @SDW_CLK_POST_DEPREPARE: post clock stop de-prepare
96  */
97 enum sdw_clk_stop_type {
98 	SDW_CLK_PRE_PREPARE = 0,
99 	SDW_CLK_POST_PREPARE,
100 	SDW_CLK_PRE_DEPREPARE,
101 	SDW_CLK_POST_DEPREPARE,
102 };
103 
104 /**
105  * enum sdw_command_response - Command response as defined by SDW spec
106  * @SDW_CMD_OK: cmd was successful
107  * @SDW_CMD_IGNORED: cmd was ignored
108  * @SDW_CMD_FAIL: cmd was NACKed
109  * @SDW_CMD_TIMEOUT: cmd timedout
110  * @SDW_CMD_FAIL_OTHER: cmd failed due to other reason than above
111  *
112  * NOTE: The enum is different than actual Spec as response in the Spec is
113  * combination of ACK/NAK bits
114  *
115  * SDW_CMD_TIMEOUT/FAIL_OTHER is defined for SW use, not in spec
116  */
117 enum sdw_command_response {
118 	SDW_CMD_OK = 0,
119 	SDW_CMD_IGNORED = 1,
120 	SDW_CMD_FAIL = 2,
121 	SDW_CMD_TIMEOUT = 3,
122 	SDW_CMD_FAIL_OTHER = 4,
123 };
124 
125 /* block group count enum */
126 enum sdw_dpn_grouping {
127 	SDW_BLK_GRP_CNT_1 = 0,
128 	SDW_BLK_GRP_CNT_2 = 1,
129 	SDW_BLK_GRP_CNT_3 = 2,
130 	SDW_BLK_GRP_CNT_4 = 3,
131 };
132 
133 /* block packing mode enum */
134 enum sdw_dpn_pkg_mode {
135 	SDW_BLK_PKG_PER_PORT = 0,
136 	SDW_BLK_PKG_PER_CHANNEL = 1
137 };
138 
139 /**
140  * enum sdw_stream_type: data stream type
141  *
142  * @SDW_STREAM_PCM: PCM data stream
143  * @SDW_STREAM_PDM: PDM data stream
144  *
145  * spec doesn't define this, but is used in implementation
146  */
147 enum sdw_stream_type {
148 	SDW_STREAM_PCM = 0,
149 	SDW_STREAM_PDM = 1,
150 };
151 
152 /**
153  * enum sdw_data_direction: Data direction
154  *
155  * @SDW_DATA_DIR_RX: Data into Port
156  * @SDW_DATA_DIR_TX: Data out of Port
157  */
158 enum sdw_data_direction {
159 	SDW_DATA_DIR_RX = 0,
160 	SDW_DATA_DIR_TX = 1,
161 };
162 
163 /**
164  * enum sdw_port_data_mode: Data Port mode
165  *
166  * @SDW_PORT_DATA_MODE_NORMAL: Normal data mode where audio data is received
167  * and transmitted.
168  * @SDW_PORT_DATA_MODE_PRBS: Test mode which uses a PRBS generator to produce
169  * a pseudo random data pattern that is transferred
170  * @SDW_PORT_DATA_MODE_STATIC_0: Simple test mode which uses static value of
171  * logic 0. The encoding will result in no signal transitions
172  * @SDW_PORT_DATA_MODE_STATIC_1: Simple test mode which uses static value of
173  * logic 1. The encoding will result in signal transitions at every bitslot
174  * owned by this Port
175  */
176 enum sdw_port_data_mode {
177 	SDW_PORT_DATA_MODE_NORMAL = 0,
178 	SDW_PORT_DATA_MODE_PRBS = 1,
179 	SDW_PORT_DATA_MODE_STATIC_0 = 2,
180 	SDW_PORT_DATA_MODE_STATIC_1 = 3,
181 };
182 
183 /*
184  * SDW properties, defined in MIPI DisCo spec v1.0
185  */
186 enum sdw_clk_stop_reset_behave {
187 	SDW_CLK_STOP_KEEP_STATUS = 1,
188 };
189 
190 /**
191  * enum sdw_p15_behave - Slave Port 15 behaviour when the Master attempts a
192  * read
193  * @SDW_P15_READ_IGNORED: Read is ignored
194  * @SDW_P15_CMD_OK: Command is ok
195  */
196 enum sdw_p15_behave {
197 	SDW_P15_READ_IGNORED = 0,
198 	SDW_P15_CMD_OK = 1,
199 };
200 
201 /**
202  * enum sdw_dpn_type - Data port types
203  * @SDW_DPN_FULL: Full Data Port is supported
204  * @SDW_DPN_SIMPLE: Simplified Data Port as defined in spec.
205  * DPN_SampleCtrl2, DPN_OffsetCtrl2, DPN_HCtrl and DPN_BlockCtrl3
206  * are not implemented.
207  * @SDW_DPN_REDUCED: Reduced Data Port as defined in spec.
208  * DPN_SampleCtrl2, DPN_HCtrl are not implemented.
209  */
210 enum sdw_dpn_type {
211 	SDW_DPN_FULL = 0,
212 	SDW_DPN_SIMPLE = 1,
213 	SDW_DPN_REDUCED = 2,
214 };
215 
216 /**
217  * enum sdw_clk_stop_mode - Clock Stop modes
218  * @SDW_CLK_STOP_MODE0: Slave can continue operation seamlessly on clock
219  * restart
220  * @SDW_CLK_STOP_MODE1: Slave may have entered a deeper power-saving mode,
221  * not capable of continuing operation seamlessly when the clock restarts
222  */
223 enum sdw_clk_stop_mode {
224 	SDW_CLK_STOP_MODE0 = 0,
225 	SDW_CLK_STOP_MODE1 = 1,
226 };
227 
228 /**
229  * struct sdw_dp0_prop - DP0 properties
230  * @max_word: Maximum number of bits in a Payload Channel Sample, 1 to 64
231  * (inclusive)
232  * @min_word: Minimum number of bits in a Payload Channel Sample, 1 to 64
233  * (inclusive)
234  * @num_words: number of wordlengths supported
235  * @words: wordlengths supported
236  * @BRA_flow_controlled: Slave implementation results in an OK_NotReady
237  * response
238  * @simple_ch_prep_sm: If channel prepare sequence is required
239  * @ch_prep_timeout: Port-specific timeout value, in milliseconds
240  * @imp_def_interrupts: If set, each bit corresponds to support for
241  * implementation-defined interrupts
242  *
243  * The wordlengths are specified by Spec as max, min AND number of
244  * discrete values, implementation can define based on the wordlengths they
245  * support
246  */
247 struct sdw_dp0_prop {
248 	u32 max_word;
249 	u32 min_word;
250 	u32 num_words;
251 	u32 *words;
252 	bool BRA_flow_controlled;
253 	bool simple_ch_prep_sm;
254 	u32 ch_prep_timeout;
255 	bool imp_def_interrupts;
256 };
257 
258 /**
259  * struct sdw_dpn_audio_mode - Audio mode properties for DPn
260  * @bus_min_freq: Minimum bus frequency, in Hz
261  * @bus_max_freq: Maximum bus frequency, in Hz
262  * @bus_num_freq: Number of discrete frequencies supported
263  * @bus_freq: Discrete bus frequencies, in Hz
264  * @min_freq: Minimum sampling frequency, in Hz
265  * @max_freq: Maximum sampling bus frequency, in Hz
266  * @num_freq: Number of discrete sampling frequency supported
267  * @freq: Discrete sampling frequencies, in Hz
268  * @prep_ch_behave: Specifies the dependencies between Channel Prepare
269  * sequence and bus clock configuration
270  * If 0, Channel Prepare can happen at any Bus clock rate
271  * If 1, Channel Prepare sequence shall happen only after Bus clock is
272  * changed to a frequency supported by this mode or compatible modes
273  * described by the next field
274  * @glitchless: Bitmap describing possible glitchless transitions from this
275  * Audio Mode to other Audio Modes
276  */
277 struct sdw_dpn_audio_mode {
278 	u32 bus_min_freq;
279 	u32 bus_max_freq;
280 	u32 bus_num_freq;
281 	u32 *bus_freq;
282 	u32 max_freq;
283 	u32 min_freq;
284 	u32 num_freq;
285 	u32 *freq;
286 	u32 prep_ch_behave;
287 	u32 glitchless;
288 };
289 
290 /**
291  * struct sdw_dpn_prop - Data Port DPn properties
292  * @num: port number
293  * @max_word: Maximum number of bits in a Payload Channel Sample, 1 to 64
294  * (inclusive)
295  * @min_word: Minimum number of bits in a Payload Channel Sample, 1 to 64
296  * (inclusive)
297  * @num_words: Number of discrete supported wordlengths
298  * @words: Discrete supported wordlength
299  * @type: Data port type. Full, Simplified or Reduced
300  * @max_grouping: Maximum number of samples that can be grouped together for
301  * a full data port
302  * @simple_ch_prep_sm: If the port supports simplified channel prepare state
303  * machine
304  * @ch_prep_timeout: Port-specific timeout value, in milliseconds
305  * @imp_def_interrupts: If set, each bit corresponds to support for
306  * implementation-defined interrupts
307  * @max_ch: Maximum channels supported
308  * @min_ch: Minimum channels supported
309  * @num_channels: Number of discrete channels supported
310  * @channels: Discrete channels supported
311  * @num_ch_combinations: Number of channel combinations supported
312  * @ch_combinations: Channel combinations supported
313  * @modes: SDW mode supported
314  * @max_async_buffer: Number of samples that this port can buffer in
315  * asynchronous modes
316  * @block_pack_mode: Type of block port mode supported
317  * @read_only_wordlength: Read Only wordlength field in DPN_BlockCtrl1 register
318  * @port_encoding: Payload Channel Sample encoding schemes supported
319  * @audio_modes: Audio modes supported
320  */
321 struct sdw_dpn_prop {
322 	u32 num;
323 	u32 max_word;
324 	u32 min_word;
325 	u32 num_words;
326 	u32 *words;
327 	enum sdw_dpn_type type;
328 	u32 max_grouping;
329 	bool simple_ch_prep_sm;
330 	u32 ch_prep_timeout;
331 	u32 imp_def_interrupts;
332 	u32 max_ch;
333 	u32 min_ch;
334 	u32 num_channels;
335 	u32 *channels;
336 	u32 num_ch_combinations;
337 	u32 *ch_combinations;
338 	u32 modes;
339 	u32 max_async_buffer;
340 	bool block_pack_mode;
341 	bool read_only_wordlength;
342 	u32 port_encoding;
343 	struct sdw_dpn_audio_mode *audio_modes;
344 };
345 
346 /**
347  * struct sdw_slave_prop - SoundWire Slave properties
348  * @mipi_revision: Spec version of the implementation
349  * @wake_capable: Wake-up events are supported
350  * @test_mode_capable: If test mode is supported
351  * @clk_stop_mode1: Clock-Stop Mode 1 is supported
352  * @simple_clk_stop_capable: Simple clock mode is supported
353  * @clk_stop_timeout: Worst-case latency of the Clock Stop Prepare State
354  * Machine transitions, in milliseconds
355  * @ch_prep_timeout: Worst-case latency of the Channel Prepare State Machine
356  * transitions, in milliseconds
357  * @reset_behave: Slave keeps the status of the SlaveStopClockPrepare
358  * state machine (P=1 SCSP_SM) after exit from clock-stop mode1
359  * @high_PHY_capable: Slave is HighPHY capable
360  * @paging_support: Slave implements paging registers SCP_AddrPage1 and
361  * SCP_AddrPage2
362  * @bank_delay_support: Slave implements bank delay/bridge support registers
363  * SCP_BankDelay and SCP_NextFrame
364  * @p15_behave: Slave behavior when the Master attempts a read to the Port15
365  * alias
366  * @lane_control_support: Slave supports lane control
367  * @master_count: Number of Masters present on this Slave
368  * @source_ports: Bitmap identifying source ports
369  * @sink_ports: Bitmap identifying sink ports
370  * @dp0_prop: Data Port 0 properties
371  * @src_dpn_prop: Source Data Port N properties
372  * @sink_dpn_prop: Sink Data Port N properties
373  * @scp_int1_mask: SCP_INT1_MASK desired settings
374  * @quirks: bitmask identifying deltas from the MIPI specification
375  * @clock_reg_supported: the Peripheral implements the clock base and scale
376  * registers introduced with the SoundWire 1.2 specification. SDCA devices
377  * do not need to set this boolean property as the registers are required.
378  * @use_domain_irq: call actual IRQ handler on slave, as well as callback
379  */
380 struct sdw_slave_prop {
381 	u32 mipi_revision;
382 	bool wake_capable;
383 	bool test_mode_capable;
384 	bool clk_stop_mode1;
385 	bool simple_clk_stop_capable;
386 	u32 clk_stop_timeout;
387 	u32 ch_prep_timeout;
388 	enum sdw_clk_stop_reset_behave reset_behave;
389 	bool high_PHY_capable;
390 	bool paging_support;
391 	bool bank_delay_support;
392 	enum sdw_p15_behave p15_behave;
393 	bool lane_control_support;
394 	u32 master_count;
395 	u32 source_ports;
396 	u32 sink_ports;
397 	struct sdw_dp0_prop *dp0_prop;
398 	struct sdw_dpn_prop *src_dpn_prop;
399 	struct sdw_dpn_prop *sink_dpn_prop;
400 	u8 scp_int1_mask;
401 	u32 quirks;
402 	bool clock_reg_supported;
403 	bool use_domain_irq;
404 };
405 
406 #define SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY	BIT(0)
407 
408 /**
409  * struct sdw_master_prop - Master properties
410  * @revision: MIPI spec version of the implementation
411  * @clk_stop_modes: Bitmap, bit N set when clock-stop-modeN supported
412  * @max_clk_freq: Maximum Bus clock frequency, in Hz
413  * @num_clk_gears: Number of clock gears supported
414  * @clk_gears: Clock gears supported
415  * @num_clk_freq: Number of clock frequencies supported, in Hz
416  * @clk_freq: Clock frequencies supported, in Hz
417  * @default_frame_rate: Controller default Frame rate, in Hz
418  * @default_row: Number of rows
419  * @default_col: Number of columns
420  * @dynamic_frame: Dynamic frame shape supported
421  * @err_threshold: Number of times that software may retry sending a single
422  * command
423  * @mclk_freq: clock reference passed to SoundWire Master, in Hz.
424  * @hw_disabled: if true, the Master is not functional, typically due to pin-mux
425  * @quirks: bitmask identifying optional behavior beyond the scope of the MIPI specification
426  */
427 struct sdw_master_prop {
428 	u32 revision;
429 	u32 clk_stop_modes;
430 	u32 max_clk_freq;
431 	u32 num_clk_gears;
432 	u32 *clk_gears;
433 	u32 num_clk_freq;
434 	u32 *clk_freq;
435 	u32 default_frame_rate;
436 	u32 default_row;
437 	u32 default_col;
438 	bool dynamic_frame;
439 	u32 err_threshold;
440 	u32 mclk_freq;
441 	bool hw_disabled;
442 	u64 quirks;
443 };
444 
445 /* Definitions for Master quirks */
446 
447 /*
448  * In a number of platforms bus clashes are reported after a hardware
449  * reset but without any explanations or evidence of a real problem.
450  * The following quirk will discard all initial bus clash interrupts
451  * but will leave the detection on should real bus clashes happen
452  */
453 #define SDW_MASTER_QUIRKS_CLEAR_INITIAL_CLASH	BIT(0)
454 
455 /*
456  * Some Slave devices have known issues with incorrect parity errors
457  * reported after a hardware reset. However during integration unexplained
458  * parity errors can be reported by Slave devices, possibly due to electrical
459  * issues at the Master level.
460  * The following quirk will discard all initial parity errors but will leave
461  * the detection on should real parity errors happen.
462  */
463 #define SDW_MASTER_QUIRKS_CLEAR_INITIAL_PARITY	BIT(1)
464 
465 int sdw_master_read_prop(struct sdw_bus *bus);
466 int sdw_slave_read_prop(struct sdw_slave *slave);
467 
468 /*
469  * SDW Slave Structures and APIs
470  */
471 
472 #define SDW_IGNORED_UNIQUE_ID 0xFF
473 
474 /**
475  * struct sdw_slave_id - Slave ID
476  * @mfg_id: MIPI Manufacturer ID
477  * @part_id: Device Part ID
478  * @class_id: MIPI Class ID (defined starting with SoundWire 1.2 spec)
479  * @unique_id: Device unique ID
480  * @sdw_version: SDW version implemented
481  *
482  * The order of the IDs here does not follow the DisCo spec definitions
483  */
484 struct sdw_slave_id {
485 	__u16 mfg_id;
486 	__u16 part_id;
487 	__u8 class_id;
488 	__u8 unique_id;
489 	__u8 sdw_version:4;
490 };
491 
492 struct sdw_peripherals {
493 	int num_peripherals;
494 	struct sdw_slave *array[];
495 };
496 
497 /*
498  * Helper macros to extract the MIPI-defined IDs
499  *
500  * Spec definition
501  *   Register		Bit	Contents
502  *   DevId_0 [7:4]	47:44	sdw_version
503  *   DevId_0 [3:0]	43:40	unique_id
504  *   DevId_1		39:32	mfg_id [15:8]
505  *   DevId_2		31:24	mfg_id [7:0]
506  *   DevId_3		23:16	part_id [15:8]
507  *   DevId_4		15:08	part_id [7:0]
508  *   DevId_5		07:00	class_id
509  *
510  * The MIPI DisCo for SoundWire defines in addition the link_id as bits 51:48
511  */
512 #define SDW_DISCO_LINK_ID_MASK	GENMASK_ULL(51, 48)
513 #define SDW_VERSION_MASK	GENMASK_ULL(47, 44)
514 #define SDW_UNIQUE_ID_MASK	GENMASK_ULL(43, 40)
515 #define SDW_MFG_ID_MASK		GENMASK_ULL(39, 24)
516 #define SDW_PART_ID_MASK	GENMASK_ULL(23, 8)
517 #define SDW_CLASS_ID_MASK	GENMASK_ULL(7, 0)
518 
519 #define SDW_DISCO_LINK_ID(addr)	FIELD_GET(SDW_DISCO_LINK_ID_MASK, addr)
520 #define SDW_VERSION(addr)	FIELD_GET(SDW_VERSION_MASK, addr)
521 #define SDW_UNIQUE_ID(addr)	FIELD_GET(SDW_UNIQUE_ID_MASK, addr)
522 #define SDW_MFG_ID(addr)	FIELD_GET(SDW_MFG_ID_MASK, addr)
523 #define SDW_PART_ID(addr)	FIELD_GET(SDW_PART_ID_MASK, addr)
524 #define SDW_CLASS_ID(addr)	FIELD_GET(SDW_CLASS_ID_MASK, addr)
525 
526 /**
527  * struct sdw_slave_intr_status - Slave interrupt status
528  * @sdca_cascade: set if the Slave device reports an SDCA interrupt
529  * @control_port: control port status
530  * @port: data port status
531  */
532 struct sdw_slave_intr_status {
533 	bool sdca_cascade;
534 	u8 control_port;
535 	u8 port[15];
536 };
537 
538 /**
539  * sdw_reg_bank - SoundWire register banks
540  * @SDW_BANK0: Soundwire register bank 0
541  * @SDW_BANK1: Soundwire register bank 1
542  */
543 enum sdw_reg_bank {
544 	SDW_BANK0,
545 	SDW_BANK1,
546 };
547 
548 /**
549  * struct sdw_prepare_ch: Prepare/De-prepare Data Port channel
550  *
551  * @num: Port number
552  * @ch_mask: Active channel mask
553  * @prepare: Prepare (true) /de-prepare (false) channel
554  * @bank: Register bank, which bank Slave/Master driver should program for
555  * implementation defined registers. This is always updated to next_bank
556  * value read from bus params.
557  *
558  */
559 struct sdw_prepare_ch {
560 	unsigned int num;
561 	unsigned int ch_mask;
562 	bool prepare;
563 	unsigned int bank;
564 };
565 
566 /**
567  * enum sdw_port_prep_ops: Prepare operations for Data Port
568  *
569  * @SDW_OPS_PORT_PRE_PREP: Pre prepare operation for the Port
570  * @SDW_OPS_PORT_PRE_DEPREP: Pre deprepare operation for the Port
571  * @SDW_OPS_PORT_POST_PREP: Post prepare operation for the Port
572  * @SDW_OPS_PORT_POST_DEPREP: Post deprepare operation for the Port
573  */
574 enum sdw_port_prep_ops {
575 	SDW_OPS_PORT_PRE_PREP = 0,
576 	SDW_OPS_PORT_PRE_DEPREP,
577 	SDW_OPS_PORT_POST_PREP,
578 	SDW_OPS_PORT_POST_DEPREP,
579 };
580 
581 /**
582  * struct sdw_bus_params: Structure holding bus configuration
583  *
584  * @curr_bank: Current bank in use (BANK0/BANK1)
585  * @next_bank: Next bank to use (BANK0/BANK1). next_bank will always be
586  * set to !curr_bank
587  * @max_dr_freq: Maximum double rate clock frequency supported, in Hz
588  * @curr_dr_freq: Current double rate clock frequency, in Hz
589  * @bandwidth: Current bandwidth
590  * @col: Active columns
591  * @row: Active rows
592  * @s_data_mode: NORMAL, STATIC or PRBS mode for all Slave ports
593  * @m_data_mode: NORMAL, STATIC or PRBS mode for all Master ports. The value
594  * should be the same to detect transmission issues, but can be different to
595  * test the interrupt reports
596  */
597 struct sdw_bus_params {
598 	enum sdw_reg_bank curr_bank;
599 	enum sdw_reg_bank next_bank;
600 	unsigned int max_dr_freq;
601 	unsigned int curr_dr_freq;
602 	unsigned int bandwidth;
603 	unsigned int col;
604 	unsigned int row;
605 	int s_data_mode;
606 	int m_data_mode;
607 };
608 
609 /**
610  * struct sdw_slave_ops: Slave driver callback ops
611  *
612  * @read_prop: Read Slave properties
613  * @interrupt_callback: Device interrupt notification (invoked in thread
614  * context)
615  * @update_status: Update Slave status
616  * @bus_config: Update the bus config for Slave
617  * @port_prep: Prepare the port with parameters
618  * @clk_stop: handle imp-def sequences before and after prepare and de-prepare
619  */
620 struct sdw_slave_ops {
621 	int (*read_prop)(struct sdw_slave *sdw);
622 	int (*interrupt_callback)(struct sdw_slave *slave,
623 				  struct sdw_slave_intr_status *status);
624 	int (*update_status)(struct sdw_slave *slave,
625 			     enum sdw_slave_status status);
626 	int (*bus_config)(struct sdw_slave *slave,
627 			  struct sdw_bus_params *params);
628 	int (*port_prep)(struct sdw_slave *slave,
629 			 struct sdw_prepare_ch *prepare_ch,
630 			 enum sdw_port_prep_ops pre_ops);
631 	int (*clk_stop)(struct sdw_slave *slave,
632 			enum sdw_clk_stop_mode mode,
633 			enum sdw_clk_stop_type type);
634 
635 };
636 
637 /**
638  * struct sdw_slave - SoundWire Slave
639  * @id: MIPI device ID
640  * @dev: Linux device
641  * @irq: IRQ number
642  * @status: Status reported by the Slave
643  * @bus: Bus handle
644  * @prop: Slave properties
645  * @debugfs: Slave debugfs
646  * @node: node for bus list
647  * @port_ready: Port ready completion flag for each Slave port
648  * @m_port_map: static Master port map for each Slave port
649  * @dev_num: Current Device Number, values can be 0 or dev_num_sticky
650  * @dev_num_sticky: one-time static Device Number assigned by Bus
651  * @probed: boolean tracking driver state
652  * @enumeration_complete: completion utility to control potential races
653  * on startup between device enumeration and read/write access to the
654  * Slave device
655  * @initialization_complete: completion utility to control potential races
656  * on startup between device enumeration and settings being restored
657  * @unattach_request: mask field to keep track why the Slave re-attached and
658  * was re-initialized. This is useful to deal with potential race conditions
659  * between the Master suspending and the codec resuming, and make sure that
660  * when the Master triggered a reset the Slave is properly enumerated and
661  * initialized
662  * @first_interrupt_done: status flag tracking if the interrupt handling
663  * for a Slave happens for the first time after enumeration
664  * @is_mockup_device: status flag used to squelch errors in the command/control
665  * protocol for SoundWire mockup devices
666  * @sdw_dev_lock: mutex used to protect callbacks/remove races
667  * @sdca_data: structure containing all device data for SDCA helpers
668  */
669 struct sdw_slave {
670 	struct sdw_slave_id id;
671 	struct device dev;
672 	int irq;
673 	enum sdw_slave_status status;
674 	struct sdw_bus *bus;
675 	struct sdw_slave_prop prop;
676 #ifdef CONFIG_DEBUG_FS
677 	struct dentry *debugfs;
678 #endif
679 	struct list_head node;
680 	struct completion port_ready[SDW_MAX_PORTS];
681 	unsigned int m_port_map[SDW_MAX_PORTS];
682 	u16 dev_num;
683 	u16 dev_num_sticky;
684 	bool probed;
685 	struct completion enumeration_complete;
686 	struct completion initialization_complete;
687 	u32 unattach_request;
688 	bool first_interrupt_done;
689 	bool is_mockup_device;
690 	struct mutex sdw_dev_lock; /* protect callbacks/remove races */
691 	struct sdca_device_data sdca_data;
692 };
693 
694 #define dev_to_sdw_dev(_dev) container_of(_dev, struct sdw_slave, dev)
695 
696 /**
697  * struct sdw_master_device - SoundWire 'Master Device' representation
698  * @dev: Linux device for this Master
699  * @bus: Bus handle shortcut
700  */
701 struct sdw_master_device {
702 	struct device dev;
703 	struct sdw_bus *bus;
704 };
705 
706 #define dev_to_sdw_master_device(d)	\
707 	container_of(d, struct sdw_master_device, dev)
708 
709 struct sdw_driver {
710 	int (*probe)(struct sdw_slave *sdw,
711 			const struct sdw_device_id *id);
712 	int (*remove)(struct sdw_slave *sdw);
713 	void (*shutdown)(struct sdw_slave *sdw);
714 
715 	const struct sdw_device_id *id_table;
716 	const struct sdw_slave_ops *ops;
717 
718 	struct device_driver driver;
719 };
720 
721 #define SDW_SLAVE_ENTRY_EXT(_mfg_id, _part_id, _version, _c_id, _drv_data) \
722 	{ .mfg_id = (_mfg_id), .part_id = (_part_id),		\
723 	  .sdw_version = (_version), .class_id = (_c_id),	\
724 	  .driver_data = (unsigned long)(_drv_data) }
725 
726 #define SDW_SLAVE_ENTRY(_mfg_id, _part_id, _drv_data)	\
727 	SDW_SLAVE_ENTRY_EXT((_mfg_id), (_part_id), 0, 0, (_drv_data))
728 
729 int sdw_handle_slave_status(struct sdw_bus *bus,
730 			enum sdw_slave_status status[]);
731 
732 /*
733  * SDW master structures and APIs
734  */
735 
736 /**
737  * struct sdw_port_params: Data Port parameters
738  *
739  * @num: Port number
740  * @bps: Word length of the Port
741  * @flow_mode: Port Data flow mode
742  * @data_mode: Test modes or normal mode
743  *
744  * This is used to program the Data Port based on Data Port stream
745  * parameters.
746  */
747 struct sdw_port_params {
748 	unsigned int num;
749 	unsigned int bps;
750 	unsigned int flow_mode;
751 	unsigned int data_mode;
752 };
753 
754 /**
755  * struct sdw_transport_params: Data Port Transport Parameters
756  *
757  * @blk_grp_ctrl_valid: Port implements block group control
758  * @num: Port number
759  * @blk_grp_ctrl: Block group control value
760  * @sample_interval: Sample interval
761  * @offset1: Blockoffset of the payload data
762  * @offset2: Blockoffset of the payload data
763  * @hstart: Horizontal start of the payload data
764  * @hstop: Horizontal stop of the payload data
765  * @blk_pkg_mode: Block per channel or block per port
766  * @lane_ctrl: Data lane Port uses for Data transfer. Currently only single
767  * data lane is supported in bus
768  *
769  * This is used to program the Data Port based on Data Port transport
770  * parameters. All these parameters are banked and can be modified
771  * during a bank switch without any artifacts in audio stream.
772  */
773 struct sdw_transport_params {
774 	bool blk_grp_ctrl_valid;
775 	unsigned int port_num;
776 	unsigned int blk_grp_ctrl;
777 	unsigned int sample_interval;
778 	unsigned int offset1;
779 	unsigned int offset2;
780 	unsigned int hstart;
781 	unsigned int hstop;
782 	unsigned int blk_pkg_mode;
783 	unsigned int lane_ctrl;
784 };
785 
786 /**
787  * struct sdw_enable_ch: Enable/disable Data Port channel
788  *
789  * @num: Port number
790  * @ch_mask: Active channel mask
791  * @enable: Enable (true) /disable (false) channel
792  */
793 struct sdw_enable_ch {
794 	unsigned int port_num;
795 	unsigned int ch_mask;
796 	bool enable;
797 };
798 
799 /**
800  * struct sdw_master_port_ops: Callback functions from bus to Master
801  * driver to set Master Data ports.
802  *
803  * @dpn_set_port_params: Set the Port parameters for the Master Port.
804  * Mandatory callback
805  * @dpn_set_port_transport_params: Set transport parameters for the Master
806  * Port. Mandatory callback
807  * @dpn_port_prep: Port prepare operations for the Master Data Port.
808  * @dpn_port_enable_ch: Enable the channels of Master Port.
809  */
810 struct sdw_master_port_ops {
811 	int (*dpn_set_port_params)(struct sdw_bus *bus,
812 			struct sdw_port_params *port_params,
813 			unsigned int bank);
814 	int (*dpn_set_port_transport_params)(struct sdw_bus *bus,
815 			struct sdw_transport_params *transport_params,
816 			enum sdw_reg_bank bank);
817 	int (*dpn_port_prep)(struct sdw_bus *bus,
818 			struct sdw_prepare_ch *prepare_ch);
819 	int (*dpn_port_enable_ch)(struct sdw_bus *bus,
820 			struct sdw_enable_ch *enable_ch, unsigned int bank);
821 };
822 
823 struct sdw_msg;
824 
825 /**
826  * struct sdw_defer - SDW deffered message
827  * @length: message length
828  * @complete: message completion
829  * @msg: SDW message
830  */
831 struct sdw_defer {
832 	int length;
833 	struct completion complete;
834 	struct sdw_msg *msg;
835 };
836 
837 /**
838  * struct sdw_master_ops - Master driver ops
839  * @read_prop: Read Master properties
840  * @override_adr: Override value read from firmware (quirk for buggy firmware)
841  * @xfer_msg: Transfer message callback
842  * @xfer_msg_defer: Defer version of transfer message callback. The message is handled with the
843  * bus struct @sdw_defer
844  * @set_bus_conf: Set the bus configuration
845  * @pre_bank_switch: Callback for pre bank switch
846  * @post_bank_switch: Callback for post bank switch
847  * @read_ping_status: Read status from PING frames, reported with two bits per Device.
848  * Bits 31:24 are reserved.
849  * @get_device_num: Callback for vendor-specific device_number allocation
850  * @put_device_num: Callback for vendor-specific device_number release
851  * @new_peripheral_assigned: Callback to handle enumeration of new peripheral.
852  */
853 struct sdw_master_ops {
854 	int (*read_prop)(struct sdw_bus *bus);
855 	u64 (*override_adr)
856 			(struct sdw_bus *bus, u64 addr);
857 	enum sdw_command_response (*xfer_msg)
858 			(struct sdw_bus *bus, struct sdw_msg *msg);
859 	enum sdw_command_response (*xfer_msg_defer)
860 			(struct sdw_bus *bus);
861 	int (*set_bus_conf)(struct sdw_bus *bus,
862 			struct sdw_bus_params *params);
863 	int (*pre_bank_switch)(struct sdw_bus *bus);
864 	int (*post_bank_switch)(struct sdw_bus *bus);
865 	u32 (*read_ping_status)(struct sdw_bus *bus);
866 	int (*get_device_num)(struct sdw_bus *bus, struct sdw_slave *slave);
867 	void (*put_device_num)(struct sdw_bus *bus, struct sdw_slave *slave);
868 	void (*new_peripheral_assigned)(struct sdw_bus *bus,
869 					struct sdw_slave *slave,
870 					int dev_num);
871 };
872 
873 /**
874  * struct sdw_bus - SoundWire bus
875  * @dev: Shortcut to &bus->md->dev to avoid changing the entire code.
876  * @md: Master device
877  * @controller_id: system-unique controller ID. If set to -1, the bus @id will be used.
878  * @link_id: Link id number, can be 0 to N, unique for each Controller
879  * @id: bus system-wide unique id
880  * @slaves: list of Slaves on this bus
881  * @assigned: Bitmap for Slave device numbers.
882  * Bit set implies used number, bit clear implies unused number.
883  * @bus_lock: bus lock
884  * @msg_lock: message lock
885  * @compute_params: points to Bus resource management implementation
886  * @ops: Master callback ops
887  * @port_ops: Master port callback ops
888  * @params: Current bus parameters
889  * @prop: Master properties
890  * @vendor_specific_prop: pointer to non-standard properties
891  * @m_rt_list: List of Master instance of all stream(s) running on Bus. This
892  * is used to compute and program bus bandwidth, clock, frame shape,
893  * transport and port parameters
894  * @debugfs: Bus debugfs
895  * @domain: IRQ domain
896  * @defer_msg: Defer message
897  * @clk_stop_timeout: Clock stop timeout computed
898  * @bank_switch_timeout: Bank switch timeout computed
899  * @multi_link: Store bus property that indicates if multi links
900  * are supported. This flag is populated by drivers after reading
901  * appropriate firmware (ACPI/DT).
902  * @hw_sync_min_links: Number of links used by a stream above which
903  * hardware-based synchronization is required. This value is only
904  * meaningful if multi_link is set. If set to 1, hardware-based
905  * synchronization will be used even if a stream only uses a single
906  * SoundWire segment.
907  * @stream_refcount: number of streams currently using this bus
908  */
909 struct sdw_bus {
910 	struct device *dev;
911 	struct sdw_master_device *md;
912 	int controller_id;
913 	unsigned int link_id;
914 	int id;
915 	struct list_head slaves;
916 	DECLARE_BITMAP(assigned, SDW_MAX_DEVICES);
917 	struct mutex bus_lock;
918 	struct lock_class_key bus_lock_key;
919 	struct mutex msg_lock;
920 	struct lock_class_key msg_lock_key;
921 	int (*compute_params)(struct sdw_bus *bus);
922 	const struct sdw_master_ops *ops;
923 	const struct sdw_master_port_ops *port_ops;
924 	struct sdw_bus_params params;
925 	struct sdw_master_prop prop;
926 	void *vendor_specific_prop;
927 	struct list_head m_rt_list;
928 #ifdef CONFIG_DEBUG_FS
929 	struct dentry *debugfs;
930 #endif
931 	struct irq_chip irq_chip;
932 	struct irq_domain *domain;
933 	struct sdw_defer defer_msg;
934 	unsigned int clk_stop_timeout;
935 	u32 bank_switch_timeout;
936 	bool multi_link;
937 	int hw_sync_min_links;
938 	int stream_refcount;
939 };
940 
941 int sdw_bus_master_add(struct sdw_bus *bus, struct device *parent,
942 		       struct fwnode_handle *fwnode);
943 void sdw_bus_master_delete(struct sdw_bus *bus);
944 
945 void sdw_show_ping_status(struct sdw_bus *bus, bool sync_delay);
946 
947 /**
948  * sdw_port_config: Master or Slave Port configuration
949  *
950  * @num: Port number
951  * @ch_mask: channels mask for port
952  */
953 struct sdw_port_config {
954 	unsigned int num;
955 	unsigned int ch_mask;
956 };
957 
958 /**
959  * sdw_stream_config: Master or Slave stream configuration
960  *
961  * @frame_rate: Audio frame rate of the stream, in Hz
962  * @ch_count: Channel count of the stream
963  * @bps: Number of bits per audio sample
964  * @direction: Data direction
965  * @type: Stream type PCM or PDM
966  */
967 struct sdw_stream_config {
968 	unsigned int frame_rate;
969 	unsigned int ch_count;
970 	unsigned int bps;
971 	enum sdw_data_direction direction;
972 	enum sdw_stream_type type;
973 };
974 
975 /**
976  * sdw_stream_state: Stream states
977  *
978  * @SDW_STREAM_ALLOCATED: New stream allocated.
979  * @SDW_STREAM_CONFIGURED: Stream configured
980  * @SDW_STREAM_PREPARED: Stream prepared
981  * @SDW_STREAM_ENABLED: Stream enabled
982  * @SDW_STREAM_DISABLED: Stream disabled
983  * @SDW_STREAM_DEPREPARED: Stream de-prepared
984  * @SDW_STREAM_RELEASED: Stream released
985  */
986 enum sdw_stream_state {
987 	SDW_STREAM_ALLOCATED = 0,
988 	SDW_STREAM_CONFIGURED = 1,
989 	SDW_STREAM_PREPARED = 2,
990 	SDW_STREAM_ENABLED = 3,
991 	SDW_STREAM_DISABLED = 4,
992 	SDW_STREAM_DEPREPARED = 5,
993 	SDW_STREAM_RELEASED = 6,
994 };
995 
996 /**
997  * sdw_stream_params: Stream parameters
998  *
999  * @rate: Sampling frequency, in Hz
1000  * @ch_count: Number of channels
1001  * @bps: bits per channel sample
1002  */
1003 struct sdw_stream_params {
1004 	unsigned int rate;
1005 	unsigned int ch_count;
1006 	unsigned int bps;
1007 };
1008 
1009 /**
1010  * sdw_stream_runtime: Runtime stream parameters
1011  *
1012  * @name: SoundWire stream name
1013  * @params: Stream parameters
1014  * @state: Current state of the stream
1015  * @type: Stream type PCM or PDM
1016  * @master_list: List of Master runtime(s) in this stream.
1017  * master_list can contain only one m_rt per Master instance
1018  * for a stream
1019  * @m_rt_count: Count of Master runtime(s) in this stream
1020  */
1021 struct sdw_stream_runtime {
1022 	const char *name;
1023 	struct sdw_stream_params params;
1024 	enum sdw_stream_state state;
1025 	enum sdw_stream_type type;
1026 	struct list_head master_list;
1027 	int m_rt_count;
1028 };
1029 
1030 struct sdw_stream_runtime *sdw_alloc_stream(const char *stream_name);
1031 void sdw_release_stream(struct sdw_stream_runtime *stream);
1032 
1033 int sdw_compute_params(struct sdw_bus *bus);
1034 
1035 int sdw_stream_add_master(struct sdw_bus *bus,
1036 		struct sdw_stream_config *stream_config,
1037 		const struct sdw_port_config *port_config,
1038 		unsigned int num_ports,
1039 		struct sdw_stream_runtime *stream);
1040 int sdw_stream_remove_master(struct sdw_bus *bus,
1041 		struct sdw_stream_runtime *stream);
1042 int sdw_startup_stream(void *sdw_substream);
1043 int sdw_prepare_stream(struct sdw_stream_runtime *stream);
1044 int sdw_enable_stream(struct sdw_stream_runtime *stream);
1045 int sdw_disable_stream(struct sdw_stream_runtime *stream);
1046 int sdw_deprepare_stream(struct sdw_stream_runtime *stream);
1047 void sdw_shutdown_stream(void *sdw_substream);
1048 int sdw_bus_prep_clk_stop(struct sdw_bus *bus);
1049 int sdw_bus_clk_stop(struct sdw_bus *bus);
1050 int sdw_bus_exit_clk_stop(struct sdw_bus *bus);
1051 
1052 int sdw_compare_devid(struct sdw_slave *slave, struct sdw_slave_id id);
1053 void sdw_extract_slave_id(struct sdw_bus *bus, u64 addr, struct sdw_slave_id *id);
1054 
1055 #if IS_ENABLED(CONFIG_SOUNDWIRE)
1056 
1057 int sdw_stream_add_slave(struct sdw_slave *slave,
1058 			 struct sdw_stream_config *stream_config,
1059 			 const struct sdw_port_config *port_config,
1060 			 unsigned int num_ports,
1061 			 struct sdw_stream_runtime *stream);
1062 int sdw_stream_remove_slave(struct sdw_slave *slave,
1063 			    struct sdw_stream_runtime *stream);
1064 
1065 /* messaging and data APIs */
1066 int sdw_read(struct sdw_slave *slave, u32 addr);
1067 int sdw_write(struct sdw_slave *slave, u32 addr, u8 value);
1068 int sdw_write_no_pm(struct sdw_slave *slave, u32 addr, u8 value);
1069 int sdw_read_no_pm(struct sdw_slave *slave, u32 addr);
1070 int sdw_nread(struct sdw_slave *slave, u32 addr, size_t count, u8 *val);
1071 int sdw_nread_no_pm(struct sdw_slave *slave, u32 addr, size_t count, u8 *val);
1072 int sdw_nwrite(struct sdw_slave *slave, u32 addr, size_t count, const u8 *val);
1073 int sdw_nwrite_no_pm(struct sdw_slave *slave, u32 addr, size_t count, const u8 *val);
1074 int sdw_update(struct sdw_slave *slave, u32 addr, u8 mask, u8 val);
1075 int sdw_update_no_pm(struct sdw_slave *slave, u32 addr, u8 mask, u8 val);
1076 
1077 #else
1078 
1079 static inline int sdw_stream_add_slave(struct sdw_slave *slave,
1080 				       struct sdw_stream_config *stream_config,
1081 				       const struct sdw_port_config *port_config,
1082 				       unsigned int num_ports,
1083 				       struct sdw_stream_runtime *stream)
1084 {
1085 	WARN_ONCE(1, "SoundWire API is disabled");
1086 	return -EINVAL;
1087 }
1088 
1089 static inline int sdw_stream_remove_slave(struct sdw_slave *slave,
1090 					  struct sdw_stream_runtime *stream)
1091 {
1092 	WARN_ONCE(1, "SoundWire API is disabled");
1093 	return -EINVAL;
1094 }
1095 
1096 /* messaging and data APIs */
1097 static inline int sdw_read(struct sdw_slave *slave, u32 addr)
1098 {
1099 	WARN_ONCE(1, "SoundWire API is disabled");
1100 	return -EINVAL;
1101 }
1102 
1103 static inline int sdw_write(struct sdw_slave *slave, u32 addr, u8 value)
1104 {
1105 	WARN_ONCE(1, "SoundWire API is disabled");
1106 	return -EINVAL;
1107 }
1108 
1109 static inline int sdw_write_no_pm(struct sdw_slave *slave, u32 addr, u8 value)
1110 {
1111 	WARN_ONCE(1, "SoundWire API is disabled");
1112 	return -EINVAL;
1113 }
1114 
1115 static inline int sdw_read_no_pm(struct sdw_slave *slave, u32 addr)
1116 {
1117 	WARN_ONCE(1, "SoundWire API is disabled");
1118 	return -EINVAL;
1119 }
1120 
1121 static inline int sdw_nread(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
1122 {
1123 	WARN_ONCE(1, "SoundWire API is disabled");
1124 	return -EINVAL;
1125 }
1126 
1127 static inline int sdw_nread_no_pm(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
1128 {
1129 	WARN_ONCE(1, "SoundWire API is disabled");
1130 	return -EINVAL;
1131 }
1132 
1133 static inline int sdw_nwrite(struct sdw_slave *slave, u32 addr, size_t count, const u8 *val)
1134 {
1135 	WARN_ONCE(1, "SoundWire API is disabled");
1136 	return -EINVAL;
1137 }
1138 
1139 static inline int sdw_nwrite_no_pm(struct sdw_slave *slave, u32 addr, size_t count, const u8 *val)
1140 {
1141 	WARN_ONCE(1, "SoundWire API is disabled");
1142 	return -EINVAL;
1143 }
1144 
1145 static inline int sdw_update(struct sdw_slave *slave, u32 addr, u8 mask, u8 val)
1146 {
1147 	WARN_ONCE(1, "SoundWire API is disabled");
1148 	return -EINVAL;
1149 }
1150 
1151 static inline int sdw_update_no_pm(struct sdw_slave *slave, u32 addr, u8 mask, u8 val)
1152 {
1153 	WARN_ONCE(1, "SoundWire API is disabled");
1154 	return -EINVAL;
1155 }
1156 
1157 #endif /* CONFIG_SOUNDWIRE */
1158 
1159 #endif /* __SOUNDWIRE_H */
1160