I strongly suggest reading the Wikipedia article on Kemalism (the principles of Atatürk for the Turkish Republic): https://en.wikipedia.org/wiki/Kemalism
Erdogan dreams of bringing Turkey back to its Ottoman glory and is willing to exploit religion (and abolish Laicism) to do so. I don't think he staged the failed coup himself but it is clear that he's benefited the most from it:
He told the Turkish people to go out and stop the coup, risking their lives and knowing full well that the soldiers wouldn't massacre the civilians but creating an opportunity to make his supporters seem like a peaceful opposition to the "violent" military.
He spread the narrative that the coup was staged not to defend Atatürk's principles but by a religious sect his ruling party fell out with. He used that narrative and the failed coup to get rid of thousands of educators, judges, officials and military officers (calling it a "purge" even).
He's basically doing what he did before: eliminating all opposition to ensure his rise to permanent and unlimited power. Except because of the clever PR move of risking his citizens' lives he now has public support for his autocratic ambitions.
Atatürk created a nation that should have been an inspiration to the entire Islamic world with a degree of secularism far outmatching that of many Christian nations. Instead Erdogan is heading full speed back into the dark ages of theocratic dictatorships. This is a political tragedy of disturbing proportions and a huge loss for Europe and the Middle East.