ite-cir.c 46 KB

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