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