assabet-uda1341.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:11k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /*
  2.  * Glue audio driver for the SA1110 Assabet board & Philips UDA1341 codec.
  3.  *
  4.  * Copyright (c) 2000 Nicolas Pitre <nico@cam.org>
  5.  *
  6.  * This program is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU General Public License.
  8.  *
  9.  * This is the machine specific part of the Assabet/UDA1341 support.
  10.  * This driver makes use of the UDA1341 and the sa1100-audio modules.
  11.  *
  12.  * History:
  13.  *
  14.  * 2000-05-21 Nicolas Pitre Initial release.
  15.  *
  16.  * 2001-06-03 Nicolas Pitre Made this file a separate module, based on
  17.  *  the former sa1100-uda1341.c driver.
  18.  *
  19.  * 2001-07-17 Nicolas Pitre Supports 44100Hz and 22050Hz samplerate now.
  20.  *
  21.  * 2001-08-03 Russell King Fix left/right channel swap.
  22.  * Attempt to reduce power consumption when idle.
  23.  *
  24.  * 2001-09-23 Russell King Remove old L3 bus driver.
  25.  *
  26.  * Please note that fiddling too much with MDREFR results in oopses, so we don't
  27.  * touch MDREFR unnecessarily, which means we don't touch it on close.
  28.  */
  29. #include <linux/module.h>
  30. #include <linux/init.h>
  31. #include <linux/types.h>
  32. #include <linux/fs.h>
  33. #include <linux/delay.h>
  34. #include <linux/pm.h>
  35. #include <linux/errno.h>
  36. #include <linux/sound.h>
  37. #include <linux/soundcard.h>
  38. #include <linux/cpufreq.h>
  39. #include <linux/l3/l3.h>
  40. #include <linux/l3/uda1341.h>
  41. #include <asm/semaphore.h>
  42. #include <asm/uaccess.h>
  43. #include <asm/hardware.h>
  44. #include <asm/dma.h>
  45. #include <asm/arch/assabet.h>
  46. #include "sa1100-audio.h"
  47. /*
  48.  * Define this to fix the power drain on early Assabets
  49.  */
  50. #define FIX_POWER_DRAIN
  51. /*
  52.  * Debugging?
  53.  */
  54. #undef DEBUG
  55. #ifdef DEBUG
  56. #define DPRINTK( x... )  printk( ##x )
  57. #else
  58. #define DPRINTK( x... )
  59. #endif
  60. #define AUDIO_RATE_DEFAULT 44100
  61. /*
  62.  * Mixer (UDA1341) interface
  63.  */
  64. static struct l3_client uda1341;
  65. static int
  66. mixer_ioctl(struct inode *inode, struct file *file, uint cmd, ulong arg)
  67. {
  68. /*
  69.  * We only accept mixer (type 'M') ioctls.
  70.  */
  71. if (_IOC_TYPE(cmd) != 'M')
  72. return -EINVAL;
  73. return l3_command(&uda1341, cmd, (void *)arg);
  74. }
  75. static struct file_operations assabet_mixer_fops = {
  76. ioctl: mixer_ioctl,
  77. owner: THIS_MODULE
  78. };
  79. /*
  80.  * Audio interface
  81.  */
  82. static long audio_samplerate = AUDIO_RATE_DEFAULT;
  83. /*
  84.  * FIXME: what about SFRM going high when SSP is disabled?
  85.  */
  86. static void assabet_set_samplerate(long val)
  87. {
  88. struct uda1341_cfg cfg;
  89. u_int clk_ref, clk_div;
  90. /* We don't want to mess with clocks when frames are in flight */
  91. Ser4SSCR0 &= ~SSCR0_SSE;
  92. /* wait for any frame to complete */
  93. udelay(125);
  94. /*
  95.  * Our clock source is derived from the CPLD on which we don't have
  96.  * much control unfortunately.  This was intended for a fixed 48000 Hz
  97.  * samplerate assuming a core clock of 221.2 MHz.  The CPLD appears
  98.  * to divide the memory clock so there is a ratio of 4608 between
  99.  * the core clock and the resulting samplerate (obtained by
  100.  * measurements, the CPLD equations should confirm that).
  101.  *
  102.  * Still we can play with the SA1110's clock divisor for the SSP port
  103.  * to get half the samplerate as well.
  104.  *
  105.  * Apparently the clock sent to the SA1110 for the SSP port is further
  106.  * more divided from the clock sent to the UDA1341 (some people tried
  107.  * to be too clever in their design, or simply failed to read the
  108.  * SA1110 manual).  If it was the same clock we would have been able
  109.  * to support a third samplerate with the UDA1341's 384FS mode.
  110.  *
  111.  * At least it would have been a minimum acceptable solution to be
  112.  * able to set the CPLD divisor by software.  The iPAQ design is
  113.  * certainly a better example to follow for a new design.
  114.  */
  115. clk_ref = cpufreq_get(0) * 1000 / 4608;
  116. if (val > clk_ref*4/7) {
  117. audio_samplerate = clk_ref;
  118. cfg.fs = 256;
  119. clk_div = SSCR0_SerClkDiv(2);
  120. } else {
  121. audio_samplerate = clk_ref/2;
  122. cfg.fs = 512;
  123. clk_div = SSCR0_SerClkDiv(4);
  124. }
  125. cfg.format = FMT_LSB16;
  126. l3_command(&uda1341, L3_UDA1341_CONFIGURE, &cfg);
  127. Ser4SSCR0 = (Ser4SSCR0 & ~0xff00) + clk_div + SSCR0_SSE;
  128. }
  129. /*
  130.  * Initialise the Assabet audio driver.
  131.  *
  132.  * Note that we have to be careful with the order that we do things here;
  133.  * there is a D-type flip flop which is clocked from the SFRM line which
  134.  * indicates whether the same is for the left or right channel to the
  135.  * UDA1341.
  136.  *
  137.  * When you disable the SSP (by clearing SSCR0_SSE) it appears that the
  138.  * SFRM signal can float high.  When you re-enable the SSP, you clock the
  139.  * flip flop once, and end up swapping the left and right channels.
  140.  *
  141.  * The ASSABET_BCR_CODEC_RST line will force this flip flop into a known
  142.  * state, but this line resets other devices as well!
  143.  *
  144.  * In addition to the above, it appears that powering down the UDA1341 on
  145.  * early Assabets leaves the UDA_WS actively driving a logic '1' into the
  146.  * chip, wasting power!  (you can tell this by D11 being half-on).  We
  147.  * attempt to correct this by kicking the flip flop on init/open/close.
  148.  * We should probably do this on PM resume as well.
  149.  *
  150.  * (Note the ordering of ASSABET_BCR_AUDIO_ON, SFRM and ASSABET_BCR_CODEC_RST
  151.  * is important).
  152.  */
  153. static void assabet_audio_init(void *dummy)
  154. {
  155. unsigned long flags;
  156. unsigned int mdrefr;
  157. local_irq_save(flags);
  158. /*
  159.  * Enable the power for the UDA1341 before driving any signals.
  160.  * We leave the audio amp (LM4880) disabled for now.
  161.  */
  162. ASSABET_BCR_set(ASSABET_BCR_AUDIO_ON);
  163. #ifdef FIX_POWER_DRAIN
  164. GPSR = GPIO_SSP_SFRM;
  165. GPCR = GPIO_SSP_SFRM;
  166. #endif
  167. ASSABET_BCR_set(ASSABET_BCR_CODEC_RST);
  168. ASSABET_BCR_clear(ASSABET_BCR_STEREO_LB);
  169. /*
  170.  * Setup the SSP uart.
  171.  */
  172. PPAR |= PPAR_SPR;
  173. Ser4SSCR0 = SSCR0_DataSize(16) + SSCR0_TI + SSCR0_SerClkDiv(2);
  174. Ser4SSCR1 = SSCR1_SClkIactL + SSCR1_SClk1P + SSCR1_ExtClk;
  175. GAFR |= GPIO_SSP_TXD | GPIO_SSP_RXD | GPIO_SSP_SCLK | GPIO_SSP_CLK;
  176. GPDR |= GPIO_SSP_TXD | GPIO_SSP_SCLK | GPIO_SSP_SFRM;
  177. GPDR &= ~(GPIO_SSP_RXD | GPIO_SSP_CLK);
  178. Ser4SSCR0 |= SSCR0_SSE;
  179. /*
  180.  * Only give SFRM to the SSP after it has been enabled.
  181.  */
  182. GAFR |= GPIO_SSP_SFRM;
  183. /*
  184.  * The assabet board uses the SDRAM clock as the source clock for
  185.  * audio. This is supplied to the SA11x0 from the CPLD on pin 19.
  186.  * At 206MHz we need to run the audio clock (SDRAM bank 2)
  187.  * at half speed. This clock will scale with core frequency so
  188.  * the audio sample rate will also scale. The CPLD on Assabet
  189.  * will need to be programmed to match the core frequency.
  190.  */
  191. mdrefr = MDREFR;
  192. if ((mdrefr & (MDREFR_K2DB2 | MDREFR_K2RUN | MDREFR_EAPD |
  193.        MDREFR_KAPD)) != (MDREFR_K2DB2 | MDREFR_K2RUN)) {
  194. mdrefr |= MDREFR_K2DB2 | MDREFR_K2RUN;
  195. mdrefr &= ~(MDREFR_EAPD | MDREFR_KAPD);
  196. MDREFR = mdrefr;
  197. (void) MDREFR;
  198. }
  199. local_irq_restore(flags);
  200. /* Wait for the UDA1341 to wake up */
  201. mdelay(1);
  202. l3_open(&uda1341);
  203. assabet_set_samplerate(audio_samplerate);
  204. /* Enable the audio power */
  205. ASSABET_BCR_clear(ASSABET_BCR_QMUTE | ASSABET_BCR_SPK_OFF);
  206. }
  207. /*
  208.  * Shutdown the Assabet audio driver.
  209.  *
  210.  * We have to be careful about the SFRM line here for the same reasons
  211.  * described in the initialisation comments above.  This basically means
  212.  * that we must hand the SSP pins back to the GPIO module before disabling
  213.  * the SSP.
  214.  *
  215.  * In addition, to reduce power drain, we toggle the SFRM line once so
  216.  * that the UDA_WS line is at logic 0.
  217.  *
  218.  * We can't clear ASSABET_BCR_CODEC_RST without knowing if the UCB1300 or
  219.  * ADV7171 driver is still active.  If it is, then we still need to play
  220.  * games, so we might as well leave ASSABET_BCR_CODEC_RST set.
  221.  */
  222. static void assabet_audio_shutdown(void *dummy)
  223. {
  224. ASSABET_BCR_set(ASSABET_BCR_STEREO_LB | ASSABET_BCR_QMUTE |
  225. ASSABET_BCR_SPK_OFF);
  226. l3_close(&uda1341);
  227. GAFR &= ~(GPIO_SSP_TXD | GPIO_SSP_RXD | GPIO_SSP_SCLK | GPIO_SSP_SFRM);
  228. Ser4SSCR0 = 0;
  229. #ifdef FIX_POWER_DRAIN
  230. GPSR = GPIO_SSP_SFRM;
  231. GPCR = GPIO_SSP_SFRM;
  232. #endif
  233. /* disable the audio power */
  234. ASSABET_BCR_clear(ASSABET_BCR_AUDIO_ON);
  235. }
  236. static int assabet_audio_ioctl( struct inode *inode, struct file *file,
  237. uint cmd, ulong arg)
  238. {
  239. long val;
  240. int ret = 0;
  241. /*
  242.  * These are platform dependent ioctls which are not handled by the
  243.  * generic sa1100-audio module.
  244.  */
  245. switch (cmd) {
  246. case SNDCTL_DSP_STEREO:
  247. ret = get_user(val, (int *) arg);
  248. if (ret)
  249. return ret;
  250. /* the UDA1341 is stereo only */
  251. ret = (val == 0) ? -EINVAL : 1;
  252. return put_user(ret, (int *) arg);
  253. case SNDCTL_DSP_CHANNELS:
  254. case SOUND_PCM_READ_CHANNELS:
  255. /* the UDA1341 is stereo only */
  256. return put_user(2, (long *) arg);
  257. case SNDCTL_DSP_SPEED:
  258. ret = get_user(val, (long *) arg);
  259. if (ret) break;
  260. assabet_set_samplerate(val);
  261. /* fall through */
  262. case SOUND_PCM_READ_RATE:
  263. return put_user(audio_samplerate, (long *) arg);
  264. case SNDCTL_DSP_SETFMT:
  265. case SNDCTL_DSP_GETFMTS:
  266. /* we can do signed 16-bit only */
  267. return put_user(AFMT_S16_LE, (long *) arg);
  268. default:
  269. /* Maybe this is meant for the mixer (As per OSS Docs) */
  270. return mixer_ioctl(inode, file, cmd, arg);
  271. }
  272. return ret;
  273. }
  274. static audio_stream_t output_stream, input_stream;
  275. static audio_state_t audio_state = {
  276. output_stream: &output_stream,
  277. output_dma: DMA_Ser4SSPWr,
  278. output_id: "Assabet UDA1341 out",
  279. input_stream: &input_stream,
  280. input_dma: DMA_Ser4SSPRd,
  281. input_id: "Assabet UDA1341 in",
  282. need_tx_for_rx: 1,
  283. hw_init: assabet_audio_init,
  284. hw_shutdown: assabet_audio_shutdown,
  285. client_ioctl: assabet_audio_ioctl,
  286. sem: __MUTEX_INITIALIZER(audio_state.sem),
  287. };
  288. static int assabet_audio_open(struct inode *inode, struct file *file)
  289. {
  290. return sa1100_audio_attach(inode, file, &audio_state);
  291. }
  292. /*
  293.  * Missing fields of this structure will be patched with the call
  294.  * to sa1100_audio_attach().
  295.  */
  296. static struct file_operations assabet_audio_fops = {
  297. open: assabet_audio_open,
  298. owner: THIS_MODULE
  299. };
  300. static int audio_dev_id, mixer_dev_id;
  301. static int __init assabet_uda1341_init(void)
  302. {
  303. int ret;
  304. if (!machine_is_assabet())
  305. return -ENODEV;
  306. ret = l3_attach_client(&uda1341, "l3-bit-sa1100-gpio", "uda1341");
  307. if (ret)
  308. goto out;
  309. /* register devices */
  310. audio_dev_id = register_sound_dsp(&assabet_audio_fops, -1);
  311. mixer_dev_id = register_sound_mixer(&assabet_mixer_fops, -1);
  312. #ifdef FIX_POWER_DRAIN
  313. {
  314. unsigned long flags;
  315. local_irq_save(flags);
  316. ASSABET_BCR_set(ASSABET_BCR_CODEC_RST);
  317. GPSR = GPIO_SSP_SFRM;
  318. GPDR |= GPIO_SSP_SFRM;
  319. GPCR = GPIO_SSP_SFRM;
  320. local_irq_restore(flags);
  321. }
  322. #endif
  323. printk(KERN_INFO "Sound: Assabet UDA1341: dsp id %d mixer id %dn",
  324. audio_dev_id, mixer_dev_id);
  325. return 0;
  326. release_l3:
  327. l3_detach_client(&uda1341);
  328. out:
  329. return ret;
  330. }
  331. static void __exit assabet_uda1341_exit(void)
  332. {
  333. unregister_sound_dsp(audio_dev_id);
  334. unregister_sound_mixer(mixer_dev_id);
  335. l3_detach_client(&uda1341);
  336. }
  337. module_init(assabet_uda1341_init);
  338. module_exit(assabet_uda1341_exit);
  339. MODULE_AUTHOR("Nicolas Pitre");
  340. MODULE_DESCRIPTION("Glue audio driver for the SA1110 Assabet board & Philips UDA1341 codec.");
  341. EXPORT_NO_SYMBOLS;