es1688_lib.c 30 KB

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