sdw.h 32 KB

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