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/slab.h>
36 +#include <linux/spinlock.h>
37 +#include <linux/gpio.h>
38 +#include <linux/platform_device.h>
39 +#include <linux/platform_data/cbus.h>
43 +#define CBUS_XFER_READ 1
44 +#define CBUS_XFER_WRITE 0
58 + * cbus_send_bit - sends one bit over the bus
59 + * @host: the host we're using
60 + * @bit: one bit of information to send
61 + * @input: whether to set data pin as input after sending
63 +static int cbus_send_bit(struct cbus_host *host, unsigned bit,
68 + gpio_set_value(host->dat_gpio, bit ? 1 : 0);
69 + gpio_set_value(host->clk_gpio, 1);
71 + /* The data bit is read on the rising edge of CLK */
73 + ret = gpio_direction_input(host->dat_gpio);
75 + gpio_set_value(host->clk_gpio, 0);
81 + * cbus_send_data - sends @len amount of data over the bus
82 + * @host: the host we're using
83 + * @data: the data to send
84 + * @len: size of the transfer
85 + * @input: whether to set data pin as input after sending
87 +static int cbus_send_data(struct cbus_host *host, unsigned data, unsigned len,
93 + for (i = len; i > 0; i--) {
94 + ret = cbus_send_bit(host, data & (1 << (i - 1)),
105 + * cbus_receive_bit - receives one bit from the bus
106 + * @host: the host we're using
108 +static int cbus_receive_bit(struct cbus_host *host)
112 + gpio_set_value(host->clk_gpio, 1);
113 + ret = gpio_get_value(host->dat_gpio);
116 + gpio_set_value(host->clk_gpio, 0);
123 + * cbus_receive_data - receives @len data from the bus
124 + * @host: the host we're using
125 + * @len: the length of data to receive
127 +static int cbus_receive_data(struct cbus_host *host, unsigned len)
132 + for (i = 16; i > 0; i--) {
133 + int bit = cbus_receive_bit(host);
139 + ret |= 1 << (i - 1);
147 + * cbus_transfer - transfers data over the bus
148 + * @host: the host we're using
149 + * @rw: read/write flag
150 + * @dev: device address
151 + * @reg: register address
152 + * @data: if @rw == 0 data to send otherwise 0
154 +static int cbus_transfer(struct cbus_host *host, unsigned rw, unsigned dev,
155 + unsigned reg, unsigned data)
157 + unsigned long flags;
161 + /* We don't want interrupts disturbing our transfer */
162 + spin_lock_irqsave(&host->lock, flags);
164 + /* Reset state and start of transfer, SEL stays down during transfer */
165 + gpio_set_value(host->sel_gpio, 0);
167 + /* Set the DAT pin to output */
168 + gpio_direction_output(host->dat_gpio, 1);
170 + /* Send the device address */
171 + ret = cbus_send_data(host, dev, 3, 0);
173 + dev_dbg(host->dev, "failed sending device addr\n");
177 + /* Send the rw flag */
178 + ret = cbus_send_bit(host, rw, 0);
180 + dev_dbg(host->dev, "failed sending read/write flag\n");
184 + /* Send the register address */
188 + ret = cbus_send_data(host, reg, 5, input);
190 + dev_dbg(host->dev, "failed sending register addr\n");
195 + ret = cbus_send_data(host, data, 16, 0);
197 + dev_dbg(host->dev, "failed sending data\n");
201 + gpio_set_value(host->clk_gpio, 1);
203 + ret = cbus_receive_data(host, 16);
205 + dev_dbg(host->dev, "failed receiving data\n");
210 + /* Indicate end of transfer, SEL goes up until next transfer */
211 + gpio_set_value(host->sel_gpio, 1);
212 + gpio_set_value(host->clk_gpio, 1);
213 + gpio_set_value(host->clk_gpio, 0);
216 + spin_unlock_irqrestore(&host->lock, flags);
222 + * cbus_read_reg - reads a given register from the device
223 + * @child: the child device
224 + * @dev: device address
225 + * @reg: register address
227 +int cbus_read_reg(struct device *child, unsigned dev, unsigned reg)
229 + struct cbus_host *host = dev_get_drvdata(child->parent);
231 + return cbus_transfer(host, CBUS_XFER_READ, dev, reg, 0);
233 +EXPORT_SYMBOL(cbus_read_reg);
236 + * cbus_write_reg - writes to a given register of the device
237 + * @child: the child device
238 + * @dev: device address
239 + * @reg: register address
240 + * @val: data to be written to @reg
242 +int cbus_write_reg(struct device *child, unsigned dev, unsigned reg,
245 + struct cbus_host *host = dev_get_drvdata(child->parent);
247 + return cbus_transfer(host, CBUS_XFER_WRITE, dev, reg, val);
249 +EXPORT_SYMBOL(cbus_write_reg);
251 +static int __init cbus_bus_probe(struct platform_device *pdev)
253 + struct cbus_host *chost;
254 + struct cbus_host_platform_data *pdata = pdev->dev.platform_data;
257 + chost = kzalloc(sizeof(*chost), GFP_KERNEL);
261 + spin_lock_init(&chost->lock);
263 + chost->clk_gpio = pdata->clk_gpio;
264 + chost->dat_gpio = pdata->dat_gpio;
265 + chost->sel_gpio = pdata->sel_gpio;
266 + chost->dev = &pdev->dev;
268 + ret = gpio_request(chost->clk_gpio, "CBUS clk");
272 + ret = gpio_request(chost->dat_gpio, "CBUS data");
276 + ret = gpio_request(chost->sel_gpio, "CBUS sel");
280 + gpio_direction_output(chost->clk_gpio, 0);
281 + gpio_direction_input(chost->dat_gpio);
282 + gpio_direction_output(chost->sel_gpio, 1);
284 + gpio_set_value(chost->clk_gpio, 1);
285 + gpio_set_value(chost->clk_gpio, 0);
287 + platform_set_drvdata(pdev, chost);
291 + gpio_free(chost->dat_gpio);
293 + gpio_free(chost->clk_gpio);
300 +static void __exit cbus_bus_remove(struct platform_device *pdev)
302 + struct cbus_host *chost = platform_get_drvdata(pdev);
304 + gpio_free(chost->sel_gpio);
305 + gpio_free(chost->dat_gpio);
306 + gpio_free(chost->clk_gpio);
311 +static struct platform_driver cbus_driver = {
312 + .remove = __exit_p(cbus_bus_remove),
318 +static int __init cbus_bus_init(void)
320 + return platform_driver_probe(&cbus_driver, cbus_bus_probe);
322 +subsys_initcall(cbus_bus_init);
324 +static void __exit cbus_bus_exit(void)
326 + platform_driver_unregister(&cbus_driver);
328 +module_exit(cbus_bus_exit);
330 +MODULE_DESCRIPTION("CBUS serial protocol");
331 +MODULE_LICENSE("GPL");
332 +MODULE_AUTHOR("Juha Yrjölä");
333 +MODULE_AUTHOR("David Weinehall");
334 +MODULE_AUTHOR("Mikko Ylinen");
335 +MODULE_AUTHOR("Felipe Balbi <felipe.balbi@nokia.com>");
338 +++ b/drivers/cbus/cbus.h
341 + * drivers/cbus/cbus.h
343 + * Copyright (C) 2004, 2005 Nokia Corporation
345 + * Written by Juha Yrjölä <juha.yrjola@nokia.com> and
346 + * David Weinehall <david.weinehall@nokia.com>
348 + * This file is subject to the terms and conditions of the GNU General
349 + * Public License. See the file "COPYING" in the main directory of this
350 + * archive for more details.
352 + * This program is distributed in the hope that it will be useful,
353 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
354 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
355 + * GNU General Public License for more details.
357 + * You should have received a copy of the GNU General Public License
358 + * along with this program; if not, write to the Free Software
359 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
362 +#ifndef __DRIVERS_CBUS_CBUS_H
363 +#define __DRIVERS_CBUS_CBUS_H
365 +extern int cbus_read_reg(struct device *, unsigned dev, unsigned reg);
366 +extern int cbus_write_reg(struct device *, unsigned dev, unsigned reg,
369 +#endif /* __DRIVERS_CBUS_CBUS_H */
371 +++ b/drivers/cbus/Kconfig
374 +# CBUS device configuration
380 + bool "CBUS support on OMAP"
382 + CBUS is a proprietary serial protocol by Nokia. It is mainly
383 + used for accessing Energy Management auxiliary chips.
385 + If you want CBUS support, you should say Y here.
389 + bool "Support for Tahvo"
391 + Tahvo is a mixed signal ASIC with some system features
393 + If you want Tahvo support, you should say Y here.
397 +config CBUS_TAHVO_USB
399 + depends on ARCH_OMAP
400 + select USB_OTG_UTILS
401 + tristate "Support for Tahvo USB transceiver"
403 + If you want Tahvo support for USB transceiver, say Y or M here.
405 +config CBUS_TAHVO_USB_HOST_BY_DEFAULT
406 + depends on CBUS_TAHVO_USB && USB_OTG
407 + boolean "Device in USB host mode by default"
409 + Say Y here, if you want the device to enter USB host mode
410 + by default on bootup.
416 + bool "Support for Retu"
418 + Retu is a mixed signal ASIC with some system features
420 + If you want Retu support, you should say Y here.
424 +config CBUS_RETU_POWERBUTTON
426 + bool "Support for Retu power button"
428 + The power button on Nokia 770 is connected to the Retu ASIC.
430 + If you want support for the Retu power button, you should say Y here.
432 +config CBUS_RETU_RTC
433 + depends on RTC_CLASS
434 + depends on ARCH_OMAP
435 + tristate "Support for Retu pseudo-RTC"
437 + Say Y here if you want support for the device that alleges to be an
438 + RTC in Retu. This will expose a sysfs interface for it.
440 +config CBUS_RETU_WDT
441 + depends on SYSFS && WATCHDOG
442 + depends on ARCH_OMAP
443 + tristate "Support for Retu watchdog timer"
445 + Say Y here if you want support for the watchdog in Retu. This will
446 + expose a sysfs interface to grok it.
448 +config CBUS_RETU_HEADSET
450 + tristate "Support for headset detection with Retu/Vilma"
452 + Say Y here if you want support detecting a headset that's connected
453 + to Retu/Vilma. Detection state and events are exposed through
460 +++ b/drivers/cbus/Makefile
463 +# Makefile for CBUS.
466 +obj-$(CONFIG_CBUS) += cbus.o
467 +obj-$(CONFIG_CBUS_TAHVO) += tahvo.o
468 +obj-$(CONFIG_CBUS_RETU) += retu.o
469 +obj-$(CONFIG_CBUS_TAHVO_USB) += tahvo-usb.o
471 +obj-$(CONFIG_CBUS_RETU_POWERBUTTON) += retu-pwrbutton.o
472 +obj-$(CONFIG_CBUS_RETU_RTC) += retu-rtc.o
473 +obj-$(CONFIG_CBUS_RETU_WDT) += retu-wdt.o
474 +obj-$(CONFIG_CBUS_RETU_HEADSET) += retu-headset.o
476 +++ b/drivers/cbus/retu.c
479 + * drivers/cbus/retu.c
481 + * Support functions for Retu ASIC
483 + * Copyright (C) 2004, 2005 Nokia Corporation
485 + * Written by Juha Yrjölä <juha.yrjola@nokia.com>,
486 + * David Weinehall <david.weinehall@nokia.com>, and
487 + * Mikko Ylinen <mikko.k.ylinen@nokia.com>
489 + * This file is subject to the terms and conditions of the GNU General
490 + * Public License. See the file "COPYING" in the main directory of this
491 + * archive for more details.
493 + * This program is distributed in the hope that it will be useful,
494 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
495 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
496 + * GNU General Public License for more details.
498 + * You should have received a copy of the GNU General Public License
499 + * along with this program; if not, write to the Free Software
500 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
503 +#include <linux/module.h>
504 +#include <linux/init.h>
506 +#include <linux/slab.h>
507 +#include <linux/kernel.h>
508 +#include <linux/errno.h>
509 +#include <linux/device.h>
510 +#include <linux/mutex.h>
511 +#include <linux/irq.h>
512 +#include <linux/interrupt.h>
513 +#include <linux/platform_device.h>
514 +#include <linux/platform_data/cbus.h>
516 +#include <asm/bitops.h>
523 + struct mutex mutex;
524 + struct device *dev;
542 +static struct retu *the_retu;
545 + * __retu_read_reg - Read a value from a register in Retu
546 + * @retu: pointer to retu structure
547 + * @reg: the register address to read from
549 +static int __retu_read_reg(struct retu *retu, unsigned reg)
551 + return cbus_read_reg(retu->dev, retu->devid, reg);
555 + * __retu_write_reg - Writes a value to a register in Retu
556 + * @retu: pointer to retu structure
557 + * @reg: the register address to write to
558 + * @val: the value to write to the register
560 +static void __retu_write_reg(struct retu *retu, unsigned reg, u16 val)
562 + cbus_write_reg(retu->dev, retu->devid, reg, val);
566 + * retu_read_reg - Read a value from a register in Retu
567 + * @child: device pointer for the calling child
568 + * @reg: the register to read from
570 + * This function returns the contents of the specified register
572 +int retu_read_reg(struct device *child, unsigned reg)
574 + struct retu *retu = dev_get_drvdata(child->parent);
576 + return __retu_read_reg(retu, reg);
578 +EXPORT_SYMBOL_GPL(retu_read_reg);
581 + * retu_write_reg - Write a value to a register in Retu
582 + * @child: the pointer to our calling child
583 + * @reg: the register to write to
584 + * @val: the value to write to the register
586 + * This function writes a value to the specified register
588 +void retu_write_reg(struct device *child, unsigned reg, u16 val)
590 + struct retu *retu = dev_get_drvdata(child->parent);
592 + mutex_lock(&retu->mutex);
593 + __retu_write_reg(retu, reg, val);
594 + mutex_unlock(&retu->mutex);
596 +EXPORT_SYMBOL_GPL(retu_write_reg);
599 + * retu_set_clear_reg_bits - helper function to read/set/clear bits
600 + * @child: device pointer to calling child
601 + * @reg: the register address
602 + * @set: mask for setting bits
603 + * @clear: mask for clearing bits
605 +void retu_set_clear_reg_bits(struct device *child, unsigned reg, u16 set,
608 + struct retu *retu = dev_get_drvdata(child->parent);
611 + mutex_lock(&retu->mutex);
612 + w = __retu_read_reg(retu, reg);
615 + __retu_write_reg(retu, reg, w);
616 + mutex_unlock(&retu->mutex);
618 +EXPORT_SYMBOL_GPL(retu_set_clear_reg_bits);
620 +#define ADC_MAX_CHAN_NUMBER 13
623 + * retu_read_adc - Reads AD conversion result
624 + * @child: device pointer to calling child
625 + * @channel: the ADC channel to read from
627 +int retu_read_adc(struct device *child, int channel)
629 + struct retu *retu = dev_get_drvdata(child->parent);
635 + if (channel < 0 || channel > ADC_MAX_CHAN_NUMBER)
638 + mutex_lock(&retu->mutex);
640 + if ((channel == 8) && retu->is_vilma) {
641 + int scr = __retu_read_reg(retu, RETU_REG_ADCSCR);
642 + int ch = (__retu_read_reg(retu, RETU_REG_ADCR) >> 10) & 0xf;
643 + if (((scr & 0xff) != 0) && (ch != 8))
644 + __retu_write_reg(retu, RETU_REG_ADCSCR, (scr & ~0xff));
647 + /* Select the channel and read result */
648 + __retu_write_reg(retu, RETU_REG_ADCR, channel << 10);
649 + res = __retu_read_reg(retu, RETU_REG_ADCR) & 0x3ff;
651 + if (retu->is_vilma)
652 + __retu_write_reg(retu, RETU_REG_ADCR, (1 << 13));
655 + mutex_unlock(&retu->mutex);
659 +EXPORT_SYMBOL_GPL(retu_read_adc);
661 +static irqreturn_t retu_irq_handler(int irq, void *_retu)
663 + struct retu *retu = _retu;
668 + mutex_lock(&retu->mutex);
669 + idr = __retu_read_reg(retu, RETU_REG_IDR);
670 + imr = __retu_read_reg(retu, RETU_REG_IMR);
671 + mutex_unlock(&retu->mutex);
675 + dev_vdbg(retu->dev, "No IRQ, spurious?\n");
680 + unsigned long pending = __ffs(idr);
683 + idr &= ~BIT(pending);
684 + irq = pending + retu->irq_base;
685 + handle_nested_irq(irq);
688 + return IRQ_HANDLED;
691 +/* -------------------------------------------------------------------------- */
693 +static void retu_irq_mask(struct irq_data *data)
695 + struct retu *retu = irq_data_get_irq_chip_data(data);
696 + int irq = data->irq;
698 + retu->mask |= (1 << (irq - retu->irq_base));
699 + retu->mask_pending = true;
702 +static void retu_irq_unmask(struct irq_data *data)
704 + struct retu *retu = irq_data_get_irq_chip_data(data);
705 + int irq = data->irq;
707 + retu->mask &= ~(1 << (irq - retu->irq_base));
708 + retu->mask_pending = true;
712 +static void retu_irq_ack(struct irq_data *data)
714 + struct retu *retu = irq_data_get_irq_chip_data(data);
715 + int irq = data->irq;
717 + retu->ack |= (1 << (irq - retu->irq_base));
718 + retu->ack_pending = true;
721 +static void retu_bus_lock(struct irq_data *data)
723 + struct retu *retu = irq_data_get_irq_chip_data(data);
725 + mutex_lock(&retu->mutex);
728 +static void retu_bus_sync_unlock(struct irq_data *data)
730 + struct retu *retu = irq_data_get_irq_chip_data(data);
732 + if (retu->mask_pending) {
733 + __retu_write_reg(retu, RETU_REG_IMR, retu->mask);
734 + retu->mask_pending = false;
737 + if (retu->ack_pending) {
738 + __retu_write_reg(retu, RETU_REG_IDR, retu->ack);
739 + retu->ack_pending = false;
742 + mutex_unlock(&retu->mutex);
745 +static struct irq_chip retu_irq_chip = {
747 + .irq_bus_lock = retu_bus_lock,
748 + .irq_bus_sync_unlock = retu_bus_sync_unlock,
749 + .irq_mask = retu_irq_mask,
750 + .irq_unmask = retu_irq_unmask,
751 + .irq_ack = retu_irq_ack,
754 +static inline void retu_irq_setup(int irq)
757 + set_irq_flags(irq, IRQF_VALID);
759 + irq_set_noprobe(irq);
763 +static void retu_irq_init(struct retu *retu)
765 + int base = retu->irq_base;
766 + int end = retu->irq_end;
769 + for (irq = base; irq < end; irq++) {
770 + irq_set_chip_data(irq, retu);
771 + irq_set_chip_and_handler(irq, &retu_irq_chip,
772 + handle_simple_irq);
773 + irq_set_nested_thread(irq, 1);
774 + retu_irq_setup(irq);
778 +static void retu_irq_exit(struct retu *retu)
780 + int base = retu->irq_base;
781 + int end = retu->irq_end;
784 + for (irq = base; irq < end; irq++) {
786 + set_irq_flags(irq, 0);
788 + irq_set_chip_and_handler(irq, NULL, NULL);
789 + irq_set_chip_data(irq, NULL);
793 +/* -------------------------------------------------------------------------- */
796 + * retu_power_off - Shut down power to system
798 + * This function puts the system in power off state
800 +static void retu_power_off(void)
802 + struct retu *retu = the_retu;
805 + reg = __retu_read_reg(retu, RETU_REG_CC1);
807 + /* Ignore power button state */
808 + __retu_write_reg(retu, RETU_REG_CC1, reg | 2);
809 + /* Expire watchdog immediately */
810 + __retu_write_reg(retu, RETU_REG_WATCHDOG, 0);
811 + /* Wait for poweroff*/
815 +static struct resource generic_resources[] = {
817 + .start = -EINVAL, /* fixed later */
818 + .flags = IORESOURCE_IRQ,
821 + .start = -EINVAL, /* fixed later */
822 + .flags = IORESOURCE_IRQ,
827 + * retu_allocate_child - Allocates one Retu child
828 + * @name: name of new child
829 + * @parent: parent device for this child
831 +static struct device *retu_allocate_child(char *name, struct device *parent,
832 + int irq_base, int irq1, int irq2, int num)
834 + struct platform_device *pdev;
837 + pdev = platform_device_alloc(name, -1);
839 + dev_dbg(parent, "can't allocate %s\n", name);
843 + pdev->dev.parent = parent;
846 + generic_resources[0].start = irq_base + irq1;
847 + generic_resources[1].start = irq_base + irq2;
849 + status = platform_device_add_resources(pdev,
850 + generic_resources, num);
852 + dev_dbg(parent, "can't add resources to %s\n", name);
857 + status = platform_device_add(pdev);
859 + dev_dbg(parent, "can't add %s\n", name);
866 + platform_device_put(pdev);
872 + * retu_allocate_children - Allocates Retu's children
874 +static int retu_allocate_children(struct device *parent, int irq_base)
876 + struct device *child;
878 + child = retu_allocate_child("retu-pwrbutton", parent, irq_base,
879 + RETU_INT_PWR, -1, 1);
883 + child = retu_allocate_child("retu-headset", parent, irq_base,
884 + RETU_INT_HOOK, -1, 1);
888 + child = retu_allocate_child("retu-rtc", parent, irq_base,
889 + RETU_INT_RTCS, RETU_INT_RTCA, 2);
893 + child = retu_allocate_child("retu-wdt", parent, -1, -1, -1, 0);
901 + * retu_probe - Probe for Retu ASIC
902 + * @dev: the Retu device
904 + * Probe for the Retu ASIC and allocate memory
905 + * for its device-struct if found
907 +static int __devinit retu_probe(struct platform_device *pdev)
910 + struct cbus_retu_platform_data *pdata = pdev->dev.platform_data;
915 + retu = kzalloc(sizeof(*retu), GFP_KERNEL);
917 + dev_err(&pdev->dev, "not enough memory\n");
921 + platform_set_drvdata(pdev, retu);
923 + ret = irq_alloc_descs(-1, 0, MAX_RETU_IRQ_HANDLERS, 0);
925 + dev_err(&pdev->dev, "failed to allocate IRQ descs\n");
929 + retu->irq = platform_get_irq(pdev, 0);
930 + retu->irq_base = ret;
931 + retu->irq_end = ret + MAX_RETU_IRQ_HANDLERS;
932 + retu->devid = pdata->devid;
933 + retu->dev = &pdev->dev;
936 + mutex_init(&retu->mutex);
938 + retu_irq_init(retu);
940 + rev = __retu_read_reg(retu, RETU_REG_ASICR) & 0xff;
941 + if (rev & (1 << 7))
942 + retu->is_vilma = true;
944 + dev_info(&pdev->dev, "%s v%d.%d found\n",
945 + retu->is_vilma ? "Vilma" : "Retu",
946 + (rev >> 4) & 0x07, rev & 0x0f);
948 + /* Mask all RETU interrupts */
949 + __retu_write_reg(retu, RETU_REG_IMR, 0xffff);
951 + ret = request_threaded_irq(retu->irq, NULL, retu_irq_handler,
952 + IRQF_ONESHOT, "retu", retu);
954 + dev_err(&pdev->dev, "Unable to register IRQ handler\n");
958 + irq_set_irq_wake(retu->irq, 1);
960 + /* Register power off function */
961 + pm_power_off = retu_power_off;
963 + ret = retu_allocate_children(&pdev->dev, retu->irq_base);
965 + dev_err(&pdev->dev, "Unable to allocate Retu children\n");
972 + pm_power_off = NULL;
973 + free_irq(retu->irq, retu);
976 + retu_irq_exit(retu);
977 + irq_free_descs(retu->irq_base, MAX_RETU_IRQ_HANDLERS);
987 +static int __devexit retu_remove(struct platform_device *pdev)
989 + struct retu *retu = platform_get_drvdata(pdev);
991 + pm_power_off = NULL;
994 + free_irq(retu->irq, retu);
995 + retu_irq_exit(retu);
996 + irq_free_descs(retu->irq_base, MAX_RETU_IRQ_HANDLERS);
1002 +static struct platform_driver retu_driver = {
1003 + .probe = retu_probe,
1004 + .remove = __devexit_p(retu_remove),
1010 +static int __init retu_init(void)
1012 + return platform_driver_register(&retu_driver);
1014 +subsys_initcall(retu_init);
1016 +static void __exit retu_exit(void)
1018 + platform_driver_unregister(&retu_driver);
1020 +module_exit(retu_exit);
1022 +MODULE_DESCRIPTION("Retu ASIC control");
1023 +MODULE_LICENSE("GPL");
1024 +MODULE_AUTHOR("Juha Yrjölä");
1025 +MODULE_AUTHOR("David Weinehall");
1026 +MODULE_AUTHOR("Mikko Ylinen");
1028 +++ b/drivers/cbus/retu.h
1031 + * drivers/cbus/retu.h
1033 + * Copyright (C) 2004, 2005 Nokia Corporation
1035 + * Written by Juha Yrjölä <juha.yrjola@nokia.com> and
1036 + * David Weinehall <david.weinehall@nokia.com>
1038 + * This file is subject to the terms and conditions of the GNU General
1039 + * Public License. See the file "COPYING" in the main directory of this
1040 + * archive for more details.
1042 + * This program is distributed in the hope that it will be useful,
1043 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1044 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1045 + * GNU General Public License for more details.
1047 + * You should have received a copy of the GNU General Public License
1048 + * along with this program; if not, write to the Free Software
1049 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1052 +#ifndef __DRIVERS_CBUS_RETU_H
1053 +#define __DRIVERS_CBUS_RETU_H
1055 +#include <linux/types.h>
1058 +#define RETU_REG_ASICR 0x00 /* ASIC ID & revision */
1059 +#define RETU_REG_IDR 0x01 /* Interrupt ID */
1060 +#define RETU_REG_IMR 0x02 /* Interrupt mask */
1061 +#define RETU_REG_RTCDSR 0x03 /* RTC seconds register */
1062 +#define RETU_REG_RTCHMR 0x04 /* RTC hours and minutes register */
1063 +#define RETU_REG_RTCHMAR 0x05 /* RTC hours and minutes alarm and time set register */
1064 +#define RETU_REG_RTCCALR 0x06 /* RTC calibration register */
1065 +#define RETU_REG_ADCR 0x08 /* ADC result */
1066 +#define RETU_REG_ADCSCR 0x09 /* ADC sample ctrl */
1067 +#define RETU_REG_CC1 0x0d /* Common control register 1 */
1068 +#define RETU_REG_CC2 0x0e /* Common control register 2 */
1069 +#define RETU_REG_CTRL_CLR 0x0f /* Regulator clear register */
1070 +#define RETU_REG_CTRL_SET 0x10 /* Regulator set register */
1071 +#define RETU_REG_STATUS 0x16 /* Status register */
1072 +#define RETU_REG_STATUS_BATAVAIL 0x0100 /* Battery available */
1073 +#define RETU_REG_STATUS_CHGPLUG 0x1000 /* Charger is plugged in */
1074 +#define RETU_REG_WATCHDOG 0x17 /* Watchdog register */
1075 +#define RETU_REG_AUDTXR 0x18 /* Audio Codec Tx register */
1076 +#define RETU_REG_MAX 0x1f
1078 +/* Interrupt sources */
1079 +#define RETU_INT_PWR 0
1080 +#define RETU_INT_CHAR 1
1081 +#define RETU_INT_RTCS 2
1082 +#define RETU_INT_RTCM 3
1083 +#define RETU_INT_RTCD 4
1084 +#define RETU_INT_RTCA 5
1085 +#define RETU_INT_HOOK 6
1086 +#define RETU_INT_HEAD 7
1087 +#define RETU_INT_ADCS 8
1089 +#define MAX_RETU_IRQ_HANDLERS 16
1092 +#define RETU_ADC_GND 0x00 /* Ground */
1093 +#define RETU_ADC_BSI 0x01 /* Battery Size Indicator */
1094 +#define RETU_ADC_BATTEMP 0x02 /* Battery temperature */
1095 +#define RETU_ADC_CHGVOLT 0x03 /* Charger voltage */
1096 +#define RETU_ADC_HEADSET 0x04 /* Headset detection */
1097 +#define RETU_ADC_HOOKDET 0x05 /* Hook detection */
1098 +#define RETU_ADC_RFGP 0x06 /* RF GP */
1099 +#define RETU_ADC_WBTX 0x07 /* Wideband Tx detection */
1100 +#define RETU_ADC_BATTVOLT 0x08 /* Battery voltage measurement */
1101 +#define RETU_ADC_GND2 0x09 /* Ground */
1102 +#define RETU_ADC_LIGHTSENS 0x0A /* Light sensor */
1103 +#define RETU_ADC_LIGHTTEMP 0x0B /* Light sensor temperature */
1104 +#define RETU_ADC_BKUPVOLT 0x0C /* Backup battery voltage */
1105 +#define RETU_ADC_TEMP 0x0D /* RETU temperature */
1108 +int retu_read_reg(struct device *child, unsigned reg);
1109 +void retu_write_reg(struct device *child, unsigned reg, u16 val);
1110 +void retu_set_clear_reg_bits(struct device *child, unsigned reg, u16 set,
1112 +int retu_read_adc(struct device *child, int channel);
1114 +#endif /* __DRIVERS_CBUS_RETU_H */
1116 +++ b/drivers/cbus/retu-headset.c
1119 + * Retu/Vilma headset detection
1121 + * Copyright (C) 2006 Nokia Corporation
1123 + * Written by Juha Yrjölä
1125 + * This file is subject to the terms and conditions of the GNU General
1126 + * Public License. See the file "COPYING" in the main directory of this
1127 + * archive for more details.
1129 + * This program is distributed in the hope that it will be useful,
1130 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1131 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1132 + * GNU General Public License for more details.
1134 + * You should have received a copy of the GNU General Public License
1135 + * along with this program; if not, write to the Free Software
1136 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1139 +#include <linux/module.h>
1140 +#include <linux/init.h>
1141 +#include <linux/kernel.h>
1142 +#include <linux/irq.h>
1143 +#include <linux/interrupt.h>
1144 +#include <linux/slab.h>
1145 +#include <linux/delay.h>
1146 +#include <linux/input.h>
1147 +#include <linux/platform_device.h>
1151 +#define RETU_ADC_CHANNEL_HOOKDET 0x05
1153 +#define RETU_HEADSET_KEY KEY_PHONE
1155 +struct retu_headset {
1157 + struct mutex mutex;
1158 + struct device *dev;
1159 + struct input_dev *idev;
1160 + unsigned bias_enabled;
1161 + unsigned detection_enabled;
1163 + struct timer_list enable_timer;
1164 + struct timer_list detect_timer;
1168 +static void retu_headset_set_bias(struct retu_headset *hs, int enable)
1171 + retu_set_clear_reg_bits(hs->dev, RETU_REG_AUDTXR,
1172 + (1 << 0) | (1 << 1), 0);
1174 + retu_set_clear_reg_bits(hs->dev, RETU_REG_AUDTXR,
1177 + retu_set_clear_reg_bits(hs->dev, RETU_REG_AUDTXR, 0,
1178 + (1 << 0) | (1 << 1) | (1 << 3));
1182 +static void retu_headset_enable(struct retu_headset *hs)
1184 + mutex_lock(&hs->mutex);
1185 + if (!hs->bias_enabled) {
1186 + hs->bias_enabled = 1;
1187 + retu_headset_set_bias(hs, 1);
1189 + mutex_unlock(&hs->mutex);
1192 +static void retu_headset_disable(struct retu_headset *hs)
1194 + mutex_lock(&hs->mutex);
1195 + if (hs->bias_enabled) {
1196 + hs->bias_enabled = 0;
1197 + retu_headset_set_bias(hs, 0);
1199 + mutex_unlock(&hs->mutex);
1202 +static void retu_headset_det_enable(struct retu_headset *hs)
1204 + mutex_lock(&hs->mutex);
1205 + if (!hs->detection_enabled) {
1206 + hs->detection_enabled = 1;
1207 + retu_set_clear_reg_bits(hs->dev, RETU_REG_CC1,
1208 + (1 << 10) | (1 << 8), 0);
1210 + mutex_unlock(&hs->mutex);
1213 +static void retu_headset_det_disable(struct retu_headset *hs)
1215 + unsigned long flags;
1217 + mutex_lock(&hs->mutex);
1218 + if (hs->detection_enabled) {
1219 + hs->detection_enabled = 0;
1220 + del_timer_sync(&hs->enable_timer);
1221 + del_timer_sync(&hs->detect_timer);
1222 + spin_lock_irqsave(&hs->lock, flags);
1224 + input_report_key(hs->idev, RETU_HEADSET_KEY, 0);
1225 + spin_unlock_irqrestore(&hs->lock, flags);
1226 + retu_set_clear_reg_bits(hs->dev, RETU_REG_CC1, 0,
1227 + (1 << 10) | (1 << 8));
1229 + mutex_unlock(&hs->mutex);
1232 +static ssize_t retu_headset_hookdet_show(struct device *dev,
1233 + struct device_attribute *attr,
1238 + val = retu_read_adc(dev, RETU_ADC_CHANNEL_HOOKDET);
1239 + return sprintf(buf, "%d\n", val);
1242 +static DEVICE_ATTR(hookdet, S_IRUGO, retu_headset_hookdet_show, NULL);
1244 +static ssize_t retu_headset_enable_show(struct device *dev,
1245 + struct device_attribute *attr,
1248 + struct retu_headset *hs = dev_get_drvdata(dev);
1250 + return sprintf(buf, "%u\n", hs->bias_enabled);
1253 +static ssize_t retu_headset_enable_store(struct device *dev,
1254 + struct device_attribute *attr,
1255 + const char *buf, size_t count)
1257 + struct retu_headset *hs = dev_get_drvdata(dev);
1260 + if (sscanf(buf, "%u", &enable) != 1)
1263 + retu_headset_enable(hs);
1265 + retu_headset_disable(hs);
1269 +static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
1270 + retu_headset_enable_show, retu_headset_enable_store);
1272 +static ssize_t retu_headset_enable_det_show(struct device *dev,
1273 + struct device_attribute *attr,
1276 + struct retu_headset *hs = dev_get_drvdata(dev);
1278 + return sprintf(buf, "%u\n", hs->detection_enabled);
1281 +static ssize_t retu_headset_enable_det_store(struct device *dev,
1282 + struct device_attribute *attr,
1283 + const char *buf, size_t count)
1285 + struct retu_headset *hs = dev_get_drvdata(dev);
1288 + if (sscanf(buf, "%u", &enable) != 1)
1291 + retu_headset_det_enable(hs);
1293 + retu_headset_det_disable(hs);
1297 +static DEVICE_ATTR(enable_det, S_IRUGO | S_IWUSR | S_IWGRP,
1298 + retu_headset_enable_det_show,
1299 + retu_headset_enable_det_store);
1301 +static irqreturn_t retu_headset_hook_interrupt(int irq, void *_hs)
1303 + struct retu_headset *hs = _hs;
1304 + unsigned long flags;
1306 + spin_lock_irqsave(&hs->lock, flags);
1307 + if (!hs->pressed) {
1308 + /* Headset button was just pressed down. */
1310 + input_report_key(hs->idev, RETU_HEADSET_KEY, 1);
1312 + spin_unlock_irqrestore(&hs->lock, flags);
1313 + retu_set_clear_reg_bits(hs->dev, RETU_REG_CC1, 0,
1314 + (1 << 10) | (1 << 8));
1315 + mod_timer(&hs->enable_timer, jiffies + msecs_to_jiffies(50));
1317 + return IRQ_HANDLED;
1320 +static void retu_headset_enable_timer(unsigned long arg)
1322 + struct retu_headset *hs = (struct retu_headset *) arg;
1324 + retu_set_clear_reg_bits(hs->dev, RETU_REG_CC1,
1325 + (1 << 10) | (1 << 8), 0);
1326 + mod_timer(&hs->detect_timer, jiffies + msecs_to_jiffies(350));
1329 +static void retu_headset_detect_timer(unsigned long arg)
1331 + struct retu_headset *hs = (struct retu_headset *) arg;
1332 + unsigned long flags;
1334 + spin_lock_irqsave(&hs->lock, flags);
1335 + if (hs->pressed) {
1337 + input_report_key(hs->idev, RETU_HEADSET_KEY, 0);
1339 + spin_unlock_irqrestore(&hs->lock, flags);
1342 +static int __init retu_headset_probe(struct platform_device *pdev)
1344 + struct retu_headset *hs;
1348 + hs = kzalloc(sizeof(*hs), GFP_KERNEL);
1352 + hs->dev = &pdev->dev;
1354 + hs->idev = input_allocate_device();
1355 + if (hs->idev == NULL) {
1359 + hs->idev->name = "retu-headset";
1360 + hs->idev->dev.parent = &pdev->dev;
1361 + set_bit(EV_KEY, hs->idev->evbit);
1362 + set_bit(RETU_HEADSET_KEY, hs->idev->keybit);
1363 + r = input_register_device(hs->idev);
1367 + r = device_create_file(&pdev->dev, &dev_attr_hookdet);
1370 + r = device_create_file(&pdev->dev, &dev_attr_enable);
1373 + r = device_create_file(&pdev->dev, &dev_attr_enable_det);
1376 + platform_set_drvdata(pdev, hs);
1378 + spin_lock_init(&hs->lock);
1379 + mutex_init(&hs->mutex);
1380 + setup_timer(&hs->enable_timer, retu_headset_enable_timer,
1381 + (unsigned long) hs);
1382 + setup_timer(&hs->detect_timer, retu_headset_detect_timer,
1383 + (unsigned long) hs);
1385 + irq = platform_get_irq(pdev, 0);
1388 + r = request_threaded_irq(irq, NULL, retu_headset_hook_interrupt, 0,
1391 + dev_err(&pdev->dev, "hookdet IRQ not available\n");
1397 + device_remove_file(&pdev->dev, &dev_attr_enable_det);
1399 + device_remove_file(&pdev->dev, &dev_attr_enable);
1401 + device_remove_file(&pdev->dev, &dev_attr_hookdet);
1403 + input_unregister_device(hs->idev);
1405 + input_free_device(hs->idev);
1411 +static int retu_headset_remove(struct platform_device *pdev)
1413 + struct retu_headset *hs = platform_get_drvdata(pdev);
1415 + device_remove_file(&pdev->dev, &dev_attr_hookdet);
1416 + device_remove_file(&pdev->dev, &dev_attr_enable);
1417 + device_remove_file(&pdev->dev, &dev_attr_enable_det);
1418 + retu_headset_disable(hs);
1419 + retu_headset_det_disable(hs);
1420 + free_irq(hs->irq, hs);
1421 + input_unregister_device(hs->idev);
1422 + input_free_device(hs->idev);
1427 +static int retu_headset_suspend(struct platform_device *pdev,
1428 + pm_message_t mesg)
1430 + struct retu_headset *hs = platform_get_drvdata(pdev);
1432 + mutex_lock(&hs->mutex);
1433 + if (hs->bias_enabled)
1434 + retu_headset_set_bias(hs, 0);
1435 + mutex_unlock(&hs->mutex);
1440 +static int retu_headset_resume(struct platform_device *pdev)
1442 + struct retu_headset *hs = platform_get_drvdata(pdev);
1444 + mutex_lock(&hs->mutex);
1445 + if (hs->bias_enabled)
1446 + retu_headset_set_bias(hs, 1);
1447 + mutex_unlock(&hs->mutex);
1452 +static struct platform_driver retu_headset_driver = {
1453 + .remove = retu_headset_remove,
1454 + .suspend = retu_headset_suspend,
1455 + .resume = retu_headset_resume,
1457 + .name = "retu-headset",
1461 +static int __init retu_headset_init(void)
1463 + return platform_driver_probe(&retu_headset_driver, retu_headset_probe);
1466 +static void __exit retu_headset_exit(void)
1468 + platform_driver_unregister(&retu_headset_driver);
1471 +module_init(retu_headset_init);
1472 +module_exit(retu_headset_exit);
1474 +MODULE_DESCRIPTION("Retu/Vilma headset detection");
1475 +MODULE_LICENSE("GPL");
1476 +MODULE_AUTHOR("Juha Yrjölä");
1478 +++ b/drivers/cbus/retu-pwrbutton.c
1481 + * drivers/cbus/retu-pwrbutton.c
1483 + * Driver for sending retu power button event to input-layer
1485 + * Copyright (C) 2004-2010 Nokia Corporation
1488 + * Ari Saastamoinen <ari.saastamoinen@elektrobit.com>
1489 + * Juha Yrjola <juha.yrjola@solidboot.com>
1491 + * Contact: Felipe Balbi <felipe.balbi@nokia.com>
1493 + * This file is subject to the terms and conditions of the GNU General
1494 + * Public License. See the file "COPYING" in the main directory of this
1495 + * archive for more details.
1497 + * This program is distributed in the hope that it will be useful,
1498 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1499 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1500 + * GNU General Public License for more details.
1502 + * You should have received a copy of the GNU General Public License
1503 + * along with this program; if not, write to the Free Software
1504 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1507 +#include <linux/module.h>
1508 +#include <linux/init.h>
1509 +#include <linux/kernel.h>
1510 +#include <linux/errno.h>
1511 +#include <linux/input.h>
1512 +#include <linux/jiffies.h>
1513 +#include <linux/bitops.h>
1514 +#include <linux/irq.h>
1515 +#include <linux/interrupt.h>
1516 +#include <linux/platform_device.h>
1517 +#include <linux/slab.h>
1521 +#define RETU_STATUS_PWRONX (1 << 5)
1523 +#define PWRBTN_DELAY 20
1524 +#define PWRBTN_UP 0
1525 +#define PWRBTN_PRESSED 1
1527 +struct retu_pwrbutton {
1528 + struct input_dev *idev;
1529 + struct device *dev;
1535 +static irqreturn_t retubutton_irq(int irq, void *_pwr)
1537 + struct retu_pwrbutton *pwr = _pwr;
1540 + if (retu_read_reg(pwr->dev, RETU_REG_STATUS) & RETU_STATUS_PWRONX)
1541 + state = PWRBTN_UP;
1543 + state = PWRBTN_PRESSED;
1545 + if (pwr->state != state) {
1546 + input_report_key(pwr->idev, KEY_POWER, state);
1547 + input_sync(pwr->idev);
1548 + pwr->state = state;
1551 + return IRQ_HANDLED;
1554 +static int __init retubutton_probe(struct platform_device *pdev)
1556 + struct retu_pwrbutton *pwr;
1559 + pwr = kzalloc(sizeof(*pwr), GFP_KERNEL);
1561 + dev_err(&pdev->dev, "not enough memory\n");
1566 + pwr->dev = &pdev->dev;
1567 + pwr->irq = platform_get_irq(pdev, 0);
1568 + platform_set_drvdata(pdev, pwr);
1570 + ret = request_threaded_irq(pwr->irq, NULL, retubutton_irq, 0,
1571 + "retu-pwrbutton", pwr);
1573 + dev_err(&pdev->dev, "Cannot allocate irq\n");
1577 + pwr->idev = input_allocate_device();
1579 + dev_err(&pdev->dev, "can't allocate input device\n");
1584 + pwr->idev->evbit[0] = BIT_MASK(EV_KEY);
1585 + pwr->idev->keybit[BIT_WORD(KEY_POWER)] = BIT_MASK(KEY_POWER);
1586 + pwr->idev->name = "retu-pwrbutton";
1588 + ret = input_register_device(pwr->idev);
1590 + dev_err(&pdev->dev, "failed to register input device\n");
1597 + input_free_device(pwr->idev);
1600 + free_irq(pwr->irq, pwr);
1609 +static int __exit retubutton_remove(struct platform_device *pdev)
1611 + struct retu_pwrbutton *pwr = platform_get_drvdata(pdev);
1613 + free_irq(pwr->irq, pwr);
1614 + input_unregister_device(pwr->idev);
1615 + input_free_device(pwr->idev);
1621 +static struct platform_driver retu_pwrbutton_driver = {
1622 + .remove = __exit_p(retubutton_remove),
1624 + .name = "retu-pwrbutton",
1628 +static int __init retubutton_init(void)
1630 + return platform_driver_probe(&retu_pwrbutton_driver, retubutton_probe);
1632 +module_init(retubutton_init);
1634 +static void __exit retubutton_exit(void)
1636 + platform_driver_unregister(&retu_pwrbutton_driver);
1638 +module_exit(retubutton_exit);
1640 +MODULE_DESCRIPTION("Retu Power Button");
1641 +MODULE_LICENSE("GPL");
1642 +MODULE_AUTHOR("Ari Saastamoinen");
1643 +MODULE_AUTHOR("Felipe Balbi <felipe.balbi@nokia.com>");
1646 +++ b/drivers/cbus/retu-rtc.c
1649 + * drivers/cbus/retu-rtc.c
1651 + * Support for Retu RTC
1653 + * Copyright (C) 2004, 2005 Nokia Corporation
1655 + * Written by Paul Mundt <paul.mundt@nokia.com> and
1656 + * Igor Stoppa <igor.stoppa@nokia.com>
1658 + * The Retu RTC is essentially a partial read-only RTC that gives us Retu's
1659 + * idea of what time actually is. It's left as a userspace excercise to map
1660 + * this back to time in the real world and ensure that calibration settings
1661 + * are sane to compensate for any horrible drift (on account of not being able
1662 + * to set the clock to anything).
1664 + * Days are semi-writeable. Namely, Retu will only track 255 days for us
1665 + * consecutively, after which the counter is explicitly stuck at 255 until
1666 + * someone comes along and clears it with a write. In the event that no one
1667 + * comes along and clears it, we no longer have any idea what day it is.
1669 + * This file is subject to the terms and conditions of the GNU General
1670 + * Public License. See the file "COPYING" in the main directory of this
1671 + * archive for more details.
1673 + * This program is distributed in the hope that it will be useful,
1674 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1675 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1676 + * GNU General Public License for more details.
1678 + * You should have received a copy of the GNU General Public License
1679 + * along with this program; if not, write to the Free Software
1680 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1683 +#include <linux/device.h>
1684 +#include <linux/init.h>
1685 +#include <linux/kernel.h>
1686 +#include <linux/slab.h>
1687 +#include <linux/module.h>
1688 +#include <linux/platform_device.h>
1689 +#include <linux/mutex.h>
1690 +#include <linux/rtc.h>
1697 + struct mutex mutex;
1698 + struct device *dev;
1699 + struct rtc_device *rtc;
1701 + u16 alarm_expired;
1706 +static void retu_rtc_do_reset(struct retu_rtc *rtc)
1710 + ccr1 = retu_read_reg(rtc->dev, RETU_REG_CC1);
1711 + /* RTC in reset */
1712 + retu_write_reg(rtc->dev, RETU_REG_CC1, ccr1 | 0x0001);
1713 + /* RTC in normal operating mode */
1714 + retu_write_reg(rtc->dev, RETU_REG_CC1, ccr1 & ~0x0001);
1716 + /* Disable alarm and RTC WD */
1717 + retu_write_reg(rtc->dev, RETU_REG_RTCHMAR, 0x7f3f);
1718 + /* Set Calibration register to default value */
1719 + retu_write_reg(rtc->dev, RETU_REG_RTCCALR, 0x00c0);
1721 + rtc->alarm_expired = 0;
1724 +static irqreturn_t retu_rtc_interrupt(int irq, void *_rtc)
1726 + struct retu_rtc *rtc = _rtc;
1728 + mutex_lock(&rtc->mutex);
1729 + rtc->alarm_expired = 1;
1730 + retu_write_reg(rtc->dev, RETU_REG_RTCHMAR, (24 << 8) | 60);
1731 + mutex_unlock(&rtc->mutex);
1733 + return IRQ_HANDLED;
1736 +static int retu_rtc_init_irq(struct retu_rtc *rtc)
1741 + irq = platform_get_irq(to_platform_device(rtc->dev), 0);
1742 + rtc->irq_rtcs = irq;
1744 + irq = platform_get_irq(to_platform_device(rtc->dev), 1);
1745 + rtc->irq_rtca = irq;
1747 + ret = request_threaded_irq(rtc->irq_rtcs, NULL, retu_rtc_interrupt,
1752 + ret = request_threaded_irq(rtc->irq_rtca, NULL, retu_rtc_interrupt,
1755 + free_irq(rtc->irq_rtcs, rtc);
1762 +static int retu_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
1764 + struct retu_rtc *rtc = dev_get_drvdata(dev);
1767 + mutex_lock(&rtc->mutex);
1769 + chmar = ((alm->time.tm_hour & 0x1f) << 8) | (alm->time.tm_min & 0x3f);
1770 + retu_write_reg(rtc->dev, RETU_REG_RTCHMAR, chmar);
1772 + mutex_unlock(&rtc->mutex);
1777 +static int retu_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
1779 + struct retu_rtc *rtc = dev_get_drvdata(dev);
1782 + mutex_lock(&rtc->mutex);
1784 + chmar = retu_read_reg(rtc->dev, RETU_REG_RTCHMAR);
1786 + alm->time.tm_hour = (chmar >> 8) & 0x1f;
1787 + alm->time.tm_min = chmar & 0x3f;
1788 + alm->enabled = !!rtc->alarm_expired;
1790 + mutex_unlock(&rtc->mutex);
1795 +static int retu_rtc_set_time(struct device *dev, struct rtc_time *tm)
1797 + struct retu_rtc *rtc = dev_get_drvdata(dev);
1801 + dsr = ((tm->tm_mday & 0xff) << 8) | (tm->tm_hour & 0xff);
1802 + hmr = ((tm->tm_min & 0xff) << 8) | (tm->tm_sec & 0xff);
1804 + mutex_lock(&rtc->mutex);
1806 + retu_write_reg(rtc->dev, RETU_REG_RTCDSR, dsr);
1807 + retu_write_reg(rtc->dev, RETU_REG_RTCHMR, hmr);
1809 + mutex_unlock(&rtc->mutex);
1814 +static int retu_rtc_read_time(struct device *dev, struct rtc_time *tm)
1816 + struct retu_rtc *rtc = dev_get_drvdata(dev);
1821 + * DSR holds days and hours
1822 + * HMR hols minutes and seconds
1824 + * both are 16 bit registers with 8-bit for each field.
1827 + mutex_lock(&rtc->mutex);
1829 + dsr = retu_read_reg(rtc->dev, RETU_REG_RTCDSR);
1830 + hmr = retu_read_reg(rtc->dev, RETU_REG_RTCHMR);
1832 + tm->tm_sec = hmr & 0xff;
1833 + tm->tm_min = hmr >> 8;
1834 + tm->tm_hour = dsr & 0xff;
1835 + tm->tm_mday = dsr >> 8;
1837 + mutex_unlock(&rtc->mutex);
1842 +static struct rtc_class_ops retu_rtc_ops = {
1843 + .read_time = retu_rtc_read_time,
1844 + .set_time = retu_rtc_set_time,
1845 + .read_alarm = retu_rtc_read_alarm,
1846 + .set_alarm = retu_rtc_set_alarm,
1849 +static int __init retu_rtc_probe(struct platform_device *pdev)
1851 + struct retu_rtc *rtc;
1854 + rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
1856 + dev_err(&pdev->dev, "not enough memory\n");
1861 + rtc->dev = &pdev->dev;
1862 + platform_set_drvdata(pdev, rtc);
1863 + mutex_init(&rtc->mutex);
1865 + rtc->alarm_expired = retu_read_reg(rtc->dev, RETU_REG_IDR) &
1866 + (0x1 << RETU_INT_RTCA);
1868 + r = retu_rtc_init_irq(rtc);
1870 + dev_err(&pdev->dev, "failed to request retu irq\n");
1874 + /* If the calibration register is zero, we've probably lost power */
1875 + if (!(retu_read_reg(rtc->dev, RETU_REG_RTCCALR) & 0x00ff))
1876 + retu_rtc_do_reset(rtc);
1878 + rtc->rtc = rtc_device_register(pdev->name, &pdev->dev, &
1879 + retu_rtc_ops, THIS_MODULE);
1880 + if (IS_ERR(rtc->rtc)) {
1881 + dev_err(&pdev->dev, "can't register RTC device\n");
1888 + free_irq(rtc->irq_rtcs, rtc);
1889 + free_irq(rtc->irq_rtca, rtc);
1898 +static int __devexit retu_rtc_remove(struct platform_device *pdev)
1900 + struct retu_rtc *rtc = platform_get_drvdata(pdev);
1902 + free_irq(rtc->irq_rtcs, rtc);
1903 + free_irq(rtc->irq_rtca, rtc);
1904 + rtc_device_unregister(rtc->rtc);
1910 +static struct platform_driver retu_rtc_driver = {
1911 + .remove = __exit_p(retu_rtc_remove),
1913 + .name = "retu-rtc",
1917 +static int __init retu_rtc_init(void)
1919 + return platform_driver_probe(&retu_rtc_driver, retu_rtc_probe);
1921 +module_init(retu_rtc_init);
1923 +static void __exit retu_rtc_exit(void)
1925 + platform_driver_unregister(&retu_rtc_driver);
1927 +module_exit(retu_rtc_exit);
1929 +MODULE_DESCRIPTION("Retu RTC");
1930 +MODULE_LICENSE("GPL");
1931 +MODULE_AUTHOR("Paul Mundt");
1932 +MODULE_AUTHOR("Igor Stoppa");
1933 +MODULE_AUTHOR("Felipe Balbi <felipe.balbi@nokia.com>");
1936 +++ b/drivers/cbus/retu-wdt.c
1939 + * drivers/cbus/retu-wdt.c
1941 + * Driver for Retu watchdog
1943 + * Copyright (C) 2004, 2005 Nokia Corporation
1945 + * Written by Amit Kucheria <amit.kucheria@nokia.com>
1947 + * Cleanups by Michael Buesch <mb@bu3sch.de> (C) 2011
1949 + * This file is subject to the terms and conditions of the GNU General
1950 + * Public License. See the file "COPYING" in the main directory of this
1951 + * archive for more details.
1953 + * This program is distributed in the hope that it will be useful,
1954 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1955 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1956 + * GNU General Public License for more details.
1958 + * You should have received a copy of the GNU General Public License
1959 + * along with this program; if not, write to the Free Software
1960 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1963 +#include <linux/kernel.h>
1964 +#include <linux/slab.h>
1965 +#include <linux/module.h>
1966 +#include <linux/device.h>
1967 +#include <linux/init.h>
1968 +#include <linux/fs.h>
1969 +#include <linux/io.h>
1970 +#include <linux/platform_device.h>
1972 +#include <linux/completion.h>
1973 +#include <linux/errno.h>
1974 +#include <linux/moduleparam.h>
1975 +#include <linux/miscdevice.h>
1976 +#include <linux/watchdog.h>
1978 +#include <asm/uaccess.h>
1980 +#include <plat/prcm.h>
1985 +/* Watchdog timeout in seconds */
1986 +#define RETU_WDT_MIN_TIMER 0
1987 +#define RETU_WDT_DEFAULT_TIMER 32
1988 +#define RETU_WDT_MAX_TIMER 63
1990 +struct retu_wdt_dev {
1991 + struct device *dev;
1992 + unsigned int period_val; /* Current period of watchdog */
1993 + unsigned long users;
1994 + struct miscdevice miscdev;
1995 + struct delayed_work ping_work;
1996 + struct mutex mutex;
2000 +static inline void _retu_modify_counter(struct retu_wdt_dev *wdev,
2003 + retu_write_reg(wdev->dev, RETU_REG_WATCHDOG, (u16)new);
2006 +static int retu_modify_counter(struct retu_wdt_dev *wdev, unsigned int new)
2008 + if (new < RETU_WDT_MIN_TIMER || new > RETU_WDT_MAX_TIMER)
2011 + mutex_lock(&wdev->mutex);
2012 + wdev->period_val = new;
2013 + _retu_modify_counter(wdev, wdev->period_val);
2014 + mutex_unlock(&wdev->mutex);
2020 + * Since retu watchdog cannot be disabled in hardware, we must kick it
2021 + * with a timer until userspace watchdog software takes over. Do this
2022 + * unless /dev/watchdog is open or CONFIG_WATCHDOG_NOWAYOUT is set.
2024 +static void retu_wdt_ping_enable(struct retu_wdt_dev *wdev)
2026 + _retu_modify_counter(wdev, RETU_WDT_MAX_TIMER);
2027 + schedule_delayed_work(&wdev->ping_work,
2028 + round_jiffies_relative(RETU_WDT_DEFAULT_TIMER * HZ));
2031 +static void retu_wdt_ping_disable(struct retu_wdt_dev *wdev)
2033 + _retu_modify_counter(wdev, RETU_WDT_MAX_TIMER);
2034 + cancel_delayed_work_sync(&wdev->ping_work);
2037 +static void retu_wdt_ping_work(struct work_struct *work)
2039 + struct retu_wdt_dev *wdev = container_of(to_delayed_work(work),
2040 + struct retu_wdt_dev, ping_work);
2041 + retu_wdt_ping_enable(wdev);
2044 +static int retu_wdt_open(struct inode *inode, struct file *file)
2046 + struct miscdevice *mdev = file->private_data;
2047 + struct retu_wdt_dev *wdev = container_of(mdev, struct retu_wdt_dev, miscdev);
2049 + if (test_and_set_bit(0, &wdev->users))
2052 + retu_wdt_ping_disable(wdev);
2054 + return nonseekable_open(inode, file);
2057 +static int retu_wdt_release(struct inode *inode, struct file *file)
2059 + struct miscdevice *mdev = file->private_data;
2060 + struct retu_wdt_dev *wdev = container_of(mdev, struct retu_wdt_dev, miscdev);
2062 +#ifndef CONFIG_WATCHDOG_NOWAYOUT
2063 + retu_wdt_ping_enable(wdev);
2065 + clear_bit(0, &wdev->users);
2070 +static ssize_t retu_wdt_write(struct file *file, const char __user *data,
2071 + size_t len, loff_t *ppos)
2073 + struct miscdevice *mdev = file->private_data;
2074 + struct retu_wdt_dev *wdev = container_of(mdev, struct retu_wdt_dev, miscdev);
2077 + retu_modify_counter(wdev, RETU_WDT_MAX_TIMER);
2082 +static long retu_wdt_ioctl(struct file *file, unsigned int cmd,
2083 + unsigned long arg)
2085 + struct miscdevice *mdev = file->private_data;
2086 + struct retu_wdt_dev *wdev = container_of(mdev, struct retu_wdt_dev, miscdev);
2089 + static const struct watchdog_info ident = {
2090 + .identity = "Retu Watchdog",
2091 + .options = WDIOF_SETTIMEOUT,
2092 + .firmware_version = 0,
2098 + case WDIOC_GETSUPPORT:
2099 + return copy_to_user((struct watchdog_info __user *)arg, &ident,
2101 + case WDIOC_GETSTATUS:
2102 + return put_user(0, (int __user *)arg);
2103 + case WDIOC_GETBOOTSTATUS:
2104 + if (cpu_is_omap16xx())
2105 + return put_user(omap_readw(ARM_SYSST),
2106 + (int __user *)arg);
2107 + if (cpu_is_omap24xx())
2108 + return put_user(omap_prcm_get_reset_sources(),
2109 + (int __user *)arg);
2110 + case WDIOC_KEEPALIVE:
2111 + retu_modify_counter(wdev, RETU_WDT_MAX_TIMER);
2113 + case WDIOC_SETTIMEOUT:
2114 + if (get_user(new_margin, (int __user *)arg))
2116 + retu_modify_counter(wdev, new_margin);
2117 + /* Fall through */
2118 + case WDIOC_GETTIMEOUT:
2119 + return put_user(wdev->period_val, (int __user *)arg);
2125 +static const struct file_operations retu_wdt_fops = {
2126 + .owner = THIS_MODULE,
2127 + .write = retu_wdt_write,
2128 + .unlocked_ioctl = retu_wdt_ioctl,
2129 + .open = retu_wdt_open,
2130 + .release = retu_wdt_release,
2133 +static int __init retu_wdt_probe(struct platform_device *pdev)
2135 + struct retu_wdt_dev *wdev;
2138 + wdev = kzalloc(sizeof(struct retu_wdt_dev), GFP_KERNEL);
2142 + wdev->dev = &pdev->dev;
2143 + wdev->period_val = RETU_WDT_DEFAULT_TIMER;
2144 + mutex_init(&wdev->mutex);
2146 + platform_set_drvdata(pdev, wdev);
2148 + wdev->miscdev.parent = &pdev->dev;
2149 + wdev->miscdev.minor = WATCHDOG_MINOR;
2150 + wdev->miscdev.name = "watchdog";
2151 + wdev->miscdev.fops = &retu_wdt_fops;
2153 + ret = misc_register(&wdev->miscdev);
2155 + goto err_free_wdev;
2157 + INIT_DELAYED_WORK(&wdev->ping_work, retu_wdt_ping_work);
2159 + /* Kick the watchdog for kernel booting to finish.
2160 + * If nowayout is not set, we start the ping work. */
2161 +#ifdef CONFIG_WATCHDOG_NOWAYOUT
2162 + retu_modify_counter(wdev, RETU_WDT_MAX_TIMER);
2164 + retu_wdt_ping_enable(wdev);
2175 +static int __devexit retu_wdt_remove(struct platform_device *pdev)
2177 + struct retu_wdt_dev *wdev;
2179 + wdev = platform_get_drvdata(pdev);
2180 + misc_deregister(&wdev->miscdev);
2181 + cancel_delayed_work_sync(&wdev->ping_work);
2187 +static struct platform_driver retu_wdt_driver = {
2188 + .remove = __exit_p(retu_wdt_remove),
2190 + .name = "retu-wdt",
2194 +static int __init retu_wdt_init(void)
2196 + return platform_driver_probe(&retu_wdt_driver, retu_wdt_probe);
2199 +static void __exit retu_wdt_exit(void)
2201 + platform_driver_unregister(&retu_wdt_driver);
2204 +module_init(retu_wdt_init);
2205 +module_exit(retu_wdt_exit);
2207 +MODULE_DESCRIPTION("Retu WatchDog");
2208 +MODULE_AUTHOR("Amit Kucheria");
2209 +MODULE_LICENSE("GPL");
2211 +++ b/drivers/cbus/tahvo.c
2214 + * drivers/cbus/tahvo.c
2216 + * Support functions for Tahvo ASIC
2218 + * Copyright (C) 2004, 2005 Nokia Corporation
2220 + * Written by Juha Yrjölä <juha.yrjola@nokia.com>,
2221 + * David Weinehall <david.weinehall@nokia.com>, and
2222 + * Mikko Ylinen <mikko.k.ylinen@nokia.com>
2224 + * This file is subject to the terms and conditions of the GNU General
2225 + * Public License. See the file "COPYING" in the main directory of this
2226 + * archive for more details.
2228 + * This program is distributed in the hope that it will be useful,
2229 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2230 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2231 + * GNU General Public License for more details.
2233 + * You should have received a copy of the GNU General Public License
2234 + * along with this program; if not, write to the Free Software
2235 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2238 +#include <linux/module.h>
2239 +#include <linux/init.h>
2241 +#include <linux/slab.h>
2242 +#include <linux/kernel.h>
2243 +#include <linux/errno.h>
2244 +#include <linux/device.h>
2245 +#include <linux/irq.h>
2246 +#include <linux/interrupt.h>
2247 +#include <linux/platform_device.h>
2248 +#include <linux/platform_data/cbus.h>
2249 +#include <linux/mutex.h>
2256 + struct mutex mutex;
2257 + struct device *dev;
2266 + unsigned int mask_pending:1;
2267 + unsigned int ack_pending:1;
2268 + unsigned int is_betty:1;
2272 + * __tahvo_read_reg - Reads a value from a register in Tahvo
2273 + * @tahvo: pointer to tahvo structure
2274 + * @reg: the register address to read from
2276 +static int __tahvo_read_reg(struct tahvo *tahvo, unsigned reg)
2278 + return cbus_read_reg(tahvo->dev, CBUS_TAHVO_DEVICE_ID, reg);
2282 + * __tahvo_write_reg - Writes a value to a register in Tahvo
2283 + * @tahvo: pointer to tahvo structure
2284 + * @reg: register address to write to
2285 + * @val: the value to be written to @reg
2287 +static void __tahvo_write_reg(struct tahvo *tahvo, unsigned reg, u16 val)
2289 + cbus_write_reg(tahvo->dev, CBUS_TAHVO_DEVICE_ID, reg, val);
2293 + * tahvo_read_reg - Read a value from a register in Tahvo
2294 + * @child: device pointer from the calling child
2295 + * @reg: the register to read from
2297 + * This function returns the contents of the specified register
2299 +int tahvo_read_reg(struct device *child, unsigned reg)
2301 + struct tahvo *tahvo = dev_get_drvdata(child->parent);
2303 + return __tahvo_read_reg(tahvo, reg);
2305 +EXPORT_SYMBOL(tahvo_read_reg);
2308 + * tahvo_write_reg - Write a value to a register in Tahvo
2309 + * @child: device pointer from the calling child
2310 + * @reg: the register to write to
2311 + * @val : the value to write to the register
2313 + * This function writes a value to the specified register
2315 +void tahvo_write_reg(struct device *child, unsigned reg, u16 val)
2317 + struct tahvo *tahvo = dev_get_drvdata(child->parent);
2319 + __tahvo_write_reg(tahvo, reg, val);
2321 +EXPORT_SYMBOL(tahvo_write_reg);
2324 + * tahvo_set_clear_reg_bits - set and clear register bits atomically
2325 + * @child: device pointer from the calling child
2326 + * @reg: the register to write to
2327 + * @bits: the bits to set
2329 + * This function sets and clears the specified Tahvo register bits atomically
2331 +void tahvo_set_clear_reg_bits(struct device *child, unsigned reg, u16 set,
2334 + struct tahvo *tahvo = dev_get_drvdata(child->parent);
2337 + mutex_lock(&tahvo->mutex);
2338 + w = __tahvo_read_reg(tahvo, reg);
2341 + __tahvo_write_reg(tahvo, reg, w);
2342 + mutex_unlock(&tahvo->mutex);
2345 +static irqreturn_t tahvo_irq_handler(int irq, void *_tahvo)
2347 + struct tahvo *tahvo = _tahvo;
2351 + id = __tahvo_read_reg(tahvo, TAHVO_REG_IDR);
2352 + im = __tahvo_read_reg(tahvo, TAHVO_REG_IMR);
2356 + dev_vdbg(tahvo->dev, "No IRQ, spurious ?\n");
2361 + unsigned long pending = __ffs(id);
2364 + id &= ~BIT(pending);
2365 + irq = pending + tahvo->irq_base;
2366 + handle_nested_irq(irq);
2369 + return IRQ_HANDLED;
2372 +/* -------------------------------------------------------------------------- */
2374 +static void tahvo_irq_bus_lock(struct irq_data *data)
2376 + struct tahvo *tahvo = irq_data_get_irq_chip_data(data);
2378 + mutex_lock(&tahvo->mutex);
2381 +static void tahvo_irq_bus_sync_unlock(struct irq_data *data)
2383 + struct tahvo *tahvo = irq_data_get_irq_chip_data(data);
2385 + if (tahvo->mask_pending) {
2386 + __tahvo_write_reg(tahvo, TAHVO_REG_IMR, tahvo->mask);
2387 + tahvo->mask_pending = false;
2390 + if (tahvo->ack_pending) {
2391 + __tahvo_write_reg(tahvo, TAHVO_REG_IDR, tahvo->ack);
2392 + tahvo->ack_pending = false;
2395 + mutex_unlock(&tahvo->mutex);
2398 +static void tahvo_irq_mask(struct irq_data *data)
2400 + struct tahvo *tahvo = irq_data_get_irq_chip_data(data);
2401 + int irq = data->irq;
2403 + tahvo->mask |= (1 << (irq - tahvo->irq_base));
2404 + tahvo->mask_pending = true;
2407 +static void tahvo_irq_unmask(struct irq_data *data)
2409 + struct tahvo *tahvo = irq_data_get_irq_chip_data(data);
2410 + int irq = data->irq;
2412 + tahvo->mask &= ~(1 << (irq - tahvo->irq_base));
2413 + tahvo->mask_pending = true;
2416 +static void tahvo_irq_ack(struct irq_data *data)
2418 + struct tahvo *tahvo = irq_data_get_irq_chip_data(data);
2419 + int irq = data->irq;
2421 + tahvo->ack |= (1 << (irq - tahvo->irq_base));
2422 + tahvo->ack_pending = true;
2425 +static struct irq_chip tahvo_irq_chip = {
2427 + .irq_bus_lock = tahvo_irq_bus_lock,
2428 + .irq_bus_sync_unlock = tahvo_irq_bus_sync_unlock,
2429 + .irq_mask = tahvo_irq_mask,
2430 + .irq_unmask = tahvo_irq_unmask,
2431 + .irq_ack = tahvo_irq_ack,
2434 +static inline void tahvo_irq_setup(int irq)
2437 + set_irq_flags(irq, IRQF_VALID);
2439 + irq_set_noprobe(irq);
2443 +static void tahvo_irq_init(struct tahvo *tahvo)
2445 + int base = tahvo->irq_base;
2446 + int end = tahvo->irq_end;
2449 + for (irq = base; irq < end; irq++) {
2450 + irq_set_chip_data(irq, tahvo);
2451 + irq_set_chip_and_handler(irq, &tahvo_irq_chip,
2452 + handle_simple_irq);
2453 + irq_set_nested_thread(irq, 1);
2454 + tahvo_irq_setup(irq);
2458 +/* -------------------------------------------------------------------------- */
2460 +static struct resource generic_resources[] = {
2462 + .start = -EINVAL, /* fixed later */
2463 + .flags = IORESOURCE_IRQ,
2467 +static struct device *tahvo_allocate_child(const char *name,
2468 + struct device *parent, int irq)
2470 + struct platform_device *pdev;
2473 + pdev = platform_device_alloc(name, -1);
2475 + dev_dbg(parent, "can't allocate %s\n", name);
2479 + pdev->dev.parent = parent;
2482 + generic_resources[0].start = irq;
2484 + ret = platform_device_add_resources(pdev, generic_resources,
2485 + ARRAY_SIZE(generic_resources));
2487 + dev_dbg(parent, "can't add resources to %s\n", name);
2492 + ret = platform_device_add(pdev);
2494 + dev_dbg(parent, "can't add %s\n", name);
2498 + return &pdev->dev;
2501 + platform_device_put(pdev);
2507 +static int tahvo_allocate_children(struct device *parent, int irq_base)
2509 + struct device *child;
2511 + child = tahvo_allocate_child("tahvo-usb", parent,
2512 + irq_base + TAHVO_INT_VBUSON);
2516 + child = tahvo_allocate_child("tahvo-pwm", parent, -1);
2523 +static int __devinit tahvo_probe(struct platform_device *pdev)
2525 + struct tahvo *tahvo;
2531 + tahvo = kzalloc(sizeof(*tahvo), GFP_KERNEL);
2533 + dev_err(&pdev->dev, "not enough memory\n");
2538 + irq = platform_get_irq(pdev, 0);
2539 + platform_set_drvdata(pdev, tahvo);
2541 + mutex_init(&tahvo->mutex);
2543 + ret = irq_alloc_descs(-1, 0, MAX_TAHVO_IRQ_HANDLERS, 0);
2545 + dev_err(&pdev->dev, "failed to allocate IRQ descs\n");
2549 + tahvo->irq_base = ret;
2550 + tahvo->irq_end = ret + MAX_TAHVO_IRQ_HANDLERS;
2551 + tahvo->dev = &pdev->dev;
2554 + tahvo_irq_init(tahvo);
2556 + rev = __tahvo_read_reg(tahvo, TAHVO_REG_ASICR);
2558 + id = (rev >> 8) & 0xff;
2561 + tahvo->is_betty = true;
2563 + ret = tahvo_allocate_children(&pdev->dev, tahvo->irq_base);
2565 + dev_err(&pdev->dev, "failed to allocate children\n");
2569 + dev_err(&pdev->dev, "%s v%d.%d found\n",
2570 + tahvo->is_betty ? "Betty" : "Tahvo",
2571 + (rev >> 4) & 0x0f, rev & 0x0f);
2573 + /* Mask all TAHVO interrupts */
2574 + __tahvo_write_reg(tahvo, TAHVO_REG_IMR, 0xffff);
2576 + ret = request_threaded_irq(irq, NULL, tahvo_irq_handler,
2577 + IRQF_TRIGGER_RISING | IRQF_ONESHOT,
2580 + dev_err(&pdev->dev, "Unable to register IRQ handler\n");
2587 + irq_free_descs(tahvo->irq_base, MAX_TAHVO_IRQ_HANDLERS);
2596 +static int __devexit tahvo_remove(struct platform_device *pdev)
2598 + struct tahvo *tahvo = platform_get_drvdata(pdev);
2601 + irq = platform_get_irq(pdev, 0);
2604 + irq_free_descs(tahvo->irq_base, MAX_TAHVO_IRQ_HANDLERS);
2610 +static struct platform_driver tahvo_driver = {
2611 + .probe = tahvo_probe,
2612 + .remove = __devexit_p(tahvo_remove),
2618 +static int __init tahvo_init(void)
2620 + return platform_driver_register(&tahvo_driver);
2622 +subsys_initcall(tahvo_init);
2624 +static void __exit tahvo_exit(void)
2626 + platform_driver_unregister(&tahvo_driver);
2628 +module_exit(tahvo_exit);
2630 +MODULE_DESCRIPTION("Tahvo ASIC control");
2631 +MODULE_LICENSE("GPL");
2632 +MODULE_AUTHOR("Juha Yrjölä");
2633 +MODULE_AUTHOR("David Weinehall");
2634 +MODULE_AUTHOR("Mikko Ylinen");
2637 +++ b/drivers/cbus/tahvo.h
2640 + * drivers/cbus/tahvo.h
2642 + * Copyright (C) 2004, 2005 Nokia Corporation
2644 + * Written by Juha Yrjölä <juha.yrjola@nokia.com> and
2645 + * David Weinehall <david.weinehall@nokia.com>
2647 + * This file is subject to the terms and conditions of the GNU General
2648 + * Public License. See the file "COPYING" in the main directory of this
2649 + * archive for more details.
2651 + * This program is distributed in the hope that it will be useful,
2652 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2653 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2654 + * GNU General Public License for more details.
2656 + * You should have received a copy of the GNU General Public License
2657 + * along with this program; if not, write to the Free Software
2658 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2661 +#ifndef __DRIVERS_CBUS_TAHVO_H
2662 +#define __DRIVERS_CBUS_TAHVO_H
2664 +#include <linux/types.h>
2667 +#define TAHVO_REG_ASICR 0x00 /* ASIC ID & revision */
2668 +#define TAHVO_REG_IDR 0x01 /* Interrupt ID */
2669 +#define TAHVO_REG_IDSR 0x02 /* Interrupt status */
2670 +#define TAHVO_REG_IMR 0x03 /* Interrupt mask */
2671 +#define TAHVO_REG_CHGCURR 0x04 /* Charge current control PWM (8-bit) */
2672 +#define TAHVO_REG_LEDPWMR 0x05 /* LED PWM */
2673 +#define TAHVO_REG_USBR 0x06 /* USB control */
2674 +#define TAHVO_REG_CHGCTL 0x08 /* Charge control register */
2675 +#define TAHVO_REG_CHGCTL_EN 0x0001 /* Global charge enable */
2676 +#define TAHVO_REG_CHGCTL_PWMOVR 0x0004 /* PWM override. Force charge PWM to 0%/100% duty cycle. */
2677 +#define TAHVO_REG_CHGCTL_PWMOVRZERO 0x0008 /* If set, PWM override is 0% (If unset -> 100%) */
2678 +#define TAHVO_REG_CHGCTL_CURMEAS 0x0040 /* Enable battery current measurement. */
2679 +#define TAHVO_REG_CHGCTL_CURTIMRST 0x0080 /* Current measure timer reset. */
2680 +#define TAHVO_REG_BATCURRTIMER 0x0c /* Battery current measure timer (8-bit) */
2681 +#define TAHVO_REG_BATCURR 0x0d /* Battery (dis)charge current (signed 16-bit) */
2683 +#define TAHVO_REG_MAX 0x0d
2685 +/* Interrupt sources */
2686 +#define TAHVO_INT_VBUSON 0
2687 +#define TAHVO_INT_BATCURR 7 /* Battery current measure timer */
2689 +#define MAX_TAHVO_IRQ_HANDLERS 8
2691 +int tahvo_read_reg(struct device *child, unsigned reg);
2692 +void tahvo_write_reg(struct device *child, unsigned reg, u16 val);
2693 +void tahvo_set_clear_reg_bits(struct device *child, unsigned reg, u16 set,
2696 +#endif /* __DRIVERS_CBUS_TAHVO_H */
2698 +++ b/drivers/cbus/tahvo-usb.c
2701 + * drivers/cbus/tahvo-usb.c
2703 + * Tahvo USB transeiver
2705 + * Copyright (C) 2005-2006 Nokia Corporation
2707 + * Parts copied from drivers/i2c/chips/isp1301_omap.c
2708 + * Copyright (C) 2004 Texas Instruments
2709 + * Copyright (C) 2004 David Brownell
2711 + * Written by Juha Yrjölä <juha.yrjola@nokia.com>,
2712 + * Tony Lindgren <tony@atomide.com>, and
2713 + * Timo Teräs <timo.teras@nokia.com>
2715 + * This file is subject to the terms and conditions of the GNU General
2716 + * Public License. See the file "COPYING" in the main directory of this
2717 + * archive for more details.
2719 + * This program is distributed in the hope that it will be useful,
2720 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2721 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2722 + * GNU General Public License for more details.
2724 + * You should have received a copy of the GNU General Public License
2725 + * along with this program; if not, write to the Free Software
2726 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2729 +#include <linux/kernel.h>
2730 +#include <linux/module.h>
2731 +#include <linux/init.h>
2732 +#include <linux/slab.h>
2733 +#include <linux/io.h>
2734 +#include <linux/interrupt.h>
2735 +#include <linux/platform_device.h>
2736 +#include <linux/usb/ch9.h>
2737 +#include <linux/usb/gadget.h>
2738 +#include <linux/usb.h>
2739 +#include <linux/usb/otg.h>
2740 +#include <linux/i2c.h>
2741 +#include <linux/workqueue.h>
2742 +#include <linux/kobject.h>
2743 +#include <linux/clk.h>
2744 +#include <linux/mutex.h>
2746 +#include <asm/irq.h>
2747 +#include <plat/usb.h>
2752 +#define DRIVER_NAME "tahvo-usb"
2754 +#define USBR_SLAVE_CONTROL (1 << 8)
2755 +#define USBR_VPPVIO_SW (1 << 7)
2756 +#define USBR_SPEED (1 << 6)
2757 +#define USBR_REGOUT (1 << 5)
2758 +#define USBR_MASTER_SW2 (1 << 4)
2759 +#define USBR_MASTER_SW1 (1 << 3)
2760 +#define USBR_SLAVE_SW (1 << 2)
2761 +#define USBR_NSUSPEND (1 << 1)
2762 +#define USBR_SEMODE (1 << 0)
2764 +/* bits in OTG_CTRL */
2766 +/* Bits that are controlled by OMAP OTG and are read-only */
2767 +#define OTG_CTRL_OMAP_MASK (OTG_PULLDOWN|OTG_PULLUP|OTG_DRV_VBUS|\
2768 + OTG_PD_VBUS|OTG_PU_VBUS|OTG_PU_ID)
2769 +/* Bits that are controlled by transceiver */
2770 +#define OTG_CTRL_XCVR_MASK (OTG_ASESSVLD|OTG_BSESSEND|\
2771 + OTG_BSESSVLD|OTG_VBUSVLD|OTG_ID)
2772 +/* Bits that are controlled by system */
2773 +#define OTG_CTRL_SYS_MASK (OTG_A_BUSREQ|OTG_A_SETB_HNPEN|OTG_B_BUSREQ|\
2774 + OTG_B_HNPEN|OTG_BUSDROP)
2776 +#if defined(CONFIG_USB_OHCI_HCD) && !defined(CONFIG_USB_OTG)
2777 +#error tahvo-otg.c does not work with OCHI yet!
2780 +#define TAHVO_MODE_HOST 0
2781 +#define TAHVO_MODE_PERIPHERAL 1
2783 +#ifdef CONFIG_USB_OTG
2784 +#define TAHVO_MODE(tu) (tu)->tahvo_mode
2785 +#elif defined(CONFIG_USB_GADGET_OMAP)
2786 +#define TAHVO_MODE(tu) TAHVO_MODE_PERIPHERAL
2788 +#define TAHVO_MODE(tu) TAHVO_MODE_HOST
2792 + struct device *dev;
2793 + struct platform_device *pt_dev;
2794 + struct otg_transceiver otg;
2796 + struct mutex serialize;
2797 +#ifdef CONFIG_USB_OTG
2804 +static struct tahvo_usb *tahvo_usb_device;
2807 + * ---------------------------------------------------------------------------
2808 + * OTG related functions
2810 + * These shoud be separated into omap-otg.c driver module, as they are used
2811 + * by various transceivers. These functions are needed in the UDC-only case
2812 + * as well. These functions are copied from GPL isp1301_omap.c
2813 + * ---------------------------------------------------------------------------
2815 +static struct platform_device *tahvo_otg_dev;
2817 +static irqreturn_t omap_otg_irq(int irq, void *arg)
2821 + otg_irq = omap_readw(OTG_IRQ_SRC);
2822 + if (otg_irq & OPRT_CHG) {
2823 + omap_writew(OPRT_CHG, OTG_IRQ_SRC);
2824 + } else if (otg_irq & B_SRP_TMROUT) {
2825 + omap_writew(B_SRP_TMROUT, OTG_IRQ_SRC);
2826 + } else if (otg_irq & B_HNP_FAIL) {
2827 + omap_writew(B_HNP_FAIL, OTG_IRQ_SRC);
2828 + } else if (otg_irq & A_SRP_DETECT) {
2829 + omap_writew(A_SRP_DETECT, OTG_IRQ_SRC);
2830 + } else if (otg_irq & A_REQ_TMROUT) {
2831 + omap_writew(A_REQ_TMROUT, OTG_IRQ_SRC);
2832 + } else if (otg_irq & A_VBUS_ERR) {
2833 + omap_writew(A_VBUS_ERR, OTG_IRQ_SRC);
2834 + } else if (otg_irq & DRIVER_SWITCH) {
2835 +#ifdef CONFIG_USB_OTG
2836 + if ((!(omap_readl(OTG_CTRL) & OTG_DRIVER_SEL)) &&
2837 + tu->otg.host && tu->otg.state == OTG_STATE_A_HOST) {
2838 + /* role is host */
2839 + usb_bus_start_enum(tu->otg.host,
2840 + tu->otg.host->otg_port);
2843 + omap_writew(DRIVER_SWITCH, OTG_IRQ_SRC);
2847 + return IRQ_HANDLED;
2851 +static int tahvo_otg_init(void)
2855 +#ifdef CONFIG_USB_OTG
2856 + if (!tahvo_otg_dev) {
2857 + printk("tahvo-usb: no tahvo_otg_dev\n");
2862 + l = omap_readl(OTG_SYSCON_1);
2863 + l &= ~OTG_IDLE_EN;
2864 + omap_writel(l, OTG_SYSCON_1);
2867 + /* some of these values are board-specific... */
2868 + l = omap_readl(OTG_SYSCON_2);
2870 + /* for B-device: */
2871 + | SRP_GPDATA /* 9msec Bdev D+ pulse */
2872 + | SRP_GPDVBUS /* discharge after VBUS pulse */
2873 + // | (3 << 24) /* 2msec VBUS pulse */
2874 + /* for A-device: */
2875 + | (0 << 20) /* 200ms nominal A_WAIT_VRISE timer */
2876 + | SRP_DPW /* detect 167+ns SRP pulses */
2877 + | SRP_DATA | SRP_VBUS; /* accept both kinds of SRP pulse */
2878 + omap_writel(l, OTG_SYSCON_2);
2880 + omap_writew(DRIVER_SWITCH | OPRT_CHG
2881 + | B_SRP_TMROUT | B_HNP_FAIL
2882 + | A_VBUS_ERR | A_SRP_DETECT | A_REQ_TMROUT,
2884 + l = omap_readl(OTG_SYSCON_2);
2886 + omap_writel(l, OTG_SYSCON_2);
2891 +static int __init omap_otg_probe(struct platform_device *pdev)
2895 + tahvo_otg_dev = pdev;
2896 + ret = tahvo_otg_init();
2898 + printk(KERN_ERR "tahvo-usb: tahvo_otg_init failed\n");
2902 + return request_irq(tahvo_otg_dev->resource[1].start,
2903 + omap_otg_irq, IRQF_DISABLED, DRIVER_NAME,
2904 + tahvo_usb_device);
2907 +static int __exit omap_otg_remove(struct platform_device *pdev)
2909 + free_irq(tahvo_otg_dev->resource[1].start, tahvo_usb_device);
2910 + tahvo_otg_dev = NULL;
2915 +struct platform_driver omap_otg_driver = {
2917 + .name = "omap_otg",
2919 + .remove = __exit_p(omap_otg_remove),
2923 + * ---------------------------------------------------------------------------
2924 + * Tahvo related functions
2925 + * These are Nokia proprietary code, except for the OTG register settings,
2926 + * which are copied from isp1301.c
2927 + * ---------------------------------------------------------------------------
2929 +static ssize_t vbus_state_show(struct device *device,
2930 + struct device_attribute *attr, char *buf)
2932 + struct tahvo_usb *tu = dev_get_drvdata(device);
2933 + return sprintf(buf, "%d\n", tu->vbus_state);
2935 +static DEVICE_ATTR(vbus_state, 0444, vbus_state_show, NULL);
2937 +int vbus_active = 0;
2939 +static void check_vbus_state(struct tahvo_usb *tu)
2941 + int reg, prev_state;
2943 + reg = tahvo_read_reg(tu->dev, TAHVO_REG_IDSR);
2948 + switch (tu->otg.state) {
2949 + case OTG_STATE_B_IDLE:
2950 + /* Enable the gadget driver */
2951 + if (tu->otg.gadget)
2952 + usb_gadget_vbus_connect(tu->otg.gadget);
2953 + /* Set B-session valid and not B-sessio ended to indicate
2954 + * Vbus to be ok. */
2955 + l = omap_readl(OTG_CTRL);
2956 + l &= ~OTG_BSESSEND;
2957 + l |= OTG_BSESSVLD;
2958 + omap_writel(l, OTG_CTRL);
2960 + tu->otg.state = OTG_STATE_B_PERIPHERAL;
2962 + case OTG_STATE_A_IDLE:
2963 + /* Session is now valid assuming the USB hub is driving Vbus */
2964 + tu->otg.state = OTG_STATE_A_HOST;
2969 + printk("USB cable connected\n");
2971 + switch (tu->otg.state) {
2972 + case OTG_STATE_B_PERIPHERAL:
2973 + if (tu->otg.gadget)
2974 + usb_gadget_vbus_disconnect(tu->otg.gadget);
2975 + tu->otg.state = OTG_STATE_B_IDLE;
2977 + case OTG_STATE_A_HOST:
2978 + tu->otg.state = OTG_STATE_A_IDLE;
2983 + printk("USB cable disconnected\n");
2987 + prev_state = tu->vbus_state;
2988 + tu->vbus_state = reg & 0x01;
2989 + if (prev_state != tu->vbus_state)
2990 + sysfs_notify(&tu->pt_dev->dev.kobj, NULL, "vbus_state");
2993 +static void tahvo_usb_become_host(struct tahvo_usb *tu)
2997 + /* Clear system and transceiver controlled bits
2998 + * also mark the A-session is always valid */
3001 + l = omap_readl(OTG_CTRL);
3002 + l &= ~(OTG_CTRL_XCVR_MASK | OTG_CTRL_SYS_MASK);
3003 + l |= OTG_ASESSVLD;
3004 + omap_writel(l, OTG_CTRL);
3006 + /* Power up the transceiver in USB host mode */
3007 + tahvo_write_reg(tu->dev, TAHVO_REG_USBR, USBR_REGOUT | USBR_NSUSPEND |
3008 + USBR_MASTER_SW2 | USBR_MASTER_SW1);
3009 + tu->otg.state = OTG_STATE_A_IDLE;
3011 + check_vbus_state(tu);
3014 +static void tahvo_usb_stop_host(struct tahvo_usb *tu)
3016 + tu->otg.state = OTG_STATE_A_IDLE;
3019 +static void tahvo_usb_become_peripheral(struct tahvo_usb *tu)
3023 + /* Clear system and transceiver controlled bits
3024 + * and enable ID to mark peripheral mode and
3025 + * BSESSEND to mark no Vbus */
3027 + l = omap_readl(OTG_CTRL);
3028 + l &= ~(OTG_CTRL_XCVR_MASK | OTG_CTRL_SYS_MASK | OTG_BSESSVLD);
3029 + l |= OTG_ID | OTG_BSESSEND;
3030 + omap_writel(l, OTG_CTRL);
3032 + /* Power up transceiver and set it in USB perhiperal mode */
3033 + tahvo_write_reg(tu->dev, TAHVO_REG_USBR, USBR_SLAVE_CONTROL | USBR_REGOUT | USBR_NSUSPEND | USBR_SLAVE_SW);
3034 + tu->otg.state = OTG_STATE_B_IDLE;
3036 + check_vbus_state(tu);
3039 +static void tahvo_usb_stop_peripheral(struct tahvo_usb *tu)
3043 + l = omap_readl(OTG_CTRL);
3044 + l &= ~OTG_BSESSVLD;
3045 + l |= OTG_BSESSEND;
3046 + omap_writel(l, OTG_CTRL);
3048 + if (tu->otg.gadget)
3049 + usb_gadget_vbus_disconnect(tu->otg.gadget);
3050 + tu->otg.state = OTG_STATE_B_IDLE;
3054 +static void tahvo_usb_power_off(struct tahvo_usb *tu)
3059 + /* Disable gadget controller if any */
3060 + if (tu->otg.gadget)
3061 + usb_gadget_vbus_disconnect(tu->otg.gadget);
3063 + /* Disable OTG and interrupts */
3064 + if (TAHVO_MODE(tu) == TAHVO_MODE_PERIPHERAL)
3068 + l = omap_readl(OTG_CTRL);
3069 + l &= ~(OTG_CTRL_XCVR_MASK | OTG_CTRL_SYS_MASK | OTG_BSESSVLD);
3070 + l |= id | OTG_BSESSEND;
3071 + omap_writel(l, OTG_CTRL);
3072 + omap_writew(0, OTG_IRQ_EN);
3074 + l = omap_readl(OTG_SYSCON_2);
3076 + omap_writel(l, OTG_SYSCON_2);
3078 + l = omap_readl(OTG_SYSCON_1);
3080 + omap_writel(l, OTG_SYSCON_1);
3082 + /* Power off transceiver */
3083 + tahvo_write_reg(tu->dev, TAHVO_REG_USBR, 0);
3084 + tu->otg.state = OTG_STATE_UNDEFINED;
3088 +static int tahvo_usb_set_power(struct otg_transceiver *dev, unsigned mA)
3090 + struct tahvo_usb *tu = container_of(dev, struct tahvo_usb, otg);
3092 + dev_dbg(&tu->pt_dev->dev, "set_power %d mA\n", mA);
3094 + if (dev->state == OTG_STATE_B_PERIPHERAL) {
3095 + /* REVISIT: Can Tahvo charge battery from VBUS? */
3100 +static int tahvo_usb_set_suspend(struct otg_transceiver *dev, int suspend)
3102 + struct tahvo_usb *tu = container_of(dev, struct tahvo_usb, otg);
3105 + dev_dbg(&tu->pt_dev->dev, "set_suspend\n");
3107 + w = tahvo_read_reg(tu->dev, TAHVO_REG_USBR);
3109 + w &= ~USBR_NSUSPEND;
3111 + w |= USBR_NSUSPEND;
3112 + tahvo_write_reg(tu->dev, TAHVO_REG_USBR, w);
3117 +static int tahvo_usb_start_srp(struct otg_transceiver *dev)
3119 + struct tahvo_usb *tu = container_of(dev, struct tahvo_usb, otg);
3122 + dev_dbg(&tu->pt_dev->dev, "start_srp\n");
3124 + if (!dev || tu->otg.state != OTG_STATE_B_IDLE)
3127 + otg_ctrl = omap_readl(OTG_CTRL);
3128 + if (!(otg_ctrl & OTG_BSESSEND))
3131 + otg_ctrl |= OTG_B_BUSREQ;
3132 + otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_SYS_MASK;
3133 + omap_writel(otg_ctrl, OTG_CTRL);
3134 + tu->otg.state = OTG_STATE_B_SRP_INIT;
3139 +static int tahvo_usb_start_hnp(struct otg_transceiver *otg)
3141 + struct tahvo_usb *tu = container_of(otg, struct tahvo_usb, otg);
3143 + dev_dbg(&tu->pt_dev->dev, "start_hnp\n");
3144 +#ifdef CONFIG_USB_OTG
3145 + /* REVISIT: Add this for OTG */
3150 +static int tahvo_usb_set_host(struct otg_transceiver *otg, struct usb_bus *host)
3152 + struct tahvo_usb *tu = container_of(otg, struct tahvo_usb, otg);
3155 + dev_dbg(&tu->pt_dev->dev, "set_host %p\n", host);
3160 +#if defined(CONFIG_USB_OTG) || !defined(CONFIG_USB_GADGET_OMAP)
3162 + mutex_lock(&tu->serialize);
3164 + if (host == NULL) {
3165 + if (TAHVO_MODE(tu) == TAHVO_MODE_HOST)
3166 + tahvo_usb_power_off(tu);
3167 + tu->otg.host = NULL;
3168 + mutex_unlock(&tu->serialize);
3172 + l = omap_readl(OTG_SYSCON_1);
3173 + l &= ~(OTG_IDLE_EN | HST_IDLE_EN | DEV_IDLE_EN);
3174 + omap_writel(l, OTG_SYSCON_1);
3176 + if (TAHVO_MODE(tu) == TAHVO_MODE_HOST) {
3177 + tu->otg.host = NULL;
3178 + tahvo_usb_become_host(tu);
3181 + tu->otg.host = host;
3183 + mutex_unlock(&tu->serialize);
3185 + /* No host mode configured, so do not allow host controlled to be set */
3192 +static int tahvo_usb_set_peripheral(struct otg_transceiver *otg, struct usb_gadget *gadget)
3194 + struct tahvo_usb *tu = container_of(otg, struct tahvo_usb, otg);
3196 + dev_dbg(&tu->pt_dev->dev, "set_peripheral %p\n", gadget);
3201 +#if defined(CONFIG_USB_OTG) || defined(CONFIG_USB_GADGET_OMAP)
3203 + mutex_lock(&tu->serialize);
3206 + if (TAHVO_MODE(tu) == TAHVO_MODE_PERIPHERAL)
3207 + tahvo_usb_power_off(tu);
3208 + tu->otg.gadget = NULL;
3209 + mutex_unlock(&tu->serialize);
3213 + tu->otg.gadget = gadget;
3214 + if (TAHVO_MODE(tu) == TAHVO_MODE_PERIPHERAL)
3215 + tahvo_usb_become_peripheral(tu);
3217 + mutex_unlock(&tu->serialize);
3219 + /* No gadget mode configured, so do not allow host controlled to be set */
3226 +static irqreturn_t tahvo_usb_vbus_interrupt(int irq, void *_tu)
3228 + struct tahvo_usb *tu = _tu;
3230 + check_vbus_state(tu);
3232 + return IRQ_HANDLED;
3235 +#ifdef CONFIG_USB_OTG
3236 +static ssize_t otg_mode_show(struct device *device,
3237 + struct device_attribute *attr, char *buf)
3239 + struct tahvo_usb *tu = dev_get_drvdata(device);
3240 + switch (tu->tahvo_mode) {
3241 + case TAHVO_MODE_HOST:
3242 + return sprintf(buf, "host\n");
3243 + case TAHVO_MODE_PERIPHERAL:
3244 + return sprintf(buf, "peripheral\n");
3246 + return sprintf(buf, "unknown\n");
3249 +static ssize_t otg_mode_store(struct device *device,
3250 + struct device_attribute *attr,
3251 + const char *buf, size_t count)
3253 + struct tahvo_usb *tu = dev_get_drvdata(device);
3257 + mutex_lock(&tu->serialize);
3258 + if (strncmp(buf, "host", 4) == 0) {
3259 + if (tu->tahvo_mode == TAHVO_MODE_PERIPHERAL)
3260 + tahvo_usb_stop_peripheral(tu);
3261 + tu->tahvo_mode = TAHVO_MODE_HOST;
3262 + if (tu->otg.host) {
3263 + printk(KERN_INFO "Selected HOST mode: host controller present.\n");
3264 + tahvo_usb_become_host(tu);
3266 + printk(KERN_INFO "Selected HOST mode: no host controller, powering off.\n");
3267 + tahvo_usb_power_off(tu);
3269 + } else if (strncmp(buf, "peripheral", 10) == 0) {
3270 + if (tu->tahvo_mode == TAHVO_MODE_HOST)
3271 + tahvo_usb_stop_host(tu);
3272 + tu->tahvo_mode = TAHVO_MODE_PERIPHERAL;
3273 + if (tu->otg.gadget) {
3274 + printk(KERN_INFO "Selected PERIPHERAL mode: gadget driver present.\n");
3275 + tahvo_usb_become_peripheral(tu);
3277 + printk(KERN_INFO "Selected PERIPHERAL mode: no gadget driver, powering off.\n");
3278 + tahvo_usb_power_off(tu);
3283 + mutex_unlock(&tu->serialize);
3287 +static DEVICE_ATTR(otg_mode, 0644, otg_mode_show, otg_mode_store);
3290 +static int __init tahvo_usb_probe(struct platform_device *pdev)
3292 + struct tahvo_usb *tu;
3293 + struct device *dev = &pdev->dev;
3297 + dev_dbg(dev, "probe\n");
3299 + /* Create driver data */
3300 + tu = kzalloc(sizeof(*tu), GFP_KERNEL);
3303 + tahvo_usb_device = tu;
3306 + tu->pt_dev = pdev;
3307 +#ifdef CONFIG_USB_OTG
3308 + /* Default mode */
3309 +#ifdef CONFIG_CBUS_TAHVO_USB_HOST_BY_DEFAULT
3310 + tu->tahvo_mode = TAHVO_MODE_HOST;
3312 + tu->tahvo_mode = TAHVO_MODE_PERIPHERAL;
3316 + mutex_init(&tu->serialize);
3318 + tu->ick = clk_get(NULL, "usb_l4_ick");
3319 + if (IS_ERR(tu->ick)) {
3320 + dev_err(dev, "Failed to get usb_l4_ick\n");
3321 + ret = PTR_ERR(tu->ick);
3324 + clk_enable(tu->ick);
3326 + /* Set initial state, so that we generate kevents only on
3327 + * state changes */
3328 + tu->vbus_state = tahvo_read_reg(tu->dev, TAHVO_REG_IDSR) & 0x01;
3330 + irq = platform_get_irq(pdev, 0);
3333 + /* We cannot enable interrupt until omap_udc is initialized */
3334 + ret = request_threaded_irq(irq, NULL, tahvo_usb_vbus_interrupt,
3335 + IRQF_ONESHOT, "tahvo-vbus", tu);
3337 + printk(KERN_ERR "Could not register Tahvo interrupt for VBUS\n");
3338 + goto err_release_clk;
3342 + ret = device_create_file(dev, &dev_attr_vbus_state);
3343 +#ifdef CONFIG_USB_OTG
3344 + ret |= device_create_file(dev, &dev_attr_otg_mode);
3347 + printk(KERN_ERR "attribute creation failed: %d\n", ret);
3349 + /* Create OTG interface */
3350 + tahvo_usb_power_off(tu);
3351 + tu->otg.state = OTG_STATE_UNDEFINED;
3352 + tu->otg.label = DRIVER_NAME;
3353 + tu->otg.set_host = tahvo_usb_set_host;
3354 + tu->otg.set_peripheral = tahvo_usb_set_peripheral;
3355 + tu->otg.set_power = tahvo_usb_set_power;
3356 + tu->otg.set_suspend = tahvo_usb_set_suspend;
3357 + tu->otg.start_srp = tahvo_usb_start_srp;
3358 + tu->otg.start_hnp = tahvo_usb_start_hnp;
3360 + ret = otg_set_transceiver(&tu->otg);
3362 + printk(KERN_ERR "Cannot register USB transceiver\n");
3363 + goto err_free_irq;
3366 + dev_set_drvdata(dev, tu);
3371 + free_irq(tu->irq, tu);
3373 + clk_disable(tu->ick);
3377 + tahvo_usb_device = NULL;
3382 +static int __exit tahvo_usb_remove(struct platform_device *pdev)
3384 + struct tahvo_usb *tu = platform_get_drvdata(pdev);
3386 + dev_dbg(&pdev->dev, "remove\n");
3388 + free_irq(tu->irq, tu);
3389 + flush_scheduled_work();
3390 + otg_set_transceiver(0);
3391 + device_remove_file(&pdev->dev, &dev_attr_vbus_state);
3392 +#ifdef CONFIG_USB_OTG
3393 + device_remove_file(&pdev->dev, &dev_attr_otg_mode);
3395 + clk_disable(tu->ick);
3399 + tahvo_usb_device = NULL;
3404 +static struct platform_driver tahvo_usb_driver = {
3406 + .name = "tahvo-usb",
3408 + .remove = __exit_p(tahvo_usb_remove),
3411 +static int __init tahvo_usb_init(void)
3415 + ret = platform_driver_probe(&tahvo_usb_driver, tahvo_usb_probe);
3419 + ret = platform_driver_probe(&omap_otg_driver, omap_otg_probe);
3421 + platform_driver_unregister(&tahvo_usb_driver);
3428 +subsys_initcall(tahvo_usb_init);
3430 +static void __exit tahvo_usb_exit(void)
3432 + platform_driver_unregister(&omap_otg_driver);
3433 + platform_driver_unregister(&tahvo_usb_driver);
3435 +module_exit(tahvo_usb_exit);
3437 +MODULE_DESCRIPTION("Tahvo USB OTG Transceiver Driver");
3438 +MODULE_LICENSE("GPL");
3439 +MODULE_AUTHOR("Juha Yrjölä, Tony Lindgren, and Timo Teräs");
3440 --- a/drivers/Makefile
3441 +++ b/drivers/Makefile
3442 @@ -76,7 +76,7 @@ obj-$(CONFIG_GAMEPORT) += input/gamepor
3443 obj-$(CONFIG_INPUT) += input/
3444 obj-$(CONFIG_I2O) += message/
3445 obj-$(CONFIG_RTC_LIB) += rtc/
3446 -obj-y += i2c/ media/
3447 +obj-y += i2c/ media/ cbus/
3448 obj-$(CONFIG_PPS) += pps/
3449 obj-$(CONFIG_PTP_1588_CLOCK) += ptp/
3450 obj-$(CONFIG_W1) += w1/
3451 --- a/drivers/Kconfig
3452 +++ b/drivers/Kconfig
3453 @@ -2,6 +2,8 @@ menu "Device Drivers"
3455 source "drivers/base/Kconfig"
3457 +source "drivers/cbus/Kconfig"
3459 source "drivers/connector/Kconfig"
3461 source "drivers/mtd/Kconfig"