Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0
  2//
  3// ak4613.c  --  Asahi Kasei ALSA Soc Audio driver
  4//
  5// Copyright (C) 2015 Renesas Electronics Corporation
  6// Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  7//
  8// Based on ak4642.c by Kuninori Morimoto
  9// Based on wm8731.c by Richard Purdie
 10// Based on ak4535.c by Richard Purdie
 11// Based on wm8753.c by Liam Girdwood
 12
 13/*
 14 *		+-------+
 15 *		|AK4613	|
 16 *	SDTO1 <-|	|
 17 *		|	|
 18 *	SDTI1 ->|	|
 19 *	SDTI2 ->|	|
 20 *	SDTI3 ->|	|
 21 *		+-------+
 22 *
 23 *	  +---+
 24 * clk	  |   |___________________________________________...
 25 *
 26 * [TDM512]
 27 * SDTO1  [L1][R1][L2][R2]
 28 * SDTI1  [L1][R1][L2][R2][L3][R3][L4][R4][L5][R5][L6][R6]
 29 *
 30 * [TDM256]
 31 * SDTO1  [L1][R1][L2][R2]
 32 * SDTI1  [L1][R1][L2][R2][L3][R3][L4][R4]
 33 * SDTI2  [L5][R5][L6][R6]
 34 *
 35 * [TDM128]
 36 * SDTO1  [L1][R1][L2][R2]
 37 * SDTI1  [L1][R1][L2][R2]
 38 * SDTI2  [L3][R3][L4][R4]
 39 * SDTI3  [L5][R5][L6][R6]
 40 *
 41 * [STEREO]
 42 *	Playback  2ch : SDTI1
 43 *	Capture   2ch : SDTO1
 44 *
 45 * [TDM512]
 46 *	Playback 12ch : SDTI1
 47 *	Capture   4ch : SDTO1
 48 *
 49 * [TDM256]
 50 *	Playback 12ch : SDTI1 + SDTI2
 51 *	Playback  8ch : SDTI1
 52 *	Capture   4ch : SDTO1
 53 *
 54 * [TDM128]
 55 *	Playback 12ch : SDTI1 + SDTI2 + SDTI3
 56 *	Playback  8ch : SDTI1 + SDTI2
 57 *	Playback  4ch : SDTI1
 58 *	Capture   4ch : SDTO1
 59 *
 60 *
 61 * !!! NOTE !!!
 62 *
 63 * Renesas is the only user of ak4613 on upstream so far,
 64 * but the chip connection is like below.
 65 * Thus, Renesas can't test all connection case.
 66 * Tested TDM is very limited.
 67 *
 68 * +-----+	+-----------+
 69 * | SoC |	|  AK4613   |
 70 * |     |<-----|SDTO1	 IN1|<-- Mic
 71 * |     |	|	 IN2|
 72 * |     |	|	    |
 73 * |     |----->|SDTI1	OUT1|--> Headphone
 74 * +-----+	|SDTI2	OUT2|
 75 *		|SDTI3	OUT3|
 76 *		|	OUT4|
 77 *		|	OUT5|
 78 *		|	OUT6|
 79 *		+-----------+
 80 *
 81 * Renesas SoC can handle [2,  6,8]    channels.
 82 * Ak4613      can handle [2,4,  8,12] channels.
 83 *
 84 * Because of above HW connection and available channels number,
 85 * Renesas could test are ...
 86 *
 87 *	[STEREO] Playback  2ch : SDTI1
 88 *		 Capture   2ch : SDTO1
 89 *	[TDM256] Playback  8ch : SDTI1 (*)
 90 *
 91 * (*) it used 8ch data between SoC <-> AK4613 on TDM256 mode,
 92 *     but could confirm is only first 2ch because only 1
 93 *     Headphone is connected.
 94 *
 95 * see
 96 *	AK4613_ENABLE_TDM_TEST
 97 */
 
 98#include <linux/clk.h>
 99#include <linux/delay.h>
