nuvoton-cir.c 34 KB

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