es1688_lib.c 30 KB

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