static int bcmpmu_i2c_write_device_direct_bulk(struct bcmpmu *bcmpmu, int map, int addr, unsigned int *val, int len)
{
	int err;
	struct bcmpmu_i2c *acc = (struct bcmpmu_i2c *)bcmpmu->accinfo;
	u8 *uval = (u8 *)val;
	int i;

	if (addr + len > acc->pagesize) return -ENODEV;

	for (i = 0; i < len; i++)
		uval[i] = (u8)val[i];


	bcmpmu_i2c_lock(acc);
	err = bcmpmu_i2c_retry(bcmpmu);
	if (err < 0)
		goto err;

	if (map == 0)
		err = i2c_smbus_write_i2c_block_data(acc->i2c_client, addr, len, uval);
	else if (map == 1)
		err = i2c_smbus_write_i2c_block_data(acc->i2c_client1, addr, len, uval);
	else err = -EIO;
	last_i2c_trans = I2C_TRANS_WRITE;
	bcmpmu_i2c_unlock(acc);
err:
	if (err < 0) return err;
	return 0;
}
static int bcmpmu_i2c_pwrmgr_read_direct(struct bcmpmu *bcmpmu,
					 int map, int addr,
					 unsigned int *val,
					 unsigned int msk)
{
	int err = 0;
	u8 temp;
	struct bcmpmu_i2c *acc = (struct bcmpmu_i2c *)bcmpmu->accinfo;
	if ((addr == 0) && (msk == 0))
		return -ENODEV;

	bcmpmu_i2c_lock(acc);

	pr_debug("%s\n", __func__);

	if (map == 0)
		err = i2c_try_read_write(bcmpmu, I2C_TRANS_READ,
				addr, acc->i2c_client->addr, &temp);
	else if (map == 1)
		err = i2c_try_read_write(bcmpmu, I2C_TRANS_READ,
				addr, acc->i2c_client1->addr, &temp);
	else
		err = -EIO;

	if (err < 0)
		goto out_unlock;

	temp &= msk;
	*val = temp;
out_unlock:
	bcmpmu_i2c_unlock(acc);
	return err;

}
static int bcmpmu_i2c_read_device_direct_bulk(struct bcmpmu *bcmpmu, int map,
					      int addr, unsigned int *val,
					      int len)
{
	int err;
	struct bcmpmu_i2c *acc = (struct bcmpmu_i2c *)bcmpmu->accinfo;
	u8 *uval = (u8 *) val;
	int i;

	if (addr + len > acc->pagesize)
		return -ENODEV;

	bcmpmu_i2c_lock(acc);
	if (map == 0)
		err =
		    i2c_smbus_read_i2c_block_data(acc->i2c_client, addr, len,
						  uval);
	else if (map == 1)
		err =
		    i2c_smbus_read_i2c_block_data(acc->i2c_client1, addr, len,
						  uval);
	else
		err = -EIO;
	bcmpmu_i2c_unlock(acc);

	for (i = len; i > 0; i--)
		val[i - 1] = (unsigned int)uval[i - 1];

	if (err < 0)
		return err;
	return 0;
}
static int bcmpmu_i2c_write_device_direct(struct bcmpmu *bcmpmu, int map, int addr, unsigned int val, unsigned int msk)
{
	int err;
	u8 value = (u8)val;
	struct bcmpmu_i2c *acc = (struct bcmpmu_i2c *)bcmpmu->accinfo;
	if ((addr == 0) && (msk == 0))  return -ENODEV;

	bcmpmu_i2c_lock(acc);
	err = bcmpmu_i2c_retry(bcmpmu);
	if (err < 0)
		goto err;

	if (map == 0)
		err = i2c_smbus_read_byte_data(acc->i2c_client, addr);
	else if (map == 1)
		err = i2c_smbus_read_byte_data(acc->i2c_client1, addr);
	else err = -EIO;
	if (err < 0) goto err;

	err = err & ~msk;
	value = value | err;

	if (map == 0)
		err = i2c_smbus_write_byte_data(acc->i2c_client, addr, value);
	else if (map == 1)
		err = i2c_smbus_write_byte_data(acc->i2c_client1, addr, value);
	else err = -EIO;
	last_i2c_trans = I2C_TRANS_WRITE;
err:
	bcmpmu_i2c_unlock(acc);
	return err;
}
static int bcmpmu_i2c_read_device(struct bcmpmu *bcmpmu, int reg, unsigned int *val, unsigned int msk)
{
	struct bcmpmu_reg_map map;
	int err;
	struct bcmpmu_i2c *acc = (struct bcmpmu_i2c *)bcmpmu->accinfo;

	if (reg >= PMU_REG_MAX) return -ENODEV;
	map = bcmpmu->regmap[reg];
	if ((map.addr == 0) && (map.mask == 0))  return -ENXIO;

	bcmpmu_i2c_lock(acc);
	err = bcmpmu_i2c_retry(bcmpmu);
	if (err < 0)
		goto err;
	if (map.map == 0)
		err = i2c_smbus_read_byte_data(acc->i2c_client, map.addr);
	else if (map.map == 1)
		err = i2c_smbus_read_byte_data(acc->i2c_client1, map.addr);
	else err = -EIO;
	last_i2c_trans = I2C_TRANS_READ;
	bcmpmu_i2c_unlock(acc);
err:
	if (err < 0) return err;
	err = err & msk;
	err = err & map.mask;
	*val = err;

	return 0;
}
static int bcmpmu_i2c_pwrmgr_write(struct bcmpmu59xxx *bcmpmu, u32 reg, u8 val)
{
	int ret = 0;
	bcmpmu_i2c_lock(bcmpmu);
	ret = bcmpmu_i2c_try_write(bcmpmu, reg, val);
	bcmpmu_i2c_unlock(bcmpmu);
	pr_pmui2c(DATA, "WR done reg %x val %x\n", reg, val);
	return ret;
}
static int bcmpmu_i2c_pwrmgr_read(struct bcmpmu59xxx *bcmpmu, u32 reg, u8 * val)
{
	int ret;
	if (!bcmpmu || !val)
		return -EINVAL;

	bcmpmu_i2c_lock(bcmpmu);
	ret = bcmpmu_i2c_try_read(bcmpmu, reg, val);
	bcmpmu_i2c_unlock(bcmpmu);
	pr_pmui2c(DATA, "RD done reg %x val %x\n", reg, *val);
	return ret;
}
static int bcmpmu_i2c_pwrmgr_write(struct bcmpmu *bcmpmu, int reg,
				   unsigned int value, unsigned int msk)
{
	struct bcmpmu_reg_map map;
	int err = 0;
	u8 temp;
	struct bcmpmu_i2c *acc = (struct bcmpmu_i2c *)bcmpmu->accinfo;

	if (reg >= PMU_REG_MAX)
		return -ENODEV;

	map = bcmpmu->regmap[reg];
	if ((map.addr == 0) && (map.mask == 0))
		return -ENXIO;

	bcmpmu_i2c_lock(acc);

	pr_debug("%s\n", __func__);

	if (map.map == 0)
		err = i2c_try_read_write(bcmpmu, true, I2C_TRANS_READ, map.addr,
					 acc->i2c_client->addr, &temp);
	else if (map.map == 1)
		err = i2c_try_read_write(bcmpmu, true, I2C_TRANS_READ, map.addr,
					 acc->i2c_client1->addr, &temp);
	else
		err = -EIO;

	if (err < 0)
		goto out_unlock;

	temp &= ~msk;
	temp &= ~map.mask;
	temp |= (u8) value;

	if (map.map == 0)
		err = i2c_try_read_write(bcmpmu, true, I2C_TRANS_WRITE,
								map.addr,
					 acc->i2c_client->addr, &temp);
	else if (map.map == 1)
		err = i2c_try_read_write(bcmpmu, true, I2C_TRANS_WRITE,
								map.addr,
					 acc->i2c_client1->addr, &temp);
	else
		err = -ENODEV;
      out_unlock:
	bcmpmu_i2c_unlock(acc);
	return err;
}
static int bcmpmu_i2c_read_device_direct_bulk(struct bcmpmu59xxx *bcmpmu,
					      u32 reg, u8 *val, int len)
{
	int err = 0;
	struct i2c_client *clt;

	if ((DEC_REG_ADD(reg) + len) >= PMU_REG_MAX)
		return -ENODEV;
	clt = bcmpmu_get_client(bcmpmu, reg);

	bcmpmu_i2c_lock(bcmpmu);
	err =
	    i2c_smbus_read_i2c_block_data(clt, (u8) DEC_REG_ADD(reg), len, val);
	bcmpmu_i2c_unlock(bcmpmu);
	return (err < 0 ? err : 0);
}
static int bcmpmu_i2c_write_device(struct bcmpmu59xxx *bcmpmu, u32 reg,
				   u8 value)
{
	int err;
	struct i2c_client *clt;

	if (DEC_REG_ADD(reg) >= PMU_REG_MAX)
		return -ENODEV;

	clt = bcmpmu_get_client(bcmpmu, reg);

	bcmpmu_i2c_lock(bcmpmu);
	err = i2c_smbus_write_byte_data(clt, (u8) DEC_REG_ADD(reg), value);
	bcmpmu_i2c_unlock(bcmpmu);
	return (err < 0 ? err : 0);
}
/* Return Error codes in i2c read/write interfaces
 *-ENODEV = if wrong register has been passed
 *-ENXIO = if register exists but no map found
 *-EIO = i2c read/write error
*/
static int bcmpmu_i2c_read_device(struct bcmpmu59xxx *bcmpmu, u32 reg, u8 *val)
{
	int err;
	struct i2c_client *clt;

	if (DEC_REG_ADD(reg) >= PMU_REG_MAX)
		return -ENODEV;
	clt = bcmpmu_get_client(bcmpmu, reg);

	bcmpmu_i2c_lock(bcmpmu);
	err = i2c_smbus_read_byte_data(clt, (u8) DEC_REG_ADD(reg));
	bcmpmu_i2c_unlock(bcmpmu);
	if (err < 0)
		return err;
	*val = err;
	return 0;
}
static int bcmpmu_i2c_pwrmgr_write_bulk(struct bcmpmu59xxx *bcmpmu,
					u32 reg, u8 *val, int len)
{
	int err = 0;
	int i;
	u8 temp;

	bcmpmu_i2c_lock(bcmpmu);
	for (i = 0; i < len; i++) {
		temp = val[i];
		err = bcmpmu_i2c_try_write(bcmpmu, reg + i, temp);
		if (err < 0)
			break;
	}
	bcmpmu_i2c_unlock(bcmpmu);
	return err;
}
static int bcmpmu_i2c_pwrmgr_read_bulk(struct bcmpmu59xxx *bcmpmu,
				       u32 reg, u8 *val, int len)
{
	int err = 0;
	int i;
	u8 temp = 0;

	bcmpmu_i2c_lock(bcmpmu);
	for (i = 0; i < len; i++) {
		err = bcmpmu_i2c_try_read(bcmpmu, reg + i, &temp);
		if (err < 0)
			break;
		val[i] = temp;
	}
	bcmpmu_i2c_unlock(bcmpmu);
	return err;
}
static int bcmpmu_i2c_write_device(struct bcmpmu *bcmpmu, int reg,
				   unsigned int value, unsigned int msk)
{
	struct bcmpmu_reg_map map;
	int err;
	struct bcmpmu_i2c *acc = (struct bcmpmu_i2c *)bcmpmu->accinfo;

	if (reg >= PMU_REG_MAX)
		return -ENODEV;
	map = bcmpmu->regmap[reg];
	if ((map.addr == 0) && (map.mask == 0))
		return -ENXIO;

	bcmpmu_i2c_lock(acc);
	if (map.map == 0)
		err = i2c_smbus_read_byte_data(acc->i2c_client, map.addr);
	else if (map.map == 1)
		err = i2c_smbus_read_byte_data(acc->i2c_client1, map.addr);
	else
		err = -EIO;
	if (err < 0)
		goto err;

	err = err & ~msk;
	err = err & ~map.mask;
	value = value | err;

	if (map.map == 0)
		err =
		    i2c_smbus_write_byte_data(acc->i2c_client, map.addr, value);
	else if (map.map == 1)
		err =
		    i2c_smbus_write_byte_data(acc->i2c_client1, map.addr,
					      value);
	else
		err = -EIO;
      err:
	bcmpmu_i2c_unlock(acc);
	return err;
}
static int bcmpmu_i2c_pwrmgr_write_direct_bulk(struct bcmpmu *bcmpmu,
					       int map, int addr,
					       unsigned int *val,
					       int len)
{
	struct bcmpmu_i2c *acc = (struct bcmpmu_i2c *)bcmpmu->accinfo;
	int err = 0;
	int i;
	u8 temp;

	if (addr + len > acc->pagesize)
		return -ENODEV;

	if ((map != 0) && (map != 1))
		return -EIO;

	bcmpmu_i2c_lock(acc);

	pr_debug("%s\n", __func__);

	for (i = 0; i < len; i++) {
		temp = val[i];

		if (map == 0)
			err = i2c_try_read_write(bcmpmu, I2C_TRANS_WRITE,
					addr + i, acc->i2c_client->addr,
					&temp);
		else
			err = i2c_try_read_write(bcmpmu, I2C_TRANS_WRITE,
					addr + i, acc->i2c_client1->addr,
					&temp);
		if (err < 0)
			break;
	}

	bcmpmu_i2c_unlock(acc);
	return err;
}
static int bcmpmu_i2c_read_device_direct(struct bcmpmu *bcmpmu, int map, int addr, unsigned int *val, unsigned int msk)
{
	int err;
	struct bcmpmu_i2c *acc = (struct bcmpmu_i2c *)bcmpmu->accinfo;
	if ((addr == 0) && (msk == 0))  return -ENODEV;

	bcmpmu_i2c_lock(acc);
	err = bcmpmu_i2c_retry(bcmpmu);
	if (err < 0)
		goto err;

	if (map == 0)
		err = i2c_smbus_read_byte_data(acc->i2c_client, addr);
	else if (map == 1)
		err = i2c_smbus_read_byte_data(acc->i2c_client1, addr);
	else err = -EIO;
	last_i2c_trans = I2C_TRANS_READ;
	bcmpmu_i2c_unlock(acc);
err:
	if (err < 0) return err;
	err = err & msk;
	*val = err;
	return 0;
}