]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/cbus/retu.c
Add Nokia CBUS support
[linux-2.6-omap-h63xx.git] / drivers / cbus / retu.c
1 /**
2  * drivers/cbus/retu.c
3  *
4  * Support functions for Retu ASIC
5  *
6  * Copyright (C) 2004, 2005 Nokia Corporation
7  *
8  * Written by Juha Yrjölä <juha.yrjola@nokia.com>,
9  *            David Weinehall <david.weinehall@nokia.com>, and
10  *            Mikko Ylinen <mikko.k.ylinen@nokia.com>
11  *
12  * This file is subject to the terms and conditions of the GNU General
13  * Public License. See the file "COPYING" in the main directory of this
14  * archive for more details.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  */
25
26 #include <linux/module.h>
27 #include <linux/init.h>
28
29 #include <linux/kernel.h>
30 #include <linux/errno.h>
31 #include <linux/device.h>
32 #include <linux/miscdevice.h>
33 #include <linux/poll.h>
34 #include <linux/fs.h>
35 #include <linux/irq.h>
36 #include <linux/interrupt.h>
37 #include <linux/platform_device.h>
38
39 #include <asm/uaccess.h>
40
41 #include <asm/arch/mux.h>
42 #include <asm/arch/gpio.h>
43 #include <asm/arch/board.h>
44
45 #include "cbus.h"
46 #include "retu.h"
47
48 #define RETU_ID                 0x01
49 #define PFX                     "retu: "
50
51 static int retu_initialized;
52 static int retu_irq_pin;
53 static int retu_is_vilma;
54
55 static struct tasklet_struct retu_tasklet;
56 spinlock_t retu_lock = SPIN_LOCK_UNLOCKED;
57
58 static struct completion device_release;
59
60 struct retu_irq_handler_desc {
61         int (*func)(unsigned long);
62         unsigned long arg;
63         char name[8];
64 };
65
66 static struct retu_irq_handler_desc retu_irq_handlers[MAX_RETU_IRQ_HANDLERS];
67
68 /**
69  * retu_read_reg - Read a value from a register in Retu
70  * @reg: the register to read from
71  *
72  * This function returns the contents of the specified register
73  */
74 int retu_read_reg(int reg)
75 {
76         BUG_ON(!retu_initialized);
77         return cbus_read_reg(cbus_host, RETU_ID, reg);
78 }
79
80 /**
81  * retu_write_reg - Write a value to a register in Retu
82  * @reg: the register to write to
83  * @reg: the value to write to the register
84  *
85  * This function writes a value to the specified register
86  */
87 void retu_write_reg(int reg, u16 val)
88 {
89         BUG_ON(!retu_initialized);
90         cbus_write_reg(cbus_host, RETU_ID, reg, val);
91 }
92
93 void retu_set_clear_reg_bits(int reg, u16 set, u16 clear)
94 {
95         unsigned long flags;
96         u16 w;
97
98         spin_lock_irqsave(&retu_lock, flags);
99         w = retu_read_reg(reg);
100         w &= ~clear;
101         w |= set;
102         retu_write_reg(reg, w);
103         spin_unlock_irqrestore(&retu_lock, flags);
104 }
105
106 #define ADC_MAX_CHAN_NUMBER     13
107
108 int retu_read_adc(int channel)
109 {
110         unsigned long flags;
111         int res;
112
113         if (channel < 0 || channel > ADC_MAX_CHAN_NUMBER)
114                 return -EINVAL;
115
116         spin_lock_irqsave(&retu_lock, flags);
117         /* Select the channel and read result */
118         retu_write_reg(RETU_REG_ADCR, channel << 10);
119         res = retu_read_reg(RETU_REG_ADCR) & 0x3ff;
120         /* Unlock retu */
121         spin_unlock_irqrestore(&retu_lock, flags);
122
123         return res;
124 }
125
126
127 static u16 retu_disable_bogus_irqs(u16 mask)
128 {
129        int i;
130
131        for (i = 0; i < MAX_RETU_IRQ_HANDLERS; i++) {
132                if (mask & (1 << i))
133                        continue;
134                if (retu_irq_handlers[i].func != NULL)
135                        continue;
136                /* an IRQ was enabled but we don't have a handler for it */
137                printk(KERN_INFO PFX "disabling bogus IRQ %d\n", i);
138                mask |= (1 << i);
139        }
140        return mask;
141 }
142
143 /*
144  * Disable given RETU interrupt
145  */
146 void retu_disable_irq(int id)
147 {
148         unsigned long flags;
149         u16 mask;
150
151         spin_lock_irqsave(&retu_lock, flags);
152         mask = retu_read_reg(RETU_REG_IMR);
153         mask |= 1 << id;
154         mask = retu_disable_bogus_irqs(mask);
155         retu_write_reg(RETU_REG_IMR, mask);
156         spin_unlock_irqrestore(&retu_lock, flags);
157 }
158
159 /*
160  * Enable given RETU interrupt
161  */
162 void retu_enable_irq(int id)
163 {
164         unsigned long flags;
165         u16 mask;
166
167         if (id == 3) {
168                 printk("Enabling Retu IRQ %d\n", id);
169                 dump_stack();
170         }
171         spin_lock_irqsave(&retu_lock, flags);
172         mask = retu_read_reg(RETU_REG_IMR);
173         mask &= ~(1 << id);
174         mask = retu_disable_bogus_irqs(mask);
175         retu_write_reg(RETU_REG_IMR, mask);
176         spin_unlock_irqrestore(&retu_lock, flags);
177 }
178
179 /*
180  * Acknowledge given RETU interrupt
181  */
182 void retu_ack_irq(int id)
183 {
184         retu_write_reg(RETU_REG_IDR, 1 << id);
185 }
186
187 /*
188  * RETU interrupt handler. Only schedules the tasklet.
189  */
190 static irqreturn_t retu_irq_handler(int irq, void *dev_id)
191 {
192         tasklet_schedule(&retu_tasklet);
193         return IRQ_HANDLED;
194 }
195
196 /*
197  * Tasklet handler
198  */
199 static void retu_tasklet_handler(unsigned long data)
200 {
201         struct retu_irq_handler_desc *hnd;
202         u16 id;
203         u16 im;
204         int i;
205
206         for (;;) {
207                 id = retu_read_reg(RETU_REG_IDR);
208                 im = ~retu_read_reg(RETU_REG_IMR);
209                 id &= im;
210
211                 if (!id)
212                         break;
213
214                 for (i = 0; id != 0; i++, id >>= 1) {
215                         if (!(id & 1))
216                                 continue;
217                         hnd = &retu_irq_handlers[i];
218                         if (hnd->func == NULL) {
219                                /* Spurious retu interrupt - disable and ack it */
220                                 printk(KERN_INFO "Spurious Retu interrupt "
221                                                  "(id %d)\n", i);
222                                 retu_disable_irq(i);
223                                 retu_ack_irq(i);
224                                 continue;
225                         }
226                         hnd->func(hnd->arg);
227                         /*
228                          * Don't acknowledge the interrupt here
229                          * It must be done explicitly
230                          */
231                 }
232         }
233 }
234
235 /*
236  * Register the handler for a given RETU interrupt source.
237  */
238 int retu_request_irq(int id, void *irq_handler, unsigned long arg, char *name)
239 {
240         struct retu_irq_handler_desc *hnd;
241
242         if (irq_handler == NULL || id >= MAX_RETU_IRQ_HANDLERS ||
243             name == NULL) {
244                 printk(KERN_ERR PFX "Invalid arguments to %s\n",
245                        __FUNCTION__);
246                 return -EINVAL;
247         }
248         hnd = &retu_irq_handlers[id];
249         if (hnd->func != NULL) {
250                 printk(KERN_ERR PFX "IRQ %d already reserved\n", id);
251                 return -EBUSY;
252         }
253         printk(KERN_INFO PFX "Registering interrupt %d for device %s\n",
254                id, name);
255         hnd->func = irq_handler;
256         hnd->arg = arg;
257         strlcpy(hnd->name, name, sizeof(hnd->name));
258
259         retu_ack_irq(id);
260         retu_enable_irq(id);
261
262         return 0;
263 }
264
265 /*
266  * Unregister the handler for a given RETU interrupt source.
267  */
268 void retu_free_irq(int id)
269 {
270         struct retu_irq_handler_desc *hnd;
271
272         if (id >= MAX_RETU_IRQ_HANDLERS) {
273                 printk(KERN_ERR PFX "Invalid argument to %s\n",
274                        __FUNCTION__);
275                 return;
276         }
277         hnd = &retu_irq_handlers[id];
278         if (hnd->func == NULL) {
279                 printk(KERN_ERR PFX "IRQ %d already freed\n", id);
280                 return;
281         }
282
283         retu_disable_irq(id);
284         hnd->func = NULL;
285 }
286
287 /**
288  * retu_power_off - Shut down power to system
289  *
290  * This function puts the system in power off state
291  */
292 static void retu_power_off(void)
293 {
294         /* Ignore power button state */
295         retu_write_reg(RETU_REG_CC1, retu_read_reg(RETU_REG_CC1) | 2);
296         /* Expire watchdog immediately */
297         retu_write_reg(RETU_REG_WATCHDOG, 0);
298         /* Wait for poweroff*/
299         for (;;);
300 }
301
302 /**
303  * retu_probe - Probe for Retu ASIC
304  * @dev: the Retu device
305  *
306  * Probe for the Retu ASIC and allocate memory
307  * for its device-struct if found
308  */
309 static int __devinit retu_probe(struct device *dev)
310 {
311         const struct omap_em_asic_bb5_config * em_asic_config;
312         int rev, ret;
313
314         /* Prepare tasklet */
315         tasklet_init(&retu_tasklet, retu_tasklet_handler, 0);
316
317         em_asic_config = omap_get_config(OMAP_TAG_EM_ASIC_BB5,
318                                          struct omap_em_asic_bb5_config);
319         if (em_asic_config == NULL) {
320                 printk(KERN_ERR PFX "Unable to retrieve config data\n");
321                 return -ENODATA;
322         }
323
324         retu_irq_pin = em_asic_config->retu_irq_gpio;
325
326         if ((ret = omap_request_gpio(retu_irq_pin)) < 0) {
327                 printk(KERN_ERR PFX "Unable to reserve IRQ GPIO\n");
328                 return ret;
329         }
330
331         /* Set the pin as input */
332         omap_set_gpio_direction(retu_irq_pin, 1);
333
334         /* Rising edge triggers the IRQ */
335         set_irq_type(OMAP_GPIO_IRQ(retu_irq_pin), IRQT_RISING);
336
337         retu_initialized = 1;
338
339         rev = retu_read_reg(RETU_REG_ASICR) & 0xff;
340         if (rev & (1 << 7))
341                 retu_is_vilma = 1;
342
343         printk(KERN_INFO "%s v%d.%d found\n", retu_is_vilma ? "Vilma" : "Retu",
344                (rev >> 4) & 0x07, rev & 0x0f);
345
346         /* Mask all RETU interrupts */
347         retu_write_reg(RETU_REG_IMR, 0xffff);
348
349         ret = request_irq(OMAP_GPIO_IRQ(retu_irq_pin), retu_irq_handler, 0,
350                           "retu", 0);
351         if (ret < 0) {
352                 printk(KERN_ERR PFX "Unable to register IRQ handler\n");
353                 omap_free_gpio(retu_irq_pin);
354                 return ret;
355         }
356         set_irq_wake(OMAP_GPIO_IRQ(retu_irq_pin), 1);
357
358         /* Register power off function */
359         pm_power_off = retu_power_off;
360
361 #ifdef CONFIG_CBUS_RETU_USER
362         /* Initialize user-space interface */
363         if (retu_user_init() < 0) {
364                 printk(KERN_ERR "Unable to initialize driver\n");
365                 free_irq(OMAP_GPIO_IRQ(retu_irq_pin), 0);
366                 omap_free_gpio(retu_irq_pin);
367                 return ret;
368         }
369 #endif
370
371         return 0;
372 }
373
374 static int retu_remove(struct device *dev)
375 {
376 #ifdef CONFIG_CBUS_RETU_USER
377         retu_user_cleanup();
378 #endif
379         /* Mask all RETU interrupts */
380         retu_write_reg(RETU_REG_IMR, 0xffff);
381         free_irq(OMAP_GPIO_IRQ(retu_irq_pin), 0);
382         omap_free_gpio(retu_irq_pin);
383         tasklet_kill(&retu_tasklet);
384
385         return 0;
386 }
387
388 static void retu_device_release(struct device *dev)
389 {
390         complete(&device_release);
391 }
392
393 static struct device_driver retu_driver = {
394         .name           = "retu",
395         .bus            = &platform_bus_type,
396         .probe          = retu_probe,
397         .remove         = retu_remove,
398 };
399
400 static struct platform_device retu_device = {
401         .name           = "retu",
402         .id             = -1,
403         .dev = {
404                 .release = retu_device_release,
405         }
406 };
407
408 /**
409  * retu_init - initialise Retu driver
410  *
411  * Initialise the Retu driver and return 0 if everything worked ok
412  */
413 static int __init retu_init(void)
414 {
415         int ret = 0;
416
417         printk(KERN_INFO "Retu/Vilma driver initialising\n");
418
419         init_completion(&device_release);
420
421         if ((ret = driver_register(&retu_driver)) < 0)
422                 return ret;
423
424         if ((ret = platform_device_register(&retu_device)) < 0) {
425                 driver_unregister(&retu_driver);
426                 return ret;
427         }
428         return 0;
429 }
430
431 /*
432  * Cleanup
433  */
434 static void __exit retu_exit(void)
435 {
436         platform_device_unregister(&retu_device);
437         driver_unregister(&retu_driver);
438         wait_for_completion(&device_release);
439 }
440
441 EXPORT_SYMBOL(retu_request_irq);
442 EXPORT_SYMBOL(retu_free_irq);
443 EXPORT_SYMBOL(retu_enable_irq);
444 EXPORT_SYMBOL(retu_disable_irq);
445 EXPORT_SYMBOL(retu_ack_irq);
446 EXPORT_SYMBOL(retu_read_reg);
447 EXPORT_SYMBOL(retu_write_reg);
448
449 subsys_initcall(retu_init);
450 module_exit(retu_exit);
451
452 MODULE_DESCRIPTION("Retu ASIC control");
453 MODULE_LICENSE("GPL");
454 MODULE_AUTHOR("Juha Yrjölä, David Weinehall, and Mikko Ylinen");