Retirement can be done at any age you like so long as you have enough income saved in a retirement fund or have some other form of income such as passive income from online websites or any other forms of income to pay your bills each month.
The only reason you need to wait until age 62 years old to retire from working is to be able to collect your social security benefits and then at 67 years of age you can collect your full social security benefits.
But if you plan to retire at age 40 you need to be putting a good chunk of your income into a retirement savings account or have other income sources.