ad1848_lib.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  3. * Routines for control of AD1848/AD1847/CS4248
  4. *
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU 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 USA
  19. *
  20. */
  21. #define SNDRV_MAIN_OBJECT_FILE
  22. #include <linux/delay.h>
  23. #include <linux/init.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/slab.h>
  26. #include <linux/ioport.h>
  27. #include <sound/core.h>
  28. #include <sound/ad1848.h>
  29. #include <sound/control.h>
  30. #include <sound/tlv.h>
  31. #include <sound/pcm_params.h>
  32. #include <asm/io.h>
  33. #include <asm/dma.h>
  34. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  35. MODULE_DESCRIPTION("Routines for control of AD1848/AD1847/CS4248");
  36. MODULE_LICENSE("GPL");
  37. #if 0
  38. #define SNDRV_DEBUG_MCE
  39. #endif
  40. /*
  41. * Some variables
  42. */
  43. static unsigned char freq_bits[14] = {
  44. /* 5510 */ 0x00 | AD1848_XTAL2,
  45. /* 6620 */ 0x0E | AD1848_XTAL2,
  46. /* 8000 */ 0x00 | AD1848_XTAL1,
  47. /* 9600 */ 0x0E | AD1848_XTAL1,
  48. /* 11025 */ 0x02 | AD1848_XTAL2,
  49. /* 16000 */ 0x02 | AD1848_XTAL1,
  50. /* 18900 */ 0x04 | AD1848_XTAL2,
  51. /* 22050 */ 0x06 | AD1848_XTAL2,
  52. /* 27042 */ 0x04 | AD1848_XTAL1,
  53. /* 32000 */ 0x06 | AD1848_XTAL1,
  54. /* 33075 */ 0x0C | AD1848_XTAL2,
  55. /* 37800 */ 0x08 | AD1848_XTAL2,
  56. /* 44100 */ 0x0A | AD1848_XTAL2,
  57. /* 48000 */ 0x0C | AD1848_XTAL1
  58. };
  59. static unsigned int rates[14] = {
  60. 5510, 6620, 8000, 9600, 11025, 16000, 18900, 22050,
  61. 27042, 32000, 33075, 37800, 44100, 48000
  62. };
  63. static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
  64. .count = ARRAY_SIZE(rates),
  65. .list = rates,
  66. .mask = 0,
  67. };
  68. static unsigned char snd_ad1848_original_image[16] =
  69. {
  70. 0x00, /* 00 - lic */
  71. 0x00, /* 01 - ric */
  72. 0x9f, /* 02 - la1ic */
  73. 0x9f, /* 03 - ra1ic */
  74. 0x9f, /* 04 - la2ic */
  75. 0x9f, /* 05 - ra2ic */
  76. 0xbf, /* 06 - loc */
  77. 0xbf, /* 07 - roc */
  78. 0x20, /* 08 - dfr */
  79. AD1848_AUTOCALIB, /* 09 - ic */
  80. 0x00, /* 0a - pc */
  81. 0x00, /* 0b - ti */
  82. 0x00, /* 0c - mi */
  83. 0x00, /* 0d - lbc */
  84. 0x00, /* 0e - dru */
  85. 0x00, /* 0f - drl */
  86. };
  87. /*
  88. * Basic I/O functions
  89. */
  90. static void snd_ad1848_wait(struct snd_wss *chip)
  91. {
  92. int timeout;
  93. for (timeout = 250; timeout > 0; timeout--) {
  94. if ((inb(chip->port + CS4231P(REGSEL)) & AD1848_INIT) == 0)
  95. break;
  96. udelay(100);
  97. }
  98. }
  99. void snd_ad1848_out(struct snd_wss *chip,
  100. unsigned char reg,
  101. unsigned char value)
  102. {
  103. snd_ad1848_wait(chip);
  104. #ifdef CONFIG_SND_DEBUG
  105. if (inb(chip->port + CS4231P(REGSEL)) & AD1848_INIT)
  106. snd_printk(KERN_WARNING "auto calibration time out - "
  107. "reg = 0x%x, value = 0x%x\n", reg, value);
  108. #endif
  109. outb(chip->mce_bit | reg, chip->port + CS4231P(REGSEL));
  110. outb(chip->image[reg] = value, chip->port + CS4231P(REG));
  111. mb();
  112. snd_printdd("codec out - reg 0x%x = 0x%x\n",
  113. chip->mce_bit | reg, value);
  114. }
  115. EXPORT_SYMBOL(snd_ad1848_out);
  116. static void snd_ad1848_dout(struct snd_wss *chip,
  117. unsigned char reg, unsigned char value)
  118. {
  119. snd_ad1848_wait(chip);
  120. outb(chip->mce_bit | reg, chip->port + CS4231P(REGSEL));
  121. outb(value, chip->port + CS4231P(REG));
  122. mb();
  123. }
  124. static unsigned char snd_ad1848_in(struct snd_wss *chip, unsigned char reg)
  125. {
  126. snd_ad1848_wait(chip);
  127. #ifdef CONFIG_SND_DEBUG
  128. if (inb(chip->port + CS4231P(REGSEL)) & AD1848_INIT)
  129. snd_printk(KERN_WARNING "auto calibration time out - "
  130. "reg = 0x%x\n", reg);
  131. #endif
  132. outb(chip->mce_bit | reg, chip->port + CS4231P(REGSEL));
  133. mb();
  134. return inb(chip->port + CS4231P(REG));
  135. }
  136. #if 0
  137. static void snd_ad1848_debug(struct snd_wss *chip)
  138. {
  139. printk(KERN_DEBUG "AD1848 REGS: INDEX = 0x%02x ", inb(chip->port + CS4231P(REGSEL)));
  140. printk(KERN_DEBUG " STATUS = 0x%02x\n", inb(chip->port + CS4231P(STATUS)));
  141. printk(KERN_DEBUG " 0x00: left input = 0x%02x ", snd_ad1848_in(chip, 0x00));
  142. printk(KERN_DEBUG " 0x08: playback format = 0x%02x\n", snd_ad1848_in(chip, 0x08));
  143. printk(KERN_DEBUG " 0x01: right input = 0x%02x ", snd_ad1848_in(chip, 0x01));
  144. printk(KERN_DEBUG " 0x09: iface (CFIG 1) = 0x%02x\n", snd_ad1848_in(chip, 0x09));
  145. printk(KERN_DEBUG " 0x02: AUXA left = 0x%02x ", snd_ad1848_in(chip, 0x02));
  146. printk(KERN_DEBUG " 0x0a: pin control = 0x%02x\n", snd_ad1848_in(chip, 0x0a));
  147. printk(KERN_DEBUG " 0x03: AUXA right = 0x%02x ", snd_ad1848_in(chip, 0x03));
  148. printk(KERN_DEBUG " 0x0b: init & status = 0x%02x\n", snd_ad1848_in(chip, 0x0b));
  149. printk(KERN_DEBUG " 0x04: AUXB left = 0x%02x ", snd_ad1848_in(chip, 0x04));
  150. printk(KERN_DEBUG " 0x0c: revision & mode = 0x%02x\n", snd_ad1848_in(chip, 0x0c));
  151. printk(KERN_DEBUG " 0x05: AUXB right = 0x%02x ", snd_ad1848_in(chip, 0x05));
  152. printk(KERN_DEBUG " 0x0d: loopback = 0x%02x\n", snd_ad1848_in(chip, 0x0d));
  153. printk(KERN_DEBUG " 0x06: left output = 0x%02x ", snd_ad1848_in(chip, 0x06));
  154. printk(KERN_DEBUG " 0x0e: data upr count = 0x%02x\n", snd_ad1848_in(chip, 0x0e));
  155. printk(KERN_DEBUG " 0x07: right output = 0x%02x ", snd_ad1848_in(chip, 0x07));
  156. printk(KERN_DEBUG " 0x0f: data lwr count = 0x%02x\n", snd_ad1848_in(chip, 0x0f));
  157. }
  158. #endif
  159. /*
  160. * AD1848 detection / MCE routines
  161. */
  162. static void snd_ad1848_mce_up(struct snd_wss *chip)
  163. {
  164. unsigned long flags;
  165. int timeout;
  166. snd_ad1848_wait(chip);
  167. #ifdef CONFIG_SND_DEBUG
  168. if (inb(chip->port + CS4231P(REGSEL)) & AD1848_INIT)
  169. snd_printk(KERN_WARNING "mce_up - auto calibration time out (0)\n");
  170. #endif
  171. spin_lock_irqsave(&chip->reg_lock, flags);
  172. chip->mce_bit |= AD1848_MCE;
  173. timeout = inb(chip->port + CS4231P(REGSEL));
  174. if (timeout == 0x80)
  175. snd_printk(KERN_WARNING "mce_up [0x%lx]: serious init problem - codec still busy\n", chip->port);
  176. if (!(timeout & AD1848_MCE))
  177. outb(chip->mce_bit | (timeout & 0x1f),
  178. chip->port + CS4231P(REGSEL));
  179. spin_unlock_irqrestore(&chip->reg_lock, flags);
  180. }
  181. static void snd_ad1848_mce_down(struct snd_wss *chip)
  182. {
  183. unsigned long flags, timeout;
  184. int reg;
  185. spin_lock_irqsave(&chip->reg_lock, flags);
  186. for (timeout = 5; timeout > 0; timeout--)
  187. inb(chip->port + CS4231P(REGSEL));
  188. /* end of cleanup sequence */
  189. for (timeout = 12000;
  190. timeout > 0 && (inb(chip->port + CS4231P(REGSEL)) & AD1848_INIT);
  191. timeout--)
  192. udelay(100);
  193. snd_printdd("(1) timeout = %ld\n", timeout);
  194. #ifdef CONFIG_SND_DEBUG
  195. if (inb(chip->port + CS4231P(REGSEL)) & AD1848_INIT)
  196. snd_printk(KERN_WARNING
  197. "mce_down [0x%lx] - auto calibration time out (0)\n",
  198. chip->port + CS4231P(REGSEL));
  199. #endif
  200. chip->mce_bit &= ~AD1848_MCE;
  201. reg = inb(chip->port + CS4231P(REGSEL));
  202. outb(chip->mce_bit | (reg & 0x1f), chip->port + CS4231P(REGSEL));
  203. if (reg == 0x80)
  204. snd_printk(KERN_WARNING "mce_down [0x%lx]: serious init problem - codec still busy\n", chip->port);
  205. if ((reg & AD1848_MCE) == 0) {
  206. spin_unlock_irqrestore(&chip->reg_lock, flags);
  207. return;
  208. }
  209. /*
  210. * Wait for auto-calibration (AC) process to finish, i.e. ACI to go low.
  211. * It may take up to 5 sample periods (at most 907 us @ 5.5125 kHz) for
  212. * the process to _start_, so it is important to wait at least that long
  213. * before checking. Otherwise we might think AC has finished when it
  214. * has in fact not begun. It could take 128 (no AC) or 384 (AC) cycles
  215. * for ACI to drop. This gives a wait of at most 70 ms with a more
  216. * typical value of 3-9 ms.
  217. */
  218. timeout = jiffies + msecs_to_jiffies(250);
  219. do {
  220. spin_unlock_irqrestore(&chip->reg_lock, flags);
  221. msleep(1);
  222. spin_lock_irqsave(&chip->reg_lock, flags);
  223. reg = snd_ad1848_in(chip, AD1848_TEST_INIT) &
  224. AD1848_CALIB_IN_PROGRESS;
  225. } while (reg && time_before(jiffies, timeout));
  226. spin_unlock_irqrestore(&chip->reg_lock, flags);
  227. if (reg)
  228. snd_printk(KERN_ERR
  229. "mce_down - auto calibration time out (2)\n");
  230. snd_printdd("(4) jiffies = %lu\n", jiffies);
  231. snd_printd("mce_down - exit = 0x%x\n",
  232. inb(chip->port + CS4231P(REGSEL)));
  233. }
  234. static unsigned int snd_ad1848_get_count(unsigned char format,
  235. unsigned int size)
  236. {
  237. switch (format & 0xe0) {
  238. case AD1848_LINEAR_16:
  239. size >>= 1;
  240. break;
  241. }
  242. if (format & AD1848_STEREO)
  243. size >>= 1;
  244. return size;
  245. }
  246. static int snd_ad1848_trigger(struct snd_wss *chip, unsigned char what,
  247. int channel, int cmd)
  248. {
  249. int result = 0;
  250. #if 0
  251. printk("codec trigger!!! - what = %i, enable = %i, status = 0x%x\n", what, enable, inb(AD1848P(card, STATUS)));
  252. #endif
  253. spin_lock(&chip->reg_lock);
  254. if (cmd == SNDRV_PCM_TRIGGER_START) {
  255. if (chip->image[AD1848_IFACE_CTRL] & what) {
  256. spin_unlock(&chip->reg_lock);
  257. return 0;
  258. }
  259. snd_ad1848_out(chip, AD1848_IFACE_CTRL, chip->image[AD1848_IFACE_CTRL] |= what);
  260. } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  261. if (!(chip->image[AD1848_IFACE_CTRL] & what)) {
  262. spin_unlock(&chip->reg_lock);
  263. return 0;
  264. }
  265. snd_ad1848_out(chip, AD1848_IFACE_CTRL, chip->image[AD1848_IFACE_CTRL] &= ~what);
  266. } else {
  267. result = -EINVAL;
  268. }
  269. spin_unlock(&chip->reg_lock);
  270. return result;
  271. }
  272. /*
  273. * CODEC I/O
  274. */
  275. static unsigned char snd_ad1848_get_rate(unsigned int rate)
  276. {
  277. int i;
  278. for (i = 0; i < ARRAY_SIZE(rates); i++)
  279. if (rate == rates[i])
  280. return freq_bits[i];
  281. snd_BUG();
  282. return freq_bits[ARRAY_SIZE(rates) - 1];
  283. }
  284. static int snd_ad1848_ioctl(struct snd_pcm_substream *substream,
  285. unsigned int cmd, void *arg)
  286. {
  287. return snd_pcm_lib_ioctl(substream, cmd, arg);
  288. }
  289. static unsigned char snd_ad1848_get_format(int format, int channels)
  290. {
  291. unsigned char rformat;
  292. rformat = AD1848_LINEAR_8;
  293. switch (format) {
  294. case SNDRV_PCM_FORMAT_A_LAW: rformat = AD1848_ALAW_8; break;
  295. case SNDRV_PCM_FORMAT_MU_LAW: rformat = AD1848_ULAW_8; break;
  296. case SNDRV_PCM_FORMAT_S16_LE: rformat = AD1848_LINEAR_16; break;
  297. }
  298. if (channels > 1)
  299. rformat |= AD1848_STEREO;
  300. #if 0
  301. snd_printk("get_format: 0x%x (mode=0x%x)\n", format, mode);
  302. #endif
  303. return rformat;
  304. }
  305. static void snd_ad1848_calibrate_mute(struct snd_wss *chip, int mute)
  306. {
  307. unsigned long flags;
  308. mute = mute ? 1 : 0;
  309. spin_lock_irqsave(&chip->reg_lock, flags);
  310. if (chip->calibrate_mute == mute) {
  311. spin_unlock_irqrestore(&chip->reg_lock, flags);
  312. return;
  313. }
  314. if (!mute) {
  315. snd_ad1848_dout(chip, AD1848_LEFT_INPUT, chip->image[AD1848_LEFT_INPUT]);
  316. snd_ad1848_dout(chip, AD1848_RIGHT_INPUT, chip->image[AD1848_RIGHT_INPUT]);
  317. }
  318. snd_ad1848_dout(chip, AD1848_AUX1_LEFT_INPUT, mute ? 0x80 : chip->image[AD1848_AUX1_LEFT_INPUT]);
  319. snd_ad1848_dout(chip, AD1848_AUX1_RIGHT_INPUT, mute ? 0x80 : chip->image[AD1848_AUX1_RIGHT_INPUT]);
  320. snd_ad1848_dout(chip, AD1848_AUX2_LEFT_INPUT, mute ? 0x80 : chip->image[AD1848_AUX2_LEFT_INPUT]);
  321. snd_ad1848_dout(chip, AD1848_AUX2_RIGHT_INPUT, mute ? 0x80 : chip->image[AD1848_AUX2_RIGHT_INPUT]);
  322. snd_ad1848_dout(chip, AD1848_LEFT_OUTPUT, mute ? 0x80 : chip->image[AD1848_LEFT_OUTPUT]);
  323. snd_ad1848_dout(chip, AD1848_RIGHT_OUTPUT, mute ? 0x80 : chip->image[AD1848_RIGHT_OUTPUT]);
  324. chip->calibrate_mute = mute;
  325. spin_unlock_irqrestore(&chip->reg_lock, flags);
  326. }
  327. static void snd_ad1848_set_data_format(struct snd_wss *chip,
  328. struct snd_pcm_hw_params *hw_params)
  329. {
  330. if (hw_params == NULL) {
  331. chip->image[AD1848_DATA_FORMAT] = 0x20;
  332. } else {
  333. chip->image[AD1848_DATA_FORMAT] =
  334. snd_ad1848_get_format(params_format(hw_params), params_channels(hw_params)) |
  335. snd_ad1848_get_rate(params_rate(hw_params));
  336. }
  337. // snd_printk(">>> pmode = 0x%x, dfr = 0x%x\n", pstr->mode, chip->image[AD1848_DATA_FORMAT]);
  338. }
  339. static int snd_ad1848_open(struct snd_wss *chip, unsigned int mode)
  340. {
  341. unsigned long flags;
  342. if (chip->mode & WSS_MODE_OPEN)
  343. return -EAGAIN;
  344. snd_ad1848_mce_down(chip);
  345. #ifdef SNDRV_DEBUG_MCE
  346. snd_printk("open: (1)\n");
  347. #endif
  348. snd_ad1848_mce_up(chip);
  349. spin_lock_irqsave(&chip->reg_lock, flags);
  350. chip->image[AD1848_IFACE_CTRL] &= ~(AD1848_PLAYBACK_ENABLE | AD1848_PLAYBACK_PIO |
  351. AD1848_CAPTURE_ENABLE | AD1848_CAPTURE_PIO |
  352. AD1848_CALIB_MODE);
  353. chip->image[AD1848_IFACE_CTRL] |= AD1848_AUTOCALIB;
  354. snd_ad1848_out(chip, AD1848_IFACE_CTRL, chip->image[AD1848_IFACE_CTRL]);
  355. spin_unlock_irqrestore(&chip->reg_lock, flags);
  356. snd_ad1848_mce_down(chip);
  357. #ifdef SNDRV_DEBUG_MCE
  358. snd_printk("open: (2)\n");
  359. #endif
  360. snd_ad1848_set_data_format(chip, NULL);
  361. snd_ad1848_mce_up(chip);
  362. spin_lock_irqsave(&chip->reg_lock, flags);
  363. snd_ad1848_out(chip, AD1848_DATA_FORMAT, chip->image[AD1848_DATA_FORMAT]);
  364. spin_unlock_irqrestore(&chip->reg_lock, flags);
  365. snd_ad1848_mce_down(chip);
  366. #ifdef SNDRV_DEBUG_MCE
  367. snd_printk("open: (3)\n");
  368. #endif
  369. /* ok. now enable and ack CODEC IRQ */
  370. spin_lock_irqsave(&chip->reg_lock, flags);
  371. outb(0, chip->port + CS4231P(STATUS)); /* clear IRQ */
  372. outb(0, chip->port + CS4231P(STATUS)); /* clear IRQ */
  373. chip->image[AD1848_PIN_CTRL] |= AD1848_IRQ_ENABLE;
  374. snd_ad1848_out(chip, AD1848_PIN_CTRL, chip->image[AD1848_PIN_CTRL]);
  375. spin_unlock_irqrestore(&chip->reg_lock, flags);
  376. chip->mode = mode;
  377. return 0;
  378. }
  379. static void snd_ad1848_close(struct snd_wss *chip)
  380. {
  381. unsigned long flags;
  382. if (!chip->mode)
  383. return;
  384. /* disable IRQ */
  385. spin_lock_irqsave(&chip->reg_lock, flags);
  386. outb(0, chip->port + CS4231P(STATUS)); /* clear IRQ */
  387. outb(0, chip->port + CS4231P(STATUS)); /* clear IRQ */
  388. chip->image[AD1848_PIN_CTRL] &= ~AD1848_IRQ_ENABLE;
  389. snd_ad1848_out(chip, AD1848_PIN_CTRL, chip->image[AD1848_PIN_CTRL]);
  390. spin_unlock_irqrestore(&chip->reg_lock, flags);
  391. /* now disable capture & playback */
  392. snd_ad1848_mce_up(chip);
  393. spin_lock_irqsave(&chip->reg_lock, flags);
  394. chip->image[AD1848_IFACE_CTRL] &= ~(AD1848_PLAYBACK_ENABLE | AD1848_PLAYBACK_PIO |
  395. AD1848_CAPTURE_ENABLE | AD1848_CAPTURE_PIO);
  396. snd_ad1848_out(chip, AD1848_IFACE_CTRL, chip->image[AD1848_IFACE_CTRL]);
  397. spin_unlock_irqrestore(&chip->reg_lock, flags);
  398. snd_ad1848_mce_down(chip);
  399. /* clear IRQ again */
  400. spin_lock_irqsave(&chip->reg_lock, flags);
  401. outb(0, chip->port + CS4231P(STATUS)); /* clear IRQ */
  402. outb(0, chip->port + CS4231P(STATUS)); /* clear IRQ */
  403. spin_unlock_irqrestore(&chip->reg_lock, flags);
  404. chip->mode = 0;
  405. }
  406. /*
  407. * ok.. exported functions..
  408. */
  409. static int snd_ad1848_playback_trigger(struct snd_pcm_substream *substream,
  410. int cmd)
  411. {
  412. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  413. return snd_ad1848_trigger(chip, AD1848_PLAYBACK_ENABLE, SNDRV_PCM_STREAM_PLAYBACK, cmd);
  414. }
  415. static int snd_ad1848_capture_trigger(struct snd_pcm_substream *substream,
  416. int cmd)
  417. {
  418. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  419. return snd_ad1848_trigger(chip, AD1848_CAPTURE_ENABLE, SNDRV_PCM_STREAM_CAPTURE, cmd);
  420. }
  421. static int snd_ad1848_playback_hw_params(struct snd_pcm_substream *substream,
  422. struct snd_pcm_hw_params *hw_params)
  423. {
  424. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  425. unsigned long flags;
  426. int err;
  427. if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
  428. return err;
  429. snd_ad1848_calibrate_mute(chip, 1);
  430. snd_ad1848_set_data_format(chip, hw_params);
  431. snd_ad1848_mce_up(chip);
  432. spin_lock_irqsave(&chip->reg_lock, flags);
  433. snd_ad1848_out(chip, AD1848_DATA_FORMAT, chip->image[AD1848_DATA_FORMAT]);
  434. spin_unlock_irqrestore(&chip->reg_lock, flags);
  435. snd_ad1848_mce_down(chip);
  436. snd_ad1848_calibrate_mute(chip, 0);
  437. return 0;
  438. }
  439. static int snd_ad1848_playback_hw_free(struct snd_pcm_substream *substream)
  440. {
  441. return snd_pcm_lib_free_pages(substream);
  442. }
  443. static int snd_ad1848_playback_prepare(struct snd_pcm_substream *substream)
  444. {
  445. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  446. struct snd_pcm_runtime *runtime = substream->runtime;
  447. unsigned long flags;
  448. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  449. unsigned int count = snd_pcm_lib_period_bytes(substream);
  450. chip->p_dma_size = size;
  451. chip->image[AD1848_IFACE_CTRL] &= ~(AD1848_PLAYBACK_ENABLE | AD1848_PLAYBACK_PIO);
  452. snd_dma_program(chip->dma1, runtime->dma_addr, size,
  453. DMA_MODE_WRITE | DMA_AUTOINIT);
  454. count = snd_ad1848_get_count(chip->image[AD1848_DATA_FORMAT], count) - 1;
  455. spin_lock_irqsave(&chip->reg_lock, flags);
  456. snd_ad1848_out(chip, AD1848_DATA_LWR_CNT, (unsigned char) count);
  457. snd_ad1848_out(chip, AD1848_DATA_UPR_CNT, (unsigned char) (count >> 8));
  458. spin_unlock_irqrestore(&chip->reg_lock, flags);
  459. return 0;
  460. }
  461. static int snd_ad1848_capture_hw_params(struct snd_pcm_substream *substream,
  462. struct snd_pcm_hw_params *hw_params)
  463. {
  464. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  465. unsigned long flags;
  466. int err;
  467. if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
  468. return err;
  469. snd_ad1848_calibrate_mute(chip, 1);
  470. snd_ad1848_set_data_format(chip, hw_params);
  471. snd_ad1848_mce_up(chip);
  472. spin_lock_irqsave(&chip->reg_lock, flags);
  473. snd_ad1848_out(chip, AD1848_DATA_FORMAT, chip->image[AD1848_DATA_FORMAT]);
  474. spin_unlock_irqrestore(&chip->reg_lock, flags);
  475. snd_ad1848_mce_down(chip);
  476. snd_ad1848_calibrate_mute(chip, 0);
  477. return 0;
  478. }
  479. static int snd_ad1848_capture_hw_free(struct snd_pcm_substream *substream)
  480. {
  481. return snd_pcm_lib_free_pages(substream);
  482. }
  483. static int snd_ad1848_capture_prepare(struct snd_pcm_substream *substream)
  484. {
  485. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  486. struct snd_pcm_runtime *runtime = substream->runtime;
  487. unsigned long flags;
  488. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  489. unsigned int count = snd_pcm_lib_period_bytes(substream);
  490. chip->c_dma_size = size;
  491. chip->image[AD1848_IFACE_CTRL] &= ~(AD1848_CAPTURE_ENABLE | AD1848_CAPTURE_PIO);
  492. snd_dma_program(chip->dma2, runtime->dma_addr, size,
  493. DMA_MODE_READ | DMA_AUTOINIT);
  494. count = snd_ad1848_get_count(chip->image[AD1848_DATA_FORMAT], count) - 1;
  495. spin_lock_irqsave(&chip->reg_lock, flags);
  496. snd_ad1848_out(chip, AD1848_DATA_LWR_CNT, (unsigned char) count);
  497. snd_ad1848_out(chip, AD1848_DATA_UPR_CNT, (unsigned char) (count >> 8));
  498. spin_unlock_irqrestore(&chip->reg_lock, flags);
  499. return 0;
  500. }
  501. static irqreturn_t snd_ad1848_interrupt(int irq, void *dev_id)
  502. {
  503. struct snd_wss *chip = dev_id;
  504. if ((chip->mode & WSS_MODE_PLAY) && chip->playback_substream)
  505. snd_pcm_period_elapsed(chip->playback_substream);
  506. if ((chip->mode & WSS_MODE_RECORD) && chip->capture_substream)
  507. snd_pcm_period_elapsed(chip->capture_substream);
  508. outb(0, chip->port + CS4231P(STATUS)); /* clear global interrupt bit */
  509. return IRQ_HANDLED;
  510. }
  511. static snd_pcm_uframes_t snd_ad1848_playback_pointer(struct snd_pcm_substream *substream)
  512. {
  513. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  514. size_t ptr;
  515. if (!(chip->image[AD1848_IFACE_CTRL] & AD1848_PLAYBACK_ENABLE))
  516. return 0;
  517. ptr = snd_dma_pointer(chip->dma1, chip->p_dma_size);
  518. return bytes_to_frames(substream->runtime, ptr);
  519. }
  520. static snd_pcm_uframes_t snd_ad1848_capture_pointer(struct snd_pcm_substream *substream)
  521. {
  522. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  523. size_t ptr;
  524. if (!(chip->image[AD1848_IFACE_CTRL] & AD1848_CAPTURE_ENABLE))
  525. return 0;
  526. ptr = snd_dma_pointer(chip->dma2, chip->c_dma_size);
  527. return bytes_to_frames(substream->runtime, ptr);
  528. }
  529. /*
  530. */
  531. static void snd_ad1848_thinkpad_twiddle(struct snd_wss *chip, int on)
  532. {
  533. int tmp;
  534. if (!chip->thinkpad_flag) return;
  535. outb(0x1c, AD1848_THINKPAD_CTL_PORT1);
  536. tmp = inb(AD1848_THINKPAD_CTL_PORT2);
  537. if (on)
  538. /* turn it on */
  539. tmp |= AD1848_THINKPAD_CS4248_ENABLE_BIT;
  540. else
  541. /* turn it off */
  542. tmp &= ~AD1848_THINKPAD_CS4248_ENABLE_BIT;
  543. outb(tmp, AD1848_THINKPAD_CTL_PORT2);
  544. }
  545. #ifdef CONFIG_PM
  546. static void snd_ad1848_suspend(struct snd_wss *chip)
  547. {
  548. snd_pcm_suspend_all(chip->pcm);
  549. if (chip->thinkpad_flag)
  550. snd_ad1848_thinkpad_twiddle(chip, 0);
  551. }
  552. static void snd_ad1848_resume(struct snd_wss *chip)
  553. {
  554. int i;
  555. if (chip->thinkpad_flag)
  556. snd_ad1848_thinkpad_twiddle(chip, 1);
  557. /* clear any pendings IRQ */
  558. inb(chip->port + CS4231P(STATUS));
  559. outb(0, chip->port + CS4231P(STATUS));
  560. mb();
  561. snd_ad1848_mce_down(chip);
  562. for (i = 0; i < 16; i++)
  563. snd_ad1848_out(chip, i, chip->image[i]);
  564. snd_ad1848_mce_up(chip);
  565. snd_ad1848_mce_down(chip);
  566. }
  567. #endif /* CONFIG_PM */
  568. static int snd_ad1848_probe(struct snd_wss *chip)
  569. {
  570. unsigned long flags;
  571. int i, id, rev, ad1847;
  572. unsigned char *ptr;
  573. #if 0
  574. snd_ad1848_debug(chip);
  575. #endif
  576. id = ad1847 = 0;
  577. for (i = 0; i < 1000; i++) {
  578. mb();
  579. if (inb(chip->port + CS4231P(REGSEL)) & AD1848_INIT)
  580. udelay(500);
  581. else {
  582. spin_lock_irqsave(&chip->reg_lock, flags);
  583. snd_ad1848_out(chip, AD1848_MISC_INFO, 0x00);
  584. snd_ad1848_out(chip, AD1848_LEFT_INPUT, 0xaa);
  585. snd_ad1848_out(chip, AD1848_RIGHT_INPUT, 0x45);
  586. rev = snd_ad1848_in(chip, AD1848_RIGHT_INPUT);
  587. if (rev == 0x65) {
  588. spin_unlock_irqrestore(&chip->reg_lock, flags);
  589. id = 1;
  590. ad1847 = 1;
  591. break;
  592. }
  593. if (snd_ad1848_in(chip, AD1848_LEFT_INPUT) == 0xaa && rev == 0x45) {
  594. spin_unlock_irqrestore(&chip->reg_lock, flags);
  595. id = 1;
  596. break;
  597. }
  598. spin_unlock_irqrestore(&chip->reg_lock, flags);
  599. }
  600. }
  601. if (id != 1)
  602. return -ENODEV; /* no valid device found */
  603. if (chip->hardware == WSS_HW_DETECT) {
  604. if (ad1847) {
  605. chip->hardware = WSS_HW_AD1847;
  606. } else {
  607. chip->hardware = WSS_HW_AD1848;
  608. rev = snd_ad1848_in(chip, AD1848_MISC_INFO);
  609. if (rev & 0x80) {
  610. chip->hardware = WSS_HW_CS4248;
  611. } else if ((rev & 0x0f) == 0x0a) {
  612. snd_ad1848_out(chip, AD1848_MISC_INFO, 0x40);
  613. for (i = 0; i < 16; ++i) {
  614. if (snd_ad1848_in(chip, i) != snd_ad1848_in(chip, i + 16)) {
  615. chip->hardware = WSS_HW_CMI8330;
  616. break;
  617. }
  618. }
  619. snd_ad1848_out(chip, AD1848_MISC_INFO, 0x00);
  620. }
  621. }
  622. }
  623. spin_lock_irqsave(&chip->reg_lock, flags);
  624. inb(chip->port + CS4231P(STATUS)); /* clear any pendings IRQ */
  625. outb(0, chip->port + CS4231P(STATUS));
  626. mb();
  627. spin_unlock_irqrestore(&chip->reg_lock, flags);
  628. chip->image[AD1848_MISC_INFO] = 0x00;
  629. chip->image[AD1848_IFACE_CTRL] =
  630. (chip->image[AD1848_IFACE_CTRL] & ~AD1848_SINGLE_DMA) | AD1848_SINGLE_DMA;
  631. ptr = (unsigned char *) &chip->image;
  632. snd_ad1848_mce_down(chip);
  633. spin_lock_irqsave(&chip->reg_lock, flags);
  634. for (i = 0; i < 16; i++) /* ok.. fill all AD1848 registers */
  635. snd_ad1848_out(chip, i, *ptr++);
  636. spin_unlock_irqrestore(&chip->reg_lock, flags);
  637. snd_ad1848_mce_up(chip);
  638. snd_ad1848_mce_down(chip);
  639. return 0; /* all things are ok.. */
  640. }
  641. /*
  642. */
  643. static struct snd_pcm_hardware snd_ad1848_playback =
  644. {
  645. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  646. SNDRV_PCM_INFO_MMAP_VALID),
  647. .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW |
  648. SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE),
  649. .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
  650. .rate_min = 5510,
  651. .rate_max = 48000,
  652. .channels_min = 1,
  653. .channels_max = 2,
  654. .buffer_bytes_max = (128*1024),
  655. .period_bytes_min = 64,
  656. .period_bytes_max = (128*1024),
  657. .periods_min = 1,
  658. .periods_max = 1024,
  659. .fifo_size = 0,
  660. };
  661. static struct snd_pcm_hardware snd_ad1848_capture =
  662. {
  663. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  664. SNDRV_PCM_INFO_MMAP_VALID),
  665. .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW |
  666. SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE),
  667. .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
  668. .rate_min = 5510,
  669. .rate_max = 48000,
  670. .channels_min = 1,
  671. .channels_max = 2,
  672. .buffer_bytes_max = (128*1024),
  673. .period_bytes_min = 64,
  674. .period_bytes_max = (128*1024),
  675. .periods_min = 1,
  676. .periods_max = 1024,
  677. .fifo_size = 0,
  678. };
  679. /*
  680. */
  681. static int snd_ad1848_playback_open(struct snd_pcm_substream *substream)
  682. {
  683. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  684. struct snd_pcm_runtime *runtime = substream->runtime;
  685. int err;
  686. err = snd_ad1848_open(chip, WSS_MODE_PLAY);
  687. if (err < 0)
  688. return err;
  689. chip->playback_substream = substream;
  690. runtime->hw = snd_ad1848_playback;
  691. snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.buffer_bytes_max);
  692. snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.period_bytes_max);
  693. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
  694. return 0;
  695. }
  696. static int snd_ad1848_capture_open(struct snd_pcm_substream *substream)
  697. {
  698. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  699. struct snd_pcm_runtime *runtime = substream->runtime;
  700. int err;
  701. err = snd_ad1848_open(chip, WSS_MODE_RECORD);
  702. if (err < 0)
  703. return err;
  704. chip->capture_substream = substream;
  705. runtime->hw = snd_ad1848_capture;
  706. snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.buffer_bytes_max);
  707. snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.period_bytes_max);
  708. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
  709. return 0;
  710. }
  711. static int snd_ad1848_playback_close(struct snd_pcm_substream *substream)
  712. {
  713. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  714. chip->mode &= ~WSS_MODE_PLAY;
  715. chip->playback_substream = NULL;
  716. snd_ad1848_close(chip);
  717. return 0;
  718. }
  719. static int snd_ad1848_capture_close(struct snd_pcm_substream *substream)
  720. {
  721. struct snd_wss *chip = snd_pcm_substream_chip(substream);
  722. chip->mode &= ~WSS_MODE_RECORD;
  723. chip->capture_substream = NULL;
  724. snd_ad1848_close(chip);
  725. return 0;
  726. }
  727. static int snd_ad1848_free(struct snd_wss *chip)
  728. {
  729. release_and_free_resource(chip->res_port);
  730. if (chip->irq >= 0)
  731. free_irq(chip->irq, (void *) chip);
  732. if (chip->dma1 >= 0) {
  733. snd_dma_disable(chip->dma1);
  734. free_dma(chip->dma1);
  735. }
  736. kfree(chip);
  737. return 0;
  738. }
  739. static int snd_ad1848_dev_free(struct snd_device *device)
  740. {
  741. struct snd_wss *chip = device->device_data;
  742. return snd_ad1848_free(chip);
  743. }
  744. static const char *snd_ad1848_chip_id(struct snd_wss *chip)
  745. {
  746. switch (chip->hardware) {
  747. case AD1848_HW_AD1847: return "AD1847";
  748. case AD1848_HW_AD1848: return "AD1848";
  749. case AD1848_HW_CS4248: return "CS4248";
  750. case AD1848_HW_CMI8330: return "CMI8330/C3D";
  751. default: return "???";
  752. }
  753. }
  754. int snd_ad1848_create(struct snd_card *card,
  755. unsigned long port,
  756. int irq, int dma,
  757. unsigned short hardware,
  758. struct snd_wss **rchip)
  759. {
  760. static struct snd_device_ops ops = {
  761. .dev_free = snd_ad1848_dev_free,
  762. };
  763. struct snd_wss *chip;
  764. int err;
  765. *rchip = NULL;
  766. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  767. if (chip == NULL)
  768. return -ENOMEM;
  769. spin_lock_init(&chip->reg_lock);
  770. chip->card = card;
  771. chip->port = port;
  772. chip->irq = -1;
  773. chip->dma1 = -1;
  774. chip->dma2 = -1;
  775. chip->single_dma = 1;
  776. chip->hardware = hardware;
  777. memcpy(&chip->image, &snd_ad1848_original_image, sizeof(snd_ad1848_original_image));
  778. if ((chip->res_port = request_region(port, 4, "AD1848")) == NULL) {
  779. snd_printk(KERN_ERR "ad1848: can't grab port 0x%lx\n", port);
  780. snd_ad1848_free(chip);
  781. return -EBUSY;
  782. }
  783. if (request_irq(irq, snd_ad1848_interrupt, IRQF_DISABLED, "AD1848", (void *) chip)) {
  784. snd_printk(KERN_ERR "ad1848: can't grab IRQ %d\n", irq);
  785. snd_ad1848_free(chip);
  786. return -EBUSY;
  787. }
  788. chip->irq = irq;
  789. if (request_dma(dma, "AD1848")) {
  790. snd_printk(KERN_ERR "ad1848: can't grab DMA %d\n", dma);
  791. snd_ad1848_free(chip);
  792. return -EBUSY;
  793. }
  794. chip->dma1 = dma;
  795. chip->dma2 = dma;
  796. if (hardware == WSS_HW_THINKPAD) {
  797. chip->thinkpad_flag = 1;
  798. chip->hardware = WSS_HW_DETECT; /* reset */
  799. snd_ad1848_thinkpad_twiddle(chip, 1);
  800. }
  801. if (snd_ad1848_probe(chip) < 0) {
  802. snd_ad1848_free(chip);
  803. return -ENODEV;
  804. }
  805. /* Register device */
  806. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  807. snd_ad1848_free(chip);
  808. return err;
  809. }
  810. #ifdef CONFIG_PM
  811. chip->suspend = snd_ad1848_suspend;
  812. chip->resume = snd_ad1848_resume;
  813. #endif
  814. *rchip = chip;
  815. return 0;
  816. }
  817. EXPORT_SYMBOL(snd_ad1848_create);
  818. static struct snd_pcm_ops snd_ad1848_playback_ops = {
  819. .open = snd_ad1848_playback_open,
  820. .close = snd_ad1848_playback_close,
  821. .ioctl = snd_ad1848_ioctl,
  822. .hw_params = snd_ad1848_playback_hw_params,
  823. .hw_free = snd_ad1848_playback_hw_free,
  824. .prepare = snd_ad1848_playback_prepare,
  825. .trigger = snd_ad1848_playback_trigger,
  826. .pointer = snd_ad1848_playback_pointer,
  827. };
  828. static struct snd_pcm_ops snd_ad1848_capture_ops = {
  829. .open = snd_ad1848_capture_open,
  830. .close = snd_ad1848_capture_close,
  831. .ioctl = snd_ad1848_ioctl,
  832. .hw_params = snd_ad1848_capture_hw_params,
  833. .hw_free = snd_ad1848_capture_hw_free,
  834. .prepare = snd_ad1848_capture_prepare,
  835. .trigger = snd_ad1848_capture_trigger,
  836. .pointer = snd_ad1848_capture_pointer,
  837. };
  838. int snd_ad1848_pcm(struct snd_wss *chip, int device, struct snd_pcm **rpcm)
  839. {
  840. struct snd_pcm *pcm;
  841. int err;
  842. if ((err = snd_pcm_new(chip->card, "AD1848", device, 1, 1, &pcm)) < 0)
  843. return err;
  844. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ad1848_playback_ops);
  845. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ad1848_capture_ops);
  846. pcm->private_data = chip;
  847. pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
  848. strcpy(pcm->name, snd_ad1848_chip_id(chip));
  849. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  850. snd_dma_isa_data(),
  851. 64 * 1024,
  852. chip->dma1 > 3 ?
  853. 128 * 1024 : 64 * 1024);
  854. chip->pcm = pcm;
  855. if (rpcm)
  856. *rpcm = pcm;
  857. return 0;
  858. }
  859. EXPORT_SYMBOL(snd_ad1848_pcm);
  860. const struct snd_pcm_ops *snd_ad1848_get_pcm_ops(int direction)
  861. {
  862. return direction == SNDRV_PCM_STREAM_PLAYBACK ?
  863. &snd_ad1848_playback_ops : &snd_ad1848_capture_ops;
  864. }
  865. EXPORT_SYMBOL(snd_ad1848_get_pcm_ops);
  866. /*
  867. * INIT part
  868. */
  869. static int __init alsa_ad1848_init(void)
  870. {
  871. return 0;
  872. }
  873. static void __exit alsa_ad1848_exit(void)
  874. {
  875. }
  876. module_init(alsa_ad1848_init)
  877. module_exit(alsa_ad1848_exit)