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