winbond-cir.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  1. /*
  2. * winbond-cir.c - Driver for the Consumer IR functionality of Winbond
  3. * SuperI/O chips.
  4. *
  5. * Currently supports the Winbond WPCD376i chip (PNP id WEC1022), but
  6. * could probably support others (Winbond WEC102X, NatSemi, etc)
  7. * with minor modifications.
  8. *
  9. * Original Author: David Härdeman <david@hardeman.nu>
  10. * Copyright (C) 2012 Sean Young <sean@mess.org>
  11. * Copyright (C) 2009 - 2011 David Härdeman <david@hardeman.nu>
  12. *
  13. * Dedicated to my daughter Matilda, without whose loving attention this
  14. * driver would have been finished in half the time and with a fraction
  15. * of the bugs.
  16. *
  17. * Written using:
  18. * o Winbond WPCD376I datasheet helpfully provided by Jesse Barnes at Intel
  19. * o NatSemi PC87338/PC97338 datasheet (for the serial port stuff)
  20. * o DSDT dumps
  21. *
  22. * Supported features:
  23. * o IR Receive
  24. * o IR Transmit
  25. * o Wake-On-CIR functionality
  26. * o Carrier detection
  27. *
  28. * This program is free software; you can redistribute it and/or modify
  29. * it under the terms of the GNU General Public License as published by
  30. * the Free Software Foundation; either version 2 of the License, or
  31. * (at your option) any later version.
  32. *
  33. * This program is distributed in the hope that it will be useful,
  34. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  36. * GNU General Public License for more details.
  37. */
  38. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  39. #include <linux/module.h>
  40. #include <linux/pnp.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/timer.h>
  43. #include <linux/leds.h>
  44. #include <linux/spinlock.h>
  45. #include <linux/pci_ids.h>
  46. #include <linux/io.h>
  47. #include <linux/bitrev.h>
  48. #include <linux/slab.h>
  49. #include <linux/wait.h>
  50. #include <linux/sched.h>
  51. #include <media/rc-core.h>
  52. #define DRVNAME "winbond-cir"
  53. /* CEIR Wake-Up Registers, relative to data->wbase */
  54. #define WBCIR_REG_WCEIR_CTL 0x03 /* CEIR Receiver Control */
  55. #define WBCIR_REG_WCEIR_STS 0x04 /* CEIR Receiver Status */
  56. #define WBCIR_REG_WCEIR_EV_EN 0x05 /* CEIR Receiver Event Enable */
  57. #define WBCIR_REG_WCEIR_CNTL 0x06 /* CEIR Receiver Counter Low */
  58. #define WBCIR_REG_WCEIR_CNTH 0x07 /* CEIR Receiver Counter High */
  59. #define WBCIR_REG_WCEIR_INDEX 0x08 /* CEIR Receiver Index */
  60. #define WBCIR_REG_WCEIR_DATA 0x09 /* CEIR Receiver Data */
  61. #define WBCIR_REG_WCEIR_CSL 0x0A /* CEIR Re. Compare Strlen */
  62. #define WBCIR_REG_WCEIR_CFG1 0x0B /* CEIR Re. Configuration 1 */
  63. #define WBCIR_REG_WCEIR_CFG2 0x0C /* CEIR Re. Configuration 2 */
  64. /* CEIR Enhanced Functionality Registers, relative to data->ebase */
  65. #define WBCIR_REG_ECEIR_CTS 0x00 /* Enhanced IR Control Status */
  66. #define WBCIR_REG_ECEIR_CCTL 0x01 /* Infrared Counter Control */
  67. #define WBCIR_REG_ECEIR_CNT_LO 0x02 /* Infrared Counter LSB */
  68. #define WBCIR_REG_ECEIR_CNT_HI 0x03 /* Infrared Counter MSB */
  69. #define WBCIR_REG_ECEIR_IREM 0x04 /* Infrared Emitter Status */
  70. /* SP3 Banked Registers, relative to data->sbase */
  71. #define WBCIR_REG_SP3_BSR 0x03 /* Bank Select, all banks */
  72. /* Bank 0 */
  73. #define WBCIR_REG_SP3_RXDATA 0x00 /* FIFO RX data (r) */
  74. #define WBCIR_REG_SP3_TXDATA 0x00 /* FIFO TX data (w) */
  75. #define WBCIR_REG_SP3_IER 0x01 /* Interrupt Enable */
  76. #define WBCIR_REG_SP3_EIR 0x02 /* Event Identification (r) */
  77. #define WBCIR_REG_SP3_FCR 0x02 /* FIFO Control (w) */
  78. #define WBCIR_REG_SP3_MCR 0x04 /* Mode Control */
  79. #define WBCIR_REG_SP3_LSR 0x05 /* Link Status */
  80. #define WBCIR_REG_SP3_MSR 0x06 /* Modem Status */
  81. #define WBCIR_REG_SP3_ASCR 0x07 /* Aux Status and Control */
  82. /* Bank 2 */
  83. #define WBCIR_REG_SP3_BGDL 0x00 /* Baud Divisor LSB */
  84. #define WBCIR_REG_SP3_BGDH 0x01 /* Baud Divisor MSB */
  85. #define WBCIR_REG_SP3_EXCR1 0x02 /* Extended Control 1 */
  86. #define WBCIR_REG_SP3_EXCR2 0x04 /* Extended Control 2 */
  87. #define WBCIR_REG_SP3_TXFLV 0x06 /* TX FIFO Level */
  88. #define WBCIR_REG_SP3_RXFLV 0x07 /* RX FIFO Level */
  89. /* Bank 3 */
  90. #define WBCIR_REG_SP3_MRID 0x00 /* Module Identification */
  91. #define WBCIR_REG_SP3_SH_LCR 0x01 /* LCR Shadow */
  92. #define WBCIR_REG_SP3_SH_FCR 0x02 /* FCR Shadow */
  93. /* Bank 4 */
  94. #define WBCIR_REG_SP3_IRCR1 0x02 /* Infrared Control 1 */
  95. /* Bank 5 */
  96. #define WBCIR_REG_SP3_IRCR2 0x04 /* Infrared Control 2 */
  97. /* Bank 6 */
  98. #define WBCIR_REG_SP3_IRCR3 0x00 /* Infrared Control 3 */
  99. #define WBCIR_REG_SP3_SIR_PW 0x02 /* SIR Pulse Width */
  100. /* Bank 7 */
  101. #define WBCIR_REG_SP3_IRRXDC 0x00 /* IR RX Demod Control */
  102. #define WBCIR_REG_SP3_IRTXMC 0x01 /* IR TX Mod Control */
  103. #define WBCIR_REG_SP3_RCCFG 0x02 /* CEIR Config */
  104. #define WBCIR_REG_SP3_IRCFG1 0x04 /* Infrared Config 1 */
  105. #define WBCIR_REG_SP3_IRCFG4 0x07 /* Infrared Config 4 */
  106. /*
  107. * Magic values follow
  108. */
  109. /* No interrupts for WBCIR_REG_SP3_IER and WBCIR_REG_SP3_EIR */
  110. #define WBCIR_IRQ_NONE 0x00
  111. /* RX data bit for WBCIR_REG_SP3_IER and WBCIR_REG_SP3_EIR */
  112. #define WBCIR_IRQ_RX 0x01
  113. /* TX data low bit for WBCIR_REG_SP3_IER and WBCIR_REG_SP3_EIR */
  114. #define WBCIR_IRQ_TX_LOW 0x02
  115. /* Over/Under-flow bit for WBCIR_REG_SP3_IER and WBCIR_REG_SP3_EIR */
  116. #define WBCIR_IRQ_ERR 0x04
  117. /* TX data empty bit for WBCEIR_REG_SP3_IER and WBCIR_REG_SP3_EIR */
  118. #define WBCIR_IRQ_TX_EMPTY 0x20
  119. /* Led enable/disable bit for WBCIR_REG_ECEIR_CTS */
  120. #define WBCIR_LED_ENABLE 0x80
  121. /* RX data available bit for WBCIR_REG_SP3_LSR */
  122. #define WBCIR_RX_AVAIL 0x01
  123. /* RX data overrun error bit for WBCIR_REG_SP3_LSR */
  124. #define WBCIR_RX_OVERRUN 0x02
  125. /* TX End-Of-Transmission bit for WBCIR_REG_SP3_ASCR */
  126. #define WBCIR_TX_EOT 0x04
  127. /* RX disable bit for WBCIR_REG_SP3_ASCR */
  128. #define WBCIR_RX_DISABLE 0x20
  129. /* TX data underrun error bit for WBCIR_REG_SP3_ASCR */
  130. #define WBCIR_TX_UNDERRUN 0x40
  131. /* Extended mode enable bit for WBCIR_REG_SP3_EXCR1 */
  132. #define WBCIR_EXT_ENABLE 0x01
  133. /* Select compare register in WBCIR_REG_WCEIR_INDEX (bits 5 & 6) */
  134. #define WBCIR_REGSEL_COMPARE 0x10
  135. /* Select mask register in WBCIR_REG_WCEIR_INDEX (bits 5 & 6) */
  136. #define WBCIR_REGSEL_MASK 0x20
  137. /* Starting address of selected register in WBCIR_REG_WCEIR_INDEX */
  138. #define WBCIR_REG_ADDR0 0x00
  139. /* Enable carrier counter */
  140. #define WBCIR_CNTR_EN 0x01
  141. /* Reset carrier counter */
  142. #define WBCIR_CNTR_R 0x02
  143. /* Invert TX */
  144. #define WBCIR_IRTX_INV 0x04
  145. /* Receiver oversampling */
  146. #define WBCIR_RX_T_OV 0x40
  147. /* Valid banks for the SP3 UART */
  148. enum wbcir_bank {
  149. WBCIR_BANK_0 = 0x00,
  150. WBCIR_BANK_1 = 0x80,
  151. WBCIR_BANK_2 = 0xE0,
  152. WBCIR_BANK_3 = 0xE4,
  153. WBCIR_BANK_4 = 0xE8,
  154. WBCIR_BANK_5 = 0xEC,
  155. WBCIR_BANK_6 = 0xF0,
  156. WBCIR_BANK_7 = 0xF4,
  157. };
  158. /* Supported power-on IR Protocols */
  159. enum wbcir_protocol {
  160. IR_PROTOCOL_RC5 = 0x0,
  161. IR_PROTOCOL_NEC = 0x1,
  162. IR_PROTOCOL_RC6 = 0x2,
  163. };
  164. /* Possible states for IR reception */
  165. enum wbcir_rxstate {
  166. WBCIR_RXSTATE_INACTIVE = 0,
  167. WBCIR_RXSTATE_ACTIVE,
  168. WBCIR_RXSTATE_ERROR
  169. };
  170. /* Possible states for IR transmission */
  171. enum wbcir_txstate {
  172. WBCIR_TXSTATE_INACTIVE = 0,
  173. WBCIR_TXSTATE_ACTIVE,
  174. WBCIR_TXSTATE_ERROR
  175. };
  176. /* Misc */
  177. #define WBCIR_NAME "Winbond CIR"
  178. #define WBCIR_ID_FAMILY 0xF1 /* Family ID for the WPCD376I */
  179. #define WBCIR_ID_CHIP 0x04 /* Chip ID for the WPCD376I */
  180. #define WAKEUP_IOMEM_LEN 0x10 /* Wake-Up I/O Reg Len */
  181. #define EHFUNC_IOMEM_LEN 0x10 /* Enhanced Func I/O Reg Len */
  182. #define SP_IOMEM_LEN 0x08 /* Serial Port 3 (IR) Reg Len */
  183. /* Per-device data */
  184. struct wbcir_data {
  185. spinlock_t spinlock;
  186. struct rc_dev *dev;
  187. struct led_classdev led;
  188. unsigned long wbase; /* Wake-Up Baseaddr */
  189. unsigned long ebase; /* Enhanced Func. Baseaddr */
  190. unsigned long sbase; /* Serial Port Baseaddr */
  191. unsigned int irq; /* Serial Port IRQ */
  192. u8 irqmask;
  193. /* RX state */
  194. enum wbcir_rxstate rxstate;
  195. int carrier_report_enabled;
  196. u32 pulse_duration;
  197. /* TX state */
  198. enum wbcir_txstate txstate;
  199. u32 txlen;
  200. u32 txoff;
  201. u32 *txbuf;
  202. u8 txmask;
  203. u32 txcarrier;
  204. };
  205. static bool invert; /* default = 0 */
  206. module_param(invert, bool, 0444);
  207. MODULE_PARM_DESC(invert, "Invert the signal from the IR receiver");
  208. static bool txandrx; /* default = 0 */
  209. module_param(txandrx, bool, 0444);
  210. MODULE_PARM_DESC(txandrx, "Allow simultaneous TX and RX");
  211. /*****************************************************************************
  212. *
  213. * UTILITY FUNCTIONS
  214. *
  215. *****************************************************************************/
  216. /* Caller needs to hold wbcir_lock */
  217. static void
  218. wbcir_set_bits(unsigned long addr, u8 bits, u8 mask)
  219. {
  220. u8 val;
  221. val = inb(addr);
  222. val = ((val & ~mask) | (bits & mask));
  223. outb(val, addr);
  224. }
  225. /* Selects the register bank for the serial port */
  226. static inline void
  227. wbcir_select_bank(struct wbcir_data *data, enum wbcir_bank bank)
  228. {
  229. outb(bank, data->sbase + WBCIR_REG_SP3_BSR);
  230. }
  231. static inline void
  232. wbcir_set_irqmask(struct wbcir_data *data, u8 irqmask)
  233. {
  234. if (data->irqmask == irqmask)
  235. return;
  236. wbcir_select_bank(data, WBCIR_BANK_0);
  237. outb(irqmask, data->sbase + WBCIR_REG_SP3_IER);
  238. data->irqmask = irqmask;
  239. }
  240. static enum led_brightness
  241. wbcir_led_brightness_get(struct led_classdev *led_cdev)
  242. {
  243. struct wbcir_data *data = container_of(led_cdev,
  244. struct wbcir_data,
  245. led);
  246. if (inb(data->ebase + WBCIR_REG_ECEIR_CTS) & WBCIR_LED_ENABLE)
  247. return LED_FULL;
  248. else
  249. return LED_OFF;
  250. }
  251. static void
  252. wbcir_led_brightness_set(struct led_classdev *led_cdev,
  253. enum led_brightness brightness)
  254. {
  255. struct wbcir_data *data = container_of(led_cdev,
  256. struct wbcir_data,
  257. led);
  258. wbcir_set_bits(data->ebase + WBCIR_REG_ECEIR_CTS,
  259. brightness == LED_OFF ? 0x00 : WBCIR_LED_ENABLE,
  260. WBCIR_LED_ENABLE);
  261. }
  262. /* Manchester encodes bits to RC6 message cells (see wbcir_shutdown) */
  263. static u8
  264. wbcir_to_rc6cells(u8 val)
  265. {
  266. u8 coded = 0x00;
  267. int i;
  268. val &= 0x0F;
  269. for (i = 0; i < 4; i++) {
  270. if (val & 0x01)
  271. coded |= 0x02 << (i * 2);
  272. else
  273. coded |= 0x01 << (i * 2);
  274. val >>= 1;
  275. }
  276. return coded;
  277. }
  278. /*****************************************************************************
  279. *
  280. * INTERRUPT FUNCTIONS
  281. *
  282. *****************************************************************************/
  283. static void
  284. wbcir_carrier_report(struct wbcir_data *data)
  285. {
  286. unsigned counter = inb(data->ebase + WBCIR_REG_ECEIR_CNT_LO) |
  287. inb(data->ebase + WBCIR_REG_ECEIR_CNT_HI) << 8;
  288. if (counter > 0 && counter < 0xffff) {
  289. DEFINE_IR_RAW_EVENT(ev);
  290. ev.carrier_report = 1;
  291. ev.carrier = DIV_ROUND_CLOSEST(counter * 1000000u,
  292. data->pulse_duration);
  293. ir_raw_event_store(data->dev, &ev);
  294. }
  295. /* reset and restart the counter */
  296. data->pulse_duration = 0;
  297. wbcir_set_bits(data->ebase + WBCIR_REG_ECEIR_CCTL, WBCIR_CNTR_R,
  298. WBCIR_CNTR_EN | WBCIR_CNTR_R);
  299. wbcir_set_bits(data->ebase + WBCIR_REG_ECEIR_CCTL, WBCIR_CNTR_EN,
  300. WBCIR_CNTR_EN | WBCIR_CNTR_R);
  301. }
  302. static void
  303. wbcir_idle_rx(struct rc_dev *dev, bool idle)
  304. {
  305. struct wbcir_data *data = dev->priv;
  306. if (!idle && data->rxstate == WBCIR_RXSTATE_INACTIVE)
  307. data->rxstate = WBCIR_RXSTATE_ACTIVE;
  308. if (idle && data->rxstate != WBCIR_RXSTATE_INACTIVE) {
  309. data->rxstate = WBCIR_RXSTATE_INACTIVE;
  310. if (data->carrier_report_enabled)
  311. wbcir_carrier_report(data);
  312. /* Tell hardware to go idle by setting RXINACTIVE */
  313. outb(WBCIR_RX_DISABLE, data->sbase + WBCIR_REG_SP3_ASCR);
  314. }
  315. }
  316. static void
  317. wbcir_irq_rx(struct wbcir_data *data, struct pnp_dev *device)
  318. {
  319. u8 irdata;
  320. DEFINE_IR_RAW_EVENT(rawir);
  321. unsigned duration;
  322. /* Since RXHDLEV is set, at least 8 bytes are in the FIFO */
  323. while (inb(data->sbase + WBCIR_REG_SP3_LSR) & WBCIR_RX_AVAIL) {
  324. irdata = inb(data->sbase + WBCIR_REG_SP3_RXDATA);
  325. if (data->rxstate == WBCIR_RXSTATE_ERROR)
  326. continue;
  327. duration = ((irdata & 0x7F) + 1) *
  328. (data->carrier_report_enabled ? 2 : 10);
  329. rawir.pulse = irdata & 0x80 ? false : true;
  330. rawir.duration = US_TO_NS(duration);
  331. if (rawir.pulse)
  332. data->pulse_duration += duration;
  333. ir_raw_event_store_with_filter(data->dev, &rawir);
  334. }
  335. ir_raw_event_handle(data->dev);
  336. }
  337. static void
  338. wbcir_irq_tx(struct wbcir_data *data)
  339. {
  340. unsigned int space;
  341. unsigned int used;
  342. u8 bytes[16];
  343. u8 byte;
  344. if (!data->txbuf)
  345. return;
  346. switch (data->txstate) {
  347. case WBCIR_TXSTATE_INACTIVE:
  348. /* TX FIFO empty */
  349. space = 16;
  350. break;
  351. case WBCIR_TXSTATE_ACTIVE:
  352. /* TX FIFO low (3 bytes or less) */
  353. space = 13;
  354. break;
  355. case WBCIR_TXSTATE_ERROR:
  356. space = 0;
  357. break;
  358. default:
  359. return;
  360. }
  361. /*
  362. * TX data is run-length coded in bytes: YXXXXXXX
  363. * Y = space (1) or pulse (0)
  364. * X = duration, encoded as (X + 1) * 10us (i.e 10 to 1280 us)
  365. */
  366. for (used = 0; used < space && data->txoff != data->txlen; used++) {
  367. if (data->txbuf[data->txoff] == 0) {
  368. data->txoff++;
  369. continue;
  370. }
  371. byte = min((u32)0x80, data->txbuf[data->txoff]);
  372. data->txbuf[data->txoff] -= byte;
  373. byte--;
  374. byte |= (data->txoff % 2 ? 0x80 : 0x00); /* pulse/space */
  375. bytes[used] = byte;
  376. }
  377. while (data->txbuf[data->txoff] == 0 && data->txoff != data->txlen)
  378. data->txoff++;
  379. if (used == 0) {
  380. /* Finished */
  381. if (data->txstate == WBCIR_TXSTATE_ERROR)
  382. /* Clear TX underrun bit */
  383. outb(WBCIR_TX_UNDERRUN, data->sbase + WBCIR_REG_SP3_ASCR);
  384. wbcir_set_irqmask(data, WBCIR_IRQ_RX | WBCIR_IRQ_ERR);
  385. kfree(data->txbuf);
  386. data->txbuf = NULL;
  387. data->txstate = WBCIR_TXSTATE_INACTIVE;
  388. } else if (data->txoff == data->txlen) {
  389. /* At the end of transmission, tell the hw before last byte */
  390. outsb(data->sbase + WBCIR_REG_SP3_TXDATA, bytes, used - 1);
  391. outb(WBCIR_TX_EOT, data->sbase + WBCIR_REG_SP3_ASCR);
  392. outb(bytes[used - 1], data->sbase + WBCIR_REG_SP3_TXDATA);
  393. wbcir_set_irqmask(data, WBCIR_IRQ_RX | WBCIR_IRQ_ERR |
  394. WBCIR_IRQ_TX_EMPTY);
  395. } else {
  396. /* More data to follow... */
  397. outsb(data->sbase + WBCIR_REG_SP3_RXDATA, bytes, used);
  398. if (data->txstate == WBCIR_TXSTATE_INACTIVE) {
  399. wbcir_set_irqmask(data, WBCIR_IRQ_RX | WBCIR_IRQ_ERR |
  400. WBCIR_IRQ_TX_LOW);
  401. data->txstate = WBCIR_TXSTATE_ACTIVE;
  402. }
  403. }
  404. }
  405. static irqreturn_t
  406. wbcir_irq_handler(int irqno, void *cookie)
  407. {
  408. struct pnp_dev *device = cookie;
  409. struct wbcir_data *data = pnp_get_drvdata(device);
  410. unsigned long flags;
  411. u8 status;
  412. spin_lock_irqsave(&data->spinlock, flags);
  413. wbcir_select_bank(data, WBCIR_BANK_0);
  414. status = inb(data->sbase + WBCIR_REG_SP3_EIR);
  415. status &= data->irqmask;
  416. if (!status) {
  417. spin_unlock_irqrestore(&data->spinlock, flags);
  418. return IRQ_NONE;
  419. }
  420. if (status & WBCIR_IRQ_ERR) {
  421. /* RX overflow? (read clears bit) */
  422. if (inb(data->sbase + WBCIR_REG_SP3_LSR) & WBCIR_RX_OVERRUN) {
  423. data->rxstate = WBCIR_RXSTATE_ERROR;
  424. ir_raw_event_reset(data->dev);
  425. }
  426. /* TX underflow? */
  427. if (inb(data->sbase + WBCIR_REG_SP3_ASCR) & WBCIR_TX_UNDERRUN)
  428. data->txstate = WBCIR_TXSTATE_ERROR;
  429. }
  430. if (status & WBCIR_IRQ_RX)
  431. wbcir_irq_rx(data, device);
  432. if (status & (WBCIR_IRQ_TX_LOW | WBCIR_IRQ_TX_EMPTY))
  433. wbcir_irq_tx(data);
  434. spin_unlock_irqrestore(&data->spinlock, flags);
  435. return IRQ_HANDLED;
  436. }
  437. /*****************************************************************************
  438. *
  439. * RC-CORE INTERFACE FUNCTIONS
  440. *
  441. *****************************************************************************/
  442. static int
  443. wbcir_set_carrier_report(struct rc_dev *dev, int enable)
  444. {
  445. struct wbcir_data *data = dev->priv;
  446. unsigned long flags;
  447. spin_lock_irqsave(&data->spinlock, flags);
  448. if (data->carrier_report_enabled == enable) {
  449. spin_unlock_irqrestore(&data->spinlock, flags);
  450. return 0;
  451. }
  452. data->pulse_duration = 0;
  453. wbcir_set_bits(data->ebase + WBCIR_REG_ECEIR_CCTL, WBCIR_CNTR_R,
  454. WBCIR_CNTR_EN | WBCIR_CNTR_R);
  455. if (enable && data->dev->idle)
  456. wbcir_set_bits(data->ebase + WBCIR_REG_ECEIR_CCTL,
  457. WBCIR_CNTR_EN, WBCIR_CNTR_EN | WBCIR_CNTR_R);
  458. /* Set a higher sampling resolution if carrier reports are enabled */
  459. wbcir_select_bank(data, WBCIR_BANK_2);
  460. data->dev->rx_resolution = US_TO_NS(enable ? 2 : 10);
  461. outb(enable ? 0x03 : 0x0f, data->sbase + WBCIR_REG_SP3_BGDL);
  462. outb(0x00, data->sbase + WBCIR_REG_SP3_BGDH);
  463. /* Enable oversampling if carrier reports are enabled */
  464. wbcir_select_bank(data, WBCIR_BANK_7);
  465. wbcir_set_bits(data->sbase + WBCIR_REG_SP3_RCCFG,
  466. enable ? WBCIR_RX_T_OV : 0, WBCIR_RX_T_OV);
  467. data->carrier_report_enabled = enable;
  468. spin_unlock_irqrestore(&data->spinlock, flags);
  469. return 0;
  470. }
  471. static int
  472. wbcir_txcarrier(struct rc_dev *dev, u32 carrier)
  473. {
  474. struct wbcir_data *data = dev->priv;
  475. unsigned long flags;
  476. u8 val;
  477. u32 freq;
  478. freq = DIV_ROUND_CLOSEST(carrier, 1000);
  479. if (freq < 30 || freq > 60)
  480. return -EINVAL;
  481. switch (freq) {
  482. case 58:
  483. case 59:
  484. case 60:
  485. val = freq - 58;
  486. freq *= 1000;
  487. break;
  488. case 57:
  489. val = freq - 27;
  490. freq = 56900;
  491. break;
  492. default:
  493. val = freq - 27;
  494. freq *= 1000;
  495. break;
  496. }
  497. spin_lock_irqsave(&data->spinlock, flags);
  498. if (data->txstate != WBCIR_TXSTATE_INACTIVE) {
  499. spin_unlock_irqrestore(&data->spinlock, flags);
  500. return -EBUSY;
  501. }
  502. if (data->txcarrier != freq) {
  503. wbcir_select_bank(data, WBCIR_BANK_7);
  504. wbcir_set_bits(data->sbase + WBCIR_REG_SP3_IRTXMC, val, 0x1F);
  505. data->txcarrier = freq;
  506. }
  507. spin_unlock_irqrestore(&data->spinlock, flags);
  508. return 0;
  509. }
  510. static int
  511. wbcir_txmask(struct rc_dev *dev, u32 mask)
  512. {
  513. struct wbcir_data *data = dev->priv;
  514. unsigned long flags;
  515. u8 val;
  516. /* return the number of transmitters */
  517. if (mask > 15)
  518. return 4;
  519. /* Four outputs, only one output can be enabled at a time */
  520. switch (mask) {
  521. case 0x1:
  522. val = 0x0;
  523. break;
  524. case 0x2:
  525. val = 0x1;
  526. break;
  527. case 0x4:
  528. val = 0x2;
  529. break;
  530. case 0x8:
  531. val = 0x3;
  532. break;
  533. default:
  534. return -EINVAL;
  535. }
  536. spin_lock_irqsave(&data->spinlock, flags);
  537. if (data->txstate != WBCIR_TXSTATE_INACTIVE) {
  538. spin_unlock_irqrestore(&data->spinlock, flags);
  539. return -EBUSY;
  540. }
  541. if (data->txmask != mask) {
  542. wbcir_set_bits(data->ebase + WBCIR_REG_ECEIR_CTS, val, 0x0c);
  543. data->txmask = mask;
  544. }
  545. spin_unlock_irqrestore(&data->spinlock, flags);
  546. return 0;
  547. }
  548. static int
  549. wbcir_tx(struct rc_dev *dev, unsigned *b, unsigned count)
  550. {
  551. struct wbcir_data *data = dev->priv;
  552. unsigned *buf;
  553. unsigned i;
  554. unsigned long flags;
  555. buf = kmalloc_array(count, sizeof(*b), GFP_KERNEL);
  556. if (!buf)
  557. return -ENOMEM;
  558. /* Convert values to multiples of 10us */
  559. for (i = 0; i < count; i++)
  560. buf[i] = DIV_ROUND_CLOSEST(b[i], 10);
  561. /* Not sure if this is possible, but better safe than sorry */
  562. spin_lock_irqsave(&data->spinlock, flags);
  563. if (data->txstate != WBCIR_TXSTATE_INACTIVE) {
  564. spin_unlock_irqrestore(&data->spinlock, flags);
  565. kfree(buf);
  566. return -EBUSY;
  567. }
  568. /* Fill the TX fifo once, the irq handler will do the rest */
  569. data->txbuf = buf;
  570. data->txlen = count;
  571. data->txoff = 0;
  572. wbcir_irq_tx(data);
  573. /* We're done */
  574. spin_unlock_irqrestore(&data->spinlock, flags);
  575. return count;
  576. }
  577. /*****************************************************************************
  578. *
  579. * SETUP/INIT/SUSPEND/RESUME FUNCTIONS
  580. *
  581. *****************************************************************************/
  582. static void
  583. wbcir_shutdown(struct pnp_dev *device)
  584. {
  585. struct device *dev = &device->dev;
  586. struct wbcir_data *data = pnp_get_drvdata(device);
  587. struct rc_dev *rc = data->dev;
  588. bool do_wake = true;
  589. u8 match[11];
  590. u8 mask[11];
  591. u8 rc6_csl = 0;
  592. u8 proto;
  593. u32 wake_sc = rc->scancode_wakeup_filter.data;
  594. u32 mask_sc = rc->scancode_wakeup_filter.mask;
  595. int i;
  596. memset(match, 0, sizeof(match));
  597. memset(mask, 0, sizeof(mask));
  598. if (!mask_sc || !device_may_wakeup(dev)) {
  599. do_wake = false;
  600. goto finish;
  601. }
  602. switch (rc->wakeup_protocol) {
  603. case RC_TYPE_RC5:
  604. /* Mask = 13 bits, ex toggle */
  605. mask[0] = (mask_sc & 0x003f);
  606. mask[0] |= (mask_sc & 0x0300) >> 2;
  607. mask[1] = (mask_sc & 0x1c00) >> 10;
  608. if (mask_sc & 0x0040) /* 2nd start bit */
  609. match[1] |= 0x10;
  610. match[0] = (wake_sc & 0x003F); /* 6 command bits */
  611. match[0] |= (wake_sc & 0x0300) >> 2; /* 2 address bits */
  612. match[1] = (wake_sc & 0x1c00) >> 10; /* 3 address bits */
  613. if (!(wake_sc & 0x0040)) /* 2nd start bit */
  614. match[1] |= 0x10;
  615. proto = IR_PROTOCOL_RC5;
  616. break;
  617. case RC_TYPE_NEC:
  618. mask[1] = bitrev8(mask_sc);
  619. mask[0] = mask[1];
  620. mask[3] = bitrev8(mask_sc >> 8);
  621. mask[2] = mask[3];
  622. match[1] = bitrev8(wake_sc);
  623. match[0] = ~match[1];
  624. match[3] = bitrev8(wake_sc >> 8);
  625. match[2] = ~match[3];
  626. proto = IR_PROTOCOL_NEC;
  627. break;
  628. case RC_TYPE_NECX:
  629. mask[1] = bitrev8(mask_sc);
  630. mask[0] = mask[1];
  631. mask[2] = bitrev8(mask_sc >> 8);
  632. mask[3] = bitrev8(mask_sc >> 16);
  633. match[1] = bitrev8(wake_sc);
  634. match[0] = ~match[1];
  635. match[2] = bitrev8(wake_sc >> 8);
  636. match[3] = bitrev8(wake_sc >> 16);
  637. proto = IR_PROTOCOL_NEC;
  638. break;
  639. case RC_TYPE_NEC32:
  640. mask[0] = bitrev8(mask_sc);
  641. mask[1] = bitrev8(mask_sc >> 8);
  642. mask[2] = bitrev8(mask_sc >> 16);
  643. mask[3] = bitrev8(mask_sc >> 24);
  644. match[0] = bitrev8(wake_sc);
  645. match[1] = bitrev8(wake_sc >> 8);
  646. match[2] = bitrev8(wake_sc >> 16);
  647. match[3] = bitrev8(wake_sc >> 24);
  648. proto = IR_PROTOCOL_NEC;
  649. break;
  650. case RC_TYPE_RC6_0:
  651. /* Command */
  652. match[0] = wbcir_to_rc6cells(wake_sc >> 0);
  653. mask[0] = wbcir_to_rc6cells(mask_sc >> 0);
  654. match[1] = wbcir_to_rc6cells(wake_sc >> 4);
  655. mask[1] = wbcir_to_rc6cells(mask_sc >> 4);
  656. /* Address */
  657. match[2] = wbcir_to_rc6cells(wake_sc >> 8);
  658. mask[2] = wbcir_to_rc6cells(mask_sc >> 8);
  659. match[3] = wbcir_to_rc6cells(wake_sc >> 12);
  660. mask[3] = wbcir_to_rc6cells(mask_sc >> 12);
  661. /* Header */
  662. match[4] = 0x50; /* mode1 = mode0 = 0, ignore toggle */
  663. mask[4] = 0xF0;
  664. match[5] = 0x09; /* start bit = 1, mode2 = 0 */
  665. mask[5] = 0x0F;
  666. rc6_csl = 44;
  667. proto = IR_PROTOCOL_RC6;
  668. break;
  669. case RC_TYPE_RC6_6A_24:
  670. case RC_TYPE_RC6_6A_32:
  671. case RC_TYPE_RC6_MCE:
  672. i = 0;
  673. /* Command */
  674. match[i] = wbcir_to_rc6cells(wake_sc >> 0);
  675. mask[i++] = wbcir_to_rc6cells(mask_sc >> 0);
  676. match[i] = wbcir_to_rc6cells(wake_sc >> 4);
  677. mask[i++] = wbcir_to_rc6cells(mask_sc >> 4);
  678. /* Address + Toggle */
  679. match[i] = wbcir_to_rc6cells(wake_sc >> 8);
  680. mask[i++] = wbcir_to_rc6cells(mask_sc >> 8);
  681. match[i] = wbcir_to_rc6cells(wake_sc >> 12);
  682. mask[i++] = wbcir_to_rc6cells(mask_sc >> 12);
  683. /* Customer bits 7 - 0 */
  684. match[i] = wbcir_to_rc6cells(wake_sc >> 16);
  685. mask[i++] = wbcir_to_rc6cells(mask_sc >> 16);
  686. if (rc->wakeup_protocol == RC_TYPE_RC6_6A_20) {
  687. rc6_csl = 52;
  688. } else {
  689. match[i] = wbcir_to_rc6cells(wake_sc >> 20);
  690. mask[i++] = wbcir_to_rc6cells(mask_sc >> 20);
  691. if (rc->wakeup_protocol == RC_TYPE_RC6_6A_24) {
  692. rc6_csl = 60;
  693. } else {
  694. /* Customer range bit and bits 15 - 8 */
  695. match[i] = wbcir_to_rc6cells(wake_sc >> 24);
  696. mask[i++] = wbcir_to_rc6cells(mask_sc >> 24);
  697. match[i] = wbcir_to_rc6cells(wake_sc >> 28);
  698. mask[i++] = wbcir_to_rc6cells(mask_sc >> 28);
  699. rc6_csl = 76;
  700. }
  701. }
  702. /* Header */
  703. match[i] = 0x93; /* mode1 = mode0 = 1, submode = 0 */
  704. mask[i++] = 0xFF;
  705. match[i] = 0x0A; /* start bit = 1, mode2 = 1 */
  706. mask[i++] = 0x0F;
  707. proto = IR_PROTOCOL_RC6;
  708. break;
  709. default:
  710. do_wake = false;
  711. break;
  712. }
  713. finish:
  714. if (do_wake) {
  715. /* Set compare and compare mask */
  716. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_INDEX,
  717. WBCIR_REGSEL_COMPARE | WBCIR_REG_ADDR0,
  718. 0x3F);
  719. outsb(data->wbase + WBCIR_REG_WCEIR_DATA, match, 11);
  720. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_INDEX,
  721. WBCIR_REGSEL_MASK | WBCIR_REG_ADDR0,
  722. 0x3F);
  723. outsb(data->wbase + WBCIR_REG_WCEIR_DATA, mask, 11);
  724. /* RC6 Compare String Len */
  725. outb(rc6_csl, data->wbase + WBCIR_REG_WCEIR_CSL);
  726. /* Clear status bits NEC_REP, BUFF, MSG_END, MATCH */
  727. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_STS, 0x17, 0x17);
  728. /* Clear BUFF_EN, Clear END_EN, Set MATCH_EN */
  729. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x01, 0x07);
  730. /* Set CEIR_EN */
  731. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CTL,
  732. (proto << 4) | 0x01, 0x31);
  733. } else {
  734. /* Clear BUFF_EN, Clear END_EN, Clear MATCH_EN */
  735. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x00, 0x07);
  736. /* Clear CEIR_EN */
  737. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CTL, 0x00, 0x01);
  738. }
  739. /*
  740. * ACPI will set the HW disable bit for SP3 which means that the
  741. * output signals are left in an undefined state which may cause
  742. * spurious interrupts which we need to ignore until the hardware
  743. * is reinitialized.
  744. */
  745. wbcir_set_irqmask(data, WBCIR_IRQ_NONE);
  746. disable_irq(data->irq);
  747. }
  748. /*
  749. * Wakeup handling is done on shutdown.
  750. */
  751. static int
  752. wbcir_set_wakeup_filter(struct rc_dev *rc, struct rc_scancode_filter *filter)
  753. {
  754. return 0;
  755. }
  756. static int
  757. wbcir_suspend(struct pnp_dev *device, pm_message_t state)
  758. {
  759. struct wbcir_data *data = pnp_get_drvdata(device);
  760. led_classdev_suspend(&data->led);
  761. wbcir_shutdown(device);
  762. return 0;
  763. }
  764. static void
  765. wbcir_init_hw(struct wbcir_data *data)
  766. {
  767. /* Disable interrupts */
  768. wbcir_set_irqmask(data, WBCIR_IRQ_NONE);
  769. /* Set RX_INV, Clear CEIR_EN (needed for the led) */
  770. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CTL, invert ? 8 : 0, 0x09);
  771. /* Clear status bits NEC_REP, BUFF, MSG_END, MATCH */
  772. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_STS, 0x17, 0x17);
  773. /* Clear BUFF_EN, Clear END_EN, Clear MATCH_EN */
  774. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x00, 0x07);
  775. /* Set RC5 cell time to correspond to 36 kHz */
  776. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CFG1, 0x4A, 0x7F);
  777. /* Set IRTX_INV */
  778. if (invert)
  779. outb(WBCIR_IRTX_INV, data->ebase + WBCIR_REG_ECEIR_CCTL);
  780. else
  781. outb(0x00, data->ebase + WBCIR_REG_ECEIR_CCTL);
  782. /*
  783. * Clear IR LED, set SP3 clock to 24Mhz, set TX mask to IRTX1,
  784. * set SP3_IRRX_SW to binary 01, helpfully not documented
  785. */
  786. outb(0x10, data->ebase + WBCIR_REG_ECEIR_CTS);
  787. data->txmask = 0x1;
  788. /* Enable extended mode */
  789. wbcir_select_bank(data, WBCIR_BANK_2);
  790. outb(WBCIR_EXT_ENABLE, data->sbase + WBCIR_REG_SP3_EXCR1);
  791. /*
  792. * Configure baud generator, IR data will be sampled at
  793. * a bitrate of: (24Mhz * prescaler) / (divisor * 16).
  794. *
  795. * The ECIR registers include a flag to change the
  796. * 24Mhz clock freq to 48Mhz.
  797. *
  798. * It's not documented in the specs, but fifo levels
  799. * other than 16 seems to be unsupported.
  800. */
  801. /* prescaler 1.0, tx/rx fifo lvl 16 */
  802. outb(0x30, data->sbase + WBCIR_REG_SP3_EXCR2);
  803. /* Set baud divisor to sample every 10 us */
  804. outb(0x0f, data->sbase + WBCIR_REG_SP3_BGDL);
  805. outb(0x00, data->sbase + WBCIR_REG_SP3_BGDH);
  806. /* Set CEIR mode */
  807. wbcir_select_bank(data, WBCIR_BANK_0);
  808. outb(0xC0, data->sbase + WBCIR_REG_SP3_MCR);
  809. inb(data->sbase + WBCIR_REG_SP3_LSR); /* Clear LSR */
  810. inb(data->sbase + WBCIR_REG_SP3_MSR); /* Clear MSR */
  811. /* Disable RX demod, enable run-length enc/dec, set freq span */
  812. wbcir_select_bank(data, WBCIR_BANK_7);
  813. outb(0x90, data->sbase + WBCIR_REG_SP3_RCCFG);
  814. /* Disable timer */
  815. wbcir_select_bank(data, WBCIR_BANK_4);
  816. outb(0x00, data->sbase + WBCIR_REG_SP3_IRCR1);
  817. /* Disable MSR interrupt, clear AUX_IRX, mask RX during TX? */
  818. wbcir_select_bank(data, WBCIR_BANK_5);
  819. outb(txandrx ? 0x03 : 0x02, data->sbase + WBCIR_REG_SP3_IRCR2);
  820. /* Disable CRC */
  821. wbcir_select_bank(data, WBCIR_BANK_6);
  822. outb(0x20, data->sbase + WBCIR_REG_SP3_IRCR3);
  823. /* Set RX demodulation freq, not really used */
  824. wbcir_select_bank(data, WBCIR_BANK_7);
  825. outb(0xF2, data->sbase + WBCIR_REG_SP3_IRRXDC);
  826. /* Set TX modulation, 36kHz, 7us pulse width */
  827. outb(0x69, data->sbase + WBCIR_REG_SP3_IRTXMC);
  828. data->txcarrier = 36000;
  829. /* Set invert and pin direction */
  830. if (invert)
  831. outb(0x10, data->sbase + WBCIR_REG_SP3_IRCFG4);
  832. else
  833. outb(0x00, data->sbase + WBCIR_REG_SP3_IRCFG4);
  834. /* Set FIFO thresholds (RX = 8, TX = 3), reset RX/TX */
  835. wbcir_select_bank(data, WBCIR_BANK_0);
  836. outb(0x97, data->sbase + WBCIR_REG_SP3_FCR);
  837. /* Clear AUX status bits */
  838. outb(0xE0, data->sbase + WBCIR_REG_SP3_ASCR);
  839. /* Clear RX state */
  840. data->rxstate = WBCIR_RXSTATE_INACTIVE;
  841. ir_raw_event_reset(data->dev);
  842. ir_raw_event_set_idle(data->dev, true);
  843. /* Clear TX state */
  844. if (data->txstate == WBCIR_TXSTATE_ACTIVE) {
  845. kfree(data->txbuf);
  846. data->txbuf = NULL;
  847. data->txstate = WBCIR_TXSTATE_INACTIVE;
  848. }
  849. /* Enable interrupts */
  850. wbcir_set_irqmask(data, WBCIR_IRQ_RX | WBCIR_IRQ_ERR);
  851. }
  852. static int
  853. wbcir_resume(struct pnp_dev *device)
  854. {
  855. struct wbcir_data *data = pnp_get_drvdata(device);
  856. wbcir_init_hw(data);
  857. enable_irq(data->irq);
  858. led_classdev_resume(&data->led);
  859. return 0;
  860. }
  861. static int
  862. wbcir_probe(struct pnp_dev *device, const struct pnp_device_id *dev_id)
  863. {
  864. struct device *dev = &device->dev;
  865. struct wbcir_data *data;
  866. int err;
  867. if (!(pnp_port_len(device, 0) == EHFUNC_IOMEM_LEN &&
  868. pnp_port_len(device, 1) == WAKEUP_IOMEM_LEN &&
  869. pnp_port_len(device, 2) == SP_IOMEM_LEN)) {
  870. dev_err(dev, "Invalid resources\n");
  871. return -ENODEV;
  872. }
  873. data = kzalloc(sizeof(*data), GFP_KERNEL);
  874. if (!data) {
  875. err = -ENOMEM;
  876. goto exit;
  877. }
  878. pnp_set_drvdata(device, data);
  879. spin_lock_init(&data->spinlock);
  880. data->ebase = pnp_port_start(device, 0);
  881. data->wbase = pnp_port_start(device, 1);
  882. data->sbase = pnp_port_start(device, 2);
  883. data->irq = pnp_irq(device, 0);
  884. if (data->wbase == 0 || data->ebase == 0 ||
  885. data->sbase == 0 || data->irq == 0) {
  886. err = -ENODEV;
  887. dev_err(dev, "Invalid resources\n");
  888. goto exit_free_data;
  889. }
  890. dev_dbg(&device->dev, "Found device (w: 0x%lX, e: 0x%lX, s: 0x%lX, i: %u)\n",
  891. data->wbase, data->ebase, data->sbase, data->irq);
  892. data->led.name = "cir::activity";
  893. data->led.default_trigger = "rc-feedback";
  894. data->led.brightness_set = wbcir_led_brightness_set;
  895. data->led.brightness_get = wbcir_led_brightness_get;
  896. err = led_classdev_register(&device->dev, &data->led);
  897. if (err)
  898. goto exit_free_data;
  899. data->dev = rc_allocate_device(RC_DRIVER_IR_RAW);
  900. if (!data->dev) {
  901. err = -ENOMEM;
  902. goto exit_unregister_led;
  903. }
  904. data->dev->driver_name = DRVNAME;
  905. data->dev->input_name = WBCIR_NAME;
  906. data->dev->input_phys = "wbcir/cir0";
  907. data->dev->input_id.bustype = BUS_HOST;
  908. data->dev->input_id.vendor = PCI_VENDOR_ID_WINBOND;
  909. data->dev->input_id.product = WBCIR_ID_FAMILY;
  910. data->dev->input_id.version = WBCIR_ID_CHIP;
  911. data->dev->map_name = RC_MAP_RC6_MCE;
  912. data->dev->s_idle = wbcir_idle_rx;
  913. data->dev->s_carrier_report = wbcir_set_carrier_report;
  914. data->dev->s_tx_mask = wbcir_txmask;
  915. data->dev->s_tx_carrier = wbcir_txcarrier;
  916. data->dev->tx_ir = wbcir_tx;
  917. data->dev->priv = data;
  918. data->dev->dev.parent = &device->dev;
  919. data->dev->min_timeout = 1;
  920. data->dev->timeout = IR_DEFAULT_TIMEOUT;
  921. data->dev->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
  922. data->dev->rx_resolution = US_TO_NS(2);
  923. data->dev->allowed_protocols = RC_BIT_ALL_IR_DECODER;
  924. data->dev->allowed_wakeup_protocols = RC_BIT_NEC | RC_BIT_NECX |
  925. RC_BIT_NEC32 | RC_BIT_RC5 | RC_BIT_RC6_0 |
  926. RC_BIT_RC6_6A_20 | RC_BIT_RC6_6A_24 |
  927. RC_BIT_RC6_6A_32 | RC_BIT_RC6_MCE;
  928. data->dev->wakeup_protocol = RC_TYPE_RC6_MCE;
  929. data->dev->scancode_wakeup_filter.data = 0x800f040c;
  930. data->dev->scancode_wakeup_filter.mask = 0xffff7fff;
  931. data->dev->s_wakeup_filter = wbcir_set_wakeup_filter;
  932. err = rc_register_device(data->dev);
  933. if (err)
  934. goto exit_free_rc;
  935. if (!request_region(data->wbase, WAKEUP_IOMEM_LEN, DRVNAME)) {
  936. dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
  937. data->wbase, data->wbase + WAKEUP_IOMEM_LEN - 1);
  938. err = -EBUSY;
  939. goto exit_unregister_device;
  940. }
  941. if (!request_region(data->ebase, EHFUNC_IOMEM_LEN, DRVNAME)) {
  942. dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
  943. data->ebase, data->ebase + EHFUNC_IOMEM_LEN - 1);
  944. err = -EBUSY;
  945. goto exit_release_wbase;
  946. }
  947. if (!request_region(data->sbase, SP_IOMEM_LEN, DRVNAME)) {
  948. dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
  949. data->sbase, data->sbase + SP_IOMEM_LEN - 1);
  950. err = -EBUSY;
  951. goto exit_release_ebase;
  952. }
  953. err = request_irq(data->irq, wbcir_irq_handler,
  954. 0, DRVNAME, device);
  955. if (err) {
  956. dev_err(dev, "Failed to claim IRQ %u\n", data->irq);
  957. err = -EBUSY;
  958. goto exit_release_sbase;
  959. }
  960. device_init_wakeup(&device->dev, 1);
  961. wbcir_init_hw(data);
  962. return 0;
  963. exit_release_sbase:
  964. release_region(data->sbase, SP_IOMEM_LEN);
  965. exit_release_ebase:
  966. release_region(data->ebase, EHFUNC_IOMEM_LEN);
  967. exit_release_wbase:
  968. release_region(data->wbase, WAKEUP_IOMEM_LEN);
  969. exit_unregister_device:
  970. rc_unregister_device(data->dev);
  971. data->dev = NULL;
  972. exit_free_rc:
  973. rc_free_device(data->dev);
  974. exit_unregister_led:
  975. led_classdev_unregister(&data->led);
  976. exit_free_data:
  977. kfree(data);
  978. pnp_set_drvdata(device, NULL);
  979. exit:
  980. return err;
  981. }
  982. static void
  983. wbcir_remove(struct pnp_dev *device)
  984. {
  985. struct wbcir_data *data = pnp_get_drvdata(device);
  986. /* Disable interrupts */
  987. wbcir_set_irqmask(data, WBCIR_IRQ_NONE);
  988. free_irq(data->irq, device);
  989. /* Clear status bits NEC_REP, BUFF, MSG_END, MATCH */
  990. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_STS, 0x17, 0x17);
  991. /* Clear CEIR_EN */
  992. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CTL, 0x00, 0x01);
  993. /* Clear BUFF_EN, END_EN, MATCH_EN */
  994. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x00, 0x07);
  995. rc_unregister_device(data->dev);
  996. led_classdev_unregister(&data->led);
  997. /* This is ok since &data->led isn't actually used */
  998. wbcir_led_brightness_set(&data->led, LED_OFF);
  999. release_region(data->wbase, WAKEUP_IOMEM_LEN);
  1000. release_region(data->ebase, EHFUNC_IOMEM_LEN);
  1001. release_region(data->sbase, SP_IOMEM_LEN);
  1002. kfree(data);
  1003. pnp_set_drvdata(device, NULL);
  1004. }
  1005. static const struct pnp_device_id wbcir_ids[] = {
  1006. { "WEC1022", 0 },
  1007. { "", 0 }
  1008. };
  1009. MODULE_DEVICE_TABLE(pnp, wbcir_ids);
  1010. static struct pnp_driver wbcir_driver = {
  1011. .name = DRVNAME,
  1012. .id_table = wbcir_ids,
  1013. .probe = wbcir_probe,
  1014. .remove = wbcir_remove,
  1015. .suspend = wbcir_suspend,
  1016. .resume = wbcir_resume,
  1017. .shutdown = wbcir_shutdown
  1018. };
  1019. static int __init
  1020. wbcir_init(void)
  1021. {
  1022. int ret;
  1023. ret = pnp_register_driver(&wbcir_driver);
  1024. if (ret)
  1025. pr_err("Unable to register driver\n");
  1026. return ret;
  1027. }
  1028. static void __exit
  1029. wbcir_exit(void)
  1030. {
  1031. pnp_unregister_driver(&wbcir_driver);
  1032. }
  1033. module_init(wbcir_init);
  1034. module_exit(wbcir_exit);
  1035. MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
  1036. MODULE_DESCRIPTION("Winbond SuperI/O Consumer IR Driver");
  1037. MODULE_LICENSE("GPL");