  We develop a first-principles scheme to calculate adiabatic and non-adiabatic
phonon frequencies in the full Brillouin zone. The method relies on the
variational properties of a force-constants functional with respect to the
first-order perturbation of the electronic charge density and on the
localization of the deformation potential in the Wannier function basis. This
allows for calculation of phonon dispersion curves free from convergence issues
related to Brillouin zone sampling. In addition our approach justify the use of
the static screened potential in the calculation of the phonon linewidth due to
decay in electron-hole pairs. We apply the method to the calculation of the
phonon dispersion and electron-phonon coupling in MgB$_2$ and CaC$_6$. In both
compounds we demonstrate the occurrence of several Kohn anomalies, absent in
previous calculations, that are manifest only after careful electron and phonon
momentum integration. In MgB$_2$, the presence of Kohn anomalies on the
E$_{2g}$ branches improves the agreement with measured phonon spectra and
affects the position of the main peak in the Eliashberg function. In CaC$_6$ we
show that the non-adiabatic effects on in-plane carbon vibrations are not
localized at zone center but are sizable throughout the full Brillouin zone.
Our method opens new perspectives in large-scale first-principles calculations
of dynamical properties and electron-phonon interaction.
