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