28 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30 #include <linux/kernel.h>
31 #include <linux/module.h>
35 #include <linux/sched.h>
36 #include <linux/slab.h>
59 u8 tmp = nvt_cr_read(nvt, reg) |
val;
60 nvt_cr_write(nvt, tmp, reg);
66 u8 tmp = nvt_cr_read(nvt, reg) & ~val;
67 nvt_cr_write(nvt, tmp, reg);
71 static inline void nvt_efm_enable(
struct nvt_dev *nvt)
79 static inline void nvt_efm_disable(
struct nvt_dev *nvt)
89 static inline void nvt_select_logical_dev(
struct nvt_dev *nvt,
u8 ldev)
112 static inline void nvt_cir_wake_reg_write(
struct nvt_dev *nvt,
129 static void cir_dump_regs(
struct nvt_dev *nvt)
135 pr_info(
" * CR CIR ACTIVE : 0x%x\n",
137 pr_info(
" * CR CIR BASE ADDR: 0x%x\n",
140 pr_info(
" * CR CIR IRQ NUM: 0x%x\n",
143 nvt_efm_disable(nvt);
165 static void cir_wake_dump_regs(
struct nvt_dev *nvt)
172 pr_info(
"%s: Dump CIR WAKE logical device registers:\n",
174 pr_info(
" * CR CIR WAKE ACTIVE : 0x%x\n",
176 pr_info(
" * CR CIR WAKE BASE ADDR: 0x%x\n",
179 pr_info(
" * CR CIR WAKE IRQ NUM: 0x%x\n",
182 nvt_efm_disable(nvt);
191 pr_info(
" * FIFO CMP DEEP: 0x%x\n",
193 pr_info(
" * FIFO CMP TOL: 0x%x\n",
195 pr_info(
" * FIFO COUNT: 0x%x\n",
205 pr_info(
" * SAMPLE RX FIFO: 0x%x\n",
207 pr_info(
" * WR FIFO DATA: 0x%x\n",
209 pr_info(
" * RD FIFO ONLY: 0x%x\n",
211 pr_info(
" * RD FIFO ONLY IDX: 0x%x\n",
213 pr_info(
" * FIFO IGNORE: 0x%x\n",
221 for (i = 0; i < fifo_len; i++)
228 static int nvt_hw_detect(
struct nvt_dev *nvt)
231 u8 chip_major, chip_minor;
234 bool chip_unknown =
false;
240 if (chip_major == 0xff) {
250 switch (chip_major) {
252 strcpy(chip_id,
"w83667hg\0");
257 strcpy(chip_id,
"w83677hg\0");
263 strcpy(chip_id,
"w83677hg-c\0");
268 strcpy(chip_id,
"w836x7hg\0");
276 "it may not work...", chip_id, chip_major, chip_minor);
278 nvt_dbg(
"%s: chip id: 0x%02x 0x%02x",
279 chip_id, chip_major, chip_minor);
281 nvt_efm_disable(nvt);
286 spin_unlock_irqrestore(&nvt->
nvt_lock, flags);
291 static void nvt_cir_ldev_init(
struct nvt_dev *nvt)
293 u8 val, psreg, psmask, psval;
306 val = nvt_cr_read(nvt, psreg);
309 nvt_cr_write(nvt, val, psreg);
320 nvt_dbg(
"CIR initialized, base io port address: 0x%lx, irq: %d",
324 static void nvt_cir_wake_ldev_init(
struct nvt_dev *nvt)
348 nvt_dbg(
"CIR Wake initialized, base io port address: 0x%lx, irq: %d",
353 static void nvt_clear_cir_fifo(
struct nvt_dev *nvt)
362 static void nvt_clear_cir_wake_fifo(
struct nvt_dev *nvt)
372 static void nvt_clear_tx_fifo(
struct nvt_dev *nvt)
381 static void nvt_set_cir_iren(
struct nvt_dev *nvt)
386 nvt_cir_reg_write(nvt, iren,
CIR_IREN);
389 static void nvt_cir_regs_init(
struct nvt_dev *nvt)
403 nvt_cir_reg_write(nvt,
409 nvt_clear_cir_fifo(nvt);
410 nvt_clear_tx_fifo(nvt);
416 nvt_set_cir_iren(nvt);
419 static void nvt_cir_wake_regs_init(
struct nvt_dev *nvt)
447 nvt_clear_cir_wake_fifo(nvt);
453 static void nvt_enable_wake(
struct nvt_dev *nvt)
465 nvt_efm_disable(nvt);
476 static u32 nvt_rx_carrier_detect(
struct nvt_dev *nvt)
481 count = nvt_cir_reg_read(nvt,
CIR_FCCL) |
482 nvt_cir_reg_read(nvt,
CIR_FCCH) << 8;
484 for (i = 0; i < nvt->
pkts; i++) {
491 if (!count || !duration) {
500 nvt_dbg(
"WTF? Carrier frequency out of range!");
502 nvt_dbg(
"Carrier frequency: %u (count %u, duration %u)",
503 carrier, count, duration);
515 static int nvt_set_tx_carrier(
struct rc_dev *
dev,
u32 carrier)
523 nvt_cir_reg_write(nvt, 1,
CIR_CP);
525 nvt_cir_reg_write(nvt, val & 0xff,
CIR_CC);
528 nvt_cir_reg_read(nvt,
CIR_CP), nvt_cir_reg_read(nvt,
CIR_CC));
551 static int nvt_tx_ir(
struct rc_dev *dev,
unsigned *txbuf,
unsigned n)
562 nvt->
tx.buf_count = (ret *
sizeof(unsigned));
564 memcpy(nvt->
tx.buf, txbuf, nvt->
tx.buf_count);
566 nvt->
tx.cur_buf_num = 0;
569 iren = nvt_cir_reg_read(nvt,
CIR_IREN);
580 for (i = 0; i < 9; i++)
583 spin_unlock_irqrestore(&nvt->
tx.lock, flags);
589 spin_unlock_irqrestore(&nvt->
tx.lock, flags);
592 nvt_cir_reg_write(nvt, iren,
CIR_IREN);
598 static void nvt_dump_rx_buf(
struct nvt_dev *nvt)
620 static void nvt_process_rx_ir_data(
struct nvt_dev *nvt)
630 nvt_dump_rx_buf(nvt);
633 carrier = nvt_rx_carrier_detect(nvt);
637 init_ir_raw_event(&rawir);
639 for (i = 0; i < nvt->
pkts; i++) {
640 sample = nvt->
buf[
i];
646 nvt_dbg(
"Storing %s with duration %d",
647 rawir.pulse ?
"pulse" :
"space", rawir.duration);
657 nvt_dbg(
"Calling ir_raw_event_handle (signal end)\n");
664 nvt_dbg(
"Calling ir_raw_event_handle (buffer empty)\n");
670 static void nvt_handle_rx_fifo_overrun(
struct nvt_dev *nvt)
675 nvt_clear_cir_fifo(nvt);
676 ir_raw_event_reset(nvt->
rdev);
680 static void nvt_get_rx_ir_data(
struct nvt_dev *nvt)
691 if (fifocount == 0xff)
699 nvt_dbg(
"attempting to fetch %u bytes from hw rx fifo", fifocount);
707 nvt_process_rx_ir_data(nvt);
712 for (i = 0; i < fifocount; i++) {
717 nvt->
pkts += fifocount;
720 nvt_process_rx_ir_data(nvt);
723 nvt_handle_rx_fifo_overrun(nvt);
725 spin_unlock_irqrestore(&nvt->
nvt_lock, flags);
728 static void nvt_cir_log_irqs(
u8 status,
u8 iren)
740 status & ~(CIR_IRSTS_RDR | CIR_IRSTS_RTR | CIR_IRSTS_PE |
741 CIR_IRSTS_RFO | CIR_IRSTS_TE | CIR_IRSTS_TTR |
742 CIR_IRSTS_TFU | CIR_IRSTS_GH) ?
" ?" :
"");
745 static bool nvt_cir_tx_inactive(
struct nvt_dev *nvt)
752 tx_state = nvt->
tx.tx_state;
753 spin_unlock_irqrestore(&nvt->
tx.lock, flags);
771 nvt_efm_disable(nvt);
786 status = nvt_cir_reg_read(nvt,
CIR_IRSTS);
794 nvt_cir_reg_write(nvt, status,
CIR_IRSTS);
798 iren = nvt_cir_reg_read(nvt,
CIR_IREN);
805 nvt_cir_log_irqs(status, iren);
810 if (nvt_cir_tx_inactive(nvt))
811 nvt_get_rx_ir_data(nvt);
815 if (nvt_cir_tx_inactive(nvt))
816 nvt_get_rx_ir_data(nvt);
822 spin_unlock_irqrestore(&nvt->
nvt_lock, flags);
825 nvt_clear_cir_fifo(nvt);
829 nvt_clear_tx_fifo(nvt);
837 pos = nvt->
tx.cur_buf_num;
838 count = nvt->
tx.buf_count;
843 nvt->
tx.cur_buf_num++;
846 tmp = nvt_cir_reg_read(nvt,
CIR_IREN);
850 spin_unlock_irqrestore(&nvt->
tx.lock, flags);
860 spin_unlock_irqrestore(&nvt->
tx.lock, flags);
868 static irqreturn_t nvt_cir_wake_isr(
int irq,
void *data)
881 nvt_clear_cir_wake_fifo(nvt);
897 nvt_dbg(
"setting wake up key: 0x%x", val);
903 spin_unlock_irqrestore(&nvt->
nvt_lock, flags);
910 static void nvt_enable_cir(
struct nvt_dev *nvt)
923 nvt_efm_disable(nvt);
929 nvt_set_cir_iren(nvt);
932 static void nvt_disable_cir(
struct nvt_dev *nvt)
935 nvt_cir_reg_write(nvt, 0,
CIR_IREN);
944 nvt_clear_cir_fifo(nvt);
945 nvt_clear_tx_fifo(nvt);
953 nvt_efm_disable(nvt);
956 static int nvt_open(
struct rc_dev *dev)
963 spin_unlock_irqrestore(&nvt->
nvt_lock, flags);
968 static void nvt_close(
struct rc_dev *dev)
974 nvt_disable_cir(nvt);
975 spin_unlock_irqrestore(&nvt->
nvt_lock, flags);
996 if (!pnp_port_valid(pdev, 0) ||
998 dev_err(&pdev->
dev,
"IR PNP Port not valid!\n");
1002 if (!pnp_irq_valid(pdev, 0)) {
1007 if (!pnp_port_valid(pdev, 1) ||
1009 dev_err(&pdev->
dev,
"Wake PNP Port not valid!\n");
1013 nvt->
cir_addr = pnp_port_start(pdev, 0);
1026 pnp_set_drvdata(pdev, nvt);
1031 ret = nvt_hw_detect(nvt);
1036 nvt_efm_enable(nvt);
1037 nvt_cir_ldev_init(nvt);
1038 nvt_cir_wake_ldev_init(nvt);
1039 nvt_efm_disable(nvt);
1042 nvt_cir_regs_init(nvt);
1043 nvt_cir_wake_regs_init(nvt);
1049 rdev->
open = nvt_open;
1050 rdev->
close = nvt_close;
1051 rdev->
tx_ir = nvt_tx_ir;
1053 rdev->
input_name =
"Nuvoton w836x7hg Infrared Remote Transceiver";
1059 rdev->
dev.parent = &pdev->
dev;
1099 cir_wake_dump_regs(nvt);
1121 struct nvt_dev *nvt = pnp_get_drvdata(pdev);
1122 unsigned long flags;
1126 nvt_cir_reg_write(nvt, 0,
CIR_IREN);
1127 nvt_disable_cir(nvt);
1129 nvt_enable_wake(nvt);
1130 spin_unlock_irqrestore(&nvt->
nvt_lock, flags);
1145 struct nvt_dev *nvt = pnp_get_drvdata(pdev);
1146 unsigned long flags;
1148 nvt_dbg(
"%s called", __func__);
1154 spin_unlock_irqrestore(&nvt->
nvt_lock, flags);
1158 spin_unlock_irqrestore(&nvt->
tx.lock, flags);
1161 nvt_cir_reg_write(nvt, 0,
CIR_IREN);
1163 nvt_efm_enable(nvt);
1169 nvt_efm_disable(nvt);
1172 nvt_enable_wake(nvt);
1177 static int nvt_resume(
struct pnp_dev *pdev)
1180 struct nvt_dev *nvt = pnp_get_drvdata(pdev);
1182 nvt_dbg(
"%s called", __func__);
1185 nvt_set_cir_iren(nvt);
1188 nvt_efm_enable(nvt);
1192 nvt_efm_disable(nvt);
1194 nvt_cir_regs_init(nvt);
1195 nvt_cir_wake_regs_init(nvt);
1200 static void nvt_shutdown(
struct pnp_dev *pdev)
1202 struct nvt_dev *nvt = pnp_get_drvdata(pdev);
1203 nvt_enable_wake(nvt);
1214 .id_table = nvt_ids,
1218 .suspend = nvt_suspend,
1219 .resume = nvt_resume,
1220 .shutdown = nvt_shutdown,