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 45#include <asm/mach/irq.h> 46 47#include <mach/gpio.h> 48#include <plat/menelaus.h> 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 269static void menelaus_mmc_cd_work(struct menelaus_chip *menelaus_hw) 270{ 271 int reg; 272 unsigned char card_mask = 0; 273 274 reg = menelaus_read_reg(MENELAUS_MCT_PIN_ST); 275 if (reg < 0) 276 return; 277 278 if (!(reg & 0x1)) 279 card_mask |= MCT_PIN_ST_S1_CD_ST; 280 281 if (!(reg & 0x2)) 282 card_mask |= MCT_PIN_ST_S2_CD_ST; 283 284 if (menelaus_hw->mmc_callback) 285 menelaus_hw->mmc_callback(menelaus_hw->mmc_callback_data, 286 card_mask); 287} 288 289/* 290 * Toggles the MMC slots between open-drain and push-pull mode. 291 */ 292int menelaus_set_mmc_opendrain(int slot, int enable) 293{ 294 int ret, val; 295 296 if (slot != 1 && slot != 2) 297 return -EINVAL; 298 mutex_lock(&the_menelaus->lock); 299 ret = menelaus_read_reg(MENELAUS_MCT_CTRL1); 300 if (ret < 0) { 301 mutex_unlock(&the_menelaus->lock); 302 return ret; 303 } 304 val = ret; 305 if (slot == 1) { 306 if (enable) 307 val |= MCT_CTRL1_S1_CMD_OD; 308 else 309 val &= ~MCT_CTRL1_S1_CMD_OD; 310 } else { 311 if (enable) 312 val |= MCT_CTRL1_S2_CMD_OD; 313 else 314 val &= ~MCT_CTRL1_S2_CMD_OD; 315 } 316 ret = menelaus_write_reg(MENELAUS_MCT_CTRL1, val); 317 mutex_unlock(&the_menelaus->lock); 318 319 return ret; 320} 321EXPORT_SYMBOL(menelaus_set_mmc_opendrain); 322 323int menelaus_set_slot_sel(int enable) 324{ 325 int ret; 326 327 mutex_lock(&the_menelaus->lock); 328 ret = menelaus_read_reg(MENELAUS_GPIO_CTRL); 329 if (ret < 0) 330 goto out; 331 ret |= GPIO2_DIR_INPUT; 332 if (enable) 333 ret |= GPIO_CTRL_SLOTSELEN; 334 else 335 ret &= ~GPIO_CTRL_SLOTSELEN; 336 ret = menelaus_write_reg(MENELAUS_GPIO_CTRL, ret); 337out: 338 mutex_unlock(&the_menelaus->lock); 339 return ret; 340} 341EXPORT_SYMBOL(menelaus_set_slot_sel); 342 343int menelaus_set_mmc_slot(int slot, int enable, int power, int cd_en) 344{ 345 int ret, val; 346 347 if (slot != 1 && slot != 2) 348 return -EINVAL; 349 if (power >= 3) 350 return -EINVAL; 351 352 mutex_lock(&the_menelaus->lock); 353 354 ret = menelaus_read_reg(MENELAUS_MCT_CTRL2); 355 if (ret < 0) 356 goto out; 357 val = ret; 358 if (slot == 1) { 359 if (cd_en) 360 val |= MCT_CTRL2_S1CD_BUFEN | MCT_CTRL2_S1CD_DBEN; 361 else 362 val &= ~(MCT_CTRL2_S1CD_BUFEN | MCT_CTRL2_S1CD_DBEN); 363 } else { 364 if (cd_en) 365 val |= MCT_CTRL2_S2CD_BUFEN | MCT_CTRL2_S2CD_BEN; 366 else 367 val &= ~(MCT_CTRL2_S2CD_BUFEN | MCT_CTRL2_S2CD_BEN); 368 } 369 ret = menelaus_write_reg(MENELAUS_MCT_CTRL2, val); 370 if (ret < 0) 371 goto out; 372 373 ret = menelaus_read_reg(MENELAUS_MCT_CTRL3); 374 if (ret < 0) 375 goto out; 376 val = ret; 377 if (slot == 1) { 378 if (enable) 379 val |= MCT_CTRL3_SLOT1_EN; 380 else 381 val &= ~MCT_CTRL3_SLOT1_EN; 382 } else { 383 int b; 384 385 if (enable) 386 val |= MCT_CTRL3_SLOT2_EN; 387 else 388 val &= ~MCT_CTRL3_SLOT2_EN; 389 b = menelaus_read_reg(MENELAUS_MCT_CTRL2); 390 b &= ~(MCT_CTRL2_VS2_SEL_D0 | MCT_CTRL2_VS2_SEL_D1); 391 b |= power; 392 ret = menelaus_write_reg(MENELAUS_MCT_CTRL2, b); 393 if (ret < 0) 394 goto out; 395 } 396 /* Disable autonomous shutdown */ 397 val &= ~(MCT_CTRL3_S1_AUTO_EN | MCT_CTRL3_S2_AUTO_EN); 398 ret = menelaus_write_reg(MENELAUS_MCT_CTRL3, val); 399out: 400 mutex_unlock(&the_menelaus->lock); 401 return ret; 402} 403EXPORT_SYMBOL(menelaus_set_mmc_slot); 404 405int menelaus_register_mmc_callback(void (*callback)(void *data, u8 card_mask), 406 void *data) 407{ 408 int ret = 0; 409 410 the_menelaus->mmc_callback_data = data; 411 the_menelaus->mmc_callback = callback; 412 ret = menelaus_add_irq_work(MENELAUS_MMC_S1CD_IRQ, 413 menelaus_mmc_cd_work); 414 if (ret < 0) 415 return ret; 416 ret = menelaus_add_irq_work(MENELAUS_MMC_S2CD_IRQ, 417 menelaus_mmc_cd_work); 418 if (ret < 0) 419 return ret; 420 ret = menelaus_add_irq_work(MENELAUS_MMC_S1D1_IRQ, 421 menelaus_mmc_cd_work); 422 if (ret < 0) 423 return ret; 424 ret = menelaus_add_irq_work(MENELAUS_MMC_S2D1_IRQ, 425 menelaus_mmc_cd_work); 426 427 return ret; 428} 429EXPORT_SYMBOL(menelaus_register_mmc_callback); 430 431void menelaus_unregister_mmc_callback(void) 432{ 433 menelaus_remove_irq_work(MENELAUS_MMC_S1CD_IRQ); 434 menelaus_remove_irq_work(MENELAUS_MMC_S2CD_IRQ); 435 menelaus_remove_irq_work(MENELAUS_MMC_S1D1_IRQ); 436 menelaus_remove_irq_work(MENELAUS_MMC_S2D1_IRQ); 437 438 the_menelaus->mmc_callback = NULL; 439 the_menelaus->mmc_callback_data = 0; 440} 441EXPORT_SYMBOL(menelaus_unregister_mmc_callback); 442 443struct menelaus_vtg { 444 const char *name; 445 u8 vtg_reg; 446 u8 vtg_shift; 447 u8 vtg_bits; 448 u8 mode_reg; 449}; 450 451struct menelaus_vtg_value { 452 u16 vtg; 453 u16 val; 454}; 455 456static int menelaus_set_voltage(const struct menelaus_vtg *vtg, int mV, 457 int vtg_val, int mode) 458{ 459 int val, ret; 460 struct i2c_client *c = the_menelaus->client; 461 462 mutex_lock(&the_menelaus->lock); 463 if (vtg == 0) 464 goto set_voltage; 465 466 ret = menelaus_read_reg(vtg->vtg_reg); 467 if (ret < 0) 468 goto out; 469 val = ret & ~(((1 << vtg->vtg_bits) - 1) << vtg->vtg_shift); 470 val |= vtg_val << vtg->vtg_shift; 471 472 dev_dbg(&c->dev, "Setting voltage '%s'" 473 "to %d mV (reg 0x%02x, val 0x%02x)\n", 474 vtg->name, mV, vtg->vtg_reg, val); 475 476 ret = menelaus_write_reg(vtg->vtg_reg, val); 477 if (ret < 0) 478 goto out; 479set_voltage: 480 ret = menelaus_write_reg(vtg->mode_reg, mode); 481out: 482 mutex_unlock(&the_menelaus->lock); 483 if (ret == 0) { 484 /* Wait for voltage to stabilize */ 485 msleep(1); 486 } 487 return ret; 488} 489 490static int menelaus_get_vtg_value(int vtg, const struct menelaus_vtg_value *tbl, 491 int n) 492{ 493 int i; 494 495 for (i = 0; i < n; i++, tbl++) 496 if (tbl->vtg == vtg) 497 return tbl->val; 498 return -EINVAL; 499} 500 501/* 502 * Vcore can be programmed in two ways: 503 * SW-controlled: Required voltage is programmed into VCORE_CTRL1 504 * HW-controlled: Required range (roof-floor) is programmed into VCORE_CTRL3 505 * and VCORE_CTRL4 506 * 507 * Call correct 'set' function accordingly 508 */ 509 510static const struct menelaus_vtg_value vcore_values[] = { 511 { 1000, 0 }, 512 { 1025, 1 }, 513 { 1050, 2 }, 514 { 1075, 3 }, 515 { 1100, 4 }, 516 { 1125, 5 }, 517 { 1150, 6 }, 518 { 1175, 7 }, 519 { 1200, 8 }, 520 { 1225, 9 }, 521 { 1250, 10 }, 522 { 1275, 11 }, 523 { 1300, 12 }, 524 { 1325, 13 }, 525 { 1350, 14 }, 526 { 1375, 15 }, 527 { 1400, 16 }, 528 { 1425, 17 }, 529 { 1450, 18 }, 530}; 531 532int menelaus_set_vcore_sw(unsigned int mV) 533{ 534 int val, ret; 535 struct i2c_client *c = the_menelaus->client; 536 537 val = menelaus_get_vtg_value(mV, vcore_values, 538 ARRAY_SIZE(vcore_values)); 539 if (val < 0) 540 return -EINVAL; 541 542 dev_dbg(&c->dev, "Setting VCORE to %d mV (val 0x%02x)\n", mV, val); 543 544 /* Set SW mode and the voltage in one go. */ 545 mutex_lock(&the_menelaus->lock); 546 ret = menelaus_write_reg(MENELAUS_VCORE_CTRL1, val); 547 if (ret == 0) 548 the_menelaus->vcore_hw_mode = 0; 549 mutex_unlock(&the_menelaus->lock); 550 msleep(1); 551 552 return ret; 553} 554 555int menelaus_set_vcore_hw(unsigned int roof_mV, unsigned int floor_mV) 556{ 557 int fval, rval, val, ret; 558 struct i2c_client *c = the_menelaus->client; 559 560 rval = menelaus_get_vtg_value(roof_mV, vcore_values, 561 ARRAY_SIZE(vcore_values)); 562 if (rval < 0) 563 return -EINVAL; 564 fval = menelaus_get_vtg_value(floor_mV, vcore_values, 565 ARRAY_SIZE(vcore_values)); 566 if (fval < 0) 567 return -EINVAL; 568 569 dev_dbg(&c->dev, "Setting VCORE FLOOR to %d mV and ROOF to %d mV\n", 570 floor_mV, roof_mV); 571 572 mutex_lock(&the_menelaus->lock); 573 ret = menelaus_write_reg(MENELAUS_VCORE_CTRL3, fval); 574 if (ret < 0) 575 goto out; 576 ret = menelaus_write_reg(MENELAUS_VCORE_CTRL4, rval); 577 if (ret < 0) 578 goto out; 579 if (!the_menelaus->vcore_hw_mode) { 580 val = menelaus_read_reg(MENELAUS_VCORE_CTRL1); 581 /* HW mode, turn OFF byte comparator */ 582 val |= (VCORE_CTRL1_HW_NSW | VCORE_CTRL1_BYP_COMP); 583 ret = menelaus_write_reg(MENELAUS_VCORE_CTRL1, val); 584 the_menelaus->vcore_hw_mode = 1; 585 } 586 msleep(1); 587out: 588 mutex_unlock(&the_menelaus->lock); 589 return ret; 590} 591 592static const struct menelaus_vtg vmem_vtg = { 593 .name = "VMEM", 594 .vtg_reg = MENELAUS_LDO_CTRL1, 595 .vtg_shift = 0, 596 .vtg_bits = 2, 597 .mode_reg = MENELAUS_LDO_CTRL3, 598}; 599 600static const struct menelaus_vtg_value vmem_values[] = { 601 { 1500, 0 }, 602 { 1800, 1 }, 603 { 1900, 2 }, 604 { 2500, 3 }, 605}; 606 607int menelaus_set_vmem(unsigned int mV) 608{ 609 int val; 610 611 if (mV == 0) 612 return menelaus_set_voltage(&vmem_vtg, 0, 0, 0); 613 614 val = menelaus_get_vtg_value(mV, vmem_values, ARRAY_SIZE(vmem_values)); 615 if (val < 0) 616 return -EINVAL; 617 return menelaus_set_voltage(&vmem_vtg, mV, val, 0x02); 618} 619EXPORT_SYMBOL(menelaus_set_vmem); 620 621static const struct menelaus_vtg vio_vtg = { 622 .name = "VIO", 623 .vtg_reg = MENELAUS_LDO_CTRL1, 624 .vtg_shift = 2, 625 .vtg_bits = 2, 626 .mode_reg = MENELAUS_LDO_CTRL4, 627}; 628 629static const struct menelaus_vtg_value vio_values[] = { 630 { 1500, 0 }, 631 { 1800, 1 }, 632 { 2500, 2 }, 633 { 2800, 3 }, 634}; 635 636int menelaus_set_vio(unsigned int mV) 637{ 638 int val; 639 640 if (mV == 0) 641 return menelaus_set_voltage(&vio_vtg, 0, 0, 0); 642 643 val = menelaus_get_vtg_value(mV, vio_values, ARRAY_SIZE(vio_values)); 644 if (val < 0) 645 return -EINVAL; 646 return menelaus_set_voltage(&vio_vtg, mV, val, 0x02); 647} 648EXPORT_SYMBOL(menelaus_set_vio); 649 650static const struct menelaus_vtg_value vdcdc_values[] = { 651 { 1500, 0 }, 652 { 1800, 1 }, 653 { 2000, 2 }, 654 { 2200, 3 }, 655 { 2400, 4 }, 656 { 2800, 5 }, 657 { 3000, 6 }, 658 { 3300, 7 }, 659}; 660 661static const struct menelaus_vtg vdcdc2_vtg = { 662 .name = "VDCDC2", 663 .vtg_reg = MENELAUS_DCDC_CTRL1, 664 .vtg_shift = 0, 665 .vtg_bits = 3, 666 .mode_reg = MENELAUS_DCDC_CTRL2, 667}; 668 669static const struct menelaus_vtg vdcdc3_vtg = { 670 .name = "VDCDC3", 671 .vtg_reg = MENELAUS_DCDC_CTRL1, 672 .vtg_shift = 3, 673 .vtg_bits = 3, 674 .mode_reg = MENELAUS_DCDC_CTRL3, 675}; 676 677int menelaus_set_vdcdc(int dcdc, unsigned int mV) 678{ 679 const struct menelaus_vtg *vtg; 680 int val; 681 682 if (dcdc != 2 && dcdc != 3) 683 return -EINVAL; 684 if (dcdc == 2) 685 vtg = &vdcdc2_vtg; 686 else 687 vtg = &vdcdc3_vtg; 688 689 if (mV == 0) 690 return menelaus_set_voltage(vtg, 0, 0, 0); 691 692 val = menelaus_get_vtg_value(mV, vdcdc_values, 693 ARRAY_SIZE(vdcdc_values)); 694 if (val < 0) 695 return -EINVAL; 696 return menelaus_set_voltage(vtg, mV, val, 0x03); 697} 698 699static const struct menelaus_vtg_value vmmc_values[] = { 700 { 1850, 0 }, 701 { 2800, 1 }, 702 { 3000, 2 }, 703 { 3100, 3 }, 704}; 705 706static const struct menelaus_vtg vmmc_vtg = { 707 .name = "VMMC", 708 .vtg_reg = MENELAUS_LDO_CTRL1, 709 .vtg_shift = 6, 710 .vtg_bits = 2, 711 .mode_reg = MENELAUS_LDO_CTRL7, 712}; 713 714int menelaus_set_vmmc(unsigned int mV) 715{ 716 int val; 717 718 if (mV == 0) 719 return menelaus_set_voltage(&vmmc_vtg, 0, 0, 0); 720 721 val = menelaus_get_vtg_value(mV, vmmc_values, ARRAY_SIZE(vmmc_values)); 722 if (val < 0) 723 return -EINVAL; 724 return menelaus_set_voltage(&vmmc_vtg, mV, val, 0x02); 725} 726EXPORT_SYMBOL(menelaus_set_vmmc); 727 728 729static const struct menelaus_vtg_value vaux_values[] = { 730 { 1500, 0 }, 731 { 1800, 1 }, 732 { 2500, 2 }, 733 { 2800, 3 }, 734}; 735 736static const struct menelaus_vtg vaux_vtg = { 737 .name = "VAUX", 738 .vtg_reg = MENELAUS_LDO_CTRL1, 739 .vtg_shift = 4, 740 .vtg_bits = 2, 741 .mode_reg = MENELAUS_LDO_CTRL6, 742}; 743 744int menelaus_set_vaux(unsigned int mV) 745{ 746 int val; 747 748 if (mV == 0) 749 return menelaus_set_voltage(&vaux_vtg, 0, 0, 0); 750 751 val = menelaus_get_vtg_value(mV, vaux_values, ARRAY_SIZE(vaux_values)); 752 if (val < 0) 753 return -EINVAL; 754 return menelaus_set_voltage(&vaux_vtg, mV, val, 0x02); 755} 756EXPORT_SYMBOL(menelaus_set_vaux); 757 758int menelaus_get_slot_pin_states(void) 759{ 760 return menelaus_read_reg(MENELAUS_MCT_PIN_ST); 761} 762EXPORT_SYMBOL(menelaus_get_slot_pin_states); 763 764int menelaus_set_regulator_sleep(int enable, u32 val) 765{ 766 int t, ret; 767 struct i2c_client *c = the_menelaus->client; 768 769 mutex_lock(&the_menelaus->lock); 770 ret = menelaus_write_reg(MENELAUS_SLEEP_CTRL2, val); 771 if (ret < 0) 772 goto out; 773 774 dev_dbg(&c->dev, "regulator sleep configuration: %02x\n", val); 775 776 ret = menelaus_read_reg(MENELAUS_GPIO_CTRL); 777 if (ret < 0) 778 goto out; 779 t = (GPIO_CTRL_SLPCTLEN | GPIO3_DIR_INPUT); 780 if (enable) 781 ret |= t; 782 else 783 ret &= ~t; 784 ret = menelaus_write_reg(MENELAUS_GPIO_CTRL, ret); 785out: 786 mutex_unlock(&the_menelaus->lock); 787 return ret; 788} 789 790/*-----------------------------------------------------------------------*/ 791 792/* Handles Menelaus interrupts. Does not run in interrupt context */ 793static void menelaus_work(struct work_struct *_menelaus) 794{ 795 struct menelaus_chip *menelaus = 796 container_of(_menelaus, struct menelaus_chip, work); 797 void (*handler)(struct menelaus_chip *menelaus); 798 799 while (1) { 800 unsigned isr; 801 802 isr = (menelaus_read_reg(MENELAUS_INT_STATUS2) 803 & ~menelaus->mask2) << 8; 804 isr |= menelaus_read_reg(MENELAUS_INT_STATUS1) 805 & ~menelaus->mask1; 806 if (!isr) 807 break; 808 809 while (isr) { 810 int irq = fls(isr) - 1; 811 isr &= ~(1 << irq); 812 813 mutex_lock(&menelaus->lock); 814 menelaus_disable_irq(irq); 815 menelaus_ack_irq(irq); 816 handler = menelaus->handlers[irq]; 817 if (handler) 818 handler(menelaus); 819 menelaus_enable_irq(irq); 820 mutex_unlock(&menelaus->lock); 821 } 822 } 823 enable_irq(menelaus->client->irq); 824} 825 826/* 827 * We cannot use I2C in interrupt context, so we just schedule work. 828 */ 829static irqreturn_t menelaus_irq(int irq, void *_menelaus) 830{ 831 struct menelaus_chip *menelaus = _menelaus; 832 833 disable_irq_nosync(irq); 834 (void)schedule_work(&menelaus->work); 835 836 return IRQ_HANDLED; 837} 838 839/*-----------------------------------------------------------------------*/ 840 841/* 842 * The RTC needs to be set once, then it runs on backup battery power. 843 * It supports alarms, including system wake alarms (from some modes); 844 * and 1/second IRQs if requested. 845 */ 846#ifdef CONFIG_RTC_DRV_TWL92330 847 848#define RTC_CTRL_RTC_EN (1 << 0) 849#define RTC_CTRL_AL_EN (1 << 1) 850#define RTC_CTRL_MODE12 (1 << 2) 851#define RTC_CTRL_EVERY_MASK (3 << 3) 852#define RTC_CTRL_EVERY_SEC (0 << 3) 853#define RTC_CTRL_EVERY_MIN (1 << 3) 854#define RTC_CTRL_EVERY_HR (2 << 3) 855#define RTC_CTRL_EVERY_DAY (3 << 3) 856 857#define RTC_UPDATE_EVERY 0x08 858 859#define RTC_HR_PM (1 << 7) 860 861static void menelaus_to_time(char *regs, struct rtc_time *t) 862{ 863 t->tm_sec = bcd2bin(regs[0]); 864 t->tm_min = bcd2bin(regs[1]); 865 if (the_menelaus->rtc_control & RTC_CTRL_MODE12) { 866 t->tm_hour = bcd2bin(regs[2] & 0x1f) - 1; 867 if (regs[2] & RTC_HR_PM) 868 t->tm_hour += 12; 869 } else 870 t->tm_hour = bcd2bin(regs[2] & 0x3f); 871 t->tm_mday = bcd2bin(regs[3]); 872 t->tm_mon = bcd2bin(regs[4]) - 1; 873 t->tm_year = bcd2bin(regs[5]) + 100; 874} 875 876static int time_to_menelaus(struct rtc_time *t, int regnum) 877{ 878 int hour, status; 879 880 status = menelaus_write_reg(regnum++, bin2bcd(t->tm_sec)); 881 if (status < 0) 882 goto fail; 883 884 status = menelaus_write_reg(regnum++, bin2bcd(t->tm_min)); 885 if (status < 0) 886 goto fail; 887 888 if (the_menelaus->rtc_control & RTC_CTRL_MODE12) { 889 hour = t->tm_hour + 1; 890 if (hour > 12) 891 hour = RTC_HR_PM | bin2bcd(hour - 12); 892 else 893 hour = bin2bcd(hour); 894 } else 895 hour = bin2bcd(t->tm_hour); 896 status = menelaus_write_reg(regnum++, hour); 897 if (status < 0) 898 goto fail; 899 900 status = menelaus_write_reg(regnum++, bin2bcd(t->tm_mday)); 901 if (status < 0) 902 goto fail; 903 904 status = menelaus_write_reg(regnum++, bin2bcd(t->tm_mon + 1)); 905 if (status < 0) 906 goto fail; 907 908 status = menelaus_write_reg(regnum++, bin2bcd(t->tm_year - 100)); 909 if (status < 0) 910 goto fail; 911 912 return 0; 913fail: 914 dev_err(&the_menelaus->client->dev, "rtc write reg %02x, err %d\n", 915 --regnum, status); 916 return status; 917} 918 919static int menelaus_read_time(struct device *dev, struct rtc_time *t) 920{ 921 struct i2c_msg msg[2]; 922 char regs[7]; 923 int status; 924 925 /* block read date and time registers */ 926 regs[0] = MENELAUS_RTC_SEC; 927 928 msg[0].addr = MENELAUS_I2C_ADDRESS; 929 msg[0].flags = 0; 930 msg[0].len = 1; 931 msg[0].buf = regs; 932 933 msg[1].addr = MENELAUS_I2C_ADDRESS; 934 msg[1].flags = I2C_M_RD; 935 msg[1].len = sizeof(regs); 936 msg[1].buf = regs; 937 938 status = i2c_transfer(the_menelaus->client->adapter, msg, 2); 939 if (status != 2) { 940 dev_err(dev, "%s error %d\n", "read", status); 941 return -EIO; 942 } 943 944 menelaus_to_time(regs, t); 945 t->tm_wday = bcd2bin(regs[6]); 946 947 return 0; 948} 949 950static int menelaus_set_time(struct device *dev, struct rtc_time *t) 951{ 952 int status; 953 954 /* write date and time registers */ 955 status = time_to_menelaus(t, MENELAUS_RTC_SEC); 956 if (status < 0) 957 return status; 958 status = menelaus_write_reg(MENELAUS_RTC_WKDAY, bin2bcd(t->tm_wday)); 959 if (status < 0) { 960 dev_err(&the_menelaus->client->dev, "rtc write reg %02x " 961 "err %d\n", MENELAUS_RTC_WKDAY, status); 962 return status; 963 } 964 965 /* now commit the write */ 966 status = menelaus_write_reg(MENELAUS_RTC_UPDATE, RTC_UPDATE_EVERY); 967 if (status < 0) 968 dev_err(&the_menelaus->client->dev, "rtc commit time, err %d\n", 969 status); 970 971 return 0; 972} 973 974static int menelaus_read_alarm(struct device *dev, struct rtc_wkalrm *w) 975{ 976 struct i2c_msg msg[2]; 977 char regs[6]; 978 int status; 979 980 /* block read alarm registers */ 981 regs[0] = MENELAUS_RTC_AL_SEC; 982 983 msg[0].addr = MENELAUS_I2C_ADDRESS; 984 msg[0].flags = 0; 985 msg[0].len = 1; 986 msg[0].buf = regs; 987 988 msg[1].addr = MENELAUS_I2C_ADDRESS; 989 msg[1].flags = I2C_M_RD; 990 msg[1].len = sizeof(regs); 991 msg[1].buf = regs; 992 993 status = i2c_transfer(the_menelaus->client->adapter, msg, 2); 994 if (status != 2) { 995 dev_err(dev, "%s error %d\n", "alarm read", status); 996 return -EIO; 997 } 998 999 menelaus_to_time(regs, &w->time); 1000 1001 w->enabled = !!(the_menelaus->rtc_control & RTC_CTRL_AL_EN); 1002 1003 /* NOTE we *could* check if actually pending... */ 1004 w->pending = 0; 1005 1006 return 0; 1007} 1008 1009static int menelaus_set_alarm(struct device *dev, struct rtc_wkalrm *w) 1010{ 1011 int status; 1012 1013 if (the_menelaus->client->irq <= 0 && w->enabled) 1014 return -ENODEV; 1015 1016 /* clear previous alarm enable */ 1017 if (the_menelaus->rtc_control & RTC_CTRL_AL_EN) { 1018 the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN; 1019 status = menelaus_write_reg(MENELAUS_RTC_CTRL, 1020 the_menelaus->rtc_control); 1021 if (status < 0) 1022 return status; 1023 } 1024 1025 /* write alarm registers */ 1026 status = time_to_menelaus(&w->time, MENELAUS_RTC_AL_SEC); 1027 if (status < 0) 1028 return status; 1029 1030 /* enable alarm if requested */ 1031 if (w->enabled) { 1032 the_menelaus->rtc_control |= RTC_CTRL_AL_EN; 1033 status = menelaus_write_reg(MENELAUS_RTC_CTRL, 1034 the_menelaus->rtc_control); 1035 } 1036 1037 return status; 1038} 1039 1040#ifdef CONFIG_RTC_INTF_DEV 1041 1042static void menelaus_rtc_update_work(struct menelaus_chip *m) 1043{ 1044 /* report 1/sec update */ 1045 local_irq_disable(); 1046 rtc_update_irq(m->rtc, 1, RTC_IRQF | RTC_UF); 1047 local_irq_enable(); 1048} 1049 1050static int menelaus_ioctl(struct device *dev, unsigned cmd, unsigned long arg) 1051{ 1052 int status; 1053 1054 if (the_menelaus->client->irq <= 0) 1055 return -ENOIOCTLCMD; 1056 1057 switch (cmd) { 1058 /* alarm IRQ */ 1059 case RTC_AIE_ON: 1060 if (the_menelaus->rtc_control & RTC_CTRL_AL_EN) 1061 return 0; 1062 the_menelaus->rtc_control |= RTC_CTRL_AL_EN; 1063 break; 1064 case RTC_AIE_OFF: 1065 if (!(the_menelaus->rtc_control & RTC_CTRL_AL_EN)) 1066 return 0; 1067 the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN; 1068 break; 1069 /* 1/second "update" IRQ */ 1070 case RTC_UIE_ON: 1071 if (the_menelaus->uie) 1072 return 0; 1073 status = menelaus_remove_irq_work(MENELAUS_RTCTMR_IRQ); 1074 status = menelaus_add_irq_work(MENELAUS_RTCTMR_IRQ, 1075 menelaus_rtc_update_work); 1076 if (status == 0) 1077 the_menelaus->uie = 1; 1078 return status; 1079 case RTC_UIE_OFF: 1080 if (!the_menelaus->uie) 1081 return 0; 1082 status = menelaus_remove_irq_work(MENELAUS_RTCTMR_IRQ); 1083 if (status == 0) 1084 the_menelaus->uie = 0; 1085 return status; 1086 default: 1087 return -ENOIOCTLCMD; 1088 } 1089 return menelaus_write_reg(MENELAUS_RTC_CTRL, the_menelaus->rtc_control); 1090} 1091 1092#else 1093#define menelaus_ioctl NULL 1094#endif 1095 1096/* REVISIT no compensation register support ... */ 1097 1098static const struct rtc_class_ops menelaus_rtc_ops = { 1099 .ioctl = menelaus_ioctl, 1100 .read_time = menelaus_read_time, 1101 .set_time = menelaus_set_time, 1102 .read_alarm = menelaus_read_alarm, 1103 .set_alarm = menelaus_set_alarm, 1104}; 1105 1106static void menelaus_rtc_alarm_work(struct menelaus_chip *m) 1107{ 1108 /* report alarm */ 1109 local_irq_disable(); 1110 rtc_update_irq(m->rtc, 1, RTC_IRQF | RTC_AF); 1111 local_irq_enable(); 1112 1113 /* then disable it; alarms are oneshot */ 1114 the_menelaus->rtc_control &= ~RTC_CTRL_AL_EN; 1115 menelaus_write_reg(MENELAUS_RTC_CTRL, the_menelaus->rtc_control); 1116} 1117 1118static inline void menelaus_rtc_init(struct menelaus_chip *m) 1119{ 1120 int alarm = (m->client->irq > 0); 1121 1122 /* assume 32KDETEN pin is pulled high */ 1123 if (!(menelaus_read_reg(MENELAUS_OSC_CTRL) & 0x80)) { 1124 dev_dbg(&m->client->dev, "no 32k oscillator\n"); 1125 return; 1126 } 1127 1128 /* support RTC alarm; it can issue wakeups */ 1129 if (alarm) { 1130 if (menelaus_add_irq_work(MENELAUS_RTCALM_IRQ, 1131 menelaus_rtc_alarm_work) < 0) { 1132 dev_err(&m->client->dev, "can't handle RTC alarm\n"); 1133 return; 1134 } 1135 device_init_wakeup(&m->client->dev, 1); 1136 } 1137 1138 /* be sure RTC is enabled; allow 1/sec irqs; leave 12hr mode alone */ 1139 m->rtc_control = menelaus_read_reg(MENELAUS_RTC_CTRL); 1140 if (!(m->rtc_control & RTC_CTRL_RTC_EN) 1141 || (m->rtc_control & RTC_CTRL_AL_EN) 1142 || (m->rtc_control & RTC_CTRL_EVERY_MASK)) { 1143 if (!(m->rtc_control & RTC_CTRL_RTC_EN)) { 1144 dev_warn(&m->client->dev, "rtc clock needs setting\n"); 1145 m->rtc_control |= RTC_CTRL_RTC_EN; 1146 } 1147 m->rtc_control &= ~RTC_CTRL_EVERY_MASK; 1148 m->rtc_control &= ~RTC_CTRL_AL_EN; 1149 menelaus_write_reg(MENELAUS_RTC_CTRL, m->rtc_control); 1150 } 1151 1152 m->rtc = rtc_device_register(DRIVER_NAME, 1153 &m->client->dev, 1154 &menelaus_rtc_ops, THIS_MODULE); 1155 if (IS_ERR(m->rtc)) { 1156 if (alarm) { 1157 menelaus_remove_irq_work(MENELAUS_RTCALM_IRQ); 1158 device_init_wakeup(&m->client->dev, 0); 1159 } 1160 dev_err(&m->client->dev, "can't register RTC: %d\n", 1161 (int) PTR_ERR(m->rtc)); 1162 the_menelaus->rtc = NULL; 1163 } 1164} 1165 1166#else 1167 1168static inline void menelaus_rtc_init(struct menelaus_chip *m) 1169{ 1170 /* nothing */ 1171} 1172 1173#endif 1174 1175/*-----------------------------------------------------------------------*/ 1176 1177static struct i2c_driver menelaus_i2c_driver; 1178 1179static int menelaus_probe(struct i2c_client *client, 1180 const struct i2c_device_id *id) 1181{ 1182 struct menelaus_chip *menelaus; 1183 int rev = 0, val; 1184 int err = 0; 1185 struct menelaus_platform_data *menelaus_pdata = 1186 client->dev.platform_data; 1187 1188 if (the_menelaus) { 1189 dev_dbg(&client->dev, "only one %s for now\n", 1190 DRIVER_NAME); 1191 return -ENODEV; 1192 } 1193 1194 menelaus = kzalloc(sizeof *menelaus, GFP_KERNEL); 1195 if (!menelaus) 1196 return -ENOMEM; 1197 1198 i2c_set_clientdata(client, menelaus); 1199 1200 the_menelaus = menelaus; 1201 menelaus->client = client; 1202 1203 /* If a true probe check the device */ 1204 rev = menelaus_read_reg(MENELAUS_REV); 1205 if (rev < 0) { 1206 pr_err(DRIVER_NAME ": device not found"); 1207 err = -ENODEV; 1208 goto fail1; 1209 } 1210 1211 /* Ack and disable all Menelaus interrupts */ 1212 menelaus_write_reg(MENELAUS_INT_ACK1, 0xff); 1213 menelaus_write_reg(MENELAUS_INT_ACK2, 0xff); 1214 menelaus_write_reg(MENELAUS_INT_MASK1, 0xff); 1215 menelaus_write_reg(MENELAUS_INT_MASK2, 0xff); 1216 menelaus->mask1 = 0xff; 1217 menelaus->mask2 = 0xff; 1218 1219 /* Set output buffer strengths */ 1220 menelaus_write_reg(MENELAUS_MCT_CTRL1, 0x73); 1221 1222 if (client->irq > 0) { 1223 err = request_irq(client->irq, menelaus_irq, IRQF_DISABLED, 1224 DRIVER_NAME, menelaus); 1225 if (err) { 1226 dev_dbg(&client->dev, "can't get IRQ %d, err %d\n", 1227 client->irq, err); 1228 goto fail1; 1229 } 1230 } 1231 1232 mutex_init(&menelaus->lock); 1233 INIT_WORK(&menelaus->work, menelaus_work); 1234 1235 pr_info("Menelaus rev %d.%d\n", rev >> 4, rev & 0x0f); 1236 1237 val = menelaus_read_reg(MENELAUS_VCORE_CTRL1); 1238 if (val < 0) 1239 goto fail2; 1240 if (val & (1 << 7)) 1241 menelaus->vcore_hw_mode = 1; 1242 else 1243 menelaus->vcore_hw_mode = 0; 1244 1245 if (menelaus_pdata != NULL && menelaus_pdata->late_init != NULL) { 1246 err = menelaus_pdata->late_init(&client->dev); 1247 if (err < 0) 1248 goto fail2; 1249 } 1250 1251 menelaus_rtc_init(menelaus); 1252 1253 return 0; 1254fail2: 1255 free_irq(client->irq, menelaus); 1256 flush_scheduled_work(); 1257fail1: 1258 kfree(menelaus); 1259 return err; 1260} 1261 1262static int __exit menelaus_remove(struct i2c_client *client) 1263{ 1264 struct menelaus_chip *menelaus = i2c_get_clientdata(client); 1265 1266 free_irq(client->irq, menelaus); 1267 kfree(menelaus); 1268 the_menelaus = NULL; 1269 return 0; 1270} 1271 1272static const struct i2c_device_id menelaus_id[] = { 1273 { "menelaus", 0 }, 1274 { } 1275}; 1276MODULE_DEVICE_TABLE(i2c, menelaus_id); 1277 1278static struct i2c_driver menelaus_i2c_driver = { 1279 .driver = { 1280 .name = DRIVER_NAME, 1281 }, 1282 .probe = menelaus_probe, 1283 .remove = __exit_p(menelaus_remove), 1284 .id_table = menelaus_id, 1285}; 1286 1287static int __init menelaus_init(void) 1288{ 1289 int res; 1290 1291 res = i2c_add_driver(&menelaus_i2c_driver); 1292 if (res < 0) { 1293 pr_err(DRIVER_NAME ": driver registration failed\n"); 1294 return res; 1295 } 1296 1297 return 0; 1298} 1299 1300static void __exit menelaus_exit(void) 1301{ 1302 i2c_del_driver(&menelaus_i2c_driver); 1303 1304} 1305 1306MODULE_AUTHOR("Texas Instruments, Inc. (and others)"); 1307MODULE_DESCRIPTION("I2C interface for Menelaus."); 1308MODULE_LICENSE("GPL"); 1309 1310module_init(menelaus_init); 1311module_exit(menelaus_exit); 1312