es1688_lib.c 30 KB

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