nuvoton-cir.c 34 KB

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