Hi Jimmy,
I must admit, that in my case, I do not allow the ISP to set the gain because I am not using AEC functionality.
Nevertheless I will tell you what I know.
The *_set_gain() function you are showing is controlled by either the AEC (auto exposure control) or the manual exposure control of the ISP. This is done via ISP's VVSENSORIOC_S_GAIN IOCTL. According to the "ISP tuning Tool User Guide" (ISPTTUG.pdf) which I highly recommend to read the max value is 32.0:

This means the gain range is between 0 and 32. This gain is to be mapped to your sensor gain. Your sensor might have analog and digital gain or both depending on the sensor. Each sensor is different.
I would recommend to e.g only use analog gain. You are not obliged to set both analog and digital gain.
Also your sensor gain range is sensor dependant. It may be from 0 to 1000 for example.
This means the formula to map ISP gain will map (0 to 32) range to (0 to 1000).
This is why the formulas are different from sensor to sensor.
If the gain is too high, the AEC algorithm will go ahead and set lower gain, etc.
Also the sensors register are mostly 16bit and spread over two registers. Therefore you see code like this:
ret |= os08a20_write_reg(sensor, 0x3508, (again >> & 0xff);
ret |= os08a20_write_reg(sensor, 0x3509, again & 0xff);
I hope this is useful.