/* Returns the new gain, which may be lower than the old gain.  */
static int ad1843_set_gain(lithium_t *lith, const ad1843_gain_t *gp, int newval)
/* Returns the new gain, which may be lower than the old gain.  */
static int ad1843_set_gain(lithium_t *lith, const ad1843_gain_t *gp, int newval)
{
	unsigned short mask = (1 << gp->lfield->nbits) - 1;
	int lg = newval >> 0 & 0xFF;
	int rg = newval >> 8;
	if (lg < 0 || lg > 100 || rg < 0 || rg > 100)
		return -EINVAL;
	lg = (lg * mask + (mask >> 1)) / 100;
	rg = (rg * mask + (mask >> 1)) / 100;
	if (gp->negative) {
		lg = mask - lg;
		rg = mask - rg;
	}
	ad1843_write_multi(lith, 2, gp->lfield, lg, gp->rfield, rg);
	return ad1843_get_gain(lith, gp);
}