It is well known that the voltage $V$ across, say, a p-n junction can be related to the electro-chemical potentials (usually and improperly called Fermi levels) at the borders of the junction $$ qV = \mu_{n}-\mu_{p}$$
I'm trying to find a way to understand this in thermodynamic terms. This formula means that the work provided by one electron going from one end of the junction to the other is the difference of electro-chemical potentials. But I'm struggling to recover that from a pure thermodynamical perspective.
When removing dN carriers from the n side of the junction, the energy of the system changes by $$dU_n = -p_n dV_n +T_n dS_n -\mu_n dN $$ when adding dN carriers to the p side of the junction, the energy of the system changes by $$dU_p = -p_p dV_p +T_p dS_p +\mu_p dN $$
The total energy change of the system equals the work and heat provided by the system, ie $$ dU_n + dU_p = \delta W + \delta Q = qV dN + \delta Q $$
Now, how do I get from there to $ qV = \mu_{n}-\mu_{p}$ ?