saa7134-alsa.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /*
  2. *
  3. * Support for audio capture
  4. * PCI function #1 of the saa7134
  5. *
  6. * (c) 2005 Mauro Carvalho Chehab <mchehab@brturbo.com.br>
  7. * (c) 2004 Gerd Knorr <kraxel@bytesex.org>
  8. * (c) 2003 Clemens Ladisch <clemens@ladisch.de>
  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. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. */
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/device.h>
  27. #include <linux/interrupt.h>
  28. #include <asm/delay.h>
  29. #include <sound/driver.h>
  30. #include <sound/core.h>
  31. #include <sound/pcm.h>
  32. #include <sound/pcm_params.h>
  33. #include <sound/control.h>
  34. #include <sound/initval.h>
  35. #include "saa7134.h"
  36. #include "saa7134-reg.h"
  37. #define dprintk(level,fmt, arg...) if (debug >= level) \
  38. printk(KERN_DEBUG "%s/1: " fmt, chip->core->name , ## arg)
  39. /****************************************************************************
  40. Data type declarations - Can be moded to a header file later
  41. ****************************************************************************/
  42. #define ANALOG_CLOCK 1792000
  43. #define CLOCK_DIV_MIN 4
  44. #define CLOCK_DIV_MAX 15
  45. #define MAX_PCM_DEVICES 4
  46. #define MAX_PCM_SUBSTREAMS 16
  47. enum { DEVICE_DIGITAL, DEVICE_ANALOG };
  48. /* These can be replaced after done */
  49. #define MIXER_ADDR_LAST MAX_saa7134_INPUT
  50. struct saa7134_audio_dev {
  51. struct saa7134_core *core;
  52. struct saa7134_buffer *buf;
  53. struct saa7134_dmaqueue q;
  54. /* pci i/o */
  55. struct pci_dev *pci;
  56. unsigned char pci_rev,pci_lat;
  57. /* audio controls */
  58. int irq;
  59. int dig_rate; /* Digital sampling rate */
  60. snd_card_t *card;
  61. spinlock_t reg_lock;
  62. unsigned int dma_size;
  63. unsigned int period_size;
  64. int mixer_volume[MIXER_ADDR_LAST+1][2];
  65. int capture_source[MIXER_ADDR_LAST+1][2];
  66. long opened;
  67. snd_pcm_substream_t *substream;
  68. };
  69. typedef struct saa7134_audio_dev snd_saa7134_card_t;
  70. /****************************************************************************
  71. Module global static vars
  72. ****************************************************************************/
  73. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  74. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  75. static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 1};
  76. module_param_array(enable, bool, NULL, 0444);
  77. MODULE_PARM_DESC(enable, "Enable saa7134x soundcard. default enabled.");
  78. /****************************************************************************
  79. Module macros
  80. ****************************************************************************/
  81. MODULE_DESCRIPTION("ALSA driver module for saa7134 based TV cards");
  82. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@brturbo.com.br>");
  83. MODULE_LICENSE("GPL");
  84. MODULE_SUPPORTED_DEVICE("{{Philips, saa7131E},"
  85. "{{Philips, saa7134},"
  86. "{{Philips, saa7133}");
  87. static unsigned int debug = 0;
  88. module_param(debug,int,0644);
  89. MODULE_PARM_DESC(debug,"enable debug messages");
  90. /****************************************************************************
  91. Module specific funtions
  92. ****************************************************************************/
  93. /*
  94. * BOARD Specific: Sets audio DMA
  95. */
  96. int saa7134_start_audio_dma(snd_saa7134_card_t *chip)
  97. {
  98. struct saa7134_core *core = chip->core;
  99. return 0;
  100. }
  101. /*
  102. * BOARD Specific: Resets audio DMA
  103. */
  104. int saa7134_stop_audio_dma(snd_saa7134_card_t *chip)
  105. {
  106. struct saa7134_core *core=chip->core;
  107. return 0;
  108. }
  109. #define MAX_IRQ_LOOP 10
  110. static void saa713401_timeout(unsigned long data)
  111. {
  112. snd_saa7134_card_t *chip = (snd_saa7134_card_t *)data;
  113. }
  114. /* FIXME: Wrong values*/
  115. static char *saa7134_aud_irqs[32] = {
  116. "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
  117. "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
  118. "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
  119. "y_sync", "u_sync", "v_sync", "vbi_sync",
  120. "opc_err", "par_err", "rip_err", "pci_abort",
  121. };
  122. static void saa713401_aud_irq(snd_saa7134_card_t *chip)
  123. {
  124. struct saa7134_core *core = chip->core;
  125. }
  126. static irqreturn_t saa7134_irq(int irq, void *dev_id, struct pt_regs *regs)
  127. {
  128. snd_saa7134_card_t *chip = dev_id;
  129. struct saa7134_core *core = chip->core;
  130. }
  131. /****************************************************************************
  132. ALSA PCM Interface
  133. ****************************************************************************/
  134. /*
  135. * Digital hardware definition
  136. */
  137. static snd_pcm_hardware_t snd_saa7134_digital_hw = {
  138. .info = SNDRV_PCM_INFO_MMAP |
  139. SNDRV_PCM_INFO_INTERLEAVED |
  140. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  141. SNDRV_PCM_INFO_MMAP_VALID,
  142. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  143. .rates = 0, /* set at runtime */
  144. .channels_min = 2,
  145. .channels_max = 2,
  146. .buffer_bytes_max = 255 * 4092,
  147. .period_bytes_min = 32,
  148. .period_bytes_max = 4092,
  149. .periods_min = 2,
  150. .periods_max = 255,
  151. };
  152. /*
  153. * Sets board to provide digital audio
  154. */
  155. static int snd_saa7134_set_digital_hw(snd_saa7134_card_t *chip, snd_pcm_runtime_t *runtime)
  156. {
  157. return 0;
  158. }
  159. /*
  160. * audio open callback
  161. */
  162. static int snd_saa7134_pcm_open(snd_pcm_substream_t *substream)
  163. {
  164. snd_saa7134_card_t *chip = snd_pcm_substream_chip(substream);
  165. snd_pcm_runtime_t *runtime = substream->runtime;
  166. int err;
  167. if (test_and_set_bit(0, &chip->opened))
  168. return -EBUSY;
  169. err = snd_saa7134_set_digital_hw(chip, runtime);
  170. if (err < 0)
  171. goto _error;
  172. err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  173. if (err < 0)
  174. goto _error;
  175. chip->substream = substream;
  176. return 0;
  177. _error:
  178. clear_bit(0, &chip->opened);
  179. smp_mb__after_clear_bit();
  180. return err;
  181. }
  182. /*
  183. * audio close callback
  184. */
  185. static int snd_saa7134_close(snd_pcm_substream_t *substream)
  186. {
  187. snd_saa7134_card_t *chip = snd_pcm_substream_chip(substream);
  188. chip->substream = NULL;
  189. clear_bit(0, &chip->opened);
  190. smp_mb__after_clear_bit();
  191. return 0;
  192. }
  193. /*
  194. * hw_params callback
  195. */
  196. static int snd_saa7134_hw_params(snd_pcm_substream_t * substream,
  197. snd_pcm_hw_params_t * hw_params)
  198. {
  199. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  200. }
  201. /*
  202. * hw free callback
  203. */
  204. static int snd_saa7134_hw_free(snd_pcm_substream_t * substream)
  205. {
  206. return snd_pcm_lib_free_pages(substream);
  207. }
  208. /*
  209. * prepare callback
  210. */
  211. static int snd_saa7134_prepare(snd_pcm_substream_t *substream)
  212. {
  213. snd_saa7134_card_t *chip = snd_pcm_substream_chip(substream);
  214. return 0;
  215. }
  216. /*
  217. * trigger callback
  218. */
  219. static int snd_saa7134_card_trigger(snd_pcm_substream_t *substream, int cmd)
  220. {
  221. snd_saa7134_card_t *chip = snd_pcm_substream_chip(substream);
  222. switch (cmd) {
  223. case SNDRV_PCM_TRIGGER_START:
  224. return snd_saa7134_start(chip);
  225. case SNDRV_PCM_TRIGGER_STOP:
  226. return snd_saa7134_stop(chip);
  227. default:
  228. return -EINVAL;
  229. }
  230. }
  231. /*
  232. * pointer callback
  233. */
  234. static snd_pcm_uframes_t snd_saa7134_pointer(snd_pcm_substream_t *substream)
  235. {
  236. // snd_saa7134_card_t *chip = snd_pcm_substream_chip(substream);
  237. // snd_pcm_runtime_t *runtime = substream->runtime;
  238. // return (snd_pcm_uframes_t)bytes_to_frames(runtime, chip->current_line * chip->line_bytes);
  239. }
  240. /*
  241. * operators
  242. */
  243. static snd_pcm_ops_t snd_saa7134_pcm_ops = {
  244. .open = snd_saa7134_pcm_open,
  245. .close = snd_saa7134_close,
  246. .ioctl = snd_pcm_lib_ioctl,
  247. .hw_params = snd_saa7134_hw_params,
  248. .hw_free = snd_saa7134_hw_free,
  249. .prepare = snd_saa7134_prepare,
  250. .trigger = snd_saa7134_card_trigger,
  251. .pointer = snd_saa7134_pointer,
  252. .page = snd_pcm_sgbuf_ops_page,
  253. };
  254. /*
  255. * create a PCM device
  256. */
  257. static int __devinit snd_saa7134_pcm(snd_saa7134_card_t *chip, int device, char *name)
  258. {
  259. int err;
  260. snd_pcm_t *pcm;
  261. err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
  262. if (err < 0)
  263. return err;
  264. pcm->private_data = chip;
  265. strcpy(pcm->name, name);
  266. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_saa7134_pcm_ops);
  267. return snd_pcm_lib_preallocate_pages_for_all(pcm,
  268. SNDRV_DMA_TYPE_DEV_SG,
  269. snd_dma_pci_data(chip->pci),
  270. 128 * 1024,
  271. (255 * 4092 + 1023) & ~1023);
  272. }
  273. /****************************************************************************
  274. CONTROL INTERFACE
  275. ****************************************************************************/
  276. static int snd_saa7134_capture_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *info)
  277. {
  278. info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  279. info->count = 1;
  280. info->value.integer.min = 0;
  281. info->value.integer.max = 0x3f;
  282. return 0;
  283. }
  284. /* OK - TODO: test it */
  285. static int snd_saa7134_capture_volume_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *value)
  286. {
  287. snd_saa7134_card_t *chip = snd_kcontrol_chip(kcontrol);
  288. struct saa7134_core *core=chip->core;
  289. return 0;
  290. }
  291. /* OK - TODO: test it */
  292. static int snd_saa7134_capture_volume_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *value)
  293. {
  294. snd_saa7134_card_t *chip = snd_kcontrol_chip(kcontrol);
  295. struct saa7134_core *core=chip->core;
  296. return 0;
  297. }
  298. static snd_kcontrol_new_t snd_saa7134_capture_volume = {
  299. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  300. .name = "Capture Volume",
  301. .info = snd_saa7134_capture_volume_info,
  302. .get = snd_saa7134_capture_volume_get,
  303. .put = snd_saa7134_capture_volume_put,
  304. };
  305. /*
  306. ***************************************
  307. */
  308. /****************************************************************************
  309. Basic Flow for Sound Devices
  310. ****************************************************************************/
  311. /*
  312. * PCI ID Table - 14f1:8801 and 14f1:8811 means function 1: Audio
  313. * Only boards with eeprom and byte 1 at eeprom=1 have it
  314. */
  315. struct pci_device_id saa7134_audio_pci_tbl[] = {
  316. {0x14f1,0x8801,PCI_ANY_ID,PCI_ANY_ID,0,0,0},
  317. {0x14f1,0x8811,PCI_ANY_ID,PCI_ANY_ID,0,0,0},
  318. {0, }
  319. };
  320. MODULE_DEVICE_TABLE(pci, saa7134_audio_pci_tbl);
  321. /*
  322. * Chip-specific destructor
  323. */
  324. static int snd_saa7134_free(snd_saa7134_card_t *chip)
  325. {
  326. if (chip->irq >= 0)
  327. free_irq(chip->irq, chip);
  328. /* free memory */
  329. saa7134_core_put(chip->core,chip->pci);
  330. pci_release_regions(chip->pci);
  331. pci_disable_device(chip->pci);
  332. kfree(chip);
  333. return 0;
  334. }
  335. /*
  336. * Component Destructor
  337. */
  338. static int snd_saa7134_dev_free(snd_device_t *device)
  339. {
  340. snd_saa7134_card_t *chip = device->device_data;
  341. return snd_saa7134_free(chip);
  342. }
  343. /*
  344. * Alsa Constructor - Component probe
  345. */
  346. static int devno=0;
  347. static int __devinit snd_saa7134_create(snd_card_t *card, struct pci_dev *pci,
  348. snd_saa7134_card_t **rchip)
  349. {
  350. snd_saa7134_card_t *chip;
  351. struct saa7134_core *core;
  352. return 0;
  353. }
  354. static int __devinit saa7134_audio_initdev(struct pci_dev *pci,
  355. const struct pci_device_id *pci_id)
  356. {
  357. snd_card_t *card;
  358. snd_saa7134_card_t *chip;
  359. int err;
  360. if (devno >= SNDRV_CARDS)
  361. return (-ENODEV);
  362. if (!enable[devno]) {
  363. ++devno;
  364. return (-ENOENT);
  365. }
  366. card = snd_card_new(index[devno], id[devno], THIS_MODULE, 0);
  367. if (!card)
  368. return (-ENOMEM);
  369. err = snd_saa7134_create(card, pci, &chip);
  370. if (err < 0)
  371. return (err);
  372. /*
  373. err = snd_saa7134_pcm(chip, DEVICE_DIGITAL, "saa7134 Digital");
  374. if (err < 0)
  375. goto fail_free;
  376. */
  377. err = snd_ctl_add(card, snd_ctl_new1(&snd_saa7134_capture_volume, chip));
  378. if (err < 0) {
  379. snd_card_free(card);
  380. return (err);
  381. }
  382. strcpy (card->driver, "saa7134_ALSA");
  383. sprintf(card->shortname, "Saa7134 %x", pci->device);
  384. sprintf(card->longname, "%s at %#lx",
  385. card->shortname, pci_resource_start(pci, 0));
  386. strcpy (card->mixername, "saa7134");
  387. dprintk (0, "%s/%i: Alsa support for saa7134x boards\n",
  388. card->driver,devno);
  389. err = snd_card_register(card);
  390. if (err < 0) {
  391. snd_card_free(card);
  392. return (err);
  393. }
  394. pci_set_drvdata(pci,card);
  395. devno++;
  396. return 0;
  397. }
  398. /*
  399. * ALSA destructor
  400. */
  401. static void __devexit saa7134_audio_finidev(struct pci_dev *pci)
  402. {
  403. snd_card_free(pci_get_drvdata(pci));
  404. pci_set_drvdata(pci, NULL);
  405. devno--;
  406. }
  407. /*
  408. * PCI driver definition
  409. */
  410. static struct pci_driver saa7134_audio_pci_driver = {
  411. .name = "saa7134_audio",
  412. .id_table = saa7134_audio_pci_tbl,
  413. .probe = saa7134_audio_initdev,
  414. .remove = saa7134_audio_finidev,
  415. SND_PCI_PM_CALLBACKS
  416. };
  417. /****************************************************************************
  418. LINUX MODULE INIT
  419. ****************************************************************************/
  420. /*
  421. * module init
  422. */
  423. static int saa7134_audio_init(void)
  424. {
  425. printk(KERN_INFO "saa7134x alsa driver version %d.%d.%d loaded\n",
  426. (saa7134_VERSION_CODE >> 16) & 0xff,
  427. (saa7134_VERSION_CODE >> 8) & 0xff,
  428. saa7134_VERSION_CODE & 0xff);
  429. #ifdef SNAPSHOT
  430. printk(KERN_INFO "saa7134x: snapshot date %04d-%02d-%02d\n",
  431. SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
  432. #endif
  433. return pci_module_init(&saa7134_audio_pci_driver);
  434. }
  435. /*
  436. * module remove
  437. */
  438. static void saa7134_audio_fini(void)
  439. {
  440. pci_unregister_driver(&saa7134_audio_pci_driver);
  441. }
  442. module_init(saa7134_audio_init);
  443. module_exit(saa7134_audio_fini);
  444. /* ----------------------------------------------------------- */
  445. /*
  446. * Local variables:
  447. * c-basic-offset: 8
  448. * End:
  449. */