Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * sam9x5_wm8731   --	SoC audio for AT91SAM9X5-based boards
  4 *			that are using WM8731 as codec.
  5 *
  6 *  Copyright (C) 2011 Atmel,
  7 *		  Nicolas Ferre <nicolas.ferre@atmel.com>
  8 *
  9 *  Copyright (C) 2013 Paratronic,
 10 *		  Richard Genoud <richard.genoud@gmail.com>
 11 *
 12 * Based on sam9g20_wm8731.c by:
 13 * Sedji Gaouaou <sedji.gaouaou@atmel.com>
 
 
 
 
 
 
 14 */
 15#include <linux/of.h>
 16#include <linux/export.h>
 17#include <linux/module.h>
 18#include <linux/mod_devicetable.h>
 19#include <linux/platform_device.h>
 20#include <linux/device.h>
 21
 22#include <sound/soc.h>
 23#include <sound/soc-dai.h>
 24#include <sound/soc-dapm.h>
 25
 26#include "../codecs/wm8731.h"
 27#include "atmel_ssc_dai.h"
 28
 29
 30#define MCLK_RATE 12288000
 31
 32#define DRV_NAME "sam9x5-snd-wm8731"
 33
 34struct sam9x5_drvdata {
 35	int ssc_id;
 36};
 37
 38/*
 39 * Logic for a wm8731 as connected on a at91sam9x5ek based board.
 40 */
 41static int sam9x5_wm8731_init(struct snd_soc_pcm_runtime *rtd)
 42{
 43	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
 44	struct device *dev = rtd->dev;
 45	int ret;
 46
 47	dev_dbg(dev, "%s called\n", __func__);
 48
 49	/* set the codec system clock for DAC and ADC */
 50	ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK_XTAL,
 51				     MCLK_RATE, SND_SOC_CLOCK_IN);
 52	if (ret < 0) {
 53		dev_err(dev, "Failed to set WM8731 SYSCLK: %d\n", ret);
 54		return ret;
 55	}
 56
 57	return 0;
 58}
 59
 60/*
 61 * Audio paths on at91sam9x5ek board:
 62 *
 63 *  |A| ------------> |      | ---R----> Headphone Jack
 64 *  |T| <----\        |  WM  | ---L--/
 65 *  |9| ---> CLK <--> | 8731 | <--R----- Line In Jack
 66 *  |1| <------------ |      | <--L--/
 67 */
 68static const struct snd_soc_dapm_widget sam9x5_dapm_widgets[] = {
 69	SND_SOC_DAPM_HP("Headphone Jack", NULL),
 70	SND_SOC_DAPM_LINE("Line In Jack", NULL),
 71};
 72
 73static int sam9x5_wm8731_driver_probe(struct platform_device *pdev)
 74{
 75	struct device_node *np = pdev->dev.of_node;
 76	struct device_node *codec_np, *cpu_np;
 77	struct snd_soc_card *card;
 78	struct snd_soc_dai_link *dai;
 79	struct sam9x5_drvdata *priv;
 80	struct snd_soc_dai_link_component *comp;
 81	int ret;
 82
 83	if (!np) {
 84		dev_err(&pdev->dev, "No device node supplied\n");
 85		return -EINVAL;
 86	}
 87
 88	card = devm_kzalloc(&pdev->dev, sizeof(*card), GFP_KERNEL);
 89	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
 90	dai = devm_kzalloc(&pdev->dev, sizeof(*dai), GFP_KERNEL);
 91	comp = devm_kzalloc(&pdev->dev, 3 * sizeof(*comp), GFP_KERNEL);
 92	if (!dai || !card || !priv || !comp) {
 93		ret = -ENOMEM;
 94		goto out;
 95	}
 96
 97	snd_soc_card_set_drvdata(card, priv);
 98
 99	card->dev = &pdev->dev;
100	card->owner = THIS_MODULE;
101	card->dai_link = dai;
102	card->num_links = 1;
103	card->dapm_widgets = sam9x5_dapm_widgets;
104	card->num_dapm_widgets = ARRAY_SIZE(sam9x5_dapm_widgets);
105
106	dai->cpus = &comp[0];
107	dai->num_cpus = 1;
108	dai->codecs = &comp[1];
109	dai->num_codecs = 1;
110	dai->platforms = &comp[2];
111	dai->num_platforms = 1;
112
113	dai->name = "WM8731";
114	dai->stream_name = "WM8731 PCM";
115	dai->codecs->dai_name = "wm8731-hifi";
116	dai->init = sam9x5_wm8731_init;
117	dai->dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_NB_NF
118		| SND_SOC_DAIFMT_CBP_CFP;
119
120	ret = snd_soc_of_parse_card_name(card, "atmel,model");
121	if (ret) {
122		dev_err(&pdev->dev, "atmel,model node missing\n");
123		goto out;
124	}
125
126	ret = snd_soc_of_parse_audio_routing(card, "atmel,audio-routing");
127	if (ret) {
128		dev_err(&pdev->dev, "atmel,audio-routing node missing\n");
129		goto out;
130	}
131
132	codec_np = of_parse_phandle(np, "atmel,audio-codec", 0);
133	if (!codec_np) {
134		dev_err(&pdev->dev, "atmel,audio-codec node missing\n");
135		ret = -EINVAL;
136		goto out;
137	}
138
139	dai->codecs->of_node = codec_np;
140
141	cpu_np = of_parse_phandle(np, "atmel,ssc-controller", 0);
142	if (!cpu_np) {
143		dev_err(&pdev->dev, "atmel,ssc-controller node missing\n");
144		ret = -EINVAL;
145		goto out_put_codec_np;
146	}
147	dai->cpus->of_node = cpu_np;
148	dai->platforms->of_node = cpu_np;
149
150	priv->ssc_id = of_alias_get_id(cpu_np, "ssc");
151
152	ret = atmel_ssc_set_audio(priv->ssc_id);
153	if (ret != 0) {
154		dev_err(&pdev->dev, "Failed to set SSC %d for audio: %d\n",
 
155			ret, priv->ssc_id);
156		goto out_put_cpu_np;
157	}
158
159	ret = devm_snd_soc_register_card(&pdev->dev, card);
 
 
 
160	if (ret) {
161		dev_err(&pdev->dev, "Platform device allocation failed\n");
 
162		goto out_put_audio;
163	}
164
165	dev_dbg(&pdev->dev, "%s ok\n", __func__);
166
167	goto out_put_cpu_np;
168
169out_put_audio:
170	atmel_ssc_put_audio(priv->ssc_id);
171out_put_cpu_np:
172	of_node_put(cpu_np);
173out_put_codec_np:
174	of_node_put(codec_np);
175out:
176	return ret;
177}
178
179static void sam9x5_wm8731_driver_remove(struct platform_device *pdev)
180{
181	struct snd_soc_card *card = platform_get_drvdata(pdev);
182	struct sam9x5_drvdata *priv = card->drvdata;
183
 
184	atmel_ssc_put_audio(priv->ssc_id);
 
 
185}
186
187static const struct of_device_id sam9x5_wm8731_of_match[] = {
188	{ .compatible = "atmel,sam9x5-wm8731-audio", },
189	{},
190};
191MODULE_DEVICE_TABLE(of, sam9x5_wm8731_of_match);
192
193static struct platform_driver sam9x5_wm8731_driver = {
194	.driver = {
195		.name = DRV_NAME,
 
196		.of_match_table = of_match_ptr(sam9x5_wm8731_of_match),
197	},
198	.probe = sam9x5_wm8731_driver_probe,
199	.remove = sam9x5_wm8731_driver_remove,
200};
201module_platform_driver(sam9x5_wm8731_driver);
202
203/* Module information */
204MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
205MODULE_AUTHOR("Richard Genoud <richard.genoud@gmail.com>");
206MODULE_DESCRIPTION("ALSA SoC machine driver for AT91SAM9x5 - WM8731");
207MODULE_LICENSE("GPL");
208MODULE_ALIAS("platform:" DRV_NAME);
v3.15
 
  1/*
  2 * sam9x5_wm8731   --	SoC audio for AT91SAM9X5-based boards
  3 *			that are using WM8731 as codec.
  4 *
  5 *  Copyright (C) 2011 Atmel,
  6 *		  Nicolas Ferre <nicolas.ferre@atmel.com>
  7 *
  8 *  Copyright (C) 2013 Paratronic,
  9 *		  Richard Genoud <richard.genoud@gmail.com>
 10 *
 11 * Based on sam9g20_wm8731.c by:
 12 * Sedji Gaouaou <sedji.gaouaou@atmel.com>
 13 *
 14 *  This program is free software; you can redistribute  it and/or modify it
 15 *  under  the terms of  the GNU General  Public License as published by the
 16 *  Free Software Foundation;  either version 2 of the  License, or (at your
 17 *  option) any later version.
 18 *
 19 */
 20#include <linux/of.h>
 21#include <linux/export.h>
 22#include <linux/module.h>
 23#include <linux/mod_devicetable.h>
 24#include <linux/platform_device.h>
 25#include <linux/device.h>
 26
 27#include <sound/soc.h>
 28#include <sound/soc-dai.h>
 29#include <sound/soc-dapm.h>
 30
 31#include "../codecs/wm8731.h"
 32#include "atmel_ssc_dai.h"
 33
 34
 35#define MCLK_RATE 12288000
 36
 37#define DRV_NAME "sam9x5-snd-wm8731"
 38
 39struct sam9x5_drvdata {
 40	int ssc_id;
 41};
 42
 43/*
 44 * Logic for a wm8731 as connected on a at91sam9x5ek based board.
 45 */
 46static int sam9x5_wm8731_init(struct snd_soc_pcm_runtime *rtd)
 47{
 48	struct snd_soc_dai *codec_dai = rtd->codec_dai;
 49	struct device *dev = rtd->dev;
 50	int ret;
 51
 52	dev_dbg(dev, "ASoC: %s called\n", __func__);
 53
 54	/* set the codec system clock for DAC and ADC */
 55	ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK_XTAL,
 56				     MCLK_RATE, SND_SOC_CLOCK_IN);
 57	if (ret < 0) {
 58		dev_err(dev, "ASoC: Failed to set WM8731 SYSCLK: %d\n", ret);
 59		return ret;
 60	}
 61
 62	return 0;
 63}
 64
 65/*
 66 * Audio paths on at91sam9x5ek board:
 67 *
 68 *  |A| ------------> |      | ---R----> Headphone Jack
 69 *  |T| <----\        |  WM  | ---L--/
 70 *  |9| ---> CLK <--> | 8731 | <--R----- Line In Jack
 71 *  |1| <------------ |      | <--L--/
 72 */
 73static const struct snd_soc_dapm_widget sam9x5_dapm_widgets[] = {
 74	SND_SOC_DAPM_HP("Headphone Jack", NULL),
 75	SND_SOC_DAPM_LINE("Line In Jack", NULL),
 76};
 77
 78static int sam9x5_wm8731_driver_probe(struct platform_device *pdev)
 79{
 80	struct device_node *np = pdev->dev.of_node;
 81	struct device_node *codec_np, *cpu_np;
 82	struct snd_soc_card *card;
 83	struct snd_soc_dai_link *dai;
 84	struct sam9x5_drvdata *priv;
 
 85	int ret;
 86
 87	if (!np) {
 88		dev_err(&pdev->dev, "No device node supplied\n");
 89		return -EINVAL;
 90	}
 91
 92	card = devm_kzalloc(&pdev->dev, sizeof(*card), GFP_KERNEL);
 93	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
 94	dai = devm_kzalloc(&pdev->dev, sizeof(*dai), GFP_KERNEL);
 95	if (!dai || !card || !priv) {
 
 96		ret = -ENOMEM;
 97		goto out;
 98	}
 99
100	snd_soc_card_set_drvdata(card, priv);
101
102	card->dev = &pdev->dev;
103	card->owner = THIS_MODULE;
104	card->dai_link = dai;
105	card->num_links = 1;
106	card->dapm_widgets = sam9x5_dapm_widgets;
107	card->num_dapm_widgets = ARRAY_SIZE(sam9x5_dapm_widgets);
 
 
 
 
 
 
 
 
108	dai->name = "WM8731";
109	dai->stream_name = "WM8731 PCM";
110	dai->codec_dai_name = "wm8731-hifi";
111	dai->init = sam9x5_wm8731_init;
112	dai->dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_NB_NF
113		| SND_SOC_DAIFMT_CBM_CFM;
114
115	ret = snd_soc_of_parse_card_name(card, "atmel,model");
116	if (ret) {
117		dev_err(&pdev->dev, "atmel,model node missing\n");
118		goto out;
119	}
120
121	ret = snd_soc_of_parse_audio_routing(card, "atmel,audio-routing");
122	if (ret) {
123		dev_err(&pdev->dev, "atmel,audio-routing node missing\n");
124		goto out;
125	}
126
127	codec_np = of_parse_phandle(np, "atmel,audio-codec", 0);
128	if (!codec_np) {
129		dev_err(&pdev->dev, "atmel,audio-codec node missing\n");
130		ret = -EINVAL;
131		goto out;
132	}
133
134	dai->codec_of_node = codec_np;
135
136	cpu_np = of_parse_phandle(np, "atmel,ssc-controller", 0);
137	if (!cpu_np) {
138		dev_err(&pdev->dev, "atmel,ssc-controller node missing\n");
139		ret = -EINVAL;
140		goto out;
141	}
142	dai->cpu_of_node = cpu_np;
143	dai->platform_of_node = cpu_np;
144
145	priv->ssc_id = of_alias_get_id(cpu_np, "ssc");
146
147	ret = atmel_ssc_set_audio(priv->ssc_id);
148	if (ret != 0) {
149		dev_err(&pdev->dev,
150			"ASoC: Failed to set SSC %d for audio: %d\n",
151			ret, priv->ssc_id);
152		goto out;
153	}
154
155	of_node_put(codec_np);
156	of_node_put(cpu_np);
157
158	ret = snd_soc_register_card(card);
159	if (ret) {
160		dev_err(&pdev->dev,
161			"ASoC: Platform device allocation failed\n");
162		goto out_put_audio;
163	}
164
165	dev_dbg(&pdev->dev, "ASoC: %s ok\n", __func__);
166
167	return ret;
168
169out_put_audio:
170	atmel_ssc_put_audio(priv->ssc_id);
 
 
 
 
171out:
172	return ret;
173}
174
175static int sam9x5_wm8731_driver_remove(struct platform_device *pdev)
176{
177	struct snd_soc_card *card = platform_get_drvdata(pdev);
178	struct sam9x5_drvdata *priv = card->drvdata;
179
180	snd_soc_unregister_card(card);
181	atmel_ssc_put_audio(priv->ssc_id);
182
183	return 0;
184}
185
186static const struct of_device_id sam9x5_wm8731_of_match[] = {
187	{ .compatible = "atmel,sam9x5-wm8731-audio", },
188	{},
189};
190MODULE_DEVICE_TABLE(of, sam9x5_wm8731_of_match);
191
192static struct platform_driver sam9x5_wm8731_driver = {
193	.driver = {
194		.name = DRV_NAME,
195		.owner = THIS_MODULE,
196		.of_match_table = of_match_ptr(sam9x5_wm8731_of_match),
197	},
198	.probe = sam9x5_wm8731_driver_probe,
199	.remove = sam9x5_wm8731_driver_remove,
200};
201module_platform_driver(sam9x5_wm8731_driver);
202
203/* Module information */
204MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
205MODULE_AUTHOR("Richard Genoud <richard.genoud@gmail.com>");
206MODULE_DESCRIPTION("ALSA SoC machine driver for AT91SAM9x5 - WM8731");
207MODULE_LICENSE("GPL");
208MODULE_ALIAS("platform:" DRV_NAME);