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