1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for ITE Tech Inc. IT8712F/IT8512 CIR
5 * Copyright (C) 2010 Juan Jesús García de Soria <skandalfo@gmail.com>
7 * Inspired by the original lirc_it87 and lirc_ite8709 drivers, on top of the
8 * skeleton provided by the nuvoton-cir driver.
10 * The lirc_it87 driver was originally written by Hans-Gunter Lutke Uphues
11 * <hg_lu@web.de> in 2001, with enhancements by Christoph Bartelmus
12 * <lirc@bartelmus.de>, Andrew Calkin <r_tay@hotmail.com> and James Edwards
13 * <jimbo-lirc@edwardsclan.net>.
15 * The lirc_ite8709 driver was written by Grégory Lardière
16 * <spmf2004-lirc@yahoo.fr> in 2008.
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/pnp.h>
23 #include <linux/interrupt.h>
24 #include <linux/sched.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
27 #include <linux/input.h>
28 #include <linux/bitops.h>
29 #include <media/rc-core.h>
30 #include <linux/pci_ids.h>
34 /* module parameters */
38 module_param(debug, int, S_IRUGO | S_IWUSR);
39 MODULE_PARM_DESC(debug, "Enable debugging output");
41 /* low limit for RX carrier freq, Hz, 0 for no RX demodulation */
42 static int rx_low_carrier_freq;
43 module_param(rx_low_carrier_freq, int, S_IRUGO | S_IWUSR);
44 MODULE_PARM_DESC(rx_low_carrier_freq, "Override low RX carrier frequency, Hz, 0 for no RX demodulation");
46 /* high limit for RX carrier freq, Hz, 0 for no RX demodulation */
47 static int rx_high_carrier_freq;
48 module_param(rx_high_carrier_freq, int, S_IRUGO | S_IWUSR);
49 MODULE_PARM_DESC(rx_high_carrier_freq, "Override high RX carrier frequency, Hz, 0 for no RX demodulation");
51 /* override tx carrier frequency */
52 static int tx_carrier_freq;
53 module_param(tx_carrier_freq, int, S_IRUGO | S_IWUSR);
54 MODULE_PARM_DESC(tx_carrier_freq, "Override TX carrier frequency, Hz");
56 /* override tx duty cycle */
57 static int tx_duty_cycle;
58 module_param(tx_duty_cycle, int, S_IRUGO | S_IWUSR);
59 MODULE_PARM_DESC(tx_duty_cycle, "Override TX duty cycle, 1-100");
61 /* override default sample period */
62 static long sample_period;
63 module_param(sample_period, long, S_IRUGO | S_IWUSR);
64 MODULE_PARM_DESC(sample_period, "Override carrier sample period, us");
66 /* override detected model id */
67 static int model_number = -1;
68 module_param(model_number, int, S_IRUGO | S_IWUSR);
69 MODULE_PARM_DESC(model_number, "Use this model number, don't autodetect");
72 /* HW-independent code functions */
74 /* check whether carrier frequency is high frequency */
75 static inline bool ite_is_high_carrier_freq(unsigned int freq)
77 return freq >= ITE_HCF_MIN_CARRIER_FREQ;
80 /* get the bits required to program the carrier frequency in CFQ bits,
82 static u8 ite_get_carrier_freq_bits(unsigned int freq)
84 if (ite_is_high_carrier_freq(freq)) {
88 else if (freq < 465000)
91 else if (freq < 490000)
98 if (freq < ITE_LCF_MIN_CARRIER_FREQ)
99 freq = ITE_LCF_MIN_CARRIER_FREQ;
100 if (freq > ITE_LCF_MAX_CARRIER_FREQ)
101 freq = ITE_LCF_MAX_CARRIER_FREQ;
103 /* convert to kHz and subtract the base freq */
105 DIV_ROUND_CLOSEST(freq - ITE_LCF_MIN_CARRIER_FREQ,
112 /* get the bits required to program the pulse with in TXMPW */
113 static u8 ite_get_pulse_width_bits(unsigned int freq, int duty_cycle)
115 unsigned long period_ns, on_ns;
117 /* sanitize freq into range */
118 if (freq < ITE_LCF_MIN_CARRIER_FREQ)
119 freq = ITE_LCF_MIN_CARRIER_FREQ;
120 if (freq > ITE_HCF_MAX_CARRIER_FREQ)
121 freq = ITE_HCF_MAX_CARRIER_FREQ;
123 period_ns = 1000000000UL / freq;
124 on_ns = period_ns * duty_cycle / 100;
126 if (ite_is_high_carrier_freq(freq)) {
130 else if (on_ns < 850)
133 else if (on_ns < 950)
136 else if (on_ns < 1080)
145 else if (on_ns < 7850)
148 else if (on_ns < 9650)
151 else if (on_ns < 11950)
159 /* decode raw bytes as received by the hardware, and push them to the ir-core
161 static void ite_decode_bytes(struct ite_dev *dev, const u8 * data, int
165 unsigned long *ldata;
166 unsigned int next_one, next_zero, size;
167 struct ir_raw_event ev = {};
172 sample_period = dev->params.sample_period;
173 ldata = (unsigned long *)data;
175 next_one = find_next_bit_le(ldata, size, 0);
179 ITE_BITS_TO_US(next_one, sample_period);
180 ir_raw_event_store_with_filter(dev->rdev, &ev);
183 while (next_one < size) {
184 next_zero = find_next_zero_bit_le(ldata, size, next_one + 1);
186 ev.duration = ITE_BITS_TO_US(next_zero - next_one, sample_period);
187 ir_raw_event_store_with_filter(dev->rdev, &ev);
189 if (next_zero < size) {
191 find_next_bit_le(ldata,
196 ITE_BITS_TO_US(next_one - next_zero,
198 ir_raw_event_store_with_filter
204 ir_raw_event_handle(dev->rdev);
206 ite_dbg_verbose("decoded %d bytes.", length);
209 /* set all the rx/tx carrier parameters; this must be called with the device
211 static void ite_set_carrier_params(struct ite_dev *dev)
213 unsigned int freq, low_freq, high_freq;
215 bool use_demodulator;
216 bool for_tx = dev->transmitting;
218 ite_dbg("%s called", __func__);
221 /* we don't need no stinking calculations */
222 freq = dev->params.tx_carrier_freq;
223 allowance = ITE_RXDCR_DEFAULT;
224 use_demodulator = false;
226 low_freq = dev->params.rx_low_carrier_freq;
227 high_freq = dev->params.rx_high_carrier_freq;
230 /* don't demodulate */
232 ITE_DEFAULT_CARRIER_FREQ;
233 allowance = ITE_RXDCR_DEFAULT;
234 use_demodulator = false;
236 /* calculate the middle freq */
237 freq = (low_freq + high_freq) / 2;
239 /* calculate the allowance */
241 DIV_ROUND_CLOSEST(10000 * (high_freq - low_freq),
242 ITE_RXDCR_PER_10000_STEP
243 * (high_freq + low_freq));
248 if (allowance > ITE_RXDCR_MAX)
249 allowance = ITE_RXDCR_MAX;
251 use_demodulator = true;
255 /* set the carrier parameters in a device-dependent way */
256 dev->params.set_carrier_params(dev, ite_is_high_carrier_freq(freq),
257 use_demodulator, ite_get_carrier_freq_bits(freq), allowance,
258 ite_get_pulse_width_bits(freq, dev->params.tx_duty_cycle));
261 /* interrupt service routine for incoming and outgoing CIR data */
262 static irqreturn_t ite_cir_isr(int irq, void *data)
264 struct ite_dev *dev = data;
266 irqreturn_t ret = IRQ_RETVAL(IRQ_NONE);
267 u8 rx_buf[ITE_RX_FIFO_LEN];
271 ite_dbg_verbose("%s firing", __func__);
273 /* grab the spinlock */
274 spin_lock_irqsave(&dev->lock, flags);
276 /* read the interrupt flags */
277 iflags = dev->params.get_irq_causes(dev);
279 /* Check for RX overflow */
280 if (iflags & ITE_IRQ_RX_FIFO_OVERRUN) {
281 dev_warn(&dev->rdev->dev, "receive overflow\n");
282 ir_raw_event_reset(dev->rdev);
285 /* check for the receive interrupt */
286 if (iflags & (ITE_IRQ_RX_FIFO | ITE_IRQ_RX_FIFO_OVERRUN)) {
287 /* read the FIFO bytes */
289 dev->params.get_rx_bytes(dev, rx_buf,
293 /* drop the spinlock, since the ir-core layer
294 * may call us back again through
296 spin_unlock_irqrestore(&dev->
300 /* decode the data we've just received */
301 ite_decode_bytes(dev, rx_buf,
304 /* reacquire the spinlock */
305 spin_lock_irqsave(&dev->lock,
308 /* mark the interrupt as serviced */
309 ret = IRQ_RETVAL(IRQ_HANDLED);
311 } else if (iflags & ITE_IRQ_TX_FIFO) {
312 /* FIFO space available interrupt */
313 ite_dbg_verbose("got interrupt for TX FIFO");
315 /* wake any sleeping transmitter */
316 wake_up_interruptible(&dev->tx_queue);
318 /* mark the interrupt as serviced */
319 ret = IRQ_RETVAL(IRQ_HANDLED);
322 /* drop the spinlock */
323 spin_unlock_irqrestore(&dev->lock, flags);
325 ite_dbg_verbose("%s done returning %d", __func__, (int)ret);
330 /* set the rx carrier freq range, guess it's in Hz... */
331 static int ite_set_rx_carrier_range(struct rc_dev *rcdev, u32 carrier_low, u32
335 struct ite_dev *dev = rcdev->priv;
337 spin_lock_irqsave(&dev->lock, flags);
338 dev->params.rx_low_carrier_freq = carrier_low;
339 dev->params.rx_high_carrier_freq = carrier_high;
340 ite_set_carrier_params(dev);
341 spin_unlock_irqrestore(&dev->lock, flags);
346 /* set the tx carrier freq, guess it's in Hz... */
347 static int ite_set_tx_carrier(struct rc_dev *rcdev, u32 carrier)
350 struct ite_dev *dev = rcdev->priv;
352 spin_lock_irqsave(&dev->lock, flags);
353 dev->params.tx_carrier_freq = carrier;
354 ite_set_carrier_params(dev);
355 spin_unlock_irqrestore(&dev->lock, flags);
360 /* set the tx duty cycle by controlling the pulse width */
361 static int ite_set_tx_duty_cycle(struct rc_dev *rcdev, u32 duty_cycle)
364 struct ite_dev *dev = rcdev->priv;
366 spin_lock_irqsave(&dev->lock, flags);
367 dev->params.tx_duty_cycle = duty_cycle;
368 ite_set_carrier_params(dev);
369 spin_unlock_irqrestore(&dev->lock, flags);
374 /* transmit out IR pulses; what you get here is a batch of alternating
375 * pulse/space/pulse/space lengths that we should write out completely through
376 * the FIFO, blocking on a full FIFO */
377 static int ite_tx_ir(struct rc_dev *rcdev, unsigned *txbuf, unsigned n)
380 struct ite_dev *dev = rcdev->priv;
381 bool is_pulse = false;
382 int remaining_us, fifo_avail, fifo_remaining, last_idx = 0;
383 int max_rle_us, next_rle_us;
385 u8 last_sent[ITE_TX_FIFO_LEN];
388 ite_dbg("%s called", __func__);
390 /* clear the array just in case */
391 memset(last_sent, 0, sizeof(last_sent));
393 spin_lock_irqsave(&dev->lock, flags);
395 /* let everybody know we're now transmitting */
396 dev->transmitting = true;
398 /* and set the carrier values for transmission */
399 ite_set_carrier_params(dev);
401 /* calculate how much time we can send in one byte */
403 (ITE_BAUDRATE_DIVISOR * dev->params.sample_period *
404 ITE_TX_MAX_RLE) / 1000;
406 /* disable the receiver */
407 dev->params.disable_rx(dev);
409 /* this is where we'll begin filling in the FIFO, until it's full.
410 * then we'll just activate the interrupt, wait for it to wake us up
411 * again, disable it, continue filling the FIFO... until everything
412 * has been pushed out */
414 ITE_TX_FIFO_LEN - dev->params.get_tx_used_slots(dev);
416 while (n > 0 && dev->in_use) {
417 /* transmit the next sample */
418 is_pulse = !is_pulse;
419 remaining_us = *(txbuf++);
423 ((is_pulse) ? "pulse" : "space"),
427 /* repeat while the pulse is non-zero length */
428 while (remaining_us > 0 && dev->in_use) {
429 if (remaining_us > max_rle_us)
430 next_rle_us = max_rle_us;
433 next_rle_us = remaining_us;
435 remaining_us -= next_rle_us;
437 /* check what's the length we have to pump out */
438 val = (ITE_TX_MAX_RLE * next_rle_us) / max_rle_us;
440 /* put it into the sent buffer */
441 last_sent[last_idx++] = val;
442 last_idx &= (ITE_TX_FIFO_LEN);
444 /* encode it for 7 bits */
445 val = (val - 1) & ITE_TX_RLE_MASK;
447 /* take into account pulse/space prefix */
455 * if we get to 0 available, read again, just in case
456 * some other slot got freed
459 fifo_avail = ITE_TX_FIFO_LEN - dev->params.get_tx_used_slots(dev);
461 /* if it's still full */
462 if (fifo_avail <= 0) {
463 /* enable the tx interrupt */
465 enable_tx_interrupt(dev);
467 /* drop the spinlock */
468 spin_unlock_irqrestore(&dev->lock, flags);
470 /* wait for the FIFO to empty enough */
471 wait_event_interruptible(dev->tx_queue, (fifo_avail = ITE_TX_FIFO_LEN - dev->params.get_tx_used_slots(dev)) >= 8);
473 /* get the spinlock again */
474 spin_lock_irqsave(&dev->lock, flags);
476 /* disable the tx interrupt again. */
478 disable_tx_interrupt(dev);
481 /* now send the byte through the FIFO */
482 dev->params.put_tx_byte(dev, val);
487 /* wait and don't return until the whole FIFO has been sent out;
488 * otherwise we could configure the RX carrier params instead of the
489 * TX ones while the transmission is still being performed! */
490 fifo_remaining = dev->params.get_tx_used_slots(dev);
492 while (fifo_remaining > 0) {
495 last_idx &= (ITE_TX_FIFO_LEN - 1);
496 remaining_us += last_sent[last_idx];
498 remaining_us = (remaining_us * max_rle_us) / (ITE_TX_MAX_RLE);
500 /* drop the spinlock while we sleep */
501 spin_unlock_irqrestore(&dev->lock, flags);
503 /* sleep remaining_us microseconds */
504 mdelay(DIV_ROUND_UP(remaining_us, 1000));
506 /* reacquire the spinlock */
507 spin_lock_irqsave(&dev->lock, flags);
509 /* now we're not transmitting anymore */
510 dev->transmitting = false;
512 /* and set the carrier values for reception */
513 ite_set_carrier_params(dev);
515 /* re-enable the receiver */
517 dev->params.enable_rx(dev);
519 /* notify transmission end */
520 wake_up_interruptible(&dev->tx_ended);
522 spin_unlock_irqrestore(&dev->lock, flags);
527 /* idle the receiver if needed */
528 static void ite_s_idle(struct rc_dev *rcdev, bool enable)
531 struct ite_dev *dev = rcdev->priv;
533 ite_dbg("%s called", __func__);
536 spin_lock_irqsave(&dev->lock, flags);
537 dev->params.idle_rx(dev);
538 spin_unlock_irqrestore(&dev->lock, flags);
543 /* IT8712F HW-specific functions */
545 /* retrieve a bitmask of the current causes for a pending interrupt; this may
546 * be composed of ITE_IRQ_TX_FIFO, ITE_IRQ_RX_FIFO and ITE_IRQ_RX_FIFO_OVERRUN
548 static int it87_get_irq_causes(struct ite_dev *dev)
553 ite_dbg("%s called", __func__);
555 /* read the interrupt flags */
556 iflags = inb(dev->cir_addr + IT87_IIR) & IT87_II;
560 ret = ITE_IRQ_RX_FIFO;
563 ret = ITE_IRQ_RX_FIFO_OVERRUN;
566 ret = ITE_IRQ_TX_FIFO;
573 /* set the carrier parameters; to be called with the spinlock held */
574 static void it87_set_carrier_params(struct ite_dev *dev, bool high_freq,
575 bool use_demodulator,
576 u8 carrier_freq_bits, u8 allowance_bits,
581 ite_dbg("%s called", __func__);
583 /* program the RCR register */
584 val = inb(dev->cir_addr + IT87_RCR)
585 & ~(IT87_HCFS | IT87_RXEND | IT87_RXDCR);
593 val |= allowance_bits;
595 outb(val, dev->cir_addr + IT87_RCR);
597 /* program the TCR2 register */
598 outb((carrier_freq_bits << IT87_CFQ_SHIFT) | pulse_width_bits,
599 dev->cir_addr + IT87_TCR2);
602 /* read up to buf_size bytes from the RX FIFO; to be called with the spinlock
604 static int it87_get_rx_bytes(struct ite_dev *dev, u8 * buf, int buf_size)
608 ite_dbg("%s called", __func__);
610 /* read how many bytes are still in the FIFO */
611 fifo = inb(dev->cir_addr + IT87_RSR) & IT87_RXFBC;
613 while (fifo > 0 && buf_size > 0) {
614 *(buf++) = inb(dev->cir_addr + IT87_DR);
623 /* return how many bytes are still in the FIFO; this will be called
624 * with the device spinlock NOT HELD while waiting for the TX FIFO to get
625 * empty; let's expect this won't be a problem */
626 static int it87_get_tx_used_slots(struct ite_dev *dev)
628 ite_dbg("%s called", __func__);
630 return inb(dev->cir_addr + IT87_TSR) & IT87_TXFBC;
633 /* put a byte to the TX fifo; this should be called with the spinlock held */
634 static void it87_put_tx_byte(struct ite_dev *dev, u8 value)
636 outb(value, dev->cir_addr + IT87_DR);
639 /* idle the receiver so that we won't receive samples until another
640 pulse is detected; this must be called with the device spinlock held */
641 static void it87_idle_rx(struct ite_dev *dev)
643 ite_dbg("%s called", __func__);
645 /* disable streaming by clearing RXACT writing it as 1 */
646 outb(inb(dev->cir_addr + IT87_RCR) | IT87_RXACT,
647 dev->cir_addr + IT87_RCR);
650 outb(inb(dev->cir_addr + IT87_TCR1) | IT87_FIFOCLR,
651 dev->cir_addr + IT87_TCR1);
654 /* disable the receiver; this must be called with the device spinlock held */
655 static void it87_disable_rx(struct ite_dev *dev)
657 ite_dbg("%s called", __func__);
659 /* disable the receiver interrupts */
660 outb(inb(dev->cir_addr + IT87_IER) & ~(IT87_RDAIE | IT87_RFOIE),
661 dev->cir_addr + IT87_IER);
663 /* disable the receiver */
664 outb(inb(dev->cir_addr + IT87_RCR) & ~IT87_RXEN,
665 dev->cir_addr + IT87_RCR);
667 /* clear the FIFO and RXACT (actually RXACT should have been cleared
668 * in the previous outb() call) */
672 /* enable the receiver; this must be called with the device spinlock held */
673 static void it87_enable_rx(struct ite_dev *dev)
675 ite_dbg("%s called", __func__);
677 /* enable the receiver by setting RXEN */
678 outb(inb(dev->cir_addr + IT87_RCR) | IT87_RXEN,
679 dev->cir_addr + IT87_RCR);
681 /* just prepare it to idle for the next reception */
684 /* enable the receiver interrupts and master enable flag */
685 outb(inb(dev->cir_addr + IT87_IER) | IT87_RDAIE | IT87_RFOIE | IT87_IEC,
686 dev->cir_addr + IT87_IER);
689 /* disable the transmitter interrupt; this must be called with the device
691 static void it87_disable_tx_interrupt(struct ite_dev *dev)
693 ite_dbg("%s called", __func__);
695 /* disable the transmitter interrupts */
696 outb(inb(dev->cir_addr + IT87_IER) & ~IT87_TLDLIE,
697 dev->cir_addr + IT87_IER);
700 /* enable the transmitter interrupt; this must be called with the device
702 static void it87_enable_tx_interrupt(struct ite_dev *dev)
704 ite_dbg("%s called", __func__);
706 /* enable the transmitter interrupts and master enable flag */
707 outb(inb(dev->cir_addr + IT87_IER) | IT87_TLDLIE | IT87_IEC,
708 dev->cir_addr + IT87_IER);
711 /* disable the device; this must be called with the device spinlock held */
712 static void it87_disable(struct ite_dev *dev)
714 ite_dbg("%s called", __func__);
716 /* clear out all interrupt enable flags */
717 outb(inb(dev->cir_addr + IT87_IER) &
718 ~(IT87_IEC | IT87_RFOIE | IT87_RDAIE | IT87_TLDLIE),
719 dev->cir_addr + IT87_IER);
721 /* disable the receiver */
722 it87_disable_rx(dev);
725 outb(IT87_FIFOCLR | inb(dev->cir_addr + IT87_TCR1),
726 dev->cir_addr + IT87_TCR1);
729 /* initialize the hardware */
730 static void it87_init_hardware(struct ite_dev *dev)
732 ite_dbg("%s called", __func__);
734 /* enable just the baud rate divisor register,
735 disabling all the interrupts at the same time */
736 outb((inb(dev->cir_addr + IT87_IER) &
737 ~(IT87_IEC | IT87_RFOIE | IT87_RDAIE | IT87_TLDLIE)) | IT87_BR,
738 dev->cir_addr + IT87_IER);
740 /* write out the baud rate divisor */
741 outb(ITE_BAUDRATE_DIVISOR & 0xff, dev->cir_addr + IT87_BDLR);
742 outb((ITE_BAUDRATE_DIVISOR >> 8) & 0xff, dev->cir_addr + IT87_BDHR);
744 /* disable the baud rate divisor register again */
745 outb(inb(dev->cir_addr + IT87_IER) & ~IT87_BR,
746 dev->cir_addr + IT87_IER);
748 /* program the RCR register defaults */
749 outb(ITE_RXDCR_DEFAULT, dev->cir_addr + IT87_RCR);
751 /* program the TCR1 register */
752 outb(IT87_TXMPM_DEFAULT | IT87_TXENDF | IT87_TXRLE
753 | IT87_FIFOTL_DEFAULT | IT87_FIFOCLR,
754 dev->cir_addr + IT87_TCR1);
756 /* program the carrier parameters */
757 ite_set_carrier_params(dev);
760 /* IT8512F on ITE8708 HW-specific functions */
762 /* retrieve a bitmask of the current causes for a pending interrupt; this may
763 * be composed of ITE_IRQ_TX_FIFO, ITE_IRQ_RX_FIFO and ITE_IRQ_RX_FIFO_OVERRUN
765 static int it8708_get_irq_causes(struct ite_dev *dev)
770 ite_dbg("%s called", __func__);
772 /* read the interrupt flags */
773 iflags = inb(dev->cir_addr + IT8708_C0IIR);
775 if (iflags & IT85_TLDLI)
776 ret |= ITE_IRQ_TX_FIFO;
777 if (iflags & IT85_RDAI)
778 ret |= ITE_IRQ_RX_FIFO;
779 if (iflags & IT85_RFOI)
780 ret |= ITE_IRQ_RX_FIFO_OVERRUN;
785 /* set the carrier parameters; to be called with the spinlock held */
786 static void it8708_set_carrier_params(struct ite_dev *dev, bool high_freq,
787 bool use_demodulator,
788 u8 carrier_freq_bits, u8 allowance_bits,
793 ite_dbg("%s called", __func__);
795 /* program the C0CFR register, with HRAE=1 */
796 outb(inb(dev->cir_addr + IT8708_BANKSEL) | IT8708_HRAE,
797 dev->cir_addr + IT8708_BANKSEL);
799 val = (inb(dev->cir_addr + IT8708_C0CFR)
800 & ~(IT85_HCFS | IT85_CFQ)) | carrier_freq_bits;
805 outb(val, dev->cir_addr + IT8708_C0CFR);
807 outb(inb(dev->cir_addr + IT8708_BANKSEL) & ~IT8708_HRAE,
808 dev->cir_addr + IT8708_BANKSEL);
810 /* program the C0RCR register */
811 val = inb(dev->cir_addr + IT8708_C0RCR)
812 & ~(IT85_RXEND | IT85_RXDCR);
817 val |= allowance_bits;
819 outb(val, dev->cir_addr + IT8708_C0RCR);
821 /* program the C0TCR register */
822 val = inb(dev->cir_addr + IT8708_C0TCR) & ~IT85_TXMPW;
823 val |= pulse_width_bits;
824 outb(val, dev->cir_addr + IT8708_C0TCR);
827 /* read up to buf_size bytes from the RX FIFO; to be called with the spinlock
829 static int it8708_get_rx_bytes(struct ite_dev *dev, u8 * buf, int buf_size)
833 ite_dbg("%s called", __func__);
835 /* read how many bytes are still in the FIFO */
836 fifo = inb(dev->cir_addr + IT8708_C0RFSR) & IT85_RXFBC;
838 while (fifo > 0 && buf_size > 0) {
839 *(buf++) = inb(dev->cir_addr + IT8708_C0DR);
848 /* return how many bytes are still in the FIFO; this will be called
849 * with the device spinlock NOT HELD while waiting for the TX FIFO to get
850 * empty; let's expect this won't be a problem */
851 static int it8708_get_tx_used_slots(struct ite_dev *dev)
853 ite_dbg("%s called", __func__);
855 return inb(dev->cir_addr + IT8708_C0TFSR) & IT85_TXFBC;
858 /* put a byte to the TX fifo; this should be called with the spinlock held */
859 static void it8708_put_tx_byte(struct ite_dev *dev, u8 value)
861 outb(value, dev->cir_addr + IT8708_C0DR);
864 /* idle the receiver so that we won't receive samples until another
865 pulse is detected; this must be called with the device spinlock held */
866 static void it8708_idle_rx(struct ite_dev *dev)
868 ite_dbg("%s called", __func__);
870 /* disable streaming by clearing RXACT writing it as 1 */
871 outb(inb(dev->cir_addr + IT8708_C0RCR) | IT85_RXACT,
872 dev->cir_addr + IT8708_C0RCR);
875 outb(inb(dev->cir_addr + IT8708_C0MSTCR) | IT85_FIFOCLR,
876 dev->cir_addr + IT8708_C0MSTCR);
879 /* disable the receiver; this must be called with the device spinlock held */
880 static void it8708_disable_rx(struct ite_dev *dev)
882 ite_dbg("%s called", __func__);
884 /* disable the receiver interrupts */
885 outb(inb(dev->cir_addr + IT8708_C0IER) &
886 ~(IT85_RDAIE | IT85_RFOIE),
887 dev->cir_addr + IT8708_C0IER);
889 /* disable the receiver */
890 outb(inb(dev->cir_addr + IT8708_C0RCR) & ~IT85_RXEN,
891 dev->cir_addr + IT8708_C0RCR);
893 /* clear the FIFO and RXACT (actually RXACT should have been cleared
894 * in the previous outb() call) */
898 /* enable the receiver; this must be called with the device spinlock held */
899 static void it8708_enable_rx(struct ite_dev *dev)
901 ite_dbg("%s called", __func__);
903 /* enable the receiver by setting RXEN */
904 outb(inb(dev->cir_addr + IT8708_C0RCR) | IT85_RXEN,
905 dev->cir_addr + IT8708_C0RCR);
907 /* just prepare it to idle for the next reception */
910 /* enable the receiver interrupts and master enable flag */
911 outb(inb(dev->cir_addr + IT8708_C0IER)
912 |IT85_RDAIE | IT85_RFOIE | IT85_IEC,
913 dev->cir_addr + IT8708_C0IER);
916 /* disable the transmitter interrupt; this must be called with the device
918 static void it8708_disable_tx_interrupt(struct ite_dev *dev)
920 ite_dbg("%s called", __func__);
922 /* disable the transmitter interrupts */
923 outb(inb(dev->cir_addr + IT8708_C0IER) & ~IT85_TLDLIE,
924 dev->cir_addr + IT8708_C0IER);
927 /* enable the transmitter interrupt; this must be called with the device
929 static void it8708_enable_tx_interrupt(struct ite_dev *dev)
931 ite_dbg("%s called", __func__);
933 /* enable the transmitter interrupts and master enable flag */
934 outb(inb(dev->cir_addr + IT8708_C0IER)
935 |IT85_TLDLIE | IT85_IEC,
936 dev->cir_addr + IT8708_C0IER);
939 /* disable the device; this must be called with the device spinlock held */
940 static void it8708_disable(struct ite_dev *dev)
942 ite_dbg("%s called", __func__);
944 /* clear out all interrupt enable flags */
945 outb(inb(dev->cir_addr + IT8708_C0IER) &
946 ~(IT85_IEC | IT85_RFOIE | IT85_RDAIE | IT85_TLDLIE),
947 dev->cir_addr + IT8708_C0IER);
949 /* disable the receiver */
950 it8708_disable_rx(dev);
953 outb(IT85_FIFOCLR | inb(dev->cir_addr + IT8708_C0MSTCR),
954 dev->cir_addr + IT8708_C0MSTCR);
957 /* initialize the hardware */
958 static void it8708_init_hardware(struct ite_dev *dev)
960 ite_dbg("%s called", __func__);
962 /* disable all the interrupts */
963 outb(inb(dev->cir_addr + IT8708_C0IER) &
964 ~(IT85_IEC | IT85_RFOIE | IT85_RDAIE | IT85_TLDLIE),
965 dev->cir_addr + IT8708_C0IER);
967 /* program the baud rate divisor */
968 outb(inb(dev->cir_addr + IT8708_BANKSEL) | IT8708_HRAE,
969 dev->cir_addr + IT8708_BANKSEL);
971 outb(ITE_BAUDRATE_DIVISOR & 0xff, dev->cir_addr + IT8708_C0BDLR);
972 outb((ITE_BAUDRATE_DIVISOR >> 8) & 0xff,
973 dev->cir_addr + IT8708_C0BDHR);
975 outb(inb(dev->cir_addr + IT8708_BANKSEL) & ~IT8708_HRAE,
976 dev->cir_addr + IT8708_BANKSEL);
978 /* program the C0MSTCR register defaults */
979 outb((inb(dev->cir_addr + IT8708_C0MSTCR) &
980 ~(IT85_ILSEL | IT85_ILE | IT85_FIFOTL |
981 IT85_FIFOCLR | IT85_RESET)) |
983 dev->cir_addr + IT8708_C0MSTCR);
985 /* program the C0RCR register defaults */
986 outb((inb(dev->cir_addr + IT8708_C0RCR) &
987 ~(IT85_RXEN | IT85_RDWOS | IT85_RXEND |
988 IT85_RXACT | IT85_RXDCR)) |
990 dev->cir_addr + IT8708_C0RCR);
992 /* program the C0TCR register defaults */
993 outb((inb(dev->cir_addr + IT8708_C0TCR) &
994 ~(IT85_TXMPM | IT85_TXMPW))
995 |IT85_TXRLE | IT85_TXENDF |
996 IT85_TXMPM_DEFAULT | IT85_TXMPW_DEFAULT,
997 dev->cir_addr + IT8708_C0TCR);
999 /* program the carrier parameters */
1000 ite_set_carrier_params(dev);
1003 /* IT8512F on ITE8709 HW-specific functions */
1005 /* read a byte from the SRAM module */
1006 static inline u8 it8709_rm(struct ite_dev *dev, int index)
1008 outb(index, dev->cir_addr + IT8709_RAM_IDX);
1009 return inb(dev->cir_addr + IT8709_RAM_VAL);
1012 /* write a byte to the SRAM module */
1013 static inline void it8709_wm(struct ite_dev *dev, u8 val, int index)
1015 outb(index, dev->cir_addr + IT8709_RAM_IDX);
1016 outb(val, dev->cir_addr + IT8709_RAM_VAL);
1019 static void it8709_wait(struct ite_dev *dev)
1023 * loop until device tells it's ready to continue
1024 * iterations count is usually ~750 but can sometimes achieve 13000
1026 for (i = 0; i < 15000; i++) {
1028 if (it8709_rm(dev, IT8709_MODE) == IT8709_IDLE)
1033 /* read the value of a CIR register */
1034 static u8 it8709_rr(struct ite_dev *dev, int index)
1036 /* just wait in case the previous access was a write */
1038 it8709_wm(dev, index, IT8709_REG_IDX);
1039 it8709_wm(dev, IT8709_READ, IT8709_MODE);
1041 /* wait for the read data to be available */
1044 /* return the read value */
1045 return it8709_rm(dev, IT8709_REG_VAL);
1048 /* write the value of a CIR register */
1049 static void it8709_wr(struct ite_dev *dev, u8 val, int index)
1051 /* we wait before writing, and not afterwards, since this allows us to
1052 * pipeline the host CPU with the microcontroller */
1054 it8709_wm(dev, val, IT8709_REG_VAL);
1055 it8709_wm(dev, index, IT8709_REG_IDX);
1056 it8709_wm(dev, IT8709_WRITE, IT8709_MODE);
1059 /* retrieve a bitmask of the current causes for a pending interrupt; this may
1060 * be composed of ITE_IRQ_TX_FIFO, ITE_IRQ_RX_FIFO and ITE_IRQ_RX_FIFO_OVERRUN
1062 static int it8709_get_irq_causes(struct ite_dev *dev)
1067 ite_dbg("%s called", __func__);
1069 /* read the interrupt flags */
1070 iflags = it8709_rm(dev, IT8709_IIR);
1072 if (iflags & IT85_TLDLI)
1073 ret |= ITE_IRQ_TX_FIFO;
1074 if (iflags & IT85_RDAI)
1075 ret |= ITE_IRQ_RX_FIFO;
1076 if (iflags & IT85_RFOI)
1077 ret |= ITE_IRQ_RX_FIFO_OVERRUN;
1082 /* set the carrier parameters; to be called with the spinlock held */
1083 static void it8709_set_carrier_params(struct ite_dev *dev, bool high_freq,
1084 bool use_demodulator,
1085 u8 carrier_freq_bits, u8 allowance_bits,
1086 u8 pulse_width_bits)
1090 ite_dbg("%s called", __func__);
1092 val = (it8709_rr(dev, IT85_C0CFR)
1093 &~(IT85_HCFS | IT85_CFQ)) |
1099 it8709_wr(dev, val, IT85_C0CFR);
1101 /* program the C0RCR register */
1102 val = it8709_rr(dev, IT85_C0RCR)
1103 & ~(IT85_RXEND | IT85_RXDCR);
1105 if (use_demodulator)
1108 val |= allowance_bits;
1110 it8709_wr(dev, val, IT85_C0RCR);
1112 /* program the C0TCR register */
1113 val = it8709_rr(dev, IT85_C0TCR) & ~IT85_TXMPW;
1114 val |= pulse_width_bits;
1115 it8709_wr(dev, val, IT85_C0TCR);
1118 /* read up to buf_size bytes from the RX FIFO; to be called with the spinlock
1120 static int it8709_get_rx_bytes(struct ite_dev *dev, u8 * buf, int buf_size)
1124 ite_dbg("%s called", __func__);
1126 /* read how many bytes are still in the FIFO */
1127 fifo = it8709_rm(dev, IT8709_RFSR) & IT85_RXFBC;
1129 while (fifo > 0 && buf_size > 0) {
1130 *(buf++) = it8709_rm(dev, IT8709_FIFO + read);
1136 /* 'clear' the FIFO by setting the writing index to 0; this is
1137 * completely bound to be racy, but we can't help it, since it's a
1138 * limitation of the protocol */
1139 it8709_wm(dev, 0, IT8709_RFSR);
1144 /* return how many bytes are still in the FIFO; this will be called
1145 * with the device spinlock NOT HELD while waiting for the TX FIFO to get
1146 * empty; let's expect this won't be a problem */
1147 static int it8709_get_tx_used_slots(struct ite_dev *dev)
1149 ite_dbg("%s called", __func__);
1151 return it8709_rr(dev, IT85_C0TFSR) & IT85_TXFBC;
1154 /* put a byte to the TX fifo; this should be called with the spinlock held */
1155 static void it8709_put_tx_byte(struct ite_dev *dev, u8 value)
1157 it8709_wr(dev, value, IT85_C0DR);
1160 /* idle the receiver so that we won't receive samples until another
1161 pulse is detected; this must be called with the device spinlock held */
1162 static void it8709_idle_rx(struct ite_dev *dev)
1164 ite_dbg("%s called", __func__);
1166 /* disable streaming by clearing RXACT writing it as 1 */
1167 it8709_wr(dev, it8709_rr(dev, IT85_C0RCR) | IT85_RXACT,
1170 /* clear the FIFO */
1171 it8709_wr(dev, it8709_rr(dev, IT85_C0MSTCR) | IT85_FIFOCLR,
1175 /* disable the receiver; this must be called with the device spinlock held */
1176 static void it8709_disable_rx(struct ite_dev *dev)
1178 ite_dbg("%s called", __func__);
1180 /* disable the receiver interrupts */
1181 it8709_wr(dev, it8709_rr(dev, IT85_C0IER) &
1182 ~(IT85_RDAIE | IT85_RFOIE),
1185 /* disable the receiver */
1186 it8709_wr(dev, it8709_rr(dev, IT85_C0RCR) & ~IT85_RXEN,
1189 /* clear the FIFO and RXACT (actually RXACT should have been cleared
1190 * in the previous it8709_wr(dev, ) call) */
1191 it8709_idle_rx(dev);
1194 /* enable the receiver; this must be called with the device spinlock held */
1195 static void it8709_enable_rx(struct ite_dev *dev)
1197 ite_dbg("%s called", __func__);
1199 /* enable the receiver by setting RXEN */
1200 it8709_wr(dev, it8709_rr(dev, IT85_C0RCR) | IT85_RXEN,
1203 /* just prepare it to idle for the next reception */
1204 it8709_idle_rx(dev);
1206 /* enable the receiver interrupts and master enable flag */
1207 it8709_wr(dev, it8709_rr(dev, IT85_C0IER)
1208 |IT85_RDAIE | IT85_RFOIE | IT85_IEC,
1212 /* disable the transmitter interrupt; this must be called with the device
1214 static void it8709_disable_tx_interrupt(struct ite_dev *dev)
1216 ite_dbg("%s called", __func__);
1218 /* disable the transmitter interrupts */
1219 it8709_wr(dev, it8709_rr(dev, IT85_C0IER) & ~IT85_TLDLIE,
1223 /* enable the transmitter interrupt; this must be called with the device
1225 static void it8709_enable_tx_interrupt(struct ite_dev *dev)
1227 ite_dbg("%s called", __func__);
1229 /* enable the transmitter interrupts and master enable flag */
1230 it8709_wr(dev, it8709_rr(dev, IT85_C0IER)
1231 |IT85_TLDLIE | IT85_IEC,
1235 /* disable the device; this must be called with the device spinlock held */
1236 static void it8709_disable(struct ite_dev *dev)
1238 ite_dbg("%s called", __func__);
1240 /* clear out all interrupt enable flags */
1241 it8709_wr(dev, it8709_rr(dev, IT85_C0IER) &
1242 ~(IT85_IEC | IT85_RFOIE | IT85_RDAIE | IT85_TLDLIE),
1245 /* disable the receiver */
1246 it8709_disable_rx(dev);
1248 /* erase the FIFO */
1249 it8709_wr(dev, IT85_FIFOCLR | it8709_rr(dev, IT85_C0MSTCR),
1253 /* initialize the hardware */
1254 static void it8709_init_hardware(struct ite_dev *dev)
1256 ite_dbg("%s called", __func__);
1258 /* disable all the interrupts */
1259 it8709_wr(dev, it8709_rr(dev, IT85_C0IER) &
1260 ~(IT85_IEC | IT85_RFOIE | IT85_RDAIE | IT85_TLDLIE),
1263 /* program the baud rate divisor */
1264 it8709_wr(dev, ITE_BAUDRATE_DIVISOR & 0xff, IT85_C0BDLR);
1265 it8709_wr(dev, (ITE_BAUDRATE_DIVISOR >> 8) & 0xff,
1268 /* program the C0MSTCR register defaults */
1269 it8709_wr(dev, (it8709_rr(dev, IT85_C0MSTCR) &
1270 ~(IT85_ILSEL | IT85_ILE | IT85_FIFOTL
1271 | IT85_FIFOCLR | IT85_RESET)) | IT85_FIFOTL_DEFAULT,
1274 /* program the C0RCR register defaults */
1275 it8709_wr(dev, (it8709_rr(dev, IT85_C0RCR) &
1276 ~(IT85_RXEN | IT85_RDWOS | IT85_RXEND | IT85_RXACT
1277 | IT85_RXDCR)) | ITE_RXDCR_DEFAULT,
1280 /* program the C0TCR register defaults */
1281 it8709_wr(dev, (it8709_rr(dev, IT85_C0TCR) & ~(IT85_TXMPM | IT85_TXMPW))
1282 | IT85_TXRLE | IT85_TXENDF | IT85_TXMPM_DEFAULT
1283 | IT85_TXMPW_DEFAULT,
1286 /* program the carrier parameters */
1287 ite_set_carrier_params(dev);
1291 /* generic hardware setup/teardown code */
1293 /* activate the device for use */
1294 static int ite_open(struct rc_dev *rcdev)
1296 struct ite_dev *dev = rcdev->priv;
1297 unsigned long flags;
1299 ite_dbg("%s called", __func__);
1301 spin_lock_irqsave(&dev->lock, flags);
1304 /* enable the receiver */
1305 dev->params.enable_rx(dev);
1307 spin_unlock_irqrestore(&dev->lock, flags);
1312 /* deactivate the device for use */
1313 static void ite_close(struct rc_dev *rcdev)
1315 struct ite_dev *dev = rcdev->priv;
1316 unsigned long flags;
1318 ite_dbg("%s called", __func__);
1320 spin_lock_irqsave(&dev->lock, flags);
1321 dev->in_use = false;
1323 /* wait for any transmission to end */
1324 spin_unlock_irqrestore(&dev->lock, flags);
1325 wait_event_interruptible(dev->tx_ended, !dev->transmitting);
1326 spin_lock_irqsave(&dev->lock, flags);
1328 dev->params.disable(dev);
1330 spin_unlock_irqrestore(&dev->lock, flags);
1333 /* supported models and their parameters */
1334 static const struct ite_dev_params ite_dev_descs[] = {
1336 .model = "ITE8704 CIR transceiver",
1337 .io_region_size = IT87_IOREG_LENGTH,
1339 .hw_tx_capable = true,
1340 .sample_period = (u32) (1000000000ULL / 115200),
1341 .tx_carrier_freq = 38000,
1342 .tx_duty_cycle = 33,
1343 .rx_low_carrier_freq = 0,
1344 .rx_high_carrier_freq = 0,
1347 .get_irq_causes = it87_get_irq_causes,
1348 .enable_rx = it87_enable_rx,
1349 .idle_rx = it87_idle_rx,
1350 .disable_rx = it87_idle_rx,
1351 .get_rx_bytes = it87_get_rx_bytes,
1352 .enable_tx_interrupt = it87_enable_tx_interrupt,
1353 .disable_tx_interrupt = it87_disable_tx_interrupt,
1354 .get_tx_used_slots = it87_get_tx_used_slots,
1355 .put_tx_byte = it87_put_tx_byte,
1356 .disable = it87_disable,
1357 .init_hardware = it87_init_hardware,
1358 .set_carrier_params = it87_set_carrier_params,
1361 .model = "ITE8713 CIR transceiver",
1362 .io_region_size = IT87_IOREG_LENGTH,
1364 .hw_tx_capable = true,
1365 .sample_period = (u32) (1000000000ULL / 115200),
1366 .tx_carrier_freq = 38000,
1367 .tx_duty_cycle = 33,
1368 .rx_low_carrier_freq = 0,
1369 .rx_high_carrier_freq = 0,
1372 .get_irq_causes = it87_get_irq_causes,
1373 .enable_rx = it87_enable_rx,
1374 .idle_rx = it87_idle_rx,
1375 .disable_rx = it87_idle_rx,
1376 .get_rx_bytes = it87_get_rx_bytes,
1377 .enable_tx_interrupt = it87_enable_tx_interrupt,
1378 .disable_tx_interrupt = it87_disable_tx_interrupt,
1379 .get_tx_used_slots = it87_get_tx_used_slots,
1380 .put_tx_byte = it87_put_tx_byte,
1381 .disable = it87_disable,
1382 .init_hardware = it87_init_hardware,
1383 .set_carrier_params = it87_set_carrier_params,
1386 .model = "ITE8708 CIR transceiver",
1387 .io_region_size = IT8708_IOREG_LENGTH,
1389 .hw_tx_capable = true,
1390 .sample_period = (u32) (1000000000ULL / 115200),
1391 .tx_carrier_freq = 38000,
1392 .tx_duty_cycle = 33,
1393 .rx_low_carrier_freq = 0,
1394 .rx_high_carrier_freq = 0,
1397 .get_irq_causes = it8708_get_irq_causes,
1398 .enable_rx = it8708_enable_rx,
1399 .idle_rx = it8708_idle_rx,
1400 .disable_rx = it8708_idle_rx,
1401 .get_rx_bytes = it8708_get_rx_bytes,
1402 .enable_tx_interrupt = it8708_enable_tx_interrupt,
1403 .disable_tx_interrupt =
1404 it8708_disable_tx_interrupt,
1405 .get_tx_used_slots = it8708_get_tx_used_slots,
1406 .put_tx_byte = it8708_put_tx_byte,
1407 .disable = it8708_disable,
1408 .init_hardware = it8708_init_hardware,
1409 .set_carrier_params = it8708_set_carrier_params,
1412 .model = "ITE8709 CIR transceiver",
1413 .io_region_size = IT8709_IOREG_LENGTH,
1415 .hw_tx_capable = true,
1416 .sample_period = (u32) (1000000000ULL / 115200),
1417 .tx_carrier_freq = 38000,
1418 .tx_duty_cycle = 33,
1419 .rx_low_carrier_freq = 0,
1420 .rx_high_carrier_freq = 0,
1423 .get_irq_causes = it8709_get_irq_causes,
1424 .enable_rx = it8709_enable_rx,
1425 .idle_rx = it8709_idle_rx,
1426 .disable_rx = it8709_idle_rx,
1427 .get_rx_bytes = it8709_get_rx_bytes,
1428 .enable_tx_interrupt = it8709_enable_tx_interrupt,
1429 .disable_tx_interrupt =
1430 it8709_disable_tx_interrupt,
1431 .get_tx_used_slots = it8709_get_tx_used_slots,
1432 .put_tx_byte = it8709_put_tx_byte,
1433 .disable = it8709_disable,
1434 .init_hardware = it8709_init_hardware,
1435 .set_carrier_params = it8709_set_carrier_params,
1439 static const struct pnp_device_id ite_ids[] = {
1440 {"ITE8704", 0}, /* Default model */
1441 {"ITE8713", 1}, /* CIR found in EEEBox 1501U */
1442 {"ITE8708", 2}, /* Bridged IT8512 */
1443 {"ITE8709", 3}, /* SRAM-Bridged IT8512 */
1447 /* allocate memory, probe hardware, and initialize everything */
1448 static int ite_probe(struct pnp_dev *pdev, const struct pnp_device_id
1451 const struct ite_dev_params *dev_desc = NULL;
1452 struct ite_dev *itdev = NULL;
1453 struct rc_dev *rdev = NULL;
1458 ite_dbg("%s called", __func__);
1460 itdev = kzalloc(sizeof(struct ite_dev), GFP_KERNEL);
1464 /* input device for IR remote (and tx) */
1465 rdev = rc_allocate_device(RC_DRIVER_IR_RAW);
1467 goto exit_free_dev_rdev;
1472 /* get the model number */
1473 model_no = (int)dev_id->driver_data;
1474 ite_pr(KERN_NOTICE, "Auto-detected model: %s\n",
1475 ite_dev_descs[model_no].model);
1477 if (model_number >= 0 && model_number < ARRAY_SIZE(ite_dev_descs)) {
1478 model_no = model_number;
1479 ite_pr(KERN_NOTICE, "The model has been fixed by a module parameter.");
1482 ite_pr(KERN_NOTICE, "Using model: %s\n", ite_dev_descs[model_no].model);
1484 /* get the description for the device */
1485 dev_desc = &ite_dev_descs[model_no];
1486 io_rsrc_no = dev_desc->io_rsrc_no;
1488 /* validate pnp resources */
1489 if (!pnp_port_valid(pdev, io_rsrc_no) ||
1490 pnp_port_len(pdev, io_rsrc_no) != dev_desc->io_region_size) {
1491 dev_err(&pdev->dev, "IR PNP Port not valid!\n");
1492 goto exit_free_dev_rdev;
1495 if (!pnp_irq_valid(pdev, 0)) {
1496 dev_err(&pdev->dev, "PNP IRQ not valid!\n");
1497 goto exit_free_dev_rdev;
1500 /* store resource values */
1501 itdev->cir_addr = pnp_port_start(pdev, io_rsrc_no);
1502 itdev->cir_irq = pnp_irq(pdev, 0);
1504 /* initialize spinlocks */
1505 spin_lock_init(&itdev->lock);
1507 /* set driver data into the pnp device */
1508 pnp_set_drvdata(pdev, itdev);
1511 /* initialize waitqueues for transmission */
1512 init_waitqueue_head(&itdev->tx_queue);
1513 init_waitqueue_head(&itdev->tx_ended);
1515 /* copy model-specific parameters */
1516 itdev->params = *dev_desc;
1518 /* apply any overrides */
1519 if (sample_period > 0)
1520 itdev->params.sample_period = sample_period;
1522 if (tx_carrier_freq > 0)
1523 itdev->params.tx_carrier_freq = tx_carrier_freq;
1525 if (tx_duty_cycle > 0 && tx_duty_cycle <= 100)
1526 itdev->params.tx_duty_cycle = tx_duty_cycle;
1528 if (rx_low_carrier_freq > 0)
1529 itdev->params.rx_low_carrier_freq = rx_low_carrier_freq;
1531 if (rx_high_carrier_freq > 0)
1532 itdev->params.rx_high_carrier_freq = rx_high_carrier_freq;
1534 /* print out parameters */
1535 ite_pr(KERN_NOTICE, "TX-capable: %d\n", (int)
1536 itdev->params.hw_tx_capable);
1537 ite_pr(KERN_NOTICE, "Sample period (ns): %ld\n", (long)
1538 itdev->params.sample_period);
1539 ite_pr(KERN_NOTICE, "TX carrier frequency (Hz): %d\n", (int)
1540 itdev->params.tx_carrier_freq);
1541 ite_pr(KERN_NOTICE, "TX duty cycle (%%): %d\n", (int)
1542 itdev->params.tx_duty_cycle);
1543 ite_pr(KERN_NOTICE, "RX low carrier frequency (Hz): %d\n", (int)
1544 itdev->params.rx_low_carrier_freq);
1545 ite_pr(KERN_NOTICE, "RX high carrier frequency (Hz): %d\n", (int)
1546 itdev->params.rx_high_carrier_freq);
1548 /* set up hardware initial state */
1549 itdev->params.init_hardware(itdev);
1551 /* set up ir-core props */
1553 rdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
1554 rdev->open = ite_open;
1555 rdev->close = ite_close;
1556 rdev->s_idle = ite_s_idle;
1557 rdev->s_rx_carrier_range = ite_set_rx_carrier_range;
1558 /* FIFO threshold is 17 bytes, so 17 * 8 samples minimum */
1559 rdev->min_timeout = 17 * 8 * ITE_BAUDRATE_DIVISOR *
1560 itdev->params.sample_period / 1000;
1561 rdev->timeout = IR_DEFAULT_TIMEOUT;
1562 rdev->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
1563 rdev->rx_resolution = ITE_BAUDRATE_DIVISOR *
1564 itdev->params.sample_period / 1000;
1565 rdev->tx_resolution = ITE_BAUDRATE_DIVISOR *
1566 itdev->params.sample_period / 1000;
1568 /* set up transmitter related values if needed */
1569 if (itdev->params.hw_tx_capable) {
1570 rdev->tx_ir = ite_tx_ir;
1571 rdev->s_tx_carrier = ite_set_tx_carrier;
1572 rdev->s_tx_duty_cycle = ite_set_tx_duty_cycle;
1575 rdev->device_name = dev_desc->model;
1576 rdev->input_id.bustype = BUS_HOST;
1577 rdev->input_id.vendor = PCI_VENDOR_ID_ITE;
1578 rdev->input_id.product = 0;
1579 rdev->input_id.version = 0;
1580 rdev->driver_name = ITE_DRIVER_NAME;
1581 rdev->map_name = RC_MAP_RC6_MCE;
1583 ret = rc_register_device(rdev);
1585 goto exit_free_dev_rdev;
1588 /* now claim resources */
1589 if (!request_region(itdev->cir_addr,
1590 dev_desc->io_region_size, ITE_DRIVER_NAME))
1591 goto exit_unregister_device;
1593 if (request_irq(itdev->cir_irq, ite_cir_isr, IRQF_SHARED,
1594 ITE_DRIVER_NAME, (void *)itdev))
1595 goto exit_release_cir_addr;
1597 ite_pr(KERN_NOTICE, "driver has been successfully loaded\n");
1601 exit_release_cir_addr:
1602 release_region(itdev->cir_addr, itdev->params.io_region_size);
1603 exit_unregister_device:
1604 rc_unregister_device(rdev);
1607 rc_free_device(rdev);
1613 static void ite_remove(struct pnp_dev *pdev)
1615 struct ite_dev *dev = pnp_get_drvdata(pdev);
1616 unsigned long flags;
1618 ite_dbg("%s called", __func__);
1620 spin_lock_irqsave(&dev->lock, flags);
1622 /* disable hardware */
1623 dev->params.disable(dev);
1625 spin_unlock_irqrestore(&dev->lock, flags);
1627 /* free resources */
1628 free_irq(dev->cir_irq, dev);
1629 release_region(dev->cir_addr, dev->params.io_region_size);
1631 rc_unregister_device(dev->rdev);
1636 static int ite_suspend(struct pnp_dev *pdev, pm_message_t state)
1638 struct ite_dev *dev = pnp_get_drvdata(pdev);
1639 unsigned long flags;
1641 ite_dbg("%s called", __func__);
1643 /* wait for any transmission to end */
1644 wait_event_interruptible(dev->tx_ended, !dev->transmitting);
1646 spin_lock_irqsave(&dev->lock, flags);
1648 /* disable all interrupts */
1649 dev->params.disable(dev);
1651 spin_unlock_irqrestore(&dev->lock, flags);
1656 static int ite_resume(struct pnp_dev *pdev)
1658 struct ite_dev *dev = pnp_get_drvdata(pdev);
1659 unsigned long flags;
1661 ite_dbg("%s called", __func__);
1663 spin_lock_irqsave(&dev->lock, flags);
1665 /* reinitialize hardware config registers */
1666 dev->params.init_hardware(dev);
1667 /* enable the receiver */
1668 dev->params.enable_rx(dev);
1670 spin_unlock_irqrestore(&dev->lock, flags);
1675 static void ite_shutdown(struct pnp_dev *pdev)
1677 struct ite_dev *dev = pnp_get_drvdata(pdev);
1678 unsigned long flags;
1680 ite_dbg("%s called", __func__);
1682 spin_lock_irqsave(&dev->lock, flags);
1684 /* disable all interrupts */
1685 dev->params.disable(dev);
1687 spin_unlock_irqrestore(&dev->lock, flags);
1690 static struct pnp_driver ite_driver = {
1691 .name = ITE_DRIVER_NAME,
1692 .id_table = ite_ids,
1694 .remove = ite_remove,
1695 .suspend = ite_suspend,
1696 .resume = ite_resume,
1697 .shutdown = ite_shutdown,
1700 MODULE_DEVICE_TABLE(pnp, ite_ids);
1701 MODULE_DESCRIPTION("ITE Tech Inc. IT8712F/ITE8512F CIR driver");
1703 MODULE_AUTHOR("Juan J. Garcia de Soria <skandalfo@gmail.com>");
1704 MODULE_LICENSE("GPL");
1706 module_pnp_driver(ite_driver);