ac97c.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. /*
  2. * Driver for Atmel AC97C
  3. *
  4. * Copyright (C) 2005-2009 Atmel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/delay.h>
  12. #include <linux/bitmap.h>
  13. #include <linux/device.h>
  14. #include <linux/dmaengine.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/atmel_pdc.h>
  17. #include <linux/init.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/mutex.h>
  22. #include <linux/gpio.h>
  23. #include <linux/types.h>
  24. #include <linux/io.h>
  25. #include <linux/of.h>
  26. #include <linux/of_gpio.h>
  27. #include <linux/of_device.h>
  28. #include <sound/core.h>
  29. #include <sound/initval.h>
  30. #include <sound/pcm.h>
  31. #include <sound/pcm_params.h>
  32. #include <sound/ac97_codec.h>
  33. #include <sound/atmel-ac97c.h>
  34. #include <sound/memalloc.h>
  35. #include <linux/platform_data/dma-dw.h>
  36. #include <linux/dma/dw.h>
  37. #ifdef CONFIG_AVR32
  38. #include <mach/cpu.h>
  39. #else
  40. #define cpu_is_at32ap7000() 0
  41. #endif
  42. #include "ac97c.h"
  43. enum {
  44. DMA_TX_READY = 0,
  45. DMA_RX_READY,
  46. DMA_TX_CHAN_PRESENT,
  47. DMA_RX_CHAN_PRESENT,
  48. };
  49. /* Serialize access to opened variable */
  50. static DEFINE_MUTEX(opened_mutex);
  51. struct atmel_ac97c_dma {
  52. struct dma_chan *rx_chan;
  53. struct dma_chan *tx_chan;
  54. };
  55. struct atmel_ac97c {
  56. struct clk *pclk;
  57. struct platform_device *pdev;
  58. struct atmel_ac97c_dma dma;
  59. struct snd_pcm_substream *playback_substream;
  60. struct snd_pcm_substream *capture_substream;
  61. struct snd_card *card;
  62. struct snd_pcm *pcm;
  63. struct snd_ac97 *ac97;
  64. struct snd_ac97_bus *ac97_bus;
  65. u64 cur_format;
  66. unsigned int cur_rate;
  67. unsigned long flags;
  68. int playback_period, capture_period;
  69. /* Serialize access to opened variable */
  70. spinlock_t lock;
  71. void __iomem *regs;
  72. int irq;
  73. int opened;
  74. int reset_pin;
  75. };
  76. #define get_chip(card) ((struct atmel_ac97c *)(card)->private_data)
  77. #define ac97c_writel(chip, reg, val) \
  78. __raw_writel((val), (chip)->regs + AC97C_##reg)
  79. #define ac97c_readl(chip, reg) \
  80. __raw_readl((chip)->regs + AC97C_##reg)
  81. /* This function is called by the DMA driver. */
  82. static void atmel_ac97c_dma_playback_period_done(void *arg)
  83. {
  84. struct atmel_ac97c *chip = arg;
  85. snd_pcm_period_elapsed(chip->playback_substream);
  86. }
  87. static void atmel_ac97c_dma_capture_period_done(void *arg)
  88. {
  89. struct atmel_ac97c *chip = arg;
  90. snd_pcm_period_elapsed(chip->capture_substream);
  91. }
  92. static int atmel_ac97c_prepare_dma(struct atmel_ac97c *chip,
  93. struct snd_pcm_substream *substream,
  94. enum dma_transfer_direction direction)
  95. {
  96. struct dma_chan *chan;
  97. struct dw_cyclic_desc *cdesc;
  98. struct snd_pcm_runtime *runtime = substream->runtime;
  99. unsigned long buffer_len, period_len;
  100. /*
  101. * We don't do DMA on "complex" transfers, i.e. with
  102. * non-halfword-aligned buffers or lengths.
  103. */
  104. if (runtime->dma_addr & 1 || runtime->buffer_size & 1) {
  105. dev_dbg(&chip->pdev->dev, "too complex transfer\n");
  106. return -EINVAL;
  107. }
  108. if (direction == DMA_MEM_TO_DEV)
  109. chan = chip->dma.tx_chan;
  110. else
  111. chan = chip->dma.rx_chan;
  112. buffer_len = frames_to_bytes(runtime, runtime->buffer_size);
  113. period_len = frames_to_bytes(runtime, runtime->period_size);
  114. cdesc = dw_dma_cyclic_prep(chan, runtime->dma_addr, buffer_len,
  115. period_len, direction);
  116. if (IS_ERR(cdesc)) {
  117. dev_dbg(&chip->pdev->dev, "could not prepare cyclic DMA\n");
  118. return PTR_ERR(cdesc);
  119. }
  120. if (direction == DMA_MEM_TO_DEV) {
  121. cdesc->period_callback = atmel_ac97c_dma_playback_period_done;
  122. set_bit(DMA_TX_READY, &chip->flags);
  123. } else {
  124. cdesc->period_callback = atmel_ac97c_dma_capture_period_done;
  125. set_bit(DMA_RX_READY, &chip->flags);
  126. }
  127. cdesc->period_callback_param = chip;
  128. return 0;
  129. }
  130. static struct snd_pcm_hardware atmel_ac97c_hw = {
  131. .info = (SNDRV_PCM_INFO_MMAP
  132. | SNDRV_PCM_INFO_MMAP_VALID
  133. | SNDRV_PCM_INFO_INTERLEAVED
  134. | SNDRV_PCM_INFO_BLOCK_TRANSFER
  135. | SNDRV_PCM_INFO_JOINT_DUPLEX
  136. | SNDRV_PCM_INFO_RESUME
  137. | SNDRV_PCM_INFO_PAUSE),
  138. .formats = (SNDRV_PCM_FMTBIT_S16_BE
  139. | SNDRV_PCM_FMTBIT_S16_LE),
  140. .rates = (SNDRV_PCM_RATE_CONTINUOUS),
  141. .rate_min = 4000,
  142. .rate_max = 48000,
  143. .channels_min = 1,
  144. .channels_max = 2,
  145. .buffer_bytes_max = 2 * 2 * 64 * 2048,
  146. .period_bytes_min = 4096,
  147. .period_bytes_max = 4096,
  148. .periods_min = 6,
  149. .periods_max = 64,
  150. };
  151. static int atmel_ac97c_playback_open(struct snd_pcm_substream *substream)
  152. {
  153. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  154. struct snd_pcm_runtime *runtime = substream->runtime;
  155. mutex_lock(&opened_mutex);
  156. chip->opened++;
  157. runtime->hw = atmel_ac97c_hw;
  158. if (chip->cur_rate) {
  159. runtime->hw.rate_min = chip->cur_rate;
  160. runtime->hw.rate_max = chip->cur_rate;
  161. }
  162. if (chip->cur_format)
  163. runtime->hw.formats = pcm_format_to_bits(chip->cur_format);
  164. mutex_unlock(&opened_mutex);
  165. chip->playback_substream = substream;
  166. return 0;
  167. }
  168. static int atmel_ac97c_capture_open(struct snd_pcm_substream *substream)
  169. {
  170. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  171. struct snd_pcm_runtime *runtime = substream->runtime;
  172. mutex_lock(&opened_mutex);
  173. chip->opened++;
  174. runtime->hw = atmel_ac97c_hw;
  175. if (chip->cur_rate) {
  176. runtime->hw.rate_min = chip->cur_rate;
  177. runtime->hw.rate_max = chip->cur_rate;
  178. }
  179. if (chip->cur_format)
  180. runtime->hw.formats = pcm_format_to_bits(chip->cur_format);
  181. mutex_unlock(&opened_mutex);
  182. chip->capture_substream = substream;
  183. return 0;
  184. }
  185. static int atmel_ac97c_playback_close(struct snd_pcm_substream *substream)
  186. {
  187. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  188. mutex_lock(&opened_mutex);
  189. chip->opened--;
  190. if (!chip->opened) {
  191. chip->cur_rate = 0;
  192. chip->cur_format = 0;
  193. }
  194. mutex_unlock(&opened_mutex);
  195. chip->playback_substream = NULL;
  196. return 0;
  197. }
  198. static int atmel_ac97c_capture_close(struct snd_pcm_substream *substream)
  199. {
  200. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  201. mutex_lock(&opened_mutex);
  202. chip->opened--;
  203. if (!chip->opened) {
  204. chip->cur_rate = 0;
  205. chip->cur_format = 0;
  206. }
  207. mutex_unlock(&opened_mutex);
  208. chip->capture_substream = NULL;
  209. return 0;
  210. }
  211. static int atmel_ac97c_playback_hw_params(struct snd_pcm_substream *substream,
  212. struct snd_pcm_hw_params *hw_params)
  213. {
  214. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  215. int retval;
  216. retval = snd_pcm_lib_malloc_pages(substream,
  217. params_buffer_bytes(hw_params));
  218. if (retval < 0)
  219. return retval;
  220. /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
  221. if (cpu_is_at32ap7000()) {
  222. /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
  223. if (retval == 1)
  224. if (test_and_clear_bit(DMA_TX_READY, &chip->flags))
  225. dw_dma_cyclic_free(chip->dma.tx_chan);
  226. }
  227. /* Set restrictions to params. */
  228. mutex_lock(&opened_mutex);
  229. chip->cur_rate = params_rate(hw_params);
  230. chip->cur_format = params_format(hw_params);
  231. mutex_unlock(&opened_mutex);
  232. return retval;
  233. }
  234. static int atmel_ac97c_capture_hw_params(struct snd_pcm_substream *substream,
  235. struct snd_pcm_hw_params *hw_params)
  236. {
  237. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  238. int retval;
  239. retval = snd_pcm_lib_malloc_pages(substream,
  240. params_buffer_bytes(hw_params));
  241. if (retval < 0)
  242. return retval;
  243. /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
  244. if (cpu_is_at32ap7000() && retval == 1)
  245. if (test_and_clear_bit(DMA_RX_READY, &chip->flags))
  246. dw_dma_cyclic_free(chip->dma.rx_chan);
  247. /* Set restrictions to params. */
  248. mutex_lock(&opened_mutex);
  249. chip->cur_rate = params_rate(hw_params);
  250. chip->cur_format = params_format(hw_params);
  251. mutex_unlock(&opened_mutex);
  252. return retval;
  253. }
  254. static int atmel_ac97c_playback_hw_free(struct snd_pcm_substream *substream)
  255. {
  256. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  257. if (cpu_is_at32ap7000()) {
  258. if (test_and_clear_bit(DMA_TX_READY, &chip->flags))
  259. dw_dma_cyclic_free(chip->dma.tx_chan);
  260. }
  261. return snd_pcm_lib_free_pages(substream);
  262. }
  263. static int atmel_ac97c_capture_hw_free(struct snd_pcm_substream *substream)
  264. {
  265. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  266. if (cpu_is_at32ap7000()) {
  267. if (test_and_clear_bit(DMA_RX_READY, &chip->flags))
  268. dw_dma_cyclic_free(chip->dma.rx_chan);
  269. }
  270. return snd_pcm_lib_free_pages(substream);
  271. }
  272. static int atmel_ac97c_playback_prepare(struct snd_pcm_substream *substream)
  273. {
  274. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  275. struct snd_pcm_runtime *runtime = substream->runtime;
  276. int block_size = frames_to_bytes(runtime, runtime->period_size);
  277. unsigned long word = ac97c_readl(chip, OCA);
  278. int retval;
  279. chip->playback_period = 0;
  280. word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
  281. /* assign channels to AC97C channel A */
  282. switch (runtime->channels) {
  283. case 1:
  284. word |= AC97C_CH_ASSIGN(PCM_LEFT, A);
  285. break;
  286. case 2:
  287. word |= AC97C_CH_ASSIGN(PCM_LEFT, A)
  288. | AC97C_CH_ASSIGN(PCM_RIGHT, A);
  289. break;
  290. default:
  291. /* TODO: support more than two channels */
  292. return -EINVAL;
  293. }
  294. ac97c_writel(chip, OCA, word);
  295. /* configure sample format and size */
  296. word = ac97c_readl(chip, CAMR);
  297. if (chip->opened <= 1)
  298. word = AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
  299. else
  300. word |= AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
  301. switch (runtime->format) {
  302. case SNDRV_PCM_FORMAT_S16_LE:
  303. if (cpu_is_at32ap7000())
  304. word |= AC97C_CMR_CEM_LITTLE;
  305. break;
  306. case SNDRV_PCM_FORMAT_S16_BE: /* fall through */
  307. word &= ~(AC97C_CMR_CEM_LITTLE);
  308. break;
  309. default:
  310. word = ac97c_readl(chip, OCA);
  311. word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
  312. ac97c_writel(chip, OCA, word);
  313. return -EINVAL;
  314. }
  315. /* Enable underrun interrupt on channel A */
  316. word |= AC97C_CSR_UNRUN;
  317. ac97c_writel(chip, CAMR, word);
  318. /* Enable channel A event interrupt */
  319. word = ac97c_readl(chip, IMR);
  320. word |= AC97C_SR_CAEVT;
  321. ac97c_writel(chip, IER, word);
  322. /* set variable rate if needed */
  323. if (runtime->rate != 48000) {
  324. word = ac97c_readl(chip, MR);
  325. word |= AC97C_MR_VRA;
  326. ac97c_writel(chip, MR, word);
  327. } else {
  328. word = ac97c_readl(chip, MR);
  329. word &= ~(AC97C_MR_VRA);
  330. ac97c_writel(chip, MR, word);
  331. }
  332. retval = snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE,
  333. runtime->rate);
  334. if (retval)
  335. dev_dbg(&chip->pdev->dev, "could not set rate %d Hz\n",
  336. runtime->rate);
  337. if (cpu_is_at32ap7000()) {
  338. if (!test_bit(DMA_TX_READY, &chip->flags))
  339. retval = atmel_ac97c_prepare_dma(chip, substream,
  340. DMA_MEM_TO_DEV);
  341. } else {
  342. /* Initialize and start the PDC */
  343. writel(runtime->dma_addr, chip->regs + ATMEL_PDC_TPR);
  344. writel(block_size / 2, chip->regs + ATMEL_PDC_TCR);
  345. writel(runtime->dma_addr + block_size,
  346. chip->regs + ATMEL_PDC_TNPR);
  347. writel(block_size / 2, chip->regs + ATMEL_PDC_TNCR);
  348. }
  349. return retval;
  350. }
  351. static int atmel_ac97c_capture_prepare(struct snd_pcm_substream *substream)
  352. {
  353. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  354. struct snd_pcm_runtime *runtime = substream->runtime;
  355. int block_size = frames_to_bytes(runtime, runtime->period_size);
  356. unsigned long word = ac97c_readl(chip, ICA);
  357. int retval;
  358. chip->capture_period = 0;
  359. word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
  360. /* assign channels to AC97C channel A */
  361. switch (runtime->channels) {
  362. case 1:
  363. word |= AC97C_CH_ASSIGN(PCM_LEFT, A);
  364. break;
  365. case 2:
  366. word |= AC97C_CH_ASSIGN(PCM_LEFT, A)
  367. | AC97C_CH_ASSIGN(PCM_RIGHT, A);
  368. break;
  369. default:
  370. /* TODO: support more than two channels */
  371. return -EINVAL;
  372. }
  373. ac97c_writel(chip, ICA, word);
  374. /* configure sample format and size */
  375. word = ac97c_readl(chip, CAMR);
  376. if (chip->opened <= 1)
  377. word = AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
  378. else
  379. word |= AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
  380. switch (runtime->format) {
  381. case SNDRV_PCM_FORMAT_S16_LE:
  382. if (cpu_is_at32ap7000())
  383. word |= AC97C_CMR_CEM_LITTLE;
  384. break;
  385. case SNDRV_PCM_FORMAT_S16_BE: /* fall through */
  386. word &= ~(AC97C_CMR_CEM_LITTLE);
  387. break;
  388. default:
  389. word = ac97c_readl(chip, ICA);
  390. word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
  391. ac97c_writel(chip, ICA, word);
  392. return -EINVAL;
  393. }
  394. /* Enable overrun interrupt on channel A */
  395. word |= AC97C_CSR_OVRUN;
  396. ac97c_writel(chip, CAMR, word);
  397. /* Enable channel A event interrupt */
  398. word = ac97c_readl(chip, IMR);
  399. word |= AC97C_SR_CAEVT;
  400. ac97c_writel(chip, IER, word);
  401. /* set variable rate if needed */
  402. if (runtime->rate != 48000) {
  403. word = ac97c_readl(chip, MR);
  404. word |= AC97C_MR_VRA;
  405. ac97c_writel(chip, MR, word);
  406. } else {
  407. word = ac97c_readl(chip, MR);
  408. word &= ~(AC97C_MR_VRA);
  409. ac97c_writel(chip, MR, word);
  410. }
  411. retval = snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE,
  412. runtime->rate);
  413. if (retval)
  414. dev_dbg(&chip->pdev->dev, "could not set rate %d Hz\n",
  415. runtime->rate);
  416. if (cpu_is_at32ap7000()) {
  417. if (!test_bit(DMA_RX_READY, &chip->flags))
  418. retval = atmel_ac97c_prepare_dma(chip, substream,
  419. DMA_DEV_TO_MEM);
  420. } else {
  421. /* Initialize and start the PDC */
  422. writel(runtime->dma_addr, chip->regs + ATMEL_PDC_RPR);
  423. writel(block_size / 2, chip->regs + ATMEL_PDC_RCR);
  424. writel(runtime->dma_addr + block_size,
  425. chip->regs + ATMEL_PDC_RNPR);
  426. writel(block_size / 2, chip->regs + ATMEL_PDC_RNCR);
  427. }
  428. return retval;
  429. }
  430. static int
  431. atmel_ac97c_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  432. {
  433. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  434. unsigned long camr, ptcr = 0;
  435. int retval = 0;
  436. camr = ac97c_readl(chip, CAMR);
  437. switch (cmd) {
  438. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
  439. case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
  440. case SNDRV_PCM_TRIGGER_START:
  441. if (cpu_is_at32ap7000()) {
  442. retval = dw_dma_cyclic_start(chip->dma.tx_chan);
  443. if (retval)
  444. goto out;
  445. } else {
  446. ptcr = ATMEL_PDC_TXTEN;
  447. }
  448. camr |= AC97C_CMR_CENA | AC97C_CSR_ENDTX;
  449. break;
  450. case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
  451. case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
  452. case SNDRV_PCM_TRIGGER_STOP:
  453. if (cpu_is_at32ap7000())
  454. dw_dma_cyclic_stop(chip->dma.tx_chan);
  455. else
  456. ptcr |= ATMEL_PDC_TXTDIS;
  457. if (chip->opened <= 1)
  458. camr &= ~AC97C_CMR_CENA;
  459. break;
  460. default:
  461. retval = -EINVAL;
  462. goto out;
  463. }
  464. ac97c_writel(chip, CAMR, camr);
  465. if (!cpu_is_at32ap7000())
  466. writel(ptcr, chip->regs + ATMEL_PDC_PTCR);
  467. out:
  468. return retval;
  469. }
  470. static int
  471. atmel_ac97c_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  472. {
  473. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  474. unsigned long camr, ptcr = 0;
  475. int retval = 0;
  476. camr = ac97c_readl(chip, CAMR);
  477. ptcr = readl(chip->regs + ATMEL_PDC_PTSR);
  478. switch (cmd) {
  479. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
  480. case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
  481. case SNDRV_PCM_TRIGGER_START:
  482. if (cpu_is_at32ap7000()) {
  483. retval = dw_dma_cyclic_start(chip->dma.rx_chan);
  484. if (retval)
  485. goto out;
  486. } else {
  487. ptcr = ATMEL_PDC_RXTEN;
  488. }
  489. camr |= AC97C_CMR_CENA | AC97C_CSR_ENDRX;
  490. break;
  491. case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
  492. case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
  493. case SNDRV_PCM_TRIGGER_STOP:
  494. if (cpu_is_at32ap7000())
  495. dw_dma_cyclic_stop(chip->dma.rx_chan);
  496. else
  497. ptcr |= (ATMEL_PDC_RXTDIS);
  498. if (chip->opened <= 1)
  499. camr &= ~AC97C_CMR_CENA;
  500. break;
  501. default:
  502. retval = -EINVAL;
  503. break;
  504. }
  505. ac97c_writel(chip, CAMR, camr);
  506. if (!cpu_is_at32ap7000())
  507. writel(ptcr, chip->regs + ATMEL_PDC_PTCR);
  508. out:
  509. return retval;
  510. }
  511. static snd_pcm_uframes_t
  512. atmel_ac97c_playback_pointer(struct snd_pcm_substream *substream)
  513. {
  514. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  515. struct snd_pcm_runtime *runtime = substream->runtime;
  516. snd_pcm_uframes_t frames;
  517. unsigned long bytes;
  518. if (cpu_is_at32ap7000())
  519. bytes = dw_dma_get_src_addr(chip->dma.tx_chan);
  520. else
  521. bytes = readl(chip->regs + ATMEL_PDC_TPR);
  522. bytes -= runtime->dma_addr;
  523. frames = bytes_to_frames(runtime, bytes);
  524. if (frames >= runtime->buffer_size)
  525. frames -= runtime->buffer_size;
  526. return frames;
  527. }
  528. static snd_pcm_uframes_t
  529. atmel_ac97c_capture_pointer(struct snd_pcm_substream *substream)
  530. {
  531. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  532. struct snd_pcm_runtime *runtime = substream->runtime;
  533. snd_pcm_uframes_t frames;
  534. unsigned long bytes;
  535. if (cpu_is_at32ap7000())
  536. bytes = dw_dma_get_dst_addr(chip->dma.rx_chan);
  537. else
  538. bytes = readl(chip->regs + ATMEL_PDC_RPR);
  539. bytes -= runtime->dma_addr;
  540. frames = bytes_to_frames(runtime, bytes);
  541. if (frames >= runtime->buffer_size)
  542. frames -= runtime->buffer_size;
  543. return frames;
  544. }
  545. static struct snd_pcm_ops atmel_ac97_playback_ops = {
  546. .open = atmel_ac97c_playback_open,
  547. .close = atmel_ac97c_playback_close,
  548. .ioctl = snd_pcm_lib_ioctl,
  549. .hw_params = atmel_ac97c_playback_hw_params,
  550. .hw_free = atmel_ac97c_playback_hw_free,
  551. .prepare = atmel_ac97c_playback_prepare,
  552. .trigger = atmel_ac97c_playback_trigger,
  553. .pointer = atmel_ac97c_playback_pointer,
  554. };
  555. static struct snd_pcm_ops atmel_ac97_capture_ops = {
  556. .open = atmel_ac97c_capture_open,
  557. .close = atmel_ac97c_capture_close,
  558. .ioctl = snd_pcm_lib_ioctl,
  559. .hw_params = atmel_ac97c_capture_hw_params,
  560. .hw_free = atmel_ac97c_capture_hw_free,
  561. .prepare = atmel_ac97c_capture_prepare,
  562. .trigger = atmel_ac97c_capture_trigger,
  563. .pointer = atmel_ac97c_capture_pointer,
  564. };
  565. static irqreturn_t atmel_ac97c_interrupt(int irq, void *dev)
  566. {
  567. struct atmel_ac97c *chip = (struct atmel_ac97c *)dev;
  568. irqreturn_t retval = IRQ_NONE;
  569. u32 sr = ac97c_readl(chip, SR);
  570. u32 casr = ac97c_readl(chip, CASR);
  571. u32 cosr = ac97c_readl(chip, COSR);
  572. u32 camr = ac97c_readl(chip, CAMR);
  573. if (sr & AC97C_SR_CAEVT) {
  574. struct snd_pcm_runtime *runtime;
  575. int offset, next_period, block_size;
  576. dev_dbg(&chip->pdev->dev, "channel A event%s%s%s%s%s%s\n",
  577. casr & AC97C_CSR_OVRUN ? " OVRUN" : "",
  578. casr & AC97C_CSR_RXRDY ? " RXRDY" : "",
  579. casr & AC97C_CSR_UNRUN ? " UNRUN" : "",
  580. casr & AC97C_CSR_TXEMPTY ? " TXEMPTY" : "",
  581. casr & AC97C_CSR_TXRDY ? " TXRDY" : "",
  582. !casr ? " NONE" : "");
  583. if (!cpu_is_at32ap7000()) {
  584. if ((casr & camr) & AC97C_CSR_ENDTX) {
  585. runtime = chip->playback_substream->runtime;
  586. block_size = frames_to_bytes(runtime,
  587. runtime->period_size);
  588. chip->playback_period++;
  589. if (chip->playback_period == runtime->periods)
  590. chip->playback_period = 0;
  591. next_period = chip->playback_period + 1;
  592. if (next_period == runtime->periods)
  593. next_period = 0;
  594. offset = block_size * next_period;
  595. writel(runtime->dma_addr + offset,
  596. chip->regs + ATMEL_PDC_TNPR);
  597. writel(block_size / 2,
  598. chip->regs + ATMEL_PDC_TNCR);
  599. snd_pcm_period_elapsed(
  600. chip->playback_substream);
  601. }
  602. if ((casr & camr) & AC97C_CSR_ENDRX) {
  603. runtime = chip->capture_substream->runtime;
  604. block_size = frames_to_bytes(runtime,
  605. runtime->period_size);
  606. chip->capture_period++;
  607. if (chip->capture_period == runtime->periods)
  608. chip->capture_period = 0;
  609. next_period = chip->capture_period + 1;
  610. if (next_period == runtime->periods)
  611. next_period = 0;
  612. offset = block_size * next_period;
  613. writel(runtime->dma_addr + offset,
  614. chip->regs + ATMEL_PDC_RNPR);
  615. writel(block_size / 2,
  616. chip->regs + ATMEL_PDC_RNCR);
  617. snd_pcm_period_elapsed(chip->capture_substream);
  618. }
  619. }
  620. retval = IRQ_HANDLED;
  621. }
  622. if (sr & AC97C_SR_COEVT) {
  623. dev_info(&chip->pdev->dev, "codec channel event%s%s%s%s%s\n",
  624. cosr & AC97C_CSR_OVRUN ? " OVRUN" : "",
  625. cosr & AC97C_CSR_RXRDY ? " RXRDY" : "",
  626. cosr & AC97C_CSR_TXEMPTY ? " TXEMPTY" : "",
  627. cosr & AC97C_CSR_TXRDY ? " TXRDY" : "",
  628. !cosr ? " NONE" : "");
  629. retval = IRQ_HANDLED;
  630. }
  631. if (retval == IRQ_NONE) {
  632. dev_err(&chip->pdev->dev, "spurious interrupt sr 0x%08x "
  633. "casr 0x%08x cosr 0x%08x\n", sr, casr, cosr);
  634. }
  635. return retval;
  636. }
  637. static struct ac97_pcm at91_ac97_pcm_defs[] = {
  638. /* Playback */
  639. {
  640. .exclusive = 1,
  641. .r = { {
  642. .slots = ((1 << AC97_SLOT_PCM_LEFT)
  643. | (1 << AC97_SLOT_PCM_RIGHT)),
  644. } },
  645. },
  646. /* PCM in */
  647. {
  648. .stream = 1,
  649. .exclusive = 1,
  650. .r = { {
  651. .slots = ((1 << AC97_SLOT_PCM_LEFT)
  652. | (1 << AC97_SLOT_PCM_RIGHT)),
  653. } }
  654. },
  655. /* Mic in */
  656. {
  657. .stream = 1,
  658. .exclusive = 1,
  659. .r = { {
  660. .slots = (1<<AC97_SLOT_MIC),
  661. } }
  662. },
  663. };
  664. static int atmel_ac97c_pcm_new(struct atmel_ac97c *chip)
  665. {
  666. struct snd_pcm *pcm;
  667. struct snd_pcm_hardware hw = atmel_ac97c_hw;
  668. int capture, playback, retval, err;
  669. capture = test_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
  670. playback = test_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
  671. if (!cpu_is_at32ap7000()) {
  672. err = snd_ac97_pcm_assign(chip->ac97_bus,
  673. ARRAY_SIZE(at91_ac97_pcm_defs),
  674. at91_ac97_pcm_defs);
  675. if (err)
  676. return err;
  677. }
  678. retval = snd_pcm_new(chip->card, chip->card->shortname,
  679. 0, playback, capture, &pcm);
  680. if (retval)
  681. return retval;
  682. if (capture)
  683. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  684. &atmel_ac97_capture_ops);
  685. if (playback)
  686. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  687. &atmel_ac97_playback_ops);
  688. retval = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  689. &chip->pdev->dev, hw.periods_min * hw.period_bytes_min,
  690. hw.buffer_bytes_max);
  691. if (retval)
  692. return retval;
  693. pcm->private_data = chip;
  694. pcm->info_flags = 0;
  695. strcpy(pcm->name, chip->card->shortname);
  696. chip->pcm = pcm;
  697. return 0;
  698. }
  699. static int atmel_ac97c_mixer_new(struct atmel_ac97c *chip)
  700. {
  701. struct snd_ac97_template template;
  702. memset(&template, 0, sizeof(template));
  703. template.private_data = chip;
  704. return snd_ac97_mixer(chip->ac97_bus, &template, &chip->ac97);
  705. }
  706. static void atmel_ac97c_write(struct snd_ac97 *ac97, unsigned short reg,
  707. unsigned short val)
  708. {
  709. struct atmel_ac97c *chip = get_chip(ac97);
  710. unsigned long word;
  711. int timeout = 40;
  712. word = (reg & 0x7f) << 16 | val;
  713. do {
  714. if (ac97c_readl(chip, COSR) & AC97C_CSR_TXRDY) {
  715. ac97c_writel(chip, COTHR, word);
  716. return;
  717. }
  718. udelay(1);
  719. } while (--timeout);
  720. dev_dbg(&chip->pdev->dev, "codec write timeout\n");
  721. }
  722. static unsigned short atmel_ac97c_read(struct snd_ac97 *ac97,
  723. unsigned short reg)
  724. {
  725. struct atmel_ac97c *chip = get_chip(ac97);
  726. unsigned long word;
  727. int timeout = 40;
  728. int write = 10;
  729. word = (0x80 | (reg & 0x7f)) << 16;
  730. if ((ac97c_readl(chip, COSR) & AC97C_CSR_RXRDY) != 0)
  731. ac97c_readl(chip, CORHR);
  732. retry_write:
  733. timeout = 40;
  734. do {
  735. if ((ac97c_readl(chip, COSR) & AC97C_CSR_TXRDY) != 0) {
  736. ac97c_writel(chip, COTHR, word);
  737. goto read_reg;
  738. }
  739. udelay(10);
  740. } while (--timeout);
  741. if (!--write)
  742. goto timed_out;
  743. goto retry_write;
  744. read_reg:
  745. do {
  746. if ((ac97c_readl(chip, COSR) & AC97C_CSR_RXRDY) != 0) {
  747. unsigned short val = ac97c_readl(chip, CORHR);
  748. return val;
  749. }
  750. udelay(10);
  751. } while (--timeout);
  752. if (!--write)
  753. goto timed_out;
  754. goto retry_write;
  755. timed_out:
  756. dev_dbg(&chip->pdev->dev, "codec read timeout\n");
  757. return 0xffff;
  758. }
  759. static bool filter(struct dma_chan *chan, void *slave)
  760. {
  761. struct dw_dma_slave *dws = slave;
  762. if (dws->dma_dev == chan->device->dev) {
  763. chan->private = dws;
  764. return true;
  765. } else
  766. return false;
  767. }
  768. static void atmel_ac97c_reset(struct atmel_ac97c *chip)
  769. {
  770. ac97c_writel(chip, MR, 0);
  771. ac97c_writel(chip, MR, AC97C_MR_ENA);
  772. ac97c_writel(chip, CAMR, 0);
  773. ac97c_writel(chip, COMR, 0);
  774. if (gpio_is_valid(chip->reset_pin)) {
  775. gpio_set_value(chip->reset_pin, 0);
  776. /* AC97 v2.2 specifications says minimum 1 us. */
  777. udelay(2);
  778. gpio_set_value(chip->reset_pin, 1);
  779. } else {
  780. ac97c_writel(chip, MR, AC97C_MR_WRST | AC97C_MR_ENA);
  781. udelay(2);
  782. ac97c_writel(chip, MR, AC97C_MR_ENA);
  783. }
  784. }
  785. #ifdef CONFIG_OF
  786. static const struct of_device_id atmel_ac97c_dt_ids[] = {
  787. { .compatible = "atmel,at91sam9263-ac97c", },
  788. { }
  789. };
  790. MODULE_DEVICE_TABLE(of, atmel_ac97c_dt_ids);
  791. static struct ac97c_platform_data *atmel_ac97c_probe_dt(struct device *dev)
  792. {
  793. struct ac97c_platform_data *pdata;
  794. struct device_node *node = dev->of_node;
  795. if (!node) {
  796. dev_err(dev, "Device does not have associated DT data\n");
  797. return ERR_PTR(-EINVAL);
  798. }
  799. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  800. if (!pdata)
  801. return ERR_PTR(-ENOMEM);
  802. pdata->reset_pin = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
  803. return pdata;
  804. }
  805. #else
  806. static struct ac97c_platform_data *atmel_ac97c_probe_dt(struct device *dev)
  807. {
  808. dev_err(dev, "no platform data defined\n");
  809. return ERR_PTR(-ENXIO);
  810. }
  811. #endif
  812. static int atmel_ac97c_probe(struct platform_device *pdev)
  813. {
  814. struct snd_card *card;
  815. struct atmel_ac97c *chip;
  816. struct resource *regs;
  817. struct ac97c_platform_data *pdata;
  818. struct clk *pclk;
  819. static struct snd_ac97_bus_ops ops = {
  820. .write = atmel_ac97c_write,
  821. .read = atmel_ac97c_read,
  822. };
  823. int retval;
  824. int irq;
  825. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  826. if (!regs) {
  827. dev_dbg(&pdev->dev, "no memory resource\n");
  828. return -ENXIO;
  829. }
  830. pdata = dev_get_platdata(&pdev->dev);
  831. if (!pdata) {
  832. pdata = atmel_ac97c_probe_dt(&pdev->dev);
  833. if (IS_ERR(pdata))
  834. return PTR_ERR(pdata);
  835. }
  836. irq = platform_get_irq(pdev, 0);
  837. if (irq < 0) {
  838. dev_dbg(&pdev->dev, "could not get irq\n");
  839. return -ENXIO;
  840. }
  841. if (cpu_is_at32ap7000()) {
  842. pclk = clk_get(&pdev->dev, "pclk");
  843. } else {
  844. pclk = clk_get(&pdev->dev, "ac97_clk");
  845. }
  846. if (IS_ERR(pclk)) {
  847. dev_dbg(&pdev->dev, "no peripheral clock\n");
  848. return PTR_ERR(pclk);
  849. }
  850. clk_prepare_enable(pclk);
  851. retval = snd_card_new(&pdev->dev, SNDRV_DEFAULT_IDX1,
  852. SNDRV_DEFAULT_STR1, THIS_MODULE,
  853. sizeof(struct atmel_ac97c), &card);
  854. if (retval) {
  855. dev_dbg(&pdev->dev, "could not create sound card device\n");
  856. goto err_snd_card_new;
  857. }
  858. chip = get_chip(card);
  859. retval = request_irq(irq, atmel_ac97c_interrupt, 0, "AC97C", chip);
  860. if (retval) {
  861. dev_dbg(&pdev->dev, "unable to request irq %d\n", irq);
  862. goto err_request_irq;
  863. }
  864. chip->irq = irq;
  865. spin_lock_init(&chip->lock);
  866. strcpy(card->driver, "Atmel AC97C");
  867. strcpy(card->shortname, "Atmel AC97C");
  868. sprintf(card->longname, "Atmel AC97 controller");
  869. chip->card = card;
  870. chip->pclk = pclk;
  871. chip->pdev = pdev;
  872. chip->regs = ioremap(regs->start, resource_size(regs));
  873. if (!chip->regs) {
  874. dev_dbg(&pdev->dev, "could not remap register memory\n");
  875. retval = -ENOMEM;
  876. goto err_ioremap;
  877. }
  878. if (gpio_is_valid(pdata->reset_pin)) {
  879. if (gpio_request(pdata->reset_pin, "reset_pin")) {
  880. dev_dbg(&pdev->dev, "reset pin not available\n");
  881. chip->reset_pin = -ENODEV;
  882. } else {
  883. gpio_direction_output(pdata->reset_pin, 1);
  884. chip->reset_pin = pdata->reset_pin;
  885. }
  886. } else {
  887. chip->reset_pin = -EINVAL;
  888. }
  889. atmel_ac97c_reset(chip);
  890. /* Enable overrun interrupt from codec channel */
  891. ac97c_writel(chip, COMR, AC97C_CSR_OVRUN);
  892. ac97c_writel(chip, IER, ac97c_readl(chip, IMR) | AC97C_SR_COEVT);
  893. retval = snd_ac97_bus(card, 0, &ops, chip, &chip->ac97_bus);
  894. if (retval) {
  895. dev_dbg(&pdev->dev, "could not register on ac97 bus\n");
  896. goto err_ac97_bus;
  897. }
  898. retval = atmel_ac97c_mixer_new(chip);
  899. if (retval) {
  900. dev_dbg(&pdev->dev, "could not register ac97 mixer\n");
  901. goto err_ac97_bus;
  902. }
  903. if (cpu_is_at32ap7000()) {
  904. if (pdata->rx_dws.dma_dev) {
  905. dma_cap_mask_t mask;
  906. dma_cap_zero(mask);
  907. dma_cap_set(DMA_SLAVE, mask);
  908. chip->dma.rx_chan = dma_request_channel(mask, filter,
  909. &pdata->rx_dws);
  910. if (chip->dma.rx_chan) {
  911. struct dma_slave_config dma_conf = {
  912. .src_addr = regs->start + AC97C_CARHR +
  913. 2,
  914. .src_addr_width =
  915. DMA_SLAVE_BUSWIDTH_2_BYTES,
  916. .src_maxburst = 1,
  917. .dst_maxburst = 1,
  918. .direction = DMA_DEV_TO_MEM,
  919. .device_fc = false,
  920. };
  921. dmaengine_slave_config(chip->dma.rx_chan,
  922. &dma_conf);
  923. }
  924. dev_info(&chip->pdev->dev, "using %s for DMA RX\n",
  925. dev_name(&chip->dma.rx_chan->dev->device));
  926. set_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
  927. }
  928. if (pdata->tx_dws.dma_dev) {
  929. dma_cap_mask_t mask;
  930. dma_cap_zero(mask);
  931. dma_cap_set(DMA_SLAVE, mask);
  932. chip->dma.tx_chan = dma_request_channel(mask, filter,
  933. &pdata->tx_dws);
  934. if (chip->dma.tx_chan) {
  935. struct dma_slave_config dma_conf = {
  936. .dst_addr = regs->start + AC97C_CATHR +
  937. 2,
  938. .dst_addr_width =
  939. DMA_SLAVE_BUSWIDTH_2_BYTES,
  940. .src_maxburst = 1,
  941. .dst_maxburst = 1,
  942. .direction = DMA_MEM_TO_DEV,
  943. .device_fc = false,
  944. };
  945. dmaengine_slave_config(chip->dma.tx_chan,
  946. &dma_conf);
  947. }
  948. dev_info(&chip->pdev->dev, "using %s for DMA TX\n",
  949. dev_name(&chip->dma.tx_chan->dev->device));
  950. set_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
  951. }
  952. if (!test_bit(DMA_RX_CHAN_PRESENT, &chip->flags) &&
  953. !test_bit(DMA_TX_CHAN_PRESENT, &chip->flags)) {
  954. dev_dbg(&pdev->dev, "DMA not available\n");
  955. retval = -ENODEV;
  956. goto err_dma;
  957. }
  958. } else {
  959. /* Just pretend that we have DMA channel(for at91 i is actually
  960. * the PDC) */
  961. set_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
  962. set_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
  963. }
  964. retval = atmel_ac97c_pcm_new(chip);
  965. if (retval) {
  966. dev_dbg(&pdev->dev, "could not register ac97 pcm device\n");
  967. goto err_dma;
  968. }
  969. retval = snd_card_register(card);
  970. if (retval) {
  971. dev_dbg(&pdev->dev, "could not register sound card\n");
  972. goto err_dma;
  973. }
  974. platform_set_drvdata(pdev, card);
  975. dev_info(&pdev->dev, "Atmel AC97 controller at 0x%p, irq = %d\n",
  976. chip->regs, irq);
  977. return 0;
  978. err_dma:
  979. if (cpu_is_at32ap7000()) {
  980. if (test_bit(DMA_RX_CHAN_PRESENT, &chip->flags))
  981. dma_release_channel(chip->dma.rx_chan);
  982. if (test_bit(DMA_TX_CHAN_PRESENT, &chip->flags))
  983. dma_release_channel(chip->dma.tx_chan);
  984. clear_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
  985. clear_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
  986. chip->dma.rx_chan = NULL;
  987. chip->dma.tx_chan = NULL;
  988. }
  989. err_ac97_bus:
  990. if (gpio_is_valid(chip->reset_pin))
  991. gpio_free(chip->reset_pin);
  992. iounmap(chip->regs);
  993. err_ioremap:
  994. free_irq(irq, chip);
  995. err_request_irq:
  996. snd_card_free(card);
  997. err_snd_card_new:
  998. clk_disable_unprepare(pclk);
  999. clk_put(pclk);
  1000. return retval;
  1001. }
  1002. #ifdef CONFIG_PM_SLEEP
  1003. static int atmel_ac97c_suspend(struct device *pdev)
  1004. {
  1005. struct snd_card *card = dev_get_drvdata(pdev);
  1006. struct atmel_ac97c *chip = card->private_data;
  1007. if (cpu_is_at32ap7000()) {
  1008. if (test_bit(DMA_RX_READY, &chip->flags))
  1009. dw_dma_cyclic_stop(chip->dma.rx_chan);
  1010. if (test_bit(DMA_TX_READY, &chip->flags))
  1011. dw_dma_cyclic_stop(chip->dma.tx_chan);
  1012. }
  1013. clk_disable_unprepare(chip->pclk);
  1014. return 0;
  1015. }
  1016. static int atmel_ac97c_resume(struct device *pdev)
  1017. {
  1018. struct snd_card *card = dev_get_drvdata(pdev);
  1019. struct atmel_ac97c *chip = card->private_data;
  1020. clk_prepare_enable(chip->pclk);
  1021. if (cpu_is_at32ap7000()) {
  1022. if (test_bit(DMA_RX_READY, &chip->flags))
  1023. dw_dma_cyclic_start(chip->dma.rx_chan);
  1024. if (test_bit(DMA_TX_READY, &chip->flags))
  1025. dw_dma_cyclic_start(chip->dma.tx_chan);
  1026. }
  1027. return 0;
  1028. }
  1029. static SIMPLE_DEV_PM_OPS(atmel_ac97c_pm, atmel_ac97c_suspend, atmel_ac97c_resume);
  1030. #define ATMEL_AC97C_PM_OPS &atmel_ac97c_pm
  1031. #else
  1032. #define ATMEL_AC97C_PM_OPS NULL
  1033. #endif
  1034. static int atmel_ac97c_remove(struct platform_device *pdev)
  1035. {
  1036. struct snd_card *card = platform_get_drvdata(pdev);
  1037. struct atmel_ac97c *chip = get_chip(card);
  1038. if (gpio_is_valid(chip->reset_pin))
  1039. gpio_free(chip->reset_pin);
  1040. ac97c_writel(chip, CAMR, 0);
  1041. ac97c_writel(chip, COMR, 0);
  1042. ac97c_writel(chip, MR, 0);
  1043. clk_disable_unprepare(chip->pclk);
  1044. clk_put(chip->pclk);
  1045. iounmap(chip->regs);
  1046. free_irq(chip->irq, chip);
  1047. if (cpu_is_at32ap7000()) {
  1048. if (test_bit(DMA_RX_CHAN_PRESENT, &chip->flags))
  1049. dma_release_channel(chip->dma.rx_chan);
  1050. if (test_bit(DMA_TX_CHAN_PRESENT, &chip->flags))
  1051. dma_release_channel(chip->dma.tx_chan);
  1052. clear_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
  1053. clear_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
  1054. chip->dma.rx_chan = NULL;
  1055. chip->dma.tx_chan = NULL;
  1056. }
  1057. snd_card_free(card);
  1058. return 0;
  1059. }
  1060. static struct platform_driver atmel_ac97c_driver = {
  1061. .probe = atmel_ac97c_probe,
  1062. .remove = atmel_ac97c_remove,
  1063. .driver = {
  1064. .name = "atmel_ac97c",
  1065. .pm = ATMEL_AC97C_PM_OPS,
  1066. .of_match_table = of_match_ptr(atmel_ac97c_dt_ids),
  1067. },
  1068. };
  1069. module_platform_driver(atmel_ac97c_driver);
  1070. MODULE_LICENSE("GPL");
  1071. MODULE_DESCRIPTION("Driver for Atmel AC97 controller");
  1072. MODULE_AUTHOR("Hans-Christian Egtvedt <egtvedt@samfundet.no>");