solo6x10-g723.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 G723_MAX_BUFFER (G723_PERIOD_BYTES * PERIODS_MAX)
  45. #define G723_INTR_ORDER 4 /* 0 - 4 */
  46. #define PERIODS_MIN (1 << G723_INTR_ORDER)
  47. #define PERIODS_MAX G723_FDMA_PAGES
  48. struct solo_snd_pcm {
  49. int on;
  50. spinlock_t lock;
  51. struct solo_dev *solo_dev;
  52. unsigned char *g723_buf;
  53. dma_addr_t g723_dma;
  54. };
  55. static void solo_g723_config(struct solo_dev *solo_dev)
  56. {
  57. int clk_div;
  58. clk_div = (solo_dev->clock_mhz * 1000000)
  59. / (SAMPLERATE * (BITRATE * 2) * 2);
  60. solo_reg_write(solo_dev, SOLO_AUDIO_SAMPLE,
  61. SOLO_AUDIO_BITRATE(BITRATE)
  62. | SOLO_AUDIO_CLK_DIV(clk_div));
  63. solo_reg_write(solo_dev, SOLO_AUDIO_FDMA_INTR,
  64. SOLO_AUDIO_FDMA_INTERVAL(1)
  65. | SOLO_AUDIO_INTR_ORDER(G723_INTR_ORDER)
  66. | SOLO_AUDIO_FDMA_BASE(SOLO_G723_EXT_ADDR(solo_dev) >> 16));
  67. solo_reg_write(solo_dev, SOLO_AUDIO_CONTROL,
  68. SOLO_AUDIO_ENABLE
  69. | SOLO_AUDIO_I2S_MODE
  70. | SOLO_AUDIO_I2S_MULTI(3)
  71. | SOLO_AUDIO_MODE(OUTMODE_MASK));
  72. }
  73. void solo_g723_isr(struct solo_dev *solo_dev)
  74. {
  75. struct snd_pcm_str *pstr =
  76. &solo_dev->snd_pcm->streams[SNDRV_PCM_STREAM_CAPTURE];
  77. struct snd_pcm_substream *ss;
  78. struct solo_snd_pcm *solo_pcm;
  79. for (ss = pstr->substream; ss != NULL; ss = ss->next) {
  80. if (snd_pcm_substream_chip(ss) == NULL)
  81. continue;
  82. /* This means open() hasn't been called on this one */
  83. if (snd_pcm_substream_chip(ss) == solo_dev)
  84. continue;
  85. /* Haven't triggered a start yet */
  86. solo_pcm = snd_pcm_substream_chip(ss);
  87. if (!solo_pcm->on)
  88. continue;
  89. snd_pcm_period_elapsed(ss);
  90. }
  91. }
  92. static int snd_solo_hw_params(struct snd_pcm_substream *ss,
  93. struct snd_pcm_hw_params *hw_params)
  94. {
  95. return snd_pcm_lib_malloc_pages(ss, params_buffer_bytes(hw_params));
  96. }
  97. static int snd_solo_hw_free(struct snd_pcm_substream *ss)
  98. {
  99. return snd_pcm_lib_free_pages(ss);
  100. }
  101. static const struct snd_pcm_hardware snd_solo_pcm_hw = {
  102. .info = (SNDRV_PCM_INFO_MMAP |
  103. SNDRV_PCM_INFO_INTERLEAVED |
  104. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  105. SNDRV_PCM_INFO_MMAP_VALID),
  106. .formats = SNDRV_PCM_FMTBIT_U8,
  107. .rates = SNDRV_PCM_RATE_8000,
  108. .rate_min = SAMPLERATE,
  109. .rate_max = SAMPLERATE,
  110. .channels_min = 1,
  111. .channels_max = 1,
  112. .buffer_bytes_max = G723_MAX_BUFFER,
  113. .period_bytes_min = G723_PERIOD_BYTES,
  114. .period_bytes_max = G723_PERIOD_BYTES,
  115. .periods_min = PERIODS_MIN,
  116. .periods_max = PERIODS_MAX,
  117. };
  118. static int snd_solo_pcm_open(struct snd_pcm_substream *ss)
  119. {
  120. struct solo_dev *solo_dev = snd_pcm_substream_chip(ss);
  121. struct solo_snd_pcm *solo_pcm;
  122. solo_pcm = kzalloc(sizeof(*solo_pcm), GFP_KERNEL);
  123. if (solo_pcm == NULL)
  124. goto oom;
  125. solo_pcm->g723_buf = pci_alloc_consistent(solo_dev->pdev,
  126. G723_PERIOD_BYTES,
  127. &solo_pcm->g723_dma);
  128. if (solo_pcm->g723_buf == NULL)
  129. goto oom;
  130. spin_lock_init(&solo_pcm->lock);
  131. solo_pcm->solo_dev = solo_dev;
  132. ss->runtime->hw = snd_solo_pcm_hw;
  133. snd_pcm_substream_chip(ss) = solo_pcm;
  134. return 0;
  135. oom:
  136. kfree(solo_pcm);
  137. return -ENOMEM;
  138. }
  139. static int snd_solo_pcm_close(struct snd_pcm_substream *ss)
  140. {
  141. struct solo_snd_pcm *solo_pcm = snd_pcm_substream_chip(ss);
  142. snd_pcm_substream_chip(ss) = solo_pcm->solo_dev;
  143. pci_free_consistent(solo_pcm->solo_dev->pdev, G723_PERIOD_BYTES,
  144. solo_pcm->g723_buf, solo_pcm->g723_dma);
  145. kfree(solo_pcm);
  146. return 0;
  147. }
  148. static int snd_solo_pcm_trigger(struct snd_pcm_substream *ss, int cmd)
  149. {
  150. struct solo_snd_pcm *solo_pcm = snd_pcm_substream_chip(ss);
  151. struct solo_dev *solo_dev = solo_pcm->solo_dev;
  152. int ret = 0;
  153. spin_lock(&solo_pcm->lock);
  154. switch (cmd) {
  155. case SNDRV_PCM_TRIGGER_START:
  156. if (solo_pcm->on == 0) {
  157. /* If this is the first user, switch on interrupts */
  158. if (atomic_inc_return(&solo_dev->snd_users) == 1)
  159. solo_irq_on(solo_dev, SOLO_IRQ_G723);
  160. solo_pcm->on = 1;
  161. }
  162. break;
  163. case SNDRV_PCM_TRIGGER_STOP:
  164. if (solo_pcm->on) {
  165. /* If this was our last user, switch them off */
  166. if (atomic_dec_return(&solo_dev->snd_users) == 0)
  167. solo_irq_off(solo_dev, SOLO_IRQ_G723);
  168. solo_pcm->on = 0;
  169. }
  170. break;
  171. default:
  172. ret = -EINVAL;
  173. }
  174. spin_unlock(&solo_pcm->lock);
  175. return ret;
  176. }
  177. static int snd_solo_pcm_prepare(struct snd_pcm_substream *ss)
  178. {
  179. return 0;
  180. }
  181. static snd_pcm_uframes_t snd_solo_pcm_pointer(struct snd_pcm_substream *ss)
  182. {
  183. struct solo_snd_pcm *solo_pcm = snd_pcm_substream_chip(ss);
  184. struct solo_dev *solo_dev = solo_pcm->solo_dev;
  185. snd_pcm_uframes_t idx = solo_reg_read(solo_dev, SOLO_AUDIO_STA) & 0x1f;
  186. return idx * G723_FRAMES_PER_PAGE;
  187. }
  188. static int snd_solo_pcm_copy(struct snd_pcm_substream *ss, int channel,
  189. snd_pcm_uframes_t pos, void __user *dst,
  190. snd_pcm_uframes_t count)
  191. {
  192. struct solo_snd_pcm *solo_pcm = snd_pcm_substream_chip(ss);
  193. struct solo_dev *solo_dev = solo_pcm->solo_dev;
  194. int err, i;
  195. for (i = 0; i < (count / G723_FRAMES_PER_PAGE); i++) {
  196. int page = (pos / G723_FRAMES_PER_PAGE) + i;
  197. err = solo_p2m_dma_t(solo_dev, 0, solo_pcm->g723_dma,
  198. SOLO_G723_EXT_ADDR(solo_dev) +
  199. (page * G723_PERIOD_BLOCK) +
  200. (ss->number * G723_PERIOD_BYTES),
  201. G723_PERIOD_BYTES, 0, 0);
  202. if (err)
  203. return err;
  204. err = copy_to_user(dst + (i * G723_PERIOD_BYTES),
  205. solo_pcm->g723_buf, G723_PERIOD_BYTES);
  206. if (err)
  207. return -EFAULT;
  208. }
  209. return 0;
  210. }
  211. static struct snd_pcm_ops snd_solo_pcm_ops = {
  212. .open = snd_solo_pcm_open,
  213. .close = snd_solo_pcm_close,
  214. .ioctl = snd_pcm_lib_ioctl,
  215. .hw_params = snd_solo_hw_params,
  216. .hw_free = snd_solo_hw_free,
  217. .prepare = snd_solo_pcm_prepare,
  218. .trigger = snd_solo_pcm_trigger,
  219. .pointer = snd_solo_pcm_pointer,
  220. .copy = snd_solo_pcm_copy,
  221. };
  222. static int snd_solo_capture_volume_info(struct snd_kcontrol *kcontrol,
  223. struct snd_ctl_elem_info *info)
  224. {
  225. info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  226. info->count = 1;
  227. info->value.integer.min = 0;
  228. info->value.integer.max = 15;
  229. info->value.integer.step = 1;
  230. return 0;
  231. }
  232. static int snd_solo_capture_volume_get(struct snd_kcontrol *kcontrol,
  233. struct snd_ctl_elem_value *value)
  234. {
  235. struct solo_dev *solo_dev = snd_kcontrol_chip(kcontrol);
  236. u8 ch = value->id.numid - 1;
  237. value->value.integer.value[0] = tw28_get_audio_gain(solo_dev, ch);
  238. return 0;
  239. }
  240. static int snd_solo_capture_volume_put(struct snd_kcontrol *kcontrol,
  241. struct snd_ctl_elem_value *value)
  242. {
  243. struct solo_dev *solo_dev = snd_kcontrol_chip(kcontrol);
  244. u8 ch = value->id.numid - 1;
  245. u8 old_val;
  246. old_val = tw28_get_audio_gain(solo_dev, ch);
  247. if (old_val == value->value.integer.value[0])
  248. return 0;
  249. tw28_set_audio_gain(solo_dev, ch, value->value.integer.value[0]);
  250. return 1;
  251. }
  252. static struct snd_kcontrol_new snd_solo_capture_volume = {
  253. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  254. .name = "Capture Volume",
  255. .info = snd_solo_capture_volume_info,
  256. .get = snd_solo_capture_volume_get,
  257. .put = snd_solo_capture_volume_put,
  258. };
  259. static int solo_snd_pcm_init(struct solo_dev *solo_dev)
  260. {
  261. struct snd_card *card = solo_dev->snd_card;
  262. struct snd_pcm *pcm;
  263. struct snd_pcm_substream *ss;
  264. int ret;
  265. int i;
  266. ret = snd_pcm_new(card, card->driver, 0, 0, solo_dev->nr_chans,
  267. &pcm);
  268. if (ret < 0)
  269. return ret;
  270. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  271. &snd_solo_pcm_ops);
  272. snd_pcm_chip(pcm) = solo_dev;
  273. pcm->info_flags = 0;
  274. strcpy(pcm->name, card->shortname);
  275. for (i = 0, ss = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  276. ss; ss = ss->next, i++)
  277. sprintf(ss->name, "Camera #%d Audio", i);
  278. ret = snd_pcm_lib_preallocate_pages_for_all(pcm,
  279. SNDRV_DMA_TYPE_CONTINUOUS,
  280. snd_dma_continuous_data(GFP_KERNEL),
  281. G723_MAX_BUFFER, G723_MAX_BUFFER);
  282. if (ret < 0)
  283. return ret;
  284. solo_dev->snd_pcm = pcm;
  285. return 0;
  286. }
  287. int solo_g723_init(struct solo_dev *solo_dev)
  288. {
  289. static struct snd_device_ops ops = { NULL };
  290. struct snd_card *card;
  291. struct snd_kcontrol_new kctl;
  292. char name[32];
  293. int ret;
  294. atomic_set(&solo_dev->snd_users, 0);
  295. /* Allows for easier mapping between video and audio */
  296. sprintf(name, "Softlogic%d", solo_dev->vfd->num);
  297. ret = snd_card_new(&solo_dev->pdev->dev,
  298. SNDRV_DEFAULT_IDX1, name, THIS_MODULE, 0,
  299. &solo_dev->snd_card);
  300. if (ret < 0)
  301. return ret;
  302. card = solo_dev->snd_card;
  303. strcpy(card->driver, SOLO6X10_NAME);
  304. strcpy(card->shortname, "SOLO-6x10 Audio");
  305. sprintf(card->longname, "%s on %s IRQ %d", card->shortname,
  306. pci_name(solo_dev->pdev), solo_dev->pdev->irq);
  307. ret = snd_device_new(card, SNDRV_DEV_LOWLEVEL, solo_dev, &ops);
  308. if (ret < 0)
  309. goto snd_error;
  310. /* Mixer controls */
  311. strcpy(card->mixername, "SOLO-6x10");
  312. kctl = snd_solo_capture_volume;
  313. kctl.count = solo_dev->nr_chans;
  314. ret = snd_ctl_add(card, snd_ctl_new1(&kctl, solo_dev));
  315. if (ret < 0)
  316. return ret;
  317. ret = solo_snd_pcm_init(solo_dev);
  318. if (ret < 0)
  319. goto snd_error;
  320. ret = snd_card_register(card);
  321. if (ret < 0)
  322. goto snd_error;
  323. solo_g723_config(solo_dev);
  324. dev_info(&solo_dev->pdev->dev, "Alsa sound card as %s\n", name);
  325. return 0;
  326. snd_error:
  327. snd_card_free(card);
  328. return ret;
  329. }
  330. void solo_g723_exit(struct solo_dev *solo_dev)
  331. {
  332. if (!solo_dev->snd_card)
  333. return;
  334. solo_reg_write(solo_dev, SOLO_AUDIO_CONTROL, 0);
  335. solo_irq_off(solo_dev, SOLO_IRQ_G723);
  336. snd_card_free(solo_dev->snd_card);
  337. solo_dev->snd_card = NULL;
  338. }