xref: /openbmc/linux/sound/soc/atmel/atmel-classd.c (revision 3d14a1df)
1e0a25b6dSSongjun Wu /* Atmel ALSA SoC Audio Class D Amplifier (CLASSD) driver
2e0a25b6dSSongjun Wu  *
3e0a25b6dSSongjun Wu  * Copyright (C) 2015 Atmel
4e0a25b6dSSongjun Wu  *
5e0a25b6dSSongjun Wu  * Author: Songjun Wu <songjun.wu@atmel.com>
6e0a25b6dSSongjun Wu  *
7e0a25b6dSSongjun Wu  * This program is free software; you can redistribute it and/or modify
8e0a25b6dSSongjun Wu  * it under the terms of the GNU General Public License version 2 or later
9e0a25b6dSSongjun Wu  * as published by the Free Software Foundation.
10e0a25b6dSSongjun Wu  */
11e0a25b6dSSongjun Wu 
12e0a25b6dSSongjun Wu #include <linux/of.h>
13e0a25b6dSSongjun Wu #include <linux/clk.h>
14e0a25b6dSSongjun Wu #include <linux/module.h>
15e0a25b6dSSongjun Wu #include <linux/platform_device.h>
16e0a25b6dSSongjun Wu #include <linux/regmap.h>
17e0a25b6dSSongjun Wu #include <sound/core.h>
18e0a25b6dSSongjun Wu #include <sound/dmaengine_pcm.h>
19e0a25b6dSSongjun Wu #include <sound/pcm_params.h>
20e0a25b6dSSongjun Wu #include <sound/tlv.h>
21e0a25b6dSSongjun Wu #include "atmel-classd.h"
22e0a25b6dSSongjun Wu 
23e0a25b6dSSongjun Wu struct atmel_classd_pdata {
24e0a25b6dSSongjun Wu 	bool non_overlap_enable;
25e0a25b6dSSongjun Wu 	int non_overlap_time;
26e0a25b6dSSongjun Wu 	int pwm_type;
27e0a25b6dSSongjun Wu 	const char *card_name;
28e0a25b6dSSongjun Wu };
29e0a25b6dSSongjun Wu 
30e0a25b6dSSongjun Wu struct atmel_classd {
31e0a25b6dSSongjun Wu 	dma_addr_t phy_base;
32e0a25b6dSSongjun Wu 	struct regmap *regmap;
33e0a25b6dSSongjun Wu 	struct clk *pclk;
34e0a25b6dSSongjun Wu 	struct clk *gclk;
356dea9df8SKuninori Morimoto 	struct device *dev;
36e0a25b6dSSongjun Wu 	int irq;
37e0a25b6dSSongjun Wu 	const struct atmel_classd_pdata *pdata;
38e0a25b6dSSongjun Wu };
39e0a25b6dSSongjun Wu 
40e0a25b6dSSongjun Wu #ifdef CONFIG_OF
41e0a25b6dSSongjun Wu static const struct of_device_id atmel_classd_of_match[] = {
42e0a25b6dSSongjun Wu 	{
43e0a25b6dSSongjun Wu 		.compatible = "atmel,sama5d2-classd",
44e0a25b6dSSongjun Wu 	}, {
45e0a25b6dSSongjun Wu 		/* sentinel */
46e0a25b6dSSongjun Wu 	}
47e0a25b6dSSongjun Wu };
48e0a25b6dSSongjun Wu MODULE_DEVICE_TABLE(of, atmel_classd_of_match);
49e0a25b6dSSongjun Wu 
50e0a25b6dSSongjun Wu static struct atmel_classd_pdata *atmel_classd_dt_init(struct device *dev)
51e0a25b6dSSongjun Wu {
52e0a25b6dSSongjun Wu 	struct device_node *np = dev->of_node;
53e0a25b6dSSongjun Wu 	struct atmel_classd_pdata *pdata;
54e0a25b6dSSongjun Wu 	const char *pwm_type;
55e0a25b6dSSongjun Wu 	int ret;
56e0a25b6dSSongjun Wu 
57e0a25b6dSSongjun Wu 	if (!np) {
58e0a25b6dSSongjun Wu 		dev_err(dev, "device node not found\n");
59e0a25b6dSSongjun Wu 		return ERR_PTR(-EINVAL);
60e0a25b6dSSongjun Wu 	}
61e0a25b6dSSongjun Wu 
62e0a25b6dSSongjun Wu 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
63e0a25b6dSSongjun Wu 	if (!pdata)
64e0a25b6dSSongjun Wu 		return ERR_PTR(-ENOMEM);
65e0a25b6dSSongjun Wu 
66e0a25b6dSSongjun Wu 	ret = of_property_read_string(np, "atmel,pwm-type", &pwm_type);
67e0a25b6dSSongjun Wu 	if ((ret == 0) && (strcmp(pwm_type, "diff") == 0))
68e0a25b6dSSongjun Wu 		pdata->pwm_type = CLASSD_MR_PWMTYP_DIFF;
69e0a25b6dSSongjun Wu 	else
70e0a25b6dSSongjun Wu 		pdata->pwm_type = CLASSD_MR_PWMTYP_SINGLE;
71e0a25b6dSSongjun Wu 
72e0a25b6dSSongjun Wu 	ret = of_property_read_u32(np,
73e0a25b6dSSongjun Wu 			"atmel,non-overlap-time", &pdata->non_overlap_time);
74e0a25b6dSSongjun Wu 	if (ret)
75e0a25b6dSSongjun Wu 		pdata->non_overlap_enable = false;
76e0a25b6dSSongjun Wu 	else
77e0a25b6dSSongjun Wu 		pdata->non_overlap_enable = true;
78e0a25b6dSSongjun Wu 
79e0a25b6dSSongjun Wu 	ret = of_property_read_string(np, "atmel,model", &pdata->card_name);
80e0a25b6dSSongjun Wu 	if (ret)
81e0a25b6dSSongjun Wu 		pdata->card_name = "CLASSD";
82e0a25b6dSSongjun Wu 
83e0a25b6dSSongjun Wu 	return pdata;
84e0a25b6dSSongjun Wu }
85e0a25b6dSSongjun Wu #else
86e0a25b6dSSongjun Wu static inline struct atmel_classd_pdata *
87e0a25b6dSSongjun Wu atmel_classd_dt_init(struct device *dev)
88e0a25b6dSSongjun Wu {
89e0a25b6dSSongjun Wu 	return ERR_PTR(-EINVAL);
90e0a25b6dSSongjun Wu }
91e0a25b6dSSongjun Wu #endif
92e0a25b6dSSongjun Wu 
93e0a25b6dSSongjun Wu #define ATMEL_CLASSD_RATES (SNDRV_PCM_RATE_8000 \
94e0a25b6dSSongjun Wu 			| SNDRV_PCM_RATE_16000	| SNDRV_PCM_RATE_22050 \
95e0a25b6dSSongjun Wu 			| SNDRV_PCM_RATE_32000	| SNDRV_PCM_RATE_44100 \
96e0a25b6dSSongjun Wu 			| SNDRV_PCM_RATE_48000	| SNDRV_PCM_RATE_88200 \
97e0a25b6dSSongjun Wu 			| SNDRV_PCM_RATE_96000)
98e0a25b6dSSongjun Wu 
99e0a25b6dSSongjun Wu static const struct snd_pcm_hardware atmel_classd_hw = {
100e0a25b6dSSongjun Wu 	.info			= SNDRV_PCM_INFO_MMAP
101e0a25b6dSSongjun Wu 				| SNDRV_PCM_INFO_MMAP_VALID
102e0a25b6dSSongjun Wu 				| SNDRV_PCM_INFO_INTERLEAVED
103e0a25b6dSSongjun Wu 				| SNDRV_PCM_INFO_RESUME
104e0a25b6dSSongjun Wu 				| SNDRV_PCM_INFO_PAUSE,
105e0a25b6dSSongjun Wu 	.formats		= (SNDRV_PCM_FMTBIT_S16_LE),
106e0a25b6dSSongjun Wu 	.rates			= ATMEL_CLASSD_RATES,
107e0a25b6dSSongjun Wu 	.rate_min		= 8000,
108e0a25b6dSSongjun Wu 	.rate_max		= 96000,
10907c55d39SSongjun Wu 	.channels_min		= 1,
110e0a25b6dSSongjun Wu 	.channels_max		= 2,
111e0a25b6dSSongjun Wu 	.buffer_bytes_max	= 64 * 1024,
112e0a25b6dSSongjun Wu 	.period_bytes_min	= 256,
113e0a25b6dSSongjun Wu 	.period_bytes_max	= 32 * 1024,
114e0a25b6dSSongjun Wu 	.periods_min		= 2,
115e0a25b6dSSongjun Wu 	.periods_max		= 256,
116e0a25b6dSSongjun Wu };
117e0a25b6dSSongjun Wu 
118e0a25b6dSSongjun Wu #define ATMEL_CLASSD_PREALLOC_BUF_SIZE  (64 * 1024)
119e0a25b6dSSongjun Wu 
120e0a25b6dSSongjun Wu /* cpu dai component */
121e0a25b6dSSongjun Wu static int atmel_classd_cpu_dai_startup(struct snd_pcm_substream *substream,
122e0a25b6dSSongjun Wu 					struct snd_soc_dai *cpu_dai)
123e0a25b6dSSongjun Wu {
124e0a25b6dSSongjun Wu 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
125e0a25b6dSSongjun Wu 	struct atmel_classd *dd = snd_soc_card_get_drvdata(rtd->card);
126e0a25b6dSSongjun Wu 
127e0a25b6dSSongjun Wu 	regmap_write(dd->regmap, CLASSD_THR, 0x0);
128e0a25b6dSSongjun Wu 
129e0a25b6dSSongjun Wu 	return clk_prepare_enable(dd->pclk);
130e0a25b6dSSongjun Wu }
131e0a25b6dSSongjun Wu 
132e0a25b6dSSongjun Wu static void atmel_classd_cpu_dai_shutdown(struct snd_pcm_substream *substream,
133e0a25b6dSSongjun Wu 					struct snd_soc_dai *cpu_dai)
134e0a25b6dSSongjun Wu {
135e0a25b6dSSongjun Wu 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
136e0a25b6dSSongjun Wu 	struct atmel_classd *dd = snd_soc_card_get_drvdata(rtd->card);
137e0a25b6dSSongjun Wu 
138e0a25b6dSSongjun Wu 	clk_disable_unprepare(dd->pclk);
139e0a25b6dSSongjun Wu }
140e0a25b6dSSongjun Wu 
141e0a25b6dSSongjun Wu static const struct snd_soc_dai_ops atmel_classd_cpu_dai_ops = {
142e0a25b6dSSongjun Wu 	.startup	= atmel_classd_cpu_dai_startup,
143e0a25b6dSSongjun Wu 	.shutdown	= atmel_classd_cpu_dai_shutdown,
144e0a25b6dSSongjun Wu };
145e0a25b6dSSongjun Wu 
146e0a25b6dSSongjun Wu static struct snd_soc_dai_driver atmel_classd_cpu_dai = {
147e0a25b6dSSongjun Wu 	.playback = {
14807c55d39SSongjun Wu 		.channels_min	= 1,
149e0a25b6dSSongjun Wu 		.channels_max	= 2,
150e0a25b6dSSongjun Wu 		.rates		= ATMEL_CLASSD_RATES,
151e0a25b6dSSongjun Wu 		.formats	= SNDRV_PCM_FMTBIT_S16_LE,},
152e0a25b6dSSongjun Wu 	.ops = &atmel_classd_cpu_dai_ops,
153e0a25b6dSSongjun Wu };
154e0a25b6dSSongjun Wu 
155e0a25b6dSSongjun Wu static const struct snd_soc_component_driver atmel_classd_cpu_dai_component = {
156e0a25b6dSSongjun Wu 	.name = "atmel-classd",
157e0a25b6dSSongjun Wu };
158e0a25b6dSSongjun Wu 
159e0a25b6dSSongjun Wu /* platform */
160e0a25b6dSSongjun Wu static int
161e0a25b6dSSongjun Wu atmel_classd_platform_configure_dma(struct snd_pcm_substream *substream,
162e0a25b6dSSongjun Wu 	struct snd_pcm_hw_params *params,
163e0a25b6dSSongjun Wu 	struct dma_slave_config *slave_config)
164e0a25b6dSSongjun Wu {
165e0a25b6dSSongjun Wu 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
166e0a25b6dSSongjun Wu 	struct atmel_classd *dd = snd_soc_card_get_drvdata(rtd->card);
167e0a25b6dSSongjun Wu 
168e0a25b6dSSongjun Wu 	if (params_physical_width(params) != 16) {
1696dea9df8SKuninori Morimoto 		dev_err(dd->dev,
170e0a25b6dSSongjun Wu 			"only supports 16-bit audio data\n");
171e0a25b6dSSongjun Wu 		return -EINVAL;
172e0a25b6dSSongjun Wu 	}
173e0a25b6dSSongjun Wu 
17407c55d39SSongjun Wu 	if (params_channels(params) == 1)
17507c55d39SSongjun Wu 		slave_config->dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
17607c55d39SSongjun Wu 	else
17707c55d39SSongjun Wu 		slave_config->dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
17807c55d39SSongjun Wu 
179e0a25b6dSSongjun Wu 	slave_config->direction		= DMA_MEM_TO_DEV;
180e0a25b6dSSongjun Wu 	slave_config->dst_addr		= dd->phy_base + CLASSD_THR;
181e0a25b6dSSongjun Wu 	slave_config->dst_maxburst	= 1;
182e0a25b6dSSongjun Wu 	slave_config->src_maxburst	= 1;
183e0a25b6dSSongjun Wu 	slave_config->device_fc		= false;
184e0a25b6dSSongjun Wu 
185e0a25b6dSSongjun Wu 	return 0;
186e0a25b6dSSongjun Wu }
187e0a25b6dSSongjun Wu 
188e0a25b6dSSongjun Wu static const struct snd_dmaengine_pcm_config
189e0a25b6dSSongjun Wu atmel_classd_dmaengine_pcm_config = {
190e0a25b6dSSongjun Wu 	.prepare_slave_config	= atmel_classd_platform_configure_dma,
191e0a25b6dSSongjun Wu 	.pcm_hardware		= &atmel_classd_hw,
192e0a25b6dSSongjun Wu 	.prealloc_buffer_size	= ATMEL_CLASSD_PREALLOC_BUF_SIZE,
193e0a25b6dSSongjun Wu };
194e0a25b6dSSongjun Wu 
195e0a25b6dSSongjun Wu /* codec */
196e0a25b6dSSongjun Wu static const char * const mono_mode_text[] = {
197e0a25b6dSSongjun Wu 	"mix", "sat", "left", "right"
198e0a25b6dSSongjun Wu };
199e0a25b6dSSongjun Wu 
200e0a25b6dSSongjun Wu static SOC_ENUM_SINGLE_DECL(classd_mono_mode_enum,
201e0a25b6dSSongjun Wu 			CLASSD_INTPMR, CLASSD_INTPMR_MONO_MODE_SHIFT,
202e0a25b6dSSongjun Wu 			mono_mode_text);
203e0a25b6dSSongjun Wu 
204e0a25b6dSSongjun Wu static const char * const eqcfg_text[] = {
205e0a25b6dSSongjun Wu 	"Treble-12dB", "Treble-6dB",
206e0a25b6dSSongjun Wu 	"Medium-8dB", "Medium-3dB",
207e0a25b6dSSongjun Wu 	"Bass-12dB", "Bass-6dB",
208e0a25b6dSSongjun Wu 	"0 dB",
209e0a25b6dSSongjun Wu 	"Bass+6dB", "Bass+12dB",
210e0a25b6dSSongjun Wu 	"Medium+3dB", "Medium+8dB",
211e0a25b6dSSongjun Wu 	"Treble+6dB", "Treble+12dB",
212e0a25b6dSSongjun Wu };
213e0a25b6dSSongjun Wu 
214e0a25b6dSSongjun Wu static const unsigned int eqcfg_value[] = {
215e0a25b6dSSongjun Wu 	CLASSD_INTPMR_EQCFG_T_CUT_12, CLASSD_INTPMR_EQCFG_T_CUT_6,
216e0a25b6dSSongjun Wu 	CLASSD_INTPMR_EQCFG_M_CUT_8, CLASSD_INTPMR_EQCFG_M_CUT_3,
217e0a25b6dSSongjun Wu 	CLASSD_INTPMR_EQCFG_B_CUT_12, CLASSD_INTPMR_EQCFG_B_CUT_6,
218e0a25b6dSSongjun Wu 	CLASSD_INTPMR_EQCFG_FLAT,
219e0a25b6dSSongjun Wu 	CLASSD_INTPMR_EQCFG_B_BOOST_6, CLASSD_INTPMR_EQCFG_B_BOOST_12,
220e0a25b6dSSongjun Wu 	CLASSD_INTPMR_EQCFG_M_BOOST_3, CLASSD_INTPMR_EQCFG_M_BOOST_8,
221e0a25b6dSSongjun Wu 	CLASSD_INTPMR_EQCFG_T_BOOST_6, CLASSD_INTPMR_EQCFG_T_BOOST_12,
222e0a25b6dSSongjun Wu };
223e0a25b6dSSongjun Wu 
224e0a25b6dSSongjun Wu static SOC_VALUE_ENUM_SINGLE_DECL(classd_eqcfg_enum,
225e0a25b6dSSongjun Wu 		CLASSD_INTPMR, CLASSD_INTPMR_EQCFG_SHIFT, 0xf,
226e0a25b6dSSongjun Wu 		eqcfg_text, eqcfg_value);
227e0a25b6dSSongjun Wu 
228e0a25b6dSSongjun Wu static const DECLARE_TLV_DB_SCALE(classd_digital_tlv, -7800, 100, 1);
229e0a25b6dSSongjun Wu 
230e0a25b6dSSongjun Wu static const struct snd_kcontrol_new atmel_classd_snd_controls[] = {
231e0a25b6dSSongjun Wu SOC_DOUBLE_TLV("Playback Volume", CLASSD_INTPMR,
232e0a25b6dSSongjun Wu 		CLASSD_INTPMR_ATTL_SHIFT, CLASSD_INTPMR_ATTR_SHIFT,
233e0a25b6dSSongjun Wu 		78, 1, classd_digital_tlv),
234e0a25b6dSSongjun Wu 
235e0a25b6dSSongjun Wu SOC_SINGLE("Deemphasis Switch", CLASSD_INTPMR,
236e0a25b6dSSongjun Wu 		CLASSD_INTPMR_DEEMP_SHIFT, 1, 0),
237e0a25b6dSSongjun Wu 
238e0a25b6dSSongjun Wu SOC_SINGLE("Mono Switch", CLASSD_INTPMR, CLASSD_INTPMR_MONO_SHIFT, 1, 0),
239e0a25b6dSSongjun Wu 
240e0a25b6dSSongjun Wu SOC_SINGLE("Swap Switch", CLASSD_INTPMR, CLASSD_INTPMR_SWAP_SHIFT, 1, 0),
241e0a25b6dSSongjun Wu 
242e0a25b6dSSongjun Wu SOC_ENUM("Mono Mode", classd_mono_mode_enum),
243e0a25b6dSSongjun Wu 
244e0a25b6dSSongjun Wu SOC_ENUM("EQ", classd_eqcfg_enum),
245e0a25b6dSSongjun Wu };
246e0a25b6dSSongjun Wu 
247e0a25b6dSSongjun Wu static const char * const pwm_type[] = {
248e0a25b6dSSongjun Wu 	"Single ended", "Differential"
249e0a25b6dSSongjun Wu };
250e0a25b6dSSongjun Wu 
2511e8ba922SKuninori Morimoto static int atmel_classd_component_probe(struct snd_soc_component *component)
252e0a25b6dSSongjun Wu {
2531e8ba922SKuninori Morimoto 	struct snd_soc_card *card = snd_soc_component_get_drvdata(component);
254e0a25b6dSSongjun Wu 	struct atmel_classd *dd = snd_soc_card_get_drvdata(card);
255e0a25b6dSSongjun Wu 	const struct atmel_classd_pdata *pdata = dd->pdata;
256e0a25b6dSSongjun Wu 	u32 mask, val;
257e0a25b6dSSongjun Wu 
258e0a25b6dSSongjun Wu 	mask = CLASSD_MR_PWMTYP_MASK;
259e0a25b6dSSongjun Wu 	val = pdata->pwm_type << CLASSD_MR_PWMTYP_SHIFT;
260e0a25b6dSSongjun Wu 
261e0a25b6dSSongjun Wu 	mask |= CLASSD_MR_NON_OVERLAP_MASK;
262e0a25b6dSSongjun Wu 	if (pdata->non_overlap_enable) {
263e0a25b6dSSongjun Wu 		val |= (CLASSD_MR_NON_OVERLAP_EN
264e0a25b6dSSongjun Wu 			<< CLASSD_MR_NON_OVERLAP_SHIFT);
265e0a25b6dSSongjun Wu 
266e0a25b6dSSongjun Wu 		mask |= CLASSD_MR_NOVR_VAL_MASK;
267e0a25b6dSSongjun Wu 		switch (pdata->non_overlap_time) {
268e0a25b6dSSongjun Wu 		case 5:
269e0a25b6dSSongjun Wu 			val |= (CLASSD_MR_NOVR_VAL_5NS
270e0a25b6dSSongjun Wu 				<< CLASSD_MR_NOVR_VAL_SHIFT);
271e0a25b6dSSongjun Wu 			break;
272e0a25b6dSSongjun Wu 		case 10:
273e0a25b6dSSongjun Wu 			val |= (CLASSD_MR_NOVR_VAL_10NS
274e0a25b6dSSongjun Wu 				<< CLASSD_MR_NOVR_VAL_SHIFT);
275e0a25b6dSSongjun Wu 			break;
276e0a25b6dSSongjun Wu 		case 15:
277e0a25b6dSSongjun Wu 			val |= (CLASSD_MR_NOVR_VAL_15NS
278e0a25b6dSSongjun Wu 				<< CLASSD_MR_NOVR_VAL_SHIFT);
279e0a25b6dSSongjun Wu 			break;
280e0a25b6dSSongjun Wu 		case 20:
281e0a25b6dSSongjun Wu 			val |= (CLASSD_MR_NOVR_VAL_20NS
282e0a25b6dSSongjun Wu 				<< CLASSD_MR_NOVR_VAL_SHIFT);
283e0a25b6dSSongjun Wu 			break;
284e0a25b6dSSongjun Wu 		default:
285e0a25b6dSSongjun Wu 			val |= (CLASSD_MR_NOVR_VAL_10NS
286e0a25b6dSSongjun Wu 				<< CLASSD_MR_NOVR_VAL_SHIFT);
2871e8ba922SKuninori Morimoto 			dev_warn(component->dev,
288e0a25b6dSSongjun Wu 				"non-overlapping value %d is invalid, the default value 10 is specified\n",
289e0a25b6dSSongjun Wu 				pdata->non_overlap_time);
290e0a25b6dSSongjun Wu 			break;
291e0a25b6dSSongjun Wu 		}
292e0a25b6dSSongjun Wu 	}
293e0a25b6dSSongjun Wu 
2941e8ba922SKuninori Morimoto 	snd_soc_component_update_bits(component, CLASSD_MR, mask, val);
295e0a25b6dSSongjun Wu 
2961e8ba922SKuninori Morimoto 	dev_info(component->dev,
297e0a25b6dSSongjun Wu 		"PWM modulation type is %s, non-overlapping is %s\n",
298e0a25b6dSSongjun Wu 		pwm_type[pdata->pwm_type],
299e0a25b6dSSongjun Wu 		pdata->non_overlap_enable?"enabled":"disabled");
300e0a25b6dSSongjun Wu 
301e0a25b6dSSongjun Wu 	return 0;
302e0a25b6dSSongjun Wu }
303e0a25b6dSSongjun Wu 
3041e8ba922SKuninori Morimoto static int atmel_classd_component_resume(struct snd_soc_component *component)
30561abce13SQuentin Schulz {
3061e8ba922SKuninori Morimoto 	struct snd_soc_card *card = snd_soc_component_get_drvdata(component);
30761abce13SQuentin Schulz 	struct atmel_classd *dd = snd_soc_card_get_drvdata(card);
30861abce13SQuentin Schulz 
30961abce13SQuentin Schulz 	return regcache_sync(dd->regmap);
31061abce13SQuentin Schulz }
31161abce13SQuentin Schulz 
3121e8ba922SKuninori Morimoto static struct snd_soc_component_driver soc_component_dev_classd = {
3131e8ba922SKuninori Morimoto 	.probe			= atmel_classd_component_probe,
3141e8ba922SKuninori Morimoto 	.resume			= atmel_classd_component_resume,
315e0a25b6dSSongjun Wu 	.controls		= atmel_classd_snd_controls,
316e0a25b6dSSongjun Wu 	.num_controls		= ARRAY_SIZE(atmel_classd_snd_controls),
3171e8ba922SKuninori Morimoto 	.idle_bias_on		= 1,
3181e8ba922SKuninori Morimoto 	.use_pmdown_time	= 1,
3191e8ba922SKuninori Morimoto 	.endianness		= 1,
3201e8ba922SKuninori Morimoto 	.non_legacy_dai_naming	= 1,
321e0a25b6dSSongjun Wu };
322e0a25b6dSSongjun Wu 
323e0a25b6dSSongjun Wu /* codec dai component */
324e0a25b6dSSongjun Wu static int atmel_classd_codec_dai_startup(struct snd_pcm_substream *substream,
325e0a25b6dSSongjun Wu 				struct snd_soc_dai *codec_dai)
326e0a25b6dSSongjun Wu {
327e0a25b6dSSongjun Wu 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
328e0a25b6dSSongjun Wu 	struct atmel_classd *dd = snd_soc_card_get_drvdata(rtd->card);
329e0a25b6dSSongjun Wu 
330e0a25b6dSSongjun Wu 	return clk_prepare_enable(dd->gclk);
331e0a25b6dSSongjun Wu }
332e0a25b6dSSongjun Wu 
333e0a25b6dSSongjun Wu static int atmel_classd_codec_dai_digital_mute(struct snd_soc_dai *codec_dai,
334e0a25b6dSSongjun Wu 	int mute)
335e0a25b6dSSongjun Wu {
3361e8ba922SKuninori Morimoto 	struct snd_soc_component *component = codec_dai->component;
337e0a25b6dSSongjun Wu 	u32 mask, val;
338e0a25b6dSSongjun Wu 
339e0a25b6dSSongjun Wu 	mask = CLASSD_MR_LMUTE_MASK | CLASSD_MR_RMUTE_MASK;
340e0a25b6dSSongjun Wu 
341e0a25b6dSSongjun Wu 	if (mute)
342e0a25b6dSSongjun Wu 		val = mask;
343e0a25b6dSSongjun Wu 	else
344e0a25b6dSSongjun Wu 		val = 0;
345e0a25b6dSSongjun Wu 
3461e8ba922SKuninori Morimoto 	snd_soc_component_update_bits(component, CLASSD_MR, mask, val);
347e0a25b6dSSongjun Wu 
348e0a25b6dSSongjun Wu 	return 0;
349e0a25b6dSSongjun Wu }
350e0a25b6dSSongjun Wu 
3514ab6cf11SQuentin Schulz #define CLASSD_GCLK_RATE_11M2896_MPY_8 (112896 * 100 * 8)
3524ab6cf11SQuentin Schulz #define CLASSD_GCLK_RATE_12M288_MPY_8  (12288 * 1000 * 8)
353e0a25b6dSSongjun Wu 
354e0a25b6dSSongjun Wu static struct {
355e0a25b6dSSongjun Wu 	int rate;
356e0a25b6dSSongjun Wu 	int sample_rate;
357e0a25b6dSSongjun Wu 	int dsp_clk;
3584ab6cf11SQuentin Schulz 	unsigned long gclk_rate;
359e0a25b6dSSongjun Wu } const sample_rates[] = {
360e0a25b6dSSongjun Wu 	{ 8000,  CLASSD_INTPMR_FRAME_8K,
3614ab6cf11SQuentin Schulz 	CLASSD_INTPMR_DSP_CLK_FREQ_12M288, CLASSD_GCLK_RATE_12M288_MPY_8 },
362e0a25b6dSSongjun Wu 	{ 16000, CLASSD_INTPMR_FRAME_16K,
3634ab6cf11SQuentin Schulz 	CLASSD_INTPMR_DSP_CLK_FREQ_12M288, CLASSD_GCLK_RATE_12M288_MPY_8 },
364e0a25b6dSSongjun Wu 	{ 32000, CLASSD_INTPMR_FRAME_32K,
3654ab6cf11SQuentin Schulz 	CLASSD_INTPMR_DSP_CLK_FREQ_12M288, CLASSD_GCLK_RATE_12M288_MPY_8 },
366e0a25b6dSSongjun Wu 	{ 48000, CLASSD_INTPMR_FRAME_48K,
3674ab6cf11SQuentin Schulz 	CLASSD_INTPMR_DSP_CLK_FREQ_12M288, CLASSD_GCLK_RATE_12M288_MPY_8 },
368e0a25b6dSSongjun Wu 	{ 96000, CLASSD_INTPMR_FRAME_96K,
3694ab6cf11SQuentin Schulz 	CLASSD_INTPMR_DSP_CLK_FREQ_12M288, CLASSD_GCLK_RATE_12M288_MPY_8 },
370e0a25b6dSSongjun Wu 	{ 22050, CLASSD_INTPMR_FRAME_22K,
3714ab6cf11SQuentin Schulz 	CLASSD_INTPMR_DSP_CLK_FREQ_11M2896, CLASSD_GCLK_RATE_11M2896_MPY_8 },
372e0a25b6dSSongjun Wu 	{ 44100, CLASSD_INTPMR_FRAME_44K,
3734ab6cf11SQuentin Schulz 	CLASSD_INTPMR_DSP_CLK_FREQ_11M2896, CLASSD_GCLK_RATE_11M2896_MPY_8 },
374e0a25b6dSSongjun Wu 	{ 88200, CLASSD_INTPMR_FRAME_88K,
3754ab6cf11SQuentin Schulz 	CLASSD_INTPMR_DSP_CLK_FREQ_11M2896, CLASSD_GCLK_RATE_11M2896_MPY_8 },
376e0a25b6dSSongjun Wu };
377e0a25b6dSSongjun Wu 
378e0a25b6dSSongjun Wu static int
379e0a25b6dSSongjun Wu atmel_classd_codec_dai_hw_params(struct snd_pcm_substream *substream,
380e0a25b6dSSongjun Wu 			    struct snd_pcm_hw_params *params,
381e0a25b6dSSongjun Wu 			    struct snd_soc_dai *codec_dai)
382e0a25b6dSSongjun Wu {
383e0a25b6dSSongjun Wu 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
384e0a25b6dSSongjun Wu 	struct atmel_classd *dd = snd_soc_card_get_drvdata(rtd->card);
3851e8ba922SKuninori Morimoto 	struct snd_soc_component *component = codec_dai->component;
386e0a25b6dSSongjun Wu 	int fs;
387e0a25b6dSSongjun Wu 	int i, best, best_val, cur_val, ret;
388e0a25b6dSSongjun Wu 	u32 mask, val;
389e0a25b6dSSongjun Wu 
390e0a25b6dSSongjun Wu 	fs = params_rate(params);
391e0a25b6dSSongjun Wu 
392e0a25b6dSSongjun Wu 	best = 0;
393e0a25b6dSSongjun Wu 	best_val = abs(fs - sample_rates[0].rate);
394e0a25b6dSSongjun Wu 	for (i = 1; i < ARRAY_SIZE(sample_rates); i++) {
395e0a25b6dSSongjun Wu 		/* Closest match */
396e0a25b6dSSongjun Wu 		cur_val = abs(fs - sample_rates[i].rate);
397e0a25b6dSSongjun Wu 		if (cur_val < best_val) {
398e0a25b6dSSongjun Wu 			best = i;
399e0a25b6dSSongjun Wu 			best_val = cur_val;
400e0a25b6dSSongjun Wu 		}
401e0a25b6dSSongjun Wu 	}
402e0a25b6dSSongjun Wu 
4031e8ba922SKuninori Morimoto 	dev_dbg(component->dev,
4044ab6cf11SQuentin Schulz 		"Selected SAMPLE_RATE of %dHz, GCLK_RATE of %ldHz\n",
4054ab6cf11SQuentin Schulz 		sample_rates[best].rate, sample_rates[best].gclk_rate);
406e0a25b6dSSongjun Wu 
407e0a25b6dSSongjun Wu 	clk_disable_unprepare(dd->gclk);
408e0a25b6dSSongjun Wu 
4094ab6cf11SQuentin Schulz 	ret = clk_set_rate(dd->gclk, sample_rates[best].gclk_rate);
410e0a25b6dSSongjun Wu 	if (ret)
411e0a25b6dSSongjun Wu 		return ret;
412e0a25b6dSSongjun Wu 
413e0a25b6dSSongjun Wu 	mask = CLASSD_INTPMR_DSP_CLK_FREQ_MASK | CLASSD_INTPMR_FRAME_MASK;
414e0a25b6dSSongjun Wu 	val = (sample_rates[best].dsp_clk << CLASSD_INTPMR_DSP_CLK_FREQ_SHIFT)
415e0a25b6dSSongjun Wu 	| (sample_rates[best].sample_rate << CLASSD_INTPMR_FRAME_SHIFT);
416e0a25b6dSSongjun Wu 
4171e8ba922SKuninori Morimoto 	snd_soc_component_update_bits(component, CLASSD_INTPMR, mask, val);
418e0a25b6dSSongjun Wu 
419e0a25b6dSSongjun Wu 	return clk_prepare_enable(dd->gclk);
420e0a25b6dSSongjun Wu }
421e0a25b6dSSongjun Wu 
422e0a25b6dSSongjun Wu static void
423e0a25b6dSSongjun Wu atmel_classd_codec_dai_shutdown(struct snd_pcm_substream *substream,
424e0a25b6dSSongjun Wu 			    struct snd_soc_dai *codec_dai)
425e0a25b6dSSongjun Wu {
426e0a25b6dSSongjun Wu 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
427e0a25b6dSSongjun Wu 	struct atmel_classd *dd = snd_soc_card_get_drvdata(rtd->card);
428e0a25b6dSSongjun Wu 
429e0a25b6dSSongjun Wu 	clk_disable_unprepare(dd->gclk);
430e0a25b6dSSongjun Wu }
431e0a25b6dSSongjun Wu 
432e0a25b6dSSongjun Wu static int atmel_classd_codec_dai_prepare(struct snd_pcm_substream *substream,
433e0a25b6dSSongjun Wu 					struct snd_soc_dai *codec_dai)
434e0a25b6dSSongjun Wu {
4351e8ba922SKuninori Morimoto 	struct snd_soc_component *component = codec_dai->component;
436e0a25b6dSSongjun Wu 
4371e8ba922SKuninori Morimoto 	snd_soc_component_update_bits(component, CLASSD_MR,
438e0a25b6dSSongjun Wu 				CLASSD_MR_LEN_MASK | CLASSD_MR_REN_MASK,
439e0a25b6dSSongjun Wu 				(CLASSD_MR_LEN_DIS << CLASSD_MR_LEN_SHIFT)
440e0a25b6dSSongjun Wu 				|(CLASSD_MR_REN_DIS << CLASSD_MR_REN_SHIFT));
441e0a25b6dSSongjun Wu 
442e0a25b6dSSongjun Wu 	return 0;
443e0a25b6dSSongjun Wu }
444e0a25b6dSSongjun Wu 
445e0a25b6dSSongjun Wu static int atmel_classd_codec_dai_trigger(struct snd_pcm_substream *substream,
446e0a25b6dSSongjun Wu 					int cmd, struct snd_soc_dai *codec_dai)
447e0a25b6dSSongjun Wu {
4481e8ba922SKuninori Morimoto 	struct snd_soc_component *component = codec_dai->component;
449e0a25b6dSSongjun Wu 	u32 mask, val;
450e0a25b6dSSongjun Wu 
451e0a25b6dSSongjun Wu 	mask = CLASSD_MR_LEN_MASK | CLASSD_MR_REN_MASK;
452e0a25b6dSSongjun Wu 
453e0a25b6dSSongjun Wu 	switch (cmd) {
454e0a25b6dSSongjun Wu 	case SNDRV_PCM_TRIGGER_START:
455e0a25b6dSSongjun Wu 	case SNDRV_PCM_TRIGGER_RESUME:
456e0a25b6dSSongjun Wu 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
457e0a25b6dSSongjun Wu 		val = mask;
458e0a25b6dSSongjun Wu 		break;
459e0a25b6dSSongjun Wu 	case SNDRV_PCM_TRIGGER_STOP:
460e0a25b6dSSongjun Wu 	case SNDRV_PCM_TRIGGER_SUSPEND:
461e0a25b6dSSongjun Wu 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
462e0a25b6dSSongjun Wu 		val = (CLASSD_MR_LEN_DIS << CLASSD_MR_LEN_SHIFT)
463e0a25b6dSSongjun Wu 			| (CLASSD_MR_REN_DIS << CLASSD_MR_REN_SHIFT);
464e0a25b6dSSongjun Wu 		break;
465e0a25b6dSSongjun Wu 	default:
466e0a25b6dSSongjun Wu 		return -EINVAL;
467e0a25b6dSSongjun Wu 	}
468e0a25b6dSSongjun Wu 
4691e8ba922SKuninori Morimoto 	snd_soc_component_update_bits(component, CLASSD_MR, mask, val);
470e0a25b6dSSongjun Wu 
471e0a25b6dSSongjun Wu 	return 0;
472e0a25b6dSSongjun Wu }
473e0a25b6dSSongjun Wu 
474e0a25b6dSSongjun Wu static const struct snd_soc_dai_ops atmel_classd_codec_dai_ops = {
475e0a25b6dSSongjun Wu 	.digital_mute	= atmel_classd_codec_dai_digital_mute,
476e0a25b6dSSongjun Wu 	.startup	= atmel_classd_codec_dai_startup,
477e0a25b6dSSongjun Wu 	.shutdown	= atmel_classd_codec_dai_shutdown,
478e0a25b6dSSongjun Wu 	.hw_params	= atmel_classd_codec_dai_hw_params,
479e0a25b6dSSongjun Wu 	.prepare	= atmel_classd_codec_dai_prepare,
480e0a25b6dSSongjun Wu 	.trigger	= atmel_classd_codec_dai_trigger,
481e0a25b6dSSongjun Wu };
482e0a25b6dSSongjun Wu 
483e0a25b6dSSongjun Wu #define ATMEL_CLASSD_CODEC_DAI_NAME  "atmel-classd-hifi"
484e0a25b6dSSongjun Wu 
485e0a25b6dSSongjun Wu static struct snd_soc_dai_driver atmel_classd_codec_dai = {
486e0a25b6dSSongjun Wu 	.name = ATMEL_CLASSD_CODEC_DAI_NAME,
487e0a25b6dSSongjun Wu 	.playback = {
488e0a25b6dSSongjun Wu 		.stream_name	= "Playback",
48907c55d39SSongjun Wu 		.channels_min	= 1,
490e0a25b6dSSongjun Wu 		.channels_max	= 2,
491e0a25b6dSSongjun Wu 		.rates		= ATMEL_CLASSD_RATES,
492e0a25b6dSSongjun Wu 		.formats	= SNDRV_PCM_FMTBIT_S16_LE,
493e0a25b6dSSongjun Wu 	},
494e0a25b6dSSongjun Wu 	.ops = &atmel_classd_codec_dai_ops,
495e0a25b6dSSongjun Wu };
496e0a25b6dSSongjun Wu 
497e0a25b6dSSongjun Wu /* ASoC sound card */
498e0a25b6dSSongjun Wu static int atmel_classd_asoc_card_init(struct device *dev,
499e0a25b6dSSongjun Wu 					struct snd_soc_card *card)
500e0a25b6dSSongjun Wu {
501e0a25b6dSSongjun Wu 	struct snd_soc_dai_link *dai_link;
502e0a25b6dSSongjun Wu 	struct atmel_classd *dd = snd_soc_card_get_drvdata(card);
5033d14a1dfSKuninori Morimoto 	struct snd_soc_dai_link_component *comp;
504e0a25b6dSSongjun Wu 
505e0a25b6dSSongjun Wu 	dai_link = devm_kzalloc(dev, sizeof(*dai_link), GFP_KERNEL);
506e0a25b6dSSongjun Wu 	if (!dai_link)
507e0a25b6dSSongjun Wu 		return -ENOMEM;
508e0a25b6dSSongjun Wu 
5093d14a1dfSKuninori Morimoto 	comp = devm_kzalloc(dev, 3 * sizeof(*comp), GFP_KERNEL);
5103d14a1dfSKuninori Morimoto 	if (!comp)
5113d14a1dfSKuninori Morimoto 		return -ENOMEM;
5123d14a1dfSKuninori Morimoto 
5133d14a1dfSKuninori Morimoto 	dai_link->cpus		= &comp[0];
5143d14a1dfSKuninori Morimoto 	dai_link->codecs	= &comp[1];
5153d14a1dfSKuninori Morimoto 	dai_link->platforms	= &comp[2];
5163d14a1dfSKuninori Morimoto 
5173d14a1dfSKuninori Morimoto 	dai_link->num_cpus	= 1;
5183d14a1dfSKuninori Morimoto 	dai_link->num_codecs	= 1;
5193d14a1dfSKuninori Morimoto 	dai_link->num_platforms	= 1;
5203d14a1dfSKuninori Morimoto 
521e0a25b6dSSongjun Wu 	dai_link->name			= "CLASSD";
522e0a25b6dSSongjun Wu 	dai_link->stream_name		= "CLASSD PCM";
5233d14a1dfSKuninori Morimoto 	dai_link->codecs->dai_name	= ATMEL_CLASSD_CODEC_DAI_NAME;
5243d14a1dfSKuninori Morimoto 	dai_link->cpus->dai_name	= dev_name(dev);
5253d14a1dfSKuninori Morimoto 	dai_link->codecs->name		= dev_name(dev);
5263d14a1dfSKuninori Morimoto 	dai_link->platforms->name	= dev_name(dev);
527e0a25b6dSSongjun Wu 
528e0a25b6dSSongjun Wu 	card->dai_link	= dai_link;
529e0a25b6dSSongjun Wu 	card->num_links	= 1;
530e0a25b6dSSongjun Wu 	card->name	= dd->pdata->card_name;
531e0a25b6dSSongjun Wu 	card->dev	= dev;
532e0a25b6dSSongjun Wu 
533e0a25b6dSSongjun Wu 	return 0;
534e0a25b6dSSongjun Wu };
535e0a25b6dSSongjun Wu 
536e0a25b6dSSongjun Wu /* regmap configuration */
537e0a25b6dSSongjun Wu static const struct reg_default atmel_classd_reg_defaults[] = {
538e0a25b6dSSongjun Wu 	{ CLASSD_INTPMR,   0x00301212 },
539e0a25b6dSSongjun Wu };
540e0a25b6dSSongjun Wu 
541e0a25b6dSSongjun Wu #define ATMEL_CLASSD_REG_MAX    0xE4
542e0a25b6dSSongjun Wu static const struct regmap_config atmel_classd_regmap_config = {
543e0a25b6dSSongjun Wu 	.reg_bits	= 32,
544e0a25b6dSSongjun Wu 	.reg_stride	= 4,
545e0a25b6dSSongjun Wu 	.val_bits	= 32,
546e0a25b6dSSongjun Wu 	.max_register	= ATMEL_CLASSD_REG_MAX,
547e0a25b6dSSongjun Wu 
548e0a25b6dSSongjun Wu 	.cache_type		= REGCACHE_FLAT,
549e0a25b6dSSongjun Wu 	.reg_defaults		= atmel_classd_reg_defaults,
550e0a25b6dSSongjun Wu 	.num_reg_defaults	= ARRAY_SIZE(atmel_classd_reg_defaults),
551e0a25b6dSSongjun Wu };
552e0a25b6dSSongjun Wu 
553e0a25b6dSSongjun Wu static int atmel_classd_probe(struct platform_device *pdev)
554e0a25b6dSSongjun Wu {
555e0a25b6dSSongjun Wu 	struct device *dev = &pdev->dev;
556e0a25b6dSSongjun Wu 	struct atmel_classd *dd;
557e0a25b6dSSongjun Wu 	struct resource *res;
558e0a25b6dSSongjun Wu 	void __iomem *io_base;
559e0a25b6dSSongjun Wu 	const struct atmel_classd_pdata *pdata;
560e0a25b6dSSongjun Wu 	struct snd_soc_card *card;
561e0a25b6dSSongjun Wu 	int ret;
562e0a25b6dSSongjun Wu 
563e0a25b6dSSongjun Wu 	pdata = dev_get_platdata(dev);
564e0a25b6dSSongjun Wu 	if (!pdata) {
565e0a25b6dSSongjun Wu 		pdata = atmel_classd_dt_init(dev);
566e0a25b6dSSongjun Wu 		if (IS_ERR(pdata))
567e0a25b6dSSongjun Wu 			return PTR_ERR(pdata);
568e0a25b6dSSongjun Wu 	}
569e0a25b6dSSongjun Wu 
570e0a25b6dSSongjun Wu 	dd = devm_kzalloc(dev, sizeof(*dd), GFP_KERNEL);
571e0a25b6dSSongjun Wu 	if (!dd)
572e0a25b6dSSongjun Wu 		return -ENOMEM;
573e0a25b6dSSongjun Wu 
574e0a25b6dSSongjun Wu 	dd->pdata = pdata;
575e0a25b6dSSongjun Wu 
576e0a25b6dSSongjun Wu 	dd->irq = platform_get_irq(pdev, 0);
577e0a25b6dSSongjun Wu 	if (dd->irq < 0) {
578e0a25b6dSSongjun Wu 		ret = dd->irq;
579e0a25b6dSSongjun Wu 		dev_err(dev, "failed to could not get irq: %d\n", ret);
580e0a25b6dSSongjun Wu 		return ret;
581e0a25b6dSSongjun Wu 	}
582e0a25b6dSSongjun Wu 
583e0a25b6dSSongjun Wu 	dd->pclk = devm_clk_get(dev, "pclk");
584e0a25b6dSSongjun Wu 	if (IS_ERR(dd->pclk)) {
585e0a25b6dSSongjun Wu 		ret = PTR_ERR(dd->pclk);
586e0a25b6dSSongjun Wu 		dev_err(dev, "failed to get peripheral clock: %d\n", ret);
587e0a25b6dSSongjun Wu 		return ret;
588e0a25b6dSSongjun Wu 	}
589e0a25b6dSSongjun Wu 
590e0a25b6dSSongjun Wu 	dd->gclk = devm_clk_get(dev, "gclk");
591e0a25b6dSSongjun Wu 	if (IS_ERR(dd->gclk)) {
592e0a25b6dSSongjun Wu 		ret = PTR_ERR(dd->gclk);
593e0a25b6dSSongjun Wu 		dev_err(dev, "failed to get GCK clock: %d\n", ret);
594e0a25b6dSSongjun Wu 		return ret;
595e0a25b6dSSongjun Wu 	}
596e0a25b6dSSongjun Wu 
597e0a25b6dSSongjun Wu 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
598e0a25b6dSSongjun Wu 	io_base = devm_ioremap_resource(dev, res);
5991f598e68SLadislav Michl 	if (IS_ERR(io_base))
6001f598e68SLadislav Michl 		return PTR_ERR(io_base);
601e0a25b6dSSongjun Wu 
602e0a25b6dSSongjun Wu 	dd->phy_base = res->start;
6036dea9df8SKuninori Morimoto 	dd->dev = dev;
604e0a25b6dSSongjun Wu 
605e0a25b6dSSongjun Wu 	dd->regmap = devm_regmap_init_mmio(dev, io_base,
606e0a25b6dSSongjun Wu 					&atmel_classd_regmap_config);
607e0a25b6dSSongjun Wu 	if (IS_ERR(dd->regmap)) {
608e0a25b6dSSongjun Wu 		ret = PTR_ERR(dd->regmap);
609e0a25b6dSSongjun Wu 		dev_err(dev, "failed to init register map: %d\n", ret);
610e0a25b6dSSongjun Wu 		return ret;
611e0a25b6dSSongjun Wu 	}
612e0a25b6dSSongjun Wu 
613e0a25b6dSSongjun Wu 	ret = devm_snd_soc_register_component(dev,
614e0a25b6dSSongjun Wu 					&atmel_classd_cpu_dai_component,
615e0a25b6dSSongjun Wu 					&atmel_classd_cpu_dai, 1);
616e0a25b6dSSongjun Wu 	if (ret) {
617e0a25b6dSSongjun Wu 		dev_err(dev, "could not register CPU DAI: %d\n", ret);
618e0a25b6dSSongjun Wu 		return ret;
619e0a25b6dSSongjun Wu 	}
620e0a25b6dSSongjun Wu 
621e0a25b6dSSongjun Wu 	ret = devm_snd_dmaengine_pcm_register(dev,
622e0a25b6dSSongjun Wu 					&atmel_classd_dmaengine_pcm_config,
623e0a25b6dSSongjun Wu 					0);
624e0a25b6dSSongjun Wu 	if (ret) {
625e0a25b6dSSongjun Wu 		dev_err(dev, "could not register platform: %d\n", ret);
626e0a25b6dSSongjun Wu 		return ret;
627e0a25b6dSSongjun Wu 	}
628e0a25b6dSSongjun Wu 
6291e8ba922SKuninori Morimoto 	ret = devm_snd_soc_register_component(dev, &soc_component_dev_classd,
630e0a25b6dSSongjun Wu 					&atmel_classd_codec_dai, 1);
631e0a25b6dSSongjun Wu 	if (ret) {
6321e8ba922SKuninori Morimoto 		dev_err(dev, "could not register component: %d\n", ret);
633e0a25b6dSSongjun Wu 		return ret;
634e0a25b6dSSongjun Wu 	}
635e0a25b6dSSongjun Wu 
636e0a25b6dSSongjun Wu 	/* register sound card */
637e0a25b6dSSongjun Wu 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
63832e69badSSongjun Wu 	if (!card) {
63932e69badSSongjun Wu 		ret = -ENOMEM;
64032e69badSSongjun Wu 		goto unregister_codec;
64132e69badSSongjun Wu 	}
642e0a25b6dSSongjun Wu 
643e0a25b6dSSongjun Wu 	snd_soc_card_set_drvdata(card, dd);
644e0a25b6dSSongjun Wu 
645e0a25b6dSSongjun Wu 	ret = atmel_classd_asoc_card_init(dev, card);
646e0a25b6dSSongjun Wu 	if (ret) {
647e0a25b6dSSongjun Wu 		dev_err(dev, "failed to init sound card\n");
64832e69badSSongjun Wu 		goto unregister_codec;
649e0a25b6dSSongjun Wu 	}
650e0a25b6dSSongjun Wu 
651e0a25b6dSSongjun Wu 	ret = devm_snd_soc_register_card(dev, card);
652e0a25b6dSSongjun Wu 	if (ret) {
653e0a25b6dSSongjun Wu 		dev_err(dev, "failed to register sound card: %d\n", ret);
65432e69badSSongjun Wu 		goto unregister_codec;
655e0a25b6dSSongjun Wu 	}
656e0a25b6dSSongjun Wu 
657e0a25b6dSSongjun Wu 	return 0;
65832e69badSSongjun Wu 
65932e69badSSongjun Wu unregister_codec:
66032e69badSSongjun Wu 	return ret;
661e0a25b6dSSongjun Wu }
662e0a25b6dSSongjun Wu 
663e0a25b6dSSongjun Wu static int atmel_classd_remove(struct platform_device *pdev)
664e0a25b6dSSongjun Wu {
665e0a25b6dSSongjun Wu 	return 0;
666e0a25b6dSSongjun Wu }
667e0a25b6dSSongjun Wu 
668e0a25b6dSSongjun Wu static struct platform_driver atmel_classd_driver = {
669e0a25b6dSSongjun Wu 	.driver	= {
670e0a25b6dSSongjun Wu 		.name		= "atmel-classd",
671e0a25b6dSSongjun Wu 		.of_match_table	= of_match_ptr(atmel_classd_of_match),
672e0a25b6dSSongjun Wu 		.pm		= &snd_soc_pm_ops,
673e0a25b6dSSongjun Wu 	},
674e0a25b6dSSongjun Wu 	.probe	= atmel_classd_probe,
675e0a25b6dSSongjun Wu 	.remove	= atmel_classd_remove,
676e0a25b6dSSongjun Wu };
677e0a25b6dSSongjun Wu module_platform_driver(atmel_classd_driver);
678e0a25b6dSSongjun Wu 
679e0a25b6dSSongjun Wu MODULE_DESCRIPTION("Atmel ClassD driver under ALSA SoC architecture");
680e0a25b6dSSongjun Wu MODULE_AUTHOR("Songjun Wu <songjun.wu@atmel.com>");
681e0a25b6dSSongjun Wu MODULE_LICENSE("GPL");
682