nuvoton-cir.h 11 KB

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