nuvoton-cir.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. #include <linux/spinlock.h>
  23. #include <linux/ioctl.h>
  24. /* platform driver name to register */
  25. #define NVT_DRIVER_NAME "nuvoton-cir"
  26. /* debugging module parameter */
  27. static int debug;
  28. #define nvt_dbg(text, ...) \
  29. if (debug) \
  30. printk(KERN_DEBUG \
  31. KBUILD_MODNAME ": " text "\n" , ## __VA_ARGS__)
  32. #define nvt_dbg_verbose(text, ...) \
  33. if (debug > 1) \
  34. printk(KERN_DEBUG \
  35. KBUILD_MODNAME ": " text "\n" , ## __VA_ARGS__)
  36. #define nvt_dbg_wake(text, ...) \
  37. if (debug > 2) \
  38. printk(KERN_DEBUG \
  39. KBUILD_MODNAME ": " text "\n" , ## __VA_ARGS__)
  40. #define RX_BUF_LEN 32
  41. #define SIO_ID_MASK 0xfff0
  42. enum nvt_chip_ver {
  43. NVT_UNKNOWN = 0,
  44. NVT_W83667HG = 0xa510,
  45. NVT_6775F = 0xb470,
  46. NVT_6776F = 0xc330,
  47. NVT_6779D = 0xc560,
  48. NVT_INVALID = 0xffff,
  49. };
  50. struct nvt_chip {
  51. const char *name;
  52. enum nvt_chip_ver chip_ver;
  53. };
  54. struct nvt_dev {
  55. struct rc_dev *rdev;
  56. spinlock_t lock;
  57. /* for rx */
  58. u8 buf[RX_BUF_LEN];
  59. unsigned int pkts;
  60. /* EFER Config register index/data pair */
  61. u32 cr_efir;
  62. u32 cr_efdr;
  63. /* hardware I/O settings */
  64. unsigned long cir_addr;
  65. unsigned long cir_wake_addr;
  66. int cir_irq;
  67. enum nvt_chip_ver chip_ver;
  68. /* hardware id */
  69. u8 chip_major;
  70. u8 chip_minor;
  71. /* carrier period = 1 / frequency */
  72. u32 carrier;
  73. };
  74. /* buffer packet constants */
  75. #define BUF_PULSE_BIT 0x80
  76. #define BUF_LEN_MASK 0x7f
  77. #define BUF_REPEAT_BYTE 0x70
  78. #define BUF_REPEAT_MASK 0xf0
  79. /* CIR settings */
  80. /* total length of CIR and CIR WAKE */
  81. #define CIR_IOREG_LENGTH 0x0f
  82. /* RX limit length, 8 high bits for SLCH, 8 low bits for SLCL */
  83. #define CIR_RX_LIMIT_COUNT (IR_DEFAULT_TIMEOUT / US_TO_NS(SAMPLE_PERIOD))
  84. /* CIR Regs */
  85. #define CIR_IRCON 0x00
  86. #define CIR_IRSTS 0x01
  87. #define CIR_IREN 0x02
  88. #define CIR_RXFCONT 0x03
  89. #define CIR_CP 0x04
  90. #define CIR_CC 0x05
  91. #define CIR_SLCH 0x06
  92. #define CIR_SLCL 0x07
  93. #define CIR_FIFOCON 0x08
  94. #define CIR_IRFIFOSTS 0x09
  95. #define CIR_SRXFIFO 0x0a
  96. #define CIR_TXFCONT 0x0b
  97. #define CIR_STXFIFO 0x0c
  98. #define CIR_FCCH 0x0d
  99. #define CIR_FCCL 0x0e
  100. #define CIR_IRFSM 0x0f
  101. /* CIR IRCON settings */
  102. #define CIR_IRCON_RECV 0x80
  103. #define CIR_IRCON_WIREN 0x40
  104. #define CIR_IRCON_TXEN 0x20
  105. #define CIR_IRCON_RXEN 0x10
  106. #define CIR_IRCON_WRXINV 0x08
  107. #define CIR_IRCON_RXINV 0x04
  108. #define CIR_IRCON_SAMPLE_PERIOD_SEL_1 0x00
  109. #define CIR_IRCON_SAMPLE_PERIOD_SEL_25 0x01
  110. #define CIR_IRCON_SAMPLE_PERIOD_SEL_50 0x02
  111. #define CIR_IRCON_SAMPLE_PERIOD_SEL_100 0x03
  112. /* FIXME: make this a runtime option */
  113. /* select sample period as 50us */
  114. #define CIR_IRCON_SAMPLE_PERIOD_SEL CIR_IRCON_SAMPLE_PERIOD_SEL_50
  115. /* CIR IRSTS settings */
  116. #define CIR_IRSTS_RDR 0x80
  117. #define CIR_IRSTS_RTR 0x40
  118. #define CIR_IRSTS_PE 0x20
  119. #define CIR_IRSTS_RFO 0x10
  120. #define CIR_IRSTS_TE 0x08
  121. #define CIR_IRSTS_TTR 0x04
  122. #define CIR_IRSTS_TFU 0x02
  123. #define CIR_IRSTS_GH 0x01
  124. /* CIR IREN settings */
  125. #define CIR_IREN_RDR 0x80
  126. #define CIR_IREN_RTR 0x40
  127. #define CIR_IREN_PE 0x20
  128. #define CIR_IREN_RFO 0x10
  129. #define CIR_IREN_TE 0x08
  130. #define CIR_IREN_TTR 0x04
  131. #define CIR_IREN_TFU 0x02
  132. #define CIR_IREN_GH 0x01
  133. /* CIR FIFOCON settings */
  134. #define CIR_FIFOCON_TXFIFOCLR 0x80
  135. #define CIR_FIFOCON_TX_TRIGGER_LEV_31 0x00
  136. #define CIR_FIFOCON_TX_TRIGGER_LEV_24 0x10
  137. #define CIR_FIFOCON_TX_TRIGGER_LEV_16 0x20
  138. #define CIR_FIFOCON_TX_TRIGGER_LEV_8 0x30
  139. /* FIXME: make this a runtime option */
  140. /* select TX trigger level as 16 */
  141. #define CIR_FIFOCON_TX_TRIGGER_LEV CIR_FIFOCON_TX_TRIGGER_LEV_16
  142. #define CIR_FIFOCON_RXFIFOCLR 0x08
  143. #define CIR_FIFOCON_RX_TRIGGER_LEV_1 0x00
  144. #define CIR_FIFOCON_RX_TRIGGER_LEV_8 0x01
  145. #define CIR_FIFOCON_RX_TRIGGER_LEV_16 0x02
  146. #define CIR_FIFOCON_RX_TRIGGER_LEV_24 0x03
  147. /* FIXME: make this a runtime option */
  148. /* select RX trigger level as 24 */
  149. #define CIR_FIFOCON_RX_TRIGGER_LEV CIR_FIFOCON_RX_TRIGGER_LEV_24
  150. /* CIR IRFIFOSTS settings */
  151. #define CIR_IRFIFOSTS_IR_PENDING 0x80
  152. #define CIR_IRFIFOSTS_RX_GS 0x40
  153. #define CIR_IRFIFOSTS_RX_FTA 0x20
  154. #define CIR_IRFIFOSTS_RX_EMPTY 0x10
  155. #define CIR_IRFIFOSTS_RX_FULL 0x08
  156. #define CIR_IRFIFOSTS_TX_FTA 0x04
  157. #define CIR_IRFIFOSTS_TX_EMPTY 0x02
  158. #define CIR_IRFIFOSTS_TX_FULL 0x01
  159. /* CIR WAKE UP Regs */
  160. #define CIR_WAKE_IRCON 0x00
  161. #define CIR_WAKE_IRSTS 0x01
  162. #define CIR_WAKE_IREN 0x02
  163. #define CIR_WAKE_FIFO_CMP_DEEP 0x03
  164. #define CIR_WAKE_FIFO_CMP_TOL 0x04
  165. #define CIR_WAKE_FIFO_COUNT 0x05
  166. #define CIR_WAKE_SLCH 0x06
  167. #define CIR_WAKE_SLCL 0x07
  168. #define CIR_WAKE_FIFOCON 0x08
  169. #define CIR_WAKE_SRXFSTS 0x09
  170. #define CIR_WAKE_SAMPLE_RX_FIFO 0x0a
  171. #define CIR_WAKE_WR_FIFO_DATA 0x0b
  172. #define CIR_WAKE_RD_FIFO_ONLY 0x0c
  173. #define CIR_WAKE_RD_FIFO_ONLY_IDX 0x0d
  174. #define CIR_WAKE_FIFO_IGNORE 0x0e
  175. #define CIR_WAKE_IRFSM 0x0f
  176. /* CIR WAKE UP IRCON settings */
  177. #define CIR_WAKE_IRCON_DEC_RST 0x80
  178. #define CIR_WAKE_IRCON_MODE1 0x40
  179. #define CIR_WAKE_IRCON_MODE0 0x20
  180. #define CIR_WAKE_IRCON_RXEN 0x10
  181. #define CIR_WAKE_IRCON_R 0x08
  182. #define CIR_WAKE_IRCON_RXINV 0x04
  183. /* FIXME/jarod: make this a runtime option */
  184. /* select a same sample period like cir register */
  185. #define CIR_WAKE_IRCON_SAMPLE_PERIOD_SEL CIR_IRCON_SAMPLE_PERIOD_SEL_50
  186. /* CIR WAKE IRSTS Bits */
  187. #define CIR_WAKE_IRSTS_RDR 0x80
  188. #define CIR_WAKE_IRSTS_RTR 0x40
  189. #define CIR_WAKE_IRSTS_PE 0x20
  190. #define CIR_WAKE_IRSTS_RFO 0x10
  191. #define CIR_WAKE_IRSTS_GH 0x08
  192. #define CIR_WAKE_IRSTS_IR_PENDING 0x01
  193. /* CIR WAKE UP IREN Bits */
  194. #define CIR_WAKE_IREN_RDR 0x80
  195. #define CIR_WAKE_IREN_RTR 0x40
  196. #define CIR_WAKE_IREN_PE 0x20
  197. #define CIR_WAKE_IREN_RFO 0x10
  198. #define CIR_WAKE_IREN_GH 0x08
  199. /* CIR WAKE FIFOCON settings */
  200. #define CIR_WAKE_FIFOCON_RXFIFOCLR 0x08
  201. #define CIR_WAKE_FIFOCON_RX_TRIGGER_LEV_67 0x00
  202. #define CIR_WAKE_FIFOCON_RX_TRIGGER_LEV_66 0x01
  203. #define CIR_WAKE_FIFOCON_RX_TRIGGER_LEV_65 0x02
  204. #define CIR_WAKE_FIFOCON_RX_TRIGGER_LEV_64 0x03
  205. /* FIXME: make this a runtime option */
  206. /* select WAKE UP RX trigger level as 67 */
  207. #define CIR_WAKE_FIFOCON_RX_TRIGGER_LEV CIR_WAKE_FIFOCON_RX_TRIGGER_LEV_67
  208. /* CIR WAKE SRXFSTS settings */
  209. #define CIR_WAKE_IRFIFOSTS_RX_GS 0x80
  210. #define CIR_WAKE_IRFIFOSTS_RX_FTA 0x40
  211. #define CIR_WAKE_IRFIFOSTS_RX_EMPTY 0x20
  212. #define CIR_WAKE_IRFIFOSTS_RX_FULL 0x10
  213. /*
  214. * The CIR Wake FIFO buffer is 67 bytes long, but the stock remote wakes
  215. * the system comparing only 65 bytes (fails with this set to 67)
  216. */
  217. #define CIR_WAKE_FIFO_CMP_BYTES 65
  218. /* CIR Wake byte comparison tolerance */
  219. #define CIR_WAKE_CMP_TOLERANCE 5
  220. /*
  221. * Extended Function Enable Registers:
  222. * Extended Function Index Register
  223. * Extended Function Data Register
  224. */
  225. #define CR_EFIR 0x2e
  226. #define CR_EFDR 0x2f
  227. /* Possible alternate EFER values, depends on how the chip is wired */
  228. #define CR_EFIR2 0x4e
  229. #define CR_EFDR2 0x4f
  230. /* Extended Function Mode enable/disable magic values */
  231. #define EFER_EFM_ENABLE 0x87
  232. #define EFER_EFM_DISABLE 0xaa
  233. /* Config regs we need to care about */
  234. #define CR_SOFTWARE_RESET 0x02
  235. #define CR_LOGICAL_DEV_SEL 0x07
  236. #define CR_CHIP_ID_HI 0x20
  237. #define CR_CHIP_ID_LO 0x21
  238. #define CR_DEV_POWER_DOWN 0x22 /* bit 2 is CIR power, default power on */
  239. #define CR_OUTPUT_PIN_SEL 0x27
  240. #define CR_MULTIFUNC_PIN_SEL 0x2c
  241. #define CR_LOGICAL_DEV_EN 0x30 /* valid for all logical devices */
  242. /* next three regs valid for both the CIR and CIR_WAKE logical devices */
  243. #define CR_CIR_BASE_ADDR_HI 0x60
  244. #define CR_CIR_BASE_ADDR_LO 0x61
  245. #define CR_CIR_IRQ_RSRC 0x70
  246. /* next three regs valid only for ACPI logical dev */
  247. #define CR_ACPI_CIR_WAKE 0xe0
  248. #define CR_ACPI_IRQ_EVENTS 0xf6
  249. #define CR_ACPI_IRQ_EVENTS2 0xf7
  250. /* Logical devices that we need to care about */
  251. #define LOGICAL_DEV_LPT 0x01
  252. #define LOGICAL_DEV_CIR 0x06
  253. #define LOGICAL_DEV_ACPI 0x0a
  254. #define LOGICAL_DEV_CIR_WAKE 0x0e
  255. #define LOGICAL_DEV_DISABLE 0x00
  256. #define LOGICAL_DEV_ENABLE 0x01
  257. #define CIR_WAKE_ENABLE_BIT 0x08
  258. #define PME_INTR_CIR_PASS_BIT 0x08
  259. /* w83677hg CIR pin config */
  260. #define OUTPUT_PIN_SEL_MASK 0xbc
  261. #define OUTPUT_ENABLE_CIR 0x01 /* Pin95=CIRRX, Pin96=CIRTX1 */
  262. #define OUTPUT_ENABLE_CIRWB 0x40 /* enable wide-band sensor */
  263. /* w83667hg CIR pin config */
  264. #define MULTIFUNC_PIN_SEL_MASK 0x1f
  265. #define MULTIFUNC_ENABLE_CIR 0x80 /* Pin75=CIRRX, Pin76=CIRTX1 */
  266. #define MULTIFUNC_ENABLE_CIRWB 0x20 /* enable wide-band sensor */
  267. /* MCE CIR signal length, related on sample period */
  268. /* MCE CIR controller signal length: about 43ms
  269. * 43ms / 50us (sample period) * 0.85 (inaccuracy)
  270. */
  271. #define CONTROLLER_BUF_LEN_MIN 830
  272. /* MCE CIR keyboard signal length: about 26ms
  273. * 26ms / 50us (sample period) * 0.85 (inaccuracy)
  274. */
  275. #define KEYBOARD_BUF_LEN_MAX 650
  276. #define KEYBOARD_BUF_LEN_MIN 610
  277. /* MCE CIR mouse signal length: about 24ms
  278. * 24ms / 50us (sample period) * 0.85 (inaccuracy)
  279. */
  280. #define MOUSE_BUF_LEN_MIN 565
  281. #define CIR_SAMPLE_PERIOD 50
  282. #define CIR_SAMPLE_LOW_INACCURACY 0.85
  283. /* MAX silence time that driver will sent to lirc */
  284. #define MAX_SILENCE_TIME 60000
  285. #if CIR_IRCON_SAMPLE_PERIOD_SEL == CIR_IRCON_SAMPLE_PERIOD_SEL_100
  286. #define SAMPLE_PERIOD 100
  287. #elif CIR_IRCON_SAMPLE_PERIOD_SEL == CIR_IRCON_SAMPLE_PERIOD_SEL_50
  288. #define SAMPLE_PERIOD 50
  289. #elif CIR_IRCON_SAMPLE_PERIOD_SEL == CIR_IRCON_SAMPLE_PERIOD_SEL_25
  290. #define SAMPLE_PERIOD 25
  291. #else
  292. #define SAMPLE_PERIOD 1
  293. #endif
  294. /* as VISTA MCE definition, valid carrier value */
  295. #define MAX_CARRIER 60000
  296. #define MIN_CARRIER 30000
  297. /* max wakeup sequence length */
  298. #define WAKEUP_MAX_SIZE 65