2 +++ b/drivers/cbus/cbus.c
5 + * drivers/cbus/cbus.c
7 + * Support functions for CBUS serial protocol
9 + * Copyright (C) 2004-2010 Nokia Corporation
10 + * Contact: Felipe Balbi <felipe.balbi@nokia.com>
12 + * Written by Juha Yrjölä <juha.yrjola@nokia.com>,
13 + * David Weinehall <david.weinehall@nokia.com>, and
14 + * Mikko Ylinen <mikko.k.ylinen@nokia.com>
16 + * Several updates and cleanups by Felipe Balbi <felipe.balbi@nokia.com>
18 + * This file is subject to the terms and conditions of the GNU General
19 + * Public License. See the file "COPYING" in the main directory of this
20 + * archive for more details.
22 + * This program is distributed in the hope that it will be useful,
23 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 + * GNU General Public License for more details.
27 + * You should have received a copy of the GNU General Public License
28 + * along with this program; if not, write to the Free Software
29 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 +#include <linux/device.h>
33 +#include <linux/init.h>
34 +#include <linux/kernel.h>
35 +#include <linux/export.h>
36 +#include <linux/module.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>");
340 +++ b/drivers/cbus/cbus.h
343 + * drivers/cbus/cbus.h
345 + * Copyright (C) 2004, 2005 Nokia Corporation
347 + * Written by Juha Yrjölä <juha.yrjola@nokia.com> and
348 + * David Weinehall <david.weinehall@nokia.com>
350 + * This file is subject to the terms and conditions of the GNU General
351 + * Public License. See the file "COPYING" in the main directory of this
352 + * archive for more details.
354 + * This program is distributed in the hope that it will be useful,
355 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
356 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
357 + * GNU General Public License for more details.
359 + * You should have received a copy of the GNU General Public License
360 + * along with this program; if not, write to the Free Software
361 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
364 +#ifndef __DRIVERS_CBUS_CBUS_H
365 +#define __DRIVERS_CBUS_CBUS_H
367 +extern int cbus_read_reg(struct device *, unsigned dev, unsigned reg);
368 +extern int cbus_write_reg(struct device *, unsigned dev, unsigned reg,
371 +#endif /* __DRIVERS_CBUS_CBUS_H */
373 +++ b/drivers/cbus/Kconfig
376 +# CBUS device configuration
382 + bool "CBUS support on OMAP"
384 + CBUS is a proprietary serial protocol by Nokia. It is mainly
385 + used for accessing Energy Management auxiliary chips.
387 + If you want CBUS support, you should say Y here.
391 + bool "Support for Tahvo"
393 + Tahvo is a mixed signal ASIC with some system features
395 + If you want Tahvo support, you should say Y here.
399 +config CBUS_TAHVO_USB
401 + depends on ARCH_OMAP
402 + select USB_OTG_UTILS
403 + tristate "Support for Tahvo USB transceiver"
405 + If you want Tahvo support for USB transceiver, say Y or M here.
407 +config CBUS_TAHVO_USB_HOST_BY_DEFAULT
408 + depends on CBUS_TAHVO_USB && USB_OTG
409 + boolean "Device in USB host mode by default"
411 + Say Y here, if you want the device to enter USB host mode
412 + by default on bootup.
418 + bool "Support for Retu"
420 + Retu is a mixed signal ASIC with some system features
422 + If you want Retu support, you should say Y here.
426 +config CBUS_RETU_POWERBUTTON
428 + bool "Support for Retu power button"
430 + The power button on Nokia 770 is connected to the Retu ASIC.
432 + If you want support for the Retu power button, you should say Y here.
434 +config CBUS_RETU_RTC
435 + depends on RTC_CLASS
436 + depends on ARCH_OMAP
437 + tristate "Support for Retu pseudo-RTC"
439 + Say Y here if you want support for the device that alleges to be an
440 + RTC in Retu. This will expose a sysfs interface for it.
442 +config CBUS_RETU_WDT
443 + depends on SYSFS && WATCHDOG
444 + depends on ARCH_OMAP
445 + tristate "Support for Retu watchdog timer"
447 + Say Y here if you want support for the watchdog in Retu. This will
448 + expose a sysfs interface to grok it.
450 +config CBUS_RETU_HEADSET
452 + tristate "Support for headset detection with Retu/Vilma"
454 + Say Y here if you want support detecting a headset that's connected
455 + to Retu/Vilma. Detection state and events are exposed through
462 +++ b/drivers/cbus/Makefile
465 +# Makefile for CBUS.
468 +obj-$(CONFIG_CBUS) += cbus.o
469 +obj-$(CONFIG_CBUS_TAHVO) += tahvo.o
470 +obj-$(CONFIG_CBUS_RETU) += retu.o
471 +obj-$(CONFIG_CBUS_TAHVO_USB) += tahvo-usb.o
473 +obj-$(CONFIG_CBUS_RETU_POWERBUTTON) += retu-pwrbutton.o
474 +obj-$(CONFIG_CBUS_RETU_RTC) += retu-rtc.o
475 +obj-$(CONFIG_CBUS_RETU_WDT) += retu-wdt.o
476 +obj-$(CONFIG_CBUS_RETU_HEADSET) += retu-headset.o
478 +++ b/drivers/cbus/retu.c
481 + * drivers/cbus/retu.c
483 + * Support functions for Retu ASIC
485 + * Copyright (C) 2004, 2005 Nokia Corporation
487 + * Written by Juha Yrjölä <juha.yrjola@nokia.com>,
488 + * David Weinehall <david.weinehall@nokia.com>, and
489 + * Mikko Ylinen <mikko.k.ylinen@nokia.com>
491 + * This file is subject to the terms and conditions of the GNU General
492 + * Public License. See the file "COPYING" in the main directory of this
493 + * archive for more details.
495 + * This program is distributed in the hope that it will be useful,
496 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
497 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
498 + * GNU General Public License for more details.
500 + * You should have received a copy of the GNU General Public License
501 + * along with this program; if not, write to the Free Software
502 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
505 +#include <linux/module.h>
506 +#include <linux/init.h>
508 +#include <linux/slab.h>
509 +#include <linux/kernel.h>
510 +#include <linux/errno.h>
511 +#include <linux/device.h>
512 +#include <linux/mutex.h>
513 +#include <linux/irq.h>
514 +#include <linux/interrupt.h>
515 +#include <linux/platform_device.h>
516 +#include <linux/platform_data/cbus.h>
518 +#include <asm/bitops.h>
525 + struct mutex mutex;
526 + struct device *dev;
544 +static struct retu *the_retu;
547 + * __retu_read_reg - Read a value from a register in Retu
548 + * @retu: pointer to retu structure
549 + * @reg: the register address to read from
551 +static int __retu_read_reg(struct retu *retu, unsigned reg)
553 + return cbus_read_reg(retu->dev, retu->devid, reg);
557 + * __retu_write_reg - Writes a value to a register in Retu
558 + * @retu: pointer to retu structure
559 + * @reg: the register address to write to
560 + * @val: the value to write to the register
562 +static void __retu_write_reg(struct retu *retu, unsigned reg, u16 val)
564 + cbus_write_reg(retu->dev, retu->devid, reg, val);
568 + * retu_read_reg - Read a value from a register in Retu
569 + * @child: device pointer for the calling child
570 + * @reg: the register to read from
572 + * This function returns the contents of the specified register
574 +int retu_read_reg(struct device *child, unsigned reg)
576 + struct retu *retu = dev_get_drvdata(child->parent);
578 + return __retu_read_reg(retu, reg);
580 +EXPORT_SYMBOL_GPL(retu_read_reg);
583 + * retu_write_reg - Write a value to a register in Retu
584 + * @child: the pointer to our calling child
585 + * @reg: the register to write to
586 + * @val: the value to write to the register
588 + * This function writes a value to the specified register
590 +void retu_write_reg(struct device *child, unsigned reg, u16 val)
592 + struct retu *retu = dev_get_drvdata(child->parent);
594 + mutex_lock(&retu->mutex);
595 + __retu_write_reg(retu, reg, val);
596 + mutex_unlock(&retu->mutex);
598 +EXPORT_SYMBOL_GPL(retu_write_reg);
601 + * retu_set_clear_reg_bits - helper function to read/set/clear bits
602 + * @child: device pointer to calling child
603 + * @reg: the register address
604 + * @set: mask for setting bits
605 + * @clear: mask for clearing bits
607 +void retu_set_clear_reg_bits(struct device *child, unsigned reg, u16 set,
610 + struct retu *retu = dev_get_drvdata(child->parent);
613 + mutex_lock(&retu->mutex);
614 + w = __retu_read_reg(retu, reg);
617 + __retu_write_reg(retu, reg, w);
618 + mutex_unlock(&retu->mutex);
620 +EXPORT_SYMBOL_GPL(retu_set_clear_reg_bits);
622 +#define ADC_MAX_CHAN_NUMBER 13
625 + * retu_read_adc - Reads AD conversion result
626 + * @child: device pointer to calling child
627 + * @channel: the ADC channel to read from
629 +int retu_read_adc(struct device *child, int channel)
631 + struct retu *retu = dev_get_drvdata(child->parent);
637 + if (channel < 0 || channel > ADC_MAX_CHAN_NUMBER)
640 + mutex_lock(&retu->mutex);
642 + if ((channel == 8) && retu->is_vilma) {
643 + int scr = __retu_read_reg(retu, RETU_REG_ADCSCR);
644 + int ch = (__retu_read_reg(retu, RETU_REG_ADCR) >> 10) & 0xf;
645 + if (((scr & 0xff) != 0) && (ch != 8))
646 + __retu_write_reg(retu, RETU_REG_ADCSCR, (scr & ~0xff));
649 + /* Select the channel and read result */
650 + __retu_write_reg(retu, RETU_REG_ADCR, channel << 10);
651 + res = __retu_read_reg(retu, RETU_REG_ADCR) & 0x3ff;
653 + if (retu->is_vilma)
654 + __retu_write_reg(retu, RETU_REG_ADCR, (1 << 13));
657 + mutex_unlock(&retu->mutex);
661 +EXPORT_SYMBOL_GPL(retu_read_adc);
663 +static irqreturn_t retu_irq_handler(int irq, void *_retu)
665 + struct retu *retu = _retu;
670 + mutex_lock(&retu->mutex);
671 + idr = __retu_read_reg(retu, RETU_REG_IDR);
672 + imr = __retu_read_reg(retu, RETU_REG_IMR);
673 + mutex_unlock(&retu->mutex);
677 + dev_vdbg(retu->dev, "No IRQ, spurious?\n");
682 + unsigned long pending = __ffs(idr);
685 + idr &= ~BIT(pending);
686 + irq = pending + retu->irq_base;
687 + handle_nested_irq(irq);
690 + return IRQ_HANDLED;
693 +/* -------------------------------------------------------------------------- */
695 +static void retu_irq_mask(struct irq_data *data)
697 + struct retu *retu = irq_data_get_irq_chip_data(data);
698 + int irq = data->irq;
700 + retu->mask |= (1 << (irq - retu->irq_base));
701 + retu->mask_pending = true;
704 +static void retu_irq_unmask(struct irq_data *data)
706 + struct retu *retu = irq_data_get_irq_chip_data(data);
707 + int irq = data->irq;
709 + retu->mask &= ~(1 << (irq - retu->irq_base));
710 + retu->mask_pending = true;
714 +static void retu_irq_ack(struct irq_data *data)
716 + struct retu *retu = irq_data_get_irq_chip_data(data);
717 + int irq = data->irq;
719 + retu->ack |= (1 << (irq - retu->irq_base));
720 + retu->ack_pending = true;
723 +static void retu_bus_lock(struct irq_data *data)
725 + struct retu *retu = irq_data_get_irq_chip_data(data);
727 + mutex_lock(&retu->mutex);
730 +static void retu_bus_sync_unlock(struct irq_data *data)
732 + struct retu *retu = irq_data_get_irq_chip_data(data);
734 + if (retu->mask_pending) {
735 + __retu_write_reg(retu, RETU_REG_IMR, retu->mask);
736 + retu->mask_pending = false;
739 + if (retu->ack_pending) {
740 + __retu_write_reg(retu, RETU_REG_IDR, retu->ack);
741 + retu->ack_pending = false;
744 + mutex_unlock(&retu->mutex);
747 +static struct irq_chip retu_irq_chip = {
749 + .irq_bus_lock = retu_bus_lock,
750 + .irq_bus_sync_unlock = retu_bus_sync_unlock,
751 + .irq_mask = retu_irq_mask,
752 + .irq_unmask = retu_irq_unmask,
753 + .irq_ack = retu_irq_ack,
756 +static inline void retu_irq_setup(int irq)
759 + set_irq_flags(irq, IRQF_VALID);
761 + irq_set_noprobe(irq);
765 +static void retu_irq_init(struct retu *retu)
767 + int base = retu->irq_base;
768 + int end = retu->irq_end;
771 + for (irq = base; irq < end; irq++) {
772 + irq_set_chip_data(irq, retu);
773 + irq_set_chip_and_handler(irq, &retu_irq_chip,
774 + handle_simple_irq);
775 + irq_set_nested_thread(irq, 1);
776 + retu_irq_setup(irq);
780 +static void retu_irq_exit(struct retu *retu)
782 + int base = retu->irq_base;
783 + int end = retu->irq_end;
786 + for (irq = base; irq < end; irq++) {
788 + set_irq_flags(irq, 0);
790 + irq_set_chip_and_handler(irq, NULL, NULL);
791 + irq_set_chip_data(irq, NULL);
795 +/* -------------------------------------------------------------------------- */
798 + * retu_power_off - Shut down power to system
800 + * This function puts the system in power off state
802 +static void retu_power_off(void)
804 + struct retu *retu = the_retu;
807 + reg = __retu_read_reg(retu, RETU_REG_CC1);
809 + /* Ignore power button state */
810 + __retu_write_reg(retu, RETU_REG_CC1, reg | 2);
811 + /* Expire watchdog immediately */
812 + __retu_write_reg(retu, RETU_REG_WATCHDOG, 0);
813 + /* Wait for poweroff*/
817 +static struct resource generic_resources[] = {
819 + .start = -EINVAL, /* fixed later */
820 + .flags = IORESOURCE_IRQ,
823 + .start = -EINVAL, /* fixed later */
824 + .flags = IORESOURCE_IRQ,
829 + * retu_allocate_child - Allocates one Retu child
830 + * @name: name of new child
831 + * @parent: parent device for this child
833 +static struct device *retu_allocate_child(char *name, struct device *parent,
834 + int irq_base, int irq1, int irq2, int num)
836 + struct platform_device *pdev;
839 + pdev = platform_device_alloc(name, -1);
841 + dev_dbg(parent, "can't allocate %s\n", name);
845 + pdev->dev.parent = parent;
848 + generic_resources[0].start = irq_base + irq1;
849 + generic_resources[1].start = irq_base + irq2;
851 + status = platform_device_add_resources(pdev,
852 + generic_resources, num);
854 + dev_dbg(parent, "can't add resources to %s\n", name);
859 + status = platform_device_add(pdev);
861 + dev_dbg(parent, "can't add %s\n", name);
868 + platform_device_put(pdev);
874 + * retu_allocate_children - Allocates Retu's children
876 +static int retu_allocate_children(struct device *parent, int irq_base)
878 + struct device *child;
880 + child = retu_allocate_child("retu-pwrbutton", parent, irq_base,
881 + RETU_INT_PWR, -1, 1);
885 + child = retu_allocate_child("retu-headset", parent, irq_base,
886 + RETU_INT_HOOK, -1, 1);
890 + child = retu_allocate_child("retu-rtc", parent, irq_base,
891 + RETU_INT_RTCS, RETU_INT_RTCA, 2);
895 + child = retu_allocate_child("retu-wdt", parent, -1, -1, -1, 0);
903 + * retu_probe - Probe for Retu ASIC
904 + * @dev: the Retu device
906 + * Probe for the Retu ASIC and allocate memory
907 + * for its device-struct if found
909 +static int __devinit retu_probe(struct platform_device *pdev)
912 + struct cbus_retu_platform_data *pdata = pdev->dev.platform_data;
917 + retu = kzalloc(sizeof(*retu), GFP_KERNEL);
919 + dev_err(&pdev->dev, "not enough memory\n");
923 + platform_set_drvdata(pdev, retu);
925 + ret = irq_alloc_descs(-1, 0, MAX_RETU_IRQ_HANDLERS, 0);
927 + dev_err(&pdev->dev, "failed to allocate IRQ descs\n");
931 + retu->irq = platform_get_irq(pdev, 0);
932 + retu->irq_base = ret;
933 + retu->irq_end = ret + MAX_RETU_IRQ_HANDLERS;
934 + retu->devid = pdata->devid;
935 + retu->dev = &pdev->dev;
938 + mutex_init(&retu->mutex);
940 + retu_irq_init(retu);
942 + rev = __retu_read_reg(retu, RETU_REG_ASICR) & 0xff;
943 + if (rev & (1 << 7))
944 + retu->is_vilma = true;
946 + dev_info(&pdev->dev, "%s v%d.%d found\n",
947 + retu->is_vilma ? "Vilma" : "Retu",
948 + (rev >> 4) & 0x07, rev & 0x0f);
950 + /* Mask all RETU interrupts */
951 + __retu_write_reg(retu, RETU_REG_IMR, 0xffff);
953 + ret = request_threaded_irq(retu->irq, NULL, retu_irq_handler,
954 + IRQF_ONESHOT, "retu", retu);
956 + dev_err(&pdev->dev, "Unable to register IRQ handler\n");
960 + irq_set_irq_wake(retu->irq, 1);
962 + /* Register power off function */
963 + pm_power_off = retu_power_off;
965 + ret = retu_allocate_children(&pdev->dev, retu->irq_base);
967 + dev_err(&pdev->dev, "Unable to allocate Retu children\n");
974 + pm_power_off = NULL;
975 + free_irq(retu->irq, retu);
978 + retu_irq_exit(retu);
979 + irq_free_descs(retu->irq_base, MAX_RETU_IRQ_HANDLERS);
989 +static int __devexit retu_remove(struct platform_device *pdev)
991 + struct retu *retu = platform_get_drvdata(pdev);
993 + pm_power_off = NULL;
996 + free_irq(retu->irq, retu);
997 + retu_irq_exit(retu);
998 + irq_free_descs(retu->irq_base, MAX_RETU_IRQ_HANDLERS);
1004 +static struct platform_driver retu_driver = {
1005 + .probe = retu_probe,
1006 + .remove = __devexit_p(retu_remove),
1012 +static int __init retu_init(void)
1014 + return platform_driver_register(&retu_driver);
1016 +subsys_initcall(retu_init);
1018 +static void __exit retu_exit(void)
1020 + platform_driver_unregister(&retu_driver);
1022 +module_exit(retu_exit);
1024 +MODULE_DESCRIPTION("Retu ASIC control");
1025 +MODULE_LICENSE("GPL");
1026 +MODULE_AUTHOR("Juha Yrjölä");
1027 +MODULE_AUTHOR("David Weinehall");
1028 +MODULE_AUTHOR("Mikko Ylinen");
1030 +++ b/drivers/cbus/retu.h
1033 + * drivers/cbus/retu.h
1035 + * Copyright (C) 2004, 2005 Nokia Corporation
1037 + * Written by Juha Yrjölä <juha.yrjola@nokia.com> and
1038 + * David Weinehall <david.weinehall@nokia.com>
1040 + * This file is subject to the terms and conditions of the GNU General
1041 + * Public License. See the file "COPYING" in the main directory of this
1042 + * archive for more details.
1044 + * This program is distributed in the hope that it will be useful,
1045 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1046 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1047 + * GNU General Public License for more details.
1049 + * You should have received a copy of the GNU General Public License
1050 + * along with this program; if not, write to the Free Software
1051 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1054 +#ifndef __DRIVERS_CBUS_RETU_H
1055 +#define __DRIVERS_CBUS_RETU_H
1057 +#include <linux/types.h>
1060 +#define RETU_REG_ASICR 0x00 /* ASIC ID & revision */
1061 +#define RETU_REG_IDR 0x01 /* Interrupt ID */
1062 +#define RETU_REG_IMR 0x02 /* Interrupt mask */
1063 +#define RETU_REG_RTCDSR 0x03 /* RTC seconds register */
1064 +#define RETU_REG_RTCHMR 0x04 /* RTC hours and minutes register */
1065 +#define RETU_REG_RTCHMAR 0x05 /* RTC hours and minutes alarm and time set register */
1066 +#define RETU_REG_RTCCALR 0x06 /* RTC calibration register */
1067 +#define RETU_REG_ADCR 0x08 /* ADC result */
1068 +#define RETU_REG_ADCSCR 0x09 /* ADC sample ctrl */
1069 +#define RETU_REG_CC1 0x0d /* Common control register 1 */
1070 +#define RETU_REG_CC2 0x0e /* Common control register 2 */
1071 +#define RETU_REG_CTRL_CLR 0x0f /* Regulator clear register */
1072 +#define RETU_REG_CTRL_SET 0x10 /* Regulator set register */
1073 +#define RETU_REG_STATUS 0x16 /* Status register */
1074 +#define RETU_REG_STATUS_BATAVAIL 0x0100 /* Battery available */
1075 +#define RETU_REG_STATUS_CHGPLUG 0x1000 /* Charger is plugged in */
1076 +#define RETU_REG_WATCHDOG 0x17 /* Watchdog register */
1077 +#define RETU_REG_AUDTXR 0x18 /* Audio Codec Tx register */
1078 +#define RETU_REG_MAX 0x1f
1080 +/* Interrupt sources */
1081 +#define RETU_INT_PWR 0
1082 +#define RETU_INT_CHAR 1
1083 +#define RETU_INT_RTCS 2
1084 +#define RETU_INT_RTCM 3
1085 +#define RETU_INT_RTCD 4
1086 +#define RETU_INT_RTCA 5
1087 +#define RETU_INT_HOOK 6
1088 +#define RETU_INT_HEAD 7
1089 +#define RETU_INT_ADCS 8
1091 +#define MAX_RETU_IRQ_HANDLERS 16
1094 +#define RETU_ADC_GND 0x00 /* Ground */
1095 +#define RETU_ADC_BSI 0x01 /* Battery Size Indicator */
1096 +#define RETU_ADC_BATTEMP 0x02 /* Battery temperature */
1097 +#define RETU_ADC_CHGVOLT 0x03 /* Charger voltage */
1098 +#define RETU_ADC_HEADSET 0x04 /* Headset detection */
1099 +#define RETU_ADC_HOOKDET 0x05 /* Hook detection */
1100 +#define RETU_ADC_RFGP 0x06 /* RF GP */
1101 +#define RETU_ADC_WBTX 0x07 /* Wideband Tx detection */
1102 +#define RETU_ADC_BATTVOLT 0x08 /* Battery voltage measurement */
1103 +#define RETU_ADC_GND2 0x09 /* Ground */
1104 +#define RETU_ADC_LIGHTSENS 0x0A /* Light sensor */
1105 +#define RETU_ADC_LIGHTTEMP 0x0B /* Light sensor temperature */
1106 +#define RETU_ADC_BKUPVOLT 0x0C /* Backup battery voltage */
1107 +#define RETU_ADC_TEMP 0x0D /* RETU temperature */
1110 +int retu_read_reg(struct device *child, unsigned reg);
1111 +void retu_write_reg(struct device *child, unsigned reg, u16 val);
1112 +void retu_set_clear_reg_bits(struct device *child, unsigned reg, u16 set,
1114 +int retu_read_adc(struct device *child, int channel);
1116 +#endif /* __DRIVERS_CBUS_RETU_H */
1118 +++ b/drivers/cbus/retu-headset.c
1121 + * Retu/Vilma headset detection
1123 + * Copyright (C) 2006 Nokia Corporation
1125 + * Written by Juha Yrjölä
1127 + * This file is subject to the terms and conditions of the GNU General
1128 + * Public License. See the file "COPYING" in the main directory of this
1129 + * archive for more details.
1131 + * This program is distributed in the hope that it will be useful,
1132 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1133 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1134 + * GNU General Public License for more details.
1136 + * You should have received a copy of the GNU General Public License
1137 + * along with this program; if not, write to the Free Software
1138 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1141 +#include <linux/module.h>
1142 +#include <linux/init.h>
1143 +#include <linux/kernel.h>
1144 +#include <linux/irq.h>
1145 +#include <linux/interrupt.h>
1146 +#include <linux/slab.h>
1147 +#include <linux/delay.h>
1148 +#include <linux/input.h>
1149 +#include <linux/platform_device.h>
1153 +#define RETU_ADC_CHANNEL_HOOKDET 0x05
1155 +#define RETU_HEADSET_KEY KEY_PHONE
1157 +struct retu_headset {
1159 + struct mutex mutex;
1160 + struct device *dev;
1161 + struct input_dev *idev;
1162 + unsigned bias_enabled;
1163 + unsigned detection_enabled;
1165 + struct timer_list enable_timer;
1166 + struct timer_list detect_timer;
1170 +static void retu_headset_set_bias(struct retu_headset *hs, int enable)
1173 + retu_set_clear_reg_bits(hs->dev, RETU_REG_AUDTXR,
1174 + (1 << 0) | (1 << 1), 0);
1176 + retu_set_clear_reg_bits(hs->dev, RETU_REG_AUDTXR,
1179 + retu_set_clear_reg_bits(hs->dev, RETU_REG_AUDTXR, 0,
1180 + (1 << 0) | (1 << 1) | (1 << 3));
1184 +static void retu_headset_enable(struct retu_headset *hs)
1186 + mutex_lock(&hs->mutex);
1187 + if (!hs->bias_enabled) {
1188 + hs->bias_enabled = 1;
1189 + retu_headset_set_bias(hs, 1);
1191 + mutex_unlock(&hs->mutex);
1194 +static void retu_headset_disable(struct retu_headset *hs)
1196 + mutex_lock(&hs->mutex);
1197 + if (hs->bias_enabled) {
1198 + hs->bias_enabled = 0;
1199 + retu_headset_set_bias(hs, 0);
1201 + mutex_unlock(&hs->mutex);
1204 +static void retu_headset_det_enable(struct retu_headset *hs)
1206 + mutex_lock(&hs->mutex);
1207 + if (!hs->detection_enabled) {
1208 + hs->detection_enabled = 1;
1209 + retu_set_clear_reg_bits(hs->dev, RETU_REG_CC1,
1210 + (1 << 10) | (1 << 8), 0);
1212 + mutex_unlock(&hs->mutex);
1215 +static void retu_headset_det_disable(struct retu_headset *hs)
1217 + unsigned long flags;
1219 + mutex_lock(&hs->mutex);
1220 + if (hs->detection_enabled) {
1221 + hs->detection_enabled = 0;
1222 + del_timer_sync(&hs->enable_timer);
1223 + del_timer_sync(&hs->detect_timer);
1224 + spin_lock_irqsave(&hs->lock, flags);
1226 + input_report_key(hs->idev, RETU_HEADSET_KEY, 0);
1227 + spin_unlock_irqrestore(&hs->lock, flags);
1228 + retu_set_clear_reg_bits(hs->dev, RETU_REG_CC1, 0,
1229 + (1 << 10) | (1 << 8));
1231 + mutex_unlock(&hs->mutex);
1234 +static ssize_t retu_headset_hookdet_show(struct device *dev,
1235 + struct device_attribute *attr,
1240 + val = retu_read_adc(dev, RETU_ADC_CHANNEL_HOOKDET);
1241 + return sprintf(buf, "%d\n", val);
1244 +static DEVICE_ATTR(hookdet, S_IRUGO, retu_headset_hookdet_show, NULL);
1246 +static ssize_t retu_headset_enable_show(struct device *dev,
1247 + struct device_attribute *attr,
1250 + struct retu_headset *hs = dev_get_drvdata(dev);
1252 + return sprintf(buf, "%u\n", hs->bias_enabled);
1255 +static ssize_t retu_headset_enable_store(struct device *dev,
1256 + struct device_attribute *attr,
1257 + const char *buf, size_t count)
1259 + struct retu_headset *hs = dev_get_drvdata(dev);
1262 + if (sscanf(buf, "%u", &enable) != 1)
1265 + retu_headset_enable(hs);
1267 + retu_headset_disable(hs);
1271 +static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
1272 + retu_headset_enable_show, retu_headset_enable_store);
1274 +static ssize_t retu_headset_enable_det_show(struct device *dev,
1275 + struct device_attribute *attr,
1278 + struct retu_headset *hs = dev_get_drvdata(dev);
1280 + return sprintf(buf, "%u\n", hs->detection_enabled);
1283 +static ssize_t retu_headset_enable_det_store(struct device *dev,
1284 + struct device_attribute *attr,
1285 + const char *buf, size_t count)
1287 + struct retu_headset *hs = dev_get_drvdata(dev);
1290 + if (sscanf(buf, "%u", &enable) != 1)
1293 + retu_headset_det_enable(hs);
1295 + retu_headset_det_disable(hs);
1299 +static DEVICE_ATTR(enable_det, S_IRUGO | S_IWUSR | S_IWGRP,
1300 + retu_headset_enable_det_show,
1301 + retu_headset_enable_det_store);
1303 +static irqreturn_t retu_headset_hook_interrupt(int irq, void *_hs)
1305 + struct retu_headset *hs = _hs;
1306 + unsigned long flags;
1308 + spin_lock_irqsave(&hs->lock, flags);
1309 + if (!hs->pressed) {
1310 + /* Headset button was just pressed down. */
1312 + input_report_key(hs->idev, RETU_HEADSET_KEY, 1);
1314 + spin_unlock_irqrestore(&hs->lock, flags);
1315 + retu_set_clear_reg_bits(hs->dev, RETU_REG_CC1, 0,
1316 + (1 << 10) | (1 << 8));
1317 + mod_timer(&hs->enable_timer, jiffies + msecs_to_jiffies(50));
1319 + return IRQ_HANDLED;
1322 +static void retu_headset_enable_timer(unsigned long arg)
1324 + struct retu_headset *hs = (struct retu_headset *) arg;
1326 + retu_set_clear_reg_bits(hs->dev, RETU_REG_CC1,
1327 + (1 << 10) | (1 << 8), 0);
1328 + mod_timer(&hs->detect_timer, jiffies + msecs_to_jiffies(350));
1331 +static void retu_headset_detect_timer(unsigned long arg)
1333 + struct retu_headset *hs = (struct retu_headset *) arg;
1334 + unsigned long flags;
1336 + spin_lock_irqsave(&hs->lock, flags);
1337 + if (hs->pressed) {
1339 + input_report_key(hs->idev, RETU_HEADSET_KEY, 0);
1341 + spin_unlock_irqrestore(&hs->lock, flags);
1344 +static int __init retu_headset_probe(struct platform_device *pdev)
1346 + struct retu_headset *hs;
1350 + hs = kzalloc(sizeof(*hs), GFP_KERNEL);
1354 + hs->dev = &pdev->dev;
1356 + hs->idev = input_allocate_device();
1357 + if (hs->idev == NULL) {
1361 + hs->idev->name = "retu-headset";
1362 + hs->idev->dev.parent = &pdev->dev;
1363 + set_bit(EV_KEY, hs->idev->evbit);
1364 + set_bit(RETU_HEADSET_KEY, hs->idev->keybit);
1365 + r = input_register_device(hs->idev);
1369 + r = device_create_file(&pdev->dev, &dev_attr_hookdet);
1372 + r = device_create_file(&pdev->dev, &dev_attr_enable);
1375 + r = device_create_file(&pdev->dev, &dev_attr_enable_det);
1378 + platform_set_drvdata(pdev, hs);
1380 + spin_lock_init(&hs->lock);
1381 + mutex_init(&hs->mutex);
1382 + setup_timer(&hs->enable_timer, retu_headset_enable_timer,
1383 + (unsigned long) hs);
1384 + setup_timer(&hs->detect_timer, retu_headset_detect_timer,
1385 + (unsigned long) hs);
1387 + irq = platform_get_irq(pdev, 0);
1390 + r = request_threaded_irq(irq, NULL, retu_headset_hook_interrupt, 0,
1393 + dev_err(&pdev->dev, "hookdet IRQ not available\n");
1399 + device_remove_file(&pdev->dev, &dev_attr_enable_det);
1401 + device_remove_file(&pdev->dev, &dev_attr_enable);
1403 + device_remove_file(&pdev->dev, &dev_attr_hookdet);
1405 + input_unregister_device(hs->idev);
1407 + input_free_device(hs->idev);
1413 +static int retu_headset_remove(struct platform_device *pdev)
1415 + struct retu_headset *hs = platform_get_drvdata(pdev);
1417 + device_remove_file(&pdev->dev, &dev_attr_hookdet);
1418 + device_remove_file(&pdev->dev, &dev_attr_enable);
1419 + device_remove_file(&pdev->dev, &dev_attr_enable_det);
1420 + retu_headset_disable(hs);
1421 + retu_headset_det_disable(hs);
1422 + free_irq(hs->irq, hs);
1423 + input_unregister_device(hs->idev);
1424 + input_free_device(hs->idev);
1429 +static int retu_headset_suspend(struct platform_device *pdev,
1430 + pm_message_t mesg)
1432 + struct retu_headset *hs = platform_get_drvdata(pdev);
1434 + mutex_lock(&hs->mutex);
1435 + if (hs->bias_enabled)
1436 + retu_headset_set_bias(hs, 0);
1437 + mutex_unlock(&hs->mutex);
1442 +static int retu_headset_resume(struct platform_device *pdev)
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, 1);
1449 + mutex_unlock(&hs->mutex);
1454 +static struct platform_driver retu_headset_driver = {
1455 + .remove = retu_headset_remove,
1456 + .suspend = retu_headset_suspend,
1457 + .resume = retu_headset_resume,
1459 + .name = "retu-headset",
1463 +static int __init retu_headset_init(void)
1465 + return platform_driver_probe(&retu_headset_driver, retu_headset_probe);
1468 +static void __exit retu_headset_exit(void)
1470 + platform_driver_unregister(&retu_headset_driver);
1473 +module_init(retu_headset_init);
1474 +module_exit(retu_headset_exit);
1476 +MODULE_DESCRIPTION("Retu/Vilma headset detection");
1477 +MODULE_LICENSE("GPL");
1478 +MODULE_AUTHOR("Juha Yrjölä");
1480 +++ b/drivers/cbus/retu-pwrbutton.c
1483 + * drivers/cbus/retu-pwrbutton.c
1485 + * Driver for sending retu power button event to input-layer
1487 + * Copyright (C) 2004-2010 Nokia Corporation
1490 + * Ari Saastamoinen <ari.saastamoinen@elektrobit.com>
1491 + * Juha Yrjola <juha.yrjola@solidboot.com>
1493 + * Contact: Felipe Balbi <felipe.balbi@nokia.com>
1495 + * This file is subject to the terms and conditions of the GNU General
1496 + * Public License. See the file "COPYING" in the main directory of this
1497 + * archive for more details.
1499 + * This program is distributed in the hope that it will be useful,
1500 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1501 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1502 + * GNU General Public License for more details.
1504 + * You should have received a copy of the GNU General Public License
1505 + * along with this program; if not, write to the Free Software
1506 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1509 +#include <linux/module.h>
1510 +#include <linux/init.h>
1511 +#include <linux/kernel.h>
1512 +#include <linux/errno.h>
1513 +#include <linux/input.h>
1514 +#include <linux/jiffies.h>
1515 +#include <linux/bitops.h>
1516 +#include <linux/irq.h>
1517 +#include <linux/interrupt.h>
1518 +#include <linux/platform_device.h>
1519 +#include <linux/slab.h>
1523 +#define RETU_STATUS_PWRONX (1 << 5)
1525 +#define PWRBTN_DELAY 20
1526 +#define PWRBTN_UP 0
1527 +#define PWRBTN_PRESSED 1
1529 +struct retu_pwrbutton {
1530 + struct input_dev *idev;
1531 + struct device *dev;
1537 +static irqreturn_t retubutton_irq(int irq, void *_pwr)
1539 + struct retu_pwrbutton *pwr = _pwr;
1542 + if (retu_read_reg(pwr->dev, RETU_REG_STATUS) & RETU_STATUS_PWRONX)
1543 + state = PWRBTN_UP;
1545 + state = PWRBTN_PRESSED;
1547 + if (pwr->state != state) {
1548 + input_report_key(pwr->idev, KEY_POWER, state);
1549 + input_sync(pwr->idev);
1550 + pwr->state = state;
1553 + return IRQ_HANDLED;
1556 +static int __init retubutton_probe(struct platform_device *pdev)
1558 + struct retu_pwrbutton *pwr;
1561 + pwr = kzalloc(sizeof(*pwr), GFP_KERNEL);
1563 + dev_err(&pdev->dev, "not enough memory\n");
1568 + pwr->dev = &pdev->dev;
1569 + pwr->irq = platform_get_irq(pdev, 0);
1570 + platform_set_drvdata(pdev, pwr);
1572 + ret = request_threaded_irq(pwr->irq, NULL, retubutton_irq, 0,
1573 + "retu-pwrbutton", pwr);
1575 + dev_err(&pdev->dev, "Cannot allocate irq\n");
1579 + pwr->idev = input_allocate_device();
1581 + dev_err(&pdev->dev, "can't allocate input device\n");
1586 + pwr->idev->evbit[0] = BIT_MASK(EV_KEY);
1587 + pwr->idev->keybit[BIT_WORD(KEY_POWER)] = BIT_MASK(KEY_POWER);
1588 + pwr->idev->name = "retu-pwrbutton";
1590 + ret = input_register_device(pwr->idev);
1592 + dev_err(&pdev->dev, "failed to register input device\n");
1599 + input_free_device(pwr->idev);
1602 + free_irq(pwr->irq, pwr);
1611 +static int __exit retubutton_remove(struct platform_device *pdev)
1613 + struct retu_pwrbutton *pwr = platform_get_drvdata(pdev);
1615 + free_irq(pwr->irq, pwr);
1616 + input_unregister_device(pwr->idev);
1617 + input_free_device(pwr->idev);
1623 +static struct platform_driver retu_pwrbutton_driver = {
1624 + .remove = __exit_p(retubutton_remove),
1626 + .name = "retu-pwrbutton",
1630 +static int __init retubutton_init(void)
1632 + return platform_driver_probe(&retu_pwrbutton_driver, retubutton_probe);
1634 +module_init(retubutton_init);
1636 +static void __exit retubutton_exit(void)
1638 + platform_driver_unregister(&retu_pwrbutton_driver);
1640 +module_exit(retubutton_exit);
1642 +MODULE_DESCRIPTION("Retu Power Button");
1643 +MODULE_LICENSE("GPL");
1644 +MODULE_AUTHOR("Ari Saastamoinen");
1645 +MODULE_AUTHOR("Felipe Balbi <felipe.balbi@nokia.com>");
1648 +++ b/drivers/cbus/retu-rtc.c
1651 + * drivers/cbus/retu-rtc.c
1653 + * Support for Retu RTC
1655 + * Copyright (C) 2004, 2005 Nokia Corporation
1657 + * Written by Paul Mundt <paul.mundt@nokia.com> and
1658 + * Igor Stoppa <igor.stoppa@nokia.com>
1660 + * The Retu RTC is essentially a partial read-only RTC that gives us Retu's
1661 + * idea of what time actually is. It's left as a userspace excercise to map
1662 + * this back to time in the real world and ensure that calibration settings
1663 + * are sane to compensate for any horrible drift (on account of not being able
1664 + * to set the clock to anything).
1666 + * Days are semi-writeable. Namely, Retu will only track 255 days for us
1667 + * consecutively, after which the counter is explicitly stuck at 255 until
1668 + * someone comes along and clears it with a write. In the event that no one
1669 + * comes along and clears it, we no longer have any idea what day it is.
1671 + * This file is subject to the terms and conditions of the GNU General
1672 + * Public License. See the file "COPYING" in the main directory of this
1673 + * archive for more details.
1675 + * This program is distributed in the hope that it will be useful,
1676 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1677 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1678 + * GNU General Public License for more details.
1680 + * You should have received a copy of the GNU General Public License
1681 + * along with this program; if not, write to the Free Software
1682 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1685 +#include <linux/device.h>
1686 +#include <linux/init.h>
1687 +#include <linux/kernel.h>
1688 +#include <linux/slab.h>
1689 +#include <linux/module.h>
1690 +#include <linux/platform_device.h>
1691 +#include <linux/mutex.h>
1692 +#include <linux/rtc.h>
1699 + struct mutex mutex;
1700 + struct device *dev;
1701 + struct rtc_device *rtc;
1703 + u16 alarm_expired;
1708 +static void retu_rtc_do_reset(struct retu_rtc *rtc)
1712 + ccr1 = retu_read_reg(rtc->dev, RETU_REG_CC1);
1713 + /* RTC in reset */
1714 + retu_write_reg(rtc->dev, RETU_REG_CC1, ccr1 | 0x0001);
1715 + /* RTC in normal operating mode */
1716 + retu_write_reg(rtc->dev, RETU_REG_CC1, ccr1 & ~0x0001);
1718 + /* Disable alarm and RTC WD */
1719 + retu_write_reg(rtc->dev, RETU_REG_RTCHMAR, 0x7f3f);
1720 + /* Set Calibration register to default value */
1721 + retu_write_reg(rtc->dev, RETU_REG_RTCCALR, 0x00c0);
1723 + rtc->alarm_expired = 0;
1726 +static irqreturn_t retu_rtc_interrupt(int irq, void *_rtc)
1728 + struct retu_rtc *rtc = _rtc;
1730 + mutex_lock(&rtc->mutex);
1731 + rtc->alarm_expired = 1;
1732 + retu_write_reg(rtc->dev, RETU_REG_RTCHMAR, (24 << 8) | 60);
1733 + mutex_unlock(&rtc->mutex);
1735 + return IRQ_HANDLED;
1738 +static int retu_rtc_init_irq(struct retu_rtc *rtc)
1743 + irq = platform_get_irq(to_platform_device(rtc->dev), 0);
1744 + rtc->irq_rtcs = irq;
1746 + irq = platform_get_irq(to_platform_device(rtc->dev), 1);
1747 + rtc->irq_rtca = irq;
1749 + ret = request_threaded_irq(rtc->irq_rtcs, NULL, retu_rtc_interrupt,
1754 + ret = request_threaded_irq(rtc->irq_rtca, NULL, retu_rtc_interrupt,
1757 + free_irq(rtc->irq_rtcs, rtc);
1764 +static int retu_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
1766 + struct retu_rtc *rtc = dev_get_drvdata(dev);
1769 + mutex_lock(&rtc->mutex);
1771 + chmar = ((alm->time.tm_hour & 0x1f) << 8) | (alm->time.tm_min & 0x3f);
1772 + retu_write_reg(rtc->dev, RETU_REG_RTCHMAR, chmar);
1774 + mutex_unlock(&rtc->mutex);
1779 +static int retu_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
1781 + struct retu_rtc *rtc = dev_get_drvdata(dev);
1784 + mutex_lock(&rtc->mutex);
1786 + chmar = retu_read_reg(rtc->dev, RETU_REG_RTCHMAR);
1788 + alm->time.tm_hour = (chmar >> 8) & 0x1f;
1789 + alm->time.tm_min = chmar & 0x3f;
1790 + alm->enabled = !!rtc->alarm_expired;
1792 + mutex_unlock(&rtc->mutex);
1797 +static int retu_rtc_set_time(struct device *dev, struct rtc_time *tm)
1799 + struct retu_rtc *rtc = dev_get_drvdata(dev);
1803 + dsr = ((tm->tm_mday & 0xff) << 8) | (tm->tm_hour & 0xff);
1804 + hmr = ((tm->tm_min & 0xff) << 8) | (tm->tm_sec & 0xff);
1806 + mutex_lock(&rtc->mutex);
1808 + retu_write_reg(rtc->dev, RETU_REG_RTCDSR, dsr);
1809 + retu_write_reg(rtc->dev, RETU_REG_RTCHMR, hmr);
1811 + mutex_unlock(&rtc->mutex);
1816 +static int retu_rtc_read_time(struct device *dev, struct rtc_time *tm)
1818 + struct retu_rtc *rtc = dev_get_drvdata(dev);
1823 + * DSR holds days and hours
1824 + * HMR hols minutes and seconds
1826 + * both are 16 bit registers with 8-bit for each field.
1829 + mutex_lock(&rtc->mutex);
1831 + dsr = retu_read_reg(rtc->dev, RETU_REG_RTCDSR);
1832 + hmr = retu_read_reg(rtc->dev, RETU_REG_RTCHMR);
1834 + tm->tm_sec = hmr & 0xff;
1835 + tm->tm_min = hmr >> 8;
1836 + tm->tm_hour = dsr & 0xff;
1837 + tm->tm_mday = dsr >> 8;
1839 + mutex_unlock(&rtc->mutex);
1844 +static struct rtc_class_ops retu_rtc_ops = {
1845 + .read_time = retu_rtc_read_time,
1846 + .set_time = retu_rtc_set_time,
1847 + .read_alarm = retu_rtc_read_alarm,
1848 + .set_alarm = retu_rtc_set_alarm,
1851 +static int __init retu_rtc_probe(struct platform_device *pdev)
1853 + struct retu_rtc *rtc;
1856 + rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
1858 + dev_err(&pdev->dev, "not enough memory\n");
1863 + rtc->dev = &pdev->dev;
1864 + platform_set_drvdata(pdev, rtc);
1865 + mutex_init(&rtc->mutex);
1867 + rtc->alarm_expired = retu_read_reg(rtc->dev, RETU_REG_IDR) &
1868 + (0x1 << RETU_INT_RTCA);
1870 + r = retu_rtc_init_irq(rtc);
1872 + dev_err(&pdev->dev, "failed to request retu irq\n");
1876 + /* If the calibration register is zero, we've probably lost power */
1877 + if (!(retu_read_reg(rtc->dev, RETU_REG_RTCCALR) & 0x00ff))
1878 + retu_rtc_do_reset(rtc);
1880 + rtc->rtc = rtc_device_register(pdev->name, &pdev->dev, &
1881 + retu_rtc_ops, THIS_MODULE);
1882 + if (IS_ERR(rtc->rtc)) {
1883 + dev_err(&pdev->dev, "can't register RTC device\n");
1890 + free_irq(rtc->irq_rtcs, rtc);
1891 + free_irq(rtc->irq_rtca, rtc);
1900 +static int __devexit retu_rtc_remove(struct platform_device *pdev)
1902 + struct retu_rtc *rtc = platform_get_drvdata(pdev);
1904 + free_irq(rtc->irq_rtcs, rtc);
1905 + free_irq(rtc->irq_rtca, rtc);
1906 + rtc_device_unregister(rtc->rtc);
1912 +static struct platform_driver retu_rtc_driver = {
1913 + .remove = __exit_p(retu_rtc_remove),
1915 + .name = "retu-rtc",
1919 +static int __init retu_rtc_init(void)
1921 + return platform_driver_probe(&retu_rtc_driver, retu_rtc_probe);
1923 +module_init(retu_rtc_init);
1925 +static void __exit retu_rtc_exit(void)
1927 + platform_driver_unregister(&retu_rtc_driver);
1929 +module_exit(retu_rtc_exit);
1931 +MODULE_DESCRIPTION("Retu RTC");
1932 +MODULE_LICENSE("GPL");
1933 +MODULE_AUTHOR("Paul Mundt");
1934 +MODULE_AUTHOR("Igor Stoppa");
1935 +MODULE_AUTHOR("Felipe Balbi <felipe.balbi@nokia.com>");
1938 +++ b/drivers/cbus/retu-wdt.c
1941 + * drivers/cbus/retu-wdt.c
1943 + * Driver for Retu watchdog
1945 + * Copyright (C) 2004, 2005 Nokia Corporation
1947 + * Written by Amit Kucheria <amit.kucheria@nokia.com>
1949 + * Cleanups by Michael Buesch <mb@bu3sch.de> (C) 2011
1951 + * This file is subject to the terms and conditions of the GNU General
1952 + * Public License. See the file "COPYING" in the main directory of this
1953 + * archive for more details.
1955 + * This program is distributed in the hope that it will be useful,
1956 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1957 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1958 + * GNU General Public License for more details.
1960 + * You should have received a copy of the GNU General Public License
1961 + * along with this program; if not, write to the Free Software
1962 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1965 +#include <linux/kernel.h>
1966 +#include <linux/slab.h>
1967 +#include <linux/module.h>
1968 +#include <linux/device.h>
1969 +#include <linux/init.h>
1970 +#include <linux/fs.h>
1971 +#include <linux/io.h>
1972 +#include <linux/platform_device.h>
1974 +#include <linux/completion.h>
1975 +#include <linux/errno.h>
1976 +#include <linux/moduleparam.h>
1977 +#include <linux/miscdevice.h>
1978 +#include <linux/watchdog.h>
1980 +#include <asm/uaccess.h>
1982 +#include <plat/prcm.h>
1987 +/* Watchdog timeout in seconds */
1988 +#define RETU_WDT_MIN_TIMER 0
1989 +#define RETU_WDT_DEFAULT_TIMER 32
1990 +#define RETU_WDT_MAX_TIMER 63
1992 +struct retu_wdt_dev {
1993 + struct device *dev;
1994 + unsigned int period_val; /* Current period of watchdog */
1995 + unsigned long users;
1996 + struct miscdevice miscdev;
1997 + struct delayed_work ping_work;
1998 + struct mutex mutex;
2002 +static inline void _retu_modify_counter(struct retu_wdt_dev *wdev,
2005 + retu_write_reg(wdev->dev, RETU_REG_WATCHDOG, (u16)new);
2008 +static int retu_modify_counter(struct retu_wdt_dev *wdev, unsigned int new)
2010 + if (new < RETU_WDT_MIN_TIMER || new > RETU_WDT_MAX_TIMER)
2013 + mutex_lock(&wdev->mutex);
2014 + wdev->period_val = new;
2015 + _retu_modify_counter(wdev, wdev->period_val);
2016 + mutex_unlock(&wdev->mutex);
2022 + * Since retu watchdog cannot be disabled in hardware, we must kick it
2023 + * with a timer until userspace watchdog software takes over. Do this
2024 + * unless /dev/watchdog is open or CONFIG_WATCHDOG_NOWAYOUT is set.
2026 +static void retu_wdt_ping_enable(struct retu_wdt_dev *wdev)
2028 + _retu_modify_counter(wdev, RETU_WDT_MAX_TIMER);
2029 + schedule_delayed_work(&wdev->ping_work,
2030 + round_jiffies_relative(RETU_WDT_DEFAULT_TIMER * HZ));
2033 +static void retu_wdt_ping_disable(struct retu_wdt_dev *wdev)
2035 + _retu_modify_counter(wdev, RETU_WDT_MAX_TIMER);
2036 + cancel_delayed_work_sync(&wdev->ping_work);
2039 +static void retu_wdt_ping_work(struct work_struct *work)
2041 + struct retu_wdt_dev *wdev = container_of(to_delayed_work(work),
2042 + struct retu_wdt_dev, ping_work);
2043 + retu_wdt_ping_enable(wdev);
2046 +static int retu_wdt_open(struct inode *inode, struct file *file)
2048 + struct miscdevice *mdev = file->private_data;
2049 + struct retu_wdt_dev *wdev = container_of(mdev, struct retu_wdt_dev, miscdev);
2051 + if (test_and_set_bit(0, &wdev->users))
2054 + retu_wdt_ping_disable(wdev);
2056 + return nonseekable_open(inode, file);
2059 +static int retu_wdt_release(struct inode *inode, struct file *file)
2061 + struct miscdevice *mdev = file->private_data;
2062 + struct retu_wdt_dev *wdev = container_of(mdev, struct retu_wdt_dev, miscdev);
2064 +#ifndef CONFIG_WATCHDOG_NOWAYOUT
2065 + retu_wdt_ping_enable(wdev);
2067 + clear_bit(0, &wdev->users);
2072 +static ssize_t retu_wdt_write(struct file *file, const char __user *data,
2073 + size_t len, loff_t *ppos)
2075 + struct miscdevice *mdev = file->private_data;
2076 + struct retu_wdt_dev *wdev = container_of(mdev, struct retu_wdt_dev, miscdev);
2079 + retu_modify_counter(wdev, RETU_WDT_MAX_TIMER);
2084 +static long retu_wdt_ioctl(struct file *file, unsigned int cmd,
2085 + unsigned long arg)
2087 + struct miscdevice *mdev = file->private_data;
2088 + struct retu_wdt_dev *wdev = container_of(mdev, struct retu_wdt_dev, miscdev);
2091 + static const struct watchdog_info ident = {
2092 + .identity = "Retu Watchdog",
2093 + .options = WDIOF_SETTIMEOUT,
2094 + .firmware_version = 0,
2100 + case WDIOC_GETSUPPORT:
2101 + return copy_to_user((struct watchdog_info __user *)arg, &ident,
2103 + case WDIOC_GETSTATUS:
2104 + return put_user(0, (int __user *)arg);
2105 + case WDIOC_GETBOOTSTATUS:
2106 + if (cpu_is_omap16xx())
2107 + return put_user(omap_readw(ARM_SYSST),
2108 + (int __user *)arg);
2109 + if (cpu_is_omap24xx())
2110 + return put_user(omap_prcm_get_reset_sources(),
2111 + (int __user *)arg);
2112 + case WDIOC_KEEPALIVE:
2113 + retu_modify_counter(wdev, RETU_WDT_MAX_TIMER);
2115 + case WDIOC_SETTIMEOUT:
2116 + if (get_user(new_margin, (int __user *)arg))
2118 + retu_modify_counter(wdev, new_margin);
2119 + /* Fall through */
2120 + case WDIOC_GETTIMEOUT:
2121 + return put_user(wdev->period_val, (int __user *)arg);
2127 +static const struct file_operations retu_wdt_fops = {
2128 + .owner = THIS_MODULE,
2129 + .write = retu_wdt_write,
2130 + .unlocked_ioctl = retu_wdt_ioctl,
2131 + .open = retu_wdt_open,
2132 + .release = retu_wdt_release,
2135 +static int __init retu_wdt_probe(struct platform_device *pdev)
2137 + struct retu_wdt_dev *wdev;
2140 + wdev = kzalloc(sizeof(struct retu_wdt_dev), GFP_KERNEL);
2144 + wdev->dev = &pdev->dev;
2145 + wdev->period_val = RETU_WDT_DEFAULT_TIMER;
2146 + mutex_init(&wdev->mutex);
2148 + platform_set_drvdata(pdev, wdev);
2150 + wdev->miscdev.parent = &pdev->dev;
2151 + wdev->miscdev.minor = WATCHDOG_MINOR;
2152 + wdev->miscdev.name = "watchdog";
2153 + wdev->miscdev.fops = &retu_wdt_fops;
2155 + ret = misc_register(&wdev->miscdev);
2157 + goto err_free_wdev;
2159 + INIT_DELAYED_WORK(&wdev->ping_work, retu_wdt_ping_work);
2161 + /* Kick the watchdog for kernel booting to finish.
2162 + * If nowayout is not set, we start the ping work. */
2163 +#ifdef CONFIG_WATCHDOG_NOWAYOUT
2164 + retu_modify_counter(wdev, RETU_WDT_MAX_TIMER);
2166 + retu_wdt_ping_enable(wdev);
2177 +static int __devexit retu_wdt_remove(struct platform_device *pdev)
2179 + struct retu_wdt_dev *wdev;
2181 + wdev = platform_get_drvdata(pdev);
2182 + misc_deregister(&wdev->miscdev);
2183 + cancel_delayed_work_sync(&wdev->ping_work);
2189 +static struct platform_driver retu_wdt_driver = {
2190 + .remove = __exit_p(retu_wdt_remove),
2192 + .name = "retu-wdt",
2196 +static int __init retu_wdt_init(void)
2198 + return platform_driver_probe(&retu_wdt_driver, retu_wdt_probe);
2201 +static void __exit retu_wdt_exit(void)
2203 + platform_driver_unregister(&retu_wdt_driver);
2206 +module_init(retu_wdt_init);
2207 +module_exit(retu_wdt_exit);
2209 +MODULE_DESCRIPTION("Retu WatchDog");
2210 +MODULE_AUTHOR("Amit Kucheria");
2211 +MODULE_LICENSE("GPL");
2213 +++ b/drivers/cbus/tahvo.c
2216 + * drivers/cbus/tahvo.c
2218 + * Support functions for Tahvo ASIC
2220 + * Copyright (C) 2004, 2005 Nokia Corporation
2222 + * Written by Juha Yrjölä <juha.yrjola@nokia.com>,
2223 + * David Weinehall <david.weinehall@nokia.com>, and
2224 + * Mikko Ylinen <mikko.k.ylinen@nokia.com>
2226 + * This file is subject to the terms and conditions of the GNU General
2227 + * Public License. See the file "COPYING" in the main directory of this
2228 + * archive for more details.
2230 + * This program is distributed in the hope that it will be useful,
2231 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2232 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2233 + * GNU General Public License for more details.
2235 + * You should have received a copy of the GNU General Public License
2236 + * along with this program; if not, write to the Free Software
2237 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2240 +#include <linux/module.h>
2241 +#include <linux/init.h>
2243 +#include <linux/slab.h>
2244 +#include <linux/kernel.h>
2245 +#include <linux/errno.h>
2246 +#include <linux/device.h>
2247 +#include <linux/irq.h>
2248 +#include <linux/interrupt.h>
2249 +#include <linux/platform_device.h>
2250 +#include <linux/platform_data/cbus.h>
2251 +#include <linux/mutex.h>
2258 + struct mutex mutex;
2259 + struct device *dev;
2268 + unsigned int mask_pending:1;
2269 + unsigned int ack_pending:1;
2270 + unsigned int is_betty:1;
2274 + * __tahvo_read_reg - Reads a value from a register in Tahvo
2275 + * @tahvo: pointer to tahvo structure
2276 + * @reg: the register address to read from
2278 +static int __tahvo_read_reg(struct tahvo *tahvo, unsigned reg)
2280 + return cbus_read_reg(tahvo->dev, CBUS_TAHVO_DEVICE_ID, reg);
2284 + * __tahvo_write_reg - Writes a value to a register in Tahvo
2285 + * @tahvo: pointer to tahvo structure
2286 + * @reg: register address to write to
2287 + * @val: the value to be written to @reg
2289 +static void __tahvo_write_reg(struct tahvo *tahvo, unsigned reg, u16 val)
2291 + cbus_write_reg(tahvo->dev, CBUS_TAHVO_DEVICE_ID, reg, val);
2295 + * tahvo_read_reg - Read a value from a register in Tahvo
2296 + * @child: device pointer from the calling child
2297 + * @reg: the register to read from
2299 + * This function returns the contents of the specified register
2301 +int tahvo_read_reg(struct device *child, unsigned reg)
2303 + struct tahvo *tahvo = dev_get_drvdata(child->parent);
2305 + return __tahvo_read_reg(tahvo, reg);
2307 +EXPORT_SYMBOL(tahvo_read_reg);
2310 + * tahvo_write_reg - Write a value to a register in Tahvo
2311 + * @child: device pointer from the calling child
2312 + * @reg: the register to write to
2313 + * @val : the value to write to the register
2315 + * This function writes a value to the specified register
2317 +void tahvo_write_reg(struct device *child, unsigned reg, u16 val)
2319 + struct tahvo *tahvo = dev_get_drvdata(child->parent);
2321 + __tahvo_write_reg(tahvo, reg, val);
2323 +EXPORT_SYMBOL(tahvo_write_reg);
2326 + * tahvo_set_clear_reg_bits - set and clear register bits atomically
2327 + * @child: device pointer from the calling child
2328 + * @reg: the register to write to
2329 + * @bits: the bits to set
2331 + * This function sets and clears the specified Tahvo register bits atomically
2333 +void tahvo_set_clear_reg_bits(struct device *child, unsigned reg, u16 set,
2336 + struct tahvo *tahvo = dev_get_drvdata(child->parent);
2339 + mutex_lock(&tahvo->mutex);
2340 + w = __tahvo_read_reg(tahvo, reg);
2343 + __tahvo_write_reg(tahvo, reg, w);
2344 + mutex_unlock(&tahvo->mutex);
2347 +static irqreturn_t tahvo_irq_handler(int irq, void *_tahvo)
2349 + struct tahvo *tahvo = _tahvo;
2353 + id = __tahvo_read_reg(tahvo, TAHVO_REG_IDR);
2354 + im = __tahvo_read_reg(tahvo, TAHVO_REG_IMR);
2358 + dev_vdbg(tahvo->dev, "No IRQ, spurious ?\n");
2363 + unsigned long pending = __ffs(id);
2366 + id &= ~BIT(pending);
2367 + irq = pending + tahvo->irq_base;
2368 + handle_nested_irq(irq);
2371 + return IRQ_HANDLED;
2374 +/* -------------------------------------------------------------------------- */
2376 +static void tahvo_irq_bus_lock(struct irq_data *data)
2378 + struct tahvo *tahvo = irq_data_get_irq_chip_data(data);
2380 + mutex_lock(&tahvo->mutex);
2383 +static void tahvo_irq_bus_sync_unlock(struct irq_data *data)
2385 + struct tahvo *tahvo = irq_data_get_irq_chip_data(data);
2387 + if (tahvo->mask_pending) {
2388 + __tahvo_write_reg(tahvo, TAHVO_REG_IMR, tahvo->mask);
2389 + tahvo->mask_pending = false;
2392 + if (tahvo->ack_pending) {
2393 + __tahvo_write_reg(tahvo, TAHVO_REG_IDR, tahvo->ack);
2394 + tahvo->ack_pending = false;
2397 + mutex_unlock(&tahvo->mutex);
2400 +static void tahvo_irq_mask(struct irq_data *data)
2402 + struct tahvo *tahvo = irq_data_get_irq_chip_data(data);
2403 + int irq = data->irq;
2405 + tahvo->mask |= (1 << (irq - tahvo->irq_base));
2406 + tahvo->mask_pending = true;
2409 +static void tahvo_irq_unmask(struct irq_data *data)
2411 + struct tahvo *tahvo = irq_data_get_irq_chip_data(data);
2412 + int irq = data->irq;
2414 + tahvo->mask &= ~(1 << (irq - tahvo->irq_base));
2415 + tahvo->mask_pending = true;
2418 +static void tahvo_irq_ack(struct irq_data *data)
2420 + struct tahvo *tahvo = irq_data_get_irq_chip_data(data);
2421 + int irq = data->irq;
2423 + tahvo->ack |= (1 << (irq - tahvo->irq_base));
2424 + tahvo->ack_pending = true;
2427 +static struct irq_chip tahvo_irq_chip = {
2429 + .irq_bus_lock = tahvo_irq_bus_lock,
2430 + .irq_bus_sync_unlock = tahvo_irq_bus_sync_unlock,
2431 + .irq_mask = tahvo_irq_mask,
2432 + .irq_unmask = tahvo_irq_unmask,
2433 + .irq_ack = tahvo_irq_ack,
2436 +static inline void tahvo_irq_setup(int irq)
2439 + set_irq_flags(irq, IRQF_VALID);
2441 + irq_set_noprobe(irq);
2445 +static void tahvo_irq_init(struct tahvo *tahvo)
2447 + int base = tahvo->irq_base;
2448 + int end = tahvo->irq_end;
2451 + for (irq = base; irq < end; irq++) {
2452 + irq_set_chip_data(irq, tahvo);
2453 + irq_set_chip_and_handler(irq, &tahvo_irq_chip,
2454 + handle_simple_irq);
2455 + irq_set_nested_thread(irq, 1);
2456 + tahvo_irq_setup(irq);
2460 +/* -------------------------------------------------------------------------- */
2462 +static struct resource generic_resources[] = {
2464 + .start = -EINVAL, /* fixed later */
2465 + .flags = IORESOURCE_IRQ,
2469 +static struct device *tahvo_allocate_child(const char *name,
2470 + struct device *parent, int irq)
2472 + struct platform_device *pdev;
2475 + pdev = platform_device_alloc(name, -1);
2477 + dev_dbg(parent, "can't allocate %s\n", name);
2481 + pdev->dev.parent = parent;
2484 + generic_resources[0].start = irq;
2486 + ret = platform_device_add_resources(pdev, generic_resources,
2487 + ARRAY_SIZE(generic_resources));
2489 + dev_dbg(parent, "can't add resources to %s\n", name);
2494 + ret = platform_device_add(pdev);
2496 + dev_dbg(parent, "can't add %s\n", name);
2500 + return &pdev->dev;
2503 + platform_device_put(pdev);
2509 +static int tahvo_allocate_children(struct device *parent, int irq_base)
2511 + struct device *child;
2513 + child = tahvo_allocate_child("tahvo-usb", parent,
2514 + irq_base + TAHVO_INT_VBUSON);
2518 + child = tahvo_allocate_child("tahvo-pwm", parent, -1);
2525 +static int __devinit tahvo_probe(struct platform_device *pdev)
2527 + struct tahvo *tahvo;
2533 + tahvo = kzalloc(sizeof(*tahvo), GFP_KERNEL);
2535 + dev_err(&pdev->dev, "not enough memory\n");
2540 + irq = platform_get_irq(pdev, 0);
2541 + platform_set_drvdata(pdev, tahvo);
2543 + mutex_init(&tahvo->mutex);
2545 + ret = irq_alloc_descs(-1, 0, MAX_TAHVO_IRQ_HANDLERS, 0);
2547 + dev_err(&pdev->dev, "failed to allocate IRQ descs\n");
2551 + tahvo->irq_base = ret;
2552 + tahvo->irq_end = ret + MAX_TAHVO_IRQ_HANDLERS;
2553 + tahvo->dev = &pdev->dev;
2556 + tahvo_irq_init(tahvo);
2558 + rev = __tahvo_read_reg(tahvo, TAHVO_REG_ASICR);
2560 + id = (rev >> 8) & 0xff;
2563 + tahvo->is_betty = true;
2565 + ret = tahvo_allocate_children(&pdev->dev, tahvo->irq_base);
2567 + dev_err(&pdev->dev, "failed to allocate children\n");
2571 + dev_err(&pdev->dev, "%s v%d.%d found\n",
2572 + tahvo->is_betty ? "Betty" : "Tahvo",
2573 + (rev >> 4) & 0x0f, rev & 0x0f);
2575 + /* Mask all TAHVO interrupts */
2576 + __tahvo_write_reg(tahvo, TAHVO_REG_IMR, 0xffff);
2578 + ret = request_threaded_irq(irq, NULL, tahvo_irq_handler,
2579 + IRQF_TRIGGER_RISING | IRQF_ONESHOT,
2582 + dev_err(&pdev->dev, "Unable to register IRQ handler\n");
2589 + irq_free_descs(tahvo->irq_base, MAX_TAHVO_IRQ_HANDLERS);
2598 +static int __devexit tahvo_remove(struct platform_device *pdev)
2600 + struct tahvo *tahvo = platform_get_drvdata(pdev);
2603 + irq = platform_get_irq(pdev, 0);
2606 + irq_free_descs(tahvo->irq_base, MAX_TAHVO_IRQ_HANDLERS);
2612 +static struct platform_driver tahvo_driver = {
2613 + .probe = tahvo_probe,
2614 + .remove = __devexit_p(tahvo_remove),
2620 +static int __init tahvo_init(void)
2622 + return platform_driver_register(&tahvo_driver);
2624 +subsys_initcall(tahvo_init);
2626 +static void __exit tahvo_exit(void)
2628 + platform_driver_unregister(&tahvo_driver);
2630 +module_exit(tahvo_exit);
2632 +MODULE_DESCRIPTION("Tahvo ASIC control");
2633 +MODULE_LICENSE("GPL");
2634 +MODULE_AUTHOR("Juha Yrjölä");
2635 +MODULE_AUTHOR("David Weinehall");
2636 +MODULE_AUTHOR("Mikko Ylinen");
2639 +++ b/drivers/cbus/tahvo.h
2642 + * drivers/cbus/tahvo.h
2644 + * Copyright (C) 2004, 2005 Nokia Corporation
2646 + * Written by Juha Yrjölä <juha.yrjola@nokia.com> and
2647 + * David Weinehall <david.weinehall@nokia.com>
2649 + * This file is subject to the terms and conditions of the GNU General
2650 + * Public License. See the file "COPYING" in the main directory of this
2651 + * archive for more details.
2653 + * This program is distributed in the hope that it will be useful,
2654 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2655 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2656 + * GNU General Public License for more details.
2658 + * You should have received a copy of the GNU General Public License
2659 + * along with this program; if not, write to the Free Software
2660 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2663 +#ifndef __DRIVERS_CBUS_TAHVO_H
2664 +#define __DRIVERS_CBUS_TAHVO_H
2666 +#include <linux/types.h>
2669 +#define TAHVO_REG_ASICR 0x00 /* ASIC ID & revision */
2670 +#define TAHVO_REG_IDR 0x01 /* Interrupt ID */
2671 +#define TAHVO_REG_IDSR 0x02 /* Interrupt status */
2672 +#define TAHVO_REG_IMR 0x03 /* Interrupt mask */
2673 +#define TAHVO_REG_CHGCURR 0x04 /* Charge current control PWM (8-bit) */
2674 +#define TAHVO_REG_LEDPWMR 0x05 /* LED PWM */
2675 +#define TAHVO_REG_USBR 0x06 /* USB control */
2676 +#define TAHVO_REG_CHGCTL 0x08 /* Charge control register */
2677 +#define TAHVO_REG_CHGCTL_EN 0x0001 /* Global charge enable */
2678 +#define TAHVO_REG_CHGCTL_PWMOVR 0x0004 /* PWM override. Force charge PWM to 0%/100% duty cycle. */
2679 +#define TAHVO_REG_CHGCTL_PWMOVRZERO 0x0008 /* If set, PWM override is 0% (If unset -> 100%) */
2680 +#define TAHVO_REG_CHGCTL_CURMEAS 0x0040 /* Enable battery current measurement. */
2681 +#define TAHVO_REG_CHGCTL_CURTIMRST 0x0080 /* Current measure timer reset. */
2682 +#define TAHVO_REG_BATCURRTIMER 0x0c /* Battery current measure timer (8-bit) */
2683 +#define TAHVO_REG_BATCURR 0x0d /* Battery (dis)charge current (signed 16-bit) */
2685 +#define TAHVO_REG_MAX 0x0d
2687 +/* Interrupt sources */
2688 +#define TAHVO_INT_VBUSON 0
2689 +#define TAHVO_INT_BATCURR 7 /* Battery current measure timer */
2691 +#define MAX_TAHVO_IRQ_HANDLERS 8
2693 +int tahvo_read_reg(struct device *child, unsigned reg);
2694 +void tahvo_write_reg(struct device *child, unsigned reg, u16 val);
2695 +void tahvo_set_clear_reg_bits(struct device *child, unsigned reg, u16 set,
2698 +#endif /* __DRIVERS_CBUS_TAHVO_H */
2700 +++ b/drivers/cbus/tahvo-usb.c
2703 + * drivers/cbus/tahvo-usb.c
2705 + * Tahvo USB transeiver
2707 + * Copyright (C) 2005-2006 Nokia Corporation
2709 + * Parts copied from drivers/i2c/chips/isp1301_omap.c
2710 + * Copyright (C) 2004 Texas Instruments
2711 + * Copyright (C) 2004 David Brownell
2713 + * Written by Juha Yrjölä <juha.yrjola@nokia.com>,
2714 + * Tony Lindgren <tony@atomide.com>, and
2715 + * Timo Teräs <timo.teras@nokia.com>
2717 + * This file is subject to the terms and conditions of the GNU General
2718 + * Public License. See the file "COPYING" in the main directory of this
2719 + * archive for more details.
2721 + * This program is distributed in the hope that it will be useful,
2722 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2723 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2724 + * GNU General Public License for more details.
2726 + * You should have received a copy of the GNU General Public License
2727 + * along with this program; if not, write to the Free Software
2728 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2731 +#include <linux/kernel.h>
2732 +#include <linux/module.h>
2733 +#include <linux/init.h>
2734 +#include <linux/slab.h>
2735 +#include <linux/io.h>
2736 +#include <linux/interrupt.h>
2737 +#include <linux/platform_device.h>
2738 +#include <linux/usb/ch9.h>
2739 +#include <linux/usb/gadget.h>
2740 +#include <linux/usb.h>
2741 +#include <linux/usb/otg.h>
2742 +#include <linux/i2c.h>
2743 +#include <linux/workqueue.h>
2744 +#include <linux/kobject.h>
2745 +#include <linux/clk.h>
2746 +#include <linux/mutex.h>
2748 +#include <asm/irq.h>
2749 +#include <plat/usb.h>
2754 +#define DRIVER_NAME "tahvo-usb"
2756 +#define USBR_SLAVE_CONTROL (1 << 8)
2757 +#define USBR_VPPVIO_SW (1 << 7)
2758 +#define USBR_SPEED (1 << 6)
2759 +#define USBR_REGOUT (1 << 5)
2760 +#define USBR_MASTER_SW2 (1 << 4)
2761 +#define USBR_MASTER_SW1 (1 << 3)
2762 +#define USBR_SLAVE_SW (1 << 2)
2763 +#define USBR_NSUSPEND (1 << 1)
2764 +#define USBR_SEMODE (1 << 0)
2766 +/* bits in OTG_CTRL */
2768 +/* Bits that are controlled by OMAP OTG and are read-only */
2769 +#define OTG_CTRL_OMAP_MASK (OTG_PULLDOWN|OTG_PULLUP|OTG_DRV_VBUS|\
2770 + OTG_PD_VBUS|OTG_PU_VBUS|OTG_PU_ID)
2771 +/* Bits that are controlled by transceiver */
2772 +#define OTG_CTRL_XCVR_MASK (OTG_ASESSVLD|OTG_BSESSEND|\
2773 + OTG_BSESSVLD|OTG_VBUSVLD|OTG_ID)
2774 +/* Bits that are controlled by system */
2775 +#define OTG_CTRL_SYS_MASK (OTG_A_BUSREQ|OTG_A_SETB_HNPEN|OTG_B_BUSREQ|\
2776 + OTG_B_HNPEN|OTG_BUSDROP)
2778 +#if defined(CONFIG_USB_OHCI_HCD) && !defined(CONFIG_USB_OTG)
2779 +#error tahvo-otg.c does not work with OCHI yet!
2782 +#define TAHVO_MODE_HOST 0
2783 +#define TAHVO_MODE_PERIPHERAL 1
2785 +#ifdef CONFIG_USB_OTG
2786 +#define TAHVO_MODE(tu) (tu)->tahvo_mode
2787 +#elif defined(CONFIG_USB_GADGET_OMAP)
2788 +#define TAHVO_MODE(tu) TAHVO_MODE_PERIPHERAL
2790 +#define TAHVO_MODE(tu) TAHVO_MODE_HOST
2794 + struct device *dev;
2795 + struct platform_device *pt_dev;
2796 + struct otg_transceiver otg;
2798 + struct mutex serialize;
2799 +#ifdef CONFIG_USB_OTG
2806 +static struct tahvo_usb *tahvo_usb_device;
2809 + * ---------------------------------------------------------------------------
2810 + * OTG related functions
2812 + * These shoud be separated into omap-otg.c driver module, as they are used
2813 + * by various transceivers. These functions are needed in the UDC-only case
2814 + * as well. These functions are copied from GPL isp1301_omap.c
2815 + * ---------------------------------------------------------------------------
2817 +static struct platform_device *tahvo_otg_dev;
2819 +static irqreturn_t omap_otg_irq(int irq, void *arg)
2823 + otg_irq = omap_readw(OTG_IRQ_SRC);
2824 + if (otg_irq & OPRT_CHG) {
2825 + omap_writew(OPRT_CHG, OTG_IRQ_SRC);
2826 + } else if (otg_irq & B_SRP_TMROUT) {
2827 + omap_writew(B_SRP_TMROUT, OTG_IRQ_SRC);
2828 + } else if (otg_irq & B_HNP_FAIL) {
2829 + omap_writew(B_HNP_FAIL, OTG_IRQ_SRC);
2830 + } else if (otg_irq & A_SRP_DETECT) {
2831 + omap_writew(A_SRP_DETECT, OTG_IRQ_SRC);
2832 + } else if (otg_irq & A_REQ_TMROUT) {
2833 + omap_writew(A_REQ_TMROUT, OTG_IRQ_SRC);
2834 + } else if (otg_irq & A_VBUS_ERR) {
2835 + omap_writew(A_VBUS_ERR, OTG_IRQ_SRC);
2836 + } else if (otg_irq & DRIVER_SWITCH) {
2837 +#ifdef CONFIG_USB_OTG
2838 + if ((!(omap_readl(OTG_CTRL) & OTG_DRIVER_SEL)) &&
2839 + tu->otg.host && tu->otg.state == OTG_STATE_A_HOST) {
2840 + /* role is host */
2841 + usb_bus_start_enum(tu->otg.host,
2842 + tu->otg.host->otg_port);
2845 + omap_writew(DRIVER_SWITCH, OTG_IRQ_SRC);
2849 + return IRQ_HANDLED;
2853 +static int tahvo_otg_init(void)
2857 +#ifdef CONFIG_USB_OTG
2858 + if (!tahvo_otg_dev) {
2859 + printk("tahvo-usb: no tahvo_otg_dev\n");
2864 + l = omap_readl(OTG_SYSCON_1);
2865 + l &= ~OTG_IDLE_EN;
2866 + omap_writel(l, OTG_SYSCON_1);
2869 + /* some of these values are board-specific... */
2870 + l = omap_readl(OTG_SYSCON_2);
2872 + /* for B-device: */
2873 + | SRP_GPDATA /* 9msec Bdev D+ pulse */
2874 + | SRP_GPDVBUS /* discharge after VBUS pulse */
2875 + // | (3 << 24) /* 2msec VBUS pulse */
2876 + /* for A-device: */
2877 + | (0 << 20) /* 200ms nominal A_WAIT_VRISE timer */
2878 + | SRP_DPW /* detect 167+ns SRP pulses */
2879 + | SRP_DATA | SRP_VBUS; /* accept both kinds of SRP pulse */
2880 + omap_writel(l, OTG_SYSCON_2);
2882 + omap_writew(DRIVER_SWITCH | OPRT_CHG
2883 + | B_SRP_TMROUT | B_HNP_FAIL
2884 + | A_VBUS_ERR | A_SRP_DETECT | A_REQ_TMROUT,
2886 + l = omap_readl(OTG_SYSCON_2);
2888 + omap_writel(l, OTG_SYSCON_2);
2893 +static int __init omap_otg_probe(struct platform_device *pdev)
2897 + tahvo_otg_dev = pdev;
2898 + ret = tahvo_otg_init();
2900 + printk(KERN_ERR "tahvo-usb: tahvo_otg_init failed\n");
2904 + return request_irq(tahvo_otg_dev->resource[1].start,
2905 + omap_otg_irq, IRQF_DISABLED, DRIVER_NAME,
2906 + tahvo_usb_device);
2909 +static int __exit omap_otg_remove(struct platform_device *pdev)
2911 + free_irq(tahvo_otg_dev->resource[1].start, tahvo_usb_device);
2912 + tahvo_otg_dev = NULL;
2917 +struct platform_driver omap_otg_driver = {
2919 + .name = "omap_otg",
2921 + .remove = __exit_p(omap_otg_remove),
2925 + * ---------------------------------------------------------------------------
2926 + * Tahvo related functions
2927 + * These are Nokia proprietary code, except for the OTG register settings,
2928 + * which are copied from isp1301.c
2929 + * ---------------------------------------------------------------------------
2931 +static ssize_t vbus_state_show(struct device *device,
2932 + struct device_attribute *attr, char *buf)
2934 + struct tahvo_usb *tu = dev_get_drvdata(device);
2935 + return sprintf(buf, "%d\n", tu->vbus_state);
2937 +static DEVICE_ATTR(vbus_state, 0444, vbus_state_show, NULL);
2939 +int vbus_active = 0;
2941 +static void check_vbus_state(struct tahvo_usb *tu)
2943 + int reg, prev_state;
2945 + reg = tahvo_read_reg(tu->dev, TAHVO_REG_IDSR);
2950 + switch (tu->otg.state) {
2951 + case OTG_STATE_B_IDLE:
2952 + /* Enable the gadget driver */
2953 + if (tu->otg.gadget)
2954 + usb_gadget_vbus_connect(tu->otg.gadget);
2955 + /* Set B-session valid and not B-sessio ended to indicate
2956 + * Vbus to be ok. */
2957 + l = omap_readl(OTG_CTRL);
2958 + l &= ~OTG_BSESSEND;
2959 + l |= OTG_BSESSVLD;
2960 + omap_writel(l, OTG_CTRL);
2962 + tu->otg.state = OTG_STATE_B_PERIPHERAL;
2964 + case OTG_STATE_A_IDLE:
2965 + /* Session is now valid assuming the USB hub is driving Vbus */
2966 + tu->otg.state = OTG_STATE_A_HOST;
2971 + printk("USB cable connected\n");
2973 + switch (tu->otg.state) {
2974 + case OTG_STATE_B_PERIPHERAL:
2975 + if (tu->otg.gadget)
2976 + usb_gadget_vbus_disconnect(tu->otg.gadget);
2977 + tu->otg.state = OTG_STATE_B_IDLE;
2979 + case OTG_STATE_A_HOST:
2980 + tu->otg.state = OTG_STATE_A_IDLE;
2985 + printk("USB cable disconnected\n");
2989 + prev_state = tu->vbus_state;
2990 + tu->vbus_state = reg & 0x01;
2991 + if (prev_state != tu->vbus_state)
2992 + sysfs_notify(&tu->pt_dev->dev.kobj, NULL, "vbus_state");
2995 +static void tahvo_usb_become_host(struct tahvo_usb *tu)
2999 + /* Clear system and transceiver controlled bits
3000 + * also mark the A-session is always valid */
3003 + l = omap_readl(OTG_CTRL);
3004 + l &= ~(OTG_CTRL_XCVR_MASK | OTG_CTRL_SYS_MASK);
3005 + l |= OTG_ASESSVLD;
3006 + omap_writel(l, OTG_CTRL);
3008 + /* Power up the transceiver in USB host mode */
3009 + tahvo_write_reg(tu->dev, TAHVO_REG_USBR, USBR_REGOUT | USBR_NSUSPEND |
3010 + USBR_MASTER_SW2 | USBR_MASTER_SW1);
3011 + tu->otg.state = OTG_STATE_A_IDLE;
3013 + check_vbus_state(tu);
3016 +static void tahvo_usb_stop_host(struct tahvo_usb *tu)
3018 + tu->otg.state = OTG_STATE_A_IDLE;
3021 +static void tahvo_usb_become_peripheral(struct tahvo_usb *tu)
3025 + /* Clear system and transceiver controlled bits
3026 + * and enable ID to mark peripheral mode and
3027 + * BSESSEND to mark no Vbus */
3029 + l = omap_readl(OTG_CTRL);
3030 + l &= ~(OTG_CTRL_XCVR_MASK | OTG_CTRL_SYS_MASK | OTG_BSESSVLD);
3031 + l |= OTG_ID | OTG_BSESSEND;
3032 + omap_writel(l, OTG_CTRL);
3034 + /* Power up transceiver and set it in USB perhiperal mode */
3035 + tahvo_write_reg(tu->dev, TAHVO_REG_USBR, USBR_SLAVE_CONTROL | USBR_REGOUT | USBR_NSUSPEND | USBR_SLAVE_SW);
3036 + tu->otg.state = OTG_STATE_B_IDLE;
3038 + check_vbus_state(tu);
3041 +static void tahvo_usb_stop_peripheral(struct tahvo_usb *tu)
3045 + l = omap_readl(OTG_CTRL);
3046 + l &= ~OTG_BSESSVLD;
3047 + l |= OTG_BSESSEND;
3048 + omap_writel(l, OTG_CTRL);
3050 + if (tu->otg.gadget)
3051 + usb_gadget_vbus_disconnect(tu->otg.gadget);
3052 + tu->otg.state = OTG_STATE_B_IDLE;
3056 +static void tahvo_usb_power_off(struct tahvo_usb *tu)
3061 + /* Disable gadget controller if any */
3062 + if (tu->otg.gadget)
3063 + usb_gadget_vbus_disconnect(tu->otg.gadget);
3065 + /* Disable OTG and interrupts */
3066 + if (TAHVO_MODE(tu) == TAHVO_MODE_PERIPHERAL)
3070 + l = omap_readl(OTG_CTRL);
3071 + l &= ~(OTG_CTRL_XCVR_MASK | OTG_CTRL_SYS_MASK | OTG_BSESSVLD);
3072 + l |= id | OTG_BSESSEND;
3073 + omap_writel(l, OTG_CTRL);
3074 + omap_writew(0, OTG_IRQ_EN);
3076 + l = omap_readl(OTG_SYSCON_2);
3078 + omap_writel(l, OTG_SYSCON_2);
3080 + l = omap_readl(OTG_SYSCON_1);
3082 + omap_writel(l, OTG_SYSCON_1);
3084 + /* Power off transceiver */
3085 + tahvo_write_reg(tu->dev, TAHVO_REG_USBR, 0);
3086 + tu->otg.state = OTG_STATE_UNDEFINED;
3090 +static int tahvo_usb_set_power(struct otg_transceiver *dev, unsigned mA)
3092 + struct tahvo_usb *tu = container_of(dev, struct tahvo_usb, otg);
3094 + dev_dbg(&tu->pt_dev->dev, "set_power %d mA\n", mA);
3096 + if (dev->state == OTG_STATE_B_PERIPHERAL) {
3097 + /* REVISIT: Can Tahvo charge battery from VBUS? */
3102 +static int tahvo_usb_set_suspend(struct otg_transceiver *dev, int suspend)
3104 + struct tahvo_usb *tu = container_of(dev, struct tahvo_usb, otg);
3107 + dev_dbg(&tu->pt_dev->dev, "set_suspend\n");
3109 + w = tahvo_read_reg(tu->dev, TAHVO_REG_USBR);
3111 + w &= ~USBR_NSUSPEND;
3113 + w |= USBR_NSUSPEND;
3114 + tahvo_write_reg(tu->dev, TAHVO_REG_USBR, w);
3119 +static int tahvo_usb_start_srp(struct otg_transceiver *dev)
3121 + struct tahvo_usb *tu = container_of(dev, struct tahvo_usb, otg);
3124 + dev_dbg(&tu->pt_dev->dev, "start_srp\n");
3126 + if (!dev || tu->otg.state != OTG_STATE_B_IDLE)
3129 + otg_ctrl = omap_readl(OTG_CTRL);
3130 + if (!(otg_ctrl & OTG_BSESSEND))
3133 + otg_ctrl |= OTG_B_BUSREQ;
3134 + otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_SYS_MASK;
3135 + omap_writel(otg_ctrl, OTG_CTRL);
3136 + tu->otg.state = OTG_STATE_B_SRP_INIT;
3141 +static int tahvo_usb_start_hnp(struct otg_transceiver *otg)
3143 + struct tahvo_usb *tu = container_of(otg, struct tahvo_usb, otg);
3145 + dev_dbg(&tu->pt_dev->dev, "start_hnp\n");
3146 +#ifdef CONFIG_USB_OTG
3147 + /* REVISIT: Add this for OTG */
3152 +static int tahvo_usb_set_host(struct otg_transceiver *otg, struct usb_bus *host)
3154 + struct tahvo_usb *tu = container_of(otg, struct tahvo_usb, otg);
3157 + dev_dbg(&tu->pt_dev->dev, "set_host %p\n", host);
3162 +#if defined(CONFIG_USB_OTG) || !defined(CONFIG_USB_GADGET_OMAP)
3164 + mutex_lock(&tu->serialize);
3166 + if (host == NULL) {
3167 + if (TAHVO_MODE(tu) == TAHVO_MODE_HOST)
3168 + tahvo_usb_power_off(tu);
3169 + tu->otg.host = NULL;
3170 + mutex_unlock(&tu->serialize);
3174 + l = omap_readl(OTG_SYSCON_1);
3175 + l &= ~(OTG_IDLE_EN | HST_IDLE_EN | DEV_IDLE_EN);
3176 + omap_writel(l, OTG_SYSCON_1);
3178 + if (TAHVO_MODE(tu) == TAHVO_MODE_HOST) {
3179 + tu->otg.host = NULL;
3180 + tahvo_usb_become_host(tu);
3183 + tu->otg.host = host;
3185 + mutex_unlock(&tu->serialize);
3187 + /* No host mode configured, so do not allow host controlled to be set */
3194 +static int tahvo_usb_set_peripheral(struct otg_transceiver *otg, struct usb_gadget *gadget)
3196 + struct tahvo_usb *tu = container_of(otg, struct tahvo_usb, otg);
3198 + dev_dbg(&tu->pt_dev->dev, "set_peripheral %p\n", gadget);
3203 +#if defined(CONFIG_USB_OTG) || defined(CONFIG_USB_GADGET_OMAP)
3205 + mutex_lock(&tu->serialize);
3208 + if (TAHVO_MODE(tu) == TAHVO_MODE_PERIPHERAL)
3209 + tahvo_usb_power_off(tu);
3210 + tu->otg.gadget = NULL;
3211 + mutex_unlock(&tu->serialize);
3215 + tu->otg.gadget = gadget;
3216 + if (TAHVO_MODE(tu) == TAHVO_MODE_PERIPHERAL)
3217 + tahvo_usb_become_peripheral(tu);
3219 + mutex_unlock(&tu->serialize);
3221 + /* No gadget mode configured, so do not allow host controlled to be set */
3228 +static irqreturn_t tahvo_usb_vbus_interrupt(int irq, void *_tu)
3230 + struct tahvo_usb *tu = _tu;
3232 + check_vbus_state(tu);
3234 + return IRQ_HANDLED;
3237 +#ifdef CONFIG_USB_OTG
3238 +static ssize_t otg_mode_show(struct device *device,
3239 + struct device_attribute *attr, char *buf)
3241 + struct tahvo_usb *tu = dev_get_drvdata(device);
3242 + switch (tu->tahvo_mode) {
3243 + case TAHVO_MODE_HOST:
3244 + return sprintf(buf, "host\n");
3245 + case TAHVO_MODE_PERIPHERAL:
3246 + return sprintf(buf, "peripheral\n");
3248 + return sprintf(buf, "unknown\n");
3251 +static ssize_t otg_mode_store(struct device *device,
3252 + struct device_attribute *attr,
3253 + const char *buf, size_t count)
3255 + struct tahvo_usb *tu = dev_get_drvdata(device);
3259 + mutex_lock(&tu->serialize);
3260 + if (strncmp(buf, "host", 4) == 0) {
3261 + if (tu->tahvo_mode == TAHVO_MODE_PERIPHERAL)
3262 + tahvo_usb_stop_peripheral(tu);
3263 + tu->tahvo_mode = TAHVO_MODE_HOST;
3264 + if (tu->otg.host) {
3265 + printk(KERN_INFO "Selected HOST mode: host controller present.\n");
3266 + tahvo_usb_become_host(tu);
3268 + printk(KERN_INFO "Selected HOST mode: no host controller, powering off.\n");
3269 + tahvo_usb_power_off(tu);
3271 + } else if (strncmp(buf, "peripheral", 10) == 0) {
3272 + if (tu->tahvo_mode == TAHVO_MODE_HOST)
3273 + tahvo_usb_stop_host(tu);
3274 + tu->tahvo_mode = TAHVO_MODE_PERIPHERAL;
3275 + if (tu->otg.gadget) {
3276 + printk(KERN_INFO "Selected PERIPHERAL mode: gadget driver present.\n");
3277 + tahvo_usb_become_peripheral(tu);
3279 + printk(KERN_INFO "Selected PERIPHERAL mode: no gadget driver, powering off.\n");
3280 + tahvo_usb_power_off(tu);
3285 + mutex_unlock(&tu->serialize);
3289 +static DEVICE_ATTR(otg_mode, 0644, otg_mode_show, otg_mode_store);
3292 +static int __init tahvo_usb_probe(struct platform_device *pdev)
3294 + struct tahvo_usb *tu;
3295 + struct device *dev = &pdev->dev;
3299 + dev_dbg(dev, "probe\n");
3301 + /* Create driver data */
3302 + tu = kzalloc(sizeof(*tu), GFP_KERNEL);
3305 + tahvo_usb_device = tu;
3308 + tu->pt_dev = pdev;
3309 +#ifdef CONFIG_USB_OTG
3310 + /* Default mode */
3311 +#ifdef CONFIG_CBUS_TAHVO_USB_HOST_BY_DEFAULT
3312 + tu->tahvo_mode = TAHVO_MODE_HOST;
3314 + tu->tahvo_mode = TAHVO_MODE_PERIPHERAL;
3318 + mutex_init(&tu->serialize);
3320 + tu->ick = clk_get(NULL, "usb_l4_ick");
3321 + if (IS_ERR(tu->ick)) {
3322 + dev_err(dev, "Failed to get usb_l4_ick\n");
3323 + ret = PTR_ERR(tu->ick);
3326 + clk_enable(tu->ick);
3328 + /* Set initial state, so that we generate kevents only on
3329 + * state changes */
3330 + tu->vbus_state = tahvo_read_reg(tu->dev, TAHVO_REG_IDSR) & 0x01;
3332 + irq = platform_get_irq(pdev, 0);
3335 + /* We cannot enable interrupt until omap_udc is initialized */
3336 + ret = request_threaded_irq(irq, NULL, tahvo_usb_vbus_interrupt,
3337 + IRQF_ONESHOT, "tahvo-vbus", tu);
3339 + printk(KERN_ERR "Could not register Tahvo interrupt for VBUS\n");
3340 + goto err_release_clk;
3344 + ret = device_create_file(dev, &dev_attr_vbus_state);
3345 +#ifdef CONFIG_USB_OTG
3346 + ret |= device_create_file(dev, &dev_attr_otg_mode);
3349 + printk(KERN_ERR "attribute creation failed: %d\n", ret);
3351 + /* Create OTG interface */
3352 + tahvo_usb_power_off(tu);
3353 + tu->otg.state = OTG_STATE_UNDEFINED;
3354 + tu->otg.label = DRIVER_NAME;
3355 + tu->otg.set_host = tahvo_usb_set_host;
3356 + tu->otg.set_peripheral = tahvo_usb_set_peripheral;
3357 + tu->otg.set_power = tahvo_usb_set_power;
3358 + tu->otg.set_suspend = tahvo_usb_set_suspend;
3359 + tu->otg.start_srp = tahvo_usb_start_srp;
3360 + tu->otg.start_hnp = tahvo_usb_start_hnp;
3362 + ret = otg_set_transceiver(&tu->otg);
3364 + printk(KERN_ERR "Cannot register USB transceiver\n");
3365 + goto err_free_irq;
3368 + dev_set_drvdata(dev, tu);
3373 + free_irq(tu->irq, tu);
3375 + clk_disable(tu->ick);
3379 + tahvo_usb_device = NULL;
3384 +static int __exit tahvo_usb_remove(struct platform_device *pdev)
3386 + struct tahvo_usb *tu = platform_get_drvdata(pdev);
3388 + dev_dbg(&pdev->dev, "remove\n");
3390 + free_irq(tu->irq, tu);
3391 + flush_scheduled_work();
3392 + otg_set_transceiver(0);
3393 + device_remove_file(&pdev->dev, &dev_attr_vbus_state);
3394 +#ifdef CONFIG_USB_OTG
3395 + device_remove_file(&pdev->dev, &dev_attr_otg_mode);
3397 + clk_disable(tu->ick);
3401 + tahvo_usb_device = NULL;
3406 +static struct platform_driver tahvo_usb_driver = {
3408 + .name = "tahvo-usb",
3410 + .remove = __exit_p(tahvo_usb_remove),
3413 +static int __init tahvo_usb_init(void)
3417 + ret = platform_driver_probe(&tahvo_usb_driver, tahvo_usb_probe);
3421 + ret = platform_driver_probe(&omap_otg_driver, omap_otg_probe);
3423 + platform_driver_unregister(&tahvo_usb_driver);
3430 +subsys_initcall(tahvo_usb_init);
3432 +static void __exit tahvo_usb_exit(void)
3434 + platform_driver_unregister(&omap_otg_driver);
3435 + platform_driver_unregister(&tahvo_usb_driver);
3437 +module_exit(tahvo_usb_exit);
3439 +MODULE_DESCRIPTION("Tahvo USB OTG Transceiver Driver");
3440 +MODULE_LICENSE("GPL");
3441 +MODULE_AUTHOR("Juha Yrjölä, Tony Lindgren, and Timo Teräs");
3442 --- a/drivers/Makefile
3443 +++ b/drivers/Makefile
3444 @@ -78,7 +78,7 @@ obj-$(CONFIG_GAMEPORT) += input/gamepor
3445 obj-$(CONFIG_INPUT) += input/
3446 obj-$(CONFIG_I2O) += message/
3447 obj-$(CONFIG_RTC_LIB) += rtc/
3448 -obj-y += i2c/ media/
3449 +obj-y += i2c/ media/ cbus/
3450 obj-$(CONFIG_PPS) += pps/
3451 obj-$(CONFIG_PTP_1588_CLOCK) += ptp/
3452 obj-$(CONFIG_W1) += w1/
3453 --- a/drivers/Kconfig
3454 +++ b/drivers/Kconfig
3455 @@ -2,6 +2,8 @@ menu "Device Drivers"
3457 source "drivers/base/Kconfig"
3459 +source "drivers/cbus/Kconfig"
3461 source "drivers/connector/Kconfig"
3463 source "drivers/mtd/Kconfig"