nuvoton-cir.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. #include <linux/spinlock.h>
  28. #include <linux/ioctl.h>
  29. /* platform driver name to register */
  30. #define NVT_DRIVER_NAME "nuvoton-cir"
  31. /* debugging module parameter */
  32. static int debug;
  33. #define nvt_pr(level, text, ...) \
  34. printk(level KBUILD_MODNAME ": " text, ## __VA_ARGS__)
  35. #define nvt_dbg(text, ...) \
  36. if (debug) \
  37. printk(KERN_DEBUG \
  38. KBUILD_MODNAME ": " text "\n" , ## __VA_ARGS__)
  39. #define nvt_dbg_verbose(text, ...) \
  40. if (debug > 1) \
  41. printk(KERN_DEBUG \
  42. KBUILD_MODNAME ": " text "\n" , ## __VA_ARGS__)
  43. #define nvt_dbg_wake(text, ...) \
  44. if (debug > 2) \
  45. printk(KERN_DEBUG \
  46. KBUILD_MODNAME ": " text "\n" , ## __VA_ARGS__)
  47. /*
  48. * Original lirc driver said min value of 76, and recommended value of 256
  49. * for the buffer length, but then used 2048. Never mind that the size of the
  50. * RX FIFO is 32 bytes... So I'm using 32 for RX and 256 for TX atm, but I'm
  51. * not sure if maybe that TX value is off by a factor of 8 (bits vs. bytes),
  52. * and I don't have TX-capable hardware to test/debug on...
  53. */
  54. #define TX_BUF_LEN 256
  55. #define RX_BUF_LEN 32
  56. struct nvt_dev {
  57. struct pnp_dev *pdev;
  58. struct rc_dev *rdev;
  59. struct ir_raw_event rawir;
  60. spinlock_t nvt_lock;
  61. /* for rx */
  62. u8 buf[RX_BUF_LEN];
  63. unsigned int pkts;
  64. struct {
  65. spinlock_t lock;
  66. u8 buf[TX_BUF_LEN];
  67. unsigned int buf_count;
  68. unsigned int cur_buf_num;
  69. wait_queue_head_t queue;
  70. u8 tx_state;
  71. } tx;
  72. /* EFER Config register index/data pair */
  73. u8 cr_efir;
  74. u8 cr_efdr;
  75. /* hardware I/O settings */
  76. unsigned long cir_addr;
  77. unsigned long cir_wake_addr;
  78. int cir_irq;
  79. int cir_wake_irq;
  80. /* hardware id */
  81. u8 chip_major;
  82. u8 chip_minor;
  83. /* hardware features */
  84. bool hw_learning_capable;
  85. bool hw_tx_capable;
  86. /* rx settings */
  87. bool learning_enabled;
  88. bool carrier_detect_enabled;
  89. /* track cir wake state */
  90. u8 wake_state;
  91. /* for study */
  92. u8 study_state;
  93. /* carrier period = 1 / frequency */
  94. u32 carrier;
  95. };
  96. /* study states */
  97. #define ST_STUDY_NONE 0x0
  98. #define ST_STUDY_START 0x1
  99. #define ST_STUDY_CARRIER 0x2
  100. #define ST_STUDY_ALL_RECV 0x4
  101. /* wake states */
  102. #define ST_WAKE_NONE 0x0
  103. #define ST_WAKE_START 0x1
  104. #define ST_WAKE_FINISH 0x2
  105. /* receive states */
  106. #define ST_RX_WAIT_7F 0x1
  107. #define ST_RX_WAIT_HEAD 0x2
  108. #define ST_RX_WAIT_SILENT_END 0x4
  109. /* send states */
  110. #define ST_TX_NONE 0x0
  111. #define ST_TX_REQUEST 0x2
  112. #define ST_TX_REPLY 0x4
  113. /* buffer packet constants */
  114. #define BUF_PULSE_BIT 0x80
  115. #define BUF_LEN_MASK 0x7f
  116. #define BUF_REPEAT_BYTE 0x70
  117. #define BUF_REPEAT_MASK 0xf0
  118. /* CIR settings */
  119. /* total length of CIR and CIR WAKE */
  120. #define CIR_IOREG_LENGTH 0x0f
  121. /* RX limit length, 8 high bits for SLCH, 8 low bits for SLCL (0x7d0 = 2000) */
  122. #define CIR_RX_LIMIT_COUNT 0x7d0
  123. /* CIR Regs */
  124. #define CIR_IRCON 0x00
  125. #define CIR_IRSTS 0x01
  126. #define CIR_IREN 0x02
  127. #define CIR_RXFCONT 0x03
  128. #define CIR_CP 0x04
  129. #define CIR_CC 0x05
  130. #define CIR_SLCH 0x06
  131. #define CIR_SLCL 0x07
  132. #define CIR_FIFOCON 0x08
  133. #define CIR_IRFIFOSTS 0x09
  134. #define CIR_SRXFIFO 0x0a
  135. #define CIR_TXFCONT 0x0b
  136. #define CIR_STXFIFO 0x0c
  137. #define CIR_FCCH 0x0d
  138. #define CIR_FCCL 0x0e
  139. #define CIR_IRFSM 0x0f
  140. /* CIR IRCON settings */
  141. #define CIR_IRCON_RECV 0x80
  142. #define CIR_IRCON_WIREN 0x40
  143. #define CIR_IRCON_TXEN 0x20
  144. #define CIR_IRCON_RXEN 0x10
  145. #define CIR_IRCON_WRXINV 0x08
  146. #define CIR_IRCON_RXINV 0x04
  147. #define CIR_IRCON_SAMPLE_PERIOD_SEL_1 0x00
  148. #define CIR_IRCON_SAMPLE_PERIOD_SEL_25 0x01
  149. #define CIR_IRCON_SAMPLE_PERIOD_SEL_50 0x02
  150. #define CIR_IRCON_SAMPLE_PERIOD_SEL_100 0x03
  151. /* FIXME: make this a runtime option */
  152. /* select sample period as 50us */
  153. #define CIR_IRCON_SAMPLE_PERIOD_SEL CIR_IRCON_SAMPLE_PERIOD_SEL_50
  154. /* CIR IRSTS settings */
  155. #define CIR_IRSTS_RDR 0x80
  156. #define CIR_IRSTS_RTR 0x40
  157. #define CIR_IRSTS_PE 0x20
  158. #define CIR_IRSTS_RFO 0x10
  159. #define CIR_IRSTS_TE 0x08
  160. #define CIR_IRSTS_TTR 0x04
  161. #define CIR_IRSTS_TFU 0x02
  162. #define CIR_IRSTS_GH 0x01
  163. /* CIR IREN settings */
  164. #define CIR_IREN_RDR 0x80
  165. #define CIR_IREN_RTR 0x40
  166. #define CIR_IREN_PE 0x20
  167. #define CIR_IREN_RFO 0x10
  168. #define CIR_IREN_TE 0x08
  169. #define CIR_IREN_TTR 0x04
  170. #define CIR_IREN_TFU 0x02
  171. #define CIR_IREN_GH 0x01
  172. /* CIR FIFOCON settings */
  173. #define CIR_FIFOCON_TXFIFOCLR 0x80
  174. #define CIR_FIFOCON_TX_TRIGGER_LEV_31 0x00
  175. #define CIR_FIFOCON_TX_TRIGGER_LEV_24 0x10
  176. #define CIR_FIFOCON_TX_TRIGGER_LEV_16 0x20
  177. #define CIR_FIFOCON_TX_TRIGGER_LEV_8 0x30
  178. /* FIXME: make this a runtime option */
  179. /* select TX trigger level as 16 */
  180. #define CIR_FIFOCON_TX_TRIGGER_LEV CIR_FIFOCON_TX_TRIGGER_LEV_16
  181. #define CIR_FIFOCON_RXFIFOCLR 0x08
  182. #define CIR_FIFOCON_RX_TRIGGER_LEV_1 0x00
  183. #define CIR_FIFOCON_RX_TRIGGER_LEV_8 0x01
  184. #define CIR_FIFOCON_RX_TRIGGER_LEV_16 0x02
  185. #define CIR_FIFOCON_RX_TRIGGER_LEV_24 0x03
  186. /* FIXME: make this a runtime option */
  187. /* select RX trigger level as 24 */
  188. #define CIR_FIFOCON_RX_TRIGGER_LEV CIR_FIFOCON_RX_TRIGGER_LEV_24
  189. /* CIR IRFIFOSTS settings */
  190. #define CIR_IRFIFOSTS_IR_PENDING 0x80
  191. #define CIR_IRFIFOSTS_RX_GS 0x40
  192. #define CIR_IRFIFOSTS_RX_FTA 0x20
  193. #define CIR_IRFIFOSTS_RX_EMPTY 0x10
  194. #define CIR_IRFIFOSTS_RX_FULL 0x08
  195. #define CIR_IRFIFOSTS_TX_FTA 0x04
  196. #define CIR_IRFIFOSTS_TX_EMPTY 0x02
  197. #define CIR_IRFIFOSTS_TX_FULL 0x01
  198. /* CIR WAKE UP Regs */
  199. #define CIR_WAKE_IRCON 0x00
  200. #define CIR_WAKE_IRSTS 0x01
  201. #define CIR_WAKE_IREN 0x02
  202. #define CIR_WAKE_FIFO_CMP_DEEP 0x03
  203. #define CIR_WAKE_FIFO_CMP_TOL 0x04
  204. #define CIR_WAKE_FIFO_COUNT 0x05
  205. #define CIR_WAKE_SLCH 0x06
  206. #define CIR_WAKE_SLCL 0x07
  207. #define CIR_WAKE_FIFOCON 0x08
  208. #define CIR_WAKE_SRXFSTS 0x09
  209. #define CIR_WAKE_SAMPLE_RX_FIFO 0x0a
  210. #define CIR_WAKE_WR_FIFO_DATA 0x0b
  211. #define CIR_WAKE_RD_FIFO_ONLY 0x0c
  212. #define CIR_WAKE_RD_FIFO_ONLY_IDX 0x0d
  213. #define CIR_WAKE_FIFO_IGNORE 0x0e
  214. #define CIR_WAKE_IRFSM 0x0f
  215. /* CIR WAKE UP IRCON settings */
  216. #define CIR_WAKE_IRCON_DEC_RST 0x80
  217. #define CIR_WAKE_IRCON_MODE1 0x40
  218. #define CIR_WAKE_IRCON_MODE0 0x20
  219. #define CIR_WAKE_IRCON_RXEN 0x10
  220. #define CIR_WAKE_IRCON_R 0x08
  221. #define CIR_WAKE_IRCON_RXINV 0x04
  222. /* FIXME/jarod: make this a runtime option */
  223. /* select a same sample period like cir register */
  224. #define CIR_WAKE_IRCON_SAMPLE_PERIOD_SEL CIR_IRCON_SAMPLE_PERIOD_SEL_50
  225. /* CIR WAKE IRSTS Bits */
  226. #define CIR_WAKE_IRSTS_RDR 0x80
  227. #define CIR_WAKE_IRSTS_RTR 0x40
  228. #define CIR_WAKE_IRSTS_PE 0x20
  229. #define CIR_WAKE_IRSTS_RFO 0x10
  230. #define CIR_WAKE_IRSTS_GH 0x08
  231. #define CIR_WAKE_IRSTS_IR_PENDING 0x01
  232. /* CIR WAKE UP IREN Bits */
  233. #define CIR_WAKE_IREN_RDR 0x80
  234. #define CIR_WAKE_IREN_RTR 0x40
  235. #define CIR_WAKE_IREN_PE 0x20
  236. #define CIR_WAKE_IREN_RFO 0x10
  237. #define CIR_WAKE_IREN_TE 0x08
  238. #define CIR_WAKE_IREN_TTR 0x04
  239. #define CIR_WAKE_IREN_TFU 0x02
  240. #define CIR_WAKE_IREN_GH 0x01
  241. /* CIR WAKE FIFOCON settings */
  242. #define CIR_WAKE_FIFOCON_RXFIFOCLR 0x08
  243. #define CIR_WAKE_FIFOCON_RX_TRIGGER_LEV_67 0x00
  244. #define CIR_WAKE_FIFOCON_RX_TRIGGER_LEV_66 0x01
  245. #define CIR_WAKE_FIFOCON_RX_TRIGGER_LEV_65 0x02
  246. #define CIR_WAKE_FIFOCON_RX_TRIGGER_LEV_64 0x03
  247. /* FIXME: make this a runtime option */
  248. /* select WAKE UP RX trigger level as 67 */
  249. #define CIR_WAKE_FIFOCON_RX_TRIGGER_LEV CIR_WAKE_FIFOCON_RX_TRIGGER_LEV_67
  250. /* CIR WAKE SRXFSTS settings */
  251. #define CIR_WAKE_IRFIFOSTS_RX_GS 0x80
  252. #define CIR_WAKE_IRFIFOSTS_RX_FTA 0x40
  253. #define CIR_WAKE_IRFIFOSTS_RX_EMPTY 0x20
  254. #define CIR_WAKE_IRFIFOSTS_RX_FULL 0x10
  255. /*
  256. * The CIR Wake FIFO buffer is 67 bytes long, but the stock remote wakes
  257. * the system comparing only 65 bytes (fails with this set to 67)
  258. */
  259. #define CIR_WAKE_FIFO_CMP_BYTES 65
  260. /* CIR Wake byte comparison tolerance */
  261. #define CIR_WAKE_CMP_TOLERANCE 5
  262. /*
  263. * Extended Function Enable Registers:
  264. * Extended Function Index Register
  265. * Extended Function Data Register
  266. */
  267. #define CR_EFIR 0x2e
  268. #define CR_EFDR 0x2f
  269. /* Possible alternate EFER values, depends on how the chip is wired */
  270. #define CR_EFIR2 0x4e
  271. #define CR_EFDR2 0x4f
  272. /* Extended Function Mode enable/disable magic values */
  273. #define EFER_EFM_ENABLE 0x87
  274. #define EFER_EFM_DISABLE 0xaa
  275. /* Chip IDs found in CR_CHIP_ID_{HI,LO} */
  276. #define CHIP_ID_HIGH_667 0xa5
  277. #define CHIP_ID_HIGH_677B 0xb4
  278. #define CHIP_ID_HIGH_677C 0xc3
  279. #define CHIP_ID_LOW_667 0x13
  280. #define CHIP_ID_LOW_677B2 0x72
  281. #define CHIP_ID_LOW_677B3 0x73
  282. #define CHIP_ID_LOW_677C 0x33
  283. /* Config regs we need to care about */
  284. #define CR_SOFTWARE_RESET 0x02
  285. #define CR_LOGICAL_DEV_SEL 0x07
  286. #define CR_CHIP_ID_HI 0x20
  287. #define CR_CHIP_ID_LO 0x21
  288. #define CR_DEV_POWER_DOWN 0x22 /* bit 2 is CIR power, default power on */
  289. #define CR_OUTPUT_PIN_SEL 0x27
  290. #define CR_MULTIFUNC_PIN_SEL 0x2c
  291. #define CR_LOGICAL_DEV_EN 0x30 /* valid for all logical devices */
  292. /* next three regs valid for both the CIR and CIR_WAKE logical devices */
  293. #define CR_CIR_BASE_ADDR_HI 0x60
  294. #define CR_CIR_BASE_ADDR_LO 0x61
  295. #define CR_CIR_IRQ_RSRC 0x70
  296. /* next three regs valid only for ACPI logical dev */
  297. #define CR_ACPI_CIR_WAKE 0xe0
  298. #define CR_ACPI_IRQ_EVENTS 0xf6
  299. #define CR_ACPI_IRQ_EVENTS2 0xf7
  300. /* Logical devices that we need to care about */
  301. #define LOGICAL_DEV_LPT 0x01
  302. #define LOGICAL_DEV_CIR 0x06
  303. #define LOGICAL_DEV_ACPI 0x0a
  304. #define LOGICAL_DEV_CIR_WAKE 0x0e
  305. #define LOGICAL_DEV_DISABLE 0x00
  306. #define LOGICAL_DEV_ENABLE 0x01
  307. #define CIR_WAKE_ENABLE_BIT 0x08
  308. #define CIR_INTR_MOUSE_IRQ_BIT 0x80
  309. #define PME_INTR_CIR_PASS_BIT 0x08
  310. /* w83677hg CIR pin config */
  311. #define OUTPUT_PIN_SEL_MASK 0xbc
  312. #define OUTPUT_ENABLE_CIR 0x01 /* Pin95=CIRRX, Pin96=CIRTX1 */
  313. #define OUTPUT_ENABLE_CIRWB 0x40 /* enable wide-band sensor */
  314. /* w83667hg CIR pin config */
  315. #define MULTIFUNC_PIN_SEL_MASK 0x1f
  316. #define MULTIFUNC_ENABLE_CIR 0x80 /* Pin75=CIRRX, Pin76=CIRTX1 */
  317. #define MULTIFUNC_ENABLE_CIRWB 0x20 /* enable wide-band sensor */
  318. /* MCE CIR signal length, related on sample period */
  319. /* MCE CIR controller signal length: about 43ms
  320. * 43ms / 50us (sample period) * 0.85 (inaccuracy)
  321. */
  322. #define CONTROLLER_BUF_LEN_MIN 830
  323. /* MCE CIR keyboard signal length: about 26ms
  324. * 26ms / 50us (sample period) * 0.85 (inaccuracy)
  325. */
  326. #define KEYBOARD_BUF_LEN_MAX 650
  327. #define KEYBOARD_BUF_LEN_MIN 610
  328. /* MCE CIR mouse signal length: about 24ms
  329. * 24ms / 50us (sample period) * 0.85 (inaccuracy)
  330. */
  331. #define MOUSE_BUF_LEN_MIN 565
  332. #define CIR_SAMPLE_PERIOD 50
  333. #define CIR_SAMPLE_LOW_INACCURACY 0.85
  334. /* MAX silence time that driver will sent to lirc */
  335. #define MAX_SILENCE_TIME 60000
  336. #if CIR_IRCON_SAMPLE_PERIOD_SEL == CIR_IRCON_SAMPLE_PERIOD_SEL_100
  337. #define SAMPLE_PERIOD 100
  338. #elif CIR_IRCON_SAMPLE_PERIOD_SEL == CIR_IRCON_SAMPLE_PERIOD_SEL_50
  339. #define SAMPLE_PERIOD 50
  340. #elif CIR_IRCON_SAMPLE_PERIOD_SEL == CIR_IRCON_SAMPLE_PERIOD_SEL_25
  341. #define SAMPLE_PERIOD 25
  342. #else
  343. #define SAMPLE_PERIOD 1
  344. #endif
  345. /* as VISTA MCE definition, valid carrier value */
  346. #define MAX_CARRIER 60000
  347. #define MIN_CARRIER 30000