100#include <linux/i2c.h>
101#include <linux/slab.h>
102#include <linux/of.h>
103#include <linux/of_graph.h>
104#include <linux/module.h>
105#include <linux/regmap.h>
106#include <sound/soc.h>
107#include <sound/pcm_params.h>
108#include <sound/tlv.h>
109
110#define PW_MGMT1	0x00 /* Power Management 1 */
111#define PW_MGMT2	0x01 /* Power Management 2 */
112#define PW_MGMT3	0x02 /* Power Management 3 */
113#define CTRL1		0x03 /* Control 1 */
114#define CTRL2		0x04 /* Control 2 */
115#define DEMP1		0x05 /* De-emphasis1 */
116#define DEMP2		0x06 /* De-emphasis2 */
117#define OFD		0x07 /* Overflow Detect */
118#define ZRD		0x08 /* Zero Detect */
119#define ICTRL		0x09 /* Input Control */
120#define OCTRL		0x0a /* Output Control */
121#define LOUT1		0x0b /* LOUT1 Volume Control */
122#define ROUT1		0x0c /* ROUT1 Volume Control */
123#define LOUT2		0x0d /* LOUT2 Volume Control */
124#define ROUT2		0x0e /* ROUT2 Volume Control */
125#define LOUT3		0x0f /* LOUT3 Volume Control */
126#define ROUT3		0x10 /* ROUT3 Volume Control */
127#define LOUT4		0x11 /* LOUT4 Volume Control */
128#define ROUT4		0x12 /* ROUT4 Volume Control */
129#define LOUT5		0x13 /* LOUT5 Volume Control */
130#define ROUT5		0x14 /* ROUT5 Volume Control */
131#define LOUT6		0x15 /* LOUT6 Volume Control */
132#define ROUT6		0x16 /* ROUT6 Volume Control */
133
134/* PW_MGMT1 */
135#define RSTN		BIT(0)
136#define PMDAC		BIT(1)
137#define PMADC		BIT(2)
138#define PMVR		BIT(3)
139
140/* PW_MGMT2 */
141#define PMAD_ALL	0x7
142
143/* PW_MGMT3 */
144#define PMDA_ALL	0x3f
145
146/* CTRL1 */
147#define DIF0		BIT(3)
148#define DIF1		BIT(4)
149#define DIF2		BIT(5)
150#define TDM0		BIT(6)
151#define TDM1		BIT(7)
152#define NO_FMT		(0xff)
153#define FMT_MASK	(0xf8)
154
155/* CTRL2 */
156#define DFS_MASK		(3 << 2)
157#define DFS_NORMAL_SPEED	(0 << 2)
158#define DFS_DOUBLE_SPEED	(1 << 2)
159#define DFS_QUAD_SPEED		(2 << 2)
160
161/* ICTRL */
162#define ICTRL_MASK	(0x3)
163
164/* OCTRL */
165#define OCTRL_MASK	(0x3F)
166
167/*
168 * configs
169 *
170 * 0x000000BA
171 *
172 * B : AK4613_CONFIG_SDTI_x
173 * A : AK4613_CONFIG_MODE_x
174 */
175#define AK4613_CONFIG_SET(priv, x)	 priv->configs |= AK4613_CONFIG_##x
176#define AK4613_CONFIG_GET(priv, x)	(priv->configs &  AK4613_CONFIG_##x##_MASK)
177
178/*
179 * AK4613_CONFIG_SDTI_x
180 *
181 * It indicates how many SDTIx is connected.
182 */
183#define AK4613_CONFIG_SDTI_MASK		(0xF << 4)
184#define AK4613_CONFIG_SDTI(x)		(((x) & 0xF) << 4)
185#define AK4613_CONFIG_SDTI_set(priv, x)	  AK4613_CONFIG_SET(priv, SDTI(x))
186#define AK4613_CONFIG_SDTI_get(priv)	((AK4613_CONFIG_GET(priv, SDTI) >> 4) & 0xF)
187
188/*
189 * AK4613_CONFIG_MODE_x
190 *
191 * Same as Ctrl1 :: TDM1/TDM0
192 * No shift is requested
193 * see
194 *	AK4613_CTRL1_TO_MODE()
195 *	Table 11/12/13/14
196 */
197#define AK4613_CONFIG_MODE_MASK		(0xF)
198#define AK4613_CONFIG_MODE_STEREO	(0x0)
199#define AK4613_CONFIG_MODE_TDM512	(0x1)
200#define AK4613_CONFIG_MODE_TDM256	(0x2)
201#define AK4613_CONFIG_MODE_TDM128	(0x3)
202
203/*
204 * !!!! FIXME !!!!
205 *
206 * Because of testable HW limitation, TDM256 8ch TDM was only tested.
207 * This driver uses AK4613_ENABLE_TDM_TEST instead of new DT property so far.
208 * Don't hesitate to update driver, you don't need to care compatible
209 * with Renesas.
210 *
211 * #define AK4613_ENABLE_TDM_TEST
212 */
213
214struct ak4613_interface {
215	unsigned int width;
216	unsigned int fmt;
217	u8 dif;
 
 
 
 
218};
219
220struct ak4613_priv {
221	struct mutex lock;
222	struct snd_pcm_hw_constraint_list constraint_rates;
223	struct snd_pcm_hw_constraint_list constraint_channels;
224	struct work_struct dummy_write_work;
225	struct snd_soc_component *component;
226	unsigned int rate;
227	unsigned int sysclk;
228
229	unsigned int fmt;
230	unsigned int configs;
231	int cnt;
232	u8 ctrl1;
233	u8 oc;
234	u8 ic;
 
235};
236
237/*
238 * Playback Volume
239 *
240 * max : 0x00 : 0 dB
241 *       ( 0.5 dB step )
242 * min : 0xFE : -127.0 dB
243 * mute: 0xFF
244 */
245static const DECLARE_TLV_DB_SCALE(out_tlv, -12750, 50, 1);
246
247static const struct snd_kcontrol_new ak4613_snd_controls[] = {
248	SOC_DOUBLE_R_TLV("Digital Playback Volume1", LOUT1, ROUT1,
249			 0, 0xFF, 1, out_tlv),
250	SOC_DOUBLE_R_TLV("Digital Playback Volume2", LOUT2, ROUT2,
251			 0, 0xFF, 1, out_tlv),
252	SOC_DOUBLE_R_TLV("Digital Playback Volume3", LOUT3, ROUT3,
253			 0, 0xFF, 1, out_tlv),
254	SOC_DOUBLE_R_TLV("Digital Playback Volume4", LOUT4, ROUT4,
255			 0, 0xFF, 1, out_tlv),
256	SOC_DOUBLE_R_TLV("Digital Playback Volume5", LOUT5, ROUT5,
257			 0, 0xFF, 1, out_tlv),
258	SOC_DOUBLE_R_TLV("Digital Playback Volume6", LOUT6, ROUT6,
259			 0, 0xFF, 1, out_tlv),
260};
261
262static const struct reg_default ak4613_reg[] = {
263	{ 0x0,  0x0f }, { 0x1,  0x07 }, { 0x2,  0x3f }, { 0x3,  0x20 },
264	{ 0x4,  0x20 }, { 0x5,  0x55 }, { 0x6,  0x05 }, { 0x7,  0x07 },
265	{ 0x8,  0x0f }, { 0x9,  0x07 }, { 0xa,  0x3f }, { 0xb,  0x00 },
266	{ 0xc,  0x00 }, { 0xd,  0x00 }, { 0xe,  0x00 }, { 0xf,  0x00 },
267	{ 0x10, 0x00 }, { 0x11, 0x00 }, { 0x12, 0x00 }, { 0x13, 0x00 },
268	{ 0x14, 0x00 }, { 0x15, 0x00 }, { 0x16, 0x00 },
269};
270
271/*
272 * CTRL1 register
273 * see
274 *	Table 11/12/13/14
275 */
276#define AUDIO_IFACE(_dif, _width, _fmt)		\
277	{					\
278		.dif	= _dif,			\
279		.width	= _width,		\
280		.fmt	= SND_SOC_DAIFMT_##_fmt,\
281	}
282static const struct ak4613_interface ak4613_iface[] = {
283	/* It doesn't support asymmetric format */
284
285	AUDIO_IFACE(0x03, 24, LEFT_J),
286	AUDIO_IFACE(0x04, 24, I2S),
287};
288#define AK4613_CTRL1_TO_MODE(priv)	((priv)->ctrl1 >> 6) /* AK4613_CONFIG_MODE_x */
289
290static const struct regmap_config ak4613_regmap_cfg = {
291	.reg_bits		= 8,
292	.val_bits		= 8,
293	.max_register		= 0x16,
294	.reg_defaults		= ak4613_reg,
295	.num_reg_defaults	= ARRAY_SIZE(ak4613_reg),
296	.cache_type		= REGCACHE_RBTREE,
297};
298
299static const struct of_device_id ak4613_of_match[] = {
300	{ .compatible = "asahi-kasei,ak4613",	.data = &ak4613_regmap_cfg },
301	{},
302};
303MODULE_DEVICE_TABLE(of, ak4613_of_match);
304
305static const struct i2c_device_id ak4613_i2c_id[] = {
306	{ "ak4613", (kernel_ulong_t)&ak4613_regmap_cfg },
307	{ }
308};
309MODULE_DEVICE_TABLE(i2c, ak4613_i2c_id);
310
311static const struct snd_soc_dapm_widget ak4613_dapm_widgets[] = {
312
313	/* Outputs */
314	SND_SOC_DAPM_OUTPUT("LOUT1"),
315	SND_SOC_DAPM_OUTPUT("LOUT2"),
316	SND_SOC_DAPM_OUTPUT("LOUT3"),
317	SND_SOC_DAPM_OUTPUT("LOUT4"),
318	SND_SOC_DAPM_OUTPUT("LOUT5"),
319	SND_SOC_DAPM_OUTPUT("LOUT6"),
320
321	SND_SOC_DAPM_OUTPUT("ROUT1"),
322	SND_SOC_DAPM_OUTPUT("ROUT2"),
323	SND_SOC_DAPM_OUTPUT("ROUT3"),
324	SND_SOC_DAPM_OUTPUT("ROUT4"),
325	SND_SOC_DAPM_OUTPUT("ROUT5"),
326	SND_SOC_DAPM_OUTPUT("ROUT6"),
327
328	/* Inputs */
329	SND_SOC_DAPM_INPUT("LIN1"),
330	SND_SOC_DAPM_INPUT("LIN2"),
331
332	SND_SOC_DAPM_INPUT("RIN1"),
333	SND_SOC_DAPM_INPUT("RIN2"),
334
335	/* DAC */
336	SND_SOC_DAPM_DAC("DAC1", NULL, PW_MGMT3, 0, 0),
337	SND_SOC_DAPM_DAC("DAC2", NULL, PW_MGMT3, 1, 0),
338	SND_SOC_DAPM_DAC("DAC3", NULL, PW_MGMT3, 2, 0),
339	SND_SOC_DAPM_DAC("DAC4", NULL, PW_MGMT3, 3, 0),
340	SND_SOC_DAPM_DAC("DAC5", NULL, PW_MGMT3, 4, 0),
341	SND_SOC_DAPM_DAC("DAC6", NULL, PW_MGMT3, 5, 0),
342
343	/* ADC */
344	SND_SOC_DAPM_ADC("ADC1", NULL, PW_MGMT2, 0, 0),
345	SND_SOC_DAPM_ADC("ADC2", NULL, PW_MGMT2, 1, 0),
346};
347
348static const struct snd_soc_dapm_route ak4613_intercon[] = {
349	{"LOUT1", NULL, "DAC1"},
350	{"LOUT2", NULL, "DAC2"},
351	{"LOUT3", NULL, "DAC3"},
352	{"LOUT4", NULL, "DAC4"},
353	{"LOUT5", NULL, "DAC5"},
354	{"LOUT6", NULL, "DAC6"},
355
356	{"ROUT1", NULL, "DAC1"},
357	{"ROUT2", NULL, "DAC2"},
358	{"ROUT3", NULL, "DAC3"},
359	{"ROUT4", NULL, "DAC4"},
360	{"ROUT5", NULL, "DAC5"},
361	{"ROUT6", NULL, "DAC6"},
362
363	{"DAC1", NULL, "Playback"},
364	{"DAC2", NULL, "Playback"},
365	{"DAC3", NULL, "Playback"},
366	{"DAC4", NULL, "Playback"},
367	{"DAC5", NULL, "Playback"},
368	{"DAC6", NULL, "Playback"},
369
370	{"Capture", NULL, "ADC1"},
371	{"Capture", NULL, "ADC2"},
372
373	{"ADC1", NULL, "LIN1"},
374	{"ADC2", NULL, "LIN2"},
375
376	{"ADC1", NULL, "RIN1"},
377	{"ADC2", NULL, "RIN2"},
378};
379
380static void ak4613_dai_shutdown(struct snd_pcm_substream *substream,
381			       struct snd_soc_dai *dai)
382{
383	struct snd_soc_component *component = dai->component;
384	struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);
385	struct device *dev = component->dev;
386
387	mutex_lock(&priv->lock);
388	priv->cnt--;
389	if (priv->cnt < 0) {
390		dev_err(dev, "unexpected counter error\n");
391		priv->cnt = 0;
392	}
393	if (!priv->cnt)
394		priv->ctrl1 = 0;
395	mutex_unlock(&priv->lock);
396}
397
398static void ak4613_hw_constraints(struct ak4613_priv *priv,
399				  struct snd_pcm_substream *substream)
400{
401	struct snd_pcm_runtime *runtime = substream->runtime;
402	static const unsigned int ak4613_rates[] = {
403		 32000,
404		 44100,
405		 48000,
406		 64000,
407		 88200,
408		 96000,
409		176400,
410		192000,
411	};
412#define AK4613_CHANNEL_2	 0
413#define AK4613_CHANNEL_4	 1
414#define AK4613_CHANNEL_8	 2
415#define AK4613_CHANNEL_12	 3
416#define AK4613_CHANNEL_NONE	-1
417	static const unsigned int ak4613_channels[] = {
418		[AK4613_CHANNEL_2]  =  2,
419		[AK4613_CHANNEL_4]  =  4,
420		[AK4613_CHANNEL_8]  =  8,
421		[AK4613_CHANNEL_12] = 12,
422	};
423#define MODE_MAX 4
424#define SDTx_MAX 4
425#define MASK(x) (1 << AK4613_CHANNEL_##x)
426	static const int mask_list[MODE_MAX][SDTx_MAX] = {
427		/* 				SDTO	 SDTIx1    SDTIx2		SDTIx3 */
428		[AK4613_CONFIG_MODE_STEREO] = { MASK(2), MASK(2),  MASK(2),		MASK(2)},
429		[AK4613_CONFIG_MODE_TDM512] = { MASK(4), MASK(12), MASK(12),		MASK(12)},
430		[AK4613_CONFIG_MODE_TDM256] = { MASK(4), MASK(8),  MASK(8)|MASK(12),	MASK(8)|MASK(12)},
431		[AK4613_CONFIG_MODE_TDM128] = { MASK(4), MASK(4),  MASK(4)|MASK(8),	MASK(4)|MASK(8)|MASK(12)},
432	};
433	struct snd_pcm_hw_constraint_list *constraint;
434	unsigned int mask;
435	unsigned int mode;
436	unsigned int fs;
437	int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
438	int sdti_num;
439	int i;
440
441	constraint		= &priv->constraint_rates;
442	constraint->list	= ak4613_rates;
443	constraint->mask	= 0;
444	constraint->count	= 0;
445
446	/*
447	 * Slave Mode
448	 *	Normal: [32kHz, 48kHz] : 256fs,384fs or 512fs
449	 *	Double: [64kHz, 96kHz] : 256fs
450	 *	Quad  : [128kHz,192kHz]: 128fs
451	 *
452	 * Master mode
453	 *	Normal: [32kHz, 48kHz] : 256fs or 512fs
454	 *	Double: [64kHz, 96kHz] : 256fs
455	 *	Quad  : [128kHz,192kHz]: 128fs
456	*/
457	for (i = 0; i < ARRAY_SIZE(ak4613_rates); i++) {
458		/* minimum fs on each range */
459		fs = (ak4613_rates[i] <= 96000) ? 256 : 128;
460
461		if (priv->sysclk >= ak4613_rates[i] * fs)
462			constraint->count = i + 1;
463	}
464
465	snd_pcm_hw_constraint_list(runtime, 0,
466				SNDRV_PCM_HW_PARAM_RATE, constraint);
467
468
469	sdti_num = AK4613_CONFIG_SDTI_get(priv);
470	if (WARN_ON(sdti_num >= SDTx_MAX))
471		return;
472
473	if (priv->cnt) {
474		/*
475		 * If it was already working,
476		 * the constraint is same as working mode.
477		 */
478		mode = AK4613_CTRL1_TO_MODE(priv);
479		mask = 0; /* no default */
480	} else {
481		/*
482		 * It is not yet working,
483		 * the constraint is based on board configs.
484		 * STEREO mask is default
485		 */
486		mode = AK4613_CONFIG_GET(priv, MODE);
487		mask = mask_list[AK4613_CONFIG_MODE_STEREO][is_play * sdti_num];
488	}
489
490	if (WARN_ON(mode >= MODE_MAX))
491		return;
492
493	/* add each mode mask */
494	mask |= mask_list[mode][is_play * sdti_num];
495
496	constraint		= &priv->constraint_channels;
497	constraint->list	= ak4613_channels;
498	constraint->mask	= mask;
499	constraint->count	= sizeof(ak4613_channels);
500	snd_pcm_hw_constraint_list(runtime, 0,
501				   SNDRV_PCM_HW_PARAM_CHANNELS, constraint);
502}
503
504static int ak4613_dai_startup(struct snd_pcm_substream *substream,
505			      struct snd_soc_dai *dai)
506{
507	struct snd_soc_component *component = dai->component;
508	struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);
509
510	mutex_lock(&priv->lock);
511	ak4613_hw_constraints(priv, substream);
512	priv->cnt++;
513	mutex_unlock(&priv->lock);
 
