omap3pandora.c 9.7 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
/*
 * omap3pandora.c  --  SoC audio for Pandora Handheld Console
 *
 * Author: Gražvydas Ignotas <notasas@gmail.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/clk.h>
#include <linux/platform_device.h>
#include <linux/gpio.h>
#include <linux/delay.h>
26
#include <linux/regulator/consumer.h>
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/soc.h>
#include <sound/soc-dapm.h>

#include <asm/mach-types.h>

#include "omap-mcbsp.h"
#include "omap-pcm.h"
#include "../codecs/twl4030.h"

#define OMAP3_PANDORA_DAC_POWER_GPIO	118
#define OMAP3_PANDORA_AMP_POWER_GPIO	14

#define PREFIX "ASoC omap3pandora: "

44 45
static struct regulator *omap3pandora_dac_reg;

46 47
static int omap3pandora_cmn_hw_params(struct snd_pcm_substream *substream,
	struct snd_pcm_hw_params *params, unsigned int fmt)
48
{
49 50 51
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
	struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
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
	int ret;

	/* Set codec DAI configuration */
	ret = snd_soc_dai_set_fmt(codec_dai, fmt);
	if (ret < 0) {
		pr_err(PREFIX "can't set codec DAI configuration\n");
		return ret;
	}

	/* Set cpu DAI configuration */
	ret = snd_soc_dai_set_fmt(cpu_dai, fmt);
	if (ret < 0) {
		pr_err(PREFIX "can't set cpu DAI configuration\n");
		return ret;
	}

	/* Set the codec system clock for DAC and ADC */
	ret = snd_soc_dai_set_sysclk(codec_dai, 0, 26000000,
					    SND_SOC_CLOCK_IN);
	if (ret < 0) {
		pr_err(PREFIX "can't set codec system clock\n");
		return ret;
	}

	/* Set McBSP clock to external */
77 78 79
	ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_MCBSP_SYSCLK_CLKS_EXT,
				     256 * params_rate(params),
				     SND_SOC_CLOCK_IN);
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	if (ret < 0) {
		pr_err(PREFIX "can't set cpu system clock\n");
		return ret;
	}

	ret = snd_soc_dai_set_clkdiv(cpu_dai, OMAP_MCBSP_CLKGDV, 8);
	if (ret < 0) {
		pr_err(PREFIX "can't set SRG clock divider\n");
		return ret;
	}

	return 0;
}

static int omap3pandora_out_hw_params(struct snd_pcm_substream *substream,
	struct snd_pcm_hw_params *params)
{
97
	return omap3pandora_cmn_hw_params(substream, params,
98 99 100 101 102 103 104 105
					  SND_SOC_DAIFMT_I2S |
					  SND_SOC_DAIFMT_IB_NF |
					  SND_SOC_DAIFMT_CBS_CFS);
}

static int omap3pandora_in_hw_params(struct snd_pcm_substream *substream,
	struct snd_pcm_hw_params *params)
{
106
	return omap3pandora_cmn_hw_params(substream, params,
107 108 109 110 111
					  SND_SOC_DAIFMT_I2S |
					  SND_SOC_DAIFMT_NB_NF |
					  SND_SOC_DAIFMT_CBS_CFS);
}

112
static int omap3pandora_dac_event(struct snd_soc_dapm_widget *w,
113 114
	struct snd_kcontrol *k, int event)
{
115 116 117 118
	/*
	 * The PCM1773 DAC datasheet requires 1ms delay between switching
	 * VCC power on/off and /PD pin high/low
	 */
119
	if (SND_SOC_DAPM_EVENT_ON(event)) {
120 121
		regulator_enable(omap3pandora_dac_reg);
		mdelay(1);
122 123 124
		gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 1);
	} else {
		gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 0);
125 126
		mdelay(1);
		regulator_disable(omap3pandora_dac_reg);
127 128 129 130 131
	}

	return 0;
}

132 133 134 135 136 137 138 139 140 141 142
static int omap3pandora_hp_event(struct snd_soc_dapm_widget *w,
	struct snd_kcontrol *k, int event)
{
	if (SND_SOC_DAPM_EVENT_ON(event))
		gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 1);
	else
		gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 0);

	return 0;
}

143 144 145 146 147 148 149 150 151
/*
 * Audio paths on Pandora board:
 *
 *  |O| ---> PCM DAC +-> AMP -> Headphone Jack
 *  |M|         A    +--------> Line Out
 *  |A| <~~clk~~+
 *  |P| <--- TWL4030 <--------- Line In and MICs
 */
