nuvoton-cir.h 11 KB

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