By this point, Charlie was fated to die. Throughout Season 3, Desmond had seen that Charlie was going to die and prevented a number of these deaths, including:
In Flashes Before Your Eyes, Hurley and Charlie get Desmond drunk and he reveals Charlie's fate and states that he "can't save Charlie forever."
DESMOND: When I saw the lightning hit the roof you were electrocuted. And when you heard Claire was in the water you -- you
drowned trying to save her. I dove in myself so you never went in.
I've tried, brother. I've tried twice to save you, but the universe
has a way of course correcting and -- and I can't stop it forever.
I'm sorry. I'm sorry because no matter what I try to do you're going
to die, Charlie.
(This leads to Charlie becoming somewhat depressed, until the incident with the camper van in Tricia Tanaka Is Dead. (Season 3, Episode 10) )
Desmond saves Charlie again in Catch-22 (Season 3, Episode 17). Charlie is constantly living with the prospect of immediate death, even more so that the rest of the group.
In Greatest Hits(Season 3, Episode 21), Desmond tells Charlie that if he drowns on the Looking Glass station, then Claire and Aaron will get to leave the island in the helicopter.1
DESMOND: What are you writing?
CHARLIE: [Pauses] Nothing. All right, tell me.
DESMOND: You sure you wanna know? I mean it might be easier if you
jus—
CHARLIE: Yeah. I want to know.
DESMOND: You're inside a hatch. It's, er... a room full of equipment.
There's a blinking yellow light above a switch. You flick the
switch... light goes off... and then you drown.
CHARLIE: [Nods, then pauses] When?
DESMOND: I don't know.
CHARLIE: You sure you saw Claire and Aaron get on that helicopter?
DESMOND: Aye.
CHARLIE: So, before I drown, I just have to flip a switch.
DESMOND: That's right.
CHARLIE: Where is it, then?
Charlie finishes recording his Greatest Hits and makes peace with his friends and dives down.
It is clear that by the time of Through the Looking Glass (Season 3, Episode 22), Charlie has made peace with the world and is ready to die, if the circumstance is right. He believes that if he makes the sacrifice when he pushes that button Claire and Aaron, the people he cares most about, will get to leave the island.
Whilst saving Desmond was a consideration, his main motivation was making Desmond's flash come to pass.
1 It's unclear if Desmond lied about this point or not.