solo6x10-g723.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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(struct snd_pcm_substream *ss, int channel,
  187. snd_pcm_uframes_t pos, void __user *dst,
  188. snd_pcm_uframes_t 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. err = copy_to_user(dst + (i * G723_PERIOD_BYTES),
  203. solo_pcm->g723_buf, G723_PERIOD_BYTES);
  204. if (err)
  205. return -EFAULT;
  206. }
  207. return 0;
  208. }
  209. static struct snd_pcm_ops snd_solo_pcm_ops = {
  210. .open = snd_solo_pcm_open,
  211. .close = snd_solo_pcm_close,
  212. .ioctl = snd_pcm_lib_ioctl,
  213. .hw_params = snd_solo_hw_params,
  214. .hw_free = snd_solo_hw_free,
  215. .prepare = snd_solo_pcm_prepare,
  216. .trigger = snd_solo_pcm_trigger,
  217. .pointer = snd_solo_pcm_pointer,
  218. .copy = snd_solo_pcm_copy,
  219. };
  220. static int snd_solo_capture_volume_info(struct snd_kcontrol *kcontrol,
  221. struct snd_ctl_elem_info *info)
  222. {
  223. info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  224. info->count = 1;
  225. info->value.integer.min = 0;
  226. info->value.integer.max = 15;
  227. info->value.integer.step = 1;
  228. return 0;
  229. }
  230. static int snd_solo_capture_volume_get(struct snd_kcontrol *kcontrol,
  231. struct snd_ctl_elem_value *value)
  232. {
  233. struct solo_dev *solo_dev = snd_kcontrol_chip(kcontrol);
  234. u8 ch = value->id.numid - 1;
  235. value->value.integer.value[0] = tw28_get_audio_gain(solo_dev, ch);
  236. return 0;
  237. }
  238. static int snd_solo_capture_volume_put(struct snd_kcontrol *kcontrol,
  239. struct snd_ctl_elem_value *value)
  240. {
  241. struct solo_dev *solo_dev = snd_kcontrol_chip(kcontrol);
  242. u8 ch = value->id.numid - 1;
  243. u8 old_val;
  244. old_val = tw28_get_audio_gain(solo_dev, ch);
  245. if (old_val == value->value.integer.value[0])
  246. return 0;
  247. tw28_set_audio_gain(solo_dev, ch, value->value.integer.value[0]);
  248. return 1;
  249. }
  250. static struct snd_kcontrol_new snd_solo_capture_volume = {
  251. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  252. .name = "Capture Volume",
  253. .info = snd_solo_capture_volume_info,
  254. .get = snd_solo_capture_volume_get,
  255. .put = snd_solo_capture_volume_put,
  256. };
  257. static int solo_snd_pcm_init(struct solo_dev *solo_dev)
  258. {
  259. struct snd_card *card = solo_dev->snd_card;
  260. struct snd_pcm *pcm;
  261. struct snd_pcm_substream *ss;
  262. int ret;
  263. int i;
  264. ret = snd_pcm_new(card, card->driver, 0, 0, solo_dev->nr_chans,
  265. &pcm);
  266. if (ret < 0)
  267. return ret;
  268. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  269. &snd_solo_pcm_ops);
  270. snd_pcm_chip(pcm) = solo_dev;
  271. pcm->info_flags = 0;
  272. strcpy(pcm->name, card->shortname);
  273. for (i = 0, ss = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  274. ss; ss = ss->next, i++)
  275. sprintf(ss->name, "Camera #%d Audio", i);
  276. ret = snd_pcm_lib_preallocate_pages_for_all(pcm,
  277. SNDRV_DMA_TYPE_CONTINUOUS,
  278. snd_dma_continuous_data(GFP_KERNEL),
  279. G723_PERIOD_BYTES * PERIODS,
  280. G723_PERIOD_BYTES * PERIODS);
  281. if (ret < 0)
  282. return ret;
  283. solo_dev->snd_pcm = pcm;
  284. return 0;
  285. }
  286. int solo_g723_init(struct solo_dev *solo_dev)
  287. {
  288. static struct snd_device_ops ops = { NULL };
  289. struct snd_card *card;
  290. struct snd_kcontrol_new kctl;
  291. char name[32];
  292. int ret;
  293. atomic_set(&solo_dev->snd_users, 0);
  294. /* Allows for easier mapping between video and audio */
  295. sprintf(name, "Softlogic%d", solo_dev->vfd->num);
  296. ret = snd_card_new(&solo_dev->pdev->dev,
  297. SNDRV_DEFAULT_IDX1, name, THIS_MODULE, 0,
  298. &solo_dev->snd_card);
  299. if (ret < 0)
  300. return ret;
  301. card = solo_dev->snd_card;
  302. strcpy(card->driver, SOLO6X10_NAME);
  303. strcpy(card->shortname, "SOLO-6x10 Audio");
  304. sprintf(card->longname, "%s on %s IRQ %d", card->shortname,
  305. pci_name(solo_dev->pdev), solo_dev->pdev->irq);
  306. ret = snd_device_new(card, SNDRV_DEV_LOWLEVEL, solo_dev, &ops);
  307. if (ret < 0)
  308. goto snd_error;
  309. /* Mixer controls */
  310. strcpy(card->mixername, "SOLO-6x10");
  311. kctl = snd_solo_capture_volume;
  312. kctl.count = solo_dev->nr_chans;
  313. ret = snd_ctl_add(card, snd_ctl_new1(&kctl, solo_dev));
  314. if (ret < 0)
  315. return ret;
  316. ret = solo_snd_pcm_init(solo_dev);
  317. if (ret < 0)
  318. goto snd_error;
  319. ret = snd_card_register(card);
  320. if (ret < 0)
  321. goto snd_error;
  322. solo_g723_config(solo_dev);
  323. dev_info(&solo_dev->pdev->dev, "Alsa sound card as %s\n", name);
  324. return 0;
  325. snd_error:
  326. snd_card_free(card);
  327. return ret;
  328. }
  329. void solo_g723_exit(struct solo_dev *solo_dev)
  330. {
  331. if (!solo_dev->snd_card)
  332. return;
  333. solo_reg_write(solo_dev, SOLO_AUDIO_CONTROL, 0);
  334. solo_irq_off(solo_dev, SOLO_IRQ_G723);
  335. snd_card_free(solo_dev->snd_card);
  336. solo_dev->snd_card = NULL;
  337. }