Don't worry, he'll probably outgrow this, here I copied this from a site for you HTH lmb
Why it happens
Head banging and body rocking are normal behaviors in children under age 3, who seem to find the rhythmic back-and-forth movements a soothing way to fall asleep. Head banging can start as late as the second year and can last for several months or even years. Some toddlers bang their foreheads or the back of their heads against the crib or bed's headboard, while others are partial to the railings.
Head banging can also soothe a child in pain. Toddlers are more likely to bang their head when they're teething or suffering from an ear infection. Head banging apparently helps them feel better, perhaps by distracting them from the discomfort in their mouth or ear.
What you can do about it
While it may look painful, your child won't get hurt head banging herself to sleep. Head banging in toddlers is generally not a sign of any behavioral or emotional problem, and you don't need to take any precautionary steps for your child's sake. Some parents find that their toddler will stop if they put a ticking metronome in the bedroom; the rhythmic sound seems to soothe and distract children from their head banging behavior. If the sound of your child's head banging bothers you, try moving the crib or bed away from the wall, or putting her to sleep on a mattress on the floor. Also be sure to tighten the screws and bolts on her crib or bed regularly.
If your toddler starts head banging after 18 months, has similar behaviors during the daytime, or if the behavior persists into her third or fourth year, consult her doctor.