514
515	return 0;
516}
517
518static int ak4613_dai_set_sysclk(struct snd_soc_dai *codec_dai,
519				 int clk_id, unsigned int freq, int dir)
520{
521	struct snd_soc_component *component = codec_dai->component;
522	struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);
523
524	priv->sysclk = freq;
525
526	return 0;
527}
528
529static int ak4613_dai_set_fmt(struct snd_soc_dai *dai, unsigned int format)
530{
531	struct snd_soc_component *component = dai->component;
532	struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);
533	unsigned int fmt;
534
535	fmt = format & SND_SOC_DAIFMT_FORMAT_MASK;
 
536	switch (fmt) {
537	case SND_SOC_DAIFMT_LEFT_J:
538	case SND_SOC_DAIFMT_I2S:
539		priv->fmt = fmt;
540		break;
541	default:
542		return -EINVAL;
543	}
544
545	fmt = format & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK;
546	switch (fmt) {
547	case SND_SOC_DAIFMT_CBC_CFC:
548		break;
549	default:
550		/*
551		 * SUPPORTME
552		 *
553		 * "clock provider" is not yet supperted
554		 */
555		return -EINVAL;
556	}
557
558	return 0;
559}
560
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
561static int ak4613_dai_hw_params(struct snd_pcm_substream *substream,
562				struct snd_pcm_hw_params *params,
563				struct snd_soc_dai *dai)
564{
565	struct snd_soc_component *component = dai->component;
566	struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);
 
567	struct device *dev = component->dev;
568	unsigned int width = params_width(params);
569	unsigned int fmt = priv->fmt;
570	unsigned int rate;
 
