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