nuvoton-cir.h 11 KB

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