blob: c96a9d30fa23573f399d0e3492dc4de2e0cb0f64 [file] [log] [blame]
Jarod Wilson6d2f5c22010-10-07 17:50:34 -03001/*
2 * Driver for Nuvoton Technology Corporation w83667hg/w83677hg-i CIR
3 *
4 * Copyright (C) 2010 Jarod Wilson <jarod@redhat.com>
5 * Copyright (C) 2009 Nuvoton PS Team
6 *
7 * Special thanks to Nuvoton for providing hardware, spec sheets and
8 * sample code upon which portions of this driver are based. Indirect
9 * thanks also to Maxim Levitsky, whose ene_ir driver this driver is
10 * modeled after.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 * USA
26 */
27
28#include <linux/spinlock.h>
Jarod Wilson4e6e29a2010-10-15 11:07:37 -030029#include <linux/ioctl.h>
Jarod Wilson6d2f5c22010-10-07 17:50:34 -030030
31/* platform driver name to register */
32#define NVT_DRIVER_NAME "nuvoton-cir"
33
34/* debugging module parameter */
35static int debug;
36
37
38#define nvt_pr(level, text, ...) \
39 printk(level KBUILD_MODNAME ": " text, ## __VA_ARGS__)
40
41#define nvt_dbg(text, ...) \
42 if (debug) \
43 printk(KERN_DEBUG \
44 KBUILD_MODNAME ": " text "\n" , ## __VA_ARGS__)
45
46#define nvt_dbg_verbose(text, ...) \
47 if (debug > 1) \
48 printk(KERN_DEBUG \
49 KBUILD_MODNAME ": " text "\n" , ## __VA_ARGS__)
50
51#define nvt_dbg_wake(text, ...) \
52 if (debug > 2) \
53 printk(KERN_DEBUG \
54 KBUILD_MODNAME ": " text "\n" , ## __VA_ARGS__)
55
56
57/*
58 * Original lirc driver said min value of 76, and recommended value of 256
59 * for the buffer length, but then used 2048. Never mind that the size of the
60 * RX FIFO is 32 bytes... So I'm using 32 for RX and 256 for TX atm, but I'm
61 * not sure if maybe that TX value is off by a factor of 8 (bits vs. bytes),
62 * and I don't have TX-capable hardware to test/debug on...
63 */
64#define TX_BUF_LEN 256
65#define RX_BUF_LEN 32
66
Heiner Kallweitb5cf7252015-10-29 19:21:38 -020067#define SIO_ID_MASK 0xfff0
68
69enum nvt_chip_ver {
70 NVT_UNKNOWN = 0,
71 NVT_W83667HG = 0xa510,
72 NVT_6775F = 0xb470,
Heiner Kallweitd0b528d2015-10-29 19:22:23 -020073 NVT_6776F = 0xc330,
74 NVT_6779D = 0xc560
Heiner Kallweitb5cf7252015-10-29 19:21:38 -020075};
76
77struct nvt_chip {
78 const char *name;
79 enum nvt_chip_ver chip_ver;
80};
81
Jarod Wilson6d2f5c22010-10-07 17:50:34 -030082struct nvt_dev {
83 struct pnp_dev *pdev;
David Härdemand8b4b582010-10-29 16:08:23 -030084 struct rc_dev *rdev;
Jarod Wilson6d2f5c22010-10-07 17:50:34 -030085
86 spinlock_t nvt_lock;
Jarod Wilson6d2f5c22010-10-07 17:50:34 -030087
88 /* for rx */
89 u8 buf[RX_BUF_LEN];
90 unsigned int pkts;
91
92 struct {
93 spinlock_t lock;
94 u8 buf[TX_BUF_LEN];
95 unsigned int buf_count;
96 unsigned int cur_buf_num;
97 wait_queue_head_t queue;
98 u8 tx_state;
99 } tx;
100
101 /* EFER Config register index/data pair */
Mauro Carvalho Chehab221cefa2013-11-01 14:37:17 -0300102 u32 cr_efir;
103 u32 cr_efdr;
Jarod Wilson6d2f5c22010-10-07 17:50:34 -0300104
105 /* hardware I/O settings */
106 unsigned long cir_addr;
107 unsigned long cir_wake_addr;
108 int cir_irq;
109 int cir_wake_irq;
110
Heiner Kallweitb5cf7252015-10-29 19:21:38 -0200111 enum nvt_chip_ver chip_ver;
Jarod Wilson6d2f5c22010-10-07 17:50:34 -0300112 /* hardware id */
113 u8 chip_major;
114 u8 chip_minor;
115
116 /* hardware features */
117 bool hw_learning_capable;
118 bool hw_tx_capable;
119
120 /* rx settings */
121 bool learning_enabled;
Jarod Wilson6d2f5c22010-10-07 17:50:34 -0300122
123 /* track cir wake state */
124 u8 wake_state;
125 /* for study */
126 u8 study_state;
127 /* carrier period = 1 / frequency */
128 u32 carrier;
129};
130
131/* study states */
132#define ST_STUDY_NONE 0x0
133#define ST_STUDY_START 0x1
134#define ST_STUDY_CARRIER 0x2
135#define ST_STUDY_ALL_RECV 0x4
136
137/* wake states */
138#define ST_WAKE_NONE 0x0
139#define ST_WAKE_START 0x1
140#define ST_WAKE_FINISH 0x2
141
142/* receive states */
143#define ST_RX_WAIT_7F 0x1
144#define ST_RX_WAIT_HEAD 0x2
145#define ST_RX_WAIT_SILENT_END 0x4
146
147/* send states */
148#define ST_TX_NONE 0x0
149#define ST_TX_REQUEST 0x2
150#define ST_TX_REPLY 0x4
151
152/* buffer packet constants */
153#define BUF_PULSE_BIT 0x80
154#define BUF_LEN_MASK 0x7f
155#define BUF_REPEAT_BYTE 0x70
156#define BUF_REPEAT_MASK 0xf0
157
158/* CIR settings */
159
160/* total length of CIR and CIR WAKE */
161#define CIR_IOREG_LENGTH 0x0f
162
163/* RX limit length, 8 high bits for SLCH, 8 low bits for SLCL (0x7d0 = 2000) */
164#define CIR_RX_LIMIT_COUNT 0x7d0
165
166/* CIR Regs */
167#define CIR_IRCON 0x00
168#define CIR_IRSTS 0x01
169#define CIR_IREN 0x02
170#define CIR_RXFCONT 0x03
171#define CIR_CP 0x04
172#define CIR_CC 0x05
173#define CIR_SLCH 0x06
174#define CIR_SLCL 0x07
175#define CIR_FIFOCON 0x08
176#define CIR_IRFIFOSTS 0x09
177#define CIR_SRXFIFO 0x0a
178#define CIR_TXFCONT 0x0b
179#define CIR_STXFIFO 0x0c
180#define CIR_FCCH 0x0d
181#define CIR_FCCL 0x0e
182#define CIR_IRFSM 0x0f
183
184/* CIR IRCON settings */
185#define CIR_IRCON_RECV 0x80
186#define CIR_IRCON_WIREN 0x40
187#define CIR_IRCON_TXEN 0x20
188#define CIR_IRCON_RXEN 0x10
189#define CIR_IRCON_WRXINV 0x08
190#define CIR_IRCON_RXINV 0x04
191
192#define CIR_IRCON_SAMPLE_PERIOD_SEL_1 0x00
193#define CIR_IRCON_SAMPLE_PERIOD_SEL_25 0x01
194#define CIR_IRCON_SAMPLE_PERIOD_SEL_50 0x02
195#define CIR_IRCON_SAMPLE_PERIOD_SEL_100 0x03
196
197/* FIXME: make this a runtime option */
198/* select sample period as 50us */
199#define CIR_IRCON_SAMPLE_PERIOD_SEL CIR_IRCON_SAMPLE_PERIOD_SEL_50
200
201/* CIR IRSTS settings */
202#define CIR_IRSTS_RDR 0x80
203#define CIR_IRSTS_RTR 0x40
204#define CIR_IRSTS_PE 0x20
205#define CIR_IRSTS_RFO 0x10
206#define CIR_IRSTS_TE 0x08
207#define CIR_IRSTS_TTR 0x04
208#define CIR_IRSTS_TFU 0x02
209#define CIR_IRSTS_GH 0x01
210
211/* CIR IREN settings */
212#define CIR_IREN_RDR 0x80
213#define CIR_IREN_RTR 0x40
214#define CIR_IREN_PE 0x20
215#define CIR_IREN_RFO 0x10
216#define CIR_IREN_TE 0x08
217#define CIR_IREN_TTR 0x04
218#define CIR_IREN_TFU 0x02
219#define CIR_IREN_GH 0x01
220
221/* CIR FIFOCON settings */
222#define CIR_FIFOCON_TXFIFOCLR 0x80
223
224#define CIR_FIFOCON_TX_TRIGGER_LEV_31 0x00
225#define CIR_FIFOCON_TX_TRIGGER_LEV_24 0x10
226#define CIR_FIFOCON_TX_TRIGGER_LEV_16 0x20
227#define CIR_FIFOCON_TX_TRIGGER_LEV_8 0x30
228
229/* FIXME: make this a runtime option */
230/* select TX trigger level as 16 */
231#define CIR_FIFOCON_TX_TRIGGER_LEV CIR_FIFOCON_TX_TRIGGER_LEV_16
232
233#define CIR_FIFOCON_RXFIFOCLR 0x08
234
235#define CIR_FIFOCON_RX_TRIGGER_LEV_1 0x00
236#define CIR_FIFOCON_RX_TRIGGER_LEV_8 0x01
237#define CIR_FIFOCON_RX_TRIGGER_LEV_16 0x02
238#define CIR_FIFOCON_RX_TRIGGER_LEV_24 0x03
239
240/* FIXME: make this a runtime option */
241/* select RX trigger level as 24 */
242#define CIR_FIFOCON_RX_TRIGGER_LEV CIR_FIFOCON_RX_TRIGGER_LEV_24
243
244/* CIR IRFIFOSTS settings */
245#define CIR_IRFIFOSTS_IR_PENDING 0x80
246#define CIR_IRFIFOSTS_RX_GS 0x40
247#define CIR_IRFIFOSTS_RX_FTA 0x20
248#define CIR_IRFIFOSTS_RX_EMPTY 0x10
249#define CIR_IRFIFOSTS_RX_FULL 0x08
250#define CIR_IRFIFOSTS_TX_FTA 0x04
251#define CIR_IRFIFOSTS_TX_EMPTY 0x02
252#define CIR_IRFIFOSTS_TX_FULL 0x01
253
254
255/* CIR WAKE UP Regs */
256#define CIR_WAKE_IRCON 0x00
257#define CIR_WAKE_IRSTS 0x01
258#define CIR_WAKE_IREN 0x02
259#define CIR_WAKE_FIFO_CMP_DEEP 0x03
260#define CIR_WAKE_FIFO_CMP_TOL 0x04
261#define CIR_WAKE_FIFO_COUNT 0x05
262#define CIR_WAKE_SLCH 0x06
263#define CIR_WAKE_SLCL 0x07
264#define CIR_WAKE_FIFOCON 0x08
265#define CIR_WAKE_SRXFSTS 0x09
266#define CIR_WAKE_SAMPLE_RX_FIFO 0x0a
267#define CIR_WAKE_WR_FIFO_DATA 0x0b
268#define CIR_WAKE_RD_FIFO_ONLY 0x0c
269#define CIR_WAKE_RD_FIFO_ONLY_IDX 0x0d
270#define CIR_WAKE_FIFO_IGNORE 0x0e
271#define CIR_WAKE_IRFSM 0x0f
272
273/* CIR WAKE UP IRCON settings */
274#define CIR_WAKE_IRCON_DEC_RST 0x80
275#define CIR_WAKE_IRCON_MODE1 0x40
276#define CIR_WAKE_IRCON_MODE0 0x20
277#define CIR_WAKE_IRCON_RXEN 0x10
278#define CIR_WAKE_IRCON_R 0x08
279#define CIR_WAKE_IRCON_RXINV 0x04
280
281/* FIXME/jarod: make this a runtime option */
282/* select a same sample period like cir register */
283#define CIR_WAKE_IRCON_SAMPLE_PERIOD_SEL CIR_IRCON_SAMPLE_PERIOD_SEL_50
284
285/* CIR WAKE IRSTS Bits */
286#define CIR_WAKE_IRSTS_RDR 0x80
287#define CIR_WAKE_IRSTS_RTR 0x40
288#define CIR_WAKE_IRSTS_PE 0x20
289#define CIR_WAKE_IRSTS_RFO 0x10
290#define CIR_WAKE_IRSTS_GH 0x08
291#define CIR_WAKE_IRSTS_IR_PENDING 0x01
292
293/* CIR WAKE UP IREN Bits */
294#define CIR_WAKE_IREN_RDR 0x80
295#define CIR_WAKE_IREN_RTR 0x40
296#define CIR_WAKE_IREN_PE 0x20
297#define CIR_WAKE_IREN_RFO 0x10
298#define CIR_WAKE_IREN_TE 0x08
299#define CIR_WAKE_IREN_TTR 0x04
300#define CIR_WAKE_IREN_TFU 0x02
301#define CIR_WAKE_IREN_GH 0x01
302
303/* CIR WAKE FIFOCON settings */
304#define CIR_WAKE_FIFOCON_RXFIFOCLR 0x08
305
306#define CIR_WAKE_FIFOCON_RX_TRIGGER_LEV_67 0x00
307#define CIR_WAKE_FIFOCON_RX_TRIGGER_LEV_66 0x01
308#define CIR_WAKE_FIFOCON_RX_TRIGGER_LEV_65 0x02
309#define CIR_WAKE_FIFOCON_RX_TRIGGER_LEV_64 0x03
310
311/* FIXME: make this a runtime option */
312/* select WAKE UP RX trigger level as 67 */
313#define CIR_WAKE_FIFOCON_RX_TRIGGER_LEV CIR_WAKE_FIFOCON_RX_TRIGGER_LEV_67
314
315/* CIR WAKE SRXFSTS settings */
316#define CIR_WAKE_IRFIFOSTS_RX_GS 0x80
317#define CIR_WAKE_IRFIFOSTS_RX_FTA 0x40
318#define CIR_WAKE_IRFIFOSTS_RX_EMPTY 0x20
319#define CIR_WAKE_IRFIFOSTS_RX_FULL 0x10
320
Jarod Wilson3198ed12011-03-01 12:38:02 -0300321/*
322 * The CIR Wake FIFO buffer is 67 bytes long, but the stock remote wakes
323 * the system comparing only 65 bytes (fails with this set to 67)
324 */
325#define CIR_WAKE_FIFO_CMP_BYTES 65
Jarod Wilson6d2f5c22010-10-07 17:50:34 -0300326/* CIR Wake byte comparison tolerance */
327#define CIR_WAKE_CMP_TOLERANCE 5
328
329/*
330 * Extended Function Enable Registers:
331 * Extended Function Index Register
332 * Extended Function Data Register
333 */
334#define CR_EFIR 0x2e
335#define CR_EFDR 0x2f
336
337/* Possible alternate EFER values, depends on how the chip is wired */
338#define CR_EFIR2 0x4e
339#define CR_EFDR2 0x4f
340
341/* Extended Function Mode enable/disable magic values */
342#define EFER_EFM_ENABLE 0x87
343#define EFER_EFM_DISABLE 0xaa
344
Jarod Wilson6d2f5c22010-10-07 17:50:34 -0300345/* Config regs we need to care about */
346#define CR_SOFTWARE_RESET 0x02
347#define CR_LOGICAL_DEV_SEL 0x07
348#define CR_CHIP_ID_HI 0x20
349#define CR_CHIP_ID_LO 0x21
350#define CR_DEV_POWER_DOWN 0x22 /* bit 2 is CIR power, default power on */
351#define CR_OUTPUT_PIN_SEL 0x27
Jarod Wilson39381d42011-04-12 13:38:27 -0300352#define CR_MULTIFUNC_PIN_SEL 0x2c
Jarod Wilson6d2f5c22010-10-07 17:50:34 -0300353#define CR_LOGICAL_DEV_EN 0x30 /* valid for all logical devices */
354/* next three regs valid for both the CIR and CIR_WAKE logical devices */
355#define CR_CIR_BASE_ADDR_HI 0x60
356#define CR_CIR_BASE_ADDR_LO 0x61
357#define CR_CIR_IRQ_RSRC 0x70
358/* next three regs valid only for ACPI logical dev */
359#define CR_ACPI_CIR_WAKE 0xe0
360#define CR_ACPI_IRQ_EVENTS 0xf6
361#define CR_ACPI_IRQ_EVENTS2 0xf7
362
363/* Logical devices that we need to care about */
364#define LOGICAL_DEV_LPT 0x01
365#define LOGICAL_DEV_CIR 0x06
366#define LOGICAL_DEV_ACPI 0x0a
367#define LOGICAL_DEV_CIR_WAKE 0x0e
368
369#define LOGICAL_DEV_DISABLE 0x00
370#define LOGICAL_DEV_ENABLE 0x01
371
372#define CIR_WAKE_ENABLE_BIT 0x08
Jarod Wilson6d2f5c22010-10-07 17:50:34 -0300373#define PME_INTR_CIR_PASS_BIT 0x08
374
Jarod Wilson39381d42011-04-12 13:38:27 -0300375/* w83677hg CIR pin config */
Jarod Wilson6d2f5c22010-10-07 17:50:34 -0300376#define OUTPUT_PIN_SEL_MASK 0xbc
377#define OUTPUT_ENABLE_CIR 0x01 /* Pin95=CIRRX, Pin96=CIRTX1 */
378#define OUTPUT_ENABLE_CIRWB 0x40 /* enable wide-band sensor */
379
Jarod Wilson39381d42011-04-12 13:38:27 -0300380/* w83667hg CIR pin config */
381#define MULTIFUNC_PIN_SEL_MASK 0x1f
382#define MULTIFUNC_ENABLE_CIR 0x80 /* Pin75=CIRRX, Pin76=CIRTX1 */
383#define MULTIFUNC_ENABLE_CIRWB 0x20 /* enable wide-band sensor */
384
Jarod Wilson6d2f5c22010-10-07 17:50:34 -0300385/* MCE CIR signal length, related on sample period */
386
387/* MCE CIR controller signal length: about 43ms
388 * 43ms / 50us (sample period) * 0.85 (inaccuracy)
389 */
390#define CONTROLLER_BUF_LEN_MIN 830
391
392/* MCE CIR keyboard signal length: about 26ms
393 * 26ms / 50us (sample period) * 0.85 (inaccuracy)
394 */
395#define KEYBOARD_BUF_LEN_MAX 650
396#define KEYBOARD_BUF_LEN_MIN 610
397
398/* MCE CIR mouse signal length: about 24ms
399 * 24ms / 50us (sample period) * 0.85 (inaccuracy)
400 */
401#define MOUSE_BUF_LEN_MIN 565
402
403#define CIR_SAMPLE_PERIOD 50
404#define CIR_SAMPLE_LOW_INACCURACY 0.85
405
406/* MAX silence time that driver will sent to lirc */
407#define MAX_SILENCE_TIME 60000
408
409#if CIR_IRCON_SAMPLE_PERIOD_SEL == CIR_IRCON_SAMPLE_PERIOD_SEL_100
410#define SAMPLE_PERIOD 100
411
412#elif CIR_IRCON_SAMPLE_PERIOD_SEL == CIR_IRCON_SAMPLE_PERIOD_SEL_50
413#define SAMPLE_PERIOD 50
414
415#elif CIR_IRCON_SAMPLE_PERIOD_SEL == CIR_IRCON_SAMPLE_PERIOD_SEL_25
416#define SAMPLE_PERIOD 25
417
418#else
419#define SAMPLE_PERIOD 1
420#endif
421
422/* as VISTA MCE definition, valid carrier value */
423#define MAX_CARRIER 60000
424#define MIN_CARRIER 30000