ngene-snd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * ngene_snd.c: nGene PCIe bridge driver ALSA support
  3. *
  4. * Copyright (C) 2005-2007 Micronas
  5. *
  6. * Based on the initial ALSA support port by Thomas Eschbach.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 only, as published by the Free Software Foundation.
  11. *
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  22. * 02110-1301, USA
  23. * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
  24. */
  25. #include <linux/version.h>
  26. #include <linux/module.h>
  27. #include "ngene.h"
  28. #include "ngene-ioctls.h"
  29. static int sound_dev;
  30. /* sound module parameters (see "Module Parameters") */
  31. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  32. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  33. static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 1};
  34. /****************************************************************************/
  35. /* PCM Sound Funktions ******************************************************/
  36. /****************************************************************************/
  37. static struct snd_pcm_hardware snd_mychip_capture_hw = {
  38. .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER),
  39. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  40. .rates = (SNDRV_PCM_RATE_11025 | SNDRV_PCM_RATE_16000
  41. | SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000
  42. | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000),
  43. .rate_min = 11025,
  44. .rate_max = 48000,
  45. .channels_min = 2,
  46. .channels_max = 2,
  47. .buffer_bytes_max = 16384,
  48. .period_bytes_min = 8192,
  49. .period_bytes_max = 8192,
  50. .periods_min = 1,
  51. .periods_max = 2,
  52. };
  53. /* open callback */
  54. static int snd_mychip_capture_open(struct snd_pcm_substream *substream)
  55. {
  56. struct mychip *chip = snd_pcm_substream_chip(substream);
  57. struct snd_pcm_runtime *runtime = substream->runtime;
  58. runtime->hw = snd_mychip_capture_hw;
  59. chip->substream = substream;
  60. return 0;
  61. }
  62. /* close callback */
  63. static int snd_mychip_capture_close(struct snd_pcm_substream *substream)
  64. {
  65. struct mychip *chip = snd_pcm_substream_chip(substream);
  66. chip->substream = NULL;
  67. return 0;
  68. }
  69. /* hw_params callback */
  70. static int snd_mychip_pcm_hw_params(struct snd_pcm_substream *substream,
  71. struct snd_pcm_hw_params *hw_params)
  72. {
  73. struct mychip *chip = snd_pcm_substream_chip(substream);
  74. struct ngene_channel *chan = chip->chan;
  75. if (chan->soundbuffisallocated == 0) {
  76. chan->soundbuffisallocated = 1;
  77. return snd_pcm_lib_malloc_pages(substream,
  78. params_buffer_bytes(hw_params));
  79. }
  80. return 0;
  81. }
  82. /* hw_free callback */
  83. static int snd_mychip_pcm_hw_free(struct snd_pcm_substream *substream)
  84. {
  85. struct mychip *chip = snd_pcm_substream_chip(substream);
  86. struct ngene_channel *chan = chip->chan;
  87. int retval = 0;
  88. if (chan->soundbuffisallocated == 1) {
  89. chan->soundbuffisallocated = 0;
  90. retval = snd_pcm_lib_free_pages(substream);
  91. }
  92. return retval;
  93. }
  94. /* prepare callback */
  95. static int snd_mychip_pcm_prepare(struct snd_pcm_substream *substream)
  96. {
  97. struct mychip *chip = snd_pcm_substream_chip(substream);
  98. struct snd_pcm_runtime *runtime = substream->runtime;
  99. struct ngene_channel *chan = chip->chan;
  100. struct ngene_channel *ch = &chan->dev->channel[chan->number - 2];
  101. struct i2c_adapter *adap = &ch->i2c_adapter;
  102. if (ch->soundstreamon == 1)
  103. ;/*ngene_command_stream_control_sound(chan->dev, chan->number,
  104. 0x00, 0x00);*/
  105. i2c_clients_command(adap, IOCTL_MIC_DEC_SRATE, &(runtime->rate));
  106. mdelay(80);
  107. if (ch->soundstreamon == 1)
  108. ;/*ngene_command_stream_control_sound(chan->dev, chan->number,
  109. 0x80, 0x04);*/
  110. return 0;
  111. }
  112. /* trigger callback */
  113. static int snd_mychip_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  114. {
  115. struct mychip *chip = snd_pcm_substream_chip(substream);
  116. struct ngene_channel *chan = chip->chan;
  117. switch (cmd) {
  118. case SNDRV_PCM_TRIGGER_START:
  119. /* do something to start the PCM engine */
  120. chan->sndbuffflag = 0;
  121. break;
  122. case SNDRV_PCM_TRIGGER_STOP:
  123. /* do something to stop the PCM engine */
  124. chip->substream = NULL;
  125. chan->sndbuffflag = 0;
  126. break;
  127. default:
  128. return -EINVAL;
  129. }
  130. return 0;
  131. }
  132. /* pointer callback */
  133. static snd_pcm_uframes_t
  134. snd_mychip_pcm_pointer(struct snd_pcm_substream *substream)
  135. {
  136. struct mychip *chip = snd_pcm_substream_chip(substream);
  137. struct ngene_channel *chan = chip->chan;
  138. unsigned int current_ptr;
  139. if (chan->sndbuffflag == 0) {
  140. current_ptr = (unsigned int)
  141. bytes_to_frames(substream->runtime, 0);
  142. } else {
  143. current_ptr = (unsigned int)
  144. bytes_to_frames(substream->runtime, 8192);
  145. }
  146. return current_ptr;
  147. }
  148. /*copy sound buffer to pcm middel layer*/
  149. static int snd_capture_copy(struct snd_pcm_substream *substream, int channel,
  150. snd_pcm_uframes_t pos, void *dst,
  151. snd_pcm_uframes_t count)
  152. {
  153. struct snd_pcm_runtime *runtime = substream->runtime;
  154. struct mychip *chip = snd_pcm_substream_chip(substream);
  155. struct ngene_channel *chan = chip->chan;
  156. memcpy(dst, chan->soundbuffer, frames_to_bytes(runtime, count));
  157. return 0;
  158. }
  159. static int snd_pcm_capture_silence(struct snd_pcm_substream *substream,
  160. int channel,
  161. snd_pcm_uframes_t pos,
  162. snd_pcm_uframes_t count)
  163. {
  164. /*
  165. struct snd_pcm_runtime *runtime = substream->runtime;
  166. struct mychip *chip = snd_pcm_substream_chip(substream);
  167. struct ngene_channel *chan = chip->chan;
  168. */
  169. return 0;
  170. }
  171. /* operators */
  172. static struct snd_pcm_ops snd_mychip_capture_ops = {
  173. .open = snd_mychip_capture_open,
  174. .close = snd_mychip_capture_close,
  175. .ioctl = snd_pcm_lib_ioctl,
  176. .hw_params = snd_mychip_pcm_hw_params,
  177. .hw_free = snd_mychip_pcm_hw_free,
  178. .prepare = snd_mychip_pcm_prepare,
  179. .trigger = snd_mychip_pcm_trigger,
  180. .pointer = snd_mychip_pcm_pointer,
  181. .copy = snd_capture_copy,
  182. .silence = snd_pcm_capture_silence,
  183. };
  184. static void mychip_pcm_free(struct snd_pcm *pcm)
  185. {
  186. pcm->private_data = NULL;
  187. }
  188. /* create a pcm device */
  189. static int snd_mychip_new_pcm(struct mychip *chip, struct ngene_channel *chan)
  190. {
  191. struct snd_pcm *pcm;
  192. int err;
  193. char gro[10];
  194. sprintf(gro, "PCM%d", chan->number);
  195. err = snd_pcm_new(chip->card, gro, 0, 0, 1, &pcm);
  196. if (err < 0)
  197. return err;
  198. pcm->private_data = chip;
  199. pcm->private_free = mychip_pcm_free;
  200. sprintf(pcm->name, "MyPCM_%d", chan->number);
  201. chip->pcm = pcm;
  202. /* set operators */
  203. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_mychip_capture_ops);
  204. /* pre-allocation of buffers */
  205. err = snd_pcm_lib_preallocate_pages_for_all(pcm,
  206. SNDRV_DMA_TYPE_CONTINUOUS,
  207. snd_dma_continuous_data(
  208. GFP_KERNEL),
  209. 0, 16 * 1024);
  210. return 0;
  211. }
  212. #define ngene_VOLUME(xname, xindex, addr) \
  213. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  214. .info = snd_volume_info, \
  215. .get = snd_volume_get, .put = snd_volume_put, \
  216. .private_value = addr }
  217. static int snd_volume_info(struct snd_kcontrol *kcontrol,
  218. struct snd_ctl_elem_info *uinfo)
  219. {
  220. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  221. uinfo->count = 2;
  222. uinfo->value.integer.min = 0;
  223. uinfo->value.integer.max = 20;
  224. return 0;
  225. }
  226. static int snd_volume_get(struct snd_kcontrol *kcontrol,
  227. struct snd_ctl_elem_value *ucontrol)
  228. {
  229. struct mychip *chip = snd_kcontrol_chip(kcontrol);
  230. int addr = kcontrol->private_value;
  231. ucontrol->value.integer.value[0] = chip->mixer_volume[addr][0];
  232. ucontrol->value.integer.value[1] = chip->mixer_volume[addr][1];
  233. return 0;
  234. }
  235. static int snd_volume_put(struct snd_kcontrol *kcontrol,
  236. struct snd_ctl_elem_value *ucontrol)
  237. {
  238. struct mychip *chip = snd_kcontrol_chip(kcontrol);
  239. int change, addr = kcontrol->private_value;
  240. int left, right;
  241. left = ucontrol->value.integer.value[0];
  242. if (left < 0)
  243. left = 0;
  244. if (left > 20)
  245. left = 20;
  246. right = ucontrol->value.integer.value[1];
  247. if (right < 0)
  248. right = 0;
  249. if (right > 20)
  250. right = 20;
  251. spin_lock_irq(&chip->mixer_lock);
  252. change = chip->mixer_volume[addr][0] != left ||
  253. chip->mixer_volume[addr][1] != right;
  254. chip->mixer_volume[addr][0] = left;
  255. chip->mixer_volume[addr][1] = right;
  256. spin_unlock_irq(&chip->mixer_lock);
  257. return change;
  258. }
  259. #define ngene_CAPSRC(xname, xindex, addr) \
  260. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  261. .info = snd_capsrc_info, \
  262. .get = snd_capsrc_get, .put = snd_capsrc_put, \
  263. .private_value = addr }
  264. static int snd_capsrc_info(struct snd_kcontrol *kcontrol,
  265. struct snd_ctl_elem_info *uinfo)
  266. {
  267. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  268. uinfo->count = 2;
  269. uinfo->value.integer.min = 0;
  270. uinfo->value.integer.max = 1;
  271. return 0;
  272. }
  273. static int snd_capsrc_get(struct snd_kcontrol *kcontrol,
  274. struct snd_ctl_elem_value *ucontrol)
  275. {
  276. struct mychip *chip = snd_kcontrol_chip(kcontrol);
  277. int addr = kcontrol->private_value;
  278. spin_lock_irq(&chip->mixer_lock);
  279. ucontrol->value.integer.value[0] = chip->capture_source[addr][0];
  280. ucontrol->value.integer.value[1] = chip->capture_source[addr][1];
  281. spin_unlock_irq(&chip->mixer_lock);
  282. return 0;
  283. }
  284. static int snd_capsrc_put(struct snd_kcontrol *kcontrol,
  285. struct snd_ctl_elem_value *ucontrol)
  286. {
  287. struct mychip *chip = snd_kcontrol_chip(kcontrol);
  288. int change, addr = kcontrol->private_value;
  289. int left, right;
  290. left = ucontrol->value.integer.value[0] & 1;
  291. right = ucontrol->value.integer.value[1] & 1;
  292. spin_lock_irq(&chip->mixer_lock);
  293. change = chip->capture_source[addr][0] != left ||
  294. chip->capture_source[addr][1] != right;
  295. chip->capture_source[addr][0] = left;
  296. chip->capture_source[addr][1] = right;
  297. spin_unlock_irq(&chip->mixer_lock);
  298. if (change)
  299. printk(KERN_INFO "snd_capsrc_put change\n");
  300. return 0;
  301. }
  302. static struct snd_kcontrol_new snd_controls[] = {
  303. ngene_VOLUME("Video Volume", 0, MIXER_ADDR_TVTUNER),
  304. ngene_CAPSRC("Video Capture Switch", 0, MIXER_ADDR_TVTUNER),
  305. };
  306. static int snd_card_new_mixer(struct mychip *chip)
  307. {
  308. struct snd_card *card = chip->card;
  309. unsigned int idx;
  310. int err;
  311. strcpy(card->mixername, "NgeneMixer");
  312. for (idx = 0; idx < ARRAY_SIZE(snd_controls); idx++) {
  313. err = snd_ctl_add(card, snd_ctl_new1(&snd_controls[idx], chip));
  314. if (err < 0)
  315. return err;
  316. }
  317. return 0;
  318. }
  319. int ngene_snd_init(struct ngene_channel *chan)
  320. {
  321. struct snd_card *card;
  322. struct mychip *chip;
  323. int err;
  324. if (sound_dev >= SNDRV_CARDS)
  325. return -ENODEV;
  326. if (!enable[sound_dev]) {
  327. sound_dev++;
  328. return -ENOENT;
  329. }
  330. card = snd_card_new(index[sound_dev], id[sound_dev],
  331. THIS_MODULE, sizeof(struct mychip));
  332. if (card == NULL)
  333. return -ENOMEM;
  334. chip = card->private_data;
  335. chip->card = card;
  336. chip->irq = -1;
  337. sprintf(card->shortname, "MyChip%d%d", chan->dev->nr, chan->number);
  338. sprintf(card->shortname, "Myown%d%d", chan->dev->nr, chan->number);
  339. sprintf(card->longname, "My first Own Chip on Card Nr.%d is %d",
  340. chan->dev->nr, chan->number);
  341. spin_lock_init(&chip->lock);
  342. spin_lock_init(&chip->mixer_lock);
  343. snd_card_new_mixer(chip);
  344. snd_mychip_new_pcm(chip, chan);
  345. err = snd_card_register(card);
  346. if (err < 0) {
  347. snd_card_free(card);
  348. return err;
  349. }
  350. chan->soundcard = card;
  351. chan->mychip = chip;
  352. chip->chan = chan;
  353. sound_dev++;
  354. return 0;
  355. }
  356. int ngene_snd_exit(struct ngene_channel *chan)
  357. {
  358. snd_card_free(chan->soundcard);
  359. return 0;
  360. }