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