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>
36 #include <linux/mutex.h>
38 #include <asm/mach-types.h>
39 #include <asm/mach/irq.h>
41 #include <asm/arch/mux.h>
42 #include <asm/arch/gpio.h>
43 #include <asm/arch/menelaus.h>
45 #define DRIVER_NAME "menelaus"
47 #define pr_err(fmt, arg...) printk(KERN_ERR DRIVER_NAME ": ", ## arg);
49 #define MENELAUS_I2C_ADDRESS 0x72
51 #define MENELAUS_REV 0x01
52 #define MENELAUS_VCORE_CTRL1 0x02
53 #define MENELAUS_VCORE_CTRL2 0x03
54 #define MENELAUS_VCORE_CTRL3 0x04
55 #define MENELAUS_VCORE_CTRL4 0x05
56 #define MENELAUS_VCORE_CTRL5 0x06
57 #define MENELAUS_DCDC_CTRL1 0x07
58 #define MENELAUS_DCDC_CTRL2 0x08
59 #define MENELAUS_DCDC_CTRL3 0x09
60 #define MENELAUS_LDO_CTRL1 0x0A
61 #define MENELAUS_LDO_CTRL2 0x0B
62 #define MENELAUS_LDO_CTRL3 0x0C
63 #define MENELAUS_LDO_CTRL4 0x0D
64 #define MENELAUS_LDO_CTRL5 0x0E
65 #define MENELAUS_LDO_CTRL6 0x0F
66 #define MENELAUS_LDO_CTRL7 0x10
67 #define MENELAUS_LDO_CTRL8 0x11
68 #define MENELAUS_SLEEP_CTRL1 0x12
69 #define MENELAUS_SLEEP_CTRL2 0x13
70 #define MENELAUS_DEVICE_OFF 0x14
71 #define MENELAUS_OSC_CTRL 0x15
72 #define MENELAUS_DETECT_CTRL 0x16
73 #define MENELAUS_INT_MASK1 0x17
74 #define MENELAUS_INT_MASK2 0x18
75 #define MENELAUS_INT_STATUS1 0x19
76 #define MENELAUS_INT_STATUS2 0x1A
77 #define MENELAUS_INT_ACK1 0x1B
78 #define MENELAUS_INT_ACK2 0x1C
79 #define MENELAUS_GPIO_CTRL 0x1D
80 #define MENELAUS_GPIO_IN 0x1E
81 #define MENELAUS_GPIO_OUT 0x1F
82 #define MENELAUS_BBSMS 0x20
83 #define MENELAUS_RTC_CTRL 0x21
84 #define MENELAUS_RTC_UPDATE 0x22
85 #define MENELAUS_RTC_SEC 0x23
86 #define MENELAUS_RTC_MIN 0x24
87 #define MENELAUS_RTC_HR 0x25
88 #define MENELAUS_RTC_DAY 0x26
89 #define MENELAUS_RTC_MON 0x27
90 #define MENELAUS_RTC_YR 0x28
91 #define MENELAUS_RTC_WKDAY 0x29
92 #define MENELAUS_RTC_AL_SEC 0x2A
93 #define MENELAUS_RTC_AL_MIN 0x2B
94 #define MENELAUS_RTC_AL_HR 0x2C
95 #define MENELAUS_RTC_AL_DAY 0x2D
96 #define MENELAUS_RTC_AL_MON 0x2E
97 #define MENELAUS_RTC_AL_YR 0x2F
98 #define MENELAUS_RTC_COMP_MSB 0x30
99 #define MENELAUS_RTC_COMP_LSB 0x31
100 #define MENELAUS_S1_PULL_EN 0x32
101 #define MENELAUS_S1_PULL_DIR 0x33
102 #define MENELAUS_S2_PULL_EN 0x34
103 #define MENELAUS_S2_PULL_DIR 0x35
104 #define MENELAUS_MCT_CTRL1 0x36
105 #define MENELAUS_MCT_CTRL2 0x37
106 #define MENELAUS_MCT_CTRL3 0x38
107 #define MENELAUS_MCT_PIN_ST 0x39
108 #define MENELAUS_DEBOUNCE1 0x3A
110 #define IH_MENELAUS_IRQS 12
111 #define MENELAUS_MMC_S1CD_IRQ 0 /* MMC slot 1 card change */
112 #define MENELAUS_MMC_S2CD_IRQ 1 /* MMC slot 2 card change */
113 #define MENELAUS_MMC_S1D1_IRQ 2 /* MMC DAT1 low in slot 1 */
114 #define MENELAUS_MMC_S2D1_IRQ 3 /* MMC DAT1 low in slot 2 */
115 #define MENELAUS_LOWBAT_IRQ 4 /* Low battery */
116 #define MENELAUS_HOTDIE_IRQ 5 /* Hot die detect */
117 #define MENELAUS_UVLO_IRQ 6 /* UVLO detect */
118 #define MENELAUS_TSHUT_IRQ 7 /* Thermal shutdown */
119 #define MENELAUS_RTCTMR_IRQ 8 /* RTC timer */
120 #define MENELAUS_RTCALM_IRQ 9 /* RTC alarm */
121 #define MENELAUS_RTCERR_IRQ 10 /* RTC error */
122 #define MENELAUS_PSHBTN_IRQ 11 /* Push button */
123 #define MENELAUS_RESERVED12_IRQ 12 /* Reserved */
124 #define MENELAUS_RESERVED13_IRQ 13 /* Reserved */
125 #define MENELAUS_RESERVED14_IRQ 14 /* Reserved */
126 #define MENELAUS_RESERVED15_IRQ 15 /* Reserved */
128 static void menelaus_work(void * _menelaus);
130 /* Initialized by menelaus_init */
131 static unsigned short normal_i2c[] = { MENELAUS_I2C_ADDRESS, I2C_CLIENT_END };
135 struct menelaus_chip {
136 unsigned long initialized;
138 struct i2c_client client;
139 struct work_struct work;
142 void (*mmc_callback)(unsigned long data, u8 mask);
143 unsigned long mmc_callback_data;
146 static struct menelaus_chip menelaus;
148 static void menelaus_write(u8 value, u8 reg)
150 if (i2c_smbus_write_byte_data(&menelaus.client, reg, value) < 0)
151 pr_err("write error");
154 static u8 menelaus_read(u8 reg)
156 int val = i2c_smbus_read_byte_data(&menelaus.client, reg);
159 pr_err("read error");
166 static void menelaus_enable_irq(int irq)
169 menelaus_write(menelaus_read(MENELAUS_INT_MASK2) &
170 ~(1 << (irq - 8)), MENELAUS_INT_MASK2);
172 menelaus_write(menelaus_read(MENELAUS_INT_MASK1) &
173 ~(1 << irq), MENELAUS_INT_MASK1);
176 static void menelaus_disable_irq(int irq)
179 menelaus_write(menelaus_read(MENELAUS_INT_MASK2)
180 | (1 << (irq - 8)), MENELAUS_INT_MASK2);
182 menelaus_write(menelaus_read(MENELAUS_INT_MASK1)
183 | (1 << irq), MENELAUS_INT_MASK1);
186 static void menelaus_ack_irq(int irq)
189 menelaus_write(1 << (irq - 8), MENELAUS_INT_ACK2);
191 menelaus_write(1 << irq, MENELAUS_INT_ACK1);
194 /* Adds a handler for an interrupt. Does not run in interrupt context */
195 static int menelaus_add_irq_work(int irq, void * handler)
197 mutex_lock(&menelaus.lock);
198 menelaus.handlers[irq] = handler;
199 menelaus_enable_irq(irq);
200 mutex_unlock(&menelaus.lock);
205 /* Removes handler for an interrupt */
206 static void menelaus_remove_irq_work(int irq)
208 mutex_lock(&menelaus.lock);
209 menelaus_disable_irq(irq);
210 menelaus.handlers[irq] = NULL;
211 mutex_unlock(&menelaus.lock);
214 /*-----------------------------------------------------------------------*/
217 * Toggles the MMC slots between open-drain and push-pull mode.
218 * We always set both slots the same way.
220 void menelaus_mmc_opendrain(int enable)
222 unsigned char reg = menelaus_read(MENELAUS_MCT_CTRL1);
229 menelaus_write(reg, MENELAUS_MCT_CTRL1);
231 EXPORT_SYMBOL(menelaus_mmc_opendrain);
234 * Gets scheduled when a card detect interrupt happens. Note that in some cases
235 * this line is wired to card cover switch rather than the card detect switch
236 * in each slot. In this case the cards are not seen by menelaus.
237 * FIXME: Add handling for D1 too
239 static int menelaus_mmc_cd_work(struct menelaus_chip *menelaus)
242 unsigned char card_mask = 0;
244 reg = menelaus_read(MENELAUS_MCT_PIN_ST);
247 card_mask |= (1 << 0);
250 card_mask |= (1 << 1);
252 if (menelaus->mmc_callback)
253 menelaus->mmc_callback(menelaus->mmc_callback_data, card_mask);
258 /* Initializes MMC slots */
259 void menelaus_mmc_register(void (*callback)(unsigned long data, u8 card_mask), unsigned long data)
264 reg = menelaus_read(MENELAUS_DCDC_CTRL1);
266 menelaus_write(reg, MENELAUS_DCDC_CTRL1);
268 reg = menelaus_read(MENELAUS_DCDC_CTRL3);
270 menelaus_write(reg, MENELAUS_DCDC_CTRL3);
272 /* Enable both slots, do not set auto shutdown */
273 reg = menelaus_read(MENELAUS_MCT_CTRL3);
275 menelaus_write(reg, MENELAUS_MCT_CTRL3);
277 /* Enable card detect for both slots, slot 2 powered from DCDC3 */
278 reg = menelaus_read(MENELAUS_MCT_CTRL2);
280 menelaus_write(reg, MENELAUS_MCT_CTRL2);
282 /* Set both slots in open-drain mode, card detect normally closed */
283 reg = menelaus_read(MENELAUS_MCT_CTRL1);
285 menelaus_write(reg, MENELAUS_MCT_CTRL1);
287 /* Set MMC voltage */
288 reg = menelaus_read(MENELAUS_LDO_CTRL7);
290 menelaus_write(reg, MENELAUS_LDO_CTRL7);
292 menelaus.mmc_callback_data = data;
293 menelaus.mmc_callback = callback;
295 menelaus_add_irq_work(MENELAUS_MMC_S1CD_IRQ, menelaus_mmc_cd_work);
296 menelaus_add_irq_work(MENELAUS_MMC_S2CD_IRQ, menelaus_mmc_cd_work);
297 menelaus_add_irq_work(MENELAUS_MMC_S1D1_IRQ, menelaus_mmc_cd_work);
298 menelaus_add_irq_work(MENELAUS_MMC_S2D1_IRQ, menelaus_mmc_cd_work);
300 EXPORT_SYMBOL(menelaus_mmc_register);
302 void menelaus_mmc_remove(void)
304 menelaus_remove_irq_work(MENELAUS_MMC_S1CD_IRQ);
305 menelaus_remove_irq_work(MENELAUS_MMC_S2CD_IRQ);
306 menelaus_remove_irq_work(MENELAUS_MMC_S1D1_IRQ);
307 menelaus_remove_irq_work(MENELAUS_MMC_S2D1_IRQ);
309 menelaus.mmc_callback = NULL;
310 menelaus.mmc_callback_data = 0;
312 /* FIXME: Shutdown MMC components of Menelaus */
314 EXPORT_SYMBOL(menelaus_mmc_remove);
316 /*-----------------------------------------------------------------------*/
318 /* Handles Menelaus interrupts. Does not run in interrupt context */
319 static void menelaus_work(void * _menelaus)
321 struct menelaus_chip *menelaus = _menelaus;
322 int (*handler)(struct menelaus_chip *menelaus);
328 isr = menelaus_read(MENELAUS_INT_STATUS1) |
329 (menelaus_read(MENELAUS_INT_STATUS2) << 8);
334 for (i = 0; i < IH_MENELAUS_IRQS; i++) {
335 if (isr & (1 << i)) {
336 mutex_lock(&menelaus->lock);
337 menelaus_disable_irq(i);
339 if (menelaus->handlers[i]) {
340 handler = menelaus->handlers[i];
343 menelaus_enable_irq(i);
344 mutex_unlock(&menelaus->lock);
348 enable_irq(menelaus->irq);
352 * We cannot use I2C in interrupt context, so we just schedule work.
354 static irqreturn_t menelaus_irq(int irq, void *_menelaus, struct pt_regs *regs)
356 struct menelaus_chip *menelaus = _menelaus;
358 disable_irq_nosync(irq);
359 (void)schedule_work(&menelaus->work);
364 static struct i2c_driver menelaus_i2c_driver;
366 static int menelaus_probe(struct i2c_adapter *adapter, int address, int kind)
368 struct i2c_client *c;
372 if (test_and_set_bit(0, &menelaus.initialized))
375 c = &menelaus.client;
376 strncpy(c->name, DRIVER_NAME, sizeof(c->name));
378 c->adapter = adapter;
379 c->driver = &menelaus_i2c_driver;
382 if ((err = i2c_attach_client(c)) < 0) {
383 pr_err("couldn't attach\n");
387 /* If a true probe check the device */
388 if (kind < 0 && (rev = menelaus_read(MENELAUS_REV)) < 0) {
389 pr_err("device not found");
394 /* Most likely Menelaus interrupt is at SYS_NIRQ */
395 omap_cfg_reg(W19_24XX_SYS_NIRQ);
396 menelaus.irq = INT_24XX_SYS_NIRQ;
398 /* Disable all menelaus interrupts */
399 for (i = 0; i < 16; i++) {
401 menelaus_disable_irq(i);
404 err = request_irq(menelaus.irq, menelaus_irq, SA_INTERRUPT,
405 DRIVER_NAME, &menelaus);
407 printk(KERN_ERR "Could not get Menelaus IRQ\n");
409 mutex_init(&menelaus.lock);
410 INIT_WORK(&menelaus.work, menelaus_work, &menelaus);
413 pr_info("Menelaus rev %d.%d\n", rev >> 4, rev & 0x0f);
418 i2c_detach_client(c);
420 clear_bit(0, &menelaus.initialized);
424 static int menelaus_remove(struct i2c_client *client)
428 free_irq(menelaus.irq, &menelaus);
430 if ((err = i2c_detach_client(client))) {
431 pr_err("client deregistration failed\n");
435 clear_bit(0, &menelaus.initialized);
440 /*-----------------------------------------------------------------------*/
442 static int menelaus_scan_bus(struct i2c_adapter *bus)
444 if (!i2c_check_functionality(bus, I2C_FUNC_SMBUS_BYTE_DATA |
445 I2C_FUNC_SMBUS_WRITE_BYTE)) {
446 pr_err("invalid i2c bus functionality\n");
450 return i2c_probe(bus, &addr_data, menelaus_probe);
453 static struct i2c_driver menelaus_i2c_driver = {
457 .id = I2C_DRIVERID_MISC, /*FIXME:accroding to i2c-ids.h */
458 .class = I2C_CLASS_HWMON,
459 .attach_adapter = menelaus_scan_bus,
460 .detach_client = menelaus_remove,
463 static int __init menelaus_init(void)
467 if ((res = i2c_add_driver(&menelaus_i2c_driver)) < 0) {
468 pr_err("driver registration failed\n");
475 static void __exit menelaus_exit(void)
477 if (i2c_del_driver(&menelaus_i2c_driver) < 0)
478 pr_err("driver remove failed\n");
480 /* FIXME: Shutdown menelaus parts that can be shut down */
483 MODULE_AUTHOR("Texas Instruments, Inc.");
484 MODULE_DESCRIPTION("I2C interface for Menelaus.");
485 MODULE_LICENSE("GPL");
487 module_init(menelaus_init);
488 module_exit(menelaus_exit);