es1688_lib.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  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 <linux/module.h>
  27. #include <linux/io.h>
  28. #include <sound/core.h>
  29. #include <sound/es1688.h>
  30. #include <sound/initval.h>
  31. #include <asm/dma.h>
  32. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  33. MODULE_DESCRIPTION("ESS ESx688 lowlevel module");
  34. MODULE_LICENSE("GPL");
  35. static int snd_es1688_dsp_command(struct snd_es1688 *chip, unsigned char val)
  36. {
  37. int i;
  38. for (i = 10000; i; i--)
  39. if ((inb(ES1688P(chip, STATUS)) & 0x80) == 0) {
  40. outb(val, ES1688P(chip, COMMAND));
  41. return 1;
  42. }
  43. #ifdef CONFIG_SND_DEBUG
  44. printk(KERN_DEBUG "snd_es1688_dsp_command: timeout (0x%x)\n", val);
  45. #endif
  46. return 0;
  47. }
  48. static int snd_es1688_dsp_get_byte(struct snd_es1688 *chip)
  49. {
  50. int i;
  51. for (i = 1000; i; i--)
  52. if (inb(ES1688P(chip, DATA_AVAIL)) & 0x80)
  53. return inb(ES1688P(chip, READ));
  54. snd_printd("es1688 get byte failed: 0x%lx = 0x%x!!!\n", ES1688P(chip, DATA_AVAIL), inb(ES1688P(chip, DATA_AVAIL)));
  55. return -ENODEV;
  56. }
  57. static int snd_es1688_write(struct snd_es1688 *chip,
  58. unsigned char reg, unsigned char data)
  59. {
  60. if (!snd_es1688_dsp_command(chip, reg))
  61. return 0;
  62. return snd_es1688_dsp_command(chip, data);
  63. }
  64. static int snd_es1688_read(struct snd_es1688 *chip, unsigned char reg)
  65. {
  66. /* Read a byte from an extended mode register of ES1688 */
  67. if (!snd_es1688_dsp_command(chip, 0xc0))
  68. return -1;
  69. if (!snd_es1688_dsp_command(chip, reg))
  70. return -1;
  71. return snd_es1688_dsp_get_byte(chip);
  72. }
  73. void snd_es1688_mixer_write(struct snd_es1688 *chip,
  74. unsigned char reg, unsigned char data)
  75. {
  76. outb(reg, ES1688P(chip, MIXER_ADDR));
  77. udelay(10);
  78. outb(data, ES1688P(chip, MIXER_DATA));
  79. udelay(10);
  80. }
  81. static unsigned char snd_es1688_mixer_read(struct snd_es1688 *chip, unsigned char reg)
  82. {
  83. unsigned char result;
  84. outb(reg, ES1688P(chip, MIXER_ADDR));
  85. udelay(10);
  86. result = inb(ES1688P(chip, MIXER_DATA));
  87. udelay(10);
  88. return result;
  89. }
  90. int snd_es1688_reset(struct snd_es1688 *chip)
  91. {
  92. int i;
  93. outb(3, ES1688P(chip, RESET)); /* valid only for ESS chips, SB -> 1 */
  94. udelay(10);
  95. outb(0, ES1688P(chip, RESET));
  96. udelay(30);
  97. for (i = 0; i < 1000 && !(inb(ES1688P(chip, DATA_AVAIL)) & 0x80); i++);
  98. if (inb(ES1688P(chip, READ)) != 0xaa) {
  99. snd_printd("ess_reset at 0x%lx: failed!!!\n", chip->port);
  100. return -ENODEV;
  101. }
  102. snd_es1688_dsp_command(chip, 0xc6); /* enable extended mode */
  103. return 0;
  104. }
  105. EXPORT_SYMBOL(snd_es1688_reset);
  106. static int snd_es1688_probe(struct snd_es1688 *chip)
  107. {
  108. unsigned long flags;
  109. unsigned short major, minor, hw;
  110. int i;
  111. /*
  112. * initialization sequence
  113. */
  114. spin_lock_irqsave(&chip->reg_lock, flags); /* Some ESS1688 cards need this */
  115. inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
  116. inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
  117. inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
  118. inb(ES1688P(chip, ENABLE2)); /* ENABLE2 */
  119. inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
  120. inb(ES1688P(chip, ENABLE2)); /* ENABLE2 */
  121. inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
  122. inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
  123. inb(ES1688P(chip, ENABLE2)); /* ENABLE2 */
  124. inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
  125. inb(ES1688P(chip, ENABLE0)); /* ENABLE0 */
  126. if (snd_es1688_reset(chip) < 0) {
  127. snd_printdd("ESS: [0x%lx] reset failed... 0x%x\n", chip->port, inb(ES1688P(chip, READ)));
  128. spin_unlock_irqrestore(&chip->reg_lock, flags);
  129. return -ENODEV;
  130. }
  131. snd_es1688_dsp_command(chip, 0xe7); /* return identification */
  132. for (i = 1000, major = minor = 0; i; i--) {
  133. if (inb(ES1688P(chip, DATA_AVAIL)) & 0x80) {
  134. if (major == 0) {
  135. major = inb(ES1688P(chip, READ));
  136. } else {
  137. minor = inb(ES1688P(chip, READ));
  138. }
  139. }
  140. }
  141. spin_unlock_irqrestore(&chip->reg_lock, flags);
  142. snd_printdd("ESS: [0x%lx] found.. major = 0x%x, minor = 0x%x\n", chip->port, major, minor);
  143. chip->version = (major << 8) | minor;
  144. if (!chip->version)
  145. return -ENODEV; /* probably SB */
  146. hw = ES1688_HW_AUTO;
  147. switch (chip->version & 0xfff0) {
  148. case 0x4880:
  149. snd_printk(KERN_ERR "[0x%lx] ESS: AudioDrive ES488 detected, "
  150. "but driver is in another place\n", chip->port);
  151. return -ENODEV;
  152. case 0x6880:
  153. hw = (chip->version & 0x0f) >= 8 ? ES1688_HW_1688 : ES1688_HW_688;
  154. break;
  155. default:
  156. snd_printk(KERN_ERR "[0x%lx] ESS: unknown AudioDrive chip "
  157. "with version 0x%x (Jazz16 soundcard?)\n",
  158. chip->port, chip->version);
  159. return -ENODEV;
  160. }
  161. spin_lock_irqsave(&chip->reg_lock, flags);
  162. snd_es1688_write(chip, 0xb1, 0x10); /* disable IRQ */
  163. snd_es1688_write(chip, 0xb2, 0x00); /* disable DMA */
  164. spin_unlock_irqrestore(&chip->reg_lock, flags);
  165. /* enable joystick, but disable OPL3 */
  166. spin_lock_irqsave(&chip->mixer_lock, flags);
  167. snd_es1688_mixer_write(chip, 0x40, 0x01);
  168. spin_unlock_irqrestore(&chip->mixer_lock, flags);
  169. return 0;
  170. }
  171. static int snd_es1688_init(struct snd_es1688 * chip, int enable)
  172. {
  173. static int irqs[16] = {-1, -1, 0, -1, -1, 1, -1, 2, -1, 0, 3, -1, -1, -1, -1, -1};
  174. unsigned long flags;
  175. int cfg, irq_bits, dma, dma_bits, tmp, tmp1;
  176. /* ok.. setup MPU-401 port and joystick and OPL3 */
  177. cfg = 0x01; /* enable joystick, but disable OPL3 */
  178. if (enable && chip->mpu_port >= 0x300 && chip->mpu_irq > 0 && chip->hardware != ES1688_HW_688) {
  179. tmp = (chip->mpu_port & 0x0f0) >> 4;
  180. if (tmp <= 3) {
  181. switch (chip->mpu_irq) {
  182. case 9:
  183. tmp1 = 4;
  184. break;
  185. case 5:
  186. tmp1 = 5;
  187. break;
  188. case 7:
  189. tmp1 = 6;
  190. break;
  191. case 10:
  192. tmp1 = 7;
  193. break;
  194. default:
  195. tmp1 = 0;
  196. }
  197. if (tmp1) {
  198. cfg |= (tmp << 3) | (tmp1 << 5);
  199. }
  200. }
  201. }
  202. #if 0
  203. snd_printk(KERN_DEBUG "mpu cfg = 0x%x\n", cfg);
  204. #endif
  205. spin_lock_irqsave(&chip->reg_lock, flags);
  206. snd_es1688_mixer_write(chip, 0x40, cfg);
  207. spin_unlock_irqrestore(&chip->reg_lock, flags);
  208. /* --- */
  209. spin_lock_irqsave(&chip->reg_lock, flags);
  210. snd_es1688_read(chip, 0xb1);
  211. snd_es1688_read(chip, 0xb2);
  212. spin_unlock_irqrestore(&chip->reg_lock, flags);
  213. if (enable) {
  214. cfg = 0xf0; /* enable only DMA counter interrupt */
  215. irq_bits = irqs[chip->irq & 0x0f];
  216. if (irq_bits < 0) {
  217. snd_printk(KERN_ERR "[0x%lx] ESS: bad IRQ %d "
  218. "for ES1688 chip!!\n",
  219. chip->port, chip->irq);
  220. #if 0
  221. irq_bits = 0;
  222. cfg = 0x10;
  223. #endif
  224. return -EINVAL;
  225. }
  226. spin_lock_irqsave(&chip->reg_lock, flags);
  227. snd_es1688_write(chip, 0xb1, cfg | (irq_bits << 2));
  228. spin_unlock_irqrestore(&chip->reg_lock, flags);
  229. cfg = 0xf0; /* extended mode DMA enable */
  230. dma = chip->dma8;
  231. if (dma > 3 || dma == 2) {
  232. snd_printk(KERN_ERR "[0x%lx] ESS: bad DMA channel %d "
  233. "for ES1688 chip!!\n", chip->port, dma);
  234. #if 0
  235. dma_bits = 0;
  236. cfg = 0x00; /* disable all DMA */
  237. #endif
  238. return -EINVAL;
  239. } else {
  240. dma_bits = dma;
  241. if (dma != 3)
  242. dma_bits++;
  243. }
  244. spin_lock_irqsave(&chip->reg_lock, flags);
  245. snd_es1688_write(chip, 0xb2, cfg | (dma_bits << 2));
  246. spin_unlock_irqrestore(&chip->reg_lock, flags);
  247. } else {
  248. spin_lock_irqsave(&chip->reg_lock, flags);
  249. snd_es1688_write(chip, 0xb1, 0x10); /* disable IRQ */
  250. snd_es1688_write(chip, 0xb2, 0x00); /* disable DMA */
  251. spin_unlock_irqrestore(&chip->reg_lock, flags);
  252. }
  253. spin_lock_irqsave(&chip->reg_lock, flags);
  254. snd_es1688_read(chip, 0xb1);
  255. snd_es1688_read(chip, 0xb2);
  256. snd_es1688_reset(chip);
  257. spin_unlock_irqrestore(&chip->reg_lock, flags);
  258. return 0;
  259. }
  260. /*
  261. */
  262. static struct snd_ratnum clocks[2] = {
  263. {
  264. .num = 795444,
  265. .den_min = 1,
  266. .den_max = 128,
  267. .den_step = 1,
  268. },
  269. {
  270. .num = 397722,
  271. .den_min = 1,
  272. .den_max = 128,
  273. .den_step = 1,
  274. }
  275. };
  276. static struct snd_pcm_hw_constraint_ratnums hw_constraints_clocks = {
  277. .nrats = 2,
  278. .rats = clocks,
  279. };
  280. static void snd_es1688_set_rate(struct snd_es1688 *chip, struct snd_pcm_substream *substream)
  281. {
  282. struct snd_pcm_runtime *runtime = substream->runtime;
  283. unsigned int bits, divider;
  284. if (runtime->rate_num == clocks[0].num)
  285. bits = 256 - runtime->rate_den;
  286. else
  287. bits = 128 - runtime->rate_den;
  288. /* set filter register */
  289. divider = 256 - 7160000*20/(8*82*runtime->rate);
  290. /* write result to hardware */
  291. snd_es1688_write(chip, 0xa1, bits);
  292. snd_es1688_write(chip, 0xa2, divider);
  293. }
  294. static int snd_es1688_ioctl(struct snd_pcm_substream *substream,
  295. unsigned int cmd, void *arg)
  296. {
  297. return snd_pcm_lib_ioctl(substream, cmd, arg);
  298. }
  299. static int snd_es1688_trigger(struct snd_es1688 *chip, int cmd, unsigned char value)
  300. {
  301. int val;
  302. if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  303. value = 0x00;
  304. } else if (cmd != SNDRV_PCM_TRIGGER_START) {
  305. return -EINVAL;
  306. }
  307. spin_lock(&chip->reg_lock);
  308. chip->trigger_value = value;
  309. val = snd_es1688_read(chip, 0xb8);
  310. if ((val < 0) || (val & 0x0f) == value) {
  311. spin_unlock(&chip->reg_lock);
  312. return -EINVAL; /* something is wrong */
  313. }
  314. #if 0
  315. printk(KERN_DEBUG "trigger: val = 0x%x, value = 0x%x\n", val, value);
  316. printk(KERN_DEBUG "trigger: pointer = 0x%x\n",
  317. snd_dma_pointer(chip->dma8, chip->dma_size));
  318. #endif
  319. snd_es1688_write(chip, 0xb8, (val & 0xf0) | value);
  320. spin_unlock(&chip->reg_lock);
  321. return 0;
  322. }
  323. static int snd_es1688_hw_params(struct snd_pcm_substream *substream,
  324. struct snd_pcm_hw_params *hw_params)
  325. {
  326. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  327. }
  328. static int snd_es1688_hw_free(struct snd_pcm_substream *substream)
  329. {
  330. return snd_pcm_lib_free_pages(substream);
  331. }
  332. static int snd_es1688_playback_prepare(struct snd_pcm_substream *substream)
  333. {
  334. unsigned long flags;
  335. struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
  336. struct snd_pcm_runtime *runtime = substream->runtime;
  337. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  338. unsigned int count = snd_pcm_lib_period_bytes(substream);
  339. chip->dma_size = size;
  340. spin_lock_irqsave(&chip->reg_lock, flags);
  341. snd_es1688_reset(chip);
  342. snd_es1688_set_rate(chip, substream);
  343. snd_es1688_write(chip, 0xb8, 4); /* auto init DMA mode */
  344. snd_es1688_write(chip, 0xa8, (snd_es1688_read(chip, 0xa8) & ~0x03) | (3 - runtime->channels));
  345. snd_es1688_write(chip, 0xb9, 2); /* demand mode (4 bytes/request) */
  346. if (runtime->channels == 1) {
  347. if (snd_pcm_format_width(runtime->format) == 8) {
  348. /* 8. bit mono */
  349. snd_es1688_write(chip, 0xb6, 0x80);
  350. snd_es1688_write(chip, 0xb7, 0x51);
  351. snd_es1688_write(chip, 0xb7, 0xd0);
  352. } else {
  353. /* 16. bit mono */
  354. snd_es1688_write(chip, 0xb6, 0x00);
  355. snd_es1688_write(chip, 0xb7, 0x71);
  356. snd_es1688_write(chip, 0xb7, 0xf4);
  357. }
  358. } else {
  359. if (snd_pcm_format_width(runtime->format) == 8) {
  360. /* 8. bit stereo */
  361. snd_es1688_write(chip, 0xb6, 0x80);
  362. snd_es1688_write(chip, 0xb7, 0x51);
  363. snd_es1688_write(chip, 0xb7, 0x98);
  364. } else {
  365. /* 16. bit stereo */
  366. snd_es1688_write(chip, 0xb6, 0x00);
  367. snd_es1688_write(chip, 0xb7, 0x71);
  368. snd_es1688_write(chip, 0xb7, 0xbc);
  369. }
  370. }
  371. snd_es1688_write(chip, 0xb1, (snd_es1688_read(chip, 0xb1) & 0x0f) | 0x50);
  372. snd_es1688_write(chip, 0xb2, (snd_es1688_read(chip, 0xb2) & 0x0f) | 0x50);
  373. snd_es1688_dsp_command(chip, ES1688_DSP_CMD_SPKON);
  374. spin_unlock_irqrestore(&chip->reg_lock, flags);
  375. /* --- */
  376. count = -count;
  377. snd_dma_program(chip->dma8, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
  378. spin_lock_irqsave(&chip->reg_lock, flags);
  379. snd_es1688_write(chip, 0xa4, (unsigned char) count);
  380. snd_es1688_write(chip, 0xa5, (unsigned char) (count >> 8));
  381. spin_unlock_irqrestore(&chip->reg_lock, flags);
  382. return 0;
  383. }
  384. static int snd_es1688_playback_trigger(struct snd_pcm_substream *substream,
  385. int cmd)
  386. {
  387. struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
  388. return snd_es1688_trigger(chip, cmd, 0x05);
  389. }
  390. static int snd_es1688_capture_prepare(struct snd_pcm_substream *substream)
  391. {
  392. unsigned long flags;
  393. struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
  394. struct snd_pcm_runtime *runtime = substream->runtime;
  395. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  396. unsigned int count = snd_pcm_lib_period_bytes(substream);
  397. chip->dma_size = size;
  398. spin_lock_irqsave(&chip->reg_lock, flags);
  399. snd_es1688_reset(chip);
  400. snd_es1688_set_rate(chip, substream);
  401. snd_es1688_dsp_command(chip, ES1688_DSP_CMD_SPKOFF);
  402. snd_es1688_write(chip, 0xb8, 0x0e); /* auto init DMA mode */
  403. snd_es1688_write(chip, 0xa8, (snd_es1688_read(chip, 0xa8) & ~0x03) | (3 - runtime->channels));
  404. snd_es1688_write(chip, 0xb9, 2); /* demand mode (4 bytes/request) */
  405. if (runtime->channels == 1) {
  406. if (snd_pcm_format_width(runtime->format) == 8) {
  407. /* 8. bit mono */
  408. snd_es1688_write(chip, 0xb7, 0x51);
  409. snd_es1688_write(chip, 0xb7, 0xd0);
  410. } else {
  411. /* 16. bit mono */
  412. snd_es1688_write(chip, 0xb7, 0x71);
  413. snd_es1688_write(chip, 0xb7, 0xf4);
  414. }
  415. } else {
  416. if (snd_pcm_format_width(runtime->format) == 8) {
  417. /* 8. bit stereo */
  418. snd_es1688_write(chip, 0xb7, 0x51);
  419. snd_es1688_write(chip, 0xb7, 0x98);
  420. } else {
  421. /* 16. bit stereo */
  422. snd_es1688_write(chip, 0xb7, 0x71);
  423. snd_es1688_write(chip, 0xb7, 0xbc);
  424. }
  425. }
  426. snd_es1688_write(chip, 0xb1, (snd_es1688_read(chip, 0xb1) & 0x0f) | 0x50);
  427. snd_es1688_write(chip, 0xb2, (snd_es1688_read(chip, 0xb2) & 0x0f) | 0x50);
  428. spin_unlock_irqrestore(&chip->reg_lock, flags);
  429. /* --- */
  430. count = -count;
  431. snd_dma_program(chip->dma8, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
  432. spin_lock_irqsave(&chip->reg_lock, flags);
  433. snd_es1688_write(chip, 0xa4, (unsigned char) count);
  434. snd_es1688_write(chip, 0xa5, (unsigned char) (count >> 8));
  435. spin_unlock_irqrestore(&chip->reg_lock, flags);
  436. return 0;
  437. }
  438. static int snd_es1688_capture_trigger(struct snd_pcm_substream *substream,
  439. int cmd)
  440. {
  441. struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
  442. return snd_es1688_trigger(chip, cmd, 0x0f);
  443. }
  444. static irqreturn_t snd_es1688_interrupt(int irq, void *dev_id)
  445. {
  446. struct snd_es1688 *chip = dev_id;
  447. if (chip->trigger_value == 0x05) /* ok.. playback is active */
  448. snd_pcm_period_elapsed(chip->playback_substream);
  449. if (chip->trigger_value == 0x0f) /* ok.. capture is active */
  450. snd_pcm_period_elapsed(chip->capture_substream);
  451. inb(ES1688P(chip, DATA_AVAIL)); /* ack interrupt */
  452. return IRQ_HANDLED;
  453. }
  454. static snd_pcm_uframes_t snd_es1688_playback_pointer(struct snd_pcm_substream *substream)
  455. {
  456. struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
  457. size_t ptr;
  458. if (chip->trigger_value != 0x05)
  459. return 0;
  460. ptr = snd_dma_pointer(chip->dma8, chip->dma_size);
  461. return bytes_to_frames(substream->runtime, ptr);
  462. }
  463. static snd_pcm_uframes_t snd_es1688_capture_pointer(struct snd_pcm_substream *substream)
  464. {
  465. struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
  466. size_t ptr;
  467. if (chip->trigger_value != 0x0f)
  468. return 0;
  469. ptr = snd_dma_pointer(chip->dma8, chip->dma_size);
  470. return bytes_to_frames(substream->runtime, ptr);
  471. }
  472. /*
  473. */
  474. static struct snd_pcm_hardware snd_es1688_playback =
  475. {
  476. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  477. SNDRV_PCM_INFO_MMAP_VALID),
  478. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  479. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  480. .rate_min = 4000,
  481. .rate_max = 48000,
  482. .channels_min = 1,
  483. .channels_max = 2,
  484. .buffer_bytes_max = 65536,
  485. .period_bytes_min = 64,
  486. .period_bytes_max = 65536,
  487. .periods_min = 1,
  488. .periods_max = 1024,
  489. .fifo_size = 0,
  490. };
  491. static struct snd_pcm_hardware snd_es1688_capture =
  492. {
  493. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  494. SNDRV_PCM_INFO_MMAP_VALID),
  495. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  496. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  497. .rate_min = 4000,
  498. .rate_max = 48000,
  499. .channels_min = 1,
  500. .channels_max = 2,
  501. .buffer_bytes_max = 65536,
  502. .period_bytes_min = 64,
  503. .period_bytes_max = 65536,
  504. .periods_min = 1,
  505. .periods_max = 1024,
  506. .fifo_size = 0,
  507. };
  508. /*
  509. */
  510. static int snd_es1688_playback_open(struct snd_pcm_substream *substream)
  511. {
  512. struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
  513. struct snd_pcm_runtime *runtime = substream->runtime;
  514. if (chip->capture_substream != NULL)
  515. return -EAGAIN;
  516. chip->playback_substream = substream;
  517. runtime->hw = snd_es1688_playback;
  518. snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  519. &hw_constraints_clocks);
  520. return 0;
  521. }
  522. static int snd_es1688_capture_open(struct snd_pcm_substream *substream)
  523. {
  524. struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
  525. struct snd_pcm_runtime *runtime = substream->runtime;
  526. if (chip->playback_substream != NULL)
  527. return -EAGAIN;
  528. chip->capture_substream = substream;
  529. runtime->hw = snd_es1688_capture;
  530. snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  531. &hw_constraints_clocks);
  532. return 0;
  533. }
  534. static int snd_es1688_playback_close(struct snd_pcm_substream *substream)
  535. {
  536. struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
  537. chip->playback_substream = NULL;
  538. return 0;
  539. }
  540. static int snd_es1688_capture_close(struct snd_pcm_substream *substream)
  541. {
  542. struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
  543. chip->capture_substream = NULL;
  544. return 0;
  545. }
  546. static int snd_es1688_free(struct snd_es1688 *chip)
  547. {
  548. if (chip->hardware != ES1688_HW_UNDEF)
  549. snd_es1688_init(chip, 0);
  550. release_and_free_resource(chip->res_port);
  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. chip->hardware = ES1688_HW_UNDEF;
  588. chip->res_port = request_region(port + 4, 12, "ES1688");
  589. if (chip->res_port == NULL) {
  590. snd_printk(KERN_ERR "es1688: can't grab port 0x%lx\n", port + 4);
  591. err = -EBUSY;
  592. goto exit;
  593. }
  594. err = request_irq(irq, snd_es1688_interrupt, 0, "ES1688", (void *) chip);
  595. if (err < 0) {
  596. snd_printk(KERN_ERR "es1688: can't grab IRQ %d\n", irq);
  597. goto exit;
  598. }
  599. chip->irq = irq;
  600. err = request_dma(dma8, "ES1688");
  601. if (err < 0) {
  602. snd_printk(KERN_ERR "es1688: can't grab DMA8 %d\n", dma8);
  603. goto exit;
  604. }
  605. chip->dma8 = dma8;
  606. spin_lock_init(&chip->reg_lock);
  607. spin_lock_init(&chip->mixer_lock);
  608. chip->port = port;
  609. mpu_port &= ~0x000f;
  610. if (mpu_port < 0x300 || mpu_port > 0x330)
  611. mpu_port = 0;
  612. chip->mpu_port = mpu_port;
  613. chip->mpu_irq = mpu_irq;
  614. chip->hardware = hardware;
  615. err = snd_es1688_probe(chip);
  616. if (err < 0)
  617. goto exit;
  618. err = snd_es1688_init(chip, 1);
  619. if (err < 0)
  620. goto exit;
  621. /* Register device */
  622. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  623. exit:
  624. if (err)
  625. snd_es1688_free(chip);
  626. return err;
  627. }
  628. static struct snd_pcm_ops snd_es1688_playback_ops = {
  629. .open = snd_es1688_playback_open,
  630. .close = snd_es1688_playback_close,
  631. .ioctl = snd_es1688_ioctl,
  632. .hw_params = snd_es1688_hw_params,
  633. .hw_free = snd_es1688_hw_free,
  634. .prepare = snd_es1688_playback_prepare,
  635. .trigger = snd_es1688_playback_trigger,
  636. .pointer = snd_es1688_playback_pointer,
  637. };
  638. static struct snd_pcm_ops snd_es1688_capture_ops = {
  639. .open = snd_es1688_capture_open,
  640. .close = snd_es1688_capture_close,
  641. .ioctl = snd_es1688_ioctl,
  642. .hw_params = snd_es1688_hw_params,
  643. .hw_free = snd_es1688_hw_free,
  644. .prepare = snd_es1688_capture_prepare,
  645. .trigger = snd_es1688_capture_trigger,
  646. .pointer = snd_es1688_capture_pointer,
  647. };
  648. int snd_es1688_pcm(struct snd_card *card, struct snd_es1688 *chip, int device)
  649. {
  650. struct snd_pcm *pcm;
  651. int err;
  652. err = snd_pcm_new(card, "ESx688", device, 1, 1, &pcm);
  653. if (err < 0)
  654. return err;
  655. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es1688_playback_ops);
  656. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es1688_capture_ops);
  657. pcm->private_data = chip;
  658. pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
  659. sprintf(pcm->name, snd_es1688_chip_id(chip));
  660. chip->pcm = pcm;
  661. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  662. snd_dma_isa_data(),
  663. 64*1024, 64*1024);
  664. return 0;
  665. }
  666. /*
  667. * MIXER part
  668. */
  669. static int snd_es1688_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  670. {
  671. static const char * const texts[8] = {
  672. "Mic", "Mic Master", "CD", "AOUT",
  673. "Mic1", "Mix", "Line", "Master"
  674. };
  675. return snd_ctl_enum_info(uinfo, 1, 8, texts);
  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)