2 * sound/arm/omap/omap-alsa-tsc2101.c
4 * Alsa codec Driver for TSC2101 chip for OMAP platform boards.
5 * Code obtained from oss omap drivers
7 * Copyright (C) 2004 Texas Instruments, Inc.
8 * Written by Nishanth Menon and Sriram Kannan
10 * Copyright (C) 2006 Instituto Nokia de Tecnologia - INdT - Manaus Brazil
11 * Alsa modularization by Daniel Petrini (d.pensator@gmail.com)
13 * Copyright (C) 2006 Mika Laitio <lamikr@cc.jyu.fi>
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version.
21 #include <linux/delay.h>
22 #include <linux/soundcard.h>
23 #include <linux/platform_device.h>
24 #include <linux/clk.h>
26 #include <asm/arch/mcbsp.h>
28 #include <linux/slab.h>
32 #include <asm/mach-types.h>
33 #include <asm/arch/dma.h>
34 #include <asm/arch/clock.h>
36 #include <asm/hardware/tsc2101.h>
38 #include <asm/arch/omap-alsa.h>
39 #include "omap-alsa-tsc2101.h"
41 static struct clk *tsc2101_mclk = 0;
43 //#define DUMP_TSC2101_AUDIO_REGISTERS
44 #undef DUMP_TSC2101_AUDIO_REGISTERS
47 * Hardware capabilities
51 * DAC USB-mode sampling rates (MCLK = 12 MHz)
52 * The rates and rate_reg_into MUST be in the same order
54 static unsigned int rates[] = {
55 7350, 8000, 8018, 8727,
56 8820, 9600, 11025, 12000,
57 14700, 16000, 22050, 24000,
58 29400, 32000, 44100, 48000,
61 static struct snd_pcm_hw_constraint_list tsc2101_hw_constraints_rates = {
62 .count = ARRAY_SIZE(rates),
67 static const struct tsc2101_samplerate_reg_info
68 rate_reg_info[NUMBER_SAMPLE_RATES_SUPPORTED] = {
95 static struct snd_pcm_hardware tsc2101_snd_omap_alsa_playback = {
96 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
97 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
98 #ifdef CONFIG_MACH_OMAP_H6300
99 .formats = (SNDRV_PCM_FMTBIT_S8),
101 .formats = (SNDRV_PCM_FMTBIT_S16_LE),
103 .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |
104 SNDRV_PCM_RATE_16000 |
105 SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |
106 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
107 SNDRV_PCM_RATE_KNOT),
112 .buffer_bytes_max = 128 * 1024,
113 .period_bytes_min = 32,
114 .period_bytes_max = 8 * 1024,
120 static struct snd_pcm_hardware tsc2101_snd_omap_alsa_capture = {
121 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
122 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
123 .formats = (SNDRV_PCM_FMTBIT_S16_LE),
124 .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |
125 SNDRV_PCM_RATE_16000 |
126 SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |
127 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
128 SNDRV_PCM_RATE_KNOT),
133 .buffer_bytes_max = 128 * 1024,
134 .period_bytes_min = 32,
135 .period_bytes_max = 8 * 1024,
142 * Simplified write for tsc2101 audio registers.
144 inline void tsc2101_audio_write(u8 address, u16 data)
146 omap_tsc2101_write(PAGE2_AUDIO_CODEC_REGISTERS, address, data);
150 * Simplified read for tsc2101 audio registers.
152 inline u16 tsc2101_audio_read(u8 address)
154 return (omap_tsc2101_read(PAGE2_AUDIO_CODEC_REGISTERS, address));
157 #ifdef DUMP_TSC2101_AUDIO_REGISTERS
158 void dump_tsc2101_audio_reg(void) {
159 printk("TSC2101_AUDIO_CTRL_1 = 0x%04x\n", tsc2101_audio_read(TSC2101_AUDIO_CTRL_1));
160 printk("TSC2101_HEADSET_GAIN_CTRL = 0x%04x\n", tsc2101_audio_read(TSC2101_HEADSET_GAIN_CTRL));
161 printk("TSC2101_DAC_GAIN_CTRL = 0x%04x\n", tsc2101_audio_read(TSC2101_DAC_GAIN_CTRL));
162 printk("TSC2101_MIXER_PGA_CTRL = 0x%04x\n", tsc2101_audio_read(TSC2101_MIXER_PGA_CTRL));
163 printk("TSC2101_AUDIO_CTRL_2 = 0x%04x\n", tsc2101_audio_read(TSC2101_AUDIO_CTRL_2));
164 printk("TSC2101_CODEC_POWER_CTRL = 0x%04x\n", tsc2101_audio_read(TSC2101_CODEC_POWER_CTRL));
165 printk("TSC2101_AUDIO_CTRL_3 = 0x%04x\n", tsc2101_audio_read(TSC2101_AUDIO_CTRL_3));
166 printk("TSC2101_LCH_BASS_BOOST_N0 = 0x%04x\n", tsc2101_audio_read(TSC2101_LCH_BASS_BOOST_N0));
167 printk("TSC2101_LCH_BASS_BOOST_N1 = 0x%04x\n", tsc2101_audio_read(TSC2101_LCH_BASS_BOOST_N1));
168 printk("TSC2101_LCH_BASS_BOOST_N2 = 0x%04x\n", tsc2101_audio_read(TSC2101_LCH_BASS_BOOST_N2));
169 printk("TSC2101_LCH_BASS_BOOST_N3 = 0x%04x\n", tsc2101_audio_read(TSC2101_LCH_BASS_BOOST_N3));
170 printk("TSC2101_LCH_BASS_BOOST_N4 = 0x%04x\n", tsc2101_audio_read(TSC2101_LCH_BASS_BOOST_N4));
171 printk("TSC2101_LCH_BASS_BOOST_N5 = 0x%04x\n", tsc2101_audio_read(TSC2101_LCH_BASS_BOOST_N5));
172 printk("TSC2101_LCH_BASS_BOOST_D1 = 0x%04x\n", tsc2101_audio_read(TSC2101_LCH_BASS_BOOST_D1));
173 printk("TSC2101_LCH_BASS_BOOST_D2 = 0x%04x\n", tsc2101_audio_read(TSC2101_LCH_BASS_BOOST_D2));
174 printk("TSC2101_LCH_BASS_BOOST_D4 = 0x%04x\n", tsc2101_audio_read(TSC2101_LCH_BASS_BOOST_D4));
175 printk("TSC2101_LCH_BASS_BOOST_D5 = 0x%04x\n", tsc2101_audio_read(TSC2101_LCH_BASS_BOOST_D5));
177 printk("TSC2101_RCH_BASS_BOOST_N0 = 0x%04x\n", tsc2101_audio_read(TSC2101_RCH_BASS_BOOST_N0));
178 printk("TSC2101_RCH_BASS_BOOST_N1 = 0x%04x\n", tsc2101_audio_read(TSC2101_RCH_BASS_BOOST_N1));
179 printk("TSC2101_RCH_BASS_BOOST_N2 = 0x%04x\n", tsc2101_audio_read(TSC2101_RCH_BASS_BOOST_N2));
180 printk("TSC2101_RCH_BASS_BOOST_N3 = 0x%04x\n", tsc2101_audio_read(TSC2101_RCH_BASS_BOOST_N3));
181 printk("TSC2101_RCH_BASS_BOOST_N4 = 0x%04x\n", tsc2101_audio_read(TSC2101_RCH_BASS_BOOST_N4));
182 printk("TSC2101_RCH_BASS_BOOST_N5 = 0x%04x\n", tsc2101_audio_read(TSC2101_RCH_BASS_BOOST_N5));
183 printk("TSC2101_RCH_BASS_BOOST_D1 = 0x%04x\n", tsc2101_audio_read(TSC2101_RCH_BASS_BOOST_D1));
184 printk("TSC2101_RCH_BASS_BOOST_D2 = 0x%04x\n", tsc2101_audio_read(TSC2101_RCH_BASS_BOOST_D2));
185 printk("TSC2101_RCH_BASS_BOOST_D4 = 0x%04x\n", tsc2101_audio_read(TSC2101_RCH_BASS_BOOST_D4));
186 printk("TSC2101_RCH_BASS_BOOST_D5 = 0x%04x\n", tsc2101_audio_read(TSC2101_RCH_BASS_BOOST_D5));
188 printk("TSC2101_PLL_PROG_1 = 0x%04x\n", tsc2101_audio_read(TSC2101_PLL_PROG_1));
189 printk("TSC2101_PLL_PROG_1 = 0x%04x\n", tsc2101_audio_read(TSC2101_PLL_PROG_2));
190 printk("TSC2101_AUDIO_CTRL_4 = 0x%04x\n", tsc2101_audio_read(TSC2101_AUDIO_CTRL_4));
191 printk("TSC2101_HANDSET_GAIN_CTRL = 0x%04x\n", tsc2101_audio_read(TSC2101_HANDSET_GAIN_CTRL));
192 printk("TSC2101_BUZZER_GAIN_CTRL = 0x%04x\n", tsc2101_audio_read(TSC2101_BUZZER_GAIN_CTRL));
193 printk("TSC2101_AUDIO_CTRL_5 = 0x%04x\n", tsc2101_audio_read(TSC2101_AUDIO_CTRL_5));
194 printk("TSC2101_AUDIO_CTRL_6 = 0x%04x\n", tsc2101_audio_read(TSC2101_AUDIO_CTRL_6));
195 printk("TSC2101_AUDIO_CTRL_7 = 0x%04x\n", tsc2101_audio_read(TSC2101_AUDIO_CTRL_7));
196 printk("TSC2101_GPIO_CTRL = 0x%04x\n", tsc2101_audio_read(TSC2101_GPIO_CTRL));
197 printk("TSC2101_AGC_CTRL = 0x%04x\n", tsc2101_audio_read(TSC2101_AGC_CTRL));
198 printk("TSC2101_POWERDOWN_STS = 0x%04x\n", tsc2101_audio_read(TSC2101_POWERDOWN_STS));
199 printk("TSC2101_MIC_AGC_CONTROL = 0x%04x\n", tsc2101_audio_read(TSC2101_MIC_AGC_CONTROL));
200 printk("TSC2101_CELL_AGC_CONTROL = 0x%04x\n", tsc2101_audio_read(TSC2101_CELL_AGC_CONTROL));
205 * ALSA operations according to board file
209 * Sample rate changing
211 void tsc2101_set_samplerate(long sample_rate)
218 /* wait for any frame to complete */
222 sample_rate = sample_rate;
223 /* Search for the right sample rate */
224 while ((rate_reg_info[count].sample_rate != sample_rate) &&
225 (count < NUMBER_SAMPLE_RATES_SUPPORTED)) {
228 if (count == NUMBER_SAMPLE_RATES_SUPPORTED) {
229 printk(KERN_ERR "Invalid Sample Rate %d requested\n",
235 data = tsc2101_audio_read(TSC2101_AUDIO_CTRL_1);
236 /* Clear prev settings */
237 data &= ~(AC1_DACFS(0x07) | AC1_ADCFS(0x07));
238 data |= AC1_DACFS(rate_reg_info[count].divisor) |
239 AC1_ADCFS(rate_reg_info[count].divisor);
240 tsc2101_audio_write(TSC2101_AUDIO_CTRL_1, data);
243 data = tsc2101_audio_read(TSC2101_AUDIO_CTRL_3);
244 /*Clear prev settings */
245 data &= ~(AC3_REFFS | AC3_SLVMS);
246 data |= (rate_reg_info[count].fs_44kHz) ? AC3_REFFS : 0;
249 #endif /* #ifdef TSC_MASTER */
250 tsc2101_audio_write(TSC2101_AUDIO_CTRL_3, data);
252 /* Program the PLLs. This code assumes that the 12 Mhz MCLK is in use.
253 * If MCLK rate is something else, these values must be changed.
254 * See the tsc2101 specification for the details.
256 if (rate_reg_info[count].fs_44kHz) {
257 /* samplerate = (44.1kHZ / x), where x is int. */
258 tsc2101_audio_write(TSC2101_PLL_PROG_1, PLL1_PLLSEL |
259 PLL1_PVAL(1) | PLL1_I_VAL(7)); /* PVAL 1; I_VAL 7 */
260 tsc2101_audio_write(TSC2101_PLL_PROG_2, PLL2_D_VAL(0x1490)); /* D_VAL 5264 */
262 /* samplerate = (48.kHZ / x), where x is int. */
263 tsc2101_audio_write(TSC2101_PLL_PROG_1, PLL1_PLLSEL |
264 PLL1_PVAL(1) | PLL1_I_VAL(8)); /* PVAL 1; I_VAL 8 */
265 tsc2101_audio_write(TSC2101_PLL_PROG_2, PLL2_D_VAL(0x780)); /* D_VAL 1920 */
268 /* Set the sample rate */
270 clkgdv = CODEC_CLOCK / (sample_rate * (DEFAULT_BITPERSAMPLE * 2 - 1));
272 srgr1 = (FWID(DEFAULT_BITPERSAMPLE - 1) | CLKGDV(clkgdv));
277 srgr2 = (CLKSM | FSGM | FPER(DEFAULT_BITPERSAMPLE * 2 - 1));
279 srgr1 = (FWID(DEFAULT_BITPERSAMPLE - 1) | CLKGDV(clkgdv));
280 srgr2 = ((GSYNC | CLKSP | FSGM | FPER(DEFAULT_BITPERSAMPLE * 2 - 1)));
282 #endif /* end of #ifdef TSC_MASTER */
283 OMAP_MCBSP_WRITE(OMAP1610_MCBSP1_BASE, SRGR2, srgr2);
284 OMAP_MCBSP_WRITE(OMAP1610_MCBSP1_BASE, SRGR1, srgr1);
287 void tsc2101_configure(void)
292 * Omap MCBSP clock and Power Management configuration
294 * Here we have some functions that allows clock to be enabled and
295 * disabled only when needed. Besides doing clock configuration
296 * it allows turn on/turn off audio when necessary.
300 * Do clock framework mclk search
302 void tsc2101_clock_setup(void)
304 tsc2101_mclk = clk_get(0, "mclk");
308 * Do some sanity check, set clock rate, starts it and turn codec audio on
310 int tsc2101_clock_on(void)
316 curUseCount = clk_get_usecount(tsc2101_mclk);
317 DPRINTK("clock use count = %d\n", curUseCount);
318 if (curUseCount > 0) {
319 // MCLK is already in use
321 "MCLK already in use at %d Hz. We change it to %d Hz\n",
322 (uint) clk_get_rate(tsc2101_mclk),
325 curRate = (uint)clk_get_rate(tsc2101_mclk);
326 if (curRate != CODEC_CLOCK) {
327 err = clk_set_rate(tsc2101_mclk, CODEC_CLOCK);
330 "Cannot set MCLK clock rate for TSC2101 CODEC, error code = %d\n", err);
334 err = clk_enable(tsc2101_mclk);
335 curRate = (uint)clk_get_rate(tsc2101_mclk);
336 curUseCount = clk_get_usecount(tsc2101_mclk);
337 DPRINTK("MCLK = %d [%d], usecount = %d, clk_enable retval = %d\n",
343 // Now turn the audio on
344 omap_tsc2101_write(PAGE2_AUDIO_CODEC_REGISTERS,
345 TSC2101_CODEC_POWER_CTRL,
351 * Do some sanity check, turn clock off and then turn codec audio off
353 int tsc2101_clock_off(void)
358 curUseCount = clk_get_usecount(tsc2101_mclk);
359 DPRINTK("clock use count = %d\n", curUseCount);
360 if (curUseCount > 0) {
361 curRate = clk_get_rate(tsc2101_mclk);
362 DPRINTK("clock rate = %d\n", curRate);
363 if (curRate != CODEC_CLOCK) {
365 "MCLK for audio should be %d Hz. But is %d Hz\n",
366 (uint) clk_get_rate(tsc2101_mclk),
369 clk_disable(tsc2101_mclk);
370 DPRINTK("clock disabled\n");
372 tsc2101_audio_write(TSC2101_CODEC_POWER_CTRL,
373 ~(CPC_SP1PWDN | CPC_SP2PWDN | CPC_BASSBC));
374 DPRINTK("audio codec off\n");
378 int tsc2101_get_default_samplerate(void)
380 return DEFAULT_SAMPLE_RATE;
383 static int __devinit snd_omap_alsa_tsc2101_probe(struct platform_device *pdev)
386 struct omap_alsa_codec_config *codec_cfg;
388 codec_cfg = pdev->dev.platform_data;
389 if (codec_cfg != NULL) {
390 codec_cfg->hw_constraints_rates = &tsc2101_hw_constraints_rates;
391 codec_cfg->snd_omap_alsa_playback = &tsc2101_snd_omap_alsa_playback;
392 codec_cfg->snd_omap_alsa_capture = &tsc2101_snd_omap_alsa_capture;
393 codec_cfg->codec_configure_dev = tsc2101_configure;
394 codec_cfg->codec_set_samplerate = tsc2101_set_samplerate;
395 codec_cfg->codec_clock_setup = tsc2101_clock_setup;
396 codec_cfg->codec_clock_on = tsc2101_clock_on;
397 codec_cfg->codec_clock_off = tsc2101_clock_off;
398 codec_cfg->get_default_samplerate = tsc2101_get_default_samplerate;
399 ret = snd_omap_alsa_post_probe(pdev, codec_cfg);
406 static struct platform_driver omap_alsa_driver = {
407 .probe = snd_omap_alsa_tsc2101_probe,
408 .remove = snd_omap_alsa_remove,
409 .suspend = snd_omap_alsa_suspend,
410 .resume = snd_omap_alsa_resume,
412 .name = "omap_alsa_mcbsp",
416 static int __init omap_alsa_tsc2101_init(void)
419 #ifdef DUMP_TSC2101_AUDIO_REGISTERS
420 printk("omap_alsa_tsc2101_init()\n");
421 dump_tsc2101_audio_reg();
423 return platform_driver_register(&omap_alsa_driver);
426 static void __exit omap_alsa_tsc2101_exit(void)
429 #ifdef DUMP_TSC2101_AUDIO_REGISTERS
430 printk("omap_alsa_tsc2101_exit()\n");
431 dump_tsc2101_audio_reg();
433 platform_driver_unregister(&omap_alsa_driver);
436 module_init(omap_alsa_tsc2101_init);
437 module_exit(omap_alsa_tsc2101_exit);