2 * drivers/i2c/chips/menelaus.c
4 * Copyright (C) 2004 Texas Instruments, Inc.
6 * Some parts based tps65010.c:
7 * Copyright (C) 2004 Texas Instruments and
8 * Copyright (C) 2004-2005 David Brownell
10 * Some parts based on tlv320aic24.c:
11 * Copyright (C) by Kai Svahn <kai.svahn@nokia.com>
13 * Changes for interrupt handling and clean-up by
14 * Tony Lindgren <tony@atomide.com> and Imre Deak <imre.deak@nokia.com>
15 * Copyright (C) 2005 Nokia Corporation
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include <linux/module.h>
33 #include <linux/i2c.h>
34 #include <linux/interrupt.h>
35 #include <linux/sched.h>
37 #include <asm/mach-types.h>
38 #include <asm/mach/irq.h>
40 #include <asm/arch/mux.h>
41 #include <asm/arch/gpio.h>
42 #include <asm/arch/menelaus.h>
44 #define DRIVER_NAME "menelaus"
46 #define pr_err(fmt, arg...) printk(KERN_ERR DRIVER_NAME ": ", ## arg);
48 #define MENELAUS_I2C_ADDRESS 0x72
50 #define MENELAUS_REV 0x01
51 #define MENELAUS_VCORE_CTRL1 0x02
52 #define MENELAUS_VCORE_CTRL2 0x03
53 #define MENELAUS_VCORE_CTRL3 0x04
54 #define MENELAUS_VCORE_CTRL4 0x05
55 #define MENELAUS_VCORE_CTRL5 0x06
56 #define MENELAUS_DCDC_CTRL1 0x07
57 #define MENELAUS_DCDC_CTRL2 0x08
58 #define MENELAUS_DCDC_CTRL3 0x09
59 #define MENELAUS_LDO_CTRL1 0x0A
60 #define MENELAUS_LDO_CTRL2 0x0B
61 #define MENELAUS_LDO_CTRL3 0x0C
62 #define MENELAUS_LDO_CTRL4 0x0D
63 #define MENELAUS_LDO_CTRL5 0x0E
64 #define MENELAUS_LDO_CTRL6 0x0F
65 #define MENELAUS_LDO_CTRL7 0x10
66 #define MENELAUS_LDO_CTRL8 0x11
67 #define MENELAUS_SLEEP_CTRL1 0x12
68 #define MENELAUS_SLEEP_CTRL2 0x13
69 #define MENELAUS_DEVICE_OFF 0x14
70 #define MENELAUS_OSC_CTRL 0x15
71 #define MENELAUS_DETECT_CTRL 0x16
72 #define MENELAUS_INT_MASK1 0x17
73 #define MENELAUS_INT_MASK2 0x18
74 #define MENELAUS_INT_STATUS1 0x19
75 #define MENELAUS_INT_STATUS2 0x1A
76 #define MENELAUS_INT_ACK1 0x1B
77 #define MENELAUS_INT_ACK2 0x1C
78 #define MENELAUS_GPIO_CTRL 0x1D
79 #define MENELAUS_GPIO_IN 0x1E
80 #define MENELAUS_GPIO_OUT 0x1F
81 #define MENELAUS_BBSMS 0x20
82 #define MENELAUS_RTC_CTRL 0x21
83 #define MENELAUS_RTC_UPDATE 0x22
84 #define MENELAUS_RTC_SEC 0x23
85 #define MENELAUS_RTC_MIN 0x24
86 #define MENELAUS_RTC_HR 0x25
87 #define MENELAUS_RTC_DAY 0x26
88 #define MENELAUS_RTC_MON 0x27
89 #define MENELAUS_RTC_YR 0x28
90 #define MENELAUS_RTC_WKDAY 0x29
91 #define MENELAUS_RTC_AL_SEC 0x2A
92 #define MENELAUS_RTC_AL_MIN 0x2B
93 #define MENELAUS_RTC_AL_HR 0x2C
94 #define MENELAUS_RTC_AL_DAY 0x2D
95 #define MENELAUS_RTC_AL_MON 0x2E
96 #define MENELAUS_RTC_AL_YR 0x2F
97 #define MENELAUS_RTC_COMP_MSB 0x30
98 #define MENELAUS_RTC_COMP_LSB 0x31
99 #define MENELAUS_S1_PULL_EN 0x32
100 #define MENELAUS_S1_PULL_DIR 0x33
101 #define MENELAUS_S2_PULL_EN 0x34
102 #define MENELAUS_S2_PULL_DIR 0x35
103 #define MENELAUS_MCT_CTRL1 0x36
104 #define MENELAUS_MCT_CTRL2 0x37
105 #define MENELAUS_MCT_CTRL3 0x38
106 #define MENELAUS_MCT_PIN_ST 0x39
107 #define MENELAUS_DEBOUNCE1 0x3A
109 #define IH_MENELAUS_IRQS 12
110 #define MENELAUS_MMC_S1CD_IRQ 0 /* MMC slot 1 card change */
111 #define MENELAUS_MMC_S2CD_IRQ 1 /* MMC slot 2 card change */
112 #define MENELAUS_MMC_S1D1_IRQ 2 /* MMC DAT1 low in slot 1 */
113 #define MENELAUS_MMC_S2D1_IRQ 3 /* MMC DAT1 low in slot 2 */
114 #define MENELAUS_LOWBAT_IRQ 4 /* Low battery */
115 #define MENELAUS_HOTDIE_IRQ 5 /* Hot die detect */
116 #define MENELAUS_UVLO_IRQ 6 /* UVLO detect */
117 #define MENELAUS_TSHUT_IRQ 7 /* Thermal shutdown */
118 #define MENELAUS_RTCTMR_IRQ 8 /* RTC timer */
119 #define MENELAUS_RTCALM_IRQ 9 /* RTC alarm */
120 #define MENELAUS_RTCERR_IRQ 10 /* RTC error */
121 #define MENELAUS_PSHBTN_IRQ 11 /* Push button */
122 #define MENELAUS_RESERVED12_IRQ 12 /* Reserved */
123 #define MENELAUS_RESERVED13_IRQ 13 /* Reserved */
124 #define MENELAUS_RESERVED14_IRQ 14 /* Reserved */
125 #define MENELAUS_RESERVED15_IRQ 15 /* Reserved */
127 static void menelaus_work(void * _menelaus);
129 /* Initialized by menelaus_init */
130 static unsigned short normal_i2c[] = { MENELAUS_I2C_ADDRESS, I2C_CLIENT_END };
134 struct menelaus_chip {
135 unsigned long initialized;
136 struct semaphore lock;
137 struct i2c_client client;
138 struct work_struct work;
141 void (*mmc_callback)(unsigned long data, u8 mask);
142 unsigned long mmc_callback_data;
145 static struct menelaus_chip menelaus;
147 static void menelaus_write(u8 value, u8 reg)
149 if (i2c_smbus_write_byte_data(&menelaus.client, reg, value) < 0)
150 pr_err("write error");
153 static u8 menelaus_read(u8 reg)
155 int val = i2c_smbus_read_byte_data(&menelaus.client, reg);
158 pr_err("read error");
165 static void menelaus_enable_irq(int irq)
168 menelaus_write(menelaus_read(MENELAUS_INT_MASK2) &
169 ~(1 << (irq - 8)), MENELAUS_INT_MASK2);
171 menelaus_write(menelaus_read(MENELAUS_INT_MASK1) &
172 ~(1 << irq), MENELAUS_INT_MASK1);
175 static void menelaus_disable_irq(int irq)
178 menelaus_write(menelaus_read(MENELAUS_INT_MASK2)
179 | (1 << (irq - 8)), MENELAUS_INT_MASK2);
181 menelaus_write(menelaus_read(MENELAUS_INT_MASK1)
182 | (1 << irq), MENELAUS_INT_MASK1);
185 static void menelaus_ack_irq(int irq)
188 menelaus_write(1 << (irq - 8), MENELAUS_INT_ACK2);
190 menelaus_write(1 << irq, MENELAUS_INT_ACK1);
193 /* Adds a handler for an interrupt. Does not run in interrupt context */
194 static int menelaus_add_irq_work(int irq, void * handler)
196 down(&menelaus.lock);
197 menelaus.handlers[irq] = handler;
198 menelaus_enable_irq(irq);
204 /* Removes handler for an interrupt */
205 static void menelaus_remove_irq_work(int irq)
207 down(&menelaus.lock);
208 menelaus_disable_irq(irq);
209 menelaus.handlers[irq] = NULL;
213 /*-----------------------------------------------------------------------*/
216 * Toggles the MMC slots between open-drain and push-pull mode.
217 * We always set both slots the same way.
219 void menelaus_mmc_opendrain(int enable)
221 unsigned char reg = menelaus_read(MENELAUS_MCT_CTRL1);
228 menelaus_write(reg, MENELAUS_MCT_CTRL1);
230 EXPORT_SYMBOL(menelaus_mmc_opendrain);
233 * Gets scheduled when a card detect interrupt happens. Note that in some cases
234 * this line is wired to card cover switch rather than the card detect switch
235 * in each slot. In this case the cards are not seen by menelaus.
236 * FIXME: Add handling for D1 too
238 static int menelaus_mmc_cd_work(struct menelaus_chip *menelaus)
241 unsigned char card_mask = 0;
243 reg = menelaus_read(MENELAUS_MCT_PIN_ST);
246 card_mask |= (1 << 0);
249 card_mask |= (1 << 1);
251 if (menelaus->mmc_callback)
252 menelaus->mmc_callback(menelaus->mmc_callback_data, card_mask);
257 /* Initializes MMC slots */
258 void menelaus_mmc_register(void (*callback)(unsigned long data, u8 card_mask), unsigned long data)
263 reg = menelaus_read(MENELAUS_DCDC_CTRL1);
265 menelaus_write(reg, MENELAUS_DCDC_CTRL1);
267 reg = menelaus_read(MENELAUS_DCDC_CTRL3);
269 menelaus_write(reg, MENELAUS_DCDC_CTRL3);
271 /* Enable both slots, do not set auto shutdown */
272 reg = menelaus_read(MENELAUS_MCT_CTRL3);
274 menelaus_write(reg, MENELAUS_MCT_CTRL3);
276 /* Enable card detect for both slots, slot 2 powered from DCDC3 */
277 reg = menelaus_read(MENELAUS_MCT_CTRL2);
279 menelaus_write(reg, MENELAUS_MCT_CTRL2);
281 /* Set both slots in open-drain mode, card detect normally closed */
282 reg = menelaus_read(MENELAUS_MCT_CTRL1);
284 menelaus_write(reg, MENELAUS_MCT_CTRL1);
286 /* Set MMC voltage */
287 reg = menelaus_read(MENELAUS_LDO_CTRL7);
289 menelaus_write(reg, MENELAUS_LDO_CTRL7);
291 menelaus.mmc_callback_data = data;
292 menelaus.mmc_callback = callback;
294 menelaus_add_irq_work(MENELAUS_MMC_S1CD_IRQ, menelaus_mmc_cd_work);
295 menelaus_add_irq_work(MENELAUS_MMC_S2CD_IRQ, menelaus_mmc_cd_work);
296 menelaus_add_irq_work(MENELAUS_MMC_S1D1_IRQ, menelaus_mmc_cd_work);
297 menelaus_add_irq_work(MENELAUS_MMC_S2D1_IRQ, menelaus_mmc_cd_work);
299 EXPORT_SYMBOL(menelaus_mmc_register);
301 void menelaus_mmc_remove(void)
303 menelaus_remove_irq_work(MENELAUS_MMC_S1CD_IRQ);
304 menelaus_remove_irq_work(MENELAUS_MMC_S2CD_IRQ);
305 menelaus_remove_irq_work(MENELAUS_MMC_S1D1_IRQ);
306 menelaus_remove_irq_work(MENELAUS_MMC_S2D1_IRQ);
308 menelaus.mmc_callback = NULL;
309 menelaus.mmc_callback_data = 0;
311 /* FIXME: Shutdown MMC components of Menelaus */
313 EXPORT_SYMBOL(menelaus_mmc_remove);
315 /*-----------------------------------------------------------------------*/
317 /* Handles Menelaus interrupts. Does not run in interrupt context */
318 static void menelaus_work(void * _menelaus)
320 struct menelaus_chip *menelaus = _menelaus;
321 int (*handler)(struct menelaus_chip *menelaus);
327 isr = menelaus_read(MENELAUS_INT_STATUS1) |
328 (menelaus_read(MENELAUS_INT_STATUS2) << 8);
333 for (i = 0; i < IH_MENELAUS_IRQS; i++) {
334 if (isr & (1 << i)) {
335 down(&menelaus->lock);
336 menelaus_disable_irq(i);
338 if (menelaus->handlers[i]) {
339 handler = menelaus->handlers[i];
342 menelaus_enable_irq(i);
347 enable_irq(menelaus->irq);
351 * We cannot use I2C in interrupt context, so we just schedule work.
353 static irqreturn_t menelaus_irq(int irq, void *_menelaus, struct pt_regs *regs)
355 struct menelaus_chip *menelaus = _menelaus;
357 disable_irq_nosync(irq);
358 (void)schedule_work(&menelaus->work);
363 static struct i2c_driver menelaus_i2c_driver;
365 static int menelaus_probe(struct i2c_adapter *adapter, int address, int kind)
367 struct i2c_client *c;
371 if (test_and_set_bit(0, &menelaus.initialized))
374 c = &menelaus.client;
375 strncpy(c->name, DRIVER_NAME, sizeof(c->name));
377 c->adapter = adapter;
378 c->driver = &menelaus_i2c_driver;
381 if ((err = i2c_attach_client(c)) < 0) {
382 pr_err("couldn't attach\n");
386 /* If a true probe check the device */
387 if (kind < 0 && (rev = menelaus_read(MENELAUS_REV)) < 0) {
388 pr_err("device not found");
393 /* Most likely Menelaus interrupt is at SYS_NIRQ */
394 omap_cfg_reg(W19_24XX_SYS_NIRQ);
395 menelaus.irq = INT_24XX_SYS_NIRQ;
397 /* Disable all menelaus interrupts */
398 for (i = 0; i < 16; i++) {
400 menelaus_disable_irq(i);
403 err = request_irq(menelaus.irq, menelaus_irq, SA_INTERRUPT,
404 DRIVER_NAME, &menelaus);
406 printk(KERN_ERR "Could not get Menelaus IRQ\n");
408 init_MUTEX(&menelaus.lock);
409 INIT_WORK(&menelaus.work, menelaus_work, &menelaus);
412 pr_info("Menelaus rev %d.%d\n", rev >> 4, rev & 0x0f);
417 i2c_detach_client(c);
419 clear_bit(0, &menelaus.initialized);
423 static int menelaus_remove(struct i2c_client *client)
427 free_irq(menelaus.irq, &menelaus);
429 if ((err = i2c_detach_client(client))) {
430 pr_err("client deregistration failed\n");
434 clear_bit(0, &menelaus.initialized);
439 /*-----------------------------------------------------------------------*/
441 static int menelaus_scan_bus(struct i2c_adapter *bus)
443 if (!i2c_check_functionality(bus, I2C_FUNC_SMBUS_BYTE_DATA |
444 I2C_FUNC_SMBUS_WRITE_BYTE)) {
445 pr_err("invalid i2c bus functionality\n");
449 return i2c_probe(bus, &addr_data, menelaus_probe);
452 static struct i2c_driver menelaus_i2c_driver = {
456 .id = I2C_DRIVERID_MISC, /*FIXME:accroding to i2c-ids.h */
457 .class = I2C_CLASS_HWMON,
458 .attach_adapter = menelaus_scan_bus,
459 .detach_client = menelaus_remove,
462 static int __init menelaus_init(void)
466 if ((res = i2c_add_driver(&menelaus_i2c_driver)) < 0) {
467 pr_err("driver registration failed\n");
474 static void __exit menelaus_exit(void)
476 if (i2c_del_driver(&menelaus_i2c_driver) < 0)
477 pr_err("driver remove failed\n");
479 /* FIXME: Shutdown menelaus parts that can be shut down */
482 MODULE_AUTHOR("Texas Instruments, Inc.");
483 MODULE_DESCRIPTION("I2C interface for Menelaus.");
484 MODULE_LICENSE("GPL");
486 module_init(menelaus_init);
487 module_exit(menelaus_exit);