nuvoton-cir.c 32 KB

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