Пример #1
0
int menelaus_set_regulator_sleep(int enable, u32 val)
{
	int t, ret;
	struct i2c_client *c = the_menelaus->client;

	mutex_lock(&the_menelaus->lock);
	ret = menelaus_write_reg(MENELAUS_SLEEP_CTRL2, val);
	if (ret < 0)
		goto out;

	dev_dbg(&c->dev, "regulator sleep configuration: %02x\n", val);

	ret = menelaus_read_reg(MENELAUS_GPIO_CTRL);
	if (ret < 0)
		goto out;
	t = (GPIO_CTRL_SLPCTLEN | GPIO3_DIR_INPUT);
	if (enable)
		ret |= t;
	else
		ret &= ~t;
	ret = menelaus_write_reg(MENELAUS_GPIO_CTRL, ret);
out:
	mutex_unlock(&the_menelaus->lock);
	return ret;
}
Пример #2
0
static int menelaus_set_alarm(struct device *dev, struct rtc_wkalrm *w)
{
	int		status;

	if (the_menelaus->client->irq <= 0 && w->enabled)
		return -ENODEV;

	/* clear previous alarm enable */
	if (the_menelaus->rtc_control & RTC_CTRL_AL_EN) {
		the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
		status = menelaus_write_reg(MENELAUS_RTC_CTRL,
				the_menelaus->rtc_control);
		if (status < 0)
			return status;
	}

	/* write alarm registers */
	status = time_to_menelaus(&w->time, MENELAUS_RTC_AL_SEC);
	if (status < 0)
		return status;

	/* enable alarm if requested */
	if (w->enabled) {
		the_menelaus->rtc_control |= RTC_CTRL_AL_EN;
		status = menelaus_write_reg(MENELAUS_RTC_CTRL,
				the_menelaus->rtc_control);
	}

	return status;
}
Пример #3
0
int menelaus_set_mmc_slot(int slot, int enable, int power, int cd_en)
{
	int ret, val;

	if (slot != 1 && slot != 2)
		return -EINVAL;
	if (power >= 3)
		return -EINVAL;

	mutex_lock(&the_menelaus->lock);

	ret = menelaus_read_reg(MENELAUS_MCT_CTRL2);
	if (ret < 0)
		goto out;
	val = ret;
	if (slot == 1) {
		if (cd_en)
			val |= (1 << 4) | (1 << 6);
		else
			val &= ~((1 << 4) | (1 << 6));
	} else {
		if (cd_en)
			val |= (1 << 5) | (1 << 7);
		else
			val &= ~((1 << 5) | (1 << 7));
	}
	ret = menelaus_write_reg(MENELAUS_MCT_CTRL2, val);
	if (ret < 0)
		goto out;

	ret = menelaus_read_reg(MENELAUS_MCT_CTRL3);
	if (ret < 0)
		goto out;
	val = ret;
	if (slot == 1) {
		if (enable)
			val |= 1 << 0;
		else
			val &= ~(1 << 0);
	} else {
		int b;

		if (enable)
			ret |= 1 << 1;
		else
			ret &= ~(1 << 1);
		b = menelaus_read_reg(MENELAUS_MCT_CTRL2);
		b &= ~0x03;
		b |= power;
		ret = menelaus_write_reg(MENELAUS_MCT_CTRL2, b);
		if (ret < 0)
			goto out;
	}
	/* Disable autonomous shutdown */
	val &= ~(0x03 << 2);
	ret = menelaus_write_reg(MENELAUS_MCT_CTRL3, val);
out:
	mutex_unlock(&the_menelaus->lock);
	return ret;
}
Пример #4
0
static int menelaus_set_voltage(const struct menelaus_vtg *vtg, int mV,
				int vtg_val, int mode)
{
	int val, ret;
	struct i2c_client *c = the_menelaus->client;

	mutex_lock(&the_menelaus->lock);
	if (vtg == 0)
		goto set_voltage;

	ret = menelaus_read_reg(vtg->vtg_reg);
	if (ret < 0)
		goto out;
	val = ret & ~(((1 << vtg->vtg_bits) - 1) << vtg->vtg_shift);
	val |= vtg_val << vtg->vtg_shift;

	dev_dbg(&c->dev, "Setting voltage '%s'"
			 "to %d mV (reg 0x%02x, val 0x%02x)\n",
			vtg->name, mV, vtg->vtg_reg, val);

	ret = menelaus_write_reg(vtg->vtg_reg, val);
	if (ret < 0)
		goto out;
set_voltage:
	ret = menelaus_write_reg(vtg->mode_reg, mode);
out:
	mutex_unlock(&the_menelaus->lock);
	if (ret == 0) {
		/* Wait for voltage to stabilize */
		msleep(1);
	}
	return ret;
}
Пример #5
0
static int menelaus_ack_irq(int irq)
{
	if (irq > 7)
		return menelaus_write_reg(MENELAUS_INT_ACK2, 1 << (irq - 8));
	else
		return menelaus_write_reg(MENELAUS_INT_ACK1, 1 << irq);
}
Пример #6
0
int menelaus_set_mmc_slot(int slot, int enable, int power, int cd_en)
{
	int ret, val;

	if (slot != 1 && slot != 2)
		return -EINVAL;
	if (power >= 3)
		return -EINVAL;

	mutex_lock(&the_menelaus->lock);

	ret = menelaus_read_reg(MENELAUS_MCT_CTRL2);
	if (ret < 0)
		goto out;
	val = ret;
	if (slot == 1) {
		if (cd_en)
			val |= MCT_CTRL2_S1CD_BUFEN | MCT_CTRL2_S1CD_DBEN;
		else
			val &= ~(MCT_CTRL2_S1CD_BUFEN | MCT_CTRL2_S1CD_DBEN);
	} else {
		if (cd_en)
			val |= MCT_CTRL2_S2CD_BUFEN | MCT_CTRL2_S2CD_BEN;
		else
			val &= ~(MCT_CTRL2_S2CD_BUFEN | MCT_CTRL2_S2CD_BEN);
	}
	ret = menelaus_write_reg(MENELAUS_MCT_CTRL2, val);
	if (ret < 0)
		goto out;

	ret = menelaus_read_reg(MENELAUS_MCT_CTRL3);
	if (ret < 0)
		goto out;
	val = ret;
	if (slot == 1) {
		if (enable)
			val |= MCT_CTRL3_SLOT1_EN;
		else
			val &= ~MCT_CTRL3_SLOT1_EN;
	} else {
		int b;

		if (enable)
			val |= MCT_CTRL3_SLOT2_EN;
		else
			val &= ~MCT_CTRL3_SLOT2_EN;
		b = menelaus_read_reg(MENELAUS_MCT_CTRL2);
		b &= ~(MCT_CTRL2_VS2_SEL_D0 | MCT_CTRL2_VS2_SEL_D1);
		b |= power;
		ret = menelaus_write_reg(MENELAUS_MCT_CTRL2, b);
		if (ret < 0)
			goto out;
	}
	/* Disable autonomous shutdown */
	val &= ~(MCT_CTRL3_S1_AUTO_EN | MCT_CTRL3_S2_AUTO_EN);
	ret = menelaus_write_reg(MENELAUS_MCT_CTRL3, val);
out:
	mutex_unlock(&the_menelaus->lock);
	return ret;
}
Пример #7
0
static int menelaus_enable_irq(int irq)
{
	if (irq > 7) {
		irq -= 8;
		the_menelaus->mask2 &= ~(1 << irq);
		return menelaus_write_reg(MENELAUS_INT_MASK2,
				the_menelaus->mask2);
	} else {
		the_menelaus->mask1 &= ~(1 << irq);
		return menelaus_write_reg(MENELAUS_INT_MASK1,
				the_menelaus->mask1);
	}
}
Пример #8
0
int menelaus_enable_slot(int slot, int enable)
{
	int ret, val;

	mutex_lock(&the_menelaus->lock);
	ret = menelaus_read_reg(MENELAUS_MCT_CTRL3);
	if (ret < 0)
		goto out;
	val = ret;
	if (slot == 1) {
		if (enable)
			val |= MCT_CTRL3_SLOT1_EN;
		else
			val &= ~MCT_CTRL3_SLOT1_EN;
	} else {
		if (enable)
			val |= MCT_CTRL3_SLOT2_EN;
		else
			val &= MCT_CTRL3_SLOT2_EN;
	}
	ret = menelaus_write_reg(MENELAUS_MCT_CTRL3, val);

out:
	mutex_unlock(&the_menelaus->lock);
	return ret;
}
Пример #9
0
/*
 * Toggles the MMC slots between open-drain and push-pull mode.
 */
