nuvoton-cir.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  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/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/pnp.h>
  30. #include <linux/io.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/sched.h>
  33. #include <linux/slab.h>
  34. #include <media/rc-core.h>
  35. #include <linux/pci_ids.h>
  36. #include "nuvoton-cir.h"
  37. static char *chip_id = "w836x7hg";
  38. /* write val to config reg */
  39. static inline void nvt_cr_write(struct nvt_dev *nvt, u8 val, u8 reg)
  40. {
  41. outb(reg, nvt->cr_efir);
  42. outb(val, nvt->cr_efdr);
  43. }
  44. /* read val from config reg */
  45. static inline u8 nvt_cr_read(struct nvt_dev *nvt, u8 reg)
  46. {
  47. outb(reg, nvt->cr_efir);
  48. return inb(nvt->cr_efdr);
  49. }
  50. /* update config register bit without changing other bits */
  51. static inline void nvt_set_reg_bit(struct nvt_dev *nvt, u8 val, u8 reg)
  52. {
  53. u8 tmp = nvt_cr_read(nvt, reg) | val;
  54. nvt_cr_write(nvt, tmp, reg);
  55. }
  56. /* clear config register bit without changing other bits */
  57. static inline void nvt_clear_reg_bit(struct nvt_dev *nvt, u8 val, u8 reg)
  58. {
  59. u8 tmp = nvt_cr_read(nvt, reg) & ~val;
  60. nvt_cr_write(nvt, tmp, reg);
  61. }
  62. /* enter extended function mode */
  63. static inline void nvt_efm_enable(struct nvt_dev *nvt)
  64. {
  65. /* Enabling Extended Function Mode explicitly requires writing 2x */
  66. outb(EFER_EFM_ENABLE, nvt->cr_efir);
  67. outb(EFER_EFM_ENABLE, nvt->cr_efir);
  68. }
  69. /* exit extended function mode */
  70. static inline void nvt_efm_disable(struct nvt_dev *nvt)
  71. {
  72. outb(EFER_EFM_DISABLE, nvt->cr_efir);
  73. }
  74. /*
  75. * When you want to address a specific logical device, write its logical
  76. * device number to CR_LOGICAL_DEV_SEL, then enable/disable by writing
  77. * 0x1/0x0 respectively to CR_LOGICAL_DEV_EN.
  78. */
  79. static inline void nvt_select_logical_dev(struct nvt_dev *nvt, u8 ldev)
  80. {
  81. outb(CR_LOGICAL_DEV_SEL, nvt->cr_efir);
  82. outb(ldev, nvt->cr_efdr);
  83. }
  84. /* write val to cir config register */
  85. static inline void nvt_cir_reg_write(struct nvt_dev *nvt, u8 val, u8 offset)
  86. {
  87. outb(val, nvt->cir_addr + offset);
  88. }
  89. /* read val from cir config register */
  90. static u8 nvt_cir_reg_read(struct nvt_dev *nvt, u8 offset)
  91. {
  92. u8 val;
  93. val = inb(nvt->cir_addr + offset);
  94. return val;
  95. }
  96. /* write val to cir wake register */
  97. static inline void nvt_cir_wake_reg_write(struct nvt_dev *nvt,
  98. u8 val, u8 offset)
  99. {
  100. outb(val, nvt->cir_wake_addr + offset);
  101. }
  102. /* read val from cir wake config register */
  103. static u8 nvt_cir_wake_reg_read(struct nvt_dev *nvt, u8 offset)
  104. {
  105. u8 val;
  106. val = inb(nvt->cir_wake_addr + offset);
  107. return val;
  108. }
  109. #define pr_reg(text, ...) \
  110. printk(KERN_INFO KBUILD_MODNAME ": " text, ## __VA_ARGS__)
  111. /* dump current cir register contents */
  112. static void cir_dump_regs(struct nvt_dev *nvt)
  113. {
  114. nvt_efm_enable(nvt);
  115. nvt_select_logical_dev(nvt, LOGICAL_DEV_CIR);
  116. pr_reg("%s: Dump CIR logical device registers:\n", NVT_DRIVER_NAME);
  117. pr_reg(" * CR CIR ACTIVE : 0x%x\n",
  118. nvt_cr_read(nvt, CR_LOGICAL_DEV_EN));
  119. pr_reg(" * CR CIR BASE ADDR: 0x%x\n",
  120. (nvt_cr_read(nvt, CR_CIR_BASE_ADDR_HI) << 8) |
  121. nvt_cr_read(nvt, CR_CIR_BASE_ADDR_LO));
  122. pr_reg(" * CR CIR IRQ NUM: 0x%x\n",
  123. nvt_cr_read(nvt, CR_CIR_IRQ_RSRC));
  124. nvt_efm_disable(nvt);
  125. pr_reg("%s: Dump CIR registers:\n", NVT_DRIVER_NAME);
  126. pr_reg(" * IRCON: 0x%x\n", nvt_cir_reg_read(nvt, CIR_IRCON));
  127. pr_reg(" * IRSTS: 0x%x\n", nvt_cir_reg_read(nvt, CIR_IRSTS));
  128. pr_reg(" * IREN: 0x%x\n", nvt_cir_reg_read(nvt, CIR_IREN));
  129. pr_reg(" * RXFCONT: 0x%x\n", nvt_cir_reg_read(nvt, CIR_RXFCONT));
  130. pr_reg(" * CP: 0x%x\n", nvt_cir_reg_read(nvt, CIR_CP));
  131. pr_reg(" * CC: 0x%x\n", nvt_cir_reg_read(nvt, CIR_CC));
  132. pr_reg(" * SLCH: 0x%x\n", nvt_cir_reg_read(nvt, CIR_SLCH));
  133. pr_reg(" * SLCL: 0x%x\n", nvt_cir_reg_read(nvt, CIR_SLCL));
  134. pr_reg(" * FIFOCON: 0x%x\n", nvt_cir_reg_read(nvt, CIR_FIFOCON));
  135. pr_reg(" * IRFIFOSTS: 0x%x\n", nvt_cir_reg_read(nvt, CIR_IRFIFOSTS));
  136. pr_reg(" * SRXFIFO: 0x%x\n", nvt_cir_reg_read(nvt, CIR_SRXFIFO));
  137. pr_reg(" * TXFCONT: 0x%x\n", nvt_cir_reg_read(nvt, CIR_TXFCONT));
  138. pr_reg(" * STXFIFO: 0x%x\n", nvt_cir_reg_read(nvt, CIR_STXFIFO));
  139. pr_reg(" * FCCH: 0x%x\n", nvt_cir_reg_read(nvt, CIR_FCCH));
  140. pr_reg(" * FCCL: 0x%x\n", nvt_cir_reg_read(nvt, CIR_FCCL));
  141. pr_reg(" * IRFSM: 0x%x\n", nvt_cir_reg_read(nvt, CIR_IRFSM));
  142. }
  143. /* dump current cir wake register contents */
  144. static void cir_wake_dump_regs(struct nvt_dev *nvt)
  145. {
  146. u8 i, fifo_len;
  147. nvt_efm_enable(nvt);
  148. nvt_select_logical_dev(nvt, LOGICAL_DEV_CIR_WAKE);
  149. pr_reg("%s: Dump CIR WAKE logical device registers:\n",
  150. NVT_DRIVER_NAME);
  151. pr_reg(" * CR CIR WAKE ACTIVE : 0x%x\n",
  152. nvt_cr_read(nvt, CR_LOGICAL_DEV_EN));
  153. pr_reg(" * CR CIR WAKE BASE ADDR: 0x%x\n",
  154. (nvt_cr_read(nvt, CR_CIR_BASE_ADDR_HI) << 8) |
  155. nvt_cr_read(nvt, CR_CIR_BASE_ADDR_LO));
  156. pr_reg(" * CR CIR WAKE IRQ NUM: 0x%x\n",
  157. nvt_cr_read(nvt, CR_CIR_IRQ_RSRC));
  158. nvt_efm_disable(nvt);
  159. pr_reg("%s: Dump CIR WAKE registers\n", NVT_DRIVER_NAME);
  160. pr_reg(" * IRCON: 0x%x\n",
  161. nvt_cir_wake_reg_read(nvt, CIR_WAKE_IRCON));
  162. pr_reg(" * IRSTS: 0x%x\n",
  163. nvt_cir_wake_reg_read(nvt, CIR_WAKE_IRSTS));
  164. pr_reg(" * IREN: 0x%x\n",
  165. nvt_cir_wake_reg_read(nvt, CIR_WAKE_IREN));
  166. pr_reg(" * FIFO CMP DEEP: 0x%x\n",
  167. nvt_cir_wake_reg_read(nvt, CIR_WAKE_FIFO_CMP_DEEP));
  168. pr_reg(" * FIFO CMP TOL: 0x%x\n",
  169. nvt_cir_wake_reg_read(nvt, CIR_WAKE_FIFO_CMP_TOL));
  170. pr_reg(" * FIFO COUNT: 0x%x\n",
  171. nvt_cir_wake_reg_read(nvt, CIR_WAKE_FIFO_COUNT));
  172. pr_reg(" * SLCH: 0x%x\n",
  173. nvt_cir_wake_reg_read(nvt, CIR_WAKE_SLCH));
  174. pr_reg(" * SLCL: 0x%x\n",
  175. nvt_cir_wake_reg_read(nvt, CIR_WAKE_SLCL));
  176. pr_reg(" * FIFOCON: 0x%x\n",
  177. nvt_cir_wake_reg_read(nvt, CIR_WAKE_FIFOCON));
  178. pr_reg(" * SRXFSTS: 0x%x\n",
  179. nvt_cir_wake_reg_read(nvt, CIR_WAKE_SRXFSTS));
  180. pr_reg(" * SAMPLE RX FIFO: 0x%x\n",
  181. nvt_cir_wake_reg_read(nvt, CIR_WAKE_SAMPLE_RX_FIFO));
  182. pr_reg(" * WR FIFO DATA: 0x%x\n",
  183. nvt_cir_wake_reg_read(nvt, CIR_WAKE_WR_FIFO_DATA));
  184. pr_reg(" * RD FIFO ONLY: 0x%x\n",
  185. nvt_cir_wake_reg_read(nvt, CIR_WAKE_RD_FIFO_ONLY));
  186. pr_reg(" * RD FIFO ONLY IDX: 0x%x\n",
  187. nvt_cir_wake_reg_read(nvt, CIR_WAKE_RD_FIFO_ONLY_IDX));
  188. pr_reg(" * FIFO IGNORE: 0x%x\n",
  189. nvt_cir_wake_reg_read(nvt, CIR_WAKE_FIFO_IGNORE));
  190. pr_reg(" * IRFSM: 0x%x\n",
  191. nvt_cir_wake_reg_read(nvt, CIR_WAKE_IRFSM));
  192. fifo_len = nvt_cir_wake_reg_read(nvt, CIR_WAKE_FIFO_COUNT);
  193. pr_reg("%s: Dump CIR WAKE FIFO (len %d)\n", NVT_DRIVER_NAME, fifo_len);
  194. pr_reg("* Contents = ");
  195. for (i = 0; i < fifo_len; i++)
  196. printk(KERN_CONT "%02x ",
  197. nvt_cir_wake_reg_read(nvt, CIR_WAKE_RD_FIFO_ONLY));
  198. printk(KERN_CONT "\n");
  199. }
  200. /* detect hardware features */
  201. static int nvt_hw_detect(struct nvt_dev *nvt)
  202. {
  203. unsigned long flags;
  204. u8 chip_major, chip_minor;
  205. int ret = 0;
  206. nvt_efm_enable(nvt);
  207. /* Check if we're wired for the alternate EFER setup */
  208. chip_major = nvt_cr_read(nvt, CR_CHIP_ID_HI);
  209. if (chip_major == 0xff) {
  210. nvt->cr_efir = CR_EFIR2;
  211. nvt->cr_efdr = CR_EFDR2;
  212. nvt_efm_enable(nvt);
  213. chip_major = nvt_cr_read(nvt, CR_CHIP_ID_HI);
  214. }
  215. chip_minor = nvt_cr_read(nvt, CR_CHIP_ID_LO);
  216. nvt_dbg("%s: chip id: 0x%02x 0x%02x", chip_id, chip_major, chip_minor);
  217. if (chip_major != CHIP_ID_HIGH ||
  218. (chip_minor != CHIP_ID_LOW && chip_minor != CHIP_ID_LOW2)) {
  219. nvt_pr(KERN_ERR, "%s: unsupported chip, id: 0x%02x 0x%02x",
  220. chip_id, chip_major, chip_minor);
  221. ret = -ENODEV;
  222. }
  223. nvt_efm_disable(nvt);
  224. spin_lock_irqsave(&nvt->nvt_lock, flags);
  225. nvt->chip_major = chip_major;
  226. nvt->chip_minor = chip_minor;
  227. spin_unlock_irqrestore(&nvt->nvt_lock, flags);
  228. return ret;
  229. }
  230. static void nvt_cir_ldev_init(struct nvt_dev *nvt)
  231. {
  232. u8 val;
  233. /* output pin selection (Pin95=CIRRX, Pin96=CIRTX1, WB enabled */
  234. val = nvt_cr_read(nvt, CR_OUTPUT_PIN_SEL);
  235. val &= OUTPUT_PIN_SEL_MASK;
  236. val |= (OUTPUT_ENABLE_CIR | OUTPUT_ENABLE_CIRWB);
  237. nvt_cr_write(nvt, val, CR_OUTPUT_PIN_SEL);
  238. /* Select CIR logical device and enable */
  239. nvt_select_logical_dev(nvt, LOGICAL_DEV_CIR);
  240. nvt_cr_write(nvt, LOGICAL_DEV_ENABLE, CR_LOGICAL_DEV_EN);
  241. nvt_cr_write(nvt, nvt->cir_addr >> 8, CR_CIR_BASE_ADDR_HI);
  242. nvt_cr_write(nvt, nvt->cir_addr & 0xff, CR_CIR_BASE_ADDR_LO);
  243. nvt_cr_write(nvt, nvt->cir_irq, CR_CIR_IRQ_RSRC);
  244. nvt_dbg("CIR initialized, base io port address: 0x%lx, irq: %d",
  245. nvt->cir_addr, nvt->cir_irq);
  246. }
  247. static void nvt_cir_wake_ldev_init(struct nvt_dev *nvt)
  248. {
  249. /* Select ACPI logical device, enable it and CIR Wake */
  250. nvt_select_logical_dev(nvt, LOGICAL_DEV_ACPI);
  251. nvt_cr_write(nvt, LOGICAL_DEV_ENABLE, CR_LOGICAL_DEV_EN);
  252. /* Enable CIR Wake via PSOUT# (Pin60) */
  253. nvt_set_reg_bit(nvt, CIR_WAKE_ENABLE_BIT, CR_ACPI_CIR_WAKE);
  254. /* enable cir interrupt of mouse/keyboard IRQ event */
  255. nvt_set_reg_bit(nvt, CIR_INTR_MOUSE_IRQ_BIT, CR_ACPI_IRQ_EVENTS);
  256. /* enable pme interrupt of cir wakeup event */
  257. nvt_set_reg_bit(nvt, PME_INTR_CIR_PASS_BIT, CR_ACPI_IRQ_EVENTS2);
  258. /* Select CIR Wake logical device and enable */
  259. nvt_select_logical_dev(nvt, LOGICAL_DEV_CIR_WAKE);
  260. nvt_cr_write(nvt, LOGICAL_DEV_ENABLE, CR_LOGICAL_DEV_EN);
  261. nvt_cr_write(nvt, nvt->cir_wake_addr >> 8, CR_CIR_BASE_ADDR_HI);
  262. nvt_cr_write(nvt, nvt->cir_wake_addr & 0xff, CR_CIR_BASE_ADDR_LO);
  263. nvt_cr_write(nvt, nvt->cir_wake_irq, CR_CIR_IRQ_RSRC);
  264. nvt_dbg("CIR Wake initialized, base io port address: 0x%lx, irq: %d",
  265. nvt->cir_wake_addr, nvt->cir_wake_irq);
  266. }
  267. /* clear out the hardware's cir rx fifo */
  268. static void nvt_clear_cir_fifo(struct nvt_dev *nvt)
  269. {
  270. u8 val;
  271. val = nvt_cir_reg_read(nvt, CIR_FIFOCON);
  272. nvt_cir_reg_write(nvt, val | CIR_FIFOCON_RXFIFOCLR, CIR_FIFOCON);
  273. }
  274. /* clear out the hardware's cir wake rx fifo */
  275. static void nvt_clear_cir_wake_fifo(struct nvt_dev *nvt)
  276. {
  277. u8 val;
  278. val = nvt_cir_wake_reg_read(nvt, CIR_WAKE_FIFOCON);
  279. nvt_cir_wake_reg_write(nvt, val | CIR_WAKE_FIFOCON_RXFIFOCLR,
  280. CIR_WAKE_FIFOCON);
  281. }
  282. /* clear out the hardware's cir tx fifo */
  283. static void nvt_clear_tx_fifo(struct nvt_dev *nvt)
  284. {
  285. u8 val;
  286. val = nvt_cir_reg_read(nvt, CIR_FIFOCON);
  287. nvt_cir_reg_write(nvt, val | CIR_FIFOCON_TXFIFOCLR, CIR_FIFOCON);
  288. }
  289. /* enable RX Trigger Level Reach and Packet End interrupts */
  290. static void nvt_set_cir_iren(struct nvt_dev *nvt)
  291. {
  292. u8 iren;
  293. iren = CIR_IREN_RTR | CIR_IREN_PE;
  294. nvt_cir_reg_write(nvt, iren, CIR_IREN);
  295. }
  296. static void nvt_cir_regs_init(struct nvt_dev *nvt)
  297. {
  298. /* set sample limit count (PE interrupt raised when reached) */
  299. nvt_cir_reg_write(nvt, CIR_RX_LIMIT_COUNT >> 8, CIR_SLCH);
  300. nvt_cir_reg_write(nvt, CIR_RX_LIMIT_COUNT & 0xff, CIR_SLCL);
  301. /* set fifo irq trigger levels */
  302. nvt_cir_reg_write(nvt, CIR_FIFOCON_TX_TRIGGER_LEV |
  303. CIR_FIFOCON_RX_TRIGGER_LEV, CIR_FIFOCON);
  304. /*
  305. * Enable TX and RX, specify carrier on = low, off = high, and set
  306. * sample period (currently 50us)
  307. */
  308. nvt_cir_reg_write(nvt,
  309. CIR_IRCON_TXEN | CIR_IRCON_RXEN |
  310. CIR_IRCON_RXINV | CIR_IRCON_SAMPLE_PERIOD_SEL,
  311. CIR_IRCON);
  312. /* clear hardware rx and tx fifos */
  313. nvt_clear_cir_fifo(nvt);
  314. nvt_clear_tx_fifo(nvt);
  315. /* clear any and all stray interrupts */
  316. nvt_cir_reg_write(nvt, 0xff, CIR_IRSTS);
  317. /* and finally, enable interrupts */
  318. nvt_set_cir_iren(nvt);
  319. }
  320. static void nvt_cir_wake_regs_init(struct nvt_dev *nvt)
  321. {
  322. /* set number of bytes needed for wake from s3 (default 65) */
  323. nvt_cir_wake_reg_write(nvt, CIR_WAKE_FIFO_CMP_BYTES,
  324. CIR_WAKE_FIFO_CMP_DEEP);
  325. /* set tolerance/variance allowed per byte during wake compare */
  326. nvt_cir_wake_reg_write(nvt, CIR_WAKE_CMP_TOLERANCE,
  327. CIR_WAKE_FIFO_CMP_TOL);
  328. /* set sample limit count (PE interrupt raised when reached) */
  329. nvt_cir_wake_reg_write(nvt, CIR_RX_LIMIT_COUNT >> 8, CIR_WAKE_SLCH);
  330. nvt_cir_wake_reg_write(nvt, CIR_RX_LIMIT_COUNT & 0xff, CIR_WAKE_SLCL);
  331. /* set cir wake fifo rx trigger level (currently 67) */
  332. nvt_cir_wake_reg_write(nvt, CIR_WAKE_FIFOCON_RX_TRIGGER_LEV,
  333. CIR_WAKE_FIFOCON);
  334. /*
  335. * Enable TX and RX, specific carrier on = low, off = high, and set
  336. * sample period (currently 50us)
  337. */
  338. nvt_cir_wake_reg_write(nvt, CIR_WAKE_IRCON_MODE0 | CIR_WAKE_IRCON_RXEN |
  339. CIR_WAKE_IRCON_R | CIR_WAKE_IRCON_RXINV |
  340. CIR_WAKE_IRCON_SAMPLE_PERIOD_SEL,
  341. CIR_WAKE_IRCON);
  342. /* clear cir wake rx fifo */
  343. nvt_clear_cir_wake_fifo(nvt);
  344. /* clear any and all stray interrupts */
  345. nvt_cir_wake_reg_write(nvt, 0xff, CIR_WAKE_IRSTS);
  346. }
  347. static void nvt_enable_wake(struct nvt_dev *nvt)
  348. {
  349. nvt_efm_enable(nvt);
  350. nvt_select_logical_dev(nvt, LOGICAL_DEV_ACPI);
  351. nvt_set_reg_bit(nvt, CIR_WAKE_ENABLE_BIT, CR_ACPI_CIR_WAKE);
  352. nvt_set_reg_bit(nvt, CIR_INTR_MOUSE_IRQ_BIT, CR_ACPI_IRQ_EVENTS);
  353. nvt_set_reg_bit(nvt, PME_INTR_CIR_PASS_BIT, CR_ACPI_IRQ_EVENTS2);
  354. nvt_select_logical_dev(nvt, LOGICAL_DEV_CIR_WAKE);
  355. nvt_cr_write(nvt, LOGICAL_DEV_ENABLE, CR_LOGICAL_DEV_EN);
  356. nvt_efm_disable(nvt);
  357. nvt_cir_wake_reg_write(nvt, CIR_WAKE_IRCON_MODE0 | CIR_WAKE_IRCON_RXEN |
  358. CIR_WAKE_IRCON_R | CIR_WAKE_IRCON_RXINV |
  359. CIR_WAKE_IRCON_SAMPLE_PERIOD_SEL,
  360. CIR_WAKE_IRCON);
  361. nvt_cir_wake_reg_write(nvt, 0xff, CIR_WAKE_IRSTS);
  362. nvt_cir_wake_reg_write(nvt, 0, CIR_WAKE_IREN);
  363. }
  364. /* rx carrier detect only works in learning mode, must be called w/nvt_lock */
  365. static u32 nvt_rx_carrier_detect(struct nvt_dev *nvt)
  366. {
  367. u32 count, carrier, duration = 0;
  368. int i;
  369. count = nvt_cir_reg_read(nvt, CIR_FCCL) |
  370. nvt_cir_reg_read(nvt, CIR_FCCH) << 8;
  371. for (i = 0; i < nvt->pkts; i++) {
  372. if (nvt->buf[i] & BUF_PULSE_BIT)
  373. duration += nvt->buf[i] & BUF_LEN_MASK;
  374. }
  375. duration *= SAMPLE_PERIOD;
  376. if (!count || !duration) {
  377. nvt_pr(KERN_NOTICE, "Unable to determine carrier! (c:%u, d:%u)",
  378. count, duration);
  379. return 0;
  380. }
  381. carrier = MS_TO_NS(count) / duration;
  382. if ((carrier > MAX_CARRIER) || (carrier < MIN_CARRIER))
  383. nvt_dbg("WTF? Carrier frequency out of range!");
  384. nvt_dbg("Carrier frequency: %u (count %u, duration %u)",
  385. carrier, count, duration);
  386. return carrier;
  387. }
  388. /*
  389. * set carrier frequency
  390. *
  391. * set carrier on 2 registers: CP & CC
  392. * always set CP as 0x81
  393. * set CC by SPEC, CC = 3MHz/carrier - 1
  394. */
  395. static int nvt_set_tx_carrier(struct rc_dev *dev, u32 carrier)
  396. {
  397. struct nvt_dev *nvt = dev->priv;
  398. u16 val;
  399. nvt_cir_reg_write(nvt, 1, CIR_CP);
  400. val = 3000000 / (carrier) - 1;
  401. nvt_cir_reg_write(nvt, val & 0xff, CIR_CC);
  402. nvt_dbg("cp: 0x%x cc: 0x%x\n",
  403. nvt_cir_reg_read(nvt, CIR_CP), nvt_cir_reg_read(nvt, CIR_CC));
  404. return 0;
  405. }
  406. /*
  407. * nvt_tx_ir
  408. *
  409. * 1) clean TX fifo first (handled by AP)
  410. * 2) copy data from user space
  411. * 3) disable RX interrupts, enable TX interrupts: TTR & TFU
  412. * 4) send 9 packets to TX FIFO to open TTR
  413. * in interrupt_handler:
  414. * 5) send all data out
  415. * go back to write():
  416. * 6) disable TX interrupts, re-enable RX interupts
  417. *
  418. * The key problem of this function is user space data may larger than
  419. * driver's data buf length. So nvt_tx_ir() will only copy TX_BUF_LEN data to
  420. * buf, and keep current copied data buf num in cur_buf_num. But driver's buf
  421. * number may larger than TXFCONT (0xff). So in interrupt_handler, it has to
  422. * set TXFCONT as 0xff, until buf_count less than 0xff.
  423. */
  424. static int nvt_tx_ir(struct rc_dev *dev, int *txbuf, u32 n)
  425. {
  426. struct nvt_dev *nvt = dev->priv;
  427. unsigned long flags;
  428. size_t cur_count;
  429. unsigned int i;
  430. u8 iren;
  431. int ret;
  432. spin_lock_irqsave(&nvt->tx.lock, flags);
  433. if (n >= TX_BUF_LEN) {
  434. nvt->tx.buf_count = cur_count = TX_BUF_LEN;
  435. ret = TX_BUF_LEN;
  436. } else {
  437. nvt->tx.buf_count = cur_count = n;
  438. ret = n;
  439. }
  440. memcpy(nvt->tx.buf, txbuf, nvt->tx.buf_count);
  441. nvt->tx.cur_buf_num = 0;
  442. /* save currently enabled interrupts */
  443. iren = nvt_cir_reg_read(nvt, CIR_IREN);
  444. /* now disable all interrupts, save TFU & TTR */
  445. nvt_cir_reg_write(nvt, CIR_IREN_TFU | CIR_IREN_TTR, CIR_IREN);
  446. nvt->tx.tx_state = ST_TX_REPLY;
  447. nvt_cir_reg_write(nvt, CIR_FIFOCON_TX_TRIGGER_LEV_8 |
  448. CIR_FIFOCON_RXFIFOCLR, CIR_FIFOCON);
  449. /* trigger TTR interrupt by writing out ones, (yes, it's ugly) */
  450. for (i = 0; i < 9; i++)
  451. nvt_cir_reg_write(nvt, 0x01, CIR_STXFIFO);
  452. spin_unlock_irqrestore(&nvt->tx.lock, flags);
  453. wait_event(nvt->tx.queue, nvt->tx.tx_state == ST_TX_REQUEST);
  454. spin_lock_irqsave(&nvt->tx.lock, flags);
  455. nvt->tx.tx_state = ST_TX_NONE;
  456. spin_unlock_irqrestore(&nvt->tx.lock, flags);
  457. /* restore enabled interrupts to prior state */
  458. nvt_cir_reg_write(nvt, iren, CIR_IREN);
  459. return ret;
  460. }
  461. /* dump contents of the last rx buffer we got from the hw rx fifo */
  462. static void nvt_dump_rx_buf(struct nvt_dev *nvt)
  463. {
  464. int i;
  465. printk(KERN_DEBUG "%s (len %d): ", __func__, nvt->pkts);
  466. for (i = 0; (i < nvt->pkts) && (i < RX_BUF_LEN); i++)
  467. printk(KERN_CONT "0x%02x ", nvt->buf[i]);
  468. printk(KERN_CONT "\n");
  469. }
  470. /*
  471. * Process raw data in rx driver buffer, store it in raw IR event kfifo,
  472. * trigger decode when appropriate.
  473. *
  474. * We get IR data samples one byte at a time. If the msb is set, its a pulse,
  475. * otherwise its a space. The lower 7 bits are the count of SAMPLE_PERIOD
  476. * (default 50us) intervals for that pulse/space. A discrete signal is
  477. * followed by a series of 0x7f packets, then either 0x7<something> or 0x80
  478. * to signal more IR coming (repeats) or end of IR, respectively. We store
  479. * sample data in the raw event kfifo until we see 0x7<something> (except f)
  480. * or 0x80, at which time, we trigger a decode operation.
  481. */
  482. static void nvt_process_rx_ir_data(struct nvt_dev *nvt)
  483. {
  484. DEFINE_IR_RAW_EVENT(rawir);
  485. unsigned int count;
  486. u32 carrier;
  487. u8 sample;
  488. int i;
  489. nvt_dbg_verbose("%s firing", __func__);
  490. if (debug)
  491. nvt_dump_rx_buf(nvt);
  492. if (nvt->carrier_detect_enabled)
  493. carrier = nvt_rx_carrier_detect(nvt);
  494. count = nvt->pkts;
  495. nvt_dbg_verbose("Processing buffer of len %d", count);
  496. init_ir_raw_event(&rawir);
  497. for (i = 0; i < count; i++) {
  498. nvt->pkts--;
  499. sample = nvt->buf[i];
  500. rawir.pulse = ((sample & BUF_PULSE_BIT) != 0);
  501. rawir.duration = US_TO_NS((sample & BUF_LEN_MASK)
  502. * SAMPLE_PERIOD);
  503. if ((sample & BUF_LEN_MASK) == BUF_LEN_MASK) {
  504. if (nvt->rawir.pulse == rawir.pulse)
  505. nvt->rawir.duration += rawir.duration;
  506. else {
  507. nvt->rawir.duration = rawir.duration;
  508. nvt->rawir.pulse = rawir.pulse;
  509. }
  510. continue;
  511. }
  512. rawir.duration += nvt->rawir.duration;
  513. init_ir_raw_event(&nvt->rawir);
  514. nvt->rawir.duration = 0;
  515. nvt->rawir.pulse = rawir.pulse;
  516. if (sample == BUF_PULSE_BIT)
  517. rawir.pulse = false;
  518. if (rawir.duration) {
  519. nvt_dbg("Storing %s with duration %d",
  520. rawir.pulse ? "pulse" : "space",
  521. rawir.duration);
  522. ir_raw_event_store(nvt->rdev, &rawir);
  523. }
  524. /*
  525. * BUF_PULSE_BIT indicates end of IR data, BUF_REPEAT_BYTE
  526. * indicates end of IR signal, but new data incoming. In both
  527. * cases, it means we're ready to call ir_raw_event_handle
  528. */
  529. if ((sample == BUF_PULSE_BIT) && nvt->pkts) {
  530. nvt_dbg("Calling ir_raw_event_handle (signal end)\n");
  531. ir_raw_event_handle(nvt->rdev);
  532. }
  533. }
  534. nvt_dbg("Calling ir_raw_event_handle (buffer empty)\n");
  535. ir_raw_event_handle(nvt->rdev);
  536. if (nvt->pkts) {
  537. nvt_dbg("Odd, pkts should be 0 now... (its %u)", nvt->pkts);
  538. nvt->pkts = 0;
  539. }
  540. nvt_dbg_verbose("%s done", __func__);
  541. }
  542. static void nvt_handle_rx_fifo_overrun(struct nvt_dev *nvt)
  543. {
  544. nvt_pr(KERN_WARNING, "RX FIFO overrun detected, flushing data!");
  545. nvt->pkts = 0;
  546. nvt_clear_cir_fifo(nvt);
  547. ir_raw_event_reset(nvt->rdev);
  548. }
  549. /* copy data from hardware rx fifo into driver buffer */
  550. static void nvt_get_rx_ir_data(struct nvt_dev *nvt)
  551. {
  552. unsigned long flags;
  553. u8 fifocount, val;
  554. unsigned int b_idx;
  555. bool overrun = false;
  556. int i;
  557. /* Get count of how many bytes to read from RX FIFO */
  558. fifocount = nvt_cir_reg_read(nvt, CIR_RXFCONT);
  559. /* if we get 0xff, probably means the logical dev is disabled */
  560. if (fifocount == 0xff)
  561. return;
  562. /* watch out for a fifo overrun condition */
  563. else if (fifocount > RX_BUF_LEN) {
  564. overrun = true;
  565. fifocount = RX_BUF_LEN;
  566. }
  567. nvt_dbg("attempting to fetch %u bytes from hw rx fifo", fifocount);
  568. spin_lock_irqsave(&nvt->nvt_lock, flags);
  569. b_idx = nvt->pkts;
  570. /* This should never happen, but lets check anyway... */
  571. if (b_idx + fifocount > RX_BUF_LEN) {
  572. nvt_process_rx_ir_data(nvt);
  573. b_idx = 0;
  574. }
  575. /* Read fifocount bytes from CIR Sample RX FIFO register */
  576. for (i = 0; i < fifocount; i++) {
  577. val = nvt_cir_reg_read(nvt, CIR_SRXFIFO);
  578. nvt->buf[b_idx + i] = val;
  579. }
  580. nvt->pkts += fifocount;
  581. nvt_dbg("%s: pkts now %d", __func__, nvt->pkts);
  582. nvt_process_rx_ir_data(nvt);
  583. if (overrun)
  584. nvt_handle_rx_fifo_overrun(nvt);
  585. spin_unlock_irqrestore(&nvt->nvt_lock, flags);
  586. }
  587. static void nvt_cir_log_irqs(u8 status, u8 iren)
  588. {
  589. nvt_pr(KERN_INFO, "IRQ 0x%02x (IREN 0x%02x) :%s%s%s%s%s%s%s%s%s",
  590. status, iren,
  591. status & CIR_IRSTS_RDR ? " RDR" : "",
  592. status & CIR_IRSTS_RTR ? " RTR" : "",
  593. status & CIR_IRSTS_PE ? " PE" : "",
  594. status & CIR_IRSTS_RFO ? " RFO" : "",
  595. status & CIR_IRSTS_TE ? " TE" : "",
  596. status & CIR_IRSTS_TTR ? " TTR" : "",
  597. status & CIR_IRSTS_TFU ? " TFU" : "",
  598. status & CIR_IRSTS_GH ? " GH" : "",
  599. status & ~(CIR_IRSTS_RDR | CIR_IRSTS_RTR | CIR_IRSTS_PE |
  600. CIR_IRSTS_RFO | CIR_IRSTS_TE | CIR_IRSTS_TTR |
  601. CIR_IRSTS_TFU | CIR_IRSTS_GH) ? " ?" : "");
  602. }
  603. static bool nvt_cir_tx_inactive(struct nvt_dev *nvt)
  604. {
  605. unsigned long flags;
  606. bool tx_inactive;
  607. u8 tx_state;
  608. spin_lock_irqsave(&nvt->tx.lock, flags);
  609. tx_state = nvt->tx.tx_state;
  610. spin_unlock_irqrestore(&nvt->tx.lock, flags);
  611. tx_inactive = (tx_state == ST_TX_NONE);
  612. return tx_inactive;
  613. }
  614. /* interrupt service routine for incoming and outgoing CIR data */
  615. static irqreturn_t nvt_cir_isr(int irq, void *data)
  616. {
  617. struct nvt_dev *nvt = data;
  618. u8 status, iren, cur_state;
  619. unsigned long flags;
  620. nvt_dbg_verbose("%s firing", __func__);
  621. nvt_efm_enable(nvt);
  622. nvt_select_logical_dev(nvt, LOGICAL_DEV_CIR);
  623. nvt_efm_disable(nvt);
  624. /*
  625. * Get IR Status register contents. Write 1 to ack/clear
  626. *
  627. * bit: reg name - description
  628. * 7: CIR_IRSTS_RDR - RX Data Ready
  629. * 6: CIR_IRSTS_RTR - RX FIFO Trigger Level Reach
  630. * 5: CIR_IRSTS_PE - Packet End
  631. * 4: CIR_IRSTS_RFO - RX FIFO Overrun (RDR will also be set)
  632. * 3: CIR_IRSTS_TE - TX FIFO Empty
  633. * 2: CIR_IRSTS_TTR - TX FIFO Trigger Level Reach
  634. * 1: CIR_IRSTS_TFU - TX FIFO Underrun
  635. * 0: CIR_IRSTS_GH - Min Length Detected
  636. */
  637. status = nvt_cir_reg_read(nvt, CIR_IRSTS);
  638. if (!status) {
  639. nvt_dbg_verbose("%s exiting, IRSTS 0x0", __func__);
  640. nvt_cir_reg_write(nvt, 0xff, CIR_IRSTS);
  641. return IRQ_RETVAL(IRQ_NONE);
  642. }
  643. /* ack/clear all irq flags we've got */
  644. nvt_cir_reg_write(nvt, status, CIR_IRSTS);
  645. nvt_cir_reg_write(nvt, 0, CIR_IRSTS);
  646. /* Interrupt may be shared with CIR Wake, bail if CIR not enabled */
  647. iren = nvt_cir_reg_read(nvt, CIR_IREN);
  648. if (!iren) {
  649. nvt_dbg_verbose("%s exiting, CIR not enabled", __func__);
  650. return IRQ_RETVAL(IRQ_NONE);
  651. }
  652. if (debug)
  653. nvt_cir_log_irqs(status, iren);
  654. if (status & CIR_IRSTS_RTR) {
  655. /* FIXME: add code for study/learn mode */
  656. /* We only do rx if not tx'ing */
  657. if (nvt_cir_tx_inactive(nvt))
  658. nvt_get_rx_ir_data(nvt);
  659. }
  660. if (status & CIR_IRSTS_PE) {
  661. if (nvt_cir_tx_inactive(nvt))
  662. nvt_get_rx_ir_data(nvt);
  663. spin_lock_irqsave(&nvt->nvt_lock, flags);
  664. cur_state = nvt->study_state;
  665. spin_unlock_irqrestore(&nvt->nvt_lock, flags);
  666. if (cur_state == ST_STUDY_NONE)
  667. nvt_clear_cir_fifo(nvt);
  668. }
  669. if (status & CIR_IRSTS_TE)
  670. nvt_clear_tx_fifo(nvt);
  671. if (status & CIR_IRSTS_TTR) {
  672. unsigned int pos, count;
  673. u8 tmp;
  674. spin_lock_irqsave(&nvt->tx.lock, flags);
  675. pos = nvt->tx.cur_buf_num;
  676. count = nvt->tx.buf_count;
  677. /* Write data into the hardware tx fifo while pos < count */
  678. if (pos < count) {
  679. nvt_cir_reg_write(nvt, nvt->tx.buf[pos], CIR_STXFIFO);
  680. nvt->tx.cur_buf_num++;
  681. /* Disable TX FIFO Trigger Level Reach (TTR) interrupt */
  682. } else {
  683. tmp = nvt_cir_reg_read(nvt, CIR_IREN);
  684. nvt_cir_reg_write(nvt, tmp & ~CIR_IREN_TTR, CIR_IREN);
  685. }
  686. spin_unlock_irqrestore(&nvt->tx.lock, flags);
  687. }
  688. if (status & CIR_IRSTS_TFU) {
  689. spin_lock_irqsave(&nvt->tx.lock, flags);
  690. if (nvt->tx.tx_state == ST_TX_REPLY) {
  691. nvt->tx.tx_state = ST_TX_REQUEST;
  692. wake_up(&nvt->tx.queue);
  693. }
  694. spin_unlock_irqrestore(&nvt->tx.lock, flags);
  695. }
  696. nvt_dbg_verbose("%s done", __func__);
  697. return IRQ_RETVAL(IRQ_HANDLED);
  698. }
  699. /* Interrupt service routine for CIR Wake */
  700. static irqreturn_t nvt_cir_wake_isr(int irq, void *data)
  701. {
  702. u8 status, iren, val;
  703. struct nvt_dev *nvt = data;
  704. unsigned long flags;
  705. nvt_dbg_wake("%s firing", __func__);
  706. status = nvt_cir_wake_reg_read(nvt, CIR_WAKE_IRSTS);
  707. if (!status)
  708. return IRQ_RETVAL(IRQ_NONE);
  709. if (status & CIR_WAKE_IRSTS_IR_PENDING)
  710. nvt_clear_cir_wake_fifo(nvt);
  711. nvt_cir_wake_reg_write(nvt, status, CIR_WAKE_IRSTS);
  712. nvt_cir_wake_reg_write(nvt, 0, CIR_WAKE_IRSTS);
  713. /* Interrupt may be shared with CIR, bail if Wake not enabled */
  714. iren = nvt_cir_wake_reg_read(nvt, CIR_WAKE_IREN);
  715. if (!iren) {
  716. nvt_dbg_wake("%s exiting, wake not enabled", __func__);
  717. return IRQ_RETVAL(IRQ_HANDLED);
  718. }
  719. if ((status & CIR_WAKE_IRSTS_PE) &&
  720. (nvt->wake_state == ST_WAKE_START)) {
  721. while (nvt_cir_wake_reg_read(nvt, CIR_WAKE_RD_FIFO_ONLY_IDX)) {
  722. val = nvt_cir_wake_reg_read(nvt, CIR_WAKE_RD_FIFO_ONLY);
  723. nvt_dbg("setting wake up key: 0x%x", val);
  724. }
  725. nvt_cir_wake_reg_write(nvt, 0, CIR_WAKE_IREN);
  726. spin_lock_irqsave(&nvt->nvt_lock, flags);
  727. nvt->wake_state = ST_WAKE_FINISH;
  728. spin_unlock_irqrestore(&nvt->nvt_lock, flags);
  729. }
  730. nvt_dbg_wake("%s done", __func__);
  731. return IRQ_RETVAL(IRQ_HANDLED);
  732. }
  733. static void nvt_enable_cir(struct nvt_dev *nvt)
  734. {
  735. /* set function enable flags */
  736. nvt_cir_reg_write(nvt, CIR_IRCON_TXEN | CIR_IRCON_RXEN |
  737. CIR_IRCON_RXINV | CIR_IRCON_SAMPLE_PERIOD_SEL,
  738. CIR_IRCON);
  739. nvt_efm_enable(nvt);
  740. /* enable the CIR logical device */
  741. nvt_select_logical_dev(nvt, LOGICAL_DEV_CIR);
  742. nvt_cr_write(nvt, LOGICAL_DEV_ENABLE, CR_LOGICAL_DEV_EN);
  743. nvt_efm_disable(nvt);
  744. /* clear all pending interrupts */
  745. nvt_cir_reg_write(nvt, 0xff, CIR_IRSTS);
  746. /* enable interrupts */
  747. nvt_set_cir_iren(nvt);
  748. }
  749. static void nvt_disable_cir(struct nvt_dev *nvt)
  750. {
  751. /* disable CIR interrupts */
  752. nvt_cir_reg_write(nvt, 0, CIR_IREN);
  753. /* clear any and all pending interrupts */
  754. nvt_cir_reg_write(nvt, 0xff, CIR_IRSTS);
  755. /* clear all function enable flags */
  756. nvt_cir_reg_write(nvt, 0, CIR_IRCON);
  757. /* clear hardware rx and tx fifos */
  758. nvt_clear_cir_fifo(nvt);
  759. nvt_clear_tx_fifo(nvt);
  760. nvt_efm_enable(nvt);
  761. /* disable the CIR logical device */
  762. nvt_select_logical_dev(nvt, LOGICAL_DEV_CIR);
  763. nvt_cr_write(nvt, LOGICAL_DEV_DISABLE, CR_LOGICAL_DEV_EN);
  764. nvt_efm_disable(nvt);
  765. }
  766. static int nvt_open(struct rc_dev *dev)
  767. {
  768. struct nvt_dev *nvt = dev->priv;
  769. unsigned long flags;
  770. spin_lock_irqsave(&nvt->nvt_lock, flags);
  771. nvt->in_use = true;
  772. nvt_enable_cir(nvt);
  773. spin_unlock_irqrestore(&nvt->nvt_lock, flags);
  774. return 0;
  775. }
  776. static void nvt_close(struct rc_dev *dev)
  777. {
  778. struct nvt_dev *nvt = dev->priv;
  779. unsigned long flags;
  780. spin_lock_irqsave(&nvt->nvt_lock, flags);
  781. nvt->in_use = false;
  782. nvt_disable_cir(nvt);
  783. spin_unlock_irqrestore(&nvt->nvt_lock, flags);
  784. }
  785. /* Allocate memory, probe hardware, and initialize everything */
  786. static int nvt_probe(struct pnp_dev *pdev, const struct pnp_device_id *dev_id)
  787. {
  788. struct nvt_dev *nvt;
  789. struct rc_dev *rdev;
  790. int ret = -ENOMEM;
  791. nvt = kzalloc(sizeof(struct nvt_dev), GFP_KERNEL);
  792. if (!nvt)
  793. return ret;
  794. /* input device for IR remote (and tx) */
  795. rdev = rc_allocate_device();
  796. if (!rdev)
  797. goto failure;
  798. ret = -ENODEV;
  799. /* validate pnp resources */
  800. if (!pnp_port_valid(pdev, 0) ||
  801. pnp_port_len(pdev, 0) < CIR_IOREG_LENGTH) {
  802. dev_err(&pdev->dev, "IR PNP Port not valid!\n");
  803. goto failure;
  804. }
  805. if (!pnp_irq_valid(pdev, 0)) {
  806. dev_err(&pdev->dev, "PNP IRQ not valid!\n");
  807. goto failure;
  808. }
  809. if (!pnp_port_valid(pdev, 1) ||
  810. pnp_port_len(pdev, 1) < CIR_IOREG_LENGTH) {
  811. dev_err(&pdev->dev, "Wake PNP Port not valid!\n");
  812. goto failure;
  813. }
  814. nvt->cir_addr = pnp_port_start(pdev, 0);
  815. nvt->cir_irq = pnp_irq(pdev, 0);
  816. nvt->cir_wake_addr = pnp_port_start(pdev, 1);
  817. /* irq is always shared between cir and cir wake */
  818. nvt->cir_wake_irq = nvt->cir_irq;
  819. nvt->cr_efir = CR_EFIR;
  820. nvt->cr_efdr = CR_EFDR;
  821. spin_lock_init(&nvt->nvt_lock);
  822. spin_lock_init(&nvt->tx.lock);
  823. init_ir_raw_event(&nvt->rawir);
  824. ret = -EBUSY;
  825. /* now claim resources */
  826. if (!request_region(nvt->cir_addr,
  827. CIR_IOREG_LENGTH, NVT_DRIVER_NAME))
  828. goto failure;
  829. if (request_irq(nvt->cir_irq, nvt_cir_isr, IRQF_SHARED,
  830. NVT_DRIVER_NAME, (void *)nvt))
  831. goto failure;
  832. if (!request_region(nvt->cir_wake_addr,
  833. CIR_IOREG_LENGTH, NVT_DRIVER_NAME))
  834. goto failure;
  835. if (request_irq(nvt->cir_wake_irq, nvt_cir_wake_isr, IRQF_SHARED,
  836. NVT_DRIVER_NAME, (void *)nvt))
  837. goto failure;
  838. pnp_set_drvdata(pdev, nvt);
  839. nvt->pdev = pdev;
  840. init_waitqueue_head(&nvt->tx.queue);
  841. ret = nvt_hw_detect(nvt);
  842. if (ret)
  843. goto failure;
  844. /* Initialize CIR & CIR Wake Logical Devices */
  845. nvt_efm_enable(nvt);
  846. nvt_cir_ldev_init(nvt);
  847. nvt_cir_wake_ldev_init(nvt);
  848. nvt_efm_disable(nvt);
  849. /* Initialize CIR & CIR Wake Config Registers */
  850. nvt_cir_regs_init(nvt);
  851. nvt_cir_wake_regs_init(nvt);
  852. /* Set up the rc device */
  853. rdev->priv = nvt;
  854. rdev->driver_type = RC_DRIVER_IR_RAW;
  855. rdev->allowed_protos = RC_TYPE_ALL;
  856. rdev->open = nvt_open;
  857. rdev->close = nvt_close;
  858. rdev->tx_ir = nvt_tx_ir;
  859. rdev->s_tx_carrier = nvt_set_tx_carrier;
  860. rdev->input_name = "Nuvoton w836x7hg Infrared Remote Transceiver";
  861. rdev->input_id.bustype = BUS_HOST;
  862. rdev->input_id.vendor = PCI_VENDOR_ID_WINBOND2;
  863. rdev->input_id.product = nvt->chip_major;
  864. rdev->input_id.version = nvt->chip_minor;
  865. rdev->driver_name = NVT_DRIVER_NAME;
  866. rdev->map_name = RC_MAP_RC6_MCE;
  867. #if 0
  868. rdev->min_timeout = XYZ;
  869. rdev->max_timeout = XYZ;
  870. rdev->timeout = XYZ;
  871. /* rx resolution is hardwired to 50us atm, 1, 25, 100 also possible */
  872. rdev->rx_resolution = XYZ;
  873. /* tx bits */
  874. rdev->tx_resolution = XYZ;
  875. #endif
  876. ret = rc_register_device(rdev);
  877. if (ret)
  878. goto failure;
  879. device_set_wakeup_capable(&pdev->dev, 1);
  880. device_set_wakeup_enable(&pdev->dev, 1);
  881. nvt->rdev = rdev;
  882. nvt_pr(KERN_NOTICE, "driver has been successfully loaded\n");
  883. if (debug) {
  884. cir_dump_regs(nvt);
  885. cir_wake_dump_regs(nvt);
  886. }
  887. return 0;
  888. failure:
  889. if (nvt->cir_irq)
  890. free_irq(nvt->cir_irq, nvt);
  891. if (nvt->cir_addr)
  892. release_region(nvt->cir_addr, CIR_IOREG_LENGTH);
  893. if (nvt->cir_wake_irq)
  894. free_irq(nvt->cir_wake_irq, nvt);
  895. if (nvt->cir_wake_addr)
  896. release_region(nvt->cir_wake_addr, CIR_IOREG_LENGTH);
  897. rc_free_device(rdev);
  898. kfree(nvt);
  899. return ret;
  900. }
  901. static void __devexit nvt_remove(struct pnp_dev *pdev)
  902. {
  903. struct nvt_dev *nvt = pnp_get_drvdata(pdev);
  904. unsigned long flags;
  905. spin_lock_irqsave(&nvt->nvt_lock, flags);
  906. /* disable CIR */
  907. nvt_cir_reg_write(nvt, 0, CIR_IREN);
  908. nvt_disable_cir(nvt);
  909. /* enable CIR Wake (for IR power-on) */
  910. nvt_enable_wake(nvt);
  911. spin_unlock_irqrestore(&nvt->nvt_lock, flags);
  912. /* free resources */
  913. free_irq(nvt->cir_irq, nvt);
  914. free_irq(nvt->cir_wake_irq, nvt);
  915. release_region(nvt->cir_addr, CIR_IOREG_LENGTH);
  916. release_region(nvt->cir_wake_addr, CIR_IOREG_LENGTH);
  917. rc_unregister_device(nvt->rdev);
  918. kfree(nvt);
  919. }
  920. static int nvt_suspend(struct pnp_dev *pdev, pm_message_t state)
  921. {
  922. struct nvt_dev *nvt = pnp_get_drvdata(pdev);
  923. unsigned long flags;
  924. nvt_dbg("%s called", __func__);
  925. /* zero out misc state tracking */
  926. spin_lock_irqsave(&nvt->nvt_lock, flags);
  927. nvt->study_state = ST_STUDY_NONE;
  928. nvt->wake_state = ST_WAKE_NONE;
  929. spin_unlock_irqrestore(&nvt->nvt_lock, flags);
  930. spin_lock_irqsave(&nvt->tx.lock, flags);
  931. nvt->tx.tx_state = ST_TX_NONE;
  932. spin_unlock_irqrestore(&nvt->tx.lock, flags);
  933. /* disable all CIR interrupts */
  934. nvt_cir_reg_write(nvt, 0, CIR_IREN);
  935. nvt_efm_enable(nvt);
  936. /* disable cir logical dev */
  937. nvt_select_logical_dev(nvt, LOGICAL_DEV_CIR);
  938. nvt_cr_write(nvt, LOGICAL_DEV_DISABLE, CR_LOGICAL_DEV_EN);
  939. nvt_efm_disable(nvt);
  940. /* make sure wake is enabled */
  941. nvt_enable_wake(nvt);
  942. return 0;
  943. }
  944. static int nvt_resume(struct pnp_dev *pdev)
  945. {
  946. int ret = 0;
  947. struct nvt_dev *nvt = pnp_get_drvdata(pdev);
  948. nvt_dbg("%s called", __func__);
  949. /* open interrupt */
  950. nvt_set_cir_iren(nvt);
  951. /* Enable CIR logical device */
  952. nvt_efm_enable(nvt);
  953. nvt_select_logical_dev(nvt, LOGICAL_DEV_CIR);
  954. nvt_cr_write(nvt, LOGICAL_DEV_ENABLE, CR_LOGICAL_DEV_EN);
  955. nvt_efm_disable(nvt);
  956. nvt_cir_regs_init(nvt);
  957. nvt_cir_wake_regs_init(nvt);
  958. return ret;
  959. }
  960. static void nvt_shutdown(struct pnp_dev *pdev)
  961. {
  962. struct nvt_dev *nvt = pnp_get_drvdata(pdev);
  963. nvt_enable_wake(nvt);
  964. }
  965. static const struct pnp_device_id nvt_ids[] = {
  966. { "WEC0530", 0 }, /* CIR */
  967. { "NTN0530", 0 }, /* CIR for new chip's pnp id*/
  968. { "", 0 },
  969. };
  970. static struct pnp_driver nvt_driver = {
  971. .name = NVT_DRIVER_NAME,
  972. .id_table = nvt_ids,
  973. .flags = PNP_DRIVER_RES_DO_NOT_CHANGE,
  974. .probe = nvt_probe,
  975. .remove = __devexit_p(nvt_remove),
  976. .suspend = nvt_suspend,
  977. .resume = nvt_resume,
  978. .shutdown = nvt_shutdown,
  979. };
  980. int nvt_init(void)
  981. {
  982. return pnp_register_driver(&nvt_driver);
  983. }
  984. void nvt_exit(void)
  985. {
  986. pnp_unregister_driver(&nvt_driver);
  987. }
  988. module_param(debug, int, S_IRUGO | S_IWUSR);
  989. MODULE_PARM_DESC(debug, "Enable debugging output");
  990. MODULE_DEVICE_TABLE(pnp, nvt_ids);
  991. MODULE_DESCRIPTION("Nuvoton W83667HG-A & W83677HG-I CIR driver");
  992. MODULE_AUTHOR("Jarod Wilson <jarod@redhat.com>");
  993. MODULE_LICENSE("GPL");
  994. module_init(nvt_init);
  995. module_exit(nvt_exit);