]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/net/irda/omap-ir.c
Merge with /home/tmlind/src/kernel/linux-2.6
[linux-2.6-omap-h63xx.git] / drivers / net / irda / omap-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 #include <linux/i2c.h>
59
60 #include <net/irda/irda.h>
61 #include <net/irda/irmod.h>
62 #include <net/irda/wrapper.h>
63 #include <net/irda/irda_device.h>
64
65 #include <asm/irq.h>
66 #include <asm/io.h>
67 #include <asm/hardware.h>
68 #include <asm/serial.h>
69 #include <asm/mach-types.h>
70 #include <asm/dma.h>
71 #include <asm/arch/mux.h>
72 #include <asm/arch/gpio.h>
73 #include <asm/arch/irda.h>
74
75 #define UART3_EFR_EN                    (1 << 4)
76 #define UART3_MCR_EN_TCR_TLR            (1 << 6)
77
78 #define UART3_LCR_WL_8                  (3 << 0)
79 #define UART3_LCR_SP2                   (1 << 2)
80 #define UART3_LCR_DIVEN                 (1 << 7)
81
82 #define UART3_FCR_FIFO_EN               (1 << 0)
83 #define UART3_FCR_FIFO_RX               (1 << 1)
84 #define UART3_FCR_FIFO_TX               (1 << 2)
85 #define UART3_FCR_FIFO_DMA1             (1 << 3)
86 #define UART3_FCR_FIFO_TX_TRIG16        (1 << 4)
87 #define UART3_FCR_FIFO_RX_TRIG16        (1 << 6)
88 #define UART3_FCR_CONFIG        UART3_FCR_FIFO_EN | UART3_FCR_FIFO_RX | \
89                                 UART3_FCR_FIFO_TX | UART3_FCR_FIFO_DMA1 | \
90                                 UART3_FCR_FIFO_TX_TRIG16 | \
91                                 UART3_FCR_FIFO_RX_TRIG16
92
93 #define UART3_SCR_TX_TRIG1              (1 << 6)
94 #define UART3_SCR_RX_TRIG1              (1 << 7)
95
96 #define UART3_MDR1_RESET                (0x07)
97 #define UART3_MDR1_SIR                  (1 << 0)
98 #define UART3_MDR1_MIR                  (4 << 0)
99 #define UART3_MDR1_FIR                  (5 << 0)
100 #define UART3_MDR1_SIP_AUTO             (1 << 6)
101
102 #define UART3_MDR2_TRIG1                (0 << 1)
103 #define UART3_MDR2_IRTX_UNDERRUN        (1 << 0)
104
105 #define UART3_ACERG_TX_UNDERRUN_DIS     (1 << 4)
106 #define UART3_ACERG_SD_MODE_LOW         (1 << 6)
107 #define UART3_ACERG_DIS_IR_RX           (1 << 5)
108
109 #define UART3_IER_EOF                   (1 << 5)
110 #define UART3_IER_CTS                   (1 << 7)
111
112 #define UART3_IIR_TX_STATUS             (1 << 5)
113 #define UART3_IIR_EOF                   (0x80)
114
115 #define IS_FIR(si)              ((si)->speed >= 4000000)
116 #define IRDA_FRAME_SIZE_LIMIT   4096
117
118 static int rx_state = 0;        /* RX state for IOCTL */
119
120 struct omap_irda {
121         unsigned char open;
122         int speed;              /* Current IrDA speed */
123         int newspeed;
124
125         struct net_device_stats stats;
126         struct irlap_cb *irlap;
127         struct qos_info qos;
128
129         int rx_dma_channel;
130         int tx_dma_channel;
131
132         dma_addr_t rx_buf_dma_phys;     /* Physical address of RX DMA buffer */
133         dma_addr_t tx_buf_dma_phys;     /* Physical address of TX DMA buffer */
134
135         void *rx_buf_dma_virt;          /* Virtual address of RX DMA buffer */
136         void *tx_buf_dma_virt;          /* Virtual address of TX DMA buffer */
137
138         struct device *dev;
139         struct omap_irda_config *pdata;
140 };
141
142 static void inline uart_reg_out(int idx, u8 val)
143 {
144         omap_writeb(val, idx);
145 }
146
147 static u8 inline uart_reg_in(int idx)
148 {
149         u8 b = omap_readb(idx);
150         return b;
151 }
152
153 /* forward declarations */
154 extern void irda_device_setup(struct net_device *dev);
155 extern void omap_stop_dma(int lch);
156 static int omap_irda_set_speed(struct net_device *dev, int speed);
157
158 static void omap_irda_start_rx_dma(struct omap_irda *si)
159 {
160         /* Configure DMA */
161         omap_set_dma_src_params(si->rx_dma_channel, 0x3, 0x0,
162                                 si->pdata->src_start,
163                                 0, 0);
164
165         omap_enable_dma_irq(si->rx_dma_channel, 0x01);
166
167         omap_set_dma_dest_params(si->rx_dma_channel, 0x0, 0x1,
168                                 si->rx_buf_dma_phys,
169                                 0, 0);
170
171         omap_set_dma_transfer_params(si->rx_dma_channel, 0x0,
172                                 IRDA_FRAME_SIZE_LIMIT, 0x1,
173                                 0x0, si->pdata->rx_trigger, 0);
174
175         omap_start_dma(si->rx_dma_channel);
176 }
177
178 static void omap_start_tx_dma(struct omap_irda *si, int size)
179 {
180         /* Configure DMA */
181         omap_set_dma_dest_params(si->tx_dma_channel, 0x03, 0x0,
182                                 si->pdata->dest_start, 0, 0);
183
184         omap_enable_dma_irq(si->tx_dma_channel, 0x01);
185
186         omap_set_dma_src_params(si->tx_dma_channel, 0x0, 0x1,
187                                 si->tx_buf_dma_phys,
188                                 0, 0);
189
190         omap_set_dma_transfer_params(si->tx_dma_channel, 0x0, size, 0x1,
191                                 0x0, si->pdata->tx_trigger, 0);
192
193         /* Start DMA */
194         omap_start_dma(si->tx_dma_channel);
195 }
196
197 /* DMA RX callback - normally, we should not go here,
198  * it calls only if something is going wrong
199  */
200 static void omap_irda_rx_dma_callback(int lch, u16 ch_status, void *data)
201 {
202         struct net_device *dev = data;
203         struct omap_irda *si = dev->priv;
204
205         printk(KERN_ERR "RX Transfer error or very big frame\n");
206
207         /* Clear interrupts */
208         uart_reg_in(UART3_IIR);
209
210         si->stats.rx_frame_errors++;
211
212         uart_reg_in(UART3_RESUME);
213
214         /* Re-init RX DMA */
215         omap_irda_start_rx_dma(si);
216 }
217
218 /* DMA TX callback - calling when frame transfer has been finished */
219 static void omap_irda_tx_dma_callback(int lch, u16 ch_status, void *data)
220 {
221         struct net_device *dev = data;
222         struct omap_irda *si = dev->priv;
223
224         /*Stop DMA controller */
225         omap_stop_dma(si->tx_dma_channel);
226 }
227
228 /*
229  * Set the IrDA communications speed.
230  * Interrupt have to be disabled here.
231  */
232 static int omap_irda_startup(struct net_device *dev)
233 {
234         struct omap_irda *si = dev->priv;
235
236
237         /* FIXME: use clk_* apis for UART3 clock*/
238         /* Enable UART3 clock and set UART3 to IrDA mode */
239         if (machine_is_omap_h2() || machine_is_omap_h3())
240                 omap_writel(omap_readl(MOD_CONF_CTRL_0) | (1 << 31) | (1 << 15),
241                                 MOD_CONF_CTRL_0);
242
243         /* Only for H2?
244          */
245         if (si->pdata->transceiver_mode && machine_is_omap_h2()) {
246                 /* Is it select_irda on H2 ? */
247                 omap_writel(omap_readl(FUNC_MUX_CTRL_A) | 7,
248                                         FUNC_MUX_CTRL_A);
249                 si->pdata->transceiver_mode(si->dev, IR_SIRMODE);
250         }
251
252         uart_reg_out(UART3_MDR1, UART3_MDR1_RESET);     /* Reset mode */
253
254         /* Clear DLH and DLL */
255         uart_reg_out(UART3_LCR, UART3_LCR_DIVEN);
256
257         uart_reg_out(UART3_DLL, 0);
258         uart_reg_out(UART3_DLH, 0);
259         uart_reg_out(UART3_LCR, 0xbf);  /* FIXME: Add #define */
260
261         uart_reg_out(UART3_EFR, UART3_EFR_EN);
262         uart_reg_out(UART3_LCR, UART3_LCR_DIVEN);
263
264         /* Enable access to UART3_TLR and UART3_TCR registers */
265         uart_reg_out(UART3_MCR, UART3_MCR_EN_TCR_TLR);
266
267         uart_reg_out(UART3_SCR, 0);
268         /* Set Rx trigger to 1 and Tx trigger to 1 */
269         uart_reg_out(UART3_TLR, 0);
270
271         /* Set LCR to 8 bits and 1 stop bit */
272         uart_reg_out(UART3_LCR, 0x03);
273
274         /* Clear RX and TX FIFO and enable FIFO */
275         /* Use DMA Req for transfers */
276         uart_reg_out(UART3_FCR, UART3_FCR_CONFIG);
277
278         uart_reg_out(UART3_MCR, 0);
279
280         uart_reg_out(UART3_SCR, UART3_SCR_TX_TRIG1 |
281                         UART3_SCR_RX_TRIG1);
282
283         /* Enable UART3 SIR Mode,(Frame-length method to end frames) */
284         uart_reg_out(UART3_MDR1, UART3_MDR1_SIR);
285
286         /* Set Status FIFO trig to 1 */
287         uart_reg_out(UART3_MDR2, 0);
288
289         /* Enables RXIR input */
290         /* and disable TX underrun */
291         /* SEND_SIP pulse */
292         uart_reg_out(UART3_ACREG, UART3_ACERG_SD_MODE_LOW |
293                         UART3_ACERG_TX_UNDERRUN_DIS);
294
295         /* Enable EOF Interrupt only */
296         uart_reg_out(UART3_IER, UART3_IER_CTS | UART3_IER_EOF);
297
298         /* Set Maximum Received Frame size to 2048 bytes */
299         uart_reg_out(UART3_RXFLL, 0x00);
300         uart_reg_out(UART3_RXFLH, 0x08);
301
302         uart_reg_in(UART3_RESUME);
303
304         return 0;
305 }
306
307 static int omap_irda_shutdown(struct omap_irda *si)
308 {
309         unsigned long flags;
310
311         local_irq_save(flags);
312
313         /* Disable all UART3 Interrupts */
314         uart_reg_out(UART3_IER, 0);
315
316         /* Disable UART3 and disable baud rate generator */
317         uart_reg_out(UART3_MDR1, UART3_MDR1_RESET);
318
319         /* set SD_MODE pin to high and Disable RX IR */
320         uart_reg_out(UART3_ACREG, (UART3_ACERG_DIS_IR_RX |
321                         ~(UART3_ACERG_SD_MODE_LOW)));
322
323         /* Clear DLH and DLL */
324         uart_reg_out(UART3_LCR, UART3_LCR_DIVEN);
325         uart_reg_out(UART3_DLL, 0);
326         uart_reg_out(UART3_DLH, 0);
327
328         local_irq_restore(flags);
329
330         return 0;
331 }
332
333 static irqreturn_t
334 omap_irda_irq(int irq, void *dev_id, struct pt_regs *hw_regs)
335 {
336         struct net_device *dev = dev_id;
337         struct omap_irda *si = dev->priv;
338         struct sk_buff *skb;
339
340         u8 status;
341         int w = 0;
342
343         /* Clear EOF interrupt */
344         status = uart_reg_in(UART3_IIR);
345
346         if (status & UART3_IIR_TX_STATUS) {
347                 u8 mdr2 = uart_reg_in(UART3_MDR2);
348                 if (mdr2 & UART3_MDR2_IRTX_UNDERRUN)
349                         printk(KERN_ERR "IrDA Buffer underrun error\n");
350
351                 si->stats.tx_packets++;
352
353                 if (si->newspeed) {
354                         omap_irda_set_speed(dev, si->newspeed);
355                         si->newspeed = 0;
356                 }
357
358                 netif_wake_queue(dev);
359                 if (!(status & UART3_IIR_EOF))
360                         return IRQ_HANDLED;
361         }
362
363         /* Stop DMA and if there are no errors, send frame to upper layer */
364         omap_stop_dma(si->rx_dma_channel);
365
366         status = uart_reg_in(UART3_SFLSR);      /* Take a frame status */
367
368         if (status != 0) {      /* Bad frame? */
369                 si->stats.rx_frame_errors++;
370                 uart_reg_in(UART3_RESUME);
371         } else {
372                 /* We got a frame! */
373                 skb = alloc_skb(IRDA_FRAME_SIZE_LIMIT, GFP_ATOMIC);
374
375                 if (!skb) {
376                         printk(KERN_ERR "omap_sir: out of memory for RX SKB\n");
377                         return IRQ_HANDLED;
378                 }
379                 /*
380                  * Align any IP headers that may be contained
381                  * within the frame.
382                  */
383
384                 skb_reserve(skb, 1);
385
386                 w = OMAP_DMA_CDAC_REG(si->rx_dma_channel);
387
388                 if (cpu_is_omap16xx())
389                         w -= OMAP1_DMA_CDSA_L_REG(si->rx_dma_channel);
390                 if (cpu_is_omap24xx())
391                         w -= OMAP2_DMA_CDSA_REG(si->rx_dma_channel);
392
393                 if (!IS_FIR(si)) {
394                         /* Copy DMA buffer to skb */
395                         memcpy(skb_put(skb, w - 2), si->rx_buf_dma_virt, w - 2);
396                 } else {
397                         /* Copy DMA buffer to skb */
398                         memcpy(skb_put(skb, w - 4), si->rx_buf_dma_virt, w - 4);
399                 }
400
401                 skb->dev = dev;
402                 skb->mac.raw = skb->data;
403                 skb->protocol = htons(ETH_P_IRDA);
404                 si->stats.rx_packets++;
405                 si->stats.rx_bytes += skb->len;
406                 netif_receive_skb(skb); /* Send data to upper level */
407         }
408
409         /* Re-init RX DMA */
410         omap_irda_start_rx_dma(si);
411
412         dev->last_rx = jiffies;
413
414         return IRQ_HANDLED;
415 }
416
417 static int omap_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
418 {
419         struct omap_irda *si = dev->priv;
420         int speed = irda_get_next_speed(skb);
421         int mtt = irda_get_mtt(skb);
422         int xbofs = irda_get_next_xbofs(skb);
423
424
425         /*
426          * Does this packet contain a request to change the interface
427          * speed?  If so, remember it until we complete the transmission
428          * of this frame.
429          */
430         if (speed != si->speed && speed != -1)
431                 si->newspeed = speed;
432
433         if (xbofs) /* Set number of addtional BOFS */
434                 uart_reg_out(UART3_EBLR, xbofs + 1);
435
436         /*
437          * If this is an empty frame, we can bypass a lot.
438          */
439         if (skb->len == 0) {
440                 if (si->newspeed) {
441                         si->newspeed = 0;
442                         omap_irda_set_speed(dev, speed);
443                 }
444                 dev_kfree_skb(skb);
445                 return 0;
446         }
447
448         netif_stop_queue(dev);
449
450         /* Copy skb data to DMA buffer */
451         memcpy(si->tx_buf_dma_virt, skb->data, skb->len);
452
453         /* Copy skb data to DMA buffer */
454         si->stats.tx_bytes += skb->len;
455
456         /* Set frame length */
457         uart_reg_out(UART3_TXFLL, (skb->len & 0xff));
458         uart_reg_out(UART3_TXFLH, (skb->len >> 8));
459
460         if (mtt > 1000)
461                 mdelay(mtt / 1000);
462         else
463                 udelay(mtt);
464
465         /* Start TX DMA transfer */
466         omap_start_tx_dma(si, skb->len);
467
468         /* We can free skb now because it's already in DMA buffer */
469         dev_kfree_skb(skb);
470
471         dev->trans_start = jiffies;
472
473         return 0;
474 }
475
476 static int
477 omap_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
478 {
479         struct if_irda_req *rq = (struct if_irda_req *)ifreq;
480         struct omap_irda *si = dev->priv;
481         int ret = -EOPNOTSUPP;
482
483
484         switch (cmd) {
485         case SIOCSBANDWIDTH:
486                 if (capable(CAP_NET_ADMIN)) {
487                         /*
488                          * We are unable to set the speed if the
489                          * device is not running.
490                          */
491                         if (si->open)
492                                 ret = omap_irda_set_speed(dev,
493                                                 rq->ifr_baudrate);
494                         else {
495                                 printk(KERN_ERR "omap_irda_ioctl: SIOCSBANDWIDTH: !netif_running\n");
496                                 ret = 0;
497                         }
498                 }
499                 break;
500
501         case SIOCSMEDIABUSY:
502                 ret = -EPERM;
503                 if (capable(CAP_NET_ADMIN)) {
504                         irda_device_set_media_busy(dev, TRUE);
505                         ret = 0;
506                 }
507                 break;
508
509         case SIOCGRECEIVING:
510                 rq->ifr_receiving = rx_state;
511                 break;
512
513         default:
514                 break;
515         }
516
517         return ret;
518 }
519
520 static struct net_device_stats *omap_irda_stats(struct net_device *dev)
521 {
522         struct omap_irda *si = dev->priv;
523         return &si->stats;
524 }
525
526 static int omap_irda_start(struct net_device *dev)
527 {
528         struct omap_irda *si = dev->priv;
529         int err;
530
531         si->speed = 9600;
532
533         err = request_irq(dev->irq, omap_irda_irq, 0, dev->name, dev);
534         if (err)
535                 goto err_irq;
536
537         /*
538          * The interrupt must remain disabled for now.
539          */
540         disable_irq(dev->irq);
541
542         /*  Request DMA channels for IrDA hardware */
543         if (omap_request_dma(si->pdata->rx_channel, "IrDA Rx DMA",
544                         (void *)omap_irda_rx_dma_callback,
545                         dev, &(si->rx_dma_channel))) {
546                 printk(KERN_ERR "Failed to request IrDA Rx DMA\n");
547                 goto err_irq;
548         }
549
550         if (omap_request_dma(si->pdata->tx_channel, "IrDA Tx DMA",
551                         (void *)omap_irda_tx_dma_callback,
552                         dev, &(si->tx_dma_channel))) {
553                 printk(KERN_ERR "Failed to request IrDA Tx DMA\n");
554                 goto err_irq;
555         }
556
557         /* Allocate TX and RX buffers for DMA channels */
558         si->rx_buf_dma_virt =
559                 dma_alloc_coherent(NULL, IRDA_FRAME_SIZE_LIMIT,
560                                 &(si->rx_buf_dma_phys),
561                                 GFP_KERNEL);
562
563         if (!si->rx_buf_dma_virt) {
564                 printk(KERN_ERR "Unable to allocate memory for rx_buf_dma\n");
565                 goto err_irq;
566         }
567
568         si->tx_buf_dma_virt =
569                 dma_alloc_coherent(NULL, IRDA_FRAME_SIZE_LIMIT,
570                                 &(si->tx_buf_dma_phys),
571                                 GFP_KERNEL);
572
573         if (!si->tx_buf_dma_virt) {
574                 printk(KERN_ERR "Unable to allocate memory for tx_buf_dma\n");
575                 goto err_mem1;
576         }
577
578         /*
579          * Setup the serial port for the specified config.
580          */
581         if (si->pdata->select_irda)
582                 si->pdata->select_irda(si->dev, IR_SEL);
583
584         err = omap_irda_startup(dev);
585
586         if (err)
587                 goto err_startup;
588
589         omap_irda_set_speed(dev, si->speed = 9600);
590
591         /*
592          * Open a new IrLAP layer instance.
593          */
594         si->irlap = irlap_open(dev, &si->qos, "omap_sir");
595
596         err = -ENOMEM;
597         if (!si->irlap)
598                 goto err_irlap;
599
600         /* Now enable the interrupt and start the queue  */
601         si->open = 1;
602
603         /* Start RX DMA */
604         omap_irda_start_rx_dma(si);
605
606         enable_irq(dev->irq);
607         netif_start_queue(dev);
608
609         return 0;
610
611 err_irlap:
612         si->open = 0;
613         omap_irda_shutdown(si);
614 err_startup:
615         dma_free_coherent(NULL, IRDA_FRAME_SIZE_LIMIT,
616                         si->tx_buf_dma_virt, si->tx_buf_dma_phys);
617 err_mem1:
618         dma_free_coherent(NULL, IRDA_FRAME_SIZE_LIMIT,
619                         si->rx_buf_dma_virt, si->rx_buf_dma_phys);
620 err_irq:
621         free_irq(dev->irq, dev);
622         return err;
623 }
624
625 static int omap_irda_stop(struct net_device *dev)
626 {
627         struct omap_irda *si = dev->priv;
628
629         disable_irq(dev->irq);
630
631         netif_stop_queue(dev);
632
633         omap_free_dma(si->rx_dma_channel);
634         omap_free_dma(si->tx_dma_channel);
635
636         if (si->rx_buf_dma_virt)
637                 dma_free_coherent(NULL, IRDA_FRAME_SIZE_LIMIT,
638                                 si->rx_buf_dma_virt, si->rx_buf_dma_phys);
639         if (si->tx_buf_dma_virt)
640                 dma_free_coherent(NULL, IRDA_FRAME_SIZE_LIMIT,
641                                 si->tx_buf_dma_virt, si->tx_buf_dma_phys);
642
643         omap_irda_shutdown(si);
644
645         /* Stop IrLAP */
646         if (si->irlap) {
647                 irlap_close(si->irlap);
648                 si->irlap = NULL;
649         }
650
651         si->open = 0;
652
653         /*
654          * Free resources
655          */
656         free_irq(dev->irq, dev);
657
658         return 0;
659 }
660
661 static int omap_irda_set_speed(struct net_device *dev, int speed)
662 {
663         struct omap_irda *si = dev->priv;
664         int divisor;
665         unsigned long flags;
666
667         /* Set IrDA speed */
668         if (speed <= 115200) {
669
670                 local_irq_save(flags);
671
672                 /* SIR mode */
673                 if (si->pdata->transceiver_mode)
674                         si->pdata->transceiver_mode(si->dev, IR_SIRMODE);
675
676                 /* Set SIR mode */
677                 uart_reg_out(UART3_MDR1, 1);
678                 uart_reg_out(UART3_EBLR, 1);
679
680                 divisor = 48000000 / (16 * speed);      /* Base clock 48 MHz */
681
682                 uart_reg_out(UART3_LCR, UART3_LCR_DIVEN);
683                 uart_reg_out(UART3_DLL, (divisor & 0xff));
684                 uart_reg_out(UART3_DLH, (divisor >> 8));
685                 uart_reg_out(UART3_LCR, 0x03);
686
687                 uart_reg_out(UART3_MCR, 0);
688
689                 local_irq_restore(flags);
690         } else if (speed <= 1152000) {
691
692                 local_irq_save(flags);
693
694                 /* Set MIR mode, auto SIP */
695                 uart_reg_out(UART3_MDR1, UART3_MDR1_MIR |
696                                 UART3_MDR1_SIP_AUTO);
697
698                 uart_reg_out(UART3_EBLR, 2);
699
700                 divisor = 48000000 / (41 * speed);      /* Base clock 48 MHz */
701
702                 uart_reg_out(UART3_LCR, UART3_LCR_DIVEN);
703                 uart_reg_out(UART3_DLL, (divisor & 0xff));
704                 uart_reg_out(UART3_DLH, (divisor >> 8));
705                 uart_reg_out(UART3_LCR, 0x03);
706
707                 if (si->pdata->transceiver_mode)
708                         si->pdata->transceiver_mode(si->dev, IR_MIRMODE);
709
710                 local_irq_restore(flags);
711         } else {
712                 local_irq_save(flags);
713
714                 /* FIR mode */
715                 uart_reg_out(UART3_MDR1, UART3_MDR1_FIR |
716                                 UART3_MDR1_SIP_AUTO);
717
718                 if (si->pdata->transceiver_mode)
719                         si->pdata->transceiver_mode(si->dev, IR_FIRMODE);
720
721                 local_irq_restore(flags);
722         }
723
724         si->speed = speed;
725
726         return 0;
727 }
728
729 #ifdef CONFIG_PM
730 /*
731  * Suspend the IrDA interface.
732  */
733 static int omap_irda_suspend(struct platform_device *pdev, pm_message_t state)
734 {
735         struct net_device *dev = platform_get_drvdata(pdev);
736         struct omap_irda *si = dev->priv;
737
738         if (!dev)
739                 return 0;
740
741         if (si->open) {
742                 /*
743                  * Stop the transmit queue
744                  */
745                 netif_device_detach(dev);
746                 disable_irq(dev->irq);
747                 omap_irda_shutdown(si);
748         }
749         return 0;
750 }
751
752 /*
753  * Resume the IrDA interface.
754  */
755 static int omap_irda_resume(struct platform_device *pdev)
756 {
757         struct net_device *dev = platform_get_drvdata(pdev);
758         struct omap_irda *si= dev->priv;
759
760         if (!dev)
761                 return 0;
762
763         if (si->open) {
764                 /*
765                  * If we missed a speed change, initialise at the new speed
766                  * directly.  It is debatable whether this is actually
767                  * required, but in the interests of continuing from where
768                  * we left off it is desireable.  The converse argument is
769                  * that we should re-negotiate at 9600 baud again.
770                  */
771                 if (si->newspeed) {
772                         si->speed = si->newspeed;
773                         si->newspeed = 0;
774                 }
775
776                 omap_irda_startup(dev);
777                 omap_irda_set_speed(dev, si->speed);
778                 enable_irq(dev->irq);
779
780                 /*
781                  * This automatically wakes up the queue
782                  */
783                 netif_device_attach(dev);
784         }
785
786         return 0;
787 }
788 #else
789 #define omap_irda_suspend       NULL
790 #define omap_irda_resume        NULL
791 #endif
792
793 static int omap_irda_probe(struct platform_device *pdev)
794 {
795         struct net_device *dev;
796         struct omap_irda *si;
797         struct omap_irda_config *pdata = pdev->dev.platform_data;
798         unsigned int baudrate_mask;
799         int err = 0;
800         int irq = NO_IRQ;
801
802         if (!pdata) {
803                 printk(KERN_ERR "IrDA Platform data not supplied\n");
804                 return -ENOENT;
805         }
806
807         if (!pdata->rx_channel || !pdata->tx_channel) {
808                 printk(KERN_ERR "IrDA invalid rx/tx channel value\n");
809                 return -ENOENT;
810         }
811
812         irq = platform_get_irq(pdev, 0);
813         if (irq <= 0) {
814                 printk(KERN_WARNING "no irq for IrDA\n");
815                 return -ENOENT;
816         }
817
818         dev = alloc_irdadev(sizeof(struct omap_irda));
819         if (!dev)
820                 goto err_mem_1;
821
822
823         si = dev->priv;
824         si->dev = &pdev->dev;
825         si->pdata = pdata;
826
827         dev->hard_start_xmit    = omap_irda_hard_xmit;
828         dev->open               = omap_irda_start;
829         dev->stop               = omap_irda_stop;
830         dev->do_ioctl           = omap_irda_ioctl;
831         dev->get_stats          = omap_irda_stats;
832         dev->irq                = irq;
833
834         irda_init_max_qos_capabilies(&si->qos);
835
836         baudrate_mask = 0;
837         if (si->pdata->transceiver_cap & IR_SIRMODE)
838                 baudrate_mask |= IR_9600|IR_19200|IR_38400|IR_57600|IR_115200;
839         if (si->pdata->transceiver_cap & IR_MIRMODE)
840                 baudrate_mask |= IR_57600 | IR_1152000;
841         if (si->pdata->transceiver_cap & IR_FIRMODE)
842                 baudrate_mask |= IR_4000000 << 8;
843
844         si->qos.baud_rate.bits &= baudrate_mask;
845         si->qos.min_turn_time.bits = 7;
846
847         irda_qos_bits_to_value(&si->qos);
848
849         /* Any better way to avoid this? No. */
850         if (machine_is_omap_h3() || machine_is_omap_h4())
851                 INIT_WORK(&si->pdata->gpio_expa, NULL, NULL);
852
853         err = register_netdev(dev);
854         if (!err)
855                 platform_set_drvdata(pdev, dev);
856         else
857                 free_netdev(dev);
858
859 err_mem_1:
860         return err;
861 }
862
863 static int omap_irda_remove(struct platform_device *pdev)
864 {
865         struct net_device *dev = platform_get_drvdata(pdev);
866
867         if (pdev) {
868                 unregister_netdev(dev);
869                 free_netdev(dev);
870         }
871         return 0;
872 }
873
874 static struct platform_driver omapir_driver = {
875         .probe          = omap_irda_probe,
876         .remove         = omap_irda_remove,
877         .suspend        = omap_irda_suspend,
878         .resume         = omap_irda_resume,
879         .driver         = {
880                 .name   = "omapirda",
881         },
882 };
883
884 static char __initdata banner[] = KERN_INFO "OMAP IrDA driver initializing\n";
885
886 static int __init omap_irda_init(void)
887 {
888         printk(banner);
889         return platform_driver_register(&omapir_driver);
890 }
891
892 static void __exit omap_irda_exit(void)
893 {
894         platform_driver_unregister(&omapir_driver);
895 }
896
897 module_init(omap_irda_init);
898 module_exit(omap_irda_exit);
899
900 MODULE_AUTHOR("MontaVista");
901 MODULE_DESCRIPTION("OMAP IrDA Driver");
902 MODULE_LICENSE("GPL");
903