omap24xx: Add n8x0 specific GPIO switch code
[openwrt.git] / target / linux / omap24xx / patches-2.6.36 / 500-cbus.patch
1 ---
2  arch/arm/Kconfig               |    4 
3  drivers/Makefile               |    2 
4  drivers/cbus/Kconfig           |   89 ++++
5  drivers/cbus/Makefile          |   14 
6  drivers/cbus/cbus.c            |  309 ++++++++++++++++
7  drivers/cbus/cbus.h            |   36 +
8  drivers/cbus/retu-headset.c    |  356 ++++++++++++++++++
9  drivers/cbus/retu-pwrbutton.c  |  118 ++++++
10  drivers/cbus/retu-rtc.c        |  477 ++++++++++++++++++++++++
11  drivers/cbus/retu-user.c       |  425 ++++++++++++++++++++++
12  drivers/cbus/retu-wdt.c        |  388 ++++++++++++++++++++
13  drivers/cbus/retu.c            |  468 ++++++++++++++++++++++++
14  drivers/cbus/retu.h            |   77 ++++
15  drivers/cbus/tahvo-usb.c       |  788 +++++++++++++++++++++++++++++++++++++++++
16  drivers/cbus/tahvo-user.c      |  407 +++++++++++++++++++++
17  drivers/cbus/tahvo.c           |  443 +++++++++++++++++++++++
18  drivers/cbus/tahvo.h           |   61 +++
19  drivers/cbus/user_retu_tahvo.h |   75 +++
20  18 files changed, 4536 insertions(+), 1 deletion(-)
21
22 --- /dev/null
23 +++ linux-2.6.36-rc4/drivers/cbus/cbus.c
24 @@ -0,0 +1,309 @@
25 +/*
26 + * drivers/cbus/cbus.c
27 + *
28 + * Support functions for CBUS serial protocol
29 + *
30 + * Copyright (C) 2004, 2005 Nokia Corporation
31 + *
32 + * Written by Juha Yrjölä <juha.yrjola@nokia.com>,
33 + *           David Weinehall <david.weinehall@nokia.com>, and
34 + *           Mikko Ylinen <mikko.k.ylinen@nokia.com>
35 + *
36 + * This file is subject to the terms and conditions of the GNU General
37 + * Public License. See the file "COPYING" in the main directory of this
38 + * archive for more details.
39 + *
40 + * This program is distributed in the hope that it will be useful,
41 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
42 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
43 + * GNU General Public License for more details.
44 + *
45 + * You should have received a copy of the GNU General Public License
46 + * along with this program; if not, write to the Free Software
47 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
48 + */
49 +
50 +#include <linux/device.h>
51 +#include <linux/init.h>
52 +#include <linux/kernel.h>
53 +#include <linux/delay.h>
54 +#include <linux/spinlock.h>
55 +#include <linux/gpio.h>
56 +#include <linux/platform_device.h>
57 +#include <linux/slab.h>
58 +
59 +#include <asm/io.h>
60 +#include <asm/mach-types.h>
61 +
62 +#include <plat/board.h>
63 +#include <plat/cbus.h>
64 +
65 +#include "cbus.h"
66 +
67 +struct cbus_host *cbus_host = NULL;
68 +EXPORT_SYMBOL(cbus_host);
69 +
70 +#ifdef CONFIG_ARCH_OMAP1
71 +/* We use our own MPUIO functions to get closer to 1MHz bus speed */
72 +
73 +static inline void cbus_set_gpio_direction(u32 base, int mpuio, int is_input)
74 +{
75 +       u16 w;
76 +
77 +       mpuio &= 0x0f;
78 +       w = __raw_readw(base + OMAP_MPUIO_IO_CNTL);
79 +       if (is_input)
80 +               w |= 1 << mpuio;
81 +       else
82 +               w &= ~(1 << mpuio);
83 +       __raw_writew(w, base + OMAP_MPUIO_IO_CNTL);
84 +
85 +}
86 +
87 +static inline void cbus_set_gpio_dataout(u32 base, int mpuio, int enable)
88 +{
89 +       u16 w;
90 +
91 +       mpuio &= 0x0f;
92 +       w = __raw_readw(base + OMAP_MPUIO_OUTPUT);
93 +       if (enable)
94 +               w |= 1 << mpuio;
95 +       else
96 +               w &= ~(1 << mpuio);
97 +       __raw_writew(w, base + OMAP_MPUIO_OUTPUT);
98 +}
99 +
100 +static inline int cbus_get_gpio_datain(u32 base, int mpuio)
101 +{
102 +       mpuio &= 0x0f;
103 +
104 +       return (__raw_readw(base + OMAP_MPUIO_INPUT_LATCH) & (1 << mpuio)) != 0;
105 +}
106 +
107 +static void cbus_send_bit(struct cbus_host *host, u32 base, int bit,
108 +                         int set_to_input)
109 +{
110 +       cbus_set_gpio_dataout(base, host->dat_gpio, bit ? 1 : 0);
111 +       cbus_set_gpio_dataout(base, host->clk_gpio, 1);
112 +
113 +       /* The data bit is read on the rising edge of CLK */
114 +       if (set_to_input)
115 +               cbus_set_gpio_direction(base, host->dat_gpio, 1);
116 +
117 +       cbus_set_gpio_dataout(base, host->clk_gpio, 0);
118 +}
119 +
120 +static u8 cbus_receive_bit(struct cbus_host *host, u32 base)
121 +{
122 +       u8 ret;
123 +
124 +       cbus_set_gpio_dataout(base, host->clk_gpio, 1);
125 +       ret = cbus_get_gpio_datain(base, host->dat_gpio);
126 +       cbus_set_gpio_dataout(base, host->clk_gpio, 0);
127 +
128 +       return ret;
129 +}
130 +
131 +#define cbus_output(base, gpio, val)   cbus_set_gpio_direction(base, gpio, 0)
132 +
133 +#else
134 +
135 +#define cbus_output(base, gpio, val)   gpio_direction_output(gpio, val)
136 +#define cbus_set_gpio_dataout(base, gpio, enable) gpio_set_value(gpio, enable)
137 +#define cbus_get_gpio_datain(base, int, gpio) gpio_get_value(gpio)
138 +
139 +static void _cbus_send_bit(struct cbus_host *host, int bit, int set_to_input)
140 +{
141 +       gpio_set_value(host->dat_gpio, bit ? 1 : 0);
142 +       gpio_set_value(host->clk_gpio, 1);
143 +
144 +       /* The data bit is read on the rising edge of CLK */
145 +       if (set_to_input)
146 +               gpio_direction_input(host->dat_gpio);
147 +
148 +       gpio_set_value(host->clk_gpio, 0);
149 +}
150 +
151 +static u8 _cbus_receive_bit(struct cbus_host *host)
152 +{
153 +       u8 ret;
154 +
155 +       gpio_set_value(host->clk_gpio, 1);
156 +       ret = gpio_get_value(host->dat_gpio);
157 +       gpio_set_value(host->clk_gpio, 0);
158 +
159 +       return ret;
160 +}
161 +
162 +#define cbus_send_bit(host, base, bit, set_to_input) _cbus_send_bit(host, bit, set_to_input)
163 +#define cbus_receive_bit(host, base) _cbus_receive_bit(host)
164 +
165 +#endif
166 +
167 +static int cbus_transfer(struct cbus_host *host, int dev, int reg, int data)
168 +{
169 +       int i;
170 +       int is_read = 0;
171 +       unsigned long flags;
172 +       u32 base;
173 +
174 +#ifdef CONFIG_ARCH_OMAP1
175 +       base = OMAP1_IO_ADDRESS(OMAP1_MPUIO_BASE);
176 +#else
177 +       base = 0;
178 +#endif
179 +
180 +       if (data < 0)
181 +               is_read = 1;
182 +
183 +       /* We don't want interrupts disturbing our transfer */
184 +       spin_lock_irqsave(&host->lock, flags);
185 +
186 +       /* Reset state and start of transfer, SEL stays down during transfer */
187 +       cbus_set_gpio_dataout(base, host->sel_gpio, 0);
188 +
189 +       /* Set the DAT pin to output */
190 +       cbus_output(base, host->dat_gpio, 1);
191 +
192 +       /* Send the device address */
193 +       for (i = 3; i > 0; i--)
194 +               cbus_send_bit(host, base, dev & (1 << (i - 1)), 0);
195 +
196 +       /* Send the rw flag */
197 +       cbus_send_bit(host, base, is_read, 0);
198 +
199 +       /* Send the register address */
200 +       for (i = 5; i > 0; i--) {
201 +               int set_to_input = 0;
202 +
203 +               if (is_read && i == 1)
204 +                       set_to_input = 1;
205 +
206 +               cbus_send_bit(host, base, reg & (1 << (i - 1)), set_to_input);
207 +       }
208 +
209 +       if (!is_read) {
210 +               for (i = 16; i > 0; i--)
211 +                       cbus_send_bit(host, base, data & (1 << (i - 1)), 0);
212 +       } else {
213 +               cbus_set_gpio_dataout(base, host->clk_gpio, 1);
214 +               data = 0;
215 +
216 +               for (i = 16; i > 0; i--) {
217 +                       u8 bit = cbus_receive_bit(host, base);
218 +
219 +                       if (bit)
220 +                               data |= 1 << (i - 1);
221 +               }
222 +       }
223 +
224 +       /* Indicate end of transfer, SEL goes up until next transfer */
225 +       cbus_set_gpio_dataout(base, host->sel_gpio, 1);
226 +       cbus_set_gpio_dataout(base, host->clk_gpio, 1);
227 +       cbus_set_gpio_dataout(base, host->clk_gpio, 0);
228 +
229 +       spin_unlock_irqrestore(&host->lock, flags);
230 +
231 +       return is_read ? data : 0;
232 +}
233 +
234 +/*
235 + * Read a given register from the device
236 + */
237 +int cbus_read_reg(struct cbus_host *host, int dev, int reg)
238 +{
239 +       return cbus_host ? cbus_transfer(host, dev, reg, -1) : -ENODEV;
240 +}
241 +EXPORT_SYMBOL(cbus_read_reg);
242 +
243 +/*
244 + * Write to a given register of the device
245 + */
246 +int cbus_write_reg(struct cbus_host *host, int dev, int reg, u16 val)
247 +{
248 +       return cbus_host ? cbus_transfer(host, dev, reg, (int)val) : -ENODEV;
249 +}
250 +EXPORT_SYMBOL(cbus_write_reg);
251 +
252 +static int __init cbus_bus_probe(struct platform_device *pdev)
253 +{
254 +       struct cbus_host *chost;
255 +       struct cbus_host_platform_data *pdata = pdev->dev.platform_data;
256 +       int ret;
257 +
258 +       chost = kzalloc(sizeof (*chost), GFP_KERNEL);
259 +       if (chost == NULL)
260 +               return -ENOMEM;
261 +
262 +       spin_lock_init(&chost->lock);
263 +
264 +       chost->clk_gpio = pdata->clk_gpio;
265 +       chost->dat_gpio = pdata->dat_gpio;
266 +       chost->sel_gpio = pdata->sel_gpio;
267 +
268 +       if ((ret = gpio_request(chost->clk_gpio, "CBUS clk")) < 0)
269 +               goto exit1;
270 +
271 +       if ((ret = gpio_request(chost->dat_gpio, "CBUS data")) < 0)
272 +               goto exit2;
273 +
274 +       if ((ret = gpio_request(chost->sel_gpio, "CBUS sel")) < 0)
275 +               goto exit3;
276 +
277 +       gpio_direction_output(chost->clk_gpio, 0);
278 +       gpio_direction_input(chost->dat_gpio);
279 +       gpio_direction_output(chost->sel_gpio, 1);
280 +
281 +       gpio_set_value(chost->clk_gpio, 1);
282 +       gpio_set_value(chost->clk_gpio, 0);
283 +
284 +       platform_set_drvdata(pdev, chost);
285 +
286 +       cbus_host = chost;
287 +
288 +       return 0;
289 +exit3:
290 +       gpio_free(chost->dat_gpio);
291 +exit2:
292 +       gpio_free(chost->clk_gpio);
293 +exit1:
294 +       kfree(chost);
295 +
296 +       return ret;
297 +}
298 +
299 +static void __exit cbus_bus_remove(struct platform_device *pdev)
300 +{
301 +       struct cbus_host        *chost = platform_get_drvdata(pdev);
302 +
303 +       gpio_free(chost->dat_gpio);
304 +       gpio_free(chost->clk_gpio);
305 +       kfree(chost);
306 +}
307 +
308 +static struct platform_driver cbus_driver = {
309 +       .remove         = __exit_p(cbus_bus_remove),
310 +       .driver         = {
311 +               .name   = "cbus",
312 +       },
313 +};
314 +
315 +static int __init cbus_bus_init(void)
316 +{
317 +       return platform_driver_probe(&cbus_driver, cbus_bus_probe);
318 +}
319 +
320 +subsys_initcall(cbus_bus_init);
321 +
322 +static void __exit cbus_bus_exit(void)
323 +{
324 +       platform_driver_unregister(&cbus_driver);
325 +}
326 +module_exit(cbus_bus_exit);
327 +
328 +MODULE_DESCRIPTION("CBUS serial protocol");
329 +MODULE_LICENSE("GPL");
330 +MODULE_AUTHOR("Juha Yrjölä");
331 +MODULE_AUTHOR("David Weinehall");
332 +MODULE_AUTHOR("Mikko Ylinen");
333 +
334 --- /dev/null
335 +++ linux-2.6.36-rc4/drivers/cbus/cbus.h
336 @@ -0,0 +1,36 @@
337 +/*
338 + * drivers/cbus/cbus.h
339 + *
340 + * Copyright (C) 2004, 2005 Nokia Corporation
341 + *
342 + * Written by Juha Yrjölä <juha.yrjola@nokia.com> and
343 + *           David Weinehall <david.weinehall@nokia.com>
344 + *
345 + * This file is subject to the terms and conditions of the GNU General
346 + * Public License. See the file "COPYING" in the main directory of this
347 + * archive for more details.
348 + *
349 + * This program is distributed in the hope that it will be useful,
350 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
351 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
352 + * GNU General Public License for more details.
353 + *
354 + * You should have received a copy of the GNU General Public License
355 + * along with this program; if not, write to the Free Software
356 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
357 + */
358 +
359 +#ifndef __DRIVERS_CBUS_CBUS_H
360 +#define __DRIVERS_CBUS_CBUS_H
361 +
362 +struct cbus_host {
363 +       int clk_gpio, dat_gpio, sel_gpio;
364 +        spinlock_t lock;
365 +};
366 +
367 +extern struct cbus_host *cbus_host;
368 +
369 +extern int cbus_read_reg(struct cbus_host *host, int dev, int reg);
370 +extern int cbus_write_reg(struct cbus_host *host, int dev, int reg, u16 val);
371 +
372 +#endif /* __DRIVERS_CBUS_CBUS_H */
373 --- /dev/null
374 +++ linux-2.6.36-rc4/drivers/cbus/Kconfig
375 @@ -0,0 +1,89 @@
376 +#
377 +# CBUS device configuration
378 +#
379 +
380 +menu "CBUS support"
381 +
382 +config CBUS
383 +       depends on ARCH_OMAP
384 +       bool "CBUS support on OMAP"
385 +       ---help---
386 +         CBUS is a proprietary serial protocol by Nokia.  It is mainly
387 +         used for accessing Energy Management auxiliary chips.
388 +
389 +         If you want CBUS support, you should say Y here.
390 +
391 +config CBUS_TAHVO
392 +       depends on CBUS
393 +       bool "Support for Tahvo"
394 +       ---help---
395 +         Tahvo is a mixed signal ASIC with some system features
396 +
397 +         If you want Tahvo support, you should say Y here.
398 +
399 +config CBUS_TAHVO_USER
400 +       depends on CBUS_TAHVO
401 +       bool "Support for Tahvo user space functions"
402 +       ---help---
403 +         If you want support for Tahvo's user space read/write etc. functions,
404 +         you should say Y here.
405 +
406 +config CBUS_TAHVO_USB
407 +       depends on CBUS_TAHVO && USB
408 +       tristate "Support for Tahvo USB transceiver"
409 +       ---help---
410 +         If you want Tahvo support for USB transceiver, say Y or M here.
411 +
412 +config CBUS_TAHVO_USB_HOST_BY_DEFAULT
413 +       depends on CBUS_TAHVO_USB && USB_OTG
414 +       boolean "Device in USB host mode by default"
415 +       ---help---
416 +         Say Y here, if you want the device to enter USB host mode
417 +         by default on bootup.
418 +
419 +config CBUS_RETU
420 +       depends on CBUS
421 +       bool "Support for Retu"
422 +       ---help---
423 +         Retu is a mixed signal ASIC with some system features
424 +
425 +         If you want Retu support, you should say Y here.
426 +
427 +config CBUS_RETU_USER
428 +       depends on CBUS_RETU
429 +       bool "Support for Retu user space functions"
430 +       ---help---
431 +         If you want support for Retu's user space read/write etc. functions,
432 +         you should say Y here.
433 +
434 +config CBUS_RETU_POWERBUTTON
435 +       depends on CBUS_RETU
436 +       bool "Support for Retu power button"
437 +       ---help---
438 +         The power button on Nokia 770 is connected to the Retu ASIC.
439 +
440 +         If you want support for the Retu power button, you should say Y here.
441 +
442 +config CBUS_RETU_RTC
443 +       depends on CBUS_RETU && SYSFS
444 +       tristate "Support for Retu pseudo-RTC"
445 +       ---help---
446 +         Say Y here if you want support for the device that alleges to be an
447 +         RTC in Retu. This will expose a sysfs interface for it.
448 +
449 +config CBUS_RETU_WDT
450 +       depends on CBUS_RETU && SYSFS && WATCHDOG
451 +       tristate "Support for Retu watchdog timer"
452 +       ---help---
453 +         Say Y here if you want support for the watchdog in Retu. This will
454 +         expose a sysfs interface to grok it.
455 +
456 +config CBUS_RETU_HEADSET
457 +       depends on CBUS_RETU && SYSFS
458 +       tristate "Support for headset detection with Retu/Vilma"
459 +       ---help---
460 +         Say Y here if you want support detecting a headset that's connected
461 +         to Retu/Vilma. Detection state and events are exposed through
462 +         sysfs.
463 +
464 +endmenu
465 --- /dev/null
466 +++ linux-2.6.36-rc4/drivers/cbus/Makefile
467 @@ -0,0 +1,14 @@
468 +#
469 +# Makefile for CBUS.
470 +#
471 +
472 +obj-$(CONFIG_CBUS)             += cbus.o
473 +obj-$(CONFIG_CBUS_TAHVO)       += tahvo.o
474 +obj-$(CONFIG_CBUS_RETU)                += retu.o
475 +obj-$(CONFIG_CBUS_TAHVO_USB)   += tahvo-usb.o
476 +obj-$(CONFIG_CBUS_RETU_POWERBUTTON) += retu-pwrbutton.o
477 +obj-$(CONFIG_CBUS_RETU_RTC)    += retu-rtc.o
478 +obj-$(CONFIG_CBUS_RETU_WDT)    += retu-wdt.o
479 +obj-$(CONFIG_CBUS_TAHVO_USER)  += tahvo-user.o
480 +obj-$(CONFIG_CBUS_RETU_USER)   += retu-user.o
481 +obj-$(CONFIG_CBUS_RETU_HEADSET)        += retu-headset.o
482 --- /dev/null
483 +++ linux-2.6.36-rc4/drivers/cbus/retu.c
484 @@ -0,0 +1,468 @@
485 +/**
486 + * drivers/cbus/retu.c
487 + *
488 + * Support functions for Retu ASIC
489 + *
490 + * Copyright (C) 2004, 2005 Nokia Corporation
491 + *
492 + * Written by Juha Yrjölä <juha.yrjola@nokia.com>,
493 + *           David Weinehall <david.weinehall@nokia.com>, and
494 + *           Mikko Ylinen <mikko.k.ylinen@nokia.com>
495 + *
496 + * This file is subject to the terms and conditions of the GNU General
497 + * Public License. See the file "COPYING" in the main directory of this
498 + * archive for more details.
499 + *
500 + * This program is distributed in the hope that it will be useful,
501 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
502 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
503 + * GNU General Public License for more details.
504 + *
505 + * You should have received a copy of the GNU General Public License
506 + * along with this program; if not, write to the Free Software
507 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
508 + */
509 +
510 +#include <linux/module.h>
511 +#include <linux/init.h>
512 +
513 +#include <linux/kernel.h>
514 +#include <linux/errno.h>
515 +#include <linux/device.h>
516 +#include <linux/miscdevice.h>
517 +#include <linux/poll.h>
518 +#include <linux/fs.h>
519 +#include <linux/irq.h>
520 +#include <linux/interrupt.h>
521 +#include <linux/platform_device.h>
522 +#include <linux/gpio.h>
523 +
524 +#include <asm/uaccess.h>
525 +#include <asm/mach-types.h>
526 +
527 +#include <plat/mux.h>
528 +#include <plat/board.h>
529 +
530 +#include "cbus.h"
531 +#include "retu.h"
532 +
533 +#define RETU_ID                        0x01
534 +#define PFX                    "retu: "
535 +
536 +static int retu_initialized;
537 +static int retu_irq_pin;
538 +static int retu_is_vilma;
539 +
540 +static struct tasklet_struct retu_tasklet;
541 +spinlock_t retu_lock = SPIN_LOCK_UNLOCKED;
542 +
543 +static struct completion device_release;
544 +
545 +struct retu_irq_handler_desc {
546 +       int (*func)(unsigned long);
547 +       unsigned long arg;
548 +       char name[8];
549 +};
550 +
551 +static struct retu_irq_handler_desc retu_irq_handlers[MAX_RETU_IRQ_HANDLERS];
552 +
553 +/**
554 + * retu_read_reg - Read a value from a register in Retu
555 + * @reg: the register to read from
556 + *
557 + * This function returns the contents of the specified register
558 + */
559 +int retu_read_reg(int reg)
560 +{
561 +       BUG_ON(!retu_initialized);
562 +       return cbus_read_reg(cbus_host, RETU_ID, reg);
563 +}
564 +
565 +/**
566 + * retu_write_reg - Write a value to a register in Retu
567 + * @reg: the register to write to
568 + * @reg: the value to write to the register
569 + *
570 + * This function writes a value to the specified register
571 + */
572 +void retu_write_reg(int reg, u16 val)
573 +{
574 +       BUG_ON(!retu_initialized);
575 +       cbus_write_reg(cbus_host, RETU_ID, reg, val);
576 +}
577 +
578 +void retu_set_clear_reg_bits(int reg, u16 set, u16 clear)
579 +{
580 +       unsigned long flags;
581 +       u16 w;
582 +
583 +       spin_lock_irqsave(&retu_lock, flags);
584 +       w = retu_read_reg(reg);
585 +       w &= ~clear;
586 +       w |= set;
587 +       retu_write_reg(reg, w);
588 +       spin_unlock_irqrestore(&retu_lock, flags);
589 +}
590 +
591 +#define ADC_MAX_CHAN_NUMBER    13
592 +
593 +int retu_read_adc(int channel)
594 +{
595 +       unsigned long flags;
596 +       int res;
597 +
598 +       if (channel < 0 || channel > ADC_MAX_CHAN_NUMBER)
599 +               return -EINVAL;
600 +
601 +       spin_lock_irqsave(&retu_lock, flags);
602 +
603 +       if ((channel == 8) && retu_is_vilma) {
604 +               int scr = retu_read_reg(RETU_REG_ADCSCR);
605 +               int ch = (retu_read_reg(RETU_REG_ADCR) >> 10) & 0xf;
606 +               if (((scr & 0xff) != 0) && (ch != 8))
607 +                       retu_write_reg (RETU_REG_ADCSCR, (scr & ~0xff));
608 +       }
609 +
610 +       /* Select the channel and read result */
611 +       retu_write_reg(RETU_REG_ADCR, channel << 10);
612 +       res = retu_read_reg(RETU_REG_ADCR) & 0x3ff;
613 +
614 +       if (retu_is_vilma)
615 +               retu_write_reg(RETU_REG_ADCR, (1 << 13));
616 +
617 +       /* Unlock retu */
618 +       spin_unlock_irqrestore(&retu_lock, flags);
619 +
620 +       return res;
621 +}
622 +
623 +
624 +static u16 retu_disable_bogus_irqs(u16 mask)
625 +{
626 +       int i;
627 +
628 +       for (i = 0; i < MAX_RETU_IRQ_HANDLERS; i++) {
629 +               if (mask & (1 << i))
630 +                       continue;
631 +               if (retu_irq_handlers[i].func != NULL)
632 +                       continue;
633 +               /* an IRQ was enabled but we don't have a handler for it */
634 +               printk(KERN_INFO PFX "disabling bogus IRQ %d\n", i);
635 +               mask |= (1 << i);
636 +       }
637 +       return mask;
638 +}
639 +
640 +/*
641 + * Disable given RETU interrupt
642 + */
643 +void retu_disable_irq(int id)
644 +{
645 +       unsigned long flags;
646 +       u16 mask;
647 +
648 +       spin_lock_irqsave(&retu_lock, flags);
649 +       mask = retu_read_reg(RETU_REG_IMR);
650 +       mask |= 1 << id;
651 +       mask = retu_disable_bogus_irqs(mask);
652 +       retu_write_reg(RETU_REG_IMR, mask);
653 +       spin_unlock_irqrestore(&retu_lock, flags);
654 +}
655 +
656 +/*
657 + * Enable given RETU interrupt
658 + */
659 +void retu_enable_irq(int id)
660 +{
661 +       unsigned long flags;
662 +       u16 mask;
663 +
664 +       if (id == 3) {
665 +               printk("Enabling Retu IRQ %d\n", id);
666 +               dump_stack();
667 +       }
668 +       spin_lock_irqsave(&retu_lock, flags);
669 +       mask = retu_read_reg(RETU_REG_IMR);
670 +       mask &= ~(1 << id);
671 +       mask = retu_disable_bogus_irqs(mask);
672 +       retu_write_reg(RETU_REG_IMR, mask);
673 +       spin_unlock_irqrestore(&retu_lock, flags);
674 +}
675 +
676 +/*
677 + * Acknowledge given RETU interrupt
678 + */
679 +void retu_ack_irq(int id)
680 +{
681 +       retu_write_reg(RETU_REG_IDR, 1 << id);
682 +}
683 +
684 +/*
685 + * RETU interrupt handler. Only schedules the tasklet.
686 + */
687 +static irqreturn_t retu_irq_handler(int irq, void *dev_id)
688 +{
689 +       tasklet_schedule(&retu_tasklet);
690 +       return IRQ_HANDLED;
691 +}
692 +
693 +/*
694 + * Tasklet handler
695 + */
696 +static void retu_tasklet_handler(unsigned long data)
697 +{
698 +       struct retu_irq_handler_desc *hnd;
699 +       u16 id;
700 +       u16 im;
701 +       int i;
702 +
703 +       for (;;) {
704 +               id = retu_read_reg(RETU_REG_IDR);
705 +               im = ~retu_read_reg(RETU_REG_IMR);
706 +               id &= im;
707 +
708 +               if (!id)
709 +                       break;
710 +
711 +               for (i = 0; id != 0; i++, id >>= 1) {
712 +                       if (!(id & 1))
713 +                               continue;
714 +                       hnd = &retu_irq_handlers[i];
715 +                       if (hnd->func == NULL) {
716 +                               /* Spurious retu interrupt - disable and ack it */
717 +                               printk(KERN_INFO "Spurious Retu interrupt "
718 +                                                "(id %d)\n", i);
719 +                               retu_disable_irq(i);
720 +                               retu_ack_irq(i);
721 +                               continue;
722 +                       }
723 +                       hnd->func(hnd->arg);
724 +                       /*
725 +                        * Don't acknowledge the interrupt here
726 +                        * It must be done explicitly
727 +                        */
728 +               }
729 +       }
730 +}
731 +
732 +/*
733 + * Register the handler for a given RETU interrupt source.
734 + */
735 +int retu_request_irq(int id, void *irq_handler, unsigned long arg, char *name)
736 +{
737 +       struct retu_irq_handler_desc *hnd;
738 +
739 +       if (irq_handler == NULL || id >= MAX_RETU_IRQ_HANDLERS ||
740 +           name == NULL) {
741 +               printk(KERN_ERR PFX "Invalid arguments to %s\n",
742 +                      __FUNCTION__);
743 +               return -EINVAL;
744 +       }
745 +       hnd = &retu_irq_handlers[id];
746 +       if (hnd->func != NULL) {
747 +               printk(KERN_ERR PFX "IRQ %d already reserved\n", id);
748 +               return -EBUSY;
749 +       }
750 +       printk(KERN_INFO PFX "Registering interrupt %d for device %s\n",
751 +              id, name);
752 +       hnd->func = irq_handler;
753 +       hnd->arg = arg;
754 +       strlcpy(hnd->name, name, sizeof(hnd->name));
755 +
756 +       retu_ack_irq(id);
757 +       retu_enable_irq(id);
758 +
759 +       return 0;
760 +}
761 +
762 +/*
763 + * Unregister the handler for a given RETU interrupt source.
764 + */
765 +void retu_free_irq(int id)
766 +{
767 +       struct retu_irq_handler_desc *hnd;
768 +
769 +       if (id >= MAX_RETU_IRQ_HANDLERS) {
770 +               printk(KERN_ERR PFX "Invalid argument to %s\n",
771 +                      __FUNCTION__);
772 +               return;
773 +       }
774 +       hnd = &retu_irq_handlers[id];
775 +       if (hnd->func == NULL) {
776 +               printk(KERN_ERR PFX "IRQ %d already freed\n", id);
777 +               return;
778 +       }
779 +
780 +       retu_disable_irq(id);
781 +       hnd->func = NULL;
782 +}
783 +
784 +/**
785 + * retu_power_off - Shut down power to system
786 + *
787 + * This function puts the system in power off state
788 + */
789 +static void retu_power_off(void)
790 +{
791 +       /* Ignore power button state */
792 +       retu_write_reg(RETU_REG_CC1, retu_read_reg(RETU_REG_CC1) | 2);
793 +       /* Expire watchdog immediately */
794 +       retu_write_reg(RETU_REG_WATCHDOG, 0);
795 +       /* Wait for poweroff*/
796 +       for (;;);
797 +}
798 +
799 +/**
800 + * retu_probe - Probe for Retu ASIC
801 + * @dev: the Retu device
802 + *
803 + * Probe for the Retu ASIC and allocate memory
804 + * for its device-struct if found
805 + */
806 +static int __devinit retu_probe(struct device *dev)
807 +{
808 +       int rev, ret;
809 +
810 +       /* Prepare tasklet */
811 +       tasklet_init(&retu_tasklet, retu_tasklet_handler, 0);
812 +
813 +       /* REVISIT: Pass these from board-*.c files in platform_data */
814 +       if (machine_is_nokia770()) {
815 +               retu_irq_pin = 62;
816 +       } else if (machine_is_nokia_n800() || machine_is_nokia_n810() ||
817 +                       machine_is_nokia_n810_wimax()) {
818 +               retu_irq_pin = 108;
819 +       } else {
820 +               printk(KERN_ERR "cbus: Unsupported board for tahvo\n");
821 +               return -ENODEV;
822 +       }
823 +
824 +       if ((ret = gpio_request(retu_irq_pin, "RETU irq")) < 0) {
825 +               printk(KERN_ERR PFX "Unable to reserve IRQ GPIO\n");
826 +               return ret;
827 +       }
828 +
829 +       /* Set the pin as input */
830 +       gpio_direction_input(retu_irq_pin);
831 +
832 +       /* Rising edge triggers the IRQ */
833 +       set_irq_type(gpio_to_irq(retu_irq_pin), IRQ_TYPE_EDGE_RISING);
834 +
835 +       retu_initialized = 1;
836 +
837 +       rev = retu_read_reg(RETU_REG_ASICR) & 0xff;
838 +       if (rev & (1 << 7))
839 +               retu_is_vilma = 1;
840 +
841 +       printk(KERN_INFO "%s v%d.%d found\n", retu_is_vilma ? "Vilma" : "Retu",
842 +              (rev >> 4) & 0x07, rev & 0x0f);
843 +
844 +       /* Mask all RETU interrupts */
845 +       retu_write_reg(RETU_REG_IMR, 0xffff);
846 +
847 +       ret = request_irq(gpio_to_irq(retu_irq_pin), retu_irq_handler, 0,
848 +                         "retu", 0);
849 +       if (ret < 0) {
850 +               printk(KERN_ERR PFX "Unable to register IRQ handler\n");
851 +               gpio_free(retu_irq_pin);
852 +               return ret;
853 +       }
854 +       set_irq_wake(gpio_to_irq(retu_irq_pin), 1);
855 +
856 +       /* Register power off function */
857 +       pm_power_off = retu_power_off;
858 +
859 +#ifdef CONFIG_CBUS_RETU_USER
860 +       /* Initialize user-space interface */
861 +       if (retu_user_init() < 0) {
862 +               printk(KERN_ERR "Unable to initialize driver\n");
863 +               free_irq(gpio_to_irq(retu_irq_pin), 0);
864 +               gpio_free(retu_irq_pin);
865 +               return ret;
866 +       }
867 +#endif
868 +
869 +       return 0;
870 +}
871 +
872 +static int retu_remove(struct device *dev)
873 +{
874 +#ifdef CONFIG_CBUS_RETU_USER
875 +       retu_user_cleanup();
876 +#endif
877 +       /* Mask all RETU interrupts */
878 +       retu_write_reg(RETU_REG_IMR, 0xffff);
879 +       free_irq(gpio_to_irq(retu_irq_pin), 0);
880 +       gpio_free(retu_irq_pin);
881 +       tasklet_kill(&retu_tasklet);
882 +
883 +       return 0;
884 +}
885 +
886 +static void retu_device_release(struct device *dev)
887 +{
888 +       complete(&device_release);
889 +}
890 +
891 +static struct device_driver retu_driver = {
892 +       .name           = "retu",
893 +       .bus            = &platform_bus_type,
894 +       .probe          = retu_probe,
895 +       .remove         = retu_remove,
896 +};
897 +
898 +static struct platform_device retu_device = {
899 +       .name           = "retu",
900 +       .id             = -1,
901 +       .dev = {
902 +               .release = retu_device_release,
903 +       }
904 +};
905 +
906 +/**
907 + * retu_init - initialise Retu driver
908 + *
909 + * Initialise the Retu driver and return 0 if everything worked ok
910 + */
911 +static int __init retu_init(void)
912 +{
913 +       int ret = 0;
914 +
915 +       printk(KERN_INFO "Retu/Vilma driver initialising\n");
916 +
917 +       init_completion(&device_release);
918 +
919 +       if ((ret = driver_register(&retu_driver)) < 0)
920 +               return ret;
921 +
922 +       if ((ret = platform_device_register(&retu_device)) < 0) {
923 +               driver_unregister(&retu_driver);
924 +               return ret;
925 +       }
926 +       return 0;
927 +}
928 +
929 +/*
930 + * Cleanup
931 + */
932 +static void __exit retu_exit(void)
933 +{
934 +       platform_device_unregister(&retu_device);
935 +       driver_unregister(&retu_driver);
936 +       wait_for_completion(&device_release);
937 +}
938 +
939 +EXPORT_SYMBOL(retu_request_irq);
940 +EXPORT_SYMBOL(retu_free_irq);
941 +EXPORT_SYMBOL(retu_enable_irq);
942 +EXPORT_SYMBOL(retu_disable_irq);
943 +EXPORT_SYMBOL(retu_ack_irq);
944 +EXPORT_SYMBOL(retu_read_reg);
945 +EXPORT_SYMBOL(retu_write_reg);
946 +
947 +subsys_initcall(retu_init);
948 +module_exit(retu_exit);
949 +
950 +MODULE_DESCRIPTION("Retu ASIC control");
951 +MODULE_LICENSE("GPL");
952 +MODULE_AUTHOR("Juha Yrjölä, David Weinehall, and Mikko Ylinen");
953 --- /dev/null
954 +++ linux-2.6.36-rc4/drivers/cbus/retu.h
955 @@ -0,0 +1,77 @@
956 +/**
957 + * drivers/cbus/retu.h
958 + *
959 + * Copyright (C) 2004, 2005 Nokia Corporation
960 + *
961 + * Written by Juha Yrjölä <juha.yrjola@nokia.com> and
962 + *           David Weinehall <david.weinehall@nokia.com>
963 + *
964 + * This file is subject to the terms and conditions of the GNU General
965 + * Public License. See the file "COPYING" in the main directory of this
966 + * archive for more details.
967 + *
968 + * This program is distributed in the hope that it will be useful,
969 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
970 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
971 + * GNU General Public License for more details.
972 +
973 + * You should have received a copy of the GNU General Public License
974 + * along with this program; if not, write to the Free Software
975 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
976 + */
977 +
978 +#ifndef __DRIVERS_CBUS_RETU_H
979 +#define __DRIVERS_CBUS_RETU_H
980 +
981 +#include <linux/types.h>
982 +
983 +/* Registers */
984 +#define RETU_REG_ASICR         0x00    /* ASIC ID & revision */
985 +#define RETU_REG_IDR           0x01    /* Interrupt ID */
986 +#define RETU_REG_IMR           0x02    /* Interrupt mask */
987 +#define RETU_REG_RTCDSR                0x03    /* RTC seconds register */
988 +#define RETU_REG_RTCHMR                0x04    /* RTC hours and minutes register */
989 +#define RETU_REG_RTCHMAR       0x05    /* RTC hours and minutes alarm and time set register */
990 +#define RETU_REG_RTCCALR       0x06    /* RTC calibration register */
991 +#define RETU_REG_ADCR          0x08    /* ADC result */
992 +#define RETU_REG_ADCSCR                0x09    /* ADC sample ctrl */
993 +#define RETU_REG_CC1           0x0d    /* Common control register 1 */
994 +#define RETU_REG_CC2           0x0e    /* Common control register 2 */
995 +#define RETU_REG_CTRL_CLR      0x0f    /* Regulator clear register */
996 +#define RETU_REG_CTRL_SET      0x10    /* Regulator set register */
997 +#define RETU_REG_STATUS                0x16    /* Status register */
998 +#define RETU_REG_WATCHDOG      0x17    /* Watchdog register */
999 +#define RETU_REG_AUDTXR                0x18    /* Audio Codec Tx register */
1000 +#define RETU_REG_MAX           0x1f
1001 +
1002 +/* Interrupt sources */
1003 +#define RETU_INT_PWR           0
1004 +#define RETU_INT_CHAR          1
1005 +#define RETU_INT_RTCS          2
1006 +#define RETU_INT_RTCM          3
1007 +#define RETU_INT_RTCD          4
1008 +#define RETU_INT_RTCA          5
1009 +#define RETU_INT_HOOK          6
1010 +#define RETU_INT_HEAD          7
1011 +#define RETU_INT_ADCS          8
1012 +
1013 +#define        MAX_RETU_IRQ_HANDLERS   16
1014 +
1015 +int retu_read_reg(int reg);
1016 +void retu_write_reg(int reg, u16 val);
1017 +void retu_set_clear_reg_bits(int reg, u16 set, u16 clear);
1018 +int retu_read_adc(int channel);
1019 +int retu_request_irq(int id, void *irq_handler, unsigned long arg, char *name);
1020 +void retu_free_irq(int id);
1021 +void retu_enable_irq(int id);
1022 +void retu_disable_irq(int id);
1023 +void retu_ack_irq(int id);
1024 +
1025 +#ifdef CONFIG_CBUS_RETU_USER
1026 +int retu_user_init(void);
1027 +void retu_user_cleanup(void);
1028 +#endif
1029 +
1030 +extern spinlock_t retu_lock;
1031 +
1032 +#endif /* __DRIVERS_CBUS_RETU_H */
1033 --- /dev/null
1034 +++ linux-2.6.36-rc4/drivers/cbus/retu-headset.c
1035 @@ -0,0 +1,356 @@
1036 +/**
1037 + * Retu/Vilma headset detection
1038 + *
1039 + * Copyright (C) 2006 Nokia Corporation
1040 + *
1041 + * Written by Juha Yrjölä
1042 + *
1043 + * This file is subject to the terms and conditions of the GNU General
1044 + * Public License. See the file "COPYING" in the main directory of this
1045 + * archive for more details.
1046 + *
1047 + * This program is distributed in the hope that it will be useful,
1048 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1049 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1050 + * GNU General Public License for more details.
1051 + *
1052 + * You should have received a copy of the GNU General Public License
1053 + * along with this program; if not, write to the Free Software
1054 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1055 + */
1056 +
1057 +#include <linux/module.h>
1058 +#include <linux/init.h>
1059 +#include <linux/kernel.h>
1060 +#include <linux/delay.h>
1061 +#include <linux/input.h>
1062 +#include <linux/platform_device.h>
1063 +#include <linux/slab.h>
1064 +
1065 +#include "retu.h"
1066 +
1067 +#define RETU_ADC_CHANNEL_HOOKDET       0x05
1068 +
1069 +#define RETU_HEADSET_KEY               KEY_PHONE
1070 +
1071 +struct retu_headset {
1072 +       spinlock_t                      lock;
1073 +       struct mutex                    mutex;
1074 +       struct platform_device          *pdev;
1075 +       struct input_dev                *idev;
1076 +       unsigned                        bias_enabled;
1077 +       unsigned                        detection_enabled;
1078 +       unsigned                        pressed;
1079 +       struct timer_list               enable_timer;
1080 +       struct timer_list               detect_timer;
1081 +};
1082 +
1083 +static void retu_headset_set_bias(int enable)
1084 +{
1085 +       if (enable) {
1086 +               retu_set_clear_reg_bits(RETU_REG_AUDTXR,
1087 +                                       (1 << 0) | (1 << 1), 0);
1088 +               msleep(2);
1089 +               retu_set_clear_reg_bits(RETU_REG_AUDTXR, 1 << 3, 0);
1090 +       } else {
1091 +               retu_set_clear_reg_bits(RETU_REG_AUDTXR, 0,
1092 +                                       (1 << 0) | (1 << 1) | (1 << 3));
1093 +       }
1094 +}
1095 +
1096 +static void retu_headset_enable(struct retu_headset *hs)
1097 +{
1098 +       mutex_lock(&hs->mutex);
1099 +       if (!hs->bias_enabled) {
1100 +               hs->bias_enabled = 1;
1101 +               retu_headset_set_bias(1);
1102 +       }
1103 +       mutex_unlock(&hs->mutex);
1104 +}
1105 +
1106 +static void retu_headset_disable(struct retu_headset *hs)
1107 +{
1108 +       mutex_lock(&hs->mutex);
1109 +       if (hs->bias_enabled) {
1110 +               hs->bias_enabled = 0;
1111 +               retu_headset_set_bias(0);
1112 +       }
1113 +       mutex_unlock(&hs->mutex);
1114 +}
1115 +
1116 +static void retu_headset_det_enable(struct retu_headset *hs)
1117 +{
1118 +       mutex_lock(&hs->mutex);
1119 +       if (!hs->detection_enabled) {
1120 +               hs->detection_enabled = 1;
1121 +               retu_set_clear_reg_bits(RETU_REG_CC1, (1 << 10) | (1 << 8), 0);
1122 +               retu_enable_irq(RETU_INT_HOOK);
1123 +       }
1124 +       mutex_unlock(&hs->mutex);
1125 +}
1126 +
1127 +static void retu_headset_det_disable(struct retu_headset *hs)
1128 +{
1129 +       unsigned long flags;
1130 +
1131 +       mutex_lock(&hs->mutex);
1132 +       if (hs->detection_enabled) {
1133 +               hs->detection_enabled = 0;
1134 +               retu_disable_irq(RETU_INT_HOOK);
1135 +               del_timer_sync(&hs->enable_timer);
1136 +               del_timer_sync(&hs->detect_timer);
1137 +               spin_lock_irqsave(&hs->lock, flags);
1138 +               if (hs->pressed)
1139 +                       input_report_key(hs->idev, RETU_HEADSET_KEY, 0);
1140 +               spin_unlock_irqrestore(&hs->lock, flags);
1141 +               retu_set_clear_reg_bits(RETU_REG_CC1, 0, (1 << 10) | (1 << 8));
1142 +       }
1143 +       mutex_unlock(&hs->mutex);
1144 +}
1145 +
1146 +static ssize_t retu_headset_hookdet_show(struct device *dev,
1147 +                                        struct device_attribute *attr,
1148 +                                        char *buf)
1149 +{
1150 +       int val;
1151 +
1152 +       val = retu_read_adc(RETU_ADC_CHANNEL_HOOKDET);
1153 +       return sprintf(buf, "%d\n", val);
1154 +}
1155 +
1156 +static DEVICE_ATTR(hookdet, S_IRUGO, retu_headset_hookdet_show, NULL);
1157 +
1158 +static ssize_t retu_headset_enable_show(struct device *dev,
1159 +                                       struct device_attribute *attr,
1160 +                                       char *buf)
1161 +{
1162 +       struct retu_headset *hs = dev_get_drvdata(dev);
1163 +
1164 +       return sprintf(buf, "%u\n", hs->bias_enabled);
1165 +}
1166 +
1167 +static ssize_t retu_headset_enable_store(struct device *dev,
1168 +                                        struct device_attribute *attr,
1169 +                                        const char *buf, size_t count)
1170 +{
1171 +       struct retu_headset *hs = dev_get_drvdata(dev);
1172 +       int enable;
1173 +
1174 +       if (sscanf(buf, "%u", &enable) != 1)
1175 +               return -EINVAL;
1176 +       if (enable)
1177 +               retu_headset_enable(hs);
1178 +       else
1179 +               retu_headset_disable(hs);
1180 +       return count;
1181 +}
1182 +
1183 +static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
1184 +                  retu_headset_enable_show, retu_headset_enable_store);
1185 +
1186 +static ssize_t retu_headset_enable_det_show(struct device *dev,
1187 +                                           struct device_attribute *attr,
1188 +                                           char *buf)
1189 +{
1190 +       struct retu_headset *hs = dev_get_drvdata(dev);
1191 +
1192 +       return sprintf(buf, "%u\n", hs->detection_enabled);
1193 +}
1194 +
1195 +static ssize_t retu_headset_enable_det_store(struct device *dev,
1196 +                                            struct device_attribute *attr,
1197 +                                            const char *buf, size_t count)
1198 +{
1199 +       struct retu_headset *hs = dev_get_drvdata(dev);
1200 +       int enable;
1201 +
1202 +       if (sscanf(buf, "%u", &enable) != 1)
1203 +               return -EINVAL;
1204 +       if (enable)
1205 +               retu_headset_det_enable(hs);
1206 +       else
1207 +               retu_headset_det_disable(hs);
1208 +       return count;
1209 +}
1210 +
1211 +static DEVICE_ATTR(enable_det, S_IRUGO | S_IWUSR | S_IWGRP,
1212 +                  retu_headset_enable_det_show,
1213 +                  retu_headset_enable_det_store);
1214 +
1215 +static void retu_headset_hook_interrupt(unsigned long arg)
1216 +{
1217 +       struct retu_headset *hs = (struct retu_headset *) arg;
1218 +       unsigned long flags;
1219 +
1220 +       retu_ack_irq(RETU_INT_HOOK);
1221 +       spin_lock_irqsave(&hs->lock, flags);
1222 +       if (!hs->pressed) {
1223 +               /* Headset button was just pressed down. */
1224 +               hs->pressed = 1;
1225 +               input_report_key(hs->idev, RETU_HEADSET_KEY, 1);
1226 +       }
1227 +       spin_unlock_irqrestore(&hs->lock, flags);
1228 +       retu_set_clear_reg_bits(RETU_REG_CC1, 0, (1 << 10) | (1 << 8));
1229 +       mod_timer(&hs->enable_timer, jiffies + msecs_to_jiffies(50));
1230 +}
1231 +
1232 +static void retu_headset_enable_timer(unsigned long arg)
1233 +{
1234 +       struct retu_headset *hs = (struct retu_headset *) arg;
1235 +
1236 +       retu_set_clear_reg_bits(RETU_REG_CC1, (1 << 10) | (1 << 8), 0);
1237 +       mod_timer(&hs->detect_timer, jiffies + msecs_to_jiffies(350));
1238 +}
1239 +
1240 +static void retu_headset_detect_timer(unsigned long arg)
1241 +{
1242 +       struct retu_headset *hs = (struct retu_headset *) arg;
1243 +       unsigned long flags;
1244 +
1245 +       spin_lock_irqsave(&hs->lock, flags);
1246 +       if (hs->pressed) {
1247 +               hs->pressed = 0;
1248 +               input_report_key(hs->idev, RETU_HEADSET_KEY, 0);
1249 +       }
1250 +       spin_unlock_irqrestore(&hs->lock, flags);
1251 +}
1252 +
1253 +static int __init retu_headset_probe(struct platform_device *pdev)
1254 +{
1255 +       struct retu_headset *hs;
1256 +       int r;
1257 +
1258 +       hs = kzalloc(sizeof(*hs), GFP_KERNEL);
1259 +       if (hs == NULL)
1260 +               return -ENOMEM;
1261 +
1262 +       hs->pdev = pdev;
1263 +
1264 +       hs->idev = input_allocate_device();
1265 +       if (hs->idev == NULL) {
1266 +               r = -ENOMEM;
1267 +               goto err1;
1268 +       }
1269 +       hs->idev->name = "retu-headset";
1270 +       hs->idev->dev.parent = &pdev->dev;
1271 +       set_bit(EV_KEY, hs->idev->evbit);
1272 +       set_bit(RETU_HEADSET_KEY, hs->idev->keybit);
1273 +       r = input_register_device(hs->idev);
1274 +       if (r < 0)
1275 +               goto err2;
1276 +
1277 +       r = device_create_file(&pdev->dev, &dev_attr_hookdet);
1278 +       if (r < 0)
1279 +               goto err3;
1280 +       r = device_create_file(&pdev->dev, &dev_attr_enable);
1281 +       if (r < 0)
1282 +               goto err4;
1283 +       r = device_create_file(&pdev->dev, &dev_attr_enable_det);
1284 +       if (r < 0)
1285 +               goto err5;
1286 +       platform_set_drvdata(pdev, hs);
1287 +
1288 +       spin_lock_init(&hs->lock);
1289 +       mutex_init(&hs->mutex);
1290 +       setup_timer(&hs->enable_timer, retu_headset_enable_timer,
1291 +                   (unsigned long) hs);
1292 +       setup_timer(&hs->detect_timer, retu_headset_detect_timer,
1293 +                   (unsigned long) hs);
1294 +
1295 +       r = retu_request_irq(RETU_INT_HOOK, retu_headset_hook_interrupt,
1296 +                            (unsigned long) hs, "hookdet");
1297 +       if (r != 0) {
1298 +               dev_err(&pdev->dev, "hookdet IRQ not available\n");
1299 +               goto err6;
1300 +       }
1301 +       retu_disable_irq(RETU_INT_HOOK);
1302 +       return 0;
1303 +err6:
1304 +       device_remove_file(&pdev->dev, &dev_attr_enable_det);
1305 +err5:
1306 +       device_remove_file(&pdev->dev, &dev_attr_enable);
1307 +err4:
1308 +       device_remove_file(&pdev->dev, &dev_attr_hookdet);
1309 +err3:
1310 +       input_unregister_device(hs->idev);
1311 +err2:
1312 +       input_free_device(hs->idev);
1313 +err1:
1314 +       kfree(hs);
1315 +       return r;
1316 +}
1317 +
1318 +static int retu_headset_remove(struct platform_device *pdev)
1319 +{
1320 +       struct retu_headset *hs = platform_get_drvdata(pdev);
1321 +
1322 +       device_remove_file(&pdev->dev, &dev_attr_hookdet);
1323 +       device_remove_file(&pdev->dev, &dev_attr_enable);
1324 +       device_remove_file(&pdev->dev, &dev_attr_enable_det);
1325 +       retu_headset_disable(hs);
1326 +       retu_headset_det_disable(hs);
1327 +       retu_free_irq(RETU_INT_HOOK);
1328 +       input_unregister_device(hs->idev);
1329 +       input_free_device(hs->idev);
1330 +       return 0;
1331 +}
1332 +
1333 +static int retu_headset_suspend(struct platform_device *pdev,
1334 +                               pm_message_t mesg)
1335 +{
1336 +       struct retu_headset *hs = platform_get_drvdata(pdev);
1337 +
1338 +       mutex_lock(&hs->mutex);
1339 +       if (hs->bias_enabled)
1340 +               retu_headset_set_bias(0);
1341 +       mutex_unlock(&hs->mutex);
1342 +
1343 +       return 0;
1344 +}
1345 +
1346 +static int retu_headset_resume(struct platform_device *pdev)
1347 +{
1348 +       struct retu_headset *hs = platform_get_drvdata(pdev);
1349 +
1350 +       mutex_lock(&hs->mutex);
1351 +       if (hs->bias_enabled)
1352 +               retu_headset_set_bias(1);
1353 +       mutex_unlock(&hs->mutex);
1354 +
1355 +       return 0;
1356 +}
1357 +
1358 +static struct platform_driver retu_headset_driver = {
1359 +       .probe          = retu_headset_probe,
1360 +       .remove         = retu_headset_remove,
1361 +       .suspend        = retu_headset_suspend,
1362 +       .resume         = retu_headset_resume,
1363 +       .driver         = {
1364 +               .name   = "retu-headset",
1365 +       },
1366 +};
1367 +
1368 +static int __init retu_headset_init(void)
1369 +{
1370 +       int r;
1371 +
1372 +       printk(KERN_INFO "Retu/Vilma headset driver initializing\n");
1373 +
1374 +       r = platform_driver_register(&retu_headset_driver);
1375 +       if (r < 0)
1376 +               return r;
1377 +
1378 +       return 0;
1379 +}
1380 +
1381 +static void __exit retu_headset_exit(void)
1382 +{
1383 +       platform_driver_unregister(&retu_headset_driver);
1384 +}
1385 +
1386 +module_init(retu_headset_init);
1387 +module_exit(retu_headset_exit);
1388 +
1389 +MODULE_DESCRIPTION("Retu/Vilma headset detection");
1390 +MODULE_LICENSE("GPL");
1391 +MODULE_AUTHOR("Juha Yrjölä");
1392 --- /dev/null
1393 +++ linux-2.6.36-rc4/drivers/cbus/retu-pwrbutton.c
1394 @@ -0,0 +1,118 @@
1395 +/**
1396 + * drivers/cbus/retu-pwrbutton.c
1397 + *
1398 + * Driver for sending retu power button event to input-layer
1399 + *
1400 + * Copyright (C) 2004 Nokia Corporation
1401 + *
1402 + * Written by Ari Saastamoinen <ari.saastamoinen@elektrobit.com>
1403 + *
1404 + * Contact Juha Yrjölä <juha.yrjola@nokia.com>
1405 + *
1406 + * This file is subject to the terms and conditions of the GNU General
1407 + * Public License. See the file "COPYING" in the main directory of this
1408 + * archive for more details.
1409 + *
1410 + * This program is distributed in the hope that it will be useful,
1411 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1412 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1413 + * GNU General Public License for more details.
1414 + *
1415 + * You should have received a copy of the GNU General Public License
1416 + * along with this program; if not, write to the Free Software
1417 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1418 + */
1419 +
1420 +#include <linux/module.h>
1421 +#include <linux/init.h>
1422 +#include <linux/kernel.h>
1423 +#include <linux/errno.h>
1424 +#include <linux/input.h>
1425 +#include <linux/timer.h>
1426 +#include <linux/jiffies.h>
1427 +#include <linux/bitops.h>
1428 +
1429 +#include "retu.h"
1430 +
1431 +#define RETU_STATUS_PWRONX     (1 << 5)
1432 +
1433 +#define PWRBTN_DELAY           20
1434 +#define PWRBTN_UP              0
1435 +#define PWRBTN_PRESSED         1
1436 +
1437 +static int pwrbtn_state;
1438 +static struct input_dev *pwrbtn_dev;
1439 +static struct timer_list pwrbtn_timer;
1440 +
1441 +static void retubutton_timer_func(unsigned long arg)
1442 +{
1443 +       int state;
1444 +
1445 +       if (retu_read_reg(RETU_REG_STATUS) & RETU_STATUS_PWRONX)
1446 +               state = PWRBTN_UP;
1447 +       else
1448 +               state = PWRBTN_PRESSED;
1449 +
1450 +       if (pwrbtn_state != state) {
1451 +               input_report_key(pwrbtn_dev, KEY_POWER, state);
1452 +               pwrbtn_state = state;
1453 +       }
1454 +}
1455 +
1456 +/**
1457 + * Interrupt function is called whenever power button key is pressed
1458 + * or released.
1459 + */
1460 +static void retubutton_irq(unsigned long arg)
1461 +{
1462 +       retu_ack_irq(RETU_INT_PWR);
1463 +       mod_timer(&pwrbtn_timer, jiffies + msecs_to_jiffies(PWRBTN_DELAY));
1464 +}
1465 +
1466 +/**
1467 + * Init function.
1468 + * Allocates interrupt for power button and registers itself to input layer.
1469 + */
1470 +static int __init retubutton_init(void)
1471 +{
1472 +       int irq;
1473 +
1474 +       printk(KERN_INFO "Retu power button driver initialized\n");
1475 +       irq = RETU_INT_PWR;
1476 +
1477 +       init_timer(&pwrbtn_timer);
1478 +       pwrbtn_timer.function = retubutton_timer_func;
1479 +
1480 +       if (retu_request_irq(irq, &retubutton_irq, 0, "PwrOnX") < 0) {
1481 +               printk(KERN_ERR "%s@%s: Cannot allocate irq\n",
1482 +                      __FUNCTION__, __FILE__);
1483 +               return -EBUSY;
1484 +       }
1485 +
1486 +       pwrbtn_dev = input_allocate_device();
1487 +       if (!pwrbtn_dev)
1488 +               return -ENOMEM;
1489 +
1490 +       pwrbtn_dev->evbit[0] = BIT_MASK(EV_KEY);
1491 +       pwrbtn_dev->keybit[BIT_WORD(KEY_POWER)] = BIT_MASK(KEY_POWER);
1492 +       pwrbtn_dev->name = "retu-pwrbutton";
1493 +
1494 +       return input_register_device(pwrbtn_dev);
1495 +}
1496 +
1497 +/**
1498 + * Cleanup function which is called when driver is unloaded
1499 + */
1500 +static void __exit retubutton_exit(void)
1501 +{
1502 +       retu_free_irq(RETU_INT_PWR);
1503 +       del_timer_sync(&pwrbtn_timer);
1504 +       input_unregister_device(pwrbtn_dev);
1505 +}
1506 +
1507 +module_init(retubutton_init);
1508 +module_exit(retubutton_exit);
1509 +
1510 +MODULE_DESCRIPTION("Retu Power Button");
1511 +MODULE_LICENSE("GPL");
1512 +MODULE_AUTHOR("Ari Saastamoinen");
1513 --- /dev/null
1514 +++ linux-2.6.36-rc4/drivers/cbus/retu-rtc.c
1515 @@ -0,0 +1,477 @@
1516 +/**
1517 + * drivers/cbus/retu-rtc.c
1518 + *
1519 + * Support for Retu RTC
1520 + *
1521 + * Copyright (C) 2004, 2005 Nokia Corporation
1522 + *
1523 + * Written by Paul Mundt <paul.mundt@nokia.com> and
1524 + *            Igor Stoppa <igor.stoppa@nokia.com>
1525 + *
1526 + * The Retu RTC is essentially a partial read-only RTC that gives us Retu's
1527 + * idea of what time actually is. It's left as a userspace excercise to map
1528 + * this back to time in the real world and ensure that calibration settings
1529 + * are sane to compensate for any horrible drift (on account of not being able
1530 + * to set the clock to anything).
1531 + *
1532 + * Days are semi-writeable. Namely, Retu will only track 255 days for us
1533 + * consecutively, after which the counter is explicitly stuck at 255 until
1534 + * someone comes along and clears it with a write. In the event that no one
1535 + * comes along and clears it, we no longer have any idea what day it is.
1536 + *
1537 + * This file is subject to the terms and conditions of the GNU General
1538 + * Public License. See the file "COPYING" in the main directory of this
1539 + * archive for more details.
1540 + *
1541 + * This program is distributed in the hope that it will be useful,
1542 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1543 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1544 + * GNU General Public License for more details.
1545 + *
1546 + * You should have received a copy of the GNU General Public License
1547 + * along with this program; if not, write to the Free Software
1548 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1549 + */
1550 +
1551 +#include <linux/device.h>
1552 +#include <linux/init.h>
1553 +#include <linux/kernel.h>
1554 +#include <linux/module.h>
1555 +#include <linux/completion.h>
1556 +#include <linux/platform_device.h>
1557 +#include <linux/mutex.h>
1558 +#include <linux/workqueue.h>
1559 +
1560 +#include "cbus.h"
1561 +#include "retu.h"
1562 +
1563 +static struct mutex retu_rtc_mutex;
1564 +static u16 retu_rtc_alarm_expired;
1565 +static u16 retu_rtc_reset_occurred;
1566 +
1567 +static DECLARE_COMPLETION(retu_rtc_exited);
1568 +static DECLARE_COMPLETION(retu_rtc_sync);
1569 +
1570 +static void retu_rtc_barrier(void);
1571 +
1572 +static void retu_rtc_device_release(struct device *dev)
1573 +{
1574 +       complete(&retu_rtc_exited);
1575 +}
1576 +
1577 +static ssize_t retu_rtc_time_show(struct device *dev, struct device_attribute *attr,
1578 +                                 char *buf)
1579 +{
1580 +       u16 dsr, hmr, dsr2;
1581 +
1582 +       mutex_lock(&retu_rtc_mutex);
1583 +
1584 +       do {
1585 +               u16 dummy;
1586 +
1587 +               /*
1588 +                * Not being in_interrupt() for a retu rtc IRQ, we need to
1589 +                * read twice for consistency..
1590 +                */
1591 +               dummy   = retu_read_reg(RETU_REG_RTCDSR);
1592 +               dsr     = retu_read_reg(RETU_REG_RTCDSR);
1593 +
1594 +               dummy   = retu_read_reg(RETU_REG_RTCHMR);
1595 +               hmr     = retu_read_reg(RETU_REG_RTCHMR);
1596 +
1597 +               dummy   = retu_read_reg(RETU_REG_RTCDSR);
1598 +               dsr2    = retu_read_reg(RETU_REG_RTCDSR);
1599 +       } while ((dsr != dsr2));
1600 +
1601 +       mutex_unlock(&retu_rtc_mutex);
1602 +
1603 +       /*
1604 +        * Format a 32-bit date-string for userspace
1605 +        *
1606 +        * days | hours | minutes | seconds
1607 +        *
1608 +        * 8 bits for each.
1609 +        *
1610 +        * This mostly sucks because days and seconds are tracked in RTCDSR
1611 +        * while hours and minutes are tracked in RTCHMR. And yes, there
1612 +        * really are no words that can describe an 8 bit day register (or
1613 +        * rather, none that will be reprinted here).
1614 +        */
1615 +       return sprintf(buf, "0x%08x\n", (((dsr >> 8) & 0xff) << 24) |
1616 +                                       (((hmr >> 8) & 0x1f) << 16) |
1617 +                                        ((hmr & 0x3f) << 8) | (dsr & 0x3f));
1618 +}
1619 +
1620 +static ssize_t retu_rtc_time_store(struct device *dev, struct device_attribute *attr,
1621 +                                  const char *buf, size_t count)
1622 +{
1623 +       mutex_lock(&retu_rtc_mutex);
1624 +       /*
1625 +        * Writing anything to the day counter forces it to 0
1626 +        * The seconds counter would be cleared by resetting the minutes counter,
1627 +        * however this won't happen, since we are using the hh:mm counters as
1628 +        * a set of free running counters and the day counter as a multiple
1629 +        * overflow holder.
1630 +        */
1631 +
1632 +       /* Reset day counter, but keep Temperature Shutdown state */
1633 +       retu_write_reg(RETU_REG_RTCDSR,
1634 +                      retu_read_reg(RETU_REG_RTCDSR) & (1 << 6));
1635 +
1636 +       mutex_unlock(&retu_rtc_mutex);
1637 +
1638 +       return count;
1639 +}
1640 +
1641 +static DEVICE_ATTR(time, S_IRUGO | S_IWUSR, retu_rtc_time_show,
1642 +                  retu_rtc_time_store);
1643 +
1644 +
1645 +static ssize_t retu_rtc_reset_show(struct device *dev, struct device_attribute *attr, char *buf)
1646 +{
1647 +       /*
1648 +        * Returns the status of the rtc
1649 +        *
1650 +        * 0: no reset has occurred or the status has been cleared
1651 +        * 1: a reset has occurred
1652 +        *
1653 +        * RTC needs to be reset only when both main battery
1654 +        * _AND_ backup battery are discharged
1655 +        */
1656 +       return sprintf(buf, "%u\n", retu_rtc_reset_occurred);
1657 +}
1658 +
1659 +static void retu_rtc_do_reset(void)
1660 +{
1661 +       u16 ccr1;
1662 +
1663 +       ccr1 = retu_read_reg(RETU_REG_CC1);
1664 +       /* RTC in reset */
1665 +       retu_write_reg(RETU_REG_CC1, ccr1 | 0x0001);
1666 +       /* RTC in normal operating mode */
1667 +       retu_write_reg(RETU_REG_CC1, ccr1 & ~0x0001);
1668 +
1669 +       retu_rtc_barrier();
1670 +       /* Disable alarm and RTC WD */
1671 +       retu_write_reg(RETU_REG_RTCHMAR, 0x7f3f);
1672 +       /* Set Calibration register to default value */
1673 +       retu_write_reg(RETU_REG_RTCCALR, 0x00c0);
1674 +
1675 +       retu_rtc_alarm_expired = 0;
1676 +       retu_rtc_reset_occurred = 1;
1677 +}
1678 +
1679 +static ssize_t retu_rtc_reset_store(struct device *dev, struct device_attribute *attr,
1680 +                                   const char *buf, size_t count)
1681 +{
1682 +       unsigned choice;
1683 +
1684 +       if(sscanf(buf, "%u", &choice) != 1)
1685 +               return count;
1686 +       mutex_lock(&retu_rtc_mutex);
1687 +       if (choice == 0)
1688 +               retu_rtc_reset_occurred = 0;
1689 +       else if (choice == 1)
1690 +               retu_rtc_do_reset();
1691 +       mutex_unlock(&retu_rtc_mutex);
1692 +       return count;
1693 +}
1694 +
1695 +static DEVICE_ATTR(reset, S_IRUGO | S_IWUSR, retu_rtc_reset_show,
1696 +                  retu_rtc_reset_store);
1697 +
1698 +static ssize_t retu_rtc_alarm_show(struct device *dev, struct device_attribute *attr,
1699 +                                  char *buf)
1700 +{
1701 +       u16 chmar;
1702 +       ssize_t retval;
1703 +
1704 +       mutex_lock(&retu_rtc_mutex);
1705 +       /*
1706 +        * Format a 16-bit date-string for userspace
1707 +        *
1708 +        * hours | minutes
1709 +        * 8 bits for each.
1710 +        */
1711 +       chmar = retu_read_reg(RETU_REG_RTCHMAR);
1712 +       /* No shifting needed, only masking unrelated bits */
1713 +       retval = sprintf(buf, "0x%04x\n", chmar & 0x1f3f);
1714 +       mutex_unlock(&retu_rtc_mutex);
1715 +
1716 +       return retval;
1717 +}
1718 +
1719 +static ssize_t retu_rtc_alarm_store(struct device *dev, struct device_attribute *attr,
1720 +                                   const char *buf, size_t count)
1721 +{
1722 +       u16 chmar;
1723 +       unsigned alrm;
1724 +       unsigned hours;
1725 +       unsigned minutes;
1726 +
1727 +       mutex_lock(&retu_rtc_mutex);
1728 +
1729 +       if(sscanf(buf, "%x", &alrm) != 1)
1730 +               return count;
1731 +       hours = (alrm >> 8) & 0x001f;
1732 +       minutes = (alrm >> 0) & 0x003f;
1733 +       if ((hours < 24 && minutes < 60) || (hours == 24 && minutes == 60)) {
1734 +               /*
1735 +                * OK, the time format for the alarm is valid (including the
1736 +                * disabling values)
1737 +                */
1738 +               /* Keeps the RTC watchdog status */
1739 +               chmar = retu_read_reg(RETU_REG_RTCHMAR) & 0x6000;
1740 +               chmar |= alrm & 0x1f3f; /* Stores the requested alarm */
1741 +               retu_rtc_barrier();
1742 +               retu_write_reg(RETU_REG_RTCHMAR, chmar);
1743 +               /* If the alarm is being disabled */
1744 +               if (hours == 24 && minutes == 60) {
1745 +                       /* disable the interrupt */
1746 +                       retu_disable_irq(RETU_INT_RTCA);
1747 +                       retu_rtc_alarm_expired = 0;
1748 +               } else
1749 +                       /* enable the interrupt */
1750 +                       retu_enable_irq(RETU_INT_RTCA);
1751 +       }
1752 +       mutex_unlock(&retu_rtc_mutex);
1753 +
1754 +       return count;
1755 +}
1756 +
1757 +static DEVICE_ATTR(alarm, S_IRUGO | S_IWUSR, retu_rtc_alarm_show,
1758 +                  retu_rtc_alarm_store);
1759 +
1760 +static ssize_t retu_rtc_alarm_expired_show(struct device *dev, struct device_attribute *attr,
1761 +                                          char *buf)
1762 +{
1763 +       ssize_t retval;
1764 +
1765 +       retval = sprintf(buf, "%u\n", retu_rtc_alarm_expired);
1766 +
1767 +       return retval;
1768 +}
1769 +
1770 +static ssize_t retu_rtc_alarm_expired_store(struct device *dev, struct device_attribute *attr,
1771 +                                           const char *buf, size_t count)
1772 +{
1773 +       retu_rtc_alarm_expired = 0;
1774 +
1775 +       return count;
1776 +}
1777 +
1778 +static DEVICE_ATTR(alarm_expired, S_IRUGO | S_IWUSR, retu_rtc_alarm_expired_show,
1779 +                  retu_rtc_alarm_expired_store);
1780 +
1781 +
1782 +static ssize_t retu_rtc_cal_show(struct device *dev, struct device_attribute *attr,
1783 +                                char *buf)
1784 +{
1785 +       u16 rtccalr1;
1786 +
1787 +       mutex_lock(&retu_rtc_mutex);
1788 +       rtccalr1 = retu_read_reg(RETU_REG_RTCCALR);
1789 +       mutex_unlock(&retu_rtc_mutex);
1790 +
1791 +       /*
1792 +        * Shows the status of the Calibration Register.
1793 +        *
1794 +        * Default, after power loss: 0x0000
1795 +        * Default, for R&D: 0x00C0
1796 +        * Default, for factory: 0x00??
1797 +        *
1798 +        */
1799 +       return sprintf(buf, "0x%04x\n", rtccalr1 & 0x00ff);
1800 +}
1801 +
1802 +static ssize_t retu_rtc_cal_store(struct device *dev, struct device_attribute *attr,
1803 +                                 const char *buf, size_t count)
1804 +{
1805 +       unsigned calibration_value;
1806 +
1807 +       if (sscanf(buf, "%x", &calibration_value) != 1)
1808 +               return count;
1809 +
1810 +       mutex_lock(&retu_rtc_mutex);
1811 +       retu_rtc_barrier();
1812 +       retu_write_reg(RETU_REG_RTCCALR, calibration_value & 0x00ff);
1813 +       mutex_unlock(&retu_rtc_mutex);
1814 +
1815 +       return count;
1816 +}
1817 +
1818 +static DEVICE_ATTR(cal, S_IRUGO | S_IWUSR, retu_rtc_cal_show,
1819 +                  retu_rtc_cal_store);
1820 +
1821 +static struct platform_device retu_rtc_device;
1822 +
1823 +static void retu_rtca_disable(void)
1824 +{
1825 +       retu_disable_irq(RETU_INT_RTCA);
1826 +       retu_rtc_alarm_expired = 1;
1827 +       retu_rtc_barrier();
1828 +       retu_write_reg(RETU_REG_RTCHMAR, (24 << 8) | 60);
1829 +}
1830 +
1831 +static void retu_rtca_expired(struct work_struct *unused)
1832 +{
1833 +       retu_rtca_disable();
1834 +       sysfs_notify(&retu_rtc_device.dev.kobj, NULL, "alarm_expired");
1835 +}
1836 +
1837 +DECLARE_WORK(retu_rtca_work, retu_rtca_expired);
1838 +
1839 +/*
1840 + * RTCHMR RTCHMAR RTCCAL must be accessed within 0.9 s since the seconds
1841 + * interrupt has been signaled in the IDR register
1842 + */
1843 +static void retu_rtcs_interrupt(unsigned long unused)
1844 +{
1845 +       retu_ack_irq(RETU_INT_RTCS);
1846 +       complete_all(&retu_rtc_sync);
1847 +}
1848 +
1849 +static void retu_rtca_interrupt(unsigned long unused)
1850 +{
1851 +       retu_ack_irq(RETU_INT_RTCA);
1852 +       schedule_work(&retu_rtca_work);
1853 +}
1854 +
1855 +static int retu_rtc_init_irq(void)
1856 +{
1857 +       int ret;
1858 +
1859 +       ret = retu_request_irq(RETU_INT_RTCS, retu_rtcs_interrupt, 0, "RTCS");
1860 +       if (ret != 0)
1861 +               return ret;
1862 +       /*
1863 +        * We will take care of enabling and disabling the interrupt
1864 +        * elsewhere, so leave it off by default..
1865 +        */
1866 +       retu_disable_irq(RETU_INT_RTCS);
1867 +
1868 +       ret = retu_request_irq(RETU_INT_RTCA, retu_rtca_interrupt, 0, "RTCA");
1869 +       if (ret != 0) {
1870 +               retu_free_irq(RETU_INT_RTCS);
1871 +               return ret;
1872 +       }
1873 +       retu_disable_irq(RETU_INT_RTCA);
1874 +
1875 +       return 0;
1876 +}
1877 +
1878 +
1879 +static int __devinit retu_rtc_probe(struct device *dev)
1880 +{
1881 +       int r;
1882 +
1883 +       retu_rtc_alarm_expired = retu_read_reg(RETU_REG_IDR) &
1884 +                                              (0x1 << RETU_INT_RTCA);
1885 +
1886 +       if ((r = retu_rtc_init_irq()) != 0)
1887 +               return r;
1888 +
1889 +       mutex_init(&retu_rtc_mutex);
1890 +
1891 +       /* If the calibration register is zero, we've probably lost
1892 +        * power */
1893 +       if (retu_read_reg(RETU_REG_RTCCALR) & 0x00ff)
1894 +               retu_rtc_reset_occurred = 0;
1895 +       else
1896 +               retu_rtc_do_reset();
1897 +
1898 +       if ((r = device_create_file(dev, &dev_attr_time)) != 0)
1899 +               return r;
1900 +       else if ((r = device_create_file(dev, &dev_attr_reset)) != 0)
1901 +               goto err_unregister_time;
1902 +       else if ((r = device_create_file(dev, &dev_attr_alarm)) != 0)
1903 +               goto err_unregister_reset;
1904 +       else if ((r = device_create_file(dev, &dev_attr_alarm_expired)) != 0)
1905 +               goto err_unregister_alarm;
1906 +       else if ((r = device_create_file(dev, &dev_attr_cal)) != 0)
1907 +               goto err_unregister_alarm_expired;
1908 +       else
1909 +               return r;
1910 +
1911 +err_unregister_alarm_expired:
1912 +       device_remove_file(dev, &dev_attr_alarm_expired);
1913 +err_unregister_alarm:
1914 +       device_remove_file(dev, &dev_attr_alarm);
1915 +err_unregister_reset:
1916 +       device_remove_file(dev, &dev_attr_reset);
1917 +err_unregister_time:
1918 +       device_remove_file(dev, &dev_attr_time);
1919 +       return r;
1920 +}
1921 +
1922 +static int __devexit retu_rtc_remove(struct device *dev)
1923 +{
1924 +       retu_disable_irq(RETU_INT_RTCS);
1925 +       retu_free_irq(RETU_INT_RTCS);
1926 +       retu_free_irq(RETU_INT_RTCA);
1927 +       device_remove_file(dev, &dev_attr_cal);
1928 +       device_remove_file(dev, &dev_attr_alarm_expired);
1929 +       device_remove_file(dev, &dev_attr_alarm);
1930 +       device_remove_file(dev, &dev_attr_reset);
1931 +       device_remove_file(dev, &dev_attr_time);
1932 +       return 0;
1933 +}
1934 +
1935 +static struct device_driver retu_rtc_driver = {
1936 +       .name           = "retu-rtc",
1937 +       .bus            = &platform_bus_type,
1938 +       .probe          = retu_rtc_probe,
1939 +       .remove         = __devexit_p(retu_rtc_remove),
1940 +};
1941 +
1942 +static struct platform_device retu_rtc_device = {
1943 +       .name           = "retu-rtc",
1944 +       .id             = -1,
1945 +       .dev            = {
1946 +               .release        = retu_rtc_device_release,
1947 +       },
1948 +};
1949 +
1950 +/* This function provides syncronization with the RTCS interrupt handler */
1951 +static void retu_rtc_barrier(void)
1952 +{
1953 +       INIT_COMPLETION(retu_rtc_sync);
1954 +       retu_ack_irq(RETU_INT_RTCS);
1955 +       retu_enable_irq(RETU_INT_RTCS);
1956 +       wait_for_completion(&retu_rtc_sync);
1957 +       retu_disable_irq(RETU_INT_RTCS);
1958 +}
1959 +
1960 +static int __init retu_rtc_init(void)
1961 +{
1962 +       int ret;
1963 +
1964 +       init_completion(&retu_rtc_exited);
1965 +
1966 +       if ((ret = driver_register(&retu_rtc_driver)) != 0)
1967 +               return ret;
1968 +
1969 +       if ((ret = platform_device_register(&retu_rtc_device)) != 0)
1970 +               goto err_unregister_driver;
1971 +
1972 +       return 0;
1973 +
1974 +err_unregister_driver:
1975 +       driver_unregister(&retu_rtc_driver);
1976 +       return ret;
1977 +}
1978 +
1979 +static void __exit retu_rtc_exit(void)
1980 +{
1981 +       platform_device_unregister(&retu_rtc_device);
1982 +       driver_unregister(&retu_rtc_driver);
1983 +
1984 +       wait_for_completion(&retu_rtc_exited);
1985 +}
1986 +
1987 +module_init(retu_rtc_init);
1988 +module_exit(retu_rtc_exit);
1989 +
1990 +MODULE_DESCRIPTION("Retu RTC");
1991 +MODULE_LICENSE("GPL");
1992 +MODULE_AUTHOR("Paul Mundt and Igor Stoppa");
1993 --- /dev/null
1994 +++ linux-2.6.36-rc4/drivers/cbus/retu-user.c
1995 @@ -0,0 +1,425 @@
1996 +/**
1997 + * drivers/cbus/retu-user.c
1998 + *
1999 + * Retu user space interface functions
2000 + *
2001 + * Copyright (C) 2004, 2005 Nokia Corporation
2002 + *
2003 + * Written by Mikko Ylinen <mikko.k.ylinen@nokia.com>
2004 + *
2005 + * This file is subject to the terms and conditions of the GNU General
2006 + * Public License. See the file "COPYING" in the main directory of this
2007 + * archive for more details.
2008 + *
2009 + * This program is distributed in the hope that it will be useful,
2010 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2011 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2012 + * GNU General Public License for more details.
2013 + *
2014 + * You should have received a copy of the GNU General Public License
2015 + * along with this program; if not, write to the Free Software
2016 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
2017 + */
2018 +
2019 +#include <linux/types.h>
2020 +#include <linux/kernel.h>
2021 +#include <linux/interrupt.h>
2022 +#include <linux/module.h>
2023 +#include <linux/init.h>
2024 +#include <linux/fs.h>
2025 +#include <linux/miscdevice.h>
2026 +#include <linux/poll.h>
2027 +#include <linux/list.h>
2028 +#include <linux/spinlock.h>
2029 +#include <linux/sched.h>
2030 +#include <linux/mutex.h>
2031 +#include <linux/slab.h>
2032 +
2033 +#include <asm/uaccess.h>
2034 +
2035 +#include "retu.h"
2036 +
2037 +#include "user_retu_tahvo.h"
2038 +
2039 +/* Maximum size of IRQ node buffer/pool */
2040 +#define RETU_MAX_IRQ_BUF_LEN   16
2041 +
2042 +#define PFX                    "retu-user: "
2043 +
2044 +/* Bitmap for marking the interrupt sources as having the handlers */
2045 +static u32 retu_irq_bits;
2046 +
2047 +/* For allowing only one user process to subscribe to the retu interrupts */
2048 +static struct file *retu_irq_subscr = NULL;
2049 +
2050 +/* For poll and IRQ passing */
2051 +struct retu_irq {
2052 +       u32 id;
2053 +       struct list_head node;
2054 +};
2055 +
2056 +static spinlock_t retu_irqs_lock;
2057 +static struct retu_irq *retu_irq_block;
2058 +static LIST_HEAD(retu_irqs);
2059 +static LIST_HEAD(retu_irqs_reserve);
2060 +
2061 +/* Wait queue - used when user wants to read the device */
2062 +DECLARE_WAIT_QUEUE_HEAD(retu_user_waitqueue);
2063 +
2064 +/* Semaphore to protect irq subscription sequence */
2065 +static struct mutex retu_mutex;
2066 +
2067 +/* This array specifies RETU register types (read/write/toggle) */
2068 +static const u8 retu_access_bits[] = {
2069 +       1,
2070 +       4,
2071 +       3,
2072 +       3,
2073 +       1,
2074 +       3,
2075 +       3,
2076 +       0,
2077 +       3,
2078 +       3,
2079 +       3,
2080 +       3,
2081 +       3,
2082 +       3,
2083 +       3,
2084 +       4,
2085 +       4,
2086 +       3,
2087 +       0,
2088 +       0,
2089 +       0,
2090 +       0,
2091 +       1,
2092 +       3,
2093 +       3,
2094 +       3,
2095 +       3,
2096 +       3,
2097 +       3,
2098 +       3,
2099 +       3,
2100 +       3
2101 +};
2102 +
2103 +/*
2104 + * The handler for all RETU interrupts.
2105 + *
2106 + * arg is the interrupt source in RETU.
2107 + */
2108 +static void retu_user_irq_handler(unsigned long arg)
2109 +{
2110 +       struct retu_irq *irq;
2111 +
2112 +       retu_ack_irq(arg);
2113 +
2114 +       spin_lock(&retu_irqs_lock);
2115 +       if (list_empty(&retu_irqs_reserve)) {
2116 +               spin_unlock(&retu_irqs_lock);
2117 +               return;
2118 +       }
2119 +       irq = list_entry((&retu_irqs_reserve)->next, struct retu_irq, node);
2120 +       irq->id = arg;
2121 +       list_move_tail(&irq->node, &retu_irqs);
2122 +       spin_unlock(&retu_irqs_lock);
2123 +
2124 +       /* wake up waiting thread */
2125 +       wake_up(&retu_user_waitqueue);
2126 +}
2127 +
2128 +/*
2129 + * This routine sets up the interrupt handler and marks an interrupt source
2130 + * in RETU as a candidate for signal delivery to the user process.
2131 + */
2132 +static int retu_user_subscribe_to_irq(int id, struct file *filp)
2133 +{
2134 +       int ret;
2135 +
2136 +       mutex_lock(&retu_mutex);
2137 +       if ((retu_irq_subscr != NULL) && (retu_irq_subscr != filp)) {
2138 +               mutex_unlock(&retu_mutex);
2139 +               return -EBUSY;
2140 +       }
2141 +       /* Store the file pointer of the first user process registering IRQs */
2142 +       retu_irq_subscr = filp;
2143 +       mutex_unlock(&retu_mutex);
2144 +
2145 +       if (retu_irq_bits & (1 << id))
2146 +               return 0;
2147 +
2148 +       ret = retu_request_irq(id, retu_user_irq_handler, id, "");
2149 +       if (ret < 0)
2150 +               return ret;
2151 +
2152 +       /* Mark that this interrupt has a handler */
2153 +       retu_irq_bits |= 1 << id;
2154 +
2155 +       return 0;
2156 +}
2157 +
2158 +/*
2159 + * Unregisters all RETU interrupt handlers.
2160 + */
2161 +static void retu_unreg_irq_handlers(void)
2162 +{
2163 +       int id;
2164 +
2165 +       if (!retu_irq_bits)
2166 +               return;
2167 +
2168 +       for (id = 0; id < MAX_RETU_IRQ_HANDLERS; id++)
2169 +               if (retu_irq_bits & (1 << id))
2170 +                       retu_free_irq(id);
2171 +
2172 +       retu_irq_bits = 0;
2173 +}
2174 +
2175 +/*
2176 + * Write to RETU register.
2177 + * Returns 0 upon success, a negative error value otherwise.
2178 + */
2179 +static int retu_user_write_with_mask(u32 field, u16 value)
2180 +{
2181 +       u32 mask;
2182 +       u32 reg;
2183 +       u_short tmp;
2184 +       unsigned long flags;
2185 +
2186 +       mask = MASK(field);
2187 +       reg = REG(field);
2188 +
2189 +       /* Detect bad mask and reg */
2190 +       if (mask == 0 || reg > RETU_REG_MAX ||
2191 +           retu_access_bits[reg] == READ_ONLY) {
2192 +               printk(KERN_ERR PFX "invalid arguments (reg=%#x, mask=%#x)\n",
2193 +                      reg, mask);
2194 +               return -EINVAL;
2195 +       }
2196 +
2197 +       /* Justify value according to mask */
2198 +       while (!(mask & 1)) {
2199 +               value = value << 1;
2200 +               mask = mask >> 1;
2201 +       }
2202 +
2203 +       spin_lock_irqsave(&retu_lock, flags);
2204 +       if (retu_access_bits[reg] == TOGGLE) {
2205 +               /* No need to detect previous content of register */
2206 +               tmp = 0;
2207 +       } else {
2208 +               /* Read current value of register */
2209 +               tmp = retu_read_reg(reg);
2210 +       }
2211 +
2212 +       /* Generate new value */
2213 +       tmp = (tmp & ~MASK(field)) | (value & MASK(field));
2214 +       /* Write data to RETU */
2215 +       retu_write_reg(reg, tmp);
2216 +       spin_unlock_irqrestore(&retu_lock, flags);
2217 +
2218 +       return 0;
2219 +}
2220 +
2221 +/*
2222 + * Read RETU register.
2223 + */
2224 +static u32 retu_user_read_with_mask(u32 field)
2225 +{
2226 +       u_short value;
2227 +       u32 mask, reg;
2228 +
2229 +       mask = MASK(field);
2230 +       reg = REG(field);
2231 +
2232 +       /* Detect bad mask and reg */
2233 +       if (mask == 0 || reg > RETU_REG_MAX) {
2234 +               printk(KERN_ERR PFX "invalid arguments (reg=%#x, mask=%#x)\n",
2235 +                      reg, mask);
2236 +               return -EINVAL;
2237 +       }
2238 +
2239 +       /* Read the register */
2240 +       value = retu_read_reg(reg) & mask;
2241 +
2242 +       /* Right justify value */
2243 +       while (!(mask & 1)) {
2244 +               value = value >> 1;
2245 +               mask = mask >> 1;
2246 +       }
2247 +
2248 +       return value;
2249 +}
2250 +
2251 +/*
2252 + * Close device
2253 + */
2254 +static int retu_close(struct inode *inode, struct file *filp)
2255 +{
2256 +       /* Unregister all interrupts that have been registered */
2257 +       if (retu_irq_subscr == filp) {
2258 +               retu_unreg_irq_handlers();
2259 +               retu_irq_subscr = NULL;
2260 +       }
2261 +
2262 +       return 0;
2263 +}
2264 +
2265 +/*
2266 + * Device control (ioctl)
2267 + */
2268 +static int retu_ioctl(struct inode *inode, struct file *filp,
2269 +                     unsigned int cmd, unsigned long arg)
2270 +{
2271 +       struct retu_tahvo_write_parms par;
2272 +       int ret;
2273 +
2274 +       switch (cmd) {
2275 +       case URT_IOCT_IRQ_SUBSCR:
2276 +               return retu_user_subscribe_to_irq(arg, filp);
2277 +       case RETU_IOCH_READ:
2278 +               return retu_user_read_with_mask(arg);
2279 +       case RETU_IOCX_WRITE:
2280 +               ret = copy_from_user(&par, (void __user *) arg, sizeof(par));
2281 +               if (ret)
2282 +                       printk(KERN_ERR "copy_from_user failed: %d\n", ret);
2283 +               par.result = retu_user_write_with_mask(par.field, par.value);
2284 +               ret = copy_to_user((void __user *) arg, &par, sizeof(par));
2285 +               if (ret)
2286 +                       printk(KERN_ERR "copy_to_user failed: %d\n", ret);
2287 +               break;
2288 +       case RETU_IOCH_ADC_READ:
2289 +               return retu_read_adc(arg);
2290 +       default:
2291 +               return -ENOIOCTLCMD;
2292 +       }
2293 +       return 0;
2294 +}
2295 +
2296 +/*
2297 + * Read from device
2298 + */
2299 +static ssize_t retu_read(struct file *filp, char *buf, size_t count,
2300 +                        loff_t * offp)
2301 +{
2302 +       struct retu_irq *irq;
2303 +
2304 +       u32 nr, i;
2305 +
2306 +       /* read not permitted if neither filp nor anyone has registered IRQs */
2307 +       if (retu_irq_subscr != filp)
2308 +               return -EPERM;
2309 +
2310 +       if ((count < sizeof(u32)) || ((count % sizeof(u32)) != 0))
2311 +               return -EINVAL;
2312 +
2313 +       nr = count / sizeof(u32);
2314 +
2315 +       for (i = 0; i < nr; i++) {
2316 +               unsigned long flags;
2317 +               u32 irq_id;
2318 +               int ret;
2319 +
2320 +               ret = wait_event_interruptible(retu_user_waitqueue,
2321 +                                              !list_empty(&retu_irqs));
2322 +               if (ret < 0)
2323 +                       return ret;
2324 +
2325 +               spin_lock_irqsave(&retu_irqs_lock, flags);
2326 +               irq = list_entry((&retu_irqs)->next, struct retu_irq, node);
2327 +               irq_id = irq->id;
2328 +               list_move(&irq->node, &retu_irqs_reserve);
2329 +               spin_unlock_irqrestore(&retu_irqs_lock, flags);
2330 +
2331 +               ret = copy_to_user(buf + i * sizeof(irq_id), &irq_id,
2332 +                                  sizeof(irq_id));
2333 +               if (ret)
2334 +                       printk(KERN_ERR "copy_to_user failed: %d\n", ret);
2335 +       }
2336 +
2337 +       return count;
2338 +}
2339 +
2340 +/*
2341 + * Poll method
2342 + */
2343 +static unsigned retu_poll(struct file *filp, struct poll_table_struct *pt)
2344 +{
2345 +       if (!list_empty(&retu_irqs))
2346 +               return POLLIN;
2347 +
2348 +       poll_wait(filp, &retu_user_waitqueue, pt);
2349 +
2350 +       if (!list_empty(&retu_irqs))
2351 +               return POLLIN;
2352 +       else
2353 +               return 0;
2354 +}
2355 +
2356 +static struct file_operations retu_user_fileops = {
2357 +       .owner = THIS_MODULE,
2358 +       .unlocked_ioctl = retu_ioctl,
2359 +       .read = retu_read,
2360 +       .release = retu_close,
2361 +       .poll = retu_poll
2362 +};
2363 +
2364 +static struct miscdevice retu_device = {
2365 +       .minor = MISC_DYNAMIC_MINOR,
2366 +       .name = "retu",
2367 +       .fops = &retu_user_fileops
2368 +};
2369 +
2370 +/*
2371 + * Initialization
2372 + *
2373 + * @return 0 if successful, error value otherwise.
2374 + */
2375 +int retu_user_init(void)
2376 +{
2377 +       struct retu_irq *irq;
2378 +       int res, i;
2379 +
2380 +       irq = kmalloc(sizeof(*irq) * RETU_MAX_IRQ_BUF_LEN, GFP_KERNEL);
2381 +       if (irq == NULL) {
2382 +               printk(KERN_ERR PFX "kmalloc failed\n");
2383 +               return -ENOMEM;
2384 +       }
2385 +       memset(irq, 0, sizeof(*irq) * RETU_MAX_IRQ_BUF_LEN);
2386 +       for (i = 0; i < RETU_MAX_IRQ_BUF_LEN; i++)
2387 +               list_add(&irq[i].node, &retu_irqs_reserve);
2388 +
2389 +       retu_irq_block = irq;
2390 +
2391 +       spin_lock_init(&retu_irqs_lock);
2392 +       mutex_init(&retu_mutex);
2393 +
2394 +       /* Request a misc device */
2395 +       res = misc_register(&retu_device);
2396 +       if (res < 0) {
2397 +               printk(KERN_ERR PFX "unable to register misc device for %s\n",
2398 +                      retu_device.name);
2399 +               kfree(irq);
2400 +               return res;
2401 +       }
2402 +
2403 +       return 0;
2404 +}
2405 +
2406 +/*
2407 + * Cleanup.
2408 + */
2409 +void retu_user_cleanup(void)
2410 +{
2411 +       /* Unregister our misc device */
2412 +       misc_deregister(&retu_device);
2413 +       /* Unregister and disable all RETU interrupts used by this module */
2414 +       retu_unreg_irq_handlers();
2415 +       kfree(retu_irq_block);
2416 +}
2417 +
2418 +MODULE_DESCRIPTION("Retu ASIC user space functions");
2419 +MODULE_LICENSE("GPL");
2420 +MODULE_AUTHOR("Mikko Ylinen");
2421 --- /dev/null
2422 +++ linux-2.6.36-rc4/drivers/cbus/retu-wdt.c
2423 @@ -0,0 +1,388 @@
2424 +/**
2425 + * drivers/cbus/retu-wdt.c
2426 + *
2427 + * Driver for Retu watchdog
2428 + *
2429 + * Copyright (C) 2004, 2005 Nokia Corporation
2430 + *
2431 + * Written by Amit Kucheria <amit.kucheria@nokia.com>
2432 + *
2433 + * This file is subject to the terms and conditions of the GNU General
2434 + * Public License. See the file "COPYING" in the main directory of this
2435 + * archive for more details.
2436 + *
2437 + * This program is distributed in the hope that it will be useful,
2438 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2439 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2440 + * GNU General Public License for more details.
2441 + *
2442 + * You should have received a copy of the GNU General Public License
2443 + * along with this program; if not, write to the Free Software
2444 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
2445 + */
2446 +
2447 +#include <linux/kernel.h>
2448 +#include <linux/module.h>
2449 +#include <linux/device.h>
2450 +#include <linux/init.h>
2451 +#include <linux/fs.h>
2452 +#include <linux/io.h>
2453 +#include <linux/platform_device.h>
2454 +#include <linux/slab.h>
2455 +
2456 +#include <linux/completion.h>
2457 +#include <linux/errno.h>
2458 +#include <linux/moduleparam.h>
2459 +#include <linux/platform_device.h>
2460 +#include <linux/miscdevice.h>
2461 +#include <linux/watchdog.h>
2462 +
2463 +#include <asm/uaccess.h>
2464 +
2465 +#include <plat/prcm.h>
2466 +
2467 +#include "cbus.h"
2468 +#include "retu.h"
2469 +
2470 +/* Watchdog timeout in seconds */
2471 +#define RETU_WDT_MIN_TIMER 0
2472 +#define RETU_WDT_DEFAULT_TIMER 32
2473 +#define RETU_WDT_MAX_TIMER 63
2474 +
2475 +static struct completion retu_wdt_completion;
2476 +static DEFINE_MUTEX(retu_wdt_mutex);
2477 +
2478 +/* Current period of watchdog */
2479 +static unsigned int period_val = RETU_WDT_DEFAULT_TIMER;
2480 +static int counter_param = RETU_WDT_MAX_TIMER;
2481 +
2482 +struct retu_wdt_dev {
2483 +       struct device           *dev;
2484 +       int                     users;
2485 +       struct miscdevice       retu_wdt_miscdev;
2486 +       struct timer_list       ping_timer;
2487 +};
2488 +
2489 +static struct retu_wdt_dev *retu_wdt;
2490 +
2491 +static void retu_wdt_set_ping_timer(unsigned long enable);
2492 +
2493 +static int _retu_modify_counter(unsigned int new)
2494 +{
2495 +       retu_write_reg(RETU_REG_WATCHDOG, (u16)new);
2496 +
2497 +       return 0;
2498 +}
2499 +
2500 +static int retu_modify_counter(unsigned int new)
2501 +{
2502 +       if (new < RETU_WDT_MIN_TIMER || new > RETU_WDT_MAX_TIMER)
2503 +               return -EINVAL;
2504 +
2505 +       mutex_lock(&retu_wdt_mutex);
2506 +       period_val = new;
2507 +       _retu_modify_counter(period_val);
2508 +       mutex_unlock(&retu_wdt_mutex);
2509 +
2510 +       return 0;
2511 +}
2512 +
2513 +static ssize_t retu_wdt_period_show(struct device *dev,
2514 +                               struct device_attribute *attr, char *buf)
2515 +{
2516 +       /* Show current max counter */
2517 +       return sprintf(buf, "%u\n", (u16)period_val);
2518 +}
2519 +
2520 +/*
2521 + * Note: This inteface is non-standard and likely to disappear!
2522 + * Use /dev/watchdog instead, that's the standard.
2523 + */
2524 +static ssize_t retu_wdt_period_store(struct device *dev,
2525 +                               struct device_attribute *attr,
2526 +                               const char *buf, size_t count)
2527 +{
2528 +       unsigned int new_period;
2529 +       int ret;
2530 +
2531 +#ifdef CONFIG_WATCHDOG_NOWAYOUT
2532 +       retu_wdt_set_ping_timer(0);
2533 +#endif
2534 +
2535 +       if (sscanf(buf, "%u", &new_period) != 1) {
2536 +               printk(KERN_ALERT "retu_wdt_period_store: Invalid input\n");
2537 +               return -EINVAL;
2538 +       }
2539 +
2540 +       ret = retu_modify_counter(new_period);
2541 +       if (ret < 0)
2542 +               return ret;
2543 +
2544 +       return strnlen(buf, count);
2545 +}
2546 +
2547 +static ssize_t retu_wdt_counter_show(struct device *dev,
2548 +                               struct device_attribute *attr, char *buf)
2549 +{
2550 +       u16 counter;
2551 +
2552 +       /* Show current value in watchdog counter */
2553 +       counter = retu_read_reg(RETU_REG_WATCHDOG);
2554 +
2555 +       /* Only the 5 LSB are important */
2556 +       return snprintf(buf, PAGE_SIZE, "%u\n", (counter & 0x3F));
2557 +}
2558 +
2559 +static DEVICE_ATTR(period, S_IRUGO | S_IWUSR, retu_wdt_period_show, \
2560 +                       retu_wdt_period_store);
2561 +static DEVICE_ATTR(counter, S_IRUGO, retu_wdt_counter_show, NULL);
2562 +
2563 +/*----------------------------------------------------------------------------*/
2564 +
2565 +/*
2566 + * Since retu watchdog cannot be disabled in hardware, we must kick it
2567 + * with a timer until userspace watchdog software takes over. Do this
2568 + * unless /dev/watchdog is open or CONFIG_WATCHDOG_NOWAYOUT is set.
2569 + */
2570 +static void retu_wdt_set_ping_timer(unsigned long enable)
2571 +{
2572 +       _retu_modify_counter(RETU_WDT_MAX_TIMER);
2573 +       if (enable)
2574 +               mod_timer(&retu_wdt->ping_timer,
2575 +                               jiffies + RETU_WDT_DEFAULT_TIMER * HZ);
2576 +       else
2577 +               del_timer_sync(&retu_wdt->ping_timer);
2578 +}
2579 +
2580 +static int retu_wdt_open(struct inode *inode, struct file *file)
2581 +{
2582 +       if (test_and_set_bit(1, (unsigned long *)&(retu_wdt->users)))
2583 +               return -EBUSY;
2584 +
2585 +       file->private_data = (void *)retu_wdt;
2586 +       retu_wdt_set_ping_timer(0);
2587 +
2588 +       return nonseekable_open(inode, file);
2589 +}
2590 +
2591 +static int retu_wdt_release(struct inode *inode, struct file *file)
2592 +{
2593 +       struct retu_wdt_dev *wdev = file->private_data;
2594 +
2595 +#ifndef CONFIG_WATCHDOG_NOWAYOUT
2596 +       retu_wdt_set_ping_timer(1);
2597 +#endif
2598 +       wdev->users = 0;
2599 +
2600 +       return 0;
2601 +}
2602 +
2603 +static ssize_t retu_wdt_write(struct file *file, const char __user *data,
2604 +                                               size_t len, loff_t *ppos)
2605 +{
2606 +       if (len)
2607 +               retu_modify_counter(RETU_WDT_MAX_TIMER);
2608 +
2609 +       return len;
2610 +}
2611 +
2612 +static int retu_wdt_ioctl(struct inode *inode, struct file *file,
2613 +                                       unsigned int cmd, unsigned long arg)
2614 +{
2615 +       int new_margin;
2616 +
2617 +       static struct watchdog_info ident = {
2618 +               .identity = "Retu Watchdog",
2619 +               .options = WDIOF_SETTIMEOUT,
2620 +               .firmware_version = 0,
2621 +       };
2622 +
2623 +       switch (cmd) {
2624 +       default:
2625 +               return -ENOTTY;
2626 +       case WDIOC_GETSUPPORT:
2627 +               return copy_to_user((struct watchdog_info __user *)arg, &ident,
2628 +                                                       sizeof(ident));
2629 +       case WDIOC_GETSTATUS:
2630 +               return put_user(0, (int __user *)arg);
2631 +       case WDIOC_GETBOOTSTATUS:
2632 +               if (cpu_is_omap16xx())
2633 +                       return put_user(omap_readw(ARM_SYSST),
2634 +                                       (int __user *)arg);
2635 +               if (cpu_is_omap24xx())
2636 +                       return put_user(omap_prcm_get_reset_sources(),
2637 +                                       (int __user *)arg);
2638 +       case WDIOC_KEEPALIVE:
2639 +               retu_modify_counter(RETU_WDT_MAX_TIMER);
2640 +               break;
2641 +       case WDIOC_SETTIMEOUT:
2642 +               if (get_user(new_margin, (int __user *)arg))
2643 +                       return -EFAULT;
2644 +               retu_modify_counter(new_margin);
2645 +               /* Fall through */
2646 +       case WDIOC_GETTIMEOUT:
2647 +               return put_user(period_val, (int __user *)arg);
2648 +       }
2649 +
2650 +       return 0;
2651 +}
2652 +
2653 +/* Start kicking retu watchdog until user space starts doing the kicking */
2654 +static int __init retu_wdt_ping(void)
2655 +{
2656 +
2657 +#ifdef CONFIG_WATCHDOG_NOWAYOUT
2658 +       retu_modify_counter(RETU_WDT_MAX_TIMER);
2659 +#else
2660 +       retu_wdt_set_ping_timer(1);
2661 +#endif
2662 +
2663 +       return 0;
2664 +}
2665 +late_initcall(retu_wdt_ping);
2666 +
2667 +static const struct file_operations retu_wdt_fops = {
2668 +       .owner = THIS_MODULE,
2669 +       .write = retu_wdt_write,
2670 +       .unlocked_ioctl = retu_wdt_ioctl,
2671 +       .open = retu_wdt_open,
2672 +       .release = retu_wdt_release,
2673 +};
2674 +
2675 +/*----------------------------------------------------------------------------*/
2676 +
2677 +static int __devinit retu_wdt_probe(struct device *dev)
2678 +{
2679 +       struct retu_wdt_dev *wdev;
2680 +       int ret;
2681 +
2682 +       wdev = kzalloc(sizeof(struct retu_wdt_dev), GFP_KERNEL);
2683 +       if (!wdev)
2684 +               return -ENOMEM;
2685 +
2686 +       wdev->users = 0;
2687 +
2688 +       ret = device_create_file(dev, &dev_attr_period);
2689 +       if (ret) {
2690 +               printk(KERN_ERR "retu_wdt_probe: Error creating "
2691 +                                       "sys device file: period\n");
2692 +               goto free1;
2693 +       }
2694 +
2695 +       ret = device_create_file(dev, &dev_attr_counter);
2696 +       if (ret) {
2697 +               printk(KERN_ERR "retu_wdt_probe: Error creating "
2698 +                                       "sys device file: counter\n");
2699 +               goto free2;
2700 +       }
2701 +
2702 +       dev_set_drvdata(dev, wdev);
2703 +       retu_wdt = wdev;
2704 +       wdev->retu_wdt_miscdev.parent = dev;
2705 +       wdev->retu_wdt_miscdev.minor = WATCHDOG_MINOR;
2706 +       wdev->retu_wdt_miscdev.name = "watchdog";
2707 +       wdev->retu_wdt_miscdev.fops = &retu_wdt_fops;
2708 +
2709 +       ret = misc_register(&(wdev->retu_wdt_miscdev));
2710 +       if (ret)
2711 +               goto free3;
2712 +
2713 +       setup_timer(&wdev->ping_timer, retu_wdt_set_ping_timer, 1);
2714 +
2715 +       /* Kick the watchdog for kernel booting to finish */
2716 +       retu_modify_counter(RETU_WDT_MAX_TIMER);
2717 +
2718 +       return 0;
2719 +
2720 +free3:
2721 +       device_remove_file(dev, &dev_attr_counter);
2722 +
2723 +free2:
2724 +       device_remove_file(dev, &dev_attr_period);
2725 +free1:
2726 +       kfree(wdev);
2727 +
2728 +       return ret;
2729 +}
2730 +
2731 +static int __devexit retu_wdt_remove(struct device *dev)
2732 +{
2733 +       struct retu_wdt_dev *wdev;
2734 +
2735 +       wdev = dev_get_drvdata(dev);
2736 +       misc_deregister(&(wdev->retu_wdt_miscdev));
2737 +       device_remove_file(dev, &dev_attr_period);
2738 +       device_remove_file(dev, &dev_attr_counter);
2739 +       kfree(wdev);
2740 +
2741 +       return 0;
2742 +}
2743 +
2744 +static void retu_wdt_device_release(struct device *dev)
2745 +{
2746 +       complete(&retu_wdt_completion);
2747 +}
2748 +
2749 +static struct platform_device retu_wdt_device = {
2750 +       .name = "retu-watchdog",
2751 +       .id = -1,
2752 +       .dev = {
2753 +               .release = retu_wdt_device_release,
2754 +       },
2755 +};
2756 +
2757 +static struct device_driver retu_wdt_driver = {
2758 +       .name = "retu-watchdog",
2759 +       .bus = &platform_bus_type,
2760 +       .probe = retu_wdt_probe,
2761 +       .remove = __devexit_p(retu_wdt_remove),
2762 +};
2763 +
2764 +static int __init retu_wdt_init(void)
2765 +{
2766 +       int ret;
2767 +
2768 +       init_completion(&retu_wdt_completion);
2769 +
2770 +       ret = driver_register(&retu_wdt_driver);
2771 +       if (ret)
2772 +               return ret;
2773 +
2774 +       ret = platform_device_register(&retu_wdt_device);
2775 +       if (ret)
2776 +               goto exit1;
2777 +
2778 +       /* passed as module parameter? */
2779 +       ret = retu_modify_counter(counter_param);
2780 +       if (ret == -EINVAL) {
2781 +               ret = retu_modify_counter(RETU_WDT_DEFAULT_TIMER);
2782 +               printk(KERN_INFO
2783 +                      "retu_wdt_init: Intializing to default value\n");
2784 +       }
2785 +
2786 +       printk(KERN_INFO "Retu watchdog driver initialized\n");
2787 +       return ret;
2788 +
2789 +exit1:
2790 +       driver_unregister(&retu_wdt_driver);
2791 +       wait_for_completion(&retu_wdt_completion);
2792 +
2793 +       return ret;
2794 +}
2795 +
2796 +static void __exit retu_wdt_exit(void)
2797 +{
2798 +       platform_device_unregister(&retu_wdt_device);
2799 +       driver_unregister(&retu_wdt_driver);
2800 +
2801 +       wait_for_completion(&retu_wdt_completion);
2802 +}
2803 +
2804 +module_init(retu_wdt_init);
2805 +module_exit(retu_wdt_exit);
2806 +module_param(counter_param, int, 0);
2807 +
2808 +MODULE_DESCRIPTION("Retu WatchDog");
2809 +MODULE_AUTHOR("Amit Kucheria");
2810 +MODULE_LICENSE("GPL");
2811 +
2812 --- /dev/null
2813 +++ linux-2.6.36-rc4/drivers/cbus/tahvo.c
2814 @@ -0,0 +1,443 @@
2815 +/**
2816 + * drivers/cbus/tahvo.c
2817 + *
2818 + * Support functions for Tahvo ASIC
2819 + *
2820 + * Copyright (C) 2004, 2005 Nokia Corporation
2821 + *
2822 + * Written by Juha Yrjölä <juha.yrjola@nokia.com>,
2823 + *           David Weinehall <david.weinehall@nokia.com>, and
2824 + *           Mikko Ylinen <mikko.k.ylinen@nokia.com>
2825 + *
2826 + * This file is subject to the terms and conditions of the GNU General
2827 + * Public License. See the file "COPYING" in the main directory of this
2828 + * archive for more details.
2829 + *
2830 + * This program is distributed in the hope that it will be useful,
2831 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2832 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2833 + * GNU General Public License for more details.
2834 + *
2835 + * You should have received a copy of the GNU General Public License
2836 + * along with this program; if not, write to the Free Software
2837 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
2838 + */
2839 +
2840 +#include <linux/module.h>
2841 +#include <linux/init.h>
2842 +
2843 +#include <linux/kernel.h>
2844 +#include <linux/errno.h>
2845 +#include <linux/device.h>
2846 +#include <linux/miscdevice.h>
2847 +#include <linux/poll.h>
2848 +#include <linux/fs.h>
2849 +#include <linux/irq.h>
2850 +#include <linux/interrupt.h>
2851 +#include <linux/platform_device.h>
2852 +#include <linux/gpio.h>
2853 +
2854 +#include <asm/uaccess.h>
2855 +#include <asm/mach-types.h>
2856 +
2857 +#include <plat/mux.h>
2858 +#include <plat/board.h>
2859 +
2860 +#include "cbus.h"
2861 +#include "tahvo.h"
2862 +
2863 +#define TAHVO_ID               0x02
2864 +#define PFX                    "tahvo: "
2865 +
2866 +static int tahvo_initialized;
2867 +static int tahvo_irq_pin;
2868 +static int tahvo_is_betty;
2869 +
2870 +static struct tasklet_struct tahvo_tasklet;
2871 +spinlock_t tahvo_lock = SPIN_LOCK_UNLOCKED;
2872 +
2873 +static struct completion device_release;
2874 +
2875 +struct tahvo_irq_handler_desc {
2876 +       int (*func)(unsigned long);
2877 +       unsigned long arg;
2878 +       char name[8];
2879 +};
2880 +
2881 +static struct tahvo_irq_handler_desc tahvo_irq_handlers[MAX_TAHVO_IRQ_HANDLERS];
2882 +
2883 +/**
2884 + * tahvo_read_reg - Read a value from a register in Tahvo
2885 + * @reg: the register to read from
2886 + *
2887 + * This function returns the contents of the specified register
2888 + */
2889 +int tahvo_read_reg(int reg)
2890 +{
2891 +       BUG_ON(!tahvo_initialized);
2892 +       return cbus_read_reg(cbus_host, TAHVO_ID, reg);
2893 +}
2894 +
2895 +/**
2896 + * tahvo_write_reg - Write a value to a register in Tahvo
2897 + * @reg: the register to write to
2898 + * @reg: the value to write to the register
2899 + *
2900 + * This function writes a value to the specified register
2901 + */
2902 +void tahvo_write_reg(int reg, u16 val)
2903 +{
2904 +       BUG_ON(!tahvo_initialized);
2905 +       cbus_write_reg(cbus_host, TAHVO_ID, reg, val);
2906 +}
2907 +
2908 +/**
2909 + * tahvo_set_clear_reg_bits - set and clear register bits atomically
2910 + * @reg: the register to write to
2911 + * @bits: the bits to set
2912 + *
2913 + * This function sets and clears the specified Tahvo register bits atomically
2914 + */
2915 +void tahvo_set_clear_reg_bits(int reg, u16 set, u16 clear)
2916 +{
2917 +       unsigned long flags;
2918 +       u16 w;
2919 +
2920 +       spin_lock_irqsave(&tahvo_lock, flags);
2921 +       w = tahvo_read_reg(reg);
2922 +       w &= ~clear;
2923 +       w |= set;
2924 +       tahvo_write_reg(reg, w);
2925 +       spin_unlock_irqrestore(&tahvo_lock, flags);
2926 +}
2927 +
2928 +/*
2929 + * Disable given TAHVO interrupt
2930 + */
2931 +void tahvo_disable_irq(int id)
2932 +{
2933 +       unsigned long flags;
2934 +       u16 mask;
2935 +
2936 +       spin_lock_irqsave(&tahvo_lock, flags);
2937 +       mask = tahvo_read_reg(TAHVO_REG_IMR);
2938 +       mask |= 1 << id;
2939 +       tahvo_write_reg(TAHVO_REG_IMR, mask);
2940 +       spin_unlock_irqrestore(&tahvo_lock, flags);
2941 +}
2942 +
2943 +/*
2944 + * Enable given TAHVO interrupt
2945 + */
2946 +void tahvo_enable_irq(int id)
2947 +{
2948 +       unsigned long flags;
2949 +       u16 mask;
2950 +
2951 +       spin_lock_irqsave(&tahvo_lock, flags);
2952 +       mask = tahvo_read_reg(TAHVO_REG_IMR);
2953 +       mask &= ~(1 << id);
2954 +       tahvo_write_reg(TAHVO_REG_IMR, mask);
2955 +       spin_unlock_irqrestore(&tahvo_lock, flags);
2956 +}
2957 +
2958 +/*
2959 + * Acknowledge given TAHVO interrupt
2960 + */
2961 +void tahvo_ack_irq(int id)
2962 +{
2963 +       tahvo_write_reg(TAHVO_REG_IDR, 1 << id);
2964 +}
2965 +
2966 +static int tahvo_7bit_backlight;
2967 +
2968 +int tahvo_get_backlight_level(void)
2969 +{
2970 +       int mask;
2971 +
2972 +       if (tahvo_7bit_backlight)
2973 +               mask = 0x7f;
2974 +       else
2975 +               mask = 0x0f;
2976 +       return tahvo_read_reg(TAHVO_REG_LEDPWMR) & mask;
2977 +}
2978 +
2979 +int tahvo_get_max_backlight_level(void)
2980 +{
2981 +       if (tahvo_7bit_backlight)
2982 +               return 0x7f;
2983 +       else
2984 +               return 0x0f;
2985 +}
2986 +
2987 +void tahvo_set_backlight_level(int level)
2988 +{
2989 +       int max_level;
2990 +
2991 +       max_level = tahvo_get_max_backlight_level();
2992 +       if (level > max_level)
2993 +               level = max_level;
2994 +       tahvo_write_reg(TAHVO_REG_LEDPWMR, level);
2995 +}
2996 +
2997 +/*
2998 + * TAHVO interrupt handler. Only schedules the tasklet.
2999 + */
3000 +static irqreturn_t tahvo_irq_handler(int irq, void *dev_id)
3001 +{
3002 +       tasklet_schedule(&tahvo_tasklet);
3003 +       return IRQ_HANDLED;
3004 +}
3005 +
3006 +/*
3007 + * Tasklet handler
3008 + */
3009 +static void tahvo_tasklet_handler(unsigned long data)
3010 +{
3011 +       struct tahvo_irq_handler_desc *hnd;
3012 +       u16 id;
3013 +       u16 im;
3014 +       int i;
3015 +
3016 +       for (;;) {
3017 +               id = tahvo_read_reg(TAHVO_REG_IDR);
3018 +               im = ~tahvo_read_reg(TAHVO_REG_IMR);
3019 +               id &= im;
3020 +
3021 +               if (!id)
3022 +                       break;
3023 +
3024 +               for (i = 0; id != 0; i++, id >>= 1) {
3025 +                       if (!(id & 1))
3026 +                               continue;
3027 +                       hnd = &tahvo_irq_handlers[i];
3028 +                       if (hnd->func == NULL) {
3029 +                               /* Spurious tahvo interrupt - just ack it */
3030 +                               printk(KERN_INFO "Spurious Tahvo interrupt "
3031 +                                                "(id %d)\n", i);
3032 +                               tahvo_disable_irq(i);
3033 +                               tahvo_ack_irq(i);
3034 +                               continue;
3035 +                       }
3036 +                       hnd->func(hnd->arg);
3037 +                       /*
3038 +                        * Don't acknowledge the interrupt here
3039 +                        * It must be done explicitly
3040 +                        */
3041 +               }
3042 +       }
3043 +}
3044 +
3045 +/*
3046 + * Register the handler for a given TAHVO interrupt source.
3047 + */
3048 +int tahvo_request_irq(int id, void *irq_handler, unsigned long arg, char *name)
3049 +{
3050 +       struct tahvo_irq_handler_desc *hnd;
3051 +
3052 +       if (irq_handler == NULL || id >= MAX_TAHVO_IRQ_HANDLERS ||
3053 +           name == NULL) {
3054 +               printk(KERN_ERR PFX "Invalid arguments to %s\n",
3055 +                      __FUNCTION__);
3056 +               return -EINVAL;
3057 +       }
3058 +       hnd = &tahvo_irq_handlers[id];
3059 +       if (hnd->func != NULL) {
3060 +               printk(KERN_ERR PFX "IRQ %d already reserved\n", id);
3061 +               return -EBUSY;
3062 +       }
3063 +       printk(KERN_INFO PFX "Registering interrupt %d for device %s\n",
3064 +              id, name);
3065 +       hnd->func = irq_handler;
3066 +       hnd->arg = arg;
3067 +       strlcpy(hnd->name, name, sizeof(hnd->name));
3068 +
3069 +       tahvo_ack_irq(id);
3070 +       tahvo_enable_irq(id);
3071 +
3072 +       return 0;
3073 +}
3074 +
3075 +/*
3076 + * Unregister the handler for a given TAHVO interrupt source.
3077 + */
3078 +void tahvo_free_irq(int id)
3079 +{
3080 +       struct tahvo_irq_handler_desc *hnd;
3081 +
3082 +       if (id >= MAX_TAHVO_IRQ_HANDLERS) {
3083 +               printk(KERN_ERR PFX "Invalid argument to %s\n",
3084 +                      __FUNCTION__);
3085 +               return;
3086 +       }
3087 +       hnd = &tahvo_irq_handlers[id];
3088 +       if (hnd->func == NULL) {
3089 +               printk(KERN_ERR PFX "IRQ %d already freed\n", id);
3090 +               return;
3091 +       }
3092 +
3093 +       tahvo_disable_irq(id);
3094 +       hnd->func = NULL;
3095 +}
3096 +
3097 +/**
3098 + * tahvo_probe - Probe for Tahvo ASIC
3099 + * @dev: the Tahvo device
3100 + *
3101 + * Probe for the Tahvo ASIC and allocate memory
3102 + * for its device-struct if found
3103 + */
3104 +static int __devinit tahvo_probe(struct device *dev)
3105 +{
3106 +       int rev, id, ret;
3107 +
3108 +       /* Prepare tasklet */
3109 +       tasklet_init(&tahvo_tasklet, tahvo_tasklet_handler, 0);
3110 +
3111 +       tahvo_initialized = 1;
3112 +
3113 +       rev = tahvo_read_reg(TAHVO_REG_ASICR);
3114 +
3115 +       id = (rev >> 8) & 0xff;
3116 +       if (id == 0x03) {
3117 +               if ((rev & 0xff) >= 0x50)
3118 +                       tahvo_7bit_backlight = 1;
3119 +       } else if (id == 0x0b) {
3120 +               tahvo_is_betty = 1;
3121 +               tahvo_7bit_backlight = 1;
3122 +       } else {
3123 +               printk(KERN_ERR "Tahvo/Betty chip not found");
3124 +               return -ENODEV;
3125 +       }
3126 +
3127 +       printk(KERN_INFO "%s v%d.%d found\n", tahvo_is_betty ? "Betty" : "Tahvo",
3128 +              (rev >> 4) & 0x0f, rev & 0x0f);
3129 +
3130 +       /* REVISIT: Pass these from board-*.c files in platform_data */
3131 +       if (machine_is_nokia770()) {
3132 +               tahvo_irq_pin = 40;
3133 +       } else if (machine_is_nokia_n800() || machine_is_nokia_n810() ||
3134 +                       machine_is_nokia_n810_wimax()) {
3135 +               tahvo_irq_pin = 111;
3136 +       } else {
3137 +               printk(KERN_ERR "cbus: Unsupported board for tahvo\n");
3138 +               return -ENODEV;
3139 +       }
3140 +
3141 +       if ((ret = gpio_request(tahvo_irq_pin, "TAHVO irq")) < 0) {
3142 +               printk(KERN_ERR PFX "Unable to reserve IRQ GPIO\n");
3143 +               return ret;
3144 +       }
3145 +
3146 +       /* Set the pin as input */
3147 +       gpio_direction_input(tahvo_irq_pin);
3148 +
3149 +       /* Rising edge triggers the IRQ */
3150 +       set_irq_type(gpio_to_irq(tahvo_irq_pin), IRQ_TYPE_EDGE_RISING);
3151 +
3152 +       /* Mask all TAHVO interrupts */
3153 +       tahvo_write_reg(TAHVO_REG_IMR, 0xffff);
3154 +
3155 +       ret = request_irq(gpio_to_irq(tahvo_irq_pin), tahvo_irq_handler, 0,
3156 +                         "tahvo", 0);
3157 +       if (ret < 0) {
3158 +               printk(KERN_ERR PFX "Unable to register IRQ handler\n");
3159 +               gpio_free(tahvo_irq_pin);
3160 +               return ret;
3161 +       }
3162 +#ifdef CONFIG_CBUS_TAHVO_USER
3163 +       /* Initialize user-space interface */
3164 +       if (tahvo_user_init() < 0) {
3165 +               printk(KERN_ERR "Unable to initialize driver\n");
3166 +               free_irq(gpio_to_irq(tahvo_irq_pin), 0);
3167 +               gpio_free(tahvo_irq_pin);
3168 +               return ret;
3169 +       }
3170 +#endif
3171 +       return 0;
3172 +}
3173 +
3174 +static int tahvo_remove(struct device *dev)
3175 +{
3176 +#ifdef CONFIG_CBUS_TAHVO_USER
3177 +       tahvo_user_cleanup();
3178 +#endif
3179 +       /* Mask all TAHVO interrupts */
3180 +       tahvo_write_reg(TAHVO_REG_IMR, 0xffff);
3181 +       free_irq(gpio_to_irq(tahvo_irq_pin), 0);
3182 +       gpio_free(tahvo_irq_pin);
3183 +       tasklet_kill(&tahvo_tasklet);
3184 +
3185 +       return 0;
3186 +}
3187 +
3188 +static void tahvo_device_release(struct device *dev)
3189 +{
3190 +       complete(&device_release);
3191 +}
3192 +
3193 +static struct device_driver tahvo_driver = {
3194 +       .name           = "tahvo",
3195 +       .bus            = &platform_bus_type,
3196 +       .probe          = tahvo_probe,
3197 +       .remove         = tahvo_remove,
3198 +};
3199 +
3200 +static struct platform_device tahvo_device = {
3201 +       .name           = "tahvo",
3202 +       .id             = -1,
3203 +       .dev = {
3204 +               .release = tahvo_device_release,
3205 +       }
3206 +};
3207 +
3208 +/**
3209 + * tahvo_init - initialise Tahvo driver
3210 + *
3211 + * Initialise the Tahvo driver and return 0 if everything worked ok
3212 + */
3213 +static int __init tahvo_init(void)
3214 +{
3215 +       int ret = 0;
3216 +
3217 +       printk(KERN_INFO "Tahvo/Betty driver initialising\n");
3218 +
3219 +       init_completion(&device_release);
3220 +
3221 +       if ((ret = driver_register(&tahvo_driver)) < 0)
3222 +               return ret;
3223 +
3224 +       if ((ret = platform_device_register(&tahvo_device)) < 0) {
3225 +               driver_unregister(&tahvo_driver);
3226 +               return ret;
3227 +       }
3228 +       return 0;
3229 +}
3230 +
3231 +/*
3232 + * Cleanup
3233 + */
3234 +static void __exit tahvo_exit(void)
3235 +{
3236 +       platform_device_unregister(&tahvo_device);
3237 +       driver_unregister(&tahvo_driver);
3238 +       wait_for_completion(&device_release);
3239 +}
3240 +
3241 +EXPORT_SYMBOL(tahvo_request_irq);
3242 +EXPORT_SYMBOL(tahvo_free_irq);
3243 +EXPORT_SYMBOL(tahvo_enable_irq);
3244 +EXPORT_SYMBOL(tahvo_disable_irq);
3245 +EXPORT_SYMBOL(tahvo_ack_irq);
3246 +EXPORT_SYMBOL(tahvo_read_reg);
3247 +EXPORT_SYMBOL(tahvo_write_reg);
3248 +EXPORT_SYMBOL(tahvo_get_backlight_level);
3249 +EXPORT_SYMBOL(tahvo_get_max_backlight_level);
3250 +EXPORT_SYMBOL(tahvo_set_backlight_level);
3251 +
3252 +subsys_initcall(tahvo_init);
3253 +module_exit(tahvo_exit);
3254 +
3255 +MODULE_DESCRIPTION("Tahvo ASIC control");
3256 +MODULE_LICENSE("GPL");
3257 +MODULE_AUTHOR("Juha Yrjölä, David Weinehall, and Mikko Ylinen");
3258 --- /dev/null
3259 +++ linux-2.6.36-rc4/drivers/cbus/tahvo.h
3260 @@ -0,0 +1,61 @@
3261 +/*
3262 + * drivers/cbus/tahvo.h
3263 + *
3264 + * Copyright (C) 2004, 2005 Nokia Corporation
3265 + *
3266 + * Written by Juha Yrjölä <juha.yrjola@nokia.com> and
3267 + *           David Weinehall <david.weinehall@nokia.com>
3268 + *
3269 + * This file is subject to the terms and conditions of the GNU General
3270 + * Public License. See the file "COPYING" in the main directory of this
3271 + * archive for more details.
3272 + *
3273 + * This program is distributed in the hope that it will be useful,
3274 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3275 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3276 + * GNU General Public License for more details.
3277 +
3278 + * You should have received a copy of the GNU General Public License
3279 + * along with this program; if not, write to the Free Software
3280 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
3281 + */
3282 +
3283 +#ifndef __DRIVERS_CBUS_TAHVO_H
3284 +#define __DRIVERS_CBUS_TAHVO_H
3285 +
3286 +#include <linux/types.h>
3287 +
3288 +/* Registers */
3289 +#define TAHVO_REG_ASICR                0x00    /* ASIC ID & revision */
3290 +#define TAHVO_REG_IDR          0x01    /* Interrupt ID */
3291 +#define TAHVO_REG_IDSR         0x02    /* Interrupt status */
3292 +#define TAHVO_REG_IMR          0x03    /* Interrupt mask */
3293 +#define TAHVO_REG_LEDPWMR      0x05    /* LED PWM */
3294 +#define TAHVO_REG_USBR         0x06    /* USB control */
3295 +#define TAHVO_REG_MAX          0x0d
3296 +
3297 +/* Interrupt sources */
3298 +#define TAHVO_INT_VBUSON       0
3299 +
3300 +#define MAX_TAHVO_IRQ_HANDLERS 8
3301 +
3302 +int tahvo_read_reg(int reg);
3303 +void tahvo_write_reg(int reg, u16 val);
3304 +void tahvo_set_clear_reg_bits(int reg, u16 set, u16 clear);
3305 +int tahvo_request_irq(int id, void *irq_handler, unsigned long arg, char *name);
3306 +void tahvo_free_irq(int id);
3307 +void tahvo_enable_irq(int id);
3308 +void tahvo_disable_irq(int id);
3309 +void tahvo_ack_irq(int id);
3310 +int tahvo_get_backlight_level(void);
3311 +int tahvo_get_max_backlight_level(void);
3312 +void tahvo_set_backlight_level(int level);
3313 +
3314 +#ifdef CONFIG_CBUS_TAHVO_USER
3315 +int tahvo_user_init(void);
3316 +void tahvo_user_cleanup(void);
3317 +#endif
3318 +
3319 +extern spinlock_t tahvo_lock;
3320 +
3321 +#endif /* __DRIVERS_CBUS_TAHVO_H */
3322 --- /dev/null
3323 +++ linux-2.6.36-rc4/drivers/cbus/tahvo-usb.c
3324 @@ -0,0 +1,788 @@
3325 +/**
3326 + * drivers/cbus/tahvo-usb.c
3327 + *
3328 + * Tahvo USB transeiver
3329 + *
3330 + * Copyright (C) 2005-2006 Nokia Corporation
3331 + *
3332 + * Parts copied from drivers/i2c/chips/isp1301_omap.c
3333 + * Copyright (C) 2004 Texas Instruments
3334 + * Copyright (C) 2004 David Brownell
3335 + *
3336 + * Written by Juha Yrjölä <juha.yrjola@nokia.com>,
3337 + *           Tony Lindgren <tony@atomide.com>, and
3338 + *           Timo Teräs <timo.teras@nokia.com>
3339 + *
3340 + * This file is subject to the terms and conditions of the GNU General
3341 + * Public License. See the file "COPYING" in the main directory of this
3342 + * archive for more details.
3343 + *
3344 + * This program is distributed in the hope that it will be useful,
3345 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3346 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3347 + * GNU General Public License for more details.
3348 + *
3349 + * You should have received a copy of the GNU General Public License
3350 + * along with this program; if not, write to the Free Software
3351 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
3352 + */
3353 +
3354 +#include <linux/kernel.h>
3355 +#include <linux/module.h>
3356 +#include <linux/init.h>
3357 +#include <linux/slab.h>
3358 +#include <linux/io.h>
3359 +#include <linux/interrupt.h>
3360 +#include <linux/platform_device.h>
3361 +#include <linux/usb/ch9.h>
3362 +#include <linux/usb/gadget.h>
3363 +#include <linux/usb.h>
3364 +#include <linux/usb/otg.h>
3365 +#include <linux/i2c.h>
3366 +#include <linux/workqueue.h>
3367 +#include <linux/kobject.h>
3368 +#include <linux/clk.h>
3369 +#include <linux/mutex.h>
3370 +
3371 +#include <asm/irq.h>
3372 +#include <plat/usb.h>
3373 +
3374 +#include "cbus.h"
3375 +#include "tahvo.h"
3376 +
3377 +#define DRIVER_NAME     "tahvo-usb"
3378 +
3379 +#define USBR_SLAVE_CONTROL     (1 << 8)
3380 +#define USBR_VPPVIO_SW         (1 << 7)
3381 +#define USBR_SPEED             (1 << 6)
3382 +#define USBR_REGOUT            (1 << 5)
3383 +#define USBR_MASTER_SW2                (1 << 4)
3384 +#define USBR_MASTER_SW1                (1 << 3)
3385 +#define USBR_SLAVE_SW          (1 << 2)
3386 +#define USBR_NSUSPEND          (1 << 1)
3387 +#define USBR_SEMODE            (1 << 0)
3388 +
3389 +/* bits in OTG_CTRL */
3390 +
3391 +/* Bits that are controlled by OMAP OTG and are read-only */
3392 +#define OTG_CTRL_OMAP_MASK     (OTG_PULLDOWN|OTG_PULLUP|OTG_DRV_VBUS|\
3393 +                               OTG_PD_VBUS|OTG_PU_VBUS|OTG_PU_ID)
3394 +/* Bits that are controlled by transceiver */
3395 +#define OTG_CTRL_XCVR_MASK     (OTG_ASESSVLD|OTG_BSESSEND|\
3396 +                               OTG_BSESSVLD|OTG_VBUSVLD|OTG_ID)
3397 +/* Bits that are controlled by system */
3398 +#define OTG_CTRL_SYS_MASK      (OTG_A_BUSREQ|OTG_A_SETB_HNPEN|OTG_B_BUSREQ|\
3399 +                               OTG_B_HNPEN|OTG_BUSDROP)
3400 +
3401 +#if defined(CONFIG_USB_OHCI_HCD) && !defined(CONFIG_USB_OTG)
3402 +#error tahvo-otg.c does not work with OCHI yet!
3403 +#endif
3404 +
3405 +#define TAHVO_MODE_HOST                0
3406 +#define TAHVO_MODE_PERIPHERAL  1
3407 +
3408 +#ifdef CONFIG_USB_OTG
3409 +#define TAHVO_MODE(tu)         (tu)->tahvo_mode
3410 +#elif defined(CONFIG_USB_GADGET_OMAP)
3411 +#define TAHVO_MODE(tu)         TAHVO_MODE_PERIPHERAL
3412 +#else
3413 +#define TAHVO_MODE(tu)         TAHVO_MODE_HOST
3414 +#endif
3415 +
3416 +struct tahvo_usb {
3417 +       struct platform_device *pt_dev;
3418 +       struct otg_transceiver otg;
3419 +       int vbus_state;
3420 +       struct work_struct irq_work;
3421 +       struct mutex serialize;
3422 +#ifdef CONFIG_USB_OTG
3423 +       int tahvo_mode;
3424 +#endif
3425 +};
3426 +static struct platform_device tahvo_usb_device;
3427 +
3428 +/*
3429 + * ---------------------------------------------------------------------------
3430 + * OTG related functions
3431 + *
3432 + * These shoud be separated into omap-otg.c driver module, as they are used
3433 + * by various transceivers. These functions are needed in the UDC-only case
3434 + * as well. These functions are copied from GPL isp1301_omap.c
3435 + * ---------------------------------------------------------------------------
3436 + */
3437 +static struct platform_device *tahvo_otg_dev;
3438 +
3439 +static irqreturn_t omap_otg_irq(int irq, void *arg)
3440 +{
3441 +       struct platform_device *otg_dev = arg;
3442 +       struct tahvo_usb *tu = platform_get_drvdata(otg_dev);
3443 +       u16 otg_irq;
3444 +
3445 +       otg_irq = omap_readw(OTG_IRQ_SRC);
3446 +       if (otg_irq & OPRT_CHG) {
3447 +               omap_writew(OPRT_CHG, OTG_IRQ_SRC);
3448 +       } else if (otg_irq & B_SRP_TMROUT) {
3449 +               omap_writew(B_SRP_TMROUT, OTG_IRQ_SRC);
3450 +       } else if (otg_irq & B_HNP_FAIL) {
3451 +               omap_writew(B_HNP_FAIL, OTG_IRQ_SRC);
3452 +       } else if (otg_irq & A_SRP_DETECT) {
3453 +               omap_writew(A_SRP_DETECT, OTG_IRQ_SRC);
3454 +       } else if (otg_irq & A_REQ_TMROUT) {
3455 +               omap_writew(A_REQ_TMROUT, OTG_IRQ_SRC);
3456 +       } else if (otg_irq & A_VBUS_ERR) {
3457 +               omap_writew(A_VBUS_ERR, OTG_IRQ_SRC);
3458 +       } else if (otg_irq & DRIVER_SWITCH) {
3459 +               if ((!(omap_readl(OTG_CTRL) & OTG_DRIVER_SEL)) &&
3460 +                  tu->otg.host && tu->otg.state == OTG_STATE_A_HOST) {
3461 +                       /* role is host */
3462 +                       usb_bus_start_enum(tu->otg.host,
3463 +                                          tu->otg.host->otg_port);
3464 +               }
3465 +               omap_writew(DRIVER_SWITCH, OTG_IRQ_SRC);
3466 +       } else
3467 +               return IRQ_NONE;
3468 +
3469 +       return IRQ_HANDLED;
3470 +
3471 +}
3472 +
3473 +static int tahvo_omap_otg_init(void)
3474 +{
3475 +       u32 l;
3476 +
3477 +#ifdef CONFIG_USB_OTG
3478 +       if (!tahvo_otg_dev) {
3479 +               printk("tahvo-usb: no tahvo_otg_dev\n");
3480 +               return -ENODEV;
3481 +       }
3482 +#endif
3483 +
3484 +       l = omap_readl(OTG_SYSCON_1);
3485 +       l &= ~OTG_IDLE_EN;
3486 +       omap_writel(l, OTG_SYSCON_1);
3487 +       udelay(100);
3488 +
3489 +       /* some of these values are board-specific... */
3490 +       l = omap_readl(OTG_SYSCON_2);
3491 +       l |= OTG_EN
3492 +               /* for B-device: */
3493 +               | SRP_GPDATA            /* 9msec Bdev D+ pulse */
3494 +               | SRP_GPDVBUS           /* discharge after VBUS pulse */
3495 +               // | (3 << 24)          /* 2msec VBUS pulse */
3496 +               /* for A-device: */
3497 +               | (0 << 20)             /* 200ms nominal A_WAIT_VRISE timer */
3498 +               | SRP_DPW               /* detect 167+ns SRP pulses */
3499 +               | SRP_DATA | SRP_VBUS;  /* accept both kinds of SRP pulse */
3500 +       omap_writel(l, OTG_SYSCON_2);
3501 +
3502 +       omap_writew(DRIVER_SWITCH | OPRT_CHG
3503 +                       | B_SRP_TMROUT | B_HNP_FAIL
3504 +                                 | A_VBUS_ERR | A_SRP_DETECT | A_REQ_TMROUT,
3505 +                                       OTG_IRQ_EN);
3506 +       l = omap_readl(OTG_SYSCON_2);
3507 +       l |= OTG_EN;
3508 +       omap_writel(l, OTG_SYSCON_2);
3509 +
3510 +       return 0;
3511 +}
3512 +
3513 +static int omap_otg_probe(struct platform_device *pdev)
3514 +{
3515 +       int ret, err;
3516 +
3517 +       tahvo_otg_dev = pdev;
3518 +       ret = tahvo_omap_otg_init();
3519 +       if (ret != 0) {
3520 +               printk(KERN_ERR "tahvo-usb: omap_otg_init failed\n");
3521 +               return ret;
3522 +       }
3523 +
3524 +       err = request_irq(tahvo_otg_dev->resource[1].start,
3525 +                         omap_otg_irq, IRQF_DISABLED, DRIVER_NAME,
3526 +                         &tahvo_usb_device);
3527 +
3528 +       return err;
3529 +}
3530 +
3531 +static int omap_otg_remove(struct platform_device *pdev)
3532 +{
3533 +       free_irq(tahvo_otg_dev->resource[1].start, &tahvo_usb_device);
3534 +       tahvo_otg_dev = NULL;
3535 +
3536 +       return 0;
3537 +}
3538 +
3539 +static struct platform_driver omap_otg_driver = {
3540 +       .probe          = omap_otg_probe,
3541 +       .remove         = omap_otg_remove,
3542 +       .driver         = {
3543 +               .name           = "omap_otg",
3544 +       }
3545 +};
3546 +
3547 +/*
3548 + * ---------------------------------------------------------------------------
3549 + * Tahvo related functions
3550 + * These are Nokia proprietary code, except for the OTG register settings,
3551 + * which are copied from isp1301.c
3552 + * ---------------------------------------------------------------------------
3553 + */
3554 +static ssize_t vbus_state_show(struct device *device,
3555 +                              struct device_attribute *attr, char *buf)
3556 +{
3557 +       struct platform_device *pdev = to_platform_device(device);
3558 +       struct tahvo_usb *tu = platform_get_drvdata(pdev);
3559 +       return sprintf(buf, "%d\n", tu->vbus_state);
3560 +}
3561 +static DEVICE_ATTR(vbus_state, 0444, vbus_state_show, NULL);
3562 +
3563 +int vbus_active = 0;
3564 +
3565 +#if 0
3566 +
3567 +static int host_suspend(struct tahvo_usb *tu)
3568 +{
3569 +       struct device   *dev;
3570 +
3571 +       if (!tu->otg.host)
3572 +               return -ENODEV;
3573 +
3574 +       /* Currently ASSUMES only the OTG port matters;
3575 +        * other ports could be active...
3576 +        */
3577 +       dev = tu->otg.host->controller;
3578 +       return dev->driver->suspend(dev, PMSG_SUSPEND);
3579 +}
3580 +
3581 +static int host_resume(struct tahvo_usb *tu)
3582 +{
3583 +       struct device   *dev;
3584 +
3585 +       if (!tu->otg.host)
3586 +               return -ENODEV;
3587 +
3588 +       dev = tu->otg.host->controller;
3589 +       return dev->driver->resume(dev);
3590 +}
3591 +
3592 +#else
3593 +
3594 +static int host_suspend(struct tahvo_usb *tu)
3595 +{
3596 +       return 0;
3597 +}
3598 +
3599 +static int host_resume(struct tahvo_usb *tu)
3600 +{
3601 +       return 0;
3602 +}
3603 +
3604 +#endif
3605 +
3606 +static void check_vbus_state(struct tahvo_usb *tu)
3607 +{
3608 +       int reg, prev_state;
3609 +
3610 +       reg = tahvo_read_reg(TAHVO_REG_IDSR);
3611 +       if (reg & 0x01) {
3612 +               u32 l;
3613 +
3614 +               vbus_active = 1;
3615 +               switch (tu->otg.state) {
3616 +               case OTG_STATE_B_IDLE:
3617 +                       /* Enable the gadget driver */
3618 +                       if (tu->otg.gadget)
3619 +                               usb_gadget_vbus_connect(tu->otg.gadget);
3620 +                       /* Set B-session valid and not B-sessio ended to indicate
3621 +                        * Vbus to be ok. */
3622 +                       l = omap_readl(OTG_CTRL);
3623 +                       l &= ~OTG_BSESSEND;
3624 +                       l |= OTG_BSESSVLD;
3625 +                       omap_writel(l, OTG_CTRL);
3626 +
3627 +                       tu->otg.state = OTG_STATE_B_PERIPHERAL;
3628 +                       break;
3629 +               case OTG_STATE_A_IDLE:
3630 +                       /* Session is now valid assuming the USB hub is driving Vbus */
3631 +                       tu->otg.state = OTG_STATE_A_HOST;
3632 +                       host_resume(tu);
3633 +                       break;
3634 +               default:
3635 +                       break;
3636 +               }
3637 +               printk("USB cable connected\n");
3638 +       } else {
3639 +               switch (tu->otg.state) {
3640 +               case OTG_STATE_B_PERIPHERAL:
3641 +                       if (tu->otg.gadget)
3642 +                               usb_gadget_vbus_disconnect(tu->otg.gadget);
3643 +                       tu->otg.state = OTG_STATE_B_IDLE;
3644 +                       break;
3645 +               case OTG_STATE_A_HOST:
3646 +                       tu->otg.state = OTG_STATE_A_IDLE;
3647 +                       break;
3648 +               default:
3649 +                       break;
3650 +               }
3651 +               printk("USB cable disconnected\n");
3652 +               vbus_active = 0;
3653 +       }
3654 +
3655 +       prev_state = tu->vbus_state;
3656 +       tu->vbus_state = reg & 0x01;
3657 +       if (prev_state != tu->vbus_state)
3658 +               sysfs_notify(&tu->pt_dev->dev.kobj, NULL, "vbus_state");
3659 +}
3660 +
3661 +static void tahvo_usb_become_host(struct tahvo_usb *tu)
3662 +{
3663 +       u32 l;
3664 +
3665 +       /* Clear system and transceiver controlled bits
3666 +        * also mark the A-session is always valid */
3667 +       tahvo_omap_otg_init();
3668 +
3669 +       l = omap_readl(OTG_CTRL);
3670 +       l &= ~(OTG_CTRL_XCVR_MASK | OTG_CTRL_SYS_MASK);
3671 +       l |= OTG_ASESSVLD;
3672 +       omap_writel(l, OTG_CTRL);
3673 +
3674 +       /* Power up the transceiver in USB host mode */
3675 +       tahvo_write_reg(TAHVO_REG_USBR, USBR_REGOUT | USBR_NSUSPEND |
3676 +                       USBR_MASTER_SW2 | USBR_MASTER_SW1);
3677 +       tu->otg.state = OTG_STATE_A_IDLE;
3678 +
3679 +       check_vbus_state(tu);
3680 +}
3681 +
3682 +static void tahvo_usb_stop_host(struct tahvo_usb *tu)
3683 +{
3684 +       host_suspend(tu);
3685 +       tu->otg.state = OTG_STATE_A_IDLE;
3686 +}
3687 +
3688 +static void tahvo_usb_become_peripheral(struct tahvo_usb *tu)
3689 +{
3690 +       u32 l;
3691 +
3692 +       /* Clear system and transceiver controlled bits
3693 +        * and enable ID to mark peripheral mode and
3694 +        * BSESSEND to mark no Vbus */
3695 +       tahvo_omap_otg_init();
3696 +       l = omap_readl(OTG_CTRL);
3697 +       l &= ~(OTG_CTRL_XCVR_MASK | OTG_CTRL_SYS_MASK | OTG_BSESSVLD);
3698 +       l |= OTG_ID | OTG_BSESSEND;
3699 +       omap_writel(l, OTG_CTRL);
3700 +
3701 +       /* Power up transceiver and set it in USB perhiperal mode */
3702 +       tahvo_write_reg(TAHVO_REG_USBR, USBR_SLAVE_CONTROL | USBR_REGOUT | USBR_NSUSPEND | USBR_SLAVE_SW);
3703 +       tu->otg.state = OTG_STATE_B_IDLE;
3704 +
3705 +       check_vbus_state(tu);
3706 +}
3707 +
3708 +static void tahvo_usb_stop_peripheral(struct tahvo_usb *tu)
3709 +{
3710 +       u32 l;
3711 +
3712 +       l = omap_readl(OTG_CTRL);
3713 +       l &= ~OTG_BSESSVLD;
3714 +       l |= OTG_BSESSEND;
3715 +       omap_writel(l, OTG_CTRL);
3716 +
3717 +       if (tu->otg.gadget)
3718 +               usb_gadget_vbus_disconnect(tu->otg.gadget);
3719 +       tu->otg.state = OTG_STATE_B_IDLE;
3720 +
3721 +}
3722 +
3723 +static void tahvo_usb_power_off(struct tahvo_usb *tu)
3724 +{
3725 +       u32 l;
3726 +       int id;
3727 +
3728 +       /* Disable gadget controller if any */
3729 +       if (tu->otg.gadget)
3730 +               usb_gadget_vbus_disconnect(tu->otg.gadget);
3731 +
3732 +       host_suspend(tu);
3733 +
3734 +       /* Disable OTG and interrupts */
3735 +       if (TAHVO_MODE(tu) == TAHVO_MODE_PERIPHERAL)
3736 +               id = OTG_ID;
3737 +       else
3738 +               id = 0;
3739 +       l = omap_readl(OTG_CTRL);
3740 +       l &= ~(OTG_CTRL_XCVR_MASK | OTG_CTRL_SYS_MASK | OTG_BSESSVLD);
3741 +       l |= id | OTG_BSESSEND;
3742 +       omap_writel(l, OTG_CTRL);
3743 +       omap_writew(0, OTG_IRQ_EN);
3744 +
3745 +       l = omap_readl(OTG_SYSCON_2);
3746 +       l &= ~OTG_EN;
3747 +       omap_writel(l, OTG_SYSCON_2);
3748 +
3749 +       l = omap_readl(OTG_SYSCON_1);
3750 +       l |= OTG_IDLE_EN;
3751 +       omap_writel(l, OTG_SYSCON_1);
3752 +
3753 +       /* Power off transceiver */
3754 +       tahvo_write_reg(TAHVO_REG_USBR, 0);
3755 +       tu->otg.state = OTG_STATE_UNDEFINED;
3756 +}
3757 +
3758 +
3759 +static int tahvo_usb_set_power(struct otg_transceiver *dev, unsigned mA)
3760 +{
3761 +       struct tahvo_usb *tu = container_of(dev, struct tahvo_usb, otg);
3762 +
3763 +       dev_dbg(&tu->pt_dev->dev, "set_power %d mA\n", mA);
3764 +
3765 +       if (dev->state == OTG_STATE_B_PERIPHERAL) {
3766 +               /* REVISIT: Can Tahvo charge battery from VBUS? */
3767 +       }
3768 +       return 0;
3769 +}
3770 +
3771 +static int tahvo_usb_set_suspend(struct otg_transceiver *dev, int suspend)
3772 +{
3773 +       struct tahvo_usb *tu = container_of(dev, struct tahvo_usb, otg);
3774 +       u16 w;
3775 +
3776 +       dev_dbg(&tu->pt_dev->dev, "set_suspend\n");
3777 +
3778 +       w = tahvo_read_reg(TAHVO_REG_USBR);
3779 +       if (suspend)
3780 +               w &= ~USBR_NSUSPEND;
3781 +       else
3782 +               w |= USBR_NSUSPEND;
3783 +       tahvo_write_reg(TAHVO_REG_USBR, w);
3784 +
3785 +       return 0;
3786 +}
3787 +
3788 +static int tahvo_usb_start_srp(struct otg_transceiver *dev)
3789 +{
3790 +       struct tahvo_usb *tu = container_of(dev, struct tahvo_usb, otg);
3791 +       u32 otg_ctrl;
3792 +
3793 +       dev_dbg(&tu->pt_dev->dev, "start_srp\n");
3794 +
3795 +       if (!dev || tu->otg.state != OTG_STATE_B_IDLE)
3796 +               return -ENODEV;
3797 +
3798 +       otg_ctrl = omap_readl(OTG_CTRL);
3799 +       if (!(otg_ctrl & OTG_BSESSEND))
3800 +               return -EINVAL;
3801 +
3802 +       otg_ctrl |= OTG_B_BUSREQ;
3803 +       otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_SYS_MASK;
3804 +       omap_writel(otg_ctrl, OTG_CTRL);
3805 +       tu->otg.state = OTG_STATE_B_SRP_INIT;
3806 +
3807 +       return 0;
3808 +}
3809 +
3810 +static int tahvo_usb_start_hnp(struct otg_transceiver *otg)
3811 +{
3812 +       struct tahvo_usb *tu = container_of(otg, struct tahvo_usb, otg);
3813 +
3814 +       dev_dbg(&tu->pt_dev->dev, "start_hnp\n");
3815 +#ifdef CONFIG_USB_OTG
3816 +       /* REVISIT: Add this for OTG */
3817 +#endif
3818 +       return -EINVAL;
3819 +}
3820 +
3821 +static int tahvo_usb_set_host(struct otg_transceiver *otg, struct usb_bus *host)
3822 +{
3823 +       struct tahvo_usb *tu = container_of(otg, struct tahvo_usb, otg);
3824 +       u32 l;
3825 +
3826 +       dev_dbg(&tu->pt_dev->dev, "set_host %p\n", host);
3827 +
3828 +       if (otg == NULL)
3829 +               return -ENODEV;
3830 +
3831 +#if defined(CONFIG_USB_OTG) || !defined(CONFIG_USB_GADGET_OMAP)
3832 +
3833 +       mutex_lock(&tu->serialize);
3834 +
3835 +       if (host == NULL) {
3836 +               if (TAHVO_MODE(tu) == TAHVO_MODE_HOST)
3837 +                       tahvo_usb_power_off(tu);
3838 +               tu->otg.host = NULL;
3839 +               mutex_unlock(&tu->serialize);
3840 +               return 0;
3841 +       }
3842 +
3843 +       l = omap_readl(OTG_SYSCON_1);
3844 +       l &= ~(OTG_IDLE_EN | HST_IDLE_EN | DEV_IDLE_EN);
3845 +       omap_writel(l, OTG_SYSCON_1);
3846 +
3847 +       if (TAHVO_MODE(tu) == TAHVO_MODE_HOST) {
3848 +               tu->otg.host = NULL;
3849 +               tahvo_usb_become_host(tu);
3850 +       } else
3851 +               host_suspend(tu);
3852 +
3853 +       tu->otg.host = host;
3854 +
3855 +       mutex_unlock(&tu->serialize);
3856 +#else
3857 +       /* No host mode configured, so do not allow host controlled to be set */
3858 +       return -EINVAL;
3859 +#endif
3860 +
3861 +       return 0;
3862 +}
3863 +
3864 +static int tahvo_usb_set_peripheral(struct otg_transceiver *otg, struct usb_gadget *gadget)
3865 +{
3866 +       struct tahvo_usb *tu = container_of(otg, struct tahvo_usb, otg);
3867 +
3868 +       dev_dbg(&tu->pt_dev->dev, "set_peripheral %p\n", gadget);
3869 +
3870 +       if (!otg)
3871 +               return -ENODEV;
3872 +
3873 +#if defined(CONFIG_USB_OTG) || defined(CONFIG_USB_GADGET_OMAP)
3874 +
3875 +       mutex_lock(&tu->serialize);
3876 +
3877 +       if (!gadget) {
3878 +               if (TAHVO_MODE(tu) == TAHVO_MODE_PERIPHERAL)
3879 +                       tahvo_usb_power_off(tu);
3880 +               tu->otg.gadget = NULL;
3881 +               mutex_unlock(&tu->serialize);
3882 +               return 0;
3883 +       }
3884 +
3885 +       tu->otg.gadget = gadget;
3886 +       if (TAHVO_MODE(tu) == TAHVO_MODE_PERIPHERAL)
3887 +               tahvo_usb_become_peripheral(tu);
3888 +
3889 +       mutex_unlock(&tu->serialize);
3890 +#else
3891 +       /* No gadget mode configured, so do not allow host controlled to be set */
3892 +       return -EINVAL;
3893 +#endif
3894 +
3895 +       return 0;
3896 +}
3897 +
3898 +static void tahvo_usb_irq_work(struct work_struct *work)
3899 +{
3900 +       struct tahvo_usb *tu = container_of(work, struct tahvo_usb, irq_work);
3901 +
3902 +       mutex_lock(&tu->serialize);
3903 +       check_vbus_state(tu);
3904 +       mutex_unlock(&tu->serialize);
3905 +}
3906 +
3907 +static void tahvo_usb_vbus_interrupt(unsigned long arg)
3908 +{
3909 +       struct tahvo_usb *tu = (struct tahvo_usb *) arg;
3910 +
3911 +       tahvo_ack_irq(TAHVO_INT_VBUSON);
3912 +       /* Seems we need this to acknowledge the interrupt */
3913 +       tahvo_read_reg(TAHVO_REG_IDSR);
3914 +       schedule_work(&tu->irq_work);
3915 +}
3916 +
3917 +#ifdef CONFIG_USB_OTG
3918 +static ssize_t otg_mode_show(struct device *device,
3919 +                            struct device_attribute *attr, char *buf)
3920 +{
3921 +       struct platform_device *pdev = to_platform_device(device);
3922 +       struct tahvo_usb *tu = platform_get_drvdata(pdev);
3923 +
3924 +       switch (tu->tahvo_mode) {
3925 +       case TAHVO_MODE_HOST:
3926 +               return sprintf(buf, "host\n");
3927 +       case TAHVO_MODE_PERIPHERAL:
3928 +               return sprintf(buf, "peripheral\n");
3929 +       }
3930 +
3931 +       return sprintf(buf, "unknown\n");
3932 +}
3933 +
3934 +static ssize_t otg_mode_store(struct device *device,
3935 +                             struct device_attribute *attr,
3936 +                             const char *buf, size_t count)
3937 +{
3938 +       struct platform_device *pdev = to_platform_device(device);
3939 +       struct tahvo_usb *tu = platform_get_drvdata(pdev);
3940 +       int r;
3941 +
3942 +       r = strlen(buf);
3943 +       mutex_lock(&tu->serialize);
3944 +       if (strncmp(buf, "host", 4) == 0) {
3945 +               if (tu->tahvo_mode == TAHVO_MODE_PERIPHERAL)
3946 +                       tahvo_usb_stop_peripheral(tu);
3947 +               tu->tahvo_mode = TAHVO_MODE_HOST;
3948 +               if (tu->otg.host) {
3949 +                       printk(KERN_INFO "Selected HOST mode: host controller present.\n");
3950 +                       tahvo_usb_become_host(tu);
3951 +               } else {
3952 +                       printk(KERN_INFO "Selected HOST mode: no host controller, powering off.\n");
3953 +                       tahvo_usb_power_off(tu);
3954 +               }
3955 +       } else if (strncmp(buf, "peripheral", 10) == 0) {
3956 +               if (tu->tahvo_mode == TAHVO_MODE_HOST)
3957 +                       tahvo_usb_stop_host(tu);
3958 +               tu->tahvo_mode = TAHVO_MODE_PERIPHERAL;
3959 +               if (tu->otg.gadget) {
3960 +                       printk(KERN_INFO "Selected PERIPHERAL mode: gadget driver present.\n");
3961 +                       tahvo_usb_become_peripheral(tu);
3962 +               } else {
3963 +                       printk(KERN_INFO "Selected PERIPHERAL mode: no gadget driver, powering off.\n");
3964 +                       tahvo_usb_power_off(tu);
3965 +               }
3966 +       } else
3967 +               r = -EINVAL;
3968 +
3969 +       mutex_unlock(&tu->serialize);
3970 +       return r;
3971 +}
3972 +
3973 +static DEVICE_ATTR(otg_mode, 0644, otg_mode_show, otg_mode_store);
3974 +#endif
3975 +
3976 +static int tahvo_usb_probe(struct platform_device *pdev)
3977 +{
3978 +       struct tahvo_usb *tu;
3979 +       int ret;
3980 +
3981 +       dev_dbg(&pdev->dev, "probe\n");
3982 +
3983 +       /* Create driver data */
3984 +       tu = kmalloc(sizeof(*tu), GFP_KERNEL);
3985 +       if (!tu)
3986 +               return -ENOMEM;
3987 +       memset(tu, 0, sizeof(*tu));
3988 +       tu->pt_dev = pdev;
3989 +#ifdef CONFIG_USB_OTG
3990 +       /* Default mode */
3991 +#ifdef CONFIG_CBUS_TAHVO_USB_HOST_BY_DEFAULT
3992 +       tu->tahvo_mode = TAHVO_MODE_HOST;
3993 +#else
3994 +       tu->tahvo_mode = TAHVO_MODE_PERIPHERAL;
3995 +#endif
3996 +#endif
3997 +
3998 +       INIT_WORK(&tu->irq_work, tahvo_usb_irq_work);
3999 +       mutex_init(&tu->serialize);
4000 +
4001 +       /* Set initial state, so that we generate kevents only on
4002 +        * state changes */
4003 +       tu->vbus_state = tahvo_read_reg(TAHVO_REG_IDSR) & 0x01;
4004 +
4005 +       /* We cannot enable interrupt until omap_udc is initialized */
4006 +       ret = tahvo_request_irq(TAHVO_INT_VBUSON, tahvo_usb_vbus_interrupt,
4007 +                               (unsigned long) tu, "vbus_interrupt");
4008 +       if (ret != 0) {
4009 +               kfree(tu);
4010 +               printk(KERN_ERR "Could not register Tahvo interrupt for VBUS\n");
4011 +               return ret;
4012 +       }
4013 +
4014 +       /* Attributes */
4015 +       ret = device_create_file(&pdev->dev, &dev_attr_vbus_state);
4016 +#ifdef CONFIG_USB_OTG
4017 +       ret |= device_create_file(&pdev->dev, &dev_attr_otg_mode);
4018 +#endif
4019 +       if (ret)
4020 +               printk(KERN_ERR "attribute creation failed: %d\n", ret);
4021 +
4022 +       /* Create OTG interface */
4023 +       tahvo_usb_power_off(tu);
4024 +       tu->otg.state = OTG_STATE_UNDEFINED;
4025 +       tu->otg.label = DRIVER_NAME;
4026 +       tu->otg.set_host = tahvo_usb_set_host;
4027 +       tu->otg.set_peripheral = tahvo_usb_set_peripheral;
4028 +       tu->otg.set_power = tahvo_usb_set_power;
4029 +       tu->otg.set_suspend = tahvo_usb_set_suspend;
4030 +       tu->otg.start_srp = tahvo_usb_start_srp;
4031 +       tu->otg.start_hnp = tahvo_usb_start_hnp;
4032 +
4033 +       ret = otg_set_transceiver(&tu->otg);
4034 +       if (ret < 0) {
4035 +               printk(KERN_ERR "Cannot register USB transceiver\n");
4036 +               kfree(tu);
4037 +               tahvo_free_irq(TAHVO_INT_VBUSON);
4038 +               return ret;
4039 +       }
4040 +
4041 +       platform_set_drvdata(pdev, tu);
4042 +
4043 +       /* Act upon current vbus state once at startup. A vbus state irq may or
4044 +        * may not be generated in addition to this. */
4045 +       schedule_work(&tu->irq_work);
4046 +       return 0;
4047 +}
4048 +
4049 +static int tahvo_usb_remove(struct platform_device *pdev)
4050 +{
4051 +       dev_dbg(&pdev->dev, "remove\n");
4052 +
4053 +       tahvo_free_irq(TAHVO_INT_VBUSON);
4054 +       flush_scheduled_work();
4055 +       otg_set_transceiver(0);
4056 +       device_remove_file(&pdev->dev, &dev_attr_vbus_state);
4057 +#ifdef CONFIG_USB_OTG
4058 +       device_remove_file(&pdev->dev, &dev_attr_otg_mode);
4059 +#endif
4060 +       return 0;
4061 +}
4062 +
4063 +static struct platform_driver tahvo_usb_driver = {
4064 +       .probe          = tahvo_usb_probe,
4065 +       .remove         = tahvo_usb_remove,
4066 +       .driver         = {
4067 +               .name           = "tahvo-usb",
4068 +       }
4069 +};
4070 +
4071 +static struct platform_device tahvo_usb_device = {
4072 +       .name           = "tahvo-usb",
4073 +       .id             = -1,
4074 +};
4075 +
4076 +static int __init tahvo_usb_init(void)
4077 +{
4078 +       int ret = 0;
4079 +
4080 +       printk(KERN_INFO "Tahvo USB transceiver driver initializing\n");
4081 +
4082 +       ret = platform_driver_register(&tahvo_usb_driver);
4083 +       if (ret)
4084 +               return ret;
4085 +       ret = platform_driver_register(&omap_otg_driver);
4086 +       if (ret) {
4087 +               platform_driver_unregister(&tahvo_usb_driver);
4088 +               return ret;
4089 +       }
4090 +
4091 +       ret = platform_device_register(&tahvo_usb_device);
4092 +       if (ret) {
4093 +               platform_driver_unregister(&omap_otg_driver);
4094 +               platform_driver_unregister(&tahvo_usb_driver);
4095 +               return ret;
4096 +       }
4097 +
4098 +       return 0;
4099 +}
4100 +subsys_initcall(tahvo_usb_init);
4101 +
4102 +static void __exit tahvo_usb_exit(void)
4103 +{
4104 +       platform_device_unregister(&tahvo_usb_device);
4105 +       platform_driver_unregister(&omap_otg_driver);
4106 +       platform_driver_unregister(&tahvo_usb_driver);
4107 +}
4108 +module_exit(tahvo_usb_exit);
4109 +
4110 +MODULE_DESCRIPTION("Tahvo USB OTG Transceiver Driver");
4111 +MODULE_LICENSE("GPL");
4112 +MODULE_AUTHOR("Juha Yrjölä, Tony Lindgren, and Timo Teräs");
4113 --- /dev/null
4114 +++ linux-2.6.36-rc4/drivers/cbus/tahvo-user.c
4115 @@ -0,0 +1,407 @@
4116 +/**
4117 + * drivers/cbus/tahvo-user.c
4118 + *
4119 + * Tahvo user space interface functions
4120 + *
4121 + * Copyright (C) 2004, 2005 Nokia Corporation
4122 + *
4123 + * Written by Mikko Ylinen <mikko.k.ylinen@nokia.com>
4124 + *
4125 + * This file is subject to the terms and conditions of the GNU General
4126 + * Public License. See the file "COPYING" in the main directory of this
4127 + * archive for more details.
4128 + *
4129 + * This program is distributed in the hope that it will be useful,
4130 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4131 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4132 + * GNU General Public License for more details.
4133 + *
4134 + * You should have received a copy of the GNU General Public License
4135 + * along with this program; if not, write to the Free Software
4136 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
4137 + */
4138 +
4139 +#include <linux/types.h>
4140 +#include <linux/kernel.h>
4141 +#include <linux/interrupt.h>
4142 +#include <linux/module.h>
4143 +#include <linux/init.h>
4144 +#include <linux/fs.h>
4145 +#include <linux/miscdevice.h>
4146 +#include <linux/poll.h>
4147 +#include <linux/list.h>
4148 +#include <linux/spinlock.h>
4149 +#include <linux/sched.h>
4150 +#include <linux/mutex.h>
4151 +#include <linux/slab.h>
4152 +
4153 +#include <asm/uaccess.h>
4154 +
4155 +#include "tahvo.h"
4156 +
4157 +#include "user_retu_tahvo.h"
4158 +
4159 +/* Maximum size of IRQ node buffer/pool */
4160 +#define TAHVO_MAX_IRQ_BUF_LEN  16
4161 +
4162 +#define PFX                    "tahvo-user: "
4163 +
4164 +/* Bitmap for marking the interrupt sources as having the handlers */
4165 +static u32 tahvo_irq_bits;
4166 +
4167 +/* For allowing only one user process to subscribe to the tahvo interrupts */
4168 +static struct file *tahvo_irq_subscr = NULL;
4169 +
4170 +/* For poll and IRQ passing */
4171 +struct tahvo_irq {
4172 +       u32 id;
4173 +       struct list_head node;
4174 +};
4175 +
4176 +static spinlock_t tahvo_irqs_lock;
4177 +static struct tahvo_irq *tahvo_irq_block;
4178 +static LIST_HEAD(tahvo_irqs);
4179 +static LIST_HEAD(tahvo_irqs_reserve);
4180 +
4181 +/* Wait queue - used when user wants to read the device */
4182 +DECLARE_WAIT_QUEUE_HEAD(tahvo_user_waitqueue);
4183 +
4184 +/* Semaphore to protect irq subscription sequence */
4185 +static struct mutex tahvo_mutex;
4186 +
4187 +/* This array specifies TAHVO register types (read/write/toggle) */
4188 +static const u8 tahvo_access_bits[] = {
4189 +       1,
4190 +       4,
4191 +       1,
4192 +       3,
4193 +       3,
4194 +       3,
4195 +       3,
4196 +       3,
4197 +       3,
4198 +       3,
4199 +       3,
4200 +       3,
4201 +       3,
4202 +       1
4203 +};
4204 +
4205 +/*
4206 + * The handler for all TAHVO interrupts.
4207 + *
4208 + * arg is the interrupt source in TAHVO.
4209 + */
4210 +static void tahvo_user_irq_handler(unsigned long arg)
4211 +{
4212 +       struct tahvo_irq *irq;
4213 +
4214 +       /* user has to re-enable the interrupt once ready
4215 +        * for receiving them again */
4216 +       tahvo_disable_irq(arg);
4217 +       tahvo_ack_irq(arg);
4218 +
4219 +       spin_lock(&tahvo_irqs_lock);
4220 +       if (list_empty(&tahvo_irqs_reserve)) {
4221 +               spin_unlock(&tahvo_irqs_lock);
4222 +               return;
4223 +       }
4224 +       irq = list_entry((&tahvo_irqs_reserve)->next, struct tahvo_irq, node);
4225 +       irq->id = arg;
4226 +       list_move_tail(&irq->node, &tahvo_irqs);
4227 +       spin_unlock(&tahvo_irqs_lock);
4228 +
4229 +       /* wake up waiting thread */
4230 +       wake_up(&tahvo_user_waitqueue);
4231 +}
4232 +
4233 +/*
4234 + * This routine sets up the interrupt handler and marks an interrupt source
4235 + * in TAHVO as a candidate for signal delivery to the user process.
4236 + */
4237 +static int tahvo_user_subscribe_to_irq(int id, struct file *filp)
4238 +{
4239 +       int ret;
4240 +
4241 +       mutex_lock(&tahvo_mutex);
4242 +       if ((tahvo_irq_subscr != NULL) && (tahvo_irq_subscr != filp)) {
4243 +               mutex_unlock(&tahvo_mutex);
4244 +               return -EBUSY;
4245 +       }
4246 +       /* Store the file pointer of the first user process registering IRQs */
4247 +       tahvo_irq_subscr = filp;
4248 +       mutex_unlock(&tahvo_mutex);
4249 +
4250 +       if (tahvo_irq_bits & (1 << id))
4251 +               return 0;
4252 +
4253 +       ret = tahvo_request_irq(id, tahvo_user_irq_handler, id, "");
4254 +       if (ret < 0)
4255 +               return ret;
4256 +
4257 +       /* Mark that this interrupt has a handler */
4258 +       tahvo_irq_bits |= 1 << id;
4259 +
4260 +       return 0;
4261 +}
4262 +
4263 +/*
4264 + * Unregister all TAHVO interrupt handlers
4265 + */
4266 +static void tahvo_unreg_irq_handlers(void)
4267 +{
4268 +       int id;
4269 +
4270 +       if (!tahvo_irq_bits)
4271 +               return;
4272 +
4273 +       for (id = 0; id < MAX_TAHVO_IRQ_HANDLERS; id++)
4274 +               if (tahvo_irq_bits & (1 << id))
4275 +                       tahvo_free_irq(id);
4276 +
4277 +       tahvo_irq_bits = 0;
4278 +}
4279 +
4280 +/*
4281 + * Write to TAHVO register.
4282 + * Returns 0 upon success, a negative error value otherwise.
4283 + */
4284 +static int tahvo_user_write_with_mask(u32 field, u16 value)
4285 +{
4286 +       u32 mask;
4287 +       u32 reg;
4288 +       u_short tmp;
4289 +       unsigned long flags;
4290 +
4291 +       mask = MASK(field);
4292 +       reg = REG(field);
4293 +
4294 +       /* Detect bad mask and reg */
4295 +       if (mask == 0 || reg > TAHVO_REG_MAX ||
4296 +           tahvo_access_bits[reg] == READ_ONLY) {
4297 +               printk(KERN_ERR PFX "invalid arguments (reg=%#x, mask=%#x)\n",
4298 +                      reg, mask);
4299 +               return -EINVAL;
4300 +       }
4301 +
4302 +       /* Justify value according to mask */
4303 +       while (!(mask & 1)) {
4304 +               value = value << 1;
4305 +               mask = mask >> 1;
4306 +       }
4307 +
4308 +       spin_lock_irqsave(&tahvo_lock, flags);
4309 +       if (tahvo_access_bits[reg] == TOGGLE) {
4310 +               /* No need to detect previous content of register */
4311 +               tmp = 0;
4312 +       } else {
4313 +               /* Read current value of register */
4314 +               tmp = tahvo_read_reg(reg);
4315 +       }
4316 +       /* Generate a new value */
4317 +       tmp = (tmp & ~MASK(field)) | (value & MASK(field));
4318 +       /* Write data to TAHVO */
4319 +       tahvo_write_reg(reg, tmp);
4320 +       spin_unlock_irqrestore(&tahvo_lock, flags);
4321 +
4322 +       return 0;
4323 +}
4324 +
4325 +/*
4326 + * Read TAHVO register.
4327 + */
4328 +static u32 tahvo_user_read_with_mask(u32 field)
4329 +{
4330 +       u_short value;
4331 +       u32 mask, reg;
4332 +
4333 +       mask = MASK(field);
4334 +       reg = REG(field);
4335 +
4336 +       /* Detect bad mask and reg */
4337 +       if (mask == 0 || reg > TAHVO_REG_MAX) {
4338 +               printk(KERN_ERR PFX "invalid arguments (reg=%#x, mask=%#x)\n",
4339 +                      reg, mask);
4340 +               return -EINVAL;
4341 +       }
4342 +
4343 +       /* Read the register */
4344 +       value = tahvo_read_reg(reg) & mask;
4345 +
4346 +       /* Right justify value */
4347 +       while (!(mask & 1)) {
4348 +               value = value >> 1;
4349 +               mask = mask >> 1;
4350 +       }
4351 +
4352 +       return value;
4353 +}
4354 +
4355 +/*
4356 + * Close device
4357 + */
4358 +static int tahvo_close(struct inode *inode, struct file *filp)
4359 +{
4360 +       /* Unregister all interrupts that have been registered */
4361 +       if (tahvo_irq_subscr == filp) {
4362 +               tahvo_unreg_irq_handlers();
4363 +               tahvo_irq_subscr = NULL;
4364 +       }
4365 +
4366 +       return 0;
4367 +}
4368 +
4369 +/*
4370 + * Device control (ioctl)
4371 + */
4372 +static int tahvo_ioctl(struct inode *inode, struct file *filp,
4373 +                      unsigned int cmd, unsigned long arg)
4374 +{
4375 +       struct retu_tahvo_write_parms par;
4376 +       int ret;
4377 +
4378 +       switch (cmd) {
4379 +       case URT_IOCT_IRQ_SUBSCR:
4380 +               return tahvo_user_subscribe_to_irq(arg, filp);
4381 +       case TAHVO_IOCH_READ:
4382 +               return tahvo_user_read_with_mask(arg);
4383 +       case TAHVO_IOCX_WRITE:
4384 +               ret = copy_from_user(&par, (void __user *) arg, sizeof(par));
4385 +               if (ret)
4386 +                       printk(KERN_ERR "copy_from_user failed: %d\n", ret);
4387 +               par.result = tahvo_user_write_with_mask(par.field, par.value);
4388 +               ret = copy_to_user((void __user *) arg, &par, sizeof(par));
4389 +               if (ret)
4390 +                       printk(KERN_ERR "copy_to_user failed: %d\n", ret);
4391 +               break;
4392 +       default:
4393 +               return -ENOIOCTLCMD;
4394 +       }
4395 +       return 0;
4396 +}
4397 +
4398 +/*
4399 + * Read from device
4400 + */
4401 +static ssize_t tahvo_read(struct file *filp, char *buf, size_t count,
4402 +                         loff_t * offp)
4403 +{
4404 +       struct tahvo_irq *irq;
4405 +
4406 +       u32 nr, i;
4407 +
4408 +       /* read not permitted if neither filp nor anyone has registered IRQs */
4409 +       if (tahvo_irq_subscr != filp)
4410 +               return -EPERM;
4411 +
4412 +       if ((count < sizeof(u32)) || ((count % sizeof(u32)) != 0))
4413 +               return -EINVAL;
4414 +
4415 +       nr = count / sizeof(u32);
4416 +
4417 +       for (i = 0; i < nr; i++) {
4418 +               unsigned long flags;
4419 +               u32 irq_id;
4420 +               int ret;
4421 +
4422 +               ret = wait_event_interruptible(tahvo_user_waitqueue,
4423 +                                              !list_empty(&tahvo_irqs));
4424 +               if (ret < 0)
4425 +                       return ret;
4426 +
4427 +               spin_lock_irqsave(&tahvo_irqs_lock, flags);
4428 +               irq = list_entry((&tahvo_irqs)->next, struct tahvo_irq, node);
4429 +               irq_id = irq->id;
4430 +               list_move(&irq->node, &tahvo_irqs_reserve);
4431 +               spin_unlock_irqrestore(&tahvo_irqs_lock, flags);
4432 +
4433 +               ret = copy_to_user(buf + i * sizeof(irq_id), &irq_id,
4434 +                                  sizeof(irq_id));
4435 +               if (ret)
4436 +                       printk(KERN_ERR "copy_to_user failed: %d\n", ret);
4437 +       }
4438 +
4439 +       return count;
4440 +}
4441 +
4442 +/*
4443 + * Poll method
4444 + */
4445 +static unsigned tahvo_poll(struct file *filp, struct poll_table_struct *pt)
4446 +{
4447 +       if (!list_empty(&tahvo_irqs))
4448 +               return POLLIN;
4449 +
4450 +       poll_wait(filp, &tahvo_user_waitqueue, pt);
4451 +
4452 +       if (!list_empty(&tahvo_irqs))
4453 +               return POLLIN;
4454 +       else
4455 +               return 0;
4456 +}
4457 +
4458 +static struct file_operations tahvo_user_fileops = {
4459 +       .owner = THIS_MODULE,
4460 +       .unlocked_ioctl = tahvo_ioctl,
4461 +       .read = tahvo_read,
4462 +       .release = tahvo_close,
4463 +       .poll = tahvo_poll
4464 +};
4465 +
4466 +static struct miscdevice tahvo_device = {
4467 +       .minor = MISC_DYNAMIC_MINOR,
4468 +       .name = "tahvo",
4469 +       .fops = &tahvo_user_fileops
4470 +};
4471 +
4472 +/*
4473 + * Initialization
4474 + *
4475 + * @return 0 if successful, error value otherwise.
4476 + */
4477 +int tahvo_user_init(void)
4478 +{
4479 +       struct tahvo_irq *irq;
4480 +       int res, i;
4481 +
4482 +       irq = kmalloc(sizeof(*irq) * TAHVO_MAX_IRQ_BUF_LEN, GFP_KERNEL);
4483 +       if (irq == NULL) {
4484 +               printk(KERN_ERR PFX "kmalloc failed\n");
4485 +               return -ENOMEM;
4486 +       }
4487 +       memset(irq, 0, sizeof(*irq) * TAHVO_MAX_IRQ_BUF_LEN);
4488 +       for (i = 0; i < TAHVO_MAX_IRQ_BUF_LEN; i++)
4489 +               list_add(&irq[i].node, &tahvo_irqs_reserve);
4490 +
4491 +       tahvo_irq_block = irq;
4492 +
4493 +       spin_lock_init(&tahvo_irqs_lock);
4494 +       mutex_init(&tahvo_mutex);
4495 +
4496 +       /* Request a misc device */
4497 +       res = misc_register(&tahvo_device);
4498 +       if (res < 0) {
4499 +               printk(KERN_ERR PFX "unable to register misc device for %s\n",
4500 +                      tahvo_device.name);
4501 +               kfree(irq);
4502 +               return res;
4503 +       }
4504 +
4505 +       return 0;
4506 +}
4507 +
4508 +/*
4509 + * Cleanup.
4510 + */
4511 +void tahvo_user_cleanup(void)
4512 +{
4513 +       /* Unregister our misc device */
4514 +       misc_deregister(&tahvo_device);
4515 +       /* Unregister and disable all TAHVO interrupts */
4516 +       tahvo_unreg_irq_handlers();
4517 +       kfree(tahvo_irq_block);
4518 +}
4519 +
4520 +MODULE_DESCRIPTION("Tahvo ASIC user space functions");
4521 +MODULE_LICENSE("GPL");
4522 +MODULE_AUTHOR("Mikko Ylinen");
4523 --- /dev/null
4524 +++ linux-2.6.36-rc4/drivers/cbus/user_retu_tahvo.h
4525 @@ -0,0 +1,75 @@
4526 +/**
4527 + * drivers/cbus/user_retu_tahvo.h
4528 + *
4529 + * Copyright (C) 2004, 2005 Nokia Corporation
4530 + *
4531 + * Written by Mikko Ylinen <mikko.k.ylinen@nokia.com>
4532 + *
4533 + * Definitions and types used by both retu-user and tahvo-user.
4534 + *
4535 + * This file is subject to the terms and conditions of the GNU General
4536 + * Public License. See the file "COPYING" in the main directory of this
4537 + * archive for more details.
4538 + *
4539 + * This program is distributed in the hope that it will be useful,
4540 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4541 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4542 + * GNU General Public License for more details.
4543 +
4544 + * You should have received a copy of the GNU General Public License
4545 + * along with this program; if not, write to the Free Software
4546 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
4547 + */
4548 +
4549 +#ifndef _USER_RETU_TAHVO_H
4550 +#define _USER_RETU_TAHVO_H
4551 +
4552 +/* Chip IDs */
4553 +#define CHIP_RETU      1
4554 +#define CHIP_TAHVO     2
4555 +
4556 +/* Register access type bits */
4557 +#define READ_ONLY              1
4558 +#define WRITE_ONLY             2
4559 +#define READ_WRITE             3
4560 +#define TOGGLE                 4
4561 +
4562 +#define MASK(field)            ((u16)(field & 0xFFFF))
4563 +#define REG(field)             ((u16)((field >> 16) & 0x3F))
4564 +
4565 +/*** IOCTL definitions. These should be kept in sync with user space **********/
4566 +
4567 +#define URT_IOC_MAGIC '`'
4568 +
4569 +/*
4570 + * IOCTL function naming conventions:
4571 + * ==================================
4572 + *  0 -- No argument and return value
4573 + *  S -- Set through a pointer
4574 + *  T -- Tell directly with the argument value
4575 + *  G -- Reply by setting through a pointer
4576 + *  Q -- response is on the return value
4577 + *  X -- S and G atomically
4578 + *  H -- T and Q atomically
4579 + */
4580 +
4581 +/* General */
4582 +#define URT_IOCT_IRQ_SUBSCR            _IO(URT_IOC_MAGIC, 0)
4583 +
4584 +/* RETU */
4585 +#define RETU_IOCH_READ                 _IO(URT_IOC_MAGIC, 1)
4586 +#define RETU_IOCX_WRITE                        _IO(URT_IOC_MAGIC, 2)
4587 +#define RETU_IOCH_ADC_READ             _IO(URT_IOC_MAGIC, 3)
4588 +
4589 +/* TAHVO */
4590 +#define TAHVO_IOCH_READ                        _IO(URT_IOC_MAGIC, 4)
4591 +#define TAHVO_IOCX_WRITE               _IO(URT_IOC_MAGIC, 5)
4592 +
4593 +/* This structure is used for writing RETU/TAHVO registers */
4594 +struct retu_tahvo_write_parms {
4595 +    u32        field;
4596 +    u16        value;
4597 +    u8 result;
4598 +};
4599 +
4600 +#endif
4601 --- linux-2.6.36-rc4.orig/drivers/Makefile
4602 +++ linux-2.6.36-rc4/drivers/Makefile
4603 @@ -74,7 +74,7 @@ obj-$(CONFIG_GAMEPORT)                += input/gamepor
4604  obj-$(CONFIG_INPUT)            += input/
4605  obj-$(CONFIG_I2O)              += message/
4606  obj-$(CONFIG_RTC_LIB)          += rtc/
4607 -obj-y                          += i2c/ media/
4608 +obj-y                          += i2c/ media/ cbus/
4609  obj-$(CONFIG_PPS)              += pps/
4610  obj-$(CONFIG_W1)               += w1/
4611  obj-$(CONFIG_POWER_SUPPLY)     += power/
4612 --- linux-2.6.36-rc4.orig/arch/arm/Kconfig
4613 +++ linux-2.6.36-rc4/arch/arm/Kconfig
4614 @@ -1760,6 +1760,10 @@ source "net/Kconfig"
4615  
4616  source "drivers/Kconfig"
4617  
4618 +if ARCH_OMAP
4619 +source "drivers/cbus/Kconfig"
4620 +endif
4621 +
4622  source "fs/Kconfig"
4623  
4624  source "arch/arm/Kconfig.debug"