2 * Driver for ITE Tech Inc. IT8712F/IT8512 CIR
4 * Copyright (C) 2010 Juan Jesús García de Soria <skandalfo@gmail.com>
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.
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.
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
21 * Inspired by the original lirc_it87 and lirc_ite8709 drivers, on top of the
22 * skeleton provided by the nuvoton-cir driver.
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>.
29 * The lirc_ite8709 driver was written by Grégory Lardière
30 * <spmf2004-lirc@yahoo.fr> in 2008.
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/pnp.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>
48 /* module parameters */
52 module_param(debug, int, S_IRUGO | S_IWUSR);
53 MODULE_PARM_DESC(debug, "Enable debugging output");
55 /* low limit for RX carrier freq, Hz, 0 for no RX demodulation */
56 static int rx_low_carrier_freq;
57 module_param(rx_low_carrier_freq, int, S_IRUGO | S_IWUSR);
58 MODULE_PARM_DESC(rx_low_carrier_freq, "Override low RX carrier frequency, Hz, "
59 "0 for no RX demodulation");
61 /* high limit for RX carrier freq, Hz, 0 for no RX demodulation */
62 static int rx_high_carrier_freq;
63 module_param(rx_high_carrier_freq, int, S_IRUGO | S_IWUSR);
64 MODULE_PARM_DESC(rx_high_carrier_freq, "Override high RX carrier frequency, "
65 "Hz, 0 for no RX demodulation");
67 /* override tx carrier frequency */
68 static int tx_carrier_freq;
69 module_param(tx_carrier_freq, int, S_IRUGO | S_IWUSR);
70 MODULE_PARM_DESC(tx_carrier_freq, "Override TX carrier frequency, Hz");
72 /* override tx duty cycle */
73 static int tx_duty_cycle;
74 module_param(tx_duty_cycle, int, S_IRUGO | S_IWUSR);
75 MODULE_PARM_DESC(tx_duty_cycle, "Override TX duty cycle, 1-100");
77 /* override default sample period */
78 static long sample_period;
79 module_param(sample_period, long, S_IRUGO | S_IWUSR);
80 MODULE_PARM_DESC(sample_period, "Override carrier sample period, us");
82 /* override detected model id */
83 static int model_number = -1;
84 module_param(model_number, int, S_IRUGO | S_IWUSR);
85 MODULE_PARM_DESC(model_number, "Use this model number, don't autodetect");
88 /* HW-independent code functions */
90 /* check whether carrier frequency is high frequency */
91 static inline bool ite_is_high_carrier_freq(unsigned int freq)
93 return freq >= ITE_HCF_MIN_CARRIER_FREQ;
96 /* get the bits required to program the carrier frequency in CFQ bits,
98 static u8 ite_get_carrier_freq_bits(unsigned int freq)
100 if (ite_is_high_carrier_freq(freq)) {
104 else if (freq < 465000)
107 else if (freq < 490000)
114 if (freq < ITE_LCF_MIN_CARRIER_FREQ)
115 freq = ITE_LCF_MIN_CARRIER_FREQ;
116 if (freq > ITE_LCF_MAX_CARRIER_FREQ)
117 freq = ITE_LCF_MAX_CARRIER_FREQ;
119 /* convert to kHz and subtract the base freq */
121 DIV_ROUND_CLOSEST(freq - ITE_LCF_MIN_CARRIER_FREQ,
128 /* get the bits required to program the pulse with in TXMPW */
129 static u8 ite_get_pulse_width_bits(unsigned int freq, int duty_cycle)
131 unsigned long period_ns, on_ns;
133 /* sanitize freq into range */
134 if (freq < ITE_LCF_MIN_CARRIER_FREQ)
135 freq = ITE_LCF_MIN_CARRIER_FREQ;
136 if (freq > ITE_HCF_MAX_CARRIER_FREQ)
137 freq = ITE_HCF_MAX_CARRIER_FREQ;
139 period_ns = 1000000000UL / freq;
140 on_ns = period_ns * duty_cycle / 100;
142 if (ite_is_high_carrier_freq(freq)) {
146 else if (on_ns < 850)
149 else if (on_ns < 950)
152 else if (on_ns < 1080)
161 else if (on_ns < 7850)
164 else if (on_ns < 9650)
167 else if (on_ns < 11950)
175 /* decode raw bytes as received by the hardware, and push them to the ir-core
177 static void ite_decode_bytes(struct ite_dev *dev, const u8 * data, int
181 unsigned long *ldata;
182 unsigned int next_one, next_zero, size;
183 DEFINE_IR_RAW_EVENT(ev);
188 sample_period = dev->params.sample_period;
189 ldata = (unsigned long *)data;
191 next_one = find_next_bit_le(ldata, size, 0);
195 ITE_BITS_TO_NS(next_one, sample_period);
196 ir_raw_event_store_with_filter(dev->rdev, &ev);
199 while (next_one < size) {
200 next_zero = find_next_zero_bit_le(ldata, size, next_one + 1);
202 ev.duration = ITE_BITS_TO_NS(next_zero - next_one, sample_period);
203 ir_raw_event_store_with_filter(dev->rdev, &ev);
205 if (next_zero < size) {
207 find_next_bit_le(ldata,
212 ITE_BITS_TO_NS(next_one - next_zero,
214 ir_raw_event_store_with_filter
220 ir_raw_event_handle(dev->rdev);
222 ite_dbg_verbose("decoded %d bytes.", length);
225 /* set all the rx/tx carrier parameters; this must be called with the device
227 static void ite_set_carrier_params(struct ite_dev *dev)
229 unsigned int freq, low_freq, high_freq;
231 bool use_demodulator;
232 bool for_tx = dev->transmitting;
234 ite_dbg("%s called", __func__);
237 /* we don't need no stinking calculations */
238 freq = dev->params.tx_carrier_freq;
239 allowance = ITE_RXDCR_DEFAULT;
240 use_demodulator = false;
242 low_freq = dev->params.rx_low_carrier_freq;
243 high_freq = dev->params.rx_high_carrier_freq;
246 /* don't demodulate */
248 ITE_DEFAULT_CARRIER_FREQ;
249 allowance = ITE_RXDCR_DEFAULT;
250 use_demodulator = false;
252 /* calculate the middle freq */
253 freq = (low_freq + high_freq) / 2;
255 /* calculate the allowance */
257 DIV_ROUND_CLOSEST(10000 * (high_freq - low_freq),
258 ITE_RXDCR_PER_10000_STEP
259 * (high_freq + low_freq));
264 if (allowance > ITE_RXDCR_MAX)
265 allowance = ITE_RXDCR_MAX;
267 use_demodulator = true;
271 /* set the carrier parameters in a device-dependent way */
272 dev->params.set_carrier_params(dev, ite_is_high_carrier_freq(freq),
273 use_demodulator, ite_get_carrier_freq_bits(freq), allowance,
274 ite_get_pulse_width_bits(freq, dev->params.tx_duty_cycle));
277 /* interrupt service routine for incoming and outgoing CIR data */
278 static irqreturn_t ite_cir_isr(int irq, void *data)
280 struct ite_dev *dev = data;
282 irqreturn_t ret = IRQ_RETVAL(IRQ_NONE);
283 u8 rx_buf[ITE_RX_FIFO_LEN];
287 ite_dbg_verbose("%s firing", __func__);
289 /* grab the spinlock */
290 spin_lock_irqsave(&dev->lock, flags);
292 /* read the interrupt flags */
293 iflags = dev->params.get_irq_causes(dev);
295 /* Check for RX overflow */
296 if (iflags & ITE_IRQ_RX_FIFO_OVERRUN) {
297 dev_warn(&dev->rdev->dev, "receive overflow\n");
298 ir_raw_event_reset(dev->rdev);
301 /* check for the receive interrupt */
302 if (iflags & ITE_IRQ_RX_FIFO) {
303 /* read the FIFO bytes */
305 dev->params.get_rx_bytes(dev, rx_buf,
309 /* drop the spinlock, since the ir-core layer
310 * may call us back again through
312 spin_unlock_irqrestore(&dev->
316 /* decode the data we've just received */
317 ite_decode_bytes(dev, rx_buf,
320 /* reacquire the spinlock */
321 spin_lock_irqsave(&dev->lock,
324 /* mark the interrupt as serviced */
325 ret = IRQ_RETVAL(IRQ_HANDLED);
327 } else if (iflags & ITE_IRQ_TX_FIFO) {
328 /* FIFO space available interrupt */
329 ite_dbg_verbose("got interrupt for TX FIFO");
331 /* wake any sleeping transmitter */
332 wake_up_interruptible(&dev->tx_queue);
334 /* mark the interrupt as serviced */
335 ret = IRQ_RETVAL(IRQ_HANDLED);
338 /* drop the spinlock */
339 spin_unlock_irqrestore(&dev->lock, flags);
341 ite_dbg_verbose("%s done returning %d", __func__, (int)ret);
346 /* set the rx carrier freq range, guess it's in Hz... */
347 static int ite_set_rx_carrier_range(struct rc_dev *rcdev, u32 carrier_low, u32
351 struct ite_dev *dev = rcdev->priv;
353 spin_lock_irqsave(&dev->lock, flags);
354 dev->params.rx_low_carrier_freq = carrier_low;
355 dev->params.rx_high_carrier_freq = carrier_high;
356 ite_set_carrier_params(dev);
357 spin_unlock_irqrestore(&dev->lock, flags);
362 /* set the tx carrier freq, guess it's in Hz... */
363 static int ite_set_tx_carrier(struct rc_dev *rcdev, u32 carrier)
366 struct ite_dev *dev = rcdev->priv;
368 spin_lock_irqsave(&dev->lock, flags);
369 dev->params.tx_carrier_freq = carrier;
370 ite_set_carrier_params(dev);
371 spin_unlock_irqrestore(&dev->lock, flags);
376 /* set the tx duty cycle by controlling the pulse width */
377 static int ite_set_tx_duty_cycle(struct rc_dev *rcdev, u32 duty_cycle)
380 struct ite_dev *dev = rcdev->priv;
382 spin_lock_irqsave(&dev->lock, flags);
383 dev->params.tx_duty_cycle = duty_cycle;
384 ite_set_carrier_params(dev);
385 spin_unlock_irqrestore(&dev->lock, flags);
390 /* transmit out IR pulses; what you get here is a batch of alternating
391 * pulse/space/pulse/space lengths that we should write out completely through
392 * the FIFO, blocking on a full FIFO */
393 static int ite_tx_ir(struct rc_dev *rcdev, unsigned *txbuf, unsigned n)
396 struct ite_dev *dev = rcdev->priv;
397 bool is_pulse = false;
398 int remaining_us, fifo_avail, fifo_remaining, last_idx = 0;
399 int max_rle_us, next_rle_us;
401 u8 last_sent[ITE_TX_FIFO_LEN];
404 ite_dbg("%s called", __func__);
406 /* clear the array just in case */
407 memset(last_sent, 0, ARRAY_SIZE(last_sent));
409 spin_lock_irqsave(&dev->lock, flags);
411 /* let everybody know we're now transmitting */
412 dev->transmitting = true;
414 /* and set the carrier values for transmission */
415 ite_set_carrier_params(dev);
417 /* calculate how much time we can send in one byte */
419 (ITE_BAUDRATE_DIVISOR * dev->params.sample_period *
420 ITE_TX_MAX_RLE) / 1000;
422 /* disable the receiver */
423 dev->params.disable_rx(dev);
425 /* this is where we'll begin filling in the FIFO, until it's full.
426 * then we'll just activate the interrupt, wait for it to wake us up
427 * again, disable it, continue filling the FIFO... until everything
428 * has been pushed out */
430 ITE_TX_FIFO_LEN - dev->params.get_tx_used_slots(dev);
432 while (n > 0 && dev->in_use) {
433 /* transmit the next sample */
434 is_pulse = !is_pulse;
435 remaining_us = *(txbuf++);
439 ((is_pulse) ? "pulse" : "space"),
443 /* repeat while the pulse is non-zero length */
444 while (remaining_us > 0 && dev->in_use) {
445 if (remaining_us > max_rle_us)
446 next_rle_us = max_rle_us;
449 next_rle_us = remaining_us;
451 remaining_us -= next_rle_us;
453 /* check what's the length we have to pump out */
454 val = (ITE_TX_MAX_RLE * next_rle_us) / max_rle_us;
456 /* put it into the sent buffer */
457 last_sent[last_idx++] = val;
458 last_idx &= (ITE_TX_FIFO_LEN);
460 /* encode it for 7 bits */
461 val = (val - 1) & ITE_TX_RLE_MASK;
463 /* take into account pulse/space prefix */
471 * if we get to 0 available, read again, just in case
472 * some other slot got freed
475 fifo_avail = ITE_TX_FIFO_LEN - dev->params.get_tx_used_slots(dev);
477 /* if it's still full */
478 if (fifo_avail <= 0) {
479 /* enable the tx interrupt */
481 enable_tx_interrupt(dev);
483 /* drop the spinlock */
484 spin_unlock_irqrestore(&dev->lock, flags);
486 /* wait for the FIFO to empty enough */
487 wait_event_interruptible(dev->tx_queue, (fifo_avail = ITE_TX_FIFO_LEN - dev->params.get_tx_used_slots(dev)) >= 8);
489 /* get the spinlock again */
490 spin_lock_irqsave(&dev->lock, flags);
492 /* disable the tx interrupt again. */
494 disable_tx_interrupt(dev);
497 /* now send the byte through the FIFO */
498 dev->params.put_tx_byte(dev, val);
503 /* wait and don't return until the whole FIFO has been sent out;
504 * otherwise we could configure the RX carrier params instead of the
505 * TX ones while the transmission is still being performed! */
506 fifo_remaining = dev->params.get_tx_used_slots(dev);
508 while (fifo_remaining > 0) {
511 last_idx &= (ITE_TX_FIFO_LEN - 1);
512 remaining_us += last_sent[last_idx];
514 remaining_us = (remaining_us * max_rle_us) / (ITE_TX_MAX_RLE);
516 /* drop the spinlock while we sleep */
517 spin_unlock_irqrestore(&dev->lock, flags);
519 /* sleep remaining_us microseconds */
520 mdelay(DIV_ROUND_UP(remaining_us, 1000));
522 /* reacquire the spinlock */
523 spin_lock_irqsave(&dev->lock, flags);
525 /* now we're not transmitting anymore */
526 dev->transmitting = false;
528 /* and set the carrier values for reception */
529 ite_set_carrier_params(dev);
531 /* reenable the receiver */
533 dev->params.enable_rx(dev);
535 /* notify transmission end */
536 wake_up_interruptible(&dev->tx_ended);
538 spin_unlock_irqrestore(&dev->lock, flags);
543 /* idle the receiver if needed */
544 static void ite_s_idle(struct rc_dev *rcdev, bool enable)
547 struct ite_dev *dev = rcdev->priv;
549 ite_dbg("%s called", __func__);
552 spin_lock_irqsave(&dev->lock, flags);
553 dev->params.idle_rx(dev);
554 spin_unlock_irqrestore(&dev->lock, flags);
559 /* IT8712F HW-specific functions */
561 /* retrieve a bitmask of the current causes for a pending interrupt; this may
562 * be composed of ITE_IRQ_TX_FIFO, ITE_IRQ_RX_FIFO and ITE_IRQ_RX_FIFO_OVERRUN
564 static int it87_get_irq_causes(struct ite_dev *dev)
569 ite_dbg("%s called", __func__);
571 /* read the interrupt flags */
572 iflags = inb(dev->cir_addr + IT87_IIR) & IT87_II;
576 ret = ITE_IRQ_RX_FIFO;
579 ret = ITE_IRQ_RX_FIFO_OVERRUN;
582 ret = ITE_IRQ_TX_FIFO;
589 /* set the carrier parameters; to be called with the spinlock held */
590 static void it87_set_carrier_params(struct ite_dev *dev, bool high_freq,
591 bool use_demodulator,
592 u8 carrier_freq_bits, u8 allowance_bits,
597 ite_dbg("%s called", __func__);
599 /* program the RCR register */
600 val = inb(dev->cir_addr + IT87_RCR)
601 & ~(IT87_HCFS | IT87_RXEND | IT87_RXDCR);
609 val |= allowance_bits;
611 outb(val, dev->cir_addr + IT87_RCR);
613 /* program the TCR2 register */
614 outb((carrier_freq_bits << IT87_CFQ_SHIFT) | pulse_width_bits,
615 dev->cir_addr + IT87_TCR2);
618 /* read up to buf_size bytes from the RX FIFO; to be called with the spinlock
620 static int it87_get_rx_bytes(struct ite_dev *dev, u8 * buf, int buf_size)
624 ite_dbg("%s called", __func__);
626 /* read how many bytes are still in the FIFO */
627 fifo = inb(dev->cir_addr + IT87_RSR) & IT87_RXFBC;
629 while (fifo > 0 && buf_size > 0) {
630 *(buf++) = inb(dev->cir_addr + IT87_DR);
639 /* return how many bytes are still in the FIFO; this will be called
640 * with the device spinlock NOT HELD while waiting for the TX FIFO to get
641 * empty; let's expect this won't be a problem */
642 static int it87_get_tx_used_slots(struct ite_dev *dev)
644 ite_dbg("%s called", __func__);
646 return inb(dev->cir_addr + IT87_TSR) & IT87_TXFBC;
649 /* put a byte to the TX fifo; this should be called with the spinlock held */
650 static void it87_put_tx_byte(struct ite_dev *dev, u8 value)
652 outb(value, dev->cir_addr + IT87_DR);
655 /* idle the receiver so that we won't receive samples until another
656 pulse is detected; this must be called with the device spinlock held */
657 static void it87_idle_rx(struct ite_dev *dev)
659 ite_dbg("%s called", __func__);
661 /* disable streaming by clearing RXACT writing it as 1 */
662 outb(inb(dev->cir_addr + IT87_RCR) | IT87_RXACT,
663 dev->cir_addr + IT87_RCR);
666 outb(inb(dev->cir_addr + IT87_TCR1) | IT87_FIFOCLR,
667 dev->cir_addr + IT87_TCR1);
670 /* disable the receiver; this must be called with the device spinlock held */
671 static void it87_disable_rx(struct ite_dev *dev)
673 ite_dbg("%s called", __func__);
675 /* disable the receiver interrupts */
676 outb(inb(dev->cir_addr + IT87_IER) & ~(IT87_RDAIE | IT87_RFOIE),
677 dev->cir_addr + IT87_IER);
679 /* disable the receiver */
680 outb(inb(dev->cir_addr + IT87_RCR) & ~IT87_RXEN,
681 dev->cir_addr + IT87_RCR);
683 /* clear the FIFO and RXACT (actually RXACT should have been cleared
684 * in the previous outb() call) */
688 /* enable the receiver; this must be called with the device spinlock held */
689 static void it87_enable_rx(struct ite_dev *dev)
691 ite_dbg("%s called", __func__);
693 /* enable the receiver by setting RXEN */
694 outb(inb(dev->cir_addr + IT87_RCR) | IT87_RXEN,
695 dev->cir_addr + IT87_RCR);
697 /* just prepare it to idle for the next reception */
700 /* enable the receiver interrupts and master enable flag */
701 outb(inb(dev->cir_addr + IT87_IER) | IT87_RDAIE | IT87_RFOIE | IT87_IEC,
702 dev->cir_addr + IT87_IER);
705 /* disable the transmitter interrupt; this must be called with the device
707 static void it87_disable_tx_interrupt(struct ite_dev *dev)
709 ite_dbg("%s called", __func__);
711 /* disable the transmitter interrupts */
712 outb(inb(dev->cir_addr + IT87_IER) & ~IT87_TLDLIE,
713 dev->cir_addr + IT87_IER);
716 /* enable the transmitter interrupt; this must be called with the device
718 static void it87_enable_tx_interrupt(struct ite_dev *dev)
720 ite_dbg("%s called", __func__);
722 /* enable the transmitter interrupts and master enable flag */
723 outb(inb(dev->cir_addr + IT87_IER) | IT87_TLDLIE | IT87_IEC,
724 dev->cir_addr + IT87_IER);
727 /* disable the device; this must be called with the device spinlock held */
728 static void it87_disable(struct ite_dev *dev)
730 ite_dbg("%s called", __func__);
732 /* clear out all interrupt enable flags */
733 outb(inb(dev->cir_addr + IT87_IER) &
734 ~(IT87_IEC | IT87_RFOIE | IT87_RDAIE | IT87_TLDLIE),
735 dev->cir_addr + IT87_IER);
737 /* disable the receiver */
738 it87_disable_rx(dev);
741 outb(IT87_FIFOCLR | inb(dev->cir_addr + IT87_TCR1),
742 dev->cir_addr + IT87_TCR1);
745 /* initialize the hardware */
746 static void it87_init_hardware(struct ite_dev *dev)
748 ite_dbg("%s called", __func__);
750 /* enable just the baud rate divisor register,
751 disabling all the interrupts at the same time */
752 outb((inb(dev->cir_addr + IT87_IER) &
753 ~(IT87_IEC | IT87_RFOIE | IT87_RDAIE | IT87_TLDLIE)) | IT87_BR,
754 dev->cir_addr + IT87_IER);
756 /* write out the baud rate divisor */
757 outb(ITE_BAUDRATE_DIVISOR & 0xff, dev->cir_addr + IT87_BDLR);
758 outb((ITE_BAUDRATE_DIVISOR >> 8) & 0xff, dev->cir_addr + IT87_BDHR);
760 /* disable the baud rate divisor register again */
761 outb(inb(dev->cir_addr + IT87_IER) & ~IT87_BR,
762 dev->cir_addr + IT87_IER);
764 /* program the RCR register defaults */
765 outb(ITE_RXDCR_DEFAULT, dev->cir_addr + IT87_RCR);
767 /* program the TCR1 register */
768 outb(IT87_TXMPM_DEFAULT | IT87_TXENDF | IT87_TXRLE
769 | IT87_FIFOTL_DEFAULT | IT87_FIFOCLR,
770 dev->cir_addr + IT87_TCR1);
772 /* program the carrier parameters */
773 ite_set_carrier_params(dev);
776 /* IT8512F on ITE8708 HW-specific functions */
778 /* retrieve a bitmask of the current causes for a pending interrupt; this may
779 * be composed of ITE_IRQ_TX_FIFO, ITE_IRQ_RX_FIFO and ITE_IRQ_RX_FIFO_OVERRUN
781 static int it8708_get_irq_causes(struct ite_dev *dev)
786 ite_dbg("%s called", __func__);
788 /* read the interrupt flags */
789 iflags = inb(dev->cir_addr + IT8708_C0IIR);
791 if (iflags & IT85_TLDLI)
792 ret |= ITE_IRQ_TX_FIFO;
793 if (iflags & IT85_RDAI)
794 ret |= ITE_IRQ_RX_FIFO;
795 if (iflags & IT85_RFOI)
796 ret |= ITE_IRQ_RX_FIFO_OVERRUN;
801 /* set the carrier parameters; to be called with the spinlock held */
802 static void it8708_set_carrier_params(struct ite_dev *dev, bool high_freq,
803 bool use_demodulator,
804 u8 carrier_freq_bits, u8 allowance_bits,
809 ite_dbg("%s called", __func__);
811 /* program the C0CFR register, with HRAE=1 */
812 outb(inb(dev->cir_addr + IT8708_BANKSEL) | IT8708_HRAE,
813 dev->cir_addr + IT8708_BANKSEL);
815 val = (inb(dev->cir_addr + IT8708_C0CFR)
816 & ~(IT85_HCFS | IT85_CFQ)) | carrier_freq_bits;
821 outb(val, dev->cir_addr + IT8708_C0CFR);
823 outb(inb(dev->cir_addr + IT8708_BANKSEL) & ~IT8708_HRAE,
824 dev->cir_addr + IT8708_BANKSEL);
826 /* program the C0RCR register */
827 val = inb(dev->cir_addr + IT8708_C0RCR)
828 & ~(IT85_RXEND | IT85_RXDCR);
833 val |= allowance_bits;
835 outb(val, dev->cir_addr + IT8708_C0RCR);
837 /* program the C0TCR register */
838 val = inb(dev->cir_addr + IT8708_C0TCR) & ~IT85_TXMPW;
839 val |= pulse_width_bits;
840 outb(val, dev->cir_addr + IT8708_C0TCR);
843 /* read up to buf_size bytes from the RX FIFO; to be called with the spinlock
845 static int it8708_get_rx_bytes(struct ite_dev *dev, u8 * buf, int buf_size)
849 ite_dbg("%s called", __func__);
851 /* read how many bytes are still in the FIFO */
852 fifo = inb(dev->cir_addr + IT8708_C0RFSR) & IT85_RXFBC;
854 while (fifo > 0 && buf_size > 0) {
855 *(buf++) = inb(dev->cir_addr + IT8708_C0DR);
864 /* return how many bytes are still in the FIFO; this will be called
865 * with the device spinlock NOT HELD while waiting for the TX FIFO to get
866 * empty; let's expect this won't be a problem */
867 static int it8708_get_tx_used_slots(struct ite_dev *dev)
869 ite_dbg("%s called", __func__);
871 return inb(dev->cir_addr + IT8708_C0TFSR) & IT85_TXFBC;
874 /* put a byte to the TX fifo; this should be called with the spinlock held */
875 static void it8708_put_tx_byte(struct ite_dev *dev, u8 value)
877 outb(value, dev->cir_addr + IT8708_C0DR);
880 /* idle the receiver so that we won't receive samples until another
881 pulse is detected; this must be called with the device spinlock held */
882 static void it8708_idle_rx(struct ite_dev *dev)
884 ite_dbg("%s called", __func__);
886 /* disable streaming by clearing RXACT writing it as 1 */
887 outb(inb(dev->cir_addr + IT8708_C0RCR) | IT85_RXACT,
888 dev->cir_addr + IT8708_C0RCR);
891 outb(inb(dev->cir_addr + IT8708_C0MSTCR) | IT85_FIFOCLR,
892 dev->cir_addr + IT8708_C0MSTCR);
895 /* disable the receiver; this must be called with the device spinlock held */
896 static void it8708_disable_rx(struct ite_dev *dev)
898 ite_dbg("%s called", __func__);
900 /* disable the receiver interrupts */
901 outb(inb(dev->cir_addr + IT8708_C0IER) &
902 ~(IT85_RDAIE | IT85_RFOIE),
903 dev->cir_addr + IT8708_C0IER);
905 /* disable the receiver */
906 outb(inb(dev->cir_addr + IT8708_C0RCR) & ~IT85_RXEN,
907 dev->cir_addr + IT8708_C0RCR);
909 /* clear the FIFO and RXACT (actually RXACT should have been cleared
910 * in the previous outb() call) */
914 /* enable the receiver; this must be called with the device spinlock held */
915 static void it8708_enable_rx(struct ite_dev *dev)
917 ite_dbg("%s called", __func__);
919 /* enable the receiver by setting RXEN */
920 outb(inb(dev->cir_addr + IT8708_C0RCR) | IT85_RXEN,
921 dev->cir_addr + IT8708_C0RCR);
923 /* just prepare it to idle for the next reception */
926 /* enable the receiver interrupts and master enable flag */
927 outb(inb(dev->cir_addr + IT8708_C0IER)
928 |IT85_RDAIE | IT85_RFOIE | IT85_IEC,
929 dev->cir_addr + IT8708_C0IER);
932 /* disable the transmitter interrupt; this must be called with the device
934 static void it8708_disable_tx_interrupt(struct ite_dev *dev)
936 ite_dbg("%s called", __func__);
938 /* disable the transmitter interrupts */
939 outb(inb(dev->cir_addr + IT8708_C0IER) & ~IT85_TLDLIE,
940 dev->cir_addr + IT8708_C0IER);
943 /* enable the transmitter interrupt; this must be called with the device
945 static void it8708_enable_tx_interrupt(struct ite_dev *dev)
947 ite_dbg("%s called", __func__);
949 /* enable the transmitter interrupts and master enable flag */
950 outb(inb(dev->cir_addr + IT8708_C0IER)
951 |IT85_TLDLIE | IT85_IEC,
952 dev->cir_addr + IT8708_C0IER);
955 /* disable the device; this must be called with the device spinlock held */
956 static void it8708_disable(struct ite_dev *dev)
958 ite_dbg("%s called", __func__);
960 /* clear out all interrupt enable flags */
961 outb(inb(dev->cir_addr + IT8708_C0IER) &
962 ~(IT85_IEC | IT85_RFOIE | IT85_RDAIE | IT85_TLDLIE),
963 dev->cir_addr + IT8708_C0IER);
965 /* disable the receiver */
966 it8708_disable_rx(dev);
969 outb(IT85_FIFOCLR | inb(dev->cir_addr + IT8708_C0MSTCR),
970 dev->cir_addr + IT8708_C0MSTCR);
973 /* initialize the hardware */
974 static void it8708_init_hardware(struct ite_dev *dev)
976 ite_dbg("%s called", __func__);
978 /* disable all the interrupts */
979 outb(inb(dev->cir_addr + IT8708_C0IER) &
980 ~(IT85_IEC | IT85_RFOIE | IT85_RDAIE | IT85_TLDLIE),
981 dev->cir_addr + IT8708_C0IER);
983 /* program the baud rate divisor */
984 outb(inb(dev->cir_addr + IT8708_BANKSEL) | IT8708_HRAE,
985 dev->cir_addr + IT8708_BANKSEL);
987 outb(ITE_BAUDRATE_DIVISOR & 0xff, dev->cir_addr + IT8708_C0BDLR);
988 outb((ITE_BAUDRATE_DIVISOR >> 8) & 0xff,
989 dev->cir_addr + IT8708_C0BDHR);
991 outb(inb(dev->cir_addr + IT8708_BANKSEL) & ~IT8708_HRAE,
992 dev->cir_addr + IT8708_BANKSEL);
994 /* program the C0MSTCR register defaults */
995 outb((inb(dev->cir_addr + IT8708_C0MSTCR) &
996 ~(IT85_ILSEL | IT85_ILE | IT85_FIFOTL |
997 IT85_FIFOCLR | IT85_RESET)) |
999 dev->cir_addr + IT8708_C0MSTCR);
1001 /* program the C0RCR register defaults */
1002 outb((inb(dev->cir_addr + IT8708_C0RCR) &
1003 ~(IT85_RXEN | IT85_RDWOS | IT85_RXEND |
1004 IT85_RXACT | IT85_RXDCR)) |
1006 dev->cir_addr + IT8708_C0RCR);
1008 /* program the C0TCR register defaults */
1009 outb((inb(dev->cir_addr + IT8708_C0TCR) &
1010 ~(IT85_TXMPM | IT85_TXMPW))
1011 |IT85_TXRLE | IT85_TXENDF |
1012 IT85_TXMPM_DEFAULT | IT85_TXMPW_DEFAULT,
1013 dev->cir_addr + IT8708_C0TCR);
1015 /* program the carrier parameters */
1016 ite_set_carrier_params(dev);
1019 /* IT8512F on ITE8709 HW-specific functions */
1021 /* read a byte from the SRAM module */
1022 static inline u8 it8709_rm(struct ite_dev *dev, int index)
1024 outb(index, dev->cir_addr + IT8709_RAM_IDX);
1025 return inb(dev->cir_addr + IT8709_RAM_VAL);
1028 /* write a byte to the SRAM module */
1029 static inline void it8709_wm(struct ite_dev *dev, u8 val, int index)
1031 outb(index, dev->cir_addr + IT8709_RAM_IDX);
1032 outb(val, dev->cir_addr + IT8709_RAM_VAL);
1035 static void it8709_wait(struct ite_dev *dev)
1039 * loop until device tells it's ready to continue
1040 * iterations count is usually ~750 but can sometimes achieve 13000
1042 for (i = 0; i < 15000; i++) {
1044 if (it8709_rm(dev, IT8709_MODE) == IT8709_IDLE)
1049 /* read the value of a CIR register */
1050 static u8 it8709_rr(struct ite_dev *dev, int index)
1052 /* just wait in case the previous access was a write */
1054 it8709_wm(dev, index, IT8709_REG_IDX);
1055 it8709_wm(dev, IT8709_READ, IT8709_MODE);
1057 /* wait for the read data to be available */
1060 /* return the read value */
1061 return it8709_rm(dev, IT8709_REG_VAL);
1064 /* write the value of a CIR register */
1065 static void it8709_wr(struct ite_dev *dev, u8 val, int index)
1067 /* we wait before writing, and not afterwards, since this allows us to
1068 * pipeline the host CPU with the microcontroller */
1070 it8709_wm(dev, val, IT8709_REG_VAL);
1071 it8709_wm(dev, index, IT8709_REG_IDX);
1072 it8709_wm(dev, IT8709_WRITE, IT8709_MODE);
1075 /* retrieve a bitmask of the current causes for a pending interrupt; this may
1076 * be composed of ITE_IRQ_TX_FIFO, ITE_IRQ_RX_FIFO and ITE_IRQ_RX_FIFO_OVERRUN
1078 static int it8709_get_irq_causes(struct ite_dev *dev)
1083 ite_dbg("%s called", __func__);
1085 /* read the interrupt flags */
1086 iflags = it8709_rm(dev, IT8709_IIR);
1088 if (iflags & IT85_TLDLI)
1089 ret |= ITE_IRQ_TX_FIFO;
1090 if (iflags & IT85_RDAI)
1091 ret |= ITE_IRQ_RX_FIFO;
1092 if (iflags & IT85_RFOI)
1093 ret |= ITE_IRQ_RX_FIFO_OVERRUN;
1098 /* set the carrier parameters; to be called with the spinlock held */
1099 static void it8709_set_carrier_params(struct ite_dev *dev, bool high_freq,
1100 bool use_demodulator,
1101 u8 carrier_freq_bits, u8 allowance_bits,
1102 u8 pulse_width_bits)
1106 ite_dbg("%s called", __func__);
1108 val = (it8709_rr(dev, IT85_C0CFR)
1109 &~(IT85_HCFS | IT85_CFQ)) |
1115 it8709_wr(dev, val, IT85_C0CFR);
1117 /* program the C0RCR register */
1118 val = it8709_rr(dev, IT85_C0RCR)
1119 & ~(IT85_RXEND | IT85_RXDCR);
1121 if (use_demodulator)
1124 val |= allowance_bits;
1126 it8709_wr(dev, val, IT85_C0RCR);
1128 /* program the C0TCR register */
1129 val = it8709_rr(dev, IT85_C0TCR) & ~IT85_TXMPW;
1130 val |= pulse_width_bits;
1131 it8709_wr(dev, val, IT85_C0TCR);
1134 /* read up to buf_size bytes from the RX FIFO; to be called with the spinlock
1136 static int it8709_get_rx_bytes(struct ite_dev *dev, u8 * buf, int buf_size)
1140 ite_dbg("%s called", __func__);
1142 /* read how many bytes are still in the FIFO */
1143 fifo = it8709_rm(dev, IT8709_RFSR) & IT85_RXFBC;
1145 while (fifo > 0 && buf_size > 0) {
1146 *(buf++) = it8709_rm(dev, IT8709_FIFO + read);
1152 /* 'clear' the FIFO by setting the writing index to 0; this is
1153 * completely bound to be racy, but we can't help it, since it's a
1154 * limitation of the protocol */
1155 it8709_wm(dev, 0, IT8709_RFSR);
1160 /* return how many bytes are still in the FIFO; this will be called
1161 * with the device spinlock NOT HELD while waiting for the TX FIFO to get
1162 * empty; let's expect this won't be a problem */
1163 static int it8709_get_tx_used_slots(struct ite_dev *dev)
1165 ite_dbg("%s called", __func__);
1167 return it8709_rr(dev, IT85_C0TFSR) & IT85_TXFBC;
1170 /* put a byte to the TX fifo; this should be called with the spinlock held */
1171 static void it8709_put_tx_byte(struct ite_dev *dev, u8 value)
1173 it8709_wr(dev, value, IT85_C0DR);
1176 /* idle the receiver so that we won't receive samples until another
1177 pulse is detected; this must be called with the device spinlock held */
1178 static void it8709_idle_rx(struct ite_dev *dev)
1180 ite_dbg("%s called", __func__);
1182 /* disable streaming by clearing RXACT writing it as 1 */
1183 it8709_wr(dev, it8709_rr(dev, IT85_C0RCR) | IT85_RXACT,
1186 /* clear the FIFO */
1187 it8709_wr(dev, it8709_rr(dev, IT85_C0MSTCR) | IT85_FIFOCLR,
1191 /* disable the receiver; this must be called with the device spinlock held */
1192 static void it8709_disable_rx(struct ite_dev *dev)
1194 ite_dbg("%s called", __func__);
1196 /* disable the receiver interrupts */
1197 it8709_wr(dev, it8709_rr(dev, IT85_C0IER) &
1198 ~(IT85_RDAIE | IT85_RFOIE),
1201 /* disable the receiver */
1202 it8709_wr(dev, it8709_rr(dev, IT85_C0RCR) & ~IT85_RXEN,
1205 /* clear the FIFO and RXACT (actually RXACT should have been cleared
1206 * in the previous it8709_wr(dev, ) call) */
1207 it8709_idle_rx(dev);
1210 /* enable the receiver; this must be called with the device spinlock held */
1211 static void it8709_enable_rx(struct ite_dev *dev)
1213 ite_dbg("%s called", __func__);
1215 /* enable the receiver by setting RXEN */
1216 it8709_wr(dev, it8709_rr(dev, IT85_C0RCR) | IT85_RXEN,
1219 /* just prepare it to idle for the next reception */
1220 it8709_idle_rx(dev);
1222 /* enable the receiver interrupts and master enable flag */
1223 it8709_wr(dev, it8709_rr(dev, IT85_C0IER)
1224 |IT85_RDAIE | IT85_RFOIE | IT85_IEC,
1228 /* disable the transmitter interrupt; this must be called with the device
1230 static void it8709_disable_tx_interrupt(struct ite_dev *dev)
1232 ite_dbg("%s called", __func__);
1234 /* disable the transmitter interrupts */
1235 it8709_wr(dev, it8709_rr(dev, IT85_C0IER) & ~IT85_TLDLIE,
1239 /* enable the transmitter interrupt; this must be called with the device
1241 static void it8709_enable_tx_interrupt(struct ite_dev *dev)
1243 ite_dbg("%s called", __func__);
1245 /* enable the transmitter interrupts and master enable flag */
1246 it8709_wr(dev, it8709_rr(dev, IT85_C0IER)
1247 |IT85_TLDLIE | IT85_IEC,
1251 /* disable the device; this must be called with the device spinlock held */
1252 static void it8709_disable(struct ite_dev *dev)
1254 ite_dbg("%s called", __func__);
1256 /* clear out all interrupt enable flags */
1257 it8709_wr(dev, it8709_rr(dev, IT85_C0IER) &
1258 ~(IT85_IEC | IT85_RFOIE | IT85_RDAIE | IT85_TLDLIE),
1261 /* disable the receiver */
1262 it8709_disable_rx(dev);
1264 /* erase the FIFO */
1265 it8709_wr(dev, IT85_FIFOCLR | it8709_rr(dev, IT85_C0MSTCR),
1269 /* initialize the hardware */
1270 static void it8709_init_hardware(struct ite_dev *dev)
1272 ite_dbg("%s called", __func__);
1274 /* disable all the interrupts */
1275 it8709_wr(dev, it8709_rr(dev, IT85_C0IER) &
1276 ~(IT85_IEC | IT85_RFOIE | IT85_RDAIE | IT85_TLDLIE),
1279 /* program the baud rate divisor */
1280 it8709_wr(dev, ITE_BAUDRATE_DIVISOR & 0xff, IT85_C0BDLR);
1281 it8709_wr(dev, (ITE_BAUDRATE_DIVISOR >> 8) & 0xff,
1284 /* program the C0MSTCR register defaults */
1285 it8709_wr(dev, (it8709_rr(dev, IT85_C0MSTCR) &
1286 ~(IT85_ILSEL | IT85_ILE | IT85_FIFOTL
1287 | IT85_FIFOCLR | IT85_RESET)) | IT85_FIFOTL_DEFAULT,
1290 /* program the C0RCR register defaults */
1291 it8709_wr(dev, (it8709_rr(dev, IT85_C0RCR) &
1292 ~(IT85_RXEN | IT85_RDWOS | IT85_RXEND | IT85_RXACT
1293 | IT85_RXDCR)) | ITE_RXDCR_DEFAULT,
1296 /* program the C0TCR register defaults */
1297 it8709_wr(dev, (it8709_rr(dev, IT85_C0TCR) & ~(IT85_TXMPM | IT85_TXMPW))
1298 | IT85_TXRLE | IT85_TXENDF | IT85_TXMPM_DEFAULT
1299 | IT85_TXMPW_DEFAULT,
1302 /* program the carrier parameters */
1303 ite_set_carrier_params(dev);
1307 /* generic hardware setup/teardown code */
1309 /* activate the device for use */
1310 static int ite_open(struct rc_dev *rcdev)
1312 struct ite_dev *dev = rcdev->priv;
1313 unsigned long flags;
1315 ite_dbg("%s called", __func__);
1317 spin_lock_irqsave(&dev->lock, flags);
1320 /* enable the receiver */
1321 dev->params.enable_rx(dev);
1323 spin_unlock_irqrestore(&dev->lock, flags);
1328 /* deactivate the device for use */
1329 static void ite_close(struct rc_dev *rcdev)
1331 struct ite_dev *dev = rcdev->priv;
1332 unsigned long flags;
1334 ite_dbg("%s called", __func__);
1336 spin_lock_irqsave(&dev->lock, flags);
1337 dev->in_use = false;
1339 /* wait for any transmission to end */
1340 spin_unlock_irqrestore(&dev->lock, flags);
1341 wait_event_interruptible(dev->tx_ended, !dev->transmitting);
1342 spin_lock_irqsave(&dev->lock, flags);
1344 dev->params.disable(dev);
1346 spin_unlock_irqrestore(&dev->lock, flags);
1349 /* supported models and their parameters */
1350 static const struct ite_dev_params ite_dev_descs[] = {
1352 .model = "ITE8704 CIR transceiver",
1353 .io_region_size = IT87_IOREG_LENGTH,
1355 .hw_tx_capable = true,
1356 .sample_period = (u32) (1000000000ULL / 115200),
1357 .tx_carrier_freq = 38000,
1358 .tx_duty_cycle = 33,
1359 .rx_low_carrier_freq = 0,
1360 .rx_high_carrier_freq = 0,
1363 .get_irq_causes = it87_get_irq_causes,
1364 .enable_rx = it87_enable_rx,
1365 .idle_rx = it87_idle_rx,
1366 .disable_rx = it87_idle_rx,
1367 .get_rx_bytes = it87_get_rx_bytes,
1368 .enable_tx_interrupt = it87_enable_tx_interrupt,
1369 .disable_tx_interrupt = it87_disable_tx_interrupt,
1370 .get_tx_used_slots = it87_get_tx_used_slots,
1371 .put_tx_byte = it87_put_tx_byte,
1372 .disable = it87_disable,
1373 .init_hardware = it87_init_hardware,
1374 .set_carrier_params = it87_set_carrier_params,
1377 .model = "ITE8713 CIR transceiver",
1378 .io_region_size = IT87_IOREG_LENGTH,
1380 .hw_tx_capable = true,
1381 .sample_period = (u32) (1000000000ULL / 115200),
1382 .tx_carrier_freq = 38000,
1383 .tx_duty_cycle = 33,
1384 .rx_low_carrier_freq = 0,
1385 .rx_high_carrier_freq = 0,
1388 .get_irq_causes = it87_get_irq_causes,
1389 .enable_rx = it87_enable_rx,
1390 .idle_rx = it87_idle_rx,
1391 .disable_rx = it87_idle_rx,
1392 .get_rx_bytes = it87_get_rx_bytes,
1393 .enable_tx_interrupt = it87_enable_tx_interrupt,
1394 .disable_tx_interrupt = it87_disable_tx_interrupt,
1395 .get_tx_used_slots = it87_get_tx_used_slots,
1396 .put_tx_byte = it87_put_tx_byte,
1397 .disable = it87_disable,
1398 .init_hardware = it87_init_hardware,
1399 .set_carrier_params = it87_set_carrier_params,
1402 .model = "ITE8708 CIR transceiver",
1403 .io_region_size = IT8708_IOREG_LENGTH,
1405 .hw_tx_capable = true,
1406 .sample_period = (u32) (1000000000ULL / 115200),
1407 .tx_carrier_freq = 38000,
1408 .tx_duty_cycle = 33,
1409 .rx_low_carrier_freq = 0,
1410 .rx_high_carrier_freq = 0,
1413 .get_irq_causes = it8708_get_irq_causes,
1414 .enable_rx = it8708_enable_rx,
1415 .idle_rx = it8708_idle_rx,
1416 .disable_rx = it8708_idle_rx,
1417 .get_rx_bytes = it8708_get_rx_bytes,
1418 .enable_tx_interrupt = it8708_enable_tx_interrupt,
1419 .disable_tx_interrupt =
1420 it8708_disable_tx_interrupt,
1421 .get_tx_used_slots = it8708_get_tx_used_slots,
1422 .put_tx_byte = it8708_put_tx_byte,
1423 .disable = it8708_disable,
1424 .init_hardware = it8708_init_hardware,
1425 .set_carrier_params = it8708_set_carrier_params,
1428 .model = "ITE8709 CIR transceiver",
1429 .io_region_size = IT8709_IOREG_LENGTH,
1431 .hw_tx_capable = true,
1432 .sample_period = (u32) (1000000000ULL / 115200),
1433 .tx_carrier_freq = 38000,
1434 .tx_duty_cycle = 33,
1435 .rx_low_carrier_freq = 0,
1436 .rx_high_carrier_freq = 0,
1439 .get_irq_causes = it8709_get_irq_causes,
1440 .enable_rx = it8709_enable_rx,
1441 .idle_rx = it8709_idle_rx,
1442 .disable_rx = it8709_idle_rx,
1443 .get_rx_bytes = it8709_get_rx_bytes,
1444 .enable_tx_interrupt = it8709_enable_tx_interrupt,
1445 .disable_tx_interrupt =
1446 it8709_disable_tx_interrupt,
1447 .get_tx_used_slots = it8709_get_tx_used_slots,
1448 .put_tx_byte = it8709_put_tx_byte,
1449 .disable = it8709_disable,
1450 .init_hardware = it8709_init_hardware,
1451 .set_carrier_params = it8709_set_carrier_params,
1455 static const struct pnp_device_id ite_ids[] = {
1456 {"ITE8704", 0}, /* Default model */
1457 {"ITE8713", 1}, /* CIR found in EEEBox 1501U */
1458 {"ITE8708", 2}, /* Bridged IT8512 */
1459 {"ITE8709", 3}, /* SRAM-Bridged IT8512 */
1463 /* allocate memory, probe hardware, and initialize everything */
1464 static int ite_probe(struct pnp_dev *pdev, const struct pnp_device_id
1467 const struct ite_dev_params *dev_desc = NULL;
1468 struct ite_dev *itdev = NULL;
1469 struct rc_dev *rdev = NULL;
1474 ite_dbg("%s called", __func__);
1476 itdev = kzalloc(sizeof(struct ite_dev), GFP_KERNEL);
1480 /* input device for IR remote (and tx) */
1481 rdev = rc_allocate_device();
1483 goto exit_free_dev_rdev;
1488 /* get the model number */
1489 model_no = (int)dev_id->driver_data;
1490 ite_pr(KERN_NOTICE, "Auto-detected model: %s\n",
1491 ite_dev_descs[model_no].model);
1493 if (model_number >= 0 && model_number < ARRAY_SIZE(ite_dev_descs)) {
1494 model_no = model_number;
1495 ite_pr(KERN_NOTICE, "The model has been fixed by a module "
1499 ite_pr(KERN_NOTICE, "Using model: %s\n", ite_dev_descs[model_no].model);
1501 /* get the description for the device */
1502 dev_desc = &ite_dev_descs[model_no];
1503 io_rsrc_no = dev_desc->io_rsrc_no;
1505 /* validate pnp resources */
1506 if (!pnp_port_valid(pdev, io_rsrc_no) ||
1507 pnp_port_len(pdev, io_rsrc_no) != dev_desc->io_region_size) {
1508 dev_err(&pdev->dev, "IR PNP Port not valid!\n");
1509 goto exit_free_dev_rdev;
1512 if (!pnp_irq_valid(pdev, 0)) {
1513 dev_err(&pdev->dev, "PNP IRQ not valid!\n");
1514 goto exit_free_dev_rdev;
1517 /* store resource values */
1518 itdev->cir_addr = pnp_port_start(pdev, io_rsrc_no);
1519 itdev->cir_irq = pnp_irq(pdev, 0);
1521 /* initialize spinlocks */
1522 spin_lock_init(&itdev->lock);
1524 /* initialize raw event */
1525 init_ir_raw_event(&itdev->rawir);
1527 /* set driver data into the pnp device */
1528 pnp_set_drvdata(pdev, itdev);
1531 /* initialize waitqueues for transmission */
1532 init_waitqueue_head(&itdev->tx_queue);
1533 init_waitqueue_head(&itdev->tx_ended);
1535 /* copy model-specific parameters */
1536 itdev->params = *dev_desc;
1538 /* apply any overrides */
1539 if (sample_period > 0)
1540 itdev->params.sample_period = sample_period;
1542 if (tx_carrier_freq > 0)
1543 itdev->params.tx_carrier_freq = tx_carrier_freq;
1545 if (tx_duty_cycle > 0 && tx_duty_cycle <= 100)
1546 itdev->params.tx_duty_cycle = tx_duty_cycle;
1548 if (rx_low_carrier_freq > 0)
1549 itdev->params.rx_low_carrier_freq = rx_low_carrier_freq;
1551 if (rx_high_carrier_freq > 0)
1552 itdev->params.rx_high_carrier_freq = rx_high_carrier_freq;
1554 /* print out parameters */
1555 ite_pr(KERN_NOTICE, "TX-capable: %d\n", (int)
1556 itdev->params.hw_tx_capable);
1557 ite_pr(KERN_NOTICE, "Sample period (ns): %ld\n", (long)
1558 itdev->params.sample_period);
1559 ite_pr(KERN_NOTICE, "TX carrier frequency (Hz): %d\n", (int)
1560 itdev->params.tx_carrier_freq);
1561 ite_pr(KERN_NOTICE, "TX duty cycle (%%): %d\n", (int)
1562 itdev->params.tx_duty_cycle);
1563 ite_pr(KERN_NOTICE, "RX low carrier frequency (Hz): %d\n", (int)
1564 itdev->params.rx_low_carrier_freq);
1565 ite_pr(KERN_NOTICE, "RX high carrier frequency (Hz): %d\n", (int)
1566 itdev->params.rx_high_carrier_freq);
1568 /* set up hardware initial state */
1569 itdev->params.init_hardware(itdev);
1571 /* set up ir-core props */
1573 rdev->driver_type = RC_DRIVER_IR_RAW;
1574 rdev->allowed_protocols = RC_BIT_ALL;
1575 rdev->open = ite_open;
1576 rdev->close = ite_close;
1577 rdev->s_idle = ite_s_idle;
1578 rdev->s_rx_carrier_range = ite_set_rx_carrier_range;
1579 rdev->min_timeout = ITE_MIN_IDLE_TIMEOUT;
1580 rdev->max_timeout = ITE_MAX_IDLE_TIMEOUT;
1581 rdev->timeout = ITE_IDLE_TIMEOUT;
1582 rdev->rx_resolution = ITE_BAUDRATE_DIVISOR *
1583 itdev->params.sample_period;
1584 rdev->tx_resolution = ITE_BAUDRATE_DIVISOR *
1585 itdev->params.sample_period;
1587 /* set up transmitter related values if needed */
1588 if (itdev->params.hw_tx_capable) {
1589 rdev->tx_ir = ite_tx_ir;
1590 rdev->s_tx_carrier = ite_set_tx_carrier;
1591 rdev->s_tx_duty_cycle = ite_set_tx_duty_cycle;
1594 rdev->input_name = dev_desc->model;
1595 rdev->input_id.bustype = BUS_HOST;
1596 rdev->input_id.vendor = PCI_VENDOR_ID_ITE;
1597 rdev->input_id.product = 0;
1598 rdev->input_id.version = 0;
1599 rdev->driver_name = ITE_DRIVER_NAME;
1600 rdev->map_name = RC_MAP_RC6_MCE;
1602 ret = rc_register_device(rdev);
1604 goto exit_free_dev_rdev;
1607 /* now claim resources */
1608 if (!request_region(itdev->cir_addr,
1609 dev_desc->io_region_size, ITE_DRIVER_NAME))
1610 goto exit_unregister_device;
1612 if (request_irq(itdev->cir_irq, ite_cir_isr, IRQF_SHARED,
1613 ITE_DRIVER_NAME, (void *)itdev))
1614 goto exit_release_cir_addr;
1616 ite_pr(KERN_NOTICE, "driver has been successfully loaded\n");
1620 exit_release_cir_addr:
1621 release_region(itdev->cir_addr, itdev->params.io_region_size);
1622 exit_unregister_device:
1623 rc_unregister_device(rdev);
1626 rc_free_device(rdev);
1632 static void ite_remove(struct pnp_dev *pdev)
1634 struct ite_dev *dev = pnp_get_drvdata(pdev);
1635 unsigned long flags;
1637 ite_dbg("%s called", __func__);
1639 spin_lock_irqsave(&dev->lock, flags);
1641 /* disable hardware */
1642 dev->params.disable(dev);
1644 spin_unlock_irqrestore(&dev->lock, flags);
1646 /* free resources */
1647 free_irq(dev->cir_irq, dev);
1648 release_region(dev->cir_addr, dev->params.io_region_size);
1650 rc_unregister_device(dev->rdev);
1655 static int ite_suspend(struct pnp_dev *pdev, pm_message_t state)
1657 struct ite_dev *dev = pnp_get_drvdata(pdev);
1658 unsigned long flags;
1660 ite_dbg("%s called", __func__);
1662 /* wait for any transmission to end */
1663 wait_event_interruptible(dev->tx_ended, !dev->transmitting);
1665 spin_lock_irqsave(&dev->lock, flags);
1667 /* disable all interrupts */
1668 dev->params.disable(dev);
1670 spin_unlock_irqrestore(&dev->lock, flags);
1675 static int ite_resume(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 /* reinitialize hardware config registers */
1685 dev->params.init_hardware(dev);
1686 /* enable the receiver */
1687 dev->params.enable_rx(dev);
1689 spin_unlock_irqrestore(&dev->lock, flags);
1694 static void ite_shutdown(struct pnp_dev *pdev)
1696 struct ite_dev *dev = pnp_get_drvdata(pdev);
1697 unsigned long flags;
1699 ite_dbg("%s called", __func__);
1701 spin_lock_irqsave(&dev->lock, flags);
1703 /* disable all interrupts */
1704 dev->params.disable(dev);
1706 spin_unlock_irqrestore(&dev->lock, flags);
1709 static struct pnp_driver ite_driver = {
1710 .name = ITE_DRIVER_NAME,
1711 .id_table = ite_ids,
1713 .remove = ite_remove,
1714 .suspend = ite_suspend,
1715 .resume = ite_resume,
1716 .shutdown = ite_shutdown,
1719 MODULE_DEVICE_TABLE(pnp, ite_ids);
1720 MODULE_DESCRIPTION("ITE Tech Inc. IT8712F/ITE8512F CIR driver");
1722 MODULE_AUTHOR("Juan J. Garcia de Soria <skandalfo@gmail.com>");
1723 MODULE_LICENSE("GPL");
1725 module_pnp_driver(ite_driver);