cs5535audio_pcm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. * Driver for audio on multifunction CS5535 companion device
  3. * Copyright (C) Jaya Kumar
  4. *
  5. * Based on Jaroslav Kysela and Takashi Iwai's examples.
  6. * This work was sponsored by CIS(M) Sdn Bhd.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. * todo: add be fmt support, spdif, pm
  23. */
  24. #include <linux/init.h>
  25. #include <linux/pci.h>
  26. #include <sound/core.h>
  27. #include <sound/control.h>
  28. #include <sound/initval.h>
  29. #include <sound/asoundef.h>
  30. #include <sound/pcm.h>
  31. #include <sound/pcm_params.h>
  32. #include <sound/ac97_codec.h>
  33. #include "cs5535audio.h"
  34. static struct snd_pcm_hardware snd_cs5535audio_playback =
  35. {
  36. .info = (
  37. SNDRV_PCM_INFO_MMAP |
  38. SNDRV_PCM_INFO_INTERLEAVED |
  39. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  40. SNDRV_PCM_INFO_MMAP_VALID |
  41. SNDRV_PCM_INFO_PAUSE |
  42. SNDRV_PCM_INFO_RESUME
  43. ),
  44. .formats = (
  45. SNDRV_PCM_FMTBIT_S16_LE
  46. ),
  47. .rates = (
  48. SNDRV_PCM_RATE_CONTINUOUS |
  49. SNDRV_PCM_RATE_8000_48000
  50. ),
  51. .rate_min = 4000,
  52. .rate_max = 48000,
  53. .channels_min = 2,
  54. .channels_max = 2,
  55. .buffer_bytes_max = (128*1024),
  56. .period_bytes_min = 64,
  57. .period_bytes_max = (64*1024 - 16),
  58. .periods_min = 1,
  59. .periods_max = CS5535AUDIO_MAX_DESCRIPTORS,
  60. .fifo_size = 0,
  61. };
  62. static struct snd_pcm_hardware snd_cs5535audio_capture =
  63. {
  64. .info = (
  65. SNDRV_PCM_INFO_MMAP |
  66. SNDRV_PCM_INFO_INTERLEAVED |
  67. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  68. SNDRV_PCM_INFO_MMAP_VALID
  69. ),
  70. .formats = (
  71. SNDRV_PCM_FMTBIT_S16_LE
  72. ),
  73. .rates = (
  74. SNDRV_PCM_RATE_CONTINUOUS |
  75. SNDRV_PCM_RATE_8000_48000
  76. ),
  77. .rate_min = 4000,
  78. .rate_max = 48000,
  79. .channels_min = 2,
  80. .channels_max = 2,
  81. .buffer_bytes_max = (128*1024),
  82. .period_bytes_min = 64,
  83. .period_bytes_max = (64*1024 - 16),
  84. .periods_min = 1,
  85. .periods_max = CS5535AUDIO_MAX_DESCRIPTORS,
  86. .fifo_size = 0,
  87. };
  88. static int snd_cs5535audio_playback_open(struct snd_pcm_substream *substream)
  89. {
  90. int err;
  91. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  92. struct snd_pcm_runtime *runtime = substream->runtime;
  93. runtime->hw = snd_cs5535audio_playback;
  94. runtime->hw.rates = cs5535au->ac97->rates[AC97_RATES_FRONT_DAC];
  95. snd_pcm_limit_hw_rates(runtime);
  96. cs5535au->playback_substream = substream;
  97. runtime->private_data = &(cs5535au->dmas[CS5535AUDIO_DMA_PLAYBACK]);
  98. if ((err = snd_pcm_hw_constraint_integer(runtime,
  99. SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  100. return err;
  101. return 0;
  102. }
  103. static int snd_cs5535audio_playback_close(struct snd_pcm_substream *substream)
  104. {
  105. return 0;
  106. }
  107. #define CS5535AUDIO_DESC_LIST_SIZE \
  108. PAGE_ALIGN(CS5535AUDIO_MAX_DESCRIPTORS * sizeof(struct cs5535audio_dma_desc))
  109. static int cs5535audio_build_dma_packets(struct cs5535audio *cs5535au,
  110. struct cs5535audio_dma *dma,
  111. struct snd_pcm_substream *substream,
  112. unsigned int periods,
  113. unsigned int period_bytes)
  114. {
  115. unsigned int i;
  116. u32 addr, desc_addr, jmpprd_addr;
  117. struct cs5535audio_dma_desc *lastdesc;
  118. if (periods > CS5535AUDIO_MAX_DESCRIPTORS)
  119. return -ENOMEM;
  120. if (dma->desc_buf.area == NULL) {
  121. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
  122. snd_dma_pci_data(cs5535au->pci),
  123. CS5535AUDIO_DESC_LIST_SIZE+1,
  124. &dma->desc_buf) < 0)
  125. return -ENOMEM;
  126. dma->period_bytes = dma->periods = 0;
  127. }
  128. if (dma->periods == periods && dma->period_bytes == period_bytes)
  129. return 0;
  130. /* the u32 cast is okay because in snd*create we successfully told
  131. pci alloc that we're only 32 bit capable so the uppper will be 0 */
  132. addr = (u32) substream->runtime->dma_addr;
  133. desc_addr = (u32) dma->desc_buf.addr;
  134. for (i = 0; i < periods; i++) {
  135. struct cs5535audio_dma_desc *desc =
  136. &((struct cs5535audio_dma_desc *) dma->desc_buf.area)[i];
  137. desc->addr = cpu_to_le32(addr);
  138. desc->size = cpu_to_le16(period_bytes);
  139. desc->ctlreserved = cpu_to_le16(PRD_EOP);
  140. desc_addr += sizeof(struct cs5535audio_dma_desc);
  141. addr += period_bytes;
  142. }
  143. /* we reserved one dummy descriptor at the end to do the PRD jump */
  144. lastdesc = &((struct cs5535audio_dma_desc *) dma->desc_buf.area)[periods];
  145. lastdesc->addr = cpu_to_le32((u32) dma->desc_buf.addr);
  146. lastdesc->size = 0;
  147. lastdesc->ctlreserved = cpu_to_le16(PRD_JMP);
  148. jmpprd_addr = (u32)dma->desc_buf.addr +
  149. sizeof(struct cs5535audio_dma_desc) * periods;
  150. dma->substream = substream;
  151. dma->period_bytes = period_bytes;
  152. dma->periods = periods;
  153. spin_lock_irq(&cs5535au->reg_lock);
  154. dma->ops->disable_dma(cs5535au);
  155. dma->ops->setup_prd(cs5535au, jmpprd_addr);
  156. spin_unlock_irq(&cs5535au->reg_lock);
  157. return 0;
  158. }
  159. static void cs5535audio_playback_enable_dma(struct cs5535audio *cs5535au)
  160. {
  161. cs_writeb(cs5535au, ACC_BM0_CMD, BM_CTL_EN);
  162. }
  163. static void cs5535audio_playback_disable_dma(struct cs5535audio *cs5535au)
  164. {
  165. cs_writeb(cs5535au, ACC_BM0_CMD, 0);
  166. }
  167. static void cs5535audio_playback_pause_dma(struct cs5535audio *cs5535au)
  168. {
  169. cs_writeb(cs5535au, ACC_BM0_CMD, BM_CTL_PAUSE);
  170. }
  171. static void cs5535audio_playback_setup_prd(struct cs5535audio *cs5535au,
  172. u32 prd_addr)
  173. {
  174. cs_writel(cs5535au, ACC_BM0_PRD, prd_addr);
  175. }
  176. static u32 cs5535audio_playback_read_prd(struct cs5535audio *cs5535au)
  177. {
  178. return cs_readl(cs5535au, ACC_BM0_PRD);
  179. }
  180. static u32 cs5535audio_playback_read_dma_pntr(struct cs5535audio *cs5535au)
  181. {
  182. return cs_readl(cs5535au, ACC_BM0_PNTR);
  183. }
  184. static void cs5535audio_capture_enable_dma(struct cs5535audio *cs5535au)
  185. {
  186. cs_writeb(cs5535au, ACC_BM1_CMD, BM_CTL_EN);
  187. }
  188. static void cs5535audio_capture_disable_dma(struct cs5535audio *cs5535au)
  189. {
  190. cs_writeb(cs5535au, ACC_BM1_CMD, 0);
  191. }
  192. static void cs5535audio_capture_pause_dma(struct cs5535audio *cs5535au)
  193. {
  194. cs_writeb(cs5535au, ACC_BM1_CMD, BM_CTL_PAUSE);
  195. }
  196. static void cs5535audio_capture_setup_prd(struct cs5535audio *cs5535au,
  197. u32 prd_addr)
  198. {
  199. cs_writel(cs5535au, ACC_BM1_PRD, prd_addr);
  200. }
  201. static u32 cs5535audio_capture_read_prd(struct cs5535audio *cs5535au)
  202. {
  203. return cs_readl(cs5535au, ACC_BM1_PRD);
  204. }
  205. static u32 cs5535audio_capture_read_dma_pntr(struct cs5535audio *cs5535au)
  206. {
  207. return cs_readl(cs5535au, ACC_BM1_PNTR);
  208. }
  209. static void cs5535audio_clear_dma_packets(struct cs5535audio *cs5535au,
  210. struct cs5535audio_dma *dma,
  211. struct snd_pcm_substream *substream)
  212. {
  213. snd_dma_free_pages(&dma->desc_buf);
  214. dma->desc_buf.area = NULL;
  215. dma->substream = NULL;
  216. }
  217. static int snd_cs5535audio_hw_params(struct snd_pcm_substream *substream,
  218. struct snd_pcm_hw_params *hw_params)
  219. {
  220. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  221. struct cs5535audio_dma *dma = substream->runtime->private_data;
  222. int err;
  223. err = snd_pcm_lib_malloc_pages(substream,
  224. params_buffer_bytes(hw_params));
  225. if (err < 0)
  226. return err;
  227. dma->buf_addr = substream->runtime->dma_addr;
  228. dma->buf_bytes = params_buffer_bytes(hw_params);
  229. err = cs5535audio_build_dma_packets(cs5535au, dma, substream,
  230. params_periods(hw_params),
  231. params_period_bytes(hw_params));
  232. if (!err)
  233. dma->pcm_open_flag = 1;
  234. return err;
  235. }
  236. static int snd_cs5535audio_hw_free(struct snd_pcm_substream *substream)
  237. {
  238. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  239. struct cs5535audio_dma *dma = substream->runtime->private_data;
  240. if (dma->pcm_open_flag) {
  241. if (substream == cs5535au->playback_substream)
  242. snd_ac97_update_power(cs5535au->ac97,
  243. AC97_PCM_FRONT_DAC_RATE, 0);
  244. else
  245. snd_ac97_update_power(cs5535au->ac97,
  246. AC97_PCM_LR_ADC_RATE, 0);
  247. dma->pcm_open_flag = 0;
  248. }
  249. cs5535audio_clear_dma_packets(cs5535au, dma, substream);
  250. return snd_pcm_lib_free_pages(substream);
  251. }
  252. static int snd_cs5535audio_playback_prepare(struct snd_pcm_substream *substream)
  253. {
  254. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  255. return snd_ac97_set_rate(cs5535au->ac97, AC97_PCM_FRONT_DAC_RATE,
  256. substream->runtime->rate);
  257. }
  258. static int snd_cs5535audio_trigger(struct snd_pcm_substream *substream, int cmd)
  259. {
  260. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  261. struct cs5535audio_dma *dma = substream->runtime->private_data;
  262. int err = 0;
  263. spin_lock(&cs5535au->reg_lock);
  264. switch (cmd) {
  265. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  266. dma->ops->pause_dma(cs5535au);
  267. break;
  268. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  269. dma->ops->enable_dma(cs5535au);
  270. break;
  271. case SNDRV_PCM_TRIGGER_START:
  272. dma->ops->enable_dma(cs5535au);
  273. break;
  274. case SNDRV_PCM_TRIGGER_RESUME:
  275. dma->ops->enable_dma(cs5535au);
  276. break;
  277. case SNDRV_PCM_TRIGGER_STOP:
  278. dma->ops->disable_dma(cs5535au);
  279. break;
  280. case SNDRV_PCM_TRIGGER_SUSPEND:
  281. dma->ops->disable_dma(cs5535au);
  282. break;
  283. default:
  284. dev_err(cs5535au->card->dev, "unhandled trigger\n");
  285. err = -EINVAL;
  286. break;
  287. }
  288. spin_unlock(&cs5535au->reg_lock);
  289. return err;
  290. }
  291. static snd_pcm_uframes_t snd_cs5535audio_pcm_pointer(struct snd_pcm_substream
  292. *substream)
  293. {
  294. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  295. u32 curdma;
  296. struct cs5535audio_dma *dma;
  297. dma = substream->runtime->private_data;
  298. curdma = dma->ops->read_dma_pntr(cs5535au);
  299. if (curdma < dma->buf_addr) {
  300. dev_err(cs5535au->card->dev, "curdma=%x < %x bufaddr.\n",
  301. curdma, dma->buf_addr);
  302. return 0;
  303. }
  304. curdma -= dma->buf_addr;
  305. if (curdma >= dma->buf_bytes) {
  306. dev_err(cs5535au->card->dev, "diff=%x >= %x buf_bytes.\n",
  307. curdma, dma->buf_bytes);
  308. return 0;
  309. }
  310. return bytes_to_frames(substream->runtime, curdma);
  311. }
  312. static int snd_cs5535audio_capture_open(struct snd_pcm_substream *substream)
  313. {
  314. int err;
  315. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  316. struct snd_pcm_runtime *runtime = substream->runtime;
  317. runtime->hw = snd_cs5535audio_capture;
  318. runtime->hw.rates = cs5535au->ac97->rates[AC97_RATES_ADC];
  319. snd_pcm_limit_hw_rates(runtime);
  320. cs5535au->capture_substream = substream;
  321. runtime->private_data = &(cs5535au->dmas[CS5535AUDIO_DMA_CAPTURE]);
  322. if ((err = snd_pcm_hw_constraint_integer(runtime,
  323. SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  324. return err;
  325. olpc_capture_open(cs5535au->ac97);
  326. return 0;
  327. }
  328. static int snd_cs5535audio_capture_close(struct snd_pcm_substream *substream)
  329. {
  330. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  331. olpc_capture_close(cs5535au->ac97);
  332. return 0;
  333. }
  334. static int snd_cs5535audio_capture_prepare(struct snd_pcm_substream *substream)
  335. {
  336. struct cs5535audio *cs5535au = snd_pcm_substream_chip(substream);
  337. return snd_ac97_set_rate(cs5535au->ac97, AC97_PCM_LR_ADC_RATE,
  338. substream->runtime->rate);
  339. }
  340. static struct snd_pcm_ops snd_cs5535audio_playback_ops = {
  341. .open = snd_cs5535audio_playback_open,
  342. .close = snd_cs5535audio_playback_close,
  343. .ioctl = snd_pcm_lib_ioctl,
  344. .hw_params = snd_cs5535audio_hw_params,
  345. .hw_free = snd_cs5535audio_hw_free,
  346. .prepare = snd_cs5535audio_playback_prepare,
  347. .trigger = snd_cs5535audio_trigger,
  348. .pointer = snd_cs5535audio_pcm_pointer,
  349. };
  350. static struct snd_pcm_ops snd_cs5535audio_capture_ops = {
  351. .open = snd_cs5535audio_capture_open,
  352. .close = snd_cs5535audio_capture_close,
  353. .ioctl = snd_pcm_lib_ioctl,
  354. .hw_params = snd_cs5535audio_hw_params,
  355. .hw_free = snd_cs5535audio_hw_free,
  356. .prepare = snd_cs5535audio_capture_prepare,
  357. .trigger = snd_cs5535audio_trigger,
  358. .pointer = snd_cs5535audio_pcm_pointer,
  359. };
  360. static struct cs5535audio_dma_ops snd_cs5535audio_playback_dma_ops = {
  361. .type = CS5535AUDIO_DMA_PLAYBACK,
  362. .enable_dma = cs5535audio_playback_enable_dma,
  363. .disable_dma = cs5535audio_playback_disable_dma,
  364. .setup_prd = cs5535audio_playback_setup_prd,
  365. .read_prd = cs5535audio_playback_read_prd,
  366. .pause_dma = cs5535audio_playback_pause_dma,
  367. .read_dma_pntr = cs5535audio_playback_read_dma_pntr,
  368. };
  369. static struct cs5535audio_dma_ops snd_cs5535audio_capture_dma_ops = {
  370. .type = CS5535AUDIO_DMA_CAPTURE,
  371. .enable_dma = cs5535audio_capture_enable_dma,
  372. .disable_dma = cs5535audio_capture_disable_dma,
  373. .setup_prd = cs5535audio_capture_setup_prd,
  374. .read_prd = cs5535audio_capture_read_prd,
  375. .pause_dma = cs5535audio_capture_pause_dma,
  376. .read_dma_pntr = cs5535audio_capture_read_dma_pntr,
  377. };
  378. int snd_cs5535audio_pcm(struct cs5535audio *cs5535au)
  379. {
  380. struct snd_pcm *pcm;
  381. int err;
  382. err = snd_pcm_new(cs5535au->card, "CS5535 Audio", 0, 1, 1, &pcm);
  383. if (err < 0)
  384. return err;
  385. cs5535au->dmas[CS5535AUDIO_DMA_PLAYBACK].ops =
  386. &snd_cs5535audio_playback_dma_ops;
  387. cs5535au->dmas[CS5535AUDIO_DMA_CAPTURE].ops =
  388. &snd_cs5535audio_capture_dma_ops;
  389. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  390. &snd_cs5535audio_playback_ops);
  391. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  392. &snd_cs5535audio_capture_ops);
  393. pcm->private_data = cs5535au;
  394. pcm->info_flags = 0;
  395. strcpy(pcm->name, "CS5535 Audio");
  396. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  397. snd_dma_pci_data(cs5535au->pci),
  398. 64*1024, 128*1024);
  399. cs5535au->pcm = pcm;
  400. return 0;
  401. }