winbond-cir.c 33 KB

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