winbond-cir.c 33 KB

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