Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * ALSA SoC driver for Migo-R |
| 3 | * |
| 4 | * Copyright (C) 2009-2010 Guennadi Liakhovetski <g.liakhovetski@gmx.de> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/device.h> |
| 12 | #include <linux/firmware.h> |
| 13 | #include <linux/module.h> |
| 14 | |
| 15 | #include <asm/clock.h> |
| 16 | |
| 17 | #include <cpu/sh7722.h> |
| 18 | |
| 19 | #include <sound/core.h> |
| 20 | #include <sound/pcm.h> |
| 21 | #include <sound/soc.h> |
| 22 | #include <sound/soc-dapm.h> |
| 23 | |
| 24 | #include "../codecs/wm8978.h" |
| 25 | #include "siu.h" |
| 26 | |
| 27 | /* Default 8000Hz sampling frequency */ |
| 28 | static unsigned long codec_freq = 8000 * 512; |
| 29 | |
| 30 | static unsigned int use_count; |
| 31 | |
| 32 | /* External clock, sourced from the codec at the SIUMCKB pin */ |
| 33 | static unsigned long siumckb_recalc(struct clk *clk) |
| 34 | { |
| 35 | return codec_freq; |
| 36 | } |
| 37 | |
| 38 | static struct clk_ops siumckb_clk_ops = { |
| 39 | .recalc = siumckb_recalc, |
| 40 | }; |
| 41 | |
| 42 | static struct clk siumckb_clk = { |
| 43 | .name = "siumckb_clk", |
| 44 | .id = -1, |
| 45 | .ops = &siumckb_clk_ops, |
| 46 | .rate = 0, /* initialised at run-time */ |
| 47 | }; |
| 48 | |
| 49 | static int migor_hw_params(struct snd_pcm_substream *substream, |
| 50 | struct snd_pcm_hw_params *params) |
| 51 | { |
| 52 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 53 | struct snd_soc_dai *codec_dai = rtd->dai->codec_dai; |
| 54 | int ret; |
| 55 | unsigned int rate = params_rate(params); |
| 56 | |
| 57 | ret = snd_soc_dai_set_sysclk(codec_dai, WM8978_PLL, 13000000, |
| 58 | SND_SOC_CLOCK_IN); |
| 59 | if (ret < 0) |
| 60 | return ret; |
| 61 | |
| 62 | ret = snd_soc_dai_set_clkdiv(codec_dai, WM8978_DACCLK, 8); |
| 63 | if (ret < 0) |
| 64 | return ret; |
| 65 | |
| 66 | ret = snd_soc_dai_set_clkdiv(codec_dai, WM8978_OPCLKRATE, rate * 512); |
| 67 | if (ret < 0) |
| 68 | return ret; |
| 69 | |
| 70 | ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_NB_IF | |
| 71 | SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS); |
| 72 | if (ret < 0) |
| 73 | return ret; |
| 74 | |
| 75 | ret = snd_soc_dai_set_fmt(rtd->dai->cpu_dai, SND_SOC_DAIFMT_NB_IF | |
| 76 | SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS); |
| 77 | if (ret < 0) |
| 78 | return ret; |
| 79 | |
| 80 | codec_freq = rate * 512; |
| 81 | /* |
| 82 | * This propagates the parent frequency change to children and |
| 83 | * recalculates the frequency table |
| 84 | */ |
| 85 | clk_set_rate(&siumckb_clk, codec_freq); |
| 86 | dev_dbg(codec_dai->dev, "%s: configure %luHz\n", __func__, codec_freq); |
| 87 | |
| 88 | ret = snd_soc_dai_set_sysclk(rtd->dai->cpu_dai, SIU_CLKB_EXT, |
| 89 | codec_freq / 2, SND_SOC_CLOCK_IN); |
| 90 | |
| 91 | if (!ret) |
| 92 | use_count++; |
| 93 | |
| 94 | return ret; |
| 95 | } |
| 96 | |
| 97 | static int migor_hw_free(struct snd_pcm_substream *substream) |
| 98 | { |
| 99 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 100 | struct snd_soc_dai *codec_dai = rtd->dai->codec_dai; |
| 101 | |
| 102 | if (use_count) { |
| 103 | use_count--; |
| 104 | |
| 105 | if (!use_count) |
| 106 | snd_soc_dai_set_sysclk(codec_dai, WM8978_PLL, 0, |
| 107 | SND_SOC_CLOCK_IN); |
| 108 | } else { |
| 109 | dev_dbg(codec_dai->dev, "Unbalanced hw_free!\n"); |
| 110 | } |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | static struct snd_soc_ops migor_dai_ops = { |
| 116 | .hw_params = migor_hw_params, |
| 117 | .hw_free = migor_hw_free, |
| 118 | }; |
| 119 | |
| 120 | static const struct snd_soc_dapm_widget migor_dapm_widgets[] = { |
| 121 | SND_SOC_DAPM_HP("Headphone", NULL), |
| 122 | SND_SOC_DAPM_MIC("Onboard Microphone", NULL), |
| 123 | SND_SOC_DAPM_MIC("External Microphone", NULL), |
| 124 | }; |
| 125 | |
| 126 | static const struct snd_soc_dapm_route audio_map[] = { |
| 127 | /* Headphone output connected to LHP/RHP, enable OUT4 for VMID */ |
| 128 | { "Headphone", NULL, "OUT4 VMID" }, |
| 129 | { "OUT4 VMID", NULL, "LHP" }, |
| 130 | { "OUT4 VMID", NULL, "RHP" }, |
| 131 | |
| 132 | /* On-board microphone */ |
| 133 | { "RMICN", NULL, "Mic Bias" }, |
| 134 | { "RMICP", NULL, "Mic Bias" }, |
| 135 | { "Mic Bias", NULL, "Onboard Microphone" }, |
| 136 | |
| 137 | /* External microphone */ |
| 138 | { "LMICN", NULL, "Mic Bias" }, |
| 139 | { "LMICP", NULL, "Mic Bias" }, |
| 140 | { "Mic Bias", NULL, "External Microphone" }, |
| 141 | }; |
| 142 | |
| 143 | static int migor_dai_init(struct snd_soc_codec *codec) |
| 144 | { |
| 145 | snd_soc_dapm_new_controls(codec, migor_dapm_widgets, |
| 146 | ARRAY_SIZE(migor_dapm_widgets)); |
| 147 | |
| 148 | snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map)); |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | /* migor digital audio interface glue - connects codec <--> CPU */ |
| 154 | static struct snd_soc_dai_link migor_dai = { |
| 155 | .name = "wm8978", |
| 156 | .stream_name = "WM8978", |
| 157 | .cpu_dai = &siu_i2s_dai, |
| 158 | .codec_dai = &wm8978_dai, |
| 159 | .ops = &migor_dai_ops, |
| 160 | .init = migor_dai_init, |
| 161 | }; |
| 162 | |
| 163 | /* migor audio machine driver */ |
| 164 | static struct snd_soc_card snd_soc_migor = { |
| 165 | .name = "Migo-R", |
| 166 | .platform = &siu_platform, |
| 167 | .dai_link = &migor_dai, |
| 168 | .num_links = 1, |
| 169 | }; |
| 170 | |
| 171 | /* migor audio subsystem */ |
| 172 | static struct snd_soc_device migor_snd_devdata = { |
| 173 | .card = &snd_soc_migor, |
| 174 | .codec_dev = &soc_codec_dev_wm8978, |
| 175 | }; |
| 176 | |
| 177 | static struct platform_device *migor_snd_device; |
| 178 | |
| 179 | static int __init migor_init(void) |
| 180 | { |
| 181 | int ret; |
| 182 | |
| 183 | ret = clk_register(&siumckb_clk); |
| 184 | if (ret < 0) |
| 185 | return ret; |
| 186 | |
| 187 | /* Port number used on this machine: port B */ |
| 188 | migor_snd_device = platform_device_alloc("soc-audio", 1); |
| 189 | if (!migor_snd_device) { |
| 190 | ret = -ENOMEM; |
| 191 | goto epdevalloc; |
| 192 | } |
| 193 | |
| 194 | platform_set_drvdata(migor_snd_device, &migor_snd_devdata); |
| 195 | |
| 196 | migor_snd_devdata.dev = &migor_snd_device->dev; |
| 197 | |
| 198 | ret = platform_device_add(migor_snd_device); |
| 199 | if (ret) |
| 200 | goto epdevadd; |
| 201 | |
| 202 | return 0; |
| 203 | |
| 204 | epdevadd: |
| 205 | platform_device_put(migor_snd_device); |
| 206 | epdevalloc: |
| 207 | clk_unregister(&siumckb_clk); |
| 208 | return ret; |
| 209 | } |
| 210 | |
| 211 | static void __exit migor_exit(void) |
| 212 | { |
| 213 | clk_unregister(&siumckb_clk); |
| 214 | platform_device_unregister(migor_snd_device); |
| 215 | } |
| 216 | |
| 217 | module_init(migor_init); |
| 218 | module_exit(migor_exit); |
| 219 | |
| 220 | MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>"); |
| 221 | MODULE_DESCRIPTION("ALSA SoC Migor"); |
| 222 | MODULE_LICENSE("GPL v2"); |