winbond-cir.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  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. struct ir_raw_event ev = {
  290. .carrier_report = 1,
  291. .carrier = DIV_ROUND_CLOSEST(counter * 1000000u,
  292. data->pulse_duration)
  293. };
  294. ir_raw_event_store(data->dev, &ev);
  295. }
  296. /* reset and restart the counter */
  297. data->pulse_duration = 0;
  298. wbcir_set_bits(data->ebase + WBCIR_REG_ECEIR_CCTL, WBCIR_CNTR_R,
  299. WBCIR_CNTR_EN | WBCIR_CNTR_R);
  300. wbcir_set_bits(data->ebase + WBCIR_REG_ECEIR_CCTL, WBCIR_CNTR_EN,
  301. WBCIR_CNTR_EN | WBCIR_CNTR_R);
  302. }
  303. static void
  304. wbcir_idle_rx(struct rc_dev *dev, bool idle)
  305. {
  306. struct wbcir_data *data = dev->priv;
  307. if (!idle && data->rxstate == WBCIR_RXSTATE_INACTIVE)
  308. data->rxstate = WBCIR_RXSTATE_ACTIVE;
  309. if (idle && data->rxstate != WBCIR_RXSTATE_INACTIVE) {
  310. data->rxstate = WBCIR_RXSTATE_INACTIVE;
  311. if (data->carrier_report_enabled)
  312. wbcir_carrier_report(data);
  313. /* Tell hardware to go idle by setting RXINACTIVE */
  314. outb(WBCIR_RX_DISABLE, data->sbase + WBCIR_REG_SP3_ASCR);
  315. }
  316. }
  317. static void
  318. wbcir_irq_rx(struct wbcir_data *data, struct pnp_dev *device)
  319. {
  320. u8 irdata;
  321. struct ir_raw_event rawir = {};
  322. unsigned duration;
  323. /* Since RXHDLEV is set, at least 8 bytes are in the FIFO */
  324. while (inb(data->sbase + WBCIR_REG_SP3_LSR) & WBCIR_RX_AVAIL) {
  325. irdata = inb(data->sbase + WBCIR_REG_SP3_RXDATA);
  326. if (data->rxstate == WBCIR_RXSTATE_ERROR)
  327. continue;
  328. duration = ((irdata & 0x7F) + 1) *
  329. (data->carrier_report_enabled ? 2 : 10);
  330. rawir.pulse = irdata & 0x80 ? false : true;
  331. rawir.duration = US_TO_NS(duration);
  332. if (rawir.pulse)
  333. data->pulse_duration += duration;
  334. ir_raw_event_store_with_filter(data->dev, &rawir);
  335. }
  336. ir_raw_event_handle(data->dev);
  337. }
  338. static void
  339. wbcir_irq_tx(struct wbcir_data *data)
  340. {
  341. unsigned int space;
  342. unsigned int used;
  343. u8 bytes[16];
  344. u8 byte;
  345. if (!data->txbuf)
  346. return;
  347. switch (data->txstate) {
  348. case WBCIR_TXSTATE_INACTIVE:
  349. /* TX FIFO empty */
  350. space = 16;
  351. break;
  352. case WBCIR_TXSTATE_ACTIVE:
  353. /* TX FIFO low (3 bytes or less) */
  354. space = 13;
  355. break;
  356. case WBCIR_TXSTATE_ERROR:
  357. space = 0;
  358. break;
  359. default:
  360. return;
  361. }
  362. /*
  363. * TX data is run-length coded in bytes: YXXXXXXX
  364. * Y = space (1) or pulse (0)
  365. * X = duration, encoded as (X + 1) * 10us (i.e 10 to 1280 us)
  366. */
  367. for (used = 0; used < space && data->txoff != data->txlen; used++) {
  368. if (data->txbuf[data->txoff] == 0) {
  369. data->txoff++;
  370. continue;
  371. }
  372. byte = min((u32)0x80, data->txbuf[data->txoff]);
  373. data->txbuf[data->txoff] -= byte;
  374. byte--;
  375. byte |= (data->txoff % 2 ? 0x80 : 0x00); /* pulse/space */
  376. bytes[used] = byte;
  377. }
  378. while (data->txoff != data->txlen && data->txbuf[data->txoff] == 0)
  379. data->txoff++;
  380. if (used == 0) {
  381. /* Finished */
  382. if (data->txstate == WBCIR_TXSTATE_ERROR)
  383. /* Clear TX underrun bit */
  384. outb(WBCIR_TX_UNDERRUN, data->sbase + WBCIR_REG_SP3_ASCR);
  385. wbcir_set_irqmask(data, WBCIR_IRQ_RX | WBCIR_IRQ_ERR);
  386. kfree(data->txbuf);
  387. data->txbuf = NULL;
  388. data->txstate = WBCIR_TXSTATE_INACTIVE;
  389. } else if (data->txoff == data->txlen) {
  390. /* At the end of transmission, tell the hw before last byte */
  391. outsb(data->sbase + WBCIR_REG_SP3_TXDATA, bytes, used - 1);
  392. outb(WBCIR_TX_EOT, data->sbase + WBCIR_REG_SP3_ASCR);
  393. outb(bytes[used - 1], data->sbase + WBCIR_REG_SP3_TXDATA);
  394. wbcir_set_irqmask(data, WBCIR_IRQ_RX | WBCIR_IRQ_ERR |
  395. WBCIR_IRQ_TX_EMPTY);
  396. } else {
  397. /* More data to follow... */
  398. outsb(data->sbase + WBCIR_REG_SP3_RXDATA, bytes, used);
  399. if (data->txstate == WBCIR_TXSTATE_INACTIVE) {
  400. wbcir_set_irqmask(data, WBCIR_IRQ_RX | WBCIR_IRQ_ERR |
  401. WBCIR_IRQ_TX_LOW);
  402. data->txstate = WBCIR_TXSTATE_ACTIVE;
  403. }
  404. }
  405. }
  406. static irqreturn_t
  407. wbcir_irq_handler(int irqno, void *cookie)
  408. {
  409. struct pnp_dev *device = cookie;
  410. struct wbcir_data *data = pnp_get_drvdata(device);
  411. unsigned long flags;
  412. u8 status;
  413. spin_lock_irqsave(&data->spinlock, flags);
  414. wbcir_select_bank(data, WBCIR_BANK_0);
  415. status = inb(data->sbase + WBCIR_REG_SP3_EIR);
  416. status &= data->irqmask;
  417. if (!status) {
  418. spin_unlock_irqrestore(&data->spinlock, flags);
  419. return IRQ_NONE;
  420. }
  421. if (status & WBCIR_IRQ_ERR) {
  422. /* RX overflow? (read clears bit) */
  423. if (inb(data->sbase + WBCIR_REG_SP3_LSR) & WBCIR_RX_OVERRUN) {
  424. data->rxstate = WBCIR_RXSTATE_ERROR;
  425. ir_raw_event_reset(data->dev);
  426. }
  427. /* TX underflow? */
  428. if (inb(data->sbase + WBCIR_REG_SP3_ASCR) & WBCIR_TX_UNDERRUN)
  429. data->txstate = WBCIR_TXSTATE_ERROR;
  430. }
  431. if (status & WBCIR_IRQ_RX)
  432. wbcir_irq_rx(data, device);
  433. if (status & (WBCIR_IRQ_TX_LOW | WBCIR_IRQ_TX_EMPTY))
  434. wbcir_irq_tx(data);
  435. spin_unlock_irqrestore(&data->spinlock, flags);
  436. return IRQ_HANDLED;
  437. }
  438. /*****************************************************************************
  439. *
  440. * RC-CORE INTERFACE FUNCTIONS
  441. *
  442. *****************************************************************************/
  443. static int
  444. wbcir_set_carrier_report(struct rc_dev *dev, int enable)
  445. {
  446. struct wbcir_data *data = dev->priv;
  447. unsigned long flags;
  448. spin_lock_irqsave(&data->spinlock, flags);
  449. if (data->carrier_report_enabled == enable) {
  450. spin_unlock_irqrestore(&data->spinlock, flags);
  451. return 0;
  452. }
  453. data->pulse_duration = 0;
  454. wbcir_set_bits(data->ebase + WBCIR_REG_ECEIR_CCTL, WBCIR_CNTR_R,
  455. WBCIR_CNTR_EN | WBCIR_CNTR_R);
  456. if (enable && data->dev->idle)
  457. wbcir_set_bits(data->ebase + WBCIR_REG_ECEIR_CCTL,
  458. WBCIR_CNTR_EN, WBCIR_CNTR_EN | WBCIR_CNTR_R);
  459. /* Set a higher sampling resolution if carrier reports are enabled */
  460. wbcir_select_bank(data, WBCIR_BANK_2);
  461. data->dev->rx_resolution = US_TO_NS(enable ? 2 : 10);
  462. outb(enable ? 0x03 : 0x0f, data->sbase + WBCIR_REG_SP3_BGDL);
  463. outb(0x00, data->sbase + WBCIR_REG_SP3_BGDH);
  464. /* Enable oversampling if carrier reports are enabled */
  465. wbcir_select_bank(data, WBCIR_BANK_7);
  466. wbcir_set_bits(data->sbase + WBCIR_REG_SP3_RCCFG,
  467. enable ? WBCIR_RX_T_OV : 0, WBCIR_RX_T_OV);
  468. data->carrier_report_enabled = enable;
  469. spin_unlock_irqrestore(&data->spinlock, flags);
  470. return 0;
  471. }
  472. static int
  473. wbcir_txcarrier(struct rc_dev *dev, u32 carrier)
  474. {
  475. struct wbcir_data *data = dev->priv;
  476. unsigned long flags;
  477. u8 val;
  478. u32 freq;
  479. freq = DIV_ROUND_CLOSEST(carrier, 1000);
  480. if (freq < 30 || freq > 60)
  481. return -EINVAL;
  482. switch (freq) {
  483. case 58:
  484. case 59:
  485. case 60:
  486. val = freq - 58;
  487. freq *= 1000;
  488. break;
  489. case 57:
  490. val = freq - 27;
  491. freq = 56900;
  492. break;
  493. default:
  494. val = freq - 27;
  495. freq *= 1000;
  496. break;
  497. }
  498. spin_lock_irqsave(&data->spinlock, flags);
  499. if (data->txstate != WBCIR_TXSTATE_INACTIVE) {
  500. spin_unlock_irqrestore(&data->spinlock, flags);
  501. return -EBUSY;
  502. }
  503. if (data->txcarrier != freq) {
  504. wbcir_select_bank(data, WBCIR_BANK_7);
  505. wbcir_set_bits(data->sbase + WBCIR_REG_SP3_IRTXMC, val, 0x1F);
  506. data->txcarrier = freq;
  507. }
  508. spin_unlock_irqrestore(&data->spinlock, flags);
  509. return 0;
  510. }
  511. static int
  512. wbcir_txmask(struct rc_dev *dev, u32 mask)
  513. {
  514. struct wbcir_data *data = dev->priv;
  515. unsigned long flags;
  516. u8 val;
  517. /* return the number of transmitters */
  518. if (mask > 15)
  519. return 4;
  520. /* Four outputs, only one output can be enabled at a time */
  521. switch (mask) {
  522. case 0x1:
  523. val = 0x0;
  524. break;
  525. case 0x2:
  526. val = 0x1;
  527. break;
  528. case 0x4:
  529. val = 0x2;
  530. break;
  531. case 0x8:
  532. val = 0x3;
  533. break;
  534. default:
  535. return -EINVAL;
  536. }
  537. spin_lock_irqsave(&data->spinlock, flags);
  538. if (data->txstate != WBCIR_TXSTATE_INACTIVE) {
  539. spin_unlock_irqrestore(&data->spinlock, flags);
  540. return -EBUSY;
  541. }
  542. if (data->txmask != mask) {
  543. wbcir_set_bits(data->ebase + WBCIR_REG_ECEIR_CTS, val, 0x0c);
  544. data->txmask = mask;
  545. }
  546. spin_unlock_irqrestore(&data->spinlock, flags);
  547. return 0;
  548. }
  549. static int
  550. wbcir_tx(struct rc_dev *dev, unsigned *b, unsigned count)
  551. {
  552. struct wbcir_data *data = dev->priv;
  553. unsigned *buf;
  554. unsigned i;
  555. unsigned long flags;
  556. buf = kmalloc_array(count, sizeof(*b), GFP_KERNEL);
  557. if (!buf)
  558. return -ENOMEM;
  559. /* Convert values to multiples of 10us */
  560. for (i = 0; i < count; i++)
  561. buf[i] = DIV_ROUND_CLOSEST(b[i], 10);
  562. /* Not sure if this is possible, but better safe than sorry */
  563. spin_lock_irqsave(&data->spinlock, flags);
  564. if (data->txstate != WBCIR_TXSTATE_INACTIVE) {
  565. spin_unlock_irqrestore(&data->spinlock, flags);
  566. kfree(buf);
  567. return -EBUSY;
  568. }
  569. /* Fill the TX fifo once, the irq handler will do the rest */
  570. data->txbuf = buf;
  571. data->txlen = count;
  572. data->txoff = 0;
  573. wbcir_irq_tx(data);
  574. /* We're done */
  575. spin_unlock_irqrestore(&data->spinlock, flags);
  576. return count;
  577. }
  578. /*****************************************************************************
  579. *
  580. * SETUP/INIT/SUSPEND/RESUME FUNCTIONS
  581. *
  582. *****************************************************************************/
  583. static void
  584. wbcir_shutdown(struct pnp_dev *device)
  585. {
  586. struct device *dev = &device->dev;
  587. struct wbcir_data *data = pnp_get_drvdata(device);
  588. struct rc_dev *rc = data->dev;
  589. bool do_wake = true;
  590. u8 match[11];
  591. u8 mask[11];
  592. u8 rc6_csl = 0;
  593. u8 proto;
  594. u32 wake_sc = rc->scancode_wakeup_filter.data;
  595. u32 mask_sc = rc->scancode_wakeup_filter.mask;
  596. int i;
  597. memset(match, 0, sizeof(match));
  598. memset(mask, 0, sizeof(mask));
  599. if (!mask_sc || !device_may_wakeup(dev)) {
  600. do_wake = false;
  601. goto finish;
  602. }
  603. switch (rc->wakeup_protocol) {
  604. case RC_PROTO_RC5:
  605. /* Mask = 13 bits, ex toggle */
  606. mask[0] = (mask_sc & 0x003f);
  607. mask[0] |= (mask_sc & 0x0300) >> 2;
  608. mask[1] = (mask_sc & 0x1c00) >> 10;
  609. if (mask_sc & 0x0040) /* 2nd start bit */
  610. match[1] |= 0x10;
  611. match[0] = (wake_sc & 0x003F); /* 6 command bits */
  612. match[0] |= (wake_sc & 0x0300) >> 2; /* 2 address bits */
  613. match[1] = (wake_sc & 0x1c00) >> 10; /* 3 address bits */
  614. if (!(wake_sc & 0x0040)) /* 2nd start bit */
  615. match[1] |= 0x10;
  616. proto = IR_PROTOCOL_RC5;
  617. break;
  618. case RC_PROTO_NEC:
  619. mask[1] = bitrev8(mask_sc);
  620. mask[0] = mask[1];
  621. mask[3] = bitrev8(mask_sc >> 8);
  622. mask[2] = mask[3];
  623. match[1] = bitrev8(wake_sc);
  624. match[0] = ~match[1];
  625. match[3] = bitrev8(wake_sc >> 8);
  626. match[2] = ~match[3];
  627. proto = IR_PROTOCOL_NEC;
  628. break;
  629. case RC_PROTO_NECX:
  630. mask[1] = bitrev8(mask_sc);
  631. mask[0] = mask[1];
  632. mask[2] = bitrev8(mask_sc >> 8);
  633. mask[3] = bitrev8(mask_sc >> 16);
  634. match[1] = bitrev8(wake_sc);
  635. match[0] = ~match[1];
  636. match[2] = bitrev8(wake_sc >> 8);
  637. match[3] = bitrev8(wake_sc >> 16);
  638. proto = IR_PROTOCOL_NEC;
  639. break;
  640. case RC_PROTO_NEC32:
  641. mask[0] = bitrev8(mask_sc);
  642. mask[1] = bitrev8(mask_sc >> 8);
  643. mask[2] = bitrev8(mask_sc >> 16);
  644. mask[3] = bitrev8(mask_sc >> 24);
  645. match[0] = bitrev8(wake_sc);
  646. match[1] = bitrev8(wake_sc >> 8);
  647. match[2] = bitrev8(wake_sc >> 16);
  648. match[3] = bitrev8(wake_sc >> 24);
  649. proto = IR_PROTOCOL_NEC;
  650. break;
  651. case RC_PROTO_RC6_0:
  652. /* Command */
  653. match[0] = wbcir_to_rc6cells(wake_sc >> 0);
  654. mask[0] = wbcir_to_rc6cells(mask_sc >> 0);
  655. match[1] = wbcir_to_rc6cells(wake_sc >> 4);
  656. mask[1] = wbcir_to_rc6cells(mask_sc >> 4);
  657. /* Address */
  658. match[2] = wbcir_to_rc6cells(wake_sc >> 8);
  659. mask[2] = wbcir_to_rc6cells(mask_sc >> 8);
  660. match[3] = wbcir_to_rc6cells(wake_sc >> 12);
  661. mask[3] = wbcir_to_rc6cells(mask_sc >> 12);
  662. /* Header */
  663. match[4] = 0x50; /* mode1 = mode0 = 0, ignore toggle */
  664. mask[4] = 0xF0;
  665. match[5] = 0x09; /* start bit = 1, mode2 = 0 */
  666. mask[5] = 0x0F;
  667. rc6_csl = 44;
  668. proto = IR_PROTOCOL_RC6;
  669. break;
  670. case RC_PROTO_RC6_6A_24:
  671. case RC_PROTO_RC6_6A_32:
  672. case RC_PROTO_RC6_MCE:
  673. i = 0;
  674. /* Command */
  675. match[i] = wbcir_to_rc6cells(wake_sc >> 0);
  676. mask[i++] = wbcir_to_rc6cells(mask_sc >> 0);
  677. match[i] = wbcir_to_rc6cells(wake_sc >> 4);
  678. mask[i++] = wbcir_to_rc6cells(mask_sc >> 4);
  679. /* Address + Toggle */
  680. match[i] = wbcir_to_rc6cells(wake_sc >> 8);
  681. mask[i++] = wbcir_to_rc6cells(mask_sc >> 8);
  682. match[i] = wbcir_to_rc6cells(wake_sc >> 12);
  683. mask[i++] = wbcir_to_rc6cells(mask_sc >> 12);
  684. /* Customer bits 7 - 0 */
  685. match[i] = wbcir_to_rc6cells(wake_sc >> 16);
  686. mask[i++] = wbcir_to_rc6cells(mask_sc >> 16);
  687. if (rc->wakeup_protocol == RC_PROTO_RC6_6A_20) {
  688. rc6_csl = 52;
  689. } else {
  690. match[i] = wbcir_to_rc6cells(wake_sc >> 20);
  691. mask[i++] = wbcir_to_rc6cells(mask_sc >> 20);
  692. if (rc->wakeup_protocol == RC_PROTO_RC6_6A_24) {
  693. rc6_csl = 60;
  694. } else {
  695. /* Customer range bit and bits 15 - 8 */
  696. match[i] = wbcir_to_rc6cells(wake_sc >> 24);
  697. mask[i++] = wbcir_to_rc6cells(mask_sc >> 24);
  698. match[i] = wbcir_to_rc6cells(wake_sc >> 28);
  699. mask[i++] = wbcir_to_rc6cells(mask_sc >> 28);
  700. rc6_csl = 76;
  701. }
  702. }
  703. /* Header */
  704. match[i] = 0x93; /* mode1 = mode0 = 1, submode = 0 */
  705. mask[i++] = 0xFF;
  706. match[i] = 0x0A; /* start bit = 1, mode2 = 1 */
  707. mask[i++] = 0x0F;
  708. proto = IR_PROTOCOL_RC6;
  709. break;
  710. default:
  711. do_wake = false;
  712. break;
  713. }
  714. finish:
  715. if (do_wake) {
  716. /* Set compare and compare mask */
  717. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_INDEX,
  718. WBCIR_REGSEL_COMPARE | WBCIR_REG_ADDR0,
  719. 0x3F);
  720. outsb(data->wbase + WBCIR_REG_WCEIR_DATA, match, 11);
  721. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_INDEX,
  722. WBCIR_REGSEL_MASK | WBCIR_REG_ADDR0,
  723. 0x3F);
  724. outsb(data->wbase + WBCIR_REG_WCEIR_DATA, mask, 11);
  725. /* RC6 Compare String Len */
  726. outb(rc6_csl, data->wbase + WBCIR_REG_WCEIR_CSL);
  727. /* Clear status bits NEC_REP, BUFF, MSG_END, MATCH */
  728. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_STS, 0x17, 0x17);
  729. /* Clear BUFF_EN, Clear END_EN, Set MATCH_EN */
  730. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x01, 0x07);
  731. /* Set CEIR_EN */
  732. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CTL,
  733. (proto << 4) | 0x01, 0x31);
  734. } else {
  735. /* Clear BUFF_EN, Clear END_EN, Clear MATCH_EN */
  736. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x00, 0x07);
  737. /* Clear CEIR_EN */
  738. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CTL, 0x00, 0x01);
  739. }
  740. /*
  741. * ACPI will set the HW disable bit for SP3 which means that the
  742. * output signals are left in an undefined state which may cause
  743. * spurious interrupts which we need to ignore until the hardware
  744. * is reinitialized.
  745. */
  746. wbcir_set_irqmask(data, WBCIR_IRQ_NONE);
  747. disable_irq(data->irq);
  748. }
  749. /*
  750. * Wakeup handling is done on shutdown.
  751. */
  752. static int
  753. wbcir_set_wakeup_filter(struct rc_dev *rc, struct rc_scancode_filter *filter)
  754. {
  755. return 0;
  756. }
  757. static int
  758. wbcir_suspend(struct pnp_dev *device, pm_message_t state)
  759. {
  760. struct wbcir_data *data = pnp_get_drvdata(device);
  761. led_classdev_suspend(&data->led);
  762. wbcir_shutdown(device);
  763. return 0;
  764. }
  765. static void
  766. wbcir_init_hw(struct wbcir_data *data)
  767. {
  768. /* Disable interrupts */
  769. wbcir_set_irqmask(data, WBCIR_IRQ_NONE);
  770. /* Set RX_INV, Clear CEIR_EN (needed for the led) */
  771. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CTL, invert ? 8 : 0, 0x09);
  772. /* Clear status bits NEC_REP, BUFF, MSG_END, MATCH */
  773. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_STS, 0x17, 0x17);
  774. /* Clear BUFF_EN, Clear END_EN, Clear MATCH_EN */
  775. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x00, 0x07);
  776. /* Set RC5 cell time to correspond to 36 kHz */
  777. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CFG1, 0x4A, 0x7F);
  778. /* Set IRTX_INV */
  779. if (invert)
  780. outb(WBCIR_IRTX_INV, data->ebase + WBCIR_REG_ECEIR_CCTL);
  781. else
  782. outb(0x00, data->ebase + WBCIR_REG_ECEIR_CCTL);
  783. /*
  784. * Clear IR LED, set SP3 clock to 24Mhz, set TX mask to IRTX1,
  785. * set SP3_IRRX_SW to binary 01, helpfully not documented
  786. */
  787. outb(0x10, data->ebase + WBCIR_REG_ECEIR_CTS);
  788. data->txmask = 0x1;
  789. /* Enable extended mode */
  790. wbcir_select_bank(data, WBCIR_BANK_2);
  791. outb(WBCIR_EXT_ENABLE, data->sbase + WBCIR_REG_SP3_EXCR1);
  792. /*
  793. * Configure baud generator, IR data will be sampled at
  794. * a bitrate of: (24Mhz * prescaler) / (divisor * 16).
  795. *
  796. * The ECIR registers include a flag to change the
  797. * 24Mhz clock freq to 48Mhz.
  798. *
  799. * It's not documented in the specs, but fifo levels
  800. * other than 16 seems to be unsupported.
  801. */
  802. /* prescaler 1.0, tx/rx fifo lvl 16 */
  803. outb(0x30, data->sbase + WBCIR_REG_SP3_EXCR2);
  804. /* Set baud divisor to sample every 10 us */
  805. outb(0x0f, data->sbase + WBCIR_REG_SP3_BGDL);
  806. outb(0x00, data->sbase + WBCIR_REG_SP3_BGDH);
  807. /* Set CEIR mode */
  808. wbcir_select_bank(data, WBCIR_BANK_0);
  809. outb(0xC0, data->sbase + WBCIR_REG_SP3_MCR);
  810. inb(data->sbase + WBCIR_REG_SP3_LSR); /* Clear LSR */
  811. inb(data->sbase + WBCIR_REG_SP3_MSR); /* Clear MSR */
  812. /* Disable RX demod, enable run-length enc/dec, set freq span */
  813. wbcir_select_bank(data, WBCIR_BANK_7);
  814. outb(0x90, data->sbase + WBCIR_REG_SP3_RCCFG);
  815. /* Disable timer */
  816. wbcir_select_bank(data, WBCIR_BANK_4);
  817. outb(0x00, data->sbase + WBCIR_REG_SP3_IRCR1);
  818. /* Disable MSR interrupt, clear AUX_IRX, mask RX during TX? */
  819. wbcir_select_bank(data, WBCIR_BANK_5);
  820. outb(txandrx ? 0x03 : 0x02, data->sbase + WBCIR_REG_SP3_IRCR2);
  821. /* Disable CRC */
  822. wbcir_select_bank(data, WBCIR_BANK_6);
  823. outb(0x20, data->sbase + WBCIR_REG_SP3_IRCR3);
  824. /* Set RX demodulation freq, not really used */
  825. wbcir_select_bank(data, WBCIR_BANK_7);
  826. outb(0xF2, data->sbase + WBCIR_REG_SP3_IRRXDC);
  827. /* Set TX modulation, 36kHz, 7us pulse width */
  828. outb(0x69, data->sbase + WBCIR_REG_SP3_IRTXMC);
  829. data->txcarrier = 36000;
  830. /* Set invert and pin direction */
  831. if (invert)
  832. outb(0x10, data->sbase + WBCIR_REG_SP3_IRCFG4);
  833. else
  834. outb(0x00, data->sbase + WBCIR_REG_SP3_IRCFG4);
  835. /* Set FIFO thresholds (RX = 8, TX = 3), reset RX/TX */
  836. wbcir_select_bank(data, WBCIR_BANK_0);
  837. outb(0x97, data->sbase + WBCIR_REG_SP3_FCR);
  838. /* Clear AUX status bits */
  839. outb(0xE0, data->sbase + WBCIR_REG_SP3_ASCR);
  840. /* Clear RX state */
  841. data->rxstate = WBCIR_RXSTATE_INACTIVE;
  842. wbcir_idle_rx(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. ir_raw_event_reset(data->dev);
  858. enable_irq(data->irq);
  859. led_classdev_resume(&data->led);
  860. return 0;
  861. }
  862. static int
  863. wbcir_probe(struct pnp_dev *device, const struct pnp_device_id *dev_id)
  864. {
  865. struct device *dev = &device->dev;
  866. struct wbcir_data *data;
  867. int err;
  868. if (!(pnp_port_len(device, 0) == EHFUNC_IOMEM_LEN &&
  869. pnp_port_len(device, 1) == WAKEUP_IOMEM_LEN &&
  870. pnp_port_len(device, 2) == SP_IOMEM_LEN)) {
  871. dev_err(dev, "Invalid resources\n");
  872. return -ENODEV;
  873. }
  874. data = kzalloc(sizeof(*data), GFP_KERNEL);
  875. if (!data) {
  876. err = -ENOMEM;
  877. goto exit;
  878. }
  879. pnp_set_drvdata(device, data);
  880. spin_lock_init(&data->spinlock);
  881. data->ebase = pnp_port_start(device, 0);
  882. data->wbase = pnp_port_start(device, 1);
  883. data->sbase = pnp_port_start(device, 2);
  884. data->irq = pnp_irq(device, 0);
  885. if (data->wbase == 0 || data->ebase == 0 ||
  886. data->sbase == 0 || data->irq == -1) {
  887. err = -ENODEV;
  888. dev_err(dev, "Invalid resources\n");
  889. goto exit_free_data;
  890. }
  891. dev_dbg(&device->dev, "Found device (w: 0x%lX, e: 0x%lX, s: 0x%lX, i: %u)\n",
  892. data->wbase, data->ebase, data->sbase, data->irq);
  893. data->led.name = "cir::activity";
  894. data->led.default_trigger = "rc-feedback";
  895. data->led.brightness_set = wbcir_led_brightness_set;
  896. data->led.brightness_get = wbcir_led_brightness_get;
  897. err = led_classdev_register(&device->dev, &data->led);
  898. if (err)
  899. goto exit_free_data;
  900. data->dev = rc_allocate_device(RC_DRIVER_IR_RAW);
  901. if (!data->dev) {
  902. err = -ENOMEM;
  903. goto exit_unregister_led;
  904. }
  905. data->dev->driver_name = DRVNAME;
  906. data->dev->device_name = WBCIR_NAME;
  907. data->dev->input_phys = "wbcir/cir0";
  908. data->dev->input_id.bustype = BUS_HOST;
  909. data->dev->input_id.vendor = PCI_VENDOR_ID_WINBOND;
  910. data->dev->input_id.product = WBCIR_ID_FAMILY;
  911. data->dev->input_id.version = WBCIR_ID_CHIP;
  912. data->dev->map_name = RC_MAP_RC6_MCE;
  913. data->dev->s_idle = wbcir_idle_rx;
  914. data->dev->s_carrier_report = wbcir_set_carrier_report;
  915. data->dev->s_tx_mask = wbcir_txmask;
  916. data->dev->s_tx_carrier = wbcir_txcarrier;
  917. data->dev->tx_ir = wbcir_tx;
  918. data->dev->priv = data;
  919. data->dev->dev.parent = &device->dev;
  920. data->dev->min_timeout = 1;
  921. data->dev->timeout = IR_DEFAULT_TIMEOUT;
  922. data->dev->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
  923. data->dev->rx_resolution = US_TO_NS(2);
  924. data->dev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
  925. data->dev->allowed_wakeup_protocols = RC_PROTO_BIT_NEC |
  926. RC_PROTO_BIT_NECX | RC_PROTO_BIT_NEC32 | RC_PROTO_BIT_RC5 |
  927. RC_PROTO_BIT_RC6_0 | RC_PROTO_BIT_RC6_6A_20 |
  928. RC_PROTO_BIT_RC6_6A_24 | RC_PROTO_BIT_RC6_6A_32 |
  929. RC_PROTO_BIT_RC6_MCE;
  930. data->dev->wakeup_protocol = RC_PROTO_RC6_MCE;
  931. data->dev->scancode_wakeup_filter.data = 0x800f040c;
  932. data->dev->scancode_wakeup_filter.mask = 0xffff7fff;
  933. data->dev->s_wakeup_filter = wbcir_set_wakeup_filter;
  934. err = rc_register_device(data->dev);
  935. if (err)
  936. goto exit_free_rc;
  937. if (!request_region(data->wbase, WAKEUP_IOMEM_LEN, DRVNAME)) {
  938. dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
  939. data->wbase, data->wbase + WAKEUP_IOMEM_LEN - 1);
  940. err = -EBUSY;
  941. goto exit_unregister_device;
  942. }
  943. if (!request_region(data->ebase, EHFUNC_IOMEM_LEN, DRVNAME)) {
  944. dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
  945. data->ebase, data->ebase + EHFUNC_IOMEM_LEN - 1);
  946. err = -EBUSY;
  947. goto exit_release_wbase;
  948. }
  949. if (!request_region(data->sbase, SP_IOMEM_LEN, DRVNAME)) {
  950. dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
  951. data->sbase, data->sbase + SP_IOMEM_LEN - 1);
  952. err = -EBUSY;
  953. goto exit_release_ebase;
  954. }
  955. err = request_irq(data->irq, wbcir_irq_handler,
  956. 0, DRVNAME, device);
  957. if (err) {
  958. dev_err(dev, "Failed to claim IRQ %u\n", data->irq);
  959. err = -EBUSY;
  960. goto exit_release_sbase;
  961. }
  962. device_init_wakeup(&device->dev, 1);
  963. wbcir_init_hw(data);
  964. return 0;
  965. exit_release_sbase:
  966. release_region(data->sbase, SP_IOMEM_LEN);
  967. exit_release_ebase:
  968. release_region(data->ebase, EHFUNC_IOMEM_LEN);
  969. exit_release_wbase:
  970. release_region(data->wbase, WAKEUP_IOMEM_LEN);
  971. exit_unregister_device:
  972. rc_unregister_device(data->dev);
  973. data->dev = NULL;
  974. exit_free_rc:
  975. rc_free_device(data->dev);
  976. exit_unregister_led:
  977. led_classdev_unregister(&data->led);
  978. exit_free_data:
  979. kfree(data);
  980. pnp_set_drvdata(device, NULL);
  981. exit:
  982. return err;
  983. }
  984. static void
  985. wbcir_remove(struct pnp_dev *device)
  986. {
  987. struct wbcir_data *data = pnp_get_drvdata(device);
  988. /* Disable interrupts */
  989. wbcir_set_irqmask(data, WBCIR_IRQ_NONE);
  990. free_irq(data->irq, device);
  991. /* Clear status bits NEC_REP, BUFF, MSG_END, MATCH */
  992. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_STS, 0x17, 0x17);
  993. /* Clear CEIR_EN */
  994. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CTL, 0x00, 0x01);
  995. /* Clear BUFF_EN, END_EN, MATCH_EN */
  996. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x00, 0x07);
  997. rc_unregister_device(data->dev);
  998. led_classdev_unregister(&data->led);
  999. /* This is ok since &data->led isn't actually used */
  1000. wbcir_led_brightness_set(&data->led, LED_OFF);
  1001. release_region(data->wbase, WAKEUP_IOMEM_LEN);
  1002. release_region(data->ebase, EHFUNC_IOMEM_LEN);
  1003. release_region(data->sbase, SP_IOMEM_LEN);
  1004. kfree(data);
  1005. pnp_set_drvdata(device, NULL);
  1006. }
  1007. static const struct pnp_device_id wbcir_ids[] = {
  1008. { "WEC1022", 0 },
  1009. { "", 0 }
  1010. };
  1011. MODULE_DEVICE_TABLE(pnp, wbcir_ids);
  1012. static struct pnp_driver wbcir_driver = {
  1013. .name = DRVNAME,
  1014. .id_table = wbcir_ids,
  1015. .probe = wbcir_probe,
  1016. .remove = wbcir_remove,
  1017. .suspend = wbcir_suspend,
  1018. .resume = wbcir_resume,
  1019. .shutdown = wbcir_shutdown
  1020. };
  1021. static int __init
  1022. wbcir_init(void)
  1023. {
  1024. int ret;
  1025. ret = pnp_register_driver(&wbcir_driver);
  1026. if (ret)
  1027. pr_err("Unable to register driver\n");
  1028. return ret;
  1029. }
  1030. static void __exit
  1031. wbcir_exit(void)
  1032. {
  1033. pnp_unregister_driver(&wbcir_driver);
  1034. }
  1035. module_init(wbcir_init);
  1036. module_exit(wbcir_exit);
  1037. MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
  1038. MODULE_DESCRIPTION("Winbond SuperI/O Consumer IR Driver");
  1039. MODULE_LICENSE("GPL");