static const struct snd_soc_dapm_widget omap3pandora_out_dapm_widgets[] = {
152 153 154
	SND_SOC_DAPM_DAC_E("PCM DAC", "HiFi Playback", SND_SOC_NOPM,
			   0, 0, omap3pandora_dac_event,
			   SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
155 156 157 158 159 160 161 162
	SND_SOC_DAPM_PGA_E("Headphone Amplifier", SND_SOC_NOPM,
			   0, 0, NULL, 0, omap3pandora_hp_event,
			   SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
	SND_SOC_DAPM_HP("Headphone Jack", NULL),
	SND_SOC_DAPM_LINE("Line Out", NULL),
};

static const struct snd_soc_dapm_widget omap3pandora_in_dapm_widgets[] = {
163
	SND_SOC_DAPM_MIC("Mic (internal)", NULL),
164 165 166 167 168
	SND_SOC_DAPM_MIC("Mic (external)", NULL),
	SND_SOC_DAPM_LINE("Line In", NULL),
};

static const struct snd_soc_dapm_route omap3pandora_out_map[] = {
169
	{"PCM DAC", NULL, "APLL Enable"},
170 171 172 173 174 175
	{"Headphone Amplifier", NULL, "PCM DAC"},
	{"Line Out", NULL, "PCM DAC"},
	{"Headphone Jack", NULL, "Headphone Amplifier"},
};

static const struct snd_soc_dapm_route omap3pandora_in_map[] = {
176 177 178 179 180 181 182 183
	{"AUXL", NULL, "Line In"},
	{"AUXR", NULL, "Line In"},

	{"MAINMIC", NULL, "Mic Bias 1"},
	{"Mic Bias 1", NULL, "Mic (internal)"},

	{"SUBMIC", NULL, "Mic Bias 2"},
	{"Mic Bias 2", NULL, "Mic (external)"},
184 185 186 187 188 189
};

static int omap3pandora_out_init(struct snd_soc_codec *codec)
{
	int ret;

190 191 192 193 194 195 196 197 198 199
	/* All TWL4030 output pins are floating */
	snd_soc_dapm_nc_pin(codec, "EARPIECE");
	snd_soc_dapm_nc_pin(codec, "PREDRIVEL");
	snd_soc_dapm_nc_pin(codec, "PREDRIVER");
	snd_soc_dapm_nc_pin(codec, "HSOL");
	snd_soc_dapm_nc_pin(codec, "HSOR");
	snd_soc_dapm_nc_pin(codec, "CARKITL");
	snd_soc_dapm_nc_pin(codec, "CARKITR");
	snd_soc_dapm_nc_pin(codec, "HFL");
	snd_soc_dapm_nc_pin(codec, "HFR");
200
	snd_soc_dapm_nc_pin(codec, "VIBRA");
201

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
	ret = snd_soc_dapm_new_controls(codec, omap3pandora_out_dapm_widgets,
				ARRAY_SIZE(omap3pandora_out_dapm_widgets));
	if (ret < 0)
		return ret;

	snd_soc_dapm_add_routes(codec, omap3pandora_out_map,
		ARRAY_SIZE(omap3pandora_out_map));

	return snd_soc_dapm_sync(codec);
}

static int omap3pandora_in_init(struct snd_soc_codec *codec)
{
	int ret;

217 218 219 220 221
	/* Not comnnected */
	snd_soc_dapm_nc_pin(codec, "HSMIC");
	snd_soc_dapm_nc_pin(codec, "CARKITMIC");
	snd_soc_dapm_nc_pin(codec, "DIGIMIC0");
	snd_soc_dapm_nc_pin(codec, "DIGIMIC1");
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
	ret = snd_soc_dapm_new_controls(codec, omap3pandora_in_dapm_widgets,
				ARRAY_SIZE(omap3pandora_in_dapm_widgets));
	if (ret < 0)
		return ret;

	snd_soc_dapm_add_routes(codec, omap3pandora_in_map,
		ARRAY_SIZE(omap3pandora_in_map));

	return snd_soc_dapm_sync(codec);
}

static struct snd_soc_ops omap3pandora_out_ops = {
	.hw_params = omap3pandora_out_hw_params,
};

static struct snd_soc_ops omap3pandora_in_ops = {
	.hw_params = omap3pandora_in_hw_params,
};

/* Digital audio interface glue - connects codec <--> CPU */
static struct snd_soc_dai_link omap3pandora_dai[] = {
	{
		.name = "PCM1773",
		.stream_name = "HiFi Out",
		.cpu_dai = &omap_mcbsp_dai[0],
248
		.codec_dai = &twl4030_dai[TWL4030_DAI_HIFI],
249 250 251 252 253 254
		.ops = &omap3pandora_out_ops,
		.init = omap3pandora_out_init,
	}, {
		.name = "TWL4030",
		.stream_name = "Line/Mic In",
		.cpu_dai = &omap_mcbsp_dai[1],
255
		.codec_dai = &twl4030_dai[TWL4030_DAI_HIFI],
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
		.ops = &omap3pandora_in_ops,
		.init = omap3pandora_in_init,
	}
};

/* SoC card */
static struct snd_soc_card snd_soc_card_omap3pandora = {
	.name = "omap3pandora",
	.platform = &omap_soc_platform,
	.dai_link = omap3pandora_dai,
	.num_links = ARRAY_SIZE(omap3pandora_dai),
};

/* Audio subsystem */
static struct snd_soc_device omap3pandora_snd_data = {
	.card = &snd_soc_card_omap3pandora,
	.codec_dev = &soc_codec_dev_twl4030,
};

static struct platform_device *omap3pandora_snd_device;

static int __init omap3pandora_soc_init(void)
{
	int ret;

281
	if (!machine_is_omap3_pandora())
282
		return -ENODEV;
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
	pr_info("OMAP3 Pandora SoC init\n");

	ret = gpio_request(OMAP3_PANDORA_DAC_POWER_GPIO, "dac_power");
	if (ret) {
		pr_err(PREFIX "Failed to get DAC power GPIO\n");
		return ret;
	}

	ret = gpio_direction_output(OMAP3_PANDORA_DAC_POWER_GPIO, 0);
	if (ret) {
		pr_err(PREFIX "Failed to set DAC power GPIO direction\n");
		goto fail0;
	}

	ret = gpio_request(OMAP3_PANDORA_AMP_POWER_GPIO, "amp_power");
	if (ret) {
		pr_err(PREFIX "Failed to get amp power GPIO\n");
		goto fail0;
	}

	ret = gpio_direction_output(OMAP3_PANDORA_AMP_POWER_GPIO, 0);
	if (ret) {
		pr_err(PREFIX "Failed to set amp power GPIO direction\n");
		goto fail1;
	}

	omap3pandora_snd_device = platform_device_alloc("soc-audio", -1);
	if (omap3pandora_snd_device == NULL) {
		pr_err(PREFIX "Platform device allocation failed\n");
		ret = -ENOMEM;
		goto fail1;
	}

	platform_set_drvdata(omap3pandora_snd_device, &omap3pandora_snd_data);
	omap3pandora_snd_data.dev = &omap3pandora_snd_device->dev;
	*(unsigned int *)omap_mcbsp_dai[0].private_data = 1; /* McBSP2 */
	*(unsigned int *)omap_mcbsp_dai[1].private_data = 3; /* McBSP4 */

	ret = platform_device_add(omap3pandora_snd_device);
	if (ret) {
		pr_err(PREFIX "Unable to add platform device\n");
		goto fail2;
	}

328 329 330 331 332 333 334 335
	omap3pandora_dac_reg = regulator_get(&omap3pandora_snd_device->dev, "vcc");
	if (IS_ERR(omap3pandora_dac_reg)) {
		pr_err(PREFIX "Failed to get DAC regulator from %s: %ld\n",
			dev_name(&omap3pandora_snd_device->dev),
			PTR_ERR(omap3pandora_dac_reg));
		goto fail3;
	}

336 337
	return 0;

338 339
fail3:
	platform_device_del(omap3pandora_snd_device);
340 341 342 343 344 345 346 347 348 349 350 351
fail2:
	platform_device_put(omap3pandora_snd_device);
fail1:
	gpio_free(OMAP3_PANDORA_AMP_POWER_GPIO);
fail0:
	gpio_free(OMAP3_PANDORA_DAC_POWER_GPIO);
	return ret;
}
module_init(omap3pandora_soc_init);

static void __exit omap3pandora_soc_exit(void)
{
352
	regulator_put(omap3pandora_dac_reg);
353 354 355 356 357 358 359 360 361
	platform_device_unregister(omap3pandora_snd_device);
	gpio_free(OMAP3_PANDORA_AMP_POWER_GPIO);
	gpio_free(OMAP3_PANDORA_DAC_POWER_GPIO);
}
module_exit(omap3pandora_soc_exit);

MODULE_AUTHOR("Grazvydas Ignotas <notasas@gmail.com>");
MODULE_DESCRIPTION("ALSA SoC OMAP3 Pandora");
MODULE_LICENSE("GPL");