12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268 |
- /*
- * Copyright (C) 2004 Texas Instruments, Inc.
- *
- * Some parts based tps65010.c:
- * Copyright (C) 2004 Texas Instruments and
- * Copyright (C) 2004-2005 David Brownell
- *
- * Some parts based on tlv320aic24.c:
- * Copyright (C) by Kai Svahn <kai.svahn@nokia.com>
- *
- * Changes for interrupt handling and clean-up by
- * Tony Lindgren <tony@atomide.com> and Imre Deak <imre.deak@nokia.com>
- * Cleanup and generalized support for voltage setting by
- * Juha Yrjola
- * Added support for controlling VCORE and regulator sleep states,
- * Amit Kucheria <amit.kucheria@nokia.com>
- * Copyright (C) 2005, 2006 Nokia Corporation
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
- #include <linux/module.h>
- #include <linux/i2c.h>
- #include <linux/interrupt.h>
- #include <linux/sched.h>
- #include <linux/mutex.h>
- #include <linux/workqueue.h>
- #include <linux/delay.h>
- #include <linux/rtc.h>
- #include <linux/bcd.h>
- #include <linux/slab.h>
- #include <linux/mfd/menelaus.h>
- #include <asm/mach/irq.h>
- #include <asm/gpio.h>
- #define DRIVER_NAME "menelaus"
- #define MENELAUS_I2C_ADDRESS 0x72
- #define MENELAUS_REV 0x01
- #define MENELAUS_VCORE_CTRL1 0x02
- #define MENELAUS_VCORE_CTRL2 0x03
- #define MENELAUS_VCORE_CTRL3 0x04
- #define MENELAUS_VCORE_CTRL4 0x05
- #define MENELAUS_VCORE_CTRL5 0x06
- #define MENELAUS_DCDC_CTRL1 0x07
- #define MENELAUS_DCDC_CTRL2 0x08
- #define MENELAUS_DCDC_CTRL3 0x09
- #define MENELAUS_LDO_CTRL1 0x0A
- #define MENELAUS_LDO_CTRL2 0x0B
- #define MENELAUS_LDO_CTRL3 0x0C
- #define MENELAUS_LDO_CTRL4 0x0D
- #define MENELAUS_LDO_CTRL5 0x0E
- #define MENELAUS_LDO_CTRL6 0x0F
- #define MENELAUS_LDO_CTRL7 0x10
- #define MENELAUS_LDO_CTRL8 0x11
- #define MENELAUS_SLEEP_CTRL1 0x12
- #define MENELAUS_SLEEP_CTRL2 0x13
- #define MENELAUS_DEVICE_OFF 0x14
- #define MENELAUS_OSC_CTRL 0x15
- #define MENELAUS_DETECT_CTRL 0x16
- #define MENELAUS_INT_MASK1 0x17
- #define MENELAUS_INT_MASK2 0x18
- #define MENELAUS_INT_STATUS1 0x19
- #define MENELAUS_INT_STATUS2 0x1A
- #define MENELAUS_INT_ACK1 0x1B
- #define MENELAUS_INT_ACK2 0x1C
- #define MENELAUS_GPIO_CTRL 0x1D
- #define MENELAUS_GPIO_IN 0x1E
- #define MENELAUS_GPIO_OUT 0x1F
- #define MENELAUS_BBSMS 0x20
- #define MENELAUS_RTC_CTRL 0x21
- #define MENELAUS_RTC_UPDATE 0x22
- #define MENELAUS_RTC_SEC 0x23
- #define MENELAUS_RTC_MIN 0x24
- #define MENELAUS_RTC_HR 0x25
- #define MENELAUS_RTC_DAY 0x26
- #define MENELAUS_RTC_MON 0x27
- #define MENELAUS_RTC_YR 0x28
- #define MENELAUS_RTC_WKDAY 0x29
- #define MENELAUS_RTC_AL_SEC 0x2A
- #define MENELAUS_RTC_AL_MIN 0x2B
- #define MENELAUS_RTC_AL_HR 0x2C
- #define MENELAUS_RTC_AL_DAY 0x2D
- #define MENELAUS_RTC_AL_MON 0x2E
- #define MENELAUS_RTC_AL_YR 0x2F
- #define MENELAUS_RTC_COMP_MSB 0x30
- #define MENELAUS_RTC_COMP_LSB 0x31
- #define MENELAUS_S1_PULL_EN 0x32
- #define MENELAUS_S1_PULL_DIR 0x33
- #define MENELAUS_S2_PULL_EN 0x34
- #define MENELAUS_S2_PULL_DIR 0x35
- #define MENELAUS_MCT_CTRL1 0x36
- #define MENELAUS_MCT_CTRL2 0x37
- #define MENELAUS_MCT_CTRL3 0x38
- #define MENELAUS_MCT_PIN_ST 0x39
- #define MENELAUS_DEBOUNCE1 0x3A
- #define IH_MENELAUS_IRQS 12
- #define MENELAUS_MMC_S1CD_IRQ 0 /* MMC slot 1 card change */
- #define MENELAUS_MMC_S2CD_IRQ 1 /* MMC slot 2 card change */
- #define MENELAUS_MMC_S1D1_IRQ 2 /* MMC DAT1 low in slot 1 */
- #define MENELAUS_MMC_S2D1_IRQ 3 /* MMC DAT1 low in slot 2 */
- #define MENELAUS_LOWBAT_IRQ 4 /* Low battery */
- #define MENELAUS_HOTDIE_IRQ 5 /* Hot die detect */
- #define MENELAUS_UVLO_IRQ 6 /* UVLO detect */
- #define MENELAUS_TSHUT_IRQ 7 /* Thermal shutdown */
- #define MENELAUS_RTCTMR_IRQ 8 /* RTC timer */
- #define MENELAUS_RTCALM_IRQ 9 /* RTC alarm */
- #define MENELAUS_RTCERR_IRQ 10 /* RTC error */
- #define MENELAUS_PSHBTN_IRQ 11 /* Push button */
- #define MENELAUS_RESERVED12_IRQ 12 /* Reserved */
- #define MENELAUS_RESERVED13_IRQ 13 /* Reserved */
- #define MENELAUS_RESERVED14_IRQ 14 /* Reserved */
- #define MENELAUS_RESERVED15_IRQ 15 /* Reserved */
- /* VCORE_CTRL1 register */
- #define VCORE_CTRL1_BYP_COMP (1 << 5)
- #define VCORE_CTRL1_HW_NSW (1 << 7)
- /* GPIO_CTRL register */
- #define GPIO_CTRL_SLOTSELEN (1 << 5)
- #define GPIO_CTRL_SLPCTLEN (1 << 6)
- #define GPIO1_DIR_INPUT (1 << 0)
- #define GPIO2_DIR_INPUT (1 << 1)
- #define GPIO3_DIR_INPUT (1 << 2)
- /* MCT_CTRL1 register */
- #define MCT_CTRL1_S1_CMD_OD (1 << 2)
- #define MCT_CTRL1_S2_CMD_OD (1 << 3)
- /* MCT_CTRL2 register */
- #define MCT_CTRL2_VS2_SEL_D0 (1 << 0)
- #define MCT_CTRL2_VS2_SEL_D1 (1 << 1)
- #define MCT_CTRL2_S1CD_BUFEN (1 << 4)
- #define MCT_CTRL2_S2CD_BUFEN (1 << 5)
- #define MCT_CTRL2_S1CD_DBEN (1 << 6)
- #define MCT_CTRL2_S2CD_BEN (1 << 7)
- /* MCT_CTRL3 register */
- #define MCT_CTRL3_SLOT1_EN (1 << 0)
- #define MCT_CTRL3_SLOT2_EN (1 << 1)
- #define MCT_CTRL3_S1_AUTO_EN (1 << 2)
- #define MCT_CTRL3_S2_AUTO_EN (1 << 3)
- /* MCT_PIN_ST register */
- #define MCT_PIN_ST_S1_CD_ST (1 << 0)
- #define MCT_PIN_ST_S2_CD_ST (1 << 1)
- static void menelaus_work(struct work_struct *_menelaus);
- struct menelaus_chip {
- struct mutex lock;
- struct i2c_client *client;
- struct work_struct work;
- #ifdef CONFIG_RTC_DRV_TWL92330
- struct rtc_device *rtc;
- u8 rtc_control;
- unsigned uie:1;
- #endif
- unsigned vcore_hw_mode:1;
- u8 mask1, mask2;
- void (*handlers[16])(struct menelaus_chip *);
- void (*mmc_callback)(void *data, u8 mask);
- void *mmc_callback_data;
- };
- static struct menelaus_chip *the_menelaus;
- static int menelaus_write_reg(int reg, u8 value)
- {
- int val = i2c_smbus_write_byte_data(the_menelaus->client, reg, value);
- if (val < 0) {
- pr_err(DRIVER_NAME ": write error");
- return val;
- }
- return 0;
- }
- static int menelaus_read_reg(int reg)
- {
- int val = i2c_smbus_read_byte_data(the_menelaus->client, reg);
- if (val < 0)
- pr_err(DRIVER_NAME ": read error");
- return val;
- }
- 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);
- }
- }
- static int menelaus_disable_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);
- }
- }
- 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);
- }
- /* Adds a handler for an interrupt. Does not run in interrupt context */
- static int menelaus_add_irq_work(int irq,
- void (*handler)(struct menelaus_chip *))
- {
- int ret = 0;
- mutex_lock(&the_menelaus->lock);
- the_menelaus->handlers[irq] = handler;
- ret = menelaus_enable_irq(irq);
- mutex_unlock(&the_menelaus->lock);
- return ret;
- }
- /* Removes handler for an interrupt */
- static int menelaus_remove_irq_work(int irq)
- {
- int ret = 0;
- mutex_lock(&the_menelaus->lock);
- ret = menelaus_disable_irq(irq);
- the_menelaus->handlers[irq] = NULL;
- mutex_unlock(&the_menelaus->lock);
- return ret;
- }
- /*
- * Gets scheduled when a card detect interrupt happens. Note that in some cases
- * this line is wired to card cover switch rather than the card detect switch
- * in each slot. In this case the cards are not seen by menelaus.
- * FIXME: Add handling for D1 too
- */
- static void menelaus_mmc_cd_work(struct menelaus_chip *menelaus_hw)
- {
- int reg;
- unsigned char card_mask = 0;
- reg = menelaus_read_reg(MENELAUS_MCT_PIN_ST);
- if (reg < 0)
- return;
- if (!(reg & 0x1))
- card_mask |= MCT_PIN_ST_S1_CD_ST;
- if (!(reg & 0x2))
- card_mask |= MCT_PIN_ST_S2_CD_ST;
- if (menelaus_hw->mmc_callback)
- menelaus_hw->mmc_callback(menelaus_hw->mmc_callback_data,
- card_mask);
- }
- /*
- * 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;
- }
- EXPORT_SYMBOL(menelaus_set_mmc_opendrain);
- 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;
- }
- EXPORT_SYMBOL(menelaus_set_slot_sel);
- 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;
- }
- EXPORT_SYMBOL(menelaus_set_mmc_slot);
- int menelaus_register_mmc_callback(void (*callback)(void *data, u8 card_mask),
- void *data)
- {
- int ret = 0;
- the_menelaus->mmc_callback_data = data;
- the_menelaus->mmc_callback = callback;
- ret = menelaus_add_irq_work(MENELAUS_MMC_S1CD_IRQ,
- menelaus_mmc_cd_work);
- if (ret < 0)
- return ret;
- ret = menelaus_add_irq_work(MENELAUS_MMC_S2CD_IRQ,
- menelaus_mmc_cd_work);
- if (ret < 0)
- return ret;
- ret = menelaus_add_irq_work(MENELAUS_MMC_S1D1_IRQ,
- menelaus_mmc_cd_work);
- if (ret < 0)
- return ret;
- ret = menelaus_add_irq_work(MENELAUS_MMC_S2D1_IRQ,
- menelaus_mmc_cd_work);
- return ret;
- }
- EXPORT_SYMBOL(menelaus_register_mmc_callback);
- void menelaus_unregister_mmc_callback(void)
- {
- menelaus_remove_irq_work(MENELAUS_MMC_S1CD_IRQ);
- menelaus_remove_irq_work(MENELAUS_MMC_S2CD_IRQ);
- menelaus_remove_irq_work(MENELAUS_MMC_S1D1_IRQ);
- menelaus_remove_irq_work(MENELAUS_MMC_S2D1_IRQ);
- the_menelaus->mmc_callback = NULL;
- the_menelaus->mmc_callback_data = NULL;
- }
- EXPORT_SYMBOL(menelaus_unregister_mmc_callback);
- struct menelaus_vtg {
- const char *name;
- u8 vtg_reg;
- u8 vtg_shift;
- u8 vtg_bits;
- u8 mode_reg;
- };
- struct menelaus_vtg_value {
- u16 vtg;
- u16 val;
- };
- 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);
- 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;
- 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;
- }
- static int menelaus_get_vtg_value(int vtg, const struct menelaus_vtg_value *tbl,
- int n)
- {
- int i;
- for (i = 0; i < n; i++, tbl++)
- if (tbl->vtg == vtg)
- return tbl->val;
- return -EINVAL;
- }
- /*
- * Vcore can be programmed in two ways:
- * SW-controlled: Required voltage is programmed into VCORE_CTRL1
- * HW-controlled: Required range (roof-floor) is programmed into VCORE_CTRL3
- * and VCORE_CTRL4
- *
- * Call correct 'set' function accordingly
- */
- static const struct menelaus_vtg_value vcore_values[] = {
- { 1000, 0 },
- { 1025, 1 },
- { 1050, 2 },
- { 1075, 3 },
- { 1100, 4 },
- { 1125, 5 },
- { 1150, 6 },
- { 1175, 7 },
- { 1200, 8 },
- { 1225, 9 },
- { 1250, 10 },
- { 1275, 11 },
- { 1300, 12 },
- { 1325, 13 },
- { 1350, 14 },
- { 1375, 15 },
- { 1400, 16 },
- { 1425, 17 },
- { 1450, 18 },
- };
- 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;
- }
- static const struct menelaus_vtg vmem_vtg = {
- .name = "VMEM",
- .vtg_reg = MENELAUS_LDO_CTRL1,
- .vtg_shift = 0,
- .vtg_bits = 2,
- .mode_reg = MENELAUS_LDO_CTRL3,
- };
- static const struct menelaus_vtg_value vmem_values[] = {
- { 1500, 0 },
- { 1800, 1 },
- { 1900, 2 },
- { 2500, 3 },
- };
- int menelaus_set_vmem(unsigned int mV)
- {
- int val;
- if (mV == 0)
- return menelaus_set_voltage(&vmem_vtg, 0, 0, 0);
- val = menelaus_get_vtg_value(mV, vmem_values, ARRAY_SIZE(vmem_values));
- if (val < 0)
- return -EINVAL;
- return menelaus_set_voltage(&vmem_vtg, mV, val, 0x02);
- }
- EXPORT_SYMBOL(menelaus_set_vmem);
- static const struct menelaus_vtg vio_vtg = {
- .name = "VIO",
- .vtg_reg = MENELAUS_LDO_CTRL1,
- .vtg_shift = 2,
- .vtg_bits = 2,
- .mode_reg = MENELAUS_LDO_CTRL4,
- };
- static const struct menelaus_vtg_value vio_values[] = {
- { 1500, 0 },
- { 1800, 1 },
- { 2500, 2 },
- { 2800, 3 },
- };
- int menelaus_set_vio(unsigned int mV)
- {
- int val;
- if (mV == 0)
- return menelaus_set_voltage(&vio_vtg, 0, 0, 0);
- val = menelaus_get_vtg_value(mV, vio_values, ARRAY_SIZE(vio_values));
- if (val < 0)
- return -EINVAL;
- return menelaus_set_voltage(&vio_vtg, mV, val, 0x02);
- }
- EXPORT_SYMBOL(menelaus_set_vio);
- static const struct menelaus_vtg_value vdcdc_values[] = {
- { 1500, 0 },
- { 1800, 1 },
- { 2000, 2 },
- { 2200, 3 },
- { 2400, 4 },
- { 2800, 5 },
- { 3000, 6 },
- { 3300, 7 },
- };
- static const struct menelaus_vtg vdcdc2_vtg = {
- .name = "VDCDC2",
- .vtg_reg = MENELAUS_DCDC_CTRL1,
- .vtg_shift = 0,
- .vtg_bits = 3,
- .mode_reg = MENELAUS_DCDC_CTRL2,
- };
- static const struct menelaus_vtg vdcdc3_vtg = {
- .name = "VDCDC3",
- .vtg_reg = MENELAUS_DCDC_CTRL1,
- .vtg_shift = 3,
- .vtg_bits = 3,
- .mode_reg = MENELAUS_DCDC_CTRL3,
- };
- int menelaus_set_vdcdc(int dcdc, unsigned int mV)
- {
- const struct menelaus_vtg *vtg;
- int val;
- if (dcdc != 2 && dcdc != 3)
- return -EINVAL;
- if (dcdc == 2)
- vtg = &vdcdc2_vtg;
- else
- vtg = &vdcdc3_vtg;
- if (mV == 0)
- return menelaus_set_voltage(vtg, 0, 0, 0);
- val = menelaus_get_vtg_value(mV, vdcdc_values,
- ARRAY_SIZE(vdcdc_values));
- if (val < 0)
- return -EINVAL;
- return menelaus_set_voltage(vtg, mV, val, 0x03);
- }
- static const struct menelaus_vtg_value vmmc_values[] = {
- { 1850, 0 },
- { 2800, 1 },
- { 3000, 2 },
- { 3100, 3 },
- };
- static const struct menelaus_vtg vmmc_vtg = {
- .name = "VMMC",
- .vtg_reg = MENELAUS_LDO_CTRL1,
- .vtg_shift = 6,
- .vtg_bits = 2,
- .mode_reg = MENELAUS_LDO_CTRL7,
- };
- int menelaus_set_vmmc(unsigned int mV)
- {
- int val;
- if (mV == 0)
- return menelaus_set_voltage(&vmmc_vtg, 0, 0, 0);
- val = menelaus_get_vtg_value(mV, vmmc_values, ARRAY_SIZE(vmmc_values));
- if (val < 0)
- return -EINVAL;
- return menelaus_set_voltage(&vmmc_vtg, mV, val, 0x02);
- }
- EXPORT_SYMBOL(menelaus_set_vmmc);
- static const struct menelaus_vtg_value vaux_values[] = {
- { 1500, 0 },
- { 1800, 1 },
- { 2500, 2 },
- { 2800, 3 },
- };
- static const struct menelaus_vtg vaux_vtg = {
- .name = "VAUX",
- .vtg_reg = MENELAUS_LDO_CTRL1,
- .vtg_shift = 4,
- .vtg_bits = 2,
- .mode_reg = MENELAUS_LDO_CTRL6,
- };
- int menelaus_set_vaux(unsigned int mV)
- {
- int val;
- if (mV == 0)
- return menelaus_set_voltage(&vaux_vtg, 0, 0, 0);
- val = menelaus_get_vtg_value(mV, vaux_values, ARRAY_SIZE(vaux_values));
- if (val < 0)
- return -EINVAL;
- return menelaus_set_voltage(&vaux_vtg, mV, val, 0x02);
- }
- EXPORT_SYMBOL(menelaus_set_vaux);
- int menelaus_get_slot_pin_states(void)
- {
- return menelaus_read_reg(MENELAUS_MCT_PIN_ST);
- }
- EXPORT_SYMBOL(menelaus_get_slot_pin_states);
- 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;
- }
- /*-----------------------------------------------------------------------*/
- /* Handles Menelaus interrupts. Does not run in interrupt context */
- static void menelaus_work(struct work_struct *_menelaus)
- {
- struct menelaus_chip *menelaus =
- container_of(_menelaus, struct menelaus_chip, work);
- void (*handler)(struct menelaus_chip *menelaus);
- while (1) {
- unsigned isr;
- isr = (menelaus_read_reg(MENELAUS_INT_STATUS2)
- & ~menelaus->mask2) << 8;
- isr |= menelaus_read_reg(MENELAUS_INT_STATUS1)
- & ~menelaus->mask1;
- if (!isr)
- break;
- while (isr) {
- int irq = fls(isr) - 1;
- isr &= ~(1 << irq);
- mutex_lock(&menelaus->lock);
- menelaus_disable_irq(irq);
- menelaus_ack_irq(irq);
- handler = menelaus->handlers[irq];
- if (handler)
- handler(menelaus);
- menelaus_enable_irq(irq);
- mutex_unlock(&menelaus->lock);
- }
- }
- enable_irq(menelaus->client->irq);
- }
- /*
- * We cannot use I2C in interrupt context, so we just schedule work.
- */
- static irqreturn_t menelaus_irq(int irq, void *_menelaus)
- {
- struct menelaus_chip *menelaus = _menelaus;
- disable_irq_nosync(irq);
- (void)schedule_work(&menelaus->work);
- return IRQ_HANDLED;
- }
- /*-----------------------------------------------------------------------*/
- /*
- * The RTC needs to be set once, then it runs on backup battery power.
- * It supports alarms, including system wake alarms (from some modes);
- * and 1/second IRQs if requested.
- */
- #ifdef CONFIG_RTC_DRV_TWL92330
- #define RTC_CTRL_RTC_EN (1 << 0)
- #define RTC_CTRL_AL_EN (1 << 1)
- #define RTC_CTRL_MODE12 (1 << 2)
- #define RTC_CTRL_EVERY_MASK (3 << 3)
- #define RTC_CTRL_EVERY_SEC (0 << 3)
- #define RTC_CTRL_EVERY_MIN (1 << 3)
- #define RTC_CTRL_EVERY_HR (2 << 3)
- #define RTC_CTRL_EVERY_DAY (3 << 3)
- #define RTC_UPDATE_EVERY 0x08
- #define RTC_HR_PM (1 << 7)
- static void menelaus_to_time(char *regs, struct rtc_time *t)
- {
- t->tm_sec = bcd2bin(regs[0]);
- t->tm_min = bcd2bin(regs[1]);
- if (the_menelaus->rtc_control & RTC_CTRL_MODE12) {
- t->tm_hour = bcd2bin(regs[2] & 0x1f) - 1;
- if (regs[2] & RTC_HR_PM)
- t->tm_hour += 12;
- } else
- t->tm_hour = bcd2bin(regs[2] & 0x3f);
- t->tm_mday = bcd2bin(regs[3]);
- t->tm_mon = bcd2bin(regs[4]) - 1;
- t->tm_year = bcd2bin(regs[5]) + 100;
- }
- 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;
- }
- static int menelaus_read_time(struct device *dev, struct rtc_time *t)
- {
- struct i2c_msg msg[2];
- char regs[7];
- int status;
- /* block read date and time registers */
- regs[0] = MENELAUS_RTC_SEC;
- msg[0].addr = MENELAUS_I2C_ADDRESS;
- msg[0].flags = 0;
- msg[0].len = 1;
- msg[0].buf = regs;
- msg[1].addr = MENELAUS_I2C_ADDRESS;
- msg[1].flags = I2C_M_RD;
- msg[1].len = sizeof(regs);
- msg[1].buf = regs;
- status = i2c_transfer(the_menelaus->client->adapter, msg, 2);
- if (status != 2) {
- dev_err(dev, "%s error %d\n", "read", status);
- return -EIO;
- }
- menelaus_to_time(regs, t);
- t->tm_wday = bcd2bin(regs[6]);
- return 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;
- }
- static int menelaus_read_alarm(struct device *dev, struct rtc_wkalrm *w)
- {
- struct i2c_msg msg[2];
- char regs[6];
- int status;
- /* block read alarm registers */
- regs[0] = MENELAUS_RTC_AL_SEC;
- msg[0].addr = MENELAUS_I2C_ADDRESS;
- msg[0].flags = 0;
- msg[0].len = 1;
- msg[0].buf = regs;
- msg[1].addr = MENELAUS_I2C_ADDRESS;
- msg[1].flags = I2C_M_RD;
- msg[1].len = sizeof(regs);
- msg[1].buf = regs;
- status = i2c_transfer(the_menelaus->client->adapter, msg, 2);
- if (status != 2) {
- dev_err(dev, "%s error %d\n", "alarm read", status);
- return -EIO;
- }
- menelaus_to_time(regs, &w->time);
- w->enabled = !!(the_menelaus->rtc_control & RTC_CTRL_AL_EN);
- /* NOTE we *could* check if actually pending... */
- w->pending = 0;
- return 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;
- }
- #ifdef CONFIG_RTC_INTF_DEV
- static void menelaus_rtc_update_work(struct menelaus_chip *m)
- {
- /* report 1/sec update */
- local_irq_disable();
- rtc_update_irq(m->rtc, 1, RTC_IRQF | RTC_UF);
- local_irq_enable();
- }
- 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);
- }
- #else
- #define menelaus_ioctl NULL
- #endif
- /* REVISIT no compensation register support ... */
- static const struct rtc_class_ops menelaus_rtc_ops = {
- .ioctl = menelaus_ioctl,
- .read_time = menelaus_read_time,
- .set_time = menelaus_set_time,
- .read_alarm = menelaus_read_alarm,
- .set_alarm = menelaus_set_alarm,
- };
- 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);
- }
- 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;
- }
- }
- #else
- static inline void menelaus_rtc_init(struct menelaus_chip *m)
- {
- /* nothing */
- }
- #endif
- /*-----------------------------------------------------------------------*/
- static struct i2c_driver menelaus_i2c_driver;
- static int menelaus_probe(struct i2c_client *client,
- const struct i2c_device_id *id)
- {
- struct menelaus_chip *menelaus;
- int rev = 0;
- int err = 0;
- struct menelaus_platform_data *menelaus_pdata =
- dev_get_platdata(&client->dev);
- if (the_menelaus) {
- dev_dbg(&client->dev, "only one %s for now\n",
- DRIVER_NAME);
- return -ENODEV;
- }
- menelaus = devm_kzalloc(&client->dev, 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) {
- pr_err(DRIVER_NAME ": device not found");
- return -ENODEV;
- }
- /* 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, 0,
- DRIVER_NAME, menelaus);
- if (err) {
- dev_dbg(&client->dev, "can't get IRQ %d, err %d\n",
- client->irq, err);
- return err;
- }
- }
- mutex_init(&menelaus->lock);
- INIT_WORK(&menelaus->work, menelaus_work);
- pr_info("Menelaus rev %d.%d\n", rev >> 4, rev & 0x0f);
- err = menelaus_read_reg(MENELAUS_VCORE_CTRL1);
- if (err < 0)
- goto fail;
- if (err & VCORE_CTRL1_HW_NSW)
- 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 fail;
- }
- menelaus_rtc_init(menelaus);
- return 0;
- fail:
- free_irq(client->irq, menelaus);
- flush_work(&menelaus->work);
- return err;
- }
- static int menelaus_remove(struct i2c_client *client)
- {
- struct menelaus_chip *menelaus = i2c_get_clientdata(client);
- free_irq(client->irq, menelaus);
- flush_work(&menelaus->work);
- the_menelaus = NULL;
- return 0;
- }
- static const struct i2c_device_id menelaus_id[] = {
- { "menelaus", 0 },
- { }
- };
- MODULE_DEVICE_TABLE(i2c, menelaus_id);
- static struct i2c_driver menelaus_i2c_driver = {
- .driver = {
- .name = DRIVER_NAME,
- },
- .probe = menelaus_probe,
- .remove = menelaus_remove,
- .id_table = menelaus_id,
- };
- module_i2c_driver(menelaus_i2c_driver);
- MODULE_AUTHOR("Texas Instruments, Inc. (and others)");
- MODULE_DESCRIPTION("I2C interface for Menelaus.");
- MODULE_LICENSE("GPL");
|