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