1 Index: linux-3.1/drivers/cbus/cbus.c
2 ===================================================================
3 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
4 +++ linux-3.1/drivers/cbus/cbus.c 2011-10-30 00:48:42.989047642 +0200
7 + * drivers/cbus/cbus.c
9 + * Support functions for CBUS serial protocol
11 + * Copyright (C) 2004-2010 Nokia Corporation
12 + * Contact: Felipe Balbi <felipe.balbi@nokia.com>
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>
18 + * Several updates and cleanups by Felipe Balbi <felipe.balbi@nokia.com>
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.
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.
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
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>
45 +#define CBUS_XFER_READ 1
46 +#define CBUS_XFER_WRITE 0
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
65 +static int cbus_send_bit(struct cbus_host *host, unsigned bit,
70 + gpio_set_value(host->dat_gpio, bit ? 1 : 0);
71 + gpio_set_value(host->clk_gpio, 1);
73 + /* The data bit is read on the rising edge of CLK */
75 + ret = gpio_direction_input(host->dat_gpio);
77 + gpio_set_value(host->clk_gpio, 0);
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
89 +static int cbus_send_data(struct cbus_host *host, unsigned data, unsigned len,
95 + for (i = len; i > 0; i--) {
96 + ret = cbus_send_bit(host, data & (1 << (i - 1)),
107 + * cbus_receive_bit - receives one bit from the bus
108 + * @host: the host we're using
110 +static int cbus_receive_bit(struct cbus_host *host)
114 + gpio_set_value(host->clk_gpio, 1);
115 + ret = gpio_get_value(host->dat_gpio);
118 + gpio_set_value(host->clk_gpio, 0);
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
129 +static int cbus_receive_data(struct cbus_host *host, unsigned len)
134 + for (i = 16; i > 0; i--) {
135 + int bit = cbus_receive_bit(host);
141 + ret |= 1 << (i - 1);
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
156 +static int cbus_transfer(struct cbus_host *host, unsigned rw, unsigned dev,
157 + unsigned reg, unsigned data)
159 + unsigned long flags;
163 + /* We don't want interrupts disturbing our transfer */
164 + spin_lock_irqsave(&host->lock, flags);
166 + /* Reset state and start of transfer, SEL stays down during transfer */
167 + gpio_set_value(host->sel_gpio, 0);
169 + /* Set the DAT pin to output */
170 + gpio_direction_output(host->dat_gpio, 1);
172 + /* Send the device address */
173 + ret = cbus_send_data(host, dev, 3, 0);
175 + dev_dbg(host->dev, "failed sending device addr\n");
179 + /* Send the rw flag */
180 + ret = cbus_send_bit(host, rw, 0);
182 + dev_dbg(host->dev, "failed sending read/write flag\n");
186 + /* Send the register address */
190 + ret = cbus_send_data(host, reg, 5, input);
192 + dev_dbg(host->dev, "failed sending register addr\n");
197 + ret = cbus_send_data(host, data, 16, 0);
199 + dev_dbg(host->dev, "failed sending data\n");
203 + gpio_set_value(host->clk_gpio, 1);
205 + ret = cbus_receive_data(host, 16);
207 + dev_dbg(host->dev, "failed receiving data\n");
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);
218 + spin_unlock_irqrestore(&host->lock, flags);
224 + * cbus_read_reg - reads a given register from the device
225 + * @child: the child device
226 + * @dev: device address
227 + * @reg: register address
229 +int cbus_read_reg(struct device *child, unsigned dev, unsigned reg)
231 + struct cbus_host *host = dev_get_drvdata(child->parent);
233 + return cbus_transfer(host, CBUS_XFER_READ, dev, reg, 0);
235 +EXPORT_SYMBOL(cbus_read_reg);
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
244 +int cbus_write_reg(struct device *child, unsigned dev, unsigned reg,
247 + struct cbus_host *host = dev_get_drvdata(child->parent);
249 + return cbus_transfer(host, CBUS_XFER_WRITE, dev, reg, val);
251 +EXPORT_SYMBOL(cbus_write_reg);
253 +static int __init cbus_bus_probe(struct platform_device *pdev)
255 + struct cbus_host *chost;
256 + struct cbus_host_platform_data *pdata = pdev->dev.platform_data;
259 + chost = kzalloc(sizeof(*chost), GFP_KERNEL);
263 + spin_lock_init(&chost->lock);
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;
270 + ret = gpio_request(chost->clk_gpio, "CBUS clk");
274 + ret = gpio_request(chost->dat_gpio, "CBUS data");
278 + ret = gpio_request(chost->sel_gpio, "CBUS sel");
282 + gpio_direction_output(chost->clk_gpio, 0);
283 + gpio_direction_input(chost->dat_gpio);
284 + gpio_direction_output(chost->sel_gpio, 1);
286 + gpio_set_value(chost->clk_gpio, 1);
287 + gpio_set_value(chost->clk_gpio, 0);
289 + platform_set_drvdata(pdev, chost);
293 + gpio_free(chost->dat_gpio);
295 + gpio_free(chost->clk_gpio);
302 +static void __exit cbus_bus_remove(struct platform_device *pdev)
304 + struct cbus_host *chost = platform_get_drvdata(pdev);
306 + gpio_free(chost->sel_gpio);
307 + gpio_free(chost->dat_gpio);
308 + gpio_free(chost->clk_gpio);
313 +static struct platform_driver cbus_driver = {
314 + .remove = __exit_p(cbus_bus_remove),
320 +static int __init cbus_bus_init(void)
322 + return platform_driver_probe(&cbus_driver, cbus_bus_probe);
324 +subsys_initcall(cbus_bus_init);
326 +static void __exit cbus_bus_exit(void)
328 + platform_driver_unregister(&cbus_driver);
330 +module_exit(cbus_bus_exit);
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>");
339 Index: linux-3.1/drivers/cbus/cbus.h
340 ===================================================================
341 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
342 +++ linux-3.1/drivers/cbus/cbus.h 2011-10-30 00:48:42.989047642 +0200
345 + * drivers/cbus/cbus.h
347 + * Copyright (C) 2004, 2005 Nokia Corporation
349 + * Written by Juha Yrjölä <juha.yrjola@nokia.com> and
350 + * David Weinehall <david.weinehall@nokia.com>
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.
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.
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
366 +#ifndef __DRIVERS_CBUS_CBUS_H
367 +#define __DRIVERS_CBUS_CBUS_H
369 +extern int cbus_read_reg(struct device *, unsigned dev, unsigned reg);
370 +extern int cbus_write_reg(struct device *, unsigned dev, unsigned reg,
373 +#endif /* __DRIVERS_CBUS_CBUS_H */
374 Index: linux-3.1/drivers/cbus/Kconfig
375 ===================================================================
376 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
377 +++ linux-3.1/drivers/cbus/Kconfig 2011-10-30 00:48:42.989047642 +0200
380 +# CBUS device configuration
386 + bool "CBUS support on OMAP"
388 + CBUS is a proprietary serial protocol by Nokia. It is mainly
389 + used for accessing Energy Management auxiliary chips.
391 + If you want CBUS support, you should say Y here.
395 + bool "Support for Tahvo"
397 + Tahvo is a mixed signal ASIC with some system features
399 + If you want Tahvo support, you should say Y here.
403 +config CBUS_TAHVO_USB
405 + depends on ARCH_OMAP
406 + select USB_OTG_UTILS
407 + tristate "Support for Tahvo USB transceiver"
409 + If you want Tahvo support for USB transceiver, say Y or M here.
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"
415 + Say Y here, if you want the device to enter USB host mode
416 + by default on bootup.
422 + bool "Support for Retu"
424 + Retu is a mixed signal ASIC with some system features
426 + If you want Retu support, you should say Y here.
430 +config CBUS_RETU_POWERBUTTON
432 + bool "Support for Retu power button"
434 + The power button on Nokia 770 is connected to the Retu ASIC.
436 + If you want support for the Retu power button, you should say Y here.
438 +config CBUS_RETU_RTC
439 + depends on RTC_CLASS
440 + depends on ARCH_OMAP
441 + tristate "Support for Retu pseudo-RTC"
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.
446 +config CBUS_RETU_WDT
447 + depends on SYSFS && WATCHDOG
448 + depends on ARCH_OMAP
449 + tristate "Support for Retu watchdog timer"
451 + Say Y here if you want support for the watchdog in Retu. This will
452 + expose a sysfs interface to grok it.
454 +config CBUS_RETU_HEADSET
456 + tristate "Support for headset detection with Retu/Vilma"
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
465 Index: linux-3.1/drivers/cbus/Makefile
466 ===================================================================
467 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
468 +++ linux-3.1/drivers/cbus/Makefile 2011-10-30 00:48:42.989047642 +0200
471 +# Makefile for CBUS.
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
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/drivers/cbus/retu.c
484 ===================================================================
485 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
486 +++ linux-3.1/drivers/cbus/retu.c 2011-10-30 00:48:42.989047642 +0200
489 + * drivers/cbus/retu.c
491 + * Support functions for Retu ASIC
493 + * Copyright (C) 2004, 2005 Nokia Corporation
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>
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.
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.
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
513 +#include <linux/module.h>
514 +#include <linux/init.h>
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>
526 +#include <asm/bitops.h>
533 + struct mutex mutex;
534 + struct device *dev;
552 +static struct retu *the_retu;
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
559 +static int __retu_read_reg(struct retu *retu, unsigned reg)
561 + return cbus_read_reg(retu->dev, retu->devid, reg);
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
570 +static void __retu_write_reg(struct retu *retu, unsigned reg, u16 val)
572 + cbus_write_reg(retu->dev, retu->devid, reg, val);
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
580 + * This function returns the contents of the specified register
582 +int retu_read_reg(struct device *child, unsigned reg)
584 + struct retu *retu = dev_get_drvdata(child->parent);
586 + return __retu_read_reg(retu, reg);
588 +EXPORT_SYMBOL_GPL(retu_read_reg);
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
596 + * This function writes a value to the specified register
598 +void retu_write_reg(struct device *child, unsigned reg, u16 val)
600 + struct retu *retu = dev_get_drvdata(child->parent);
602 + mutex_lock(&retu->mutex);
603 + __retu_write_reg(retu, reg, val);
604 + mutex_unlock(&retu->mutex);
606 +EXPORT_SYMBOL_GPL(retu_write_reg);
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
615 +void retu_set_clear_reg_bits(struct device *child, unsigned reg, u16 set,
618 + struct retu *retu = dev_get_drvdata(child->parent);
621 + mutex_lock(&retu->mutex);
622 + w = __retu_read_reg(retu, reg);
625 + __retu_write_reg(retu, reg, w);
626 + mutex_unlock(&retu->mutex);
628 +EXPORT_SYMBOL_GPL(retu_set_clear_reg_bits);
630 +#define ADC_MAX_CHAN_NUMBER 13
633 + * retu_read_adc - Reads AD conversion result
634 + * @child: device pointer to calling child
635 + * @channel: the ADC channel to read from
637 +int retu_read_adc(struct device *child, int channel)
639 + struct retu *retu = dev_get_drvdata(child->parent);
645 + if (channel < 0 || channel > ADC_MAX_CHAN_NUMBER)
648 + mutex_lock(&retu->mutex);
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));
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;
661 + if (retu->is_vilma)
662 + __retu_write_reg(retu, RETU_REG_ADCR, (1 << 13));
665 + mutex_unlock(&retu->mutex);
669 +EXPORT_SYMBOL_GPL(retu_read_adc);
671 +static irqreturn_t retu_irq_handler(int irq, void *_retu)
673 + struct retu *retu = _retu;
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);
685 + dev_vdbg(retu->dev, "No IRQ, spurious?\n");
690 + unsigned long pending = __ffs(idr);
693 + idr &= ~BIT(pending);
694 + irq = pending + retu->irq_base;
695 + handle_nested_irq(irq);
698 + return IRQ_HANDLED;
701 +/* -------------------------------------------------------------------------- */
703 +static void retu_irq_mask(struct irq_data *data)
705 + struct retu *retu = irq_data_get_irq_chip_data(data);
706 + int irq = data->irq;
708 + retu->mask |= (1 << (irq - retu->irq_base));
709 + retu->mask_pending = true;
712 +static void retu_irq_unmask(struct irq_data *data)
714 + struct retu *retu = irq_data_get_irq_chip_data(data);
715 + int irq = data->irq;
717 + retu->mask &= ~(1 << (irq - retu->irq_base));
718 + retu->mask_pending = true;
722 +static void retu_irq_ack(struct irq_data *data)
724 + struct retu *retu = irq_data_get_irq_chip_data(data);
725 + int irq = data->irq;
727 + retu->ack |= (1 << (irq - retu->irq_base));
728 + retu->ack_pending = true;
731 +static void retu_bus_lock(struct irq_data *data)
733 + struct retu *retu = irq_data_get_irq_chip_data(data);
735 + mutex_lock(&retu->mutex);
738 +static void retu_bus_sync_unlock(struct irq_data *data)
740 + struct retu *retu = irq_data_get_irq_chip_data(data);
742 + if (retu->mask_pending) {
743 + __retu_write_reg(retu, RETU_REG_IMR, retu->mask);
744 + retu->mask_pending = false;
747 + if (retu->ack_pending) {
748 + __retu_write_reg(retu, RETU_REG_IDR, retu->ack);
749 + retu->ack_pending = false;
752 + mutex_unlock(&retu->mutex);
755 +static struct irq_chip retu_irq_chip = {
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,
764 +static inline void retu_irq_setup(int irq)
767 + set_irq_flags(irq, IRQF_VALID);
769 + irq_set_noprobe(irq);
773 +static void retu_irq_init(struct retu *retu)
775 + int base = retu->irq_base;
776 + int end = retu->irq_end;
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);
788 +static void retu_irq_exit(struct retu *retu)
790 + int base = retu->irq_base;
791 + int end = retu->irq_end;
794 + for (irq = base; irq < end; irq++) {
796 + set_irq_flags(irq, 0);
798 + irq_set_chip_and_handler(irq, NULL, NULL);
799 + irq_set_chip_data(irq, NULL);
803 +/* -------------------------------------------------------------------------- */
806 + * retu_power_off - Shut down power to system
808 + * This function puts the system in power off state
810 +static void retu_power_off(void)
812 + struct retu *retu = the_retu;
815 + reg = __retu_read_reg(retu, RETU_REG_CC1);
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*/
825 +static struct resource generic_resources[] = {
827 + .start = -EINVAL, /* fixed later */
828 + .flags = IORESOURCE_IRQ,
831 + .start = -EINVAL, /* fixed later */
832 + .flags = IORESOURCE_IRQ,
837 + * retu_allocate_child - Allocates one Retu child
838 + * @name: name of new child
839 + * @parent: parent device for this child
841 +static struct device *retu_allocate_child(char *name, struct device *parent,
842 + int irq_base, int irq1, int irq2, int num)
844 + struct platform_device *pdev;
847 + pdev = platform_device_alloc(name, -1);
849 + dev_dbg(parent, "can't allocate %s\n", name);
853 + pdev->dev.parent = parent;
856 + generic_resources[0].start = irq_base + irq1;
857 + generic_resources[1].start = irq_base + irq2;
859 + status = platform_device_add_resources(pdev,
860 + generic_resources, num);
862 + dev_dbg(parent, "can't add resources to %s\n", name);
867 + status = platform_device_add(pdev);
869 + dev_dbg(parent, "can't add %s\n", name);
876 + platform_device_put(pdev);
882 + * retu_allocate_children - Allocates Retu's children
884 +static int retu_allocate_children(struct device *parent, int irq_base)
886 + struct device *child;
888 + child = retu_allocate_child("retu-pwrbutton", parent, irq_base,
889 + RETU_INT_PWR, -1, 1);
893 + child = retu_allocate_child("retu-headset", parent, irq_base,
894 + RETU_INT_HOOK, -1, 1);
898 + child = retu_allocate_child("retu-rtc", parent, irq_base,
899 + RETU_INT_RTCS, RETU_INT_RTCA, 2);
903 + child = retu_allocate_child("retu-wdt", parent, -1, -1, -1, 0);
911 + * retu_probe - Probe for Retu ASIC
912 + * @dev: the Retu device
914 + * Probe for the Retu ASIC and allocate memory
915 + * for its device-struct if found
917 +static int __devinit retu_probe(struct platform_device *pdev)
920 + struct cbus_retu_platform_data *pdata = pdev->dev.platform_data;
925 + retu = kzalloc(sizeof(*retu), GFP_KERNEL);
927 + dev_err(&pdev->dev, "not enough memory\n");
931 + platform_set_drvdata(pdev, retu);
933 + ret = irq_alloc_descs(-1, 0, MAX_RETU_IRQ_HANDLERS, 0);
935 + dev_err(&pdev->dev, "failed to allocate IRQ descs\n");
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;
946 + mutex_init(&retu->mutex);
948 + retu_irq_init(retu);
950 + rev = __retu_read_reg(retu, RETU_REG_ASICR) & 0xff;
951 + if (rev & (1 << 7))
952 + retu->is_vilma = true;
954 + dev_info(&pdev->dev, "%s v%d.%d found\n",
955 + retu->is_vilma ? "Vilma" : "Retu",
956 + (rev >> 4) & 0x07, rev & 0x0f);
958 + /* Mask all RETU interrupts */
959 + __retu_write_reg(retu, RETU_REG_IMR, 0xffff);
961 + ret = request_threaded_irq(retu->irq, NULL, retu_irq_handler,
962 + IRQF_ONESHOT, "retu", retu);
964 + dev_err(&pdev->dev, "Unable to register IRQ handler\n");
968 + irq_set_irq_wake(retu->irq, 1);
970 + /* Register power off function */
971 + pm_power_off = retu_power_off;
973 + ret = retu_allocate_children(&pdev->dev, retu->irq_base);
975 + dev_err(&pdev->dev, "Unable to allocate Retu children\n");
982 + pm_power_off = NULL;
983 + free_irq(retu->irq, retu);
986 + retu_irq_exit(retu);
987 + irq_free_descs(retu->irq_base, MAX_RETU_IRQ_HANDLERS);
997 +static int __devexit retu_remove(struct platform_device *pdev)
999 + struct retu *retu = platform_get_drvdata(pdev);
1001 + pm_power_off = NULL;
1004 + free_irq(retu->irq, retu);
1005 + retu_irq_exit(retu);
1006 + irq_free_descs(retu->irq_base, MAX_RETU_IRQ_HANDLERS);
1012 +static struct platform_driver retu_driver = {
1013 + .probe = retu_probe,
1014 + .remove = __devexit_p(retu_remove),
1020 +static int __init retu_init(void)
1022 + return platform_driver_register(&retu_driver);
1024 +subsys_initcall(retu_init);
1026 +static void __exit retu_exit(void)
1028 + platform_driver_unregister(&retu_driver);
1030 +module_exit(retu_exit);
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/drivers/cbus/retu.h
1038 ===================================================================
1039 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1040 +++ linux-3.1/drivers/cbus/retu.h 2011-10-30 00:48:42.989047642 +0200
1043 + * drivers/cbus/retu.h
1045 + * Copyright (C) 2004, 2005 Nokia Corporation
1047 + * Written by Juha Yrjölä <juha.yrjola@nokia.com> and
1048 + * David Weinehall <david.weinehall@nokia.com>
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.
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.
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
1064 +#ifndef __DRIVERS_CBUS_RETU_H
1065 +#define __DRIVERS_CBUS_RETU_H
1067 +#include <linux/types.h>
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
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
1101 +#define MAX_RETU_IRQ_HANDLERS 16
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 */
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,
1124 +int retu_read_adc(struct device *child, int channel);
1126 +#endif /* __DRIVERS_CBUS_RETU_H */
1127 Index: linux-3.1/drivers/cbus/retu-headset.c
1128 ===================================================================
1129 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1130 +++ linux-3.1/drivers/cbus/retu-headset.c 2011-10-30 00:48:42.989047642 +0200
1133 + * Retu/Vilma headset detection
1135 + * Copyright (C) 2006 Nokia Corporation
1137 + * Written by Juha Yrjölä
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.
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.
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
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>
1165 +#define RETU_ADC_CHANNEL_HOOKDET 0x05
1167 +#define RETU_HEADSET_KEY KEY_PHONE
1169 +struct retu_headset {
1171 + struct mutex mutex;
1172 + struct device *dev;
1173 + struct input_dev *idev;
1174 + unsigned bias_enabled;
1175 + unsigned detection_enabled;
1177 + struct timer_list enable_timer;
1178 + struct timer_list detect_timer;
1182 +static void retu_headset_set_bias(struct retu_headset *hs, int enable)
1185 + retu_set_clear_reg_bits(hs->dev, RETU_REG_AUDTXR,
1186 + (1 << 0) | (1 << 1), 0);
1188 + retu_set_clear_reg_bits(hs->dev, RETU_REG_AUDTXR,
1191 + retu_set_clear_reg_bits(hs->dev, RETU_REG_AUDTXR, 0,
1192 + (1 << 0) | (1 << 1) | (1 << 3));
1196 +static void retu_headset_enable(struct retu_headset *hs)
1198 + mutex_lock(&hs->mutex);
1199 + if (!hs->bias_enabled) {
1200 + hs->bias_enabled = 1;
1201 + retu_headset_set_bias(hs, 1);
1203 + mutex_unlock(&hs->mutex);
1206 +static void retu_headset_disable(struct retu_headset *hs)
1208 + mutex_lock(&hs->mutex);
1209 + if (hs->bias_enabled) {
1210 + hs->bias_enabled = 0;
1211 + retu_headset_set_bias(hs, 0);
1213 + mutex_unlock(&hs->mutex);
1216 +static void retu_headset_det_enable(struct retu_headset *hs)
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);
1224 + mutex_unlock(&hs->mutex);
1227 +static void retu_headset_det_disable(struct retu_headset *hs)
1229 + unsigned long flags;
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);
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));
1243 + mutex_unlock(&hs->mutex);
1246 +static ssize_t retu_headset_hookdet_show(struct device *dev,
1247 + struct device_attribute *attr,
1252 + val = retu_read_adc(dev, RETU_ADC_CHANNEL_HOOKDET);
1253 + return sprintf(buf, "%d\n", val);
1256 +static DEVICE_ATTR(hookdet, S_IRUGO, retu_headset_hookdet_show, NULL);
1258 +static ssize_t retu_headset_enable_show(struct device *dev,
1259 + struct device_attribute *attr,
1262 + struct retu_headset *hs = dev_get_drvdata(dev);
1264 + return sprintf(buf, "%u\n", hs->bias_enabled);
1267 +static ssize_t retu_headset_enable_store(struct device *dev,
1268 + struct device_attribute *attr,
1269 + const char *buf, size_t count)
1271 + struct retu_headset *hs = dev_get_drvdata(dev);
1274 + if (sscanf(buf, "%u", &enable) != 1)
1277 + retu_headset_enable(hs);
1279 + retu_headset_disable(hs);
1283 +static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
1284 + retu_headset_enable_show, retu_headset_enable_store);
1286 +static ssize_t retu_headset_enable_det_show(struct device *dev,
1287 + struct device_attribute *attr,
1290 + struct retu_headset *hs = dev_get_drvdata(dev);
1292 + return sprintf(buf, "%u\n", hs->detection_enabled);
1295 +static ssize_t retu_headset_enable_det_store(struct device *dev,
1296 + struct device_attribute *attr,
1297 + const char *buf, size_t count)
1299 + struct retu_headset *hs = dev_get_drvdata(dev);
1302 + if (sscanf(buf, "%u", &enable) != 1)
1305 + retu_headset_det_enable(hs);
1307 + retu_headset_det_disable(hs);
1311 +static DEVICE_ATTR(enable_det, S_IRUGO | S_IWUSR | S_IWGRP,
1312 + retu_headset_enable_det_show,
1313 + retu_headset_enable_det_store);
1315 +static irqreturn_t retu_headset_hook_interrupt(int irq, void *_hs)
1317 + struct retu_headset *hs = _hs;
1318 + unsigned long flags;
1320 + spin_lock_irqsave(&hs->lock, flags);
1321 + if (!hs->pressed) {
1322 + /* Headset button was just pressed down. */
1324 + input_report_key(hs->idev, RETU_HEADSET_KEY, 1);
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));
1331 + return IRQ_HANDLED;
1334 +static void retu_headset_enable_timer(unsigned long arg)
1336 + struct retu_headset *hs = (struct retu_headset *) arg;
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));
1343 +static void retu_headset_detect_timer(unsigned long arg)
1345 + struct retu_headset *hs = (struct retu_headset *) arg;
1346 + unsigned long flags;
1348 + spin_lock_irqsave(&hs->lock, flags);
1349 + if (hs->pressed) {
1351 + input_report_key(hs->idev, RETU_HEADSET_KEY, 0);
1353 + spin_unlock_irqrestore(&hs->lock, flags);
1356 +static int __init retu_headset_probe(struct platform_device *pdev)
1358 + struct retu_headset *hs;
1362 + hs = kzalloc(sizeof(*hs), GFP_KERNEL);
1366 + hs->dev = &pdev->dev;
1368 + hs->idev = input_allocate_device();
1369 + if (hs->idev == NULL) {
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);
1381 + r = device_create_file(&pdev->dev, &dev_attr_hookdet);
1384 + r = device_create_file(&pdev->dev, &dev_attr_enable);
1387 + r = device_create_file(&pdev->dev, &dev_attr_enable_det);
1390 + platform_set_drvdata(pdev, hs);
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);
1399 + irq = platform_get_irq(pdev, 0);
1402 + r = request_threaded_irq(irq, NULL, retu_headset_hook_interrupt, 0,
1405 + dev_err(&pdev->dev, "hookdet IRQ not available\n");
1411 + device_remove_file(&pdev->dev, &dev_attr_enable_det);
1413 + device_remove_file(&pdev->dev, &dev_attr_enable);
1415 + device_remove_file(&pdev->dev, &dev_attr_hookdet);
1417 + input_unregister_device(hs->idev);
1419 + input_free_device(hs->idev);
1425 +static int retu_headset_remove(struct platform_device *pdev)
1427 + struct retu_headset *hs = platform_get_drvdata(pdev);
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);
1441 +static int retu_headset_suspend(struct platform_device *pdev,
1442 + pm_message_t mesg)
1444 + struct retu_headset *hs = platform_get_drvdata(pdev);
1446 + mutex_lock(&hs->mutex);
1447 + if (hs->bias_enabled)
1448 + retu_headset_set_bias(hs, 0);
1449 + mutex_unlock(&hs->mutex);
1454 +static int retu_headset_resume(struct platform_device *pdev)
1456 + struct retu_headset *hs = platform_get_drvdata(pdev);
1458 + mutex_lock(&hs->mutex);
1459 + if (hs->bias_enabled)
1460 + retu_headset_set_bias(hs, 1);
1461 + mutex_unlock(&hs->mutex);
1466 +static struct platform_driver retu_headset_driver = {
1467 + .remove = retu_headset_remove,
1468 + .suspend = retu_headset_suspend,
1469 + .resume = retu_headset_resume,
1471 + .name = "retu-headset",
1475 +static int __init retu_headset_init(void)
1477 + return platform_driver_probe(&retu_headset_driver, retu_headset_probe);
1480 +static void __exit retu_headset_exit(void)
1482 + platform_driver_unregister(&retu_headset_driver);
1485 +module_init(retu_headset_init);
1486 +module_exit(retu_headset_exit);
1488 +MODULE_DESCRIPTION("Retu/Vilma headset detection");
1489 +MODULE_LICENSE("GPL");
1490 +MODULE_AUTHOR("Juha Yrjölä");
1491 Index: linux-3.1/drivers/cbus/retu-pwrbutton.c
1492 ===================================================================
1493 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1494 +++ linux-3.1/drivers/cbus/retu-pwrbutton.c 2011-10-30 00:48:42.989047642 +0200
1497 + * drivers/cbus/retu-pwrbutton.c
1499 + * Driver for sending retu power button event to input-layer
1501 + * Copyright (C) 2004-2010 Nokia Corporation
1504 + * Ari Saastamoinen <ari.saastamoinen@elektrobit.com>
1505 + * Juha Yrjola <juha.yrjola@solidboot.com>
1507 + * Contact: Felipe Balbi <felipe.balbi@nokia.com>
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.
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.
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
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>
1537 +#define RETU_STATUS_PWRONX (1 << 5)
1539 +#define PWRBTN_DELAY 20
1540 +#define PWRBTN_UP 0
1541 +#define PWRBTN_PRESSED 1
1543 +struct retu_pwrbutton {
1544 + struct input_dev *idev;
1545 + struct device *dev;
1551 +static irqreturn_t retubutton_irq(int irq, void *_pwr)
1553 + struct retu_pwrbutton *pwr = _pwr;
1556 + if (retu_read_reg(pwr->dev, RETU_REG_STATUS) & RETU_STATUS_PWRONX)
1557 + state = PWRBTN_UP;
1559 + state = PWRBTN_PRESSED;
1561 + if (pwr->state != state) {
1562 + input_report_key(pwr->idev, KEY_POWER, state);
1563 + input_sync(pwr->idev);
1564 + pwr->state = state;
1567 + return IRQ_HANDLED;
1570 +static int __init retubutton_probe(struct platform_device *pdev)
1572 + struct retu_pwrbutton *pwr;
1575 + pwr = kzalloc(sizeof(*pwr), GFP_KERNEL);
1577 + dev_err(&pdev->dev, "not enough memory\n");
1582 + pwr->dev = &pdev->dev;
1583 + pwr->irq = platform_get_irq(pdev, 0);
1584 + platform_set_drvdata(pdev, pwr);
1586 + ret = request_threaded_irq(pwr->irq, NULL, retubutton_irq, 0,
1587 + "retu-pwrbutton", pwr);
1589 + dev_err(&pdev->dev, "Cannot allocate irq\n");
1593 + pwr->idev = input_allocate_device();
1595 + dev_err(&pdev->dev, "can't allocate input device\n");
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";
1604 + ret = input_register_device(pwr->idev);
1606 + dev_err(&pdev->dev, "failed to register input device\n");
1613 + input_free_device(pwr->idev);
1616 + free_irq(pwr->irq, pwr);
1625 +static int __exit retubutton_remove(struct platform_device *pdev)
1627 + struct retu_pwrbutton *pwr = platform_get_drvdata(pdev);
1629 + free_irq(pwr->irq, pwr);
1630 + input_unregister_device(pwr->idev);
1631 + input_free_device(pwr->idev);
1637 +static struct platform_driver retu_pwrbutton_driver = {
1638 + .remove = __exit_p(retubutton_remove),
1640 + .name = "retu-pwrbutton",
1644 +static int __init retubutton_init(void)
1646 + return platform_driver_probe(&retu_pwrbutton_driver, retubutton_probe);
1648 +module_init(retubutton_init);
1650 +static void __exit retubutton_exit(void)
1652 + platform_driver_unregister(&retu_pwrbutton_driver);
1654 +module_exit(retubutton_exit);
1656 +MODULE_DESCRIPTION("Retu Power Button");
1657 +MODULE_LICENSE("GPL");
1658 +MODULE_AUTHOR("Ari Saastamoinen");
1659 +MODULE_AUTHOR("Felipe Balbi <felipe.balbi@nokia.com>");
1661 Index: linux-3.1/drivers/cbus/retu-rtc.c
1662 ===================================================================
1663 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1664 +++ linux-3.1/drivers/cbus/retu-rtc.c 2011-10-30 00:48:42.989047642 +0200
1667 + * drivers/cbus/retu-rtc.c
1669 + * Support for Retu RTC
1671 + * Copyright (C) 2004, 2005 Nokia Corporation
1673 + * Written by Paul Mundt <paul.mundt@nokia.com> and
1674 + * Igor Stoppa <igor.stoppa@nokia.com>
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).
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.
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.
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.
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
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>
1715 + struct mutex mutex;
1716 + struct device *dev;
1717 + struct rtc_device *rtc;
1719 + u16 alarm_expired;
1724 +static void retu_rtc_do_reset(struct retu_rtc *rtc)
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);
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);
1739 + rtc->alarm_expired = 0;
1742 +static irqreturn_t retu_rtc_interrupt(int irq, void *_rtc)
1744 + struct retu_rtc *rtc = _rtc;
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);
1751 + return IRQ_HANDLED;
1754 +static int retu_rtc_init_irq(struct retu_rtc *rtc)
1759 + irq = platform_get_irq(to_platform_device(rtc->dev), 0);
1760 + rtc->irq_rtcs = irq;
1762 + irq = platform_get_irq(to_platform_device(rtc->dev), 1);
1763 + rtc->irq_rtca = irq;
1765 + ret = request_threaded_irq(rtc->irq_rtcs, NULL, retu_rtc_interrupt,
1770 + ret = request_threaded_irq(rtc->irq_rtca, NULL, retu_rtc_interrupt,
1773 + free_irq(rtc->irq_rtcs, rtc);
1780 +static int retu_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
1782 + struct retu_rtc *rtc = dev_get_drvdata(dev);
1785 + mutex_lock(&rtc->mutex);
1787 + chmar = ((alm->time.tm_hour & 0x1f) << 8) | (alm->time.tm_min & 0x3f);
1788 + retu_write_reg(rtc->dev, RETU_REG_RTCHMAR, chmar);
1790 + mutex_unlock(&rtc->mutex);
1795 +static int retu_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
1797 + struct retu_rtc *rtc = dev_get_drvdata(dev);
1800 + mutex_lock(&rtc->mutex);
1802 + chmar = retu_read_reg(rtc->dev, RETU_REG_RTCHMAR);
1804 + alm->time.tm_hour = (chmar >> 8) & 0x1f;
1805 + alm->time.tm_min = chmar & 0x3f;
1806 + alm->enabled = !!rtc->alarm_expired;
1808 + mutex_unlock(&rtc->mutex);
1813 +static int retu_rtc_set_time(struct device *dev, struct rtc_time *tm)
1815 + struct retu_rtc *rtc = dev_get_drvdata(dev);
1819 + dsr = ((tm->tm_mday & 0xff) << 8) | (tm->tm_hour & 0xff);
1820 + hmr = ((tm->tm_min & 0xff) << 8) | (tm->tm_sec & 0xff);
1822 + mutex_lock(&rtc->mutex);
1824 + retu_write_reg(rtc->dev, RETU_REG_RTCDSR, dsr);
1825 + retu_write_reg(rtc->dev, RETU_REG_RTCHMR, hmr);
1827 + mutex_unlock(&rtc->mutex);
1832 +static int retu_rtc_read_time(struct device *dev, struct rtc_time *tm)
1834 + struct retu_rtc *rtc = dev_get_drvdata(dev);
1839 + * DSR holds days and hours
1840 + * HMR hols minutes and seconds
1842 + * both are 16 bit registers with 8-bit for each field.
1845 + mutex_lock(&rtc->mutex);
1847 + dsr = retu_read_reg(rtc->dev, RETU_REG_RTCDSR);
1848 + hmr = retu_read_reg(rtc->dev, RETU_REG_RTCHMR);
1850 + tm->tm_sec = hmr & 0xff;
1851 + tm->tm_min = hmr >> 8;
1852 + tm->tm_hour = dsr & 0xff;
1853 + tm->tm_mday = dsr >> 8;
1855 + mutex_unlock(&rtc->mutex);
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,
1867 +static int __init retu_rtc_probe(struct platform_device *pdev)
1869 + struct retu_rtc *rtc;
1872 + rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
1874 + dev_err(&pdev->dev, "not enough memory\n");
1879 + rtc->dev = &pdev->dev;
1880 + platform_set_drvdata(pdev, rtc);
1881 + mutex_init(&rtc->mutex);
1883 + rtc->alarm_expired = retu_read_reg(rtc->dev, RETU_REG_IDR) &
1884 + (0x1 << RETU_INT_RTCA);
1886 + r = retu_rtc_init_irq(rtc);
1888 + dev_err(&pdev->dev, "failed to request retu irq\n");
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);
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");
1906 + free_irq(rtc->irq_rtcs, rtc);
1907 + free_irq(rtc->irq_rtca, rtc);
1916 +static int __devexit retu_rtc_remove(struct platform_device *pdev)
1918 + struct retu_rtc *rtc = platform_get_drvdata(pdev);
1920 + free_irq(rtc->irq_rtcs, rtc);
1921 + free_irq(rtc->irq_rtca, rtc);
1922 + rtc_device_unregister(rtc->rtc);
1928 +static struct platform_driver retu_rtc_driver = {
1929 + .remove = __exit_p(retu_rtc_remove),
1931 + .name = "retu-rtc",
1935 +static int __init retu_rtc_init(void)
1937 + return platform_driver_probe(&retu_rtc_driver, retu_rtc_probe);
1939 +module_init(retu_rtc_init);
1941 +static void __exit retu_rtc_exit(void)
1943 + platform_driver_unregister(&retu_rtc_driver);
1945 +module_exit(retu_rtc_exit);
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>");
1953 Index: linux-3.1/drivers/cbus/retu-wdt.c
1954 ===================================================================
1955 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1956 +++ linux-3.1/drivers/cbus/retu-wdt.c 2011-10-30 00:48:42.989047642 +0200
1959 + * drivers/cbus/retu-wdt.c
1961 + * Driver for Retu watchdog
1963 + * Copyright (C) 2004, 2005 Nokia Corporation
1965 + * Written by Amit Kucheria <amit.kucheria@nokia.com>
1967 + * Cleanups by Michael Buesch <mb@bu3sch.de> (C) 2011
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.
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.
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
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>
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>
1998 +#include <asm/uaccess.h>
2000 +#include <plat/prcm.h>
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
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;
2020 +static inline void _retu_modify_counter(struct retu_wdt_dev *wdev,
2023 + retu_write_reg(wdev->dev, RETU_REG_WATCHDOG, (u16)new);
2026 +static int retu_modify_counter(struct retu_wdt_dev *wdev, unsigned int new)
2028 + if (new < RETU_WDT_MIN_TIMER || new > RETU_WDT_MAX_TIMER)
2031 + mutex_lock(&wdev->mutex);
2032 + wdev->period_val = new;
2033 + _retu_modify_counter(wdev, wdev->period_val);
2034 + mutex_unlock(&wdev->mutex);
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.
2044 +static void retu_wdt_ping_enable(struct retu_wdt_dev *wdev)
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));
2051 +static void retu_wdt_ping_disable(struct retu_wdt_dev *wdev)
2053 + _retu_modify_counter(wdev, RETU_WDT_MAX_TIMER);
2054 + cancel_delayed_work_sync(&wdev->ping_work);
2057 +static void retu_wdt_ping_work(struct work_struct *work)
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);
2064 +static int retu_wdt_open(struct inode *inode, struct file *file)
2066 + struct miscdevice *mdev = file->private_data;
2067 + struct retu_wdt_dev *wdev = container_of(mdev, struct retu_wdt_dev, miscdev);
2069 + if (test_and_set_bit(0, &wdev->users))
2072 + retu_wdt_ping_disable(wdev);
2074 + return nonseekable_open(inode, file);
2077 +static int retu_wdt_release(struct inode *inode, struct file *file)
2079 + struct miscdevice *mdev = file->private_data;
2080 + struct retu_wdt_dev *wdev = container_of(mdev, struct retu_wdt_dev, miscdev);
2082 +#ifndef CONFIG_WATCHDOG_NOWAYOUT
2083 + retu_wdt_ping_enable(wdev);
2085 + clear_bit(0, &wdev->users);
2090 +static ssize_t retu_wdt_write(struct file *file, const char __user *data,
2091 + size_t len, loff_t *ppos)
2093 + struct miscdevice *mdev = file->private_data;
2094 + struct retu_wdt_dev *wdev = container_of(mdev, struct retu_wdt_dev, miscdev);
2097 + retu_modify_counter(wdev, RETU_WDT_MAX_TIMER);
2102 +static long retu_wdt_ioctl(struct file *file, unsigned int cmd,
2103 + unsigned long arg)
2105 + struct miscdevice *mdev = file->private_data;
2106 + struct retu_wdt_dev *wdev = container_of(mdev, struct retu_wdt_dev, miscdev);
2109 + static const struct watchdog_info ident = {
2110 + .identity = "Retu Watchdog",
2111 + .options = WDIOF_SETTIMEOUT,
2112 + .firmware_version = 0,
2118 + case WDIOC_GETSUPPORT:
2119 + return copy_to_user((struct watchdog_info __user *)arg, &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);
2133 + case WDIOC_SETTIMEOUT:
2134 + if (get_user(new_margin, (int __user *)arg))
2136 + retu_modify_counter(wdev, new_margin);
2137 + /* Fall through */
2138 + case WDIOC_GETTIMEOUT:
2139 + return put_user(wdev->period_val, (int __user *)arg);
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,
2153 +static int __init retu_wdt_probe(struct platform_device *pdev)
2155 + struct retu_wdt_dev *wdev;
2158 + wdev = kzalloc(sizeof(struct retu_wdt_dev), GFP_KERNEL);
2162 + wdev->dev = &pdev->dev;
2163 + wdev->period_val = RETU_WDT_DEFAULT_TIMER;
2164 + mutex_init(&wdev->mutex);
2166 + platform_set_drvdata(pdev, wdev);
2168 + wdev->miscdev.parent = &pdev->dev;
2169 + wdev->miscdev.minor = WATCHDOG_MINOR;
2170 + wdev->miscdev.name = "watchdog";
2171 + wdev->miscdev.fops = &retu_wdt_fops;
2173 + ret = misc_register(&wdev->miscdev);
2175 + goto err_free_wdev;
2177 + INIT_DELAYED_WORK(&wdev->ping_work, retu_wdt_ping_work);
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);
2184 + retu_wdt_ping_enable(wdev);
2195 +static int __devexit retu_wdt_remove(struct platform_device *pdev)
2197 + struct retu_wdt_dev *wdev;
2199 + wdev = platform_get_drvdata(pdev);
2200 + misc_deregister(&wdev->miscdev);
2201 + cancel_delayed_work_sync(&wdev->ping_work);
2207 +static struct platform_driver retu_wdt_driver = {
2208 + .remove = __exit_p(retu_wdt_remove),
2210 + .name = "retu-wdt",
2214 +static int __init retu_wdt_init(void)
2216 + return platform_driver_probe(&retu_wdt_driver, retu_wdt_probe);
2219 +static void __exit retu_wdt_exit(void)
2221 + platform_driver_unregister(&retu_wdt_driver);
2224 +module_init(retu_wdt_init);
2225 +module_exit(retu_wdt_exit);
2227 +MODULE_DESCRIPTION("Retu WatchDog");
2228 +MODULE_AUTHOR("Amit Kucheria");
2229 +MODULE_LICENSE("GPL");
2230 Index: linux-3.1/drivers/cbus/tahvo.c
2231 ===================================================================
2232 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
2233 +++ linux-3.1/drivers/cbus/tahvo.c 2011-10-30 00:48:42.989047642 +0200
2236 + * drivers/cbus/tahvo.c
2238 + * Support functions for Tahvo ASIC
2240 + * Copyright (C) 2004, 2005 Nokia Corporation
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>
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.
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.
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
2260 +#include <linux/module.h>
2261 +#include <linux/init.h>
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>
2278 + struct mutex mutex;
2279 + struct device *dev;
2288 + unsigned int mask_pending:1;
2289 + unsigned int ack_pending:1;
2290 + unsigned int is_betty:1;
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
2298 +static int __tahvo_read_reg(struct tahvo *tahvo, unsigned reg)
2300 + return cbus_read_reg(tahvo->dev, CBUS_TAHVO_DEVICE_ID, reg);
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
2309 +static void __tahvo_write_reg(struct tahvo *tahvo, unsigned reg, u16 val)
2311 + cbus_write_reg(tahvo->dev, CBUS_TAHVO_DEVICE_ID, reg, val);
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
2319 + * This function returns the contents of the specified register
2321 +int tahvo_read_reg(struct device *child, unsigned reg)
2323 + struct tahvo *tahvo = dev_get_drvdata(child->parent);
2325 + return __tahvo_read_reg(tahvo, reg);
2327 +EXPORT_SYMBOL(tahvo_read_reg);
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
2335 + * This function writes a value to the specified register
2337 +void tahvo_write_reg(struct device *child, unsigned reg, u16 val)
2339 + struct tahvo *tahvo = dev_get_drvdata(child->parent);
2341 + __tahvo_write_reg(tahvo, reg, val);
2343 +EXPORT_SYMBOL(tahvo_write_reg);
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
2351 + * This function sets and clears the specified Tahvo register bits atomically
2353 +void tahvo_set_clear_reg_bits(struct device *child, unsigned reg, u16 set,
2356 + struct tahvo *tahvo = dev_get_drvdata(child->parent);
2359 + mutex_lock(&tahvo->mutex);
2360 + w = __tahvo_read_reg(tahvo, reg);
2363 + __tahvo_write_reg(tahvo, reg, w);
2364 + mutex_unlock(&tahvo->mutex);
2367 +static irqreturn_t tahvo_irq_handler(int irq, void *_tahvo)
2369 + struct tahvo *tahvo = _tahvo;
2373 + id = __tahvo_read_reg(tahvo, TAHVO_REG_IDR);
2374 + im = __tahvo_read_reg(tahvo, TAHVO_REG_IMR);
2378 + dev_vdbg(tahvo->dev, "No IRQ, spurious ?\n");
2383 + unsigned long pending = __ffs(id);
2386 + id &= ~BIT(pending);
2387 + irq = pending + tahvo->irq_base;
2388 + handle_nested_irq(irq);
2391 + return IRQ_HANDLED;
2394 +/* -------------------------------------------------------------------------- */
2396 +static void tahvo_irq_bus_lock(struct irq_data *data)
2398 + struct tahvo *tahvo = irq_data_get_irq_chip_data(data);
2400 + mutex_lock(&tahvo->mutex);
2403 +static void tahvo_irq_bus_sync_unlock(struct irq_data *data)
2405 + struct tahvo *tahvo = irq_data_get_irq_chip_data(data);
2407 + if (tahvo->mask_pending) {
2408 + __tahvo_write_reg(tahvo, TAHVO_REG_IMR, tahvo->mask);
2409 + tahvo->mask_pending = false;
2412 + if (tahvo->ack_pending) {
2413 + __tahvo_write_reg(tahvo, TAHVO_REG_IDR, tahvo->ack);
2414 + tahvo->ack_pending = false;
2417 + mutex_unlock(&tahvo->mutex);
2420 +static void tahvo_irq_mask(struct irq_data *data)
2422 + struct tahvo *tahvo = irq_data_get_irq_chip_data(data);
2423 + int irq = data->irq;
2425 + tahvo->mask |= (1 << (irq - tahvo->irq_base));
2426 + tahvo->mask_pending = true;
2429 +static void tahvo_irq_unmask(struct irq_data *data)
2431 + struct tahvo *tahvo = irq_data_get_irq_chip_data(data);
2432 + int irq = data->irq;
2434 + tahvo->mask &= ~(1 << (irq - tahvo->irq_base));
2435 + tahvo->mask_pending = true;
2438 +static void tahvo_irq_ack(struct irq_data *data)
2440 + struct tahvo *tahvo = irq_data_get_irq_chip_data(data);
2441 + int irq = data->irq;
2443 + tahvo->ack |= (1 << (irq - tahvo->irq_base));
2444 + tahvo->ack_pending = true;
2447 +static struct irq_chip tahvo_irq_chip = {
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,
2456 +static inline void tahvo_irq_setup(int irq)
2459 + set_irq_flags(irq, IRQF_VALID);
2461 + irq_set_noprobe(irq);
2465 +static void tahvo_irq_init(struct tahvo *tahvo)
2467 + int base = tahvo->irq_base;
2468 + int end = tahvo->irq_end;
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);
2480 +/* -------------------------------------------------------------------------- */
2482 +static struct resource generic_resources[] = {
2484 + .start = -EINVAL, /* fixed later */
2485 + .flags = IORESOURCE_IRQ,
2489 +static struct device *tahvo_allocate_child(const char *name,
2490 + struct device *parent, int irq)
2492 + struct platform_device *pdev;
2495 + pdev = platform_device_alloc(name, -1);
2497 + dev_dbg(parent, "can't allocate %s\n", name);
2501 + pdev->dev.parent = parent;
2504 + generic_resources[0].start = irq;
2506 + ret = platform_device_add_resources(pdev, generic_resources,
2507 + ARRAY_SIZE(generic_resources));
2509 + dev_dbg(parent, "can't add resources to %s\n", name);
2514 + ret = platform_device_add(pdev);
2516 + dev_dbg(parent, "can't add %s\n", name);
2520 + return &pdev->dev;
2523 + platform_device_put(pdev);
2529 +static int tahvo_allocate_children(struct device *parent, int irq_base)
2531 + struct device *child;
2533 + child = tahvo_allocate_child("tahvo-usb", parent,
2534 + irq_base + TAHVO_INT_VBUSON);
2538 + child = tahvo_allocate_child("tahvo-pwm", parent, -1);
2545 +static int __devinit tahvo_probe(struct platform_device *pdev)
2547 + struct tahvo *tahvo;
2553 + tahvo = kzalloc(sizeof(*tahvo), GFP_KERNEL);
2555 + dev_err(&pdev->dev, "not enough memory\n");
2560 + irq = platform_get_irq(pdev, 0);
2561 + platform_set_drvdata(pdev, tahvo);
2563 + mutex_init(&tahvo->mutex);
2565 + ret = irq_alloc_descs(-1, 0, MAX_TAHVO_IRQ_HANDLERS, 0);
2567 + dev_err(&pdev->dev, "failed to allocate IRQ descs\n");
2571 + tahvo->irq_base = ret;
2572 + tahvo->irq_end = ret + MAX_TAHVO_IRQ_HANDLERS;
2573 + tahvo->dev = &pdev->dev;
2576 + tahvo_irq_init(tahvo);
2578 + rev = __tahvo_read_reg(tahvo, TAHVO_REG_ASICR);
2580 + id = (rev >> 8) & 0xff;
2583 + tahvo->is_betty = true;
2585 + ret = tahvo_allocate_children(&pdev->dev, tahvo->irq_base);
2587 + dev_err(&pdev->dev, "failed to allocate children\n");
2591 + dev_err(&pdev->dev, "%s v%d.%d found\n",
2592 + tahvo->is_betty ? "Betty" : "Tahvo",
2593 + (rev >> 4) & 0x0f, rev & 0x0f);
2595 + /* Mask all TAHVO interrupts */
2596 + __tahvo_write_reg(tahvo, TAHVO_REG_IMR, 0xffff);
2598 + ret = request_threaded_irq(irq, NULL, tahvo_irq_handler,
2599 + IRQF_TRIGGER_RISING | IRQF_ONESHOT,
2602 + dev_err(&pdev->dev, "Unable to register IRQ handler\n");
2609 + irq_free_descs(tahvo->irq_base, MAX_TAHVO_IRQ_HANDLERS);
2618 +static int __devexit tahvo_remove(struct platform_device *pdev)
2620 + struct tahvo *tahvo = platform_get_drvdata(pdev);
2623 + irq = platform_get_irq(pdev, 0);
2626 + irq_free_descs(tahvo->irq_base, MAX_TAHVO_IRQ_HANDLERS);
2632 +static struct platform_driver tahvo_driver = {
2633 + .probe = tahvo_probe,
2634 + .remove = __devexit_p(tahvo_remove),
2640 +static int __init tahvo_init(void)
2642 + return platform_driver_register(&tahvo_driver);
2644 +subsys_initcall(tahvo_init);
2646 +static void __exit tahvo_exit(void)
2648 + platform_driver_unregister(&tahvo_driver);
2650 +module_exit(tahvo_exit);
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");
2658 Index: linux-3.1/drivers/cbus/tahvo.h
2659 ===================================================================
2660 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
2661 +++ linux-3.1/drivers/cbus/tahvo.h 2011-10-30 00:48:42.989047642 +0200
2664 + * drivers/cbus/tahvo.h
2666 + * Copyright (C) 2004, 2005 Nokia Corporation
2668 + * Written by Juha Yrjölä <juha.yrjola@nokia.com> and
2669 + * David Weinehall <david.weinehall@nokia.com>
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.
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.
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
2685 +#ifndef __DRIVERS_CBUS_TAHVO_H
2686 +#define __DRIVERS_CBUS_TAHVO_H
2688 +#include <linux/types.h>
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) */
2707 +#define TAHVO_REG_MAX 0x0d
2709 +/* Interrupt sources */
2710 +#define TAHVO_INT_VBUSON 0
2711 +#define TAHVO_INT_BATCURR 7 /* Battery current measure timer */
2713 +#define MAX_TAHVO_IRQ_HANDLERS 8
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,
2720 +#endif /* __DRIVERS_CBUS_TAHVO_H */
2721 Index: linux-3.1/drivers/cbus/tahvo-usb.c
2722 ===================================================================
2723 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
2724 +++ linux-3.1/drivers/cbus/tahvo-usb.c 2011-10-30 00:48:42.989047642 +0200
2727 + * drivers/cbus/tahvo-usb.c
2729 + * Tahvo USB transeiver
2731 + * Copyright (C) 2005-2006 Nokia Corporation
2733 + * Parts copied from drivers/i2c/chips/isp1301_omap.c
2734 + * Copyright (C) 2004 Texas Instruments
2735 + * Copyright (C) 2004 David Brownell
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>
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.
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.
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
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>
2772 +#include <asm/irq.h>
2773 +#include <plat/usb.h>
2778 +#define DRIVER_NAME "tahvo-usb"
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)
2790 +/* bits in OTG_CTRL */
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)
2802 +#if defined(CONFIG_USB_OHCI_HCD) && !defined(CONFIG_USB_OTG)
2803 +#error tahvo-otg.c does not work with OCHI yet!
2806 +#define TAHVO_MODE_HOST 0
2807 +#define TAHVO_MODE_PERIPHERAL 1
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
2814 +#define TAHVO_MODE(tu) TAHVO_MODE_HOST
2818 + struct device *dev;
2819 + struct platform_device *pt_dev;
2820 + struct otg_transceiver otg;
2822 + struct mutex serialize;
2823 +#ifdef CONFIG_USB_OTG
2830 +static struct tahvo_usb *tahvo_usb_device;
2833 + * ---------------------------------------------------------------------------
2834 + * OTG related functions
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 + * ---------------------------------------------------------------------------
2841 +static struct platform_device *tahvo_otg_dev;
2843 +static irqreturn_t omap_otg_irq(int irq, void *arg)
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);
2869 + omap_writew(DRIVER_SWITCH, OTG_IRQ_SRC);
2873 + return IRQ_HANDLED;
2877 +static int tahvo_otg_init(void)
2881 +#ifdef CONFIG_USB_OTG
2882 + if (!tahvo_otg_dev) {
2883 + printk("tahvo-usb: no tahvo_otg_dev\n");
2888 + l = omap_readl(OTG_SYSCON_1);
2889 + l &= ~OTG_IDLE_EN;
2890 + omap_writel(l, OTG_SYSCON_1);
2893 + /* some of these values are board-specific... */
2894 + l = omap_readl(OTG_SYSCON_2);
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);
2906 + omap_writew(DRIVER_SWITCH | OPRT_CHG
2907 + | B_SRP_TMROUT | B_HNP_FAIL
2908 + | A_VBUS_ERR | A_SRP_DETECT | A_REQ_TMROUT,
2910 + l = omap_readl(OTG_SYSCON_2);
2912 + omap_writel(l, OTG_SYSCON_2);
2917 +static int __init omap_otg_probe(struct platform_device *pdev)
2921 + tahvo_otg_dev = pdev;
2922 + ret = tahvo_otg_init();
2924 + printk(KERN_ERR "tahvo-usb: tahvo_otg_init failed\n");
2928 + return request_irq(tahvo_otg_dev->resource[1].start,
2929 + omap_otg_irq, IRQF_DISABLED, DRIVER_NAME,
2930 + tahvo_usb_device);
2933 +static int __exit omap_otg_remove(struct platform_device *pdev)
2935 + free_irq(tahvo_otg_dev->resource[1].start, tahvo_usb_device);
2936 + tahvo_otg_dev = NULL;
2941 +struct platform_driver omap_otg_driver = {
2943 + .name = "omap_otg",
2945 + .remove = __exit_p(omap_otg_remove),
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 + * ---------------------------------------------------------------------------
2955 +static ssize_t vbus_state_show(struct device *device,
2956 + struct device_attribute *attr, char *buf)
2958 + struct tahvo_usb *tu = dev_get_drvdata(device);
2959 + return sprintf(buf, "%d\n", tu->vbus_state);
2961 +static DEVICE_ATTR(vbus_state, 0444, vbus_state_show, NULL);
2963 +int vbus_active = 0;
2965 +static void check_vbus_state(struct tahvo_usb *tu)
2967 + int reg, prev_state;
2969 + reg = tahvo_read_reg(tu->dev, TAHVO_REG_IDSR);
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);
2986 + tu->otg.state = OTG_STATE_B_PERIPHERAL;
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;
2995 + printk("USB cable connected\n");
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;
3003 + case OTG_STATE_A_HOST:
3004 + tu->otg.state = OTG_STATE_A_IDLE;
3009 + printk("USB cable disconnected\n");
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");
3019 +static void tahvo_usb_become_host(struct tahvo_usb *tu)
3023 + /* Clear system and transceiver controlled bits
3024 + * also mark the A-session is always valid */
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);
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;
3037 + check_vbus_state(tu);
3040 +static void tahvo_usb_stop_host(struct tahvo_usb *tu)
3042 + tu->otg.state = OTG_STATE_A_IDLE;
3045 +static void tahvo_usb_become_peripheral(struct tahvo_usb *tu)
3049 + /* Clear system and transceiver controlled bits
3050 + * and enable ID to mark peripheral mode and
3051 + * BSESSEND to mark no Vbus */
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);
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;
3062 + check_vbus_state(tu);
3065 +static void tahvo_usb_stop_peripheral(struct tahvo_usb *tu)
3069 + l = omap_readl(OTG_CTRL);
3070 + l &= ~OTG_BSESSVLD;
3071 + l |= OTG_BSESSEND;
3072 + omap_writel(l, OTG_CTRL);
3074 + if (tu->otg.gadget)
3075 + usb_gadget_vbus_disconnect(tu->otg.gadget);
3076 + tu->otg.state = OTG_STATE_B_IDLE;
3080 +static void tahvo_usb_power_off(struct tahvo_usb *tu)
3085 + /* Disable gadget controller if any */
3086 + if (tu->otg.gadget)
3087 + usb_gadget_vbus_disconnect(tu->otg.gadget);
3089 + /* Disable OTG and interrupts */
3090 + if (TAHVO_MODE(tu) == TAHVO_MODE_PERIPHERAL)
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);
3100 + l = omap_readl(OTG_SYSCON_2);
3102 + omap_writel(l, OTG_SYSCON_2);
3104 + l = omap_readl(OTG_SYSCON_1);
3106 + omap_writel(l, OTG_SYSCON_1);
3108 + /* Power off transceiver */
3109 + tahvo_write_reg(tu->dev, TAHVO_REG_USBR, 0);
3110 + tu->otg.state = OTG_STATE_UNDEFINED;
3114 +static int tahvo_usb_set_power(struct otg_transceiver *dev, unsigned mA)
3116 + struct tahvo_usb *tu = container_of(dev, struct tahvo_usb, otg);
3118 + dev_dbg(&tu->pt_dev->dev, "set_power %d mA\n", mA);
3120 + if (dev->state == OTG_STATE_B_PERIPHERAL) {
3121 + /* REVISIT: Can Tahvo charge battery from VBUS? */
3126 +static int tahvo_usb_set_suspend(struct otg_transceiver *dev, int suspend)
3128 + struct tahvo_usb *tu = container_of(dev, struct tahvo_usb, otg);
3131 + dev_dbg(&tu->pt_dev->dev, "set_suspend\n");
3133 + w = tahvo_read_reg(tu->dev, TAHVO_REG_USBR);
3135 + w &= ~USBR_NSUSPEND;
3137 + w |= USBR_NSUSPEND;
3138 + tahvo_write_reg(tu->dev, TAHVO_REG_USBR, w);
3143 +static int tahvo_usb_start_srp(struct otg_transceiver *dev)
3145 + struct tahvo_usb *tu = container_of(dev, struct tahvo_usb, otg);
3148 + dev_dbg(&tu->pt_dev->dev, "start_srp\n");
3150 + if (!dev || tu->otg.state != OTG_STATE_B_IDLE)
3153 + otg_ctrl = omap_readl(OTG_CTRL);
3154 + if (!(otg_ctrl & OTG_BSESSEND))
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;
3165 +static int tahvo_usb_start_hnp(struct otg_transceiver *otg)
3167 + struct tahvo_usb *tu = container_of(otg, struct tahvo_usb, otg);
3169 + dev_dbg(&tu->pt_dev->dev, "start_hnp\n");
3170 +#ifdef CONFIG_USB_OTG
3171 + /* REVISIT: Add this for OTG */
3176 +static int tahvo_usb_set_host(struct otg_transceiver *otg, struct usb_bus *host)
3178 + struct tahvo_usb *tu = container_of(otg, struct tahvo_usb, otg);
3181 + dev_dbg(&tu->pt_dev->dev, "set_host %p\n", host);
3186 +#if defined(CONFIG_USB_OTG) || !defined(CONFIG_USB_GADGET_OMAP)
3188 + mutex_lock(&tu->serialize);
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);
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);
3202 + if (TAHVO_MODE(tu) == TAHVO_MODE_HOST) {
3203 + tu->otg.host = NULL;
3204 + tahvo_usb_become_host(tu);
3207 + tu->otg.host = host;
3209 + mutex_unlock(&tu->serialize);
3211 + /* No host mode configured, so do not allow host controlled to be set */
3218 +static int tahvo_usb_set_peripheral(struct otg_transceiver *otg, struct usb_gadget *gadget)
3220 + struct tahvo_usb *tu = container_of(otg, struct tahvo_usb, otg);
3222 + dev_dbg(&tu->pt_dev->dev, "set_peripheral %p\n", gadget);
3227 +#if defined(CONFIG_USB_OTG) || defined(CONFIG_USB_GADGET_OMAP)
3229 + mutex_lock(&tu->serialize);
3232 + if (TAHVO_MODE(tu) == TAHVO_MODE_PERIPHERAL)
3233 + tahvo_usb_power_off(tu);
3234 + tu->otg.gadget = NULL;
3235 + mutex_unlock(&tu->serialize);
3239 + tu->otg.gadget = gadget;
3240 + if (TAHVO_MODE(tu) == TAHVO_MODE_PERIPHERAL)
3241 + tahvo_usb_become_peripheral(tu);
3243 + mutex_unlock(&tu->serialize);
3245 + /* No gadget mode configured, so do not allow host controlled to be set */
3252 +static irqreturn_t tahvo_usb_vbus_interrupt(int irq, void *_tu)
3254 + struct tahvo_usb *tu = _tu;
3256 + check_vbus_state(tu);
3258 + return IRQ_HANDLED;
3261 +#ifdef CONFIG_USB_OTG
3262 +static ssize_t otg_mode_show(struct device *device,
3263 + struct device_attribute *attr, char *buf)
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");
3272 + return sprintf(buf, "unknown\n");
3275 +static ssize_t otg_mode_store(struct device *device,
3276 + struct device_attribute *attr,
3277 + const char *buf, size_t count)
3279 + struct tahvo_usb *tu = dev_get_drvdata(device);
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);
3292 + printk(KERN_INFO "Selected HOST mode: no host controller, powering off.\n");
3293 + tahvo_usb_power_off(tu);
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);
3303 + printk(KERN_INFO "Selected PERIPHERAL mode: no gadget driver, powering off.\n");
3304 + tahvo_usb_power_off(tu);
3309 + mutex_unlock(&tu->serialize);
3313 +static DEVICE_ATTR(otg_mode, 0644, otg_mode_show, otg_mode_store);
3316 +static int __init tahvo_usb_probe(struct platform_device *pdev)
3318 + struct tahvo_usb *tu;
3319 + struct device *dev = &pdev->dev;
3323 + dev_dbg(dev, "probe\n");
3325 + /* Create driver data */
3326 + tu = kzalloc(sizeof(*tu), GFP_KERNEL);
3329 + tahvo_usb_device = tu;
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;
3338 + tu->tahvo_mode = TAHVO_MODE_PERIPHERAL;
3342 + mutex_init(&tu->serialize);
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);
3350 + clk_enable(tu->ick);
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;
3356 + irq = platform_get_irq(pdev, 0);
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);
3363 + printk(KERN_ERR "Could not register Tahvo interrupt for VBUS\n");
3364 + goto err_release_clk;
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);
3373 + printk(KERN_ERR "attribute creation failed: %d\n", ret);
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;
3386 + ret = otg_set_transceiver(&tu->otg);
3388 + printk(KERN_ERR "Cannot register USB transceiver\n");
3389 + goto err_free_irq;
3392 + dev_set_drvdata(dev, tu);
3397 + free_irq(tu->irq, tu);
3399 + clk_disable(tu->ick);
3403 + tahvo_usb_device = NULL;
3408 +static int __exit tahvo_usb_remove(struct platform_device *pdev)
3410 + struct tahvo_usb *tu = platform_get_drvdata(pdev);
3412 + dev_dbg(&pdev->dev, "remove\n");
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);
3421 + clk_disable(tu->ick);
3425 + tahvo_usb_device = NULL;
3430 +static struct platform_driver tahvo_usb_driver = {
3432 + .name = "tahvo-usb",
3434 + .remove = __exit_p(tahvo_usb_remove),
3437 +static int __init tahvo_usb_init(void)
3441 + ret = platform_driver_probe(&tahvo_usb_driver, tahvo_usb_probe);
3445 + ret = platform_driver_probe(&omap_otg_driver, omap_otg_probe);
3447 + platform_driver_unregister(&tahvo_usb_driver);
3454 +subsys_initcall(tahvo_usb_init);
3456 +static void __exit tahvo_usb_exit(void)
3458 + platform_driver_unregister(&omap_otg_driver);
3459 + platform_driver_unregister(&tahvo_usb_driver);
3461 +module_exit(tahvo_usb_exit);
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/drivers/Makefile
3467 ===================================================================
3468 --- linux-3.1.orig/drivers/Makefile 2011-10-30 00:48:29.709056726 +0200
3469 +++ linux-3.1/drivers/Makefile 2011-10-30 00:48:42.989047642 +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/drivers/Kconfig
3480 ===================================================================
3481 --- linux-3.1.orig/drivers/Kconfig 2011-10-30 00:48:29.653056764 +0200
3482 +++ linux-3.1/drivers/Kconfig 2011-10-30 00:48:42.989047642 +0200
3483 @@ -2,6 +2,8 @@ menu "Device Drivers"
3485 source "drivers/base/Kconfig"
3487 +source "drivers/cbus/Kconfig"
3489 source "drivers/connector/Kconfig"
3491 source "drivers/mtd/Kconfig"