ite-cir.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736
  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/slab.h>
  39. #include <linux/input.h>
  40. #include <linux/bitops.h>
  41. #include <media/rc-core.h>
  42. #include <linux/pci_ids.h>
  43. #include "ite-cir.h"
  44. /* module parameters */
  45. /* debug level */
  46. static int debug;
  47. module_param(debug, int, S_IRUGO | S_IWUSR);
  48. MODULE_PARM_DESC(debug, "Enable debugging output");
  49. /* low limit for RX carrier freq, Hz, 0 for no RX demodulation */
  50. static int rx_low_carrier_freq;
  51. module_param(rx_low_carrier_freq, int, S_IRUGO | S_IWUSR);
  52. MODULE_PARM_DESC(rx_low_carrier_freq, "Override low RX carrier frequency, Hz, "
  53. "0 for no RX demodulation");
  54. /* high limit for RX carrier freq, Hz, 0 for no RX demodulation */
  55. static int rx_high_carrier_freq;
  56. module_param(rx_high_carrier_freq, int, S_IRUGO | S_IWUSR);
  57. MODULE_PARM_DESC(rx_high_carrier_freq, "Override high RX carrier frequency, "
  58. "Hz, 0 for no RX demodulation");
  59. /* override tx carrier frequency */
  60. static int tx_carrier_freq;
  61. module_param(tx_carrier_freq, int, S_IRUGO | S_IWUSR);
  62. MODULE_PARM_DESC(tx_carrier_freq, "Override TX carrier frequency, Hz");
  63. /* override tx duty cycle */
  64. static int tx_duty_cycle;
  65. module_param(tx_duty_cycle, int, S_IRUGO | S_IWUSR);
  66. MODULE_PARM_DESC(tx_duty_cycle, "Override TX duty cycle, 1-100");
  67. /* override default sample period */
  68. static long sample_period;
  69. module_param(sample_period, long, S_IRUGO | S_IWUSR);
  70. MODULE_PARM_DESC(sample_period, "Override carrier sample period, us");
  71. /* override detected model id */
  72. static int model_number = -1;
  73. module_param(model_number, int, S_IRUGO | S_IWUSR);
  74. MODULE_PARM_DESC(model_number, "Use this model number, don't autodetect");
  75. /* HW-independent code functions */
  76. /* check whether carrier frequency is high frequency */
  77. static inline bool ite_is_high_carrier_freq(unsigned int freq)
  78. {
  79. return freq >= ITE_HCF_MIN_CARRIER_FREQ;
  80. }
  81. /* get the bits required to program the carrier frequency in CFQ bits,
  82. * unshifted */
  83. static u8 ite_get_carrier_freq_bits(unsigned int freq)
  84. {
  85. if (ite_is_high_carrier_freq(freq)) {
  86. if (freq < 425000)
  87. return ITE_CFQ_400;
  88. else if (freq < 465000)
  89. return ITE_CFQ_450;
  90. else if (freq < 490000)
  91. return ITE_CFQ_480;
  92. else
  93. return ITE_CFQ_500;
  94. } else {
  95. /* trim to limits */
  96. if (freq < ITE_LCF_MIN_CARRIER_FREQ)
  97. freq = ITE_LCF_MIN_CARRIER_FREQ;
  98. if (freq > ITE_LCF_MAX_CARRIER_FREQ)
  99. freq = ITE_LCF_MAX_CARRIER_FREQ;
  100. /* convert to kHz and subtract the base freq */
  101. freq =
  102. DIV_ROUND_CLOSEST(freq - ITE_LCF_MIN_CARRIER_FREQ,
  103. 1000);
  104. return (u8) freq;
  105. }
  106. }
  107. /* get the bits required to program the pulse with in TXMPW */
  108. static u8 ite_get_pulse_width_bits(unsigned int freq, int duty_cycle)
  109. {
  110. unsigned long period_ns, on_ns;
  111. /* sanitize freq into range */
  112. if (freq < ITE_LCF_MIN_CARRIER_FREQ)
  113. freq = ITE_LCF_MIN_CARRIER_FREQ;
  114. if (freq > ITE_HCF_MAX_CARRIER_FREQ)
  115. freq = ITE_HCF_MAX_CARRIER_FREQ;
  116. period_ns = 1000000000UL / freq;
  117. on_ns = period_ns * duty_cycle / 100;
  118. if (ite_is_high_carrier_freq(freq)) {
  119. if (on_ns < 750)
  120. return ITE_TXMPW_A;
  121. else if (on_ns < 850)
  122. return ITE_TXMPW_B;
  123. else if (on_ns < 950)
  124. return ITE_TXMPW_C;
  125. else if (on_ns < 1080)
  126. return ITE_TXMPW_D;
  127. else
  128. return ITE_TXMPW_E;
  129. } else {
  130. if (on_ns < 6500)
  131. return ITE_TXMPW_A;
  132. else if (on_ns < 7850)
  133. return ITE_TXMPW_B;
  134. else if (on_ns < 9650)
  135. return ITE_TXMPW_C;
  136. else if (on_ns < 11950)
  137. return ITE_TXMPW_D;
  138. else
  139. return ITE_TXMPW_E;
  140. }
  141. }
  142. /* decode raw bytes as received by the hardware, and push them to the ir-core
  143. * layer */
  144. static void ite_decode_bytes(struct ite_dev *dev, const u8 * data, int
  145. length)
  146. {
  147. u32 sample_period;
  148. unsigned long *ldata;
  149. unsigned int next_one, next_zero, size;
  150. DEFINE_IR_RAW_EVENT(ev);
  151. if (length == 0)
  152. return;
  153. sample_period = dev->params.sample_period;
  154. ldata = (unsigned long *)data;
  155. size = length << 3;
  156. next_one = find_next_bit_le(ldata, size, 0);
  157. if (next_one > 0) {
  158. ev.pulse = true;
  159. ev.duration =
  160. ITE_BITS_TO_NS(next_one, sample_period);
  161. ir_raw_event_store_with_filter(dev->rdev, &ev);
  162. }
  163. while (next_one < size) {
  164. next_zero = find_next_zero_bit_le(ldata, size, next_one + 1);
  165. ev.pulse = false;
  166. ev.duration = ITE_BITS_TO_NS(next_zero - next_one, sample_period);
  167. ir_raw_event_store_with_filter(dev->rdev, &ev);
  168. if (next_zero < size) {
  169. next_one =
  170. find_next_bit_le(ldata,
  171. size,
  172. next_zero + 1);
  173. ev.pulse = true;
  174. ev.duration =
  175. ITE_BITS_TO_NS(next_one - next_zero,
  176. sample_period);
  177. ir_raw_event_store_with_filter
  178. (dev->rdev, &ev);
  179. } else
  180. next_one = size;
  181. }
  182. ir_raw_event_handle(dev->rdev);
  183. ite_dbg_verbose("decoded %d bytes.", length);
  184. }
  185. /* set all the rx/tx carrier parameters; this must be called with the device
  186. * spinlock held */
  187. static void ite_set_carrier_params(struct ite_dev *dev)
  188. {
  189. unsigned int freq, low_freq, high_freq;
  190. int allowance;
  191. bool use_demodulator;
  192. bool for_tx = dev->transmitting;
  193. ite_dbg("%s called", __func__);
  194. if (for_tx) {
  195. /* we don't need no stinking calculations */
  196. freq = dev->params.tx_carrier_freq;
  197. allowance = ITE_RXDCR_DEFAULT;
  198. use_demodulator = false;
  199. } else {
  200. low_freq = dev->params.rx_low_carrier_freq;
  201. high_freq = dev->params.rx_high_carrier_freq;
  202. if (low_freq == 0) {
  203. /* don't demodulate */
  204. freq =
  205. ITE_DEFAULT_CARRIER_FREQ;
  206. allowance = ITE_RXDCR_DEFAULT;
  207. use_demodulator = false;
  208. } else {
  209. /* calculate the middle freq */
  210. freq = (low_freq + high_freq) / 2;
  211. /* calculate the allowance */
  212. allowance =
  213. DIV_ROUND_CLOSEST(10000 * (high_freq - low_freq),
  214. ITE_RXDCR_PER_10000_STEP
  215. * (high_freq + low_freq));
  216. if (allowance < 1)
  217. allowance = 1;
  218. if (allowance > ITE_RXDCR_MAX)
  219. allowance = ITE_RXDCR_MAX;
  220. }
  221. }
  222. /* set the carrier parameters in a device-dependent way */
  223. dev->params.set_carrier_params(dev, ite_is_high_carrier_freq(freq),
  224. use_demodulator, ite_get_carrier_freq_bits(freq), allowance,
  225. ite_get_pulse_width_bits(freq, dev->params.tx_duty_cycle));
  226. }
  227. /* interrupt service routine for incoming and outgoing CIR data */
  228. static irqreturn_t ite_cir_isr(int irq, void *data)
  229. {
  230. struct ite_dev *dev = data;
  231. unsigned long flags;
  232. irqreturn_t ret = IRQ_RETVAL(IRQ_NONE);
  233. u8 rx_buf[ITE_RX_FIFO_LEN];
  234. int rx_bytes;
  235. int iflags;
  236. ite_dbg_verbose("%s firing", __func__);
  237. /* grab the spinlock */
  238. spin_lock_irqsave(&dev->lock, flags);
  239. /* read the interrupt flags */
  240. iflags = dev->params.get_irq_causes(dev);
  241. /* check for the receive interrupt */
  242. if (iflags & (ITE_IRQ_RX_FIFO | ITE_IRQ_RX_FIFO_OVERRUN)) {
  243. /* read the FIFO bytes */
  244. rx_bytes =
  245. dev->params.get_rx_bytes(dev, rx_buf,
  246. ITE_RX_FIFO_LEN);
  247. if (rx_bytes > 0) {
  248. /* drop the spinlock, since the ir-core layer
  249. * may call us back again through
  250. * ite_s_idle() */
  251. spin_unlock_irqrestore(&dev->
  252. lock,
  253. flags);
  254. /* decode the data we've just received */
  255. ite_decode_bytes(dev, rx_buf,
  256. rx_bytes);
  257. /* reacquire the spinlock */
  258. spin_lock_irqsave(&dev->lock,
  259. flags);
  260. /* mark the interrupt as serviced */
  261. ret = IRQ_RETVAL(IRQ_HANDLED);
  262. }
  263. } else if (iflags & ITE_IRQ_TX_FIFO) {
  264. /* FIFO space available interrupt */
  265. ite_dbg_verbose("got interrupt for TX FIFO");
  266. /* wake any sleeping transmitter */
  267. wake_up_interruptible(&dev->tx_queue);
  268. /* mark the interrupt as serviced */
  269. ret = IRQ_RETVAL(IRQ_HANDLED);
  270. }
  271. /* drop the spinlock */
  272. spin_unlock_irqrestore(&dev->lock, flags);
  273. ite_dbg_verbose("%s done returning %d", __func__, (int)ret);
  274. return ret;
  275. }
  276. /* set the rx carrier freq range, guess it's in Hz... */
  277. static int ite_set_rx_carrier_range(struct rc_dev *rcdev, u32 carrier_low, u32
  278. carrier_high)
  279. {
  280. unsigned long flags;
  281. struct ite_dev *dev = rcdev->priv;
  282. spin_lock_irqsave(&dev->lock, flags);
  283. dev->params.rx_low_carrier_freq = carrier_low;
  284. dev->params.rx_high_carrier_freq = carrier_high;
  285. ite_set_carrier_params(dev);
  286. spin_unlock_irqrestore(&dev->lock, flags);
  287. return 0;
  288. }
  289. /* set the tx carrier freq, guess it's in Hz... */
  290. static int ite_set_tx_carrier(struct rc_dev *rcdev, u32 carrier)
  291. {
  292. unsigned long flags;
  293. struct ite_dev *dev = rcdev->priv;
  294. spin_lock_irqsave(&dev->lock, flags);
  295. dev->params.tx_carrier_freq = carrier;
  296. ite_set_carrier_params(dev);
  297. spin_unlock_irqrestore(&dev->lock, flags);
  298. return 0;
  299. }
  300. /* set the tx duty cycle by controlling the pulse width */
  301. static int ite_set_tx_duty_cycle(struct rc_dev *rcdev, u32 duty_cycle)
  302. {
  303. unsigned long flags;
  304. struct ite_dev *dev = rcdev->priv;
  305. spin_lock_irqsave(&dev->lock, flags);
  306. dev->params.tx_duty_cycle = duty_cycle;
  307. ite_set_carrier_params(dev);
  308. spin_unlock_irqrestore(&dev->lock, flags);
  309. return 0;
  310. }
  311. /* transmit out IR pulses; what you get here is a batch of alternating
  312. * pulse/space/pulse/space lengths that we should write out completely through
  313. * the FIFO, blocking on a full FIFO */
  314. static int ite_tx_ir(struct rc_dev *rcdev, int *txbuf, u32 n)
  315. {
  316. unsigned long flags;
  317. struct ite_dev *dev = rcdev->priv;
  318. bool is_pulse = false;
  319. int remaining_us, fifo_avail, fifo_remaining, last_idx = 0;
  320. int max_rle_us, next_rle_us;
  321. int ret = n;
  322. u8 last_sent[ITE_TX_FIFO_LEN];
  323. u8 val;
  324. ite_dbg("%s called", __func__);
  325. /* clear the array just in case */
  326. memset(last_sent, 0, ARRAY_SIZE(last_sent));
  327. /* n comes in bytes; convert to ints */
  328. n /= sizeof(int);
  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,
  991. it8709_rr(dev,
  992. IT85_C0IER) & ~(IT85_IEC | IT85_RFOIE |
  993. IT85_RDAIE |
  994. IT85_TLDLIE), IT85_C0IER);
  995. /* disable the receiver */
  996. it8709_disable_rx(dev);
  997. /* erase the FIFO */
  998. it8709_wr(dev, IT85_FIFOCLR | it8709_rr(dev, IT85_C0MSTCR),
  999. IT85_C0MSTCR);
  1000. }
  1001. /* initialize the hardware */
  1002. static void it8709_init_hardware(struct ite_dev *dev)
  1003. {
  1004. ite_dbg("%s called", __func__);
  1005. /* disable all the interrupts */
  1006. it8709_wr(dev,
  1007. it8709_rr(dev,
  1008. IT85_C0IER) & ~(IT85_IEC | IT85_RFOIE |
  1009. IT85_RDAIE |
  1010. IT85_TLDLIE), IT85_C0IER);
  1011. /* program the baud rate divisor */
  1012. it8709_wr(dev, ITE_BAUDRATE_DIVISOR & 0xff, IT85_C0BDLR);
  1013. it8709_wr(dev, (ITE_BAUDRATE_DIVISOR >> 8) & 0xff,
  1014. IT85_C0BDHR);
  1015. /* program the C0MSTCR register defaults */
  1016. it8709_wr(dev, (it8709_rr(dev, IT85_C0MSTCR) & ~(IT85_ILSEL |
  1017. IT85_ILE
  1018. | IT85_FIFOTL
  1019. |
  1020. IT85_FIFOCLR
  1021. |
  1022. IT85_RESET))
  1023. | IT85_FIFOTL_DEFAULT, IT85_C0MSTCR);
  1024. /* program the C0RCR register defaults */
  1025. it8709_wr(dev,
  1026. (it8709_rr(dev, IT85_C0RCR) &
  1027. ~(IT85_RXEN | IT85_RDWOS | IT85_RXEND
  1028. | IT85_RXACT | IT85_RXDCR)) |
  1029. ITE_RXDCR_DEFAULT, IT85_C0RCR);
  1030. /* program the C0TCR register defaults */
  1031. it8709_wr(dev, (it8709_rr(dev, IT85_C0TCR)
  1032. &~(IT85_TXMPM | IT85_TXMPW))
  1033. |IT85_TXRLE | IT85_TXENDF |
  1034. IT85_TXMPM_DEFAULT |
  1035. IT85_TXMPW_DEFAULT, IT85_C0TCR);
  1036. /* program the carrier parameters */
  1037. ite_set_carrier_params(dev);
  1038. }
  1039. /* generic hardware setup/teardown code */
  1040. /* activate the device for use */
  1041. static int ite_open(struct rc_dev *rcdev)
  1042. {
  1043. struct ite_dev *dev = rcdev->priv;
  1044. unsigned long flags;
  1045. ite_dbg("%s called", __func__);
  1046. spin_lock_irqsave(&dev->lock, flags);
  1047. dev->in_use = true;
  1048. /* enable the receiver */
  1049. dev->params.enable_rx(dev);
  1050. spin_unlock_irqrestore(&dev->lock, flags);
  1051. return 0;
  1052. }
  1053. /* deactivate the device for use */
  1054. static void ite_close(struct rc_dev *rcdev)
  1055. {
  1056. struct ite_dev *dev = rcdev->priv;
  1057. unsigned long flags;
  1058. ite_dbg("%s called", __func__);
  1059. spin_lock_irqsave(&dev->lock, flags);
  1060. dev->in_use = false;
  1061. /* wait for any transmission to end */
  1062. spin_unlock_irqrestore(&dev->lock, flags);
  1063. wait_event_interruptible(dev->tx_ended, !dev->transmitting);
  1064. spin_lock_irqsave(&dev->lock, flags);
  1065. dev->params.disable(dev);
  1066. spin_unlock_irqrestore(&dev->lock, flags);
  1067. }
  1068. /* supported models and their parameters */
  1069. static const struct ite_dev_params ite_dev_descs[] = {
  1070. { /* 0: ITE8704 */
  1071. .model = "ITE8704 CIR transceiver",
  1072. .io_region_size = IT87_IOREG_LENGTH,
  1073. .hw_tx_capable = true,
  1074. .sample_period = (u32) (1000000000ULL / 115200),
  1075. .tx_carrier_freq = 38000,
  1076. .tx_duty_cycle = 33,
  1077. .rx_low_carrier_freq = 0,
  1078. .rx_high_carrier_freq = 0,
  1079. /* operations */
  1080. .get_irq_causes = it87_get_irq_causes,
  1081. .enable_rx = it87_enable_rx,
  1082. .idle_rx = it87_idle_rx,
  1083. .disable_rx = it87_idle_rx,
  1084. .get_rx_bytes = it87_get_rx_bytes,
  1085. .enable_tx_interrupt = it87_enable_tx_interrupt,
  1086. .disable_tx_interrupt = it87_disable_tx_interrupt,
  1087. .get_tx_used_slots = it87_get_tx_used_slots,
  1088. .put_tx_byte = it87_put_tx_byte,
  1089. .disable = it87_disable,
  1090. .init_hardware = it87_init_hardware,
  1091. .set_carrier_params = it87_set_carrier_params,
  1092. },
  1093. { /* 1: ITE8713 */
  1094. .model = "ITE8713 CIR transceiver",
  1095. .io_region_size = IT87_IOREG_LENGTH,
  1096. .hw_tx_capable = true,
  1097. .sample_period = (u32) (1000000000ULL / 115200),
  1098. .tx_carrier_freq = 38000,
  1099. .tx_duty_cycle = 33,
  1100. .rx_low_carrier_freq = 0,
  1101. .rx_high_carrier_freq = 0,
  1102. /* operations */
  1103. .get_irq_causes = it87_get_irq_causes,
  1104. .enable_rx = it87_enable_rx,
  1105. .idle_rx = it87_idle_rx,
  1106. .disable_rx = it87_idle_rx,
  1107. .get_rx_bytes = it87_get_rx_bytes,
  1108. .enable_tx_interrupt = it87_enable_tx_interrupt,
  1109. .disable_tx_interrupt = it87_disable_tx_interrupt,
  1110. .get_tx_used_slots = it87_get_tx_used_slots,
  1111. .put_tx_byte = it87_put_tx_byte,
  1112. .disable = it87_disable,
  1113. .init_hardware = it87_init_hardware,
  1114. .set_carrier_params = it87_set_carrier_params,
  1115. },
  1116. { /* 2: ITE8708 */
  1117. .model = "ITE8708 CIR transceiver",
  1118. .io_region_size = IT8708_IOREG_LENGTH,
  1119. .hw_tx_capable = true,
  1120. .sample_period = (u32) (1000000000ULL / 115200),
  1121. .tx_carrier_freq = 38000,
  1122. .tx_duty_cycle = 33,
  1123. .rx_low_carrier_freq = 0,
  1124. .rx_high_carrier_freq = 0,
  1125. /* operations */
  1126. .get_irq_causes = it8708_get_irq_causes,
  1127. .enable_rx = it8708_enable_rx,
  1128. .idle_rx = it8708_idle_rx,
  1129. .disable_rx = it8708_idle_rx,
  1130. .get_rx_bytes = it8708_get_rx_bytes,
  1131. .enable_tx_interrupt = it8708_enable_tx_interrupt,
  1132. .disable_tx_interrupt =
  1133. it8708_disable_tx_interrupt,
  1134. .get_tx_used_slots = it8708_get_tx_used_slots,
  1135. .put_tx_byte = it8708_put_tx_byte,
  1136. .disable = it8708_disable,
  1137. .init_hardware = it8708_init_hardware,
  1138. .set_carrier_params = it8708_set_carrier_params,
  1139. },
  1140. { /* 3: ITE8709 */
  1141. .model = "ITE8709 CIR transceiver",
  1142. .io_region_size = IT8709_IOREG_LENGTH,
  1143. .hw_tx_capable = true,
  1144. .sample_period = (u32) (1000000000ULL / 115200),
  1145. .tx_carrier_freq = 38000,
  1146. .tx_duty_cycle = 33,
  1147. .rx_low_carrier_freq = 0,
  1148. .rx_high_carrier_freq = 0,
  1149. /* operations */
  1150. .get_irq_causes = it8709_get_irq_causes,
  1151. .enable_rx = it8709_enable_rx,
  1152. .idle_rx = it8709_idle_rx,
  1153. .disable_rx = it8709_idle_rx,
  1154. .get_rx_bytes = it8709_get_rx_bytes,
  1155. .enable_tx_interrupt = it8709_enable_tx_interrupt,
  1156. .disable_tx_interrupt =
  1157. it8709_disable_tx_interrupt,
  1158. .get_tx_used_slots = it8709_get_tx_used_slots,
  1159. .put_tx_byte = it8709_put_tx_byte,
  1160. .disable = it8709_disable,
  1161. .init_hardware = it8709_init_hardware,
  1162. .set_carrier_params = it8709_set_carrier_params,
  1163. },
  1164. };
  1165. static const struct pnp_device_id ite_ids[] = {
  1166. {"ITE8704", 0}, /* Default model */
  1167. {"ITE8713", 1}, /* CIR found in EEEBox 1501U */
  1168. {"ITE8708", 2}, /* Bridged IT8512 */
  1169. {"ITE8709", 3}, /* SRAM-Bridged IT8512 */
  1170. {"", 0},
  1171. };
  1172. /* allocate memory, probe hardware, and initialize everything */
  1173. static int ite_probe(struct pnp_dev *pdev, const struct pnp_device_id
  1174. *dev_id)
  1175. {
  1176. const struct ite_dev_params *dev_desc = NULL;
  1177. struct ite_dev *itdev = NULL;
  1178. struct rc_dev *rdev = NULL;
  1179. int ret = -ENOMEM;
  1180. int model_no;
  1181. ite_dbg("%s called", __func__);
  1182. itdev = kzalloc(sizeof(struct ite_dev), GFP_KERNEL);
  1183. if (!itdev)
  1184. return ret;
  1185. /* input device for IR remote (and tx) */
  1186. rdev = rc_allocate_device();
  1187. if (!rdev)
  1188. goto failure;
  1189. ret = -ENODEV;
  1190. /* get the model number */
  1191. model_no = (int)dev_id->driver_data;
  1192. ite_pr(KERN_NOTICE, "Auto-detected model: %s\n",
  1193. ite_dev_descs[model_no].model);
  1194. if (model_number >= 0 && model_number < ARRAY_SIZE(ite_dev_descs)) {
  1195. model_no = model_number;
  1196. ite_pr(KERN_NOTICE, "The model has been fixed by a module "
  1197. "parameter.");
  1198. }
  1199. ite_pr(KERN_NOTICE, "Using model: %s\n", ite_dev_descs[model_no].model);
  1200. /* get the description for the device */
  1201. dev_desc = &ite_dev_descs[model_no];
  1202. /* validate pnp resources */
  1203. if (!pnp_port_valid(pdev, 0) ||
  1204. pnp_port_len(pdev, 0) != dev_desc->io_region_size) {
  1205. dev_err(&pdev->dev, "IR PNP Port not valid!\n");
  1206. goto failure;
  1207. }
  1208. if (!pnp_irq_valid(pdev, 0)) {
  1209. dev_err(&pdev->dev, "PNP IRQ not valid!\n");
  1210. goto failure;
  1211. }
  1212. /* store resource values */
  1213. itdev->cir_addr = pnp_port_start(pdev, 0);
  1214. itdev->cir_irq = pnp_irq(pdev, 0);
  1215. /* initialize spinlocks */
  1216. spin_lock_init(&itdev->lock);
  1217. /* initialize raw event */
  1218. init_ir_raw_event(&itdev->rawir);
  1219. ret = -EBUSY;
  1220. /* now claim resources */
  1221. if (!request_region(itdev->cir_addr,
  1222. dev_desc->io_region_size, ITE_DRIVER_NAME))
  1223. goto failure;
  1224. if (request_irq(itdev->cir_irq, ite_cir_isr, IRQF_SHARED,
  1225. ITE_DRIVER_NAME, (void *)itdev))
  1226. goto failure;
  1227. /* set driver data into the pnp device */
  1228. pnp_set_drvdata(pdev, itdev);
  1229. itdev->pdev = pdev;
  1230. /* initialize waitqueues for transmission */
  1231. init_waitqueue_head(&itdev->tx_queue);
  1232. init_waitqueue_head(&itdev->tx_ended);
  1233. /* copy model-specific parameters */
  1234. itdev->params = *dev_desc;
  1235. /* apply any overrides */
  1236. if (sample_period > 0)
  1237. itdev->params.sample_period = sample_period;
  1238. if (tx_carrier_freq > 0)
  1239. itdev->params.tx_carrier_freq = tx_carrier_freq;
  1240. if (tx_duty_cycle > 0 && tx_duty_cycle <= 100)
  1241. itdev->params.tx_duty_cycle = tx_duty_cycle;
  1242. if (rx_low_carrier_freq > 0)
  1243. itdev->params.rx_low_carrier_freq = rx_low_carrier_freq;
  1244. if (rx_high_carrier_freq > 0)
  1245. itdev->params.rx_high_carrier_freq = rx_high_carrier_freq;
  1246. /* print out parameters */
  1247. ite_pr(KERN_NOTICE, "TX-capable: %d\n", (int)
  1248. itdev->params.hw_tx_capable);
  1249. ite_pr(KERN_NOTICE, "Sample period (ns): %ld\n", (long)
  1250. itdev->params.sample_period);
  1251. ite_pr(KERN_NOTICE, "TX carrier frequency (Hz): %d\n", (int)
  1252. itdev->params.tx_carrier_freq);
  1253. ite_pr(KERN_NOTICE, "TX duty cycle (%%): %d\n", (int)
  1254. itdev->params.tx_duty_cycle);
  1255. ite_pr(KERN_NOTICE, "RX low carrier frequency (Hz): %d\n", (int)
  1256. itdev->params.rx_low_carrier_freq);
  1257. ite_pr(KERN_NOTICE, "RX high carrier frequency (Hz): %d\n", (int)
  1258. itdev->params.rx_high_carrier_freq);
  1259. /* set up hardware initial state */
  1260. itdev->params.init_hardware(itdev);
  1261. /* set up ir-core props */
  1262. rdev->priv = itdev;
  1263. rdev->driver_type = RC_DRIVER_IR_RAW;
  1264. rdev->allowed_protos = RC_TYPE_ALL;
  1265. rdev->open = ite_open;
  1266. rdev->close = ite_close;
  1267. rdev->s_idle = ite_s_idle;
  1268. rdev->s_rx_carrier_range = ite_set_rx_carrier_range;
  1269. rdev->min_timeout = ITE_MIN_IDLE_TIMEOUT;
  1270. rdev->max_timeout = ITE_MAX_IDLE_TIMEOUT;
  1271. rdev->timeout = ITE_IDLE_TIMEOUT;
  1272. rdev->rx_resolution = ITE_BAUDRATE_DIVISOR *
  1273. itdev->params.sample_period;
  1274. rdev->tx_resolution = ITE_BAUDRATE_DIVISOR *
  1275. itdev->params.sample_period;
  1276. /* set up transmitter related values if needed */
  1277. if (itdev->params.hw_tx_capable) {
  1278. rdev->tx_ir = ite_tx_ir;
  1279. rdev->s_tx_carrier = ite_set_tx_carrier;
  1280. rdev->s_tx_duty_cycle = ite_set_tx_duty_cycle;
  1281. }
  1282. rdev->input_name = dev_desc->model;
  1283. rdev->input_id.bustype = BUS_HOST;
  1284. rdev->input_id.vendor = PCI_VENDOR_ID_ITE;
  1285. rdev->input_id.product = 0;
  1286. rdev->input_id.version = 0;
  1287. rdev->driver_name = ITE_DRIVER_NAME;
  1288. rdev->map_name = RC_MAP_RC6_MCE;
  1289. ret = rc_register_device(rdev);
  1290. if (ret)
  1291. goto failure;
  1292. itdev->rdev = rdev;
  1293. ite_pr(KERN_NOTICE, "driver has been successfully loaded\n");
  1294. return 0;
  1295. failure:
  1296. if (itdev->cir_irq)
  1297. free_irq(itdev->cir_irq, itdev);
  1298. if (itdev->cir_addr)
  1299. release_region(itdev->cir_addr, itdev->params.io_region_size);
  1300. rc_free_device(rdev);
  1301. kfree(itdev);
  1302. return ret;
  1303. }
  1304. static void __devexit ite_remove(struct pnp_dev *pdev)
  1305. {
  1306. struct ite_dev *dev = pnp_get_drvdata(pdev);
  1307. unsigned long flags;
  1308. ite_dbg("%s called", __func__);
  1309. spin_lock_irqsave(&dev->lock, flags);
  1310. /* disable hardware */
  1311. dev->params.disable(dev);
  1312. spin_unlock_irqrestore(&dev->lock, flags);
  1313. /* free resources */
  1314. free_irq(dev->cir_irq, dev);
  1315. release_region(dev->cir_addr, dev->params.io_region_size);
  1316. rc_unregister_device(dev->rdev);
  1317. kfree(dev);
  1318. }
  1319. static int ite_suspend(struct pnp_dev *pdev, pm_message_t state)
  1320. {
  1321. struct ite_dev *dev = pnp_get_drvdata(pdev);
  1322. unsigned long flags;
  1323. ite_dbg("%s called", __func__);
  1324. spin_lock_irqsave(&dev->lock, flags);
  1325. /* disable all interrupts */
  1326. dev->params.disable(dev);
  1327. spin_unlock_irqrestore(&dev->lock, flags);
  1328. return 0;
  1329. }
  1330. static int ite_resume(struct pnp_dev *pdev)
  1331. {
  1332. int ret = 0;
  1333. struct ite_dev *dev = pnp_get_drvdata(pdev);
  1334. unsigned long flags;
  1335. ite_dbg("%s called", __func__);
  1336. spin_lock_irqsave(&dev->lock, flags);
  1337. if (dev->transmitting) {
  1338. /* wake up the transmitter */
  1339. wake_up_interruptible(&dev->tx_queue);
  1340. } else {
  1341. /* enable the receiver */
  1342. dev->params.enable_rx(dev);
  1343. }
  1344. spin_unlock_irqrestore(&dev->lock, flags);
  1345. return ret;
  1346. }
  1347. static void ite_shutdown(struct pnp_dev *pdev)
  1348. {
  1349. struct ite_dev *dev = pnp_get_drvdata(pdev);
  1350. unsigned long flags;
  1351. ite_dbg("%s called", __func__);
  1352. spin_lock_irqsave(&dev->lock, flags);
  1353. /* disable all interrupts */
  1354. dev->params.disable(dev);
  1355. spin_unlock_irqrestore(&dev->lock, flags);
  1356. }
  1357. static struct pnp_driver ite_driver = {
  1358. .name = ITE_DRIVER_NAME,
  1359. .id_table = ite_ids,
  1360. .probe = ite_probe,
  1361. .remove = __devexit_p(ite_remove),
  1362. .suspend = ite_suspend,
  1363. .resume = ite_resume,
  1364. .shutdown = ite_shutdown,
  1365. };
  1366. int ite_init(void)
  1367. {
  1368. return pnp_register_driver(&ite_driver);
  1369. }
  1370. void ite_exit(void)
  1371. {
  1372. pnp_unregister_driver(&ite_driver);
  1373. }
  1374. MODULE_DEVICE_TABLE(pnp, ite_ids);
  1375. MODULE_DESCRIPTION("ITE Tech Inc. IT8712F/ITE8512F CIR driver");
  1376. MODULE_AUTHOR("Juan J. Garcia de Soria <skandalfo@gmail.com>");
  1377. MODULE_LICENSE("GPL");
  1378. module_init(ite_init);
  1379. module_exit(ite_exit);