xref: /openbmc/linux/sound/soc/atmel/atmel-classd.c (revision 69295df0)
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;
35e0a25b6dSSongjun Wu 	struct clk *aclk;
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) {
169e0a25b6dSSongjun Wu 		dev_err(rtd->platform->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 
251e0a25b6dSSongjun Wu static int atmel_classd_codec_probe(struct snd_soc_codec *codec)
252e0a25b6dSSongjun Wu {
253e0a25b6dSSongjun Wu 	struct snd_soc_card *card = snd_soc_codec_get_drvdata(codec);
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);
287e0a25b6dSSongjun Wu 			dev_warn(codec->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 
294e0a25b6dSSongjun Wu 	snd_soc_update_bits(codec, CLASSD_MR, mask, val);
295e0a25b6dSSongjun Wu 
296e0a25b6dSSongjun Wu 	dev_info(codec->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 
304e0a25b6dSSongjun Wu static struct regmap *atmel_classd_codec_get_remap(struct device *dev)
305e0a25b6dSSongjun Wu {
306e0a25b6dSSongjun Wu 	return dev_get_regmap(dev, NULL);
307e0a25b6dSSongjun Wu }
308e0a25b6dSSongjun Wu 
309e0a25b6dSSongjun Wu static struct snd_soc_codec_driver soc_codec_dev_classd = {
310e0a25b6dSSongjun Wu 	.probe		= atmel_classd_codec_probe,
31169295df0SKuninori Morimoto 	.get_regmap	= atmel_classd_codec_get_remap,
31269295df0SKuninori Morimoto 	.component_driver = {
313e0a25b6dSSongjun Wu 		.controls		= atmel_classd_snd_controls,
314e0a25b6dSSongjun Wu 		.num_controls		= ARRAY_SIZE(atmel_classd_snd_controls),
31569295df0SKuninori Morimoto 	},
316e0a25b6dSSongjun Wu };
317e0a25b6dSSongjun Wu 
318e0a25b6dSSongjun Wu /* codec dai component */
319e0a25b6dSSongjun Wu static int atmel_classd_codec_dai_startup(struct snd_pcm_substream *substream,
320e0a25b6dSSongjun Wu 				struct snd_soc_dai *codec_dai)
321e0a25b6dSSongjun Wu {
322e0a25b6dSSongjun Wu 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
323e0a25b6dSSongjun Wu 	struct atmel_classd *dd = snd_soc_card_get_drvdata(rtd->card);
324e0a25b6dSSongjun Wu 	int ret;
325e0a25b6dSSongjun Wu 
326e0a25b6dSSongjun Wu 	ret = clk_prepare_enable(dd->aclk);
327e0a25b6dSSongjun Wu 	if (ret)
328e0a25b6dSSongjun Wu 		return ret;
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 {
336e0a25b6dSSongjun Wu 	struct snd_soc_codec *codec = codec_dai->codec;
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 
346e0a25b6dSSongjun Wu 	snd_soc_update_bits(codec, CLASSD_MR, mask, val);
347e0a25b6dSSongjun Wu 
348e0a25b6dSSongjun Wu 	return 0;
349e0a25b6dSSongjun Wu }
350e0a25b6dSSongjun Wu 
351e0a25b6dSSongjun Wu #define CLASSD_ACLK_RATE_11M2896_MPY_8 (112896 * 100 * 8)
352e0a25b6dSSongjun Wu #define CLASSD_ACLK_RATE_12M288_MPY_8  (12228 * 1000 * 8)
353e0a25b6dSSongjun Wu 
354e0a25b6dSSongjun Wu static struct {
355e0a25b6dSSongjun Wu 	int rate;
356e0a25b6dSSongjun Wu 	int sample_rate;
357e0a25b6dSSongjun Wu 	int dsp_clk;
358e0a25b6dSSongjun Wu 	unsigned long aclk_rate;
359e0a25b6dSSongjun Wu } const sample_rates[] = {
360e0a25b6dSSongjun Wu 	{ 8000,  CLASSD_INTPMR_FRAME_8K,
361e0a25b6dSSongjun Wu 	CLASSD_INTPMR_DSP_CLK_FREQ_12M288, CLASSD_ACLK_RATE_12M288_MPY_8 },
362e0a25b6dSSongjun Wu 	{ 16000, CLASSD_INTPMR_FRAME_16K,
363e0a25b6dSSongjun Wu 	CLASSD_INTPMR_DSP_CLK_FREQ_12M288, CLASSD_ACLK_RATE_12M288_MPY_8 },
364e0a25b6dSSongjun Wu 	{ 32000, CLASSD_INTPMR_FRAME_32K,
365e0a25b6dSSongjun Wu 	CLASSD_INTPMR_DSP_CLK_FREQ_12M288, CLASSD_ACLK_RATE_12M288_MPY_8 },
366e0a25b6dSSongjun Wu 	{ 48000, CLASSD_INTPMR_FRAME_48K,
367e0a25b6dSSongjun Wu 	CLASSD_INTPMR_DSP_CLK_FREQ_12M288, CLASSD_ACLK_RATE_12M288_MPY_8 },
368e0a25b6dSSongjun Wu 	{ 96000, CLASSD_INTPMR_FRAME_96K,
369e0a25b6dSSongjun Wu 	CLASSD_INTPMR_DSP_CLK_FREQ_12M288, CLASSD_ACLK_RATE_12M288_MPY_8 },
370e0a25b6dSSongjun Wu 	{ 22050, CLASSD_INTPMR_FRAME_22K,
371e0a25b6dSSongjun Wu 	CLASSD_INTPMR_DSP_CLK_FREQ_11M2896, CLASSD_ACLK_RATE_11M2896_MPY_8 },
372e0a25b6dSSongjun Wu 	{ 44100, CLASSD_INTPMR_FRAME_44K,
373e0a25b6dSSongjun Wu 	CLASSD_INTPMR_DSP_CLK_FREQ_11M2896, CLASSD_ACLK_RATE_11M2896_MPY_8 },
374e0a25b6dSSongjun Wu 	{ 88200, CLASSD_INTPMR_FRAME_88K,
375e0a25b6dSSongjun Wu 	CLASSD_INTPMR_DSP_CLK_FREQ_11M2896, CLASSD_ACLK_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);
385e0a25b6dSSongjun Wu 	struct snd_soc_codec *codec = codec_dai->codec;
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 
403e0a25b6dSSongjun Wu 	dev_dbg(codec->dev,
404e0a25b6dSSongjun Wu 		"Selected SAMPLE_RATE of %dHz, ACLK_RATE of %ldHz\n",
405e0a25b6dSSongjun Wu 		sample_rates[best].rate, sample_rates[best].aclk_rate);
406e0a25b6dSSongjun Wu 
407e0a25b6dSSongjun Wu 	clk_disable_unprepare(dd->gclk);
408e0a25b6dSSongjun Wu 	clk_disable_unprepare(dd->aclk);
409e0a25b6dSSongjun Wu 
410e0a25b6dSSongjun Wu 	ret = clk_set_rate(dd->aclk, sample_rates[best].aclk_rate);
411e0a25b6dSSongjun Wu 	if (ret)
412e0a25b6dSSongjun Wu 		return ret;
413e0a25b6dSSongjun Wu 
414e0a25b6dSSongjun Wu 	mask = CLASSD_INTPMR_DSP_CLK_FREQ_MASK | CLASSD_INTPMR_FRAME_MASK;
415e0a25b6dSSongjun Wu 	val = (sample_rates[best].dsp_clk << CLASSD_INTPMR_DSP_CLK_FREQ_SHIFT)
416e0a25b6dSSongjun Wu 	| (sample_rates[best].sample_rate << CLASSD_INTPMR_FRAME_SHIFT);
417e0a25b6dSSongjun Wu 
418e0a25b6dSSongjun Wu 	snd_soc_update_bits(codec, CLASSD_INTPMR, mask, val);
419e0a25b6dSSongjun Wu 
420e0a25b6dSSongjun Wu 	ret = clk_prepare_enable(dd->aclk);
421e0a25b6dSSongjun Wu 	if (ret)
422e0a25b6dSSongjun Wu 		return ret;
423e0a25b6dSSongjun Wu 
424e0a25b6dSSongjun Wu 	return clk_prepare_enable(dd->gclk);
425e0a25b6dSSongjun Wu }
426e0a25b6dSSongjun Wu 
427e0a25b6dSSongjun Wu static void
428e0a25b6dSSongjun Wu atmel_classd_codec_dai_shutdown(struct snd_pcm_substream *substream,
429e0a25b6dSSongjun Wu 			    struct snd_soc_dai *codec_dai)
430e0a25b6dSSongjun Wu {
431e0a25b6dSSongjun Wu 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
432e0a25b6dSSongjun Wu 	struct atmel_classd *dd = snd_soc_card_get_drvdata(rtd->card);
433e0a25b6dSSongjun Wu 
434e0a25b6dSSongjun Wu 	clk_disable_unprepare(dd->gclk);
435e0a25b6dSSongjun Wu 	clk_disable_unprepare(dd->aclk);
436e0a25b6dSSongjun Wu }
437e0a25b6dSSongjun Wu 
438e0a25b6dSSongjun Wu static int atmel_classd_codec_dai_prepare(struct snd_pcm_substream *substream,
439e0a25b6dSSongjun Wu 					struct snd_soc_dai *codec_dai)
440e0a25b6dSSongjun Wu {
441e0a25b6dSSongjun Wu 	struct snd_soc_codec *codec = codec_dai->codec;
442e0a25b6dSSongjun Wu 
443e0a25b6dSSongjun Wu 	snd_soc_update_bits(codec, CLASSD_MR,
444e0a25b6dSSongjun Wu 				CLASSD_MR_LEN_MASK | CLASSD_MR_REN_MASK,
445e0a25b6dSSongjun Wu 				(CLASSD_MR_LEN_DIS << CLASSD_MR_LEN_SHIFT)
446e0a25b6dSSongjun Wu 				|(CLASSD_MR_REN_DIS << CLASSD_MR_REN_SHIFT));
447e0a25b6dSSongjun Wu 
448e0a25b6dSSongjun Wu 	return 0;
449e0a25b6dSSongjun Wu }
450e0a25b6dSSongjun Wu 
451e0a25b6dSSongjun Wu static int atmel_classd_codec_dai_trigger(struct snd_pcm_substream *substream,
452e0a25b6dSSongjun Wu 					int cmd, struct snd_soc_dai *codec_dai)
453e0a25b6dSSongjun Wu {
454e0a25b6dSSongjun Wu 	struct snd_soc_codec *codec = codec_dai->codec;
455e0a25b6dSSongjun Wu 	u32 mask, val;
456e0a25b6dSSongjun Wu 
457e0a25b6dSSongjun Wu 	mask = CLASSD_MR_LEN_MASK | CLASSD_MR_REN_MASK;
458e0a25b6dSSongjun Wu 
459e0a25b6dSSongjun Wu 	switch (cmd) {
460e0a25b6dSSongjun Wu 	case SNDRV_PCM_TRIGGER_START:
461e0a25b6dSSongjun Wu 	case SNDRV_PCM_TRIGGER_RESUME:
462e0a25b6dSSongjun Wu 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
463e0a25b6dSSongjun Wu 		val = mask;
464e0a25b6dSSongjun Wu 		break;
465e0a25b6dSSongjun Wu 	case SNDRV_PCM_TRIGGER_STOP:
466e0a25b6dSSongjun Wu 	case SNDRV_PCM_TRIGGER_SUSPEND:
467e0a25b6dSSongjun Wu 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
468e0a25b6dSSongjun Wu 		val = (CLASSD_MR_LEN_DIS << CLASSD_MR_LEN_SHIFT)
469e0a25b6dSSongjun Wu 			| (CLASSD_MR_REN_DIS << CLASSD_MR_REN_SHIFT);
470e0a25b6dSSongjun Wu 		break;
471e0a25b6dSSongjun Wu 	default:
472e0a25b6dSSongjun Wu 		return -EINVAL;
473e0a25b6dSSongjun Wu 	}
474e0a25b6dSSongjun Wu 
475e0a25b6dSSongjun Wu 	snd_soc_update_bits(codec, CLASSD_MR, mask, val);
476e0a25b6dSSongjun Wu 
477e0a25b6dSSongjun Wu 	return 0;
478e0a25b6dSSongjun Wu }
479e0a25b6dSSongjun Wu 
480e0a25b6dSSongjun Wu static const struct snd_soc_dai_ops atmel_classd_codec_dai_ops = {
481e0a25b6dSSongjun Wu 	.digital_mute	= atmel_classd_codec_dai_digital_mute,
482e0a25b6dSSongjun Wu 	.startup	= atmel_classd_codec_dai_startup,
483e0a25b6dSSongjun Wu 	.shutdown	= atmel_classd_codec_dai_shutdown,
484e0a25b6dSSongjun Wu 	.hw_params	= atmel_classd_codec_dai_hw_params,
485e0a25b6dSSongjun Wu 	.prepare	= atmel_classd_codec_dai_prepare,
486e0a25b6dSSongjun Wu 	.trigger	= atmel_classd_codec_dai_trigger,
487e0a25b6dSSongjun Wu };
488e0a25b6dSSongjun Wu 
489e0a25b6dSSongjun Wu #define ATMEL_CLASSD_CODEC_DAI_NAME  "atmel-classd-hifi"
490e0a25b6dSSongjun Wu 
491e0a25b6dSSongjun Wu static struct snd_soc_dai_driver atmel_classd_codec_dai = {
492e0a25b6dSSongjun Wu 	.name = ATMEL_CLASSD_CODEC_DAI_NAME,
493e0a25b6dSSongjun Wu 	.playback = {
494e0a25b6dSSongjun Wu 		.stream_name	= "Playback",
49507c55d39SSongjun Wu 		.channels_min	= 1,
496e0a25b6dSSongjun Wu 		.channels_max	= 2,
497e0a25b6dSSongjun Wu 		.rates		= ATMEL_CLASSD_RATES,
498e0a25b6dSSongjun Wu 		.formats	= SNDRV_PCM_FMTBIT_S16_LE,
499e0a25b6dSSongjun Wu 	},
500e0a25b6dSSongjun Wu 	.ops = &atmel_classd_codec_dai_ops,
501e0a25b6dSSongjun Wu };
502e0a25b6dSSongjun Wu 
503e0a25b6dSSongjun Wu /* ASoC sound card */
504e0a25b6dSSongjun Wu static int atmel_classd_asoc_card_init(struct device *dev,
505e0a25b6dSSongjun Wu 					struct snd_soc_card *card)
506e0a25b6dSSongjun Wu {
507e0a25b6dSSongjun Wu 	struct snd_soc_dai_link *dai_link;
508e0a25b6dSSongjun Wu 	struct atmel_classd *dd = snd_soc_card_get_drvdata(card);
509e0a25b6dSSongjun Wu 
510e0a25b6dSSongjun Wu 	dai_link = devm_kzalloc(dev, sizeof(*dai_link), GFP_KERNEL);
511e0a25b6dSSongjun Wu 	if (!dai_link)
512e0a25b6dSSongjun Wu 		return -ENOMEM;
513e0a25b6dSSongjun Wu 
514e0a25b6dSSongjun Wu 	dai_link->name			= "CLASSD";
515e0a25b6dSSongjun Wu 	dai_link->stream_name		= "CLASSD PCM";
516e0a25b6dSSongjun Wu 	dai_link->codec_dai_name	= ATMEL_CLASSD_CODEC_DAI_NAME;
517e0a25b6dSSongjun Wu 	dai_link->cpu_dai_name		= dev_name(dev);
518e0a25b6dSSongjun Wu 	dai_link->codec_name		= dev_name(dev);
519e0a25b6dSSongjun Wu 	dai_link->platform_name		= dev_name(dev);
520e0a25b6dSSongjun Wu 
521e0a25b6dSSongjun Wu 	card->dai_link	= dai_link;
522e0a25b6dSSongjun Wu 	card->num_links	= 1;
523e0a25b6dSSongjun Wu 	card->name	= dd->pdata->card_name;
524e0a25b6dSSongjun Wu 	card->dev	= dev;
525e0a25b6dSSongjun Wu 
526e0a25b6dSSongjun Wu 	return 0;
527e0a25b6dSSongjun Wu };
528e0a25b6dSSongjun Wu 
529e0a25b6dSSongjun Wu /* regmap configuration */
530e0a25b6dSSongjun Wu static const struct reg_default atmel_classd_reg_defaults[] = {
531e0a25b6dSSongjun Wu 	{ CLASSD_INTPMR,   0x00301212 },
532e0a25b6dSSongjun Wu };
533e0a25b6dSSongjun Wu 
534e0a25b6dSSongjun Wu #define ATMEL_CLASSD_REG_MAX    0xE4
535e0a25b6dSSongjun Wu static const struct regmap_config atmel_classd_regmap_config = {
536e0a25b6dSSongjun Wu 	.reg_bits	= 32,
537e0a25b6dSSongjun Wu 	.reg_stride	= 4,
538e0a25b6dSSongjun Wu 	.val_bits	= 32,
539e0a25b6dSSongjun Wu 	.max_register	= ATMEL_CLASSD_REG_MAX,
540e0a25b6dSSongjun Wu 
541e0a25b6dSSongjun Wu 	.cache_type		= REGCACHE_FLAT,
542e0a25b6dSSongjun Wu 	.reg_defaults		= atmel_classd_reg_defaults,
543e0a25b6dSSongjun Wu 	.num_reg_defaults	= ARRAY_SIZE(atmel_classd_reg_defaults),
544e0a25b6dSSongjun Wu };
545e0a25b6dSSongjun Wu 
546e0a25b6dSSongjun Wu static int atmel_classd_probe(struct platform_device *pdev)
547e0a25b6dSSongjun Wu {
548e0a25b6dSSongjun Wu 	struct device *dev = &pdev->dev;
549e0a25b6dSSongjun Wu 	struct atmel_classd *dd;
550e0a25b6dSSongjun Wu 	struct resource *res;
551e0a25b6dSSongjun Wu 	void __iomem *io_base;
552e0a25b6dSSongjun Wu 	const struct atmel_classd_pdata *pdata;
553e0a25b6dSSongjun Wu 	struct snd_soc_card *card;
554e0a25b6dSSongjun Wu 	int ret;
555e0a25b6dSSongjun Wu 
556e0a25b6dSSongjun Wu 	pdata = dev_get_platdata(dev);
557e0a25b6dSSongjun Wu 	if (!pdata) {
558e0a25b6dSSongjun Wu 		pdata = atmel_classd_dt_init(dev);
559e0a25b6dSSongjun Wu 		if (IS_ERR(pdata))
560e0a25b6dSSongjun Wu 			return PTR_ERR(pdata);
561e0a25b6dSSongjun Wu 	}
562e0a25b6dSSongjun Wu 
563e0a25b6dSSongjun Wu 	dd = devm_kzalloc(dev, sizeof(*dd), GFP_KERNEL);
564e0a25b6dSSongjun Wu 	if (!dd)
565e0a25b6dSSongjun Wu 		return -ENOMEM;
566e0a25b6dSSongjun Wu 
567e0a25b6dSSongjun Wu 	dd->pdata = pdata;
568e0a25b6dSSongjun Wu 
569e0a25b6dSSongjun Wu 	dd->irq = platform_get_irq(pdev, 0);
570e0a25b6dSSongjun Wu 	if (dd->irq < 0) {
571e0a25b6dSSongjun Wu 		ret = dd->irq;
572e0a25b6dSSongjun Wu 		dev_err(dev, "failed to could not get irq: %d\n", ret);
573e0a25b6dSSongjun Wu 		return ret;
574e0a25b6dSSongjun Wu 	}
575e0a25b6dSSongjun Wu 
576e0a25b6dSSongjun Wu 	dd->pclk = devm_clk_get(dev, "pclk");
577e0a25b6dSSongjun Wu 	if (IS_ERR(dd->pclk)) {
578e0a25b6dSSongjun Wu 		ret = PTR_ERR(dd->pclk);
579e0a25b6dSSongjun Wu 		dev_err(dev, "failed to get peripheral clock: %d\n", ret);
580e0a25b6dSSongjun Wu 		return ret;
581e0a25b6dSSongjun Wu 	}
582e0a25b6dSSongjun Wu 
583e0a25b6dSSongjun Wu 	dd->gclk = devm_clk_get(dev, "gclk");
584e0a25b6dSSongjun Wu 	if (IS_ERR(dd->gclk)) {
585e0a25b6dSSongjun Wu 		ret = PTR_ERR(dd->gclk);
586e0a25b6dSSongjun Wu 		dev_err(dev, "failed to get GCK clock: %d\n", ret);
587e0a25b6dSSongjun Wu 		return ret;
588e0a25b6dSSongjun Wu 	}
589e0a25b6dSSongjun Wu 
590e0a25b6dSSongjun Wu 	dd->aclk = devm_clk_get(dev, "aclk");
591e0a25b6dSSongjun Wu 	if (IS_ERR(dd->aclk)) {
592e0a25b6dSSongjun Wu 		ret = PTR_ERR(dd->aclk);
593e0a25b6dSSongjun Wu 		dev_err(dev, "failed to get audio 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);
599e0a25b6dSSongjun Wu 	if (IS_ERR(io_base)) {
600e0a25b6dSSongjun Wu 		ret =  PTR_ERR(io_base);
601e0a25b6dSSongjun Wu 		dev_err(dev, "failed to remap register memory: %d\n", ret);
602e0a25b6dSSongjun Wu 		return ret;
603e0a25b6dSSongjun Wu 	}
604e0a25b6dSSongjun Wu 
605e0a25b6dSSongjun Wu 	dd->phy_base = res->start;
606e0a25b6dSSongjun Wu 
607e0a25b6dSSongjun Wu 	dd->regmap = devm_regmap_init_mmio(dev, io_base,
608e0a25b6dSSongjun Wu 					&atmel_classd_regmap_config);
609e0a25b6dSSongjun Wu 	if (IS_ERR(dd->regmap)) {
610e0a25b6dSSongjun Wu 		ret = PTR_ERR(dd->regmap);
611e0a25b6dSSongjun Wu 		dev_err(dev, "failed to init register map: %d\n", ret);
612e0a25b6dSSongjun Wu 		return ret;
613e0a25b6dSSongjun Wu 	}
614e0a25b6dSSongjun Wu 
615e0a25b6dSSongjun Wu 	ret = devm_snd_soc_register_component(dev,
616e0a25b6dSSongjun Wu 					&atmel_classd_cpu_dai_component,
617e0a25b6dSSongjun Wu 					&atmel_classd_cpu_dai, 1);
618e0a25b6dSSongjun Wu 	if (ret) {
619e0a25b6dSSongjun Wu 		dev_err(dev, "could not register CPU DAI: %d\n", ret);
620e0a25b6dSSongjun Wu 		return ret;
621e0a25b6dSSongjun Wu 	}
622e0a25b6dSSongjun Wu 
623e0a25b6dSSongjun Wu 	ret = devm_snd_dmaengine_pcm_register(dev,
624e0a25b6dSSongjun Wu 					&atmel_classd_dmaengine_pcm_config,
625e0a25b6dSSongjun Wu 					0);
626e0a25b6dSSongjun Wu 	if (ret) {
627e0a25b6dSSongjun Wu 		dev_err(dev, "could not register platform: %d\n", ret);
628e0a25b6dSSongjun Wu 		return ret;
629e0a25b6dSSongjun Wu 	}
630e0a25b6dSSongjun Wu 
631e0a25b6dSSongjun Wu 	ret = snd_soc_register_codec(dev, &soc_codec_dev_classd,
632e0a25b6dSSongjun Wu 					&atmel_classd_codec_dai, 1);
633e0a25b6dSSongjun Wu 	if (ret) {
634e0a25b6dSSongjun Wu 		dev_err(dev, "could not register codec: %d\n", ret);
635e0a25b6dSSongjun Wu 		return ret;
636e0a25b6dSSongjun Wu 	}
637e0a25b6dSSongjun Wu 
638e0a25b6dSSongjun Wu 	/* register sound card */
639e0a25b6dSSongjun Wu 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
64032e69badSSongjun Wu 	if (!card) {
64132e69badSSongjun Wu 		ret = -ENOMEM;
64232e69badSSongjun Wu 		goto unregister_codec;
64332e69badSSongjun Wu 	}
644e0a25b6dSSongjun Wu 
645e0a25b6dSSongjun Wu 	snd_soc_card_set_drvdata(card, dd);
646e0a25b6dSSongjun Wu 	platform_set_drvdata(pdev, card);
647e0a25b6dSSongjun Wu 
648e0a25b6dSSongjun Wu 	ret = atmel_classd_asoc_card_init(dev, card);
649e0a25b6dSSongjun Wu 	if (ret) {
650e0a25b6dSSongjun Wu 		dev_err(dev, "failed to init sound card\n");
65132e69badSSongjun Wu 		goto unregister_codec;
652e0a25b6dSSongjun Wu 	}
653e0a25b6dSSongjun Wu 
654e0a25b6dSSongjun Wu 	ret = devm_snd_soc_register_card(dev, card);
655e0a25b6dSSongjun Wu 	if (ret) {
656e0a25b6dSSongjun Wu 		dev_err(dev, "failed to register sound card: %d\n", ret);
65732e69badSSongjun Wu 		goto unregister_codec;
658e0a25b6dSSongjun Wu 	}
659e0a25b6dSSongjun Wu 
660e0a25b6dSSongjun Wu 	return 0;
66132e69badSSongjun Wu 
66232e69badSSongjun Wu unregister_codec:
66332e69badSSongjun Wu 	snd_soc_unregister_codec(dev);
66432e69badSSongjun Wu 	return ret;
665e0a25b6dSSongjun Wu }
666e0a25b6dSSongjun Wu 
667e0a25b6dSSongjun Wu static int atmel_classd_remove(struct platform_device *pdev)
668e0a25b6dSSongjun Wu {
669e0a25b6dSSongjun Wu 	snd_soc_unregister_codec(&pdev->dev);
670e0a25b6dSSongjun Wu 	return 0;
671e0a25b6dSSongjun Wu }
672e0a25b6dSSongjun Wu 
673e0a25b6dSSongjun Wu static struct platform_driver atmel_classd_driver = {
674e0a25b6dSSongjun Wu 	.driver	= {
675e0a25b6dSSongjun Wu 		.name		= "atmel-classd",
676e0a25b6dSSongjun Wu 		.of_match_table	= of_match_ptr(atmel_classd_of_match),
677e0a25b6dSSongjun Wu 		.pm		= &snd_soc_pm_ops,
678e0a25b6dSSongjun Wu 	},
679e0a25b6dSSongjun Wu 	.probe	= atmel_classd_probe,
680e0a25b6dSSongjun Wu 	.remove	= atmel_classd_remove,
681e0a25b6dSSongjun Wu };
682e0a25b6dSSongjun Wu module_platform_driver(atmel_classd_driver);
683e0a25b6dSSongjun Wu 
684e0a25b6dSSongjun Wu MODULE_DESCRIPTION("Atmel ClassD driver under ALSA SoC architecture");
685e0a25b6dSSongjun Wu MODULE_AUTHOR("Songjun Wu <songjun.wu@atmel.com>");
686e0a25b6dSSongjun Wu MODULE_LICENSE("GPL");
687