From the slang use of the word 'funny' to mean weird, unusual and the description of mad people as 'funny in the head'. An early citation of 'funny farm' is in John Knowles' novel, set in Hew Hampshire, USA - A Separate Peace, 1959:
"You might start to believe it, then I'd have to make a reservation for you at the Funny Farm."
Other variants are 'funny bin', 'funny place', 'funny house', 'funny factory' etc.
See other phrases that were coined in the USA.