ite-cir.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713
  1. /*
  2. * Driver for ITE Tech Inc. IT8712F/IT8512 CIR
  3. *
  4. * Copyright (C) 2010 Juan Jesús García de Soria <skandalfo@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * Inspired by the original lirc_it87 and lirc_ite8709 drivers, on top of the
  17. * skeleton provided by the nuvoton-cir driver.
  18. *
  19. * The lirc_it87 driver was originally written by Hans-Gunter Lutke Uphues
  20. * <hg_lu@web.de> in 2001, with enhancements by Christoph Bartelmus
  21. * <lirc@bartelmus.de>, Andrew Calkin <r_tay@hotmail.com> and James Edwards
  22. * <jimbo-lirc@edwardsclan.net>.
  23. *
  24. * The lirc_ite8709 driver was written by Grégory Lardière
  25. * <spmf2004-lirc@yahoo.fr> in 2008.
  26. */
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/pnp.h>
  30. #include <linux/io.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/sched.h>
  33. #include <linux/delay.h>
  34. #include <linux/slab.h>
  35. #include <linux/input.h>
  36. #include <linux/bitops.h>
  37. #include <media/rc-core.h>
  38. #include <linux/pci_ids.h>
  39. #include "ite-cir.h"
  40. /* module parameters */
  41. /* debug level */
  42. static int debug;
  43. module_param(debug, int, S_IRUGO | S_IWUSR);
  44. MODULE_PARM_DESC(debug, "Enable debugging output");
  45. /* low limit for RX carrier freq, Hz, 0 for no RX demodulation */
  46. static int rx_low_carrier_freq;
  47. module_param(rx_low_carrier_freq, int, S_IRUGO | S_IWUSR);
  48. MODULE_PARM_DESC(rx_low_carrier_freq, "Override low RX carrier frequency, Hz, 0 for no RX demodulation");
  49. /* high limit for RX carrier freq, Hz, 0 for no RX demodulation */
  50. static int rx_high_carrier_freq;
  51. module_param(rx_high_carrier_freq, int, S_IRUGO | S_IWUSR);
  52. MODULE_PARM_DESC(rx_high_carrier_freq, "Override high RX carrier frequency, Hz, 0 for no RX demodulation");
  53. /* override tx carrier frequency */
  54. static int tx_carrier_freq;
  55. module_param(tx_carrier_freq, int, S_IRUGO | S_IWUSR);
  56. MODULE_PARM_DESC(tx_carrier_freq, "Override TX carrier frequency, Hz");
  57. /* override tx duty cycle */
  58. static int tx_duty_cycle;
  59. module_param(tx_duty_cycle, int, S_IRUGO | S_IWUSR);
  60. MODULE_PARM_DESC(tx_duty_cycle, "Override TX duty cycle, 1-100");
  61. /* override default sample period */
  62. static long sample_period;
  63. module_param(sample_period, long, S_IRUGO | S_IWUSR);
  64. MODULE_PARM_DESC(sample_period, "Override carrier sample period, us");
  65. /* override detected model id */
  66. static int model_number = -1;
  67. module_param(model_number, int, S_IRUGO | S_IWUSR);
  68. MODULE_PARM_DESC(model_number, "Use this model number, don't autodetect");
  69. /* HW-independent code functions */
  70. /* check whether carrier frequency is high frequency */
  71. static inline bool ite_is_high_carrier_freq(unsigned int freq)
  72. {
  73. return freq >= ITE_HCF_MIN_CARRIER_FREQ;
  74. }
  75. /* get the bits required to program the carrier frequency in CFQ bits,
  76. * unshifted */
  77. static u8 ite_get_carrier_freq_bits(unsigned int freq)
  78. {
  79. if (ite_is_high_carrier_freq(freq)) {
  80. if (freq < 425000)
  81. return ITE_CFQ_400;
  82. else if (freq < 465000)
  83. return ITE_CFQ_450;
  84. else if (freq < 490000)
  85. return ITE_CFQ_480;
  86. else
  87. return ITE_CFQ_500;
  88. } else {
  89. /* trim to limits */
  90. if (freq < ITE_LCF_MIN_CARRIER_FREQ)
  91. freq = ITE_LCF_MIN_CARRIER_FREQ;
  92. if (freq > ITE_LCF_MAX_CARRIER_FREQ)
  93. freq = ITE_LCF_MAX_CARRIER_FREQ;
  94. /* convert to kHz and subtract the base freq */
  95. freq =
  96. DIV_ROUND_CLOSEST(freq - ITE_LCF_MIN_CARRIER_FREQ,
  97. 1000);
  98. return (u8) freq;
  99. }
  100. }
  101. /* get the bits required to program the pulse with in TXMPW */
  102. static u8 ite_get_pulse_width_bits(unsigned int freq, int duty_cycle)
  103. {
  104. unsigned long period_ns, on_ns;
  105. /* sanitize freq into range */
  106. if (freq < ITE_LCF_MIN_CARRIER_FREQ)
  107. freq = ITE_LCF_MIN_CARRIER_FREQ;
  108. if (freq > ITE_HCF_MAX_CARRIER_FREQ)
  109. freq = ITE_HCF_MAX_CARRIER_FREQ;
  110. period_ns = 1000000000UL / freq;
  111. on_ns = period_ns * duty_cycle / 100;
  112. if (ite_is_high_carrier_freq(freq)) {
  113. if (on_ns < 750)
  114. return ITE_TXMPW_A;
  115. else if (on_ns < 850)
  116. return ITE_TXMPW_B;
  117. else if (on_ns < 950)
  118. return ITE_TXMPW_C;
  119. else if (on_ns < 1080)
  120. return ITE_TXMPW_D;
  121. else
  122. return ITE_TXMPW_E;
  123. } else {
  124. if (on_ns < 6500)
  125. return ITE_TXMPW_A;
  126. else if (on_ns < 7850)
  127. return ITE_TXMPW_B;
  128. else if (on_ns < 9650)
  129. return ITE_TXMPW_C;
  130. else if (on_ns < 11950)
  131. return ITE_TXMPW_D;
  132. else
  133. return ITE_TXMPW_E;
  134. }
  135. }
  136. /* decode raw bytes as received by the hardware, and push them to the ir-core
  137. * layer */
  138. static void ite_decode_bytes(struct ite_dev *dev, const u8 * data, int
  139. length)
  140. {
  141. u32 sample_period;
  142. unsigned long *ldata;
  143. unsigned int next_one, next_zero, size;
  144. DEFINE_IR_RAW_EVENT(ev);
  145. if (length == 0)
  146. return;
  147. sample_period = dev->params.sample_period;
  148. ldata = (unsigned long *)data;
  149. size = length << 3;
  150. next_one = find_next_bit_le(ldata, size, 0);
  151. if (next_one > 0) {
  152. ev.pulse = true;
  153. ev.duration =
  154. ITE_BITS_TO_NS(next_one, sample_period);
  155. ir_raw_event_store_with_filter(dev->rdev, &ev);
  156. }
  157. while (next_one < size) {
  158. next_zero = find_next_zero_bit_le(ldata, size, next_one + 1);
  159. ev.pulse = false;
  160. ev.duration = ITE_BITS_TO_NS(next_zero - next_one, sample_period);
  161. ir_raw_event_store_with_filter(dev->rdev, &ev);
  162. if (next_zero < size) {
  163. next_one =
  164. find_next_bit_le(ldata,
  165. size,
  166. next_zero + 1);
  167. ev.pulse = true;
  168. ev.duration =
  169. ITE_BITS_TO_NS(next_one - next_zero,
  170. sample_period);
  171. ir_raw_event_store_with_filter
  172. (dev->rdev, &ev);
  173. } else
  174. next_one = size;
  175. }
  176. ir_raw_event_handle(dev->rdev);
  177. ite_dbg_verbose("decoded %d bytes.", length);
  178. }
  179. /* set all the rx/tx carrier parameters; this must be called with the device
  180. * spinlock held */
  181. static void ite_set_carrier_params(struct ite_dev *dev)
  182. {
  183. unsigned int freq, low_freq, high_freq;
  184. int allowance;
  185. bool use_demodulator;
  186. bool for_tx = dev->transmitting;
  187. ite_dbg("%s called", __func__);
  188. if (for_tx) {
  189. /* we don't need no stinking calculations */
  190. freq = dev->params.tx_carrier_freq;
  191. allowance = ITE_RXDCR_DEFAULT;
  192. use_demodulator = false;
  193. } else {
  194. low_freq = dev->params.rx_low_carrier_freq;
  195. high_freq = dev->params.rx_high_carrier_freq;
  196. if (low_freq == 0) {
  197. /* don't demodulate */
  198. freq =
  199. ITE_DEFAULT_CARRIER_FREQ;
  200. allowance = ITE_RXDCR_DEFAULT;
  201. use_demodulator = false;
  202. } else {
  203. /* calculate the middle freq */
  204. freq = (low_freq + high_freq) / 2;
  205. /* calculate the allowance */
  206. allowance =
  207. DIV_ROUND_CLOSEST(10000 * (high_freq - low_freq),
  208. ITE_RXDCR_PER_10000_STEP
  209. * (high_freq + low_freq));
  210. if (allowance < 1)
  211. allowance = 1;
  212. if (allowance > ITE_RXDCR_MAX)
  213. allowance = ITE_RXDCR_MAX;
  214. use_demodulator = true;
  215. }
  216. }
  217. /* set the carrier parameters in a device-dependent way */
  218. dev->params.set_carrier_params(dev, ite_is_high_carrier_freq(freq),
  219. use_demodulator, ite_get_carrier_freq_bits(freq), allowance,
  220. ite_get_pulse_width_bits(freq, dev->params.tx_duty_cycle));
  221. }
  222. /* interrupt service routine for incoming and outgoing CIR data */
  223. static irqreturn_t ite_cir_isr(int irq, void *data)
  224. {
  225. struct ite_dev *dev = data;
  226. unsigned long flags;
  227. irqreturn_t ret = IRQ_RETVAL(IRQ_NONE);
  228. u8 rx_buf[ITE_RX_FIFO_LEN];
  229. int rx_bytes;
  230. int iflags;
  231. ite_dbg_verbose("%s firing", __func__);
  232. /* grab the spinlock */
  233. spin_lock_irqsave(&dev->lock, flags);
  234. /* read the interrupt flags */
  235. iflags = dev->params.get_irq_causes(dev);
  236. /* check for the receive interrupt */
  237. if (iflags & (ITE_IRQ_RX_FIFO | ITE_IRQ_RX_FIFO_OVERRUN)) {
  238. /* read the FIFO bytes */
  239. rx_bytes =
  240. dev->params.get_rx_bytes(dev, rx_buf,
  241. ITE_RX_FIFO_LEN);
  242. if (rx_bytes > 0) {
  243. /* drop the spinlock, since the ir-core layer
  244. * may call us back again through
  245. * ite_s_idle() */
  246. spin_unlock_irqrestore(&dev->
  247. lock,
  248. flags);
  249. /* decode the data we've just received */
  250. ite_decode_bytes(dev, rx_buf,
  251. rx_bytes);
  252. /* reacquire the spinlock */
  253. spin_lock_irqsave(&dev->lock,
  254. flags);
  255. /* mark the interrupt as serviced */
  256. ret = IRQ_RETVAL(IRQ_HANDLED);
  257. }
  258. } else if (iflags & ITE_IRQ_TX_FIFO) {
  259. /* FIFO space available interrupt */
  260. ite_dbg_verbose("got interrupt for TX FIFO");
  261. /* wake any sleeping transmitter */
  262. wake_up_interruptible(&dev->tx_queue);
  263. /* mark the interrupt as serviced */
  264. ret = IRQ_RETVAL(IRQ_HANDLED);
  265. }
  266. /* drop the spinlock */
  267. spin_unlock_irqrestore(&dev->lock, flags);
  268. ite_dbg_verbose("%s done returning %d", __func__, (int)ret);
  269. return ret;
  270. }
  271. /* set the rx carrier freq range, guess it's in Hz... */
  272. static int ite_set_rx_carrier_range(struct rc_dev *rcdev, u32 carrier_low, u32
  273. carrier_high)
  274. {
  275. unsigned long flags;
  276. struct ite_dev *dev = rcdev->priv;
  277. spin_lock_irqsave(&dev->lock, flags);
  278. dev->params.rx_low_carrier_freq = carrier_low;
  279. dev->params.rx_high_carrier_freq = carrier_high;
  280. ite_set_carrier_params(dev);
  281. spin_unlock_irqrestore(&dev->lock, flags);
  282. return 0;
  283. }
  284. /* set the tx carrier freq, guess it's in Hz... */
  285. static int ite_set_tx_carrier(struct rc_dev *rcdev, u32 carrier)
  286. {
  287. unsigned long flags;
  288. struct ite_dev *dev = rcdev->priv;
  289. spin_lock_irqsave(&dev->lock, flags);
  290. dev->params.tx_carrier_freq = carrier;
  291. ite_set_carrier_params(dev);
  292. spin_unlock_irqrestore(&dev->lock, flags);
  293. return 0;
  294. }
  295. /* set the tx duty cycle by controlling the pulse width */
  296. static int ite_set_tx_duty_cycle(struct rc_dev *rcdev, u32 duty_cycle)
  297. {
  298. unsigned long flags;
  299. struct ite_dev *dev = rcdev->priv;
  300. spin_lock_irqsave(&dev->lock, flags);
  301. dev->params.tx_duty_cycle = duty_cycle;
  302. ite_set_carrier_params(dev);
  303. spin_unlock_irqrestore(&dev->lock, flags);
  304. return 0;
  305. }
  306. /* transmit out IR pulses; what you get here is a batch of alternating
  307. * pulse/space/pulse/space lengths that we should write out completely through
  308. * the FIFO, blocking on a full FIFO */
  309. static int ite_tx_ir(struct rc_dev *rcdev, unsigned *txbuf, unsigned n)
  310. {
  311. unsigned long flags;
  312. struct ite_dev *dev = rcdev->priv;
  313. bool is_pulse = false;
  314. int remaining_us, fifo_avail, fifo_remaining, last_idx = 0;
  315. int max_rle_us, next_rle_us;
  316. int ret = n;
  317. u8 last_sent[ITE_TX_FIFO_LEN];
  318. u8 val;
  319. ite_dbg("%s called", __func__);
  320. /* clear the array just in case */
  321. memset(last_sent, 0, ARRAY_SIZE(last_sent));
  322. spin_lock_irqsave(&dev->lock, flags);
  323. /* let everybody know we're now transmitting */
  324. dev->transmitting = true;
  325. /* and set the carrier values for transmission */
  326. ite_set_carrier_params(dev);
  327. /* calculate how much time we can send in one byte */
  328. max_rle_us =
  329. (ITE_BAUDRATE_DIVISOR * dev->params.sample_period *
  330. ITE_TX_MAX_RLE) / 1000;
  331. /* disable the receiver */
  332. dev->params.disable_rx(dev);
  333. /* this is where we'll begin filling in the FIFO, until it's full.
  334. * then we'll just activate the interrupt, wait for it to wake us up
  335. * again, disable it, continue filling the FIFO... until everything
  336. * has been pushed out */
  337. fifo_avail =
  338. ITE_TX_FIFO_LEN - dev->params.get_tx_used_slots(dev);
  339. while (n > 0 && dev->in_use) {
  340. /* transmit the next sample */
  341. is_pulse = !is_pulse;
  342. remaining_us = *(txbuf++);
  343. n--;
  344. ite_dbg("%s: %ld",
  345. ((is_pulse) ? "pulse" : "space"),
  346. (long int)
  347. remaining_us);
  348. /* repeat while the pulse is non-zero length */
  349. while (remaining_us > 0 && dev->in_use) {
  350. if (remaining_us > max_rle_us)
  351. next_rle_us = max_rle_us;
  352. else
  353. next_rle_us = remaining_us;
  354. remaining_us -= next_rle_us;
  355. /* check what's the length we have to pump out */
  356. val = (ITE_TX_MAX_RLE * next_rle_us) / max_rle_us;
  357. /* put it into the sent buffer */
  358. last_sent[last_idx++] = val;
  359. last_idx &= (ITE_TX_FIFO_LEN);
  360. /* encode it for 7 bits */
  361. val = (val - 1) & ITE_TX_RLE_MASK;
  362. /* take into account pulse/space prefix */
  363. if (is_pulse)
  364. val |= ITE_TX_PULSE;
  365. else
  366. val |= ITE_TX_SPACE;
  367. /*
  368. * if we get to 0 available, read again, just in case
  369. * some other slot got freed
  370. */
  371. if (fifo_avail <= 0)
  372. fifo_avail = ITE_TX_FIFO_LEN - dev->params.get_tx_used_slots(dev);
  373. /* if it's still full */
  374. if (fifo_avail <= 0) {
  375. /* enable the tx interrupt */
  376. dev->params.
  377. enable_tx_interrupt(dev);
  378. /* drop the spinlock */
  379. spin_unlock_irqrestore(&dev->lock, flags);
  380. /* wait for the FIFO to empty enough */
  381. wait_event_interruptible(dev->tx_queue, (fifo_avail = ITE_TX_FIFO_LEN - dev->params.get_tx_used_slots(dev)) >= 8);
  382. /* get the spinlock again */
  383. spin_lock_irqsave(&dev->lock, flags);
  384. /* disable the tx interrupt again. */
  385. dev->params.
  386. disable_tx_interrupt(dev);
  387. }
  388. /* now send the byte through the FIFO */
  389. dev->params.put_tx_byte(dev, val);
  390. fifo_avail--;
  391. }
  392. }
  393. /* wait and don't return until the whole FIFO has been sent out;
  394. * otherwise we could configure the RX carrier params instead of the
  395. * TX ones while the transmission is still being performed! */
  396. fifo_remaining = dev->params.get_tx_used_slots(dev);
  397. remaining_us = 0;
  398. while (fifo_remaining > 0) {
  399. fifo_remaining--;
  400. last_idx--;
  401. last_idx &= (ITE_TX_FIFO_LEN - 1);
  402. remaining_us += last_sent[last_idx];
  403. }
  404. remaining_us = (remaining_us * max_rle_us) / (ITE_TX_MAX_RLE);
  405. /* drop the spinlock while we sleep */
  406. spin_unlock_irqrestore(&dev->lock, flags);
  407. /* sleep remaining_us microseconds */
  408. mdelay(DIV_ROUND_UP(remaining_us, 1000));
  409. /* reacquire the spinlock */
  410. spin_lock_irqsave(&dev->lock, flags);
  411. /* now we're not transmitting anymore */
  412. dev->transmitting = false;
  413. /* and set the carrier values for reception */
  414. ite_set_carrier_params(dev);
  415. /* reenable the receiver */
  416. if (dev->in_use)
  417. dev->params.enable_rx(dev);
  418. /* notify transmission end */
  419. wake_up_interruptible(&dev->tx_ended);
  420. spin_unlock_irqrestore(&dev->lock, flags);
  421. return ret;
  422. }
  423. /* idle the receiver if needed */
  424. static void ite_s_idle(struct rc_dev *rcdev, bool enable)
  425. {
  426. unsigned long flags;
  427. struct ite_dev *dev = rcdev->priv;
  428. ite_dbg("%s called", __func__);
  429. if (enable) {
  430. spin_lock_irqsave(&dev->lock, flags);
  431. dev->params.idle_rx(dev);
  432. spin_unlock_irqrestore(&dev->lock, flags);
  433. }
  434. }
  435. /* IT8712F HW-specific functions */
  436. /* retrieve a bitmask of the current causes for a pending interrupt; this may
  437. * be composed of ITE_IRQ_TX_FIFO, ITE_IRQ_RX_FIFO and ITE_IRQ_RX_FIFO_OVERRUN
  438. * */
  439. static int it87_get_irq_causes(struct ite_dev *dev)
  440. {
  441. u8 iflags;
  442. int ret = 0;
  443. ite_dbg("%s called", __func__);
  444. /* read the interrupt flags */
  445. iflags = inb(dev->cir_addr + IT87_IIR) & IT87_II;
  446. switch (iflags) {
  447. case IT87_II_RXDS:
  448. ret = ITE_IRQ_RX_FIFO;
  449. break;
  450. case IT87_II_RXFO:
  451. ret = ITE_IRQ_RX_FIFO_OVERRUN;
  452. break;
  453. case IT87_II_TXLDL:
  454. ret = ITE_IRQ_TX_FIFO;
  455. break;
  456. }
  457. return ret;
  458. }
  459. /* set the carrier parameters; to be called with the spinlock held */
  460. static void it87_set_carrier_params(struct ite_dev *dev, bool high_freq,
  461. bool use_demodulator,
  462. u8 carrier_freq_bits, u8 allowance_bits,
  463. u8 pulse_width_bits)
  464. {
  465. u8 val;
  466. ite_dbg("%s called", __func__);
  467. /* program the RCR register */
  468. val = inb(dev->cir_addr + IT87_RCR)
  469. & ~(IT87_HCFS | IT87_RXEND | IT87_RXDCR);
  470. if (high_freq)
  471. val |= IT87_HCFS;
  472. if (use_demodulator)
  473. val |= IT87_RXEND;
  474. val |= allowance_bits;
  475. outb(val, dev->cir_addr + IT87_RCR);
  476. /* program the TCR2 register */
  477. outb((carrier_freq_bits << IT87_CFQ_SHIFT) | pulse_width_bits,
  478. dev->cir_addr + IT87_TCR2);
  479. }
  480. /* read up to buf_size bytes from the RX FIFO; to be called with the spinlock
  481. * held */
  482. static int it87_get_rx_bytes(struct ite_dev *dev, u8 * buf, int buf_size)
  483. {
  484. int fifo, read = 0;
  485. ite_dbg("%s called", __func__);
  486. /* read how many bytes are still in the FIFO */
  487. fifo = inb(dev->cir_addr + IT87_RSR) & IT87_RXFBC;
  488. while (fifo > 0 && buf_size > 0) {
  489. *(buf++) = inb(dev->cir_addr + IT87_DR);
  490. fifo--;
  491. read++;
  492. buf_size--;
  493. }
  494. return read;
  495. }
  496. /* return how many bytes are still in the FIFO; this will be called
  497. * with the device spinlock NOT HELD while waiting for the TX FIFO to get
  498. * empty; let's expect this won't be a problem */
  499. static int it87_get_tx_used_slots(struct ite_dev *dev)
  500. {
  501. ite_dbg("%s called", __func__);
  502. return inb(dev->cir_addr + IT87_TSR) & IT87_TXFBC;
  503. }
  504. /* put a byte to the TX fifo; this should be called with the spinlock held */
  505. static void it87_put_tx_byte(struct ite_dev *dev, u8 value)
  506. {
  507. outb(value, dev->cir_addr + IT87_DR);
  508. }
  509. /* idle the receiver so that we won't receive samples until another
  510. pulse is detected; this must be called with the device spinlock held */
  511. static void it87_idle_rx(struct ite_dev *dev)
  512. {
  513. ite_dbg("%s called", __func__);
  514. /* disable streaming by clearing RXACT writing it as 1 */
  515. outb(inb(dev->cir_addr + IT87_RCR) | IT87_RXACT,
  516. dev->cir_addr + IT87_RCR);
  517. /* clear the FIFO */
  518. outb(inb(dev->cir_addr + IT87_TCR1) | IT87_FIFOCLR,
  519. dev->cir_addr + IT87_TCR1);
  520. }
  521. /* disable the receiver; this must be called with the device spinlock held */
  522. static void it87_disable_rx(struct ite_dev *dev)
  523. {
  524. ite_dbg("%s called", __func__);
  525. /* disable the receiver interrupts */
  526. outb(inb(dev->cir_addr + IT87_IER) & ~(IT87_RDAIE | IT87_RFOIE),
  527. dev->cir_addr + IT87_IER);
  528. /* disable the receiver */
  529. outb(inb(dev->cir_addr + IT87_RCR) & ~IT87_RXEN,
  530. dev->cir_addr + IT87_RCR);
  531. /* clear the FIFO and RXACT (actually RXACT should have been cleared
  532. * in the previous outb() call) */
  533. it87_idle_rx(dev);
  534. }
  535. /* enable the receiver; this must be called with the device spinlock held */
  536. static void it87_enable_rx(struct ite_dev *dev)
  537. {
  538. ite_dbg("%s called", __func__);
  539. /* enable the receiver by setting RXEN */
  540. outb(inb(dev->cir_addr + IT87_RCR) | IT87_RXEN,
  541. dev->cir_addr + IT87_RCR);
  542. /* just prepare it to idle for the next reception */
  543. it87_idle_rx(dev);
  544. /* enable the receiver interrupts and master enable flag */
  545. outb(inb(dev->cir_addr + IT87_IER) | IT87_RDAIE | IT87_RFOIE | IT87_IEC,
  546. dev->cir_addr + IT87_IER);
  547. }
  548. /* disable the transmitter interrupt; this must be called with the device
  549. * spinlock held */
  550. static void it87_disable_tx_interrupt(struct ite_dev *dev)
  551. {
  552. ite_dbg("%s called", __func__);
  553. /* disable the transmitter interrupts */
  554. outb(inb(dev->cir_addr + IT87_IER) & ~IT87_TLDLIE,
  555. dev->cir_addr + IT87_IER);
  556. }
  557. /* enable the transmitter interrupt; this must be called with the device
  558. * spinlock held */
  559. static void it87_enable_tx_interrupt(struct ite_dev *dev)
  560. {
  561. ite_dbg("%s called", __func__);
  562. /* enable the transmitter interrupts and master enable flag */
  563. outb(inb(dev->cir_addr + IT87_IER) | IT87_TLDLIE | IT87_IEC,
  564. dev->cir_addr + IT87_IER);
  565. }
  566. /* disable the device; this must be called with the device spinlock held */
  567. static void it87_disable(struct ite_dev *dev)
  568. {
  569. ite_dbg("%s called", __func__);
  570. /* clear out all interrupt enable flags */
  571. outb(inb(dev->cir_addr + IT87_IER) &
  572. ~(IT87_IEC | IT87_RFOIE | IT87_RDAIE | IT87_TLDLIE),
  573. dev->cir_addr + IT87_IER);
  574. /* disable the receiver */
  575. it87_disable_rx(dev);
  576. /* erase the FIFO */
  577. outb(IT87_FIFOCLR | inb(dev->cir_addr + IT87_TCR1),
  578. dev->cir_addr + IT87_TCR1);
  579. }
  580. /* initialize the hardware */
  581. static void it87_init_hardware(struct ite_dev *dev)
  582. {
  583. ite_dbg("%s called", __func__);
  584. /* enable just the baud rate divisor register,
  585. disabling all the interrupts at the same time */
  586. outb((inb(dev->cir_addr + IT87_IER) &
  587. ~(IT87_IEC | IT87_RFOIE | IT87_RDAIE | IT87_TLDLIE)) | IT87_BR,
  588. dev->cir_addr + IT87_IER);
  589. /* write out the baud rate divisor */
  590. outb(ITE_BAUDRATE_DIVISOR & 0xff, dev->cir_addr + IT87_BDLR);
  591. outb((ITE_BAUDRATE_DIVISOR >> 8) & 0xff, dev->cir_addr + IT87_BDHR);
  592. /* disable the baud rate divisor register again */
  593. outb(inb(dev->cir_addr + IT87_IER) & ~IT87_BR,
  594. dev->cir_addr + IT87_IER);
  595. /* program the RCR register defaults */
  596. outb(ITE_RXDCR_DEFAULT, dev->cir_addr + IT87_RCR);
  597. /* program the TCR1 register */
  598. outb(IT87_TXMPM_DEFAULT | IT87_TXENDF | IT87_TXRLE
  599. | IT87_FIFOTL_DEFAULT | IT87_FIFOCLR,
  600. dev->cir_addr + IT87_TCR1);
  601. /* program the carrier parameters */
  602. ite_set_carrier_params(dev);
  603. }
  604. /* IT8512F on ITE8708 HW-specific functions */
  605. /* retrieve a bitmask of the current causes for a pending interrupt; this may
  606. * be composed of ITE_IRQ_TX_FIFO, ITE_IRQ_RX_FIFO and ITE_IRQ_RX_FIFO_OVERRUN
  607. * */
  608. static int it8708_get_irq_causes(struct ite_dev *dev)
  609. {
  610. u8 iflags;
  611. int ret = 0;
  612. ite_dbg("%s called", __func__);
  613. /* read the interrupt flags */
  614. iflags = inb(dev->cir_addr + IT8708_C0IIR);
  615. if (iflags & IT85_TLDLI)
  616. ret |= ITE_IRQ_TX_FIFO;
  617. if (iflags & IT85_RDAI)
  618. ret |= ITE_IRQ_RX_FIFO;
  619. if (iflags & IT85_RFOI)
  620. ret |= ITE_IRQ_RX_FIFO_OVERRUN;
  621. return ret;
  622. }
  623. /* set the carrier parameters; to be called with the spinlock held */
  624. static void it8708_set_carrier_params(struct ite_dev *dev, bool high_freq,
  625. bool use_demodulator,
  626. u8 carrier_freq_bits, u8 allowance_bits,
  627. u8 pulse_width_bits)
  628. {
  629. u8 val;
  630. ite_dbg("%s called", __func__);
  631. /* program the C0CFR register, with HRAE=1 */
  632. outb(inb(dev->cir_addr + IT8708_BANKSEL) | IT8708_HRAE,
  633. dev->cir_addr + IT8708_BANKSEL);
  634. val = (inb(dev->cir_addr + IT8708_C0CFR)
  635. & ~(IT85_HCFS | IT85_CFQ)) | carrier_freq_bits;
  636. if (high_freq)
  637. val |= IT85_HCFS;
  638. outb(val, dev->cir_addr + IT8708_C0CFR);
  639. outb(inb(dev->cir_addr + IT8708_BANKSEL) & ~IT8708_HRAE,
  640. dev->cir_addr + IT8708_BANKSEL);
  641. /* program the C0RCR register */
  642. val = inb(dev->cir_addr + IT8708_C0RCR)
  643. & ~(IT85_RXEND | IT85_RXDCR);
  644. if (use_demodulator)
  645. val |= IT85_RXEND;
  646. val |= allowance_bits;
  647. outb(val, dev->cir_addr + IT8708_C0RCR);
  648. /* program the C0TCR register */
  649. val = inb(dev->cir_addr + IT8708_C0TCR) & ~IT85_TXMPW;
  650. val |= pulse_width_bits;
  651. outb(val, dev->cir_addr + IT8708_C0TCR);
  652. }
  653. /* read up to buf_size bytes from the RX FIFO; to be called with the spinlock
  654. * held */
  655. static int it8708_get_rx_bytes(struct ite_dev *dev, u8 * buf, int buf_size)
  656. {
  657. int fifo, read = 0;
  658. ite_dbg("%s called", __func__);
  659. /* read how many bytes are still in the FIFO */
  660. fifo = inb(dev->cir_addr + IT8708_C0RFSR) & IT85_RXFBC;
  661. while (fifo > 0 && buf_size > 0) {
  662. *(buf++) = inb(dev->cir_addr + IT8708_C0DR);
  663. fifo--;
  664. read++;
  665. buf_size--;
  666. }
  667. return read;
  668. }
  669. /* return how many bytes are still in the FIFO; this will be called
  670. * with the device spinlock NOT HELD while waiting for the TX FIFO to get
  671. * empty; let's expect this won't be a problem */
  672. static int it8708_get_tx_used_slots(struct ite_dev *dev)
  673. {
  674. ite_dbg("%s called", __func__);
  675. return inb(dev->cir_addr + IT8708_C0TFSR) & IT85_TXFBC;
  676. }
  677. /* put a byte to the TX fifo; this should be called with the spinlock held */
  678. static void it8708_put_tx_byte(struct ite_dev *dev, u8 value)
  679. {
  680. outb(value, dev->cir_addr + IT8708_C0DR);
  681. }
  682. /* idle the receiver so that we won't receive samples until another
  683. pulse is detected; this must be called with the device spinlock held */
  684. static void it8708_idle_rx(struct ite_dev *dev)
  685. {
  686. ite_dbg("%s called", __func__);
  687. /* disable streaming by clearing RXACT writing it as 1 */
  688. outb(inb(dev->cir_addr + IT8708_C0RCR) | IT85_RXACT,
  689. dev->cir_addr + IT8708_C0RCR);
  690. /* clear the FIFO */
  691. outb(inb(dev->cir_addr + IT8708_C0MSTCR) | IT85_FIFOCLR,
  692. dev->cir_addr + IT8708_C0MSTCR);
  693. }
  694. /* disable the receiver; this must be called with the device spinlock held */
  695. static void it8708_disable_rx(struct ite_dev *dev)
  696. {
  697. ite_dbg("%s called", __func__);
  698. /* disable the receiver interrupts */
  699. outb(inb(dev->cir_addr + IT8708_C0IER) &
  700. ~(IT85_RDAIE | IT85_RFOIE),
  701. dev->cir_addr + IT8708_C0IER);
  702. /* disable the receiver */
  703. outb(inb(dev->cir_addr + IT8708_C0RCR) & ~IT85_RXEN,
  704. dev->cir_addr + IT8708_C0RCR);
  705. /* clear the FIFO and RXACT (actually RXACT should have been cleared
  706. * in the previous outb() call) */
  707. it8708_idle_rx(dev);
  708. }
  709. /* enable the receiver; this must be called with the device spinlock held */
  710. static void it8708_enable_rx(struct ite_dev *dev)
  711. {
  712. ite_dbg("%s called", __func__);
  713. /* enable the receiver by setting RXEN */
  714. outb(inb(dev->cir_addr + IT8708_C0RCR) | IT85_RXEN,
  715. dev->cir_addr + IT8708_C0RCR);
  716. /* just prepare it to idle for the next reception */
  717. it8708_idle_rx(dev);
  718. /* enable the receiver interrupts and master enable flag */
  719. outb(inb(dev->cir_addr + IT8708_C0IER)
  720. |IT85_RDAIE | IT85_RFOIE | IT85_IEC,
  721. dev->cir_addr + IT8708_C0IER);
  722. }
  723. /* disable the transmitter interrupt; this must be called with the device
  724. * spinlock held */
  725. static void it8708_disable_tx_interrupt(struct ite_dev *dev)
  726. {
  727. ite_dbg("%s called", __func__);
  728. /* disable the transmitter interrupts */
  729. outb(inb(dev->cir_addr + IT8708_C0IER) & ~IT85_TLDLIE,
  730. dev->cir_addr + IT8708_C0IER);
  731. }
  732. /* enable the transmitter interrupt; this must be called with the device
  733. * spinlock held */
  734. static void it8708_enable_tx_interrupt(struct ite_dev *dev)
  735. {
  736. ite_dbg("%s called", __func__);
  737. /* enable the transmitter interrupts and master enable flag */
  738. outb(inb(dev->cir_addr + IT8708_C0IER)
  739. |IT85_TLDLIE | IT85_IEC,
  740. dev->cir_addr + IT8708_C0IER);
  741. }
  742. /* disable the device; this must be called with the device spinlock held */
  743. static void it8708_disable(struct ite_dev *dev)
  744. {
  745. ite_dbg("%s called", __func__);
  746. /* clear out all interrupt enable flags */
  747. outb(inb(dev->cir_addr + IT8708_C0IER) &
  748. ~(IT85_IEC | IT85_RFOIE | IT85_RDAIE | IT85_TLDLIE),
  749. dev->cir_addr + IT8708_C0IER);
  750. /* disable the receiver */
  751. it8708_disable_rx(dev);
  752. /* erase the FIFO */
  753. outb(IT85_FIFOCLR | inb(dev->cir_addr + IT8708_C0MSTCR),
  754. dev->cir_addr + IT8708_C0MSTCR);
  755. }
  756. /* initialize the hardware */
  757. static void it8708_init_hardware(struct ite_dev *dev)
  758. {
  759. ite_dbg("%s called", __func__);
  760. /* disable all the interrupts */
  761. outb(inb(dev->cir_addr + IT8708_C0IER) &
  762. ~(IT85_IEC | IT85_RFOIE | IT85_RDAIE | IT85_TLDLIE),
  763. dev->cir_addr + IT8708_C0IER);
  764. /* program the baud rate divisor */
  765. outb(inb(dev->cir_addr + IT8708_BANKSEL) | IT8708_HRAE,
  766. dev->cir_addr + IT8708_BANKSEL);
  767. outb(ITE_BAUDRATE_DIVISOR & 0xff, dev->cir_addr + IT8708_C0BDLR);
  768. outb((ITE_BAUDRATE_DIVISOR >> 8) & 0xff,
  769. dev->cir_addr + IT8708_C0BDHR);
  770. outb(inb(dev->cir_addr + IT8708_BANKSEL) & ~IT8708_HRAE,
  771. dev->cir_addr + IT8708_BANKSEL);
  772. /* program the C0MSTCR register defaults */
  773. outb((inb(dev->cir_addr + IT8708_C0MSTCR) &
  774. ~(IT85_ILSEL | IT85_ILE | IT85_FIFOTL |
  775. IT85_FIFOCLR | IT85_RESET)) |
  776. IT85_FIFOTL_DEFAULT,
  777. dev->cir_addr + IT8708_C0MSTCR);
  778. /* program the C0RCR register defaults */
  779. outb((inb(dev->cir_addr + IT8708_C0RCR) &
  780. ~(IT85_RXEN | IT85_RDWOS | IT85_RXEND |
  781. IT85_RXACT | IT85_RXDCR)) |
  782. ITE_RXDCR_DEFAULT,
  783. dev->cir_addr + IT8708_C0RCR);
  784. /* program the C0TCR register defaults */
  785. outb((inb(dev->cir_addr + IT8708_C0TCR) &
  786. ~(IT85_TXMPM | IT85_TXMPW))
  787. |IT85_TXRLE | IT85_TXENDF |
  788. IT85_TXMPM_DEFAULT | IT85_TXMPW_DEFAULT,
  789. dev->cir_addr + IT8708_C0TCR);
  790. /* program the carrier parameters */
  791. ite_set_carrier_params(dev);
  792. }
  793. /* IT8512F on ITE8709 HW-specific functions */
  794. /* read a byte from the SRAM module */
  795. static inline u8 it8709_rm(struct ite_dev *dev, int index)
  796. {
  797. outb(index, dev->cir_addr + IT8709_RAM_IDX);
  798. return inb(dev->cir_addr + IT8709_RAM_VAL);
  799. }
  800. /* write a byte to the SRAM module */
  801. static inline void it8709_wm(struct ite_dev *dev, u8 val, int index)
  802. {
  803. outb(index, dev->cir_addr + IT8709_RAM_IDX);
  804. outb(val, dev->cir_addr + IT8709_RAM_VAL);
  805. }
  806. static void it8709_wait(struct ite_dev *dev)
  807. {
  808. int i = 0;
  809. /*
  810. * loop until device tells it's ready to continue
  811. * iterations count is usually ~750 but can sometimes achieve 13000
  812. */
  813. for (i = 0; i < 15000; i++) {
  814. udelay(2);
  815. if (it8709_rm(dev, IT8709_MODE) == IT8709_IDLE)
  816. break;
  817. }
  818. }
  819. /* read the value of a CIR register */
  820. static u8 it8709_rr(struct ite_dev *dev, int index)
  821. {
  822. /* just wait in case the previous access was a write */
  823. it8709_wait(dev);
  824. it8709_wm(dev, index, IT8709_REG_IDX);
  825. it8709_wm(dev, IT8709_READ, IT8709_MODE);
  826. /* wait for the read data to be available */
  827. it8709_wait(dev);
  828. /* return the read value */
  829. return it8709_rm(dev, IT8709_REG_VAL);
  830. }
  831. /* write the value of a CIR register */
  832. static void it8709_wr(struct ite_dev *dev, u8 val, int index)
  833. {
  834. /* we wait before writing, and not afterwards, since this allows us to
  835. * pipeline the host CPU with the microcontroller */
  836. it8709_wait(dev);
  837. it8709_wm(dev, val, IT8709_REG_VAL);
  838. it8709_wm(dev, index, IT8709_REG_IDX);
  839. it8709_wm(dev, IT8709_WRITE, IT8709_MODE);
  840. }
  841. /* retrieve a bitmask of the current causes for a pending interrupt; this may
  842. * be composed of ITE_IRQ_TX_FIFO, ITE_IRQ_RX_FIFO and ITE_IRQ_RX_FIFO_OVERRUN
  843. * */
  844. static int it8709_get_irq_causes(struct ite_dev *dev)
  845. {
  846. u8 iflags;
  847. int ret = 0;
  848. ite_dbg("%s called", __func__);
  849. /* read the interrupt flags */
  850. iflags = it8709_rm(dev, IT8709_IIR);
  851. if (iflags & IT85_TLDLI)
  852. ret |= ITE_IRQ_TX_FIFO;
  853. if (iflags & IT85_RDAI)
  854. ret |= ITE_IRQ_RX_FIFO;
  855. if (iflags & IT85_RFOI)
  856. ret |= ITE_IRQ_RX_FIFO_OVERRUN;
  857. return ret;
  858. }
  859. /* set the carrier parameters; to be called with the spinlock held */
  860. static void it8709_set_carrier_params(struct ite_dev *dev, bool high_freq,
  861. bool use_demodulator,
  862. u8 carrier_freq_bits, u8 allowance_bits,
  863. u8 pulse_width_bits)
  864. {
  865. u8 val;
  866. ite_dbg("%s called", __func__);
  867. val = (it8709_rr(dev, IT85_C0CFR)
  868. &~(IT85_HCFS | IT85_CFQ)) |
  869. carrier_freq_bits;
  870. if (high_freq)
  871. val |= IT85_HCFS;
  872. it8709_wr(dev, val, IT85_C0CFR);
  873. /* program the C0RCR register */
  874. val = it8709_rr(dev, IT85_C0RCR)
  875. & ~(IT85_RXEND | IT85_RXDCR);
  876. if (use_demodulator)
  877. val |= IT85_RXEND;
  878. val |= allowance_bits;
  879. it8709_wr(dev, val, IT85_C0RCR);
  880. /* program the C0TCR register */
  881. val = it8709_rr(dev, IT85_C0TCR) & ~IT85_TXMPW;
  882. val |= pulse_width_bits;
  883. it8709_wr(dev, val, IT85_C0TCR);
  884. }
  885. /* read up to buf_size bytes from the RX FIFO; to be called with the spinlock
  886. * held */
  887. static int it8709_get_rx_bytes(struct ite_dev *dev, u8 * buf, int buf_size)
  888. {
  889. int fifo, read = 0;
  890. ite_dbg("%s called", __func__);
  891. /* read how many bytes are still in the FIFO */
  892. fifo = it8709_rm(dev, IT8709_RFSR) & IT85_RXFBC;
  893. while (fifo > 0 && buf_size > 0) {
  894. *(buf++) = it8709_rm(dev, IT8709_FIFO + read);
  895. fifo--;
  896. read++;
  897. buf_size--;
  898. }
  899. /* 'clear' the FIFO by setting the writing index to 0; this is
  900. * completely bound to be racy, but we can't help it, since it's a
  901. * limitation of the protocol */
  902. it8709_wm(dev, 0, IT8709_RFSR);
  903. return read;
  904. }
  905. /* return how many bytes are still in the FIFO; this will be called
  906. * with the device spinlock NOT HELD while waiting for the TX FIFO to get
  907. * empty; let's expect this won't be a problem */
  908. static int it8709_get_tx_used_slots(struct ite_dev *dev)
  909. {
  910. ite_dbg("%s called", __func__);
  911. return it8709_rr(dev, IT85_C0TFSR) & IT85_TXFBC;
  912. }
  913. /* put a byte to the TX fifo; this should be called with the spinlock held */
  914. static void it8709_put_tx_byte(struct ite_dev *dev, u8 value)
  915. {
  916. it8709_wr(dev, value, IT85_C0DR);
  917. }
  918. /* idle the receiver so that we won't receive samples until another
  919. pulse is detected; this must be called with the device spinlock held */
  920. static void it8709_idle_rx(struct ite_dev *dev)
  921. {
  922. ite_dbg("%s called", __func__);
  923. /* disable streaming by clearing RXACT writing it as 1 */
  924. it8709_wr(dev, it8709_rr(dev, IT85_C0RCR) | IT85_RXACT,
  925. IT85_C0RCR);
  926. /* clear the FIFO */
  927. it8709_wr(dev, it8709_rr(dev, IT85_C0MSTCR) | IT85_FIFOCLR,
  928. IT85_C0MSTCR);
  929. }
  930. /* disable the receiver; this must be called with the device spinlock held */
  931. static void it8709_disable_rx(struct ite_dev *dev)
  932. {
  933. ite_dbg("%s called", __func__);
  934. /* disable the receiver interrupts */
  935. it8709_wr(dev, it8709_rr(dev, IT85_C0IER) &
  936. ~(IT85_RDAIE | IT85_RFOIE),
  937. IT85_C0IER);
  938. /* disable the receiver */
  939. it8709_wr(dev, it8709_rr(dev, IT85_C0RCR) & ~IT85_RXEN,
  940. IT85_C0RCR);
  941. /* clear the FIFO and RXACT (actually RXACT should have been cleared
  942. * in the previous it8709_wr(dev, ) call) */
  943. it8709_idle_rx(dev);
  944. }
  945. /* enable the receiver; this must be called with the device spinlock held */
  946. static void it8709_enable_rx(struct ite_dev *dev)
  947. {
  948. ite_dbg("%s called", __func__);
  949. /* enable the receiver by setting RXEN */
  950. it8709_wr(dev, it8709_rr(dev, IT85_C0RCR) | IT85_RXEN,
  951. IT85_C0RCR);
  952. /* just prepare it to idle for the next reception */
  953. it8709_idle_rx(dev);
  954. /* enable the receiver interrupts and master enable flag */
  955. it8709_wr(dev, it8709_rr(dev, IT85_C0IER)
  956. |IT85_RDAIE | IT85_RFOIE | IT85_IEC,
  957. IT85_C0IER);
  958. }
  959. /* disable the transmitter interrupt; this must be called with the device
  960. * spinlock held */
  961. static void it8709_disable_tx_interrupt(struct ite_dev *dev)
  962. {
  963. ite_dbg("%s called", __func__);
  964. /* disable the transmitter interrupts */
  965. it8709_wr(dev, it8709_rr(dev, IT85_C0IER) & ~IT85_TLDLIE,
  966. IT85_C0IER);
  967. }
  968. /* enable the transmitter interrupt; this must be called with the device
  969. * spinlock held */
  970. static void it8709_enable_tx_interrupt(struct ite_dev *dev)
  971. {
  972. ite_dbg("%s called", __func__);
  973. /* enable the transmitter interrupts and master enable flag */
  974. it8709_wr(dev, it8709_rr(dev, IT85_C0IER)
  975. |IT85_TLDLIE | IT85_IEC,
  976. IT85_C0IER);
  977. }
  978. /* disable the device; this must be called with the device spinlock held */
  979. static void it8709_disable(struct ite_dev *dev)
  980. {
  981. ite_dbg("%s called", __func__);
  982. /* clear out all interrupt enable flags */
  983. it8709_wr(dev, it8709_rr(dev, IT85_C0IER) &
  984. ~(IT85_IEC | IT85_RFOIE | IT85_RDAIE | IT85_TLDLIE),
  985. IT85_C0IER);
  986. /* disable the receiver */
  987. it8709_disable_rx(dev);
  988. /* erase the FIFO */
  989. it8709_wr(dev, IT85_FIFOCLR | it8709_rr(dev, IT85_C0MSTCR),
  990. IT85_C0MSTCR);
  991. }
  992. /* initialize the hardware */
  993. static void it8709_init_hardware(struct ite_dev *dev)
  994. {
  995. ite_dbg("%s called", __func__);
  996. /* disable all the interrupts */
  997. it8709_wr(dev, it8709_rr(dev, IT85_C0IER) &
  998. ~(IT85_IEC | IT85_RFOIE | IT85_RDAIE | IT85_TLDLIE),
  999. IT85_C0IER);
  1000. /* program the baud rate divisor */
  1001. it8709_wr(dev, ITE_BAUDRATE_DIVISOR & 0xff, IT85_C0BDLR);
  1002. it8709_wr(dev, (ITE_BAUDRATE_DIVISOR >> 8) & 0xff,
  1003. IT85_C0BDHR);
  1004. /* program the C0MSTCR register defaults */
  1005. it8709_wr(dev, (it8709_rr(dev, IT85_C0MSTCR) &
  1006. ~(IT85_ILSEL | IT85_ILE | IT85_FIFOTL
  1007. | IT85_FIFOCLR | IT85_RESET)) | IT85_FIFOTL_DEFAULT,
  1008. IT85_C0MSTCR);
  1009. /* program the C0RCR register defaults */
  1010. it8709_wr(dev, (it8709_rr(dev, IT85_C0RCR) &
  1011. ~(IT85_RXEN | IT85_RDWOS | IT85_RXEND | IT85_RXACT
  1012. | IT85_RXDCR)) | ITE_RXDCR_DEFAULT,
  1013. IT85_C0RCR);
  1014. /* program the C0TCR register defaults */
  1015. it8709_wr(dev, (it8709_rr(dev, IT85_C0TCR) & ~(IT85_TXMPM | IT85_TXMPW))
  1016. | IT85_TXRLE | IT85_TXENDF | IT85_TXMPM_DEFAULT
  1017. | IT85_TXMPW_DEFAULT,
  1018. IT85_C0TCR);
  1019. /* program the carrier parameters */
  1020. ite_set_carrier_params(dev);
  1021. }
  1022. /* generic hardware setup/teardown code */
  1023. /* activate the device for use */
  1024. static int ite_open(struct rc_dev *rcdev)
  1025. {
  1026. struct ite_dev *dev = rcdev->priv;
  1027. unsigned long flags;
  1028. ite_dbg("%s called", __func__);
  1029. spin_lock_irqsave(&dev->lock, flags);
  1030. dev->in_use = true;
  1031. /* enable the receiver */
  1032. dev->params.enable_rx(dev);
  1033. spin_unlock_irqrestore(&dev->lock, flags);
  1034. return 0;
  1035. }
  1036. /* deactivate the device for use */
  1037. static void ite_close(struct rc_dev *rcdev)
  1038. {
  1039. struct ite_dev *dev = rcdev->priv;
  1040. unsigned long flags;
  1041. ite_dbg("%s called", __func__);
  1042. spin_lock_irqsave(&dev->lock, flags);
  1043. dev->in_use = false;
  1044. /* wait for any transmission to end */
  1045. spin_unlock_irqrestore(&dev->lock, flags);
  1046. wait_event_interruptible(dev->tx_ended, !dev->transmitting);
  1047. spin_lock_irqsave(&dev->lock, flags);
  1048. dev->params.disable(dev);
  1049. spin_unlock_irqrestore(&dev->lock, flags);
  1050. }
  1051. /* supported models and their parameters */
  1052. static const struct ite_dev_params ite_dev_descs[] = {
  1053. { /* 0: ITE8704 */
  1054. .model = "ITE8704 CIR transceiver",
  1055. .io_region_size = IT87_IOREG_LENGTH,
  1056. .io_rsrc_no = 0,
  1057. .hw_tx_capable = true,
  1058. .sample_period = (u32) (1000000000ULL / 115200),
  1059. .tx_carrier_freq = 38000,
  1060. .tx_duty_cycle = 33,
  1061. .rx_low_carrier_freq = 0,
  1062. .rx_high_carrier_freq = 0,
  1063. /* operations */
  1064. .get_irq_causes = it87_get_irq_causes,
  1065. .enable_rx = it87_enable_rx,
  1066. .idle_rx = it87_idle_rx,
  1067. .disable_rx = it87_idle_rx,
  1068. .get_rx_bytes = it87_get_rx_bytes,
  1069. .enable_tx_interrupt = it87_enable_tx_interrupt,
  1070. .disable_tx_interrupt = it87_disable_tx_interrupt,
  1071. .get_tx_used_slots = it87_get_tx_used_slots,
  1072. .put_tx_byte = it87_put_tx_byte,
  1073. .disable = it87_disable,
  1074. .init_hardware = it87_init_hardware,
  1075. .set_carrier_params = it87_set_carrier_params,
  1076. },
  1077. { /* 1: ITE8713 */
  1078. .model = "ITE8713 CIR transceiver",
  1079. .io_region_size = IT87_IOREG_LENGTH,
  1080. .io_rsrc_no = 0,
  1081. .hw_tx_capable = true,
  1082. .sample_period = (u32) (1000000000ULL / 115200),
  1083. .tx_carrier_freq = 38000,
  1084. .tx_duty_cycle = 33,
  1085. .rx_low_carrier_freq = 0,
  1086. .rx_high_carrier_freq = 0,
  1087. /* operations */
  1088. .get_irq_causes = it87_get_irq_causes,
  1089. .enable_rx = it87_enable_rx,
  1090. .idle_rx = it87_idle_rx,
  1091. .disable_rx = it87_idle_rx,
  1092. .get_rx_bytes = it87_get_rx_bytes,
  1093. .enable_tx_interrupt = it87_enable_tx_interrupt,
  1094. .disable_tx_interrupt = it87_disable_tx_interrupt,
  1095. .get_tx_used_slots = it87_get_tx_used_slots,
  1096. .put_tx_byte = it87_put_tx_byte,
  1097. .disable = it87_disable,
  1098. .init_hardware = it87_init_hardware,
  1099. .set_carrier_params = it87_set_carrier_params,
  1100. },
  1101. { /* 2: ITE8708 */
  1102. .model = "ITE8708 CIR transceiver",
  1103. .io_region_size = IT8708_IOREG_LENGTH,
  1104. .io_rsrc_no = 0,
  1105. .hw_tx_capable = true,
  1106. .sample_period = (u32) (1000000000ULL / 115200),
  1107. .tx_carrier_freq = 38000,
  1108. .tx_duty_cycle = 33,
  1109. .rx_low_carrier_freq = 0,
  1110. .rx_high_carrier_freq = 0,
  1111. /* operations */
  1112. .get_irq_causes = it8708_get_irq_causes,
  1113. .enable_rx = it8708_enable_rx,
  1114. .idle_rx = it8708_idle_rx,
  1115. .disable_rx = it8708_idle_rx,
  1116. .get_rx_bytes = it8708_get_rx_bytes,
  1117. .enable_tx_interrupt = it8708_enable_tx_interrupt,
  1118. .disable_tx_interrupt =
  1119. it8708_disable_tx_interrupt,
  1120. .get_tx_used_slots = it8708_get_tx_used_slots,
  1121. .put_tx_byte = it8708_put_tx_byte,
  1122. .disable = it8708_disable,
  1123. .init_hardware = it8708_init_hardware,
  1124. .set_carrier_params = it8708_set_carrier_params,
  1125. },
  1126. { /* 3: ITE8709 */
  1127. .model = "ITE8709 CIR transceiver",
  1128. .io_region_size = IT8709_IOREG_LENGTH,
  1129. .io_rsrc_no = 2,
  1130. .hw_tx_capable = true,
  1131. .sample_period = (u32) (1000000000ULL / 115200),
  1132. .tx_carrier_freq = 38000,
  1133. .tx_duty_cycle = 33,
  1134. .rx_low_carrier_freq = 0,
  1135. .rx_high_carrier_freq = 0,
  1136. /* operations */
  1137. .get_irq_causes = it8709_get_irq_causes,
  1138. .enable_rx = it8709_enable_rx,
  1139. .idle_rx = it8709_idle_rx,
  1140. .disable_rx = it8709_idle_rx,
  1141. .get_rx_bytes = it8709_get_rx_bytes,
  1142. .enable_tx_interrupt = it8709_enable_tx_interrupt,
  1143. .disable_tx_interrupt =
  1144. it8709_disable_tx_interrupt,
  1145. .get_tx_used_slots = it8709_get_tx_used_slots,
  1146. .put_tx_byte = it8709_put_tx_byte,
  1147. .disable = it8709_disable,
  1148. .init_hardware = it8709_init_hardware,
  1149. .set_carrier_params = it8709_set_carrier_params,
  1150. },
  1151. };
  1152. static const struct pnp_device_id ite_ids[] = {
  1153. {"ITE8704", 0}, /* Default model */
  1154. {"ITE8713", 1}, /* CIR found in EEEBox 1501U */
  1155. {"ITE8708", 2}, /* Bridged IT8512 */
  1156. {"ITE8709", 3}, /* SRAM-Bridged IT8512 */
  1157. {"", 0},
  1158. };
  1159. /* allocate memory, probe hardware, and initialize everything */
  1160. static int ite_probe(struct pnp_dev *pdev, const struct pnp_device_id
  1161. *dev_id)
  1162. {
  1163. const struct ite_dev_params *dev_desc = NULL;
  1164. struct ite_dev *itdev = NULL;
  1165. struct rc_dev *rdev = NULL;
  1166. int ret = -ENOMEM;
  1167. int model_no;
  1168. int io_rsrc_no;
  1169. ite_dbg("%s called", __func__);
  1170. itdev = kzalloc(sizeof(struct ite_dev), GFP_KERNEL);
  1171. if (!itdev)
  1172. return ret;
  1173. /* input device for IR remote (and tx) */
  1174. rdev = rc_allocate_device(RC_DRIVER_IR_RAW);
  1175. if (!rdev)
  1176. goto exit_free_dev_rdev;
  1177. itdev->rdev = rdev;
  1178. ret = -ENODEV;
  1179. /* get the model number */
  1180. model_no = (int)dev_id->driver_data;
  1181. ite_pr(KERN_NOTICE, "Auto-detected model: %s\n",
  1182. ite_dev_descs[model_no].model);
  1183. if (model_number >= 0 && model_number < ARRAY_SIZE(ite_dev_descs)) {
  1184. model_no = model_number;
  1185. ite_pr(KERN_NOTICE, "The model has been fixed by a module parameter.");
  1186. }
  1187. ite_pr(KERN_NOTICE, "Using model: %s\n", ite_dev_descs[model_no].model);
  1188. /* get the description for the device */
  1189. dev_desc = &ite_dev_descs[model_no];
  1190. io_rsrc_no = dev_desc->io_rsrc_no;
  1191. /* validate pnp resources */
  1192. if (!pnp_port_valid(pdev, io_rsrc_no) ||
  1193. pnp_port_len(pdev, io_rsrc_no) != dev_desc->io_region_size) {
  1194. dev_err(&pdev->dev, "IR PNP Port not valid!\n");
  1195. goto exit_free_dev_rdev;
  1196. }
  1197. if (!pnp_irq_valid(pdev, 0)) {
  1198. dev_err(&pdev->dev, "PNP IRQ not valid!\n");
  1199. goto exit_free_dev_rdev;
  1200. }
  1201. /* store resource values */
  1202. itdev->cir_addr = pnp_port_start(pdev, io_rsrc_no);
  1203. itdev->cir_irq = pnp_irq(pdev, 0);
  1204. /* initialize spinlocks */
  1205. spin_lock_init(&itdev->lock);
  1206. /* initialize raw event */
  1207. init_ir_raw_event(&itdev->rawir);
  1208. /* set driver data into the pnp device */
  1209. pnp_set_drvdata(pdev, itdev);
  1210. itdev->pdev = pdev;
  1211. /* initialize waitqueues for transmission */
  1212. init_waitqueue_head(&itdev->tx_queue);
  1213. init_waitqueue_head(&itdev->tx_ended);
  1214. /* copy model-specific parameters */
  1215. itdev->params = *dev_desc;
  1216. /* apply any overrides */
  1217. if (sample_period > 0)
  1218. itdev->params.sample_period = sample_period;
  1219. if (tx_carrier_freq > 0)
  1220. itdev->params.tx_carrier_freq = tx_carrier_freq;
  1221. if (tx_duty_cycle > 0 && tx_duty_cycle <= 100)
  1222. itdev->params.tx_duty_cycle = tx_duty_cycle;
  1223. if (rx_low_carrier_freq > 0)
  1224. itdev->params.rx_low_carrier_freq = rx_low_carrier_freq;
  1225. if (rx_high_carrier_freq > 0)
  1226. itdev->params.rx_high_carrier_freq = rx_high_carrier_freq;
  1227. /* print out parameters */
  1228. ite_pr(KERN_NOTICE, "TX-capable: %d\n", (int)
  1229. itdev->params.hw_tx_capable);
  1230. ite_pr(KERN_NOTICE, "Sample period (ns): %ld\n", (long)
  1231. itdev->params.sample_period);
  1232. ite_pr(KERN_NOTICE, "TX carrier frequency (Hz): %d\n", (int)
  1233. itdev->params.tx_carrier_freq);
  1234. ite_pr(KERN_NOTICE, "TX duty cycle (%%): %d\n", (int)
  1235. itdev->params.tx_duty_cycle);
  1236. ite_pr(KERN_NOTICE, "RX low carrier frequency (Hz): %d\n", (int)
  1237. itdev->params.rx_low_carrier_freq);
  1238. ite_pr(KERN_NOTICE, "RX high carrier frequency (Hz): %d\n", (int)
  1239. itdev->params.rx_high_carrier_freq);
  1240. /* set up hardware initial state */
  1241. itdev->params.init_hardware(itdev);
  1242. /* set up ir-core props */
  1243. rdev->priv = itdev;
  1244. rdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
  1245. rdev->open = ite_open;
  1246. rdev->close = ite_close;
  1247. rdev->s_idle = ite_s_idle;
  1248. rdev->s_rx_carrier_range = ite_set_rx_carrier_range;
  1249. /* FIFO threshold is 17 bytes, so 17 * 8 samples minimum */
  1250. rdev->min_timeout = 17 * 8 * ITE_BAUDRATE_DIVISOR *
  1251. itdev->params.sample_period;
  1252. rdev->timeout = IR_DEFAULT_TIMEOUT;
  1253. rdev->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
  1254. rdev->rx_resolution = ITE_BAUDRATE_DIVISOR *
  1255. itdev->params.sample_period;
  1256. rdev->tx_resolution = ITE_BAUDRATE_DIVISOR *
  1257. itdev->params.sample_period;
  1258. /* set up transmitter related values if needed */
  1259. if (itdev->params.hw_tx_capable) {
  1260. rdev->tx_ir = ite_tx_ir;
  1261. rdev->s_tx_carrier = ite_set_tx_carrier;
  1262. rdev->s_tx_duty_cycle = ite_set_tx_duty_cycle;
  1263. }
  1264. rdev->device_name = dev_desc->model;
  1265. rdev->input_id.bustype = BUS_HOST;
  1266. rdev->input_id.vendor = PCI_VENDOR_ID_ITE;
  1267. rdev->input_id.product = 0;
  1268. rdev->input_id.version = 0;
  1269. rdev->driver_name = ITE_DRIVER_NAME;
  1270. rdev->map_name = RC_MAP_RC6_MCE;
  1271. ret = rc_register_device(rdev);
  1272. if (ret)
  1273. goto exit_free_dev_rdev;
  1274. ret = -EBUSY;
  1275. /* now claim resources */
  1276. if (!request_region(itdev->cir_addr,
  1277. dev_desc->io_region_size, ITE_DRIVER_NAME))
  1278. goto exit_unregister_device;
  1279. if (request_irq(itdev->cir_irq, ite_cir_isr, IRQF_SHARED,
  1280. ITE_DRIVER_NAME, (void *)itdev))
  1281. goto exit_release_cir_addr;
  1282. ite_pr(KERN_NOTICE, "driver has been successfully loaded\n");
  1283. return 0;
  1284. exit_release_cir_addr:
  1285. release_region(itdev->cir_addr, itdev->params.io_region_size);
  1286. exit_unregister_device:
  1287. rc_unregister_device(rdev);
  1288. rdev = NULL;
  1289. exit_free_dev_rdev:
  1290. rc_free_device(rdev);
  1291. kfree(itdev);
  1292. return ret;
  1293. }
  1294. static void ite_remove(struct pnp_dev *pdev)
  1295. {
  1296. struct ite_dev *dev = pnp_get_drvdata(pdev);
  1297. unsigned long flags;
  1298. ite_dbg("%s called", __func__);
  1299. spin_lock_irqsave(&dev->lock, flags);
  1300. /* disable hardware */
  1301. dev->params.disable(dev);
  1302. spin_unlock_irqrestore(&dev->lock, flags);
  1303. /* free resources */
  1304. free_irq(dev->cir_irq, dev);
  1305. release_region(dev->cir_addr, dev->params.io_region_size);
  1306. rc_unregister_device(dev->rdev);
  1307. kfree(dev);
  1308. }
  1309. static int ite_suspend(struct pnp_dev *pdev, pm_message_t state)
  1310. {
  1311. struct ite_dev *dev = pnp_get_drvdata(pdev);
  1312. unsigned long flags;
  1313. ite_dbg("%s called", __func__);
  1314. /* wait for any transmission to end */
  1315. wait_event_interruptible(dev->tx_ended, !dev->transmitting);
  1316. spin_lock_irqsave(&dev->lock, flags);
  1317. /* disable all interrupts */
  1318. dev->params.disable(dev);
  1319. spin_unlock_irqrestore(&dev->lock, flags);
  1320. return 0;
  1321. }
  1322. static int ite_resume(struct pnp_dev *pdev)
  1323. {
  1324. struct ite_dev *dev = pnp_get_drvdata(pdev);
  1325. unsigned long flags;
  1326. ite_dbg("%s called", __func__);
  1327. spin_lock_irqsave(&dev->lock, flags);
  1328. /* reinitialize hardware config registers */
  1329. dev->params.init_hardware(dev);
  1330. /* enable the receiver */
  1331. dev->params.enable_rx(dev);
  1332. spin_unlock_irqrestore(&dev->lock, flags);
  1333. return 0;
  1334. }
  1335. static void ite_shutdown(struct pnp_dev *pdev)
  1336. {
  1337. struct ite_dev *dev = pnp_get_drvdata(pdev);
  1338. unsigned long flags;
  1339. ite_dbg("%s called", __func__);
  1340. spin_lock_irqsave(&dev->lock, flags);
  1341. /* disable all interrupts */
  1342. dev->params.disable(dev);
  1343. spin_unlock_irqrestore(&dev->lock, flags);
  1344. }
  1345. static struct pnp_driver ite_driver = {
  1346. .name = ITE_DRIVER_NAME,
  1347. .id_table = ite_ids,
  1348. .probe = ite_probe,
  1349. .remove = ite_remove,
  1350. .suspend = ite_suspend,
  1351. .resume = ite_resume,
  1352. .shutdown = ite_shutdown,
  1353. };
  1354. MODULE_DEVICE_TABLE(pnp, ite_ids);
  1355. MODULE_DESCRIPTION("ITE Tech Inc. IT8712F/ITE8512F CIR driver");
  1356. MODULE_AUTHOR("Juan J. Garcia de Soria <skandalfo@gmail.com>");
  1357. MODULE_LICENSE("GPL");
  1358. module_pnp_driver(ite_driver);