Federer, who turns 40 next month, had two knee surgeries in 2020 which resulted in more than a year of rehabilitation.
He withdrew from the French Open after winning his third round match to save himself for the grasscourt season. He was beaten in the quarter-final at Wimbledon.
“I am greatly disappointed, as it has been an honour and highlight of my career each time I have represented Switzerland.
“I have already begun rehabilitation in the hopes of returning to the tour later this summer. I wish the entire Swiss team the best of luck and I will be rooting hard from afar.”
Federer won the gold medal in the doubles at the 2008 Games in Beijing and a silver medal in the singles four years later in London.