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