Mg line formation in late-type stellar atmospheres - I. The model atom