ac97c.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. /*
  2. * Driver for the Atmel AC97C controller
  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/dmaengine.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/mutex.h>
  20. #include <linux/gpio.h>
  21. #include <linux/io.h>
  22. #include <sound/core.h>
  23. #include <sound/initval.h>
  24. #include <sound/pcm.h>
  25. #include <sound/pcm_params.h>
  26. #include <sound/ac97_codec.h>
  27. #include <sound/atmel-ac97c.h>
  28. #include <sound/memalloc.h>
  29. #include <linux/dw_dmac.h>
  30. #include "ac97c.h"
  31. enum {
  32. DMA_TX_READY = 0,
  33. DMA_RX_READY,
  34. DMA_TX_CHAN_PRESENT,
  35. DMA_RX_CHAN_PRESENT,
  36. };
  37. /* Serialize access to opened variable */
  38. static DEFINE_MUTEX(opened_mutex);
  39. struct atmel_ac97c_dma {
  40. struct dma_chan *rx_chan;
  41. struct dma_chan *tx_chan;
  42. };
  43. struct atmel_ac97c {
  44. struct clk *pclk;
  45. struct platform_device *pdev;
  46. struct atmel_ac97c_dma dma;
  47. struct snd_pcm_substream *playback_substream;
  48. struct snd_pcm_substream *capture_substream;
  49. struct snd_card *card;
  50. struct snd_pcm *pcm;
  51. struct snd_ac97 *ac97;
  52. struct snd_ac97_bus *ac97_bus;
  53. u64 cur_format;
  54. unsigned int cur_rate;
  55. unsigned long flags;
  56. /* Serialize access to opened variable */
  57. spinlock_t lock;
  58. void __iomem *regs;
  59. int opened;
  60. int reset_pin;
  61. };
  62. #define get_chip(card) ((struct atmel_ac97c *)(card)->private_data)
  63. #define ac97c_writel(chip, reg, val) \
  64. __raw_writel((val), (chip)->regs + AC97C_##reg)
  65. #define ac97c_readl(chip, reg) \
  66. __raw_readl((chip)->regs + AC97C_##reg)
  67. /* This function is called by the DMA driver. */
  68. static void atmel_ac97c_dma_playback_period_done(void *arg)
  69. {
  70. struct atmel_ac97c *chip = arg;
  71. snd_pcm_period_elapsed(chip->playback_substream);
  72. }
  73. static void atmel_ac97c_dma_capture_period_done(void *arg)
  74. {
  75. struct atmel_ac97c *chip = arg;
  76. snd_pcm_period_elapsed(chip->capture_substream);
  77. }
  78. static int atmel_ac97c_prepare_dma(struct atmel_ac97c *chip,
  79. struct snd_pcm_substream *substream,
  80. enum dma_data_direction direction)
  81. {
  82. struct dma_chan *chan;
  83. struct dw_cyclic_desc *cdesc;
  84. struct snd_pcm_runtime *runtime = substream->runtime;
  85. unsigned long buffer_len, period_len;
  86. /*
  87. * We don't do DMA on "complex" transfers, i.e. with
  88. * non-halfword-aligned buffers or lengths.
  89. */
  90. if (runtime->dma_addr & 1 || runtime->buffer_size & 1) {
  91. dev_dbg(&chip->pdev->dev, "too complex transfer\n");
  92. return -EINVAL;
  93. }
  94. if (direction == DMA_TO_DEVICE)
  95. chan = chip->dma.tx_chan;
  96. else
  97. chan = chip->dma.rx_chan;
  98. buffer_len = frames_to_bytes(runtime, runtime->buffer_size);
  99. period_len = frames_to_bytes(runtime, runtime->period_size);
  100. cdesc = dw_dma_cyclic_prep(chan, runtime->dma_addr, buffer_len,
  101. period_len, direction);
  102. if (IS_ERR(cdesc)) {
  103. dev_dbg(&chip->pdev->dev, "could not prepare cyclic DMA\n");
  104. return PTR_ERR(cdesc);
  105. }
  106. if (direction == DMA_TO_DEVICE) {
  107. cdesc->period_callback = atmel_ac97c_dma_playback_period_done;
  108. set_bit(DMA_TX_READY, &chip->flags);
  109. } else {
  110. cdesc->period_callback = atmel_ac97c_dma_capture_period_done;
  111. set_bit(DMA_RX_READY, &chip->flags);
  112. }
  113. cdesc->period_callback_param = chip;
  114. return 0;
  115. }
  116. static struct snd_pcm_hardware atmel_ac97c_hw = {
  117. .info = (SNDRV_PCM_INFO_MMAP
  118. | SNDRV_PCM_INFO_MMAP_VALID
  119. | SNDRV_PCM_INFO_INTERLEAVED
  120. | SNDRV_PCM_INFO_BLOCK_TRANSFER
  121. | SNDRV_PCM_INFO_JOINT_DUPLEX
  122. | SNDRV_PCM_INFO_RESUME
  123. | SNDRV_PCM_INFO_PAUSE),
  124. .formats = (SNDRV_PCM_FMTBIT_S16_BE
  125. | SNDRV_PCM_FMTBIT_S16_LE),
  126. .rates = (SNDRV_PCM_RATE_CONTINUOUS),
  127. .rate_min = 4000,
  128. .rate_max = 48000,
  129. .channels_min = 1,
  130. .channels_max = 2,
  131. .buffer_bytes_max = 64 * 4096,
  132. .period_bytes_min = 4096,
  133. .period_bytes_max = 4096,
  134. .periods_min = 4,
  135. .periods_max = 64,
  136. };
  137. static int atmel_ac97c_playback_open(struct snd_pcm_substream *substream)
  138. {
  139. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  140. struct snd_pcm_runtime *runtime = substream->runtime;
  141. mutex_lock(&opened_mutex);
  142. chip->opened++;
  143. runtime->hw = atmel_ac97c_hw;
  144. if (chip->cur_rate) {
  145. runtime->hw.rate_min = chip->cur_rate;
  146. runtime->hw.rate_max = chip->cur_rate;
  147. }
  148. if (chip->cur_format)
  149. runtime->hw.formats = (1ULL << chip->cur_format);
  150. mutex_unlock(&opened_mutex);
  151. chip->playback_substream = substream;
  152. return 0;
  153. }
  154. static int atmel_ac97c_capture_open(struct snd_pcm_substream *substream)
  155. {
  156. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  157. struct snd_pcm_runtime *runtime = substream->runtime;
  158. mutex_lock(&opened_mutex);
  159. chip->opened++;
  160. runtime->hw = atmel_ac97c_hw;
  161. if (chip->cur_rate) {
  162. runtime->hw.rate_min = chip->cur_rate;
  163. runtime->hw.rate_max = chip->cur_rate;
  164. }
  165. if (chip->cur_format)
  166. runtime->hw.formats = (1ULL << chip->cur_format);
  167. mutex_unlock(&opened_mutex);
  168. chip->capture_substream = substream;
  169. return 0;
  170. }
  171. static int atmel_ac97c_playback_close(struct snd_pcm_substream *substream)
  172. {
  173. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  174. mutex_lock(&opened_mutex);
  175. chip->opened--;
  176. if (!chip->opened) {
  177. chip->cur_rate = 0;
  178. chip->cur_format = 0;
  179. }
  180. mutex_unlock(&opened_mutex);
  181. chip->playback_substream = NULL;
  182. return 0;
  183. }
  184. static int atmel_ac97c_capture_close(struct snd_pcm_substream *substream)
  185. {
  186. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  187. mutex_lock(&opened_mutex);
  188. chip->opened--;
  189. if (!chip->opened) {
  190. chip->cur_rate = 0;
  191. chip->cur_format = 0;
  192. }
  193. mutex_unlock(&opened_mutex);
  194. chip->capture_substream = NULL;
  195. return 0;
  196. }
  197. static int atmel_ac97c_playback_hw_params(struct snd_pcm_substream *substream,
  198. struct snd_pcm_hw_params *hw_params)
  199. {
  200. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  201. int retval;
  202. retval = snd_pcm_lib_malloc_pages(substream,
  203. params_buffer_bytes(hw_params));
  204. if (retval < 0)
  205. return retval;
  206. /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
  207. if (retval == 1)
  208. if (test_and_clear_bit(DMA_TX_READY, &chip->flags))
  209. dw_dma_cyclic_free(chip->dma.tx_chan);
  210. /* Set restrictions to params. */
  211. mutex_lock(&opened_mutex);
  212. chip->cur_rate = params_rate(hw_params);
  213. chip->cur_format = params_format(hw_params);
  214. mutex_unlock(&opened_mutex);
  215. return retval;
  216. }
  217. static int atmel_ac97c_capture_hw_params(struct snd_pcm_substream *substream,
  218. struct snd_pcm_hw_params *hw_params)
  219. {
  220. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  221. int retval;
  222. retval = snd_pcm_lib_malloc_pages(substream,
  223. params_buffer_bytes(hw_params));
  224. if (retval < 0)
  225. return retval;
  226. /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
  227. if (retval == 1)
  228. if (test_and_clear_bit(DMA_RX_READY, &chip->flags))
  229. dw_dma_cyclic_free(chip->dma.rx_chan);
  230. /* Set restrictions to params. */
  231. mutex_lock(&opened_mutex);
  232. chip->cur_rate = params_rate(hw_params);
  233. chip->cur_format = params_format(hw_params);
  234. mutex_unlock(&opened_mutex);
  235. return retval;
  236. }
  237. static int atmel_ac97c_playback_hw_free(struct snd_pcm_substream *substream)
  238. {
  239. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  240. if (test_and_clear_bit(DMA_TX_READY, &chip->flags))
  241. dw_dma_cyclic_free(chip->dma.tx_chan);
  242. return snd_pcm_lib_free_pages(substream);
  243. }
  244. static int atmel_ac97c_capture_hw_free(struct snd_pcm_substream *substream)
  245. {
  246. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  247. if (test_and_clear_bit(DMA_RX_READY, &chip->flags))
  248. dw_dma_cyclic_free(chip->dma.rx_chan);
  249. return snd_pcm_lib_free_pages(substream);
  250. }
  251. static int atmel_ac97c_playback_prepare(struct snd_pcm_substream *substream)
  252. {
  253. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  254. struct snd_pcm_runtime *runtime = substream->runtime;
  255. unsigned long word = 0;
  256. int retval;
  257. /* assign channels to AC97C channel A */
  258. switch (runtime->channels) {
  259. case 1:
  260. word |= AC97C_CH_ASSIGN(PCM_LEFT, A);
  261. break;
  262. case 2:
  263. word |= AC97C_CH_ASSIGN(PCM_LEFT, A)
  264. | AC97C_CH_ASSIGN(PCM_RIGHT, A);
  265. break;
  266. default:
  267. /* TODO: support more than two channels */
  268. return -EINVAL;
  269. break;
  270. }
  271. ac97c_writel(chip, OCA, word);
  272. /* configure sample format and size */
  273. word = AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
  274. switch (runtime->format) {
  275. case SNDRV_PCM_FORMAT_S16_LE:
  276. word |= AC97C_CMR_CEM_LITTLE;
  277. break;
  278. case SNDRV_PCM_FORMAT_S16_BE: /* fall through */
  279. default:
  280. word &= ~(AC97C_CMR_CEM_LITTLE);
  281. break;
  282. }
  283. ac97c_writel(chip, CAMR, word);
  284. /* set variable rate if needed */
  285. if (runtime->rate != 48000) {
  286. word = ac97c_readl(chip, MR);
  287. word |= AC97C_MR_VRA;
  288. ac97c_writel(chip, MR, word);
  289. } else {
  290. word = ac97c_readl(chip, MR);
  291. word &= ~(AC97C_MR_VRA);
  292. ac97c_writel(chip, MR, word);
  293. }
  294. retval = snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE,
  295. runtime->rate);
  296. if (retval)
  297. dev_dbg(&chip->pdev->dev, "could not set rate %d Hz\n",
  298. runtime->rate);
  299. if (!test_bit(DMA_TX_READY, &chip->flags))
  300. retval = atmel_ac97c_prepare_dma(chip, substream,
  301. DMA_TO_DEVICE);
  302. return retval;
  303. }
  304. static int atmel_ac97c_capture_prepare(struct snd_pcm_substream *substream)
  305. {
  306. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  307. struct snd_pcm_runtime *runtime = substream->runtime;
  308. unsigned long word = 0;
  309. int retval;
  310. /* assign channels to AC97C channel A */
  311. switch (runtime->channels) {
  312. case 1:
  313. word |= AC97C_CH_ASSIGN(PCM_LEFT, A);
  314. break;
  315. case 2:
  316. word |= AC97C_CH_ASSIGN(PCM_LEFT, A)
  317. | AC97C_CH_ASSIGN(PCM_RIGHT, A);
  318. break;
  319. default:
  320. /* TODO: support more than two channels */
  321. return -EINVAL;
  322. break;
  323. }
  324. ac97c_writel(chip, ICA, word);
  325. /* configure sample format and size */
  326. word = AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
  327. switch (runtime->format) {
  328. case SNDRV_PCM_FORMAT_S16_LE:
  329. word |= AC97C_CMR_CEM_LITTLE;
  330. break;
  331. case SNDRV_PCM_FORMAT_S16_BE: /* fall through */
  332. default:
  333. word &= ~(AC97C_CMR_CEM_LITTLE);
  334. break;
  335. }
  336. ac97c_writel(chip, CAMR, word);
  337. /* set variable rate if needed */
  338. if (runtime->rate != 48000) {
  339. word = ac97c_readl(chip, MR);
  340. word |= AC97C_MR_VRA;
  341. ac97c_writel(chip, MR, word);
  342. } else {
  343. word = ac97c_readl(chip, MR);
  344. word &= ~(AC97C_MR_VRA);
  345. ac97c_writel(chip, MR, word);
  346. }
  347. retval = snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE,
  348. runtime->rate);
  349. if (retval)
  350. dev_dbg(&chip->pdev->dev, "could not set rate %d Hz\n",
  351. runtime->rate);
  352. if (!test_bit(DMA_RX_READY, &chip->flags))
  353. retval = atmel_ac97c_prepare_dma(chip, substream,
  354. DMA_FROM_DEVICE);
  355. return retval;
  356. }
  357. static int
  358. atmel_ac97c_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  359. {
  360. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  361. unsigned long camr;
  362. int retval = 0;
  363. camr = ac97c_readl(chip, CAMR);
  364. switch (cmd) {
  365. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
  366. case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
  367. case SNDRV_PCM_TRIGGER_START:
  368. retval = dw_dma_cyclic_start(chip->dma.tx_chan);
  369. if (retval)
  370. goto out;
  371. camr |= AC97C_CMR_CENA;
  372. break;
  373. case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
  374. case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
  375. case SNDRV_PCM_TRIGGER_STOP:
  376. dw_dma_cyclic_stop(chip->dma.tx_chan);
  377. if (chip->opened <= 1)
  378. camr &= ~AC97C_CMR_CENA;
  379. break;
  380. default:
  381. retval = -EINVAL;
  382. goto out;
  383. }
  384. ac97c_writel(chip, CAMR, camr);
  385. out:
  386. return retval;
  387. }
  388. static int
  389. atmel_ac97c_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  390. {
  391. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  392. unsigned long camr;
  393. int retval = 0;
  394. camr = ac97c_readl(chip, CAMR);
  395. switch (cmd) {
  396. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
  397. case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
  398. case SNDRV_PCM_TRIGGER_START:
  399. retval = dw_dma_cyclic_start(chip->dma.rx_chan);
  400. if (retval)
  401. goto out;
  402. camr |= AC97C_CMR_CENA;
  403. break;
  404. case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
  405. case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
  406. case SNDRV_PCM_TRIGGER_STOP:
  407. dw_dma_cyclic_stop(chip->dma.rx_chan);
  408. if (chip->opened <= 1)
  409. camr &= ~AC97C_CMR_CENA;
  410. break;
  411. default:
  412. retval = -EINVAL;
  413. break;
  414. }
  415. ac97c_writel(chip, CAMR, camr);
  416. out:
  417. return retval;
  418. }
  419. static snd_pcm_uframes_t
  420. atmel_ac97c_playback_pointer(struct snd_pcm_substream *substream)
  421. {
  422. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  423. struct snd_pcm_runtime *runtime = substream->runtime;
  424. snd_pcm_uframes_t frames;
  425. unsigned long bytes;
  426. bytes = dw_dma_get_src_addr(chip->dma.tx_chan);
  427. bytes -= runtime->dma_addr;
  428. frames = bytes_to_frames(runtime, bytes);
  429. if (frames >= runtime->buffer_size)
  430. frames -= runtime->buffer_size;
  431. return frames;
  432. }
  433. static snd_pcm_uframes_t
  434. atmel_ac97c_capture_pointer(struct snd_pcm_substream *substream)
  435. {
  436. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  437. struct snd_pcm_runtime *runtime = substream->runtime;
  438. snd_pcm_uframes_t frames;
  439. unsigned long bytes;
  440. bytes = dw_dma_get_dst_addr(chip->dma.rx_chan);
  441. bytes -= runtime->dma_addr;
  442. frames = bytes_to_frames(runtime, bytes);
  443. if (frames >= runtime->buffer_size)
  444. frames -= runtime->buffer_size;
  445. return frames;
  446. }
  447. static struct snd_pcm_ops atmel_ac97_playback_ops = {
  448. .open = atmel_ac97c_playback_open,
  449. .close = atmel_ac97c_playback_close,
  450. .ioctl = snd_pcm_lib_ioctl,
  451. .hw_params = atmel_ac97c_playback_hw_params,
  452. .hw_free = atmel_ac97c_playback_hw_free,
  453. .prepare = atmel_ac97c_playback_prepare,
  454. .trigger = atmel_ac97c_playback_trigger,
  455. .pointer = atmel_ac97c_playback_pointer,
  456. };
  457. static struct snd_pcm_ops atmel_ac97_capture_ops = {
  458. .open = atmel_ac97c_capture_open,
  459. .close = atmel_ac97c_capture_close,
  460. .ioctl = snd_pcm_lib_ioctl,
  461. .hw_params = atmel_ac97c_capture_hw_params,
  462. .hw_free = atmel_ac97c_capture_hw_free,
  463. .prepare = atmel_ac97c_capture_prepare,
  464. .trigger = atmel_ac97c_capture_trigger,
  465. .pointer = atmel_ac97c_capture_pointer,
  466. };
  467. static int __devinit atmel_ac97c_pcm_new(struct atmel_ac97c *chip)
  468. {
  469. struct snd_pcm *pcm;
  470. struct snd_pcm_hardware hw = atmel_ac97c_hw;
  471. int capture, playback, retval;
  472. capture = test_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
  473. playback = test_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
  474. retval = snd_pcm_new(chip->card, chip->card->shortname,
  475. chip->pdev->id, playback, capture, &pcm);
  476. if (retval)
  477. return retval;
  478. if (capture)
  479. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  480. &atmel_ac97_capture_ops);
  481. if (playback)
  482. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  483. &atmel_ac97_playback_ops);
  484. retval = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  485. &chip->pdev->dev, hw.periods_min * hw.period_bytes_min,
  486. hw.buffer_bytes_max);
  487. if (retval)
  488. return retval;
  489. pcm->private_data = chip;
  490. pcm->info_flags = 0;
  491. strcpy(pcm->name, chip->card->shortname);
  492. chip->pcm = pcm;
  493. return 0;
  494. }
  495. static int atmel_ac97c_mixer_new(struct atmel_ac97c *chip)
  496. {
  497. struct snd_ac97_template template;
  498. memset(&template, 0, sizeof(template));
  499. template.private_data = chip;
  500. return snd_ac97_mixer(chip->ac97_bus, &template, &chip->ac97);
  501. }
  502. static void atmel_ac97c_write(struct snd_ac97 *ac97, unsigned short reg,
  503. unsigned short val)
  504. {
  505. struct atmel_ac97c *chip = get_chip(ac97);
  506. unsigned long word;
  507. int timeout = 40;
  508. word = (reg & 0x7f) << 16 | val;
  509. do {
  510. if (ac97c_readl(chip, COSR) & AC97C_CSR_TXRDY) {
  511. ac97c_writel(chip, COTHR, word);
  512. return;
  513. }
  514. udelay(1);
  515. } while (--timeout);
  516. dev_dbg(&chip->pdev->dev, "codec write timeout\n");
  517. }
  518. static unsigned short atmel_ac97c_read(struct snd_ac97 *ac97,
  519. unsigned short reg)
  520. {
  521. struct atmel_ac97c *chip = get_chip(ac97);
  522. unsigned long word;
  523. int timeout = 40;
  524. int write = 10;
  525. word = (0x80 | (reg & 0x7f)) << 16;
  526. if ((ac97c_readl(chip, COSR) & AC97C_CSR_RXRDY) != 0)
  527. ac97c_readl(chip, CORHR);
  528. retry_write:
  529. timeout = 40;
  530. do {
  531. if ((ac97c_readl(chip, COSR) & AC97C_CSR_TXRDY) != 0) {
  532. ac97c_writel(chip, COTHR, word);
  533. goto read_reg;
  534. }
  535. udelay(10);
  536. } while (--timeout);
  537. if (!--write)
  538. goto timed_out;
  539. goto retry_write;
  540. read_reg:
  541. do {
  542. if ((ac97c_readl(chip, COSR) & AC97C_CSR_RXRDY) != 0) {
  543. unsigned short val = ac97c_readl(chip, CORHR);
  544. return val;
  545. }
  546. udelay(10);
  547. } while (--timeout);
  548. if (!--write)
  549. goto timed_out;
  550. goto retry_write;
  551. timed_out:
  552. dev_dbg(&chip->pdev->dev, "codec read timeout\n");
  553. return 0xffff;
  554. }
  555. static bool filter(struct dma_chan *chan, void *slave)
  556. {
  557. struct dw_dma_slave *dws = slave;
  558. if (dws->dma_dev == chan->device->dev) {
  559. chan->private = dws;
  560. return true;
  561. } else
  562. return false;
  563. }
  564. static void atmel_ac97c_reset(struct atmel_ac97c *chip)
  565. {
  566. ac97c_writel(chip, MR, AC97C_MR_WRST);
  567. if (gpio_is_valid(chip->reset_pin)) {
  568. gpio_set_value(chip->reset_pin, 0);
  569. /* AC97 v2.2 specifications says minimum 1 us. */
  570. udelay(10);
  571. gpio_set_value(chip->reset_pin, 1);
  572. }
  573. udelay(1);
  574. ac97c_writel(chip, MR, AC97C_MR_ENA);
  575. }
  576. static int __devinit atmel_ac97c_probe(struct platform_device *pdev)
  577. {
  578. struct snd_card *card;
  579. struct atmel_ac97c *chip;
  580. struct resource *regs;
  581. struct ac97c_platform_data *pdata;
  582. struct clk *pclk;
  583. static struct snd_ac97_bus_ops ops = {
  584. .write = atmel_ac97c_write,
  585. .read = atmel_ac97c_read,
  586. };
  587. int retval;
  588. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  589. if (!regs) {
  590. dev_dbg(&pdev->dev, "no memory resource\n");
  591. return -ENXIO;
  592. }
  593. pdata = pdev->dev.platform_data;
  594. if (!pdata) {
  595. dev_dbg(&pdev->dev, "no platform data\n");
  596. return -ENXIO;
  597. }
  598. pclk = clk_get(&pdev->dev, "pclk");
  599. if (IS_ERR(pclk)) {
  600. dev_dbg(&pdev->dev, "no peripheral clock\n");
  601. return PTR_ERR(pclk);
  602. }
  603. clk_enable(pclk);
  604. retval = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  605. THIS_MODULE, sizeof(struct atmel_ac97c), &card);
  606. if (retval) {
  607. dev_dbg(&pdev->dev, "could not create sound card device\n");
  608. goto err_snd_card_new;
  609. }
  610. chip = get_chip(card);
  611. spin_lock_init(&chip->lock);
  612. strcpy(card->driver, "Atmel AC97C");
  613. strcpy(card->shortname, "Atmel AC97C");
  614. sprintf(card->longname, "Atmel AC97 controller");
  615. chip->card = card;
  616. chip->pclk = pclk;
  617. chip->pdev = pdev;
  618. chip->regs = ioremap(regs->start, regs->end - regs->start + 1);
  619. if (!chip->regs) {
  620. dev_dbg(&pdev->dev, "could not remap register memory\n");
  621. goto err_ioremap;
  622. }
  623. if (gpio_is_valid(pdata->reset_pin)) {
  624. if (gpio_request(pdata->reset_pin, "reset_pin")) {
  625. dev_dbg(&pdev->dev, "reset pin not available\n");
  626. chip->reset_pin = -ENODEV;
  627. } else {
  628. gpio_direction_output(pdata->reset_pin, 1);
  629. chip->reset_pin = pdata->reset_pin;
  630. }
  631. }
  632. snd_card_set_dev(card, &pdev->dev);
  633. retval = snd_ac97_bus(card, 0, &ops, chip, &chip->ac97_bus);
  634. if (retval) {
  635. dev_dbg(&pdev->dev, "could not register on ac97 bus\n");
  636. goto err_ac97_bus;
  637. }
  638. atmel_ac97c_reset(chip);
  639. retval = atmel_ac97c_mixer_new(chip);
  640. if (retval) {
  641. dev_dbg(&pdev->dev, "could not register ac97 mixer\n");
  642. goto err_ac97_bus;
  643. }
  644. if (pdata->rx_dws.dma_dev) {
  645. struct dw_dma_slave *dws = &pdata->rx_dws;
  646. dma_cap_mask_t mask;
  647. dws->rx_reg = regs->start + AC97C_CARHR + 2;
  648. dma_cap_zero(mask);
  649. dma_cap_set(DMA_SLAVE, mask);
  650. chip->dma.rx_chan = dma_request_channel(mask, filter, dws);
  651. dev_info(&chip->pdev->dev, "using %s for DMA RX\n",
  652. chip->dma.rx_chan->dev->device.bus_id);
  653. set_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
  654. }
  655. if (pdata->tx_dws.dma_dev) {
  656. struct dw_dma_slave *dws = &pdata->tx_dws;
  657. dma_cap_mask_t mask;
  658. dws->tx_reg = regs->start + AC97C_CATHR + 2;
  659. dma_cap_zero(mask);
  660. dma_cap_set(DMA_SLAVE, mask);
  661. chip->dma.tx_chan = dma_request_channel(mask, filter, dws);
  662. dev_info(&chip->pdev->dev, "using %s for DMA TX\n",
  663. chip->dma.tx_chan->dev->device.bus_id);
  664. set_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
  665. }
  666. if (!test_bit(DMA_RX_CHAN_PRESENT, &chip->flags) &&
  667. !test_bit(DMA_TX_CHAN_PRESENT, &chip->flags)) {
  668. dev_dbg(&pdev->dev, "DMA not available\n");
  669. retval = -ENODEV;
  670. goto err_dma;
  671. }
  672. retval = atmel_ac97c_pcm_new(chip);
  673. if (retval) {
  674. dev_dbg(&pdev->dev, "could not register ac97 pcm device\n");
  675. goto err_dma;
  676. }
  677. retval = snd_card_register(card);
  678. if (retval) {
  679. dev_dbg(&pdev->dev, "could not register sound card\n");
  680. goto err_ac97_bus;
  681. }
  682. platform_set_drvdata(pdev, card);
  683. dev_info(&pdev->dev, "Atmel AC97 controller at 0x%p\n",
  684. chip->regs);
  685. return 0;
  686. err_dma:
  687. if (test_bit(DMA_RX_CHAN_PRESENT, &chip->flags))
  688. dma_release_channel(chip->dma.rx_chan);
  689. if (test_bit(DMA_TX_CHAN_PRESENT, &chip->flags))
  690. dma_release_channel(chip->dma.tx_chan);
  691. clear_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
  692. clear_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
  693. chip->dma.rx_chan = NULL;
  694. chip->dma.tx_chan = NULL;
  695. err_ac97_bus:
  696. snd_card_set_dev(card, NULL);
  697. if (gpio_is_valid(chip->reset_pin))
  698. gpio_free(chip->reset_pin);
  699. iounmap(chip->regs);
  700. err_ioremap:
  701. snd_card_free(card);
  702. err_snd_card_new:
  703. clk_disable(pclk);
  704. clk_put(pclk);
  705. return retval;
  706. }
  707. #ifdef CONFIG_PM
  708. static int atmel_ac97c_suspend(struct platform_device *pdev, pm_message_t msg)
  709. {
  710. struct snd_card *card = platform_get_drvdata(pdev);
  711. struct atmel_ac97c *chip = card->private_data;
  712. if (test_bit(DMA_RX_READY, &chip->flags))
  713. dw_dma_cyclic_stop(chip->dma.rx_chan);
  714. if (test_bit(DMA_TX_READY, &chip->flags))
  715. dw_dma_cyclic_stop(chip->dma.tx_chan);
  716. clk_disable(chip->pclk);
  717. return 0;
  718. }
  719. static int atmel_ac97c_resume(struct platform_device *pdev)
  720. {
  721. struct snd_card *card = platform_get_drvdata(pdev);
  722. struct atmel_ac97c *chip = card->private_data;
  723. clk_enable(chip->pclk);
  724. if (test_bit(DMA_RX_READY, &chip->flags))
  725. dw_dma_cyclic_start(chip->dma.rx_chan);
  726. if (test_bit(DMA_TX_READY, &chip->flags))
  727. dw_dma_cyclic_start(chip->dma.tx_chan);
  728. return 0;
  729. }
  730. #else
  731. #define atmel_ac97c_suspend NULL
  732. #define atmel_ac97c_resume NULL
  733. #endif
  734. static int __devexit atmel_ac97c_remove(struct platform_device *pdev)
  735. {
  736. struct snd_card *card = platform_get_drvdata(pdev);
  737. struct atmel_ac97c *chip = get_chip(card);
  738. if (gpio_is_valid(chip->reset_pin))
  739. gpio_free(chip->reset_pin);
  740. clk_disable(chip->pclk);
  741. clk_put(chip->pclk);
  742. iounmap(chip->regs);
  743. if (test_bit(DMA_RX_CHAN_PRESENT, &chip->flags))
  744. dma_release_channel(chip->dma.rx_chan);
  745. if (test_bit(DMA_TX_CHAN_PRESENT, &chip->flags))
  746. dma_release_channel(chip->dma.tx_chan);
  747. clear_bit(DMA_RX_CHAN_PRESENT, &chip->flags);
  748. clear_bit(DMA_TX_CHAN_PRESENT, &chip->flags);
  749. chip->dma.rx_chan = NULL;
  750. chip->dma.tx_chan = NULL;
  751. snd_card_set_dev(card, NULL);
  752. snd_card_free(card);
  753. platform_set_drvdata(pdev, NULL);
  754. return 0;
  755. }
  756. static struct platform_driver atmel_ac97c_driver = {
  757. .remove = __devexit_p(atmel_ac97c_remove),
  758. .driver = {
  759. .name = "atmel_ac97c",
  760. },
  761. .suspend = atmel_ac97c_suspend,
  762. .resume = atmel_ac97c_resume,
  763. };
  764. static int __init atmel_ac97c_init(void)
  765. {
  766. return platform_driver_probe(&atmel_ac97c_driver,
  767. atmel_ac97c_probe);
  768. }
  769. module_init(atmel_ac97c_init);
  770. static void __exit atmel_ac97c_exit(void)
  771. {
  772. platform_driver_unregister(&atmel_ac97c_driver);
  773. }
  774. module_exit(atmel_ac97c_exit);
  775. MODULE_LICENSE("GPL");
  776. MODULE_DESCRIPTION("Driver for Atmel AC97 controller");
  777. MODULE_AUTHOR("Hans-Christian Egtvedt <hans-christian.egtvedt@atmel.com>");