571	int i, ret;
572	u8 ctrl2;
573
574	rate = params_rate(params);
575	switch (rate) {
576	case 32000:
577	case 44100:
578	case 48000:
579		ctrl2 = DFS_NORMAL_SPEED;
580		break;
581	case 64000:
582	case 88200:
583	case 96000:
584		ctrl2 = DFS_DOUBLE_SPEED;
585		break;
586	case 176400:
587	case 192000:
588		ctrl2 = DFS_QUAD_SPEED;
589		break;
590	default:
591		return -EINVAL;
592	}
593	priv->rate = rate;
594
595	/*
596	 * FIXME
597	 *
598	 * It doesn't have full TDM suppert yet
599	 */
 
600	ret = -EINVAL;
 
601
602	mutex_lock(&priv->lock);
603	if (priv->cnt > 1) {
604		/*
605		 * If it was already working, use current priv->ctrl1
606		 */
607		ret = 0;
608	} else {
609		/*
610		 * It is not yet working,
611		 */
612		unsigned int channel = params_channels(params);
613		u8 tdm;
614
615		/* STEREO or TDM */
616		if (channel == 2)
617			tdm = AK4613_CONFIG_MODE_STEREO;
618		else
619			tdm = AK4613_CONFIG_GET(priv, MODE);
620
621		for (i = ARRAY_SIZE(ak4613_iface) - 1; i >= 0; i--) {
622			const struct ak4613_interface *iface = ak4613_iface + i;
623
624			if ((iface->fmt == fmt) && (iface->width == width)) {
625				/*
626				 * Ctrl1
627				 * | D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0  |
628				 * |TDM1|TDM0|DIF2|DIF1|DIF0|ATS1|ATS0|SMUTE|
629				 *  <  tdm  > < iface->dif >
630				 */
631				priv->ctrl1 = (tdm << 6) | (iface->dif << 3);
632				ret = 0;
633				break;
634			}
635		}
636	}
 
 
 
 
 
 
637	mutex_unlock(&priv->lock);
638
639	if (ret < 0)
640		goto hw_params_end;
641
642	snd_soc_component_update_bits(component, CTRL1, FMT_MASK, priv->ctrl1);
 
 
643	snd_soc_component_update_bits(component, CTRL2, DFS_MASK, ctrl2);
644
645	snd_soc_component_update_bits(component, ICTRL, ICTRL_MASK, priv->ic);
646	snd_soc_component_update_bits(component, OCTRL, OCTRL_MASK, priv->oc);
647
648hw_params_end:
649	if (ret < 0)
650		dev_warn(dev, "unsupported data width/format combination\n");
651
652	return ret;
653}
654
655static int ak4613_set_bias_level(struct snd_soc_component *component,
656				 enum snd_soc_bias_level level)
657{
658	u8 mgmt1 = 0;
659
660	switch (level) {
661	case SND_SOC_BIAS_ON:
662		mgmt1 |= RSTN;
663		fallthrough;
664	case SND_SOC_BIAS_PREPARE:
665		mgmt1 |= PMADC | PMDAC;
666		fallthrough;
667	case SND_SOC_BIAS_STANDBY:
668		mgmt1 |= PMVR;
669		fallthrough;
670	case SND_SOC_BIAS_OFF:
671	default:
672		break;
673	}
674
675	snd_soc_component_write(component, PW_MGMT1, mgmt1);
676
677	return 0;
678}
679
680static void ak4613_dummy_write(struct work_struct *work)
681{
682	struct ak4613_priv *priv = container_of(work,
683						struct ak4613_priv,
684						dummy_write_work);
685	struct snd_soc_component *component = priv->component;
686	unsigned int mgmt1;
687	unsigned int mgmt3;
688
689	/*
690	 * PW_MGMT1 / PW_MGMT3 needs dummy write at least after 5 LR clocks
691	 *
692	 * Note
693	 *
694	 * To avoid extra delay, we want to avoid preemption here,
695	 * but we can't. Because it uses I2C access which is using IRQ
696	 * and sleep. Thus, delay might be more than 5 LR clocks
697	 * see also
698	 *	ak4613_dai_trigger()
699	 */
700	udelay(5000000 / priv->rate);
701
702	mgmt1 = snd_soc_component_read(component, PW_MGMT1);
703	mgmt3 = snd_soc_component_read(component, PW_MGMT3);
704
705	snd_soc_component_write(component, PW_MGMT1, mgmt1);
706	snd_soc_component_write(component, PW_MGMT3, mgmt3);
707}
708
709static int ak4613_dai_trigger(struct snd_pcm_substream *substream, int cmd,
710			      struct snd_soc_dai *dai)
711{
712	struct snd_soc_component *component = dai->component;
713	struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);
714
715	/*
716	 * FIXME
717	 *
718	 * PW_MGMT1 / PW_MGMT3 needs dummy write at least after 5 LR clocks
719	 * from Power Down Release. Otherwise, Playback volume will be 0dB.
720	 * To avoid complex multiple delay/dummy_write method from
721	 * ak4613_set_bias_level() / SND_SOC_DAPM_DAC_E("DACx", ...),
722	 * call it once here.
723	 *
724	 * But, unfortunately, we can't "write" here because here is atomic
725	 * context (It uses I2C access for writing).
726	 * Thus, use schedule_work() to switching to normal context
727	 * immediately.
728	 *
729	 * Note
730	 *
731	 * Calling ak4613_dummy_write() function might be delayed.
732	 * In such case, ak4613 volume might be temporarily 0dB when
733	 * beggining of playback.
734	 * see also
735	 *	ak4613_dummy_write()
736	 */
737
738	if ((cmd != SNDRV_PCM_TRIGGER_START) &&
739	    (cmd != SNDRV_PCM_TRIGGER_RESUME))
740		return 0;
741
742	if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
743		return  0;
744
745	priv->component = component;
746	schedule_work(&priv->dummy_write_work);
747
748	return 0;
749}
750
751/*
752 * Select below from Sound Card, not Auto
753 *	SND_SOC_DAIFMT_CBC_CFC
754 *	SND_SOC_DAIFMT_CBP_CFP
755 */
756static const u64 ak4613_dai_formats =
757	SND_SOC_POSSIBLE_DAIFMT_I2S	|
758	SND_SOC_POSSIBLE_DAIFMT_LEFT_J;
759
760static const struct snd_soc_dai_ops ak4613_dai_ops = {
761	.startup	= ak4613_dai_startup,
762	.shutdown	= ak4613_dai_shutdown,
763	.set_sysclk	= ak4613_dai_set_sysclk,
764	.set_fmt	= ak4613_dai_set_fmt,
765	.trigger	= ak4613_dai_trigger,
766	.hw_params	= ak4613_dai_hw_params,
767	.auto_selectable_formats	= &ak4613_dai_formats,
768	.num_auto_selectable_formats	= 1,
769};
770
771#define AK4613_PCM_RATE		(SNDRV_PCM_RATE_32000  |\
772				 SNDRV_PCM_RATE_44100  |\
773				 SNDRV_PCM_RATE_48000  |\
774				 SNDRV_PCM_RATE_64000  |\
775				 SNDRV_PCM_RATE_88200  |\
776				 SNDRV_PCM_RATE_96000  |\
777				 SNDRV_PCM_RATE_176400 |\
778				 SNDRV_PCM_RATE_192000)
779#define AK4613_PCM_FMTBIT	(SNDRV_PCM_FMTBIT_S24_LE)
780
781static struct snd_soc_dai_driver ak4613_dai = {
782	.name = "ak4613-hifi",
783	.playback = {
784		.stream_name	= "Playback",
785		.channels_min	= 2,
786		.channels_max	= 12,
787		.rates		= AK4613_PCM_RATE,
788		.formats	= AK4613_PCM_FMTBIT,
789	},
790	.capture = {
791		.stream_name	= "Capture",
792		.channels_min	= 2,
793		.channels_max	= 4,
794		.rates		= AK4613_PCM_RATE,
795		.formats	= AK4613_PCM_FMTBIT,
796	},
797	.ops = &ak4613_dai_ops,
798	.symmetric_rate = 1,
799};
800
801static int ak4613_suspend(struct snd_soc_component *component)
802{
803	struct regmap *regmap = dev_get_regmap(component->dev, NULL);
804
805	regcache_cache_only(regmap, true);
806	regcache_mark_dirty(regmap);
807	return 0;
808}
809
810static int ak4613_resume(struct snd_soc_component *component)
811{
812	struct regmap *regmap = dev_get_regmap(component->dev, NULL);
813
814	regcache_cache_only(regmap, false);
815	return regcache_sync(regmap);
816}
817
818static const struct snd_soc_component_driver soc_component_dev_ak4613 = {
819	.suspend		= ak4613_suspend,
820	.resume			= ak4613_resume,
821	.set_bias_level		= ak4613_set_bias_level,
822	.controls		= ak4613_snd_controls,
823	.num_controls		= ARRAY_SIZE(ak4613_snd_controls),
824	.dapm_widgets		= ak4613_dapm_widgets,
825	.num_dapm_widgets	= ARRAY_SIZE(ak4613_dapm_widgets),
826	.dapm_routes		= ak4613_intercon,
827	.num_dapm_routes	= ARRAY_SIZE(ak4613_intercon),
828	.idle_bias_on		= 1,
829	.endianness		= 1,
 
830};
831
832static void ak4613_parse_of(struct ak4613_priv *priv,
833			    struct device *dev)
834{
835	struct device_node *np = dev->of_node;
836	char prop[32];
837	int sdti_num;
838	int i;
839
840	/* Input 1 - 2 */
841	for (i = 0; i < 2; i++) {
842		snprintf(prop, sizeof(prop), "asahi-kasei,in%d-single-end", i + 1);
843		if (!of_property_read_bool(np, prop))
844			priv->ic |= 1 << i;
845	}
846
847	/* Output 1 - 6 */
848	for (i = 0; i < 6; i++) {
849		snprintf(prop, sizeof(prop), "asahi-kasei,out%d-single-end", i + 1);
850		if (!of_property_read_bool(np, prop))
851			priv->oc |= 1 << i;
852	}
853
854	/*
855	 * enable TDM256 test
856	 *
857	 * !!! FIXME !!!
858	 *
859	 * It should be configured by DT or other way
860	 * if it was full supported.
861	 * But it is using ifdef style for now for test
862	 * purpose.
863	 */
864#if defined(AK4613_ENABLE_TDM_TEST)
865	AK4613_CONFIG_SET(priv, MODE_TDM256);
866#endif
867
868	/*
869	 * connected STDI
870	 * TDM support is assuming it is probed via Audio-Graph-Card style here.
871	 * Default is SDTIx1 if it was probed via Simple-Audio-Card for now.
872	 */
873	sdti_num = of_graph_get_endpoint_count(np);
874	if ((sdti_num >= SDTx_MAX) || (sdti_num < 1))
875		sdti_num = 1;
876
877	AK4613_CONFIG_SDTI_set(priv, sdti_num);
878}
879
880static int ak4613_i2c_probe(struct i2c_client *i2c)
 
