1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * winbond-cir.c - Driver for the Consumer IR functionality of Winbond
6 * Currently supports the Winbond WPCD376i chip (PNP id WEC1022), but
7 * could probably support others (Winbond WEC102X, NatSemi, etc)
8 * with minor modifications.
10 * Original Author: David Härdeman <david@hardeman.nu>
11 * Copyright (C) 2012 Sean Young <sean@mess.org>
12 * Copyright (C) 2009 - 2011 David Härdeman <david@hardeman.nu>
14 * Dedicated to my daughter Matilda, without whose loving attention this
15 * driver would have been finished in half the time and with a fraction
19 * o Winbond WPCD376I datasheet helpfully provided by Jesse Barnes at Intel
20 * o NatSemi PC87338/PC97338 datasheet (for the serial port stuff)
26 * o Wake-On-CIR functionality
30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32 #include <linux/module.h>
33 #include <linux/pnp.h>
34 #include <linux/interrupt.h>
35 #include <linux/timer.h>
36 #include <linux/leds.h>
37 #include <linux/spinlock.h>
38 #include <linux/pci_ids.h>
40 #include <linux/bitrev.h>
41 #include <linux/slab.h>
42 #include <linux/wait.h>
43 #include <linux/sched.h>
44 #include <media/rc-core.h>
46 #define DRVNAME "winbond-cir"
48 /* CEIR Wake-Up Registers, relative to data->wbase */
49 #define WBCIR_REG_WCEIR_CTL 0x03 /* CEIR Receiver Control */
50 #define WBCIR_REG_WCEIR_STS 0x04 /* CEIR Receiver Status */
51 #define WBCIR_REG_WCEIR_EV_EN 0x05 /* CEIR Receiver Event Enable */
52 #define WBCIR_REG_WCEIR_CNTL 0x06 /* CEIR Receiver Counter Low */
53 #define WBCIR_REG_WCEIR_CNTH 0x07 /* CEIR Receiver Counter High */
54 #define WBCIR_REG_WCEIR_INDEX 0x08 /* CEIR Receiver Index */
55 #define WBCIR_REG_WCEIR_DATA 0x09 /* CEIR Receiver Data */
56 #define WBCIR_REG_WCEIR_CSL 0x0A /* CEIR Re. Compare Strlen */
57 #define WBCIR_REG_WCEIR_CFG1 0x0B /* CEIR Re. Configuration 1 */
58 #define WBCIR_REG_WCEIR_CFG2 0x0C /* CEIR Re. Configuration 2 */
60 /* CEIR Enhanced Functionality Registers, relative to data->ebase */
61 #define WBCIR_REG_ECEIR_CTS 0x00 /* Enhanced IR Control Status */
62 #define WBCIR_REG_ECEIR_CCTL 0x01 /* Infrared Counter Control */
63 #define WBCIR_REG_ECEIR_CNT_LO 0x02 /* Infrared Counter LSB */
64 #define WBCIR_REG_ECEIR_CNT_HI 0x03 /* Infrared Counter MSB */
65 #define WBCIR_REG_ECEIR_IREM 0x04 /* Infrared Emitter Status */
67 /* SP3 Banked Registers, relative to data->sbase */
68 #define WBCIR_REG_SP3_BSR 0x03 /* Bank Select, all banks */
70 #define WBCIR_REG_SP3_RXDATA 0x00 /* FIFO RX data (r) */
71 #define WBCIR_REG_SP3_TXDATA 0x00 /* FIFO TX data (w) */
72 #define WBCIR_REG_SP3_IER 0x01 /* Interrupt Enable */
73 #define WBCIR_REG_SP3_EIR 0x02 /* Event Identification (r) */
74 #define WBCIR_REG_SP3_FCR 0x02 /* FIFO Control (w) */
75 #define WBCIR_REG_SP3_MCR 0x04 /* Mode Control */
76 #define WBCIR_REG_SP3_LSR 0x05 /* Link Status */
77 #define WBCIR_REG_SP3_MSR 0x06 /* Modem Status */
78 #define WBCIR_REG_SP3_ASCR 0x07 /* Aux Status and Control */
80 #define WBCIR_REG_SP3_BGDL 0x00 /* Baud Divisor LSB */
81 #define WBCIR_REG_SP3_BGDH 0x01 /* Baud Divisor MSB */
82 #define WBCIR_REG_SP3_EXCR1 0x02 /* Extended Control 1 */
83 #define WBCIR_REG_SP3_EXCR2 0x04 /* Extended Control 2 */
84 #define WBCIR_REG_SP3_TXFLV 0x06 /* TX FIFO Level */
85 #define WBCIR_REG_SP3_RXFLV 0x07 /* RX FIFO Level */
87 #define WBCIR_REG_SP3_MRID 0x00 /* Module Identification */
88 #define WBCIR_REG_SP3_SH_LCR 0x01 /* LCR Shadow */
89 #define WBCIR_REG_SP3_SH_FCR 0x02 /* FCR Shadow */
91 #define WBCIR_REG_SP3_IRCR1 0x02 /* Infrared Control 1 */
93 #define WBCIR_REG_SP3_IRCR2 0x04 /* Infrared Control 2 */
95 #define WBCIR_REG_SP3_IRCR3 0x00 /* Infrared Control 3 */
96 #define WBCIR_REG_SP3_SIR_PW 0x02 /* SIR Pulse Width */
98 #define WBCIR_REG_SP3_IRRXDC 0x00 /* IR RX Demod Control */
99 #define WBCIR_REG_SP3_IRTXMC 0x01 /* IR TX Mod Control */
100 #define WBCIR_REG_SP3_RCCFG 0x02 /* CEIR Config */
101 #define WBCIR_REG_SP3_IRCFG1 0x04 /* Infrared Config 1 */
102 #define WBCIR_REG_SP3_IRCFG4 0x07 /* Infrared Config 4 */
105 * Magic values follow
108 /* No interrupts for WBCIR_REG_SP3_IER and WBCIR_REG_SP3_EIR */
109 #define WBCIR_IRQ_NONE 0x00
110 /* RX data bit for WBCIR_REG_SP3_IER and WBCIR_REG_SP3_EIR */
111 #define WBCIR_IRQ_RX 0x01
112 /* TX data low bit for WBCIR_REG_SP3_IER and WBCIR_REG_SP3_EIR */
113 #define WBCIR_IRQ_TX_LOW 0x02
114 /* Over/Under-flow bit for WBCIR_REG_SP3_IER and WBCIR_REG_SP3_EIR */
115 #define WBCIR_IRQ_ERR 0x04
116 /* TX data empty bit for WBCEIR_REG_SP3_IER and WBCIR_REG_SP3_EIR */
117 #define WBCIR_IRQ_TX_EMPTY 0x20
118 /* Led enable/disable bit for WBCIR_REG_ECEIR_CTS */
119 #define WBCIR_LED_ENABLE 0x80
120 /* RX data available bit for WBCIR_REG_SP3_LSR */
121 #define WBCIR_RX_AVAIL 0x01
122 /* RX data overrun error bit for WBCIR_REG_SP3_LSR */
123 #define WBCIR_RX_OVERRUN 0x02
124 /* TX End-Of-Transmission bit for WBCIR_REG_SP3_ASCR */
125 #define WBCIR_TX_EOT 0x04
126 /* RX disable bit for WBCIR_REG_SP3_ASCR */
127 #define WBCIR_RX_DISABLE 0x20
128 /* TX data underrun error bit for WBCIR_REG_SP3_ASCR */
129 #define WBCIR_TX_UNDERRUN 0x40
130 /* Extended mode enable bit for WBCIR_REG_SP3_EXCR1 */
131 #define WBCIR_EXT_ENABLE 0x01
132 /* Select compare register in WBCIR_REG_WCEIR_INDEX (bits 5 & 6) */
133 #define WBCIR_REGSEL_COMPARE 0x10
134 /* Select mask register in WBCIR_REG_WCEIR_INDEX (bits 5 & 6) */
135 #define WBCIR_REGSEL_MASK 0x20
136 /* Starting address of selected register in WBCIR_REG_WCEIR_INDEX */
137 #define WBCIR_REG_ADDR0 0x00
138 /* Enable carrier counter */
139 #define WBCIR_CNTR_EN 0x01
140 /* Reset carrier counter */
141 #define WBCIR_CNTR_R 0x02
143 #define WBCIR_IRTX_INV 0x04
144 /* Receiver oversampling */
145 #define WBCIR_RX_T_OV 0x40
147 /* Valid banks for the SP3 UART */
159 /* Supported power-on IR Protocols */
160 enum wbcir_protocol
{
161 IR_PROTOCOL_RC5
= 0x0,
162 IR_PROTOCOL_NEC
= 0x1,
163 IR_PROTOCOL_RC6
= 0x2,
166 /* Possible states for IR reception */
168 WBCIR_RXSTATE_INACTIVE
= 0,
169 WBCIR_RXSTATE_ACTIVE
,
173 /* Possible states for IR transmission */
175 WBCIR_TXSTATE_INACTIVE
= 0,
176 WBCIR_TXSTATE_ACTIVE
,
181 #define WBCIR_NAME "Winbond CIR"
182 #define WBCIR_ID_FAMILY 0xF1 /* Family ID for the WPCD376I */
183 #define WBCIR_ID_CHIP 0x04 /* Chip ID for the WPCD376I */
184 #define WAKEUP_IOMEM_LEN 0x10 /* Wake-Up I/O Reg Len */
185 #define EHFUNC_IOMEM_LEN 0x10 /* Enhanced Func I/O Reg Len */
186 #define SP_IOMEM_LEN 0x08 /* Serial Port 3 (IR) Reg Len */
188 /* Per-device data */
192 struct led_classdev led
;
194 unsigned long wbase
; /* Wake-Up Baseaddr */
195 unsigned long ebase
; /* Enhanced Func. Baseaddr */
196 unsigned long sbase
; /* Serial Port Baseaddr */
197 unsigned int irq
; /* Serial Port IRQ */
201 enum wbcir_rxstate rxstate
;
202 int carrier_report_enabled
;
206 enum wbcir_txstate txstate
;
214 static bool invert
; /* default = 0 */
215 module_param(invert
, bool, 0444);
216 MODULE_PARM_DESC(invert
, "Invert the signal from the IR receiver");
218 static bool txandrx
; /* default = 0 */
219 module_param(txandrx
, bool, 0444);
220 MODULE_PARM_DESC(txandrx
, "Allow simultaneous TX and RX");
223 /*****************************************************************************
227 *****************************************************************************/
229 /* Caller needs to hold wbcir_lock */
231 wbcir_set_bits(unsigned long addr
, u8 bits
, u8 mask
)
236 val
= ((val
& ~mask
) | (bits
& mask
));
240 /* Selects the register bank for the serial port */
242 wbcir_select_bank(struct wbcir_data
*data
, enum wbcir_bank bank
)
244 outb(bank
, data
->sbase
+ WBCIR_REG_SP3_BSR
);
248 wbcir_set_irqmask(struct wbcir_data
*data
, u8 irqmask
)
250 if (data
->irqmask
== irqmask
)
253 wbcir_select_bank(data
, WBCIR_BANK_0
);
254 outb(irqmask
, data
->sbase
+ WBCIR_REG_SP3_IER
);
255 data
->irqmask
= irqmask
;
258 static enum led_brightness
259 wbcir_led_brightness_get(struct led_classdev
*led_cdev
)
261 struct wbcir_data
*data
= container_of(led_cdev
,
265 if (inb(data
->ebase
+ WBCIR_REG_ECEIR_CTS
) & WBCIR_LED_ENABLE
)
272 wbcir_led_brightness_set(struct led_classdev
*led_cdev
,
273 enum led_brightness brightness
)
275 struct wbcir_data
*data
= container_of(led_cdev
,
279 wbcir_set_bits(data
->ebase
+ WBCIR_REG_ECEIR_CTS
,
280 brightness
== LED_OFF
? 0x00 : WBCIR_LED_ENABLE
,
284 /* Manchester encodes bits to RC6 message cells (see wbcir_shutdown) */
286 wbcir_to_rc6cells(u8 val
)
292 for (i
= 0; i
< 4; i
++) {
294 coded
|= 0x02 << (i
* 2);
296 coded
|= 0x01 << (i
* 2);
303 /*****************************************************************************
305 * INTERRUPT FUNCTIONS
307 *****************************************************************************/
310 wbcir_carrier_report(struct wbcir_data
*data
)
312 unsigned counter
= inb(data
->ebase
+ WBCIR_REG_ECEIR_CNT_LO
) |
313 inb(data
->ebase
+ WBCIR_REG_ECEIR_CNT_HI
) << 8;
315 if (counter
> 0 && counter
< 0xffff) {
316 struct ir_raw_event ev
= {
318 .carrier
= DIV_ROUND_CLOSEST(counter
* 1000000u,
319 data
->pulse_duration
)
322 ir_raw_event_store(data
->dev
, &ev
);
325 /* reset and restart the counter */
326 data
->pulse_duration
= 0;
327 wbcir_set_bits(data
->ebase
+ WBCIR_REG_ECEIR_CCTL
, WBCIR_CNTR_R
,
328 WBCIR_CNTR_EN
| WBCIR_CNTR_R
);
329 wbcir_set_bits(data
->ebase
+ WBCIR_REG_ECEIR_CCTL
, WBCIR_CNTR_EN
,
330 WBCIR_CNTR_EN
| WBCIR_CNTR_R
);
334 wbcir_idle_rx(struct rc_dev
*dev
, bool idle
)
336 struct wbcir_data
*data
= dev
->priv
;
338 if (!idle
&& data
->rxstate
== WBCIR_RXSTATE_INACTIVE
)
339 data
->rxstate
= WBCIR_RXSTATE_ACTIVE
;
341 if (idle
&& data
->rxstate
!= WBCIR_RXSTATE_INACTIVE
) {
342 data
->rxstate
= WBCIR_RXSTATE_INACTIVE
;
344 if (data
->carrier_report_enabled
)
345 wbcir_carrier_report(data
);
347 /* Tell hardware to go idle by setting RXINACTIVE */
348 outb(WBCIR_RX_DISABLE
, data
->sbase
+ WBCIR_REG_SP3_ASCR
);
353 wbcir_irq_rx(struct wbcir_data
*data
, struct pnp_dev
*device
)
356 struct ir_raw_event rawir
= {};
359 /* Since RXHDLEV is set, at least 8 bytes are in the FIFO */
360 while (inb(data
->sbase
+ WBCIR_REG_SP3_LSR
) & WBCIR_RX_AVAIL
) {
361 irdata
= inb(data
->sbase
+ WBCIR_REG_SP3_RXDATA
);
362 if (data
->rxstate
== WBCIR_RXSTATE_ERROR
)
365 duration
= ((irdata
& 0x7F) + 1) *
366 (data
->carrier_report_enabled
? 2 : 10);
367 rawir
.pulse
= irdata
& 0x80 ? false : true;
368 rawir
.duration
= US_TO_NS(duration
);
371 data
->pulse_duration
+= duration
;
373 ir_raw_event_store_with_filter(data
->dev
, &rawir
);
376 ir_raw_event_handle(data
->dev
);
380 wbcir_irq_tx(struct wbcir_data
*data
)
390 switch (data
->txstate
) {
391 case WBCIR_TXSTATE_INACTIVE
:
395 case WBCIR_TXSTATE_ACTIVE
:
396 /* TX FIFO low (3 bytes or less) */
399 case WBCIR_TXSTATE_ERROR
:
407 * TX data is run-length coded in bytes: YXXXXXXX
408 * Y = space (1) or pulse (0)
409 * X = duration, encoded as (X + 1) * 10us (i.e 10 to 1280 us)
411 for (used
= 0; used
< space
&& data
->txoff
!= data
->txlen
; used
++) {
412 if (data
->txbuf
[data
->txoff
] == 0) {
416 byte
= min((u32
)0x80, data
->txbuf
[data
->txoff
]);
417 data
->txbuf
[data
->txoff
] -= byte
;
419 byte
|= (data
->txoff
% 2 ? 0x80 : 0x00); /* pulse/space */
423 while (data
->txoff
!= data
->txlen
&& data
->txbuf
[data
->txoff
] == 0)
428 if (data
->txstate
== WBCIR_TXSTATE_ERROR
)
429 /* Clear TX underrun bit */
430 outb(WBCIR_TX_UNDERRUN
, data
->sbase
+ WBCIR_REG_SP3_ASCR
);
431 wbcir_set_irqmask(data
, WBCIR_IRQ_RX
| WBCIR_IRQ_ERR
);
434 data
->txstate
= WBCIR_TXSTATE_INACTIVE
;
435 } else if (data
->txoff
== data
->txlen
) {
436 /* At the end of transmission, tell the hw before last byte */
437 outsb(data
->sbase
+ WBCIR_REG_SP3_TXDATA
, bytes
, used
- 1);
438 outb(WBCIR_TX_EOT
, data
->sbase
+ WBCIR_REG_SP3_ASCR
);
439 outb(bytes
[used
- 1], data
->sbase
+ WBCIR_REG_SP3_TXDATA
);
440 wbcir_set_irqmask(data
, WBCIR_IRQ_RX
| WBCIR_IRQ_ERR
|
443 /* More data to follow... */
444 outsb(data
->sbase
+ WBCIR_REG_SP3_RXDATA
, bytes
, used
);
445 if (data
->txstate
== WBCIR_TXSTATE_INACTIVE
) {
446 wbcir_set_irqmask(data
, WBCIR_IRQ_RX
| WBCIR_IRQ_ERR
|
448 data
->txstate
= WBCIR_TXSTATE_ACTIVE
;
454 wbcir_irq_handler(int irqno
, void *cookie
)
456 struct pnp_dev
*device
= cookie
;
457 struct wbcir_data
*data
= pnp_get_drvdata(device
);
461 spin_lock_irqsave(&data
->spinlock
, flags
);
462 wbcir_select_bank(data
, WBCIR_BANK_0
);
463 status
= inb(data
->sbase
+ WBCIR_REG_SP3_EIR
);
464 status
&= data
->irqmask
;
467 spin_unlock_irqrestore(&data
->spinlock
, flags
);
471 if (status
& WBCIR_IRQ_ERR
) {
472 /* RX overflow? (read clears bit) */
473 if (inb(data
->sbase
+ WBCIR_REG_SP3_LSR
) & WBCIR_RX_OVERRUN
) {
474 data
->rxstate
= WBCIR_RXSTATE_ERROR
;
475 ir_raw_event_reset(data
->dev
);
479 if (inb(data
->sbase
+ WBCIR_REG_SP3_ASCR
) & WBCIR_TX_UNDERRUN
)
480 data
->txstate
= WBCIR_TXSTATE_ERROR
;
483 if (status
& WBCIR_IRQ_RX
)
484 wbcir_irq_rx(data
, device
);
486 if (status
& (WBCIR_IRQ_TX_LOW
| WBCIR_IRQ_TX_EMPTY
))
489 spin_unlock_irqrestore(&data
->spinlock
, flags
);
493 /*****************************************************************************
495 * RC-CORE INTERFACE FUNCTIONS
497 *****************************************************************************/
500 wbcir_set_carrier_report(struct rc_dev
*dev
, int enable
)
502 struct wbcir_data
*data
= dev
->priv
;
505 spin_lock_irqsave(&data
->spinlock
, flags
);
507 if (data
->carrier_report_enabled
== enable
) {
508 spin_unlock_irqrestore(&data
->spinlock
, flags
);
512 data
->pulse_duration
= 0;
513 wbcir_set_bits(data
->ebase
+ WBCIR_REG_ECEIR_CCTL
, WBCIR_CNTR_R
,
514 WBCIR_CNTR_EN
| WBCIR_CNTR_R
);
516 if (enable
&& data
->dev
->idle
)
517 wbcir_set_bits(data
->ebase
+ WBCIR_REG_ECEIR_CCTL
,
518 WBCIR_CNTR_EN
, WBCIR_CNTR_EN
| WBCIR_CNTR_R
);
520 /* Set a higher sampling resolution if carrier reports are enabled */
521 wbcir_select_bank(data
, WBCIR_BANK_2
);
522 data
->dev
->rx_resolution
= US_TO_NS(enable
? 2 : 10);
523 outb(enable
? 0x03 : 0x0f, data
->sbase
+ WBCIR_REG_SP3_BGDL
);
524 outb(0x00, data
->sbase
+ WBCIR_REG_SP3_BGDH
);
526 /* Enable oversampling if carrier reports are enabled */
527 wbcir_select_bank(data
, WBCIR_BANK_7
);
528 wbcir_set_bits(data
->sbase
+ WBCIR_REG_SP3_RCCFG
,
529 enable
? WBCIR_RX_T_OV
: 0, WBCIR_RX_T_OV
);
531 data
->carrier_report_enabled
= enable
;
532 spin_unlock_irqrestore(&data
->spinlock
, flags
);
538 wbcir_txcarrier(struct rc_dev
*dev
, u32 carrier
)
540 struct wbcir_data
*data
= dev
->priv
;
545 freq
= DIV_ROUND_CLOSEST(carrier
, 1000);
546 if (freq
< 30 || freq
> 60)
566 spin_lock_irqsave(&data
->spinlock
, flags
);
567 if (data
->txstate
!= WBCIR_TXSTATE_INACTIVE
) {
568 spin_unlock_irqrestore(&data
->spinlock
, flags
);
572 if (data
->txcarrier
!= freq
) {
573 wbcir_select_bank(data
, WBCIR_BANK_7
);
574 wbcir_set_bits(data
->sbase
+ WBCIR_REG_SP3_IRTXMC
, val
, 0x1F);
575 data
->txcarrier
= freq
;
578 spin_unlock_irqrestore(&data
->spinlock
, flags
);
583 wbcir_txmask(struct rc_dev
*dev
, u32 mask
)
585 struct wbcir_data
*data
= dev
->priv
;
589 /* return the number of transmitters */
593 /* Four outputs, only one output can be enabled at a time */
611 spin_lock_irqsave(&data
->spinlock
, flags
);
612 if (data
->txstate
!= WBCIR_TXSTATE_INACTIVE
) {
613 spin_unlock_irqrestore(&data
->spinlock
, flags
);
617 if (data
->txmask
!= mask
) {
618 wbcir_set_bits(data
->ebase
+ WBCIR_REG_ECEIR_CTS
, val
, 0x0c);
622 spin_unlock_irqrestore(&data
->spinlock
, flags
);
627 wbcir_tx(struct rc_dev
*dev
, unsigned *b
, unsigned count
)
629 struct wbcir_data
*data
= dev
->priv
;
634 buf
= kmalloc_array(count
, sizeof(*b
), GFP_KERNEL
);
638 /* Convert values to multiples of 10us */
639 for (i
= 0; i
< count
; i
++)
640 buf
[i
] = DIV_ROUND_CLOSEST(b
[i
], 10);
642 /* Not sure if this is possible, but better safe than sorry */
643 spin_lock_irqsave(&data
->spinlock
, flags
);
644 if (data
->txstate
!= WBCIR_TXSTATE_INACTIVE
) {
645 spin_unlock_irqrestore(&data
->spinlock
, flags
);
650 /* Fill the TX fifo once, the irq handler will do the rest */
657 spin_unlock_irqrestore(&data
->spinlock
, flags
);
661 /*****************************************************************************
663 * SETUP/INIT/SUSPEND/RESUME FUNCTIONS
665 *****************************************************************************/
668 wbcir_shutdown(struct pnp_dev
*device
)
670 struct device
*dev
= &device
->dev
;
671 struct wbcir_data
*data
= pnp_get_drvdata(device
);
672 struct rc_dev
*rc
= data
->dev
;
678 u32 wake_sc
= rc
->scancode_wakeup_filter
.data
;
679 u32 mask_sc
= rc
->scancode_wakeup_filter
.mask
;
682 memset(match
, 0, sizeof(match
));
683 memset(mask
, 0, sizeof(mask
));
685 if (!mask_sc
|| !device_may_wakeup(dev
)) {
690 switch (rc
->wakeup_protocol
) {
692 /* Mask = 13 bits, ex toggle */
693 mask
[0] = (mask_sc
& 0x003f);
694 mask
[0] |= (mask_sc
& 0x0300) >> 2;
695 mask
[1] = (mask_sc
& 0x1c00) >> 10;
696 if (mask_sc
& 0x0040) /* 2nd start bit */
699 match
[0] = (wake_sc
& 0x003F); /* 6 command bits */
700 match
[0] |= (wake_sc
& 0x0300) >> 2; /* 2 address bits */
701 match
[1] = (wake_sc
& 0x1c00) >> 10; /* 3 address bits */
702 if (!(wake_sc
& 0x0040)) /* 2nd start bit */
705 proto
= IR_PROTOCOL_RC5
;
709 mask
[1] = bitrev8(mask_sc
);
711 mask
[3] = bitrev8(mask_sc
>> 8);
714 match
[1] = bitrev8(wake_sc
);
715 match
[0] = ~match
[1];
716 match
[3] = bitrev8(wake_sc
>> 8);
717 match
[2] = ~match
[3];
719 proto
= IR_PROTOCOL_NEC
;
723 mask
[1] = bitrev8(mask_sc
);
725 mask
[2] = bitrev8(mask_sc
>> 8);
726 mask
[3] = bitrev8(mask_sc
>> 16);
728 match
[1] = bitrev8(wake_sc
);
729 match
[0] = ~match
[1];
730 match
[2] = bitrev8(wake_sc
>> 8);
731 match
[3] = bitrev8(wake_sc
>> 16);
733 proto
= IR_PROTOCOL_NEC
;
737 mask
[0] = bitrev8(mask_sc
);
738 mask
[1] = bitrev8(mask_sc
>> 8);
739 mask
[2] = bitrev8(mask_sc
>> 16);
740 mask
[3] = bitrev8(mask_sc
>> 24);
742 match
[0] = bitrev8(wake_sc
);
743 match
[1] = bitrev8(wake_sc
>> 8);
744 match
[2] = bitrev8(wake_sc
>> 16);
745 match
[3] = bitrev8(wake_sc
>> 24);
747 proto
= IR_PROTOCOL_NEC
;
752 match
[0] = wbcir_to_rc6cells(wake_sc
>> 0);
753 mask
[0] = wbcir_to_rc6cells(mask_sc
>> 0);
754 match
[1] = wbcir_to_rc6cells(wake_sc
>> 4);
755 mask
[1] = wbcir_to_rc6cells(mask_sc
>> 4);
758 match
[2] = wbcir_to_rc6cells(wake_sc
>> 8);
759 mask
[2] = wbcir_to_rc6cells(mask_sc
>> 8);
760 match
[3] = wbcir_to_rc6cells(wake_sc
>> 12);
761 mask
[3] = wbcir_to_rc6cells(mask_sc
>> 12);
764 match
[4] = 0x50; /* mode1 = mode0 = 0, ignore toggle */
766 match
[5] = 0x09; /* start bit = 1, mode2 = 0 */
770 proto
= IR_PROTOCOL_RC6
;
773 case RC_PROTO_RC6_6A_24
:
774 case RC_PROTO_RC6_6A_32
:
775 case RC_PROTO_RC6_MCE
:
779 match
[i
] = wbcir_to_rc6cells(wake_sc
>> 0);
780 mask
[i
++] = wbcir_to_rc6cells(mask_sc
>> 0);
781 match
[i
] = wbcir_to_rc6cells(wake_sc
>> 4);
782 mask
[i
++] = wbcir_to_rc6cells(mask_sc
>> 4);
784 /* Address + Toggle */
785 match
[i
] = wbcir_to_rc6cells(wake_sc
>> 8);
786 mask
[i
++] = wbcir_to_rc6cells(mask_sc
>> 8);
787 match
[i
] = wbcir_to_rc6cells(wake_sc
>> 12);
788 mask
[i
++] = wbcir_to_rc6cells(mask_sc
>> 12);
790 /* Customer bits 7 - 0 */
791 match
[i
] = wbcir_to_rc6cells(wake_sc
>> 16);
792 mask
[i
++] = wbcir_to_rc6cells(mask_sc
>> 16);
794 if (rc
->wakeup_protocol
== RC_PROTO_RC6_6A_20
) {
797 match
[i
] = wbcir_to_rc6cells(wake_sc
>> 20);
798 mask
[i
++] = wbcir_to_rc6cells(mask_sc
>> 20);
800 if (rc
->wakeup_protocol
== RC_PROTO_RC6_6A_24
) {
803 /* Customer range bit and bits 15 - 8 */
804 match
[i
] = wbcir_to_rc6cells(wake_sc
>> 24);
805 mask
[i
++] = wbcir_to_rc6cells(mask_sc
>> 24);
806 match
[i
] = wbcir_to_rc6cells(wake_sc
>> 28);
807 mask
[i
++] = wbcir_to_rc6cells(mask_sc
>> 28);
813 match
[i
] = 0x93; /* mode1 = mode0 = 1, submode = 0 */
815 match
[i
] = 0x0A; /* start bit = 1, mode2 = 1 */
817 proto
= IR_PROTOCOL_RC6
;
826 /* Set compare and compare mask */
827 wbcir_set_bits(data
->wbase
+ WBCIR_REG_WCEIR_INDEX
,
828 WBCIR_REGSEL_COMPARE
| WBCIR_REG_ADDR0
,
830 outsb(data
->wbase
+ WBCIR_REG_WCEIR_DATA
, match
, 11);
831 wbcir_set_bits(data
->wbase
+ WBCIR_REG_WCEIR_INDEX
,
832 WBCIR_REGSEL_MASK
| WBCIR_REG_ADDR0
,
834 outsb(data
->wbase
+ WBCIR_REG_WCEIR_DATA
, mask
, 11);
836 /* RC6 Compare String Len */
837 outb(rc6_csl
, data
->wbase
+ WBCIR_REG_WCEIR_CSL
);
839 /* Clear status bits NEC_REP, BUFF, MSG_END, MATCH */
840 wbcir_set_bits(data
->wbase
+ WBCIR_REG_WCEIR_STS
, 0x17, 0x17);
842 /* Clear BUFF_EN, Clear END_EN, Set MATCH_EN */
843 wbcir_set_bits(data
->wbase
+ WBCIR_REG_WCEIR_EV_EN
, 0x01, 0x07);
846 wbcir_set_bits(data
->wbase
+ WBCIR_REG_WCEIR_CTL
,
847 (proto
<< 4) | 0x01, 0x31);
850 /* Clear BUFF_EN, Clear END_EN, Clear MATCH_EN */
851 wbcir_set_bits(data
->wbase
+ WBCIR_REG_WCEIR_EV_EN
, 0x00, 0x07);
854 wbcir_set_bits(data
->wbase
+ WBCIR_REG_WCEIR_CTL
, 0x00, 0x01);
858 * ACPI will set the HW disable bit for SP3 which means that the
859 * output signals are left in an undefined state which may cause
860 * spurious interrupts which we need to ignore until the hardware
863 wbcir_set_irqmask(data
, WBCIR_IRQ_NONE
);
864 disable_irq(data
->irq
);
868 * Wakeup handling is done on shutdown.
871 wbcir_set_wakeup_filter(struct rc_dev
*rc
, struct rc_scancode_filter
*filter
)
877 wbcir_suspend(struct pnp_dev
*device
, pm_message_t state
)
879 struct wbcir_data
*data
= pnp_get_drvdata(device
);
880 led_classdev_suspend(&data
->led
);
881 wbcir_shutdown(device
);
886 wbcir_init_hw(struct wbcir_data
*data
)
888 /* Disable interrupts */
889 wbcir_set_irqmask(data
, WBCIR_IRQ_NONE
);
891 /* Set RX_INV, Clear CEIR_EN (needed for the led) */
892 wbcir_set_bits(data
->wbase
+ WBCIR_REG_WCEIR_CTL
, invert
? 8 : 0, 0x09);
894 /* Clear status bits NEC_REP, BUFF, MSG_END, MATCH */
895 wbcir_set_bits(data
->wbase
+ WBCIR_REG_WCEIR_STS
, 0x17, 0x17);
897 /* Clear BUFF_EN, Clear END_EN, Clear MATCH_EN */
898 wbcir_set_bits(data
->wbase
+ WBCIR_REG_WCEIR_EV_EN
, 0x00, 0x07);
900 /* Set RC5 cell time to correspond to 36 kHz */
901 wbcir_set_bits(data
->wbase
+ WBCIR_REG_WCEIR_CFG1
, 0x4A, 0x7F);
905 outb(WBCIR_IRTX_INV
, data
->ebase
+ WBCIR_REG_ECEIR_CCTL
);
907 outb(0x00, data
->ebase
+ WBCIR_REG_ECEIR_CCTL
);
910 * Clear IR LED, set SP3 clock to 24Mhz, set TX mask to IRTX1,
911 * set SP3_IRRX_SW to binary 01, helpfully not documented
913 outb(0x10, data
->ebase
+ WBCIR_REG_ECEIR_CTS
);
916 /* Enable extended mode */
917 wbcir_select_bank(data
, WBCIR_BANK_2
);
918 outb(WBCIR_EXT_ENABLE
, data
->sbase
+ WBCIR_REG_SP3_EXCR1
);
921 * Configure baud generator, IR data will be sampled at
922 * a bitrate of: (24Mhz * prescaler) / (divisor * 16).
924 * The ECIR registers include a flag to change the
925 * 24Mhz clock freq to 48Mhz.
927 * It's not documented in the specs, but fifo levels
928 * other than 16 seems to be unsupported.
931 /* prescaler 1.0, tx/rx fifo lvl 16 */
932 outb(0x30, data
->sbase
+ WBCIR_REG_SP3_EXCR2
);
934 /* Set baud divisor to sample every 10 us */
935 outb(0x0f, data
->sbase
+ WBCIR_REG_SP3_BGDL
);
936 outb(0x00, data
->sbase
+ WBCIR_REG_SP3_BGDH
);
939 wbcir_select_bank(data
, WBCIR_BANK_0
);
940 outb(0xC0, data
->sbase
+ WBCIR_REG_SP3_MCR
);
941 inb(data
->sbase
+ WBCIR_REG_SP3_LSR
); /* Clear LSR */
942 inb(data
->sbase
+ WBCIR_REG_SP3_MSR
); /* Clear MSR */
944 /* Disable RX demod, enable run-length enc/dec, set freq span */
945 wbcir_select_bank(data
, WBCIR_BANK_7
);
946 outb(0x90, data
->sbase
+ WBCIR_REG_SP3_RCCFG
);
949 wbcir_select_bank(data
, WBCIR_BANK_4
);
950 outb(0x00, data
->sbase
+ WBCIR_REG_SP3_IRCR1
);
952 /* Disable MSR interrupt, clear AUX_IRX, mask RX during TX? */
953 wbcir_select_bank(data
, WBCIR_BANK_5
);
954 outb(txandrx
? 0x03 : 0x02, data
->sbase
+ WBCIR_REG_SP3_IRCR2
);
957 wbcir_select_bank(data
, WBCIR_BANK_6
);
958 outb(0x20, data
->sbase
+ WBCIR_REG_SP3_IRCR3
);
960 /* Set RX demodulation freq, not really used */
961 wbcir_select_bank(data
, WBCIR_BANK_7
);
962 outb(0xF2, data
->sbase
+ WBCIR_REG_SP3_IRRXDC
);
964 /* Set TX modulation, 36kHz, 7us pulse width */
965 outb(0x69, data
->sbase
+ WBCIR_REG_SP3_IRTXMC
);
966 data
->txcarrier
= 36000;
968 /* Set invert and pin direction */
970 outb(0x10, data
->sbase
+ WBCIR_REG_SP3_IRCFG4
);
972 outb(0x00, data
->sbase
+ WBCIR_REG_SP3_IRCFG4
);
974 /* Set FIFO thresholds (RX = 8, TX = 3), reset RX/TX */
975 wbcir_select_bank(data
, WBCIR_BANK_0
);
976 outb(0x97, data
->sbase
+ WBCIR_REG_SP3_FCR
);
978 /* Clear AUX status bits */
979 outb(0xE0, data
->sbase
+ WBCIR_REG_SP3_ASCR
);
982 data
->rxstate
= WBCIR_RXSTATE_INACTIVE
;
983 wbcir_idle_rx(data
->dev
, true);
986 if (data
->txstate
== WBCIR_TXSTATE_ACTIVE
) {
989 data
->txstate
= WBCIR_TXSTATE_INACTIVE
;
992 /* Enable interrupts */
993 wbcir_set_irqmask(data
, WBCIR_IRQ_RX
| WBCIR_IRQ_ERR
);
997 wbcir_resume(struct pnp_dev
*device
)
999 struct wbcir_data
*data
= pnp_get_drvdata(device
);
1001 wbcir_init_hw(data
);
1002 ir_raw_event_reset(data
->dev
);
1003 enable_irq(data
->irq
);
1004 led_classdev_resume(&data
->led
);
1010 wbcir_probe(struct pnp_dev
*device
, const struct pnp_device_id
*dev_id
)
1012 struct device
*dev
= &device
->dev
;
1013 struct wbcir_data
*data
;
1016 if (!(pnp_port_len(device
, 0) == EHFUNC_IOMEM_LEN
&&
1017 pnp_port_len(device
, 1) == WAKEUP_IOMEM_LEN
&&
1018 pnp_port_len(device
, 2) == SP_IOMEM_LEN
)) {
1019 dev_err(dev
, "Invalid resources\n");
1023 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
1029 pnp_set_drvdata(device
, data
);
1031 spin_lock_init(&data
->spinlock
);
1032 data
->ebase
= pnp_port_start(device
, 0);
1033 data
->wbase
= pnp_port_start(device
, 1);
1034 data
->sbase
= pnp_port_start(device
, 2);
1035 data
->irq
= pnp_irq(device
, 0);
1037 if (data
->wbase
== 0 || data
->ebase
== 0 ||
1038 data
->sbase
== 0 || data
->irq
== -1) {
1040 dev_err(dev
, "Invalid resources\n");
1041 goto exit_free_data
;
1044 dev_dbg(&device
->dev
, "Found device (w: 0x%lX, e: 0x%lX, s: 0x%lX, i: %u)\n",
1045 data
->wbase
, data
->ebase
, data
->sbase
, data
->irq
);
1047 data
->led
.name
= "cir::activity";
1048 data
->led
.default_trigger
= "rc-feedback";
1049 data
->led
.brightness_set
= wbcir_led_brightness_set
;
1050 data
->led
.brightness_get
= wbcir_led_brightness_get
;
1051 err
= led_classdev_register(&device
->dev
, &data
->led
);
1053 goto exit_free_data
;
1055 data
->dev
= rc_allocate_device(RC_DRIVER_IR_RAW
);
1058 goto exit_unregister_led
;
1061 data
->dev
->driver_name
= DRVNAME
;
1062 data
->dev
->device_name
= WBCIR_NAME
;
1063 data
->dev
->input_phys
= "wbcir/cir0";
1064 data
->dev
->input_id
.bustype
= BUS_HOST
;
1065 data
->dev
->input_id
.vendor
= PCI_VENDOR_ID_WINBOND
;
1066 data
->dev
->input_id
.product
= WBCIR_ID_FAMILY
;
1067 data
->dev
->input_id
.version
= WBCIR_ID_CHIP
;
1068 data
->dev
->map_name
= RC_MAP_RC6_MCE
;
1069 data
->dev
->s_idle
= wbcir_idle_rx
;
1070 data
->dev
->s_carrier_report
= wbcir_set_carrier_report
;
1071 data
->dev
->s_tx_mask
= wbcir_txmask
;
1072 data
->dev
->s_tx_carrier
= wbcir_txcarrier
;
1073 data
->dev
->tx_ir
= wbcir_tx
;
1074 data
->dev
->priv
= data
;
1075 data
->dev
->dev
.parent
= &device
->dev
;
1076 data
->dev
->min_timeout
= 1;
1077 data
->dev
->timeout
= IR_DEFAULT_TIMEOUT
;
1078 data
->dev
->max_timeout
= 10 * IR_DEFAULT_TIMEOUT
;
1079 data
->dev
->rx_resolution
= US_TO_NS(2);
1080 data
->dev
->allowed_protocols
= RC_PROTO_BIT_ALL_IR_DECODER
;
1081 data
->dev
->allowed_wakeup_protocols
= RC_PROTO_BIT_NEC
|
1082 RC_PROTO_BIT_NECX
| RC_PROTO_BIT_NEC32
| RC_PROTO_BIT_RC5
|
1083 RC_PROTO_BIT_RC6_0
| RC_PROTO_BIT_RC6_6A_20
|
1084 RC_PROTO_BIT_RC6_6A_24
| RC_PROTO_BIT_RC6_6A_32
|
1085 RC_PROTO_BIT_RC6_MCE
;
1086 data
->dev
->wakeup_protocol
= RC_PROTO_RC6_MCE
;
1087 data
->dev
->scancode_wakeup_filter
.data
= 0x800f040c;
1088 data
->dev
->scancode_wakeup_filter
.mask
= 0xffff7fff;
1089 data
->dev
->s_wakeup_filter
= wbcir_set_wakeup_filter
;
1091 err
= rc_register_device(data
->dev
);
1095 if (!request_region(data
->wbase
, WAKEUP_IOMEM_LEN
, DRVNAME
)) {
1096 dev_err(dev
, "Region 0x%lx-0x%lx already in use!\n",
1097 data
->wbase
, data
->wbase
+ WAKEUP_IOMEM_LEN
- 1);
1099 goto exit_unregister_device
;
1102 if (!request_region(data
->ebase
, EHFUNC_IOMEM_LEN
, DRVNAME
)) {
1103 dev_err(dev
, "Region 0x%lx-0x%lx already in use!\n",
1104 data
->ebase
, data
->ebase
+ EHFUNC_IOMEM_LEN
- 1);
1106 goto exit_release_wbase
;
1109 if (!request_region(data
->sbase
, SP_IOMEM_LEN
, DRVNAME
)) {
1110 dev_err(dev
, "Region 0x%lx-0x%lx already in use!\n",
1111 data
->sbase
, data
->sbase
+ SP_IOMEM_LEN
- 1);
1113 goto exit_release_ebase
;
1116 err
= request_irq(data
->irq
, wbcir_irq_handler
,
1117 0, DRVNAME
, device
);
1119 dev_err(dev
, "Failed to claim IRQ %u\n", data
->irq
);
1121 goto exit_release_sbase
;
1124 device_init_wakeup(&device
->dev
, 1);
1126 wbcir_init_hw(data
);
1131 release_region(data
->sbase
, SP_IOMEM_LEN
);
1133 release_region(data
->ebase
, EHFUNC_IOMEM_LEN
);
1135 release_region(data
->wbase
, WAKEUP_IOMEM_LEN
);
1136 exit_unregister_device
:
1137 rc_unregister_device(data
->dev
);
1140 rc_free_device(data
->dev
);
1141 exit_unregister_led
:
1142 led_classdev_unregister(&data
->led
);
1145 pnp_set_drvdata(device
, NULL
);
1151 wbcir_remove(struct pnp_dev
*device
)
1153 struct wbcir_data
*data
= pnp_get_drvdata(device
);
1155 /* Disable interrupts */
1156 wbcir_set_irqmask(data
, WBCIR_IRQ_NONE
);
1157 free_irq(data
->irq
, device
);
1159 /* Clear status bits NEC_REP, BUFF, MSG_END, MATCH */
1160 wbcir_set_bits(data
->wbase
+ WBCIR_REG_WCEIR_STS
, 0x17, 0x17);
1163 wbcir_set_bits(data
->wbase
+ WBCIR_REG_WCEIR_CTL
, 0x00, 0x01);
1165 /* Clear BUFF_EN, END_EN, MATCH_EN */
1166 wbcir_set_bits(data
->wbase
+ WBCIR_REG_WCEIR_EV_EN
, 0x00, 0x07);
1168 rc_unregister_device(data
->dev
);
1170 led_classdev_unregister(&data
->led
);
1172 /* This is ok since &data->led isn't actually used */
1173 wbcir_led_brightness_set(&data
->led
, LED_OFF
);
1175 release_region(data
->wbase
, WAKEUP_IOMEM_LEN
);
1176 release_region(data
->ebase
, EHFUNC_IOMEM_LEN
);
1177 release_region(data
->sbase
, SP_IOMEM_LEN
);
1181 pnp_set_drvdata(device
, NULL
);
1184 static const struct pnp_device_id wbcir_ids
[] = {
1188 MODULE_DEVICE_TABLE(pnp
, wbcir_ids
);
1190 static struct pnp_driver wbcir_driver
= {
1192 .id_table
= wbcir_ids
,
1193 .probe
= wbcir_probe
,
1194 .remove
= wbcir_remove
,
1195 .suspend
= wbcir_suspend
,
1196 .resume
= wbcir_resume
,
1197 .shutdown
= wbcir_shutdown
1205 ret
= pnp_register_driver(&wbcir_driver
);
1207 pr_err("Unable to register driver\n");
1215 pnp_unregister_driver(&wbcir_driver
);
1218 module_init(wbcir_init
);
1219 module_exit(wbcir_exit
);
1221 MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
1222 MODULE_DESCRIPTION("Winbond SuperI/O Consumer IR Driver");
1223 MODULE_LICENSE("GPL");