winbond-cir.c 33 KB

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