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