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