Handheld Console / Raspberry Pi CM4 Amp

Hi All!
I'm incorporating an amplifier into a handheld console I've been developing. I'm trying to keep things simple as the rest of the board has been complex so trying to avoid the need for GPIO to talk to the amp. It takes the PWM signals from a raspberry Pi CM4 through the following driver buffer:
View attachment 1319011
Rather than the audio jack I then intend to run the signals through an amplifier (something like a TPA2012D2):
View attachment 1319015 The idea is I would then take the outputs to drive two onboard speakers but then when I plug in headphones to an audio jack this is rerouted into the headphones. This can be achieved using a headphone jack like the one in this image:
View attachment 1319014
The problem is the outputs from the amp are L+, L-, R+, R- so I'm guessing I need to use some form of op-amp configuration to convert this to a single-ended output. Then I can achieve the above since it will work for the headphones and the drivers.
Am I on the right lines here? Is this a stupid way of doing things? I'm fairly good with PCB layouts and Arduino stuff but for whatever reason when it comes to Pi's and amps that talk over I2C to set everything up my brain switches off.

Perhaps there's an Amp IC out there that offers Speaker Out/Headphone Out along with a detect pin for when the headphones are inserted? No need for any other MCU to handle things?
 
It would seem I2C is the way to go, if you can.
I have used this little board before for a similar project: https://thepihut.com/products/adafruit-mono-2-5w-class-d-audio-amplifier-pam8302 it is simple and easy to setup, although this is a mono only board I would presume there will be a stereo equivalent.
It can also easily drive headphones.
The only issue I can remember with it is a large 'POP' as the board starts to play, this was solved be addressing the SD 'shutdown' pin via a python GPIO call just before and after playback.

I could do with knowing more about your requirements, are you just needing beeps and tones or full music? what are the drive requirements for your headphones?

The headphone jack in the last link appears to be a good and simple solution if your output L R share common ground.
 
Hey thanks for the reply! Sorry it took me a while to see it.
Yeh I figured biting the bullet and doing it properly with an I2C interface is probably the way to go. Then I can use the aux port to send a signal to trigger when the amp sends audio to the headphones or speakers.
Essentially the project is a raspberry Pi CM4 integrated into my own PCB as part of a Retropie handheld console. So it will be emulating games from gameboy advance to PSP. On the audio side I can take the PWM outputs and create an audio signal as I shared but then I need to amplify it for some integrated speakers. Ideally stereo but mono is an option. Then when I plug in headphones it should stop output to these speakers and drive the headphones.
The other option is I2S as you suggest but that's a little more involved from the coding side I believe? In which case I still need the same response with the headphones/speakers. Have you come across any solutions that allowed for headphones/speakers?
 
I found this: https://learn.adafruit.com/adafruit-speaker-bonnet-for-raspberry-pi
3346-09.jpg

I would say this would fit your needs, I don't know how much space you can afford in your console but I'm thinking you could daughter board it on your main board and maybe de-solder the connectors if you need to make it slimmer.
As for headphones, any board that is designed for driving speakers will handle headphone with ease. Check that the speaker out grounds are connected together and then you can just wire your headphone jack the same as the picture in your post.

When it comes to software setup adafruit provides a script and instructions here:
https://learn.adafruit.com/adafruit-speaker-bonnet-for-raspberry-pi/raspberry-pi-usage

It looks very straight forward.
Do you have any pictures of your project yet?
 
That's a cool hat. I'm using the Raspberry Pi CM4 (super small, no GPIO out like the normal PI) so everything is integrated on a single board. The amplifier chip they use on that hat could prove useful though so thanks! My issue isn't so much will it drive headphones but just telling the amplifier "don't send any signal to the speakers as there's headphones plugged in now".
You highlighter the issue I found with using the mechanical switches in an aux port though in that most modern amps will drive out L+, L-, etc. and not share a common ground as is needed for a headphone jack. In reality I think I will need to use a modern IC amplifier that has dedicated out for both drivers and headphones. They normally have some form of I2c control for telling the amp when to switch between the outputs and that would be driven by the Pi. Just didn't want to get too deep into coding but alas it may be unavoidable.
No pics as of yet just a page of pure electronics schematics hahah! Will share as it develops. Keen to get onto the physical design soon.
 
I am thinking about how are they driving the speaker from a single rail supply without coupling capacitors. Maybe the chip operates the "negative" terminal at 1/2 rail voltage, flicking though the datasheet (https://cdn-shop.adafruit.com/product-files/3006/MAX98357A-MAX98357B.pdf) I was unable to find info on how the output works exactly.
If this is the case, it would make common ground to the headphones impossible. However it would be possible for a mono configuration as long as the "negative" from the chip is left floating and you could use the jack to do the switching. It would be simple, tiny, low power usage and low cost but a trade off losing stereo output.

As for the MAX98357 chip it's in stock at Digikey for £2.30ish and looks easy to implement:
from the datasheet:
max_audio1.png


For the adafruit hat:
raspberry_pi_schem.png

I would definitely use a 0.1uF and say 47uF on the power rail close to the chip.
 
Sorry for the delay, I think the solution finally just made sense to me whilst I was thinking it over. I think the right thing to do is switch the audio signal on the unamplified side. If you imagine the outputs to the headphone port on the Pi is what is switched between the amp for speakers and headphones rather than trying to switch it on the amplified side. Makes much more sense and then can be done simply using the mechanical switches within the jack itself.
It might mean less power to the headphones but this is how the Pi would normally drive them anyway. Plus no programming required!
Thanks again for your help!