mbox series

[RFC,0/5] Apple Macs machine-level ASoC driver

Message ID 20220331000449.41062-1-povik+lin@cutebit.org
Headers show
Series Apple Macs machine-level ASoC driver | expand

Message

Martin Povišer March 31, 2022, 12:04 a.m. UTC
Hi,

I put together a machine-level ASoC driver for recent Apple Macs (the
ones with ARM64 SoCs) and want to gauge opinions.

Commit 1 is the binding. It is some subset of simple-audio-card with
the extra distinction of allowing multiple CPU/CODEC DAIs per a DAI
link. I want to draw special attention to the issue of describing
speaker topologies. The way it now works is that the driver expects
the speakers to be declared in a fixed order in the sound-dai= list.
This populates a topology the driver expects on a particular machine
model. Mark (in CC) has made the suggestion of keeping the topology
descriptions with the codec nodes themselves in some generic manner,
akin to how sound-name-prefix= already helps identify codecs to the
user.

Commit 2 adds a new ASoC card method (filter_controls) to let the card
prevent some codec kcontrols from being visible to userspace. For example
the TAS2770 speaker amp driver would be happy to expose TDM slot selection
and ISENSE/VSENSE enables which is ridiculous. I am all ears on how to
make the patch acceptable to upstream.

Commit 3 makes ASoC tolerate N-to-M DAI links, not sure what the right
(simple) approach should be there. Commit 4 adds some utility function
and commit 5 is the driver itself.

Let me know what you think.

Martin

Martin Povišer (5):
  dt-bindings: sound: Add Apple Macs sound system
  HACK: ASoC: Add card->filter_controls hook
  HACK: ASoC: Tolerate N-cpus-to-M-codecs links
  ASoC: Introduce snd_soc_of_get_dai_link_cpus
  ASoC: Add macaudio machine driver

 .../bindings/sound/apple,macaudio.yaml        | 103 +++
 include/sound/soc.h                           |   7 +
 sound/soc/apple/Kconfig                       |  10 +
 sound/soc/apple/Makefile                      |   3 +
 sound/soc/apple/macaudio.c                    | 597 ++++++++++++++++++
 sound/soc/soc-core.c                          | 125 +++-
 sound/soc/soc-dapm.c                          |  34 +-
 sound/soc/soc-pcm.c                           |   3 +
 8 files changed, 860 insertions(+), 22 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/sound/apple,macaudio.yaml
 create mode 100644 sound/soc/apple/Kconfig
 create mode 100644 sound/soc/apple/Makefile
 create mode 100644 sound/soc/apple/macaudio.c

Comments

Martin Povišer April 22, 2022, 10:43 a.m. UTC | #1
> On 31. 3. 2022, at 17:36, Mark Brown <broonie@kernel.org> wrote:
> 
> On Thu, Mar 31, 2022 at 05:04:32PM +0200, Martin Povišer wrote:
>>> On 31. 3. 2022, at 16:18, Mark Brown <broonie@kernel.org> wrote:
> 
>>> Yes, having two devices driving the bus at the same time wouldn't be
>>> great.  How is the TDM slot selection for the signals done in the
>>> hardware, I'm not seeing anything immediately obvious in the driver?
>>> I'd have thought that things would be implemented such that you could
>>> implement speaker protection on all speakers simultaneously but perhaps
>>> not.
> 
>> I don’t know. I would have to go study the details of this. Should I see
>> if I can find a combination of ‘ASI1 Sel’ ‘VSENSE’ ‘ISENSE’ settings
>> that would lead to driver conflict on one of the models, or is there
>> a chance we could hide those controls just on the basis of ‘it doesn’t
>> do anything usable and is possibly dangerous’?
> 
> If ISENSE and VSENSE output are controlled by the same mux as routing
> then we should lock one of the controls out for at least stereo devices
> (it might be a good idea to check if the output is actually high Z when
> ISENSE and VSENSE are off rather than just driving zeros, if not it
> definitely has to be the routing control).  My instinct is that it's
> better to preserve the ability to implement speaker protection in future
> since that is something that'd be broadly useful, especially if someone
> comes up with a generic speaker protection implementation in which case
> there should be an awful lot of systems out there which could benefit. 

Sorry for having put this on hold for a while.

I looked in the TAS2770 and TAS2764 drivers/datasheets, and to answer
the questions we had:

 * VSENSE/ISENSE output slots are configured independently of audio samples
   routing. Kernel drivers configure the slots based on the 'ti,imon-slot-no'
   and 'ti,vmon-slot-no' properties of devicetree.

 * By default codecs transmit Hi-Z for duration of unused slots.

