1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Driver for Nuvoton Technology Corporation w83667hg/w83677hg-i CIR
5 * Copyright (C) 2010 Jarod Wilson <jarod@redhat.com>
6 * Copyright (C) 2009 Nuvoton PS Team
8 * Special thanks to Nuvoton for providing hardware, spec sheets and
9 * sample code upon which portions of this driver are based. Indirect
10 * thanks also to Maxim Levitsky, whose ene_ir driver this driver is
14 #include <linux/spinlock.h>
15 #include <linux/ioctl.h>
17 /* platform driver name to register */
18 #define NVT_DRIVER_NAME "nuvoton-cir"
20 /* debugging module parameter */
24 #define nvt_dbg(text, ...) \
27 KBUILD_MODNAME ": " text "\n" , ## __VA_ARGS__)
29 #define nvt_dbg_verbose(text, ...) \
32 KBUILD_MODNAME ": " text "\n" , ## __VA_ARGS__)
34 #define nvt_dbg_wake(text, ...) \
37 KBUILD_MODNAME ": " text "\n" , ## __VA_ARGS__)
42 #define SIO_ID_MASK 0xfff0
46 NVT_W83667HG = 0xa510,
55 enum nvt_chip_ver chip_ver;
67 /* EFER Config register index/data pair */
71 /* hardware I/O settings */
72 unsigned long cir_addr;
73 unsigned long cir_wake_addr;
76 enum nvt_chip_ver chip_ver;
81 /* carrier period = 1 / frequency */
85 /* buffer packet constants */
86 #define BUF_PULSE_BIT 0x80
87 #define BUF_LEN_MASK 0x7f
88 #define BUF_REPEAT_BYTE 0x70
89 #define BUF_REPEAT_MASK 0xf0
93 /* total length of CIR and CIR WAKE */
94 #define CIR_IOREG_LENGTH 0x0f
96 /* RX limit length, 8 high bits for SLCH, 8 low bits for SLCL */
97 #define CIR_RX_LIMIT_COUNT (IR_DEFAULT_TIMEOUT / SAMPLE_PERIOD)
100 #define CIR_IRCON 0x00
101 #define CIR_IRSTS 0x01
102 #define CIR_IREN 0x02
103 #define CIR_RXFCONT 0x03
106 #define CIR_SLCH 0x06
107 #define CIR_SLCL 0x07
108 #define CIR_FIFOCON 0x08
109 #define CIR_IRFIFOSTS 0x09
110 #define CIR_SRXFIFO 0x0a
111 #define CIR_TXFCONT 0x0b
112 #define CIR_STXFIFO 0x0c
113 #define CIR_FCCH 0x0d
114 #define CIR_FCCL 0x0e
115 #define CIR_IRFSM 0x0f
117 /* CIR IRCON settings */
118 #define CIR_IRCON_RECV 0x80
119 #define CIR_IRCON_WIREN 0x40
120 #define CIR_IRCON_TXEN 0x20
121 #define CIR_IRCON_RXEN 0x10
122 #define CIR_IRCON_WRXINV 0x08
123 #define CIR_IRCON_RXINV 0x04
125 #define CIR_IRCON_SAMPLE_PERIOD_SEL_1 0x00
126 #define CIR_IRCON_SAMPLE_PERIOD_SEL_25 0x01
127 #define CIR_IRCON_SAMPLE_PERIOD_SEL_50 0x02
128 #define CIR_IRCON_SAMPLE_PERIOD_SEL_100 0x03
130 /* FIXME: make this a runtime option */
131 /* select sample period as 50us */
132 #define CIR_IRCON_SAMPLE_PERIOD_SEL CIR_IRCON_SAMPLE_PERIOD_SEL_50
134 /* CIR IRSTS settings */
135 #define CIR_IRSTS_RDR 0x80
136 #define CIR_IRSTS_RTR 0x40
137 #define CIR_IRSTS_PE 0x20
138 #define CIR_IRSTS_RFO 0x10
139 #define CIR_IRSTS_TE 0x08
140 #define CIR_IRSTS_TTR 0x04
141 #define CIR_IRSTS_TFU 0x02
142 #define CIR_IRSTS_GH 0x01
144 /* CIR IREN settings */
145 #define CIR_IREN_RDR 0x80
146 #define CIR_IREN_RTR 0x40
147 #define CIR_IREN_PE 0x20
148 #define CIR_IREN_RFO 0x10
149 #define CIR_IREN_TE 0x08
150 #define CIR_IREN_TTR 0x04
151 #define CIR_IREN_TFU 0x02
152 #define CIR_IREN_GH 0x01
154 /* CIR FIFOCON settings */
155 #define CIR_FIFOCON_TXFIFOCLR 0x80
157 #define CIR_FIFOCON_TX_TRIGGER_LEV_31 0x00
158 #define CIR_FIFOCON_TX_TRIGGER_LEV_24 0x10
159 #define CIR_FIFOCON_TX_TRIGGER_LEV_16 0x20
160 #define CIR_FIFOCON_TX_TRIGGER_LEV_8 0x30
162 /* FIXME: make this a runtime option */
163 /* select TX trigger level as 16 */
164 #define CIR_FIFOCON_TX_TRIGGER_LEV CIR_FIFOCON_TX_TRIGGER_LEV_16
166 #define CIR_FIFOCON_RXFIFOCLR 0x08
168 #define CIR_FIFOCON_RX_TRIGGER_LEV_1 0x00
169 #define CIR_FIFOCON_RX_TRIGGER_LEV_8 0x01
170 #define CIR_FIFOCON_RX_TRIGGER_LEV_16 0x02
171 #define CIR_FIFOCON_RX_TRIGGER_LEV_24 0x03
173 /* FIXME: make this a runtime option */
174 /* select RX trigger level as 24 */
175 #define CIR_FIFOCON_RX_TRIGGER_LEV CIR_FIFOCON_RX_TRIGGER_LEV_24
177 /* CIR IRFIFOSTS settings */
178 #define CIR_IRFIFOSTS_IR_PENDING 0x80
179 #define CIR_IRFIFOSTS_RX_GS 0x40
180 #define CIR_IRFIFOSTS_RX_FTA 0x20
181 #define CIR_IRFIFOSTS_RX_EMPTY 0x10
182 #define CIR_IRFIFOSTS_RX_FULL 0x08
183 #define CIR_IRFIFOSTS_TX_FTA 0x04
184 #define CIR_IRFIFOSTS_TX_EMPTY 0x02
185 #define CIR_IRFIFOSTS_TX_FULL 0x01
188 /* CIR WAKE UP Regs */
189 #define CIR_WAKE_IRCON 0x00
190 #define CIR_WAKE_IRSTS 0x01
191 #define CIR_WAKE_IREN 0x02
192 #define CIR_WAKE_FIFO_CMP_DEEP 0x03
193 #define CIR_WAKE_FIFO_CMP_TOL 0x04
194 #define CIR_WAKE_FIFO_COUNT 0x05
195 #define CIR_WAKE_SLCH 0x06
196 #define CIR_WAKE_SLCL 0x07
197 #define CIR_WAKE_FIFOCON 0x08
198 #define CIR_WAKE_SRXFSTS 0x09
199 #define CIR_WAKE_SAMPLE_RX_FIFO 0x0a
200 #define CIR_WAKE_WR_FIFO_DATA 0x0b
201 #define CIR_WAKE_RD_FIFO_ONLY 0x0c
202 #define CIR_WAKE_RD_FIFO_ONLY_IDX 0x0d
203 #define CIR_WAKE_FIFO_IGNORE 0x0e
204 #define CIR_WAKE_IRFSM 0x0f
206 /* CIR WAKE UP IRCON settings */
207 #define CIR_WAKE_IRCON_DEC_RST 0x80
208 #define CIR_WAKE_IRCON_MODE1 0x40
209 #define CIR_WAKE_IRCON_MODE0 0x20
210 #define CIR_WAKE_IRCON_RXEN 0x10
211 #define CIR_WAKE_IRCON_R 0x08
212 #define CIR_WAKE_IRCON_RXINV 0x04
214 /* FIXME/jarod: make this a runtime option */
215 /* select a same sample period like cir register */
216 #define CIR_WAKE_IRCON_SAMPLE_PERIOD_SEL CIR_IRCON_SAMPLE_PERIOD_SEL_50
218 /* CIR WAKE IRSTS Bits */
219 #define CIR_WAKE_IRSTS_RDR 0x80
220 #define CIR_WAKE_IRSTS_RTR 0x40
221 #define CIR_WAKE_IRSTS_PE 0x20
222 #define CIR_WAKE_IRSTS_RFO 0x10
223 #define CIR_WAKE_IRSTS_GH 0x08
224 #define CIR_WAKE_IRSTS_IR_PENDING 0x01
226 /* CIR WAKE UP IREN Bits */
227 #define CIR_WAKE_IREN_RDR 0x80
228 #define CIR_WAKE_IREN_RTR 0x40
229 #define CIR_WAKE_IREN_PE 0x20
230 #define CIR_WAKE_IREN_RFO 0x10
231 #define CIR_WAKE_IREN_GH 0x08
233 /* CIR WAKE FIFOCON settings */
234 #define CIR_WAKE_FIFOCON_RXFIFOCLR 0x08
236 #define CIR_WAKE_FIFOCON_RX_TRIGGER_LEV_67 0x00
237 #define CIR_WAKE_FIFOCON_RX_TRIGGER_LEV_66 0x01
238 #define CIR_WAKE_FIFOCON_RX_TRIGGER_LEV_65 0x02
239 #define CIR_WAKE_FIFOCON_RX_TRIGGER_LEV_64 0x03
241 /* FIXME: make this a runtime option */
242 /* select WAKE UP RX trigger level as 67 */
243 #define CIR_WAKE_FIFOCON_RX_TRIGGER_LEV CIR_WAKE_FIFOCON_RX_TRIGGER_LEV_67
245 /* CIR WAKE SRXFSTS settings */
246 #define CIR_WAKE_IRFIFOSTS_RX_GS 0x80
247 #define CIR_WAKE_IRFIFOSTS_RX_FTA 0x40
248 #define CIR_WAKE_IRFIFOSTS_RX_EMPTY 0x20
249 #define CIR_WAKE_IRFIFOSTS_RX_FULL 0x10
252 * The CIR Wake FIFO buffer is 67 bytes long, but the stock remote wakes
253 * the system comparing only 65 bytes (fails with this set to 67)
255 #define CIR_WAKE_FIFO_CMP_BYTES 65
256 /* CIR Wake byte comparison tolerance */
257 #define CIR_WAKE_CMP_TOLERANCE 5
260 * Extended Function Enable Registers:
261 * Extended Function Index Register
262 * Extended Function Data Register
267 /* Possible alternate EFER values, depends on how the chip is wired */
268 #define CR_EFIR2 0x4e
269 #define CR_EFDR2 0x4f
271 /* Extended Function Mode enable/disable magic values */
272 #define EFER_EFM_ENABLE 0x87
273 #define EFER_EFM_DISABLE 0xaa
275 /* Config regs we need to care about */
276 #define CR_SOFTWARE_RESET 0x02
277 #define CR_LOGICAL_DEV_SEL 0x07
278 #define CR_CHIP_ID_HI 0x20
279 #define CR_CHIP_ID_LO 0x21
280 #define CR_DEV_POWER_DOWN 0x22 /* bit 2 is CIR power, default power on */
281 #define CR_OUTPUT_PIN_SEL 0x27
282 #define CR_MULTIFUNC_PIN_SEL 0x2c
283 #define CR_LOGICAL_DEV_EN 0x30 /* valid for all logical devices */
284 /* next three regs valid for both the CIR and CIR_WAKE logical devices */
285 #define CR_CIR_BASE_ADDR_HI 0x60
286 #define CR_CIR_BASE_ADDR_LO 0x61
287 #define CR_CIR_IRQ_RSRC 0x70
288 /* next three regs valid only for ACPI logical dev */
289 #define CR_ACPI_CIR_WAKE 0xe0
290 #define CR_ACPI_IRQ_EVENTS 0xf6
291 #define CR_ACPI_IRQ_EVENTS2 0xf7
293 /* Logical devices that we need to care about */
294 #define LOGICAL_DEV_LPT 0x01
295 #define LOGICAL_DEV_CIR 0x06
296 #define LOGICAL_DEV_ACPI 0x0a
297 #define LOGICAL_DEV_CIR_WAKE 0x0e
299 #define LOGICAL_DEV_DISABLE 0x00
300 #define LOGICAL_DEV_ENABLE 0x01
302 #define CIR_WAKE_ENABLE_BIT 0x08
303 #define PME_INTR_CIR_PASS_BIT 0x08
305 /* w83677hg CIR pin config */
306 #define OUTPUT_PIN_SEL_MASK 0xbc
307 #define OUTPUT_ENABLE_CIR 0x01 /* Pin95=CIRRX, Pin96=CIRTX1 */
308 #define OUTPUT_ENABLE_CIRWB 0x40 /* enable wide-band sensor */
310 /* w83667hg CIR pin config */
311 #define MULTIFUNC_PIN_SEL_MASK 0x1f
312 #define MULTIFUNC_ENABLE_CIR 0x80 /* Pin75=CIRRX, Pin76=CIRTX1 */
313 #define MULTIFUNC_ENABLE_CIRWB 0x20 /* enable wide-band sensor */
315 /* MCE CIR signal length, related on sample period */
317 /* MCE CIR controller signal length: about 43ms
318 * 43ms / 50us (sample period) * 0.85 (inaccuracy)
320 #define CONTROLLER_BUF_LEN_MIN 830
322 /* MCE CIR keyboard signal length: about 26ms
323 * 26ms / 50us (sample period) * 0.85 (inaccuracy)
325 #define KEYBOARD_BUF_LEN_MAX 650
326 #define KEYBOARD_BUF_LEN_MIN 610
328 /* MCE CIR mouse signal length: about 24ms
329 * 24ms / 50us (sample period) * 0.85 (inaccuracy)
331 #define MOUSE_BUF_LEN_MIN 565
333 #define CIR_SAMPLE_PERIOD 50
334 #define CIR_SAMPLE_LOW_INACCURACY 0.85
336 /* MAX silence time that driver will sent to lirc */
337 #define MAX_SILENCE_TIME 60000
339 #if CIR_IRCON_SAMPLE_PERIOD_SEL == CIR_IRCON_SAMPLE_PERIOD_SEL_100
340 #define SAMPLE_PERIOD 100
342 #elif CIR_IRCON_SAMPLE_PERIOD_SEL == CIR_IRCON_SAMPLE_PERIOD_SEL_50
343 #define SAMPLE_PERIOD 50
345 #elif CIR_IRCON_SAMPLE_PERIOD_SEL == CIR_IRCON_SAMPLE_PERIOD_SEL_25
346 #define SAMPLE_PERIOD 25
349 #define SAMPLE_PERIOD 1
352 /* as VISTA MCE definition, valid carrier value */
353 #define MAX_CARRIER 60000
354 #define MIN_CARRIER 30000
356 /* max wakeup sequence length */
357 #define WAKEUP_MAX_SIZE 65