es1688_lib.c 29 KB

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