ASoC jack detection

ALSA has a standard API for representing physical jacks to user space,the kernel side of which can be seen in include/sound/jack.h. ASoCprovides a version of this API adding two additional features:

  • It allows more than one jack detection method to work together on oneuser visible jack. In embedded systems it is common for multipleto be present on a single jack but handled by separate bits ofhardware.

  • Integration with DAPM, allowing DAPM endpoints to be updatedautomatically based on the detected jack status (eg, turning off theheadphone outputs if no headphones are present).

This is done by splitting the jacks up into three things workingtogether: the jack itself represented by astructsnd_soc_jack, sets ofsnd_soc_jack_pins representing DAPM endpoints to update and blocks ofcode providing jack reporting mechanisms.

For example, a system may have a stereo headset jack with two reportingmechanisms, one for the headphone and one for the microphone. Somesystems won’t be able to use their speaker output while a headphone isconnected and so will want to make sure to update both speaker andheadphone when the headphone jack status changes.

The jack - struct snd_soc_jack

This represents a physical jack on the system and is what is visible touser space. The jack itself is completely passive, it is set up by themachine driver and updated by jack detection methods.

Jacks are created by the machine driver callingsnd_soc_jack_new().

snd_soc_jack_pin

These represent a DAPM pin to update depending on some of the statusbits supported by the jack. Each snd_soc_jack has zero or more of thesewhich are updated automatically. They are created by the machine driverand associated with the jack usingsnd_soc_jack_add_pins(). The statusof the endpoint may configured to be the opposite of the jack status ifrequired (eg, enabling a built in microphone if a microphone is notconnected via a jack).

Jack detection methods

Actual jack detection is done by code which is able to monitor someinput to the system and update a jack by callingsnd_soc_jack_report(),specifying a subset of bits to update. The jack detection code shouldbe set up by the machine driver, taking configuration for the jack toupdate and the set of things to report when the jack is connected.

Often this is done based on the status of a GPIO - a handler for this isprovided by thesnd_soc_jack_add_gpio() function. Other methods arealso available, for example integrated into CODECs. One example ofCODEC integrated jack detection can be see in the WM8350 driver.

Each jack may have multiple reporting mechanisms, though it will need atleast one to be useful.

Machine drivers

These are all hooked together by the machine driver depending on thesystem hardware. The machine driver will set up the snd_soc_jack andthe list of pins to update then set up one or more jack detectionmechanisms to update that jack based on their current status.