]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/i2c/chips/menelaus.c
[PATCH] ARM: OMAP: Menelaus: sem2mutex conversion
[linux-2.6-omap-h63xx.git] / drivers / i2c / chips / menelaus.c
1 /*
2  * drivers/i2c/chips/menelaus.c
3  *
4  * Copyright (C) 2004 Texas Instruments, Inc.
5  *
6  * Some parts based tps65010.c:
7  * Copyright (C) 2004 Texas Instruments and
8  * Copyright (C) 2004-2005 David Brownell
9  *
10  * Some parts based on tlv320aic24.c:
11  * Copyright (C) by Kai Svahn <kai.svahn@nokia.com>
12  *
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
16  *
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.
21  *
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.
26  *
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
30  */
31
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>
37
38 #include <asm/mach-types.h>
39 #include <asm/mach/irq.h>
40
41 #include <asm/arch/mux.h>
42 #include <asm/arch/gpio.h>
43 #include <asm/arch/menelaus.h>
44
45 #define DRIVER_NAME                     "menelaus"
46
47 #define pr_err(fmt, arg...)     printk(KERN_ERR DRIVER_NAME ": ", ## arg);
48
49 #define MENELAUS_I2C_ADDRESS            0x72
50
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
109
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 */
127
128 static void menelaus_work(void * _menelaus);
129
130 /* Initialized by menelaus_init */
131 static unsigned short normal_i2c[] = { MENELAUS_I2C_ADDRESS, I2C_CLIENT_END };
132
133 I2C_CLIENT_INSMOD;
134
135 struct menelaus_chip {
136         unsigned long           initialized;
137         struct mutex            lock;
138         struct i2c_client       client;
139         struct work_struct      work;
140         int                     irq;
141         void                    *handlers[16];
142         void                    (*mmc_callback)(unsigned long data, u8 mask);
143         unsigned long           mmc_callback_data;
144 };
145
146 static struct menelaus_chip menelaus;
147
148 static void menelaus_write(u8 value, u8 reg)
149 {
150         if (i2c_smbus_write_byte_data(&menelaus.client, reg, value) < 0)
151                 pr_err("write error");
152 }
153
154 static u8 menelaus_read(u8 reg)
155 {
156         int val = i2c_smbus_read_byte_data(&menelaus.client, reg);
157
158         if (val < 0) {
159                 pr_err("read error");
160                 return 0;
161         }
162
163         return val;
164 }
165
166 static void menelaus_enable_irq(int irq)
167 {
168         if (irq > 7)
169                 menelaus_write(menelaus_read(MENELAUS_INT_MASK2) &
170                                ~(1 << (irq - 8)), MENELAUS_INT_MASK2);
171         else
172                 menelaus_write(menelaus_read(MENELAUS_INT_MASK1) &
173                                ~(1 << irq), MENELAUS_INT_MASK1);
174 }
175
176 static void menelaus_disable_irq(int irq)
177 {
178         if (irq > 7)
179                 menelaus_write(menelaus_read(MENELAUS_INT_MASK2)
180                                | (1 << (irq - 8)), MENELAUS_INT_MASK2);
181         else
182                 menelaus_write(menelaus_read(MENELAUS_INT_MASK1)
183                                | (1 << irq), MENELAUS_INT_MASK1);
184 }
185
186 static void menelaus_ack_irq(int irq)
187 {
188         if (irq > 7)
189                 menelaus_write(1 << (irq - 8), MENELAUS_INT_ACK2);
190         else
191                 menelaus_write(1 << irq, MENELAUS_INT_ACK1);    
192 }
193
194 /* Adds a handler for an interrupt. Does not run in interrupt context */
195 static int menelaus_add_irq_work(int irq, void * handler)
196 {
197         mutex_lock(&menelaus.lock);
198         menelaus.handlers[irq] = handler;
199         menelaus_enable_irq(irq);
200         mutex_unlock(&menelaus.lock);
201
202         return 0;
203 }
204
205 /* Removes handler for an interrupt */
206 static void menelaus_remove_irq_work(int irq)
207 {
208         mutex_lock(&menelaus.lock);
209         menelaus_disable_irq(irq);
210         menelaus.handlers[irq] = NULL;
211         mutex_unlock(&menelaus.lock);
212 }
213
214 /*-----------------------------------------------------------------------*/
215
216 /*
217  * Toggles the MMC slots between open-drain and push-pull mode.
218  * We always set both slots the same way.
219  */
220 void menelaus_mmc_opendrain(int enable)
221 {
222         unsigned char reg = menelaus_read(MENELAUS_MCT_CTRL1);
223
224         if (enable)
225                 reg |= (0x3 << 2);
226         else
227                 reg &= ~(0x3 << 2);
228
229         menelaus_write(reg, MENELAUS_MCT_CTRL1);
230 }
231 EXPORT_SYMBOL(menelaus_mmc_opendrain);
232
233 /*
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
238  */
239 static int menelaus_mmc_cd_work(struct menelaus_chip *menelaus)
240 {
241         unsigned char reg;
242         unsigned char card_mask = 0;
243
244         reg = menelaus_read(MENELAUS_MCT_PIN_ST);
245
246         if (!(reg & 0x1))
247                 card_mask |= (1 << 0);
248
249         if (!(reg & 0x2))
250                 card_mask |= (1 << 1);
251
252         if (menelaus->mmc_callback)
253                 menelaus->mmc_callback(menelaus->mmc_callback_data, card_mask);
254
255         return 0;
256 }
257
258 /* Initializes MMC slots */
259 void menelaus_mmc_register(void (*callback)(unsigned long data, u8 card_mask), unsigned long data)
260 {
261         int reg;
262
263         /* DCDC3 to 3V */
264         reg = menelaus_read(MENELAUS_DCDC_CTRL1);
265         reg |= 0x6 << 4;
266         menelaus_write(reg, MENELAUS_DCDC_CTRL1);
267
268         reg = menelaus_read(MENELAUS_DCDC_CTRL3);
269         reg |= 0x6;
270         menelaus_write(reg, MENELAUS_DCDC_CTRL3);
271
272         /* Enable both slots, do not set auto shutdown */
273         reg = menelaus_read(MENELAUS_MCT_CTRL3);
274         reg |= 0x3;
275         menelaus_write(reg, MENELAUS_MCT_CTRL3);
276
277         /* Enable card detect for both slots, slot 2 powered from DCDC3 */
278         reg = menelaus_read(MENELAUS_MCT_CTRL2);
279         reg |= 0xf0;
280         menelaus_write(reg, MENELAUS_MCT_CTRL2);
281
282         /* Set both slots in open-drain mode, card detect normally closed */
283         reg = menelaus_read(MENELAUS_MCT_CTRL1);
284         reg |= 0xfc;
285         menelaus_write(reg, MENELAUS_MCT_CTRL1);
286
287         /* Set MMC voltage */
288         reg = menelaus_read(MENELAUS_LDO_CTRL7);
289         reg |= 0x03;
290         menelaus_write(reg, MENELAUS_LDO_CTRL7);
291
292         menelaus.mmc_callback_data = data;
293         menelaus.mmc_callback = callback;
294
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);
299 }
300 EXPORT_SYMBOL(menelaus_mmc_register);
301
302 void menelaus_mmc_remove(void)
303 {
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);
308
309         menelaus.mmc_callback = NULL;
310         menelaus.mmc_callback_data = 0;
311
312         /* FIXME: Shutdown MMC components of Menelaus */
313 }
314 EXPORT_SYMBOL(menelaus_mmc_remove);
315
316 /*-----------------------------------------------------------------------*/
317
318 /* Handles Menelaus interrupts. Does not run in interrupt context */
319 static void menelaus_work(void * _menelaus)
320 {
321         struct menelaus_chip *menelaus = _menelaus;
322         int (*handler)(struct menelaus_chip *menelaus);
323  
324         while (1) {
325                 int i;
326                 unsigned char isr;
327
328                 isr = menelaus_read(MENELAUS_INT_STATUS1) |
329                       (menelaus_read(MENELAUS_INT_STATUS2) << 8);
330
331                 if (!isr)
332                         break;
333
334                 for (i = 0; i < IH_MENELAUS_IRQS; i++) {
335                         if (isr & (1 << i)) {
336                                 mutex_lock(&menelaus->lock);
337                                 menelaus_disable_irq(i);
338                                 menelaus_ack_irq(i);
339                                 if (menelaus->handlers[i]) {
340                                         handler = menelaus->handlers[i];
341                                         handler(menelaus);
342                                 }
343                                 menelaus_enable_irq(i);
344                                 mutex_unlock(&menelaus->lock);
345                         }
346                 }
347         }
348         enable_irq(menelaus->irq);
349 }
350
351 /*
352  * We cannot use I2C in interrupt context, so we just schedule work.
353  */
354 static irqreturn_t menelaus_irq(int irq, void *_menelaus, struct pt_regs *regs)
355 {
356         struct menelaus_chip *menelaus = _menelaus;
357
358         disable_irq_nosync(irq);
359         (void)schedule_work(&menelaus->work);
360
361         return IRQ_HANDLED;
362 }
363
364 static struct i2c_driver menelaus_i2c_driver;
365
366 static int menelaus_probe(struct i2c_adapter *adapter, int address, int kind)
367 {
368         struct i2c_client       *c;
369         int                     rev = 0;
370         int                     err = 0, i;
371
372         if (test_and_set_bit(0, &menelaus.initialized))
373                 return -EBUSY;
374
375         c = &menelaus.client;
376         strncpy(c->name, DRIVER_NAME, sizeof(c->name));
377         c->addr         = address;
378         c->adapter      = adapter;
379         c->driver       = &menelaus_i2c_driver;
380         c->flags        = 0;
381
382         if ((err = i2c_attach_client(c)) < 0) {
383                 pr_err("couldn't attach\n");
384                 goto fail1;
385         }
386
387         /* If a true probe check the device */
388         if (kind < 0 && (rev = menelaus_read(MENELAUS_REV)) < 0) {
389                 pr_err("device not found");
390                 err = -ENODEV;
391                 goto fail2;
392         }
393
394         /* Most likely Menelaus interrupt is at SYS_NIRQ */
395         omap_cfg_reg(W19_24XX_SYS_NIRQ);
396         menelaus.irq = INT_24XX_SYS_NIRQ;
397
398         /* Disable all menelaus interrupts */
399         for (i = 0; i < 16; i++) {
400                 menelaus_ack_irq(i);
401                 menelaus_disable_irq(i);
402         }
403
404         err = request_irq(menelaus.irq, menelaus_irq, SA_INTERRUPT,
405                           DRIVER_NAME, &menelaus);
406         if (err)
407                 printk(KERN_ERR "Could not get Menelaus IRQ\n");
408
409         mutex_init(&menelaus.lock);
410         INIT_WORK(&menelaus.work, menelaus_work, &menelaus);
411
412         if (kind < 0)
413                 pr_info("Menelaus rev %d.%d\n", rev >> 4, rev & 0x0f);
414
415         return 0;
416
417 fail2:
418         i2c_detach_client(c);
419 fail1:
420         clear_bit(0, &menelaus.initialized);
421         return err;
422 }
423
424 static int menelaus_remove(struct i2c_client *client)
425 {
426         int err;
427
428         free_irq(menelaus.irq, &menelaus);
429
430         if ((err = i2c_detach_client(client))) {
431                 pr_err("client deregistration failed\n");
432                 return err;
433         }
434
435         clear_bit(0, &menelaus.initialized);
436
437         return 0;
438 }
439
440 /*-----------------------------------------------------------------------*/
441
442 static int menelaus_scan_bus(struct i2c_adapter *bus)
443 {
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");
447                 return -EINVAL;
448         }
449
450         return i2c_probe(bus, &addr_data, menelaus_probe);
451 }
452
453 static struct i2c_driver menelaus_i2c_driver = {
454         .driver = {
455                 .name           = DRIVER_NAME,
456         },
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,
461 };
462
463 static int __init menelaus_init(void)
464 {
465         int res;
466
467         if ((res = i2c_add_driver(&menelaus_i2c_driver)) < 0) {
468                 pr_err("driver registration failed\n");
469                 return res;
470         }
471
472         return 0;
473 }
474
475 static void __exit menelaus_exit(void)
476 {
477         if (i2c_del_driver(&menelaus_i2c_driver) < 0)
478                 pr_err("driver remove failed\n");
479
480         /* FIXME: Shutdown menelaus parts that can be shut down */
481 }
482
483 MODULE_AUTHOR("Texas Instruments, Inc.");
484 MODULE_DESCRIPTION("I2C interface for Menelaus.");
485 MODULE_LICENSE("GPL");
486
487 module_init(menelaus_init);
488 module_exit(menelaus_exit);