es1688_lib.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  3. * Routines for control of ESS ES1688/688/488 chip
  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. #include <linux/init.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/delay.h>
  24. #include <linux/slab.h>
  25. #include <linux/ioport.h>
  26. #include <sound/core.h>
  27. #include <sound/es1688.h>
  28. #include <sound/initval.h>
  29. #include <asm/io.h>
  30. #include <asm/dma.h>
  31. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  32. MODULE_DESCRIPTION("ESS ESx688 lowlevel module");
  33. MODULE_LICENSE("GPL");
  34. static int snd_es1688_dsp_command(struct snd_es1688 *chip, unsigned char val)
  35. {
  36. int i;
  37. for (i = 10000; i; i--)
  38. if ((inb(ES1688P(chip, STATUS)) & 0x80) == 0) {
  39. outb(val, ES1688P(chip, COMMAND));
  40. return 1;
  41. }
  42. #ifdef CONFIG_SND_DEBUG
  43. printk(KERN_DEBUG "snd_es1688_dsp_command: timeout (0x%x)\n", val);
  44. #endif
  45. return 0;
  46. }
  47. static int snd_es1688_dsp_get_byte(struct snd_es1688 *chip)
  48. {
  49. int i;
  50. for (i = 1000; i; i--)
  51. if (inb(ES1688P(chip, DATA_AVAIL)) & 0x80)
  52. return inb(ES1688P(chip, READ));
  53. snd_printd("es1688 get byte failed: 0x%lx = 0x%x!!!\n", ES1688P(chip, DATA_AVAIL), inb(ES1688P(chip, DATA_AVAIL)));
  54. return -ENODEV;
  55. }
  56. static int snd_es1688_write(struct snd_es1688 *chip,
  57. unsigned char reg, unsigned char data)
  58. {
  59. if (!snd_es1688_dsp_command(chip, reg))
  60. return 0;
  61. return snd_es1688_dsp_command(chip, data);
  62. }
  63. static int snd_es1688_read(struct snd_es1688 *chip, unsigned char reg)
  64. {
  65. /* Read a byte from an extended mode register of ES1688 */
  66. if (!snd_es1688_dsp_command(chip, 0xc0))
  67. return -1;
  68. if (!snd_es1688_dsp_command(chip, reg))
  69. return -1;
  70. return snd_es1688_dsp_get_byte(chip);
  71. }
  72. void snd_es1688_mixer_write(struct snd_es1688 *chip,
  73. unsigned char reg, unsigned char data)
  74. {
  75. outb(reg, ES1688P(chip, MIXER_ADDR));
  76. udelay(10);
  77. outb(data, ES1688P(chip, MIXER_DATA));
  78. udelay(10);
  79. }
  80. static unsigned char snd_es1688_mixer_read(struct snd_es1688 *chip, unsigned char reg)
  81. {
  82. unsigned char result;
  83. outb(reg, ES1688P(chip, MIXER_ADDR));
  84. udelay(10);
  85. result = inb(ES1688P(chip, MIXER_DATA));
  86. udelay(10);
  87. return result;
  88. }
  89. int snd_es1688_reset(struct snd_es1688 *chip)
  90. {
  91. int i;
  92. outb(3, ES1688P(chip, RESET)); /* valid only for ESS chips, SB -> 1 */
  93. udelay(10);
  94. outb(0, ES1688P(chip, RESET));
  95. udelay(30);
  96. for (i = 0; i < 1000 && !(inb(ES1688P(chip, DATA_AVAIL)) & 0x80); i++);
  97. if (inb(ES1688P(chip, READ)) != 0xaa) {
  98. snd_printd("ess_reset at 0x%lx: failed!!!\n", chip->port);
  99. return -ENODEV;
  100. }
  101. snd_es1688_dsp_command(chip, 0xc6); /* enable extended mode */
  102. return 0;
  103. }
  104. EXPORT_SYMBOL(snd_es1688_reset);
  105. static int snd_es1688_probe(struct snd_es1688 *chip)
  106. {
  107. unsigned long flags;
  108. unsigned short major, minor, hw;
  109. int i;
  110. /*
  111. * initialization sequence
  112. */
  113. spin_lock_irqsave(&chip->reg_lock, flags); /* Some ESS1688 cards need this */
  114. inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
  115. inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
  116. inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
  117. inb(ES1688P(chip, ENABLE2)); /* ENABLE2 */
  118. inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
  119. inb(ES1688P(chip, ENABLE2)); /* ENABLE2 */
  120. inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
  121. inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
  122. inb(ES1688P(chip, ENABLE2)); /* ENABLE2 */
  123. inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
  124. inb(ES1688P(chip, ENABLE0)); /* ENABLE0 */
  125. if (snd_es1688_reset(chip) < 0) {
  126. snd_printdd("ESS: [0x%lx] reset failed... 0x%x\n", chip->port, inb(ES1688P(chip, READ)));
  127. spin_unlock_irqrestore(&chip->reg_lock, flags);
  128. return -ENODEV;
  129. }
  130. snd_es1688_dsp_command(chip, 0xe7); /* return identification */
  131. for (i = 1000, major = minor = 0; i; i--) {
  132. if (inb(ES1688P(chip, DATA_AVAIL)) & 0x80) {
  133. if (major == 0) {
  134. major = inb(ES1688P(chip, READ));
  135. } else {
  136. minor = inb(ES1688P(chip, READ));
  137. }
  138. }
  139. }
  140. spin_unlock_irqrestore(&chip->reg_lock, flags);
  141. snd_printdd("ESS: [0x%lx] found.. major = 0x%x, minor = 0x%x\n", chip->port, major, minor);
  142. chip->version = (major << 8) | minor;
  143. if (!chip->version)
  144. return -ENODEV; /* probably SB */
  145. hw = ES1688_HW_AUTO;
  146. switch (chip->version & 0xfff0) {
  147. case 0x4880:
  148. snd_printk(KERN_ERR "[0x%lx] ESS: AudioDrive ES488 detected, "
  149. "but driver is in another place\n", chip->port);
  150. return -ENODEV;
  151. case 0x6880:
  152. hw = (chip->version & 0x0f) >= 8 ? ES1688_HW_1688 : ES1688_HW_688;
  153. break;
  154. default:
  155. snd_printk(KERN_ERR "[0x%lx] ESS: unknown AudioDrive chip "
  156. "with version 0x%x (Jazz16 soundcard?)\n",
  157. chip->port, chip->version);
  158. return -ENODEV;
  159. }
  160. spin_lock_irqsave(&chip->reg_lock, flags);
  161. snd_es1688_write(chip, 0xb1, 0x10); /* disable IRQ */
  162. snd_es1688_write(chip, 0xb2, 0x00); /* disable DMA */
  163. spin_unlock_irqrestore(&chip->reg_lock, flags);
  164. /* enable joystick, but disable OPL3 */
  165. spin_lock_irqsave(&chip->mixer_lock, flags);
  166. snd_es1688_mixer_write(chip, 0x40, 0x01);
  167. spin_unlock_irqrestore(&chip->mixer_lock, flags);
  168. return 0;
  169. }
  170. static int snd_es1688_init(struct snd_es1688 * chip, int enable)
  171. {
  172. static int irqs[16] = {-1, -1, 0, -1, -1, 1, -1, 2, -1, 0, 3, -1, -1, -1, -1, -1};
  173. unsigned long flags;
  174. int cfg, irq_bits, dma, dma_bits, tmp, tmp1;
  175. /* ok.. setup MPU-401 port and joystick and OPL3 */
  176. cfg = 0x01; /* enable joystick, but disable OPL3 */
  177. if (enable && chip->mpu_port >= 0x300 && chip->mpu_irq > 0 && chip->hardware != ES1688_HW_688) {
  178. tmp = (chip->mpu_port & 0x0f0) >> 4;
  179. if (tmp <= 3) {
  180. switch (chip->mpu_irq) {
  181. case 9:
  182. tmp1 = 4;
  183. break;
  184. case 5:
  185. tmp1 = 5;
  186. break;
  187. case 7:
  188. tmp1 = 6;
  189. break;
  190. case 10:
  191. tmp1 = 7;
  192. break;
  193. default:
  194. tmp1 = 0;
  195. }
  196. if (tmp1) {
  197. cfg |= (tmp << 3) | (tmp1 << 5);
  198. }
  199. }
  200. }
  201. #if 0
  202. snd_printk(KERN_DEBUG "mpu cfg = 0x%x\n", cfg);
  203. #endif
  204. spin_lock_irqsave(&chip->reg_lock, flags);
  205. snd_es1688_mixer_write(chip, 0x40, cfg);
  206. spin_unlock_irqrestore(&chip->reg_lock, flags);
  207. /* --- */
  208. spin_lock_irqsave(&chip->reg_lock, flags);
  209. snd_es1688_read(chip, 0xb1);
  210. snd_es1688_read(chip, 0xb2);
  211. spin_unlock_irqrestore(&chip->reg_lock, flags);
  212. if (enable) {
  213. cfg = 0xf0; /* enable only DMA counter interrupt */
  214. irq_bits = irqs[chip->irq & 0x0f];
  215. if (irq_bits < 0) {
  216. snd_printk(KERN_ERR "[0x%lx] ESS: bad IRQ %d "
  217. "for ES1688 chip!!\n",
  218. chip->port, chip->irq);
  219. #if 0
  220. irq_bits = 0;
  221. cfg = 0x10;
  222. #endif
  223. return -EINVAL;
  224. }
  225. spin_lock_irqsave(&chip->reg_lock, flags);
  226. snd_es1688_write(chip, 0xb1, cfg | (irq_bits << 2));
  227. spin_unlock_irqrestore(&chip->reg_lock, flags);
  228. cfg = 0xf0; /* extended mode DMA enable */
  229. dma = chip->dma8;
  230. if (dma > 3 || dma == 2) {
  231. snd_printk(KERN_ERR "[0x%lx] ESS: bad DMA channel %d "
  232. "for ES1688 chip!!\n", chip->port, dma);
  233. #if 0
  234. dma_bits = 0;
  235. cfg = 0x00; /* disable all DMA */
  236. #endif
  237. return -EINVAL;
  238. } else {
  239. dma_bits = dma;
  240. if (dma != 3)
  241. dma_bits++;
  242. }
  243. spin_lock_irqsave(&chip->reg_lock, flags);
  244. snd_es1688_write(chip, 0xb2, cfg | (dma_bits << 2));
  245. spin_unlock_irqrestore(&chip->reg_lock, flags);
  246. } else {
  247. spin_lock_irqsave(&chip->reg_lock, flags);
  248. snd_es1688_write(chip, 0xb1, 0x10); /* disable IRQ */
  249. snd_es1688_write(chip, 0xb2, 0x00); /* disable DMA */
  250. spin_unlock_irqrestore(&chip->reg_lock, flags);
  251. }
  252. spin_lock_irqsave(&chip->reg_lock, flags);
  253. snd_es1688_read(chip, 0xb1);
  254. snd_es1688_read(chip, 0xb2);
  255. snd_es1688_reset(chip);
  256. spin_unlock_irqrestore(&chip->reg_lock, flags);
  257. return 0;
  258. }
  259. /*
  260. */
  261. static struct snd_ratnum clocks[2] = {
  262. {
  263. .num = 795444,
  264. .den_min = 1,
  265. .den_max = 128,
  266. .den_step = 1,
  267. },
  268. {
  269. .num = 397722,
  270. .den_min = 1,
  271. .den_max = 128,
  272. .den_step = 1,
  273. }
  274. };
  275. static struct snd_pcm_hw_constraint_ratnums hw_constraints_clocks = {
  276. .nrats = 2,
  277. .rats = clocks,
  278. };
  279. static void snd_es1688_set_rate(struct snd_es1688 *chip, struct snd_pcm_substream *substream)
  280. {
  281. struct snd_pcm_runtime *runtime = substream->runtime;
  282. unsigned int bits, divider;
  283. if (runtime->rate_num == clocks[0].num)
  284. bits = 256 - runtime->rate_den;
  285. else
  286. bits = 128 - runtime->rate_den;
  287. /* set filter register */
  288. divider = 256 - 7160000*20/(8*82*runtime->rate);
  289. /* write result to hardware */
  290. snd_es1688_write(chip, 0xa1, bits);
  291. snd_es1688_write(chip, 0xa2, divider);
  292. }
  293. static int snd_es1688_ioctl(struct snd_pcm_substream *substream,
  294. unsigned int cmd, void *arg)
  295. {
  296. return snd_pcm_lib_ioctl(substream, cmd, arg);
  297. }
  298. static int snd_es1688_trigger(struct snd_es1688 *chip, int cmd, unsigned char value)
  299. {
  300. int val;
  301. if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  302. value = 0x00;
  303. } else if (cmd != SNDRV_PCM_TRIGGER_START) {
  304. return -EINVAL;
  305. }
  306. spin_lock(&chip->reg_lock);
  307. chip->trigger_value = value;
  308. val = snd_es1688_read(chip, 0xb8);
  309. if ((val < 0) || (val & 0x0f) == value) {
  310. spin_unlock(&chip->reg_lock);
  311. return -EINVAL; /* something is wrong */
  312. }
  313. #if 0
  314. printk(KERN_DEBUG "trigger: val = 0x%x, value = 0x%x\n", val, value);
  315. printk(KERN_DEBUG "trigger: pointer = 0x%x\n",
  316. snd_dma_pointer(chip->dma8, chip->dma_size));
  317. #endif
  318. snd_es1688_write(chip, 0xb8, (val & 0xf0) | value);
  319. spin_unlock(&chip->reg_lock);
  320. return 0;
  321. }
  322. static int snd_es1688_hw_params(struct snd_pcm_substream *substream,
  323. struct snd_pcm_hw_params *hw_params)
  324. {
  325. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  326. }
  327. static int snd_es1688_hw_free(struct snd_pcm_substream *substream)
  328. {
  329. return snd_pcm_lib_free_pages(substream);
  330. }
  331. static int snd_es1688_playback_prepare(struct snd_pcm_substream *substream)
  332. {
  333. unsigned long flags;
  334. struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
  335. struct snd_pcm_runtime *runtime = substream->runtime;
  336. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  337. unsigned int count = snd_pcm_lib_period_bytes(substream);
  338. chip->dma_size = size;
  339. spin_lock_irqsave(&chip->reg_lock, flags);
  340. snd_es1688_reset(chip);
  341. snd_es1688_set_rate(chip, substream);
  342. snd_es1688_write(chip, 0xb8, 4); /* auto init DMA mode */
  343. snd_es1688_write(chip, 0xa8, (snd_es1688_read(chip, 0xa8) & ~0x03) | (3 - runtime->channels));
  344. snd_es1688_write(chip, 0xb9, 2); /* demand mode (4 bytes/request) */
  345. if (runtime->channels == 1) {
  346. if (snd_pcm_format_width(runtime->format) == 8) {
  347. /* 8. bit mono */
  348. snd_es1688_write(chip, 0xb6, 0x80);
  349. snd_es1688_write(chip, 0xb7, 0x51);
  350. snd_es1688_write(chip, 0xb7, 0xd0);
  351. } else {
  352. /* 16. bit mono */
  353. snd_es1688_write(chip, 0xb6, 0x00);
  354. snd_es1688_write(chip, 0xb7, 0x71);
  355. snd_es1688_write(chip, 0xb7, 0xf4);
  356. }
  357. } else {
  358. if (snd_pcm_format_width(runtime->format) == 8) {
  359. /* 8. bit stereo */
  360. snd_es1688_write(chip, 0xb6, 0x80);
  361. snd_es1688_write(chip, 0xb7, 0x51);
  362. snd_es1688_write(chip, 0xb7, 0x98);
  363. } else {
  364. /* 16. bit stereo */
  365. snd_es1688_write(chip, 0xb6, 0x00);
  366. snd_es1688_write(chip, 0xb7, 0x71);
  367. snd_es1688_write(chip, 0xb7, 0xbc);
  368. }
  369. }
  370. snd_es1688_write(chip, 0xb1, (snd_es1688_read(chip, 0xb1) & 0x0f) | 0x50);
  371. snd_es1688_write(chip, 0xb2, (snd_es1688_read(chip, 0xb2) & 0x0f) | 0x50);
  372. snd_es1688_dsp_command(chip, ES1688_DSP_CMD_SPKON);
  373. spin_unlock_irqrestore(&chip->reg_lock, flags);
  374. /* --- */
  375. count = -count;
  376. snd_dma_program(chip->dma8, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
  377. spin_lock_irqsave(&chip->reg_lock, flags);
  378. snd_es1688_write(chip, 0xa4, (unsigned char) count);
  379. snd_es1688_write(chip, 0xa5, (unsigned char) (count >> 8));
  380. spin_unlock_irqrestore(&chip->reg_lock, flags);
  381. return 0;
  382. }
  383. static int snd_es1688_playback_trigger(struct snd_pcm_substream *substream,
  384. int cmd)
  385. {
  386. struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
  387. return snd_es1688_trigger(chip, cmd, 0x05);
  388. }
  389. static int snd_es1688_capture_prepare(struct snd_pcm_substream *substream)
  390. {
  391. unsigned long flags;
  392. struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
  393. struct snd_pcm_runtime *runtime = substream->runtime;
  394. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  395. unsigned int count = snd_pcm_lib_period_bytes(substream);
  396. chip->dma_size = size;
  397. spin_lock_irqsave(&chip->reg_lock, flags);
  398. snd_es1688_reset(chip);
  399. snd_es1688_set_rate(chip, substream);
  400. snd_es1688_dsp_command(chip, ES1688_DSP_CMD_SPKOFF);
  401. snd_es1688_write(chip, 0xb8, 0x0e); /* auto init DMA mode */
  402. snd_es1688_write(chip, 0xa8, (snd_es1688_read(chip, 0xa8) & ~0x03) | (3 - runtime->channels));
  403. snd_es1688_write(chip, 0xb9, 2); /* demand mode (4 bytes/request) */
  404. if (runtime->channels == 1) {
  405. if (snd_pcm_format_width(runtime->format) == 8) {
  406. /* 8. bit mono */
  407. snd_es1688_write(chip, 0xb7, 0x51);
  408. snd_es1688_write(chip, 0xb7, 0xd0);
  409. } else {
  410. /* 16. bit mono */
  411. snd_es1688_write(chip, 0xb7, 0x71);
  412. snd_es1688_write(chip, 0xb7, 0xf4);
  413. }
  414. } else {
  415. if (snd_pcm_format_width(runtime->format) == 8) {
  416. /* 8. bit stereo */
  417. snd_es1688_write(chip, 0xb7, 0x51);
  418. snd_es1688_write(chip, 0xb7, 0x98);
  419. } else {
  420. /* 16. bit stereo */
  421. snd_es1688_write(chip, 0xb7, 0x71);
  422. snd_es1688_write(chip, 0xb7, 0xbc);
  423. }
  424. }
  425. snd_es1688_write(chip, 0xb1, (snd_es1688_read(chip, 0xb1) & 0x0f) | 0x50);
  426. snd_es1688_write(chip, 0xb2, (snd_es1688_read(chip, 0xb2) & 0x0f) | 0x50);
  427. spin_unlock_irqrestore(&chip->reg_lock, flags);
  428. /* --- */
  429. count = -count;
  430. snd_dma_program(chip->dma8, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
  431. spin_lock_irqsave(&chip->reg_lock, flags);
  432. snd_es1688_write(chip, 0xa4, (unsigned char) count);
  433. snd_es1688_write(chip, 0xa5, (unsigned char) (count >> 8));
  434. spin_unlock_irqrestore(&chip->reg_lock, flags);
  435. return 0;
  436. }
  437. static int snd_es1688_capture_trigger(struct snd_pcm_substream *substream,
  438. int cmd)
  439. {
  440. struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
  441. return snd_es1688_trigger(chip, cmd, 0x0f);
  442. }
  443. static irqreturn_t snd_es1688_interrupt(int irq, void *dev_id)
  444. {
  445. struct snd_es1688 *chip = dev_id;
  446. if (chip->trigger_value == 0x05) /* ok.. playback is active */
  447. snd_pcm_period_elapsed(chip->playback_substream);
  448. if (chip->trigger_value == 0x0f) /* ok.. capture is active */
  449. snd_pcm_period_elapsed(chip->capture_substream);
  450. inb(ES1688P(chip, DATA_AVAIL)); /* ack interrupt */
  451. return IRQ_HANDLED;
  452. }
  453. static snd_pcm_uframes_t snd_es1688_playback_pointer(struct snd_pcm_substream *substream)
  454. {
  455. struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
  456. size_t ptr;
  457. if (chip->trigger_value != 0x05)
  458. return 0;
  459. ptr = snd_dma_pointer(chip->dma8, chip->dma_size);
  460. return bytes_to_frames(substream->runtime, ptr);
  461. }
  462. static snd_pcm_uframes_t snd_es1688_capture_pointer(struct snd_pcm_substream *substream)
  463. {
  464. struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
  465. size_t ptr;
  466. if (chip->trigger_value != 0x0f)
  467. return 0;
  468. ptr = snd_dma_pointer(chip->dma8, chip->dma_size);
  469. return bytes_to_frames(substream->runtime, ptr);
  470. }
  471. /*
  472. */
  473. static struct snd_pcm_hardware snd_es1688_playback =
  474. {
  475. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  476. SNDRV_PCM_INFO_MMAP_VALID),
  477. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  478. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  479. .rate_min = 4000,
  480. .rate_max = 48000,
  481. .channels_min = 1,
  482. .channels_max = 2,
  483. .buffer_bytes_max = 65536,
  484. .period_bytes_min = 64,
  485. .period_bytes_max = 65536,
  486. .periods_min = 1,
  487. .periods_max = 1024,
  488. .fifo_size = 0,
  489. };
  490. static struct snd_pcm_hardware snd_es1688_capture =
  491. {
  492. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  493. SNDRV_PCM_INFO_MMAP_VALID),
  494. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  495. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  496. .rate_min = 4000,
  497. .rate_max = 48000,
  498. .channels_min = 1,
  499. .channels_max = 2,
  500. .buffer_bytes_max = 65536,
  501. .period_bytes_min = 64,
  502. .period_bytes_max = 65536,
  503. .periods_min = 1,
  504. .periods_max = 1024,
  505. .fifo_size = 0,
  506. };
  507. /*
  508. */
  509. static int snd_es1688_playback_open(struct snd_pcm_substream *substream)
  510. {
  511. struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
  512. struct snd_pcm_runtime *runtime = substream->runtime;
  513. if (chip->capture_substream != NULL)
  514. return -EAGAIN;
  515. chip->playback_substream = substream;
  516. runtime->hw = snd_es1688_playback;
  517. snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  518. &hw_constraints_clocks);
  519. return 0;
  520. }
  521. static int snd_es1688_capture_open(struct snd_pcm_substream *substream)
  522. {
  523. struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
  524. struct snd_pcm_runtime *runtime = substream->runtime;
  525. if (chip->playback_substream != NULL)
  526. return -EAGAIN;
  527. chip->capture_substream = substream;
  528. runtime->hw = snd_es1688_capture;
  529. snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  530. &hw_constraints_clocks);
  531. return 0;
  532. }
  533. static int snd_es1688_playback_close(struct snd_pcm_substream *substream)
  534. {
  535. struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
  536. chip->playback_substream = NULL;
  537. return 0;
  538. }
  539. static int snd_es1688_capture_close(struct snd_pcm_substream *substream)
  540. {
  541. struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
  542. chip->capture_substream = NULL;
  543. return 0;
  544. }
  545. static int snd_es1688_free(struct snd_es1688 *chip)
  546. {
  547. if (chip->res_port) {
  548. snd_es1688_init(chip, 0);
  549. release_and_free_resource(chip->res_port);
  550. }
  551. if (chip->irq >= 0)
  552. free_irq(chip->irq, (void *) chip);
  553. if (chip->dma8 >= 0) {
  554. disable_dma(chip->dma8);
  555. free_dma(chip->dma8);
  556. }
  557. return 0;
  558. }
  559. static int snd_es1688_dev_free(struct snd_device *device)
  560. {
  561. struct snd_es1688 *chip = device->device_data;
  562. return snd_es1688_free(chip);
  563. }
  564. static const char *snd_es1688_chip_id(struct snd_es1688 *chip)
  565. {
  566. static char tmp[16];
  567. sprintf(tmp, "ES%s688 rev %i", chip->hardware == ES1688_HW_688 ? "" : "1", chip->version & 0x0f);
  568. return tmp;
  569. }
  570. int snd_es1688_create(struct snd_card *card,
  571. struct snd_es1688 *chip,
  572. unsigned long port,
  573. unsigned long mpu_port,
  574. int irq,
  575. int mpu_irq,
  576. int dma8,
  577. unsigned short hardware)
  578. {
  579. static struct snd_device_ops ops = {
  580. .dev_free = snd_es1688_dev_free,
  581. };
  582. int err;
  583. if (chip == NULL)
  584. return -ENOMEM;
  585. chip->irq = -1;
  586. chip->dma8 = -1;
  587. if ((chip->res_port = request_region(port + 4, 12, "ES1688")) == NULL) {
  588. snd_printk(KERN_ERR "es1688: can't grab port 0x%lx\n", port + 4);
  589. return -EBUSY;
  590. }
  591. if (request_irq(irq, snd_es1688_interrupt, IRQF_DISABLED, "ES1688", (void *) chip)) {
  592. snd_printk(KERN_ERR "es1688: can't grab IRQ %d\n", irq);
  593. return -EBUSY;
  594. }
  595. chip->irq = irq;
  596. if (request_dma(dma8, "ES1688")) {
  597. snd_printk(KERN_ERR "es1688: can't grab DMA8 %d\n", dma8);
  598. return -EBUSY;
  599. }
  600. chip->dma8 = dma8;
  601. spin_lock_init(&chip->reg_lock);
  602. spin_lock_init(&chip->mixer_lock);
  603. chip->port = port;
  604. mpu_port &= ~0x000f;
  605. if (mpu_port < 0x300 || mpu_port > 0x330)
  606. mpu_port = 0;
  607. chip->mpu_port = mpu_port;
  608. chip->mpu_irq = mpu_irq;
  609. chip->hardware = hardware;
  610. err = snd_es1688_probe(chip);
  611. if (err < 0)
  612. return err;
  613. err = snd_es1688_init(chip, 1);
  614. if (err < 0)
  615. return err;
  616. /* Register device */
  617. return snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  618. }
  619. static struct snd_pcm_ops snd_es1688_playback_ops = {
  620. .open = snd_es1688_playback_open,
  621. .close = snd_es1688_playback_close,
  622. .ioctl = snd_es1688_ioctl,
  623. .hw_params = snd_es1688_hw_params,
  624. .hw_free = snd_es1688_hw_free,
  625. .prepare = snd_es1688_playback_prepare,
  626. .trigger = snd_es1688_playback_trigger,
  627. .pointer = snd_es1688_playback_pointer,
  628. };
  629. static struct snd_pcm_ops snd_es1688_capture_ops = {
  630. .open = snd_es1688_capture_open,
  631. .close = snd_es1688_capture_close,
  632. .ioctl = snd_es1688_ioctl,
  633. .hw_params = snd_es1688_hw_params,
  634. .hw_free = snd_es1688_hw_free,
  635. .prepare = snd_es1688_capture_prepare,
  636. .trigger = snd_es1688_capture_trigger,
  637. .pointer = snd_es1688_capture_pointer,
  638. };
  639. int snd_es1688_pcm(struct snd_card *card, struct snd_es1688 *chip,
  640. int device, struct snd_pcm **rpcm)
  641. {
  642. struct snd_pcm *pcm;
  643. int err;
  644. err = snd_pcm_new(card, "ESx688", device, 1, 1, &pcm);
  645. if (err < 0)
  646. return err;
  647. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es1688_playback_ops);
  648. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es1688_capture_ops);
  649. pcm->private_data = chip;
  650. pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
  651. sprintf(pcm->name, snd_es1688_chip_id(chip));
  652. chip->pcm = pcm;
  653. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  654. snd_dma_isa_data(),
  655. 64*1024, 64*1024);
  656. if (rpcm)
  657. *rpcm = pcm;
  658. return 0;
  659. }
  660. /*
  661. * MIXER part
  662. */
  663. static int snd_es1688_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  664. {
  665. static char *texts[9] = {
  666. "Mic", "Mic Master", "CD", "AOUT",
  667. "Mic1", "Mix", "Line", "Master"
  668. };
  669. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  670. uinfo->count = 1;
  671. uinfo->value.enumerated.items = 8;
  672. if (uinfo->value.enumerated.item > 7)
  673. uinfo->value.enumerated.item = 7;
  674. strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
  675. return 0;
  676. }
  677. static int snd_es1688_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  678. {
  679. struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
  680. ucontrol->value.enumerated.item[0] = snd_es1688_mixer_read(chip, ES1688_REC_DEV) & 7;
  681. return 0;
  682. }
  683. static int snd_es1688_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  684. {
  685. struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
  686. unsigned long flags;
  687. unsigned char oval, nval;
  688. int change;
  689. if (ucontrol->value.enumerated.item[0] > 8)
  690. return -EINVAL;
  691. spin_lock_irqsave(&chip->reg_lock, flags);
  692. oval = snd_es1688_mixer_read(chip, ES1688_REC_DEV);
  693. nval = (ucontrol->value.enumerated.item[0] & 7) | (oval & ~15);
  694. change = nval != oval;
  695. if (change)
  696. snd_es1688_mixer_write(chip, ES1688_REC_DEV, nval);
  697. spin_unlock_irqrestore(&chip->reg_lock, flags);
  698. return change;
  699. }
  700. #define ES1688_SINGLE(xname, xindex, reg, shift, mask, invert) \
  701. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  702. .info = snd_es1688_info_single, \
  703. .get = snd_es1688_get_single, .put = snd_es1688_put_single, \
  704. .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
  705. static int snd_es1688_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  706. {
  707. int mask = (kcontrol->private_value >> 16) & 0xff;
  708. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  709. uinfo->count = 1;
  710. uinfo->value.integer.min = 0;
  711. uinfo->value.integer.max = mask;
  712. return 0;
  713. }
  714. static int snd_es1688_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  715. {
  716. struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
  717. unsigned long flags;
  718. int reg = kcontrol->private_value & 0xff;
  719. int shift = (kcontrol->private_value >> 8) & 0xff;
  720. int mask = (kcontrol->private_value >> 16) & 0xff;
  721. int invert = (kcontrol->private_value >> 24) & 0xff;
  722. spin_lock_irqsave(&chip->reg_lock, flags);
  723. ucontrol->value.integer.value[0] = (snd_es1688_mixer_read(chip, reg) >> shift) & mask;
  724. spin_unlock_irqrestore(&chip->reg_lock, flags);
  725. if (invert)
  726. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  727. return 0;
  728. }
  729. static int snd_es1688_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  730. {
  731. struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
  732. unsigned long flags;
  733. int reg = kcontrol->private_value & 0xff;
  734. int shift = (kcontrol->private_value >> 8) & 0xff;
  735. int mask = (kcontrol->private_value >> 16) & 0xff;
  736. int invert = (kcontrol->private_value >> 24) & 0xff;
  737. int change;
  738. unsigned char oval, nval;
  739. nval = (ucontrol->value.integer.value[0] & mask);
  740. if (invert)
  741. nval = mask - nval;
  742. nval <<= shift;
  743. spin_lock_irqsave(&chip->reg_lock, flags);
  744. oval = snd_es1688_mixer_read(chip, reg);
  745. nval = (oval & ~(mask << shift)) | nval;
  746. change = nval != oval;
  747. if (change)
  748. snd_es1688_mixer_write(chip, reg, nval);
  749. spin_unlock_irqrestore(&chip->reg_lock, flags);
  750. return change;
  751. }
  752. #define ES1688_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
  753. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  754. .info = snd_es1688_info_double, \
  755. .get = snd_es1688_get_double, .put = snd_es1688_put_double, \
  756. .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
  757. static int snd_es1688_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  758. {
  759. int mask = (kcontrol->private_value >> 24) & 0xff;
  760. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  761. uinfo->count = 2;
  762. uinfo->value.integer.min = 0;
  763. uinfo->value.integer.max = mask;
  764. return 0;
  765. }
  766. static int snd_es1688_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  767. {
  768. struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
  769. unsigned long flags;
  770. int left_reg = kcontrol->private_value & 0xff;
  771. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  772. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  773. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  774. int mask = (kcontrol->private_value >> 24) & 0xff;
  775. int invert = (kcontrol->private_value >> 22) & 1;
  776. unsigned char left, right;
  777. spin_lock_irqsave(&chip->reg_lock, flags);
  778. if (left_reg < 0xa0)
  779. left = snd_es1688_mixer_read(chip, left_reg);
  780. else
  781. left = snd_es1688_read(chip, left_reg);
  782. if (left_reg != right_reg) {
  783. if (right_reg < 0xa0)
  784. right = snd_es1688_mixer_read(chip, right_reg);
  785. else
  786. right = snd_es1688_read(chip, right_reg);
  787. } else
  788. right = left;
  789. spin_unlock_irqrestore(&chip->reg_lock, flags);
  790. ucontrol->value.integer.value[0] = (left >> shift_left) & mask;
  791. ucontrol->value.integer.value[1] = (right >> shift_right) & mask;
  792. if (invert) {
  793. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  794. ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
  795. }
  796. return 0;
  797. }
  798. static int snd_es1688_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  799. {
  800. struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
  801. unsigned long flags;
  802. int left_reg = kcontrol->private_value & 0xff;
  803. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  804. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  805. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  806. int mask = (kcontrol->private_value >> 24) & 0xff;
  807. int invert = (kcontrol->private_value >> 22) & 1;
  808. int change;
  809. unsigned char val1, val2, oval1, oval2;
  810. val1 = ucontrol->value.integer.value[0] & mask;
  811. val2 = ucontrol->value.integer.value[1] & mask;
  812. if (invert) {
  813. val1 = mask - val1;
  814. val2 = mask - val2;
  815. }
  816. val1 <<= shift_left;
  817. val2 <<= shift_right;
  818. spin_lock_irqsave(&chip->reg_lock, flags);
  819. if (left_reg != right_reg) {
  820. if (left_reg < 0xa0)
  821. oval1 = snd_es1688_mixer_read(chip, left_reg);
  822. else
  823. oval1 = snd_es1688_read(chip, left_reg);
  824. if (right_reg < 0xa0)
  825. oval2 = snd_es1688_mixer_read(chip, right_reg);
  826. else
  827. oval2 = snd_es1688_read(chip, right_reg);
  828. val1 = (oval1 & ~(mask << shift_left)) | val1;
  829. val2 = (oval2 & ~(mask << shift_right)) | val2;
  830. change = val1 != oval1 || val2 != oval2;
  831. if (change) {
  832. if (left_reg < 0xa0)
  833. snd_es1688_mixer_write(chip, left_reg, val1);
  834. else
  835. snd_es1688_write(chip, left_reg, val1);
  836. if (right_reg < 0xa0)
  837. snd_es1688_mixer_write(chip, right_reg, val1);
  838. else
  839. snd_es1688_write(chip, right_reg, val1);
  840. }
  841. } else {
  842. if (left_reg < 0xa0)
  843. oval1 = snd_es1688_mixer_read(chip, left_reg);
  844. else
  845. oval1 = snd_es1688_read(chip, left_reg);
  846. val1 = (oval1 & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
  847. change = val1 != oval1;
  848. if (change) {
  849. if (left_reg < 0xa0)
  850. snd_es1688_mixer_write(chip, left_reg, val1);
  851. else
  852. snd_es1688_write(chip, left_reg, val1);
  853. }
  854. }
  855. spin_unlock_irqrestore(&chip->reg_lock, flags);
  856. return change;
  857. }
  858. static struct snd_kcontrol_new snd_es1688_controls[] = {
  859. ES1688_DOUBLE("Master Playback Volume", 0, ES1688_MASTER_DEV, ES1688_MASTER_DEV, 4, 0, 15, 0),
  860. ES1688_DOUBLE("PCM Playback Volume", 0, ES1688_PCM_DEV, ES1688_PCM_DEV, 4, 0, 15, 0),
  861. ES1688_DOUBLE("Line Playback Volume", 0, ES1688_LINE_DEV, ES1688_LINE_DEV, 4, 0, 15, 0),
  862. ES1688_DOUBLE("CD Playback Volume", 0, ES1688_CD_DEV, ES1688_CD_DEV, 4, 0, 15, 0),
  863. ES1688_DOUBLE("FM Playback Volume", 0, ES1688_FM_DEV, ES1688_FM_DEV, 4, 0, 15, 0),
  864. ES1688_DOUBLE("Mic Playback Volume", 0, ES1688_MIC_DEV, ES1688_MIC_DEV, 4, 0, 15, 0),
  865. ES1688_DOUBLE("Aux Playback Volume", 0, ES1688_AUX_DEV, ES1688_AUX_DEV, 4, 0, 15, 0),
  866. ES1688_SINGLE("Beep Playback Volume", 0, ES1688_SPEAKER_DEV, 0, 7, 0),
  867. ES1688_DOUBLE("Capture Volume", 0, ES1688_RECLEV_DEV, ES1688_RECLEV_DEV, 4, 0, 15, 0),
  868. ES1688_SINGLE("Capture Switch", 0, ES1688_REC_DEV, 4, 1, 1),
  869. {
  870. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  871. .name = "Capture Source",
  872. .info = snd_es1688_info_mux,
  873. .get = snd_es1688_get_mux,
  874. .put = snd_es1688_put_mux,
  875. },
  876. };
  877. #define ES1688_INIT_TABLE_SIZE (sizeof(snd_es1688_init_table)/2)
  878. static unsigned char snd_es1688_init_table[][2] = {
  879. { ES1688_MASTER_DEV, 0 },
  880. { ES1688_PCM_DEV, 0 },
  881. { ES1688_LINE_DEV, 0 },
  882. { ES1688_CD_DEV, 0 },
  883. { ES1688_FM_DEV, 0 },
  884. { ES1688_MIC_DEV, 0 },
  885. { ES1688_AUX_DEV, 0 },
  886. { ES1688_SPEAKER_DEV, 0 },
  887. { ES1688_RECLEV_DEV, 0 },
  888. { ES1688_REC_DEV, 0x17 }
  889. };
  890. int snd_es1688_mixer(struct snd_card *card, struct snd_es1688 *chip)
  891. {
  892. unsigned int idx;
  893. int err;
  894. unsigned char reg, val;
  895. if (snd_BUG_ON(!chip || !card))
  896. return -EINVAL;
  897. strcpy(card->mixername, snd_es1688_chip_id(chip));
  898. for (idx = 0; idx < ARRAY_SIZE(snd_es1688_controls); idx++) {
  899. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es1688_controls[idx], chip))) < 0)
  900. return err;
  901. }
  902. for (idx = 0; idx < ES1688_INIT_TABLE_SIZE; idx++) {
  903. reg = snd_es1688_init_table[idx][0];
  904. val = snd_es1688_init_table[idx][1];
  905. if (reg < 0xa0)
  906. snd_es1688_mixer_write(chip, reg, val);
  907. else
  908. snd_es1688_write(chip, reg, val);
  909. }
  910. return 0;
  911. }
  912. EXPORT_SYMBOL(snd_es1688_mixer_write);
  913. EXPORT_SYMBOL(snd_es1688_create);
  914. EXPORT_SYMBOL(snd_es1688_pcm);
  915. EXPORT_SYMBOL(snd_es1688_mixer);
  916. /*
  917. * INIT part
  918. */
  919. static int __init alsa_es1688_init(void)
  920. {
  921. return 0;
  922. }
  923. static void __exit alsa_es1688_exit(void)
  924. {
  925. }
  926. module_init(alsa_es1688_init)
  927. module_exit(alsa_es1688_exit)