So once we supply the devicetree props it should be electrically sound
under any configuration of userspace knobs.

One final thought on the playback routing controls: On systems with >2
speakers, the codecs need to be assigned slots through set_tdm_slot.
The macaudio driver RFCed here assigns a single slot to each speaker,
making the effect of each speaker's routing control this:

  'I2C offset' -- uses a random slot

  'Left' 'Right' 'LeftRight' -- uses the single slot we configured

I suppose I better assign two slots to speakers in each left-right pair
of the same kind (e.g. woofer 1, woofer 2, tweeter). This way the
routing control will mimic its behavior from simple stereo systems but
replicated within each left-right pair.  (I would prefer to hide the
controls altogether, but as I learned that hiding things unless proven
dangerous is an ASoC non-goal, this way I can make the controls do
something interesting.)

Martin
Mark Brown April 22, 2022, 11:19 a.m. UTC | #2
On Fri, Apr 22, 2022 at 12:43:30PM +0200, Martin Povišer wrote:

> I looked in the TAS2770 and TAS2764 drivers/datasheets, and to answer
> the questions we had:

>  * VSENSE/ISENSE output slots are configured independently of audio samples
>    routing. Kernel drivers configure the slots based on the 'ti,imon-slot-no'
>    and 'ti,vmon-slot-no' properties of devicetree.

>  * By default codecs transmit Hi-Z for duration of unused slots.

> So once we supply the devicetree props it should be electrically sound
> under any configuration of userspace knobs.

Great, that's a relief.

> One final thought on the playback routing controls: On systems with >2
> speakers, the codecs need to be assigned slots through set_tdm_slot.
> The macaudio driver RFCed here assigns a single slot to each speaker,
> making the effect of each speaker's routing control this:

>   'I2C offset' -- uses a random slot

>   'Left' 'Right' 'LeftRight' -- uses the single slot we configured

> I suppose I better assign two slots to speakers in each left-right pair
> of the same kind (e.g. woofer 1, woofer 2, tweeter). This way the
> routing control will mimic its behavior from simple stereo systems but
> replicated within each left-right pair.  (I would prefer to hide the
> controls altogether, but as I learned that hiding things unless proven
> dangerous is an ASoC non-goal, this way I can make the controls do
> something interesting.)

I don't quite grasp the difference between the arrangement you're
proposing and assigning a single slot to each speaker?  Possibly it's
just a reordering of the slots?
Martin Povišer April 22, 2022, 11:28 a.m. UTC | #3
> On 22. 4. 2022, at 13:19, Mark Brown <broonie@kernel.org> wrote:
> 
> On Fri, Apr 22, 2022 at 12:43:30PM +0200, Martin Povišer wrote:
> 
>> I looked in the TAS2770 and TAS2764 drivers/datasheets, and to answer
>> the questions we had:
> 
>> * VSENSE/ISENSE output slots are configured independently of audio samples
>>   routing. Kernel drivers configure the slots based on the 'ti,imon-slot-no'
>>   and 'ti,vmon-slot-no' properties of devicetree.
> 
>> * By default codecs transmit Hi-Z for duration of unused slots.
> 
>> So once we supply the devicetree props it should be electrically sound
>> under any configuration of userspace knobs.
> 
> Great, that's a relief.
> 
>> One final thought on the playback routing controls: On systems with >2
>> speakers, the codecs need to be assigned slots through set_tdm_slot.
>> The macaudio driver RFCed here assigns a single slot to each speaker,
>> making the effect of each speaker's routing control this:
> 
>>  'I2C offset' -- uses a random slot
> 
>>  'Left' 'Right' 'LeftRight' -- uses the single slot we configured
> 
>> I suppose I better assign two slots to speakers in each left-right pair
>> of the same kind (e.g. woofer 1, woofer 2, tweeter). This way the
>> routing control will mimic its behavior from simple stereo systems but
>> replicated within each left-right pair.  (I would prefer to hide the
>> controls altogether, but as I learned that hiding things unless proven
>> dangerous is an ASoC non-goal, this way I can make the controls do
>> something interesting.)
> 
> I don't quite grasp the difference between the arrangement you're
> proposing and assigning a single slot to each speaker?  Possibly it's
> just a reordering of the slots?

Ah, maybe what’s missing is the fact that the way the speaker amp drivers
are written, if they are assigned two slots with a call to set_tdm_slot,
the first slot is considered 'left' and the second is 'right'.