881{
882	struct device *dev = &i2c->dev;
 
883	const struct regmap_config *regmap_cfg;
884	struct regmap *regmap;
885	struct ak4613_priv *priv;
886
887	regmap_cfg = i2c_get_match_data(i2c);
 
 
 
 
 
 
 
 
 
 
888	if (!regmap_cfg)
889		return -EINVAL;
890
891	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
892	if (!priv)
893		return -ENOMEM;
894
895	ak4613_parse_of(priv, dev);
896
897	priv->ctrl1		= 0;
898	priv->cnt		= 0;
899	priv->sysclk		= 0;
900	INIT_WORK(&priv->dummy_write_work, ak4613_dummy_write);
901
902	mutex_init(&priv->lock);
903
904	i2c_set_clientdata(i2c, priv);
905
906	regmap = devm_regmap_init_i2c(i2c, regmap_cfg);
907	if (IS_ERR(regmap))
908		return PTR_ERR(regmap);
909
910	return devm_snd_soc_register_component(dev, &soc_component_dev_ak4613,
911				      &ak4613_dai, 1);
912}
913
 
 
 
 
 
914static struct i2c_driver ak4613_i2c_driver = {
915	.driver = {
916		.name = "ak4613-codec",
917		.of_match_table = ak4613_of_match,
918	},
919	.probe		= ak4613_i2c_probe,
 
920	.id_table	= ak4613_i2c_id,
921};
922
923module_i2c_driver(ak4613_i2c_driver);
924
925MODULE_DESCRIPTION("Soc AK4613 driver");
926MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
927MODULE_LICENSE("GPL v2");
v4.17
 
 
 
 
 
 
 
 
 
 
 
 
  1/*
  2 * ak4613.c  --  Asahi Kasei ALSA Soc Audio driver
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  3 *
  4 * Copyright (C) 2015 Renesas Electronics Corporation
  5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  6 *
  7 * Based on ak4642.c by Kuninori Morimoto
  8 * Based on wm8731.c by Richard Purdie
  9 * Based on ak4535.c by Richard Purdie
 10 * Based on wm8753.c by Liam Girdwood
 11 *
 12 * This program is free software; you can redistribute it and/or modify
 13 * it under the terms of the GNU General Public License version 2 as
 14 * published by the Free Software Foundation.
 
 
 
 
 
 15 */
 16
 17#include <linux/clk.h>
 18#include <linux/delay.h>
 19#include <linux/i2c.h>
 20#include <linux/slab.h>
 21#include <linux/of_device.h>
 
 22#include <linux/module.h>
 23#include <linux/regmap.h>
 24#include <sound/soc.h>
 25#include <sound/pcm_params.h>
 26#include <sound/tlv.h>
 27
 28#define PW_MGMT1	0x00 /* Power Management 1 */
 29#define PW_MGMT2	0x01 /* Power Management 2 */
 30#define PW_MGMT3	0x02 /* Power Management 3 */
 31#define CTRL1		0x03 /* Control 1 */
 32#define CTRL2		0x04 /* Control 2 */
 33#define DEMP1		0x05 /* De-emphasis1 */
 34#define DEMP2		0x06 /* De-emphasis2 */
 35#define OFD		0x07 /* Overflow Detect */
 36#define ZRD		0x08 /* Zero Detect */
 37#define ICTRL		0x09 /* Input Control */
 38#define OCTRL		0x0a /* Output Control */
 39#define LOUT1		0x0b /* LOUT1 Volume Control */
 40#define ROUT1		0x0c /* ROUT1 Volume Control */
 41#define LOUT2		0x0d /* LOUT2 Volume Control */
 42#define ROUT2		0x0e /* ROUT2 Volume Control */
 43#define LOUT3		0x0f /* LOUT3 Volume Control */
 44#define ROUT3		0x10 /* ROUT3 Volume Control */
 45#define LOUT4		0x11 /* LOUT4 Volume Control */
 46#define ROUT4		0x12 /* ROUT4 Volume Control */
 47#define LOUT5		0x13 /* LOUT5 Volume Control */
 48#define ROUT5		0x14 /* ROUT5 Volume Control */
 49#define LOUT6		0x15 /* LOUT6 Volume Control */
 50#define ROUT6		0x16 /* ROUT6 Volume Control */
 51
 52/* PW_MGMT1 */
 53#define RSTN		BIT(0)
 54#define PMDAC		BIT(1)
 55#define PMADC		BIT(2)
 56#define PMVR		BIT(3)
 57
 58/* PW_MGMT2 */
 59#define PMAD_ALL	0x7
 60
 61/* PW_MGMT3 */
 62#define PMDA_ALL	0x3f
 63
 64/* CTRL1 */
 65#define DIF0		BIT(3)
 66#define DIF1		BIT(4)
 67#define DIF2		BIT(5)
 68#define TDM0		BIT(6)
 69#define TDM1		BIT(7)
 70#define NO_FMT		(0xff)
 71#define FMT_MASK	(0xf8)
 72
 73/* CTRL2 */
 74#define DFS_MASK		(3 << 2)
 75#define DFS_NORMAL_SPEED	(0 << 2)
 76#define DFS_DOUBLE_SPEED	(1 << 2)
 77#define DFS_QUAD_SPEED		(2 << 2)
 78
 79/* ICTRL */
 80#define ICTRL_MASK	(0x3)
 81
 82/* OCTRL */
 83#define OCTRL_MASK	(0x3F)
 84
 85struct ak4613_formats {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 86	unsigned int width;
 87	unsigned int fmt;
 88};
 89
 90struct ak4613_interface {
 91	struct ak4613_formats capture;
 92	struct ak4613_formats playback;
 93};
 94
 95struct ak4613_priv {
 96	struct mutex lock;
 97	const struct ak4613_interface *iface;
 98	struct snd_pcm_hw_constraint_list constraint;
 99	struct work_struct dummy_write_work;
100	struct snd_soc_component *component;
101	unsigned int rate;
102	unsigned int sysclk;
103
104	unsigned int fmt;
 
 
 
105	u8 oc;
106	u8 ic;
107	int cnt;
108};
109
110/*
111 * Playback Volume
112 *
113 * max : 0x00 : 0 dB
114 *       ( 0.5 dB step )
115 * min : 0xFE : -127.0 dB
116 * mute: 0xFF
117 */
118static const DECLARE_TLV_DB_SCALE(out_tlv, -12750, 50, 1);
119
120static const struct snd_kcontrol_new ak4613_snd_controls[] = {
121	SOC_DOUBLE_R_TLV("Digital Playback Volume1", LOUT1, ROUT1,
122			 0, 0xFF, 1, out_tlv),
123	SOC_DOUBLE_R_TLV("Digital Playback Volume2", LOUT2, ROUT2,
124			 0, 0xFF, 1, out_tlv),
125	SOC_DOUBLE_R_TLV("Digital Playback Volume3", LOUT3, ROUT3,
126			 0, 0xFF, 1, out_tlv),
127	SOC_DOUBLE_R_TLV("Digital Playback Volume4", LOUT4, ROUT4,
128			 0, 0xFF, 1, out_tlv),
129	SOC_DOUBLE_R_TLV("Digital Playback Volume5", LOUT5, ROUT5,
130			 0, 0xFF, 1, out_tlv),
131	SOC_DOUBLE_R_TLV("Digital Playback Volume6", LOUT6, ROUT6,
132			 0, 0xFF, 1, out_tlv),
133};
134
135static const struct reg_default ak4613_reg[] = {
136	{ 0x0,  0x0f }, { 0x1,  0x07 }, { 0x2,  0x3f }, { 0x3,  0x20 },
137	{ 0x4,  0x20 }, { 0x5,  0x55 }, { 0x6,  0x05 }, { 0x7,  0x07 },
138	{ 0x8,  0x0f }, { 0x9,  0x07 }, { 0xa,  0x3f }, { 0xb,  0x00 },
139	{ 0xc,  0x00 }, { 0xd,  0x00 }, { 0xe,  0x00 }, { 0xf,  0x00 },
140	{ 0x10, 0x00 }, { 0x11, 0x00 }, { 0x12, 0x00 }, { 0x13, 0x00 },
141	{ 0x14, 0x00 }, { 0x15, 0x00 }, { 0x16, 0x00 },
142};
143
144#define AUDIO_IFACE_TO_VAL(fmts) ((fmts - ak4613_iface) << 3)
145#define AUDIO_IFACE(b, fmt) { b, SND_SOC_DAIFMT_##fmt }
 
 
 
 
 
 
 
 
 
