Grazvydas Ignotas | 68fb740 | 2008-12-04 22:39:54 +0200 | [diff] [blame] | 1 | /* |
| 2 | * omap3pandora.c -- SoC audio for Pandora Handheld Console |
| 3 | * |
| 4 | * Author: GraÅžvydas Ignotas <notasas@gmail.com> |
| 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 | * version 2 as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 18 | * 02110-1301 USA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <linux/clk.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/gpio.h> |
| 25 | #include <linux/delay.h> |
| 26 | |
| 27 | #include <sound/core.h> |
| 28 | #include <sound/pcm.h> |
| 29 | #include <sound/soc.h> |
| 30 | #include <sound/soc-dapm.h> |
| 31 | |
| 32 | #include <asm/mach-types.h> |
| 33 | |
| 34 | #include "omap-mcbsp.h" |
| 35 | #include "omap-pcm.h" |
| 36 | #include "../codecs/twl4030.h" |
| 37 | |
| 38 | #define OMAP3_PANDORA_DAC_POWER_GPIO 118 |
| 39 | #define OMAP3_PANDORA_AMP_POWER_GPIO 14 |
| 40 | |
| 41 | #define PREFIX "ASoC omap3pandora: " |
| 42 | |
| 43 | static int omap3pandora_cmn_hw_params(struct snd_soc_dai *codec_dai, |
| 44 | struct snd_soc_dai *cpu_dai, unsigned int fmt) |
| 45 | { |
| 46 | int ret; |
| 47 | |
| 48 | /* Set codec DAI configuration */ |
| 49 | ret = snd_soc_dai_set_fmt(codec_dai, fmt); |
| 50 | if (ret < 0) { |
| 51 | pr_err(PREFIX "can't set codec DAI configuration\n"); |
| 52 | return ret; |
| 53 | } |
| 54 | |
| 55 | /* Set cpu DAI configuration */ |
| 56 | ret = snd_soc_dai_set_fmt(cpu_dai, fmt); |
| 57 | if (ret < 0) { |
| 58 | pr_err(PREFIX "can't set cpu DAI configuration\n"); |
| 59 | return ret; |
| 60 | } |
| 61 | |
| 62 | /* Set the codec system clock for DAC and ADC */ |
| 63 | ret = snd_soc_dai_set_sysclk(codec_dai, 0, 26000000, |
| 64 | SND_SOC_CLOCK_IN); |
| 65 | if (ret < 0) { |
| 66 | pr_err(PREFIX "can't set codec system clock\n"); |
| 67 | return ret; |
| 68 | } |
| 69 | |
| 70 | /* Set McBSP clock to external */ |
| 71 | ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_MCBSP_SYSCLK_CLKS_EXT, 0, |
| 72 | SND_SOC_CLOCK_IN); |
| 73 | if (ret < 0) { |
| 74 | pr_err(PREFIX "can't set cpu system clock\n"); |
| 75 | return ret; |
| 76 | } |
| 77 | |
| 78 | ret = snd_soc_dai_set_clkdiv(cpu_dai, OMAP_MCBSP_CLKGDV, 8); |
| 79 | if (ret < 0) { |
| 80 | pr_err(PREFIX "can't set SRG clock divider\n"); |
| 81 | return ret; |
| 82 | } |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | static int omap3pandora_out_hw_params(struct snd_pcm_substream *substream, |
| 88 | struct snd_pcm_hw_params *params) |
| 89 | { |
| 90 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 91 | struct snd_soc_dai *codec_dai = rtd->dai->codec_dai; |
| 92 | struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai; |
| 93 | |
| 94 | return omap3pandora_cmn_hw_params(codec_dai, cpu_dai, |
| 95 | SND_SOC_DAIFMT_I2S | |
| 96 | SND_SOC_DAIFMT_IB_NF | |
| 97 | SND_SOC_DAIFMT_CBS_CFS); |
| 98 | } |
| 99 | |
| 100 | static int omap3pandora_in_hw_params(struct snd_pcm_substream *substream, |
| 101 | struct snd_pcm_hw_params *params) |
| 102 | { |
| 103 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 104 | struct snd_soc_dai *codec_dai = rtd->dai->codec_dai; |
| 105 | struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai; |
| 106 | |
| 107 | return omap3pandora_cmn_hw_params(codec_dai, cpu_dai, |
| 108 | SND_SOC_DAIFMT_I2S | |
| 109 | SND_SOC_DAIFMT_NB_NF | |
| 110 | SND_SOC_DAIFMT_CBS_CFS); |
| 111 | } |
| 112 | |
| 113 | static int omap3pandora_hp_event(struct snd_soc_dapm_widget *w, |
| 114 | struct snd_kcontrol *k, int event) |
| 115 | { |
| 116 | if (SND_SOC_DAPM_EVENT_ON(event)) { |
| 117 | gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 1); |
| 118 | gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 1); |
| 119 | } else { |
| 120 | gpio_set_value(OMAP3_PANDORA_AMP_POWER_GPIO, 0); |
| 121 | mdelay(1); |
| 122 | gpio_set_value(OMAP3_PANDORA_DAC_POWER_GPIO, 0); |
| 123 | } |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * Audio paths on Pandora board: |
| 130 | * |
| 131 | * |O| ---> PCM DAC +-> AMP -> Headphone Jack |
| 132 | * |M| A +--------> Line Out |
| 133 | * |A| <~~clk~~+ |
| 134 | * |P| <--- TWL4030 <--------- Line In and MICs |
| 135 | */ |
| 136 | static const struct snd_soc_dapm_widget omap3pandora_out_dapm_widgets[] = { |
Grazvydas Ignotas | f3dd704 | 2009-11-07 23:16:12 +0200 | [diff] [blame] | 137 | SND_SOC_DAPM_DAC("PCM DAC", "HiFi Playback", SND_SOC_NOPM, 0, 0), |
Grazvydas Ignotas | 68fb740 | 2008-12-04 22:39:54 +0200 | [diff] [blame] | 138 | SND_SOC_DAPM_PGA_E("Headphone Amplifier", SND_SOC_NOPM, |
| 139 | 0, 0, NULL, 0, omap3pandora_hp_event, |
| 140 | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), |
| 141 | SND_SOC_DAPM_HP("Headphone Jack", NULL), |
| 142 | SND_SOC_DAPM_LINE("Line Out", NULL), |
| 143 | }; |
| 144 | |
| 145 | static const struct snd_soc_dapm_widget omap3pandora_in_dapm_widgets[] = { |
Grazvydas Ignotas | 8f00806 | 2009-01-31 16:29:24 +0200 | [diff] [blame] | 146 | SND_SOC_DAPM_MIC("Mic (internal)", NULL), |
Grazvydas Ignotas | 68fb740 | 2008-12-04 22:39:54 +0200 | [diff] [blame] | 147 | SND_SOC_DAPM_MIC("Mic (external)", NULL), |
| 148 | SND_SOC_DAPM_LINE("Line In", NULL), |
| 149 | }; |
| 150 | |
| 151 | static const struct snd_soc_dapm_route omap3pandora_out_map[] = { |
| 152 | {"Headphone Amplifier", NULL, "PCM DAC"}, |
| 153 | {"Line Out", NULL, "PCM DAC"}, |
| 154 | {"Headphone Jack", NULL, "Headphone Amplifier"}, |
| 155 | }; |
| 156 | |
| 157 | static const struct snd_soc_dapm_route omap3pandora_in_map[] = { |
Grazvydas Ignotas | 8f00806 | 2009-01-31 16:29:24 +0200 | [diff] [blame] | 158 | {"AUXL", NULL, "Line In"}, |
| 159 | {"AUXR", NULL, "Line In"}, |
| 160 | |
| 161 | {"MAINMIC", NULL, "Mic Bias 1"}, |
| 162 | {"Mic Bias 1", NULL, "Mic (internal)"}, |
| 163 | |
| 164 | {"SUBMIC", NULL, "Mic Bias 2"}, |
| 165 | {"Mic Bias 2", NULL, "Mic (external)"}, |
Grazvydas Ignotas | 68fb740 | 2008-12-04 22:39:54 +0200 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | static int omap3pandora_out_init(struct snd_soc_codec *codec) |
| 169 | { |
| 170 | int ret; |
| 171 | |
Grazvydas Ignotas | 8f00806 | 2009-01-31 16:29:24 +0200 | [diff] [blame] | 172 | /* All TWL4030 output pins are floating */ |
| 173 | snd_soc_dapm_nc_pin(codec, "OUTL"); |
| 174 | snd_soc_dapm_nc_pin(codec, "OUTR"); |
| 175 | snd_soc_dapm_nc_pin(codec, "EARPIECE"); |
| 176 | snd_soc_dapm_nc_pin(codec, "PREDRIVEL"); |
| 177 | snd_soc_dapm_nc_pin(codec, "PREDRIVER"); |
| 178 | snd_soc_dapm_nc_pin(codec, "HSOL"); |
| 179 | snd_soc_dapm_nc_pin(codec, "HSOR"); |
| 180 | snd_soc_dapm_nc_pin(codec, "CARKITL"); |
| 181 | snd_soc_dapm_nc_pin(codec, "CARKITR"); |
| 182 | snd_soc_dapm_nc_pin(codec, "HFL"); |
| 183 | snd_soc_dapm_nc_pin(codec, "HFR"); |
Grazvydas Ignotas | f3dd704 | 2009-11-07 23:16:12 +0200 | [diff] [blame] | 184 | snd_soc_dapm_nc_pin(codec, "VIBRA"); |
Grazvydas Ignotas | 8f00806 | 2009-01-31 16:29:24 +0200 | [diff] [blame] | 185 | |
Grazvydas Ignotas | 68fb740 | 2008-12-04 22:39:54 +0200 | [diff] [blame] | 186 | ret = snd_soc_dapm_new_controls(codec, omap3pandora_out_dapm_widgets, |
| 187 | ARRAY_SIZE(omap3pandora_out_dapm_widgets)); |
| 188 | if (ret < 0) |
| 189 | return ret; |
| 190 | |
| 191 | snd_soc_dapm_add_routes(codec, omap3pandora_out_map, |
| 192 | ARRAY_SIZE(omap3pandora_out_map)); |
| 193 | |
| 194 | return snd_soc_dapm_sync(codec); |
| 195 | } |
| 196 | |
| 197 | static int omap3pandora_in_init(struct snd_soc_codec *codec) |
| 198 | { |
| 199 | int ret; |
| 200 | |
Grazvydas Ignotas | 8f00806 | 2009-01-31 16:29:24 +0200 | [diff] [blame] | 201 | /* Not comnnected */ |
| 202 | snd_soc_dapm_nc_pin(codec, "HSMIC"); |
| 203 | snd_soc_dapm_nc_pin(codec, "CARKITMIC"); |
| 204 | snd_soc_dapm_nc_pin(codec, "DIGIMIC0"); |
| 205 | snd_soc_dapm_nc_pin(codec, "DIGIMIC1"); |
Grazvydas Ignotas | 7f18534 | 2008-12-23 12:04:48 +0200 | [diff] [blame] | 206 | |
Grazvydas Ignotas | 68fb740 | 2008-12-04 22:39:54 +0200 | [diff] [blame] | 207 | ret = snd_soc_dapm_new_controls(codec, omap3pandora_in_dapm_widgets, |
| 208 | ARRAY_SIZE(omap3pandora_in_dapm_widgets)); |
| 209 | if (ret < 0) |
| 210 | return ret; |
| 211 | |
| 212 | snd_soc_dapm_add_routes(codec, omap3pandora_in_map, |
| 213 | ARRAY_SIZE(omap3pandora_in_map)); |
| 214 | |
| 215 | return snd_soc_dapm_sync(codec); |
| 216 | } |
| 217 | |
| 218 | static struct snd_soc_ops omap3pandora_out_ops = { |
| 219 | .hw_params = omap3pandora_out_hw_params, |
| 220 | }; |
| 221 | |
| 222 | static struct snd_soc_ops omap3pandora_in_ops = { |
| 223 | .hw_params = omap3pandora_in_hw_params, |
| 224 | }; |
| 225 | |
| 226 | /* Digital audio interface glue - connects codec <--> CPU */ |
| 227 | static struct snd_soc_dai_link omap3pandora_dai[] = { |
| 228 | { |
| 229 | .name = "PCM1773", |
| 230 | .stream_name = "HiFi Out", |
| 231 | .cpu_dai = &omap_mcbsp_dai[0], |
Joonyoung Shim | 7154b3e | 2009-04-20 19:21:35 +0900 | [diff] [blame] | 232 | .codec_dai = &twl4030_dai[TWL4030_DAI_HIFI], |
Grazvydas Ignotas | 68fb740 | 2008-12-04 22:39:54 +0200 | [diff] [blame] | 233 | .ops = &omap3pandora_out_ops, |
| 234 | .init = omap3pandora_out_init, |
| 235 | }, { |
| 236 | .name = "TWL4030", |
| 237 | .stream_name = "Line/Mic In", |
| 238 | .cpu_dai = &omap_mcbsp_dai[1], |
Joonyoung Shim | 7154b3e | 2009-04-20 19:21:35 +0900 | [diff] [blame] | 239 | .codec_dai = &twl4030_dai[TWL4030_DAI_HIFI], |
Grazvydas Ignotas | 68fb740 | 2008-12-04 22:39:54 +0200 | [diff] [blame] | 240 | .ops = &omap3pandora_in_ops, |
| 241 | .init = omap3pandora_in_init, |
| 242 | } |
| 243 | }; |
| 244 | |
| 245 | /* SoC card */ |
| 246 | static struct snd_soc_card snd_soc_card_omap3pandora = { |
| 247 | .name = "omap3pandora", |
| 248 | .platform = &omap_soc_platform, |
| 249 | .dai_link = omap3pandora_dai, |
| 250 | .num_links = ARRAY_SIZE(omap3pandora_dai), |
| 251 | }; |
| 252 | |
| 253 | /* Audio subsystem */ |
| 254 | static struct snd_soc_device omap3pandora_snd_data = { |
| 255 | .card = &snd_soc_card_omap3pandora, |
| 256 | .codec_dev = &soc_codec_dev_twl4030, |
| 257 | }; |
| 258 | |
| 259 | static struct platform_device *omap3pandora_snd_device; |
| 260 | |
| 261 | static int __init omap3pandora_soc_init(void) |
| 262 | { |
| 263 | int ret; |
| 264 | |
Grazvydas Ignotas | 8f00806 | 2009-01-31 16:29:24 +0200 | [diff] [blame] | 265 | if (!machine_is_omap3_pandora()) |
Grazvydas Ignotas | 68fb740 | 2008-12-04 22:39:54 +0200 | [diff] [blame] | 266 | return -ENODEV; |
Grazvydas Ignotas | 8f00806 | 2009-01-31 16:29:24 +0200 | [diff] [blame] | 267 | |
Grazvydas Ignotas | 68fb740 | 2008-12-04 22:39:54 +0200 | [diff] [blame] | 268 | pr_info("OMAP3 Pandora SoC init\n"); |
| 269 | |
| 270 | ret = gpio_request(OMAP3_PANDORA_DAC_POWER_GPIO, "dac_power"); |
| 271 | if (ret) { |
| 272 | pr_err(PREFIX "Failed to get DAC power GPIO\n"); |
| 273 | return ret; |
| 274 | } |
| 275 | |
| 276 | ret = gpio_direction_output(OMAP3_PANDORA_DAC_POWER_GPIO, 0); |
| 277 | if (ret) { |
| 278 | pr_err(PREFIX "Failed to set DAC power GPIO direction\n"); |
| 279 | goto fail0; |
| 280 | } |
| 281 | |
| 282 | ret = gpio_request(OMAP3_PANDORA_AMP_POWER_GPIO, "amp_power"); |
| 283 | if (ret) { |
| 284 | pr_err(PREFIX "Failed to get amp power GPIO\n"); |
| 285 | goto fail0; |
| 286 | } |
| 287 | |
| 288 | ret = gpio_direction_output(OMAP3_PANDORA_AMP_POWER_GPIO, 0); |
| 289 | if (ret) { |
| 290 | pr_err(PREFIX "Failed to set amp power GPIO direction\n"); |
| 291 | goto fail1; |
| 292 | } |
| 293 | |
| 294 | omap3pandora_snd_device = platform_device_alloc("soc-audio", -1); |
| 295 | if (omap3pandora_snd_device == NULL) { |
| 296 | pr_err(PREFIX "Platform device allocation failed\n"); |
| 297 | ret = -ENOMEM; |
| 298 | goto fail1; |
| 299 | } |
| 300 | |
| 301 | platform_set_drvdata(omap3pandora_snd_device, &omap3pandora_snd_data); |
| 302 | omap3pandora_snd_data.dev = &omap3pandora_snd_device->dev; |
| 303 | *(unsigned int *)omap_mcbsp_dai[0].private_data = 1; /* McBSP2 */ |
| 304 | *(unsigned int *)omap_mcbsp_dai[1].private_data = 3; /* McBSP4 */ |
| 305 | |
| 306 | ret = platform_device_add(omap3pandora_snd_device); |
| 307 | if (ret) { |
| 308 | pr_err(PREFIX "Unable to add platform device\n"); |
| 309 | goto fail2; |
| 310 | } |
| 311 | |
| 312 | return 0; |
| 313 | |
| 314 | fail2: |
| 315 | platform_device_put(omap3pandora_snd_device); |
| 316 | fail1: |
| 317 | gpio_free(OMAP3_PANDORA_AMP_POWER_GPIO); |
| 318 | fail0: |
| 319 | gpio_free(OMAP3_PANDORA_DAC_POWER_GPIO); |
| 320 | return ret; |
| 321 | } |
| 322 | module_init(omap3pandora_soc_init); |
| 323 | |
| 324 | static void __exit omap3pandora_soc_exit(void) |
| 325 | { |
| 326 | platform_device_unregister(omap3pandora_snd_device); |
| 327 | gpio_free(OMAP3_PANDORA_AMP_POWER_GPIO); |
| 328 | gpio_free(OMAP3_PANDORA_DAC_POWER_GPIO); |
| 329 | } |
| 330 | module_exit(omap3pandora_soc_exit); |
| 331 | |
| 332 | MODULE_AUTHOR("Grazvydas Ignotas <notasas@gmail.com>"); |
| 333 | MODULE_DESCRIPTION("ALSA SoC OMAP3 Pandora"); |
| 334 | MODULE_LICENSE("GPL"); |