ep93xx-ac97.c 11 KB

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