pdaudiocf_core.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Driver for Sound Core PDAudioCF soundcard
  3. *
  4. * Copyright (c) 2003 by Jaroslav Kysela <perex@perex.cz>
  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. #include <linux/delay.h>
  21. #include <linux/slab.h>
  22. #include <sound/core.h>
  23. #include <sound/info.h>
  24. #include "pdaudiocf.h"
  25. #include <sound/initval.h>
  26. /*
  27. *
  28. */
  29. static unsigned char pdacf_ak4117_read(void *private_data, unsigned char reg)
  30. {
  31. struct snd_pdacf *chip = private_data;
  32. unsigned long timeout;
  33. unsigned long flags;
  34. unsigned char res;
  35. spin_lock_irqsave(&chip->ak4117_lock, flags);
  36. timeout = 1000;
  37. while (pdacf_reg_read(chip, PDAUDIOCF_REG_SCR) & PDAUDIOCF_AK_SBP) {
  38. udelay(5);
  39. if (--timeout == 0) {
  40. spin_unlock_irqrestore(&chip->ak4117_lock, flags);
  41. snd_printk(KERN_ERR "AK4117 ready timeout (read)\n");
  42. return 0;
  43. }
  44. }
  45. pdacf_reg_write(chip, PDAUDIOCF_REG_AK_IFR, (u16)reg << 8);
  46. timeout = 1000;
  47. while (pdacf_reg_read(chip, PDAUDIOCF_REG_SCR) & PDAUDIOCF_AK_SBP) {
  48. udelay(5);
  49. if (--timeout == 0) {
  50. spin_unlock_irqrestore(&chip->ak4117_lock, flags);
  51. snd_printk(KERN_ERR "AK4117 read timeout (read2)\n");
  52. return 0;
  53. }
  54. }
  55. res = (unsigned char)pdacf_reg_read(chip, PDAUDIOCF_REG_AK_IFR);
  56. spin_unlock_irqrestore(&chip->ak4117_lock, flags);
  57. return res;
  58. }
  59. static void pdacf_ak4117_write(void *private_data, unsigned char reg, unsigned char val)
  60. {
  61. struct snd_pdacf *chip = private_data;
  62. unsigned long timeout;
  63. unsigned long flags;
  64. spin_lock_irqsave(&chip->ak4117_lock, flags);
  65. timeout = 1000;
  66. while (inw(chip->port + PDAUDIOCF_REG_SCR) & PDAUDIOCF_AK_SBP) {
  67. udelay(5);
  68. if (--timeout == 0) {
  69. spin_unlock_irqrestore(&chip->ak4117_lock, flags);
  70. snd_printk(KERN_ERR "AK4117 ready timeout (write)\n");
  71. return;
  72. }
  73. }
  74. outw((u16)reg << 8 | val | (1<<13), chip->port + PDAUDIOCF_REG_AK_IFR);
  75. spin_unlock_irqrestore(&chip->ak4117_lock, flags);
  76. }
  77. #if 0
  78. void pdacf_dump(struct snd_pdacf *chip)
  79. {
  80. printk(KERN_DEBUG "PDAUDIOCF DUMP (0x%lx):\n", chip->port);
  81. printk(KERN_DEBUG "WPD : 0x%x\n",
  82. inw(chip->port + PDAUDIOCF_REG_WDP));
  83. printk(KERN_DEBUG "RDP : 0x%x\n",
  84. inw(chip->port + PDAUDIOCF_REG_RDP));
  85. printk(KERN_DEBUG "TCR : 0x%x\n",
  86. inw(chip->port + PDAUDIOCF_REG_TCR));
  87. printk(KERN_DEBUG "SCR : 0x%x\n",
  88. inw(chip->port + PDAUDIOCF_REG_SCR));
  89. printk(KERN_DEBUG "ISR : 0x%x\n",
  90. inw(chip->port + PDAUDIOCF_REG_ISR));
  91. printk(KERN_DEBUG "IER : 0x%x\n",
  92. inw(chip->port + PDAUDIOCF_REG_IER));
  93. printk(KERN_DEBUG "AK_IFR : 0x%x\n",
  94. inw(chip->port + PDAUDIOCF_REG_AK_IFR));
  95. }
  96. #endif
  97. static int pdacf_reset(struct snd_pdacf *chip, int powerdown)
  98. {
  99. u16 val;
  100. val = pdacf_reg_read(chip, PDAUDIOCF_REG_SCR);
  101. val |= PDAUDIOCF_PDN;
  102. val &= ~PDAUDIOCF_RECORD; /* for sure */
  103. pdacf_reg_write(chip, PDAUDIOCF_REG_SCR, val);
  104. udelay(5);
  105. val |= PDAUDIOCF_RST;
  106. pdacf_reg_write(chip, PDAUDIOCF_REG_SCR, val);
  107. udelay(200);
  108. val &= ~PDAUDIOCF_RST;
  109. pdacf_reg_write(chip, PDAUDIOCF_REG_SCR, val);
  110. udelay(5);
  111. if (!powerdown) {
  112. val &= ~PDAUDIOCF_PDN;
  113. pdacf_reg_write(chip, PDAUDIOCF_REG_SCR, val);
  114. udelay(200);
  115. }
  116. return 0;
  117. }
  118. void pdacf_reinit(struct snd_pdacf *chip, int resume)
  119. {
  120. pdacf_reset(chip, 0);
  121. if (resume)
  122. pdacf_reg_write(chip, PDAUDIOCF_REG_SCR, chip->suspend_reg_scr);
  123. snd_ak4117_reinit(chip->ak4117);
  124. pdacf_reg_write(chip, PDAUDIOCF_REG_TCR, chip->regmap[PDAUDIOCF_REG_TCR>>1]);
  125. pdacf_reg_write(chip, PDAUDIOCF_REG_IER, chip->regmap[PDAUDIOCF_REG_IER>>1]);
  126. }
  127. static void pdacf_proc_read(struct snd_info_entry * entry,
  128. struct snd_info_buffer *buffer)
  129. {
  130. struct snd_pdacf *chip = entry->private_data;
  131. u16 tmp;
  132. snd_iprintf(buffer, "PDAudioCF\n\n");
  133. tmp = pdacf_reg_read(chip, PDAUDIOCF_REG_SCR);
  134. snd_iprintf(buffer, "FPGA revision : 0x%x\n", PDAUDIOCF_FPGAREV(tmp));
  135. }
  136. static void pdacf_proc_init(struct snd_pdacf *chip)
  137. {
  138. struct snd_info_entry *entry;
  139. if (! snd_card_proc_new(chip->card, "pdaudiocf", &entry))
  140. snd_info_set_text_ops(entry, chip, pdacf_proc_read);
  141. }
  142. struct snd_pdacf *snd_pdacf_create(struct snd_card *card)
  143. {
  144. struct snd_pdacf *chip;
  145. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  146. if (chip == NULL)
  147. return NULL;
  148. chip->card = card;
  149. spin_lock_init(&chip->reg_lock);
  150. spin_lock_init(&chip->ak4117_lock);
  151. tasklet_init(&chip->tq, pdacf_tasklet, (unsigned long)chip);
  152. card->private_data = chip;
  153. pdacf_proc_init(chip);
  154. return chip;
  155. }
  156. static void snd_pdacf_ak4117_change(struct ak4117 *ak4117, unsigned char c0, unsigned char c1)
  157. {
  158. struct snd_pdacf *chip = ak4117->change_callback_private;
  159. unsigned long flags;
  160. u16 val;
  161. if (!(c0 & AK4117_UNLCK))
  162. return;
  163. spin_lock_irqsave(&chip->reg_lock, flags);
  164. val = chip->regmap[PDAUDIOCF_REG_SCR>>1];
  165. if (ak4117->rcs0 & AK4117_UNLCK)
  166. val |= PDAUDIOCF_BLUE_LED_OFF;
  167. else
  168. val &= ~PDAUDIOCF_BLUE_LED_OFF;
  169. pdacf_reg_write(chip, PDAUDIOCF_REG_SCR, val);
  170. spin_unlock_irqrestore(&chip->reg_lock, flags);
  171. }
  172. int snd_pdacf_ak4117_create(struct snd_pdacf *chip)
  173. {
  174. int err;
  175. u16 val;
  176. /* design note: if we unmask PLL unlock, parity, valid, audio or auto bit interrupts */
  177. /* from AK4117 then INT1 pin from AK4117 will be high all time, because PCMCIA interrupts are */
  178. /* egde based and FPGA does logical OR for all interrupt sources, we cannot use these */
  179. /* high-rate sources */
  180. static unsigned char pgm[5] = {
  181. AK4117_XTL_24_576M | AK4117_EXCT, /* AK4117_REG_PWRDN */
  182. AK4117_CM_PLL_XTAL | AK4117_PKCS_128fs | AK4117_XCKS_128fs, /* AK4117_REQ_CLOCK */
  183. AK4117_EFH_1024LRCLK | AK4117_DIF_24R | AK4117_IPS, /* AK4117_REG_IO */
  184. 0xff, /* AK4117_REG_INT0_MASK */
  185. AK4117_MAUTO | AK4117_MAUD | AK4117_MULK | AK4117_MPAR | AK4117_MV, /* AK4117_REG_INT1_MASK */
  186. };
  187. err = pdacf_reset(chip, 0);
  188. if (err < 0)
  189. return err;
  190. err = snd_ak4117_create(chip->card, pdacf_ak4117_read, pdacf_ak4117_write, pgm, chip, &chip->ak4117);
  191. if (err < 0)
  192. return err;
  193. val = pdacf_reg_read(chip, PDAUDIOCF_REG_TCR);
  194. #if 1 /* normal operation */
  195. val &= ~(PDAUDIOCF_ELIMAKMBIT|PDAUDIOCF_TESTDATASEL);
  196. #else /* debug */
  197. val |= PDAUDIOCF_ELIMAKMBIT;
  198. val &= ~PDAUDIOCF_TESTDATASEL;
  199. #endif
  200. pdacf_reg_write(chip, PDAUDIOCF_REG_TCR, val);
  201. /* setup the FPGA to match AK4117 setup */
  202. val = pdacf_reg_read(chip, PDAUDIOCF_REG_SCR);
  203. val &= ~(PDAUDIOCF_CLKDIV0 | PDAUDIOCF_CLKDIV1); /* use 24.576Mhz clock */
  204. val &= ~(PDAUDIOCF_RED_LED_OFF|PDAUDIOCF_BLUE_LED_OFF);
  205. val |= PDAUDIOCF_DATAFMT0 | PDAUDIOCF_DATAFMT1; /* 24-bit data */
  206. pdacf_reg_write(chip, PDAUDIOCF_REG_SCR, val);
  207. /* setup LEDs and IRQ */
  208. val = pdacf_reg_read(chip, PDAUDIOCF_REG_IER);
  209. val &= ~(PDAUDIOCF_IRQLVLEN0 | PDAUDIOCF_IRQLVLEN1);
  210. val &= ~(PDAUDIOCF_BLUEDUTY0 | PDAUDIOCF_REDDUTY0 | PDAUDIOCF_REDDUTY1);
  211. val |= PDAUDIOCF_BLUEDUTY1 | PDAUDIOCF_HALFRATE;
  212. val |= PDAUDIOCF_IRQOVREN | PDAUDIOCF_IRQAKMEN;
  213. pdacf_reg_write(chip, PDAUDIOCF_REG_IER, val);
  214. chip->ak4117->change_callback_private = chip;
  215. chip->ak4117->change_callback = snd_pdacf_ak4117_change;
  216. /* update LED status */
  217. snd_pdacf_ak4117_change(chip->ak4117, AK4117_UNLCK, 0);
  218. return 0;
  219. }
  220. void snd_pdacf_powerdown(struct snd_pdacf *chip)
  221. {
  222. u16 val;
  223. val = pdacf_reg_read(chip, PDAUDIOCF_REG_SCR);
  224. chip->suspend_reg_scr = val;
  225. val |= PDAUDIOCF_RED_LED_OFF | PDAUDIOCF_BLUE_LED_OFF;
  226. pdacf_reg_write(chip, PDAUDIOCF_REG_SCR, val);
  227. /* disable interrupts, but use direct write to preserve old register value in chip->regmap */
  228. val = inw(chip->port + PDAUDIOCF_REG_IER);
  229. val &= ~(PDAUDIOCF_IRQOVREN|PDAUDIOCF_IRQAKMEN|PDAUDIOCF_IRQLVLEN0|PDAUDIOCF_IRQLVLEN1);
  230. outw(val, chip->port + PDAUDIOCF_REG_IER);
  231. pdacf_reset(chip, 1);
  232. }
  233. #ifdef CONFIG_PM
  234. int snd_pdacf_suspend(struct snd_pdacf *chip, pm_message_t state)
  235. {
  236. u16 val;
  237. snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
  238. snd_pcm_suspend_all(chip->pcm);
  239. /* disable interrupts, but use direct write to preserve old register value in chip->regmap */
  240. val = inw(chip->port + PDAUDIOCF_REG_IER);
  241. val &= ~(PDAUDIOCF_IRQOVREN|PDAUDIOCF_IRQAKMEN|PDAUDIOCF_IRQLVLEN0|PDAUDIOCF_IRQLVLEN1);
  242. outw(val, chip->port + PDAUDIOCF_REG_IER);
  243. chip->chip_status |= PDAUDIOCF_STAT_IS_SUSPENDED; /* ignore interrupts from now */
  244. snd_pdacf_powerdown(chip);
  245. return 0;
  246. }
  247. static inline int check_signal(struct snd_pdacf *chip)
  248. {
  249. return (chip->ak4117->rcs0 & AK4117_UNLCK) == 0;
  250. }
  251. int snd_pdacf_resume(struct snd_pdacf *chip)
  252. {
  253. int timeout = 40;
  254. pdacf_reinit(chip, 1);
  255. /* wait for AK4117's PLL */
  256. while (timeout-- > 0 &&
  257. (snd_ak4117_external_rate(chip->ak4117) <= 0 || !check_signal(chip)))
  258. mdelay(1);
  259. chip->chip_status &= ~PDAUDIOCF_STAT_IS_SUSPENDED;
  260. snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
  261. return 0;
  262. }
  263. #endif