nuvoton-cir.h 11 KB

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