snd-go7007.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2005-2006 Micronas USA Inc.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/moduleparam.h>
  8. #include <linux/spinlock.h>
  9. #include <linux/delay.h>
  10. #include <linux/sched.h>
  11. #include <linux/time.h>
  12. #include <linux/mm.h>
  13. #include <linux/i2c.h>
  14. #include <linux/mutex.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/slab.h>
  17. #include <sound/core.h>
  18. #include <sound/pcm.h>
  19. #include <sound/initval.h>
  20. #include "go7007-priv.h"
  21. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  22. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  23. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  24. module_param_array(index, int, NULL, 0444);
  25. module_param_array(id, charp, NULL, 0444);
  26. module_param_array(enable, bool, NULL, 0444);
  27. MODULE_PARM_DESC(index, "Index value for the go7007 audio driver");
  28. MODULE_PARM_DESC(id, "ID string for the go7007 audio driver");
  29. MODULE_PARM_DESC(enable, "Enable for the go7007 audio driver");
  30. struct go7007_snd {
  31. struct snd_card *card;
  32. struct snd_pcm *pcm;
  33. struct snd_pcm_substream *substream;
  34. spinlock_t lock;
  35. int w_idx;
  36. int hw_ptr;
  37. int avail;
  38. int capturing;
  39. };
  40. static const struct snd_pcm_hardware go7007_snd_capture_hw = {
  41. .info = (SNDRV_PCM_INFO_MMAP |
  42. SNDRV_PCM_INFO_INTERLEAVED |
  43. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  44. SNDRV_PCM_INFO_MMAP_VALID),
  45. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  46. .rates = SNDRV_PCM_RATE_48000,
  47. .rate_min = 48000,
  48. .rate_max = 48000,
  49. .channels_min = 2,
  50. .channels_max = 2,
  51. .buffer_bytes_max = (128*1024),
  52. .period_bytes_min = 4096,
  53. .period_bytes_max = (128*1024),
  54. .periods_min = 1,
  55. .periods_max = 32,
  56. };
  57. static void parse_audio_stream_data(struct go7007 *go, u8 *buf, int length)
  58. {
  59. struct go7007_snd *gosnd = go->snd_context;
  60. struct snd_pcm_runtime *runtime = gosnd->substream->runtime;
  61. int frames = bytes_to_frames(runtime, length);
  62. unsigned long flags;
  63. spin_lock_irqsave(&gosnd->lock, flags);
  64. gosnd->hw_ptr += frames;
  65. if (gosnd->hw_ptr >= runtime->buffer_size)
  66. gosnd->hw_ptr -= runtime->buffer_size;
  67. gosnd->avail += frames;
  68. spin_unlock_irqrestore(&gosnd->lock, flags);
  69. if (gosnd->w_idx + length > runtime->dma_bytes) {
  70. int cpy = runtime->dma_bytes - gosnd->w_idx;
  71. memcpy(runtime->dma_area + gosnd->w_idx, buf, cpy);
  72. length -= cpy;
  73. buf += cpy;
  74. gosnd->w_idx = 0;
  75. }
  76. memcpy(runtime->dma_area + gosnd->w_idx, buf, length);
  77. gosnd->w_idx += length;
  78. spin_lock_irqsave(&gosnd->lock, flags);
  79. if (gosnd->avail < runtime->period_size) {
  80. spin_unlock_irqrestore(&gosnd->lock, flags);
  81. return;
  82. }
  83. gosnd->avail -= runtime->period_size;
  84. spin_unlock_irqrestore(&gosnd->lock, flags);
  85. if (gosnd->capturing)
  86. snd_pcm_period_elapsed(gosnd->substream);
  87. }
  88. static int go7007_snd_hw_params(struct snd_pcm_substream *substream,
  89. struct snd_pcm_hw_params *hw_params)
  90. {
  91. struct go7007 *go = snd_pcm_substream_chip(substream);
  92. go->audio_deliver = parse_audio_stream_data;
  93. return 0;
  94. }
  95. static int go7007_snd_hw_free(struct snd_pcm_substream *substream)
  96. {
  97. struct go7007 *go = snd_pcm_substream_chip(substream);
  98. go->audio_deliver = NULL;
  99. return 0;
  100. }
  101. static int go7007_snd_capture_open(struct snd_pcm_substream *substream)
  102. {
  103. struct go7007 *go = snd_pcm_substream_chip(substream);
  104. struct go7007_snd *gosnd = go->snd_context;
  105. unsigned long flags;
  106. int r;
  107. spin_lock_irqsave(&gosnd->lock, flags);
  108. if (gosnd->substream == NULL) {
  109. gosnd->substream = substream;
  110. substream->runtime->hw = go7007_snd_capture_hw;
  111. r = 0;
  112. } else
  113. r = -EBUSY;
  114. spin_unlock_irqrestore(&gosnd->lock, flags);
  115. return r;
  116. }
  117. static int go7007_snd_capture_close(struct snd_pcm_substream *substream)
  118. {
  119. struct go7007 *go = snd_pcm_substream_chip(substream);
  120. struct go7007_snd *gosnd = go->snd_context;
  121. gosnd->substream = NULL;
  122. return 0;
  123. }
  124. static int go7007_snd_pcm_prepare(struct snd_pcm_substream *substream)
  125. {
  126. return 0;
  127. }
  128. static int go7007_snd_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  129. {
  130. struct go7007 *go = snd_pcm_substream_chip(substream);
  131. struct go7007_snd *gosnd = go->snd_context;
  132. switch (cmd) {
  133. case SNDRV_PCM_TRIGGER_START:
  134. /* Just set a flag to indicate we should signal ALSA when
  135. * sound comes in */
  136. gosnd->capturing = 1;
  137. return 0;
  138. case SNDRV_PCM_TRIGGER_STOP:
  139. gosnd->hw_ptr = gosnd->w_idx = gosnd->avail = 0;
  140. gosnd->capturing = 0;
  141. return 0;
  142. default:
  143. return -EINVAL;
  144. }
  145. }
  146. static snd_pcm_uframes_t go7007_snd_pcm_pointer(struct snd_pcm_substream *substream)
  147. {
  148. struct go7007 *go = snd_pcm_substream_chip(substream);
  149. struct go7007_snd *gosnd = go->snd_context;
  150. return gosnd->hw_ptr;
  151. }
  152. static const struct snd_pcm_ops go7007_snd_capture_ops = {
  153. .open = go7007_snd_capture_open,
  154. .close = go7007_snd_capture_close,
  155. .hw_params = go7007_snd_hw_params,
  156. .hw_free = go7007_snd_hw_free,
  157. .prepare = go7007_snd_pcm_prepare,
  158. .trigger = go7007_snd_pcm_trigger,
  159. .pointer = go7007_snd_pcm_pointer,
  160. };
  161. static int go7007_snd_free(struct snd_device *device)
  162. {
  163. struct go7007 *go = device->device_data;
  164. kfree(go->snd_context);
  165. go->snd_context = NULL;
  166. return 0;
  167. }
  168. static const struct snd_device_ops go7007_snd_device_ops = {
  169. .dev_free = go7007_snd_free,
  170. };
  171. int go7007_snd_init(struct go7007 *go)
  172. {
  173. static int dev;
  174. struct go7007_snd *gosnd;
  175. int ret;
  176. if (dev >= SNDRV_CARDS)
  177. return -ENODEV;
  178. if (!enable[dev]) {
  179. dev++;
  180. return -ENOENT;
  181. }
  182. gosnd = kmalloc(sizeof(struct go7007_snd), GFP_KERNEL);
  183. if (gosnd == NULL)
  184. return -ENOMEM;
  185. spin_lock_init(&gosnd->lock);
  186. gosnd->hw_ptr = gosnd->w_idx = gosnd->avail = 0;
  187. gosnd->capturing = 0;
  188. ret = snd_card_new(go->dev, index[dev], id[dev], THIS_MODULE, 0,
  189. &gosnd->card);
  190. if (ret < 0)
  191. goto free_snd;
  192. ret = snd_device_new(gosnd->card, SNDRV_DEV_LOWLEVEL, go,
  193. &go7007_snd_device_ops);
  194. if (ret < 0)
  195. goto free_card;
  196. ret = snd_pcm_new(gosnd->card, "go7007", 0, 0, 1, &gosnd->pcm);
  197. if (ret < 0)
  198. goto free_card;
  199. strscpy(gosnd->card->driver, "go7007", sizeof(gosnd->card->driver));
  200. strscpy(gosnd->card->shortname, go->name, sizeof(gosnd->card->shortname));
  201. strscpy(gosnd->card->longname, gosnd->card->shortname,
  202. sizeof(gosnd->card->longname));
  203. gosnd->pcm->private_data = go;
  204. snd_pcm_set_ops(gosnd->pcm, SNDRV_PCM_STREAM_CAPTURE,
  205. &go7007_snd_capture_ops);
  206. snd_pcm_set_managed_buffer_all(gosnd->pcm, SNDRV_DMA_TYPE_VMALLOC,
  207. NULL, 0, 0);
  208. ret = snd_card_register(gosnd->card);
  209. if (ret < 0)
  210. goto free_card;
  211. gosnd->substream = NULL;
  212. go->snd_context = gosnd;
  213. v4l2_device_get(&go->v4l2_dev);
  214. ++dev;
  215. return 0;
  216. free_card:
  217. snd_card_free(gosnd->card);
  218. free_snd:
  219. kfree(gosnd);
  220. return ret;
  221. }
  222. EXPORT_SYMBOL(go7007_snd_init);
  223. int go7007_snd_remove(struct go7007 *go)
  224. {
  225. struct go7007_snd *gosnd = go->snd_context;
  226. snd_card_disconnect(gosnd->card);
  227. snd_card_free_when_closed(gosnd->card);
  228. v4l2_device_put(&go->v4l2_dev);
  229. return 0;
  230. }
  231. EXPORT_SYMBOL(go7007_snd_remove);
  232. MODULE_LICENSE("GPL v2");