nuvoton-cir.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  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. ret = -ENODEV;
  220. nvt_efm_disable(nvt);
  221. spin_lock_irqsave(&nvt->nvt_lock, flags);
  222. nvt->chip_major = chip_major;
  223. nvt->chip_minor = chip_minor;
  224. spin_unlock_irqrestore(&nvt->nvt_lock, flags);
  225. return ret;
  226. }
  227. static void nvt_cir_ldev_init(struct nvt_dev *nvt)
  228. {
  229. u8 val;
  230. /* output pin selection (Pin95=CIRRX, Pin96=CIRTX1, WB enabled */
  231. val = nvt_cr_read(nvt, CR_OUTPUT_PIN_SEL);
  232. val &= OUTPUT_PIN_SEL_MASK;
  233. val |= (OUTPUT_ENABLE_CIR | OUTPUT_ENABLE_CIRWB);
  234. nvt_cr_write(nvt, val, CR_OUTPUT_PIN_SEL);
  235. /* Select CIR logical device and enable */
  236. nvt_select_logical_dev(nvt, LOGICAL_DEV_CIR);
  237. nvt_cr_write(nvt, LOGICAL_DEV_ENABLE, CR_LOGICAL_DEV_EN);
  238. nvt_cr_write(nvt, nvt->cir_addr >> 8, CR_CIR_BASE_ADDR_HI);
  239. nvt_cr_write(nvt, nvt->cir_addr & 0xff, CR_CIR_BASE_ADDR_LO);
  240. nvt_cr_write(nvt, nvt->cir_irq, CR_CIR_IRQ_RSRC);
  241. nvt_dbg("CIR initialized, base io port address: 0x%lx, irq: %d",
  242. nvt->cir_addr, nvt->cir_irq);
  243. }
  244. static void nvt_cir_wake_ldev_init(struct nvt_dev *nvt)
  245. {
  246. /* Select ACPI logical device, enable it and CIR Wake */
  247. nvt_select_logical_dev(nvt, LOGICAL_DEV_ACPI);
  248. nvt_cr_write(nvt, LOGICAL_DEV_ENABLE, CR_LOGICAL_DEV_EN);
  249. /* Enable CIR Wake via PSOUT# (Pin60) */
  250. nvt_set_reg_bit(nvt, CIR_WAKE_ENABLE_BIT, CR_ACPI_CIR_WAKE);
  251. /* enable cir interrupt of mouse/keyboard IRQ event */
  252. nvt_set_reg_bit(nvt, CIR_INTR_MOUSE_IRQ_BIT, CR_ACPI_IRQ_EVENTS);
  253. /* enable pme interrupt of cir wakeup event */
  254. nvt_set_reg_bit(nvt, PME_INTR_CIR_PASS_BIT, CR_ACPI_IRQ_EVENTS2);
  255. /* Select CIR Wake logical device and enable */
  256. nvt_select_logical_dev(nvt, LOGICAL_DEV_CIR_WAKE);
  257. nvt_cr_write(nvt, LOGICAL_DEV_ENABLE, CR_LOGICAL_DEV_EN);
  258. nvt_cr_write(nvt, nvt->cir_wake_addr >> 8, CR_CIR_BASE_ADDR_HI);
  259. nvt_cr_write(nvt, nvt->cir_wake_addr & 0xff, CR_CIR_BASE_ADDR_LO);
  260. nvt_cr_write(nvt, nvt->cir_wake_irq, CR_CIR_IRQ_RSRC);
  261. nvt_dbg("CIR Wake initialized, base io port address: 0x%lx, irq: %d",
  262. nvt->cir_wake_addr, nvt->cir_wake_irq);
  263. }
  264. /* clear out the hardware's cir rx fifo */
  265. static void nvt_clear_cir_fifo(struct nvt_dev *nvt)
  266. {
  267. u8 val;
  268. val = nvt_cir_reg_read(nvt, CIR_FIFOCON);
  269. nvt_cir_reg_write(nvt, val | CIR_FIFOCON_RXFIFOCLR, CIR_FIFOCON);
  270. }
  271. /* clear out the hardware's cir wake rx fifo */
  272. static void nvt_clear_cir_wake_fifo(struct nvt_dev *nvt)
  273. {
  274. u8 val;
  275. val = nvt_cir_wake_reg_read(nvt, CIR_WAKE_FIFOCON);
  276. nvt_cir_wake_reg_write(nvt, val | CIR_WAKE_FIFOCON_RXFIFOCLR,
  277. CIR_WAKE_FIFOCON);
  278. }
  279. /* clear out the hardware's cir tx fifo */
  280. static void nvt_clear_tx_fifo(struct nvt_dev *nvt)
  281. {
  282. u8 val;
  283. val = nvt_cir_reg_read(nvt, CIR_FIFOCON);
  284. nvt_cir_reg_write(nvt, val | CIR_FIFOCON_TXFIFOCLR, CIR_FIFOCON);
  285. }
  286. /* enable RX Trigger Level Reach and Packet End interrupts */
  287. static void nvt_set_cir_iren(struct nvt_dev *nvt)
  288. {
  289. u8 iren;
  290. iren = CIR_IREN_RTR | CIR_IREN_PE;
  291. nvt_cir_reg_write(nvt, iren, CIR_IREN);
  292. }
  293. static void nvt_cir_regs_init(struct nvt_dev *nvt)
  294. {
  295. /* set sample limit count (PE interrupt raised when reached) */
  296. nvt_cir_reg_write(nvt, CIR_RX_LIMIT_COUNT >> 8, CIR_SLCH);
  297. nvt_cir_reg_write(nvt, CIR_RX_LIMIT_COUNT & 0xff, CIR_SLCL);
  298. /* set fifo irq trigger levels */
  299. nvt_cir_reg_write(nvt, CIR_FIFOCON_TX_TRIGGER_LEV |
  300. CIR_FIFOCON_RX_TRIGGER_LEV, CIR_FIFOCON);
  301. /*
  302. * Enable TX and RX, specify carrier on = low, off = high, and set
  303. * sample period (currently 50us)
  304. */
  305. nvt_cir_reg_write(nvt,
  306. CIR_IRCON_TXEN | CIR_IRCON_RXEN |
  307. CIR_IRCON_RXINV | CIR_IRCON_SAMPLE_PERIOD_SEL,
  308. CIR_IRCON);
  309. /* clear hardware rx and tx fifos */
  310. nvt_clear_cir_fifo(nvt);
  311. nvt_clear_tx_fifo(nvt);
  312. /* clear any and all stray interrupts */
  313. nvt_cir_reg_write(nvt, 0xff, CIR_IRSTS);
  314. /* and finally, enable interrupts */
  315. nvt_set_cir_iren(nvt);
  316. }
  317. static void nvt_cir_wake_regs_init(struct nvt_dev *nvt)
  318. {
  319. /* set number of bytes needed for wake key comparison (default 67) */
  320. nvt_cir_wake_reg_write(nvt, CIR_WAKE_FIFO_LEN, CIR_WAKE_FIFO_CMP_DEEP);
  321. /* set tolerance/variance allowed per byte during wake compare */
  322. nvt_cir_wake_reg_write(nvt, CIR_WAKE_CMP_TOLERANCE,
  323. CIR_WAKE_FIFO_CMP_TOL);
  324. /* set sample limit count (PE interrupt raised when reached) */
  325. nvt_cir_wake_reg_write(nvt, CIR_RX_LIMIT_COUNT >> 8, CIR_WAKE_SLCH);
  326. nvt_cir_wake_reg_write(nvt, CIR_RX_LIMIT_COUNT & 0xff, CIR_WAKE_SLCL);
  327. /* set cir wake fifo rx trigger level (currently 67) */
  328. nvt_cir_wake_reg_write(nvt, CIR_WAKE_FIFOCON_RX_TRIGGER_LEV,
  329. CIR_WAKE_FIFOCON);
  330. /*
  331. * Enable TX and RX, specific carrier on = low, off = high, and set
  332. * sample period (currently 50us)
  333. */
  334. nvt_cir_wake_reg_write(nvt, CIR_WAKE_IRCON_MODE0 | CIR_WAKE_IRCON_RXEN |
  335. CIR_WAKE_IRCON_R | CIR_WAKE_IRCON_RXINV |
  336. CIR_WAKE_IRCON_SAMPLE_PERIOD_SEL,
  337. CIR_WAKE_IRCON);
  338. /* clear cir wake rx fifo */
  339. nvt_clear_cir_wake_fifo(nvt);
  340. /* clear any and all stray interrupts */
  341. nvt_cir_wake_reg_write(nvt, 0xff, CIR_WAKE_IRSTS);
  342. }
  343. static void nvt_enable_wake(struct nvt_dev *nvt)
  344. {
  345. nvt_efm_enable(nvt);
  346. nvt_select_logical_dev(nvt, LOGICAL_DEV_ACPI);
  347. nvt_set_reg_bit(nvt, CIR_WAKE_ENABLE_BIT, CR_ACPI_CIR_WAKE);
  348. nvt_set_reg_bit(nvt, CIR_INTR_MOUSE_IRQ_BIT, CR_ACPI_IRQ_EVENTS);
  349. nvt_set_reg_bit(nvt, PME_INTR_CIR_PASS_BIT, CR_ACPI_IRQ_EVENTS2);
  350. nvt_select_logical_dev(nvt, LOGICAL_DEV_CIR_WAKE);
  351. nvt_cr_write(nvt, LOGICAL_DEV_ENABLE, CR_LOGICAL_DEV_EN);
  352. nvt_efm_disable(nvt);
  353. nvt_cir_wake_reg_write(nvt, CIR_WAKE_IRCON_MODE0 | CIR_WAKE_IRCON_RXEN |
  354. CIR_WAKE_IRCON_R | CIR_WAKE_IRCON_RXINV |
  355. CIR_WAKE_IRCON_SAMPLE_PERIOD_SEL,
  356. CIR_WAKE_IRCON);
  357. nvt_cir_wake_reg_write(nvt, 0xff, CIR_WAKE_IRSTS);
  358. nvt_cir_wake_reg_write(nvt, 0, CIR_WAKE_IREN);
  359. }
  360. /* rx carrier detect only works in learning mode, must be called w/nvt_lock */
  361. static u32 nvt_rx_carrier_detect(struct nvt_dev *nvt)
  362. {
  363. u32 count, carrier, duration = 0;
  364. int i;
  365. count = nvt_cir_reg_read(nvt, CIR_FCCL) |
  366. nvt_cir_reg_read(nvt, CIR_FCCH) << 8;
  367. for (i = 0; i < nvt->pkts; i++) {
  368. if (nvt->buf[i] & BUF_PULSE_BIT)
  369. duration += nvt->buf[i] & BUF_LEN_MASK;
  370. }
  371. duration *= SAMPLE_PERIOD;
  372. if (!count || !duration) {
  373. nvt_pr(KERN_NOTICE, "Unable to determine carrier! (c:%u, d:%u)",
  374. count, duration);
  375. return 0;
  376. }
  377. carrier = (count * 1000000) / duration;
  378. if ((carrier > MAX_CARRIER) || (carrier < MIN_CARRIER))
  379. nvt_dbg("WTF? Carrier frequency out of range!");
  380. nvt_dbg("Carrier frequency: %u (count %u, duration %u)",
  381. carrier, count, duration);
  382. return carrier;
  383. }
  384. /*
  385. * set carrier frequency
  386. *
  387. * set carrier on 2 registers: CP & CC
  388. * always set CP as 0x81
  389. * set CC by SPEC, CC = 3MHz/carrier - 1
  390. */
  391. static int nvt_set_tx_carrier(struct rc_dev *dev, u32 carrier)
  392. {
  393. struct nvt_dev *nvt = dev->priv;
  394. u16 val;
  395. nvt_cir_reg_write(nvt, 1, CIR_CP);
  396. val = 3000000 / (carrier) - 1;
  397. nvt_cir_reg_write(nvt, val & 0xff, CIR_CC);
  398. nvt_dbg("cp: 0x%x cc: 0x%x\n",
  399. nvt_cir_reg_read(nvt, CIR_CP), nvt_cir_reg_read(nvt, CIR_CC));
  400. return 0;
  401. }
  402. /*
  403. * nvt_tx_ir
  404. *
  405. * 1) clean TX fifo first (handled by AP)
  406. * 2) copy data from user space
  407. * 3) disable RX interrupts, enable TX interrupts: TTR & TFU
  408. * 4) send 9 packets to TX FIFO to open TTR
  409. * in interrupt_handler:
  410. * 5) send all data out
  411. * go back to write():
  412. * 6) disable TX interrupts, re-enable RX interupts
  413. *
  414. * The key problem of this function is user space data may larger than
  415. * driver's data buf length. So nvt_tx_ir() will only copy TX_BUF_LEN data to
  416. * buf, and keep current copied data buf num in cur_buf_num. But driver's buf
  417. * number may larger than TXFCONT (0xff). So in interrupt_handler, it has to
  418. * set TXFCONT as 0xff, until buf_count less than 0xff.
  419. */
  420. static int nvt_tx_ir(struct rc_dev *dev, int *txbuf, u32 n)
  421. {
  422. struct nvt_dev *nvt = dev->priv;
  423. unsigned long flags;
  424. size_t cur_count;
  425. unsigned int i;
  426. u8 iren;
  427. int ret;
  428. spin_lock_irqsave(&nvt->tx.lock, flags);
  429. if (n >= TX_BUF_LEN) {
  430. nvt->tx.buf_count = cur_count = TX_BUF_LEN;
  431. ret = TX_BUF_LEN;
  432. } else {
  433. nvt->tx.buf_count = cur_count = n;
  434. ret = n;
  435. }
  436. memcpy(nvt->tx.buf, txbuf, nvt->tx.buf_count);
  437. nvt->tx.cur_buf_num = 0;
  438. /* save currently enabled interrupts */
  439. iren = nvt_cir_reg_read(nvt, CIR_IREN);
  440. /* now disable all interrupts, save TFU & TTR */
  441. nvt_cir_reg_write(nvt, CIR_IREN_TFU | CIR_IREN_TTR, CIR_IREN);
  442. nvt->tx.tx_state = ST_TX_REPLY;
  443. nvt_cir_reg_write(nvt, CIR_FIFOCON_TX_TRIGGER_LEV_8 |
  444. CIR_FIFOCON_RXFIFOCLR, CIR_FIFOCON);
  445. /* trigger TTR interrupt by writing out ones, (yes, it's ugly) */
  446. for (i = 0; i < 9; i++)
  447. nvt_cir_reg_write(nvt, 0x01, CIR_STXFIFO);
  448. spin_unlock_irqrestore(&nvt->tx.lock, flags);
  449. wait_event(nvt->tx.queue, nvt->tx.tx_state == ST_TX_REQUEST);
  450. spin_lock_irqsave(&nvt->tx.lock, flags);
  451. nvt->tx.tx_state = ST_TX_NONE;
  452. spin_unlock_irqrestore(&nvt->tx.lock, flags);
  453. /* restore enabled interrupts to prior state */
  454. nvt_cir_reg_write(nvt, iren, CIR_IREN);
  455. return ret;
  456. }
  457. /* dump contents of the last rx buffer we got from the hw rx fifo */
  458. static void nvt_dump_rx_buf(struct nvt_dev *nvt)
  459. {
  460. int i;
  461. printk(KERN_DEBUG "%s (len %d): ", __func__, nvt->pkts);
  462. for (i = 0; (i < nvt->pkts) && (i < RX_BUF_LEN); i++)
  463. printk(KERN_CONT "0x%02x ", nvt->buf[i]);
  464. printk(KERN_CONT "\n");
  465. }
  466. /*
  467. * Process raw data in rx driver buffer, store it in raw IR event kfifo,
  468. * trigger decode when appropriate.
  469. *
  470. * We get IR data samples one byte at a time. If the msb is set, its a pulse,
  471. * otherwise its a space. The lower 7 bits are the count of SAMPLE_PERIOD
  472. * (default 50us) intervals for that pulse/space. A discrete signal is
  473. * followed by a series of 0x7f packets, then either 0x7<something> or 0x80
  474. * to signal more IR coming (repeats) or end of IR, respectively. We store
  475. * sample data in the raw event kfifo until we see 0x7<something> (except f)
  476. * or 0x80, at which time, we trigger a decode operation.
  477. */
  478. static void nvt_process_rx_ir_data(struct nvt_dev *nvt)
  479. {
  480. DEFINE_IR_RAW_EVENT(rawir);
  481. unsigned int count;
  482. u32 carrier;
  483. u8 sample;
  484. int i;
  485. nvt_dbg_verbose("%s firing", __func__);
  486. if (debug)
  487. nvt_dump_rx_buf(nvt);
  488. if (nvt->carrier_detect_enabled)
  489. carrier = nvt_rx_carrier_detect(nvt);
  490. count = nvt->pkts;
  491. nvt_dbg_verbose("Processing buffer of len %d", count);
  492. init_ir_raw_event(&rawir);
  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) && nvt->pkts) {
  526. nvt_dbg("Calling ir_raw_event_handle (signal end)\n");
  527. ir_raw_event_handle(nvt->rdev);
  528. }
  529. }
  530. nvt_dbg("Calling ir_raw_event_handle (buffer empty)\n");
  531. ir_raw_event_handle(nvt->rdev);
  532. if (nvt->pkts) {
  533. nvt_dbg("Odd, pkts should be 0 now... (its %u)", nvt->pkts);
  534. nvt->pkts = 0;
  535. }
  536. nvt_dbg_verbose("%s done", __func__);
  537. }
  538. static void nvt_handle_rx_fifo_overrun(struct nvt_dev *nvt)
  539. {
  540. nvt_pr(KERN_WARNING, "RX FIFO overrun detected, flushing data!");
  541. nvt->pkts = 0;
  542. nvt_clear_cir_fifo(nvt);
  543. ir_raw_event_reset(nvt->rdev);
  544. }
  545. /* copy data from hardware rx fifo into driver buffer */
  546. static void nvt_get_rx_ir_data(struct nvt_dev *nvt)
  547. {
  548. unsigned long flags;
  549. u8 fifocount, val;
  550. unsigned int b_idx;
  551. bool overrun = false;
  552. int i;
  553. /* Get count of how many bytes to read from RX FIFO */
  554. fifocount = nvt_cir_reg_read(nvt, CIR_RXFCONT);
  555. /* if we get 0xff, probably means the logical dev is disabled */
  556. if (fifocount == 0xff)
  557. return;
  558. /* watch out for a fifo overrun condition */
  559. else if (fifocount > RX_BUF_LEN) {
  560. overrun = true;
  561. fifocount = RX_BUF_LEN;
  562. }
  563. nvt_dbg("attempting to fetch %u bytes from hw rx fifo", fifocount);
  564. spin_lock_irqsave(&nvt->nvt_lock, flags);
  565. b_idx = nvt->pkts;
  566. /* This should never happen, but lets check anyway... */
  567. if (b_idx + fifocount > RX_BUF_LEN) {
  568. nvt_process_rx_ir_data(nvt);
  569. b_idx = 0;
  570. }
  571. /* Read fifocount bytes from CIR Sample RX FIFO register */
  572. for (i = 0; i < fifocount; i++) {
  573. val = nvt_cir_reg_read(nvt, CIR_SRXFIFO);
  574. nvt->buf[b_idx + i] = val;
  575. }
  576. nvt->pkts += fifocount;
  577. nvt_dbg("%s: pkts now %d", __func__, nvt->pkts);
  578. nvt_process_rx_ir_data(nvt);
  579. if (overrun)
  580. nvt_handle_rx_fifo_overrun(nvt);
  581. spin_unlock_irqrestore(&nvt->nvt_lock, flags);
  582. }
  583. static void nvt_cir_log_irqs(u8 status, u8 iren)
  584. {
  585. nvt_pr(KERN_INFO, "IRQ 0x%02x (IREN 0x%02x) :%s%s%s%s%s%s%s%s%s",
  586. status, iren,
  587. status & CIR_IRSTS_RDR ? " RDR" : "",
  588. status & CIR_IRSTS_RTR ? " RTR" : "",
  589. status & CIR_IRSTS_PE ? " PE" : "",
  590. status & CIR_IRSTS_RFO ? " RFO" : "",
  591. status & CIR_IRSTS_TE ? " TE" : "",
  592. status & CIR_IRSTS_TTR ? " TTR" : "",
  593. status & CIR_IRSTS_TFU ? " TFU" : "",
  594. status & CIR_IRSTS_GH ? " GH" : "",
  595. status & ~(CIR_IRSTS_RDR | CIR_IRSTS_RTR | CIR_IRSTS_PE |
  596. CIR_IRSTS_RFO | CIR_IRSTS_TE | CIR_IRSTS_TTR |
  597. CIR_IRSTS_TFU | CIR_IRSTS_GH) ? " ?" : "");
  598. }
  599. static bool nvt_cir_tx_inactive(struct nvt_dev *nvt)
  600. {
  601. unsigned long flags;
  602. bool tx_inactive;
  603. u8 tx_state;
  604. spin_lock_irqsave(&nvt->tx.lock, flags);
  605. tx_state = nvt->tx.tx_state;
  606. spin_unlock_irqrestore(&nvt->tx.lock, flags);
  607. tx_inactive = (tx_state == ST_TX_NONE);
  608. return tx_inactive;
  609. }
  610. /* interrupt service routine for incoming and outgoing CIR data */
  611. static irqreturn_t nvt_cir_isr(int irq, void *data)
  612. {
  613. struct nvt_dev *nvt = data;
  614. u8 status, iren, cur_state;
  615. unsigned long flags;
  616. nvt_dbg_verbose("%s firing", __func__);
  617. nvt_efm_enable(nvt);
  618. nvt_select_logical_dev(nvt, LOGICAL_DEV_CIR);
  619. nvt_efm_disable(nvt);
  620. /*
  621. * Get IR Status register contents. Write 1 to ack/clear
  622. *
  623. * bit: reg name - description
  624. * 7: CIR_IRSTS_RDR - RX Data Ready
  625. * 6: CIR_IRSTS_RTR - RX FIFO Trigger Level Reach
  626. * 5: CIR_IRSTS_PE - Packet End
  627. * 4: CIR_IRSTS_RFO - RX FIFO Overrun (RDR will also be set)
  628. * 3: CIR_IRSTS_TE - TX FIFO Empty
  629. * 2: CIR_IRSTS_TTR - TX FIFO Trigger Level Reach
  630. * 1: CIR_IRSTS_TFU - TX FIFO Underrun
  631. * 0: CIR_IRSTS_GH - Min Length Detected
  632. */
  633. status = nvt_cir_reg_read(nvt, CIR_IRSTS);
  634. if (!status) {
  635. nvt_dbg_verbose("%s exiting, IRSTS 0x0", __func__);
  636. nvt_cir_reg_write(nvt, 0xff, CIR_IRSTS);
  637. return IRQ_RETVAL(IRQ_NONE);
  638. }
  639. /* ack/clear all irq flags we've got */
  640. nvt_cir_reg_write(nvt, status, CIR_IRSTS);
  641. nvt_cir_reg_write(nvt, 0, CIR_IRSTS);
  642. /* Interrupt may be shared with CIR Wake, bail if CIR not enabled */
  643. iren = nvt_cir_reg_read(nvt, CIR_IREN);
  644. if (!iren) {
  645. nvt_dbg_verbose("%s exiting, CIR not enabled", __func__);
  646. return IRQ_RETVAL(IRQ_NONE);
  647. }
  648. if (debug)
  649. nvt_cir_log_irqs(status, iren);
  650. if (status & CIR_IRSTS_RTR) {
  651. /* FIXME: add code for study/learn mode */
  652. /* We only do rx if not tx'ing */
  653. if (nvt_cir_tx_inactive(nvt))
  654. nvt_get_rx_ir_data(nvt);
  655. }
  656. if (status & CIR_IRSTS_PE) {
  657. if (nvt_cir_tx_inactive(nvt))
  658. nvt_get_rx_ir_data(nvt);
  659. spin_lock_irqsave(&nvt->nvt_lock, flags);
  660. cur_state = nvt->study_state;
  661. spin_unlock_irqrestore(&nvt->nvt_lock, flags);
  662. if (cur_state == ST_STUDY_NONE)
  663. nvt_clear_cir_fifo(nvt);
  664. }
  665. if (status & CIR_IRSTS_TE)
  666. nvt_clear_tx_fifo(nvt);
  667. if (status & CIR_IRSTS_TTR) {
  668. unsigned int pos, count;
  669. u8 tmp;
  670. spin_lock_irqsave(&nvt->tx.lock, flags);
  671. pos = nvt->tx.cur_buf_num;
  672. count = nvt->tx.buf_count;
  673. /* Write data into the hardware tx fifo while pos < count */
  674. if (pos < count) {
  675. nvt_cir_reg_write(nvt, nvt->tx.buf[pos], CIR_STXFIFO);
  676. nvt->tx.cur_buf_num++;
  677. /* Disable TX FIFO Trigger Level Reach (TTR) interrupt */
  678. } else {
  679. tmp = nvt_cir_reg_read(nvt, CIR_IREN);
  680. nvt_cir_reg_write(nvt, tmp & ~CIR_IREN_TTR, CIR_IREN);
  681. }
  682. spin_unlock_irqrestore(&nvt->tx.lock, flags);
  683. }
  684. if (status & CIR_IRSTS_TFU) {
  685. spin_lock_irqsave(&nvt->tx.lock, flags);
  686. if (nvt->tx.tx_state == ST_TX_REPLY) {
  687. nvt->tx.tx_state = ST_TX_REQUEST;
  688. wake_up(&nvt->tx.queue);
  689. }
  690. spin_unlock_irqrestore(&nvt->tx.lock, flags);
  691. }
  692. nvt_dbg_verbose("%s done", __func__);
  693. return IRQ_RETVAL(IRQ_HANDLED);
  694. }
  695. /* Interrupt service routine for CIR Wake */
  696. static irqreturn_t nvt_cir_wake_isr(int irq, void *data)
  697. {
  698. u8 status, iren, val;
  699. struct nvt_dev *nvt = data;
  700. unsigned long flags;
  701. nvt_dbg_wake("%s firing", __func__);
  702. status = nvt_cir_wake_reg_read(nvt, CIR_WAKE_IRSTS);
  703. if (!status)
  704. return IRQ_RETVAL(IRQ_NONE);
  705. if (status & CIR_WAKE_IRSTS_IR_PENDING)
  706. nvt_clear_cir_wake_fifo(nvt);
  707. nvt_cir_wake_reg_write(nvt, status, CIR_WAKE_IRSTS);
  708. nvt_cir_wake_reg_write(nvt, 0, CIR_WAKE_IRSTS);
  709. /* Interrupt may be shared with CIR, bail if Wake not enabled */
  710. iren = nvt_cir_wake_reg_read(nvt, CIR_WAKE_IREN);
  711. if (!iren) {
  712. nvt_dbg_wake("%s exiting, wake not enabled", __func__);
  713. return IRQ_RETVAL(IRQ_HANDLED);
  714. }
  715. if ((status & CIR_WAKE_IRSTS_PE) &&
  716. (nvt->wake_state == ST_WAKE_START)) {
  717. while (nvt_cir_wake_reg_read(nvt, CIR_WAKE_RD_FIFO_ONLY_IDX)) {
  718. val = nvt_cir_wake_reg_read(nvt, CIR_WAKE_RD_FIFO_ONLY);
  719. nvt_dbg("setting wake up key: 0x%x", val);
  720. }
  721. nvt_cir_wake_reg_write(nvt, 0, CIR_WAKE_IREN);
  722. spin_lock_irqsave(&nvt->nvt_lock, flags);
  723. nvt->wake_state = ST_WAKE_FINISH;
  724. spin_unlock_irqrestore(&nvt->nvt_lock, flags);
  725. }
  726. nvt_dbg_wake("%s done", __func__);
  727. return IRQ_RETVAL(IRQ_HANDLED);
  728. }
  729. static void nvt_enable_cir(struct nvt_dev *nvt)
  730. {
  731. /* set function enable flags */
  732. nvt_cir_reg_write(nvt, CIR_IRCON_TXEN | CIR_IRCON_RXEN |
  733. CIR_IRCON_RXINV | CIR_IRCON_SAMPLE_PERIOD_SEL,
  734. CIR_IRCON);
  735. nvt_efm_enable(nvt);
  736. /* enable the CIR logical device */
  737. nvt_select_logical_dev(nvt, LOGICAL_DEV_CIR);
  738. nvt_cr_write(nvt, LOGICAL_DEV_ENABLE, CR_LOGICAL_DEV_EN);
  739. nvt_efm_disable(nvt);
  740. /* clear all pending interrupts */
  741. nvt_cir_reg_write(nvt, 0xff, CIR_IRSTS);
  742. /* enable interrupts */
  743. nvt_set_cir_iren(nvt);
  744. }
  745. static void nvt_disable_cir(struct nvt_dev *nvt)
  746. {
  747. /* disable CIR interrupts */
  748. nvt_cir_reg_write(nvt, 0, CIR_IREN);
  749. /* clear any and all pending interrupts */
  750. nvt_cir_reg_write(nvt, 0xff, CIR_IRSTS);
  751. /* clear all function enable flags */
  752. nvt_cir_reg_write(nvt, 0, CIR_IRCON);
  753. /* clear hardware rx and tx fifos */
  754. nvt_clear_cir_fifo(nvt);
  755. nvt_clear_tx_fifo(nvt);
  756. nvt_efm_enable(nvt);
  757. /* disable the CIR logical device */
  758. nvt_select_logical_dev(nvt, LOGICAL_DEV_CIR);
  759. nvt_cr_write(nvt, LOGICAL_DEV_DISABLE, CR_LOGICAL_DEV_EN);
  760. nvt_efm_disable(nvt);
  761. }
  762. static int nvt_open(struct rc_dev *dev)
  763. {
  764. struct nvt_dev *nvt = dev->priv;
  765. unsigned long flags;
  766. spin_lock_irqsave(&nvt->nvt_lock, flags);
  767. nvt->in_use = true;
  768. nvt_enable_cir(nvt);
  769. spin_unlock_irqrestore(&nvt->nvt_lock, flags);
  770. return 0;
  771. }
  772. static void nvt_close(struct rc_dev *dev)
  773. {
  774. struct nvt_dev *nvt = dev->priv;
  775. unsigned long flags;
  776. spin_lock_irqsave(&nvt->nvt_lock, flags);
  777. nvt->in_use = false;
  778. nvt_disable_cir(nvt);
  779. spin_unlock_irqrestore(&nvt->nvt_lock, flags);
  780. }
  781. /* Allocate memory, probe hardware, and initialize everything */
  782. static int nvt_probe(struct pnp_dev *pdev, const struct pnp_device_id *dev_id)
  783. {
  784. struct nvt_dev *nvt;
  785. struct rc_dev *rdev;
  786. int ret = -ENOMEM;
  787. nvt = kzalloc(sizeof(struct nvt_dev), GFP_KERNEL);
  788. if (!nvt)
  789. return ret;
  790. /* input device for IR remote (and tx) */
  791. rdev = rc_allocate_device();
  792. if (!rdev)
  793. goto failure;
  794. ret = -ENODEV;
  795. /* validate pnp resources */
  796. if (!pnp_port_valid(pdev, 0) ||
  797. pnp_port_len(pdev, 0) < CIR_IOREG_LENGTH) {
  798. dev_err(&pdev->dev, "IR PNP Port not valid!\n");
  799. goto failure;
  800. }
  801. if (!pnp_irq_valid(pdev, 0)) {
  802. dev_err(&pdev->dev, "PNP IRQ not valid!\n");
  803. goto failure;
  804. }
  805. if (!pnp_port_valid(pdev, 1) ||
  806. pnp_port_len(pdev, 1) < CIR_IOREG_LENGTH) {
  807. dev_err(&pdev->dev, "Wake PNP Port not valid!\n");
  808. goto failure;
  809. }
  810. nvt->cir_addr = pnp_port_start(pdev, 0);
  811. nvt->cir_irq = pnp_irq(pdev, 0);
  812. nvt->cir_wake_addr = pnp_port_start(pdev, 1);
  813. /* irq is always shared between cir and cir wake */
  814. nvt->cir_wake_irq = nvt->cir_irq;
  815. nvt->cr_efir = CR_EFIR;
  816. nvt->cr_efdr = CR_EFDR;
  817. spin_lock_init(&nvt->nvt_lock);
  818. spin_lock_init(&nvt->tx.lock);
  819. init_ir_raw_event(&nvt->rawir);
  820. ret = -EBUSY;
  821. /* now claim resources */
  822. if (!request_region(nvt->cir_addr,
  823. CIR_IOREG_LENGTH, NVT_DRIVER_NAME))
  824. goto failure;
  825. if (request_irq(nvt->cir_irq, nvt_cir_isr, IRQF_SHARED,
  826. NVT_DRIVER_NAME, (void *)nvt))
  827. goto failure;
  828. if (!request_region(nvt->cir_wake_addr,
  829. CIR_IOREG_LENGTH, NVT_DRIVER_NAME))
  830. goto failure;
  831. if (request_irq(nvt->cir_wake_irq, nvt_cir_wake_isr, IRQF_SHARED,
  832. NVT_DRIVER_NAME, (void *)nvt))
  833. goto failure;
  834. pnp_set_drvdata(pdev, nvt);
  835. nvt->pdev = pdev;
  836. init_waitqueue_head(&nvt->tx.queue);
  837. ret = nvt_hw_detect(nvt);
  838. if (ret)
  839. goto failure;
  840. /* Initialize CIR & CIR Wake Logical Devices */
  841. nvt_efm_enable(nvt);
  842. nvt_cir_ldev_init(nvt);
  843. nvt_cir_wake_ldev_init(nvt);
  844. nvt_efm_disable(nvt);
  845. /* Initialize CIR & CIR Wake Config Registers */
  846. nvt_cir_regs_init(nvt);
  847. nvt_cir_wake_regs_init(nvt);
  848. /* Set up the rc device */
  849. rdev->priv = nvt;
  850. rdev->driver_type = RC_DRIVER_IR_RAW;
  851. rdev->allowed_protos = IR_TYPE_ALL;
  852. rdev->open = nvt_open;
  853. rdev->close = nvt_close;
  854. rdev->tx_ir = nvt_tx_ir;
  855. rdev->s_tx_carrier = nvt_set_tx_carrier;
  856. rdev->input_name = "Nuvoton w836x7hg Infrared Remote Transceiver";
  857. rdev->input_id.bustype = BUS_HOST;
  858. rdev->input_id.vendor = PCI_VENDOR_ID_WINBOND2;
  859. rdev->input_id.product = nvt->chip_major;
  860. rdev->input_id.version = nvt->chip_minor;
  861. rdev->driver_name = NVT_DRIVER_NAME;
  862. rdev->map_name = RC_MAP_RC6_MCE;
  863. #if 0
  864. rdev->min_timeout = XYZ;
  865. rdev->max_timeout = XYZ;
  866. rdev->timeout = XYZ;
  867. /* rx resolution is hardwired to 50us atm, 1, 25, 100 also possible */
  868. rdev->rx_resolution = XYZ;
  869. /* tx bits */
  870. rdev->tx_resolution = XYZ;
  871. #endif
  872. ret = rc_register_device(rdev);
  873. if (ret)
  874. goto failure;
  875. device_set_wakeup_capable(&pdev->dev, 1);
  876. device_set_wakeup_enable(&pdev->dev, 1);
  877. nvt->rdev = rdev;
  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. rc_free_device(rdev);
  894. kfree(nvt);
  895. return ret;
  896. }
  897. static void __devexit nvt_remove(struct pnp_dev *pdev)
  898. {
  899. struct nvt_dev *nvt = pnp_get_drvdata(pdev);
  900. unsigned long flags;
  901. spin_lock_irqsave(&nvt->nvt_lock, flags);
  902. /* disable CIR */
  903. nvt_cir_reg_write(nvt, 0, CIR_IREN);
  904. nvt_disable_cir(nvt);
  905. /* enable CIR Wake (for IR power-on) */
  906. nvt_enable_wake(nvt);
  907. spin_unlock_irqrestore(&nvt->nvt_lock, flags);
  908. /* free resources */
  909. free_irq(nvt->cir_irq, nvt);
  910. free_irq(nvt->cir_wake_irq, nvt);
  911. release_region(nvt->cir_addr, CIR_IOREG_LENGTH);
  912. release_region(nvt->cir_wake_addr, CIR_IOREG_LENGTH);
  913. rc_unregister_device(nvt->rdev);
  914. kfree(nvt);
  915. }
  916. static int nvt_suspend(struct pnp_dev *pdev, pm_message_t state)
  917. {
  918. struct nvt_dev *nvt = pnp_get_drvdata(pdev);
  919. unsigned long flags;
  920. nvt_dbg("%s called", __func__);
  921. /* zero out misc state tracking */
  922. spin_lock_irqsave(&nvt->nvt_lock, flags);
  923. nvt->study_state = ST_STUDY_NONE;
  924. nvt->wake_state = ST_WAKE_NONE;
  925. spin_unlock_irqrestore(&nvt->nvt_lock, flags);
  926. spin_lock_irqsave(&nvt->tx.lock, flags);
  927. nvt->tx.tx_state = ST_TX_NONE;
  928. spin_unlock_irqrestore(&nvt->tx.lock, flags);
  929. /* disable all CIR interrupts */
  930. nvt_cir_reg_write(nvt, 0, CIR_IREN);
  931. nvt_efm_enable(nvt);
  932. /* disable cir logical dev */
  933. nvt_select_logical_dev(nvt, LOGICAL_DEV_CIR);
  934. nvt_cr_write(nvt, LOGICAL_DEV_DISABLE, CR_LOGICAL_DEV_EN);
  935. nvt_efm_disable(nvt);
  936. /* make sure wake is enabled */
  937. nvt_enable_wake(nvt);
  938. return 0;
  939. }
  940. static int nvt_resume(struct pnp_dev *pdev)
  941. {
  942. int ret = 0;
  943. struct nvt_dev *nvt = pnp_get_drvdata(pdev);
  944. nvt_dbg("%s called", __func__);
  945. /* open interrupt */
  946. nvt_set_cir_iren(nvt);
  947. /* Enable CIR logical device */
  948. nvt_efm_enable(nvt);
  949. nvt_select_logical_dev(nvt, LOGICAL_DEV_CIR);
  950. nvt_cr_write(nvt, LOGICAL_DEV_ENABLE, CR_LOGICAL_DEV_EN);
  951. nvt_efm_disable(nvt);
  952. nvt_cir_regs_init(nvt);
  953. nvt_cir_wake_regs_init(nvt);
  954. return ret;
  955. }
  956. static void nvt_shutdown(struct pnp_dev *pdev)
  957. {
  958. struct nvt_dev *nvt = pnp_get_drvdata(pdev);
  959. nvt_enable_wake(nvt);
  960. }
  961. static const struct pnp_device_id nvt_ids[] = {
  962. { "WEC0530", 0 }, /* CIR */
  963. { "NTN0530", 0 }, /* CIR for new chip's pnp id*/
  964. { "", 0 },
  965. };
  966. static struct pnp_driver nvt_driver = {
  967. .name = NVT_DRIVER_NAME,
  968. .id_table = nvt_ids,
  969. .flags = PNP_DRIVER_RES_DO_NOT_CHANGE,
  970. .probe = nvt_probe,
  971. .remove = __devexit_p(nvt_remove),
  972. .suspend = nvt_suspend,
  973. .resume = nvt_resume,
  974. .shutdown = nvt_shutdown,
  975. };
  976. int nvt_init(void)
  977. {
  978. return pnp_register_driver(&nvt_driver);
  979. }
  980. void nvt_exit(void)
  981. {
  982. pnp_unregister_driver(&nvt_driver);
  983. }
  984. module_param(debug, int, S_IRUGO | S_IWUSR);
  985. MODULE_PARM_DESC(debug, "Enable debugging output");
  986. MODULE_DEVICE_TABLE(pnp, nvt_ids);
  987. MODULE_DESCRIPTION("Nuvoton W83667HG-A & W83677HG-I CIR driver");
  988. MODULE_AUTHOR("Jarod Wilson <jarod@redhat.com>");
  989. MODULE_LICENSE("GPL");
  990. module_init(nvt_init);
  991. module_exit(nvt_exit);