board-n800-audio.c 8.04 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
/*
 * linux/arch/arm/mach-omap2/board-n800-audio.c
 *
 * Copyright (C) 2006 Nokia Corporation
 * Contact: Juha Yrjola
 *          Jarkko Nikula <jarkko.nikula@nokia.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include <linux/err.h>
#include <linux/clk.h>
#include <linux/platform_device.h>
#include <linux/spi/tsc2301.h>

#include <asm/io.h>
#include <asm/arch/eac.h>

#include "../plat-omap/dsp/dsp_common.h"

#if defined(CONFIG_SPI_TSC2301_AUDIO) && defined(CONFIG_SND_OMAP24XX_EAC)
#define AUDIO_ENABLED

static struct clk *sys_clkout2;
static struct clk *func96m_clk;
static struct device *eac_device;
static struct device *tsc2301_device;

static int enable_audio;
static int audio_ok;
static spinlock_t audio_lock;

/*
 * Leaving EAC and sys_clkout2 pins multiplexed to those subsystems results
 * in about 2 mA extra current leak when audios are powered down. The
 * workaround is to multiplex them to protected mode (with pull-ups enabled)
 * whenever audio is not being used.
 */
static int eac_mux_disabled = 0;
static int clkout2_mux_disabled = 0;
static u32 saved_mux[2];

static void n800_enable_eac_mux(void)
{
	if (!eac_mux_disabled)
		return;
	__raw_writel(saved_mux[1], IO_ADDRESS(0x48000124));
	eac_mux_disabled = 0;
}

static void n800_disable_eac_mux(void)
{
	if (eac_mux_disabled) {
		WARN_ON(eac_mux_disabled);
		return;
	}
	saved_mux[1] = __raw_readl(IO_ADDRESS(0x48000124));
	__raw_writel(0x1f1f1f1f, IO_ADDRESS(0x48000124));
	eac_mux_disabled = 1;
}

static void n800_enable_clkout2_mux(void)
{
	if (!clkout2_mux_disabled)
		return;
	__raw_writel(saved_mux[0], IO_ADDRESS(0x480000e8));
	clkout2_mux_disabled = 0;
}

static void n800_disable_clkout2_mux(void)
{
	u32 l;

	if (clkout2_mux_disabled) {
		WARN_ON(clkout2_mux_disabled);
		return;
	}
	saved_mux[0] = __raw_readl(IO_ADDRESS(0x480000e8));
	l = saved_mux[0] & ~0xff;
	l |= 0x1f;
	__raw_writel(l, IO_ADDRESS(0x480000e8));
	clkout2_mux_disabled = 1;
}

static int n800_eac_enable_ext_clocks(struct device *dev)
{
	BUG_ON(tsc2301_device == NULL);
	n800_enable_eac_mux();
	tsc2301_mixer_enable_mclk(tsc2301_device);

	return 0;
}

static void n800_eac_disable_ext_clocks(struct device *dev)
{
	BUG_ON(tsc2301_device == NULL);
	tsc2301_mixer_disable_mclk(tsc2301_device);
	n800_disable_eac_mux();
}

static int n800_audio_set_power(void *pdata, int dac, int adc)
{
	BUG_ON(pdata != tsc2301_device);
	tsc2301_mixer_set_power(tsc2301_device, dac, adc);

	return 0;
}

static int n800_audio_register_controls(void *pdata, struct snd_card *card)
{
	BUG_ON(pdata != tsc2301_device);
	return tsc2301_mixer_register_controls(tsc2301_device, card);
}

static struct eac_codec n800_eac_codec = {
	.mclk_src = EAC_MCLK_EXT_2x12288000,
	.codec_mode = EAC_CODEC_I2S,
	.codec_conf.i2s.polarity_changed_mode = 0,
	.codec_conf.i2s.sync_delay_enable = 0,
	.default_rate = 48000,
	.set_power = n800_audio_set_power,
	.register_controls = n800_audio_register_controls,
	.short_name = "TSC2301",
};

static int n800_register_codec(void)
{
	int r, do_enable = 0;
	unsigned long flags;

	n800_eac_codec.private_data = tsc2301_device;
	r = eac_register_codec(eac_device, &n800_eac_codec);
	if (r < 0)
		return r;
	spin_lock_irqsave(&audio_lock, flags);
	audio_ok = 1;
	if (enable_audio)
		do_enable = 1;
	spin_unlock_irqrestore(&audio_lock, flags);
	if (do_enable)
		eac_set_mode(eac_device, 1, 1);
	return 0;
}

static void n800_unregister_codec(void)
{
	audio_ok = 0;
	eac_unregister_codec(eac_device);
	eac_set_mode(eac_device, 0, 0);
}

static int n800_eac_init(struct device *dev)
{
	int r;

	BUG_ON(eac_device != NULL);
	eac_device = dev;
	if (tsc2301_device != NULL) {
		r = n800_register_codec();
		if (r < 0)
			return r;
	}

	return 0;
}

static void n800_eac_cleanup(struct device *dev)
{
	eac_device = NULL;
	if (tsc2301_device != NULL)
		n800_unregister_codec();
}

