nuvoton-cir.c 34 KB

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