146static const struct ak4613_interface ak4613_iface[] = {
147	/* capture */				/* playback */
148	/* [0] - [2] are not supported */
149	[3] = {	AUDIO_IFACE(24, LEFT_J),	AUDIO_IFACE(24, LEFT_J) },
150	[4] = {	AUDIO_IFACE(24, I2S),		AUDIO_IFACE(24, I2S) },
151};
 
152
153static const struct regmap_config ak4613_regmap_cfg = {
154	.reg_bits		= 8,
155	.val_bits		= 8,
156	.max_register		= 0x16,
157	.reg_defaults		= ak4613_reg,
158	.num_reg_defaults	= ARRAY_SIZE(ak4613_reg),
159	.cache_type		= REGCACHE_RBTREE,
160};
161
162static const struct of_device_id ak4613_of_match[] = {
163	{ .compatible = "asahi-kasei,ak4613",	.data = &ak4613_regmap_cfg },
164	{},
165};
166MODULE_DEVICE_TABLE(of, ak4613_of_match);
167
168static const struct i2c_device_id ak4613_i2c_id[] = {
169	{ "ak4613", (kernel_ulong_t)&ak4613_regmap_cfg },
170	{ }
171};
172MODULE_DEVICE_TABLE(i2c, ak4613_i2c_id);
173
174static const struct snd_soc_dapm_widget ak4613_dapm_widgets[] = {
175
176	/* Outputs */
177	SND_SOC_DAPM_OUTPUT("LOUT1"),
178	SND_SOC_DAPM_OUTPUT("LOUT2"),
179	SND_SOC_DAPM_OUTPUT("LOUT3"),
180	SND_SOC_DAPM_OUTPUT("LOUT4"),
181	SND_SOC_DAPM_OUTPUT("LOUT5"),
182	SND_SOC_DAPM_OUTPUT("LOUT6"),
183
184	SND_SOC_DAPM_OUTPUT("ROUT1"),
185	SND_SOC_DAPM_OUTPUT("ROUT2"),
186	SND_SOC_DAPM_OUTPUT("ROUT3"),
187	SND_SOC_DAPM_OUTPUT("ROUT4"),
188	SND_SOC_DAPM_OUTPUT("ROUT5"),
189	SND_SOC_DAPM_OUTPUT("ROUT6"),
190
191	/* Inputs */
192	SND_SOC_DAPM_INPUT("LIN1"),
193	SND_SOC_DAPM_INPUT("LIN2"),
194
195	SND_SOC_DAPM_INPUT("RIN1"),
196	SND_SOC_DAPM_INPUT("RIN2"),
197
198	/* DAC */
199	SND_SOC_DAPM_DAC("DAC1", NULL, PW_MGMT3, 0, 0),
200	SND_SOC_DAPM_DAC("DAC2", NULL, PW_MGMT3, 1, 0),
201	SND_SOC_DAPM_DAC("DAC3", NULL, PW_MGMT3, 2, 0),
202	SND_SOC_DAPM_DAC("DAC4", NULL, PW_MGMT3, 3, 0),
203	SND_SOC_DAPM_DAC("DAC5", NULL, PW_MGMT3, 4, 0),
204	SND_SOC_DAPM_DAC("DAC6", NULL, PW_MGMT3, 5, 0),
205
206	/* ADC */
207	SND_SOC_DAPM_ADC("ADC1", NULL, PW_MGMT2, 0, 0),
208	SND_SOC_DAPM_ADC("ADC2", NULL, PW_MGMT2, 1, 0),
209};
210
211static const struct snd_soc_dapm_route ak4613_intercon[] = {
212	{"LOUT1", NULL, "DAC1"},
213	{"LOUT2", NULL, "DAC2"},
214	{"LOUT3", NULL, "DAC3"},
215	{"LOUT4", NULL, "DAC4"},
216	{"LOUT5", NULL, "DAC5"},
217	{"LOUT6", NULL, "DAC6"},
218
219	{"ROUT1", NULL, "DAC1"},
220	{"ROUT2", NULL, "DAC2"},
221	{"ROUT3", NULL, "DAC3"},
222	{"ROUT4", NULL, "DAC4"},
223	{"ROUT5", NULL, "DAC5"},
224	{"ROUT6", NULL, "DAC6"},
225
226	{"DAC1", NULL, "Playback"},
227	{"DAC2", NULL, "Playback"},
228	{"DAC3", NULL, "Playback"},
229	{"DAC4", NULL, "Playback"},
230	{"DAC5", NULL, "Playback"},
231	{"DAC6", NULL, "Playback"},
232
233	{"Capture", NULL, "ADC1"},
234	{"Capture", NULL, "ADC2"},
235
236	{"ADC1", NULL, "LIN1"},
237	{"ADC2", NULL, "LIN2"},
238
239	{"ADC1", NULL, "RIN1"},
240	{"ADC2", NULL, "RIN2"},
241};
242
243static void ak4613_dai_shutdown(struct snd_pcm_substream *substream,
244			       struct snd_soc_dai *dai)
245{
246	struct snd_soc_component *component = dai->component;
247	struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);
248	struct device *dev = component->dev;
249
250	mutex_lock(&priv->lock);
251	priv->cnt--;
252	if (priv->cnt < 0) {
253		dev_err(dev, "unexpected counter error\n");
254		priv->cnt = 0;
255	}
256	if (!priv->cnt)
257		priv->iface = NULL;
258	mutex_unlock(&priv->lock);
259}
260
261static void ak4613_hw_constraints(struct ak4613_priv *priv,
262				  struct snd_pcm_runtime *runtime)
263{
 
264	static const unsigned int ak4613_rates[] = {
265		 32000,
266		 44100,
267		 48000,
268		 64000,
269		 88200,
270		 96000,
271		176400,
272		192000,
273	};
274	struct snd_pcm_hw_constraint_list *constraint = &priv->constraint;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
275	unsigned int fs;
 
 
276	int i;
277
 
278	constraint->list	= ak4613_rates;
279	constraint->mask	= 0;
280	constraint->count	= 0;
281
282	/*
283	 * Slave Mode
284	 *	Normal: [32kHz, 48kHz] : 256fs,384fs or 512fs
285	 *	Double: [64kHz, 96kHz] : 256fs
286	 *	Quad  : [128kHz,192kHz]: 128fs
287	 *
288	 * Master mode
289	 *	Normal: [32kHz, 48kHz] : 256fs or 512fs
290	 *	Double: [64kHz, 96kHz] : 256fs
291	 *	Quad  : [128kHz,192kHz]: 128fs
292	*/
293	for (i = 0; i < ARRAY_SIZE(ak4613_rates); i++) {
294		/* minimum fs on each range */
295		fs = (ak4613_rates[i] <= 96000) ? 256 : 128;
296
297		if (priv->sysclk >= ak4613_rates[i] * fs)
298			constraint->count = i + 1;
299	}
300
301	snd_pcm_hw_constraint_list(runtime, 0,
302				SNDRV_PCM_HW_PARAM_RATE, constraint);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
303}
304
305static int ak4613_dai_startup(struct snd_pcm_substream *substream,
306			      struct snd_soc_dai *dai)
307{
308	struct snd_soc_component *component = dai->component;
309	struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);
310
 
 
311	priv->cnt++;
312
313	ak4613_hw_constraints(priv, substream->runtime);
314
315	return 0;
316}
317
318static int ak4613_dai_set_sysclk(struct snd_soc_dai *codec_dai,
319				 int clk_id, unsigned int freq, int dir)
320{
321	struct snd_soc_component *component = codec_dai->component;
322	struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);
323
324	priv->sysclk = freq;
325
326	return 0;
327}
328
329static int ak4613_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
330{
331	struct snd_soc_component *component = dai->component;
332	struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);
 
