winbond-cir.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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) 2009 - 2010 David Härdeman <david@hardeman.nu>
  11. *
  12. * Dedicated to my daughter Matilda, without whose loving attention this
  13. * driver would have been finished in half the time and with a fraction
  14. * of the bugs.
  15. *
  16. * Written using:
  17. * o Winbond WPCD376I datasheet helpfully provided by Jesse Barnes at Intel
  18. * o NatSemi PC87338/PC97338 datasheet (for the serial port stuff)
  19. * o DSDT dumps
  20. *
  21. * Supported features:
  22. * o Wake-On-CIR functionality
  23. *
  24. * To do:
  25. * o Learning
  26. * o IR Transmit
  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. #include <linux/module.h>
  43. #include <linux/pnp.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/timer.h>
  46. #include <linux/leds.h>
  47. #include <linux/spinlock.h>
  48. #include <linux/pci_ids.h>
  49. #include <linux/io.h>
  50. #include <linux/bitrev.h>
  51. #include <linux/slab.h>
  52. #include <media/rc-core.h>
  53. #define DRVNAME "winbond-cir"
  54. /* CEIR Wake-Up Registers, relative to data->wbase */
  55. #define WBCIR_REG_WCEIR_CTL 0x03 /* CEIR Receiver Control */
  56. #define WBCIR_REG_WCEIR_STS 0x04 /* CEIR Receiver Status */
  57. #define WBCIR_REG_WCEIR_EV_EN 0x05 /* CEIR Receiver Event Enable */
  58. #define WBCIR_REG_WCEIR_CNTL 0x06 /* CEIR Receiver Counter Low */
  59. #define WBCIR_REG_WCEIR_CNTH 0x07 /* CEIR Receiver Counter High */
  60. #define WBCIR_REG_WCEIR_INDEX 0x08 /* CEIR Receiver Index */
  61. #define WBCIR_REG_WCEIR_DATA 0x09 /* CEIR Receiver Data */
  62. #define WBCIR_REG_WCEIR_CSL 0x0A /* CEIR Re. Compare Strlen */
  63. #define WBCIR_REG_WCEIR_CFG1 0x0B /* CEIR Re. Configuration 1 */
  64. #define WBCIR_REG_WCEIR_CFG2 0x0C /* CEIR Re. Configuration 2 */
  65. /* CEIR Enhanced Functionality Registers, relative to data->ebase */
  66. #define WBCIR_REG_ECEIR_CTS 0x00 /* Enhanced IR Control Status */
  67. #define WBCIR_REG_ECEIR_CCTL 0x01 /* Infrared Counter Control */
  68. #define WBCIR_REG_ECEIR_CNT_LO 0x02 /* Infrared Counter LSB */
  69. #define WBCIR_REG_ECEIR_CNT_HI 0x03 /* Infrared Counter MSB */
  70. #define WBCIR_REG_ECEIR_IREM 0x04 /* Infrared Emitter Status */
  71. /* SP3 Banked Registers, relative to data->sbase */
  72. #define WBCIR_REG_SP3_BSR 0x03 /* Bank Select, all banks */
  73. /* Bank 0 */
  74. #define WBCIR_REG_SP3_RXDATA 0x00 /* FIFO RX data (r) */
  75. #define WBCIR_REG_SP3_TXDATA 0x00 /* FIFO TX data (w) */
  76. #define WBCIR_REG_SP3_IER 0x01 /* Interrupt Enable */
  77. #define WBCIR_REG_SP3_EIR 0x02 /* Event Identification (r) */
  78. #define WBCIR_REG_SP3_FCR 0x02 /* FIFO Control (w) */
  79. #define WBCIR_REG_SP3_MCR 0x04 /* Mode Control */
  80. #define WBCIR_REG_SP3_LSR 0x05 /* Link Status */
  81. #define WBCIR_REG_SP3_MSR 0x06 /* Modem Status */
  82. #define WBCIR_REG_SP3_ASCR 0x07 /* Aux Status and Control */
  83. /* Bank 2 */
  84. #define WBCIR_REG_SP3_BGDL 0x00 /* Baud Divisor LSB */
  85. #define WBCIR_REG_SP3_BGDH 0x01 /* Baud Divisor MSB */
  86. #define WBCIR_REG_SP3_EXCR1 0x02 /* Extended Control 1 */
  87. #define WBCIR_REG_SP3_EXCR2 0x04 /* Extended Control 2 */
  88. #define WBCIR_REG_SP3_TXFLV 0x06 /* TX FIFO Level */
  89. #define WBCIR_REG_SP3_RXFLV 0x07 /* RX FIFO Level */
  90. /* Bank 3 */
  91. #define WBCIR_REG_SP3_MRID 0x00 /* Module Identification */
  92. #define WBCIR_REG_SP3_SH_LCR 0x01 /* LCR Shadow */
  93. #define WBCIR_REG_SP3_SH_FCR 0x02 /* FCR Shadow */
  94. /* Bank 4 */
  95. #define WBCIR_REG_SP3_IRCR1 0x02 /* Infrared Control 1 */
  96. /* Bank 5 */
  97. #define WBCIR_REG_SP3_IRCR2 0x04 /* Infrared Control 2 */
  98. /* Bank 6 */
  99. #define WBCIR_REG_SP3_IRCR3 0x00 /* Infrared Control 3 */
  100. #define WBCIR_REG_SP3_SIR_PW 0x02 /* SIR Pulse Width */
  101. /* Bank 7 */
  102. #define WBCIR_REG_SP3_IRRXDC 0x00 /* IR RX Demod Control */
  103. #define WBCIR_REG_SP3_IRTXMC 0x01 /* IR TX Mod Control */
  104. #define WBCIR_REG_SP3_RCCFG 0x02 /* CEIR Config */
  105. #define WBCIR_REG_SP3_IRCFG1 0x04 /* Infrared Config 1 */
  106. #define WBCIR_REG_SP3_IRCFG4 0x07 /* Infrared Config 4 */
  107. /*
  108. * Magic values follow
  109. */
  110. /* No interrupts for WBCIR_REG_SP3_IER and WBCIR_REG_SP3_EIR */
  111. #define WBCIR_IRQ_NONE 0x00
  112. /* RX data bit for WBCIR_REG_SP3_IER and WBCIR_REG_SP3_EIR */
  113. #define WBCIR_IRQ_RX 0x01
  114. /* Over/Under-flow bit for WBCIR_REG_SP3_IER and WBCIR_REG_SP3_EIR */
  115. #define WBCIR_IRQ_ERR 0x04
  116. /* Led enable/disable bit for WBCIR_REG_ECEIR_CTS */
  117. #define WBCIR_LED_ENABLE 0x80
  118. /* RX data available bit for WBCIR_REG_SP3_LSR */
  119. #define WBCIR_RX_AVAIL 0x01
  120. /* RX disable bit for WBCIR_REG_SP3_ASCR */
  121. #define WBCIR_RX_DISABLE 0x20
  122. /* Extended mode enable bit for WBCIR_REG_SP3_EXCR1 */
  123. #define WBCIR_EXT_ENABLE 0x01
  124. /* Select compare register in WBCIR_REG_WCEIR_INDEX (bits 5 & 6) */
  125. #define WBCIR_REGSEL_COMPARE 0x10
  126. /* Select mask register in WBCIR_REG_WCEIR_INDEX (bits 5 & 6) */
  127. #define WBCIR_REGSEL_MASK 0x20
  128. /* Starting address of selected register in WBCIR_REG_WCEIR_INDEX */
  129. #define WBCIR_REG_ADDR0 0x00
  130. /* Valid banks for the SP3 UART */
  131. enum wbcir_bank {
  132. WBCIR_BANK_0 = 0x00,
  133. WBCIR_BANK_1 = 0x80,
  134. WBCIR_BANK_2 = 0xE0,
  135. WBCIR_BANK_3 = 0xE4,
  136. WBCIR_BANK_4 = 0xE8,
  137. WBCIR_BANK_5 = 0xEC,
  138. WBCIR_BANK_6 = 0xF0,
  139. WBCIR_BANK_7 = 0xF4,
  140. };
  141. /* Supported power-on IR Protocols */
  142. enum wbcir_protocol {
  143. IR_PROTOCOL_RC5 = 0x0,
  144. IR_PROTOCOL_NEC = 0x1,
  145. IR_PROTOCOL_RC6 = 0x2,
  146. };
  147. /* Misc */
  148. #define WBCIR_NAME "Winbond CIR"
  149. #define WBCIR_ID_FAMILY 0xF1 /* Family ID for the WPCD376I */
  150. #define WBCIR_ID_CHIP 0x04 /* Chip ID for the WPCD376I */
  151. #define INVALID_SCANCODE 0x7FFFFFFF /* Invalid with all protos */
  152. #define WAKEUP_IOMEM_LEN 0x10 /* Wake-Up I/O Reg Len */
  153. #define EHFUNC_IOMEM_LEN 0x10 /* Enhanced Func I/O Reg Len */
  154. #define SP_IOMEM_LEN 0x08 /* Serial Port 3 (IR) Reg Len */
  155. /* Per-device data */
  156. struct wbcir_data {
  157. spinlock_t spinlock;
  158. unsigned long wbase; /* Wake-Up Baseaddr */
  159. unsigned long ebase; /* Enhanced Func. Baseaddr */
  160. unsigned long sbase; /* Serial Port Baseaddr */
  161. unsigned int irq; /* Serial Port IRQ */
  162. struct rc_dev *dev;
  163. struct led_trigger *rxtrigger;
  164. struct led_trigger *txtrigger;
  165. struct led_classdev led;
  166. /* RX irdata state */
  167. bool irdata_active;
  168. bool irdata_error;
  169. struct ir_raw_event ev;
  170. };
  171. static enum wbcir_protocol protocol = IR_PROTOCOL_RC6;
  172. module_param(protocol, uint, 0444);
  173. MODULE_PARM_DESC(protocol, "IR protocol to use for the power-on command "
  174. "(0 = RC5, 1 = NEC, 2 = RC6A, default)");
  175. static int invert; /* default = 0 */
  176. module_param(invert, bool, 0444);
  177. MODULE_PARM_DESC(invert, "Invert the signal from the IR receiver");
  178. static unsigned int wake_sc = 0x800F040C;
  179. module_param(wake_sc, uint, 0644);
  180. MODULE_PARM_DESC(wake_sc, "Scancode of the power-on IR command");
  181. static unsigned int wake_rc6mode = 6;
  182. module_param(wake_rc6mode, uint, 0644);
  183. MODULE_PARM_DESC(wake_rc6mode, "RC6 mode for the power-on command "
  184. "(0 = 0, 6 = 6A, default)");
  185. /*****************************************************************************
  186. *
  187. * UTILITY FUNCTIONS
  188. *
  189. *****************************************************************************/
  190. /* Caller needs to hold wbcir_lock */
  191. static void
  192. wbcir_set_bits(unsigned long addr, u8 bits, u8 mask)
  193. {
  194. u8 val;
  195. val = inb(addr);
  196. val = ((val & ~mask) | (bits & mask));
  197. outb(val, addr);
  198. }
  199. /* Selects the register bank for the serial port */
  200. static inline void
  201. wbcir_select_bank(struct wbcir_data *data, enum wbcir_bank bank)
  202. {
  203. outb(bank, data->sbase + WBCIR_REG_SP3_BSR);
  204. }
  205. static enum led_brightness
  206. wbcir_led_brightness_get(struct led_classdev *led_cdev)
  207. {
  208. struct wbcir_data *data = container_of(led_cdev,
  209. struct wbcir_data,
  210. led);
  211. if (inb(data->ebase + WBCIR_REG_ECEIR_CTS) & WBCIR_LED_ENABLE)
  212. return LED_FULL;
  213. else
  214. return LED_OFF;
  215. }
  216. static void
  217. wbcir_led_brightness_set(struct led_classdev *led_cdev,
  218. enum led_brightness brightness)
  219. {
  220. struct wbcir_data *data = container_of(led_cdev,
  221. struct wbcir_data,
  222. led);
  223. wbcir_set_bits(data->ebase + WBCIR_REG_ECEIR_CTS,
  224. brightness == LED_OFF ? 0x00 : WBCIR_LED_ENABLE,
  225. WBCIR_LED_ENABLE);
  226. }
  227. /* Manchester encodes bits to RC6 message cells (see wbcir_shutdown) */
  228. static u8
  229. wbcir_to_rc6cells(u8 val)
  230. {
  231. u8 coded = 0x00;
  232. int i;
  233. val &= 0x0F;
  234. for (i = 0; i < 4; i++) {
  235. if (val & 0x01)
  236. coded |= 0x02 << (i * 2);
  237. else
  238. coded |= 0x01 << (i * 2);
  239. val >>= 1;
  240. }
  241. return coded;
  242. }
  243. /*****************************************************************************
  244. *
  245. * INTERRUPT FUNCTIONS
  246. *
  247. *****************************************************************************/
  248. static irqreturn_t
  249. wbcir_irq_handler(int irqno, void *cookie)
  250. {
  251. struct pnp_dev *device = cookie;
  252. struct wbcir_data *data = pnp_get_drvdata(device);
  253. unsigned long flags;
  254. u8 irdata[8];
  255. u8 disable = true;
  256. u8 status;
  257. int i;
  258. spin_lock_irqsave(&data->spinlock, flags);
  259. wbcir_select_bank(data, WBCIR_BANK_0);
  260. status = inb(data->sbase + WBCIR_REG_SP3_EIR);
  261. if (!(status & (WBCIR_IRQ_RX | WBCIR_IRQ_ERR))) {
  262. spin_unlock_irqrestore(&data->spinlock, flags);
  263. return IRQ_NONE;
  264. }
  265. /* Check for e.g. buffer overflow */
  266. if (status & WBCIR_IRQ_ERR) {
  267. data->irdata_error = true;
  268. ir_raw_event_reset(data->dev);
  269. }
  270. if (!(status & WBCIR_IRQ_RX))
  271. goto out;
  272. if (!data->irdata_active) {
  273. data->irdata_active = true;
  274. led_trigger_event(data->rxtrigger, LED_FULL);
  275. }
  276. /* Since RXHDLEV is set, at least 8 bytes are in the FIFO */
  277. insb(data->sbase + WBCIR_REG_SP3_RXDATA, &irdata[0], 8);
  278. for (i = 0; i < 8; i++) {
  279. u8 pulse;
  280. u32 duration;
  281. if (irdata[i] != 0xFF && irdata[i] != 0x00)
  282. disable = false;
  283. if (data->irdata_error)
  284. continue;
  285. pulse = irdata[i] & 0x80 ? false : true;
  286. duration = (irdata[i] & 0x7F) * 10000; /* ns */
  287. if (data->ev.pulse != pulse) {
  288. if (data->ev.duration != 0) {
  289. ir_raw_event_store(data->dev, &data->ev);
  290. data->ev.duration = 0;
  291. }
  292. data->ev.pulse = pulse;
  293. }
  294. data->ev.duration += duration;
  295. }
  296. if (disable) {
  297. if (data->ev.duration != 0 && !data->irdata_error) {
  298. ir_raw_event_store(data->dev, &data->ev);
  299. data->ev.duration = 0;
  300. }
  301. /* Set RXINACTIVE */
  302. outb(WBCIR_RX_DISABLE, data->sbase + WBCIR_REG_SP3_ASCR);
  303. /* Drain the FIFO */
  304. while (inb(data->sbase + WBCIR_REG_SP3_LSR) & WBCIR_RX_AVAIL)
  305. inb(data->sbase + WBCIR_REG_SP3_RXDATA);
  306. ir_raw_event_reset(data->dev);
  307. data->irdata_error = false;
  308. data->irdata_active = false;
  309. led_trigger_event(data->rxtrigger, LED_OFF);
  310. }
  311. ir_raw_event_handle(data->dev);
  312. out:
  313. spin_unlock_irqrestore(&data->spinlock, flags);
  314. return IRQ_HANDLED;
  315. }
  316. /*****************************************************************************
  317. *
  318. * SETUP/INIT/SUSPEND/RESUME FUNCTIONS
  319. *
  320. *****************************************************************************/
  321. static void
  322. wbcir_shutdown(struct pnp_dev *device)
  323. {
  324. struct device *dev = &device->dev;
  325. struct wbcir_data *data = pnp_get_drvdata(device);
  326. int do_wake = 1;
  327. u8 match[11];
  328. u8 mask[11];
  329. u8 rc6_csl = 0;
  330. int i;
  331. memset(match, 0, sizeof(match));
  332. memset(mask, 0, sizeof(mask));
  333. if (wake_sc == INVALID_SCANCODE || !device_may_wakeup(dev)) {
  334. do_wake = 0;
  335. goto finish;
  336. }
  337. switch (protocol) {
  338. case IR_PROTOCOL_RC5:
  339. if (wake_sc > 0xFFF) {
  340. do_wake = 0;
  341. dev_err(dev, "RC5 - Invalid wake scancode\n");
  342. break;
  343. }
  344. /* Mask = 13 bits, ex toggle */
  345. mask[0] = 0xFF;
  346. mask[1] = 0x17;
  347. match[0] = (wake_sc & 0x003F); /* 6 command bits */
  348. match[0] |= (wake_sc & 0x0180) >> 1; /* 2 address bits */
  349. match[1] = (wake_sc & 0x0E00) >> 9; /* 3 address bits */
  350. if (!(wake_sc & 0x0040)) /* 2nd start bit */
  351. match[1] |= 0x10;
  352. break;
  353. case IR_PROTOCOL_NEC:
  354. if (wake_sc > 0xFFFFFF) {
  355. do_wake = 0;
  356. dev_err(dev, "NEC - Invalid wake scancode\n");
  357. break;
  358. }
  359. mask[0] = mask[1] = mask[2] = mask[3] = 0xFF;
  360. match[1] = bitrev8((wake_sc & 0xFF));
  361. match[0] = ~match[1];
  362. match[3] = bitrev8((wake_sc & 0xFF00) >> 8);
  363. if (wake_sc > 0xFFFF)
  364. match[2] = bitrev8((wake_sc & 0xFF0000) >> 16);
  365. else
  366. match[2] = ~match[3];
  367. break;
  368. case IR_PROTOCOL_RC6:
  369. if (wake_rc6mode == 0) {
  370. if (wake_sc > 0xFFFF) {
  371. do_wake = 0;
  372. dev_err(dev, "RC6 - Invalid wake scancode\n");
  373. break;
  374. }
  375. /* Command */
  376. match[0] = wbcir_to_rc6cells(wake_sc >> 0);
  377. mask[0] = 0xFF;
  378. match[1] = wbcir_to_rc6cells(wake_sc >> 4);
  379. mask[1] = 0xFF;
  380. /* Address */
  381. match[2] = wbcir_to_rc6cells(wake_sc >> 8);
  382. mask[2] = 0xFF;
  383. match[3] = wbcir_to_rc6cells(wake_sc >> 12);
  384. mask[3] = 0xFF;
  385. /* Header */
  386. match[4] = 0x50; /* mode1 = mode0 = 0, ignore toggle */
  387. mask[4] = 0xF0;
  388. match[5] = 0x09; /* start bit = 1, mode2 = 0 */
  389. mask[5] = 0x0F;
  390. rc6_csl = 44;
  391. } else if (wake_rc6mode == 6) {
  392. i = 0;
  393. /* Command */
  394. match[i] = wbcir_to_rc6cells(wake_sc >> 0);
  395. mask[i++] = 0xFF;
  396. match[i] = wbcir_to_rc6cells(wake_sc >> 4);
  397. mask[i++] = 0xFF;
  398. /* Address + Toggle */
  399. match[i] = wbcir_to_rc6cells(wake_sc >> 8);
  400. mask[i++] = 0xFF;
  401. match[i] = wbcir_to_rc6cells(wake_sc >> 12);
  402. mask[i++] = 0x3F;
  403. /* Customer bits 7 - 0 */
  404. match[i] = wbcir_to_rc6cells(wake_sc >> 16);
  405. mask[i++] = 0xFF;
  406. match[i] = wbcir_to_rc6cells(wake_sc >> 20);
  407. mask[i++] = 0xFF;
  408. if (wake_sc & 0x80000000) {
  409. /* Customer range bit and bits 15 - 8 */
  410. match[i] = wbcir_to_rc6cells(wake_sc >> 24);
  411. mask[i++] = 0xFF;
  412. match[i] = wbcir_to_rc6cells(wake_sc >> 28);
  413. mask[i++] = 0xFF;
  414. rc6_csl = 76;
  415. } else if (wake_sc <= 0x007FFFFF) {
  416. rc6_csl = 60;
  417. } else {
  418. do_wake = 0;
  419. dev_err(dev, "RC6 - Invalid wake scancode\n");
  420. break;
  421. }
  422. /* Header */
  423. match[i] = 0x93; /* mode1 = mode0 = 1, submode = 0 */
  424. mask[i++] = 0xFF;
  425. match[i] = 0x0A; /* start bit = 1, mode2 = 1 */
  426. mask[i++] = 0x0F;
  427. } else {
  428. do_wake = 0;
  429. dev_err(dev, "RC6 - Invalid wake mode\n");
  430. }
  431. break;
  432. default:
  433. do_wake = 0;
  434. break;
  435. }
  436. finish:
  437. if (do_wake) {
  438. /* Set compare and compare mask */
  439. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_INDEX,
  440. WBCIR_REGSEL_COMPARE | WBCIR_REG_ADDR0,
  441. 0x3F);
  442. outsb(data->wbase + WBCIR_REG_WCEIR_DATA, match, 11);
  443. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_INDEX,
  444. WBCIR_REGSEL_MASK | WBCIR_REG_ADDR0,
  445. 0x3F);
  446. outsb(data->wbase + WBCIR_REG_WCEIR_DATA, mask, 11);
  447. /* RC6 Compare String Len */
  448. outb(rc6_csl, data->wbase + WBCIR_REG_WCEIR_CSL);
  449. /* Clear status bits NEC_REP, BUFF, MSG_END, MATCH */
  450. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_STS, 0x17, 0x17);
  451. /* Clear BUFF_EN, Clear END_EN, Set MATCH_EN */
  452. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x01, 0x07);
  453. /* Set CEIR_EN */
  454. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CTL, 0x01, 0x01);
  455. } else {
  456. /* Clear BUFF_EN, Clear END_EN, Clear MATCH_EN */
  457. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x00, 0x07);
  458. /* Clear CEIR_EN */
  459. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CTL, 0x00, 0x01);
  460. }
  461. /* Disable interrupts */
  462. wbcir_select_bank(data, WBCIR_BANK_0);
  463. outb(WBCIR_IRQ_NONE, data->sbase + WBCIR_REG_SP3_IER);
  464. /* Disable LED */
  465. data->irdata_active = false;
  466. led_trigger_event(data->rxtrigger, LED_OFF);
  467. /*
  468. * ACPI will set the HW disable bit for SP3 which means that the
  469. * output signals are left in an undefined state which may cause
  470. * spurious interrupts which we need to ignore until the hardware
  471. * is reinitialized.
  472. */
  473. disable_irq(data->irq);
  474. }
  475. static int
  476. wbcir_suspend(struct pnp_dev *device, pm_message_t state)
  477. {
  478. wbcir_shutdown(device);
  479. return 0;
  480. }
  481. static void
  482. wbcir_init_hw(struct wbcir_data *data)
  483. {
  484. u8 tmp;
  485. /* Disable interrupts */
  486. wbcir_select_bank(data, WBCIR_BANK_0);
  487. outb(WBCIR_IRQ_NONE, data->sbase + WBCIR_REG_SP3_IER);
  488. /* Set PROT_SEL, RX_INV, Clear CEIR_EN (needed for the led) */
  489. tmp = protocol << 4;
  490. if (invert)
  491. tmp |= 0x08;
  492. outb(tmp, data->wbase + WBCIR_REG_WCEIR_CTL);
  493. /* Clear status bits NEC_REP, BUFF, MSG_END, MATCH */
  494. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_STS, 0x17, 0x17);
  495. /* Clear BUFF_EN, Clear END_EN, Clear MATCH_EN */
  496. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x00, 0x07);
  497. /* Set RC5 cell time to correspond to 36 kHz */
  498. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CFG1, 0x4A, 0x7F);
  499. /* Set IRTX_INV */
  500. if (invert)
  501. outb(0x04, data->ebase + WBCIR_REG_ECEIR_CCTL);
  502. else
  503. outb(0x00, data->ebase + WBCIR_REG_ECEIR_CCTL);
  504. /*
  505. * Clear IR LED, set SP3 clock to 24Mhz
  506. * set SP3_IRRX_SW to binary 01, helpfully not documented
  507. */
  508. outb(0x10, data->ebase + WBCIR_REG_ECEIR_CTS);
  509. /* Enable extended mode */
  510. wbcir_select_bank(data, WBCIR_BANK_2);
  511. outb(WBCIR_EXT_ENABLE, data->sbase + WBCIR_REG_SP3_EXCR1);
  512. /*
  513. * Configure baud generator, IR data will be sampled at
  514. * a bitrate of: (24Mhz * prescaler) / (divisor * 16).
  515. *
  516. * The ECIR registers include a flag to change the
  517. * 24Mhz clock freq to 48Mhz.
  518. *
  519. * It's not documented in the specs, but fifo levels
  520. * other than 16 seems to be unsupported.
  521. */
  522. /* prescaler 1.0, tx/rx fifo lvl 16 */
  523. outb(0x30, data->sbase + WBCIR_REG_SP3_EXCR2);
  524. /* Set baud divisor to generate one byte per bit/cell */
  525. switch (protocol) {
  526. case IR_PROTOCOL_RC5:
  527. outb(0xA7, data->sbase + WBCIR_REG_SP3_BGDL);
  528. break;
  529. case IR_PROTOCOL_RC6:
  530. outb(0x53, data->sbase + WBCIR_REG_SP3_BGDL);
  531. break;
  532. case IR_PROTOCOL_NEC:
  533. outb(0x69, data->sbase + WBCIR_REG_SP3_BGDL);
  534. break;
  535. }
  536. outb(0x00, data->sbase + WBCIR_REG_SP3_BGDH);
  537. /* Set CEIR mode */
  538. wbcir_select_bank(data, WBCIR_BANK_0);
  539. outb(0xC0, data->sbase + WBCIR_REG_SP3_MCR);
  540. inb(data->sbase + WBCIR_REG_SP3_LSR); /* Clear LSR */
  541. inb(data->sbase + WBCIR_REG_SP3_MSR); /* Clear MSR */
  542. /* Disable RX demod, run-length encoding/decoding, set freq span */
  543. wbcir_select_bank(data, WBCIR_BANK_7);
  544. outb(0x10, data->sbase + WBCIR_REG_SP3_RCCFG);
  545. /* Disable timer */
  546. wbcir_select_bank(data, WBCIR_BANK_4);
  547. outb(0x00, data->sbase + WBCIR_REG_SP3_IRCR1);
  548. /* Enable MSR interrupt, Clear AUX_IRX */
  549. wbcir_select_bank(data, WBCIR_BANK_5);
  550. outb(0x00, data->sbase + WBCIR_REG_SP3_IRCR2);
  551. /* Disable CRC */
  552. wbcir_select_bank(data, WBCIR_BANK_6);
  553. outb(0x20, data->sbase + WBCIR_REG_SP3_IRCR3);
  554. /* Set RX/TX (de)modulation freq, not really used */
  555. wbcir_select_bank(data, WBCIR_BANK_7);
  556. outb(0xF2, data->sbase + WBCIR_REG_SP3_IRRXDC);
  557. outb(0x69, data->sbase + WBCIR_REG_SP3_IRTXMC);
  558. /* Set invert and pin direction */
  559. if (invert)
  560. outb(0x10, data->sbase + WBCIR_REG_SP3_IRCFG4);
  561. else
  562. outb(0x00, data->sbase + WBCIR_REG_SP3_IRCFG4);
  563. /* Set FIFO thresholds (RX = 8, TX = 3), reset RX/TX */
  564. wbcir_select_bank(data, WBCIR_BANK_0);
  565. outb(0x97, data->sbase + WBCIR_REG_SP3_FCR);
  566. /* Clear AUX status bits */
  567. outb(0xE0, data->sbase + WBCIR_REG_SP3_ASCR);
  568. /* Clear IR decoding state */
  569. data->irdata_active = false;
  570. led_trigger_event(data->rxtrigger, LED_OFF);
  571. data->irdata_error = false;
  572. data->ev.duration = 0;
  573. ir_raw_event_reset(data->dev);
  574. ir_raw_event_handle(data->dev);
  575. /* Enable interrupts */
  576. outb(WBCIR_IRQ_RX | WBCIR_IRQ_ERR, data->sbase + WBCIR_REG_SP3_IER);
  577. }
  578. static int
  579. wbcir_resume(struct pnp_dev *device)
  580. {
  581. struct wbcir_data *data = pnp_get_drvdata(device);
  582. wbcir_init_hw(data);
  583. enable_irq(data->irq);
  584. return 0;
  585. }
  586. static int __devinit
  587. wbcir_probe(struct pnp_dev *device, const struct pnp_device_id *dev_id)
  588. {
  589. struct device *dev = &device->dev;
  590. struct wbcir_data *data;
  591. int err;
  592. if (!(pnp_port_len(device, 0) == EHFUNC_IOMEM_LEN &&
  593. pnp_port_len(device, 1) == WAKEUP_IOMEM_LEN &&
  594. pnp_port_len(device, 2) == SP_IOMEM_LEN)) {
  595. dev_err(dev, "Invalid resources\n");
  596. return -ENODEV;
  597. }
  598. data = kzalloc(sizeof(*data), GFP_KERNEL);
  599. if (!data) {
  600. err = -ENOMEM;
  601. goto exit;
  602. }
  603. pnp_set_drvdata(device, data);
  604. spin_lock_init(&data->spinlock);
  605. data->ebase = pnp_port_start(device, 0);
  606. data->wbase = pnp_port_start(device, 1);
  607. data->sbase = pnp_port_start(device, 2);
  608. data->irq = pnp_irq(device, 0);
  609. if (data->wbase == 0 || data->ebase == 0 ||
  610. data->sbase == 0 || data->irq == 0) {
  611. err = -ENODEV;
  612. dev_err(dev, "Invalid resources\n");
  613. goto exit_free_data;
  614. }
  615. dev_dbg(&device->dev, "Found device "
  616. "(w: 0x%lX, e: 0x%lX, s: 0x%lX, i: %u)\n",
  617. data->wbase, data->ebase, data->sbase, data->irq);
  618. if (!request_region(data->wbase, WAKEUP_IOMEM_LEN, DRVNAME)) {
  619. dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
  620. data->wbase, data->wbase + WAKEUP_IOMEM_LEN - 1);
  621. err = -EBUSY;
  622. goto exit_free_data;
  623. }
  624. if (!request_region(data->ebase, EHFUNC_IOMEM_LEN, DRVNAME)) {
  625. dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
  626. data->ebase, data->ebase + EHFUNC_IOMEM_LEN - 1);
  627. err = -EBUSY;
  628. goto exit_release_wbase;
  629. }
  630. if (!request_region(data->sbase, SP_IOMEM_LEN, DRVNAME)) {
  631. dev_err(dev, "Region 0x%lx-0x%lx already in use!\n",
  632. data->sbase, data->sbase + SP_IOMEM_LEN - 1);
  633. err = -EBUSY;
  634. goto exit_release_ebase;
  635. }
  636. err = request_irq(data->irq, wbcir_irq_handler,
  637. IRQF_DISABLED, DRVNAME, device);
  638. if (err) {
  639. dev_err(dev, "Failed to claim IRQ %u\n", data->irq);
  640. err = -EBUSY;
  641. goto exit_release_sbase;
  642. }
  643. led_trigger_register_simple("cir-tx", &data->txtrigger);
  644. if (!data->txtrigger) {
  645. err = -ENOMEM;
  646. goto exit_free_irq;
  647. }
  648. led_trigger_register_simple("cir-rx", &data->rxtrigger);
  649. if (!data->rxtrigger) {
  650. err = -ENOMEM;
  651. goto exit_unregister_txtrigger;
  652. }
  653. data->led.name = "cir::activity";
  654. data->led.default_trigger = "cir-rx";
  655. data->led.brightness_set = wbcir_led_brightness_set;
  656. data->led.brightness_get = wbcir_led_brightness_get;
  657. err = led_classdev_register(&device->dev, &data->led);
  658. if (err)
  659. goto exit_unregister_rxtrigger;
  660. data->dev = rc_allocate_device();
  661. if (!data->dev) {
  662. err = -ENOMEM;
  663. goto exit_unregister_led;
  664. }
  665. data->dev->driver_name = WBCIR_NAME;
  666. data->dev->input_name = WBCIR_NAME;
  667. data->dev->input_phys = "wbcir/cir0";
  668. data->dev->input_id.bustype = BUS_HOST;
  669. data->dev->input_id.vendor = PCI_VENDOR_ID_WINBOND;
  670. data->dev->input_id.product = WBCIR_ID_FAMILY;
  671. data->dev->input_id.version = WBCIR_ID_CHIP;
  672. data->dev->priv = data;
  673. data->dev->dev.parent = &device->dev;
  674. err = rc_register_device(data->dev);
  675. if (err)
  676. goto exit_free_rc;
  677. device_init_wakeup(&device->dev, 1);
  678. wbcir_init_hw(data);
  679. return 0;
  680. exit_free_rc:
  681. rc_free_device(data->dev);
  682. exit_unregister_led:
  683. led_classdev_unregister(&data->led);
  684. exit_unregister_rxtrigger:
  685. led_trigger_unregister_simple(data->rxtrigger);
  686. exit_unregister_txtrigger:
  687. led_trigger_unregister_simple(data->txtrigger);
  688. exit_free_irq:
  689. free_irq(data->irq, device);
  690. exit_release_sbase:
  691. release_region(data->sbase, SP_IOMEM_LEN);
  692. exit_release_ebase:
  693. release_region(data->ebase, EHFUNC_IOMEM_LEN);
  694. exit_release_wbase:
  695. release_region(data->wbase, WAKEUP_IOMEM_LEN);
  696. exit_free_data:
  697. kfree(data);
  698. pnp_set_drvdata(device, NULL);
  699. exit:
  700. return err;
  701. }
  702. static void __devexit
  703. wbcir_remove(struct pnp_dev *device)
  704. {
  705. struct wbcir_data *data = pnp_get_drvdata(device);
  706. /* Disable interrupts */
  707. wbcir_select_bank(data, WBCIR_BANK_0);
  708. outb(WBCIR_IRQ_NONE, data->sbase + WBCIR_REG_SP3_IER);
  709. free_irq(data->irq, device);
  710. /* Clear status bits NEC_REP, BUFF, MSG_END, MATCH */
  711. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_STS, 0x17, 0x17);
  712. /* Clear CEIR_EN */
  713. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_CTL, 0x00, 0x01);
  714. /* Clear BUFF_EN, END_EN, MATCH_EN */
  715. wbcir_set_bits(data->wbase + WBCIR_REG_WCEIR_EV_EN, 0x00, 0x07);
  716. rc_unregister_device(data->dev);
  717. led_trigger_unregister_simple(data->rxtrigger);
  718. led_trigger_unregister_simple(data->txtrigger);
  719. led_classdev_unregister(&data->led);
  720. /* This is ok since &data->led isn't actually used */
  721. wbcir_led_brightness_set(&data->led, LED_OFF);
  722. release_region(data->wbase, WAKEUP_IOMEM_LEN);
  723. release_region(data->ebase, EHFUNC_IOMEM_LEN);
  724. release_region(data->sbase, SP_IOMEM_LEN);
  725. kfree(data);
  726. pnp_set_drvdata(device, NULL);
  727. }
  728. static const struct pnp_device_id wbcir_ids[] = {
  729. { "WEC1022", 0 },
  730. { "", 0 }
  731. };
  732. MODULE_DEVICE_TABLE(pnp, wbcir_ids);
  733. static struct pnp_driver wbcir_driver = {
  734. .name = WBCIR_NAME,
  735. .id_table = wbcir_ids,
  736. .probe = wbcir_probe,
  737. .remove = __devexit_p(wbcir_remove),
  738. .suspend = wbcir_suspend,
  739. .resume = wbcir_resume,
  740. .shutdown = wbcir_shutdown
  741. };
  742. static int __init
  743. wbcir_init(void)
  744. {
  745. int ret;
  746. switch (protocol) {
  747. case IR_PROTOCOL_RC5:
  748. case IR_PROTOCOL_NEC:
  749. case IR_PROTOCOL_RC6:
  750. break;
  751. default:
  752. printk(KERN_ERR DRVNAME ": Invalid power-on protocol\n");
  753. }
  754. ret = pnp_register_driver(&wbcir_driver);
  755. if (ret)
  756. printk(KERN_ERR DRVNAME ": Unable to register driver\n");
  757. return ret;
  758. }
  759. static void __exit
  760. wbcir_exit(void)
  761. {
  762. pnp_unregister_driver(&wbcir_driver);
  763. }
  764. module_init(wbcir_init);
  765. module_exit(wbcir_exit);
  766. MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
  767. MODULE_DESCRIPTION("Winbond SuperI/O Consumer IR Driver");
  768. MODULE_LICENSE("GPL");