GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / media / rc / ite-cir.c
1 /*
2  * Driver for ITE Tech Inc. IT8712F/IT8512 CIR
3  *
4  * Copyright (C) 2010 Juan Jesús García de Soria <skandalfo@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * General Public License for more details.
15  *
16  * Inspired by the original lirc_it87 and lirc_ite8709 drivers, on top of the
17  * skeleton provided by the nuvoton-cir driver.
18  *
19  * The lirc_it87 driver was originally written by Hans-Gunter Lutke Uphues
20  * <hg_lu@web.de> in 2001, with enhancements by Christoph Bartelmus
21  * <lirc@bartelmus.de>, Andrew Calkin <r_tay@hotmail.com> and James Edwards
22  * <jimbo-lirc@edwardsclan.net>.
23  *
24  * The lirc_ite8709 driver was written by Grégory Lardière
25  * <spmf2004-lirc@yahoo.fr> in 2008.
26  */
27
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/pnp.h>
31 #include <linux/io.h>
32 #include <linux/interrupt.h>
33 #include <linux/sched.h>
34 #include <linux/delay.h>
35 #include <linux/slab.h>
36 #include <linux/input.h>
37 #include <linux/bitops.h>
38 #include <media/rc-core.h>
39 #include <linux/pci_ids.h>
40
41 #include "ite-cir.h"
42
43 /* module parameters */
44
45 /* debug level */
46 static int debug;
47 module_param(debug, int, S_IRUGO | S_IWUSR);
48 MODULE_PARM_DESC(debug, "Enable debugging output");
49
50 /* low limit for RX carrier freq, Hz, 0 for no RX demodulation */
51 static int rx_low_carrier_freq;
52 module_param(rx_low_carrier_freq, int, S_IRUGO | S_IWUSR);
53 MODULE_PARM_DESC(rx_low_carrier_freq, "Override low RX carrier frequency, Hz, 0 for no RX demodulation");
54
55 /* high limit for RX carrier freq, Hz, 0 for no RX demodulation */
56 static int rx_high_carrier_freq;
57 module_param(rx_high_carrier_freq, int, S_IRUGO | S_IWUSR);
58 MODULE_PARM_DESC(rx_high_carrier_freq, "Override high RX carrier frequency, Hz, 0 for no RX demodulation");
59
60 /* override tx carrier frequency */
61 static int tx_carrier_freq;
62 module_param(tx_carrier_freq, int, S_IRUGO | S_IWUSR);
63 MODULE_PARM_DESC(tx_carrier_freq, "Override TX carrier frequency, Hz");
64
65 /* override tx duty cycle */
66 static int tx_duty_cycle;
67 module_param(tx_duty_cycle, int, S_IRUGO | S_IWUSR);
68 MODULE_PARM_DESC(tx_duty_cycle, "Override TX duty cycle, 1-100");
69
70 /* override default sample period */
71 static long sample_period;
72 module_param(sample_period, long, S_IRUGO | S_IWUSR);
73 MODULE_PARM_DESC(sample_period, "Override carrier sample period, us");
74
75 /* override detected model id */
76 static int model_number = -1;
77 module_param(model_number, int, S_IRUGO | S_IWUSR);
78 MODULE_PARM_DESC(model_number, "Use this model number, don't autodetect");
79
80
81 /* HW-independent code functions */
82
83 /* check whether carrier frequency is high frequency */
84 static inline bool ite_is_high_carrier_freq(unsigned int freq)
85 {
86         return freq >= ITE_HCF_MIN_CARRIER_FREQ;
87 }
88
89 /* get the bits required to program the carrier frequency in CFQ bits,
90  * unshifted */
91 static u8 ite_get_carrier_freq_bits(unsigned int freq)
92 {
93         if (ite_is_high_carrier_freq(freq)) {
94                 if (freq < 425000)
95                         return ITE_CFQ_400;
96
97                 else if (freq < 465000)
98                         return ITE_CFQ_450;
99
100                 else if (freq < 490000)
101                         return ITE_CFQ_480;
102
103                 else
104                         return ITE_CFQ_500;
105         } else {
106                         /* trim to limits */
107                 if (freq < ITE_LCF_MIN_CARRIER_FREQ)
108                         freq = ITE_LCF_MIN_CARRIER_FREQ;
109                 if (freq > ITE_LCF_MAX_CARRIER_FREQ)
110                         freq = ITE_LCF_MAX_CARRIER_FREQ;
111
112                 /* convert to kHz and subtract the base freq */
113                 freq =
114                     DIV_ROUND_CLOSEST(freq - ITE_LCF_MIN_CARRIER_FREQ,
115                                       1000);
116
117                 return (u8) freq;
118         }
119 }
120
121 /* get the bits required to program the pulse with in TXMPW */
122 static u8 ite_get_pulse_width_bits(unsigned int freq, int duty_cycle)
123 {
124         unsigned long period_ns, on_ns;
125
126         /* sanitize freq into range */
127         if (freq < ITE_LCF_MIN_CARRIER_FREQ)
128                 freq = ITE_LCF_MIN_CARRIER_FREQ;
129         if (freq > ITE_HCF_MAX_CARRIER_FREQ)
130                 freq = ITE_HCF_MAX_CARRIER_FREQ;
131
132         period_ns = 1000000000UL / freq;
133         on_ns = period_ns * duty_cycle / 100;
134
135         if (ite_is_high_carrier_freq(freq)) {
136                 if (on_ns < 750)
137                         return ITE_TXMPW_A;
138
139                 else if (on_ns < 850)
140                         return ITE_TXMPW_B;
141
142                 else if (on_ns < 950)
143                         return ITE_TXMPW_C;
144
145                 else if (on_ns < 1080)
146                         return ITE_TXMPW_D;
147
148                 else
149                         return ITE_TXMPW_E;
150         } else {
151                 if (on_ns < 6500)
152                         return ITE_TXMPW_A;
153
154                 else if (on_ns < 7850)
155                         return ITE_TXMPW_B;
156
157                 else if (on_ns < 9650)
158                         return ITE_TXMPW_C;
159
160                 else if (on_ns < 11950)
161                         return ITE_TXMPW_D;
162
163                 else
164                         return ITE_TXMPW_E;
165         }
166 }
167
168 /* decode raw bytes as received by the hardware, and push them to the ir-core
169  * layer */
170 static void ite_decode_bytes(struct ite_dev *dev, const u8 * data, int
171                              length)
172 {
173         u32 sample_period;
174         unsigned long *ldata;
175         unsigned int next_one, next_zero, size;
176         DEFINE_IR_RAW_EVENT(ev);
177
178         if (length == 0)
179                 return;
180
181         sample_period = dev->params.sample_period;
182         ldata = (unsigned long *)data;
183         size = length << 3;
184         next_one = find_next_bit_le(ldata, size, 0);
185         if (next_one > 0) {
186                 ev.pulse = true;
187                 ev.duration =
188                     ITE_BITS_TO_NS(next_one, sample_period);
189                 ir_raw_event_store_with_filter(dev->rdev, &ev);
190         }
191
192         while (next_one < size) {
193                 next_zero = find_next_zero_bit_le(ldata, size, next_one + 1);
194                 ev.pulse = false;
195                 ev.duration = ITE_BITS_TO_NS(next_zero - next_one, sample_period);
196                 ir_raw_event_store_with_filter(dev->rdev, &ev);
197
198                 if (next_zero < size) {
199                         next_one =
200                             find_next_bit_le(ldata,
201                                                      size,
202                                                      next_zero + 1);
203                         ev.pulse = true;
204                         ev.duration =
205                             ITE_BITS_TO_NS(next_one - next_zero,
206                                            sample_period);
207                         ir_raw_event_store_with_filter
208                             (dev->rdev, &ev);
209                 } else
210                         next_one = size;
211         }
212
213         ir_raw_event_handle(dev->rdev);
214
215         ite_dbg_verbose("decoded %d bytes.", length);
216 }
217
218 /* set all the rx/tx carrier parameters; this must be called with the device
219  * spinlock held */
220 static void ite_set_carrier_params(struct ite_dev *dev)
221 {
222         unsigned int freq, low_freq, high_freq;
223         int allowance;
224         bool use_demodulator;
225         bool for_tx = dev->transmitting;
226
227         ite_dbg("%s called", __func__);
228
229         if (for_tx) {
230                 /* we don't need no stinking calculations */
231                 freq = dev->params.tx_carrier_freq;
232                 allowance = ITE_RXDCR_DEFAULT;
233                 use_demodulator = false;
234         } else {
235                 low_freq = dev->params.rx_low_carrier_freq;
236                 high_freq = dev->params.rx_high_carrier_freq;
237
238                 if (low_freq == 0) {
239                         /* don't demodulate */
240                         freq =
241                         ITE_DEFAULT_CARRIER_FREQ;
242                         allowance = ITE_RXDCR_DEFAULT;
243                         use_demodulator = false;
244                 } else {
245                         /* calculate the middle freq */
246                         freq = (low_freq + high_freq) / 2;
247
248                         /* calculate the allowance */
249                         allowance =
250                             DIV_ROUND_CLOSEST(10000 * (high_freq - low_freq),
251                                               ITE_RXDCR_PER_10000_STEP
252                                               * (high_freq + low_freq));
253
254                         if (allowance < 1)
255                                 allowance = 1;
256
257                         if (allowance > ITE_RXDCR_MAX)
258                                 allowance = ITE_RXDCR_MAX;
259
260                         use_demodulator = true;
261                 }
262         }
263
264         /* set the carrier parameters in a device-dependent way */
265         dev->params.set_carrier_params(dev, ite_is_high_carrier_freq(freq),
266                  use_demodulator, ite_get_carrier_freq_bits(freq), allowance,
267                  ite_get_pulse_width_bits(freq, dev->params.tx_duty_cycle));
268 }
269
270 /* interrupt service routine for incoming and outgoing CIR data */
271 static irqreturn_t ite_cir_isr(int irq, void *data)
272 {
273         struct ite_dev *dev = data;
274         unsigned long flags;
275         irqreturn_t ret = IRQ_RETVAL(IRQ_NONE);
276         u8 rx_buf[ITE_RX_FIFO_LEN];
277         int rx_bytes;
278         int iflags;
279
280         ite_dbg_verbose("%s firing", __func__);
281
282         /* grab the spinlock */
283         spin_lock_irqsave(&dev->lock, flags);
284
285         /* read the interrupt flags */
286         iflags = dev->params.get_irq_causes(dev);
287
288         /* Check for RX overflow */
289         if (iflags & ITE_IRQ_RX_FIFO_OVERRUN) {
290                 dev_warn(&dev->rdev->dev, "receive overflow\n");
291                 ir_raw_event_reset(dev->rdev);
292         }
293
294         /* check for the receive interrupt */
295         if (iflags & (ITE_IRQ_RX_FIFO | ITE_IRQ_RX_FIFO_OVERRUN)) {
296                 /* read the FIFO bytes */
297                 rx_bytes =
298                         dev->params.get_rx_bytes(dev, rx_buf,
299                                              ITE_RX_FIFO_LEN);
300
301                 if (rx_bytes > 0) {
302                         /* drop the spinlock, since the ir-core layer
303                          * may call us back again through
304                          * ite_s_idle() */
305                         spin_unlock_irqrestore(&dev->
306                                                                          lock,
307                                                                          flags);
308
309                         /* decode the data we've just received */
310                         ite_decode_bytes(dev, rx_buf,
311                                                                    rx_bytes);
312
313                         /* reacquire the spinlock */
314                         spin_lock_irqsave(&dev->lock,
315                                                                     flags);
316
317                         /* mark the interrupt as serviced */
318                         ret = IRQ_RETVAL(IRQ_HANDLED);
319                 }
320         } else if (iflags & ITE_IRQ_TX_FIFO) {
321                 /* FIFO space available interrupt */
322                 ite_dbg_verbose("got interrupt for TX FIFO");
323
324                 /* wake any sleeping transmitter */
325                 wake_up_interruptible(&dev->tx_queue);
326
327                 /* mark the interrupt as serviced */
328                 ret = IRQ_RETVAL(IRQ_HANDLED);
329         }
330
331         /* drop the spinlock */
332         spin_unlock_irqrestore(&dev->lock, flags);
333
334         ite_dbg_verbose("%s done returning %d", __func__, (int)ret);
335
336         return ret;
337 }
338
339 /* set the rx carrier freq range, guess it's in Hz... */
340 static int ite_set_rx_carrier_range(struct rc_dev *rcdev, u32 carrier_low, u32
341                                     carrier_high)
342 {
343         unsigned long flags;
344         struct ite_dev *dev = rcdev->priv;
345
346         spin_lock_irqsave(&dev->lock, flags);
347         dev->params.rx_low_carrier_freq = carrier_low;
348         dev->params.rx_high_carrier_freq = carrier_high;
349         ite_set_carrier_params(dev);
350         spin_unlock_irqrestore(&dev->lock, flags);
351
352         return 0;
353 }
354
355 /* set the tx carrier freq, guess it's in Hz... */
356 static int ite_set_tx_carrier(struct rc_dev *rcdev, u32 carrier)
357 {
358         unsigned long flags;
359         struct ite_dev *dev = rcdev->priv;
360
361         spin_lock_irqsave(&dev->lock, flags);
362         dev->params.tx_carrier_freq = carrier;
363         ite_set_carrier_params(dev);
364         spin_unlock_irqrestore(&dev->lock, flags);
365
366         return 0;
367 }
368
369 /* set the tx duty cycle by controlling the pulse width */
370 static int ite_set_tx_duty_cycle(struct rc_dev *rcdev, u32 duty_cycle)
371 {
372         unsigned long flags;
373         struct ite_dev *dev = rcdev->priv;
374
375         spin_lock_irqsave(&dev->lock, flags);
376         dev->params.tx_duty_cycle = duty_cycle;
377         ite_set_carrier_params(dev);
378         spin_unlock_irqrestore(&dev->lock, flags);
379
380         return 0;
381 }
382
383 /* transmit out IR pulses; what you get here is a batch of alternating
384  * pulse/space/pulse/space lengths that we should write out completely through
385  * the FIFO, blocking on a full FIFO */
386 static int ite_tx_ir(struct rc_dev *rcdev, unsigned *txbuf, unsigned n)
387 {
388         unsigned long flags;
389         struct ite_dev *dev = rcdev->priv;
390         bool is_pulse = false;
391         int remaining_us, fifo_avail, fifo_remaining, last_idx = 0;
392         int max_rle_us, next_rle_us;
393         int ret = n;
394         u8 last_sent[ITE_TX_FIFO_LEN];
395         u8 val;
396
397         ite_dbg("%s called", __func__);
398
399         /* clear the array just in case */
400         memset(last_sent, 0, ARRAY_SIZE(last_sent));
401
402         spin_lock_irqsave(&dev->lock, flags);
403
404         /* let everybody know we're now transmitting */
405         dev->transmitting = true;
406
407         /* and set the carrier values for transmission */
408         ite_set_carrier_params(dev);
409
410         /* calculate how much time we can send in one byte */
411         max_rle_us =
412             (ITE_BAUDRATE_DIVISOR * dev->params.sample_period *
413              ITE_TX_MAX_RLE) / 1000;
414
415         /* disable the receiver */
416         dev->params.disable_rx(dev);
417
418         /* this is where we'll begin filling in the FIFO, until it's full.
419          * then we'll just activate the interrupt, wait for it to wake us up
420          * again, disable it, continue filling the FIFO... until everything
421          * has been pushed out */
422         fifo_avail =
423             ITE_TX_FIFO_LEN - dev->params.get_tx_used_slots(dev);
424
425         while (n > 0 && dev->in_use) {
426                 /* transmit the next sample */
427                 is_pulse = !is_pulse;
428                 remaining_us = *(txbuf++);
429                 n--;
430
431                 ite_dbg("%s: %ld",
432                                       ((is_pulse) ? "pulse" : "space"),
433                                       (long int)
434                                       remaining_us);
435
436                 /* repeat while the pulse is non-zero length */
437                 while (remaining_us > 0 && dev->in_use) {
438                         if (remaining_us > max_rle_us)
439                                 next_rle_us = max_rle_us;
440
441                         else
442                                 next_rle_us = remaining_us;
443
444                         remaining_us -= next_rle_us;
445
446                         /* check what's the length we have to pump out */
447                         val = (ITE_TX_MAX_RLE * next_rle_us) / max_rle_us;
448
449                         /* put it into the sent buffer */
450                         last_sent[last_idx++] = val;
451                         last_idx &= (ITE_TX_FIFO_LEN);
452
453                         /* encode it for 7 bits */
454                         val = (val - 1) & ITE_TX_RLE_MASK;
455
456                         /* take into account pulse/space prefix */
457                         if (is_pulse)
458                                 val |= ITE_TX_PULSE;
459
460                         else
461                                 val |= ITE_TX_SPACE;
462
463                         /*
464                          * if we get to 0 available, read again, just in case
465                          * some other slot got freed
466                          */
467                         if (fifo_avail <= 0)
468                                 fifo_avail = ITE_TX_FIFO_LEN - dev->params.get_tx_used_slots(dev);
469
470                         /* if it's still full */
471                         if (fifo_avail <= 0) {
472                                 /* enable the tx interrupt */
473                                 dev->params.
474                                 enable_tx_interrupt(dev);
475
476                                 /* drop the spinlock */
477                                 spin_unlock_irqrestore(&dev->lock, flags);
478
479                                 /* wait for the FIFO to empty enough */
480                                 wait_event_interruptible(dev->tx_queue, (fifo_avail = ITE_TX_FIFO_LEN - dev->params.get_tx_used_slots(dev)) >= 8);
481
482                                 /* get the spinlock again */
483                                 spin_lock_irqsave(&dev->lock, flags);
484
485                                 /* disable the tx interrupt again. */
486                                 dev->params.
487                                 disable_tx_interrupt(dev);
488                         }
489
490                         /* now send the byte through the FIFO */
491                         dev->params.put_tx_byte(dev, val);
492                         fifo_avail--;
493                 }
494         }
495
496         /* wait and don't return until the whole FIFO has been sent out;
497          * otherwise we could configure the RX carrier params instead of the
498          * TX ones while the transmission is still being performed! */
499         fifo_remaining = dev->params.get_tx_used_slots(dev);
500         remaining_us = 0;
501         while (fifo_remaining > 0) {
502                 fifo_remaining--;
503                 last_idx--;
504                 last_idx &= (ITE_TX_FIFO_LEN - 1);
505                 remaining_us += last_sent[last_idx];
506         }
507         remaining_us = (remaining_us * max_rle_us) / (ITE_TX_MAX_RLE);
508
509         /* drop the spinlock while we sleep */
510         spin_unlock_irqrestore(&dev->lock, flags);
511
512         /* sleep remaining_us microseconds */
513         mdelay(DIV_ROUND_UP(remaining_us, 1000));
514
515         /* reacquire the spinlock */
516         spin_lock_irqsave(&dev->lock, flags);
517
518         /* now we're not transmitting anymore */
519         dev->transmitting = false;
520
521         /* and set the carrier values for reception */
522         ite_set_carrier_params(dev);
523
524         /* reenable the receiver */
525         if (dev->in_use)
526                 dev->params.enable_rx(dev);
527
528         /* notify transmission end */
529         wake_up_interruptible(&dev->tx_ended);
530
531         spin_unlock_irqrestore(&dev->lock, flags);
532
533         return ret;
534 }
535
536 /* idle the receiver if needed */
537 static void ite_s_idle(struct rc_dev *rcdev, bool enable)
538 {
539         unsigned long flags;
540         struct ite_dev *dev = rcdev->priv;
541
542         ite_dbg("%s called", __func__);
543
544         if (enable) {
545                 spin_lock_irqsave(&dev->lock, flags);
546                 dev->params.idle_rx(dev);
547                 spin_unlock_irqrestore(&dev->lock, flags);
548         }
549 }
550
551
552 /* IT8712F HW-specific functions */
553
554 /* retrieve a bitmask of the current causes for a pending interrupt; this may
555  * be composed of ITE_IRQ_TX_FIFO, ITE_IRQ_RX_FIFO and ITE_IRQ_RX_FIFO_OVERRUN
556  * */
557 static int it87_get_irq_causes(struct ite_dev *dev)
558 {
559         u8 iflags;
560         int ret = 0;
561
562         ite_dbg("%s called", __func__);
563
564         /* read the interrupt flags */
565         iflags = inb(dev->cir_addr + IT87_IIR) & IT87_II;
566
567         switch (iflags) {
568         case IT87_II_RXDS:
569                 ret = ITE_IRQ_RX_FIFO;
570                 break;
571         case IT87_II_RXFO:
572                 ret = ITE_IRQ_RX_FIFO_OVERRUN;
573                 break;
574         case IT87_II_TXLDL:
575                 ret = ITE_IRQ_TX_FIFO;
576                 break;
577         }
578
579         return ret;
580 }
581
582 /* set the carrier parameters; to be called with the spinlock held */
583 static void it87_set_carrier_params(struct ite_dev *dev, bool high_freq,
584                                     bool use_demodulator,
585                                     u8 carrier_freq_bits, u8 allowance_bits,
586                                     u8 pulse_width_bits)
587 {
588         u8 val;
589
590         ite_dbg("%s called", __func__);
591
592         /* program the RCR register */
593         val = inb(dev->cir_addr + IT87_RCR)
594                 & ~(IT87_HCFS | IT87_RXEND | IT87_RXDCR);
595
596         if (high_freq)
597                 val |= IT87_HCFS;
598
599         if (use_demodulator)
600                 val |= IT87_RXEND;
601
602         val |= allowance_bits;
603
604         outb(val, dev->cir_addr + IT87_RCR);
605
606         /* program the TCR2 register */
607         outb((carrier_freq_bits << IT87_CFQ_SHIFT) | pulse_width_bits,
608                 dev->cir_addr + IT87_TCR2);
609 }
610
611 /* read up to buf_size bytes from the RX FIFO; to be called with the spinlock
612  * held */
613 static int it87_get_rx_bytes(struct ite_dev *dev, u8 * buf, int buf_size)
614 {
615         int fifo, read = 0;
616
617         ite_dbg("%s called", __func__);
618
619         /* read how many bytes are still in the FIFO */
620         fifo = inb(dev->cir_addr + IT87_RSR) & IT87_RXFBC;
621
622         while (fifo > 0 && buf_size > 0) {
623                 *(buf++) = inb(dev->cir_addr + IT87_DR);
624                 fifo--;
625                 read++;
626                 buf_size--;
627         }
628
629         return read;
630 }
631
632 /* return how many bytes are still in the FIFO; this will be called
633  * with the device spinlock NOT HELD while waiting for the TX FIFO to get
634  * empty; let's expect this won't be a problem */
635 static int it87_get_tx_used_slots(struct ite_dev *dev)
636 {
637         ite_dbg("%s called", __func__);
638
639         return inb(dev->cir_addr + IT87_TSR) & IT87_TXFBC;
640 }
641
642 /* put a byte to the TX fifo; this should be called with the spinlock held */
643 static void it87_put_tx_byte(struct ite_dev *dev, u8 value)
644 {
645         outb(value, dev->cir_addr + IT87_DR);
646 }
647
648 /* idle the receiver so that we won't receive samples until another
649   pulse is detected; this must be called with the device spinlock held */
650 static void it87_idle_rx(struct ite_dev *dev)
651 {
652         ite_dbg("%s called", __func__);
653
654         /* disable streaming by clearing RXACT writing it as 1 */
655         outb(inb(dev->cir_addr + IT87_RCR) | IT87_RXACT,
656                 dev->cir_addr + IT87_RCR);
657
658         /* clear the FIFO */
659         outb(inb(dev->cir_addr + IT87_TCR1) | IT87_FIFOCLR,
660                 dev->cir_addr + IT87_TCR1);
661 }
662
663 /* disable the receiver; this must be called with the device spinlock held */
664 static void it87_disable_rx(struct ite_dev *dev)
665 {
666         ite_dbg("%s called", __func__);
667
668         /* disable the receiver interrupts */
669         outb(inb(dev->cir_addr + IT87_IER) & ~(IT87_RDAIE | IT87_RFOIE),
670                 dev->cir_addr + IT87_IER);
671
672         /* disable the receiver */
673         outb(inb(dev->cir_addr + IT87_RCR) & ~IT87_RXEN,
674                 dev->cir_addr + IT87_RCR);
675
676         /* clear the FIFO and RXACT (actually RXACT should have been cleared
677         * in the previous outb() call) */
678         it87_idle_rx(dev);
679 }
680
681 /* enable the receiver; this must be called with the device spinlock held */
682 static void it87_enable_rx(struct ite_dev *dev)
683 {
684         ite_dbg("%s called", __func__);
685
686         /* enable the receiver by setting RXEN */
687         outb(inb(dev->cir_addr + IT87_RCR) | IT87_RXEN,
688                 dev->cir_addr + IT87_RCR);
689
690         /* just prepare it to idle for the next reception */
691         it87_idle_rx(dev);
692
693         /* enable the receiver interrupts and master enable flag */
694         outb(inb(dev->cir_addr + IT87_IER) | IT87_RDAIE | IT87_RFOIE | IT87_IEC,
695                 dev->cir_addr + IT87_IER);
696 }
697
698 /* disable the transmitter interrupt; this must be called with the device
699  * spinlock held */
700 static void it87_disable_tx_interrupt(struct ite_dev *dev)
701 {
702         ite_dbg("%s called", __func__);
703
704         /* disable the transmitter interrupts */
705         outb(inb(dev->cir_addr + IT87_IER) & ~IT87_TLDLIE,
706                 dev->cir_addr + IT87_IER);
707 }
708
709 /* enable the transmitter interrupt; this must be called with the device
710  * spinlock held */
711 static void it87_enable_tx_interrupt(struct ite_dev *dev)
712 {
713         ite_dbg("%s called", __func__);
714
715         /* enable the transmitter interrupts and master enable flag */
716         outb(inb(dev->cir_addr + IT87_IER) | IT87_TLDLIE | IT87_IEC,
717                 dev->cir_addr + IT87_IER);
718 }
719
720 /* disable the device; this must be called with the device spinlock held */
721 static void it87_disable(struct ite_dev *dev)
722 {
723         ite_dbg("%s called", __func__);
724
725         /* clear out all interrupt enable flags */
726         outb(inb(dev->cir_addr + IT87_IER) &
727                 ~(IT87_IEC | IT87_RFOIE | IT87_RDAIE | IT87_TLDLIE),
728                 dev->cir_addr + IT87_IER);
729
730         /* disable the receiver */
731         it87_disable_rx(dev);
732
733         /* erase the FIFO */
734         outb(IT87_FIFOCLR | inb(dev->cir_addr + IT87_TCR1),
735                 dev->cir_addr + IT87_TCR1);
736 }
737
738 /* initialize the hardware */
739 static void it87_init_hardware(struct ite_dev *dev)
740 {
741         ite_dbg("%s called", __func__);
742
743         /* enable just the baud rate divisor register,
744         disabling all the interrupts at the same time */
745         outb((inb(dev->cir_addr + IT87_IER) &
746                 ~(IT87_IEC | IT87_RFOIE | IT87_RDAIE | IT87_TLDLIE)) | IT87_BR,
747                 dev->cir_addr + IT87_IER);
748
749         /* write out the baud rate divisor */
750         outb(ITE_BAUDRATE_DIVISOR & 0xff, dev->cir_addr + IT87_BDLR);
751         outb((ITE_BAUDRATE_DIVISOR >> 8) & 0xff, dev->cir_addr + IT87_BDHR);
752
753         /* disable the baud rate divisor register again */
754         outb(inb(dev->cir_addr + IT87_IER) & ~IT87_BR,
755                 dev->cir_addr + IT87_IER);
756
757         /* program the RCR register defaults */
758         outb(ITE_RXDCR_DEFAULT, dev->cir_addr + IT87_RCR);
759
760         /* program the TCR1 register */
761         outb(IT87_TXMPM_DEFAULT | IT87_TXENDF | IT87_TXRLE
762                 | IT87_FIFOTL_DEFAULT | IT87_FIFOCLR,
763                 dev->cir_addr + IT87_TCR1);
764
765         /* program the carrier parameters */
766         ite_set_carrier_params(dev);
767 }
768
769 /* IT8512F on ITE8708 HW-specific functions */
770
771 /* retrieve a bitmask of the current causes for a pending interrupt; this may
772  * be composed of ITE_IRQ_TX_FIFO, ITE_IRQ_RX_FIFO and ITE_IRQ_RX_FIFO_OVERRUN
773  * */
774 static int it8708_get_irq_causes(struct ite_dev *dev)
775 {
776         u8 iflags;
777         int ret = 0;
778
779         ite_dbg("%s called", __func__);
780
781         /* read the interrupt flags */
782         iflags = inb(dev->cir_addr + IT8708_C0IIR);
783
784         if (iflags & IT85_TLDLI)
785                 ret |= ITE_IRQ_TX_FIFO;
786         if (iflags & IT85_RDAI)
787                 ret |= ITE_IRQ_RX_FIFO;
788         if (iflags & IT85_RFOI)
789                 ret |= ITE_IRQ_RX_FIFO_OVERRUN;
790
791         return ret;
792 }
793
794 /* set the carrier parameters; to be called with the spinlock held */
795 static void it8708_set_carrier_params(struct ite_dev *dev, bool high_freq,
796                                       bool use_demodulator,
797                                       u8 carrier_freq_bits, u8 allowance_bits,
798                                       u8 pulse_width_bits)
799 {
800         u8 val;
801
802         ite_dbg("%s called", __func__);
803
804         /* program the C0CFR register, with HRAE=1 */
805         outb(inb(dev->cir_addr + IT8708_BANKSEL) | IT8708_HRAE,
806                 dev->cir_addr + IT8708_BANKSEL);
807
808         val = (inb(dev->cir_addr + IT8708_C0CFR)
809                 & ~(IT85_HCFS | IT85_CFQ)) | carrier_freq_bits;
810
811         if (high_freq)
812                 val |= IT85_HCFS;
813
814         outb(val, dev->cir_addr + IT8708_C0CFR);
815
816         outb(inb(dev->cir_addr + IT8708_BANKSEL) & ~IT8708_HRAE,
817                    dev->cir_addr + IT8708_BANKSEL);
818
819         /* program the C0RCR register */
820         val = inb(dev->cir_addr + IT8708_C0RCR)
821                 & ~(IT85_RXEND | IT85_RXDCR);
822
823         if (use_demodulator)
824                 val |= IT85_RXEND;
825
826         val |= allowance_bits;
827
828         outb(val, dev->cir_addr + IT8708_C0RCR);
829
830         /* program the C0TCR register */
831         val = inb(dev->cir_addr + IT8708_C0TCR) & ~IT85_TXMPW;
832         val |= pulse_width_bits;
833         outb(val, dev->cir_addr + IT8708_C0TCR);
834 }
835
836 /* read up to buf_size bytes from the RX FIFO; to be called with the spinlock
837  * held */
838 static int it8708_get_rx_bytes(struct ite_dev *dev, u8 * buf, int buf_size)
839 {
840         int fifo, read = 0;
841
842         ite_dbg("%s called", __func__);
843
844         /* read how many bytes are still in the FIFO */
845         fifo = inb(dev->cir_addr + IT8708_C0RFSR) & IT85_RXFBC;
846
847         while (fifo > 0 && buf_size > 0) {
848                 *(buf++) = inb(dev->cir_addr + IT8708_C0DR);
849                 fifo--;
850                 read++;
851                 buf_size--;
852         }
853
854         return read;
855 }
856
857 /* return how many bytes are still in the FIFO; this will be called
858  * with the device spinlock NOT HELD while waiting for the TX FIFO to get
859  * empty; let's expect this won't be a problem */
860 static int it8708_get_tx_used_slots(struct ite_dev *dev)
861 {
862         ite_dbg("%s called", __func__);
863
864         return inb(dev->cir_addr + IT8708_C0TFSR) & IT85_TXFBC;
865 }
866
867 /* put a byte to the TX fifo; this should be called with the spinlock held */
868 static void it8708_put_tx_byte(struct ite_dev *dev, u8 value)
869 {
870         outb(value, dev->cir_addr + IT8708_C0DR);
871 }
872
873 /* idle the receiver so that we won't receive samples until another
874   pulse is detected; this must be called with the device spinlock held */
875 static void it8708_idle_rx(struct ite_dev *dev)
876 {
877         ite_dbg("%s called", __func__);
878
879         /* disable streaming by clearing RXACT writing it as 1 */
880         outb(inb(dev->cir_addr + IT8708_C0RCR) | IT85_RXACT,
881                 dev->cir_addr + IT8708_C0RCR);
882
883         /* clear the FIFO */
884         outb(inb(dev->cir_addr + IT8708_C0MSTCR) | IT85_FIFOCLR,
885                 dev->cir_addr + IT8708_C0MSTCR);
886 }
887
888 /* disable the receiver; this must be called with the device spinlock held */
889 static void it8708_disable_rx(struct ite_dev *dev)
890 {
891         ite_dbg("%s called", __func__);
892
893         /* disable the receiver interrupts */
894         outb(inb(dev->cir_addr + IT8708_C0IER) &
895                 ~(IT85_RDAIE | IT85_RFOIE),
896                 dev->cir_addr + IT8708_C0IER);
897
898         /* disable the receiver */
899         outb(inb(dev->cir_addr + IT8708_C0RCR) & ~IT85_RXEN,
900                 dev->cir_addr + IT8708_C0RCR);
901
902         /* clear the FIFO and RXACT (actually RXACT should have been cleared
903          * in the previous outb() call) */
904         it8708_idle_rx(dev);
905 }
906
907 /* enable the receiver; this must be called with the device spinlock held */
908 static void it8708_enable_rx(struct ite_dev *dev)
909 {
910         ite_dbg("%s called", __func__);
911
912         /* enable the receiver by setting RXEN */
913         outb(inb(dev->cir_addr + IT8708_C0RCR) | IT85_RXEN,
914                 dev->cir_addr + IT8708_C0RCR);
915
916         /* just prepare it to idle for the next reception */
917         it8708_idle_rx(dev);
918
919         /* enable the receiver interrupts and master enable flag */
920         outb(inb(dev->cir_addr + IT8708_C0IER)
921                 |IT85_RDAIE | IT85_RFOIE | IT85_IEC,
922                 dev->cir_addr + IT8708_C0IER);
923 }
924
925 /* disable the transmitter interrupt; this must be called with the device
926  * spinlock held */
927 static void it8708_disable_tx_interrupt(struct ite_dev *dev)
928 {
929         ite_dbg("%s called", __func__);
930
931         /* disable the transmitter interrupts */
932         outb(inb(dev->cir_addr + IT8708_C0IER) & ~IT85_TLDLIE,
933                 dev->cir_addr + IT8708_C0IER);
934 }
935
936 /* enable the transmitter interrupt; this must be called with the device
937  * spinlock held */
938 static void it8708_enable_tx_interrupt(struct ite_dev *dev)
939 {
940         ite_dbg("%s called", __func__);
941
942         /* enable the transmitter interrupts and master enable flag */
943         outb(inb(dev->cir_addr + IT8708_C0IER)
944                 |IT85_TLDLIE | IT85_IEC,
945                 dev->cir_addr + IT8708_C0IER);
946 }
947
948 /* disable the device; this must be called with the device spinlock held */
949 static void it8708_disable(struct ite_dev *dev)
950 {
951         ite_dbg("%s called", __func__);
952
953         /* clear out all interrupt enable flags */
954         outb(inb(dev->cir_addr + IT8708_C0IER) &
955                 ~(IT85_IEC | IT85_RFOIE | IT85_RDAIE | IT85_TLDLIE),
956                 dev->cir_addr + IT8708_C0IER);
957
958         /* disable the receiver */
959         it8708_disable_rx(dev);
960
961         /* erase the FIFO */
962         outb(IT85_FIFOCLR | inb(dev->cir_addr + IT8708_C0MSTCR),
963                 dev->cir_addr + IT8708_C0MSTCR);
964 }
965
966 /* initialize the hardware */
967 static void it8708_init_hardware(struct ite_dev *dev)
968 {
969         ite_dbg("%s called", __func__);
970
971         /* disable all the interrupts */
972         outb(inb(dev->cir_addr + IT8708_C0IER) &
973                 ~(IT85_IEC | IT85_RFOIE | IT85_RDAIE | IT85_TLDLIE),
974                 dev->cir_addr + IT8708_C0IER);
975
976         /* program the baud rate divisor */
977         outb(inb(dev->cir_addr + IT8708_BANKSEL) | IT8708_HRAE,
978                 dev->cir_addr + IT8708_BANKSEL);
979
980         outb(ITE_BAUDRATE_DIVISOR & 0xff, dev->cir_addr + IT8708_C0BDLR);
981         outb((ITE_BAUDRATE_DIVISOR >> 8) & 0xff,
982                    dev->cir_addr + IT8708_C0BDHR);
983
984         outb(inb(dev->cir_addr + IT8708_BANKSEL) & ~IT8708_HRAE,
985                    dev->cir_addr + IT8708_BANKSEL);
986
987         /* program the C0MSTCR register defaults */
988         outb((inb(dev->cir_addr + IT8708_C0MSTCR) &
989                         ~(IT85_ILSEL | IT85_ILE | IT85_FIFOTL |
990                           IT85_FIFOCLR | IT85_RESET)) |
991                        IT85_FIFOTL_DEFAULT,
992                        dev->cir_addr + IT8708_C0MSTCR);
993
994         /* program the C0RCR register defaults */
995         outb((inb(dev->cir_addr + IT8708_C0RCR) &
996                         ~(IT85_RXEN | IT85_RDWOS | IT85_RXEND |
997                           IT85_RXACT | IT85_RXDCR)) |
998                        ITE_RXDCR_DEFAULT,
999                        dev->cir_addr + IT8708_C0RCR);
1000
1001         /* program the C0TCR register defaults */
1002         outb((inb(dev->cir_addr + IT8708_C0TCR) &
1003                         ~(IT85_TXMPM | IT85_TXMPW))
1004                        |IT85_TXRLE | IT85_TXENDF |
1005                        IT85_TXMPM_DEFAULT | IT85_TXMPW_DEFAULT,
1006                        dev->cir_addr + IT8708_C0TCR);
1007
1008         /* program the carrier parameters */
1009         ite_set_carrier_params(dev);
1010 }
1011
1012 /* IT8512F on ITE8709 HW-specific functions */
1013
1014 /* read a byte from the SRAM module */
1015 static inline u8 it8709_rm(struct ite_dev *dev, int index)
1016 {
1017         outb(index, dev->cir_addr + IT8709_RAM_IDX);
1018         return inb(dev->cir_addr + IT8709_RAM_VAL);
1019 }
1020
1021 /* write a byte to the SRAM module */
1022 static inline void it8709_wm(struct ite_dev *dev, u8 val, int index)
1023 {
1024         outb(index, dev->cir_addr + IT8709_RAM_IDX);
1025         outb(val, dev->cir_addr + IT8709_RAM_VAL);
1026 }
1027
1028 static void it8709_wait(struct ite_dev *dev)
1029 {
1030         int i = 0;
1031         /*
1032          * loop until device tells it's ready to continue
1033          * iterations count is usually ~750 but can sometimes achieve 13000
1034          */
1035         for (i = 0; i < 15000; i++) {
1036                 udelay(2);
1037                 if (it8709_rm(dev, IT8709_MODE) == IT8709_IDLE)
1038                         break;
1039         }
1040 }
1041
1042 /* read the value of a CIR register */
1043 static u8 it8709_rr(struct ite_dev *dev, int index)
1044 {
1045         /* just wait in case the previous access was a write */
1046         it8709_wait(dev);
1047         it8709_wm(dev, index, IT8709_REG_IDX);
1048         it8709_wm(dev, IT8709_READ, IT8709_MODE);
1049
1050         /* wait for the read data to be available */
1051         it8709_wait(dev);
1052
1053         /* return the read value */
1054         return it8709_rm(dev, IT8709_REG_VAL);
1055 }
1056
1057 /* write the value of a CIR register */
1058 static void it8709_wr(struct ite_dev *dev, u8 val, int index)
1059 {
1060         /* we wait before writing, and not afterwards, since this allows us to
1061          * pipeline the host CPU with the microcontroller */
1062         it8709_wait(dev);
1063         it8709_wm(dev, val, IT8709_REG_VAL);
1064         it8709_wm(dev, index, IT8709_REG_IDX);
1065         it8709_wm(dev, IT8709_WRITE, IT8709_MODE);
1066 }
1067
1068 /* retrieve a bitmask of the current causes for a pending interrupt; this may
1069  * be composed of ITE_IRQ_TX_FIFO, ITE_IRQ_RX_FIFO and ITE_IRQ_RX_FIFO_OVERRUN
1070  * */
1071 static int it8709_get_irq_causes(struct ite_dev *dev)
1072 {
1073         u8 iflags;
1074         int ret = 0;
1075
1076         ite_dbg("%s called", __func__);
1077
1078         /* read the interrupt flags */
1079         iflags = it8709_rm(dev, IT8709_IIR);
1080
1081         if (iflags & IT85_TLDLI)
1082                 ret |= ITE_IRQ_TX_FIFO;
1083         if (iflags & IT85_RDAI)
1084                 ret |= ITE_IRQ_RX_FIFO;
1085         if (iflags & IT85_RFOI)
1086                 ret |= ITE_IRQ_RX_FIFO_OVERRUN;
1087
1088         return ret;
1089 }
1090
1091 /* set the carrier parameters; to be called with the spinlock held */
1092 static void it8709_set_carrier_params(struct ite_dev *dev, bool high_freq,
1093                                       bool use_demodulator,
1094                                       u8 carrier_freq_bits, u8 allowance_bits,
1095                                       u8 pulse_width_bits)
1096 {
1097         u8 val;
1098
1099         ite_dbg("%s called", __func__);
1100
1101         val = (it8709_rr(dev, IT85_C0CFR)
1102                      &~(IT85_HCFS | IT85_CFQ)) |
1103             carrier_freq_bits;
1104
1105         if (high_freq)
1106                 val |= IT85_HCFS;
1107
1108         it8709_wr(dev, val, IT85_C0CFR);
1109
1110         /* program the C0RCR register */
1111         val = it8709_rr(dev, IT85_C0RCR)
1112                 & ~(IT85_RXEND | IT85_RXDCR);
1113
1114         if (use_demodulator)
1115                 val |= IT85_RXEND;
1116
1117         val |= allowance_bits;
1118
1119         it8709_wr(dev, val, IT85_C0RCR);
1120
1121         /* program the C0TCR register */
1122         val = it8709_rr(dev, IT85_C0TCR) & ~IT85_TXMPW;
1123         val |= pulse_width_bits;
1124         it8709_wr(dev, val, IT85_C0TCR);
1125 }
1126
1127 /* read up to buf_size bytes from the RX FIFO; to be called with the spinlock
1128  * held */
1129 static int it8709_get_rx_bytes(struct ite_dev *dev, u8 * buf, int buf_size)
1130 {
1131         int fifo, read = 0;
1132
1133         ite_dbg("%s called", __func__);
1134
1135         /* read how many bytes are still in the FIFO */
1136         fifo = it8709_rm(dev, IT8709_RFSR) & IT85_RXFBC;
1137
1138         while (fifo > 0 && buf_size > 0) {
1139                 *(buf++) = it8709_rm(dev, IT8709_FIFO + read);
1140                 fifo--;
1141                 read++;
1142                 buf_size--;
1143         }
1144
1145         /* 'clear' the FIFO by setting the writing index to 0; this is
1146          * completely bound to be racy, but we can't help it, since it's a
1147          * limitation of the protocol */
1148         it8709_wm(dev, 0, IT8709_RFSR);
1149
1150         return read;
1151 }
1152
1153 /* return how many bytes are still in the FIFO; this will be called
1154  * with the device spinlock NOT HELD while waiting for the TX FIFO to get
1155  * empty; let's expect this won't be a problem */
1156 static int it8709_get_tx_used_slots(struct ite_dev *dev)
1157 {
1158         ite_dbg("%s called", __func__);
1159
1160         return it8709_rr(dev, IT85_C0TFSR) & IT85_TXFBC;
1161 }
1162
1163 /* put a byte to the TX fifo; this should be called with the spinlock held */
1164 static void it8709_put_tx_byte(struct ite_dev *dev, u8 value)
1165 {
1166         it8709_wr(dev, value, IT85_C0DR);
1167 }
1168
1169 /* idle the receiver so that we won't receive samples until another
1170   pulse is detected; this must be called with the device spinlock held */
1171 static void it8709_idle_rx(struct ite_dev *dev)
1172 {
1173         ite_dbg("%s called", __func__);
1174
1175         /* disable streaming by clearing RXACT writing it as 1 */
1176         it8709_wr(dev, it8709_rr(dev, IT85_C0RCR) | IT85_RXACT,
1177                             IT85_C0RCR);
1178
1179         /* clear the FIFO */
1180         it8709_wr(dev, it8709_rr(dev, IT85_C0MSTCR) | IT85_FIFOCLR,
1181                             IT85_C0MSTCR);
1182 }
1183
1184 /* disable the receiver; this must be called with the device spinlock held */
1185 static void it8709_disable_rx(struct ite_dev *dev)
1186 {
1187         ite_dbg("%s called", __func__);
1188
1189         /* disable the receiver interrupts */
1190         it8709_wr(dev, it8709_rr(dev, IT85_C0IER) &
1191                             ~(IT85_RDAIE | IT85_RFOIE),
1192                             IT85_C0IER);
1193
1194         /* disable the receiver */
1195         it8709_wr(dev, it8709_rr(dev, IT85_C0RCR) & ~IT85_RXEN,
1196                             IT85_C0RCR);
1197
1198         /* clear the FIFO and RXACT (actually RXACT should have been cleared
1199          * in the previous it8709_wr(dev, ) call) */
1200         it8709_idle_rx(dev);
1201 }
1202
1203 /* enable the receiver; this must be called with the device spinlock held */
1204 static void it8709_enable_rx(struct ite_dev *dev)
1205 {
1206         ite_dbg("%s called", __func__);
1207
1208         /* enable the receiver by setting RXEN */
1209         it8709_wr(dev, it8709_rr(dev, IT85_C0RCR) | IT85_RXEN,
1210                             IT85_C0RCR);
1211
1212         /* just prepare it to idle for the next reception */
1213         it8709_idle_rx(dev);
1214
1215         /* enable the receiver interrupts and master enable flag */
1216         it8709_wr(dev, it8709_rr(dev, IT85_C0IER)
1217                             |IT85_RDAIE | IT85_RFOIE | IT85_IEC,
1218                             IT85_C0IER);
1219 }
1220
1221 /* disable the transmitter interrupt; this must be called with the device
1222  * spinlock held */
1223 static void it8709_disable_tx_interrupt(struct ite_dev *dev)
1224 {
1225         ite_dbg("%s called", __func__);
1226
1227         /* disable the transmitter interrupts */
1228         it8709_wr(dev, it8709_rr(dev, IT85_C0IER) & ~IT85_TLDLIE,
1229                             IT85_C0IER);
1230 }
1231
1232 /* enable the transmitter interrupt; this must be called with the device
1233  * spinlock held */
1234 static void it8709_enable_tx_interrupt(struct ite_dev *dev)
1235 {
1236         ite_dbg("%s called", __func__);
1237
1238         /* enable the transmitter interrupts and master enable flag */
1239         it8709_wr(dev, it8709_rr(dev, IT85_C0IER)
1240                             |IT85_TLDLIE | IT85_IEC,
1241                             IT85_C0IER);
1242 }
1243
1244 /* disable the device; this must be called with the device spinlock held */
1245 static void it8709_disable(struct ite_dev *dev)
1246 {
1247         ite_dbg("%s called", __func__);
1248
1249         /* clear out all interrupt enable flags */
1250         it8709_wr(dev, it8709_rr(dev, IT85_C0IER) &
1251                         ~(IT85_IEC | IT85_RFOIE | IT85_RDAIE | IT85_TLDLIE),
1252                   IT85_C0IER);
1253
1254         /* disable the receiver */
1255         it8709_disable_rx(dev);
1256
1257         /* erase the FIFO */
1258         it8709_wr(dev, IT85_FIFOCLR | it8709_rr(dev, IT85_C0MSTCR),
1259                             IT85_C0MSTCR);
1260 }
1261
1262 /* initialize the hardware */
1263 static void it8709_init_hardware(struct ite_dev *dev)
1264 {
1265         ite_dbg("%s called", __func__);
1266
1267         /* disable all the interrupts */
1268         it8709_wr(dev, it8709_rr(dev, IT85_C0IER) &
1269                         ~(IT85_IEC | IT85_RFOIE | IT85_RDAIE | IT85_TLDLIE),
1270                   IT85_C0IER);
1271
1272         /* program the baud rate divisor */
1273         it8709_wr(dev, ITE_BAUDRATE_DIVISOR & 0xff, IT85_C0BDLR);
1274         it8709_wr(dev, (ITE_BAUDRATE_DIVISOR >> 8) & 0xff,
1275                         IT85_C0BDHR);
1276
1277         /* program the C0MSTCR register defaults */
1278         it8709_wr(dev, (it8709_rr(dev, IT85_C0MSTCR) &
1279                         ~(IT85_ILSEL | IT85_ILE | IT85_FIFOTL
1280                           | IT85_FIFOCLR | IT85_RESET)) | IT85_FIFOTL_DEFAULT,
1281                   IT85_C0MSTCR);
1282
1283         /* program the C0RCR register defaults */
1284         it8709_wr(dev, (it8709_rr(dev, IT85_C0RCR) &
1285                         ~(IT85_RXEN | IT85_RDWOS | IT85_RXEND | IT85_RXACT
1286                           | IT85_RXDCR)) | ITE_RXDCR_DEFAULT,
1287                   IT85_C0RCR);
1288
1289         /* program the C0TCR register defaults */
1290         it8709_wr(dev, (it8709_rr(dev, IT85_C0TCR) & ~(IT85_TXMPM | IT85_TXMPW))
1291                         | IT85_TXRLE | IT85_TXENDF | IT85_TXMPM_DEFAULT
1292                         | IT85_TXMPW_DEFAULT,
1293                   IT85_C0TCR);
1294
1295         /* program the carrier parameters */
1296         ite_set_carrier_params(dev);
1297 }
1298
1299
1300 /* generic hardware setup/teardown code */
1301
1302 /* activate the device for use */
1303 static int ite_open(struct rc_dev *rcdev)
1304 {
1305         struct ite_dev *dev = rcdev->priv;
1306         unsigned long flags;
1307
1308         ite_dbg("%s called", __func__);
1309
1310         spin_lock_irqsave(&dev->lock, flags);
1311         dev->in_use = true;
1312
1313         /* enable the receiver */
1314         dev->params.enable_rx(dev);
1315
1316         spin_unlock_irqrestore(&dev->lock, flags);
1317
1318         return 0;
1319 }
1320
1321 /* deactivate the device for use */
1322 static void ite_close(struct rc_dev *rcdev)
1323 {
1324         struct ite_dev *dev = rcdev->priv;
1325         unsigned long flags;
1326
1327         ite_dbg("%s called", __func__);
1328
1329         spin_lock_irqsave(&dev->lock, flags);
1330         dev->in_use = false;
1331
1332         /* wait for any transmission to end */
1333         spin_unlock_irqrestore(&dev->lock, flags);
1334         wait_event_interruptible(dev->tx_ended, !dev->transmitting);
1335         spin_lock_irqsave(&dev->lock, flags);
1336
1337         dev->params.disable(dev);
1338
1339         spin_unlock_irqrestore(&dev->lock, flags);
1340 }
1341
1342 /* supported models and their parameters */
1343 static const struct ite_dev_params ite_dev_descs[] = {
1344         {       /* 0: ITE8704 */
1345                .model = "ITE8704 CIR transceiver",
1346                .io_region_size = IT87_IOREG_LENGTH,
1347                .io_rsrc_no = 0,
1348                .hw_tx_capable = true,
1349                .sample_period = (u32) (1000000000ULL / 115200),
1350                .tx_carrier_freq = 38000,
1351                .tx_duty_cycle = 33,
1352                .rx_low_carrier_freq = 0,
1353                .rx_high_carrier_freq = 0,
1354
1355                 /* operations */
1356                .get_irq_causes = it87_get_irq_causes,
1357                .enable_rx = it87_enable_rx,
1358                .idle_rx = it87_idle_rx,
1359                .disable_rx = it87_idle_rx,
1360                .get_rx_bytes = it87_get_rx_bytes,
1361                .enable_tx_interrupt = it87_enable_tx_interrupt,
1362                .disable_tx_interrupt = it87_disable_tx_interrupt,
1363                .get_tx_used_slots = it87_get_tx_used_slots,
1364                .put_tx_byte = it87_put_tx_byte,
1365                .disable = it87_disable,
1366                .init_hardware = it87_init_hardware,
1367                .set_carrier_params = it87_set_carrier_params,
1368                },
1369         {       /* 1: ITE8713 */
1370                .model = "ITE8713 CIR transceiver",
1371                .io_region_size = IT87_IOREG_LENGTH,
1372                .io_rsrc_no = 0,
1373                .hw_tx_capable = true,
1374                .sample_period = (u32) (1000000000ULL / 115200),
1375                .tx_carrier_freq = 38000,
1376                .tx_duty_cycle = 33,
1377                .rx_low_carrier_freq = 0,
1378                .rx_high_carrier_freq = 0,
1379
1380                 /* operations */
1381                .get_irq_causes = it87_get_irq_causes,
1382                .enable_rx = it87_enable_rx,
1383                .idle_rx = it87_idle_rx,
1384                .disable_rx = it87_idle_rx,
1385                .get_rx_bytes = it87_get_rx_bytes,
1386                .enable_tx_interrupt = it87_enable_tx_interrupt,
1387                .disable_tx_interrupt = it87_disable_tx_interrupt,
1388                .get_tx_used_slots = it87_get_tx_used_slots,
1389                .put_tx_byte = it87_put_tx_byte,
1390                .disable = it87_disable,
1391                .init_hardware = it87_init_hardware,
1392                .set_carrier_params = it87_set_carrier_params,
1393                },
1394         {       /* 2: ITE8708 */
1395                .model = "ITE8708 CIR transceiver",
1396                .io_region_size = IT8708_IOREG_LENGTH,
1397                .io_rsrc_no = 0,
1398                .hw_tx_capable = true,
1399                .sample_period = (u32) (1000000000ULL / 115200),
1400                .tx_carrier_freq = 38000,
1401                .tx_duty_cycle = 33,
1402                .rx_low_carrier_freq = 0,
1403                .rx_high_carrier_freq = 0,
1404
1405                 /* operations */
1406                .get_irq_causes = it8708_get_irq_causes,
1407                .enable_rx = it8708_enable_rx,
1408                .idle_rx = it8708_idle_rx,
1409                .disable_rx = it8708_idle_rx,
1410                .get_rx_bytes = it8708_get_rx_bytes,
1411                .enable_tx_interrupt = it8708_enable_tx_interrupt,
1412                .disable_tx_interrupt =
1413                it8708_disable_tx_interrupt,
1414                .get_tx_used_slots = it8708_get_tx_used_slots,
1415                .put_tx_byte = it8708_put_tx_byte,
1416                .disable = it8708_disable,
1417                .init_hardware = it8708_init_hardware,
1418                .set_carrier_params = it8708_set_carrier_params,
1419                },
1420         {       /* 3: ITE8709 */
1421                .model = "ITE8709 CIR transceiver",
1422                .io_region_size = IT8709_IOREG_LENGTH,
1423                .io_rsrc_no = 2,
1424                .hw_tx_capable = true,
1425                .sample_period = (u32) (1000000000ULL / 115200),
1426                .tx_carrier_freq = 38000,
1427                .tx_duty_cycle = 33,
1428                .rx_low_carrier_freq = 0,
1429                .rx_high_carrier_freq = 0,
1430
1431                 /* operations */
1432                .get_irq_causes = it8709_get_irq_causes,
1433                .enable_rx = it8709_enable_rx,
1434                .idle_rx = it8709_idle_rx,
1435                .disable_rx = it8709_idle_rx,
1436                .get_rx_bytes = it8709_get_rx_bytes,
1437                .enable_tx_interrupt = it8709_enable_tx_interrupt,
1438                .disable_tx_interrupt =
1439                it8709_disable_tx_interrupt,
1440                .get_tx_used_slots = it8709_get_tx_used_slots,
1441                .put_tx_byte = it8709_put_tx_byte,
1442                .disable = it8709_disable,
1443                .init_hardware = it8709_init_hardware,
1444                .set_carrier_params = it8709_set_carrier_params,
1445                },
1446 };
1447
1448 static const struct pnp_device_id ite_ids[] = {
1449         {"ITE8704", 0},         /* Default model */
1450         {"ITE8713", 1},         /* CIR found in EEEBox 1501U */
1451         {"ITE8708", 2},         /* Bridged IT8512 */
1452         {"ITE8709", 3},         /* SRAM-Bridged IT8512 */
1453         {"", 0},
1454 };
1455
1456 /* allocate memory, probe hardware, and initialize everything */
1457 static int ite_probe(struct pnp_dev *pdev, const struct pnp_device_id
1458                      *dev_id)
1459 {
1460         const struct ite_dev_params *dev_desc = NULL;
1461         struct ite_dev *itdev = NULL;
1462         struct rc_dev *rdev = NULL;
1463         int ret = -ENOMEM;
1464         int model_no;
1465         int io_rsrc_no;
1466
1467         ite_dbg("%s called", __func__);
1468
1469         itdev = kzalloc(sizeof(struct ite_dev), GFP_KERNEL);
1470         if (!itdev)
1471                 return ret;
1472
1473         /* input device for IR remote (and tx) */
1474         rdev = rc_allocate_device(RC_DRIVER_IR_RAW);
1475         if (!rdev)
1476                 goto exit_free_dev_rdev;
1477         itdev->rdev = rdev;
1478
1479         ret = -ENODEV;
1480
1481         /* get the model number */
1482         model_no = (int)dev_id->driver_data;
1483         ite_pr(KERN_NOTICE, "Auto-detected model: %s\n",
1484                 ite_dev_descs[model_no].model);
1485
1486         if (model_number >= 0 && model_number < ARRAY_SIZE(ite_dev_descs)) {
1487                 model_no = model_number;
1488                 ite_pr(KERN_NOTICE, "The model has been fixed by a module parameter.");
1489         }
1490
1491         ite_pr(KERN_NOTICE, "Using model: %s\n", ite_dev_descs[model_no].model);
1492
1493         /* get the description for the device */
1494         dev_desc = &ite_dev_descs[model_no];
1495         io_rsrc_no = dev_desc->io_rsrc_no;
1496
1497         /* validate pnp resources */
1498         if (!pnp_port_valid(pdev, io_rsrc_no) ||
1499             pnp_port_len(pdev, io_rsrc_no) != dev_desc->io_region_size) {
1500                 dev_err(&pdev->dev, "IR PNP Port not valid!\n");
1501                 goto exit_free_dev_rdev;
1502         }
1503
1504         if (!pnp_irq_valid(pdev, 0)) {
1505                 dev_err(&pdev->dev, "PNP IRQ not valid!\n");
1506                 goto exit_free_dev_rdev;
1507         }
1508
1509         /* store resource values */
1510         itdev->cir_addr = pnp_port_start(pdev, io_rsrc_no);
1511         itdev->cir_irq = pnp_irq(pdev, 0);
1512
1513         /* initialize spinlocks */
1514         spin_lock_init(&itdev->lock);
1515
1516         /* initialize raw event */
1517         init_ir_raw_event(&itdev->rawir);
1518
1519         /* set driver data into the pnp device */
1520         pnp_set_drvdata(pdev, itdev);
1521         itdev->pdev = pdev;
1522
1523         /* initialize waitqueues for transmission */
1524         init_waitqueue_head(&itdev->tx_queue);
1525         init_waitqueue_head(&itdev->tx_ended);
1526
1527         /* copy model-specific parameters */
1528         itdev->params = *dev_desc;
1529
1530         /* apply any overrides */
1531         if (sample_period > 0)
1532                 itdev->params.sample_period = sample_period;
1533
1534         if (tx_carrier_freq > 0)
1535                 itdev->params.tx_carrier_freq = tx_carrier_freq;
1536
1537         if (tx_duty_cycle > 0 && tx_duty_cycle <= 100)
1538                 itdev->params.tx_duty_cycle = tx_duty_cycle;
1539
1540         if (rx_low_carrier_freq > 0)
1541                 itdev->params.rx_low_carrier_freq = rx_low_carrier_freq;
1542
1543         if (rx_high_carrier_freq > 0)
1544                 itdev->params.rx_high_carrier_freq = rx_high_carrier_freq;
1545
1546         /* print out parameters */
1547         ite_pr(KERN_NOTICE, "TX-capable: %d\n", (int)
1548                          itdev->params.hw_tx_capable);
1549         ite_pr(KERN_NOTICE, "Sample period (ns): %ld\n", (long)
1550                      itdev->params.sample_period);
1551         ite_pr(KERN_NOTICE, "TX carrier frequency (Hz): %d\n", (int)
1552                      itdev->params.tx_carrier_freq);
1553         ite_pr(KERN_NOTICE, "TX duty cycle (%%): %d\n", (int)
1554                      itdev->params.tx_duty_cycle);
1555         ite_pr(KERN_NOTICE, "RX low carrier frequency (Hz): %d\n", (int)
1556                      itdev->params.rx_low_carrier_freq);
1557         ite_pr(KERN_NOTICE, "RX high carrier frequency (Hz): %d\n", (int)
1558                      itdev->params.rx_high_carrier_freq);
1559
1560         /* set up hardware initial state */
1561         itdev->params.init_hardware(itdev);
1562
1563         /* set up ir-core props */
1564         rdev->priv = itdev;
1565         rdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
1566         rdev->open = ite_open;
1567         rdev->close = ite_close;
1568         rdev->s_idle = ite_s_idle;
1569         rdev->s_rx_carrier_range = ite_set_rx_carrier_range;
1570         rdev->min_timeout = ITE_MIN_IDLE_TIMEOUT;
1571         rdev->max_timeout = ITE_MAX_IDLE_TIMEOUT;
1572         rdev->timeout = ITE_IDLE_TIMEOUT;
1573         rdev->rx_resolution = ITE_BAUDRATE_DIVISOR *
1574                                 itdev->params.sample_period;
1575         rdev->tx_resolution = ITE_BAUDRATE_DIVISOR *
1576                                 itdev->params.sample_period;
1577
1578         /* set up transmitter related values if needed */
1579         if (itdev->params.hw_tx_capable) {
1580                 rdev->tx_ir = ite_tx_ir;
1581                 rdev->s_tx_carrier = ite_set_tx_carrier;
1582                 rdev->s_tx_duty_cycle = ite_set_tx_duty_cycle;
1583         }
1584
1585         rdev->device_name = dev_desc->model;
1586         rdev->input_id.bustype = BUS_HOST;
1587         rdev->input_id.vendor = PCI_VENDOR_ID_ITE;
1588         rdev->input_id.product = 0;
1589         rdev->input_id.version = 0;
1590         rdev->driver_name = ITE_DRIVER_NAME;
1591         rdev->map_name = RC_MAP_RC6_MCE;
1592
1593         ret = rc_register_device(rdev);
1594         if (ret)
1595                 goto exit_free_dev_rdev;
1596
1597         ret = -EBUSY;
1598         /* now claim resources */
1599         if (!request_region(itdev->cir_addr,
1600                                 dev_desc->io_region_size, ITE_DRIVER_NAME))
1601                 goto exit_unregister_device;
1602
1603         if (request_irq(itdev->cir_irq, ite_cir_isr, IRQF_SHARED,
1604                         ITE_DRIVER_NAME, (void *)itdev))
1605                 goto exit_release_cir_addr;
1606
1607         ite_pr(KERN_NOTICE, "driver has been successfully loaded\n");
1608
1609         return 0;
1610
1611 exit_release_cir_addr:
1612         release_region(itdev->cir_addr, itdev->params.io_region_size);
1613 exit_unregister_device:
1614         rc_unregister_device(rdev);
1615         rdev = NULL;
1616 exit_free_dev_rdev:
1617         rc_free_device(rdev);
1618         kfree(itdev);
1619
1620         return ret;
1621 }
1622
1623 static void ite_remove(struct pnp_dev *pdev)
1624 {
1625         struct ite_dev *dev = pnp_get_drvdata(pdev);
1626         unsigned long flags;
1627
1628         ite_dbg("%s called", __func__);
1629
1630         spin_lock_irqsave(&dev->lock, flags);
1631
1632         /* disable hardware */
1633         dev->params.disable(dev);
1634
1635         spin_unlock_irqrestore(&dev->lock, flags);
1636
1637         /* free resources */
1638         free_irq(dev->cir_irq, dev);
1639         release_region(dev->cir_addr, dev->params.io_region_size);
1640
1641         rc_unregister_device(dev->rdev);
1642
1643         kfree(dev);
1644 }
1645
1646 static int ite_suspend(struct pnp_dev *pdev, pm_message_t state)
1647 {
1648         struct ite_dev *dev = pnp_get_drvdata(pdev);
1649         unsigned long flags;
1650
1651         ite_dbg("%s called", __func__);
1652
1653         /* wait for any transmission to end */
1654         wait_event_interruptible(dev->tx_ended, !dev->transmitting);
1655
1656         spin_lock_irqsave(&dev->lock, flags);
1657
1658         /* disable all interrupts */
1659         dev->params.disable(dev);
1660
1661         spin_unlock_irqrestore(&dev->lock, flags);
1662
1663         return 0;
1664 }
1665
1666 static int ite_resume(struct pnp_dev *pdev)
1667 {
1668         struct ite_dev *dev = pnp_get_drvdata(pdev);
1669         unsigned long flags;
1670
1671         ite_dbg("%s called", __func__);
1672
1673         spin_lock_irqsave(&dev->lock, flags);
1674
1675         /* reinitialize hardware config registers */
1676         dev->params.init_hardware(dev);
1677         /* enable the receiver */
1678         dev->params.enable_rx(dev);
1679
1680         spin_unlock_irqrestore(&dev->lock, flags);
1681
1682         return 0;
1683 }
1684
1685 static void ite_shutdown(struct pnp_dev *pdev)
1686 {
1687         struct ite_dev *dev = pnp_get_drvdata(pdev);
1688         unsigned long flags;
1689
1690         ite_dbg("%s called", __func__);
1691
1692         spin_lock_irqsave(&dev->lock, flags);
1693
1694         /* disable all interrupts */
1695         dev->params.disable(dev);
1696
1697         spin_unlock_irqrestore(&dev->lock, flags);
1698 }
1699
1700 static struct pnp_driver ite_driver = {
1701         .name           = ITE_DRIVER_NAME,
1702         .id_table       = ite_ids,
1703         .probe          = ite_probe,
1704         .remove         = ite_remove,
1705         .suspend        = ite_suspend,
1706         .resume         = ite_resume,
1707         .shutdown       = ite_shutdown,
1708 };
1709
1710 MODULE_DEVICE_TABLE(pnp, ite_ids);
1711 MODULE_DESCRIPTION("ITE Tech Inc. IT8712F/ITE8512F CIR driver");
1712
1713 MODULE_AUTHOR("Juan J. Garcia de Soria <skandalfo@gmail.com>");
1714 MODULE_LICENSE("GPL");
1715
1716 module_pnp_driver(ite_driver);