Funny farm was a slang term for an insane asylum.
In 2285, Leonard McCoy was to be sent to the Federation funny farm due to his mental issues, which had been induced by Spock's katra. (Star Trek III: The Search for Spock)
In 2364, Madeline told "Dixon Hill" that if Detective Dan Bell had seen him in those threads, he'd send him to the funny farm. (TNG: "The Big Goodbye")