solo6x10-g723.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * Copyright (C) 2010-2013 Bluecherry, LLC <http://www.bluecherrydvr.com>
  3. *
  4. * Original author:
  5. * Ben Collins <bcollins@ubuntu.com>
  6. *
  7. * Additional work by:
  8. * John Brooks <john.brooks@bluecherry.net>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/mempool.h>
  22. #include <linux/poll.h>
  23. #include <linux/kthread.h>
  24. #include <linux/freezer.h>
  25. #include <linux/module.h>
  26. #include <linux/slab.h>
  27. #include <sound/core.h>
  28. #include <sound/initval.h>
  29. #include <sound/pcm.h>
  30. #include <sound/control.h>
  31. #include "solo6x10.h"
  32. #include "solo6x10-tw28.h"
  33. #define G723_FDMA_PAGES 32
  34. #define G723_PERIOD_BYTES 48
  35. #define G723_PERIOD_BLOCK 1024
  36. #define G723_FRAMES_PER_PAGE 48
  37. /* Sets up channels 16-19 for decoding and 0-15 for encoding */
  38. #define OUTMODE_MASK 0x300
  39. #define SAMPLERATE 8000
  40. #define BITRATE 25
  41. /* The solo writes to 1k byte pages, 32 pages, in the dma. Each 1k page
  42. * is broken down to 20 * 48 byte regions (one for each channel possible)
  43. * with the rest of the page being dummy data. */
  44. #define PERIODS G723_FDMA_PAGES
  45. #define G723_INTR_ORDER 4 /* 0 - 4 */
  46. struct solo_snd_pcm {
  47. int on;
  48. spinlock_t lock;
  49. struct solo_dev *solo_dev;
  50. u8 *g723_buf;
  51. dma_addr_t g723_dma;
  52. };
  53. static void solo_g723_config(struct solo_dev *solo_dev)
  54. {
  55. int clk_div;
  56. clk_div = (solo_dev->clock_mhz * 1000000)
  57. / (SAMPLERATE * (BITRATE * 2) * 2);
  58. solo_reg_write(solo_dev, SOLO_AUDIO_SAMPLE,
  59. SOLO_AUDIO_BITRATE(BITRATE)
  60. | SOLO_AUDIO_CLK_DIV(clk_div));
  61. solo_reg_write(solo_dev, SOLO_AUDIO_FDMA_INTR,
  62. SOLO_AUDIO_FDMA_INTERVAL(1)
  63. | SOLO_AUDIO_INTR_ORDER(G723_INTR_ORDER)
  64. | SOLO_AUDIO_FDMA_BASE(SOLO_G723_EXT_ADDR(solo_dev) >> 16));
  65. solo_reg_write(solo_dev, SOLO_AUDIO_CONTROL,
  66. SOLO_AUDIO_ENABLE
  67. | SOLO_AUDIO_I2S_MODE
  68. | SOLO_AUDIO_I2S_MULTI(3)
  69. | SOLO_AUDIO_MODE(OUTMODE_MASK));
  70. }
  71. void solo_g723_isr(struct solo_dev *solo_dev)
  72. {
  73. struct snd_pcm_str *pstr =
  74. &solo_dev->snd_pcm->streams[SNDRV_PCM_STREAM_CAPTURE];
  75. struct snd_pcm_substream *ss;
  76. struct solo_snd_pcm *solo_pcm;
  77. for (ss = pstr->substream; ss != NULL; ss = ss->next) {
  78. if (snd_pcm_substream_chip(ss) == NULL)
  79. continue;
  80. /* This means open() hasn't been called on this one */
  81. if (snd_pcm_substream_chip(ss) == solo_dev)
  82. continue;
  83. /* Haven't triggered a start yet */
  84. solo_pcm = snd_pcm_substream_chip(ss);
  85. if (!solo_pcm->on)
  86. continue;
  87. snd_pcm_period_elapsed(ss);
  88. }
  89. }
  90. static int snd_solo_hw_params(struct snd_pcm_substream *ss,
  91. struct snd_pcm_hw_params *hw_params)
  92. {
  93. return snd_pcm_lib_malloc_pages(ss, params_buffer_bytes(hw_params));
  94. }
  95. static int snd_solo_hw_free(struct snd_pcm_substream *ss)
  96. {
  97. return snd_pcm_lib_free_pages(ss);
  98. }
  99. static const struct snd_pcm_hardware snd_solo_pcm_hw = {
  100. .info = (SNDRV_PCM_INFO_MMAP |
  101. SNDRV_PCM_INFO_INTERLEAVED |
  102. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  103. SNDRV_PCM_INFO_MMAP_VALID),
  104. .formats = SNDRV_PCM_FMTBIT_U8,
  105. .rates = SNDRV_PCM_RATE_8000,
  106. .rate_min = SAMPLERATE,
  107. .rate_max = SAMPLERATE,
  108. .channels_min = 1,
  109. .channels_max = 1,
  110. .buffer_bytes_max = G723_PERIOD_BYTES * PERIODS,
  111. .period_bytes_min = G723_PERIOD_BYTES,
  112. .period_bytes_max = G723_PERIOD_BYTES,
  113. .periods_min = PERIODS,
  114. .periods_max = PERIODS,
  115. };
  116. static int snd_solo_pcm_open(struct snd_pcm_substream *ss)
  117. {
  118. struct solo_dev *solo_dev = snd_pcm_substream_chip(ss);
  119. struct solo_snd_pcm *solo_pcm;
  120. solo_pcm = kzalloc(sizeof(*solo_pcm), GFP_KERNEL);
  121. if (solo_pcm == NULL)
  122. goto oom;
  123. solo_pcm->g723_buf = pci_alloc_consistent(solo_dev->pdev,
  124. G723_PERIOD_BYTES,
  125. &solo_pcm->g723_dma);
  126. if (solo_pcm->g723_buf == NULL)
  127. goto oom;
  128. spin_lock_init(&solo_pcm->lock);
  129. solo_pcm->solo_dev = solo_dev;
  130. ss->runtime->hw = snd_solo_pcm_hw;
  131. snd_pcm_substream_chip(ss) = solo_pcm;
  132. return 0;
  133. oom:
  134. kfree(solo_pcm);
  135. return -ENOMEM;
  136. }
  137. static int snd_solo_pcm_close(struct snd_pcm_substream *ss)
  138. {
  139. struct solo_snd_pcm *solo_pcm = snd_pcm_substream_chip(ss);
  140. snd_pcm_substream_chip(ss) = solo_pcm->solo_dev;
  141. pci_free_consistent(solo_pcm->solo_dev->pdev, G723_PERIOD_BYTES,
  142. solo_pcm->g723_buf, solo_pcm->g723_dma);
  143. kfree(solo_pcm);
  144. return 0;
  145. }
  146. static int snd_solo_pcm_trigger(struct snd_pcm_substream *ss, int cmd)
  147. {
  148. struct solo_snd_pcm *solo_pcm = snd_pcm_substream_chip(ss);
  149. struct solo_dev *solo_dev = solo_pcm->solo_dev;
  150. int ret = 0;
  151. spin_lock(&solo_pcm->lock);
  152. switch (cmd) {
  153. case SNDRV_PCM_TRIGGER_START:
  154. if (solo_pcm->on == 0) {
  155. /* If this is the first user, switch on interrupts */
  156. if (atomic_inc_return(&solo_dev->snd_users) == 1)
  157. solo_irq_on(solo_dev, SOLO_IRQ_G723);
  158. solo_pcm->on = 1;
  159. }
  160. break;
  161. case SNDRV_PCM_TRIGGER_STOP:
  162. if (solo_pcm->on) {
  163. /* If this was our last user, switch them off */
  164. if (atomic_dec_return(&solo_dev->snd_users) == 0)
  165. solo_irq_off(solo_dev, SOLO_IRQ_G723);
  166. solo_pcm->on = 0;
  167. }
  168. break;
  169. default:
  170. ret = -EINVAL;
  171. }
  172. spin_unlock(&solo_pcm->lock);
  173. return ret;
  174. }
  175. static int snd_solo_pcm_prepare(struct snd_pcm_substream *ss)
  176. {
  177. return 0;
  178. }
  179. static snd_pcm_uframes_t snd_solo_pcm_pointer(struct snd_pcm_substream *ss)
  180. {
  181. struct solo_snd_pcm *solo_pcm = snd_pcm_substream_chip(ss);
  182. struct solo_dev *solo_dev = solo_pcm->solo_dev;
  183. snd_pcm_uframes_t idx = solo_reg_read(solo_dev, SOLO_AUDIO_STA) & 0x1f;
  184. return idx * G723_FRAMES_PER_PAGE;
  185. }
  186. static int snd_solo_pcm_copy_user(struct snd_pcm_substream *ss, int channel,
  187. unsigned long pos, void __user *dst,
  188. unsigned long count)
  189. {
  190. struct solo_snd_pcm *solo_pcm = snd_pcm_substream_chip(ss);
  191. struct solo_dev *solo_dev = solo_pcm->solo_dev;
  192. int err, i;
  193. for (i = 0; i < (count / G723_FRAMES_PER_PAGE); i++) {
  194. int page = (pos / G723_FRAMES_PER_PAGE) + i;
  195. err = solo_p2m_dma_t(solo_dev, 0, solo_pcm->g723_dma,
  196. SOLO_G723_EXT_ADDR(solo_dev) +
  197. (page * G723_PERIOD_BLOCK) +
  198. (ss->number * G723_PERIOD_BYTES),
  199. G723_PERIOD_BYTES, 0, 0);
  200. if (err)
  201. return err;
  202. if (copy_to_user(dst, solo_pcm->g723_buf, G723_PERIOD_BYTES))
  203. return -EFAULT;
  204. dst += G723_PERIOD_BYTES;
  205. }
  206. return 0;
  207. }
  208. static int snd_solo_pcm_copy_kernel(struct snd_pcm_substream *ss, int channel,
  209. unsigned long pos, void *dst,
  210. unsigned long count)
  211. {
  212. struct solo_snd_pcm *solo_pcm = snd_pcm_substream_chip(ss);
  213. struct solo_dev *solo_dev = solo_pcm->solo_dev;
  214. int err, i;
  215. for (i = 0; i < (count / G723_FRAMES_PER_PAGE); i++) {
  216. int page = (pos / G723_FRAMES_PER_PAGE) + i;
  217. err = solo_p2m_dma_t(solo_dev, 0, solo_pcm->g723_dma,
  218. SOLO_G723_EXT_ADDR(solo_dev) +
  219. (page * G723_PERIOD_BLOCK) +
  220. (ss->number * G723_PERIOD_BYTES),
  221. G723_PERIOD_BYTES, 0, 0);
  222. if (err)
  223. return err;
  224. memcpy(dst, solo_pcm->g723_buf, G723_PERIOD_BYTES);
  225. dst += G723_PERIOD_BYTES;
  226. }
  227. return 0;
  228. }
  229. static const struct snd_pcm_ops snd_solo_pcm_ops = {
  230. .open = snd_solo_pcm_open,
  231. .close = snd_solo_pcm_close,
  232. .ioctl = snd_pcm_lib_ioctl,
  233. .hw_params = snd_solo_hw_params,
  234. .hw_free = snd_solo_hw_free,
  235. .prepare = snd_solo_pcm_prepare,
  236. .trigger = snd_solo_pcm_trigger,
  237. .pointer = snd_solo_pcm_pointer,
  238. .copy_user = snd_solo_pcm_copy_user,
  239. .copy_kernel = snd_solo_pcm_copy_kernel,
  240. };
  241. static int snd_solo_capture_volume_info(struct snd_kcontrol *kcontrol,
  242. struct snd_ctl_elem_info *info)
  243. {
  244. info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  245. info->count = 1;
  246. info->value.integer.min = 0;
  247. info->value.integer.max = 15;
  248. info->value.integer.step = 1;
  249. return 0;
  250. }
  251. static int snd_solo_capture_volume_get(struct snd_kcontrol *kcontrol,
  252. struct snd_ctl_elem_value *value)
  253. {
  254. struct solo_dev *solo_dev = snd_kcontrol_chip(kcontrol);
  255. u8 ch = value->id.numid - 1;
  256. value->value.integer.value[0] = tw28_get_audio_gain(solo_dev, ch);
  257. return 0;
  258. }
  259. static int snd_solo_capture_volume_put(struct snd_kcontrol *kcontrol,
  260. struct snd_ctl_elem_value *value)
  261. {
  262. struct solo_dev *solo_dev = snd_kcontrol_chip(kcontrol);
  263. u8 ch = value->id.numid - 1;
  264. u8 old_val;
  265. old_val = tw28_get_audio_gain(solo_dev, ch);
  266. if (old_val == value->value.integer.value[0])
  267. return 0;
  268. tw28_set_audio_gain(solo_dev, ch, value->value.integer.value[0]);
  269. return 1;
  270. }
  271. static const struct snd_kcontrol_new snd_solo_capture_volume = {
  272. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  273. .name = "Capture Volume",
  274. .info = snd_solo_capture_volume_info,
  275. .get = snd_solo_capture_volume_get,
  276. .put = snd_solo_capture_volume_put,
  277. };
  278. static int solo_snd_pcm_init(struct solo_dev *solo_dev)
  279. {
  280. struct snd_card *card = solo_dev->snd_card;
  281. struct snd_pcm *pcm;
  282. struct snd_pcm_substream *ss;
  283. int ret;
  284. int i;
  285. ret = snd_pcm_new(card, card->driver, 0, 0, solo_dev->nr_chans,
  286. &pcm);
  287. if (ret < 0)
  288. return ret;
  289. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  290. &snd_solo_pcm_ops);
  291. snd_pcm_chip(pcm) = solo_dev;
  292. pcm->info_flags = 0;
  293. strcpy(pcm->name, card->shortname);
  294. for (i = 0, ss = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  295. ss; ss = ss->next, i++)
  296. sprintf(ss->name, "Camera #%d Audio", i);
  297. ret = snd_pcm_lib_preallocate_pages_for_all(pcm,
  298. SNDRV_DMA_TYPE_CONTINUOUS,
  299. snd_dma_continuous_data(GFP_KERNEL),
  300. G723_PERIOD_BYTES * PERIODS,
  301. G723_PERIOD_BYTES * PERIODS);
  302. if (ret < 0)
  303. return ret;
  304. solo_dev->snd_pcm = pcm;
  305. return 0;
  306. }
  307. int solo_g723_init(struct solo_dev *solo_dev)
  308. {
  309. static struct snd_device_ops ops = { };
  310. struct snd_card *card;
  311. struct snd_kcontrol_new kctl;
  312. char name[32];
  313. int ret;
  314. atomic_set(&solo_dev->snd_users, 0);
  315. /* Allows for easier mapping between video and audio */
  316. sprintf(name, "Softlogic%d", solo_dev->vfd->num);
  317. ret = snd_card_new(&solo_dev->pdev->dev,
  318. SNDRV_DEFAULT_IDX1, name, THIS_MODULE, 0,
  319. &solo_dev->snd_card);
  320. if (ret < 0)
  321. return ret;
  322. card = solo_dev->snd_card;
  323. strcpy(card->driver, SOLO6X10_NAME);
  324. strcpy(card->shortname, "SOLO-6x10 Audio");
  325. sprintf(card->longname, "%s on %s IRQ %d", card->shortname,
  326. pci_name(solo_dev->pdev), solo_dev->pdev->irq);
  327. ret = snd_device_new(card, SNDRV_DEV_LOWLEVEL, solo_dev, &ops);
  328. if (ret < 0)
  329. goto snd_error;
  330. /* Mixer controls */
  331. strcpy(card->mixername, "SOLO-6x10");
  332. kctl = snd_solo_capture_volume;
  333. kctl.count = solo_dev->nr_chans;
  334. ret = snd_ctl_add(card, snd_ctl_new1(&kctl, solo_dev));
  335. if (ret < 0)
  336. return ret;
  337. ret = solo_snd_pcm_init(solo_dev);
  338. if (ret < 0)
  339. goto snd_error;
  340. ret = snd_card_register(card);
  341. if (ret < 0)
  342. goto snd_error;
  343. solo_g723_config(solo_dev);
  344. dev_info(&solo_dev->pdev->dev, "Alsa sound card as %s\n", name);
  345. return 0;
  346. snd_error:
  347. snd_card_free(card);
  348. return ret;
  349. }
  350. void solo_g723_exit(struct solo_dev *solo_dev)
  351. {
  352. if (!solo_dev->snd_card)
  353. return;
  354. solo_reg_write(solo_dev, SOLO_AUDIO_CONTROL, 0);
  355. solo_irq_off(solo_dev, SOLO_IRQ_G723);
  356. snd_card_free(solo_dev->snd_card);
  357. solo_dev->snd_card = NULL;
  358. }