omap24xx: bump to 3.1.3
[openwrt.git] / target / linux / omap24xx / patches-3.1 / 250-cbus.patch
1 --- /dev/null
2 +++ b/drivers/cbus/cbus.c
3 @@ -0,0 +1,333 @@
4 +/*
5 + * drivers/cbus/cbus.c
6 + *
7 + * Support functions for CBUS serial protocol
8 + *
9 + * Copyright (C) 2004-2010 Nokia Corporation
10 + * Contact: Felipe Balbi <felipe.balbi@nokia.com>
11 + *
12 + * Written by Juha Yrjölä <juha.yrjola@nokia.com>,
13 + *           David Weinehall <david.weinehall@nokia.com>, and
14 + *           Mikko Ylinen <mikko.k.ylinen@nokia.com>
15 + *
16 + * Several updates and cleanups by Felipe Balbi <felipe.balbi@nokia.com>
17 + *
18 + * This file is subject to the terms and conditions of the GNU General
19 + * Public License. See the file "COPYING" in the main directory of this
20 + * archive for more details.
21 + *
22 + * This program is distributed in the hope that it will be useful,
23 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 + * GNU General Public License for more details.
26 + *
27 + * You should have received a copy of the GNU General Public License
28 + * along with this program; if not, write to the Free Software
29 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
30 + */
31 +
32 +#include <linux/device.h>
33 +#include <linux/init.h>
34 +#include <linux/kernel.h>
35 +#include <linux/slab.h>
36 +#include <linux/spinlock.h>
37 +#include <linux/gpio.h>
38 +#include <linux/platform_device.h>
39 +#include <linux/platform_data/cbus.h>
40 +
41 +#include "cbus.h"
42 +
43 +#define CBUS_XFER_READ         1
44 +#define CBUS_XFER_WRITE                0
45 +
46 +struct cbus_host {
47 +       /* host lock */
48 +       spinlock_t      lock;
49 +
50 +       struct device   *dev;
51 +
52 +       int             clk_gpio;
53 +       int             dat_gpio;
54 +       int             sel_gpio;
55 +};
56 +
57 +/**
58 + * cbus_send_bit - sends one bit over the bus
59 + * @host: the host we're using
60 + * @bit: one bit of information to send
61 + * @input: whether to set data pin as input after sending
62 + */
63 +static int cbus_send_bit(struct cbus_host *host, unsigned bit,
64 +               unsigned input)
65 +{
66 +       int ret = 0;
67 +
68 +       gpio_set_value(host->dat_gpio, bit ? 1 : 0);
69 +       gpio_set_value(host->clk_gpio, 1);
70 +
71 +       /* The data bit is read on the rising edge of CLK */
72 +       if (input)
73 +               ret = gpio_direction_input(host->dat_gpio);
74 +
75 +       gpio_set_value(host->clk_gpio, 0);
76 +
77 +       return ret;
78 +}
79 +
80 +/**
81 + * cbus_send_data - sends @len amount of data over the bus
82 + * @host: the host we're using
83 + * @data: the data to send
84 + * @len: size of the transfer
85 + * @input: whether to set data pin as input after sending
86 + */
87 +static int cbus_send_data(struct cbus_host *host, unsigned data, unsigned len,
88 +               unsigned input)
89 +{
90 +       int ret = 0;
91 +       int i;
92 +
93 +       for (i = len; i > 0; i--) {
94 +               ret = cbus_send_bit(host, data & (1 << (i - 1)),
95 +                               input && (i == 1));
96 +               if (ret < 0)
97 +                       goto out;
98 +       }
99 +
100 +out:
101 +       return ret;
102 +}
103 +
104 +/**
105 + * cbus_receive_bit - receives one bit from the bus
106 + * @host: the host we're using
107 + */
108 +static int cbus_receive_bit(struct cbus_host *host)
109 +{
110 +       int ret;
111 +
112 +       gpio_set_value(host->clk_gpio, 1);
113 +       ret = gpio_get_value(host->dat_gpio);
114 +       if (ret < 0)
115 +               goto out;
116 +       gpio_set_value(host->clk_gpio, 0);
117 +
118 +out:
119 +       return ret;
120 +}
121 +
122 +/**
123 + * cbus_receive_data - receives @len data from the bus
124 + * @host: the host we're using
125 + * @len: the length of data to receive
126 + */
127 +static int cbus_receive_data(struct cbus_host *host, unsigned len)
128 +{
129 +       int ret = 0;
130 +       int i;
131 +
132 +       for (i = 16; i > 0; i--) {
133 +               int bit = cbus_receive_bit(host);
134 +
135 +               if (bit < 0)
136 +                       goto out;
137 +
138 +               if (bit)
139 +                       ret |= 1 << (i - 1);
140 +       }
141 +
142 +out:
143 +       return ret;
144 +}
145 +
146 +/**
147 + * cbus_transfer - transfers data over the bus
148 + * @host: the host we're using
149 + * @rw: read/write flag
150 + * @dev: device address
151 + * @reg: register address
152 + * @data: if @rw == 0 data to send otherwise 0
153 + */
154 +static int cbus_transfer(struct cbus_host *host, unsigned rw, unsigned dev,
155 +               unsigned reg, unsigned data)
156 +{
157 +       unsigned long flags;
158 +       int input = 0;
159 +       int ret = 0;
160 +
161 +       /* We don't want interrupts disturbing our transfer */
162 +       spin_lock_irqsave(&host->lock, flags);
163 +
164 +       /* Reset state and start of transfer, SEL stays down during transfer */
165 +       gpio_set_value(host->sel_gpio, 0);
166 +
167 +       /* Set the DAT pin to output */
168 +       gpio_direction_output(host->dat_gpio, 1);
169 +
170 +       /* Send the device address */
171 +       ret = cbus_send_data(host, dev, 3, 0);
172 +       if (ret < 0) {
173 +               dev_dbg(host->dev, "failed sending device addr\n");
174 +               goto out;
175 +       }
176 +
177 +       /* Send the rw flag */
178 +       ret = cbus_send_bit(host, rw, 0);
179 +       if (ret < 0) {
180 +               dev_dbg(host->dev, "failed sending read/write flag\n");
181 +               goto out;
182 +       }
183 +
184 +       /* Send the register address */
185 +       if (rw)
186 +               input = true;
187 +
188 +       ret = cbus_send_data(host, reg, 5, input);
189 +       if (ret < 0) {
190 +               dev_dbg(host->dev, "failed sending register addr\n");
191 +               goto out;
192 +       }
193 +
194 +       if (!rw) {
195 +               ret = cbus_send_data(host, data, 16, 0);
196 +               if (ret < 0) {
197 +                       dev_dbg(host->dev, "failed sending data\n");
198 +                       goto out;
199 +               }
200 +       } else {
201 +               gpio_set_value(host->clk_gpio, 1);
202 +
203 +               ret = cbus_receive_data(host, 16);
204 +               if (ret < 0) {
205 +                       dev_dbg(host->dev, "failed receiving data\n");
206 +                       goto out;
207 +               }
208 +       }
209 +
210 +       /* Indicate end of transfer, SEL goes up until next transfer */
211 +       gpio_set_value(host->sel_gpio, 1);
212 +       gpio_set_value(host->clk_gpio, 1);
213 +       gpio_set_value(host->clk_gpio, 0);
214 +
215 +out:
216 +       spin_unlock_irqrestore(&host->lock, flags);
217 +
218 +       return ret;
219 +}
220 +
221 +/**
222 + * cbus_read_reg - reads a given register from the device
223 + * @child: the child device
224 + * @dev: device address
225 + * @reg: register address
226 + */
227 +int cbus_read_reg(struct device *child, unsigned dev, unsigned reg)
228 +{
229 +       struct cbus_host        *host = dev_get_drvdata(child->parent);
230 +
231 +       return cbus_transfer(host, CBUS_XFER_READ, dev, reg, 0);
232 +}
233 +EXPORT_SYMBOL(cbus_read_reg);
234 +
235 +/**
236 + * cbus_write_reg - writes to a given register of the device
237 + * @child: the child device
238 + * @dev: device address
239 + * @reg: register address
240 + * @val: data to be written to @reg
241 + */
242 +int cbus_write_reg(struct device *child, unsigned dev, unsigned reg,
243 +               unsigned val)
244 +{
245 +       struct cbus_host        *host = dev_get_drvdata(child->parent);
246 +
247 +       return cbus_transfer(host, CBUS_XFER_WRITE, dev, reg, val);
248 +}
249 +EXPORT_SYMBOL(cbus_write_reg);
250 +
251 +static int __init cbus_bus_probe(struct platform_device *pdev)
252 +{
253 +       struct cbus_host *chost;
254 +       struct cbus_host_platform_data *pdata = pdev->dev.platform_data;
255 +       int ret;
256 +
257 +       chost = kzalloc(sizeof(*chost), GFP_KERNEL);
258 +       if (chost == NULL)
259 +               return -ENOMEM;
260 +
261 +       spin_lock_init(&chost->lock);
262 +
263 +       chost->clk_gpio = pdata->clk_gpio;
264 +       chost->dat_gpio = pdata->dat_gpio;
265 +       chost->sel_gpio = pdata->sel_gpio;
266 +       chost->dev = &pdev->dev;
267 +
268 +       ret = gpio_request(chost->clk_gpio, "CBUS clk");
269 +       if (ret < 0)
270 +               goto exit1;
271 +
272 +       ret = gpio_request(chost->dat_gpio, "CBUS data");
273 +       if (ret < 0)
274 +               goto exit2;
275 +
276 +       ret = gpio_request(chost->sel_gpio, "CBUS sel");
277 +       if (ret < 0)
278 +               goto exit3;
279 +
280 +       gpio_direction_output(chost->clk_gpio, 0);
281 +       gpio_direction_input(chost->dat_gpio);
282 +       gpio_direction_output(chost->sel_gpio, 1);
283 +
284 +       gpio_set_value(chost->clk_gpio, 1);
285 +       gpio_set_value(chost->clk_gpio, 0);
286 +
287 +       platform_set_drvdata(pdev, chost);
288 +
289 +       return 0;
290 +exit3:
291 +       gpio_free(chost->dat_gpio);
292 +exit2:
293 +       gpio_free(chost->clk_gpio);
294 +exit1:
295 +       kfree(chost);
296 +
297 +       return ret;
298 +}
299 +
300 +static void __exit cbus_bus_remove(struct platform_device *pdev)
301 +{
302 +       struct cbus_host        *chost = platform_get_drvdata(pdev);
303 +
304 +       gpio_free(chost->sel_gpio);
305 +       gpio_free(chost->dat_gpio);
306 +       gpio_free(chost->clk_gpio);
307 +
308 +       kfree(chost);
309 +}
310 +
311 +static struct platform_driver cbus_driver = {
312 +       .remove         = __exit_p(cbus_bus_remove),
313 +       .driver         = {
314 +               .name   = "cbus",
315 +       },
316 +};
317 +
318 +static int __init cbus_bus_init(void)
319 +{
320 +       return platform_driver_probe(&cbus_driver, cbus_bus_probe);
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 +MODULE_AUTHOR("Felipe Balbi <felipe.balbi@nokia.com>");
336 +
337 --- /dev/null
338 +++ b/drivers/cbus/cbus.h
339 @@ -0,0 +1,30 @@
340 +/*
341 + * drivers/cbus/cbus.h
342 + *
343 + * Copyright (C) 2004, 2005 Nokia Corporation
344 + *
345 + * Written by Juha Yrjölä <juha.yrjola@nokia.com> and
346 + *           David Weinehall <david.weinehall@nokia.com>
347 + *
348 + * This file is subject to the terms and conditions of the GNU General
349 + * Public License. See the file "COPYING" in the main directory of this
350 + * archive for more details.
351 + *
352 + * This program is distributed in the hope that it will be useful,
353 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
354 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
355 + * GNU General Public License for more details.
356 + *
357 + * You should have received a copy of the GNU General Public License
358 + * along with this program; if not, write to the Free Software
359 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
360 + */
361 +
362 +#ifndef __DRIVERS_CBUS_CBUS_H
363 +#define __DRIVERS_CBUS_CBUS_H
364 +
365 +extern int cbus_read_reg(struct device *, unsigned dev, unsigned reg);
366 +extern int cbus_write_reg(struct device *, unsigned dev, unsigned reg,
367 +               unsigned val);
368 +
369 +#endif /* __DRIVERS_CBUS_CBUS_H */
370 --- /dev/null
371 +++ b/drivers/cbus/Kconfig
372 @@ -0,0 +1,86 @@
373 +#
374 +# CBUS device configuration
375 +#
376 +
377 +menu "CBUS support"
378 +
379 +config CBUS
380 +       bool "CBUS support on OMAP"
381 +       ---help---
382 +         CBUS is a proprietary serial protocol by Nokia.  It is mainly
383 +         used for accessing Energy Management auxiliary chips.
384 +
385 +         If you want CBUS support, you should say Y here.
386 +
387 +config CBUS_TAHVO
388 +       depends on CBUS
389 +       bool "Support for Tahvo"
390 +       ---help---
391 +         Tahvo is a mixed signal ASIC with some system features
392 +
393 +         If you want Tahvo support, you should say Y here.
394 +
395 +if CBUS_TAHVO
396 +
397 +config CBUS_TAHVO_USB
398 +       depends on USB
399 +       depends on ARCH_OMAP
400 +       select USB_OTG_UTILS
401 +       tristate "Support for Tahvo USB transceiver"
402 +       ---help---
403 +         If you want Tahvo support for USB transceiver, say Y or M here.
404 +
405 +config CBUS_TAHVO_USB_HOST_BY_DEFAULT
406 +       depends on CBUS_TAHVO_USB && USB_OTG
407 +       boolean "Device in USB host mode by default"
408 +       ---help---
409 +         Say Y here, if you want the device to enter USB host mode
410 +         by default on bootup.
411 +
412 +endif # CBUS_TAHVO
413 +
414 +config CBUS_RETU
415 +       depends on CBUS
416 +       bool "Support for Retu"
417 +       ---help---
418 +         Retu is a mixed signal ASIC with some system features
419 +
420 +         If you want Retu support, you should say Y here.
421 +
422 +if CBUS_RETU
423 +
424 +config CBUS_RETU_POWERBUTTON
425 +       depends on INPUT
426 +       bool "Support for Retu power button"
427 +       ---help---
428 +         The power button on Nokia 770 is connected to the Retu ASIC.
429 +
430 +         If you want support for the Retu power button, you should say Y here.
431 +
432 +config CBUS_RETU_RTC
433 +       depends on RTC_CLASS
434 +       depends on ARCH_OMAP
435 +       tristate "Support for Retu pseudo-RTC"
436 +       ---help---
437 +         Say Y here if you want support for the device that alleges to be an
438 +         RTC in Retu. This will expose a sysfs interface for it.
439 +
440 +config CBUS_RETU_WDT
441 +       depends on SYSFS && WATCHDOG
442 +       depends on ARCH_OMAP
443 +       tristate "Support for Retu watchdog timer"
444 +       ---help---
445 +         Say Y here if you want support for the watchdog in Retu. This will
446 +         expose a sysfs interface to grok it.
447 +
448 +config CBUS_RETU_HEADSET
449 +       depends on SYSFS
450 +       tristate "Support for headset detection with Retu/Vilma"
451 +       ---help---
452 +         Say Y here if you want support detecting a headset that's connected
453 +         to Retu/Vilma. Detection state and events are exposed through
454 +         sysfs.
455 +
456 +endif # CBUS_RETU
457 +
458 +endmenu
459 --- /dev/null
460 +++ b/drivers/cbus/Makefile
461 @@ -0,0 +1,13 @@
462 +#
463 +# Makefile for CBUS.
464 +#
465 +
466 +obj-$(CONFIG_CBUS)             += cbus.o
467 +obj-$(CONFIG_CBUS_TAHVO)       += tahvo.o
468 +obj-$(CONFIG_CBUS_RETU)                += retu.o
469 +obj-$(CONFIG_CBUS_TAHVO_USB)   += tahvo-usb.o
470 +
471 +obj-$(CONFIG_CBUS_RETU_POWERBUTTON) += retu-pwrbutton.o
472 +obj-$(CONFIG_CBUS_RETU_RTC)    += retu-rtc.o
473 +obj-$(CONFIG_CBUS_RETU_WDT)    += retu-wdt.o
474 +obj-$(CONFIG_CBUS_RETU_HEADSET)        += retu-headset.o
475 --- /dev/null
476 +++ b/drivers/cbus/retu.c
477 @@ -0,0 +1,549 @@
478 +/**
479 + * drivers/cbus/retu.c
480 + *
481 + * Support functions for Retu ASIC
482 + *
483 + * Copyright (C) 2004, 2005 Nokia Corporation
484 + *
485 + * Written by Juha Yrjölä <juha.yrjola@nokia.com>,
486 + *           David Weinehall <david.weinehall@nokia.com>, and
487 + *           Mikko Ylinen <mikko.k.ylinen@nokia.com>
488 + *
489 + * This file is subject to the terms and conditions of the GNU General
490 + * Public License. See the file "COPYING" in the main directory of this
491 + * archive for more details.
492 + *
493 + * This program is distributed in the hope that it will be useful,
494 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
495 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
496 + * GNU General Public License for more details.
497 + *
498 + * You should have received a copy of the GNU General Public License
499 + * along with this program; if not, write to the Free Software
500 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
501 + */
502 +
503 +#include <linux/module.h>
504 +#include <linux/init.h>
505 +
506 +#include <linux/slab.h>
507 +#include <linux/kernel.h>
508 +#include <linux/errno.h>
509 +#include <linux/device.h>
510 +#include <linux/mutex.h>
511 +#include <linux/irq.h>
512 +#include <linux/interrupt.h>
513 +#include <linux/platform_device.h>
514 +#include <linux/platform_data/cbus.h>
515 +
516 +#include <asm/bitops.h>
517 +
518 +#include "cbus.h"
519 +#include "retu.h"
520 +
521 +struct retu {
522 +       /* Device lock */
523 +       struct mutex            mutex;
524 +       struct device           *dev;
525 +
526 +       int                     devid;
527 +
528 +       int                     irq_base;
529 +       int                     irq_end;
530 +
531 +       int                     irq;
532 +
533 +       int                     ack;
534 +       bool                    ack_pending;
535 +
536 +       int                     mask;
537 +       bool                    mask_pending;
538 +
539 +       bool                    is_vilma;
540 +};
541 +
542 +static struct retu *the_retu;
543 +
544 +/**
545 + * __retu_read_reg - Read a value from a register in Retu
546 + * @retu: pointer to retu structure
547 + * @reg: the register address to read from
548 + */
549 +static int __retu_read_reg(struct retu *retu, unsigned reg)
550 +{
551 +       return cbus_read_reg(retu->dev, retu->devid, reg);
552 +}
553 +
554 +/**
555 + * __retu_write_reg - Writes a value to a register in Retu
556 + * @retu: pointer to retu structure
557 + * @reg: the register address to write to
558 + * @val: the value to write to the register
559 + */
560 +static void __retu_write_reg(struct retu *retu, unsigned reg, u16 val)
561 +{
562 +       cbus_write_reg(retu->dev, retu->devid, reg, val);
563 +}
564 +
565 +/**
566 + * retu_read_reg - Read a value from a register in Retu
567 + * @child: device pointer for the calling child
568 + * @reg: the register to read from
569 + *
570 + * This function returns the contents of the specified register
571 + */
572 +int retu_read_reg(struct device *child, unsigned reg)
573 +{
574 +       struct retu             *retu = dev_get_drvdata(child->parent);
575 +
576 +       return __retu_read_reg(retu, reg);
577 +}
578 +EXPORT_SYMBOL_GPL(retu_read_reg);
579 +
580 +/**
581 + * retu_write_reg - Write a value to a register in Retu
582 + * @child: the pointer to our calling child
583 + * @reg: the register to write to
584 + * @val: the value to write to the register
585 + *
586 + * This function writes a value to the specified register
587 + */
588 +void retu_write_reg(struct device *child, unsigned reg, u16 val)
589 +{
590 +       struct retu             *retu = dev_get_drvdata(child->parent);
591 +
592 +       mutex_lock(&retu->mutex);
593 +       __retu_write_reg(retu, reg, val);
594 +       mutex_unlock(&retu->mutex);
595 +}
596 +EXPORT_SYMBOL_GPL(retu_write_reg);
597 +
598 +/**
599 + * retu_set_clear_reg_bits - helper function to read/set/clear bits
600 + * @child: device pointer to calling child
601 + * @reg: the register address
602 + * @set: mask for setting bits
603 + * @clear: mask for clearing bits
604 + */
605 +void retu_set_clear_reg_bits(struct device *child, unsigned reg, u16 set,
606 +               u16 clear)
607 +{
608 +       struct retu             *retu = dev_get_drvdata(child->parent);
609 +       u16                     w;
610 +
611 +       mutex_lock(&retu->mutex);
612 +       w = __retu_read_reg(retu, reg);
613 +       w &= ~clear;
614 +       w |= set;
615 +       __retu_write_reg(retu, reg, w);
616 +       mutex_unlock(&retu->mutex);
617 +}
618 +EXPORT_SYMBOL_GPL(retu_set_clear_reg_bits);
619 +
620 +#define ADC_MAX_CHAN_NUMBER    13
621 +
622 +/**
623 + * retu_read_adc - Reads AD conversion result
624 + * @child: device pointer to calling child
625 + * @channel: the ADC channel to read from
626 + */
627 +int retu_read_adc(struct device *child, int channel)
628 +{
629 +       struct retu             *retu = dev_get_drvdata(child->parent);
630 +       int                     res;
631 +
632 +       if (!retu)
633 +               return -ENODEV;
634 +
635 +       if (channel < 0 || channel > ADC_MAX_CHAN_NUMBER)
636 +               return -EINVAL;
637 +
638 +       mutex_lock(&retu->mutex);
639 +
640 +       if ((channel == 8) && retu->is_vilma) {
641 +               int scr = __retu_read_reg(retu, RETU_REG_ADCSCR);
642 +               int ch = (__retu_read_reg(retu, RETU_REG_ADCR) >> 10) & 0xf;
643 +               if (((scr & 0xff) != 0) && (ch != 8))
644 +                       __retu_write_reg(retu, RETU_REG_ADCSCR, (scr & ~0xff));
645 +       }
646 +
647 +       /* Select the channel and read result */
648 +       __retu_write_reg(retu, RETU_REG_ADCR, channel << 10);
649 +       res = __retu_read_reg(retu, RETU_REG_ADCR) & 0x3ff;
650 +
651 +       if (retu->is_vilma)
652 +               __retu_write_reg(retu, RETU_REG_ADCR, (1 << 13));
653 +
654 +       /* Unlock retu */
655 +       mutex_unlock(&retu->mutex);
656 +
657 +       return res;
658 +}
659 +EXPORT_SYMBOL_GPL(retu_read_adc);
660 +
661 +static irqreturn_t retu_irq_handler(int irq, void *_retu)
662 +{
663 +       struct retu             *retu = _retu;
664 +
665 +       u16                     idr;
666 +       u16                     imr;
667 +
668 +       mutex_lock(&retu->mutex);
669 +       idr = __retu_read_reg(retu, RETU_REG_IDR);
670 +       imr = __retu_read_reg(retu, RETU_REG_IMR);
671 +       mutex_unlock(&retu->mutex);
672 +
673 +       idr &= ~imr;
674 +       if (!idr) {
675 +               dev_vdbg(retu->dev, "No IRQ, spurious?\n");
676 +               return IRQ_NONE;
677 +       }
678 +
679 +       while (idr) {
680 +               unsigned long   pending = __ffs(idr);
681 +               unsigned int    irq;
682 +
683 +               idr &= ~BIT(pending);
684 +               irq = pending + retu->irq_base;
685 +               handle_nested_irq(irq);
686 +       }
687 +
688 +       return IRQ_HANDLED;
689 +}
690 +
691 +/* -------------------------------------------------------------------------- */
692 +
693 +static void retu_irq_mask(struct irq_data *data)
694 +{
695 +       struct retu             *retu = irq_data_get_irq_chip_data(data);
696 +       int                     irq = data->irq;
697 +
698 +       retu->mask |= (1 << (irq - retu->irq_base));
699 +       retu->mask_pending = true;
700 +}
701 +
702 +static void retu_irq_unmask(struct irq_data *data)
703 +{
704 +       struct retu             *retu = irq_data_get_irq_chip_data(data);
705 +       int                     irq = data->irq;
706 +
707 +       retu->mask &= ~(1 << (irq - retu->irq_base));
708 +       retu->mask_pending = true;
709 +
710 +}
711 +
712 +static void retu_irq_ack(struct irq_data *data)
713 +{
714 +       struct retu             *retu = irq_data_get_irq_chip_data(data);
715 +       int                     irq = data->irq;
716 +
717 +       retu->ack |= (1 << (irq - retu->irq_base));
718 +       retu->ack_pending = true;
719 +}
720 +
721 +static void retu_bus_lock(struct irq_data *data)
722 +{
723 +       struct retu             *retu = irq_data_get_irq_chip_data(data);
724 +
725 +       mutex_lock(&retu->mutex);
726 +}
727 +
728 +static void retu_bus_sync_unlock(struct irq_data *data)
729 +{
730 +       struct retu             *retu = irq_data_get_irq_chip_data(data);
731 +
732 +       if (retu->mask_pending) {
733 +               __retu_write_reg(retu, RETU_REG_IMR, retu->mask);
734 +               retu->mask_pending = false;
735 +       }
736 +
737 +       if (retu->ack_pending) {
738 +               __retu_write_reg(retu, RETU_REG_IDR, retu->ack);
739 +               retu->ack_pending = false;
740 +       }
741 +
742 +       mutex_unlock(&retu->mutex);
743 +}
744 +
745 +static struct irq_chip retu_irq_chip = {
746 +       .name                   = "retu",
747 +       .irq_bus_lock           = retu_bus_lock,
748 +       .irq_bus_sync_unlock    = retu_bus_sync_unlock,
749 +       .irq_mask               = retu_irq_mask,
750 +       .irq_unmask             = retu_irq_unmask,
751 +       .irq_ack                = retu_irq_ack,
752 +};
753 +
754 +static inline void retu_irq_setup(int irq)
755 +{
756 +#ifdef CONFIG_ARM
757 +       set_irq_flags(irq, IRQF_VALID);
758 +#else
759 +       irq_set_noprobe(irq);
760 +#endif
761 +}
762 +
763 +static void retu_irq_init(struct retu *retu)
764 +{
765 +       int                     base = retu->irq_base;
766 +       int                     end = retu->irq_end;
767 +       int                     irq;
768 +
769 +       for (irq = base; irq < end; irq++) {
770 +               irq_set_chip_data(irq, retu);
771 +               irq_set_chip_and_handler(irq, &retu_irq_chip,
772 +                               handle_simple_irq);
773 +               irq_set_nested_thread(irq, 1);
774 +               retu_irq_setup(irq);
775 +       }
776 +}
777 +
778 +static void retu_irq_exit(struct retu *retu)
779 +{
780 +       int                     base = retu->irq_base;
781 +       int                     end = retu->irq_end;
782 +       int                     irq;
783 +
784 +       for (irq = base; irq < end; irq++) {
785 +#ifdef CONFIG_ARM
786 +               set_irq_flags(irq, 0);
787 +#endif
788 +               irq_set_chip_and_handler(irq, NULL, NULL);
789 +               irq_set_chip_data(irq, NULL);
790 +       }
791 +}
792 +
793 +/* -------------------------------------------------------------------------- */
794 +
795 +/**
796 + * retu_power_off - Shut down power to system
797 + *
798 + * This function puts the system in power off state
799 + */
800 +static void retu_power_off(void)
801 +{
802 +       struct retu             *retu = the_retu;
803 +       unsigned                reg;
804 +
805 +       reg = __retu_read_reg(retu, RETU_REG_CC1);
806 +
807 +       /* Ignore power button state */
808 +       __retu_write_reg(retu, RETU_REG_CC1, reg | 2);
809 +       /* Expire watchdog immediately */
810 +       __retu_write_reg(retu, RETU_REG_WATCHDOG, 0);
811 +       /* Wait for poweroff*/
812 +       for (;;);
813 +}
814 +
815 +static struct resource generic_resources[] = {
816 +       {
817 +               .start  = -EINVAL,      /* fixed later */
818 +               .flags  = IORESOURCE_IRQ,
819 +       },
820 +       {
821 +               .start  = -EINVAL,      /* fixed later */
822 +               .flags  = IORESOURCE_IRQ,
823 +       },
824 +};
825 +
826 +/**
827 + * retu_allocate_child - Allocates one Retu child
828 + * @name: name of new child
829 + * @parent: parent device for this child
830 + */
831 +static struct device *retu_allocate_child(char *name, struct device *parent,
832 +               int irq_base, int irq1, int irq2, int num)
833 +{
834 +       struct platform_device          *pdev;
835 +       int                             status;
836 +
837 +       pdev = platform_device_alloc(name, -1);
838 +       if (!pdev) {
839 +               dev_dbg(parent, "can't allocate %s\n", name);
840 +               goto err;
841 +       }
842 +
843 +       pdev->dev.parent = parent;
844 +
845 +       if (num) {
846 +               generic_resources[0].start = irq_base + irq1;
847 +               generic_resources[1].start = irq_base + irq2;
848 +
849 +               status = platform_device_add_resources(pdev,
850 +                               generic_resources, num);
851 +               if (status < 0) {
852 +                       dev_dbg(parent, "can't add resources to %s\n", name);
853 +                       goto err;
854 +               }
855 +       }
856 +
857 +       status = platform_device_add(pdev);
858 +       if (status < 0) {
859 +               dev_dbg(parent, "can't add %s\n", name);
860 +               goto err;
861 +       }
862 +
863 +       return &pdev->dev;
864 +
865 +err:
866 +       platform_device_put(pdev);
867 +
868 +       return NULL;
869 +}
870 +
871 +/**
872 + * retu_allocate_children - Allocates Retu's children
873 + */
874 +static int retu_allocate_children(struct device *parent, int irq_base)
875 +{
876 +       struct device   *child;
877 +
878 +       child = retu_allocate_child("retu-pwrbutton", parent, irq_base,
879 +                       RETU_INT_PWR, -1, 1);
880 +       if (!child)
881 +               return -ENOMEM;
882 +
883 +       child = retu_allocate_child("retu-headset", parent, irq_base,
884 +                       RETU_INT_HOOK, -1, 1);
885 +       if (!child)
886 +               return -ENOMEM;
887 +
888 +       child = retu_allocate_child("retu-rtc", parent, irq_base,
889 +                       RETU_INT_RTCS, RETU_INT_RTCA, 2);
890 +       if (!child)
891 +               return -ENOMEM;
892 +
893 +       child = retu_allocate_child("retu-wdt", parent, -1, -1, -1, 0);
894 +       if (!child)
895 +               return -ENOMEM;
896 +
897 +       return 0;
898 +}
899 +
900 +/**
901 + * retu_probe - Probe for Retu ASIC
902 + * @dev: the Retu device
903 + *
904 + * Probe for the Retu ASIC and allocate memory
905 + * for its device-struct if found
906 + */
907 +static int __devinit retu_probe(struct platform_device *pdev)
908 +{
909 +       struct retu     *retu;
910 +       struct cbus_retu_platform_data *pdata = pdev->dev.platform_data;
911 +
912 +       int             ret = -ENOMEM;
913 +       int             rev;
914 +
915 +       retu = kzalloc(sizeof(*retu), GFP_KERNEL);
916 +       if (!retu) {
917 +               dev_err(&pdev->dev, "not enough memory\n");
918 +               goto err0;
919 +       }
920 +
921 +       platform_set_drvdata(pdev, retu);
922 +
923 +       ret = irq_alloc_descs(-1, 0, MAX_RETU_IRQ_HANDLERS, 0);
924 +       if (ret < 0) {
925 +               dev_err(&pdev->dev, "failed to allocate IRQ descs\n");
926 +               goto err1;
927 +       }
928 +
929 +       retu->irq       = platform_get_irq(pdev, 0);
930 +       retu->irq_base  = ret;
931 +       retu->irq_end   = ret + MAX_RETU_IRQ_HANDLERS;
932 +       retu->devid     = pdata->devid;
933 +       retu->dev       = &pdev->dev;
934 +       the_retu        = retu;
935 +
936 +       mutex_init(&retu->mutex);
937 +
938 +       retu_irq_init(retu);
939 +
940 +       rev = __retu_read_reg(retu, RETU_REG_ASICR) & 0xff;
941 +       if (rev & (1 << 7))
942 +               retu->is_vilma = true;
943 +
944 +       dev_info(&pdev->dev, "%s v%d.%d found\n",
945 +                       retu->is_vilma ? "Vilma" : "Retu",
946 +                       (rev >> 4) & 0x07, rev & 0x0f);
947 +
948 +       /* Mask all RETU interrupts */
949 +       __retu_write_reg(retu, RETU_REG_IMR, 0xffff);
950 +
951 +       ret = request_threaded_irq(retu->irq, NULL, retu_irq_handler,
952 +                       IRQF_ONESHOT, "retu", retu);
953 +       if (ret < 0) {
954 +               dev_err(&pdev->dev, "Unable to register IRQ handler\n");
955 +               goto err2;
956 +       }
957 +
958 +       irq_set_irq_wake(retu->irq, 1);
959 +
960 +       /* Register power off function */
961 +       pm_power_off = retu_power_off;
962 +
963 +       ret = retu_allocate_children(&pdev->dev, retu->irq_base);
964 +       if (ret < 0) {
965 +               dev_err(&pdev->dev, "Unable to allocate Retu children\n");
966 +               goto err3;
967 +       }
968 +
969 +       return 0;
970 +
971 +err3:
972 +       pm_power_off = NULL;
973 +       free_irq(retu->irq, retu);
974 +
975 +err2:
976 +       retu_irq_exit(retu);
977 +       irq_free_descs(retu->irq_base, MAX_RETU_IRQ_HANDLERS);
978 +
979 +err1:
980 +       kfree(retu);
981 +       the_retu = NULL;
982 +
983 +err0:
984 +       return ret;
985 +}
986 +
987 +static int __devexit retu_remove(struct platform_device *pdev)
988 +{
989 +       struct retu             *retu = platform_get_drvdata(pdev);
990 +
991 +       pm_power_off = NULL;
992 +       the_retu = NULL;
993 +
994 +       free_irq(retu->irq, retu);
995 +       retu_irq_exit(retu);
996 +       irq_free_descs(retu->irq_base, MAX_RETU_IRQ_HANDLERS);
997 +       kfree(retu);
998 +
999 +       return 0;
1000 +}
1001 +
1002 +static struct platform_driver retu_driver = {
1003 +       .probe          = retu_probe,
1004 +       .remove         = __devexit_p(retu_remove),
1005 +       .driver         = {
1006 +               .name   = "retu",
1007 +       },
1008 +};
1009 +
1010 +static int __init retu_init(void)
1011 +{
1012 +       return platform_driver_register(&retu_driver);
1013 +}
1014 +subsys_initcall(retu_init);
1015 +
1016 +static void __exit retu_exit(void)
1017 +{
1018 +       platform_driver_unregister(&retu_driver);
1019 +}
1020 +module_exit(retu_exit);
1021 +
1022 +MODULE_DESCRIPTION("Retu ASIC control");
1023 +MODULE_LICENSE("GPL");
1024 +MODULE_AUTHOR("Juha Yrjölä");
1025 +MODULE_AUTHOR("David Weinehall");
1026 +MODULE_AUTHOR("Mikko Ylinen");
1027 --- /dev/null
1028 +++ b/drivers/cbus/retu.h
1029 @@ -0,0 +1,85 @@
1030 +/**
1031 + * drivers/cbus/retu.h
1032 + *
1033 + * Copyright (C) 2004, 2005 Nokia Corporation
1034 + *
1035 + * Written by Juha Yrjölä <juha.yrjola@nokia.com> and
1036 + *           David Weinehall <david.weinehall@nokia.com>
1037 + *
1038 + * This file is subject to the terms and conditions of the GNU General
1039 + * Public License. See the file "COPYING" in the main directory of this
1040 + * archive for more details.
1041 + *
1042 + * This program is distributed in the hope that it will be useful,
1043 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1044 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1045 + * GNU General Public License for more details.
1046 +
1047 + * You should have received a copy of the GNU General Public License
1048 + * along with this program; if not, write to the Free Software
1049 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1050 + */
1051 +
1052 +#ifndef __DRIVERS_CBUS_RETU_H
1053 +#define __DRIVERS_CBUS_RETU_H
1054 +
1055 +#include <linux/types.h>
1056 +
1057 +/* Registers */
1058 +#define RETU_REG_ASICR         0x00    /* ASIC ID & revision */
1059 +#define RETU_REG_IDR           0x01    /* Interrupt ID */
1060 +#define RETU_REG_IMR           0x02    /* Interrupt mask */
1061 +#define RETU_REG_RTCDSR                0x03    /* RTC seconds register */
1062 +#define RETU_REG_RTCHMR                0x04    /* RTC hours and minutes register */
1063 +#define RETU_REG_RTCHMAR       0x05    /* RTC hours and minutes alarm and time set register */
1064 +#define RETU_REG_RTCCALR       0x06    /* RTC calibration register */
1065 +#define RETU_REG_ADCR          0x08    /* ADC result */
1066 +#define RETU_REG_ADCSCR                0x09    /* ADC sample ctrl */
1067 +#define RETU_REG_CC1           0x0d    /* Common control register 1 */
1068 +#define RETU_REG_CC2           0x0e    /* Common control register 2 */
1069 +#define RETU_REG_CTRL_CLR      0x0f    /* Regulator clear register */
1070 +#define RETU_REG_CTRL_SET      0x10    /* Regulator set register */
1071 +#define RETU_REG_STATUS                0x16    /* Status register */
1072 +#define  RETU_REG_STATUS_BATAVAIL      0x0100 /* Battery available */
1073 +#define  RETU_REG_STATUS_CHGPLUG       0x1000 /* Charger is plugged in */
1074 +#define RETU_REG_WATCHDOG      0x17    /* Watchdog register */
1075 +#define RETU_REG_AUDTXR                0x18    /* Audio Codec Tx register */
1076 +#define RETU_REG_MAX           0x1f
1077 +
1078 +/* Interrupt sources */
1079 +#define RETU_INT_PWR           0
1080 +#define RETU_INT_CHAR          1
1081 +#define RETU_INT_RTCS          2
1082 +#define RETU_INT_RTCM          3
1083 +#define RETU_INT_RTCD          4
1084 +#define RETU_INT_RTCA          5
1085 +#define RETU_INT_HOOK          6
1086 +#define RETU_INT_HEAD          7
1087 +#define RETU_INT_ADCS          8
1088 +
1089 +#define        MAX_RETU_IRQ_HANDLERS   16
1090 +
1091 +/* ADC channels */
1092 +#define RETU_ADC_GND           0x00 /* Ground */
1093 +#define RETU_ADC_BSI           0x01 /* Battery Size Indicator */
1094 +#define RETU_ADC_BATTEMP       0x02 /* Battery temperature */
1095 +#define RETU_ADC_CHGVOLT       0x03 /* Charger voltage */
1096 +#define RETU_ADC_HEADSET       0x04 /* Headset detection */
1097 +#define RETU_ADC_HOOKDET       0x05 /* Hook detection */
1098 +#define RETU_ADC_RFGP          0x06 /* RF GP */
1099 +#define RETU_ADC_WBTX          0x07 /* Wideband Tx detection */
1100 +#define RETU_ADC_BATTVOLT      0x08 /* Battery voltage measurement */
1101 +#define RETU_ADC_GND2          0x09 /* Ground */
1102 +#define RETU_ADC_LIGHTSENS     0x0A /* Light sensor */
1103 +#define RETU_ADC_LIGHTTEMP     0x0B /* Light sensor temperature */
1104 +#define RETU_ADC_BKUPVOLT      0x0C /* Backup battery voltage */
1105 +#define RETU_ADC_TEMP          0x0D /* RETU temperature */
1106 +
1107 +
1108 +int retu_read_reg(struct device *child, unsigned reg);
1109 +void retu_write_reg(struct device *child, unsigned reg, u16 val);
1110 +void retu_set_clear_reg_bits(struct device *child, unsigned reg, u16 set,
1111 +               u16 clear);
1112 +int retu_read_adc(struct device *child, int channel);
1113 +
1114 +#endif /* __DRIVERS_CBUS_RETU_H */
1115 --- /dev/null
1116 +++ b/drivers/cbus/retu-headset.c
1117 @@ -0,0 +1,359 @@
1118 +/**
1119 + * Retu/Vilma headset detection
1120 + *
1121 + * Copyright (C) 2006 Nokia Corporation
1122 + *
1123 + * Written by Juha Yrjölä
1124 + *
1125 + * This file is subject to the terms and conditions of the GNU General
1126 + * Public License. See the file "COPYING" in the main directory of this
1127 + * archive for more details.
1128 + *
1129 + * This program is distributed in the hope that it will be useful,
1130 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1131 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1132 + * GNU General Public License for more details.
1133 + *
1134 + * You should have received a copy of the GNU General Public License
1135 + * along with this program; if not, write to the Free Software
1136 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1137 + */
1138 +
1139 +#include <linux/module.h>
1140 +#include <linux/init.h>
1141 +#include <linux/kernel.h>
1142 +#include <linux/irq.h>
1143 +#include <linux/interrupt.h>
1144 +#include <linux/slab.h>
1145 +#include <linux/delay.h>
1146 +#include <linux/input.h>
1147 +#include <linux/platform_device.h>
1148 +
1149 +#include "retu.h"
1150 +
1151 +#define RETU_ADC_CHANNEL_HOOKDET       0x05
1152 +
1153 +#define RETU_HEADSET_KEY               KEY_PHONE
1154 +
1155 +struct retu_headset {
1156 +       spinlock_t                      lock;
1157 +       struct mutex                    mutex;
1158 +       struct device                   *dev;
1159 +       struct input_dev                *idev;
1160 +       unsigned                        bias_enabled;
1161 +       unsigned                        detection_enabled;
1162 +       unsigned                        pressed;
1163 +       struct timer_list               enable_timer;
1164 +       struct timer_list               detect_timer;
1165 +       int                             irq;
1166 +};
1167 +
1168 +static void retu_headset_set_bias(struct retu_headset *hs, int enable)
1169 +{
1170 +       if (enable) {
1171 +               retu_set_clear_reg_bits(hs->dev, RETU_REG_AUDTXR,
1172 +                                       (1 << 0) | (1 << 1), 0);
1173 +               msleep(2);
1174 +               retu_set_clear_reg_bits(hs->dev, RETU_REG_AUDTXR,
1175 +                               1 << 3, 0);
1176 +       } else {
1177 +               retu_set_clear_reg_bits(hs->dev, RETU_REG_AUDTXR, 0,
1178 +                                       (1 << 0) | (1 << 1) | (1 << 3));
1179 +       }
1180 +}
1181 +
1182 +static void retu_headset_enable(struct retu_headset *hs)
1183 +{
1184 +       mutex_lock(&hs->mutex);
1185 +       if (!hs->bias_enabled) {
1186 +               hs->bias_enabled = 1;
1187 +               retu_headset_set_bias(hs, 1);
1188 +       }
1189 +       mutex_unlock(&hs->mutex);
1190 +}
1191 +
1192 +static void retu_headset_disable(struct retu_headset *hs)
1193 +{
1194 +       mutex_lock(&hs->mutex);
1195 +       if (hs->bias_enabled) {
1196 +               hs->bias_enabled = 0;
1197 +               retu_headset_set_bias(hs, 0);
1198 +       }
1199 +       mutex_unlock(&hs->mutex);
1200 +}
1201 +
1202 +static void retu_headset_det_enable(struct retu_headset *hs)
1203 +{
1204 +       mutex_lock(&hs->mutex);
1205 +       if (!hs->detection_enabled) {
1206 +               hs->detection_enabled = 1;
1207 +               retu_set_clear_reg_bits(hs->dev, RETU_REG_CC1,
1208 +                               (1 << 10) | (1 << 8), 0);
1209 +       }
1210 +       mutex_unlock(&hs->mutex);
1211 +}
1212 +
1213 +static void retu_headset_det_disable(struct retu_headset *hs)
1214 +{
1215 +       unsigned long flags;
1216 +
1217 +       mutex_lock(&hs->mutex);
1218 +       if (hs->detection_enabled) {
1219 +               hs->detection_enabled = 0;
1220 +               del_timer_sync(&hs->enable_timer);
1221 +               del_timer_sync(&hs->detect_timer);
1222 +               spin_lock_irqsave(&hs->lock, flags);
1223 +               if (hs->pressed)
1224 +                       input_report_key(hs->idev, RETU_HEADSET_KEY, 0);
1225 +               spin_unlock_irqrestore(&hs->lock, flags);
1226 +               retu_set_clear_reg_bits(hs->dev, RETU_REG_CC1, 0,
1227 +                               (1 << 10) | (1 << 8));
1228 +       }
1229 +       mutex_unlock(&hs->mutex);
1230 +}
1231 +
1232 +static ssize_t retu_headset_hookdet_show(struct device *dev,
1233 +                                        struct device_attribute *attr,
1234 +                                        char *buf)
1235 +{
1236 +       int val;
1237 +
1238 +       val = retu_read_adc(dev, RETU_ADC_CHANNEL_HOOKDET);
1239 +       return sprintf(buf, "%d\n", val);
1240 +}
1241 +
1242 +static DEVICE_ATTR(hookdet, S_IRUGO, retu_headset_hookdet_show, NULL);
1243 +
1244 +static ssize_t retu_headset_enable_show(struct device *dev,
1245 +                                       struct device_attribute *attr,
1246 +                                       char *buf)
1247 +{
1248 +       struct retu_headset *hs = dev_get_drvdata(dev);
1249 +
1250 +       return sprintf(buf, "%u\n", hs->bias_enabled);
1251 +}
1252 +
1253 +static ssize_t retu_headset_enable_store(struct device *dev,
1254 +                                        struct device_attribute *attr,
1255 +                                        const char *buf, size_t count)
1256 +{
1257 +       struct retu_headset *hs = dev_get_drvdata(dev);
1258 +       int enable;
1259 +
1260 +       if (sscanf(buf, "%u", &enable) != 1)
1261 +               return -EINVAL;
1262 +       if (enable)
1263 +               retu_headset_enable(hs);
1264 +       else
1265 +               retu_headset_disable(hs);
1266 +       return count;
1267 +}
1268 +
1269 +static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
1270 +                  retu_headset_enable_show, retu_headset_enable_store);
1271 +
1272 +static ssize_t retu_headset_enable_det_show(struct device *dev,
1273 +                                           struct device_attribute *attr,
1274 +                                           char *buf)
1275 +{
1276 +       struct retu_headset *hs = dev_get_drvdata(dev);
1277 +
1278 +       return sprintf(buf, "%u\n", hs->detection_enabled);
1279 +}
1280 +
1281 +static ssize_t retu_headset_enable_det_store(struct device *dev,
1282 +                                            struct device_attribute *attr,
1283 +                                            const char *buf, size_t count)
1284 +{
1285 +       struct retu_headset *hs = dev_get_drvdata(dev);
1286 +       int enable;
1287 +
1288 +       if (sscanf(buf, "%u", &enable) != 1)
1289 +               return -EINVAL;
1290 +       if (enable)
1291 +               retu_headset_det_enable(hs);
1292 +       else
1293 +               retu_headset_det_disable(hs);
1294 +       return count;
1295 +}
1296 +
1297 +static DEVICE_ATTR(enable_det, S_IRUGO | S_IWUSR | S_IWGRP,
1298 +                  retu_headset_enable_det_show,
1299 +                  retu_headset_enable_det_store);
1300 +
1301 +static irqreturn_t retu_headset_hook_interrupt(int irq, void *_hs)
1302 +{
1303 +       struct retu_headset     *hs = _hs;
1304 +       unsigned long           flags;
1305 +
1306 +       spin_lock_irqsave(&hs->lock, flags);
1307 +       if (!hs->pressed) {
1308 +               /* Headset button was just pressed down. */
1309 +               hs->pressed = 1;
1310 +               input_report_key(hs->idev, RETU_HEADSET_KEY, 1);
1311 +       }
1312 +       spin_unlock_irqrestore(&hs->lock, flags);
1313 +       retu_set_clear_reg_bits(hs->dev, RETU_REG_CC1, 0,
1314 +                       (1 << 10) | (1 << 8));
1315 +       mod_timer(&hs->enable_timer, jiffies + msecs_to_jiffies(50));
1316 +
1317 +       return IRQ_HANDLED;
1318 +}
1319 +
1320 +static void retu_headset_enable_timer(unsigned long arg)
1321 +{
1322 +       struct retu_headset *hs = (struct retu_headset *) arg;
1323 +
1324 +       retu_set_clear_reg_bits(hs->dev, RETU_REG_CC1,
1325 +                       (1 << 10) | (1 << 8), 0);
1326 +       mod_timer(&hs->detect_timer, jiffies + msecs_to_jiffies(350));
1327 +}
1328 +
1329 +static void retu_headset_detect_timer(unsigned long arg)
1330 +{
1331 +       struct retu_headset *hs = (struct retu_headset *) arg;
1332 +       unsigned long flags;
1333 +
1334 +       spin_lock_irqsave(&hs->lock, flags);
1335 +       if (hs->pressed) {
1336 +               hs->pressed = 0;
1337 +               input_report_key(hs->idev, RETU_HEADSET_KEY, 0);
1338 +       }
1339 +       spin_unlock_irqrestore(&hs->lock, flags);
1340 +}
1341 +
1342 +static int __init retu_headset_probe(struct platform_device *pdev)
1343 +{
1344 +       struct retu_headset *hs;
1345 +       int irq;
1346 +       int r;
1347 +
1348 +       hs = kzalloc(sizeof(*hs), GFP_KERNEL);
1349 +       if (hs == NULL)
1350 +               return -ENOMEM;
1351 +
1352 +       hs->dev = &pdev->dev;
1353 +
1354 +       hs->idev = input_allocate_device();
1355 +       if (hs->idev == NULL) {
1356 +               r = -ENOMEM;
1357 +               goto err1;
1358 +       }
1359 +       hs->idev->name = "retu-headset";
1360 +       hs->idev->dev.parent = &pdev->dev;
1361 +       set_bit(EV_KEY, hs->idev->evbit);
1362 +       set_bit(RETU_HEADSET_KEY, hs->idev->keybit);
1363 +       r = input_register_device(hs->idev);
1364 +       if (r < 0)
1365 +               goto err2;
1366 +
1367 +       r = device_create_file(&pdev->dev, &dev_attr_hookdet);
1368 +       if (r < 0)
1369 +               goto err3;
1370 +       r = device_create_file(&pdev->dev, &dev_attr_enable);
1371 +       if (r < 0)
1372 +               goto err4;
1373 +       r = device_create_file(&pdev->dev, &dev_attr_enable_det);
1374 +       if (r < 0)
1375 +               goto err5;
1376 +       platform_set_drvdata(pdev, hs);
1377 +
1378 +       spin_lock_init(&hs->lock);
1379 +       mutex_init(&hs->mutex);
1380 +       setup_timer(&hs->enable_timer, retu_headset_enable_timer,
1381 +                   (unsigned long) hs);
1382 +       setup_timer(&hs->detect_timer, retu_headset_detect_timer,
1383 +                   (unsigned long) hs);
1384 +
1385 +       irq = platform_get_irq(pdev, 0);
1386 +       hs->irq = irq;
1387 +
1388 +       r = request_threaded_irq(irq, NULL, retu_headset_hook_interrupt, 0,
1389 +                       "hookdet", hs);
1390 +       if (r != 0) {
1391 +               dev_err(&pdev->dev, "hookdet IRQ not available\n");
1392 +               goto err6;
1393 +       }
1394 +
1395 +       return 0;
1396 +err6:
1397 +       device_remove_file(&pdev->dev, &dev_attr_enable_det);
1398 +err5:
1399 +       device_remove_file(&pdev->dev, &dev_attr_enable);
1400 +err4:
1401 +       device_remove_file(&pdev->dev, &dev_attr_hookdet);
1402 +err3:
1403 +       input_unregister_device(hs->idev);
1404 +err2:
1405 +       input_free_device(hs->idev);
1406 +err1:
1407 +       kfree(hs);
1408 +       return r;
1409 +}
1410 +
1411 +static int retu_headset_remove(struct platform_device *pdev)
1412 +{
1413 +       struct retu_headset *hs = platform_get_drvdata(pdev);
1414 +
1415 +       device_remove_file(&pdev->dev, &dev_attr_hookdet);
1416 +       device_remove_file(&pdev->dev, &dev_attr_enable);
1417 +       device_remove_file(&pdev->dev, &dev_attr_enable_det);
1418 +       retu_headset_disable(hs);
1419 +       retu_headset_det_disable(hs);
1420 +       free_irq(hs->irq, hs);
1421 +       input_unregister_device(hs->idev);
1422 +       input_free_device(hs->idev);
1423 +
1424 +       return 0;
1425 +}
1426 +
1427 +static int retu_headset_suspend(struct platform_device *pdev,
1428 +                               pm_message_t mesg)
1429 +{
1430 +       struct retu_headset *hs = platform_get_drvdata(pdev);
1431 +
1432 +       mutex_lock(&hs->mutex);
1433 +       if (hs->bias_enabled)
1434 +               retu_headset_set_bias(hs, 0);
1435 +       mutex_unlock(&hs->mutex);
1436 +
1437 +       return 0;
1438 +}
1439 +
1440 +static int retu_headset_resume(struct platform_device *pdev)
1441 +{
1442 +       struct retu_headset *hs = platform_get_drvdata(pdev);
1443 +
1444 +       mutex_lock(&hs->mutex);
1445 +       if (hs->bias_enabled)
1446 +               retu_headset_set_bias(hs, 1);
1447 +       mutex_unlock(&hs->mutex);
1448 +
1449 +       return 0;
1450 +}
1451 +
1452 +static struct platform_driver retu_headset_driver = {
1453 +       .remove         = retu_headset_remove,
1454 +       .suspend        = retu_headset_suspend,
1455 +       .resume         = retu_headset_resume,
1456 +       .driver         = {
1457 +               .name   = "retu-headset",
1458 +       },
1459 +};
1460 +
1461 +static int __init retu_headset_init(void)
1462 +{
1463 +       return platform_driver_probe(&retu_headset_driver, retu_headset_probe);
1464 +}
1465 +
1466 +static void __exit retu_headset_exit(void)
1467 +{
1468 +       platform_driver_unregister(&retu_headset_driver);
1469 +}
1470 +
1471 +module_init(retu_headset_init);
1472 +module_exit(retu_headset_exit);
1473 +
1474 +MODULE_DESCRIPTION("Retu/Vilma headset detection");
1475 +MODULE_LICENSE("GPL");
1476 +MODULE_AUTHOR("Juha Yrjölä");
1477 --- /dev/null
1478 +++ b/drivers/cbus/retu-pwrbutton.c
1479 @@ -0,0 +1,165 @@
1480 +/**
1481 + * drivers/cbus/retu-pwrbutton.c
1482 + *
1483 + * Driver for sending retu power button event to input-layer
1484 + *
1485 + * Copyright (C) 2004-2010 Nokia Corporation
1486 + *
1487 + * Written by
1488 + *     Ari Saastamoinen <ari.saastamoinen@elektrobit.com>
1489 + *     Juha Yrjola <juha.yrjola@solidboot.com>
1490 + *
1491 + * Contact: Felipe Balbi <felipe.balbi@nokia.com>
1492 + *
1493 + * This file is subject to the terms and conditions of the GNU General
1494 + * Public License. See the file "COPYING" in the main directory of this
1495 + * archive for more details.
1496 + *
1497 + * This program is distributed in the hope that it will be useful,
1498 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1499 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1500 + * GNU General Public License for more details.
1501 + *
1502 + * You should have received a copy of the GNU General Public License
1503 + * along with this program; if not, write to the Free Software
1504 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1505 + */
1506 +
1507 +#include <linux/module.h>
1508 +#include <linux/init.h>
1509 +#include <linux/kernel.h>
1510 +#include <linux/errno.h>
1511 +#include <linux/input.h>
1512 +#include <linux/jiffies.h>
1513 +#include <linux/bitops.h>
1514 +#include <linux/irq.h>
1515 +#include <linux/interrupt.h>
1516 +#include <linux/platform_device.h>
1517 +#include <linux/slab.h>
1518 +
1519 +#include "retu.h"
1520 +
1521 +#define RETU_STATUS_PWRONX     (1 << 5)
1522 +
1523 +#define PWRBTN_DELAY           20
1524 +#define PWRBTN_UP              0
1525 +#define PWRBTN_PRESSED         1
1526 +
1527 +struct retu_pwrbutton {
1528 +       struct input_dev        *idev;
1529 +       struct device           *dev;
1530 +
1531 +       int                     state;
1532 +       int                     irq;
1533 +};
1534 +
1535 +static irqreturn_t retubutton_irq(int irq, void *_pwr)
1536 +{
1537 +       struct retu_pwrbutton *pwr = _pwr;
1538 +       int state;
1539 +
1540 +       if (retu_read_reg(pwr->dev, RETU_REG_STATUS) & RETU_STATUS_PWRONX)
1541 +               state = PWRBTN_UP;
1542 +       else
1543 +               state = PWRBTN_PRESSED;
1544 +
1545 +       if (pwr->state != state) {
1546 +               input_report_key(pwr->idev, KEY_POWER, state);
1547 +               input_sync(pwr->idev);
1548 +               pwr->state = state;
1549 +       }
1550 +
1551 +       return IRQ_HANDLED;
1552 +}
1553 +
1554 +static int __init retubutton_probe(struct platform_device *pdev)
1555 +{
1556 +       struct retu_pwrbutton           *pwr;
1557 +       int                             ret = 0;
1558 +
1559 +       pwr = kzalloc(sizeof(*pwr), GFP_KERNEL);
1560 +       if (!pwr) {
1561 +               dev_err(&pdev->dev, "not enough memory\n");
1562 +               ret = -ENOMEM;
1563 +               goto err0;
1564 +       }
1565 +
1566 +       pwr->dev = &pdev->dev;
1567 +       pwr->irq = platform_get_irq(pdev, 0);
1568 +       platform_set_drvdata(pdev, pwr);
1569 +
1570 +       ret = request_threaded_irq(pwr->irq, NULL, retubutton_irq, 0,
1571 +                       "retu-pwrbutton", pwr);
1572 +       if (ret < 0) {
1573 +               dev_err(&pdev->dev, "Cannot allocate irq\n");
1574 +               goto err1;
1575 +       }
1576 +
1577 +       pwr->idev = input_allocate_device();
1578 +       if (!pwr->idev) {
1579 +               dev_err(&pdev->dev, "can't allocate input device\n");
1580 +               ret = -ENOMEM;
1581 +               goto err2;
1582 +       }
1583 +
1584 +       pwr->idev->evbit[0] = BIT_MASK(EV_KEY);
1585 +       pwr->idev->keybit[BIT_WORD(KEY_POWER)] = BIT_MASK(KEY_POWER);
1586 +       pwr->idev->name = "retu-pwrbutton";
1587 +
1588 +       ret = input_register_device(pwr->idev);
1589 +       if (ret < 0) {
1590 +               dev_err(&pdev->dev, "failed to register input device\n");
1591 +               goto err3;
1592 +       }
1593 +
1594 +       return 0;
1595 +
1596 +err3:
1597 +       input_free_device(pwr->idev);
1598 +
1599 +err2:
1600 +       free_irq(pwr->irq, pwr);
1601 +
1602 +err1:
1603 +       kfree(pwr);
1604 +
1605 +err0:
1606 +       return ret;
1607 +}
1608 +
1609 +static int __exit retubutton_remove(struct platform_device *pdev)
1610 +{
1611 +       struct retu_pwrbutton           *pwr = platform_get_drvdata(pdev);
1612 +
1613 +       free_irq(pwr->irq, pwr);
1614 +       input_unregister_device(pwr->idev);
1615 +       input_free_device(pwr->idev);
1616 +       kfree(pwr);
1617 +
1618 +       return 0;
1619 +}
1620 +
1621 +static struct platform_driver retu_pwrbutton_driver = {
1622 +       .remove         = __exit_p(retubutton_remove),
1623 +       .driver         = {
1624 +               .name   = "retu-pwrbutton",
1625 +       },
1626 +};
1627 +
1628 +static int __init retubutton_init(void)
1629 +{
1630 +       return platform_driver_probe(&retu_pwrbutton_driver, retubutton_probe);
1631 +}
1632 +module_init(retubutton_init);
1633 +
1634 +static void __exit retubutton_exit(void)
1635 +{
1636 +       platform_driver_unregister(&retu_pwrbutton_driver);
1637 +}
1638 +module_exit(retubutton_exit);
1639 +
1640 +MODULE_DESCRIPTION("Retu Power Button");
1641 +MODULE_LICENSE("GPL");
1642 +MODULE_AUTHOR("Ari Saastamoinen");
1643 +MODULE_AUTHOR("Felipe Balbi <felipe.balbi@nokia.com>");
1644 +
1645 --- /dev/null
1646 +++ b/drivers/cbus/retu-rtc.c
1647 @@ -0,0 +1,287 @@
1648 +/**
1649 + * drivers/cbus/retu-rtc.c
1650 + *
1651 + * Support for Retu RTC
1652 + *
1653 + * Copyright (C) 2004, 2005 Nokia Corporation
1654 + *
1655 + * Written by Paul Mundt <paul.mundt@nokia.com> and
1656 + *            Igor Stoppa <igor.stoppa@nokia.com>
1657 + *
1658 + * The Retu RTC is essentially a partial read-only RTC that gives us Retu's
1659 + * idea of what time actually is. It's left as a userspace excercise to map
1660 + * this back to time in the real world and ensure that calibration settings
1661 + * are sane to compensate for any horrible drift (on account of not being able
1662 + * to set the clock to anything).
1663 + *
1664 + * Days are semi-writeable. Namely, Retu will only track 255 days for us
1665 + * consecutively, after which the counter is explicitly stuck at 255 until
1666 + * someone comes along and clears it with a write. In the event that no one
1667 + * comes along and clears it, we no longer have any idea what day it is.
1668 + *
1669 + * This file is subject to the terms and conditions of the GNU General
1670 + * Public License. See the file "COPYING" in the main directory of this
1671 + * archive for more details.
1672 + *
1673 + * This program is distributed in the hope that it will be useful,
1674 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1675 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1676 + * GNU General Public License for more details.
1677 + *
1678 + * You should have received a copy of the GNU General Public License
1679 + * along with this program; if not, write to the Free Software
1680 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1681 + */
1682 +
1683 +#include <linux/device.h>
1684 +#include <linux/init.h>
1685 +#include <linux/kernel.h>
1686 +#include <linux/slab.h>
1687 +#include <linux/module.h>
1688 +#include <linux/platform_device.h>
1689 +#include <linux/mutex.h>
1690 +#include <linux/rtc.h>
1691 +
1692 +#include "cbus.h"
1693 +#include "retu.h"
1694 +
1695 +struct retu_rtc {
1696 +       /* device lock */
1697 +       struct mutex            mutex;
1698 +       struct device           *dev;
1699 +       struct rtc_device       *rtc;
1700 +
1701 +       u16                     alarm_expired;
1702 +       int                     irq_rtcs;
1703 +       int                     irq_rtca;
1704 +};
1705 +
1706 +static void retu_rtc_do_reset(struct retu_rtc *rtc)
1707 +{
1708 +       u16 ccr1;
1709 +
1710 +       ccr1 = retu_read_reg(rtc->dev, RETU_REG_CC1);
1711 +       /* RTC in reset */
1712 +       retu_write_reg(rtc->dev, RETU_REG_CC1, ccr1 | 0x0001);
1713 +       /* RTC in normal operating mode */
1714 +       retu_write_reg(rtc->dev, RETU_REG_CC1, ccr1 & ~0x0001);
1715 +
1716 +       /* Disable alarm and RTC WD */
1717 +       retu_write_reg(rtc->dev, RETU_REG_RTCHMAR, 0x7f3f);
1718 +       /* Set Calibration register to default value */
1719 +       retu_write_reg(rtc->dev, RETU_REG_RTCCALR, 0x00c0);
1720 +
1721 +       rtc->alarm_expired = 0;
1722 +}
1723 +
1724 +static irqreturn_t retu_rtc_interrupt(int irq, void *_rtc)
1725 +{
1726 +       struct retu_rtc         *rtc = _rtc;
1727 +
1728 +       mutex_lock(&rtc->mutex);
1729 +       rtc->alarm_expired = 1;
1730 +       retu_write_reg(rtc->dev, RETU_REG_RTCHMAR, (24 << 8) | 60);
1731 +       mutex_unlock(&rtc->mutex);
1732 +
1733 +       return IRQ_HANDLED;
1734 +}
1735 +
1736 +static int retu_rtc_init_irq(struct retu_rtc *rtc)
1737 +{
1738 +       int irq;
1739 +       int ret;
1740 +
1741 +       irq = platform_get_irq(to_platform_device(rtc->dev), 0);
1742 +       rtc->irq_rtcs = irq;
1743 +
1744 +       irq = platform_get_irq(to_platform_device(rtc->dev), 1);
1745 +       rtc->irq_rtca = irq;
1746 +
1747 +       ret = request_threaded_irq(rtc->irq_rtcs, NULL, retu_rtc_interrupt,
1748 +                       0, "RTCS", rtc);
1749 +       if (ret != 0)
1750 +               return ret;
1751 +
1752 +       ret = request_threaded_irq(rtc->irq_rtca, NULL, retu_rtc_interrupt,
1753 +                       0, "RTCA", rtc);
1754 +       if (ret != 0) {
1755 +               free_irq(rtc->irq_rtcs, rtc);
1756 +               return ret;
1757 +       }
1758 +
1759 +       return 0;
1760 +}
1761 +
1762 +static int retu_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
1763 +{
1764 +       struct retu_rtc         *rtc = dev_get_drvdata(dev);
1765 +       u16                     chmar;
1766 +
1767 +       mutex_lock(&rtc->mutex);
1768 +
1769 +       chmar = ((alm->time.tm_hour & 0x1f) << 8) | (alm->time.tm_min & 0x3f);
1770 +       retu_write_reg(rtc->dev, RETU_REG_RTCHMAR, chmar);
1771 +
1772 +       mutex_unlock(&rtc->mutex);
1773 +
1774 +       return 0;
1775 +}
1776 +
1777 +static int retu_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
1778 +{
1779 +       struct retu_rtc         *rtc = dev_get_drvdata(dev);
1780 +       u16                     chmar;
1781 +
1782 +       mutex_lock(&rtc->mutex);
1783 +
1784 +       chmar = retu_read_reg(rtc->dev, RETU_REG_RTCHMAR);
1785 +
1786 +       alm->time.tm_hour       = (chmar >> 8) & 0x1f;
1787 +       alm->time.tm_min        = chmar & 0x3f;
1788 +       alm->enabled            = !!rtc->alarm_expired;
1789 +
1790 +       mutex_unlock(&rtc->mutex);
1791 +
1792 +       return 0;
1793 +}
1794 +
1795 +static int retu_rtc_set_time(struct device *dev, struct rtc_time *tm)
1796 +{
1797 +       struct retu_rtc         *rtc = dev_get_drvdata(dev);
1798 +       u16                     dsr;
1799 +       u16                     hmr;
1800 +
1801 +       dsr = ((tm->tm_mday & 0xff) << 8) | (tm->tm_hour & 0xff);
1802 +       hmr = ((tm->tm_min & 0xff) << 8) | (tm->tm_sec & 0xff);
1803 +
1804 +       mutex_lock(&rtc->mutex);
1805 +
1806 +       retu_write_reg(rtc->dev, RETU_REG_RTCDSR, dsr);
1807 +       retu_write_reg(rtc->dev, RETU_REG_RTCHMR, hmr);
1808 +
1809 +       mutex_unlock(&rtc->mutex);
1810 +
1811 +       return 0;
1812 +}
1813 +
1814 +static int retu_rtc_read_time(struct device *dev, struct rtc_time *tm)
1815 +{
1816 +       struct retu_rtc         *rtc = dev_get_drvdata(dev);
1817 +       u16                     dsr;
1818 +       u16                     hmr;
1819 +
1820 +       /*
1821 +        * DSR holds days and hours
1822 +        * HMR hols minutes and seconds
1823 +        *
1824 +        * both are 16 bit registers with 8-bit for each field.
1825 +        */
1826 +
1827 +       mutex_lock(&rtc->mutex);
1828 +
1829 +       dsr     = retu_read_reg(rtc->dev, RETU_REG_RTCDSR);
1830 +       hmr     = retu_read_reg(rtc->dev, RETU_REG_RTCHMR);
1831 +
1832 +       tm->tm_sec      = hmr & 0xff;
1833 +       tm->tm_min      = hmr >> 8;
1834 +       tm->tm_hour     = dsr & 0xff;
1835 +       tm->tm_mday     = dsr >> 8;
1836 +
1837 +       mutex_unlock(&rtc->mutex);
1838 +
1839 +       return 0;
1840 +}
1841 +
1842 +static struct rtc_class_ops retu_rtc_ops = {
1843 +       .read_time              = retu_rtc_read_time,
1844 +       .set_time               = retu_rtc_set_time,
1845 +       .read_alarm             = retu_rtc_read_alarm,
1846 +       .set_alarm              = retu_rtc_set_alarm,
1847 +};
1848 +
1849 +static int __init retu_rtc_probe(struct platform_device *pdev)
1850 +{
1851 +       struct retu_rtc         *rtc;
1852 +       int                     r;
1853 +
1854 +       rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
1855 +       if (!rtc) {
1856 +               dev_err(&pdev->dev, "not enough memory\n");
1857 +               r = -ENOMEM;
1858 +               goto err0;
1859 +       }
1860 +
1861 +       rtc->dev = &pdev->dev;
1862 +       platform_set_drvdata(pdev, rtc);
1863 +       mutex_init(&rtc->mutex);
1864 +
1865 +       rtc->alarm_expired = retu_read_reg(rtc->dev, RETU_REG_IDR) &
1866 +               (0x1 << RETU_INT_RTCA);
1867 +
1868 +       r = retu_rtc_init_irq(rtc);
1869 +       if (r < 0) {
1870 +               dev_err(&pdev->dev, "failed to request retu irq\n");
1871 +               goto err1;
1872 +       }
1873 +
1874 +       /* If the calibration register is zero, we've probably lost power */
1875 +       if (!(retu_read_reg(rtc->dev, RETU_REG_RTCCALR) & 0x00ff))
1876 +               retu_rtc_do_reset(rtc);
1877 +
1878 +       rtc->rtc = rtc_device_register(pdev->name, &pdev->dev, &
1879 +                       retu_rtc_ops, THIS_MODULE);
1880 +       if (IS_ERR(rtc->rtc)) {
1881 +               dev_err(&pdev->dev, "can't register RTC device\n");
1882 +               goto err2;
1883 +       }
1884 +
1885 +       return 0;
1886 +
1887 +err2:
1888 +       free_irq(rtc->irq_rtcs, rtc);
1889 +       free_irq(rtc->irq_rtca, rtc);
1890 +
1891 +err1:
1892 +       kfree(rtc);
1893 +
1894 +err0:
1895 +       return r;
1896 +}
1897 +
1898 +static int __devexit retu_rtc_remove(struct platform_device *pdev)
1899 +{
1900 +       struct retu_rtc         *rtc = platform_get_drvdata(pdev);
1901 +
1902 +       free_irq(rtc->irq_rtcs, rtc);
1903 +       free_irq(rtc->irq_rtca, rtc);
1904 +       rtc_device_unregister(rtc->rtc);
1905 +       kfree(rtc);
1906 +
1907 +       return 0;
1908 +}
1909 +
1910 +static struct platform_driver retu_rtc_driver = {
1911 +       .remove         = __exit_p(retu_rtc_remove),
1912 +       .driver         = {
1913 +               .name   = "retu-rtc",
1914 +       },
1915 +};
1916 +
1917 +static int __init retu_rtc_init(void)
1918 +{
1919 +       return platform_driver_probe(&retu_rtc_driver, retu_rtc_probe);
1920 +}
1921 +module_init(retu_rtc_init);
1922 +
1923 +static void __exit retu_rtc_exit(void)
1924 +{
1925 +       platform_driver_unregister(&retu_rtc_driver);
1926 +}
1927 +module_exit(retu_rtc_exit);
1928 +
1929 +MODULE_DESCRIPTION("Retu RTC");
1930 +MODULE_LICENSE("GPL");
1931 +MODULE_AUTHOR("Paul Mundt");
1932 +MODULE_AUTHOR("Igor Stoppa");
1933 +MODULE_AUTHOR("Felipe Balbi <felipe.balbi@nokia.com>");
1934 +
1935 --- /dev/null
1936 +++ b/drivers/cbus/retu-wdt.c
1937 @@ -0,0 +1,272 @@
1938 +/**
1939 + * drivers/cbus/retu-wdt.c
1940 + *
1941 + * Driver for Retu watchdog
1942 + *
1943 + * Copyright (C) 2004, 2005 Nokia Corporation
1944 + *
1945 + * Written by Amit Kucheria <amit.kucheria@nokia.com>
1946 + *
1947 + * Cleanups by Michael Buesch <mb@bu3sch.de> (C) 2011
1948 + *
1949 + * This file is subject to the terms and conditions of the GNU General
1950 + * Public License. See the file "COPYING" in the main directory of this
1951 + * archive for more details.
1952 + *
1953 + * This program is distributed in the hope that it will be useful,
1954 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1955 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1956 + * GNU General Public License for more details.
1957 + *
1958 + * You should have received a copy of the GNU General Public License
1959 + * along with this program; if not, write to the Free Software
1960 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1961 + */
1962 +
1963 +#include <linux/kernel.h>
1964 +#include <linux/slab.h>
1965 +#include <linux/module.h>
1966 +#include <linux/device.h>
1967 +#include <linux/init.h>
1968 +#include <linux/fs.h>
1969 +#include <linux/io.h>
1970 +#include <linux/platform_device.h>
1971 +
1972 +#include <linux/completion.h>
1973 +#include <linux/errno.h>
1974 +#include <linux/moduleparam.h>
1975 +#include <linux/miscdevice.h>
1976 +#include <linux/watchdog.h>
1977 +
1978 +#include <asm/uaccess.h>
1979 +
1980 +#include <plat/prcm.h>
1981 +
1982 +#include "cbus.h"
1983 +#include "retu.h"
1984 +
1985 +/* Watchdog timeout in seconds */
1986 +#define RETU_WDT_MIN_TIMER 0
1987 +#define RETU_WDT_DEFAULT_TIMER 32
1988 +#define RETU_WDT_MAX_TIMER 63
1989 +
1990 +struct retu_wdt_dev {
1991 +       struct device           *dev;
1992 +       unsigned int            period_val;     /* Current period of watchdog */
1993 +       unsigned long           users;
1994 +       struct miscdevice       miscdev;
1995 +       struct delayed_work     ping_work;
1996 +       struct mutex            mutex;
1997 +};
1998 +
1999 +
2000 +static inline void _retu_modify_counter(struct retu_wdt_dev *wdev,
2001 +                                       unsigned int new)
2002 +{
2003 +       retu_write_reg(wdev->dev, RETU_REG_WATCHDOG, (u16)new);
2004 +}
2005 +
2006 +static int retu_modify_counter(struct retu_wdt_dev *wdev, unsigned int new)
2007 +{
2008 +       if (new < RETU_WDT_MIN_TIMER || new > RETU_WDT_MAX_TIMER)
2009 +               return -EINVAL;
2010 +
2011 +       mutex_lock(&wdev->mutex);
2012 +       wdev->period_val = new;
2013 +       _retu_modify_counter(wdev, wdev->period_val);
2014 +       mutex_unlock(&wdev->mutex);
2015 +
2016 +       return 0;
2017 +}
2018 +
2019 +/*
2020 + * Since retu watchdog cannot be disabled in hardware, we must kick it
2021 + * with a timer until userspace watchdog software takes over. Do this
2022 + * unless /dev/watchdog is open or CONFIG_WATCHDOG_NOWAYOUT is set.
2023 + */
2024 +static void retu_wdt_ping_enable(struct retu_wdt_dev *wdev)
2025 +{
2026 +       _retu_modify_counter(wdev, RETU_WDT_MAX_TIMER);
2027 +       schedule_delayed_work(&wdev->ping_work,
2028 +                             round_jiffies_relative(RETU_WDT_DEFAULT_TIMER * HZ));
2029 +}
2030 +
2031 +static void retu_wdt_ping_disable(struct retu_wdt_dev *wdev)
2032 +{
2033 +       _retu_modify_counter(wdev, RETU_WDT_MAX_TIMER);
2034 +       cancel_delayed_work_sync(&wdev->ping_work);
2035 +}
2036 +
2037 +static void retu_wdt_ping_work(struct work_struct *work)
2038 +{
2039 +       struct retu_wdt_dev *wdev = container_of(to_delayed_work(work),
2040 +                                       struct retu_wdt_dev, ping_work);
2041 +       retu_wdt_ping_enable(wdev);
2042 +}
2043 +
2044 +static int retu_wdt_open(struct inode *inode, struct file *file)
2045 +{
2046 +       struct miscdevice *mdev = file->private_data;
2047 +       struct retu_wdt_dev *wdev = container_of(mdev, struct retu_wdt_dev, miscdev);
2048 +
2049 +       if (test_and_set_bit(0, &wdev->users))
2050 +               return -EBUSY;
2051 +
2052 +       retu_wdt_ping_disable(wdev);
2053 +
2054 +       return nonseekable_open(inode, file);
2055 +}
2056 +
2057 +static int retu_wdt_release(struct inode *inode, struct file *file)
2058 +{
2059 +       struct miscdevice *mdev = file->private_data;
2060 +       struct retu_wdt_dev *wdev = container_of(mdev, struct retu_wdt_dev, miscdev);
2061 +
2062 +#ifndef CONFIG_WATCHDOG_NOWAYOUT
2063 +       retu_wdt_ping_enable(wdev);
2064 +#endif
2065 +       clear_bit(0, &wdev->users);
2066 +
2067 +       return 0;
2068 +}
2069 +
2070 +static ssize_t retu_wdt_write(struct file *file, const char __user *data,
2071 +                                               size_t len, loff_t *ppos)
2072 +{
2073 +       struct miscdevice *mdev = file->private_data;
2074 +       struct retu_wdt_dev *wdev = container_of(mdev, struct retu_wdt_dev, miscdev);
2075 +
2076 +       if (len)
2077 +               retu_modify_counter(wdev, RETU_WDT_MAX_TIMER);
2078 +
2079 +       return len;
2080 +}
2081 +
2082 +static long retu_wdt_ioctl(struct file *file, unsigned int cmd,
2083 +                          unsigned long arg)
2084 +{
2085 +       struct miscdevice *mdev = file->private_data;
2086 +       struct retu_wdt_dev *wdev = container_of(mdev, struct retu_wdt_dev, miscdev);
2087 +       int new_margin;
2088 +
2089 +       static const struct watchdog_info ident = {
2090 +               .identity = "Retu Watchdog",
2091 +               .options = WDIOF_SETTIMEOUT,
2092 +               .firmware_version = 0,
2093 +       };
2094 +
2095 +       switch (cmd) {
2096 +       default:
2097 +               return -ENOTTY;
2098 +       case WDIOC_GETSUPPORT:
2099 +               return copy_to_user((struct watchdog_info __user *)arg, &ident,
2100 +                                                       sizeof(ident));
2101 +       case WDIOC_GETSTATUS:
2102 +               return put_user(0, (int __user *)arg);
2103 +       case WDIOC_GETBOOTSTATUS:
2104 +               if (cpu_is_omap16xx())
2105 +                       return put_user(omap_readw(ARM_SYSST),
2106 +                                       (int __user *)arg);
2107 +               if (cpu_is_omap24xx())
2108 +                       return put_user(omap_prcm_get_reset_sources(),
2109 +                                       (int __user *)arg);
2110 +       case WDIOC_KEEPALIVE:
2111 +               retu_modify_counter(wdev, RETU_WDT_MAX_TIMER);
2112 +               break;
2113 +       case WDIOC_SETTIMEOUT:
2114 +               if (get_user(new_margin, (int __user *)arg))
2115 +                       return -EFAULT;
2116 +               retu_modify_counter(wdev, new_margin);
2117 +               /* Fall through */
2118 +       case WDIOC_GETTIMEOUT:
2119 +               return put_user(wdev->period_val, (int __user *)arg);
2120 +       }
2121 +
2122 +       return 0;
2123 +}
2124 +
2125 +static const struct file_operations retu_wdt_fops = {
2126 +       .owner          = THIS_MODULE,
2127 +       .write          = retu_wdt_write,
2128 +       .unlocked_ioctl = retu_wdt_ioctl,
2129 +       .open           = retu_wdt_open,
2130 +       .release        = retu_wdt_release,
2131 +};
2132 +
2133 +static int __init retu_wdt_probe(struct platform_device *pdev)
2134 +{
2135 +       struct retu_wdt_dev *wdev;
2136 +       int ret;
2137 +
2138 +       wdev = kzalloc(sizeof(struct retu_wdt_dev), GFP_KERNEL);
2139 +       if (!wdev)
2140 +               return -ENOMEM;
2141 +
2142 +       wdev->dev = &pdev->dev;
2143 +       wdev->period_val = RETU_WDT_DEFAULT_TIMER;
2144 +       mutex_init(&wdev->mutex);
2145 +
2146 +       platform_set_drvdata(pdev, wdev);
2147 +
2148 +       wdev->miscdev.parent = &pdev->dev;
2149 +       wdev->miscdev.minor = WATCHDOG_MINOR;
2150 +       wdev->miscdev.name = "watchdog";
2151 +       wdev->miscdev.fops = &retu_wdt_fops;
2152 +
2153 +       ret = misc_register(&wdev->miscdev);
2154 +       if (ret)
2155 +               goto err_free_wdev;
2156 +
2157 +       INIT_DELAYED_WORK(&wdev->ping_work, retu_wdt_ping_work);
2158 +
2159 +       /* Kick the watchdog for kernel booting to finish.
2160 +        * If nowayout is not set, we start the ping work. */
2161 +#ifdef CONFIG_WATCHDOG_NOWAYOUT
2162 +       retu_modify_counter(wdev, RETU_WDT_MAX_TIMER);
2163 +#else
2164 +       retu_wdt_ping_enable(wdev);
2165 +#endif
2166 +
2167 +       return 0;
2168 +
2169 +err_free_wdev:
2170 +       kfree(wdev);
2171 +
2172 +       return ret;
2173 +}
2174 +
2175 +static int __devexit retu_wdt_remove(struct platform_device *pdev)
2176 +{
2177 +       struct retu_wdt_dev *wdev;
2178 +
2179 +       wdev = platform_get_drvdata(pdev);
2180 +       misc_deregister(&wdev->miscdev);
2181 +       cancel_delayed_work_sync(&wdev->ping_work);
2182 +       kfree(wdev);
2183 +
2184 +       return 0;
2185 +}
2186 +
2187 +static struct platform_driver retu_wdt_driver = {
2188 +       .remove         = __exit_p(retu_wdt_remove),
2189 +       .driver         = {
2190 +               .name   = "retu-wdt",
2191 +       },
2192 +};
2193 +
2194 +static int __init retu_wdt_init(void)
2195 +{
2196 +       return platform_driver_probe(&retu_wdt_driver, retu_wdt_probe);
2197 +}
2198 +
2199 +static void __exit retu_wdt_exit(void)
2200 +{
2201 +       platform_driver_unregister(&retu_wdt_driver);
2202 +}
2203 +
2204 +module_init(retu_wdt_init);
2205 +module_exit(retu_wdt_exit);
2206 +
2207 +MODULE_DESCRIPTION("Retu WatchDog");
2208 +MODULE_AUTHOR("Amit Kucheria");
2209 +MODULE_LICENSE("GPL");
2210 --- /dev/null
2211 +++ b/drivers/cbus/tahvo.c
2212 @@ -0,0 +1,423 @@
2213 +/**
2214 + * drivers/cbus/tahvo.c
2215 + *
2216 + * Support functions for Tahvo ASIC
2217 + *
2218 + * Copyright (C) 2004, 2005 Nokia Corporation
2219 + *
2220 + * Written by Juha Yrjölä <juha.yrjola@nokia.com>,
2221 + *           David Weinehall <david.weinehall@nokia.com>, and
2222 + *           Mikko Ylinen <mikko.k.ylinen@nokia.com>
2223 + *
2224 + * This file is subject to the terms and conditions of the GNU General
2225 + * Public License. See the file "COPYING" in the main directory of this
2226 + * archive for more details.
2227 + *
2228 + * This program is distributed in the hope that it will be useful,
2229 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2230 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2231 + * GNU General Public License for more details.
2232 + *
2233 + * You should have received a copy of the GNU General Public License
2234 + * along with this program; if not, write to the Free Software
2235 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
2236 + */
2237 +
2238 +#include <linux/module.h>
2239 +#include <linux/init.h>
2240 +
2241 +#include <linux/slab.h>
2242 +#include <linux/kernel.h>
2243 +#include <linux/errno.h>
2244 +#include <linux/device.h>
2245 +#include <linux/irq.h>
2246 +#include <linux/interrupt.h>
2247 +#include <linux/platform_device.h>
2248 +#include <linux/platform_data/cbus.h>
2249 +#include <linux/mutex.h>
2250 +
2251 +#include "cbus.h"
2252 +#include "tahvo.h"
2253 +
2254 +struct tahvo {
2255 +       /* device lock */
2256 +       struct mutex    mutex;
2257 +       struct device   *dev;
2258 +
2259 +       int             irq_base;
2260 +       int             irq_end;
2261 +       int             irq;
2262 +
2263 +       int             ack;
2264 +       int             mask;
2265 +
2266 +       unsigned int    mask_pending:1;
2267 +       unsigned int    ack_pending:1;
2268 +       unsigned int    is_betty:1;
2269 +};
2270 +
2271 +/**
2272 + * __tahvo_read_reg - Reads a value from a register in Tahvo
2273 + * @tahvo: pointer to tahvo structure
2274 + * @reg: the register address to read from
2275 + */
2276 +static int __tahvo_read_reg(struct tahvo *tahvo, unsigned reg)
2277 +{
2278 +       return cbus_read_reg(tahvo->dev, CBUS_TAHVO_DEVICE_ID, reg);
2279 +}
2280 +
2281 +/**
2282 + * __tahvo_write_reg - Writes a value to a register in Tahvo
2283 + * @tahvo: pointer to tahvo structure
2284 + * @reg: register address to write to
2285 + * @val: the value to be written to @reg
2286 + */
2287 +static void __tahvo_write_reg(struct tahvo *tahvo, unsigned reg, u16 val)
2288 +{
2289 +       cbus_write_reg(tahvo->dev, CBUS_TAHVO_DEVICE_ID, reg, val);
2290 +}
2291 +
2292 +/**
2293 + * tahvo_read_reg - Read a value from a register in Tahvo
2294 + * @child: device pointer from the calling child
2295 + * @reg: the register to read from
2296 + *
2297 + * This function returns the contents of the specified register
2298 + */
2299 +int tahvo_read_reg(struct device *child, unsigned reg)
2300 +{
2301 +       struct tahvo            *tahvo = dev_get_drvdata(child->parent);
2302 +
2303 +       return __tahvo_read_reg(tahvo, reg);
2304 +}
2305 +EXPORT_SYMBOL(tahvo_read_reg);
2306 +
2307 +/**
2308 + * tahvo_write_reg - Write a value to a register in Tahvo
2309 + * @child: device pointer from the calling child
2310 + * @reg: the register to write to
2311 + * @val : the value to write to the register
2312 + *
2313 + * This function writes a value to the specified register
2314 + */
2315 +void tahvo_write_reg(struct device *child, unsigned reg, u16 val)
2316 +{
2317 +       struct tahvo            *tahvo = dev_get_drvdata(child->parent);
2318 +
2319 +       __tahvo_write_reg(tahvo, reg, val);
2320 +}
2321 +EXPORT_SYMBOL(tahvo_write_reg);
2322 +
2323 +/**
2324 + * tahvo_set_clear_reg_bits - set and clear register bits atomically
2325 + * @child: device pointer from the calling child
2326 + * @reg: the register to write to
2327 + * @bits: the bits to set
2328 + *
2329 + * This function sets and clears the specified Tahvo register bits atomically
2330 + */
2331 +void tahvo_set_clear_reg_bits(struct device *child, unsigned reg, u16 set,
2332 +               u16 clear)
2333 +{
2334 +       struct tahvo            *tahvo = dev_get_drvdata(child->parent);
2335 +       u16                     w;
2336 +
2337 +       mutex_lock(&tahvo->mutex);
2338 +       w = __tahvo_read_reg(tahvo, reg);
2339 +       w &= ~clear;
2340 +       w |= set;
2341 +       __tahvo_write_reg(tahvo, reg, w);
2342 +       mutex_unlock(&tahvo->mutex);
2343 +}
2344 +
2345 +static irqreturn_t tahvo_irq_handler(int irq, void *_tahvo)
2346 +{
2347 +       struct tahvo            *tahvo = _tahvo;
2348 +       u16                     id;
2349 +       u16                     im;
2350 +
2351 +       id = __tahvo_read_reg(tahvo, TAHVO_REG_IDR);
2352 +       im = __tahvo_read_reg(tahvo, TAHVO_REG_IMR);
2353 +       id &= ~im;
2354 +
2355 +       if (!id) {
2356 +               dev_vdbg(tahvo->dev, "No IRQ, spurious ?\n");
2357 +               return IRQ_NONE;
2358 +       }
2359 +
2360 +       while (id) {
2361 +               unsigned long   pending = __ffs(id);
2362 +               unsigned int    irq;
2363 +
2364 +               id &= ~BIT(pending);
2365 +               irq = pending + tahvo->irq_base;
2366 +               handle_nested_irq(irq);
2367 +       }
2368 +
2369 +       return IRQ_HANDLED;
2370 +}
2371 +
2372 +/* -------------------------------------------------------------------------- */
2373 +
2374 +static void tahvo_irq_bus_lock(struct irq_data *data)
2375 +{
2376 +       struct tahvo            *tahvo = irq_data_get_irq_chip_data(data);
2377 +
2378 +       mutex_lock(&tahvo->mutex);
2379 +}
2380 +
2381 +static void tahvo_irq_bus_sync_unlock(struct irq_data *data)
2382 +{
2383 +       struct tahvo            *tahvo = irq_data_get_irq_chip_data(data);
2384 +
2385 +       if (tahvo->mask_pending) {
2386 +               __tahvo_write_reg(tahvo, TAHVO_REG_IMR, tahvo->mask);
2387 +               tahvo->mask_pending = false;
2388 +       }
2389 +
2390 +       if (tahvo->ack_pending) {
2391 +               __tahvo_write_reg(tahvo, TAHVO_REG_IDR, tahvo->ack);
2392 +               tahvo->ack_pending = false;
2393 +       }
2394 +
2395 +       mutex_unlock(&tahvo->mutex);
2396 +}
2397 +
2398 +static void tahvo_irq_mask(struct irq_data *data)
2399 +{
2400 +       struct tahvo            *tahvo = irq_data_get_irq_chip_data(data);
2401 +       int                     irq = data->irq;
2402 +
2403 +       tahvo->mask |= (1 << (irq - tahvo->irq_base));
2404 +       tahvo->mask_pending = true;
2405 +}
2406 +
2407 +static void tahvo_irq_unmask(struct irq_data *data)
2408 +{
2409 +       struct tahvo            *tahvo = irq_data_get_irq_chip_data(data);
2410 +       int                     irq = data->irq;
2411 +
2412 +       tahvo->mask &= ~(1 << (irq - tahvo->irq_base));
2413 +       tahvo->mask_pending = true;
2414 +}
2415 +
2416 +static void tahvo_irq_ack(struct irq_data *data)
2417 +{
2418 +       struct tahvo            *tahvo = irq_data_get_irq_chip_data(data);
2419 +       int                     irq = data->irq;
2420 +
2421 +       tahvo->ack |= (1 << (irq - tahvo->irq_base));
2422 +       tahvo->ack_pending = true;
2423 +}
2424 +
2425 +static struct irq_chip tahvo_irq_chip = {
2426 +       .name                   = "tahvo",
2427 +       .irq_bus_lock           = tahvo_irq_bus_lock,
2428 +       .irq_bus_sync_unlock    = tahvo_irq_bus_sync_unlock,
2429 +       .irq_mask               = tahvo_irq_mask,
2430 +       .irq_unmask             = tahvo_irq_unmask,
2431 +       .irq_ack                = tahvo_irq_ack,
2432 +};
2433 +
2434 +static inline void tahvo_irq_setup(int irq)
2435 +{
2436 +#ifdef CONFIG_ARM
2437 +       set_irq_flags(irq, IRQF_VALID);
2438 +#else
2439 +       irq_set_noprobe(irq);
2440 +#endif
2441 +}
2442 +
2443 +static void tahvo_irq_init(struct tahvo *tahvo)
2444 +{
2445 +       int                     base = tahvo->irq_base;
2446 +       int                     end = tahvo->irq_end;
2447 +       int                     irq;
2448 +
2449 +       for (irq = base; irq < end; irq++) {
2450 +               irq_set_chip_data(irq, tahvo);
2451 +               irq_set_chip_and_handler(irq, &tahvo_irq_chip,
2452 +                               handle_simple_irq);
2453 +               irq_set_nested_thread(irq, 1);
2454 +               tahvo_irq_setup(irq);
2455 +       }
2456 +}
2457 +
2458 +/* -------------------------------------------------------------------------- */
2459 +
2460 +static struct resource generic_resources[] = {
2461 +       {
2462 +               .start          = -EINVAL,      /* fixed later */
2463 +               .flags          = IORESOURCE_IRQ,
2464 +       },
2465 +};
2466 +
2467 +static struct device *tahvo_allocate_child(const char *name,
2468 +               struct device *parent, int irq)
2469 +{
2470 +       struct platform_device  *pdev;
2471 +       int                     ret;
2472 +
2473 +       pdev = platform_device_alloc(name, -1);
2474 +       if (!pdev) {
2475 +               dev_dbg(parent, "can't allocate %s\n", name);
2476 +               goto err0;
2477 +       }
2478 +
2479 +       pdev->dev.parent = parent;
2480 +
2481 +       if (irq > 0) {
2482 +               generic_resources[0].start = irq;
2483 +
2484 +               ret = platform_device_add_resources(pdev, generic_resources,
2485 +                               ARRAY_SIZE(generic_resources));
2486 +               if (ret < 0) {
2487 +                       dev_dbg(parent, "can't add resources to %s\n", name);
2488 +                       goto err1;
2489 +               }
2490 +       }
2491 +
2492 +       ret = platform_device_add(pdev);
2493 +       if (ret < 0) {
2494 +               dev_dbg(parent, "can't add %s\n", name);
2495 +               goto err1;
2496 +       }
2497 +
2498 +       return &pdev->dev;
2499 +
2500 +err1:
2501 +       platform_device_put(pdev);
2502 +
2503 +err0:
2504 +       return NULL;
2505 +}
2506 +
2507 +static int tahvo_allocate_children(struct device *parent, int irq_base)
2508 +{
2509 +       struct device           *child;
2510 +
2511 +       child = tahvo_allocate_child("tahvo-usb", parent,
2512 +                       irq_base + TAHVO_INT_VBUSON);
2513 +       if (!child)
2514 +               return -ENOMEM;
2515 +
2516 +       child = tahvo_allocate_child("tahvo-pwm", parent, -1);
2517 +       if (!child)
2518 +               return -ENOMEM;
2519 +
2520 +       return 0;
2521 +}
2522 +
2523 +static int __devinit tahvo_probe(struct platform_device *pdev)
2524 +{
2525 +       struct tahvo            *tahvo;
2526 +       int                     rev;
2527 +       int                     ret;
2528 +       int                     irq;
2529 +       int                     id;
2530 +
2531 +       tahvo = kzalloc(sizeof(*tahvo), GFP_KERNEL);
2532 +       if (!tahvo) {
2533 +               dev_err(&pdev->dev, "not enough memory\n");
2534 +               ret = -ENOMEM;
2535 +               goto err0;
2536 +       }
2537 +
2538 +       irq = platform_get_irq(pdev, 0);
2539 +       platform_set_drvdata(pdev, tahvo);
2540 +
2541 +       mutex_init(&tahvo->mutex);
2542 +
2543 +       ret = irq_alloc_descs(-1, 0, MAX_TAHVO_IRQ_HANDLERS, 0);
2544 +       if (ret < 0) {
2545 +               dev_err(&pdev->dev, "failed to allocate IRQ descs\n");
2546 +               goto err1;
2547 +       }
2548 +
2549 +       tahvo->irq_base = ret;
2550 +       tahvo->irq_end  = ret + MAX_TAHVO_IRQ_HANDLERS;
2551 +       tahvo->dev      = &pdev->dev;
2552 +       tahvo->irq      = irq;
2553 +
2554 +       tahvo_irq_init(tahvo);
2555 +
2556 +       rev = __tahvo_read_reg(tahvo, TAHVO_REG_ASICR);
2557 +
2558 +       id = (rev >> 8) & 0xff;
2559 +
2560 +       if (id == 0x0b)
2561 +               tahvo->is_betty = true;
2562 +
2563 +       ret = tahvo_allocate_children(&pdev->dev, tahvo->irq_base);
2564 +       if (ret < 0) {
2565 +               dev_err(&pdev->dev, "failed to allocate children\n");
2566 +               goto err2;
2567 +       }
2568 +
2569 +       dev_err(&pdev->dev, "%s v%d.%d found\n",
2570 +                       tahvo->is_betty ? "Betty" : "Tahvo",
2571 +                       (rev >> 4) & 0x0f, rev & 0x0f);
2572 +
2573 +       /* Mask all TAHVO interrupts */
2574 +       __tahvo_write_reg(tahvo, TAHVO_REG_IMR, 0xffff);
2575 +
2576 +       ret = request_threaded_irq(irq, NULL, tahvo_irq_handler,
2577 +                       IRQF_TRIGGER_RISING | IRQF_ONESHOT,
2578 +                       "tahvo", tahvo);
2579 +       if (ret < 0) {
2580 +               dev_err(&pdev->dev, "Unable to register IRQ handler\n");
2581 +               goto err2;
2582 +       }
2583 +
2584 +       return 0;
2585 +
2586 +err2:
2587 +       irq_free_descs(tahvo->irq_base, MAX_TAHVO_IRQ_HANDLERS);
2588 +
2589 +err1:
2590 +       kfree(tahvo);
2591 +
2592 +err0:
2593 +       return ret;
2594 +}
2595 +
2596 +static int __devexit tahvo_remove(struct platform_device *pdev)
2597 +{
2598 +       struct tahvo            *tahvo = platform_get_drvdata(pdev);
2599 +       int                     irq;
2600 +
2601 +       irq = platform_get_irq(pdev, 0);
2602 +
2603 +       free_irq(irq, 0);
2604 +       irq_free_descs(tahvo->irq_base, MAX_TAHVO_IRQ_HANDLERS);
2605 +       kfree(tahvo);
2606 +
2607 +       return 0;
2608 +}
2609 +
2610 +static struct platform_driver tahvo_driver = {
2611 +       .probe          = tahvo_probe,
2612 +       .remove         = __devexit_p(tahvo_remove),
2613 +       .driver         = {
2614 +               .name   = "tahvo",
2615 +       },
2616 +};
2617 +
2618 +static int __init tahvo_init(void)
2619 +{
2620 +       return platform_driver_register(&tahvo_driver);
2621 +}
2622 +subsys_initcall(tahvo_init);
2623 +
2624 +static void __exit tahvo_exit(void)
2625 +{
2626 +       platform_driver_unregister(&tahvo_driver);
2627 +}
2628 +module_exit(tahvo_exit);
2629 +
2630 +MODULE_DESCRIPTION("Tahvo ASIC control");
2631 +MODULE_LICENSE("GPL");
2632 +MODULE_AUTHOR("Juha Yrjölä");
2633 +MODULE_AUTHOR("David Weinehall");
2634 +MODULE_AUTHOR("Mikko Ylinen");
2635 +
2636 --- /dev/null
2637 +++ b/drivers/cbus/tahvo.h
2638 @@ -0,0 +1,58 @@
2639 +/*
2640 + * drivers/cbus/tahvo.h
2641 + *
2642 + * Copyright (C) 2004, 2005 Nokia Corporation
2643 + *
2644 + * Written by Juha Yrjölä <juha.yrjola@nokia.com> and
2645 + *           David Weinehall <david.weinehall@nokia.com>
2646 + *
2647 + * This file is subject to the terms and conditions of the GNU General
2648 + * Public License. See the file "COPYING" in the main directory of this
2649 + * archive for more details.
2650 + *
2651 + * This program is distributed in the hope that it will be useful,
2652 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2653 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2654 + * GNU General Public License for more details.
2655 +
2656 + * You should have received a copy of the GNU General Public License
2657 + * along with this program; if not, write to the Free Software
2658 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
2659 + */
2660 +
2661 +#ifndef __DRIVERS_CBUS_TAHVO_H
2662 +#define __DRIVERS_CBUS_TAHVO_H
2663 +
2664 +#include <linux/types.h>
2665 +
2666 +/* Registers */
2667 +#define TAHVO_REG_ASICR                0x00    /* ASIC ID & revision */
2668 +#define TAHVO_REG_IDR          0x01    /* Interrupt ID */
2669 +#define TAHVO_REG_IDSR         0x02    /* Interrupt status */
2670 +#define TAHVO_REG_IMR          0x03    /* Interrupt mask */
2671 +#define TAHVO_REG_CHGCURR      0x04    /* Charge current control PWM (8-bit) */
2672 +#define TAHVO_REG_LEDPWMR      0x05    /* LED PWM */
2673 +#define TAHVO_REG_USBR         0x06    /* USB control */
2674 +#define TAHVO_REG_CHGCTL       0x08    /* Charge control register */
2675 +#define  TAHVO_REG_CHGCTL_EN           0x0001  /* Global charge enable */
2676 +#define  TAHVO_REG_CHGCTL_PWMOVR       0x0004  /* PWM override. Force charge PWM to 0%/100% duty cycle. */
2677 +#define  TAHVO_REG_CHGCTL_PWMOVRZERO   0x0008  /* If set, PWM override is 0% (If unset -> 100%) */
2678 +#define  TAHVO_REG_CHGCTL_CURMEAS      0x0040  /* Enable battery current measurement. */
2679 +#define  TAHVO_REG_CHGCTL_CURTIMRST    0x0080  /* Current measure timer reset. */
2680 +#define TAHVO_REG_BATCURRTIMER 0x0c    /* Battery current measure timer (8-bit) */
2681 +#define TAHVO_REG_BATCURR      0x0d    /* Battery (dis)charge current (signed 16-bit) */
2682 +
2683 +#define TAHVO_REG_MAX          0x0d
2684 +
2685 +/* Interrupt sources */
2686 +#define TAHVO_INT_VBUSON       0
2687 +#define TAHVO_INT_BATCURR      7 /* Battery current measure timer */
2688 +
2689 +#define MAX_TAHVO_IRQ_HANDLERS 8
2690 +
2691 +int tahvo_read_reg(struct device *child, unsigned reg);
2692 +void tahvo_write_reg(struct device *child, unsigned reg, u16 val);
2693 +void tahvo_set_clear_reg_bits(struct device *child, unsigned reg, u16 set,
2694 +               u16 clear);
2695 +
2696 +#endif /* __DRIVERS_CBUS_TAHVO_H */
2697 --- /dev/null
2698 +++ b/drivers/cbus/tahvo-usb.c
2699 @@ -0,0 +1,740 @@
2700 +/**
2701 + * drivers/cbus/tahvo-usb.c
2702 + *
2703 + * Tahvo USB transeiver
2704 + *
2705 + * Copyright (C) 2005-2006 Nokia Corporation
2706 + *
2707 + * Parts copied from drivers/i2c/chips/isp1301_omap.c
2708 + * Copyright (C) 2004 Texas Instruments
2709 + * Copyright (C) 2004 David Brownell
2710 + *
2711 + * Written by Juha Yrjölä <juha.yrjola@nokia.com>,
2712 + *           Tony Lindgren <tony@atomide.com>, and
2713 + *           Timo Teräs <timo.teras@nokia.com>
2714 + *
2715 + * This file is subject to the terms and conditions of the GNU General
2716 + * Public License. See the file "COPYING" in the main directory of this
2717 + * archive for more details.
2718 + *
2719 + * This program is distributed in the hope that it will be useful,
2720 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2721 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2722 + * GNU General Public License for more details.
2723 + *
2724 + * You should have received a copy of the GNU General Public License
2725 + * along with this program; if not, write to the Free Software
2726 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
2727 + */
2728 +
2729 +#include <linux/kernel.h>
2730 +#include <linux/module.h>
2731 +#include <linux/init.h>
2732 +#include <linux/slab.h>
2733 +#include <linux/io.h>
2734 +#include <linux/interrupt.h>
2735 +#include <linux/platform_device.h>
2736 +#include <linux/usb/ch9.h>
2737 +#include <linux/usb/gadget.h>
2738 +#include <linux/usb.h>
2739 +#include <linux/usb/otg.h>
2740 +#include <linux/i2c.h>
2741 +#include <linux/workqueue.h>
2742 +#include <linux/kobject.h>
2743 +#include <linux/clk.h>
2744 +#include <linux/mutex.h>
2745 +
2746 +#include <asm/irq.h>
2747 +#include <plat/usb.h>
2748 +
2749 +#include "cbus.h"
2750 +#include "tahvo.h"
2751 +
2752 +#define DRIVER_NAME     "tahvo-usb"
2753 +
2754 +#define USBR_SLAVE_CONTROL     (1 << 8)
2755 +#define USBR_VPPVIO_SW         (1 << 7)
2756 +#define USBR_SPEED             (1 << 6)
2757 +#define USBR_REGOUT            (1 << 5)
2758 +#define USBR_MASTER_SW2                (1 << 4)
2759 +#define USBR_MASTER_SW1                (1 << 3)
2760 +#define USBR_SLAVE_SW          (1 << 2)
2761 +#define USBR_NSUSPEND          (1 << 1)
2762 +#define USBR_SEMODE            (1 << 0)
2763 +
2764 +/* bits in OTG_CTRL */
2765 +
2766 +/* Bits that are controlled by OMAP OTG and are read-only */
2767 +#define OTG_CTRL_OMAP_MASK     (OTG_PULLDOWN|OTG_PULLUP|OTG_DRV_VBUS|\
2768 +                               OTG_PD_VBUS|OTG_PU_VBUS|OTG_PU_ID)
2769 +/* Bits that are controlled by transceiver */
2770 +#define OTG_CTRL_XCVR_MASK     (OTG_ASESSVLD|OTG_BSESSEND|\
2771 +                               OTG_BSESSVLD|OTG_VBUSVLD|OTG_ID)
2772 +/* Bits that are controlled by system */
2773 +#define OTG_CTRL_SYS_MASK      (OTG_A_BUSREQ|OTG_A_SETB_HNPEN|OTG_B_BUSREQ|\
2774 +                               OTG_B_HNPEN|OTG_BUSDROP)
2775 +
2776 +#if defined(CONFIG_USB_OHCI_HCD) && !defined(CONFIG_USB_OTG)
2777 +#error tahvo-otg.c does not work with OCHI yet!
2778 +#endif
2779 +
2780 +#define TAHVO_MODE_HOST                0
2781 +#define TAHVO_MODE_PERIPHERAL  1
2782 +
2783 +#ifdef CONFIG_USB_OTG
2784 +#define TAHVO_MODE(tu)         (tu)->tahvo_mode
2785 +#elif defined(CONFIG_USB_GADGET_OMAP)
2786 +#define TAHVO_MODE(tu)         TAHVO_MODE_PERIPHERAL
2787 +#else
2788 +#define TAHVO_MODE(tu)         TAHVO_MODE_HOST
2789 +#endif
2790 +
2791 +struct tahvo_usb {
2792 +       struct device           *dev;
2793 +       struct platform_device *pt_dev;
2794 +       struct otg_transceiver otg;
2795 +       int vbus_state;
2796 +       struct mutex serialize;
2797 +#ifdef CONFIG_USB_OTG
2798 +       int tahvo_mode;
2799 +#endif
2800 +       struct clk *ick;
2801 +
2802 +       int             irq;
2803 +};
2804 +static struct tahvo_usb *tahvo_usb_device;
2805 +
2806 +/*
2807 + * ---------------------------------------------------------------------------
2808 + * OTG related functions
2809 + *
2810 + * These shoud be separated into omap-otg.c driver module, as they are used
2811 + * by various transceivers. These functions are needed in the UDC-only case
2812 + * as well. These functions are copied from GPL isp1301_omap.c
2813 + * ---------------------------------------------------------------------------
2814 + */
2815 +static struct platform_device *tahvo_otg_dev;
2816 +
2817 +static irqreturn_t omap_otg_irq(int irq, void *arg)
2818 +{
2819 +       u16 otg_irq;
2820 +
2821 +       otg_irq = omap_readw(OTG_IRQ_SRC);
2822 +       if (otg_irq & OPRT_CHG) {
2823 +               omap_writew(OPRT_CHG, OTG_IRQ_SRC);
2824 +       } else if (otg_irq & B_SRP_TMROUT) {
2825 +               omap_writew(B_SRP_TMROUT, OTG_IRQ_SRC);
2826 +       } else if (otg_irq & B_HNP_FAIL) {
2827 +               omap_writew(B_HNP_FAIL, OTG_IRQ_SRC);
2828 +       } else if (otg_irq & A_SRP_DETECT) {
2829 +               omap_writew(A_SRP_DETECT, OTG_IRQ_SRC);
2830 +       } else if (otg_irq & A_REQ_TMROUT) {
2831 +               omap_writew(A_REQ_TMROUT, OTG_IRQ_SRC);
2832 +       } else if (otg_irq & A_VBUS_ERR) {
2833 +               omap_writew(A_VBUS_ERR, OTG_IRQ_SRC);
2834 +       } else if (otg_irq & DRIVER_SWITCH) {
2835 +#ifdef CONFIG_USB_OTG
2836 +               if ((!(omap_readl(OTG_CTRL) & OTG_DRIVER_SEL)) &&
2837 +                  tu->otg.host && tu->otg.state == OTG_STATE_A_HOST) {
2838 +                       /* role is host */
2839 +                       usb_bus_start_enum(tu->otg.host,
2840 +                                          tu->otg.host->otg_port);
2841 +               }
2842 +#endif
2843 +               omap_writew(DRIVER_SWITCH, OTG_IRQ_SRC);
2844 +       } else
2845 +               return IRQ_NONE;
2846 +
2847 +       return IRQ_HANDLED;
2848 +
2849 +}
2850 +
2851 +static int tahvo_otg_init(void)
2852 +{
2853 +       u32 l;
2854 +
2855 +#ifdef CONFIG_USB_OTG
2856 +       if (!tahvo_otg_dev) {
2857 +               printk("tahvo-usb: no tahvo_otg_dev\n");
2858 +               return -ENODEV;
2859 +       }
2860 +#endif
2861 +
2862 +       l = omap_readl(OTG_SYSCON_1);
2863 +       l &= ~OTG_IDLE_EN;
2864 +       omap_writel(l, OTG_SYSCON_1);
2865 +       udelay(100);
2866 +
2867 +       /* some of these values are board-specific... */
2868 +       l = omap_readl(OTG_SYSCON_2);
2869 +       l |= OTG_EN
2870 +               /* for B-device: */
2871 +               | SRP_GPDATA            /* 9msec Bdev D+ pulse */
2872 +               | SRP_GPDVBUS           /* discharge after VBUS pulse */
2873 +               // | (3 << 24)          /* 2msec VBUS pulse */
2874 +               /* for A-device: */
2875 +               | (0 << 20)             /* 200ms nominal A_WAIT_VRISE timer */
2876 +               | SRP_DPW               /* detect 167+ns SRP pulses */
2877 +               | SRP_DATA | SRP_VBUS;  /* accept both kinds of SRP pulse */
2878 +       omap_writel(l, OTG_SYSCON_2);
2879 +
2880 +       omap_writew(DRIVER_SWITCH | OPRT_CHG
2881 +                       | B_SRP_TMROUT | B_HNP_FAIL
2882 +                                 | A_VBUS_ERR | A_SRP_DETECT | A_REQ_TMROUT,
2883 +                                       OTG_IRQ_EN);
2884 +       l = omap_readl(OTG_SYSCON_2);
2885 +       l |= OTG_EN;
2886 +       omap_writel(l, OTG_SYSCON_2);
2887 +
2888 +       return 0;
2889 +}
2890 +
2891 +static int __init omap_otg_probe(struct platform_device *pdev)
2892 +{
2893 +       int ret;
2894 +
2895 +       tahvo_otg_dev = pdev;
2896 +       ret = tahvo_otg_init();
2897 +       if (ret != 0) {
2898 +               printk(KERN_ERR "tahvo-usb: tahvo_otg_init failed\n");
2899 +               return ret;
2900 +       }
2901 +
2902 +       return request_irq(tahvo_otg_dev->resource[1].start,
2903 +                          omap_otg_irq, IRQF_DISABLED, DRIVER_NAME,
2904 +                          tahvo_usb_device);
2905 +}
2906 +
2907 +static int __exit omap_otg_remove(struct platform_device *pdev)
2908 +{
2909 +       free_irq(tahvo_otg_dev->resource[1].start, tahvo_usb_device);
2910 +       tahvo_otg_dev = NULL;
2911 +
2912 +       return 0;
2913 +}
2914 +
2915 +struct platform_driver omap_otg_driver = {
2916 +       .driver         = {
2917 +               .name   = "omap_otg",
2918 +       },
2919 +       .remove         = __exit_p(omap_otg_remove),
2920 +};
2921 +
2922 +/*
2923 + * ---------------------------------------------------------------------------
2924 + * Tahvo related functions
2925 + * These are Nokia proprietary code, except for the OTG register settings,
2926 + * which are copied from isp1301.c
2927 + * ---------------------------------------------------------------------------
2928 + */
2929 +static ssize_t vbus_state_show(struct device *device,
2930 +                              struct device_attribute *attr, char *buf)
2931 +{
2932 +       struct tahvo_usb *tu = dev_get_drvdata(device);
2933 +       return sprintf(buf, "%d\n", tu->vbus_state);
2934 +}
2935 +static DEVICE_ATTR(vbus_state, 0444, vbus_state_show, NULL);
2936 +
2937 +int vbus_active = 0;
2938 +
2939 +static void check_vbus_state(struct tahvo_usb *tu)
2940 +{
2941 +       int reg, prev_state;
2942 +
2943 +       reg = tahvo_read_reg(tu->dev, TAHVO_REG_IDSR);
2944 +       if (reg & 0x01) {
2945 +               u32 l;
2946 +
2947 +               vbus_active = 1;
2948 +               switch (tu->otg.state) {
2949 +               case OTG_STATE_B_IDLE:
2950 +                       /* Enable the gadget driver */
2951 +                       if (tu->otg.gadget)
2952 +                               usb_gadget_vbus_connect(tu->otg.gadget);
2953 +                       /* Set B-session valid and not B-sessio ended to indicate
2954 +                        * Vbus to be ok. */
2955 +                       l = omap_readl(OTG_CTRL);
2956 +                       l &= ~OTG_BSESSEND;
2957 +                       l |= OTG_BSESSVLD;
2958 +                       omap_writel(l, OTG_CTRL);
2959 +
2960 +                       tu->otg.state = OTG_STATE_B_PERIPHERAL;
2961 +                       break;
2962 +               case OTG_STATE_A_IDLE:
2963 +                       /* Session is now valid assuming the USB hub is driving Vbus */
2964 +                       tu->otg.state = OTG_STATE_A_HOST;
2965 +                       break;
2966 +               default:
2967 +                       break;
2968 +               }
2969 +               printk("USB cable connected\n");
2970 +       } else {
2971 +               switch (tu->otg.state) {
2972 +               case OTG_STATE_B_PERIPHERAL:
2973 +                       if (tu->otg.gadget)
2974 +                               usb_gadget_vbus_disconnect(tu->otg.gadget);
2975 +                       tu->otg.state = OTG_STATE_B_IDLE;
2976 +                       break;
2977 +               case OTG_STATE_A_HOST:
2978 +                       tu->otg.state = OTG_STATE_A_IDLE;
2979 +                       break;
2980 +               default:
2981 +                       break;
2982 +               }
2983 +               printk("USB cable disconnected\n");
2984 +               vbus_active = 0;
2985 +       }
2986 +
2987 +       prev_state = tu->vbus_state;
2988 +       tu->vbus_state = reg & 0x01;
2989 +       if (prev_state != tu->vbus_state)
2990 +               sysfs_notify(&tu->pt_dev->dev.kobj, NULL, "vbus_state");
2991 +}
2992 +
2993 +static void tahvo_usb_become_host(struct tahvo_usb *tu)
2994 +{
2995 +       u32 l;
2996 +
2997 +       /* Clear system and transceiver controlled bits
2998 +        * also mark the A-session is always valid */
2999 +       tahvo_otg_init();
3000 +
3001 +       l = omap_readl(OTG_CTRL);
3002 +       l &= ~(OTG_CTRL_XCVR_MASK | OTG_CTRL_SYS_MASK);
3003 +       l |= OTG_ASESSVLD;
3004 +       omap_writel(l, OTG_CTRL);
3005 +
3006 +       /* Power up the transceiver in USB host mode */
3007 +       tahvo_write_reg(tu->dev, TAHVO_REG_USBR, USBR_REGOUT | USBR_NSUSPEND |
3008 +                       USBR_MASTER_SW2 | USBR_MASTER_SW1);
3009 +       tu->otg.state = OTG_STATE_A_IDLE;
3010 +
3011 +       check_vbus_state(tu);
3012 +}
3013 +
3014 +static void tahvo_usb_stop_host(struct tahvo_usb *tu)
3015 +{
3016 +       tu->otg.state = OTG_STATE_A_IDLE;
3017 +}
3018 +
3019 +static void tahvo_usb_become_peripheral(struct tahvo_usb *tu)
3020 +{
3021 +       u32 l;
3022 +
3023 +       /* Clear system and transceiver controlled bits
3024 +        * and enable ID to mark peripheral mode and
3025 +        * BSESSEND to mark no Vbus */
3026 +       tahvo_otg_init();
3027 +       l = omap_readl(OTG_CTRL);
3028 +       l &= ~(OTG_CTRL_XCVR_MASK | OTG_CTRL_SYS_MASK | OTG_BSESSVLD);
3029 +       l |= OTG_ID | OTG_BSESSEND;
3030 +       omap_writel(l, OTG_CTRL);
3031 +
3032 +       /* Power up transceiver and set it in USB perhiperal mode */
3033 +       tahvo_write_reg(tu->dev, TAHVO_REG_USBR, USBR_SLAVE_CONTROL | USBR_REGOUT | USBR_NSUSPEND | USBR_SLAVE_SW);
3034 +       tu->otg.state = OTG_STATE_B_IDLE;
3035 +
3036 +       check_vbus_state(tu);
3037 +}
3038 +
3039 +static void tahvo_usb_stop_peripheral(struct tahvo_usb *tu)
3040 +{
3041 +       u32 l;
3042 +
3043 +       l = omap_readl(OTG_CTRL);
3044 +       l &= ~OTG_BSESSVLD;
3045 +       l |= OTG_BSESSEND;
3046 +       omap_writel(l, OTG_CTRL);
3047 +
3048 +       if (tu->otg.gadget)
3049 +               usb_gadget_vbus_disconnect(tu->otg.gadget);
3050 +       tu->otg.state = OTG_STATE_B_IDLE;
3051 +
3052 +}
3053 +
3054 +static void tahvo_usb_power_off(struct tahvo_usb *tu)
3055 +{
3056 +       u32 l;
3057 +       int id;
3058 +
3059 +       /* Disable gadget controller if any */
3060 +       if (tu->otg.gadget)
3061 +               usb_gadget_vbus_disconnect(tu->otg.gadget);
3062 +
3063 +       /* Disable OTG and interrupts */
3064 +       if (TAHVO_MODE(tu) == TAHVO_MODE_PERIPHERAL)
3065 +               id = OTG_ID;
3066 +       else
3067 +               id = 0;
3068 +       l = omap_readl(OTG_CTRL);
3069 +       l &= ~(OTG_CTRL_XCVR_MASK | OTG_CTRL_SYS_MASK | OTG_BSESSVLD);
3070 +       l |= id | OTG_BSESSEND;
3071 +       omap_writel(l, OTG_CTRL);
3072 +       omap_writew(0, OTG_IRQ_EN);
3073 +
3074 +       l = omap_readl(OTG_SYSCON_2);
3075 +       l &= ~OTG_EN;
3076 +       omap_writel(l, OTG_SYSCON_2);
3077 +
3078 +       l = omap_readl(OTG_SYSCON_1);
3079 +       l |= OTG_IDLE_EN;
3080 +       omap_writel(l, OTG_SYSCON_1);
3081 +
3082 +       /* Power off transceiver */
3083 +       tahvo_write_reg(tu->dev, TAHVO_REG_USBR, 0);
3084 +       tu->otg.state = OTG_STATE_UNDEFINED;
3085 +}
3086 +
3087 +
3088 +static int tahvo_usb_set_power(struct otg_transceiver *dev, unsigned mA)
3089 +{
3090 +       struct tahvo_usb *tu = container_of(dev, struct tahvo_usb, otg);
3091 +
3092 +       dev_dbg(&tu->pt_dev->dev, "set_power %d mA\n", mA);
3093 +
3094 +       if (dev->state == OTG_STATE_B_PERIPHERAL) {
3095 +               /* REVISIT: Can Tahvo charge battery from VBUS? */
3096 +       }
3097 +       return 0;
3098 +}
3099 +
3100 +static int tahvo_usb_set_suspend(struct otg_transceiver *dev, int suspend)
3101 +{
3102 +       struct tahvo_usb *tu = container_of(dev, struct tahvo_usb, otg);
3103 +       u16 w;
3104 +
3105 +       dev_dbg(&tu->pt_dev->dev, "set_suspend\n");
3106 +
3107 +       w = tahvo_read_reg(tu->dev, TAHVO_REG_USBR);
3108 +       if (suspend)
3109 +               w &= ~USBR_NSUSPEND;
3110 +       else
3111 +               w |= USBR_NSUSPEND;
3112 +       tahvo_write_reg(tu->dev, TAHVO_REG_USBR, w);
3113 +
3114 +       return 0;
3115 +}
3116 +
3117 +static int tahvo_usb_start_srp(struct otg_transceiver *dev)
3118 +{
3119 +       struct tahvo_usb *tu = container_of(dev, struct tahvo_usb, otg);
3120 +       u32 otg_ctrl;
3121 +
3122 +       dev_dbg(&tu->pt_dev->dev, "start_srp\n");
3123 +
3124 +       if (!dev || tu->otg.state != OTG_STATE_B_IDLE)
3125 +               return -ENODEV;
3126 +
3127 +       otg_ctrl = omap_readl(OTG_CTRL);
3128 +       if (!(otg_ctrl & OTG_BSESSEND))
3129 +               return -EINVAL;
3130 +
3131 +       otg_ctrl |= OTG_B_BUSREQ;
3132 +       otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_SYS_MASK;
3133 +       omap_writel(otg_ctrl, OTG_CTRL);
3134 +       tu->otg.state = OTG_STATE_B_SRP_INIT;
3135 +
3136 +       return 0;
3137 +}
3138 +
3139 +static int tahvo_usb_start_hnp(struct otg_transceiver *otg)
3140 +{
3141 +       struct tahvo_usb *tu = container_of(otg, struct tahvo_usb, otg);
3142 +
3143 +       dev_dbg(&tu->pt_dev->dev, "start_hnp\n");
3144 +#ifdef CONFIG_USB_OTG
3145 +       /* REVISIT: Add this for OTG */
3146 +#endif
3147 +       return -EINVAL;
3148 +}
3149 +
3150 +static int tahvo_usb_set_host(struct otg_transceiver *otg, struct usb_bus *host)
3151 +{
3152 +       struct tahvo_usb *tu = container_of(otg, struct tahvo_usb, otg);
3153 +       u32 l;
3154 +
3155 +       dev_dbg(&tu->pt_dev->dev, "set_host %p\n", host);
3156 +
3157 +       if (otg == NULL)
3158 +               return -ENODEV;
3159 +
3160 +#if defined(CONFIG_USB_OTG) || !defined(CONFIG_USB_GADGET_OMAP)
3161 +
3162 +       mutex_lock(&tu->serialize);
3163 +
3164 +       if (host == NULL) {
3165 +               if (TAHVO_MODE(tu) == TAHVO_MODE_HOST)
3166 +                       tahvo_usb_power_off(tu);
3167 +               tu->otg.host = NULL;
3168 +               mutex_unlock(&tu->serialize);
3169 +               return 0;
3170 +       }
3171 +
3172 +       l = omap_readl(OTG_SYSCON_1);
3173 +       l &= ~(OTG_IDLE_EN | HST_IDLE_EN | DEV_IDLE_EN);
3174 +       omap_writel(l, OTG_SYSCON_1);
3175 +
3176 +       if (TAHVO_MODE(tu) == TAHVO_MODE_HOST) {
3177 +               tu->otg.host = NULL;
3178 +               tahvo_usb_become_host(tu);
3179 +       }
3180 +
3181 +       tu->otg.host = host;
3182 +
3183 +       mutex_unlock(&tu->serialize);
3184 +#else
3185 +       /* No host mode configured, so do not allow host controlled to be set */
3186 +       return -EINVAL;
3187 +#endif
3188 +
3189 +       return 0;
3190 +}
3191 +
3192 +static int tahvo_usb_set_peripheral(struct otg_transceiver *otg, struct usb_gadget *gadget)
3193 +{
3194 +       struct tahvo_usb *tu = container_of(otg, struct tahvo_usb, otg);
3195 +
3196 +       dev_dbg(&tu->pt_dev->dev, "set_peripheral %p\n", gadget);
3197 +
3198 +       if (!otg)
3199 +               return -ENODEV;
3200 +
3201 +#if defined(CONFIG_USB_OTG) || defined(CONFIG_USB_GADGET_OMAP)
3202 +
3203 +       mutex_lock(&tu->serialize);
3204 +
3205 +       if (!gadget) {
3206 +               if (TAHVO_MODE(tu) == TAHVO_MODE_PERIPHERAL)
3207 +                       tahvo_usb_power_off(tu);
3208 +               tu->otg.gadget = NULL;
3209 +               mutex_unlock(&tu->serialize);
3210 +               return 0;
3211 +       }
3212 +
3213 +       tu->otg.gadget = gadget;
3214 +       if (TAHVO_MODE(tu) == TAHVO_MODE_PERIPHERAL)
3215 +               tahvo_usb_become_peripheral(tu);
3216 +
3217 +       mutex_unlock(&tu->serialize);
3218 +#else
3219 +       /* No gadget mode configured, so do not allow host controlled to be set */
3220 +       return -EINVAL;
3221 +#endif
3222 +
3223 +       return 0;
3224 +}
3225 +
3226 +static irqreturn_t tahvo_usb_vbus_interrupt(int irq, void *_tu)
3227 +{
3228 +       struct tahvo_usb *tu = _tu;
3229 +
3230 +       check_vbus_state(tu);
3231 +
3232 +       return IRQ_HANDLED;
3233 +}
3234 +
3235 +#ifdef CONFIG_USB_OTG
3236 +static ssize_t otg_mode_show(struct device *device,
3237 +                            struct device_attribute *attr, char *buf)
3238 +{
3239 +       struct tahvo_usb *tu = dev_get_drvdata(device);
3240 +       switch (tu->tahvo_mode) {
3241 +       case TAHVO_MODE_HOST:
3242 +               return sprintf(buf, "host\n");
3243 +       case TAHVO_MODE_PERIPHERAL:
3244 +               return sprintf(buf, "peripheral\n");
3245 +       }
3246 +       return sprintf(buf, "unknown\n");
3247 +}
3248 +
3249 +static ssize_t otg_mode_store(struct device *device,
3250 +                             struct device_attribute *attr,
3251 +                             const char *buf, size_t count)
3252 +{
3253 +       struct tahvo_usb *tu = dev_get_drvdata(device);
3254 +       int r;
3255 +
3256 +       r = strlen(buf);
3257 +       mutex_lock(&tu->serialize);
3258 +       if (strncmp(buf, "host", 4) == 0) {
3259 +               if (tu->tahvo_mode == TAHVO_MODE_PERIPHERAL)
3260 +                       tahvo_usb_stop_peripheral(tu);
3261 +               tu->tahvo_mode = TAHVO_MODE_HOST;
3262 +               if (tu->otg.host) {
3263 +                       printk(KERN_INFO "Selected HOST mode: host controller present.\n");
3264 +                       tahvo_usb_become_host(tu);
3265 +               } else {
3266 +                       printk(KERN_INFO "Selected HOST mode: no host controller, powering off.\n");
3267 +                       tahvo_usb_power_off(tu);
3268 +               }
3269 +       } else if (strncmp(buf, "peripheral", 10) == 0) {
3270 +               if (tu->tahvo_mode == TAHVO_MODE_HOST)
3271 +                       tahvo_usb_stop_host(tu);
3272 +               tu->tahvo_mode = TAHVO_MODE_PERIPHERAL;
3273 +               if (tu->otg.gadget) {
3274 +                       printk(KERN_INFO "Selected PERIPHERAL mode: gadget driver present.\n");
3275 +                       tahvo_usb_become_peripheral(tu);
3276 +               } else {
3277 +                       printk(KERN_INFO "Selected PERIPHERAL mode: no gadget driver, powering off.\n");
3278 +                       tahvo_usb_power_off(tu);
3279 +               }
3280 +       } else
3281 +               r = -EINVAL;
3282 +
3283 +       mutex_unlock(&tu->serialize);
3284 +       return r;
3285 +}
3286 +
3287 +static DEVICE_ATTR(otg_mode, 0644, otg_mode_show, otg_mode_store);
3288 +#endif
3289 +
3290 +static int __init tahvo_usb_probe(struct platform_device *pdev)
3291 +{
3292 +       struct tahvo_usb *tu;
3293 +       struct device *dev = &pdev->dev;
3294 +       int ret;
3295 +       int irq;
3296 +
3297 +       dev_dbg(dev, "probe\n");
3298 +
3299 +       /* Create driver data */
3300 +       tu = kzalloc(sizeof(*tu), GFP_KERNEL);
3301 +       if (!tu)
3302 +               return -ENOMEM;
3303 +       tahvo_usb_device = tu;
3304 +
3305 +       tu->dev = dev;
3306 +       tu->pt_dev = pdev;
3307 +#ifdef CONFIG_USB_OTG
3308 +       /* Default mode */
3309 +#ifdef CONFIG_CBUS_TAHVO_USB_HOST_BY_DEFAULT
3310 +       tu->tahvo_mode = TAHVO_MODE_HOST;
3311 +#else
3312 +       tu->tahvo_mode = TAHVO_MODE_PERIPHERAL;
3313 +#endif
3314 +#endif
3315 +
3316 +       mutex_init(&tu->serialize);
3317 +
3318 +       tu->ick = clk_get(NULL, "usb_l4_ick");
3319 +       if (IS_ERR(tu->ick)) {
3320 +               dev_err(dev, "Failed to get usb_l4_ick\n");
3321 +               ret = PTR_ERR(tu->ick);
3322 +               goto err_free_tu;
3323 +       }
3324 +       clk_enable(tu->ick);
3325 +
3326 +       /* Set initial state, so that we generate kevents only on
3327 +        * state changes */
3328 +       tu->vbus_state = tahvo_read_reg(tu->dev, TAHVO_REG_IDSR) & 0x01;
3329 +
3330 +       irq = platform_get_irq(pdev, 0);
3331 +       tu->irq = irq;
3332 +
3333 +       /* We cannot enable interrupt until omap_udc is initialized */
3334 +       ret = request_threaded_irq(irq, NULL, tahvo_usb_vbus_interrupt,
3335 +                       IRQF_ONESHOT, "tahvo-vbus", tu);
3336 +       if (ret != 0) {
3337 +               printk(KERN_ERR "Could not register Tahvo interrupt for VBUS\n");
3338 +               goto err_release_clk;
3339 +       }
3340 +
3341 +       /* Attributes */
3342 +       ret = device_create_file(dev, &dev_attr_vbus_state);
3343 +#ifdef CONFIG_USB_OTG
3344 +       ret |= device_create_file(dev, &dev_attr_otg_mode);
3345 +#endif
3346 +       if (ret)
3347 +               printk(KERN_ERR "attribute creation failed: %d\n", ret);
3348 +
3349 +       /* Create OTG interface */
3350 +       tahvo_usb_power_off(tu);
3351 +       tu->otg.state = OTG_STATE_UNDEFINED;
3352 +       tu->otg.label = DRIVER_NAME;
3353 +       tu->otg.set_host = tahvo_usb_set_host;
3354 +       tu->otg.set_peripheral = tahvo_usb_set_peripheral;
3355 +       tu->otg.set_power = tahvo_usb_set_power;
3356 +       tu->otg.set_suspend = tahvo_usb_set_suspend;
3357 +       tu->otg.start_srp = tahvo_usb_start_srp;
3358 +       tu->otg.start_hnp = tahvo_usb_start_hnp;
3359 +
3360 +       ret = otg_set_transceiver(&tu->otg);
3361 +       if (ret < 0) {
3362 +               printk(KERN_ERR "Cannot register USB transceiver\n");
3363 +               goto err_free_irq;
3364 +       }
3365 +
3366 +       dev_set_drvdata(dev, tu);
3367 +
3368 +       return 0;
3369 +
3370 +err_free_irq:
3371 +       free_irq(tu->irq, tu);
3372 +err_release_clk:
3373 +       clk_disable(tu->ick);
3374 +       clk_put(tu->ick);
3375 +err_free_tu:
3376 +       kfree(tu);
3377 +       tahvo_usb_device = NULL;
3378 +
3379 +       return ret;
3380 +}
3381 +
3382 +static int __exit tahvo_usb_remove(struct platform_device *pdev)
3383 +{
3384 +       struct tahvo_usb *tu = platform_get_drvdata(pdev);
3385 +
3386 +       dev_dbg(&pdev->dev, "remove\n");
3387 +
3388 +       free_irq(tu->irq, tu);
3389 +       flush_scheduled_work();
3390 +       otg_set_transceiver(0);
3391 +       device_remove_file(&pdev->dev, &dev_attr_vbus_state);
3392 +#ifdef CONFIG_USB_OTG
3393 +       device_remove_file(&pdev->dev, &dev_attr_otg_mode);
3394 +#endif
3395 +       clk_disable(tu->ick);
3396 +       clk_put(tu->ick);
3397 +
3398 +       kfree(tu);
3399 +       tahvo_usb_device = NULL;
3400 +
3401 +       return 0;
3402 +}
3403 +
3404 +static struct platform_driver tahvo_usb_driver = {
3405 +       .driver         = {
3406 +               .name   = "tahvo-usb",
3407 +       },
3408 +       .remove         = __exit_p(tahvo_usb_remove),
3409 +};
3410 +
3411 +static int __init tahvo_usb_init(void)
3412 +{
3413 +       int ret = 0;
3414 +
3415 +       ret = platform_driver_probe(&tahvo_usb_driver, tahvo_usb_probe);
3416 +       if (ret)
3417 +               return ret;
3418 +
3419 +       ret = platform_driver_probe(&omap_otg_driver, omap_otg_probe);
3420 +       if (ret) {
3421 +               platform_driver_unregister(&tahvo_usb_driver);
3422 +               return ret;
3423 +       }
3424 +
3425 +       return 0;
3426 +}
3427 +
3428 +subsys_initcall(tahvo_usb_init);
3429 +
3430 +static void __exit tahvo_usb_exit(void)
3431 +{
3432 +       platform_driver_unregister(&omap_otg_driver);
3433 +       platform_driver_unregister(&tahvo_usb_driver);
3434 +}
3435 +module_exit(tahvo_usb_exit);
3436 +
3437 +MODULE_DESCRIPTION("Tahvo USB OTG Transceiver Driver");
3438 +MODULE_LICENSE("GPL");
3439 +MODULE_AUTHOR("Juha Yrjölä, Tony Lindgren, and Timo Teräs");
3440 --- a/drivers/Makefile
3441 +++ b/drivers/Makefile
3442 @@ -76,7 +76,7 @@ obj-$(CONFIG_GAMEPORT)                += input/gamepor
3443  obj-$(CONFIG_INPUT)            += input/
3444  obj-$(CONFIG_I2O)              += message/
3445  obj-$(CONFIG_RTC_LIB)          += rtc/
3446 -obj-y                          += i2c/ media/
3447 +obj-y                          += i2c/ media/ cbus/
3448  obj-$(CONFIG_PPS)              += pps/
3449  obj-$(CONFIG_PTP_1588_CLOCK)   += ptp/
3450  obj-$(CONFIG_W1)               += w1/
3451 --- a/drivers/Kconfig
3452 +++ b/drivers/Kconfig
3453 @@ -2,6 +2,8 @@ menu "Device Drivers"
3454  
3455  source "drivers/base/Kconfig"
3456  
3457 +source "drivers/cbus/Kconfig"
3458 +
3459  source "drivers/connector/Kconfig"
3460  
3461  source "drivers/mtd/Kconfig"