Jarod Wilson | 6d2f5c2 | 2010-10-07 17:50:34 -0300 | [diff] [blame] | 1 | /* |
| 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 Wilson | 4e6e29a | 2010-10-15 11:07:37 -0300 | [diff] [blame^] | 29 | #include <linux/ioctl.h> |
Jarod Wilson | 6d2f5c2 | 2010-10-07 17:50:34 -0300 | [diff] [blame] | 30 | |
| 31 | /* platform driver name to register */ |
| 32 | #define NVT_DRIVER_NAME "nuvoton-cir" |
| 33 | |
| 34 | /* debugging module parameter */ |
| 35 | static 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 | |
| 67 | struct nvt_dev { |
| 68 | struct pnp_dev *pdev; |
| 69 | struct input_dev *rdev; |
| 70 | struct ir_dev_props *props; |
| 71 | struct ir_raw_event rawir; |
| 72 | |
| 73 | spinlock_t nvt_lock; |
| 74 | bool in_use; |
| 75 | |
| 76 | /* for rx */ |
| 77 | u8 buf[RX_BUF_LEN]; |
| 78 | unsigned int pkts; |
| 79 | |
| 80 | struct { |
| 81 | spinlock_t lock; |
| 82 | u8 buf[TX_BUF_LEN]; |
| 83 | unsigned int buf_count; |
| 84 | unsigned int cur_buf_num; |
| 85 | wait_queue_head_t queue; |
| 86 | u8 tx_state; |
| 87 | } tx; |
| 88 | |
| 89 | /* EFER Config register index/data pair */ |
| 90 | u8 cr_efir; |
| 91 | u8 cr_efdr; |
| 92 | |
| 93 | /* hardware I/O settings */ |
| 94 | unsigned long cir_addr; |
| 95 | unsigned long cir_wake_addr; |
| 96 | int cir_irq; |
| 97 | int cir_wake_irq; |
| 98 | |
| 99 | /* hardware id */ |
| 100 | u8 chip_major; |
| 101 | u8 chip_minor; |
| 102 | |
| 103 | /* hardware features */ |
| 104 | bool hw_learning_capable; |
| 105 | bool hw_tx_capable; |
| 106 | |
| 107 | /* rx settings */ |
| 108 | bool learning_enabled; |
| 109 | bool carrier_detect_enabled; |
| 110 | |
| 111 | /* track cir wake state */ |
| 112 | u8 wake_state; |
| 113 | /* for study */ |
| 114 | u8 study_state; |
| 115 | /* carrier period = 1 / frequency */ |
| 116 | u32 carrier; |
| 117 | }; |
| 118 | |
| 119 | /* study states */ |
| 120 | #define ST_STUDY_NONE 0x0 |
| 121 | #define ST_STUDY_START 0x1 |
| 122 | #define ST_STUDY_CARRIER 0x2 |
| 123 | #define ST_STUDY_ALL_RECV 0x4 |
| 124 | |
| 125 | /* wake states */ |
| 126 | #define ST_WAKE_NONE 0x0 |
| 127 | #define ST_WAKE_START 0x1 |
| 128 | #define ST_WAKE_FINISH 0x2 |
| 129 | |
| 130 | /* receive states */ |
| 131 | #define ST_RX_WAIT_7F 0x1 |
| 132 | #define ST_RX_WAIT_HEAD 0x2 |
| 133 | #define ST_RX_WAIT_SILENT_END 0x4 |
| 134 | |
| 135 | /* send states */ |
| 136 | #define ST_TX_NONE 0x0 |
| 137 | #define ST_TX_REQUEST 0x2 |
| 138 | #define ST_TX_REPLY 0x4 |
| 139 | |
| 140 | /* buffer packet constants */ |
| 141 | #define BUF_PULSE_BIT 0x80 |
| 142 | #define BUF_LEN_MASK 0x7f |
| 143 | #define BUF_REPEAT_BYTE 0x70 |
| 144 | #define BUF_REPEAT_MASK 0xf0 |
| 145 | |
| 146 | /* CIR settings */ |
| 147 | |
| 148 | /* total length of CIR and CIR WAKE */ |
| 149 | #define CIR_IOREG_LENGTH 0x0f |
| 150 | |
| 151 | /* RX limit length, 8 high bits for SLCH, 8 low bits for SLCL (0x7d0 = 2000) */ |
| 152 | #define CIR_RX_LIMIT_COUNT 0x7d0 |
| 153 | |
| 154 | /* CIR Regs */ |
| 155 | #define CIR_IRCON 0x00 |
| 156 | #define CIR_IRSTS 0x01 |
| 157 | #define CIR_IREN 0x02 |
| 158 | #define CIR_RXFCONT 0x03 |
| 159 | #define CIR_CP 0x04 |
| 160 | #define CIR_CC 0x05 |
| 161 | #define CIR_SLCH 0x06 |
| 162 | #define CIR_SLCL 0x07 |
| 163 | #define CIR_FIFOCON 0x08 |
| 164 | #define CIR_IRFIFOSTS 0x09 |
| 165 | #define CIR_SRXFIFO 0x0a |
| 166 | #define CIR_TXFCONT 0x0b |
| 167 | #define CIR_STXFIFO 0x0c |
| 168 | #define CIR_FCCH 0x0d |
| 169 | #define CIR_FCCL 0x0e |
| 170 | #define CIR_IRFSM 0x0f |
| 171 | |
| 172 | /* CIR IRCON settings */ |
| 173 | #define CIR_IRCON_RECV 0x80 |
| 174 | #define CIR_IRCON_WIREN 0x40 |
| 175 | #define CIR_IRCON_TXEN 0x20 |
| 176 | #define CIR_IRCON_RXEN 0x10 |
| 177 | #define CIR_IRCON_WRXINV 0x08 |
| 178 | #define CIR_IRCON_RXINV 0x04 |
| 179 | |
| 180 | #define CIR_IRCON_SAMPLE_PERIOD_SEL_1 0x00 |
| 181 | #define CIR_IRCON_SAMPLE_PERIOD_SEL_25 0x01 |
| 182 | #define CIR_IRCON_SAMPLE_PERIOD_SEL_50 0x02 |
| 183 | #define CIR_IRCON_SAMPLE_PERIOD_SEL_100 0x03 |
| 184 | |
| 185 | /* FIXME: make this a runtime option */ |
| 186 | /* select sample period as 50us */ |
| 187 | #define CIR_IRCON_SAMPLE_PERIOD_SEL CIR_IRCON_SAMPLE_PERIOD_SEL_50 |
| 188 | |
| 189 | /* CIR IRSTS settings */ |
| 190 | #define CIR_IRSTS_RDR 0x80 |
| 191 | #define CIR_IRSTS_RTR 0x40 |
| 192 | #define CIR_IRSTS_PE 0x20 |
| 193 | #define CIR_IRSTS_RFO 0x10 |
| 194 | #define CIR_IRSTS_TE 0x08 |
| 195 | #define CIR_IRSTS_TTR 0x04 |
| 196 | #define CIR_IRSTS_TFU 0x02 |
| 197 | #define CIR_IRSTS_GH 0x01 |
| 198 | |
| 199 | /* CIR IREN settings */ |
| 200 | #define CIR_IREN_RDR 0x80 |
| 201 | #define CIR_IREN_RTR 0x40 |
| 202 | #define CIR_IREN_PE 0x20 |
| 203 | #define CIR_IREN_RFO 0x10 |
| 204 | #define CIR_IREN_TE 0x08 |
| 205 | #define CIR_IREN_TTR 0x04 |
| 206 | #define CIR_IREN_TFU 0x02 |
| 207 | #define CIR_IREN_GH 0x01 |
| 208 | |
| 209 | /* CIR FIFOCON settings */ |
| 210 | #define CIR_FIFOCON_TXFIFOCLR 0x80 |
| 211 | |
| 212 | #define CIR_FIFOCON_TX_TRIGGER_LEV_31 0x00 |
| 213 | #define CIR_FIFOCON_TX_TRIGGER_LEV_24 0x10 |
| 214 | #define CIR_FIFOCON_TX_TRIGGER_LEV_16 0x20 |
| 215 | #define CIR_FIFOCON_TX_TRIGGER_LEV_8 0x30 |
| 216 | |
| 217 | /* FIXME: make this a runtime option */ |
| 218 | /* select TX trigger level as 16 */ |
| 219 | #define CIR_FIFOCON_TX_TRIGGER_LEV CIR_FIFOCON_TX_TRIGGER_LEV_16 |
| 220 | |
| 221 | #define CIR_FIFOCON_RXFIFOCLR 0x08 |
| 222 | |
| 223 | #define CIR_FIFOCON_RX_TRIGGER_LEV_1 0x00 |
| 224 | #define CIR_FIFOCON_RX_TRIGGER_LEV_8 0x01 |
| 225 | #define CIR_FIFOCON_RX_TRIGGER_LEV_16 0x02 |
| 226 | #define CIR_FIFOCON_RX_TRIGGER_LEV_24 0x03 |
| 227 | |
| 228 | /* FIXME: make this a runtime option */ |
| 229 | /* select RX trigger level as 24 */ |
| 230 | #define CIR_FIFOCON_RX_TRIGGER_LEV CIR_FIFOCON_RX_TRIGGER_LEV_24 |
| 231 | |
| 232 | /* CIR IRFIFOSTS settings */ |
| 233 | #define CIR_IRFIFOSTS_IR_PENDING 0x80 |
| 234 | #define CIR_IRFIFOSTS_RX_GS 0x40 |
| 235 | #define CIR_IRFIFOSTS_RX_FTA 0x20 |
| 236 | #define CIR_IRFIFOSTS_RX_EMPTY 0x10 |
| 237 | #define CIR_IRFIFOSTS_RX_FULL 0x08 |
| 238 | #define CIR_IRFIFOSTS_TX_FTA 0x04 |
| 239 | #define CIR_IRFIFOSTS_TX_EMPTY 0x02 |
| 240 | #define CIR_IRFIFOSTS_TX_FULL 0x01 |
| 241 | |
| 242 | |
| 243 | /* CIR WAKE UP Regs */ |
| 244 | #define CIR_WAKE_IRCON 0x00 |
| 245 | #define CIR_WAKE_IRSTS 0x01 |
| 246 | #define CIR_WAKE_IREN 0x02 |
| 247 | #define CIR_WAKE_FIFO_CMP_DEEP 0x03 |
| 248 | #define CIR_WAKE_FIFO_CMP_TOL 0x04 |
| 249 | #define CIR_WAKE_FIFO_COUNT 0x05 |
| 250 | #define CIR_WAKE_SLCH 0x06 |
| 251 | #define CIR_WAKE_SLCL 0x07 |
| 252 | #define CIR_WAKE_FIFOCON 0x08 |
| 253 | #define CIR_WAKE_SRXFSTS 0x09 |
| 254 | #define CIR_WAKE_SAMPLE_RX_FIFO 0x0a |
| 255 | #define CIR_WAKE_WR_FIFO_DATA 0x0b |
| 256 | #define CIR_WAKE_RD_FIFO_ONLY 0x0c |
| 257 | #define CIR_WAKE_RD_FIFO_ONLY_IDX 0x0d |
| 258 | #define CIR_WAKE_FIFO_IGNORE 0x0e |
| 259 | #define CIR_WAKE_IRFSM 0x0f |
| 260 | |
| 261 | /* CIR WAKE UP IRCON settings */ |
| 262 | #define CIR_WAKE_IRCON_DEC_RST 0x80 |
| 263 | #define CIR_WAKE_IRCON_MODE1 0x40 |
| 264 | #define CIR_WAKE_IRCON_MODE0 0x20 |
| 265 | #define CIR_WAKE_IRCON_RXEN 0x10 |
| 266 | #define CIR_WAKE_IRCON_R 0x08 |
| 267 | #define CIR_WAKE_IRCON_RXINV 0x04 |
| 268 | |
| 269 | /* FIXME/jarod: make this a runtime option */ |
| 270 | /* select a same sample period like cir register */ |
| 271 | #define CIR_WAKE_IRCON_SAMPLE_PERIOD_SEL CIR_IRCON_SAMPLE_PERIOD_SEL_50 |
| 272 | |
| 273 | /* CIR WAKE IRSTS Bits */ |
| 274 | #define CIR_WAKE_IRSTS_RDR 0x80 |
| 275 | #define CIR_WAKE_IRSTS_RTR 0x40 |
| 276 | #define CIR_WAKE_IRSTS_PE 0x20 |
| 277 | #define CIR_WAKE_IRSTS_RFO 0x10 |
| 278 | #define CIR_WAKE_IRSTS_GH 0x08 |
| 279 | #define CIR_WAKE_IRSTS_IR_PENDING 0x01 |
| 280 | |
| 281 | /* CIR WAKE UP IREN Bits */ |
| 282 | #define CIR_WAKE_IREN_RDR 0x80 |
| 283 | #define CIR_WAKE_IREN_RTR 0x40 |
| 284 | #define CIR_WAKE_IREN_PE 0x20 |
| 285 | #define CIR_WAKE_IREN_RFO 0x10 |
| 286 | #define CIR_WAKE_IREN_TE 0x08 |
| 287 | #define CIR_WAKE_IREN_TTR 0x04 |
| 288 | #define CIR_WAKE_IREN_TFU 0x02 |
| 289 | #define CIR_WAKE_IREN_GH 0x01 |
| 290 | |
| 291 | /* CIR WAKE FIFOCON settings */ |
| 292 | #define CIR_WAKE_FIFOCON_RXFIFOCLR 0x08 |
| 293 | |
| 294 | #define CIR_WAKE_FIFOCON_RX_TRIGGER_LEV_67 0x00 |
| 295 | #define CIR_WAKE_FIFOCON_RX_TRIGGER_LEV_66 0x01 |
| 296 | #define CIR_WAKE_FIFOCON_RX_TRIGGER_LEV_65 0x02 |
| 297 | #define CIR_WAKE_FIFOCON_RX_TRIGGER_LEV_64 0x03 |
| 298 | |
| 299 | /* FIXME: make this a runtime option */ |
| 300 | /* select WAKE UP RX trigger level as 67 */ |
| 301 | #define CIR_WAKE_FIFOCON_RX_TRIGGER_LEV CIR_WAKE_FIFOCON_RX_TRIGGER_LEV_67 |
| 302 | |
| 303 | /* CIR WAKE SRXFSTS settings */ |
| 304 | #define CIR_WAKE_IRFIFOSTS_RX_GS 0x80 |
| 305 | #define CIR_WAKE_IRFIFOSTS_RX_FTA 0x40 |
| 306 | #define CIR_WAKE_IRFIFOSTS_RX_EMPTY 0x20 |
| 307 | #define CIR_WAKE_IRFIFOSTS_RX_FULL 0x10 |
| 308 | |
| 309 | /* CIR Wake FIFO buffer is 67 bytes long */ |
| 310 | #define CIR_WAKE_FIFO_LEN 67 |
| 311 | /* CIR Wake byte comparison tolerance */ |
| 312 | #define CIR_WAKE_CMP_TOLERANCE 5 |
| 313 | |
| 314 | /* |
| 315 | * Extended Function Enable Registers: |
| 316 | * Extended Function Index Register |
| 317 | * Extended Function Data Register |
| 318 | */ |
| 319 | #define CR_EFIR 0x2e |
| 320 | #define CR_EFDR 0x2f |
| 321 | |
| 322 | /* Possible alternate EFER values, depends on how the chip is wired */ |
| 323 | #define CR_EFIR2 0x4e |
| 324 | #define CR_EFDR2 0x4f |
| 325 | |
| 326 | /* Extended Function Mode enable/disable magic values */ |
| 327 | #define EFER_EFM_ENABLE 0x87 |
| 328 | #define EFER_EFM_DISABLE 0xaa |
| 329 | |
| 330 | /* Chip IDs found in CR_CHIP_ID_{HI,LO} */ |
| 331 | #define CHIP_ID_HIGH 0xb4 |
| 332 | #define CHIP_ID_LOW 0x72 |
| 333 | #define CHIP_ID_LOW2 0x73 |
| 334 | |
| 335 | /* Config regs we need to care about */ |
| 336 | #define CR_SOFTWARE_RESET 0x02 |
| 337 | #define CR_LOGICAL_DEV_SEL 0x07 |
| 338 | #define CR_CHIP_ID_HI 0x20 |
| 339 | #define CR_CHIP_ID_LO 0x21 |
| 340 | #define CR_DEV_POWER_DOWN 0x22 /* bit 2 is CIR power, default power on */ |
| 341 | #define CR_OUTPUT_PIN_SEL 0x27 |
| 342 | #define CR_LOGICAL_DEV_EN 0x30 /* valid for all logical devices */ |
| 343 | /* next three regs valid for both the CIR and CIR_WAKE logical devices */ |
| 344 | #define CR_CIR_BASE_ADDR_HI 0x60 |
| 345 | #define CR_CIR_BASE_ADDR_LO 0x61 |
| 346 | #define CR_CIR_IRQ_RSRC 0x70 |
| 347 | /* next three regs valid only for ACPI logical dev */ |
| 348 | #define CR_ACPI_CIR_WAKE 0xe0 |
| 349 | #define CR_ACPI_IRQ_EVENTS 0xf6 |
| 350 | #define CR_ACPI_IRQ_EVENTS2 0xf7 |
| 351 | |
| 352 | /* Logical devices that we need to care about */ |
| 353 | #define LOGICAL_DEV_LPT 0x01 |
| 354 | #define LOGICAL_DEV_CIR 0x06 |
| 355 | #define LOGICAL_DEV_ACPI 0x0a |
| 356 | #define LOGICAL_DEV_CIR_WAKE 0x0e |
| 357 | |
| 358 | #define LOGICAL_DEV_DISABLE 0x00 |
| 359 | #define LOGICAL_DEV_ENABLE 0x01 |
| 360 | |
| 361 | #define CIR_WAKE_ENABLE_BIT 0x08 |
| 362 | #define CIR_INTR_MOUSE_IRQ_BIT 0x80 |
| 363 | #define PME_INTR_CIR_PASS_BIT 0x08 |
| 364 | |
| 365 | #define OUTPUT_PIN_SEL_MASK 0xbc |
| 366 | #define OUTPUT_ENABLE_CIR 0x01 /* Pin95=CIRRX, Pin96=CIRTX1 */ |
| 367 | #define OUTPUT_ENABLE_CIRWB 0x40 /* enable wide-band sensor */ |
| 368 | |
| 369 | /* MCE CIR signal length, related on sample period */ |
| 370 | |
| 371 | /* MCE CIR controller signal length: about 43ms |
| 372 | * 43ms / 50us (sample period) * 0.85 (inaccuracy) |
| 373 | */ |
| 374 | #define CONTROLLER_BUF_LEN_MIN 830 |
| 375 | |
| 376 | /* MCE CIR keyboard signal length: about 26ms |
| 377 | * 26ms / 50us (sample period) * 0.85 (inaccuracy) |
| 378 | */ |
| 379 | #define KEYBOARD_BUF_LEN_MAX 650 |
| 380 | #define KEYBOARD_BUF_LEN_MIN 610 |
| 381 | |
| 382 | /* MCE CIR mouse signal length: about 24ms |
| 383 | * 24ms / 50us (sample period) * 0.85 (inaccuracy) |
| 384 | */ |
| 385 | #define MOUSE_BUF_LEN_MIN 565 |
| 386 | |
| 387 | #define CIR_SAMPLE_PERIOD 50 |
| 388 | #define CIR_SAMPLE_LOW_INACCURACY 0.85 |
| 389 | |
| 390 | /* MAX silence time that driver will sent to lirc */ |
| 391 | #define MAX_SILENCE_TIME 60000 |
| 392 | |
| 393 | #if CIR_IRCON_SAMPLE_PERIOD_SEL == CIR_IRCON_SAMPLE_PERIOD_SEL_100 |
| 394 | #define SAMPLE_PERIOD 100 |
| 395 | |
| 396 | #elif CIR_IRCON_SAMPLE_PERIOD_SEL == CIR_IRCON_SAMPLE_PERIOD_SEL_50 |
| 397 | #define SAMPLE_PERIOD 50 |
| 398 | |
| 399 | #elif CIR_IRCON_SAMPLE_PERIOD_SEL == CIR_IRCON_SAMPLE_PERIOD_SEL_25 |
| 400 | #define SAMPLE_PERIOD 25 |
| 401 | |
| 402 | #else |
| 403 | #define SAMPLE_PERIOD 1 |
| 404 | #endif |
| 405 | |
| 406 | /* as VISTA MCE definition, valid carrier value */ |
| 407 | #define MAX_CARRIER 60000 |
| 408 | #define MIN_CARRIER 30000 |