abdac.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /*
  2. * Driver for the Atmel on-chip Audio Bitstream DAC (ABDAC)
  3. *
  4. * Copyright (C) 2006-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/bitmap.h>
  12. #include <linux/dmaengine.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/types.h>
  19. #include <linux/io.h>
  20. #include <sound/core.h>
  21. #include <sound/initval.h>
  22. #include <sound/pcm.h>
  23. #include <sound/pcm_params.h>
  24. #include <sound/atmel-abdac.h>
  25. #include <linux/platform_data/dma-dw.h>
  26. #include <linux/dma/dw.h>
  27. /* DAC register offsets */
  28. #define DAC_DATA 0x0000
  29. #define DAC_CTRL 0x0008
  30. #define DAC_INT_MASK 0x000c
  31. #define DAC_INT_EN 0x0010
  32. #define DAC_INT_DIS 0x0014
  33. #define DAC_INT_CLR 0x0018
  34. #define DAC_INT_STATUS 0x001c
  35. /* Bitfields in CTRL */
  36. #define DAC_SWAP_OFFSET 30
  37. #define DAC_SWAP_SIZE 1
  38. #define DAC_EN_OFFSET 31
  39. #define DAC_EN_SIZE 1
  40. /* Bitfields in INT_MASK/INT_EN/INT_DIS/INT_STATUS/INT_CLR */
  41. #define DAC_UNDERRUN_OFFSET 28
  42. #define DAC_UNDERRUN_SIZE 1
  43. #define DAC_TX_READY_OFFSET 29
  44. #define DAC_TX_READY_SIZE 1
  45. /* Bit manipulation macros */
  46. #define DAC_BIT(name) \
  47. (1 << DAC_##name##_OFFSET)
  48. #define DAC_BF(name, value) \
  49. (((value) & ((1 << DAC_##name##_SIZE) - 1)) \
  50. << DAC_##name##_OFFSET)
  51. #define DAC_BFEXT(name, value) \
  52. (((value) >> DAC_##name##_OFFSET) \
  53. & ((1 << DAC_##name##_SIZE) - 1))
  54. #define DAC_BFINS(name, value, old) \
  55. (((old) & ~(((1 << DAC_##name##_SIZE) - 1) \
  56. << DAC_##name##_OFFSET)) \
  57. | DAC_BF(name, value))
  58. /* Register access macros */
  59. #define dac_readl(port, reg) \
  60. __raw_readl((port)->regs + DAC_##reg)
  61. #define dac_writel(port, reg, value) \
  62. __raw_writel((value), (port)->regs + DAC_##reg)
  63. /*
  64. * ABDAC supports a maximum of 6 different rates from a generic clock. The
  65. * generic clock has a power of two divider, which gives 6 steps from 192 kHz
  66. * to 5112 Hz.
  67. */
  68. #define MAX_NUM_RATES 6
  69. /* ALSA seems to use rates between 192000 Hz and 5112 Hz. */
  70. #define RATE_MAX 192000
  71. #define RATE_MIN 5112
  72. enum {
  73. DMA_READY = 0,
  74. };
  75. struct atmel_abdac_dma {
  76. struct dma_chan *chan;
  77. struct dw_cyclic_desc *cdesc;
  78. };
  79. struct atmel_abdac {
  80. struct clk *pclk;
  81. struct clk *sample_clk;
  82. struct platform_device *pdev;
  83. struct atmel_abdac_dma dma;
  84. struct snd_pcm_hw_constraint_list constraints_rates;
  85. struct snd_pcm_substream *substream;
  86. struct snd_card *card;
  87. struct snd_pcm *pcm;
  88. void __iomem *regs;
  89. unsigned long flags;
  90. unsigned int rates[MAX_NUM_RATES];
  91. unsigned int rates_num;
  92. int irq;
  93. };
  94. #define get_dac(card) ((struct atmel_abdac *)(card)->private_data)
  95. /* This function is called by the DMA driver. */
  96. static void atmel_abdac_dma_period_done(void *arg)
  97. {
  98. struct atmel_abdac *dac = arg;
  99. snd_pcm_period_elapsed(dac->substream);
  100. }
  101. static int atmel_abdac_prepare_dma(struct atmel_abdac *dac,
  102. struct snd_pcm_substream *substream,
  103. enum dma_data_direction direction)
  104. {
  105. struct dma_chan *chan = dac->dma.chan;
  106. struct dw_cyclic_desc *cdesc;
  107. struct snd_pcm_runtime *runtime = substream->runtime;
  108. unsigned long buffer_len, period_len;
  109. /*
  110. * We don't do DMA on "complex" transfers, i.e. with
  111. * non-halfword-aligned buffers or lengths.
  112. */
  113. if (runtime->dma_addr & 1 || runtime->buffer_size & 1) {
  114. dev_dbg(&dac->pdev->dev, "too complex transfer\n");
  115. return -EINVAL;
  116. }
  117. buffer_len = frames_to_bytes(runtime, runtime->buffer_size);
  118. period_len = frames_to_bytes(runtime, runtime->period_size);
  119. cdesc = dw_dma_cyclic_prep(chan, runtime->dma_addr, buffer_len,
  120. period_len, DMA_MEM_TO_DEV);
  121. if (IS_ERR(cdesc)) {
  122. dev_dbg(&dac->pdev->dev, "could not prepare cyclic DMA\n");
  123. return PTR_ERR(cdesc);
  124. }
  125. cdesc->period_callback = atmel_abdac_dma_period_done;
  126. cdesc->period_callback_param = dac;
  127. dac->dma.cdesc = cdesc;
  128. set_bit(DMA_READY, &dac->flags);
  129. return 0;
  130. }
  131. static struct snd_pcm_hardware atmel_abdac_hw = {
  132. .info = (SNDRV_PCM_INFO_MMAP
  133. | SNDRV_PCM_INFO_MMAP_VALID
  134. | SNDRV_PCM_INFO_INTERLEAVED
  135. | SNDRV_PCM_INFO_BLOCK_TRANSFER
  136. | SNDRV_PCM_INFO_RESUME
  137. | SNDRV_PCM_INFO_PAUSE),
  138. .formats = (SNDRV_PCM_FMTBIT_S16_BE),
  139. .rates = (SNDRV_PCM_RATE_KNOT),
  140. .rate_min = RATE_MIN,
  141. .rate_max = RATE_MAX,
  142. .channels_min = 2,
  143. .channels_max = 2,
  144. .buffer_bytes_max = 64 * 4096,
  145. .period_bytes_min = 4096,
  146. .period_bytes_max = 4096,
  147. .periods_min = 6,
  148. .periods_max = 64,
  149. };
  150. static int atmel_abdac_open(struct snd_pcm_substream *substream)
  151. {
  152. struct atmel_abdac *dac = snd_pcm_substream_chip(substream);
  153. dac->substream = substream;
  154. atmel_abdac_hw.rate_max = dac->rates[dac->rates_num - 1];
  155. atmel_abdac_hw.rate_min = dac->rates[0];
  156. substream->runtime->hw = atmel_abdac_hw;
  157. return snd_pcm_hw_constraint_list(substream->runtime, 0,
  158. SNDRV_PCM_HW_PARAM_RATE, &dac->constraints_rates);
  159. }
  160. static int atmel_abdac_close(struct snd_pcm_substream *substream)
  161. {
  162. struct atmel_abdac *dac = snd_pcm_substream_chip(substream);
  163. dac->substream = NULL;
  164. return 0;
  165. }
  166. static int atmel_abdac_hw_params(struct snd_pcm_substream *substream,
  167. struct snd_pcm_hw_params *hw_params)
  168. {
  169. struct atmel_abdac *dac = snd_pcm_substream_chip(substream);
  170. int retval;
  171. retval = snd_pcm_lib_malloc_pages(substream,
  172. params_buffer_bytes(hw_params));
  173. if (retval < 0)
  174. return retval;
  175. /* snd_pcm_lib_malloc_pages returns 1 if buffer is changed. */
  176. if (retval == 1)
  177. if (test_and_clear_bit(DMA_READY, &dac->flags))
  178. dw_dma_cyclic_free(dac->dma.chan);
  179. return retval;
  180. }
  181. static int atmel_abdac_hw_free(struct snd_pcm_substream *substream)
  182. {
  183. struct atmel_abdac *dac = snd_pcm_substream_chip(substream);
  184. if (test_and_clear_bit(DMA_READY, &dac->flags))
  185. dw_dma_cyclic_free(dac->dma.chan);
  186. return snd_pcm_lib_free_pages(substream);
  187. }
  188. static int atmel_abdac_prepare(struct snd_pcm_substream *substream)
  189. {
  190. struct atmel_abdac *dac = snd_pcm_substream_chip(substream);
  191. int retval;
  192. retval = clk_set_rate(dac->sample_clk, 256 * substream->runtime->rate);
  193. if (retval)
  194. return retval;
  195. if (!test_bit(DMA_READY, &dac->flags))
  196. retval = atmel_abdac_prepare_dma(dac, substream, DMA_TO_DEVICE);
  197. return retval;
  198. }
  199. static int atmel_abdac_trigger(struct snd_pcm_substream *substream, int cmd)
  200. {
  201. struct atmel_abdac *dac = snd_pcm_substream_chip(substream);
  202. int retval = 0;
  203. switch (cmd) {
  204. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
  205. case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
  206. case SNDRV_PCM_TRIGGER_START:
  207. clk_prepare_enable(dac->sample_clk);
  208. retval = dw_dma_cyclic_start(dac->dma.chan);
  209. if (retval)
  210. goto out;
  211. dac_writel(dac, CTRL, DAC_BIT(EN));
  212. break;
  213. case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
  214. case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
  215. case SNDRV_PCM_TRIGGER_STOP:
  216. dw_dma_cyclic_stop(dac->dma.chan);
  217. dac_writel(dac, DATA, 0);
  218. dac_writel(dac, CTRL, 0);
  219. clk_disable_unprepare(dac->sample_clk);
  220. break;
  221. default:
  222. retval = -EINVAL;
  223. break;
  224. }
  225. out:
  226. return retval;
  227. }
  228. static snd_pcm_uframes_t
  229. atmel_abdac_pointer(struct snd_pcm_substream *substream)
  230. {
  231. struct atmel_abdac *dac = snd_pcm_substream_chip(substream);
  232. struct snd_pcm_runtime *runtime = substream->runtime;
  233. snd_pcm_uframes_t frames;
  234. unsigned long bytes;
  235. bytes = dw_dma_get_src_addr(dac->dma.chan);
  236. bytes -= runtime->dma_addr;
  237. frames = bytes_to_frames(runtime, bytes);
  238. if (frames >= runtime->buffer_size)
  239. frames -= runtime->buffer_size;
  240. return frames;
  241. }
  242. static irqreturn_t abdac_interrupt(int irq, void *dev_id)
  243. {
  244. struct atmel_abdac *dac = dev_id;
  245. u32 status;
  246. status = dac_readl(dac, INT_STATUS);
  247. if (status & DAC_BIT(UNDERRUN)) {
  248. dev_err(&dac->pdev->dev, "underrun detected\n");
  249. dac_writel(dac, INT_CLR, DAC_BIT(UNDERRUN));
  250. } else {
  251. dev_err(&dac->pdev->dev, "spurious interrupt (status=0x%x)\n",
  252. status);
  253. dac_writel(dac, INT_CLR, status);
  254. }
  255. return IRQ_HANDLED;
  256. }
  257. static struct snd_pcm_ops atmel_abdac_ops = {
  258. .open = atmel_abdac_open,
  259. .close = atmel_abdac_close,
  260. .ioctl = snd_pcm_lib_ioctl,
  261. .hw_params = atmel_abdac_hw_params,
  262. .hw_free = atmel_abdac_hw_free,
  263. .prepare = atmel_abdac_prepare,
  264. .trigger = atmel_abdac_trigger,
  265. .pointer = atmel_abdac_pointer,
  266. };
  267. static int atmel_abdac_pcm_new(struct atmel_abdac *dac)
  268. {
  269. struct snd_pcm_hardware hw = atmel_abdac_hw;
  270. struct snd_pcm *pcm;
  271. int retval;
  272. retval = snd_pcm_new(dac->card, dac->card->shortname,
  273. dac->pdev->id, 1, 0, &pcm);
  274. if (retval)
  275. return retval;
  276. strcpy(pcm->name, dac->card->shortname);
  277. pcm->private_data = dac;
  278. pcm->info_flags = 0;
  279. dac->pcm = pcm;
  280. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &atmel_abdac_ops);
  281. retval = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  282. &dac->pdev->dev, hw.periods_min * hw.period_bytes_min,
  283. hw.buffer_bytes_max);
  284. return retval;
  285. }
  286. static bool filter(struct dma_chan *chan, void *slave)
  287. {
  288. struct dw_dma_slave *dws = slave;
  289. if (dws->dma_dev == chan->device->dev) {
  290. chan->private = dws;
  291. return true;
  292. } else
  293. return false;
  294. }
  295. static int set_sample_rates(struct atmel_abdac *dac)
  296. {
  297. long new_rate = RATE_MAX;
  298. int retval = -EINVAL;
  299. int index = 0;
  300. /* we start at 192 kHz and work our way down to 5112 Hz */
  301. while (new_rate >= RATE_MIN && index < (MAX_NUM_RATES + 1)) {
  302. new_rate = clk_round_rate(dac->sample_clk, 256 * new_rate);
  303. if (new_rate <= 0)
  304. break;
  305. /* make sure we are below the ABDAC clock */
  306. if (index < MAX_NUM_RATES &&
  307. new_rate <= clk_get_rate(dac->pclk)) {
  308. dac->rates[index] = new_rate / 256;
  309. index++;
  310. }
  311. /* divide by 256 and then by two to get next rate */
  312. new_rate /= 256 * 2;
  313. }
  314. if (index) {
  315. int i;
  316. /* reverse array, smallest go first */
  317. for (i = 0; i < (index / 2); i++) {
  318. unsigned int tmp = dac->rates[index - 1 - i];
  319. dac->rates[index - 1 - i] = dac->rates[i];
  320. dac->rates[i] = tmp;
  321. }
  322. dac->constraints_rates.count = index;
  323. dac->constraints_rates.list = dac->rates;
  324. dac->constraints_rates.mask = 0;
  325. dac->rates_num = index;
  326. retval = 0;
  327. }
  328. return retval;
  329. }
  330. static int atmel_abdac_probe(struct platform_device *pdev)
  331. {
  332. struct snd_card *card;
  333. struct atmel_abdac *dac;
  334. struct resource *regs;
  335. struct atmel_abdac_pdata *pdata;
  336. struct clk *pclk;
  337. struct clk *sample_clk;
  338. int retval;
  339. int irq;
  340. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  341. if (!regs) {
  342. dev_dbg(&pdev->dev, "no memory resource\n");
  343. return -ENXIO;
  344. }
  345. irq = platform_get_irq(pdev, 0);
  346. if (irq < 0) {
  347. dev_dbg(&pdev->dev, "could not get IRQ number\n");
  348. return irq;
  349. }
  350. pdata = pdev->dev.platform_data;
  351. if (!pdata) {
  352. dev_dbg(&pdev->dev, "no platform data\n");
  353. return -ENXIO;
  354. }
  355. pclk = clk_get(&pdev->dev, "pclk");
  356. if (IS_ERR(pclk)) {
  357. dev_dbg(&pdev->dev, "no peripheral clock\n");
  358. return PTR_ERR(pclk);
  359. }
  360. sample_clk = clk_get(&pdev->dev, "sample_clk");
  361. if (IS_ERR(sample_clk)) {
  362. dev_dbg(&pdev->dev, "no sample clock\n");
  363. retval = PTR_ERR(sample_clk);
  364. goto out_put_pclk;
  365. }
  366. clk_prepare_enable(pclk);
  367. retval = snd_card_new(&pdev->dev, SNDRV_DEFAULT_IDX1,
  368. SNDRV_DEFAULT_STR1, THIS_MODULE,
  369. sizeof(struct atmel_abdac), &card);
  370. if (retval) {
  371. dev_dbg(&pdev->dev, "could not create sound card device\n");
  372. goto out_put_sample_clk;
  373. }
  374. dac = get_dac(card);
  375. dac->irq = irq;
  376. dac->card = card;
  377. dac->pclk = pclk;
  378. dac->sample_clk = sample_clk;
  379. dac->pdev = pdev;
  380. retval = set_sample_rates(dac);
  381. if (retval < 0) {
  382. dev_dbg(&pdev->dev, "could not set supported rates\n");
  383. goto out_free_card;
  384. }
  385. dac->regs = ioremap(regs->start, resource_size(regs));
  386. if (!dac->regs) {
  387. dev_dbg(&pdev->dev, "could not remap register memory\n");
  388. retval = -ENOMEM;
  389. goto out_free_card;
  390. }
  391. /* make sure the DAC is silent and disabled */
  392. dac_writel(dac, DATA, 0);
  393. dac_writel(dac, CTRL, 0);
  394. retval = request_irq(irq, abdac_interrupt, 0, "abdac", dac);
  395. if (retval) {
  396. dev_dbg(&pdev->dev, "could not request irq\n");
  397. goto out_unmap_regs;
  398. }
  399. if (pdata->dws.dma_dev) {
  400. dma_cap_mask_t mask;
  401. dma_cap_zero(mask);
  402. dma_cap_set(DMA_SLAVE, mask);
  403. dac->dma.chan = dma_request_channel(mask, filter, &pdata->dws);
  404. if (dac->dma.chan) {
  405. struct dma_slave_config dma_conf = {
  406. .dst_addr = regs->start + DAC_DATA,
  407. .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
  408. .src_maxburst = 1,
  409. .dst_maxburst = 1,
  410. .direction = DMA_MEM_TO_DEV,
  411. .device_fc = false,
  412. };
  413. dmaengine_slave_config(dac->dma.chan, &dma_conf);
  414. }
  415. }
  416. if (!pdata->dws.dma_dev || !dac->dma.chan) {
  417. dev_dbg(&pdev->dev, "DMA not available\n");
  418. retval = -ENODEV;
  419. goto out_unmap_regs;
  420. }
  421. strcpy(card->driver, "Atmel ABDAC");
  422. strcpy(card->shortname, "Atmel ABDAC");
  423. sprintf(card->longname, "Atmel Audio Bitstream DAC");
  424. retval = atmel_abdac_pcm_new(dac);
  425. if (retval) {
  426. dev_dbg(&pdev->dev, "could not register ABDAC pcm device\n");
  427. goto out_release_dma;
  428. }
  429. retval = snd_card_register(card);
  430. if (retval) {
  431. dev_dbg(&pdev->dev, "could not register sound card\n");
  432. goto out_release_dma;
  433. }
  434. platform_set_drvdata(pdev, card);
  435. dev_info(&pdev->dev, "Atmel ABDAC at 0x%p using %s\n",
  436. dac->regs, dev_name(&dac->dma.chan->dev->device));
  437. return retval;
  438. out_release_dma:
  439. dma_release_channel(dac->dma.chan);
  440. dac->dma.chan = NULL;
  441. out_unmap_regs:
  442. iounmap(dac->regs);
  443. out_free_card:
  444. snd_card_free(card);
  445. out_put_sample_clk:
  446. clk_put(sample_clk);
  447. clk_disable_unprepare(pclk);
  448. out_put_pclk:
  449. clk_put(pclk);
  450. return retval;
  451. }
  452. #ifdef CONFIG_PM_SLEEP
  453. static int atmel_abdac_suspend(struct device *pdev)
  454. {
  455. struct snd_card *card = dev_get_drvdata(pdev);
  456. struct atmel_abdac *dac = card->private_data;
  457. dw_dma_cyclic_stop(dac->dma.chan);
  458. clk_disable_unprepare(dac->sample_clk);
  459. clk_disable_unprepare(dac->pclk);
  460. return 0;
  461. }
  462. static int atmel_abdac_resume(struct device *pdev)
  463. {
  464. struct snd_card *card = dev_get_drvdata(pdev);
  465. struct atmel_abdac *dac = card->private_data;
  466. clk_prepare_enable(dac->pclk);
  467. clk_prepare_enable(dac->sample_clk);
  468. if (test_bit(DMA_READY, &dac->flags))
  469. dw_dma_cyclic_start(dac->dma.chan);
  470. return 0;
  471. }
  472. static SIMPLE_DEV_PM_OPS(atmel_abdac_pm, atmel_abdac_suspend, atmel_abdac_resume);
  473. #define ATMEL_ABDAC_PM_OPS &atmel_abdac_pm
  474. #else
  475. #define ATMEL_ABDAC_PM_OPS NULL
  476. #endif
  477. static int atmel_abdac_remove(struct platform_device *pdev)
  478. {
  479. struct snd_card *card = platform_get_drvdata(pdev);
  480. struct atmel_abdac *dac = get_dac(card);
  481. clk_put(dac->sample_clk);
  482. clk_disable_unprepare(dac->pclk);
  483. clk_put(dac->pclk);
  484. dma_release_channel(dac->dma.chan);
  485. dac->dma.chan = NULL;
  486. iounmap(dac->regs);
  487. free_irq(dac->irq, dac);
  488. snd_card_free(card);
  489. return 0;
  490. }
  491. static struct platform_driver atmel_abdac_driver = {
  492. .remove = atmel_abdac_remove,
  493. .driver = {
  494. .name = "atmel_abdac",
  495. .pm = ATMEL_ABDAC_PM_OPS,
  496. },
  497. };
  498. static int __init atmel_abdac_init(void)
  499. {
  500. return platform_driver_probe(&atmel_abdac_driver,
  501. atmel_abdac_probe);
  502. }
  503. module_init(atmel_abdac_init);
  504. static void __exit atmel_abdac_exit(void)
  505. {
  506. platform_driver_unregister(&atmel_abdac_driver);
  507. }
  508. module_exit(atmel_abdac_exit);
  509. MODULE_LICENSE("GPL");
  510. MODULE_DESCRIPTION("Driver for Atmel Audio Bitstream DAC (ABDAC)");
  511. MODULE_AUTHOR("Hans-Christian Egtvedt <egtvedt@samfundet.no>");