Linux Audio

Check our new training course

Loading...
v5.9
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * Copyright (C) 2004 Texas Instruments, Inc.
   4 *
   5 * Some parts based tps65010.c:
   6 * Copyright (C) 2004 Texas Instruments and
   7 * Copyright (C) 2004-2005 David Brownell
   8 *
   9 * Some parts based on tlv320aic24.c:
  10 * Copyright (C) by Kai Svahn <kai.svahn@nokia.com>
  11 *
  12 * Changes for interrupt handling and clean-up by
  13 * Tony Lindgren <tony@atomide.com> and Imre Deak <imre.deak@nokia.com>
  14 * Cleanup and generalized support for voltage setting by
  15 * Juha Yrjola
  16 * Added support for controlling VCORE and regulator sleep states,
  17 * Amit Kucheria <amit.kucheria@nokia.com>
  18 * Copyright (C) 2005, 2006 Nokia Corporation
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  19 */
  20
  21#include <linux/module.h>
  22#include <linux/i2c.h>
  23#include <linux/interrupt.h>
  24#include <linux/sched.h>
  25#include <linux/mutex.h>
  26#include <linux/workqueue.h>
  27#include <linux/delay.h>
  28#include <linux/rtc.h>
  29#include <linux/bcd.h>
  30#include <linux/slab.h>
  31#include <linux/mfd/menelaus.h>
  32#include <linux/gpio.h>
  33
  34#include <asm/mach/irq.h>
  35
  36
  37#define DRIVER_NAME			"menelaus"
  38
  39#define MENELAUS_I2C_ADDRESS		0x72
  40
  41#define MENELAUS_REV			0x01
  42#define MENELAUS_VCORE_CTRL1		0x02
  43#define MENELAUS_VCORE_CTRL2		0x03
  44#define MENELAUS_VCORE_CTRL3		0x04
  45#define MENELAUS_VCORE_CTRL4		0x05
  46#define MENELAUS_VCORE_CTRL5		0x06
  47#define MENELAUS_DCDC_CTRL1		0x07
  48#define MENELAUS_DCDC_CTRL2		0x08
  49#define MENELAUS_DCDC_CTRL3		0x09
  50#define MENELAUS_LDO_CTRL1		0x0A
  51#define MENELAUS_LDO_CTRL2		0x0B
  52#define MENELAUS_LDO_CTRL3		0x0C
  53#define MENELAUS_LDO_CTRL4		0x0D
  54#define MENELAUS_LDO_CTRL5		0x0E
  55#define MENELAUS_LDO_CTRL6		0x0F
  56#define MENELAUS_LDO_CTRL7		0x10
  57#define MENELAUS_LDO_CTRL8		0x11
  58#define MENELAUS_SLEEP_CTRL1		0x12
  59#define MENELAUS_SLEEP_CTRL2		0x13
  60#define MENELAUS_DEVICE_OFF		0x14
  61#define MENELAUS_OSC_CTRL		0x15
  62#define MENELAUS_DETECT_CTRL		0x16
  63#define MENELAUS_INT_MASK1		0x17
  64#define MENELAUS_INT_MASK2		0x18
  65#define MENELAUS_INT_STATUS1		0x19
  66#define MENELAUS_INT_STATUS2		0x1A
  67#define MENELAUS_INT_ACK1		0x1B
  68#define MENELAUS_INT_ACK2		0x1C
  69#define MENELAUS_GPIO_CTRL		0x1D
  70#define MENELAUS_GPIO_IN		0x1E
  71#define MENELAUS_GPIO_OUT		0x1F
  72#define MENELAUS_BBSMS			0x20
  73#define MENELAUS_RTC_CTRL		0x21
  74#define MENELAUS_RTC_UPDATE		0x22
  75#define MENELAUS_RTC_SEC		0x23
  76#define MENELAUS_RTC_MIN		0x24
  77#define MENELAUS_RTC_HR			0x25
  78#define MENELAUS_RTC_DAY		0x26
  79#define MENELAUS_RTC_MON		0x27
  80#define MENELAUS_RTC_YR			0x28
  81#define MENELAUS_RTC_WKDAY		0x29
  82#define MENELAUS_RTC_AL_SEC		0x2A
  83#define MENELAUS_RTC_AL_MIN		0x2B
  84#define MENELAUS_RTC_AL_HR		0x2C
  85#define MENELAUS_RTC_AL_DAY		0x2D
  86#define MENELAUS_RTC_AL_MON		0x2E
  87#define MENELAUS_RTC_AL_YR		0x2F
  88#define MENELAUS_RTC_COMP_MSB		0x30
  89#define MENELAUS_RTC_COMP_LSB		0x31
  90#define MENELAUS_S1_PULL_EN		0x32
  91#define MENELAUS_S1_PULL_DIR		0x33
  92#define MENELAUS_S2_PULL_EN		0x34
  93#define MENELAUS_S2_PULL_DIR		0x35
  94#define MENELAUS_MCT_CTRL1		0x36
  95#define MENELAUS_MCT_CTRL2		0x37
  96#define MENELAUS_MCT_CTRL3		0x38
  97#define MENELAUS_MCT_PIN_ST		0x39
  98#define MENELAUS_DEBOUNCE1		0x3A
  99
 100#define IH_MENELAUS_IRQS		12
 101#define MENELAUS_MMC_S1CD_IRQ		0	/* MMC slot 1 card change */
 102#define MENELAUS_MMC_S2CD_IRQ		1	/* MMC slot 2 card change */
 103#define MENELAUS_MMC_S1D1_IRQ		2	/* MMC DAT1 low in slot 1 */
 104#define MENELAUS_MMC_S2D1_IRQ		3	/* MMC DAT1 low in slot 2 */
 105#define MENELAUS_LOWBAT_IRQ		4	/* Low battery */
 106#define MENELAUS_HOTDIE_IRQ		5	/* Hot die detect */
 107#define MENELAUS_UVLO_IRQ		6	/* UVLO detect */
 108#define MENELAUS_TSHUT_IRQ		7	/* Thermal shutdown */
 109#define MENELAUS_RTCTMR_IRQ		8	/* RTC timer */
 110#define MENELAUS_RTCALM_IRQ		9	/* RTC alarm */
 111#define MENELAUS_RTCERR_IRQ		10	/* RTC error */
 112#define MENELAUS_PSHBTN_IRQ		11	/* Push button */
 113#define MENELAUS_RESERVED12_IRQ		12	/* Reserved */
 114#define MENELAUS_RESERVED13_IRQ		13	/* Reserved */
 115#define MENELAUS_RESERVED14_IRQ		14	/* Reserved */
 116#define MENELAUS_RESERVED15_IRQ		15	/* Reserved */
 117
 118/* VCORE_CTRL1 register */
 119#define VCORE_CTRL1_BYP_COMP		(1 << 5)
 120#define VCORE_CTRL1_HW_NSW		(1 << 7)
 121
 122/* GPIO_CTRL register */
 123#define GPIO_CTRL_SLOTSELEN		(1 << 5)
 124#define GPIO_CTRL_SLPCTLEN		(1 << 6)
 125#define GPIO1_DIR_INPUT			(1 << 0)
 126#define GPIO2_DIR_INPUT			(1 << 1)
 127#define GPIO3_DIR_INPUT			(1 << 2)
 128
 129/* MCT_CTRL1 register */
 130#define MCT_CTRL1_S1_CMD_OD		(1 << 2)
 131#define MCT_CTRL1_S2_CMD_OD		(1 << 3)
 132
 133/* MCT_CTRL2 register */
 134#define MCT_CTRL2_VS2_SEL_D0		(1 << 0)
 135#define MCT_CTRL2_VS2_SEL_D1		(1 << 1)
 136#define MCT_CTRL2_S1CD_BUFEN		(1 << 4)
 137#define MCT_CTRL2_S2CD_BUFEN		(1 << 5)
 138#define MCT_CTRL2_S1CD_DBEN		(1 << 6)
 139#define MCT_CTRL2_S2CD_BEN		(1 << 7)
 140
 141/* MCT_CTRL3 register */
 142#define MCT_CTRL3_SLOT1_EN		(1 << 0)
 143#define MCT_CTRL3_SLOT2_EN		(1 << 1)
 144#define MCT_CTRL3_S1_AUTO_EN		(1 << 2)
 145#define MCT_CTRL3_S2_AUTO_EN		(1 << 3)
 146
 147/* MCT_PIN_ST register */
 148#define MCT_PIN_ST_S1_CD_ST		(1 << 0)
 149#define MCT_PIN_ST_S2_CD_ST		(1 << 1)
 150
 151static void menelaus_work(struct work_struct *_menelaus);
 152
 153struct menelaus_chip {
 154	struct mutex		lock;
 155	struct i2c_client	*client;
 156	struct work_struct	work;
 157#ifdef CONFIG_RTC_DRV_TWL92330
 158	struct rtc_device	*rtc;
 159	u8			rtc_control;
 160	unsigned		uie:1;
 161#endif
 162	unsigned		vcore_hw_mode:1;
 163	u8			mask1, mask2;
 164	void			(*handlers[16])(struct menelaus_chip *);
 165	void			(*mmc_callback)(void *data, u8 mask);
 166	void			*mmc_callback_data;
 167};
 168
 169static struct menelaus_chip *the_menelaus;
 170
 171static int menelaus_write_reg(int reg, u8 value)
 172{
 173	int val = i2c_smbus_write_byte_data(the_menelaus->client, reg, value);
 174
 175	if (val < 0) {
 176		pr_err(DRIVER_NAME ": write error");
 177		return val;
 178	}
 179
 180	return 0;
 181}
 182
 183static int menelaus_read_reg(int reg)
 184{
 185	int val = i2c_smbus_read_byte_data(the_menelaus->client, reg);
 186
 187	if (val < 0)
 188		pr_err(DRIVER_NAME ": read error");
 189
 190	return val;
 191}
 192
 193static int menelaus_enable_irq(int irq)
 194{
 195	if (irq > 7) {
 196		irq -= 8;
 197		the_menelaus->mask2 &= ~(1 << irq);
 198		return menelaus_write_reg(MENELAUS_INT_MASK2,
 199				the_menelaus->mask2);
 200	} else {
 201		the_menelaus->mask1 &= ~(1 << irq);
 202		return menelaus_write_reg(MENELAUS_INT_MASK1,
 203				the_menelaus->mask1);
 204	}
 205}
 206
 207static int menelaus_disable_irq(int irq)
 208{
 209	if (irq > 7) {
 210		irq -= 8;
 211		the_menelaus->mask2 |= (1 << irq);
 212		return menelaus_write_reg(MENELAUS_INT_MASK2,
 213				the_menelaus->mask2);
 214	} else {
 215		the_menelaus->mask1 |= (1 << irq);
 216		return menelaus_write_reg(MENELAUS_INT_MASK1,
 217				the_menelaus->mask1);
 218	}
 219}
 220
 221static int menelaus_ack_irq(int irq)
 222{
 223	if (irq > 7)
 224		return menelaus_write_reg(MENELAUS_INT_ACK2, 1 << (irq - 8));
 225	else
 226		return menelaus_write_reg(MENELAUS_INT_ACK1, 1 << irq);
 227}
 228
 229/* Adds a handler for an interrupt. Does not run in interrupt context */
 230static int menelaus_add_irq_work(int irq,
 231		void (*handler)(struct menelaus_chip *))
 232{
 233	int ret = 0;
 234
 235	mutex_lock(&the_menelaus->lock);
 236	the_menelaus->handlers[irq] = handler;
 237	ret = menelaus_enable_irq(irq);
 238	mutex_unlock(&the_menelaus->lock);
 239
 240	return ret;
 241}
 242
 243/* Removes handler for an interrupt */
 244static int menelaus_remove_irq_work(int irq)
 245{
 246	int ret = 0;
 247
 248	mutex_lock(&the_menelaus->lock);
 249	ret = menelaus_disable_irq(irq);
 250	the_menelaus->handlers[irq] = NULL;
 251	mutex_unlock(&the_menelaus->lock);
 252
 253	return ret;
 254}
 255
 256/*
 257 * Gets scheduled when a card detect interrupt happens. Note that in some cases
 258 * this line is wired to card cover switch rather than the card detect switch
 259 * in each slot. In this case the cards are not seen by menelaus.
 260 * FIXME: Add handling for D1 too
 261 */
 262static void menelaus_mmc_cd_work(struct menelaus_chip *menelaus_hw)
 263{
 264	int reg;
 265	unsigned char card_mask = 0;
 266
 267	reg = menelaus_read_reg(MENELAUS_MCT_PIN_ST);
 268	if (reg < 0)
 269		return;
 270
 271	if (!(reg & 0x1))
 272		card_mask |= MCT_PIN_ST_S1_CD_ST;
 273
 274	if (!(reg & 0x2))
 275		card_mask |= MCT_PIN_ST_S2_CD_ST;
 276
 277	if (menelaus_hw->mmc_callback)
 278		menelaus_hw->mmc_callback(menelaus_hw->mmc_callback_data,
 279					  card_mask);
 280}
 281
 282/*
 283 * Toggles the MMC slots between open-drain and push-pull mode.
 284 */
 285int menelaus_set_mmc_opendrain(int slot, int enable)
 286{
 287	int ret, val;
 288
 289	if (slot != 1 && slot != 2)
 290		return -EINVAL;
 291	mutex_lock(&the_menelaus->lock);
 292	ret = menelaus_read_reg(MENELAUS_MCT_CTRL1);
 293	if (ret < 0) {
 294		mutex_unlock(&the_menelaus->lock);
 295		return ret;
 296	}
 297	val = ret;
 298	if (slot == 1) {
 299		if (enable)
 300			val |= MCT_CTRL1_S1_CMD_OD;
 301		else
 302			val &= ~MCT_CTRL1_S1_CMD_OD;
 303	} else {
 304		if (enable)
 305			val |= MCT_CTRL1_S2_CMD_OD;
 306		else
 307			val &= ~MCT_CTRL1_S2_CMD_OD;
 308	}
 309	ret = menelaus_write_reg(MENELAUS_MCT_CTRL1, val);
 310	mutex_unlock(&the_menelaus->lock);
 311
 312	return ret;
 313}
 314EXPORT_SYMBOL(menelaus_set_mmc_opendrain);
 315
 316int menelaus_set_slot_sel(int enable)
 317{
 318	int ret;
 319
 320	mutex_lock(&the_menelaus->lock);
 321	ret = menelaus_read_reg(MENELAUS_GPIO_CTRL);
 322	if (ret < 0)
 323		goto out;
 324	ret |= GPIO2_DIR_INPUT;
 325	if (enable)
 326		ret |= GPIO_CTRL_SLOTSELEN;
 327	else
 328		ret &= ~GPIO_CTRL_SLOTSELEN;
 329	ret = menelaus_write_reg(MENELAUS_GPIO_CTRL, ret);
 330out:
 331	mutex_unlock(&the_menelaus->lock);
 332	return ret;
 333}
 334EXPORT_SYMBOL(menelaus_set_slot_sel);
 335
 336int menelaus_set_mmc_slot(int slot, int enable, int power, int cd_en)
 337{
 338	int ret, val;
 339
 340	if (slot != 1 && slot != 2)
 341		return -EINVAL;
 342	if (power >= 3)
 343		return -EINVAL;
 344
 345	mutex_lock(&the_menelaus->lock);
 346
 347	ret = menelaus_read_reg(MENELAUS_MCT_CTRL2);
 348	if (ret < 0)
 349		goto out;
 350	val = ret;
 351	if (slot == 1) {
 352		if (cd_en)
 353			val |= MCT_CTRL2_S1CD_BUFEN | MCT_CTRL2_S1CD_DBEN;
 354		else
 355			val &= ~(MCT_CTRL2_S1CD_BUFEN | MCT_CTRL2_S1CD_DBEN);
 356	} else {
 357		if (cd_en)
 358			val |= MCT_CTRL2_S2CD_BUFEN | MCT_CTRL2_S2CD_BEN;
 359		else
 360			val &= ~(MCT_CTRL2_S2CD_BUFEN | MCT_CTRL2_S2CD_BEN);
 361	}
 362	ret = menelaus_write_reg(MENELAUS_MCT_CTRL2, val);
 363	if (ret < 0)
 364		goto out;
 365
 366	ret = menelaus_read_reg(MENELAUS_MCT_CTRL3);
 367	if (ret < 0)
 368		goto out;
 369	val = ret;
 370	if (slot == 1) {
 371		if (enable)
 372			val |= MCT_CTRL3_SLOT1_EN;
 373		else
 374			val &= ~MCT_CTRL3_SLOT1_EN;
 375	} else {
 376		int b;
 377
 378		if (enable)
 379			val |= MCT_CTRL3_SLOT2_EN;
 380		else
 381			val &= ~MCT_CTRL3_SLOT2_EN;
 382		b = menelaus_read_reg(MENELAUS_MCT_CTRL2);
 383		b &= ~(MCT_CTRL2_VS2_SEL_D0 | MCT_CTRL2_VS2_SEL_D1);
 384		b |= power;
 385		ret = menelaus_write_reg(MENELAUS_MCT_CTRL2, b);
 386		if (ret < 0)
 387			goto out;
 388	}
 389	/* Disable autonomous shutdown */
 390	val &= ~(MCT_CTRL3_S1_AUTO_EN | MCT_CTRL3_S2_AUTO_EN);
 391	ret = menelaus_write_reg(MENELAUS_MCT_CTRL3, val);
 392out:
 393	mutex_unlock(&the_menelaus->lock);
 394	return ret;
 395}
 396EXPORT_SYMBOL(menelaus_set_mmc_slot);
 397
 398int menelaus_register_mmc_callback(void (*callback)(void *data, u8 card_mask),
 399				   void *data)
 400{
 401	int ret = 0;
 402
 403	the_menelaus->mmc_callback_data = data;
 404	the_menelaus->mmc_callback = callback;
 405	ret = menelaus_add_irq_work(MENELAUS_MMC_S1CD_IRQ,
 406				    menelaus_mmc_cd_work);
 407	if (ret < 0)
 408		return ret;
 409	ret = menelaus_add_irq_work(MENELAUS_MMC_S2CD_IRQ,
 410				    menelaus_mmc_cd_work);
 411	if (ret < 0)
 412		return ret;
 413	ret = menelaus_add_irq_work(MENELAUS_MMC_S1D1_IRQ,
 414				    menelaus_mmc_cd_work);
 415	if (ret < 0)
 416		return ret;
 417	ret = menelaus_add_irq_work(MENELAUS_MMC_S2D1_IRQ,
 418				    menelaus_mmc_cd_work);
 419
 420	return ret;
 421}
 422EXPORT_SYMBOL(menelaus_register_mmc_callback);
 423
 424void menelaus_unregister_mmc_callback(void)
 425{
 426	menelaus_remove_irq_work(MENELAUS_MMC_S1CD_IRQ);
 427	menelaus_remove_irq_work(MENELAUS_MMC_S2CD_IRQ);
 428	menelaus_remove_irq_work(MENELAUS_MMC_S1D1_IRQ);
 429	menelaus_remove_irq_work(MENELAUS_MMC_S2D1_IRQ);
 430
 431	the_menelaus->mmc_callback = NULL;
 432	the_menelaus->mmc_callback_data = NULL;
 433}
 434EXPORT_SYMBOL(menelaus_unregister_mmc_callback);
 435
 436struct menelaus_vtg {
 437	const char *name;
 438	u8 vtg_reg;
 439	u8 vtg_shift;
 440	u8 vtg_bits;
 441	u8 mode_reg;
 442};
 443
 444struct menelaus_vtg_value {
 445	u16 vtg;
 446	u16 val;
 447};
 448
 449static int menelaus_set_voltage(const struct menelaus_vtg *vtg, int mV,
 450				int vtg_val, int mode)
 451{
 452	int val, ret;
 453	struct i2c_client *c = the_menelaus->client;
 454
 455	mutex_lock(&the_menelaus->lock);
 456
 457	ret = menelaus_read_reg(vtg->vtg_reg);
 458	if (ret < 0)
 459		goto out;
 460	val = ret & ~(((1 << vtg->vtg_bits) - 1) << vtg->vtg_shift);
 461	val |= vtg_val << vtg->vtg_shift;
 462
 463	dev_dbg(&c->dev, "Setting voltage '%s'"
 464			 "to %d mV (reg 0x%02x, val 0x%02x)\n",
 465			vtg->name, mV, vtg->vtg_reg, val);
 466
 467	ret = menelaus_write_reg(vtg->vtg_reg, val);
 468	if (ret < 0)
 469		goto out;
 470	ret = menelaus_write_reg(vtg->mode_reg, mode);
 471out:
 472	mutex_unlock(&the_menelaus->lock);
 473	if (ret == 0) {
 474		/* Wait for voltage to stabilize */
 475		msleep(1);
 476	}
 477	return ret;
 478}
 479
 480static int menelaus_get_vtg_value(int vtg, const struct menelaus_vtg_value *tbl,
 481				  int n)
 482{
 483	int i;
 484
 485	for (i = 0; i < n; i++, tbl++)
 486		if (tbl->vtg == vtg)
 487			return tbl->val;
 488	return -EINVAL;
 489}
 490
 491/*
 492 * Vcore can be programmed in two ways:
 493 * SW-controlled: Required voltage is programmed into VCORE_CTRL1
 494 * HW-controlled: Required range (roof-floor) is programmed into VCORE_CTRL3
 495 * and VCORE_CTRL4
 496 *
 497 * Call correct 'set' function accordingly
 498 */
 499
 500static const struct menelaus_vtg_value vcore_values[] = {
 501	{ 1000, 0 },
 502	{ 1025, 1 },
 503	{ 1050, 2 },
 504	{ 1075, 3 },
 505	{ 1100, 4 },
 506	{ 1125, 5 },
 507	{ 1150, 6 },
 508	{ 1175, 7 },
 509	{ 1200, 8 },
 510	{ 1225, 9 },
 511	{ 1250, 10 },
 512	{ 1275, 11 },
 513	{ 1300, 12 },
 514	{ 1325, 13 },
 515	{ 1350, 14 },
 516	{ 1375, 15 },
 517	{ 1400, 16 },
 518	{ 1425, 17 },
 519	{ 1450, 18 },
 520};
 521
 522int menelaus_set_vcore_hw(unsigned int roof_mV, unsigned int floor_mV)
 523{
 524	int fval, rval, val, ret;
 525	struct i2c_client *c = the_menelaus->client;
 526
 527	rval = menelaus_get_vtg_value(roof_mV, vcore_values,
 528				      ARRAY_SIZE(vcore_values));
 529	if (rval < 0)
 530		return -EINVAL;
 531	fval = menelaus_get_vtg_value(floor_mV, vcore_values,
 532				      ARRAY_SIZE(vcore_values));
 533	if (fval < 0)
 534		return -EINVAL;
 535
 536	dev_dbg(&c->dev, "Setting VCORE FLOOR to %d mV and ROOF to %d mV\n",
 537	       floor_mV, roof_mV);
 538
 539	mutex_lock(&the_menelaus->lock);
 540	ret = menelaus_write_reg(MENELAUS_VCORE_CTRL3, fval);
 541	if (ret < 0)
 542		goto out;
 543	ret = menelaus_write_reg(MENELAUS_VCORE_CTRL4, rval);
 544	if (ret < 0)
 545		goto out;
 546	if (!the_menelaus->vcore_hw_mode) {
 547		val = menelaus_read_reg(MENELAUS_VCORE_CTRL1);
 548		/* HW mode, turn OFF byte comparator */
 549		val |= (VCORE_CTRL1_HW_NSW | VCORE_CTRL1_BYP_COMP);
 550		ret = menelaus_write_reg(MENELAUS_VCORE_CTRL1, val);
 551		the_menelaus->vcore_hw_mode = 1;
 552	}
 553	msleep(1);
 554out:
 555	mutex_unlock(&the_menelaus->lock);
 556	return ret;
 557}
 558
 559static const struct menelaus_vtg vmem_vtg = {
 560	.name = "VMEM",
 561	.vtg_reg = MENELAUS_LDO_CTRL1,
 562	.vtg_shift = 0,
 563	.vtg_bits = 2,
 564	.mode_reg = MENELAUS_LDO_CTRL3,
 565};
 566
 567static const struct menelaus_vtg_value vmem_values[] = {
 568	{ 1500, 0 },
 569	{ 1800, 1 },
 570	{ 1900, 2 },
 571	{ 2500, 3 },
 572};
 573
 574int menelaus_set_vmem(unsigned int mV)
 575{
 576	int val;
 577
 578	if (mV == 0)
 579		return menelaus_set_voltage(&vmem_vtg, 0, 0, 0);
 580
 581	val = menelaus_get_vtg_value(mV, vmem_values, ARRAY_SIZE(vmem_values));
 582	if (val < 0)
 583		return -EINVAL;
 584	return menelaus_set_voltage(&vmem_vtg, mV, val, 0x02);
 585}
 586EXPORT_SYMBOL(menelaus_set_vmem);
 587
 588static const struct menelaus_vtg vio_vtg = {
 589	.name = "VIO",
 590	.vtg_reg = MENELAUS_LDO_CTRL1,
 591	.vtg_shift = 2,
 592	.vtg_bits = 2,
 593	.mode_reg = MENELAUS_LDO_CTRL4,
 594};
 595
 596static const struct menelaus_vtg_value vio_values[] = {
 597	{ 1500, 0 },
 598	{ 1800, 1 },
 599	{ 2500, 2 },
 600	{ 2800, 3 },
 601};
 602
 603int menelaus_set_vio(unsigned int mV)
 604{
 605	int val;
 606
 607	if (mV == 0)
 608		return menelaus_set_voltage(&vio_vtg, 0, 0, 0);
 609
 610	val = menelaus_get_vtg_value(mV, vio_values, ARRAY_SIZE(vio_values));
 611	if (val < 0)
 612		return -EINVAL;
 613	return menelaus_set_voltage(&vio_vtg, mV, val, 0x02);
 614}
 615EXPORT_SYMBOL(menelaus_set_vio);
 616
 617static const struct menelaus_vtg_value vdcdc_values[] = {
 618	{ 1500, 0 },
 619	{ 1800, 1 },
 620	{ 2000, 2 },
 621	{ 2200, 3 },
 622	{ 2400, 4 },
 623	{ 2800, 5 },
 624	{ 3000, 6 },
 625	{ 3300, 7 },
 626};
 627
 628static const struct menelaus_vtg vdcdc2_vtg = {
 629	.name = "VDCDC2",
 630	.vtg_reg = MENELAUS_DCDC_CTRL1,
 631	.vtg_shift = 0,
 632	.vtg_bits = 3,
 633	.mode_reg = MENELAUS_DCDC_CTRL2,
 634};
 635
 636static const struct menelaus_vtg vdcdc3_vtg = {
 637	.name = "VDCDC3",
 638	.vtg_reg = MENELAUS_DCDC_CTRL1,
 639	.vtg_shift = 3,
 640	.vtg_bits = 3,
 641	.mode_reg = MENELAUS_DCDC_CTRL3,
 642};
 643
 644int menelaus_set_vdcdc(int dcdc, unsigned int mV)
 645{
 646	const struct menelaus_vtg *vtg;
 647	int val;
 648
 649	if (dcdc != 2 && dcdc != 3)
 650		return -EINVAL;
 651	if (dcdc == 2)
 652		vtg = &vdcdc2_vtg;
 653	else
 654		vtg = &vdcdc3_vtg;
 655
 656	if (mV == 0)
 657		return menelaus_set_voltage(vtg, 0, 0, 0);
 658
 659	val = menelaus_get_vtg_value(mV, vdcdc_values,
 660				     ARRAY_SIZE(vdcdc_values));
 661	if (val < 0)
 662		return -EINVAL;
 663	return menelaus_set_voltage(vtg, mV, val, 0x03);
 664}
 665
 666static const struct menelaus_vtg_value vmmc_values[] = {
 667	{ 1850, 0 },
 668	{ 2800, 1 },
 669	{ 3000, 2 },
 670	{ 3100, 3 },
 671};
 672
 673static const struct menelaus_vtg vmmc_vtg = {
 674	.name = "VMMC",
 675	.vtg_reg = MENELAUS_LDO_CTRL1,
 676	.vtg_shift = 6,
 677	.vtg_bits = 2,
 678	.mode_reg = MENELAUS_LDO_CTRL7,
 679};
 680
 681int menelaus_set_vmmc(unsigned int mV)
 682{
 683	int val;
 684
 685	if (mV == 0)
 686		return menelaus_set_voltage(&vmmc_vtg, 0, 0, 0);
 687
 688	val = menelaus_get_vtg_value(mV, vmmc_values, ARRAY_SIZE(vmmc_values));
 689	if (val < 0)
 690		return -EINVAL;
 691	return menelaus_set_voltage(&vmmc_vtg, mV, val, 0x02);
 692}
 693EXPORT_SYMBOL(menelaus_set_vmmc);
 694
 695
 696static const struct menelaus_vtg_value vaux_values[] = {
 697	{ 1500, 0 },
 698	{ 1800, 1 },
 699	{ 2500, 2 },
 700	{ 2800, 3 },
 701};
 702
 703static const struct menelaus_vtg vaux_vtg = {
 704	.name = "VAUX",
 705	.vtg_reg = MENELAUS_LDO_CTRL1,
 706	.vtg_shift = 4,
 707	.vtg_bits = 2,
 708	.mode_reg = MENELAUS_LDO_CTRL6,
 709};
 710
 711int menelaus_set_vaux(unsigned int mV)
 712{
 713	int val;
 714
 715	if (mV == 0)
 716		return menelaus_set_voltage(&vaux_vtg, 0, 0, 0);
 717
 718	val = menelaus_get_vtg_value(mV, vaux_values, ARRAY_SIZE(vaux_values));
 719	if (val < 0)
 720		return -EINVAL;
 721	return menelaus_set_voltage(&vaux_vtg, mV, val, 0x02);
 722}
 723EXPORT_SYMBOL(menelaus_set_vaux);
 724
 725int menelaus_get_slot_pin_states(void)
 726{
 727	return menelaus_read_reg(MENELAUS_MCT_PIN_ST);
 728}
 729EXPORT_SYMBOL(menelaus_get_slot_pin_states);
 730
 731int menelaus_set_regulator_sleep(int enable, u32 val)
 732{
 733	int t, ret;
 734	struct i2c_client *c = the_menelaus->client;
 735
 736	mutex_lock(&the_menelaus->lock);
 737	ret = menelaus_write_reg(MENELAUS_SLEEP_CTRL2, val);
 738	if (ret < 0)
 739		goto out;
 740
 741	dev_dbg(&c->dev, "regulator sleep configuration: %02x\n", val);
 742
 743	ret = menelaus_read_reg(MENELAUS_GPIO_CTRL);
 744	if (ret < 0)
 745		goto out;
 746	t = (GPIO_CTRL_SLPCTLEN | GPIO3_DIR_INPUT);
 747	if (enable)
 748		ret |= t;
 749	else
 750		ret &= ~t;
 751	ret = menelaus_write_reg(MENELAUS_GPIO_CTRL, ret);
 752out:
 753	mutex_unlock(&the_menelaus->lock);
 754	return ret;
 755}
 756
 757/*-----------------------------------------------------------------------*/
 758
 759/* Handles Menelaus interrupts. Does not run in interrupt context */
 760static void menelaus_work(struct work_struct *_menelaus)
 761{
 762	struct menelaus_chip *menelaus =
 763			container_of(_menelaus, struct menelaus_chip, work);
 764	void (*handler)(struct menelaus_chip *menelaus);
 765
 766	while (1) {
 767		unsigned isr;
 768
 769		isr = (menelaus_read_reg(MENELAUS_INT_STATUS2)
 770				& ~menelaus->mask2) << 8;
 771		isr |= menelaus_read_reg(MENELAUS_INT_STATUS1)
 772				& ~menelaus->mask1;
 773		if (!isr)
 774			break;
 775
 776		while (isr) {
 777			int irq = fls(isr) - 1;
 778			isr &= ~(1 << irq);
 779
 780			mutex_lock(&menelaus->lock);
 781			menelaus_disable_irq(irq);
 782			menelaus_ack_irq(irq);
 783			handler = menelaus->handlers[irq];
 784			if (handler)
 785				handler(menelaus);
 786			menelaus_enable_irq(irq);
 787			mutex_unlock(&menelaus->lock);
 788		}
 789	}
 790	enable_irq(menelaus->client->irq);
 791}
 792
 793/*
 794 * We cannot use I2C in interrupt context, so we just schedule work.
 795 */
 796static irqreturn_t menelaus_irq(int irq, void *_menelaus)
 797{
 798	struct menelaus_chip *menelaus = _menelaus;
 799
 800	disable_irq_nosync(irq);
 801	(void)schedule_work(&menelaus->work);
 802
 803	return IRQ_HANDLED;
 804}
 805
 806/*-----------------------------------------------------------------------*/
 807
 808/*
 809 * The RTC needs to be set once, then it runs on backup battery power.
 810 * It supports alarms, including system wake alarms (from some modes);
 811 * and 1/second IRQs if requested.
 812 */
 813#ifdef CONFIG_RTC_DRV_TWL92330
 814
 815#define RTC_CTRL_RTC_EN		(1 << 0)
 816#define RTC_CTRL_AL_EN		(1 << 1)
 817#define RTC_CTRL_MODE12		(1 << 2)
 818#define RTC_CTRL_EVERY_MASK	(3 << 3)
 819#define RTC_CTRL_EVERY_SEC	(0 << 3)
 820#define RTC_CTRL_EVERY_MIN	(1 << 3)
 821#define RTC_CTRL_EVERY_HR	(2 << 3)
 822#define RTC_CTRL_EVERY_DAY	(3 << 3)
 823
 824#define RTC_UPDATE_EVERY	0x08
 825
 826#define RTC_HR_PM		(1 << 7)
 827
 828static void menelaus_to_time(char *regs, struct rtc_time *t)
 829{
 830	t->tm_sec = bcd2bin(regs[0]);
 831	t->tm_min = bcd2bin(regs[1]);
 832	if (the_menelaus->rtc_control & RTC_CTRL_MODE12) {
 833		t->tm_hour = bcd2bin(regs[2] & 0x1f) - 1;
 834		if (regs[2] & RTC_HR_PM)
 835			t->tm_hour += 12;
 836	} else
 837		t->tm_hour = bcd2bin(regs[2] & 0x3f);
 838	t->tm_mday = bcd2bin(regs[3]);
 839	t->tm_mon = bcd2bin(regs[4]) - 1;
 840	t->tm_year = bcd2bin(regs[5]) + 100;
 841}
 842
 843static int time_to_menelaus(struct rtc_time *t, int regnum)
 844{
 845	int	hour, status;
 846
 847	status = menelaus_write_reg(regnum++, bin2bcd(t->tm_sec));
 848	if (status < 0)
 849		goto fail;
 850
 851	status = menelaus_write_reg(regnum++, bin2bcd(t->tm_min));
 852	if (status < 0)
 853		goto fail;
 854
 855	if (the_menelaus->rtc_control & RTC_CTRL_MODE12) {
 856		hour = t->tm_hour + 1;
 857		if (hour > 12)
 858			hour = RTC_HR_PM | bin2bcd(hour - 12);
 859		else
 860			hour = bin2bcd(hour);
 861	} else
 862		hour = bin2bcd(t->tm_hour);
 863	status = menelaus_write_reg(regnum++, hour);
 864	if (status < 0)
 865		goto fail;
 866
 867	status = menelaus_write_reg(regnum++, bin2bcd(t->tm_mday));
 868	if (status < 0)
 869		goto fail;
 870
 871	status = menelaus_write_reg(regnum++, bin2bcd(t->tm_mon + 1));
 872	if (status < 0)
 873		goto fail;
 874
 875	status = menelaus_write_reg(regnum++, bin2bcd(t->tm_year - 100));
 876	if (status < 0)
 877		goto fail;
 878
 879	return 0;
 880fail:
 881	dev_err(&the_menelaus->client->dev, "rtc write reg %02x, err %d\n",
 882			--regnum, status);
 883	return status;
 884}
 885
 886static int menelaus_read_time(struct device *dev, struct rtc_time *t)
 887{
 888	struct i2c_msg	msg[2];
 889	char		regs[7];
 890	int		status;
 891
 892	/* block read date and time registers */
 893	regs[0] = MENELAUS_RTC_SEC;
 894
 895	msg[0].addr = MENELAUS_I2C_ADDRESS;
 896	msg[0].flags = 0;
 897	msg[0].len = 1;
 898	msg[0].buf = regs;
 899
 900	msg[1].addr = MENELAUS_I2C_ADDRESS;
 901	msg[1].flags = I2C_M_RD;
 902	msg[1].len = sizeof(regs);
 903	msg[1].buf = regs;
 904
 905	status = i2c_transfer(the_menelaus->client->adapter, msg, 2);
 906	if (status != 2) {
 907		dev_err(dev, "%s error %d\n", "read", status);
 908		return -EIO;
 909	}
 910
 911	menelaus_to_time(regs, t);
 912	t->tm_wday = bcd2bin(regs[6]);
 913
 914	return 0;
 915}
 916
 917static int menelaus_set_time(struct device *dev, struct rtc_time *t)
 918{
 919	int		status;
 920
 921	/* write date and time registers */
 922	status = time_to_menelaus(t, MENELAUS_RTC_SEC);
 923	if (status < 0)
 924		return status;
 925	status = menelaus_write_reg(MENELAUS_RTC_WKDAY, bin2bcd(t->tm_wday));
 926	if (status < 0) {
 927		dev_err(&the_menelaus->client->dev, "rtc write reg %02x "
 928				"err %d\n", MENELAUS_RTC_WKDAY, status);
 929		return status;
 930	}
 931
 932	/* now commit the write */
 933	status = menelaus_write_reg(MENELAUS_RTC_UPDATE, RTC_UPDATE_EVERY);
 934	if (status < 0)
 935		dev_err(&the_menelaus->client->dev, "rtc commit time, err %d\n",
 936				status);
 937
 938	return 0;
 939}
 940
 941static int menelaus_read_alarm(struct device *dev, struct rtc_wkalrm *w)
 942{
 943	struct i2c_msg	msg[2];
 944	char		regs[6];
 945	int		status;
 946
 947	/* block read alarm registers */
 948	regs[0] = MENELAUS_RTC_AL_SEC;
 949
 950	msg[0].addr = MENELAUS_I2C_ADDRESS;
 951	msg[0].flags = 0;
 952	msg[0].len = 1;
 953	msg[0].buf = regs;
 954
 955	msg[1].addr = MENELAUS_I2C_ADDRESS;
 956	msg[1].flags = I2C_M_RD;
 957	msg[1].len = sizeof(regs);
 958	msg[1].buf = regs;
 959
 960	status = i2c_transfer(the_menelaus->client->adapter, msg, 2);
 961	if (status != 2) {
 962		dev_err(dev, "%s error %d\n", "alarm read", status);
 963		return -EIO;
 964	}
 965
 966	menelaus_to_time(regs, &w->time);
 967
 968	w->enabled = !!(the_menelaus->rtc_control & RTC_CTRL_AL_EN);
 969
 970	/* NOTE we *could* check if actually pending... */
 971	w->pending = 0;
 972
 973	return 0;
 974}
 975
 976static int menelaus_set_alarm(struct device *dev, struct rtc_wkalrm *w)
 977{
 978	int		status;
 979
 980	if (the_menelaus->client->irq <= 0 && w->enabled)
 981		return -ENODEV;
 982
 983	/* clear previous alarm enable */
 984	if (the_menelaus->rtc_control & RTC_CTRL_AL_EN) {
 985		the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
 986		status = menelaus_write_reg(MENELAUS_RTC_CTRL,
 987				the_menelaus->rtc_control);
 988		if (status < 0)
 989			return status;
 990	}
 991
 992	/* write alarm registers */
 993	status = time_to_menelaus(&w->time, MENELAUS_RTC_AL_SEC);
 994	if (status < 0)
 995		return status;
 996
 997	/* enable alarm if requested */
 998	if (w->enabled) {
 999		the_menelaus->rtc_control |= RTC_CTRL_AL_EN;
1000		status = menelaus_write_reg(MENELAUS_RTC_CTRL,
1001				the_menelaus->rtc_control);
1002	}
1003
1004	return status;
1005}
1006
1007#ifdef CONFIG_RTC_INTF_DEV
1008
1009static void menelaus_rtc_update_work(struct menelaus_chip *m)
1010{
1011	/* report 1/sec update */
 
1012	rtc_update_irq(m->rtc, 1, RTC_IRQF | RTC_UF);
 
1013}
1014
1015static int menelaus_ioctl(struct device *dev, unsigned cmd, unsigned long arg)
1016{
1017	int	status;
1018
1019	if (the_menelaus->client->irq <= 0)
1020		return -ENOIOCTLCMD;
1021
1022	switch (cmd) {
1023	/* alarm IRQ */
1024	case RTC_AIE_ON:
1025		if (the_menelaus->rtc_control & RTC_CTRL_AL_EN)
1026			return 0;
1027		the_menelaus->rtc_control |= RTC_CTRL_AL_EN;
1028		break;
1029	case RTC_AIE_OFF:
1030		if (!(the_menelaus->rtc_control & RTC_CTRL_AL_EN))
1031			return 0;
1032		the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
1033		break;
1034	/* 1/second "update" IRQ */
1035	case RTC_UIE_ON:
1036		if (the_menelaus->uie)
1037			return 0;
1038		status = menelaus_remove_irq_work(MENELAUS_RTCTMR_IRQ);
1039		status = menelaus_add_irq_work(MENELAUS_RTCTMR_IRQ,
1040				menelaus_rtc_update_work);
1041		if (status == 0)
1042			the_menelaus->uie = 1;
1043		return status;
1044	case RTC_UIE_OFF:
1045		if (!the_menelaus->uie)
1046			return 0;
1047		status = menelaus_remove_irq_work(MENELAUS_RTCTMR_IRQ);
1048		if (status == 0)
1049			the_menelaus->uie = 0;
1050		return status;
1051	default:
1052		return -ENOIOCTLCMD;
1053	}
1054	return menelaus_write_reg(MENELAUS_RTC_CTRL, the_menelaus->rtc_control);
1055}
1056
1057#else
1058#define menelaus_ioctl	NULL
1059#endif
1060
1061/* REVISIT no compensation register support ... */
1062
1063static const struct rtc_class_ops menelaus_rtc_ops = {
1064	.ioctl			= menelaus_ioctl,
1065	.read_time		= menelaus_read_time,
1066	.set_time		= menelaus_set_time,
1067	.read_alarm		= menelaus_read_alarm,
1068	.set_alarm		= menelaus_set_alarm,
1069};
1070
1071static void menelaus_rtc_alarm_work(struct menelaus_chip *m)
1072{
1073	/* report alarm */
 
1074	rtc_update_irq(m->rtc, 1, RTC_IRQF | RTC_AF);
 
1075
1076	/* then disable it; alarms are oneshot */
1077	the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
1078	menelaus_write_reg(MENELAUS_RTC_CTRL, the_menelaus->rtc_control);
1079}
1080
1081static inline void menelaus_rtc_init(struct menelaus_chip *m)
1082{
1083	int	alarm = (m->client->irq > 0);
1084	int	err;
1085
1086	/* assume 32KDETEN pin is pulled high */
1087	if (!(menelaus_read_reg(MENELAUS_OSC_CTRL) & 0x80)) {
1088		dev_dbg(&m->client->dev, "no 32k oscillator\n");
1089		return;
1090	}
1091
1092	m->rtc = devm_rtc_allocate_device(&m->client->dev);
1093	if (IS_ERR(m->rtc))
1094		return;
1095
1096	m->rtc->ops = &menelaus_rtc_ops;
1097
1098	/* support RTC alarm; it can issue wakeups */
1099	if (alarm) {
1100		if (menelaus_add_irq_work(MENELAUS_RTCALM_IRQ,
1101				menelaus_rtc_alarm_work) < 0) {
1102			dev_err(&m->client->dev, "can't handle RTC alarm\n");
1103			return;
1104		}
1105		device_init_wakeup(&m->client->dev, 1);
1106	}
1107
1108	/* be sure RTC is enabled; allow 1/sec irqs; leave 12hr mode alone */
1109	m->rtc_control = menelaus_read_reg(MENELAUS_RTC_CTRL);
1110	if (!(m->rtc_control & RTC_CTRL_RTC_EN)
1111			|| (m->rtc_control & RTC_CTRL_AL_EN)
1112			|| (m->rtc_control & RTC_CTRL_EVERY_MASK)) {
1113		if (!(m->rtc_control & RTC_CTRL_RTC_EN)) {
1114			dev_warn(&m->client->dev, "rtc clock needs setting\n");
1115			m->rtc_control |= RTC_CTRL_RTC_EN;
1116		}
1117		m->rtc_control &= ~RTC_CTRL_EVERY_MASK;
1118		m->rtc_control &= ~RTC_CTRL_AL_EN;
1119		menelaus_write_reg(MENELAUS_RTC_CTRL, m->rtc_control);
1120	}
1121
1122	err = rtc_register_device(m->rtc);
1123	if (err) {
 
 
1124		if (alarm) {
1125			menelaus_remove_irq_work(MENELAUS_RTCALM_IRQ);
1126			device_init_wakeup(&m->client->dev, 0);
1127		}
 
 
1128		the_menelaus->rtc = NULL;
1129	}
1130}
1131
1132#else
1133
1134static inline void menelaus_rtc_init(struct menelaus_chip *m)
1135{
1136	/* nothing */
1137}
1138
1139#endif
1140
1141/*-----------------------------------------------------------------------*/
1142
1143static struct i2c_driver menelaus_i2c_driver;
1144
1145static int menelaus_probe(struct i2c_client *client,
1146			  const struct i2c_device_id *id)
1147{
1148	struct menelaus_chip	*menelaus;
1149	int			rev = 0;
1150	int			err = 0;
1151	struct menelaus_platform_data *menelaus_pdata =
1152					dev_get_platdata(&client->dev);
1153
1154	if (the_menelaus) {
1155		dev_dbg(&client->dev, "only one %s for now\n",
1156				DRIVER_NAME);
1157		return -ENODEV;
1158	}
1159
1160	menelaus = devm_kzalloc(&client->dev, sizeof(*menelaus), GFP_KERNEL);
1161	if (!menelaus)
1162		return -ENOMEM;
1163
1164	i2c_set_clientdata(client, menelaus);
1165
1166	the_menelaus = menelaus;
1167	menelaus->client = client;
1168
1169	/* If a true probe check the device */
1170	rev = menelaus_read_reg(MENELAUS_REV);
1171	if (rev < 0) {
1172		pr_err(DRIVER_NAME ": device not found");
1173		return -ENODEV;
1174	}
1175
1176	/* Ack and disable all Menelaus interrupts */
1177	menelaus_write_reg(MENELAUS_INT_ACK1, 0xff);
1178	menelaus_write_reg(MENELAUS_INT_ACK2, 0xff);
1179	menelaus_write_reg(MENELAUS_INT_MASK1, 0xff);
1180	menelaus_write_reg(MENELAUS_INT_MASK2, 0xff);
1181	menelaus->mask1 = 0xff;
1182	menelaus->mask2 = 0xff;
1183
1184	/* Set output buffer strengths */
1185	menelaus_write_reg(MENELAUS_MCT_CTRL1, 0x73);
1186
1187	if (client->irq > 0) {
1188		err = request_irq(client->irq, menelaus_irq, 0,
1189				  DRIVER_NAME, menelaus);
1190		if (err) {
1191			dev_dbg(&client->dev,  "can't get IRQ %d, err %d\n",
1192					client->irq, err);
1193			return err;
1194		}
1195	}
1196
1197	mutex_init(&menelaus->lock);
1198	INIT_WORK(&menelaus->work, menelaus_work);
1199
1200	pr_info("Menelaus rev %d.%d\n", rev >> 4, rev & 0x0f);
1201
1202	err = menelaus_read_reg(MENELAUS_VCORE_CTRL1);
1203	if (err < 0)
1204		goto fail;
1205	if (err & VCORE_CTRL1_HW_NSW)
1206		menelaus->vcore_hw_mode = 1;
1207	else
1208		menelaus->vcore_hw_mode = 0;
1209
1210	if (menelaus_pdata != NULL && menelaus_pdata->late_init != NULL) {
1211		err = menelaus_pdata->late_init(&client->dev);
1212		if (err < 0)
1213			goto fail;
1214	}
1215
1216	menelaus_rtc_init(menelaus);
1217
1218	return 0;
1219fail:
1220	free_irq(client->irq, menelaus);
1221	flush_work(&menelaus->work);
1222	return err;
1223}
1224
1225static int menelaus_remove(struct i2c_client *client)
1226{
1227	struct menelaus_chip	*menelaus = i2c_get_clientdata(client);
1228
1229	free_irq(client->irq, menelaus);
1230	flush_work(&menelaus->work);
1231	the_menelaus = NULL;
1232	return 0;
1233}
1234
1235static const struct i2c_device_id menelaus_id[] = {
1236	{ "menelaus", 0 },
1237	{ }
1238};
1239MODULE_DEVICE_TABLE(i2c, menelaus_id);
1240
1241static struct i2c_driver menelaus_i2c_driver = {
1242	.driver = {
1243		.name		= DRIVER_NAME,
1244	},
1245	.probe		= menelaus_probe,
1246	.remove		= menelaus_remove,
1247	.id_table	= menelaus_id,
1248};
1249
1250module_i2c_driver(menelaus_i2c_driver);
1251
1252MODULE_AUTHOR("Texas Instruments, Inc. (and others)");
1253MODULE_DESCRIPTION("I2C interface for Menelaus.");
1254MODULE_LICENSE("GPL");
v4.10.11
 
   1/*
   2 * Copyright (C) 2004 Texas Instruments, Inc.
   3 *
   4 * Some parts based tps65010.c:
   5 * Copyright (C) 2004 Texas Instruments and
   6 * Copyright (C) 2004-2005 David Brownell
   7 *
   8 * Some parts based on tlv320aic24.c:
   9 * Copyright (C) by Kai Svahn <kai.svahn@nokia.com>
  10 *
  11 * Changes for interrupt handling and clean-up by
  12 * Tony Lindgren <tony@atomide.com> and Imre Deak <imre.deak@nokia.com>
  13 * Cleanup and generalized support for voltage setting by
  14 * Juha Yrjola
  15 * Added support for controlling VCORE and regulator sleep states,
  16 * Amit Kucheria <amit.kucheria@nokia.com>
  17 * Copyright (C) 2005, 2006 Nokia Corporation
  18 *
  19 * This program is free software; you can redistribute it and/or modify
  20 * it under the terms of the GNU General Public License as published by
  21 * the Free Software Foundation; either version 2 of the License, or
  22 * (at your option) any later version.
  23 *
  24 * This program is distributed in the hope that it will be useful,
  25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27 * GNU General Public License for more details.
  28 *
  29 * You should have received a copy of the GNU General Public License
  30 * along with this program; if not, write to the Free Software
  31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  32 */
  33
  34#include <linux/module.h>
  35#include <linux/i2c.h>
  36#include <linux/interrupt.h>
  37#include <linux/sched.h>
  38#include <linux/mutex.h>
  39#include <linux/workqueue.h>
  40#include <linux/delay.h>
  41#include <linux/rtc.h>
  42#include <linux/bcd.h>
  43#include <linux/slab.h>
  44#include <linux/mfd/menelaus.h>
  45#include <linux/gpio.h>
  46
  47#include <asm/mach/irq.h>
  48
  49
  50#define DRIVER_NAME			"menelaus"
  51
  52#define MENELAUS_I2C_ADDRESS		0x72
  53
  54#define MENELAUS_REV			0x01
  55#define MENELAUS_VCORE_CTRL1		0x02
  56#define MENELAUS_VCORE_CTRL2		0x03
  57#define MENELAUS_VCORE_CTRL3		0x04
  58#define MENELAUS_VCORE_CTRL4		0x05
  59#define MENELAUS_VCORE_CTRL5		0x06
  60#define MENELAUS_DCDC_CTRL1		0x07
  61#define MENELAUS_DCDC_CTRL2		0x08
  62#define MENELAUS_DCDC_CTRL3		0x09
  63#define MENELAUS_LDO_CTRL1		0x0A
  64#define MENELAUS_LDO_CTRL2		0x0B
  65#define MENELAUS_LDO_CTRL3		0x0C
  66#define MENELAUS_LDO_CTRL4		0x0D
  67#define MENELAUS_LDO_CTRL5		0x0E
  68#define MENELAUS_LDO_CTRL6		0x0F
  69#define MENELAUS_LDO_CTRL7		0x10
  70#define MENELAUS_LDO_CTRL8		0x11
  71#define MENELAUS_SLEEP_CTRL1		0x12
  72#define MENELAUS_SLEEP_CTRL2		0x13
  73#define MENELAUS_DEVICE_OFF		0x14
  74#define MENELAUS_OSC_CTRL		0x15
  75#define MENELAUS_DETECT_CTRL		0x16
  76#define MENELAUS_INT_MASK1		0x17
  77#define MENELAUS_INT_MASK2		0x18
  78#define MENELAUS_INT_STATUS1		0x19
  79#define MENELAUS_INT_STATUS2		0x1A
  80#define MENELAUS_INT_ACK1		0x1B
  81#define MENELAUS_INT_ACK2		0x1C
  82#define MENELAUS_GPIO_CTRL		0x1D
  83#define MENELAUS_GPIO_IN		0x1E
  84#define MENELAUS_GPIO_OUT		0x1F
  85#define MENELAUS_BBSMS			0x20
  86#define MENELAUS_RTC_CTRL		0x21
  87#define MENELAUS_RTC_UPDATE		0x22
  88#define MENELAUS_RTC_SEC		0x23
  89#define MENELAUS_RTC_MIN		0x24
  90#define MENELAUS_RTC_HR			0x25
  91#define MENELAUS_RTC_DAY		0x26
  92#define MENELAUS_RTC_MON		0x27
  93#define MENELAUS_RTC_YR			0x28
  94#define MENELAUS_RTC_WKDAY		0x29
  95#define MENELAUS_RTC_AL_SEC		0x2A
  96#define MENELAUS_RTC_AL_MIN		0x2B
  97#define MENELAUS_RTC_AL_HR		0x2C
  98#define MENELAUS_RTC_AL_DAY		0x2D
  99#define MENELAUS_RTC_AL_MON		0x2E
 100#define MENELAUS_RTC_AL_YR		0x2F
 101#define MENELAUS_RTC_COMP_MSB		0x30
 102#define MENELAUS_RTC_COMP_LSB		0x31
 103#define MENELAUS_S1_PULL_EN		0x32
 104#define MENELAUS_S1_PULL_DIR		0x33
 105#define MENELAUS_S2_PULL_EN		0x34
 106#define MENELAUS_S2_PULL_DIR		0x35
 107#define MENELAUS_MCT_CTRL1		0x36
 108#define MENELAUS_MCT_CTRL2		0x37
 109#define MENELAUS_MCT_CTRL3		0x38
 110#define MENELAUS_MCT_PIN_ST		0x39
 111#define MENELAUS_DEBOUNCE1		0x3A
 112
 113#define IH_MENELAUS_IRQS		12
 114#define MENELAUS_MMC_S1CD_IRQ		0	/* MMC slot 1 card change */
 115#define MENELAUS_MMC_S2CD_IRQ		1	/* MMC slot 2 card change */
 116#define MENELAUS_MMC_S1D1_IRQ		2	/* MMC DAT1 low in slot 1 */
 117#define MENELAUS_MMC_S2D1_IRQ		3	/* MMC DAT1 low in slot 2 */
 118#define MENELAUS_LOWBAT_IRQ		4	/* Low battery */
 119#define MENELAUS_HOTDIE_IRQ		5	/* Hot die detect */
 120#define MENELAUS_UVLO_IRQ		6	/* UVLO detect */
 121#define MENELAUS_TSHUT_IRQ		7	/* Thermal shutdown */
 122#define MENELAUS_RTCTMR_IRQ		8	/* RTC timer */
 123#define MENELAUS_RTCALM_IRQ		9	/* RTC alarm */
 124#define MENELAUS_RTCERR_IRQ		10	/* RTC error */
 125#define MENELAUS_PSHBTN_IRQ		11	/* Push button */
 126#define MENELAUS_RESERVED12_IRQ		12	/* Reserved */
 127#define MENELAUS_RESERVED13_IRQ		13	/* Reserved */
 128#define MENELAUS_RESERVED14_IRQ		14	/* Reserved */
 129#define MENELAUS_RESERVED15_IRQ		15	/* Reserved */
 130
 131/* VCORE_CTRL1 register */
 132#define VCORE_CTRL1_BYP_COMP		(1 << 5)
 133#define VCORE_CTRL1_HW_NSW		(1 << 7)
 134
 135/* GPIO_CTRL register */
 136#define GPIO_CTRL_SLOTSELEN		(1 << 5)
 137#define GPIO_CTRL_SLPCTLEN		(1 << 6)
 138#define GPIO1_DIR_INPUT			(1 << 0)
 139#define GPIO2_DIR_INPUT			(1 << 1)
 140#define GPIO3_DIR_INPUT			(1 << 2)
 141
 142/* MCT_CTRL1 register */
 143#define MCT_CTRL1_S1_CMD_OD		(1 << 2)
 144#define MCT_CTRL1_S2_CMD_OD		(1 << 3)
 145
 146/* MCT_CTRL2 register */
 147#define MCT_CTRL2_VS2_SEL_D0		(1 << 0)
 148#define MCT_CTRL2_VS2_SEL_D1		(1 << 1)
 149#define MCT_CTRL2_S1CD_BUFEN		(1 << 4)
 150#define MCT_CTRL2_S2CD_BUFEN		(1 << 5)
 151#define MCT_CTRL2_S1CD_DBEN		(1 << 6)
 152#define MCT_CTRL2_S2CD_BEN		(1 << 7)
 153
 154/* MCT_CTRL3 register */
 155#define MCT_CTRL3_SLOT1_EN		(1 << 0)
 156#define MCT_CTRL3_SLOT2_EN		(1 << 1)
 157#define MCT_CTRL3_S1_AUTO_EN		(1 << 2)
 158#define MCT_CTRL3_S2_AUTO_EN		(1 << 3)
 159
 160/* MCT_PIN_ST register */
 161#define MCT_PIN_ST_S1_CD_ST		(1 << 0)
 162#define MCT_PIN_ST_S2_CD_ST		(1 << 1)
 163
 164static void menelaus_work(struct work_struct *_menelaus);
 165
 166struct menelaus_chip {
 167	struct mutex		lock;
 168	struct i2c_client	*client;
 169	struct work_struct	work;
 170#ifdef CONFIG_RTC_DRV_TWL92330
 171	struct rtc_device	*rtc;
 172	u8			rtc_control;
 173	unsigned		uie:1;
 174#endif
 175	unsigned		vcore_hw_mode:1;
 176	u8			mask1, mask2;
 177	void			(*handlers[16])(struct menelaus_chip *);
 178	void			(*mmc_callback)(void *data, u8 mask);
 179	void			*mmc_callback_data;
 180};
 181
 182static struct menelaus_chip *the_menelaus;
 183
 184static int menelaus_write_reg(int reg, u8 value)
 185{
 186	int val = i2c_smbus_write_byte_data(the_menelaus->client, reg, value);
 187
 188	if (val < 0) {
 189		pr_err(DRIVER_NAME ": write error");
 190		return val;
 191	}
 192
 193	return 0;
 194}
 195
 196static int menelaus_read_reg(int reg)
 197{
 198	int val = i2c_smbus_read_byte_data(the_menelaus->client, reg);
 199
 200	if (val < 0)
 201		pr_err(DRIVER_NAME ": read error");
 202
 203	return val;
 204}
 205
 206static int menelaus_enable_irq(int irq)
 207{
 208	if (irq > 7) {
 209		irq -= 8;
 210		the_menelaus->mask2 &= ~(1 << irq);
 211		return menelaus_write_reg(MENELAUS_INT_MASK2,
 212				the_menelaus->mask2);
 213	} else {
 214		the_menelaus->mask1 &= ~(1 << irq);
 215		return menelaus_write_reg(MENELAUS_INT_MASK1,
 216				the_menelaus->mask1);
 217	}
 218}
 219
 220static int menelaus_disable_irq(int irq)
 221{
 222	if (irq > 7) {
 223		irq -= 8;
 224		the_menelaus->mask2 |= (1 << irq);
 225		return menelaus_write_reg(MENELAUS_INT_MASK2,
 226				the_menelaus->mask2);
 227	} else {
 228		the_menelaus->mask1 |= (1 << irq);
 229		return menelaus_write_reg(MENELAUS_INT_MASK1,
 230				the_menelaus->mask1);
 231	}
 232}
 233
 234static int menelaus_ack_irq(int irq)
 235{
 236	if (irq > 7)
 237		return menelaus_write_reg(MENELAUS_INT_ACK2, 1 << (irq - 8));
 238	else
 239		return menelaus_write_reg(MENELAUS_INT_ACK1, 1 << irq);
 240}
 241
 242/* Adds a handler for an interrupt. Does not run in interrupt context */
 243static int menelaus_add_irq_work(int irq,
 244		void (*handler)(struct menelaus_chip *))
 245{
 246	int ret = 0;
 247
 248	mutex_lock(&the_menelaus->lock);
 249	the_menelaus->handlers[irq] = handler;
 250	ret = menelaus_enable_irq(irq);
 251	mutex_unlock(&the_menelaus->lock);
 252
 253	return ret;
 254}
 255
 256/* Removes handler for an interrupt */
 257static int menelaus_remove_irq_work(int irq)
 258{
 259	int ret = 0;
 260
 261	mutex_lock(&the_menelaus->lock);
 262	ret = menelaus_disable_irq(irq);
 263	the_menelaus->handlers[irq] = NULL;
 264	mutex_unlock(&the_menelaus->lock);
 265
 266	return ret;
 267}
 268
 269/*
 270 * Gets scheduled when a card detect interrupt happens. Note that in some cases
 271 * this line is wired to card cover switch rather than the card detect switch
 272 * in each slot. In this case the cards are not seen by menelaus.
 273 * FIXME: Add handling for D1 too
 274 */
 275static void menelaus_mmc_cd_work(struct menelaus_chip *menelaus_hw)
 276{
 277	int reg;
 278	unsigned char card_mask = 0;
 279
 280	reg = menelaus_read_reg(MENELAUS_MCT_PIN_ST);
 281	if (reg < 0)
 282		return;
 283
 284	if (!(reg & 0x1))
 285		card_mask |= MCT_PIN_ST_S1_CD_ST;
 286
 287	if (!(reg & 0x2))
 288		card_mask |= MCT_PIN_ST_S2_CD_ST;
 289
 290	if (menelaus_hw->mmc_callback)
 291		menelaus_hw->mmc_callback(menelaus_hw->mmc_callback_data,
 292					  card_mask);
 293}
 294
 295/*
 296 * Toggles the MMC slots between open-drain and push-pull mode.
 297 */
 298int menelaus_set_mmc_opendrain(int slot, int enable)
 299{
 300	int ret, val;
 301
 302	if (slot != 1 && slot != 2)
 303		return -EINVAL;
 304	mutex_lock(&the_menelaus->lock);
 305	ret = menelaus_read_reg(MENELAUS_MCT_CTRL1);
 306	if (ret < 0) {
 307		mutex_unlock(&the_menelaus->lock);
 308		return ret;
 309	}
 310	val = ret;
 311	if (slot == 1) {
 312		if (enable)
 313			val |= MCT_CTRL1_S1_CMD_OD;
 314		else
 315			val &= ~MCT_CTRL1_S1_CMD_OD;
 316	} else {
 317		if (enable)
 318			val |= MCT_CTRL1_S2_CMD_OD;
 319		else
 320			val &= ~MCT_CTRL1_S2_CMD_OD;
 321	}
 322	ret = menelaus_write_reg(MENELAUS_MCT_CTRL1, val);
 323	mutex_unlock(&the_menelaus->lock);
 324
 325	return ret;
 326}
 327EXPORT_SYMBOL(menelaus_set_mmc_opendrain);
 328
 329int menelaus_set_slot_sel(int enable)
 330{
 331	int ret;
 332
 333	mutex_lock(&the_menelaus->lock);
 334	ret = menelaus_read_reg(MENELAUS_GPIO_CTRL);
 335	if (ret < 0)
 336		goto out;
 337	ret |= GPIO2_DIR_INPUT;
 338	if (enable)
 339		ret |= GPIO_CTRL_SLOTSELEN;
 340	else
 341		ret &= ~GPIO_CTRL_SLOTSELEN;
 342	ret = menelaus_write_reg(MENELAUS_GPIO_CTRL, ret);
 343out:
 344	mutex_unlock(&the_menelaus->lock);
 345	return ret;
 346}
 347EXPORT_SYMBOL(menelaus_set_slot_sel);
 348
 349int menelaus_set_mmc_slot(int slot, int enable, int power, int cd_en)
 350{
 351	int ret, val;
 352
 353	if (slot != 1 && slot != 2)
 354		return -EINVAL;
 355	if (power >= 3)
 356		return -EINVAL;
 357
 358	mutex_lock(&the_menelaus->lock);
 359
 360	ret = menelaus_read_reg(MENELAUS_MCT_CTRL2);
 361	if (ret < 0)
 362		goto out;
 363	val = ret;
 364	if (slot == 1) {
 365		if (cd_en)
 366			val |= MCT_CTRL2_S1CD_BUFEN | MCT_CTRL2_S1CD_DBEN;
 367		else
 368			val &= ~(MCT_CTRL2_S1CD_BUFEN | MCT_CTRL2_S1CD_DBEN);
 369	} else {
 370		if (cd_en)
 371			val |= MCT_CTRL2_S2CD_BUFEN | MCT_CTRL2_S2CD_BEN;
 372		else
 373			val &= ~(MCT_CTRL2_S2CD_BUFEN | MCT_CTRL2_S2CD_BEN);
 374	}
 375	ret = menelaus_write_reg(MENELAUS_MCT_CTRL2, val);
 376	if (ret < 0)
 377		goto out;
 378
 379	ret = menelaus_read_reg(MENELAUS_MCT_CTRL3);
 380	if (ret < 0)
 381		goto out;
 382	val = ret;
 383	if (slot == 1) {
 384		if (enable)
 385			val |= MCT_CTRL3_SLOT1_EN;
 386		else
 387			val &= ~MCT_CTRL3_SLOT1_EN;
 388	} else {
 389		int b;
 390
 391		if (enable)
 392			val |= MCT_CTRL3_SLOT2_EN;
 393		else
 394			val &= ~MCT_CTRL3_SLOT2_EN;
 395		b = menelaus_read_reg(MENELAUS_MCT_CTRL2);
 396		b &= ~(MCT_CTRL2_VS2_SEL_D0 | MCT_CTRL2_VS2_SEL_D1);
 397		b |= power;
 398		ret = menelaus_write_reg(MENELAUS_MCT_CTRL2, b);
 399		if (ret < 0)
 400			goto out;
 401	}
 402	/* Disable autonomous shutdown */
 403	val &= ~(MCT_CTRL3_S1_AUTO_EN | MCT_CTRL3_S2_AUTO_EN);
 404	ret = menelaus_write_reg(MENELAUS_MCT_CTRL3, val);
 405out:
 406	mutex_unlock(&the_menelaus->lock);
 407	return ret;
 408}
 409EXPORT_SYMBOL(menelaus_set_mmc_slot);
 410
 411int menelaus_register_mmc_callback(void (*callback)(void *data, u8 card_mask),
 412				   void *data)
 413{
 414	int ret = 0;
 415
 416	the_menelaus->mmc_callback_data = data;
 417	the_menelaus->mmc_callback = callback;
 418	ret = menelaus_add_irq_work(MENELAUS_MMC_S1CD_IRQ,
 419				    menelaus_mmc_cd_work);
 420	if (ret < 0)
 421		return ret;
 422	ret = menelaus_add_irq_work(MENELAUS_MMC_S2CD_IRQ,
 423				    menelaus_mmc_cd_work);
 424	if (ret < 0)
 425		return ret;
 426	ret = menelaus_add_irq_work(MENELAUS_MMC_S1D1_IRQ,
 427				    menelaus_mmc_cd_work);
 428	if (ret < 0)
 429		return ret;
 430	ret = menelaus_add_irq_work(MENELAUS_MMC_S2D1_IRQ,
 431				    menelaus_mmc_cd_work);
 432
 433	return ret;
 434}
 435EXPORT_SYMBOL(menelaus_register_mmc_callback);
 436
 437void menelaus_unregister_mmc_callback(void)
 438{
 439	menelaus_remove_irq_work(MENELAUS_MMC_S1CD_IRQ);
 440	menelaus_remove_irq_work(MENELAUS_MMC_S2CD_IRQ);
 441	menelaus_remove_irq_work(MENELAUS_MMC_S1D1_IRQ);
 442	menelaus_remove_irq_work(MENELAUS_MMC_S2D1_IRQ);
 443
 444	the_menelaus->mmc_callback = NULL;
 445	the_menelaus->mmc_callback_data = NULL;
 446}
 447EXPORT_SYMBOL(menelaus_unregister_mmc_callback);
 448
 449struct menelaus_vtg {
 450	const char *name;
 451	u8 vtg_reg;
 452	u8 vtg_shift;
 453	u8 vtg_bits;
 454	u8 mode_reg;
 455};
 456
 457struct menelaus_vtg_value {
 458	u16 vtg;
 459	u16 val;
 460};
 461
 462static int menelaus_set_voltage(const struct menelaus_vtg *vtg, int mV,
 463				int vtg_val, int mode)
 464{
 465	int val, ret;
 466	struct i2c_client *c = the_menelaus->client;
 467
 468	mutex_lock(&the_menelaus->lock);
 469
 470	ret = menelaus_read_reg(vtg->vtg_reg);
 471	if (ret < 0)
 472		goto out;
 473	val = ret & ~(((1 << vtg->vtg_bits) - 1) << vtg->vtg_shift);
 474	val |= vtg_val << vtg->vtg_shift;
 475
 476	dev_dbg(&c->dev, "Setting voltage '%s'"
 477			 "to %d mV (reg 0x%02x, val 0x%02x)\n",
 478			vtg->name, mV, vtg->vtg_reg, val);
 479
 480	ret = menelaus_write_reg(vtg->vtg_reg, val);
 481	if (ret < 0)
 482		goto out;
 483	ret = menelaus_write_reg(vtg->mode_reg, mode);
 484out:
 485	mutex_unlock(&the_menelaus->lock);
 486	if (ret == 0) {
 487		/* Wait for voltage to stabilize */
 488		msleep(1);
 489	}
 490	return ret;
 491}
 492
 493static int menelaus_get_vtg_value(int vtg, const struct menelaus_vtg_value *tbl,
 494				  int n)
 495{
 496	int i;
 497
 498	for (i = 0; i < n; i++, tbl++)
 499		if (tbl->vtg == vtg)
 500			return tbl->val;
 501	return -EINVAL;
 502}
 503
 504/*
 505 * Vcore can be programmed in two ways:
 506 * SW-controlled: Required voltage is programmed into VCORE_CTRL1
 507 * HW-controlled: Required range (roof-floor) is programmed into VCORE_CTRL3
 508 * and VCORE_CTRL4
 509 *
 510 * Call correct 'set' function accordingly
 511 */
 512
 513static const struct menelaus_vtg_value vcore_values[] = {
 514	{ 1000, 0 },
 515	{ 1025, 1 },
 516	{ 1050, 2 },
 517	{ 1075, 3 },
 518	{ 1100, 4 },
 519	{ 1125, 5 },
 520	{ 1150, 6 },
 521	{ 1175, 7 },
 522	{ 1200, 8 },
 523	{ 1225, 9 },
 524	{ 1250, 10 },
 525	{ 1275, 11 },
 526	{ 1300, 12 },
 527	{ 1325, 13 },
 528	{ 1350, 14 },
 529	{ 1375, 15 },
 530	{ 1400, 16 },
 531	{ 1425, 17 },
 532	{ 1450, 18 },
 533};
 534
 535int menelaus_set_vcore_hw(unsigned int roof_mV, unsigned int floor_mV)
 536{
 537	int fval, rval, val, ret;
 538	struct i2c_client *c = the_menelaus->client;
 539
 540	rval = menelaus_get_vtg_value(roof_mV, vcore_values,
 541				      ARRAY_SIZE(vcore_values));
 542	if (rval < 0)
 543		return -EINVAL;
 544	fval = menelaus_get_vtg_value(floor_mV, vcore_values,
 545				      ARRAY_SIZE(vcore_values));
 546	if (fval < 0)
 547		return -EINVAL;
 548
 549	dev_dbg(&c->dev, "Setting VCORE FLOOR to %d mV and ROOF to %d mV\n",
 550	       floor_mV, roof_mV);
 551
 552	mutex_lock(&the_menelaus->lock);
 553	ret = menelaus_write_reg(MENELAUS_VCORE_CTRL3, fval);
 554	if (ret < 0)
 555		goto out;
 556	ret = menelaus_write_reg(MENELAUS_VCORE_CTRL4, rval);
 557	if (ret < 0)
 558		goto out;
 559	if (!the_menelaus->vcore_hw_mode) {
 560		val = menelaus_read_reg(MENELAUS_VCORE_CTRL1);
 561		/* HW mode, turn OFF byte comparator */
 562		val |= (VCORE_CTRL1_HW_NSW | VCORE_CTRL1_BYP_COMP);
 563		ret = menelaus_write_reg(MENELAUS_VCORE_CTRL1, val);
 564		the_menelaus->vcore_hw_mode = 1;
 565	}
 566	msleep(1);
 567out:
 568	mutex_unlock(&the_menelaus->lock);
 569	return ret;
 570}
 571
 572static const struct menelaus_vtg vmem_vtg = {
 573	.name = "VMEM",
 574	.vtg_reg = MENELAUS_LDO_CTRL1,
 575	.vtg_shift = 0,
 576	.vtg_bits = 2,
 577	.mode_reg = MENELAUS_LDO_CTRL3,
 578};
 579
 580static const struct menelaus_vtg_value vmem_values[] = {
 581	{ 1500, 0 },
 582	{ 1800, 1 },
 583	{ 1900, 2 },
 584	{ 2500, 3 },
 585};
 586
 587int menelaus_set_vmem(unsigned int mV)
 588{
 589	int val;
 590
 591	if (mV == 0)
 592		return menelaus_set_voltage(&vmem_vtg, 0, 0, 0);
 593
 594	val = menelaus_get_vtg_value(mV, vmem_values, ARRAY_SIZE(vmem_values));
 595	if (val < 0)
 596		return -EINVAL;
 597	return menelaus_set_voltage(&vmem_vtg, mV, val, 0x02);
 598}
 599EXPORT_SYMBOL(menelaus_set_vmem);
 600
 601static const struct menelaus_vtg vio_vtg = {
 602	.name = "VIO",
 603	.vtg_reg = MENELAUS_LDO_CTRL1,
 604	.vtg_shift = 2,
 605	.vtg_bits = 2,
 606	.mode_reg = MENELAUS_LDO_CTRL4,
 607};
 608
 609static const struct menelaus_vtg_value vio_values[] = {
 610	{ 1500, 0 },
 611	{ 1800, 1 },
 612	{ 2500, 2 },
 613	{ 2800, 3 },
 614};
 615
 616int menelaus_set_vio(unsigned int mV)
 617{
 618	int val;
 619
 620	if (mV == 0)
 621		return menelaus_set_voltage(&vio_vtg, 0, 0, 0);
 622
 623	val = menelaus_get_vtg_value(mV, vio_values, ARRAY_SIZE(vio_values));
 624	if (val < 0)
 625		return -EINVAL;
 626	return menelaus_set_voltage(&vio_vtg, mV, val, 0x02);
 627}
 628EXPORT_SYMBOL(menelaus_set_vio);
 629
 630static const struct menelaus_vtg_value vdcdc_values[] = {
 631	{ 1500, 0 },
 632	{ 1800, 1 },
 633	{ 2000, 2 },
 634	{ 2200, 3 },
 635	{ 2400, 4 },
 636	{ 2800, 5 },
 637	{ 3000, 6 },
 638	{ 3300, 7 },
 639};
 640
 641static const struct menelaus_vtg vdcdc2_vtg = {
 642	.name = "VDCDC2",
 643	.vtg_reg = MENELAUS_DCDC_CTRL1,
 644	.vtg_shift = 0,
 645	.vtg_bits = 3,
 646	.mode_reg = MENELAUS_DCDC_CTRL2,
 647};
 648
 649static const struct menelaus_vtg vdcdc3_vtg = {
 650	.name = "VDCDC3",
 651	.vtg_reg = MENELAUS_DCDC_CTRL1,
 652	.vtg_shift = 3,
 653	.vtg_bits = 3,
 654	.mode_reg = MENELAUS_DCDC_CTRL3,
 655};
 656
 657int menelaus_set_vdcdc(int dcdc, unsigned int mV)
 658{
 659	const struct menelaus_vtg *vtg;
 660	int val;
 661
 662	if (dcdc != 2 && dcdc != 3)
 663		return -EINVAL;
 664	if (dcdc == 2)
 665		vtg = &vdcdc2_vtg;
 666	else
 667		vtg = &vdcdc3_vtg;
 668
 669	if (mV == 0)
 670		return menelaus_set_voltage(vtg, 0, 0, 0);
 671
 672	val = menelaus_get_vtg_value(mV, vdcdc_values,
 673				     ARRAY_SIZE(vdcdc_values));
 674	if (val < 0)
 675		return -EINVAL;
 676	return menelaus_set_voltage(vtg, mV, val, 0x03);
 677}
 678
 679static const struct menelaus_vtg_value vmmc_values[] = {
 680	{ 1850, 0 },
 681	{ 2800, 1 },
 682	{ 3000, 2 },
 683	{ 3100, 3 },
 684};
 685
 686static const struct menelaus_vtg vmmc_vtg = {
 687	.name = "VMMC",
 688	.vtg_reg = MENELAUS_LDO_CTRL1,
 689	.vtg_shift = 6,
 690	.vtg_bits = 2,
 691	.mode_reg = MENELAUS_LDO_CTRL7,
 692};
 693
 694int menelaus_set_vmmc(unsigned int mV)
 695{
 696	int val;
 697
 698	if (mV == 0)
 699		return menelaus_set_voltage(&vmmc_vtg, 0, 0, 0);
 700
 701	val = menelaus_get_vtg_value(mV, vmmc_values, ARRAY_SIZE(vmmc_values));
 702	if (val < 0)
 703		return -EINVAL;
 704	return menelaus_set_voltage(&vmmc_vtg, mV, val, 0x02);
 705}
 706EXPORT_SYMBOL(menelaus_set_vmmc);
 707
 708
 709static const struct menelaus_vtg_value vaux_values[] = {
 710	{ 1500, 0 },
 711	{ 1800, 1 },
 712	{ 2500, 2 },
 713	{ 2800, 3 },
 714};
 715
 716static const struct menelaus_vtg vaux_vtg = {
 717	.name = "VAUX",
 718	.vtg_reg = MENELAUS_LDO_CTRL1,
 719	.vtg_shift = 4,
 720	.vtg_bits = 2,
 721	.mode_reg = MENELAUS_LDO_CTRL6,
 722};
 723
 724int menelaus_set_vaux(unsigned int mV)
 725{
 726	int val;
 727
 728	if (mV == 0)
 729		return menelaus_set_voltage(&vaux_vtg, 0, 0, 0);
 730
 731	val = menelaus_get_vtg_value(mV, vaux_values, ARRAY_SIZE(vaux_values));
 732	if (val < 0)
 733		return -EINVAL;
 734	return menelaus_set_voltage(&vaux_vtg, mV, val, 0x02);
 735}
 736EXPORT_SYMBOL(menelaus_set_vaux);
 737
 738int menelaus_get_slot_pin_states(void)
 739{
 740	return menelaus_read_reg(MENELAUS_MCT_PIN_ST);
 741}
 742EXPORT_SYMBOL(menelaus_get_slot_pin_states);
 743
 744int menelaus_set_regulator_sleep(int enable, u32 val)
 745{
 746	int t, ret;
 747	struct i2c_client *c = the_menelaus->client;
 748
 749	mutex_lock(&the_menelaus->lock);
 750	ret = menelaus_write_reg(MENELAUS_SLEEP_CTRL2, val);
 751	if (ret < 0)
 752		goto out;
 753
 754	dev_dbg(&c->dev, "regulator sleep configuration: %02x\n", val);
 755
 756	ret = menelaus_read_reg(MENELAUS_GPIO_CTRL);
 757	if (ret < 0)
 758		goto out;
 759	t = (GPIO_CTRL_SLPCTLEN | GPIO3_DIR_INPUT);
 760	if (enable)
 761		ret |= t;
 762	else
 763		ret &= ~t;
 764	ret = menelaus_write_reg(MENELAUS_GPIO_CTRL, ret);
 765out:
 766	mutex_unlock(&the_menelaus->lock);
 767	return ret;
 768}
 769
 770/*-----------------------------------------------------------------------*/
 771
 772/* Handles Menelaus interrupts. Does not run in interrupt context */
 773static void menelaus_work(struct work_struct *_menelaus)
 774{
 775	struct menelaus_chip *menelaus =
 776			container_of(_menelaus, struct menelaus_chip, work);
 777	void (*handler)(struct menelaus_chip *menelaus);
 778
 779	while (1) {
 780		unsigned isr;
 781
 782		isr = (menelaus_read_reg(MENELAUS_INT_STATUS2)
 783				& ~menelaus->mask2) << 8;
 784		isr |= menelaus_read_reg(MENELAUS_INT_STATUS1)
 785				& ~menelaus->mask1;
 786		if (!isr)
 787			break;
 788
 789		while (isr) {
 790			int irq = fls(isr) - 1;
 791			isr &= ~(1 << irq);
 792
 793			mutex_lock(&menelaus->lock);
 794			menelaus_disable_irq(irq);
 795			menelaus_ack_irq(irq);
 796			handler = menelaus->handlers[irq];
 797			if (handler)
 798				handler(menelaus);
 799			menelaus_enable_irq(irq);
 800			mutex_unlock(&menelaus->lock);
 801		}
 802	}
 803	enable_irq(menelaus->client->irq);
 804}
 805
 806/*
 807 * We cannot use I2C in interrupt context, so we just schedule work.
 808 */
 809static irqreturn_t menelaus_irq(int irq, void *_menelaus)
 810{
 811	struct menelaus_chip *menelaus = _menelaus;
 812
 813	disable_irq_nosync(irq);
 814	(void)schedule_work(&menelaus->work);
 815
 816	return IRQ_HANDLED;
 817}
 818
 819/*-----------------------------------------------------------------------*/
 820
 821/*
 822 * The RTC needs to be set once, then it runs on backup battery power.
 823 * It supports alarms, including system wake alarms (from some modes);
 824 * and 1/second IRQs if requested.
 825 */
 826#ifdef CONFIG_RTC_DRV_TWL92330
 827
 828#define RTC_CTRL_RTC_EN		(1 << 0)
 829#define RTC_CTRL_AL_EN		(1 << 1)
 830#define RTC_CTRL_MODE12		(1 << 2)
 831#define RTC_CTRL_EVERY_MASK	(3 << 3)
 832#define RTC_CTRL_EVERY_SEC	(0 << 3)
 833#define RTC_CTRL_EVERY_MIN	(1 << 3)
 834#define RTC_CTRL_EVERY_HR	(2 << 3)
 835#define RTC_CTRL_EVERY_DAY	(3 << 3)
 836
 837#define RTC_UPDATE_EVERY	0x08
 838
 839#define RTC_HR_PM		(1 << 7)
 840
 841static void menelaus_to_time(char *regs, struct rtc_time *t)
 842{
 843	t->tm_sec = bcd2bin(regs[0]);
 844	t->tm_min = bcd2bin(regs[1]);
 845	if (the_menelaus->rtc_control & RTC_CTRL_MODE12) {
 846		t->tm_hour = bcd2bin(regs[2] & 0x1f) - 1;
 847		if (regs[2] & RTC_HR_PM)
 848			t->tm_hour += 12;
 849	} else
 850		t->tm_hour = bcd2bin(regs[2] & 0x3f);
 851	t->tm_mday = bcd2bin(regs[3]);
 852	t->tm_mon = bcd2bin(regs[4]) - 1;
 853	t->tm_year = bcd2bin(regs[5]) + 100;
 854}
 855
 856static int time_to_menelaus(struct rtc_time *t, int regnum)
 857{
 858	int	hour, status;
 859
 860	status = menelaus_write_reg(regnum++, bin2bcd(t->tm_sec));
 861	if (status < 0)
 862		goto fail;
 863
 864	status = menelaus_write_reg(regnum++, bin2bcd(t->tm_min));
 865	if (status < 0)
 866		goto fail;
 867
 868	if (the_menelaus->rtc_control & RTC_CTRL_MODE12) {
 869		hour = t->tm_hour + 1;
 870		if (hour > 12)
 871			hour = RTC_HR_PM | bin2bcd(hour - 12);
 872		else
 873			hour = bin2bcd(hour);
 874	} else
 875		hour = bin2bcd(t->tm_hour);
 876	status = menelaus_write_reg(regnum++, hour);
 877	if (status < 0)
 878		goto fail;
 879
 880	status = menelaus_write_reg(regnum++, bin2bcd(t->tm_mday));
 881	if (status < 0)
 882		goto fail;
 883
 884	status = menelaus_write_reg(regnum++, bin2bcd(t->tm_mon + 1));
 885	if (status < 0)
 886		goto fail;
 887
 888	status = menelaus_write_reg(regnum++, bin2bcd(t->tm_year - 100));
 889	if (status < 0)
 890		goto fail;
 891
 892	return 0;
 893fail:
 894	dev_err(&the_menelaus->client->dev, "rtc write reg %02x, err %d\n",
 895			--regnum, status);
 896	return status;
 897}
 898
 899static int menelaus_read_time(struct device *dev, struct rtc_time *t)
 900{
 901	struct i2c_msg	msg[2];
 902	char		regs[7];
 903	int		status;
 904
 905	/* block read date and time registers */
 906	regs[0] = MENELAUS_RTC_SEC;
 907
 908	msg[0].addr = MENELAUS_I2C_ADDRESS;
 909	msg[0].flags = 0;
 910	msg[0].len = 1;
 911	msg[0].buf = regs;
 912
 913	msg[1].addr = MENELAUS_I2C_ADDRESS;
 914	msg[1].flags = I2C_M_RD;
 915	msg[1].len = sizeof(regs);
 916	msg[1].buf = regs;
 917
 918	status = i2c_transfer(the_menelaus->client->adapter, msg, 2);
 919	if (status != 2) {
 920		dev_err(dev, "%s error %d\n", "read", status);
 921		return -EIO;
 922	}
 923
 924	menelaus_to_time(regs, t);
 925	t->tm_wday = bcd2bin(regs[6]);
 926
 927	return 0;
 928}
 929
 930static int menelaus_set_time(struct device *dev, struct rtc_time *t)
 931{
 932	int		status;
 933
 934	/* write date and time registers */
 935	status = time_to_menelaus(t, MENELAUS_RTC_SEC);
 936	if (status < 0)
 937		return status;
 938	status = menelaus_write_reg(MENELAUS_RTC_WKDAY, bin2bcd(t->tm_wday));
 939	if (status < 0) {
 940		dev_err(&the_menelaus->client->dev, "rtc write reg %02x "
 941				"err %d\n", MENELAUS_RTC_WKDAY, status);
 942		return status;
 943	}
 944
 945	/* now commit the write */
 946	status = menelaus_write_reg(MENELAUS_RTC_UPDATE, RTC_UPDATE_EVERY);
 947	if (status < 0)
 948		dev_err(&the_menelaus->client->dev, "rtc commit time, err %d\n",
 949				status);
 950
 951	return 0;
 952}
 953
 954static int menelaus_read_alarm(struct device *dev, struct rtc_wkalrm *w)
 955{
 956	struct i2c_msg	msg[2];
 957	char		regs[6];
 958	int		status;
 959
 960	/* block read alarm registers */
 961	regs[0] = MENELAUS_RTC_AL_SEC;
 962
 963	msg[0].addr = MENELAUS_I2C_ADDRESS;
 964	msg[0].flags = 0;
 965	msg[0].len = 1;
 966	msg[0].buf = regs;
 967
 968	msg[1].addr = MENELAUS_I2C_ADDRESS;
 969	msg[1].flags = I2C_M_RD;
 970	msg[1].len = sizeof(regs);
 971	msg[1].buf = regs;
 972
 973	status = i2c_transfer(the_menelaus->client->adapter, msg, 2);
 974	if (status != 2) {
 975		dev_err(dev, "%s error %d\n", "alarm read", status);
 976		return -EIO;
 977	}
 978
 979	menelaus_to_time(regs, &w->time);
 980
 981	w->enabled = !!(the_menelaus->rtc_control & RTC_CTRL_AL_EN);
 982
 983	/* NOTE we *could* check if actually pending... */
 984	w->pending = 0;
 985
 986	return 0;
 987}
 988
 989static int menelaus_set_alarm(struct device *dev, struct rtc_wkalrm *w)
 990{
 991	int		status;
 992
 993	if (the_menelaus->client->irq <= 0 && w->enabled)
 994		return -ENODEV;
 995
 996	/* clear previous alarm enable */
 997	if (the_menelaus->rtc_control & RTC_CTRL_AL_EN) {
 998		the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
 999		status = menelaus_write_reg(MENELAUS_RTC_CTRL,
1000				the_menelaus->rtc_control);
1001		if (status < 0)
1002			return status;
1003	}
1004
1005	/* write alarm registers */
1006	status = time_to_menelaus(&w->time, MENELAUS_RTC_AL_SEC);
1007	if (status < 0)
1008		return status;
1009
1010	/* enable alarm if requested */
1011	if (w->enabled) {
1012		the_menelaus->rtc_control |= RTC_CTRL_AL_EN;
1013		status = menelaus_write_reg(MENELAUS_RTC_CTRL,
1014				the_menelaus->rtc_control);
1015	}
1016
1017	return status;
1018}
1019
1020#ifdef CONFIG_RTC_INTF_DEV
1021
1022static void menelaus_rtc_update_work(struct menelaus_chip *m)
1023{
1024	/* report 1/sec update */
1025	local_irq_disable();
1026	rtc_update_irq(m->rtc, 1, RTC_IRQF | RTC_UF);
1027	local_irq_enable();
1028}
1029
1030static int menelaus_ioctl(struct device *dev, unsigned cmd, unsigned long arg)
1031{
1032	int	status;
1033
1034	if (the_menelaus->client->irq <= 0)
1035		return -ENOIOCTLCMD;
1036
1037	switch (cmd) {
1038	/* alarm IRQ */
1039	case RTC_AIE_ON:
1040		if (the_menelaus->rtc_control & RTC_CTRL_AL_EN)
1041			return 0;
1042		the_menelaus->rtc_control |= RTC_CTRL_AL_EN;
1043		break;
1044	case RTC_AIE_OFF:
1045		if (!(the_menelaus->rtc_control & RTC_CTRL_AL_EN))
1046			return 0;
1047		the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
1048		break;
1049	/* 1/second "update" IRQ */
1050	case RTC_UIE_ON:
1051		if (the_menelaus->uie)
1052			return 0;
1053		status = menelaus_remove_irq_work(MENELAUS_RTCTMR_IRQ);
1054		status = menelaus_add_irq_work(MENELAUS_RTCTMR_IRQ,
1055				menelaus_rtc_update_work);
1056		if (status == 0)
1057			the_menelaus->uie = 1;
1058		return status;
1059	case RTC_UIE_OFF:
1060		if (!the_menelaus->uie)
1061			return 0;
1062		status = menelaus_remove_irq_work(MENELAUS_RTCTMR_IRQ);
1063		if (status == 0)
1064			the_menelaus->uie = 0;
1065		return status;
1066	default:
1067		return -ENOIOCTLCMD;
1068	}
1069	return menelaus_write_reg(MENELAUS_RTC_CTRL, the_menelaus->rtc_control);
1070}
1071
1072#else
1073#define menelaus_ioctl	NULL
1074#endif
1075
1076/* REVISIT no compensation register support ... */
1077
1078static const struct rtc_class_ops menelaus_rtc_ops = {
1079	.ioctl			= menelaus_ioctl,
1080	.read_time		= menelaus_read_time,
1081	.set_time		= menelaus_set_time,
1082	.read_alarm		= menelaus_read_alarm,
1083	.set_alarm		= menelaus_set_alarm,
1084};
1085
1086static void menelaus_rtc_alarm_work(struct menelaus_chip *m)
1087{
1088	/* report alarm */
1089	local_irq_disable();
1090	rtc_update_irq(m->rtc, 1, RTC_IRQF | RTC_AF);
1091	local_irq_enable();
1092
1093	/* then disable it; alarms are oneshot */
1094	the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN;
1095	menelaus_write_reg(MENELAUS_RTC_CTRL, the_menelaus->rtc_control);
1096}
1097
1098static inline void menelaus_rtc_init(struct menelaus_chip *m)
1099{
1100	int	alarm = (m->client->irq > 0);
 
1101
1102	/* assume 32KDETEN pin is pulled high */
1103	if (!(menelaus_read_reg(MENELAUS_OSC_CTRL) & 0x80)) {
1104		dev_dbg(&m->client->dev, "no 32k oscillator\n");
1105		return;
1106	}
1107
 
 
 
 
 
 
1108	/* support RTC alarm; it can issue wakeups */
1109	if (alarm) {
1110		if (menelaus_add_irq_work(MENELAUS_RTCALM_IRQ,
1111				menelaus_rtc_alarm_work) < 0) {
1112			dev_err(&m->client->dev, "can't handle RTC alarm\n");
1113			return;
1114		}
1115		device_init_wakeup(&m->client->dev, 1);
1116	}
1117
1118	/* be sure RTC is enabled; allow 1/sec irqs; leave 12hr mode alone */
1119	m->rtc_control = menelaus_read_reg(MENELAUS_RTC_CTRL);
1120	if (!(m->rtc_control & RTC_CTRL_RTC_EN)
1121			|| (m->rtc_control & RTC_CTRL_AL_EN)
1122			|| (m->rtc_control & RTC_CTRL_EVERY_MASK)) {
1123		if (!(m->rtc_control & RTC_CTRL_RTC_EN)) {
1124			dev_warn(&m->client->dev, "rtc clock needs setting\n");
1125			m->rtc_control |= RTC_CTRL_RTC_EN;
1126		}
1127		m->rtc_control &= ~RTC_CTRL_EVERY_MASK;
1128		m->rtc_control &= ~RTC_CTRL_AL_EN;
1129		menelaus_write_reg(MENELAUS_RTC_CTRL, m->rtc_control);
1130	}
1131
1132	m->rtc = rtc_device_register(DRIVER_NAME,
1133			&m->client->dev,
1134			&menelaus_rtc_ops, THIS_MODULE);
1135	if (IS_ERR(m->rtc)) {
1136		if (alarm) {
1137			menelaus_remove_irq_work(MENELAUS_RTCALM_IRQ);
1138			device_init_wakeup(&m->client->dev, 0);
1139		}
1140		dev_err(&m->client->dev, "can't register RTC: %d\n",
1141				(int) PTR_ERR(m->rtc));
1142		the_menelaus->rtc = NULL;
1143	}
1144}
1145
1146#else
1147
1148static inline void menelaus_rtc_init(struct menelaus_chip *m)
1149{
1150	/* nothing */
1151}
1152
1153#endif
1154
1155/*-----------------------------------------------------------------------*/
1156
1157static struct i2c_driver menelaus_i2c_driver;
1158
1159static int menelaus_probe(struct i2c_client *client,
1160			  const struct i2c_device_id *id)
1161{
1162	struct menelaus_chip	*menelaus;
1163	int			rev = 0;
1164	int			err = 0;
1165	struct menelaus_platform_data *menelaus_pdata =
1166					dev_get_platdata(&client->dev);
1167
1168	if (the_menelaus) {
1169		dev_dbg(&client->dev, "only one %s for now\n",
1170				DRIVER_NAME);
1171		return -ENODEV;
1172	}
1173
1174	menelaus = devm_kzalloc(&client->dev, sizeof(*menelaus), GFP_KERNEL);
1175	if (!menelaus)
1176		return -ENOMEM;
1177
1178	i2c_set_clientdata(client, menelaus);
1179
1180	the_menelaus = menelaus;
1181	menelaus->client = client;
1182
1183	/* If a true probe check the device */
1184	rev = menelaus_read_reg(MENELAUS_REV);
1185	if (rev < 0) {
1186		pr_err(DRIVER_NAME ": device not found");
1187		return -ENODEV;
1188	}
1189
1190	/* Ack and disable all Menelaus interrupts */
1191	menelaus_write_reg(MENELAUS_INT_ACK1, 0xff);
1192	menelaus_write_reg(MENELAUS_INT_ACK2, 0xff);
1193	menelaus_write_reg(MENELAUS_INT_MASK1, 0xff);
1194	menelaus_write_reg(MENELAUS_INT_MASK2, 0xff);
1195	menelaus->mask1 = 0xff;
1196	menelaus->mask2 = 0xff;
1197
1198	/* Set output buffer strengths */
1199	menelaus_write_reg(MENELAUS_MCT_CTRL1, 0x73);
1200
1201	if (client->irq > 0) {
1202		err = request_irq(client->irq, menelaus_irq, 0,
1203				  DRIVER_NAME, menelaus);
1204		if (err) {
1205			dev_dbg(&client->dev,  "can't get IRQ %d, err %d\n",
1206					client->irq, err);
1207			return err;
1208		}
1209	}
1210
1211	mutex_init(&menelaus->lock);
1212	INIT_WORK(&menelaus->work, menelaus_work);
1213
1214	pr_info("Menelaus rev %d.%d\n", rev >> 4, rev & 0x0f);
1215
1216	err = menelaus_read_reg(MENELAUS_VCORE_CTRL1);
1217	if (err < 0)
1218		goto fail;
1219	if (err & VCORE_CTRL1_HW_NSW)
1220		menelaus->vcore_hw_mode = 1;
1221	else
1222		menelaus->vcore_hw_mode = 0;
1223
1224	if (menelaus_pdata != NULL && menelaus_pdata->late_init != NULL) {
1225		err = menelaus_pdata->late_init(&client->dev);
1226		if (err < 0)
1227			goto fail;
1228	}
1229
1230	menelaus_rtc_init(menelaus);
1231
1232	return 0;
1233fail:
1234	free_irq(client->irq, menelaus);
1235	flush_work(&menelaus->work);
1236	return err;
1237}
1238
1239static int menelaus_remove(struct i2c_client *client)
1240{
1241	struct menelaus_chip	*menelaus = i2c_get_clientdata(client);
1242
1243	free_irq(client->irq, menelaus);
1244	flush_work(&menelaus->work);
1245	the_menelaus = NULL;
1246	return 0;
1247}
1248
1249static const struct i2c_device_id menelaus_id[] = {
1250	{ "menelaus", 0 },
1251	{ }
1252};
1253MODULE_DEVICE_TABLE(i2c, menelaus_id);
1254
1255static struct i2c_driver menelaus_i2c_driver = {
1256	.driver = {
1257		.name		= DRIVER_NAME,
1258	},
1259	.probe		= menelaus_probe,
1260	.remove		= menelaus_remove,
1261	.id_table	= menelaus_id,
1262};
1263
1264module_i2c_driver(menelaus_i2c_driver);
1265
1266MODULE_AUTHOR("Texas Instruments, Inc. (and others)");
1267MODULE_DESCRIPTION("I2C interface for Menelaus.");
1268MODULE_LICENSE("GPL");