2 * Driver for Nuvoton Technology Corporation w83667hg/w83677hg-i CIR
4 * Copyright (C) 2010 Jarod Wilson <jarod@redhat.com>
5 * Copyright (C) 2009 Nuvoton PS Team
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
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.
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.
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
28 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/pnp.h>
34 #include <linux/interrupt.h>
35 #include <linux/sched.h>
36 #include <linux/slab.h>
37 #include <media/rc-core.h>
38 #include <linux/pci_ids.h>
40 #include "nuvoton-cir.h"
42 static void nvt_clear_cir_wake_fifo(struct nvt_dev
*nvt
);
44 static const struct nvt_chip nvt_chips
[] = {
45 { "w83667hg", NVT_W83667HG
},
46 { "NCT6775F", NVT_6775F
},
47 { "NCT6776F", NVT_6776F
},
48 { "NCT6779D", NVT_6779D
},
51 static inline struct device
*nvt_get_dev(const struct nvt_dev
*nvt
)
53 return nvt
->rdev
->dev
.parent
;
56 static inline bool is_w83667hg(struct nvt_dev
*nvt
)
58 return nvt
->chip_ver
== NVT_W83667HG
;
61 /* write val to config reg */
62 static inline void nvt_cr_write(struct nvt_dev
*nvt
, u8 val
, u8 reg
)
64 outb(reg
, nvt
->cr_efir
);
65 outb(val
, nvt
->cr_efdr
);
68 /* read val from config reg */
69 static inline u8
nvt_cr_read(struct nvt_dev
*nvt
, u8 reg
)
71 outb(reg
, nvt
->cr_efir
);
72 return inb(nvt
->cr_efdr
);
75 /* update config register bit without changing other bits */
76 static inline void nvt_set_reg_bit(struct nvt_dev
*nvt
, u8 val
, u8 reg
)
78 u8 tmp
= nvt_cr_read(nvt
, reg
) | val
;
79 nvt_cr_write(nvt
, tmp
, reg
);
82 /* clear config register bit without changing other bits */
83 static inline void nvt_clear_reg_bit(struct nvt_dev
*nvt
, u8 val
, u8 reg
)
85 u8 tmp
= nvt_cr_read(nvt
, reg
) & ~val
;
86 nvt_cr_write(nvt
, tmp
, reg
);
89 /* enter extended function mode */
90 static inline int nvt_efm_enable(struct nvt_dev
*nvt
)
92 if (!request_muxed_region(nvt
->cr_efir
, 2, NVT_DRIVER_NAME
))
95 /* Enabling Extended Function Mode explicitly requires writing 2x */
96 outb(EFER_EFM_ENABLE
, nvt
->cr_efir
);
97 outb(EFER_EFM_ENABLE
, nvt
->cr_efir
);
102 /* exit extended function mode */
103 static inline void nvt_efm_disable(struct nvt_dev
*nvt
)
105 outb(EFER_EFM_DISABLE
, nvt
->cr_efir
);
107 release_region(nvt
->cr_efir
, 2);
111 * When you want to address a specific logical device, write its logical
112 * device number to CR_LOGICAL_DEV_SEL, then enable/disable by writing
113 * 0x1/0x0 respectively to CR_LOGICAL_DEV_EN.
115 static inline void nvt_select_logical_dev(struct nvt_dev
*nvt
, u8 ldev
)
117 nvt_cr_write(nvt
, ldev
, CR_LOGICAL_DEV_SEL
);
120 /* select and enable logical device with setting EFM mode*/
121 static inline void nvt_enable_logical_dev(struct nvt_dev
*nvt
, u8 ldev
)
124 nvt_select_logical_dev(nvt
, ldev
);
125 nvt_cr_write(nvt
, LOGICAL_DEV_ENABLE
, CR_LOGICAL_DEV_EN
);
126 nvt_efm_disable(nvt
);
129 /* select and disable logical device with setting EFM mode*/
130 static inline void nvt_disable_logical_dev(struct nvt_dev
*nvt
, u8 ldev
)
133 nvt_select_logical_dev(nvt
, ldev
);
134 nvt_cr_write(nvt
, LOGICAL_DEV_DISABLE
, CR_LOGICAL_DEV_EN
);
135 nvt_efm_disable(nvt
);
138 /* write val to cir config register */
139 static inline void nvt_cir_reg_write(struct nvt_dev
*nvt
, u8 val
, u8 offset
)
141 outb(val
, nvt
->cir_addr
+ offset
);
144 /* read val from cir config register */
145 static u8
nvt_cir_reg_read(struct nvt_dev
*nvt
, u8 offset
)
147 return inb(nvt
->cir_addr
+ offset
);
150 /* write val to cir wake register */
151 static inline void nvt_cir_wake_reg_write(struct nvt_dev
*nvt
,
154 outb(val
, nvt
->cir_wake_addr
+ offset
);
157 /* read val from cir wake config register */
158 static u8
nvt_cir_wake_reg_read(struct nvt_dev
*nvt
, u8 offset
)
160 return inb(nvt
->cir_wake_addr
+ offset
);
163 /* don't override io address if one is set already */
164 static void nvt_set_ioaddr(struct nvt_dev
*nvt
, unsigned long *ioaddr
)
166 unsigned long old_addr
;
168 old_addr
= nvt_cr_read(nvt
, CR_CIR_BASE_ADDR_HI
) << 8;
169 old_addr
|= nvt_cr_read(nvt
, CR_CIR_BASE_ADDR_LO
);
174 nvt_cr_write(nvt
, *ioaddr
>> 8, CR_CIR_BASE_ADDR_HI
);
175 nvt_cr_write(nvt
, *ioaddr
& 0xff, CR_CIR_BASE_ADDR_LO
);
179 static ssize_t
wakeup_data_show(struct device
*dev
,
180 struct device_attribute
*attr
,
183 struct rc_dev
*rc_dev
= to_rc_dev(dev
);
184 struct nvt_dev
*nvt
= rc_dev
->priv
;
185 int fifo_len
, duration
;
190 spin_lock_irqsave(&nvt
->lock
, flags
);
192 fifo_len
= nvt_cir_wake_reg_read(nvt
, CIR_WAKE_FIFO_COUNT
);
193 fifo_len
= min(fifo_len
, WAKEUP_MAX_SIZE
);
195 /* go to first element to be read */
196 while (nvt_cir_wake_reg_read(nvt
, CIR_WAKE_RD_FIFO_ONLY_IDX
))
197 nvt_cir_wake_reg_read(nvt
, CIR_WAKE_RD_FIFO_ONLY
);
199 for (i
= 0; i
< fifo_len
; i
++) {
200 duration
= nvt_cir_wake_reg_read(nvt
, CIR_WAKE_RD_FIFO_ONLY
);
201 duration
= (duration
& BUF_LEN_MASK
) * SAMPLE_PERIOD
;
202 buf_len
+= snprintf(buf
+ buf_len
, PAGE_SIZE
- buf_len
,
205 buf_len
+= snprintf(buf
+ buf_len
, PAGE_SIZE
- buf_len
, "\n");
207 spin_unlock_irqrestore(&nvt
->lock
, flags
);
212 static ssize_t
wakeup_data_store(struct device
*dev
,
213 struct device_attribute
*attr
,
214 const char *buf
, size_t len
)
216 struct rc_dev
*rc_dev
= to_rc_dev(dev
);
217 struct nvt_dev
*nvt
= rc_dev
->priv
;
219 u8 tolerance
, config
, wake_buf
[WAKEUP_MAX_SIZE
];
225 argv
= argv_split(GFP_KERNEL
, buf
, &count
);
228 if (!count
|| count
> WAKEUP_MAX_SIZE
) {
233 for (i
= 0; i
< count
; i
++) {
234 ret
= kstrtouint(argv
[i
], 10, &val
);
237 val
= DIV_ROUND_CLOSEST(val
, SAMPLE_PERIOD
);
238 if (!val
|| val
> 0x7f) {
243 /* sequence must start with a pulse */
245 wake_buf
[i
] |= BUF_PULSE_BIT
;
248 /* hardcode the tolerance to 10% */
249 tolerance
= DIV_ROUND_UP(count
, 10);
251 spin_lock_irqsave(&nvt
->lock
, flags
);
253 nvt_clear_cir_wake_fifo(nvt
);
254 nvt_cir_wake_reg_write(nvt
, count
, CIR_WAKE_FIFO_CMP_DEEP
);
255 nvt_cir_wake_reg_write(nvt
, tolerance
, CIR_WAKE_FIFO_CMP_TOL
);
257 config
= nvt_cir_wake_reg_read(nvt
, CIR_WAKE_IRCON
);
259 /* enable writes to wake fifo */
260 nvt_cir_wake_reg_write(nvt
, config
| CIR_WAKE_IRCON_MODE1
,
263 for (i
= 0; i
< count
; i
++)
264 nvt_cir_wake_reg_write(nvt
, wake_buf
[i
], CIR_WAKE_WR_FIFO_DATA
);
266 nvt_cir_wake_reg_write(nvt
, config
, CIR_WAKE_IRCON
);
268 spin_unlock_irqrestore(&nvt
->lock
, flags
);
275 static DEVICE_ATTR_RW(wakeup_data
);
277 /* dump current cir register contents */
278 static void cir_dump_regs(struct nvt_dev
*nvt
)
281 nvt_select_logical_dev(nvt
, LOGICAL_DEV_CIR
);
283 pr_info("%s: Dump CIR logical device registers:\n", NVT_DRIVER_NAME
);
284 pr_info(" * CR CIR ACTIVE : 0x%x\n",
285 nvt_cr_read(nvt
, CR_LOGICAL_DEV_EN
));
286 pr_info(" * CR CIR BASE ADDR: 0x%x\n",
287 (nvt_cr_read(nvt
, CR_CIR_BASE_ADDR_HI
) << 8) |
288 nvt_cr_read(nvt
, CR_CIR_BASE_ADDR_LO
));
289 pr_info(" * CR CIR IRQ NUM: 0x%x\n",
290 nvt_cr_read(nvt
, CR_CIR_IRQ_RSRC
));
292 nvt_efm_disable(nvt
);
294 pr_info("%s: Dump CIR registers:\n", NVT_DRIVER_NAME
);
295 pr_info(" * IRCON: 0x%x\n", nvt_cir_reg_read(nvt
, CIR_IRCON
));
296 pr_info(" * IRSTS: 0x%x\n", nvt_cir_reg_read(nvt
, CIR_IRSTS
));
297 pr_info(" * IREN: 0x%x\n", nvt_cir_reg_read(nvt
, CIR_IREN
));
298 pr_info(" * RXFCONT: 0x%x\n", nvt_cir_reg_read(nvt
, CIR_RXFCONT
));
299 pr_info(" * CP: 0x%x\n", nvt_cir_reg_read(nvt
, CIR_CP
));
300 pr_info(" * CC: 0x%x\n", nvt_cir_reg_read(nvt
, CIR_CC
));
301 pr_info(" * SLCH: 0x%x\n", nvt_cir_reg_read(nvt
, CIR_SLCH
));
302 pr_info(" * SLCL: 0x%x\n", nvt_cir_reg_read(nvt
, CIR_SLCL
));
303 pr_info(" * FIFOCON: 0x%x\n", nvt_cir_reg_read(nvt
, CIR_FIFOCON
));
304 pr_info(" * IRFIFOSTS: 0x%x\n", nvt_cir_reg_read(nvt
, CIR_IRFIFOSTS
));
305 pr_info(" * SRXFIFO: 0x%x\n", nvt_cir_reg_read(nvt
, CIR_SRXFIFO
));
306 pr_info(" * TXFCONT: 0x%x\n", nvt_cir_reg_read(nvt
, CIR_TXFCONT
));
307 pr_info(" * STXFIFO: 0x%x\n", nvt_cir_reg_read(nvt
, CIR_STXFIFO
));
308 pr_info(" * FCCH: 0x%x\n", nvt_cir_reg_read(nvt
, CIR_FCCH
));
309 pr_info(" * FCCL: 0x%x\n", nvt_cir_reg_read(nvt
, CIR_FCCL
));
310 pr_info(" * IRFSM: 0x%x\n", nvt_cir_reg_read(nvt
, CIR_IRFSM
));
313 /* dump current cir wake register contents */
314 static void cir_wake_dump_regs(struct nvt_dev
*nvt
)
319 nvt_select_logical_dev(nvt
, LOGICAL_DEV_CIR_WAKE
);
321 pr_info("%s: Dump CIR WAKE logical device registers:\n",
323 pr_info(" * CR CIR WAKE ACTIVE : 0x%x\n",
324 nvt_cr_read(nvt
, CR_LOGICAL_DEV_EN
));
325 pr_info(" * CR CIR WAKE BASE ADDR: 0x%x\n",
326 (nvt_cr_read(nvt
, CR_CIR_BASE_ADDR_HI
) << 8) |
327 nvt_cr_read(nvt
, CR_CIR_BASE_ADDR_LO
));
328 pr_info(" * CR CIR WAKE IRQ NUM: 0x%x\n",
329 nvt_cr_read(nvt
, CR_CIR_IRQ_RSRC
));
331 nvt_efm_disable(nvt
);
333 pr_info("%s: Dump CIR WAKE registers\n", NVT_DRIVER_NAME
);
334 pr_info(" * IRCON: 0x%x\n",
335 nvt_cir_wake_reg_read(nvt
, CIR_WAKE_IRCON
));
336 pr_info(" * IRSTS: 0x%x\n",
337 nvt_cir_wake_reg_read(nvt
, CIR_WAKE_IRSTS
));
338 pr_info(" * IREN: 0x%x\n",
339 nvt_cir_wake_reg_read(nvt
, CIR_WAKE_IREN
));
340 pr_info(" * FIFO CMP DEEP: 0x%x\n",
341 nvt_cir_wake_reg_read(nvt
, CIR_WAKE_FIFO_CMP_DEEP
));
342 pr_info(" * FIFO CMP TOL: 0x%x\n",
343 nvt_cir_wake_reg_read(nvt
, CIR_WAKE_FIFO_CMP_TOL
));
344 pr_info(" * FIFO COUNT: 0x%x\n",
345 nvt_cir_wake_reg_read(nvt
, CIR_WAKE_FIFO_COUNT
));
346 pr_info(" * SLCH: 0x%x\n",
347 nvt_cir_wake_reg_read(nvt
, CIR_WAKE_SLCH
));
348 pr_info(" * SLCL: 0x%x\n",
349 nvt_cir_wake_reg_read(nvt
, CIR_WAKE_SLCL
));
350 pr_info(" * FIFOCON: 0x%x\n",
351 nvt_cir_wake_reg_read(nvt
, CIR_WAKE_FIFOCON
));
352 pr_info(" * SRXFSTS: 0x%x\n",
353 nvt_cir_wake_reg_read(nvt
, CIR_WAKE_SRXFSTS
));
354 pr_info(" * SAMPLE RX FIFO: 0x%x\n",
355 nvt_cir_wake_reg_read(nvt
, CIR_WAKE_SAMPLE_RX_FIFO
));
356 pr_info(" * WR FIFO DATA: 0x%x\n",
357 nvt_cir_wake_reg_read(nvt
, CIR_WAKE_WR_FIFO_DATA
));
358 pr_info(" * RD FIFO ONLY: 0x%x\n",
359 nvt_cir_wake_reg_read(nvt
, CIR_WAKE_RD_FIFO_ONLY
));
360 pr_info(" * RD FIFO ONLY IDX: 0x%x\n",
361 nvt_cir_wake_reg_read(nvt
, CIR_WAKE_RD_FIFO_ONLY_IDX
));
362 pr_info(" * FIFO IGNORE: 0x%x\n",
363 nvt_cir_wake_reg_read(nvt
, CIR_WAKE_FIFO_IGNORE
));
364 pr_info(" * IRFSM: 0x%x\n",
365 nvt_cir_wake_reg_read(nvt
, CIR_WAKE_IRFSM
));
367 fifo_len
= nvt_cir_wake_reg_read(nvt
, CIR_WAKE_FIFO_COUNT
);
368 pr_info("%s: Dump CIR WAKE FIFO (len %d)\n", NVT_DRIVER_NAME
, fifo_len
);
369 pr_info("* Contents =");
370 for (i
= 0; i
< fifo_len
; i
++)
372 nvt_cir_wake_reg_read(nvt
, CIR_WAKE_RD_FIFO_ONLY
));
376 static inline const char *nvt_find_chip(struct nvt_dev
*nvt
, int id
)
380 for (i
= 0; i
< ARRAY_SIZE(nvt_chips
); i
++)
381 if ((id
& SIO_ID_MASK
) == nvt_chips
[i
].chip_ver
) {
382 nvt
->chip_ver
= nvt_chips
[i
].chip_ver
;
383 return nvt_chips
[i
].name
;
390 /* detect hardware features */
391 static int nvt_hw_detect(struct nvt_dev
*nvt
)
393 struct device
*dev
= nvt_get_dev(nvt
);
394 const char *chip_name
;
399 /* Check if we're wired for the alternate EFER setup */
400 nvt
->chip_major
= nvt_cr_read(nvt
, CR_CHIP_ID_HI
);
401 if (nvt
->chip_major
== 0xff) {
402 nvt_efm_disable(nvt
);
403 nvt
->cr_efir
= CR_EFIR2
;
404 nvt
->cr_efdr
= CR_EFDR2
;
406 nvt
->chip_major
= nvt_cr_read(nvt
, CR_CHIP_ID_HI
);
408 nvt
->chip_minor
= nvt_cr_read(nvt
, CR_CHIP_ID_LO
);
410 nvt_efm_disable(nvt
);
412 chip_id
= nvt
->chip_major
<< 8 | nvt
->chip_minor
;
413 if (chip_id
== NVT_INVALID
) {
414 dev_err(dev
, "No device found on either EFM port\n");
418 chip_name
= nvt_find_chip(nvt
, chip_id
);
420 /* warn, but still let the driver load, if we don't know this chip */
423 "unknown chip, id: 0x%02x 0x%02x, it may not work...",
424 nvt
->chip_major
, nvt
->chip_minor
);
426 dev_info(dev
, "found %s or compatible: chip id: 0x%02x 0x%02x",
427 chip_name
, nvt
->chip_major
, nvt
->chip_minor
);
432 static void nvt_cir_ldev_init(struct nvt_dev
*nvt
)
434 u8 val
, psreg
, psmask
, psval
;
436 if (is_w83667hg(nvt
)) {
437 psreg
= CR_MULTIFUNC_PIN_SEL
;
438 psmask
= MULTIFUNC_PIN_SEL_MASK
;
439 psval
= MULTIFUNC_ENABLE_CIR
| MULTIFUNC_ENABLE_CIRWB
;
441 psreg
= CR_OUTPUT_PIN_SEL
;
442 psmask
= OUTPUT_PIN_SEL_MASK
;
443 psval
= OUTPUT_ENABLE_CIR
| OUTPUT_ENABLE_CIRWB
;
446 /* output pin selection: enable CIR, with WB sensor enabled */
447 val
= nvt_cr_read(nvt
, psreg
);
450 nvt_cr_write(nvt
, val
, psreg
);
452 /* Select CIR logical device */
453 nvt_select_logical_dev(nvt
, LOGICAL_DEV_CIR
);
455 nvt_set_ioaddr(nvt
, &nvt
->cir_addr
);
457 nvt_cr_write(nvt
, nvt
->cir_irq
, CR_CIR_IRQ_RSRC
);
459 nvt_dbg("CIR initialized, base io port address: 0x%lx, irq: %d",
460 nvt
->cir_addr
, nvt
->cir_irq
);
463 static void nvt_cir_wake_ldev_init(struct nvt_dev
*nvt
)
465 /* Select ACPI logical device and anable it */
466 nvt_select_logical_dev(nvt
, LOGICAL_DEV_ACPI
);
467 nvt_cr_write(nvt
, LOGICAL_DEV_ENABLE
, CR_LOGICAL_DEV_EN
);
469 /* Enable CIR Wake via PSOUT# (Pin60) */
470 nvt_set_reg_bit(nvt
, CIR_WAKE_ENABLE_BIT
, CR_ACPI_CIR_WAKE
);
472 /* enable pme interrupt of cir wakeup event */
473 nvt_set_reg_bit(nvt
, PME_INTR_CIR_PASS_BIT
, CR_ACPI_IRQ_EVENTS2
);
475 /* Select CIR Wake logical device */
476 nvt_select_logical_dev(nvt
, LOGICAL_DEV_CIR_WAKE
);
478 nvt_set_ioaddr(nvt
, &nvt
->cir_wake_addr
);
480 nvt_dbg("CIR Wake initialized, base io port address: 0x%lx",
484 /* clear out the hardware's cir rx fifo */
485 static void nvt_clear_cir_fifo(struct nvt_dev
*nvt
)
487 u8 val
= nvt_cir_reg_read(nvt
, CIR_FIFOCON
);
488 nvt_cir_reg_write(nvt
, val
| CIR_FIFOCON_RXFIFOCLR
, CIR_FIFOCON
);
491 /* clear out the hardware's cir wake rx fifo */
492 static void nvt_clear_cir_wake_fifo(struct nvt_dev
*nvt
)
496 config
= nvt_cir_wake_reg_read(nvt
, CIR_WAKE_IRCON
);
498 /* clearing wake fifo works in learning mode only */
499 nvt_cir_wake_reg_write(nvt
, config
& ~CIR_WAKE_IRCON_MODE0
,
502 val
= nvt_cir_wake_reg_read(nvt
, CIR_WAKE_FIFOCON
);
503 nvt_cir_wake_reg_write(nvt
, val
| CIR_WAKE_FIFOCON_RXFIFOCLR
,
506 nvt_cir_wake_reg_write(nvt
, config
, CIR_WAKE_IRCON
);
509 /* clear out the hardware's cir tx fifo */
510 static void nvt_clear_tx_fifo(struct nvt_dev
*nvt
)
514 val
= nvt_cir_reg_read(nvt
, CIR_FIFOCON
);
515 nvt_cir_reg_write(nvt
, val
| CIR_FIFOCON_TXFIFOCLR
, CIR_FIFOCON
);
518 /* enable RX Trigger Level Reach and Packet End interrupts */
519 static void nvt_set_cir_iren(struct nvt_dev
*nvt
)
523 iren
= CIR_IREN_RTR
| CIR_IREN_PE
| CIR_IREN_RFO
;
524 nvt_cir_reg_write(nvt
, iren
, CIR_IREN
);
527 static void nvt_cir_regs_init(struct nvt_dev
*nvt
)
529 /* set sample limit count (PE interrupt raised when reached) */
530 nvt_cir_reg_write(nvt
, CIR_RX_LIMIT_COUNT
>> 8, CIR_SLCH
);
531 nvt_cir_reg_write(nvt
, CIR_RX_LIMIT_COUNT
& 0xff, CIR_SLCL
);
533 /* set fifo irq trigger levels */
534 nvt_cir_reg_write(nvt
, CIR_FIFOCON_TX_TRIGGER_LEV
|
535 CIR_FIFOCON_RX_TRIGGER_LEV
, CIR_FIFOCON
);
538 * Enable TX and RX, specify carrier on = low, off = high, and set
539 * sample period (currently 50us)
541 nvt_cir_reg_write(nvt
,
542 CIR_IRCON_TXEN
| CIR_IRCON_RXEN
|
543 CIR_IRCON_RXINV
| CIR_IRCON_SAMPLE_PERIOD_SEL
,
546 /* clear hardware rx and tx fifos */
547 nvt_clear_cir_fifo(nvt
);
548 nvt_clear_tx_fifo(nvt
);
550 /* clear any and all stray interrupts */
551 nvt_cir_reg_write(nvt
, 0xff, CIR_IRSTS
);
553 /* and finally, enable interrupts */
554 nvt_set_cir_iren(nvt
);
556 /* enable the CIR logical device */
557 nvt_enable_logical_dev(nvt
, LOGICAL_DEV_CIR
);
560 static void nvt_cir_wake_regs_init(struct nvt_dev
*nvt
)
563 * Disable RX, set specific carrier on = low, off = high,
564 * and sample period (currently 50us)
566 nvt_cir_wake_reg_write(nvt
, CIR_WAKE_IRCON_MODE0
|
567 CIR_WAKE_IRCON_R
| CIR_WAKE_IRCON_RXINV
|
568 CIR_WAKE_IRCON_SAMPLE_PERIOD_SEL
,
571 /* clear any and all stray interrupts */
572 nvt_cir_wake_reg_write(nvt
, 0xff, CIR_WAKE_IRSTS
);
574 /* enable the CIR WAKE logical device */
575 nvt_enable_logical_dev(nvt
, LOGICAL_DEV_CIR_WAKE
);
578 static void nvt_enable_wake(struct nvt_dev
*nvt
)
584 nvt_select_logical_dev(nvt
, LOGICAL_DEV_ACPI
);
585 nvt_set_reg_bit(nvt
, CIR_WAKE_ENABLE_BIT
, CR_ACPI_CIR_WAKE
);
586 nvt_set_reg_bit(nvt
, PME_INTR_CIR_PASS_BIT
, CR_ACPI_IRQ_EVENTS2
);
588 nvt_select_logical_dev(nvt
, LOGICAL_DEV_CIR_WAKE
);
589 nvt_cr_write(nvt
, LOGICAL_DEV_ENABLE
, CR_LOGICAL_DEV_EN
);
591 nvt_efm_disable(nvt
);
593 spin_lock_irqsave(&nvt
->lock
, flags
);
595 nvt_cir_wake_reg_write(nvt
, CIR_WAKE_IRCON_MODE0
| CIR_WAKE_IRCON_RXEN
|
596 CIR_WAKE_IRCON_R
| CIR_WAKE_IRCON_RXINV
|
597 CIR_WAKE_IRCON_SAMPLE_PERIOD_SEL
,
599 nvt_cir_wake_reg_write(nvt
, 0xff, CIR_WAKE_IRSTS
);
600 nvt_cir_wake_reg_write(nvt
, 0, CIR_WAKE_IREN
);
602 spin_unlock_irqrestore(&nvt
->lock
, flags
);
605 #if 0 /* Currently unused */
606 /* rx carrier detect only works in learning mode, must be called w/lock */
607 static u32
nvt_rx_carrier_detect(struct nvt_dev
*nvt
)
609 u32 count
, carrier
, duration
= 0;
612 count
= nvt_cir_reg_read(nvt
, CIR_FCCL
) |
613 nvt_cir_reg_read(nvt
, CIR_FCCH
) << 8;
615 for (i
= 0; i
< nvt
->pkts
; i
++) {
616 if (nvt
->buf
[i
] & BUF_PULSE_BIT
)
617 duration
+= nvt
->buf
[i
] & BUF_LEN_MASK
;
620 duration
*= SAMPLE_PERIOD
;
622 if (!count
|| !duration
) {
623 dev_notice(nvt_get_dev(nvt
),
624 "Unable to determine carrier! (c:%u, d:%u)",
629 carrier
= MS_TO_NS(count
) / duration
;
631 if ((carrier
> MAX_CARRIER
) || (carrier
< MIN_CARRIER
))
632 nvt_dbg("WTF? Carrier frequency out of range!");
634 nvt_dbg("Carrier frequency: %u (count %u, duration %u)",
635 carrier
, count
, duration
);
641 * set carrier frequency
643 * set carrier on 2 registers: CP & CC
644 * always set CP as 0x81
645 * set CC by SPEC, CC = 3MHz/carrier - 1
647 static int nvt_set_tx_carrier(struct rc_dev
*dev
, u32 carrier
)
649 struct nvt_dev
*nvt
= dev
->priv
;
655 nvt_cir_reg_write(nvt
, 1, CIR_CP
);
656 val
= 3000000 / (carrier
) - 1;
657 nvt_cir_reg_write(nvt
, val
& 0xff, CIR_CC
);
659 nvt_dbg("cp: 0x%x cc: 0x%x\n",
660 nvt_cir_reg_read(nvt
, CIR_CP
), nvt_cir_reg_read(nvt
, CIR_CC
));
668 * 1) clean TX fifo first (handled by AP)
669 * 2) copy data from user space
670 * 3) disable RX interrupts, enable TX interrupts: TTR & TFU
671 * 4) send 9 packets to TX FIFO to open TTR
672 * in interrupt_handler:
673 * 5) send all data out
674 * go back to write():
675 * 6) disable TX interrupts, re-enable RX interupts
677 * The key problem of this function is user space data may larger than
678 * driver's data buf length. So nvt_tx_ir() will only copy TX_BUF_LEN data to
679 * buf, and keep current copied data buf num in cur_buf_num. But driver's buf
680 * number may larger than TXFCONT (0xff). So in interrupt_handler, it has to
681 * set TXFCONT as 0xff, until buf_count less than 0xff.
683 static int nvt_tx_ir(struct rc_dev
*dev
, unsigned *txbuf
, unsigned n
)
685 struct nvt_dev
*nvt
= dev
->priv
;
691 spin_lock_irqsave(&nvt
->lock
, flags
);
693 ret
= min((unsigned)(TX_BUF_LEN
/ sizeof(unsigned)), n
);
694 nvt
->tx
.buf_count
= (ret
* sizeof(unsigned));
696 memcpy(nvt
->tx
.buf
, txbuf
, nvt
->tx
.buf_count
);
698 nvt
->tx
.cur_buf_num
= 0;
700 /* save currently enabled interrupts */
701 iren
= nvt_cir_reg_read(nvt
, CIR_IREN
);
703 /* now disable all interrupts, save TFU & TTR */
704 nvt_cir_reg_write(nvt
, CIR_IREN_TFU
| CIR_IREN_TTR
, CIR_IREN
);
706 nvt
->tx
.tx_state
= ST_TX_REPLY
;
708 nvt_cir_reg_write(nvt
, CIR_FIFOCON_TX_TRIGGER_LEV_8
|
709 CIR_FIFOCON_RXFIFOCLR
, CIR_FIFOCON
);
711 /* trigger TTR interrupt by writing out ones, (yes, it's ugly) */
712 for (i
= 0; i
< 9; i
++)
713 nvt_cir_reg_write(nvt
, 0x01, CIR_STXFIFO
);
715 spin_unlock_irqrestore(&nvt
->lock
, flags
);
717 wait_event(nvt
->tx
.queue
, nvt
->tx
.tx_state
== ST_TX_REQUEST
);
719 spin_lock_irqsave(&nvt
->lock
, flags
);
720 nvt
->tx
.tx_state
= ST_TX_NONE
;
721 spin_unlock_irqrestore(&nvt
->lock
, flags
);
723 /* restore enabled interrupts to prior state */
724 nvt_cir_reg_write(nvt
, iren
, CIR_IREN
);
729 /* dump contents of the last rx buffer we got from the hw rx fifo */
730 static void nvt_dump_rx_buf(struct nvt_dev
*nvt
)
734 printk(KERN_DEBUG
"%s (len %d): ", __func__
, nvt
->pkts
);
735 for (i
= 0; (i
< nvt
->pkts
) && (i
< RX_BUF_LEN
); i
++)
736 printk(KERN_CONT
"0x%02x ", nvt
->buf
[i
]);
737 printk(KERN_CONT
"\n");
741 * Process raw data in rx driver buffer, store it in raw IR event kfifo,
742 * trigger decode when appropriate.
744 * We get IR data samples one byte at a time. If the msb is set, its a pulse,
745 * otherwise its a space. The lower 7 bits are the count of SAMPLE_PERIOD
746 * (default 50us) intervals for that pulse/space. A discrete signal is
747 * followed by a series of 0x7f packets, then either 0x7<something> or 0x80
748 * to signal more IR coming (repeats) or end of IR, respectively. We store
749 * sample data in the raw event kfifo until we see 0x7<something> (except f)
750 * or 0x80, at which time, we trigger a decode operation.
752 static void nvt_process_rx_ir_data(struct nvt_dev
*nvt
)
754 DEFINE_IR_RAW_EVENT(rawir
);
758 nvt_dbg_verbose("%s firing", __func__
);
761 nvt_dump_rx_buf(nvt
);
763 nvt_dbg_verbose("Processing buffer of len %d", nvt
->pkts
);
765 for (i
= 0; i
< nvt
->pkts
; i
++) {
766 sample
= nvt
->buf
[i
];
768 rawir
.pulse
= ((sample
& BUF_PULSE_BIT
) != 0);
769 rawir
.duration
= US_TO_NS((sample
& BUF_LEN_MASK
)
772 nvt_dbg("Storing %s with duration %d",
773 rawir
.pulse
? "pulse" : "space", rawir
.duration
);
775 ir_raw_event_store_with_filter(nvt
->rdev
, &rawir
);
780 nvt_dbg("Calling ir_raw_event_handle\n");
781 ir_raw_event_handle(nvt
->rdev
);
783 nvt_dbg_verbose("%s done", __func__
);
786 static void nvt_handle_rx_fifo_overrun(struct nvt_dev
*nvt
)
788 dev_warn(nvt_get_dev(nvt
), "RX FIFO overrun detected, flushing data!");
791 nvt_clear_cir_fifo(nvt
);
792 ir_raw_event_reset(nvt
->rdev
);
795 /* copy data from hardware rx fifo into driver buffer */
796 static void nvt_get_rx_ir_data(struct nvt_dev
*nvt
)
801 /* Get count of how many bytes to read from RX FIFO */
802 fifocount
= nvt_cir_reg_read(nvt
, CIR_RXFCONT
);
804 nvt_dbg("attempting to fetch %u bytes from hw rx fifo", fifocount
);
806 /* Read fifocount bytes from CIR Sample RX FIFO register */
807 for (i
= 0; i
< fifocount
; i
++)
808 nvt
->buf
[i
] = nvt_cir_reg_read(nvt
, CIR_SRXFIFO
);
810 nvt
->pkts
= fifocount
;
811 nvt_dbg("%s: pkts now %d", __func__
, nvt
->pkts
);
813 nvt_process_rx_ir_data(nvt
);
816 static void nvt_cir_log_irqs(u8 status
, u8 iren
)
818 nvt_dbg("IRQ 0x%02x (IREN 0x%02x) :%s%s%s%s%s%s%s%s%s",
820 status
& CIR_IRSTS_RDR
? " RDR" : "",
821 status
& CIR_IRSTS_RTR
? " RTR" : "",
822 status
& CIR_IRSTS_PE
? " PE" : "",
823 status
& CIR_IRSTS_RFO
? " RFO" : "",
824 status
& CIR_IRSTS_TE
? " TE" : "",
825 status
& CIR_IRSTS_TTR
? " TTR" : "",
826 status
& CIR_IRSTS_TFU
? " TFU" : "",
827 status
& CIR_IRSTS_GH
? " GH" : "",
828 status
& ~(CIR_IRSTS_RDR
| CIR_IRSTS_RTR
| CIR_IRSTS_PE
|
829 CIR_IRSTS_RFO
| CIR_IRSTS_TE
| CIR_IRSTS_TTR
|
830 CIR_IRSTS_TFU
| CIR_IRSTS_GH
) ? " ?" : "");
833 static bool nvt_cir_tx_inactive(struct nvt_dev
*nvt
)
835 return nvt
->tx
.tx_state
== ST_TX_NONE
;
838 /* interrupt service routine for incoming and outgoing CIR data */
839 static irqreturn_t
nvt_cir_isr(int irq
, void *data
)
841 struct nvt_dev
*nvt
= data
;
844 nvt_dbg_verbose("%s firing", __func__
);
846 spin_lock(&nvt
->lock
);
849 * Get IR Status register contents. Write 1 to ack/clear
851 * bit: reg name - description
852 * 7: CIR_IRSTS_RDR - RX Data Ready
853 * 6: CIR_IRSTS_RTR - RX FIFO Trigger Level Reach
854 * 5: CIR_IRSTS_PE - Packet End
855 * 4: CIR_IRSTS_RFO - RX FIFO Overrun (RDR will also be set)
856 * 3: CIR_IRSTS_TE - TX FIFO Empty
857 * 2: CIR_IRSTS_TTR - TX FIFO Trigger Level Reach
858 * 1: CIR_IRSTS_TFU - TX FIFO Underrun
859 * 0: CIR_IRSTS_GH - Min Length Detected
861 status
= nvt_cir_reg_read(nvt
, CIR_IRSTS
);
862 iren
= nvt_cir_reg_read(nvt
, CIR_IREN
);
864 /* At least NCT6779D creates a spurious interrupt when the
865 * logical device is being disabled.
867 if (status
== 0xff && iren
== 0xff) {
868 spin_unlock(&nvt
->lock
);
869 nvt_dbg_verbose("Spurious interrupt detected");
873 /* IRQ may be shared with CIR WAKE, therefore check for each
874 * status bit whether the related interrupt source is enabled
876 if (!(status
& iren
)) {
877 spin_unlock(&nvt
->lock
);
878 nvt_dbg_verbose("%s exiting, IRSTS 0x0", __func__
);
882 /* ack/clear all irq flags we've got */
883 nvt_cir_reg_write(nvt
, status
, CIR_IRSTS
);
884 nvt_cir_reg_write(nvt
, 0, CIR_IRSTS
);
886 nvt_cir_log_irqs(status
, iren
);
888 if (status
& CIR_IRSTS_RFO
)
889 nvt_handle_rx_fifo_overrun(nvt
);
891 else if (status
& (CIR_IRSTS_RTR
| CIR_IRSTS_PE
)) {
892 /* We only do rx if not tx'ing */
893 if (nvt_cir_tx_inactive(nvt
))
894 nvt_get_rx_ir_data(nvt
);
897 if (status
& CIR_IRSTS_TE
)
898 nvt_clear_tx_fifo(nvt
);
900 if (status
& CIR_IRSTS_TTR
) {
901 unsigned int pos
, count
;
904 pos
= nvt
->tx
.cur_buf_num
;
905 count
= nvt
->tx
.buf_count
;
907 /* Write data into the hardware tx fifo while pos < count */
909 nvt_cir_reg_write(nvt
, nvt
->tx
.buf
[pos
], CIR_STXFIFO
);
910 nvt
->tx
.cur_buf_num
++;
911 /* Disable TX FIFO Trigger Level Reach (TTR) interrupt */
913 tmp
= nvt_cir_reg_read(nvt
, CIR_IREN
);
914 nvt_cir_reg_write(nvt
, tmp
& ~CIR_IREN_TTR
, CIR_IREN
);
918 if (status
& CIR_IRSTS_TFU
) {
919 if (nvt
->tx
.tx_state
== ST_TX_REPLY
) {
920 nvt
->tx
.tx_state
= ST_TX_REQUEST
;
921 wake_up(&nvt
->tx
.queue
);
925 spin_unlock(&nvt
->lock
);
927 nvt_dbg_verbose("%s done", __func__
);
931 static void nvt_disable_cir(struct nvt_dev
*nvt
)
935 spin_lock_irqsave(&nvt
->lock
, flags
);
937 /* disable CIR interrupts */
938 nvt_cir_reg_write(nvt
, 0, CIR_IREN
);
940 /* clear any and all pending interrupts */
941 nvt_cir_reg_write(nvt
, 0xff, CIR_IRSTS
);
943 /* clear all function enable flags */
944 nvt_cir_reg_write(nvt
, 0, CIR_IRCON
);
946 /* clear hardware rx and tx fifos */
947 nvt_clear_cir_fifo(nvt
);
948 nvt_clear_tx_fifo(nvt
);
950 spin_unlock_irqrestore(&nvt
->lock
, flags
);
952 /* disable the CIR logical device */
953 nvt_disable_logical_dev(nvt
, LOGICAL_DEV_CIR
);
956 static int nvt_open(struct rc_dev
*dev
)
958 struct nvt_dev
*nvt
= dev
->priv
;
961 spin_lock_irqsave(&nvt
->lock
, flags
);
963 /* set function enable flags */
964 nvt_cir_reg_write(nvt
, CIR_IRCON_TXEN
| CIR_IRCON_RXEN
|
965 CIR_IRCON_RXINV
| CIR_IRCON_SAMPLE_PERIOD_SEL
,
968 /* clear all pending interrupts */
969 nvt_cir_reg_write(nvt
, 0xff, CIR_IRSTS
);
971 /* enable interrupts */
972 nvt_set_cir_iren(nvt
);
974 spin_unlock_irqrestore(&nvt
->lock
, flags
);
976 /* enable the CIR logical device */
977 nvt_enable_logical_dev(nvt
, LOGICAL_DEV_CIR
);
982 static void nvt_close(struct rc_dev
*dev
)
984 struct nvt_dev
*nvt
= dev
->priv
;
986 nvt_disable_cir(nvt
);
989 /* Allocate memory, probe hardware, and initialize everything */
990 static int nvt_probe(struct pnp_dev
*pdev
, const struct pnp_device_id
*dev_id
)
996 nvt
= devm_kzalloc(&pdev
->dev
, sizeof(struct nvt_dev
), GFP_KERNEL
);
1000 /* input device for IR remote (and tx) */
1001 nvt
->rdev
= devm_rc_allocate_device(&pdev
->dev
);
1006 /* activate pnp device */
1007 ret
= pnp_activate_dev(pdev
);
1009 dev_err(&pdev
->dev
, "Could not activate PNP device!\n");
1013 /* validate pnp resources */
1014 if (!pnp_port_valid(pdev
, 0) ||
1015 pnp_port_len(pdev
, 0) < CIR_IOREG_LENGTH
) {
1016 dev_err(&pdev
->dev
, "IR PNP Port not valid!\n");
1020 if (!pnp_irq_valid(pdev
, 0)) {
1021 dev_err(&pdev
->dev
, "PNP IRQ not valid!\n");
1025 if (!pnp_port_valid(pdev
, 1) ||
1026 pnp_port_len(pdev
, 1) < CIR_IOREG_LENGTH
) {
1027 dev_err(&pdev
->dev
, "Wake PNP Port not valid!\n");
1031 nvt
->cir_addr
= pnp_port_start(pdev
, 0);
1032 nvt
->cir_irq
= pnp_irq(pdev
, 0);
1034 nvt
->cir_wake_addr
= pnp_port_start(pdev
, 1);
1036 nvt
->cr_efir
= CR_EFIR
;
1037 nvt
->cr_efdr
= CR_EFDR
;
1039 spin_lock_init(&nvt
->lock
);
1041 pnp_set_drvdata(pdev
, nvt
);
1043 init_waitqueue_head(&nvt
->tx
.queue
);
1045 ret
= nvt_hw_detect(nvt
);
1049 /* Initialize CIR & CIR Wake Logical Devices */
1050 nvt_efm_enable(nvt
);
1051 nvt_cir_ldev_init(nvt
);
1052 nvt_cir_wake_ldev_init(nvt
);
1053 nvt_efm_disable(nvt
);
1056 * Initialize CIR & CIR Wake Config Registers
1057 * and enable logical devices
1059 nvt_cir_regs_init(nvt
);
1060 nvt_cir_wake_regs_init(nvt
);
1062 /* Set up the rc device */
1064 rdev
->driver_type
= RC_DRIVER_IR_RAW
;
1065 rdev
->allowed_protocols
= RC_BIT_ALL
;
1066 rdev
->open
= nvt_open
;
1067 rdev
->close
= nvt_close
;
1068 rdev
->tx_ir
= nvt_tx_ir
;
1069 rdev
->s_tx_carrier
= nvt_set_tx_carrier
;
1070 rdev
->input_name
= "Nuvoton w836x7hg Infrared Remote Transceiver";
1071 rdev
->input_phys
= "nuvoton/cir0";
1072 rdev
->input_id
.bustype
= BUS_HOST
;
1073 rdev
->input_id
.vendor
= PCI_VENDOR_ID_WINBOND2
;
1074 rdev
->input_id
.product
= nvt
->chip_major
;
1075 rdev
->input_id
.version
= nvt
->chip_minor
;
1076 rdev
->driver_name
= NVT_DRIVER_NAME
;
1077 rdev
->map_name
= RC_MAP_RC6_MCE
;
1078 rdev
->timeout
= MS_TO_NS(100);
1079 /* rx resolution is hardwired to 50us atm, 1, 25, 100 also possible */
1080 rdev
->rx_resolution
= US_TO_NS(CIR_SAMPLE_PERIOD
);
1082 rdev
->min_timeout
= XYZ
;
1083 rdev
->max_timeout
= XYZ
;
1085 rdev
->tx_resolution
= XYZ
;
1087 ret
= devm_rc_register_device(&pdev
->dev
, rdev
);
1091 /* now claim resources */
1092 if (!devm_request_region(&pdev
->dev
, nvt
->cir_addr
,
1093 CIR_IOREG_LENGTH
, NVT_DRIVER_NAME
))
1096 ret
= devm_request_irq(&pdev
->dev
, nvt
->cir_irq
, nvt_cir_isr
,
1097 IRQF_SHARED
, NVT_DRIVER_NAME
, nvt
);
1101 if (!devm_request_region(&pdev
->dev
, nvt
->cir_wake_addr
,
1102 CIR_IOREG_LENGTH
, NVT_DRIVER_NAME
"-wake"))
1105 ret
= device_create_file(&rdev
->dev
, &dev_attr_wakeup_data
);
1109 device_init_wakeup(&pdev
->dev
, true);
1111 dev_notice(&pdev
->dev
, "driver has been successfully loaded\n");
1114 cir_wake_dump_regs(nvt
);
1120 static void nvt_remove(struct pnp_dev
*pdev
)
1122 struct nvt_dev
*nvt
= pnp_get_drvdata(pdev
);
1124 device_remove_file(&nvt
->rdev
->dev
, &dev_attr_wakeup_data
);
1126 nvt_disable_cir(nvt
);
1128 /* enable CIR Wake (for IR power-on) */
1129 nvt_enable_wake(nvt
);
1132 static int nvt_suspend(struct pnp_dev
*pdev
, pm_message_t state
)
1134 struct nvt_dev
*nvt
= pnp_get_drvdata(pdev
);
1135 unsigned long flags
;
1137 nvt_dbg("%s called", __func__
);
1139 spin_lock_irqsave(&nvt
->lock
, flags
);
1141 nvt
->tx
.tx_state
= ST_TX_NONE
;
1143 /* disable all CIR interrupts */
1144 nvt_cir_reg_write(nvt
, 0, CIR_IREN
);
1146 spin_unlock_irqrestore(&nvt
->lock
, flags
);
1148 /* disable cir logical dev */
1149 nvt_disable_logical_dev(nvt
, LOGICAL_DEV_CIR
);
1151 /* make sure wake is enabled */
1152 nvt_enable_wake(nvt
);
1157 static int nvt_resume(struct pnp_dev
*pdev
)
1159 struct nvt_dev
*nvt
= pnp_get_drvdata(pdev
);
1161 nvt_dbg("%s called", __func__
);
1163 nvt_cir_regs_init(nvt
);
1164 nvt_cir_wake_regs_init(nvt
);
1169 static void nvt_shutdown(struct pnp_dev
*pdev
)
1171 struct nvt_dev
*nvt
= pnp_get_drvdata(pdev
);
1173 nvt_enable_wake(nvt
);
1176 static const struct pnp_device_id nvt_ids
[] = {
1177 { "WEC0530", 0 }, /* CIR */
1178 { "NTN0530", 0 }, /* CIR for new chip's pnp id*/
1182 static struct pnp_driver nvt_driver
= {
1183 .name
= NVT_DRIVER_NAME
,
1184 .id_table
= nvt_ids
,
1185 .flags
= PNP_DRIVER_RES_DO_NOT_CHANGE
,
1187 .remove
= nvt_remove
,
1188 .suspend
= nvt_suspend
,
1189 .resume
= nvt_resume
,
1190 .shutdown
= nvt_shutdown
,
1193 module_param(debug
, int, S_IRUGO
| S_IWUSR
);
1194 MODULE_PARM_DESC(debug
, "Enable debugging output");
1196 MODULE_DEVICE_TABLE(pnp
, nvt_ids
);
1197 MODULE_DESCRIPTION("Nuvoton W83667HG-A & W83677HG-I CIR driver");
1199 MODULE_AUTHOR("Jarod Wilson <jarod@redhat.com>");
1200 MODULE_LICENSE("GPL");
1202 module_pnp_driver(nvt_driver
);