ep93xx-ac97.c 11 KB

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