es1688_lib.c 29 KB

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