ep93xx-ac97.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * ASoC driver for Cirrus Logic EP93xx AC97 controller.
  3. *
  4. * Copyright (c) 2010 Mika Westerberg
  5. *
  6. * Based on s3c-ac97 ASoC driver by Jaswinder Singh.
  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/delay.h>
  13. #include <linux/io.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/slab.h>
  18. #include <sound/core.h>
  19. #include <sound/ac97_codec.h>
  20. #include <sound/soc.h>
  21. #include <mach/dma.h>
  22. #include "ep93xx-pcm.h"
  23. /*
  24. * Per channel (1-4) registers.
  25. */
  26. #define AC97CH(n) (((n) - 1) * 0x20)
  27. #define AC97DR(n) (AC97CH(n) + 0x0000)
  28. #define AC97RXCR(n) (AC97CH(n) + 0x0004)
  29. #define AC97RXCR_REN BIT(0)
  30. #define AC97RXCR_RX3 BIT(3)
  31. #define AC97RXCR_RX4 BIT(4)
  32. #define AC97RXCR_CM BIT(15)
  33. #define AC97TXCR(n) (AC97CH(n) + 0x0008)
  34. #define AC97TXCR_TEN BIT(0)
  35. #define AC97TXCR_TX3 BIT(3)
  36. #define AC97TXCR_TX4 BIT(4)
  37. #define AC97TXCR_CM BIT(15)
  38. #define AC97SR(n) (AC97CH(n) + 0x000c)
  39. #define AC97SR_TXFE BIT(1)
  40. #define AC97SR_TXUE BIT(6)
  41. #define AC97RISR(n) (AC97CH(n) + 0x0010)
  42. #define AC97ISR(n) (AC97CH(n) + 0x0014)
  43. #define AC97IE(n) (AC97CH(n) + 0x0018)
  44. /*
  45. * Global AC97 controller registers.
  46. */
  47. #define AC97S1DATA 0x0080
  48. #define AC97S2DATA 0x0084
  49. #define AC97S12DATA 0x0088
  50. #define AC97RGIS 0x008c
  51. #define AC97GIS 0x0090
  52. #define AC97IM 0x0094
  53. /*
  54. * Common bits for RGIS, GIS and IM registers.
  55. */
  56. #define AC97_SLOT2RXVALID BIT(1)
  57. #define AC97_CODECREADY BIT(5)
  58. #define AC97_SLOT2TXCOMPLETE BIT(6)
  59. #define AC97EOI 0x0098
  60. #define AC97EOI_WINT BIT(0)
  61. #define AC97EOI_CODECREADY BIT(1)
  62. #define AC97GCR 0x009c
  63. #define AC97GCR_AC97IFE BIT(0)
  64. #define AC97RESET 0x00a0
  65. #define AC97RESET_TIMEDRESET BIT(0)
  66. #define AC97SYNC 0x00a4
  67. #define AC97SYNC_TIMEDSYNC BIT(0)
  68. #define AC97_TIMEOUT msecs_to_jiffies(5)
  69. /**
  70. * struct ep93xx_ac97_info - EP93xx AC97 controller info structure
  71. * @lock: mutex serializing access to the bus (slot 1 & 2 ops)
  72. * @dev: pointer to the platform device dev structure
  73. * @mem: physical memory resource for the registers
  74. * @regs: mapped AC97 controller registers
  75. * @irq: AC97 interrupt number
  76. * @done: bus ops wait here for an interrupt
  77. */
  78. struct ep93xx_ac97_info {
  79. struct mutex lock;
  80. struct device *dev;
  81. struct resource *mem;
  82. void __iomem *regs;
  83. int irq;
  84. struct completion done;
  85. };
  86. /* currently ALSA only supports a single AC97 device */
  87. static struct ep93xx_ac97_info *ep93xx_ac97_info;
  88. static struct ep93xx_pcm_dma_params ep93xx_ac97_pcm_out = {
  89. .name = "ac97-pcm-out",
  90. .dma_port = EP93XX_DMA_AAC1,
  91. };
  92. static struct ep93xx_pcm_dma_params ep93xx_ac97_pcm_in = {
  93. .name = "ac97-pcm-in",
  94. .dma_port = EP93XX_DMA_AAC1,
  95. };
  96. static inline unsigned ep93xx_ac97_read_reg(struct ep93xx_ac97_info *info,
  97. unsigned reg)
  98. {
  99. return __raw_readl(info->regs + reg);
  100. }
  101. static inline void ep93xx_ac97_write_reg(struct ep93xx_ac97_info *info,
  102. unsigned reg, unsigned val)
  103. {
  104. __raw_writel(val, info->regs + reg);
  105. }
  106. static unsigned short ep93xx_ac97_read(struct snd_ac97 *ac97,
  107. unsigned short reg)
  108. {
  109. struct ep93xx_ac97_info *info = ep93xx_ac97_info;
  110. unsigned short val;
  111. mutex_lock(&info->lock);
  112. ep93xx_ac97_write_reg(info, AC97S1DATA, reg);
  113. ep93xx_ac97_write_reg(info, AC97IM, AC97_SLOT2RXVALID);
  114. if (!wait_for_completion_timeout(&info->done, AC97_TIMEOUT)) {
  115. dev_warn(info->dev, "timeout reading register %x\n", reg);
  116. mutex_unlock(&info->lock);
  117. return -ETIMEDOUT;
  118. }
  119. val = (unsigned short)ep93xx_ac97_read_reg(info, AC97S2DATA);
  120. mutex_unlock(&info->lock);
  121. return val;
  122. }
  123. static void ep93xx_ac97_write(struct snd_ac97 *ac97,
  124. unsigned short reg,
  125. unsigned short val)
  126. {
  127. struct ep93xx_ac97_info *info = ep93xx_ac97_info;
  128. mutex_lock(&info->lock);
  129. /*
  130. * Writes to the codec need to be done so that slot 2 is filled in
  131. * before slot 1.
  132. */
  133. ep93xx_ac97_write_reg(info, AC97S2DATA, val);
  134. ep93xx_ac97_write_reg(info, AC97S1DATA, reg);
  135. ep93xx_ac97_write_reg(info, AC97IM, AC97_SLOT2TXCOMPLETE);
  136. if (!wait_for_completion_timeout(&info->done, AC97_TIMEOUT))
  137. dev_warn(info->dev, "timeout writing register %x\n", reg);
  138. mutex_unlock(&info->lock);
  139. }
  140. static void ep93xx_ac97_warm_reset(struct snd_ac97 *ac97)
  141. {
  142. struct ep93xx_ac97_info *info = ep93xx_ac97_info;
  143. mutex_lock(&info->lock);
  144. /*
  145. * We are assuming that before this functions gets called, the codec
  146. * BIT_CLK is stopped by forcing the codec into powerdown mode. We can
  147. * control the SYNC signal directly via AC97SYNC register. Using
  148. * TIMEDSYNC the controller will keep the SYNC high > 1us.
  149. */
  150. ep93xx_ac97_write_reg(info, AC97SYNC, AC97SYNC_TIMEDSYNC);
  151. ep93xx_ac97_write_reg(info, AC97IM, AC97_CODECREADY);
  152. if (!wait_for_completion_timeout(&info->done, AC97_TIMEOUT))
  153. dev_warn(info->dev, "codec warm reset timeout\n");
  154. mutex_unlock(&info->lock);
  155. }
  156. static void ep93xx_ac97_cold_reset(struct snd_ac97 *ac97)
  157. {
  158. struct ep93xx_ac97_info *info = ep93xx_ac97_info;
  159. mutex_lock(&info->lock);
  160. /*
  161. * For doing cold reset, we disable the AC97 controller interface, clear
  162. * WINT and CODECREADY bits, and finally enable the interface again.
  163. */
  164. ep93xx_ac97_write_reg(info, AC97GCR, 0);
  165. ep93xx_ac97_write_reg(info, AC97EOI, AC97EOI_CODECREADY | AC97EOI_WINT);
  166. ep93xx_ac97_write_reg(info, AC97GCR, AC97GCR_AC97IFE);
  167. /*
  168. * Now, assert the reset and wait for the codec to become ready.
  169. */
  170. ep93xx_ac97_write_reg(info, AC97RESET, AC97RESET_TIMEDRESET);
  171. ep93xx_ac97_write_reg(info, AC97IM, AC97_CODECREADY);
  172. if (!wait_for_completion_timeout(&info->done, AC97_TIMEOUT))
  173. dev_warn(info->dev, "codec cold reset timeout\n");
  174. /*
  175. * Give the codec some time to come fully out from the reset. This way
  176. * we ensure that the subsequent reads/writes will work.
  177. */
  178. usleep_range(15000, 20000);
  179. mutex_unlock(&info->lock);
  180. }
  181. static irqreturn_t ep93xx_ac97_interrupt(int irq, void *dev_id)
  182. {
  183. struct ep93xx_ac97_info *info = dev_id;
  184. unsigned status, mask;
  185. /*
  186. * Just mask out the interrupt and wake up the waiting thread.
  187. * Interrupts are cleared via reading/writing to slot 1 & 2 registers by
  188. * the waiting thread.
  189. */
  190. status = ep93xx_ac97_read_reg(info, AC97GIS);
  191. mask = ep93xx_ac97_read_reg(info, AC97IM);
  192. mask &= ~status;
  193. ep93xx_ac97_write_reg(info, AC97IM, mask);
  194. complete(&info->done);
  195. return IRQ_HANDLED;
  196. }
  197. struct snd_ac97_bus_ops soc_ac97_ops = {
  198. .read = ep93xx_ac97_read,
  199. .write = ep93xx_ac97_write,
  200. .reset = ep93xx_ac97_cold_reset,
  201. .warm_reset = ep93xx_ac97_warm_reset,
  202. };
  203. EXPORT_SYMBOL_GPL(soc_ac97_ops);
  204. static int ep93xx_ac97_trigger(struct snd_pcm_substream *substream,
  205. int cmd, struct snd_soc_dai *dai)
  206. {
  207. struct ep93xx_ac97_info *info = snd_soc_dai_get_drvdata(dai);
  208. unsigned v = 0;
  209. switch (cmd) {
  210. case SNDRV_PCM_TRIGGER_START:
  211. case SNDRV_PCM_TRIGGER_RESUME:
  212. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  213. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  214. /*
  215. * Enable compact mode, TX slots 3 & 4, and the TX FIFO
  216. * itself.
  217. */
  218. v |= AC97TXCR_CM;
  219. v |= AC97TXCR_TX3 | AC97TXCR_TX4;
  220. v |= AC97TXCR_TEN;
  221. ep93xx_ac97_write_reg(info, AC97TXCR(1), v);
  222. } else {
  223. /*
  224. * Enable compact mode, RX slots 3 & 4, and the RX FIFO
  225. * itself.
  226. */
  227. v |= AC97RXCR_CM;
  228. v |= AC97RXCR_RX3 | AC97RXCR_RX4;
  229. v |= AC97RXCR_REN;
  230. ep93xx_ac97_write_reg(info, AC97RXCR(1), v);
  231. }
  232. break;
  233. case SNDRV_PCM_TRIGGER_STOP:
  234. case SNDRV_PCM_TRIGGER_SUSPEND:
  235. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  236. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  237. /*
  238. * As per Cirrus EP93xx errata described below:
  239. *
  240. * http://www.cirrus.com/en/pubs/errata/ER667E2B.pdf
  241. *
  242. * we will wait for the TX FIFO to be empty before
  243. * clearing the TEN bit.
  244. */
  245. unsigned long timeout = jiffies + AC97_TIMEOUT;
  246. do {
  247. v = ep93xx_ac97_read_reg(info, AC97SR(1));
  248. if (time_after(jiffies, timeout)) {
  249. dev_warn(info->dev, "TX timeout\n");
  250. break;
  251. }
  252. } while (!(v & (AC97SR_TXFE | AC97SR_TXUE)));
  253. /* disable the TX FIFO */
  254. ep93xx_ac97_write_reg(info, AC97TXCR(1), 0);
  255. } else {
  256. /* disable the RX FIFO */
  257. ep93xx_ac97_write_reg(info, AC97RXCR(1), 0);
  258. }
  259. break;
  260. default:
  261. dev_warn(info->dev, "unknown command %d\n", cmd);
  262. return -EINVAL;
  263. }
  264. return 0;
  265. }
  266. static int ep93xx_ac97_startup(struct snd_pcm_substream *substream,
  267. struct snd_soc_dai *dai)
  268. {
  269. struct ep93xx_pcm_dma_params *dma_data;
  270. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  271. dma_data = &ep93xx_ac97_pcm_out;
  272. else
  273. dma_data = &ep93xx_ac97_pcm_in;
  274. snd_soc_dai_set_dma_data(dai, substream, dma_data);
  275. return 0;
  276. }
  277. static const struct snd_soc_dai_ops ep93xx_ac97_dai_ops = {
  278. .startup = ep93xx_ac97_startup,
  279. .trigger = ep93xx_ac97_trigger,
  280. };
  281. static struct snd_soc_dai_driver ep93xx_ac97_dai = {
  282. .name = "ep93xx-ac97",
  283. .id = 0,
  284. .ac97_control = 1,
  285. .playback = {
  286. .stream_name = "AC97 Playback",
  287. .channels_min = 2,
  288. .channels_max = 2,
  289. .rates = SNDRV_PCM_RATE_8000_48000,
  290. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  291. },
  292. .capture = {
  293. .stream_name = "AC97 Capture",
  294. .channels_min = 2,
  295. .channels_max = 2,
  296. .rates = SNDRV_PCM_RATE_8000_48000,
  297. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  298. },
  299. .ops = &ep93xx_ac97_dai_ops,
  300. };
  301. static int __devinit ep93xx_ac97_probe(struct platform_device *pdev)
  302. {
  303. struct ep93xx_ac97_info *info;
  304. int ret;
  305. info = kzalloc(sizeof(struct ep93xx_ac97_info), GFP_KERNEL);
  306. if (!info)
  307. return -ENOMEM;
  308. dev_set_drvdata(&pdev->dev, info);
  309. mutex_init(&info->lock);
  310. init_completion(&info->done);
  311. info->dev = &pdev->dev;
  312. info->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  313. if (!info->mem) {
  314. ret = -ENXIO;
  315. goto fail_free_info;
  316. }
  317. info->irq = platform_get_irq(pdev, 0);
  318. if (!info->irq) {
  319. ret = -ENXIO;
  320. goto fail_free_info;
  321. }
  322. if (!request_mem_region(info->mem->start, resource_size(info->mem),
  323. pdev->name)) {
  324. ret = -EBUSY;
  325. goto fail_free_info;
  326. }
  327. info->regs = ioremap(info->mem->start, resource_size(info->mem));
  328. if (!info->regs) {
  329. ret = -ENOMEM;
  330. goto fail_release_mem;
  331. }
  332. ret = request_irq(info->irq, ep93xx_ac97_interrupt, IRQF_TRIGGER_HIGH,
  333. pdev->name, info);
  334. if (ret)
  335. goto fail_unmap_mem;
  336. ep93xx_ac97_info = info;
  337. platform_set_drvdata(pdev, info);
  338. ret = snd_soc_register_dai(&pdev->dev, &ep93xx_ac97_dai);
  339. if (ret)
  340. goto fail_free_irq;
  341. return 0;
  342. fail_free_irq:
  343. platform_set_drvdata(pdev, NULL);
  344. free_irq(info->irq, info);
  345. fail_unmap_mem:
  346. iounmap(info->regs);
  347. fail_release_mem:
  348. release_mem_region(info->mem->start, resource_size(info->mem));
  349. fail_free_info:
  350. kfree(info);
  351. return ret;
  352. }
  353. static int __devexit ep93xx_ac97_remove(struct platform_device *pdev)
  354. {
  355. struct ep93xx_ac97_info *info = platform_get_drvdata(pdev);
  356. snd_soc_unregister_dai(&pdev->dev);
  357. /* disable the AC97 controller */
  358. ep93xx_ac97_write_reg(info, AC97GCR, 0);
  359. free_irq(info->irq, info);
  360. iounmap(info->regs);
  361. release_mem_region(info->mem->start, resource_size(info->mem));
  362. platform_set_drvdata(pdev, NULL);
  363. kfree(info);
  364. return 0;
  365. }
  366. static struct platform_driver ep93xx_ac97_driver = {
  367. .probe = ep93xx_ac97_probe,
  368. .remove = __devexit_p(ep93xx_ac97_remove),
  369. .driver = {
  370. .name = "ep93xx-ac97",
  371. .owner = THIS_MODULE,
  372. },
  373. };
  374. module_platform_driver(ep93xx_ac97_driver);
  375. MODULE_DESCRIPTION("EP93xx AC97 ASoC Driver");
  376. MODULE_AUTHOR("Mika Westerberg <mika.westerberg@iki.fi>");
  377. MODULE_LICENSE("GPL");
  378. MODULE_ALIAS("platform:ep93xx-ac97");