333
334	fmt &= SND_SOC_DAIFMT_FORMAT_MASK;
335
336	switch (fmt) {
337	case SND_SOC_DAIFMT_LEFT_J:
338	case SND_SOC_DAIFMT_I2S:
339		priv->fmt = fmt;
340		break;
341	default:
342		return -EINVAL;
343	}
344
 
 
 
 
 
 
 
 
 
 
 
 
 
345	return 0;
346}
347
348static bool ak4613_dai_fmt_matching(const struct ak4613_interface *iface,
349				    int is_play,
350				    unsigned int fmt, unsigned int width)
351{
352	const struct ak4613_formats *fmts;
353
354	fmts = (is_play) ? &iface->playback : &iface->capture;
355
356	if (fmts->fmt != fmt)
357		return false;
358
359	if (fmts->width != width)
360		return false;
361
362	return true;
363}
364
365static int ak4613_dai_hw_params(struct snd_pcm_substream *substream,
366				struct snd_pcm_hw_params *params,
367				struct snd_soc_dai *dai)
368{
369	struct snd_soc_component *component = dai->component;
370	struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);
371	const struct ak4613_interface *iface;
372	struct device *dev = component->dev;
373	unsigned int width = params_width(params);
374	unsigned int fmt = priv->fmt;
375	unsigned int rate;
376	int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
377	int i, ret;
378	u8 fmt_ctrl, ctrl2;
379
380	rate = params_rate(params);
381	switch (rate) {
382	case 32000:
383	case 44100:
384	case 48000:
385		ctrl2 = DFS_NORMAL_SPEED;
386		break;
387	case 64000:
388	case 88200:
389	case 96000:
390		ctrl2 = DFS_DOUBLE_SPEED;
391		break;
392	case 176400:
393	case 192000:
394		ctrl2 = DFS_QUAD_SPEED;
395		break;
396	default:
397		return -EINVAL;
398	}
399	priv->rate = rate;
400
401	/*
402	 * FIXME
403	 *
404	 * It doesn't support TDM at this point
405	 */
406	fmt_ctrl = NO_FMT;
407	ret = -EINVAL;
408	iface = NULL;
409
410	mutex_lock(&priv->lock);
411	if (priv->iface) {
412		if (ak4613_dai_fmt_matching(priv->iface, is_play, fmt, width))
413			iface = priv->iface;
 
 
414	} else {
 
 
 
 
 
 
 
 
 
 
 
 
415		for (i = ARRAY_SIZE(ak4613_iface) - 1; i >= 0; i--) {
416			if (!ak4613_dai_fmt_matching(ak4613_iface + i,
417						     is_play,
418						     fmt, width))
419				continue;
420			iface = ak4613_iface + i;
421			break;
 
 
 
 
 
 
 
422		}
423	}
424
425	if ((priv->iface == NULL) ||
426	    (priv->iface == iface)) {
427		priv->iface = iface;
428		ret = 0;
429	}
430	mutex_unlock(&priv->lock);
431
432	if (ret < 0)
433		goto hw_params_end;
434
435	fmt_ctrl = AUDIO_IFACE_TO_VAL(iface);
436
437	snd_soc_component_update_bits(component, CTRL1, FMT_MASK, fmt_ctrl);
438	snd_soc_component_update_bits(component, CTRL2, DFS_MASK, ctrl2);
439
440	snd_soc_component_update_bits(component, ICTRL, ICTRL_MASK, priv->ic);
441	snd_soc_component_update_bits(component, OCTRL, OCTRL_MASK, priv->oc);
442
443hw_params_end:
444	if (ret < 0)
445		dev_warn(dev, "unsupported data width/format combination\n");
446
447	return ret;
448}
449
450static int ak4613_set_bias_level(struct snd_soc_component *component,
451				 enum snd_soc_bias_level level)
452{
453	u8 mgmt1 = 0;
454
455	switch (level) {
456	case SND_SOC_BIAS_ON:
457		mgmt1 |= RSTN;
458		/* fall through */
459	case SND_SOC_BIAS_PREPARE:
460		mgmt1 |= PMADC | PMDAC;
461		/* fall through */
462	case SND_SOC_BIAS_STANDBY:
463		mgmt1 |= PMVR;
464		/* fall through */
465	case SND_SOC_BIAS_OFF:
466	default:
467		break;
468	}
469
470	snd_soc_component_write(component, PW_MGMT1, mgmt1);
471
472	return 0;
473}
474
475static void ak4613_dummy_write(struct work_struct *work)
476{
477	struct ak4613_priv *priv = container_of(work,
478						struct ak4613_priv,
479						dummy_write_work);
480	struct snd_soc_component *component = priv->component;
481	unsigned int mgmt1;
482	unsigned int mgmt3;
483
484	/*
485	 * PW_MGMT1 / PW_MGMT3 needs dummy write at least after 5 LR clocks
486	 *
487	 * Note
488	 *
489	 * To avoid extra delay, we want to avoid preemption here,
490	 * but we can't. Because it uses I2C access which is using IRQ
491	 * and sleep. Thus, delay might be more than 5 LR clocks
492	 * see also
493	 *	ak4613_dai_trigger()
494	 */
495	udelay(5000000 / priv->rate);
496
497	snd_soc_component_read(component, PW_MGMT1, &mgmt1);
498	snd_soc_component_read(component, PW_MGMT3, &mgmt3);
499
500	snd_soc_component_write(component, PW_MGMT1, mgmt1);
501	snd_soc_component_write(component, PW_MGMT3, mgmt3);
502}
503
504static int ak4613_dai_trigger(struct snd_pcm_substream *substream, int cmd,
505			      struct snd_soc_dai *dai)
506{
507	struct snd_soc_component *component = dai->component;
508	struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);
509
510	/*
511	 * FIXME
512	 *
513	 * PW_MGMT1 / PW_MGMT3 needs dummy write at least after 5 LR clocks
514	 * from Power Down Release. Otherwise, Playback volume will be 0dB.
515	 * To avoid complex multiple delay/dummy_write method from
516	 * ak4613_set_bias_level() / SND_SOC_DAPM_DAC_E("DACx", ...),
517	 * call it once here.
518	 *
519	 * But, unfortunately, we can't "write" here because here is atomic
520	 * context (It uses I2C access for writing).
521	 * Thus, use schedule_work() to switching to normal context
522	 * immediately.
523	 *
524	 * Note
525	 *
526	 * Calling ak4613_dummy_write() function might be delayed.
527	 * In such case, ak4613 volume might be temporarily 0dB when
528	 * beggining of playback.
529	 * see also
530	 *	ak4613_dummy_write()
531	 */
532
533	if ((cmd != SNDRV_PCM_TRIGGER_START) &&
534	    (cmd != SNDRV_PCM_TRIGGER_RESUME))
535		return 0;
536
537	if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
538		return  0;
539
540	priv->component = component;
541	schedule_work(&priv->dummy_write_work);
542
543	return 0;
544}
545
 
 
 
 
 
 
 
 
 
