ac97c.c 25 KB

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