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 | |
Guennadi Liakhovetski | f4fd58d | 2011-01-11 08:34:52 +0100 | [diff] [blame] | 11 | #include <linux/clkdev.h> |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 12 | #include <linux/device.h> |
| 13 | #include <linux/firmware.h> |
| 14 | #include <linux/module.h> |
| 15 | |
| 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> |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 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 | |
Guennadi Liakhovetski | c858719 | 2012-05-09 12:57:05 +0200 | [diff] [blame] | 38 | static struct sh_clk_ops siumckb_clk_ops = { |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 39 | .recalc = siumckb_recalc, |
| 40 | }; |
| 41 | |
| 42 | static struct clk siumckb_clk = { |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 43 | .ops = &siumckb_clk_ops, |
| 44 | .rate = 0, /* initialised at run-time */ |
| 45 | }; |
| 46 | |
Guennadi Liakhovetski | cbfa518 | 2010-09-17 12:30:11 +0200 | [diff] [blame] | 47 | static struct clk_lookup *siumckb_lookup; |
| 48 | |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 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; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 53 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 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 | |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 62 | ret = snd_soc_dai_set_clkdiv(codec_dai, WM8978_OPCLKRATE, rate * 512); |
| 63 | if (ret < 0) |
| 64 | return ret; |
| 65 | |
| 66 | ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_NB_IF | |
| 67 | SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS); |
| 68 | if (ret < 0) |
| 69 | return ret; |
| 70 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 71 | 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] | 72 | SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS); |
| 73 | if (ret < 0) |
| 74 | return ret; |
| 75 | |
| 76 | codec_freq = rate * 512; |
| 77 | /* |
| 78 | * This propagates the parent frequency change to children and |
| 79 | * recalculates the frequency table |
| 80 | */ |
| 81 | clk_set_rate(&siumckb_clk, codec_freq); |
| 82 | dev_dbg(codec_dai->dev, "%s: configure %luHz\n", __func__, codec_freq); |
| 83 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 84 | ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, SIU_CLKB_EXT, |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 85 | codec_freq / 2, SND_SOC_CLOCK_IN); |
| 86 | |
| 87 | if (!ret) |
| 88 | use_count++; |
| 89 | |
| 90 | return ret; |
| 91 | } |
| 92 | |
| 93 | static int migor_hw_free(struct snd_pcm_substream *substream) |
| 94 | { |
| 95 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 96 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 97 | |
| 98 | if (use_count) { |
| 99 | use_count--; |
| 100 | |
| 101 | if (!use_count) |
| 102 | snd_soc_dai_set_sysclk(codec_dai, WM8978_PLL, 0, |
| 103 | SND_SOC_CLOCK_IN); |
| 104 | } else { |
| 105 | dev_dbg(codec_dai->dev, "Unbalanced hw_free!\n"); |
| 106 | } |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | static struct snd_soc_ops migor_dai_ops = { |
| 112 | .hw_params = migor_hw_params, |
| 113 | .hw_free = migor_hw_free, |
| 114 | }; |
| 115 | |
| 116 | static const struct snd_soc_dapm_widget migor_dapm_widgets[] = { |
| 117 | SND_SOC_DAPM_HP("Headphone", NULL), |
| 118 | SND_SOC_DAPM_MIC("Onboard Microphone", NULL), |
| 119 | SND_SOC_DAPM_MIC("External Microphone", NULL), |
| 120 | }; |
| 121 | |
| 122 | static const struct snd_soc_dapm_route audio_map[] = { |
| 123 | /* Headphone output connected to LHP/RHP, enable OUT4 for VMID */ |
| 124 | { "Headphone", NULL, "OUT4 VMID" }, |
| 125 | { "OUT4 VMID", NULL, "LHP" }, |
| 126 | { "OUT4 VMID", NULL, "RHP" }, |
| 127 | |
| 128 | /* On-board microphone */ |
| 129 | { "RMICN", NULL, "Mic Bias" }, |
| 130 | { "RMICP", NULL, "Mic Bias" }, |
| 131 | { "Mic Bias", NULL, "Onboard Microphone" }, |
| 132 | |
| 133 | /* External microphone */ |
| 134 | { "LMICN", NULL, "Mic Bias" }, |
| 135 | { "LMICP", NULL, "Mic Bias" }, |
| 136 | { "Mic Bias", NULL, "External Microphone" }, |
| 137 | }; |
| 138 | |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 139 | /* migor digital audio interface glue - connects codec <--> CPU */ |
| 140 | static struct snd_soc_dai_link migor_dai = { |
| 141 | .name = "wm8978", |
| 142 | .stream_name = "WM8978", |
Kuninori Morimoto | 0a09dfa | 2013-03-22 00:54:50 -0700 | [diff] [blame] | 143 | .cpu_dai_name = "siu-pcm-audio", |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 144 | .codec_dai_name = "wm8978-hifi", |
| 145 | .platform_name = "siu-pcm-audio", |
Mark Brown | 5250a50 | 2010-09-19 16:07:06 +0100 | [diff] [blame] | 146 | .codec_name = "wm8978.0-001a", |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 147 | .ops = &migor_dai_ops, |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | /* migor audio machine driver */ |
| 151 | static struct snd_soc_card snd_soc_migor = { |
| 152 | .name = "Migo-R", |
Axel Lin | 4a7042e | 2011-12-23 14:53:32 +0800 | [diff] [blame] | 153 | .owner = THIS_MODULE, |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 154 | .dai_link = &migor_dai, |
| 155 | .num_links = 1, |
Lars-Peter Clausen | 23a735c | 2014-03-03 08:14:14 +0100 | [diff] [blame] | 156 | |
| 157 | .dapm_widgets = migor_dapm_widgets, |
| 158 | .num_dapm_widgets = ARRAY_SIZE(migor_dapm_widgets), |
| 159 | .dapm_routes = audio_map, |
| 160 | .num_dapm_routes = ARRAY_SIZE(audio_map), |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 161 | }; |
| 162 | |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 163 | static struct platform_device *migor_snd_device; |
| 164 | |
| 165 | static int __init migor_init(void) |
| 166 | { |
| 167 | int ret; |
| 168 | |
| 169 | ret = clk_register(&siumckb_clk); |
| 170 | if (ret < 0) |
| 171 | return ret; |
| 172 | |
Guennadi Liakhovetski | cbfa518 | 2010-09-17 12:30:11 +0200 | [diff] [blame] | 173 | siumckb_lookup = clkdev_alloc(&siumckb_clk, "siumckb_clk", NULL); |
| 174 | if (!siumckb_lookup) { |
| 175 | ret = -ENOMEM; |
| 176 | goto eclkdevalloc; |
| 177 | } |
| 178 | clkdev_add(siumckb_lookup); |
| 179 | |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 180 | /* Port number used on this machine: port B */ |
| 181 | migor_snd_device = platform_device_alloc("soc-audio", 1); |
| 182 | if (!migor_snd_device) { |
| 183 | ret = -ENOMEM; |
| 184 | goto epdevalloc; |
| 185 | } |
| 186 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 187 | platform_set_drvdata(migor_snd_device, &snd_soc_migor); |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 188 | |
| 189 | ret = platform_device_add(migor_snd_device); |
| 190 | if (ret) |
| 191 | goto epdevadd; |
| 192 | |
| 193 | return 0; |
| 194 | |
| 195 | epdevadd: |
| 196 | platform_device_put(migor_snd_device); |
| 197 | epdevalloc: |
Guennadi Liakhovetski | cbfa518 | 2010-09-17 12:30:11 +0200 | [diff] [blame] | 198 | clkdev_drop(siumckb_lookup); |
| 199 | eclkdevalloc: |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 200 | clk_unregister(&siumckb_clk); |
| 201 | return ret; |
| 202 | } |
| 203 | |
| 204 | static void __exit migor_exit(void) |
| 205 | { |
Guennadi Liakhovetski | cbfa518 | 2010-09-17 12:30:11 +0200 | [diff] [blame] | 206 | clkdev_drop(siumckb_lookup); |
Guennadi Liakhovetski | 9f5b64b | 2010-01-27 12:15:00 +0100 | [diff] [blame] | 207 | clk_unregister(&siumckb_clk); |
| 208 | platform_device_unregister(migor_snd_device); |
| 209 | } |
| 210 | |
| 211 | module_init(migor_init); |
| 212 | module_exit(migor_exit); |
| 213 | |
| 214 | MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>"); |
| 215 | MODULE_DESCRIPTION("ALSA SoC Migor"); |
| 216 | MODULE_LICENSE("GPL v2"); |