cs5535audio_pcm.c 12 KB

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