All-rounder Matt Coles has agreed to make his loan move to Hampshire permanent, having signed a three-year deal.