static void _max77888_restore_muic_reg(struct max77888_dev *max77888)
{
	pr_info("%s:Restore muic irq\n", __func__);
	max77888_write_reg(max77888->muic, MAX77888_MUIC_REG_INTMASK1, 0x09);
	max77888_write_reg(max77888->muic, MAX77888_MUIC_REG_INTMASK2, 0x11);
	max77888_update_reg(max77888->muic, MAX77888_MUIC_REG_CDETCTRL1,
				(0x01 << CHGTYPM_SHIFT), CHGTYPM_MASK);
	max77888_write_reg(max77888->muic, MAX77888_MUIC_REG_CTRL4, 0x02);
	max77888_muic_regdump();
}
static int max77888_restore(struct device *dev)
{
	struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
	struct max77888_dev *max77888 = i2c_get_clientdata(i2c);
	int i;

	enable_irq(max77888->irq);

	for (i = 0; i < ARRAY_SIZE(max77888_dumpaddr_pmic); i++)
		max77888_write_reg(i2c, max77888_dumpaddr_pmic[i],
				max77888->reg_pmic_dump[i]);

	for (i = 0; i < ARRAY_SIZE(max77888_dumpaddr_muic); i++)
		max77888_write_reg(i2c, max77888_dumpaddr_muic[i],
				max77888->reg_muic_dump[i]);

	for (i = 0; i < ARRAY_SIZE(max77888_dumpaddr_haptic); i++)
		max77888_write_reg(i2c, max77888_dumpaddr_haptic[i],
				max77888->reg_haptic_dump[i]);

	return 0;
}
static int max77888_set_bits(struct i2c_client *client, const u8 reg,
			     const u8 mask, const u8 inval)
{
	int ret;
	u8 value;

	ret = max77888_read_reg(client, reg, &value);
	if (unlikely(ret < 0))
		return ret;

	value = (value & ~mask) | (inval & mask);

	ret = max77888_write_reg(client, reg, value);

	return ret;
}
static void max77888_irq_sync_unlock(struct irq_data *data)
{
	struct max77888_dev *max77888 = irq_get_chip_data(data->irq);
	int i;

	for (i = 0; i < MAX77888_IRQ_GROUP_NR; i++) {
		u8 mask_reg = max77888_mask_reg[i];
		struct i2c_client *i2c = get_i2c(max77888, i);

		if (mask_reg == MAX77888_REG_INVALID ||
				IS_ERR_OR_NULL(i2c))
			continue;
		max77888->irq_masks_cache[i] = max77888->irq_masks_cur[i];

		max77888_write_reg(i2c, max77888_mask_reg[i],
				max77888->irq_masks_cur[i]);
	}

	mutex_unlock(&max77888->irqlock);
}
static int max77888_led_setup(struct max77888_led_data *led_data)
{
	int ret = 0;
	struct max77888_led *data = led_data->data;
	int id = data->id;
	int value;

	DEBUG_MAX77888("[LED] %s\n", __func__);

	ret |= max77888_write_reg(led_data->i2c, MAX77888_LED_REG_VOUT_CNTL,
				  MAX77888_BOOST_FLASH_MODE_FLED1);
	ret |= max77888_write_reg(led_data->i2c, MAX77888_LED_REG_VOUT_FLASH,
				  MAX77888_BOOST_VOUT_FLASH_FROM_VOLT(3300));
	ret |= max77888_write_reg(led_data->i2c, MAX77888_CHG_REG_CHG_CNFG_11, 0x2B);
	ret |= max77888_write_reg(led_data->i2c,
				MAX77888_LED_REG_MAX_FLASH1, 0xBC);
	ret |= max77888_write_reg(led_data->i2c,
				MAX77888_LED_REG_MAX_FLASH2, 0x00);

	value = max77888_led_get_en_value(led_data, 0);

	ret |= max77888_set_bits(led_data->i2c, MAX77888_LED_REG_FLASH_EN,
				 led_en_mask[id],
				 value << led_en_shift[id]);

	/* Set TORCH_TMR_DUR or FLASH_TMR_DUR */
	if (reg_led_timer[id] == MAX77888_LED_REG_FLASH_TIMER) {
		ret |= max77888_write_reg(led_data->i2c, reg_led_timer[id],
					(data->timer | data->timer_mode << 7));
	} else {
		ret |= max77888_write_reg(led_data->i2c, reg_led_timer[id],
					0xC0);
	}

	/* Set current */
	ret |= max77888_set_bits(led_data->i2c, reg_led_current[id],
				led_current_mask[id],
				led_data->brightness << led_current_shift[id]);

	return ret;
}
static void max77888_haptic_i2c(struct max77888_haptic_data *hap_data, bool en)
{
	int ret;
	u8 value = hap_data->pdata->reg2;
	u8 lscnfg_val = 0x00;

	pr_debug("[VIB] %s %d\n", __func__, en);

	if (en) {
		value |= MOTOR_EN;
		lscnfg_val = 0x80;
	}

	ret = max77888_update_reg(hap_data->pmic_i2c, MAX77888_PMIC_REG_LSCNFG,
				lscnfg_val, 0x80);
	if (ret)
		pr_err("[VIB] i2c update error %d\n", ret);

	ret = max77888_write_reg(hap_data->i2c,
				 MAX77888_HAPTIC_REG_CONFIG2, value);
	if (ret)
		pr_err("[VIB] i2c write error %d\n", ret);
}
int max77888_irq_init(struct max77888_dev *max77888)
{
	int i;
	int cur_irq;
	int ret;
	u8 i2c_data;

	pr_info("func: %s, irq_gpio: %d, irq_base: %d\n", __func__,
		max77888->irq_gpio, max77888->irq_base);

#ifdef CONFIG_MUIC_RESET_PIN_ENABLE
	INIT_DELAYED_WORK(&muic_restore_work, max77888_restore_muic_reg);
#endif

	if (!max77888->irq_gpio) {
		dev_warn(max77888->dev, "No interrupt specified.\n");
		max77888->irq_base = 0;
		return 0;
	}

	if (!max77888->irq_base) {
		dev_err(max77888->dev, "No interrupt base specified.\n");
		return 0;
	}

	mutex_init(&max77888->irqlock);

	max77888->irq = gpio_to_irq(max77888->irq_gpio);
	ret = gpio_request(max77888->irq_gpio, "if_pmic_irq");
	if (ret) {
		dev_err(max77888->dev, "%s: failed requesting gpio %d\n",
			__func__, max77888->irq_gpio);
		return ret;
	}
	gpio_direction_input(max77888->irq_gpio);
	gpio_free(max77888->irq_gpio);

#ifdef CONFIG_MUIC_RESET_PIN_ENABLE
	if (muic_reset_pin)
	{
		max77888->irq_reset = gpio_to_irq(max77888->irq_reset_gpio);
		ret = gpio_request(max77888->irq_reset_gpio, "muic_reset_irq");
		if (ret) {
			dev_err(max77888->dev, "%s: failed requesting gpio %d\n",
				__func__, max77888->irq_reset_gpio);
			return ret;
		}
		gpio_direction_input(max77888->irq_reset_gpio);
		gpio_free(max77888->irq_reset_gpio);
	}
#endif

	/* Mask individual interrupt sources */
	for (i = 0; i < MAX77888_IRQ_GROUP_NR; i++) {
		struct i2c_client *i2c;
		/* MUIC IRQ  0:MASK 1:NOT MASK */
		/* Other IRQ 1:MASK 0:NOT MASK */
		if (i >= MUIC_INT1 && i <= MUIC_INT3) {
			max77888->irq_masks_cur[i] = 0x00;
			max77888->irq_masks_cache[i] = 0x00;
		} else {
			max77888->irq_masks_cur[i] = 0xff;
			max77888->irq_masks_cache[i] = 0xff;
		}
		i2c = get_i2c(max77888, i);

		if (IS_ERR_OR_NULL(i2c))
			continue;
		if (max77888_mask_reg[i] == MAX77888_REG_INVALID)
			continue;
		if (i >= MUIC_INT1 && i <= MUIC_INT3)
			max77888_write_reg(i2c, max77888_mask_reg[i], 0x00);
		else
			max77888_write_reg(i2c, max77888_mask_reg[i], 0xff);
	}

	/* Register with genirq */
	for (i = 0; i < MAX77888_IRQ_NR; i++) {
		cur_irq = i + max77888->irq_base;
		irq_set_chip_data(cur_irq, max77888);
		irq_set_chip_and_handler(cur_irq, &max77888_irq_chip,
					 handle_edge_irq);
		irq_set_nested_thread(cur_irq, 1);
#ifdef CONFIG_ARM
		set_irq_flags(cur_irq, IRQF_VALID);
#else
		irq_set_noprobe(cur_irq);
#endif
	}

	/* Unmask max77888 interrupt */
	ret = max77888_read_reg(max77888->i2c, MAX77888_PMIC_REG_INTSRC_MASK,
			  &i2c_data);
	if (ret) {
		dev_err(max77888->dev, "%s: fail to read muic reg\n", __func__);
		return ret;
	}

	i2c_data &= ~(MAX77888_IRQSRC_CHG);	/* Unmask charger interrupt */
	i2c_data &= ~(MAX77888_IRQSRC_MUIC);	/* Unmask muic interrupt */
	max77888_write_reg(max77888->i2c, MAX77888_PMIC_REG_INTSRC_MASK,
			   i2c_data);

	ret = request_threaded_irq(max77888->irq, NULL, max77888_irq_thread,
			IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
			"max77888-irq", max77888);

	if (ret) {
		dev_err(max77888->dev, "Failed to request IRQ %d: %d\n",
			max77888->irq, ret);
		return ret;
	}

#ifdef CONFIG_MUIC_RESET_PIN_ENABLE
	if (muic_reset_pin)
	{
		ret = request_threaded_irq(max77888->irq_reset, NULL, max77888_reset_irq_thread,
				IRQF_TRIGGER_RISING | IRQF_ONESHOT,
				"max77888-reset_irq", max77888);

		if (ret) {
			dev_err(max77888->dev, "Failed to request IRQ %d: %d\n",
				max77888->irq_reset, ret);
			return ret;
		}
	}
#endif
	return 0;
}
static irqreturn_t max77888_irq_thread(int irq, void *data)
{
	struct max77888_dev *max77888 = data;
	u8 irq_reg[MAX77888_IRQ_GROUP_NR] = {0};
	u8 tmp_irq_reg[MAX77888_IRQ_GROUP_NR] = {};
	u8 irq_src;
	int ret;
	int i;

	/* INTMASK1  3:ADC1K 0:ADC */
	/* INTMASK2  4:VBVolt 0:Chgtype */
	max77888_write_reg(max77888->muic, MAX77888_MUIC_REG_INTMASK1, 0x09);
	max77888_write_reg(max77888->muic, MAX77888_MUIC_REG_INTMASK2, 0x11);
clear_retry:
	ret = max77888_read_reg(max77888->i2c,
			MAX77888_PMIC_REG_INTSRC, &irq_src);
	if (ret < 0) {
		dev_err(max77888->dev,
			"Failed to read interrupt source: %d\n", ret);
		return IRQ_NONE;
	}

	pr_info("%s: interrupt source(0x%02x)\n", __func__, irq_src);

	if (irq_src & MAX77888_IRQSRC_CHG) {
		/* CHG_INT */
		ret = max77888_read_reg(max77888->i2c, MAX77888_CHG_REG_CHG_INT,
				&irq_reg[CHG_INT]);
		pr_info("%s: charger interrupt(0x%02x)\n",
				__func__, irq_reg[CHG_INT]);
		/* mask chgin to prevent chgin infinite interrupt
		 * chgin is unmasked chgin isr
		 */
		if (irq_reg[CHG_INT] &
				max77888_irqs[MAX77888_CHG_IRQ_CHGIN_I].mask) {
			u8 reg_data;
			max77888_read_reg(max77888->i2c,
				MAX77888_CHG_REG_CHG_INT_MASK, &reg_data);
			reg_data |= (1 << 6);
			max77888_write_reg(max77888->i2c,
				MAX77888_CHG_REG_CHG_INT_MASK, reg_data);
		}
	}

	if (irq_src & MAX77888_IRQSRC_TOP) {
		/* TOPSYS_INT */
		ret = max77888_read_reg(max77888->i2c,
				MAX77888_PMIC_REG_TOPSYS_INT,
				&irq_reg[TOPSYS_INT]);
		pr_info("%s: topsys interrupt(0x%02x)\n",
				__func__, irq_reg[TOPSYS_INT]);
	}

	if (irq_src & MAX77888_IRQSRC_FLASH) {
		/* LED_INT */
		ret = max77888_read_reg(max77888->i2c,
				MAX77888_LED_REG_FLASH_INT, &irq_reg[LED_INT]);
		pr_info("%s: led interrupt(0x%02x)\n",
				__func__, irq_reg[LED_INT]);
	}

	if (irq_src & MAX77888_IRQSRC_MUIC) {
		/* MUIC INT1 ~ INT3 */
		max77888_bulk_read(max77888->muic, MAX77888_MUIC_REG_INT1,
				MAX77888_NUM_IRQ_MUIC_REGS,
				&tmp_irq_reg[MUIC_INT1]);

		/* Or temp irq register to irq register for if it retries */
		for (i = MUIC_INT1; i < MAX77888_IRQ_GROUP_NR; i++)
			irq_reg[i] |= tmp_irq_reg[i];

		pr_info("%s: muic interrupt(0x%02x, 0x%02x, 0x%02x)\n",
			__func__, irq_reg[MUIC_INT1],
			irq_reg[MUIC_INT2], irq_reg[MUIC_INT3]);
	}

	pr_debug("%s: irq gpio post-state(0x%02x)\n", __func__,
		gpio_get_value(max77888->irq_gpio));

	if (gpio_get_value(max77888->irq_gpio) == 0) {
		pr_warn("%s: irq_gpio is not High!\n", __func__);
		goto clear_retry;
	}
#if 0
	/* Apply masking */
	for (i = 0; i < MAX77888_IRQ_GROUP_NR; i++) {
		if (i >= MUIC_INT1 && i <= MUIC_INT3)
			irq_reg[i] &= max77888->irq_masks_cur[i];
		else
			irq_reg[i] &= ~max77888->irq_masks_cur[i];
	}
#endif
	/* Report */
	for (i = 0; i < MAX77888_IRQ_NR; i++) {
		if (irq_reg[max77888_irqs[i].group] & max77888_irqs[i].mask)
			handle_nested_irq(max77888->irq_base + i);
	}

	return IRQ_HANDLED;
}