io.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
  3. * Creative Labs, Inc.
  4. * Routines for control of EMU10K1 chips
  5. *
  6. * BUGS:
  7. * --
  8. *
  9. * TODO:
  10. * --
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. */
  27. #include <sound/driver.h>
  28. #include <linux/time.h>
  29. #include <sound/core.h>
  30. #include <sound/emu10k1.h>
  31. unsigned int snd_emu10k1_ptr_read(emu10k1_t * emu, unsigned int reg, unsigned int chn)
  32. {
  33. unsigned long flags;
  34. unsigned int regptr, val;
  35. unsigned int mask;
  36. mask = emu->audigy ? A_PTR_ADDRESS_MASK : PTR_ADDRESS_MASK;
  37. regptr = ((reg << 16) & mask) | (chn & PTR_CHANNELNUM_MASK);
  38. if (reg & 0xff000000) {
  39. unsigned char size, offset;
  40. size = (reg >> 24) & 0x3f;
  41. offset = (reg >> 16) & 0x1f;
  42. mask = ((1 << size) - 1) << offset;
  43. spin_lock_irqsave(&emu->emu_lock, flags);
  44. outl(regptr, emu->port + PTR);
  45. val = inl(emu->port + DATA);
  46. spin_unlock_irqrestore(&emu->emu_lock, flags);
  47. return (val & mask) >> offset;
  48. } else {
  49. spin_lock_irqsave(&emu->emu_lock, flags);
  50. outl(regptr, emu->port + PTR);
  51. val = inl(emu->port + DATA);
  52. spin_unlock_irqrestore(&emu->emu_lock, flags);
  53. return val;
  54. }
  55. }
  56. void snd_emu10k1_ptr_write(emu10k1_t *emu, unsigned int reg, unsigned int chn, unsigned int data)
  57. {
  58. unsigned int regptr;
  59. unsigned long flags;
  60. unsigned int mask;
  61. mask = emu->audigy ? A_PTR_ADDRESS_MASK : PTR_ADDRESS_MASK;
  62. regptr = ((reg << 16) & mask) | (chn & PTR_CHANNELNUM_MASK);
  63. if (reg & 0xff000000) {
  64. unsigned char size, offset;
  65. size = (reg >> 24) & 0x3f;
  66. offset = (reg >> 16) & 0x1f;
  67. mask = ((1 << size) - 1) << offset;
  68. data = (data << offset) & mask;
  69. spin_lock_irqsave(&emu->emu_lock, flags);
  70. outl(regptr, emu->port + PTR);
  71. data |= inl(emu->port + DATA) & ~mask;
  72. outl(data, emu->port + DATA);
  73. spin_unlock_irqrestore(&emu->emu_lock, flags);
  74. } else {
  75. spin_lock_irqsave(&emu->emu_lock, flags);
  76. outl(regptr, emu->port + PTR);
  77. outl(data, emu->port + DATA);
  78. spin_unlock_irqrestore(&emu->emu_lock, flags);
  79. }
  80. }
  81. unsigned int snd_emu10k1_ptr20_read(emu10k1_t * emu,
  82. unsigned int reg,
  83. unsigned int chn)
  84. {
  85. unsigned long flags;
  86. unsigned int regptr, val;
  87. regptr = (reg << 16) | chn;
  88. spin_lock_irqsave(&emu->emu_lock, flags);
  89. outl(regptr, emu->port + 0x20 + PTR);
  90. val = inl(emu->port + 0x20 + DATA);
  91. spin_unlock_irqrestore(&emu->emu_lock, flags);
  92. return val;
  93. }
  94. void snd_emu10k1_ptr20_write(emu10k1_t *emu,
  95. unsigned int reg,
  96. unsigned int chn,
  97. unsigned int data)
  98. {
  99. unsigned int regptr;
  100. unsigned long flags;
  101. regptr = (reg << 16) | chn;
  102. spin_lock_irqsave(&emu->emu_lock, flags);
  103. outl(regptr, emu->port + 0x20 + PTR);
  104. outl(data, emu->port + 0x20 + DATA);
  105. spin_unlock_irqrestore(&emu->emu_lock, flags);
  106. }
  107. void snd_emu10k1_intr_enable(emu10k1_t *emu, unsigned int intrenb)
  108. {
  109. unsigned long flags;
  110. unsigned int enable;
  111. spin_lock_irqsave(&emu->emu_lock, flags);
  112. enable = inl(emu->port + INTE) | intrenb;
  113. outl(enable, emu->port + INTE);
  114. spin_unlock_irqrestore(&emu->emu_lock, flags);
  115. }
  116. void snd_emu10k1_intr_disable(emu10k1_t *emu, unsigned int intrenb)
  117. {
  118. unsigned long flags;
  119. unsigned int enable;
  120. spin_lock_irqsave(&emu->emu_lock, flags);
  121. enable = inl(emu->port + INTE) & ~intrenb;
  122. outl(enable, emu->port + INTE);
  123. spin_unlock_irqrestore(&emu->emu_lock, flags);
  124. }
  125. void snd_emu10k1_voice_intr_enable(emu10k1_t *emu, unsigned int voicenum)
  126. {
  127. unsigned long flags;
  128. unsigned int val;
  129. spin_lock_irqsave(&emu->emu_lock, flags);
  130. /* voice interrupt */
  131. if (voicenum >= 32) {
  132. outl(CLIEH << 16, emu->port + PTR);
  133. val = inl(emu->port + DATA);
  134. val |= 1 << (voicenum - 32);
  135. } else {
  136. outl(CLIEL << 16, emu->port + PTR);
  137. val = inl(emu->port + DATA);
  138. val |= 1 << voicenum;
  139. }
  140. outl(val, emu->port + DATA);
  141. spin_unlock_irqrestore(&emu->emu_lock, flags);
  142. }
  143. void snd_emu10k1_voice_intr_disable(emu10k1_t *emu, unsigned int voicenum)
  144. {
  145. unsigned long flags;
  146. unsigned int val;
  147. spin_lock_irqsave(&emu->emu_lock, flags);
  148. /* voice interrupt */
  149. if (voicenum >= 32) {
  150. outl(CLIEH << 16, emu->port + PTR);
  151. val = inl(emu->port + DATA);
  152. val &= ~(1 << (voicenum - 32));
  153. } else {
  154. outl(CLIEL << 16, emu->port + PTR);
  155. val = inl(emu->port + DATA);
  156. val &= ~(1 << voicenum);
  157. }
  158. outl(val, emu->port + DATA);
  159. spin_unlock_irqrestore(&emu->emu_lock, flags);
  160. }
  161. void snd_emu10k1_voice_intr_ack(emu10k1_t *emu, unsigned int voicenum)
  162. {
  163. unsigned long flags;
  164. spin_lock_irqsave(&emu->emu_lock, flags);
  165. /* voice interrupt */
  166. if (voicenum >= 32) {
  167. outl(CLIPH << 16, emu->port + PTR);
  168. voicenum = 1 << (voicenum - 32);
  169. } else {
  170. outl(CLIPL << 16, emu->port + PTR);
  171. voicenum = 1 << voicenum;
  172. }
  173. outl(voicenum, emu->port + DATA);
  174. spin_unlock_irqrestore(&emu->emu_lock, flags);
  175. }
  176. void snd_emu10k1_voice_half_loop_intr_enable(emu10k1_t *emu, unsigned int voicenum)
  177. {
  178. unsigned long flags;
  179. unsigned int val;
  180. spin_lock_irqsave(&emu->emu_lock, flags);
  181. /* voice interrupt */
  182. if (voicenum >= 32) {
  183. outl(HLIEH << 16, emu->port + PTR);
  184. val = inl(emu->port + DATA);
  185. val |= 1 << (voicenum - 32);
  186. } else {
  187. outl(HLIEL << 16, emu->port + PTR);
  188. val = inl(emu->port + DATA);
  189. val |= 1 << voicenum;
  190. }
  191. outl(val, emu->port + DATA);
  192. spin_unlock_irqrestore(&emu->emu_lock, flags);
  193. }
  194. void snd_emu10k1_voice_half_loop_intr_disable(emu10k1_t *emu, unsigned int voicenum)
  195. {
  196. unsigned long flags;
  197. unsigned int val;
  198. spin_lock_irqsave(&emu->emu_lock, flags);
  199. /* voice interrupt */
  200. if (voicenum >= 32) {
  201. outl(HLIEH << 16, emu->port + PTR);
  202. val = inl(emu->port + DATA);
  203. val &= ~(1 << (voicenum - 32));
  204. } else {
  205. outl(HLIEL << 16, emu->port + PTR);
  206. val = inl(emu->port + DATA);
  207. val &= ~(1 << voicenum);
  208. }
  209. outl(val, emu->port + DATA);
  210. spin_unlock_irqrestore(&emu->emu_lock, flags);
  211. }
  212. void snd_emu10k1_voice_half_loop_intr_ack(emu10k1_t *emu, unsigned int voicenum)
  213. {
  214. unsigned long flags;
  215. spin_lock_irqsave(&emu->emu_lock, flags);
  216. /* voice interrupt */
  217. if (voicenum >= 32) {
  218. outl(HLIPH << 16, emu->port + PTR);
  219. voicenum = 1 << (voicenum - 32);
  220. } else {
  221. outl(HLIPL << 16, emu->port + PTR);
  222. voicenum = 1 << voicenum;
  223. }
  224. outl(voicenum, emu->port + DATA);
  225. spin_unlock_irqrestore(&emu->emu_lock, flags);
  226. }
  227. void snd_emu10k1_voice_set_loop_stop(emu10k1_t *emu, unsigned int voicenum)
  228. {
  229. unsigned long flags;
  230. unsigned int sol;
  231. spin_lock_irqsave(&emu->emu_lock, flags);
  232. /* voice interrupt */
  233. if (voicenum >= 32) {
  234. outl(SOLEH << 16, emu->port + PTR);
  235. sol = inl(emu->port + DATA);
  236. sol |= 1 << (voicenum - 32);
  237. } else {
  238. outl(SOLEL << 16, emu->port + PTR);
  239. sol = inl(emu->port + DATA);
  240. sol |= 1 << voicenum;
  241. }
  242. outl(sol, emu->port + DATA);
  243. spin_unlock_irqrestore(&emu->emu_lock, flags);
  244. }
  245. void snd_emu10k1_voice_clear_loop_stop(emu10k1_t *emu, unsigned int voicenum)
  246. {
  247. unsigned long flags;
  248. unsigned int sol;
  249. spin_lock_irqsave(&emu->emu_lock, flags);
  250. /* voice interrupt */
  251. if (voicenum >= 32) {
  252. outl(SOLEH << 16, emu->port + PTR);
  253. sol = inl(emu->port + DATA);
  254. sol &= ~(1 << (voicenum - 32));
  255. } else {
  256. outl(SOLEL << 16, emu->port + PTR);
  257. sol = inl(emu->port + DATA);
  258. sol &= ~(1 << voicenum);
  259. }
  260. outl(sol, emu->port + DATA);
  261. spin_unlock_irqrestore(&emu->emu_lock, flags);
  262. }
  263. void snd_emu10k1_wait(emu10k1_t *emu, unsigned int wait)
  264. {
  265. volatile unsigned count;
  266. unsigned int newtime = 0, curtime;
  267. curtime = inl(emu->port + WC) >> 6;
  268. while (wait-- > 0) {
  269. count = 0;
  270. while (count++ < 16384) {
  271. newtime = inl(emu->port + WC) >> 6;
  272. if (newtime != curtime)
  273. break;
  274. }
  275. if (count >= 16384)
  276. break;
  277. curtime = newtime;
  278. }
  279. }
  280. unsigned short snd_emu10k1_ac97_read(ac97_t *ac97, unsigned short reg)
  281. {
  282. emu10k1_t *emu = ac97->private_data;
  283. unsigned long flags;
  284. unsigned short val;
  285. spin_lock_irqsave(&emu->emu_lock, flags);
  286. outb(reg, emu->port + AC97ADDRESS);
  287. val = inw(emu->port + AC97DATA);
  288. spin_unlock_irqrestore(&emu->emu_lock, flags);
  289. return val;
  290. }
  291. void snd_emu10k1_ac97_write(ac97_t *ac97, unsigned short reg, unsigned short data)
  292. {
  293. emu10k1_t *emu = ac97->private_data;
  294. unsigned long flags;
  295. spin_lock_irqsave(&emu->emu_lock, flags);
  296. outb(reg, emu->port + AC97ADDRESS);
  297. outw(data, emu->port + AC97DATA);
  298. spin_unlock_irqrestore(&emu->emu_lock, flags);
  299. }
  300. /*
  301. * convert rate to pitch
  302. */
  303. unsigned int snd_emu10k1_rate_to_pitch(unsigned int rate)
  304. {
  305. static u32 logMagTable[128] = {
  306. 0x00000, 0x02dfc, 0x05b9e, 0x088e6, 0x0b5d6, 0x0e26f, 0x10eb3, 0x13aa2,
  307. 0x1663f, 0x1918a, 0x1bc84, 0x1e72e, 0x2118b, 0x23b9a, 0x2655d, 0x28ed5,
  308. 0x2b803, 0x2e0e8, 0x30985, 0x331db, 0x359eb, 0x381b6, 0x3a93d, 0x3d081,
  309. 0x3f782, 0x41e42, 0x444c1, 0x46b01, 0x49101, 0x4b6c4, 0x4dc49, 0x50191,
  310. 0x5269e, 0x54b6f, 0x57006, 0x59463, 0x5b888, 0x5dc74, 0x60029, 0x623a7,
  311. 0x646ee, 0x66a00, 0x68cdd, 0x6af86, 0x6d1fa, 0x6f43c, 0x7164b, 0x73829,
  312. 0x759d4, 0x77b4f, 0x79c9a, 0x7bdb5, 0x7dea1, 0x7ff5e, 0x81fed, 0x8404e,
  313. 0x86082, 0x88089, 0x8a064, 0x8c014, 0x8df98, 0x8fef1, 0x91e20, 0x93d26,
  314. 0x95c01, 0x97ab4, 0x9993e, 0x9b79f, 0x9d5d9, 0x9f3ec, 0xa11d8, 0xa2f9d,
  315. 0xa4d3c, 0xa6ab5, 0xa8808, 0xaa537, 0xac241, 0xadf26, 0xafbe7, 0xb1885,
  316. 0xb3500, 0xb5157, 0xb6d8c, 0xb899f, 0xba58f, 0xbc15e, 0xbdd0c, 0xbf899,
  317. 0xc1404, 0xc2f50, 0xc4a7b, 0xc6587, 0xc8073, 0xc9b3f, 0xcb5ed, 0xcd07c,
  318. 0xceaec, 0xd053f, 0xd1f73, 0xd398a, 0xd5384, 0xd6d60, 0xd8720, 0xda0c3,
  319. 0xdba4a, 0xdd3b4, 0xded03, 0xe0636, 0xe1f4e, 0xe384a, 0xe512c, 0xe69f3,
  320. 0xe829f, 0xe9b31, 0xeb3a9, 0xecc08, 0xee44c, 0xefc78, 0xf148a, 0xf2c83,
  321. 0xf4463, 0xf5c2a, 0xf73da, 0xf8b71, 0xfa2f0, 0xfba57, 0xfd1a7, 0xfe8df
  322. };
  323. static char logSlopeTable[128] = {
  324. 0x5c, 0x5c, 0x5b, 0x5a, 0x5a, 0x59, 0x58, 0x58,
  325. 0x57, 0x56, 0x56, 0x55, 0x55, 0x54, 0x53, 0x53,
  326. 0x52, 0x52, 0x51, 0x51, 0x50, 0x50, 0x4f, 0x4f,
  327. 0x4e, 0x4d, 0x4d, 0x4d, 0x4c, 0x4c, 0x4b, 0x4b,
  328. 0x4a, 0x4a, 0x49, 0x49, 0x48, 0x48, 0x47, 0x47,
  329. 0x47, 0x46, 0x46, 0x45, 0x45, 0x45, 0x44, 0x44,
  330. 0x43, 0x43, 0x43, 0x42, 0x42, 0x42, 0x41, 0x41,
  331. 0x41, 0x40, 0x40, 0x40, 0x3f, 0x3f, 0x3f, 0x3e,
  332. 0x3e, 0x3e, 0x3d, 0x3d, 0x3d, 0x3c, 0x3c, 0x3c,
  333. 0x3b, 0x3b, 0x3b, 0x3b, 0x3a, 0x3a, 0x3a, 0x39,
  334. 0x39, 0x39, 0x39, 0x38, 0x38, 0x38, 0x38, 0x37,
  335. 0x37, 0x37, 0x37, 0x36, 0x36, 0x36, 0x36, 0x35,
  336. 0x35, 0x35, 0x35, 0x34, 0x34, 0x34, 0x34, 0x34,
  337. 0x33, 0x33, 0x33, 0x33, 0x32, 0x32, 0x32, 0x32,
  338. 0x32, 0x31, 0x31, 0x31, 0x31, 0x31, 0x30, 0x30,
  339. 0x30, 0x30, 0x30, 0x2f, 0x2f, 0x2f, 0x2f, 0x2f
  340. };
  341. int i;
  342. if (rate == 0)
  343. return 0; /* Bail out if no leading "1" */
  344. rate *= 11185; /* Scale 48000 to 0x20002380 */
  345. for (i = 31; i > 0; i--) {
  346. if (rate & 0x80000000) { /* Detect leading "1" */
  347. return (((unsigned int) (i - 15) << 20) +
  348. logMagTable[0x7f & (rate >> 24)] +
  349. (0x7f & (rate >> 17)) *
  350. logSlopeTable[0x7f & (rate >> 24)]);
  351. }
  352. rate <<= 1;
  353. }
  354. return 0; /* Should never reach this point */
  355. }