]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/net/irda/omap1610-ir.c
[PATCH] ARM: OMAP: camera: c99 style structure
[linux-2.6-omap-h63xx.git] / drivers / net / irda / omap1610-ir.c
1 /*
2  * BRIEF MODULE DESCRIPTION
3  *
4  *      Infra-red driver for the OMAP1610-H2 and OMAP1710-H3 Platforms
5  *          (SIR/MIR/FIR modes)
6  *          (based on omap-sir.c)
7  *
8  * Copyright 2003 MontaVista Software Inc.
9  * Author: MontaVista Software, Inc.
10  *         source@mvista.com
11  * 
12  * Copyright 2004 Texas Instruments.
13  *
14  *  This program is free software; you can redistribute  it and/or modify it
15  *  under  the terms of  the GNU General  Public License as published by the
16  *  Free Software Foundation;  either version 2 of the  License, or (at your
17  *  option) any later version.
18  *
19  *  THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
20  *  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
21  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
22  *  NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
23  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  *  NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
25  *  USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  *  ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
27  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  *  You should have received a copy of the  GNU General Public License along
31  *  with this program; if not, write  to the Free Software Foundation, Inc.,
32  *  675 Mass Ave, Cambridge, MA 02139, USA.
33  *
34  Modifications:
35  Feb 2004, Texas Instruments
36  - Ported to 2.6 kernel (Feb 2004).
37  *
38  Apr 2004, Texas Instruments
39  - Added support for H3 (Apr 2004). 
40  Nov 2004, Texas Instruments
41  - Added support for Power Management.
42  
43  */
44
45 #include <linux/config.h>
46 #include <linux/module.h>
47 #include <linux/types.h>
48 #include <linux/init.h>
49 #include <linux/errno.h>
50 #include <linux/netdevice.h>
51 #include <linux/slab.h>
52 #include <linux/rtnetlink.h>
53 #include <linux/interrupt.h>
54 #include <linux/delay.h>
55 #include <linux/ioport.h>
56 #include <linux/dma-mapping.h>
57 #include <linux/platform_device.h>
58
59 #include <net/irda/irda.h>
60 #include <net/irda/irmod.h>
61 #include <net/irda/wrapper.h>
62 #include <net/irda/irda_device.h>
63
64 #include <asm/irq.h>
65 #include <asm/io.h>
66 #include <asm/hardware.h>
67 #include <asm/serial.h>
68 #include <asm/mach-types.h>
69 #include <asm/dma.h>
70 #include <asm/arch/mux.h>
71 #include <asm/arch/gpio.h>
72 #include <linux/i2c.h>
73
74 #ifdef CONFIG_MACH_OMAP_H3
75 #include <asm/arch/gpioexpander.h>
76 #endif
77
78 #define SIR_MODE 0
79 #define MIR_MODE 1
80 #define FIR_MODE 2
81
82 #define OMAP1610_H2_FIRSEL_GPIO 17
83
84 static int rx_state = 0;        /* RX state for IOCTL */
85
86 struct omap1610_irda {
87         unsigned char open;
88         int speed;              /* Current IrDA speed */
89         int newspeed;
90
91         struct net_device_stats stats;
92         struct irlap_cb *irlap;
93         struct qos_info qos;
94
95         int rx_dma_channel;
96         int tx_dma_channel;
97
98         dma_addr_t rx_buf_dma_phys;     /* Physical adress of RX DMA buffer */
99         dma_addr_t tx_buf_dma_phys;     /* Physical adress of TX DMA buffer */
100
101         void *rx_buf_dma_virt;  /* Virtual adress of RX DMA buffer */
102         void *tx_buf_dma_virt;  /* Virtual adress of TX DMA buffer */
103
104         struct device *dev;
105 };
106
107 #define OMAP_IRDA_DEBUG 0
108
109 #if (OMAP_IRDA_DEBUG > 0)
110 #define DBG(format, args...) printk(KERN_ERR "%s(): " format, __FUNCTION__, ## args);
111 #define DBG_IRQ(format, args...) printk(KERN_ERR "%s(): " format, __FUNCTION__, ## args);
112 #else
113 #define DBG(format, args...)
114 #define DBG_IRQ(format, args...)
115 #endif
116
117 #if (OMAP_IRDA_DEBUG > 1)
118 #define __ECHO_IN printk(KERN_ERR "%s: enter\n",__FUNCTION__);
119 #define __ECHO_OUT printk(KERN_ERR "%s: exit\n",__FUNCTION__);
120 #else
121 #define __ECHO_IN
122 #define __ECHO_OUT
123 #endif
124
125 #ifdef OMAP1610_IR_HARDWARE_DEBUG_ENABLE
126 #define HDBG_DELAY 200
127
128 void hard_debug1(u16 i)
129 {
130         for (; i; i--) {
131                 omap_writew(0x2000,
132                             OMAP1610_GPIO1_BASE + OMAP1610_GPIO_CLEAR_DATAOUT);
133                 udelay(HDBG_DELAY);
134
135                 omap_writew(0x2000,
136                             OMAP1610_GPIO1_BASE + OMAP1610_GPIO_SET_DATAOUT);
137                 udelay(HDBG_DELAY);
138         }
139 }
140
141 void hard_debug2(u16 i)
142 {
143         for (; i; i--) {
144                 omap_writew(0x8000,
145                             OMAP1610_GPIO1_BASE + OMAP1610_GPIO_CLEAR_DATAOUT);
146                 udelay(HDBG_DELAY);
147
148                 omap_writew(0x8000,
149                             OMAP1610_GPIO1_BASE + OMAP1610_GPIO_SET_DATAOUT);
150                 udelay(HDBG_DELAY);
151         }
152 }
153
154 #define HDBG1(i) hard_debug1(i)
155 #define HDBG2(i) hard_debug2(i)
156 #else
157 #define HDBG1(i)
158 #define HDBG2(i)
159 #endif
160
161 /* forward declarations */
162
163 extern void irda_device_setup(struct net_device *dev);
164 extern void omap_stop_dma(int lch);
165 static int omap1610_irda_set_speed(struct net_device *dev, int speed);
166
167 static void omap1610_irda_start_rx_dma(struct omap1610_irda *si)
168 {
169         /* Configure DMA */
170         omap_set_dma_src_params(si->rx_dma_channel, 0x3, 0x0, (unsigned long)UART3_RHR,
171                                 0, 0);
172
173         omap_enable_dma_irq(si->rx_dma_channel, 0x01);
174
175         omap_set_dma_dest_params(si->rx_dma_channel, 0x0, 0x1,
176                                  si->rx_buf_dma_phys,
177                                  0, 0);
178
179         omap_set_dma_transfer_params(si->rx_dma_channel, 0x0, 4096, 0x1, 0x0, 0, 0);
180
181         omap_start_dma(si->rx_dma_channel);
182 }
183
184 static void omap1610_start_tx_dma(struct omap1610_irda *si, int size)
185 {
186         __ECHO_IN;
187
188         /* Configure DMA */
189         omap_set_dma_dest_params(si->tx_dma_channel, 0x03, 0x0, (unsigned long)UART3_THR,
190                                         0, 0);
191         omap_enable_dma_irq(si->tx_dma_channel, 0x01);
192
193         omap_set_dma_src_params(si->tx_dma_channel, 0x0, 0x1,
194                                 si->tx_buf_dma_phys,
195                                 0, 0);
196
197         omap_set_dma_transfer_params(si->tx_dma_channel, 0x0, size, 0x1, 0x0, 0, 0);
198
199         HDBG1(1);
200
201         /* Start DMA */
202         omap_start_dma(si->tx_dma_channel);
203
204         HDBG1(1);
205
206         __ECHO_OUT;
207 }
208
209 /* DMA RX callback - normally, we should not go here, 
210    it calls only if something is going wrong
211  */
212
213 static void omap1610_irda_rx_dma_callback(int lch, u16 ch_status, void *data)
214 {
215         struct net_device *dev = data;
216         struct omap1610_irda *si = dev->priv;
217
218         printk(KERN_ERR "RX Transfer error or very big frame \n");
219
220         /* Clear interrupts */
221         omap_readb(UART3_IIR);
222
223         si->stats.rx_frame_errors++;
224
225         omap_readb(UART3_RESUME);
226
227         /* Re-init RX DMA */
228         omap1610_irda_start_rx_dma(si);
229
230 }
231
232 /* DMA TX callback - calling when frame transfer has been finished */
233
234 static void omap1610_irda_tx_dma_callback(int lch, u16 ch_status, void *data)
235 {
236         struct net_device *dev = data;
237         struct omap1610_irda *si = dev->priv;
238
239         __ECHO_IN;
240
241         /*Stop DMA controller */
242         omap_stop_dma(si->tx_dma_channel);
243
244         __ECHO_OUT;
245
246 }
247
248 /*
249  * Set the IrDA communications speed.
250  * Interrupt have to be disabled here.
251  */
252
253 static int omap1610_irda_startup(struct net_device *dev)
254 {
255         __ECHO_IN;
256
257         /* Enable UART3 clock and set UART3 to IrDA mode */
258         omap_writel(omap_readl(MOD_CONF_CTRL_0) | (1 << 31) | (1 << 15),
259                     MOD_CONF_CTRL_0);
260
261         if (machine_is_omap_h2()) {
262 //              omap_cfg_reg(Y15_1610_GPIO17);
263                 omap_writel(omap_readl(FUNC_MUX_CTRL_A) | 7, FUNC_MUX_CTRL_A);
264
265                 omap_set_gpio_direction(OMAP1610_H2_FIRSEL_GPIO, 0);
266                 omap_set_gpio_dataout(OMAP1610_H2_FIRSEL_GPIO, 0);
267         }
268
269         omap_writeb(0x07, UART3_MDR1);  /* Put UART3 in reset mode */
270
271         /* Clear DLH and DLL */
272         omap_writeb(1 << 7, UART3_LCR);
273
274         omap_writeb(0, UART3_DLL);
275         omap_writeb(0, UART3_DLH);
276
277         omap_writeb(0xbf, UART3_LCR);
278
279         omap_writeb(1 << 4, UART3_EFR);
280
281         omap_writeb(1 << 7, UART3_LCR);
282
283         /* Enable access to UART3_TLR and UART3_TCR registers */
284         omap_writeb(1 << 6, UART3_MCR);
285
286         omap_writeb(0, UART3_SCR);
287
288         /* Set Rx trigger to 1 and Tx trigger to 1 */
289         omap_writeb(0, UART3_TLR);
290
291         /* Set LCR to 8 bits and 1 stop bit */
292         omap_writeb(0x03, UART3_LCR);
293
294         /* Clear RX and TX FIFO and enable FIFO */
295         /* Use DMA Req for transfers */
296
297         omap_writeb((1 << 2) | (1 << 1) | (1 << 3) | (1 << 4) | (1 << 6) | 1,
298                     UART3_FCR);
299
300         omap_writeb(0, UART3_MCR);
301
302         omap_writeb((1 << 7) | (1 << 6), UART3_SCR);
303
304         /* Enable UART3 SIR Mode,(Frame-length method to end frames) */
305         omap_writeb(1, UART3_MDR1);
306
307         /* Set Status FIFO trig to 1 */
308         omap_writeb(0, UART3_MDR2);
309
310         /* Enables RXIR input */
311         /* and disable TX underrun */
312         /* SEND_SIP pulse */
313
314         //   omap_writeb((1 << 7) | (1 << 6) | (1 << 4), UART3_ACREG);
315         omap_writeb((1 << 6) | (1 << 4), UART3_ACREG);
316
317         /* Enable EOF Interrupt only */
318         omap_writeb((1 << 7) | (1 << 5), UART3_IER);
319
320         /* Set Maximum Received Frame size to 2048 bytes */
321         omap_writeb(0x00, UART3_RXFLL);
322         omap_writeb(0x08, UART3_RXFLH);
323
324         omap_readb(UART3_RESUME);
325
326         __ECHO_OUT;
327
328         return 0;
329
330 }
331
332 static int omap1610_irda_shutdown(struct omap1610_irda *si)
333 {
334         /* Disable all UART3 Interrupts */
335         omap_writeb(0, UART3_IER);
336
337         /* Disable UART3 and disable baud rate generator */
338         omap_writeb(0x07, UART3_MDR1);  /* Put UART3 in reset mode */
339
340         omap_writeb((1 << 5), UART3_ACREG);     /* set SD_MODE pin to high and Disable RX IR */
341
342         /* Clear DLH and DLL */
343         omap_writeb(1 << 7, UART3_LCR);
344         omap_writeb(0, UART3_DLL);
345         omap_writeb(0, UART3_DLH);
346
347         return 0;
348 }
349
350 static irqreturn_t
351 omap1610_irda_irq(int irq, void *dev_id, struct pt_regs *hw_regs)
352 {
353         struct net_device *dev = dev_id;
354         struct omap1610_irda *si = dev->priv;
355         struct sk_buff *skb;
356
357         u8 status;
358         int w = 0;
359
360         __ECHO_IN;
361
362         /* Clear EOF interrupt */
363         status = omap_readb(UART3_IIR);
364
365         if (status & (1 << 5)) {
366                 u8 mdr2 = omap_readb(UART3_MDR2);
367                 HDBG1(2);
368                 if (mdr2 & 1)
369                         printk(KERN_ERR "IRDA Buffer underrun error");
370
371                 si->stats.tx_packets++;
372
373                 if (si->newspeed) {
374                         omap1610_irda_set_speed(dev, si->newspeed);
375                         si->newspeed = 0;
376                 }
377
378                 netif_wake_queue(dev);
379
380                 if (!(status & 0x80))
381                         return IRQ_HANDLED;
382         }
383
384         /* Stop DMA and if there are no errors, send frame to upper layer */
385
386         omap_stop_dma(si->rx_dma_channel);
387
388         status = omap_readb(UART3_SFLSR);       /* Take a frame status */
389
390         if (status != 0) {      /* Bad frame? */
391                 si->stats.rx_frame_errors++;
392                 omap_readb(UART3_RESUME);
393         } else {
394                 /* We got a frame! */
395                 skb = alloc_skb(4096, GFP_ATOMIC);
396
397                 if (!skb) {
398                         printk(KERN_ERR "omap_sir: out of memory for RX SKB\n");
399                         return IRQ_HANDLED;
400                 }
401                 /*
402                  * Align any IP headers that may be contained
403                  * within the frame.
404                  */
405
406                 skb_reserve(skb, 1);
407
408                 w = OMAP_DMA_CDAC_REG(si->rx_dma_channel);
409                 w -= OMAP1_DMA_CDSA_L_REG(si->rx_dma_channel);
410
411                 if (si->speed != 4000000) {
412                         memcpy(skb_put(skb, w - 2), si->rx_buf_dma_virt, w - 2);        /* Copy DMA buffer to skb */
413                 } else {
414                         memcpy(skb_put(skb, w - 4), si->rx_buf_dma_virt, w - 4);        /* Copy DMA buffer to skb */
415                 }
416
417                 skb->dev = dev;
418                 skb->mac.raw = skb->data;
419                 skb->protocol = htons(ETH_P_IRDA);
420                 si->stats.rx_packets++;
421                 si->stats.rx_bytes += skb->len;
422                 netif_receive_skb(skb); /* Send data to upper level */
423         }
424
425         /* Re-init RX DMA */
426         omap1610_irda_start_rx_dma(si);
427
428         dev->last_rx = jiffies;
429
430         __ECHO_OUT;
431
432         return IRQ_HANDLED;
433 }
434
435 static int omap1610_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
436 {
437         struct omap1610_irda *si = dev->priv;
438         int speed = irda_get_next_speed(skb);
439         int mtt = irda_get_mtt(skb);
440         int xbofs = irda_get_next_xbofs(skb);
441
442         __ECHO_IN;
443
444         /*
445          * Does this packet contain a request to change the interface
446          * speed?  If so, remember it until we complete the transmission
447          * of this frame.
448          */
449         if (speed != si->speed && speed != -1)
450                 si->newspeed = speed;
451
452         if (xbofs) {
453                 /* Set number of addtional BOFS */
454                 omap_writeb(xbofs + 1, UART3_EBLR);
455         }
456
457         /*
458          * If this is an empty frame, we can bypass a lot.
459          */
460         if (skb->len == 0) {
461                 if (si->newspeed) {
462                         si->newspeed = 0;
463                         omap1610_irda_set_speed(dev, speed);
464                 }
465                 dev_kfree_skb(skb);
466                 return 0;
467         }
468
469         netif_stop_queue(dev);
470
471         /* Copy skb data to DMA buffer */
472
473         memcpy(si->tx_buf_dma_virt, skb->data, skb->len);
474
475         si->stats.tx_bytes += skb->len;
476
477         /* Set frame length */
478
479         omap_writeb((skb->len & 0xff), UART3_TXFLL);
480         omap_writeb((skb->len >> 8), UART3_TXFLH);
481
482         if (mtt > 1000)
483                 mdelay(mtt / 1000);
484         else
485                 udelay(mtt);
486
487         /* Start TX DMA transfer */
488
489         omap1610_start_tx_dma(si, skb->len);
490
491         /* We can free skb now because it's already in DMA buffer */
492
493         dev_kfree_skb(skb);
494
495         dev->trans_start = jiffies;
496
497         __ECHO_OUT;
498
499         return 0;
500 }
501
502 static int
503 omap1610_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
504 {
505         struct if_irda_req *rq = (struct if_irda_req *)ifreq;
506         struct omap1610_irda *si = dev->priv;
507         int ret = -EOPNOTSUPP;
508
509         __ECHO_IN;
510
511         switch (cmd) {
512         case SIOCSBANDWIDTH:
513                 if (capable(CAP_NET_ADMIN)) {
514                         /*
515                          * We are unable to set the speed if the
516                          * device is not running.
517                          */
518                         if (si->open) {
519                                 ret =
520                                     omap1610_irda_set_speed(dev,
521                                                             rq->ifr_baudrate);
522                         } else {
523                                 printk
524                                     (KERN_ERR
525                                      "omap_irda_ioctl: SIOCSBANDWIDTH: !netif_running\n");
526                                 ret = 0;
527                         }
528                 }
529                 break;
530
531         case SIOCSMEDIABUSY:
532                 ret = -EPERM;
533                 if (capable(CAP_NET_ADMIN)) {
534                         irda_device_set_media_busy(dev, TRUE);
535                         ret = 0;
536                 }
537                 break;
538
539         case SIOCGRECEIVING:
540                 rq->ifr_receiving = rx_state;
541                 break;
542
543         default:
544                 break;
545         }
546
547         __ECHO_OUT;
548
549         return ret;
550 }
551
552 static struct net_device_stats *omap1610_irda_stats(struct net_device *dev)
553 {
554         struct omap1610_irda *si = dev->priv;
555         return &si->stats;
556 }
557
558 static int omap1610_irda_start(struct net_device *dev)
559 {
560         struct omap1610_irda *si = dev->priv;
561         int err;
562         unsigned long flags = 0;
563
564 #ifdef CONFIG_MACH_OMAP_H3
565         u8 ioExpanderVal = 0;
566 #endif
567
568         __ECHO_IN;
569         si->speed = 9600;
570
571         err = request_irq(dev->irq, omap1610_irda_irq, 0, dev->name, dev);
572         if (err)
573                 goto err_irq;
574
575         /*
576          * The interrupt must remain disabled for now.
577          */
578
579         disable_irq(dev->irq);
580
581         /*  Request DMA channels for IrDA hardware */
582
583         if (omap_request_dma(OMAP_DMA_UART3_RX, "IrDA Rx DMA",
584                              (void *)omap1610_irda_rx_dma_callback,
585                              dev, &(si->rx_dma_channel))) {
586                 printk(KERN_ERR "Failed to request IrDA Rx DMA \n");
587                 goto err_irq;
588         }
589
590         if (omap_request_dma(OMAP_DMA_UART3_TX, "IrDA Tx DMA",
591                              (void *)omap1610_irda_tx_dma_callback,
592                              dev, &(si->tx_dma_channel))) {
593                 printk(KERN_ERR "Failed to request IrDA Tx DMA \n");
594                 goto err_irq;
595         }
596
597         /* Allocate TX and RX buffers for DMA channels */
598
599         si->rx_buf_dma_virt =
600             dma_alloc_coherent(NULL, 4096, &(si->rx_buf_dma_phys), flags);
601
602         si->tx_buf_dma_virt =
603             dma_alloc_coherent(NULL, 4096, &(si->tx_buf_dma_phys), flags);
604
605         /*
606          * Setup the serial port for the specified config.
607          */
608
609 #ifdef CONFIG_MACH_OMAP_H3
610
611         if ((err = read_gpio_expa(&ioExpanderVal, 0x26))) {
612                 printk(KERN_ERR "Error reading from I/O EXPANDER \n");
613                 return err;
614         }
615
616         ioExpanderVal |= 0x40;  /* 'P6' Enable IRDA_TX and IRDA_RX */
617
618         if ((err = write_gpio_expa(ioExpanderVal, 0x26))) {
619                 printk(KERN_ERR "Error writing to I/O EXPANDER \n");
620                 return err;
621         }
622 #endif
623         err = omap1610_irda_startup(dev);
624
625         if (err)
626                 goto err_startup;
627
628         omap1610_irda_set_speed(dev, si->speed = 9600);
629
630         /*
631          * Open a new IrLAP layer instance.
632          */
633
634         si->irlap = irlap_open(dev, &si->qos, "omap_sir");
635
636         err = -ENOMEM;
637         if (!si->irlap)
638                 goto err_irlap;
639
640         /* Now enable the interrupt and start the queue  */
641         si->open = 1;
642
643         /* Start RX DMA */
644
645         omap1610_irda_start_rx_dma(si);
646
647         enable_irq(dev->irq);
648         netif_start_queue(dev);
649
650         __ECHO_OUT;
651
652         return 0;
653
654       err_irlap:
655         si->open = 0;
656         omap1610_irda_shutdown(si);
657       err_startup:
658       err_irq:
659         free_irq(dev->irq, dev);
660         return err;
661 }
662
663 static int omap1610_irda_stop(struct net_device *dev)
664 {
665         struct omap1610_irda *si = dev->priv;
666
667         __ECHO_IN;
668
669         disable_irq(dev->irq);
670
671         netif_stop_queue(dev);
672
673         omap_free_dma(si->rx_dma_channel);
674         omap_free_dma(si->tx_dma_channel);
675
676         dma_free_coherent(NULL, 4096, si->rx_buf_dma_virt, si->rx_buf_dma_phys);
677         dma_free_coherent(NULL, 4096, si->tx_buf_dma_virt, si->tx_buf_dma_phys);
678
679         omap1610_irda_shutdown(si);
680
681         /* Stop IrLAP */
682         if (si->irlap) {
683                 irlap_close(si->irlap);
684                 si->irlap = NULL;
685         }
686
687         si->open = 0;
688
689         /*
690          * Free resources
691          */
692
693         free_irq(dev->irq, dev);
694
695         __ECHO_OUT;
696
697         return 0;
698 }
699
700 #ifdef CONFIG_MACH_OMAP_H3
701
702 static void set_h3_gpio_expa(u8 FIR_SEL, u8 IrDA_INVSEL)
703 {
704         u8 ioExpanderVal = 0;
705
706         if (read_gpio_expa(&ioExpanderVal, 0x27) != 0) {
707                 printk(KERN_ERR "Error reading from I/O EXPANDER \n");
708                 return;
709         }
710
711         ioExpanderVal &= ~0x03;
712         ioExpanderVal |= FIR_SEL << 1;
713         ioExpanderVal |= IrDA_INVSEL << 0;
714
715         if (write_gpio_expa(ioExpanderVal, 0x27) != 0) {
716                 printk(KERN_ERR "Error writing to I/O EXPANDER \n");
717                 return;
718         }
719         if (read_gpio_expa(&ioExpanderVal, 0x27) != 0) {
720                 printk(KERN_ERR "Error reading from I/O EXPANDER \n");
721                 return;
722         }
723 }
724
725 int which_speed;
726
727 static void set_h3_gpio_expa_handler(void *data)
728 {
729         int *mode = data;
730
731         if (*mode == SIR_MODE)
732                 set_h3_gpio_expa(0, 1);
733         else if (*mode == MIR_MODE)
734                 set_h3_gpio_expa(1, 1);
735         else if (*mode == FIR_MODE)
736                 set_h3_gpio_expa(1, 1);
737 }
738
739 DECLARE_WORK(set_h3_gpio_expa_work, &set_h3_gpio_expa_handler, &which_speed);
740
741 static inline void set_h3_irda_mode(int mode)
742 {
743         cancel_delayed_work(&set_h3_gpio_expa_work);
744         which_speed = mode;
745         schedule_work(&set_h3_gpio_expa_work);
746 }
747 #else
748 #define set_h3_irda_mode(x)
749 #endif
750
751 static int omap1610_irda_set_speed(struct net_device *dev, int speed)
752 {
753         struct omap1610_irda *si = dev->priv;
754         int divisor;
755
756         __ECHO_IN;
757
758         /* Set IrDA speed */
759         if (speed <= 115200) {
760                 /* SIR mode */
761                 if (machine_is_omap_h2()) {
762                         omap_set_gpio_dataout(OMAP1610_H2_FIRSEL_GPIO, 0);
763                 }
764
765                 if (machine_is_omap_h3())
766                         set_h3_irda_mode(SIR_MODE);
767
768                 printk("Set SIR Mode! Speed: %d\n", speed);
769
770                 omap_writeb(1, UART3_MDR1);     /* Set SIR mode */
771
772                 omap_writeb(1, UART3_EBLR);
773
774                 divisor = 48000000 / (16 * speed);      /* Base clock 48 MHz */
775
776                 HDBG2(1);
777                 omap_writeb(1 << 7, UART3_LCR);
778
779                 omap_writeb((divisor & 0xFF), UART3_DLL);
780
781                 omap_writeb((divisor >> 8), UART3_DLH);
782
783                 omap_writeb(0x03, UART3_LCR);
784
785                 omap_writeb(0, UART3_MCR);
786
787                 HDBG2(1);
788
789         } else if (speed <= 1152000) {
790                 /* MIR mode */
791                 printk("Set MIR Mode! Speed: %d\n", speed);
792
793                 omap_writeb((1 << 2) | (1 << 6), UART3_MDR1);   /* Set MIR mode with 
794                                                                    SIP after each frame */
795
796                 omap_writeb(2, UART3_EBLR);
797
798                 divisor = 48000000 / (41 * speed);      /* Base clock 48 MHz */
799
800                 omap_writeb(1 << 7, UART3_LCR);
801
802                 omap_writeb((divisor & 0xFF), UART3_DLL);
803
804                 omap_writeb((divisor >> 8), UART3_DLH);
805
806                 omap_writeb(0x03, UART3_LCR);
807
808                 if (machine_is_omap_h2())
809                         omap_set_gpio_dataout(OMAP1610_H2_FIRSEL_GPIO, 1);
810
811                 if (machine_is_omap_h3())
812                         set_h3_irda_mode(MIR_MODE);
813
814         } else {
815                 /* FIR mode */
816
817                 printk("Set FIR Mode! Speed: %d\n", speed);
818
819                 omap_writeb((1 << 2) | (1 << 6) | 1, UART3_MDR1);       /* Set FIR mode
820                                                                            with SIP after each frame */
821                 if (machine_is_omap_h2())
822                         omap_set_gpio_dataout(OMAP1610_H2_FIRSEL_GPIO, 1);
823
824                 if (machine_is_omap_h3())
825                         set_h3_irda_mode(FIR_MODE);
826         }
827
828         si->speed = speed;
829
830         __ECHO_OUT;
831
832         return 0;
833
834 }
835
836 #ifdef CONFIG_PM
837 /*
838  * Suspend the IrDA interface.
839  */
840 static int omap1610_irda_suspend(struct device *_dev, u32 state, u32 level)
841 {
842         struct net_device *dev = dev_get_drvdata(_dev);
843         struct omap1610_irda *si = dev->priv;
844
845         if (!dev || level != SUSPEND_DISABLE)
846                 return 0;
847
848         if (si->open) {
849                 /*
850                  * Stop the transmit queue
851                  */
852                 netif_device_detach(dev);
853                 disable_irq(dev->irq);
854                 omap1610_irda_shutdown(si);
855         }
856         return 0;
857 }
858
859 /*
860  * Resume the IrDA interface.
861  */
862 static int omap1610_irda_resume(struct device *_dev, u32 level)
863 {
864         struct net_device *dev = dev_get_drvdata(_dev);
865         struct omap1610_irda *si= dev->priv;
866
867         if (!dev || level != RESUME_ENABLE)
868                 return 0;
869
870         if (si->open) {
871                 /*
872                  * If we missed a speed change, initialise at the new speed
873                  * directly.  It is debatable whether this is actually
874                  * required, but in the interests of continuing from where
875                  * we left off it is desireable.  The converse argument is
876                  * that we should re-negotiate at 9600 baud again.
877                  */
878                 if (si->newspeed) {
879                         si->speed = si->newspeed;
880                         si->newspeed = 0;
881                 }
882
883                 omap1610_irda_startup(dev);
884                 omap1610_irda_set_speed(dev, si->speed);
885                 enable_irq(dev->irq);
886
887                 /*
888                  * This automatically wakes up the queue
889                  */
890                 netif_device_attach(dev);
891         }
892
893         return 0;
894 }
895 #else
896 #define omap1610_irda_suspend   NULL
897 #define omap1610_irda_resume    NULL
898 #endif
899
900 static int omap1610_irda_probe(struct device *_dev)
901 {
902         struct platform_device *pdev = to_platform_device(_dev);
903         struct net_device *dev;
904         struct omap1610_irda *si;
905         unsigned int baudrate_mask;
906         int err = 0;
907
908         dev = alloc_irdadev(sizeof(struct omap1610_irda));
909         if (!dev)
910                 goto err_mem_1;
911
912         si = dev->priv;
913         si->dev = &pdev->dev;
914         dev->hard_start_xmit = omap1610_irda_hard_xmit;
915         dev->open = omap1610_irda_start;
916         dev->stop = omap1610_irda_stop;
917         dev->do_ioctl = omap1610_irda_ioctl;
918         dev->get_stats = omap1610_irda_stats;
919         dev->irq = INT_UART3;
920
921         irda_init_max_qos_capabilies(&si->qos);
922
923         /*
924          *  OMAP1610  supports SIR, MIR, FIR modes,
925          *  but actualy supported modes depend on hardware implementation.
926          *  OMAP1610 Innovator supports only SIR and 
927          *  OMAP1610 H2 supports both SIR and FIR
928          */
929
930         baudrate_mask =
931             IR_9600 | IR_19200 | IR_38400 | IR_57600 | IR_115200 | IR_576000 |
932             IR_1152000;
933
934         if (machine_is_omap_h2() || machine_is_omap_h3()) {
935
936                 baudrate_mask |= (IR_4000000 << 8);
937         }
938
939         si->qos.baud_rate.bits &= baudrate_mask;
940         si->qos.min_turn_time.bits = 7;
941
942         irda_qos_bits_to_value(&si->qos);
943
944         err = register_netdev(dev);
945         if (!err)
946                 dev_set_drvdata(&pdev->dev, dev);
947         else 
948                 free_netdev(dev);
949
950       err_mem_1:
951         return err;
952 }
953
954 static int omap1610_irda_remove(struct device *_dev)
955 {
956         struct net_device *dev = dev_get_drvdata(_dev);
957         
958 #ifdef CONFIG_MACH_OMAP_H3
959         if (machine_is_omap_h3())
960                 cancel_delayed_work(&set_h3_gpio_expa_work);
961 #endif
962         if (dev) {
963                 unregister_netdev(dev);
964                 free_netdev(dev);
965         }
966         return 0;
967 }
968
969 static struct device_driver omap1610ir_driver = {
970         .name = "omap1610-ir",
971         .bus = &platform_bus_type,
972         .probe = omap1610_irda_probe,
973         .remove = omap1610_irda_remove,
974         .suspend = omap1610_irda_suspend,
975         .resume = omap1610_irda_resume,
976 };
977
978 static int __init omap1610_irda_init(void)
979 {
980         return driver_register(&omap1610ir_driver);
981
982 }
983
984 static void __exit omap1610_irda_exit(void)
985 {
986         driver_unregister(&omap1610ir_driver);
987 }
988
989 module_init(omap1610_irda_init);
990 module_exit(omap1610_irda_exit);
991
992 MODULE_AUTHOR("MontaVista");
993 MODULE_DESCRIPTION("OMAP IrDA Driver");
994 MODULE_LICENSE("GPL");
995