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 struct sdw_extended_slave_id { 486 int link_id; 487 struct sdw_slave_id id; 488 }; 489 490 /* 491 * Helper macros to extract the MIPI-defined IDs 492 * 493 * Spec definition 494 * Register Bit Contents 495 * DevId_0 [7:4] 47:44 sdw_version 496 * DevId_0 [3:0] 43:40 unique_id 497 * DevId_1 39:32 mfg_id [15:8] 498 * DevId_2 31:24 mfg_id [7:0] 499 * DevId_3 23:16 part_id [15:8] 500 * DevId_4 15:08 part_id [7:0] 501 * DevId_5 07:00 class_id 502 * 503 * The MIPI DisCo for SoundWire defines in addition the link_id as bits 51:48 504 */ 505 #define SDW_DISCO_LINK_ID_MASK GENMASK_ULL(51, 48) 506 #define SDW_VERSION_MASK GENMASK_ULL(47, 44) 507 #define SDW_UNIQUE_ID_MASK GENMASK_ULL(43, 40) 508 #define SDW_MFG_ID_MASK GENMASK_ULL(39, 24) 509 #define SDW_PART_ID_MASK GENMASK_ULL(23, 8) 510 #define SDW_CLASS_ID_MASK GENMASK_ULL(7, 0) 511 512 #define SDW_DISCO_LINK_ID(addr) FIELD_GET(SDW_DISCO_LINK_ID_MASK, addr) 513 #define SDW_VERSION(addr) FIELD_GET(SDW_VERSION_MASK, addr) 514 #define SDW_UNIQUE_ID(addr) FIELD_GET(SDW_UNIQUE_ID_MASK, addr) 515 #define SDW_MFG_ID(addr) FIELD_GET(SDW_MFG_ID_MASK, addr) 516 #define SDW_PART_ID(addr) FIELD_GET(SDW_PART_ID_MASK, addr) 517 #define SDW_CLASS_ID(addr) FIELD_GET(SDW_CLASS_ID_MASK, addr) 518 519 /** 520 * struct sdw_slave_intr_status - Slave interrupt status 521 * @sdca_cascade: set if the Slave device reports an SDCA interrupt 522 * @control_port: control port status 523 * @port: data port status 524 */ 525 struct sdw_slave_intr_status { 526 bool sdca_cascade; 527 u8 control_port; 528 u8 port[15]; 529 }; 530 531 /** 532 * sdw_reg_bank - SoundWire register banks 533 * @SDW_BANK0: Soundwire register bank 0 534 * @SDW_BANK1: Soundwire register bank 1 535 */ 536 enum sdw_reg_bank { 537 SDW_BANK0, 538 SDW_BANK1, 539 }; 540 541 /** 542 * struct sdw_bus_conf: Bus configuration 543 * 544 * @clk_freq: Clock frequency, in Hz 545 * @num_rows: Number of rows in frame 546 * @num_cols: Number of columns in frame 547 * @bank: Next register bank 548 */ 549 struct sdw_bus_conf { 550 unsigned int clk_freq; 551 unsigned int num_rows; 552 unsigned int num_cols; 553 unsigned int bank; 554 }; 555 556 /** 557 * struct sdw_prepare_ch: Prepare/De-prepare Data Port channel 558 * 559 * @num: Port number 560 * @ch_mask: Active channel mask 561 * @prepare: Prepare (true) /de-prepare (false) channel 562 * @bank: Register bank, which bank Slave/Master driver should program for 563 * implementation defined registers. This is always updated to next_bank 564 * value read from bus params. 565 * 566 */ 567 struct sdw_prepare_ch { 568 unsigned int num; 569 unsigned int ch_mask; 570 bool prepare; 571 unsigned int bank; 572 }; 573 574 /** 575 * enum sdw_port_prep_ops: Prepare operations for Data Port 576 * 577 * @SDW_OPS_PORT_PRE_PREP: Pre prepare operation for the Port 578 * @SDW_OPS_PORT_PRE_DEPREP: Pre deprepare operation for the Port 579 * @SDW_OPS_PORT_POST_PREP: Post prepare operation for the Port 580 * @SDW_OPS_PORT_POST_DEPREP: Post deprepare operation for the Port 581 */ 582 enum sdw_port_prep_ops { 583 SDW_OPS_PORT_PRE_PREP = 0, 584 SDW_OPS_PORT_PRE_DEPREP, 585 SDW_OPS_PORT_POST_PREP, 586 SDW_OPS_PORT_POST_DEPREP, 587 }; 588 589 /** 590 * struct sdw_bus_params: Structure holding bus configuration 591 * 592 * @curr_bank: Current bank in use (BANK0/BANK1) 593 * @next_bank: Next bank to use (BANK0/BANK1). next_bank will always be 594 * set to !curr_bank 595 * @max_dr_freq: Maximum double rate clock frequency supported, in Hz 596 * @curr_dr_freq: Current double rate clock frequency, in Hz 597 * @bandwidth: Current bandwidth 598 * @col: Active columns 599 * @row: Active rows 600 * @s_data_mode: NORMAL, STATIC or PRBS mode for all Slave ports 601 * @m_data_mode: NORMAL, STATIC or PRBS mode for all Master ports. The value 602 * should be the same to detect transmission issues, but can be different to 603 * test the interrupt reports 604 */ 605 struct sdw_bus_params { 606 enum sdw_reg_bank curr_bank; 607 enum sdw_reg_bank next_bank; 608 unsigned int max_dr_freq; 609 unsigned int curr_dr_freq; 610 unsigned int bandwidth; 611 unsigned int col; 612 unsigned int row; 613 int s_data_mode; 614 int m_data_mode; 615 }; 616 617 /** 618 * struct sdw_slave_ops: Slave driver callback ops 619 * 620 * @read_prop: Read Slave properties 621 * @interrupt_callback: Device interrupt notification (invoked in thread 622 * context) 623 * @update_status: Update Slave status 624 * @bus_config: Update the bus config for Slave 625 * @port_prep: Prepare the port with parameters 626 * @clk_stop: handle imp-def sequences before and after prepare and de-prepare 627 */ 628 struct sdw_slave_ops { 629 int (*read_prop)(struct sdw_slave *sdw); 630 int (*interrupt_callback)(struct sdw_slave *slave, 631 struct sdw_slave_intr_status *status); 632 int (*update_status)(struct sdw_slave *slave, 633 enum sdw_slave_status status); 634 int (*bus_config)(struct sdw_slave *slave, 635 struct sdw_bus_params *params); 636 int (*port_prep)(struct sdw_slave *slave, 637 struct sdw_prepare_ch *prepare_ch, 638 enum sdw_port_prep_ops pre_ops); 639 int (*clk_stop)(struct sdw_slave *slave, 640 enum sdw_clk_stop_mode mode, 641 enum sdw_clk_stop_type type); 642 643 }; 644 645 /** 646 * struct sdw_slave - SoundWire Slave 647 * @id: MIPI device ID 648 * @dev: Linux device 649 * @status: Status reported by the Slave 650 * @bus: Bus handle 651 * @prop: Slave properties 652 * @debugfs: Slave debugfs 653 * @node: node for bus list 654 * @port_ready: Port ready completion flag for each Slave port 655 * @m_port_map: static Master port map for each Slave port 656 * @dev_num: Current Device Number, values can be 0 or dev_num_sticky 657 * @dev_num_sticky: one-time static Device Number assigned by Bus 658 * @probed: boolean tracking driver state 659 * @enumeration_complete: completion utility to control potential races 660 * on startup between device enumeration and read/write access to the 661 * Slave device 662 * @initialization_complete: completion utility to control potential races 663 * on startup between device enumeration and settings being restored 664 * @unattach_request: mask field to keep track why the Slave re-attached and 665 * was re-initialized. This is useful to deal with potential race conditions 666 * between the Master suspending and the codec resuming, and make sure that 667 * when the Master triggered a reset the Slave is properly enumerated and 668 * initialized 669 * @first_interrupt_done: status flag tracking if the interrupt handling 670 * for a Slave happens for the first time after enumeration 671 * @is_mockup_device: status flag used to squelch errors in the command/control 672 * protocol for SoundWire mockup devices 673 * @sdw_dev_lock: mutex used to protect callbacks/remove races 674 */ 675 struct sdw_slave { 676 struct sdw_slave_id id; 677 struct device dev; 678 enum sdw_slave_status status; 679 struct sdw_bus *bus; 680 struct sdw_slave_prop prop; 681 #ifdef CONFIG_DEBUG_FS 682 struct dentry *debugfs; 683 #endif 684 struct list_head node; 685 struct completion port_ready[SDW_MAX_PORTS]; 686 unsigned int m_port_map[SDW_MAX_PORTS]; 687 u16 dev_num; 688 u16 dev_num_sticky; 689 bool probed; 690 struct completion enumeration_complete; 691 struct completion initialization_complete; 692 u32 unattach_request; 693 bool first_interrupt_done; 694 bool is_mockup_device; 695 struct mutex sdw_dev_lock; /* protect callbacks/remove races */ 696 }; 697 698 #define dev_to_sdw_dev(_dev) container_of(_dev, struct sdw_slave, dev) 699 700 /** 701 * struct sdw_master_device - SoundWire 'Master Device' representation 702 * @dev: Linux device for this Master 703 * @bus: Bus handle shortcut 704 */ 705 struct sdw_master_device { 706 struct device dev; 707 struct sdw_bus *bus; 708 }; 709 710 #define dev_to_sdw_master_device(d) \ 711 container_of(d, struct sdw_master_device, dev) 712 713 struct sdw_driver { 714 const char *name; 715 716 int (*probe)(struct sdw_slave *sdw, 717 const struct sdw_device_id *id); 718 int (*remove)(struct sdw_slave *sdw); 719 void (*shutdown)(struct sdw_slave *sdw); 720 721 const struct sdw_device_id *id_table; 722 const struct sdw_slave_ops *ops; 723 724 struct device_driver driver; 725 }; 726 727 #define SDW_SLAVE_ENTRY_EXT(_mfg_id, _part_id, _version, _c_id, _drv_data) \ 728 { .mfg_id = (_mfg_id), .part_id = (_part_id), \ 729 .sdw_version = (_version), .class_id = (_c_id), \ 730 .driver_data = (unsigned long)(_drv_data) } 731 732 #define SDW_SLAVE_ENTRY(_mfg_id, _part_id, _drv_data) \ 733 SDW_SLAVE_ENTRY_EXT((_mfg_id), (_part_id), 0, 0, (_drv_data)) 734 735 int sdw_handle_slave_status(struct sdw_bus *bus, 736 enum sdw_slave_status status[]); 737 738 /* 739 * SDW master structures and APIs 740 */ 741 742 /** 743 * struct sdw_port_params: Data Port parameters 744 * 745 * @num: Port number 746 * @bps: Word length of the Port 747 * @flow_mode: Port Data flow mode 748 * @data_mode: Test modes or normal mode 749 * 750 * This is used to program the Data Port based on Data Port stream 751 * parameters. 752 */ 753 struct sdw_port_params { 754 unsigned int num; 755 unsigned int bps; 756 unsigned int flow_mode; 757 unsigned int data_mode; 758 }; 759 760 /** 761 * struct sdw_transport_params: Data Port Transport Parameters 762 * 763 * @blk_grp_ctrl_valid: Port implements block group control 764 * @num: Port number 765 * @blk_grp_ctrl: Block group control value 766 * @sample_interval: Sample interval 767 * @offset1: Blockoffset of the payload data 768 * @offset2: Blockoffset of the payload data 769 * @hstart: Horizontal start of the payload data 770 * @hstop: Horizontal stop of the payload data 771 * @blk_pkg_mode: Block per channel or block per port 772 * @lane_ctrl: Data lane Port uses for Data transfer. Currently only single 773 * data lane is supported in bus 774 * 775 * This is used to program the Data Port based on Data Port transport 776 * parameters. All these parameters are banked and can be modified 777 * during a bank switch without any artifacts in audio stream. 778 */ 779 struct sdw_transport_params { 780 bool blk_grp_ctrl_valid; 781 unsigned int port_num; 782 unsigned int blk_grp_ctrl; 783 unsigned int sample_interval; 784 unsigned int offset1; 785 unsigned int offset2; 786 unsigned int hstart; 787 unsigned int hstop; 788 unsigned int blk_pkg_mode; 789 unsigned int lane_ctrl; 790 }; 791 792 /** 793 * struct sdw_enable_ch: Enable/disable Data Port channel 794 * 795 * @num: Port number 796 * @ch_mask: Active channel mask 797 * @enable: Enable (true) /disable (false) channel 798 */ 799 struct sdw_enable_ch { 800 unsigned int port_num; 801 unsigned int ch_mask; 802 bool enable; 803 }; 804 805 /** 806 * struct sdw_master_port_ops: Callback functions from bus to Master 807 * driver to set Master Data ports. 808 * 809 * @dpn_set_port_params: Set the Port parameters for the Master Port. 810 * Mandatory callback 811 * @dpn_set_port_transport_params: Set transport parameters for the Master 812 * Port. Mandatory callback 813 * @dpn_port_prep: Port prepare operations for the Master Data Port. 814 * @dpn_port_enable_ch: Enable the channels of Master Port. 815 */ 816 struct sdw_master_port_ops { 817 int (*dpn_set_port_params)(struct sdw_bus *bus, 818 struct sdw_port_params *port_params, 819 unsigned int bank); 820 int (*dpn_set_port_transport_params)(struct sdw_bus *bus, 821 struct sdw_transport_params *transport_params, 822 enum sdw_reg_bank bank); 823 int (*dpn_port_prep)(struct sdw_bus *bus, 824 struct sdw_prepare_ch *prepare_ch); 825 int (*dpn_port_enable_ch)(struct sdw_bus *bus, 826 struct sdw_enable_ch *enable_ch, unsigned int bank); 827 }; 828 829 struct sdw_msg; 830 831 /** 832 * struct sdw_defer - SDW deffered message 833 * @length: message length 834 * @complete: message completion 835 * @msg: SDW message 836 */ 837 struct sdw_defer { 838 int length; 839 struct completion complete; 840 struct sdw_msg *msg; 841 }; 842 843 /** 844 * struct sdw_master_ops - Master driver ops 845 * @read_prop: Read Master properties 846 * @override_adr: Override value read from firmware (quirk for buggy firmware) 847 * @xfer_msg: Transfer message callback 848 * @xfer_msg_defer: Defer version of transfer message callback. The message is handled with the 849 * bus struct @sdw_defer 850 * @set_bus_conf: Set the bus configuration 851 * @pre_bank_switch: Callback for pre bank switch 852 * @post_bank_switch: Callback for post bank switch 853 * @read_ping_status: Read status from PING frames, reported with two bits per Device. 854 * Bits 31:24 are reserved. 855 * @new_peripheral_assigned: Callback to handle enumeration of new peripheral. 856 */ 857 struct sdw_master_ops { 858 int (*read_prop)(struct sdw_bus *bus); 859 u64 (*override_adr) 860 (struct sdw_bus *bus, u64 addr); 861 enum sdw_command_response (*xfer_msg) 862 (struct sdw_bus *bus, struct sdw_msg *msg); 863 enum sdw_command_response (*xfer_msg_defer) 864 (struct sdw_bus *bus); 865 int (*set_bus_conf)(struct sdw_bus *bus, 866 struct sdw_bus_params *params); 867 int (*pre_bank_switch)(struct sdw_bus *bus); 868 int (*post_bank_switch)(struct sdw_bus *bus); 869 u32 (*read_ping_status)(struct sdw_bus *bus); 870 void (*new_peripheral_assigned)(struct sdw_bus *bus, 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 * @link_id: Link id number, can be 0 to N, unique for each Master 878 * @id: bus system-wide unique id 879 * @slaves: list of Slaves on this bus 880 * @assigned: Bitmap for Slave device numbers. 881 * Bit set implies used number, bit clear implies unused number. 882 * @bus_lock: bus lock 883 * @msg_lock: message lock 884 * @compute_params: points to Bus resource management implementation 885 * @ops: Master callback ops 886 * @port_ops: Master port callback ops 887 * @params: Current bus parameters 888 * @prop: Master properties 889 * @m_rt_list: List of Master instance of all stream(s) running on Bus. This 890 * is used to compute and program bus bandwidth, clock, frame shape, 891 * transport and port parameters 892 * @debugfs: Bus debugfs 893 * @defer_msg: Defer message 894 * @clk_stop_timeout: Clock stop timeout computed 895 * @bank_switch_timeout: Bank switch timeout computed 896 * @multi_link: Store bus property that indicates if multi links 897 * are supported. This flag is populated by drivers after reading 898 * appropriate firmware (ACPI/DT). 899 * @hw_sync_min_links: Number of links used by a stream above which 900 * hardware-based synchronization is required. This value is only 901 * meaningful if multi_link is set. If set to 1, hardware-based 902 * synchronization will be used even if a stream only uses a single 903 * SoundWire segment. 904 * @dev_num_ida_min: if set, defines the minimum values for the IDA 905 * used to allocate system-unique device numbers. This value needs to be 906 * identical across all SoundWire bus in the system. 907 */ 908 struct sdw_bus { 909 struct device *dev; 910 struct sdw_master_device *md; 911 unsigned int link_id; 912 int id; 913 struct list_head slaves; 914 DECLARE_BITMAP(assigned, SDW_MAX_DEVICES); 915 struct mutex bus_lock; 916 struct lock_class_key bus_lock_key; 917 struct mutex msg_lock; 918 struct lock_class_key msg_lock_key; 919 int (*compute_params)(struct sdw_bus *bus); 920 const struct sdw_master_ops *ops; 921 const struct sdw_master_port_ops *port_ops; 922 struct sdw_bus_params params; 923 struct sdw_master_prop prop; 924 struct list_head m_rt_list; 925 #ifdef CONFIG_DEBUG_FS 926 struct dentry *debugfs; 927 #endif 928 struct sdw_defer defer_msg; 929 unsigned int clk_stop_timeout; 930 u32 bank_switch_timeout; 931 bool multi_link; 932 int hw_sync_min_links; 933 int dev_num_ida_min; 934 }; 935 936 int sdw_bus_master_add(struct sdw_bus *bus, struct device *parent, 937 struct fwnode_handle *fwnode); 938 void sdw_bus_master_delete(struct sdw_bus *bus); 939 940 void sdw_show_ping_status(struct sdw_bus *bus, bool sync_delay); 941 942 /** 943 * sdw_port_config: Master or Slave Port configuration 944 * 945 * @num: Port number 946 * @ch_mask: channels mask for port 947 */ 948 struct sdw_port_config { 949 unsigned int num; 950 unsigned int ch_mask; 951 }; 952 953 /** 954 * sdw_stream_config: Master or Slave stream configuration 955 * 956 * @frame_rate: Audio frame rate of the stream, in Hz 957 * @ch_count: Channel count of the stream 958 * @bps: Number of bits per audio sample 959 * @direction: Data direction 960 * @type: Stream type PCM or PDM 961 */ 962 struct sdw_stream_config { 963 unsigned int frame_rate; 964 unsigned int ch_count; 965 unsigned int bps; 966 enum sdw_data_direction direction; 967 enum sdw_stream_type type; 968 }; 969 970 /** 971 * sdw_stream_state: Stream states 972 * 973 * @SDW_STREAM_ALLOCATED: New stream allocated. 974 * @SDW_STREAM_CONFIGURED: Stream configured 975 * @SDW_STREAM_PREPARED: Stream prepared 976 * @SDW_STREAM_ENABLED: Stream enabled 977 * @SDW_STREAM_DISABLED: Stream disabled 978 * @SDW_STREAM_DEPREPARED: Stream de-prepared 979 * @SDW_STREAM_RELEASED: Stream released 980 */ 981 enum sdw_stream_state { 982 SDW_STREAM_ALLOCATED = 0, 983 SDW_STREAM_CONFIGURED = 1, 984 SDW_STREAM_PREPARED = 2, 985 SDW_STREAM_ENABLED = 3, 986 SDW_STREAM_DISABLED = 4, 987 SDW_STREAM_DEPREPARED = 5, 988 SDW_STREAM_RELEASED = 6, 989 }; 990 991 /** 992 * sdw_stream_params: Stream parameters 993 * 994 * @rate: Sampling frequency, in Hz 995 * @ch_count: Number of channels 996 * @bps: bits per channel sample 997 */ 998 struct sdw_stream_params { 999 unsigned int rate; 1000 unsigned int ch_count; 1001 unsigned int bps; 1002 }; 1003 1004 /** 1005 * sdw_stream_runtime: Runtime stream parameters 1006 * 1007 * @name: SoundWire stream name 1008 * @params: Stream parameters 1009 * @state: Current state of the stream 1010 * @type: Stream type PCM or PDM 1011 * @master_list: List of Master runtime(s) in this stream. 1012 * master_list can contain only one m_rt per Master instance 1013 * for a stream 1014 * @m_rt_count: Count of Master runtime(s) in this stream 1015 */ 1016 struct sdw_stream_runtime { 1017 const char *name; 1018 struct sdw_stream_params params; 1019 enum sdw_stream_state state; 1020 enum sdw_stream_type type; 1021 struct list_head master_list; 1022 int m_rt_count; 1023 }; 1024 1025 struct sdw_stream_runtime *sdw_alloc_stream(const char *stream_name); 1026 void sdw_release_stream(struct sdw_stream_runtime *stream); 1027 1028 int sdw_compute_params(struct sdw_bus *bus); 1029 1030 int sdw_stream_add_master(struct sdw_bus *bus, 1031 struct sdw_stream_config *stream_config, 1032 struct sdw_port_config *port_config, 1033 unsigned int num_ports, 1034 struct sdw_stream_runtime *stream); 1035 int sdw_stream_remove_master(struct sdw_bus *bus, 1036 struct sdw_stream_runtime *stream); 1037 int sdw_startup_stream(void *sdw_substream); 1038 int sdw_prepare_stream(struct sdw_stream_runtime *stream); 1039 int sdw_enable_stream(struct sdw_stream_runtime *stream); 1040 int sdw_disable_stream(struct sdw_stream_runtime *stream); 1041 int sdw_deprepare_stream(struct sdw_stream_runtime *stream); 1042 void sdw_shutdown_stream(void *sdw_substream); 1043 int sdw_bus_prep_clk_stop(struct sdw_bus *bus); 1044 int sdw_bus_clk_stop(struct sdw_bus *bus); 1045 int sdw_bus_exit_clk_stop(struct sdw_bus *bus); 1046 1047 int sdw_compare_devid(struct sdw_slave *slave, struct sdw_slave_id id); 1048 void sdw_extract_slave_id(struct sdw_bus *bus, u64 addr, struct sdw_slave_id *id); 1049 1050 #if IS_ENABLED(CONFIG_SOUNDWIRE) 1051 1052 int sdw_stream_add_slave(struct sdw_slave *slave, 1053 struct sdw_stream_config *stream_config, 1054 struct sdw_port_config *port_config, 1055 unsigned int num_ports, 1056 struct sdw_stream_runtime *stream); 1057 int sdw_stream_remove_slave(struct sdw_slave *slave, 1058 struct sdw_stream_runtime *stream); 1059 1060 /* messaging and data APIs */ 1061 int sdw_read(struct sdw_slave *slave, u32 addr); 1062 int sdw_write(struct sdw_slave *slave, u32 addr, u8 value); 1063 int sdw_write_no_pm(struct sdw_slave *slave, u32 addr, u8 value); 1064 int sdw_read_no_pm(struct sdw_slave *slave, u32 addr); 1065 int sdw_nread(struct sdw_slave *slave, u32 addr, size_t count, u8 *val); 1066 int sdw_nread_no_pm(struct sdw_slave *slave, u32 addr, size_t count, u8 *val); 1067 int sdw_nwrite(struct sdw_slave *slave, u32 addr, size_t count, const u8 *val); 1068 int sdw_nwrite_no_pm(struct sdw_slave *slave, u32 addr, size_t count, const u8 *val); 1069 int sdw_update(struct sdw_slave *slave, u32 addr, u8 mask, u8 val); 1070 int sdw_update_no_pm(struct sdw_slave *slave, u32 addr, u8 mask, u8 val); 1071 1072 #else 1073 1074 static inline int sdw_stream_add_slave(struct sdw_slave *slave, 1075 struct sdw_stream_config *stream_config, 1076 struct sdw_port_config *port_config, 1077 unsigned int num_ports, 1078 struct sdw_stream_runtime *stream) 1079 { 1080 WARN_ONCE(1, "SoundWire API is disabled"); 1081 return -EINVAL; 1082 } 1083 1084 static inline int sdw_stream_remove_slave(struct sdw_slave *slave, 1085 struct sdw_stream_runtime *stream) 1086 { 1087 WARN_ONCE(1, "SoundWire API is disabled"); 1088 return -EINVAL; 1089 } 1090 1091 /* messaging and data APIs */ 1092 static inline int sdw_read(struct sdw_slave *slave, u32 addr) 1093 { 1094 WARN_ONCE(1, "SoundWire API is disabled"); 1095 return -EINVAL; 1096 } 1097 1098 static inline int sdw_write(struct sdw_slave *slave, u32 addr, u8 value) 1099 { 1100 WARN_ONCE(1, "SoundWire API is disabled"); 1101 return -EINVAL; 1102 } 1103 1104 static inline int sdw_write_no_pm(struct sdw_slave *slave, u32 addr, u8 value) 1105 { 1106 WARN_ONCE(1, "SoundWire API is disabled"); 1107 return -EINVAL; 1108 } 1109 1110 static inline int sdw_read_no_pm(struct sdw_slave *slave, u32 addr) 1111 { 1112 WARN_ONCE(1, "SoundWire API is disabled"); 1113 return -EINVAL; 1114 } 1115 1116 static inline int sdw_nread(struct sdw_slave *slave, u32 addr, size_t count, u8 *val) 1117 { 1118 WARN_ONCE(1, "SoundWire API is disabled"); 1119 return -EINVAL; 1120 } 1121 1122 static inline int sdw_nread_no_pm(struct sdw_slave *slave, u32 addr, size_t count, u8 *val) 1123 { 1124 WARN_ONCE(1, "SoundWire API is disabled"); 1125 return -EINVAL; 1126 } 1127 1128 static inline int sdw_nwrite(struct sdw_slave *slave, u32 addr, size_t count, const u8 *val) 1129 { 1130 WARN_ONCE(1, "SoundWire API is disabled"); 1131 return -EINVAL; 1132 } 1133 1134 static inline int sdw_nwrite_no_pm(struct sdw_slave *slave, u32 addr, size_t count, const u8 *val) 1135 { 1136 WARN_ONCE(1, "SoundWire API is disabled"); 1137 return -EINVAL; 1138 } 1139 1140 static inline int sdw_update(struct sdw_slave *slave, u32 addr, u8 mask, u8 val) 1141 { 1142 WARN_ONCE(1, "SoundWire API is disabled"); 1143 return -EINVAL; 1144 } 1145 1146 static inline int sdw_update_no_pm(struct sdw_slave *slave, u32 addr, u8 mask, u8 val) 1147 { 1148 WARN_ONCE(1, "SoundWire API is disabled"); 1149 return -EINVAL; 1150 } 1151 1152 #endif /* CONFIG_SOUNDWIRE */ 1153 1154 #endif /* __SOUNDWIRE_H */ 1155