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