int menelaus_set_mmc_opendrain(int slot, int enable)
{
	int ret, val;

	if (slot != 1 && slot != 2)
		return -EINVAL;
	mutex_lock(&the_menelaus->lock);
	ret = menelaus_read_reg(MENELAUS_MCT_CTRL1);
	if (ret < 0) {
		mutex_unlock(&the_menelaus->lock);
		return ret;
	}
	val = ret;
	if (slot == 1) {
		if (enable)
			val |= MCT_CTRL1_S1_CMD_OD;
		else
			val &= ~MCT_CTRL1_S1_CMD_OD;
	} else {
		if (enable)
			val |= MCT_CTRL1_S2_CMD_OD;
		else
			val &= ~MCT_CTRL1_S2_CMD_OD;
	}
	ret = menelaus_write_reg(MENELAUS_MCT_CTRL1, val);
	mutex_unlock(&the_menelaus->lock);

	return ret;
}
Пример #10
0
static void menelaus_rtc_alarm_work(struct menelaus_chip *m)
{
	/* report alarm */
	local_irq_disable();
	rtc_update_irq(m->rtc, 1, RTC_IRQF | RTC_AF);
	local_irq_enable();

	/* then disable it; alarms are oneshot */
	the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
	menelaus_write_reg(MENELAUS_RTC_CTRL, the_menelaus->rtc_control);
}
Пример #11
0
static int menelaus_set_time(struct device *dev, struct rtc_time *t)
{
	int		status;

	/* write date and time registers */
	status = time_to_menelaus(t, MENELAUS_RTC_SEC);
	if (status < 0)
		return status;
	status = menelaus_write_reg(MENELAUS_RTC_WKDAY, bin2bcd(t->tm_wday));
	if (status < 0) {
		dev_err(&the_menelaus->client->dev, "rtc write reg %02x"
				"err %d\n", MENELAUS_RTC_WKDAY, status);
		return status;
	}

	/* now commit the write */
	status = menelaus_write_reg(MENELAUS_RTC_UPDATE, RTC_UPDATE_EVERY);
	if (status < 0)
		dev_err(&the_menelaus->client->dev, "rtc commit time, err %d\n",
				status);

	return 0;
}
Пример #12
0
int menelaus_set_vcore_hw(unsigned int roof_mV, unsigned int floor_mV)
{
	int fval, rval, val, ret;
	struct i2c_client *c = the_menelaus->client;

	rval = menelaus_get_vtg_value(roof_mV, vcore_values,
				      ARRAY_SIZE(vcore_values));
	if (rval < 0)
		return -EINVAL;
	fval = menelaus_get_vtg_value(floor_mV, vcore_values,
				      ARRAY_SIZE(vcore_values));
	if (fval < 0)
		return -EINVAL;

	dev_dbg(&c->dev, "Setting VCORE FLOOR to %d mV and ROOF to %d mV\n",
	       floor_mV, roof_mV);

	mutex_lock(&the_menelaus->lock);
	ret = menelaus_write_reg(MENELAUS_VCORE_CTRL3, fval);
	if (ret < 0)
		goto out;
	ret = menelaus_write_reg(MENELAUS_VCORE_CTRL4, rval);
	if (ret < 0)
		goto out;
	if (!the_menelaus->vcore_hw_mode) {
		val = menelaus_read_reg(MENELAUS_VCORE_CTRL1);
		/* HW mode, turn OFF byte comparator */
		val |= (VCORE_CTRL1_HW_NSW | VCORE_CTRL1_BYP_COMP);
		ret = menelaus_write_reg(MENELAUS_VCORE_CTRL1, val);
		the_menelaus->vcore_hw_mode = 1;
	}
	msleep(1);
out:
	mutex_unlock(&the_menelaus->lock);
	return ret;
}
Пример #13
0
static inline void menelaus_rtc_init(struct menelaus_chip *m)
{
	int	alarm = (m->client->irq > 0);

	/* assume 32KDETEN pin is pulled high */
	if (!(menelaus_read_reg(MENELAUS_OSC_CTRL) & 0x80)) {
		dev_dbg(&m->client->dev, "no 32k oscillator\n");
		return;
	}

	/* support RTC alarm; it can issue wakeups */
	if (alarm) {
		if (menelaus_add_irq_work(MENELAUS_RTCALM_IRQ,
				menelaus_rtc_alarm_work) < 0) {
			dev_err(&m->client->dev, "can't handle RTC alarm\n");
			return;
		}
		device_init_wakeup(&m->client->dev, 1);
	}

	/* be sure RTC is enabled; allow 1/sec irqs; leave 12hr mode alone */
	m->rtc_control = menelaus_read_reg(MENELAUS_RTC_CTRL);
	if (!(m->rtc_control & RTC_CTRL_RTC_EN)
			|| (m->rtc_control & RTC_CTRL_AL_EN)
			|| (m->rtc_control & RTC_CTRL_EVERY_MASK)) {
		if (!(m->rtc_control & RTC_CTRL_RTC_EN)) {
			dev_warn(&m->client->dev, "rtc clock needs setting\n");
			m->rtc_control |= RTC_CTRL_RTC_EN;
		}
		m->rtc_control &= ~RTC_CTRL_EVERY_MASK;
		m->rtc_control &= ~RTC_CTRL_AL_EN;
		menelaus_write_reg(MENELAUS_RTC_CTRL, m->rtc_control);
	}

	m->rtc = rtc_device_register(DRIVER_NAME,
			&m->client->dev,
			&menelaus_rtc_ops, THIS_MODULE);
	if (IS_ERR(m->rtc)) {
		if (alarm) {
			menelaus_remove_irq_work(MENELAUS_RTCALM_IRQ);
			device_init_wakeup(&m->client->dev, 0);
		}
		dev_err(&m->client->dev, "can't register RTC: %d\n",
				(int) PTR_ERR(m->rtc));
		the_menelaus->rtc = NULL;
	}
}
Пример #14
0
int menelaus_set_slot_sel(int enable)
{
	int ret;

	mutex_lock(&the_menelaus->lock);
	ret = menelaus_read_reg(MENELAUS_GPIO_CTRL);
	if (ret < 0)
		goto out;
	ret |= GPIO2_DIR_INPUT;
	if (enable)
		ret |= GPIO_CTRL_SLOTSELEN;
	else
		ret &= ~GPIO_CTRL_SLOTSELEN;
	ret = menelaus_write_reg(MENELAUS_GPIO_CTRL, ret);
out:
	mutex_unlock(&the_menelaus->lock);
	return ret;
}
Пример #15
0
int menelaus_set_slot_sel(int enable)
{
	int ret;

	mutex_lock(&the_menelaus->lock);
	ret = menelaus_read_reg(MENELAUS_GPIO_CTRL);
	if (ret < 0)
		goto out;
	ret |= 0x02;
	if (enable)
		ret |= 1 << 5;
	else
		ret &= ~(1 << 5);
	ret = menelaus_write_reg(MENELAUS_GPIO_CTRL, ret);
out:
	mutex_unlock(&the_menelaus->lock);
	return ret;
}
Пример #16
0
static int menelaus_ioctl(struct device *dev, unsigned cmd, unsigned long arg)
{
	int	status;

	if (the_menelaus->client->irq <= 0)
		return -ENOIOCTLCMD;

	switch (cmd) {
	/* alarm IRQ */
	case RTC_AIE_ON:
		if (the_menelaus->rtc_control & RTC_CTRL_AL_EN)
			return 0;
		the_menelaus->rtc_control |= RTC_CTRL_AL_EN;
		break;
	case RTC_AIE_OFF:
		if (!(the_menelaus->rtc_control & RTC_CTRL_AL_EN))
			return 0;
		the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
		break;
	/* 1/second "update" IRQ */
	case RTC_UIE_ON:
		if (the_menelaus->uie)
			return 0;
		status = menelaus_remove_irq_work(MENELAUS_RTCTMR_IRQ);
		status = menelaus_add_irq_work(MENELAUS_RTCTMR_IRQ,
				menelaus_rtc_update_work);
		if (status == 0)
			the_menelaus->uie = 1;
		return status;
	case RTC_UIE_OFF:
		if (!the_menelaus->uie)
			return 0;
		status = menelaus_remove_irq_work(MENELAUS_RTCTMR_IRQ);
		if (status == 0)
			the_menelaus->uie = 0;
		return status;
	default:
		return -ENOIOCTLCMD;
	}
	return menelaus_write_reg(MENELAUS_RTC_CTRL, the_menelaus->rtc_control);
}
Пример #17
0
int menelaus_set_vcore_sw(unsigned int mV)
{
	int val, ret;
	struct i2c_client *c = the_menelaus->client;

	val = menelaus_get_vtg_value(mV, vcore_values,
				     ARRAY_SIZE(vcore_values));
	if (val < 0)
		return -EINVAL;

	dev_dbg(&c->dev, "Setting VCORE to %d mV (val 0x%02x)\n", mV, val);

	/* Set SW mode and the voltage in one go. */
	mutex_lock(&the_menelaus->lock);
	ret = menelaus_write_reg(MENELAUS_VCORE_CTRL1, val);
	if (ret == 0)
		the_menelaus->vcore_hw_mode = 0;
	mutex_unlock(&the_menelaus->lock);
	msleep(1);

	return ret;
}
Пример #18
0
static int time_to_menelaus(struct rtc_time *t, int regnum)
{
	int	hour, status;

	status = menelaus_write_reg(regnum++, bin2bcd(t->tm_sec));
	if (status < 0)
		goto fail;

	status = menelaus_write_reg(regnum++, bin2bcd(t->tm_min));
	if (status < 0)
		goto fail;

	if (the_menelaus->rtc_control & RTC_CTRL_MODE12) {
		hour = t->tm_hour + 1;
		if (hour > 12)
			hour = RTC_HR_PM | bin2bcd(hour - 12);
		else
			hour = bin2bcd(hour);
	} else
		hour = bin2bcd(t->tm_hour);
	status = menelaus_write_reg(regnum++, hour);
	if (status < 0)
		goto fail;

	status = menelaus_write_reg(regnum++, bin2bcd(t->tm_mday));
	if (status < 0)
		goto fail;

	status = menelaus_write_reg(regnum++, bin2bcd(t->tm_mon + 1));
	if (status < 0)
		goto fail;

	status = menelaus_write_reg(regnum++, bin2bcd(t->tm_year - 100));
	if (status < 0)
		goto fail;

	return 0;
fail:
	dev_err(&the_menelaus->client->dev, "rtc write reg %02x, err %d\n",
			--regnum, status);
	return status;
}
Пример #19
0
static int menelaus_probe(struct i2c_client *client,
			  const struct i2c_device_id *id)
{
	struct menelaus_chip	*menelaus;
	int			rev = 0, val;
	int			err = 0;
	struct menelaus_platform_data *menelaus_pdata =
					client->dev.platform_data;

	if (the_menelaus) {
		dev_dbg(&client->dev, "only one %s for now\n",
				DRIVER_NAME);
		return -ENODEV;
	}

	menelaus = kzalloc(sizeof *menelaus, GFP_KERNEL);
	if (!menelaus)
		return -ENOMEM;

	i2c_set_clientdata(client, menelaus);

	the_menelaus = menelaus;
	menelaus->client = client;

	/* If a true probe check the device */
	rev = menelaus_read_reg(MENELAUS_REV);
	if (rev < 0) {
		dev_err(&client->dev, "device not found");
		err = -ENODEV;
		goto fail1;
	}

	/* Ack and disable all Menelaus interrupts */
	menelaus_write_reg(MENELAUS_INT_ACK1, 0xff);
	menelaus_write_reg(MENELAUS_INT_ACK2, 0xff);
	menelaus_write_reg(MENELAUS_INT_MASK1, 0xff);
	menelaus_write_reg(MENELAUS_INT_MASK2, 0xff);
	menelaus->mask1 = 0xff;
	menelaus->mask2 = 0xff;

	/* Set output buffer strengths */
	menelaus_write_reg(MENELAUS_MCT_CTRL1, 0x73);

	if (client->irq > 0) {
		err = request_irq(client->irq, menelaus_irq, IRQF_DISABLED,
				  DRIVER_NAME, menelaus);
		if (err) {
			dev_dbg(&client->dev,  "can't get IRQ %d, err %d",
					client->irq, err);
			goto fail1;
		}
	}

	mutex_init(&menelaus->lock);
	INIT_WORK(&menelaus->work, menelaus_work);

	dev_info(&client->dev, "Menelaus rev %d.%d\n", rev >> 4, rev & 0x0f);

	val = menelaus_read_reg(MENELAUS_VCORE_CTRL1);
	if (val < 0)
		goto fail2;
	if (val & (1 << 7))
		menelaus->vcore_hw_mode = 1;
	else
		menelaus->vcore_hw_mode = 0;

	if (menelaus_pdata != NULL && menelaus_pdata->late_init != NULL) {
		err = menelaus_pdata->late_init(&client->dev);
		if (err < 0)
			goto fail2;
	}

	menelaus_rtc_init(menelaus);

	return 0;
fail2:
	free_irq(client->irq, menelaus);
	flush_scheduled_work();
fail1:
	kfree(menelaus);
	return err;
}