2 Answers

Sounds like you need to replace the proximity sensor flex cable and the earspeaker. The flex cables has the top noise cancelling mic attached to it. It is what you use on speakerphone, voice memos, apps etc. The bottom speaker is for regular calls. Definitely is hardware not software. Make sure the cable is plugged in the logic board.

Resources

Repairability

Stay in the loop

It's time to speak out for your right to repair

We have a chance to guarantee our right to repair electronic
equipment—like smartphones, computers, and even farm equipment. This is a once-in-a-generation
chance to protect local repair jobs—the corner mom-and-pop repair shops that
keep getting squeezed out by manufacturers.

Join the cause and tell your state representative to support Right to Repair. Tell them you believe repair should be fair, affordable, and accessible. Stand up for your right to repair!