sa11xx-uda1341.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. /*
  2. * Driver for Philips UDA1341TS on Compaq iPAQ H3600 soundcard
  3. * Copyright (C) 2002 Tomas Kasparek <tomas.kasparek@seznam.cz>
  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.
  7. *
  8. * History:
  9. *
  10. * 2002-03-13 Tomas Kasparek initial release - based on h3600-uda1341.c from OSS
  11. * 2002-03-20 Tomas Kasparek playback over ALSA is working
  12. * 2002-03-28 Tomas Kasparek playback over OSS emulation is working
  13. * 2002-03-29 Tomas Kasparek basic capture is working (native ALSA)
  14. * 2002-03-29 Tomas Kasparek capture is working (OSS emulation)
  15. * 2002-04-04 Tomas Kasparek better rates handling (allow non-standard rates)
  16. * 2003-02-14 Brian Avery fixed full duplex mode, other updates
  17. * 2003-02-20 Tomas Kasparek merged updates by Brian (except HAL)
  18. * 2003-04-19 Jaroslav Kysela recoded DMA stuff to follow 2.4.18rmk3-hh24 kernel
  19. * working suspend and resume
  20. * 2003-04-28 Tomas Kasparek updated work by Jaroslav to compile it under 2.5.x again
  21. * merged HAL layer (patches from Brian)
  22. */
  23. /* $Id: sa11xx-uda1341.c,v 1.24 2005/11/17 10:25:22 tiwai Exp $ */
  24. /***************************************************************************************************
  25. *
  26. * To understand what Alsa Drivers should be doing look at "Writing an Alsa Driver" by Takashi Iwai
  27. * available in the Alsa doc section on the website
  28. *
  29. * A few notes to make things clearer. The UDA1341 is hooked up to Serial port 4 on the SA1100.
  30. * We are using SSP mode to talk to the UDA1341. The UDA1341 bit & wordselect clocks are generated
  31. * by this UART. Unfortunately, the clock only runs if the transmit buffer has something in it.
  32. * So, if we are just recording, we feed the transmit DMA stream a bunch of 0x0000 so that the
  33. * transmit buffer is full and the clock keeps going. The zeroes come from FLUSH_BASE_PHYS which
  34. * is a mem loc that always decodes to 0's w/ no off chip access.
  35. *
  36. * Some alsa terminology:
  37. * frame => num_channels * sample_size e.g stereo 16 bit is 2 * 16 = 32 bytes
  38. * period => the least number of bytes that will generate an interrupt e.g. we have a 1024 byte
  39. * buffer and 4 periods in the runtime structure this means we'll get an int every 256
  40. * bytes or 4 times per buffer.
  41. * A number of the sizes are in frames rather than bytes, use frames_to_bytes and
  42. * bytes_to_frames to convert. The easiest way to tell the units is to look at the
  43. * type i.e. runtime-> buffer_size is in frames and its type is snd_pcm_uframes_t
  44. *
  45. * Notes about the pointer fxn:
  46. * The pointer fxn needs to return the offset into the dma buffer in frames.
  47. * Interrupts must be blocked before calling the dma_get_pos fxn to avoid race with interrupts.
  48. *
  49. * Notes about pause/resume
  50. * Implementing this would be complicated so it's skipped. The problem case is:
  51. * A full duplex connection is going, then play is paused. At this point you need to start xmitting
  52. * 0's to keep the record active which means you cant just freeze the dma and resume it later you'd
  53. * need to save off the dma info, and restore it properly on a resume. Yeach!
  54. *
  55. * Notes about transfer methods:
  56. * The async write calls fail. I probably need to implement something else to support them?
  57. *
  58. ***************************************************************************************************/
  59. #include <linux/config.h>
  60. #include <sound/driver.h>
  61. #include <linux/module.h>
  62. #include <linux/moduleparam.h>
  63. #include <linux/init.h>
  64. #include <linux/errno.h>
  65. #include <linux/ioctl.h>
  66. #include <linux/delay.h>
  67. #include <linux/slab.h>
  68. #ifdef CONFIG_PM
  69. #include <linux/pm.h>
  70. #endif
  71. #include <asm/hardware.h>
  72. #include <asm/arch/h3600.h>
  73. #include <asm/mach-types.h>
  74. #include <asm/dma.h>
  75. #ifdef CONFIG_H3600_HAL
  76. #include <asm/semaphore.h>
  77. #include <asm/uaccess.h>
  78. #include <asm/arch/h3600_hal.h>
  79. #endif
  80. #include <sound/core.h>
  81. #include <sound/pcm.h>
  82. #include <sound/initval.h>
  83. #include <linux/l3/l3.h>
  84. #undef DEBUG_MODE
  85. #undef DEBUG_FUNCTION_NAMES
  86. #include <sound/uda1341.h>
  87. /*
  88. * FIXME: Is this enough as autodetection of 2.4.X-rmkY-hhZ kernels?
  89. * We use DMA stuff from 2.4.18-rmk3-hh24 here to be able to compile this
  90. * module for Familiar 0.6.1
  91. */
  92. #ifdef CONFIG_H3600_HAL
  93. #define HH_VERSION 1
  94. #endif
  95. /* {{{ Type definitions */
  96. MODULE_AUTHOR("Tomas Kasparek <tomas.kasparek@seznam.cz>");
  97. MODULE_LICENSE("GPL");
  98. MODULE_DESCRIPTION("SA1100/SA1111 + UDA1341TS driver for ALSA");
  99. MODULE_SUPPORTED_DEVICE("{{UDA1341,iPAQ H3600 UDA1341TS}}");
  100. static char *id = NULL; /* ID for this card */
  101. module_param(id, charp, 0444);
  102. MODULE_PARM_DESC(id, "ID string for SA1100/SA1111 + UDA1341TS soundcard.");
  103. typedef struct audio_stream {
  104. char *id; /* identification string */
  105. int stream_id; /* numeric identification */
  106. dma_device_t dma_dev; /* device identifier for DMA */
  107. #ifdef HH_VERSION
  108. dmach_t dmach; /* dma channel identification */
  109. #else
  110. dma_regs_t *dma_regs; /* points to our DMA registers */
  111. #endif
  112. int active:1; /* we are using this stream for transfer now */
  113. int period; /* current transfer period */
  114. int periods; /* current count of periods registerd in the DMA engine */
  115. int tx_spin; /* are we recoding - flag used to do DMA trans. for sync */
  116. unsigned int old_offset;
  117. spinlock_t dma_lock; /* for locking in DMA operations (see dma-sa1100.c in the kernel) */
  118. snd_pcm_substream_t *stream;
  119. }audio_stream_t;
  120. typedef struct snd_card_sa11xx_uda1341 {
  121. snd_card_t *card;
  122. struct l3_client *uda1341;
  123. snd_pcm_t *pcm;
  124. long samplerate;
  125. audio_stream_t s[2]; /* playback & capture */
  126. } sa11xx_uda1341_t;
  127. static unsigned int rates[] = {
  128. 8000, 10666, 10985, 14647,
  129. 16000, 21970, 22050, 24000,
  130. 29400, 32000, 44100, 48000,
  131. };
  132. static snd_pcm_hw_constraint_list_t hw_constraints_rates = {
  133. .count = ARRAY_SIZE(rates),
  134. .list = rates,
  135. .mask = 0,
  136. };
  137. /* }}} */
  138. /* {{{ Clock and sample rate stuff */
  139. /*
  140. * Stop-gap solution until rest of hh.org HAL stuff is merged.
  141. */
  142. #define GPIO_H3600_CLK_SET0 GPIO_GPIO (12)
  143. #define GPIO_H3600_CLK_SET1 GPIO_GPIO (13)
  144. #ifdef CONFIG_SA1100_H3XXX
  145. #define clr_sa11xx_uda1341_egpio(x) clr_h3600_egpio(x)
  146. #define set_sa11xx_uda1341_egpio(x) set_h3600_egpio(x)
  147. #else
  148. #error This driver could serve H3x00 handhelds only!
  149. #endif
  150. static void sa11xx_uda1341_set_audio_clock(long val)
  151. {
  152. switch (val) {
  153. case 24000: case 32000: case 48000: /* 00: 12.288 MHz */
  154. GPCR = GPIO_H3600_CLK_SET0 | GPIO_H3600_CLK_SET1;
  155. break;
  156. case 22050: case 29400: case 44100: /* 01: 11.2896 MHz */
  157. GPSR = GPIO_H3600_CLK_SET0;
  158. GPCR = GPIO_H3600_CLK_SET1;
  159. break;
  160. case 8000: case 10666: case 16000: /* 10: 4.096 MHz */
  161. GPCR = GPIO_H3600_CLK_SET0;
  162. GPSR = GPIO_H3600_CLK_SET1;
  163. break;
  164. case 10985: case 14647: case 21970: /* 11: 5.6245 MHz */
  165. GPSR = GPIO_H3600_CLK_SET0 | GPIO_H3600_CLK_SET1;
  166. break;
  167. }
  168. }
  169. static void sa11xx_uda1341_set_samplerate(sa11xx_uda1341_t *sa11xx_uda1341, long rate)
  170. {
  171. int clk_div = 0;
  172. int clk=0;
  173. /* We don't want to mess with clocks when frames are in flight */
  174. Ser4SSCR0 &= ~SSCR0_SSE;
  175. /* wait for any frame to complete */
  176. udelay(125);
  177. /*
  178. * We have the following clock sources:
  179. * 4.096 MHz, 5.6245 MHz, 11.2896 MHz, 12.288 MHz
  180. * Those can be divided either by 256, 384 or 512.
  181. * This makes up 12 combinations for the following samplerates...
  182. */
  183. if (rate >= 48000)
  184. rate = 48000;
  185. else if (rate >= 44100)
  186. rate = 44100;
  187. else if (rate >= 32000)
  188. rate = 32000;
  189. else if (rate >= 29400)
  190. rate = 29400;
  191. else if (rate >= 24000)
  192. rate = 24000;
  193. else if (rate >= 22050)
  194. rate = 22050;
  195. else if (rate >= 21970)
  196. rate = 21970;
  197. else if (rate >= 16000)
  198. rate = 16000;
  199. else if (rate >= 14647)
  200. rate = 14647;
  201. else if (rate >= 10985)
  202. rate = 10985;
  203. else if (rate >= 10666)
  204. rate = 10666;
  205. else
  206. rate = 8000;
  207. /* Set the external clock generator */
  208. #ifdef CONFIG_H3600_HAL
  209. h3600_audio_clock(rate);
  210. #else
  211. sa11xx_uda1341_set_audio_clock(rate);
  212. #endif
  213. /* Select the clock divisor */
  214. switch (rate) {
  215. case 8000:
  216. case 10985:
  217. case 22050:
  218. case 24000:
  219. clk = F512;
  220. clk_div = SSCR0_SerClkDiv(16);
  221. break;
  222. case 16000:
  223. case 21970:
  224. case 44100:
  225. case 48000:
  226. clk = F256;
  227. clk_div = SSCR0_SerClkDiv(8);
  228. break;
  229. case 10666:
  230. case 14647:
  231. case 29400:
  232. case 32000:
  233. clk = F384;
  234. clk_div = SSCR0_SerClkDiv(12);
  235. break;
  236. }
  237. /* FMT setting should be moved away when other FMTs are added (FIXME) */
  238. l3_command(sa11xx_uda1341->uda1341, CMD_FORMAT, (void *)LSB16);
  239. l3_command(sa11xx_uda1341->uda1341, CMD_FS, (void *)clk);
  240. Ser4SSCR0 = (Ser4SSCR0 & ~0xff00) + clk_div + SSCR0_SSE;
  241. sa11xx_uda1341->samplerate = rate;
  242. }
  243. /* }}} */
  244. /* {{{ HW init and shutdown */
  245. static void sa11xx_uda1341_audio_init(sa11xx_uda1341_t *sa11xx_uda1341)
  246. {
  247. unsigned long flags;
  248. /* Setup DMA stuff */
  249. sa11xx_uda1341->s[SNDRV_PCM_STREAM_PLAYBACK].id = "UDA1341 out";
  250. sa11xx_uda1341->s[SNDRV_PCM_STREAM_PLAYBACK].stream_id = SNDRV_PCM_STREAM_PLAYBACK;
  251. sa11xx_uda1341->s[SNDRV_PCM_STREAM_PLAYBACK].dma_dev = DMA_Ser4SSPWr;
  252. sa11xx_uda1341->s[SNDRV_PCM_STREAM_CAPTURE].id = "UDA1341 in";
  253. sa11xx_uda1341->s[SNDRV_PCM_STREAM_CAPTURE].stream_id = SNDRV_PCM_STREAM_CAPTURE;
  254. sa11xx_uda1341->s[SNDRV_PCM_STREAM_CAPTURE].dma_dev = DMA_Ser4SSPRd;
  255. /* Initialize the UDA1341 internal state */
  256. /* Setup the uarts */
  257. local_irq_save(flags);
  258. GAFR |= (GPIO_SSP_CLK);
  259. GPDR &= ~(GPIO_SSP_CLK);
  260. Ser4SSCR0 = 0;
  261. Ser4SSCR0 = SSCR0_DataSize(16) + SSCR0_TI + SSCR0_SerClkDiv(8);
  262. Ser4SSCR1 = SSCR1_SClkIactL + SSCR1_SClk1P + SSCR1_ExtClk;
  263. Ser4SSCR0 |= SSCR0_SSE;
  264. local_irq_restore(flags);
  265. /* Enable the audio power */
  266. #ifdef CONFIG_H3600_HAL
  267. h3600_audio_power(AUDIO_RATE_DEFAULT);
  268. #else
  269. clr_sa11xx_uda1341_egpio(IPAQ_EGPIO_CODEC_NRESET);
  270. set_sa11xx_uda1341_egpio(IPAQ_EGPIO_AUDIO_ON);
  271. set_sa11xx_uda1341_egpio(IPAQ_EGPIO_QMUTE);
  272. #endif
  273. /* Wait for the UDA1341 to wake up */
  274. mdelay(1); //FIXME - was removed by Perex - Why?
  275. /* Initialize the UDA1341 internal state */
  276. l3_open(sa11xx_uda1341->uda1341);
  277. /* external clock configuration (after l3_open - regs must be initialized */
  278. sa11xx_uda1341_set_samplerate(sa11xx_uda1341, sa11xx_uda1341->samplerate);
  279. /* Wait for the UDA1341 to wake up */
  280. set_sa11xx_uda1341_egpio(IPAQ_EGPIO_CODEC_NRESET);
  281. mdelay(1);
  282. /* make the left and right channels unswapped (flip the WS latch) */
  283. Ser4SSDR = 0;
  284. #ifdef CONFIG_H3600_HAL
  285. h3600_audio_mute(0);
  286. #else
  287. clr_sa11xx_uda1341_egpio(IPAQ_EGPIO_QMUTE);
  288. #endif
  289. }
  290. static void sa11xx_uda1341_audio_shutdown(sa11xx_uda1341_t *sa11xx_uda1341)
  291. {
  292. /* mute on */
  293. #ifdef CONFIG_H3600_HAL
  294. h3600_audio_mute(1);
  295. #else
  296. set_sa11xx_uda1341_egpio(IPAQ_EGPIO_QMUTE);
  297. #endif
  298. /* disable the audio power and all signals leading to the audio chip */
  299. l3_close(sa11xx_uda1341->uda1341);
  300. Ser4SSCR0 = 0;
  301. clr_sa11xx_uda1341_egpio(IPAQ_EGPIO_CODEC_NRESET);
  302. /* power off and mute off */
  303. /* FIXME - is muting off necesary??? */
  304. #ifdef CONFIG_H3600_HAL
  305. h3600_audio_power(0);
  306. h3600_audio_mute(0);
  307. #else
  308. clr_sa11xx_uda1341_egpio(IPAQ_EGPIO_AUDIO_ON);
  309. clr_sa11xx_uda1341_egpio(IPAQ_EGPIO_QMUTE);
  310. #endif
  311. }
  312. /* }}} */
  313. /* {{{ DMA staff */
  314. /*
  315. * these are the address and sizes used to fill the xmit buffer
  316. * so we can get a clock in record only mode
  317. */
  318. #define FORCE_CLOCK_ADDR (dma_addr_t)FLUSH_BASE_PHYS
  319. #define FORCE_CLOCK_SIZE 4096 // was 2048
  320. // FIXME Why this value exactly - wrote comment
  321. #define DMA_BUF_SIZE 8176 /* <= MAX_DMA_SIZE from asm/arch-sa1100/dma.h */
  322. #ifdef HH_VERSION
  323. static int audio_dma_request(audio_stream_t *s, void (*callback)(void *, int))
  324. {
  325. int ret;
  326. ret = sa1100_request_dma(&s->dmach, s->id, s->dma_dev);
  327. if (ret < 0) {
  328. printk(KERN_ERR "unable to grab audio dma 0x%x\n", s->dma_dev);
  329. return ret;
  330. }
  331. sa1100_dma_set_callback(s->dmach, callback);
  332. return 0;
  333. }
  334. static inline void audio_dma_free(audio_stream_t *s)
  335. {
  336. sa1100_free_dma(s->dmach);
  337. s->dmach = -1;
  338. }
  339. #else
  340. static int audio_dma_request(audio_stream_t *s, void (*callback)(void *))
  341. {
  342. int ret;
  343. ret = sa1100_request_dma(s->dma_dev, s->id, callback, s, &s->dma_regs);
  344. if (ret < 0)
  345. printk(KERN_ERR "unable to grab audio dma 0x%x\n", s->dma_dev);
  346. return ret;
  347. }
  348. static void audio_dma_free(audio_stream_t *s)
  349. {
  350. sa1100_free_dma(s->dma_regs);
  351. s->dma_regs = 0;
  352. }
  353. #endif
  354. static u_int audio_get_dma_pos(audio_stream_t *s)
  355. {
  356. snd_pcm_substream_t * substream = s->stream;
  357. snd_pcm_runtime_t *runtime = substream->runtime;
  358. unsigned int offset;
  359. unsigned long flags;
  360. dma_addr_t addr;
  361. // this must be called w/ interrupts locked out see dma-sa1100.c in the kernel
  362. spin_lock_irqsave(&s->dma_lock, flags);
  363. #ifdef HH_VERSION
  364. sa1100_dma_get_current(s->dmach, NULL, &addr);
  365. #else
  366. addr = sa1100_get_dma_pos((s)->dma_regs);
  367. #endif
  368. offset = addr - runtime->dma_addr;
  369. spin_unlock_irqrestore(&s->dma_lock, flags);
  370. offset = bytes_to_frames(runtime,offset);
  371. if (offset >= runtime->buffer_size)
  372. offset = 0;
  373. return offset;
  374. }
  375. /*
  376. * this stops the dma and clears the dma ptrs
  377. */
  378. static void audio_stop_dma(audio_stream_t *s)
  379. {
  380. unsigned long flags;
  381. spin_lock_irqsave(&s->dma_lock, flags);
  382. s->active = 0;
  383. s->period = 0;
  384. /* this stops the dma channel and clears the buffer ptrs */
  385. #ifdef HH_VERSION
  386. sa1100_dma_flush_all(s->dmach);
  387. #else
  388. sa1100_clear_dma(s->dma_regs);
  389. #endif
  390. spin_unlock_irqrestore(&s->dma_lock, flags);
  391. }
  392. static void audio_process_dma(audio_stream_t *s)
  393. {
  394. snd_pcm_substream_t *substream = s->stream;
  395. snd_pcm_runtime_t *runtime;
  396. unsigned int dma_size;
  397. unsigned int offset;
  398. int ret;
  399. /* we are requested to process synchronization DMA transfer */
  400. if (s->tx_spin) {
  401. snd_assert(s->stream_id == SNDRV_PCM_STREAM_PLAYBACK, return);
  402. /* fill the xmit dma buffers and return */
  403. #ifdef HH_VERSION
  404. sa1100_dma_set_spin(s->dmach, FORCE_CLOCK_ADDR, FORCE_CLOCK_SIZE);
  405. #else
  406. while (1) {
  407. ret = sa1100_start_dma(s->dma_regs, FORCE_CLOCK_ADDR, FORCE_CLOCK_SIZE);
  408. if (ret)
  409. return;
  410. }
  411. #endif
  412. return;
  413. }
  414. /* must be set here - only valid for running streams, not for forced_clock dma fills */
  415. runtime = substream->runtime;
  416. while (s->active && s->periods < runtime->periods) {
  417. dma_size = frames_to_bytes(runtime, runtime->period_size);
  418. if (s->old_offset) {
  419. /* a little trick, we need resume from old position */
  420. offset = frames_to_bytes(runtime, s->old_offset - 1);
  421. s->old_offset = 0;
  422. s->periods = 0;
  423. s->period = offset / dma_size;
  424. offset %= dma_size;
  425. dma_size = dma_size - offset;
  426. if (!dma_size)
  427. continue; /* special case */
  428. } else {
  429. offset = dma_size * s->period;
  430. snd_assert(dma_size <= DMA_BUF_SIZE, );
  431. }
  432. #ifdef HH_VERSION
  433. ret = sa1100_dma_queue_buffer(s->dmach, s, runtime->dma_addr + offset, dma_size);
  434. if (ret)
  435. return; //FIXME
  436. #else
  437. ret = sa1100_start_dma((s)->dma_regs, runtime->dma_addr + offset, dma_size);
  438. if (ret) {
  439. printk(KERN_ERR "audio_process_dma: cannot queue DMA buffer (%i)\n", ret);
  440. return;
  441. }
  442. #endif
  443. s->period++;
  444. s->period %= runtime->periods;
  445. s->periods++;
  446. }
  447. }
  448. #ifdef HH_VERSION
  449. static void audio_dma_callback(void *data, int size)
  450. #else
  451. static void audio_dma_callback(void *data)
  452. #endif
  453. {
  454. audio_stream_t *s = data;
  455. /*
  456. * If we are getting a callback for an active stream then we inform
  457. * the PCM middle layer we've finished a period
  458. */
  459. if (s->active)
  460. snd_pcm_period_elapsed(s->stream);
  461. spin_lock(&s->dma_lock);
  462. if (!s->tx_spin && s->periods > 0)
  463. s->periods--;
  464. audio_process_dma(s);
  465. spin_unlock(&s->dma_lock);
  466. }
  467. /* }}} */
  468. /* {{{ PCM setting */
  469. /* {{{ trigger & timer */
  470. static int snd_sa11xx_uda1341_trigger(snd_pcm_substream_t * substream, int cmd)
  471. {
  472. sa11xx_uda1341_t *chip = snd_pcm_substream_chip(substream);
  473. int stream_id = substream->pstr->stream;
  474. audio_stream_t *s = &chip->s[stream_id];
  475. audio_stream_t *s1 = &chip->s[stream_id ^ 1];
  476. int err = 0;
  477. /* note local interrupts are already disabled in the midlevel code */
  478. spin_lock(&s->dma_lock);
  479. switch (cmd) {
  480. case SNDRV_PCM_TRIGGER_START:
  481. /* now we need to make sure a record only stream has a clock */
  482. if (stream_id == SNDRV_PCM_STREAM_CAPTURE && !s1->active) {
  483. /* we need to force fill the xmit DMA with zeros */
  484. s1->tx_spin = 1;
  485. audio_process_dma(s1);
  486. }
  487. /* this case is when you were recording then you turn on a
  488. * playback stream so we stop (also clears it) the dma first,
  489. * clear the sync flag and then we let it turned on
  490. */
  491. else {
  492. s->tx_spin = 0;
  493. }
  494. /* requested stream startup */
  495. s->active = 1;
  496. audio_process_dma(s);
  497. break;
  498. case SNDRV_PCM_TRIGGER_STOP:
  499. /* requested stream shutdown */
  500. audio_stop_dma(s);
  501. /*
  502. * now we need to make sure a record only stream has a clock
  503. * so if we're stopping a playback with an active capture
  504. * we need to turn the 0 fill dma on for the xmit side
  505. */
  506. if (stream_id == SNDRV_PCM_STREAM_PLAYBACK && s1->active) {
  507. /* we need to force fill the xmit DMA with zeros */
  508. s->tx_spin = 1;
  509. audio_process_dma(s);
  510. }
  511. /*
  512. * we killed a capture only stream, so we should also kill
  513. * the zero fill transmit
  514. */
  515. else {
  516. if (s1->tx_spin) {
  517. s1->tx_spin = 0;
  518. audio_stop_dma(s1);
  519. }
  520. }
  521. break;
  522. case SNDRV_PCM_TRIGGER_SUSPEND:
  523. s->active = 0;
  524. #ifdef HH_VERSION
  525. sa1100_dma_stop(s->dmach);
  526. #else
  527. //FIXME - DMA API
  528. #endif
  529. s->old_offset = audio_get_dma_pos(s) + 1;
  530. #ifdef HH_VERSION
  531. sa1100_dma_flush_all(s->dmach);
  532. #else
  533. //FIXME - DMA API
  534. #endif
  535. s->periods = 0;
  536. break;
  537. case SNDRV_PCM_TRIGGER_RESUME:
  538. s->active = 1;
  539. s->tx_spin = 0;
  540. audio_process_dma(s);
  541. if (stream_id == SNDRV_PCM_STREAM_CAPTURE && !s1->active) {
  542. s1->tx_spin = 1;
  543. audio_process_dma(s1);
  544. }
  545. break;
  546. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  547. #ifdef HH_VERSION
  548. sa1100_dma_stop(s->dmach);
  549. #else
  550. //FIXME - DMA API
  551. #endif
  552. s->active = 0;
  553. if (stream_id == SNDRV_PCM_STREAM_PLAYBACK) {
  554. if (s1->active) {
  555. s->tx_spin = 1;
  556. s->old_offset = audio_get_dma_pos(s) + 1;
  557. #ifdef HH_VERSION
  558. sa1100_dma_flush_all(s->dmach);
  559. #else
  560. //FIXME - DMA API
  561. #endif
  562. audio_process_dma(s);
  563. }
  564. } else {
  565. if (s1->tx_spin) {
  566. s1->tx_spin = 0;
  567. #ifdef HH_VERSION
  568. sa1100_dma_flush_all(s1->dmach);
  569. #else
  570. //FIXME - DMA API
  571. #endif
  572. }
  573. }
  574. break;
  575. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  576. s->active = 1;
  577. if (s->old_offset) {
  578. s->tx_spin = 0;
  579. audio_process_dma(s);
  580. break;
  581. }
  582. if (stream_id == SNDRV_PCM_STREAM_CAPTURE && !s1->active) {
  583. s1->tx_spin = 1;
  584. audio_process_dma(s1);
  585. }
  586. #ifdef HH_VERSION
  587. sa1100_dma_resume(s->dmach);
  588. #else
  589. //FIXME - DMA API
  590. #endif
  591. break;
  592. default:
  593. err = -EINVAL;
  594. break;
  595. }
  596. spin_unlock(&s->dma_lock);
  597. return err;
  598. }
  599. static int snd_sa11xx_uda1341_prepare(snd_pcm_substream_t * substream)
  600. {
  601. sa11xx_uda1341_t *chip = snd_pcm_substream_chip(substream);
  602. snd_pcm_runtime_t *runtime = substream->runtime;
  603. audio_stream_t *s = &chip->s[substream->pstr->stream];
  604. /* set requested samplerate */
  605. sa11xx_uda1341_set_samplerate(chip, runtime->rate);
  606. /* set requestd format when available */
  607. /* set FMT here !!! FIXME */
  608. s->period = 0;
  609. s->periods = 0;
  610. return 0;
  611. }
  612. static snd_pcm_uframes_t snd_sa11xx_uda1341_pointer(snd_pcm_substream_t * substream)
  613. {
  614. sa11xx_uda1341_t *chip = snd_pcm_substream_chip(substream);
  615. return audio_get_dma_pos(&chip->s[substream->pstr->stream]);
  616. }
  617. /* }}} */
  618. static snd_pcm_hardware_t snd_sa11xx_uda1341_capture =
  619. {
  620. .info = (SNDRV_PCM_INFO_INTERLEAVED |
  621. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  622. SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
  623. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
  624. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  625. .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |\
  626. SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |\
  627. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
  628. SNDRV_PCM_RATE_KNOT),
  629. .rate_min = 8000,
  630. .rate_max = 48000,
  631. .channels_min = 2,
  632. .channels_max = 2,
  633. .buffer_bytes_max = 64*1024,
  634. .period_bytes_min = 64,
  635. .period_bytes_max = DMA_BUF_SIZE,
  636. .periods_min = 2,
  637. .periods_max = 255,
  638. .fifo_size = 0,
  639. };
  640. static snd_pcm_hardware_t snd_sa11xx_uda1341_playback =
  641. {
  642. .info = (SNDRV_PCM_INFO_INTERLEAVED |
  643. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  644. SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
  645. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
  646. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  647. .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |\
  648. SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |\
  649. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
  650. SNDRV_PCM_RATE_KNOT),
  651. .rate_min = 8000,
  652. .rate_max = 48000,
  653. .channels_min = 2,
  654. .channels_max = 2,
  655. .buffer_bytes_max = 64*1024,
  656. .period_bytes_min = 64,
  657. .period_bytes_max = DMA_BUF_SIZE,
  658. .periods_min = 2,
  659. .periods_max = 255,
  660. .fifo_size = 0,
  661. };
  662. static int snd_card_sa11xx_uda1341_open(snd_pcm_substream_t * substream)
  663. {
  664. sa11xx_uda1341_t *chip = snd_pcm_substream_chip(substream);
  665. snd_pcm_runtime_t *runtime = substream->runtime;
  666. int stream_id = substream->pstr->stream;
  667. int err;
  668. chip->s[stream_id].stream = substream;
  669. if (stream_id == SNDRV_PCM_STREAM_PLAYBACK)
  670. runtime->hw = snd_sa11xx_uda1341_playback;
  671. else
  672. runtime->hw = snd_sa11xx_uda1341_capture;
  673. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  674. return err;
  675. if ((err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates)) < 0)
  676. return err;
  677. return 0;
  678. }
  679. static int snd_card_sa11xx_uda1341_close(snd_pcm_substream_t * substream)
  680. {
  681. sa11xx_uda1341_t *chip = snd_pcm_substream_chip(substream);
  682. chip->s[substream->pstr->stream].stream = NULL;
  683. return 0;
  684. }
  685. /* {{{ HW params & free */
  686. static int snd_sa11xx_uda1341_hw_params(snd_pcm_substream_t * substream,
  687. snd_pcm_hw_params_t * hw_params)
  688. {
  689. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  690. }
  691. static int snd_sa11xx_uda1341_hw_free(snd_pcm_substream_t * substream)
  692. {
  693. return snd_pcm_lib_free_pages(substream);
  694. }
  695. /* }}} */
  696. static snd_pcm_ops_t snd_card_sa11xx_uda1341_playback_ops = {
  697. .open = snd_card_sa11xx_uda1341_open,
  698. .close = snd_card_sa11xx_uda1341_close,
  699. .ioctl = snd_pcm_lib_ioctl,
  700. .hw_params = snd_sa11xx_uda1341_hw_params,
  701. .hw_free = snd_sa11xx_uda1341_hw_free,
  702. .prepare = snd_sa11xx_uda1341_prepare,
  703. .trigger = snd_sa11xx_uda1341_trigger,
  704. .pointer = snd_sa11xx_uda1341_pointer,
  705. };
  706. static snd_pcm_ops_t snd_card_sa11xx_uda1341_capture_ops = {
  707. .open = snd_card_sa11xx_uda1341_open,
  708. .close = snd_card_sa11xx_uda1341_close,
  709. .ioctl = snd_pcm_lib_ioctl,
  710. .hw_params = snd_sa11xx_uda1341_hw_params,
  711. .hw_free = snd_sa11xx_uda1341_hw_free,
  712. .prepare = snd_sa11xx_uda1341_prepare,
  713. .trigger = snd_sa11xx_uda1341_trigger,
  714. .pointer = snd_sa11xx_uda1341_pointer,
  715. };
  716. static int __init snd_card_sa11xx_uda1341_pcm(sa11xx_uda1341_t *sa11xx_uda1341, int device)
  717. {
  718. snd_pcm_t *pcm;
  719. int err;
  720. if ((err = snd_pcm_new(sa11xx_uda1341->card, "UDA1341 PCM", device, 1, 1, &pcm)) < 0)
  721. return err;
  722. /*
  723. * this sets up our initial buffers and sets the dma_type to isa.
  724. * isa works but I'm not sure why (or if) it's the right choice
  725. * this may be too large, trying it for now
  726. */
  727. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  728. snd_dma_isa_data(),
  729. 64*1024, 64*1024);
  730. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_card_sa11xx_uda1341_playback_ops);
  731. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_sa11xx_uda1341_capture_ops);
  732. pcm->private_data = sa11xx_uda1341;
  733. pcm->info_flags = 0;
  734. strcpy(pcm->name, "UDA1341 PCM");
  735. sa11xx_uda1341_audio_init(sa11xx_uda1341);
  736. /* setup DMA controller */
  737. audio_dma_request(&sa11xx_uda1341->s[SNDRV_PCM_STREAM_PLAYBACK], audio_dma_callback);
  738. audio_dma_request(&sa11xx_uda1341->s[SNDRV_PCM_STREAM_CAPTURE], audio_dma_callback);
  739. sa11xx_uda1341->pcm = pcm;
  740. return 0;
  741. }
  742. /* }}} */
  743. /* {{{ module init & exit */
  744. #ifdef CONFIG_PM
  745. static int snd_sa11xx_uda1341_suspend(snd_card_t *card, pm_message_t state)
  746. {
  747. sa11xx_uda1341_t *chip = card->pm_private_data;
  748. snd_pcm_suspend_all(chip->pcm);
  749. #ifdef HH_VERSION
  750. sa1100_dma_sleep(chip->s[SNDRV_PCM_STREAM_PLAYBACK].dmach);
  751. sa1100_dma_sleep(chip->s[SNDRV_PCM_STREAM_CAPTURE].dmach);
  752. #else
  753. //FIXME
  754. #endif
  755. l3_command(chip->uda1341, CMD_SUSPEND, NULL);
  756. sa11xx_uda1341_audio_shutdown(chip);
  757. return 0;
  758. }
  759. static int snd_sa11xx_uda1341_resume(snd_card_t *card)
  760. {
  761. sa11xx_uda1341_t *chip = card->pm_private_data;
  762. sa11xx_uda1341_audio_init(chip);
  763. l3_command(chip->uda1341, CMD_RESUME, NULL);
  764. #ifdef HH_VERSION
  765. sa1100_dma_wakeup(chip->s[SNDRV_PCM_STREAM_PLAYBACK].dmach);
  766. sa1100_dma_wakeup(chip->s[SNDRV_PCM_STREAM_CAPTURE].dmach);
  767. #else
  768. //FIXME
  769. #endif
  770. return 0;
  771. }
  772. #endif /* COMFIG_PM */
  773. void snd_sa11xx_uda1341_free(snd_card_t *card)
  774. {
  775. sa11xx_uda1341_t *chip = card->private_data;
  776. audio_dma_free(&chip->s[SNDRV_PCM_STREAM_PLAYBACK]);
  777. audio_dma_free(&chip->s[SNDRV_PCM_STREAM_CAPTURE]);
  778. }
  779. static snd_card_t *sa11xx_uda1341_card;
  780. static int __init sa11xx_uda1341_init(void)
  781. {
  782. int err;
  783. snd_card_t *card;
  784. sa11xx_uda1341_t *chip;
  785. if (!machine_is_h3xxx())
  786. return -ENODEV;
  787. /* register the soundcard */
  788. card = snd_card_new(-1, id, THIS_MODULE, sizeof(sa11xx_uda1341_t));
  789. if (card == NULL)
  790. return -ENOMEM;
  791. sa11xx_uda1341 = kzalloc(sizeof(*sa11xx_uda1341), GFP_KERNEL);
  792. if (sa11xx_uda1341 == NULL)
  793. return -ENOMEM;
  794. card->private_free = snd_sa11xx_uda1341_free;
  795. chip = card->private_data;
  796. spin_lock_init(&chip->s[0].dma_lock);
  797. spin_lock_init(&chip->s[1].dma_lock);
  798. chip->card = card;
  799. chip->samplerate = AUDIO_RATE_DEFAULT;
  800. // mixer
  801. if ((err = snd_chip_uda1341_mixer_new(chip->card, &sa11xx_uda1341->uda1341)))
  802. goto nodev;
  803. // PCM
  804. if ((err = snd_card_sa11xx_uda1341_pcm(chip, 0)) < 0)
  805. goto nodev;
  806. snd_card_set_generic_pm_callback(card,
  807. snd_sa11xx_uda1341_suspend, snd_sa11_uda1341_resume,
  808. chip);
  809. strcpy(card->driver, "UDA1341");
  810. strcpy(card->shortname, "H3600 UDA1341TS");
  811. sprintf(card->longname, "Compaq iPAQ H3600 with Philips UDA1341TS");
  812. if ((err = snd_card_set_generic_dev(card)) < 0)
  813. goto nodev;
  814. if ((err = snd_card_register(card)) == 0) {
  815. printk( KERN_INFO "iPAQ audio support initialized\n" );
  816. sa11xx_uda1341_card = card;
  817. return 0;
  818. }
  819. nodev:
  820. snd_card_free(card);
  821. return err;
  822. }
  823. static void __exit sa11xx_uda1341_exit(void)
  824. {
  825. snd_card_free(sa11xx_uda1341_card);
  826. }
  827. module_init(sa11xx_uda1341_init);
  828. module_exit(sa11xx_uda1341_exit);
  829. /* }}} */
  830. /*
  831. * Local variables:
  832. * indent-tabs-mode: t
  833. * End:
  834. */