Linux Audio

Check our new training course

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