g723.c 10 KB

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