This paper introduces a novel iterative algorithm for optimizing pilot and data power control (PC) in cell-free massive multiple-input multiple-output (CF-mMIMO) systems, aiming to enhance system performance under real-time channel conditions. The approach begins by deriving the signal-to-interference-plus-noise ratio (SINR) using a matched filtering receiver and formulating a min-max optimization problem to minimize the normalized mean square error (NMSE). Utilizing McCormick relaxation, the algorithm adjusts pilot power dynamically, ensuring efficient channel estimation. A subsequent max-min optimization problem allocates data power, balancing fairness and efficiency. The iterative process refines pilot and data power allocations based on updated channel state information (CSI) and NMSE results, optimizing spectral efficiency. By leveraging geometric programming (GP) for data power allocation, the proposed method achieves a robust trade-off between simplicity and performance, significantly improving system capacity and fairness. The simulation results demonstrate that dynamic adjustment of both pilot and data PC substantially enhances overall spectral efficiency and fairness, outperforming the existing schemes in the literature.