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 | |
Guennadi Liakhovetski | cbfa518 | 2010-09-17 12:30:11 +0200 | [diff] [blame] | 15 | #include <asm/clkdev.h> |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 16 | #include <asm/clock.h> |
| 17 | |
| 18 | #include <cpu/sh7722.h> |
| 19 | |
| 20 | #include <sound/core.h> |
| 21 | #include <sound/pcm.h> |
| 22 | #include <sound/soc.h> |
| 23 | #include <sound/soc-dapm.h> |
| 24 | |
| 25 | #include "../codecs/wm8978.h" |
| 26 | #include "siu.h" |
| 27 | |
| 28 | /* Default 8000Hz sampling frequency */ |
| 29 | static unsigned long codec_freq = 8000 * 512; |
| 30 | |
| 31 | static unsigned int use_count; |
| 32 | |
| 33 | /* External clock, sourced from the codec at the SIUMCKB pin */ |
| 34 | static unsigned long siumckb_recalc(struct clk *clk) |
| 35 | { |
| 36 | return codec_freq; |
| 37 | } |
| 38 | |
| 39 | static struct clk_ops siumckb_clk_ops = { |
| 40 | .recalc = siumckb_recalc, |
| 41 | }; |
| 42 | |
| 43 | static struct clk siumckb_clk = { |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 44 | .ops = &siumckb_clk_ops, |
| 45 | .rate = 0, /* initialised at run-time */ |
| 46 | }; |
| 47 | |
Guennadi Liakhovetski | cbfa518 | 2010-09-17 12:30:11 +0200 | [diff] [blame] | 48 | static struct clk_lookup *siumckb_lookup; |
| 49 | |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 50 | static int migor_hw_params(struct snd_pcm_substream *substream, |
| 51 | struct snd_pcm_hw_params *params) |
| 52 | { |
| 53 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 54 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 55 | int ret; |
| 56 | unsigned int rate = params_rate(params); |
| 57 | |
| 58 | ret = snd_soc_dai_set_sysclk(codec_dai, WM8978_PLL, 13000000, |
| 59 | SND_SOC_CLOCK_IN); |
| 60 | if (ret < 0) |
| 61 | return ret; |
| 62 | |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 63 | ret = snd_soc_dai_set_clkdiv(codec_dai, WM8978_OPCLKRATE, rate * 512); |
| 64 | if (ret < 0) |
| 65 | return ret; |
| 66 | |
| 67 | ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_NB_IF | |
| 68 | SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS); |
| 69 | if (ret < 0) |
| 70 | return ret; |
| 71 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 72 | ret = snd_soc_dai_set_fmt(rtd->cpu_dai, SND_SOC_DAIFMT_NB_IF | |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 73 | SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS); |
| 74 | if (ret < 0) |
| 75 | return ret; |
| 76 | |
| 77 | codec_freq = rate * 512; |
| 78 | /* |
| 79 | * This propagates the parent frequency change to children and |
| 80 | * recalculates the frequency table |
| 81 | */ |
| 82 | clk_set_rate(&siumckb_clk, codec_freq); |
| 83 | dev_dbg(codec_dai->dev, "%s: configure %luHz\n", __func__, codec_freq); |
| 84 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 85 | ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, SIU_CLKB_EXT, |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 86 | codec_freq / 2, SND_SOC_CLOCK_IN); |
| 87 | |
| 88 | if (!ret) |
| 89 | use_count++; |
| 90 | |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | static int migor_hw_free(struct snd_pcm_substream *substream) |
| 95 | { |
| 96 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 97 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 98 | |
| 99 | if (use_count) { |
| 100 | use_count--; |
| 101 | |
| 102 | if (!use_count) |
| 103 | snd_soc_dai_set_sysclk(codec_dai, WM8978_PLL, 0, |
| 104 | SND_SOC_CLOCK_IN); |
| 105 | } else { |
| 106 | dev_dbg(codec_dai->dev, "Unbalanced hw_free!\n"); |
| 107 | } |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static struct snd_soc_ops migor_dai_ops = { |
| 113 | .hw_params = migor_hw_params, |
| 114 | .hw_free = migor_hw_free, |
| 115 | }; |
| 116 | |
| 117 | static const struct snd_soc_dapm_widget migor_dapm_widgets[] = { |
| 118 | SND_SOC_DAPM_HP("Headphone", NULL), |
| 119 | SND_SOC_DAPM_MIC("Onboard Microphone", NULL), |
| 120 | SND_SOC_DAPM_MIC("External Microphone", NULL), |
| 121 | }; |
| 122 | |
| 123 | static const struct snd_soc_dapm_route audio_map[] = { |
| 124 | /* Headphone output connected to LHP/RHP, enable OUT4 for VMID */ |
| 125 | { "Headphone", NULL, "OUT4 VMID" }, |
| 126 | { "OUT4 VMID", NULL, "LHP" }, |
| 127 | { "OUT4 VMID", NULL, "RHP" }, |
| 128 | |
| 129 | /* On-board microphone */ |
| 130 | { "RMICN", NULL, "Mic Bias" }, |
| 131 | { "RMICP", NULL, "Mic Bias" }, |
| 132 | { "Mic Bias", NULL, "Onboard Microphone" }, |
| 133 | |
| 134 | /* External microphone */ |
| 135 | { "LMICN", NULL, "Mic Bias" }, |
| 136 | { "LMICP", NULL, "Mic Bias" }, |
| 137 | { "Mic Bias", NULL, "External Microphone" }, |
| 138 | }; |
| 139 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 140 | static int migor_dai_init(struct snd_soc_pcm_runtime *rtd) |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 141 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 142 | struct snd_soc_codec *codec = rtd->codec; |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame^] | 143 | struct snd_soc_dapm_context *dapm = &codec->dapm; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 144 | |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame^] | 145 | snd_soc_dapm_new_controls(dapm, migor_dapm_widgets, |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 146 | ARRAY_SIZE(migor_dapm_widgets)); |
| 147 | |
Liam Girdwood | ce6120c | 2010-11-05 15:53:46 +0200 | [diff] [blame^] | 148 | snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map)); |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 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", |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 157 | .cpu_dai_name = "siu-i2s-dai", |
| 158 | .codec_dai_name = "wm8978-hifi", |
| 159 | .platform_name = "siu-pcm-audio", |
Mark Brown | 5250a50 | 2010-09-19 16:07:06 +0100 | [diff] [blame] | 160 | .codec_name = "wm8978.0-001a", |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 161 | .ops = &migor_dai_ops, |
| 162 | .init = migor_dai_init, |
| 163 | }; |
| 164 | |
| 165 | /* migor audio machine driver */ |
| 166 | static struct snd_soc_card snd_soc_migor = { |
| 167 | .name = "Migo-R", |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 168 | .dai_link = &migor_dai, |
| 169 | .num_links = 1, |
| 170 | }; |
| 171 | |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 172 | static struct platform_device *migor_snd_device; |
| 173 | |
| 174 | static int __init migor_init(void) |
| 175 | { |
| 176 | int ret; |
| 177 | |
| 178 | ret = clk_register(&siumckb_clk); |
| 179 | if (ret < 0) |
| 180 | return ret; |
| 181 | |
Guennadi Liakhovetski | cbfa518 | 2010-09-17 12:30:11 +0200 | [diff] [blame] | 182 | siumckb_lookup = clkdev_alloc(&siumckb_clk, "siumckb_clk", NULL); |
| 183 | if (!siumckb_lookup) { |
| 184 | ret = -ENOMEM; |
| 185 | goto eclkdevalloc; |
| 186 | } |
| 187 | clkdev_add(siumckb_lookup); |
| 188 | |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 189 | /* Port number used on this machine: port B */ |
| 190 | migor_snd_device = platform_device_alloc("soc-audio", 1); |
| 191 | if (!migor_snd_device) { |
| 192 | ret = -ENOMEM; |
| 193 | goto epdevalloc; |
| 194 | } |
| 195 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 196 | platform_set_drvdata(migor_snd_device, &snd_soc_migor); |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 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: |
Guennadi Liakhovetski | cbfa518 | 2010-09-17 12:30:11 +0200 | [diff] [blame] | 207 | clkdev_drop(siumckb_lookup); |
| 208 | eclkdevalloc: |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 209 | clk_unregister(&siumckb_clk); |
| 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | static void __exit migor_exit(void) |
| 214 | { |
Guennadi Liakhovetski | cbfa518 | 2010-09-17 12:30:11 +0200 | [diff] [blame] | 215 | clkdev_drop(siumckb_lookup); |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 216 | clk_unregister(&siumckb_clk); |
| 217 | platform_device_unregister(migor_snd_device); |
| 218 | } |
| 219 | |
| 220 | module_init(migor_init); |
| 221 | module_exit(migor_exit); |
| 222 | |
| 223 | MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>"); |
| 224 | MODULE_DESCRIPTION("ALSA SoC Migor"); |
| 225 | MODULE_LICENSE("GPL v2"); |