546static const struct snd_soc_dai_ops ak4613_dai_ops = {
547	.startup	= ak4613_dai_startup,
548	.shutdown	= ak4613_dai_shutdown,
549	.set_sysclk	= ak4613_dai_set_sysclk,
550	.set_fmt	= ak4613_dai_set_fmt,
551	.trigger	= ak4613_dai_trigger,
552	.hw_params	= ak4613_dai_hw_params,
 
 
553};
554
555#define AK4613_PCM_RATE		(SNDRV_PCM_RATE_32000  |\
556				 SNDRV_PCM_RATE_44100  |\
557				 SNDRV_PCM_RATE_48000  |\
558				 SNDRV_PCM_RATE_64000  |\
559				 SNDRV_PCM_RATE_88200  |\
560				 SNDRV_PCM_RATE_96000  |\
561				 SNDRV_PCM_RATE_176400 |\
562				 SNDRV_PCM_RATE_192000)
563#define AK4613_PCM_FMTBIT	(SNDRV_PCM_FMTBIT_S24_LE)
564
565static struct snd_soc_dai_driver ak4613_dai = {
566	.name = "ak4613-hifi",
567	.playback = {
568		.stream_name	= "Playback",
569		.channels_min	= 2,
570		.channels_max	= 2,
571		.rates		= AK4613_PCM_RATE,
572		.formats	= AK4613_PCM_FMTBIT,
573	},
574	.capture = {
575		.stream_name	= "Capture",
576		.channels_min	= 2,
577		.channels_max	= 2,
578		.rates		= AK4613_PCM_RATE,
579		.formats	= AK4613_PCM_FMTBIT,
580	},
581	.ops = &ak4613_dai_ops,
582	.symmetric_rates = 1,
583};
584
585static int ak4613_suspend(struct snd_soc_component *component)
586{
587	struct regmap *regmap = dev_get_regmap(component->dev, NULL);
588
589	regcache_cache_only(regmap, true);
590	regcache_mark_dirty(regmap);
591	return 0;
592}
593
594static int ak4613_resume(struct snd_soc_component *component)
595{
596	struct regmap *regmap = dev_get_regmap(component->dev, NULL);
597
598	regcache_cache_only(regmap, false);
599	return regcache_sync(regmap);
600}
601
602static const struct snd_soc_component_driver soc_component_dev_ak4613 = {
603	.suspend		= ak4613_suspend,
604	.resume			= ak4613_resume,
605	.set_bias_level		= ak4613_set_bias_level,
606	.controls		= ak4613_snd_controls,
607	.num_controls		= ARRAY_SIZE(ak4613_snd_controls),
608	.dapm_widgets		= ak4613_dapm_widgets,
609	.num_dapm_widgets	= ARRAY_SIZE(ak4613_dapm_widgets),
610	.dapm_routes		= ak4613_intercon,
611	.num_dapm_routes	= ARRAY_SIZE(ak4613_intercon),
612	.idle_bias_on		= 1,
613	.endianness		= 1,
614	.non_legacy_dai_naming	= 1,
615};
616
617static void ak4613_parse_of(struct ak4613_priv *priv,
618			    struct device *dev)
619{
620	struct device_node *np = dev->of_node;
621	char prop[32];
 
622	int i;
623
624	/* Input 1 - 2 */
625	for (i = 0; i < 2; i++) {
626		snprintf(prop, sizeof(prop), "asahi-kasei,in%d-single-end", i + 1);
627		if (!of_get_property(np, prop, NULL))
628			priv->ic |= 1 << i;
629	}
630
631	/* Output 1 - 6 */
632	for (i = 0; i < 6; i++) {
633		snprintf(prop, sizeof(prop), "asahi-kasei,out%d-single-end", i + 1);
634		if (!of_get_property(np, prop, NULL))
635			priv->oc |= 1 << i;
636	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
637}
638
639static int ak4613_i2c_probe(struct i2c_client *i2c,
640			    const struct i2c_device_id *id)
641{
642	struct device *dev = &i2c->dev;
643	struct device_node *np = dev->of_node;
644	const struct regmap_config *regmap_cfg;
645	struct regmap *regmap;
646	struct ak4613_priv *priv;
647
648	regmap_cfg = NULL;
649	if (np) {
650		const struct of_device_id *of_id;
651
652		of_id = of_match_device(ak4613_of_match, dev);
653		if (of_id)
654			regmap_cfg = of_id->data;
655	} else {
656		regmap_cfg = (const struct regmap_config *)id->driver_data;
657	}
658
659	if (!regmap_cfg)
660		return -EINVAL;
661
662	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
663	if (!priv)
664		return -ENOMEM;
665
666	ak4613_parse_of(priv, dev);
667
668	priv->iface		= NULL;
669	priv->cnt		= 0;
670	priv->sysclk		= 0;
671	INIT_WORK(&priv->dummy_write_work, ak4613_dummy_write);
672
673	mutex_init(&priv->lock);
674
675	i2c_set_clientdata(i2c, priv);
676
677	regmap = devm_regmap_init_i2c(i2c, regmap_cfg);
678	if (IS_ERR(regmap))
679		return PTR_ERR(regmap);
680
681	return devm_snd_soc_register_component(dev, &soc_component_dev_ak4613,
682				      &ak4613_dai, 1);
683}
684
685static int ak4613_i2c_remove(struct i2c_client *client)
686{
687	return 0;
688}
689
690static struct i2c_driver ak4613_i2c_driver = {
691	.driver = {
692		.name = "ak4613-codec",
693		.of_match_table = ak4613_of_match,
694	},
695	.probe		= ak4613_i2c_probe,
696	.remove		= ak4613_i2c_remove,
697	.id_table	= ak4613_i2c_id,
698};
699
700module_i2c_driver(ak4613_i2c_driver);
701
702MODULE_DESCRIPTION("Soc AK4613 driver");
703MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
704MODULE_LICENSE("GPL v2");