fintek-cir.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /*
  2. * Driver for Feature Integration Technology Inc. (aka Fintek) LPC CIR
  3. *
  4. * Copyright (C) 2011 Jarod Wilson <jarod@redhat.com>
  5. *
  6. * Special thanks to Fintek for providing hardware and spec sheets.
  7. * This driver is based upon the nuvoton, ite and ene drivers for
  8. * similar hardware.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  23. * USA
  24. */
  25. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/pnp.h>
  29. #include <linux/io.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/sched.h>
  32. #include <linux/slab.h>
  33. #include <media/rc-core.h>
  34. #include "fintek-cir.h"
  35. /* write val to config reg */
  36. static inline void fintek_cr_write(struct fintek_dev *fintek, u8 val, u8 reg)
  37. {
  38. fit_dbg("%s: reg 0x%02x, val 0x%02x (ip/dp: %02x/%02x)",
  39. __func__, reg, val, fintek->cr_ip, fintek->cr_dp);
  40. outb(reg, fintek->cr_ip);
  41. outb(val, fintek->cr_dp);
  42. }
  43. /* read val from config reg */
  44. static inline u8 fintek_cr_read(struct fintek_dev *fintek, u8 reg)
  45. {
  46. u8 val;
  47. outb(reg, fintek->cr_ip);
  48. val = inb(fintek->cr_dp);
  49. fit_dbg("%s: reg 0x%02x, val 0x%02x (ip/dp: %02x/%02x)",
  50. __func__, reg, val, fintek->cr_ip, fintek->cr_dp);
  51. return val;
  52. }
  53. /* update config register bit without changing other bits */
  54. static inline void fintek_set_reg_bit(struct fintek_dev *fintek, u8 val, u8 reg)
  55. {
  56. u8 tmp = fintek_cr_read(fintek, reg) | val;
  57. fintek_cr_write(fintek, tmp, reg);
  58. }
  59. /* clear config register bit without changing other bits */
  60. static inline void fintek_clear_reg_bit(struct fintek_dev *fintek, u8 val, u8 reg)
  61. {
  62. u8 tmp = fintek_cr_read(fintek, reg) & ~val;
  63. fintek_cr_write(fintek, tmp, reg);
  64. }
  65. /* enter config mode */
  66. static inline void fintek_config_mode_enable(struct fintek_dev *fintek)
  67. {
  68. /* Enabling Config Mode explicitly requires writing 2x */
  69. outb(CONFIG_REG_ENABLE, fintek->cr_ip);
  70. outb(CONFIG_REG_ENABLE, fintek->cr_ip);
  71. }
  72. /* exit config mode */
  73. static inline void fintek_config_mode_disable(struct fintek_dev *fintek)
  74. {
  75. outb(CONFIG_REG_DISABLE, fintek->cr_ip);
  76. }
  77. /*
  78. * When you want to address a specific logical device, write its logical
  79. * device number to GCR_LOGICAL_DEV_NO
  80. */
  81. static inline void fintek_select_logical_dev(struct fintek_dev *fintek, u8 ldev)
  82. {
  83. fintek_cr_write(fintek, ldev, GCR_LOGICAL_DEV_NO);
  84. }
  85. /* write val to cir config register */
  86. static inline void fintek_cir_reg_write(struct fintek_dev *fintek, u8 val, u8 offset)
  87. {
  88. outb(val, fintek->cir_addr + offset);
  89. }
  90. /* read val from cir config register */
  91. static u8 fintek_cir_reg_read(struct fintek_dev *fintek, u8 offset)
  92. {
  93. u8 val;
  94. val = inb(fintek->cir_addr + offset);
  95. return val;
  96. }
  97. /* dump current cir register contents */
  98. static void cir_dump_regs(struct fintek_dev *fintek)
  99. {
  100. fintek_config_mode_enable(fintek);
  101. fintek_select_logical_dev(fintek, fintek->logical_dev_cir);
  102. pr_info("%s: Dump CIR logical device registers:\n", FINTEK_DRIVER_NAME);
  103. pr_info(" * CR CIR BASE ADDR: 0x%x\n",
  104. (fintek_cr_read(fintek, CIR_CR_BASE_ADDR_HI) << 8) |
  105. fintek_cr_read(fintek, CIR_CR_BASE_ADDR_LO));
  106. pr_info(" * CR CIR IRQ NUM: 0x%x\n",
  107. fintek_cr_read(fintek, CIR_CR_IRQ_SEL));
  108. fintek_config_mode_disable(fintek);
  109. pr_info("%s: Dump CIR registers:\n", FINTEK_DRIVER_NAME);
  110. pr_info(" * STATUS: 0x%x\n",
  111. fintek_cir_reg_read(fintek, CIR_STATUS));
  112. pr_info(" * CONTROL: 0x%x\n",
  113. fintek_cir_reg_read(fintek, CIR_CONTROL));
  114. pr_info(" * RX_DATA: 0x%x\n",
  115. fintek_cir_reg_read(fintek, CIR_RX_DATA));
  116. pr_info(" * TX_CONTROL: 0x%x\n",
  117. fintek_cir_reg_read(fintek, CIR_TX_CONTROL));
  118. pr_info(" * TX_DATA: 0x%x\n",
  119. fintek_cir_reg_read(fintek, CIR_TX_DATA));
  120. }
  121. /* detect hardware features */
  122. static int fintek_hw_detect(struct fintek_dev *fintek)
  123. {
  124. unsigned long flags;
  125. u8 chip_major, chip_minor;
  126. u8 vendor_major, vendor_minor;
  127. u8 portsel, ir_class;
  128. u16 vendor, chip;
  129. fintek_config_mode_enable(fintek);
  130. /* Check if we're using config port 0x4e or 0x2e */
  131. portsel = fintek_cr_read(fintek, GCR_CONFIG_PORT_SEL);
  132. if (portsel == 0xff) {
  133. fit_pr(KERN_INFO, "first portsel read was bunk, trying alt");
  134. fintek_config_mode_disable(fintek);
  135. fintek->cr_ip = CR_INDEX_PORT2;
  136. fintek->cr_dp = CR_DATA_PORT2;
  137. fintek_config_mode_enable(fintek);
  138. portsel = fintek_cr_read(fintek, GCR_CONFIG_PORT_SEL);
  139. }
  140. fit_dbg("portsel reg: 0x%02x", portsel);
  141. ir_class = fintek_cir_reg_read(fintek, CIR_CR_CLASS);
  142. fit_dbg("ir_class reg: 0x%02x", ir_class);
  143. switch (ir_class) {
  144. case CLASS_RX_2TX:
  145. case CLASS_RX_1TX:
  146. fintek->hw_tx_capable = true;
  147. break;
  148. case CLASS_RX_ONLY:
  149. default:
  150. fintek->hw_tx_capable = false;
  151. break;
  152. }
  153. chip_major = fintek_cr_read(fintek, GCR_CHIP_ID_HI);
  154. chip_minor = fintek_cr_read(fintek, GCR_CHIP_ID_LO);
  155. chip = chip_major << 8 | chip_minor;
  156. vendor_major = fintek_cr_read(fintek, GCR_VENDOR_ID_HI);
  157. vendor_minor = fintek_cr_read(fintek, GCR_VENDOR_ID_LO);
  158. vendor = vendor_major << 8 | vendor_minor;
  159. if (vendor != VENDOR_ID_FINTEK)
  160. fit_pr(KERN_WARNING, "Unknown vendor ID: 0x%04x", vendor);
  161. else
  162. fit_dbg("Read Fintek vendor ID from chip");
  163. fintek_config_mode_disable(fintek);
  164. spin_lock_irqsave(&fintek->fintek_lock, flags);
  165. fintek->chip_major = chip_major;
  166. fintek->chip_minor = chip_minor;
  167. fintek->chip_vendor = vendor;
  168. /*
  169. * Newer reviews of this chipset uses port 8 instead of 5
  170. */
  171. if ((chip != 0x0408) && (chip != 0x0804))
  172. fintek->logical_dev_cir = LOGICAL_DEV_CIR_REV2;
  173. else
  174. fintek->logical_dev_cir = LOGICAL_DEV_CIR_REV1;
  175. spin_unlock_irqrestore(&fintek->fintek_lock, flags);
  176. return 0;
  177. }
  178. static void fintek_cir_ldev_init(struct fintek_dev *fintek)
  179. {
  180. /* Select CIR logical device and enable */
  181. fintek_select_logical_dev(fintek, fintek->logical_dev_cir);
  182. fintek_cr_write(fintek, LOGICAL_DEV_ENABLE, CIR_CR_DEV_EN);
  183. /* Write allocated CIR address and IRQ information to hardware */
  184. fintek_cr_write(fintek, fintek->cir_addr >> 8, CIR_CR_BASE_ADDR_HI);
  185. fintek_cr_write(fintek, fintek->cir_addr & 0xff, CIR_CR_BASE_ADDR_LO);
  186. fintek_cr_write(fintek, fintek->cir_irq, CIR_CR_IRQ_SEL);
  187. fit_dbg("CIR initialized, base io address: 0x%lx, irq: %d (len: %d)",
  188. fintek->cir_addr, fintek->cir_irq, fintek->cir_port_len);
  189. }
  190. /* enable CIR interrupts */
  191. static void fintek_enable_cir_irq(struct fintek_dev *fintek)
  192. {
  193. fintek_cir_reg_write(fintek, CIR_STATUS_IRQ_EN, CIR_STATUS);
  194. }
  195. static void fintek_cir_regs_init(struct fintek_dev *fintek)
  196. {
  197. /* clear any and all stray interrupts */
  198. fintek_cir_reg_write(fintek, CIR_STATUS_IRQ_MASK, CIR_STATUS);
  199. /* and finally, enable interrupts */
  200. fintek_enable_cir_irq(fintek);
  201. }
  202. static void fintek_enable_wake(struct fintek_dev *fintek)
  203. {
  204. fintek_config_mode_enable(fintek);
  205. fintek_select_logical_dev(fintek, LOGICAL_DEV_ACPI);
  206. /* Allow CIR PME's to wake system */
  207. fintek_set_reg_bit(fintek, ACPI_WAKE_EN_CIR_BIT, LDEV_ACPI_WAKE_EN_REG);
  208. /* Enable CIR PME's */
  209. fintek_set_reg_bit(fintek, ACPI_PME_CIR_BIT, LDEV_ACPI_PME_EN_REG);
  210. /* Clear CIR PME status register */
  211. fintek_set_reg_bit(fintek, ACPI_PME_CIR_BIT, LDEV_ACPI_PME_CLR_REG);
  212. /* Save state */
  213. fintek_set_reg_bit(fintek, ACPI_STATE_CIR_BIT, LDEV_ACPI_STATE_REG);
  214. fintek_config_mode_disable(fintek);
  215. }
  216. static int fintek_cmdsize(u8 cmd, u8 subcmd)
  217. {
  218. int datasize = 0;
  219. switch (cmd) {
  220. case BUF_COMMAND_NULL:
  221. if (subcmd == BUF_HW_CMD_HEADER)
  222. datasize = 1;
  223. break;
  224. case BUF_HW_CMD_HEADER:
  225. if (subcmd == BUF_CMD_G_REVISION)
  226. datasize = 2;
  227. break;
  228. case BUF_COMMAND_HEADER:
  229. switch (subcmd) {
  230. case BUF_CMD_S_CARRIER:
  231. case BUF_CMD_S_TIMEOUT:
  232. case BUF_RSP_PULSE_COUNT:
  233. datasize = 2;
  234. break;
  235. case BUF_CMD_SIG_END:
  236. case BUF_CMD_S_TXMASK:
  237. case BUF_CMD_S_RXSENSOR:
  238. datasize = 1;
  239. break;
  240. }
  241. }
  242. return datasize;
  243. }
  244. /* process ir data stored in driver buffer */
  245. static void fintek_process_rx_ir_data(struct fintek_dev *fintek)
  246. {
  247. DEFINE_IR_RAW_EVENT(rawir);
  248. u8 sample;
  249. bool event = false;
  250. int i;
  251. for (i = 0; i < fintek->pkts; i++) {
  252. sample = fintek->buf[i];
  253. switch (fintek->parser_state) {
  254. case CMD_HEADER:
  255. fintek->cmd = sample;
  256. if ((fintek->cmd == BUF_COMMAND_HEADER) ||
  257. ((fintek->cmd & BUF_COMMAND_MASK) !=
  258. BUF_PULSE_BIT)) {
  259. fintek->parser_state = SUBCMD;
  260. continue;
  261. }
  262. fintek->rem = (fintek->cmd & BUF_LEN_MASK);
  263. fit_dbg("%s: rem: 0x%02x", __func__, fintek->rem);
  264. if (fintek->rem)
  265. fintek->parser_state = PARSE_IRDATA;
  266. else
  267. ir_raw_event_reset(fintek->rdev);
  268. break;
  269. case SUBCMD:
  270. fintek->rem = fintek_cmdsize(fintek->cmd, sample);
  271. fintek->parser_state = CMD_DATA;
  272. break;
  273. case CMD_DATA:
  274. fintek->rem--;
  275. break;
  276. case PARSE_IRDATA:
  277. fintek->rem--;
  278. init_ir_raw_event(&rawir);
  279. rawir.pulse = ((sample & BUF_PULSE_BIT) != 0);
  280. rawir.duration = US_TO_NS((sample & BUF_SAMPLE_MASK)
  281. * CIR_SAMPLE_PERIOD);
  282. fit_dbg("Storing %s with duration %d",
  283. rawir.pulse ? "pulse" : "space",
  284. rawir.duration);
  285. if (ir_raw_event_store_with_filter(fintek->rdev,
  286. &rawir))
  287. event = true;
  288. break;
  289. }
  290. if ((fintek->parser_state != CMD_HEADER) && !fintek->rem)
  291. fintek->parser_state = CMD_HEADER;
  292. }
  293. fintek->pkts = 0;
  294. if (event) {
  295. fit_dbg("Calling ir_raw_event_handle");
  296. ir_raw_event_handle(fintek->rdev);
  297. }
  298. }
  299. /* copy data from hardware rx register into driver buffer */
  300. static void fintek_get_rx_ir_data(struct fintek_dev *fintek, u8 rx_irqs)
  301. {
  302. unsigned long flags;
  303. u8 sample, status;
  304. spin_lock_irqsave(&fintek->fintek_lock, flags);
  305. /*
  306. * We must read data from CIR_RX_DATA until the hardware IR buffer
  307. * is empty and clears the RX_TIMEOUT and/or RX_RECEIVE flags in
  308. * the CIR_STATUS register
  309. */
  310. do {
  311. sample = fintek_cir_reg_read(fintek, CIR_RX_DATA);
  312. fit_dbg("%s: sample: 0x%02x", __func__, sample);
  313. fintek->buf[fintek->pkts] = sample;
  314. fintek->pkts++;
  315. status = fintek_cir_reg_read(fintek, CIR_STATUS);
  316. if (!(status & CIR_STATUS_IRQ_EN))
  317. break;
  318. } while (status & rx_irqs);
  319. fintek_process_rx_ir_data(fintek);
  320. spin_unlock_irqrestore(&fintek->fintek_lock, flags);
  321. }
  322. static void fintek_cir_log_irqs(u8 status)
  323. {
  324. fit_pr(KERN_INFO, "IRQ 0x%02x:%s%s%s%s%s", status,
  325. status & CIR_STATUS_IRQ_EN ? " IRQEN" : "",
  326. status & CIR_STATUS_TX_FINISH ? " TXF" : "",
  327. status & CIR_STATUS_TX_UNDERRUN ? " TXU" : "",
  328. status & CIR_STATUS_RX_TIMEOUT ? " RXTO" : "",
  329. status & CIR_STATUS_RX_RECEIVE ? " RXOK" : "");
  330. }
  331. /* interrupt service routine for incoming and outgoing CIR data */
  332. static irqreturn_t fintek_cir_isr(int irq, void *data)
  333. {
  334. struct fintek_dev *fintek = data;
  335. u8 status, rx_irqs;
  336. fit_dbg_verbose("%s firing", __func__);
  337. fintek_config_mode_enable(fintek);
  338. fintek_select_logical_dev(fintek, fintek->logical_dev_cir);
  339. fintek_config_mode_disable(fintek);
  340. /*
  341. * Get IR Status register contents. Write 1 to ack/clear
  342. *
  343. * bit: reg name - description
  344. * 3: TX_FINISH - TX is finished
  345. * 2: TX_UNDERRUN - TX underrun
  346. * 1: RX_TIMEOUT - RX data timeout
  347. * 0: RX_RECEIVE - RX data received
  348. */
  349. status = fintek_cir_reg_read(fintek, CIR_STATUS);
  350. if (!(status & CIR_STATUS_IRQ_MASK) || status == 0xff) {
  351. fit_dbg_verbose("%s exiting, IRSTS 0x%02x", __func__, status);
  352. fintek_cir_reg_write(fintek, CIR_STATUS_IRQ_MASK, CIR_STATUS);
  353. return IRQ_RETVAL(IRQ_NONE);
  354. }
  355. if (debug)
  356. fintek_cir_log_irqs(status);
  357. rx_irqs = status & (CIR_STATUS_RX_RECEIVE | CIR_STATUS_RX_TIMEOUT);
  358. if (rx_irqs)
  359. fintek_get_rx_ir_data(fintek, rx_irqs);
  360. /* ack/clear all irq flags we've got */
  361. fintek_cir_reg_write(fintek, status, CIR_STATUS);
  362. fit_dbg_verbose("%s done", __func__);
  363. return IRQ_RETVAL(IRQ_HANDLED);
  364. }
  365. static void fintek_enable_cir(struct fintek_dev *fintek)
  366. {
  367. /* set IRQ enabled */
  368. fintek_cir_reg_write(fintek, CIR_STATUS_IRQ_EN, CIR_STATUS);
  369. fintek_config_mode_enable(fintek);
  370. /* enable the CIR logical device */
  371. fintek_select_logical_dev(fintek, fintek->logical_dev_cir);
  372. fintek_cr_write(fintek, LOGICAL_DEV_ENABLE, CIR_CR_DEV_EN);
  373. fintek_config_mode_disable(fintek);
  374. /* clear all pending interrupts */
  375. fintek_cir_reg_write(fintek, CIR_STATUS_IRQ_MASK, CIR_STATUS);
  376. /* enable interrupts */
  377. fintek_enable_cir_irq(fintek);
  378. }
  379. static void fintek_disable_cir(struct fintek_dev *fintek)
  380. {
  381. fintek_config_mode_enable(fintek);
  382. /* disable the CIR logical device */
  383. fintek_select_logical_dev(fintek, fintek->logical_dev_cir);
  384. fintek_cr_write(fintek, LOGICAL_DEV_DISABLE, CIR_CR_DEV_EN);
  385. fintek_config_mode_disable(fintek);
  386. }
  387. static int fintek_open(struct rc_dev *dev)
  388. {
  389. struct fintek_dev *fintek = dev->priv;
  390. unsigned long flags;
  391. spin_lock_irqsave(&fintek->fintek_lock, flags);
  392. fintek_enable_cir(fintek);
  393. spin_unlock_irqrestore(&fintek->fintek_lock, flags);
  394. return 0;
  395. }
  396. static void fintek_close(struct rc_dev *dev)
  397. {
  398. struct fintek_dev *fintek = dev->priv;
  399. unsigned long flags;
  400. spin_lock_irqsave(&fintek->fintek_lock, flags);
  401. fintek_disable_cir(fintek);
  402. spin_unlock_irqrestore(&fintek->fintek_lock, flags);
  403. }
  404. /* Allocate memory, probe hardware, and initialize everything */
  405. static int fintek_probe(struct pnp_dev *pdev, const struct pnp_device_id *dev_id)
  406. {
  407. struct fintek_dev *fintek;
  408. struct rc_dev *rdev;
  409. int ret = -ENOMEM;
  410. fintek = kzalloc(sizeof(struct fintek_dev), GFP_KERNEL);
  411. if (!fintek)
  412. return ret;
  413. /* input device for IR remote (and tx) */
  414. rdev = rc_allocate_device();
  415. if (!rdev)
  416. goto exit_free_dev_rdev;
  417. ret = -ENODEV;
  418. /* validate pnp resources */
  419. if (!pnp_port_valid(pdev, 0)) {
  420. dev_err(&pdev->dev, "IR PNP Port not valid!\n");
  421. goto exit_free_dev_rdev;
  422. }
  423. if (!pnp_irq_valid(pdev, 0)) {
  424. dev_err(&pdev->dev, "IR PNP IRQ not valid!\n");
  425. goto exit_free_dev_rdev;
  426. }
  427. fintek->cir_addr = pnp_port_start(pdev, 0);
  428. fintek->cir_irq = pnp_irq(pdev, 0);
  429. fintek->cir_port_len = pnp_port_len(pdev, 0);
  430. fintek->cr_ip = CR_INDEX_PORT;
  431. fintek->cr_dp = CR_DATA_PORT;
  432. spin_lock_init(&fintek->fintek_lock);
  433. pnp_set_drvdata(pdev, fintek);
  434. fintek->pdev = pdev;
  435. ret = fintek_hw_detect(fintek);
  436. if (ret)
  437. goto exit_free_dev_rdev;
  438. /* Initialize CIR & CIR Wake Logical Devices */
  439. fintek_config_mode_enable(fintek);
  440. fintek_cir_ldev_init(fintek);
  441. fintek_config_mode_disable(fintek);
  442. /* Initialize CIR & CIR Wake Config Registers */
  443. fintek_cir_regs_init(fintek);
  444. /* Set up the rc device */
  445. rdev->priv = fintek;
  446. rdev->driver_type = RC_DRIVER_IR_RAW;
  447. rdev->allowed_protocols = RC_BIT_ALL;
  448. rdev->open = fintek_open;
  449. rdev->close = fintek_close;
  450. rdev->input_name = FINTEK_DESCRIPTION;
  451. rdev->input_phys = "fintek/cir0";
  452. rdev->input_id.bustype = BUS_HOST;
  453. rdev->input_id.vendor = VENDOR_ID_FINTEK;
  454. rdev->input_id.product = fintek->chip_major;
  455. rdev->input_id.version = fintek->chip_minor;
  456. rdev->dev.parent = &pdev->dev;
  457. rdev->driver_name = FINTEK_DRIVER_NAME;
  458. rdev->map_name = RC_MAP_RC6_MCE;
  459. rdev->timeout = US_TO_NS(1000);
  460. /* rx resolution is hardwired to 50us atm, 1, 25, 100 also possible */
  461. rdev->rx_resolution = US_TO_NS(CIR_SAMPLE_PERIOD);
  462. fintek->rdev = rdev;
  463. ret = -EBUSY;
  464. /* now claim resources */
  465. if (!request_region(fintek->cir_addr,
  466. fintek->cir_port_len, FINTEK_DRIVER_NAME))
  467. goto exit_free_dev_rdev;
  468. if (request_irq(fintek->cir_irq, fintek_cir_isr, IRQF_SHARED,
  469. FINTEK_DRIVER_NAME, (void *)fintek))
  470. goto exit_free_cir_addr;
  471. ret = rc_register_device(rdev);
  472. if (ret)
  473. goto exit_free_irq;
  474. device_init_wakeup(&pdev->dev, true);
  475. fit_pr(KERN_NOTICE, "driver has been successfully loaded\n");
  476. if (debug)
  477. cir_dump_regs(fintek);
  478. return 0;
  479. exit_free_irq:
  480. free_irq(fintek->cir_irq, fintek);
  481. exit_free_cir_addr:
  482. release_region(fintek->cir_addr, fintek->cir_port_len);
  483. exit_free_dev_rdev:
  484. rc_free_device(rdev);
  485. kfree(fintek);
  486. return ret;
  487. }
  488. static void fintek_remove(struct pnp_dev *pdev)
  489. {
  490. struct fintek_dev *fintek = pnp_get_drvdata(pdev);
  491. unsigned long flags;
  492. spin_lock_irqsave(&fintek->fintek_lock, flags);
  493. /* disable CIR */
  494. fintek_disable_cir(fintek);
  495. fintek_cir_reg_write(fintek, CIR_STATUS_IRQ_MASK, CIR_STATUS);
  496. /* enable CIR Wake (for IR power-on) */
  497. fintek_enable_wake(fintek);
  498. spin_unlock_irqrestore(&fintek->fintek_lock, flags);
  499. /* free resources */
  500. free_irq(fintek->cir_irq, fintek);
  501. release_region(fintek->cir_addr, fintek->cir_port_len);
  502. rc_unregister_device(fintek->rdev);
  503. kfree(fintek);
  504. }
  505. static int fintek_suspend(struct pnp_dev *pdev, pm_message_t state)
  506. {
  507. struct fintek_dev *fintek = pnp_get_drvdata(pdev);
  508. unsigned long flags;
  509. fit_dbg("%s called", __func__);
  510. spin_lock_irqsave(&fintek->fintek_lock, flags);
  511. /* disable all CIR interrupts */
  512. fintek_cir_reg_write(fintek, CIR_STATUS_IRQ_MASK, CIR_STATUS);
  513. spin_unlock_irqrestore(&fintek->fintek_lock, flags);
  514. fintek_config_mode_enable(fintek);
  515. /* disable cir logical dev */
  516. fintek_select_logical_dev(fintek, fintek->logical_dev_cir);
  517. fintek_cr_write(fintek, LOGICAL_DEV_DISABLE, CIR_CR_DEV_EN);
  518. fintek_config_mode_disable(fintek);
  519. /* make sure wake is enabled */
  520. fintek_enable_wake(fintek);
  521. return 0;
  522. }
  523. static int fintek_resume(struct pnp_dev *pdev)
  524. {
  525. struct fintek_dev *fintek = pnp_get_drvdata(pdev);
  526. fit_dbg("%s called", __func__);
  527. /* open interrupt */
  528. fintek_enable_cir_irq(fintek);
  529. /* Enable CIR logical device */
  530. fintek_config_mode_enable(fintek);
  531. fintek_select_logical_dev(fintek, fintek->logical_dev_cir);
  532. fintek_cr_write(fintek, LOGICAL_DEV_ENABLE, CIR_CR_DEV_EN);
  533. fintek_config_mode_disable(fintek);
  534. fintek_cir_regs_init(fintek);
  535. return 0;
  536. }
  537. static void fintek_shutdown(struct pnp_dev *pdev)
  538. {
  539. struct fintek_dev *fintek = pnp_get_drvdata(pdev);
  540. fintek_enable_wake(fintek);
  541. }
  542. static const struct pnp_device_id fintek_ids[] = {
  543. { "FIT0002", 0 }, /* CIR */
  544. { "", 0 },
  545. };
  546. static struct pnp_driver fintek_driver = {
  547. .name = FINTEK_DRIVER_NAME,
  548. .id_table = fintek_ids,
  549. .flags = PNP_DRIVER_RES_DO_NOT_CHANGE,
  550. .probe = fintek_probe,
  551. .remove = fintek_remove,
  552. .suspend = fintek_suspend,
  553. .resume = fintek_resume,
  554. .shutdown = fintek_shutdown,
  555. };
  556. module_param(debug, int, S_IRUGO | S_IWUSR);
  557. MODULE_PARM_DESC(debug, "Enable debugging output");
  558. MODULE_DEVICE_TABLE(pnp, fintek_ids);
  559. MODULE_DESCRIPTION(FINTEK_DESCRIPTION " driver");
  560. MODULE_AUTHOR("Jarod Wilson <jarod@redhat.com>");
  561. MODULE_LICENSE("GPL");
  562. module_pnp_driver(fintek_driver);