xref: /aosp_15_r20/external/coreboot/Documentation/drivers/soundwire.md (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1# SoundWire Implementation in coreboot
2
3## Introduction
4
5SoundWire is an audio interface specification from the MIPI Alliance.
6
7- Low complexity
8- Low power
9- Low latency
10- Two pins (clock and data)
11- Multi-drop capable
12- Multiple audio streams
13- Embedded control/command channel
14
15The main *SoundWire Specification* is at version 1.2 and can be downloaded from
16<https://mipi.org> but it is unfortunately only available to MIPI Alliance members.
17
18There is a separate *SoundWire Discovery and Configuration (DisCo) Specification* which
19is at version 1.0 and is available for non-members after providing name and email at
20<https://resources.mipi.org/disco_soundwire>.
21
22The coreboot implementation is based on the SoundWire DisCo Specification which defines
23object hierarchy and properties for providing topology and configuration information to
24OS kernel drivers via ACPI or DeviceTree.
25
26SoundWire itself is architecture independent and the coreboot basic definition is also not
27specific to any to any SoC.  The examples in this document use ACPI to generate properties,
28but the same structures and properties would be needed in a DeviceTree implementation.
29
30## Bus
31
32The SoundWire bus commonly consists of two pins:
33
34* Clock: A common clock signal distributed from the master to all of the slaves.
35* Data: A shared data signal that can be driven by any of the devices, and has a defined
36value when no device is driving it.
37
38While most designs have one data lane it is possible for a multi-lane device to have up
39to 8 data lanes and thus would have more than two pins.
40
41A SoundWire bus consists of one master device, up to 11 slave devices, and an optional
42monitor interface for debug.
43
44SoundWire is an enumerable bus, but not a discoverable one.  That means it is required
45for firmware to provide details about the connected devices to the OS.
46
47### Controller
48
49A SoundWire controller contains one or more master devices.  The handling of multiple
50masters is left up to the implementation, they may share a clock or be operated
51independently or entirely in tandem.  The master devices connected to a controller are
52also referred to as links.
53
54In coreboot the controller device is provided by the SoC or an add-in PCI card.
55
56### Master
57
58A SoundWire master (or link) device is responsible for clock and data handling, bus
59management, and bit slot allocation.
60
61In coreboot the definition of the master device is left up to the controller and the
62mainboard should only need to know the controller's SoundWire topology (number of masters)
63to configure `devicetree.cb`.
64
65It may however be expected to provide some additional SoC-specific configuration data to
66the controller, such as an input clock rate or a list of available masters that cannot
67be determined at run time.
68
69### Slave
70
71SoundWire slave devices are connected to a master and respond to the two-wire control
72information on the SoundWire bus.  There can be up to 11 slave devices on a bus and they
73are capable of interrupting and waking the host.
74
75Slave devices may also have master links which can be connected to other slave devices.
76It is also possible for a multi-lane slave device to have multiple data lanes connected
77to different combinations of master and slave devices.
78
79In coreboot the slave device is defined by a codec driver which should be found in the
80source tree at `src/drivers/soundwire`.
81
82The mainboard provides:
83
84* Master link that this slave device is connected to.
85* Unique ID that this codec responds to on the SoundWire bus.
86* Multi-lane mapping. (optional)
87
88The codec driver provides:
89
90* Slave device properties.
91* Audio Mode properties including bus frequencies and sampling rates.
92* Data Port 1-14 properties such as word lengths, interrupt support, channels.
93* Data Port 0 and Bulk Register Access properties. (optional)
94
95### Monitor
96
97A SoundWire monitor device is defined that allows for test equipment to snoop the bus and
98take over and issue commands.  The monitor interface is not defined for coreboot.
99
100### Example SoundWire Bus
101
102```
103+---------------+                                       +---------------+
104|               |                       Clock Signal    |               |
105|    Master     |-------+-------------------------------|    Slave      |
106|   Interface   |       |               Data Signal     |  Interface 1  |
107|               |-------|-------+-----------------------|               |
108+---------------+       |       |                       +---------------+
109                        |       |
110                        |       |
111                        |       |
112                     +--+-------+--+
113                     |             |
114                     |   Slave     |
115                     | Interface 2 |
116                     |             |
117                     +-------------+
118```
119
120## coreboot
121
122The coreboot implementation of SoundWire integrates with the device model and takes
123advantage of the hierarchical nature of `devicetree.cb` to populate the topology.
124
125The architecture-independent SoundWire tables are defined at
126
127    src/include/device/soundwire.h
128
129Support for new devices comes in three forms:
130
1311. New controller and master drivers.  The first implementation in coreboot is for an Intel
132SoC but the SoundWire specification is in wide use on various ARM SoCs.
133
134    Controller drivers can be implemented in `src/soc` or `src/drivers` and should
135    strive to re-use code as much as possible between different SoC generations from the
136    same vendor.
137
1382. New codec drivers.  These should be implemented for each codec that is added which
139supports SoundWire.  The properties vary between codecs and careful study of the data sheet
140is necessary to ensure proper operation.
141
142    Codec drivers should be implemented in `src/drivers/soundwire` as separate chip drivers.
143    As every codec is different there may not be opportunities of code re-use except between
144    similar codecs from the same vendor.
145
1463. New mainboards with SoundWire support.  The mainboard will combine controllers and codecs
147to form a topology that is described in `devicetree.cb`.  Some devices may need to provide
148board-specific configuration information, and multi-lane devices will need to provide the
149master/slave lane map.
150
151## ACPI Implementation
152
153The implementation for x86 devices relies on ACPI for providing device properties to the OS
154kernel drivers.
155
156The ACPI implementation can be found at
157
158    src/acpi/soundwire.c
159
160And used by including
161
162    #include <acpi/acpi_soundwire.h>
163
164### Controller
165
166The controller driver should populate a `struct soundwire_controller`:
167
168```c
169/**
170 * struct soundwire_controller - SoundWire controller properties.
171 * @master_count: Number of masters present on this device.
172 * @master_list: One entry for each master device.
173 */
174struct soundwire_controller {
175	unsigned int master_list_count;
176	struct soundwire_link master_list[SOUNDWIRE_MAX_DEV];
177};
178```
179
180Once the detail of the master links are specified in the `master_list` variable, the controller
181properties for the ACPI object can be generated:
182
183```c
184struct acpi_dp *dsd = acpi_dp_new_table("_DSD");
185soundwire_gen_controller(dsd, &soc_controller, NULL);
186acpi_dp_write(dsd);
187```
188
189If the controller needs to generate custom properties for links it can provide a callback
190function to `soundwire_gen_controller()` instead of passing NULL:
191
192```c
193static void controller_link_prop_cb(struct acpi_dp *dsd, unsigned int id,
194                                    struct soundwire_controller *controller)
195{
196	acpi_dp_add_integer(dsd, "custom-link-property", 1);
197}
198```
199
200### Codec
201
202The codec driver should populate a *struct soundwire_codec* with necessary properties:
203
204```c
205/**
206 * struct soundwire_codec - Contains all configuration for a SoundWire codec slave device.
207 * @slave: Properties for slave device.
208 * @audio_mode: Properties for Audio Mode for Data Ports 1-14.
209 * @dpn: Properties for Data Ports 1-14.
210 * @multilane: Properties for slave multilane device. (optional)
211 * @dp0_bra_mode: Properties for Bulk Register Access mode for Data Port 0. (optional)
212 * @dp0: Properties for Data Port 0 for Bulk Register Access. (optional)
213 */
214struct soundwire_codec {
215	struct soundwire_slave *slave;
216	struct soundwire_audio_mode *audio_mode[SOUNDWIRE_MAX_DEV];
217	struct soundwire_dpn_entry dpn[SOUNDWIRE_MAX_DPN - SOUNDWIRE_MIN_DPN];
218	struct soundwire_multilane *multilane;
219	struct soundwire_bra_mode *dp0_bra_mode[SOUNDWIRE_MAX_DEV];
220	struct soundwire_dp0 *dp0;
221};
222```
223
224Many of these properties are optional, and depending on the codec will not be supported.
225
226#### Slave Device Properties
227
228These properties provide information about the codec device and what features it supports:
229
230* Wake capability
231* Clock stop behavior
232* Clock and channel state machine behavior
233* Features like register pages, broadcast read, bank delay, and high performance PHY
234
235#### Multi-lane Slave Device Properties
236
237Most slave devices have a single data pin and a single lane, but it is possible for up to
2387 other lanes to be supported on a device.  These lanes can be connected to other master
239links or to other slave devices.
240
241If a codec supports this feature it must indicate that by providing an entry for
242`struct soundwire_multilane` in the chip configuration.
243
244```c
245/**
246 * struct drivers_soundwire_example_config - Example codec configuration.
247 * @multilane: Multi-lane slave configuration.
248 */
249struct drivers_soundwire_example_config {
250	struct soundwire_multilane multilane;
251};
252```
253
254The mainboard is required to provide the lane map in `devicetree.cb` for any codec that has
255multiple lanes connected.  This includes the definition up to 7 entries that indicate which
256lane number on the slave devices (array index starting at 1) maps to which other device:
257
258```
259chip drivers/soundwire/multilane_codec
260	register "multilane.lane_mapping" = "{
261		{
262			# Slave Lane 1 maps to Master Lane 2
263			.lane = 1,
264			.direction = MASTER_LANE,
265			.connection.master_lane = 2
266		},
267		{
268			# Slave Lane 3 maps to Slave Link B
269			.lane = 3,
270			.direction = SLAVE_LINK,
271			.connection.slave_link = 1
272		}
273	}"
274	device generic 0.0 on end
275end
276```
277
278#### Data Port 0 Properties
279
280SoundWire Data Port 0 (DP0) is a special port used for control and status operation relating
281to the whole device interface, and as a special data port for bulk read/write operations.
282
283The properties for data port 0 are different from that of data ports 1-14 and are about the
284control channel behavior and the overall bulk register mode.
285
286Data port 0 is not required to be supported by the slave device.
287
288#### Bulk Register Access Mode Properties
289
290Bulk Register Access (BRA) is an optional mechanism for transporting higher bandwidth of
291register operations than the typical command mechanism.  The BRA protocol is a particular
292format of the data on the (optional) data port 0 connection between the master and slave.
293
294The BRA protocol may have alignment or timing requirements that are directly related to the
295bus frequencies.  As a result there may be several configurations listed, for symmetry with
296the audio modes paired with data ports 1-14.
297
298#### Data Port 1-14 Properties
299
300Data ports 1-14 are typically dedicated to streaming audio payloads, and each data port can
301have from 1 to 8 channels.  There are different levels of data ports, with some registers
302being required and supported on all data ports and some optional registers only being used
303on some data ports.
304
305Data ports can have both a sink and a source component, and the codec may support one or
306both of these on each port.
307
308Similar to data port 0 the properties defined here describe the capabilities and supported
309features of each data port, and they may be configured separately.  For example the Maxim
310MAX98373 codec supports a 32bit source data port for speaker output, and a 16bit sink data
311port for speaker sense data.
312
313#### Audio Mode Properties
314
315Each data port may be tied to one or more audio modes.  The audio mode describes the actual
316audio capabilities of the codec, including supported frequencies and sample rates.  These
317modes can be shared by multiple data ports and do not need to be duplicated.
318
319For example:
320
321```
322static struct soundwire_audio_mode audio_mode = {
323	.bus_frequency_max = 24 * MHz,
324	.bus_frequency_min = 24 * KHz,
325	.max_sampling_frequency = 192 * KHz,
326	.min_sampling_frequency = 8 * KHz,
327};
328static struct soundwire_dpn codec_dp1 = {
329    [...]
330	.port_audio_mode_count = 1,
331	.port_audio_mode_list = {0}
332};
333static struct soundwire_dpn codec_dp3 = {
334    [...]
335	.port_audio_mode_count = 1,
336	.port_audio_mode_list = {0}
337};
338```
339
340### Generating Codec Properties
341
342Once the properties are known it can generate the ACPI code with:
343
344```c
345struct acpi_dp *dsd = acpi_dp_new_table("_DSD");
346soundwire_gen_codec(dsd, &soundwire_codec, NULL);
347acpi_dp_write(dsd);
348```
349
350If the codec needs to generate custom properties for links it can provide a callback
351function to `soundwire_gen_codec()` instead of passing NULL:
352
353```c
354static void codec_dp_prop_cb(struct acpi_dp *dsd, unsigned int id,
355                             struct soundwire_codec *codec)
356{
357	acpi_dp_add_integer(dsd, "custom-dp-property", 1);
358}
359```
360
361#### Codec Address
362
363SoundWire slave devices use a SoundWire defined ACPI _ADR that requires a 64-bit integer
364and uses the master link ID and slave device unique ID to form a unique address for the
365device on this controller.
366
367SoundWire addresses must be distinguishable from all other slave devices on the same master
368link, so multiple instances of the same manufacturer and part on the same master link will
369need different unique IDs.  The value is typically determined by strapping pins on the codec
370chip and can be decoded for this table with the codec datasheet and board schematics.
371
372```c
373/**
374 * struct soundwire_address - SoundWire ACPI Device Address Encoding.
375 * @version: SoundWire specification version from &enum soundwire_version.
376 * @link_id: Zero-based SoundWire Link Number.
377 * @unique_id: Unique ID for multiple devices.
378 * @manufacturer_id: Manufacturer ID from include/mipi/ids.h.
379 * @part_id: Vendor defined part ID.
380 * @class: MIPI class encoding in &enum mipi_class.
381 */
382struct soundwire_address {
383	enum soundwire_version version;
384	uint8_t link_id;
385	uint8_t unique_id;
386	uint16_t manufacturer_id;
387	uint16_t part_id;
388	enum mipi_class class;
389};
390```
391
392This ACPI address can be generated by calling the provided acpigen function:
393
394    acpigen_write_ADR_soundwire_device(const struct soundwire_address *sdw);
395
396### Mainboard
397
398The mainboard needs to select appropriate drivers in `Kconfig` and define the topology in
399`devicetree.cb` with the controllers and codecs that exist on the board.
400
401The topology uses the **generic** device to describe SoundWire:
402
403```c
404struct generic_path {
405	unsigned int id;       /* SoundWire Master Link ID */
406	unsigned int subid;    /* SoundWire Slave Unique ID */
407};
408```
409
410This allows devices to be specified in `devicetree.cb` with the necessary information to
411generate ACPI address and device properties.
412
413```
414chip drivers/intel/soundwire
415	# SoundWire Controller 0
416	device generic 0 on
417		chip drivers/soundwire/codec1
418			# SoundWire Link 0 ID 0
419			device generic 0.0 on end
420		end
421		chip drivers/soundwire/codec2
422			# SoundWire Link 1 ID 2
423			device generic 1.2 on end
424		end
425	end
426end
427```
428
429## Volteer Example
430
431This is an example of an Intel Tiger Lake reference board using SoundWire Link 0 for the
432headphone codec connection, and Link 1 for connecting two speaker amps for stereo speakers.
433
434The mainboard can be found at
435
436    src/mainboard/google/volteer
437
438```
439  +------------------+         +-------------------+
440  |                  |         | Headphone Codec   |
441  | Intel Tiger Lake |    +--->| Realtek ALC5682   |
442  |    SoundWire     |    |    |       ID 1        |
443  |    Controller    |    |    +-------------------+
444  |                  |    |
445  |           Link 0 +----+    +-------------------+
446  |                  |         | Left Speaker Amp  |
447  |           Link 1 +----+--->| Maxim MAX98373    |
448  |                  |    |    |       ID 3        |
449  |           Link 2 |    |    +-------------------+
450  |                  |    |
451  |           Link 3 |    |    +-------------------+
452  |                  |    |    | Right Speaker Amp |
453  +------------------+    +--->| Maxim MAX98373    |
454                               |       ID 7        |
455                               +-------------------+
456```
457
458This implementation requires a controller driver for the Intel Tigerlake SoC and a codec
459driver for the Realtek and Maxim chips.  If those drivers did not already exist they would
460need to be added and reviewed separately before adding the support to the mainboard.
461
462The volteer example requires some `Kconfig` options to be selected:
463
464```
465config BOARD_GOOGLE_BASEBOARD_VOLTEER
466	select DRIVERS_INTEL_SOUNDWIRE
467	select DRIVERS_SOUNDWIRE_ALC5682
468	select DRIVERS_SOUNDWIRE_MAX98373
469```
470
471And the following `devicetree.cb` entries to define this topology:
472
473```
474device pci 1f.3 on
475	chip drivers/intel/soundwire
476		# SoundWire Controller 0
477		device generic 0 on
478			chip drivers/soundwire/alc5682
479				# SoundWire Link 0 ID 1
480				register "desc" = ""Headphone Jack""
481				device generic 0.1 on end
482			end
483			chip drivers/soundwire/max98373
484				# SoundWire Link 0 ID 1
485				register "desc" = ""Left Speaker Amp""
486				device generic 1.3 on end
487			end
488			chip drivers/soundwire/max98373
489				# SoundWire Link 1 ID 7
490				register "desc" = ""Right Speaker Amp""
491				device generic 1.7 on end
492			end
493		end
494	end
495end
496```
497