fintek-cir.c 18 KB

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