static int __devinit max77686_rtc_init_reg(struct max77686_rtc_info *info,
					struct max77686_platform_data *pdata)
{
	u8 data[2], update0, cntl;
	int ret;

	max77686_rtc_update(info, MAX77686_RTC_READ);

	ret = max77686_read_reg(info->rtc, MAX77686_RTC_CONTROL, &cntl);
	if (ret < 0) {
		dev_err(info->dev, "%s: fail to read control reg(%d)\n",
			__func__, ret);
		return ret;
	}

	ret = max77686_read_reg(info->rtc, MAX77686_RTC_UPDATE0, &update0);
	if (ret < 0) {
		dev_err(info->dev, "%s: fail to read update0 reg(%d)\n",
			__func__, ret);
		return ret;
	}
	info->update0_reg = update0 & ~(RTC_UDR_MASK | RTC_RBUDR_MASK);

	/* If the value of CONTROL register is 0, RTC registers were reset */
	if (cntl == MODEL24_MASK)
		return 0;

	/* Set RTC control register : Binary mode, 24hour mode */
	data[0] = BCD_EN_MASK | MODEL24_MASK;
	data[1] = MODEL24_MASK;

	ret = max77686_bulk_write(info->rtc, MAX77686_RTC_CONTROLM, 2, data);
	if (ret < 0) {
		dev_err(info->dev, "%s: fail to write controlm reg(%d)\n",
			__func__, ret);
		return ret;
	}

	ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
	if (ret < 0)
		return ret;

	if (pdata->init_time) {
		dev_info(info->dev, "%s: initialize RTC time\n", __func__);
		ret = max77686_rtc_set_time(info->dev, pdata->init_time);
	}
	return ret;
}
static int max77686_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
{
	struct max77686_rtc_info *info = dev_get_drvdata(dev);
	u8 data[NR_RTC_CNT_REGS], val;
	int ret;

	mutex_lock(&info->lock);
	ret = max77686_rtc_update(info, MAX77686_RTC_READ);
	if (ret < 0)
		goto out;

	ret = max77686_bulk_read(info->rtc, MAX77686_ALARM1_SEC,
			NR_RTC_CNT_REGS, data);
	if (ret < 0) {
		dev_err(info->dev, "%s:%d fail to read alarm reg(%d)\n",
			__func__, __LINE__, ret);
		goto out;
	}

	max77686_rtc_data_to_tm(data, &alrm->time);

	dev_dbg(info->dev, "%s: %d-%02d-%02d %02d:%02d:%02d(%d)\n", __func__,
			alrm->time.tm_year + 1900, alrm->time.tm_mon + 1,
			alrm->time.tm_mday, alrm->time.tm_hour,
			alrm->time.tm_min, alrm->time.tm_sec,
			alrm->time.tm_wday);

	alrm->enabled = info->alarm_enabled;
	alrm->pending = 0;
	ret = max77686_read_reg(info->max77686->i2c, MAX77686_REG_STATUS2,
			&val);
	if (ret < 0) {
		dev_err(info->dev, "%s:%d fail to read status1 reg(%d)\n",
			__func__, __LINE__, ret);
		goto out;
	}
	if (val & STATUS2_RTCA1_MASK)
		alrm->pending = 1;
out:
	mutex_unlock(&info->lock);
	return ret;
}
static int max77686_i2c_probe(struct i2c_client *i2c,
			      const struct i2c_device_id *id)
{
	struct max77686_dev *max77686;
	struct max77686_platform_data *pdata = i2c->dev.platform_data;
	u8 data;
	int ret = 0;

	max77686 = kzalloc(sizeof(struct max77686_dev), GFP_KERNEL);
	if (max77686 == NULL)
		return -ENOMEM;

	i2c_set_clientdata(i2c, max77686);
	max77686->dev = &i2c->dev;
	max77686->i2c = i2c;
	max77686->type = id->driver_data;

	if (!pdata) {
		ret = -EIO;
		goto err;
	}

	max77686->wakeup = pdata->wakeup;
	max77686->irq_gpio = pdata->irq_gpio;
	max77686->irq_base = pdata->irq_base;

	mutex_init(&max77686->iolock);

	if (max77686_read_reg(i2c, MAX77686_REG_DEVICE_ID, &data) < 0) {
		dev_err(max77686->dev,
			"device not found on this channel (this is not an error)\n");
		ret = -ENODEV;
		goto err;
	} else
		dev_info(max77686->dev, "device found\n");

#ifdef CONFIG_RTC_DRV_MAX77686
	max77686->rtc = i2c_new_dummy(i2c->adapter, I2C_ADDR_RTC);
	i2c_set_clientdata(max77686->rtc, max77686);
#endif

	max77686_irq_init(max77686);

	ret = mfd_add_devices(max77686->dev, -1, max77686_devs,
			      ARRAY_SIZE(max77686_devs), NULL, 0);

	if (ret < 0)
		goto err_mfd;

	device_init_wakeup(max77686->dev, pdata->wakeup);

	return ret;

err_mfd:
	mfd_remove_devices(max77686->dev);
#ifdef CONFIG_RTC_DRV_MAX77686
	i2c_unregister_device(max77686->rtc);
#endif
err:
	kfree(max77686);
	return ret;
}