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