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