static int n800_codec_get_clocks(struct device *dev)
{
	sys_clkout2 = clk_get(dev, "sys_clkout2");
	if (IS_ERR(sys_clkout2)) {
		dev_err(dev, "Could not get sys_clkout2\n");
		return -ENODEV;
	}
	/* configure 12 MHz output on SYS_CLKOUT2. Therefore we must use
	 * 96 MHz as its parent in order to get 12 MHz */
	func96m_clk = clk_get(dev, "func_96m_ck");
	if (IS_ERR(func96m_clk)) {
		dev_err(dev, "Could not get func 96M clock\n");
		clk_put(sys_clkout2);
		return -ENODEV;
	}

	clk_set_parent(sys_clkout2, func96m_clk);
	clk_set_rate(sys_clkout2, 12000000);

	return 0;
}

static void n800_codec_put_clocks(struct device *dev)
{
	clk_put(func96m_clk);
	clk_put(sys_clkout2);
}

static int n800_codec_enable_clock(struct device *dev)
{
	n800_enable_clkout2_mux();
	return clk_enable(sys_clkout2);
}

static void n800_codec_disable_clock(struct device *dev)
{
	clk_disable(sys_clkout2);
	n800_disable_clkout2_mux();
}

static int n800_codec_init(struct device *dev)
{
	int r;

	BUG_ON(tsc2301_device != NULL);
	tsc2301_device = dev;
	if ((r = n800_codec_get_clocks(dev)) < 0)
		return r;
	if (eac_device != NULL) {
		r = n800_register_codec();
		if (r < 0) {
			n800_codec_put_clocks(dev);
			return r;
		}
	}
	return 0;
}

static void n800_codec_cleanup(struct device *dev)
{
	tsc2301_device = NULL;
	if (eac_device != NULL)
		n800_unregister_codec();
	n800_codec_put_clocks(dev);
}

static struct eac_platform_data n800_eac_data = {
	.init = n800_eac_init,
	.cleanup = n800_eac_cleanup,
	.enable_ext_clocks = n800_eac_enable_ext_clocks,
	.disable_ext_clocks = n800_eac_disable_ext_clocks,
};

static const struct tsc2301_mixer_gpio n800_mixer_gpios[] = {
	{
		.name			= "Headset Amplifier",
		.gpio			= 1,
		.deactivate_on_pd	= 1,
	}, {
		.name			= "Speaker Amplifier",
		.gpio			= 2,
		.def_enable		= 1,
		.deactivate_on_pd	= 1,
	}, {
		.name			= "Headset Mic Select",
		.gpio			= 3,
	}
};

static struct platform_device retu_headset_device = {
	.name		= "retu-headset",
	.id		= -1,
	.dev		= {
		.release	= NULL,
	},
};

void __init n800_audio_init(struct tsc2301_platform_data *tc)
{
	spin_lock_init(&audio_lock);

	if (platform_device_register(&retu_headset_device) < 0)
		return;
	omap_init_eac(&n800_eac_data);

	tc->pll_pdc = 7;
	tc->pll_a = 7;
	tc->pll_n = 9;
	tc->pll_output = 1;
	tc->mclk_ratio = TSC2301_MCLK_256xFS;
	tc->i2s_sample_rate = TSC2301_I2S_SR_48000;
	tc->i2s_format = TSC2301_I2S_FORMAT0;
	tc->power_down_blocks = TSC2301_REG_PD_MISC_MOPD;
	tc->mixer_gpios = n800_mixer_gpios;
	tc->n_mixer_gpios = ARRAY_SIZE(n800_mixer_gpios);
	tc->codec_init = n800_codec_init;
	tc->codec_cleanup = n800_codec_cleanup;
	tc->enable_clock = n800_codec_enable_clock;
	tc->disable_clock = n800_codec_disable_clock;
}

#else

void __init n800_audio_init(void)
{
}

#endif

#ifdef CONFIG_OMAP_DSP

int n800_audio_enable(struct dsp_kfunc_device *kdev, int stage)
{
#ifdef AUDIO_ENABLED
	unsigned long flags;
	int do_enable = 0;

	spin_lock_irqsave(&audio_lock, flags);

	pr_debug("DSP power up request (audio codec %sinitialized)\n",
		 audio_ok ? "" : "not ");

	if (enable_audio)
		goto out;
	enable_audio = 1;
	if (audio_ok)
		do_enable = 1;
out:
	spin_unlock_irqrestore(&audio_lock, flags);
	if (do_enable)
		eac_set_mode(eac_device, 1, 1);
#endif
	return 0;
}

int n800_audio_disable(struct dsp_kfunc_device *kdev, int stage)
{
#ifdef AUDIO_ENABLED
	unsigned long flags;
	int do_disable = 0;

	spin_lock_irqsave(&audio_lock, flags);

	pr_debug("DSP power down request (audio codec %sinitialized)\n",
		audio_ok ? "" : "not ");

	if (!enable_audio)
		goto out;
	enable_audio = 0;
	if (audio_ok)
		do_disable = 1;
out:
	spin_unlock_irqrestore(&audio_lock, flags);
	if (do_disable)
		eac_set_mode(eac_device, 0, 0);
#endif
	return 0;
}

#endif /* CONFIG_OMAP_DSP */