/*1* Driver for Feature Integration Technology Inc. (aka Fintek) LPC CIR2*3* Copyright (C) 2011 Jarod Wilson <[email protected]>4*5* Special thanks to Fintek for providing hardware and spec sheets.6* This driver is based upon the nuvoton, ite and ene drivers for7* similar hardware.8*9* This program is free software; you can redistribute it and/or10* modify it under the terms of the GNU General Public License as11* published by the Free Software Foundation; either version 2 of the12* License, or (at your option) any later version.13*14* This program is distributed in the hope that it will be useful, but15* WITHOUT ANY WARRANTY; without even the implied warranty of16* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU17* General Public License for more details.18*19* You should have received a copy of the GNU General Public License20* along with this program; if not, write to the Free Software21* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-130722* USA23*/2425#include <linux/spinlock.h>26#include <linux/ioctl.h>2728/* platform driver name to register */29#define FINTEK_DRIVER_NAME "fintek-cir"30#define FINTEK_DESCRIPTION "Fintek LPC SuperIO Consumer IR Transceiver"31#define VENDOR_ID_FINTEK 0x1934323334/* debugging module parameter */35static int debug;3637#define fit_pr(level, text, ...) \38printk(level KBUILD_MODNAME ": " text, ## __VA_ARGS__)3940#define fit_dbg(text, ...) \41if (debug) \42printk(KERN_DEBUG \43KBUILD_MODNAME ": " text "\n" , ## __VA_ARGS__)4445#define fit_dbg_verbose(text, ...) \46if (debug > 1) \47printk(KERN_DEBUG \48KBUILD_MODNAME ": " text "\n" , ## __VA_ARGS__)4950#define fit_dbg_wake(text, ...) \51if (debug > 2) \52printk(KERN_DEBUG \53KBUILD_MODNAME ": " text "\n" , ## __VA_ARGS__)545556#define TX_BUF_LEN 25657#define RX_BUF_LEN 325859struct fintek_dev {60struct pnp_dev *pdev;61struct rc_dev *rdev;6263spinlock_t fintek_lock;6465/* for rx */66u8 buf[RX_BUF_LEN];67unsigned int pkts;6869struct {70spinlock_t lock;71u8 buf[TX_BUF_LEN];72unsigned int buf_count;73unsigned int cur_buf_num;74wait_queue_head_t queue;75} tx;7677/* Config register index/data port pair */78u8 cr_ip;79u8 cr_dp;8081/* hardware I/O settings */82unsigned long cir_addr;83int cir_irq;84int cir_port_len;8586/* hardware id */87u8 chip_major;88u8 chip_minor;89u16 chip_vendor;9091/* hardware features */92bool hw_learning_capable;93bool hw_tx_capable;9495/* rx settings */96bool learning_enabled;97bool carrier_detect_enabled;9899enum {100CMD_HEADER = 0,101SUBCMD,102CMD_DATA,103PARSE_IRDATA,104} parser_state;105106u8 cmd, rem;107108/* carrier period = 1 / frequency */109u32 carrier;110};111112/* buffer packet constants, largely identical to mceusb.c */113#define BUF_PULSE_BIT 0x80114#define BUF_LEN_MASK 0x1f115#define BUF_SAMPLE_MASK 0x7f116117#define BUF_COMMAND_HEADER 0x9f118#define BUF_COMMAND_MASK 0xe0119#define BUF_COMMAND_NULL 0x00120#define BUF_HW_CMD_HEADER 0xff121#define BUF_CMD_G_REVISION 0x0b122#define BUF_CMD_S_CARRIER 0x06123#define BUF_CMD_S_TIMEOUT 0x0c124#define BUF_CMD_SIG_END 0x01125#define BUF_CMD_S_TXMASK 0x08126#define BUF_CMD_S_RXSENSOR 0x14127#define BUF_RSP_PULSE_COUNT 0x15128129#define CIR_SAMPLE_PERIOD 50130131/*132* Configuration Register:133* Index Port134* Data Port135*/136#define CR_INDEX_PORT 0x2e137#define CR_DATA_PORT 0x2f138139/* Possible alternate values, depends on how the chip is wired */140#define CR_INDEX_PORT2 0x4e141#define CR_DATA_PORT2 0x4f142143/*144* GCR_CONFIG_PORT_SEL bit 4 specifies which Index Port value is145* active. 1 = 0x4e, 0 = 0x2e146*/147#define PORT_SEL_PORT_4E_EN 0x10148149/* Extended Function Mode enable/disable magic values */150#define CONFIG_REG_ENABLE 0x87151#define CONFIG_REG_DISABLE 0xaa152153/* Chip IDs found in CR_CHIP_ID_{HI,LO} */154#define CHIP_ID_HIGH_F71809U 0x04155#define CHIP_ID_LOW_F71809U 0x08156157/*158* Global control regs we need to care about:159* Global Control def.160* Register name addr val. */161#define GCR_SOFTWARE_RESET 0x02 /* 0x00 */162#define GCR_LOGICAL_DEV_NO 0x07 /* 0x00 */163#define GCR_CHIP_ID_HI 0x20 /* 0x04 */164#define GCR_CHIP_ID_LO 0x21 /* 0x08 */165#define GCR_VENDOR_ID_HI 0x23 /* 0x19 */166#define GCR_VENDOR_ID_LO 0x24 /* 0x34 */167#define GCR_CONFIG_PORT_SEL 0x25 /* 0x01 */168#define GCR_KBMOUSE_WAKEUP 0x27169170#define LOGICAL_DEV_DISABLE 0x00171#define LOGICAL_DEV_ENABLE 0x01172173/* Logical device number of the CIR function */174#define LOGICAL_DEV_CIR 0x05175176/* CIR Logical Device (LDN 0x08) config registers */177#define CIR_CR_COMMAND_INDEX 0x04178#define CIR_CR_IRCS 0x05 /* Before host writes command to IR, host179must set to 1. When host finshes write180command to IR, host must clear to 0. */181#define CIR_CR_COMMAND_DATA 0x06 /* Host read or write comand data */182#define CIR_CR_CLASS 0x07 /* 0xff = rx-only, 0x66 = rx + 2 tx,1830x33 = rx + 1 tx */184#define CIR_CR_DEV_EN 0x30 /* bit0 = 1 enables CIR */185#define CIR_CR_BASE_ADDR_HI 0x60 /* MSB of CIR IO base addr */186#define CIR_CR_BASE_ADDR_LO 0x61 /* LSB of CIR IO base addr */187#define CIR_CR_IRQ_SEL 0x70 /* bits3-0 store CIR IRQ */188#define CIR_CR_PSOUT_STATUS 0xf1189#define CIR_CR_WAKE_KEY3_ADDR 0xf8190#define CIR_CR_WAKE_KEY3_CODE 0xf9191#define CIR_CR_WAKE_KEY3_DC 0xfa192#define CIR_CR_WAKE_CONTROL 0xfb193#define CIR_CR_WAKE_KEY12_ADDR 0xfc194#define CIR_CR_WAKE_KEY4_ADDR 0xfd195#define CIR_CR_WAKE_KEY5_ADDR 0xfe196197#define CLASS_RX_ONLY 0xff198#define CLASS_RX_2TX 0x66199#define CLASS_RX_1TX 0x33200201/* CIR device registers */202#define CIR_STATUS 0x00203#define CIR_RX_DATA 0x01204#define CIR_TX_CONTROL 0x02205#define CIR_TX_DATA 0x03206#define CIR_CONTROL 0x04207208/* Bits to enable CIR wake */209#define LOGICAL_DEV_ACPI 0x01210#define LDEV_ACPI_WAKE_EN_REG 0xe8211#define ACPI_WAKE_EN_CIR_BIT 0x04212213#define LDEV_ACPI_PME_EN_REG 0xf0214#define LDEV_ACPI_PME_CLR_REG 0xf1215#define ACPI_PME_CIR_BIT 0x02216217#define LDEV_ACPI_STATE_REG 0xf4218#define ACPI_STATE_CIR_BIT 0x20219220/*221* CIR status register (0x00):222* 7 - CIR_IRQ_EN (1 = enable CIR IRQ, 0 = disable)223* 3 - TX_FINISH (1 when TX finished, write 1 to clear)224* 2 - TX_UNDERRUN (1 on TX underrun, write 1 to clear)225* 1 - RX_TIMEOUT (1 on RX timeout, write 1 to clear)226* 0 - RX_RECEIVE (1 on RX receive, write 1 to clear)227*/228#define CIR_STATUS_IRQ_EN 0x80229#define CIR_STATUS_TX_FINISH 0x08230#define CIR_STATUS_TX_UNDERRUN 0x04231#define CIR_STATUS_RX_TIMEOUT 0x02232#define CIR_STATUS_RX_RECEIVE 0x01233#define CIR_STATUS_IRQ_MASK 0x0f234235/*236* CIR TX control register (0x02):237* 7 - TX_START (1 to indicate TX start, auto-cleared when done)238* 6 - TX_END (1 to indicate TX data written to TX fifo)239*/240#define CIR_TX_CONTROL_TX_START 0x80241#define CIR_TX_CONTROL_TX_END 0x40242243244245