winbond-cir.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  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. /* Four outputs, only one output can be enabled at a time */
  533. switch (mask) {
  534. case 0x1:
  535. val = 0x0;
  536. break;
  537. case 0x2:
  538. val = 0x1;
  539. break;
  540. case 0x4:
  541. val = 0x2;
  542. break;
  543. case 0x8:
  544. val = 0x3;
  545. break;
  546. default:
  547. return -EINVAL;
  548. }
  549. spin_lock_irqsave(&data->spinlock, flags);
  550. if (data->txstate != WBCIR_TXSTATE_INACTIVE) {
  551. spin_unlock_irqrestore(&data->spinlock, flags);
  552. return -EBUSY;
  553. }
  554. if (data->txmask != mask) {
  555. wbcir_set_bits(data->ebase + WBCIR_REG_ECEIR_CTS, val, 0x0c);
  556. data->txmask = mask;
  557. }
  558. spin_unlock_irqrestore(&data->spinlock, flags);
  559. return 0;
  560. }
  561. static int
  562. wbcir_tx(struct rc_dev *dev, unsigned *b, unsigned count)
  563. {
  564. struct wbcir_data *data = dev->priv;
  565. unsigned *buf;
  566. unsigned i;
  567. unsigned long flags;
  568. buf = kmalloc(count * sizeof(*b), GFP_KERNEL);
  569. if (!buf)
  570. return -ENOMEM;
  571. /* Convert values to multiples of 10us */
  572. for (i = 0; i < count; i++)
  573. buf[i] = DIV_ROUND_CLOSEST(b[i], 10);
  574. /* Not sure if this is possible, but better safe than sorry */
  575. spin_lock_irqsave(&data->spinlock, flags);
  576. if (data->txstate != WBCIR_TXSTATE_INACTIVE) {
  577. spin_unlock_irqrestore(&data->spinlock, flags);
  578. kfree(buf);
  579. return -EBUSY;
  580. }
  581. /* Fill the TX fifo once, the irq handler will do the rest */
  582. data->txbuf = buf;
  583. data->txlen = count;
  584. data->txoff = 0;
  585. wbcir_irq_tx(data);
  586. /* We're done */
  587. spin_unlock_irqrestore(&data->spinlock, flags);
  588. return count;
  589. }
  590. /*****************************************************************************
  591. *
  592. * SETUP/INIT/SUSPEND/RESUME FUNCTIONS
  593. *
  594. *****************************************************************************/
  595. static void
  596. wbcir_shutdown(struct pnp_dev *device)
  597. {
  598. struct device *dev = &device->dev;
  599. struct wbcir_data *data = pnp_get_drvdata(device);
  600. bool do_wake = true;
  601. u8 match[11];
  602. u8 mask[11];
  603. u8 rc6_csl = 0;
  604. int i;
  605. memset(match, 0, sizeof(match));
  606. memset(mask, 0, sizeof(mask));
  607. if (wake_sc == INVALID_SCANCODE || !device_may_wakeup(dev)) {
  608. do_wake = false;
  609. goto finish;
  610. }
  611. switch (protocol) {
  612. case IR_PROTOCOL_RC5:
  613. if (wake_sc > 0xFFF) {
  614. do_wake = false;
  615. dev_err(dev, "RC5 - Invalid wake scancode\n");
  616. break;
  617. }
  618. /* Mask = 13 bits, ex toggle */
  619. mask[0] = 0xFF;
  620. mask[1] = 0x17;
  621. match[0] = (wake_sc & 0x003F); /* 6 command bits */
  622. match[0] |= (wake_sc & 0x0180) >> 1; /* 2 address bits */
  623. match[1] = (wake_sc & 0x0E00) >> 9; /* 3 address bits */
  624. if (!(wake_sc & 0x0040)) /* 2nd start bit */
  625. match[1] |= 0x10;
  626. break;
  627. case IR_PROTOCOL_NEC:
  628. if (wake_sc > 0xFFFFFF) {
  629. do_wake = false;
  630. dev_err(dev, "NEC - Invalid wake scancode\n");
  631. break;
  632. }
  633. mask[0] = mask[1] = mask[2] = mask[3] = 0xFF;
  634. match[1] = bitrev8((wake_sc & 0xFF));
  635. match[0] = ~match[1];
  636. match[3] = bitrev8((wake_sc & 0xFF00) >> 8);
  637. if (wake_sc > 0xFFFF)
  638. match[2] = bitrev8((wake_sc & 0xFF0000) >> 16);
  639. else
  640. match[2] = ~match[3];
  641. break;
  642. case IR_PROTOCOL_RC6:
  643. if (wake_rc6mode == 0) {
  644. if (wake_sc > 0xFFFF) {
  645. do_wake = false;
  646. dev_err(dev, "RC6 - Invalid wake scancode\n");
  647. break;
  648. }
  649. /* Command */
  650. match[0] = wbcir_to_rc6cells(wake_sc >> 0);
  651. mask[0] = 0xFF;
  652. match[1] = wbcir_to_rc6cells(wake_sc >> 4);
  653. mask[1] = 0xFF;
  654. /* Address */
  655. match[2] = wbcir_to_rc6cells(wake_sc >> 8);
  656. mask[2] = 0xFF;
  657. match[3] = wbcir_to_rc6cells(wake_sc >> 12);
  658. mask[3] = 0xFF;
  659. /* Header */
  660. match[4] = 0x50; /* mode1 = mode0 = 0, ignore toggle */
  661. mask[4] = 0xF0;
  662. match[5] = 0x09; /* start bit = 1, mode2 = 0 */
  663. mask[5] = 0x0F;
  664. rc6_csl = 44;
  665. } else if (wake_rc6mode == 6) {
  666. i = 0;
  667. /* Command */
  668. match[i] = wbcir_to_rc6cells(wake_sc >> 0);
  669. mask[i++] = 0xFF;
  670. match[i] = wbcir_to_rc6cells(wake_sc >> 4);
  671. mask[i++] = 0xFF;
  672. /* Address + Toggle */
  673. match[i] = wbcir_to_rc6cells(wake_sc >> 8);
  674. mask[i++] = 0xFF;
  675. match[i] = wbcir_to_rc6cells(wake_sc >> 12);
  676. mask[i++] = 0x3F;
  677. /* Customer bits 7 - 0 */
  678. match[i] = wbcir_to_rc6cells(wake_sc >> 16);
  679. mask[i++] = 0xFF;
  680. match[i] = wbcir_to_rc6cells(wake_sc >> 20);
  681. mask[i++] = 0xFF;
  682. if (wake_sc & 0x80000000) {
  683. /* Customer range bit and bits 15 - 8 */
  684. match[i] = wbcir_to_rc6cells(wake_sc >> 24);
  685. mask[i++] = 0xFF;
  686. match[i] = wbcir_to_rc6cells(wake_sc >> 28);
  687. mask[i++] = 0xFF;
  688. rc6_csl = 76;
  689. } else if (wake_sc <= 0x007FFFFF) {
  690. rc6_csl = 60;
  691. } else {
  692. do_wake = false;
  693. dev_err(dev, "RC6 - Invalid wake scancode\n");
  694. break;
  695. }
  696. /* Header */
  697. match[i] = 0x93; /* mode1 = mode0 = 1, submode = 0 */
  698. mask[i++] = 0xFF;
  699. match[i] = 0x0A; /* start bit = 1, mode2 = 1 */
  700. mask[i++] = 0x0F;
  701. } else {
  702. do_wake = false;
  703. dev_err(dev, "RC6 - Invalid wake mode\n");
  704. }
  705. break;
  706. default:
  707. do_wake = false;
  708. break;
  709. }
  710. finish:
  711. if (do_wake) {
  712. /* Set compare and compare mask */
  713. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_INDEX,
  714. WBCIR_REGSEL_COMPARE | WBCIR_REG_ADDR0,
  715. 0x3F);
  716. outsb(data->wbase + WBCIR_REG_WCEIR_DATA, match, 11);
  717. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_INDEX,
  718. WBCIR_REGSEL_MASK | WBCIR_REG_ADDR0,
  719. 0x3F);
  720. outsb(data->wbase + WBCIR_REG_WCEIR_DATA, mask, 11);
  721. /* RC6 Compare String Len */
  722. outb(rc6_csl, data->wbase + WBCIR_REG_WCEIR_CSL);
  723. /* Clear status bits NEC_REP, BUFF, MSG_END, MATCH */
  724. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_STS, 0x17, 0x17);
  725. /* Clear BUFF_EN, Clear END_EN, Set MATCH_EN */
  726. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x01, 0x07);
  727. /* Set CEIR_EN */
  728. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CTL, 0x01, 0x01);
  729. } else {
  730. /* Clear BUFF_EN, Clear END_EN, Clear MATCH_EN */
  731. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x00, 0x07);
  732. /* Clear CEIR_EN */
  733. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CTL, 0x00, 0x01);
  734. }
  735. /*
  736. * ACPI will set the HW disable bit for SP3 which means that the
  737. * output signals are left in an undefined state which may cause
  738. * spurious interrupts which we need to ignore until the hardware
  739. * is reinitialized.
  740. */
  741. wbcir_set_irqmask(data, WBCIR_IRQ_NONE);
  742. disable_irq(data->irq);
  743. }
  744. static int
  745. wbcir_suspend(struct pnp_dev *device, pm_message_t state)
  746. {
  747. struct wbcir_data *data = pnp_get_drvdata(device);
  748. led_classdev_suspend(&data->led);
  749. wbcir_shutdown(device);
  750. return 0;
  751. }
  752. static void
  753. wbcir_init_hw(struct wbcir_data *data)
  754. {
  755. u8 tmp;
  756. /* Disable interrupts */
  757. wbcir_set_irqmask(data, WBCIR_IRQ_NONE);
  758. /* Set PROT_SEL, RX_INV, Clear CEIR_EN (needed for the led) */
  759. tmp = protocol << 4;
  760. if (invert)
  761. tmp |= 0x08;
  762. outb(tmp, data->wbase + WBCIR_REG_WCEIR_CTL);
  763. /* Clear status bits NEC_REP, BUFF, MSG_END, MATCH */
  764. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_STS, 0x17, 0x17);
  765. /* Clear BUFF_EN, Clear END_EN, Clear MATCH_EN */
  766. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x00, 0x07);
  767. /* Set RC5 cell time to correspond to 36 kHz */
  768. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CFG1, 0x4A, 0x7F);
  769. /* Set IRTX_INV */
  770. if (invert)
  771. outb(WBCIR_IRTX_INV, data->ebase + WBCIR_REG_ECEIR_CCTL);
  772. else
  773. outb(0x00, data->ebase + WBCIR_REG_ECEIR_CCTL);
  774. /*
  775. * Clear IR LED, set SP3 clock to 24Mhz, set TX mask to IRTX1,
  776. * set SP3_IRRX_SW to binary 01, helpfully not documented
  777. */
  778. outb(0x10, data->ebase + WBCIR_REG_ECEIR_CTS);
  779. data->txmask = 0x1;
  780. /* Enable extended mode */
  781. wbcir_select_bank(data, WBCIR_BANK_2);
  782. outb(WBCIR_EXT_ENABLE, data->sbase + WBCIR_REG_SP3_EXCR1);
  783. /*
  784. * Configure baud generator, IR data will be sampled at
  785. * a bitrate of: (24Mhz * prescaler) / (divisor * 16).
  786. *
  787. * The ECIR registers include a flag to change the
  788. * 24Mhz clock freq to 48Mhz.
  789. *
  790. * It's not documented in the specs, but fifo levels
  791. * other than 16 seems to be unsupported.
  792. */
  793. /* prescaler 1.0, tx/rx fifo lvl 16 */
  794. outb(0x30, data->sbase + WBCIR_REG_SP3_EXCR2);
  795. /* Set baud divisor to sample every 10 us */
  796. outb(0x0f, data->sbase + WBCIR_REG_SP3_BGDL);
  797. outb(0x00, data->sbase + WBCIR_REG_SP3_BGDH);
  798. /* Set CEIR mode */
  799. wbcir_select_bank(data, WBCIR_BANK_0);
  800. outb(0xC0, data->sbase + WBCIR_REG_SP3_MCR);
  801. inb(data->sbase + WBCIR_REG_SP3_LSR); /* Clear LSR */
  802. inb(data->sbase + WBCIR_REG_SP3_MSR); /* Clear MSR */
  803. /* Disable RX demod, enable run-length enc/dec, set freq span */
  804. wbcir_select_bank(data, WBCIR_BANK_7);
  805. outb(0x90, data->sbase + WBCIR_REG_SP3_RCCFG);
  806. /* Disable timer */
  807. wbcir_select_bank(data, WBCIR_BANK_4);
  808. outb(0x00, data->sbase + WBCIR_REG_SP3_IRCR1);
  809. /* Disable MSR interrupt, clear AUX_IRX, mask RX during TX? */
  810. wbcir_select_bank(data, WBCIR_BANK_5);
  811. outb(txandrx ? 0x03 : 0x02, data->sbase + WBCIR_REG_SP3_IRCR2);
  812. /* Disable CRC */
  813. wbcir_select_bank(data, WBCIR_BANK_6);
  814. outb(0x20, data->sbase + WBCIR_REG_SP3_IRCR3);
  815. /* Set RX demodulation freq, not really used */
  816. wbcir_select_bank(data, WBCIR_BANK_7);
  817. outb(0xF2, data->sbase + WBCIR_REG_SP3_IRRXDC);
  818. /* Set TX modulation, 36kHz, 7us pulse width */
  819. outb(0x69, data->sbase + WBCIR_REG_SP3_IRTXMC);
  820. data->txcarrier = 36000;
  821. /* Set invert and pin direction */
  822. if (invert)
  823. outb(0x10, data->sbase + WBCIR_REG_SP3_IRCFG4);
  824. else
  825. outb(0x00, data->sbase + WBCIR_REG_SP3_IRCFG4);
  826. /* Set FIFO thresholds (RX = 8, TX = 3), reset RX/TX */
  827. wbcir_select_bank(data, WBCIR_BANK_0);
  828. outb(0x97, data->sbase + WBCIR_REG_SP3_FCR);
  829. /* Clear AUX status bits */
  830. outb(0xE0, data->sbase + WBCIR_REG_SP3_ASCR);
  831. /* Clear RX state */
  832. data->rxstate = WBCIR_RXSTATE_INACTIVE;
  833. ir_raw_event_reset(data->dev);
  834. ir_raw_event_set_idle(data->dev, true);
  835. /* Clear TX state */
  836. if (data->txstate == WBCIR_TXSTATE_ACTIVE) {
  837. kfree(data->txbuf);
  838. data->txbuf = NULL;
  839. data->txstate = WBCIR_TXSTATE_INACTIVE;
  840. }
  841. /* Enable interrupts */
  842. wbcir_set_irqmask(data, WBCIR_IRQ_RX | WBCIR_IRQ_ERR);
  843. }
  844. static int
  845. wbcir_resume(struct pnp_dev *device)
  846. {
  847. struct wbcir_data *data = pnp_get_drvdata(device);
  848. wbcir_init_hw(data);
  849. enable_irq(data->irq);
  850. led_classdev_resume(&data->led);
  851. return 0;
  852. }
  853. static int
  854. wbcir_probe(struct pnp_dev *device, const struct pnp_device_id *dev_id)
  855. {
  856. struct device *dev = &device->dev;
  857. struct wbcir_data *data;
  858. int err;
  859. if (!(pnp_port_len(device, 0) == EHFUNC_IOMEM_LEN &&
  860. pnp_port_len(device, 1) == WAKEUP_IOMEM_LEN &&
  861. pnp_port_len(device, 2) == SP_IOMEM_LEN)) {
  862. dev_err(dev, "Invalid resources\n");
  863. return -ENODEV;
  864. }
  865. data = kzalloc(sizeof(*data), GFP_KERNEL);
  866. if (!data) {
  867. err = -ENOMEM;
  868. goto exit;
  869. }
  870. pnp_set_drvdata(device, data);
  871. spin_lock_init(&data->spinlock);
  872. data->ebase = pnp_port_start(device, 0);
  873. data->wbase = pnp_port_start(device, 1);
  874. data->sbase = pnp_port_start(device, 2);
  875. data->irq = pnp_irq(device, 0);
  876. if (data->wbase == 0 || data->ebase == 0 ||
  877. data->sbase == 0 || data->irq == 0) {
  878. err = -ENODEV;
  879. dev_err(dev, "Invalid resources\n");
  880. goto exit_free_data;
  881. }
  882. dev_dbg(&device->dev, "Found device "
  883. "(w: 0x%lX, e: 0x%lX, s: 0x%lX, i: %u)\n",
  884. data->wbase, data->ebase, data->sbase, data->irq);
  885. data->led.name = "cir::activity";
  886. data->led.default_trigger = "rc-feedback";
  887. data->led.brightness_set = wbcir_led_brightness_set;
  888. data->led.brightness_get = wbcir_led_brightness_get;
  889. err = led_classdev_register(&device->dev, &data->led);
  890. if (err)
  891. goto exit_free_data;
  892. data->dev = rc_allocate_device();
  893. if (!data->dev) {
  894. err = -ENOMEM;
  895. goto exit_unregister_led;
  896. }
  897. data->dev->driver_type = RC_DRIVER_IR_RAW;
  898. data->dev->driver_name = DRVNAME;
  899. data->dev->input_name = WBCIR_NAME;
  900. data->dev->input_phys = "wbcir/cir0";
  901. data->dev->input_id.bustype = BUS_HOST;
  902. data->dev->input_id.vendor = PCI_VENDOR_ID_WINBOND;
  903. data->dev->input_id.product = WBCIR_ID_FAMILY;
  904. data->dev->input_id.version = WBCIR_ID_CHIP;
  905. data->dev->map_name = RC_MAP_RC6_MCE;
  906. data->dev->s_idle = wbcir_idle_rx;
  907. data->dev->s_carrier_report = wbcir_set_carrier_report;
  908. data->dev->s_tx_mask = wbcir_txmask;
  909. data->dev->s_tx_carrier = wbcir_txcarrier;
  910. data->dev->tx_ir = wbcir_tx;
  911. data->dev->priv = data;
  912. data->dev->dev.parent = &device->dev;
  913. data->dev->timeout = MS_TO_NS(100);
  914. data->dev->rx_resolution = US_TO_NS(2);
  915. data->dev->allowed_protocols = RC_BIT_ALL;
  916. err = rc_register_device(data->dev);
  917. if (err)
  918. goto exit_free_rc;
  919. if (!request_region(data->wbase, WAKEUP_IOMEM_LEN, DRVNAME)) {
  920. dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
  921. data->wbase, data->wbase + WAKEUP_IOMEM_LEN - 1);
  922. err = -EBUSY;
  923. goto exit_unregister_device;
  924. }
  925. if (!request_region(data->ebase, EHFUNC_IOMEM_LEN, DRVNAME)) {
  926. dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
  927. data->ebase, data->ebase + EHFUNC_IOMEM_LEN - 1);
  928. err = -EBUSY;
  929. goto exit_release_wbase;
  930. }
  931. if (!request_region(data->sbase, SP_IOMEM_LEN, DRVNAME)) {
  932. dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
  933. data->sbase, data->sbase + SP_IOMEM_LEN - 1);
  934. err = -EBUSY;
  935. goto exit_release_ebase;
  936. }
  937. err = request_irq(data->irq, wbcir_irq_handler,
  938. 0, DRVNAME, device);
  939. if (err) {
  940. dev_err(dev, "Failed to claim IRQ %u\n", data->irq);
  941. err = -EBUSY;
  942. goto exit_release_sbase;
  943. }
  944. device_init_wakeup(&device->dev, 1);
  945. wbcir_init_hw(data);
  946. return 0;
  947. exit_release_sbase:
  948. release_region(data->sbase, SP_IOMEM_LEN);
  949. exit_release_ebase:
  950. release_region(data->ebase, EHFUNC_IOMEM_LEN);
  951. exit_release_wbase:
  952. release_region(data->wbase, WAKEUP_IOMEM_LEN);
  953. exit_unregister_device:
  954. rc_unregister_device(data->dev);
  955. data->dev = NULL;
  956. exit_free_rc:
  957. rc_free_device(data->dev);
  958. exit_unregister_led:
  959. led_classdev_unregister(&data->led);
  960. exit_free_data:
  961. kfree(data);
  962. pnp_set_drvdata(device, NULL);
  963. exit:
  964. return err;
  965. }
  966. static void
  967. wbcir_remove(struct pnp_dev *device)
  968. {
  969. struct wbcir_data *data = pnp_get_drvdata(device);
  970. /* Disable interrupts */
  971. wbcir_set_irqmask(data, WBCIR_IRQ_NONE);
  972. free_irq(data->irq, device);
  973. /* Clear status bits NEC_REP, BUFF, MSG_END, MATCH */
  974. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_STS, 0x17, 0x17);
  975. /* Clear CEIR_EN */
  976. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CTL, 0x00, 0x01);
  977. /* Clear BUFF_EN, END_EN, MATCH_EN */
  978. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x00, 0x07);
  979. rc_unregister_device(data->dev);
  980. led_classdev_unregister(&data->led);
  981. /* This is ok since &data->led isn't actually used */
  982. wbcir_led_brightness_set(&data->led, LED_OFF);
  983. release_region(data->wbase, WAKEUP_IOMEM_LEN);
  984. release_region(data->ebase, EHFUNC_IOMEM_LEN);
  985. release_region(data->sbase, SP_IOMEM_LEN);
  986. kfree(data);
  987. pnp_set_drvdata(device, NULL);
  988. }
  989. static const struct pnp_device_id wbcir_ids[] = {
  990. { "WEC1022", 0 },
  991. { "", 0 }
  992. };
  993. MODULE_DEVICE_TABLE(pnp, wbcir_ids);
  994. static struct pnp_driver wbcir_driver = {
  995. .name = WBCIR_NAME,
  996. .id_table = wbcir_ids,
  997. .probe = wbcir_probe,
  998. .remove = wbcir_remove,
  999. .suspend = wbcir_suspend,
  1000. .resume = wbcir_resume,
  1001. .shutdown = wbcir_shutdown
  1002. };
  1003. static int __init
  1004. wbcir_init(void)
  1005. {
  1006. int ret;
  1007. switch (protocol) {
  1008. case IR_PROTOCOL_RC5:
  1009. case IR_PROTOCOL_NEC:
  1010. case IR_PROTOCOL_RC6:
  1011. break;
  1012. default:
  1013. pr_err("Invalid power-on protocol\n");
  1014. }
  1015. ret = pnp_register_driver(&wbcir_driver);
  1016. if (ret)
  1017. pr_err("Unable to register driver\n");
  1018. return ret;
  1019. }
  1020. static void __exit
  1021. wbcir_exit(void)
  1022. {
  1023. pnp_unregister_driver(&wbcir_driver);
  1024. }
  1025. module_init(wbcir_init);
  1026. module_exit(wbcir_exit);
  1027. MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
  1028. MODULE_DESCRIPTION("Winbond SuperI/O Consumer IR Driver");
  1029. MODULE_LICENSE("GPL");