ad1848_lib.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  3. * Routines for control of AD1848/AD1847/CS4248
  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. #define SNDRV_MAIN_OBJECT_FILE
  22. #include <sound/driver.h>
  23. #include <linux/delay.h>
  24. #include <linux/init.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/slab.h>
  27. #include <linux/ioport.h>
  28. #include <sound/core.h>
  29. #include <sound/ad1848.h>
  30. #include <sound/control.h>
  31. #include <sound/tlv.h>
  32. #include <sound/pcm_params.h>
  33. #include <asm/io.h>
  34. #include <asm/dma.h>
  35. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  36. MODULE_DESCRIPTION("Routines for control of AD1848/AD1847/CS4248");
  37. MODULE_LICENSE("GPL");
  38. #if 0
  39. #define SNDRV_DEBUG_MCE
  40. #endif
  41. /*
  42. * Some variables
  43. */
  44. static unsigned char freq_bits[14] = {
  45. /* 5510 */ 0x00 | AD1848_XTAL2,
  46. /* 6620 */ 0x0E | AD1848_XTAL2,
  47. /* 8000 */ 0x00 | AD1848_XTAL1,
  48. /* 9600 */ 0x0E | AD1848_XTAL1,
  49. /* 11025 */ 0x02 | AD1848_XTAL2,
  50. /* 16000 */ 0x02 | AD1848_XTAL1,
  51. /* 18900 */ 0x04 | AD1848_XTAL2,
  52. /* 22050 */ 0x06 | AD1848_XTAL2,
  53. /* 27042 */ 0x04 | AD1848_XTAL1,
  54. /* 32000 */ 0x06 | AD1848_XTAL1,
  55. /* 33075 */ 0x0C | AD1848_XTAL2,
  56. /* 37800 */ 0x08 | AD1848_XTAL2,
  57. /* 44100 */ 0x0A | AD1848_XTAL2,
  58. /* 48000 */ 0x0C | AD1848_XTAL1
  59. };
  60. static unsigned int rates[14] = {
  61. 5510, 6620, 8000, 9600, 11025, 16000, 18900, 22050,
  62. 27042, 32000, 33075, 37800, 44100, 48000
  63. };
  64. static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
  65. .count = ARRAY_SIZE(rates),
  66. .list = rates,
  67. .mask = 0,
  68. };
  69. static unsigned char snd_ad1848_original_image[16] =
  70. {
  71. 0x00, /* 00 - lic */
  72. 0x00, /* 01 - ric */
  73. 0x9f, /* 02 - la1ic */
  74. 0x9f, /* 03 - ra1ic */
  75. 0x9f, /* 04 - la2ic */
  76. 0x9f, /* 05 - ra2ic */
  77. 0xbf, /* 06 - loc */
  78. 0xbf, /* 07 - roc */
  79. 0x20, /* 08 - dfr */
  80. AD1848_AUTOCALIB, /* 09 - ic */
  81. 0x00, /* 0a - pc */
  82. 0x00, /* 0b - ti */
  83. 0x00, /* 0c - mi */
  84. 0x00, /* 0d - lbc */
  85. 0x00, /* 0e - dru */
  86. 0x00, /* 0f - drl */
  87. };
  88. /*
  89. * Basic I/O functions
  90. */
  91. static void snd_ad1848_wait(struct snd_ad1848 *chip)
  92. {
  93. int timeout;
  94. for (timeout = 250; timeout > 0; timeout--) {
  95. if ((inb(AD1848P(chip, REGSEL)) & AD1848_INIT) == 0)
  96. break;
  97. udelay(100);
  98. }
  99. }
  100. void snd_ad1848_out(struct snd_ad1848 *chip,
  101. unsigned char reg,
  102. unsigned char value)
  103. {
  104. snd_ad1848_wait(chip);
  105. #ifdef CONFIG_SND_DEBUG
  106. if (inb(AD1848P(chip, REGSEL)) & AD1848_INIT)
  107. snd_printk(KERN_WARNING "auto calibration time out - "
  108. "reg = 0x%x, value = 0x%x\n", reg, value);
  109. #endif
  110. outb(chip->mce_bit | reg, AD1848P(chip, REGSEL));
  111. outb(chip->image[reg] = value, AD1848P(chip, REG));
  112. mb();
  113. snd_printdd("codec out - reg 0x%x = 0x%x\n",
  114. chip->mce_bit | reg, value);
  115. }
  116. EXPORT_SYMBOL(snd_ad1848_out);
  117. static void snd_ad1848_dout(struct snd_ad1848 *chip,
  118. unsigned char reg, unsigned char value)
  119. {
  120. snd_ad1848_wait(chip);
  121. outb(chip->mce_bit | reg, AD1848P(chip, REGSEL));
  122. outb(value, AD1848P(chip, REG));
  123. mb();
  124. }
  125. static unsigned char snd_ad1848_in(struct snd_ad1848 *chip, unsigned char reg)
  126. {
  127. snd_ad1848_wait(chip);
  128. #ifdef CONFIG_SND_DEBUG
  129. if (inb(AD1848P(chip, REGSEL)) & AD1848_INIT)
  130. snd_printk(KERN_WARNING "auto calibration time out - "
  131. "reg = 0x%x\n", reg);
  132. #endif
  133. outb(chip->mce_bit | reg, AD1848P(chip, REGSEL));
  134. mb();
  135. return inb(AD1848P(chip, REG));
  136. }
  137. #if 0
  138. static void snd_ad1848_debug(struct snd_ad1848 *chip)
  139. {
  140. printk("AD1848 REGS: INDEX = 0x%02x ", inb(AD1848P(chip, REGSEL)));
  141. printk(" STATUS = 0x%02x\n", inb(AD1848P(chip, STATUS)));
  142. printk(" 0x00: left input = 0x%02x ", snd_ad1848_in(chip, 0x00));
  143. printk(" 0x08: playback format = 0x%02x\n", snd_ad1848_in(chip, 0x08));
  144. printk(" 0x01: right input = 0x%02x ", snd_ad1848_in(chip, 0x01));
  145. printk(" 0x09: iface (CFIG 1) = 0x%02x\n", snd_ad1848_in(chip, 0x09));
  146. printk(" 0x02: AUXA left = 0x%02x ", snd_ad1848_in(chip, 0x02));
  147. printk(" 0x0a: pin control = 0x%02x\n", snd_ad1848_in(chip, 0x0a));
  148. printk(" 0x03: AUXA right = 0x%02x ", snd_ad1848_in(chip, 0x03));
  149. printk(" 0x0b: init & status = 0x%02x\n", snd_ad1848_in(chip, 0x0b));
  150. printk(" 0x04: AUXB left = 0x%02x ", snd_ad1848_in(chip, 0x04));
  151. printk(" 0x0c: revision & mode = 0x%02x\n", snd_ad1848_in(chip, 0x0c));
  152. printk(" 0x05: AUXB right = 0x%02x ", snd_ad1848_in(chip, 0x05));
  153. printk(" 0x0d: loopback = 0x%02x\n", snd_ad1848_in(chip, 0x0d));
  154. printk(" 0x06: left output = 0x%02x ", snd_ad1848_in(chip, 0x06));
  155. printk(" 0x0e: data upr count = 0x%02x\n", snd_ad1848_in(chip, 0x0e));
  156. printk(" 0x07: right output = 0x%02x ", snd_ad1848_in(chip, 0x07));
  157. printk(" 0x0f: data lwr count = 0x%02x\n", snd_ad1848_in(chip, 0x0f));
  158. }
  159. #endif
  160. /*
  161. * AD1848 detection / MCE routines
  162. */
  163. static void snd_ad1848_mce_up(struct snd_ad1848 *chip)
  164. {
  165. unsigned long flags;
  166. int timeout;
  167. snd_ad1848_wait(chip);
  168. #ifdef CONFIG_SND_DEBUG
  169. if (inb(AD1848P(chip, REGSEL)) & AD1848_INIT)
  170. snd_printk(KERN_WARNING "mce_up - auto calibration time out (0)\n");
  171. #endif
  172. spin_lock_irqsave(&chip->reg_lock, flags);
  173. chip->mce_bit |= AD1848_MCE;
  174. timeout = inb(AD1848P(chip, REGSEL));
  175. if (timeout == 0x80)
  176. snd_printk(KERN_WARNING "mce_up [0x%lx]: serious init problem - codec still busy\n", chip->port);
  177. if (!(timeout & AD1848_MCE))
  178. outb(chip->mce_bit | (timeout & 0x1f), AD1848P(chip, REGSEL));
  179. spin_unlock_irqrestore(&chip->reg_lock, flags);
  180. }
  181. static void snd_ad1848_mce_down(struct snd_ad1848 *chip)
  182. {
  183. unsigned long flags, timeout;
  184. int reg;
  185. spin_lock_irqsave(&chip->reg_lock, flags);
  186. for (timeout = 5; timeout > 0; timeout--)
  187. inb(AD1848P(chip, REGSEL));
  188. /* end of cleanup sequence */
  189. for (timeout = 12000; timeout > 0 && (inb(AD1848P(chip, REGSEL)) & AD1848_INIT); timeout--)
  190. udelay(100);
  191. snd_printdd("(1) timeout = %d\n", timeout);
  192. #ifdef CONFIG_SND_DEBUG
  193. if (inb(AD1848P(chip, REGSEL)) & AD1848_INIT)
  194. snd_printk(KERN_WARNING "mce_down [0x%lx] - auto calibration time out (0)\n", AD1848P(chip, REGSEL));
  195. #endif
  196. chip->mce_bit &= ~AD1848_MCE;
  197. reg = inb(AD1848P(chip, REGSEL));
  198. outb(chip->mce_bit | (reg & 0x1f), AD1848P(chip, REGSEL));
  199. if (reg == 0x80)
  200. snd_printk(KERN_WARNING "mce_down [0x%lx]: serious init problem - codec still busy\n", chip->port);
  201. if ((reg & AD1848_MCE) == 0) {
  202. spin_unlock_irqrestore(&chip->reg_lock, flags);
  203. return;
  204. }
  205. /*
  206. * Wait for auto-calibration (AC) process to finish, i.e. ACI to go low.
  207. * It may take up to 5 sample periods (at most 907 us @ 5.5125 kHz) for
  208. * the process to _start_, so it is important to wait at least that long
  209. * before checking. Otherwise we might think AC has finished when it
  210. * has in fact not begun. It could take 128 (no AC) or 384 (AC) cycles
  211. * for ACI to drop. This gives a wait of at most 70 ms with a more
  212. * typical value of 3-9 ms.
  213. */
  214. timeout = jiffies + msecs_to_jiffies(250);
  215. do {
  216. spin_unlock_irqrestore(&chip->reg_lock, flags);
  217. msleep(1);
  218. spin_lock_irqsave(&chip->reg_lock, flags);
  219. reg = snd_ad1848_in(chip, AD1848_TEST_INIT) &
  220. AD1848_CALIB_IN_PROGRESS;
  221. } while (reg && time_before(jiffies, timeout));
  222. spin_unlock_irqrestore(&chip->reg_lock, flags);
  223. if (reg)
  224. snd_printk(KERN_ERR
  225. "mce_down - auto calibration time out (2)\n");
  226. snd_printdd("(4) jiffies = %lu\n", jiffies);
  227. snd_printd("mce_down - exit = 0x%x\n", inb(AD1848P(chip, REGSEL)));
  228. }
  229. static unsigned int snd_ad1848_get_count(unsigned char format,
  230. unsigned int size)
  231. {
  232. switch (format & 0xe0) {
  233. case AD1848_LINEAR_16:
  234. size >>= 1;
  235. break;
  236. }
  237. if (format & AD1848_STEREO)
  238. size >>= 1;
  239. return size;
  240. }
  241. static int snd_ad1848_trigger(struct snd_ad1848 *chip, unsigned char what,
  242. int channel, int cmd)
  243. {
  244. int result = 0;
  245. #if 0
  246. printk("codec trigger!!! - what = %i, enable = %i, status = 0x%x\n", what, enable, inb(AD1848P(card, STATUS)));
  247. #endif
  248. spin_lock(&chip->reg_lock);
  249. if (cmd == SNDRV_PCM_TRIGGER_START) {
  250. if (chip->image[AD1848_IFACE_CTRL] & what) {
  251. spin_unlock(&chip->reg_lock);
  252. return 0;
  253. }
  254. snd_ad1848_out(chip, AD1848_IFACE_CTRL, chip->image[AD1848_IFACE_CTRL] |= what);
  255. chip->mode |= AD1848_MODE_RUNNING;
  256. } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  257. if (!(chip->image[AD1848_IFACE_CTRL] & what)) {
  258. spin_unlock(&chip->reg_lock);
  259. return 0;
  260. }
  261. snd_ad1848_out(chip, AD1848_IFACE_CTRL, chip->image[AD1848_IFACE_CTRL] &= ~what);
  262. chip->mode &= ~AD1848_MODE_RUNNING;
  263. } else {
  264. result = -EINVAL;
  265. }
  266. spin_unlock(&chip->reg_lock);
  267. return result;
  268. }
  269. /*
  270. * CODEC I/O
  271. */
  272. static unsigned char snd_ad1848_get_rate(unsigned int rate)
  273. {
  274. int i;
  275. for (i = 0; i < ARRAY_SIZE(rates); i++)
  276. if (rate == rates[i])
  277. return freq_bits[i];
  278. snd_BUG();
  279. return freq_bits[ARRAY_SIZE(rates) - 1];
  280. }
  281. static int snd_ad1848_ioctl(struct snd_pcm_substream *substream,
  282. unsigned int cmd, void *arg)
  283. {
  284. return snd_pcm_lib_ioctl(substream, cmd, arg);
  285. }
  286. static unsigned char snd_ad1848_get_format(int format, int channels)
  287. {
  288. unsigned char rformat;
  289. rformat = AD1848_LINEAR_8;
  290. switch (format) {
  291. case SNDRV_PCM_FORMAT_A_LAW: rformat = AD1848_ALAW_8; break;
  292. case SNDRV_PCM_FORMAT_MU_LAW: rformat = AD1848_ULAW_8; break;
  293. case SNDRV_PCM_FORMAT_S16_LE: rformat = AD1848_LINEAR_16; break;
  294. }
  295. if (channels > 1)
  296. rformat |= AD1848_STEREO;
  297. #if 0
  298. snd_printk("get_format: 0x%x (mode=0x%x)\n", format, mode);
  299. #endif
  300. return rformat;
  301. }
  302. static void snd_ad1848_calibrate_mute(struct snd_ad1848 *chip, int mute)
  303. {
  304. unsigned long flags;
  305. mute = mute ? 1 : 0;
  306. spin_lock_irqsave(&chip->reg_lock, flags);
  307. if (chip->calibrate_mute == mute) {
  308. spin_unlock_irqrestore(&chip->reg_lock, flags);
  309. return;
  310. }
  311. if (!mute) {
  312. snd_ad1848_dout(chip, AD1848_LEFT_INPUT, chip->image[AD1848_LEFT_INPUT]);
  313. snd_ad1848_dout(chip, AD1848_RIGHT_INPUT, chip->image[AD1848_RIGHT_INPUT]);
  314. }
  315. snd_ad1848_dout(chip, AD1848_AUX1_LEFT_INPUT, mute ? 0x80 : chip->image[AD1848_AUX1_LEFT_INPUT]);
  316. snd_ad1848_dout(chip, AD1848_AUX1_RIGHT_INPUT, mute ? 0x80 : chip->image[AD1848_AUX1_RIGHT_INPUT]);
  317. snd_ad1848_dout(chip, AD1848_AUX2_LEFT_INPUT, mute ? 0x80 : chip->image[AD1848_AUX2_LEFT_INPUT]);
  318. snd_ad1848_dout(chip, AD1848_AUX2_RIGHT_INPUT, mute ? 0x80 : chip->image[AD1848_AUX2_RIGHT_INPUT]);
  319. snd_ad1848_dout(chip, AD1848_LEFT_OUTPUT, mute ? 0x80 : chip->image[AD1848_LEFT_OUTPUT]);
  320. snd_ad1848_dout(chip, AD1848_RIGHT_OUTPUT, mute ? 0x80 : chip->image[AD1848_RIGHT_OUTPUT]);
  321. chip->calibrate_mute = mute;
  322. spin_unlock_irqrestore(&chip->reg_lock, flags);
  323. }
  324. static void snd_ad1848_set_data_format(struct snd_ad1848 *chip, struct snd_pcm_hw_params *hw_params)
  325. {
  326. if (hw_params == NULL) {
  327. chip->image[AD1848_DATA_FORMAT] = 0x20;
  328. } else {
  329. chip->image[AD1848_DATA_FORMAT] =
  330. snd_ad1848_get_format(params_format(hw_params), params_channels(hw_params)) |
  331. snd_ad1848_get_rate(params_rate(hw_params));
  332. }
  333. // snd_printk(">>> pmode = 0x%x, dfr = 0x%x\n", pstr->mode, chip->image[AD1848_DATA_FORMAT]);
  334. }
  335. static int snd_ad1848_open(struct snd_ad1848 *chip, unsigned int mode)
  336. {
  337. unsigned long flags;
  338. if (chip->mode & AD1848_MODE_OPEN)
  339. return -EAGAIN;
  340. snd_ad1848_mce_down(chip);
  341. #ifdef SNDRV_DEBUG_MCE
  342. snd_printk("open: (1)\n");
  343. #endif
  344. snd_ad1848_mce_up(chip);
  345. spin_lock_irqsave(&chip->reg_lock, flags);
  346. chip->image[AD1848_IFACE_CTRL] &= ~(AD1848_PLAYBACK_ENABLE | AD1848_PLAYBACK_PIO |
  347. AD1848_CAPTURE_ENABLE | AD1848_CAPTURE_PIO |
  348. AD1848_CALIB_MODE);
  349. chip->image[AD1848_IFACE_CTRL] |= AD1848_AUTOCALIB;
  350. snd_ad1848_out(chip, AD1848_IFACE_CTRL, chip->image[AD1848_IFACE_CTRL]);
  351. spin_unlock_irqrestore(&chip->reg_lock, flags);
  352. snd_ad1848_mce_down(chip);
  353. #ifdef SNDRV_DEBUG_MCE
  354. snd_printk("open: (2)\n");
  355. #endif
  356. snd_ad1848_set_data_format(chip, NULL);
  357. snd_ad1848_mce_up(chip);
  358. spin_lock_irqsave(&chip->reg_lock, flags);
  359. snd_ad1848_out(chip, AD1848_DATA_FORMAT, chip->image[AD1848_DATA_FORMAT]);
  360. spin_unlock_irqrestore(&chip->reg_lock, flags);
  361. snd_ad1848_mce_down(chip);
  362. #ifdef SNDRV_DEBUG_MCE
  363. snd_printk("open: (3)\n");
  364. #endif
  365. /* ok. now enable and ack CODEC IRQ */
  366. spin_lock_irqsave(&chip->reg_lock, flags);
  367. outb(0, AD1848P(chip, STATUS)); /* clear IRQ */
  368. outb(0, AD1848P(chip, STATUS)); /* clear IRQ */
  369. chip->image[AD1848_PIN_CTRL] |= AD1848_IRQ_ENABLE;
  370. snd_ad1848_out(chip, AD1848_PIN_CTRL, chip->image[AD1848_PIN_CTRL]);
  371. spin_unlock_irqrestore(&chip->reg_lock, flags);
  372. chip->mode = mode;
  373. return 0;
  374. }
  375. static void snd_ad1848_close(struct snd_ad1848 *chip)
  376. {
  377. unsigned long flags;
  378. if (!chip->mode)
  379. return;
  380. /* disable IRQ */
  381. spin_lock_irqsave(&chip->reg_lock, flags);
  382. outb(0, AD1848P(chip, STATUS)); /* clear IRQ */
  383. outb(0, AD1848P(chip, STATUS)); /* clear IRQ */
  384. chip->image[AD1848_PIN_CTRL] &= ~AD1848_IRQ_ENABLE;
  385. snd_ad1848_out(chip, AD1848_PIN_CTRL, chip->image[AD1848_PIN_CTRL]);
  386. spin_unlock_irqrestore(&chip->reg_lock, flags);
  387. /* now disable capture & playback */
  388. snd_ad1848_mce_up(chip);
  389. spin_lock_irqsave(&chip->reg_lock, flags);
  390. chip->image[AD1848_IFACE_CTRL] &= ~(AD1848_PLAYBACK_ENABLE | AD1848_PLAYBACK_PIO |
  391. AD1848_CAPTURE_ENABLE | AD1848_CAPTURE_PIO);
  392. snd_ad1848_out(chip, AD1848_IFACE_CTRL, chip->image[AD1848_IFACE_CTRL]);
  393. spin_unlock_irqrestore(&chip->reg_lock, flags);
  394. snd_ad1848_mce_down(chip);
  395. /* clear IRQ again */
  396. spin_lock_irqsave(&chip->reg_lock, flags);
  397. outb(0, AD1848P(chip, STATUS)); /* clear IRQ */
  398. outb(0, AD1848P(chip, STATUS)); /* clear IRQ */
  399. spin_unlock_irqrestore(&chip->reg_lock, flags);
  400. chip->mode = 0;
  401. }
  402. /*
  403. * ok.. exported functions..
  404. */
  405. static int snd_ad1848_playback_trigger(struct snd_pcm_substream *substream,
  406. int cmd)
  407. {
  408. struct snd_ad1848 *chip = snd_pcm_substream_chip(substream);
  409. return snd_ad1848_trigger(chip, AD1848_PLAYBACK_ENABLE, SNDRV_PCM_STREAM_PLAYBACK, cmd);
  410. }
  411. static int snd_ad1848_capture_trigger(struct snd_pcm_substream *substream,
  412. int cmd)
  413. {
  414. struct snd_ad1848 *chip = snd_pcm_substream_chip(substream);
  415. return snd_ad1848_trigger(chip, AD1848_CAPTURE_ENABLE, SNDRV_PCM_STREAM_CAPTURE, cmd);
  416. }
  417. static int snd_ad1848_playback_hw_params(struct snd_pcm_substream *substream,
  418. struct snd_pcm_hw_params *hw_params)
  419. {
  420. struct snd_ad1848 *chip = snd_pcm_substream_chip(substream);
  421. unsigned long flags;
  422. int err;
  423. if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
  424. return err;
  425. snd_ad1848_calibrate_mute(chip, 1);
  426. snd_ad1848_set_data_format(chip, hw_params);
  427. snd_ad1848_mce_up(chip);
  428. spin_lock_irqsave(&chip->reg_lock, flags);
  429. snd_ad1848_out(chip, AD1848_DATA_FORMAT, chip->image[AD1848_DATA_FORMAT]);
  430. spin_unlock_irqrestore(&chip->reg_lock, flags);
  431. snd_ad1848_mce_down(chip);
  432. snd_ad1848_calibrate_mute(chip, 0);
  433. return 0;
  434. }
  435. static int snd_ad1848_playback_hw_free(struct snd_pcm_substream *substream)
  436. {
  437. return snd_pcm_lib_free_pages(substream);
  438. }
  439. static int snd_ad1848_playback_prepare(struct snd_pcm_substream *substream)
  440. {
  441. struct snd_ad1848 *chip = snd_pcm_substream_chip(substream);
  442. struct snd_pcm_runtime *runtime = substream->runtime;
  443. unsigned long flags;
  444. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  445. unsigned int count = snd_pcm_lib_period_bytes(substream);
  446. chip->dma_size = size;
  447. chip->image[AD1848_IFACE_CTRL] &= ~(AD1848_PLAYBACK_ENABLE | AD1848_PLAYBACK_PIO);
  448. snd_dma_program(chip->dma, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
  449. count = snd_ad1848_get_count(chip->image[AD1848_DATA_FORMAT], count) - 1;
  450. spin_lock_irqsave(&chip->reg_lock, flags);
  451. snd_ad1848_out(chip, AD1848_DATA_LWR_CNT, (unsigned char) count);
  452. snd_ad1848_out(chip, AD1848_DATA_UPR_CNT, (unsigned char) (count >> 8));
  453. spin_unlock_irqrestore(&chip->reg_lock, flags);
  454. return 0;
  455. }
  456. static int snd_ad1848_capture_hw_params(struct snd_pcm_substream *substream,
  457. struct snd_pcm_hw_params *hw_params)
  458. {
  459. struct snd_ad1848 *chip = snd_pcm_substream_chip(substream);
  460. unsigned long flags;
  461. int err;
  462. if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
  463. return err;
  464. snd_ad1848_calibrate_mute(chip, 1);
  465. snd_ad1848_set_data_format(chip, hw_params);
  466. snd_ad1848_mce_up(chip);
  467. spin_lock_irqsave(&chip->reg_lock, flags);
  468. snd_ad1848_out(chip, AD1848_DATA_FORMAT, chip->image[AD1848_DATA_FORMAT]);
  469. spin_unlock_irqrestore(&chip->reg_lock, flags);
  470. snd_ad1848_mce_down(chip);
  471. snd_ad1848_calibrate_mute(chip, 0);
  472. return 0;
  473. }
  474. static int snd_ad1848_capture_hw_free(struct snd_pcm_substream *substream)
  475. {
  476. return snd_pcm_lib_free_pages(substream);
  477. }
  478. static int snd_ad1848_capture_prepare(struct snd_pcm_substream *substream)
  479. {
  480. struct snd_ad1848 *chip = snd_pcm_substream_chip(substream);
  481. struct snd_pcm_runtime *runtime = substream->runtime;
  482. unsigned long flags;
  483. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  484. unsigned int count = snd_pcm_lib_period_bytes(substream);
  485. chip->dma_size = size;
  486. chip->image[AD1848_IFACE_CTRL] &= ~(AD1848_CAPTURE_ENABLE | AD1848_CAPTURE_PIO);
  487. snd_dma_program(chip->dma, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
  488. count = snd_ad1848_get_count(chip->image[AD1848_DATA_FORMAT], count) - 1;
  489. spin_lock_irqsave(&chip->reg_lock, flags);
  490. snd_ad1848_out(chip, AD1848_DATA_LWR_CNT, (unsigned char) count);
  491. snd_ad1848_out(chip, AD1848_DATA_UPR_CNT, (unsigned char) (count >> 8));
  492. spin_unlock_irqrestore(&chip->reg_lock, flags);
  493. return 0;
  494. }
  495. static irqreturn_t snd_ad1848_interrupt(int irq, void *dev_id)
  496. {
  497. struct snd_ad1848 *chip = dev_id;
  498. if ((chip->mode & AD1848_MODE_PLAY) && chip->playback_substream &&
  499. (chip->mode & AD1848_MODE_RUNNING))
  500. snd_pcm_period_elapsed(chip->playback_substream);
  501. if ((chip->mode & AD1848_MODE_CAPTURE) && chip->capture_substream &&
  502. (chip->mode & AD1848_MODE_RUNNING))
  503. snd_pcm_period_elapsed(chip->capture_substream);
  504. outb(0, AD1848P(chip, STATUS)); /* clear global interrupt bit */
  505. return IRQ_HANDLED;
  506. }
  507. static snd_pcm_uframes_t snd_ad1848_playback_pointer(struct snd_pcm_substream *substream)
  508. {
  509. struct snd_ad1848 *chip = snd_pcm_substream_chip(substream);
  510. size_t ptr;
  511. if (!(chip->image[AD1848_IFACE_CTRL] & AD1848_PLAYBACK_ENABLE))
  512. return 0;
  513. ptr = snd_dma_pointer(chip->dma, chip->dma_size);
  514. return bytes_to_frames(substream->runtime, ptr);
  515. }
  516. static snd_pcm_uframes_t snd_ad1848_capture_pointer(struct snd_pcm_substream *substream)
  517. {
  518. struct snd_ad1848 *chip = snd_pcm_substream_chip(substream);
  519. size_t ptr;
  520. if (!(chip->image[AD1848_IFACE_CTRL] & AD1848_CAPTURE_ENABLE))
  521. return 0;
  522. ptr = snd_dma_pointer(chip->dma, chip->dma_size);
  523. return bytes_to_frames(substream->runtime, ptr);
  524. }
  525. /*
  526. */
  527. static void snd_ad1848_thinkpad_twiddle(struct snd_ad1848 *chip, int on) {
  528. int tmp;
  529. if (!chip->thinkpad_flag) return;
  530. outb(0x1c, AD1848_THINKPAD_CTL_PORT1);
  531. tmp = inb(AD1848_THINKPAD_CTL_PORT2);
  532. if (on)
  533. /* turn it on */
  534. tmp |= AD1848_THINKPAD_CS4248_ENABLE_BIT;
  535. else
  536. /* turn it off */
  537. tmp &= ~AD1848_THINKPAD_CS4248_ENABLE_BIT;
  538. outb(tmp, AD1848_THINKPAD_CTL_PORT2);
  539. }
  540. #ifdef CONFIG_PM
  541. static void snd_ad1848_suspend(struct snd_ad1848 *chip)
  542. {
  543. snd_pcm_suspend_all(chip->pcm);
  544. if (chip->thinkpad_flag)
  545. snd_ad1848_thinkpad_twiddle(chip, 0);
  546. }
  547. static void snd_ad1848_resume(struct snd_ad1848 *chip)
  548. {
  549. int i;
  550. if (chip->thinkpad_flag)
  551. snd_ad1848_thinkpad_twiddle(chip, 1);
  552. /* clear any pendings IRQ */
  553. inb(AD1848P(chip, STATUS));
  554. outb(0, AD1848P(chip, STATUS));
  555. mb();
  556. snd_ad1848_mce_down(chip);
  557. for (i = 0; i < 16; i++)
  558. snd_ad1848_out(chip, i, chip->image[i]);
  559. snd_ad1848_mce_up(chip);
  560. snd_ad1848_mce_down(chip);
  561. }
  562. #endif /* CONFIG_PM */
  563. static int snd_ad1848_probe(struct snd_ad1848 * chip)
  564. {
  565. unsigned long flags;
  566. int i, id, rev, ad1847;
  567. unsigned char *ptr;
  568. #if 0
  569. snd_ad1848_debug(chip);
  570. #endif
  571. id = ad1847 = 0;
  572. for (i = 0; i < 1000; i++) {
  573. mb();
  574. if (inb(AD1848P(chip, REGSEL)) & AD1848_INIT)
  575. udelay(500);
  576. else {
  577. spin_lock_irqsave(&chip->reg_lock, flags);
  578. snd_ad1848_out(chip, AD1848_MISC_INFO, 0x00);
  579. snd_ad1848_out(chip, AD1848_LEFT_INPUT, 0xaa);
  580. snd_ad1848_out(chip, AD1848_RIGHT_INPUT, 0x45);
  581. rev = snd_ad1848_in(chip, AD1848_RIGHT_INPUT);
  582. if (rev == 0x65) {
  583. spin_unlock_irqrestore(&chip->reg_lock, flags);
  584. id = 1;
  585. ad1847 = 1;
  586. break;
  587. }
  588. if (snd_ad1848_in(chip, AD1848_LEFT_INPUT) == 0xaa && rev == 0x45) {
  589. spin_unlock_irqrestore(&chip->reg_lock, flags);
  590. id = 1;
  591. break;
  592. }
  593. spin_unlock_irqrestore(&chip->reg_lock, flags);
  594. }
  595. }
  596. if (id != 1)
  597. return -ENODEV; /* no valid device found */
  598. if (chip->hardware == AD1848_HW_DETECT) {
  599. if (ad1847) {
  600. chip->hardware = AD1848_HW_AD1847;
  601. } else {
  602. chip->hardware = AD1848_HW_AD1848;
  603. rev = snd_ad1848_in(chip, AD1848_MISC_INFO);
  604. if (rev & 0x80) {
  605. chip->hardware = AD1848_HW_CS4248;
  606. } else if ((rev & 0x0f) == 0x0a) {
  607. snd_ad1848_out(chip, AD1848_MISC_INFO, 0x40);
  608. for (i = 0; i < 16; ++i) {
  609. if (snd_ad1848_in(chip, i) != snd_ad1848_in(chip, i + 16)) {
  610. chip->hardware = AD1848_HW_CMI8330;
  611. break;
  612. }
  613. }
  614. snd_ad1848_out(chip, AD1848_MISC_INFO, 0x00);
  615. }
  616. }
  617. }
  618. spin_lock_irqsave(&chip->reg_lock, flags);
  619. inb(AD1848P(chip, STATUS)); /* clear any pendings IRQ */
  620. outb(0, AD1848P(chip, STATUS));
  621. mb();
  622. spin_unlock_irqrestore(&chip->reg_lock, flags);
  623. chip->image[AD1848_MISC_INFO] = 0x00;
  624. chip->image[AD1848_IFACE_CTRL] =
  625. (chip->image[AD1848_IFACE_CTRL] & ~AD1848_SINGLE_DMA) | AD1848_SINGLE_DMA;
  626. ptr = (unsigned char *) &chip->image;
  627. snd_ad1848_mce_down(chip);
  628. spin_lock_irqsave(&chip->reg_lock, flags);
  629. for (i = 0; i < 16; i++) /* ok.. fill all AD1848 registers */
  630. snd_ad1848_out(chip, i, *ptr++);
  631. spin_unlock_irqrestore(&chip->reg_lock, flags);
  632. snd_ad1848_mce_up(chip);
  633. snd_ad1848_mce_down(chip);
  634. return 0; /* all things are ok.. */
  635. }
  636. /*
  637. */
  638. static struct snd_pcm_hardware snd_ad1848_playback =
  639. {
  640. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  641. SNDRV_PCM_INFO_MMAP_VALID),
  642. .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW |
  643. SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE),
  644. .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
  645. .rate_min = 5510,
  646. .rate_max = 48000,
  647. .channels_min = 1,
  648. .channels_max = 2,
  649. .buffer_bytes_max = (128*1024),
  650. .period_bytes_min = 64,
  651. .period_bytes_max = (128*1024),
  652. .periods_min = 1,
  653. .periods_max = 1024,
  654. .fifo_size = 0,
  655. };
  656. static struct snd_pcm_hardware snd_ad1848_capture =
  657. {
  658. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  659. SNDRV_PCM_INFO_MMAP_VALID),
  660. .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW |
  661. SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE),
  662. .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
  663. .rate_min = 5510,
  664. .rate_max = 48000,
  665. .channels_min = 1,
  666. .channels_max = 2,
  667. .buffer_bytes_max = (128*1024),
  668. .period_bytes_min = 64,
  669. .period_bytes_max = (128*1024),
  670. .periods_min = 1,
  671. .periods_max = 1024,
  672. .fifo_size = 0,
  673. };
  674. /*
  675. */
  676. static int snd_ad1848_playback_open(struct snd_pcm_substream *substream)
  677. {
  678. struct snd_ad1848 *chip = snd_pcm_substream_chip(substream);
  679. struct snd_pcm_runtime *runtime = substream->runtime;
  680. int err;
  681. if ((err = snd_ad1848_open(chip, AD1848_MODE_PLAY)) < 0)
  682. return err;
  683. chip->playback_substream = substream;
  684. runtime->hw = snd_ad1848_playback;
  685. snd_pcm_limit_isa_dma_size(chip->dma, &runtime->hw.buffer_bytes_max);
  686. snd_pcm_limit_isa_dma_size(chip->dma, &runtime->hw.period_bytes_max);
  687. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
  688. return 0;
  689. }
  690. static int snd_ad1848_capture_open(struct snd_pcm_substream *substream)
  691. {
  692. struct snd_ad1848 *chip = snd_pcm_substream_chip(substream);
  693. struct snd_pcm_runtime *runtime = substream->runtime;
  694. int err;
  695. if ((err = snd_ad1848_open(chip, AD1848_MODE_CAPTURE)) < 0)
  696. return err;
  697. chip->capture_substream = substream;
  698. runtime->hw = snd_ad1848_capture;
  699. snd_pcm_limit_isa_dma_size(chip->dma, &runtime->hw.buffer_bytes_max);
  700. snd_pcm_limit_isa_dma_size(chip->dma, &runtime->hw.period_bytes_max);
  701. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
  702. return 0;
  703. }
  704. static int snd_ad1848_playback_close(struct snd_pcm_substream *substream)
  705. {
  706. struct snd_ad1848 *chip = snd_pcm_substream_chip(substream);
  707. chip->mode &= ~AD1848_MODE_PLAY;
  708. chip->playback_substream = NULL;
  709. snd_ad1848_close(chip);
  710. return 0;
  711. }
  712. static int snd_ad1848_capture_close(struct snd_pcm_substream *substream)
  713. {
  714. struct snd_ad1848 *chip = snd_pcm_substream_chip(substream);
  715. chip->mode &= ~AD1848_MODE_CAPTURE;
  716. chip->capture_substream = NULL;
  717. snd_ad1848_close(chip);
  718. return 0;
  719. }
  720. static int snd_ad1848_free(struct snd_ad1848 *chip)
  721. {
  722. release_and_free_resource(chip->res_port);
  723. if (chip->irq >= 0)
  724. free_irq(chip->irq, (void *) chip);
  725. if (chip->dma >= 0) {
  726. snd_dma_disable(chip->dma);
  727. free_dma(chip->dma);
  728. }
  729. kfree(chip);
  730. return 0;
  731. }
  732. static int snd_ad1848_dev_free(struct snd_device *device)
  733. {
  734. struct snd_ad1848 *chip = device->device_data;
  735. return snd_ad1848_free(chip);
  736. }
  737. static const char *snd_ad1848_chip_id(struct snd_ad1848 *chip)
  738. {
  739. switch (chip->hardware) {
  740. case AD1848_HW_AD1847: return "AD1847";
  741. case AD1848_HW_AD1848: return "AD1848";
  742. case AD1848_HW_CS4248: return "CS4248";
  743. case AD1848_HW_CMI8330: return "CMI8330/C3D";
  744. default: return "???";
  745. }
  746. }
  747. int snd_ad1848_create(struct snd_card *card,
  748. unsigned long port,
  749. int irq, int dma,
  750. unsigned short hardware,
  751. struct snd_ad1848 ** rchip)
  752. {
  753. static struct snd_device_ops ops = {
  754. .dev_free = snd_ad1848_dev_free,
  755. };
  756. struct snd_ad1848 *chip;
  757. int err;
  758. *rchip = NULL;
  759. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  760. if (chip == NULL)
  761. return -ENOMEM;
  762. spin_lock_init(&chip->reg_lock);
  763. chip->card = card;
  764. chip->port = port;
  765. chip->irq = -1;
  766. chip->dma = -1;
  767. chip->hardware = hardware;
  768. memcpy(&chip->image, &snd_ad1848_original_image, sizeof(snd_ad1848_original_image));
  769. if ((chip->res_port = request_region(port, 4, "AD1848")) == NULL) {
  770. snd_printk(KERN_ERR "ad1848: can't grab port 0x%lx\n", port);
  771. snd_ad1848_free(chip);
  772. return -EBUSY;
  773. }
  774. if (request_irq(irq, snd_ad1848_interrupt, IRQF_DISABLED, "AD1848", (void *) chip)) {
  775. snd_printk(KERN_ERR "ad1848: can't grab IRQ %d\n", irq);
  776. snd_ad1848_free(chip);
  777. return -EBUSY;
  778. }
  779. chip->irq = irq;
  780. if (request_dma(dma, "AD1848")) {
  781. snd_printk(KERN_ERR "ad1848: can't grab DMA %d\n", dma);
  782. snd_ad1848_free(chip);
  783. return -EBUSY;
  784. }
  785. chip->dma = dma;
  786. if (hardware == AD1848_HW_THINKPAD) {
  787. chip->thinkpad_flag = 1;
  788. chip->hardware = AD1848_HW_DETECT; /* reset */
  789. snd_ad1848_thinkpad_twiddle(chip, 1);
  790. }
  791. if (snd_ad1848_probe(chip) < 0) {
  792. snd_ad1848_free(chip);
  793. return -ENODEV;
  794. }
  795. /* Register device */
  796. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  797. snd_ad1848_free(chip);
  798. return err;
  799. }
  800. #ifdef CONFIG_PM
  801. chip->suspend = snd_ad1848_suspend;
  802. chip->resume = snd_ad1848_resume;
  803. #endif
  804. *rchip = chip;
  805. return 0;
  806. }
  807. EXPORT_SYMBOL(snd_ad1848_create);
  808. static struct snd_pcm_ops snd_ad1848_playback_ops = {
  809. .open = snd_ad1848_playback_open,
  810. .close = snd_ad1848_playback_close,
  811. .ioctl = snd_ad1848_ioctl,
  812. .hw_params = snd_ad1848_playback_hw_params,
  813. .hw_free = snd_ad1848_playback_hw_free,
  814. .prepare = snd_ad1848_playback_prepare,
  815. .trigger = snd_ad1848_playback_trigger,
  816. .pointer = snd_ad1848_playback_pointer,
  817. };
  818. static struct snd_pcm_ops snd_ad1848_capture_ops = {
  819. .open = snd_ad1848_capture_open,
  820. .close = snd_ad1848_capture_close,
  821. .ioctl = snd_ad1848_ioctl,
  822. .hw_params = snd_ad1848_capture_hw_params,
  823. .hw_free = snd_ad1848_capture_hw_free,
  824. .prepare = snd_ad1848_capture_prepare,
  825. .trigger = snd_ad1848_capture_trigger,
  826. .pointer = snd_ad1848_capture_pointer,
  827. };
  828. int snd_ad1848_pcm(struct snd_ad1848 *chip, int device, struct snd_pcm **rpcm)
  829. {
  830. struct snd_pcm *pcm;
  831. int err;
  832. if ((err = snd_pcm_new(chip->card, "AD1848", device, 1, 1, &pcm)) < 0)
  833. return err;
  834. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ad1848_playback_ops);
  835. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ad1848_capture_ops);
  836. pcm->private_data = chip;
  837. pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
  838. strcpy(pcm->name, snd_ad1848_chip_id(chip));
  839. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  840. snd_dma_isa_data(),
  841. 64*1024, chip->dma > 3 ? 128*1024 : 64*1024);
  842. chip->pcm = pcm;
  843. if (rpcm)
  844. *rpcm = pcm;
  845. return 0;
  846. }
  847. EXPORT_SYMBOL(snd_ad1848_pcm);
  848. const struct snd_pcm_ops *snd_ad1848_get_pcm_ops(int direction)
  849. {
  850. return direction == SNDRV_PCM_STREAM_PLAYBACK ?
  851. &snd_ad1848_playback_ops : &snd_ad1848_capture_ops;
  852. }
  853. EXPORT_SYMBOL(snd_ad1848_get_pcm_ops);
  854. /*
  855. * MIXER part
  856. */
  857. static int snd_ad1848_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  858. {
  859. static char *texts[4] = {
  860. "Line", "Aux", "Mic", "Mix"
  861. };
  862. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  863. uinfo->count = 2;
  864. uinfo->value.enumerated.items = 4;
  865. if (uinfo->value.enumerated.item > 3)
  866. uinfo->value.enumerated.item = 3;
  867. strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
  868. return 0;
  869. }
  870. static int snd_ad1848_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  871. {
  872. struct snd_ad1848 *chip = snd_kcontrol_chip(kcontrol);
  873. unsigned long flags;
  874. spin_lock_irqsave(&chip->reg_lock, flags);
  875. ucontrol->value.enumerated.item[0] = (chip->image[AD1848_LEFT_INPUT] & AD1848_MIXS_ALL) >> 6;
  876. ucontrol->value.enumerated.item[1] = (chip->image[AD1848_RIGHT_INPUT] & AD1848_MIXS_ALL) >> 6;
  877. spin_unlock_irqrestore(&chip->reg_lock, flags);
  878. return 0;
  879. }
  880. static int snd_ad1848_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  881. {
  882. struct snd_ad1848 *chip = snd_kcontrol_chip(kcontrol);
  883. unsigned long flags;
  884. unsigned short left, right;
  885. int change;
  886. if (ucontrol->value.enumerated.item[0] > 3 ||
  887. ucontrol->value.enumerated.item[1] > 3)
  888. return -EINVAL;
  889. left = ucontrol->value.enumerated.item[0] << 6;
  890. right = ucontrol->value.enumerated.item[1] << 6;
  891. spin_lock_irqsave(&chip->reg_lock, flags);
  892. left = (chip->image[AD1848_LEFT_INPUT] & ~AD1848_MIXS_ALL) | left;
  893. right = (chip->image[AD1848_RIGHT_INPUT] & ~AD1848_MIXS_ALL) | right;
  894. change = left != chip->image[AD1848_LEFT_INPUT] ||
  895. right != chip->image[AD1848_RIGHT_INPUT];
  896. snd_ad1848_out(chip, AD1848_LEFT_INPUT, left);
  897. snd_ad1848_out(chip, AD1848_RIGHT_INPUT, right);
  898. spin_unlock_irqrestore(&chip->reg_lock, flags);
  899. return change;
  900. }
  901. static int snd_ad1848_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  902. {
  903. int mask = (kcontrol->private_value >> 16) & 0xff;
  904. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  905. uinfo->count = 1;
  906. uinfo->value.integer.min = 0;
  907. uinfo->value.integer.max = mask;
  908. return 0;
  909. }
  910. static int snd_ad1848_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  911. {
  912. struct snd_ad1848 *chip = snd_kcontrol_chip(kcontrol);
  913. unsigned long flags;
  914. int reg = kcontrol->private_value & 0xff;
  915. int shift = (kcontrol->private_value >> 8) & 0xff;
  916. int mask = (kcontrol->private_value >> 16) & 0xff;
  917. int invert = (kcontrol->private_value >> 24) & 0xff;
  918. spin_lock_irqsave(&chip->reg_lock, flags);
  919. ucontrol->value.integer.value[0] = (chip->image[reg] >> shift) & mask;
  920. spin_unlock_irqrestore(&chip->reg_lock, flags);
  921. if (invert)
  922. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  923. return 0;
  924. }
  925. static int snd_ad1848_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  926. {
  927. struct snd_ad1848 *chip = snd_kcontrol_chip(kcontrol);
  928. unsigned long flags;
  929. int reg = kcontrol->private_value & 0xff;
  930. int shift = (kcontrol->private_value >> 8) & 0xff;
  931. int mask = (kcontrol->private_value >> 16) & 0xff;
  932. int invert = (kcontrol->private_value >> 24) & 0xff;
  933. int change;
  934. unsigned short val;
  935. val = (ucontrol->value.integer.value[0] & mask);
  936. if (invert)
  937. val = mask - val;
  938. val <<= shift;
  939. spin_lock_irqsave(&chip->reg_lock, flags);
  940. val = (chip->image[reg] & ~(mask << shift)) | val;
  941. change = val != chip->image[reg];
  942. snd_ad1848_out(chip, reg, val);
  943. spin_unlock_irqrestore(&chip->reg_lock, flags);
  944. return change;
  945. }
  946. static int snd_ad1848_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  947. {
  948. int mask = (kcontrol->private_value >> 24) & 0xff;
  949. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  950. uinfo->count = 2;
  951. uinfo->value.integer.min = 0;
  952. uinfo->value.integer.max = mask;
  953. return 0;
  954. }
  955. static int snd_ad1848_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  956. {
  957. struct snd_ad1848 *chip = snd_kcontrol_chip(kcontrol);
  958. unsigned long flags;
  959. int left_reg = kcontrol->private_value & 0xff;
  960. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  961. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  962. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  963. int mask = (kcontrol->private_value >> 24) & 0xff;
  964. int invert = (kcontrol->private_value >> 22) & 1;
  965. spin_lock_irqsave(&chip->reg_lock, flags);
  966. ucontrol->value.integer.value[0] = (chip->image[left_reg] >> shift_left) & mask;
  967. ucontrol->value.integer.value[1] = (chip->image[right_reg] >> shift_right) & mask;
  968. spin_unlock_irqrestore(&chip->reg_lock, flags);
  969. if (invert) {
  970. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  971. ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
  972. }
  973. return 0;
  974. }
  975. static int snd_ad1848_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  976. {
  977. struct snd_ad1848 *chip = snd_kcontrol_chip(kcontrol);
  978. unsigned long flags;
  979. int left_reg = kcontrol->private_value & 0xff;
  980. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  981. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  982. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  983. int mask = (kcontrol->private_value >> 24) & 0xff;
  984. int invert = (kcontrol->private_value >> 22) & 1;
  985. int change;
  986. unsigned short val1, val2;
  987. val1 = ucontrol->value.integer.value[0] & mask;
  988. val2 = ucontrol->value.integer.value[1] & mask;
  989. if (invert) {
  990. val1 = mask - val1;
  991. val2 = mask - val2;
  992. }
  993. val1 <<= shift_left;
  994. val2 <<= shift_right;
  995. spin_lock_irqsave(&chip->reg_lock, flags);
  996. if (left_reg != right_reg) {
  997. val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1;
  998. val2 = (chip->image[right_reg] & ~(mask << shift_right)) | val2;
  999. change = val1 != chip->image[left_reg] || val2 != chip->image[right_reg];
  1000. snd_ad1848_out(chip, left_reg, val1);
  1001. snd_ad1848_out(chip, right_reg, val2);
  1002. } else {
  1003. val1 = (chip->image[left_reg] & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
  1004. change = val1 != chip->image[left_reg];
  1005. snd_ad1848_out(chip, left_reg, val1);
  1006. }
  1007. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1008. return change;
  1009. }
  1010. /*
  1011. */
  1012. int snd_ad1848_add_ctl_elem(struct snd_ad1848 *chip,
  1013. const struct ad1848_mix_elem *c)
  1014. {
  1015. static struct snd_kcontrol_new newctls[] = {
  1016. [AD1848_MIX_SINGLE] = {
  1017. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1018. .info = snd_ad1848_info_single,
  1019. .get = snd_ad1848_get_single,
  1020. .put = snd_ad1848_put_single,
  1021. },
  1022. [AD1848_MIX_DOUBLE] = {
  1023. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1024. .info = snd_ad1848_info_double,
  1025. .get = snd_ad1848_get_double,
  1026. .put = snd_ad1848_put_double,
  1027. },
  1028. [AD1848_MIX_CAPTURE] = {
  1029. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1030. .info = snd_ad1848_info_mux,
  1031. .get = snd_ad1848_get_mux,
  1032. .put = snd_ad1848_put_mux,
  1033. },
  1034. };
  1035. struct snd_kcontrol *ctl;
  1036. int err;
  1037. ctl = snd_ctl_new1(&newctls[c->type], chip);
  1038. if (! ctl)
  1039. return -ENOMEM;
  1040. strlcpy(ctl->id.name, c->name, sizeof(ctl->id.name));
  1041. ctl->id.index = c->index;
  1042. ctl->private_value = c->private_value;
  1043. if (c->tlv) {
  1044. ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  1045. ctl->tlv.p = c->tlv;
  1046. }
  1047. if ((err = snd_ctl_add(chip->card, ctl)) < 0)
  1048. return err;
  1049. return 0;
  1050. }
  1051. EXPORT_SYMBOL(snd_ad1848_add_ctl_elem);
  1052. static const DECLARE_TLV_DB_SCALE(db_scale_6bit, -9450, 150, 0);
  1053. static const DECLARE_TLV_DB_SCALE(db_scale_5bit_12db_max, -3450, 150, 0);
  1054. static const DECLARE_TLV_DB_SCALE(db_scale_rec_gain, 0, 150, 0);
  1055. static struct ad1848_mix_elem snd_ad1848_controls[] = {
  1056. AD1848_DOUBLE("PCM Playback Switch", 0, AD1848_LEFT_OUTPUT, AD1848_RIGHT_OUTPUT, 7, 7, 1, 1),
  1057. AD1848_DOUBLE_TLV("PCM Playback Volume", 0, AD1848_LEFT_OUTPUT, AD1848_RIGHT_OUTPUT, 0, 0, 63, 1,
  1058. db_scale_6bit),
  1059. AD1848_DOUBLE("Aux Playback Switch", 0, AD1848_AUX1_LEFT_INPUT, AD1848_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
  1060. AD1848_DOUBLE_TLV("Aux Playback Volume", 0, AD1848_AUX1_LEFT_INPUT, AD1848_AUX1_RIGHT_INPUT, 0, 0, 31, 1,
  1061. db_scale_5bit_12db_max),
  1062. AD1848_DOUBLE("Aux Playback Switch", 1, AD1848_AUX2_LEFT_INPUT, AD1848_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
  1063. AD1848_DOUBLE_TLV("Aux Playback Volume", 1, AD1848_AUX2_LEFT_INPUT, AD1848_AUX2_RIGHT_INPUT, 0, 0, 31, 1,
  1064. db_scale_5bit_12db_max),
  1065. AD1848_DOUBLE_TLV("Capture Volume", 0, AD1848_LEFT_INPUT, AD1848_RIGHT_INPUT, 0, 0, 15, 0,
  1066. db_scale_rec_gain),
  1067. {
  1068. .name = "Capture Source",
  1069. .type = AD1848_MIX_CAPTURE,
  1070. },
  1071. AD1848_SINGLE("Loopback Capture Switch", 0, AD1848_LOOPBACK, 0, 1, 0),
  1072. AD1848_SINGLE_TLV("Loopback Capture Volume", 0, AD1848_LOOPBACK, 1, 63, 0,
  1073. db_scale_6bit),
  1074. };
  1075. int snd_ad1848_mixer(struct snd_ad1848 *chip)
  1076. {
  1077. struct snd_card *card;
  1078. struct snd_pcm *pcm;
  1079. unsigned int idx;
  1080. int err;
  1081. snd_assert(chip != NULL && chip->pcm != NULL, return -EINVAL);
  1082. pcm = chip->pcm;
  1083. card = chip->card;
  1084. strcpy(card->mixername, pcm->name);
  1085. for (idx = 0; idx < ARRAY_SIZE(snd_ad1848_controls); idx++)
  1086. if ((err = snd_ad1848_add_ctl_elem(chip, &snd_ad1848_controls[idx])) < 0)
  1087. return err;
  1088. return 0;
  1089. }
  1090. EXPORT_SYMBOL(snd_ad1848_mixer);
  1091. /*
  1092. * INIT part
  1093. */
  1094. static int __init alsa_ad1848_init(void)
  1095. {
  1096. return 0;
  1097. }
  1098. static void __exit alsa_ad1848_exit(void)
  1099. {
  1100. }
  1101. module_init(alsa_ad1848_init)
  1102. module_exit(alsa_ad1848_exit)