pxa2xx-ac97.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * linux/sound/pxa2xx-ac97.c -- AC97 support for the Intel PXA2xx chip.
  3. *
  4. * Author: Nicolas Pitre
  5. * Created: Dec 02, 2004
  6. * Copyright: MontaVista Software Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/wait.h>
  17. #include <linux/delay.h>
  18. #include <sound/driver.h>
  19. #include <sound/core.h>
  20. #include <sound/pcm.h>
  21. #include <sound/ac97_codec.h>
  22. #include <sound/initval.h>
  23. #include <sound/soc.h>
  24. #include <asm/irq.h>
  25. #include <linux/mutex.h>
  26. #include <asm/hardware.h>
  27. #include <asm/arch/pxa-regs.h>
  28. #include <asm/arch/audio.h>
  29. #include "pxa2xx-pcm.h"
  30. static DEFINE_MUTEX(car_mutex);
  31. static DECLARE_WAIT_QUEUE_HEAD(gsr_wq);
  32. static volatile long gsr_bits;
  33. #define AC97_DIR \
  34. (SND_SOC_DAIDIR_PLAYBACK | SND_SOC_DAIDIR_CAPTURE)
  35. #define AC97_RATES \
  36. (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | SNDRV_PCM_RATE_16000 | \
  37. SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
  38. /* may need to expand this */
  39. static struct snd_soc_dai_mode pxa2xx_ac97_modes[] = {
  40. {
  41. .pcmfmt = SNDRV_PCM_FMTBIT_S16_LE,
  42. .pcmrate = AC97_RATES,
  43. .pcmdir = AC97_DIR,
  44. },
  45. };
  46. /*
  47. * Beware PXA27x bugs:
  48. *
  49. * o Slot 12 read from modem space will hang controller.
  50. * o CDONE, SDONE interrupt fails after any slot 12 IO.
  51. *
  52. * We therefore have an hybrid approach for waiting on SDONE (interrupt or
  53. * 1 jiffy timeout if interrupt never comes).
  54. */
  55. static unsigned short pxa2xx_ac97_read(struct snd_ac97 *ac97,
  56. unsigned short reg)
  57. {
  58. unsigned short val = -1;
  59. volatile u32 *reg_addr;
  60. mutex_lock(&car_mutex);
  61. /* set up primary or secondary codec/modem space */
  62. #ifdef CONFIG_PXA27x
  63. reg_addr = ac97->num ? &SAC_REG_BASE : &PAC_REG_BASE;
  64. #else
  65. if (reg == AC97_GPIO_STATUS)
  66. reg_addr = ac97->num ? &SMC_REG_BASE : &PMC_REG_BASE;
  67. else
  68. reg_addr = ac97->num ? &SAC_REG_BASE : &PAC_REG_BASE;
  69. #endif
  70. reg_addr += (reg >> 1);
  71. #ifndef CONFIG_PXA27x
  72. if (reg == AC97_GPIO_STATUS) {
  73. /* read from controller cache */
  74. val = *reg_addr;
  75. goto out;
  76. }
  77. #endif
  78. /* start read access across the ac97 link */
  79. GSR = GSR_CDONE | GSR_SDONE;
  80. gsr_bits = 0;
  81. val = *reg_addr;
  82. wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_SDONE, 1);
  83. if (!((GSR | gsr_bits) & GSR_SDONE)) {
  84. printk(KERN_ERR "%s: read error (ac97_reg=%x GSR=%#lx)\n",
  85. __FUNCTION__, reg, GSR | gsr_bits);
  86. val = -1;
  87. goto out;
  88. }
  89. /* valid data now */
  90. GSR = GSR_CDONE | GSR_SDONE;
  91. gsr_bits = 0;
  92. val = *reg_addr;
  93. /* but we've just started another cycle... */
  94. wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_SDONE, 1);
  95. out: mutex_unlock(&car_mutex);
  96. return val;
  97. }
  98. static void pxa2xx_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
  99. unsigned short val)
  100. {
  101. volatile u32 *reg_addr;
  102. mutex_lock(&car_mutex);
  103. /* set up primary or secondary codec/modem space */
  104. #ifdef CONFIG_PXA27x
  105. reg_addr = ac97->num ? &SAC_REG_BASE : &PAC_REG_BASE;
  106. #else
  107. if (reg == AC97_GPIO_STATUS)
  108. reg_addr = ac97->num ? &SMC_REG_BASE : &PMC_REG_BASE;
  109. else
  110. reg_addr = ac97->num ? &SAC_REG_BASE : &PAC_REG_BASE;
  111. #endif
  112. reg_addr += (reg >> 1);
  113. GSR = GSR_CDONE | GSR_SDONE;
  114. gsr_bits = 0;
  115. *reg_addr = val;
  116. wait_event_timeout(gsr_wq, (GSR | gsr_bits) & GSR_CDONE, 1);
  117. if (!((GSR | gsr_bits) & GSR_CDONE))
  118. printk(KERN_ERR "%s: write error (ac97_reg=%x GSR=%#lx)\n",
  119. __FUNCTION__, reg, GSR | gsr_bits);
  120. mutex_unlock(&car_mutex);
  121. }
  122. static void pxa2xx_ac97_warm_reset(struct snd_ac97 *ac97)
  123. {
  124. gsr_bits = 0;
  125. #ifdef CONFIG_PXA27x
  126. /* warm reset broken on Bulverde,
  127. so manually keep AC97 reset high */
  128. pxa_gpio_mode(113 | GPIO_OUT | GPIO_DFLT_HIGH);
  129. udelay(10);
  130. GCR |= GCR_WARM_RST;
  131. pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
  132. udelay(500);
  133. #else
  134. GCR |= GCR_WARM_RST | GCR_PRIRDY_IEN | GCR_SECRDY_IEN;
  135. wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
  136. #endif
  137. if (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)))
  138. printk(KERN_INFO "%s: warm reset timeout (GSR=%#lx)\n",
  139. __FUNCTION__, gsr_bits);
  140. GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
  141. GCR |= GCR_SDONE_IE|GCR_CDONE_IE;
  142. }
  143. static void pxa2xx_ac97_cold_reset(struct snd_ac97 *ac97)
  144. {
  145. GCR &= GCR_COLD_RST; /* clear everything but nCRST */
  146. GCR &= ~GCR_COLD_RST; /* then assert nCRST */
  147. gsr_bits = 0;
  148. #ifdef CONFIG_PXA27x
  149. /* PXA27x Developers Manual section 13.5.2.2.1 */
  150. pxa_set_cken(1 << 31, 1);
  151. udelay(5);
  152. pxa_set_cken(1 << 31, 0);
  153. GCR = GCR_COLD_RST;
  154. udelay(50);
  155. #else
  156. GCR = GCR_COLD_RST;
  157. GCR |= GCR_CDONE_IE|GCR_SDONE_IE;
  158. wait_event_timeout(gsr_wq, gsr_bits & (GSR_PCR | GSR_SCR), 1);
  159. #endif
  160. if (!((GSR | gsr_bits) & (GSR_PCR | GSR_SCR)))
  161. printk(KERN_INFO "%s: cold reset timeout (GSR=%#lx)\n",
  162. __FUNCTION__, gsr_bits);
  163. GCR &= ~(GCR_PRIRDY_IEN|GCR_SECRDY_IEN);
  164. GCR |= GCR_SDONE_IE|GCR_CDONE_IE;
  165. }
  166. static irqreturn_t pxa2xx_ac97_irq(int irq, void *dev_id)
  167. {
  168. long status;
  169. status = GSR;
  170. if (status) {
  171. GSR = status;
  172. gsr_bits |= status;
  173. wake_up(&gsr_wq);
  174. #ifdef CONFIG_PXA27x
  175. /* Although we don't use those we still need to clear them
  176. since they tend to spuriously trigger when MMC is used
  177. (hardware bug? go figure)... */
  178. MISR = MISR_EOC;
  179. PISR = PISR_EOC;
  180. MCSR = MCSR_EOC;
  181. #endif
  182. return IRQ_HANDLED;
  183. }
  184. return IRQ_NONE;
  185. }
  186. struct snd_ac97_bus_ops soc_ac97_ops = {
  187. .read = pxa2xx_ac97_read,
  188. .write = pxa2xx_ac97_write,
  189. .warm_reset = pxa2xx_ac97_warm_reset,
  190. .reset = pxa2xx_ac97_cold_reset,
  191. };
  192. static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_stereo_out = {
  193. .name = "AC97 PCM Stereo out",
  194. .dev_addr = __PREG(PCDR),
  195. .drcmr = &DRCMRTXPCDR,
  196. .dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG |
  197. DCMD_BURST32 | DCMD_WIDTH4,
  198. };
  199. static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_stereo_in = {
  200. .name = "AC97 PCM Stereo in",
  201. .dev_addr = __PREG(PCDR),
  202. .drcmr = &DRCMRRXPCDR,
  203. .dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC |
  204. DCMD_BURST32 | DCMD_WIDTH4,
  205. };
  206. static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_aux_mono_out = {
  207. .name = "AC97 Aux PCM (Slot 5) Mono out",
  208. .dev_addr = __PREG(MODR),
  209. .drcmr = &DRCMRTXMODR,
  210. .dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG |
  211. DCMD_BURST16 | DCMD_WIDTH2,
  212. };
  213. static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_aux_mono_in = {
  214. .name = "AC97 Aux PCM (Slot 5) Mono in",
  215. .dev_addr = __PREG(MODR),
  216. .drcmr = &DRCMRRXMODR,
  217. .dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC |
  218. DCMD_BURST16 | DCMD_WIDTH2,
  219. };
  220. static struct pxa2xx_pcm_dma_params pxa2xx_ac97_pcm_mic_mono_in = {
  221. .name = "AC97 Mic PCM (Slot 6) Mono in",
  222. .dev_addr = __PREG(MCDR),
  223. .drcmr = &DRCMRRXMCDR,
  224. .dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC |
  225. DCMD_BURST16 | DCMD_WIDTH2,
  226. };
  227. #ifdef CONFIG_PM
  228. static int pxa2xx_ac97_suspend(struct platform_device *pdev,
  229. struct snd_soc_cpu_dai *dai)
  230. {
  231. GCR |= GCR_ACLINK_OFF;
  232. pxa_set_cken(CKEN2_AC97, 0);
  233. return 0;
  234. }
  235. static int pxa2xx_ac97_resume(struct platform_device *pdev,
  236. struct snd_soc_cpu_dai *dai)
  237. {
  238. pxa_gpio_mode(GPIO31_SYNC_AC97_MD);
  239. pxa_gpio_mode(GPIO30_SDATA_OUT_AC97_MD);
  240. pxa_gpio_mode(GPIO28_BITCLK_AC97_MD);
  241. pxa_gpio_mode(GPIO29_SDATA_IN_AC97_MD);
  242. #ifdef CONFIG_PXA27x
  243. /* Use GPIO 113 as AC97 Reset on Bulverde */
  244. pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
  245. #endif
  246. pxa_set_cken(CKEN2_AC97, 1);
  247. return 0;
  248. }
  249. #else
  250. #define pxa2xx_ac97_suspend NULL
  251. #define pxa2xx_ac97_resume NULL
  252. #endif
  253. static int pxa2xx_ac97_probe(struct platform_device *pdev)
  254. {
  255. int ret;
  256. ret = request_irq(IRQ_AC97, pxa2xx_ac97_irq, IRQF_DISABLED, "AC97", NULL);
  257. if (ret < 0)
  258. goto err;
  259. pxa_gpio_mode(GPIO31_SYNC_AC97_MD);
  260. pxa_gpio_mode(GPIO30_SDATA_OUT_AC97_MD);
  261. pxa_gpio_mode(GPIO28_BITCLK_AC97_MD);
  262. pxa_gpio_mode(GPIO29_SDATA_IN_AC97_MD);
  263. #ifdef CONFIG_PXA27x
  264. /* Use GPIO 113 as AC97 Reset on Bulverde */
  265. pxa_gpio_mode(113 | GPIO_ALT_FN_2_OUT);
  266. #endif
  267. pxa_set_cken(CKEN2_AC97, 1);
  268. return 0;
  269. err:
  270. if (CKEN & CKEN2_AC97) {
  271. GCR |= GCR_ACLINK_OFF;
  272. free_irq(IRQ_AC97, NULL);
  273. pxa_set_cken(CKEN2_AC97, 0);
  274. }
  275. return ret;
  276. }
  277. static void pxa2xx_ac97_remove(struct platform_device *pdev)
  278. {
  279. GCR |= GCR_ACLINK_OFF;
  280. free_irq(IRQ_AC97, NULL);
  281. pxa_set_cken(CKEN2_AC97, 0);
  282. }
  283. static int pxa2xx_ac97_hw_params(struct snd_pcm_substream *substream,
  284. struct snd_pcm_hw_params *params)
  285. {
  286. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  287. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  288. rtd->cpu_dai->dma_data = &pxa2xx_ac97_pcm_stereo_out;
  289. else
  290. rtd->cpu_dai->dma_data = &pxa2xx_ac97_pcm_stereo_in;
  291. return 0;
  292. }
  293. static int pxa2xx_ac97_hw_aux_params(struct snd_pcm_substream *substream,
  294. struct snd_pcm_hw_params *params)
  295. {
  296. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  297. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  298. rtd->cpu_dai->dma_data = &pxa2xx_ac97_pcm_aux_mono_out;
  299. else
  300. rtd->cpu_dai->dma_data = &pxa2xx_ac97_pcm_aux_mono_in;
  301. return 0;
  302. }
  303. static int pxa2xx_ac97_hw_mic_params(struct snd_pcm_substream *substream,
  304. struct snd_pcm_hw_params *params)
  305. {
  306. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  307. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  308. return -ENODEV;
  309. else
  310. rtd->cpu_dai->dma_data = &pxa2xx_ac97_pcm_mic_mono_in;
  311. return 0;
  312. }
  313. /*
  314. * There is only 1 physical AC97 interface for pxa2xx, but it
  315. * has extra fifo's that can be used for aux DACs and ADCs.
  316. */
  317. struct snd_soc_cpu_dai pxa_ac97_dai[] = {
  318. {
  319. .name = "pxa2xx-ac97",
  320. .id = 0,
  321. .type = SND_SOC_DAI_AC97,
  322. .probe = pxa2xx_ac97_probe,
  323. .remove = pxa2xx_ac97_remove,
  324. .suspend = pxa2xx_ac97_suspend,
  325. .resume = pxa2xx_ac97_resume,
  326. .playback = {
  327. .stream_name = "AC97 Playback",
  328. .channels_min = 2,
  329. .channels_max = 2,},
  330. .capture = {
  331. .stream_name = "AC97 Capture",
  332. .channels_min = 2,
  333. .channels_max = 2,},
  334. .ops = {
  335. .hw_params = pxa2xx_ac97_hw_params,},
  336. .caps = {
  337. .num_modes = ARRAY_SIZE(pxa2xx_ac97_modes),
  338. .mode = pxa2xx_ac97_modes,},
  339. },
  340. {
  341. .name = "pxa2xx-ac97-aux",
  342. .id = 1,
  343. .type = SND_SOC_DAI_AC97,
  344. .playback = {
  345. .stream_name = "AC97 Aux Playback",
  346. .channels_min = 1,
  347. .channels_max = 1,},
  348. .capture = {
  349. .stream_name = "AC97 Aux Capture",
  350. .channels_min = 1,
  351. .channels_max = 1,},
  352. .ops = {
  353. .hw_params = pxa2xx_ac97_hw_aux_params,},
  354. .caps = {
  355. .num_modes = ARRAY_SIZE(pxa2xx_ac97_modes),
  356. .mode = pxa2xx_ac97_modes,},
  357. },
  358. {
  359. .name = "pxa2xx-ac97-mic",
  360. .id = 2,
  361. .type = SND_SOC_DAI_AC97,
  362. .capture = {
  363. .stream_name = "AC97 Mic Capture",
  364. .channels_min = 1,
  365. .channels_max = 1,},
  366. .ops = {
  367. .hw_params = pxa2xx_ac97_hw_mic_params,},
  368. .caps = {
  369. .num_modes = ARRAY_SIZE(pxa2xx_ac97_modes),
  370. .mode = pxa2xx_ac97_modes,},},
  371. };
  372. EXPORT_SYMBOL_GPL(pxa_ac97_dai);
  373. EXPORT_SYMBOL_GPL(soc_ac97_ops);
  374. MODULE_AUTHOR("Nicolas Pitre");
  375. MODULE_DESCRIPTION("AC97 driver for the Intel PXA2xx chip");
  376. MODULE_LICENSE("GPL");