So in the arrangement I am proposing the 'Left', 'Right' and 'LeftRight'
values of the routing control have the nominal effect (within the left-right
speaker pair), while in the other arrangement it is as I described above.
Mark Brown April 22, 2022, 11:33 a.m. UTC | #4
On Fri, Apr 22, 2022 at 01:28:20PM +0200, Martin Povišer wrote:
> > On 22. 4. 2022, at 13:19, Mark Brown <broonie@kernel.org> wrote:
> > On Fri, Apr 22, 2022 at 12:43:30PM +0200, Martin Povišer wrote:

> >> One final thought on the playback routing controls: On systems with >2
> >> speakers, the codecs need to be assigned slots through set_tdm_slot.
> >> The macaudio driver RFCed here assigns a single slot to each speaker,
> >> making the effect of each speaker's routing control this:

...

> > I don't quite grasp the difference between the arrangement you're
> > proposing and assigning a single slot to each speaker?  Possibly it's
> > just a reordering of the slots?

> Ah, maybe what’s missing is the fact that the way the speaker amp drivers
> are written, if they are assigned two slots with a call to set_tdm_slot,
> the first slot is considered 'left' and the second is 'right'.

> So in the arrangement I am proposing the 'Left', 'Right' and 'LeftRight'
> values of the routing control have the nominal effect (within the left-right
> speaker pair), while in the other arrangement it is as I described above.

So previously each speaker would get two slots but now it just gets one?
Martin Povišer April 22, 2022, 11:44 a.m. UTC | #5
> On 22. 4. 2022, at 13:33, Mark Brown <broonie@kernel.org> wrote:
> 
> On Fri, Apr 22, 2022 at 01:28:20PM +0200, Martin Povišer wrote:
>>> On 22. 4. 2022, at 13:19, Mark Brown <broonie@kernel.org> wrote:
>>> On Fri, Apr 22, 2022 at 12:43:30PM +0200, Martin Povišer wrote:
> 
>>>> One final thought on the playback routing controls: On systems with >2
>>>> speakers, the codecs need to be assigned slots through set_tdm_slot.
>>>> The macaudio driver RFCed here assigns a single slot to each speaker,
>>>> making the effect of each speaker's routing control this:
> 
> ...
> 
>>> I don't quite grasp the difference between the arrangement you're
>>> proposing and assigning a single slot to each speaker?  Possibly it's
>>> just a reordering of the slots?
> 
>> Ah, maybe what’s missing is the fact that the way the speaker amp drivers
>> are written, if they are assigned two slots with a call to set_tdm_slot,
>> the first slot is considered 'left' and the second is 'right'.
> 
>> So in the arrangement I am proposing the 'Left', 'Right' and 'LeftRight'
>> values of the routing control have the nominal effect (within the left-right
>> speaker pair), while in the other arrangement it is as I described above.
> 
> So previously each speaker would get two slots but now it just gets one?

No the other way around. Previously (with the driver as it is RFCed),
each speaker gets a single slot, and 'Left', 'Right' and ‘LeftRight'
values of the routing control don't do anything different from each
other (well except maybe 'LeftRight' lessens the volume due to how
the chip handles the edge case of mixing down two channels from the
same slot).

With the new arrangement I am proposing, the two speakers in a left-right
pair get both the same two slots, meaning they get to choose one of the
two slots based on the 'Left' 'Right' value of their routing control.
Mark Brown April 22, 2022, 12:22 p.m. UTC | #6
On Fri, Apr 22, 2022 at 01:44:06PM +0200, Martin Povišer wrote:

> > So previously each speaker would get two slots but now it just gets one?

> No the other way around. Previously (with the driver as it is RFCed),
> each speaker gets a single slot, and 'Left', 'Right' and ‘LeftRight'
> values of the routing control don't do anything different from each
> other (well except maybe 'LeftRight' lessens the volume due to how
> the chip handles the edge case of mixing down two channels from the
> same slot).

> With the new arrangement I am proposing, the two speakers in a left-right
> pair get both the same two slots, meaning they get to choose one of the
> two slots based on the 'Left' 'Right' value of their routing control.

Ah, I think the confusion here is that I'm using slot and channel
interchangably whereas you're saying that previously the driver would
allocate two channels to each speaker with duplicate data?
Mark Brown April 22, 2022, 12:44 p.m. UTC | #7
On Fri, Apr 22, 2022 at 02:36:03PM +0200, Martin Povišer wrote:

> > Ah, I think the confusion here is that I'm using slot and channel
> > interchangably whereas you're saying that previously the driver would
> > allocate two channels to each speaker with duplicate data?

> I guess you could say that. Not that there’s duplicate data on the I2S
> bus, but the speaker amp would previously be configured to look for the
> left and right channel in the same TDM slot (see e.g. set_tdm_slot of
> tas2770 [0]).  (Each speaker amp drives a single speaker, but it still
> has a notion of left and right channel.)

Oh, I see - the speaker actually allows configuration of the slots
independently.  Usually the left/right thing on mono devices only does
something for I2S where the bus clocking enforces that there be both
left and right channels.  Either configuration is fine by me TBH, if you
can do that then you could just keep them mapped to the same channel
then mark the control as disabled since it should have no effect.
Martin Povišer April 22, 2022, 12:53 p.m. UTC | #8
> On 22. 4. 2022, at 14:44, Mark Brown <broonie@kernel.org> wrote:
> 
> On Fri, Apr 22, 2022 at 02:36:03PM +0200, Martin Povišer wrote:
> 
>>> Ah, I think the confusion here is that I'm using slot and channel
>>> interchangably whereas you're saying that previously the driver would
>>> allocate two channels to each speaker with duplicate data?
> 
>> I guess you could say that. Not that there’s duplicate data on the I2S
>> bus, but the speaker amp would previously be configured to look for the
>> left and right channel in the same TDM slot (see e.g. set_tdm_slot of
>> tas2770 [0]).  (Each speaker amp drives a single speaker, but it still
>> has a notion of left and right channel.)
> 
> Oh, I see - the speaker actually allows configuration of the slots
> independently.  Usually the left/right thing on mono devices only does
> something for I2S where the bus clocking enforces that there be both
> left and right channels.  Either configuration is fine by me TBH, if you
> can do that then you could just keep them mapped to the same channel
> then mark the control as disabled since it should have no effect.

Well but is there some established way to mark a control as disabled?

Another issue here is that if I disable it I can’t leave the routing
control in it’s default value, which is ‘I2C Offset’ and makes the speaker
amp ignore the slot mapping.
Mark Brown April 22, 2022, 1:06 p.m. UTC | #9
On Fri, Apr 22, 2022 at 02:53:54PM +0200, Martin Povišer wrote:

> > Oh, I see - the speaker actually allows configuration of the slots
> > independently.  Usually the left/right thing on mono devices only does
> > something for I2S where the bus clocking enforces that there be both
> > left and right channels.  Either configuration is fine by me TBH, if you
> > can do that then you could just keep them mapped to the same channel
> > then mark the control as disabled since it should have no effect.

> Well but is there some established way to mark a control as disabled?

snd_ctl_activate_id().

> Another issue here is that if I disable it I can’t leave the routing
> control in it’s default value, which is ‘I2C Offset’ and makes the speaker
> amp ignore the slot mapping.

Sure, that's fine - if a control genuinely has no effect it's fine to
hide it from userspace.  The issue is where it's just that you don't see
the use, if the control demonstrably does nothing then that's fine.
Martin Povišer April 22, 2022, 1:59 p.m. UTC | #10
> On 22. 4. 2022, at 15:06, Mark Brown <broonie@kernel.org> wrote:
> 
> On Fri, Apr 22, 2022 at 02:53:54PM +0200, Martin Povišer wrote:
> 
>>> Oh, I see - the speaker actually allows configuration of the slots
>>> independently.  Usually the left/right thing on mono devices only does
>>> something for I2S where the bus clocking enforces that there be both
>>> left and right channels.  Either configuration is fine by me TBH, if you
>>> can do that then you could just keep them mapped to the same channel
>>> then mark the control as disabled since it should have no effect.
> 
>> Well but is there some established way to mark a control as disabled?
> 
> snd_ctl_activate_id().

Ha! Great.

>> Another issue here is that if I disable it I can’t leave the routing
>> control in it’s default value, which is ‘I2C Offset’ and makes the speaker
>> amp ignore the slot mapping.
> 
> Sure, that's fine - if a control genuinely has no effect it's fine to
> hide it from userspace.  The issue is where it's just that you don't see
> the use, if the control demonstrably does nothing then that's fine.

So I assume I can set the control from the machine driver then disable it.

Anyway, good, this is what I meant earlier when I said the controls I want
to hide are 'useless/confusing at best’. I must walk back that they are
‘dangerous at worst’, but I am glad we can hide them anyway. (Not all of
them of course, ISENSE/VSENSE will not be hidden, neither the routing
control on systems with single mono speaker.)