See
Intel 4-wire PWM fan specification. The PWM and tach signals are open collector at less than 30kHz frequencies; no need for an analog comparator.
I use
NX138AK N-channel MOSFETs in SOT23-3 (AKA TO-236AB) for this; see e.g. my
dual fan ATtiny85 controller, and my
5V to 12V PWM fan adapter.
The gate to the MOSFET is the input from either the fan (tach) or motherboard (PWM), and can handle ±20V. Even if shorted to ground or 12V, it doesn't mind. The source is always to ground, and the drain is the signal, pulled up to whatever VCC is used (down to 2V works fine) using a suitable resistor. 10kΩ is just a value I decided on looking at the current ranges one can expect based on the spec, that also seemed to behave well in simulations with acceptable amounts of waste (heat).
For PWM output (to the fan) or tach output (to the motherboard), the output from my circuit is always an open collector one using NX138AKs. The drain can be pulled up to 60V without damage; my circuits don't do above 12V or 24V. For safe level translation, I use two NX138AKs in sequence, simply because a single one will invert the signal.
My own controller circuits has the microcontroller handle inverted signals, so only one translator per signal (in either direction) is needed for safe inputs and outputs.
It would be trivial to modify the dual ATtiny85 controller to act like a PWM modifier stage with additional potentiometer input, so that the PWM duty cycle from the motherboard is mapped arbitrarily to the actual PWM duty cycle sent to the microcontroller, and the tachometer rate similarly arbitrarily mapped from the fan to the motherboard, using just four NX138AKs,
making this absolutely safe with any motherboard and fan following the Intel 4-wire fan spec at least roughly.
While there are many other N-channel enhancement mode MOSFETs like BSS138 that can do the job just as well, I picked NX138AKRs because they are cheap (5.5€ for a lot of 100 at Mouser; $0.0327 each at JLCPCB assembly, 2.66€ for a lot of 100 at LCSC), easily modeled using LTspice or KiCAD/ngspice (
NX138AK.lib spice model), they tolerate up to ±20V between gate and source (enough for my MOSFET needs!), and behave quite well in this configuration up to pretty high frequencies (faster than BSS138 and and smaller gate charge than BSS138, but not relevant here, since the PWM is just 25 kHz, and the tach signal is just two pulses per revolution, under 1 kHz for up to 30k RPM). It's basically my current 3.3V logic level N-channel enhancement mode MOSFET for signal uses.