What should I do when I set the function to PWM and change it to I/O whenever necessary and want to use it as a PWM function again?
Can you tell which part number devices and SDK you're using?
If you using S32K1xx series MCUs and RTM, you can take a look at this function:
PINS_DRV_SetMuxModeSel();
when you want to change the PWM -> GPIO or GPIO->PWM, you can use this function to change the PIN mux.