winbond-cir.c 33 KB

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